blob: e67477b73b6b56c03370043f2d320496c1d0fd0d [file] [log] [blame]
Martin Poola0397492000-10-26 07:24:18 +00001/* -*- c-file-style: "linux"; -*-
2
Martin Pool0c5a7922001-03-19 08:19:11 +00003 Copyright (C) 1998-2001 by Andrew Tridgell <tridge@samba.org>
Martin Pool18c71e92001-02-23 01:45:46 +00004 Copyright (C) 2000-2001 by Martin Pool <mbp@samba.org>
Andrew Tridgell0b76cd61998-05-09 14:00:55 +00005
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19*/
20
21/*
Martin Pool3e3dcd62001-02-21 07:18:50 +000022 Logging and utility functions.
Martin Pool0c5a7922001-03-19 08:19:11 +000023 tridge, May 1998
Andrew Tridgell0b76cd61998-05-09 14:00:55 +000024
Martin Pool3e3dcd62001-02-21 07:18:50 +000025 Mapping to human-readable messages added by Martin Pool
26 <mbp@samba.org>, Oct 2000.
Andrew Tridgell0b76cd61998-05-09 14:00:55 +000027 */
28#include "rsync.h"
29
David Dykstra45a83542000-10-24 18:50:08 +000030static char *logfname;
Andrew Tridgell4f6325c1998-09-09 05:51:42 +000031static FILE *logfile;
Andrew Tridgell554e0a82000-01-23 07:36:56 +000032static int log_error_fd = -1;
Andrew Tridgelle0414f41998-09-09 06:06:08 +000033
Andrew Tridgellff81e802001-03-22 07:36:51 +000034int log_got_error=0;
Martin Poolaf642a62000-11-10 03:28:15 +000035
36struct {
37 int code;
38 char const *name;
39} const rerr_names[] = {
40 { RERR_SYNTAX , "syntax or usage error" },
41 { RERR_PROTOCOL , "protocol incompatibility" },
42 { RERR_FILESELECT , "errors selecting input/output files, dirs" },
Andrew Tridgell19b27a42001-03-23 01:26:04 +000043 { RERR_UNSUPPORTED, "requested action not supported" },
Martin Poolaf642a62000-11-10 03:28:15 +000044 { RERR_SOCKETIO , "error in socket IO" },
45 { RERR_FILEIO , "error in file IO" },
46 { RERR_STREAMIO , "error in rsync protocol data stream" },
47 { RERR_MESSAGEIO , "errors with program diagnostics" },
48 { RERR_IPC , "error in IPC code" },
Martin Pool3e3dcd62001-02-21 07:18:50 +000049 { RERR_SIGNAL , "received SIGUSR1 or SIGINT" },
Martin Poolaf642a62000-11-10 03:28:15 +000050 { RERR_WAITCHILD , "some error returned by waitpid()" },
51 { RERR_MALLOC , "error allocating core memory buffers" },
Andrew Tridgell19b27a42001-03-23 01:26:04 +000052 { RERR_PARTIAL , "partial transfer" },
Martin Poolaf642a62000-11-10 03:28:15 +000053 { RERR_TIMEOUT , "timeout in data send/receive" },
Andrew Tridgell19b27a42001-03-23 01:26:04 +000054 { RERR_CMD_FAILED , "remote shell failed" },
55 { RERR_CMD_KILLED , "remote shell killed" },
56 { RERR_CMD_RUN, "remote command could not be run" },
57 { RERR_CMD_NOTFOUND, "remote command not found" },
Martin Poolaf642a62000-11-10 03:28:15 +000058 { 0, NULL }
59};
60
61
62
63/*
64 * Map from rsync error code to name, or return NULL.
65 */
66static char const *rerr_name(int code)
67{
68 int i;
69 for (i = 0; rerr_names[i].name; i++) {
70 if (rerr_names[i].code == code)
71 return rerr_names[i].name;
72 }
73 return NULL;
74}
75
Andrew Tridgell90ba34e2001-03-21 13:12:44 +000076struct err_list {
77 struct err_list *next;
78 char *buf;
79 int len;
80 int written; /* how many bytes we have written so far */
81};
82
83static struct err_list *err_list_head;
84static struct err_list *err_list_tail;
85
86/* add an error message to the pending error list */
87static void err_list_add(int code, char *buf, int len)
88{
89 struct err_list *el;
90 el = (struct err_list *)malloc(sizeof(*el));
91 if (!el) exit_cleanup(RERR_MALLOC);
92 el->next = NULL;
93 el->buf = malloc(len+4);
94 if (!el->buf) exit_cleanup(RERR_MALLOC);
95 memcpy(el->buf+4, buf, len);
96 SIVAL(el->buf, 0, ((code+MPLEX_BASE)<<24) | len);
97 el->len = len+4;
98 el->written = 0;
99 if (err_list_tail) {
100 err_list_tail->next = el;
101 } else {
102 err_list_head = el;
103 }
104 err_list_tail = el;
105}
106
107
108/* try to push errors off the error list onto the wire */
109void err_list_push(void)
110{
111 if (log_error_fd == -1) return;
112
113 while (err_list_head) {
114 struct err_list *el = err_list_head;
115 int n = write(log_error_fd, el->buf+el->written, el->len - el->written);
Andrew Tridgellff81e802001-03-22 07:36:51 +0000116 /* don't check for an error if the best way of handling the error is
117 to ignore it */
Andrew Tridgell90ba34e2001-03-21 13:12:44 +0000118 if (n == -1) break;
119 if (n > 0) {
120 el->written += n;
121 }
122 if (el->written == el->len) {
123 free(el->buf);
124 err_list_head = el->next;
125 if (!err_list_head) err_list_tail = NULL;
126 free(el);
127 }
128 }
129}
130
Martin Poolaf642a62000-11-10 03:28:15 +0000131
Andrew Tridgell4f6325c1998-09-09 05:51:42 +0000132static void logit(int priority, char *buf)
133{
David Dykstra45a83542000-10-24 18:50:08 +0000134 if (logfname) {
David Dykstra15b84e12000-10-25 19:57:42 +0000135 if (!logfile)
136 log_open();
Andrew Tridgell67ea0d41998-10-29 11:16:51 +0000137 fprintf(logfile,"%s [%d] %s",
Andrew Tridgellf7632fc1998-11-02 00:52:01 +0000138 timestring(time(NULL)), (int)getpid(), buf);
Andrew Tridgell4f6325c1998-09-09 05:51:42 +0000139 fflush(logfile);
140 } else {
141 syslog(priority, "%s", buf);
142 }
143}
Andrew Tridgelle42c9451998-05-15 09:26:01 +0000144
David Dykstra45a83542000-10-24 18:50:08 +0000145void log_init(void)
Andrew Tridgell1a016bf1998-05-15 10:34:07 +0000146{
147 static int initialised;
148 int options = LOG_PID;
Andrew Tridgellbcf5b131998-07-02 10:57:20 +0000149 time_t t;
Andrew Tridgell1a016bf1998-05-15 10:34:07 +0000150
151 if (initialised) return;
152 initialised = 1;
153
Andrew Tridgell958f3731998-10-30 11:18:38 +0000154 /* this looks pointless, but it is needed in order for the
155 C library on some systems to fetch the timezone info
156 before the chroot */
157 t = time(NULL);
158 localtime(&t);
159
160 /* optionally use a log file instead of syslog */
David Dykstra45a83542000-10-24 18:50:08 +0000161 logfname = lp_log_file();
162 if (logfname) {
David Dykstra15b84e12000-10-25 19:57:42 +0000163 if (*logfname) {
164 log_open();
David Dykstra45a83542000-10-24 18:50:08 +0000165 return;
David Dykstra15b84e12000-10-25 19:57:42 +0000166 }
David Dykstra45a83542000-10-24 18:50:08 +0000167 logfname = NULL;
Andrew Tridgell4f6325c1998-09-09 05:51:42 +0000168 }
169
Andrew Tridgell1a016bf1998-05-15 10:34:07 +0000170#ifdef LOG_NDELAY
171 options |= LOG_NDELAY;
172#endif
173
174#ifdef LOG_DAEMON
175 openlog("rsyncd", options, lp_syslog_facility());
176#else
177 openlog("rsyncd", options);
178#endif
179
180#ifndef LOG_NDELAY
Andrew Tridgell4f6325c1998-09-09 05:51:42 +0000181 logit(LOG_INFO,"rsyncd started\n");
Andrew Tridgell1a016bf1998-05-15 10:34:07 +0000182#endif
183}
Andrew Tridgell1a016bf1998-05-15 10:34:07 +0000184
David Dykstra15b84e12000-10-25 19:57:42 +0000185void log_open()
186{
187 if (logfname && !logfile) {
188 extern int orig_umask;
189 int old_umask = umask(022 | orig_umask);
190 logfile = fopen(logfname, "a");
191 umask(old_umask);
192 }
193}
194
195void log_close()
David Dykstra45a83542000-10-24 18:50:08 +0000196{
197 if (logfile) {
198 fclose(logfile);
199 logfile = NULL;
200 }
201}
202
Andrew Tridgell554e0a82000-01-23 07:36:56 +0000203/* setup the error file descriptor - used when we are a server
204 that is receiving files */
205void set_error_fd(int fd)
Andrew Tridgell0b76cd61998-05-09 14:00:55 +0000206{
Andrew Tridgell554e0a82000-01-23 07:36:56 +0000207 log_error_fd = fd;
Andrew Tridgell90ba34e2001-03-21 13:12:44 +0000208 set_nonblocking(log_error_fd);
Andrew Tridgell554e0a82000-01-23 07:36:56 +0000209}
210
211/* this is the underlying (unformatted) rsync debugging function. Call
212 it with FINFO, FERROR or FLOG */
Andrew Tridgellff41a592000-01-28 15:29:59 +0000213void rwrite(enum logcode code, char *buf, int len)
Andrew Tridgell554e0a82000-01-23 07:36:56 +0000214{
Andrew Tridgell0b76cd61998-05-09 14:00:55 +0000215 FILE *f=NULL;
216 extern int am_daemon;
Andrew Tridgell4a814632000-01-29 05:16:13 +0000217 extern int am_server;
David Dykstrab86f0ce1999-02-09 17:25:35 +0000218 extern int quiet;
Andrew Tridgell8d9dc9f1998-05-18 08:20:26 +0000219 /* recursion can happen with certain fatal conditions */
Andrew Tridgell8d9dc9f1998-05-18 08:20:26 +0000220
Andrew Tridgell6d7b6082000-01-29 04:50:01 +0000221 if (quiet && code == FINFO) return;
David Dykstrab86f0ce1999-02-09 17:25:35 +0000222
Andrew Tridgell65417571998-11-03 07:08:27 +0000223 if (len < 0) exit_cleanup(RERR_MESSAGEIO);
Andrew Tridgell0b76cd61998-05-09 14:00:55 +0000224
Andrew Tridgellff8b29b1998-05-10 08:24:38 +0000225 buf[len] = 0;
Andrew Tridgell0b76cd61998-05-09 14:00:55 +0000226
Andrew Tridgellff41a592000-01-28 15:29:59 +0000227 if (code == FLOG) {
Andrew Tridgell11a5a3c1998-10-28 03:28:30 +0000228 if (am_daemon) logit(LOG_INFO, buf);
Andrew Tridgell11a5a3c1998-10-28 03:28:30 +0000229 return;
230 }
231
Andrew Tridgell90ba34e2001-03-21 13:12:44 +0000232 /* first try to pass it off to our sibling */
233 if (am_server && log_error_fd != -1) {
234 err_list_add(code, buf, len);
235 err_list_push();
Andrew Tridgell6d7b6082000-01-29 04:50:01 +0000236 return;
237 }
238
Martin Pool18c71e92001-02-23 01:45:46 +0000239 /* if that fails, try to pass it to the other end */
Andrew Tridgell4a814632000-01-29 05:16:13 +0000240 if (am_server && io_multiplex_write(code, buf, len)) {
Andrew Tridgell6d7b6082000-01-29 04:50:01 +0000241 return;
242 }
Andrew Tridgellff41a592000-01-28 15:29:59 +0000243
Andrew Tridgellff8b29b1998-05-10 08:24:38 +0000244 if (am_daemon) {
Andrew Tridgellb24203b1998-10-30 02:43:10 +0000245 static int depth;
Andrew Tridgellff8b29b1998-05-10 08:24:38 +0000246 int priority = LOG_INFO;
Andrew Tridgellff41a592000-01-28 15:29:59 +0000247 if (code == FERROR) priority = LOG_WARNING;
Andrew Tridgell45ccc5c1998-05-10 08:31:50 +0000248
Andrew Tridgellb24203b1998-10-30 02:43:10 +0000249 if (depth) return;
250
Andrew Tridgellf27b53f1998-10-30 02:32:29 +0000251 depth++;
252
David Dykstra45a83542000-10-24 18:50:08 +0000253 log_init();
Andrew Tridgell6d7b6082000-01-29 04:50:01 +0000254 logit(priority, buf);
Andrew Tridgell8d9dc9f1998-05-18 08:20:26 +0000255
256 depth--;
Andrew Tridgellff8b29b1998-05-10 08:24:38 +0000257 return;
Andrew Tridgell0b76cd61998-05-09 14:00:55 +0000258 }
259
Andrew Tridgellff41a592000-01-28 15:29:59 +0000260 if (code == FERROR) {
Andrew Tridgellff81e802001-03-22 07:36:51 +0000261 log_got_error = 1;
Andrew Tridgellff8b29b1998-05-10 08:24:38 +0000262 f = stderr;
263 }
264
Andrew Tridgellff41a592000-01-28 15:29:59 +0000265 if (code == FINFO) {
Andrew Tridgellff8b29b1998-05-10 08:24:38 +0000266 if (am_server)
267 f = stderr;
268 else
269 f = stdout;
270 }
271
Andrew Tridgell65417571998-11-03 07:08:27 +0000272 if (!f) exit_cleanup(RERR_MESSAGEIO);
Andrew Tridgell0b76cd61998-05-09 14:00:55 +0000273
Andrew Tridgell65417571998-11-03 07:08:27 +0000274 if (fwrite(buf, len, 1, f) != 1) exit_cleanup(RERR_MESSAGEIO);
Andrew Tridgell8d9dc9f1998-05-18 08:20:26 +0000275
Andrew Tridgellbcf5b131998-07-02 10:57:20 +0000276 if (buf[len-1] == '\r' || buf[len-1] == '\n') fflush(f);
Andrew Tridgell0b76cd61998-05-09 14:00:55 +0000277}
Andrew Tridgell554e0a82000-01-23 07:36:56 +0000278
279
Martin Poola0397492000-10-26 07:24:18 +0000280/* This is the rsync debugging function. Call it with FINFO, FERROR or
281 * FLOG. */
282void rprintf(enum logcode code, const char *format, ...)
Andrew Tridgell554e0a82000-01-23 07:36:56 +0000283{
284 va_list ap;
285 char buf[1024];
286 int len;
287
288 va_start(ap, format);
289 len = vslprintf(buf, sizeof(buf), format, ap);
290 va_end(ap);
291
292 if (len > sizeof(buf)-1) exit_cleanup(RERR_MESSAGEIO);
293
Andrew Tridgellff41a592000-01-28 15:29:59 +0000294 rwrite(code, buf, len);
Andrew Tridgell554e0a82000-01-23 07:36:56 +0000295}
Andrew Tridgell0b76cd61998-05-09 14:00:55 +0000296
Martin Poola0397492000-10-26 07:24:18 +0000297
298/* This is like rprintf, but it also tries to print some
299 * representation of the error code. Normally errcode = errno.
300 *
301 * Unlike rprintf, this always adds a newline and there should not be
302 * one in the format string.
303 *
304 * Note that since strerror might involve dynamically loading a
305 * message catalog we need to call it once before chroot-ing. */
306void rsyserr(enum logcode code, int errcode, const char *format, ...)
307{
308 va_list ap;
309 char buf[1024];
310 int len, sys_len;
311 char *sysmsg;
312
313 va_start(ap, format);
314 len = vslprintf(buf, sizeof(buf), format, ap);
315 va_end(ap);
316
317 if (len > sizeof(buf)-1) exit_cleanup(RERR_MESSAGEIO);
318
319 sysmsg = strerror(errcode);
320 sys_len = strlen(sysmsg);
321 if (len + 3 + sys_len > sizeof(buf) - 1)
322 exit_cleanup(RERR_MESSAGEIO);
323
324 strcpy(buf + len, ": ");
325 len += 2;
326 strcpy(buf + len, sysmsg);
327 len += sys_len;
328 strcpy(buf + len, "\n");
329 len++;
330
331 rwrite(code, buf, len);
332}
333
334
335
Andrew Tridgellff41a592000-01-28 15:29:59 +0000336void rflush(enum logcode code)
Andrew Tridgell0b76cd61998-05-09 14:00:55 +0000337{
338 FILE *f = NULL;
339 extern int am_daemon;
340
341 if (am_daemon) {
342 return;
343 }
344
Andrew Tridgellff41a592000-01-28 15:29:59 +0000345 if (code == FLOG) {
Andrew Tridgelle08bfe11998-10-30 02:24:47 +0000346 return;
347 }
348
Andrew Tridgellff41a592000-01-28 15:29:59 +0000349 if (code == FERROR) {
Andrew Tridgell0b76cd61998-05-09 14:00:55 +0000350 f = stderr;
351 }
352
Andrew Tridgellff41a592000-01-28 15:29:59 +0000353 if (code == FINFO) {
Andrew Tridgell0b76cd61998-05-09 14:00:55 +0000354 extern int am_server;
355 if (am_server)
356 f = stderr;
357 else
358 f = stdout;
359 }
360
Andrew Tridgell65417571998-11-03 07:08:27 +0000361 if (!f) exit_cleanup(RERR_MESSAGEIO);
Andrew Tridgell0b76cd61998-05-09 14:00:55 +0000362 fflush(f);
363}
364
Andrew Tridgell11a5a3c1998-10-28 03:28:30 +0000365
Andrew Tridgelle08bfe11998-10-30 02:24:47 +0000366
367/* a generic logging routine for send/recv, with parameter
368 substitiution */
Andrew Tridgell0f3203c2000-01-29 02:49:03 +0000369static void log_formatted(enum logcode code,
Andrew Tridgellb6062651998-11-03 03:48:47 +0000370 char *format, char *op, struct file_struct *file,
Andrew Tridgell1b7c47c1998-11-02 04:17:56 +0000371 struct stats *initial_stats)
Andrew Tridgelle08bfe11998-10-30 02:24:47 +0000372{
373 extern int module_id;
Andrew Tridgell97cb8dc1998-10-30 05:53:05 +0000374 extern char *auth_user;
Andrew Tridgelle08bfe11998-10-30 02:24:47 +0000375 char buf[1024];
Andrew Tridgellab7104d1998-11-02 06:40:11 +0000376 char buf2[1024];
Andrew Tridgelle08bfe11998-10-30 02:24:47 +0000377 char *p, *s, *n;
Andrew Tridgelle08bfe11998-10-30 02:24:47 +0000378 int l;
Andrew Tridgell1b7c47c1998-11-02 04:17:56 +0000379 extern struct stats stats;
380 extern int am_sender;
Andrew Tridgellaf77cc61998-11-16 23:50:28 +0000381 extern int am_daemon;
Andrew Tridgell1b7c47c1998-11-02 04:17:56 +0000382 int64 b;
Andrew Tridgelle08bfe11998-10-30 02:24:47 +0000383
Andrew Tridgell37f98051998-11-14 23:31:58 +0000384 strlcpy(buf, format, sizeof(buf));
Andrew Tridgelle08bfe11998-10-30 02:24:47 +0000385
386 for (s=&buf[0];
387 s && (p=strchr(s,'%')); ) {
388 n = NULL;
389 s = p + 1;
390
391 switch (p[1]) {
Andrew Tridgellaf77cc61998-11-16 23:50:28 +0000392 case 'h': if (am_daemon) n = client_name(0); break;
393 case 'a': if (am_daemon) n = client_addr(0); break;
Andrew Tridgelle08bfe11998-10-30 02:24:47 +0000394 case 'l':
Andrew Tridgell37f98051998-11-14 23:31:58 +0000395 slprintf(buf2,sizeof(buf2),"%.0f",
Andrew Tridgelle08bfe11998-10-30 02:24:47 +0000396 (double)file->length);
397 n = buf2;
398 break;
399 case 'p':
Andrew Tridgell37f98051998-11-14 23:31:58 +0000400 slprintf(buf2,sizeof(buf2),"%d",
Andrew Tridgelle08bfe11998-10-30 02:24:47 +0000401 (int)getpid());
402 n = buf2;
403 break;
404 case 'o': n = op; break;
Andrew Tridgellab7104d1998-11-02 06:40:11 +0000405 case 'f':
Andrew Tridgell37f98051998-11-14 23:31:58 +0000406 slprintf(buf2, sizeof(buf2), "%s/%s",
Andrew Tridgellab7104d1998-11-02 06:40:11 +0000407 file->basedir?file->basedir:"",
408 f_name(file));
409 clean_fname(buf2);
410 n = buf2;
Andrew Tridgellb6062651998-11-03 03:48:47 +0000411 if (*n == '/') n++;
Andrew Tridgellab7104d1998-11-02 06:40:11 +0000412 break;
Andrew Tridgell97cb8dc1998-10-30 05:53:05 +0000413 case 'm': n = lp_name(module_id); break;
Andrew Tridgellb6062651998-11-03 03:48:47 +0000414 case 't': n = timestring(time(NULL)); break;
Andrew Tridgell97cb8dc1998-10-30 05:53:05 +0000415 case 'P': n = lp_path(module_id); break;
416 case 'u': n = auth_user; break;
Andrew Tridgell1b7c47c1998-11-02 04:17:56 +0000417 case 'b':
418 if (am_sender) {
419 b = stats.total_written -
420 initial_stats->total_written;
421 } else {
422 b = stats.total_read -
423 initial_stats->total_read;
424 }
Andrew Tridgell37f98051998-11-14 23:31:58 +0000425 slprintf(buf2,sizeof(buf2),"%.0f", (double)b);
Andrew Tridgell1b7c47c1998-11-02 04:17:56 +0000426 n = buf2;
427 break;
428 case 'c':
429 if (!am_sender) {
430 b = stats.total_written -
431 initial_stats->total_written;
432 } else {
433 b = stats.total_read -
434 initial_stats->total_read;
435 }
Andrew Tridgell37f98051998-11-14 23:31:58 +0000436 slprintf(buf2,sizeof(buf2),"%.0f", (double)b);
Andrew Tridgell1b7c47c1998-11-02 04:17:56 +0000437 n = buf2;
438 break;
Andrew Tridgelle08bfe11998-10-30 02:24:47 +0000439 }
440
441 if (!n) continue;
442
443 l = strlen(n);
444
445 if ((l-1) + ((int)(s - &buf[0])) > sizeof(buf)) {
446 rprintf(FERROR,"buffer overflow expanding %%%c - exiting\n",
447 p[0]);
Andrew Tridgell65417571998-11-03 07:08:27 +0000448 exit_cleanup(RERR_MESSAGEIO);
Andrew Tridgelle08bfe11998-10-30 02:24:47 +0000449 }
450
451 if (l != 2) {
Andrew Tridgell34720091998-10-30 02:36:05 +0000452 memmove(s+(l-1), s+1, strlen(s+1)+1);
Andrew Tridgelle08bfe11998-10-30 02:24:47 +0000453 }
454 memcpy(p, n, l);
455
456 s = p+l;
457 }
458
Andrew Tridgell0f3203c2000-01-29 02:49:03 +0000459 rprintf(code,"%s\n", buf);
Andrew Tridgelle08bfe11998-10-30 02:24:47 +0000460}
461
Andrew Tridgell11a5a3c1998-10-28 03:28:30 +0000462/* log the outgoing transfer of a file */
Andrew Tridgell1b7c47c1998-11-02 04:17:56 +0000463void log_send(struct file_struct *file, struct stats *initial_stats)
Andrew Tridgell11a5a3c1998-10-28 03:28:30 +0000464{
465 extern int module_id;
Andrew Tridgellb6062651998-11-03 03:48:47 +0000466 extern int am_server;
467 extern char *log_format;
468
Andrew Tridgell11a5a3c1998-10-28 03:28:30 +0000469 if (lp_transfer_logging(module_id)) {
Andrew Tridgellb6062651998-11-03 03:48:47 +0000470 log_formatted(FLOG, lp_log_format(module_id), "send", file, initial_stats);
471 } else if (log_format && !am_server) {
472 log_formatted(FINFO, log_format, "send", file, initial_stats);
Andrew Tridgell11a5a3c1998-10-28 03:28:30 +0000473 }
474}
475
476/* log the incoming transfer of a file */
Andrew Tridgell1b7c47c1998-11-02 04:17:56 +0000477void log_recv(struct file_struct *file, struct stats *initial_stats)
Andrew Tridgell11a5a3c1998-10-28 03:28:30 +0000478{
479 extern int module_id;
Andrew Tridgellb6062651998-11-03 03:48:47 +0000480 extern int am_server;
481 extern char *log_format;
482
Andrew Tridgell11a5a3c1998-10-28 03:28:30 +0000483 if (lp_transfer_logging(module_id)) {
David Dykstra117af101999-01-05 20:08:45 +0000484 log_formatted(FLOG, lp_log_format(module_id), "recv", file, initial_stats);
Andrew Tridgellb6062651998-11-03 03:48:47 +0000485 } else if (log_format && !am_server) {
David Dykstra117af101999-01-05 20:08:45 +0000486 log_formatted(FINFO, log_format, "recv", file, initial_stats);
Andrew Tridgell11a5a3c1998-10-28 03:28:30 +0000487 }
488}
489
Martin Poolaf642a62000-11-10 03:28:15 +0000490
491
492
493/*
494 * Called when the transfer is interrupted for some reason.
495 *
496 * Code is one of the RERR_* codes from errcode.h, or terminating
497 * successfully.
498 */
Andrew Tridgella9766ef1998-11-15 01:21:42 +0000499void log_exit(int code, const char *file, int line)
Andrew Tridgell9b73d1c1998-10-29 11:11:38 +0000500{
501 if (code == 0) {
502 extern struct stats stats;
Andrew Tridgell67ea0d41998-10-29 11:16:51 +0000503 rprintf(FLOG,"wrote %.0f bytes read %.0f bytes total size %.0f\n",
Andrew Tridgell9b73d1c1998-10-29 11:11:38 +0000504 (double)stats.total_written,
505 (double)stats.total_read,
506 (double)stats.total_size);
507 } else {
Martin Poolaf642a62000-11-10 03:28:15 +0000508 const char *name;
509
510 name = rerr_name(code);
511 if (!name)
512 name = "unexplained error";
513
Andrew Tridgell19b27a42001-03-23 01:26:04 +0000514 rprintf(FERROR,"transfer error: %s (code %d) at %s(%d)\n",
Martin Poolaf642a62000-11-10 03:28:15 +0000515 name, code, file, line);
Andrew Tridgell9b73d1c1998-10-29 11:11:38 +0000516 }
517}
518
Martin Poolaf642a62000-11-10 03:28:15 +0000519
520
521
Andrew Tridgell11a5a3c1998-10-28 03:28:30 +0000522/* log the incoming transfer of a file for interactive use, this
Andrew Tridgellb6062651998-11-03 03:48:47 +0000523 will be called at the end where the client was run
524
525 it i called when a file starts to be transferred
526*/
Andrew Tridgella9766ef1998-11-15 01:21:42 +0000527void log_transfer(struct file_struct *file, const char *fname)
Andrew Tridgell11a5a3c1998-10-28 03:28:30 +0000528{
529 extern int verbose;
530
531 if (!verbose) return;
532
533 rprintf(FINFO,"%s\n", fname);
534}
Andrew Tridgell9b73d1c1998-10-29 11:11:38 +0000535