Changed RERR_CRASH to RERR_CRASHED.
[rsync/rsync.git] / log.c
... / ...
CommitLineData
1/* -*- c-file-style: "linux"; -*-
2
3 Copyright (C) 1998-2001 by Andrew Tridgell <tridge@samba.org>
4 Copyright (C) 2000-2001 by Martin Pool <mbp@samba.org>
5
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.
10
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.
15
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.
19*/
20
21/*
22 Logging and utility functions.
23 tridge, May 1998
24
25 Mapping to human-readable messages added by Martin Pool
26 <mbp@samba.org>, Oct 2000.
27 */
28#include "rsync.h"
29
30extern int verbose;
31extern int dry_run;
32extern int am_daemon;
33extern int am_server;
34extern int am_sender;
35extern int local_server;
36extern int quiet;
37extern int module_id;
38extern int msg_fd_out;
39extern int protocol_version;
40extern int preserve_times;
41extern int log_format_has_o_or_i;
42extern int daemon_log_format_has_o_or_i;
43extern char *auth_user;
44extern char *log_format;
45
46static int log_initialised;
47static int logfile_was_closed;
48static char *logfname;
49static FILE *logfile;
50struct stats stats;
51
52int log_got_error = 0;
53
54struct {
55 int code;
56 char const *name;
57} const rerr_names[] = {
58 { RERR_SYNTAX , "syntax or usage error" },
59 { RERR_PROTOCOL , "protocol incompatibility" },
60 { RERR_FILESELECT , "errors selecting input/output files, dirs" },
61 { RERR_UNSUPPORTED, "requested action not supported" },
62 { RERR_STARTCLIENT, "error starting client-server protocol" },
63 { RERR_SOCKETIO , "error in socket IO" },
64 { RERR_FILEIO , "error in file IO" },
65 { RERR_STREAMIO , "error in rsync protocol data stream" },
66 { RERR_MESSAGEIO , "errors with program diagnostics" },
67 { RERR_IPC , "error in IPC code" },
68 { RERR_CRASHED , "sibling process crashed" },
69 { RERR_TERMINATED , "sibling process terminated abnormally" },
70 { RERR_SIGNAL , "received SIGUSR1 or SIGINT" },
71 { RERR_WAITCHILD , "some error returned by waitpid()" },
72 { RERR_MALLOC , "error allocating core memory buffers" },
73 { RERR_PARTIAL , "some files could not be transferred" },
74 { RERR_VANISHED , "some files vanished before they could be transferred" },
75 { RERR_TIMEOUT , "timeout in data send/receive" },
76 { RERR_CMD_FAILED , "remote shell failed" },
77 { RERR_CMD_KILLED , "remote shell killed" },
78 { RERR_CMD_RUN , "remote command could not be run" },
79 { RERR_CMD_NOTFOUND,"remote command not found" },
80 { RERR_DEL_LIMIT , "the --max-delete limit stopped deletions" },
81 { 0, NULL }
82};
83
84
85
86/*
87 * Map from rsync error code to name, or return NULL.
88 */
89static char const *rerr_name(int code)
90{
91 int i;
92 for (i = 0; rerr_names[i].name; i++) {
93 if (rerr_names[i].code == code)
94 return rerr_names[i].name;
95 }
96 return NULL;
97}
98
99
100static void logit(int priority, char *buf)
101{
102 if (logfile_was_closed)
103 logfile_reopen();
104 if (logfile) {
105 fprintf(logfile,"%s [%d] %s",
106 timestring(time(NULL)), (int)getpid(), buf);
107 fflush(logfile);
108 } else {
109 syslog(priority, "%s", buf);
110 }
111}
112
113static void syslog_init()
114{
115 static int been_here = 0;
116 int options = LOG_PID;
117
118 if (been_here)
119 return;
120 been_here = 1;
121
122#ifdef LOG_NDELAY
123 options |= LOG_NDELAY;
124#endif
125
126#ifdef LOG_DAEMON
127 openlog("rsyncd", options, lp_syslog_facility());
128#else
129 openlog("rsyncd", options);
130#endif
131
132#ifndef LOG_NDELAY
133 logit(LOG_INFO, "rsyncd started\n");
134#endif
135}
136
137static void logfile_open(void)
138{
139 extern int orig_umask;
140 int old_umask = umask(022 | orig_umask);
141 logfile = fopen(logfname, "a");
142 umask(old_umask);
143 if (!logfile) {
144 int fopen_errno = errno;
145 /* Rsync falls back to using syslog on failure. */
146 syslog_init();
147 rsyserr(FERROR, fopen_errno,
148 "failed to open log-file %s", logfname);
149 rprintf(FINFO, "Ignoring \"log file\" setting.\n");
150 }
151}
152
153void log_init(void)
154{
155 time_t t;
156
157 if (log_initialised)
158 return;
159 log_initialised = 1;
160
161 /* this looks pointless, but it is needed in order for the
162 * C library on some systems to fetch the timezone info
163 * before the chroot */
164 t = time(NULL);
165 localtime(&t);
166
167 /* optionally use a log file instead of syslog */
168 logfname = lp_log_file();
169 if (logfname && *logfname)
170 logfile_open();
171 else
172 syslog_init();
173}
174
175void logfile_close(void)
176{
177 if (logfile) {
178 logfile_was_closed = 1;
179 fclose(logfile);
180 logfile = NULL;
181 }
182}
183
184void logfile_reopen(void)
185{
186 if (logfile_was_closed) {
187 logfile_was_closed = 0;
188 logfile_open();
189 }
190}
191
192/* this is the underlying (unformatted) rsync debugging function. Call
193 * it with FINFO, FERROR or FLOG */
194void rwrite(enum logcode code, char *buf, int len)
195{
196 FILE *f = NULL;
197 /* recursion can happen with certain fatal conditions */
198
199 if (quiet && code == FINFO)
200 return;
201
202 if (len < 0)
203 exit_cleanup(RERR_MESSAGEIO);
204
205 buf[len] = 0;
206
207 if (am_server && msg_fd_out >= 0) {
208 /* Pass the message to our sibling. */
209 send_msg((enum msgcode)code, buf, len);
210 return;
211 }
212
213 if (code == FCLIENT)
214 code = FINFO;
215 else if (am_daemon) {
216 static int in_block;
217 char msg[2048];
218 int priority = code == FERROR ? LOG_WARNING : LOG_INFO;
219
220 if (in_block)
221 return;
222 in_block = 1;
223 if (!log_initialised)
224 log_init();
225 strlcpy(msg, buf, MIN((int)sizeof msg, len + 1));
226 logit(priority, msg);
227 in_block = 0;
228
229 if (code == FLOG || !am_server)
230 return;
231 } else if (code == FLOG)
232 return;
233
234 if (am_server) {
235 /* Pass the message to the non-server side. */
236 if (io_multiplex_write((enum msgcode)code, buf, len))
237 return;
238 if (am_daemon) {
239 /* TODO: can we send the error to the user somehow? */
240 return;
241 }
242 }
243
244 if (code == FERROR) {
245 log_got_error = 1;
246 f = stderr;
247 }
248
249 if (code == FINFO)
250 f = am_server ? stderr : stdout;
251
252 if (!f)
253 exit_cleanup(RERR_MESSAGEIO);
254
255 if (fwrite(buf, len, 1, f) != 1)
256 exit_cleanup(RERR_MESSAGEIO);
257
258 if (buf[len-1] == '\r' || buf[len-1] == '\n')
259 fflush(f);
260}
261
262
263/* This is the rsync debugging function. Call it with FINFO, FERROR or
264 * FLOG. */
265void rprintf(enum logcode code, const char *format, ...)
266{
267 va_list ap;
268 char buf[MAXPATHLEN+512];
269 size_t len;
270
271 va_start(ap, format);
272 len = vsnprintf(buf, sizeof buf, format, ap);
273 va_end(ap);
274
275 /* Deal with buffer overruns. Instead of panicking, just
276 * truncate the resulting string. (Note that configure ensures
277 * that we have a vsnprintf() that doesn't ever return -1.) */
278 if (len > sizeof buf - 1) {
279 const char ellipsis[] = "[...]";
280
281 /* Reset length, and zero-terminate the end of our buffer */
282 len = sizeof buf - 1;
283 buf[len] = '\0';
284
285 /* Copy the ellipsis to the end of the string, but give
286 * us one extra character:
287 *
288 * v--- null byte at buf[sizeof buf - 1]
289 * abcdefghij0
290 * -> abcd[...]00 <-- now two null bytes at end
291 *
292 * If the input format string has a trailing newline,
293 * we copy it into that extra null; if it doesn't, well,
294 * all we lose is one byte. */
295 strncpy(buf+len-sizeof ellipsis, ellipsis, sizeof ellipsis);
296 if (format[strlen(format)-1] == '\n') {
297 buf[len-1] = '\n';
298 }
299 }
300
301 rwrite(code, buf, len);
302}
303
304
305/* This is like rprintf, but it also tries to print some
306 * representation of the error code. Normally errcode = errno.
307 *
308 * Unlike rprintf, this always adds a newline and there should not be
309 * one in the format string.
310 *
311 * Note that since strerror might involve dynamically loading a
312 * message catalog we need to call it once before chroot-ing. */
313void rsyserr(enum logcode code, int errcode, const char *format, ...)
314{
315 va_list ap;
316 char buf[MAXPATHLEN+512];
317 size_t len;
318
319 strcpy(buf, RSYNC_NAME ": ");
320 len = (sizeof RSYNC_NAME ": ") - 1;
321
322 va_start(ap, format);
323 len += vsnprintf(buf + len, sizeof buf - len, format, ap);
324 va_end(ap);
325
326 if (len < sizeof buf) {
327 len += snprintf(buf + len, sizeof buf - len,
328 ": %s (%d)\n", strerror(errcode), errcode);
329 }
330 if (len >= sizeof buf)
331 exit_cleanup(RERR_MESSAGEIO);
332
333 rwrite(code, buf, len);
334}
335
336
337
338void rflush(enum logcode code)
339{
340 FILE *f = NULL;
341
342 if (am_daemon) {
343 return;
344 }
345
346 if (code == FLOG) {
347 return;
348 }
349
350 if (code == FERROR) {
351 f = stderr;
352 }
353
354 if (code == FINFO) {
355 if (am_server)
356 f = stderr;
357 else
358 f = stdout;
359 }
360
361 if (!f) exit_cleanup(RERR_MESSAGEIO);
362 fflush(f);
363}
364
365
366
367/* a generic logging routine for send/recv, with parameter
368 * substitiution */
369static void log_formatted(enum logcode code, char *format, char *op,
370 struct file_struct *file, struct stats *initial_stats,
371 int iflags, char *hlink)
372{
373 char buf[MAXPATHLEN+1024], buf2[MAXPATHLEN], fmt[32];
374 char *p, *s, *n;
375 size_t len, total;
376 int64 b;
377
378 *fmt = '%';
379
380 /* We expand % codes one by one in place in buf. We don't
381 * copy in the terminating null of the inserted strings, but
382 * rather keep going until we reach the null of the format. */
383 total = strlcpy(buf, format, sizeof buf);
384 if (total > MAXPATHLEN) {
385 rprintf(FERROR, "log-format string is WAY too long!\n");
386 exit_cleanup(RERR_MESSAGEIO);
387 }
388 buf[total++] = '\n';
389 buf[total] = '\0';
390
391 for (p = buf; (p = strchr(p, '%')) != NULL; ) {
392 s = p++;
393 n = fmt + 1;
394 if (*p == '-')
395 *n++ = *p++;
396 while (isdigit(*(uchar*)p) && n - fmt < (int)(sizeof fmt) - 8)
397 *n++ = *p++;
398 if (!*p)
399 break;
400 *n = '\0';
401 n = NULL;
402
403 switch (*p) {
404 case 'h': if (am_daemon) n = client_name(0); break;
405 case 'a': if (am_daemon) n = client_addr(0); break;
406 case 'l':
407 strlcat(fmt, ".0f", sizeof fmt);
408 snprintf(buf2, sizeof buf2, fmt,
409 (double)file->length);
410 n = buf2;
411 break;
412 case 'p':
413 strlcat(fmt, "ld", sizeof fmt);
414 snprintf(buf2, sizeof buf2, fmt,
415 (long)getpid());
416 n = buf2;
417 break;
418 case 'o': n = op; break;
419 case 'f':
420 n = safe_fname(f_name(file));
421 if (am_sender && file->dir.root) {
422 pathjoin(buf2, sizeof buf2,
423 file->dir.root, n);
424 /* The buffer from safe_fname() has more
425 * room than MAXPATHLEN, so this is safe. */
426 if (fmt[1])
427 strcpy(n, buf2);
428 else
429 n = buf2;
430 }
431 clean_fname(n, 0);
432 if (*n == '/')
433 n++;
434 break;
435 case 'n':
436 n = safe_fname(f_name(file));
437 if (S_ISDIR(file->mode)) {
438 /* The buffer from safe_fname() has more
439 * room than MAXPATHLEN, so this is safe. */
440 strcat(n, "/");
441 }
442 break;
443 case 'L':
444 if (hlink && *hlink) {
445 n = safe_fname(hlink);
446 strcpy(buf2, " => ");
447 } else if (S_ISLNK(file->mode) && file->u.link) {
448 n = safe_fname(file->u.link);
449 strcpy(buf2, " -> ");
450 } else {
451 n = "";
452 if (!fmt[1])
453 break;
454 strcpy(buf2, " ");
455 }
456 strlcat(fmt, "s", sizeof fmt);
457 snprintf(buf2 + 4, sizeof buf2 - 4, fmt, n);
458 n = buf2;
459 break;
460 case 'm': n = lp_name(module_id); break;
461 case 't': n = timestring(time(NULL)); break;
462 case 'P': n = lp_path(module_id); break;
463 case 'u': n = auth_user; break;
464 case 'b':
465 if (am_sender) {
466 b = stats.total_written -
467 initial_stats->total_written;
468 } else {
469 b = stats.total_read -
470 initial_stats->total_read;
471 }
472 strlcat(fmt, ".0f", sizeof fmt);
473 snprintf(buf2, sizeof buf2, fmt, (double)b);
474 n = buf2;
475 break;
476 case 'c':
477 if (!am_sender) {
478 b = stats.total_written -
479 initial_stats->total_written;
480 } else {
481 b = stats.total_read -
482 initial_stats->total_read;
483 }
484 strlcat(fmt, ".0f", sizeof fmt);
485 snprintf(buf2, sizeof buf2, fmt, (double)b);
486 n = buf2;
487 break;
488 case 'i':
489 if (iflags & ITEM_DELETED) {
490 n = "*deleting";
491 break;
492 }
493 n = buf2 + MAXPATHLEN - 32;
494 n[0] = iflags & ITEM_LOCAL_CHANGE
495 ? iflags & ITEM_XNAME_FOLLOWS ? 'h' : 'c'
496 : !(iflags & ITEM_TRANSFER) ? '.'
497 : !local_server && *op == 's' ? '<' : '>';
498 n[1] = S_ISDIR(file->mode) ? 'd'
499 : IS_DEVICE(file->mode) ? 'D'
500 : S_ISLNK(file->mode) ? 'L' : 'f';
501 n[2] = !(iflags & ITEM_REPORT_CHECKSUM) ? '.' : 'c';
502 n[3] = !(iflags & ITEM_REPORT_SIZE) ? '.' : 's';
503 n[4] = !(iflags & ITEM_REPORT_TIME) ? '.'
504 : !preserve_times || IS_DEVICE(file->mode)
505 || S_ISLNK(file->mode) ? 'T' : 't';
506 n[5] = !(iflags & ITEM_REPORT_PERMS) ? '.' : 'p';
507 n[6] = !(iflags & ITEM_REPORT_OWNER) ? '.' : 'o';
508 n[7] = !(iflags & ITEM_REPORT_GROUP) ? '.' : 'g';
509 n[8] = !(iflags & ITEM_REPORT_XATTRS) ? '.' : 'a';
510 n[9] = '\0';
511
512 if (iflags & (ITEM_IS_NEW|ITEM_MISSING_DATA)) {
513 char ch = iflags & ITEM_IS_NEW ? '+' : '?';
514 int i;
515 for (i = 2; n[i]; i++)
516 n[i] = ch;
517 } else if (n[0] == '.' || n[0] == 'h') {
518 int i;
519 for (i = 2; n[i]; i++) {
520 if (n[i] != '.')
521 break;
522 }
523 if (!n[i]) {
524 for (i = 2; n[i]; i++)
525 n[i] = ' ';
526 }
527 }
528 break;
529 }
530
531 /* "n" is the string to be inserted in place of this % code. */
532 if (!n)
533 continue;
534 if (n != buf2 && fmt[1]) {
535 strlcat(fmt, "s", sizeof fmt);
536 snprintf(buf2, sizeof buf2, fmt, n);
537 n = buf2;
538 }
539 len = strlen(n);
540
541 /* Subtract the length of the escape from the string's size. */
542 total -= p - s + 1;
543
544 if (len + total >= (size_t)sizeof buf) {
545 rprintf(FERROR,
546 "buffer overflow expanding %%%c -- exiting\n",
547 p[0]);
548 exit_cleanup(RERR_MESSAGEIO);
549 }
550
551 /* Shuffle the rest of the string along to make space for n */
552 if (len != (size_t)(p - s + 1))
553 memmove(s + len, p + 1, total - (s - buf) + 1);
554 total += len;
555
556 /* Insert the contents of string "n", but NOT its null. */
557 if (len)
558 memcpy(s, n, len);
559
560 /* Skip over inserted string; continue looking */
561 p = s + len;
562 }
563
564 rwrite(code, buf, total);
565}
566
567/* Return 1 if the format escape is in the log-format string (e.g. look for
568 * the 'b' in the "%9b" format escape). */
569int log_format_has(const char *format, char esc)
570{
571 const char *p;
572
573 if (!format)
574 return 0;
575
576 for (p = format; (p = strchr(p, '%')) != NULL; ) {
577 if (*++p == '-')
578 p++;
579 while (isdigit(*(uchar*)p))
580 p++;
581 if (!*p)
582 break;
583 if (*p == esc)
584 return 1;
585 }
586 return 0;
587}
588
589/* log the transfer of a file */
590void log_item(struct file_struct *file, struct stats *initial_stats,
591 int iflags, char *hlink)
592{
593 char *s_or_r = am_sender ? "send" : "recv";
594
595 if (lp_transfer_logging(module_id)) {
596 log_formatted(FLOG, lp_log_format(module_id), s_or_r,
597 file, initial_stats, iflags, hlink);
598 } else if (log_format && !am_server) {
599 log_formatted(FINFO, log_format, s_or_r,
600 file, initial_stats, iflags, hlink);
601 }
602}
603
604void maybe_log_item(struct file_struct *file, int iflags, int itemizing,
605 char *buf)
606{
607 int significant_flags = iflags & SIGNIFICANT_ITEM_FLAGS;
608 int see_item = itemizing && (significant_flags || *buf || verbose > 1);
609 int local_change = iflags & ITEM_LOCAL_CHANGE
610 && (!(iflags & ITEM_XNAME_FOLLOWS) || significant_flags);
611 if (am_server) {
612 if (am_daemon && !dry_run && see_item)
613 log_item(file, &stats, iflags, buf);
614 } else if (see_item || local_change || *buf
615 || (S_ISDIR(file->mode) && significant_flags))
616 log_item(file, &stats, iflags, buf);
617}
618
619void log_delete(char *fname, int mode)
620{
621 static struct file_struct file;
622 int len = strlen(fname);
623 char *fmt;
624
625 file.mode = mode;
626 file.basename = fname;
627
628 if (!verbose && !log_format)
629 ;
630 else if (am_server && protocol_version >= 29 && len < MAXPATHLEN) {
631 if (S_ISDIR(mode))
632 len++; /* directories include trailing null */
633 send_msg(MSG_DELETED, fname, len);
634 } else {
635 fmt = log_format_has_o_or_i ? log_format : "deleting %n";
636 log_formatted(FCLIENT, fmt, "del.", &file, &stats,
637 ITEM_DELETED, NULL);
638 }
639
640 if (!am_daemon || dry_run || !lp_transfer_logging(module_id))
641 return;
642
643 fmt = daemon_log_format_has_o_or_i ? lp_log_format(module_id) : "deleting %n";
644 log_formatted(FLOG, fmt, "del.", &file, &stats, ITEM_DELETED, NULL);
645}
646
647
648/*
649 * Called when the transfer is interrupted for some reason.
650 *
651 * Code is one of the RERR_* codes from errcode.h, or terminating
652 * successfully.
653 */
654void log_exit(int code, const char *file, int line)
655{
656 if (code == 0) {
657 rprintf(FLOG,"sent %.0f bytes received %.0f bytes total size %.0f\n",
658 (double)stats.total_written,
659 (double)stats.total_read,
660 (double)stats.total_size);
661 } else {
662 const char *name;
663
664 name = rerr_name(code);
665 if (!name)
666 name = "unexplained error";
667
668 /* VANISHED is not an error, only a warning */
669 if (code == RERR_VANISHED) {
670 rprintf(FINFO, "rsync warning: %s (code %d) at %s(%d)\n",
671 name, code, file, line);
672 } else {
673 rprintf(FERROR, "rsync error: %s (code %d) at %s(%d)\n",
674 name, code, file, line);
675 }
676 }
677}