Commit | Line | Data |
---|---|---|
0b76cd63 | 1 | /* |
0f78b815 WD |
2 | * Logging and utility functions. |
3 | * | |
4 | * Copyright (C) 1998-2001 Andrew Tridgell <tridge@samba.org> | |
5 | * Copyright (C) 2000-2001 Martin Pool <mbp@samba.org> | |
b3bf9b9d | 6 | * Copyright (C) 2003-2009 Wayne Davison |
0f78b815 WD |
7 | * |
8 | * This program is free software; you can redistribute it and/or modify | |
8e41b68e WD |
9 | * it under the terms of the GNU General Public License as published by |
10 | * the Free Software Foundation; either version 3 of the License, or | |
11 | * (at your option) any later version. | |
0f78b815 WD |
12 | * |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
e7c67065 | 18 | * You should have received a copy of the GNU General Public License along |
4fd842f9 | 19 | * with this program; if not, visit the http://fsf.org website. |
0f78b815 | 20 | */ |
0b76cd63 | 21 | |
0b76cd63 | 22 | #include "rsync.h" |
5dd14f0c WD |
23 | #include "itypes.h" |
24 | #include "inums.h" | |
0b76cd63 | 25 | |
088aff1a | 26 | extern int dry_run; |
548abf96 WD |
27 | extern int am_daemon; |
28 | extern int am_server; | |
29 | extern int am_sender; | |
332cf6df | 30 | extern int am_generator; |
c1759b9f | 31 | extern int local_server; |
548abf96 WD |
32 | extern int quiet; |
33 | extern int module_id; | |
886df221 | 34 | extern int checksum_len; |
507433f6 | 35 | extern int allow_8bit_chars; |
19bc826d | 36 | extern int protocol_version; |
886df221 | 37 | extern int always_checksum; |
ef74f5d6 | 38 | extern int preserve_times; |
eabc85ef | 39 | extern int msgs2stderr; |
9b25ef35 WD |
40 | extern int uid_ndx; |
41 | extern int gid_ndx; | |
b5343510 WD |
42 | extern int stdout_format_has_i; |
43 | extern int stdout_format_has_o_or_i; | |
d0133e6e | 44 | extern int logfile_format_has_i; |
13b597fa | 45 | extern int logfile_format_has_o_or_i; |
1ed56a05 | 46 | extern int receiver_symlink_times; |
4dde3347 WD |
47 | extern int64 total_data_written; |
48 | extern int64 total_data_read; | |
c5b7aa15 | 49 | extern mode_t orig_umask; |
548abf96 | 50 | extern char *auth_user; |
b5343510 | 51 | extern char *stdout_format; |
13b597fa WD |
52 | extern char *logfile_format; |
53 | extern char *logfile_name; | |
332cf6df | 54 | #ifdef ICONV_CONST |
9a31746b WD |
55 | extern iconv_t ic_chck; |
56 | #endif | |
332cf6df | 57 | #ifdef ICONV_OPTION |
93465f51 | 58 | extern iconv_t ic_recv; |
332cf6df | 59 | #endif |
b791d680 | 60 | extern char curr_dir[MAXPATHLEN]; |
26e21efc | 61 | extern char *full_module_path; |
6fb7cc38 | 62 | extern unsigned int module_dirlen; |
b791d680 | 63 | extern char sender_file_sum[MAX_DIGEST_LEN]; |
11ef77b7 | 64 | extern const char undetermined_hostname[]; |
548abf96 | 65 | |
ceca8cca | 66 | static int log_initialised; |
64c37826 | 67 | static int logfile_was_closed; |
13b597fa | 68 | static FILE *logfile_fp; |
b35d0d8e | 69 | struct stats stats; |
e0414f42 | 70 | |
3f0211b6 | 71 | int got_xfer_error = 0; |
951e826b | 72 | int output_needs_newline = 0; |
20caffd2 | 73 | int send_msgs_to_gen = 0; |
af642a61 | 74 | |
4dde3347 WD |
75 | static int64 initial_data_written; |
76 | static int64 initial_data_read; | |
77 | ||
af642a61 MP |
78 | struct { |
79 | int code; | |
80 | char const *name; | |
81 | } const rerr_names[] = { | |
4a7319be WD |
82 | { RERR_SYNTAX , "syntax or usage error" }, |
83 | { RERR_PROTOCOL , "protocol incompatibility" }, | |
84 | { RERR_FILESELECT , "errors selecting input/output files, dirs" }, | |
85 | { RERR_UNSUPPORTED, "requested action not supported" }, | |
86 | { RERR_STARTCLIENT, "error starting client-server protocol" }, | |
87 | { RERR_SOCKETIO , "error in socket IO" }, | |
88 | { RERR_FILEIO , "error in file IO" }, | |
89 | { RERR_STREAMIO , "error in rsync protocol data stream" }, | |
90 | { RERR_MESSAGEIO , "errors with program diagnostics" }, | |
91 | { RERR_IPC , "error in IPC code" }, | |
60168410 | 92 | { RERR_CRASHED , "sibling process crashed" }, |
0047f535 | 93 | { RERR_TERMINATED , "sibling process terminated abnormally" }, |
4a50a217 WD |
94 | { RERR_SIGNAL1 , "received SIGUSR1" }, |
95 | { RERR_SIGNAL , "received SIGINT, SIGTERM, or SIGHUP" }, | |
f14a65d9 | 96 | { RERR_WAITCHILD , "waitpid() failed" }, |
4a7319be | 97 | { RERR_MALLOC , "error allocating core memory buffers" }, |
94a4a125 | 98 | { RERR_PARTIAL , "some files/attrs were not transferred (see previous errors)" }, |
584ba4eb | 99 | { RERR_VANISHED , "some files vanished before they could be transferred" }, |
f9185203 | 100 | { RERR_DEL_LIMIT , "the --max-delete limit stopped deletions" }, |
4a7319be | 101 | { RERR_TIMEOUT , "timeout in data send/receive" }, |
ba22c9e2 | 102 | { RERR_CONTIMEOUT , "timeout waiting for daemon connection" }, |
19b27a48 AT |
103 | { RERR_CMD_FAILED , "remote shell failed" }, |
104 | { RERR_CMD_KILLED , "remote shell killed" }, | |
24cecf13 WD |
105 | { RERR_CMD_RUN , "remote command could not be run" }, |
106 | { RERR_CMD_NOTFOUND,"remote command not found" }, | |
4a7319be | 107 | { 0, NULL } |
af642a61 MP |
108 | }; |
109 | ||
af642a61 MP |
110 | /* |
111 | * Map from rsync error code to name, or return NULL. | |
112 | */ | |
113 | static char const *rerr_name(int code) | |
114 | { | |
4a7319be WD |
115 | int i; |
116 | for (i = 0; rerr_names[i].name; i++) { | |
117 | if (rerr_names[i].code == code) | |
118 | return rerr_names[i].name; | |
119 | } | |
120 | return NULL; | |
af642a61 MP |
121 | } |
122 | ||
4a19c3b2 | 123 | static void logit(int priority, const char *buf) |
4f6325c3 | 124 | { |
64c37826 WD |
125 | if (logfile_was_closed) |
126 | logfile_reopen(); | |
13b597fa WD |
127 | if (logfile_fp) { |
128 | fprintf(logfile_fp, "%s [%d] %s", | |
f7632fc6 | 129 | timestring(time(NULL)), (int)getpid(), buf); |
13b597fa | 130 | fflush(logfile_fp); |
4f6325c3 AT |
131 | } else { |
132 | syslog(priority, "%s", buf); | |
133 | } | |
134 | } | |
e42c9458 | 135 | |
6afb9077 | 136 | static void syslog_init() |
1a016bfd | 137 | { |
6afb9077 | 138 | static int been_here = 0; |
1a016bfd | 139 | int options = LOG_PID; |
6afb9077 WD |
140 | |
141 | if (been_here) | |
142 | return; | |
143 | been_here = 1; | |
144 | ||
145 | #ifdef LOG_NDELAY | |
146 | options |= LOG_NDELAY; | |
147 | #endif | |
148 | ||
149 | #ifdef LOG_DAEMON | |
04c84119 | 150 | openlog("rsyncd", options, lp_syslog_facility(module_id)); |
6afb9077 WD |
151 | #else |
152 | openlog("rsyncd", options); | |
153 | #endif | |
154 | ||
155 | #ifndef LOG_NDELAY | |
156 | logit(LOG_INFO, "rsyncd started\n"); | |
157 | #endif | |
158 | } | |
159 | ||
64c37826 WD |
160 | static void logfile_open(void) |
161 | { | |
c5b7aa15 | 162 | mode_t old_umask = umask(022 | orig_umask); |
13b597fa | 163 | logfile_fp = fopen(logfile_name, "a"); |
64c37826 | 164 | umask(old_umask); |
13b597fa | 165 | if (!logfile_fp) { |
64c37826 WD |
166 | int fopen_errno = errno; |
167 | /* Rsync falls back to using syslog on failure. */ | |
168 | syslog_init(); | |
169 | rsyserr(FERROR, fopen_errno, | |
13b597fa | 170 | "failed to open log-file %s", logfile_name); |
64c37826 WD |
171 | rprintf(FINFO, "Ignoring \"log file\" setting.\n"); |
172 | } | |
173 | } | |
174 | ||
04c84119 | 175 | void log_init(int restart) |
6afb9077 | 176 | { |
04c84119 WD |
177 | if (log_initialised) { |
178 | if (!restart) | |
179 | return; | |
180 | if (strcmp(logfile_name, lp_log_file(module_id)) != 0) { | |
181 | if (logfile_fp) { | |
182 | fclose(logfile_fp); | |
183 | logfile_fp = NULL; | |
184 | } else | |
185 | closelog(); | |
186 | logfile_name = NULL; | |
187 | } else if (*logfile_name) | |
188 | return; /* unchanged, non-empty "log file" names */ | |
189 | else if (lp_syslog_facility(-1) != lp_syslog_facility(module_id)) | |
190 | closelog(); | |
191 | else | |
192 | return; /* unchanged syslog settings */ | |
193 | } else | |
194 | log_initialised = 1; | |
1a016bfd | 195 | |
38de2866 | 196 | /* This looks pointless, but it is needed in order for the |
4a7319be | 197 | * C library on some systems to fetch the timezone info |
38de2866 WD |
198 | * before the chroot. */ |
199 | timestring(time(NULL)); | |
958f3735 | 200 | |
13b597fa WD |
201 | /* Optionally use a log file instead of syslog. (Non-daemon |
202 | * rsyncs will have already set logfile_name, as needed.) */ | |
203 | if (am_daemon && !logfile_name) | |
04c84119 | 204 | logfile_name = lp_log_file(module_id); |
13b597fa | 205 | if (logfile_name && *logfile_name) |
64c37826 WD |
206 | logfile_open(); |
207 | else | |
208 | syslog_init(); | |
1a016bfd | 209 | } |
1a016bfd | 210 | |
64c37826 | 211 | void logfile_close(void) |
4a239e98 | 212 | { |
13b597fa | 213 | if (logfile_fp) { |
64c37826 | 214 | logfile_was_closed = 1; |
13b597fa WD |
215 | fclose(logfile_fp); |
216 | logfile_fp = NULL; | |
4a239e98 WD |
217 | } |
218 | } | |
219 | ||
64c37826 | 220 | void logfile_reopen(void) |
4a239e98 | 221 | { |
64c37826 WD |
222 | if (logfile_was_closed) { |
223 | logfile_was_closed = 0; | |
224 | logfile_open(); | |
4a239e98 WD |
225 | } |
226 | } | |
227 | ||
3648ab3a | 228 | static void filtered_fwrite(FILE *f, const char *buf, int len, int use_isprint) |
be0602ec WD |
229 | { |
230 | const char *s, *end = buf + len; | |
231 | for (s = buf; s < end; s++) { | |
232 | if ((s < end - 4 | |
3648ab3a | 233 | && *s == '\\' && s[1] == '#' |
2dc7b8bd WD |
234 | && isDigit(s + 2) |
235 | && isDigit(s + 3) | |
236 | && isDigit(s + 4)) | |
3648ab3a | 237 | || (*s != '\t' |
2dc7b8bd | 238 | && ((use_isprint && !isPrint(s)) |
3648ab3a | 239 | || *(uchar*)s < ' '))) { |
be0602ec | 240 | if (s != buf && fwrite(buf, s - buf, 1, f) != 1) |
25f637a3 | 241 | exit_cleanup(RERR_MESSAGEIO); |
3648ab3a | 242 | fprintf(f, "\\#%03o", *(uchar*)s); |
be0602ec WD |
243 | buf = s + 1; |
244 | } | |
245 | } | |
246 | if (buf != end && fwrite(buf, end - buf, 1, f) != 1) | |
25f637a3 | 247 | exit_cleanup(RERR_MESSAGEIO); |
be0602ec WD |
248 | } |
249 | ||
554e0a8d | 250 | /* this is the underlying (unformatted) rsync debugging function. Call |
3f0211b6 WD |
251 | * it with FINFO, FERROR_*, FWARNING, FLOG, or FCLIENT. Note: recursion |
252 | * can happen with certain fatal conditions. */ | |
332cf6df | 253 | void rwrite(enum logcode code, const char *buf, int len, int is_utf8) |
0b76cd63 | 254 | { |
be0602ec | 255 | int trailing_CR_or_NL; |
1d891835 | 256 | FILE *f = msgs2stderr ? stderr : stdout; |
332cf6df WD |
257 | #ifdef ICONV_OPTION |
258 | iconv_t ic = is_utf8 && ic_recv != (iconv_t)-1 ? ic_recv : ic_chck; | |
259 | #else | |
260 | #ifdef ICONV_CONST | |
261 | iconv_t ic = ic_chck; | |
262 | #endif | |
263 | #endif | |
8d9dc9f9 | 264 | |
a644fc3c | 265 | if (len < 0) |
25f637a3 | 266 | exit_cleanup(RERR_MESSAGEIO); |
0b76cd63 | 267 | |
6be8a8b1 WD |
268 | if (msgs2stderr) { |
269 | if (!am_daemon) { | |
270 | if (code == FLOG) | |
271 | return; | |
272 | goto output_msg; | |
273 | } | |
274 | if (code == FCLIENT) | |
275 | return; | |
276 | code = FLOG; | |
277 | } else if (send_msgs_to_gen) { | |
332cf6df | 278 | assert(!is_utf8); |
7c7462cd | 279 | /* Pass the message to our sibling in native charset. */ |
332cf6df | 280 | send_msg((enum msgcode)code, buf, len, 0); |
11a5a3c7 AT |
281 | return; |
282 | } | |
283 | ||
3f0211b6 | 284 | if (code == FERROR_SOCKET) /* This gets simplified for a non-sibling. */ |
b66e31bf | 285 | code = FERROR; |
7c7462cd WD |
286 | else if (code == FERROR_UTF8) { |
287 | is_utf8 = 1; | |
288 | code = FERROR; | |
289 | } | |
b66e31bf | 290 | |
1eec003a WD |
291 | if (code == FCLIENT) |
292 | code = FINFO; | |
d0133e6e | 293 | else if (am_daemon || logfile_name) { |
0bb4d176 | 294 | static int in_block; |
d0133e6e | 295 | char msg[2048]; |
3f0211b6 | 296 | int priority = code == FINFO || code == FLOG ? LOG_INFO : LOG_WARNING; |
0bb4d176 WD |
297 | |
298 | if (in_block) | |
a644fc3c | 299 | return; |
0bb4d176 WD |
300 | in_block = 1; |
301 | if (!log_initialised) | |
04c84119 | 302 | log_init(0); |
0bb4d176 | 303 | strlcpy(msg, buf, MIN((int)sizeof msg, len + 1)); |
d0133e6e | 304 | logit(priority, msg); |
0bb4d176 WD |
305 | in_block = 0; |
306 | ||
13b597fa | 307 | if (code == FLOG || (am_daemon && !am_server)) |
a644fc3c | 308 | return; |
0bb4d176 | 309 | } else if (code == FLOG) |
ff8b29b8 | 310 | return; |
0bb4d176 | 311 | |
3f0211b6 | 312 | if (quiet && code == FINFO) |
b3e8e7c7 WD |
313 | return; |
314 | ||
0bb4d176 | 315 | if (am_server) { |
3f0211b6 WD |
316 | enum msgcode msg = (enum msgcode)code; |
317 | if (protocol_version < 30) { | |
318 | if (msg == MSG_ERROR) | |
319 | msg = MSG_ERROR_XFER; | |
320 | else if (msg == MSG_WARNING) | |
321 | msg = MSG_INFO; | |
322 | } | |
0bb4d176 | 323 | /* Pass the message to the non-server side. */ |
3f0211b6 | 324 | if (send_msg(msg, buf, len, !is_utf8)) |
0bb4d176 WD |
325 | return; |
326 | if (am_daemon) { | |
327 | /* TODO: can we send the error to the user somehow? */ | |
328 | return; | |
329 | } | |
1d891835 | 330 | f = stderr; |
0b76cd63 AT |
331 | } |
332 | ||
37077bd3 | 333 | output_msg: |
be0602ec | 334 | switch (code) { |
3f0211b6 WD |
335 | case FERROR_XFER: |
336 | got_xfer_error = 1; | |
9468cf79 | 337 | /* FALL THROUGH */ |
be0602ec | 338 | case FERROR: |
ef9d3a15 WD |
339 | case FERROR_UTF8: |
340 | case FERROR_SOCKET: | |
3f0211b6 | 341 | case FWARNING: |
ff8b29b8 | 342 | f = stderr; |
be0602ec | 343 | break; |
ef9d3a15 | 344 | case FLOG: |
d0133e6e | 345 | case FINFO: |
37077bd3 | 346 | case FCLIENT: |
be0602ec WD |
347 | break; |
348 | default: | |
4351c039 | 349 | fprintf(stderr, "Unknown logcode in rwrite(): %d [%s]\n", (int)code, who_am_i()); |
25f637a3 | 350 | exit_cleanup(RERR_MESSAGEIO); |
be0602ec | 351 | } |
0b76cd63 | 352 | |
951e826b | 353 | if (output_needs_newline) { |
f3185096 | 354 | fputc('\n', f); |
951e826b | 355 | output_needs_newline = 0; |
f3185096 WD |
356 | } |
357 | ||
be0602ec WD |
358 | trailing_CR_or_NL = len && (buf[len-1] == '\n' || buf[len-1] == '\r') |
359 | ? buf[--len] : 0; | |
8d9dc9f9 | 360 | |
bb4e4d88 WD |
361 | if (len && buf[0] == '\r') { |
362 | fputc('\r', f); | |
363 | buf++; | |
364 | len--; | |
365 | } | |
366 | ||
332cf6df WD |
367 | #ifdef ICONV_CONST |
368 | if (ic != (iconv_t)-1) { | |
2509753f | 369 | xbuf outbuf, inbuf; |
9a31746b | 370 | char convbuf[1024]; |
2509753f WD |
371 | int ierrno; |
372 | ||
373 | INIT_CONST_XBUF(outbuf, convbuf); | |
bb640d32 | 374 | INIT_XBUF(inbuf, (char*)buf, len, (size_t)-1); |
2509753f WD |
375 | |
376 | while (inbuf.len) { | |
d8a7290f | 377 | iconvbufs(ic, &inbuf, &outbuf, inbuf.pos ? 0 : ICB_INIT); |
2509753f WD |
378 | ierrno = errno; |
379 | if (outbuf.len) { | |
380 | filtered_fwrite(f, convbuf, outbuf.len, 0); | |
381 | outbuf.len = 0; | |
9a31746b | 382 | } |
2509753f | 383 | if (!ierrno || ierrno == E2BIG) |
9a31746b | 384 | continue; |
2509753f WD |
385 | fprintf(f, "\\#%03o", CVAL(inbuf.buf, inbuf.pos++)); |
386 | inbuf.len--; | |
9a31746b | 387 | } |
9a31746b WD |
388 | } else |
389 | #endif | |
507433f6 | 390 | filtered_fwrite(f, buf, len, !allow_8bit_chars); |
be0602ec WD |
391 | |
392 | if (trailing_CR_or_NL) { | |
393 | fputc(trailing_CR_or_NL, f); | |
0bb4d176 | 394 | fflush(f); |
be0602ec | 395 | } |
0b76cd63 | 396 | } |
0455cd93 | 397 | |
3f0211b6 WD |
398 | /* This is the rsync debugging function. Call it with FINFO, FERROR_*, |
399 | * FWARNING, FLOG, or FCLIENT. */ | |
a039749b | 400 | void rprintf(enum logcode code, const char *format, ...) |
554e0a8d | 401 | { |
4a7319be | 402 | va_list ap; |
b74b3d53 | 403 | char buf[BIGPATHBUFLEN]; |
8fcdc444 | 404 | size_t len; |
554e0a8d AT |
405 | |
406 | va_start(ap, format); | |
ef74f5d6 | 407 | len = vsnprintf(buf, sizeof buf, format, ap); |
554e0a8d AT |
408 | va_end(ap); |
409 | ||
b2f02464 | 410 | /* Deal with buffer overruns. Instead of panicking, just |
8fcdc444 WD |
411 | * truncate the resulting string. (Note that configure ensures |
412 | * that we have a vsnprintf() that doesn't ever return -1.) */ | |
413 | if (len > sizeof buf - 1) { | |
b74b3d53 | 414 | static const char ellipsis[] = "[...]"; |
b2f02464 MP |
415 | |
416 | /* Reset length, and zero-terminate the end of our buffer */ | |
ef74f5d6 | 417 | len = sizeof buf - 1; |
b2f02464 MP |
418 | buf[len] = '\0'; |
419 | ||
420 | /* Copy the ellipsis to the end of the string, but give | |
421 | * us one extra character: | |
422 | * | |
ef74f5d6 | 423 | * v--- null byte at buf[sizeof buf - 1] |
b2f02464 MP |
424 | * abcdefghij0 |
425 | * -> abcd[...]00 <-- now two null bytes at end | |
426 | * | |
427 | * If the input format string has a trailing newline, | |
428 | * we copy it into that extra null; if it doesn't, well, | |
429 | * all we lose is one byte. */ | |
b74b3d53 | 430 | memcpy(buf+len-sizeof ellipsis, ellipsis, sizeof ellipsis); |
b2f02464 MP |
431 | if (format[strlen(format)-1] == '\n') { |
432 | buf[len-1] = '\n'; | |
433 | } | |
434 | } | |
554e0a8d | 435 | |
332cf6df | 436 | rwrite(code, buf, len, 0); |
554e0a8d | 437 | } |
0b76cd63 | 438 | |
a039749b MP |
439 | /* This is like rprintf, but it also tries to print some |
440 | * representation of the error code. Normally errcode = errno. | |
441 | * | |
442 | * Unlike rprintf, this always adds a newline and there should not be | |
443 | * one in the format string. | |
444 | * | |
445 | * Note that since strerror might involve dynamically loading a | |
446 | * message catalog we need to call it once before chroot-ing. */ | |
447 | void rsyserr(enum logcode code, int errcode, const char *format, ...) | |
448 | { | |
4a7319be | 449 | va_list ap; |
b74b3d53 | 450 | char buf[BIGPATHBUFLEN]; |
8fcdc444 WD |
451 | size_t len; |
452 | ||
c9bce0b8 | 453 | strlcpy(buf, RSYNC_NAME ": ", sizeof buf); |
8fcdc444 | 454 | len = (sizeof RSYNC_NAME ": ") - 1; |
a039749b MP |
455 | |
456 | va_start(ap, format); | |
8fcdc444 | 457 | len += vsnprintf(buf + len, sizeof buf - len, format, ap); |
a039749b MP |
458 | va_end(ap); |
459 | ||
8fcdc444 WD |
460 | if (len < sizeof buf) { |
461 | len += snprintf(buf + len, sizeof buf - len, | |
462 | ": %s (%d)\n", strerror(errcode), errcode); | |
463 | } | |
464 | if (len >= sizeof buf) | |
25f637a3 | 465 | exit_cleanup(RERR_MESSAGEIO); |
a039749b | 466 | |
332cf6df | 467 | rwrite(code, buf, len, 0); |
a039749b MP |
468 | } |
469 | ||
ff41a59f | 470 | void rflush(enum logcode code) |
0b76cd63 | 471 | { |
1d891835 | 472 | FILE *f; |
0455cd93 | 473 | |
13b597fa | 474 | if (am_daemon || code == FLOG) |
0b76cd63 | 475 | return; |
0b76cd63 | 476 | |
951e826b | 477 | if (!am_server && (code == FINFO || code == FCLIENT)) |
13b597fa | 478 | f = stdout; |
3f0211b6 WD |
479 | else |
480 | f = stderr; | |
0b76cd63 | 481 | |
0b76cd63 AT |
482 | fflush(f); |
483 | } | |
484 | ||
4dde3347 WD |
485 | void remember_initial_stats(void) |
486 | { | |
487 | initial_data_read = total_data_read; | |
488 | initial_data_written = total_data_written; | |
489 | } | |
490 | ||
b5343510 | 491 | /* A generic logging routine for send/recv, with parameter substitiution. */ |
4a19c3b2 | 492 | static void log_formatted(enum logcode code, const char *format, const char *op, |
4dde3347 | 493 | struct file_struct *file, const char *fname, int iflags, |
112d728f | 494 | const char *hlink) |
e08bfe12 | 495 | { |
ddd74b67 | 496 | char buf[MAXPATHLEN+1024], buf2[MAXPATHLEN], fmt[32]; |
4a19c3b2 WD |
497 | char *p, *s, *c; |
498 | const char *n; | |
ef74f5d6 | 499 | size_t len, total; |
1b7c47cb | 500 | int64 b; |
e08bfe12 | 501 | |
ddd74b67 WD |
502 | *fmt = '%'; |
503 | ||
aa126974 | 504 | /* We expand % codes one by one in place in buf. We don't |
126e7aff WD |
505 | * copy in the terminating null of the inserted strings, but |
506 | * rather keep going until we reach the null of the format. */ | |
ef74f5d6 | 507 | total = strlcpy(buf, format, sizeof buf); |
d9c0051f WD |
508 | if (total > MAXPATHLEN) { |
509 | rprintf(FERROR, "log-format string is WAY too long!\n"); | |
25f637a3 | 510 | exit_cleanup(RERR_MESSAGEIO); |
d9c0051f WD |
511 | } |
512 | buf[total++] = '\n'; | |
513 | buf[total] = '\0'; | |
0455cd93 | 514 | |
16f960fe | 515 | for (p = buf; (p = strchr(p, '%')) != NULL; ) { |
df7ec1cf | 516 | int humanize = 0; |
ddd74b67 | 517 | s = p++; |
4a19c3b2 | 518 | c = fmt + 1; |
df7ec1cf WD |
519 | while (*p == '\'') { |
520 | humanize++; | |
521 | p++; | |
522 | } | |
9baed760 | 523 | if (*p == '-') |
4a19c3b2 WD |
524 | *c++ = *p++; |
525 | while (isDigit(p) && c - fmt < (int)(sizeof fmt) - 8) | |
526 | *c++ = *p++; | |
df7ec1cf WD |
527 | while (*p == '\'') { |
528 | humanize++; | |
529 | p++; | |
530 | } | |
e145d51b WD |
531 | if (!*p) |
532 | break; | |
4a19c3b2 | 533 | *c = '\0'; |
e08bfe12 | 534 | n = NULL; |
e08bfe12 | 535 | |
11ef77b7 MM |
536 | /* Note for %h and %a: it doesn't matter what fd we pass to |
537 | * client_{name,addr} because rsync_module will already have | |
538 | * forced the answer to be cached (assuming, of course, for %h | |
539 | * that lp_reverse_lookup(module_id) is true). */ | |
0455cd93 | 540 | switch (*p) { |
b74b3d53 | 541 | case 'h': |
11ef77b7 MM |
542 | if (am_daemon) { |
543 | n = lp_reverse_lookup(module_id) | |
544 | ? client_name(0) : undetermined_hostname; | |
545 | } | |
b74b3d53 WD |
546 | break; |
547 | case 'a': | |
548 | if (am_daemon) | |
549 | n = client_addr(0); | |
550 | break; | |
4a7319be | 551 | case 'l': |
7f0db4fd | 552 | strlcat(fmt, "s", sizeof fmt); |
ddd74b67 | 553 | snprintf(buf2, sizeof buf2, fmt, |
df7ec1cf | 554 | do_big_num(F_LENGTH(file), humanize, NULL)); |
e08bfe12 AT |
555 | n = buf2; |
556 | break; | |
427b6179 | 557 | case 'U': |
4ade505c | 558 | strlcat(fmt, "u", sizeof fmt); |
427b6179 | 559 | snprintf(buf2, sizeof buf2, fmt, |
9b25ef35 | 560 | uid_ndx ? F_OWNER(file) : 0); |
427b6179 WD |
561 | n = buf2; |
562 | break; | |
563 | case 'G': | |
9b25ef35 | 564 | if (!gid_ndx || file->flags & FLAG_SKIP_GROUP) |
998113fe WD |
565 | n = "DEFAULT"; |
566 | else { | |
4ade505c | 567 | strlcat(fmt, "u", sizeof fmt); |
998113fe | 568 | snprintf(buf2, sizeof buf2, fmt, |
4ade505c | 569 | F_GROUP(file)); |
998113fe WD |
570 | n = buf2; |
571 | } | |
427b6179 | 572 | break; |
4a7319be | 573 | case 'p': |
126e7aff | 574 | strlcat(fmt, "ld", sizeof fmt); |
ddd74b67 | 575 | snprintf(buf2, sizeof buf2, fmt, |
126e7aff | 576 | (long)getpid()); |
e08bfe12 AT |
577 | n = buf2; |
578 | break; | |
427b6179 | 579 | case 'M': |
4a19c3b2 | 580 | n = c = timestring(file->modtime); |
f2681d42 | 581 | while ((c = strchr(c, ' ')) != NULL) |
4a19c3b2 | 582 | *c = '-'; |
427b6179 WD |
583 | break; |
584 | case 'B': | |
4a19c3b2 WD |
585 | c = buf2 + MAXPATHLEN - PERMSTRING_SIZE - 1; |
586 | permstring(c, file->mode); | |
587 | n = c + 1; /* skip the type char */ | |
427b6179 | 588 | break; |
b74b3d53 WD |
589 | case 'o': |
590 | n = op; | |
591 | break; | |
4a7319be | 592 | case 'f': |
112d728f WD |
593 | if (fname) { |
594 | c = f_name_buf(); | |
595 | strlcpy(c, fname, MAXPATHLEN); | |
596 | } else | |
597 | c = f_name(file, NULL); | |
8fc4033e | 598 | if (am_sender && F_PATHNAME(file)) { |
9baed760 | 599 | pathjoin(buf2, sizeof buf2, |
8fc4033e | 600 | F_PATHNAME(file), c); |
0ee6ca98 | 601 | clean_fname(buf2, 0); |
4a19c3b2 WD |
602 | if (fmt[1]) { |
603 | strlcpy(c, buf2, MAXPATHLEN); | |
604 | n = c; | |
605 | } else | |
dcbae654 | 606 | n = buf2; |
1b81f797 | 607 | } else if (am_daemon && *c != '/') { |
ec55b4f2 | 608 | pathjoin(buf2, sizeof buf2, |
4a19c3b2 | 609 | curr_dir + module_dirlen, c); |
ec55b4f2 | 610 | clean_fname(buf2, 0); |
4a19c3b2 WD |
611 | if (fmt[1]) { |
612 | strlcpy(c, buf2, MAXPATHLEN); | |
613 | n = c; | |
614 | } else | |
ec55b4f2 | 615 | n = buf2; |
4a19c3b2 WD |
616 | } else { |
617 | clean_fname(c, 0); | |
618 | n = c; | |
619 | } | |
9baed760 WD |
620 | if (*n == '/') |
621 | n++; | |
ab7104da | 622 | break; |
ef74f5d6 | 623 | case 'n': |
112d728f WD |
624 | if (fname) { |
625 | c = f_name_buf(); | |
626 | strlcpy(c, fname, MAXPATHLEN); | |
627 | } else | |
628 | c = f_name(file, NULL); | |
0ee6ca98 | 629 | if (S_ISDIR(file->mode)) |
4a19c3b2 WD |
630 | strlcat(c, "/", MAXPATHLEN); |
631 | n = c; | |
ef74f5d6 WD |
632 | break; |
633 | case 'L': | |
afc65a5a | 634 | if (hlink && *hlink) { |
0ee6ca98 | 635 | n = hlink; |
c9bce0b8 | 636 | strlcpy(buf2, " => ", sizeof buf2); |
112d728f | 637 | } else if (S_ISLNK(file->mode) && !fname) { |
82ad07c4 | 638 | n = F_SYMLINK(file); |
c9bce0b8 | 639 | strlcpy(buf2, " -> ", sizeof buf2); |
126e7aff | 640 | } else { |
ef74f5d6 | 641 | n = ""; |
126e7aff WD |
642 | if (!fmt[1]) |
643 | break; | |
c9bce0b8 | 644 | strlcpy(buf2, " ", sizeof buf2); |
126e7aff WD |
645 | } |
646 | strlcat(fmt, "s", sizeof fmt); | |
647 | snprintf(buf2 + 4, sizeof buf2 - 4, fmt, n); | |
648 | n = buf2; | |
ef74f5d6 | 649 | break; |
b74b3d53 WD |
650 | case 'm': |
651 | n = lp_name(module_id); | |
652 | break; | |
653 | case 't': | |
654 | n = timestring(time(NULL)); | |
655 | break; | |
656 | case 'P': | |
26e21efc | 657 | n = full_module_path; |
b74b3d53 WD |
658 | break; |
659 | case 'u': | |
660 | n = auth_user; | |
661 | break; | |
4a7319be | 662 | case 'b': |
4dde3347 WD |
663 | if (!(iflags & ITEM_TRANSFER)) |
664 | b = 0; | |
665 | else if (am_sender) | |
666 | b = total_data_written - initial_data_written; | |
667 | else | |
668 | b = total_data_read - initial_data_read; | |
7f0db4fd | 669 | strlcat(fmt, "s", sizeof fmt); |
df7ec1cf WD |
670 | snprintf(buf2, sizeof buf2, fmt, |
671 | do_big_num(b, humanize, NULL)); | |
1b7c47cb AT |
672 | n = buf2; |
673 | break; | |
4a7319be | 674 | case 'c': |
4dde3347 WD |
675 | if (!(iflags & ITEM_TRANSFER)) |
676 | b = 0; | |
677 | else if (!am_sender) | |
678 | b = total_data_written - initial_data_written; | |
679 | else | |
680 | b = total_data_read - initial_data_read; | |
7f0db4fd | 681 | strlcat(fmt, "s", sizeof fmt); |
df7ec1cf WD |
682 | snprintf(buf2, sizeof buf2, fmt, |
683 | do_big_num(b, humanize, NULL)); | |
1b7c47cb AT |
684 | n = buf2; |
685 | break; | |
886df221 WD |
686 | case 'C': |
687 | if (protocol_version >= 30 | |
688 | && (iflags & ITEM_TRANSFER | |
689 | || (always_checksum && S_ISREG(file->mode)))) { | |
690 | int i, x1, x2; | |
691 | const char *sum = iflags & ITEM_TRANSFER | |
692 | ? sender_file_sum : F_SUM(file); | |
693 | c = buf2 + checksum_len*2; | |
694 | *c = '\0'; | |
695 | for (i = checksum_len; --i >= 0; ) { | |
696 | x1 = CVAL(sum, i); | |
697 | x2 = x1 >> 4; | |
698 | x1 &= 0xF; | |
699 | *--c = x1 <= 9 ? x1 + '0' : x1 + 'a' - 10; | |
700 | *--c = x2 <= 9 ? x2 + '0' : x2 + 'a' - 10; | |
701 | } | |
702 | } else { | |
703 | memset(buf2, ' ', checksum_len*2); | |
704 | buf2[checksum_len*2] = '\0'; | |
705 | } | |
706 | n = buf2; | |
707 | break; | |
ef74f5d6 | 708 | case 'i': |
19bc826d | 709 | if (iflags & ITEM_DELETED) { |
46f800e8 | 710 | n = "*deleting "; |
19bc826d WD |
711 | break; |
712 | } | |
4a19c3b2 WD |
713 | n = c = buf2 + MAXPATHLEN - 32; |
714 | c[0] = iflags & ITEM_LOCAL_CHANGE | |
fd84673e | 715 | ? iflags & ITEM_XNAME_FOLLOWS ? 'h' : 'c' |
ca62acc3 | 716 | : !(iflags & ITEM_TRANSFER) ? '.' |
c1759b9f | 717 | : !local_server && *op == 's' ? '<' : '>'; |
1ed9018e WD |
718 | if (S_ISLNK(file->mode)) { |
719 | c[1] = 'L'; | |
720 | c[3] = '.'; | |
721 | c[4] = !(iflags & ITEM_REPORT_TIME) ? '.' | |
722 | : !preserve_times || !receiver_symlink_times | |
723 | || (iflags & ITEM_REPORT_TIMEFAIL) ? 'T' : 't'; | |
724 | } else { | |
725 | c[1] = S_ISDIR(file->mode) ? 'd' | |
726 | : IS_SPECIAL(file->mode) ? 'S' | |
727 | : IS_DEVICE(file->mode) ? 'D' : 'f'; | |
728 | c[3] = !(iflags & ITEM_REPORT_SIZE) ? '.' : 's'; | |
729 | c[4] = !(iflags & ITEM_REPORT_TIME) ? '.' | |
730 | : !preserve_times ? 'T' : 't'; | |
731 | } | |
732 | c[2] = !(iflags & ITEM_REPORT_CHANGE) ? '.' : 'c'; | |
4a19c3b2 WD |
733 | c[5] = !(iflags & ITEM_REPORT_PERMS) ? '.' : 'p'; |
734 | c[6] = !(iflags & ITEM_REPORT_OWNER) ? '.' : 'o'; | |
735 | c[7] = !(iflags & ITEM_REPORT_GROUP) ? '.' : 'g'; | |
1c3344a1 WD |
736 | c[8] = !(iflags & ITEM_REPORT_ATIME) ? '.' : 'u'; |
737 | c[9] = !(iflags & ITEM_REPORT_ACL) ? '.' : 'a'; | |
738 | c[10] = !(iflags & ITEM_REPORT_XATTR) ? '.' : 'x'; | |
739 | c[11] = '\0'; | |
ef74f5d6 WD |
740 | |
741 | if (iflags & (ITEM_IS_NEW|ITEM_MISSING_DATA)) { | |
742 | char ch = iflags & ITEM_IS_NEW ? '+' : '?'; | |
743 | int i; | |
4a19c3b2 WD |
744 | for (i = 2; c[i]; i++) |
745 | c[i] = ch; | |
746 | } else if (c[0] == '.' || c[0] == 'h' || c[0] == 'c') { | |
19bc826d | 747 | int i; |
4a19c3b2 WD |
748 | for (i = 2; c[i]; i++) { |
749 | if (c[i] != '.') | |
19bc826d WD |
750 | break; |
751 | } | |
4a19c3b2 WD |
752 | if (!c[i]) { |
753 | for (i = 2; c[i]; i++) | |
754 | c[i] = ' '; | |
19bc826d | 755 | } |
ef74f5d6 WD |
756 | } |
757 | break; | |
e08bfe12 AT |
758 | } |
759 | ||
ddd74b67 WD |
760 | /* "n" is the string to be inserted in place of this % code. */ |
761 | if (!n) | |
762 | continue; | |
9baed760 WD |
763 | if (n != buf2 && fmt[1]) { |
764 | strlcat(fmt, "s", sizeof fmt); | |
765 | snprintf(buf2, sizeof buf2, fmt, n); | |
766 | n = buf2; | |
767 | } | |
ef74f5d6 | 768 | len = strlen(n); |
e08bfe12 | 769 | |
e145d51b | 770 | /* Subtract the length of the escape from the string's size. */ |
0455cd93 | 771 | total -= p - s + 1; |
e145d51b | 772 | |
d9c0051f | 773 | if (len + total >= (size_t)sizeof buf) { |
1e7098b5 WD |
774 | rprintf(FERROR, |
775 | "buffer overflow expanding %%%c -- exiting\n", | |
0455cd93 | 776 | p[0]); |
25f637a3 | 777 | exit_cleanup(RERR_MESSAGEIO); |
e08bfe12 AT |
778 | } |
779 | ||
aa126974 | 780 | /* Shuffle the rest of the string along to make space for n */ |
0455cd93 WD |
781 | if (len != (size_t)(p - s + 1)) |
782 | memmove(s + len, p + 1, total - (s - buf) + 1); | |
ddd74b67 | 783 | total += len; |
aa126974 | 784 | |
ddd74b67 | 785 | /* Insert the contents of string "n", but NOT its null. */ |
ef74f5d6 | 786 | if (len) |
ddd74b67 | 787 | memcpy(s, n, len); |
e08bfe12 | 788 | |
aa126974 | 789 | /* Skip over inserted string; continue looking */ |
ddd74b67 | 790 | p = s + len; |
e08bfe12 AT |
791 | } |
792 | ||
332cf6df | 793 | rwrite(code, buf, total, 0); |
e08bfe12 AT |
794 | } |
795 | ||
0455cd93 WD |
796 | /* Return 1 if the format escape is in the log-format string (e.g. look for |
797 | * the 'b' in the "%9b" format escape). */ | |
16f960fe WD |
798 | int log_format_has(const char *format, char esc) |
799 | { | |
800 | const char *p; | |
801 | ||
802 | if (!format) | |
803 | return 0; | |
804 | ||
805 | for (p = format; (p = strchr(p, '%')) != NULL; ) { | |
2907af47 | 806 | for (p++; *p == '\''; p++) {} /*SHARED ITERATOR*/ |
44a97a34 | 807 | if (*p == '-') |
16f960fe | 808 | p++; |
2dc7b8bd | 809 | while (isDigit(p)) |
16f960fe | 810 | p++; |
44a97a34 | 811 | while (*p == '\'') p++; |
16f960fe WD |
812 | if (!*p) |
813 | break; | |
814 | if (*p == esc) | |
815 | return 1; | |
816 | } | |
817 | return 0; | |
818 | } | |
819 | ||
d0133e6e | 820 | /* Log the transfer of a file. If the code is FCLIENT, the output just goes |
b5343510 WD |
821 | * to stdout. If it is FLOG, it just goes to the log file. Otherwise we |
822 | * output to both. */ | |
4dde3347 | 823 | void log_item(enum logcode code, struct file_struct *file, int iflags, const char *hlink) |
11a5a3c7 | 824 | { |
4a19c3b2 | 825 | const char *s_or_r = am_sender ? "send" : "recv"; |
11a5a3c7 | 826 | |
4dde3347 WD |
827 | if (code != FLOG && stdout_format && !am_server) |
828 | log_formatted(FCLIENT, stdout_format, s_or_r, file, NULL, iflags, hlink); | |
829 | if (code != FCLIENT && logfile_format && *logfile_format) | |
830 | log_formatted(FLOG, logfile_format, s_or_r, file, NULL, iflags, hlink); | |
11a5a3c7 AT |
831 | } |
832 | ||
1c3e3679 | 833 | void maybe_log_item(struct file_struct *file, int iflags, int itemizing, |
4a19c3b2 | 834 | const char *buf) |
1c3e3679 | 835 | { |
f2b6fe44 | 836 | int significant_flags = iflags & SIGNIFICANT_ITEM_FLAGS; |
6d4ecad1 | 837 | int see_item = itemizing && (significant_flags || *buf |
951e826b | 838 | || stdout_format_has_i > 1 || (INFO_GTE(NAME, 2) && stdout_format_has_i)); |
6d4ecad1 | 839 | int local_change = iflags & ITEM_LOCAL_CHANGE && significant_flags; |
1c3e3679 | 840 | if (am_server) { |
d0133e6e WD |
841 | if (logfile_name && !dry_run && see_item |
842 | && (significant_flags || logfile_format_has_i)) | |
4dde3347 | 843 | log_item(FLOG, file, iflags, buf); |
f2b6fe44 | 844 | } else if (see_item || local_change || *buf |
d0133e6e WD |
845 | || (S_ISDIR(file->mode) && significant_flags)) { |
846 | enum logcode code = significant_flags || logfile_format_has_i ? FINFO : FCLIENT; | |
4dde3347 | 847 | log_item(code, file, iflags, buf); |
d0133e6e | 848 | } |
1c3e3679 WD |
849 | } |
850 | ||
4a19c3b2 | 851 | void log_delete(const char *fname, int mode) |
19bc826d | 852 | { |
112d728f | 853 | static struct { |
aac5cab8 | 854 | union file_extras ex[4]; /* just in case... */ |
112d728f | 855 | struct file_struct file; |
be8234cd | 856 | } x; /* Zero-initialized due to static declaration. */ |
19bc826d | 857 | int len = strlen(fname); |
4a19c3b2 | 858 | const char *fmt; |
19bc826d | 859 | |
112d728f | 860 | x.file.mode = mode; |
19bc826d | 861 | |
951e826b | 862 | if (!INFO_GTE(DEL, 1) && !stdout_format) |
41b5b5e7 WD |
863 | ; |
864 | else if (am_server && protocol_version >= 29 && len < MAXPATHLEN) { | |
19bc826d WD |
865 | if (S_ISDIR(mode)) |
866 | len++; /* directories include trailing null */ | |
332cf6df | 867 | send_msg(MSG_DELETED, fname, len, am_generator); |
19bc826d | 868 | } else { |
b5343510 | 869 | fmt = stdout_format_has_o_or_i ? stdout_format : "deleting %n"; |
4dde3347 | 870 | log_formatted(FCLIENT, fmt, "del.", &x.file, fname, ITEM_DELETED, NULL); |
19bc826d WD |
871 | } |
872 | ||
13b597fa | 873 | if (!logfile_name || dry_run || !logfile_format) |
1eec003a WD |
874 | return; |
875 | ||
13b597fa | 876 | fmt = logfile_format_has_o_or_i ? logfile_format : "deleting %n"; |
4dde3347 | 877 | log_formatted(FLOG, fmt, "del.", &x.file, fname, ITEM_DELETED, NULL); |
19bc826d | 878 | } |
af642a61 | 879 | |
af642a61 MP |
880 | /* |
881 | * Called when the transfer is interrupted for some reason. | |
882 | * | |
883 | * Code is one of the RERR_* codes from errcode.h, or terminating | |
884 | * successfully. | |
885 | */ | |
a9766ef1 | 886 | void log_exit(int code, const char *file, int line) |
9b73d1c0 AT |
887 | { |
888 | if (code == 0) { | |
6d56efa6 | 889 | rprintf(FLOG,"sent %s bytes received %s bytes total size %s\n", |
adc2476f WD |
890 | comma_num(stats.total_written), |
891 | comma_num(stats.total_read), | |
892 | comma_num(stats.total_size)); | |
969cdffb | 893 | } else if (am_server != 2) { |
4a7319be WD |
894 | const char *name; |
895 | ||
896 | name = rerr_name(code); | |
897 | if (!name) | |
898 | name = "unexplained error"; | |
af642a61 | 899 | |
1bca1de6 WD |
900 | /* VANISHED is not an error, only a warning */ |
901 | if (code == RERR_VANISHED) { | |
3f0211b6 | 902 | rprintf(FWARNING, "rsync warning: %s (code %d) at %s(%d) [%s=%s]\n", |
43eae40e | 903 | name, code, file, line, who_am_i(), RSYNC_VERSION); |
1bca1de6 | 904 | } else { |
43eae40e WD |
905 | rprintf(FERROR, "rsync error: %s (code %d) at %s(%d) [%s=%s]\n", |
906 | name, code, file, line, who_am_i(), RSYNC_VERSION); | |
1bca1de6 | 907 | } |
9b73d1c0 AT |
908 | } |
909 | } |