Call this 2.4.6dev so that we don't get bug reports claiming to be
[rsync/rsync.git] / log.c
CommitLineData
a039749b
MP
1/* -*- c-file-style: "linux"; -*-
2
3 Copyright (C) 1998-2000 by Andrew Tridgell
0b76cd63
AT
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18*/
19
20/*
21 logging and utility functions
22
23 tridge, May 1998
24 */
25#include "rsync.h"
26
45a83540 27static char *logfname;
4f6325c3 28static FILE *logfile;
554e0a8d 29static int log_error_fd = -1;
e0414f42 30
af642a61
MP
31
32struct {
33 int code;
34 char const *name;
35} const rerr_names[] = {
36 { RERR_SYNTAX , "syntax or usage error" },
37 { RERR_PROTOCOL , "protocol incompatibility" },
38 { RERR_FILESELECT , "errors selecting input/output files, dirs" },
39 { RERR_UNSUPPORTED , "requested action not supported" },
40 { RERR_SOCKETIO , "error in socket IO" },
41 { RERR_FILEIO , "error in file IO" },
42 { RERR_STREAMIO , "error in rsync protocol data stream" },
43 { RERR_MESSAGEIO , "errors with program diagnostics" },
44 { RERR_IPC , "error in IPC code" },
45 { RERR_SIGNAL , "status returned when sent SIGUSR1, SIGINT" },
46 { RERR_WAITCHILD , "some error returned by waitpid()" },
47 { RERR_MALLOC , "error allocating core memory buffers" },
48 { RERR_TIMEOUT , "timeout in data send/receive" },
49 { 0, NULL }
50};
51
52
53
54/*
55 * Map from rsync error code to name, or return NULL.
56 */
57static char const *rerr_name(int code)
58{
59 int i;
60 for (i = 0; rerr_names[i].name; i++) {
61 if (rerr_names[i].code == code)
62 return rerr_names[i].name;
63 }
64 return NULL;
65}
66
67
4f6325c3
AT
68static void logit(int priority, char *buf)
69{
45a83540 70 if (logfname) {
15b84e14
DD
71 if (!logfile)
72 log_open();
67ea0d48 73 fprintf(logfile,"%s [%d] %s",
f7632fc6 74 timestring(time(NULL)), (int)getpid(), buf);
4f6325c3
AT
75 fflush(logfile);
76 } else {
77 syslog(priority, "%s", buf);
78 }
79}
e42c9458 80
45a83540 81void log_init(void)
1a016bfd
AT
82{
83 static int initialised;
84 int options = LOG_PID;
bcf5b133 85 time_t t;
1a016bfd
AT
86
87 if (initialised) return;
88 initialised = 1;
89
958f3735
AT
90 /* this looks pointless, but it is needed in order for the
91 C library on some systems to fetch the timezone info
92 before the chroot */
93 t = time(NULL);
94 localtime(&t);
95
96 /* optionally use a log file instead of syslog */
45a83540
DD
97 logfname = lp_log_file();
98 if (logfname) {
15b84e14
DD
99 if (*logfname) {
100 log_open();
45a83540 101 return;
15b84e14 102 }
45a83540 103 logfname = NULL;
4f6325c3
AT
104 }
105
1a016bfd
AT
106#ifdef LOG_NDELAY
107 options |= LOG_NDELAY;
108#endif
109
110#ifdef LOG_DAEMON
111 openlog("rsyncd", options, lp_syslog_facility());
112#else
113 openlog("rsyncd", options);
114#endif
115
116#ifndef LOG_NDELAY
4f6325c3 117 logit(LOG_INFO,"rsyncd started\n");
1a016bfd
AT
118#endif
119}
1a016bfd 120
15b84e14
DD
121void log_open()
122{
123 if (logfname && !logfile) {
124 extern int orig_umask;
125 int old_umask = umask(022 | orig_umask);
126 logfile = fopen(logfname, "a");
127 umask(old_umask);
128 }
129}
130
131void log_close()
45a83540
DD
132{
133 if (logfile) {
134 fclose(logfile);
135 logfile = NULL;
136 }
137}
138
554e0a8d
AT
139/* setup the error file descriptor - used when we are a server
140 that is receiving files */
141void set_error_fd(int fd)
142{
143 log_error_fd = fd;
144}
145
146/* this is the underlying (unformatted) rsync debugging function. Call
147 it with FINFO, FERROR or FLOG */
ff41a59f 148void rwrite(enum logcode code, char *buf, int len)
0b76cd63 149{
0b76cd63
AT
150 FILE *f=NULL;
151 extern int am_daemon;
4a814638 152 extern int am_server;
b86f0cef 153 extern int quiet;
8d9dc9f9 154 /* recursion can happen with certain fatal conditions */
8d9dc9f9 155
6d7b6081 156 if (quiet && code == FINFO) return;
b86f0cef 157
65417579 158 if (len < 0) exit_cleanup(RERR_MESSAGEIO);
0b76cd63 159
ff8b29b8
AT
160 buf[len] = 0;
161
ff41a59f 162 if (code == FLOG) {
11a5a3c7 163 if (am_daemon) logit(LOG_INFO, buf);
11a5a3c7
AT
164 return;
165 }
166
4a814638
AT
167 /* first try to pass it off the our sibling */
168 if (am_server && io_error_write(log_error_fd, code, buf, len)) {
6d7b6081
AT
169 return;
170 }
171
4a814638
AT
172 /* then try to pass it to the other end */
173 if (am_server && io_multiplex_write(code, buf, len)) {
6d7b6081
AT
174 return;
175 }
ff41a59f 176
ff8b29b8 177 if (am_daemon) {
b24203b3 178 static int depth;
ff8b29b8 179 int priority = LOG_INFO;
ff41a59f 180 if (code == FERROR) priority = LOG_WARNING;
45ccc5c0 181
b24203b3
AT
182 if (depth) return;
183
f27b53f5
AT
184 depth++;
185
45a83540 186 log_init();
6d7b6081 187 logit(priority, buf);
8d9dc9f9
AT
188
189 depth--;
ff8b29b8 190 return;
0b76cd63
AT
191 }
192
ff41a59f 193 if (code == FERROR) {
ff8b29b8
AT
194 f = stderr;
195 }
196
ff41a59f 197 if (code == FINFO) {
ff8b29b8
AT
198 if (am_server)
199 f = stderr;
200 else
201 f = stdout;
202 }
203
65417579 204 if (!f) exit_cleanup(RERR_MESSAGEIO);
0b76cd63 205
65417579 206 if (fwrite(buf, len, 1, f) != 1) exit_cleanup(RERR_MESSAGEIO);
8d9dc9f9 207
bcf5b133 208 if (buf[len-1] == '\r' || buf[len-1] == '\n') fflush(f);
0b76cd63 209}
554e0a8d
AT
210
211
a039749b
MP
212/* This is the rsync debugging function. Call it with FINFO, FERROR or
213 * FLOG. */
214void rprintf(enum logcode code, const char *format, ...)
554e0a8d
AT
215{
216 va_list ap;
217 char buf[1024];
218 int len;
219
220 va_start(ap, format);
221 len = vslprintf(buf, sizeof(buf), format, ap);
222 va_end(ap);
223
224 if (len > sizeof(buf)-1) exit_cleanup(RERR_MESSAGEIO);
225
ff41a59f 226 rwrite(code, buf, len);
554e0a8d 227}
0b76cd63 228
a039749b
MP
229
230/* This is like rprintf, but it also tries to print some
231 * representation of the error code. Normally errcode = errno.
232 *
233 * Unlike rprintf, this always adds a newline and there should not be
234 * one in the format string.
235 *
236 * Note that since strerror might involve dynamically loading a
237 * message catalog we need to call it once before chroot-ing. */
238void rsyserr(enum logcode code, int errcode, const char *format, ...)
239{
240 va_list ap;
241 char buf[1024];
242 int len, sys_len;
243 char *sysmsg;
244
245 va_start(ap, format);
246 len = vslprintf(buf, sizeof(buf), format, ap);
247 va_end(ap);
248
249 if (len > sizeof(buf)-1) exit_cleanup(RERR_MESSAGEIO);
250
251 sysmsg = strerror(errcode);
252 sys_len = strlen(sysmsg);
253 if (len + 3 + sys_len > sizeof(buf) - 1)
254 exit_cleanup(RERR_MESSAGEIO);
255
256 strcpy(buf + len, ": ");
257 len += 2;
258 strcpy(buf + len, sysmsg);
259 len += sys_len;
260 strcpy(buf + len, "\n");
261 len++;
262
263 rwrite(code, buf, len);
264}
265
266
267
ff41a59f 268void rflush(enum logcode code)
0b76cd63
AT
269{
270 FILE *f = NULL;
271 extern int am_daemon;
272
273 if (am_daemon) {
274 return;
275 }
276
ff41a59f 277 if (code == FLOG) {
e08bfe12
AT
278 return;
279 }
280
ff41a59f 281 if (code == FERROR) {
0b76cd63
AT
282 f = stderr;
283 }
284
ff41a59f 285 if (code == FINFO) {
0b76cd63
AT
286 extern int am_server;
287 if (am_server)
288 f = stderr;
289 else
290 f = stdout;
291 }
292
65417579 293 if (!f) exit_cleanup(RERR_MESSAGEIO);
0b76cd63
AT
294 fflush(f);
295}
296
11a5a3c7 297
e08bfe12
AT
298
299/* a generic logging routine for send/recv, with parameter
300 substitiution */
0f3203c3 301static void log_formatted(enum logcode code,
b6062654 302 char *format, char *op, struct file_struct *file,
1b7c47cb 303 struct stats *initial_stats)
e08bfe12
AT
304{
305 extern int module_id;
97cb8dc2 306 extern char *auth_user;
e08bfe12 307 char buf[1024];
ab7104da 308 char buf2[1024];
e08bfe12 309 char *p, *s, *n;
e08bfe12 310 int l;
1b7c47cb
AT
311 extern struct stats stats;
312 extern int am_sender;
af77cc6b 313 extern int am_daemon;
1b7c47cb 314 int64 b;
e08bfe12 315
37f9805d 316 strlcpy(buf, format, sizeof(buf));
e08bfe12
AT
317
318 for (s=&buf[0];
319 s && (p=strchr(s,'%')); ) {
320 n = NULL;
321 s = p + 1;
322
323 switch (p[1]) {
af77cc6b
AT
324 case 'h': if (am_daemon) n = client_name(0); break;
325 case 'a': if (am_daemon) n = client_addr(0); break;
e08bfe12 326 case 'l':
37f9805d 327 slprintf(buf2,sizeof(buf2),"%.0f",
e08bfe12
AT
328 (double)file->length);
329 n = buf2;
330 break;
331 case 'p':
37f9805d 332 slprintf(buf2,sizeof(buf2),"%d",
e08bfe12
AT
333 (int)getpid());
334 n = buf2;
335 break;
336 case 'o': n = op; break;
ab7104da 337 case 'f':
37f9805d 338 slprintf(buf2, sizeof(buf2), "%s/%s",
ab7104da
AT
339 file->basedir?file->basedir:"",
340 f_name(file));
341 clean_fname(buf2);
342 n = buf2;
b6062654 343 if (*n == '/') n++;
ab7104da 344 break;
97cb8dc2 345 case 'm': n = lp_name(module_id); break;
b6062654 346 case 't': n = timestring(time(NULL)); break;
97cb8dc2
AT
347 case 'P': n = lp_path(module_id); break;
348 case 'u': n = auth_user; break;
1b7c47cb
AT
349 case 'b':
350 if (am_sender) {
351 b = stats.total_written -
352 initial_stats->total_written;
353 } else {
354 b = stats.total_read -
355 initial_stats->total_read;
356 }
37f9805d 357 slprintf(buf2,sizeof(buf2),"%.0f", (double)b);
1b7c47cb
AT
358 n = buf2;
359 break;
360 case 'c':
361 if (!am_sender) {
362 b = stats.total_written -
363 initial_stats->total_written;
364 } else {
365 b = stats.total_read -
366 initial_stats->total_read;
367 }
37f9805d 368 slprintf(buf2,sizeof(buf2),"%.0f", (double)b);
1b7c47cb
AT
369 n = buf2;
370 break;
e08bfe12
AT
371 }
372
373 if (!n) continue;
374
375 l = strlen(n);
376
377 if ((l-1) + ((int)(s - &buf[0])) > sizeof(buf)) {
378 rprintf(FERROR,"buffer overflow expanding %%%c - exiting\n",
379 p[0]);
65417579 380 exit_cleanup(RERR_MESSAGEIO);
e08bfe12
AT
381 }
382
383 if (l != 2) {
34720097 384 memmove(s+(l-1), s+1, strlen(s+1)+1);
e08bfe12
AT
385 }
386 memcpy(p, n, l);
387
388 s = p+l;
389 }
390
0f3203c3 391 rprintf(code,"%s\n", buf);
e08bfe12
AT
392}
393
11a5a3c7 394/* log the outgoing transfer of a file */
1b7c47cb 395void log_send(struct file_struct *file, struct stats *initial_stats)
11a5a3c7
AT
396{
397 extern int module_id;
b6062654
AT
398 extern int am_server;
399 extern char *log_format;
400
11a5a3c7 401 if (lp_transfer_logging(module_id)) {
b6062654
AT
402 log_formatted(FLOG, lp_log_format(module_id), "send", file, initial_stats);
403 } else if (log_format && !am_server) {
404 log_formatted(FINFO, log_format, "send", file, initial_stats);
11a5a3c7
AT
405 }
406}
407
408/* log the incoming transfer of a file */
1b7c47cb 409void log_recv(struct file_struct *file, struct stats *initial_stats)
11a5a3c7
AT
410{
411 extern int module_id;
b6062654
AT
412 extern int am_server;
413 extern char *log_format;
414
11a5a3c7 415 if (lp_transfer_logging(module_id)) {
117af102 416 log_formatted(FLOG, lp_log_format(module_id), "recv", file, initial_stats);
b6062654 417 } else if (log_format && !am_server) {
117af102 418 log_formatted(FINFO, log_format, "recv", file, initial_stats);
11a5a3c7
AT
419 }
420}
421
af642a61
MP
422
423
424
425/*
426 * Called when the transfer is interrupted for some reason.
427 *
428 * Code is one of the RERR_* codes from errcode.h, or terminating
429 * successfully.
430 */
a9766ef1 431void log_exit(int code, const char *file, int line)
9b73d1c0
AT
432{
433 if (code == 0) {
434 extern struct stats stats;
67ea0d48 435 rprintf(FLOG,"wrote %.0f bytes read %.0f bytes total size %.0f\n",
9b73d1c0
AT
436 (double)stats.total_written,
437 (double)stats.total_read,
438 (double)stats.total_size);
439 } else {
af642a61
MP
440 const char *name;
441
442 name = rerr_name(code);
443 if (!name)
444 name = "unexplained error";
445
446 rprintf(FLOG,"transfer interrupted: %s (code %d) at %s(%d)\n",
447 name, code, file, line);
9b73d1c0
AT
448 }
449}
450
af642a61
MP
451
452
453
11a5a3c7 454/* log the incoming transfer of a file for interactive use, this
b6062654
AT
455 will be called at the end where the client was run
456
457 it i called when a file starts to be transferred
458*/
a9766ef1 459void log_transfer(struct file_struct *file, const char *fname)
11a5a3c7
AT
460{
461 extern int verbose;
462
463 if (!verbose) return;
464
465 rprintf(FINFO,"%s\n", fname);
466}
9b73d1c0 467