1 This patch allows a non-daemon server and/or a client rsync to log what they
2 are doing, similar to how a daemon logs its actions.
10 +extern int am_server;
12 extern int keep_partial;
13 extern int log_got_error;
14 extern char *partial_dir;
15 +extern char *logfile_name;
18 static struct sigaction sigact;
19 @@ -149,7 +151,7 @@ void _exit_cleanup(int code, const char
24 + if (code || am_daemon || (am_server && logfile_name))
25 log_exit(code, file, line);
28 --- old/clientserver.c
29 +++ new/clientserver.c
30 @@ -44,10 +44,14 @@ extern int protocol_version;
31 extern int io_timeout;
33 extern int default_af_hint;
34 +extern int logfile_format_has_i;
35 +extern int logfile_format_has_o_or_i;
36 extern mode_t orig_umask;
37 extern char *bind_address;
38 extern char *sockopts;
39 extern char *config_file;
40 +extern char *logfile_format;
41 +extern char *logfile_name;
42 extern char *files_from;
44 extern struct chmod_mode_struct *chmod_modes;
45 @@ -55,8 +59,6 @@ extern struct filter_list_struct server_
49 -int daemon_log_format_has_i = 0;
50 -int daemon_log_format_has_o_or_i = 0;
52 struct chmod_mode_struct *daemon_chmod_modes;
54 @@ -329,11 +331,12 @@ static int rsync_module(int f_in, int f_
57 if (lp_transfer_logging(i)) {
58 - if (log_format_has(lp_log_format(i), 'i'))
59 - daemon_log_format_has_i = 1;
60 - if (daemon_log_format_has_i
61 - || log_format_has(lp_log_format(i), 'o'))
62 - daemon_log_format_has_o_or_i = 1;
63 + logfile_format = lp_log_format(i);
64 + if (log_format_has(logfile_format, 'i'))
65 + logfile_format_has_i = 1;
66 + if (logfile_format_has_i
67 + || log_format_has(logfile_format, 'o'))
68 + logfile_format_has_o_or_i = 1;
71 am_root = (MY_UID() == 0);
74 @@ -95,15 +95,15 @@ static int show_filelist_p(void)
76 static void start_filelist_progress(char *kind)
78 - rprintf(FINFO, "%s ... ", kind);
79 + rprintf(FCLIENT, "%s ... ", kind);
80 if (verbose > 1 || do_progress)
81 - rprintf(FINFO, "\n");
82 + rprintf(FCLIENT, "\n");
86 static void emit_filelist_progress(int count)
88 - rprintf(FINFO, " %d files...\r", count);
89 + rprintf(FCLIENT, " %d files...\r", count);
92 static void maybe_emit_filelist_progress(int count)
93 @@ -295,7 +295,7 @@ void flist_expand(struct file_list *flis
96 if (verbose >= 2 && flist->malloced != FLIST_START) {
97 - rprintf(FINFO, "[%s] expand file_list to %.0f bytes, did%s move\n",
98 + rprintf(FCLIENT, "[%s] expand file_list to %.0f bytes, did%s move\n",
100 (double)sizeof flist->files[0] * flist->malloced,
101 (new_ptr == flist->files) ? " not" : "");
102 @@ -1077,6 +1077,7 @@ struct file_list *send_file_list(int f,
106 + rprintf(FLOG, "building file list\n");
107 if (show_filelist_p())
108 start_filelist_progress("building file list");
110 @@ -1343,6 +1344,7 @@ struct file_list *recv_file_list(int f)
111 unsigned short flags;
114 + rprintf(FLOG, "receiving file list\n");
115 if (show_filelist_p())
116 start_filelist_progress("receiving file list");
120 @@ -27,7 +27,7 @@ extern int verbose;
123 extern int log_format_has_i;
124 -extern int daemon_log_format_has_i;
125 +extern int logfile_format_has_i;
127 extern int am_server;
128 extern int am_daemon;
129 @@ -661,7 +661,7 @@ static int try_dests_reg(struct file_str
130 } else if (itemizing)
131 itemize(file, ndx, 0, stp, 0, 0, NULL);
132 if (verbose > 1 && maybe_ATTRS_REPORT) {
133 - code = daemon_log_format_has_i || dry_run
134 + code = logfile_format_has_i || dry_run
136 rprintf(code, "%s is uptodate\n", fname);
138 @@ -684,7 +684,7 @@ static int try_dests_reg(struct file_str
139 if (maybe_ATTRS_REPORT
140 && ((!itemizing && verbose && match_level == 2)
141 || (verbose > 1 && match_level == 3))) {
142 - code = daemon_log_format_has_i || dry_run
143 + code = logfile_format_has_i || dry_run
145 rprintf(code, "%s%s\n", fname,
146 match_level == 3 ? " is uptodate" : "");
147 @@ -746,7 +746,7 @@ static int try_dests_non(struct file_str
148 itemize(file, ndx, 0, &st, changes, 0, lp);
150 if (verbose > 1 && maybe_ATTRS_REPORT) {
151 - code = daemon_log_format_has_i || dry_run
152 + code = logfile_format_has_i || dry_run
154 rprintf(code, "%s is uptodate\n", fname);
156 @@ -1309,9 +1309,9 @@ void generate_files(int f_out, struct fi
157 if (protocol_version >= 29) {
159 maybe_ATTRS_REPORT = log_format_has_i ? 0 : ATTRS_REPORT;
160 - code = daemon_log_format_has_i ? 0 : FLOG;
161 + code = logfile_format_has_i ? 0 : FLOG;
162 } else if (am_daemon) {
163 - itemizing = daemon_log_format_has_i && do_xfers;
164 + itemizing = logfile_format_has_i && do_xfers;
165 maybe_ATTRS_REPORT = ATTRS_REPORT;
166 code = itemizing || !do_xfers ? FCLIENT : FINFO;
167 } else if (!am_server) {
170 @@ -44,18 +44,19 @@ extern int protocol_version;
171 extern int preserve_times;
172 extern int log_format_has_i;
173 extern int log_format_has_o_or_i;
174 -extern int daemon_log_format_has_o_or_i;
175 +extern int logfile_format_has_o_or_i;
176 extern mode_t orig_umask;
177 extern char *auth_user;
178 extern char *log_format;
179 +extern char *logfile_format;
180 +extern char *logfile_name;
181 #if defined HAVE_ICONV_OPEN && defined HAVE_ICONV_H
182 extern iconv_t ic_chck;
185 static int log_initialised;
186 static int logfile_was_closed;
187 -static char *logfname;
188 -static FILE *logfile;
189 +static FILE *logfile_fp;
192 int log_got_error = 0;
193 @@ -109,10 +110,10 @@ static void logit(int priority, char *bu
195 if (logfile_was_closed)
198 - fprintf(logfile,"%s [%d] %s",
200 + fprintf(logfile_fp, "%s [%d] %s",
201 timestring(time(NULL)), (int)getpid(), buf);
203 + fflush(logfile_fp);
205 syslog(priority, "%s", buf);
207 @@ -145,14 +146,14 @@ static void syslog_init()
208 static void logfile_open(void)
210 mode_t old_umask = umask(022 | orig_umask);
211 - logfile = fopen(logfname, "a");
212 + logfile_fp = fopen(logfile_name, "a");
216 int fopen_errno = errno;
217 /* Rsync falls back to using syslog on failure. */
219 rsyserr(FERROR, fopen_errno,
220 - "failed to open log-file %s", logfname);
221 + "failed to open log-file %s", logfile_name);
222 rprintf(FINFO, "Ignoring \"log file\" setting.\n");
225 @@ -171,9 +172,11 @@ void log_init(void)
229 - /* optionally use a log file instead of syslog */
230 - logfname = lp_log_file();
231 - if (logfname && *logfname)
232 + /* Optionally use a log file instead of syslog. (Non-daemon
233 + * rsyncs will have already set logfile_name, as needed.) */
235 + logfile_name = lp_log_file();
236 + if (logfile_name && *logfile_name)
240 @@ -181,10 +184,10 @@ void log_init(void)
242 void logfile_close(void)
246 logfile_was_closed = 1;
249 + fclose(logfile_fp);
254 @@ -243,9 +246,9 @@ void rwrite(enum logcode code, char *buf
258 - else if (am_daemon) {
259 + else if (am_daemon || logfile_name) {
262 + char msg[2048], *s;
263 int priority = code == FERROR ? LOG_WARNING : LOG_INFO;
266 @@ -254,10 +257,11 @@ void rwrite(enum logcode code, char *buf
267 if (!log_initialised)
269 strlcpy(msg, buf, MIN((int)sizeof msg, len + 1));
270 - logit(priority, msg);
271 + for (s = msg; *s == '\n' && s[1]; s++) {}
272 + logit(priority, s);
275 - if (code == FLOG || !am_server)
276 + if (code == FLOG || (am_daemon && !am_server))
278 } else if (code == FLOG)
280 @@ -403,26 +407,14 @@ void rflush(enum logcode code)
285 + if (am_daemon || code == FLOG)
289 - if (code == FLOG) {
293 - if (code == FERROR) {
294 + if (code == FERROR || am_server)
298 - if (code == FINFO) {
307 - if (!f) exit_cleanup(RERR_MESSAGEIO);
311 @@ -695,12 +687,12 @@ void log_item(struct file_struct *file,
313 char *s_or_r = am_sender ? "send" : "recv";
315 - if (lp_transfer_logging(module_id)) {
316 - log_formatted(FLOG, lp_log_format(module_id), s_or_r,
317 - file, initial_stats, iflags, hlink);
318 - } else if (log_format && !am_server) {
319 + if (log_format && !am_server) {
320 log_formatted(FNAME, log_format, s_or_r,
321 file, initial_stats, iflags, hlink);
322 + } else if (logfile_format) {
323 + log_formatted(FLOG, logfile_format, s_or_r,
324 + file, initial_stats, iflags, hlink);
328 @@ -712,7 +704,7 @@ void maybe_log_item(struct file_struct *
329 || log_format_has_i > 1 || (verbose > 1 && log_format_has_i));
330 int local_change = iflags & ITEM_LOCAL_CHANGE && significant_flags;
332 - if (am_daemon && !dry_run && see_item)
333 + if (logfile_name && !dry_run && see_item)
334 log_item(file, &stats, iflags, buf);
335 } else if (see_item || local_change || *buf
336 || (S_ISDIR(file->mode) && significant_flags))
337 @@ -740,10 +732,10 @@ void log_delete(char *fname, int mode)
341 - if (!am_daemon || dry_run || !lp_transfer_logging(module_id))
342 + if (!logfile_name || dry_run || !logfile_format)
345 - fmt = daemon_log_format_has_o_or_i ? lp_log_format(module_id) : "deleting %n";
346 + fmt = logfile_format_has_o_or_i ? logfile_format : "deleting %n";
347 log_formatted(FLOG, fmt, "del.", &file, &stats, ITEM_DELETED, NULL);
352 @@ -168,7 +168,6 @@ static void handle_stats(int f)
356 - log_exit(0, __FILE__, __LINE__);
357 if (f == -1 || !am_sender)
362 @@ -146,6 +146,8 @@ char *basis_dir[MAX_BASIS_DIRS+1];
363 char *config_file = NULL;
364 char *shell_cmd = NULL;
365 char *log_format = NULL;
366 +char *logfile_name = NULL;
367 +char *logfile_format = NULL;
368 char *password_file = NULL;
369 char *rsync_path = RSYNC_PATH;
370 char *backup_dir = NULL;
371 @@ -162,7 +164,9 @@ int verbose = 0;
373 int log_before_transfer = 0;
374 int log_format_has_i = 0;
375 +int logfile_format_has_i = 0;
376 int log_format_has_o_or_i = 0;
377 +int logfile_format_has_o_or_i = 0;
378 int always_checksum = 0;
381 @@ -359,6 +363,7 @@ void usage(enum logcode F)
382 rprintf(F," --progress show progress during transfer\n");
383 rprintf(F," -P same as --partial --progress\n");
384 rprintf(F," -i, --itemize-changes output a change-summary for all updates\n");
385 + rprintf(F," --log-file=FILE output what we're doing to a log file\n");
386 rprintf(F," --log-format=FORMAT output filenames using the specified format\n");
387 rprintf(F," --password-file=FILE read password from FILE\n");
388 rprintf(F," --list-only list the files instead of copying them\n");
389 @@ -492,6 +497,7 @@ static struct poptOption long_options[]
390 {"partial-dir", 0, POPT_ARG_STRING, &partial_dir, 0, 0, 0 },
391 {"delay-updates", 0, POPT_ARG_NONE, &delay_updates, 0, 0, 0 },
392 {"prune-empty-dirs",'m', POPT_ARG_NONE, &prune_empty_dirs, 0, 0, 0 },
393 + {"log-file", 0, POPT_ARG_STRING, &logfile_name, 0, 0, 0 },
394 {"log-format", 0, POPT_ARG_STRING, &log_format, 0, 0, 0 },
395 {"itemize-changes", 'i', POPT_ARG_NONE, 0, 'i', 0, 0 },
396 {"bwlimit", 0, POPT_ARG_INT, &bwlimit, 0, 0, 0 },
397 @@ -1311,6 +1317,21 @@ int parse_arguments(int *argc, const cha
398 if (log_format_has_i || log_format_has(log_format, 'o'))
399 log_format_has_o_or_i = 1;
402 + logfile_name = NULL;
403 + else if (logfile_name) {
405 + logfile_format = "%i %n%L";
406 + logfile_format_has_i = logfile_format_has_o_or_i = 1;
407 + } else if (log_format) {
408 + logfile_format = log_format;
409 + logfile_format_has_i = log_format_has_i;
410 + logfile_format_has_o_or_i = log_format_has_o_or_i;
412 + log_before_transfer = !am_server;
416 if (daemon_bwlimit && (!bwlimit || bwlimit > daemon_bwlimit))
417 bwlimit = daemon_bwlimit;
421 @@ -103,7 +103,7 @@ static void rprint_progress(OFF_T ofs, O
425 - rprintf(FINFO, "%12s %3d%% %7.2f%s %4d:%02d:%02d%s",
426 + rprintf(FCLIENT, "%12s %3d%% %7.2f%s %4d:%02d:%02d%s",
427 human_num(ofs), pct, rate, units,
428 remain_h, remain_m, remain_s, eol);
432 @@ -27,7 +27,7 @@ extern int am_server;
433 extern int do_progress;
434 extern int log_before_transfer;
435 extern int log_format_has_i;
436 -extern int daemon_log_format_has_i;
437 +extern int logfile_format_has_i;
438 extern int csum_length;
439 extern int read_batch;
440 extern int write_batch;
441 @@ -341,8 +341,7 @@ int recv_files(int f_in, struct file_lis
442 struct file_struct *file;
443 struct stats initial_stats;
444 int save_make_backups = make_backups;
445 - int itemizing = am_daemon ? daemon_log_format_has_i
446 - : !am_server && log_format_has_i;
447 + int itemizing = am_server ? logfile_format_has_i : log_format_has_i;
448 int max_phase = protocol_version >= 29 ? 2 : 1;
457 -extern int daemon_log_format_has_i;
458 +extern int logfile_format_has_i;
459 extern int preserve_perms;
460 extern int preserve_executability;
461 extern int preserve_times;
462 @@ -218,7 +218,7 @@ int set_file_attrs(char *fname, struct f
465 if (verbose > 1 && flags & ATTRS_REPORT) {
466 - enum logcode code = daemon_log_format_has_i || dry_run
467 + enum logcode code = logfile_format_has_i || dry_run
470 rprintf(code, "%s\n", fname);
476 /* Log-message categories. Only FERROR and FINFO get sent over the socket.
477 - * FLOG and FCLIENT are only used on the daemon side for custom logging,
478 - * while FNAME is only used on the client side. */
479 + * FLOG only goes to the log file, not to the client; FCLIENT is the opposite.
480 + * FNAME is a client-side message when outputting a filename on its own. */
481 enum logcode { FERROR=1, FINFO=2, FLOG=3, FCLIENT=4, FNAME=5, FSOCKERR=6 };
483 /* Messages types that are sent over the message channel. The logcode
486 @@ -387,6 +387,7 @@ to the detailed description below for a
487 --progress show progress during transfer
488 -P same as --partial --progress
489 -i, --itemize-changes output a change-summary for all updates
490 + --log-file=FILE output what we're doing to a log file
491 --log-format=FORMAT output filenames using the specified format
492 --password-file=FILE read password from FILE
493 --list-only list the files instead of copying them
494 @@ -1423,6 +1424,23 @@ the string "*deleting" for each item tha
495 you are talking to a recent enough rsync that it logs deletions instead of
496 outputting them as a verbose message).
498 +dit(bf(--log-file=FILE)) This option causes rsync to log what it is doing
499 +to a file. This is similar to the logging that a daemon does, but can be
500 +requested for the client side and/or the server side of a non-daemon
501 +transfer. If specified as a client option, transfer logging will in effect
502 +if the bf(--log-format) option was either specified or implied (e.g.
503 +bf(--verbose) implies a basic log format). If explicitly sent to a server
504 +via the bf(--rsync-path) option, transfer logging will always occur using
505 +the default bf(--itemize-changes) format.
507 +Here's a example command that requests the remote side to log what is
510 +verb( rsync -av --rsync-path="path --log-file=/tmp/rlog" src/ dest/)
512 +This is very useful if you need to debug why a connection is closing
515 dit(bf(--log-format=FORMAT)) This allows you to specify exactly what the
516 rsync client outputs to the user on a per-file basis. The format is a text
517 string containing embedded single-character escape sequences prefixed with
520 @@ -25,7 +25,7 @@ extern int am_server;
521 extern int am_daemon;
522 extern int log_before_transfer;
523 extern int log_format_has_i;
524 -extern int daemon_log_format_has_i;
525 +extern int logfile_format_has_i;
526 extern int csum_length;
527 extern int append_mode;
529 @@ -215,8 +215,7 @@ void send_files(struct file_list *flist,
530 int phase = 0, max_phase = protocol_version >= 29 ? 2 : 1;
531 struct stats initial_stats;
532 int save_make_backups = make_backups;
533 - int itemizing = am_daemon ? daemon_log_format_has_i
534 - : !am_server && log_format_has_i;
535 + int itemizing = am_server ? logfile_format_has_i : log_format_has_i;
536 int f_xfer = write_batch < 0 ? batch_fd : f_out;