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, 2004, 2005, 2006 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 2 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, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
24 #if defined HAVE_ICONV_OPEN && defined HAVE_ICONV_H
33 extern int local_server;
36 extern int msg_fd_out;
37 extern int allow_8bit_chars;
38 extern int protocol_version;
39 extern int preserve_times;
40 extern int preserve_uid;
41 extern int preserve_gid;
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 mode_t orig_umask;
47 extern char *auth_user;
48 extern char *stdout_format;
49 extern char *logfile_format;
50 extern char *logfile_name;
51 #if defined HAVE_ICONV_OPEN && defined HAVE_ICONV_H
52 extern iconv_t ic_chck;
54 extern char curr_dir[];
55 extern unsigned int module_dirlen;
57 static int log_initialised;
58 static int logfile_was_closed;
59 static FILE *logfile_fp;
62 int log_got_error = 0;
67 } const rerr_names[] = {
68 { RERR_SYNTAX , "syntax or usage error" },
69 { RERR_PROTOCOL , "protocol incompatibility" },
70 { RERR_FILESELECT , "errors selecting input/output files, dirs" },
71 { RERR_UNSUPPORTED, "requested action not supported" },
72 { RERR_STARTCLIENT, "error starting client-server protocol" },
73 { RERR_SOCKETIO , "error in socket IO" },
74 { RERR_FILEIO , "error in file IO" },
75 { RERR_STREAMIO , "error in rsync protocol data stream" },
76 { RERR_MESSAGEIO , "errors with program diagnostics" },
77 { RERR_IPC , "error in IPC code" },
78 { RERR_CRASHED , "sibling process crashed" },
79 { RERR_TERMINATED , "sibling process terminated abnormally" },
80 { RERR_SIGNAL1 , "received SIGUSR1" },
81 { RERR_SIGNAL , "received SIGINT, SIGTERM, or SIGHUP" },
82 { RERR_WAITCHILD , "waitpid() failed" },
83 { RERR_MALLOC , "error allocating core memory buffers" },
84 { RERR_PARTIAL , "some files could not be transferred" },
85 { RERR_VANISHED , "some files vanished before they could be transferred" },
86 { RERR_TIMEOUT , "timeout in data send/receive" },
87 { RERR_CMD_FAILED , "remote shell failed" },
88 { RERR_CMD_KILLED , "remote shell killed" },
89 { RERR_CMD_RUN , "remote command could not be run" },
90 { RERR_CMD_NOTFOUND,"remote command not found" },
91 { RERR_DEL_LIMIT , "the --max-delete limit stopped deletions" },
96 * Map from rsync error code to name, or return NULL.
98 static char const *rerr_name(int code)
101 for (i = 0; rerr_names[i].name; i++) {
102 if (rerr_names[i].code == code)
103 return rerr_names[i].name;
108 static void logit(int priority, const char *buf)
110 if (logfile_was_closed)
113 fprintf(logfile_fp, "%s [%d] %s",
114 timestring(time(NULL)), (int)getpid(), buf);
117 syslog(priority, "%s", buf);
121 static void syslog_init()
123 static int been_here = 0;
124 int options = LOG_PID;
131 options |= LOG_NDELAY;
135 openlog("rsyncd", options, lp_syslog_facility(module_id));
137 openlog("rsyncd", options);
141 logit(LOG_INFO, "rsyncd started\n");
145 static void logfile_open(void)
147 mode_t old_umask = umask(022 | orig_umask);
148 logfile_fp = fopen(logfile_name, "a");
151 int fopen_errno = errno;
152 /* Rsync falls back to using syslog on failure. */
154 rsyserr(FERROR, fopen_errno,
155 "failed to open log-file %s", logfile_name);
156 rprintf(FINFO, "Ignoring \"log file\" setting.\n");
160 void log_init(int restart)
162 if (log_initialised) {
165 if (strcmp(logfile_name, lp_log_file(module_id)) != 0) {
172 } else if (*logfile_name)
173 return; /* unchanged, non-empty "log file" names */
174 else if (lp_syslog_facility(-1) != lp_syslog_facility(module_id))
177 return; /* unchanged syslog settings */
181 /* This looks pointless, but it is needed in order for the
182 * C library on some systems to fetch the timezone info
183 * before the chroot. */
184 timestring(time(NULL));
186 /* Optionally use a log file instead of syslog. (Non-daemon
187 * rsyncs will have already set logfile_name, as needed.) */
188 if (am_daemon && !logfile_name)
189 logfile_name = lp_log_file(module_id);
190 if (logfile_name && *logfile_name)
196 void logfile_close(void)
199 logfile_was_closed = 1;
205 void logfile_reopen(void)
207 if (logfile_was_closed) {
208 logfile_was_closed = 0;
213 static void filtered_fwrite(FILE *f, const char *buf, int len, int use_isprint)
215 const char *s, *end = buf + len;
216 for (s = buf; s < end; s++) {
218 && *s == '\\' && s[1] == '#'
223 && ((use_isprint && !isPrint(s))
224 || *(uchar*)s < ' '))) {
225 if (s != buf && fwrite(buf, s - buf, 1, f) != 1)
226 exit_cleanup(RERR_MESSAGEIO);
227 fprintf(f, "\\#%03o", *(uchar*)s);
231 if (buf != end && fwrite(buf, end - buf, 1, f) != 1)
232 exit_cleanup(RERR_MESSAGEIO);
235 /* this is the underlying (unformatted) rsync debugging function. Call
236 * it with FINFO, FERROR or FLOG. Note: recursion can happen with
237 * certain fatal conditions. */
238 void rwrite(enum logcode code, const char *buf, int len)
240 int trailing_CR_or_NL;
244 exit_cleanup(RERR_MESSAGEIO);
246 if (am_server && msg_fd_out >= 0) {
247 /* Pass the message to our sibling. */
248 send_msg((enum msgcode)code, buf, len);
252 if (code == FSOCKERR) /* This gets simplified for a non-sibling. */
257 else if (am_daemon || logfile_name) {
260 int priority = code == FERROR ? LOG_WARNING : LOG_INFO;
265 if (!log_initialised)
267 strlcpy(msg, buf, MIN((int)sizeof msg, len + 1));
268 logit(priority, msg);
271 if (code == FLOG || (am_daemon && !am_server))
273 } else if (code == FLOG)
276 if (quiet && code != FERROR)
280 /* Pass the message to the non-server side. */
281 if (send_msg((enum msgcode)code, buf, len))
284 /* TODO: can we send the error to the user somehow? */
295 f = am_server ? stderr : stdout;
298 exit_cleanup(RERR_MESSAGEIO);
301 trailing_CR_or_NL = len && (buf[len-1] == '\n' || buf[len-1] == '\r')
304 #if defined HAVE_ICONV_OPEN && defined HAVE_ICONV_H
308 if (ic_chck != (iconv_t)-1) {
310 ICONV_CONST char *in_buf = (ICONV_CONST char *)buf;
311 char *out_buf = convbuf;
312 size_t in_cnt = len, out_cnt = sizeof convbuf - 1;
314 iconv(ic_chck, NULL, 0, NULL, 0);
315 while (iconv(ic_chck, &in_buf,&in_cnt,
316 &out_buf,&out_cnt) == (size_t)-1) {
317 if (out_buf != convbuf) {
318 filtered_fwrite(f, convbuf, out_buf - convbuf, 0);
320 out_cnt = sizeof convbuf - 1;
324 fprintf(f, "\\#%03o", *(uchar*)in_buf++);
327 if (out_buf != convbuf)
328 filtered_fwrite(f, convbuf, out_buf - convbuf, 0);
331 filtered_fwrite(f, buf, len, !allow_8bit_chars);
333 if (trailing_CR_or_NL) {
334 fputc(trailing_CR_or_NL, f);
339 /* This is the rsync debugging function. Call it with FINFO, FERROR or
341 void rprintf(enum logcode code, const char *format, ...)
344 char buf[BIGPATHBUFLEN];
347 va_start(ap, format);
348 len = vsnprintf(buf, sizeof buf, format, ap);
351 /* Deal with buffer overruns. Instead of panicking, just
352 * truncate the resulting string. (Note that configure ensures
353 * that we have a vsnprintf() that doesn't ever return -1.) */
354 if (len > sizeof buf - 1) {
355 static const char ellipsis[] = "[...]";
357 /* Reset length, and zero-terminate the end of our buffer */
358 len = sizeof buf - 1;
361 /* Copy the ellipsis to the end of the string, but give
362 * us one extra character:
364 * v--- null byte at buf[sizeof buf - 1]
366 * -> abcd[...]00 <-- now two null bytes at end
368 * If the input format string has a trailing newline,
369 * we copy it into that extra null; if it doesn't, well,
370 * all we lose is one byte. */
371 memcpy(buf+len-sizeof ellipsis, ellipsis, sizeof ellipsis);
372 if (format[strlen(format)-1] == '\n') {
377 rwrite(code, buf, len);
380 /* This is like rprintf, but it also tries to print some
381 * representation of the error code. Normally errcode = errno.
383 * Unlike rprintf, this always adds a newline and there should not be
384 * one in the format string.
386 * Note that since strerror might involve dynamically loading a
387 * message catalog we need to call it once before chroot-ing. */
388 void rsyserr(enum logcode code, int errcode, const char *format, ...)
391 char buf[BIGPATHBUFLEN];
394 strlcpy(buf, RSYNC_NAME ": ", sizeof buf);
395 len = (sizeof RSYNC_NAME ": ") - 1;
397 va_start(ap, format);
398 len += vsnprintf(buf + len, sizeof buf - len, format, ap);
401 if (len < sizeof buf) {
402 len += snprintf(buf + len, sizeof buf - len,
403 ": %s (%d)\n", strerror(errcode), errcode);
405 if (len >= sizeof buf)
406 exit_cleanup(RERR_MESSAGEIO);
408 rwrite(code, buf, len);
411 void rflush(enum logcode code)
415 if (am_daemon || code == FLOG)
418 if (code == FERROR || am_server)
426 /* A generic logging routine for send/recv, with parameter substitiution. */
427 static void log_formatted(enum logcode code, const char *format, const char *op,
428 struct file_struct *file, const char *fname,
429 struct stats *initial_stats, int iflags,
432 char buf[MAXPATHLEN+1024], buf2[MAXPATHLEN], fmt[32];
440 /* We expand % codes one by one in place in buf. We don't
441 * copy in the terminating null of the inserted strings, but
442 * rather keep going until we reach the null of the format. */
443 total = strlcpy(buf, format, sizeof buf);
444 if (total > MAXPATHLEN) {
445 rprintf(FERROR, "log-format string is WAY too long!\n");
446 exit_cleanup(RERR_MESSAGEIO);
451 for (p = buf; (p = strchr(p, '%')) != NULL; ) {
456 while (isDigit(p) && c - fmt < (int)(sizeof fmt) - 8)
473 strlcat(fmt, ".0f", sizeof fmt);
474 snprintf(buf2, sizeof buf2, fmt,
475 (double)F_LENGTH(file));
479 strlcat(fmt, "ld", sizeof fmt);
480 snprintf(buf2, sizeof buf2, fmt,
481 preserve_uid ? (long)F_UID(file) : 0);
485 if (!preserve_gid || F_GID(file) == GID_NONE)
488 strlcat(fmt, "ld", sizeof fmt);
489 snprintf(buf2, sizeof buf2, fmt,
495 strlcat(fmt, "ld", sizeof fmt);
496 snprintf(buf2, sizeof buf2, fmt,
501 n = c = timestring(file->modtime);
502 while ((c = strchr(p, ' ')) != NULL)
506 c = buf2 + MAXPATHLEN - PERMSTRING_SIZE - 1;
507 permstring(c, file->mode);
508 n = c + 1; /* skip the type char */
516 strlcpy(c, fname, MAXPATHLEN);
518 c = f_name(file, NULL);
519 if (am_sender && F_ROOTDIR(file)) {
520 pathjoin(buf2, sizeof buf2,
522 clean_fname(buf2, 0);
524 strlcpy(c, buf2, MAXPATHLEN);
528 } else if (am_daemon && *c != '/') {
529 pathjoin(buf2, sizeof buf2,
530 curr_dir + module_dirlen, c);
531 clean_fname(buf2, 0);
533 strlcpy(c, buf2, MAXPATHLEN);
547 strlcpy(c, fname, MAXPATHLEN);
549 c = f_name(file, NULL);
550 if (S_ISDIR(file->mode))
551 strlcat(c, "/", MAXPATHLEN);
555 if (hlink && *hlink) {
557 strlcpy(buf2, " => ", sizeof buf2);
558 } else if (S_ISLNK(file->mode) && !fname) {
560 strlcpy(buf2, " -> ", sizeof buf2);
565 strlcpy(buf2, " ", sizeof buf2);
567 strlcat(fmt, "s", sizeof fmt);
568 snprintf(buf2 + 4, sizeof buf2 - 4, fmt, n);
572 n = lp_name(module_id);
575 n = timestring(time(NULL));
578 n = lp_path(module_id);
585 b = stats.total_written -
586 initial_stats->total_written;
588 b = stats.total_read -
589 initial_stats->total_read;
591 strlcat(fmt, ".0f", sizeof fmt);
592 snprintf(buf2, sizeof buf2, fmt, (double)b);
597 b = stats.total_written -
598 initial_stats->total_written;
600 b = stats.total_read -
601 initial_stats->total_read;
603 strlcat(fmt, ".0f", sizeof fmt);
604 snprintf(buf2, sizeof buf2, fmt, (double)b);
608 if (iflags & ITEM_DELETED) {
612 n = c = buf2 + MAXPATHLEN - 32;
613 c[0] = iflags & ITEM_LOCAL_CHANGE
614 ? iflags & ITEM_XNAME_FOLLOWS ? 'h' : 'c'
615 : !(iflags & ITEM_TRANSFER) ? '.'
616 : !local_server && *op == 's' ? '<' : '>';
617 c[1] = S_ISDIR(file->mode) ? 'd'
618 : IS_SPECIAL(file->mode) ? 'S'
619 : IS_DEVICE(file->mode) ? 'D'
620 : S_ISLNK(file->mode) ? 'L' : 'f';
621 c[2] = !(iflags & ITEM_REPORT_CHECKSUM) ? '.' : 'c';
622 c[3] = !(iflags & ITEM_REPORT_SIZE) ? '.' : 's';
623 c[4] = !(iflags & ITEM_REPORT_TIME) ? '.'
624 : !preserve_times || S_ISLNK(file->mode) ? 'T' : 't';
625 c[5] = !(iflags & ITEM_REPORT_PERMS) ? '.' : 'p';
626 c[6] = !(iflags & ITEM_REPORT_OWNER) ? '.' : 'o';
627 c[7] = !(iflags & ITEM_REPORT_GROUP) ? '.' : 'g';
631 if (iflags & (ITEM_IS_NEW|ITEM_MISSING_DATA)) {
632 char ch = iflags & ITEM_IS_NEW ? '+' : '?';
634 for (i = 2; c[i]; i++)
636 } else if (c[0] == '.' || c[0] == 'h' || c[0] == 'c') {
638 for (i = 2; c[i]; i++) {
643 for (i = 2; c[i]; i++)
650 /* "n" is the string to be inserted in place of this % code. */
653 if (n != buf2 && fmt[1]) {
654 strlcat(fmt, "s", sizeof fmt);
655 snprintf(buf2, sizeof buf2, fmt, n);
660 /* Subtract the length of the escape from the string's size. */
663 if (len + total >= (size_t)sizeof buf) {
665 "buffer overflow expanding %%%c -- exiting\n",
667 exit_cleanup(RERR_MESSAGEIO);
670 /* Shuffle the rest of the string along to make space for n */
671 if (len != (size_t)(p - s + 1))
672 memmove(s + len, p + 1, total - (s - buf) + 1);
675 /* Insert the contents of string "n", but NOT its null. */
679 /* Skip over inserted string; continue looking */
683 rwrite(code, buf, total);
686 /* Return 1 if the format escape is in the log-format string (e.g. look for
687 * the 'b' in the "%9b" format escape). */
688 int log_format_has(const char *format, char esc)
695 for (p = format; (p = strchr(p, '%')) != NULL; ) {
708 /* Log the transfer of a file. If the code is FCLIENT, the output just goes
709 * to stdout. If it is FLOG, it just goes to the log file. Otherwise we
711 void log_item(enum logcode code, struct file_struct *file,
712 struct stats *initial_stats, int iflags, const char *hlink)
714 const char *s_or_r = am_sender ? "send" : "recv";
716 if (code != FLOG && stdout_format && !am_server) {
717 log_formatted(FCLIENT, stdout_format, s_or_r,
718 file, NULL, initial_stats, iflags, hlink);
720 if (code != FCLIENT && logfile_format && *logfile_format) {
721 log_formatted(FLOG, logfile_format, s_or_r,
722 file, NULL, initial_stats, iflags, hlink);
726 void maybe_log_item(struct file_struct *file, int iflags, int itemizing,
729 int significant_flags = iflags & SIGNIFICANT_ITEM_FLAGS;
730 int see_item = itemizing && (significant_flags || *buf
731 || stdout_format_has_i > 1 || (verbose > 1 && stdout_format_has_i));
732 int local_change = iflags & ITEM_LOCAL_CHANGE && significant_flags;
734 if (logfile_name && !dry_run && see_item
735 && (significant_flags || logfile_format_has_i))
736 log_item(FLOG, file, &stats, iflags, buf);
737 } else if (see_item || local_change || *buf
738 || (S_ISDIR(file->mode) && significant_flags)) {
739 enum logcode code = significant_flags || logfile_format_has_i ? FINFO : FCLIENT;
740 log_item(code, file, &stats, iflags, buf);
744 void log_delete(const char *fname, int mode)
747 union file_extras ex[4]; /* just in case... */
748 struct file_struct file;
750 int len = strlen(fname);
755 if (!verbose && !stdout_format)
757 else if (am_server && protocol_version >= 29 && len < MAXPATHLEN) {
759 len++; /* directories include trailing null */
760 send_msg(MSG_DELETED, fname, len);
762 fmt = stdout_format_has_o_or_i ? stdout_format : "deleting %n";
763 log_formatted(FCLIENT, fmt, "del.", &x.file, fname, &stats,
767 if (!logfile_name || dry_run || !logfile_format)
770 fmt = logfile_format_has_o_or_i ? logfile_format : "deleting %n";
771 log_formatted(FLOG, fmt, "del.", &x.file, fname, &stats, ITEM_DELETED, NULL);
775 * Called when the transfer is interrupted for some reason.
777 * Code is one of the RERR_* codes from errcode.h, or terminating
780 void log_exit(int code, const char *file, int line)
783 rprintf(FLOG,"sent %.0f bytes received %.0f bytes total size %.0f\n",
784 (double)stats.total_written,
785 (double)stats.total_read,
786 (double)stats.total_size);
790 name = rerr_name(code);
792 name = "unexplained error";
794 /* VANISHED is not an error, only a warning */
795 if (code == RERR_VANISHED) {
796 rprintf(FINFO, "rsync warning: %s (code %d) at %s(%d) [%s=%s]\n",
797 name, code, file, line, who_am_i(), RSYNC_VERSION);
799 rprintf(FERROR, "rsync error: %s (code %d) at %s(%d) [%s=%s]\n",
800 name, code, file, line, who_am_i(), RSYNC_VERSION);