1 /* -*- c-file-style: "linux"; -*-
3 Copyright (C) 1998-2001 by Andrew Tridgell <tridge@samba.org>
4 Copyright (C) 2000-2001 by Martin Pool <mbp@samba.org>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 Logging and utility functions.
25 Mapping to human-readable messages added by Martin Pool
26 <mbp@samba.org>, Oct 2000.
29 #if defined HAVE_ICONV_OPEN && defined HAVE_ICONV_H
38 extern int local_server;
41 extern int msg_fd_out;
42 extern int allow_8bit_chars;
43 extern int protocol_version;
44 extern int preserve_times;
45 extern int log_format_has_i;
46 extern int log_format_has_o_or_i;
47 extern int daemon_log_format_has_o_or_i;
48 extern char *auth_user;
49 extern char *log_format;
50 #if defined HAVE_ICONV_OPEN && defined HAVE_ICONV_H
51 extern iconv_t ic_chck;
54 static int log_initialised;
55 static int logfile_was_closed;
56 static char *logfname;
60 int log_got_error = 0;
65 } const rerr_names[] = {
66 { RERR_SYNTAX , "syntax or usage error" },
67 { RERR_PROTOCOL , "protocol incompatibility" },
68 { RERR_FILESELECT , "errors selecting input/output files, dirs" },
69 { RERR_UNSUPPORTED, "requested action not supported" },
70 { RERR_STARTCLIENT, "error starting client-server protocol" },
71 { RERR_SOCKETIO , "error in socket IO" },
72 { RERR_FILEIO , "error in file IO" },
73 { RERR_STREAMIO , "error in rsync protocol data stream" },
74 { RERR_MESSAGEIO , "errors with program diagnostics" },
75 { RERR_IPC , "error in IPC code" },
76 { RERR_CRASHED , "sibling process crashed" },
77 { RERR_TERMINATED , "sibling process terminated abnormally" },
78 { RERR_SIGNAL1 , "received SIGUSR1" },
79 { RERR_SIGNAL , "received SIGINT, SIGTERM, or SIGHUP" },
80 { RERR_WAITCHILD , "waitpid() failed" },
81 { RERR_MALLOC , "error allocating core memory buffers" },
82 { RERR_PARTIAL , "some files could not be transferred" },
83 { RERR_VANISHED , "some files vanished before they could be transferred" },
84 { RERR_TIMEOUT , "timeout in data send/receive" },
85 { RERR_CMD_FAILED , "remote shell failed" },
86 { RERR_CMD_KILLED , "remote shell killed" },
87 { RERR_CMD_RUN , "remote command could not be run" },
88 { RERR_CMD_NOTFOUND,"remote command not found" },
89 { RERR_DEL_LIMIT , "the --max-delete limit stopped deletions" },
95 * Map from rsync error code to name, or return NULL.
97 static char const *rerr_name(int code)
100 for (i = 0; rerr_names[i].name; i++) {
101 if (rerr_names[i].code == code)
102 return rerr_names[i].name;
107 static void logit(int priority, char *buf)
109 if (logfile_was_closed)
112 fprintf(logfile,"%s [%d] %s",
113 timestring(time(NULL)), (int)getpid(), buf);
116 syslog(priority, "%s", buf);
120 static void syslog_init()
122 static int been_here = 0;
123 int options = LOG_PID;
130 options |= LOG_NDELAY;
134 openlog("rsyncd", options, lp_syslog_facility());
136 openlog("rsyncd", options);
140 logit(LOG_INFO, "rsyncd started\n");
144 static void logfile_open(void)
146 extern int orig_umask;
147 int old_umask = umask(022 | orig_umask);
148 logfile = fopen(logfname, "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", logfname);
156 rprintf(FINFO, "Ignoring \"log file\" setting.\n");
168 /* this looks pointless, but it is needed in order for the
169 * C library on some systems to fetch the timezone info
170 * before the chroot */
174 /* optionally use a log file instead of syslog */
175 logfname = lp_log_file();
176 if (logfname && *logfname)
182 void logfile_close(void)
185 logfile_was_closed = 1;
191 void logfile_reopen(void)
193 if (logfile_was_closed) {
194 logfile_was_closed = 0;
199 static void filtered_fwrite(FILE *f, const char *buf, int len, int use_isprint)
201 const char *s, *end = buf + len;
202 for (s = buf; s < end; s++) {
204 && *s == '\\' && s[1] == '#'
205 && isdigit(*(uchar*)(s+2))
206 && isdigit(*(uchar*)(s+3))
207 && isdigit(*(uchar*)(s+4)))
209 && ((use_isprint && !isprint(*(uchar*)s))
210 || *(uchar*)s < ' '))) {
211 if (s != buf && fwrite(buf, s - buf, 1, f) != 1)
212 exit_cleanup(RERR_MESSAGEIO);
213 fprintf(f, "\\#%03o", *(uchar*)s);
217 if (buf != end && fwrite(buf, end - buf, 1, f) != 1)
218 exit_cleanup(RERR_MESSAGEIO);
221 /* this is the underlying (unformatted) rsync debugging function. Call
222 * it with FINFO, FERROR or FLOG. Note: recursion can happen with
223 * certain fatal conditions. */
224 void rwrite(enum logcode code, char *buf, int len)
226 int trailing_CR_or_NL;
230 exit_cleanup(RERR_MESSAGEIO);
232 if (quiet && code == FINFO)
235 if (am_server && msg_fd_out >= 0) {
236 /* Pass the message to our sibling. */
237 send_msg((enum msgcode)code, buf, len);
241 if (code == FSOCKERR) /* This gets simplified for a non-sibling. */
246 else if (am_daemon) {
249 int priority = code == FERROR ? LOG_WARNING : LOG_INFO;
254 if (!log_initialised)
256 strlcpy(msg, buf, MIN((int)sizeof msg, len + 1));
257 logit(priority, msg);
260 if (code == FLOG || !am_server)
262 } else if (code == FLOG)
266 /* Pass the message to the non-server side. */
267 if (io_multiplex_write((enum msgcode)code, buf, len))
270 /* TODO: can we send the error to the user somehow? */
281 f = am_server ? stderr : stdout;
283 while (len > 1 && *buf == '\n') {
290 f = am_server ? stderr : stdout;
293 exit_cleanup(RERR_MESSAGEIO);
296 trailing_CR_or_NL = len && (buf[len-1] == '\n' || buf[len-1] == '\r')
299 #if defined HAVE_ICONV_OPEN && defined HAVE_ICONV_H
300 if (ic_chck != (iconv_t)-1) {
302 char *in_buf = buf, *out_buf = convbuf;
303 size_t in_cnt = len, out_cnt = sizeof convbuf - 1;
305 iconv(ic_chck, NULL, 0, NULL, 0);
306 while (iconv(ic_chck, &in_buf,&in_cnt,
307 &out_buf,&out_cnt) == (size_t)-1) {
308 if (out_buf != convbuf) {
309 filtered_fwrite(f, convbuf, out_buf - convbuf, 0);
311 out_cnt = sizeof convbuf - 1;
315 fprintf(f, "\\#%03o", *(uchar*)in_buf++);
318 if (out_buf != convbuf)
319 filtered_fwrite(f, convbuf, out_buf - convbuf, 0);
322 filtered_fwrite(f, buf, len, !allow_8bit_chars);
324 if (trailing_CR_or_NL) {
325 fputc(trailing_CR_or_NL, f);
330 /* This is the rsync debugging function. Call it with FINFO, FERROR or
332 void rprintf(enum logcode code, const char *format, ...)
335 char buf[BIGPATHBUFLEN];
338 va_start(ap, format);
339 len = vsnprintf(buf, sizeof buf, format, ap);
342 /* Deal with buffer overruns. Instead of panicking, just
343 * truncate the resulting string. (Note that configure ensures
344 * that we have a vsnprintf() that doesn't ever return -1.) */
345 if (len > sizeof buf - 1) {
346 static const char ellipsis[] = "[...]";
348 /* Reset length, and zero-terminate the end of our buffer */
349 len = sizeof buf - 1;
352 /* Copy the ellipsis to the end of the string, but give
353 * us one extra character:
355 * v--- null byte at buf[sizeof buf - 1]
357 * -> abcd[...]00 <-- now two null bytes at end
359 * If the input format string has a trailing newline,
360 * we copy it into that extra null; if it doesn't, well,
361 * all we lose is one byte. */
362 memcpy(buf+len-sizeof ellipsis, ellipsis, sizeof ellipsis);
363 if (format[strlen(format)-1] == '\n') {
368 rwrite(code, buf, len);
371 /* This is like rprintf, but it also tries to print some
372 * representation of the error code. Normally errcode = errno.
374 * Unlike rprintf, this always adds a newline and there should not be
375 * one in the format string.
377 * Note that since strerror might involve dynamically loading a
378 * message catalog we need to call it once before chroot-ing. */
379 void rsyserr(enum logcode code, int errcode, const char *format, ...)
382 char buf[BIGPATHBUFLEN];
385 strcpy(buf, RSYNC_NAME ": ");
386 len = (sizeof RSYNC_NAME ": ") - 1;
388 va_start(ap, format);
389 len += vsnprintf(buf + len, sizeof buf - len, format, ap);
392 if (len < sizeof buf) {
393 len += snprintf(buf + len, sizeof buf - len,
394 ": %s (%d)\n", strerror(errcode), errcode);
396 if (len >= sizeof buf)
397 exit_cleanup(RERR_MESSAGEIO);
399 rwrite(code, buf, len);
402 void rflush(enum logcode code)
414 if (code == FERROR) {
425 if (!f) exit_cleanup(RERR_MESSAGEIO);
429 /* a generic logging routine for send/recv, with parameter
431 static void log_formatted(enum logcode code, char *format, char *op,
432 struct file_struct *file, struct stats *initial_stats,
433 int iflags, char *hlink)
435 char buf[MAXPATHLEN+1024], buf2[MAXPATHLEN], fmt[32];
442 /* We expand % codes one by one in place in buf. We don't
443 * copy in the terminating null of the inserted strings, but
444 * rather keep going until we reach the null of the format. */
445 total = strlcpy(buf, format, sizeof buf);
446 if (total > MAXPATHLEN) {
447 rprintf(FERROR, "log-format string is WAY too long!\n");
448 exit_cleanup(RERR_MESSAGEIO);
453 for (p = buf; (p = strchr(p, '%')) != NULL; ) {
458 while (isdigit(*(uchar*)p) && n - fmt < (int)(sizeof fmt) - 8)
475 strlcat(fmt, ".0f", sizeof fmt);
476 snprintf(buf2, sizeof buf2, fmt,
477 (double)file->length);
481 strlcat(fmt, "ld", sizeof fmt);
482 snprintf(buf2, sizeof buf2, fmt,
487 if (file->gid == GID_NONE)
490 strlcat(fmt, "ld", sizeof fmt);
491 snprintf(buf2, sizeof buf2, fmt,
497 strlcat(fmt, "ld", sizeof fmt);
498 snprintf(buf2, sizeof buf2, fmt,
503 n = timestring(file->modtime);
506 while ((cp = strchr(cp, ' ')) != NULL)
511 n = buf2 + MAXPATHLEN - PERMSTRING_SIZE;
512 permstring(n - 1, file->mode); /* skip the type char */
518 n = f_name(file, NULL);
519 if (am_sender && file->dir.root) {
520 pathjoin(buf2, sizeof buf2,
522 clean_fname(buf2, 0);
524 strlcpy(n, buf2, MAXPATHLEN);
533 n = f_name(file, NULL);
534 if (S_ISDIR(file->mode))
535 strlcat(n, "/", MAXPATHLEN);
538 if (hlink && *hlink) {
540 strcpy(buf2, " => ");
541 } else if (S_ISLNK(file->mode) && file->u.link) {
543 strcpy(buf2, " -> ");
550 strlcat(fmt, "s", sizeof fmt);
551 snprintf(buf2 + 4, sizeof buf2 - 4, fmt, n);
555 n = lp_name(module_id);
558 n = timestring(time(NULL));
561 n = lp_path(module_id);
568 b = stats.total_written -
569 initial_stats->total_written;
571 b = stats.total_read -
572 initial_stats->total_read;
574 strlcat(fmt, ".0f", sizeof fmt);
575 snprintf(buf2, sizeof buf2, fmt, (double)b);
580 b = stats.total_written -
581 initial_stats->total_written;
583 b = stats.total_read -
584 initial_stats->total_read;
586 strlcat(fmt, ".0f", sizeof fmt);
587 snprintf(buf2, sizeof buf2, fmt, (double)b);
591 if (iflags & ITEM_DELETED) {
595 n = buf2 + MAXPATHLEN - 32;
596 n[0] = iflags & ITEM_LOCAL_CHANGE
597 ? iflags & ITEM_XNAME_FOLLOWS ? 'h' : 'c'
598 : !(iflags & ITEM_TRANSFER) ? '.'
599 : !local_server && *op == 's' ? '<' : '>';
600 n[1] = S_ISDIR(file->mode) ? 'd'
601 : IS_SPECIAL(file->mode) ? 'S'
602 : IS_DEVICE(file->mode) ? 'D'
603 : S_ISLNK(file->mode) ? 'L' : 'f';
604 n[2] = !(iflags & ITEM_REPORT_CHECKSUM) ? '.' : 'c';
605 n[3] = !(iflags & ITEM_REPORT_SIZE) ? '.' : 's';
606 n[4] = !(iflags & ITEM_REPORT_TIME) ? '.'
607 : !preserve_times || S_ISLNK(file->mode) ? 'T' : 't';
608 n[5] = !(iflags & ITEM_REPORT_PERMS) ? '.' : 'p';
609 n[6] = !(iflags & ITEM_REPORT_OWNER) ? '.' : 'o';
610 n[7] = !(iflags & ITEM_REPORT_GROUP) ? '.' : 'g';
614 if (iflags & (ITEM_IS_NEW|ITEM_MISSING_DATA)) {
615 char ch = iflags & ITEM_IS_NEW ? '+' : '?';
617 for (i = 2; n[i]; i++)
619 } else if (n[0] == '.' || n[0] == 'h'
620 || (n[0] == 'c' && n[1] == 'f')) {
622 for (i = 2; n[i]; i++) {
627 for (i = 2; n[i]; i++)
634 /* "n" is the string to be inserted in place of this % code. */
637 if (n != buf2 && fmt[1]) {
638 strlcat(fmt, "s", sizeof fmt);
639 snprintf(buf2, sizeof buf2, fmt, n);
644 /* Subtract the length of the escape from the string's size. */
647 if (len + total >= (size_t)sizeof buf) {
649 "buffer overflow expanding %%%c -- exiting\n",
651 exit_cleanup(RERR_MESSAGEIO);
654 /* Shuffle the rest of the string along to make space for n */
655 if (len != (size_t)(p - s + 1))
656 memmove(s + len, p + 1, total - (s - buf) + 1);
659 /* Insert the contents of string "n", but NOT its null. */
663 /* Skip over inserted string; continue looking */
667 rwrite(code, buf, total);
670 /* Return 1 if the format escape is in the log-format string (e.g. look for
671 * the 'b' in the "%9b" format escape). */
672 int log_format_has(const char *format, char esc)
679 for (p = format; (p = strchr(p, '%')) != NULL; ) {
682 while (isdigit(*(uchar*)p))
692 /* log the transfer of a file */
693 void log_item(struct file_struct *file, struct stats *initial_stats,
694 int iflags, char *hlink)
696 char *s_or_r = am_sender ? "send" : "recv";
698 if (lp_transfer_logging(module_id)) {
699 log_formatted(FLOG, lp_log_format(module_id), s_or_r,
700 file, initial_stats, iflags, hlink);
701 } else if (log_format && !am_server) {
702 log_formatted(FNAME, log_format, s_or_r,
703 file, initial_stats, iflags, hlink);
707 void maybe_log_item(struct file_struct *file, int iflags, int itemizing,
710 int significant_flags = iflags & SIGNIFICANT_ITEM_FLAGS;
711 int see_item = itemizing && (significant_flags || *buf
712 || log_format_has_i > 1 || (verbose > 1 && log_format_has_i));
713 int local_change = iflags & ITEM_LOCAL_CHANGE && significant_flags;
715 if (am_daemon && !dry_run && see_item)
716 log_item(file, &stats, iflags, buf);
717 } else if (see_item || local_change || *buf
718 || (S_ISDIR(file->mode) && significant_flags))
719 log_item(file, &stats, iflags, buf);
722 void log_delete(char *fname, int mode)
724 static struct file_struct file;
725 int len = strlen(fname);
729 file.basename = fname;
731 if (!verbose && !log_format)
733 else if (am_server && protocol_version >= 29 && len < MAXPATHLEN) {
735 len++; /* directories include trailing null */
736 send_msg(MSG_DELETED, fname, len);
738 fmt = log_format_has_o_or_i ? log_format : "deleting %n";
739 log_formatted(FCLIENT, fmt, "del.", &file, &stats,
743 if (!am_daemon || dry_run || !lp_transfer_logging(module_id))
746 fmt = daemon_log_format_has_o_or_i ? lp_log_format(module_id) : "deleting %n";
747 log_formatted(FLOG, fmt, "del.", &file, &stats, ITEM_DELETED, NULL);
751 * Called when the transfer is interrupted for some reason.
753 * Code is one of the RERR_* codes from errcode.h, or terminating
756 void log_exit(int code, const char *file, int line)
759 rprintf(FLOG,"sent %.0f bytes received %.0f bytes total size %.0f\n",
760 (double)stats.total_written,
761 (double)stats.total_read,
762 (double)stats.total_size);
766 name = rerr_name(code);
768 name = "unexplained error";
770 /* VANISHED is not an error, only a warning */
771 if (code == RERR_VANISHED) {
772 rprintf(FINFO, "rsync warning: %s (code %d) at %s(%d) [%s]\n",
773 name, code, file, line, who_am_i());
775 rprintf(FERROR, "rsync error: %s (code %d) at %s(%d) [%s]\n",
776 name, code, file, line, who_am_i());