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.
30 extern int am_generator;
31 extern int local_server;
34 extern int msg_fd_out;
35 extern int allow_8bit_chars;
36 extern int protocol_version;
37 extern int preserve_times;
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 mode_t orig_umask;
46 extern char *auth_user;
47 extern char *stdout_format;
48 extern char *logfile_format;
49 extern char *logfile_name;
51 extern iconv_t ic_chck;
54 extern iconv_t ic_send, ic_recv;
56 extern char curr_dir[];
57 extern char *module_dir;
58 extern unsigned int module_dirlen;
60 static int log_initialised;
61 static int logfile_was_closed;
62 static FILE *logfile_fp;
65 int got_xfer_error = 0;
70 } const rerr_names[] = {
71 { RERR_SYNTAX , "syntax or usage error" },
72 { RERR_PROTOCOL , "protocol incompatibility" },
73 { RERR_FILESELECT , "errors selecting input/output files, dirs" },
74 { RERR_UNSUPPORTED, "requested action not supported" },
75 { RERR_STARTCLIENT, "error starting client-server protocol" },
76 { RERR_SOCKETIO , "error in socket IO" },
77 { RERR_FILEIO , "error in file IO" },
78 { RERR_STREAMIO , "error in rsync protocol data stream" },
79 { RERR_MESSAGEIO , "errors with program diagnostics" },
80 { RERR_IPC , "error in IPC code" },
81 { RERR_CRASHED , "sibling process crashed" },
82 { RERR_TERMINATED , "sibling process terminated abnormally" },
83 { RERR_SIGNAL1 , "received SIGUSR1" },
84 { RERR_SIGNAL , "received SIGINT, SIGTERM, or SIGHUP" },
85 { RERR_WAITCHILD , "waitpid() failed" },
86 { RERR_MALLOC , "error allocating core memory buffers" },
87 { RERR_PARTIAL , "some files could not be transferred" },
88 { RERR_VANISHED , "some files vanished before they could be transferred" },
89 { RERR_TIMEOUT , "timeout in data send/receive" },
90 { RERR_CONTIMEOUT , "timeout waiting for daemon connection" },
91 { RERR_CMD_FAILED , "remote shell failed" },
92 { RERR_CMD_KILLED , "remote shell killed" },
93 { RERR_CMD_RUN , "remote command could not be run" },
94 { RERR_CMD_NOTFOUND,"remote command not found" },
95 { RERR_DEL_LIMIT , "the --max-delete limit stopped deletions" },
100 * Map from rsync error code to name, or return NULL.
102 static char const *rerr_name(int code)
105 for (i = 0; rerr_names[i].name; i++) {
106 if (rerr_names[i].code == code)
107 return rerr_names[i].name;
112 static void logit(int priority, const char *buf)
114 if (logfile_was_closed)
117 fprintf(logfile_fp, "%s [%d] %s",
118 timestring(time(NULL)), (int)getpid(), buf);
121 syslog(priority, "%s", buf);
125 static void syslog_init()
127 static int been_here = 0;
128 int options = LOG_PID;
135 options |= LOG_NDELAY;
139 openlog("rsyncd", options, lp_syslog_facility(module_id));
141 openlog("rsyncd", options);
145 logit(LOG_INFO, "rsyncd started\n");
149 static void logfile_open(void)
151 mode_t old_umask = umask(022 | orig_umask);
152 logfile_fp = fopen(logfile_name, "a");
155 int fopen_errno = errno;
156 /* Rsync falls back to using syslog on failure. */
158 rsyserr(FERROR, fopen_errno,
159 "failed to open log-file %s", logfile_name);
160 rprintf(FINFO, "Ignoring \"log file\" setting.\n");
164 void log_init(int restart)
166 if (log_initialised) {
169 if (strcmp(logfile_name, lp_log_file(module_id)) != 0) {
176 } else if (*logfile_name)
177 return; /* unchanged, non-empty "log file" names */
178 else if (lp_syslog_facility(-1) != lp_syslog_facility(module_id))
181 return; /* unchanged syslog settings */
185 /* This looks pointless, but it is needed in order for the
186 * C library on some systems to fetch the timezone info
187 * before the chroot. */
188 timestring(time(NULL));
190 /* Optionally use a log file instead of syslog. (Non-daemon
191 * rsyncs will have already set logfile_name, as needed.) */
192 if (am_daemon && !logfile_name)
193 logfile_name = lp_log_file(module_id);
194 if (logfile_name && *logfile_name)
200 void logfile_close(void)
203 logfile_was_closed = 1;
209 void logfile_reopen(void)
211 if (logfile_was_closed) {
212 logfile_was_closed = 0;
217 static void filtered_fwrite(FILE *f, const char *buf, int len, int use_isprint)
219 const char *s, *end = buf + len;
220 for (s = buf; s < end; s++) {
222 && *s == '\\' && s[1] == '#'
227 && ((use_isprint && !isPrint(s))
228 || *(uchar*)s < ' '))) {
229 if (s != buf && fwrite(buf, s - buf, 1, f) != 1)
230 exit_cleanup(RERR_MESSAGEIO);
231 fprintf(f, "\\#%03o", *(uchar*)s);
235 if (buf != end && fwrite(buf, end - buf, 1, f) != 1)
236 exit_cleanup(RERR_MESSAGEIO);
239 /* this is the underlying (unformatted) rsync debugging function. Call
240 * it with FINFO, FERROR_*, FWARNING, FLOG, or FCLIENT. Note: recursion
241 * can happen with certain fatal conditions. */
242 void rwrite(enum logcode code, const char *buf, int len, int is_utf8)
244 int trailing_CR_or_NL;
247 iconv_t ic = is_utf8 && ic_recv != (iconv_t)-1 ? ic_recv : ic_chck;
250 iconv_t ic = ic_chck;
255 exit_cleanup(RERR_MESSAGEIO);
257 if (am_server && msg_fd_out >= 0) {
259 /* Pass the message to our sibling. */
260 send_msg((enum msgcode)code, buf, len, 0);
264 if (code == FERROR_SOCKET) /* This gets simplified for a non-sibling. */
269 else if (am_daemon || logfile_name) {
272 int priority = code == FINFO || code == FLOG ? LOG_INFO : LOG_WARNING;
277 if (!log_initialised)
279 strlcpy(msg, buf, MIN((int)sizeof msg, len + 1));
280 logit(priority, msg);
283 if (code == FLOG || (am_daemon && !am_server))
285 } else if (code == FLOG)
288 if (quiet && code == FINFO)
292 enum msgcode msg = (enum msgcode)code;
293 if (protocol_version < 30) {
294 if (msg == MSG_ERROR)
295 msg = MSG_ERROR_XFER;
296 else if (msg == MSG_WARNING)
299 /* Pass the message to the non-server side. */
300 if (send_msg(msg, buf, len, !is_utf8))
303 /* TODO: can we send the error to the user somehow? */
317 f = am_server ? stderr : stdout;
320 exit_cleanup(RERR_MESSAGEIO);
323 trailing_CR_or_NL = len && (buf[len-1] == '\n' || buf[len-1] == '\r')
327 if (ic != (iconv_t)-1) {
332 INIT_CONST_XBUF(outbuf, convbuf);
333 INIT_XBUF(inbuf, (char*)buf, len, -1);
336 iconvbufs(ic, &inbuf, &outbuf, 0);
339 filtered_fwrite(f, convbuf, outbuf.len, 0);
342 if (!ierrno || ierrno == E2BIG)
344 fprintf(f, "\\#%03o", CVAL(inbuf.buf, inbuf.pos++));
349 filtered_fwrite(f, buf, len, !allow_8bit_chars);
351 if (trailing_CR_or_NL) {
352 fputc(trailing_CR_or_NL, f);
357 /* This is the rsync debugging function. Call it with FINFO, FERROR_*,
358 * FWARNING, FLOG, or FCLIENT. */
359 void rprintf(enum logcode code, const char *format, ...)
362 char buf[BIGPATHBUFLEN];
365 va_start(ap, format);
366 len = vsnprintf(buf, sizeof buf, format, ap);
369 /* Deal with buffer overruns. Instead of panicking, just
370 * truncate the resulting string. (Note that configure ensures
371 * that we have a vsnprintf() that doesn't ever return -1.) */
372 if (len > sizeof buf - 1) {
373 static const char ellipsis[] = "[...]";
375 /* Reset length, and zero-terminate the end of our buffer */
376 len = sizeof buf - 1;
379 /* Copy the ellipsis to the end of the string, but give
380 * us one extra character:
382 * v--- null byte at buf[sizeof buf - 1]
384 * -> abcd[...]00 <-- now two null bytes at end
386 * If the input format string has a trailing newline,
387 * we copy it into that extra null; if it doesn't, well,
388 * all we lose is one byte. */
389 memcpy(buf+len-sizeof ellipsis, ellipsis, sizeof ellipsis);
390 if (format[strlen(format)-1] == '\n') {
395 rwrite(code, buf, len, 0);
398 /* This is like rprintf, but it also tries to print some
399 * representation of the error code. Normally errcode = errno.
401 * Unlike rprintf, this always adds a newline and there should not be
402 * one in the format string.
404 * Note that since strerror might involve dynamically loading a
405 * message catalog we need to call it once before chroot-ing. */
406 void rsyserr(enum logcode code, int errcode, const char *format, ...)
409 char buf[BIGPATHBUFLEN];
412 strlcpy(buf, RSYNC_NAME ": ", sizeof buf);
413 len = (sizeof RSYNC_NAME ": ") - 1;
415 va_start(ap, format);
416 len += vsnprintf(buf + len, sizeof buf - len, format, ap);
419 if (len < sizeof buf) {
420 len += snprintf(buf + len, sizeof buf - len,
421 ": %s (%d)\n", strerror(errcode), errcode);
423 if (len >= sizeof buf)
424 exit_cleanup(RERR_MESSAGEIO);
426 rwrite(code, buf, len, 0);
429 void rflush(enum logcode code)
433 if (am_daemon || code == FLOG)
436 if (code == FINFO && !am_server)
444 /* A generic logging routine for send/recv, with parameter substitiution. */
445 static void log_formatted(enum logcode code, const char *format, const char *op,
446 struct file_struct *file, const char *fname,
447 struct stats *initial_stats, int iflags,
450 char buf[MAXPATHLEN+1024], buf2[MAXPATHLEN], fmt[32];
458 /* We expand % codes one by one in place in buf. We don't
459 * copy in the terminating null of the inserted strings, but
460 * rather keep going until we reach the null of the format. */
461 total = strlcpy(buf, format, sizeof buf);
462 if (total > MAXPATHLEN) {
463 rprintf(FERROR, "log-format string is WAY too long!\n");
464 exit_cleanup(RERR_MESSAGEIO);
469 for (p = buf; (p = strchr(p, '%')) != NULL; ) {
474 while (isDigit(p) && c - fmt < (int)(sizeof fmt) - 8)
491 strlcat(fmt, ".0f", sizeof fmt);
492 snprintf(buf2, sizeof buf2, fmt,
493 (double)F_LENGTH(file));
497 strlcat(fmt, "u", sizeof fmt);
498 snprintf(buf2, sizeof buf2, fmt,
499 uid_ndx ? F_OWNER(file) : 0);
503 if (!gid_ndx || file->flags & FLAG_SKIP_GROUP)
506 strlcat(fmt, "u", sizeof fmt);
507 snprintf(buf2, sizeof buf2, fmt,
513 strlcat(fmt, "ld", sizeof fmt);
514 snprintf(buf2, sizeof buf2, fmt,
519 n = c = timestring(file->modtime);
520 while ((c = strchr(p, ' ')) != NULL)
524 c = buf2 + MAXPATHLEN - PERMSTRING_SIZE - 1;
525 permstring(c, file->mode);
526 n = c + 1; /* skip the type char */
534 strlcpy(c, fname, MAXPATHLEN);
536 c = f_name(file, NULL);
537 if (am_sender && F_PATHNAME(file)) {
538 pathjoin(buf2, sizeof buf2,
539 F_PATHNAME(file), c);
540 clean_fname(buf2, 0);
542 strlcpy(c, buf2, MAXPATHLEN);
546 } else if (am_daemon && *c != '/') {
547 pathjoin(buf2, sizeof buf2,
548 curr_dir + module_dirlen, c);
549 clean_fname(buf2, 0);
551 strlcpy(c, buf2, MAXPATHLEN);
565 strlcpy(c, fname, MAXPATHLEN);
567 c = f_name(file, NULL);
568 if (S_ISDIR(file->mode))
569 strlcat(c, "/", MAXPATHLEN);
573 if (hlink && *hlink) {
575 strlcpy(buf2, " => ", sizeof buf2);
576 } else if (S_ISLNK(file->mode) && !fname) {
578 strlcpy(buf2, " -> ", sizeof buf2);
583 strlcpy(buf2, " ", sizeof buf2);
585 strlcat(fmt, "s", sizeof fmt);
586 snprintf(buf2 + 4, sizeof buf2 - 4, fmt, n);
590 n = lp_name(module_id);
593 n = timestring(time(NULL));
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);
615 b = stats.total_written -
616 initial_stats->total_written;
618 b = stats.total_read -
619 initial_stats->total_read;
621 strlcat(fmt, ".0f", sizeof fmt);
622 snprintf(buf2, sizeof buf2, fmt, (double)b);
626 if (iflags & ITEM_DELETED) {
630 n = c = buf2 + MAXPATHLEN - 32;
631 c[0] = iflags & ITEM_LOCAL_CHANGE
632 ? iflags & ITEM_XNAME_FOLLOWS ? 'h' : 'c'
633 : !(iflags & ITEM_TRANSFER) ? '.'
634 : !local_server && *op == 's' ? '<' : '>';
635 c[1] = S_ISDIR(file->mode) ? 'd'
636 : IS_SPECIAL(file->mode) ? 'S'
637 : IS_DEVICE(file->mode) ? 'D'
638 : S_ISLNK(file->mode) ? 'L' : 'f';
639 c[2] = !(iflags & ITEM_REPORT_CHECKSUM) ? '.' : 'c';
640 c[3] = !(iflags & ITEM_REPORT_SIZE) ? '.' : 's';
641 c[4] = !(iflags & ITEM_REPORT_TIME) ? '.'
642 : !preserve_times || (!receiver_symlink_times && S_ISLNK(file->mode))
644 c[5] = !(iflags & ITEM_REPORT_PERMS) ? '.' : 'p';
645 c[6] = !(iflags & ITEM_REPORT_OWNER) ? '.' : 'o';
646 c[7] = !(iflags & ITEM_REPORT_GROUP) ? '.' : 'g';
647 c[8] = !(iflags & ITEM_REPORT_ATIME) ? '.' : 'u';
648 c[9] = !(iflags & ITEM_REPORT_ACL) ? '.' : 'a';
649 c[10] = !(iflags & ITEM_REPORT_XATTR) ? '.' : 'x';
652 if (iflags & (ITEM_IS_NEW|ITEM_MISSING_DATA)) {
653 char ch = iflags & ITEM_IS_NEW ? '+' : '?';
655 for (i = 2; c[i]; i++)
657 } else if (c[0] == '.' || c[0] == 'h' || c[0] == 'c') {
659 for (i = 2; c[i]; i++) {
664 for (i = 2; c[i]; i++)
671 /* "n" is the string to be inserted in place of this % code. */
674 if (n != buf2 && fmt[1]) {
675 strlcat(fmt, "s", sizeof fmt);
676 snprintf(buf2, sizeof buf2, fmt, n);
681 /* Subtract the length of the escape from the string's size. */
684 if (len + total >= (size_t)sizeof buf) {
686 "buffer overflow expanding %%%c -- exiting\n",
688 exit_cleanup(RERR_MESSAGEIO);
691 /* Shuffle the rest of the string along to make space for n */
692 if (len != (size_t)(p - s + 1))
693 memmove(s + len, p + 1, total - (s - buf) + 1);
696 /* Insert the contents of string "n", but NOT its null. */
700 /* Skip over inserted string; continue looking */
704 rwrite(code, buf, total, 0);
707 /* Return 1 if the format escape is in the log-format string (e.g. look for
708 * the 'b' in the "%9b" format escape). */
709 int log_format_has(const char *format, char esc)
716 for (p = format; (p = strchr(p, '%')) != NULL; ) {
729 /* Log the transfer of a file. If the code is FCLIENT, the output just goes
730 * to stdout. If it is FLOG, it just goes to the log file. Otherwise we
732 void log_item(enum logcode code, struct file_struct *file,
733 struct stats *initial_stats, int iflags, const char *hlink)
735 const char *s_or_r = am_sender ? "send" : "recv";
737 if (code != FLOG && stdout_format && !am_server) {
738 log_formatted(FCLIENT, stdout_format, s_or_r,
739 file, NULL, initial_stats, iflags, hlink);
741 if (code != FCLIENT && logfile_format && *logfile_format) {
742 log_formatted(FLOG, logfile_format, s_or_r,
743 file, NULL, initial_stats, iflags, hlink);
747 void maybe_log_item(struct file_struct *file, int iflags, int itemizing,
750 int significant_flags = iflags & SIGNIFICANT_ITEM_FLAGS;
751 int see_item = itemizing && (significant_flags || *buf
752 || stdout_format_has_i > 1 || (verbose > 1 && stdout_format_has_i));
753 int local_change = iflags & ITEM_LOCAL_CHANGE && significant_flags;
755 if (logfile_name && !dry_run && see_item
756 && (significant_flags || logfile_format_has_i))
757 log_item(FLOG, file, &stats, iflags, buf);
758 } else if (see_item || local_change || *buf
759 || (S_ISDIR(file->mode) && significant_flags)) {
760 enum logcode code = significant_flags || logfile_format_has_i ? FINFO : FCLIENT;
761 log_item(code, file, &stats, iflags, buf);
765 void log_delete(const char *fname, int mode)
768 union file_extras ex[4]; /* just in case... */
769 struct file_struct file;
771 int len = strlen(fname);
776 if (!verbose && !stdout_format)
778 else if (am_server && protocol_version >= 29 && len < MAXPATHLEN) {
780 len++; /* directories include trailing null */
781 send_msg(MSG_DELETED, fname, len, am_generator);
783 fmt = stdout_format_has_o_or_i ? stdout_format : "deleting %n";
784 log_formatted(FCLIENT, fmt, "del.", &x.file, fname, &stats,
788 if (!logfile_name || dry_run || !logfile_format)
791 fmt = logfile_format_has_o_or_i ? logfile_format : "deleting %n";
792 log_formatted(FLOG, fmt, "del.", &x.file, fname, &stats, ITEM_DELETED, NULL);
796 * Called when the transfer is interrupted for some reason.
798 * Code is one of the RERR_* codes from errcode.h, or terminating
801 void log_exit(int code, const char *file, int line)
804 rprintf(FLOG,"sent %.0f bytes received %.0f bytes total size %.0f\n",
805 (double)stats.total_written,
806 (double)stats.total_read,
807 (double)stats.total_size);
808 } else if (am_server != 2) {
811 name = rerr_name(code);
813 name = "unexplained error";
815 /* VANISHED is not an error, only a warning */
816 if (code == RERR_VANISHED) {
817 rprintf(FWARNING, "rsync warning: %s (code %d) at %s(%d) [%s=%s]\n",
818 name, code, file, line, who_am_i(), RSYNC_VERSION);
820 rprintf(FERROR, "rsync error: %s (code %d) at %s(%d) [%s=%s]\n",
821 name, code, file, line, who_am_i(), RSYNC_VERSION);