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-2007 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 stdout_format_has_i
;
41 extern int stdout_format_has_o_or_i
;
42 extern int logfile_format_has_i
;
43 extern int logfile_format_has_o_or_i
;
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_send
, ic_recv
;
55 extern char curr_dir
[];
56 extern char *module_dir
;
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 could not be transferred" },
87 { RERR_VANISHED
, "some files vanished before they could be transferred" },
88 { RERR_TIMEOUT
, "timeout in data send/receive" },
89 { RERR_CMD_FAILED
, "remote shell failed" },
90 { RERR_CMD_KILLED
, "remote shell killed" },
91 { RERR_CMD_RUN
, "remote command could not be run" },
92 { RERR_CMD_NOTFOUND
,"remote command not found" },
93 { RERR_DEL_LIMIT
, "the --max-delete limit stopped deletions" },
98 * Map from rsync error code to name, or return NULL.
100 static char const *rerr_name(int code
)
103 for (i
= 0; rerr_names
[i
].name
; i
++) {
104 if (rerr_names
[i
].code
== code
)
105 return rerr_names
[i
].name
;
110 static void logit(int priority
, const char *buf
)
112 if (logfile_was_closed
)
115 fprintf(logfile_fp
, "%s [%d] %s",
116 timestring(time(NULL
)), (int)getpid(), buf
);
119 syslog(priority
, "%s", buf
);
123 static void syslog_init()
125 static int been_here
= 0;
126 int options
= LOG_PID
;
133 options
|= LOG_NDELAY
;
137 openlog("rsyncd", options
, lp_syslog_facility(module_id
));
139 openlog("rsyncd", options
);
143 logit(LOG_INFO
, "rsyncd started\n");
147 static void logfile_open(void)
149 mode_t old_umask
= umask(022 | orig_umask
);
150 logfile_fp
= fopen(logfile_name
, "a");
153 int fopen_errno
= errno
;
154 /* Rsync falls back to using syslog on failure. */
156 rsyserr(FERROR
, fopen_errno
,
157 "failed to open log-file %s", logfile_name
);
158 rprintf(FINFO
, "Ignoring \"log file\" setting.\n");
162 void log_init(int restart
)
164 if (log_initialised
) {
167 if (strcmp(logfile_name
, lp_log_file(module_id
)) != 0) {
174 } else if (*logfile_name
)
175 return; /* unchanged, non-empty "log file" names */
176 else if (lp_syslog_facility(-1) != lp_syslog_facility(module_id
))
179 return; /* unchanged syslog settings */
183 /* This looks pointless, but it is needed in order for the
184 * C library on some systems to fetch the timezone info
185 * before the chroot. */
186 timestring(time(NULL
));
188 /* Optionally use a log file instead of syslog. (Non-daemon
189 * rsyncs will have already set logfile_name, as needed.) */
190 if (am_daemon
&& !logfile_name
)
191 logfile_name
= lp_log_file(module_id
);
192 if (logfile_name
&& *logfile_name
)
198 void logfile_close(void)
201 logfile_was_closed
= 1;
207 void logfile_reopen(void)
209 if (logfile_was_closed
) {
210 logfile_was_closed
= 0;
215 static void filtered_fwrite(FILE *f
, const char *buf
, int len
, int use_isprint
)
217 const char *s
, *end
= buf
+ len
;
218 for (s
= buf
; s
< end
; s
++) {
220 && *s
== '\\' && s
[1] == '#'
225 && ((use_isprint
&& !isPrint(s
))
226 || *(uchar
*)s
< ' '))) {
227 if (s
!= buf
&& fwrite(buf
, s
- buf
, 1, f
) != 1)
228 exit_cleanup(RERR_MESSAGEIO
);
229 fprintf(f
, "\\#%03o", *(uchar
*)s
);
233 if (buf
!= end
&& fwrite(buf
, end
- buf
, 1, f
) != 1)
234 exit_cleanup(RERR_MESSAGEIO
);
237 /* this is the underlying (unformatted) rsync debugging function. Call
238 * it with FINFO, FERROR_*, FWARNING, FLOG, or FCLIENT. Note: recursion
239 * can happen with certain fatal conditions. */
240 void rwrite(enum logcode code
, const char *buf
, int len
, int is_utf8
)
242 int trailing_CR_or_NL
;
245 iconv_t ic
= is_utf8
&& ic_recv
!= (iconv_t
)-1 ? ic_recv
: ic_chck
;
248 iconv_t ic
= ic_chck
;
253 exit_cleanup(RERR_MESSAGEIO
);
255 if (am_server
&& msg_fd_out
>= 0) {
257 /* Pass the message to our sibling. */
258 send_msg((enum msgcode
)code
, buf
, len
, 0);
262 if (code
== FERROR_SOCKET
) /* This gets simplified for a non-sibling. */
267 else if (am_daemon
|| logfile_name
) {
270 int priority
= code
== FINFO
|| code
== FLOG
? LOG_INFO
: LOG_WARNING
;
275 if (!log_initialised
)
277 strlcpy(msg
, buf
, MIN((int)sizeof msg
, len
+ 1));
278 logit(priority
, msg
);
281 if (code
== FLOG
|| (am_daemon
&& !am_server
))
283 } else if (code
== FLOG
)
286 if (quiet
&& code
== FINFO
)
290 enum msgcode msg
= (enum msgcode
)code
;
291 if (protocol_version
< 30) {
292 if (msg
== MSG_ERROR
)
293 msg
= MSG_ERROR_XFER
;
294 else if (msg
== MSG_WARNING
)
297 /* Pass the message to the non-server side. */
298 if (send_msg(msg
, buf
, len
, !is_utf8
))
301 /* TODO: can we send the error to the user somehow? */
315 f
= am_server
? stderr
: stdout
;
318 exit_cleanup(RERR_MESSAGEIO
);
321 trailing_CR_or_NL
= len
&& (buf
[len
-1] == '\n' || buf
[len
-1] == '\r')
325 if (ic
!= (iconv_t
)-1) {
330 INIT_CONST_XBUF(outbuf
, convbuf
);
331 INIT_XBUF(inbuf
, (char*)buf
, len
, -1);
334 iconvbufs(ic
, &inbuf
, &outbuf
, 0);
337 filtered_fwrite(f
, convbuf
, outbuf
.len
, 0);
340 if (!ierrno
|| ierrno
== E2BIG
)
342 fprintf(f
, "\\#%03o", CVAL(inbuf
.buf
, inbuf
.pos
++));
347 filtered_fwrite(f
, buf
, len
, !allow_8bit_chars
);
349 if (trailing_CR_or_NL
) {
350 fputc(trailing_CR_or_NL
, f
);
355 /* This is the rsync debugging function. Call it with FINFO, FERROR_*,
356 * FWARNING, FLOG, or FCLIENT. */
357 void rprintf(enum logcode code
, const char *format
, ...)
360 char buf
[BIGPATHBUFLEN
];
363 va_start(ap
, format
);
364 len
= vsnprintf(buf
, sizeof buf
, format
, ap
);
367 /* Deal with buffer overruns. Instead of panicking, just
368 * truncate the resulting string. (Note that configure ensures
369 * that we have a vsnprintf() that doesn't ever return -1.) */
370 if (len
> sizeof buf
- 1) {
371 static const char ellipsis
[] = "[...]";
373 /* Reset length, and zero-terminate the end of our buffer */
374 len
= sizeof buf
- 1;
377 /* Copy the ellipsis to the end of the string, but give
378 * us one extra character:
380 * v--- null byte at buf[sizeof buf - 1]
382 * -> abcd[...]00 <-- now two null bytes at end
384 * If the input format string has a trailing newline,
385 * we copy it into that extra null; if it doesn't, well,
386 * all we lose is one byte. */
387 memcpy(buf
+len
-sizeof ellipsis
, ellipsis
, sizeof ellipsis
);
388 if (format
[strlen(format
)-1] == '\n') {
393 rwrite(code
, buf
, len
, 0);
396 /* This is like rprintf, but it also tries to print some
397 * representation of the error code. Normally errcode = errno.
399 * Unlike rprintf, this always adds a newline and there should not be
400 * one in the format string.
402 * Note that since strerror might involve dynamically loading a
403 * message catalog we need to call it once before chroot-ing. */
404 void rsyserr(enum logcode code
, int errcode
, const char *format
, ...)
407 char buf
[BIGPATHBUFLEN
];
410 strlcpy(buf
, RSYNC_NAME
": ", sizeof buf
);
411 len
= (sizeof RSYNC_NAME
": ") - 1;
413 va_start(ap
, format
);
414 len
+= vsnprintf(buf
+ len
, sizeof buf
- len
, format
, ap
);
417 if (len
< sizeof buf
) {
418 len
+= snprintf(buf
+ len
, sizeof buf
- len
,
419 ": %s (%d)\n", strerror(errcode
), errcode
);
421 if (len
>= sizeof buf
)
422 exit_cleanup(RERR_MESSAGEIO
);
424 rwrite(code
, buf
, len
, 0);
427 void rflush(enum logcode code
)
431 if (am_daemon
|| code
== FLOG
)
434 if (code
== FINFO
&& !am_server
)
442 /* A generic logging routine for send/recv, with parameter substitiution. */
443 static void log_formatted(enum logcode code
, const char *format
, const char *op
,
444 struct file_struct
*file
, const char *fname
,
445 struct stats
*initial_stats
, int iflags
,
448 char buf
[MAXPATHLEN
+1024], buf2
[MAXPATHLEN
], fmt
[32];
456 /* We expand % codes one by one in place in buf. We don't
457 * copy in the terminating null of the inserted strings, but
458 * rather keep going until we reach the null of the format. */
459 total
= strlcpy(buf
, format
, sizeof buf
);
460 if (total
> MAXPATHLEN
) {
461 rprintf(FERROR
, "log-format string is WAY too long!\n");
462 exit_cleanup(RERR_MESSAGEIO
);
467 for (p
= buf
; (p
= strchr(p
, '%')) != NULL
; ) {
472 while (isDigit(p
) && c
- fmt
< (int)(sizeof fmt
) - 8)
489 strlcat(fmt
, ".0f", sizeof fmt
);
490 snprintf(buf2
, sizeof buf2
, fmt
,
491 (double)F_LENGTH(file
));
495 strlcat(fmt
, "u", sizeof fmt
);
496 snprintf(buf2
, sizeof buf2
, fmt
,
497 uid_ndx
? F_OWNER(file
) : 0);
501 if (!gid_ndx
|| file
->flags
& FLAG_SKIP_GROUP
)
504 strlcat(fmt
, "u", sizeof fmt
);
505 snprintf(buf2
, sizeof buf2
, fmt
,
511 strlcat(fmt
, "ld", sizeof fmt
);
512 snprintf(buf2
, sizeof buf2
, fmt
,
517 n
= c
= timestring(file
->modtime
);
518 while ((c
= strchr(p
, ' ')) != NULL
)
522 c
= buf2
+ MAXPATHLEN
- PERMSTRING_SIZE
- 1;
523 permstring(c
, file
->mode
);
524 n
= c
+ 1; /* skip the type char */
532 strlcpy(c
, fname
, MAXPATHLEN
);
534 c
= f_name(file
, NULL
);
535 if (am_sender
&& F_PATHNAME(file
)) {
536 pathjoin(buf2
, sizeof buf2
,
537 F_PATHNAME(file
), c
);
538 clean_fname(buf2
, 0);
540 strlcpy(c
, buf2
, MAXPATHLEN
);
544 } else if (am_daemon
&& *c
!= '/') {
545 pathjoin(buf2
, sizeof buf2
,
546 curr_dir
+ module_dirlen
, c
);
547 clean_fname(buf2
, 0);
549 strlcpy(c
, buf2
, MAXPATHLEN
);
563 strlcpy(c
, fname
, MAXPATHLEN
);
565 c
= f_name(file
, NULL
);
566 if (S_ISDIR(file
->mode
))
567 strlcat(c
, "/", MAXPATHLEN
);
571 if (hlink
&& *hlink
) {
573 strlcpy(buf2
, " => ", sizeof buf2
);
574 } else if (S_ISLNK(file
->mode
) && !fname
) {
576 strlcpy(buf2
, " -> ", sizeof buf2
);
581 strlcpy(buf2
, " ", sizeof buf2
);
583 strlcat(fmt
, "s", sizeof fmt
);
584 snprintf(buf2
+ 4, sizeof buf2
- 4, fmt
, n
);
588 n
= lp_name(module_id
);
591 n
= timestring(time(NULL
));
601 b
= stats
.total_written
-
602 initial_stats
->total_written
;
604 b
= stats
.total_read
-
605 initial_stats
->total_read
;
607 strlcat(fmt
, ".0f", sizeof fmt
);
608 snprintf(buf2
, sizeof buf2
, fmt
, (double)b
);
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
);
624 if (iflags
& ITEM_DELETED
) {
628 n
= c
= buf2
+ MAXPATHLEN
- 32;
629 c
[0] = iflags
& ITEM_LOCAL_CHANGE
630 ? iflags
& ITEM_XNAME_FOLLOWS
? 'h' : 'c'
631 : !(iflags
& ITEM_TRANSFER
) ? '.'
632 : !local_server
&& *op
== 's' ? '<' : '>';
633 c
[1] = S_ISDIR(file
->mode
) ? 'd'
634 : IS_SPECIAL(file
->mode
) ? 'S'
635 : IS_DEVICE(file
->mode
) ? 'D'
636 : S_ISLNK(file
->mode
) ? 'L' : 'f';
637 c
[2] = !(iflags
& ITEM_REPORT_CHECKSUM
) ? '.' : 'c';
638 c
[3] = !(iflags
& ITEM_REPORT_SIZE
) ? '.' : 's';
639 c
[4] = !(iflags
& ITEM_REPORT_TIME
) ? '.'
640 : !preserve_times
|| S_ISLNK(file
->mode
) ? 'T' : 't';
641 c
[5] = !(iflags
& ITEM_REPORT_PERMS
) ? '.' : 'p';
642 c
[6] = !(iflags
& ITEM_REPORT_OWNER
) ? '.' : 'o';
643 c
[7] = !(iflags
& ITEM_REPORT_GROUP
) ? '.' : 'g';
644 c
[8] = !(iflags
& ITEM_REPORT_ATIME
) ? '.' : 'u';
645 c
[9] = !(iflags
& ITEM_REPORT_ACL
) ? '.' : 'a';
646 c
[10] = !(iflags
& ITEM_REPORT_XATTR
) ? '.' : 'x';
649 if (iflags
& (ITEM_IS_NEW
|ITEM_MISSING_DATA
)) {
650 char ch
= iflags
& ITEM_IS_NEW
? '+' : '?';
652 for (i
= 2; c
[i
]; i
++)
654 } else if (c
[0] == '.' || c
[0] == 'h' || c
[0] == 'c') {
656 for (i
= 2; c
[i
]; i
++) {
661 for (i
= 2; c
[i
]; i
++)
668 /* "n" is the string to be inserted in place of this % code. */
671 if (n
!= buf2
&& fmt
[1]) {
672 strlcat(fmt
, "s", sizeof fmt
);
673 snprintf(buf2
, sizeof buf2
, fmt
, n
);
678 /* Subtract the length of the escape from the string's size. */
681 if (len
+ total
>= (size_t)sizeof buf
) {
683 "buffer overflow expanding %%%c -- exiting\n",
685 exit_cleanup(RERR_MESSAGEIO
);
688 /* Shuffle the rest of the string along to make space for n */
689 if (len
!= (size_t)(p
- s
+ 1))
690 memmove(s
+ len
, p
+ 1, total
- (s
- buf
) + 1);
693 /* Insert the contents of string "n", but NOT its null. */
697 /* Skip over inserted string; continue looking */
701 rwrite(code
, buf
, total
, 0);
704 /* Return 1 if the format escape is in the log-format string (e.g. look for
705 * the 'b' in the "%9b" format escape). */
706 int log_format_has(const char *format
, char esc
)
713 for (p
= format
; (p
= strchr(p
, '%')) != NULL
; ) {
726 /* Log the transfer of a file. If the code is FCLIENT, the output just goes
727 * to stdout. If it is FLOG, it just goes to the log file. Otherwise we
729 void log_item(enum logcode code
, struct file_struct
*file
,
730 struct stats
*initial_stats
, int iflags
, const char *hlink
)
732 const char *s_or_r
= am_sender
? "send" : "recv";
734 if (code
!= FLOG
&& stdout_format
&& !am_server
) {
735 log_formatted(FCLIENT
, stdout_format
, s_or_r
,
736 file
, NULL
, initial_stats
, iflags
, hlink
);
738 if (code
!= FCLIENT
&& logfile_format
&& *logfile_format
) {
739 log_formatted(FLOG
, logfile_format
, s_or_r
,
740 file
, NULL
, initial_stats
, iflags
, hlink
);
744 void maybe_log_item(struct file_struct
*file
, int iflags
, int itemizing
,
747 int significant_flags
= iflags
& SIGNIFICANT_ITEM_FLAGS
;
748 int see_item
= itemizing
&& (significant_flags
|| *buf
749 || stdout_format_has_i
> 1 || (verbose
> 1 && stdout_format_has_i
));
750 int local_change
= iflags
& ITEM_LOCAL_CHANGE
&& significant_flags
;
752 if (logfile_name
&& !dry_run
&& see_item
753 && (significant_flags
|| logfile_format_has_i
))
754 log_item(FLOG
, file
, &stats
, iflags
, buf
);
755 } else if (see_item
|| local_change
|| *buf
756 || (S_ISDIR(file
->mode
) && significant_flags
)) {
757 enum logcode code
= significant_flags
|| logfile_format_has_i
? FINFO
: FCLIENT
;
758 log_item(code
, file
, &stats
, iflags
, buf
);
762 void log_delete(const char *fname
, int mode
)
765 union file_extras ex
[4]; /* just in case... */
766 struct file_struct file
;
768 int len
= strlen(fname
);
773 if (!verbose
&& !stdout_format
)
775 else if (am_server
&& protocol_version
>= 29 && len
< MAXPATHLEN
) {
777 len
++; /* directories include trailing null */
778 send_msg(MSG_DELETED
, fname
, len
, am_generator
);
780 fmt
= stdout_format_has_o_or_i
? stdout_format
: "deleting %n";
781 log_formatted(FCLIENT
, fmt
, "del.", &x
.file
, fname
, &stats
,
785 if (!logfile_name
|| dry_run
|| !logfile_format
)
788 fmt
= logfile_format_has_o_or_i
? logfile_format
: "deleting %n";
789 log_formatted(FLOG
, fmt
, "del.", &x
.file
, fname
, &stats
, ITEM_DELETED
, NULL
);
793 * Called when the transfer is interrupted for some reason.
795 * Code is one of the RERR_* codes from errcode.h, or terminating
798 void log_exit(int code
, const char *file
, int line
)
801 rprintf(FLOG
,"sent %.0f bytes received %.0f bytes total size %.0f\n",
802 (double)stats
.total_written
,
803 (double)stats
.total_read
,
804 (double)stats
.total_size
);
805 } else if (am_server
!= 2) {
808 name
= rerr_name(code
);
810 name
= "unexplained error";
812 /* VANISHED is not an error, only a warning */
813 if (code
== RERR_VANISHED
) {
814 rprintf(FWARNING
, "rsync warning: %s (code %d) at %s(%d) [%s=%s]\n",
815 name
, code
, file
, line
, who_am_i(), RSYNC_VERSION
);
817 rprintf(FERROR
, "rsync error: %s (code %d) at %s(%d) [%s=%s]\n",
818 name
, code
, file
, line
, who_am_i(), RSYNC_VERSION
);