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 transfered" },
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;
89 static void logit(int priority, char *buf)
94 fprintf(logfile,"%s [%d] %s",
95 timestring(time(NULL)), (int)getpid(), buf);
98 syslog(priority, "%s", buf);
104 int options = LOG_PID;
111 /* this looks pointless, but it is needed in order for the
112 * C library on some systems to fetch the timezone info
113 * before the chroot */
117 /* optionally use a log file instead of syslog */
118 logfname = lp_log_file();
128 options |= LOG_NDELAY;
132 openlog("rsyncd", options, lp_syslog_facility());
134 openlog("rsyncd", options);
138 logit(LOG_INFO,"rsyncd started\n");
144 if (logfname && !logfile) {
145 extern int orig_umask;
146 int old_umask = umask(022 | orig_umask);
147 logfile = fopen(logfname, "a");
160 /* this is the underlying (unformatted) rsync debugging function. Call
161 * it with FINFO, FERROR or FLOG */
162 void rwrite(enum logcode code, char *buf, int len)
165 /* recursion can happen with certain fatal conditions */
167 if (quiet && code == FINFO)
171 exit_cleanup(RERR_MESSAGEIO);
175 if (am_server && msg_fd_out >= 0) {
176 /* Pass the message to our sibling. */
177 send_msg((enum msgcode)code, buf, len);
184 int priority = code == FERROR ? LOG_WARNING : LOG_INFO;
189 if (!log_initialised)
191 strlcpy(msg, buf, MIN((int)sizeof msg, len + 1));
192 logit(priority, msg);
195 if (code == FLOG || !am_server)
197 } else if (code == FLOG)
201 /* Pass the message to the non-server side. */
202 if (io_multiplex_write((enum msgcode)code, buf, len))
205 /* TODO: can we send the error to the user somehow? */
210 if (code == FERROR) {
216 f = am_server ? stderr : stdout;
219 exit_cleanup(RERR_MESSAGEIO);
221 if (fwrite(buf, len, 1, f) != 1)
222 exit_cleanup(RERR_MESSAGEIO);
224 if (buf[len-1] == '\r' || buf[len-1] == '\n')
229 /* This is the rsync debugging function. Call it with FINFO, FERROR or
231 void rprintf(enum logcode code, const char *format, ...)
234 char buf[MAXPATHLEN+512];
237 va_start(ap, format);
238 len = vsnprintf(buf, sizeof(buf), format, ap);
241 /* Deal with buffer overruns. Instead of panicking, just
242 * truncate the resulting string. (Note that configure ensures
243 * that we have a vsnprintf() that doesn't ever return -1.) */
244 if (len > sizeof buf - 1) {
245 const char ellipsis[] = "[...]";
247 /* Reset length, and zero-terminate the end of our buffer */
251 /* Copy the ellipsis to the end of the string, but give
252 * us one extra character:
254 * v--- null byte at buf[sizeof(buf)-1]
256 * -> abcd[...]00 <-- now two null bytes at end
258 * If the input format string has a trailing newline,
259 * we copy it into that extra null; if it doesn't, well,
260 * all we lose is one byte. */
261 strncpy(buf+len-sizeof(ellipsis), ellipsis, sizeof(ellipsis));
262 if (format[strlen(format)-1] == '\n') {
267 rwrite(code, buf, len);
271 /* This is like rprintf, but it also tries to print some
272 * representation of the error code. Normally errcode = errno.
274 * Unlike rprintf, this always adds a newline and there should not be
275 * one in the format string.
277 * Note that since strerror might involve dynamically loading a
278 * message catalog we need to call it once before chroot-ing. */
279 void rsyserr(enum logcode code, int errcode, const char *format, ...)
282 char buf[MAXPATHLEN+512];
285 strcpy(buf, RSYNC_NAME ": ");
286 len = (sizeof RSYNC_NAME ": ") - 1;
288 va_start(ap, format);
289 len += vsnprintf(buf + len, sizeof buf - len, format, ap);
292 if (len < sizeof buf) {
293 len += snprintf(buf + len, sizeof buf - len,
294 ": %s (%d)\n", strerror(errcode), errcode);
296 if (len >= sizeof buf)
297 exit_cleanup(RERR_MESSAGEIO);
299 rwrite(code, buf, len);
304 void rflush(enum logcode code)
316 if (code == FERROR) {
327 if (!f) exit_cleanup(RERR_MESSAGEIO);
333 /* a generic logging routine for send/recv, with parameter
335 static void log_formatted(enum logcode code,
336 char *format, char *op, struct file_struct *file,
337 struct stats *initial_stats)
345 /* We expand % codes one by one in place in buf. We don't
346 * copy in the terminating nul of the inserted strings, but
347 * rather keep going until we reach the nul of the format.
348 * Just to make sure we don't clobber that nul and therefore
349 * accidentally keep going, we zero the buffer now. */
350 l = strlcpy(buf, format, sizeof buf);
352 memset(buf + l, 0, sizeof buf - l);
354 for (s = &buf[0]; s && (p = strchr(s,'%')); ) {
359 case 'h': if (am_daemon) n = client_name(0); break;
360 case 'a': if (am_daemon) n = client_addr(0); break;
362 snprintf(buf2,sizeof(buf2),"%.0f",
363 (double)file->length);
367 snprintf(buf2,sizeof(buf2),"%d",
371 case 'o': n = op; break;
373 pathjoin(buf2, sizeof buf2,
374 file->basedir ? file->basedir : "",
380 case 'm': n = lp_name(module_id); break;
381 case 't': n = timestring(time(NULL)); break;
382 case 'P': n = lp_path(module_id); break;
383 case 'u': n = auth_user; break;
386 b = stats.total_written -
387 initial_stats->total_written;
389 b = stats.total_read -
390 initial_stats->total_read;
392 snprintf(buf2,sizeof(buf2),"%.0f", (double)b);
397 b = stats.total_written -
398 initial_stats->total_written;
400 b = stats.total_read -
401 initial_stats->total_read;
403 snprintf(buf2,sizeof(buf2),"%.0f", (double)b);
408 /* n is the string to be inserted in place of this %
409 * code; l is its length not including the trailing
416 if (l + ((int)(s - &buf[0])) >= sizeof(buf)) {
417 rprintf(FERROR,"buffer overflow expanding %%%c - exiting\n",
419 exit_cleanup(RERR_MESSAGEIO);
422 /* Shuffle the rest of the string along to make space for n */
424 memmove(s+(l-1), s+1, strlen(s+1)+1);
427 /* Copy in n but NOT its nul, because the format sting
428 * probably continues after this. */
431 /* Skip over inserted string; continue looking */
435 rprintf(code,"%s\n", buf);
438 /* log the outgoing transfer of a file */
439 void log_send(struct file_struct *file, struct stats *initial_stats)
441 if (lp_transfer_logging(module_id)) {
442 log_formatted(FLOG, lp_log_format(module_id), "send", file, initial_stats);
443 } else if (log_format && !am_server) {
444 log_formatted(FINFO, log_format, "send", file, initial_stats);
448 /* log the incoming transfer of a file */
449 void log_recv(struct file_struct *file, struct stats *initial_stats)
451 if (lp_transfer_logging(module_id)) {
452 log_formatted(FLOG, lp_log_format(module_id), "recv", file, initial_stats);
453 } else if (log_format && !am_server) {
454 log_formatted(FINFO, log_format, "recv", file, initial_stats);
462 * Called when the transfer is interrupted for some reason.
464 * Code is one of the RERR_* codes from errcode.h, or terminating
467 void log_exit(int code, const char *file, int line)
470 rprintf(FLOG,"wrote %.0f bytes read %.0f bytes total size %.0f\n",
471 (double)stats.total_written,
472 (double)stats.total_read,
473 (double)stats.total_size);
477 name = rerr_name(code);
479 name = "unexplained error";
481 /* VANISHED is not an error, only a warning */
482 if (code == RERR_VANISHED) {
483 rprintf(FINFO, "rsync warning: %s (code %d) at %s(%d)\n",
484 name, code, file, line);
486 rprintf(FERROR, "rsync error: %s (code %d) at %s(%d)\n",
487 name, code, file, line);