diff options
Diffstat (limited to 'src/lib/err/torerr.c')
-rw-r--r-- | src/lib/err/torerr.c | 31 |
1 files changed, 28 insertions, 3 deletions
diff --git a/src/lib/err/torerr.c b/src/lib/err/torerr.c index a5c00ca389..2de75c0be4 100644 --- a/src/lib/err/torerr.c +++ b/src/lib/err/torerr.c @@ -1,7 +1,7 @@ /* Copyright (c) 2001, Matej Pfajfar. * Copyright (c) 2001-2004, Roger Dingledine. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson. - * Copyright (c) 2007-2019, The Tor Project, Inc. */ + * Copyright (c) 2007-2020, The Tor Project, Inc. */ /* See LICENSE for licensing information */ /** @@ -151,6 +151,30 @@ tor_log_reset_sigsafe_err_fds(void) } /** + * Flush the list of fds that get errors from inside a signal handler or + * other emergency condition. These fds are shared with the logging code: + * flushing them also flushes the log buffers. + * + * This function is safe to call during signal handlers. + */ +void +tor_log_flush_sigsafe_err_fds(void) +{ + /* If we don't have fsync() in unistd.h, we can't flush the logs. */ +#ifdef HAVE_FSYNC + int n_fds, i; + const int *fds = NULL; + + n_fds = tor_log_get_sigsafe_err_fds(&fds); + for (i = 0; i < n_fds; ++i) { + /* This function is called on error and on shutdown, so we don't log, or + * take any other action, if fsync() fails. */ + (void)fsync(fds[i]); + } +#endif /* defined(HAVE_FSYNC) */ +} + +/** * Set the granularity (in ms) to use when reporting fatal errors outside * the logging system. */ @@ -191,12 +215,13 @@ tor_raw_assertion_failed_msg_(const char *file, int line, const char *expr, /** * Call the abort() function to kill the current process with a fatal - * error. This is a separate function, so that log users don't have to include - * the header for abort(). + * error. But first, flush the raw error file descriptors, so error messages + * are written before process termination. **/ void tor_raw_abort_(void) { + tor_log_flush_sigsafe_err_fds(); abort(); } |