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.
36 extern int msg_fd_out;
37 extern int preserve_times;
38 extern char *auth_user;
39 extern char *log_format;
41 static int log_initialised;
42 static char *logfname;
46 int log_got_error = 0;
51 } const rerr_names[] = {
52 { RERR_SYNTAX , "syntax or usage error" },
53 { RERR_PROTOCOL , "protocol incompatibility" },
54 { RERR_FILESELECT , "errors selecting input/output files, dirs" },
55 { RERR_UNSUPPORTED, "requested action not supported" },
56 { RERR_STARTCLIENT, "error starting client-server protocol" },
57 { RERR_SOCKETIO , "error in socket IO" },
58 { RERR_FILEIO , "error in file IO" },
59 { RERR_STREAMIO , "error in rsync protocol data stream" },
60 { RERR_MESSAGEIO , "errors with program diagnostics" },
61 { RERR_IPC , "error in IPC code" },
62 { RERR_SIGNAL , "received SIGUSR1 or SIGINT" },
63 { RERR_WAITCHILD , "some error returned by waitpid()" },
64 { RERR_MALLOC , "error allocating core memory buffers" },
65 { RERR_PARTIAL , "some files could not be transferred" },
66 { RERR_VANISHED , "some files vanished before they could be transferred" },
67 { RERR_TIMEOUT , "timeout in data send/receive" },
68 { RERR_CMD_FAILED , "remote shell failed" },
69 { RERR_CMD_KILLED , "remote shell killed" },
70 { RERR_CMD_RUN, "remote command could not be run" },
71 { RERR_CMD_NOTFOUND, "remote command not found" },
78 * Map from rsync error code to name, or return NULL.
80 static char const *rerr_name(int code)
83 for (i = 0; rerr_names[i].name; i++) {
84 if (rerr_names[i].code == code)
85 return rerr_names[i].name;
90 static void log_open(void)
92 if (logfname && !logfile) {
93 extern int orig_umask;
94 int old_umask = umask(022 | orig_umask);
95 logfile = fopen(logfname, "a");
98 am_daemon = 0; /* avoid trying to log again */
99 rsyserr(FERROR, errno, "fopen() of log-file failed");
100 exit_cleanup(RERR_FILESELECT);
105 static void logit(int priority, char *buf)
110 fprintf(logfile,"%s [%d] %s",
111 timestring(time(NULL)), (int)getpid(), buf);
114 syslog(priority, "%s", buf);
120 int options = LOG_PID;
127 /* this looks pointless, but it is needed in order for the
128 * C library on some systems to fetch the timezone info
129 * before the chroot */
133 /* optionally use a log file instead of syslog */
134 logfname = lp_log_file();
144 options |= LOG_NDELAY;
148 openlog("rsyncd", options, lp_syslog_facility());
150 openlog("rsyncd", options);
154 logit(LOG_INFO,"rsyncd started\n");
166 /* this is the underlying (unformatted) rsync debugging function. Call
167 * it with FINFO, FERROR or FLOG */
168 void rwrite(enum logcode code, char *buf, int len)
171 /* recursion can happen with certain fatal conditions */
173 if (quiet && code == FINFO)
177 exit_cleanup(RERR_MESSAGEIO);
181 if (am_server && msg_fd_out >= 0) {
182 /* Pass the message to our sibling. */
183 send_msg((enum msgcode)code, buf, len);
190 int priority = code == FERROR ? LOG_WARNING : LOG_INFO;
195 if (!log_initialised)
197 strlcpy(msg, buf, MIN((int)sizeof msg, len + 1));
198 logit(priority, msg);
201 if (code == FLOG || !am_server)
203 } else if (code == FLOG)
207 /* Pass the message to the non-server side. */
208 if (io_multiplex_write((enum msgcode)code, buf, len))
211 /* TODO: can we send the error to the user somehow? */
216 if (code == FERROR) {
222 f = am_server ? stderr : stdout;
225 exit_cleanup(RERR_MESSAGEIO);
227 if (fwrite(buf, len, 1, f) != 1)
228 exit_cleanup(RERR_MESSAGEIO);
230 if (buf[len-1] == '\r' || buf[len-1] == '\n')
235 /* This is the rsync debugging function. Call it with FINFO, FERROR or
237 void rprintf(enum logcode code, const char *format, ...)
240 char buf[MAXPATHLEN+512];
243 va_start(ap, format);
244 len = vsnprintf(buf, sizeof buf, format, ap);
247 /* Deal with buffer overruns. Instead of panicking, just
248 * truncate the resulting string. (Note that configure ensures
249 * that we have a vsnprintf() that doesn't ever return -1.) */
250 if (len > sizeof buf - 1) {
251 const char ellipsis[] = "[...]";
253 /* Reset length, and zero-terminate the end of our buffer */
254 len = sizeof buf - 1;
257 /* Copy the ellipsis to the end of the string, but give
258 * us one extra character:
260 * v--- null byte at buf[sizeof buf - 1]
262 * -> abcd[...]00 <-- now two null bytes at end
264 * If the input format string has a trailing newline,
265 * we copy it into that extra null; if it doesn't, well,
266 * all we lose is one byte. */
267 strncpy(buf+len-sizeof ellipsis, ellipsis, sizeof ellipsis);
268 if (format[strlen(format)-1] == '\n') {
273 rwrite(code, buf, len);
277 /* This is like rprintf, but it also tries to print some
278 * representation of the error code. Normally errcode = errno.
280 * Unlike rprintf, this always adds a newline and there should not be
281 * one in the format string.
283 * Note that since strerror might involve dynamically loading a
284 * message catalog we need to call it once before chroot-ing. */
285 void rsyserr(enum logcode code, int errcode, const char *format, ...)
288 char buf[MAXPATHLEN+512];
291 strcpy(buf, RSYNC_NAME ": ");
292 len = (sizeof RSYNC_NAME ": ") - 1;
294 va_start(ap, format);
295 len += vsnprintf(buf + len, sizeof buf - len, format, ap);
298 if (len < sizeof buf) {
299 len += snprintf(buf + len, sizeof buf - len,
300 ": %s (%d)\n", strerror(errcode), errcode);
302 if (len >= sizeof buf)
303 exit_cleanup(RERR_MESSAGEIO);
305 rwrite(code, buf, len);
310 void rflush(enum logcode code)
322 if (code == FERROR) {
333 if (!f) exit_cleanup(RERR_MESSAGEIO);
339 /* a generic logging routine for send/recv, with parameter
341 static void log_formatted(enum logcode code,
342 char *format, char *op, struct file_struct *file,
343 struct stats *initial_stats, int iflags)
345 char buf[MAXPATHLEN+1024];
351 /* We expand % codes one by one in place in buf. We don't
352 * copy in the terminating nul of the inserted strings, but
353 * rather keep going until we reach the nul of the format. */
354 total = strlcpy(buf, format, sizeof buf);
356 for (p = buf; (p = strchr(p, '%')) != NULL && p[1]; ) {
360 case 'h': if (am_daemon) n = client_name(0); break;
361 case 'a': if (am_daemon) n = client_addr(0); break;
363 snprintf(buf2, sizeof buf2, "%.0f",
364 (double)file->length);
368 snprintf(buf2, sizeof buf2, "%d",
372 case 'o': n = op; break;
374 pathjoin(buf2, sizeof buf2,
375 am_sender && file->dir.root ? file->dir.root : "",
376 safe_fname(f_name(file)));
377 clean_fname(buf2, 0);
382 n = (char*)safe_fname(f_name(file));
385 if (S_ISLNK(file->mode)) {
386 snprintf(buf2, sizeof buf2, " -> %s",
387 safe_fname(file->u.link));
392 case 'm': n = lp_name(module_id); break;
393 case 't': n = timestring(time(NULL)); break;
394 case 'P': n = lp_path(module_id); break;
395 case 'u': n = auth_user; break;
398 b = stats.total_written -
399 initial_stats->total_written;
401 b = stats.total_read -
402 initial_stats->total_read;
404 snprintf(buf2, sizeof buf2, "%.0f", (double)b);
409 b = stats.total_written -
410 initial_stats->total_written;
412 b = stats.total_read -
413 initial_stats->total_read;
415 snprintf(buf2, sizeof buf2, "%.0f", (double)b);
420 n[0] = !(iflags & ITEM_UPDATING) ? ' ' : dry_run
421 ? '*' : *op == 's' ? '>' : '<';
422 n[1] = S_ISDIR(file->mode) ? 'd' : IS_DEVICE(file->mode) ? 'D'
423 : S_ISLNK(file->mode) ? 'L' : 'f';
424 n[2] = !(iflags & ITEM_REPORT_CHECKSUM) ? '-' : 'c';
425 n[3] = !(iflags & ITEM_REPORT_SIZE) ? '-' : 's';
426 n[4] = !(iflags & ITEM_REPORT_TIME) ? '-'
427 : !preserve_times || IS_DEVICE(file->mode)
428 || S_ISLNK(file->mode) ? 'T' : 't';
429 n[5] = !(iflags & ITEM_REPORT_PERMS) ? '-' : 'p';
430 n[6] = !(iflags & ITEM_REPORT_OWNER) ? '-' : 'o';
431 n[7] = !(iflags & ITEM_REPORT_GROUP) ? '-' : 'g';
434 if (iflags & (ITEM_IS_NEW|ITEM_MISSING_DATA)) {
435 char ch = iflags & ITEM_IS_NEW ? '+' : '?';
437 for (i = 2; n[i]; i++)
443 /* n is the string to be inserted in place of this %
444 * code; len is its length not including the trailing
453 if (len + total - 2 >= sizeof buf) {
455 "buffer overflow expanding %%%c -- exiting\n",
457 exit_cleanup(RERR_MESSAGEIO);
460 /* Shuffle the rest of the string along to make space for n */
462 memmove(p + len, p + 2, total - (p + 2 - buf) + 1);
465 /* Insert the contents of string "n", but NOT its nul. */
469 /* Skip over inserted string; continue looking */
473 rprintf(code, "%s\n", buf);
476 /* log the outgoing transfer of a file */
477 void log_send(struct file_struct *file, struct stats *initial_stats, int iflags)
479 if (lp_transfer_logging(module_id)) {
480 log_formatted(FLOG, lp_log_format(module_id), "send",
481 file, initial_stats, iflags);
482 } else if (log_format && !am_server) {
483 log_formatted(FINFO, log_format, "send",
484 file, initial_stats, iflags);
488 /* log the incoming transfer of a file */
489 void log_recv(struct file_struct *file, struct stats *initial_stats, int iflags)
491 if (lp_transfer_logging(module_id)) {
492 log_formatted(FLOG, lp_log_format(module_id), "recv",
493 file, initial_stats, iflags);
494 } else if (log_format && !am_server) {
495 log_formatted(FINFO, log_format, "recv",
496 file, initial_stats, iflags);
504 * Called when the transfer is interrupted for some reason.
506 * Code is one of the RERR_* codes from errcode.h, or terminating
509 void log_exit(int code, const char *file, int line)
512 rprintf(FLOG,"wrote %.0f bytes read %.0f bytes total size %.0f\n",
513 (double)stats.total_written,
514 (double)stats.total_read,
515 (double)stats.total_size);
519 name = rerr_name(code);
521 name = "unexplained error";
523 /* VANISHED is not an error, only a warning */
524 if (code == RERR_VANISHED) {
525 rprintf(FINFO, "rsync warning: %s (code %d) at %s(%d)\n",
526 name, code, file, line);
528 rprintf(FERROR, "rsync error: %s (code %d) at %s(%d)\n",
529 name, code, file, line);