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