don't clobber argv[0], so ps shows the right thing
[rsync/rsync.git] / log.c
CommitLineData
0b76cd63
AT
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
4f6325c3 26static FILE *logfile;
554e0a8d 27static int log_error_fd = -1;
e0414f42 28
4f6325c3
AT
29static void logit(int priority, char *buf)
30{
31 if (logfile) {
67ea0d48 32 fprintf(logfile,"%s [%d] %s",
f7632fc6 33 timestring(time(NULL)), (int)getpid(), buf);
4f6325c3
AT
34 fflush(logfile);
35 } else {
36 syslog(priority, "%s", buf);
37 }
38}
e42c9458 39
1a016bfd
AT
40void log_open(void)
41{
42 static int initialised;
43 int options = LOG_PID;
bcf5b133 44 time_t t;
27d3cdbc 45 char *logf;
1a016bfd
AT
46
47 if (initialised) return;
48 initialised = 1;
49
958f3735
AT
50 /* this looks pointless, but it is needed in order for the
51 C library on some systems to fetch the timezone info
52 before the chroot */
53 t = time(NULL);
54 localtime(&t);
55
56 /* optionally use a log file instead of syslog */
27d3cdbc
AT
57 logf = lp_log_file();
58 if (logf && *logf) {
e8030905
AT
59 extern int orig_umask;
60 int old_umask = umask(022 | orig_umask);
27d3cdbc 61 logfile = fopen(logf, "a");
6265551a 62 umask(old_umask);
4f6325c3
AT
63 return;
64 }
65
1a016bfd
AT
66#ifdef LOG_NDELAY
67 options |= LOG_NDELAY;
68#endif
69
70#ifdef LOG_DAEMON
71 openlog("rsyncd", options, lp_syslog_facility());
72#else
73 openlog("rsyncd", options);
74#endif
75
76#ifndef LOG_NDELAY
4f6325c3 77 logit(LOG_INFO,"rsyncd started\n");
1a016bfd
AT
78#endif
79}
1a016bfd 80
554e0a8d
AT
81/* setup the error file descriptor - used when we are a server
82 that is receiving files */
83void set_error_fd(int fd)
84{
85 log_error_fd = fd;
86}
87
88/* this is the underlying (unformatted) rsync debugging function. Call
89 it with FINFO, FERROR or FLOG */
ff41a59f 90void rwrite(enum logcode code, char *buf, int len)
0b76cd63 91{
0b76cd63
AT
92 FILE *f=NULL;
93 extern int am_daemon;
4a814638 94 extern int am_server;
b86f0cef 95 extern int quiet;
8d9dc9f9 96 /* recursion can happen with certain fatal conditions */
8d9dc9f9 97
6d7b6081 98 if (quiet && code == FINFO) return;
b86f0cef 99
65417579 100 if (len < 0) exit_cleanup(RERR_MESSAGEIO);
0b76cd63 101
ff8b29b8
AT
102 buf[len] = 0;
103
ff41a59f 104 if (code == FLOG) {
11a5a3c7 105 if (am_daemon) logit(LOG_INFO, buf);
11a5a3c7
AT
106 return;
107 }
108
4a814638
AT
109 /* first try to pass it off the our sibling */
110 if (am_server && io_error_write(log_error_fd, code, buf, len)) {
6d7b6081
AT
111 return;
112 }
113
4a814638
AT
114 /* then try to pass it to the other end */
115 if (am_server && io_multiplex_write(code, buf, len)) {
6d7b6081
AT
116 return;
117 }
ff41a59f 118
ff8b29b8 119 if (am_daemon) {
b24203b3 120 static int depth;
ff8b29b8 121 int priority = LOG_INFO;
ff41a59f 122 if (code == FERROR) priority = LOG_WARNING;
45ccc5c0 123
b24203b3
AT
124 if (depth) return;
125
f27b53f5
AT
126 depth++;
127
1a016bfd 128 log_open();
6d7b6081 129 logit(priority, buf);
8d9dc9f9
AT
130
131 depth--;
ff8b29b8 132 return;
0b76cd63
AT
133 }
134
ff41a59f 135 if (code == FERROR) {
ff8b29b8
AT
136 f = stderr;
137 }
138
ff41a59f 139 if (code == FINFO) {
ff8b29b8
AT
140 if (am_server)
141 f = stderr;
142 else
143 f = stdout;
144 }
145
65417579 146 if (!f) exit_cleanup(RERR_MESSAGEIO);
0b76cd63 147
65417579 148 if (fwrite(buf, len, 1, f) != 1) exit_cleanup(RERR_MESSAGEIO);
8d9dc9f9 149
bcf5b133 150 if (buf[len-1] == '\r' || buf[len-1] == '\n') fflush(f);
0b76cd63 151}
554e0a8d
AT
152
153
154/* this is the rsync debugging function. Call it with FINFO, FERROR or FLOG */
ff41a59f 155 void rprintf(enum logcode code, const char *format, ...)
554e0a8d
AT
156{
157 va_list ap;
158 char buf[1024];
159 int len;
160
161 va_start(ap, format);
162 len = vslprintf(buf, sizeof(buf), format, ap);
163 va_end(ap);
164
165 if (len > sizeof(buf)-1) exit_cleanup(RERR_MESSAGEIO);
166
ff41a59f 167 rwrite(code, buf, len);
554e0a8d 168}
0b76cd63 169
ff41a59f 170void rflush(enum logcode code)
0b76cd63
AT
171{
172 FILE *f = NULL;
173 extern int am_daemon;
174
175 if (am_daemon) {
176 return;
177 }
178
ff41a59f 179 if (code == FLOG) {
e08bfe12
AT
180 return;
181 }
182
ff41a59f 183 if (code == FERROR) {
0b76cd63
AT
184 f = stderr;
185 }
186
ff41a59f 187 if (code == FINFO) {
0b76cd63
AT
188 extern int am_server;
189 if (am_server)
190 f = stderr;
191 else
192 f = stdout;
193 }
194
65417579 195 if (!f) exit_cleanup(RERR_MESSAGEIO);
0b76cd63
AT
196 fflush(f);
197}
198
11a5a3c7 199
e08bfe12
AT
200
201/* a generic logging routine for send/recv, with parameter
202 substitiution */
0f3203c3 203static void log_formatted(enum logcode code,
b6062654 204 char *format, char *op, struct file_struct *file,
1b7c47cb 205 struct stats *initial_stats)
e08bfe12
AT
206{
207 extern int module_id;
97cb8dc2 208 extern char *auth_user;
e08bfe12 209 char buf[1024];
ab7104da 210 char buf2[1024];
e08bfe12 211 char *p, *s, *n;
e08bfe12 212 int l;
1b7c47cb
AT
213 extern struct stats stats;
214 extern int am_sender;
af77cc6b 215 extern int am_daemon;
1b7c47cb 216 int64 b;
e08bfe12 217
37f9805d 218 strlcpy(buf, format, sizeof(buf));
e08bfe12
AT
219
220 for (s=&buf[0];
221 s && (p=strchr(s,'%')); ) {
222 n = NULL;
223 s = p + 1;
224
225 switch (p[1]) {
af77cc6b
AT
226 case 'h': if (am_daemon) n = client_name(0); break;
227 case 'a': if (am_daemon) n = client_addr(0); break;
e08bfe12 228 case 'l':
37f9805d 229 slprintf(buf2,sizeof(buf2),"%.0f",
e08bfe12
AT
230 (double)file->length);
231 n = buf2;
232 break;
233 case 'p':
37f9805d 234 slprintf(buf2,sizeof(buf2),"%d",
e08bfe12
AT
235 (int)getpid());
236 n = buf2;
237 break;
238 case 'o': n = op; break;
ab7104da 239 case 'f':
37f9805d 240 slprintf(buf2, sizeof(buf2), "%s/%s",
ab7104da
AT
241 file->basedir?file->basedir:"",
242 f_name(file));
243 clean_fname(buf2);
244 n = buf2;
b6062654 245 if (*n == '/') n++;
ab7104da 246 break;
97cb8dc2 247 case 'm': n = lp_name(module_id); break;
b6062654 248 case 't': n = timestring(time(NULL)); break;
97cb8dc2
AT
249 case 'P': n = lp_path(module_id); break;
250 case 'u': n = auth_user; break;
1b7c47cb
AT
251 case 'b':
252 if (am_sender) {
253 b = stats.total_written -
254 initial_stats->total_written;
255 } else {
256 b = stats.total_read -
257 initial_stats->total_read;
258 }
37f9805d 259 slprintf(buf2,sizeof(buf2),"%.0f", (double)b);
1b7c47cb
AT
260 n = buf2;
261 break;
262 case 'c':
263 if (!am_sender) {
264 b = stats.total_written -
265 initial_stats->total_written;
266 } else {
267 b = stats.total_read -
268 initial_stats->total_read;
269 }
37f9805d 270 slprintf(buf2,sizeof(buf2),"%.0f", (double)b);
1b7c47cb
AT
271 n = buf2;
272 break;
e08bfe12
AT
273 }
274
275 if (!n) continue;
276
277 l = strlen(n);
278
279 if ((l-1) + ((int)(s - &buf[0])) > sizeof(buf)) {
280 rprintf(FERROR,"buffer overflow expanding %%%c - exiting\n",
281 p[0]);
65417579 282 exit_cleanup(RERR_MESSAGEIO);
e08bfe12
AT
283 }
284
285 if (l != 2) {
34720097 286 memmove(s+(l-1), s+1, strlen(s+1)+1);
e08bfe12
AT
287 }
288 memcpy(p, n, l);
289
290 s = p+l;
291 }
292
0f3203c3 293 rprintf(code,"%s\n", buf);
e08bfe12
AT
294}
295
11a5a3c7 296/* log the outgoing transfer of a file */
1b7c47cb 297void log_send(struct file_struct *file, struct stats *initial_stats)
11a5a3c7
AT
298{
299 extern int module_id;
b6062654
AT
300 extern int am_server;
301 extern char *log_format;
302
11a5a3c7 303 if (lp_transfer_logging(module_id)) {
b6062654
AT
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);
11a5a3c7
AT
307 }
308}
309
310/* log the incoming transfer of a file */
1b7c47cb 311void log_recv(struct file_struct *file, struct stats *initial_stats)
11a5a3c7
AT
312{
313 extern int module_id;
b6062654
AT
314 extern int am_server;
315 extern char *log_format;
316
11a5a3c7 317 if (lp_transfer_logging(module_id)) {
117af102 318 log_formatted(FLOG, lp_log_format(module_id), "recv", file, initial_stats);
b6062654 319 } else if (log_format && !am_server) {
117af102 320 log_formatted(FINFO, log_format, "recv", file, initial_stats);
11a5a3c7
AT
321 }
322}
323
9b73d1c0 324/* called when the transfer is interrupted for some reason */
a9766ef1 325void log_exit(int code, const char *file, int line)
9b73d1c0
AT
326{
327 if (code == 0) {
328 extern struct stats stats;
67ea0d48 329 rprintf(FLOG,"wrote %.0f bytes read %.0f bytes total size %.0f\n",
9b73d1c0
AT
330 (double)stats.total_written,
331 (double)stats.total_read,
332 (double)stats.total_size);
333 } else {
a9766ef1
AT
334 rprintf(FLOG,"transfer interrupted (code %d) at %s(%d)\n",
335 code, file, line);
9b73d1c0
AT
336 }
337}
338
11a5a3c7 339/* log the incoming transfer of a file for interactive use, this
b6062654
AT
340 will be called at the end where the client was run
341
342 it i called when a file starts to be transferred
343*/
a9766ef1 344void log_transfer(struct file_struct *file, const char *fname)
11a5a3c7
AT
345{
346 extern int verbose;
347
348 if (!verbose) return;
349
350 rprintf(FINFO,"%s\n", fname);
351}
9b73d1c0 352