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