tor  0.4.2.0-alpha-dev
Functions | Variables
torerr.c File Reference
#include "orconfig.h"
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "lib/err/torerr.h"
#include "lib/err/backtrace.h"

Go to the source code of this file.

Functions

static int tor_log_err_sigsafe_write (const char *s)
 
void tor_log_err_sigsafe (const char *m,...)
 
int tor_log_get_sigsafe_err_fds (const int **out)
 
void tor_log_set_sigsafe_err_fds (const int *fds, int n)
 
void tor_log_reset_sigsafe_err_fds (void)
 
void tor_log_close_sigsafe_err_fds (void)
 
void tor_log_sigsafe_err_set_granularity (int ms)
 
void tor_raw_assertion_failed_msg_ (const char *file, int line, const char *expr, const char *msg)
 
void tor_raw_abort_ (void)
 
static int format_number_sigsafe (unsigned long x, char *buf, int buf_len, unsigned int radix)
 
int format_hex_number_sigsafe (unsigned long x, char *buf, int buf_len)
 
int format_dec_number_sigsafe (unsigned long x, char *buf, int buf_len)
 

Variables

static int sigsafe_log_fds [TOR_SIGSAFE_LOG_MAX_FDS] = { STDERR_FILENO }
 
static int n_sigsafe_log_fds = 1
 
static int log_granularity = 1000
 

Detailed Description

Handling code for unrecoverable emergencies, at a lower level than the logging code.

There are plenty of places that things can go wrong in Tor's backend libraries: the allocator can fail, the locking subsystem can fail, and so on. But since these subsystems are used themselves by the logging module, they can't use the logging code directly to report their errors.

As a workaround, the logging code provides this module with a set of raw fds to be used for reporting errors in the lowest-level Tor code.

Definition in file torerr.c.

Function Documentation

◆ format_dec_number_sigsafe()

int format_dec_number_sigsafe ( unsigned long  x,
char *  buf,
int  buf_len 
)

As format_hex_number_sigsafe, but format the number in base 10.

Definition at line 307 of file torerr.c.

Referenced by tor_log_err_sigsafe(), and tor_raw_assertion_failed_msg_().

◆ format_hex_number_sigsafe()

int format_hex_number_sigsafe ( unsigned long  x,
char *  buf,
int  buf_len 
)

Helper function to output hex numbers from within a signal handler.

Writes the nul-terminated hexadecimal digits of x into a buffer buf of size buf_len, and return the actual number of digits written, not counting the terminal NUL.

If there is insufficient space, write nothing and return 0.

This accepts an unsigned int because format_helper_exit_status() needs to call it with a signed int and an unsigned char, and since the C standard does not guarantee that an int is wider than a char (an int must be at least 16 bits but it is permitted for a char to be that wide as well), we can't assume a signed int is sufficient to accommodate an unsigned char. Thus, callers will still need to add any required '-' to the final string.

For most purposes, you'd want to use tor_snprintf("%x") instead of this function; it's designed to be used in code paths where you can't call arbitrary C functions.

Definition at line 300 of file torerr.c.

◆ tor_log_close_sigsafe_err_fds()

void tor_log_close_sigsafe_err_fds ( void  )

Close the list of fds that get errors from inside a signal handler or other emergency condition. These fds are shared with the logging code: closing them flushes the log buffers, and prevents any further logging.

This function closes stderr, so it should only be called immediately before process shutdown.

Definition at line 162 of file torerr.c.

References tor_log_get_sigsafe_err_fds(), and tor_log_set_sigsafe_err_fds().

Referenced by tor_raw_abort_().

◆ tor_log_err_sigsafe()

void tor_log_err_sigsafe ( const char *  m,
  ... 
)

Given a list of string arguments ending with a NULL, writes them to our logs and to stderr (if possible). This function is safe to call from within a signal handler.

Definition at line 70 of file torerr.c.

References format_dec_number_sigsafe(), log_granularity, and tor_log_err_sigsafe_write().

Referenced by nodump_mem(), and tor_raw_assertion_failed_msg_().

◆ tor_log_err_sigsafe_write()

static int tor_log_err_sigsafe_write ( const char *  s)
static

Write s to each element of sigsafe_log_fds. Return 0 on success, -1 on failure.

Definition at line 53 of file torerr.c.

References n_sigsafe_log_fds, and sigsafe_log_fds.

Referenced by tor_log_err_sigsafe(), and tor_raw_assertion_failed_msg_().

◆ tor_log_get_sigsafe_err_fds()

int tor_log_get_sigsafe_err_fds ( const int **  out)

Set *out to a pointer to an array of the fds to log errors to from inside a signal handler or other emergency condition. Return the number of elements in the array.

Definition at line 103 of file torerr.c.

References n_sigsafe_log_fds, and sigsafe_log_fds.

Referenced by tor_log_close_sigsafe_err_fds().

◆ tor_log_reset_sigsafe_err_fds()

void tor_log_reset_sigsafe_err_fds ( void  )

Reset the list of emergency error fds to its default.

Definition at line 147 of file torerr.c.

References tor_log_set_sigsafe_err_fds().

◆ tor_log_set_sigsafe_err_fds()

void tor_log_set_sigsafe_err_fds ( const int *  fds,
int  n 
)

Update the list of fds that get errors from inside a signal handler or other emergency condition. Ignore any beyond the first TOR_SIGSAFE_LOG_MAX_FDS.

These fds must remain open even after the log module has shut down. (And they should remain open even while logs are being reconfigured.) Therefore, any fds closed by the log module should be dup()ed, and the duplicate fd should be given to the err module in fds. In particular, the log module closes the file log fds, but does not close the stdio log fds.

If fds is NULL or n is 0, clears the list of error fds.

Definition at line 123 of file torerr.c.

References n_sigsafe_log_fds, sigsafe_log_fds, and TOR_SIGSAFE_LOG_MAX_FDS.

Referenced by tor_log_close_sigsafe_err_fds(), and tor_log_reset_sigsafe_err_fds().

◆ tor_log_sigsafe_err_set_granularity()

void tor_log_sigsafe_err_set_granularity ( int  ms)

Set the granularity (in ms) to use when reporting fatal errors outside the logging system.

Definition at line 184 of file torerr.c.

References log_granularity.

Referenced by set_log_time_granularity().

◆ tor_raw_abort_()

void tor_raw_abort_ ( void  )

Call the abort() function to kill the current process with a fatal error. But first, close the raw error file descriptors, so error messages are written before process termination.

Definition at line 224 of file torerr.c.

References tor_log_close_sigsafe_err_fds().

Referenced by tor_abort_().

◆ tor_raw_assertion_failed_msg_()

void tor_raw_assertion_failed_msg_ ( const char *  file,
int  line,
const char *  expr,
const char *  msg 
)

Log an emergency assertion failure message.

This kind of message is safe to send from within a log handler, a signal handler, or other emergency situation.

Definition at line 196 of file torerr.c.

References format_dec_number_sigsafe(), get_tor_backtrace_version(), tor_log_err_sigsafe(), and tor_log_err_sigsafe_write().

Variable Documentation

◆ log_granularity

int log_granularity = 1000
static

Log granularity in milliseconds.

Definition at line 48 of file torerr.c.

Referenced by tor_log_err_sigsafe(), and tor_log_sigsafe_err_set_granularity().

◆ n_sigsafe_log_fds

int n_sigsafe_log_fds = 1
static

The number of elements used in sigsafe_log_fds

Definition at line 46 of file torerr.c.

Referenced by tor_log_err_sigsafe_write(), tor_log_get_sigsafe_err_fds(), and tor_log_set_sigsafe_err_fds().

◆ sigsafe_log_fds

int sigsafe_log_fds[TOR_SIGSAFE_LOG_MAX_FDS] = { STDERR_FILENO }
static

Array of fds to log crash-style warnings to.

Definition at line 44 of file torerr.c.

Referenced by tor_log_err_sigsafe_write(), tor_log_get_sigsafe_err_fds(), and tor_log_set_sigsafe_err_fds().