1 /* -*- c-file-style: "linux"; -*-
3 Copyright (C) 1998-2001 by Andrew Tridgell <mbp@samba.org>
4 Copyright (C) 2000-2001 by Martin Pool <mbp@samba.org>
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.
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.
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.
22 Logging and utility functions.
24 Mapping to human-readable messages added by Martin Pool
25 <mbp@samba.org>, Oct 2000.
29 static char *logfname;
31 static int log_error_fd = -1;
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" },
57 * Map from rsync error code to name, or return NULL.
59 static char const *rerr_name(int code)
62 for (i = 0; rerr_names[i].name; i++) {
63 if (rerr_names[i].code == code)
64 return rerr_names[i].name;
70 static void logit(int priority, char *buf)
75 fprintf(logfile,"%s [%d] %s",
76 timestring(time(NULL)), (int)getpid(), buf);
79 syslog(priority, "%s", buf);
85 static int initialised;
86 int options = LOG_PID;
89 if (initialised) return;
92 /* this looks pointless, but it is needed in order for the
93 C library on some systems to fetch the timezone info
98 /* optionally use a log file instead of syslog */
99 logfname = lp_log_file();
109 options |= LOG_NDELAY;
113 openlog("rsyncd", options, lp_syslog_facility());
115 openlog("rsyncd", options);
119 logit(LOG_INFO,"rsyncd started\n");
125 if (logfname && !logfile) {
126 extern int orig_umask;
127 int old_umask = umask(022 | orig_umask);
128 logfile = fopen(logfname, "a");
141 /* setup the error file descriptor - used when we are a server
142 that is receiving files */
143 void set_error_fd(int fd)
148 /* this is the underlying (unformatted) rsync debugging function. Call
149 it with FINFO, FERROR or FLOG */
150 void rwrite(enum logcode code, char *buf, int len)
153 extern int am_daemon;
154 extern int am_server;
156 /* recursion can happen with certain fatal conditions */
158 if (quiet && code == FINFO) return;
160 if (len < 0) exit_cleanup(RERR_MESSAGEIO);
165 if (am_daemon) logit(LOG_INFO, buf);
169 /* first try to pass it off the our sibling */
170 if (am_server && io_error_write(log_error_fd, code, buf, len)) {
174 /* if that fails, try to pass it to the other end */
175 if (am_server && io_multiplex_write(code, buf, len)) {
181 int priority = LOG_INFO;
182 if (code == FERROR) priority = LOG_WARNING;
189 logit(priority, buf);
195 if (code == FERROR) {
206 if (!f) exit_cleanup(RERR_MESSAGEIO);
208 if (fwrite(buf, len, 1, f) != 1) exit_cleanup(RERR_MESSAGEIO);
210 if (buf[len-1] == '\r' || buf[len-1] == '\n') fflush(f);
214 /* This is the rsync debugging function. Call it with FINFO, FERROR or
216 void rprintf(enum logcode code, const char *format, ...)
222 va_start(ap, format);
223 len = vslprintf(buf, sizeof(buf), format, ap);
226 if (len > sizeof(buf)-1) exit_cleanup(RERR_MESSAGEIO);
228 rwrite(code, buf, len);
232 /* This is like rprintf, but it also tries to print some
233 * representation of the error code. Normally errcode = errno.
235 * Unlike rprintf, this always adds a newline and there should not be
236 * one in the format string.
238 * Note that since strerror might involve dynamically loading a
239 * message catalog we need to call it once before chroot-ing. */
240 void rsyserr(enum logcode code, int errcode, const char *format, ...)
247 va_start(ap, format);
248 len = vslprintf(buf, sizeof(buf), format, ap);
251 if (len > sizeof(buf)-1) exit_cleanup(RERR_MESSAGEIO);
253 sysmsg = strerror(errcode);
254 sys_len = strlen(sysmsg);
255 if (len + 3 + sys_len > sizeof(buf) - 1)
256 exit_cleanup(RERR_MESSAGEIO);
258 strcpy(buf + len, ": ");
260 strcpy(buf + len, sysmsg);
262 strcpy(buf + len, "\n");
265 rwrite(code, buf, len);
270 void rflush(enum logcode code)
273 extern int am_daemon;
283 if (code == FERROR) {
288 extern int am_server;
295 if (!f) exit_cleanup(RERR_MESSAGEIO);
301 /* a generic logging routine for send/recv, with parameter
303 static void log_formatted(enum logcode code,
304 char *format, char *op, struct file_struct *file,
305 struct stats *initial_stats)
307 extern int module_id;
308 extern char *auth_user;
313 extern struct stats stats;
314 extern int am_sender;
315 extern int am_daemon;
318 strlcpy(buf, format, sizeof(buf));
321 s && (p=strchr(s,'%')); ) {
326 case 'h': if (am_daemon) n = client_name(0); break;
327 case 'a': if (am_daemon) n = client_addr(0); break;
329 slprintf(buf2,sizeof(buf2),"%.0f",
330 (double)file->length);
334 slprintf(buf2,sizeof(buf2),"%d",
338 case 'o': n = op; break;
340 slprintf(buf2, sizeof(buf2), "%s/%s",
341 file->basedir?file->basedir:"",
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;
353 b = stats.total_written -
354 initial_stats->total_written;
356 b = stats.total_read -
357 initial_stats->total_read;
359 slprintf(buf2,sizeof(buf2),"%.0f", (double)b);
364 b = stats.total_written -
365 initial_stats->total_written;
367 b = stats.total_read -
368 initial_stats->total_read;
370 slprintf(buf2,sizeof(buf2),"%.0f", (double)b);
379 if ((l-1) + ((int)(s - &buf[0])) > sizeof(buf)) {
380 rprintf(FERROR,"buffer overflow expanding %%%c - exiting\n",
382 exit_cleanup(RERR_MESSAGEIO);
386 memmove(s+(l-1), s+1, strlen(s+1)+1);
393 rprintf(code,"%s\n", buf);
396 /* log the outgoing transfer of a file */
397 void log_send(struct file_struct *file, struct stats *initial_stats)
399 extern int module_id;
400 extern int am_server;
401 extern char *log_format;
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);
410 /* log the incoming transfer of a file */
411 void log_recv(struct file_struct *file, struct stats *initial_stats)
413 extern int module_id;
414 extern int am_server;
415 extern char *log_format;
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);
428 * Called when the transfer is interrupted for some reason.
430 * Code is one of the RERR_* codes from errcode.h, or terminating
433 void log_exit(int code, const char *file, int line)
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);
444 name = rerr_name(code);
446 name = "unexplained error";
448 rprintf(FLOG,"transfer interrupted: %s (code %d) at %s(%d)\n",
449 name, code, file, line);
456 /* log the incoming transfer of a file for interactive use, this
457 will be called at the end where the client was run
459 it i called when a file starts to be transferred
461 void log_transfer(struct file_struct *file, const char *fname)
465 if (!verbose) return;
467 rprintf(FINFO,"%s\n", fname);