* Don't call getnameinfo() if we've already populated the addr_buf.
[rsync.git] / log.c
blobab8644ed0b2130346c75502ad71fffcf0359b3de
1 /* -*- c-file-style: "linux"; -*-
3 Copyright (C) 1998-2001 by Andrew Tridgell <tridge@samba.org>
4 Copyright (C) 2000-2001 by Martin Pool <mbp@samba.org>
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.
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.
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.
22 Logging and utility functions.
23 tridge, May 1998
25 Mapping to human-readable messages added by Martin Pool
26 <mbp@samba.org>, Oct 2000.
28 #include "rsync.h"
30 static char *logfname;
31 static FILE *logfile;
32 static int log_error_fd = -1;
33 struct stats stats;
35 int log_got_error=0;
37 struct {
38 int code;
39 char const *name;
40 } const rerr_names[] = {
41 { RERR_SYNTAX , "syntax or usage error" },
42 { RERR_PROTOCOL , "protocol incompatibility" },
43 { RERR_FILESELECT , "errors selecting input/output files, dirs" },
44 { RERR_UNSUPPORTED, "requested action not supported" },
45 { RERR_STARTCLIENT, "error starting client-server protocol" },
46 { RERR_SOCKETIO , "error in socket IO" },
47 { RERR_FILEIO , "error in file IO" },
48 { RERR_STREAMIO , "error in rsync protocol data stream" },
49 { RERR_MESSAGEIO , "errors with program diagnostics" },
50 { RERR_IPC , "error in IPC code" },
51 { RERR_SIGNAL , "received SIGUSR1 or SIGINT" },
52 { RERR_WAITCHILD , "some error returned by waitpid()" },
53 { RERR_MALLOC , "error allocating core memory buffers" },
54 { RERR_PARTIAL , "some files could not be transferred" },
55 { RERR_TIMEOUT , "timeout in data send/receive" },
56 { RERR_CMD_FAILED , "remote shell failed" },
57 { RERR_CMD_KILLED , "remote shell killed" },
58 { RERR_CMD_RUN, "remote command could not be run" },
59 { RERR_CMD_NOTFOUND, "remote command not found" },
60 { 0, NULL }
66 * Map from rsync error code to name, or return NULL.
68 static char const *rerr_name(int code)
70 int i;
71 for (i = 0; rerr_names[i].name; i++) {
72 if (rerr_names[i].code == code)
73 return rerr_names[i].name;
75 return NULL;
78 struct err_list {
79 struct err_list *next;
80 char *buf;
81 int len;
82 int written; /* how many bytes we have written so far */
85 static struct err_list *err_list_head;
86 static struct err_list *err_list_tail;
88 /* add an error message to the pending error list */
89 static void err_list_add(int code, char *buf, int len)
91 struct err_list *el;
92 el = (struct err_list *)malloc(sizeof(*el));
93 if (!el) exit_cleanup(RERR_MALLOC);
94 el->next = NULL;
95 el->buf = malloc(len+4);
96 if (!el->buf) exit_cleanup(RERR_MALLOC);
97 memcpy(el->buf+4, buf, len);
98 SIVAL(el->buf, 0, ((code+MPLEX_BASE)<<24) | len);
99 el->len = len+4;
100 el->written = 0;
101 if (err_list_tail) {
102 err_list_tail->next = el;
103 } else {
104 err_list_head = el;
106 err_list_tail = el;
110 /* try to push errors off the error list onto the wire */
111 void err_list_push(void)
113 if (log_error_fd == -1) return;
115 while (err_list_head) {
116 struct err_list *el = err_list_head;
117 int n = write(log_error_fd, el->buf+el->written, el->len - el->written);
118 /* don't check for an error if the best way of handling the error is
119 to ignore it */
120 if (n == -1) break;
121 if (n > 0) {
122 el->written += n;
124 if (el->written == el->len) {
125 free(el->buf);
126 err_list_head = el->next;
127 if (!err_list_head) err_list_tail = NULL;
128 free(el);
134 static void logit(int priority, char *buf)
136 if (logfname) {
137 if (!logfile)
138 log_open();
139 fprintf(logfile,"%s [%d] %s",
140 timestring(time(NULL)), (int)getpid(), buf);
141 fflush(logfile);
142 } else {
143 syslog(priority, "%s", buf);
147 void log_init(void)
149 static int initialised;
150 int options = LOG_PID;
151 time_t t;
153 if (initialised) return;
154 initialised = 1;
156 /* this looks pointless, but it is needed in order for the
157 C library on some systems to fetch the timezone info
158 before the chroot */
159 t = time(NULL);
160 localtime(&t);
162 /* optionally use a log file instead of syslog */
163 logfname = lp_log_file();
164 if (logfname) {
165 if (*logfname) {
166 log_open();
167 return;
169 logfname = NULL;
172 #ifdef LOG_NDELAY
173 options |= LOG_NDELAY;
174 #endif
176 #ifdef LOG_DAEMON
177 openlog("rsyncd", options, lp_syslog_facility());
178 #else
179 openlog("rsyncd", options);
180 #endif
182 #ifndef LOG_NDELAY
183 logit(LOG_INFO,"rsyncd started\n");
184 #endif
187 void log_open()
189 if (logfname && !logfile) {
190 extern int orig_umask;
191 int old_umask = umask(022 | orig_umask);
192 logfile = fopen(logfname, "a");
193 umask(old_umask);
197 void log_close()
199 if (logfile) {
200 fclose(logfile);
201 logfile = NULL;
205 /* setup the error file descriptor - used when we are a server
206 that is receiving files */
207 void set_error_fd(int fd)
209 log_error_fd = fd;
210 set_nonblocking(log_error_fd);
213 /* this is the underlying (unformatted) rsync debugging function. Call
214 it with FINFO, FERROR or FLOG */
215 void rwrite(enum logcode code, char *buf, int len)
217 FILE *f=NULL;
218 extern int am_daemon;
219 extern int am_server;
220 extern int quiet;
221 /* recursion can happen with certain fatal conditions */
223 if (quiet && code == FINFO) return;
225 if (len < 0) exit_cleanup(RERR_MESSAGEIO);
227 buf[len] = 0;
229 if (code == FLOG) {
230 if (am_daemon) logit(LOG_INFO, buf);
231 return;
234 /* first try to pass it off to our sibling */
235 if (am_server && log_error_fd != -1) {
236 err_list_add(code, buf, len);
237 err_list_push();
238 return;
241 /* If that fails, try to pass it to the other end.
243 * io_multiplex_write can fail if we do not have a multiplexed
244 * connection at the moment, in which case we fall through and
245 * log locally instead. */
246 if (am_server && io_multiplex_write(code, buf, len)) {
247 return;
250 if (am_daemon) {
251 static int depth;
252 int priority = LOG_INFO;
253 if (code == FERROR) priority = LOG_WARNING;
255 if (depth) return;
257 depth++;
259 log_init();
260 logit(priority, buf);
262 depth--;
263 return;
266 if (code == FERROR) {
267 log_got_error = 1;
268 f = stderr;
271 if (code == FINFO) {
272 if (am_server)
273 f = stderr;
274 else
275 f = stdout;
278 if (!f) exit_cleanup(RERR_MESSAGEIO);
280 if (fwrite(buf, len, 1, f) != 1) exit_cleanup(RERR_MESSAGEIO);
282 if (buf[len-1] == '\r' || buf[len-1] == '\n') fflush(f);
286 /* This is the rsync debugging function. Call it with FINFO, FERROR or
287 * FLOG. */
288 void rprintf(enum logcode code, const char *format, ...)
290 va_list ap;
291 char buf[1024];
292 int len;
294 va_start(ap, format);
295 /* Note: might return -1 */
296 len = vsnprintf(buf, sizeof(buf), format, ap);
297 va_end(ap);
299 /* Deal with buffer overruns. Instead of panicking, just
300 * truncate the resulting string. Note that some vsnprintf()s
301 * return -1 on truncation, e.g., glibc 2.0.6 and earlier. */
302 if ((size_t) len > sizeof(buf)-1 || len < 0) {
303 const char ellipsis[] = "[...]";
305 /* Reset length, and zero-terminate the end of our buffer */
306 len = sizeof(buf)-1;
307 buf[len] = '\0';
309 /* Copy the ellipsis to the end of the string, but give
310 * us one extra character:
312 * v--- null byte at buf[sizeof(buf)-1]
313 * abcdefghij0
314 * -> abcd[...]00 <-- now two null bytes at end
316 * If the input format string has a trailing newline,
317 * we copy it into that extra null; if it doesn't, well,
318 * all we lose is one byte. */
319 strncpy(buf+len-sizeof(ellipsis), ellipsis, sizeof(ellipsis));
320 if (format[strlen(format)-1] == '\n') {
321 buf[len-1] = '\n';
325 rwrite(code, buf, len);
329 /* This is like rprintf, but it also tries to print some
330 * representation of the error code. Normally errcode = errno.
332 * Unlike rprintf, this always adds a newline and there should not be
333 * one in the format string.
335 * Note that since strerror might involve dynamically loading a
336 * message catalog we need to call it once before chroot-ing. */
337 void rsyserr(enum logcode code, int errcode, const char *format, ...)
339 va_list ap;
340 char buf[1024];
341 int len;
342 size_t sys_len;
343 char *sysmsg;
345 va_start(ap, format);
346 /* Note: might return <0 */
347 len = vsnprintf(buf, sizeof(buf), format, ap);
348 va_end(ap);
350 /* TODO: Put in RSYNC_NAME at the start. */
352 if ((size_t) len > sizeof(buf)-1)
353 exit_cleanup(RERR_MESSAGEIO);
355 sysmsg = strerror(errcode);
356 sys_len = strlen(sysmsg);
357 if ((size_t) len + 3 + sys_len > sizeof(buf) - 1)
358 exit_cleanup(RERR_MESSAGEIO);
360 strcpy(buf + len, ": ");
361 len += 2;
362 strcpy(buf + len, sysmsg);
363 len += sys_len;
364 strcpy(buf + len, "\n");
365 len++;
367 rwrite(code, buf, len);
372 void rflush(enum logcode code)
374 FILE *f = NULL;
375 extern int am_daemon;
377 if (am_daemon) {
378 return;
381 if (code == FLOG) {
382 return;
385 if (code == FERROR) {
386 f = stderr;
389 if (code == FINFO) {
390 extern int am_server;
391 if (am_server)
392 f = stderr;
393 else
394 f = stdout;
397 if (!f) exit_cleanup(RERR_MESSAGEIO);
398 fflush(f);
403 /* a generic logging routine for send/recv, with parameter
404 substitiution */
405 static void log_formatted(enum logcode code,
406 char *format, char *op, struct file_struct *file,
407 struct stats *initial_stats)
409 extern int module_id;
410 extern char *auth_user;
411 char buf[1024];
412 char buf2[1024];
413 char *p, *s, *n;
414 size_t l;
415 extern struct stats stats;
416 extern int am_sender;
417 extern int am_daemon;
418 int64 b;
420 /* We expand % codes one by one in place in buf. We don't
421 * copy in the terminating nul of the inserted strings, but
422 * rather keep going until we reach the nul of the format.
423 * Just to make sure we don't clobber that nul and therefore
424 * accidentally keep going, we zero the buffer now. */
425 memset(buf, 0, sizeof buf);
426 strlcpy(buf, format, sizeof(buf));
428 for (s=&buf[0];
429 s && (p=strchr(s,'%')); ) {
430 n = NULL;
431 s = p + 1;
433 switch (p[1]) {
434 case 'h': if (am_daemon) n = client_name(0); break;
435 case 'a': if (am_daemon) n = client_addr(0); break;
436 case 'l':
437 snprintf(buf2,sizeof(buf2),"%.0f",
438 (double)file->length);
439 n = buf2;
440 break;
441 case 'p':
442 snprintf(buf2,sizeof(buf2),"%d",
443 (int)getpid());
444 n = buf2;
445 break;
446 case 'o': n = op; break;
447 case 'f':
448 snprintf(buf2, sizeof(buf2), "%s/%s",
449 file->basedir?file->basedir:"",
450 f_name(file));
451 clean_fname(buf2);
452 n = buf2;
453 if (*n == '/') n++;
454 break;
455 case 'm': n = lp_name(module_id); break;
456 case 't': n = timestring(time(NULL)); break;
457 case 'P': n = lp_path(module_id); break;
458 case 'u': n = auth_user; break;
459 case 'b':
460 if (am_sender) {
461 b = stats.total_written -
462 initial_stats->total_written;
463 } else {
464 b = stats.total_read -
465 initial_stats->total_read;
467 snprintf(buf2,sizeof(buf2),"%.0f", (double)b);
468 n = buf2;
469 break;
470 case 'c':
471 if (!am_sender) {
472 b = stats.total_written -
473 initial_stats->total_written;
474 } else {
475 b = stats.total_read -
476 initial_stats->total_read;
478 snprintf(buf2,sizeof(buf2),"%.0f", (double)b);
479 n = buf2;
480 break;
483 /* n is the string to be inserted in place of this %
484 * code; l is its length not including the trailing
485 * NUL */
486 if (!n)
487 continue;
489 l = strlen(n);
491 if (l + ((int)(s - &buf[0])) >= sizeof(buf)) {
492 rprintf(FERROR,"buffer overflow expanding %%%c - exiting\n",
493 p[0]);
494 exit_cleanup(RERR_MESSAGEIO);
497 /* Shuffle the rest of the string along to make space for n */
498 if (l != 2) {
499 memmove(s+(l-1), s+1, strlen(s+1)+1);
502 /* Copy in n but NOT its nul, because the format sting
503 * probably continues after this. */
504 memcpy(p, n, l);
506 /* Skip over inserted string; continue looking */
507 s = p+l;
510 rprintf(code,"%s\n", buf);
513 /* log the outgoing transfer of a file */
514 void log_send(struct file_struct *file, struct stats *initial_stats)
516 extern int module_id;
517 extern int am_server;
518 extern char *log_format;
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);
527 /* log the incoming transfer of a file */
528 void log_recv(struct file_struct *file, struct stats *initial_stats)
530 extern int module_id;
531 extern int am_server;
532 extern char *log_format;
534 if (lp_transfer_logging(module_id)) {
535 log_formatted(FLOG, lp_log_format(module_id), "recv", file, initial_stats);
536 } else if (log_format && !am_server) {
537 log_formatted(FINFO, log_format, "recv", file, initial_stats);
545 * Called when the transfer is interrupted for some reason.
547 * Code is one of the RERR_* codes from errcode.h, or terminating
548 * successfully.
550 void log_exit(int code, const char *file, int line)
552 if (code == 0) {
553 extern struct stats stats;
554 rprintf(FLOG,"wrote %.0f bytes read %.0f bytes total size %.0f\n",
555 (double)stats.total_written,
556 (double)stats.total_read,
557 (double)stats.total_size);
558 } else {
559 const char *name;
561 name = rerr_name(code);
562 if (!name)
563 name = "unexplained error";
565 rprintf(FERROR,"rsync error: %s (code %d) at %s(%d)\n",
566 name, code, file, line);
571 * Log the incoming transfer of a file for interactive use,
572 * this will be called at the end where the client was run.
573 * Called when a file starts to be transferred.
575 void log_transfer(struct file_struct *file, const char *fname)
577 extern int verbose;
579 if (!verbose) return;
581 rprintf(FINFO, "%s\n", fname);