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