2 * Logging and utility functions.
4 * Copyright (C) 1998-2001 Andrew Tridgell <tridge@samba.org>
5 * Copyright (C) 2000-2001 Martin Pool <mbp@samba.org>
6 * Copyright (C) 2003-2007 Wayne Davison
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, visit the http://fsf.org website.
29 extern int am_generator;
30 extern int local_server;
33 extern int msg_fd_out;
34 extern int allow_8bit_chars;
35 extern int protocol_version;
36 extern int preserve_times;
39 extern int stdout_format_has_i;
40 extern int stdout_format_has_o_or_i;
41 extern int logfile_format_has_i;
42 extern int logfile_format_has_o_or_i;
43 extern mode_t orig_umask;
44 extern char *auth_user;
45 extern char *stdout_format;
46 extern char *logfile_format;
47 extern char *logfile_name;
49 extern iconv_t ic_chck;
52 extern iconv_t ic_send, ic_recv;
54 extern char curr_dir[];
55 extern char *module_dir;
56 extern unsigned int module_dirlen;
58 static int log_initialised;
59 static int logfile_was_closed;
60 static FILE *logfile_fp;
63 int log_got_error = 0;
68 } const rerr_names[] = {
69 { RERR_SYNTAX , "syntax or usage error" },
70 { RERR_PROTOCOL , "protocol incompatibility" },
71 { RERR_FILESELECT , "errors selecting input/output files, dirs" },
72 { RERR_UNSUPPORTED, "requested action not supported" },
73 { RERR_STARTCLIENT, "error starting client-server protocol" },
74 { RERR_SOCKETIO , "error in socket IO" },
75 { RERR_FILEIO , "error in file IO" },
76 { RERR_STREAMIO , "error in rsync protocol data stream" },
77 { RERR_MESSAGEIO , "errors with program diagnostics" },
78 { RERR_IPC , "error in IPC code" },
79 { RERR_CRASHED , "sibling process crashed" },
80 { RERR_TERMINATED , "sibling process terminated abnormally" },
81 { RERR_SIGNAL1 , "received SIGUSR1" },
82 { RERR_SIGNAL , "received SIGINT, SIGTERM, or SIGHUP" },
83 { RERR_WAITCHILD , "waitpid() failed" },
84 { RERR_MALLOC , "error allocating core memory buffers" },
85 { RERR_PARTIAL , "some files could not be transferred" },
86 { RERR_VANISHED , "some files vanished before they could be transferred" },
87 { RERR_TIMEOUT , "timeout in data send/receive" },
88 { RERR_CMD_FAILED , "remote shell failed" },
89 { RERR_CMD_KILLED , "remote shell killed" },
90 { RERR_CMD_RUN , "remote command could not be run" },
91 { RERR_CMD_NOTFOUND,"remote command not found" },
92 { RERR_DEL_LIMIT , "the --max-delete limit stopped deletions" },
97 * Map from rsync error code to name, or return NULL.
99 static char const *rerr_name(int code)
102 for (i = 0; rerr_names[i].name; i++) {
103 if (rerr_names[i].code == code)
104 return rerr_names[i].name;
109 static void logit(int priority, const char *buf)
111 if (logfile_was_closed)
114 fprintf(logfile_fp, "%s [%d] %s",
115 timestring(time(NULL)), (int)getpid(), buf);
118 syslog(priority, "%s", buf);
122 static void syslog_init()
124 static int been_here = 0;
125 int options = LOG_PID;
132 options |= LOG_NDELAY;
136 openlog("rsyncd", options, lp_syslog_facility(module_id));
138 openlog("rsyncd", options);
142 logit(LOG_INFO, "rsyncd started\n");
146 static void logfile_open(void)
148 mode_t old_umask = umask(022 | orig_umask);
149 logfile_fp = fopen(logfile_name, "a");
152 int fopen_errno = errno;
153 /* Rsync falls back to using syslog on failure. */
155 rsyserr(FERROR, fopen_errno,
156 "failed to open log-file %s", logfile_name);
157 rprintf(FINFO, "Ignoring \"log file\" setting.\n");
161 void log_init(int restart)
163 if (log_initialised) {
166 if (strcmp(logfile_name, lp_log_file(module_id)) != 0) {
173 } else if (*logfile_name)
174 return; /* unchanged, non-empty "log file" names */
175 else if (lp_syslog_facility(-1) != lp_syslog_facility(module_id))
178 return; /* unchanged syslog settings */
182 /* This looks pointless, but it is needed in order for the
183 * C library on some systems to fetch the timezone info
184 * before the chroot. */
185 timestring(time(NULL));
187 /* Optionally use a log file instead of syslog. (Non-daemon
188 * rsyncs will have already set logfile_name, as needed.) */
189 if (am_daemon && !logfile_name)
190 logfile_name = lp_log_file(module_id);
191 if (logfile_name && *logfile_name)
197 void logfile_close(void)
200 logfile_was_closed = 1;
206 void logfile_reopen(void)
208 if (logfile_was_closed) {
209 logfile_was_closed = 0;
214 static void filtered_fwrite(FILE *f, const char *buf, int len, int use_isprint)
216 const char *s, *end = buf + len;
217 for (s = buf; s < end; s++) {
219 && *s == '\\' && s[1] == '#'
224 && ((use_isprint && !isPrint(s))
225 || *(uchar*)s < ' '))) {
226 if (s != buf && fwrite(buf, s - buf, 1, f) != 1)
227 exit_cleanup(RERR_MESSAGEIO);
228 fprintf(f, "\\#%03o", *(uchar*)s);
232 if (buf != end && fwrite(buf, end - buf, 1, f) != 1)
233 exit_cleanup(RERR_MESSAGEIO);
236 /* this is the underlying (unformatted) rsync debugging function. Call
237 * it with FINFO, FERROR or FLOG. Note: recursion can happen with
238 * certain fatal conditions. */
239 void rwrite(enum logcode code, const char *buf, int len, int is_utf8)
241 int trailing_CR_or_NL;
244 iconv_t ic = is_utf8 && ic_recv != (iconv_t)-1 ? ic_recv : ic_chck;
247 iconv_t ic = ic_chck;
252 exit_cleanup(RERR_MESSAGEIO);
254 if (am_server && msg_fd_out >= 0) {
256 /* Pass the message to our sibling. */
257 send_msg((enum msgcode)code, buf, len, 0);
261 if (code == FSOCKERR) /* This gets simplified for a non-sibling. */
266 else if (am_daemon || logfile_name) {
269 int priority = code == FERROR ? LOG_WARNING : LOG_INFO;
274 if (!log_initialised)
276 strlcpy(msg, buf, MIN((int)sizeof msg, len + 1));
277 logit(priority, msg);
280 if (code == FLOG || (am_daemon && !am_server))
282 } else if (code == FLOG)
285 if (quiet && code != FERROR)
289 /* Pass the message to the non-server side. */
290 if (send_msg((enum msgcode)code, buf, len, !is_utf8))
293 /* TODO: can we send the error to the user somehow? */
304 f = am_server ? stderr : stdout;
307 exit_cleanup(RERR_MESSAGEIO);
310 trailing_CR_or_NL = len && (buf[len-1] == '\n' || buf[len-1] == '\r')
314 if (ic != (iconv_t)-1) {
316 ICONV_CONST char *in_buf = (ICONV_CONST char *)buf;
317 char *out_buf = convbuf;
318 size_t in_cnt = len, out_cnt = sizeof convbuf - 1;
320 iconv(ic, NULL, 0, NULL, 0);
321 while (iconv(ic, &in_buf,&in_cnt,
322 &out_buf,&out_cnt) == (size_t)-1) {
323 if (out_buf != convbuf) {
324 filtered_fwrite(f, convbuf, out_buf - convbuf, 0);
326 out_cnt = sizeof convbuf - 1;
330 fprintf(f, "\\#%03o", *(uchar*)in_buf++);
333 if (out_buf != convbuf)
334 filtered_fwrite(f, convbuf, out_buf - convbuf, 0);
337 filtered_fwrite(f, buf, len, !allow_8bit_chars);
339 if (trailing_CR_or_NL) {
340 fputc(trailing_CR_or_NL, f);
345 /* This is the rsync debugging function. Call it with FINFO, FERROR or
347 void rprintf(enum logcode code, const char *format, ...)
350 char buf[BIGPATHBUFLEN];
353 va_start(ap, format);
354 len = vsnprintf(buf, sizeof buf, format, ap);
357 /* Deal with buffer overruns. Instead of panicking, just
358 * truncate the resulting string. (Note that configure ensures
359 * that we have a vsnprintf() that doesn't ever return -1.) */
360 if (len > sizeof buf - 1) {
361 static const char ellipsis[] = "[...]";
363 /* Reset length, and zero-terminate the end of our buffer */
364 len = sizeof buf - 1;
367 /* Copy the ellipsis to the end of the string, but give
368 * us one extra character:
370 * v--- null byte at buf[sizeof buf - 1]
372 * -> abcd[...]00 <-- now two null bytes at end
374 * If the input format string has a trailing newline,
375 * we copy it into that extra null; if it doesn't, well,
376 * all we lose is one byte. */
377 memcpy(buf+len-sizeof ellipsis, ellipsis, sizeof ellipsis);
378 if (format[strlen(format)-1] == '\n') {
383 rwrite(code, buf, len, 0);
386 /* This is like rprintf, but it also tries to print some
387 * representation of the error code. Normally errcode = errno.
389 * Unlike rprintf, this always adds a newline and there should not be
390 * one in the format string.
392 * Note that since strerror might involve dynamically loading a
393 * message catalog we need to call it once before chroot-ing. */
394 void rsyserr(enum logcode code, int errcode, const char *format, ...)
397 char buf[BIGPATHBUFLEN];
400 strlcpy(buf, RSYNC_NAME ": ", sizeof buf);
401 len = (sizeof RSYNC_NAME ": ") - 1;
403 va_start(ap, format);
404 len += vsnprintf(buf + len, sizeof buf - len, format, ap);
407 if (len < sizeof buf) {
408 len += snprintf(buf + len, sizeof buf - len,
409 ": %s (%d)\n", strerror(errcode), errcode);
411 if (len >= sizeof buf)
412 exit_cleanup(RERR_MESSAGEIO);
414 rwrite(code, buf, len, 0);
417 void rflush(enum logcode code)
421 if (am_daemon || code == FLOG)
424 if (code == FERROR || am_server)
432 /* A generic logging routine for send/recv, with parameter substitiution. */
433 static void log_formatted(enum logcode code, const char *format, const char *op,
434 struct file_struct *file, const char *fname,
435 struct stats *initial_stats, int iflags,
438 char buf[MAXPATHLEN+1024], buf2[MAXPATHLEN], fmt[32];
446 /* We expand % codes one by one in place in buf. We don't
447 * copy in the terminating null of the inserted strings, but
448 * rather keep going until we reach the null of the format. */
449 total = strlcpy(buf, format, sizeof buf);
450 if (total > MAXPATHLEN) {
451 rprintf(FERROR, "log-format string is WAY too long!\n");
452 exit_cleanup(RERR_MESSAGEIO);
457 for (p = buf; (p = strchr(p, '%')) != NULL; ) {
462 while (isDigit(p) && c - fmt < (int)(sizeof fmt) - 8)
479 strlcat(fmt, ".0f", sizeof fmt);
480 snprintf(buf2, sizeof buf2, fmt,
481 (double)F_LENGTH(file));
485 strlcat(fmt, "u", sizeof fmt);
486 snprintf(buf2, sizeof buf2, fmt,
487 uid_ndx ? F_OWNER(file) : 0);
491 if (!gid_ndx || file->flags & FLAG_SKIP_GROUP)
494 strlcat(fmt, "u", sizeof fmt);
495 snprintf(buf2, sizeof buf2, fmt,
501 strlcat(fmt, "ld", sizeof fmt);
502 snprintf(buf2, sizeof buf2, fmt,
507 n = c = timestring(file->modtime);
508 while ((c = strchr(p, ' ')) != NULL)
512 c = buf2 + MAXPATHLEN - PERMSTRING_SIZE - 1;
513 permstring(c, file->mode);
514 n = c + 1; /* skip the type char */
522 strlcpy(c, fname, MAXPATHLEN);
524 c = f_name(file, NULL);
525 if (am_sender && F_PATHNAME(file)) {
526 pathjoin(buf2, sizeof buf2,
527 F_PATHNAME(file), c);
528 clean_fname(buf2, 0);
530 strlcpy(c, buf2, MAXPATHLEN);
534 } else if (am_daemon && *c != '/') {
535 pathjoin(buf2, sizeof buf2,
536 curr_dir + module_dirlen, c);
537 clean_fname(buf2, 0);
539 strlcpy(c, buf2, MAXPATHLEN);
553 strlcpy(c, fname, MAXPATHLEN);
555 c = f_name(file, NULL);
556 if (S_ISDIR(file->mode))
557 strlcat(c, "/", MAXPATHLEN);
561 if (hlink && *hlink) {
563 strlcpy(buf2, " => ", sizeof buf2);
564 } else if (S_ISLNK(file->mode) && !fname) {
566 strlcpy(buf2, " -> ", sizeof buf2);
571 strlcpy(buf2, " ", sizeof buf2);
573 strlcat(fmt, "s", sizeof fmt);
574 snprintf(buf2 + 4, sizeof buf2 - 4, fmt, n);
578 n = lp_name(module_id);
581 n = timestring(time(NULL));
591 b = stats.total_written -
592 initial_stats->total_written;
594 b = stats.total_read -
595 initial_stats->total_read;
597 strlcat(fmt, ".0f", sizeof fmt);
598 snprintf(buf2, sizeof buf2, fmt, (double)b);
603 b = stats.total_written -
604 initial_stats->total_written;
606 b = stats.total_read -
607 initial_stats->total_read;
609 strlcat(fmt, ".0f", sizeof fmt);
610 snprintf(buf2, sizeof buf2, fmt, (double)b);
614 if (iflags & ITEM_DELETED) {
618 n = c = buf2 + MAXPATHLEN - 32;
619 c[0] = iflags & ITEM_LOCAL_CHANGE
620 ? iflags & ITEM_XNAME_FOLLOWS ? 'h' : 'c'
621 : !(iflags & ITEM_TRANSFER) ? '.'
622 : !local_server && *op == 's' ? '<' : '>';
623 c[1] = S_ISDIR(file->mode) ? 'd'
624 : IS_SPECIAL(file->mode) ? 'S'
625 : IS_DEVICE(file->mode) ? 'D'
626 : S_ISLNK(file->mode) ? 'L' : 'f';
627 c[2] = !(iflags & ITEM_REPORT_CHECKSUM) ? '.' : 'c';
628 c[3] = !(iflags & ITEM_REPORT_SIZE) ? '.' : 's';
629 c[4] = !(iflags & ITEM_REPORT_TIME) ? '.'
630 : !preserve_times || S_ISLNK(file->mode) ? 'T' : 't';
631 c[5] = !(iflags & ITEM_REPORT_PERMS) ? '.' : 'p';
632 c[6] = !(iflags & ITEM_REPORT_OWNER) ? '.' : 'o';
633 c[7] = !(iflags & ITEM_REPORT_GROUP) ? '.' : 'g';
634 c[8] = !(iflags & ITEM_REPORT_ATIME) ? '.' : 'u';
635 c[9] = !(iflags & ITEM_REPORT_ACL) ? '.' : 'a';
636 c[10] = !(iflags & ITEM_REPORT_XATTR) ? '.' : 'x';
639 if (iflags & (ITEM_IS_NEW|ITEM_MISSING_DATA)) {
640 char ch = iflags & ITEM_IS_NEW ? '+' : '?';
642 for (i = 2; c[i]; i++)
644 } else if (c[0] == '.' || c[0] == 'h' || c[0] == 'c') {
646 for (i = 2; c[i]; i++) {
651 for (i = 2; c[i]; i++)
658 /* "n" is the string to be inserted in place of this % code. */
661 if (n != buf2 && fmt[1]) {
662 strlcat(fmt, "s", sizeof fmt);
663 snprintf(buf2, sizeof buf2, fmt, n);
668 /* Subtract the length of the escape from the string's size. */
671 if (len + total >= (size_t)sizeof buf) {
673 "buffer overflow expanding %%%c -- exiting\n",
675 exit_cleanup(RERR_MESSAGEIO);
678 /* Shuffle the rest of the string along to make space for n */
679 if (len != (size_t)(p - s + 1))
680 memmove(s + len, p + 1, total - (s - buf) + 1);
683 /* Insert the contents of string "n", but NOT its null. */
687 /* Skip over inserted string; continue looking */
691 rwrite(code, buf, total, 0);
694 /* Return 1 if the format escape is in the log-format string (e.g. look for
695 * the 'b' in the "%9b" format escape). */
696 int log_format_has(const char *format, char esc)
703 for (p = format; (p = strchr(p, '%')) != NULL; ) {
716 /* Log the transfer of a file. If the code is FCLIENT, the output just goes
717 * to stdout. If it is FLOG, it just goes to the log file. Otherwise we
719 void log_item(enum logcode code, struct file_struct *file,
720 struct stats *initial_stats, int iflags, const char *hlink)
722 const char *s_or_r = am_sender ? "send" : "recv";
724 if (code != FLOG && stdout_format && !am_server) {
725 log_formatted(FCLIENT, stdout_format, s_or_r,
726 file, NULL, initial_stats, iflags, hlink);
728 if (code != FCLIENT && logfile_format && *logfile_format) {
729 log_formatted(FLOG, logfile_format, s_or_r,
730 file, NULL, initial_stats, iflags, hlink);
734 void maybe_log_item(struct file_struct *file, int iflags, int itemizing,
737 int significant_flags = iflags & SIGNIFICANT_ITEM_FLAGS;
738 int see_item = itemizing && (significant_flags || *buf
739 || stdout_format_has_i > 1 || (verbose > 1 && stdout_format_has_i));
740 int local_change = iflags & ITEM_LOCAL_CHANGE && significant_flags;
742 if (logfile_name && !dry_run && see_item
743 && (significant_flags || logfile_format_has_i))
744 log_item(FLOG, file, &stats, iflags, buf);
745 } else if (see_item || local_change || *buf
746 || (S_ISDIR(file->mode) && significant_flags)) {
747 enum logcode code = significant_flags || logfile_format_has_i ? FINFO : FCLIENT;
748 log_item(code, file, &stats, iflags, buf);
752 void log_delete(const char *fname, int mode)
755 union file_extras ex[4]; /* just in case... */
756 struct file_struct file;
758 int len = strlen(fname);
763 if (!verbose && !stdout_format)
765 else if (am_server && protocol_version >= 29 && len < MAXPATHLEN) {
767 len++; /* directories include trailing null */
768 send_msg(MSG_DELETED, fname, len, am_generator);
770 fmt = stdout_format_has_o_or_i ? stdout_format : "deleting %n";
771 log_formatted(FCLIENT, fmt, "del.", &x.file, fname, &stats,
775 if (!logfile_name || dry_run || !logfile_format)
778 fmt = logfile_format_has_o_or_i ? logfile_format : "deleting %n";
779 log_formatted(FLOG, fmt, "del.", &x.file, fname, &stats, ITEM_DELETED, NULL);
783 * Called when the transfer is interrupted for some reason.
785 * Code is one of the RERR_* codes from errcode.h, or terminating
788 void log_exit(int code, const char *file, int line)
791 rprintf(FLOG,"sent %.0f bytes received %.0f bytes total size %.0f\n",
792 (double)stats.total_written,
793 (double)stats.total_read,
794 (double)stats.total_size);
798 name = rerr_name(code);
800 name = "unexplained error";
802 /* VANISHED is not an error, only a warning */
803 if (code == RERR_VANISHED) {
804 rprintf(FINFO, "rsync warning: %s (code %d) at %s(%d) [%s=%s]\n",
805 name, code, file, line, who_am_i(), RSYNC_VERSION);
807 rprintf(FERROR, "rsync error: %s (code %d) at %s(%d) [%s=%s]\n",
808 name, code, file, line, who_am_i(), RSYNC_VERSION);