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 protocol_version;
43 extern int preserve_times;
44 extern int log_format_has_i;
45 extern int log_format_has_o_or_i;
46 extern int daemon_log_format_has_o_or_i;
47 extern char *auth_user;
48 extern char *log_format;
49 #if defined HAVE_ICONV_OPEN && defined HAVE_ICONV_H
50 extern iconv_t ic_chck;
53 static int log_initialised;
54 static int logfile_was_closed;
55 static char *logfname;
59 int log_got_error = 0;
64 } const rerr_names[] = {
65 { RERR_SYNTAX , "syntax or usage error" },
66 { RERR_PROTOCOL , "protocol incompatibility" },
67 { RERR_FILESELECT , "errors selecting input/output files, dirs" },
68 { RERR_UNSUPPORTED, "requested action not supported" },
69 { RERR_STARTCLIENT, "error starting client-server protocol" },
70 { RERR_SOCKETIO , "error in socket IO" },
71 { RERR_FILEIO , "error in file IO" },
72 { RERR_STREAMIO , "error in rsync protocol data stream" },
73 { RERR_MESSAGEIO , "errors with program diagnostics" },
74 { RERR_IPC , "error in IPC code" },
75 { RERR_CRASHED , "sibling process crashed" },
76 { RERR_TERMINATED , "sibling process terminated abnormally" },
77 { RERR_SIGNAL1 , "received SIGUSR1" },
78 { RERR_SIGNAL , "received SIGINT, SIGTERM, or SIGHUP" },
79 { RERR_WAITCHILD , "waitpid() failed" },
80 { RERR_MALLOC , "error allocating core memory buffers" },
81 { RERR_PARTIAL , "some files could not be transferred" },
82 { RERR_VANISHED , "some files vanished before they could be transferred" },
83 { RERR_TIMEOUT , "timeout in data send/receive" },
84 { RERR_CMD_FAILED , "remote shell failed" },
85 { RERR_CMD_KILLED , "remote shell killed" },
86 { RERR_CMD_RUN , "remote command could not be run" },
87 { RERR_CMD_NOTFOUND,"remote command not found" },
88 { RERR_DEL_LIMIT , "the --max-delete limit stopped deletions" },
94 * Map from rsync error code to name, or return NULL.
96 static char const *rerr_name(int code)
99 for (i = 0; rerr_names[i].name; i++) {
100 if (rerr_names[i].code == code)
101 return rerr_names[i].name;
106 static void logit(int priority, char *buf)
108 if (logfile_was_closed)
111 fprintf(logfile,"%s [%d] %s",
112 timestring(time(NULL)), (int)getpid(), buf);
115 syslog(priority, "%s", buf);
119 static void syslog_init()
121 static int been_here = 0;
122 int options = LOG_PID;
129 options |= LOG_NDELAY;
133 openlog("rsyncd", options, lp_syslog_facility());
135 openlog("rsyncd", options);
139 logit(LOG_INFO, "rsyncd started\n");
143 static void logfile_open(void)
145 extern int orig_umask;
146 int old_umask = umask(022 | orig_umask);
147 logfile = fopen(logfname, "a");
150 int fopen_errno = errno;
151 /* Rsync falls back to using syslog on failure. */
153 rsyserr(FERROR, fopen_errno,
154 "failed to open log-file %s", logfname);
155 rprintf(FINFO, "Ignoring \"log file\" setting.\n");
167 /* this looks pointless, but it is needed in order for the
168 * C library on some systems to fetch the timezone info
169 * before the chroot */
173 /* optionally use a log file instead of syslog */
174 logfname = lp_log_file();
175 if (logfname && *logfname)
181 void logfile_close(void)
184 logfile_was_closed = 1;
190 void logfile_reopen(void)
192 if (logfile_was_closed) {
193 logfile_was_closed = 0;
198 static void filtered_fwrite(const char *buf, int len, FILE *f)
200 const char *s, *end = buf + len;
201 for (s = buf; s < end; s++) {
203 && *s == '\\' && s[1] == '0'
204 && isdigit(*(uchar*)(s+2))
205 && isdigit(*(uchar*)(s+3))
206 && isdigit(*(uchar*)(s+4)))
207 #if defined HAVE_ICONV_OPEN && defined HAVE_ICONV_H
208 || (*(uchar*)s < ' ' && *s != '\t')
210 || ((!isprint(*(uchar*)s) || *(uchar*)s < ' ') && *s != '\t')
213 if (s != buf && fwrite(buf, s - buf, 1, f) != 1)
214 exit_cleanup(RERR_MESSAGEIO);
215 fprintf(f, "\\%04o", *(uchar*)s);
219 if (buf != end && fwrite(buf, end - buf, 1, f) != 1)
220 exit_cleanup(RERR_MESSAGEIO);
223 /* this is the underlying (unformatted) rsync debugging function. Call
224 * it with FINFO, FERROR or FLOG. Note: recursion can happen with
225 * certain fatal conditions. */
226 void rwrite(enum logcode code, char *buf, int len)
228 int trailing_CR_or_NL;
232 exit_cleanup(RERR_MESSAGEIO);
234 if (quiet && code == FINFO)
237 if (am_server && msg_fd_out >= 0) {
238 /* Pass the message to our sibling. */
239 send_msg((enum msgcode)code, buf, len);
243 if (code == FSOCKERR) /* This gets simplified for a non-sibling. */
248 else if (am_daemon) {
251 int priority = code == FERROR ? LOG_WARNING : LOG_INFO;
256 if (!log_initialised)
258 strlcpy(msg, buf, MIN((int)sizeof msg, len + 1));
259 logit(priority, msg);
262 if (code == FLOG || !am_server)
264 } else if (code == FLOG)
268 /* Pass the message to the non-server side. */
269 if (io_multiplex_write((enum msgcode)code, buf, len))
272 /* TODO: can we send the error to the user somehow? */
283 f = am_server ? stderr : stdout;
285 while (len > 1 && *buf == '\n') {
292 f = am_server ? stderr : stdout;
295 exit_cleanup(RERR_MESSAGEIO);
298 trailing_CR_or_NL = len && (buf[len-1] == '\n' || buf[len-1] == '\r')
301 #if defined HAVE_ICONV_OPEN && defined HAVE_ICONV_H
302 if (ic_chck != (iconv_t)-1) {
304 char *in_buf = buf, *out_buf = convbuf;
305 size_t in_cnt = len, out_cnt = sizeof convbuf - 1;
307 iconv(ic_chck, NULL, 0, NULL, 0);
308 while (iconv(ic_chck, &in_buf,&in_cnt,
309 &out_buf,&out_cnt) == (size_t)-1) {
310 if (out_buf != convbuf) {
311 filtered_fwrite(convbuf, out_buf - convbuf, f);
313 out_cnt = sizeof convbuf - 1;
317 fprintf(f, "\\%04o", *(uchar*)in_buf++);
320 if (out_buf != convbuf)
321 filtered_fwrite(convbuf, out_buf - convbuf, f);
324 filtered_fwrite(buf, len, f);
326 if (trailing_CR_or_NL) {
327 fputc(trailing_CR_or_NL, f);
332 /* This is the rsync debugging function. Call it with FINFO, FERROR or
334 void rprintf(enum logcode code, const char *format, ...)
337 char buf[BIGPATHBUFLEN];
340 va_start(ap, format);
341 len = vsnprintf(buf, sizeof buf, format, ap);
344 /* Deal with buffer overruns. Instead of panicking, just
345 * truncate the resulting string. (Note that configure ensures
346 * that we have a vsnprintf() that doesn't ever return -1.) */
347 if (len > sizeof buf - 1) {
348 static const char ellipsis[] = "[...]";
350 /* Reset length, and zero-terminate the end of our buffer */
351 len = sizeof buf - 1;
354 /* Copy the ellipsis to the end of the string, but give
355 * us one extra character:
357 * v--- null byte at buf[sizeof buf - 1]
359 * -> abcd[...]00 <-- now two null bytes at end
361 * If the input format string has a trailing newline,
362 * we copy it into that extra null; if it doesn't, well,
363 * all we lose is one byte. */
364 memcpy(buf+len-sizeof ellipsis, ellipsis, sizeof ellipsis);
365 if (format[strlen(format)-1] == '\n') {
370 rwrite(code, buf, len);
373 /* This is like rprintf, but it also tries to print some
374 * representation of the error code. Normally errcode = errno.
376 * Unlike rprintf, this always adds a newline and there should not be
377 * one in the format string.
379 * Note that since strerror might involve dynamically loading a
380 * message catalog we need to call it once before chroot-ing. */
381 void rsyserr(enum logcode code, int errcode, const char *format, ...)
384 char buf[BIGPATHBUFLEN];
387 strcpy(buf, RSYNC_NAME ": ");
388 len = (sizeof RSYNC_NAME ": ") - 1;
390 va_start(ap, format);
391 len += vsnprintf(buf + len, sizeof buf - len, format, ap);
394 if (len < sizeof buf) {
395 len += snprintf(buf + len, sizeof buf - len,
396 ": %s (%d)\n", strerror(errcode), errcode);
398 if (len >= sizeof buf)
399 exit_cleanup(RERR_MESSAGEIO);
401 rwrite(code, buf, len);
404 void rflush(enum logcode code)
416 if (code == FERROR) {
427 if (!f) exit_cleanup(RERR_MESSAGEIO);
431 /* a generic logging routine for send/recv, with parameter
433 static void log_formatted(enum logcode code, char *format, char *op,
434 struct file_struct *file, struct stats *initial_stats,
435 int iflags, char *hlink)
437 char buf[MAXPATHLEN+1024], buf2[MAXPATHLEN], fmt[32];
444 /* We expand % codes one by one in place in buf. We don't
445 * copy in the terminating null of the inserted strings, but
446 * rather keep going until we reach the null of the format. */
447 total = strlcpy(buf, format, sizeof buf);
448 if (total > MAXPATHLEN) {
449 rprintf(FERROR, "log-format string is WAY too long!\n");
450 exit_cleanup(RERR_MESSAGEIO);
455 for (p = buf; (p = strchr(p, '%')) != NULL; ) {
460 while (isdigit(*(uchar*)p) && n - fmt < (int)(sizeof fmt) - 8)
477 strlcat(fmt, ".0f", sizeof fmt);
478 snprintf(buf2, sizeof buf2, fmt,
479 (double)file->length);
483 strlcat(fmt, "ld", sizeof fmt);
484 snprintf(buf2, sizeof buf2, fmt,
489 if (file->gid == GID_NONE)
492 strlcat(fmt, "ld", sizeof fmt);
493 snprintf(buf2, sizeof buf2, fmt,
499 strlcat(fmt, "ld", sizeof fmt);
500 snprintf(buf2, sizeof buf2, fmt,
505 n = timestring(file->modtime);
508 while ((cp = strchr(cp, ' ')) != NULL)
513 n = buf2 + MAXPATHLEN - PERMSTRING_SIZE;
514 permstring(n - 1, file->mode); /* skip the type char */
520 n = f_name(file, NULL);
521 if (am_sender && file->dir.root) {
522 pathjoin(buf2, sizeof buf2,
524 clean_fname(buf2, 0);
526 strlcpy(n, buf2, MAXPATHLEN);
535 n = f_name(file, NULL);
536 if (S_ISDIR(file->mode))
537 strlcat(n, "/", MAXPATHLEN);
540 if (hlink && *hlink) {
542 strcpy(buf2, " => ");
543 } else if (S_ISLNK(file->mode) && file->u.link) {
545 strcpy(buf2, " -> ");
552 strlcat(fmt, "s", sizeof fmt);
553 snprintf(buf2 + 4, sizeof buf2 - 4, fmt, n);
557 n = lp_name(module_id);
560 n = timestring(time(NULL));
563 n = lp_path(module_id);
570 b = stats.total_written -
571 initial_stats->total_written;
573 b = stats.total_read -
574 initial_stats->total_read;
576 strlcat(fmt, ".0f", sizeof fmt);
577 snprintf(buf2, sizeof buf2, fmt, (double)b);
582 b = stats.total_written -
583 initial_stats->total_written;
585 b = stats.total_read -
586 initial_stats->total_read;
588 strlcat(fmt, ".0f", sizeof fmt);
589 snprintf(buf2, sizeof buf2, fmt, (double)b);
593 if (iflags & ITEM_DELETED) {
597 n = buf2 + MAXPATHLEN - 32;
598 n[0] = iflags & ITEM_LOCAL_CHANGE
599 ? iflags & ITEM_XNAME_FOLLOWS ? 'h' : 'c'
600 : !(iflags & ITEM_TRANSFER) ? '.'
601 : !local_server && *op == 's' ? '<' : '>';
602 n[1] = S_ISDIR(file->mode) ? 'd'
603 : IS_SPECIAL(file->mode) ? 'S'
604 : IS_DEVICE(file->mode) ? 'D'
605 : S_ISLNK(file->mode) ? 'L' : 'f';
606 n[2] = !(iflags & ITEM_REPORT_CHECKSUM) ? '.' : 'c';
607 n[3] = !(iflags & ITEM_REPORT_SIZE) ? '.' : 's';
608 n[4] = !(iflags & ITEM_REPORT_TIME) ? '.'
609 : !preserve_times || S_ISLNK(file->mode) ? 'T' : 't';
610 n[5] = !(iflags & ITEM_REPORT_PERMS) ? '.' : 'p';
611 n[6] = !(iflags & ITEM_REPORT_OWNER) ? '.' : 'o';
612 n[7] = !(iflags & ITEM_REPORT_GROUP) ? '.' : 'g';
615 if (iflags & (ITEM_IS_NEW|ITEM_MISSING_DATA)) {
616 char ch = iflags & ITEM_IS_NEW ? '+' : '?';
618 for (i = 2; n[i]; i++)
620 } else if (n[0] == '.' || n[0] == 'h'
621 || (n[0] == 'c' && n[1] == 'f')) {
623 for (i = 2; n[i]; i++) {
628 for (i = 2; n[i]; i++)
635 /* "n" is the string to be inserted in place of this % code. */
638 if (n != buf2 && fmt[1]) {
639 strlcat(fmt, "s", sizeof fmt);
640 snprintf(buf2, sizeof buf2, fmt, n);
645 /* Subtract the length of the escape from the string's size. */
648 if (len + total >= (size_t)sizeof buf) {
650 "buffer overflow expanding %%%c -- exiting\n",
652 exit_cleanup(RERR_MESSAGEIO);
655 /* Shuffle the rest of the string along to make space for n */
656 if (len != (size_t)(p - s + 1))
657 memmove(s + len, p + 1, total - (s - buf) + 1);
660 /* Insert the contents of string "n", but NOT its null. */
664 /* Skip over inserted string; continue looking */
668 rwrite(code, buf, total);
671 /* Return 1 if the format escape is in the log-format string (e.g. look for
672 * the 'b' in the "%9b" format escape). */
673 int log_format_has(const char *format, char esc)
680 for (p = format; (p = strchr(p, '%')) != NULL; ) {
683 while (isdigit(*(uchar*)p))
693 /* log the transfer of a file */
694 void log_item(struct file_struct *file, struct stats *initial_stats,
695 int iflags, char *hlink)
697 char *s_or_r = am_sender ? "send" : "recv";
699 if (lp_transfer_logging(module_id)) {
700 log_formatted(FLOG, lp_log_format(module_id), s_or_r,
701 file, initial_stats, iflags, hlink);
702 } else if (log_format && !am_server) {
703 log_formatted(FNAME, log_format, s_or_r,
704 file, initial_stats, iflags, hlink);
708 void maybe_log_item(struct file_struct *file, int iflags, int itemizing,
711 int significant_flags = iflags & SIGNIFICANT_ITEM_FLAGS;
712 int see_item = itemizing && (significant_flags || *buf
713 || log_format_has_i > 1 || (verbose > 1 && log_format_has_i));
714 int local_change = iflags & ITEM_LOCAL_CHANGE && significant_flags;
716 if (am_daemon && !dry_run && see_item)
717 log_item(file, &stats, iflags, buf);
718 } else if (see_item || local_change || *buf
719 || (S_ISDIR(file->mode) && significant_flags))
720 log_item(file, &stats, iflags, buf);
723 void log_delete(char *fname, int mode)
725 static struct file_struct file;
726 int len = strlen(fname);
730 file.basename = fname;
732 if (!verbose && !log_format)
734 else if (am_server && protocol_version >= 29 && len < MAXPATHLEN) {
736 len++; /* directories include trailing null */
737 send_msg(MSG_DELETED, fname, len);
739 fmt = log_format_has_o_or_i ? log_format : "deleting %n";
740 log_formatted(FCLIENT, fmt, "del.", &file, &stats,
744 if (!am_daemon || dry_run || !lp_transfer_logging(module_id))
747 fmt = daemon_log_format_has_o_or_i ? lp_log_format(module_id) : "deleting %n";
748 log_formatted(FLOG, fmt, "del.", &file, &stats, ITEM_DELETED, NULL);
752 * Called when the transfer is interrupted for some reason.
754 * Code is one of the RERR_* codes from errcode.h, or terminating
757 void log_exit(int code, const char *file, int line)
760 rprintf(FLOG,"sent %.0f bytes received %.0f bytes total size %.0f\n",
761 (double)stats.total_written,
762 (double)stats.total_read,
763 (double)stats.total_size);
767 name = rerr_name(code);
769 name = "unexplained error";
771 /* VANISHED is not an error, only a warning */
772 if (code == RERR_VANISHED) {
773 rprintf(FINFO, "rsync warning: %s (code %d) at %s(%d) [%s]\n",
774 name, code, file, line, who_am_i());
776 rprintf(FERROR, "rsync error: %s (code %d) at %s(%d) [%s]\n",
777 name, code, file, line, who_am_i());