If an error occurs, print an explanatory string rather
[rsync/rsync.git] / log.c
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
27 static char *logfname;
28 static FILE *logfile;
29 static int log_error_fd = -1;
30
31
32 struct {
33         int code;
34         char const *name;
35 } const rerr_names[] = {
36         { RERR_SYNTAX     , "syntax or usage error" }, 
37         { RERR_PROTOCOL   , "protocol incompatibility" }, 
38         { RERR_FILESELECT , "errors selecting input/output files, dirs" }, 
39         { RERR_UNSUPPORTED , "requested action not supported" }, 
40         { RERR_SOCKETIO   , "error in socket IO" }, 
41         { RERR_FILEIO     , "error in file IO" }, 
42         { RERR_STREAMIO   , "error in rsync protocol data stream" }, 
43         { RERR_MESSAGEIO  , "errors with program diagnostics" }, 
44         { RERR_IPC        , "error in IPC code" }, 
45         { RERR_SIGNAL     , "status returned when sent SIGUSR1, SIGINT" }, 
46         { RERR_WAITCHILD  , "some error returned by waitpid()" }, 
47         { RERR_MALLOC     , "error allocating core memory buffers" }, 
48         { RERR_TIMEOUT    , "timeout in data send/receive" }, 
49         { 0, NULL }
50 };
51
52
53
54 /*
55  * Map from rsync error code to name, or return NULL.
56  */
57 static char const *rerr_name(int code)
58 {
59         int i;
60         for (i = 0; rerr_names[i].name; i++) {
61                 if (rerr_names[i].code == code)
62                         return rerr_names[i].name;
63         }
64         return NULL;
65 }
66
67
68 static void logit(int priority, char *buf)
69 {
70         if (logfname) {
71                 if (!logfile)
72                         log_open();
73                 fprintf(logfile,"%s [%d] %s", 
74                         timestring(time(NULL)), (int)getpid(), buf);
75                 fflush(logfile);
76         } else {
77                 syslog(priority, "%s", buf);
78         }
79 }
80
81 void log_init(void)
82 {
83         static int initialised;
84         int options = LOG_PID;
85         time_t t;
86
87         if (initialised) return;
88         initialised = 1;
89
90         /* this looks pointless, but it is needed in order for the
91            C library on some systems to fetch the timezone info
92            before the chroot */
93         t = time(NULL);
94         localtime(&t);
95
96         /* optionally use a log file instead of syslog */
97         logfname = lp_log_file();
98         if (logfname) {
99                 if (*logfname) {
100                         log_open();
101                         return;
102                 }
103                 logfname = NULL;
104         }
105
106 #ifdef LOG_NDELAY
107         options |= LOG_NDELAY;
108 #endif
109
110 #ifdef LOG_DAEMON
111         openlog("rsyncd", options, lp_syslog_facility());
112 #else
113         openlog("rsyncd", options);
114 #endif
115
116 #ifndef LOG_NDELAY
117         logit(LOG_INFO,"rsyncd started\n");
118 #endif
119 }
120
121 void log_open()
122 {
123         if (logfname && !logfile) {
124                 extern int orig_umask;
125                 int old_umask = umask(022 | orig_umask);
126                 logfile = fopen(logfname, "a");
127                 umask(old_umask);
128         }
129 }
130
131 void log_close()
132 {
133         if (logfile) {
134                 fclose(logfile);
135                 logfile = NULL;
136         }
137 }
138
139 /* setup the error file descriptor - used when we are a server
140    that is receiving files */
141 void set_error_fd(int fd)
142 {
143         log_error_fd = fd;
144 }
145
146 /* this is the underlying (unformatted) rsync debugging function. Call
147    it with FINFO, FERROR or FLOG */
148 void rwrite(enum logcode code, char *buf, int len)
149 {
150         FILE *f=NULL;
151         extern int am_daemon;
152         extern int am_server;
153         extern int quiet;
154         /* recursion can happen with certain fatal conditions */
155
156         if (quiet && code == FINFO) return;
157
158         if (len < 0) exit_cleanup(RERR_MESSAGEIO);
159
160         buf[len] = 0;
161
162         if (code == FLOG) {
163                 if (am_daemon) logit(LOG_INFO, buf);
164                 return;
165         }
166
167         /* first try to pass it off the our sibling */
168         if (am_server && io_error_write(log_error_fd, code, buf, len)) {
169                 return;
170         }
171
172         /* then try to pass it to the other end */
173         if (am_server && io_multiplex_write(code, buf, len)) {
174                 return;
175         }
176
177         if (am_daemon) {
178                 static int depth;
179                 int priority = LOG_INFO;
180                 if (code == FERROR) priority = LOG_WARNING;
181
182                 if (depth) return;
183
184                 depth++;
185
186                 log_init();
187                 logit(priority, buf);
188
189                 depth--;
190                 return;
191         }
192
193         if (code == FERROR) {
194                 f = stderr;
195         } 
196
197         if (code == FINFO) {
198                 if (am_server) 
199                         f = stderr;
200                 else
201                         f = stdout;
202         } 
203
204         if (!f) exit_cleanup(RERR_MESSAGEIO);
205
206         if (fwrite(buf, len, 1, f) != 1) exit_cleanup(RERR_MESSAGEIO);
207
208         if (buf[len-1] == '\r' || buf[len-1] == '\n') fflush(f);
209 }
210                 
211
212 /* This is the rsync debugging function. Call it with FINFO, FERROR or
213  * FLOG. */
214 void rprintf(enum logcode code, const char *format, ...)
215 {
216         va_list ap;  
217         char buf[1024];
218         int len;
219
220         va_start(ap, format);
221         len = vslprintf(buf, sizeof(buf), format, ap);
222         va_end(ap);
223
224         if (len > sizeof(buf)-1) exit_cleanup(RERR_MESSAGEIO);
225
226         rwrite(code, buf, len);
227 }
228
229
230 /* This is like rprintf, but it also tries to print some
231  * representation of the error code.  Normally errcode = errno.
232  *
233  * Unlike rprintf, this always adds a newline and there should not be
234  * one in the format string.
235  *
236  * Note that since strerror might involve dynamically loading a
237  * message catalog we need to call it once before chroot-ing. */
238 void rsyserr(enum logcode code, int errcode, const char *format, ...)
239 {
240         va_list ap;  
241         char buf[1024];
242         int len, sys_len;
243         char *sysmsg;
244
245         va_start(ap, format);
246         len = vslprintf(buf, sizeof(buf), format, ap);
247         va_end(ap);
248
249         if (len > sizeof(buf)-1) exit_cleanup(RERR_MESSAGEIO);
250
251         sysmsg = strerror(errcode);
252         sys_len = strlen(sysmsg);
253         if (len + 3 + sys_len > sizeof(buf) - 1)
254                 exit_cleanup(RERR_MESSAGEIO);
255
256         strcpy(buf + len, ": ");
257         len += 2;
258         strcpy(buf + len, sysmsg);
259         len += sys_len;
260         strcpy(buf + len, "\n");
261         len++;
262
263         rwrite(code, buf, len);
264 }
265
266
267
268 void rflush(enum logcode code)
269 {
270         FILE *f = NULL;
271         extern int am_daemon;
272         
273         if (am_daemon) {
274                 return;
275         }
276
277         if (code == FLOG) {
278                 return;
279         } 
280
281         if (code == FERROR) {
282                 f = stderr;
283         } 
284
285         if (code == FINFO) {
286                 extern int am_server;
287                 if (am_server) 
288                         f = stderr;
289                 else
290                         f = stdout;
291         } 
292
293         if (!f) exit_cleanup(RERR_MESSAGEIO);
294         fflush(f);
295 }
296
297
298
299 /* a generic logging routine for send/recv, with parameter
300    substitiution */
301 static void log_formatted(enum logcode code,
302                           char *format, char *op, struct file_struct *file,
303                           struct stats *initial_stats)
304 {
305         extern int module_id;
306         extern char *auth_user;
307         char buf[1024];
308         char buf2[1024];
309         char *p, *s, *n;
310         int l;
311         extern struct stats stats;              
312         extern int am_sender;
313         extern int am_daemon;
314         int64 b;
315
316         strlcpy(buf, format, sizeof(buf));
317         
318         for (s=&buf[0]; 
319              s && (p=strchr(s,'%')); ) {
320                 n = NULL;
321                 s = p + 1;
322
323                 switch (p[1]) {
324                 case 'h': if (am_daemon) n = client_name(0); break;
325                 case 'a': if (am_daemon) n = client_addr(0); break;
326                 case 'l': 
327                         slprintf(buf2,sizeof(buf2),"%.0f", 
328                                  (double)file->length); 
329                         n = buf2;
330                         break;
331                 case 'p': 
332                         slprintf(buf2,sizeof(buf2),"%d", 
333                                  (int)getpid()); 
334                         n = buf2;
335                         break;
336                 case 'o': n = op; break;
337                 case 'f': 
338                         slprintf(buf2, sizeof(buf2), "%s/%s", 
339                                  file->basedir?file->basedir:"", 
340                                  f_name(file));
341                         clean_fname(buf2);
342                         n = buf2; 
343                         if (*n == '/') n++;
344                         break;
345                 case 'm': n = lp_name(module_id); break;
346                 case 't': n = timestring(time(NULL)); break;
347                 case 'P': n = lp_path(module_id); break;
348                 case 'u': n = auth_user; break;
349                 case 'b': 
350                         if (am_sender) {
351                                 b = stats.total_written - 
352                                         initial_stats->total_written;
353                         } else {
354                                 b = stats.total_read - 
355                                         initial_stats->total_read;
356                         }
357                         slprintf(buf2,sizeof(buf2),"%.0f", (double)b); 
358                         n = buf2;
359                         break;
360                 case 'c': 
361                         if (!am_sender) {
362                                 b = stats.total_written - 
363                                         initial_stats->total_written;
364                         } else {
365                                 b = stats.total_read - 
366                                         initial_stats->total_read;
367                         }
368                         slprintf(buf2,sizeof(buf2),"%.0f", (double)b); 
369                         n = buf2;
370                         break;
371                 }
372
373                 if (!n) continue;
374
375                 l = strlen(n);
376
377                 if ((l-1) + ((int)(s - &buf[0])) > sizeof(buf)) {
378                         rprintf(FERROR,"buffer overflow expanding %%%c - exiting\n",
379                                 p[0]);
380                         exit_cleanup(RERR_MESSAGEIO);
381                 }
382
383                 if (l != 2) {
384                         memmove(s+(l-1), s+1, strlen(s+1)+1);
385                 }
386                 memcpy(p, n, l);
387
388                 s = p+l;
389         }
390
391         rprintf(code,"%s\n", buf);
392 }
393
394 /* log the outgoing transfer of a file */
395 void log_send(struct file_struct *file, struct stats *initial_stats)
396 {
397         extern int module_id;
398         extern int am_server;
399         extern char *log_format;
400
401         if (lp_transfer_logging(module_id)) {
402                 log_formatted(FLOG, lp_log_format(module_id), "send", file, initial_stats);
403         } else if (log_format && !am_server) {
404                 log_formatted(FINFO, log_format, "send", file, initial_stats);
405         }
406 }
407
408 /* log the incoming transfer of a file */
409 void log_recv(struct file_struct *file, struct stats *initial_stats)
410 {
411         extern int module_id;
412         extern int am_server;
413         extern char *log_format;
414
415         if (lp_transfer_logging(module_id)) {
416                 log_formatted(FLOG, lp_log_format(module_id), "recv", file, initial_stats);
417         } else if (log_format && !am_server) {
418                 log_formatted(FINFO, log_format, "recv", file, initial_stats);
419         }
420 }
421
422
423
424
425 /*
426  * Called when the transfer is interrupted for some reason.
427  *
428  * Code is one of the RERR_* codes from errcode.h, or terminating
429  * successfully.
430  */
431 void log_exit(int code, const char *file, int line)
432 {
433         if (code == 0) {
434                 extern struct stats stats;              
435                 rprintf(FLOG,"wrote %.0f bytes  read %.0f bytes  total size %.0f\n",
436                         (double)stats.total_written,
437                         (double)stats.total_read,
438                         (double)stats.total_size);
439         } else {
440                 const char *name;
441
442                 name = rerr_name(code);
443                 if (!name)
444                         name = "unexplained error";
445                 
446                 rprintf(FLOG,"transfer interrupted: %s (code %d) at %s(%d)\n", 
447                         name, code, file, line);
448         }
449 }
450
451
452
453
454 /* log the incoming transfer of a file for interactive use, this
455    will be called at the end where the client was run 
456    
457    it i called when a file starts to be transferred
458 */
459 void log_transfer(struct file_struct *file, const char *fname)
460 {
461         extern int verbose;
462
463         if (!verbose) return;
464
465         rprintf(FINFO,"%s\n", fname);
466 }
467