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