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-2013 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;
40 extern int stdout_format_has_i;
41 extern int stdout_format_has_o_or_i;
42 extern int logfile_format_has_i;
43 extern int logfile_format_has_o_or_i;
44 extern int receiver_symlink_times;
45 extern int64 total_data_written;
46 extern int64 total_data_read;
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;
73 static int64 initial_data_written;
74 static int64 initial_data_read;
79 } const rerr_names[] = {
80 { RERR_SYNTAX , "syntax or usage error" },
81 { RERR_PROTOCOL , "protocol incompatibility" },
82 { RERR_FILESELECT , "errors selecting input/output files, dirs" },
83 { RERR_UNSUPPORTED, "requested action not supported" },
84 { RERR_STARTCLIENT, "error starting client-server protocol" },
85 { RERR_SOCKETIO , "error in socket IO" },
86 { RERR_FILEIO , "error in file IO" },
87 { RERR_STREAMIO , "error in rsync protocol data stream" },
88 { RERR_MESSAGEIO , "errors with program diagnostics" },
89 { RERR_IPC , "error in IPC code" },
90 { RERR_CRASHED , "sibling process crashed" },
91 { RERR_TERMINATED , "sibling process terminated abnormally" },
92 { RERR_SIGNAL1 , "received SIGUSR1" },
93 { RERR_SIGNAL , "received SIGINT, SIGTERM, or SIGHUP" },
94 { RERR_WAITCHILD , "waitpid() failed" },
95 { RERR_MALLOC , "error allocating core memory buffers" },
96 { RERR_PARTIAL , "some files/attrs were not transferred (see previous errors)" },
97 { RERR_VANISHED , "some files vanished before they could be transferred" },
98 { RERR_DEL_LIMIT , "the --max-delete limit stopped deletions" },
99 { RERR_TIMEOUT , "timeout in data send/receive" },
100 { RERR_CONTIMEOUT , "timeout waiting for daemon connection" },
101 { RERR_CMD_FAILED , "remote shell failed" },
102 { RERR_CMD_KILLED , "remote shell killed" },
103 { RERR_CMD_RUN , "remote command could not be run" },
104 { RERR_CMD_NOTFOUND,"remote command not found" },
109 * Map from rsync error code to name, or return NULL.
111 static char const *rerr_name(int code)
114 for (i = 0; rerr_names[i].name; i++) {
115 if (rerr_names[i].code == code)
116 return rerr_names[i].name;
121 static void logit(int priority, const char *buf)
123 if (logfile_was_closed)
126 fprintf(logfile_fp, "%s [%d] %s", timestring(time(NULL)), (int)getpid(), buf);
129 syslog(priority, "%s", buf);
133 static void syslog_init()
135 static int been_here = 0;
136 int options = LOG_PID;
143 options |= LOG_NDELAY;
147 openlog("rsyncd", options, lp_syslog_facility(module_id));
149 openlog("rsyncd", options);
153 logit(LOG_INFO, "rsyncd started\n");
157 static void logfile_open(void)
159 mode_t old_umask = umask(022 | orig_umask);
160 logfile_fp = fopen(logfile_name, "a");
163 int fopen_errno = errno;
164 /* Rsync falls back to using syslog on failure. */
166 rsyserr(FERROR, fopen_errno,
167 "failed to open log-file %s", logfile_name);
168 rprintf(FINFO, "Ignoring \"log file\" setting.\n");
172 void log_init(int restart)
174 if (log_initialised) {
177 if (strcmp(logfile_name, lp_log_file(module_id)) != 0) {
184 } else if (*logfile_name)
185 return; /* unchanged, non-empty "log file" names */
186 else if (lp_syslog_facility(-1) != lp_syslog_facility(module_id))
189 return; /* unchanged syslog settings */
193 /* This looks pointless, but it is needed in order for the
194 * C library on some systems to fetch the timezone info
195 * before the chroot. */
196 timestring(time(NULL));
198 /* Optionally use a log file instead of syslog. (Non-daemon
199 * rsyncs will have already set logfile_name, as needed.) */
200 if (am_daemon && !logfile_name)
201 logfile_name = lp_log_file(module_id);
202 if (logfile_name && *logfile_name)
208 void logfile_close(void)
211 logfile_was_closed = 1;
217 void logfile_reopen(void)
219 if (logfile_was_closed) {
220 logfile_was_closed = 0;
225 static void filtered_fwrite(FILE *f, const char *buf, int len, int use_isprint)
227 const char *s, *end = buf + len;
228 for (s = buf; s < end; s++) {
230 && *s == '\\' && s[1] == '#'
235 && ((use_isprint && !isPrint(s))
236 || *(uchar*)s < ' '))) {
237 if (s != buf && fwrite(buf, s - buf, 1, f) != 1)
238 exit_cleanup(RERR_MESSAGEIO);
239 fprintf(f, "\\#%03o", *(uchar*)s);
243 if (buf != end && fwrite(buf, end - buf, 1, f) != 1)
244 exit_cleanup(RERR_MESSAGEIO);
247 /* this is the underlying (unformatted) rsync debugging function. Call
248 * it with FINFO, FERROR_*, FWARNING, FLOG, or FCLIENT. Note: recursion
249 * can happen with certain fatal conditions. */
250 void rwrite(enum logcode code, const char *buf, int len, int is_utf8)
252 int trailing_CR_or_NL;
253 FILE *f = msgs2stderr ? stderr : stdout;
255 iconv_t ic = is_utf8 && ic_recv != (iconv_t)-1 ? ic_recv : ic_chck;
258 iconv_t ic = ic_chck;
263 exit_cleanup(RERR_MESSAGEIO);
274 } else if (send_msgs_to_gen) {
276 /* Pass the message to our sibling in native charset. */
277 send_msg((enum msgcode)code, buf, len, 0);
281 if (code == FERROR_SOCKET) /* This gets simplified for a non-sibling. */
283 else if (code == FERROR_UTF8) {
290 else if (am_daemon || logfile_name) {
293 int priority = code == FINFO || code == FLOG ? LOG_INFO : LOG_WARNING;
298 if (!log_initialised)
300 strlcpy(msg, buf, MIN((int)sizeof msg, len + 1));
301 logit(priority, msg);
304 if (code == FLOG || (am_daemon && !am_server))
306 } else if (code == FLOG)
309 if (quiet && code == FINFO)
313 enum msgcode msg = (enum msgcode)code;
314 if (protocol_version < 30) {
315 if (msg == MSG_ERROR)
316 msg = MSG_ERROR_XFER;
317 else if (msg == MSG_WARNING)
320 /* Pass the message to the non-server side. */
321 if (send_msg(msg, buf, len, !is_utf8))
324 /* TODO: can we send the error to the user somehow? */
346 fprintf(stderr, "Unknown logcode in rwrite(): %d [%s]\n", (int)code, who_am_i());
347 exit_cleanup(RERR_MESSAGEIO);
350 if (output_needs_newline) {
352 output_needs_newline = 0;
355 trailing_CR_or_NL = len && (buf[len-1] == '\n' || buf[len-1] == '\r')
358 if (len && buf[0] == '\r') {
365 if (ic != (iconv_t)-1) {
370 INIT_CONST_XBUF(outbuf, convbuf);
371 INIT_XBUF(inbuf, (char*)buf, len, (size_t)-1);
374 iconvbufs(ic, &inbuf, &outbuf, inbuf.pos ? 0 : ICB_INIT);
377 filtered_fwrite(f, convbuf, outbuf.len, 0);
380 if (!ierrno || ierrno == E2BIG)
382 fprintf(f, "\\#%03o", CVAL(inbuf.buf, inbuf.pos++));
387 filtered_fwrite(f, buf, len, !allow_8bit_chars);
389 if (trailing_CR_or_NL) {
390 fputc(trailing_CR_or_NL, f);
395 /* This is the rsync debugging function. Call it with FINFO, FERROR_*,
396 * FWARNING, FLOG, or FCLIENT. */
397 void rprintf(enum logcode code, const char *format, ...)
400 char buf[BIGPATHBUFLEN];
403 va_start(ap, format);
404 len = vsnprintf(buf, sizeof buf, format, ap);
407 /* Deal with buffer overruns. Instead of panicking, just
408 * truncate the resulting string. (Note that configure ensures
409 * that we have a vsnprintf() that doesn't ever return -1.) */
410 if (len > sizeof buf - 1) {
411 static const char ellipsis[] = "[...]";
413 /* Reset length, and zero-terminate the end of our buffer */
414 len = sizeof buf - 1;
417 /* Copy the ellipsis to the end of the string, but give
418 * us one extra character:
420 * v--- null byte at buf[sizeof buf - 1]
422 * -> abcd[...]00 <-- now two null bytes at end
424 * If the input format string has a trailing newline,
425 * we copy it into that extra null; if it doesn't, well,
426 * all we lose is one byte. */
427 memcpy(buf+len-sizeof ellipsis, ellipsis, sizeof ellipsis);
428 if (format[strlen(format)-1] == '\n') {
433 rwrite(code, buf, len, 0);
436 /* This is like rprintf, but it also tries to print some
437 * representation of the error code. Normally errcode = errno.
439 * Unlike rprintf, this always adds a newline and there should not be
440 * one in the format string.
442 * Note that since strerror might involve dynamically loading a
443 * message catalog we need to call it once before chroot-ing. */
444 void rsyserr(enum logcode code, int errcode, const char *format, ...)
447 char buf[BIGPATHBUFLEN];
450 strlcpy(buf, RSYNC_NAME ": ", sizeof buf);
451 len = (sizeof RSYNC_NAME ": ") - 1;
453 va_start(ap, format);
454 len += vsnprintf(buf + len, sizeof buf - len, format, ap);
457 if (len < sizeof buf) {
458 len += snprintf(buf + len, sizeof buf - len,
459 ": %s (%d)\n", strerror(errcode), errcode);
461 if (len >= sizeof buf)
462 exit_cleanup(RERR_MESSAGEIO);
464 rwrite(code, buf, len, 0);
467 void rflush(enum logcode code)
471 if (am_daemon || code == FLOG)
474 if (!am_server && (code == FINFO || code == FCLIENT))
482 void remember_initial_stats(void)
484 initial_data_read = total_data_read;
485 initial_data_written = total_data_written;
488 /* A generic logging routine for send/recv, with parameter substitiution. */
489 static void log_formatted(enum logcode code, const char *format, const char *op,
490 struct file_struct *file, const char *fname, int iflags,
493 char buf[MAXPATHLEN+1024], buf2[MAXPATHLEN], fmt[32];
501 /* We expand % codes one by one in place in buf. We don't
502 * copy in the terminating null of the inserted strings, but
503 * rather keep going until we reach the null of the format. */
504 total = strlcpy(buf, format, sizeof buf);
505 if (total > MAXPATHLEN) {
506 rprintf(FERROR, "log-format string is WAY too long!\n");
507 exit_cleanup(RERR_MESSAGEIO);
512 for (p = buf; (p = strchr(p, '%')) != NULL; ) {
522 while (isDigit(p) && c - fmt < (int)(sizeof fmt) - 8)
533 /* Note for %h and %a: it doesn't matter what fd we pass to
534 * client_{name,addr} because rsync_module will already have
535 * forced the answer to be cached (assuming, of course, for %h
536 * that lp_reverse_lookup(module_id) is true). */
540 n = lp_reverse_lookup(module_id)
541 ? client_name(0) : undetermined_hostname;
549 strlcat(fmt, "s", sizeof fmt);
550 snprintf(buf2, sizeof buf2, fmt,
551 do_big_num(F_LENGTH(file), humanize, NULL));
555 strlcat(fmt, "u", sizeof fmt);
556 snprintf(buf2, sizeof buf2, fmt,
557 uid_ndx ? F_OWNER(file) : 0);
561 if (!gid_ndx || file->flags & FLAG_SKIP_GROUP)
564 strlcat(fmt, "u", sizeof fmt);
565 snprintf(buf2, sizeof buf2, fmt,
571 strlcat(fmt, "d", sizeof fmt);
572 snprintf(buf2, sizeof buf2, fmt, (int)getpid());
576 n = c = timestring(file->modtime);
577 while ((c = strchr(c, ' ')) != NULL)
581 c = buf2 + MAXPATHLEN - PERMSTRING_SIZE - 1;
582 permstring(c, file->mode);
583 n = c + 1; /* skip the type char */
591 strlcpy(c, fname, MAXPATHLEN);
593 c = f_name(file, NULL);
594 if (am_sender && F_PATHNAME(file)) {
595 pathjoin(buf2, sizeof buf2,
596 F_PATHNAME(file), c);
597 clean_fname(buf2, 0);
599 strlcpy(c, buf2, MAXPATHLEN);
603 } else if (am_daemon && *c != '/') {
604 pathjoin(buf2, sizeof buf2,
605 curr_dir + module_dirlen, c);
606 clean_fname(buf2, 0);
608 strlcpy(c, buf2, MAXPATHLEN);
622 strlcpy(c, fname, MAXPATHLEN);
624 c = f_name(file, NULL);
625 if (S_ISDIR(file->mode))
626 strlcat(c, "/", MAXPATHLEN);
630 if (hlink && *hlink) {
632 strlcpy(buf2, " => ", sizeof buf2);
633 } else if (S_ISLNK(file->mode) && !fname) {
635 strlcpy(buf2, " -> ", sizeof buf2);
640 strlcpy(buf2, " ", sizeof buf2);
642 strlcat(fmt, "s", sizeof fmt);
643 snprintf(buf2 + 4, sizeof buf2 - 4, fmt, n);
647 n = lp_name(module_id);
650 n = timestring(time(NULL));
653 n = full_module_path;
659 if (!(iflags & ITEM_TRANSFER))
662 b = total_data_written - initial_data_written;
664 b = total_data_read - initial_data_read;
665 strlcat(fmt, "s", sizeof fmt);
666 snprintf(buf2, sizeof buf2, fmt,
667 do_big_num(b, humanize, NULL));
671 if (!(iflags & ITEM_TRANSFER))
674 b = total_data_written - initial_data_written;
676 b = total_data_read - initial_data_read;
677 strlcat(fmt, "s", sizeof fmt);
678 snprintf(buf2, sizeof buf2, fmt,
679 do_big_num(b, humanize, NULL));
683 if (protocol_version >= 30
684 && (iflags & ITEM_TRANSFER
685 || (always_checksum && S_ISREG(file->mode)))) {
687 const char *sum = iflags & ITEM_TRANSFER
688 ? sender_file_sum : F_SUM(file);
689 c = buf2 + checksum_len*2;
691 for (i = checksum_len; --i >= 0; ) {
695 *--c = x1 <= 9 ? x1 + '0' : x1 + 'a' - 10;
696 *--c = x2 <= 9 ? x2 + '0' : x2 + 'a' - 10;
699 memset(buf2, ' ', checksum_len*2);
700 buf2[checksum_len*2] = '\0';
705 if (iflags & ITEM_DELETED) {
709 n = c = buf2 + MAXPATHLEN - 32;
710 c[0] = iflags & ITEM_LOCAL_CHANGE
711 ? iflags & ITEM_XNAME_FOLLOWS ? 'h' : 'c'
712 : !(iflags & ITEM_TRANSFER) ? '.'
713 : !local_server && *op == 's' ? '<' : '>';
714 if (S_ISLNK(file->mode)) {
717 c[4] = !(iflags & ITEM_REPORT_TIME) ? '.'
718 : !preserve_times || !receiver_symlink_times
719 || (iflags & ITEM_REPORT_TIMEFAIL) ? 'T' : 't';
721 c[1] = S_ISDIR(file->mode) ? 'd'
722 : IS_SPECIAL(file->mode) ? 'S'
723 : IS_DEVICE(file->mode) ? 'D' : 'f';
724 c[3] = !(iflags & ITEM_REPORT_SIZE) ? '.' : 's';
725 c[4] = !(iflags & ITEM_REPORT_TIME) ? '.'
726 : !preserve_times ? 'T' : 't';
728 c[2] = !(iflags & ITEM_REPORT_CHANGE) ? '.' : 'c';
729 c[5] = !(iflags & ITEM_REPORT_PERMS) ? '.' : 'p';
730 c[6] = !(iflags & ITEM_REPORT_OWNER) ? '.' : 'o';
731 c[7] = !(iflags & ITEM_REPORT_GROUP) ? '.' : 'g';
732 c[8] = !(iflags & ITEM_REPORT_ATIME) ? '.' : 'u';
733 c[9] = !(iflags & ITEM_REPORT_ACL) ? '.' : 'a';
734 c[10] = !(iflags & ITEM_REPORT_XATTR) ? '.' : 'x';
737 if (iflags & (ITEM_IS_NEW|ITEM_MISSING_DATA)) {
738 char ch = iflags & ITEM_IS_NEW ? '+' : '?';
740 for (i = 2; c[i]; i++)
742 } else if (c[0] == '.' || c[0] == 'h' || c[0] == 'c') {
744 for (i = 2; c[i]; i++) {
749 for (i = 2; c[i]; i++)
756 /* "n" is the string to be inserted in place of this % code. */
759 if (n != buf2 && fmt[1]) {
760 strlcat(fmt, "s", sizeof fmt);
761 snprintf(buf2, sizeof buf2, fmt, n);
766 /* Subtract the length of the escape from the string's size. */
769 if (len + total >= (size_t)sizeof buf) {
771 "buffer overflow expanding %%%c -- exiting\n",
773 exit_cleanup(RERR_MESSAGEIO);
776 /* Shuffle the rest of the string along to make space for n */
777 if (len != (size_t)(p - s + 1))
778 memmove(s + len, p + 1, total - (s - buf) + 1);
781 /* Insert the contents of string "n", but NOT its null. */
785 /* Skip over inserted string; continue looking */
789 rwrite(code, buf, total, 0);
792 /* Return 1 if the format escape is in the log-format string (e.g. look for
793 * the 'b' in the "%9b" format escape). */
794 int log_format_has(const char *format, char esc)
801 for (p = format; (p = strchr(p, '%')) != NULL; ) {
802 for (p++; *p == '\''; p++) {} /*SHARED ITERATOR*/
807 while (*p == '\'') p++;
816 /* Log the transfer of a file. If the code is FCLIENT, the output just goes
817 * to stdout. If it is FLOG, it just goes to the log file. Otherwise we
819 void log_item(enum logcode code, struct file_struct *file, int iflags, const char *hlink)
821 const char *s_or_r = am_sender ? "send" : "recv";
823 if (code != FLOG && stdout_format && !am_server)
824 log_formatted(FCLIENT, stdout_format, s_or_r, file, NULL, iflags, hlink);
825 if (code != FCLIENT && logfile_format && *logfile_format)
826 log_formatted(FLOG, logfile_format, s_or_r, file, NULL, iflags, hlink);
829 void maybe_log_item(struct file_struct *file, int iflags, int itemizing,
832 int significant_flags = iflags & SIGNIFICANT_ITEM_FLAGS;
833 int see_item = itemizing && (significant_flags || *buf
834 || stdout_format_has_i > 1 || (INFO_GTE(NAME, 2) && stdout_format_has_i));
835 int local_change = iflags & ITEM_LOCAL_CHANGE && significant_flags;
837 if (logfile_name && !dry_run && see_item
838 && (significant_flags || logfile_format_has_i))
839 log_item(FLOG, file, iflags, buf);
840 } else if (see_item || local_change || *buf
841 || (S_ISDIR(file->mode) && significant_flags)) {
842 enum logcode code = significant_flags || logfile_format_has_i ? FINFO : FCLIENT;
843 log_item(code, file, iflags, buf);
847 void log_delete(const char *fname, int mode)
850 union file_extras ex[4]; /* just in case... */
851 struct file_struct file;
852 } x; /* Zero-initialized due to static declaration. */
853 int len = strlen(fname);
858 if (!INFO_GTE(DEL, 1) && !stdout_format)
860 else if (am_server && protocol_version >= 29 && len < MAXPATHLEN) {
862 len++; /* directories include trailing null */
863 send_msg(MSG_DELETED, fname, len, am_generator);
865 fmt = stdout_format_has_o_or_i ? stdout_format : "deleting %n";
866 log_formatted(FCLIENT, fmt, "del.", &x.file, fname, ITEM_DELETED, NULL);
869 if (!logfile_name || dry_run || !logfile_format)
872 fmt = logfile_format_has_o_or_i ? logfile_format : "deleting %n";
873 log_formatted(FLOG, fmt, "del.", &x.file, fname, ITEM_DELETED, NULL);
877 * Called when the transfer is interrupted for some reason.
879 * Code is one of the RERR_* codes from errcode.h, or terminating
882 void log_exit(int code, const char *file, int line)
885 rprintf(FLOG,"sent %s bytes received %s bytes total size %s\n",
886 comma_num(stats.total_written),
887 comma_num(stats.total_read),
888 comma_num(stats.total_size));
889 } else if (am_server != 2) {
892 name = rerr_name(code);
894 name = "unexplained error";
896 /* VANISHED is not an error, only a warning */
897 if (code == RERR_VANISHED) {
898 rprintf(FWARNING, "rsync warning: %s (code %d) at %s(%d) [%s=%s]\n",
899 name, code, file, line, who_am_i(), RSYNC_VERSION);
901 rprintf(FERROR, "rsync error: %s (code %d) at %s(%d) [%s=%s]\n",
902 name, code, file, line, who_am_i(), RSYNC_VERSION);