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-2009 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.
30 extern int am_generator;
31 extern int local_server;
34 extern int checksum_len;
35 extern int allow_8bit_chars;
36 extern int protocol_version;
37 extern int always_checksum;
38 extern int preserve_times;
39 extern int msgs2stderr;
42 extern int stdout_format_has_i;
43 extern int stdout_format_has_o_or_i;
44 extern int logfile_format_has_i;
45 extern int logfile_format_has_o_or_i;
46 extern int receiver_symlink_times;
47 extern mode_t orig_umask;
48 extern char *auth_user;
49 extern char *stdout_format;
50 extern char *logfile_format;
51 extern char *logfile_name;
53 extern iconv_t ic_chck;
56 extern iconv_t ic_recv;
58 extern char curr_dir[MAXPATHLEN];
59 extern char *full_module_path;
60 extern unsigned int module_dirlen;
61 extern char sender_file_sum[MAX_DIGEST_LEN];
62 extern const char undetermined_hostname[];
64 static int log_initialised;
65 static int logfile_was_closed;
66 static FILE *logfile_fp;
69 int got_xfer_error = 0;
70 int output_needs_newline = 0;
71 int send_msgs_to_gen = 0;
76 } const rerr_names[] = {
77 { RERR_SYNTAX , "syntax or usage error" },
78 { RERR_PROTOCOL , "protocol incompatibility" },
79 { RERR_FILESELECT , "errors selecting input/output files, dirs" },
80 { RERR_UNSUPPORTED, "requested action not supported" },
81 { RERR_STARTCLIENT, "error starting client-server protocol" },
82 { RERR_SOCKETIO , "error in socket IO" },
83 { RERR_FILEIO , "error in file IO" },
84 { RERR_STREAMIO , "error in rsync protocol data stream" },
85 { RERR_MESSAGEIO , "errors with program diagnostics" },
86 { RERR_IPC , "error in IPC code" },
87 { RERR_CRASHED , "sibling process crashed" },
88 { RERR_TERMINATED , "sibling process terminated abnormally" },
89 { RERR_SIGNAL1 , "received SIGUSR1" },
90 { RERR_SIGNAL , "received SIGINT, SIGTERM, or SIGHUP" },
91 { RERR_WAITCHILD , "waitpid() failed" },
92 { RERR_MALLOC , "error allocating core memory buffers" },
93 { RERR_PARTIAL , "some files/attrs were not transferred (see previous errors)" },
94 { RERR_VANISHED , "some files vanished before they could be transferred" },
95 { RERR_TIMEOUT , "timeout in data send/receive" },
96 { RERR_CONTIMEOUT , "timeout waiting for daemon connection" },
97 { RERR_CMD_FAILED , "remote shell failed" },
98 { RERR_CMD_KILLED , "remote shell killed" },
99 { RERR_CMD_RUN , "remote command could not be run" },
100 { RERR_CMD_NOTFOUND,"remote command not found" },
101 { RERR_DEL_LIMIT , "the --max-delete limit stopped deletions" },
106 * Map from rsync error code to name, or return NULL.
108 static char const *rerr_name(int code)
111 for (i = 0; rerr_names[i].name; i++) {
112 if (rerr_names[i].code == code)
113 return rerr_names[i].name;
118 static void logit(int priority, const char *buf)
120 if (logfile_was_closed)
123 fprintf(logfile_fp, "%s [%d] %s",
124 timestring(time(NULL)), (int)getpid(), buf);
127 syslog(priority, "%s", buf);
131 static void syslog_init()
133 static int been_here = 0;
134 int options = LOG_PID;
141 options |= LOG_NDELAY;
145 openlog("rsyncd", options, lp_syslog_facility(module_id));
147 openlog("rsyncd", options);
151 logit(LOG_INFO, "rsyncd started\n");
155 static void logfile_open(void)
157 mode_t old_umask = umask(022 | orig_umask);
158 logfile_fp = fopen(logfile_name, "a");
161 int fopen_errno = errno;
162 /* Rsync falls back to using syslog on failure. */
164 rsyserr(FERROR, fopen_errno,
165 "failed to open log-file %s", logfile_name);
166 rprintf(FINFO, "Ignoring \"log file\" setting.\n");
170 void log_init(int restart)
172 if (log_initialised) {
175 if (strcmp(logfile_name, lp_log_file(module_id)) != 0) {
182 } else if (*logfile_name)
183 return; /* unchanged, non-empty "log file" names */
184 else if (lp_syslog_facility(-1) != lp_syslog_facility(module_id))
187 return; /* unchanged syslog settings */
191 /* This looks pointless, but it is needed in order for the
192 * C library on some systems to fetch the timezone info
193 * before the chroot. */
194 timestring(time(NULL));
196 /* Optionally use a log file instead of syslog. (Non-daemon
197 * rsyncs will have already set logfile_name, as needed.) */
198 if (am_daemon && !logfile_name)
199 logfile_name = lp_log_file(module_id);
200 if (logfile_name && *logfile_name)
206 void logfile_close(void)
209 logfile_was_closed = 1;
215 void logfile_reopen(void)
217 if (logfile_was_closed) {
218 logfile_was_closed = 0;
223 static void filtered_fwrite(FILE *f, const char *buf, int len, int use_isprint)
225 const char *s, *end = buf + len;
226 for (s = buf; s < end; s++) {
228 && *s == '\\' && s[1] == '#'
233 && ((use_isprint && !isPrint(s))
234 || *(uchar*)s < ' '))) {
235 if (s != buf && fwrite(buf, s - buf, 1, f) != 1)
236 exit_cleanup(RERR_MESSAGEIO);
237 fprintf(f, "\\#%03o", *(uchar*)s);
241 if (buf != end && fwrite(buf, end - buf, 1, f) != 1)
242 exit_cleanup(RERR_MESSAGEIO);
245 /* this is the underlying (unformatted) rsync debugging function. Call
246 * it with FINFO, FERROR_*, FWARNING, FLOG, or FCLIENT. Note: recursion
247 * can happen with certain fatal conditions. */
248 void rwrite(enum logcode code, const char *buf, int len, int is_utf8)
250 int trailing_CR_or_NL;
251 FILE *f = msgs2stderr ? stderr : stdout;
253 iconv_t ic = is_utf8 && ic_recv != (iconv_t)-1 ? ic_recv : ic_chck;
256 iconv_t ic = ic_chck;
261 exit_cleanup(RERR_MESSAGEIO);
263 if (msgs2stderr && code != FLOG)
266 if (send_msgs_to_gen) {
268 /* Pass the message to our sibling in native charset. */
269 send_msg((enum msgcode)code, buf, len, 0);
273 if (code == FERROR_SOCKET) /* This gets simplified for a non-sibling. */
275 else if (code == FERROR_UTF8) {
282 else if (am_daemon || logfile_name) {
285 int priority = code == FINFO || code == FLOG ? LOG_INFO : LOG_WARNING;
290 if (!log_initialised)
292 strlcpy(msg, buf, MIN((int)sizeof msg, len + 1));
293 logit(priority, msg);
296 if (code == FLOG || (am_daemon && !am_server))
298 } else if (code == FLOG)
301 if (quiet && code == FINFO)
305 enum msgcode msg = (enum msgcode)code;
306 if (protocol_version < 30) {
307 if (msg == MSG_ERROR)
308 msg = MSG_ERROR_XFER;
309 else if (msg == MSG_WARNING)
312 /* Pass the message to the non-server side. */
313 if (send_msg(msg, buf, len, !is_utf8))
316 /* TODO: can we send the error to the user somehow? */
335 exit_cleanup(RERR_MESSAGEIO);
338 if (output_needs_newline) {
340 output_needs_newline = 0;
343 trailing_CR_or_NL = len && (buf[len-1] == '\n' || buf[len-1] == '\r')
346 if (len && buf[0] == '\r') {
353 if (ic != (iconv_t)-1) {
358 INIT_CONST_XBUF(outbuf, convbuf);
359 INIT_XBUF(inbuf, (char*)buf, len, (size_t)-1);
362 iconvbufs(ic, &inbuf, &outbuf, inbuf.pos ? 0 : ICB_INIT);
365 filtered_fwrite(f, convbuf, outbuf.len, 0);
368 if (!ierrno || ierrno == E2BIG)
370 fprintf(f, "\\#%03o", CVAL(inbuf.buf, inbuf.pos++));
375 filtered_fwrite(f, buf, len, !allow_8bit_chars);
377 if (trailing_CR_or_NL) {
378 fputc(trailing_CR_or_NL, f);
383 /* This is the rsync debugging function. Call it with FINFO, FERROR_*,
384 * FWARNING, FLOG, or FCLIENT. */
385 void rprintf(enum logcode code, const char *format, ...)
388 char buf[BIGPATHBUFLEN];
391 va_start(ap, format);
392 len = vsnprintf(buf, sizeof buf, format, ap);
395 /* Deal with buffer overruns. Instead of panicking, just
396 * truncate the resulting string. (Note that configure ensures
397 * that we have a vsnprintf() that doesn't ever return -1.) */
398 if (len > sizeof buf - 1) {
399 static const char ellipsis[] = "[...]";
401 /* Reset length, and zero-terminate the end of our buffer */
402 len = sizeof buf - 1;
405 /* Copy the ellipsis to the end of the string, but give
406 * us one extra character:
408 * v--- null byte at buf[sizeof buf - 1]
410 * -> abcd[...]00 <-- now two null bytes at end
412 * If the input format string has a trailing newline,
413 * we copy it into that extra null; if it doesn't, well,
414 * all we lose is one byte. */
415 memcpy(buf+len-sizeof ellipsis, ellipsis, sizeof ellipsis);
416 if (format[strlen(format)-1] == '\n') {
421 rwrite(code, buf, len, 0);
424 /* This is like rprintf, but it also tries to print some
425 * representation of the error code. Normally errcode = errno.
427 * Unlike rprintf, this always adds a newline and there should not be
428 * one in the format string.
430 * Note that since strerror might involve dynamically loading a
431 * message catalog we need to call it once before chroot-ing. */
432 void rsyserr(enum logcode code, int errcode, const char *format, ...)
435 char buf[BIGPATHBUFLEN];
438 strlcpy(buf, RSYNC_NAME ": ", sizeof buf);
439 len = (sizeof RSYNC_NAME ": ") - 1;
441 va_start(ap, format);
442 len += vsnprintf(buf + len, sizeof buf - len, format, ap);
445 if (len < sizeof buf) {
446 len += snprintf(buf + len, sizeof buf - len,
447 ": %s (%d)\n", strerror(errcode), errcode);
449 if (len >= sizeof buf)
450 exit_cleanup(RERR_MESSAGEIO);
452 rwrite(code, buf, len, 0);
455 void rflush(enum logcode code)
459 if (am_daemon || code == FLOG)
462 if (!am_server && (code == FINFO || code == FCLIENT))
470 /* A generic logging routine for send/recv, with parameter substitiution. */
471 static void log_formatted(enum logcode code, const char *format, const char *op,
472 struct file_struct *file, const char *fname,
473 struct stats *initial_stats, int iflags,
476 char buf[MAXPATHLEN+1024], buf2[MAXPATHLEN], fmt[32];
484 /* We expand % codes one by one in place in buf. We don't
485 * copy in the terminating null of the inserted strings, but
486 * rather keep going until we reach the null of the format. */
487 total = strlcpy(buf, format, sizeof buf);
488 if (total > MAXPATHLEN) {
489 rprintf(FERROR, "log-format string is WAY too long!\n");
490 exit_cleanup(RERR_MESSAGEIO);
495 for (p = buf; (p = strchr(p, '%')) != NULL; ) {
505 while (isDigit(p) && c - fmt < (int)(sizeof fmt) - 8)
516 /* Note for %h and %a: it doesn't matter what fd we pass to
517 * client_{name,addr} because rsync_module will already have
518 * forced the answer to be cached (assuming, of course, for %h
519 * that lp_reverse_lookup(module_id) is true). */
523 n = lp_reverse_lookup(module_id)
524 ? client_name(0) : undetermined_hostname;
532 strlcat(fmt, "s", sizeof fmt);
533 snprintf(buf2, sizeof buf2, fmt,
534 do_big_num(F_LENGTH(file), humanize, NULL));
538 strlcat(fmt, "u", sizeof fmt);
539 snprintf(buf2, sizeof buf2, fmt,
540 uid_ndx ? F_OWNER(file) : 0);
544 if (!gid_ndx || file->flags & FLAG_SKIP_GROUP)
547 strlcat(fmt, "u", sizeof fmt);
548 snprintf(buf2, sizeof buf2, fmt,
554 strlcat(fmt, "ld", sizeof fmt);
555 snprintf(buf2, sizeof buf2, fmt,
560 n = c = timestring(file->modtime);
561 while ((c = strchr(c, ' ')) != NULL)
565 c = buf2 + MAXPATHLEN - PERMSTRING_SIZE - 1;
566 permstring(c, file->mode);
567 n = c + 1; /* skip the type char */
575 strlcpy(c, fname, MAXPATHLEN);
577 c = f_name(file, NULL);
578 if (am_sender && F_PATHNAME(file)) {
579 pathjoin(buf2, sizeof buf2,
580 F_PATHNAME(file), c);
581 clean_fname(buf2, 0);
583 strlcpy(c, buf2, MAXPATHLEN);
587 } else if (am_daemon && *c != '/') {
588 pathjoin(buf2, sizeof buf2,
589 curr_dir + module_dirlen, c);
590 clean_fname(buf2, 0);
592 strlcpy(c, buf2, MAXPATHLEN);
606 strlcpy(c, fname, MAXPATHLEN);
608 c = f_name(file, NULL);
609 if (S_ISDIR(file->mode))
610 strlcat(c, "/", MAXPATHLEN);
614 if (hlink && *hlink) {
616 strlcpy(buf2, " => ", sizeof buf2);
617 } else if (S_ISLNK(file->mode) && !fname) {
619 strlcpy(buf2, " -> ", sizeof buf2);
624 strlcpy(buf2, " ", sizeof buf2);
626 strlcat(fmt, "s", sizeof fmt);
627 snprintf(buf2 + 4, sizeof buf2 - 4, fmt, n);
631 n = lp_name(module_id);
634 n = timestring(time(NULL));
637 n = full_module_path;
644 b = stats.total_written -
645 initial_stats->total_written;
647 b = stats.total_read -
648 initial_stats->total_read;
650 strlcat(fmt, "s", sizeof fmt);
651 snprintf(buf2, sizeof buf2, fmt,
652 do_big_num(b, humanize, NULL));
657 b = stats.total_written -
658 initial_stats->total_written;
660 b = stats.total_read -
661 initial_stats->total_read;
663 strlcat(fmt, "s", sizeof fmt);
664 snprintf(buf2, sizeof buf2, fmt,
665 do_big_num(b, humanize, NULL));
669 if (protocol_version >= 30
670 && (iflags & ITEM_TRANSFER
671 || (always_checksum && S_ISREG(file->mode)))) {
673 const char *sum = iflags & ITEM_TRANSFER
674 ? sender_file_sum : F_SUM(file);
675 c = buf2 + checksum_len*2;
677 for (i = checksum_len; --i >= 0; ) {
681 *--c = x1 <= 9 ? x1 + '0' : x1 + 'a' - 10;
682 *--c = x2 <= 9 ? x2 + '0' : x2 + 'a' - 10;
685 memset(buf2, ' ', checksum_len*2);
686 buf2[checksum_len*2] = '\0';
691 if (iflags & ITEM_DELETED) {
695 n = c = buf2 + MAXPATHLEN - 32;
696 c[0] = iflags & ITEM_LOCAL_CHANGE
697 ? iflags & ITEM_XNAME_FOLLOWS ? 'h' : 'c'
698 : !(iflags & ITEM_TRANSFER) ? '.'
699 : !local_server && *op == 's' ? '<' : '>';
700 if (S_ISLNK(file->mode)) {
703 c[4] = !(iflags & ITEM_REPORT_TIME) ? '.'
704 : !preserve_times || !receiver_symlink_times
705 || (iflags & ITEM_REPORT_TIMEFAIL) ? 'T' : 't';
707 c[1] = S_ISDIR(file->mode) ? 'd'
708 : IS_SPECIAL(file->mode) ? 'S'
709 : IS_DEVICE(file->mode) ? 'D' : 'f';
710 c[3] = !(iflags & ITEM_REPORT_SIZE) ? '.' : 's';
711 c[4] = !(iflags & ITEM_REPORT_TIME) ? '.'
712 : !preserve_times ? 'T' : 't';
714 c[2] = !(iflags & ITEM_REPORT_CHANGE) ? '.' : 'c';
715 c[5] = !(iflags & ITEM_REPORT_PERMS) ? '.' : 'p';
716 c[6] = !(iflags & ITEM_REPORT_OWNER) ? '.' : 'o';
717 c[7] = !(iflags & ITEM_REPORT_GROUP) ? '.' : 'g';
718 c[8] = !(iflags & ITEM_REPORT_ATIME) ? '.' : 'u';
719 c[9] = !(iflags & ITEM_REPORT_ACL) ? '.' : 'a';
720 c[10] = !(iflags & ITEM_REPORT_XATTR) ? '.' : 'x';
723 if (iflags & (ITEM_IS_NEW|ITEM_MISSING_DATA)) {
724 char ch = iflags & ITEM_IS_NEW ? '+' : '?';
726 for (i = 2; c[i]; i++)
728 } else if (c[0] == '.' || c[0] == 'h' || c[0] == 'c') {
730 for (i = 2; c[i]; i++) {
735 for (i = 2; c[i]; i++)
742 /* "n" is the string to be inserted in place of this % code. */
745 if (n != buf2 && fmt[1]) {
746 strlcat(fmt, "s", sizeof fmt);
747 snprintf(buf2, sizeof buf2, fmt, n);
752 /* Subtract the length of the escape from the string's size. */
755 if (len + total >= (size_t)sizeof buf) {
757 "buffer overflow expanding %%%c -- exiting\n",
759 exit_cleanup(RERR_MESSAGEIO);
762 /* Shuffle the rest of the string along to make space for n */
763 if (len != (size_t)(p - s + 1))
764 memmove(s + len, p + 1, total - (s - buf) + 1);
767 /* Insert the contents of string "n", but NOT its null. */
771 /* Skip over inserted string; continue looking */
775 rwrite(code, buf, total, 0);
778 /* Return 1 if the format escape is in the log-format string (e.g. look for
779 * the 'b' in the "%9b" format escape). */
780 int log_format_has(const char *format, char esc)
787 for (p = format; (p = strchr(p, '%')) != NULL; ) {
800 /* Log the transfer of a file. If the code is FCLIENT, the output just goes
801 * to stdout. If it is FLOG, it just goes to the log file. Otherwise we
803 void log_item(enum logcode code, struct file_struct *file,
804 struct stats *initial_stats, int iflags, const char *hlink)
806 const char *s_or_r = am_sender ? "send" : "recv";
808 if (code != FLOG && stdout_format && !am_server) {
809 log_formatted(FCLIENT, stdout_format, s_or_r,
810 file, NULL, initial_stats, iflags, hlink);
812 if (code != FCLIENT && logfile_format && *logfile_format) {
813 log_formatted(FLOG, logfile_format, s_or_r,
814 file, NULL, initial_stats, iflags, hlink);
818 void maybe_log_item(struct file_struct *file, int iflags, int itemizing,
821 int significant_flags = iflags & SIGNIFICANT_ITEM_FLAGS;
822 int see_item = itemizing && (significant_flags || *buf
823 || stdout_format_has_i > 1 || (INFO_GTE(NAME, 2) && stdout_format_has_i));
824 int local_change = iflags & ITEM_LOCAL_CHANGE && significant_flags;
826 if (logfile_name && !dry_run && see_item
827 && (significant_flags || logfile_format_has_i))
828 log_item(FLOG, file, &stats, iflags, buf);
829 } else if (see_item || local_change || *buf
830 || (S_ISDIR(file->mode) && significant_flags)) {
831 enum logcode code = significant_flags || logfile_format_has_i ? FINFO : FCLIENT;
832 log_item(code, file, &stats, iflags, buf);
836 void log_delete(const char *fname, int mode)
839 union file_extras ex[4]; /* just in case... */
840 struct file_struct file;
842 int len = strlen(fname);
847 if (!INFO_GTE(DEL, 1) && !stdout_format)
849 else if (am_server && protocol_version >= 29 && len < MAXPATHLEN) {
851 len++; /* directories include trailing null */
852 send_msg(MSG_DELETED, fname, len, am_generator);
854 fmt = stdout_format_has_o_or_i ? stdout_format : "deleting %n";
855 log_formatted(FCLIENT, fmt, "del.", &x.file, fname, &stats,
859 if (!logfile_name || dry_run || !logfile_format)
862 fmt = logfile_format_has_o_or_i ? logfile_format : "deleting %n";
863 log_formatted(FLOG, fmt, "del.", &x.file, fname, &stats, ITEM_DELETED, NULL);
867 * Called when the transfer is interrupted for some reason.
869 * Code is one of the RERR_* codes from errcode.h, or terminating
872 void log_exit(int code, const char *file, int line)
875 rprintf(FLOG,"sent %s bytes received %s bytes total size %s\n",
876 comma_num(stats.total_written),
877 comma_num(stats.total_read),
878 comma_num(stats.total_size));
879 } else if (am_server != 2) {
882 name = rerr_name(code);
884 name = "unexplained error";
886 /* VANISHED is not an error, only a warning */
887 if (code == RERR_VANISHED) {
888 rprintf(FWARNING, "rsync warning: %s (code %d) at %s(%d) [%s=%s]\n",
889 name, code, file, line, who_am_i(), RSYNC_VERSION);
891 rprintf(FERROR, "rsync error: %s (code %d) at %s(%d) [%s=%s]\n",
892 name, code, file, line, who_am_i(), RSYNC_VERSION);