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 char *auth_user;
36 extern char *log_format;
38 static int log_initialised;
39 static char *logfname;
41 static int log_error_fd = -1;
49 } const rerr_names[] = {
50 { RERR_SYNTAX , "syntax or usage error" },
51 { RERR_PROTOCOL , "protocol incompatibility" },
52 { RERR_FILESELECT , "errors selecting input/output files, dirs" },
53 { RERR_UNSUPPORTED, "requested action not supported" },
54 { RERR_STARTCLIENT, "error starting client-server protocol" },
55 { RERR_SOCKETIO , "error in socket IO" },
56 { RERR_FILEIO , "error in file IO" },
57 { RERR_STREAMIO , "error in rsync protocol data stream" },
58 { RERR_MESSAGEIO , "errors with program diagnostics" },
59 { RERR_IPC , "error in IPC code" },
60 { RERR_SIGNAL , "received SIGUSR1 or SIGINT" },
61 { RERR_WAITCHILD , "some error returned by waitpid()" },
62 { RERR_MALLOC , "error allocating core memory buffers" },
63 { RERR_PARTIAL , "some files could not be transferred" },
64 { RERR_VANISHED , "some files vanished before they could be transfered" },
65 { RERR_TIMEOUT , "timeout in data send/receive" },
66 { RERR_CMD_FAILED , "remote shell failed" },
67 { RERR_CMD_KILLED , "remote shell killed" },
68 { RERR_CMD_RUN, "remote command could not be run" },
69 { RERR_CMD_NOTFOUND, "remote command not found" },
76 * Map from rsync error code to name, or return NULL.
78 static char const *rerr_name(int code)
81 for (i = 0; rerr_names[i].name; i++) {
82 if (rerr_names[i].code == code)
83 return rerr_names[i].name;
89 struct err_list *next;
92 int written; /* how many bytes we have written so far */
95 static struct err_list *err_list_head;
96 static struct err_list *err_list_tail;
98 /* add an error message to the pending error list */
99 static void err_list_add(int code, char *buf, int len)
102 el = new(struct err_list);
103 if (!el) exit_cleanup(RERR_MALLOC);
105 el->buf = new_array(char, len+4);
106 if (!el->buf) exit_cleanup(RERR_MALLOC);
107 memcpy(el->buf+4, buf, len);
108 SIVAL(el->buf, 0, ((code+MPLEX_BASE)<<24) | len);
112 err_list_tail->next = el;
120 /* try to push errors off the error list onto the wire */
121 void err_list_push(void)
123 if (log_error_fd == -1) return;
125 while (err_list_head) {
126 struct err_list *el = err_list_head;
127 int n = write(log_error_fd, el->buf+el->written, el->len - el->written);
128 /* don't check for an error if the best way of handling the error is
134 if (el->written == el->len) {
136 err_list_head = el->next;
137 if (!err_list_head) err_list_tail = NULL;
144 static void logit(int priority, char *buf)
149 fprintf(logfile,"%s [%d] %s",
150 timestring(time(NULL)), (int)getpid(), buf);
153 syslog(priority, "%s", buf);
159 int options = LOG_PID;
162 if (log_initialised) return;
165 /* this looks pointless, but it is needed in order for the
166 * C library on some systems to fetch the timezone info
167 * before the chroot */
171 /* optionally use a log file instead of syslog */
172 logfname = lp_log_file();
182 options |= LOG_NDELAY;
186 openlog("rsyncd", options, lp_syslog_facility());
188 openlog("rsyncd", options);
192 logit(LOG_INFO,"rsyncd started\n");
198 if (logfname && !logfile) {
199 extern int orig_umask;
200 int old_umask = umask(022 | orig_umask);
201 logfile = fopen(logfname, "a");
214 /* setup the error file descriptor - used when we are a server
215 * that is receiving files */
216 void set_error_fd(int fd)
219 set_nonblocking(log_error_fd);
222 /* this is the underlying (unformatted) rsync debugging function. Call
223 * it with FINFO, FERROR or FLOG */
224 void rwrite(enum logcode code, char *buf, int len)
227 /* recursion can happen with certain fatal conditions */
229 if (quiet && code == FINFO) return;
231 if (len < 0) exit_cleanup(RERR_MESSAGEIO);
236 if (am_daemon) logit(LOG_INFO, buf);
240 /* first try to pass it off to our sibling */
241 if (am_server && log_error_fd != -1) {
242 err_list_add(code, buf, len);
247 /* next, if we are a server and multiplexing is enabled,
248 * pass it to the other side. */
249 if (am_server && io_multiplex_write(code, buf, len)) {
253 /* otherwise, if in daemon mode and either we are not a server
254 * (that is, we are not running --daemon over a remote shell) or
255 * the log has already been initialised, log the message on this
256 * side because we don't want the client to see most errors for
257 * security reasons. We do want early messages when running daemon
258 * mode over a remote shell to go to the remote side; those will
259 * fall through to the next case.
260 * Note that this is only for the time before multiplexing is enabled.
262 if (am_daemon && (!am_server || log_initialised)) {
264 int priority = LOG_INFO;
265 if (code == FERROR) priority = LOG_WARNING;
272 logit(priority, buf);
278 if (code == FERROR) {
290 if (!f) exit_cleanup(RERR_MESSAGEIO);
292 if (fwrite(buf, len, 1, f) != 1) exit_cleanup(RERR_MESSAGEIO);
294 if (buf[len-1] == '\r' || buf[len-1] == '\n') fflush(f);
298 /* This is the rsync debugging function. Call it with FINFO, FERROR or
300 void rprintf(enum logcode code, const char *format, ...)
306 va_start(ap, format);
307 /* Note: might return -1 */
308 len = vsnprintf(buf, sizeof(buf), format, ap);
311 /* Deal with buffer overruns. Instead of panicking, just
312 * truncate the resulting string. Note that some vsnprintf()s
313 * return -1 on truncation, e.g., glibc 2.0.6 and earlier. */
314 if ((size_t) len > sizeof(buf)-1 || len < 0) {
315 const char ellipsis[] = "[...]";
317 /* Reset length, and zero-terminate the end of our buffer */
321 /* Copy the ellipsis to the end of the string, but give
322 * us one extra character:
324 * v--- null byte at buf[sizeof(buf)-1]
326 * -> abcd[...]00 <-- now two null bytes at end
328 * If the input format string has a trailing newline,
329 * we copy it into that extra null; if it doesn't, well,
330 * all we lose is one byte. */
331 strncpy(buf+len-sizeof(ellipsis), ellipsis, sizeof(ellipsis));
332 if (format[strlen(format)-1] == '\n') {
337 rwrite(code, buf, len);
341 /* This is like rprintf, but it also tries to print some
342 * representation of the error code. Normally errcode = errno.
344 * Unlike rprintf, this always adds a newline and there should not be
345 * one in the format string.
347 * Note that since strerror might involve dynamically loading a
348 * message catalog we need to call it once before chroot-ing. */
349 void rsyserr(enum logcode code, int errcode, const char *format, ...)
357 va_start(ap, format);
358 /* Note: might return <0 */
359 len = vsnprintf(buf, sizeof(buf), format, ap);
362 /* TODO: Put in RSYNC_NAME at the start. */
364 if ((size_t) len > sizeof(buf)-1)
365 exit_cleanup(RERR_MESSAGEIO);
367 sysmsg = strerror(errcode);
368 sys_len = strlen(sysmsg);
369 if ((size_t) len + 3 + sys_len > sizeof(buf) - 1)
370 exit_cleanup(RERR_MESSAGEIO);
372 strcpy(buf + len, ": ");
374 strcpy(buf + len, sysmsg);
376 strcpy(buf + len, "\n");
379 rwrite(code, buf, len);
384 void rflush(enum logcode code)
396 if (code == FERROR) {
407 if (!f) exit_cleanup(RERR_MESSAGEIO);
413 /* a generic logging routine for send/recv, with parameter
415 static void log_formatted(enum logcode code,
416 char *format, char *op, struct file_struct *file,
417 struct stats *initial_stats)
425 /* We expand % codes one by one in place in buf. We don't
426 * copy in the terminating nul of the inserted strings, but
427 * rather keep going until we reach the nul of the format.
428 * Just to make sure we don't clobber that nul and therefore
429 * accidentally keep going, we zero the buffer now. */
430 memset(buf, 0, sizeof buf);
431 strlcpy(buf, format, sizeof(buf));
433 for (s = &buf[0]; s && (p = strchr(s,'%')); ) {
438 case 'h': if (am_daemon) n = client_name(0); break;
439 case 'a': if (am_daemon) n = client_addr(0); break;
441 snprintf(buf2,sizeof(buf2),"%.0f",
442 (double)file->length);
446 snprintf(buf2,sizeof(buf2),"%d",
450 case 'o': n = op; break;
452 snprintf(buf2, sizeof(buf2), "%s/%s",
453 file->basedir?file->basedir:"",
459 case 'm': n = lp_name(module_id); break;
460 case 't': n = timestring(time(NULL)); break;
461 case 'P': n = lp_path(module_id); break;
462 case 'u': n = auth_user; break;
465 b = stats.total_written -
466 initial_stats->total_written;
468 b = stats.total_read -
469 initial_stats->total_read;
471 snprintf(buf2,sizeof(buf2),"%.0f", (double)b);
476 b = stats.total_written -
477 initial_stats->total_written;
479 b = stats.total_read -
480 initial_stats->total_read;
482 snprintf(buf2,sizeof(buf2),"%.0f", (double)b);
487 /* n is the string to be inserted in place of this %
488 * code; l is its length not including the trailing
495 if (l + ((int)(s - &buf[0])) >= sizeof(buf)) {
496 rprintf(FERROR,"buffer overflow expanding %%%c - exiting\n",
498 exit_cleanup(RERR_MESSAGEIO);
501 /* Shuffle the rest of the string along to make space for n */
503 memmove(s+(l-1), s+1, strlen(s+1)+1);
506 /* Copy in n but NOT its nul, because the format sting
507 * probably continues after this. */
510 /* Skip over inserted string; continue looking */
514 rprintf(code,"%s\n", buf);
517 /* log the outgoing transfer of a file */
518 void log_send(struct file_struct *file, struct stats *initial_stats)
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 if (lp_transfer_logging(module_id)) {
531 log_formatted(FLOG, lp_log_format(module_id), "recv", file, initial_stats);
532 } else if (log_format && !am_server) {
533 log_formatted(FINFO, log_format, "recv", file, initial_stats);
541 * Called when the transfer is interrupted for some reason.
543 * Code is one of the RERR_* codes from errcode.h, or terminating
546 void log_exit(int code, const char *file, int line)
549 rprintf(FLOG,"wrote %.0f bytes read %.0f bytes total size %.0f\n",
550 (double)stats.total_written,
551 (double)stats.total_read,
552 (double)stats.total_size);
556 name = rerr_name(code);
558 name = "unexplained error";
560 /* VANISHED is not an error, only a warning */
561 if (code == RERR_VANISHED) {
562 rprintf(FINFO, "rsync warning: %s (code %d) at %s(%d)\n",
563 name, code, file, line);
565 rprintf(FERROR, "rsync error: %s (code %d) at %s(%d)\n",
566 name, code, file, line);