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