X-Git-Url: https://mattmccutchen.net/rsync/rsync.git/blobdiff_plain/ec52c3b9dadc91c7c5b27d6ff0e2b736b1be0b5a..9ef506a2b2270b242cfd5734722c44e7e3d3478f:/log.c diff --git a/log.c b/log.c index 01fb10e0..2355214e 100644 --- a/log.c +++ b/log.c @@ -37,8 +37,10 @@ extern int msg_fd_out; extern int allow_8bit_chars; extern int protocol_version; extern int preserve_times; +extern int in_exit_cleanup; extern int stdout_format_has_i; extern int stdout_format_has_o_or_i; +extern int logfile_format_has_i; extern int logfile_format_has_o_or_i; extern mode_t orig_umask; extern char *auth_user; @@ -48,6 +50,8 @@ extern char *logfile_name; #if defined HAVE_ICONV_OPEN && defined HAVE_ICONV_H extern iconv_t ic_chck; #endif +extern char curr_dir[]; +extern unsigned int module_dirlen; static int log_initialised; static int logfile_was_closed; @@ -87,6 +91,13 @@ struct { { 0, NULL } }; +#define EXIT_OR_RETURN(err) \ + do { \ + if (in_exit_cleanup) \ + return; \ + exit_cleanup(err); \ + } while (0) + /* * Map from rsync error code to name, or return NULL. @@ -128,7 +139,7 @@ static void syslog_init() #endif #ifdef LOG_DAEMON - openlog("rsyncd", options, lp_syslog_facility()); + openlog("rsyncd", options, lp_syslog_facility(module_id)); #else openlog("rsyncd", options); #endif @@ -153,11 +164,26 @@ static void logfile_open(void) } } -void log_init(void) +void log_init(int restart) { - if (log_initialised) - return; - log_initialised = 1; + if (log_initialised) { + if (!restart) + return; + if (strcmp(logfile_name, lp_log_file(module_id)) != 0) { + if (logfile_fp) { + fclose(logfile_fp); + logfile_fp = NULL; + } else + closelog(); + logfile_name = NULL; + } else if (*logfile_name) + return; /* unchanged, non-empty "log file" names */ + else if (lp_syslog_facility(-1) != lp_syslog_facility(module_id)) + closelog(); + else + return; /* unchanged syslog settings */ + } else + log_initialised = 1; /* This looks pointless, but it is needed in order for the * C library on some systems to fetch the timezone info @@ -167,7 +193,7 @@ void log_init(void) /* Optionally use a log file instead of syslog. (Non-daemon * rsyncs will have already set logfile_name, as needed.) */ if (am_daemon && !logfile_name) - logfile_name = lp_log_file(); + logfile_name = lp_log_file(module_id); if (logfile_name && *logfile_name) logfile_open(); else @@ -204,13 +230,13 @@ static void filtered_fwrite(FILE *f, const char *buf, int len, int use_isprint) && ((use_isprint && !isprint(*(uchar*)s)) || *(uchar*)s < ' '))) { if (s != buf && fwrite(buf, s - buf, 1, f) != 1) - exit_cleanup(RERR_MESSAGEIO); + EXIT_OR_RETURN(RERR_MESSAGEIO); fprintf(f, "\\#%03o", *(uchar*)s); buf = s + 1; } } if (buf != end && fwrite(buf, end - buf, 1, f) != 1) - exit_cleanup(RERR_MESSAGEIO); + EXIT_OR_RETURN(RERR_MESSAGEIO); } /* this is the underlying (unformatted) rsync debugging function. Call @@ -222,7 +248,7 @@ void rwrite(enum logcode code, char *buf, int len) FILE *f = NULL; if (len < 0) - exit_cleanup(RERR_MESSAGEIO); + EXIT_OR_RETURN(RERR_MESSAGEIO); if (am_server && msg_fd_out >= 0) { /* Pass the message to our sibling. */ @@ -235,22 +261,18 @@ void rwrite(enum logcode code, char *buf, int len) if (code == FCLIENT) code = FINFO; - else if (code == FNAME) { - if (am_server) - code = FINFO; - } else if (am_daemon || logfile_name) { + else if (am_daemon || logfile_name) { static int in_block; - char msg[2048], *s; + char msg[2048]; int priority = code == FERROR ? LOG_WARNING : LOG_INFO; if (in_block) return; in_block = 1; if (!log_initialised) - log_init(); + log_init(0); strlcpy(msg, buf, MIN((int)sizeof msg, len + 1)); - for (s = msg; *s == '\n' && s[1]; s++) {} - logit(priority, s); + logit(priority, msg); in_block = 0; if (code == FLOG || (am_daemon && !am_server)) @@ -275,21 +297,12 @@ void rwrite(enum logcode code, char *buf, int len) case FERROR: log_got_error = 1; f = stderr; - goto pre_scan; - case FINFO: - f = am_server ? stderr : stdout; - pre_scan: - while (len > 1 && *buf == '\n') { - fputc(*buf, f); - buf++; - len--; - } break; - case FNAME: + case FINFO: f = am_server ? stderr : stdout; break; default: - exit_cleanup(RERR_MESSAGEIO); + EXIT_OR_RETURN(RERR_MESSAGEIO); } trailing_CR_or_NL = len && (buf[len-1] == '\n' || buf[len-1] == '\r') @@ -393,7 +406,7 @@ void rsyserr(enum logcode code, int errcode, const char *format, ...) ": %s (%d)\n", strerror(errcode), errcode); } if (len >= sizeof buf) - exit_cleanup(RERR_MESSAGEIO); + EXIT_OR_RETURN(RERR_MESSAGEIO); rwrite(code, buf, len); } @@ -431,7 +444,7 @@ static void log_formatted(enum logcode code, char *format, char *op, total = strlcpy(buf, format, sizeof buf); if (total > MAXPATHLEN) { rprintf(FERROR, "log-format string is WAY too long!\n"); - exit_cleanup(RERR_MESSAGEIO); + EXIT_OR_RETURN(RERR_MESSAGEIO); } buf[total++] = '\n'; buf[total] = '\0'; @@ -510,6 +523,14 @@ static void log_formatted(enum logcode code, char *format, char *op, strlcpy(n, buf2, MAXPATHLEN); else n = buf2; + } else if (*n != '/') { + pathjoin(buf2, sizeof buf2, + curr_dir + module_dirlen, n); + clean_fname(buf2, 0); + if (fmt[1]) + strlcpy(n, buf2, MAXPATHLEN); + else + n = buf2; } else clean_fname(n, 0); if (*n == '/') @@ -675,7 +696,7 @@ int log_format_has(const char *format, char esc) return 0; } -/* Log the transfer of a file. If the code is FNAME, the output just goes +/* Log the transfer of a file. If the code is FCLIENT, the output just goes * to stdout. If it is FLOG, it just goes to the log file. Otherwise we * output to both. */ void log_item(enum logcode code, struct file_struct *file, @@ -684,10 +705,10 @@ void log_item(enum logcode code, struct file_struct *file, char *s_or_r = am_sender ? "send" : "recv"; if (code != FLOG && stdout_format && !am_server) { - log_formatted(FNAME, stdout_format, s_or_r, + log_formatted(FCLIENT, stdout_format, s_or_r, file, initial_stats, iflags, hlink); } - if (code != FNAME && logfile_format && *logfile_format) { + if (code != FCLIENT && logfile_format && *logfile_format) { log_formatted(FLOG, logfile_format, s_or_r, file, initial_stats, iflags, hlink); } @@ -701,11 +722,14 @@ void maybe_log_item(struct file_struct *file, int iflags, int itemizing, || stdout_format_has_i > 1 || (verbose > 1 && stdout_format_has_i)); int local_change = iflags & ITEM_LOCAL_CHANGE && significant_flags; if (am_server) { - if (logfile_name && !dry_run && see_item) + if (logfile_name && !dry_run && see_item + && (significant_flags || logfile_format_has_i)) log_item(FLOG, file, &stats, iflags, buf); } else if (see_item || local_change || *buf - || (S_ISDIR(file->mode) && significant_flags)) - log_item(FINFO, file, &stats, iflags, buf); + || (S_ISDIR(file->mode) && significant_flags)) { + enum logcode code = significant_flags || logfile_format_has_i ? FINFO : FCLIENT; + log_item(code, file, &stats, iflags, buf); + } } void log_delete(char *fname, int mode)