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