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 /****************************************************************************
30 return the date and time as a string
31 ****************************************************************************/
32 static char *timestring(void )
34 static char TimeBuf[200];
35 time_t t = time(NULL);
36 struct tm *tm = localtime(&t);
39 strftime(TimeBuf,sizeof(TimeBuf)-1,"%Y/%m/%d %T",tm);
41 strlcpy(TimeBuf, asctime(tm), sizeof(TimeBuf)-1);
44 if (TimeBuf[strlen(TimeBuf)-1] == '\n') {
45 TimeBuf[strlen(TimeBuf)-1] = 0;
51 static void logit(int priority, char *buf)
54 fprintf(logfile,"%s [%d] %s",
55 timestring(), (int)getpid(), buf);
58 syslog(priority, "%s", buf);
64 static int initialised;
65 int options = LOG_PID;
69 if (initialised) return;
72 /* this looks pointless, but it is needed in order for the
73 C library on some systems to fetch the timezone info
78 /* optionally use a log file instead of syslog */
81 extern int orig_umask;
82 int old_umask = umask(022 | orig_umask);
83 logfile = fopen(logf, "a");
89 options |= LOG_NDELAY;
93 openlog("rsyncd", options, lp_syslog_facility());
95 openlog("rsyncd", options);
99 logit(LOG_INFO,"rsyncd started\n");
104 /* this is the rsync debugging function. Call it with FINFO, FERROR or FLOG */
105 void rprintf(int fd, const char *format, ...)
111 extern int am_daemon;
112 /* recursion can happen with certain fatal conditions */
114 va_start(ap, format);
115 len = vslprintf(buf, sizeof(buf)-1, format, ap);
118 if (len < 0) exit_cleanup(1);
120 if (len > sizeof(buf)-1) exit_cleanup(1);
125 if (am_daemon) logit(LOG_INFO, buf);
131 int priority = LOG_INFO;
132 if (fd == FERROR) priority = LOG_WARNING;
139 if (!io_multiplex_write(fd, buf, strlen(buf))) {
140 logit(priority, buf);
152 extern int am_server;
159 if (!f) exit_cleanup(1);
161 if (fwrite(buf, len, 1, f) != 1) exit_cleanup(1);
163 if (buf[len-1] == '\r' || buf[len-1] == '\n') fflush(f);
169 extern int am_daemon;
184 extern int am_server;
191 if (!f) exit_cleanup(1);
197 /* a generic logging routine for send/recv, with parameter
199 static void log_formatted(char *op, struct file_struct *file)
201 extern int module_id;
202 extern char *auth_user;
208 strlcpy(buf, lp_log_format(module_id), sizeof(buf)-1);
211 s && (p=strchr(s,'%')); ) {
216 case 'h': n = client_name(0); break;
217 case 'a': n = client_addr(0); break;
219 slprintf(buf2,sizeof(buf2)-1,"%.0f",
220 (double)file->length);
224 slprintf(buf2,sizeof(buf2)-1,"%d",
228 case 'o': n = op; break;
229 case 'f': n = f_name(file); break;
230 case 'm': n = lp_name(module_id); break;
231 case 'P': n = lp_path(module_id); break;
232 case 'u': n = auth_user; break;
239 if ((l-1) + ((int)(s - &buf[0])) > sizeof(buf)) {
240 rprintf(FERROR,"buffer overflow expanding %%%c - exiting\n",
246 memmove(s+(l-1), s+1, strlen(s+1)+1);
253 rprintf(FLOG,"%s\n", buf);
256 /* log the outgoing transfer of a file */
257 void log_send(struct file_struct *file)
259 extern int module_id;
260 if (lp_transfer_logging(module_id)) {
261 log_formatted("send", file);
265 /* log the incoming transfer of a file */
266 void log_recv(struct file_struct *file)
268 extern int module_id;
269 if (lp_transfer_logging(module_id)) {
270 log_formatted("recv", file);
274 /* called when the transfer is interrupted for some reason */
275 void log_exit(int code)
278 extern struct stats stats;
279 rprintf(FLOG,"wrote %.0f bytes read %.0f bytes total size %.0f\n",
280 (double)stats.total_written,
281 (double)stats.total_read,
282 (double)stats.total_size);
284 rprintf(FLOG,"transfer interrupted\n");
288 /* log the incoming transfer of a file for interactive use, this
289 will be called at the end where the client was run */
290 void log_transfer(struct file_struct *file, char *fname)
294 if (!verbose) return;
296 rprintf(FINFO,"%s\n", fname);