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;
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;
107 if (log_initialised) return;
110 /* this looks pointless, but it is needed in order for the
111 * C library on some systems to fetch the timezone info
112 * before the chroot */
116 /* optionally use a log file instead of syslog */
117 logfname = lp_log_file();
127 options |= LOG_NDELAY;
131 openlog("rsyncd", options, lp_syslog_facility());
133 openlog("rsyncd", options);
137 logit(LOG_INFO,"rsyncd started\n");
143 if (logfname && !logfile) {
144 extern int orig_umask;
145 int old_umask = umask(022 | orig_umask);
146 logfile = fopen(logfname, "a");
159 /* this is the underlying (unformatted) rsync debugging function. Call
160 * it with FINFO, FERROR or FLOG */
161 void rwrite(enum logcode code, char *buf, int len)
164 /* recursion can happen with certain fatal conditions */
166 if (quiet && code == FINFO)
170 exit_cleanup(RERR_MESSAGEIO);
175 if (am_daemon) logit(LOG_INFO, buf);
180 /* Pass it to non-server side, perhaps through our sibling. */
181 if (msg_fd_out >= 0) {
182 send_msg((enum msgcode)code, buf, len);
185 if (io_multiplex_write((enum msgcode)code, buf, len))
189 /* otherwise, if in daemon mode and either we are not a server
190 * (that is, we are not running --daemon over a remote shell) or
191 * the log has already been initialised, log the message on this
192 * side because we don't want the client to see most errors for
193 * security reasons. We do want early messages when running daemon
194 * mode over a remote shell to go to the remote side; those will
195 * fall through to the next case.
196 * Note that this is only for the time before multiplexing is enabled.
198 if (am_daemon && (!am_server || log_initialised)) {
200 int priority = LOG_INFO;
201 if (code == FERROR) priority = LOG_WARNING;
208 logit(priority, buf);
214 if (code == FERROR) {
226 if (!f) exit_cleanup(RERR_MESSAGEIO);
228 if (fwrite(buf, len, 1, f) != 1) exit_cleanup(RERR_MESSAGEIO);
230 if (buf[len-1] == '\r' || buf[len-1] == '\n') fflush(f);
234 /* This is the rsync debugging function. Call it with FINFO, FERROR or
236 void rprintf(enum logcode code, const char *format, ...)
242 va_start(ap, format);
243 /* Note: might return -1 */
244 len = vsnprintf(buf, sizeof(buf), format, ap);
247 /* Deal with buffer overruns. Instead of panicking, just
248 * truncate the resulting string. Note that some vsnprintf()s
249 * return -1 on truncation, e.g., glibc 2.0.6 and earlier. */
250 if ((size_t) len > sizeof(buf)-1 || len < 0) {
251 const char ellipsis[] = "[...]";
253 /* Reset length, and zero-terminate the end of our buffer */
257 /* Copy the ellipsis to the end of the string, but give
258 * us one extra character:
260 * v--- null byte at buf[sizeof(buf)-1]
262 * -> abcd[...]00 <-- now two null bytes at end
264 * If the input format string has a trailing newline,
265 * we copy it into that extra null; if it doesn't, well,
266 * all we lose is one byte. */
267 strncpy(buf+len-sizeof(ellipsis), ellipsis, sizeof(ellipsis));
268 if (format[strlen(format)-1] == '\n') {
273 rwrite(code, buf, len);
277 /* This is like rprintf, but it also tries to print some
278 * representation of the error code. Normally errcode = errno.
280 * Unlike rprintf, this always adds a newline and there should not be
281 * one in the format string.
283 * Note that since strerror might involve dynamically loading a
284 * message catalog we need to call it once before chroot-ing. */
285 void rsyserr(enum logcode code, int errcode, const char *format, ...)
293 va_start(ap, format);
294 /* Note: might return <0 */
295 len = vsnprintf(buf, sizeof(buf), format, ap);
298 /* TODO: Put in RSYNC_NAME at the start. */
300 if ((size_t) len > sizeof(buf)-1)
301 exit_cleanup(RERR_MESSAGEIO);
303 sysmsg = strerror(errcode);
304 sys_len = strlen(sysmsg);
305 if ((size_t) len + 3 + sys_len > sizeof(buf) - 1)
306 exit_cleanup(RERR_MESSAGEIO);
308 strcpy(buf + len, ": ");
310 strcpy(buf + len, sysmsg);
312 strcpy(buf + len, "\n");
315 rwrite(code, buf, len);
320 void rflush(enum logcode code)
332 if (code == FERROR) {
343 if (!f) exit_cleanup(RERR_MESSAGEIO);
349 /* a generic logging routine for send/recv, with parameter
351 static void log_formatted(enum logcode code,
352 char *format, char *op, struct file_struct *file,
353 struct stats *initial_stats)
361 /* We expand % codes one by one in place in buf. We don't
362 * copy in the terminating nul of the inserted strings, but
363 * rather keep going until we reach the nul of the format.
364 * Just to make sure we don't clobber that nul and therefore
365 * accidentally keep going, we zero the buffer now. */
366 memset(buf, 0, sizeof buf);
367 strlcpy(buf, format, sizeof(buf));
369 for (s = &buf[0]; s && (p = strchr(s,'%')); ) {
374 case 'h': if (am_daemon) n = client_name(0); break;
375 case 'a': if (am_daemon) n = client_addr(0); break;
377 snprintf(buf2,sizeof(buf2),"%.0f",
378 (double)file->length);
382 snprintf(buf2,sizeof(buf2),"%d",
386 case 'o': n = op; break;
388 snprintf(buf2, sizeof(buf2), "%s/%s",
389 file->basedir?file->basedir:"",
395 case 'm': n = lp_name(module_id); break;
396 case 't': n = timestring(time(NULL)); break;
397 case 'P': n = lp_path(module_id); break;
398 case 'u': n = auth_user; break;
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 b = stats.total_written -
413 initial_stats->total_written;
415 b = stats.total_read -
416 initial_stats->total_read;
418 snprintf(buf2,sizeof(buf2),"%.0f", (double)b);
423 /* n is the string to be inserted in place of this %
424 * code; l is its length not including the trailing
431 if (l + ((int)(s - &buf[0])) >= sizeof(buf)) {
432 rprintf(FERROR,"buffer overflow expanding %%%c - exiting\n",
434 exit_cleanup(RERR_MESSAGEIO);
437 /* Shuffle the rest of the string along to make space for n */
439 memmove(s+(l-1), s+1, strlen(s+1)+1);
442 /* Copy in n but NOT its nul, because the format sting
443 * probably continues after this. */
446 /* Skip over inserted string; continue looking */
450 rprintf(code,"%s\n", buf);
453 /* log the outgoing transfer of a file */
454 void log_send(struct file_struct *file, struct stats *initial_stats)
456 if (lp_transfer_logging(module_id)) {
457 log_formatted(FLOG, lp_log_format(module_id), "send", file, initial_stats);
458 } else if (log_format && !am_server) {
459 log_formatted(FINFO, log_format, "send", file, initial_stats);
463 /* log the incoming transfer of a file */
464 void log_recv(struct file_struct *file, struct stats *initial_stats)
466 if (lp_transfer_logging(module_id)) {
467 log_formatted(FLOG, lp_log_format(module_id), "recv", file, initial_stats);
468 } else if (log_format && !am_server) {
469 log_formatted(FINFO, log_format, "recv", file, initial_stats);
477 * Called when the transfer is interrupted for some reason.
479 * Code is one of the RERR_* codes from errcode.h, or terminating
482 void log_exit(int code, const char *file, int line)
485 rprintf(FLOG,"wrote %.0f bytes read %.0f bytes total size %.0f\n",
486 (double)stats.total_written,
487 (double)stats.total_read,
488 (double)stats.total_size);
492 name = rerr_name(code);
494 name = "unexplained error";
496 /* VANISHED is not an error, only a warning */
497 if (code == RERR_VANISHED) {
498 rprintf(FINFO, "rsync warning: %s (code %d) at %s(%d)\n",
499 name, code, file, line);
501 rprintf(FERROR, "rsync error: %s (code %d) at %s(%d)\n",
502 name, code, file, line);