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)
35 fprintf(logfile,"%s [%d] %s",
36 timestring(time(NULL)), (int)getpid(), buf);
39 syslog(priority, "%s", buf);
45 static int initialised;
46 int options = LOG_PID;
49 if (initialised) return;
52 /* this looks pointless, but it is needed in order for the
53 C library on some systems to fetch the timezone info
58 /* optionally use a log file instead of syslog */
59 logfname = lp_log_file();
69 options |= LOG_NDELAY;
73 openlog("rsyncd", options, lp_syslog_facility());
75 openlog("rsyncd", options);
79 logit(LOG_INFO,"rsyncd started\n");
85 if (logfname && !logfile) {
86 extern int orig_umask;
87 int old_umask = umask(022 | orig_umask);
88 logfile = fopen(logfname, "a");
101 /* setup the error file descriptor - used when we are a server
102 that is receiving files */
103 void set_error_fd(int fd)
108 /* this is the underlying (unformatted) rsync debugging function. Call
109 it with FINFO, FERROR or FLOG */
110 void rwrite(enum logcode code, char *buf, int len)
113 extern int am_daemon;
114 extern int am_server;
116 /* recursion can happen with certain fatal conditions */
118 if (quiet && code == FINFO) return;
120 if (len < 0) exit_cleanup(RERR_MESSAGEIO);
125 if (am_daemon) logit(LOG_INFO, buf);
129 /* first try to pass it off the our sibling */
130 if (am_server && io_error_write(log_error_fd, code, buf, len)) {
134 /* then try to pass it to the other end */
135 if (am_server && io_multiplex_write(code, buf, len)) {
141 int priority = LOG_INFO;
142 if (code == FERROR) priority = LOG_WARNING;
149 logit(priority, buf);
155 if (code == FERROR) {
166 if (!f) exit_cleanup(RERR_MESSAGEIO);
168 if (fwrite(buf, len, 1, f) != 1) exit_cleanup(RERR_MESSAGEIO);
170 if (buf[len-1] == '\r' || buf[len-1] == '\n') fflush(f);
174 /* this is the rsync debugging function. Call it with FINFO, FERROR or FLOG */
175 void rprintf(enum logcode code, const char *format, ...)
181 va_start(ap, format);
182 len = vslprintf(buf, sizeof(buf), format, ap);
185 if (len > sizeof(buf)-1) exit_cleanup(RERR_MESSAGEIO);
187 rwrite(code, buf, len);
190 void rflush(enum logcode code)
193 extern int am_daemon;
203 if (code == FERROR) {
208 extern int am_server;
215 if (!f) exit_cleanup(RERR_MESSAGEIO);
221 /* a generic logging routine for send/recv, with parameter
223 static void log_formatted(enum logcode code,
224 char *format, char *op, struct file_struct *file,
225 struct stats *initial_stats)
227 extern int module_id;
228 extern char *auth_user;
233 extern struct stats stats;
234 extern int am_sender;
235 extern int am_daemon;
238 strlcpy(buf, format, sizeof(buf));
241 s && (p=strchr(s,'%')); ) {
246 case 'h': if (am_daemon) n = client_name(0); break;
247 case 'a': if (am_daemon) n = client_addr(0); break;
249 slprintf(buf2,sizeof(buf2),"%.0f",
250 (double)file->length);
254 slprintf(buf2,sizeof(buf2),"%d",
258 case 'o': n = op; break;
260 slprintf(buf2, sizeof(buf2), "%s/%s",
261 file->basedir?file->basedir:"",
267 case 'm': n = lp_name(module_id); break;
268 case 't': n = timestring(time(NULL)); break;
269 case 'P': n = lp_path(module_id); break;
270 case 'u': n = auth_user; break;
273 b = stats.total_written -
274 initial_stats->total_written;
276 b = stats.total_read -
277 initial_stats->total_read;
279 slprintf(buf2,sizeof(buf2),"%.0f", (double)b);
284 b = stats.total_written -
285 initial_stats->total_written;
287 b = stats.total_read -
288 initial_stats->total_read;
290 slprintf(buf2,sizeof(buf2),"%.0f", (double)b);
299 if ((l-1) + ((int)(s - &buf[0])) > sizeof(buf)) {
300 rprintf(FERROR,"buffer overflow expanding %%%c - exiting\n",
302 exit_cleanup(RERR_MESSAGEIO);
306 memmove(s+(l-1), s+1, strlen(s+1)+1);
313 rprintf(code,"%s\n", buf);
316 /* log the outgoing transfer of a file */
317 void log_send(struct file_struct *file, struct stats *initial_stats)
319 extern int module_id;
320 extern int am_server;
321 extern char *log_format;
323 if (lp_transfer_logging(module_id)) {
324 log_formatted(FLOG, lp_log_format(module_id), "send", file, initial_stats);
325 } else if (log_format && !am_server) {
326 log_formatted(FINFO, log_format, "send", file, initial_stats);
330 /* log the incoming transfer of a file */
331 void log_recv(struct file_struct *file, struct stats *initial_stats)
333 extern int module_id;
334 extern int am_server;
335 extern char *log_format;
337 if (lp_transfer_logging(module_id)) {
338 log_formatted(FLOG, lp_log_format(module_id), "recv", file, initial_stats);
339 } else if (log_format && !am_server) {
340 log_formatted(FINFO, log_format, "recv", file, initial_stats);
344 /* called when the transfer is interrupted for some reason */
345 void log_exit(int code, const char *file, int line)
348 extern struct stats stats;
349 rprintf(FLOG,"wrote %.0f bytes read %.0f bytes total size %.0f\n",
350 (double)stats.total_written,
351 (double)stats.total_read,
352 (double)stats.total_size);
354 rprintf(FLOG,"transfer interrupted (code %d) at %s(%d)\n",
359 /* log the incoming transfer of a file for interactive use, this
360 will be called at the end where the client was run
362 it i called when a file starts to be transferred
364 void log_transfer(struct file_struct *file, const char *fname)
368 if (!verbose) return;
370 rprintf(FINFO,"%s\n", fname);