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