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_SIGNAL , "received SIGINT, SIGTERM, SIGHUP, or SIGUSR1" },
72 { RERR_WAITCHILD , "waitpid() failed" },
73 { RERR_MALLOC , "error allocating core memory buffers" },
74 { RERR_PARTIAL , "some files could not be transferred" },
75 { RERR_VANISHED , "some files vanished before they could be transferred" },
76 { RERR_TIMEOUT , "timeout in data send/receive" },
77 { RERR_CMD_FAILED , "remote shell failed" },
78 { RERR_CMD_KILLED , "remote shell killed" },
79 { RERR_CMD_RUN , "remote command could not be run" },
80 { RERR_CMD_NOTFOUND,"remote command not found" },
81 { RERR_DEL_LIMIT , "the --max-delete limit stopped deletions" },
88 * Map from rsync error code to name, or return NULL.
90 static char const *rerr_name(int code)
93 for (i = 0; rerr_names[i].name; i++) {
94 if (rerr_names[i].code == code)
95 return rerr_names[i].name;
101 static void logit(int priority, char *buf)
103 if (logfile_was_closed)
106 fprintf(logfile,"%s [%d] %s",
107 timestring(time(NULL)), (int)getpid(), buf);
110 syslog(priority, "%s", buf);
114 static void syslog_init()
116 static int been_here = 0;
117 int options = LOG_PID;
124 options |= LOG_NDELAY;
128 openlog("rsyncd", options, lp_syslog_facility());
130 openlog("rsyncd", options);
134 logit(LOG_INFO, "rsyncd started\n");
138 static void logfile_open(void)
140 extern int orig_umask;
141 int old_umask = umask(022 | orig_umask);
142 logfile = fopen(logfname, "a");
145 int fopen_errno = errno;
146 /* Rsync falls back to using syslog on failure. */
148 rsyserr(FERROR, fopen_errno,
149 "failed to open log-file %s", logfname);
150 rprintf(FINFO, "Ignoring \"log file\" setting.\n");
162 /* this looks pointless, but it is needed in order for the
163 * C library on some systems to fetch the timezone info
164 * before the chroot */
168 /* optionally use a log file instead of syslog */
169 logfname = lp_log_file();
170 if (logfname && *logfname)
176 void logfile_close(void)
179 logfile_was_closed = 1;
185 void logfile_reopen(void)
187 if (logfile_was_closed) {
188 logfile_was_closed = 0;
193 /* this is the underlying (unformatted) rsync debugging function. Call
194 * it with FINFO, FERROR or FLOG */
195 void rwrite(enum logcode code, char *buf, int len)
198 /* recursion can happen with certain fatal conditions */
200 if (quiet && code == FINFO)
204 exit_cleanup(RERR_MESSAGEIO);
208 if (am_server && msg_fd_out >= 0) {
209 /* Pass the message to our sibling. */
210 send_msg((enum msgcode)code, buf, len);
216 else if (am_daemon) {
219 int priority = code == FERROR ? LOG_WARNING : LOG_INFO;
224 if (!log_initialised)
226 strlcpy(msg, buf, MIN((int)sizeof msg, len + 1));
227 logit(priority, msg);
230 if (code == FLOG || !am_server)
232 } else if (code == FLOG)
236 /* Pass the message to the non-server side. */
237 if (io_multiplex_write((enum msgcode)code, buf, len))
240 /* TODO: can we send the error to the user somehow? */
245 if (code == FERROR) {
251 f = am_server ? stderr : stdout;
254 exit_cleanup(RERR_MESSAGEIO);
256 if (fwrite(buf, len, 1, f) != 1)
257 exit_cleanup(RERR_MESSAGEIO);
259 if (buf[len-1] == '\r' || buf[len-1] == '\n')
264 /* This is the rsync debugging function. Call it with FINFO, FERROR or
266 void rprintf(enum logcode code, const char *format, ...)
269 char buf[MAXPATHLEN+512];
272 va_start(ap, format);
273 len = vsnprintf(buf, sizeof buf, format, ap);
276 /* Deal with buffer overruns. Instead of panicking, just
277 * truncate the resulting string. (Note that configure ensures
278 * that we have a vsnprintf() that doesn't ever return -1.) */
279 if (len > sizeof buf - 1) {
280 const char ellipsis[] = "[...]";
282 /* Reset length, and zero-terminate the end of our buffer */
283 len = sizeof buf - 1;
286 /* Copy the ellipsis to the end of the string, but give
287 * us one extra character:
289 * v--- null byte at buf[sizeof buf - 1]
291 * -> abcd[...]00 <-- now two null bytes at end
293 * If the input format string has a trailing newline,
294 * we copy it into that extra null; if it doesn't, well,
295 * all we lose is one byte. */
296 strncpy(buf+len-sizeof ellipsis, ellipsis, sizeof ellipsis);
297 if (format[strlen(format)-1] == '\n') {
302 rwrite(code, buf, len);
306 /* This is like rprintf, but it also tries to print some
307 * representation of the error code. Normally errcode = errno.
309 * Unlike rprintf, this always adds a newline and there should not be
310 * one in the format string.
312 * Note that since strerror might involve dynamically loading a
313 * message catalog we need to call it once before chroot-ing. */
314 void rsyserr(enum logcode code, int errcode, const char *format, ...)
317 char buf[MAXPATHLEN+512];
320 strcpy(buf, RSYNC_NAME ": ");
321 len = (sizeof RSYNC_NAME ": ") - 1;
323 va_start(ap, format);
324 len += vsnprintf(buf + len, sizeof buf - len, format, ap);
327 if (len < sizeof buf) {
328 len += snprintf(buf + len, sizeof buf - len,
329 ": %s (%d)\n", strerror(errcode), errcode);
331 if (len >= sizeof buf)
332 exit_cleanup(RERR_MESSAGEIO);
334 rwrite(code, buf, len);
339 void rflush(enum logcode code)
351 if (code == FERROR) {
362 if (!f) exit_cleanup(RERR_MESSAGEIO);
368 /* a generic logging routine for send/recv, with parameter
370 static void log_formatted(enum logcode code, char *format, char *op,
371 struct file_struct *file, struct stats *initial_stats,
372 int iflags, char *hlink)
374 char buf[MAXPATHLEN+1024], buf2[MAXPATHLEN], fmt[32];
381 /* We expand % codes one by one in place in buf. We don't
382 * copy in the terminating null of the inserted strings, but
383 * rather keep going until we reach the null of the format. */
384 total = strlcpy(buf, format, sizeof buf);
385 if (total > MAXPATHLEN) {
386 rprintf(FERROR, "log-format string is WAY too long!\n");
387 exit_cleanup(RERR_MESSAGEIO);
392 for (p = buf; (p = strchr(p, '%')) != NULL; ) {
397 while (isdigit(*(uchar*)p) && n - fmt < (int)(sizeof fmt) - 8)
405 case 'h': if (am_daemon) n = client_name(0); break;
406 case 'a': if (am_daemon) n = client_addr(0); break;
408 strlcat(fmt, ".0f", sizeof fmt);
409 snprintf(buf2, sizeof buf2, fmt,
410 (double)file->length);
414 strlcat(fmt, "ld", sizeof fmt);
415 snprintf(buf2, sizeof buf2, fmt,
419 case 'o': n = op; break;
421 n = safe_fname(f_name(file));
422 if (am_sender && file->dir.root) {
423 pathjoin(buf2, sizeof buf2,
425 /* The buffer from safe_fname() has more
426 * room than MAXPATHLEN, so this is safe. */
437 n = safe_fname(f_name(file));
438 if (S_ISDIR(file->mode)) {
439 /* The buffer from safe_fname() has more
440 * room than MAXPATHLEN, so this is safe. */
445 if (hlink && *hlink) {
446 n = safe_fname(hlink);
447 strcpy(buf2, " => ");
448 } else if (S_ISLNK(file->mode) && file->u.link) {
449 n = safe_fname(file->u.link);
450 strcpy(buf2, " -> ");
457 strlcat(fmt, "s", sizeof fmt);
458 snprintf(buf2 + 4, sizeof buf2 - 4, fmt, n);
461 case 'm': n = lp_name(module_id); break;
462 case 't': n = timestring(time(NULL)); break;
463 case 'P': n = lp_path(module_id); break;
464 case 'u': n = auth_user; break;
467 b = stats.total_written -
468 initial_stats->total_written;
470 b = stats.total_read -
471 initial_stats->total_read;
473 strlcat(fmt, ".0f", sizeof fmt);
474 snprintf(buf2, sizeof buf2, fmt, (double)b);
479 b = stats.total_written -
480 initial_stats->total_written;
482 b = stats.total_read -
483 initial_stats->total_read;
485 strlcat(fmt, ".0f", sizeof fmt);
486 snprintf(buf2, sizeof buf2, fmt, (double)b);
490 if (iflags & ITEM_DELETED) {
494 n = buf2 + MAXPATHLEN - 32;
495 n[0] = iflags & ITEM_LOCAL_CHANGE
496 ? iflags & ITEM_XNAME_FOLLOWS ? 'h' : 'c'
497 : !(iflags & ITEM_TRANSFER) ? '.'
498 : !local_server && *op == 's' ? '<' : '>';
499 n[1] = S_ISDIR(file->mode) ? 'd'
500 : IS_DEVICE(file->mode) ? 'D'
501 : S_ISLNK(file->mode) ? 'L' : 'f';
502 n[2] = !(iflags & ITEM_REPORT_CHECKSUM) ? '.' : 'c';
503 n[3] = !(iflags & ITEM_REPORT_SIZE) ? '.' : 's';
504 n[4] = !(iflags & ITEM_REPORT_TIME) ? '.'
505 : !preserve_times || IS_DEVICE(file->mode)
506 || S_ISLNK(file->mode) ? 'T' : 't';
507 n[5] = !(iflags & ITEM_REPORT_PERMS) ? '.' : 'p';
508 n[6] = !(iflags & ITEM_REPORT_OWNER) ? '.' : 'o';
509 n[7] = !(iflags & ITEM_REPORT_GROUP) ? '.' : 'g';
510 n[8] = !(iflags & ITEM_REPORT_XATTRS) ? '.' : 'a';
513 if (iflags & (ITEM_IS_NEW|ITEM_MISSING_DATA)) {
514 char ch = iflags & ITEM_IS_NEW ? '+' : '?';
516 for (i = 2; n[i]; i++)
518 } else if (n[0] == '.' || n[0] == 'h'
519 || (n[0] == 'c' && n[1] == 'f')) {
521 for (i = 2; n[i]; i++) {
526 for (i = 2; n[i]; i++)
533 /* "n" is the string to be inserted in place of this % code. */
536 if (n != buf2 && fmt[1]) {
537 strlcat(fmt, "s", sizeof fmt);
538 snprintf(buf2, sizeof buf2, fmt, n);
543 /* Subtract the length of the escape from the string's size. */
546 if (len + total >= (size_t)sizeof buf) {
548 "buffer overflow expanding %%%c -- exiting\n",
550 exit_cleanup(RERR_MESSAGEIO);
553 /* Shuffle the rest of the string along to make space for n */
554 if (len != (size_t)(p - s + 1))
555 memmove(s + len, p + 1, total - (s - buf) + 1);
558 /* Insert the contents of string "n", but NOT its null. */
562 /* Skip over inserted string; continue looking */
566 rwrite(code, buf, total);
569 /* Return 1 if the format escape is in the log-format string (e.g. look for
570 * the 'b' in the "%9b" format escape). */
571 int log_format_has(const char *format, char esc)
578 for (p = format; (p = strchr(p, '%')) != NULL; ) {
581 while (isdigit(*(uchar*)p))
591 /* log the transfer of a file */
592 void log_item(struct file_struct *file, struct stats *initial_stats,
593 int iflags, char *hlink)
595 char *s_or_r = am_sender ? "send" : "recv";
597 if (lp_transfer_logging(module_id)) {
598 log_formatted(FLOG, lp_log_format(module_id), s_or_r,
599 file, initial_stats, iflags, hlink);
600 } else if (log_format && !am_server) {
601 log_formatted(FINFO, log_format, s_or_r,
602 file, initial_stats, iflags, hlink);
606 void maybe_log_item(struct file_struct *file, int iflags, int itemizing,
609 int significant_flags = iflags & SIGNIFICANT_ITEM_FLAGS;
610 int see_item = itemizing && (significant_flags || *buf
611 || (verbose > 1 && log_format_has_i));
612 int local_change = iflags & ITEM_LOCAL_CHANGE && significant_flags;
614 if (am_daemon && !dry_run && see_item)
615 log_item(file, &stats, iflags, buf);
616 } else if (see_item || local_change || *buf
617 || (S_ISDIR(file->mode) && significant_flags))
618 log_item(file, &stats, iflags, buf);
621 void log_delete(char *fname, int mode)
623 static struct file_struct file;
624 int len = strlen(fname);
628 file.basename = fname;
630 if (!verbose && !log_format)
632 else if (am_server && protocol_version >= 29 && len < MAXPATHLEN) {
634 len++; /* directories include trailing null */
635 send_msg(MSG_DELETED, fname, len);
637 fmt = log_format_has_o_or_i ? log_format : "deleting %n";
638 log_formatted(FCLIENT, fmt, "del.", &file, &stats,
642 if (!am_daemon || dry_run || !lp_transfer_logging(module_id))
645 fmt = daemon_log_format_has_o_or_i ? lp_log_format(module_id) : "deleting %n";
646 log_formatted(FLOG, fmt, "del.", &file, &stats, ITEM_DELETED, NULL);
651 * Called when the transfer is interrupted for some reason.
653 * Code is one of the RERR_* codes from errcode.h, or terminating
656 void log_exit(int code, const char *file, int line)
659 rprintf(FLOG,"sent %.0f bytes received %.0f bytes total size %.0f\n",
660 (double)stats.total_written,
661 (double)stats.total_read,
662 (double)stats.total_size);
666 name = rerr_name(code);
668 name = "unexplained error";
670 /* VANISHED is not an error, only a warning */
671 if (code == RERR_VANISHED) {
672 rprintf(FINFO, "rsync warning: %s (code %d) at %s(%d)\n",
673 name, code, file, line);
675 rprintf(FERROR, "rsync error: %s (code %d) at %s(%d)\n",
676 name, code, file, line);