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