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