added copyright notice from Patrick Powell
[rsync/rsync.git] / log.c
... / ...
CommitLineData
1/*
2 Copyright (C) Andrew Tridgell 1998
3
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.
8
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.
13
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.
17*/
18
19/*
20 logging and utility functions
21
22 tridge, May 1998
23 */
24#include "rsync.h"
25
26static FILE *logfile;
27
28
29/****************************************************************************
30 return the date and time as a string
31****************************************************************************/
32static char *timestring(void )
33{
34 static char TimeBuf[200];
35 time_t t = time(NULL);
36 struct tm *tm = localtime(&t);
37
38#ifdef HAVE_STRFTIME
39 strftime(TimeBuf,sizeof(TimeBuf)-1,"%Y/%m/%d %T",tm);
40#else
41 strlcpy(TimeBuf, asctime(tm), sizeof(TimeBuf)-1);
42#endif
43
44 if (TimeBuf[strlen(TimeBuf)-1] == '\n') {
45 TimeBuf[strlen(TimeBuf)-1] = 0;
46 }
47
48 return(TimeBuf);
49}
50
51static void logit(int priority, char *buf)
52{
53 if (logfile) {
54 fprintf(logfile,"%s [%d] %s",
55 timestring(), (int)getpid(), buf);
56 fflush(logfile);
57 } else {
58 syslog(priority, "%s", buf);
59 }
60}
61
62void log_open(void)
63{
64 static int initialised;
65 int options = LOG_PID;
66 time_t t;
67 char *logf;
68
69 if (initialised) return;
70 initialised = 1;
71
72 /* this looks pointless, but it is needed in order for the
73 C library on some systems to fetch the timezone info
74 before the chroot */
75 t = time(NULL);
76 localtime(&t);
77
78 /* optionally use a log file instead of syslog */
79 logf = lp_log_file();
80 if (logf && *logf) {
81 extern int orig_umask;
82 int old_umask = umask(022 | orig_umask);
83 logfile = fopen(logf, "a");
84 umask(old_umask);
85 return;
86 }
87
88#ifdef LOG_NDELAY
89 options |= LOG_NDELAY;
90#endif
91
92#ifdef LOG_DAEMON
93 openlog("rsyncd", options, lp_syslog_facility());
94#else
95 openlog("rsyncd", options);
96#endif
97
98#ifndef LOG_NDELAY
99 logit(LOG_INFO,"rsyncd started\n");
100#endif
101}
102
103
104/* this is the rsync debugging function. Call it with FINFO, FERROR or FLOG */
105 void rprintf(int fd, const char *format, ...)
106{
107 va_list ap;
108 char buf[1024];
109 int len;
110 FILE *f=NULL;
111 extern int am_daemon;
112 /* recursion can happen with certain fatal conditions */
113
114 va_start(ap, format);
115 len = vslprintf(buf, sizeof(buf)-1, format, ap);
116 va_end(ap);
117
118 if (len < 0) exit_cleanup(1);
119
120 if (len > sizeof(buf)-1) exit_cleanup(1);
121
122 buf[len] = 0;
123
124 if (fd == FLOG) {
125 if (am_daemon) logit(LOG_INFO, buf);
126 return;
127 }
128
129 if (am_daemon) {
130 static int depth;
131 int priority = LOG_INFO;
132 if (fd == FERROR) priority = LOG_WARNING;
133
134 if (depth) return;
135
136 depth++;
137
138 log_open();
139 if (!io_multiplex_write(fd, buf, strlen(buf))) {
140 logit(priority, buf);
141 }
142
143 depth--;
144 return;
145 }
146
147 if (fd == FERROR) {
148 f = stderr;
149 }
150
151 if (fd == FINFO) {
152 extern int am_server;
153 if (am_server)
154 f = stderr;
155 else
156 f = stdout;
157 }
158
159 if (!f) exit_cleanup(1);
160
161 if (fwrite(buf, len, 1, f) != 1) exit_cleanup(1);
162
163 if (buf[len-1] == '\r' || buf[len-1] == '\n') fflush(f);
164}
165
166void rflush(int fd)
167{
168 FILE *f = NULL;
169 extern int am_daemon;
170
171 if (am_daemon) {
172 return;
173 }
174
175 if (fd == FLOG) {
176 return;
177 }
178
179 if (fd == FERROR) {
180 f = stderr;
181 }
182
183 if (fd == FINFO) {
184 extern int am_server;
185 if (am_server)
186 f = stderr;
187 else
188 f = stdout;
189 }
190
191 if (!f) exit_cleanup(1);
192 fflush(f);
193}
194
195
196
197/* a generic logging routine for send/recv, with parameter
198 substitiution */
199static void log_formatted(char *op, struct file_struct *file)
200{
201 extern int module_id;
202 extern char *auth_user;
203 char buf[1024];
204 char *p, *s, *n;
205 char buf2[100];
206 int l;
207
208 strlcpy(buf, lp_log_format(module_id), sizeof(buf)-1);
209
210 for (s=&buf[0];
211 s && (p=strchr(s,'%')); ) {
212 n = NULL;
213 s = p + 1;
214
215 switch (p[1]) {
216 case 'h': n = client_name(0); break;
217 case 'a': n = client_addr(0); break;
218 case 'l':
219 slprintf(buf2,sizeof(buf2)-1,"%.0f",
220 (double)file->length);
221 n = buf2;
222 break;
223 case 'p':
224 slprintf(buf2,sizeof(buf2)-1,"%d",
225 (int)getpid());
226 n = buf2;
227 break;
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;
233 }
234
235 if (!n) continue;
236
237 l = strlen(n);
238
239 if ((l-1) + ((int)(s - &buf[0])) > sizeof(buf)) {
240 rprintf(FERROR,"buffer overflow expanding %%%c - exiting\n",
241 p[0]);
242 exit_cleanup(1);
243 }
244
245 if (l != 2) {
246 memmove(s+(l-1), s+1, strlen(s+1)+1);
247 }
248 memcpy(p, n, l);
249
250 s = p+l;
251 }
252
253 rprintf(FLOG,"%s\n", buf);
254}
255
256/* log the outgoing transfer of a file */
257void log_send(struct file_struct *file)
258{
259 extern int module_id;
260 if (lp_transfer_logging(module_id)) {
261 log_formatted("send", file);
262 }
263}
264
265/* log the incoming transfer of a file */
266void log_recv(struct file_struct *file)
267{
268 extern int module_id;
269 if (lp_transfer_logging(module_id)) {
270 log_formatted("recv", file);
271 }
272}
273
274/* called when the transfer is interrupted for some reason */
275void log_exit(int code)
276{
277 if (code == 0) {
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);
283 } else {
284 rprintf(FLOG,"transfer interrupted\n");
285 }
286}
287
288/* log the incoming transfer of a file for interactive use, this
289 will be called at the end where the client was run */
290void log_transfer(struct file_struct *file, char *fname)
291{
292 extern int verbose;
293
294 if (!verbose) return;
295
296 rprintf(FINFO,"%s\n", fname);
297}
298