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 in_exit_cleanup;
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" },
94 #define EXIT_OR_RETURN(err) \
96 if (in_exit_cleanup) \
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, 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] == '#'
226 && isdigit(*(uchar*)(s+2))
227 && isdigit(*(uchar*)(s+3))
228 && isdigit(*(uchar*)(s+4)))
230 && ((use_isprint && !isprint(*(uchar*)s))
231 || *(uchar*)s < ' '))) {
232 if (s != buf && fwrite(buf, s - buf, 1, f) != 1)
233 EXIT_OR_RETURN(RERR_MESSAGEIO);
234 fprintf(f, "\\#%03o", *(uchar*)s);
238 if (buf != end && fwrite(buf, end - buf, 1, f) != 1)
239 EXIT_OR_RETURN(RERR_MESSAGEIO);
242 /* this is the underlying (unformatted) rsync debugging function. Call
243 * it with FINFO, FERROR or FLOG. Note: recursion can happen with
244 * certain fatal conditions. */
245 void rwrite(enum logcode code, char *buf, int len)
247 int trailing_CR_or_NL;
251 EXIT_OR_RETURN(RERR_MESSAGEIO);
253 if (am_server && msg_fd_out >= 0) {
254 /* Pass the message to our sibling. */
255 send_msg((enum msgcode)code, buf, len);
259 if (code == FSOCKERR) /* This gets simplified for a non-sibling. */
264 else if (am_daemon || logfile_name) {
267 int priority = code == FERROR ? LOG_WARNING : LOG_INFO;
272 if (!log_initialised)
274 strlcpy(msg, buf, MIN((int)sizeof msg, len + 1));
275 logit(priority, msg);
278 if (code == FLOG || (am_daemon && !am_server))
280 } else if (code == FLOG)
283 if (quiet && code != FERROR)
287 /* Pass the message to the non-server side. */
288 if (send_msg((enum msgcode)code, buf, len))
291 /* TODO: can we send the error to the user somehow? */
302 f = am_server ? stderr : stdout;
305 EXIT_OR_RETURN(RERR_MESSAGEIO);
308 trailing_CR_or_NL = len && (buf[len-1] == '\n' || buf[len-1] == '\r')
311 #if defined HAVE_ICONV_OPEN && defined HAVE_ICONV_H
312 if (ic_chck != (iconv_t)-1) {
314 char *in_buf = buf, *out_buf = convbuf;
315 size_t in_cnt = len, out_cnt = sizeof convbuf - 1;
317 iconv(ic_chck, NULL, 0, NULL, 0);
318 while (iconv(ic_chck, &in_buf,&in_cnt,
319 &out_buf,&out_cnt) == (size_t)-1) {
320 if (out_buf != convbuf) {
321 filtered_fwrite(f, convbuf, out_buf - convbuf, 0);
323 out_cnt = sizeof convbuf - 1;
327 fprintf(f, "\\#%03o", *(uchar*)in_buf++);
330 if (out_buf != convbuf)
331 filtered_fwrite(f, convbuf, out_buf - convbuf, 0);
334 filtered_fwrite(f, buf, len, !allow_8bit_chars);
336 if (trailing_CR_or_NL) {
337 fputc(trailing_CR_or_NL, f);
342 /* This is the rsync debugging function. Call it with FINFO, FERROR or
344 void rprintf(enum logcode code, const char *format, ...)
347 char buf[BIGPATHBUFLEN];
350 va_start(ap, format);
351 len = vsnprintf(buf, sizeof buf, format, ap);
354 /* Deal with buffer overruns. Instead of panicking, just
355 * truncate the resulting string. (Note that configure ensures
356 * that we have a vsnprintf() that doesn't ever return -1.) */
357 if (len > sizeof buf - 1) {
358 static const char ellipsis[] = "[...]";
360 /* Reset length, and zero-terminate the end of our buffer */
361 len = sizeof buf - 1;
364 /* Copy the ellipsis to the end of the string, but give
365 * us one extra character:
367 * v--- null byte at buf[sizeof buf - 1]
369 * -> abcd[...]00 <-- now two null bytes at end
371 * If the input format string has a trailing newline,
372 * we copy it into that extra null; if it doesn't, well,
373 * all we lose is one byte. */
374 memcpy(buf+len-sizeof ellipsis, ellipsis, sizeof ellipsis);
375 if (format[strlen(format)-1] == '\n') {
380 rwrite(code, buf, len);
383 /* This is like rprintf, but it also tries to print some
384 * representation of the error code. Normally errcode = errno.
386 * Unlike rprintf, this always adds a newline and there should not be
387 * one in the format string.
389 * Note that since strerror might involve dynamically loading a
390 * message catalog we need to call it once before chroot-ing. */
391 void rsyserr(enum logcode code, int errcode, const char *format, ...)
394 char buf[BIGPATHBUFLEN];
397 strlcpy(buf, RSYNC_NAME ": ", sizeof buf);
398 len = (sizeof RSYNC_NAME ": ") - 1;
400 va_start(ap, format);
401 len += vsnprintf(buf + len, sizeof buf - len, format, ap);
404 if (len < sizeof buf) {
405 len += snprintf(buf + len, sizeof buf - len,
406 ": %s (%d)\n", strerror(errcode), errcode);
408 if (len >= sizeof buf)
409 EXIT_OR_RETURN(RERR_MESSAGEIO);
411 rwrite(code, buf, len);
414 void rflush(enum logcode code)
418 if (am_daemon || code == FLOG)
421 if (code == FERROR || am_server)
429 /* A generic logging routine for send/recv, with parameter substitiution. */
430 static void log_formatted(enum logcode code, char *format, char *op,
431 struct file_struct *file, struct stats *initial_stats,
432 int iflags, char *hlink)
434 char buf[MAXPATHLEN+1024], buf2[MAXPATHLEN], fmt[32];
441 /* We expand % codes one by one in place in buf. We don't
442 * copy in the terminating null of the inserted strings, but
443 * rather keep going until we reach the null of the format. */
444 total = strlcpy(buf, format, sizeof buf);
445 if (total > MAXPATHLEN) {
446 rprintf(FERROR, "log-format string is WAY too long!\n");
447 EXIT_OR_RETURN(RERR_MESSAGEIO);
452 for (p = buf; (p = strchr(p, '%')) != NULL; ) {
457 while (isdigit(*(uchar*)p) && n - fmt < (int)(sizeof fmt) - 8)
474 strlcat(fmt, ".0f", sizeof fmt);
475 snprintf(buf2, sizeof buf2, fmt,
476 (double)file->length);
480 strlcat(fmt, "ld", sizeof fmt);
481 snprintf(buf2, sizeof buf2, fmt,
486 if (file->gid == GID_NONE)
489 strlcat(fmt, "ld", sizeof fmt);
490 snprintf(buf2, sizeof buf2, fmt,
496 strlcat(fmt, "ld", sizeof fmt);
497 snprintf(buf2, sizeof buf2, fmt,
502 n = timestring(file->modtime);
505 while ((cp = strchr(cp, ' ')) != NULL)
510 n = buf2 + MAXPATHLEN - PERMSTRING_SIZE;
511 permstring(n - 1, file->mode); /* skip the type char */
517 n = f_name(file, NULL);
518 if (am_sender && file->dir.root) {
519 pathjoin(buf2, sizeof buf2,
521 clean_fname(buf2, 0);
523 strlcpy(n, buf2, MAXPATHLEN);
526 } else if (*n != '/') {
527 pathjoin(buf2, sizeof buf2,
528 curr_dir + module_dirlen, n);
529 clean_fname(buf2, 0);
531 strlcpy(n, buf2, MAXPATHLEN);
540 n = f_name(file, NULL);
541 if (S_ISDIR(file->mode))
542 strlcat(n, "/", MAXPATHLEN);
545 if (hlink && *hlink) {
547 strlcpy(buf2, " => ", sizeof buf2);
548 } else if (S_ISLNK(file->mode) && file->u.link) {
550 strlcpy(buf2, " -> ", sizeof buf2);
555 strlcpy(buf2, " ", sizeof buf2);
557 strlcat(fmt, "s", sizeof fmt);
558 snprintf(buf2 + 4, sizeof buf2 - 4, fmt, n);
562 n = lp_name(module_id);
565 n = timestring(time(NULL));
568 n = lp_path(module_id);
575 b = stats.total_written -
576 initial_stats->total_written;
578 b = stats.total_read -
579 initial_stats->total_read;
581 strlcat(fmt, ".0f", sizeof fmt);
582 snprintf(buf2, sizeof buf2, fmt, (double)b);
587 b = stats.total_written -
588 initial_stats->total_written;
590 b = stats.total_read -
591 initial_stats->total_read;
593 strlcat(fmt, ".0f", sizeof fmt);
594 snprintf(buf2, sizeof buf2, fmt, (double)b);
598 if (iflags & ITEM_DELETED) {
602 n = buf2 + MAXPATHLEN - 32;
603 n[0] = iflags & ITEM_LOCAL_CHANGE
604 ? iflags & ITEM_XNAME_FOLLOWS ? 'h' : 'c'
605 : !(iflags & ITEM_TRANSFER) ? '.'
606 : !local_server && *op == 's' ? '<' : '>';
607 n[1] = S_ISDIR(file->mode) ? 'd'
608 : IS_SPECIAL(file->mode) ? 'S'
609 : IS_DEVICE(file->mode) ? 'D'
610 : S_ISLNK(file->mode) ? 'L' : 'f';
611 n[2] = !(iflags & ITEM_REPORT_CHECKSUM) ? '.' : 'c';
612 n[3] = !(iflags & ITEM_REPORT_SIZE) ? '.' : 's';
613 n[4] = !(iflags & ITEM_REPORT_TIME) ? '.'
614 : !preserve_times || S_ISLNK(file->mode) ? 'T' : 't';
615 n[5] = !(iflags & ITEM_REPORT_PERMS) ? '.' : 'p';
616 n[6] = !(iflags & ITEM_REPORT_OWNER) ? '.' : 'o';
617 n[7] = !(iflags & ITEM_REPORT_GROUP) ? '.' : 'g';
621 if (iflags & (ITEM_IS_NEW|ITEM_MISSING_DATA)) {
622 char ch = iflags & ITEM_IS_NEW ? '+' : '?';
624 for (i = 2; n[i]; i++)
626 } else if (n[0] == '.' || n[0] == 'h'
627 || (n[0] == 'c' && n[1] == 'f')) {
629 for (i = 2; n[i]; i++) {
634 for (i = 2; n[i]; i++)
641 /* "n" is the string to be inserted in place of this % code. */
644 if (n != buf2 && fmt[1]) {
645 strlcat(fmt, "s", sizeof fmt);
646 snprintf(buf2, sizeof buf2, fmt, n);
651 /* Subtract the length of the escape from the string's size. */
654 if (len + total >= (size_t)sizeof buf) {
656 "buffer overflow expanding %%%c -- exiting\n",
658 EXIT_OR_RETURN(RERR_MESSAGEIO);
661 /* Shuffle the rest of the string along to make space for n */
662 if (len != (size_t)(p - s + 1))
663 memmove(s + len, p + 1, total - (s - buf) + 1);
666 /* Insert the contents of string "n", but NOT its null. */
670 /* Skip over inserted string; continue looking */
674 rwrite(code, buf, total);
677 /* Return 1 if the format escape is in the log-format string (e.g. look for
678 * the 'b' in the "%9b" format escape). */
679 int log_format_has(const char *format, char esc)
686 for (p = format; (p = strchr(p, '%')) != NULL; ) {
689 while (isdigit(*(uchar*)p))
699 /* Log the transfer of a file. If the code is FCLIENT, the output just goes
700 * to stdout. If it is FLOG, it just goes to the log file. Otherwise we
702 void log_item(enum logcode code, struct file_struct *file,
703 struct stats *initial_stats, int iflags, char *hlink)
705 char *s_or_r = am_sender ? "send" : "recv";
707 if (code != FLOG && stdout_format && !am_server) {
708 log_formatted(FCLIENT, stdout_format, s_or_r,
709 file, initial_stats, iflags, hlink);
711 if (code != FCLIENT && logfile_format && *logfile_format) {
712 log_formatted(FLOG, logfile_format, s_or_r,
713 file, initial_stats, iflags, hlink);
717 void maybe_log_item(struct file_struct *file, int iflags, int itemizing,
720 int significant_flags = iflags & SIGNIFICANT_ITEM_FLAGS;
721 int see_item = itemizing && (significant_flags || *buf
722 || stdout_format_has_i > 1 || (verbose > 1 && stdout_format_has_i));
723 int local_change = iflags & ITEM_LOCAL_CHANGE && significant_flags;
725 if (logfile_name && !dry_run && see_item
726 && (significant_flags || logfile_format_has_i))
727 log_item(FLOG, file, &stats, iflags, buf);
728 } else if (see_item || local_change || *buf
729 || (S_ISDIR(file->mode) && significant_flags)) {
730 enum logcode code = significant_flags || logfile_format_has_i ? FINFO : FCLIENT;
731 log_item(code, file, &stats, iflags, buf);
735 void log_delete(char *fname, int mode)
737 static struct file_struct file;
738 int len = strlen(fname);
742 file.basename = fname;
744 if (!verbose && !stdout_format)
746 else if (am_server && protocol_version >= 29 && len < MAXPATHLEN) {
748 len++; /* directories include trailing null */
749 send_msg(MSG_DELETED, fname, len);
751 fmt = stdout_format_has_o_or_i ? stdout_format : "deleting %n";
752 log_formatted(FCLIENT, fmt, "del.", &file, &stats,
756 if (!logfile_name || dry_run || !logfile_format)
759 fmt = logfile_format_has_o_or_i ? logfile_format : "deleting %n";
760 log_formatted(FLOG, fmt, "del.", &file, &stats, ITEM_DELETED, NULL);
764 * Called when the transfer is interrupted for some reason.
766 * Code is one of the RERR_* codes from errcode.h, or terminating
769 void log_exit(int code, const char *file, int line)
772 rprintf(FLOG,"sent %.0f bytes received %.0f bytes total size %.0f\n",
773 (double)stats.total_written,
774 (double)stats.total_read,
775 (double)stats.total_size);
779 name = rerr_name(code);
781 name = "unexplained error";
783 /* VANISHED is not an error, only a warning */
784 if (code == RERR_VANISHED) {
785 rprintf(FINFO, "rsync warning: %s (code %d) at %s(%d) [%s=%s]\n",
786 name, code, file, line, who_am_i(), RSYNC_VERSION);
788 rprintf(FERROR, "rsync error: %s (code %d) at %s(%d) [%s=%s]\n",
789 name, code, file, line, who_am_i(), RSYNC_VERSION);