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
;
38 extern int progress_is_active
;
39 extern int stdout_format_has_i
;
40 extern int stdout_format_has_o_or_i
;
41 extern int logfile_format_has_i
;
42 extern int logfile_format_has_o_or_i
;
43 extern int receiver_symlink_times
;
44 extern mode_t orig_umask
;
45 extern char *auth_user
;
46 extern char *stdout_format
;
47 extern char *logfile_format
;
48 extern char *logfile_name
;
50 extern iconv_t ic_chck
;
53 extern iconv_t ic_recv
;
55 extern char curr_dir
[MAXPATHLEN
];
56 extern char *full_module_path
;
57 extern unsigned int module_dirlen
;
59 static int log_initialised
;
60 static int logfile_was_closed
;
61 static FILE *logfile_fp
;
64 int got_xfer_error
= 0;
69 } const rerr_names
[] = {
70 { RERR_SYNTAX
, "syntax or usage error" },
71 { RERR_PROTOCOL
, "protocol incompatibility" },
72 { RERR_FILESELECT
, "errors selecting input/output files, dirs" },
73 { RERR_UNSUPPORTED
, "requested action not supported" },
74 { RERR_STARTCLIENT
, "error starting client-server protocol" },
75 { RERR_SOCKETIO
, "error in socket IO" },
76 { RERR_FILEIO
, "error in file IO" },
77 { RERR_STREAMIO
, "error in rsync protocol data stream" },
78 { RERR_MESSAGEIO
, "errors with program diagnostics" },
79 { RERR_IPC
, "error in IPC code" },
80 { RERR_CRASHED
, "sibling process crashed" },
81 { RERR_TERMINATED
, "sibling process terminated abnormally" },
82 { RERR_SIGNAL1
, "received SIGUSR1" },
83 { RERR_SIGNAL
, "received SIGINT, SIGTERM, or SIGHUP" },
84 { RERR_WAITCHILD
, "waitpid() failed" },
85 { RERR_MALLOC
, "error allocating core memory buffers" },
86 { RERR_PARTIAL
, "some files/attrs were not transferred (see previous errors)" },
87 { RERR_VANISHED
, "some files vanished before they could be transferred" },
88 { RERR_TIMEOUT
, "timeout in data send/receive" },
89 { RERR_CONTIMEOUT
, "timeout waiting for daemon connection" },
90 { RERR_CMD_FAILED
, "remote shell failed" },
91 { RERR_CMD_KILLED
, "remote shell killed" },
92 { RERR_CMD_RUN
, "remote command could not be run" },
93 { RERR_CMD_NOTFOUND
,"remote command not found" },
94 { RERR_DEL_LIMIT
, "the --max-delete limit stopped deletions" },
99 * Map from rsync error code to name, or return NULL.
101 static char const *rerr_name(int code
)
104 for (i
= 0; rerr_names
[i
].name
; i
++) {
105 if (rerr_names
[i
].code
== code
)
106 return rerr_names
[i
].name
;
111 static void logit(int priority
, const char *buf
)
113 if (logfile_was_closed
)
116 fprintf(logfile_fp
, "%s [%d] %s",
117 timestring(time(NULL
)), (int)getpid(), buf
);
120 syslog(priority
, "%s", buf
);
124 static void syslog_init()
126 static int been_here
= 0;
127 int options
= LOG_PID
;
134 options
|= LOG_NDELAY
;
138 openlog("rsyncd", options
, lp_syslog_facility(module_id
));
140 openlog("rsyncd", options
);
144 logit(LOG_INFO
, "rsyncd started\n");
148 static void logfile_open(void)
150 mode_t old_umask
= umask(022 | orig_umask
);
151 logfile_fp
= fopen(logfile_name
, "a");
154 int fopen_errno
= errno
;
155 /* Rsync falls back to using syslog on failure. */
157 rsyserr(FERROR
, fopen_errno
,
158 "failed to open log-file %s", logfile_name
);
159 rprintf(FINFO
, "Ignoring \"log file\" setting.\n");
163 void log_init(int restart
)
165 if (log_initialised
) {
168 if (strcmp(logfile_name
, lp_log_file(module_id
)) != 0) {
175 } else if (*logfile_name
)
176 return; /* unchanged, non-empty "log file" names */
177 else if (lp_syslog_facility(-1) != lp_syslog_facility(module_id
))
180 return; /* unchanged syslog settings */
184 /* This looks pointless, but it is needed in order for the
185 * C library on some systems to fetch the timezone info
186 * before the chroot. */
187 timestring(time(NULL
));
189 /* Optionally use a log file instead of syslog. (Non-daemon
190 * rsyncs will have already set logfile_name, as needed.) */
191 if (am_daemon
&& !logfile_name
)
192 logfile_name
= lp_log_file(module_id
);
193 if (logfile_name
&& *logfile_name
)
199 void logfile_close(void)
202 logfile_was_closed
= 1;
208 void logfile_reopen(void)
210 if (logfile_was_closed
) {
211 logfile_was_closed
= 0;
216 static void filtered_fwrite(FILE *f
, const char *buf
, int len
, int use_isprint
)
218 const char *s
, *end
= buf
+ len
;
219 for (s
= buf
; s
< end
; s
++) {
221 && *s
== '\\' && s
[1] == '#'
226 && ((use_isprint
&& !isPrint(s
))
227 || *(uchar
*)s
< ' '))) {
228 if (s
!= buf
&& fwrite(buf
, s
- buf
, 1, f
) != 1)
229 exit_cleanup(RERR_MESSAGEIO
);
230 fprintf(f
, "\\#%03o", *(uchar
*)s
);
234 if (buf
!= end
&& fwrite(buf
, end
- buf
, 1, f
) != 1)
235 exit_cleanup(RERR_MESSAGEIO
);
238 /* this is the underlying (unformatted) rsync debugging function. Call
239 * it with FINFO, FERROR_*, FWARNING, FLOG, or FCLIENT. Note: recursion
240 * can happen with certain fatal conditions. */
241 void rwrite(enum logcode code
, const char *buf
, int len
, int is_utf8
)
243 int trailing_CR_or_NL
;
246 iconv_t ic
= is_utf8
&& ic_recv
!= (iconv_t
)-1 ? ic_recv
: ic_chck
;
249 iconv_t ic
= ic_chck
;
254 exit_cleanup(RERR_MESSAGEIO
);
256 if (am_server
&& msg_fd_out
>= 0) {
258 /* Pass the message to our sibling in native charset. */
259 send_msg((enum msgcode
)code
, buf
, len
, 0);
263 if (code
== FERROR_SOCKET
) /* This gets simplified for a non-sibling. */
265 else if (code
== FERROR_UTF8
) {
272 else if (am_daemon
|| logfile_name
) {
275 int priority
= code
== FINFO
|| code
== FLOG
? LOG_INFO
: LOG_WARNING
;
280 if (!log_initialised
)
282 strlcpy(msg
, buf
, MIN((int)sizeof msg
, len
+ 1));
283 logit(priority
, msg
);
286 if (code
== FLOG
|| (am_daemon
&& !am_server
))
288 } else if (code
== FLOG
)
291 if (quiet
&& code
== FINFO
)
295 enum msgcode msg
= (enum msgcode
)code
;
296 if (protocol_version
< 30) {
297 if (msg
== MSG_ERROR
)
298 msg
= MSG_ERROR_XFER
;
299 else if (msg
== MSG_WARNING
)
302 /* Pass the message to the non-server side. */
303 if (send_msg(msg
, buf
, len
, !is_utf8
))
306 /* TODO: can we send the error to the user somehow? */
320 f
= am_server
? stderr
: stdout
;
323 exit_cleanup(RERR_MESSAGEIO
);
326 if (progress_is_active
&& !am_server
) {
328 progress_is_active
= 0;
331 trailing_CR_or_NL
= len
&& (buf
[len
-1] == '\n' || buf
[len
-1] == '\r')
335 if (ic
!= (iconv_t
)-1) {
340 INIT_CONST_XBUF(outbuf
, convbuf
);
341 INIT_XBUF(inbuf
, (char*)buf
, len
, -1);
344 iconvbufs(ic
, &inbuf
, &outbuf
, 0);
347 filtered_fwrite(f
, convbuf
, outbuf
.len
, 0);
350 if (!ierrno
|| ierrno
== E2BIG
)
352 fprintf(f
, "\\#%03o", CVAL(inbuf
.buf
, inbuf
.pos
++));
357 filtered_fwrite(f
, buf
, len
, !allow_8bit_chars
);
359 if (trailing_CR_or_NL
) {
360 fputc(trailing_CR_or_NL
, f
);
365 /* This is the rsync debugging function. Call it with FINFO, FERROR_*,
366 * FWARNING, FLOG, or FCLIENT. */
367 void rprintf(enum logcode code
, const char *format
, ...)
370 char buf
[BIGPATHBUFLEN
];
373 va_start(ap
, format
);
374 len
= vsnprintf(buf
, sizeof buf
, format
, ap
);
377 /* Deal with buffer overruns. Instead of panicking, just
378 * truncate the resulting string. (Note that configure ensures
379 * that we have a vsnprintf() that doesn't ever return -1.) */
380 if (len
> sizeof buf
- 1) {
381 static const char ellipsis
[] = "[...]";
383 /* Reset length, and zero-terminate the end of our buffer */
384 len
= sizeof buf
- 1;
387 /* Copy the ellipsis to the end of the string, but give
388 * us one extra character:
390 * v--- null byte at buf[sizeof buf - 1]
392 * -> abcd[...]00 <-- now two null bytes at end
394 * If the input format string has a trailing newline,
395 * we copy it into that extra null; if it doesn't, well,
396 * all we lose is one byte. */
397 memcpy(buf
+len
-sizeof ellipsis
, ellipsis
, sizeof ellipsis
);
398 if (format
[strlen(format
)-1] == '\n') {
403 rwrite(code
, buf
, len
, 0);
406 /* This is like rprintf, but it also tries to print some
407 * representation of the error code. Normally errcode = errno.
409 * Unlike rprintf, this always adds a newline and there should not be
410 * one in the format string.
412 * Note that since strerror might involve dynamically loading a
413 * message catalog we need to call it once before chroot-ing. */
414 void rsyserr(enum logcode code
, int errcode
, const char *format
, ...)
417 char buf
[BIGPATHBUFLEN
];
420 strlcpy(buf
, RSYNC_NAME
": ", sizeof buf
);
421 len
= (sizeof RSYNC_NAME
": ") - 1;
423 va_start(ap
, format
);
424 len
+= vsnprintf(buf
+ len
, sizeof buf
- len
, format
, ap
);
427 if (len
< sizeof buf
) {
428 len
+= snprintf(buf
+ len
, sizeof buf
- len
,
429 ": %s (%d)\n", strerror(errcode
), errcode
);
431 if (len
>= sizeof buf
)
432 exit_cleanup(RERR_MESSAGEIO
);
434 rwrite(code
, buf
, len
, 0);
437 void rflush(enum logcode code
)
441 if (am_daemon
|| code
== FLOG
)
444 if (code
== FINFO
&& !am_server
)
452 /* A generic logging routine for send/recv, with parameter substitiution. */
453 static void log_formatted(enum logcode code
, const char *format
, const char *op
,
454 struct file_struct
*file
, const char *fname
,
455 struct stats
*initial_stats
, int iflags
,
458 char buf
[MAXPATHLEN
+1024], buf2
[MAXPATHLEN
], fmt
[32];
466 /* We expand % codes one by one in place in buf. We don't
467 * copy in the terminating null of the inserted strings, but
468 * rather keep going until we reach the null of the format. */
469 total
= strlcpy(buf
, format
, sizeof buf
);
470 if (total
> MAXPATHLEN
) {
471 rprintf(FERROR
, "log-format string is WAY too long!\n");
472 exit_cleanup(RERR_MESSAGEIO
);
477 for (p
= buf
; (p
= strchr(p
, '%')) != NULL
; ) {
482 while (isDigit(p
) && c
- fmt
< (int)(sizeof fmt
) - 8)
499 strlcat(fmt
, ".0f", sizeof fmt
);
500 snprintf(buf2
, sizeof buf2
, fmt
,
501 (double)F_LENGTH(file
));
505 strlcat(fmt
, "u", sizeof fmt
);
506 snprintf(buf2
, sizeof buf2
, fmt
,
507 uid_ndx
? F_OWNER(file
) : 0);
511 if (!gid_ndx
|| file
->flags
& FLAG_SKIP_GROUP
)
514 strlcat(fmt
, "u", sizeof fmt
);
515 snprintf(buf2
, sizeof buf2
, fmt
,
521 strlcat(fmt
, "ld", sizeof fmt
);
522 snprintf(buf2
, sizeof buf2
, fmt
,
527 n
= c
= timestring(file
->modtime
);
528 while ((c
= strchr(c
, ' ')) != NULL
)
532 c
= buf2
+ MAXPATHLEN
- PERMSTRING_SIZE
- 1;
533 permstring(c
, file
->mode
);
534 n
= c
+ 1; /* skip the type char */
542 strlcpy(c
, fname
, MAXPATHLEN
);
544 c
= f_name(file
, NULL
);
545 if (am_sender
&& F_PATHNAME(file
)) {
546 pathjoin(buf2
, sizeof buf2
,
547 F_PATHNAME(file
), c
);
548 clean_fname(buf2
, 0);
550 strlcpy(c
, buf2
, MAXPATHLEN
);
554 } else if (am_daemon
&& *c
!= '/') {
555 pathjoin(buf2
, sizeof buf2
,
556 curr_dir
+ module_dirlen
, c
);
557 clean_fname(buf2
, 0);
559 strlcpy(c
, buf2
, MAXPATHLEN
);
573 strlcpy(c
, fname
, MAXPATHLEN
);
575 c
= f_name(file
, NULL
);
576 if (S_ISDIR(file
->mode
))
577 strlcat(c
, "/", MAXPATHLEN
);
581 if (hlink
&& *hlink
) {
583 strlcpy(buf2
, " => ", sizeof buf2
);
584 } else if (S_ISLNK(file
->mode
) && !fname
) {
586 strlcpy(buf2
, " -> ", sizeof buf2
);
591 strlcpy(buf2
, " ", sizeof buf2
);
593 strlcat(fmt
, "s", sizeof fmt
);
594 snprintf(buf2
+ 4, sizeof buf2
- 4, fmt
, n
);
598 n
= lp_name(module_id
);
601 n
= timestring(time(NULL
));
604 n
= full_module_path
;
611 b
= stats
.total_written
-
612 initial_stats
->total_written
;
614 b
= stats
.total_read
-
615 initial_stats
->total_read
;
617 strlcat(fmt
, ".0f", sizeof fmt
);
618 snprintf(buf2
, sizeof buf2
, fmt
, (double)b
);
623 b
= stats
.total_written
-
624 initial_stats
->total_written
;
626 b
= stats
.total_read
-
627 initial_stats
->total_read
;
629 strlcat(fmt
, ".0f", sizeof fmt
);
630 snprintf(buf2
, sizeof buf2
, fmt
, (double)b
);
634 if (iflags
& ITEM_DELETED
) {
638 n
= c
= buf2
+ MAXPATHLEN
- 32;
639 c
[0] = iflags
& ITEM_LOCAL_CHANGE
640 ? iflags
& ITEM_XNAME_FOLLOWS
? 'h' : 'c'
641 : !(iflags
& ITEM_TRANSFER
) ? '.'
642 : !local_server
&& *op
== 's' ? '<' : '>';
643 if (S_ISLNK(file
->mode
)) {
646 c
[4] = !(iflags
& ITEM_REPORT_TIME
) ? '.'
647 : !preserve_times
|| !receiver_symlink_times
648 || (iflags
& ITEM_REPORT_TIMEFAIL
) ? 'T' : 't';
650 c
[1] = S_ISDIR(file
->mode
) ? 'd'
651 : IS_SPECIAL(file
->mode
) ? 'S'
652 : IS_DEVICE(file
->mode
) ? 'D' : 'f';
653 c
[3] = !(iflags
& ITEM_REPORT_SIZE
) ? '.' : 's';
654 c
[4] = !(iflags
& ITEM_REPORT_TIME
) ? '.'
655 : !preserve_times
? 'T' : 't';
657 c
[2] = !(iflags
& ITEM_REPORT_CHANGE
) ? '.' : 'c';
658 c
[5] = !(iflags
& ITEM_REPORT_PERMS
) ? '.' : 'p';
659 c
[6] = !(iflags
& ITEM_REPORT_OWNER
) ? '.' : 'o';
660 c
[7] = !(iflags
& ITEM_REPORT_GROUP
) ? '.' : 'g';
661 c
[8] = !(iflags
& ITEM_REPORT_ATIME
) ? '.' : 'u';
662 c
[9] = !(iflags
& ITEM_REPORT_ACL
) ? '.' : 'a';
663 c
[10] = !(iflags
& ITEM_REPORT_XATTR
) ? '.' : 'x';
666 if (iflags
& (ITEM_IS_NEW
|ITEM_MISSING_DATA
)) {
667 char ch
= iflags
& ITEM_IS_NEW
? '+' : '?';
669 for (i
= 2; c
[i
]; i
++)
671 } else if (c
[0] == '.' || c
[0] == 'h' || c
[0] == 'c') {
673 for (i
= 2; c
[i
]; i
++) {
678 for (i
= 2; c
[i
]; i
++)
685 /* "n" is the string to be inserted in place of this % code. */
688 if (n
!= buf2
&& fmt
[1]) {
689 strlcat(fmt
, "s", sizeof fmt
);
690 snprintf(buf2
, sizeof buf2
, fmt
, n
);
695 /* Subtract the length of the escape from the string's size. */
698 if (len
+ total
>= (size_t)sizeof buf
) {
700 "buffer overflow expanding %%%c -- exiting\n",
702 exit_cleanup(RERR_MESSAGEIO
);
705 /* Shuffle the rest of the string along to make space for n */
706 if (len
!= (size_t)(p
- s
+ 1))
707 memmove(s
+ len
, p
+ 1, total
- (s
- buf
) + 1);
710 /* Insert the contents of string "n", but NOT its null. */
714 /* Skip over inserted string; continue looking */
718 rwrite(code
, buf
, total
, 0);
721 /* Return 1 if the format escape is in the log-format string (e.g. look for
722 * the 'b' in the "%9b" format escape). */
723 int log_format_has(const char *format
, char esc
)
730 for (p
= format
; (p
= strchr(p
, '%')) != NULL
; ) {
743 /* Log the transfer of a file. If the code is FCLIENT, the output just goes
744 * to stdout. If it is FLOG, it just goes to the log file. Otherwise we
746 void log_item(enum logcode code
, struct file_struct
*file
,
747 struct stats
*initial_stats
, int iflags
, const char *hlink
)
749 const char *s_or_r
= am_sender
? "send" : "recv";
751 if (code
!= FLOG
&& stdout_format
&& !am_server
) {
752 log_formatted(FCLIENT
, stdout_format
, s_or_r
,
753 file
, NULL
, initial_stats
, iflags
, hlink
);
755 if (code
!= FCLIENT
&& logfile_format
&& *logfile_format
) {
756 log_formatted(FLOG
, logfile_format
, s_or_r
,
757 file
, NULL
, initial_stats
, iflags
, hlink
);
761 void maybe_log_item(struct file_struct
*file
, int iflags
, int itemizing
,
764 int significant_flags
= iflags
& SIGNIFICANT_ITEM_FLAGS
;
765 int see_item
= itemizing
&& (significant_flags
|| *buf
766 || stdout_format_has_i
> 1 || (verbose
> 1 && stdout_format_has_i
));
767 int local_change
= iflags
& ITEM_LOCAL_CHANGE
&& significant_flags
;
769 if (logfile_name
&& !dry_run
&& see_item
770 && (significant_flags
|| logfile_format_has_i
))
771 log_item(FLOG
, file
, &stats
, iflags
, buf
);
772 } else if (see_item
|| local_change
|| *buf
773 || (S_ISDIR(file
->mode
) && significant_flags
)) {
774 enum logcode code
= significant_flags
|| logfile_format_has_i
? FINFO
: FCLIENT
;
775 log_item(code
, file
, &stats
, iflags
, buf
);
779 void log_delete(const char *fname
, int mode
)
782 union file_extras ex
[4]; /* just in case... */
783 struct file_struct file
;
785 int len
= strlen(fname
);
790 if (!verbose
&& !stdout_format
)
792 else if (am_server
&& protocol_version
>= 29 && len
< MAXPATHLEN
) {
794 len
++; /* directories include trailing null */
795 send_msg(MSG_DELETED
, fname
, len
, am_generator
);
797 fmt
= stdout_format_has_o_or_i
? stdout_format
: "deleting %n";
798 log_formatted(FCLIENT
, fmt
, "del.", &x
.file
, fname
, &stats
,
802 if (!logfile_name
|| dry_run
|| !logfile_format
)
805 fmt
= logfile_format_has_o_or_i
? logfile_format
: "deleting %n";
806 log_formatted(FLOG
, fmt
, "del.", &x
.file
, fname
, &stats
, ITEM_DELETED
, NULL
);
810 * Called when the transfer is interrupted for some reason.
812 * Code is one of the RERR_* codes from errcode.h, or terminating
815 void log_exit(int code
, const char *file
, int line
)
818 rprintf(FLOG
,"sent %.0f bytes received %.0f bytes total size %.0f\n",
819 (double)stats
.total_written
,
820 (double)stats
.total_read
,
821 (double)stats
.total_size
);
822 } else if (am_server
!= 2) {
825 name
= rerr_name(code
);
827 name
= "unexplained error";
829 /* VANISHED is not an error, only a warning */
830 if (code
== RERR_VANISHED
) {
831 rprintf(FWARNING
, "rsync warning: %s (code %d) at %s(%d) [%s=%s]\n",
832 name
, code
, file
, line
, who_am_i(), RSYNC_VERSION
);
834 rprintf(FERROR
, "rsync error: %s (code %d) at %s(%d) [%s=%s]\n",
835 name
, code
, file
, line
, who_am_i(), RSYNC_VERSION
);