When running as --daemon in the background and using a "log file" rsyncd.conf
[rsync/rsync.git] / log.c
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
26 static char *logfname;
27 static FILE *logfile;
28 static int log_error_fd = -1;
29
30 static void logit(int priority, char *buf)
31 {
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                 }
39                 fprintf(logfile,"%s [%d] %s", 
40                         timestring(time(NULL)), (int)getpid(), buf);
41                 fflush(logfile);
42         } else {
43                 syslog(priority, "%s", buf);
44         }
45 }
46
47 void log_init(void)
48 {
49         static int initialised;
50         int options = LOG_PID;
51         time_t t;
52
53         if (initialised) return;
54         initialised = 1;
55
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 */
63         logfname = lp_log_file();
64         if (logfname) {
65                 if (*logfname)
66                         return;
67                 logfname = NULL;
68         }
69
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
81         logit(LOG_INFO,"rsyncd started\n");
82 #endif
83 }
84
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
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 */
104 void rwrite(enum logcode code, char *buf, int len)
105 {
106         FILE *f=NULL;
107         extern int am_daemon;
108         extern int am_server;
109         extern int quiet;
110         /* recursion can happen with certain fatal conditions */
111
112         if (quiet && code == FINFO) return;
113
114         if (len < 0) exit_cleanup(RERR_MESSAGEIO);
115
116         buf[len] = 0;
117
118         if (code == FLOG) {
119                 if (am_daemon) logit(LOG_INFO, buf);
120                 return;
121         }
122
123         /* first try to pass it off the our sibling */
124         if (am_server && io_error_write(log_error_fd, code, buf, len)) {
125                 return;
126         }
127
128         /* then try to pass it to the other end */
129         if (am_server && io_multiplex_write(code, buf, len)) {
130                 return;
131         }
132
133         if (am_daemon) {
134                 static int depth;
135                 int priority = LOG_INFO;
136                 if (code == FERROR) priority = LOG_WARNING;
137
138                 if (depth) return;
139
140                 depth++;
141
142                 log_init();
143                 logit(priority, buf);
144
145                 depth--;
146                 return;
147         }
148
149         if (code == FERROR) {
150                 f = stderr;
151         } 
152
153         if (code == FINFO) {
154                 if (am_server) 
155                         f = stderr;
156                 else
157                         f = stdout;
158         } 
159
160         if (!f) exit_cleanup(RERR_MESSAGEIO);
161
162         if (fwrite(buf, len, 1, f) != 1) exit_cleanup(RERR_MESSAGEIO);
163
164         if (buf[len-1] == '\r' || buf[len-1] == '\n') fflush(f);
165 }
166                 
167
168 /* this is the rsync debugging function. Call it with FINFO, FERROR or FLOG */
169  void rprintf(enum logcode code, const char *format, ...)
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
181         rwrite(code, buf, len);
182 }
183
184 void rflush(enum logcode code)
185 {
186         FILE *f = NULL;
187         extern int am_daemon;
188         
189         if (am_daemon) {
190                 return;
191         }
192
193         if (code == FLOG) {
194                 return;
195         } 
196
197         if (code == FERROR) {
198                 f = stderr;
199         } 
200
201         if (code == FINFO) {
202                 extern int am_server;
203                 if (am_server) 
204                         f = stderr;
205                 else
206                         f = stdout;
207         } 
208
209         if (!f) exit_cleanup(RERR_MESSAGEIO);
210         fflush(f);
211 }
212
213
214
215 /* a generic logging routine for send/recv, with parameter
216    substitiution */
217 static void log_formatted(enum logcode code,
218                           char *format, char *op, struct file_struct *file,
219                           struct stats *initial_stats)
220 {
221         extern int module_id;
222         extern char *auth_user;
223         char buf[1024];
224         char buf2[1024];
225         char *p, *s, *n;
226         int l;
227         extern struct stats stats;              
228         extern int am_sender;
229         extern int am_daemon;
230         int64 b;
231
232         strlcpy(buf, format, sizeof(buf));
233         
234         for (s=&buf[0]; 
235              s && (p=strchr(s,'%')); ) {
236                 n = NULL;
237                 s = p + 1;
238
239                 switch (p[1]) {
240                 case 'h': if (am_daemon) n = client_name(0); break;
241                 case 'a': if (am_daemon) n = client_addr(0); break;
242                 case 'l': 
243                         slprintf(buf2,sizeof(buf2),"%.0f", 
244                                  (double)file->length); 
245                         n = buf2;
246                         break;
247                 case 'p': 
248                         slprintf(buf2,sizeof(buf2),"%d", 
249                                  (int)getpid()); 
250                         n = buf2;
251                         break;
252                 case 'o': n = op; break;
253                 case 'f': 
254                         slprintf(buf2, sizeof(buf2), "%s/%s", 
255                                  file->basedir?file->basedir:"", 
256                                  f_name(file));
257                         clean_fname(buf2);
258                         n = buf2; 
259                         if (*n == '/') n++;
260                         break;
261                 case 'm': n = lp_name(module_id); break;
262                 case 't': n = timestring(time(NULL)); break;
263                 case 'P': n = lp_path(module_id); break;
264                 case 'u': n = auth_user; break;
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                         }
273                         slprintf(buf2,sizeof(buf2),"%.0f", (double)b); 
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                         }
284                         slprintf(buf2,sizeof(buf2),"%.0f", (double)b); 
285                         n = buf2;
286                         break;
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]);
296                         exit_cleanup(RERR_MESSAGEIO);
297                 }
298
299                 if (l != 2) {
300                         memmove(s+(l-1), s+1, strlen(s+1)+1);
301                 }
302                 memcpy(p, n, l);
303
304                 s = p+l;
305         }
306
307         rprintf(code,"%s\n", buf);
308 }
309
310 /* log the outgoing transfer of a file */
311 void log_send(struct file_struct *file, struct stats *initial_stats)
312 {
313         extern int module_id;
314         extern int am_server;
315         extern char *log_format;
316
317         if (lp_transfer_logging(module_id)) {
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);
321         }
322 }
323
324 /* log the incoming transfer of a file */
325 void log_recv(struct file_struct *file, struct stats *initial_stats)
326 {
327         extern int module_id;
328         extern int am_server;
329         extern char *log_format;
330
331         if (lp_transfer_logging(module_id)) {
332                 log_formatted(FLOG, lp_log_format(module_id), "recv", file, initial_stats);
333         } else if (log_format && !am_server) {
334                 log_formatted(FINFO, log_format, "recv", file, initial_stats);
335         }
336 }
337
338 /* called when the transfer is interrupted for some reason */
339 void log_exit(int code, const char *file, int line)
340 {
341         if (code == 0) {
342                 extern struct stats stats;              
343                 rprintf(FLOG,"wrote %.0f bytes  read %.0f bytes  total size %.0f\n",
344                         (double)stats.total_written,
345                         (double)stats.total_read,
346                         (double)stats.total_size);
347         } else {
348                 rprintf(FLOG,"transfer interrupted (code %d) at %s(%d)\n", 
349                         code, file, line);
350         }
351 }
352
353 /* log the incoming transfer of a file for interactive use, this
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 */
358 void log_transfer(struct file_struct *file, const char *fname)
359 {
360         extern int verbose;
361
362         if (!verbose) return;
363
364         rprintf(FINFO,"%s\n", fname);
365 }
366