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.
30 static char *logfname
;
32 static int log_error_fd
= -1;
40 } const rerr_names
[] = {
41 { RERR_SYNTAX
, "syntax or usage error" },
42 { RERR_PROTOCOL
, "protocol incompatibility" },
43 { RERR_FILESELECT
, "errors selecting input/output files, dirs" },
44 { RERR_UNSUPPORTED
, "requested action not supported" },
45 { RERR_STARTCLIENT
, "error starting client-server protocol" },
46 { RERR_SOCKETIO
, "error in socket IO" },
47 { RERR_FILEIO
, "error in file IO" },
48 { RERR_STREAMIO
, "error in rsync protocol data stream" },
49 { RERR_MESSAGEIO
, "errors with program diagnostics" },
50 { RERR_IPC
, "error in IPC code" },
51 { RERR_SIGNAL
, "received SIGUSR1 or SIGINT" },
52 { RERR_WAITCHILD
, "some error returned by waitpid()" },
53 { RERR_MALLOC
, "error allocating core memory buffers" },
54 { RERR_PARTIAL
, "some files could not be transferred" },
55 { RERR_TIMEOUT
, "timeout in data send/receive" },
56 { RERR_CMD_FAILED
, "remote shell failed" },
57 { RERR_CMD_KILLED
, "remote shell killed" },
58 { RERR_CMD_RUN
, "remote command could not be run" },
59 { RERR_CMD_NOTFOUND
, "remote command not found" },
66 * Map from rsync error code to name, or return NULL.
68 static char const *rerr_name(int code
)
71 for (i
= 0; rerr_names
[i
].name
; i
++) {
72 if (rerr_names
[i
].code
== code
)
73 return rerr_names
[i
].name
;
79 struct err_list
*next
;
82 int written
; /* how many bytes we have written so far */
85 static struct err_list
*err_list_head
;
86 static struct err_list
*err_list_tail
;
88 /* add an error message to the pending error list */
89 static void err_list_add(int code
, char *buf
, int len
)
92 el
= (struct err_list
*)malloc(sizeof(*el
));
93 if (!el
) exit_cleanup(RERR_MALLOC
);
95 el
->buf
= malloc(len
+4);
96 if (!el
->buf
) exit_cleanup(RERR_MALLOC
);
97 memcpy(el
->buf
+4, buf
, len
);
98 SIVAL(el
->buf
, 0, ((code
+MPLEX_BASE
)<<24) | len
);
102 err_list_tail
->next
= el
;
110 /* try to push errors off the error list onto the wire */
111 void err_list_push(void)
113 if (log_error_fd
== -1) return;
115 while (err_list_head
) {
116 struct err_list
*el
= err_list_head
;
117 int n
= write(log_error_fd
, el
->buf
+el
->written
, el
->len
- el
->written
);
118 /* don't check for an error if the best way of handling the error is
124 if (el
->written
== el
->len
) {
126 err_list_head
= el
->next
;
127 if (!err_list_head
) err_list_tail
= NULL
;
134 static void logit(int priority
, char *buf
)
139 fprintf(logfile
,"%s [%d] %s",
140 timestring(time(NULL
)), (int)getpid(), buf
);
143 syslog(priority
, "%s", buf
);
149 static int initialised
;
150 int options
= LOG_PID
;
153 if (initialised
) return;
156 /* this looks pointless, but it is needed in order for the
157 C library on some systems to fetch the timezone info
162 /* optionally use a log file instead of syslog */
163 logfname
= lp_log_file();
173 options
|= LOG_NDELAY
;
177 openlog("rsyncd", options
, lp_syslog_facility());
179 openlog("rsyncd", options
);
183 logit(LOG_INFO
,"rsyncd started\n");
189 if (logfname
&& !logfile
) {
190 extern int orig_umask
;
191 int old_umask
= umask(022 | orig_umask
);
192 logfile
= fopen(logfname
, "a");
205 /* setup the error file descriptor - used when we are a server
206 that is receiving files */
207 void set_error_fd(int fd
)
210 set_nonblocking(log_error_fd
);
213 /* this is the underlying (unformatted) rsync debugging function. Call
214 it with FINFO, FERROR or FLOG */
215 void rwrite(enum logcode code
, char *buf
, int len
)
218 extern int am_daemon
;
219 extern int am_server
;
221 /* recursion can happen with certain fatal conditions */
223 if (quiet
&& code
== FINFO
) return;
225 if (len
< 0) exit_cleanup(RERR_MESSAGEIO
);
230 if (am_daemon
) logit(LOG_INFO
, buf
);
234 /* first try to pass it off to our sibling */
235 if (am_server
&& log_error_fd
!= -1) {
236 err_list_add(code
, buf
, len
);
241 /* If that fails, try to pass it to the other end.
243 * io_multiplex_write can fail if we do not have a multiplexed
244 * connection at the moment, in which case we fall through and
245 * log locally instead. */
246 if (am_server
&& io_multiplex_write(code
, buf
, len
)) {
252 int priority
= LOG_INFO
;
253 if (code
== FERROR
) priority
= LOG_WARNING
;
260 logit(priority
, buf
);
266 if (code
== FERROR
) {
278 if (!f
) exit_cleanup(RERR_MESSAGEIO
);
280 if (fwrite(buf
, len
, 1, f
) != 1) exit_cleanup(RERR_MESSAGEIO
);
282 if (buf
[len
-1] == '\r' || buf
[len
-1] == '\n') fflush(f
);
286 /* This is the rsync debugging function. Call it with FINFO, FERROR or
288 void rprintf(enum logcode code
, const char *format
, ...)
294 va_start(ap
, format
);
295 /* Note: might return -1 */
296 len
= vsnprintf(buf
, sizeof(buf
), format
, ap
);
299 /* Deal with buffer overruns. Instead of panicking, just
300 * truncate the resulting string. Note that some vsnprintf()s
301 * return -1 on truncation, e.g., glibc 2.0.6 and earlier. */
302 if ((size_t) len
> sizeof(buf
)-1 || len
< 0) {
303 const char ellipsis
[] = "[...]";
305 /* Reset length, and zero-terminate the end of our buffer */
309 /* Copy the ellipsis to the end of the string, but give
310 * us one extra character:
312 * v--- null byte at buf[sizeof(buf)-1]
314 * -> abcd[...]00 <-- now two null bytes at end
316 * If the input format string has a trailing newline,
317 * we copy it into that extra null; if it doesn't, well,
318 * all we lose is one byte. */
319 strncpy(buf
+len
-sizeof(ellipsis
), ellipsis
, sizeof(ellipsis
));
320 if (format
[strlen(format
)-1] == '\n') {
325 rwrite(code
, buf
, len
);
329 /* This is like rprintf, but it also tries to print some
330 * representation of the error code. Normally errcode = errno.
332 * Unlike rprintf, this always adds a newline and there should not be
333 * one in the format string.
335 * Note that since strerror might involve dynamically loading a
336 * message catalog we need to call it once before chroot-ing. */
337 void rsyserr(enum logcode code
, int errcode
, const char *format
, ...)
345 va_start(ap
, format
);
346 /* Note: might return <0 */
347 len
= vsnprintf(buf
, sizeof(buf
), format
, ap
);
350 /* TODO: Put in RSYNC_NAME at the start. */
352 if ((size_t) len
> sizeof(buf
)-1)
353 exit_cleanup(RERR_MESSAGEIO
);
355 sysmsg
= strerror(errcode
);
356 sys_len
= strlen(sysmsg
);
357 if ((size_t) len
+ 3 + sys_len
> sizeof(buf
) - 1)
358 exit_cleanup(RERR_MESSAGEIO
);
360 strcpy(buf
+ len
, ": ");
362 strcpy(buf
+ len
, sysmsg
);
364 strcpy(buf
+ len
, "\n");
367 rwrite(code
, buf
, len
);
372 void rflush(enum logcode code
)
375 extern int am_daemon
;
385 if (code
== FERROR
) {
390 extern int am_server
;
397 if (!f
) exit_cleanup(RERR_MESSAGEIO
);
403 /* a generic logging routine for send/recv, with parameter
405 static void log_formatted(enum logcode code
,
406 char *format
, char *op
, struct file_struct
*file
,
407 struct stats
*initial_stats
)
409 extern int module_id
;
410 extern char *auth_user
;
415 extern struct stats stats
;
416 extern int am_sender
;
417 extern int am_daemon
;
420 /* We expand % codes one by one in place in buf. We don't
421 * copy in the terminating nul of the inserted strings, but
422 * rather keep going until we reach the nul of the format.
423 * Just to make sure we don't clobber that nul and therefore
424 * accidentally keep going, we zero the buffer now. */
425 memset(buf
, 0, sizeof buf
);
426 strlcpy(buf
, format
, sizeof(buf
));
429 s
&& (p
=strchr(s
,'%')); ) {
434 case 'h': if (am_daemon
) n
= client_name(0); break;
435 case 'a': if (am_daemon
) n
= client_addr(0); break;
437 snprintf(buf2
,sizeof(buf2
),"%.0f",
438 (double)file
->length
);
442 snprintf(buf2
,sizeof(buf2
),"%d",
446 case 'o': n
= op
; break;
448 snprintf(buf2
, sizeof(buf2
), "%s/%s",
449 file
->basedir
?file
->basedir
:"",
455 case 'm': n
= lp_name(module_id
); break;
456 case 't': n
= timestring(time(NULL
)); break;
457 case 'P': n
= lp_path(module_id
); break;
458 case 'u': n
= auth_user
; break;
461 b
= stats
.total_written
-
462 initial_stats
->total_written
;
464 b
= stats
.total_read
-
465 initial_stats
->total_read
;
467 snprintf(buf2
,sizeof(buf2
),"%.0f", (double)b
);
472 b
= stats
.total_written
-
473 initial_stats
->total_written
;
475 b
= stats
.total_read
-
476 initial_stats
->total_read
;
478 snprintf(buf2
,sizeof(buf2
),"%.0f", (double)b
);
483 /* n is the string to be inserted in place of this %
484 * code; l is its length not including the trailing
491 if (l
+ ((int)(s
- &buf
[0])) >= sizeof(buf
)) {
492 rprintf(FERROR
,"buffer overflow expanding %%%c - exiting\n",
494 exit_cleanup(RERR_MESSAGEIO
);
497 /* Shuffle the rest of the string along to make space for n */
499 memmove(s
+(l
-1), s
+1, strlen(s
+1)+1);
502 /* Copy in n but NOT its nul, because the format sting
503 * probably continues after this. */
506 /* Skip over inserted string; continue looking */
510 rprintf(code
,"%s\n", buf
);
513 /* log the outgoing transfer of a file */
514 void log_send(struct file_struct
*file
, struct stats
*initial_stats
)
516 extern int module_id
;
517 extern int am_server
;
518 extern char *log_format
;
520 if (lp_transfer_logging(module_id
)) {
521 log_formatted(FLOG
, lp_log_format(module_id
), "send", file
, initial_stats
);
522 } else if (log_format
&& !am_server
) {
523 log_formatted(FINFO
, log_format
, "send", file
, initial_stats
);
527 /* log the incoming transfer of a file */
528 void log_recv(struct file_struct
*file
, struct stats
*initial_stats
)
530 extern int module_id
;
531 extern int am_server
;
532 extern char *log_format
;
534 if (lp_transfer_logging(module_id
)) {
535 log_formatted(FLOG
, lp_log_format(module_id
), "recv", file
, initial_stats
);
536 } else if (log_format
&& !am_server
) {
537 log_formatted(FINFO
, log_format
, "recv", file
, initial_stats
);
545 * Called when the transfer is interrupted for some reason.
547 * Code is one of the RERR_* codes from errcode.h, or terminating
550 void log_exit(int code
, const char *file
, int line
)
553 extern struct stats stats
;
554 rprintf(FLOG
,"wrote %.0f bytes read %.0f bytes total size %.0f\n",
555 (double)stats
.total_written
,
556 (double)stats
.total_read
,
557 (double)stats
.total_size
);
561 name
= rerr_name(code
);
563 name
= "unexplained error";
565 rprintf(FERROR
,"rsync error: %s (code %d) at %s(%d)\n",
566 name
, code
, file
, line
);
571 * Log the incoming transfer of a file for interactive use,
572 * this will be called at the end where the client was run.
573 * Called when a file starts to be transferred.
575 void log_transfer(struct file_struct
*file
, const char *fname
)
579 if (!verbose
) return;
581 rprintf(FINFO
, "%s\n", fname
);