2 * Logging and utility functions.
4 * Copyright (C) 1998-2001 Andrew Tridgell <tridge@samba.org>
5 * Copyright (C) 2000-2001 Martin Pool <mbp@samba.org>
6 * Copyright (C) 2003-2020 Wayne Davison
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, visit the http://fsf.org website.
30 extern int am_generator;
31 extern int local_server;
34 extern int allow_8bit_chars;
35 extern int protocol_version;
36 extern int always_checksum;
37 extern int preserve_times;
38 extern int msgs2stderr;
39 extern int xfersum_type;
40 extern int checksum_type;
41 extern int stdout_format_has_i;
42 extern int stdout_format_has_o_or_i;
43 extern int logfile_format_has_i;
44 extern int logfile_format_has_o_or_i;
45 extern int receiver_symlink_times;
46 extern int64 total_data_written;
47 extern int64 total_data_read;
48 extern mode_t orig_umask;
49 extern char *auth_user;
50 extern char *stdout_format;
51 extern char *logfile_format;
52 extern char *logfile_name;
54 extern iconv_t ic_chck;
57 extern iconv_t ic_recv;
59 extern char curr_dir[MAXPATHLEN];
60 extern char *full_module_path;
61 extern unsigned int module_dirlen;
62 extern char sender_file_sum[MAX_DIGEST_LEN];
63 extern const char undetermined_hostname[];
65 static int log_initialised;
66 static int logfile_was_closed;
67 static FILE *logfile_fp;
70 int got_xfer_error = 0;
71 int output_needs_newline = 0;
72 int send_msgs_to_gen = 0;
74 static int64 initial_data_written;
75 static int64 initial_data_read;
80 } const rerr_names[] = {
81 { RERR_SYNTAX , "syntax or usage error" },
82 { RERR_PROTOCOL , "protocol incompatibility" },
83 { RERR_FILESELECT , "errors selecting input/output files, dirs" },
84 { RERR_UNSUPPORTED, "requested action not supported" },
85 { RERR_STARTCLIENT, "error starting client-server protocol" },
86 { RERR_SOCKETIO , "error in socket IO" },
87 { RERR_FILEIO , "error in file IO" },
88 { RERR_STREAMIO , "error in rsync protocol data stream" },
89 { RERR_MESSAGEIO , "errors with program diagnostics" },
90 { RERR_IPC , "error in IPC code" },
91 { RERR_CRASHED , "sibling process crashed" },
92 { RERR_TERMINATED , "sibling process terminated abnormally" },
93 { RERR_SIGNAL1 , "received SIGUSR1" },
94 { RERR_SIGNAL , "received SIGINT, SIGTERM, or SIGHUP" },
95 { RERR_WAITCHILD , "waitpid() failed" },
96 { RERR_MALLOC , "error allocating core memory buffers" },
97 { RERR_PARTIAL , "some files/attrs were not transferred (see previous errors)" },
98 { RERR_VANISHED , "some files vanished before they could be transferred" },
99 { RERR_DEL_LIMIT , "the --max-delete limit stopped deletions" },
100 { RERR_TIMEOUT , "timeout in data send/receive" },
101 { RERR_CONTIMEOUT , "timeout waiting for daemon connection" },
102 { RERR_CMD_FAILED , "remote shell failed" },
103 { RERR_CMD_KILLED , "remote shell killed" },
104 { RERR_CMD_RUN , "remote command could not be run" },
105 { RERR_CMD_NOTFOUND,"remote command not found" },
110 * Map from rsync error code to name, or return NULL.
112 static char const *rerr_name(int code)
115 for (i = 0; rerr_names[i].name; i++) {
116 if (rerr_names[i].code == code)
117 return rerr_names[i].name;
122 static void logit(int priority, const char *buf)
124 if (logfile_was_closed)
127 fprintf(logfile_fp, "%s [%d] %s", timestring(time(NULL)), (int)getpid(), buf);
130 syslog(priority, "%s", buf);
134 static void syslog_init()
136 int options = LOG_PID;
139 options |= LOG_NDELAY;
143 openlog(lp_syslog_tag(module_id), options, lp_syslog_facility(module_id));
145 openlog(lp_syslog_tag(module_id), options);
149 logit(LOG_INFO, "rsyncd started\n");
153 static void logfile_open(void)
155 mode_t old_umask = umask(022 | orig_umask);
156 logfile_fp = fopen(logfile_name, "a");
159 int fopen_errno = errno;
160 /* Rsync falls back to using syslog on failure. */
162 rsyserr(FERROR, fopen_errno,
163 "failed to open log-file %s", logfile_name);
164 rprintf(FINFO, "Ignoring \"log file\" setting.\n");
169 void log_init(int restart)
171 if (log_initialised) {
172 if (!restart) /* Note: a restart only happens with am_daemon */
174 assert(logfile_name); /* all am_daemon procs got at least an empty string */
175 if (strcmp(logfile_name, lp_log_file(module_id)) != 0) {
182 } else if (*logfile_name)
183 return; /* unchanged, non-empty "log file" names */
184 else if (lp_syslog_facility(-1) != lp_syslog_facility(module_id)
185 || strcmp(lp_syslog_tag(-1), lp_syslog_tag(module_id)) != 0)
188 return; /* unchanged syslog settings */
192 /* This looks pointless, but it is needed in order for the
193 * C library on some systems to fetch the timezone info
194 * before the chroot. */
195 timestring(time(NULL));
197 /* Optionally use a log file instead of syslog. (Non-daemon
198 * rsyncs will have already set logfile_name, as needed.) */
199 if (am_daemon && !logfile_name)
200 logfile_name = lp_log_file(module_id);
201 if (logfile_name && *logfile_name)
207 /* Note that this close & reopen idiom intentionally ignores syslog logging. */
208 void logfile_close(void)
211 logfile_was_closed = 1;
217 void logfile_reopen(void)
219 if (logfile_was_closed) {
220 logfile_was_closed = 0;
225 static void filtered_fwrite(FILE *f, const char *in_buf, int in_len, int use_isprint, char end_char)
227 char outbuf[1024], *ob = outbuf;
228 const char *end = in_buf + in_len;
229 while (in_buf < end) {
230 if (ob - outbuf >= (int)sizeof outbuf - 10) {
231 if (fwrite(outbuf, ob - outbuf, 1, f) != 1)
232 exit_cleanup(RERR_MESSAGEIO);
235 if ((in_buf < end - 4 && *in_buf == '\\' && in_buf[1] == '#'
236 && isDigit(in_buf + 2) && isDigit(in_buf + 3) && isDigit(in_buf + 4))
237 || (*in_buf != '\t' && ((use_isprint && !isPrint(in_buf)) || *(uchar*)in_buf < ' ')))
238 ob += snprintf(ob, 6, "\\#%03o", *(uchar*)in_buf++);
242 if (end_char) /* The "- 10" above means that there is always room for one more char here. */
244 if (ob != outbuf && fwrite(outbuf, ob - outbuf, 1, f) != 1)
245 exit_cleanup(RERR_MESSAGEIO);
248 /* this is the underlying (unformatted) rsync debugging function. Call
249 * it with FINFO, FERROR_*, FWARNING, FLOG, or FCLIENT. Note: recursion
250 * can happen with certain fatal conditions. */
251 void rwrite(enum logcode code, const char *buf, int len, int is_utf8)
253 char trailing_CR_or_NL;
254 FILE *f = msgs2stderr ? stderr : stdout;
256 iconv_t ic = is_utf8 && ic_recv != (iconv_t)-1 ? ic_recv : ic_chck;
259 iconv_t ic = ic_chck;
264 exit_cleanup(RERR_MESSAGEIO);
267 /* A normal daemon can get msgs2stderr set if the socket is busted, so we
268 * change the message destination into an FLOG message in order to try to
269 * get some info about an abnormal-exit into the log file. An rsh daemon
270 * can have this set via user request, so we'll leave the code alone so
271 * that the msg gets logged and then sent to stderr after that. */
272 if (am_daemon > 0 && code != FCLIENT)
274 } else if (send_msgs_to_gen) {
276 /* Pass the message to our sibling in native charset. */
277 send_msg((enum msgcode)code, buf, len, 0);
281 if (code == FERROR_SOCKET) /* This gets simplified for a non-sibling. */
283 else if (code == FERROR_UTF8) {
290 else if (am_daemon || logfile_name) {
293 int priority = code == FINFO || code == FLOG ? LOG_INFO : LOG_WARNING;
298 if (!log_initialised)
300 strlcpy(msg, buf, MIN((int)sizeof msg, len + 1));
301 logit(priority, msg);
304 if (code == FLOG || (am_daemon && !am_server))
306 } else if (code == FLOG)
324 //case FERROR_SOCKET:
326 fprintf(stderr, "Bad logcode in rwrite(): %d [%s]\n", (int)code, who_am_i());
327 exit_cleanup(RERR_MESSAGEIO);
330 if (am_server && !msgs2stderr) {
331 enum msgcode msg = (enum msgcode)code;
332 if (protocol_version < 30) {
333 if (msg == MSG_ERROR)
334 msg = MSG_ERROR_XFER;
335 else if (msg == MSG_WARNING)
338 /* Pass the message to the non-server side. */
339 if (send_msg(msg, buf, len, !is_utf8))
342 /* TODO: can we send the error to the user somehow? */
348 if (output_needs_newline) {
350 output_needs_newline = 0;
353 trailing_CR_or_NL = len && (buf[len-1] == '\n' || buf[len-1] == '\r')
356 if (len && buf[0] == '\r') {
363 if (ic != (iconv_t)-1) {
368 INIT_CONST_XBUF(outbuf, convbuf);
369 INIT_XBUF(inbuf, (char*)buf, len, (size_t)-1);
372 iconvbufs(ic, &inbuf, &outbuf, inbuf.pos ? 0 : ICB_INIT);
375 filtered_fwrite(f, convbuf, outbuf.len, 0, 0);
378 /* Log one byte of illegal/incomplete sequence and continue with
379 * the next character. Check that the buffer is non-empty for the
380 * sake of robustness. */
381 if ((ierrno == EILSEQ || ierrno == EINVAL) && inbuf.len) {
382 fprintf(f, "\\#%03o", CVAL(inbuf.buf, inbuf.pos++));
387 if (trailing_CR_or_NL) {
388 fputc(trailing_CR_or_NL, f);
394 filtered_fwrite(f, buf, len, !allow_8bit_chars, trailing_CR_or_NL);
395 if (trailing_CR_or_NL)
400 /* This is the rsync debugging function. Call it with FINFO, FERROR_*,
401 * FWARNING, FLOG, or FCLIENT. */
402 void rprintf(enum logcode code, const char *format, ...)
405 char buf[BIGPATHBUFLEN];
408 va_start(ap, format);
409 len = vsnprintf(buf, sizeof buf, format, ap);
412 /* Deal with buffer overruns. Instead of panicking, just
413 * truncate the resulting string. (Note that configure ensures
414 * that we have a vsnprintf() that doesn't ever return -1.) */
415 if (len > sizeof buf - 1) {
416 static const char ellipsis[] = "[...]";
418 /* Reset length, and zero-terminate the end of our buffer */
419 len = sizeof buf - 1;
422 /* Copy the ellipsis to the end of the string, but give
423 * us one extra character:
425 * v--- null byte at buf[sizeof buf - 1]
427 * -> abcd[...]00 <-- now two null bytes at end
429 * If the input format string has a trailing newline,
430 * we copy it into that extra null; if it doesn't, well,
431 * all we lose is one byte. */
432 memcpy(buf+len-sizeof ellipsis, ellipsis, sizeof ellipsis);
433 if (format[strlen(format)-1] == '\n') {
438 rwrite(code, buf, len, 0);
441 /* This is like rprintf, but it also tries to print some
442 * representation of the error code. Normally errcode = errno.
444 * Unlike rprintf, this always adds a newline and there should not be
445 * one in the format string.
447 * Note that since strerror might involve dynamically loading a
448 * message catalog we need to call it once before chroot-ing. */
449 void rsyserr(enum logcode code, int errcode, const char *format, ...)
452 char buf[BIGPATHBUFLEN];
455 strlcpy(buf, RSYNC_NAME ": ", sizeof buf);
456 len = (sizeof RSYNC_NAME ": ") - 1;
458 va_start(ap, format);
459 len += vsnprintf(buf + len, sizeof buf - len, format, ap);
462 if (len < sizeof buf) {
463 len += snprintf(buf + len, sizeof buf - len,
464 ": %s (%d)\n", strerror(errcode), errcode);
466 if (len >= sizeof buf)
467 exit_cleanup(RERR_MESSAGEIO);
469 rwrite(code, buf, len, 0);
472 void rflush(enum logcode code)
476 if (am_daemon || code == FLOG)
479 if (!am_server && (code == FINFO || code == FCLIENT))
487 void remember_initial_stats(void)
489 initial_data_read = total_data_read;
490 initial_data_written = total_data_written;
493 /* A generic logging routine for send/recv, with parameter substitiution. */
494 static void log_formatted(enum logcode code, const char *format, const char *op,
495 struct file_struct *file, const char *fname, int iflags,
498 char buf[MAXPATHLEN+1024], buf2[MAXPATHLEN], fmt[32];
506 /* We expand % codes one by one in place in buf. We don't
507 * copy in the terminating null of the inserted strings, but
508 * rather keep going until we reach the null of the format. */
509 total = strlcpy(buf, format, sizeof buf);
510 if (total > MAXPATHLEN) {
511 rprintf(FERROR, "log-format string is WAY too long!\n");
512 exit_cleanup(RERR_MESSAGEIO);
517 for (p = buf; (p = strchr(p, '%')) != NULL; ) {
527 while (isDigit(p) && c - fmt < (int)(sizeof fmt) - 8)
538 /* Note for %h and %a: it doesn't matter what fd we pass to
539 * client_{name,addr} because rsync_module will already have
540 * forced the answer to be cached (assuming, of course, for %h
541 * that lp_reverse_lookup(module_id) is true). */
545 n = lp_reverse_lookup(module_id)
546 ? client_name(0) : undetermined_hostname;
554 strlcat(fmt, "s", sizeof fmt);
555 snprintf(buf2, sizeof buf2, fmt,
556 do_big_num(F_LENGTH(file), humanize, NULL));
560 strlcat(fmt, "u", sizeof fmt);
561 snprintf(buf2, sizeof buf2, fmt,
562 uid_ndx ? F_OWNER(file) : 0);
566 if (!gid_ndx || file->flags & FLAG_SKIP_GROUP)
569 strlcat(fmt, "u", sizeof fmt);
570 snprintf(buf2, sizeof buf2, fmt,
576 strlcat(fmt, "d", sizeof fmt);
577 snprintf(buf2, sizeof buf2, fmt, (int)getpid());
581 n = c = timestring(file->modtime);
582 while ((c = strchr(c, ' ')) != NULL)
586 c = buf2 + MAXPATHLEN - PERMSTRING_SIZE - 1;
587 permstring(c, file->mode);
588 n = c + 1; /* skip the type char */
596 strlcpy(c, fname, MAXPATHLEN);
598 c = f_name(file, NULL);
599 if (am_sender && F_PATHNAME(file)) {
600 pathjoin(buf2, sizeof buf2,
601 F_PATHNAME(file), c);
602 clean_fname(buf2, 0);
604 strlcpy(c, buf2, MAXPATHLEN);
608 } else if (am_daemon && *c != '/') {
609 pathjoin(buf2, sizeof buf2,
610 curr_dir + module_dirlen, c);
611 clean_fname(buf2, 0);
613 strlcpy(c, buf2, MAXPATHLEN);
627 strlcpy(c, fname, MAXPATHLEN);
629 c = f_name(file, NULL);
630 if (S_ISDIR(file->mode))
631 strlcat(c, "/", MAXPATHLEN);
635 if (hlink && *hlink) {
637 strlcpy(buf2, " => ", sizeof buf2);
638 } else if (S_ISLNK(file->mode) && !fname) {
640 strlcpy(buf2, " -> ", sizeof buf2);
645 strlcpy(buf2, " ", sizeof buf2);
647 strlcat(fmt, "s", sizeof fmt);
648 snprintf(buf2 + 4, sizeof buf2 - 4, fmt, n);
652 n = lp_name(module_id);
655 n = timestring(time(NULL));
658 n = full_module_path;
665 if (!(iflags & ITEM_TRANSFER))
667 else if ((!!am_sender) ^ (*p == 'c'))
668 b = total_data_written - initial_data_written;
670 b = total_data_read - initial_data_read;
671 strlcat(fmt, "s", sizeof fmt);
672 snprintf(buf2, sizeof buf2, fmt,
673 do_big_num(b, humanize, NULL));
678 if (S_ISREG(file->mode)) {
680 n = sum_as_hex(checksum_type, F_SUM(file), 1);
681 else if (iflags & ITEM_TRANSFER)
682 n = sum_as_hex(xfersum_type, sender_file_sum, 0);
685 int sum_len = csum_len_for_type(always_checksum ? checksum_type : xfersum_type,
687 memset(buf2, ' ', sum_len*2);
688 buf2[sum_len*2] = '\0';
693 if (iflags & ITEM_DELETED) {
697 n = c = buf2 + MAXPATHLEN - 32;
698 c[0] = iflags & ITEM_LOCAL_CHANGE
699 ? iflags & ITEM_XNAME_FOLLOWS ? 'h' : 'c'
700 : !(iflags & ITEM_TRANSFER) ? '.'
701 : !local_server && *op == 's' ? '<' : '>';
702 if (S_ISLNK(file->mode)) {
705 c[4] = !(iflags & ITEM_REPORT_TIME) ? '.'
706 : !preserve_times || !receiver_symlink_times
707 || (iflags & ITEM_REPORT_TIMEFAIL) ? 'T' : 't';
709 c[1] = S_ISDIR(file->mode) ? 'd'
710 : IS_SPECIAL(file->mode) ? 'S'
711 : IS_DEVICE(file->mode) ? 'D' : 'f';
712 c[3] = !(iflags & ITEM_REPORT_SIZE) ? '.' : 's';
713 c[4] = !(iflags & ITEM_REPORT_TIME) ? '.'
714 : !preserve_times ? 'T' : 't';
716 c[2] = !(iflags & ITEM_REPORT_CHANGE) ? '.' : 'c';
717 c[5] = !(iflags & ITEM_REPORT_PERMS) ? '.' : 'p';
718 c[6] = !(iflags & ITEM_REPORT_OWNER) ? '.' : 'o';
719 c[7] = !(iflags & ITEM_REPORT_GROUP) ? '.' : 'g';
720 c[8] = !(iflags & ITEM_REPORT_ATIME) ? '.'
721 : S_ISLNK(file->mode) ? 'U' : 'u';
722 c[9] = !(iflags & ITEM_REPORT_ACL) ? '.' : 'a';
723 c[10] = !(iflags & ITEM_REPORT_XATTR) ? '.' : 'x';
726 if (iflags & (ITEM_IS_NEW|ITEM_MISSING_DATA)) {
727 char ch = iflags & ITEM_IS_NEW ? '+' : '?';
729 for (i = 2; c[i]; i++)
731 } else if (c[0] == '.' || c[0] == 'h' || c[0] == 'c') {
733 for (i = 2; c[i]; i++) {
738 for (i = 2; c[i]; i++)
745 /* "n" is the string to be inserted in place of this % code. */
748 if (n != buf2 && fmt[1]) {
749 strlcat(fmt, "s", sizeof fmt);
750 snprintf(buf2, sizeof buf2, fmt, n);
755 /* Subtract the length of the escape from the string's size. */
758 if (len + total >= (size_t)sizeof buf) {
760 "buffer overflow expanding %%%c -- exiting\n",
762 exit_cleanup(RERR_MESSAGEIO);
765 /* Shuffle the rest of the string along to make space for n */
766 if (len != (size_t)(p - s + 1))
767 memmove(s + len, p + 1, total - (s - buf) + 1);
770 /* Insert the contents of string "n", but NOT its null. */
774 /* Skip over inserted string; continue looking */
778 rwrite(code, buf, total, 0);
781 /* Return 1 if the format escape is in the log-format string (e.g. look for
782 * the 'b' in the "%9b" format escape). */
783 int log_format_has(const char *format, char esc)
790 for (p = format; (p = strchr(p, '%')) != NULL; ) {
791 for (p++; *p == '\''; p++) {} /*SHARED ITERATOR*/
796 while (*p == '\'') p++;
805 /* Log the transfer of a file. If the code is FCLIENT, the output just goes
806 * to stdout. If it is FLOG, it just goes to the log file. Otherwise we
808 void log_item(enum logcode code, struct file_struct *file, int iflags, const char *hlink)
810 const char *s_or_r = am_sender ? "send" : "recv";
812 if (code != FLOG && stdout_format && !am_server)
813 log_formatted(FCLIENT, stdout_format, s_or_r, file, NULL, iflags, hlink);
814 if (code != FCLIENT && logfile_format && *logfile_format)
815 log_formatted(FLOG, logfile_format, s_or_r, file, NULL, iflags, hlink);
818 void maybe_log_item(struct file_struct *file, int iflags, int itemizing, const char *buf)
820 int significant_flags = iflags & SIGNIFICANT_ITEM_FLAGS;
821 int see_item = itemizing && (significant_flags || *buf
822 || stdout_format_has_i > 1 || (INFO_GTE(NAME, 2) && stdout_format_has_i));
823 int local_change = iflags & ITEM_LOCAL_CHANGE && significant_flags;
825 if (logfile_name && !dry_run && see_item
826 && (significant_flags || logfile_format_has_i))
827 log_item(FLOG, file, iflags, buf);
828 } else if (see_item || local_change || *buf
829 || (S_ISDIR(file->mode) && significant_flags)) {
830 enum logcode code = significant_flags || logfile_format_has_i ? FINFO : FCLIENT;
831 log_item(code, file, iflags, buf);
835 void log_delete(const char *fname, int mode)
838 union file_extras ex[4]; /* just in case... */
839 struct file_struct file;
840 } x; /* Zero-initialized due to static declaration. */
841 int len = strlen(fname);
846 if (am_server && protocol_version >= 29 && len < MAXPATHLEN) {
848 len++; /* directories include trailing null */
849 send_msg(MSG_DELETED, fname, len, am_generator);
850 } else if (!INFO_GTE(DEL, 1) && !stdout_format)
853 fmt = stdout_format_has_o_or_i ? stdout_format : "deleting %n";
854 log_formatted(FCLIENT, fmt, "del.", &x.file, fname, ITEM_DELETED, NULL);
857 if (!logfile_name || dry_run || !logfile_format)
860 fmt = logfile_format_has_o_or_i ? logfile_format : "deleting %n";
861 log_formatted(FLOG, fmt, "del.", &x.file, fname, ITEM_DELETED, NULL);
865 * Called when the transfer is interrupted for some reason.
867 * Code is one of the RERR_* codes from errcode.h, or terminating
870 void log_exit(int code, const char *file, int line)
872 /* The receiving side's stats are split between 2 procs until the
873 * end of the run, so only the sender can output non-final info. */
874 if (code == 0 || am_sender) {
875 rprintf(FLOG,"sent %s bytes received %s bytes total size %s\n",
876 big_num(stats.total_written),
877 big_num(stats.total_read),
878 big_num(stats.total_size));
880 if (code != 0 && am_server != 2) {
883 name = rerr_name(code);
885 name = "unexplained error";
887 /* VANISHED is not an error, only a warning */
888 if (code == RERR_VANISHED) {
889 rprintf(FWARNING, "rsync warning: %s (code %d) at %s(%d) [%s=%s]\n",
890 name, code, file, line, who_am_i(), RSYNC_VERSION);
892 rprintf(FERROR, "rsync error: %s (code %d) at %s(%d) [%s=%s]\n",
893 name, code, file, line, who_am_i(), RSYNC_VERSION);