Make batch mode actually work and add man page documentation. From Jos Backus.
[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
30static char *logfname;
31static FILE *logfile;
32static int log_error_fd = -1;
33
34int log_got_error=0;
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" },
43 { RERR_UNSUPPORTED, "requested action not supported" },
44 { RERR_STARTCLIENT, "error starting client-server protocol" },
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" },
50 { RERR_SIGNAL , "received SIGUSR1 or SIGINT" },
51 { RERR_WAITCHILD , "some error returned by waitpid()" },
52 { RERR_MALLOC , "error allocating core memory buffers" },
53 { RERR_PARTIAL , "partial transfer" },
54 { RERR_TIMEOUT , "timeout in data send/receive" },
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" },
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
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);
117 /* don't check for an error if the best way of handling the error is
118 to ignore it */
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
132
133static void logit(int priority, char *buf)
134{
135 if (logfname) {
136 if (!logfile)
137 log_open();
138 fprintf(logfile,"%s [%d] %s",
139 timestring(time(NULL)), (int)getpid(), buf);
140 fflush(logfile);
141 } else {
142 syslog(priority, "%s", buf);
143 }
144}
145
146void log_init(void)
147{
148 static int initialised;
149 int options = LOG_PID;
150 time_t t;
151
152 if (initialised) return;
153 initialised = 1;
154
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 */
162 logfname = lp_log_file();
163 if (logfname) {
164 if (*logfname) {
165 log_open();
166 return;
167 }
168 logfname = NULL;
169 }
170
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
182 logit(LOG_INFO,"rsyncd started\n");
183#endif
184}
185
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()
197{
198 if (logfile) {
199 fclose(logfile);
200 logfile = NULL;
201 }
202}
203
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;
209 set_nonblocking(log_error_fd);
210}
211
212/* this is the underlying (unformatted) rsync debugging function. Call
213 it with FINFO, FERROR or FLOG */
214void rwrite(enum logcode code, char *buf, int len)
215{
216 FILE *f=NULL;
217 extern int am_daemon;
218 extern int am_server;
219 extern int quiet;
220 /* recursion can happen with certain fatal conditions */
221
222 if (quiet && code == FINFO) return;
223
224 if (len < 0) exit_cleanup(RERR_MESSAGEIO);
225
226 buf[len] = 0;
227
228 if (code == FLOG) {
229 if (am_daemon) logit(LOG_INFO, buf);
230 return;
231 }
232
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();
237 return;
238 }
239
240 /* if that fails, try to pass it to the other end */
241 if (am_server && io_multiplex_write(code, buf, len)) {
242 return;
243 }
244
245 if (am_daemon) {
246 static int depth;
247 int priority = LOG_INFO;
248 if (code == FERROR) priority = LOG_WARNING;
249
250 if (depth) return;
251
252 depth++;
253
254 log_init();
255 logit(priority, buf);
256
257 depth--;
258 return;
259 }
260
261 if (code == FERROR) {
262 log_got_error = 1;
263 f = stderr;
264 }
265
266 if (code == FINFO) {
267 if (am_server)
268 f = stderr;
269 else
270 f = stdout;
271 }
272
273 if (!f) exit_cleanup(RERR_MESSAGEIO);
274
275 if (fwrite(buf, len, 1, f) != 1) exit_cleanup(RERR_MESSAGEIO);
276
277 if (buf[len-1] == '\r' || buf[len-1] == '\n') fflush(f);
278}
279
280
281/* This is the rsync debugging function. Call it with FINFO, FERROR or
282 * FLOG. */
283void rprintf(enum logcode code, const char *format, ...)
284{
285 va_list ap;
286 char buf[1024];
287 int len;
288
289 va_start(ap, format);
290 /* Note: might return -1 */
291 len = vsnprintf(buf, sizeof(buf), format, ap);
292 va_end(ap);
293
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. */
297 if ((size_t) len > sizeof(buf)-1 || len < 0) {
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 }
319
320 rwrite(code, buf, len);
321}
322
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];
336 int len;
337 size_t sys_len;
338 char *sysmsg;
339
340 va_start(ap, format);
341 /* Note: might return <0 */
342 len = vsnprintf(buf, sizeof(buf), format, ap);
343 va_end(ap);
344
345 if ((size_t) len > sizeof(buf)-1)
346 exit_cleanup(RERR_MESSAGEIO);
347
348 sysmsg = strerror(errcode);
349 sys_len = strlen(sysmsg);
350 if ((size_t) len + 3 + sys_len > sizeof(buf) - 1)
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
365void rflush(enum logcode code)
366{
367 FILE *f = NULL;
368 extern int am_daemon;
369
370 if (am_daemon) {
371 return;
372 }
373
374 if (code == FLOG) {
375 return;
376 }
377
378 if (code == FERROR) {
379 f = stderr;
380 }
381
382 if (code == FINFO) {
383 extern int am_server;
384 if (am_server)
385 f = stderr;
386 else
387 f = stdout;
388 }
389
390 if (!f) exit_cleanup(RERR_MESSAGEIO);
391 fflush(f);
392}
393
394
395
396/* a generic logging routine for send/recv, with parameter
397 substitiution */
398static void log_formatted(enum logcode code,
399 char *format, char *op, struct file_struct *file,
400 struct stats *initial_stats)
401{
402 extern int module_id;
403 extern char *auth_user;
404 char buf[1024];
405 char buf2[1024];
406 char *p, *s, *n;
407 size_t l;
408 extern struct stats stats;
409 extern int am_sender;
410 extern int am_daemon;
411 int64 b;
412
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);
419 strlcpy(buf, format, sizeof(buf));
420
421 for (s=&buf[0];
422 s && (p=strchr(s,'%')); ) {
423 n = NULL;
424 s = p + 1;
425
426 switch (p[1]) {
427 case 'h': if (am_daemon) n = client_name(0); break;
428 case 'a': if (am_daemon) n = client_addr(0); break;
429 case 'l':
430 snprintf(buf2,sizeof(buf2),"%.0f",
431 (double)file->length);
432 n = buf2;
433 break;
434 case 'p':
435 snprintf(buf2,sizeof(buf2),"%d",
436 (int)getpid());
437 n = buf2;
438 break;
439 case 'o': n = op; break;
440 case 'f':
441 snprintf(buf2, sizeof(buf2), "%s/%s",
442 file->basedir?file->basedir:"",
443 f_name(file));
444 clean_fname(buf2);
445 n = buf2;
446 if (*n == '/') n++;
447 break;
448 case 'm': n = lp_name(module_id); break;
449 case 't': n = timestring(time(NULL)); break;
450 case 'P': n = lp_path(module_id); break;
451 case 'u': n = auth_user; break;
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 }
460 snprintf(buf2,sizeof(buf2),"%.0f", (double)b);
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 }
471 snprintf(buf2,sizeof(buf2),"%.0f", (double)b);
472 n = buf2;
473 break;
474 }
475
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;
481
482 l = strlen(n);
483
484 if (l + ((int)(s - &buf[0])) >= sizeof(buf)) {
485 rprintf(FERROR,"buffer overflow expanding %%%c - exiting\n",
486 p[0]);
487 exit_cleanup(RERR_MESSAGEIO);
488 }
489
490 /* Shuffle the rest of the string along to make space for n */
491 if (l != 2) {
492 memmove(s+(l-1), s+1, strlen(s+1)+1);
493 }
494
495 /* Copy in n but NOT its nul, because the format sting
496 * probably continues after this. */
497 memcpy(p, n, l);
498
499 /* Skip over inserted string; continue looking */
500 s = p+l;
501 }
502
503 rprintf(code,"%s\n", buf);
504}
505
506/* log the outgoing transfer of a file */
507void log_send(struct file_struct *file, struct stats *initial_stats)
508{
509 extern int module_id;
510 extern int am_server;
511 extern char *log_format;
512
513 if (lp_transfer_logging(module_id)) {
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);
517 }
518}
519
520/* log the incoming transfer of a file */
521void log_recv(struct file_struct *file, struct stats *initial_stats)
522{
523 extern int module_id;
524 extern int am_server;
525 extern char *log_format;
526
527 if (lp_transfer_logging(module_id)) {
528 log_formatted(FLOG, lp_log_format(module_id), "recv", file, initial_stats);
529 } else if (log_format && !am_server) {
530 log_formatted(FINFO, log_format, "recv", file, initial_stats);
531 }
532}
533
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 */
543void log_exit(int code, const char *file, int line)
544{
545 if (code == 0) {
546 extern struct stats stats;
547 rprintf(FLOG,"wrote %.0f bytes read %.0f bytes total size %.0f\n",
548 (double)stats.total_written,
549 (double)stats.total_read,
550 (double)stats.total_size);
551 } else {
552 const char *name;
553
554 name = rerr_name(code);
555 if (!name)
556 name = "unexplained error";
557
558 rprintf(FERROR,"rsync error: %s (code %d) at %s(%d)\n",
559 name, code, file, line);
560 }
561}
562
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 */
568void log_transfer(struct file_struct *file, const char *fname)
569{
570 extern int verbose;
571
572 if (!verbose) return;
573
574 rprintf(FINFO, "%s\n", fname);
575}