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 version 2 as
10 * published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
23 #if defined HAVE_ICONV_OPEN && defined HAVE_ICONV_H
32 extern int local_server;
35 extern int msg_fd_out;
36 extern int allow_8bit_chars;
37 extern int protocol_version;
38 extern int preserve_times;
39 extern int preserve_uid;
40 extern int preserve_gid;
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 mode_t orig_umask;
46 extern char *auth_user;
47 extern char *stdout_format;
48 extern char *logfile_format;
49 extern char *logfile_name;
50 #if defined HAVE_ICONV_OPEN && defined HAVE_ICONV_H
51 extern iconv_t ic_chck;
53 extern char curr_dir[];
54 extern unsigned int module_dirlen;
56 static int log_initialised;
57 static int logfile_was_closed;
58 static FILE *logfile_fp;
61 int log_got_error = 0;
66 } const rerr_names[] = {
67 { RERR_SYNTAX , "syntax or usage error" },
68 { RERR_PROTOCOL , "protocol incompatibility" },
69 { RERR_FILESELECT , "errors selecting input/output files, dirs" },
70 { RERR_UNSUPPORTED, "requested action not supported" },
71 { RERR_STARTCLIENT, "error starting client-server protocol" },
72 { RERR_SOCKETIO , "error in socket IO" },
73 { RERR_FILEIO , "error in file IO" },
74 { RERR_STREAMIO , "error in rsync protocol data stream" },
75 { RERR_MESSAGEIO , "errors with program diagnostics" },
76 { RERR_IPC , "error in IPC code" },
77 { RERR_CRASHED , "sibling process crashed" },
78 { RERR_TERMINATED , "sibling process terminated abnormally" },
79 { RERR_SIGNAL1 , "received SIGUSR1" },
80 { RERR_SIGNAL , "received SIGINT, SIGTERM, or SIGHUP" },
81 { RERR_WAITCHILD , "waitpid() failed" },
82 { RERR_MALLOC , "error allocating core memory buffers" },
83 { RERR_PARTIAL , "some files could not be transferred" },
84 { RERR_VANISHED , "some files vanished before they could be transferred" },
85 { RERR_TIMEOUT , "timeout in data send/receive" },
86 { RERR_CMD_FAILED , "remote shell failed" },
87 { RERR_CMD_KILLED , "remote shell killed" },
88 { RERR_CMD_RUN , "remote command could not be run" },
89 { RERR_CMD_NOTFOUND,"remote command not found" },
90 { RERR_DEL_LIMIT , "the --max-delete limit stopped deletions" },
95 * Map from rsync error code to name, or return NULL.
97 static char const *rerr_name(int code)
100 for (i = 0; rerr_names[i].name; i++) {
101 if (rerr_names[i].code == code)
102 return rerr_names[i].name;
107 static void logit(int priority, const char *buf)
109 if (logfile_was_closed)
112 fprintf(logfile_fp, "%s [%d] %s",
113 timestring(time(NULL)), (int)getpid(), buf);
116 syslog(priority, "%s", buf);
120 static void syslog_init()
122 static int been_here = 0;
123 int options = LOG_PID;
130 options |= LOG_NDELAY;
134 openlog("rsyncd", options, lp_syslog_facility(module_id));
136 openlog("rsyncd", options);
140 logit(LOG_INFO, "rsyncd started\n");
144 static void logfile_open(void)
146 mode_t old_umask = umask(022 | orig_umask);
147 logfile_fp = fopen(logfile_name, "a");
150 int fopen_errno = errno;
151 /* Rsync falls back to using syslog on failure. */
153 rsyserr(FERROR, fopen_errno,
154 "failed to open log-file %s", logfile_name);
155 rprintf(FINFO, "Ignoring \"log file\" setting.\n");
159 void log_init(int restart)
161 if (log_initialised) {
164 if (strcmp(logfile_name, lp_log_file(module_id)) != 0) {
171 } else if (*logfile_name)
172 return; /* unchanged, non-empty "log file" names */
173 else if (lp_syslog_facility(-1) != lp_syslog_facility(module_id))
176 return; /* unchanged syslog settings */
180 /* This looks pointless, but it is needed in order for the
181 * C library on some systems to fetch the timezone info
182 * before the chroot. */
183 timestring(time(NULL));
185 /* Optionally use a log file instead of syslog. (Non-daemon
186 * rsyncs will have already set logfile_name, as needed.) */
187 if (am_daemon && !logfile_name)
188 logfile_name = lp_log_file(module_id);
189 if (logfile_name && *logfile_name)
195 void logfile_close(void)
198 logfile_was_closed = 1;
204 void logfile_reopen(void)
206 if (logfile_was_closed) {
207 logfile_was_closed = 0;
212 static void filtered_fwrite(FILE *f, const char *buf, int len, int use_isprint)
214 const char *s, *end = buf + len;
215 for (s = buf; s < end; s++) {
217 && *s == '\\' && s[1] == '#'
222 && ((use_isprint && !isPrint(s))
223 || *(uchar*)s < ' '))) {
224 if (s != buf && fwrite(buf, s - buf, 1, f) != 1)
225 exit_cleanup(RERR_MESSAGEIO);
226 fprintf(f, "\\#%03o", *(uchar*)s);
230 if (buf != end && fwrite(buf, end - buf, 1, f) != 1)
231 exit_cleanup(RERR_MESSAGEIO);
234 /* this is the underlying (unformatted) rsync debugging function. Call
235 * it with FINFO, FERROR or FLOG. Note: recursion can happen with
236 * certain fatal conditions. */
237 void rwrite(enum logcode code, const char *buf, int len)
239 int trailing_CR_or_NL;
243 exit_cleanup(RERR_MESSAGEIO);
245 if (am_server && msg_fd_out >= 0) {
246 /* Pass the message to our sibling. */
247 send_msg((enum msgcode)code, buf, len);
251 if (code == FSOCKERR) /* This gets simplified for a non-sibling. */
256 else if (am_daemon || logfile_name) {
259 int priority = code == FERROR ? LOG_WARNING : LOG_INFO;
264 if (!log_initialised)
266 strlcpy(msg, buf, MIN((int)sizeof msg, len + 1));
267 logit(priority, msg);
270 if (code == FLOG || (am_daemon && !am_server))
272 } else if (code == FLOG)
275 if (quiet && code != FERROR)
279 /* Pass the message to the non-server side. */
280 if (send_msg((enum msgcode)code, buf, len))
283 /* TODO: can we send the error to the user somehow? */
294 f = am_server ? stderr : stdout;
297 exit_cleanup(RERR_MESSAGEIO);
300 trailing_CR_or_NL = len && (buf[len-1] == '\n' || buf[len-1] == '\r')
303 #if defined HAVE_ICONV_OPEN && defined HAVE_ICONV_H
307 if (ic_chck != (iconv_t)-1) {
309 ICONV_CONST char *in_buf = (ICONV_CONST char *)buf;
310 char *out_buf = convbuf;
311 size_t in_cnt = len, out_cnt = sizeof convbuf - 1;
313 iconv(ic_chck, NULL, 0, NULL, 0);
314 while (iconv(ic_chck, &in_buf,&in_cnt,
315 &out_buf,&out_cnt) == (size_t)-1) {
316 if (out_buf != convbuf) {
317 filtered_fwrite(f, convbuf, out_buf - convbuf, 0);
319 out_cnt = sizeof convbuf - 1;
323 fprintf(f, "\\#%03o", *(uchar*)in_buf++);
326 if (out_buf != convbuf)
327 filtered_fwrite(f, convbuf, out_buf - convbuf, 0);
330 filtered_fwrite(f, buf, len, !allow_8bit_chars);
332 if (trailing_CR_or_NL) {
333 fputc(trailing_CR_or_NL, f);
338 /* This is the rsync debugging function. Call it with FINFO, FERROR or
340 void rprintf(enum logcode code, const char *format, ...)
343 char buf[BIGPATHBUFLEN];
346 va_start(ap, format);
347 len = vsnprintf(buf, sizeof buf, format, ap);
350 /* Deal with buffer overruns. Instead of panicking, just
351 * truncate the resulting string. (Note that configure ensures
352 * that we have a vsnprintf() that doesn't ever return -1.) */
353 if (len > sizeof buf - 1) {
354 static const char ellipsis[] = "[...]";
356 /* Reset length, and zero-terminate the end of our buffer */
357 len = sizeof buf - 1;
360 /* Copy the ellipsis to the end of the string, but give
361 * us one extra character:
363 * v--- null byte at buf[sizeof buf - 1]
365 * -> abcd[...]00 <-- now two null bytes at end
367 * If the input format string has a trailing newline,
368 * we copy it into that extra null; if it doesn't, well,
369 * all we lose is one byte. */
370 memcpy(buf+len-sizeof ellipsis, ellipsis, sizeof ellipsis);
371 if (format[strlen(format)-1] == '\n') {
376 rwrite(code, buf, len);
379 /* This is like rprintf, but it also tries to print some
380 * representation of the error code. Normally errcode = errno.
382 * Unlike rprintf, this always adds a newline and there should not be
383 * one in the format string.
385 * Note that since strerror might involve dynamically loading a
386 * message catalog we need to call it once before chroot-ing. */
387 void rsyserr(enum logcode code, int errcode, const char *format, ...)
390 char buf[BIGPATHBUFLEN];
393 strlcpy(buf, RSYNC_NAME ": ", sizeof buf);
394 len = (sizeof RSYNC_NAME ": ") - 1;
396 va_start(ap, format);
397 len += vsnprintf(buf + len, sizeof buf - len, format, ap);
400 if (len < sizeof buf) {
401 len += snprintf(buf + len, sizeof buf - len,
402 ": %s (%d)\n", strerror(errcode), errcode);
404 if (len >= sizeof buf)
405 exit_cleanup(RERR_MESSAGEIO);
407 rwrite(code, buf, len);
410 void rflush(enum logcode code)
414 if (am_daemon || code == FLOG)
417 if (code == FERROR || am_server)
425 /* A generic logging routine for send/recv, with parameter substitiution. */
426 static void log_formatted(enum logcode code, const char *format, const char *op,
427 struct file_struct *file, const char *fname,
428 struct stats *initial_stats, int iflags,
431 char buf[MAXPATHLEN+1024], buf2[MAXPATHLEN], fmt[32];
439 /* We expand % codes one by one in place in buf. We don't
440 * copy in the terminating null of the inserted strings, but
441 * rather keep going until we reach the null of the format. */
442 total = strlcpy(buf, format, sizeof buf);
443 if (total > MAXPATHLEN) {
444 rprintf(FERROR, "log-format string is WAY too long!\n");
445 exit_cleanup(RERR_MESSAGEIO);
450 for (p = buf; (p = strchr(p, '%')) != NULL; ) {
455 while (isDigit(p) && c - fmt < (int)(sizeof fmt) - 8)
472 strlcat(fmt, ".0f", sizeof fmt);
473 snprintf(buf2, sizeof buf2, fmt,
474 (double)F_LENGTH(file));
478 strlcat(fmt, "ld", sizeof fmt);
479 snprintf(buf2, sizeof buf2, fmt,
480 preserve_uid ? (long)F_UID(file) : 0);
484 if (!preserve_gid || F_GID(file) == GID_NONE)
487 strlcat(fmt, "ld", sizeof fmt);
488 snprintf(buf2, sizeof buf2, fmt,
494 strlcat(fmt, "ld", sizeof fmt);
495 snprintf(buf2, sizeof buf2, fmt,
500 n = c = timestring(file->modtime);
501 while ((c = strchr(p, ' ')) != NULL)
505 c = buf2 + MAXPATHLEN - PERMSTRING_SIZE - 1;
506 permstring(c, file->mode);
507 n = c + 1; /* skip the type char */
515 strlcpy(c, fname, MAXPATHLEN);
517 c = f_name(file, NULL);
518 if (am_sender && F_PATHNAME(file)) {
519 pathjoin(buf2, sizeof buf2,
520 F_PATHNAME(file), c);
521 clean_fname(buf2, 0);
523 strlcpy(c, buf2, MAXPATHLEN);
527 } else if (am_daemon && *c != '/') {
528 pathjoin(buf2, sizeof buf2,
529 curr_dir + module_dirlen, c);
530 clean_fname(buf2, 0);
532 strlcpy(c, buf2, MAXPATHLEN);
546 strlcpy(c, fname, MAXPATHLEN);
548 c = f_name(file, NULL);
549 if (S_ISDIR(file->mode))
550 strlcat(c, "/", MAXPATHLEN);
554 if (hlink && *hlink) {
556 strlcpy(buf2, " => ", sizeof buf2);
557 } else if (S_ISLNK(file->mode) && !fname) {
559 strlcpy(buf2, " -> ", sizeof buf2);
564 strlcpy(buf2, " ", sizeof buf2);
566 strlcat(fmt, "s", sizeof fmt);
567 snprintf(buf2 + 4, sizeof buf2 - 4, fmt, n);
571 n = lp_name(module_id);
574 n = timestring(time(NULL));
577 n = lp_path(module_id);
584 b = stats.total_written -
585 initial_stats->total_written;
587 b = stats.total_read -
588 initial_stats->total_read;
590 strlcat(fmt, ".0f", sizeof fmt);
591 snprintf(buf2, sizeof buf2, fmt, (double)b);
596 b = stats.total_written -
597 initial_stats->total_written;
599 b = stats.total_read -
600 initial_stats->total_read;
602 strlcat(fmt, ".0f", sizeof fmt);
603 snprintf(buf2, sizeof buf2, fmt, (double)b);
607 if (iflags & ITEM_DELETED) {
611 n = c = buf2 + MAXPATHLEN - 32;
612 c[0] = iflags & ITEM_LOCAL_CHANGE
613 ? iflags & ITEM_XNAME_FOLLOWS ? 'h' : 'c'
614 : !(iflags & ITEM_TRANSFER) ? '.'
615 : !local_server && *op == 's' ? '<' : '>';
616 c[1] = S_ISDIR(file->mode) ? 'd'
617 : IS_SPECIAL(file->mode) ? 'S'
618 : IS_DEVICE(file->mode) ? 'D'
619 : S_ISLNK(file->mode) ? 'L' : 'f';
620 c[2] = !(iflags & ITEM_REPORT_CHECKSUM) ? '.' : 'c';
621 c[3] = !(iflags & ITEM_REPORT_SIZE) ? '.' : 's';
622 c[4] = !(iflags & ITEM_REPORT_TIME) ? '.'
623 : !preserve_times || S_ISLNK(file->mode) ? 'T' : 't';
624 c[5] = !(iflags & ITEM_REPORT_PERMS) ? '.' : 'p';
625 c[6] = !(iflags & ITEM_REPORT_OWNER) ? '.' : 'o';
626 c[7] = !(iflags & ITEM_REPORT_GROUP) ? '.' : 'g';
627 c[8] = !(iflags & ITEM_REPORT_ATIME) ? '.' : 'u';
628 c[9] = !(iflags & ITEM_REPORT_ACL) ? '.' : 'a';
629 c[10] = !(iflags & ITEM_REPORT_XATTR) ? '.' : 'x';
632 if (iflags & (ITEM_IS_NEW|ITEM_MISSING_DATA)) {
633 char ch = iflags & ITEM_IS_NEW ? '+' : '?';
635 for (i = 2; c[i]; i++)
637 } else if (c[0] == '.' || c[0] == 'h' || c[0] == 'c') {
639 for (i = 2; c[i]; i++) {
644 for (i = 2; c[i]; i++)
651 /* "n" is the string to be inserted in place of this % code. */
654 if (n != buf2 && fmt[1]) {
655 strlcat(fmt, "s", sizeof fmt);
656 snprintf(buf2, sizeof buf2, fmt, n);
661 /* Subtract the length of the escape from the string's size. */
664 if (len + total >= (size_t)sizeof buf) {
666 "buffer overflow expanding %%%c -- exiting\n",
668 exit_cleanup(RERR_MESSAGEIO);
671 /* Shuffle the rest of the string along to make space for n */
672 if (len != (size_t)(p - s + 1))
673 memmove(s + len, p + 1, total - (s - buf) + 1);
676 /* Insert the contents of string "n", but NOT its null. */
680 /* Skip over inserted string; continue looking */
684 rwrite(code, buf, total);
687 /* Return 1 if the format escape is in the log-format string (e.g. look for
688 * the 'b' in the "%9b" format escape). */
689 int log_format_has(const char *format, char esc)
696 for (p = format; (p = strchr(p, '%')) != NULL; ) {
709 /* Log the transfer of a file. If the code is FCLIENT, the output just goes
710 * to stdout. If it is FLOG, it just goes to the log file. Otherwise we
712 void log_item(enum logcode code, struct file_struct *file,
713 struct stats *initial_stats, int iflags, const char *hlink)
715 const char *s_or_r = am_sender ? "send" : "recv";
717 if (code != FLOG && stdout_format && !am_server) {
718 log_formatted(FCLIENT, stdout_format, s_or_r,
719 file, NULL, initial_stats, iflags, hlink);
721 if (code != FCLIENT && logfile_format && *logfile_format) {
722 log_formatted(FLOG, logfile_format, s_or_r,
723 file, NULL, initial_stats, iflags, hlink);
727 void maybe_log_item(struct file_struct *file, int iflags, int itemizing,
730 int significant_flags = iflags & SIGNIFICANT_ITEM_FLAGS;
731 int see_item = itemizing && (significant_flags || *buf
732 || stdout_format_has_i > 1 || (verbose > 1 && stdout_format_has_i));
733 int local_change = iflags & ITEM_LOCAL_CHANGE && significant_flags;
735 if (logfile_name && !dry_run && see_item
736 && (significant_flags || logfile_format_has_i))
737 log_item(FLOG, file, &stats, iflags, buf);
738 } else if (see_item || local_change || *buf
739 || (S_ISDIR(file->mode) && significant_flags)) {
740 enum logcode code = significant_flags || logfile_format_has_i ? FINFO : FCLIENT;
741 log_item(code, file, &stats, iflags, buf);
745 void log_delete(const char *fname, int mode)
748 union file_extras ex[4]; /* just in case... */
749 struct file_struct file;
751 int len = strlen(fname);
756 if (!verbose && !stdout_format)
758 else if (am_server && protocol_version >= 29 && len < MAXPATHLEN) {
760 len++; /* directories include trailing null */
761 send_msg(MSG_DELETED, fname, len);
763 fmt = stdout_format_has_o_or_i ? stdout_format : "deleting %n";
764 log_formatted(FCLIENT, fmt, "del.", &x.file, fname, &stats,
768 if (!logfile_name || dry_run || !logfile_format)
771 fmt = logfile_format_has_o_or_i ? logfile_format : "deleting %n";
772 log_formatted(FLOG, fmt, "del.", &x.file, fname, &stats, ITEM_DELETED, NULL);
776 * Called when the transfer is interrupted for some reason.
778 * Code is one of the RERR_* codes from errcode.h, or terminating
781 void log_exit(int code, const char *file, int line)
784 rprintf(FLOG,"sent %.0f bytes received %.0f bytes total size %.0f\n",
785 (double)stats.total_written,
786 (double)stats.total_read,
787 (double)stats.total_size);
791 name = rerr_name(code);
793 name = "unexplained error";
795 /* VANISHED is not an error, only a warning */
796 if (code == RERR_VANISHED) {
797 rprintf(FINFO, "rsync warning: %s (code %d) at %s(%d) [%s=%s]\n",
798 name, code, file, line, who_am_i(), RSYNC_VERSION);
800 rprintf(FERROR, "rsync error: %s (code %d) at %s(%d) [%s=%s]\n",
801 name, code, file, line, who_am_i(), RSYNC_VERSION);