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.
35 extern int local_server;
38 extern int msg_fd_out;
39 extern int protocol_version;
40 extern int preserve_times;
41 extern int log_format_has_o_or_i;
42 extern int daemon_log_format_has_o_or_i;
43 extern char *auth_user;
44 extern char *log_format;
46 static int log_initialised;
47 static int logfile_was_closed;
48 static char *logfname;
52 int log_got_error = 0;
57 } const rerr_names[] = {
58 { RERR_SYNTAX , "syntax or usage error" },
59 { RERR_PROTOCOL , "protocol incompatibility" },
60 { RERR_FILESELECT , "errors selecting input/output files, dirs" },
61 { RERR_UNSUPPORTED, "requested action not supported" },
62 { RERR_STARTCLIENT, "error starting client-server protocol" },
63 { RERR_SOCKETIO , "error in socket IO" },
64 { RERR_FILEIO , "error in file IO" },
65 { RERR_STREAMIO , "error in rsync protocol data stream" },
66 { RERR_MESSAGEIO , "errors with program diagnostics" },
67 { RERR_IPC , "error in IPC code" },
68 { RERR_CRASHED , "sibling process crashed" },
69 { RERR_TERMINATED , "sibling process terminated abnormally" },
70 { RERR_SIGNAL , "received SIGUSR1 or SIGINT" },
71 { RERR_WAITCHILD , "waitpid() failed" },
72 { RERR_MALLOC , "error allocating core memory buffers" },
73 { RERR_PARTIAL , "some files could not be transferred" },
74 { RERR_VANISHED , "some files vanished before they could be transferred" },
75 { RERR_TIMEOUT , "timeout in data send/receive" },
76 { RERR_CMD_FAILED , "remote shell failed" },
77 { RERR_CMD_KILLED , "remote shell killed" },
78 { RERR_CMD_RUN , "remote command could not be run" },
79 { RERR_CMD_NOTFOUND,"remote command not found" },
80 { RERR_DEL_LIMIT , "the --max-delete limit stopped deletions" },
87 * Map from rsync error code to name, or return NULL.
89 static char const *rerr_name(int code)
92 for (i = 0; rerr_names[i].name; i++) {
93 if (rerr_names[i].code == code)
94 return rerr_names[i].name;
100 static void logit(int priority, char *buf)
102 if (logfile_was_closed)
105 fprintf(logfile,"%s [%d] %s",
106 timestring(time(NULL)), (int)getpid(), buf);
109 syslog(priority, "%s", buf);
113 static void syslog_init()
115 static int been_here = 0;
116 int options = LOG_PID;
123 options |= LOG_NDELAY;
127 openlog("rsyncd", options, lp_syslog_facility());
129 openlog("rsyncd", options);
133 logit(LOG_INFO, "rsyncd started\n");
137 static void logfile_open(void)
139 extern int orig_umask;
140 int old_umask = umask(022 | orig_umask);
141 logfile = fopen(logfname, "a");
144 int fopen_errno = errno;
145 /* Rsync falls back to using syslog on failure. */
147 rsyserr(FERROR, fopen_errno,
148 "failed to open log-file %s", logfname);
149 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 */
167 /* optionally use a log file instead of syslog */
168 logfname = lp_log_file();
169 if (logfname && *logfname)
175 void logfile_close(void)
178 logfile_was_closed = 1;
184 void logfile_reopen(void)
186 if (logfile_was_closed) {
187 logfile_was_closed = 0;
192 /* this is the underlying (unformatted) rsync debugging function. Call
193 * it with FINFO, FERROR or FLOG */
194 void rwrite(enum logcode code, char *buf, int len)
197 /* recursion can happen with certain fatal conditions */
199 if (quiet && code == FINFO)
203 exit_cleanup(RERR_MESSAGEIO);
207 if (am_server && msg_fd_out >= 0) {
208 /* Pass the message to our sibling. */
209 send_msg((enum msgcode)code, buf, len);
215 else if (am_daemon) {
218 int priority = code == FERROR ? LOG_WARNING : LOG_INFO;
223 if (!log_initialised)
225 strlcpy(msg, buf, MIN((int)sizeof msg, len + 1));
226 logit(priority, msg);
229 if (code == FLOG || !am_server)
231 } else if (code == FLOG)
235 /* Pass the message to the non-server side. */
236 if (io_multiplex_write((enum msgcode)code, buf, len))
239 /* TODO: can we send the error to the user somehow? */
244 if (code == FERROR) {
250 f = am_server ? stderr : stdout;
253 exit_cleanup(RERR_MESSAGEIO);
255 if (fwrite(buf, len, 1, f) != 1)
256 exit_cleanup(RERR_MESSAGEIO);
258 if (buf[len-1] == '\r' || buf[len-1] == '\n')
263 /* This is the rsync debugging function. Call it with FINFO, FERROR or
265 void rprintf(enum logcode code, const char *format, ...)
268 char buf[MAXPATHLEN+512];
271 va_start(ap, format);
272 len = vsnprintf(buf, sizeof buf, format, ap);
275 /* Deal with buffer overruns. Instead of panicking, just
276 * truncate the resulting string. (Note that configure ensures
277 * that we have a vsnprintf() that doesn't ever return -1.) */
278 if (len > sizeof buf - 1) {
279 const char ellipsis[] = "[...]";
281 /* Reset length, and zero-terminate the end of our buffer */
282 len = sizeof buf - 1;
285 /* Copy the ellipsis to the end of the string, but give
286 * us one extra character:
288 * v--- null byte at buf[sizeof buf - 1]
290 * -> abcd[...]00 <-- now two null bytes at end
292 * If the input format string has a trailing newline,
293 * we copy it into that extra null; if it doesn't, well,
294 * all we lose is one byte. */
295 strncpy(buf+len-sizeof ellipsis, ellipsis, sizeof ellipsis);
296 if (format[strlen(format)-1] == '\n') {
301 rwrite(code, buf, len);
305 /* This is like rprintf, but it also tries to print some
306 * representation of the error code. Normally errcode = errno.
308 * Unlike rprintf, this always adds a newline and there should not be
309 * one in the format string.
311 * Note that since strerror might involve dynamically loading a
312 * message catalog we need to call it once before chroot-ing. */
313 void rsyserr(enum logcode code, int errcode, const char *format, ...)
316 char buf[MAXPATHLEN+512];
319 strcpy(buf, RSYNC_NAME ": ");
320 len = (sizeof RSYNC_NAME ": ") - 1;
322 va_start(ap, format);
323 len += vsnprintf(buf + len, sizeof buf - len, format, ap);
326 if (len < sizeof buf) {
327 len += snprintf(buf + len, sizeof buf - len,
328 ": %s (%d)\n", strerror(errcode), errcode);
330 if (len >= sizeof buf)
331 exit_cleanup(RERR_MESSAGEIO);
333 rwrite(code, buf, len);
338 void rflush(enum logcode code)
350 if (code == FERROR) {
361 if (!f) exit_cleanup(RERR_MESSAGEIO);
367 /* a generic logging routine for send/recv, with parameter
369 static void log_formatted(enum logcode code, char *format, char *op,
370 struct file_struct *file, struct stats *initial_stats,
371 int iflags, char *hlink)
373 char buf[MAXPATHLEN+1024], buf2[MAXPATHLEN], fmt[32];
380 /* We expand % codes one by one in place in buf. We don't
381 * copy in the terminating null of the inserted strings, but
382 * rather keep going until we reach the null of the format. */
383 total = strlcpy(buf, format, sizeof buf);
384 if (total > MAXPATHLEN) {
385 rprintf(FERROR, "log-format string is WAY too long!\n");
386 exit_cleanup(RERR_MESSAGEIO);
391 for (p = buf; (p = strchr(p, '%')) != NULL; ) {
396 while (isdigit(*(uchar*)p) && n - fmt < (int)(sizeof fmt) - 8)
404 case 'h': if (am_daemon) n = client_name(0); break;
405 case 'a': if (am_daemon) n = client_addr(0); break;
407 strlcat(fmt, ".0f", sizeof fmt);
408 snprintf(buf2, sizeof buf2, fmt,
409 (double)file->length);
413 strlcat(fmt, "ld", sizeof fmt);
414 snprintf(buf2, sizeof buf2, fmt,
418 case 'o': n = op; break;
420 n = safe_fname(f_name(file));
421 if (am_sender && file->dir.root) {
422 pathjoin(buf2, sizeof buf2,
424 /* The buffer from safe_fname() has more
425 * room than MAXPATHLEN, so this is safe. */
436 n = safe_fname(f_name(file));
437 if (S_ISDIR(file->mode)) {
438 /* The buffer from safe_fname() has more
439 * room than MAXPATHLEN, so this is safe. */
444 if (hlink && *hlink) {
445 n = safe_fname(hlink);
446 strcpy(buf2, " => ");
447 } else if (S_ISLNK(file->mode) && file->u.link) {
448 n = safe_fname(file->u.link);
449 strcpy(buf2, " -> ");
456 strlcat(fmt, "s", sizeof fmt);
457 snprintf(buf2 + 4, sizeof buf2 - 4, fmt, n);
460 case 'm': n = lp_name(module_id); break;
461 case 't': n = timestring(time(NULL)); break;
462 case 'P': n = lp_path(module_id); break;
463 case 'u': n = auth_user; break;
466 b = stats.total_written -
467 initial_stats->total_written;
469 b = stats.total_read -
470 initial_stats->total_read;
472 strlcat(fmt, ".0f", sizeof fmt);
473 snprintf(buf2, sizeof buf2, fmt, (double)b);
478 b = stats.total_written -
479 initial_stats->total_written;
481 b = stats.total_read -
482 initial_stats->total_read;
484 strlcat(fmt, ".0f", sizeof fmt);
485 snprintf(buf2, sizeof buf2, fmt, (double)b);
489 if (iflags & ITEM_DELETED) {
493 n = buf2 + MAXPATHLEN - 32;
494 n[0] = iflags & ITEM_LOCAL_CHANGE
495 ? iflags & ITEM_XNAME_FOLLOWS ? 'h' : 'c'
496 : !(iflags & ITEM_TRANSFER) ? '.'
497 : !local_server && *op == 's' ? '<' : '>';
498 n[1] = S_ISDIR(file->mode) ? 'd'
499 : IS_DEVICE(file->mode) ? 'D'
500 : S_ISLNK(file->mode) ? 'L' : 'f';
501 n[2] = !(iflags & ITEM_REPORT_CHECKSUM) ? '.' : 'c';
502 n[3] = !(iflags & ITEM_REPORT_SIZE) ? '.' : 's';
503 n[4] = !(iflags & ITEM_REPORT_TIME) ? '.'
504 : !preserve_times || IS_DEVICE(file->mode)
505 || S_ISLNK(file->mode) ? 'T' : 't';
506 n[5] = !(iflags & ITEM_REPORT_PERMS) ? '.' : 'p';
507 n[6] = !(iflags & ITEM_REPORT_OWNER) ? '.' : 'o';
508 n[7] = !(iflags & ITEM_REPORT_GROUP) ? '.' : 'g';
509 n[8] = !(iflags & ITEM_REPORT_XATTRS) ? '.' : 'a';
512 if (iflags & (ITEM_IS_NEW|ITEM_MISSING_DATA)) {
513 char ch = iflags & ITEM_IS_NEW ? '+' : '?';
515 for (i = 2; n[i]; i++)
517 } else if (n[0] == '.' || n[0] == 'h') {
519 for (i = 2; n[i]; i++) {
524 for (i = 2; n[i]; i++)
531 /* "n" is the string to be inserted in place of this % code. */
534 if (n != buf2 && fmt[1]) {
535 strlcat(fmt, "s", sizeof fmt);
536 snprintf(buf2, sizeof buf2, fmt, n);
541 /* Subtract the length of the escape from the string's size. */
544 if (len + total >= (size_t)sizeof buf) {
546 "buffer overflow expanding %%%c -- exiting\n",
548 exit_cleanup(RERR_MESSAGEIO);
551 /* Shuffle the rest of the string along to make space for n */
552 if (len != (size_t)(p - s + 1))
553 memmove(s + len, p + 1, total - (s - buf) + 1);
556 /* Insert the contents of string "n", but NOT its null. */
560 /* Skip over inserted string; continue looking */
564 rwrite(code, buf, total);
567 /* Return 1 if the format escape is in the log-format string (e.g. look for
568 * the 'b' in the "%9b" format escape). */
569 int log_format_has(const char *format, char esc)
576 for (p = format; (p = strchr(p, '%')) != NULL; ) {
579 while (isdigit(*(uchar*)p))
589 /* log the transfer of a file */
590 void log_item(struct file_struct *file, struct stats *initial_stats,
591 int iflags, char *hlink)
593 char *s_or_r = am_sender ? "send" : "recv";
595 if (lp_transfer_logging(module_id)) {
596 log_formatted(FLOG, lp_log_format(module_id), s_or_r,
597 file, initial_stats, iflags, hlink);
598 } else if (log_format && !am_server) {
599 log_formatted(FINFO, log_format, s_or_r,
600 file, initial_stats, iflags, hlink);
604 void maybe_log_item(struct file_struct *file, int iflags, int itemizing,
607 int significant_flags = iflags & SIGNIFICANT_ITEM_FLAGS;
608 int see_item = itemizing && (significant_flags || *buf || verbose > 1);
609 int local_change = iflags & ITEM_LOCAL_CHANGE
610 && (!(iflags & ITEM_XNAME_FOLLOWS) || significant_flags);
612 if (am_daemon && !dry_run && see_item)
613 log_item(file, &stats, iflags, buf);
614 } else if (see_item || local_change || *buf
615 || (S_ISDIR(file->mode) && significant_flags))
616 log_item(file, &stats, iflags, buf);
619 void log_delete(char *fname, int mode)
621 static struct file_struct file;
622 int len = strlen(fname);
626 file.basename = fname;
628 if (!verbose && !log_format)
630 else if (am_server && protocol_version >= 29 && len < MAXPATHLEN) {
632 len++; /* directories include trailing null */
633 send_msg(MSG_DELETED, fname, len);
635 fmt = log_format_has_o_or_i ? log_format : "deleting %n";
636 log_formatted(FCLIENT, fmt, "del.", &file, &stats,
640 if (!am_daemon || dry_run || !lp_transfer_logging(module_id))
643 fmt = daemon_log_format_has_o_or_i ? lp_log_format(module_id) : "deleting %n";
644 log_formatted(FLOG, fmt, "del.", &file, &stats, ITEM_DELETED, NULL);
649 * Called when the transfer is interrupted for some reason.
651 * Code is one of the RERR_* codes from errcode.h, or terminating
654 void log_exit(int code, const char *file, int line)
657 rprintf(FLOG,"sent %.0f bytes received %.0f bytes total size %.0f\n",
658 (double)stats.total_written,
659 (double)stats.total_read,
660 (double)stats.total_size);
664 name = rerr_name(code);
666 name = "unexplained error";
668 /* VANISHED is not an error, only a warning */
669 if (code == RERR_VANISHED) {
670 rprintf(FINFO, "rsync warning: %s (code %d) at %s(%d)\n",
671 name, code, file, line);
673 rprintf(FERROR, "rsync error: %s (code %d) at %s(%d)\n",
674 name, code, file, line);