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