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