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
26 static char *logfname;
28 static int log_error_fd = -1;
30 static void logit(int priority, char *buf)
34 extern int orig_umask;
35 int old_umask = umask(022 | orig_umask);
36 logfile = fopen(logfname, "a");
39 fprintf(logfile,"%s [%d] %s",
40 timestring(time(NULL)), (int)getpid(), buf);
43 syslog(priority, "%s", buf);
49 static int initialised;
50 int options = LOG_PID;
53 if (initialised) return;
56 /* this looks pointless, but it is needed in order for the
57 C library on some systems to fetch the timezone info
62 /* optionally use a log file instead of syslog */
63 logfname = lp_log_file();
71 options |= LOG_NDELAY;
75 openlog("rsyncd", options, lp_syslog_facility());
77 openlog("rsyncd", options);
81 logit(LOG_INFO,"rsyncd started\n");
85 /* for long runs when using a log file, close it before potential long waits
86 so it can be trimmed by another process instead of growing forever */
95 /* setup the error file descriptor - used when we are a server
96 that is receiving files */
97 void set_error_fd(int fd)
102 /* this is the underlying (unformatted) rsync debugging function. Call
103 it with FINFO, FERROR or FLOG */
104 void rwrite(enum logcode code, char *buf, int len)
107 extern int am_daemon;
108 extern int am_server;
110 /* recursion can happen with certain fatal conditions */
112 if (quiet && code == FINFO) return;
114 if (len < 0) exit_cleanup(RERR_MESSAGEIO);
119 if (am_daemon) logit(LOG_INFO, buf);
123 /* first try to pass it off the our sibling */
124 if (am_server && io_error_write(log_error_fd, code, buf, len)) {
128 /* then try to pass it to the other end */
129 if (am_server && io_multiplex_write(code, buf, len)) {
135 int priority = LOG_INFO;
136 if (code == FERROR) priority = LOG_WARNING;
143 logit(priority, buf);
149 if (code == FERROR) {
160 if (!f) exit_cleanup(RERR_MESSAGEIO);
162 if (fwrite(buf, len, 1, f) != 1) exit_cleanup(RERR_MESSAGEIO);
164 if (buf[len-1] == '\r' || buf[len-1] == '\n') fflush(f);
168 /* this is the rsync debugging function. Call it with FINFO, FERROR or FLOG */
169 void rprintf(enum logcode code, const char *format, ...)
175 va_start(ap, format);
176 len = vslprintf(buf, sizeof(buf), format, ap);
179 if (len > sizeof(buf)-1) exit_cleanup(RERR_MESSAGEIO);
181 rwrite(code, buf, len);
184 void rflush(enum logcode code)
187 extern int am_daemon;
197 if (code == FERROR) {
202 extern int am_server;
209 if (!f) exit_cleanup(RERR_MESSAGEIO);
215 /* a generic logging routine for send/recv, with parameter
217 static void log_formatted(enum logcode code,
218 char *format, char *op, struct file_struct *file,
219 struct stats *initial_stats)
221 extern int module_id;
222 extern char *auth_user;
227 extern struct stats stats;
228 extern int am_sender;
229 extern int am_daemon;
232 strlcpy(buf, format, sizeof(buf));
235 s && (p=strchr(s,'%')); ) {
240 case 'h': if (am_daemon) n = client_name(0); break;
241 case 'a': if (am_daemon) n = client_addr(0); break;
243 slprintf(buf2,sizeof(buf2),"%.0f",
244 (double)file->length);
248 slprintf(buf2,sizeof(buf2),"%d",
252 case 'o': n = op; break;
254 slprintf(buf2, sizeof(buf2), "%s/%s",
255 file->basedir?file->basedir:"",
261 case 'm': n = lp_name(module_id); break;
262 case 't': n = timestring(time(NULL)); break;
263 case 'P': n = lp_path(module_id); break;
264 case 'u': n = auth_user; break;
267 b = stats.total_written -
268 initial_stats->total_written;
270 b = stats.total_read -
271 initial_stats->total_read;
273 slprintf(buf2,sizeof(buf2),"%.0f", (double)b);
278 b = stats.total_written -
279 initial_stats->total_written;
281 b = stats.total_read -
282 initial_stats->total_read;
284 slprintf(buf2,sizeof(buf2),"%.0f", (double)b);
293 if ((l-1) + ((int)(s - &buf[0])) > sizeof(buf)) {
294 rprintf(FERROR,"buffer overflow expanding %%%c - exiting\n",
296 exit_cleanup(RERR_MESSAGEIO);
300 memmove(s+(l-1), s+1, strlen(s+1)+1);
307 rprintf(code,"%s\n", buf);
310 /* log the outgoing transfer of a file */
311 void log_send(struct file_struct *file, struct stats *initial_stats)
313 extern int module_id;
314 extern int am_server;
315 extern char *log_format;
317 if (lp_transfer_logging(module_id)) {
318 log_formatted(FLOG, lp_log_format(module_id), "send", file, initial_stats);
319 } else if (log_format && !am_server) {
320 log_formatted(FINFO, log_format, "send", file, initial_stats);
324 /* log the incoming transfer of a file */
325 void log_recv(struct file_struct *file, struct stats *initial_stats)
327 extern int module_id;
328 extern int am_server;
329 extern char *log_format;
331 if (lp_transfer_logging(module_id)) {
332 log_formatted(FLOG, lp_log_format(module_id), "recv", file, initial_stats);
333 } else if (log_format && !am_server) {
334 log_formatted(FINFO, log_format, "recv", file, initial_stats);
338 /* called when the transfer is interrupted for some reason */
339 void log_exit(int code, const char *file, int line)
342 extern struct stats stats;
343 rprintf(FLOG,"wrote %.0f bytes read %.0f bytes total size %.0f\n",
344 (double)stats.total_written,
345 (double)stats.total_read,
346 (double)stats.total_size);
348 rprintf(FLOG,"transfer interrupted (code %d) at %s(%d)\n",
353 /* log the incoming transfer of a file for interactive use, this
354 will be called at the end where the client was run
356 it i called when a file starts to be transferred
358 void log_transfer(struct file_struct *file, const char *fname)
362 if (!verbose) return;
364 rprintf(FINFO,"%s\n", fname);