Doc.
[rsync/rsync.git] / log.c
... / ...
CommitLineData
1/* -*- c-file-style: "linux"; -*-
2
3 Copyright (C) 1998-2000 by Andrew Tridgell
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
27static char *logfname;
28static FILE *logfile;
29static int log_error_fd = -1;
30
31static void logit(int priority, char *buf)
32{
33 if (logfname) {
34 if (!logfile)
35 log_open();
36 fprintf(logfile,"%s [%d] %s",
37 timestring(time(NULL)), (int)getpid(), buf);
38 fflush(logfile);
39 } else {
40 syslog(priority, "%s", buf);
41 }
42}
43
44void log_init(void)
45{
46 static int initialised;
47 int options = LOG_PID;
48 time_t t;
49
50 if (initialised) return;
51 initialised = 1;
52
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 */
60 logfname = lp_log_file();
61 if (logfname) {
62 if (*logfname) {
63 log_open();
64 return;
65 }
66 logfname = NULL;
67 }
68
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
80 logit(LOG_INFO,"rsyncd started\n");
81#endif
82}
83
84void 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
94void log_close()
95{
96 if (logfile) {
97 fclose(logfile);
98 logfile = NULL;
99 }
100}
101
102/* setup the error file descriptor - used when we are a server
103 that is receiving files */
104void 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 */
111void rwrite(enum logcode code, char *buf, int len)
112{
113 FILE *f=NULL;
114 extern int am_daemon;
115 extern int am_server;
116 extern int quiet;
117 /* recursion can happen with certain fatal conditions */
118
119 if (quiet && code == FINFO) return;
120
121 if (len < 0) exit_cleanup(RERR_MESSAGEIO);
122
123 buf[len] = 0;
124
125 if (code == FLOG) {
126 if (am_daemon) logit(LOG_INFO, buf);
127 return;
128 }
129
130 /* first try to pass it off the our sibling */
131 if (am_server && io_error_write(log_error_fd, code, buf, len)) {
132 return;
133 }
134
135 /* then try to pass it to the other end */
136 if (am_server && io_multiplex_write(code, buf, len)) {
137 return;
138 }
139
140 if (am_daemon) {
141 static int depth;
142 int priority = LOG_INFO;
143 if (code == FERROR) priority = LOG_WARNING;
144
145 if (depth) return;
146
147 depth++;
148
149 log_init();
150 logit(priority, buf);
151
152 depth--;
153 return;
154 }
155
156 if (code == FERROR) {
157 f = stderr;
158 }
159
160 if (code == FINFO) {
161 if (am_server)
162 f = stderr;
163 else
164 f = stdout;
165 }
166
167 if (!f) exit_cleanup(RERR_MESSAGEIO);
168
169 if (fwrite(buf, len, 1, f) != 1) exit_cleanup(RERR_MESSAGEIO);
170
171 if (buf[len-1] == '\r' || buf[len-1] == '\n') fflush(f);
172}
173
174
175/* This is the rsync debugging function. Call it with FINFO, FERROR or
176 * FLOG. */
177void rprintf(enum logcode code, const char *format, ...)
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
189 rwrite(code, buf, len);
190}
191
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. */
201void 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
231void rflush(enum logcode code)
232{
233 FILE *f = NULL;
234 extern int am_daemon;
235
236 if (am_daemon) {
237 return;
238 }
239
240 if (code == FLOG) {
241 return;
242 }
243
244 if (code == FERROR) {
245 f = stderr;
246 }
247
248 if (code == FINFO) {
249 extern int am_server;
250 if (am_server)
251 f = stderr;
252 else
253 f = stdout;
254 }
255
256 if (!f) exit_cleanup(RERR_MESSAGEIO);
257 fflush(f);
258}
259
260
261
262/* a generic logging routine for send/recv, with parameter
263 substitiution */
264static void log_formatted(enum logcode code,
265 char *format, char *op, struct file_struct *file,
266 struct stats *initial_stats)
267{
268 extern int module_id;
269 extern char *auth_user;
270 char buf[1024];
271 char buf2[1024];
272 char *p, *s, *n;
273 int l;
274 extern struct stats stats;
275 extern int am_sender;
276 extern int am_daemon;
277 int64 b;
278
279 strlcpy(buf, format, sizeof(buf));
280
281 for (s=&buf[0];
282 s && (p=strchr(s,'%')); ) {
283 n = NULL;
284 s = p + 1;
285
286 switch (p[1]) {
287 case 'h': if (am_daemon) n = client_name(0); break;
288 case 'a': if (am_daemon) n = client_addr(0); break;
289 case 'l':
290 slprintf(buf2,sizeof(buf2),"%.0f",
291 (double)file->length);
292 n = buf2;
293 break;
294 case 'p':
295 slprintf(buf2,sizeof(buf2),"%d",
296 (int)getpid());
297 n = buf2;
298 break;
299 case 'o': n = op; break;
300 case 'f':
301 slprintf(buf2, sizeof(buf2), "%s/%s",
302 file->basedir?file->basedir:"",
303 f_name(file));
304 clean_fname(buf2);
305 n = buf2;
306 if (*n == '/') n++;
307 break;
308 case 'm': n = lp_name(module_id); break;
309 case 't': n = timestring(time(NULL)); break;
310 case 'P': n = lp_path(module_id); break;
311 case 'u': n = auth_user; break;
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 }
320 slprintf(buf2,sizeof(buf2),"%.0f", (double)b);
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 }
331 slprintf(buf2,sizeof(buf2),"%.0f", (double)b);
332 n = buf2;
333 break;
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]);
343 exit_cleanup(RERR_MESSAGEIO);
344 }
345
346 if (l != 2) {
347 memmove(s+(l-1), s+1, strlen(s+1)+1);
348 }
349 memcpy(p, n, l);
350
351 s = p+l;
352 }
353
354 rprintf(code,"%s\n", buf);
355}
356
357/* log the outgoing transfer of a file */
358void log_send(struct file_struct *file, struct stats *initial_stats)
359{
360 extern int module_id;
361 extern int am_server;
362 extern char *log_format;
363
364 if (lp_transfer_logging(module_id)) {
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);
368 }
369}
370
371/* log the incoming transfer of a file */
372void log_recv(struct file_struct *file, struct stats *initial_stats)
373{
374 extern int module_id;
375 extern int am_server;
376 extern char *log_format;
377
378 if (lp_transfer_logging(module_id)) {
379 log_formatted(FLOG, lp_log_format(module_id), "recv", file, initial_stats);
380 } else if (log_format && !am_server) {
381 log_formatted(FINFO, log_format, "recv", file, initial_stats);
382 }
383}
384
385/* called when the transfer is interrupted for some reason */
386void log_exit(int code, const char *file, int line)
387{
388 if (code == 0) {
389 extern struct stats stats;
390 rprintf(FLOG,"wrote %.0f bytes read %.0f bytes total size %.0f\n",
391 (double)stats.total_written,
392 (double)stats.total_read,
393 (double)stats.total_size);
394 } else {
395 rprintf(FLOG,"transfer interrupted (code %d) at %s(%d)\n",
396 code, file, line);
397 }
398}
399
400/* log the incoming transfer of a file for interactive use, this
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*/
405void log_transfer(struct file_struct *file, const char *fname)
406{
407 extern int verbose;
408
409 if (!verbose) return;
410
411 rprintf(FINFO,"%s\n", fname);
412}
413