1 /* -*- c-file-style: "linux"; -*-
3 Copyright (C) 1998-2001 by Andrew Tridgell <tridge@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.
25 Mapping to human-readable messages added by Martin Pool
26 <mbp@samba.org>, Oct 2000.
30 static char *logfname;
32 static int log_error_fd = -1;
38 } const rerr_names[] = {
39 { RERR_SYNTAX , "syntax or usage error" },
40 { RERR_PROTOCOL , "protocol incompatibility" },
41 { RERR_FILESELECT , "errors selecting input/output files, dirs" },
42 { RERR_UNSUPPORTED , "requested action not supported" },
43 { RERR_SOCKETIO , "error in socket IO" },
44 { RERR_FILEIO , "error in file IO" },
45 { RERR_STREAMIO , "error in rsync protocol data stream" },
46 { RERR_MESSAGEIO , "errors with program diagnostics" },
47 { RERR_IPC , "error in IPC code" },
48 { RERR_SIGNAL , "received SIGUSR1 or SIGINT" },
49 { RERR_WAITCHILD , "some error returned by waitpid()" },
50 { RERR_MALLOC , "error allocating core memory buffers" },
51 { RERR_TIMEOUT , "timeout in data send/receive" },
58 * Map from rsync error code to name, or return NULL.
60 static char const *rerr_name(int code)
63 for (i = 0; rerr_names[i].name; i++) {
64 if (rerr_names[i].code == code)
65 return rerr_names[i].name;
71 static void logit(int priority, char *buf)
76 fprintf(logfile,"%s [%d] %s",
77 timestring(time(NULL)), (int)getpid(), buf);
80 syslog(priority, "%s", buf);
86 static int initialised;
87 int options = LOG_PID;
90 if (initialised) return;
93 /* this looks pointless, but it is needed in order for the
94 C library on some systems to fetch the timezone info
99 /* optionally use a log file instead of syslog */
100 logfname = lp_log_file();
110 options |= LOG_NDELAY;
114 openlog("rsyncd", options, lp_syslog_facility());
116 openlog("rsyncd", options);
120 logit(LOG_INFO,"rsyncd started\n");
126 if (logfname && !logfile) {
127 extern int orig_umask;
128 int old_umask = umask(022 | orig_umask);
129 logfile = fopen(logfname, "a");
142 /* setup the error file descriptor - used when we are a server
143 that is receiving files */
144 void set_error_fd(int fd)
149 /* this is the underlying (unformatted) rsync debugging function. Call
150 it with FINFO, FERROR or FLOG */
151 void rwrite(enum logcode code, char *buf, int len)
154 extern int am_daemon;
155 extern int am_server;
157 /* recursion can happen with certain fatal conditions */
159 if (quiet && code == FINFO) return;
161 if (len < 0) exit_cleanup(RERR_MESSAGEIO);
166 if (am_daemon) logit(LOG_INFO, buf);
170 /* first try to pass it off the our sibling */
171 if (am_server && io_error_write(log_error_fd, code, buf, len)) {
175 /* if that fails, try to pass it to the other end */
176 if (am_server && io_multiplex_write(code, buf, len)) {
182 int priority = LOG_INFO;
183 if (code == FERROR) priority = LOG_WARNING;
190 logit(priority, buf);
196 if (code == FERROR) {
207 if (!f) exit_cleanup(RERR_MESSAGEIO);
209 if (fwrite(buf, len, 1, f) != 1) exit_cleanup(RERR_MESSAGEIO);
211 if (buf[len-1] == '\r' || buf[len-1] == '\n') fflush(f);
215 /* This is the rsync debugging function. Call it with FINFO, FERROR or
217 void rprintf(enum logcode code, const char *format, ...)
223 va_start(ap, format);
224 len = vslprintf(buf, sizeof(buf), format, ap);
227 if (len > sizeof(buf)-1) exit_cleanup(RERR_MESSAGEIO);
229 rwrite(code, buf, len);
233 /* This is like rprintf, but it also tries to print some
234 * representation of the error code. Normally errcode = errno.
236 * Unlike rprintf, this always adds a newline and there should not be
237 * one in the format string.
239 * Note that since strerror might involve dynamically loading a
240 * message catalog we need to call it once before chroot-ing. */
241 void rsyserr(enum logcode code, int errcode, const char *format, ...)
248 va_start(ap, format);
249 len = vslprintf(buf, sizeof(buf), format, ap);
252 if (len > sizeof(buf)-1) exit_cleanup(RERR_MESSAGEIO);
254 sysmsg = strerror(errcode);
255 sys_len = strlen(sysmsg);
256 if (len + 3 + sys_len > sizeof(buf) - 1)
257 exit_cleanup(RERR_MESSAGEIO);
259 strcpy(buf + len, ": ");
261 strcpy(buf + len, sysmsg);
263 strcpy(buf + len, "\n");
266 rwrite(code, buf, len);
271 void rflush(enum logcode code)
274 extern int am_daemon;
284 if (code == FERROR) {
289 extern int am_server;
296 if (!f) exit_cleanup(RERR_MESSAGEIO);
302 /* a generic logging routine for send/recv, with parameter
304 static void log_formatted(enum logcode code,
305 char *format, char *op, struct file_struct *file,
306 struct stats *initial_stats)
308 extern int module_id;
309 extern char *auth_user;
314 extern struct stats stats;
315 extern int am_sender;
316 extern int am_daemon;
319 strlcpy(buf, format, sizeof(buf));
322 s && (p=strchr(s,'%')); ) {
327 case 'h': if (am_daemon) n = client_name(0); break;
328 case 'a': if (am_daemon) n = client_addr(0); break;
330 slprintf(buf2,sizeof(buf2),"%.0f",
331 (double)file->length);
335 slprintf(buf2,sizeof(buf2),"%d",
339 case 'o': n = op; break;
341 slprintf(buf2, sizeof(buf2), "%s/%s",
342 file->basedir?file->basedir:"",
348 case 'm': n = lp_name(module_id); break;
349 case 't': n = timestring(time(NULL)); break;
350 case 'P': n = lp_path(module_id); break;
351 case 'u': n = auth_user; break;
354 b = stats.total_written -
355 initial_stats->total_written;
357 b = stats.total_read -
358 initial_stats->total_read;
360 slprintf(buf2,sizeof(buf2),"%.0f", (double)b);
365 b = stats.total_written -
366 initial_stats->total_written;
368 b = stats.total_read -
369 initial_stats->total_read;
371 slprintf(buf2,sizeof(buf2),"%.0f", (double)b);
380 if ((l-1) + ((int)(s - &buf[0])) > sizeof(buf)) {
381 rprintf(FERROR,"buffer overflow expanding %%%c - exiting\n",
383 exit_cleanup(RERR_MESSAGEIO);
387 memmove(s+(l-1), s+1, strlen(s+1)+1);
394 rprintf(code,"%s\n", buf);
397 /* log the outgoing transfer of a file */
398 void log_send(struct file_struct *file, struct stats *initial_stats)
400 extern int module_id;
401 extern int am_server;
402 extern char *log_format;
404 if (lp_transfer_logging(module_id)) {
405 log_formatted(FLOG, lp_log_format(module_id), "send", file, initial_stats);
406 } else if (log_format && !am_server) {
407 log_formatted(FINFO, log_format, "send", file, initial_stats);
411 /* log the incoming transfer of a file */
412 void log_recv(struct file_struct *file, struct stats *initial_stats)
414 extern int module_id;
415 extern int am_server;
416 extern char *log_format;
418 if (lp_transfer_logging(module_id)) {
419 log_formatted(FLOG, lp_log_format(module_id), "recv", file, initial_stats);
420 } else if (log_format && !am_server) {
421 log_formatted(FINFO, log_format, "recv", file, initial_stats);
429 * Called when the transfer is interrupted for some reason.
431 * Code is one of the RERR_* codes from errcode.h, or terminating
434 void log_exit(int code, const char *file, int line)
437 extern struct stats stats;
438 rprintf(FLOG,"wrote %.0f bytes read %.0f bytes total size %.0f\n",
439 (double)stats.total_written,
440 (double)stats.total_read,
441 (double)stats.total_size);
445 name = rerr_name(code);
447 name = "unexplained error";
449 rprintf(FLOG,"transfer interrupted: %s (code %d) at %s(%d)\n",
450 name, code, file, line);
457 /* log the incoming transfer of a file for interactive use, this
458 will be called at the end where the client was run
460 it i called when a file starts to be transferred
462 void log_transfer(struct file_struct *file, const char *fname)
466 if (!verbose) return;
468 rprintf(FINFO,"%s\n", fname);