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-2009 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 msg_fd_out
;
35 extern int allow_8bit_chars
;
36 extern int protocol_version
;
37 extern int preserve_times
;
40 extern int progress_is_active
;
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 mode_t orig_umask
;
47 extern char *auth_user
;
48 extern char *stdout_format
;
49 extern char *logfile_format
;
50 extern char *logfile_name
;
52 extern iconv_t ic_chck
;
55 extern iconv_t ic_recv
;
57 extern char curr_dir
[MAXPATHLEN
];
58 extern char *full_module_path
;
59 extern unsigned int module_dirlen
;
61 static int log_initialised
;
62 static int logfile_was_closed
;
63 static FILE *logfile_fp
;
66 int got_xfer_error
= 0;
71 } const rerr_names
[] = {
72 { RERR_SYNTAX
, "syntax or usage error" },
73 { RERR_PROTOCOL
, "protocol incompatibility" },
74 { RERR_FILESELECT
, "errors selecting input/output files, dirs" },
75 { RERR_UNSUPPORTED
, "requested action not supported" },
76 { RERR_STARTCLIENT
, "error starting client-server protocol" },
77 { RERR_SOCKETIO
, "error in socket IO" },
78 { RERR_FILEIO
, "error in file IO" },
79 { RERR_STREAMIO
, "error in rsync protocol data stream" },
80 { RERR_MESSAGEIO
, "errors with program diagnostics" },
81 { RERR_IPC
, "error in IPC code" },
82 { RERR_CRASHED
, "sibling process crashed" },
83 { RERR_TERMINATED
, "sibling process terminated abnormally" },
84 { RERR_SIGNAL1
, "received SIGUSR1" },
85 { RERR_SIGNAL
, "received SIGINT, SIGTERM, or SIGHUP" },
86 { RERR_WAITCHILD
, "waitpid() failed" },
87 { RERR_MALLOC
, "error allocating core memory buffers" },
88 { RERR_PARTIAL
, "some files/attrs were not transferred (see previous errors)" },
89 { RERR_VANISHED
, "some files vanished before they could be transferred" },
90 { RERR_TIMEOUT
, "timeout in data send/receive" },
91 { RERR_CONTIMEOUT
, "timeout waiting for daemon connection" },
92 { RERR_CMD_FAILED
, "remote shell failed" },
93 { RERR_CMD_KILLED
, "remote shell killed" },
94 { RERR_CMD_RUN
, "remote command could not be run" },
95 { RERR_CMD_NOTFOUND
,"remote command not found" },
96 { RERR_DEL_LIMIT
, "the --max-delete limit stopped deletions" },
101 * Map from rsync error code to name, or return NULL.
103 static char const *rerr_name(int code
)
106 for (i
= 0; rerr_names
[i
].name
; i
++) {
107 if (rerr_names
[i
].code
== code
)
108 return rerr_names
[i
].name
;
113 static void logit(int priority
, const char *buf
)
115 if (logfile_was_closed
)
118 fprintf(logfile_fp
, "%s [%d] %s",
119 timestring(time(NULL
)), (int)getpid(), buf
);
122 syslog(priority
, "%s", buf
);
126 static void syslog_init()
128 static int been_here
= 0;
129 int options
= LOG_PID
;
136 options
|= LOG_NDELAY
;
140 openlog("rsyncd", options
, lp_syslog_facility(module_id
));
142 openlog("rsyncd", options
);
146 logit(LOG_INFO
, "rsyncd started\n");
150 static void logfile_open(void)
152 mode_t old_umask
= umask(022 | orig_umask
);
153 logfile_fp
= fopen(logfile_name
, "a");
156 int fopen_errno
= errno
;
157 /* Rsync falls back to using syslog on failure. */
159 rsyserr(FERROR
, fopen_errno
,
160 "failed to open log-file %s", logfile_name
);
161 rprintf(FINFO
, "Ignoring \"log file\" setting.\n");
165 void log_init(int restart
)
167 if (log_initialised
) {
170 if (strcmp(logfile_name
, lp_log_file(module_id
)) != 0) {
177 } else if (*logfile_name
)
178 return; /* unchanged, non-empty "log file" names */
179 else if (lp_syslog_facility(-1) != lp_syslog_facility(module_id
))
182 return; /* unchanged syslog settings */
186 /* This looks pointless, but it is needed in order for the
187 * C library on some systems to fetch the timezone info
188 * before the chroot. */
189 timestring(time(NULL
));
191 /* Optionally use a log file instead of syslog. (Non-daemon
192 * rsyncs will have already set logfile_name, as needed.) */
193 if (am_daemon
&& !logfile_name
)
194 logfile_name
= lp_log_file(module_id
);
195 if (logfile_name
&& *logfile_name
)
201 void logfile_close(void)
204 logfile_was_closed
= 1;
210 void logfile_reopen(void)
212 if (logfile_was_closed
) {
213 logfile_was_closed
= 0;
218 static void filtered_fwrite(FILE *f
, const char *buf
, int len
, int use_isprint
)
220 const char *s
, *end
= buf
+ len
;
221 for (s
= buf
; s
< end
; s
++) {
223 && *s
== '\\' && s
[1] == '#'
228 && ((use_isprint
&& !isPrint(s
))
229 || *(uchar
*)s
< ' '))) {
230 if (s
!= buf
&& fwrite(buf
, s
- buf
, 1, f
) != 1)
231 exit_cleanup(RERR_MESSAGEIO
);
232 fprintf(f
, "\\#%03o", *(uchar
*)s
);
236 if (buf
!= end
&& fwrite(buf
, end
- buf
, 1, f
) != 1)
237 exit_cleanup(RERR_MESSAGEIO
);
240 /* this is the underlying (unformatted) rsync debugging function. Call
241 * it with FINFO, FERROR_*, FWARNING, FLOG, or FCLIENT. Note: recursion
242 * can happen with certain fatal conditions. */
243 void rwrite(enum logcode code
, const char *buf
, int len
, int is_utf8
)
245 int trailing_CR_or_NL
;
248 iconv_t ic
= is_utf8
&& ic_recv
!= (iconv_t
)-1 ? ic_recv
: ic_chck
;
251 iconv_t ic
= ic_chck
;
256 exit_cleanup(RERR_MESSAGEIO
);
258 if (am_server
&& msg_fd_out
>= 0) {
260 /* Pass the message to our sibling in native charset. */
261 send_msg((enum msgcode
)code
, buf
, len
, 0);
265 if (code
== FERROR_SOCKET
) /* This gets simplified for a non-sibling. */
267 else if (code
== FERROR_UTF8
) {
274 else if (am_daemon
|| logfile_name
) {
277 int priority
= code
== FINFO
|| code
== FLOG
? LOG_INFO
: LOG_WARNING
;
282 if (!log_initialised
)
284 strlcpy(msg
, buf
, MIN((int)sizeof msg
, len
+ 1));
285 logit(priority
, msg
);
288 if (code
== FLOG
|| (am_daemon
&& !am_server
))
290 } else if (code
== FLOG
)
293 if (quiet
&& code
== FINFO
)
297 enum msgcode msg
= (enum msgcode
)code
;
298 if (protocol_version
< 30) {
299 if (msg
== MSG_ERROR
)
300 msg
= MSG_ERROR_XFER
;
301 else if (msg
== MSG_WARNING
)
304 /* Pass the message to the non-server side. */
305 if (send_msg(msg
, buf
, len
, !is_utf8
))
308 /* TODO: can we send the error to the user somehow? */
322 f
= am_server
? stderr
: stdout
;
325 exit_cleanup(RERR_MESSAGEIO
);
328 if (progress_is_active
&& !am_server
) {
330 progress_is_active
= 0;
333 trailing_CR_or_NL
= len
&& (buf
[len
-1] == '\n' || buf
[len
-1] == '\r')
337 if (ic
!= (iconv_t
)-1) {
342 INIT_CONST_XBUF(outbuf
, convbuf
);
343 INIT_XBUF(inbuf
, (char*)buf
, len
, -1);
346 iconvbufs(ic
, &inbuf
, &outbuf
, 0);
349 filtered_fwrite(f
, convbuf
, outbuf
.len
, 0);
352 if (!ierrno
|| ierrno
== E2BIG
)
354 fprintf(f
, "\\#%03o", CVAL(inbuf
.buf
, inbuf
.pos
++));
359 filtered_fwrite(f
, buf
, len
, !allow_8bit_chars
);
361 if (trailing_CR_or_NL
) {
362 fputc(trailing_CR_or_NL
, f
);
367 /* This is the rsync debugging function. Call it with FINFO, FERROR_*,
368 * FWARNING, FLOG, or FCLIENT. */
369 void rprintf(enum logcode code
, const char *format
, ...)
372 char buf
[BIGPATHBUFLEN
];
375 va_start(ap
, format
);
376 len
= vsnprintf(buf
, sizeof buf
, format
, ap
);
379 /* Deal with buffer overruns. Instead of panicking, just
380 * truncate the resulting string. (Note that configure ensures
381 * that we have a vsnprintf() that doesn't ever return -1.) */
382 if (len
> sizeof buf
- 1) {
383 static const char ellipsis
[] = "[...]";
385 /* Reset length, and zero-terminate the end of our buffer */
386 len
= sizeof buf
- 1;
389 /* Copy the ellipsis to the end of the string, but give
390 * us one extra character:
392 * v--- null byte at buf[sizeof buf - 1]
394 * -> abcd[...]00 <-- now two null bytes at end
396 * If the input format string has a trailing newline,
397 * we copy it into that extra null; if it doesn't, well,
398 * all we lose is one byte. */
399 memcpy(buf
+len
-sizeof ellipsis
, ellipsis
, sizeof ellipsis
);
400 if (format
[strlen(format
)-1] == '\n') {
405 rwrite(code
, buf
, len
, 0);
408 /* This is like rprintf, but it also tries to print some
409 * representation of the error code. Normally errcode = errno.
411 * Unlike rprintf, this always adds a newline and there should not be
412 * one in the format string.
414 * Note that since strerror might involve dynamically loading a
415 * message catalog we need to call it once before chroot-ing. */
416 void rsyserr(enum logcode code
, int errcode
, const char *format
, ...)
419 char buf
[BIGPATHBUFLEN
];
422 strlcpy(buf
, RSYNC_NAME
": ", sizeof buf
);
423 len
= (sizeof RSYNC_NAME
": ") - 1;
425 va_start(ap
, format
);
426 len
+= vsnprintf(buf
+ len
, sizeof buf
- len
, format
, ap
);
429 if (len
< sizeof buf
) {
430 len
+= snprintf(buf
+ len
, sizeof buf
- len
,
431 ": %s (%d)\n", strerror(errcode
), errcode
);
433 if (len
>= sizeof buf
)
434 exit_cleanup(RERR_MESSAGEIO
);
436 rwrite(code
, buf
, len
, 0);
439 void rflush(enum logcode code
)
443 if (am_daemon
|| code
== FLOG
)
446 if (code
== FINFO
&& !am_server
)
454 /* A generic logging routine for send/recv, with parameter substitiution. */
455 static void log_formatted(enum logcode code
, const char *format
, const char *op
,
456 struct file_struct
*file
, const char *fname
,
457 struct stats
*initial_stats
, int iflags
,
460 char buf
[MAXPATHLEN
+1024], buf2
[MAXPATHLEN
], fmt
[32];
468 /* We expand % codes one by one in place in buf. We don't
469 * copy in the terminating null of the inserted strings, but
470 * rather keep going until we reach the null of the format. */
471 total
= strlcpy(buf
, format
, sizeof buf
);
472 if (total
> MAXPATHLEN
) {
473 rprintf(FERROR
, "log-format string is WAY too long!\n");
474 exit_cleanup(RERR_MESSAGEIO
);
479 for (p
= buf
; (p
= strchr(p
, '%')) != NULL
; ) {
484 while (isDigit(p
) && c
- fmt
< (int)(sizeof fmt
) - 8)
501 strlcat(fmt
, ".0f", sizeof fmt
);
502 snprintf(buf2
, sizeof buf2
, fmt
,
503 (double)F_LENGTH(file
));
507 strlcat(fmt
, "u", sizeof fmt
);
508 snprintf(buf2
, sizeof buf2
, fmt
,
509 uid_ndx
? F_OWNER(file
) : 0);
513 if (!gid_ndx
|| file
->flags
& FLAG_SKIP_GROUP
)
516 strlcat(fmt
, "u", sizeof fmt
);
517 snprintf(buf2
, sizeof buf2
, fmt
,
523 strlcat(fmt
, "ld", sizeof fmt
);
524 snprintf(buf2
, sizeof buf2
, fmt
,
529 n
= c
= timestring(file
->modtime
);
530 while ((c
= strchr(c
, ' ')) != NULL
)
534 c
= buf2
+ MAXPATHLEN
- PERMSTRING_SIZE
- 1;
535 permstring(c
, file
->mode
);
536 n
= c
+ 1; /* skip the type char */
544 strlcpy(c
, fname
, MAXPATHLEN
);
546 c
= f_name(file
, NULL
);
547 if (am_sender
&& F_PATHNAME(file
)) {
548 pathjoin(buf2
, sizeof buf2
,
549 F_PATHNAME(file
), c
);
550 clean_fname(buf2
, 0);
552 strlcpy(c
, buf2
, MAXPATHLEN
);
556 } else if (am_daemon
&& *c
!= '/') {
557 pathjoin(buf2
, sizeof buf2
,
558 curr_dir
+ module_dirlen
, c
);
559 clean_fname(buf2
, 0);
561 strlcpy(c
, buf2
, MAXPATHLEN
);
575 strlcpy(c
, fname
, MAXPATHLEN
);
577 c
= f_name(file
, NULL
);
578 if (S_ISDIR(file
->mode
))
579 strlcat(c
, "/", MAXPATHLEN
);
583 if (hlink
&& *hlink
) {
585 strlcpy(buf2
, " => ", sizeof buf2
);
586 } else if (S_ISLNK(file
->mode
) && !fname
) {
588 strlcpy(buf2
, " -> ", sizeof buf2
);
593 strlcpy(buf2
, " ", sizeof buf2
);
595 strlcat(fmt
, "s", sizeof fmt
);
596 snprintf(buf2
+ 4, sizeof buf2
- 4, fmt
, n
);
600 n
= lp_name(module_id
);
603 n
= timestring(time(NULL
));
606 n
= full_module_path
;
613 b
= stats
.total_written
-
614 initial_stats
->total_written
;
616 b
= stats
.total_read
-
617 initial_stats
->total_read
;
619 strlcat(fmt
, ".0f", sizeof fmt
);
620 snprintf(buf2
, sizeof buf2
, fmt
, (double)b
);
625 b
= stats
.total_written
-
626 initial_stats
->total_written
;
628 b
= stats
.total_read
-
629 initial_stats
->total_read
;
631 strlcat(fmt
, ".0f", sizeof fmt
);
632 snprintf(buf2
, sizeof buf2
, fmt
, (double)b
);
636 if (iflags
& ITEM_DELETED
) {
640 n
= c
= buf2
+ MAXPATHLEN
- 32;
641 c
[0] = iflags
& ITEM_LOCAL_CHANGE
642 ? iflags
& ITEM_XNAME_FOLLOWS
? 'h' : 'c'
643 : !(iflags
& ITEM_TRANSFER
) ? '.'
644 : !local_server
&& *op
== 's' ? '<' : '>';
645 if (S_ISLNK(file
->mode
)) {
648 c
[4] = !(iflags
& ITEM_REPORT_TIME
) ? '.'
649 : !preserve_times
|| !receiver_symlink_times
650 || (iflags
& ITEM_REPORT_TIMEFAIL
) ? 'T' : 't';
652 c
[1] = S_ISDIR(file
->mode
) ? 'd'
653 : IS_SPECIAL(file
->mode
) ? 'S'
654 : IS_DEVICE(file
->mode
) ? 'D' : 'f';
655 c
[3] = !(iflags
& ITEM_REPORT_SIZE
) ? '.' : 's';
656 c
[4] = !(iflags
& ITEM_REPORT_TIME
) ? '.'
657 : !preserve_times
? 'T' : 't';
659 c
[2] = !(iflags
& ITEM_REPORT_CHANGE
) ? '.' : 'c';
660 c
[5] = !(iflags
& ITEM_REPORT_PERMS
) ? '.' : 'p';
661 c
[6] = !(iflags
& ITEM_REPORT_OWNER
) ? '.' : 'o';
662 c
[7] = !(iflags
& ITEM_REPORT_GROUP
) ? '.' : 'g';
663 c
[8] = !(iflags
& ITEM_REPORT_ATIME
) ? '.' : 'u';
664 c
[9] = !(iflags
& ITEM_REPORT_ACL
) ? '.' : 'a';
665 c
[10] = !(iflags
& ITEM_REPORT_XATTR
) ? '.' : 'x';
668 if (iflags
& (ITEM_IS_NEW
|ITEM_MISSING_DATA
)) {
669 char ch
= iflags
& ITEM_IS_NEW
? '+' : '?';
671 for (i
= 2; c
[i
]; i
++)
673 } else if (c
[0] == '.' || c
[0] == 'h' || c
[0] == 'c') {
675 for (i
= 2; c
[i
]; i
++) {
680 for (i
= 2; c
[i
]; i
++)
687 /* "n" is the string to be inserted in place of this % code. */
690 if (n
!= buf2
&& fmt
[1]) {
691 strlcat(fmt
, "s", sizeof fmt
);
692 snprintf(buf2
, sizeof buf2
, fmt
, n
);
697 /* Subtract the length of the escape from the string's size. */
700 if (len
+ total
>= (size_t)sizeof buf
) {
702 "buffer overflow expanding %%%c -- exiting\n",
704 exit_cleanup(RERR_MESSAGEIO
);
707 /* Shuffle the rest of the string along to make space for n */
708 if (len
!= (size_t)(p
- s
+ 1))
709 memmove(s
+ len
, p
+ 1, total
- (s
- buf
) + 1);
712 /* Insert the contents of string "n", but NOT its null. */
716 /* Skip over inserted string; continue looking */
720 rwrite(code
, buf
, total
, 0);
723 /* Return 1 if the format escape is in the log-format string (e.g. look for
724 * the 'b' in the "%9b" format escape). */
725 int log_format_has(const char *format
, char esc
)
732 for (p
= format
; (p
= strchr(p
, '%')) != NULL
; ) {
745 /* Log the transfer of a file. If the code is FCLIENT, the output just goes
746 * to stdout. If it is FLOG, it just goes to the log file. Otherwise we
748 void log_item(enum logcode code
, struct file_struct
*file
,
749 struct stats
*initial_stats
, int iflags
, const char *hlink
)
751 const char *s_or_r
= am_sender
? "send" : "recv";
753 if (code
!= FLOG
&& stdout_format
&& !am_server
) {
754 log_formatted(FCLIENT
, stdout_format
, s_or_r
,
755 file
, NULL
, initial_stats
, iflags
, hlink
);
757 if (code
!= FCLIENT
&& logfile_format
&& *logfile_format
) {
758 log_formatted(FLOG
, logfile_format
, s_or_r
,
759 file
, NULL
, initial_stats
, iflags
, hlink
);
763 void maybe_log_item(struct file_struct
*file
, int iflags
, int itemizing
,
766 int significant_flags
= iflags
& SIGNIFICANT_ITEM_FLAGS
;
767 int see_item
= itemizing
&& (significant_flags
|| *buf
768 || stdout_format_has_i
> 1 || (verbose
> 1 && stdout_format_has_i
));
769 int local_change
= iflags
& ITEM_LOCAL_CHANGE
&& significant_flags
;
771 if (logfile_name
&& !dry_run
&& see_item
772 && (significant_flags
|| logfile_format_has_i
))
773 log_item(FLOG
, file
, &stats
, iflags
, buf
);
774 } else if (see_item
|| local_change
|| *buf
775 || (S_ISDIR(file
->mode
) && significant_flags
)) {
776 enum logcode code
= significant_flags
|| logfile_format_has_i
? FINFO
: FCLIENT
;
777 log_item(code
, file
, &stats
, iflags
, buf
);
781 void log_delete(const char *fname
, int mode
)
784 union file_extras ex
[4]; /* just in case... */
785 struct file_struct file
;
787 int len
= strlen(fname
);
792 if (!verbose
&& !stdout_format
)
794 else if (am_server
&& protocol_version
>= 29 && len
< MAXPATHLEN
) {
796 len
++; /* directories include trailing null */
797 send_msg(MSG_DELETED
, fname
, len
, am_generator
);
799 fmt
= stdout_format_has_o_or_i
? stdout_format
: "deleting %n";
800 log_formatted(FCLIENT
, fmt
, "del.", &x
.file
, fname
, &stats
,
804 if (!logfile_name
|| dry_run
|| !logfile_format
)
807 fmt
= logfile_format_has_o_or_i
? logfile_format
: "deleting %n";
808 log_formatted(FLOG
, fmt
, "del.", &x
.file
, fname
, &stats
, ITEM_DELETED
, NULL
);
812 * Called when the transfer is interrupted for some reason.
814 * Code is one of the RERR_* codes from errcode.h, or terminating
817 void log_exit(int code
, const char *file
, int line
)
820 rprintf(FLOG
,"sent %.0f bytes received %.0f bytes total size %.0f\n",
821 (double)stats
.total_written
,
822 (double)stats
.total_read
,
823 (double)stats
.total_size
);
824 } else if (am_server
!= 2) {
827 name
= rerr_name(code
);
829 name
= "unexplained error";
831 /* VANISHED is not an error, only a warning */
832 if (code
== RERR_VANISHED
) {
833 rprintf(FWARNING
, "rsync warning: %s (code %d) at %s(%d) [%s=%s]\n",
834 name
, code
, file
, line
, who_am_i(), RSYNC_VERSION
);
836 rprintf(FERROR
, "rsync error: %s (code %d) at %s(%d) [%s=%s]\n",
837 name
, code
, file
, line
, who_am_i(), RSYNC_VERSION
);