diff options
author | teor <teor@torproject.org> | 2020-02-12 12:47:15 +1000 |
---|---|---|
committer | teor <teor@torproject.org> | 2020-02-13 00:00:41 +1000 |
commit | 3d1ef3b6f89e760b4340ba77e0b3db1246dc5c80 (patch) | |
tree | 2cc62a0f4ed8a74f60ae4b57533f3fe93008d751 /src/lib/log | |
parent | e0ea7407a4370c977ebbf0b70712c9e5ff7937fa (diff) | |
download | tor-3d1ef3b6f89e760b4340ba77e0b3db1246dc5c80.tar.gz tor-3d1ef3b6f89e760b4340ba77e0b3db1246dc5c80.zip |
err/log: Stop closing stderr and stdout during shutdown
Closing these file descriptors can hide sanitiser logs.
Instead, flush the logs before tor exits, using fsync().
Some Windows environments don't have fsync(), so we check
for it at compile time.
Fixes bug 33087; bugfix on 0.4.1.6.
Diffstat (limited to 'src/lib/log')
-rw-r--r-- | src/lib/log/log.c | 29 | ||||
-rw-r--r-- | src/lib/log/log.h | 2 | ||||
-rw-r--r-- | src/lib/log/util_bug.c | 2 |
3 files changed, 16 insertions, 17 deletions
diff --git a/src/lib/log/log.c b/src/lib/log/log.c index 75f8f79da2..4813a4faec 100644 --- a/src/lib/log/log.c +++ b/src/lib/log/log.c @@ -667,12 +667,9 @@ tor_log_update_sigsafe_err_fds(void) /* log_fds and err_fds contain matching entries: log_fds are the fds used by * the log module, and err_fds are the fds used by the err module. - * For stdio logs, the log_fd and err_fd values are identical, - * and the err module closes the fd on shutdown. - * For file logs, the err_fd is a dup() of the log_fd, - * and the log and err modules both close their respective fds on shutdown. - * (Once all fds representing a file are closed, the underlying file is - * closed.) + * For stdio logs, the log_fd and err_fd values are identical. + * For file logs, the err_fd is a dup() of the log_fd. + * Both the log and err modules flush these fds on shutdown. */ int log_fds[TOR_SIGSAFE_LOG_MAX_FDS]; int err_fds[TOR_SIGSAFE_LOG_MAX_FDS]; @@ -704,12 +701,12 @@ tor_log_update_sigsafe_err_fds(void) log_fds[n_fds] = lf->fd; if (lf->needs_close) { /* File log fds are duplicated, because close_log() closes the log - * module's fd, and tor_log_close_sigsafe_err_fds() closes the err + * module's fd, and tor_log_flush_sigsafe_err_fds() closes the err * module's fd. Both refer to the same file. */ err_fds[n_fds] = dup(lf->fd); } else { /* stdio log fds are not closed by the log module. - * tor_log_close_sigsafe_err_fds() closes stdio logs. */ + * tor_log_flush_sigsafe_err_fds() closes stdio logs. */ err_fds[n_fds] = lf->fd; } n_fds++; @@ -841,16 +838,16 @@ logs_free_all(void) * log mutex. */ } -/** Close signal-safe log files. - * Closing the log files makes the process and OS flush log buffers. +/** Flush the signal-safe log files. * - * This function is safe to call from a signal handler. It should only be - * called when shutting down the log or err modules. It is currenly called - * by the err module, when terminating the process on an abnormal condition. + * This function is safe to call from a signal handler. It is currenly called + * by the BUG() macros, when terminating the process on an abnormal condition. */ void -logs_close_sigsafe(void) +logs_flush_sigsafe(void) { + /* If we don't have fsync() in unistd.h, we can't flush the logs. */ +#ifdef HAVE_FSYNC logfile_t *victim, *next; /* We can't LOCK_LOGS() in a signal handler, because it may call * signal-unsafe functions. And we can't deallocate memory, either. */ @@ -860,9 +857,11 @@ logs_close_sigsafe(void) victim = next; next = next->next; if (victim->needs_close) { - close_log_sigsafe(victim); + /* We can't do anything useful if the flush fails. */ + (void)fsync(victim->fd); } } +#endif } /** Remove and free the log entry <b>victim</b> from the linked-list diff --git a/src/lib/log/log.h b/src/lib/log/log.h index cb588635d7..aafbf9be2f 100644 --- a/src/lib/log/log.h +++ b/src/lib/log/log.h @@ -186,7 +186,7 @@ void logs_set_domain_logging(int enabled); int get_min_log_level(void); void switch_logs_debug(void); void logs_free_all(void); -void logs_close_sigsafe(void); +void logs_flush_sigsafe(void); void add_default_log(int min_severity); void close_temp_logs(void); void rollback_log_changes(void); diff --git a/src/lib/log/util_bug.c b/src/lib/log/util_bug.c index de44d30e4e..581ae85f47 100644 --- a/src/lib/log/util_bug.c +++ b/src/lib/log/util_bug.c @@ -172,7 +172,7 @@ tor_bug_occurred_(const char *fname, unsigned int line, void tor_abort_(void) { - logs_close_sigsafe(); + logs_flush_sigsafe(); tor_raw_abort_(); } |