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.
37 extern int msg_fd_out;
38 extern int protocol_version;
39 extern int preserve_times;
40 extern int log_format_has_o_or_i;
41 extern int daemon_log_format_has_o_or_i;
42 extern char *auth_user;
43 extern char *log_format;
45 static int log_initialised;
46 static char *logfname;
50 int log_got_error = 0;
55 } const rerr_names[] = {
56 { RERR_SYNTAX , "syntax or usage error" },
57 { RERR_PROTOCOL , "protocol incompatibility" },
58 { RERR_FILESELECT , "errors selecting input/output files, dirs" },
59 { RERR_UNSUPPORTED, "requested action not supported" },
60 { RERR_STARTCLIENT, "error starting client-server protocol" },
61 { RERR_LOG_FAILURE, "daemon unable to append to log-file" },
62 { RERR_SOCKETIO , "error in socket IO" },
63 { RERR_FILEIO , "error in file IO" },
64 { RERR_STREAMIO , "error in rsync protocol data stream" },
65 { RERR_MESSAGEIO , "errors with program diagnostics" },
66 { RERR_IPC , "error in IPC code" },
67 { RERR_SIGNAL , "received SIGUSR1 or SIGINT" },
68 { RERR_WAITCHILD , "some error returned by waitpid()" },
69 { RERR_MALLOC , "error allocating core memory buffers" },
70 { RERR_PARTIAL , "some files could not be transferred" },
71 { RERR_VANISHED , "some files vanished before they could be transferred" },
72 { RERR_TIMEOUT , "timeout in data send/receive" },
73 { RERR_CMD_FAILED , "remote shell failed" },
74 { RERR_CMD_KILLED , "remote shell killed" },
75 { RERR_CMD_RUN , "remote command could not be run" },
76 { RERR_CMD_NOTFOUND,"remote command not found" },
77 { RERR_DEL_LIMIT , "the --max-delete limit stopped deletions" },
84 * Map from rsync error code to name, or return NULL.
86 static char const *rerr_name(int code)
89 for (i = 0; rerr_names[i].name; i++) {
90 if (rerr_names[i].code == code)
91 return rerr_names[i].name;
97 static void logit(int priority, char *buf)
102 fprintf(logfile,"%s [%d] %s",
103 timestring(time(NULL)), (int)getpid(), buf);
106 syslog(priority, "%s", buf);
112 int options = LOG_PID;
119 /* this looks pointless, but it is needed in order for the
120 * C library on some systems to fetch the timezone info
121 * before the chroot */
125 /* optionally use a log file instead of syslog */
126 logfname = lp_log_file();
136 options |= LOG_NDELAY;
140 openlog("rsyncd", options, lp_syslog_facility());
142 openlog("rsyncd", options);
146 logit(LOG_INFO,"rsyncd started\n");
152 if (logfname && !logfile) {
153 extern int orig_umask;
154 int old_umask = umask(022 | orig_umask);
155 logfile = fopen(logfname, "a");
158 am_daemon = 0; /* avoid trying to log again */
159 rsyserr(FERROR, errno, "fopen() of log-file failed");
160 exit_cleanup(RERR_LOG_FAILURE);
173 /* this is the underlying (unformatted) rsync debugging function. Call
174 * it with FINFO, FERROR or FLOG */
175 void rwrite(enum logcode code, char *buf, int len)
178 /* recursion can happen with certain fatal conditions */
180 if (quiet && code == FINFO)
184 exit_cleanup(RERR_MESSAGEIO);
188 if (am_server && msg_fd_out >= 0) {
189 /* Pass the message to our sibling. */
190 send_msg((enum msgcode)code, buf, len);
196 else if (am_daemon) {
199 int priority = code == FERROR ? LOG_WARNING : LOG_INFO;
204 if (!log_initialised)
206 strlcpy(msg, buf, MIN((int)sizeof msg, len + 1));
207 logit(priority, msg);
210 if (code == FLOG || !am_server)
212 } else if (code == FLOG)
216 /* Pass the message to the non-server side. */
217 if (io_multiplex_write((enum msgcode)code, buf, len))
220 /* TODO: can we send the error to the user somehow? */
225 if (code == FERROR) {
231 f = am_server ? stderr : stdout;
234 exit_cleanup(RERR_MESSAGEIO);
236 if (fwrite(buf, len, 1, f) != 1)
237 exit_cleanup(RERR_MESSAGEIO);
239 if (buf[len-1] == '\r' || buf[len-1] == '\n')
244 /* This is the rsync debugging function. Call it with FINFO, FERROR or
246 void rprintf(enum logcode code, const char *format, ...)
249 char buf[MAXPATHLEN+512];
252 va_start(ap, format);
253 len = vsnprintf(buf, sizeof buf, format, ap);
256 /* Deal with buffer overruns. Instead of panicking, just
257 * truncate the resulting string. (Note that configure ensures
258 * that we have a vsnprintf() that doesn't ever return -1.) */
259 if (len > sizeof buf - 1) {
260 const char ellipsis[] = "[...]";
262 /* Reset length, and zero-terminate the end of our buffer */
263 len = sizeof buf - 1;
266 /* Copy the ellipsis to the end of the string, but give
267 * us one extra character:
269 * v--- null byte at buf[sizeof buf - 1]
271 * -> abcd[...]00 <-- now two null bytes at end
273 * If the input format string has a trailing newline,
274 * we copy it into that extra null; if it doesn't, well,
275 * all we lose is one byte. */
276 strncpy(buf+len-sizeof ellipsis, ellipsis, sizeof ellipsis);
277 if (format[strlen(format)-1] == '\n') {
282 rwrite(code, buf, len);
286 /* This is like rprintf, but it also tries to print some
287 * representation of the error code. Normally errcode = errno.
289 * Unlike rprintf, this always adds a newline and there should not be
290 * one in the format string.
292 * Note that since strerror might involve dynamically loading a
293 * message catalog we need to call it once before chroot-ing. */
294 void rsyserr(enum logcode code, int errcode, const char *format, ...)
297 char buf[MAXPATHLEN+512];
300 strcpy(buf, RSYNC_NAME ": ");
301 len = (sizeof RSYNC_NAME ": ") - 1;
303 va_start(ap, format);
304 len += vsnprintf(buf + len, sizeof buf - len, format, ap);
307 if (len < sizeof buf) {
308 len += snprintf(buf + len, sizeof buf - len,
309 ": %s (%d)\n", strerror(errcode), errcode);
311 if (len >= sizeof buf)
312 exit_cleanup(RERR_MESSAGEIO);
314 rwrite(code, buf, len);
319 void rflush(enum logcode code)
331 if (code == FERROR) {
342 if (!f) exit_cleanup(RERR_MESSAGEIO);
348 /* a generic logging routine for send/recv, with parameter
350 static void log_formatted(enum logcode code, char *format, char *op,
351 struct file_struct *file, struct stats *initial_stats,
352 int iflags, char *hlink)
354 char buf[MAXPATHLEN+1024];
355 char buf2[MAXPATHLEN];
360 /* We expand % codes one by one in place in buf. We don't
361 * copy in the terminating nul of the inserted strings, but
362 * rather keep going until we reach the nul of the format. */
363 total = strlcpy(buf, format, sizeof buf);
365 for (p = buf; (p = strchr(p, '%')) != NULL && p[1]; ) {
369 case 'h': if (am_daemon) n = client_name(0); break;
370 case 'a': if (am_daemon) n = client_addr(0); break;
372 snprintf(buf2, sizeof buf2, "%.0f",
373 (double)file->length);
377 snprintf(buf2, sizeof buf2, "%d",
381 case 'o': n = op; break;
383 pathjoin(buf2, sizeof buf2,
384 am_sender && file->dir.root ? file->dir.root : "",
385 safe_fname(f_name(file)));
386 clean_fname(buf2, 0);
391 n = (char*)safe_fname(f_name(file));
392 if (S_ISDIR(file->mode)) {
393 /* The buffer from safe_fname() has more
394 * room than MAXPATHLEN, so this is safe. */
399 if (hlink && *hlink) {
400 snprintf(buf2, sizeof buf2, " => %s",
403 } else if (S_ISLNK(file->mode) && file->u.link) {
404 snprintf(buf2, sizeof buf2, " -> %s",
405 safe_fname(file->u.link));
410 case 'm': n = lp_name(module_id); break;
411 case 't': n = timestring(time(NULL)); break;
412 case 'P': n = lp_path(module_id); break;
413 case 'u': n = auth_user; break;
416 b = stats.total_written -
417 initial_stats->total_written;
419 b = stats.total_read -
420 initial_stats->total_read;
422 snprintf(buf2, sizeof buf2, "%.0f", (double)b);
427 b = stats.total_written -
428 initial_stats->total_written;
430 b = stats.total_read -
431 initial_stats->total_read;
433 snprintf(buf2, sizeof buf2, "%.0f", (double)b);
437 if (iflags & ITEM_DELETED) {
442 n[0] = iflags & ITEM_LOCAL_CHANGE
443 ? iflags & ITEM_XNAME_FOLLOWS ? 'h' : 'c'
444 : !(iflags & ITEM_TRANSFER) ? '.'
445 : *op == 's' ? '>' : '<';
446 n[1] = S_ISDIR(file->mode) ? 'd'
447 : IS_DEVICE(file->mode) ? 'D'
448 : S_ISLNK(file->mode) ? 'L' : 'f';
449 n[2] = !(iflags & ITEM_REPORT_CHECKSUM) ? '.' : 'c';
450 n[3] = !(iflags & ITEM_REPORT_SIZE) ? '.' : 's';
451 n[4] = !(iflags & ITEM_REPORT_TIME) ? '.'
452 : !preserve_times || IS_DEVICE(file->mode)
453 || S_ISLNK(file->mode) ? 'T' : 't';
454 n[5] = !(iflags & ITEM_REPORT_PERMS) ? '.' : 'p';
455 n[6] = !(iflags & ITEM_REPORT_OWNER) ? '.' : 'o';
456 n[7] = !(iflags & ITEM_REPORT_GROUP) ? '.' : 'g';
457 n[8] = !(iflags & ITEM_REPORT_XATTRS) ? '.' : 'a';
460 if (iflags & (ITEM_IS_NEW|ITEM_MISSING_DATA)) {
461 char ch = iflags & ITEM_IS_NEW ? '+' : '?';
463 for (i = 2; n[i]; i++)
465 } else if (!(iflags & (ITEM_TRANSFER|ITEM_LOCAL_CHANGE))) {
467 for (i = 2; n[i]; i++) {
472 for (i = 2; n[i]; i++)
479 /* n is the string to be inserted in place of this %
480 * code; len is its length not including the trailing
489 if (len + total - 2 >= sizeof buf) {
491 "buffer overflow expanding %%%c -- exiting\n",
493 exit_cleanup(RERR_MESSAGEIO);
496 /* Shuffle the rest of the string along to make space for n */
498 memmove(p + len, p + 2, total - (p + 2 - buf) + 1);
501 /* Insert the contents of string "n", but NOT its nul. */
505 /* Skip over inserted string; continue looking */
509 rprintf(code, "%s\n", buf);
512 /* log the transfer of a file */
513 void log_item(struct file_struct *file, struct stats *initial_stats,
514 int iflags, char *hlink)
516 char *s_or_r = am_sender ? "send" : "recv";
518 if (lp_transfer_logging(module_id)) {
519 log_formatted(FLOG, lp_log_format(module_id), s_or_r,
520 file, initial_stats, iflags, hlink);
521 } else if (log_format && !am_server) {
522 log_formatted(FINFO, log_format, s_or_r,
523 file, initial_stats, iflags, hlink);
527 void maybe_log_item(struct file_struct *file, int iflags, int itemizing,
530 int see_item = itemizing && (iflags || verbose > 1);
532 if (am_daemon && !dry_run && see_item)
533 log_item(file, &stats, iflags, buf);
534 } else if (see_item || iflags & ITEM_LOCAL_CHANGE || *buf
535 || (S_ISDIR(file->mode) && iflags & SIGNIFICANT_ITEM_FLAGS))
536 log_item(file, &stats, iflags, buf);
539 void log_delete(char *fname, int mode)
541 static struct file_struct file;
542 int len = strlen(fname);
546 file.basename = fname;
548 if (!verbose && !log_format)
550 else if (am_server && protocol_version >= 29 && len < MAXPATHLEN) {
552 len++; /* directories include trailing null */
553 send_msg(MSG_DELETED, fname, len);
555 fmt = log_format_has_o_or_i ? log_format : "deleting %n";
556 log_formatted(FCLIENT, fmt, "del.", &file, &stats,
560 if (!am_daemon || dry_run || !lp_transfer_logging(module_id))
563 fmt = daemon_log_format_has_o_or_i ? lp_log_format(module_id) : "deleting %n";
564 log_formatted(FLOG, fmt, "del.", &file, &stats, ITEM_DELETED, NULL);
569 * Called when the transfer is interrupted for some reason.
571 * Code is one of the RERR_* codes from errcode.h, or terminating
574 void log_exit(int code, const char *file, int line)
577 rprintf(FLOG,"sent %.0f bytes received %.0f bytes total size %.0f\n",
578 (double)stats.total_written,
579 (double)stats.total_read,
580 (double)stats.total_size);
584 name = rerr_name(code);
586 name = "unexplained error";
588 /* VANISHED is not an error, only a warning */
589 if (code == RERR_VANISHED) {
590 rprintf(FINFO, "rsync warning: %s (code %d) at %s(%d)\n",
591 name, code, file, line);
593 rprintf(FERROR, "rsync error: %s (code %d) at %s(%d)\n",
594 name, code, file, line);