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;
39 } const rerr_names[] = {
40 { RERR_SYNTAX , "syntax or usage error" },
41 { RERR_PROTOCOL , "protocol incompatibility" },
42 { RERR_FILESELECT , "errors selecting input/output files, dirs" },
43 { RERR_UNSUPPORTED , "requested action not supported" },
44 { RERR_SOCKETIO , "error in socket IO" },
45 { RERR_FILEIO , "error in file IO" },
46 { RERR_STREAMIO , "error in rsync protocol data stream" },
47 { RERR_MESSAGEIO , "errors with program diagnostics" },
48 { RERR_IPC , "error in IPC code" },
49 { RERR_SIGNAL , "received SIGUSR1 or SIGINT" },
50 { RERR_WAITCHILD , "some error returned by waitpid()" },
51 { RERR_MALLOC , "error allocating core memory buffers" },
52 { RERR_TIMEOUT , "timeout in data send/receive" },
59 * Map from rsync error code to name, or return NULL.
61 static char const *rerr_name(int code)
64 for (i = 0; rerr_names[i].name; i++) {
65 if (rerr_names[i].code == code)
66 return rerr_names[i].name;
72 struct err_list *next;
75 int written; /* how many bytes we have written so far */
78 static struct err_list *err_list_head;
79 static struct err_list *err_list_tail;
81 /* add an error message to the pending error list */
82 static void err_list_add(int code, char *buf, int len)
85 el = (struct err_list *)malloc(sizeof(*el));
86 if (!el) exit_cleanup(RERR_MALLOC);
88 el->buf = malloc(len+4);
89 if (!el->buf) exit_cleanup(RERR_MALLOC);
90 memcpy(el->buf+4, buf, len);
91 SIVAL(el->buf, 0, ((code+MPLEX_BASE)<<24) | len);
95 err_list_tail->next = el;
103 /* try to push errors off the error list onto the wire */
104 void err_list_push(void)
106 if (log_error_fd == -1) return;
108 while (err_list_head) {
109 struct err_list *el = err_list_head;
110 int n = write(log_error_fd, el->buf+el->written, el->len - el->written);
111 /* don't check for an error if the best way of handling the error is
117 if (el->written == el->len) {
119 err_list_head = el->next;
120 if (!err_list_head) err_list_tail = NULL;
127 static void logit(int priority, char *buf)
132 fprintf(logfile,"%s [%d] %s",
133 timestring(time(NULL)), (int)getpid(), buf);
136 syslog(priority, "%s", buf);
142 static int initialised;
143 int options = LOG_PID;
146 if (initialised) return;
149 /* this looks pointless, but it is needed in order for the
150 C library on some systems to fetch the timezone info
155 /* optionally use a log file instead of syslog */
156 logfname = lp_log_file();
166 options |= LOG_NDELAY;
170 openlog("rsyncd", options, lp_syslog_facility());
172 openlog("rsyncd", options);
176 logit(LOG_INFO,"rsyncd started\n");
182 if (logfname && !logfile) {
183 extern int orig_umask;
184 int old_umask = umask(022 | orig_umask);
185 logfile = fopen(logfname, "a");
198 /* setup the error file descriptor - used when we are a server
199 that is receiving files */
200 void set_error_fd(int fd)
203 set_nonblocking(log_error_fd);
206 /* this is the underlying (unformatted) rsync debugging function. Call
207 it with FINFO, FERROR or FLOG */
208 void rwrite(enum logcode code, char *buf, int len)
211 extern int am_daemon;
212 extern int am_server;
214 /* recursion can happen with certain fatal conditions */
216 if (quiet && code == FINFO) return;
218 if (len < 0) exit_cleanup(RERR_MESSAGEIO);
223 if (am_daemon) logit(LOG_INFO, buf);
227 /* first try to pass it off to our sibling */
228 if (am_server && log_error_fd != -1) {
229 err_list_add(code, buf, len);
234 /* if that fails, try to pass it to the other end */
235 if (am_server && io_multiplex_write(code, buf, len)) {
241 int priority = LOG_INFO;
242 if (code == FERROR) priority = LOG_WARNING;
249 logit(priority, buf);
255 if (code == FERROR) {
267 if (!f) exit_cleanup(RERR_MESSAGEIO);
269 if (fwrite(buf, len, 1, f) != 1) exit_cleanup(RERR_MESSAGEIO);
271 if (buf[len-1] == '\r' || buf[len-1] == '\n') fflush(f);
275 /* This is the rsync debugging function. Call it with FINFO, FERROR or
277 void rprintf(enum logcode code, const char *format, ...)
283 va_start(ap, format);
284 len = vslprintf(buf, sizeof(buf), format, ap);
287 if (len > sizeof(buf)-1) exit_cleanup(RERR_MESSAGEIO);
289 rwrite(code, buf, len);
293 /* This is like rprintf, but it also tries to print some
294 * representation of the error code. Normally errcode = errno.
296 * Unlike rprintf, this always adds a newline and there should not be
297 * one in the format string.
299 * Note that since strerror might involve dynamically loading a
300 * message catalog we need to call it once before chroot-ing. */
301 void rsyserr(enum logcode code, int errcode, const char *format, ...)
308 va_start(ap, format);
309 len = vslprintf(buf, sizeof(buf), format, ap);
312 if (len > sizeof(buf)-1) exit_cleanup(RERR_MESSAGEIO);
314 sysmsg = strerror(errcode);
315 sys_len = strlen(sysmsg);
316 if (len + 3 + sys_len > sizeof(buf) - 1)
317 exit_cleanup(RERR_MESSAGEIO);
319 strcpy(buf + len, ": ");
321 strcpy(buf + len, sysmsg);
323 strcpy(buf + len, "\n");
326 rwrite(code, buf, len);
331 void rflush(enum logcode code)
334 extern int am_daemon;
344 if (code == FERROR) {
349 extern int am_server;
356 if (!f) exit_cleanup(RERR_MESSAGEIO);
362 /* a generic logging routine for send/recv, with parameter
364 static void log_formatted(enum logcode code,
365 char *format, char *op, struct file_struct *file,
366 struct stats *initial_stats)
368 extern int module_id;
369 extern char *auth_user;
374 extern struct stats stats;
375 extern int am_sender;
376 extern int am_daemon;
379 strlcpy(buf, format, sizeof(buf));
382 s && (p=strchr(s,'%')); ) {
387 case 'h': if (am_daemon) n = client_name(0); break;
388 case 'a': if (am_daemon) n = client_addr(0); break;
390 slprintf(buf2,sizeof(buf2),"%.0f",
391 (double)file->length);
395 slprintf(buf2,sizeof(buf2),"%d",
399 case 'o': n = op; break;
401 slprintf(buf2, sizeof(buf2), "%s/%s",
402 file->basedir?file->basedir:"",
408 case 'm': n = lp_name(module_id); break;
409 case 't': n = timestring(time(NULL)); break;
410 case 'P': n = lp_path(module_id); break;
411 case 'u': n = auth_user; break;
414 b = stats.total_written -
415 initial_stats->total_written;
417 b = stats.total_read -
418 initial_stats->total_read;
420 slprintf(buf2,sizeof(buf2),"%.0f", (double)b);
425 b = stats.total_written -
426 initial_stats->total_written;
428 b = stats.total_read -
429 initial_stats->total_read;
431 slprintf(buf2,sizeof(buf2),"%.0f", (double)b);
440 if ((l-1) + ((int)(s - &buf[0])) > sizeof(buf)) {
441 rprintf(FERROR,"buffer overflow expanding %%%c - exiting\n",
443 exit_cleanup(RERR_MESSAGEIO);
447 memmove(s+(l-1), s+1, strlen(s+1)+1);
454 rprintf(code,"%s\n", buf);
457 /* log the outgoing transfer of a file */
458 void log_send(struct file_struct *file, struct stats *initial_stats)
460 extern int module_id;
461 extern int am_server;
462 extern char *log_format;
464 if (lp_transfer_logging(module_id)) {
465 log_formatted(FLOG, lp_log_format(module_id), "send", file, initial_stats);
466 } else if (log_format && !am_server) {
467 log_formatted(FINFO, log_format, "send", file, initial_stats);
471 /* log the incoming transfer of a file */
472 void log_recv(struct file_struct *file, struct stats *initial_stats)
474 extern int module_id;
475 extern int am_server;
476 extern char *log_format;
478 if (lp_transfer_logging(module_id)) {
479 log_formatted(FLOG, lp_log_format(module_id), "recv", file, initial_stats);
480 } else if (log_format && !am_server) {
481 log_formatted(FINFO, log_format, "recv", file, initial_stats);
489 * Called when the transfer is interrupted for some reason.
491 * Code is one of the RERR_* codes from errcode.h, or terminating
494 void log_exit(int code, const char *file, int line)
497 extern struct stats stats;
498 rprintf(FLOG,"wrote %.0f bytes read %.0f bytes total size %.0f\n",
499 (double)stats.total_written,
500 (double)stats.total_read,
501 (double)stats.total_size);
505 name = rerr_name(code);
507 name = "unexplained error";
509 rprintf(FLOG,"transfer interrupted: %s (code %d) at %s(%d)\n",
510 name, code, file, line);
517 /* log the incoming transfer of a file for interactive use, this
518 will be called at the end where the client was run
520 it i called when a file starts to be transferred
522 void log_transfer(struct file_struct *file, const char *fname)
526 if (!verbose) return;
528 rprintf(FINFO,"%s\n", fname);