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