move the read only daemon test to after the protocol setup
[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
4f6325c3 26static FILE *logfile;
554e0a8d 27static int log_error_fd = -1;
e0414f42 28
4f6325c3
AT
29static void logit(int priority, char *buf)
30{
31 if (logfile) {
67ea0d48 32 fprintf(logfile,"%s [%d] %s",
f7632fc6 33 timestring(time(NULL)), (int)getpid(), buf);
4f6325c3
AT
34 fflush(logfile);
35 } else {
36 syslog(priority, "%s", buf);
37 }
38}
e42c9458 39
1a016bfd
AT
40void log_open(void)
41{
42 static int initialised;
43 int options = LOG_PID;
bcf5b133 44 time_t t;
27d3cdbc 45 char *logf;
1a016bfd
AT
46
47 if (initialised) return;
48 initialised = 1;
49
958f3735
AT
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 */
27d3cdbc
AT
57 logf = lp_log_file();
58 if (logf && *logf) {
e8030905
AT
59 extern int orig_umask;
60 int old_umask = umask(022 | orig_umask);
27d3cdbc 61 logfile = fopen(logf, "a");
6265551a 62 umask(old_umask);
4f6325c3
AT
63 return;
64 }
65
1a016bfd
AT
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
4f6325c3 77 logit(LOG_INFO,"rsyncd started\n");
1a016bfd
AT
78#endif
79}
1a016bfd 80
554e0a8d
AT
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 */
ff41a59f 90void rwrite(enum logcode code, char *buf, int len)
0b76cd63 91{
0b76cd63
AT
92 FILE *f=NULL;
93 extern int am_daemon;
b86f0cef 94 extern int quiet;
8d9dc9f9 95 /* recursion can happen with certain fatal conditions */
8d9dc9f9 96
6d7b6081 97 if (quiet && code == FINFO) return;
b86f0cef 98
65417579 99 if (len < 0) exit_cleanup(RERR_MESSAGEIO);
0b76cd63 100
ff8b29b8
AT
101 buf[len] = 0;
102
ff41a59f 103 if (code == FLOG) {
11a5a3c7 104 if (am_daemon) logit(LOG_INFO, buf);
11a5a3c7
AT
105 return;
106 }
107
6d7b6081
AT
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 }
ff41a59f 115
ff8b29b8 116 if (am_daemon) {
b24203b3 117 static int depth;
ff8b29b8 118 int priority = LOG_INFO;
ff41a59f 119 if (code == FERROR) priority = LOG_WARNING;
45ccc5c0 120
b24203b3
AT
121 if (depth) return;
122
f27b53f5
AT
123 depth++;
124
1a016bfd 125 log_open();
6d7b6081 126 logit(priority, buf);
8d9dc9f9
AT
127
128 depth--;
ff8b29b8 129 return;
0b76cd63
AT
130 }
131
ff41a59f 132 if (code == FERROR) {
ff8b29b8
AT
133 f = stderr;
134 }
135
ff41a59f 136 if (code == FINFO) {
ff8b29b8
AT
137 extern int am_server;
138 if (am_server)
139 f = stderr;
140 else
141 f = stdout;
142 }
143
65417579 144 if (!f) exit_cleanup(RERR_MESSAGEIO);
0b76cd63 145
65417579 146 if (fwrite(buf, len, 1, f) != 1) exit_cleanup(RERR_MESSAGEIO);
8d9dc9f9 147
bcf5b133 148 if (buf[len-1] == '\r' || buf[len-1] == '\n') fflush(f);
0b76cd63 149}
554e0a8d
AT
150
151
152/* this is the rsync debugging function. Call it with FINFO, FERROR or FLOG */
ff41a59f 153 void rprintf(enum logcode code, const char *format, ...)
554e0a8d
AT
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
ff41a59f 165 rwrite(code, buf, len);
554e0a8d 166}
0b76cd63 167
ff41a59f 168void rflush(enum logcode code)
0b76cd63
AT
169{
170 FILE *f = NULL;
171 extern int am_daemon;
172
173 if (am_daemon) {
174 return;
175 }
176
ff41a59f 177 if (code == FLOG) {
e08bfe12
AT
178 return;
179 }
180
ff41a59f 181 if (code == FERROR) {
0b76cd63
AT
182 f = stderr;
183 }
184
ff41a59f 185 if (code == FINFO) {
0b76cd63
AT
186 extern int am_server;
187 if (am_server)
188 f = stderr;
189 else
190 f = stdout;
191 }
192
65417579 193 if (!f) exit_cleanup(RERR_MESSAGEIO);
0b76cd63
AT
194 fflush(f);
195}
196
11a5a3c7 197
e08bfe12
AT
198
199/* a generic logging routine for send/recv, with parameter
200 substitiution */
0f3203c3 201static void log_formatted(enum logcode code,
b6062654 202 char *format, char *op, struct file_struct *file,
1b7c47cb 203 struct stats *initial_stats)
e08bfe12
AT
204{
205 extern int module_id;
97cb8dc2 206 extern char *auth_user;
e08bfe12 207 char buf[1024];
ab7104da 208 char buf2[1024];
e08bfe12 209 char *p, *s, *n;
e08bfe12 210 int l;
1b7c47cb
AT
211 extern struct stats stats;
212 extern int am_sender;
af77cc6b 213 extern int am_daemon;
1b7c47cb 214 int64 b;
e08bfe12 215
37f9805d 216 strlcpy(buf, format, sizeof(buf));
e08bfe12
AT
217
218 for (s=&buf[0];
219 s && (p=strchr(s,'%')); ) {
220 n = NULL;
221 s = p + 1;
222
223 switch (p[1]) {
af77cc6b
AT
224 case 'h': if (am_daemon) n = client_name(0); break;
225 case 'a': if (am_daemon) n = client_addr(0); break;
e08bfe12 226 case 'l':
37f9805d 227 slprintf(buf2,sizeof(buf2),"%.0f",
e08bfe12
AT
228 (double)file->length);
229 n = buf2;
230 break;
231 case 'p':
37f9805d 232 slprintf(buf2,sizeof(buf2),"%d",
e08bfe12
AT
233 (int)getpid());
234 n = buf2;
235 break;
236 case 'o': n = op; break;
ab7104da 237 case 'f':
37f9805d 238 slprintf(buf2, sizeof(buf2), "%s/%s",
ab7104da
AT
239 file->basedir?file->basedir:"",
240 f_name(file));
241 clean_fname(buf2);
242 n = buf2;
b6062654 243 if (*n == '/') n++;
ab7104da 244 break;
97cb8dc2 245 case 'm': n = lp_name(module_id); break;
b6062654 246 case 't': n = timestring(time(NULL)); break;
97cb8dc2
AT
247 case 'P': n = lp_path(module_id); break;
248 case 'u': n = auth_user; break;
1b7c47cb
AT
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 }
37f9805d 257 slprintf(buf2,sizeof(buf2),"%.0f", (double)b);
1b7c47cb
AT
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 }
37f9805d 268 slprintf(buf2,sizeof(buf2),"%.0f", (double)b);
1b7c47cb
AT
269 n = buf2;
270 break;
e08bfe12
AT
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]);
65417579 280 exit_cleanup(RERR_MESSAGEIO);
e08bfe12
AT
281 }
282
283 if (l != 2) {
34720097 284 memmove(s+(l-1), s+1, strlen(s+1)+1);
e08bfe12
AT
285 }
286 memcpy(p, n, l);
287
288 s = p+l;
289 }
290
0f3203c3 291 rprintf(code,"%s\n", buf);
e08bfe12
AT
292}
293
11a5a3c7 294/* log the outgoing transfer of a file */
1b7c47cb 295void log_send(struct file_struct *file, struct stats *initial_stats)
11a5a3c7
AT
296{
297 extern int module_id;
b6062654
AT
298 extern int am_server;
299 extern char *log_format;
300
11a5a3c7 301 if (lp_transfer_logging(module_id)) {
b6062654
AT
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);
11a5a3c7
AT
305 }
306}
307
308/* log the incoming transfer of a file */
1b7c47cb 309void log_recv(struct file_struct *file, struct stats *initial_stats)
11a5a3c7
AT
310{
311 extern int module_id;
b6062654
AT
312 extern int am_server;
313 extern char *log_format;
314
11a5a3c7 315 if (lp_transfer_logging(module_id)) {
117af102 316 log_formatted(FLOG, lp_log_format(module_id), "recv", file, initial_stats);
b6062654 317 } else if (log_format && !am_server) {
117af102 318 log_formatted(FINFO, log_format, "recv", file, initial_stats);
11a5a3c7
AT
319 }
320}
321
9b73d1c0 322/* called when the transfer is interrupted for some reason */
a9766ef1 323void log_exit(int code, const char *file, int line)
9b73d1c0
AT
324{
325 if (code == 0) {
326 extern struct stats stats;
67ea0d48 327 rprintf(FLOG,"wrote %.0f bytes read %.0f bytes total size %.0f\n",
9b73d1c0
AT
328 (double)stats.total_written,
329 (double)stats.total_read,
330 (double)stats.total_size);
331 } else {
a9766ef1
AT
332 rprintf(FLOG,"transfer interrupted (code %d) at %s(%d)\n",
333 code, file, line);
9b73d1c0
AT
334 }
335}
336
11a5a3c7 337/* log the incoming transfer of a file for interactive use, this
b6062654
AT
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*/
a9766ef1 342void log_transfer(struct file_struct *file, const char *fname)
11a5a3c7
AT
343{
344 extern int verbose;
345
346 if (!verbose) return;
347
348 rprintf(FINFO,"%s\n", fname);
349}
9b73d1c0 350