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 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 mode_t orig_umask;
45 extern char *auth_user;
46 extern char *stdout_format;
47 extern char *logfile_format;
48 extern char *logfile_name;
49 #if defined HAVE_ICONV_OPEN && defined HAVE_ICONV_H
50 extern iconv_t ic_chck;
53 static int log_initialised;
54 static int logfile_was_closed;
55 static FILE *logfile_fp;
58 int log_got_error = 0;
63 } const rerr_names[] = {
64 { RERR_SYNTAX , "syntax or usage error" },
65 { RERR_PROTOCOL , "protocol incompatibility" },
66 { RERR_FILESELECT , "errors selecting input/output files, dirs" },
67 { RERR_UNSUPPORTED, "requested action not supported" },
68 { RERR_STARTCLIENT, "error starting client-server protocol" },
69 { RERR_SOCKETIO , "error in socket IO" },
70 { RERR_FILEIO , "error in file IO" },
71 { RERR_STREAMIO , "error in rsync protocol data stream" },
72 { RERR_MESSAGEIO , "errors with program diagnostics" },
73 { RERR_IPC , "error in IPC code" },
74 { RERR_CRASHED , "sibling process crashed" },
75 { RERR_TERMINATED , "sibling process terminated abnormally" },
76 { RERR_SIGNAL1 , "received SIGUSR1" },
77 { RERR_SIGNAL , "received SIGINT, SIGTERM, or SIGHUP" },
78 { RERR_WAITCHILD , "waitpid() failed" },
79 { RERR_MALLOC , "error allocating core memory buffers" },
80 { RERR_PARTIAL , "some files could not be transferred" },
81 { RERR_VANISHED , "some files vanished before they could be transferred" },
82 { RERR_TIMEOUT , "timeout in data send/receive" },
83 { RERR_CMD_FAILED , "remote shell failed" },
84 { RERR_CMD_KILLED , "remote shell killed" },
85 { RERR_CMD_RUN , "remote command could not be run" },
86 { RERR_CMD_NOTFOUND,"remote command not found" },
87 { RERR_DEL_LIMIT , "the --max-delete limit stopped deletions" },
93 * Map from rsync error code to name, or return NULL.
95 static char const *rerr_name(int code)
98 for (i = 0; rerr_names[i].name; i++) {
99 if (rerr_names[i].code == code)
100 return rerr_names[i].name;
105 static void logit(int priority, char *buf)
107 if (logfile_was_closed)
110 fprintf(logfile_fp, "%s [%d] %s",
111 timestring(time(NULL)), (int)getpid(), buf);
114 syslog(priority, "%s", buf);
118 static void syslog_init()
120 static int been_here = 0;
121 int options = LOG_PID;
128 options |= LOG_NDELAY;
132 openlog("rsyncd", options, lp_syslog_facility());
134 openlog("rsyncd", options);
138 logit(LOG_INFO, "rsyncd started\n");
142 static void logfile_open(void)
144 mode_t old_umask = umask(022 | orig_umask);
145 logfile_fp = fopen(logfile_name, "a");
148 int fopen_errno = errno;
149 /* Rsync falls back to using syslog on failure. */
151 rsyserr(FERROR, fopen_errno,
152 "failed to open log-file %s", logfile_name);
153 rprintf(FINFO, "Ignoring \"log file\" setting.\n");
163 /* This looks pointless, but it is needed in order for the
164 * C library on some systems to fetch the timezone info
165 * before the chroot. */
166 timestring(time(NULL));
168 /* Optionally use a log file instead of syslog. (Non-daemon
169 * rsyncs will have already set logfile_name, as needed.) */
170 if (am_daemon && !logfile_name)
171 logfile_name = lp_log_file();
172 if (logfile_name && *logfile_name)
178 void logfile_close(void)
181 logfile_was_closed = 1;
187 void logfile_reopen(void)
189 if (logfile_was_closed) {
190 logfile_was_closed = 0;
195 static void filtered_fwrite(FILE *f, const char *buf, int len, int use_isprint)
197 const char *s, *end = buf + len;
198 for (s = buf; s < end; s++) {
200 && *s == '\\' && s[1] == '#'
201 && isdigit(*(uchar*)(s+2))
202 && isdigit(*(uchar*)(s+3))
203 && isdigit(*(uchar*)(s+4)))
205 && ((use_isprint && !isprint(*(uchar*)s))
206 || *(uchar*)s < ' '))) {
207 if (s != buf && fwrite(buf, s - buf, 1, f) != 1)
208 exit_cleanup(RERR_MESSAGEIO);
209 fprintf(f, "\\#%03o", *(uchar*)s);
213 if (buf != end && fwrite(buf, end - buf, 1, f) != 1)
214 exit_cleanup(RERR_MESSAGEIO);
217 /* this is the underlying (unformatted) rsync debugging function. Call
218 * it with FINFO, FERROR or FLOG. Note: recursion can happen with
219 * certain fatal conditions. */
220 void rwrite(enum logcode code, char *buf, int len)
222 int trailing_CR_or_NL;
226 exit_cleanup(RERR_MESSAGEIO);
228 if (am_server && msg_fd_out >= 0) {
229 /* Pass the message to our sibling. */
230 send_msg((enum msgcode)code, buf, len);
234 if (code == FSOCKERR) /* This gets simplified for a non-sibling. */
239 else if (am_daemon || logfile_name) {
242 int priority = code == FERROR ? LOG_WARNING : LOG_INFO;
247 if (!log_initialised)
249 strlcpy(msg, buf, MIN((int)sizeof msg, len + 1));
250 logit(priority, msg);
253 if (code == FLOG || (am_daemon && !am_server))
255 } else if (code == FLOG)
258 if (quiet && code != FERROR)
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;
280 exit_cleanup(RERR_MESSAGEIO);
283 trailing_CR_or_NL = len && (buf[len-1] == '\n' || buf[len-1] == '\r')
286 #if defined HAVE_ICONV_OPEN && defined HAVE_ICONV_H
287 if (ic_chck != (iconv_t)-1) {
289 char *in_buf = buf, *out_buf = convbuf;
290 size_t in_cnt = len, out_cnt = sizeof convbuf - 1;
292 iconv(ic_chck, NULL, 0, NULL, 0);
293 while (iconv(ic_chck, &in_buf,&in_cnt,
294 &out_buf,&out_cnt) == (size_t)-1) {
295 if (out_buf != convbuf) {
296 filtered_fwrite(f, convbuf, out_buf - convbuf, 0);
298 out_cnt = sizeof convbuf - 1;
302 fprintf(f, "\\#%03o", *(uchar*)in_buf++);
305 if (out_buf != convbuf)
306 filtered_fwrite(f, convbuf, out_buf - convbuf, 0);
309 filtered_fwrite(f, buf, len, !allow_8bit_chars);
311 if (trailing_CR_or_NL) {
312 fputc(trailing_CR_or_NL, f);
317 /* This is the rsync debugging function. Call it with FINFO, FERROR or
319 void rprintf(enum logcode code, const char *format, ...)
322 char buf[BIGPATHBUFLEN];
325 va_start(ap, format);
326 len = vsnprintf(buf, sizeof buf, format, ap);
329 /* Deal with buffer overruns. Instead of panicking, just
330 * truncate the resulting string. (Note that configure ensures
331 * that we have a vsnprintf() that doesn't ever return -1.) */
332 if (len > sizeof buf - 1) {
333 static const char ellipsis[] = "[...]";
335 /* Reset length, and zero-terminate the end of our buffer */
336 len = sizeof buf - 1;
339 /* Copy the ellipsis to the end of the string, but give
340 * us one extra character:
342 * v--- null byte at buf[sizeof buf - 1]
344 * -> abcd[...]00 <-- now two null bytes at end
346 * If the input format string has a trailing newline,
347 * we copy it into that extra null; if it doesn't, well,
348 * all we lose is one byte. */
349 memcpy(buf+len-sizeof ellipsis, ellipsis, sizeof ellipsis);
350 if (format[strlen(format)-1] == '\n') {
355 rwrite(code, buf, len);
358 /* This is like rprintf, but it also tries to print some
359 * representation of the error code. Normally errcode = errno.
361 * Unlike rprintf, this always adds a newline and there should not be
362 * one in the format string.
364 * Note that since strerror might involve dynamically loading a
365 * message catalog we need to call it once before chroot-ing. */
366 void rsyserr(enum logcode code, int errcode, const char *format, ...)
369 char buf[BIGPATHBUFLEN];
372 strcpy(buf, RSYNC_NAME ": ");
373 len = (sizeof RSYNC_NAME ": ") - 1;
375 va_start(ap, format);
376 len += vsnprintf(buf + len, sizeof buf - len, format, ap);
379 if (len < sizeof buf) {
380 len += snprintf(buf + len, sizeof buf - len,
381 ": %s (%d)\n", strerror(errcode), errcode);
383 if (len >= sizeof buf)
384 exit_cleanup(RERR_MESSAGEIO);
386 rwrite(code, buf, len);
389 void rflush(enum logcode code)
393 if (am_daemon || code == FLOG)
396 if (code == FERROR || am_server)
404 /* A generic logging routine for send/recv, with parameter substitiution. */
405 static void log_formatted(enum logcode code, char *format, char *op,
406 struct file_struct *file, struct stats *initial_stats,
407 int iflags, char *hlink)
409 char buf[MAXPATHLEN+1024], buf2[MAXPATHLEN], fmt[32];
416 /* We expand % codes one by one in place in buf. We don't
417 * copy in the terminating null of the inserted strings, but
418 * rather keep going until we reach the null of the format. */
419 total = strlcpy(buf, format, sizeof buf);
420 if (total > MAXPATHLEN) {
421 rprintf(FERROR, "log-format string is WAY too long!\n");
422 exit_cleanup(RERR_MESSAGEIO);
427 for (p = buf; (p = strchr(p, '%')) != NULL; ) {
432 while (isdigit(*(uchar*)p) && n - fmt < (int)(sizeof fmt) - 8)
449 strlcat(fmt, ".0f", sizeof fmt);
450 snprintf(buf2, sizeof buf2, fmt,
451 (double)file->length);
455 strlcat(fmt, "ld", sizeof fmt);
456 snprintf(buf2, sizeof buf2, fmt,
461 if (file->gid == GID_NONE)
464 strlcat(fmt, "ld", sizeof fmt);
465 snprintf(buf2, sizeof buf2, fmt,
471 strlcat(fmt, "ld", sizeof fmt);
472 snprintf(buf2, sizeof buf2, fmt,
477 n = timestring(file->modtime);
480 while ((cp = strchr(cp, ' ')) != NULL)
485 n = buf2 + MAXPATHLEN - PERMSTRING_SIZE;
486 permstring(n - 1, file->mode); /* skip the type char */
492 n = f_name(file, NULL);
493 if (am_sender && file->dir.root) {
494 pathjoin(buf2, sizeof buf2,
496 clean_fname(buf2, 0);
498 strlcpy(n, buf2, MAXPATHLEN);
507 n = f_name(file, NULL);
508 if (S_ISDIR(file->mode))
509 strlcat(n, "/", MAXPATHLEN);
512 if (hlink && *hlink) {
514 strcpy(buf2, " => ");
515 } else if (S_ISLNK(file->mode) && file->u.link) {
517 strcpy(buf2, " -> ");
524 strlcat(fmt, "s", sizeof fmt);
525 snprintf(buf2 + 4, sizeof buf2 - 4, fmt, n);
529 n = lp_name(module_id);
532 n = timestring(time(NULL));
535 n = lp_path(module_id);
542 b = stats.total_written -
543 initial_stats->total_written;
545 b = stats.total_read -
546 initial_stats->total_read;
548 strlcat(fmt, ".0f", sizeof fmt);
549 snprintf(buf2, sizeof buf2, fmt, (double)b);
554 b = stats.total_written -
555 initial_stats->total_written;
557 b = stats.total_read -
558 initial_stats->total_read;
560 strlcat(fmt, ".0f", sizeof fmt);
561 snprintf(buf2, sizeof buf2, fmt, (double)b);
565 if (iflags & ITEM_DELETED) {
569 n = buf2 + MAXPATHLEN - 32;
570 n[0] = iflags & ITEM_LOCAL_CHANGE
571 ? iflags & ITEM_XNAME_FOLLOWS ? 'h' : 'c'
572 : !(iflags & ITEM_TRANSFER) ? '.'
573 : !local_server && *op == 's' ? '<' : '>';
574 n[1] = S_ISDIR(file->mode) ? 'd'
575 : IS_SPECIAL(file->mode) ? 'S'
576 : IS_DEVICE(file->mode) ? 'D'
577 : S_ISLNK(file->mode) ? 'L' : 'f';
578 n[2] = !(iflags & ITEM_REPORT_CHECKSUM) ? '.' : 'c';
579 n[3] = !(iflags & ITEM_REPORT_SIZE) ? '.' : 's';
580 n[4] = !(iflags & ITEM_REPORT_TIME) ? '.'
581 : !preserve_times || S_ISLNK(file->mode) ? 'T' : 't';
582 n[5] = !(iflags & ITEM_REPORT_PERMS) ? '.' : 'p';
583 n[6] = !(iflags & ITEM_REPORT_OWNER) ? '.' : 'o';
584 n[7] = !(iflags & ITEM_REPORT_GROUP) ? '.' : 'g';
588 if (iflags & (ITEM_IS_NEW|ITEM_MISSING_DATA)) {
589 char ch = iflags & ITEM_IS_NEW ? '+' : '?';
591 for (i = 2; n[i]; i++)
593 } else if (n[0] == '.' || n[0] == 'h'
594 || (n[0] == 'c' && n[1] == 'f')) {
596 for (i = 2; n[i]; i++) {
601 for (i = 2; n[i]; i++)
608 /* "n" is the string to be inserted in place of this % code. */
611 if (n != buf2 && fmt[1]) {
612 strlcat(fmt, "s", sizeof fmt);
613 snprintf(buf2, sizeof buf2, fmt, n);
618 /* Subtract the length of the escape from the string's size. */
621 if (len + total >= (size_t)sizeof buf) {
623 "buffer overflow expanding %%%c -- exiting\n",
625 exit_cleanup(RERR_MESSAGEIO);
628 /* Shuffle the rest of the string along to make space for n */
629 if (len != (size_t)(p - s + 1))
630 memmove(s + len, p + 1, total - (s - buf) + 1);
633 /* Insert the contents of string "n", but NOT its null. */
637 /* Skip over inserted string; continue looking */
641 rwrite(code, buf, total);
644 /* Return 1 if the format escape is in the log-format string (e.g. look for
645 * the 'b' in the "%9b" format escape). */
646 int log_format_has(const char *format, char esc)
653 for (p = format; (p = strchr(p, '%')) != NULL; ) {
656 while (isdigit(*(uchar*)p))
666 /* Log the transfer of a file. If the code is FCLIENT, the output just goes
667 * to stdout. If it is FLOG, it just goes to the log file. Otherwise we
669 void log_item(enum logcode code, struct file_struct *file,
670 struct stats *initial_stats, int iflags, char *hlink)
672 char *s_or_r = am_sender ? "send" : "recv";
674 if (code != FLOG && stdout_format && !am_server) {
675 log_formatted(FCLIENT, stdout_format, s_or_r,
676 file, initial_stats, iflags, hlink);
678 if (code != FCLIENT && logfile_format && *logfile_format) {
679 log_formatted(FLOG, logfile_format, s_or_r,
680 file, initial_stats, iflags, hlink);
684 void maybe_log_item(struct file_struct *file, int iflags, int itemizing,
687 int significant_flags = iflags & SIGNIFICANT_ITEM_FLAGS;
688 int see_item = itemizing && (significant_flags || *buf
689 || stdout_format_has_i > 1 || (verbose > 1 && stdout_format_has_i));
690 int local_change = iflags & ITEM_LOCAL_CHANGE && significant_flags;
692 if (logfile_name && !dry_run && see_item
693 && (significant_flags || logfile_format_has_i))
694 log_item(FLOG, file, &stats, iflags, buf);
695 } else if (see_item || local_change || *buf
696 || (S_ISDIR(file->mode) && significant_flags)) {
697 enum logcode code = significant_flags || logfile_format_has_i ? FINFO : FCLIENT;
698 log_item(code, file, &stats, iflags, buf);
702 void log_delete(char *fname, int mode)
704 static struct file_struct file;
705 int len = strlen(fname);
709 file.basename = fname;
711 if (!verbose && !stdout_format)
713 else if (am_server && protocol_version >= 29 && len < MAXPATHLEN) {
715 len++; /* directories include trailing null */
716 send_msg(MSG_DELETED, fname, len);
718 fmt = stdout_format_has_o_or_i ? stdout_format : "deleting %n";
719 log_formatted(FCLIENT, fmt, "del.", &file, &stats,
723 if (!logfile_name || dry_run || !logfile_format)
726 fmt = logfile_format_has_o_or_i ? logfile_format : "deleting %n";
727 log_formatted(FLOG, fmt, "del.", &file, &stats, ITEM_DELETED, NULL);
731 * Called when the transfer is interrupted for some reason.
733 * Code is one of the RERR_* codes from errcode.h, or terminating
736 void log_exit(int code, const char *file, int line)
739 rprintf(FLOG,"sent %.0f bytes received %.0f bytes total size %.0f\n",
740 (double)stats.total_written,
741 (double)stats.total_read,
742 (double)stats.total_size);
746 name = rerr_name(code);
748 name = "unexplained error";
750 /* VANISHED is not an error, only a warning */
751 if (code == RERR_VANISHED) {
752 rprintf(FINFO, "rsync warning: %s (code %d) at %s(%d) [%s=%s]\n",
753 name, code, file, line, who_am_i(), RSYNC_VERSION);
755 rprintf(FERROR, "rsync error: %s (code %d) at %s(%d) [%s=%s]\n",
756 name, code, file, line, who_am_i(), RSYNC_VERSION);