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 log_format_has_i;
41 extern int log_format_has_o_or_i;
42 extern int logfile_format_has_o_or_i;
43 extern mode_t orig_umask;
44 extern char *auth_user;
45 extern char *log_format;
46 extern char *logfile_format;
47 extern char *logfile_name;
48 #if defined HAVE_ICONV_OPEN && defined HAVE_ICONV_H
49 extern iconv_t ic_chck;
52 static int log_initialised;
53 static int logfile_was_closed;
54 static FILE *logfile_fp;
57 int log_got_error = 0;
62 } const rerr_names[] = {
63 { RERR_SYNTAX , "syntax or usage error" },
64 { RERR_PROTOCOL , "protocol incompatibility" },
65 { RERR_FILESELECT , "errors selecting input/output files, dirs" },
66 { RERR_UNSUPPORTED, "requested action not supported" },
67 { RERR_STARTCLIENT, "error starting client-server protocol" },
68 { RERR_SOCKETIO , "error in socket IO" },
69 { RERR_FILEIO , "error in file IO" },
70 { RERR_STREAMIO , "error in rsync protocol data stream" },
71 { RERR_MESSAGEIO , "errors with program diagnostics" },
72 { RERR_IPC , "error in IPC code" },
73 { RERR_CRASHED , "sibling process crashed" },
74 { RERR_TERMINATED , "sibling process terminated abnormally" },
75 { RERR_SIGNAL1 , "received SIGUSR1" },
76 { RERR_SIGNAL , "received SIGINT, SIGTERM, or SIGHUP" },
77 { RERR_WAITCHILD , "waitpid() failed" },
78 { RERR_MALLOC , "error allocating core memory buffers" },
79 { RERR_PARTIAL , "some files could not be transferred" },
80 { RERR_VANISHED , "some files vanished before they could be transferred" },
81 { RERR_TIMEOUT , "timeout in data send/receive" },
82 { RERR_CMD_FAILED , "remote shell failed" },
83 { RERR_CMD_KILLED , "remote shell killed" },
84 { RERR_CMD_RUN , "remote command could not be run" },
85 { RERR_CMD_NOTFOUND,"remote command not found" },
86 { RERR_DEL_LIMIT , "the --max-delete limit stopped deletions" },
92 * Map from rsync error code to name, or return NULL.
94 static char const *rerr_name(int code)
97 for (i = 0; rerr_names[i].name; i++) {
98 if (rerr_names[i].code == code)
99 return rerr_names[i].name;
104 static void logit(int priority, char *buf)
106 if (logfile_was_closed)
109 fprintf(logfile_fp, "%s [%d] %s",
110 timestring(time(NULL)), (int)getpid(), buf);
113 syslog(priority, "%s", buf);
117 static void syslog_init()
119 static int been_here = 0;
120 int options = LOG_PID;
127 options |= LOG_NDELAY;
131 openlog("rsyncd", options, lp_syslog_facility());
133 openlog("rsyncd", options);
137 logit(LOG_INFO, "rsyncd started\n");
141 static void logfile_open(void)
143 mode_t old_umask = umask(022 | orig_umask);
144 logfile_fp = fopen(logfile_name, "a");
147 int fopen_errno = errno;
148 /* Rsync falls back to using syslog on failure. */
150 rsyserr(FERROR, fopen_errno,
151 "failed to open log-file %s", logfile_name);
152 rprintf(FINFO, "Ignoring \"log file\" setting.\n");
162 /* This looks pointless, but it is needed in order for the
163 * C library on some systems to fetch the timezone info
164 * before the chroot. */
165 timestring(time(NULL));
167 /* Optionally use a log file instead of syslog. (Non-daemon
168 * rsyncs will have already set logfile_name, as needed.) */
169 if (am_daemon && !logfile_name)
170 logfile_name = lp_log_file();
171 if (logfile_name && *logfile_name)
177 void logfile_close(void)
180 logfile_was_closed = 1;
186 void logfile_reopen(void)
188 if (logfile_was_closed) {
189 logfile_was_closed = 0;
194 static void filtered_fwrite(FILE *f, const char *buf, int len, int use_isprint)
196 const char *s, *end = buf + len;
197 for (s = buf; s < end; s++) {
199 && *s == '\\' && s[1] == '#'
200 && isdigit(*(uchar*)(s+2))
201 && isdigit(*(uchar*)(s+3))
202 && isdigit(*(uchar*)(s+4)))
204 && ((use_isprint && !isprint(*(uchar*)s))
205 || *(uchar*)s < ' '))) {
206 if (s != buf && fwrite(buf, s - buf, 1, f) != 1)
207 exit_cleanup(RERR_MESSAGEIO);
208 fprintf(f, "\\#%03o", *(uchar*)s);
212 if (buf != end && fwrite(buf, end - buf, 1, f) != 1)
213 exit_cleanup(RERR_MESSAGEIO);
216 /* this is the underlying (unformatted) rsync debugging function. Call
217 * it with FINFO, FERROR or FLOG. Note: recursion can happen with
218 * certain fatal conditions. */
219 void rwrite(enum logcode code, char *buf, int len)
221 int trailing_CR_or_NL;
225 exit_cleanup(RERR_MESSAGEIO);
227 if (quiet && code == FINFO)
230 if (am_server && msg_fd_out >= 0) {
231 /* Pass the message to our sibling. */
232 send_msg((enum msgcode)code, buf, len);
236 if (code == FSOCKERR) /* This gets simplified for a non-sibling. */
241 else if (am_daemon || logfile_name) {
244 int priority = code == FERROR ? LOG_WARNING : LOG_INFO;
249 if (!log_initialised)
251 strlcpy(msg, buf, MIN((int)sizeof msg, len + 1));
252 for (s = msg; *s == '\n' && s[1]; s++) {}
256 if (code == FLOG || (am_daemon && !am_server))
258 } else if (code == FLOG)
262 /* Pass the message to the non-server side. */
263 if (send_msg((enum msgcode)code, buf, len))
266 /* TODO: can we send the error to the user somehow? */
277 f = am_server ? stderr : stdout;
279 while (len > 1 && *buf == '\n') {
286 f = am_server ? stderr : stdout;
289 exit_cleanup(RERR_MESSAGEIO);
292 trailing_CR_or_NL = len && (buf[len-1] == '\n' || buf[len-1] == '\r')
295 #if defined HAVE_ICONV_OPEN && defined HAVE_ICONV_H
296 if (ic_chck != (iconv_t)-1) {
298 char *in_buf = buf, *out_buf = convbuf;
299 size_t in_cnt = len, out_cnt = sizeof convbuf - 1;
301 iconv(ic_chck, NULL, 0, NULL, 0);
302 while (iconv(ic_chck, &in_buf,&in_cnt,
303 &out_buf,&out_cnt) == (size_t)-1) {
304 if (out_buf != convbuf) {
305 filtered_fwrite(f, convbuf, out_buf - convbuf, 0);
307 out_cnt = sizeof convbuf - 1;
311 fprintf(f, "\\#%03o", *(uchar*)in_buf++);
314 if (out_buf != convbuf)
315 filtered_fwrite(f, convbuf, out_buf - convbuf, 0);
318 filtered_fwrite(f, buf, len, !allow_8bit_chars);
320 if (trailing_CR_or_NL) {
321 fputc(trailing_CR_or_NL, f);
326 /* This is the rsync debugging function. Call it with FINFO, FERROR or
328 void rprintf(enum logcode code, const char *format, ...)
331 char buf[BIGPATHBUFLEN];
334 va_start(ap, format);
335 len = vsnprintf(buf, sizeof buf, format, ap);
338 /* Deal with buffer overruns. Instead of panicking, just
339 * truncate the resulting string. (Note that configure ensures
340 * that we have a vsnprintf() that doesn't ever return -1.) */
341 if (len > sizeof buf - 1) {
342 static const char ellipsis[] = "[...]";
344 /* Reset length, and zero-terminate the end of our buffer */
345 len = sizeof buf - 1;
348 /* Copy the ellipsis to the end of the string, but give
349 * us one extra character:
351 * v--- null byte at buf[sizeof buf - 1]
353 * -> abcd[...]00 <-- now two null bytes at end
355 * If the input format string has a trailing newline,
356 * we copy it into that extra null; if it doesn't, well,
357 * all we lose is one byte. */
358 memcpy(buf+len-sizeof ellipsis, ellipsis, sizeof ellipsis);
359 if (format[strlen(format)-1] == '\n') {
364 rwrite(code, buf, len);
367 /* This is like rprintf, but it also tries to print some
368 * representation of the error code. Normally errcode = errno.
370 * Unlike rprintf, this always adds a newline and there should not be
371 * one in the format string.
373 * Note that since strerror might involve dynamically loading a
374 * message catalog we need to call it once before chroot-ing. */
375 void rsyserr(enum logcode code, int errcode, const char *format, ...)
378 char buf[BIGPATHBUFLEN];
381 strcpy(buf, RSYNC_NAME ": ");
382 len = (sizeof RSYNC_NAME ": ") - 1;
384 va_start(ap, format);
385 len += vsnprintf(buf + len, sizeof buf - len, format, ap);
388 if (len < sizeof buf) {
389 len += snprintf(buf + len, sizeof buf - len,
390 ": %s (%d)\n", strerror(errcode), errcode);
392 if (len >= sizeof buf)
393 exit_cleanup(RERR_MESSAGEIO);
395 rwrite(code, buf, len);
398 void rflush(enum logcode code)
402 if (am_daemon || code == FLOG)
405 if (code == FERROR || am_server)
413 /* a generic logging routine for send/recv, with parameter
415 static void log_formatted(enum logcode code, char *format, char *op,
416 struct file_struct *file, struct stats *initial_stats,
417 int iflags, char *hlink)
419 char buf[MAXPATHLEN+1024], buf2[MAXPATHLEN], fmt[32];
426 /* We expand % codes one by one in place in buf. We don't
427 * copy in the terminating null of the inserted strings, but
428 * rather keep going until we reach the null of the format. */
429 total = strlcpy(buf, format, sizeof buf);
430 if (total > MAXPATHLEN) {
431 rprintf(FERROR, "log-format string is WAY too long!\n");
432 exit_cleanup(RERR_MESSAGEIO);
437 for (p = buf; (p = strchr(p, '%')) != NULL; ) {
442 while (isdigit(*(uchar*)p) && n - fmt < (int)(sizeof fmt) - 8)
459 strlcat(fmt, ".0f", sizeof fmt);
460 snprintf(buf2, sizeof buf2, fmt,
461 (double)file->length);
465 strlcat(fmt, "ld", sizeof fmt);
466 snprintf(buf2, sizeof buf2, fmt,
471 if (file->gid == GID_NONE)
474 strlcat(fmt, "ld", sizeof fmt);
475 snprintf(buf2, sizeof buf2, fmt,
481 strlcat(fmt, "ld", sizeof fmt);
482 snprintf(buf2, sizeof buf2, fmt,
487 n = timestring(file->modtime);
490 while ((cp = strchr(cp, ' ')) != NULL)
495 n = buf2 + MAXPATHLEN - PERMSTRING_SIZE;
496 permstring(n - 1, file->mode); /* skip the type char */
502 n = f_name(file, NULL);
503 if (am_sender && file->dir.root) {
504 pathjoin(buf2, sizeof buf2,
506 clean_fname(buf2, 0);
508 strlcpy(n, buf2, MAXPATHLEN);
517 n = f_name(file, NULL);
518 if (S_ISDIR(file->mode))
519 strlcat(n, "/", MAXPATHLEN);
522 if (hlink && *hlink) {
524 strcpy(buf2, " => ");
525 } else if (S_ISLNK(file->mode) && file->u.link) {
527 strcpy(buf2, " -> ");
534 strlcat(fmt, "s", sizeof fmt);
535 snprintf(buf2 + 4, sizeof buf2 - 4, fmt, n);
539 n = lp_name(module_id);
542 n = timestring(time(NULL));
545 n = lp_path(module_id);
552 b = stats.total_written -
553 initial_stats->total_written;
555 b = stats.total_read -
556 initial_stats->total_read;
558 strlcat(fmt, ".0f", sizeof fmt);
559 snprintf(buf2, sizeof buf2, fmt, (double)b);
564 b = stats.total_written -
565 initial_stats->total_written;
567 b = stats.total_read -
568 initial_stats->total_read;
570 strlcat(fmt, ".0f", sizeof fmt);
571 snprintf(buf2, sizeof buf2, fmt, (double)b);
575 if (iflags & ITEM_DELETED) {
579 n = buf2 + MAXPATHLEN - 32;
580 n[0] = iflags & ITEM_LOCAL_CHANGE
581 ? iflags & ITEM_XNAME_FOLLOWS ? 'h' : 'c'
582 : !(iflags & ITEM_TRANSFER) ? '.'
583 : !local_server && *op == 's' ? '<' : '>';
584 n[1] = S_ISDIR(file->mode) ? 'd'
585 : IS_SPECIAL(file->mode) ? 'S'
586 : IS_DEVICE(file->mode) ? 'D'
587 : S_ISLNK(file->mode) ? 'L' : 'f';
588 n[2] = !(iflags & ITEM_REPORT_CHECKSUM) ? '.' : 'c';
589 n[3] = !(iflags & ITEM_REPORT_SIZE) ? '.' : 's';
590 n[4] = !(iflags & ITEM_REPORT_TIME) ? '.'
591 : !preserve_times || S_ISLNK(file->mode) ? 'T' : 't';
592 n[5] = !(iflags & ITEM_REPORT_PERMS) ? '.' : 'p';
593 n[6] = !(iflags & ITEM_REPORT_OWNER) ? '.' : 'o';
594 n[7] = !(iflags & ITEM_REPORT_GROUP) ? '.' : 'g';
598 if (iflags & (ITEM_IS_NEW|ITEM_MISSING_DATA)) {
599 char ch = iflags & ITEM_IS_NEW ? '+' : '?';
601 for (i = 2; n[i]; i++)
603 } else if (n[0] == '.' || n[0] == 'h'
604 || (n[0] == 'c' && n[1] == 'f')) {
606 for (i = 2; n[i]; i++) {
611 for (i = 2; n[i]; i++)
618 /* "n" is the string to be inserted in place of this % code. */
621 if (n != buf2 && fmt[1]) {
622 strlcat(fmt, "s", sizeof fmt);
623 snprintf(buf2, sizeof buf2, fmt, n);
628 /* Subtract the length of the escape from the string's size. */
631 if (len + total >= (size_t)sizeof buf) {
633 "buffer overflow expanding %%%c -- exiting\n",
635 exit_cleanup(RERR_MESSAGEIO);
638 /* Shuffle the rest of the string along to make space for n */
639 if (len != (size_t)(p - s + 1))
640 memmove(s + len, p + 1, total - (s - buf) + 1);
643 /* Insert the contents of string "n", but NOT its null. */
647 /* Skip over inserted string; continue looking */
651 rwrite(code, buf, total);
654 /* Return 1 if the format escape is in the log-format string (e.g. look for
655 * the 'b' in the "%9b" format escape). */
656 int log_format_has(const char *format, char esc)
663 for (p = format; (p = strchr(p, '%')) != NULL; ) {
666 while (isdigit(*(uchar*)p))
676 /* log the transfer of a file */
677 void log_item(struct file_struct *file, struct stats *initial_stats,
678 int iflags, char *hlink)
680 char *s_or_r = am_sender ? "send" : "recv";
682 if (log_format && !am_server) {
683 log_formatted(FNAME, log_format, s_or_r,
684 file, initial_stats, iflags, hlink);
685 } else if (logfile_format) {
686 log_formatted(FLOG, logfile_format, s_or_r,
687 file, initial_stats, iflags, hlink);
691 void maybe_log_item(struct file_struct *file, int iflags, int itemizing,
694 int significant_flags = iflags & SIGNIFICANT_ITEM_FLAGS;
695 int see_item = itemizing && (significant_flags || *buf
696 || log_format_has_i > 1 || (verbose > 1 && log_format_has_i));
697 int local_change = iflags & ITEM_LOCAL_CHANGE && significant_flags;
699 if (logfile_name && !dry_run && see_item)
700 log_item(file, &stats, iflags, buf);
701 } else if (see_item || local_change || *buf
702 || (S_ISDIR(file->mode) && significant_flags))
703 log_item(file, &stats, iflags, buf);
706 void log_delete(char *fname, int mode)
708 static struct file_struct file;
709 int len = strlen(fname);
713 file.basename = fname;
715 if (!verbose && !log_format)
717 else if (am_server && protocol_version >= 29 && len < MAXPATHLEN) {
719 len++; /* directories include trailing null */
720 send_msg(MSG_DELETED, fname, len);
722 fmt = log_format_has_o_or_i ? log_format : "deleting %n";
723 log_formatted(FCLIENT, fmt, "del.", &file, &stats,
727 if (!logfile_name || dry_run || !logfile_format)
730 fmt = logfile_format_has_o_or_i ? logfile_format : "deleting %n";
731 log_formatted(FLOG, fmt, "del.", &file, &stats, ITEM_DELETED, NULL);
735 * Called when the transfer is interrupted for some reason.
737 * Code is one of the RERR_* codes from errcode.h, or terminating
740 void log_exit(int code, const char *file, int line)
743 rprintf(FLOG,"sent %.0f bytes received %.0f bytes total size %.0f\n",
744 (double)stats.total_written,
745 (double)stats.total_read,
746 (double)stats.total_size);
750 name = rerr_name(code);
752 name = "unexplained error";
754 /* VANISHED is not an error, only a warning */
755 if (code == RERR_VANISHED) {
756 rprintf(FINFO, "rsync warning: %s (code %d) at %s(%d) [%s=%s]\n",
757 name, code, file, line, who_am_i(), RSYNC_VERSION);
759 rprintf(FERROR, "rsync error: %s (code %d) at %s(%d) [%s=%s]\n",
760 name, code, file, line, who_am_i(), RSYNC_VERSION);