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_i;
42 extern int log_format_has_o_or_i;
43 extern int daemon_log_format_has_o_or_i;
44 extern char *auth_user;
45 extern char *log_format;
47 static int log_initialised;
48 static int logfile_was_closed;
49 static char *logfname;
53 int log_got_error = 0;
58 } const rerr_names[] = {
59 { RERR_SYNTAX , "syntax or usage error" },
60 { RERR_PROTOCOL , "protocol incompatibility" },
61 { RERR_FILESELECT , "errors selecting input/output files, dirs" },
62 { RERR_UNSUPPORTED, "requested action not supported" },
63 { RERR_STARTCLIENT, "error starting client-server protocol" },
64 { RERR_SOCKETIO , "error in socket IO" },
65 { RERR_FILEIO , "error in file IO" },
66 { RERR_STREAMIO , "error in rsync protocol data stream" },
67 { RERR_MESSAGEIO , "errors with program diagnostics" },
68 { RERR_IPC , "error in IPC code" },
69 { RERR_CRASHED , "sibling process crashed" },
70 { RERR_TERMINATED , "sibling process terminated abnormally" },
71 { RERR_SIGNAL1 , "received SIGUSR1" },
72 { RERR_SIGNAL , "received SIGINT, SIGTERM, or SIGHUP" },
73 { RERR_WAITCHILD , "waitpid() failed" },
74 { RERR_MALLOC , "error allocating core memory buffers" },
75 { RERR_PARTIAL , "some files could not be transferred" },
76 { RERR_VANISHED , "some files vanished before they could be transferred" },
77 { RERR_TIMEOUT , "timeout in data send/receive" },
78 { RERR_CMD_FAILED , "remote shell failed" },
79 { RERR_CMD_KILLED , "remote shell killed" },
80 { RERR_CMD_RUN , "remote command could not be run" },
81 { RERR_CMD_NOTFOUND,"remote command not found" },
82 { RERR_DEL_LIMIT , "the --max-delete limit stopped deletions" },
89 * Map from rsync error code to name, or return NULL.
91 static char const *rerr_name(int code)
94 for (i = 0; rerr_names[i].name; i++) {
95 if (rerr_names[i].code == code)
96 return rerr_names[i].name;
102 static void logit(int priority, char *buf)
104 if (logfile_was_closed)
107 fprintf(logfile,"%s [%d] %s",
108 timestring(time(NULL)), (int)getpid(), buf);
111 syslog(priority, "%s", buf);
115 static void syslog_init()
117 static int been_here = 0;
118 int options = LOG_PID;
125 options |= LOG_NDELAY;
129 openlog("rsyncd", options, lp_syslog_facility());
131 openlog("rsyncd", options);
135 logit(LOG_INFO, "rsyncd started\n");
139 static void logfile_open(void)
141 extern int orig_umask;
142 int 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");
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 */
169 /* optionally use a log file instead of syslog */
170 logfname = lp_log_file();
171 if (logfname && *logfname)
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 /* this is the underlying (unformatted) rsync debugging function. Call
195 * it with FINFO, FERROR or FLOG */
196 void rwrite(enum logcode code, char *buf, int len)
199 /* recursion can happen with certain fatal conditions */
201 if (quiet && code == FINFO)
205 exit_cleanup(RERR_MESSAGEIO);
209 if (am_server && msg_fd_out >= 0) {
210 /* Pass the message to our sibling. */
211 send_msg((enum msgcode)code, buf, len);
217 else if (am_daemon) {
220 int priority = code == FERROR ? LOG_WARNING : LOG_INFO;
225 if (!log_initialised)
227 strlcpy(msg, buf, MIN((int)sizeof msg, len + 1));
228 logit(priority, msg);
231 if (code == FLOG || !am_server)
233 } else if (code == FLOG)
237 /* Pass the message to the non-server side. */
238 if (io_multiplex_write((enum msgcode)code, buf, len))
241 /* TODO: can we send the error to the user somehow? */
246 if (code == FERROR) {
252 f = am_server ? stderr : stdout;
255 exit_cleanup(RERR_MESSAGEIO);
257 if (fwrite(buf, len, 1, f) != 1)
258 exit_cleanup(RERR_MESSAGEIO);
260 if (buf[len-1] == '\r' || buf[len-1] == '\n')
265 /* This is the rsync debugging function. Call it with FINFO, FERROR or
267 void rprintf(enum logcode code, const char *format, ...)
270 char buf[BIGPATHBUFLEN];
273 va_start(ap, format);
274 len = vsnprintf(buf, sizeof buf, format, ap);
277 /* Deal with buffer overruns. Instead of panicking, just
278 * truncate the resulting string. (Note that configure ensures
279 * that we have a vsnprintf() that doesn't ever return -1.) */
280 if (len > sizeof buf - 1) {
281 static const char ellipsis[] = "[...]";
283 /* Reset length, and zero-terminate the end of our buffer */
284 len = sizeof buf - 1;
287 /* Copy the ellipsis to the end of the string, but give
288 * us one extra character:
290 * v--- null byte at buf[sizeof buf - 1]
292 * -> abcd[...]00 <-- now two null bytes at end
294 * If the input format string has a trailing newline,
295 * we copy it into that extra null; if it doesn't, well,
296 * all we lose is one byte. */
297 memcpy(buf+len-sizeof ellipsis, ellipsis, sizeof ellipsis);
298 if (format[strlen(format)-1] == '\n') {
303 rwrite(code, buf, len);
307 /* This is like rprintf, but it also tries to print some
308 * representation of the error code. Normally errcode = errno.
310 * Unlike rprintf, this always adds a newline and there should not be
311 * one in the format string.
313 * Note that since strerror might involve dynamically loading a
314 * message catalog we need to call it once before chroot-ing. */
315 void rsyserr(enum logcode code, int errcode, const char *format, ...)
318 char buf[BIGPATHBUFLEN];
321 strcpy(buf, RSYNC_NAME ": ");
322 len = (sizeof RSYNC_NAME ": ") - 1;
324 va_start(ap, format);
325 len += vsnprintf(buf + len, sizeof buf - len, format, ap);
328 if (len < sizeof buf) {
329 len += snprintf(buf + len, sizeof buf - len,
330 ": %s (%d)\n", strerror(errcode), errcode);
332 if (len >= sizeof buf)
333 exit_cleanup(RERR_MESSAGEIO);
335 rwrite(code, buf, len);
340 void rflush(enum logcode code)
352 if (code == FERROR) {
363 if (!f) exit_cleanup(RERR_MESSAGEIO);
369 /* a generic logging routine for send/recv, with parameter
371 static void log_formatted(enum logcode code, char *format, char *op,
372 struct file_struct *file, struct stats *initial_stats,
373 int iflags, char *hlink)
375 char buf[MAXPATHLEN+1024], buf2[MAXPATHLEN], fmt[32];
382 /* We expand % codes one by one in place in buf. We don't
383 * copy in the terminating null of the inserted strings, but
384 * rather keep going until we reach the null of the format. */
385 total = strlcpy(buf, format, sizeof buf);
386 if (total > MAXPATHLEN) {
387 rprintf(FERROR, "log-format string is WAY too long!\n");
388 exit_cleanup(RERR_MESSAGEIO);
393 for (p = buf; (p = strchr(p, '%')) != NULL; ) {
398 while (isdigit(*(uchar*)p) && n - fmt < (int)(sizeof fmt) - 8)
415 strlcat(fmt, ".0f", sizeof fmt);
416 snprintf(buf2, sizeof buf2, fmt,
417 (double)file->length);
421 strlcat(fmt, "ld", sizeof fmt);
422 snprintf(buf2, sizeof buf2, fmt,
430 n = safe_fname(f_name(file));
431 if (am_sender && file->dir.root) {
432 pathjoin(buf2, sizeof buf2,
434 /* The buffer from safe_fname() has more
435 * room than MAXPATHLEN, so this is safe. */
446 n = safe_fname(f_name(file));
447 if (S_ISDIR(file->mode)) {
448 /* The buffer from safe_fname() has more
449 * room than MAXPATHLEN, so this is safe. */
454 if (hlink && *hlink) {
455 n = safe_fname(hlink);
456 strcpy(buf2, " => ");
457 } else if (S_ISLNK(file->mode) && file->u.link) {
458 n = safe_fname(file->u.link);
459 strcpy(buf2, " -> ");
466 strlcat(fmt, "s", sizeof fmt);
467 snprintf(buf2 + 4, sizeof buf2 - 4, fmt, n);
471 n = lp_name(module_id);
474 n = timestring(time(NULL));
477 n = lp_path(module_id);
484 b = stats.total_written -
485 initial_stats->total_written;
487 b = stats.total_read -
488 initial_stats->total_read;
490 strlcat(fmt, ".0f", sizeof fmt);
491 snprintf(buf2, sizeof buf2, fmt, (double)b);
496 b = stats.total_written -
497 initial_stats->total_written;
499 b = stats.total_read -
500 initial_stats->total_read;
502 strlcat(fmt, ".0f", sizeof fmt);
503 snprintf(buf2, sizeof buf2, fmt, (double)b);
507 if (iflags & ITEM_DELETED) {
511 n = buf2 + MAXPATHLEN - 32;
512 n[0] = iflags & ITEM_LOCAL_CHANGE
513 ? iflags & ITEM_XNAME_FOLLOWS ? 'h' : 'c'
514 : !(iflags & ITEM_TRANSFER) ? '.'
515 : !local_server && *op == 's' ? '<' : '>';
516 n[1] = S_ISDIR(file->mode) ? 'd'
517 : IS_DEVICE(file->mode) ? 'D'
518 : S_ISLNK(file->mode) ? 'L' : 'f';
519 n[2] = !(iflags & ITEM_REPORT_CHECKSUM) ? '.' : 'c';
520 n[3] = !(iflags & ITEM_REPORT_SIZE) ? '.' : 's';
521 n[4] = !(iflags & ITEM_REPORT_TIME) ? '.'
522 : !preserve_times || IS_DEVICE(file->mode)
523 || S_ISLNK(file->mode) ? 'T' : 't';
524 n[5] = !(iflags & ITEM_REPORT_PERMS) ? '.' : 'p';
525 n[6] = !(iflags & ITEM_REPORT_OWNER) ? '.' : 'o';
526 n[7] = !(iflags & ITEM_REPORT_GROUP) ? '.' : 'g';
527 n[8] = !(iflags & ITEM_REPORT_XATTRS) ? '.' : 'a';
530 if (iflags & (ITEM_IS_NEW|ITEM_MISSING_DATA)) {
531 char ch = iflags & ITEM_IS_NEW ? '+' : '?';
533 for (i = 2; n[i]; i++)
535 } else if (n[0] == '.' || n[0] == 'h'
536 || (n[0] == 'c' && n[1] == 'f')) {
538 for (i = 2; n[i]; i++) {
543 for (i = 2; n[i]; i++)
550 /* "n" is the string to be inserted in place of this % code. */
553 if (n != buf2 && fmt[1]) {
554 strlcat(fmt, "s", sizeof fmt);
555 snprintf(buf2, sizeof buf2, fmt, n);
560 /* Subtract the length of the escape from the string's size. */
563 if (len + total >= (size_t)sizeof buf) {
565 "buffer overflow expanding %%%c -- exiting\n",
567 exit_cleanup(RERR_MESSAGEIO);
570 /* Shuffle the rest of the string along to make space for n */
571 if (len != (size_t)(p - s + 1))
572 memmove(s + len, p + 1, total - (s - buf) + 1);
575 /* Insert the contents of string "n", but NOT its null. */
579 /* Skip over inserted string; continue looking */
583 rwrite(code, buf, total);
586 /* Return 1 if the format escape is in the log-format string (e.g. look for
587 * the 'b' in the "%9b" format escape). */
588 int log_format_has(const char *format, char esc)
595 for (p = format; (p = strchr(p, '%')) != NULL; ) {
598 while (isdigit(*(uchar*)p))
608 /* log the transfer of a file */
609 void log_item(struct file_struct *file, struct stats *initial_stats,
610 int iflags, char *hlink)
612 char *s_or_r = am_sender ? "send" : "recv";
614 if (lp_transfer_logging(module_id)) {
615 log_formatted(FLOG, lp_log_format(module_id), s_or_r,
616 file, initial_stats, iflags, hlink);
617 } else if (log_format && !am_server) {
618 log_formatted(FINFO, log_format, s_or_r,
619 file, initial_stats, iflags, hlink);
623 void maybe_log_item(struct file_struct *file, int iflags, int itemizing,
626 int significant_flags = iflags & SIGNIFICANT_ITEM_FLAGS;
627 int see_item = itemizing && (significant_flags || *buf
628 || (verbose > 1 && log_format_has_i));
629 int local_change = iflags & ITEM_LOCAL_CHANGE && significant_flags;
631 if (am_daemon && !dry_run && see_item)
632 log_item(file, &stats, iflags, buf);
633 } else if (see_item || local_change || *buf
634 || (S_ISDIR(file->mode) && significant_flags))
635 log_item(file, &stats, iflags, buf);
638 void log_delete(char *fname, int mode)
640 static struct file_struct file;
641 int len = strlen(fname);
645 file.basename = fname;
647 if (!verbose && !log_format)
649 else if (am_server && protocol_version >= 29 && len < MAXPATHLEN) {
651 len++; /* directories include trailing null */
652 send_msg(MSG_DELETED, fname, len);
654 fmt = log_format_has_o_or_i ? log_format : "deleting %n";
655 log_formatted(FCLIENT, fmt, "del.", &file, &stats,
659 if (!am_daemon || dry_run || !lp_transfer_logging(module_id))
662 fmt = daemon_log_format_has_o_or_i ? lp_log_format(module_id) : "deleting %n";
663 log_formatted(FLOG, fmt, "del.", &file, &stats, ITEM_DELETED, NULL);
668 * Called when the transfer is interrupted for some reason.
670 * Code is one of the RERR_* codes from errcode.h, or terminating
673 void log_exit(int code, const char *file, int line)
676 rprintf(FLOG,"sent %.0f bytes received %.0f bytes total size %.0f\n",
677 (double)stats.total_written,
678 (double)stats.total_read,
679 (double)stats.total_size);
683 name = rerr_name(code);
685 name = "unexplained error";
687 /* VANISHED is not an error, only a warning */
688 if (code == RERR_VANISHED) {
689 rprintf(FINFO, "rsync warning: %s (code %d) at %s(%d)\n",
690 name, code, file, line);
692 rprintf(FERROR, "rsync error: %s (code %d) at %s(%d)\n",
693 name, code, file, line);