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