Give the user an idea of how far along in the transfer we are
[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
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
38static int log_initialised;
39static char *logfname;
40static FILE *logfile;
41static int log_error_fd = -1;
42struct stats stats;
43
44int log_got_error=0;
45
46struct {
47 int code;
48 char const *name;
49} const rerr_names[] = {
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" },
64 { RERR_VANISHED , "some files vanished before they could be transfered" },
65 { RERR_TIMEOUT , "timeout in data send/receive" },
66 { RERR_CMD_FAILED , "remote shell failed" },
67 { RERR_CMD_KILLED , "remote shell killed" },
68 { RERR_CMD_RUN, "remote command could not be run" },
69 { RERR_CMD_NOTFOUND, "remote command not found" },
70 { 0, NULL }
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{
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;
86}
87
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;
102 el = new(struct err_list);
103 if (!el) exit_cleanup(RERR_MALLOC);
104 el->next = NULL;
105 el->buf = new_array(char, len+4);
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);
128 /* don't check for an error if the best way of handling the error is
129 * to ignore it */
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
143
144static void logit(int priority, char *buf)
145{
146 if (logfname) {
147 if (!logfile)
148 log_open();
149 fprintf(logfile,"%s [%d] %s",
150 timestring(time(NULL)), (int)getpid(), buf);
151 fflush(logfile);
152 } else {
153 syslog(priority, "%s", buf);
154 }
155}
156
157void log_init(void)
158{
159 int options = LOG_PID;
160 time_t t;
161
162 if (log_initialised) return;
163 log_initialised = 1;
164
165 /* this looks pointless, but it is needed in order for the
166 * C library on some systems to fetch the timezone info
167 * before the chroot */
168 t = time(NULL);
169 localtime(&t);
170
171 /* optionally use a log file instead of syslog */
172 logfname = lp_log_file();
173 if (logfname) {
174 if (*logfname) {
175 log_open();
176 return;
177 }
178 logfname = NULL;
179 }
180
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
192 logit(LOG_INFO,"rsyncd started\n");
193#endif
194}
195
196void log_open(void)
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
206void log_close(void)
207{
208 if (logfile) {
209 fclose(logfile);
210 logfile = NULL;
211 }
212}
213
214/* setup the error file descriptor - used when we are a server
215 * that is receiving files */
216void set_error_fd(int fd)
217{
218 log_error_fd = fd;
219 set_nonblocking(log_error_fd);
220}
221
222/* this is the underlying (unformatted) rsync debugging function. Call
223 * it with FINFO, FERROR or FLOG */
224void rwrite(enum logcode code, char *buf, int len)
225{
226 FILE *f=NULL;
227 /* recursion can happen with certain fatal conditions */
228
229 if (quiet && code == FINFO) return;
230
231 if (len < 0) exit_cleanup(RERR_MESSAGEIO);
232
233 buf[len] = 0;
234
235 if (code == FLOG) {
236 if (am_daemon) logit(LOG_INFO, buf);
237 return;
238 }
239
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();
244 return;
245 }
246
247 /* next, if we are a server and multiplexing is enabled,
248 * pass it to the other side. */
249 if (am_server && io_multiplex_write(code, buf, len)) {
250 return;
251 }
252
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
259 * fall through to the next case.
260 * Note that this is only for the time before multiplexing is enabled.
261 */
262 if (am_daemon && (!am_server || log_initialised)) {
263 static int depth;
264 int priority = LOG_INFO;
265 if (code == FERROR) priority = LOG_WARNING;
266
267 if (depth) return;
268
269 depth++;
270
271 log_init();
272 logit(priority, buf);
273
274 depth--;
275 return;
276 }
277
278 if (code == FERROR) {
279 log_got_error = 1;
280 f = stderr;
281 }
282
283 if (code == FINFO) {
284 if (am_server)
285 f = stderr;
286 else
287 f = stdout;
288 }
289
290 if (!f) exit_cleanup(RERR_MESSAGEIO);
291
292 if (fwrite(buf, len, 1, f) != 1) exit_cleanup(RERR_MESSAGEIO);
293
294 if (buf[len-1] == '\r' || buf[len-1] == '\n') fflush(f);
295}
296
297
298/* This is the rsync debugging function. Call it with FINFO, FERROR or
299 * FLOG. */
300void rprintf(enum logcode code, const char *format, ...)
301{
302 va_list ap;
303 char buf[1024];
304 int len;
305
306 va_start(ap, format);
307 /* Note: might return -1 */
308 len = vsnprintf(buf, sizeof(buf), format, ap);
309 va_end(ap);
310
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. */
314 if ((size_t) len > sizeof(buf)-1 || len < 0) {
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 }
336
337 rwrite(code, buf, len);
338}
339
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{
351 va_list ap;
352 char buf[1024];
353 int len;
354 size_t sys_len;
355 char *sysmsg;
356
357 va_start(ap, format);
358 /* Note: might return <0 */
359 len = vsnprintf(buf, sizeof(buf), format, ap);
360 va_end(ap);
361
362 /* TODO: Put in RSYNC_NAME at the start. */
363
364 if ((size_t) len > sizeof(buf)-1)
365 exit_cleanup(RERR_MESSAGEIO);
366
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);
371
372 strcpy(buf + len, ": ");
373 len += 2;
374 strcpy(buf + len, sysmsg);
375 len += sys_len;
376 strcpy(buf + len, "\n");
377 len++;
378
379 rwrite(code, buf, len);
380}
381
382
383
384void rflush(enum logcode code)
385{
386 FILE *f = NULL;
387
388 if (am_daemon) {
389 return;
390 }
391
392 if (code == FLOG) {
393 return;
394 }
395
396 if (code == FERROR) {
397 f = stderr;
398 }
399
400 if (code == FINFO) {
401 if (am_server)
402 f = stderr;
403 else
404 f = stdout;
405 }
406
407 if (!f) exit_cleanup(RERR_MESSAGEIO);
408 fflush(f);
409}
410
411
412
413/* a generic logging routine for send/recv, with parameter
414 * substitiution */
415static void log_formatted(enum logcode code,
416 char *format, char *op, struct file_struct *file,
417 struct stats *initial_stats)
418{
419 char buf[1024];
420 char buf2[1024];
421 char *p, *s, *n;
422 size_t l;
423 int64 b;
424
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);
431 strlcpy(buf, format, sizeof(buf));
432
433 for (s = &buf[0]; s && (p = strchr(s,'%')); ) {
434 n = NULL;
435 s = p + 1;
436
437 switch (p[1]) {
438 case 'h': if (am_daemon) n = client_name(0); break;
439 case 'a': if (am_daemon) n = client_addr(0); break;
440 case 'l':
441 snprintf(buf2,sizeof(buf2),"%.0f",
442 (double)file->length);
443 n = buf2;
444 break;
445 case 'p':
446 snprintf(buf2,sizeof(buf2),"%d",
447 (int)getpid());
448 n = buf2;
449 break;
450 case 'o': n = op; break;
451 case 'f':
452 snprintf(buf2, sizeof(buf2), "%s/%s",
453 file->basedir?file->basedir:"",
454 f_name(file));
455 clean_fname(buf2);
456 n = buf2;
457 if (*n == '/') n++;
458 break;
459 case 'm': n = lp_name(module_id); break;
460 case 't': n = timestring(time(NULL)); break;
461 case 'P': n = lp_path(module_id); break;
462 case 'u': n = auth_user; break;
463 case 'b':
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 case 'c':
475 if (!am_sender) {
476 b = stats.total_written -
477 initial_stats->total_written;
478 } else {
479 b = stats.total_read -
480 initial_stats->total_read;
481 }
482 snprintf(buf2,sizeof(buf2),"%.0f", (double)b);
483 n = buf2;
484 break;
485 }
486
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;
492
493 l = strlen(n);
494
495 if (l + ((int)(s - &buf[0])) >= sizeof(buf)) {
496 rprintf(FERROR,"buffer overflow expanding %%%c - exiting\n",
497 p[0]);
498 exit_cleanup(RERR_MESSAGEIO);
499 }
500
501 /* Shuffle the rest of the string along to make space for n */
502 if (l != 2) {
503 memmove(s+(l-1), s+1, strlen(s+1)+1);
504 }
505
506 /* Copy in n but NOT its nul, because the format sting
507 * probably continues after this. */
508 memcpy(p, n, l);
509
510 /* Skip over inserted string; continue looking */
511 s = p+l;
512 }
513
514 rprintf(code,"%s\n", buf);
515}
516
517/* log the outgoing transfer of a file */
518void log_send(struct file_struct *file, struct stats *initial_stats)
519{
520 if (lp_transfer_logging(module_id)) {
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);
524 }
525}
526
527/* log the incoming transfer of a file */
528void log_recv(struct file_struct *file, struct stats *initial_stats)
529{
530 if (lp_transfer_logging(module_id)) {
531 log_formatted(FLOG, lp_log_format(module_id), "recv", file, initial_stats);
532 } else if (log_format && !am_server) {
533 log_formatted(FINFO, log_format, "recv", file, initial_stats);
534 }
535}
536
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 */
546void log_exit(int code, const char *file, int line)
547{
548 if (code == 0) {
549 rprintf(FLOG,"wrote %.0f bytes read %.0f bytes total size %.0f\n",
550 (double)stats.total_written,
551 (double)stats.total_read,
552 (double)stats.total_size);
553 } else {
554 const char *name;
555
556 name = rerr_name(code);
557 if (!name)
558 name = "unexplained error";
559
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 }
568 }
569}