1 /* -*- c-file-style: "linux"; -*-
3 Copyright (C) 1998-2001 by Andrew Tridgell <tridge@samba.org>
4 Copyright (C) 2000-2001 by Martin Pool <mbp@samba.org>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 Logging and utility functions.
25 Mapping to human-readable messages added by Martin Pool
26 <mbp@samba.org>, Oct 2000.
35 extern int local_server
;
38 extern int msg_fd_out
;
39 extern int protocol_version
;
40 extern int preserve_times
;
41 extern int log_format_has_o_or_i
;
42 extern int daemon_log_format_has_o_or_i
;
43 extern char *auth_user
;
44 extern char *log_format
;
46 static int log_initialised
;
47 static int logfile_was_closed
;
48 static char *logfname
;
52 int log_got_error
= 0;
57 } const rerr_names
[] = {
58 { RERR_SYNTAX
, "syntax or usage error" },
59 { RERR_PROTOCOL
, "protocol incompatibility" },
60 { RERR_FILESELECT
, "errors selecting input/output files, dirs" },
61 { RERR_UNSUPPORTED
, "requested action not supported" },
62 { RERR_STARTCLIENT
, "error starting client-server protocol" },
63 { RERR_SOCKETIO
, "error in socket IO" },
64 { RERR_FILEIO
, "error in file IO" },
65 { RERR_STREAMIO
, "error in rsync protocol data stream" },
66 { RERR_MESSAGEIO
, "errors with program diagnostics" },
67 { RERR_IPC
, "error in IPC code" },
68 { RERR_SIGNAL
, "received SIGUSR1 or SIGINT" },
69 { RERR_WAITCHILD
, "some error returned by waitpid()" },
70 { RERR_MALLOC
, "error allocating core memory buffers" },
71 { RERR_PARTIAL
, "some files could not be transferred" },
72 { RERR_VANISHED
, "some files vanished before they could be transferred" },
73 { RERR_TIMEOUT
, "timeout in data send/receive" },
74 { RERR_CMD_FAILED
, "remote shell failed" },
75 { RERR_CMD_KILLED
, "remote shell killed" },
76 { RERR_CMD_RUN
, "remote command could not be run" },
77 { RERR_CMD_NOTFOUND
,"remote command not found" },
78 { RERR_DEL_LIMIT
, "the --max-delete limit stopped deletions" },
85 * Map from rsync error code to name, or return NULL.
87 static char const *rerr_name(int code
)
90 for (i
= 0; rerr_names
[i
].name
; i
++) {
91 if (rerr_names
[i
].code
== code
)
92 return rerr_names
[i
].name
;
98 static void logit(int priority
, char *buf
)
100 if (logfile_was_closed
)
103 fprintf(logfile
,"%s [%d] %s",
104 timestring(time(NULL
)), (int)getpid(), buf
);
107 syslog(priority
, "%s", buf
);
111 static void syslog_init()
113 static int been_here
= 0;
114 int options
= LOG_PID
;
121 options
|= LOG_NDELAY
;
125 openlog("rsyncd", options
, lp_syslog_facility());
127 openlog("rsyncd", options
);
131 logit(LOG_INFO
, "rsyncd started\n");
135 static void logfile_open(void)
137 extern int orig_umask
;
138 int old_umask
= umask(022 | orig_umask
);
139 logfile
= fopen(logfname
, "a");
142 int fopen_errno
= errno
;
143 /* Rsync falls back to using syslog on failure. */
145 rsyserr(FERROR
, fopen_errno
,
146 "failed to open log-file %s", logfname
);
147 rprintf(FINFO
, "Ignoring \"log file\" setting.\n");
159 /* this looks pointless, but it is needed in order for the
160 * C library on some systems to fetch the timezone info
161 * before the chroot */
165 /* optionally use a log file instead of syslog */
166 logfname
= lp_log_file();
167 if (logfname
&& *logfname
)
173 void logfile_close(void)
176 logfile_was_closed
= 1;
182 void logfile_reopen(void)
184 if (logfile_was_closed
) {
185 logfile_was_closed
= 0;
190 /* this is the underlying (unformatted) rsync debugging function. Call
191 * it with FINFO, FERROR or FLOG */
192 void rwrite(enum logcode code
, char *buf
, int len
)
195 /* recursion can happen with certain fatal conditions */
197 if (quiet
&& code
== FINFO
)
201 exit_cleanup(RERR_MESSAGEIO
);
205 if (am_server
&& msg_fd_out
>= 0) {
206 /* Pass the message to our sibling. */
207 send_msg((enum msgcode
)code
, buf
, len
);
213 else if (am_daemon
) {
216 int priority
= code
== FERROR
? LOG_WARNING
: LOG_INFO
;
221 if (!log_initialised
)
223 strlcpy(msg
, buf
, MIN((int)sizeof msg
, len
+ 1));
224 logit(priority
, msg
);
227 if (code
== FLOG
|| !am_server
)
229 } else if (code
== FLOG
)
233 /* Pass the message to the non-server side. */
234 if (io_multiplex_write((enum msgcode
)code
, buf
, len
))
237 /* TODO: can we send the error to the user somehow? */
242 if (code
== FERROR
) {
248 f
= am_server
? stderr
: stdout
;
251 exit_cleanup(RERR_MESSAGEIO
);
253 if (fwrite(buf
, len
, 1, f
) != 1)
254 exit_cleanup(RERR_MESSAGEIO
);
256 if (buf
[len
-1] == '\r' || buf
[len
-1] == '\n')
261 /* This is the rsync debugging function. Call it with FINFO, FERROR or
263 void rprintf(enum logcode code
, const char *format
, ...)
266 char buf
[MAXPATHLEN
+512];
269 va_start(ap
, format
);
270 len
= vsnprintf(buf
, sizeof buf
, format
, ap
);
273 /* Deal with buffer overruns. Instead of panicking, just
274 * truncate the resulting string. (Note that configure ensures
275 * that we have a vsnprintf() that doesn't ever return -1.) */
276 if (len
> sizeof buf
- 1) {
277 const char ellipsis
[] = "[...]";
279 /* Reset length, and zero-terminate the end of our buffer */
280 len
= sizeof buf
- 1;
283 /* Copy the ellipsis to the end of the string, but give
284 * us one extra character:
286 * v--- null byte at buf[sizeof buf - 1]
288 * -> abcd[...]00 <-- now two null bytes at end
290 * If the input format string has a trailing newline,
291 * we copy it into that extra null; if it doesn't, well,
292 * all we lose is one byte. */
293 strncpy(buf
+len
-sizeof ellipsis
, ellipsis
, sizeof ellipsis
);
294 if (format
[strlen(format
)-1] == '\n') {
299 rwrite(code
, buf
, len
);
303 /* This is like rprintf, but it also tries to print some
304 * representation of the error code. Normally errcode = errno.
306 * Unlike rprintf, this always adds a newline and there should not be
307 * one in the format string.
309 * Note that since strerror might involve dynamically loading a
310 * message catalog we need to call it once before chroot-ing. */
311 void rsyserr(enum logcode code
, int errcode
, const char *format
, ...)
314 char buf
[MAXPATHLEN
+512];
317 strcpy(buf
, RSYNC_NAME
": ");
318 len
= (sizeof RSYNC_NAME
": ") - 1;
320 va_start(ap
, format
);
321 len
+= vsnprintf(buf
+ len
, sizeof buf
- len
, format
, ap
);
324 if (len
< sizeof buf
) {
325 len
+= snprintf(buf
+ len
, sizeof buf
- len
,
326 ": %s (%d)\n", strerror(errcode
), errcode
);
328 if (len
>= sizeof buf
)
329 exit_cleanup(RERR_MESSAGEIO
);
331 rwrite(code
, buf
, len
);
336 void rflush(enum logcode code
)
348 if (code
== FERROR
) {
359 if (!f
) exit_cleanup(RERR_MESSAGEIO
);
365 /* a generic logging routine for send/recv, with parameter
367 static void log_formatted(enum logcode code
, char *format
, char *op
,
368 struct file_struct
*file
, struct stats
*initial_stats
,
369 int iflags
, char *hlink
)
371 char buf
[MAXPATHLEN
+1024], buf2
[MAXPATHLEN
], fmt
[32];
378 /* We expand % codes one by one in place in buf. We don't
379 * copy in the terminating null of the inserted strings, but
380 * rather keep going until we reach the null of the format. */
381 total
= strlcpy(buf
, format
, sizeof buf
);
382 if (total
> MAXPATHLEN
) {
383 rprintf(FERROR
, "log-format string is WAY too long!\n");
384 exit_cleanup(RERR_MESSAGEIO
);
389 for (p
= buf
; (p
= strchr(p
, '%')) != NULL
; ) {
394 while (isdigit(*(uchar
*)p
) && n
- fmt
< (int)(sizeof fmt
) - 8)
402 case 'h': if (am_daemon
) n
= client_name(0); break;
403 case 'a': if (am_daemon
) n
= client_addr(0); break;
405 strlcat(fmt
, ".0f", sizeof fmt
);
406 snprintf(buf2
, sizeof buf2
, fmt
,
407 (double)file
->length
);
411 strlcat(fmt
, "ld", sizeof fmt
);
412 snprintf(buf2
, sizeof buf2
, fmt
,
416 case 'o': n
= op
; break;
418 n
= safe_fname(f_name(file
));
419 if (am_sender
&& file
->dir
.root
) {
420 pathjoin(buf2
, sizeof buf2
,
422 /* The buffer from safe_fname() has more
423 * room than MAXPATHLEN, so this is safe. */
434 n
= safe_fname(f_name(file
));
435 if (S_ISDIR(file
->mode
)) {
436 /* The buffer from safe_fname() has more
437 * room than MAXPATHLEN, so this is safe. */
442 if (hlink
&& *hlink
) {
443 n
= safe_fname(hlink
);
444 strcpy(buf2
, " => ");
445 } else if (S_ISLNK(file
->mode
) && file
->u
.link
) {
446 n
= safe_fname(file
->u
.link
);
447 strcpy(buf2
, " -> ");
454 strlcat(fmt
, "s", sizeof fmt
);
455 snprintf(buf2
+ 4, sizeof buf2
- 4, fmt
, n
);
458 case 'm': n
= lp_name(module_id
); break;
459 case 't': n
= timestring(time(NULL
)); break;
460 case 'P': n
= lp_path(module_id
); break;
461 case 'u': n
= auth_user
; break;
464 b
= stats
.total_written
-
465 initial_stats
->total_written
;
467 b
= stats
.total_read
-
468 initial_stats
->total_read
;
470 strlcat(fmt
, ".0f", sizeof fmt
);
471 snprintf(buf2
, sizeof buf2
, fmt
, (double)b
);
476 b
= stats
.total_written
-
477 initial_stats
->total_written
;
479 b
= stats
.total_read
-
480 initial_stats
->total_read
;
482 strlcat(fmt
, ".0f", sizeof fmt
);
483 snprintf(buf2
, sizeof buf2
, fmt
, (double)b
);
487 if (iflags
& ITEM_DELETED
) {
491 n
= buf2
+ MAXPATHLEN
- 32;
492 n
[0] = iflags
& ITEM_LOCAL_CHANGE
493 ? iflags
& ITEM_XNAME_FOLLOWS
? 'h' : 'c'
494 : !(iflags
& ITEM_TRANSFER
) ? '.'
495 : !local_server
&& *op
== 's' ? '<' : '>';
496 n
[1] = S_ISDIR(file
->mode
) ? 'd'
497 : IS_DEVICE(file
->mode
) ? 'D'
498 : S_ISLNK(file
->mode
) ? 'L' : 'f';
499 n
[2] = !(iflags
& ITEM_REPORT_CHECKSUM
) ? '.' : 'c';
500 n
[3] = !(iflags
& ITEM_REPORT_SIZE
) ? '.' : 's';
501 n
[4] = !(iflags
& ITEM_REPORT_TIME
) ? '.'
502 : !preserve_times
|| IS_DEVICE(file
->mode
)
503 || S_ISLNK(file
->mode
) ? 'T' : 't';
504 n
[5] = !(iflags
& ITEM_REPORT_PERMS
) ? '.' : 'p';
505 n
[6] = !(iflags
& ITEM_REPORT_OWNER
) ? '.' : 'o';
506 n
[7] = !(iflags
& ITEM_REPORT_GROUP
) ? '.' : 'g';
507 n
[8] = !(iflags
& ITEM_REPORT_XATTRS
) ? '.' : 'a';
510 if (iflags
& (ITEM_IS_NEW
|ITEM_MISSING_DATA
)) {
511 char ch
= iflags
& ITEM_IS_NEW
? '+' : '?';
513 for (i
= 2; n
[i
]; i
++)
515 } else if (!(iflags
& (ITEM_TRANSFER
|ITEM_LOCAL_CHANGE
))) {
517 for (i
= 2; n
[i
]; i
++) {
522 for (i
= 2; n
[i
]; i
++)
529 /* "n" is the string to be inserted in place of this % code. */
532 if (n
!= buf2
&& fmt
[1]) {
533 strlcat(fmt
, "s", sizeof fmt
);
534 snprintf(buf2
, sizeof buf2
, fmt
, n
);
539 /* Subtract the length of the escape from the string's size. */
542 if (len
+ total
>= (size_t)sizeof buf
) {
544 "buffer overflow expanding %%%c -- exiting\n",
546 exit_cleanup(RERR_MESSAGEIO
);
549 /* Shuffle the rest of the string along to make space for n */
550 if (len
!= (size_t)(p
- s
+ 1))
551 memmove(s
+ len
, p
+ 1, total
- (s
- buf
) + 1);
554 /* Insert the contents of string "n", but NOT its null. */
558 /* Skip over inserted string; continue looking */
562 rwrite(code
, buf
, total
);
565 /* Return 1 if the format escape is in the log-format string (e.g. look for
566 * the 'b' in the "%9b" format escape). */
567 int log_format_has(const char *format
, char esc
)
574 for (p
= format
; (p
= strchr(p
, '%')) != NULL
; ) {
577 while (isdigit(*(uchar
*)p
))
587 /* log the transfer of a file */
588 void log_item(struct file_struct
*file
, struct stats
*initial_stats
,
589 int iflags
, char *hlink
)
591 char *s_or_r
= am_sender
? "send" : "recv";
593 if (lp_transfer_logging(module_id
)) {
594 log_formatted(FLOG
, lp_log_format(module_id
), s_or_r
,
595 file
, initial_stats
, iflags
, hlink
);
596 } else if (log_format
&& !am_server
) {
597 log_formatted(FINFO
, log_format
, s_or_r
,
598 file
, initial_stats
, iflags
, hlink
);
602 void maybe_log_item(struct file_struct
*file
, int iflags
, int itemizing
,
605 int see_item
= itemizing
&& (iflags
|| verbose
> 1);
607 if (am_daemon
&& !dry_run
&& see_item
)
608 log_item(file
, &stats
, iflags
, buf
);
609 } else if (see_item
|| iflags
& ITEM_LOCAL_CHANGE
|| *buf
610 || (S_ISDIR(file
->mode
) && iflags
& SIGNIFICANT_ITEM_FLAGS
))
611 log_item(file
, &stats
, iflags
, buf
);
614 void log_delete(char *fname
, int mode
)
616 static struct file_struct file
;
617 int len
= strlen(fname
);
621 file
.basename
= fname
;
623 if (!verbose
&& !log_format
)
625 else if (am_server
&& protocol_version
>= 29 && len
< MAXPATHLEN
) {
627 len
++; /* directories include trailing null */
628 send_msg(MSG_DELETED
, fname
, len
);
630 fmt
= log_format_has_o_or_i
? log_format
: "deleting %n";
631 log_formatted(FCLIENT
, fmt
, "del.", &file
, &stats
,
635 if (!am_daemon
|| dry_run
|| !lp_transfer_logging(module_id
))
638 fmt
= daemon_log_format_has_o_or_i
? lp_log_format(module_id
) : "deleting %n";
639 log_formatted(FLOG
, fmt
, "del.", &file
, &stats
, ITEM_DELETED
, NULL
);
644 * Called when the transfer is interrupted for some reason.
646 * Code is one of the RERR_* codes from errcode.h, or terminating
649 void log_exit(int code
, const char *file
, int line
)
652 rprintf(FLOG
,"sent %.0f bytes received %.0f bytes total size %.0f\n",
653 (double)stats
.total_written
,
654 (double)stats
.total_read
,
655 (double)stats
.total_size
);
659 name
= rerr_name(code
);
661 name
= "unexplained error";
663 /* VANISHED is not an error, only a warning */
664 if (code
== RERR_VANISHED
) {
665 rprintf(FINFO
, "rsync warning: %s (code %d) at %s(%d)\n",
666 name
, code
, file
, line
);
668 rprintf(FERROR
, "rsync error: %s (code %d) at %s(%d)\n",
669 name
, code
, file
, line
);