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