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