More accurately, the uid/gid is set to -2, not "nobody".
[rsync/rsync.git] / log.c
... / ...
CommitLineData
1/* -*- c-file-style: "linux"; -*-
2
3 Copyright (C) 1998-2001 by Andrew Tridgell <mbp@samba.org>
4 Copyright (C) 2000-2001 by Martin Pool <mbp@samba.org>
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/*
22 Logging and utility functions.
23
24 Mapping to human-readable messages added by Martin Pool
25 <mbp@samba.org>, Oct 2000.
26 */
27#include "rsync.h"
28
29static char *logfname;
30static FILE *logfile;
31static int log_error_fd = -1;
32
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" },
47 { RERR_SIGNAL , "received SIGUSR1 or SIGINT" },
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
70static void logit(int priority, char *buf)
71{
72 if (logfname) {
73 if (!logfile)
74 log_open();
75 fprintf(logfile,"%s [%d] %s",
76 timestring(time(NULL)), (int)getpid(), buf);
77 fflush(logfile);
78 } else {
79 syslog(priority, "%s", buf);
80 }
81}
82
83void log_init(void)
84{
85 static int initialised;
86 int options = LOG_PID;
87 time_t t;
88
89 if (initialised) return;
90 initialised = 1;
91
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 */
99 logfname = lp_log_file();
100 if (logfname) {
101 if (*logfname) {
102 log_open();
103 return;
104 }
105 logfname = NULL;
106 }
107
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
119 logit(LOG_INFO,"rsyncd started\n");
120#endif
121}
122
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()
134{
135 if (logfile) {
136 fclose(logfile);
137 logfile = NULL;
138 }
139}
140
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 */
150void rwrite(enum logcode code, char *buf, int len)
151{
152 FILE *f=NULL;
153 extern int am_daemon;
154 extern int am_server;
155 extern int quiet;
156 /* recursion can happen with certain fatal conditions */
157
158 if (quiet && code == FINFO) return;
159
160 if (len < 0) exit_cleanup(RERR_MESSAGEIO);
161
162 buf[len] = 0;
163
164 if (code == FLOG) {
165 if (am_daemon) logit(LOG_INFO, buf);
166 return;
167 }
168
169 /* first try to pass it off the our sibling */
170 if (am_server && io_error_write(log_error_fd, code, buf, len)) {
171 return;
172 }
173
174 /* if that fails, try to pass it to the other end */
175 if (am_server && io_multiplex_write(code, buf, len)) {
176 return;
177 }
178
179 if (am_daemon) {
180 static int depth;
181 int priority = LOG_INFO;
182 if (code == FERROR) priority = LOG_WARNING;
183
184 if (depth) return;
185
186 depth++;
187
188 log_init();
189 logit(priority, buf);
190
191 depth--;
192 return;
193 }
194
195 if (code == FERROR) {
196 f = stderr;
197 }
198
199 if (code == FINFO) {
200 if (am_server)
201 f = stderr;
202 else
203 f = stdout;
204 }
205
206 if (!f) exit_cleanup(RERR_MESSAGEIO);
207
208 if (fwrite(buf, len, 1, f) != 1) exit_cleanup(RERR_MESSAGEIO);
209
210 if (buf[len-1] == '\r' || buf[len-1] == '\n') fflush(f);
211}
212
213
214/* This is the rsync debugging function. Call it with FINFO, FERROR or
215 * FLOG. */
216void rprintf(enum logcode code, const char *format, ...)
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
228 rwrite(code, buf, len);
229}
230
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
270void rflush(enum logcode code)
271{
272 FILE *f = NULL;
273 extern int am_daemon;
274
275 if (am_daemon) {
276 return;
277 }
278
279 if (code == FLOG) {
280 return;
281 }
282
283 if (code == FERROR) {
284 f = stderr;
285 }
286
287 if (code == FINFO) {
288 extern int am_server;
289 if (am_server)
290 f = stderr;
291 else
292 f = stdout;
293 }
294
295 if (!f) exit_cleanup(RERR_MESSAGEIO);
296 fflush(f);
297}
298
299
300
301/* a generic logging routine for send/recv, with parameter
302 substitiution */
303static void log_formatted(enum logcode code,
304 char *format, char *op, struct file_struct *file,
305 struct stats *initial_stats)
306{
307 extern int module_id;
308 extern char *auth_user;
309 char buf[1024];
310 char buf2[1024];
311 char *p, *s, *n;
312 int l;
313 extern struct stats stats;
314 extern int am_sender;
315 extern int am_daemon;
316 int64 b;
317
318 strlcpy(buf, format, sizeof(buf));
319
320 for (s=&buf[0];
321 s && (p=strchr(s,'%')); ) {
322 n = NULL;
323 s = p + 1;
324
325 switch (p[1]) {
326 case 'h': if (am_daemon) n = client_name(0); break;
327 case 'a': if (am_daemon) n = client_addr(0); break;
328 case 'l':
329 slprintf(buf2,sizeof(buf2),"%.0f",
330 (double)file->length);
331 n = buf2;
332 break;
333 case 'p':
334 slprintf(buf2,sizeof(buf2),"%d",
335 (int)getpid());
336 n = buf2;
337 break;
338 case 'o': n = op; break;
339 case 'f':
340 slprintf(buf2, sizeof(buf2), "%s/%s",
341 file->basedir?file->basedir:"",
342 f_name(file));
343 clean_fname(buf2);
344 n = buf2;
345 if (*n == '/') n++;
346 break;
347 case 'm': n = lp_name(module_id); break;
348 case 't': n = timestring(time(NULL)); break;
349 case 'P': n = lp_path(module_id); break;
350 case 'u': n = auth_user; break;
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 }
359 slprintf(buf2,sizeof(buf2),"%.0f", (double)b);
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 }
370 slprintf(buf2,sizeof(buf2),"%.0f", (double)b);
371 n = buf2;
372 break;
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]);
382 exit_cleanup(RERR_MESSAGEIO);
383 }
384
385 if (l != 2) {
386 memmove(s+(l-1), s+1, strlen(s+1)+1);
387 }
388 memcpy(p, n, l);
389
390 s = p+l;
391 }
392
393 rprintf(code,"%s\n", buf);
394}
395
396/* log the outgoing transfer of a file */
397void log_send(struct file_struct *file, struct stats *initial_stats)
398{
399 extern int module_id;
400 extern int am_server;
401 extern char *log_format;
402
403 if (lp_transfer_logging(module_id)) {
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);
407 }
408}
409
410/* log the incoming transfer of a file */
411void log_recv(struct file_struct *file, struct stats *initial_stats)
412{
413 extern int module_id;
414 extern int am_server;
415 extern char *log_format;
416
417 if (lp_transfer_logging(module_id)) {
418 log_formatted(FLOG, lp_log_format(module_id), "recv", file, initial_stats);
419 } else if (log_format && !am_server) {
420 log_formatted(FINFO, log_format, "recv", file, initial_stats);
421 }
422}
423
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 */
433void log_exit(int code, const char *file, int line)
434{
435 if (code == 0) {
436 extern struct stats stats;
437 rprintf(FLOG,"wrote %.0f bytes read %.0f bytes total size %.0f\n",
438 (double)stats.total_written,
439 (double)stats.total_read,
440 (double)stats.total_size);
441 } else {
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);
450 }
451}
452
453
454
455
456/* log the incoming transfer of a file for interactive use, this
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*/
461void log_transfer(struct file_struct *file, const char *fname)
462{
463 extern int verbose;
464
465 if (!verbose) return;
466
467 rprintf(FINFO,"%s\n", fname);
468}
469