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.
35 extern int msg_fd_out;
36 extern char *auth_user;
37 extern char *log_format;
39 static int log_initialised;
40 static char *logfname;
44 int log_got_error = 0;
49 } const rerr_names[] = {
50 { RERR_SYNTAX , "syntax or usage error" },
51 { RERR_PROTOCOL , "protocol incompatibility" },
52 { RERR_FILESELECT , "errors selecting input/output files, dirs" },
53 { RERR_UNSUPPORTED, "requested action not supported" },
54 { RERR_STARTCLIENT, "error starting client-server protocol" },
55 { RERR_SOCKETIO , "error in socket IO" },
56 { RERR_FILEIO , "error in file IO" },
57 { RERR_STREAMIO , "error in rsync protocol data stream" },
58 { RERR_MESSAGEIO , "errors with program diagnostics" },
59 { RERR_IPC , "error in IPC code" },
60 { RERR_SIGNAL , "received SIGUSR1 or SIGINT" },
61 { RERR_WAITCHILD , "some error returned by waitpid()" },
62 { RERR_MALLOC , "error allocating core memory buffers" },
63 { RERR_PARTIAL , "some files could not be transferred" },
64 { RERR_VANISHED , "some files vanished before they could be transferred" },
65 { RERR_TIMEOUT , "timeout in data send/receive" },
66 { RERR_CMD_FAILED , "remote shell failed" },
67 { RERR_CMD_KILLED , "remote shell killed" },
68 { RERR_CMD_RUN, "remote command could not be run" },
69 { RERR_CMD_NOTFOUND, "remote command not found" },
76 * Map from rsync error code to name, or return NULL.
78 static char const *rerr_name(int code)
81 for (i = 0; rerr_names[i].name; i++) {
82 if (rerr_names[i].code == code)
83 return rerr_names[i].name;
88 static void log_open(void)
90 if (logfname && !logfile) {
91 extern int orig_umask;
92 int old_umask = umask(022 | orig_umask);
93 logfile = fopen(logfname, "a");
96 am_daemon = 0; /* avoid trying to log again */
97 rsyserr(FERROR, errno, "fopen() of log-file failed");
98 exit_cleanup(RERR_FILESELECT);
103 static void logit(int priority, char *buf)
108 fprintf(logfile,"%s [%d] %s",
109 timestring(time(NULL)), (int)getpid(), buf);
112 syslog(priority, "%s", buf);
118 int options = LOG_PID;
125 /* this looks pointless, but it is needed in order for the
126 * C library on some systems to fetch the timezone info
127 * before the chroot */
131 /* optionally use a log file instead of syslog */
132 logfname = lp_log_file();
142 options |= LOG_NDELAY;
146 openlog("rsyncd", options, lp_syslog_facility());
148 openlog("rsyncd", options);
152 logit(LOG_INFO,"rsyncd started\n");
164 /* this is the underlying (unformatted) rsync debugging function. Call
165 * it with FINFO, FERROR or FLOG */
166 void rwrite(enum logcode code, char *buf, int len)
169 /* recursion can happen with certain fatal conditions */
171 if (quiet && code == FINFO)
175 exit_cleanup(RERR_MESSAGEIO);
179 if (am_server && msg_fd_out >= 0) {
180 /* Pass the message to our sibling. */
181 send_msg((enum msgcode)code, buf, len);
188 int priority = code == FERROR ? LOG_WARNING : LOG_INFO;
193 if (!log_initialised)
195 strlcpy(msg, buf, MIN((int)sizeof msg, len + 1));
196 logit(priority, msg);
199 if (code == FLOG || !am_server)
201 } else if (code == FLOG)
205 /* Pass the message to the non-server side. */
206 if (io_multiplex_write((enum msgcode)code, buf, len))
209 /* TODO: can we send the error to the user somehow? */
214 if (code == FERROR) {
220 f = am_server ? stderr : stdout;
223 exit_cleanup(RERR_MESSAGEIO);
225 if (fwrite(buf, len, 1, f) != 1)
226 exit_cleanup(RERR_MESSAGEIO);
228 if (buf[len-1] == '\r' || buf[len-1] == '\n')
233 /* This is the rsync debugging function. Call it with FINFO, FERROR or
235 void rprintf(enum logcode code, const char *format, ...)
238 char buf[MAXPATHLEN+512];
241 va_start(ap, format);
242 len = vsnprintf(buf, sizeof(buf), format, ap);
245 /* Deal with buffer overruns. Instead of panicking, just
246 * truncate the resulting string. (Note that configure ensures
247 * that we have a vsnprintf() that doesn't ever return -1.) */
248 if (len > sizeof buf - 1) {
249 const char ellipsis[] = "[...]";
251 /* Reset length, and zero-terminate the end of our buffer */
255 /* Copy the ellipsis to the end of the string, but give
256 * us one extra character:
258 * v--- null byte at buf[sizeof(buf)-1]
260 * -> abcd[...]00 <-- now two null bytes at end
262 * If the input format string has a trailing newline,
263 * we copy it into that extra null; if it doesn't, well,
264 * all we lose is one byte. */
265 strncpy(buf+len-sizeof(ellipsis), ellipsis, sizeof(ellipsis));
266 if (format[strlen(format)-1] == '\n') {
271 rwrite(code, buf, len);
275 /* This is like rprintf, but it also tries to print some
276 * representation of the error code. Normally errcode = errno.
278 * Unlike rprintf, this always adds a newline and there should not be
279 * one in the format string.
281 * Note that since strerror might involve dynamically loading a
282 * message catalog we need to call it once before chroot-ing. */
283 void rsyserr(enum logcode code, int errcode, const char *format, ...)
286 char buf[MAXPATHLEN+512];
289 strcpy(buf, RSYNC_NAME ": ");
290 len = (sizeof RSYNC_NAME ": ") - 1;
292 va_start(ap, format);
293 len += vsnprintf(buf + len, sizeof buf - len, format, ap);
296 if (len < sizeof buf) {
297 len += snprintf(buf + len, sizeof buf - len,
298 ": %s (%d)\n", strerror(errcode), errcode);
300 if (len >= sizeof buf)
301 exit_cleanup(RERR_MESSAGEIO);
303 rwrite(code, buf, len);
308 void rflush(enum logcode code)
320 if (code == FERROR) {
331 if (!f) exit_cleanup(RERR_MESSAGEIO);
337 /* a generic logging routine for send/recv, with parameter
339 static void log_formatted(enum logcode code,
340 char *format, char *op, struct file_struct *file,
341 struct stats *initial_stats)
349 /* We expand % codes one by one in place in buf. We don't
350 * copy in the terminating nul of the inserted strings, but
351 * rather keep going until we reach the nul of the format.
352 * Just to make sure we don't clobber that nul and therefore
353 * accidentally keep going, we zero the buffer now. */
354 l = strlcpy(buf, format, sizeof buf);
356 memset(buf + l, 0, sizeof buf - l);
358 for (s = &buf[0]; s && (p = strchr(s,'%')); ) {
363 case 'h': if (am_daemon) n = client_name(0); break;
364 case 'a': if (am_daemon) n = client_addr(0); break;
366 snprintf(buf2,sizeof(buf2),"%.0f",
367 (double)file->length);
371 snprintf(buf2,sizeof(buf2),"%d",
375 case 'o': n = op; break;
377 pathjoin(buf2, sizeof buf2,
378 file->basedir ? file->basedir : "",
380 clean_fname(buf2, 0);
384 case 'm': n = lp_name(module_id); break;
385 case 't': n = timestring(time(NULL)); break;
386 case 'P': n = lp_path(module_id); break;
387 case 'u': n = auth_user; break;
390 b = stats.total_written -
391 initial_stats->total_written;
393 b = stats.total_read -
394 initial_stats->total_read;
396 snprintf(buf2,sizeof(buf2),"%.0f", (double)b);
401 b = stats.total_written -
402 initial_stats->total_written;
404 b = stats.total_read -
405 initial_stats->total_read;
407 snprintf(buf2,sizeof(buf2),"%.0f", (double)b);
412 /* n is the string to be inserted in place of this %
413 * code; l is its length not including the trailing
420 if (l + ((int)(s - &buf[0])) >= sizeof(buf)) {
421 rprintf(FERROR,"buffer overflow expanding %%%c - exiting\n",
423 exit_cleanup(RERR_MESSAGEIO);
426 /* Shuffle the rest of the string along to make space for n */
428 memmove(s+(l-1), s+1, strlen(s+1)+1);
431 /* Copy in n but NOT its nul, because the format sting
432 * probably continues after this. */
435 /* Skip over inserted string; continue looking */
439 rprintf(code,"%s\n", buf);
442 /* log the outgoing transfer of a file */
443 void log_send(struct file_struct *file, struct stats *initial_stats)
445 if (lp_transfer_logging(module_id)) {
446 log_formatted(FLOG, lp_log_format(module_id), "send", file, initial_stats);
447 } else if (log_format && !am_server) {
448 log_formatted(FINFO, log_format, "send", file, initial_stats);
452 /* log the incoming transfer of a file */
453 void log_recv(struct file_struct *file, struct stats *initial_stats)
455 if (lp_transfer_logging(module_id)) {
456 log_formatted(FLOG, lp_log_format(module_id), "recv", file, initial_stats);
457 } else if (log_format && !am_server) {
458 log_formatted(FINFO, log_format, "recv", file, initial_stats);
466 * Called when the transfer is interrupted for some reason.
468 * Code is one of the RERR_* codes from errcode.h, or terminating
471 void log_exit(int code, const char *file, int line)
474 rprintf(FLOG,"wrote %.0f bytes read %.0f bytes total size %.0f\n",
475 (double)stats.total_written,
476 (double)stats.total_read,
477 (double)stats.total_size);
481 name = rerr_name(code);
483 name = "unexplained error";
485 /* VANISHED is not an error, only a warning */
486 if (code == RERR_VANISHED) {
487 rprintf(FINFO, "rsync warning: %s (code %d) at %s(%d)\n",
488 name, code, file, line);
490 rprintf(FERROR, "rsync error: %s (code %d) at %s(%d)\n",
491 name, code, file, line);