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