Don't generate code for zlib and popt.
[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
900748fc
MP
240 /* If that fails, try to pass it to the other end.
241 *
242 * io_multiplex_write can fail if we do not have a multiplexed
243 * connection at the moment, in which case we fall through and
244 * log locally instead. */
4a814638 245 if (am_server && io_multiplex_write(code, buf, len)) {
6d7b6081
AT
246 return;
247 }
ff41a59f 248
ff8b29b8 249 if (am_daemon) {
b24203b3 250 static int depth;
ff8b29b8 251 int priority = LOG_INFO;
ff41a59f 252 if (code == FERROR) priority = LOG_WARNING;
45ccc5c0 253
b24203b3
AT
254 if (depth) return;
255
f27b53f5
AT
256 depth++;
257
45a83540 258 log_init();
6d7b6081 259 logit(priority, buf);
8d9dc9f9
AT
260
261 depth--;
ff8b29b8 262 return;
0b76cd63
AT
263 }
264
ff41a59f 265 if (code == FERROR) {
ff81e809 266 log_got_error = 1;
ff8b29b8
AT
267 f = stderr;
268 }
269
ff41a59f 270 if (code == FINFO) {
ff8b29b8
AT
271 if (am_server)
272 f = stderr;
273 else
274 f = stdout;
275 }
276
65417579 277 if (!f) exit_cleanup(RERR_MESSAGEIO);
0b76cd63 278
65417579 279 if (fwrite(buf, len, 1, f) != 1) exit_cleanup(RERR_MESSAGEIO);
8d9dc9f9 280
bcf5b133 281 if (buf[len-1] == '\r' || buf[len-1] == '\n') fflush(f);
0b76cd63 282}
554e0a8d
AT
283
284
a039749b
MP
285/* This is the rsync debugging function. Call it with FINFO, FERROR or
286 * FLOG. */
287void rprintf(enum logcode code, const char *format, ...)
554e0a8d
AT
288{
289 va_list ap;
290 char buf[1024];
291 int len;
292
293 va_start(ap, format);
59ee743c 294 /* Note: might return -1 */
8950ac03 295 len = vsnprintf(buf, sizeof(buf), format, ap);
554e0a8d
AT
296 va_end(ap);
297
b2f02464
MP
298 /* Deal with buffer overruns. Instead of panicking, just
299 * truncate the resulting string. Note that some vsnprintf()s
300 * return -1 on truncation, e.g., glibc 2.0.6 and earlier. */
59ee743c 301 if ((size_t) len > sizeof(buf)-1 || len < 0) {
b2f02464
MP
302 const char ellipsis[] = "[...]";
303
304 /* Reset length, and zero-terminate the end of our buffer */
305 len = sizeof(buf)-1;
306 buf[len] = '\0';
307
308 /* Copy the ellipsis to the end of the string, but give
309 * us one extra character:
310 *
311 * v--- null byte at buf[sizeof(buf)-1]
312 * abcdefghij0
313 * -> abcd[...]00 <-- now two null bytes at end
314 *
315 * If the input format string has a trailing newline,
316 * we copy it into that extra null; if it doesn't, well,
317 * all we lose is one byte. */
318 strncpy(buf+len-sizeof(ellipsis), ellipsis, sizeof(ellipsis));
319 if (format[strlen(format)-1] == '\n') {
320 buf[len-1] = '\n';
321 }
322 }
554e0a8d 323
ff41a59f 324 rwrite(code, buf, len);
554e0a8d 325}
0b76cd63 326
a039749b
MP
327
328/* This is like rprintf, but it also tries to print some
329 * representation of the error code. Normally errcode = errno.
330 *
331 * Unlike rprintf, this always adds a newline and there should not be
332 * one in the format string.
333 *
334 * Note that since strerror might involve dynamically loading a
335 * message catalog we need to call it once before chroot-ing. */
336void rsyserr(enum logcode code, int errcode, const char *format, ...)
337{
338 va_list ap;
339 char buf[1024];
59ee743c
MP
340 int len;
341 size_t sys_len;
a039749b
MP
342 char *sysmsg;
343
344 va_start(ap, format);
59ee743c 345 /* Note: might return <0 */
8950ac03 346 len = vsnprintf(buf, sizeof(buf), format, ap);
a039749b
MP
347 va_end(ap);
348
4f092bee
MP
349 /* TODO: Put in RSYNC_NAME at the start. */
350
59ee743c
MP
351 if ((size_t) len > sizeof(buf)-1)
352 exit_cleanup(RERR_MESSAGEIO);
a039749b
MP
353
354 sysmsg = strerror(errcode);
355 sys_len = strlen(sysmsg);
59ee743c 356 if ((size_t) len + 3 + sys_len > sizeof(buf) - 1)
a039749b
MP
357 exit_cleanup(RERR_MESSAGEIO);
358
359 strcpy(buf + len, ": ");
360 len += 2;
361 strcpy(buf + len, sysmsg);
362 len += sys_len;
363 strcpy(buf + len, "\n");
364 len++;
365
366 rwrite(code, buf, len);
367}
368
369
370
ff41a59f 371void rflush(enum logcode code)
0b76cd63
AT
372{
373 FILE *f = NULL;
374 extern int am_daemon;
375
376 if (am_daemon) {
377 return;
378 }
379
ff41a59f 380 if (code == FLOG) {
e08bfe12
AT
381 return;
382 }
383
ff41a59f 384 if (code == FERROR) {
0b76cd63
AT
385 f = stderr;
386 }
387
ff41a59f 388 if (code == FINFO) {
0b76cd63
AT
389 extern int am_server;
390 if (am_server)
391 f = stderr;
392 else
393 f = stdout;
394 }
395
65417579 396 if (!f) exit_cleanup(RERR_MESSAGEIO);
0b76cd63
AT
397 fflush(f);
398}
399
11a5a3c7 400
e08bfe12
AT
401
402/* a generic logging routine for send/recv, with parameter
403 substitiution */
0f3203c3 404static void log_formatted(enum logcode code,
b6062654 405 char *format, char *op, struct file_struct *file,
1b7c47cb 406 struct stats *initial_stats)
e08bfe12
AT
407{
408 extern int module_id;
97cb8dc2 409 extern char *auth_user;
e08bfe12 410 char buf[1024];
ab7104da 411 char buf2[1024];
e08bfe12 412 char *p, *s, *n;
59ee743c 413 size_t l;
1b7c47cb
AT
414 extern struct stats stats;
415 extern int am_sender;
af77cc6b 416 extern int am_daemon;
1b7c47cb 417 int64 b;
e08bfe12 418
aa126974
MP
419 /* We expand % codes one by one in place in buf. We don't
420 * copy in the terminating nul of the inserted strings, but
421 * rather keep going until we reach the nul of the format.
422 * Just to make sure we don't clobber that nul and therefore
423 * accidentally keep going, we zero the buffer now. */
424 memset(buf, 0, sizeof buf);
37f9805d 425 strlcpy(buf, format, sizeof(buf));
e08bfe12
AT
426
427 for (s=&buf[0];
428 s && (p=strchr(s,'%')); ) {
429 n = NULL;
430 s = p + 1;
431
432 switch (p[1]) {
af77cc6b
AT
433 case 'h': if (am_daemon) n = client_name(0); break;
434 case 'a': if (am_daemon) n = client_addr(0); break;
e08bfe12 435 case 'l':
8950ac03 436 snprintf(buf2,sizeof(buf2),"%.0f",
e08bfe12
AT
437 (double)file->length);
438 n = buf2;
439 break;
440 case 'p':
8950ac03 441 snprintf(buf2,sizeof(buf2),"%d",
e08bfe12
AT
442 (int)getpid());
443 n = buf2;
444 break;
445 case 'o': n = op; break;
ab7104da 446 case 'f':
8950ac03 447 snprintf(buf2, sizeof(buf2), "%s/%s",
ab7104da
AT
448 file->basedir?file->basedir:"",
449 f_name(file));
450 clean_fname(buf2);
451 n = buf2;
b6062654 452 if (*n == '/') n++;
ab7104da 453 break;
97cb8dc2 454 case 'm': n = lp_name(module_id); break;
b6062654 455 case 't': n = timestring(time(NULL)); break;
97cb8dc2
AT
456 case 'P': n = lp_path(module_id); break;
457 case 'u': n = auth_user; break;
1b7c47cb
AT
458 case 'b':
459 if (am_sender) {
460 b = stats.total_written -
461 initial_stats->total_written;
462 } else {
463 b = stats.total_read -
464 initial_stats->total_read;
465 }
8950ac03 466 snprintf(buf2,sizeof(buf2),"%.0f", (double)b);
1b7c47cb
AT
467 n = buf2;
468 break;
469 case 'c':
470 if (!am_sender) {
471 b = stats.total_written -
472 initial_stats->total_written;
473 } else {
474 b = stats.total_read -
475 initial_stats->total_read;
476 }
8950ac03 477 snprintf(buf2,sizeof(buf2),"%.0f", (double)b);
1b7c47cb
AT
478 n = buf2;
479 break;
e08bfe12
AT
480 }
481
aa126974
MP
482 /* n is the string to be inserted in place of this %
483 * code; l is its length not including the trailing
484 * NUL */
485 if (!n)
486 continue;
e08bfe12
AT
487
488 l = strlen(n);
489
9dd891bb 490 if (l + ((int)(s - &buf[0])) >= sizeof(buf)) {
e08bfe12
AT
491 rprintf(FERROR,"buffer overflow expanding %%%c - exiting\n",
492 p[0]);
65417579 493 exit_cleanup(RERR_MESSAGEIO);
e08bfe12
AT
494 }
495
aa126974 496 /* Shuffle the rest of the string along to make space for n */
e08bfe12 497 if (l != 2) {
34720097 498 memmove(s+(l-1), s+1, strlen(s+1)+1);
e08bfe12 499 }
aa126974
MP
500
501 /* Copy in n but NOT its nul, because the format sting
502 * probably continues after this. */
e08bfe12
AT
503 memcpy(p, n, l);
504
aa126974 505 /* Skip over inserted string; continue looking */
e08bfe12
AT
506 s = p+l;
507 }
508
0f3203c3 509 rprintf(code,"%s\n", buf);
e08bfe12
AT
510}
511
11a5a3c7 512/* log the outgoing transfer of a file */
1b7c47cb 513void log_send(struct file_struct *file, struct stats *initial_stats)
11a5a3c7
AT
514{
515 extern int module_id;
b6062654
AT
516 extern int am_server;
517 extern char *log_format;
518
11a5a3c7 519 if (lp_transfer_logging(module_id)) {
b6062654
AT
520 log_formatted(FLOG, lp_log_format(module_id), "send", file, initial_stats);
521 } else if (log_format && !am_server) {
522 log_formatted(FINFO, log_format, "send", file, initial_stats);
11a5a3c7
AT
523 }
524}
525
526/* log the incoming transfer of a file */
1b7c47cb 527void log_recv(struct file_struct *file, struct stats *initial_stats)
11a5a3c7
AT
528{
529 extern int module_id;
b6062654
AT
530 extern int am_server;
531 extern char *log_format;
532
11a5a3c7 533 if (lp_transfer_logging(module_id)) {
117af102 534 log_formatted(FLOG, lp_log_format(module_id), "recv", file, initial_stats);
b6062654 535 } else if (log_format && !am_server) {
117af102 536 log_formatted(FINFO, log_format, "recv", file, initial_stats);
11a5a3c7
AT
537 }
538}
539
af642a61
MP
540
541
542
543/*
544 * Called when the transfer is interrupted for some reason.
545 *
546 * Code is one of the RERR_* codes from errcode.h, or terminating
547 * successfully.
548 */
a9766ef1 549void log_exit(int code, const char *file, int line)
9b73d1c0
AT
550{
551 if (code == 0) {
552 extern struct stats stats;
67ea0d48 553 rprintf(FLOG,"wrote %.0f bytes read %.0f bytes total size %.0f\n",
9b73d1c0
AT
554 (double)stats.total_written,
555 (double)stats.total_read,
556 (double)stats.total_size);
557 } else {
af642a61
MP
558 const char *name;
559
560 name = rerr_name(code);
561 if (!name)
562 name = "unexplained error";
563
ba358243 564 rprintf(FERROR,"rsync error: %s (code %d) at %s(%d)\n",
af642a61 565 name, code, file, line);
9b73d1c0
AT
566 }
567}
568
088aac85
DD
569/*
570 * Log the incoming transfer of a file for interactive use,
571 * this will be called at the end where the client was run.
572 * Called when a file starts to be transferred.
573 */
a9766ef1 574void log_transfer(struct file_struct *file, const char *fname)
11a5a3c7
AT
575{
576 extern int verbose;
577
578 if (!verbose) return;
579
088aac85 580 rprintf(FINFO, "%s\n", fname);
11a5a3c7 581}