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-2008 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_send
, ic_recv
;
57 extern char curr_dir
[];
58 extern char *module_dir
;
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 could not be transferred" },
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. */
261 send_msg((enum msgcode
)code
, buf
, len
, 0);
265 if (code
== FERROR_SOCKET
) /* This gets simplified for a non-sibling. */
270 else if (am_daemon
|| logfile_name
) {
273 int priority
= code
== FINFO
|| code
== FLOG
? LOG_INFO
: LOG_WARNING
;
278 if (!log_initialised
)
280 strlcpy(msg
, buf
, MIN((int)sizeof msg
, len
+ 1));
281 logit(priority
, msg
);
284 if (code
== FLOG
|| (am_daemon
&& !am_server
))
286 } else if (code
== FLOG
)
289 if (quiet
&& code
== FINFO
)
293 enum msgcode msg
= (enum msgcode
)code
;
294 if (protocol_version
< 30) {
295 if (msg
== MSG_ERROR
)
296 msg
= MSG_ERROR_XFER
;
297 else if (msg
== MSG_WARNING
)
300 /* Pass the message to the non-server side. */
301 if (send_msg(msg
, buf
, len
, !is_utf8
))
304 /* TODO: can we send the error to the user somehow? */
318 f
= am_server
? stderr
: stdout
;
321 exit_cleanup(RERR_MESSAGEIO
);
324 if (progress_is_active
&& !am_server
) {
326 progress_is_active
= 0;
329 trailing_CR_or_NL
= len
&& (buf
[len
-1] == '\n' || buf
[len
-1] == '\r')
333 if (ic
!= (iconv_t
)-1) {
338 INIT_CONST_XBUF(outbuf
, convbuf
);
339 INIT_XBUF(inbuf
, (char*)buf
, len
, -1);
342 iconvbufs(ic
, &inbuf
, &outbuf
, 0);
345 filtered_fwrite(f
, convbuf
, outbuf
.len
, 0);
348 if (!ierrno
|| ierrno
== E2BIG
)
350 fprintf(f
, "\\#%03o", CVAL(inbuf
.buf
, inbuf
.pos
++));
355 filtered_fwrite(f
, buf
, len
, !allow_8bit_chars
);
357 if (trailing_CR_or_NL
) {
358 fputc(trailing_CR_or_NL
, f
);
363 /* This is the rsync debugging function. Call it with FINFO, FERROR_*,
364 * FWARNING, FLOG, or FCLIENT. */
365 void rprintf(enum logcode code
, const char *format
, ...)
368 char buf
[BIGPATHBUFLEN
];
371 va_start(ap
, format
);
372 len
= vsnprintf(buf
, sizeof buf
, format
, ap
);
375 /* Deal with buffer overruns. Instead of panicking, just
376 * truncate the resulting string. (Note that configure ensures
377 * that we have a vsnprintf() that doesn't ever return -1.) */
378 if (len
> sizeof buf
- 1) {
379 static const char ellipsis
[] = "[...]";
381 /* Reset length, and zero-terminate the end of our buffer */
382 len
= sizeof buf
- 1;
385 /* Copy the ellipsis to the end of the string, but give
386 * us one extra character:
388 * v--- null byte at buf[sizeof buf - 1]
390 * -> abcd[...]00 <-- now two null bytes at end
392 * If the input format string has a trailing newline,
393 * we copy it into that extra null; if it doesn't, well,
394 * all we lose is one byte. */
395 memcpy(buf
+len
-sizeof ellipsis
, ellipsis
, sizeof ellipsis
);
396 if (format
[strlen(format
)-1] == '\n') {
401 rwrite(code
, buf
, len
, 0);
404 /* This is like rprintf, but it also tries to print some
405 * representation of the error code. Normally errcode = errno.
407 * Unlike rprintf, this always adds a newline and there should not be
408 * one in the format string.
410 * Note that since strerror might involve dynamically loading a
411 * message catalog we need to call it once before chroot-ing. */
412 void rsyserr(enum logcode code
, int errcode
, const char *format
, ...)
415 char buf
[BIGPATHBUFLEN
];
418 strlcpy(buf
, RSYNC_NAME
": ", sizeof buf
);
419 len
= (sizeof RSYNC_NAME
": ") - 1;
421 va_start(ap
, format
);
422 len
+= vsnprintf(buf
+ len
, sizeof buf
- len
, format
, ap
);
425 if (len
< sizeof buf
) {
426 len
+= snprintf(buf
+ len
, sizeof buf
- len
,
427 ": %s (%d)\n", strerror(errcode
), errcode
);
429 if (len
>= sizeof buf
)
430 exit_cleanup(RERR_MESSAGEIO
);
432 rwrite(code
, buf
, len
, 0);
435 void rflush(enum logcode code
)
439 if (am_daemon
|| code
== FLOG
)
442 if (code
== FINFO
&& !am_server
)
450 /* A generic logging routine for send/recv, with parameter substitiution. */
451 static void log_formatted(enum logcode code
, const char *format
, const char *op
,
452 struct file_struct
*file
, const char *fname
,
453 struct stats
*initial_stats
, int iflags
,
456 char buf
[MAXPATHLEN
+1024], buf2
[MAXPATHLEN
], fmt
[32];
464 /* We expand % codes one by one in place in buf. We don't
465 * copy in the terminating null of the inserted strings, but
466 * rather keep going until we reach the null of the format. */
467 total
= strlcpy(buf
, format
, sizeof buf
);
468 if (total
> MAXPATHLEN
) {
469 rprintf(FERROR
, "log-format string is WAY too long!\n");
470 exit_cleanup(RERR_MESSAGEIO
);
475 for (p
= buf
; (p
= strchr(p
, '%')) != NULL
; ) {
480 while (isDigit(p
) && c
- fmt
< (int)(sizeof fmt
) - 8)
497 strlcat(fmt
, ".0f", sizeof fmt
);
498 snprintf(buf2
, sizeof buf2
, fmt
,
499 (double)F_LENGTH(file
));
503 strlcat(fmt
, "u", sizeof fmt
);
504 snprintf(buf2
, sizeof buf2
, fmt
,
505 uid_ndx
? F_OWNER(file
) : 0);
509 if (!gid_ndx
|| file
->flags
& FLAG_SKIP_GROUP
)
512 strlcat(fmt
, "u", sizeof fmt
);
513 snprintf(buf2
, sizeof buf2
, fmt
,
519 strlcat(fmt
, "ld", sizeof fmt
);
520 snprintf(buf2
, sizeof buf2
, fmt
,
525 n
= c
= timestring(file
->modtime
);
526 while ((c
= strchr(c
, ' ')) != NULL
)
530 c
= buf2
+ MAXPATHLEN
- PERMSTRING_SIZE
- 1;
531 permstring(c
, file
->mode
);
532 n
= c
+ 1; /* skip the type char */
540 strlcpy(c
, fname
, MAXPATHLEN
);
542 c
= f_name(file
, NULL
);
543 if (am_sender
&& F_PATHNAME(file
)) {
544 pathjoin(buf2
, sizeof buf2
,
545 F_PATHNAME(file
), c
);
546 clean_fname(buf2
, 0);
548 strlcpy(c
, buf2
, MAXPATHLEN
);
552 } else if (am_daemon
&& *c
!= '/') {
553 pathjoin(buf2
, sizeof buf2
,
554 curr_dir
+ module_dirlen
, c
);
555 clean_fname(buf2
, 0);
557 strlcpy(c
, buf2
, MAXPATHLEN
);
571 strlcpy(c
, fname
, MAXPATHLEN
);
573 c
= f_name(file
, NULL
);
574 if (S_ISDIR(file
->mode
))
575 strlcat(c
, "/", MAXPATHLEN
);
579 if (hlink
&& *hlink
) {
581 strlcpy(buf2
, " => ", sizeof buf2
);
582 } else if (S_ISLNK(file
->mode
) && !fname
) {
584 strlcpy(buf2
, " -> ", sizeof buf2
);
589 strlcpy(buf2
, " ", sizeof buf2
);
591 strlcat(fmt
, "s", sizeof fmt
);
592 snprintf(buf2
+ 4, sizeof buf2
- 4, fmt
, n
);
596 n
= lp_name(module_id
);
599 n
= timestring(time(NULL
));
609 b
= stats
.total_written
-
610 initial_stats
->total_written
;
612 b
= stats
.total_read
-
613 initial_stats
->total_read
;
615 strlcat(fmt
, ".0f", sizeof fmt
);
616 snprintf(buf2
, sizeof buf2
, fmt
, (double)b
);
621 b
= stats
.total_written
-
622 initial_stats
->total_written
;
624 b
= stats
.total_read
-
625 initial_stats
->total_read
;
627 strlcat(fmt
, ".0f", sizeof fmt
);
628 snprintf(buf2
, sizeof buf2
, fmt
, (double)b
);
632 if (iflags
& ITEM_DELETED
) {
636 n
= c
= buf2
+ MAXPATHLEN
- 32;
637 c
[0] = iflags
& ITEM_LOCAL_CHANGE
638 ? iflags
& ITEM_XNAME_FOLLOWS
? 'h' : 'c'
639 : !(iflags
& ITEM_TRANSFER
) ? '.'
640 : !local_server
&& *op
== 's' ? '<' : '>';
641 if (S_ISLNK(file
->mode
)) {
644 c
[4] = !(iflags
& ITEM_REPORT_TIME
) ? '.'
645 : !preserve_times
|| !receiver_symlink_times
646 || (iflags
& ITEM_REPORT_TIMEFAIL
) ? 'T' : 't';
648 c
[1] = S_ISDIR(file
->mode
) ? 'd'
649 : IS_SPECIAL(file
->mode
) ? 'S'
650 : IS_DEVICE(file
->mode
) ? 'D' : 'f';
651 c
[3] = !(iflags
& ITEM_REPORT_SIZE
) ? '.' : 's';
652 c
[4] = !(iflags
& ITEM_REPORT_TIME
) ? '.'
653 : !preserve_times
? 'T' : 't';
655 c
[2] = !(iflags
& ITEM_REPORT_CHANGE
) ? '.' : 'c';
656 c
[5] = !(iflags
& ITEM_REPORT_PERMS
) ? '.' : 'p';
657 c
[6] = !(iflags
& ITEM_REPORT_OWNER
) ? '.' : 'o';
658 c
[7] = !(iflags
& ITEM_REPORT_GROUP
) ? '.' : 'g';
659 c
[8] = !(iflags
& ITEM_REPORT_ATIME
) ? '.' : 'u';
660 c
[9] = !(iflags
& ITEM_REPORT_ACL
) ? '.' : 'a';
661 c
[10] = !(iflags
& ITEM_REPORT_XATTR
) ? '.' : 'x';
664 if (iflags
& (ITEM_IS_NEW
|ITEM_MISSING_DATA
)) {
665 char ch
= iflags
& ITEM_IS_NEW
? '+' : '?';
667 for (i
= 2; c
[i
]; i
++)
669 } else if (c
[0] == '.' || c
[0] == 'h' || c
[0] == 'c') {
671 for (i
= 2; c
[i
]; i
++) {
676 for (i
= 2; c
[i
]; i
++)
683 /* "n" is the string to be inserted in place of this % code. */
686 if (n
!= buf2
&& fmt
[1]) {
687 strlcat(fmt
, "s", sizeof fmt
);
688 snprintf(buf2
, sizeof buf2
, fmt
, n
);
693 /* Subtract the length of the escape from the string's size. */
696 if (len
+ total
>= (size_t)sizeof buf
) {
698 "buffer overflow expanding %%%c -- exiting\n",
700 exit_cleanup(RERR_MESSAGEIO
);
703 /* Shuffle the rest of the string along to make space for n */
704 if (len
!= (size_t)(p
- s
+ 1))
705 memmove(s
+ len
, p
+ 1, total
- (s
- buf
) + 1);
708 /* Insert the contents of string "n", but NOT its null. */
712 /* Skip over inserted string; continue looking */
716 rwrite(code
, buf
, total
, 0);
719 /* Return 1 if the format escape is in the log-format string (e.g. look for
720 * the 'b' in the "%9b" format escape). */
721 int log_format_has(const char *format
, char esc
)
728 for (p
= format
; (p
= strchr(p
, '%')) != NULL
; ) {
741 /* Log the transfer of a file. If the code is FCLIENT, the output just goes
742 * to stdout. If it is FLOG, it just goes to the log file. Otherwise we
744 void log_item(enum logcode code
, struct file_struct
*file
,
745 struct stats
*initial_stats
, int iflags
, const char *hlink
)
747 const char *s_or_r
= am_sender
? "send" : "recv";
749 if (code
!= FLOG
&& stdout_format
&& !am_server
) {
750 log_formatted(FCLIENT
, stdout_format
, s_or_r
,
751 file
, NULL
, initial_stats
, iflags
, hlink
);
753 if (code
!= FCLIENT
&& logfile_format
&& *logfile_format
) {
754 log_formatted(FLOG
, logfile_format
, s_or_r
,
755 file
, NULL
, initial_stats
, iflags
, hlink
);
759 void maybe_log_item(struct file_struct
*file
, int iflags
, int itemizing
,
762 int significant_flags
= iflags
& SIGNIFICANT_ITEM_FLAGS
;
763 int see_item
= itemizing
&& (significant_flags
|| *buf
764 || stdout_format_has_i
> 1 || (verbose
> 1 && stdout_format_has_i
));
765 int local_change
= iflags
& ITEM_LOCAL_CHANGE
&& significant_flags
;
767 if (logfile_name
&& !dry_run
&& see_item
768 && (significant_flags
|| logfile_format_has_i
))
769 log_item(FLOG
, file
, &stats
, iflags
, buf
);
770 } else if (see_item
|| local_change
|| *buf
771 || (S_ISDIR(file
->mode
) && significant_flags
)) {
772 enum logcode code
= significant_flags
|| logfile_format_has_i
? FINFO
: FCLIENT
;
773 log_item(code
, file
, &stats
, iflags
, buf
);
777 void log_delete(const char *fname
, int mode
)
780 union file_extras ex
[4]; /* just in case... */
781 struct file_struct file
;
783 int len
= strlen(fname
);
788 if (!verbose
&& !stdout_format
)
790 else if (am_server
&& protocol_version
>= 29 && len
< MAXPATHLEN
) {
792 len
++; /* directories include trailing null */
793 send_msg(MSG_DELETED
, fname
, len
, am_generator
);
795 fmt
= stdout_format_has_o_or_i
? stdout_format
: "deleting %n";
796 log_formatted(FCLIENT
, fmt
, "del.", &x
.file
, fname
, &stats
,
800 if (!logfile_name
|| dry_run
|| !logfile_format
)
803 fmt
= logfile_format_has_o_or_i
? logfile_format
: "deleting %n";
804 log_formatted(FLOG
, fmt
, "del.", &x
.file
, fname
, &stats
, ITEM_DELETED
, NULL
);
808 * Called when the transfer is interrupted for some reason.
810 * Code is one of the RERR_* codes from errcode.h, or terminating
813 void log_exit(int code
, const char *file
, int line
)
816 rprintf(FLOG
,"sent %.0f bytes received %.0f bytes total size %.0f\n",
817 (double)stats
.total_written
,
818 (double)stats
.total_read
,
819 (double)stats
.total_size
);
820 } else if (am_server
!= 2) {
823 name
= rerr_name(code
);
825 name
= "unexplained error";
827 /* VANISHED is not an error, only a warning */
828 if (code
== RERR_VANISHED
) {
829 rprintf(FWARNING
, "rsync warning: %s (code %d) at %s(%d) [%s=%s]\n",
830 name
, code
, file
, line
, who_am_i(), RSYNC_VERSION
);
832 rprintf(FERROR
, "rsync error: %s (code %d) at %s(%d) [%s=%s]\n",
833 name
, code
, file
, line
, who_am_i(), RSYNC_VERSION
);