1 /* -*- c-file-style: "linux"; -*-
3 Copyright (C) 1998-2001 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.
23 Mapping to human-readable messages added by Martin Pool
24 <mbp@samba.org>, Oct 2000.
28 static char *logfname;
30 static int log_error_fd = -1;
36 } const rerr_names[] = {
37 { RERR_SYNTAX , "syntax or usage error" },
38 { RERR_PROTOCOL , "protocol incompatibility" },
39 { RERR_FILESELECT , "errors selecting input/output files, dirs" },
40 { RERR_UNSUPPORTED , "requested action not supported" },
41 { RERR_SOCKETIO , "error in socket IO" },
42 { RERR_FILEIO , "error in file IO" },
43 { RERR_STREAMIO , "error in rsync protocol data stream" },
44 { RERR_MESSAGEIO , "errors with program diagnostics" },
45 { RERR_IPC , "error in IPC code" },
46 { RERR_SIGNAL , "received SIGUSR1 or SIGINT" },
47 { RERR_WAITCHILD , "some error returned by waitpid()" },
48 { RERR_MALLOC , "error allocating core memory buffers" },
49 { RERR_TIMEOUT , "timeout in data send/receive" },
56 * Map from rsync error code to name, or return NULL.
58 static char const *rerr_name(int code)
61 for (i = 0; rerr_names[i].name; i++) {
62 if (rerr_names[i].code == code)
63 return rerr_names[i].name;
69 static void logit(int priority, char *buf)
74 fprintf(logfile,"%s [%d] %s",
75 timestring(time(NULL)), (int)getpid(), buf);
78 syslog(priority, "%s", buf);
84 static int initialised;
85 int options = LOG_PID;
88 if (initialised) return;
91 /* this looks pointless, but it is needed in order for the
92 C library on some systems to fetch the timezone info
97 /* optionally use a log file instead of syslog */
98 logfname = lp_log_file();
108 options |= LOG_NDELAY;
112 openlog("rsyncd", options, lp_syslog_facility());
114 openlog("rsyncd", options);
118 logit(LOG_INFO,"rsyncd started\n");
124 if (logfname && !logfile) {
125 extern int orig_umask;
126 int old_umask = umask(022 | orig_umask);
127 logfile = fopen(logfname, "a");
140 /* setup the error file descriptor - used when we are a server
141 that is receiving files */
142 void set_error_fd(int fd)
147 /* this is the underlying (unformatted) rsync debugging function. Call
148 it with FINFO, FERROR or FLOG */
149 void rwrite(enum logcode code, char *buf, int len)
152 extern int am_daemon;
153 extern int am_server;
155 /* recursion can happen with certain fatal conditions */
157 if (quiet && code == FINFO) return;
159 if (len < 0) exit_cleanup(RERR_MESSAGEIO);
164 if (am_daemon) logit(LOG_INFO, buf);
168 /* first try to pass it off the our sibling */
169 if (am_server && io_error_write(log_error_fd, code, buf, len)) {
173 /* then try to pass it to the other end */
174 if (am_server && io_multiplex_write(code, buf, len)) {
180 int priority = LOG_INFO;
181 if (code == FERROR) priority = LOG_WARNING;
188 logit(priority, buf);
194 if (code == FERROR) {
205 if (!f) exit_cleanup(RERR_MESSAGEIO);
207 if (fwrite(buf, len, 1, f) != 1) exit_cleanup(RERR_MESSAGEIO);
209 if (buf[len-1] == '\r' || buf[len-1] == '\n') fflush(f);
213 /* This is the rsync debugging function. Call it with FINFO, FERROR or
215 void rprintf(enum logcode code, const char *format, ...)
221 va_start(ap, format);
222 len = vslprintf(buf, sizeof(buf), format, ap);
225 if (len > sizeof(buf)-1) exit_cleanup(RERR_MESSAGEIO);
227 rwrite(code, buf, len);
231 /* This is like rprintf, but it also tries to print some
232 * representation of the error code. Normally errcode = errno.
234 * Unlike rprintf, this always adds a newline and there should not be
235 * one in the format string.
237 * Note that since strerror might involve dynamically loading a
238 * message catalog we need to call it once before chroot-ing. */
239 void rsyserr(enum logcode code, int errcode, const char *format, ...)
246 va_start(ap, format);
247 len = vslprintf(buf, sizeof(buf), format, ap);
250 if (len > sizeof(buf)-1) exit_cleanup(RERR_MESSAGEIO);
252 sysmsg = strerror(errcode);
253 sys_len = strlen(sysmsg);
254 if (len + 3 + sys_len > sizeof(buf) - 1)
255 exit_cleanup(RERR_MESSAGEIO);
257 strcpy(buf + len, ": ");
259 strcpy(buf + len, sysmsg);
261 strcpy(buf + len, "\n");
264 rwrite(code, buf, len);
269 void rflush(enum logcode code)
272 extern int am_daemon;
282 if (code == FERROR) {
287 extern int am_server;
294 if (!f) exit_cleanup(RERR_MESSAGEIO);
300 /* a generic logging routine for send/recv, with parameter
302 static void log_formatted(enum logcode code,
303 char *format, char *op, struct file_struct *file,
304 struct stats *initial_stats)
306 extern int module_id;
307 extern char *auth_user;
312 extern struct stats stats;
313 extern int am_sender;
314 extern int am_daemon;
317 strlcpy(buf, format, sizeof(buf));
320 s && (p=strchr(s,'%')); ) {
325 case 'h': if (am_daemon) n = client_name(0); break;
326 case 'a': if (am_daemon) n = client_addr(0); break;
328 slprintf(buf2,sizeof(buf2),"%.0f",
329 (double)file->length);
333 slprintf(buf2,sizeof(buf2),"%d",
337 case 'o': n = op; break;
339 slprintf(buf2, sizeof(buf2), "%s/%s",
340 file->basedir?file->basedir:"",
346 case 'm': n = lp_name(module_id); break;
347 case 't': n = timestring(time(NULL)); break;
348 case 'P': n = lp_path(module_id); break;
349 case 'u': n = auth_user; break;
352 b = stats.total_written -
353 initial_stats->total_written;
355 b = stats.total_read -
356 initial_stats->total_read;
358 slprintf(buf2,sizeof(buf2),"%.0f", (double)b);
363 b = stats.total_written -
364 initial_stats->total_written;
366 b = stats.total_read -
367 initial_stats->total_read;
369 slprintf(buf2,sizeof(buf2),"%.0f", (double)b);
378 if ((l-1) + ((int)(s - &buf[0])) > sizeof(buf)) {
379 rprintf(FERROR,"buffer overflow expanding %%%c - exiting\n",
381 exit_cleanup(RERR_MESSAGEIO);
385 memmove(s+(l-1), s+1, strlen(s+1)+1);
392 rprintf(code,"%s\n", buf);
395 /* log the outgoing transfer of a file */
396 void log_send(struct file_struct *file, struct stats *initial_stats)
398 extern int module_id;
399 extern int am_server;
400 extern char *log_format;
402 if (lp_transfer_logging(module_id)) {
403 log_formatted(FLOG, lp_log_format(module_id), "send", file, initial_stats);
404 } else if (log_format && !am_server) {
405 log_formatted(FINFO, log_format, "send", file, initial_stats);
409 /* log the incoming transfer of a file */
410 void log_recv(struct file_struct *file, struct stats *initial_stats)
412 extern int module_id;
413 extern int am_server;
414 extern char *log_format;
416 if (lp_transfer_logging(module_id)) {
417 log_formatted(FLOG, lp_log_format(module_id), "recv", file, initial_stats);
418 } else if (log_format && !am_server) {
419 log_formatted(FINFO, log_format, "recv", file, initial_stats);
427 * Called when the transfer is interrupted for some reason.
429 * Code is one of the RERR_* codes from errcode.h, or terminating
432 void log_exit(int code, const char *file, int line)
435 extern struct stats stats;
436 rprintf(FLOG,"wrote %.0f bytes read %.0f bytes total size %.0f\n",
437 (double)stats.total_written,
438 (double)stats.total_read,
439 (double)stats.total_size);
443 name = rerr_name(code);
445 name = "unexplained error";
447 rprintf(FLOG,"transfer interrupted: %s (code %d) at %s(%d)\n",
448 name, code, file, line);
455 /* log the incoming transfer of a file for interactive use, this
456 will be called at the end where the client was run
458 it i called when a file starts to be transferred
460 void log_transfer(struct file_struct *file, const char *fname)
464 if (!verbose) return;
466 rprintf(FINFO,"%s\n", fname);