Change wording of --compare-dest option to refer to the --link-dest option.
[rsync/rsync.git] / log.c
CommitLineData
a039749b
MP
1/* -*- c-file-style: "linux"; -*-
2
0c5a792a 3 Copyright (C) 1998-2001 by Andrew Tridgell <tridge@samba.org>
18c71e96 4 Copyright (C) 2000-2001 by Martin Pool <mbp@samba.org>
0b76cd63
AT
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/*
3e3dcd62 22 Logging and utility functions.
0c5a792a 23 tridge, May 1998
0b76cd63 24
3e3dcd62
MP
25 Mapping to human-readable messages added by Martin Pool
26 <mbp@samba.org>, Oct 2000.
0b76cd63
AT
27 */
28#include "rsync.h"
29
45a83540 30static char *logfname;
4f6325c3 31static FILE *logfile;
554e0a8d 32static int log_error_fd = -1;
b35d0d8e 33struct stats stats;
e0414f42 34
ff81e809 35int log_got_error=0;
af642a61
MP
36
37struct {
38 int code;
39 char const *name;
40} const rerr_names[] = {
41 { RERR_SYNTAX , "syntax or usage error" },
42 { RERR_PROTOCOL , "protocol incompatibility" },
43 { RERR_FILESELECT , "errors selecting input/output files, dirs" },
19b27a48 44 { RERR_UNSUPPORTED, "requested action not supported" },
088aac85 45 { RERR_STARTCLIENT, "error starting client-server protocol" },
af642a61
MP
46 { RERR_SOCKETIO , "error in socket IO" },
47 { RERR_FILEIO , "error in file IO" },
48 { RERR_STREAMIO , "error in rsync protocol data stream" },
49 { RERR_MESSAGEIO , "errors with program diagnostics" },
50 { RERR_IPC , "error in IPC code" },
3e3dcd62 51 { RERR_SIGNAL , "received SIGUSR1 or SIGINT" },
af642a61
MP
52 { RERR_WAITCHILD , "some error returned by waitpid()" },
53 { RERR_MALLOC , "error allocating core memory buffers" },
a83600cc 54 { RERR_PARTIAL , "some files could not be transferred" },
af642a61 55 { RERR_TIMEOUT , "timeout in data send/receive" },
19b27a48
AT
56 { RERR_CMD_FAILED , "remote shell failed" },
57 { RERR_CMD_KILLED , "remote shell killed" },
58 { RERR_CMD_RUN, "remote command could not be run" },
59 { RERR_CMD_NOTFOUND, "remote command not found" },
af642a61
MP
60 { 0, NULL }
61};
62
63
64
65/*
66 * Map from rsync error code to name, or return NULL.
67 */
68static char const *rerr_name(int code)
69{
70 int i;
71 for (i = 0; rerr_names[i].name; i++) {
72 if (rerr_names[i].code == code)
73 return rerr_names[i].name;
74 }
75 return NULL;
76}
77
90ba34e2
AT
78struct err_list {
79 struct err_list *next;
80 char *buf;
81 int len;
82 int written; /* how many bytes we have written so far */
83};
84
85static struct err_list *err_list_head;
86static struct err_list *err_list_tail;
87
88/* add an error message to the pending error list */
89static void err_list_add(int code, char *buf, int len)
90{
91 struct err_list *el;
92 el = (struct err_list *)malloc(sizeof(*el));
93 if (!el) exit_cleanup(RERR_MALLOC);
94 el->next = NULL;
95 el->buf = malloc(len+4);
96 if (!el->buf) exit_cleanup(RERR_MALLOC);
97 memcpy(el->buf+4, buf, len);
98 SIVAL(el->buf, 0, ((code+MPLEX_BASE)<<24) | len);
99 el->len = len+4;
100 el->written = 0;
101 if (err_list_tail) {
102 err_list_tail->next = el;
103 } else {
104 err_list_head = el;
105 }
106 err_list_tail = el;
107}
108
109
110/* try to push errors off the error list onto the wire */
111void err_list_push(void)
112{
113 if (log_error_fd == -1) return;
114
115 while (err_list_head) {
116 struct err_list *el = err_list_head;
117 int n = write(log_error_fd, el->buf+el->written, el->len - el->written);
ff81e809
AT
118 /* don't check for an error if the best way of handling the error is
119 to ignore it */
90ba34e2
AT
120 if (n == -1) break;
121 if (n > 0) {
122 el->written += n;
123 }
124 if (el->written == el->len) {
125 free(el->buf);
126 err_list_head = el->next;
127 if (!err_list_head) err_list_tail = NULL;
128 free(el);
129 }
130 }
131}
132
af642a61 133
4f6325c3
AT
134static void logit(int priority, char *buf)
135{
45a83540 136 if (logfname) {
15b84e14
DD
137 if (!logfile)
138 log_open();
67ea0d48 139 fprintf(logfile,"%s [%d] %s",
f7632fc6 140 timestring(time(NULL)), (int)getpid(), buf);
4f6325c3
AT
141 fflush(logfile);
142 } else {
143 syslog(priority, "%s", buf);
144 }
145}
e42c9458 146
45a83540 147void log_init(void)
1a016bfd
AT
148{
149 static int initialised;
150 int options = LOG_PID;
bcf5b133 151 time_t t;
1a016bfd
AT
152
153 if (initialised) return;
154 initialised = 1;
155
958f3735
AT
156 /* this looks pointless, but it is needed in order for the
157 C library on some systems to fetch the timezone info
158 before the chroot */
159 t = time(NULL);
160 localtime(&t);
161
162 /* optionally use a log file instead of syslog */
45a83540
DD
163 logfname = lp_log_file();
164 if (logfname) {
15b84e14
DD
165 if (*logfname) {
166 log_open();
45a83540 167 return;
15b84e14 168 }
45a83540 169 logfname = NULL;
4f6325c3
AT
170 }
171
1a016bfd
AT
172#ifdef LOG_NDELAY
173 options |= LOG_NDELAY;
174#endif
175
176#ifdef LOG_DAEMON
177 openlog("rsyncd", options, lp_syslog_facility());
178#else
179 openlog("rsyncd", options);
180#endif
181
182#ifndef LOG_NDELAY
4f6325c3 183 logit(LOG_INFO,"rsyncd started\n");
1a016bfd
AT
184#endif
185}
1a016bfd 186
15b84e14
DD
187void log_open()
188{
189 if (logfname && !logfile) {
190 extern int orig_umask;
191 int old_umask = umask(022 | orig_umask);
192 logfile = fopen(logfname, "a");
193 umask(old_umask);
194 }
195}
196
197void log_close()
45a83540
DD
198{
199 if (logfile) {
200 fclose(logfile);
201 logfile = NULL;
202 }
203}
204
554e0a8d
AT
205/* setup the error file descriptor - used when we are a server
206 that is receiving files */
207void set_error_fd(int fd)
208{
209 log_error_fd = fd;
90ba34e2 210 set_nonblocking(log_error_fd);
554e0a8d
AT
211}
212
213/* this is the underlying (unformatted) rsync debugging function. Call
214 it with FINFO, FERROR or FLOG */
ff41a59f 215void rwrite(enum logcode code, char *buf, int len)
0b76cd63 216{
0b76cd63
AT
217 FILE *f=NULL;
218 extern int am_daemon;
4a814638 219 extern int am_server;
b86f0cef 220 extern int quiet;
8d9dc9f9 221 /* recursion can happen with certain fatal conditions */
8d9dc9f9 222
6d7b6081 223 if (quiet && code == FINFO) return;
b86f0cef 224
65417579 225 if (len < 0) exit_cleanup(RERR_MESSAGEIO);
0b76cd63 226
ff8b29b8
AT
227 buf[len] = 0;
228
ff41a59f 229 if (code == FLOG) {
11a5a3c7 230 if (am_daemon) logit(LOG_INFO, buf);
11a5a3c7
AT
231 return;
232 }
233
90ba34e2
AT
234 /* first try to pass it off to our sibling */
235 if (am_server && log_error_fd != -1) {
236 err_list_add(code, buf, len);
237 err_list_push();
6d7b6081
AT
238 return;
239 }
240
900748fc
MP
241 /* If that fails, try to pass it to the other end.
242 *
243 * io_multiplex_write can fail if we do not have a multiplexed
244 * connection at the moment, in which case we fall through and
245 * log locally instead. */
4a814638 246 if (am_server && io_multiplex_write(code, buf, len)) {
6d7b6081
AT
247 return;
248 }
ff41a59f 249
ff8b29b8 250 if (am_daemon) {
b24203b3 251 static int depth;
ff8b29b8 252 int priority = LOG_INFO;
ff41a59f 253 if (code == FERROR) priority = LOG_WARNING;
45ccc5c0 254
b24203b3
AT
255 if (depth) return;
256
f27b53f5
AT
257 depth++;
258
45a83540 259 log_init();
6d7b6081 260 logit(priority, buf);
8d9dc9f9
AT
261
262 depth--;
ff8b29b8 263 return;
0b76cd63
AT
264 }
265
ff41a59f 266 if (code == FERROR) {
ff81e809 267 log_got_error = 1;
ff8b29b8
AT
268 f = stderr;
269 }
270
ff41a59f 271 if (code == FINFO) {
ff8b29b8
AT
272 if (am_server)
273 f = stderr;
274 else
275 f = stdout;
276 }
277
65417579 278 if (!f) exit_cleanup(RERR_MESSAGEIO);
0b76cd63 279
65417579 280 if (fwrite(buf, len, 1, f) != 1) exit_cleanup(RERR_MESSAGEIO);
8d9dc9f9 281
bcf5b133 282 if (buf[len-1] == '\r' || buf[len-1] == '\n') fflush(f);
0b76cd63 283}
554e0a8d
AT
284
285
a039749b
MP
286/* This is the rsync debugging function. Call it with FINFO, FERROR or
287 * FLOG. */
288void rprintf(enum logcode code, const char *format, ...)
554e0a8d
AT
289{
290 va_list ap;
291 char buf[1024];
292 int len;
293
294 va_start(ap, format);
59ee743c 295 /* Note: might return -1 */
8950ac03 296 len = vsnprintf(buf, sizeof(buf), format, ap);
554e0a8d
AT
297 va_end(ap);
298
b2f02464
MP
299 /* Deal with buffer overruns. Instead of panicking, just
300 * truncate the resulting string. Note that some vsnprintf()s
301 * return -1 on truncation, e.g., glibc 2.0.6 and earlier. */
59ee743c 302 if ((size_t) len > sizeof(buf)-1 || len < 0) {
b2f02464
MP
303 const char ellipsis[] = "[...]";
304
305 /* Reset length, and zero-terminate the end of our buffer */
306 len = sizeof(buf)-1;
307 buf[len] = '\0';
308
309 /* Copy the ellipsis to the end of the string, but give
310 * us one extra character:
311 *
312 * v--- null byte at buf[sizeof(buf)-1]
313 * abcdefghij0
314 * -> abcd[...]00 <-- now two null bytes at end
315 *
316 * If the input format string has a trailing newline,
317 * we copy it into that extra null; if it doesn't, well,
318 * all we lose is one byte. */
319 strncpy(buf+len-sizeof(ellipsis), ellipsis, sizeof(ellipsis));
320 if (format[strlen(format)-1] == '\n') {
321 buf[len-1] = '\n';
322 }
323 }
554e0a8d 324
ff41a59f 325 rwrite(code, buf, len);
554e0a8d 326}
0b76cd63 327
a039749b
MP
328
329/* This is like rprintf, but it also tries to print some
330 * representation of the error code. Normally errcode = errno.
331 *
332 * Unlike rprintf, this always adds a newline and there should not be
333 * one in the format string.
334 *
335 * Note that since strerror might involve dynamically loading a
336 * message catalog we need to call it once before chroot-ing. */
337void rsyserr(enum logcode code, int errcode, const char *format, ...)
338{
339 va_list ap;
340 char buf[1024];
59ee743c
MP
341 int len;
342 size_t sys_len;
a039749b
MP
343 char *sysmsg;
344
345 va_start(ap, format);
59ee743c 346 /* Note: might return <0 */
8950ac03 347 len = vsnprintf(buf, sizeof(buf), format, ap);
a039749b
MP
348 va_end(ap);
349
4f092bee
MP
350 /* TODO: Put in RSYNC_NAME at the start. */
351
59ee743c
MP
352 if ((size_t) len > sizeof(buf)-1)
353 exit_cleanup(RERR_MESSAGEIO);
a039749b
MP
354
355 sysmsg = strerror(errcode);
356 sys_len = strlen(sysmsg);
59ee743c 357 if ((size_t) len + 3 + sys_len > sizeof(buf) - 1)
a039749b
MP
358 exit_cleanup(RERR_MESSAGEIO);
359
360 strcpy(buf + len, ": ");
361 len += 2;
362 strcpy(buf + len, sysmsg);
363 len += sys_len;
364 strcpy(buf + len, "\n");
365 len++;
366
367 rwrite(code, buf, len);
368}
369
370
371
ff41a59f 372void rflush(enum logcode code)
0b76cd63
AT
373{
374 FILE *f = NULL;
375 extern int am_daemon;
376
377 if (am_daemon) {
378 return;
379 }
380
ff41a59f 381 if (code == FLOG) {
e08bfe12
AT
382 return;
383 }
384
ff41a59f 385 if (code == FERROR) {
0b76cd63
AT
386 f = stderr;
387 }
388
ff41a59f 389 if (code == FINFO) {
0b76cd63
AT
390 extern int am_server;
391 if (am_server)
392 f = stderr;
393 else
394 f = stdout;
395 }
396
65417579 397 if (!f) exit_cleanup(RERR_MESSAGEIO);
0b76cd63
AT
398 fflush(f);
399}
400
11a5a3c7 401
e08bfe12
AT
402
403/* a generic logging routine for send/recv, with parameter
404 substitiution */
0f3203c3 405static void log_formatted(enum logcode code,
b6062654 406 char *format, char *op, struct file_struct *file,
1b7c47cb 407 struct stats *initial_stats)
e08bfe12
AT
408{
409 extern int module_id;
97cb8dc2 410 extern char *auth_user;
e08bfe12 411 char buf[1024];
ab7104da 412 char buf2[1024];
e08bfe12 413 char *p, *s, *n;
59ee743c 414 size_t l;
1b7c47cb
AT
415 extern struct stats stats;
416 extern int am_sender;
af77cc6b 417 extern int am_daemon;
1b7c47cb 418 int64 b;
e08bfe12 419
aa126974
MP
420 /* We expand % codes one by one in place in buf. We don't
421 * copy in the terminating nul of the inserted strings, but
422 * rather keep going until we reach the nul of the format.
423 * Just to make sure we don't clobber that nul and therefore
424 * accidentally keep going, we zero the buffer now. */
425 memset(buf, 0, sizeof buf);
37f9805d 426 strlcpy(buf, format, sizeof(buf));
e08bfe12
AT
427
428 for (s=&buf[0];
429 s && (p=strchr(s,'%')); ) {
430 n = NULL;
431 s = p + 1;
432
433 switch (p[1]) {
af77cc6b
AT
434 case 'h': if (am_daemon) n = client_name(0); break;
435 case 'a': if (am_daemon) n = client_addr(0); break;
e08bfe12 436 case 'l':
8950ac03 437 snprintf(buf2,sizeof(buf2),"%.0f",
e08bfe12
AT
438 (double)file->length);
439 n = buf2;
440 break;
441 case 'p':
8950ac03 442 snprintf(buf2,sizeof(buf2),"%d",
e08bfe12
AT
443 (int)getpid());
444 n = buf2;
445 break;
446 case 'o': n = op; break;
ab7104da 447 case 'f':
8950ac03 448 snprintf(buf2, sizeof(buf2), "%s/%s",
ab7104da
AT
449 file->basedir?file->basedir:"",
450 f_name(file));
451 clean_fname(buf2);
452 n = buf2;
b6062654 453 if (*n == '/') n++;
ab7104da 454 break;
97cb8dc2 455 case 'm': n = lp_name(module_id); break;
b6062654 456 case 't': n = timestring(time(NULL)); break;
97cb8dc2
AT
457 case 'P': n = lp_path(module_id); break;
458 case 'u': n = auth_user; break;
1b7c47cb
AT
459 case 'b':
460 if (am_sender) {
461 b = stats.total_written -
462 initial_stats->total_written;
463 } else {
464 b = stats.total_read -
465 initial_stats->total_read;
466 }
8950ac03 467 snprintf(buf2,sizeof(buf2),"%.0f", (double)b);
1b7c47cb
AT
468 n = buf2;
469 break;
470 case 'c':
471 if (!am_sender) {
472 b = stats.total_written -
473 initial_stats->total_written;
474 } else {
475 b = stats.total_read -
476 initial_stats->total_read;
477 }
8950ac03 478 snprintf(buf2,sizeof(buf2),"%.0f", (double)b);
1b7c47cb
AT
479 n = buf2;
480 break;
e08bfe12
AT
481 }
482
aa126974
MP
483 /* n is the string to be inserted in place of this %
484 * code; l is its length not including the trailing
485 * NUL */
486 if (!n)
487 continue;
e08bfe12
AT
488
489 l = strlen(n);
490
9dd891bb 491 if (l + ((int)(s - &buf[0])) >= sizeof(buf)) {
e08bfe12
AT
492 rprintf(FERROR,"buffer overflow expanding %%%c - exiting\n",
493 p[0]);
65417579 494 exit_cleanup(RERR_MESSAGEIO);
e08bfe12
AT
495 }
496
aa126974 497 /* Shuffle the rest of the string along to make space for n */
e08bfe12 498 if (l != 2) {
34720097 499 memmove(s+(l-1), s+1, strlen(s+1)+1);
e08bfe12 500 }
aa126974
MP
501
502 /* Copy in n but NOT its nul, because the format sting
503 * probably continues after this. */
e08bfe12
AT
504 memcpy(p, n, l);
505
aa126974 506 /* Skip over inserted string; continue looking */
e08bfe12
AT
507 s = p+l;
508 }
509
0f3203c3 510 rprintf(code,"%s\n", buf);
e08bfe12
AT
511}
512
11a5a3c7 513/* log the outgoing transfer of a file */
1b7c47cb 514void log_send(struct file_struct *file, struct stats *initial_stats)
11a5a3c7
AT
515{
516 extern int module_id;
b6062654
AT
517 extern int am_server;
518 extern char *log_format;
519
11a5a3c7 520 if (lp_transfer_logging(module_id)) {
b6062654
AT
521 log_formatted(FLOG, lp_log_format(module_id), "send", file, initial_stats);
522 } else if (log_format && !am_server) {
523 log_formatted(FINFO, log_format, "send", file, initial_stats);
11a5a3c7
AT
524 }
525}
526
527/* log the incoming transfer of a file */
1b7c47cb 528void log_recv(struct file_struct *file, struct stats *initial_stats)
11a5a3c7
AT
529{
530 extern int module_id;
b6062654
AT
531 extern int am_server;
532 extern char *log_format;
533
11a5a3c7 534 if (lp_transfer_logging(module_id)) {
117af102 535 log_formatted(FLOG, lp_log_format(module_id), "recv", file, initial_stats);
b6062654 536 } else if (log_format && !am_server) {
117af102 537 log_formatted(FINFO, log_format, "recv", file, initial_stats);
11a5a3c7
AT
538 }
539}
540
af642a61
MP
541
542
543
544/*
545 * Called when the transfer is interrupted for some reason.
546 *
547 * Code is one of the RERR_* codes from errcode.h, or terminating
548 * successfully.
549 */
a9766ef1 550void log_exit(int code, const char *file, int line)
9b73d1c0
AT
551{
552 if (code == 0) {
553 extern struct stats stats;
67ea0d48 554 rprintf(FLOG,"wrote %.0f bytes read %.0f bytes total size %.0f\n",
9b73d1c0
AT
555 (double)stats.total_written,
556 (double)stats.total_read,
557 (double)stats.total_size);
558 } else {
af642a61
MP
559 const char *name;
560
561 name = rerr_name(code);
562 if (!name)
563 name = "unexplained error";
564
ba358243 565 rprintf(FERROR,"rsync error: %s (code %d) at %s(%d)\n",
af642a61 566 name, code, file, line);
9b73d1c0
AT
567 }
568}
569
088aac85
DD
570/*
571 * Log the incoming transfer of a file for interactive use,
572 * this will be called at the end where the client was run.
573 * Called when a file starts to be transferred.
574 */
a9766ef1 575void log_transfer(struct file_struct *file, const char *fname)
11a5a3c7
AT
576{
577 extern int verbose;
578
579 if (!verbose) return;
580
088aac85 581 rprintf(FINFO, "%s\n", fname);
11a5a3c7 582}