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