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