1 /* -*- c-file-style: "linux"; -*-
3 Copyright (C) 1998-2000 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
27 static char *logfname;
29 static int log_error_fd = -1;
31 static void logit(int priority, char *buf)
36 fprintf(logfile,"%s [%d] %s",
37 timestring(time(NULL)), (int)getpid(), buf);
40 syslog(priority, "%s", buf);
46 static int initialised;
47 int options = LOG_PID;
50 if (initialised) return;
53 /* this looks pointless, but it is needed in order for the
54 C library on some systems to fetch the timezone info
59 /* optionally use a log file instead of syslog */
60 logfname = lp_log_file();
70 options |= LOG_NDELAY;
74 openlog("rsyncd", options, lp_syslog_facility());
76 openlog("rsyncd", options);
80 logit(LOG_INFO,"rsyncd started\n");
86 if (logfname && !logfile) {
87 extern int orig_umask;
88 int old_umask = umask(022 | orig_umask);
89 logfile = fopen(logfname, "a");
102 /* setup the error file descriptor - used when we are a server
103 that is receiving files */
104 void set_error_fd(int fd)
109 /* this is the underlying (unformatted) rsync debugging function. Call
110 it with FINFO, FERROR or FLOG */
111 void rwrite(enum logcode code, char *buf, int len)
114 extern int am_daemon;
115 extern int am_server;
117 /* recursion can happen with certain fatal conditions */
119 if (quiet && code == FINFO) return;
121 if (len < 0) exit_cleanup(RERR_MESSAGEIO);
126 if (am_daemon) logit(LOG_INFO, buf);
130 /* first try to pass it off the our sibling */
131 if (am_server && io_error_write(log_error_fd, code, buf, len)) {
135 /* then try to pass it to the other end */
136 if (am_server && io_multiplex_write(code, buf, len)) {
142 int priority = LOG_INFO;
143 if (code == FERROR) priority = LOG_WARNING;
150 logit(priority, buf);
156 if (code == FERROR) {
167 if (!f) exit_cleanup(RERR_MESSAGEIO);
169 if (fwrite(buf, len, 1, f) != 1) exit_cleanup(RERR_MESSAGEIO);
171 if (buf[len-1] == '\r' || buf[len-1] == '\n') fflush(f);
175 /* This is the rsync debugging function. Call it with FINFO, FERROR or
177 void rprintf(enum logcode code, const char *format, ...)
183 va_start(ap, format);
184 len = vslprintf(buf, sizeof(buf), format, ap);
187 if (len > sizeof(buf)-1) exit_cleanup(RERR_MESSAGEIO);
189 rwrite(code, buf, len);
193 /* This is like rprintf, but it also tries to print some
194 * representation of the error code. Normally errcode = errno.
196 * Unlike rprintf, this always adds a newline and there should not be
197 * one in the format string.
199 * Note that since strerror might involve dynamically loading a
200 * message catalog we need to call it once before chroot-ing. */
201 void rsyserr(enum logcode code, int errcode, const char *format, ...)
208 va_start(ap, format);
209 len = vslprintf(buf, sizeof(buf), format, ap);
212 if (len > sizeof(buf)-1) exit_cleanup(RERR_MESSAGEIO);
214 sysmsg = strerror(errcode);
215 sys_len = strlen(sysmsg);
216 if (len + 3 + sys_len > sizeof(buf) - 1)
217 exit_cleanup(RERR_MESSAGEIO);
219 strcpy(buf + len, ": ");
221 strcpy(buf + len, sysmsg);
223 strcpy(buf + len, "\n");
226 rwrite(code, buf, len);
231 void rflush(enum logcode code)
234 extern int am_daemon;
244 if (code == FERROR) {
249 extern int am_server;
256 if (!f) exit_cleanup(RERR_MESSAGEIO);
262 /* a generic logging routine for send/recv, with parameter
264 static void log_formatted(enum logcode code,
265 char *format, char *op, struct file_struct *file,
266 struct stats *initial_stats)
268 extern int module_id;
269 extern char *auth_user;
274 extern struct stats stats;
275 extern int am_sender;
276 extern int am_daemon;
279 strlcpy(buf, format, sizeof(buf));
282 s && (p=strchr(s,'%')); ) {
287 case 'h': if (am_daemon) n = client_name(0); break;
288 case 'a': if (am_daemon) n = client_addr(0); break;
290 slprintf(buf2,sizeof(buf2),"%.0f",
291 (double)file->length);
295 slprintf(buf2,sizeof(buf2),"%d",
299 case 'o': n = op; break;
301 slprintf(buf2, sizeof(buf2), "%s/%s",
302 file->basedir?file->basedir:"",
308 case 'm': n = lp_name(module_id); break;
309 case 't': n = timestring(time(NULL)); break;
310 case 'P': n = lp_path(module_id); break;
311 case 'u': n = auth_user; break;
314 b = stats.total_written -
315 initial_stats->total_written;
317 b = stats.total_read -
318 initial_stats->total_read;
320 slprintf(buf2,sizeof(buf2),"%.0f", (double)b);
325 b = stats.total_written -
326 initial_stats->total_written;
328 b = stats.total_read -
329 initial_stats->total_read;
331 slprintf(buf2,sizeof(buf2),"%.0f", (double)b);
340 if ((l-1) + ((int)(s - &buf[0])) > sizeof(buf)) {
341 rprintf(FERROR,"buffer overflow expanding %%%c - exiting\n",
343 exit_cleanup(RERR_MESSAGEIO);
347 memmove(s+(l-1), s+1, strlen(s+1)+1);
354 rprintf(code,"%s\n", buf);
357 /* log the outgoing transfer of a file */
358 void log_send(struct file_struct *file, struct stats *initial_stats)
360 extern int module_id;
361 extern int am_server;
362 extern char *log_format;
364 if (lp_transfer_logging(module_id)) {
365 log_formatted(FLOG, lp_log_format(module_id), "send", file, initial_stats);
366 } else if (log_format && !am_server) {
367 log_formatted(FINFO, log_format, "send", file, initial_stats);
371 /* log the incoming transfer of a file */
372 void log_recv(struct file_struct *file, struct stats *initial_stats)
374 extern int module_id;
375 extern int am_server;
376 extern char *log_format;
378 if (lp_transfer_logging(module_id)) {
379 log_formatted(FLOG, lp_log_format(module_id), "recv", file, initial_stats);
380 } else if (log_format && !am_server) {
381 log_formatted(FINFO, log_format, "recv", file, initial_stats);
385 /* called when the transfer is interrupted for some reason */
386 void log_exit(int code, const char *file, int line)
389 extern struct stats stats;
390 rprintf(FLOG,"wrote %.0f bytes read %.0f bytes total size %.0f\n",
391 (double)stats.total_written,
392 (double)stats.total_read,
393 (double)stats.total_size);
395 rprintf(FLOG,"transfer interrupted (code %d) at %s(%d)\n",
400 /* log the incoming transfer of a file for interactive use, this
401 will be called at the end where the client was run
403 it i called when a file starts to be transferred
405 void log_transfer(struct file_struct *file, const char *fname)
409 if (!verbose) return;
411 rprintf(FINFO,"%s\n", fname);