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;
39 } const rerr_names
[] = {
40 { RERR_SYNTAX
, "syntax or usage error" },
41 { RERR_PROTOCOL
, "protocol incompatibility" },
42 { RERR_FILESELECT
, "errors selecting input/output files, dirs" },
43 { RERR_UNSUPPORTED
, "requested action not supported" },
44 { RERR_SOCKETIO
, "error in socket IO" },
45 { RERR_FILEIO
, "error in file IO" },
46 { RERR_STREAMIO
, "error in rsync protocol data stream" },
47 { RERR_MESSAGEIO
, "errors with program diagnostics" },
48 { RERR_IPC
, "error in IPC code" },
49 { RERR_SIGNAL
, "received SIGUSR1 or SIGINT" },
50 { RERR_WAITCHILD
, "some error returned by waitpid()" },
51 { RERR_MALLOC
, "error allocating core memory buffers" },
52 { RERR_PARTIAL
, "partial transfer" },
53 { RERR_TIMEOUT
, "timeout in data send/receive" },
54 { RERR_CMD_FAILED
, "remote shell failed" },
55 { RERR_CMD_KILLED
, "remote shell killed" },
56 { RERR_CMD_RUN
, "remote command could not be run" },
57 { RERR_CMD_NOTFOUND
, "remote command not found" },
64 * Map from rsync error code to name, or return NULL.
66 static char const *rerr_name(int code
)
69 for (i
= 0; rerr_names
[i
].name
; i
++) {
70 if (rerr_names
[i
].code
== code
)
71 return rerr_names
[i
].name
;
77 struct err_list
*next
;
80 int written
; /* how many bytes we have written so far */
83 static struct err_list
*err_list_head
;
84 static struct err_list
*err_list_tail
;
86 /* add an error message to the pending error list */
87 static void err_list_add(int code
, char *buf
, int len
)
90 el
= (struct err_list
*)malloc(sizeof(*el
));
91 if (!el
) exit_cleanup(RERR_MALLOC
);
93 el
->buf
= malloc(len
+4);
94 if (!el
->buf
) exit_cleanup(RERR_MALLOC
);
95 memcpy(el
->buf
+4, buf
, len
);
96 SIVAL(el
->buf
, 0, ((code
+MPLEX_BASE
)<<24) | len
);
100 err_list_tail
->next
= el
;
108 /* try to push errors off the error list onto the wire */
109 void err_list_push(void)
111 if (log_error_fd
== -1) return;
113 while (err_list_head
) {
114 struct err_list
*el
= err_list_head
;
115 int n
= write(log_error_fd
, el
->buf
+el
->written
, el
->len
- el
->written
);
116 /* don't check for an error if the best way of handling the error is
122 if (el
->written
== el
->len
) {
124 err_list_head
= el
->next
;
125 if (!err_list_head
) err_list_tail
= NULL
;
132 static void logit(int priority
, char *buf
)
137 fprintf(logfile
,"%s [%d] %s",
138 timestring(time(NULL
)), (int)getpid(), buf
);
141 syslog(priority
, "%s", buf
);
147 static int initialised
;
148 int options
= LOG_PID
;
151 if (initialised
) return;
154 /* this looks pointless, but it is needed in order for the
155 C library on some systems to fetch the timezone info
160 /* optionally use a log file instead of syslog */
161 logfname
= lp_log_file();
171 options
|= LOG_NDELAY
;
175 openlog("rsyncd", options
, lp_syslog_facility());
177 openlog("rsyncd", options
);
181 logit(LOG_INFO
,"rsyncd started\n");
187 if (logfname
&& !logfile
) {
188 extern int orig_umask
;
189 int old_umask
= umask(022 | orig_umask
);
190 logfile
= fopen(logfname
, "a");
203 /* setup the error file descriptor - used when we are a server
204 that is receiving files */
205 void set_error_fd(int fd
)
208 set_nonblocking(log_error_fd
);
211 /* this is the underlying (unformatted) rsync debugging function. Call
212 it with FINFO, FERROR or FLOG */
213 void rwrite(enum logcode code
, char *buf
, int len
)
216 extern int am_daemon
;
217 extern int am_server
;
219 /* recursion can happen with certain fatal conditions */
221 if (quiet
&& code
== FINFO
) return;
223 if (len
< 0) exit_cleanup(RERR_MESSAGEIO
);
228 if (am_daemon
) logit(LOG_INFO
, buf
);
232 /* first try to pass it off to our sibling */
233 if (am_server
&& log_error_fd
!= -1) {
234 err_list_add(code
, buf
, len
);
239 /* if that fails, try to pass it to the other end */
240 if (am_server
&& io_multiplex_write(code
, buf
, len
)) {
246 int priority
= LOG_INFO
;
247 if (code
== FERROR
) priority
= LOG_WARNING
;
254 logit(priority
, buf
);
260 if (code
== FERROR
) {
272 if (!f
) exit_cleanup(RERR_MESSAGEIO
);
274 if (fwrite(buf
, len
, 1, f
) != 1) exit_cleanup(RERR_MESSAGEIO
);
276 if (buf
[len
-1] == '\r' || buf
[len
-1] == '\n') fflush(f
);
280 /* This is the rsync debugging function. Call it with FINFO, FERROR or
282 void rprintf(enum logcode code
, const char *format
, ...)
288 va_start(ap
, format
);
289 len
= vsnprintf(buf
, sizeof(buf
), format
, ap
);
292 /* Deal with buffer overruns. Instead of panicking, just
293 * truncate the resulting string. Note that some vsnprintf()s
294 * return -1 on truncation, e.g., glibc 2.0.6 and earlier. */
295 if (len
> sizeof(buf
)-1 || len
< 0) {
296 const char ellipsis
[] = "[...]";
298 /* Reset length, and zero-terminate the end of our buffer */
302 /* Copy the ellipsis to the end of the string, but give
303 * us one extra character:
305 * v--- null byte at buf[sizeof(buf)-1]
307 * -> abcd[...]00 <-- now two null bytes at end
309 * If the input format string has a trailing newline,
310 * we copy it into that extra null; if it doesn't, well,
311 * all we lose is one byte. */
312 strncpy(buf
+len
-sizeof(ellipsis
), ellipsis
, sizeof(ellipsis
));
313 if (format
[strlen(format
)-1] == '\n') {
318 rwrite(code
, buf
, len
);
322 /* This is like rprintf, but it also tries to print some
323 * representation of the error code. Normally errcode = errno.
325 * Unlike rprintf, this always adds a newline and there should not be
326 * one in the format string.
328 * Note that since strerror might involve dynamically loading a
329 * message catalog we need to call it once before chroot-ing. */
330 void rsyserr(enum logcode code
, int errcode
, const char *format
, ...)
337 va_start(ap
, format
);
338 len
= vsnprintf(buf
, sizeof(buf
), format
, ap
);
341 if (len
> sizeof(buf
)-1) exit_cleanup(RERR_MESSAGEIO
);
343 sysmsg
= strerror(errcode
);
344 sys_len
= strlen(sysmsg
);
345 if (len
+ 3 + sys_len
> sizeof(buf
) - 1)
346 exit_cleanup(RERR_MESSAGEIO
);
348 strcpy(buf
+ len
, ": ");
350 strcpy(buf
+ len
, sysmsg
);
352 strcpy(buf
+ len
, "\n");
355 rwrite(code
, buf
, len
);
360 void rflush(enum logcode code
)
363 extern int am_daemon
;
373 if (code
== FERROR
) {
378 extern int am_server
;
385 if (!f
) exit_cleanup(RERR_MESSAGEIO
);
391 /* a generic logging routine for send/recv, with parameter
393 static void log_formatted(enum logcode code
,
394 char *format
, char *op
, struct file_struct
*file
,
395 struct stats
*initial_stats
)
397 extern int module_id
;
398 extern char *auth_user
;
403 extern struct stats stats
;
404 extern int am_sender
;
405 extern int am_daemon
;
408 strlcpy(buf
, format
, sizeof(buf
));
411 s
&& (p
=strchr(s
,'%')); ) {
416 case 'h': if (am_daemon
) n
= client_name(0); break;
417 case 'a': if (am_daemon
) n
= client_addr(0); break;
419 snprintf(buf2
,sizeof(buf2
),"%.0f",
420 (double)file
->length
);
424 snprintf(buf2
,sizeof(buf2
),"%d",
428 case 'o': n
= op
; break;
430 snprintf(buf2
, sizeof(buf2
), "%s/%s",
431 file
->basedir
?file
->basedir
:"",
437 case 'm': n
= lp_name(module_id
); break;
438 case 't': n
= timestring(time(NULL
)); break;
439 case 'P': n
= lp_path(module_id
); break;
440 case 'u': n
= auth_user
; break;
443 b
= stats
.total_written
-
444 initial_stats
->total_written
;
446 b
= stats
.total_read
-
447 initial_stats
->total_read
;
449 snprintf(buf2
,sizeof(buf2
),"%.0f", (double)b
);
454 b
= stats
.total_written
-
455 initial_stats
->total_written
;
457 b
= stats
.total_read
-
458 initial_stats
->total_read
;
460 snprintf(buf2
,sizeof(buf2
),"%.0f", (double)b
);
469 if ((l
-1) + ((int)(s
- &buf
[0])) > sizeof(buf
)) {
470 rprintf(FERROR
,"buffer overflow expanding %%%c - exiting\n",
472 exit_cleanup(RERR_MESSAGEIO
);
476 memmove(s
+(l
-1), s
+1, strlen(s
+1)+1);
483 rprintf(code
,"%s\n", buf
);
486 /* log the outgoing transfer of a file */
487 void log_send(struct file_struct
*file
, struct stats
*initial_stats
)
489 extern int module_id
;
490 extern int am_server
;
491 extern char *log_format
;
493 if (lp_transfer_logging(module_id
)) {
494 log_formatted(FLOG
, lp_log_format(module_id
), "send", file
, initial_stats
);
495 } else if (log_format
&& !am_server
) {
496 log_formatted(FINFO
, log_format
, "send", file
, initial_stats
);
500 /* log the incoming transfer of a file */
501 void log_recv(struct file_struct
*file
, struct stats
*initial_stats
)
503 extern int module_id
;
504 extern int am_server
;
505 extern char *log_format
;
507 if (lp_transfer_logging(module_id
)) {
508 log_formatted(FLOG
, lp_log_format(module_id
), "recv", file
, initial_stats
);
509 } else if (log_format
&& !am_server
) {
510 log_formatted(FINFO
, log_format
, "recv", file
, initial_stats
);
518 * Called when the transfer is interrupted for some reason.
520 * Code is one of the RERR_* codes from errcode.h, or terminating
523 void log_exit(int code
, const char *file
, int line
)
526 extern struct stats stats
;
527 rprintf(FLOG
,"wrote %.0f bytes read %.0f bytes total size %.0f\n",
528 (double)stats
.total_written
,
529 (double)stats
.total_read
,
530 (double)stats
.total_size
);
534 name
= rerr_name(code
);
536 name
= "unexplained error";
538 rprintf(FERROR
,"rsync error: %s (code %d) at %s(%d)\n",
539 name
, code
, file
, line
);
546 /* log the incoming transfer of a file for interactive use, this
547 will be called at the end where the client was run
549 it i called when a file starts to be transferred
551 void log_transfer(struct file_struct
*file
, const char *fname
)
555 if (!verbose
) return;
557 rprintf(FINFO
,"%s\n", fname
);