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_STARTCLIENT, "error starting client-server protocol" },
45 { RERR_SOCKETIO , "error in socket IO" },
46 { RERR_FILEIO , "error in file IO" },
47 { RERR_STREAMIO , "error in rsync protocol data stream" },
48 { RERR_MESSAGEIO , "errors with program diagnostics" },
49 { RERR_IPC , "error in IPC code" },
50 { RERR_SIGNAL , "received SIGUSR1 or SIGINT" },
51 { RERR_WAITCHILD , "some error returned by waitpid()" },
52 { RERR_MALLOC , "error allocating core memory buffers" },
53 { RERR_PARTIAL , "partial transfer" },
54 { RERR_TIMEOUT , "timeout in data send/receive" },
55 { RERR_CMD_FAILED , "remote shell failed" },
56 { RERR_CMD_KILLED , "remote shell killed" },
57 { RERR_CMD_RUN, "remote command could not be run" },
58 { RERR_CMD_NOTFOUND, "remote command not found" },
65 * Map from rsync error code to name, or return NULL.
67 static char const *rerr_name(int code)
70 for (i = 0; rerr_names[i].name; i++) {
71 if (rerr_names[i].code == code)
72 return rerr_names[i].name;
78 struct err_list *next;
81 int written; /* how many bytes we have written so far */
84 static struct err_list *err_list_head;
85 static struct err_list *err_list_tail;
87 /* add an error message to the pending error list */
88 static void err_list_add(int code, char *buf, int len)
91 el = (struct err_list *)malloc(sizeof(*el));
92 if (!el) exit_cleanup(RERR_MALLOC);
94 el->buf = malloc(len+4);
95 if (!el->buf) exit_cleanup(RERR_MALLOC);
96 memcpy(el->buf+4, buf, len);
97 SIVAL(el->buf, 0, ((code+MPLEX_BASE)<<24) | len);
101 err_list_tail->next = el;
109 /* try to push errors off the error list onto the wire */
110 void err_list_push(void)
112 if (log_error_fd == -1) return;
114 while (err_list_head) {
115 struct err_list *el = err_list_head;
116 int n = write(log_error_fd, el->buf+el->written, el->len - el->written);
117 /* don't check for an error if the best way of handling the error is
123 if (el->written == el->len) {
125 err_list_head = el->next;
126 if (!err_list_head) err_list_tail = NULL;
133 static void logit(int priority, char *buf)
138 fprintf(logfile,"%s [%d] %s",
139 timestring(time(NULL)), (int)getpid(), buf);
142 syslog(priority, "%s", buf);
148 static int initialised;
149 int options = LOG_PID;
152 if (initialised) return;
155 /* this looks pointless, but it is needed in order for the
156 C library on some systems to fetch the timezone info
161 /* optionally use a log file instead of syslog */
162 logfname = lp_log_file();
172 options |= LOG_NDELAY;
176 openlog("rsyncd", options, lp_syslog_facility());
178 openlog("rsyncd", options);
182 logit(LOG_INFO,"rsyncd started\n");
188 if (logfname && !logfile) {
189 extern int orig_umask;
190 int old_umask = umask(022 | orig_umask);
191 logfile = fopen(logfname, "a");
204 /* setup the error file descriptor - used when we are a server
205 that is receiving files */
206 void set_error_fd(int fd)
209 set_nonblocking(log_error_fd);
212 /* this is the underlying (unformatted) rsync debugging function. Call
213 it with FINFO, FERROR or FLOG */
214 void rwrite(enum logcode code, char *buf, int len)
217 extern int am_daemon;
218 extern int am_server;
220 /* recursion can happen with certain fatal conditions */
222 if (quiet && code == FINFO) return;
224 if (len < 0) exit_cleanup(RERR_MESSAGEIO);
229 if (am_daemon) logit(LOG_INFO, buf);
233 /* first try to pass it off to our sibling */
234 if (am_server && log_error_fd != -1) {
235 err_list_add(code, buf, len);
240 /* If that fails, try to pass it to the other end.
242 * io_multiplex_write can fail if we do not have a multiplexed
243 * connection at the moment, in which case we fall through and
244 * log locally instead. */
245 if (am_server && io_multiplex_write(code, buf, len)) {
251 int priority = LOG_INFO;
252 if (code == FERROR) priority = LOG_WARNING;
259 logit(priority, buf);
265 if (code == FERROR) {
277 if (!f) exit_cleanup(RERR_MESSAGEIO);
279 if (fwrite(buf, len, 1, f) != 1) exit_cleanup(RERR_MESSAGEIO);
281 if (buf[len-1] == '\r' || buf[len-1] == '\n') fflush(f);
285 /* This is the rsync debugging function. Call it with FINFO, FERROR or
287 void rprintf(enum logcode code, const char *format, ...)
293 va_start(ap, format);
294 /* Note: might return -1 */
295 len = vsnprintf(buf, sizeof(buf), format, ap);
298 /* Deal with buffer overruns. Instead of panicking, just
299 * truncate the resulting string. Note that some vsnprintf()s
300 * return -1 on truncation, e.g., glibc 2.0.6 and earlier. */
301 if ((size_t) len > sizeof(buf)-1 || len < 0) {
302 const char ellipsis[] = "[...]";
304 /* Reset length, and zero-terminate the end of our buffer */
308 /* Copy the ellipsis to the end of the string, but give
309 * us one extra character:
311 * v--- null byte at buf[sizeof(buf)-1]
313 * -> abcd[...]00 <-- now two null bytes at end
315 * If the input format string has a trailing newline,
316 * we copy it into that extra null; if it doesn't, well,
317 * all we lose is one byte. */
318 strncpy(buf+len-sizeof(ellipsis), ellipsis, sizeof(ellipsis));
319 if (format[strlen(format)-1] == '\n') {
324 rwrite(code, buf, len);
328 /* This is like rprintf, but it also tries to print some
329 * representation of the error code. Normally errcode = errno.
331 * Unlike rprintf, this always adds a newline and there should not be
332 * one in the format string.
334 * Note that since strerror might involve dynamically loading a
335 * message catalog we need to call it once before chroot-ing. */
336 void rsyserr(enum logcode code, int errcode, const char *format, ...)
344 va_start(ap, format);
345 /* Note: might return <0 */
346 len = vsnprintf(buf, sizeof(buf), format, ap);
349 /* TODO: Put in RSYNC_NAME at the start. */
351 if ((size_t) len > sizeof(buf)-1)
352 exit_cleanup(RERR_MESSAGEIO);
354 sysmsg = strerror(errcode);
355 sys_len = strlen(sysmsg);
356 if ((size_t) len + 3 + sys_len > sizeof(buf) - 1)
357 exit_cleanup(RERR_MESSAGEIO);
359 strcpy(buf + len, ": ");
361 strcpy(buf + len, sysmsg);
363 strcpy(buf + len, "\n");
366 rwrite(code, buf, len);
371 void rflush(enum logcode code)
374 extern int am_daemon;
384 if (code == FERROR) {
389 extern int am_server;
396 if (!f) exit_cleanup(RERR_MESSAGEIO);
402 /* a generic logging routine for send/recv, with parameter
404 static void log_formatted(enum logcode code,
405 char *format, char *op, struct file_struct *file,
406 struct stats *initial_stats)
408 extern int module_id;
409 extern char *auth_user;
414 extern struct stats stats;
415 extern int am_sender;
416 extern int am_daemon;
419 /* We expand % codes one by one in place in buf. We don't
420 * copy in the terminating nul of the inserted strings, but
421 * rather keep going until we reach the nul of the format.
422 * Just to make sure we don't clobber that nul and therefore
423 * accidentally keep going, we zero the buffer now. */
424 memset(buf, 0, sizeof buf);
425 strlcpy(buf, format, sizeof(buf));
428 s && (p=strchr(s,'%')); ) {
433 case 'h': if (am_daemon) n = client_name(0); break;
434 case 'a': if (am_daemon) n = client_addr(0); break;
436 snprintf(buf2,sizeof(buf2),"%.0f",
437 (double)file->length);
441 snprintf(buf2,sizeof(buf2),"%d",
445 case 'o': n = op; break;
447 snprintf(buf2, sizeof(buf2), "%s/%s",
448 file->basedir?file->basedir:"",
454 case 'm': n = lp_name(module_id); break;
455 case 't': n = timestring(time(NULL)); break;
456 case 'P': n = lp_path(module_id); break;
457 case 'u': n = auth_user; break;
460 b = stats.total_written -
461 initial_stats->total_written;
463 b = stats.total_read -
464 initial_stats->total_read;
466 snprintf(buf2,sizeof(buf2),"%.0f", (double)b);
471 b = stats.total_written -
472 initial_stats->total_written;
474 b = stats.total_read -
475 initial_stats->total_read;
477 snprintf(buf2,sizeof(buf2),"%.0f", (double)b);
482 /* n is the string to be inserted in place of this %
483 * code; l is its length not including the trailing
490 if (l + ((int)(s - &buf[0])) >= sizeof(buf)) {
491 rprintf(FERROR,"buffer overflow expanding %%%c - exiting\n",
493 exit_cleanup(RERR_MESSAGEIO);
496 /* Shuffle the rest of the string along to make space for n */
498 memmove(s+(l-1), s+1, strlen(s+1)+1);
501 /* Copy in n but NOT its nul, because the format sting
502 * probably continues after this. */
505 /* Skip over inserted string; continue looking */
509 rprintf(code,"%s\n", buf);
512 /* log the outgoing transfer of a file */
513 void log_send(struct file_struct *file, struct stats *initial_stats)
515 extern int module_id;
516 extern int am_server;
517 extern char *log_format;
519 if (lp_transfer_logging(module_id)) {
520 log_formatted(FLOG, lp_log_format(module_id), "send", file, initial_stats);
521 } else if (log_format && !am_server) {
522 log_formatted(FINFO, log_format, "send", file, initial_stats);
526 /* log the incoming transfer of a file */
527 void log_recv(struct file_struct *file, struct stats *initial_stats)
529 extern int module_id;
530 extern int am_server;
531 extern char *log_format;
533 if (lp_transfer_logging(module_id)) {
534 log_formatted(FLOG, lp_log_format(module_id), "recv", file, initial_stats);
535 } else if (log_format && !am_server) {
536 log_formatted(FINFO, log_format, "recv", file, initial_stats);
544 * Called when the transfer is interrupted for some reason.
546 * Code is one of the RERR_* codes from errcode.h, or terminating
549 void log_exit(int code, const char *file, int line)
552 extern struct stats stats;
553 rprintf(FLOG,"wrote %.0f bytes read %.0f bytes total size %.0f\n",
554 (double)stats.total_written,
555 (double)stats.total_read,
556 (double)stats.total_size);
560 name = rerr_name(code);
562 name = "unexplained error";
564 rprintf(FERROR,"rsync error: %s (code %d) at %s(%d)\n",
565 name, code, file, line);
570 * Log the incoming transfer of a file for interactive use,
571 * this will be called at the end where the client was run.
572 * Called when a file starts to be transferred.
574 void log_transfer(struct file_struct *file, const char *fname)
578 if (!verbose) return;
580 rprintf(FINFO, "%s\n", fname);