Check for alloca.h and mcheck.h, as included popt needs to know about
[rsync/rsync.git] / log.c
1 /* -*- c-file-style: "linux"; -*-
2    
3    Copyright (C) 1998-2001 by Andrew Tridgell <tridge@samba.org>
4    Copyright (C) 2000-2001 by Martin Pool <mbp@samba.org>
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 /*
22   Logging and utility functions.
23   tridge, May 1998
24
25   Mapping to human-readable messages added by Martin Pool
26   <mbp@samba.org>, Oct 2000.
27   */
28 #include "rsync.h"
29
30 static char *logfname;
31 static FILE *logfile;
32 static int log_error_fd = -1;
33
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" }, 
48         { RERR_SIGNAL     , "received SIGUSR1 or SIGINT" }, 
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
71 static void logit(int priority, char *buf)
72 {
73         if (logfname) {
74                 if (!logfile)
75                         log_open();
76                 fprintf(logfile,"%s [%d] %s", 
77                         timestring(time(NULL)), (int)getpid(), buf);
78                 fflush(logfile);
79         } else {
80                 syslog(priority, "%s", buf);
81         }
82 }
83
84 void log_init(void)
85 {
86         static int initialised;
87         int options = LOG_PID;
88         time_t t;
89
90         if (initialised) return;
91         initialised = 1;
92
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 */
100         logfname = lp_log_file();
101         if (logfname) {
102                 if (*logfname) {
103                         log_open();
104                         return;
105                 }
106                 logfname = NULL;
107         }
108
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
120         logit(LOG_INFO,"rsyncd started\n");
121 #endif
122 }
123
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()
135 {
136         if (logfile) {
137                 fclose(logfile);
138                 logfile = NULL;
139         }
140 }
141
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 */
151 void rwrite(enum logcode code, char *buf, int len)
152 {
153         FILE *f=NULL;
154         extern int am_daemon;
155         extern int am_server;
156         extern int quiet;
157         /* recursion can happen with certain fatal conditions */
158
159         if (quiet && code == FINFO) return;
160
161         if (len < 0) exit_cleanup(RERR_MESSAGEIO);
162
163         buf[len] = 0;
164
165         if (code == FLOG) {
166                 if (am_daemon) logit(LOG_INFO, buf);
167                 return;
168         }
169
170         /* first try to pass it off the our sibling */
171         if (am_server && io_error_write(log_error_fd, code, buf, len)) {
172                 return;
173         }
174
175         /* if that fails, try to pass it to the other end */
176         if (am_server && io_multiplex_write(code, buf, len)) {
177                 return;
178         }
179
180         if (am_daemon) {
181                 static int depth;
182                 int priority = LOG_INFO;
183                 if (code == FERROR) priority = LOG_WARNING;
184
185                 if (depth) return;
186
187                 depth++;
188
189                 log_init();
190                 logit(priority, buf);
191
192                 depth--;
193                 return;
194         }
195
196         if (code == FERROR) {
197                 f = stderr;
198         } 
199
200         if (code == FINFO) {
201                 if (am_server) 
202                         f = stderr;
203                 else
204                         f = stdout;
205         } 
206
207         if (!f) exit_cleanup(RERR_MESSAGEIO);
208
209         if (fwrite(buf, len, 1, f) != 1) exit_cleanup(RERR_MESSAGEIO);
210
211         if (buf[len-1] == '\r' || buf[len-1] == '\n') fflush(f);
212 }
213                 
214
215 /* This is the rsync debugging function. Call it with FINFO, FERROR or
216  * FLOG. */
217 void rprintf(enum logcode code, const char *format, ...)
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
229         rwrite(code, buf, len);
230 }
231
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
271 void rflush(enum logcode code)
272 {
273         FILE *f = NULL;
274         extern int am_daemon;
275         
276         if (am_daemon) {
277                 return;
278         }
279
280         if (code == FLOG) {
281                 return;
282         } 
283
284         if (code == FERROR) {
285                 f = stderr;
286         } 
287
288         if (code == FINFO) {
289                 extern int am_server;
290                 if (am_server) 
291                         f = stderr;
292                 else
293                         f = stdout;
294         } 
295
296         if (!f) exit_cleanup(RERR_MESSAGEIO);
297         fflush(f);
298 }
299
300
301
302 /* a generic logging routine for send/recv, with parameter
303    substitiution */
304 static void log_formatted(enum logcode code,
305                           char *format, char *op, struct file_struct *file,
306                           struct stats *initial_stats)
307 {
308         extern int module_id;
309         extern char *auth_user;
310         char buf[1024];
311         char buf2[1024];
312         char *p, *s, *n;
313         int l;
314         extern struct stats stats;              
315         extern int am_sender;
316         extern int am_daemon;
317         int64 b;
318
319         strlcpy(buf, format, sizeof(buf));
320         
321         for (s=&buf[0]; 
322              s && (p=strchr(s,'%')); ) {
323                 n = NULL;
324                 s = p + 1;
325
326                 switch (p[1]) {
327                 case 'h': if (am_daemon) n = client_name(0); break;
328                 case 'a': if (am_daemon) n = client_addr(0); break;
329                 case 'l': 
330                         slprintf(buf2,sizeof(buf2),"%.0f", 
331                                  (double)file->length); 
332                         n = buf2;
333                         break;
334                 case 'p': 
335                         slprintf(buf2,sizeof(buf2),"%d", 
336                                  (int)getpid()); 
337                         n = buf2;
338                         break;
339                 case 'o': n = op; break;
340                 case 'f': 
341                         slprintf(buf2, sizeof(buf2), "%s/%s", 
342                                  file->basedir?file->basedir:"", 
343                                  f_name(file));
344                         clean_fname(buf2);
345                         n = buf2; 
346                         if (*n == '/') n++;
347                         break;
348                 case 'm': n = lp_name(module_id); break;
349                 case 't': n = timestring(time(NULL)); break;
350                 case 'P': n = lp_path(module_id); break;
351                 case 'u': n = auth_user; break;
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                         }
360                         slprintf(buf2,sizeof(buf2),"%.0f", (double)b); 
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                         }
371                         slprintf(buf2,sizeof(buf2),"%.0f", (double)b); 
372                         n = buf2;
373                         break;
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]);
383                         exit_cleanup(RERR_MESSAGEIO);
384                 }
385
386                 if (l != 2) {
387                         memmove(s+(l-1), s+1, strlen(s+1)+1);
388                 }
389                 memcpy(p, n, l);
390
391                 s = p+l;
392         }
393
394         rprintf(code,"%s\n", buf);
395 }
396
397 /* log the outgoing transfer of a file */
398 void log_send(struct file_struct *file, struct stats *initial_stats)
399 {
400         extern int module_id;
401         extern int am_server;
402         extern char *log_format;
403
404         if (lp_transfer_logging(module_id)) {
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);
408         }
409 }
410
411 /* log the incoming transfer of a file */
412 void log_recv(struct file_struct *file, struct stats *initial_stats)
413 {
414         extern int module_id;
415         extern int am_server;
416         extern char *log_format;
417
418         if (lp_transfer_logging(module_id)) {
419                 log_formatted(FLOG, lp_log_format(module_id), "recv", file, initial_stats);
420         } else if (log_format && !am_server) {
421                 log_formatted(FINFO, log_format, "recv", file, initial_stats);
422         }
423 }
424
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  */
434 void log_exit(int code, const char *file, int line)
435 {
436         if (code == 0) {
437                 extern struct stats stats;              
438                 rprintf(FLOG,"wrote %.0f bytes  read %.0f bytes  total size %.0f\n",
439                         (double)stats.total_written,
440                         (double)stats.total_read,
441                         (double)stats.total_size);
442         } else {
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);
451         }
452 }
453
454
455
456
457 /* log the incoming transfer of a file for interactive use, this
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 */
462 void log_transfer(struct file_struct *file, const char *fname)
463 {
464         extern int verbose;
465
466         if (!verbose) return;
467
468         rprintf(FINFO,"%s\n", fname);
469 }
470