Commit | Line | Data |
---|---|---|
a039749b MP |
1 | /* -*- c-file-style: "linux"; -*- |
2 | ||
3 | Copyright (C) 1998-2000 by Andrew Tridgell | |
0b76cd63 AT |
4 | |
5 | This program is free software; you can redistribute it and/or modify | |
6 | it under the terms of the GNU General Public License as published by | |
7 | the Free Software Foundation; either version 2 of the License, or | |
8 | (at your option) any later version. | |
9 | ||
10 | This program is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | GNU General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU General Public License | |
16 | along with this program; if not, write to the Free Software | |
17 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
18 | */ | |
19 | ||
20 | /* | |
21 | logging and utility functions | |
22 | ||
23 | tridge, May 1998 | |
24 | */ | |
25 | #include "rsync.h" | |
26 | ||
45a83540 | 27 | static char *logfname; |
4f6325c3 | 28 | static FILE *logfile; |
554e0a8d | 29 | static int log_error_fd = -1; |
e0414f42 | 30 | |
4f6325c3 AT |
31 | static void logit(int priority, char *buf) |
32 | { | |
45a83540 | 33 | if (logfname) { |
15b84e14 DD |
34 | if (!logfile) |
35 | log_open(); | |
67ea0d48 | 36 | fprintf(logfile,"%s [%d] %s", |
f7632fc6 | 37 | timestring(time(NULL)), (int)getpid(), buf); |
4f6325c3 AT |
38 | fflush(logfile); |
39 | } else { | |
40 | syslog(priority, "%s", buf); | |
41 | } | |
42 | } | |
e42c9458 | 43 | |
45a83540 | 44 | void log_init(void) |
1a016bfd AT |
45 | { |
46 | static int initialised; | |
47 | int options = LOG_PID; | |
bcf5b133 | 48 | time_t t; |
1a016bfd AT |
49 | |
50 | if (initialised) return; | |
51 | initialised = 1; | |
52 | ||
958f3735 AT |
53 | /* this looks pointless, but it is needed in order for the |
54 | C library on some systems to fetch the timezone info | |
55 | before the chroot */ | |
56 | t = time(NULL); | |
57 | localtime(&t); | |
58 | ||
59 | /* optionally use a log file instead of syslog */ | |
45a83540 DD |
60 | logfname = lp_log_file(); |
61 | if (logfname) { | |
15b84e14 DD |
62 | if (*logfname) { |
63 | log_open(); | |
45a83540 | 64 | return; |
15b84e14 | 65 | } |
45a83540 | 66 | logfname = NULL; |
4f6325c3 AT |
67 | } |
68 | ||
1a016bfd AT |
69 | #ifdef LOG_NDELAY |
70 | options |= LOG_NDELAY; | |
71 | #endif | |
72 | ||
73 | #ifdef LOG_DAEMON | |
74 | openlog("rsyncd", options, lp_syslog_facility()); | |
75 | #else | |
76 | openlog("rsyncd", options); | |
77 | #endif | |
78 | ||
79 | #ifndef LOG_NDELAY | |
4f6325c3 | 80 | logit(LOG_INFO,"rsyncd started\n"); |
1a016bfd AT |
81 | #endif |
82 | } | |
1a016bfd | 83 | |
15b84e14 DD |
84 | void log_open() |
85 | { | |
86 | if (logfname && !logfile) { | |
87 | extern int orig_umask; | |
88 | int old_umask = umask(022 | orig_umask); | |
89 | logfile = fopen(logfname, "a"); | |
90 | umask(old_umask); | |
91 | } | |
92 | } | |
93 | ||
94 | void log_close() | |
45a83540 DD |
95 | { |
96 | if (logfile) { | |
97 | fclose(logfile); | |
98 | logfile = NULL; | |
99 | } | |
100 | } | |
101 | ||
554e0a8d AT |
102 | /* setup the error file descriptor - used when we are a server |
103 | that is receiving files */ | |
104 | void set_error_fd(int fd) | |
105 | { | |
106 | log_error_fd = fd; | |
107 | } | |
108 | ||
109 | /* this is the underlying (unformatted) rsync debugging function. Call | |
110 | it with FINFO, FERROR or FLOG */ | |
ff41a59f | 111 | void rwrite(enum logcode code, char *buf, int len) |
0b76cd63 | 112 | { |
0b76cd63 AT |
113 | FILE *f=NULL; |
114 | extern int am_daemon; | |
4a814638 | 115 | extern int am_server; |
b86f0cef | 116 | extern int quiet; |
8d9dc9f9 | 117 | /* recursion can happen with certain fatal conditions */ |
8d9dc9f9 | 118 | |
6d7b6081 | 119 | if (quiet && code == FINFO) return; |
b86f0cef | 120 | |
65417579 | 121 | if (len < 0) exit_cleanup(RERR_MESSAGEIO); |
0b76cd63 | 122 | |
ff8b29b8 AT |
123 | buf[len] = 0; |
124 | ||
ff41a59f | 125 | if (code == FLOG) { |
11a5a3c7 | 126 | if (am_daemon) logit(LOG_INFO, buf); |
11a5a3c7 AT |
127 | return; |
128 | } | |
129 | ||
4a814638 AT |
130 | /* first try to pass it off the our sibling */ |
131 | if (am_server && io_error_write(log_error_fd, code, buf, len)) { | |
6d7b6081 AT |
132 | return; |
133 | } | |
134 | ||
4a814638 AT |
135 | /* then try to pass it to the other end */ |
136 | if (am_server && io_multiplex_write(code, buf, len)) { | |
6d7b6081 AT |
137 | return; |
138 | } | |
ff41a59f | 139 | |
ff8b29b8 | 140 | if (am_daemon) { |
b24203b3 | 141 | static int depth; |
ff8b29b8 | 142 | int priority = LOG_INFO; |
ff41a59f | 143 | if (code == FERROR) priority = LOG_WARNING; |
45ccc5c0 | 144 | |
b24203b3 AT |
145 | if (depth) return; |
146 | ||
f27b53f5 AT |
147 | depth++; |
148 | ||
45a83540 | 149 | log_init(); |
6d7b6081 | 150 | logit(priority, buf); |
8d9dc9f9 AT |
151 | |
152 | depth--; | |
ff8b29b8 | 153 | return; |
0b76cd63 AT |
154 | } |
155 | ||
ff41a59f | 156 | if (code == FERROR) { |
ff8b29b8 AT |
157 | f = stderr; |
158 | } | |
159 | ||
ff41a59f | 160 | if (code == FINFO) { |
ff8b29b8 AT |
161 | if (am_server) |
162 | f = stderr; | |
163 | else | |
164 | f = stdout; | |
165 | } | |
166 | ||
65417579 | 167 | if (!f) exit_cleanup(RERR_MESSAGEIO); |
0b76cd63 | 168 | |
65417579 | 169 | if (fwrite(buf, len, 1, f) != 1) exit_cleanup(RERR_MESSAGEIO); |
8d9dc9f9 | 170 | |
bcf5b133 | 171 | if (buf[len-1] == '\r' || buf[len-1] == '\n') fflush(f); |
0b76cd63 | 172 | } |
554e0a8d AT |
173 | |
174 | ||
a039749b MP |
175 | /* This is the rsync debugging function. Call it with FINFO, FERROR or |
176 | * FLOG. */ | |
177 | void rprintf(enum logcode code, const char *format, ...) | |
554e0a8d AT |
178 | { |
179 | va_list ap; | |
180 | char buf[1024]; | |
181 | int len; | |
182 | ||
183 | va_start(ap, format); | |
184 | len = vslprintf(buf, sizeof(buf), format, ap); | |
185 | va_end(ap); | |
186 | ||
187 | if (len > sizeof(buf)-1) exit_cleanup(RERR_MESSAGEIO); | |
188 | ||
ff41a59f | 189 | rwrite(code, buf, len); |
554e0a8d | 190 | } |
0b76cd63 | 191 | |
a039749b MP |
192 | |
193 | /* This is like rprintf, but it also tries to print some | |
194 | * representation of the error code. Normally errcode = errno. | |
195 | * | |
196 | * Unlike rprintf, this always adds a newline and there should not be | |
197 | * one in the format string. | |
198 | * | |
199 | * Note that since strerror might involve dynamically loading a | |
200 | * message catalog we need to call it once before chroot-ing. */ | |
201 | void rsyserr(enum logcode code, int errcode, const char *format, ...) | |
202 | { | |
203 | va_list ap; | |
204 | char buf[1024]; | |
205 | int len, sys_len; | |
206 | char *sysmsg; | |
207 | ||
208 | va_start(ap, format); | |
209 | len = vslprintf(buf, sizeof(buf), format, ap); | |
210 | va_end(ap); | |
211 | ||
212 | if (len > sizeof(buf)-1) exit_cleanup(RERR_MESSAGEIO); | |
213 | ||
214 | sysmsg = strerror(errcode); | |
215 | sys_len = strlen(sysmsg); | |
216 | if (len + 3 + sys_len > sizeof(buf) - 1) | |
217 | exit_cleanup(RERR_MESSAGEIO); | |
218 | ||
219 | strcpy(buf + len, ": "); | |
220 | len += 2; | |
221 | strcpy(buf + len, sysmsg); | |
222 | len += sys_len; | |
223 | strcpy(buf + len, "\n"); | |
224 | len++; | |
225 | ||
226 | rwrite(code, buf, len); | |
227 | } | |
228 | ||
229 | ||
230 | ||
ff41a59f | 231 | void rflush(enum logcode code) |
0b76cd63 AT |
232 | { |
233 | FILE *f = NULL; | |
234 | extern int am_daemon; | |
235 | ||
236 | if (am_daemon) { | |
237 | return; | |
238 | } | |
239 | ||
ff41a59f | 240 | if (code == FLOG) { |
e08bfe12 AT |
241 | return; |
242 | } | |
243 | ||
ff41a59f | 244 | if (code == FERROR) { |
0b76cd63 AT |
245 | f = stderr; |
246 | } | |
247 | ||
ff41a59f | 248 | if (code == FINFO) { |
0b76cd63 AT |
249 | extern int am_server; |
250 | if (am_server) | |
251 | f = stderr; | |
252 | else | |
253 | f = stdout; | |
254 | } | |
255 | ||
65417579 | 256 | if (!f) exit_cleanup(RERR_MESSAGEIO); |
0b76cd63 AT |
257 | fflush(f); |
258 | } | |
259 | ||
11a5a3c7 | 260 | |
e08bfe12 AT |
261 | |
262 | /* a generic logging routine for send/recv, with parameter | |
263 | substitiution */ | |
0f3203c3 | 264 | static void log_formatted(enum logcode code, |
b6062654 | 265 | char *format, char *op, struct file_struct *file, |
1b7c47cb | 266 | struct stats *initial_stats) |
e08bfe12 AT |
267 | { |
268 | extern int module_id; | |
97cb8dc2 | 269 | extern char *auth_user; |
e08bfe12 | 270 | char buf[1024]; |
ab7104da | 271 | char buf2[1024]; |
e08bfe12 | 272 | char *p, *s, *n; |
e08bfe12 | 273 | int l; |
1b7c47cb AT |
274 | extern struct stats stats; |
275 | extern int am_sender; | |
af77cc6b | 276 | extern int am_daemon; |
1b7c47cb | 277 | int64 b; |
e08bfe12 | 278 | |
37f9805d | 279 | strlcpy(buf, format, sizeof(buf)); |
e08bfe12 AT |
280 | |
281 | for (s=&buf[0]; | |
282 | s && (p=strchr(s,'%')); ) { | |
283 | n = NULL; | |
284 | s = p + 1; | |
285 | ||
286 | switch (p[1]) { | |
af77cc6b AT |
287 | case 'h': if (am_daemon) n = client_name(0); break; |
288 | case 'a': if (am_daemon) n = client_addr(0); break; | |
e08bfe12 | 289 | case 'l': |
37f9805d | 290 | slprintf(buf2,sizeof(buf2),"%.0f", |
e08bfe12 AT |
291 | (double)file->length); |
292 | n = buf2; | |
293 | break; | |
294 | case 'p': | |
37f9805d | 295 | slprintf(buf2,sizeof(buf2),"%d", |
e08bfe12 AT |
296 | (int)getpid()); |
297 | n = buf2; | |
298 | break; | |
299 | case 'o': n = op; break; | |
ab7104da | 300 | case 'f': |
37f9805d | 301 | slprintf(buf2, sizeof(buf2), "%s/%s", |
ab7104da AT |
302 | file->basedir?file->basedir:"", |
303 | f_name(file)); | |
304 | clean_fname(buf2); | |
305 | n = buf2; | |
b6062654 | 306 | if (*n == '/') n++; |
ab7104da | 307 | break; |
97cb8dc2 | 308 | case 'm': n = lp_name(module_id); break; |
b6062654 | 309 | case 't': n = timestring(time(NULL)); break; |
97cb8dc2 AT |
310 | case 'P': n = lp_path(module_id); break; |
311 | case 'u': n = auth_user; break; | |
1b7c47cb AT |
312 | case 'b': |
313 | if (am_sender) { | |
314 | b = stats.total_written - | |
315 | initial_stats->total_written; | |
316 | } else { | |
317 | b = stats.total_read - | |
318 | initial_stats->total_read; | |
319 | } | |
37f9805d | 320 | slprintf(buf2,sizeof(buf2),"%.0f", (double)b); |
1b7c47cb AT |
321 | n = buf2; |
322 | break; | |
323 | case 'c': | |
324 | if (!am_sender) { | |
325 | b = stats.total_written - | |
326 | initial_stats->total_written; | |
327 | } else { | |
328 | b = stats.total_read - | |
329 | initial_stats->total_read; | |
330 | } | |
37f9805d | 331 | slprintf(buf2,sizeof(buf2),"%.0f", (double)b); |
1b7c47cb AT |
332 | n = buf2; |
333 | break; | |
e08bfe12 AT |
334 | } |
335 | ||
336 | if (!n) continue; | |
337 | ||
338 | l = strlen(n); | |
339 | ||
340 | if ((l-1) + ((int)(s - &buf[0])) > sizeof(buf)) { | |
341 | rprintf(FERROR,"buffer overflow expanding %%%c - exiting\n", | |
342 | p[0]); | |
65417579 | 343 | exit_cleanup(RERR_MESSAGEIO); |
e08bfe12 AT |
344 | } |
345 | ||
346 | if (l != 2) { | |
34720097 | 347 | memmove(s+(l-1), s+1, strlen(s+1)+1); |
e08bfe12 AT |
348 | } |
349 | memcpy(p, n, l); | |
350 | ||
351 | s = p+l; | |
352 | } | |
353 | ||
0f3203c3 | 354 | rprintf(code,"%s\n", buf); |
e08bfe12 AT |
355 | } |
356 | ||
11a5a3c7 | 357 | /* log the outgoing transfer of a file */ |
1b7c47cb | 358 | void log_send(struct file_struct *file, struct stats *initial_stats) |
11a5a3c7 AT |
359 | { |
360 | extern int module_id; | |
b6062654 AT |
361 | extern int am_server; |
362 | extern char *log_format; | |
363 | ||
11a5a3c7 | 364 | if (lp_transfer_logging(module_id)) { |
b6062654 AT |
365 | log_formatted(FLOG, lp_log_format(module_id), "send", file, initial_stats); |
366 | } else if (log_format && !am_server) { | |
367 | log_formatted(FINFO, log_format, "send", file, initial_stats); | |
11a5a3c7 AT |
368 | } |
369 | } | |
370 | ||
371 | /* log the incoming transfer of a file */ | |
1b7c47cb | 372 | void log_recv(struct file_struct *file, struct stats *initial_stats) |
11a5a3c7 AT |
373 | { |
374 | extern int module_id; | |
b6062654 AT |
375 | extern int am_server; |
376 | extern char *log_format; | |
377 | ||
11a5a3c7 | 378 | if (lp_transfer_logging(module_id)) { |
117af102 | 379 | log_formatted(FLOG, lp_log_format(module_id), "recv", file, initial_stats); |
b6062654 | 380 | } else if (log_format && !am_server) { |
117af102 | 381 | log_formatted(FINFO, log_format, "recv", file, initial_stats); |
11a5a3c7 AT |
382 | } |
383 | } | |
384 | ||
9b73d1c0 | 385 | /* called when the transfer is interrupted for some reason */ |
a9766ef1 | 386 | void log_exit(int code, const char *file, int line) |
9b73d1c0 AT |
387 | { |
388 | if (code == 0) { | |
389 | extern struct stats stats; | |
67ea0d48 | 390 | rprintf(FLOG,"wrote %.0f bytes read %.0f bytes total size %.0f\n", |
9b73d1c0 AT |
391 | (double)stats.total_written, |
392 | (double)stats.total_read, | |
393 | (double)stats.total_size); | |
394 | } else { | |
a9766ef1 AT |
395 | rprintf(FLOG,"transfer interrupted (code %d) at %s(%d)\n", |
396 | code, file, line); | |
9b73d1c0 AT |
397 | } |
398 | } | |
399 | ||
11a5a3c7 | 400 | /* log the incoming transfer of a file for interactive use, this |
b6062654 AT |
401 | will be called at the end where the client was run |
402 | ||
403 | it i called when a file starts to be transferred | |
404 | */ | |
a9766ef1 | 405 | void log_transfer(struct file_struct *file, const char *fname) |
11a5a3c7 AT |
406 | { |
407 | extern int verbose; | |
408 | ||
409 | if (!verbose) return; | |
410 | ||
411 | rprintf(FINFO,"%s\n", fname); | |
412 | } | |
9b73d1c0 | 413 |