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