2 Copyright (C) Andrew Tridgell 1998
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 logging and utility functions
27 static int log_error_fd = -1;
29 static void logit(int priority, char *buf)
32 fprintf(logfile,"%s [%d] %s",
33 timestring(time(NULL)), (int)getpid(), buf);
36 syslog(priority, "%s", buf);
42 static int initialised;
43 int options = LOG_PID;
47 if (initialised) return;
50 /* this looks pointless, but it is needed in order for the
51 C library on some systems to fetch the timezone info
56 /* optionally use a log file instead of syslog */
59 extern int orig_umask;
60 int old_umask = umask(022 | orig_umask);
61 logfile = fopen(logf, "a");
67 options |= LOG_NDELAY;
71 openlog("rsyncd", options, lp_syslog_facility());
73 openlog("rsyncd", options);
77 logit(LOG_INFO,"rsyncd started\n");
81 /* setup the error file descriptor - used when we are a server
82 that is receiving files */
83 void set_error_fd(int fd)
88 /* this is the underlying (unformatted) rsync debugging function. Call
89 it with FINFO, FERROR or FLOG */
90 void rwrite(enum logcode code, char *buf, int len)
95 /* recursion can happen with certain fatal conditions */
97 if (quiet && code == FINFO) return;
99 if (len < 0) exit_cleanup(RERR_MESSAGEIO);
104 if (am_daemon) logit(LOG_INFO, buf);
108 if (io_error_write(log_error_fd, code, buf, strlen(buf))) {
112 if (io_multiplex_write(code, buf, strlen(buf))) {
118 int priority = LOG_INFO;
119 if (code == FERROR) priority = LOG_WARNING;
126 logit(priority, buf);
132 if (code == FERROR) {
137 extern int am_server;
144 if (!f) exit_cleanup(RERR_MESSAGEIO);
146 if (fwrite(buf, len, 1, f) != 1) exit_cleanup(RERR_MESSAGEIO);
148 if (buf[len-1] == '\r' || buf[len-1] == '\n') fflush(f);
152 /* this is the rsync debugging function. Call it with FINFO, FERROR or FLOG */
153 void rprintf(enum logcode code, const char *format, ...)
159 va_start(ap, format);
160 len = vslprintf(buf, sizeof(buf), format, ap);
163 if (len > sizeof(buf)-1) exit_cleanup(RERR_MESSAGEIO);
165 rwrite(code, buf, len);
168 void rflush(enum logcode code)
171 extern int am_daemon;
181 if (code == FERROR) {
186 extern int am_server;
193 if (!f) exit_cleanup(RERR_MESSAGEIO);
199 /* a generic logging routine for send/recv, with parameter
201 static void log_formatted(enum logcode code,
202 char *format, char *op, struct file_struct *file,
203 struct stats *initial_stats)
205 extern int module_id;
206 extern char *auth_user;
211 extern struct stats stats;
212 extern int am_sender;
213 extern int am_daemon;
216 strlcpy(buf, format, sizeof(buf));
219 s && (p=strchr(s,'%')); ) {
224 case 'h': if (am_daemon) n = client_name(0); break;
225 case 'a': if (am_daemon) n = client_addr(0); break;
227 slprintf(buf2,sizeof(buf2),"%.0f",
228 (double)file->length);
232 slprintf(buf2,sizeof(buf2),"%d",
236 case 'o': n = op; break;
238 slprintf(buf2, sizeof(buf2), "%s/%s",
239 file->basedir?file->basedir:"",
245 case 'm': n = lp_name(module_id); break;
246 case 't': n = timestring(time(NULL)); break;
247 case 'P': n = lp_path(module_id); break;
248 case 'u': n = auth_user; break;
251 b = stats.total_written -
252 initial_stats->total_written;
254 b = stats.total_read -
255 initial_stats->total_read;
257 slprintf(buf2,sizeof(buf2),"%.0f", (double)b);
262 b = stats.total_written -
263 initial_stats->total_written;
265 b = stats.total_read -
266 initial_stats->total_read;
268 slprintf(buf2,sizeof(buf2),"%.0f", (double)b);
277 if ((l-1) + ((int)(s - &buf[0])) > sizeof(buf)) {
278 rprintf(FERROR,"buffer overflow expanding %%%c - exiting\n",
280 exit_cleanup(RERR_MESSAGEIO);
284 memmove(s+(l-1), s+1, strlen(s+1)+1);
291 rprintf(code,"%s\n", buf);
294 /* log the outgoing transfer of a file */
295 void log_send(struct file_struct *file, struct stats *initial_stats)
297 extern int module_id;
298 extern int am_server;
299 extern char *log_format;
301 if (lp_transfer_logging(module_id)) {
302 log_formatted(FLOG, lp_log_format(module_id), "send", file, initial_stats);
303 } else if (log_format && !am_server) {
304 log_formatted(FINFO, log_format, "send", file, initial_stats);
308 /* log the incoming transfer of a file */
309 void log_recv(struct file_struct *file, struct stats *initial_stats)
311 extern int module_id;
312 extern int am_server;
313 extern char *log_format;
315 if (lp_transfer_logging(module_id)) {
316 log_formatted(FLOG, lp_log_format(module_id), "recv", file, initial_stats);
317 } else if (log_format && !am_server) {
318 log_formatted(FINFO, log_format, "recv", file, initial_stats);
322 /* called when the transfer is interrupted for some reason */
323 void log_exit(int code, const char *file, int line)
326 extern struct stats stats;
327 rprintf(FLOG,"wrote %.0f bytes read %.0f bytes total size %.0f\n",
328 (double)stats.total_written,
329 (double)stats.total_read,
330 (double)stats.total_size);
332 rprintf(FLOG,"transfer interrupted (code %d) at %s(%d)\n",
337 /* log the incoming transfer of a file for interactive use, this
338 will be called at the end where the client was run
340 it i called when a file starts to be transferred
342 void log_transfer(struct file_struct *file, const char *fname)
346 if (!verbose) return;
348 rprintf(FINFO,"%s\n", fname);