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