1 /* -*- c-file-style: "linux"; -*-
3 Copyright (C) 1998-2001 by Andrew Tridgell <tridge@samba.org>
4 Copyright (C) 2000-2001 by Martin Pool <mbp@samba.org>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 Logging and utility functions.
25 Mapping to human-readable messages added by Martin Pool
26 <mbp@samba.org>, Oct 2000.
35 extern int local_server;
38 extern int msg_fd_out;
39 extern int protocol_version;
40 extern int preserve_times;
41 extern int log_format_has_o_or_i;
42 extern int daemon_log_format_has_o_or_i;
43 extern char *auth_user;
44 extern char *log_format;
46 static int log_initialised;
47 static char *logfname;
51 int log_got_error = 0;
56 } const rerr_names[] = {
57 { RERR_SYNTAX , "syntax or usage error" },
58 { RERR_PROTOCOL , "protocol incompatibility" },
59 { RERR_FILESELECT , "errors selecting input/output files, dirs" },
60 { RERR_UNSUPPORTED, "requested action not supported" },
61 { RERR_STARTCLIENT, "error starting client-server protocol" },
62 { RERR_LOG_FAILURE, "daemon unable to append to log-file" },
63 { RERR_SOCKETIO , "error in socket IO" },
64 { RERR_FILEIO , "error in file IO" },
65 { RERR_STREAMIO , "error in rsync protocol data stream" },
66 { RERR_MESSAGEIO , "errors with program diagnostics" },
67 { RERR_IPC , "error in IPC code" },
68 { RERR_SIGNAL , "received SIGUSR1 or SIGINT" },
69 { RERR_WAITCHILD , "some error returned by waitpid()" },
70 { RERR_MALLOC , "error allocating core memory buffers" },
71 { RERR_PARTIAL , "some files could not be transferred" },
72 { RERR_VANISHED , "some files vanished before they could be transferred" },
73 { RERR_TIMEOUT , "timeout in data send/receive" },
74 { RERR_CMD_FAILED , "remote shell failed" },
75 { RERR_CMD_KILLED , "remote shell killed" },
76 { RERR_CMD_RUN , "remote command could not be run" },
77 { RERR_CMD_NOTFOUND,"remote command not found" },
78 { RERR_DEL_LIMIT , "the --max-delete limit stopped deletions" },
85 * Map from rsync error code to name, or return NULL.
87 static char const *rerr_name(int code)
90 for (i = 0; rerr_names[i].name; i++) {
91 if (rerr_names[i].code == code)
92 return rerr_names[i].name;
98 static void logit(int priority, char *buf)
103 fprintf(logfile,"%s [%d] %s",
104 timestring(time(NULL)), (int)getpid(), buf);
107 syslog(priority, "%s", buf);
113 int options = LOG_PID;
120 /* this looks pointless, but it is needed in order for the
121 * C library on some systems to fetch the timezone info
122 * before the chroot */
126 /* optionally use a log file instead of syslog */
127 logfname = lp_log_file();
137 options |= LOG_NDELAY;
141 openlog("rsyncd", options, lp_syslog_facility());
143 openlog("rsyncd", options);
147 logit(LOG_INFO,"rsyncd started\n");
153 if (logfname && !logfile) {
154 extern int orig_umask;
155 int old_umask = umask(022 | orig_umask);
156 logfile = fopen(logfname, "a");
159 am_daemon = 0; /* avoid trying to log again */
160 rsyserr(FERROR, errno, "fopen() of log-file failed");
161 exit_cleanup(RERR_LOG_FAILURE);
174 /* this is the underlying (unformatted) rsync debugging function. Call
175 * it with FINFO, FERROR or FLOG */
176 void rwrite(enum logcode code, char *buf, int len)
179 /* recursion can happen with certain fatal conditions */
181 if (quiet && code == FINFO)
185 exit_cleanup(RERR_MESSAGEIO);
189 if (am_server && msg_fd_out >= 0) {
190 /* Pass the message to our sibling. */
191 send_msg((enum msgcode)code, buf, len);
197 else if (am_daemon) {
200 int priority = code == FERROR ? LOG_WARNING : LOG_INFO;
205 if (!log_initialised)
207 strlcpy(msg, buf, MIN((int)sizeof msg, len + 1));
208 logit(priority, msg);
211 if (code == FLOG || !am_server)
213 } else if (code == FLOG)
217 /* Pass the message to the non-server side. */
218 if (io_multiplex_write((enum msgcode)code, buf, len))
221 /* TODO: can we send the error to the user somehow? */
226 if (code == FERROR) {
232 f = am_server ? stderr : stdout;
235 exit_cleanup(RERR_MESSAGEIO);
237 if (fwrite(buf, len, 1, f) != 1)
238 exit_cleanup(RERR_MESSAGEIO);
240 if (buf[len-1] == '\r' || buf[len-1] == '\n')
245 /* This is the rsync debugging function. Call it with FINFO, FERROR or
247 void rprintf(enum logcode code, const char *format, ...)
250 char buf[MAXPATHLEN+512];
253 va_start(ap, format);
254 len = vsnprintf(buf, sizeof buf, format, ap);
257 /* Deal with buffer overruns. Instead of panicking, just
258 * truncate the resulting string. (Note that configure ensures
259 * that we have a vsnprintf() that doesn't ever return -1.) */
260 if (len > sizeof buf - 1) {
261 const char ellipsis[] = "[...]";
263 /* Reset length, and zero-terminate the end of our buffer */
264 len = sizeof buf - 1;
267 /* Copy the ellipsis to the end of the string, but give
268 * us one extra character:
270 * v--- null byte at buf[sizeof buf - 1]
272 * -> abcd[...]00 <-- now two null bytes at end
274 * If the input format string has a trailing newline,
275 * we copy it into that extra null; if it doesn't, well,
276 * all we lose is one byte. */
277 strncpy(buf+len-sizeof ellipsis, ellipsis, sizeof ellipsis);
278 if (format[strlen(format)-1] == '\n') {
283 rwrite(code, buf, len);
287 /* This is like rprintf, but it also tries to print some
288 * representation of the error code. Normally errcode = errno.
290 * Unlike rprintf, this always adds a newline and there should not be
291 * one in the format string.
293 * Note that since strerror might involve dynamically loading a
294 * message catalog we need to call it once before chroot-ing. */
295 void rsyserr(enum logcode code, int errcode, const char *format, ...)
298 char buf[MAXPATHLEN+512];
301 strcpy(buf, RSYNC_NAME ": ");
302 len = (sizeof RSYNC_NAME ": ") - 1;
304 va_start(ap, format);
305 len += vsnprintf(buf + len, sizeof buf - len, format, ap);
308 if (len < sizeof buf) {
309 len += snprintf(buf + len, sizeof buf - len,
310 ": %s (%d)\n", strerror(errcode), errcode);
312 if (len >= sizeof buf)
313 exit_cleanup(RERR_MESSAGEIO);
315 rwrite(code, buf, len);
320 void rflush(enum logcode code)
332 if (code == FERROR) {
343 if (!f) exit_cleanup(RERR_MESSAGEIO);
349 /* a generic logging routine for send/recv, with parameter
351 static void log_formatted(enum logcode code, char *format, char *op,
352 struct file_struct *file, struct stats *initial_stats,
353 int iflags, char *hlink)
355 char buf[MAXPATHLEN+1024], buf2[MAXPATHLEN], fmt[32];
362 /* We expand % codes one by one in place in buf. We don't
363 * copy in the terminating null of the inserted strings, but
364 * rather keep going until we reach the null of the format. */
365 total = strlcpy(buf, format, sizeof buf);
366 if (total > MAXPATHLEN) {
367 rprintf(FERROR, "log-format string is WAY too long!\n");
368 exit_cleanup(RERR_MESSAGEIO);
373 for (p = buf; (p = strchr(p, '%')) != NULL; ) {
378 while (isdigit(*(uchar*)p) && n - fmt < (int)(sizeof fmt) - 8)
386 case 'h': if (am_daemon) n = client_name(0); break;
387 case 'a': if (am_daemon) n = client_addr(0); break;
389 strlcat(fmt, ".0f", sizeof fmt);
390 snprintf(buf2, sizeof buf2, fmt,
391 (double)file->length);
395 strlcat(fmt, "ld", sizeof fmt);
396 snprintf(buf2, sizeof buf2, fmt,
400 case 'o': n = op; break;
402 n = safe_fname(f_name(file));
403 if (am_sender && file->dir.root) {
404 pathjoin(buf2, sizeof buf2,
406 /* The buffer from safe_fname() has more
407 * room than MAXPATHLEN, so this is safe. */
418 n = safe_fname(f_name(file));
419 if (S_ISDIR(file->mode)) {
420 /* The buffer from safe_fname() has more
421 * room than MAXPATHLEN, so this is safe. */
426 if (hlink && *hlink) {
427 n = safe_fname(hlink);
428 strcpy(buf2, " => ");
429 } else if (S_ISLNK(file->mode) && file->u.link) {
430 n = safe_fname(file->u.link);
431 strcpy(buf2, " -> ");
438 strlcat(fmt, "s", sizeof fmt);
439 snprintf(buf2 + 4, sizeof buf2 - 4, fmt, n);
442 case 'm': n = lp_name(module_id); break;
443 case 't': n = timestring(time(NULL)); break;
444 case 'P': n = lp_path(module_id); break;
445 case 'u': n = auth_user; break;
448 b = stats.total_written -
449 initial_stats->total_written;
451 b = stats.total_read -
452 initial_stats->total_read;
454 strlcat(fmt, ".0f", sizeof fmt);
455 snprintf(buf2, sizeof buf2, fmt, (double)b);
460 b = stats.total_written -
461 initial_stats->total_written;
463 b = stats.total_read -
464 initial_stats->total_read;
466 strlcat(fmt, ".0f", sizeof fmt);
467 snprintf(buf2, sizeof buf2, fmt, (double)b);
471 if (iflags & ITEM_DELETED) {
475 n = buf2 + MAXPATHLEN - 32;
476 n[0] = iflags & ITEM_LOCAL_CHANGE
477 ? iflags & ITEM_XNAME_FOLLOWS ? 'h' : 'c'
478 : !(iflags & ITEM_TRANSFER) ? '.'
479 : !local_server && *op == 's' ? '<' : '>';
480 n[1] = S_ISDIR(file->mode) ? 'd'
481 : IS_DEVICE(file->mode) ? 'D'
482 : S_ISLNK(file->mode) ? 'L' : 'f';
483 n[2] = !(iflags & ITEM_REPORT_CHECKSUM) ? '.' : 'c';
484 n[3] = !(iflags & ITEM_REPORT_SIZE) ? '.' : 's';
485 n[4] = !(iflags & ITEM_REPORT_TIME) ? '.'
486 : !preserve_times || IS_DEVICE(file->mode)
487 || S_ISLNK(file->mode) ? 'T' : 't';
488 n[5] = !(iflags & ITEM_REPORT_PERMS) ? '.' : 'p';
489 n[6] = !(iflags & ITEM_REPORT_OWNER) ? '.' : 'o';
490 n[7] = !(iflags & ITEM_REPORT_GROUP) ? '.' : 'g';
491 n[8] = !(iflags & ITEM_REPORT_XATTRS) ? '.' : 'a';
494 if (iflags & (ITEM_IS_NEW|ITEM_MISSING_DATA)) {
495 char ch = iflags & ITEM_IS_NEW ? '+' : '?';
497 for (i = 2; n[i]; i++)
499 } else if (!(iflags & (ITEM_TRANSFER|ITEM_LOCAL_CHANGE))) {
501 for (i = 2; n[i]; i++) {
506 for (i = 2; n[i]; i++)
513 /* "n" is the string to be inserted in place of this % code. */
516 if (n != buf2 && fmt[1]) {
517 strlcat(fmt, "s", sizeof fmt);
518 snprintf(buf2, sizeof buf2, fmt, n);
523 /* Subtract the length of the escape from the string's size. */
526 if (len + total >= (size_t)sizeof buf) {
528 "buffer overflow expanding %%%c -- exiting\n",
530 exit_cleanup(RERR_MESSAGEIO);
533 /* Shuffle the rest of the string along to make space for n */
534 if (len != (size_t)(p - s + 1))
535 memmove(s + len, p + 1, total - (s - buf) + 1);
538 /* Insert the contents of string "n", but NOT its null. */
542 /* Skip over inserted string; continue looking */
546 rwrite(code, buf, total);
549 /* Return 1 if the format escape is in the log-format string (e.g. look for
550 * the 'b' in the "%9b" format escape). */
551 int log_format_has(const char *format, char esc)
558 for (p = format; (p = strchr(p, '%')) != NULL; ) {
561 while (isdigit(*(uchar*)p))
571 /* log the transfer of a file */
572 void log_item(struct file_struct *file, struct stats *initial_stats,
573 int iflags, char *hlink)
575 char *s_or_r = am_sender ? "send" : "recv";
577 if (lp_transfer_logging(module_id)) {
578 log_formatted(FLOG, lp_log_format(module_id), s_or_r,
579 file, initial_stats, iflags, hlink);
580 } else if (log_format && !am_server) {
581 log_formatted(FINFO, log_format, s_or_r,
582 file, initial_stats, iflags, hlink);
586 void maybe_log_item(struct file_struct *file, int iflags, int itemizing,
589 int see_item = itemizing && (iflags || verbose > 1);
591 if (am_daemon && !dry_run && see_item)
592 log_item(file, &stats, iflags, buf);
593 } else if (see_item || iflags & ITEM_LOCAL_CHANGE || *buf
594 || (S_ISDIR(file->mode) && iflags & SIGNIFICANT_ITEM_FLAGS))
595 log_item(file, &stats, iflags, buf);
598 void log_delete(char *fname, int mode)
600 static struct file_struct file;
601 int len = strlen(fname);
605 file.basename = fname;
607 if (!verbose && !log_format)
609 else if (am_server && protocol_version >= 29 && len < MAXPATHLEN) {
611 len++; /* directories include trailing null */
612 send_msg(MSG_DELETED, fname, len);
614 fmt = log_format_has_o_or_i ? log_format : "deleting %n";
615 log_formatted(FCLIENT, fmt, "del.", &file, &stats,
619 if (!am_daemon || dry_run || !lp_transfer_logging(module_id))
622 fmt = daemon_log_format_has_o_or_i ? lp_log_format(module_id) : "deleting %n";
623 log_formatted(FLOG, fmt, "del.", &file, &stats, ITEM_DELETED, NULL);
628 * Called when the transfer is interrupted for some reason.
630 * Code is one of the RERR_* codes from errcode.h, or terminating
633 void log_exit(int code, const char *file, int line)
636 rprintf(FLOG,"sent %.0f bytes received %.0f bytes total size %.0f\n",
637 (double)stats.total_written,
638 (double)stats.total_read,
639 (double)stats.total_size);
643 name = rerr_name(code);
645 name = "unexplained error";
647 /* VANISHED is not an error, only a warning */
648 if (code == RERR_VANISHED) {
649 rprintf(FINFO, "rsync warning: %s (code %d) at %s(%d)\n",
650 name, code, file, line);
652 rprintf(FERROR, "rsync error: %s (code %d) at %s(%d)\n",
653 name, code, file, line);