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