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 protocol_version;
38 extern int preserve_times;
39 extern int log_format_has_o_or_i;
40 extern int daemon_log_format_has_o_or_i;
41 extern char *auth_user;
42 extern char *log_format;
44 static int log_initialised;
45 static char *logfname;
49 int log_got_error = 0;
54 } const rerr_names[] = {
55 { RERR_SYNTAX , "syntax or usage error" },
56 { RERR_PROTOCOL , "protocol incompatibility" },
57 { RERR_FILESELECT , "errors selecting input/output files, dirs" },
58 { RERR_UNSUPPORTED, "requested action not supported" },
59 { RERR_STARTCLIENT, "error starting client-server protocol" },
60 { RERR_SOCKETIO , "error in socket IO" },
61 { RERR_FILEIO , "error in file IO" },
62 { RERR_STREAMIO , "error in rsync protocol data stream" },
63 { RERR_MESSAGEIO , "errors with program diagnostics" },
64 { RERR_IPC , "error in IPC code" },
65 { RERR_SIGNAL , "received SIGUSR1 or SIGINT" },
66 { RERR_WAITCHILD , "some error returned by waitpid()" },
67 { RERR_MALLOC , "error allocating core memory buffers" },
68 { RERR_PARTIAL , "some files could not be transferred" },
69 { RERR_VANISHED , "some files vanished before they could be transferred" },
70 { RERR_TIMEOUT , "timeout in data send/receive" },
71 { RERR_CMD_FAILED , "remote shell failed" },
72 { RERR_CMD_KILLED , "remote shell killed" },
73 { RERR_CMD_RUN, "remote command could not be run" },
74 { RERR_CMD_NOTFOUND, "remote command not found" },
81 * Map from rsync error code to name, or return NULL.
83 static char const *rerr_name(int code)
86 for (i = 0; rerr_names[i].name; i++) {
87 if (rerr_names[i].code == code)
88 return rerr_names[i].name;
94 static void logit(int priority, char *buf)
99 fprintf(logfile,"%s [%d] %s",
100 timestring(time(NULL)), (int)getpid(), buf);
103 syslog(priority, "%s", buf);
109 int options = LOG_PID;
116 /* this looks pointless, but it is needed in order for the
117 * C library on some systems to fetch the timezone info
118 * before the chroot */
122 /* optionally use a log file instead of syslog */
123 logfname = lp_log_file();
133 options |= LOG_NDELAY;
137 openlog("rsyncd", options, lp_syslog_facility());
139 openlog("rsyncd", options);
143 logit(LOG_INFO,"rsyncd started\n");
149 if (logfname && !logfile) {
150 extern int orig_umask;
151 int old_umask = umask(022 | orig_umask);
152 logfile = fopen(logfname, "a");
155 am_daemon = 0; /* avoid trying to log again */
156 rsyserr(FERROR, errno, "fopen() of log-file failed");
157 exit_cleanup(RERR_FILESELECT);
170 /* this is the underlying (unformatted) rsync debugging function. Call
171 * it with FINFO, FERROR or FLOG */
172 void rwrite(enum logcode code, char *buf, int len)
175 /* recursion can happen with certain fatal conditions */
177 if (quiet && code == FINFO)
181 exit_cleanup(RERR_MESSAGEIO);
185 if (am_server && msg_fd_out >= 0) {
186 /* Pass the message to our sibling. */
187 send_msg((enum msgcode)code, buf, len);
193 else if (am_daemon) {
196 int priority = code == FERROR ? LOG_WARNING : LOG_INFO;
201 if (!log_initialised)
203 strlcpy(msg, buf, MIN((int)sizeof msg, len + 1));
204 logit(priority, msg);
207 if (code == FLOG || !am_server)
209 } else if (code == FLOG)
213 /* Pass the message to the non-server side. */
214 if (io_multiplex_write((enum msgcode)code, buf, len))
217 /* TODO: can we send the error to the user somehow? */
222 if (code == FERROR) {
228 f = am_server ? stderr : stdout;
231 exit_cleanup(RERR_MESSAGEIO);
233 if (fwrite(buf, len, 1, f) != 1)
234 exit_cleanup(RERR_MESSAGEIO);
236 if (buf[len-1] == '\r' || buf[len-1] == '\n')
241 /* This is the rsync debugging function. Call it with FINFO, FERROR or
243 void rprintf(enum logcode code, const char *format, ...)
246 char buf[MAXPATHLEN+512];
249 va_start(ap, format);
250 len = vsnprintf(buf, sizeof buf, format, ap);
253 /* Deal with buffer overruns. Instead of panicking, just
254 * truncate the resulting string. (Note that configure ensures
255 * that we have a vsnprintf() that doesn't ever return -1.) */
256 if (len > sizeof buf - 1) {
257 const char ellipsis[] = "[...]";
259 /* Reset length, and zero-terminate the end of our buffer */
260 len = sizeof buf - 1;
263 /* Copy the ellipsis to the end of the string, but give
264 * us one extra character:
266 * v--- null byte at buf[sizeof buf - 1]
268 * -> abcd[...]00 <-- now two null bytes at end
270 * If the input format string has a trailing newline,
271 * we copy it into that extra null; if it doesn't, well,
272 * all we lose is one byte. */
273 strncpy(buf+len-sizeof ellipsis, ellipsis, sizeof ellipsis);
274 if (format[strlen(format)-1] == '\n') {
279 rwrite(code, buf, len);
283 /* This is like rprintf, but it also tries to print some
284 * representation of the error code. Normally errcode = errno.
286 * Unlike rprintf, this always adds a newline and there should not be
287 * one in the format string.
289 * Note that since strerror might involve dynamically loading a
290 * message catalog we need to call it once before chroot-ing. */
291 void rsyserr(enum logcode code, int errcode, const char *format, ...)
294 char buf[MAXPATHLEN+512];
297 strcpy(buf, RSYNC_NAME ": ");
298 len = (sizeof RSYNC_NAME ": ") - 1;
300 va_start(ap, format);
301 len += vsnprintf(buf + len, sizeof buf - len, format, ap);
304 if (len < sizeof buf) {
305 len += snprintf(buf + len, sizeof buf - len,
306 ": %s (%d)\n", strerror(errcode), errcode);
308 if (len >= sizeof buf)
309 exit_cleanup(RERR_MESSAGEIO);
311 rwrite(code, buf, len);
316 void rflush(enum logcode code)
328 if (code == FERROR) {
339 if (!f) exit_cleanup(RERR_MESSAGEIO);
345 /* a generic logging routine for send/recv, with parameter
347 static void log_formatted(enum logcode code,
348 char *format, char *op, struct file_struct *file,
349 struct stats *initial_stats, int iflags)
351 char buf[MAXPATHLEN+1024];
352 char buf2[MAXPATHLEN];
357 /* We expand % codes one by one in place in buf. We don't
358 * copy in the terminating nul of the inserted strings, but
359 * rather keep going until we reach the nul of the format. */
360 total = strlcpy(buf, format, sizeof buf);
362 for (p = buf; (p = strchr(p, '%')) != NULL && p[1]; ) {
366 case 'h': if (am_daemon) n = client_name(0); break;
367 case 'a': if (am_daemon) n = client_addr(0); break;
369 snprintf(buf2, sizeof buf2, "%.0f",
370 (double)file->length);
374 snprintf(buf2, sizeof buf2, "%d",
378 case 'o': n = op; break;
380 pathjoin(buf2, sizeof buf2,
381 am_sender && file->dir.root ? file->dir.root : "",
382 safe_fname(f_name(file)));
383 clean_fname(buf2, 0);
388 n = (char*)safe_fname(f_name(file));
389 if (S_ISDIR(file->mode)) {
390 /* The buffer from safe_fname() has more
391 * room than MAXPATHLEN, so this is safe. */
396 if (S_ISLNK(file->mode) && file->u.link) {
397 snprintf(buf2, sizeof buf2, " -> %s",
398 safe_fname(file->u.link));
403 case 'm': n = lp_name(module_id); break;
404 case 't': n = timestring(time(NULL)); break;
405 case 'P': n = lp_path(module_id); break;
406 case 'u': n = auth_user; break;
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 b = stats.total_written -
421 initial_stats->total_written;
423 b = stats.total_read -
424 initial_stats->total_read;
426 snprintf(buf2, sizeof buf2, "%.0f", (double)b);
430 if (iflags & ITEM_DELETED) {
435 n[0] = !(iflags & ITEM_UPDATING) ? '.'
436 : *op == 's' ? '>' : '<';
437 n[1] = S_ISDIR(file->mode) ? 'd'
438 : IS_DEVICE(file->mode) ? 'D'
439 : S_ISLNK(file->mode) ? 'L' : 'f';
440 n[2] = !(iflags & ITEM_REPORT_CHECKSUM) ? '.' : 'c';
441 n[3] = !(iflags & ITEM_REPORT_SIZE) ? '.' : 's';
442 n[4] = !(iflags & ITEM_REPORT_TIME) ? '.'
443 : !preserve_times || IS_DEVICE(file->mode)
444 || S_ISLNK(file->mode) ? 'T' : 't';
445 n[5] = !(iflags & ITEM_REPORT_PERMS) ? '.' : 'p';
446 n[6] = !(iflags & ITEM_REPORT_OWNER) ? '.' : 'o';
447 n[7] = !(iflags & ITEM_REPORT_GROUP) ? '.' : 'g';
450 if (iflags & (ITEM_IS_NEW|ITEM_MISSING_DATA)) {
451 char ch = iflags & ITEM_IS_NEW ? '+' : '?';
453 for (i = 2; n[i]; i++)
455 } else if (!(iflags & ITEM_UPDATING)) {
457 for (i = 2; n[i]; i++) {
462 for (i = 2; n[i]; i++)
470 /* n is the string to be inserted in place of this %
471 * code; len is its length not including the trailing
480 if (len + total - 2 >= sizeof buf) {
482 "buffer overflow expanding %%%c -- exiting\n",
484 exit_cleanup(RERR_MESSAGEIO);
487 /* Shuffle the rest of the string along to make space for n */
489 memmove(p + len, p + 2, total - (p + 2 - buf) + 1);
492 /* Insert the contents of string "n", but NOT its nul. */
496 /* Skip over inserted string; continue looking */
500 rprintf(code, "%s\n", buf);
503 /* log the outgoing transfer of a file */
504 void log_send(struct file_struct *file, struct stats *initial_stats, int iflags)
506 if (lp_transfer_logging(module_id)) {
507 log_formatted(FLOG, lp_log_format(module_id), "send",
508 file, initial_stats, iflags);
509 } else if (log_format && !am_server) {
510 log_formatted(FINFO, log_format, "send",
511 file, initial_stats, iflags);
515 /* log the incoming transfer of a file */
516 void log_recv(struct file_struct *file, struct stats *initial_stats, int iflags)
518 if (lp_transfer_logging(module_id)) {
519 log_formatted(FLOG, lp_log_format(module_id), "recv",
520 file, initial_stats, iflags);
521 } else if (log_format && !am_server) {
522 log_formatted(FINFO, log_format, "recv",
523 file, initial_stats, iflags);
528 void log_delete(char *fname, int mode)
530 static struct file_struct file;
531 int len = strlen(fname);
535 file.basename = fname;
537 if (am_server && protocol_version >= 29 && len < MAXPATHLEN) {
539 len++; /* directories include trailing null */
540 send_msg(MSG_DELETED, fname, len);
542 fmt = log_format_has_o_or_i ? log_format : "%i %n";
543 log_formatted(FCLIENT, fmt, "del.", &file, &stats, ITEM_DELETED);
546 if (!am_daemon || dry_run)
549 fmt = daemon_log_format_has_o_or_i ? lp_log_format(module_id) : "%i %n";
550 log_formatted(FLOG, fmt, "del.", &file, &stats, ITEM_DELETED);
555 * Called when the transfer is interrupted for some reason.
557 * Code is one of the RERR_* codes from errcode.h, or terminating
560 void log_exit(int code, const char *file, int line)
563 rprintf(FLOG,"sent %.0f bytes received %.0f bytes total size %.0f\n",
564 (double)stats.total_written,
565 (double)stats.total_read,
566 (double)stats.total_size);
570 name = rerr_name(code);
572 name = "unexplained error";
574 /* VANISHED is not an error, only a warning */
575 if (code == RERR_VANISHED) {
576 rprintf(FINFO, "rsync warning: %s (code %d) at %s(%d)\n",
577 name, code, file, line);
579 rprintf(FERROR, "rsync error: %s (code %d) at %s(%d)\n",
580 name, code, file, line);