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
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");
82 /* this is the rsync debugging function. Call it with FINFO, FERROR or FLOG */
83 void rprintf(int fd, const char *format, ...)
90 /* recursion can happen with certain fatal conditions */
93 len = vslprintf(buf, sizeof(buf)-1, format, ap);
96 if (len < 0) exit_cleanup(1);
98 if (len > sizeof(buf)-1) exit_cleanup(1);
103 if (am_daemon) logit(LOG_INFO, buf);
109 int priority = LOG_INFO;
110 if (fd == FERROR) priority = LOG_WARNING;
117 if (!io_multiplex_write(fd, buf, strlen(buf))) {
118 logit(priority, buf);
130 extern int am_server;
137 if (!f) exit_cleanup(1);
139 if (fwrite(buf, len, 1, f) != 1) exit_cleanup(1);
141 if (buf[len-1] == '\r' || buf[len-1] == '\n') fflush(f);
147 extern int am_daemon;
162 extern int am_server;
169 if (!f) exit_cleanup(1);
175 /* a generic logging routine for send/recv, with parameter
177 static void log_formatted(char *op, struct file_struct *file,
178 struct stats *initial_stats)
180 extern int module_id;
181 extern char *auth_user;
186 extern struct stats stats;
187 extern int am_sender;
190 strlcpy(buf, lp_log_format(module_id), sizeof(buf)-1);
193 s && (p=strchr(s,'%')); ) {
198 case 'h': n = client_name(0); break;
199 case 'a': n = client_addr(0); break;
201 slprintf(buf2,sizeof(buf2)-1,"%.0f",
202 (double)file->length);
206 slprintf(buf2,sizeof(buf2)-1,"%d",
210 case 'o': n = op; break;
212 slprintf(buf2, sizeof(buf2)-1, "%s/%s",
213 file->basedir?file->basedir:"",
218 case 'm': n = lp_name(module_id); break;
219 case 'P': n = lp_path(module_id); break;
220 case 'u': n = auth_user; break;
223 b = stats.total_written -
224 initial_stats->total_written;
226 b = stats.total_read -
227 initial_stats->total_read;
229 slprintf(buf2,sizeof(buf2)-1,"%.0f", (double)b);
234 b = stats.total_written -
235 initial_stats->total_written;
237 b = stats.total_read -
238 initial_stats->total_read;
240 slprintf(buf2,sizeof(buf2)-1,"%.0f", (double)b);
249 if ((l-1) + ((int)(s - &buf[0])) > sizeof(buf)) {
250 rprintf(FERROR,"buffer overflow expanding %%%c - exiting\n",
256 memmove(s+(l-1), s+1, strlen(s+1)+1);
263 rprintf(FLOG,"%s\n", buf);
266 /* log the outgoing transfer of a file */
267 void log_send(struct file_struct *file, struct stats *initial_stats)
269 extern int module_id;
270 if (lp_transfer_logging(module_id)) {
271 log_formatted("send", file, initial_stats);
275 /* log the incoming transfer of a file */
276 void log_recv(struct file_struct *file, struct stats *initial_stats)
278 extern int module_id;
279 if (lp_transfer_logging(module_id)) {
280 log_formatted("recv", file, initial_stats);
284 /* called when the transfer is interrupted for some reason */
285 void log_exit(int code)
288 extern struct stats stats;
289 rprintf(FLOG,"wrote %.0f bytes read %.0f bytes total size %.0f\n",
290 (double)stats.total_written,
291 (double)stats.total_read,
292 (double)stats.total_size);
294 rprintf(FLOG,"transfer interrupted\n");
298 /* log the incoming transfer of a file for interactive use, this
299 will be called at the end where the client was run */
300 void log_transfer(struct file_struct *file, char *fname)
304 if (!verbose) return;
306 rprintf(FINFO,"%s\n", fname);