Make sure the log file is always opened before root privileges (if any)
[rsync/rsync.git] / log.c
CommitLineData
0b76cd63
AT
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
45a83540 26static char *logfname;
4f6325c3 27static FILE *logfile;
554e0a8d 28static int log_error_fd = -1;
e0414f42 29
4f6325c3
AT
30static void logit(int priority, char *buf)
31{
45a83540 32 if (logfname) {
15b84e14
DD
33 if (!logfile)
34 log_open();
67ea0d48 35 fprintf(logfile,"%s [%d] %s",
f7632fc6 36 timestring(time(NULL)), (int)getpid(), buf);
4f6325c3
AT
37 fflush(logfile);
38 } else {
39 syslog(priority, "%s", buf);
40 }
41}
e42c9458 42
45a83540 43void log_init(void)
1a016bfd
AT
44{
45 static int initialised;
46 int options = LOG_PID;
bcf5b133 47 time_t t;
1a016bfd
AT
48
49 if (initialised) return;
50 initialised = 1;
51
958f3735
AT
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 */
45a83540
DD
59 logfname = lp_log_file();
60 if (logfname) {
15b84e14
DD
61 if (*logfname) {
62 log_open();
45a83540 63 return;
15b84e14 64 }
45a83540 65 logfname = NULL;
4f6325c3
AT
66 }
67
1a016bfd
AT
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
4f6325c3 79 logit(LOG_INFO,"rsyncd started\n");
1a016bfd
AT
80#endif
81}
1a016bfd 82
15b84e14
DD
83void 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
93void log_close()
45a83540
DD
94{
95 if (logfile) {
96 fclose(logfile);
97 logfile = NULL;
98 }
99}
100
554e0a8d
AT
101/* setup the error file descriptor - used when we are a server
102 that is receiving files */
103void 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 */
ff41a59f 110void rwrite(enum logcode code, char *buf, int len)
0b76cd63 111{
0b76cd63
AT
112 FILE *f=NULL;
113 extern int am_daemon;
4a814638 114 extern int am_server;
b86f0cef 115 extern int quiet;
8d9dc9f9 116 /* recursion can happen with certain fatal conditions */
8d9dc9f9 117
6d7b6081 118 if (quiet && code == FINFO) return;
b86f0cef 119
65417579 120 if (len < 0) exit_cleanup(RERR_MESSAGEIO);
0b76cd63 121
ff8b29b8
AT
122 buf[len] = 0;
123
ff41a59f 124 if (code == FLOG) {
11a5a3c7 125 if (am_daemon) logit(LOG_INFO, buf);
11a5a3c7
AT
126 return;
127 }
128
4a814638
AT
129 /* first try to pass it off the our sibling */
130 if (am_server && io_error_write(log_error_fd, code, buf, len)) {
6d7b6081
AT
131 return;
132 }
133
4a814638
AT
134 /* then try to pass it to the other end */
135 if (am_server && io_multiplex_write(code, buf, len)) {
6d7b6081
AT
136 return;
137 }
ff41a59f 138
ff8b29b8 139 if (am_daemon) {
b24203b3 140 static int depth;
ff8b29b8 141 int priority = LOG_INFO;
ff41a59f 142 if (code == FERROR) priority = LOG_WARNING;
45ccc5c0 143
b24203b3
AT
144 if (depth) return;
145
f27b53f5
AT
146 depth++;
147
45a83540 148 log_init();
6d7b6081 149 logit(priority, buf);
8d9dc9f9
AT
150
151 depth--;
ff8b29b8 152 return;
0b76cd63
AT
153 }
154
ff41a59f 155 if (code == FERROR) {
ff8b29b8
AT
156 f = stderr;
157 }
158
ff41a59f 159 if (code == FINFO) {
ff8b29b8
AT
160 if (am_server)
161 f = stderr;
162 else
163 f = stdout;
164 }
165
65417579 166 if (!f) exit_cleanup(RERR_MESSAGEIO);
0b76cd63 167
65417579 168 if (fwrite(buf, len, 1, f) != 1) exit_cleanup(RERR_MESSAGEIO);
8d9dc9f9 169
bcf5b133 170 if (buf[len-1] == '\r' || buf[len-1] == '\n') fflush(f);
0b76cd63 171}
554e0a8d
AT
172
173
174/* this is the rsync debugging function. Call it with FINFO, FERROR or FLOG */
ff41a59f 175 void rprintf(enum logcode code, const char *format, ...)
554e0a8d
AT
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
ff41a59f 187 rwrite(code, buf, len);
554e0a8d 188}
0b76cd63 189
ff41a59f 190void rflush(enum logcode code)
0b76cd63
AT
191{
192 FILE *f = NULL;
193 extern int am_daemon;
194
195 if (am_daemon) {
196 return;
197 }
198
ff41a59f 199 if (code == FLOG) {
e08bfe12
AT
200 return;
201 }
202
ff41a59f 203 if (code == FERROR) {
0b76cd63
AT
204 f = stderr;
205 }
206
ff41a59f 207 if (code == FINFO) {
0b76cd63
AT
208 extern int am_server;
209 if (am_server)
210 f = stderr;
211 else
212 f = stdout;
213 }
214
65417579 215 if (!f) exit_cleanup(RERR_MESSAGEIO);
0b76cd63
AT
216 fflush(f);
217}
218
11a5a3c7 219
e08bfe12
AT
220
221/* a generic logging routine for send/recv, with parameter
222 substitiution */
0f3203c3 223static void log_formatted(enum logcode code,
b6062654 224 char *format, char *op, struct file_struct *file,
1b7c47cb 225 struct stats *initial_stats)
e08bfe12
AT
226{
227 extern int module_id;
97cb8dc2 228 extern char *auth_user;
e08bfe12 229 char buf[1024];
ab7104da 230 char buf2[1024];
e08bfe12 231 char *p, *s, *n;
e08bfe12 232 int l;
1b7c47cb
AT
233 extern struct stats stats;
234 extern int am_sender;
af77cc6b 235 extern int am_daemon;
1b7c47cb 236 int64 b;
e08bfe12 237
37f9805d 238 strlcpy(buf, format, sizeof(buf));
e08bfe12
AT
239
240 for (s=&buf[0];
241 s && (p=strchr(s,'%')); ) {
242 n = NULL;
243 s = p + 1;
244
245 switch (p[1]) {
af77cc6b
AT
246 case 'h': if (am_daemon) n = client_name(0); break;
247 case 'a': if (am_daemon) n = client_addr(0); break;
e08bfe12 248 case 'l':
37f9805d 249 slprintf(buf2,sizeof(buf2),"%.0f",
e08bfe12
AT
250 (double)file->length);
251 n = buf2;
252 break;
253 case 'p':
37f9805d 254 slprintf(buf2,sizeof(buf2),"%d",
e08bfe12
AT
255 (int)getpid());
256 n = buf2;
257 break;
258 case 'o': n = op; break;
ab7104da 259 case 'f':
37f9805d 260 slprintf(buf2, sizeof(buf2), "%s/%s",
ab7104da
AT
261 file->basedir?file->basedir:"",
262 f_name(file));
263 clean_fname(buf2);
264 n = buf2;
b6062654 265 if (*n == '/') n++;
ab7104da 266 break;
97cb8dc2 267 case 'm': n = lp_name(module_id); break;
b6062654 268 case 't': n = timestring(time(NULL)); break;
97cb8dc2
AT
269 case 'P': n = lp_path(module_id); break;
270 case 'u': n = auth_user; break;
1b7c47cb
AT
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 }
37f9805d 279 slprintf(buf2,sizeof(buf2),"%.0f", (double)b);
1b7c47cb
AT
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 }
37f9805d 290 slprintf(buf2,sizeof(buf2),"%.0f", (double)b);
1b7c47cb
AT
291 n = buf2;
292 break;
e08bfe12
AT
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]);
65417579 302 exit_cleanup(RERR_MESSAGEIO);
e08bfe12
AT
303 }
304
305 if (l != 2) {
34720097 306 memmove(s+(l-1), s+1, strlen(s+1)+1);
e08bfe12
AT
307 }
308 memcpy(p, n, l);
309
310 s = p+l;
311 }
312
0f3203c3 313 rprintf(code,"%s\n", buf);
e08bfe12
AT
314}
315
11a5a3c7 316/* log the outgoing transfer of a file */
1b7c47cb 317void log_send(struct file_struct *file, struct stats *initial_stats)
11a5a3c7
AT
318{
319 extern int module_id;
b6062654
AT
320 extern int am_server;
321 extern char *log_format;
322
11a5a3c7 323 if (lp_transfer_logging(module_id)) {
b6062654
AT
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);
11a5a3c7
AT
327 }
328}
329
330/* log the incoming transfer of a file */
1b7c47cb 331void log_recv(struct file_struct *file, struct stats *initial_stats)
11a5a3c7
AT
332{
333 extern int module_id;
b6062654
AT
334 extern int am_server;
335 extern char *log_format;
336
11a5a3c7 337 if (lp_transfer_logging(module_id)) {
117af102 338 log_formatted(FLOG, lp_log_format(module_id), "recv", file, initial_stats);
b6062654 339 } else if (log_format && !am_server) {
117af102 340 log_formatted(FINFO, log_format, "recv", file, initial_stats);
11a5a3c7
AT
341 }
342}
343
9b73d1c0 344/* called when the transfer is interrupted for some reason */
a9766ef1 345void log_exit(int code, const char *file, int line)
9b73d1c0
AT
346{
347 if (code == 0) {
348 extern struct stats stats;
67ea0d48 349 rprintf(FLOG,"wrote %.0f bytes read %.0f bytes total size %.0f\n",
9b73d1c0
AT
350 (double)stats.total_written,
351 (double)stats.total_read,
352 (double)stats.total_size);
353 } else {
a9766ef1
AT
354 rprintf(FLOG,"transfer interrupted (code %d) at %s(%d)\n",
355 code, file, line);
9b73d1c0
AT
356 }
357}
358
11a5a3c7 359/* log the incoming transfer of a file for interactive use, this
b6062654
AT
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*/
a9766ef1 364void log_transfer(struct file_struct *file, const char *fname)
11a5a3c7
AT
365{
366 extern int verbose;
367
368 if (!verbose) return;
369
370 rprintf(FINFO,"%s\n", fname);
371}
9b73d1c0 372