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 daemon_log_format_has_o_or_i;
43 extern mode_t orig_umask;
44 extern char *auth_user;
45 extern char *log_format;
46 #if defined HAVE_ICONV_OPEN && defined HAVE_ICONV_H
47 extern iconv_t ic_chck;
50 static int log_initialised;
51 static int logfile_was_closed;
52 static char *logfname;
56 int log_got_error = 0;
61 } const rerr_names[] = {
62 { RERR_SYNTAX , "syntax or usage error" },
63 { RERR_PROTOCOL , "protocol incompatibility" },
64 { RERR_FILESELECT , "errors selecting input/output files, dirs" },
65 { RERR_UNSUPPORTED, "requested action not supported" },
66 { RERR_STARTCLIENT, "error starting client-server protocol" },
67 { RERR_SOCKETIO , "error in socket IO" },
68 { RERR_FILEIO , "error in file IO" },
69 { RERR_STREAMIO , "error in rsync protocol data stream" },
70 { RERR_MESSAGEIO , "errors with program diagnostics" },
71 { RERR_IPC , "error in IPC code" },
72 { RERR_CRASHED , "sibling process crashed" },
73 { RERR_TERMINATED , "sibling process terminated abnormally" },
74 { RERR_SIGNAL1 , "received SIGUSR1" },
75 { RERR_SIGNAL , "received SIGINT, SIGTERM, or SIGHUP" },
76 { RERR_WAITCHILD , "waitpid() failed" },
77 { RERR_MALLOC , "error allocating core memory buffers" },
78 { RERR_PARTIAL , "some files could not be transferred" },
79 { RERR_VANISHED , "some files vanished before they could be transferred" },
80 { RERR_TIMEOUT , "timeout in data send/receive" },
81 { RERR_CMD_FAILED , "remote shell failed" },
82 { RERR_CMD_KILLED , "remote shell killed" },
83 { RERR_CMD_RUN , "remote command could not be run" },
84 { RERR_CMD_NOTFOUND,"remote command not found" },
85 { RERR_DEL_LIMIT , "the --max-delete limit stopped deletions" },
91 * Map from rsync error code to name, or return NULL.
93 static char const *rerr_name(int code)
96 for (i = 0; rerr_names[i].name; i++) {
97 if (rerr_names[i].code == code)
98 return rerr_names[i].name;
103 static void logit(int priority, char *buf)
105 if (logfile_was_closed)
108 fprintf(logfile,"%s [%d] %s",
109 timestring(time(NULL)), (int)getpid(), buf);
112 syslog(priority, "%s", buf);
116 static void syslog_init()
118 static int been_here = 0;
119 int options = LOG_PID;
126 options |= LOG_NDELAY;
130 openlog("rsyncd", options, lp_syslog_facility());
132 openlog("rsyncd", options);
136 logit(LOG_INFO, "rsyncd started\n");
140 static void logfile_open(void)
142 mode_t old_umask = umask(022 | orig_umask);
143 logfile = fopen(logfname, "a");
146 int fopen_errno = errno;
147 /* Rsync falls back to using syslog on failure. */
149 rsyserr(FERROR, fopen_errno,
150 "failed to open log-file %s", logfname);
151 rprintf(FINFO, "Ignoring \"log file\" setting.\n");
161 /* This looks pointless, but it is needed in order for the
162 * C library on some systems to fetch the timezone info
163 * before the chroot. */
164 timestring(time(NULL));
166 /* optionally use a log file instead of syslog */
167 logfname = lp_log_file();
168 if (logfname && *logfname)
174 void logfile_close(void)
177 logfile_was_closed = 1;
183 void logfile_reopen(void)
185 if (logfile_was_closed) {
186 logfile_was_closed = 0;
191 static void filtered_fwrite(FILE *f, const char *buf, int len, int use_isprint)
193 const char *s, *end = buf + len;
194 for (s = buf; s < end; s++) {
196 && *s == '\\' && s[1] == '#'
197 && isdigit(*(uchar*)(s+2))
198 && isdigit(*(uchar*)(s+3))
199 && isdigit(*(uchar*)(s+4)))
201 && ((use_isprint && !isprint(*(uchar*)s))
202 || *(uchar*)s < ' '))) {
203 if (s != buf && fwrite(buf, s - buf, 1, f) != 1)
204 exit_cleanup(RERR_MESSAGEIO);
205 fprintf(f, "\\#%03o", *(uchar*)s);
209 if (buf != end && fwrite(buf, end - buf, 1, f) != 1)
210 exit_cleanup(RERR_MESSAGEIO);
213 /* this is the underlying (unformatted) rsync debugging function. Call
214 * it with FINFO, FERROR or FLOG. Note: recursion can happen with
215 * certain fatal conditions. */
216 void rwrite(enum logcode code, char *buf, int len)
218 int trailing_CR_or_NL;
222 exit_cleanup(RERR_MESSAGEIO);
224 if (quiet && code == FINFO)
227 if (am_server && msg_fd_out >= 0) {
228 /* Pass the message to our sibling. */
229 send_msg((enum msgcode)code, buf, len);
233 if (code == FSOCKERR) /* This gets simplified for a non-sibling. */
238 else if (am_daemon) {
241 int priority = code == FERROR ? LOG_WARNING : LOG_INFO;
246 if (!log_initialised)
248 strlcpy(msg, buf, MIN((int)sizeof msg, len + 1));
249 logit(priority, msg);
252 if (code == FLOG || !am_server)
254 } else if (code == FLOG)
258 /* Pass the message to the non-server side. */
259 if (send_msg((enum msgcode)code, buf, len))
262 /* TODO: can we send the error to the user somehow? */
273 f = am_server ? stderr : stdout;
275 while (len > 1 && *buf == '\n') {
282 f = am_server ? stderr : stdout;
285 exit_cleanup(RERR_MESSAGEIO);
288 trailing_CR_or_NL = len && (buf[len-1] == '\n' || buf[len-1] == '\r')
291 #if defined HAVE_ICONV_OPEN && defined HAVE_ICONV_H
292 if (ic_chck != (iconv_t)-1) {
294 char *in_buf = buf, *out_buf = convbuf;
295 size_t in_cnt = len, out_cnt = sizeof convbuf - 1;
297 iconv(ic_chck, NULL, 0, NULL, 0);
298 while (iconv(ic_chck, &in_buf,&in_cnt,
299 &out_buf,&out_cnt) == (size_t)-1) {
300 if (out_buf != convbuf) {
301 filtered_fwrite(f, convbuf, out_buf - convbuf, 0);
303 out_cnt = sizeof convbuf - 1;
307 fprintf(f, "\\#%03o", *(uchar*)in_buf++);
310 if (out_buf != convbuf)
311 filtered_fwrite(f, convbuf, out_buf - convbuf, 0);
314 filtered_fwrite(f, buf, len, !allow_8bit_chars);
316 if (trailing_CR_or_NL) {
317 fputc(trailing_CR_or_NL, f);
322 /* This is the rsync debugging function. Call it with FINFO, FERROR or
324 void rprintf(enum logcode code, const char *format, ...)
327 char buf[BIGPATHBUFLEN];
330 va_start(ap, format);
331 len = vsnprintf(buf, sizeof buf, format, ap);
334 /* Deal with buffer overruns. Instead of panicking, just
335 * truncate the resulting string. (Note that configure ensures
336 * that we have a vsnprintf() that doesn't ever return -1.) */
337 if (len > sizeof buf - 1) {
338 static const char ellipsis[] = "[...]";
340 /* Reset length, and zero-terminate the end of our buffer */
341 len = sizeof buf - 1;
344 /* Copy the ellipsis to the end of the string, but give
345 * us one extra character:
347 * v--- null byte at buf[sizeof buf - 1]
349 * -> abcd[...]00 <-- now two null bytes at end
351 * If the input format string has a trailing newline,
352 * we copy it into that extra null; if it doesn't, well,
353 * all we lose is one byte. */
354 memcpy(buf+len-sizeof ellipsis, ellipsis, sizeof ellipsis);
355 if (format[strlen(format)-1] == '\n') {
360 rwrite(code, buf, len);
363 /* This is like rprintf, but it also tries to print some
364 * representation of the error code. Normally errcode = errno.
366 * Unlike rprintf, this always adds a newline and there should not be
367 * one in the format string.
369 * Note that since strerror might involve dynamically loading a
370 * message catalog we need to call it once before chroot-ing. */
371 void rsyserr(enum logcode code, int errcode, const char *format, ...)
374 char buf[BIGPATHBUFLEN];
377 strcpy(buf, RSYNC_NAME ": ");
378 len = (sizeof RSYNC_NAME ": ") - 1;
380 va_start(ap, format);
381 len += vsnprintf(buf + len, sizeof buf - len, format, ap);
384 if (len < sizeof buf) {
385 len += snprintf(buf + len, sizeof buf - len,
386 ": %s (%d)\n", strerror(errcode), errcode);
388 if (len >= sizeof buf)
389 exit_cleanup(RERR_MESSAGEIO);
391 rwrite(code, buf, len);
394 void rflush(enum logcode code)
406 if (code == FERROR) {
417 if (!f) exit_cleanup(RERR_MESSAGEIO);
421 /* a generic logging routine for send/recv, with parameter
423 static void log_formatted(enum logcode code, char *format, char *op,
424 struct file_struct *file, struct stats *initial_stats,
425 int iflags, char *hlink)
427 char buf[MAXPATHLEN+1024], buf2[MAXPATHLEN], fmt[32];
434 /* We expand % codes one by one in place in buf. We don't
435 * copy in the terminating null of the inserted strings, but
436 * rather keep going until we reach the null of the format. */
437 total = strlcpy(buf, format, sizeof buf);
438 if (total > MAXPATHLEN) {
439 rprintf(FERROR, "log-format string is WAY too long!\n");
440 exit_cleanup(RERR_MESSAGEIO);
445 for (p = buf; (p = strchr(p, '%')) != NULL; ) {
450 while (isdigit(*(uchar*)p) && n - fmt < (int)(sizeof fmt) - 8)
467 strlcat(fmt, ".0f", sizeof fmt);
468 snprintf(buf2, sizeof buf2, fmt,
469 (double)file->length);
473 strlcat(fmt, "ld", sizeof fmt);
474 snprintf(buf2, sizeof buf2, fmt,
479 if (file->gid == GID_NONE)
482 strlcat(fmt, "ld", sizeof fmt);
483 snprintf(buf2, sizeof buf2, fmt,
489 strlcat(fmt, "ld", sizeof fmt);
490 snprintf(buf2, sizeof buf2, fmt,
495 n = timestring(file->modtime);
498 while ((cp = strchr(cp, ' ')) != NULL)
503 n = buf2 + MAXPATHLEN - PERMSTRING_SIZE;
504 permstring(n - 1, file->mode); /* skip the type char */
510 n = f_name(file, NULL);
511 if (am_sender && file->dir.root) {
512 pathjoin(buf2, sizeof buf2,
514 clean_fname(buf2, 0);
516 strlcpy(n, buf2, MAXPATHLEN);
525 n = f_name(file, NULL);
526 if (S_ISDIR(file->mode))
527 strlcat(n, "/", MAXPATHLEN);
530 if (hlink && *hlink) {
532 strcpy(buf2, " => ");
533 } else if (S_ISLNK(file->mode) && file->u.link) {
535 strcpy(buf2, " -> ");
542 strlcat(fmt, "s", sizeof fmt);
543 snprintf(buf2 + 4, sizeof buf2 - 4, fmt, n);
547 n = lp_name(module_id);
550 n = timestring(time(NULL));
553 n = lp_path(module_id);
560 b = stats.total_written -
561 initial_stats->total_written;
563 b = stats.total_read -
564 initial_stats->total_read;
566 strlcat(fmt, ".0f", sizeof fmt);
567 snprintf(buf2, sizeof buf2, fmt, (double)b);
572 b = stats.total_written -
573 initial_stats->total_written;
575 b = stats.total_read -
576 initial_stats->total_read;
578 strlcat(fmt, ".0f", sizeof fmt);
579 snprintf(buf2, sizeof buf2, fmt, (double)b);
583 if (iflags & ITEM_DELETED) {
587 n = buf2 + MAXPATHLEN - 32;
588 n[0] = iflags & ITEM_LOCAL_CHANGE
589 ? iflags & ITEM_XNAME_FOLLOWS ? 'h' : 'c'
590 : !(iflags & ITEM_TRANSFER) ? '.'
591 : !local_server && *op == 's' ? '<' : '>';
592 n[1] = S_ISDIR(file->mode) ? 'd'
593 : IS_SPECIAL(file->mode) ? 'S'
594 : IS_DEVICE(file->mode) ? 'D'
595 : S_ISLNK(file->mode) ? 'L' : 'f';
596 n[2] = !(iflags & ITEM_REPORT_CHECKSUM) ? '.' : 'c';
597 n[3] = !(iflags & ITEM_REPORT_SIZE) ? '.' : 's';
598 n[4] = !(iflags & ITEM_REPORT_TIME) ? '.'
599 : !preserve_times || S_ISLNK(file->mode) ? 'T' : 't';
600 n[5] = !(iflags & ITEM_REPORT_PERMS) ? '.' : 'p';
601 n[6] = !(iflags & ITEM_REPORT_OWNER) ? '.' : 'o';
602 n[7] = !(iflags & ITEM_REPORT_GROUP) ? '.' : 'g';
606 if (iflags & (ITEM_IS_NEW|ITEM_MISSING_DATA)) {
607 char ch = iflags & ITEM_IS_NEW ? '+' : '?';
609 for (i = 2; n[i]; i++)
611 } else if (n[0] == '.' || n[0] == 'h'
612 || (n[0] == 'c' && n[1] == 'f')) {
614 for (i = 2; n[i]; i++) {
619 for (i = 2; n[i]; i++)
626 /* "n" is the string to be inserted in place of this % code. */
629 if (n != buf2 && fmt[1]) {
630 strlcat(fmt, "s", sizeof fmt);
631 snprintf(buf2, sizeof buf2, fmt, n);
636 /* Subtract the length of the escape from the string's size. */
639 if (len + total >= (size_t)sizeof buf) {
641 "buffer overflow expanding %%%c -- exiting\n",
643 exit_cleanup(RERR_MESSAGEIO);
646 /* Shuffle the rest of the string along to make space for n */
647 if (len != (size_t)(p - s + 1))
648 memmove(s + len, p + 1, total - (s - buf) + 1);
651 /* Insert the contents of string "n", but NOT its null. */
655 /* Skip over inserted string; continue looking */
659 rwrite(code, buf, total);
662 /* Return 1 if the format escape is in the log-format string (e.g. look for
663 * the 'b' in the "%9b" format escape). */
664 int log_format_has(const char *format, char esc)
671 for (p = format; (p = strchr(p, '%')) != NULL; ) {
674 while (isdigit(*(uchar*)p))
684 /* log the transfer of a file */
685 void log_item(struct file_struct *file, struct stats *initial_stats,
686 int iflags, char *hlink)
688 char *s_or_r = am_sender ? "send" : "recv";
690 if (lp_transfer_logging(module_id)) {
691 log_formatted(FLOG, lp_log_format(module_id), s_or_r,
692 file, initial_stats, iflags, hlink);
693 } else if (log_format && !am_server) {
694 log_formatted(FNAME, log_format, s_or_r,
695 file, initial_stats, iflags, hlink);
699 void maybe_log_item(struct file_struct *file, int iflags, int itemizing,
702 int significant_flags = iflags & SIGNIFICANT_ITEM_FLAGS;
703 int see_item = itemizing && (significant_flags || *buf
704 || log_format_has_i > 1 || (verbose > 1 && log_format_has_i));
705 int local_change = iflags & ITEM_LOCAL_CHANGE && significant_flags;
707 if (am_daemon && !dry_run && see_item)
708 log_item(file, &stats, iflags, buf);
709 } else if (see_item || local_change || *buf
710 || (S_ISDIR(file->mode) && significant_flags))
711 log_item(file, &stats, iflags, buf);
714 void log_delete(char *fname, int mode)
716 static struct file_struct file;
717 int len = strlen(fname);
721 file.basename = fname;
723 if (!verbose && !log_format)
725 else if (am_server && protocol_version >= 29 && len < MAXPATHLEN) {
727 len++; /* directories include trailing null */
728 send_msg(MSG_DELETED, fname, len);
730 fmt = log_format_has_o_or_i ? log_format : "deleting %n";
731 log_formatted(FCLIENT, fmt, "del.", &file, &stats,
735 if (!am_daemon || dry_run || !lp_transfer_logging(module_id))
738 fmt = daemon_log_format_has_o_or_i ? lp_log_format(module_id) : "deleting %n";
739 log_formatted(FLOG, fmt, "del.", &file, &stats, ITEM_DELETED, NULL);
743 * Called when the transfer is interrupted for some reason.
745 * Code is one of the RERR_* codes from errcode.h, or terminating
748 void log_exit(int code, const char *file, int line)
751 rprintf(FLOG,"sent %.0f bytes received %.0f bytes total size %.0f\n",
752 (double)stats.total_written,
753 (double)stats.total_read,
754 (double)stats.total_size);
758 name = rerr_name(code);
760 name = "unexplained error";
762 /* VANISHED is not an error, only a warning */
763 if (code == RERR_VANISHED) {
764 rprintf(FINFO, "rsync warning: %s (code %d) at %s(%d) [%s=%s]\n",
765 name, code, file, line, who_am_i(), RSYNC_VERSION);
767 rprintf(FERROR, "rsync error: %s (code %d) at %s(%d) [%s=%s]\n",
768 name, code, file, line, who_am_i(), RSYNC_VERSION);