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