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