Mention two more things.
[rsync/rsync.git] / log.c
CommitLineData
a039749b 1/* -*- c-file-style: "linux"; -*-
4a7319be 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>
4a7319be 5
0b76cd63
AT
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.
4a7319be 10
0b76cd63
AT
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.
4a7319be 15
0b76cd63
AT
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
088aff1a 30extern int dry_run;
548abf96
WD
31extern int am_daemon;
32extern int am_server;
33extern int am_sender;
34extern int quiet;
35extern int module_id;
a644fc3c 36extern int msg_fd_out;
19bc826d 37extern int protocol_version;
ef74f5d6 38extern int preserve_times;
d6707171 39extern int log_format_has_o_or_i;
1eec003a 40extern int daemon_log_format_has_o_or_i;
548abf96
WD
41extern char *auth_user;
42extern char *log_format;
43
30e8c8e1 44static int log_initialised;
45a83540 45static char *logfname;
4f6325c3 46static FILE *logfile;
b35d0d8e 47struct stats stats;
e0414f42 48
8fcdc444 49int log_got_error = 0;
af642a61
MP
50
51struct {
52 int code;
53 char const *name;
54} const rerr_names[] = {
4a7319be
WD
55 { RERR_SYNTAX , "syntax or usage error" },
56 { RERR_PROTOCOL , "protocol incompatibility" },
57 { RERR_FILESELECT , "errors selecting input/output files, dirs" },
58 { RERR_UNSUPPORTED, "requested action not supported" },
59 { RERR_STARTCLIENT, "error starting client-server protocol" },
60 { RERR_SOCKETIO , "error in socket IO" },
61 { RERR_FILEIO , "error in file IO" },
62 { RERR_STREAMIO , "error in rsync protocol data stream" },
63 { RERR_MESSAGEIO , "errors with program diagnostics" },
64 { RERR_IPC , "error in IPC code" },
65 { RERR_SIGNAL , "received SIGUSR1 or SIGINT" },
66 { RERR_WAITCHILD , "some error returned by waitpid()" },
67 { RERR_MALLOC , "error allocating core memory buffers" },
68 { RERR_PARTIAL , "some files could not be transferred" },
584ba4eb 69 { RERR_VANISHED , "some files vanished before they could be transferred" },
4a7319be 70 { RERR_TIMEOUT , "timeout in data send/receive" },
19b27a48
AT
71 { RERR_CMD_FAILED , "remote shell failed" },
72 { RERR_CMD_KILLED , "remote shell killed" },
73 { RERR_CMD_RUN, "remote command could not be run" },
4a7319be
WD
74 { RERR_CMD_NOTFOUND, "remote command not found" },
75 { 0, NULL }
af642a61
MP
76};
77
78
79
80/*
81 * Map from rsync error code to name, or return NULL.
82 */
83static char const *rerr_name(int code)
84{
4a7319be
WD
85 int i;
86 for (i = 0; rerr_names[i].name; i++) {
87 if (rerr_names[i].code == code)
88 return rerr_names[i].name;
89 }
90 return NULL;
af642a61
MP
91}
92
2267efea 93void log_open(void)
63ecee4d
WD
94{
95 if (logfname && !logfile) {
96 extern int orig_umask;
97 int old_umask = umask(022 | orig_umask);
98 logfile = fopen(logfname, "a");
99 umask(old_umask);
100 if (!logfile) {
101 am_daemon = 0; /* avoid trying to log again */
102 rsyserr(FERROR, errno, "fopen() of log-file failed");
103 exit_cleanup(RERR_FILESELECT);
104 }
105 }
106}
af642a61 107
2267efea
WD
108void log_close(void)
109{
110 if (logfile) {
111 fclose(logfile);
112 logfile = NULL;
113 }
114}
115
4f6325c3
AT
116static void logit(int priority, char *buf)
117{
45a83540 118 if (logfname) {
15b84e14
DD
119 if (!logfile)
120 log_open();
4a7319be 121 fprintf(logfile,"%s [%d] %s",
f7632fc6 122 timestring(time(NULL)), (int)getpid(), buf);
4f6325c3
AT
123 fflush(logfile);
124 } else {
125 syslog(priority, "%s", buf);
126 }
127}
e42c9458 128
45a83540 129void log_init(void)
1a016bfd 130{
1a016bfd 131 int options = LOG_PID;
bcf5b133 132 time_t t;
1a016bfd 133
0bb4d176
WD
134 if (log_initialised)
135 return;
30e8c8e1 136 log_initialised = 1;
1a016bfd 137
958f3735 138 /* this looks pointless, but it is needed in order for the
4a7319be
WD
139 * C library on some systems to fetch the timezone info
140 * before the chroot */
958f3735
AT
141 t = time(NULL);
142 localtime(&t);
143
144 /* optionally use a log file instead of syslog */
45a83540
DD
145 logfname = lp_log_file();
146 if (logfname) {
15b84e14
DD
147 if (*logfname) {
148 log_open();
45a83540 149 return;
15b84e14 150 }
45a83540 151 logfname = NULL;
4f6325c3
AT
152 }
153
1a016bfd
AT
154#ifdef LOG_NDELAY
155 options |= LOG_NDELAY;
156#endif
157
158#ifdef LOG_DAEMON
159 openlog("rsyncd", options, lp_syslog_facility());
160#else
161 openlog("rsyncd", options);
162#endif
163
164#ifndef LOG_NDELAY
4f6325c3 165 logit(LOG_INFO,"rsyncd started\n");
1a016bfd
AT
166#endif
167}
1a016bfd 168
554e0a8d 169/* this is the underlying (unformatted) rsync debugging function. Call
4a7319be 170 * it with FINFO, FERROR or FLOG */
ff41a59f 171void rwrite(enum logcode code, char *buf, int len)
0b76cd63 172{
8fcdc444 173 FILE *f = NULL;
8d9dc9f9 174 /* recursion can happen with certain fatal conditions */
8d9dc9f9 175
a644fc3c
WD
176 if (quiet && code == FINFO)
177 return;
b86f0cef 178
a644fc3c
WD
179 if (len < 0)
180 exit_cleanup(RERR_MESSAGEIO);
0b76cd63 181
ff8b29b8
AT
182 buf[len] = 0;
183
0bb4d176
WD
184 if (am_server && msg_fd_out >= 0) {
185 /* Pass the message to our sibling. */
186 send_msg((enum msgcode)code, buf, len);
11a5a3c7
AT
187 return;
188 }
189
1eec003a
WD
190 if (code == FCLIENT)
191 code = FINFO;
192 else if (am_daemon) {
0bb4d176
WD
193 static int in_block;
194 char msg[2048];
195 int priority = code == FERROR ? LOG_WARNING : LOG_INFO;
196
197 if (in_block)
a644fc3c 198 return;
0bb4d176
WD
199 in_block = 1;
200 if (!log_initialised)
201 log_init();
202 strlcpy(msg, buf, MIN((int)sizeof msg, len + 1));
203 logit(priority, msg);
204 in_block = 0;
205
206 if (code == FLOG || !am_server)
a644fc3c 207 return;
0bb4d176 208 } else if (code == FLOG)
ff8b29b8 209 return;
0bb4d176
WD
210
211 if (am_server) {
212 /* Pass the message to the non-server side. */
213 if (io_multiplex_write((enum msgcode)code, buf, len))
214 return;
215 if (am_daemon) {
216 /* TODO: can we send the error to the user somehow? */
217 return;
218 }
0b76cd63
AT
219 }
220
ff41a59f 221 if (code == FERROR) {
ff81e809 222 log_got_error = 1;
ff8b29b8 223 f = stderr;
4a7319be 224 }
ff8b29b8 225
0bb4d176
WD
226 if (code == FINFO)
227 f = am_server ? stderr : stdout;
ff8b29b8 228
0bb4d176
WD
229 if (!f)
230 exit_cleanup(RERR_MESSAGEIO);
0b76cd63 231
0bb4d176
WD
232 if (fwrite(buf, len, 1, f) != 1)
233 exit_cleanup(RERR_MESSAGEIO);
8d9dc9f9 234
0bb4d176
WD
235 if (buf[len-1] == '\r' || buf[len-1] == '\n')
236 fflush(f);
0b76cd63 237}
554e0a8d
AT
238
239
a039749b
MP
240/* This is the rsync debugging function. Call it with FINFO, FERROR or
241 * FLOG. */
242void rprintf(enum logcode code, const char *format, ...)
554e0a8d 243{
4a7319be 244 va_list ap;
8fcdc444
WD
245 char buf[MAXPATHLEN+512];
246 size_t len;
554e0a8d
AT
247
248 va_start(ap, format);
ef74f5d6 249 len = vsnprintf(buf, sizeof buf, format, ap);
554e0a8d
AT
250 va_end(ap);
251
b2f02464 252 /* Deal with buffer overruns. Instead of panicking, just
8fcdc444
WD
253 * truncate the resulting string. (Note that configure ensures
254 * that we have a vsnprintf() that doesn't ever return -1.) */
255 if (len > sizeof buf - 1) {
b2f02464
MP
256 const char ellipsis[] = "[...]";
257
258 /* Reset length, and zero-terminate the end of our buffer */
ef74f5d6 259 len = sizeof buf - 1;
b2f02464
MP
260 buf[len] = '\0';
261
262 /* Copy the ellipsis to the end of the string, but give
263 * us one extra character:
264 *
ef74f5d6 265 * v--- null byte at buf[sizeof buf - 1]
b2f02464
MP
266 * abcdefghij0
267 * -> abcd[...]00 <-- now two null bytes at end
268 *
269 * If the input format string has a trailing newline,
270 * we copy it into that extra null; if it doesn't, well,
271 * all we lose is one byte. */
ef74f5d6 272 strncpy(buf+len-sizeof ellipsis, ellipsis, sizeof ellipsis);
b2f02464
MP
273 if (format[strlen(format)-1] == '\n') {
274 buf[len-1] = '\n';
275 }
276 }
554e0a8d 277
ff41a59f 278 rwrite(code, buf, len);
554e0a8d 279}
0b76cd63 280
a039749b
MP
281
282/* This is like rprintf, but it also tries to print some
283 * representation of the error code. Normally errcode = errno.
284 *
285 * Unlike rprintf, this always adds a newline and there should not be
286 * one in the format string.
287 *
288 * Note that since strerror might involve dynamically loading a
289 * message catalog we need to call it once before chroot-ing. */
290void rsyserr(enum logcode code, int errcode, const char *format, ...)
291{
4a7319be 292 va_list ap;
8fcdc444
WD
293 char buf[MAXPATHLEN+512];
294 size_t len;
295
296 strcpy(buf, RSYNC_NAME ": ");
297 len = (sizeof RSYNC_NAME ": ") - 1;
a039749b
MP
298
299 va_start(ap, format);
8fcdc444 300 len += vsnprintf(buf + len, sizeof buf - len, format, ap);
a039749b
MP
301 va_end(ap);
302
8fcdc444
WD
303 if (len < sizeof buf) {
304 len += snprintf(buf + len, sizeof buf - len,
305 ": %s (%d)\n", strerror(errcode), errcode);
306 }
307 if (len >= sizeof buf)
4a7319be 308 exit_cleanup(RERR_MESSAGEIO);
a039749b 309
a039749b
MP
310 rwrite(code, buf, len);
311}
312
313
314
ff41a59f 315void rflush(enum logcode code)
0b76cd63
AT
316{
317 FILE *f = NULL;
0b76cd63
AT
318
319 if (am_daemon) {
320 return;
321 }
322
ff41a59f 323 if (code == FLOG) {
e08bfe12 324 return;
4a7319be 325 }
e08bfe12 326
ff41a59f 327 if (code == FERROR) {
0b76cd63 328 f = stderr;
4a7319be 329 }
0b76cd63 330
ff41a59f 331 if (code == FINFO) {
4a7319be 332 if (am_server)
0b76cd63
AT
333 f = stderr;
334 else
335 f = stdout;
4a7319be 336 }
0b76cd63 337
65417579 338 if (!f) exit_cleanup(RERR_MESSAGEIO);
0b76cd63
AT
339 fflush(f);
340}
341
11a5a3c7 342
e08bfe12
AT
343
344/* a generic logging routine for send/recv, with parameter
4a7319be 345 * substitiution */
0f3203c3 346static void log_formatted(enum logcode code,
b6062654 347 char *format, char *op, struct file_struct *file,
ef74f5d6 348 struct stats *initial_stats, int iflags)
e08bfe12 349{
ef74f5d6 350 char buf[MAXPATHLEN+1024];
e66d70e3 351 char buf2[MAXPATHLEN];
ef74f5d6
WD
352 char *p, *n;
353 size_t len, total;
1b7c47cb 354 int64 b;
e08bfe12 355
aa126974
MP
356 /* We expand % codes one by one in place in buf. We don't
357 * copy in the terminating nul of the inserted strings, but
ef74f5d6
WD
358 * rather keep going until we reach the nul of the format. */
359 total = strlcpy(buf, format, sizeof buf);
e08bfe12 360
ef74f5d6 361 for (p = buf; (p = strchr(p, '%')) != NULL && p[1]; ) {
e08bfe12 362 n = NULL;
e08bfe12
AT
363
364 switch (p[1]) {
af77cc6b
AT
365 case 'h': if (am_daemon) n = client_name(0); break;
366 case 'a': if (am_daemon) n = client_addr(0); break;
4a7319be 367 case 'l':
ef74f5d6 368 snprintf(buf2, sizeof buf2, "%.0f",
4a7319be 369 (double)file->length);
e08bfe12
AT
370 n = buf2;
371 break;
4a7319be 372 case 'p':
ef74f5d6 373 snprintf(buf2, sizeof buf2, "%d",
4a7319be 374 (int)getpid());
e08bfe12
AT
375 n = buf2;
376 break;
377 case 'o': n = op; break;
4a7319be 378 case 'f':
81d2b0ef 379 pathjoin(buf2, sizeof buf2,
c32edbe0 380 am_sender && file->dir.root ? file->dir.root : "",
4875d6b6 381 safe_fname(f_name(file)));
58b1999e 382 clean_fname(buf2, 0);
4a7319be 383 n = buf2;
b6062654 384 if (*n == '/') n++;
ab7104da 385 break;
ef74f5d6
WD
386 case 'n':
387 n = (char*)safe_fname(f_name(file));
e66d70e3
WD
388 if (S_ISDIR(file->mode)) {
389 /* The buffer from safe_fname() has more
390 * room than MAXPATHLEN, so this is safe. */
391 strcat(n, "/");
392 }
ef74f5d6
WD
393 break;
394 case 'L':
19bc826d 395 if (S_ISLNK(file->mode) && file->u.link) {
ef74f5d6
WD
396 snprintf(buf2, sizeof buf2, " -> %s",
397 safe_fname(file->u.link));
398 n = buf2;
399 } else
400 n = "";
401 break;
97cb8dc2 402 case 'm': n = lp_name(module_id); break;
b6062654 403 case 't': n = timestring(time(NULL)); break;
97cb8dc2
AT
404 case 'P': n = lp_path(module_id); break;
405 case 'u': n = auth_user; break;
4a7319be 406 case 'b':
1b7c47cb 407 if (am_sender) {
4a7319be 408 b = stats.total_written -
1b7c47cb
AT
409 initial_stats->total_written;
410 } else {
4a7319be 411 b = stats.total_read -
1b7c47cb
AT
412 initial_stats->total_read;
413 }
ef74f5d6 414 snprintf(buf2, sizeof buf2, "%.0f", (double)b);
1b7c47cb
AT
415 n = buf2;
416 break;
4a7319be 417 case 'c':
1b7c47cb 418 if (!am_sender) {
4a7319be 419 b = stats.total_written -
1b7c47cb
AT
420 initial_stats->total_written;
421 } else {
4a7319be 422 b = stats.total_read -
1b7c47cb
AT
423 initial_stats->total_read;
424 }
ef74f5d6 425 snprintf(buf2, sizeof buf2, "%.0f", (double)b);
1b7c47cb
AT
426 n = buf2;
427 break;
ef74f5d6 428 case 'i':
19bc826d
WD
429 if (iflags & ITEM_DELETED) {
430 n = "deleting";
431 break;
432 }
ef74f5d6 433 n = buf2;
19bc826d 434 n[0] = !(iflags & ITEM_UPDATING) ? '.'
d4e01963 435 : *op == 's' ? '>' : '<';
19bc826d
WD
436 n[1] = S_ISDIR(file->mode) ? 'd'
437 : IS_DEVICE(file->mode) ? 'D'
ef74f5d6 438 : S_ISLNK(file->mode) ? 'L' : 'f';
d4e01963
WD
439 n[2] = !(iflags & ITEM_REPORT_CHECKSUM) ? '.' : 'c';
440 n[3] = !(iflags & ITEM_REPORT_SIZE) ? '.' : 's';
441 n[4] = !(iflags & ITEM_REPORT_TIME) ? '.'
ef74f5d6
WD
442 : !preserve_times || IS_DEVICE(file->mode)
443 || S_ISLNK(file->mode) ? 'T' : 't';
d4e01963
WD
444 n[5] = !(iflags & ITEM_REPORT_PERMS) ? '.' : 'p';
445 n[6] = !(iflags & ITEM_REPORT_OWNER) ? '.' : 'o';
446 n[7] = !(iflags & ITEM_REPORT_GROUP) ? '.' : 'g';
ef74f5d6
WD
447 n[8] = '\0';
448
449 if (iflags & (ITEM_IS_NEW|ITEM_MISSING_DATA)) {
450 char ch = iflags & ITEM_IS_NEW ? '+' : '?';
451 int i;
452 for (i = 2; n[i]; i++)
453 n[i] = ch;
19bc826d
WD
454 } else if (!(iflags & ITEM_UPDATING)) {
455 int i;
456 for (i = 2; n[i]; i++) {
457 if (n[i] != '.')
458 break;
459 }
460 if (!n[i]) {
461 for (i = 2; n[i]; i++)
462 n[i] = ' ';
463 n[0] = '=';
464 }
ef74f5d6
WD
465 }
466 break;
e08bfe12
AT
467 }
468
aa126974 469 /* n is the string to be inserted in place of this %
ef74f5d6 470 * code; len is its length not including the trailing
aa126974 471 * NUL */
ef74f5d6
WD
472 if (!n) {
473 p += 2;
aa126974 474 continue;
ef74f5d6 475 }
e08bfe12 476
ef74f5d6 477 len = strlen(n);
e08bfe12 478
ef74f5d6 479 if (len + total - 2 >= sizeof buf) {
1e7098b5
WD
480 rprintf(FERROR,
481 "buffer overflow expanding %%%c -- exiting\n",
e08bfe12 482 p[0]);
65417579 483 exit_cleanup(RERR_MESSAGEIO);
e08bfe12
AT
484 }
485
aa126974 486 /* Shuffle the rest of the string along to make space for n */
ef74f5d6
WD
487 if (len != 2)
488 memmove(p + len, p + 2, total - (p + 2 - buf) + 1);
489 total += len - 2;
aa126974 490
ef74f5d6
WD
491 /* Insert the contents of string "n", but NOT its nul. */
492 if (len)
493 memcpy(p, n, len);
e08bfe12 494
aa126974 495 /* Skip over inserted string; continue looking */
ef74f5d6 496 p += len;
e08bfe12
AT
497 }
498
ef74f5d6 499 rprintf(code, "%s\n", buf);
e08bfe12
AT
500}
501
11a5a3c7 502/* log the outgoing transfer of a file */
ef74f5d6 503void log_send(struct file_struct *file, struct stats *initial_stats, int iflags)
11a5a3c7 504{
11a5a3c7 505 if (lp_transfer_logging(module_id)) {
ef74f5d6
WD
506 log_formatted(FLOG, lp_log_format(module_id), "send",
507 file, initial_stats, iflags);
b6062654 508 } else if (log_format && !am_server) {
ef74f5d6
WD
509 log_formatted(FINFO, log_format, "send",
510 file, initial_stats, iflags);
11a5a3c7
AT
511 }
512}
513
514/* log the incoming transfer of a file */
ef74f5d6 515void log_recv(struct file_struct *file, struct stats *initial_stats, int iflags)
11a5a3c7 516{
11a5a3c7 517 if (lp_transfer_logging(module_id)) {
ef74f5d6
WD
518 log_formatted(FLOG, lp_log_format(module_id), "recv",
519 file, initial_stats, iflags);
b6062654 520 } else if (log_format && !am_server) {
ef74f5d6
WD
521 log_formatted(FINFO, log_format, "recv",
522 file, initial_stats, iflags);
11a5a3c7
AT
523 }
524}
525
af642a61 526
19bc826d
WD
527void log_delete(char *fname, int mode)
528{
529 static struct file_struct file;
530 int len = strlen(fname);
19bc826d
WD
531 char *fmt;
532
533 file.mode = mode;
534 file.basename = fname;
535
536 if (am_server && protocol_version >= 29 && len < MAXPATHLEN) {
537 if (S_ISDIR(mode))
538 len++; /* directories include trailing null */
539 send_msg(MSG_DELETED, fname, len);
19bc826d 540 } else {
d6707171 541 fmt = log_format_has_o_or_i ? log_format : "%i %n";
1eec003a 542 log_formatted(FCLIENT, fmt, "del.", &file, &stats, ITEM_DELETED);
19bc826d
WD
543 }
544
1eec003a
WD
545 if (!am_daemon || dry_run)
546 return;
547
548 fmt = daemon_log_format_has_o_or_i ? lp_log_format(module_id) : "%i %n";
549 log_formatted(FLOG, fmt, "del.", &file, &stats, ITEM_DELETED);
19bc826d 550}
af642a61
MP
551
552
553/*
554 * Called when the transfer is interrupted for some reason.
555 *
556 * Code is one of the RERR_* codes from errcode.h, or terminating
557 * successfully.
558 */
a9766ef1 559void log_exit(int code, const char *file, int line)
9b73d1c0
AT
560{
561 if (code == 0) {
088aff1a 562 rprintf(FLOG,"sent %.0f bytes received %.0f bytes total size %.0f\n",
9b73d1c0
AT
563 (double)stats.total_written,
564 (double)stats.total_read,
565 (double)stats.total_size);
566 } else {
4a7319be
WD
567 const char *name;
568
569 name = rerr_name(code);
570 if (!name)
571 name = "unexplained error";
af642a61 572
1bca1de6
WD
573 /* VANISHED is not an error, only a warning */
574 if (code == RERR_VANISHED) {
575 rprintf(FINFO, "rsync warning: %s (code %d) at %s(%d)\n",
576 name, code, file, line);
577 } else {
578 rprintf(FERROR, "rsync error: %s (code %d) at %s(%d)\n",
579 name, code, file, line);
580 }
9b73d1c0
AT
581 }
582}