1 /* -*- c-file-style: "linux"; -*-
3 Copyright (C) 1998-2000 by Andrew Tridgell
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.
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.
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.
21 logging and utility functions
27 static char *logfname;
29 static int log_error_fd = -1;
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" },
55 * Map from rsync error code to name, or return NULL.
57 static char const *rerr_name(int code)
60 for (i = 0; rerr_names[i].name; i++) {
61 if (rerr_names[i].code == code)
62 return rerr_names[i].name;
68 static void logit(int priority, char *buf)
73 fprintf(logfile,"%s [%d] %s",
74 timestring(time(NULL)), (int)getpid(), buf);
77 syslog(priority, "%s", buf);
83 static int initialised;
84 int options = LOG_PID;
87 if (initialised) return;
90 /* this looks pointless, but it is needed in order for the
91 C library on some systems to fetch the timezone info
96 /* optionally use a log file instead of syslog */
97 logfname = lp_log_file();
107 options |= LOG_NDELAY;
111 openlog("rsyncd", options, lp_syslog_facility());
113 openlog("rsyncd", options);
117 logit(LOG_INFO,"rsyncd started\n");
123 if (logfname && !logfile) {
124 extern int orig_umask;
125 int old_umask = umask(022 | orig_umask);
126 logfile = fopen(logfname, "a");
139 /* setup the error file descriptor - used when we are a server
140 that is receiving files */
141 void set_error_fd(int fd)
146 /* this is the underlying (unformatted) rsync debugging function. Call
147 it with FINFO, FERROR or FLOG */
148 void rwrite(enum logcode code, char *buf, int len)
151 extern int am_daemon;
152 extern int am_server;
154 /* recursion can happen with certain fatal conditions */
156 if (quiet && code == FINFO) return;
158 if (len < 0) exit_cleanup(RERR_MESSAGEIO);
163 if (am_daemon) logit(LOG_INFO, buf);
167 /* first try to pass it off the our sibling */
168 if (am_server && io_error_write(log_error_fd, code, buf, len)) {
172 /* then try to pass it to the other end */
173 if (am_server && io_multiplex_write(code, buf, len)) {
179 int priority = LOG_INFO;
180 if (code == FERROR) priority = LOG_WARNING;
187 logit(priority, buf);
193 if (code == FERROR) {
204 if (!f) exit_cleanup(RERR_MESSAGEIO);
206 if (fwrite(buf, len, 1, f) != 1) exit_cleanup(RERR_MESSAGEIO);
208 if (buf[len-1] == '\r' || buf[len-1] == '\n') fflush(f);
212 /* This is the rsync debugging function. Call it with FINFO, FERROR or
214 void rprintf(enum logcode code, const char *format, ...)
220 va_start(ap, format);
221 len = vslprintf(buf, sizeof(buf), format, ap);
224 if (len > sizeof(buf)-1) exit_cleanup(RERR_MESSAGEIO);
226 rwrite(code, buf, len);
230 /* This is like rprintf, but it also tries to print some
231 * representation of the error code. Normally errcode = errno.
233 * Unlike rprintf, this always adds a newline and there should not be
234 * one in the format string.
236 * Note that since strerror might involve dynamically loading a
237 * message catalog we need to call it once before chroot-ing. */
238 void rsyserr(enum logcode code, int errcode, const char *format, ...)
245 va_start(ap, format);
246 len = vslprintf(buf, sizeof(buf), format, ap);
249 if (len > sizeof(buf)-1) exit_cleanup(RERR_MESSAGEIO);
251 sysmsg = strerror(errcode);
252 sys_len = strlen(sysmsg);
253 if (len + 3 + sys_len > sizeof(buf) - 1)
254 exit_cleanup(RERR_MESSAGEIO);
256 strcpy(buf + len, ": ");
258 strcpy(buf + len, sysmsg);
260 strcpy(buf + len, "\n");
263 rwrite(code, buf, len);
268 void rflush(enum logcode code)
271 extern int am_daemon;
281 if (code == FERROR) {
286 extern int am_server;
293 if (!f) exit_cleanup(RERR_MESSAGEIO);
299 /* a generic logging routine for send/recv, with parameter
301 static void log_formatted(enum logcode code,
302 char *format, char *op, struct file_struct *file,
303 struct stats *initial_stats)
305 extern int module_id;
306 extern char *auth_user;
311 extern struct stats stats;
312 extern int am_sender;
313 extern int am_daemon;
316 strlcpy(buf, format, sizeof(buf));
319 s && (p=strchr(s,'%')); ) {
324 case 'h': if (am_daemon) n = client_name(0); break;
325 case 'a': if (am_daemon) n = client_addr(0); break;
327 slprintf(buf2,sizeof(buf2),"%.0f",
328 (double)file->length);
332 slprintf(buf2,sizeof(buf2),"%d",
336 case 'o': n = op; break;
338 slprintf(buf2, sizeof(buf2), "%s/%s",
339 file->basedir?file->basedir:"",
345 case 'm': n = lp_name(module_id); break;
346 case 't': n = timestring(time(NULL)); break;
347 case 'P': n = lp_path(module_id); break;
348 case 'u': n = auth_user; break;
351 b = stats.total_written -
352 initial_stats->total_written;
354 b = stats.total_read -
355 initial_stats->total_read;
357 slprintf(buf2,sizeof(buf2),"%.0f", (double)b);
362 b = stats.total_written -
363 initial_stats->total_written;
365 b = stats.total_read -
366 initial_stats->total_read;
368 slprintf(buf2,sizeof(buf2),"%.0f", (double)b);
377 if ((l-1) + ((int)(s - &buf[0])) > sizeof(buf)) {
378 rprintf(FERROR,"buffer overflow expanding %%%c - exiting\n",
380 exit_cleanup(RERR_MESSAGEIO);
384 memmove(s+(l-1), s+1, strlen(s+1)+1);
391 rprintf(code,"%s\n", buf);
394 /* log the outgoing transfer of a file */
395 void log_send(struct file_struct *file, struct stats *initial_stats)
397 extern int module_id;
398 extern int am_server;
399 extern char *log_format;
401 if (lp_transfer_logging(module_id)) {
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);
408 /* log the incoming transfer of a file */
409 void log_recv(struct file_struct *file, struct stats *initial_stats)
411 extern int module_id;
412 extern int am_server;
413 extern char *log_format;
415 if (lp_transfer_logging(module_id)) {
416 log_formatted(FLOG, lp_log_format(module_id), "recv", file, initial_stats);
417 } else if (log_format && !am_server) {
418 log_formatted(FINFO, log_format, "recv", file, initial_stats);
426 * Called when the transfer is interrupted for some reason.
428 * Code is one of the RERR_* codes from errcode.h, or terminating
431 void log_exit(int code, const char *file, int line)
434 extern struct stats stats;
435 rprintf(FLOG,"wrote %.0f bytes read %.0f bytes total size %.0f\n",
436 (double)stats.total_written,
437 (double)stats.total_read,
438 (double)stats.total_size);
442 name = rerr_name(code);
444 name = "unexplained error";
446 rprintf(FLOG,"transfer interrupted: %s (code %d) at %s(%d)\n",
447 name, code, file, line);
454 /* log the incoming transfer of a file for interactive use, this
455 will be called at the end where the client was run
457 it i called when a file starts to be transferred
459 void log_transfer(struct file_struct *file, const char *fname)
463 if (!verbose) return;
465 rprintf(FINFO,"%s\n", fname);