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