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-2008 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 checksum_len;
35 extern int allow_8bit_chars;
36 extern int protocol_version;
37 extern int always_checksum;
38 extern int preserve_times;
41 extern int stdout_format_has_i;
42 extern int stdout_format_has_o_or_i;
43 extern int logfile_format_has_i;
44 extern int logfile_format_has_o_or_i;
45 extern int receiver_symlink_times;
46 extern mode_t orig_umask;
47 extern char *auth_user;
48 extern char *stdout_format;
49 extern char *logfile_format;
50 extern char *logfile_name;
52 extern iconv_t ic_chck;
55 extern iconv_t ic_send, ic_recv;
57 extern char curr_dir[];
58 extern char *module_dir;
59 extern unsigned int module_dirlen;
60 extern char sender_file_sum[];
62 static int log_initialised;
63 static int logfile_was_closed;
64 static FILE *logfile_fp;
67 int got_xfer_error = 0;
68 int output_needs_newline = 0;
73 } const rerr_names[] = {
74 { RERR_SYNTAX , "syntax or usage error" },
75 { RERR_PROTOCOL , "protocol incompatibility" },
76 { RERR_FILESELECT , "errors selecting input/output files, dirs" },
77 { RERR_UNSUPPORTED, "requested action not supported" },
78 { RERR_STARTCLIENT, "error starting client-server protocol" },
79 { RERR_SOCKETIO , "error in socket IO" },
80 { RERR_FILEIO , "error in file IO" },
81 { RERR_STREAMIO , "error in rsync protocol data stream" },
82 { RERR_MESSAGEIO , "errors with program diagnostics" },
83 { RERR_IPC , "error in IPC code" },
84 { RERR_CRASHED , "sibling process crashed" },
85 { RERR_TERMINATED , "sibling process terminated abnormally" },
86 { RERR_SIGNAL1 , "received SIGUSR1" },
87 { RERR_SIGNAL , "received SIGINT, SIGTERM, or SIGHUP" },
88 { RERR_WAITCHILD , "waitpid() failed" },
89 { RERR_MALLOC , "error allocating core memory buffers" },
90 { RERR_PARTIAL , "some files could not be transferred" },
91 { RERR_VANISHED , "some files vanished before they could be transferred" },
92 { RERR_TIMEOUT , "timeout in data send/receive" },
93 { RERR_CONTIMEOUT , "timeout waiting for daemon connection" },
94 { RERR_CMD_FAILED , "remote shell failed" },
95 { RERR_CMD_KILLED , "remote shell killed" },
96 { RERR_CMD_RUN , "remote command could not be run" },
97 { RERR_CMD_NOTFOUND,"remote command not found" },
98 { RERR_DEL_LIMIT , "the --max-delete limit stopped deletions" },
103 * Map from rsync error code to name, or return NULL.
105 static char const *rerr_name(int code)
108 for (i = 0; rerr_names[i].name; i++) {
109 if (rerr_names[i].code == code)
110 return rerr_names[i].name;
115 static void logit(int priority, const char *buf)
117 if (logfile_was_closed)
120 fprintf(logfile_fp, "%s [%d] %s",
121 timestring(time(NULL)), (int)getpid(), buf);
124 syslog(priority, "%s", buf);
128 static void syslog_init()
130 static int been_here = 0;
131 int options = LOG_PID;
138 options |= LOG_NDELAY;
142 openlog("rsyncd", options, lp_syslog_facility(module_id));
144 openlog("rsyncd", options);
148 logit(LOG_INFO, "rsyncd started\n");
152 static void logfile_open(void)
154 mode_t old_umask = umask(022 | orig_umask);
155 logfile_fp = fopen(logfile_name, "a");
158 int fopen_errno = errno;
159 /* Rsync falls back to using syslog on failure. */
161 rsyserr(FERROR, fopen_errno,
162 "failed to open log-file %s", logfile_name);
163 rprintf(FINFO, "Ignoring \"log file\" setting.\n");
167 void log_init(int restart)
169 if (log_initialised) {
172 if (strcmp(logfile_name, lp_log_file(module_id)) != 0) {
179 } else if (*logfile_name)
180 return; /* unchanged, non-empty "log file" names */
181 else if (lp_syslog_facility(-1) != lp_syslog_facility(module_id))
184 return; /* unchanged syslog settings */
188 /* This looks pointless, but it is needed in order for the
189 * C library on some systems to fetch the timezone info
190 * before the chroot. */
191 timestring(time(NULL));
193 /* Optionally use a log file instead of syslog. (Non-daemon
194 * rsyncs will have already set logfile_name, as needed.) */
195 if (am_daemon && !logfile_name)
196 logfile_name = lp_log_file(module_id);
197 if (logfile_name && *logfile_name)
203 void logfile_close(void)
206 logfile_was_closed = 1;
212 void logfile_reopen(void)
214 if (logfile_was_closed) {
215 logfile_was_closed = 0;
220 static void filtered_fwrite(FILE *f, const char *buf, int len, int use_isprint)
222 const char *s, *end = buf + len;
223 for (s = buf; s < end; s++) {
225 && *s == '\\' && s[1] == '#'
230 && ((use_isprint && !isPrint(s))
231 || *(uchar*)s < ' '))) {
232 if (s != buf && fwrite(buf, s - buf, 1, f) != 1)
233 exit_cleanup(RERR_MESSAGEIO);
234 fprintf(f, "\\#%03o", *(uchar*)s);
238 if (buf != end && fwrite(buf, end - buf, 1, f) != 1)
239 exit_cleanup(RERR_MESSAGEIO);
242 /* this is the underlying (unformatted) rsync debugging function. Call
243 * it with FINFO, FERROR_*, FWARNING, FLOG, or FCLIENT. Note: recursion
244 * can happen with certain fatal conditions. */
245 void rwrite(enum logcode code, const char *buf, int len, int is_utf8)
247 int trailing_CR_or_NL;
250 iconv_t ic = is_utf8 && ic_recv != (iconv_t)-1 ? ic_recv : ic_chck;
253 iconv_t ic = ic_chck;
258 exit_cleanup(RERR_MESSAGEIO);
260 if (am_server && msg_fd_out >= 0) {
262 /* Pass the message to our sibling. */
263 send_msg((enum msgcode)code, buf, len, 0);
267 if (code == FERROR_SOCKET) /* This gets simplified for a non-sibling. */
272 else if (am_daemon || logfile_name) {
275 int priority = code == FINFO || code == FLOG ? LOG_INFO : LOG_WARNING;
280 if (!log_initialised)
282 strlcpy(msg, buf, MIN((int)sizeof msg, len + 1));
283 logit(priority, msg);
286 if (code == FLOG || (am_daemon && !am_server))
288 } else if (code == FLOG)
291 if (quiet && code == FINFO)
295 enum msgcode msg = (enum msgcode)code;
296 if (protocol_version < 30) {
297 if (msg == MSG_ERROR)
298 msg = MSG_ERROR_XFER;
299 else if (msg == MSG_WARNING)
302 /* Pass the message to the non-server side. */
303 if (send_msg(msg, buf, len, !is_utf8))
306 /* TODO: can we send the error to the user somehow? */
320 f = am_server ? stderr : stdout;
323 exit_cleanup(RERR_MESSAGEIO);
326 if (output_needs_newline) {
328 output_needs_newline = 0;
331 trailing_CR_or_NL = len && (buf[len-1] == '\n' || buf[len-1] == '\r')
334 if (len && buf[0] == '\r') {
341 if (ic != (iconv_t)-1) {
346 INIT_CONST_XBUF(outbuf, convbuf);
347 INIT_XBUF(inbuf, (char*)buf, len, -1);
350 iconvbufs(ic, &inbuf, &outbuf, 0);
353 filtered_fwrite(f, convbuf, outbuf.len, 0);
356 if (!ierrno || ierrno == E2BIG)
358 fprintf(f, "\\#%03o", CVAL(inbuf.buf, inbuf.pos++));
363 filtered_fwrite(f, buf, len, !allow_8bit_chars);
365 if (trailing_CR_or_NL) {
366 fputc(trailing_CR_or_NL, f);
371 /* This is the rsync debugging function. Call it with FINFO, FERROR_*,
372 * FWARNING, FLOG, or FCLIENT. */
373 void rprintf(enum logcode code, const char *format, ...)
376 char buf[BIGPATHBUFLEN];
379 va_start(ap, format);
380 len = vsnprintf(buf, sizeof buf, format, ap);
383 /* Deal with buffer overruns. Instead of panicking, just
384 * truncate the resulting string. (Note that configure ensures
385 * that we have a vsnprintf() that doesn't ever return -1.) */
386 if (len > sizeof buf - 1) {
387 static const char ellipsis[] = "[...]";
389 /* Reset length, and zero-terminate the end of our buffer */
390 len = sizeof buf - 1;
393 /* Copy the ellipsis to the end of the string, but give
394 * us one extra character:
396 * v--- null byte at buf[sizeof buf - 1]
398 * -> abcd[...]00 <-- now two null bytes at end
400 * If the input format string has a trailing newline,
401 * we copy it into that extra null; if it doesn't, well,
402 * all we lose is one byte. */
403 memcpy(buf+len-sizeof ellipsis, ellipsis, sizeof ellipsis);
404 if (format[strlen(format)-1] == '\n') {
409 rwrite(code, buf, len, 0);
412 /* This is like rprintf, but it also tries to print some
413 * representation of the error code. Normally errcode = errno.
415 * Unlike rprintf, this always adds a newline and there should not be
416 * one in the format string.
418 * Note that since strerror might involve dynamically loading a
419 * message catalog we need to call it once before chroot-ing. */
420 void rsyserr(enum logcode code, int errcode, const char *format, ...)
423 char buf[BIGPATHBUFLEN];
426 strlcpy(buf, RSYNC_NAME ": ", sizeof buf);
427 len = (sizeof RSYNC_NAME ": ") - 1;
429 va_start(ap, format);
430 len += vsnprintf(buf + len, sizeof buf - len, format, ap);
433 if (len < sizeof buf) {
434 len += snprintf(buf + len, sizeof buf - len,
435 ": %s (%d)\n", strerror(errcode), errcode);
437 if (len >= sizeof buf)
438 exit_cleanup(RERR_MESSAGEIO);
440 rwrite(code, buf, len, 0);
443 void rflush(enum logcode code)
447 if (am_daemon || code == FLOG)
450 if (!am_server && (code == FINFO || code == FCLIENT))
458 /* A generic logging routine for send/recv, with parameter substitiution. */
459 static void log_formatted(enum logcode code, const char *format, const char *op,
460 struct file_struct *file, const char *fname,
461 struct stats *initial_stats, int iflags,
464 char buf[MAXPATHLEN+1024], buf2[MAXPATHLEN], fmt[32];
472 /* We expand % codes one by one in place in buf. We don't
473 * copy in the terminating null of the inserted strings, but
474 * rather keep going until we reach the null of the format. */
475 total = strlcpy(buf, format, sizeof buf);
476 if (total > MAXPATHLEN) {
477 rprintf(FERROR, "log-format string is WAY too long!\n");
478 exit_cleanup(RERR_MESSAGEIO);
483 for (p = buf; (p = strchr(p, '%')) != NULL; ) {
488 while (isDigit(p) && c - fmt < (int)(sizeof fmt) - 8)
505 strlcat(fmt, "s", sizeof fmt);
506 snprintf(buf2, sizeof buf2, fmt,
507 big_num(F_LENGTH(file), 0));
511 strlcat(fmt, "u", sizeof fmt);
512 snprintf(buf2, sizeof buf2, fmt,
513 uid_ndx ? F_OWNER(file) : 0);
517 if (!gid_ndx || file->flags & FLAG_SKIP_GROUP)
520 strlcat(fmt, "u", sizeof fmt);
521 snprintf(buf2, sizeof buf2, fmt,
527 strlcat(fmt, "ld", sizeof fmt);
528 snprintf(buf2, sizeof buf2, fmt,
533 n = c = timestring(file->modtime);
534 while ((c = strchr(c, ' ')) != NULL)
538 c = buf2 + MAXPATHLEN - PERMSTRING_SIZE - 1;
539 permstring(c, file->mode);
540 n = c + 1; /* skip the type char */
548 strlcpy(c, fname, MAXPATHLEN);
550 c = f_name(file, NULL);
551 if (am_sender && F_PATHNAME(file)) {
552 pathjoin(buf2, sizeof buf2,
553 F_PATHNAME(file), c);
554 clean_fname(buf2, 0);
556 strlcpy(c, buf2, MAXPATHLEN);
560 } else if (am_daemon && *c != '/') {
561 pathjoin(buf2, sizeof buf2,
562 curr_dir + module_dirlen, c);
563 clean_fname(buf2, 0);
565 strlcpy(c, buf2, MAXPATHLEN);
579 strlcpy(c, fname, MAXPATHLEN);
581 c = f_name(file, NULL);
582 if (S_ISDIR(file->mode))
583 strlcat(c, "/", MAXPATHLEN);
587 if (hlink && *hlink) {
589 strlcpy(buf2, " => ", sizeof buf2);
590 } else if (S_ISLNK(file->mode) && !fname) {
592 strlcpy(buf2, " -> ", sizeof buf2);
597 strlcpy(buf2, " ", sizeof buf2);
599 strlcat(fmt, "s", sizeof fmt);
600 snprintf(buf2 + 4, sizeof buf2 - 4, fmt, n);
604 n = lp_name(module_id);
607 n = timestring(time(NULL));
617 b = stats.total_written -
618 initial_stats->total_written;
620 b = stats.total_read -
621 initial_stats->total_read;
623 strlcat(fmt, "s", sizeof fmt);
624 snprintf(buf2, sizeof buf2, fmt, big_num(b, 0));
629 b = stats.total_written -
630 initial_stats->total_written;
632 b = stats.total_read -
633 initial_stats->total_read;
635 strlcat(fmt, "s", sizeof fmt);
636 snprintf(buf2, sizeof buf2, fmt, big_num(b, 0));
640 if (protocol_version >= 30
641 && (iflags & ITEM_TRANSFER
642 || (always_checksum && S_ISREG(file->mode)))) {
644 const char *sum = iflags & ITEM_TRANSFER
645 ? sender_file_sum : F_SUM(file);
646 c = buf2 + checksum_len*2;
648 for (i = checksum_len; --i >= 0; ) {
652 *--c = x1 <= 9 ? x1 + '0' : x1 + 'a' - 10;
653 *--c = x2 <= 9 ? x2 + '0' : x2 + 'a' - 10;
656 memset(buf2, ' ', checksum_len*2);
657 buf2[checksum_len*2] = '\0';
662 if (iflags & ITEM_DELETED) {
666 n = c = buf2 + MAXPATHLEN - 32;
667 c[0] = iflags & ITEM_LOCAL_CHANGE
668 ? iflags & ITEM_XNAME_FOLLOWS ? 'h' : 'c'
669 : !(iflags & ITEM_TRANSFER) ? '.'
670 : !local_server && *op == 's' ? '<' : '>';
671 if (S_ISLNK(file->mode)) {
674 c[4] = !(iflags & ITEM_REPORT_TIME) ? '.'
675 : !preserve_times || !receiver_symlink_times
676 || (iflags & ITEM_REPORT_TIMEFAIL) ? 'T' : 't';
678 c[1] = S_ISDIR(file->mode) ? 'd'
679 : IS_SPECIAL(file->mode) ? 'S'
680 : IS_DEVICE(file->mode) ? 'D' : 'f';
681 c[3] = !(iflags & ITEM_REPORT_SIZE) ? '.' : 's';
682 c[4] = !(iflags & ITEM_REPORT_TIME) ? '.'
683 : !preserve_times ? 'T' : 't';
685 c[2] = !(iflags & ITEM_REPORT_CHANGE) ? '.' : 'c';
686 c[5] = !(iflags & ITEM_REPORT_PERMS) ? '.' : 'p';
687 c[6] = !(iflags & ITEM_REPORT_OWNER) ? '.' : 'o';
688 c[7] = !(iflags & ITEM_REPORT_GROUP) ? '.' : 'g';
689 c[8] = !(iflags & ITEM_REPORT_ATIME) ? '.' : 'u';
690 c[9] = !(iflags & ITEM_REPORT_ACL) ? '.' : 'a';
691 c[10] = !(iflags & ITEM_REPORT_XATTR) ? '.' : 'x';
694 if (iflags & (ITEM_IS_NEW|ITEM_MISSING_DATA)) {
695 char ch = iflags & ITEM_IS_NEW ? '+' : '?';
697 for (i = 2; c[i]; i++)
699 } else if (c[0] == '.' || c[0] == 'h' || c[0] == 'c') {
701 for (i = 2; c[i]; i++) {
706 for (i = 2; c[i]; i++)
713 /* "n" is the string to be inserted in place of this % code. */
716 if (n != buf2 && fmt[1]) {
717 strlcat(fmt, "s", sizeof fmt);
718 snprintf(buf2, sizeof buf2, fmt, n);
723 /* Subtract the length of the escape from the string's size. */
726 if (len + total >= (size_t)sizeof buf) {
728 "buffer overflow expanding %%%c -- exiting\n",
730 exit_cleanup(RERR_MESSAGEIO);
733 /* Shuffle the rest of the string along to make space for n */
734 if (len != (size_t)(p - s + 1))
735 memmove(s + len, p + 1, total - (s - buf) + 1);
738 /* Insert the contents of string "n", but NOT its null. */
742 /* Skip over inserted string; continue looking */
746 rwrite(code, buf, total, 0);
749 /* Return 1 if the format escape is in the log-format string (e.g. look for
750 * the 'b' in the "%9b" format escape). */
751 int log_format_has(const char *format, char esc)
758 for (p = format; (p = strchr(p, '%')) != NULL; ) {
771 /* Log the transfer of a file. If the code is FCLIENT, the output just goes
772 * to stdout. If it is FLOG, it just goes to the log file. Otherwise we
774 void log_item(enum logcode code, struct file_struct *file,
775 struct stats *initial_stats, int iflags, const char *hlink)
777 const char *s_or_r = am_sender ? "send" : "recv";
779 if (code != FLOG && stdout_format && !am_server) {
780 log_formatted(FCLIENT, stdout_format, s_or_r,
781 file, NULL, initial_stats, iflags, hlink);
783 if (code != FCLIENT && logfile_format && *logfile_format) {
784 log_formatted(FLOG, logfile_format, s_or_r,
785 file, NULL, initial_stats, iflags, hlink);
789 void maybe_log_item(struct file_struct *file, int iflags, int itemizing,
792 int significant_flags = iflags & SIGNIFICANT_ITEM_FLAGS;
793 int see_item = itemizing && (significant_flags || *buf
794 || stdout_format_has_i > 1 || (INFO_GTE(NAME, 2) && stdout_format_has_i));
795 int local_change = iflags & ITEM_LOCAL_CHANGE && significant_flags;
797 if (logfile_name && !dry_run && see_item
798 && (significant_flags || logfile_format_has_i))
799 log_item(FLOG, file, &stats, iflags, buf);
800 } else if (see_item || local_change || *buf
801 || (S_ISDIR(file->mode) && significant_flags)) {
802 enum logcode code = significant_flags || logfile_format_has_i ? FINFO : FCLIENT;
803 log_item(code, file, &stats, iflags, buf);
807 void log_delete(const char *fname, int mode)
810 union file_extras ex[4]; /* just in case... */
811 struct file_struct file;
813 int len = strlen(fname);
818 if (!INFO_GTE(DEL, 1) && !stdout_format)
820 else if (am_server && protocol_version >= 29 && len < MAXPATHLEN) {
822 len++; /* directories include trailing null */
823 send_msg(MSG_DELETED, fname, len, am_generator);
825 fmt = stdout_format_has_o_or_i ? stdout_format : "deleting %n";
826 log_formatted(FCLIENT, fmt, "del.", &x.file, fname, &stats,
830 if (!logfile_name || dry_run || !logfile_format)
833 fmt = logfile_format_has_o_or_i ? logfile_format : "deleting %n";
834 log_formatted(FLOG, fmt, "del.", &x.file, fname, &stats, ITEM_DELETED, NULL);
838 * Called when the transfer is interrupted for some reason.
840 * Code is one of the RERR_* codes from errcode.h, or terminating
843 void log_exit(int code, const char *file, int line)
846 rprintf(FLOG,"sent %s bytes received %s bytes total size %s\n",
847 big_num(stats.total_written, 0),
848 big_num(stats.total_read, 0),
849 big_num(stats.total_size, 0));
850 } else if (am_server != 2) {
853 name = rerr_name(code);
855 name = "unexplained error";
857 /* VANISHED is not an error, only a warning */
858 if (code == RERR_VANISHED) {
859 rprintf(FWARNING, "rsync warning: %s (code %d) at %s(%d) [%s=%s]\n",
860 name, code, file, line, who_am_i(), RSYNC_VERSION);
862 rprintf(FERROR, "rsync error: %s (code %d) at %s(%d) [%s=%s]\n",
863 name, code, file, line, who_am_i(), RSYNC_VERSION);