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