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)
96 /* recursion can happen with certain fatal conditions */
98 if (quiet && code == FINFO) return;
100 if (len < 0) exit_cleanup(RERR_MESSAGEIO);
105 if (am_daemon) logit(LOG_INFO, buf);
109 /* first try to pass it off the our sibling */
110 if (am_server && io_error_write(log_error_fd, code, buf, len)) {
114 /* then try to pass it to the other end */
115 if (am_server && io_multiplex_write(code, buf, len)) {
121 int priority = LOG_INFO;
122 if (code == FERROR) priority = LOG_WARNING;
129 logit(priority, buf);
135 if (code == FERROR) {
146 if (!f) exit_cleanup(RERR_MESSAGEIO);
148 if (fwrite(buf, len, 1, f) != 1) exit_cleanup(RERR_MESSAGEIO);
150 if (buf[len-1] == '\r' || buf[len-1] == '\n') fflush(f);
154 /* this is the rsync debugging function. Call it with FINFO, FERROR or FLOG */
155 void rprintf(enum logcode code, const char *format, ...)
161 va_start(ap, format);
162 len = vslprintf(buf, sizeof(buf), format, ap);
165 if (len > sizeof(buf)-1) exit_cleanup(RERR_MESSAGEIO);
167 rwrite(code, buf, len);
170 void rflush(enum logcode code)
173 extern int am_daemon;
183 if (code == FERROR) {
188 extern int am_server;
195 if (!f) exit_cleanup(RERR_MESSAGEIO);
201 /* a generic logging routine for send/recv, with parameter
203 static void log_formatted(enum logcode code,
204 char *format, char *op, struct file_struct *file,
205 struct stats *initial_stats)
207 extern int module_id;
208 extern char *auth_user;
213 extern struct stats stats;
214 extern int am_sender;
215 extern int am_daemon;
218 strlcpy(buf, format, sizeof(buf));
221 s && (p=strchr(s,'%')); ) {
226 case 'h': if (am_daemon) n = client_name(0); break;
227 case 'a': if (am_daemon) n = client_addr(0); break;
229 slprintf(buf2,sizeof(buf2),"%.0f",
230 (double)file->length);
234 slprintf(buf2,sizeof(buf2),"%d",
238 case 'o': n = op; break;
240 slprintf(buf2, sizeof(buf2), "%s/%s",
241 file->basedir?file->basedir:"",
247 case 'm': n = lp_name(module_id); break;
248 case 't': n = timestring(time(NULL)); break;
249 case 'P': n = lp_path(module_id); break;
250 case 'u': n = auth_user; break;
253 b = stats.total_written -
254 initial_stats->total_written;
256 b = stats.total_read -
257 initial_stats->total_read;
259 slprintf(buf2,sizeof(buf2),"%.0f", (double)b);
264 b = stats.total_written -
265 initial_stats->total_written;
267 b = stats.total_read -
268 initial_stats->total_read;
270 slprintf(buf2,sizeof(buf2),"%.0f", (double)b);
279 if ((l-1) + ((int)(s - &buf[0])) > sizeof(buf)) {
280 rprintf(FERROR,"buffer overflow expanding %%%c - exiting\n",
282 exit_cleanup(RERR_MESSAGEIO);
286 memmove(s+(l-1), s+1, strlen(s+1)+1);
293 rprintf(code,"%s\n", buf);
296 /* log the outgoing transfer of a file */
297 void log_send(struct file_struct *file, struct stats *initial_stats)
299 extern int module_id;
300 extern int am_server;
301 extern char *log_format;
303 if (lp_transfer_logging(module_id)) {
304 log_formatted(FLOG, lp_log_format(module_id), "send", file, initial_stats);
305 } else if (log_format && !am_server) {
306 log_formatted(FINFO, log_format, "send", file, initial_stats);
310 /* log the incoming transfer of a file */
311 void log_recv(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), "recv", file, initial_stats);
319 } else if (log_format && !am_server) {
320 log_formatted(FINFO, log_format, "recv", file, initial_stats);
324 /* called when the transfer is interrupted for some reason */
325 void log_exit(int code, const char *file, int line)
328 extern struct stats stats;
329 rprintf(FLOG,"wrote %.0f bytes read %.0f bytes total size %.0f\n",
330 (double)stats.total_written,
331 (double)stats.total_read,
332 (double)stats.total_size);
334 rprintf(FLOG,"transfer interrupted (code %d) at %s(%d)\n",
339 /* log the incoming transfer of a file for interactive use, this
340 will be called at the end where the client was run
342 it i called when a file starts to be transferred
344 void log_transfer(struct file_struct *file, const char *fname)
348 if (!verbose) return;
350 rprintf(FINFO,"%s\n", fname);