Macros | Functions | Variables
main.c File Reference
#include "core/or/or.h"
#include "app/config/config.h"
#include "app/config/statefile.h"
#include "app/main/main.h"
#include "app/main/ntmain.h"
#include "app/main/subsysmgr.h"
#include "core/mainloop/connection.h"
#include "core/mainloop/cpuworker.h"
#include "core/mainloop/mainloop.h"
#include "core/mainloop/netstatus.h"
#include "core/or/channel.h"
#include "core/or/channelpadding.h"
#include "core/or/circuitpadding.h"
#include "core/or/channeltls.h"
#include "core/or/circuitlist.h"
#include "core/or/circuitmux_ewma.h"
#include "core/or/command.h"
#include "core/or/connection_edge.h"
#include "core/or/connection_or.h"
#include "core/or/dos.h"
#include "core/or/policies.h"
#include "core/or/protover.h"
#include "core/or/relay.h"
#include "core/or/scheduler.h"
#include "core/or/status.h"
#include "core/or/versions.h"
#include "feature/api/tor_api.h"
#include "feature/api/tor_api_internal.h"
#include "feature/client/addressmap.h"
#include "feature/client/bridges.h"
#include "feature/client/entrynodes.h"
#include "feature/client/transports.h"
#include "feature/control/control.h"
#include "feature/dirauth/bwauth.h"
#include "feature/dirauth/keypin.h"
#include "feature/dirauth/process_descs.h"
#include "feature/dircache/consdiffmgr.h"
#include "feature/dircache/dirserv.h"
#include "feature/dirparse/routerparse.h"
#include "feature/hibernate/hibernate.h"
#include "feature/hs/hs_cache.h"
#include "feature/nodelist/authcert.h"
#include "feature/nodelist/microdesc.h"
#include "feature/nodelist/networkstatus.h"
#include "feature/nodelist/nodelist.h"
#include "feature/nodelist/routerlist.h"
#include "feature/relay/dns.h"
#include "feature/relay/ext_orport.h"
#include "feature/relay/onion_queue.h"
#include "feature/relay/routerkeys.h"
#include "feature/relay/routermode.h"
#include "feature/rend/rendcache.h"
#include "feature/rend/rendclient.h"
#include "feature/rend/rendservice.h"
#include "feature/stats/geoip_stats.h"
#include "feature/stats/predict_ports.h"
#include "feature/stats/rephist.h"
#include "lib/compress/compress.h"
#include "lib/buf/buffers.h"
#include "lib/crypt_ops/crypto_rand.h"
#include "lib/crypt_ops/crypto_s2k.h"
#include "lib/geoip/geoip.h"
#include "lib/net/resolve.h"
#include "lib/process/waitpid.h"
#include "lib/meminfo/meminfo.h"
#include "lib/osinfo/uname.h"
#include "lib/sandbox/sandbox.h"
#include "lib/fs/lockfile.h"
#include "lib/tls/tortls.h"
#include "lib/evloop/compat_libevent.h"
#include "lib/encoding/confline.h"
#include "lib/evloop/timers.h"
#include "lib/crypt_ops/crypto_init.h"
#include "lib/version/torversion.h"
#include <event2/event.h>
#include "feature/dirauth/dirvote.h"
#include "feature/dirauth/authmode.h"
#include "feature/dirauth/shared_random.h"
#include "core/or/or_connection_st.h"
#include "core/or/port_cfg_st.h"

Go to the source code of this file.


#define UNIX_ONLY   1
#define OPEN(name)   sandbox_cfg_allow_open_filename(&cfg, tor_strdup(name))
#define OPEN_DATADIR(name)   sandbox_cfg_allow_open_filename(&cfg, get_datadir_fname(name))
#define OPEN_DATADIR2(name, name2)   sandbox_cfg_allow_open_filename(&cfg, get_datadir_fname2((name), (name2)))
#define OPEN_DATADIR_SUFFIX(name, suffix)
#define OPEN_DATADIR2_SUFFIX(name, name2, suffix)
#define OPEN_KEY_DIRECTORY()   sandbox_cfg_allow_open_filename(&cfg, tor_strdup(options->KeyDirectory))
#define OPEN_CACHEDIR(name)   sandbox_cfg_allow_open_filename(&cfg, get_cachedir_fname(name))
#define OPEN_CACHEDIR_SUFFIX(name, suffix)
#define OPEN_KEYDIR(name)   sandbox_cfg_allow_open_filename(&cfg, get_keydir_fname(name))
#define OPEN_KEYDIR_SUFFIX(name, suffix)
#define RENAME_SUFFIX(name, suffix)
#define RENAME_SUFFIX2(prefix, name, suffix)
#define RENAME_CACHEDIR_SUFFIX(name, suffix)
#define RENAME_KEYDIR_SUFFIX(name, suffix)
#define STAT_DATADIR(name)   sandbox_cfg_allow_stat_filename(&cfg, get_datadir_fname(name))
#define STAT_CACHEDIR(name)   sandbox_cfg_allow_stat_filename(&cfg, get_cachedir_fname(name))
#define STAT_DATADIR2(name, name2)   sandbox_cfg_allow_stat_filename(&cfg, get_datadir_fname2((name), (name2)))
#define STAT_KEY_DIRECTORY()   sandbox_cfg_allow_stat_filename(&cfg, tor_strdup(options->KeyDirectory))


void evdns_shutdown (int)
static void dumpmemusage (int severity)
static void dumpstats (int severity)
static void process_signal (int sig)
static int do_hup (void)
static void signal_callback (evutil_socket_t fd, short events, void *arg)
void handle_signals (void)
void activate_signal (int signal_num)
int tor_init (int argc, char *argv[])
int try_locking (const or_options_t *options, int err_if_locked)
int have_lockfile (void)
void release_lockfile (void)
void tor_free_all (int postfork)
void tor_remove_file (const char *filename)
void tor_cleanup (void)
static int do_list_fingerprint (void)
static void do_hash_password (void)
static int do_dump_config (void)
static void init_addrinfo (void)
static sandbox_cfg_tsandbox_init_filter (void)
int run_tor_main_loop (void)
int tor_run_main (const tor_main_configuration_t *tor_cfg)


int quiet_level = 0
struct {
   int   signal_value
   int   try_to_register
   struct event *   signal_event
signal_handlers []
static tor_lockfile_tlockfile = NULL

Detailed Description

Invocation module. Initializes subsystems and runs the main loop.

Definition in file main.c.

Macro Definition Documentation


#define OPEN_CACHEDIR_SUFFIX (   name,
do { \
OPEN_CACHEDIR(name suffix); \
} while (0)


#define OPEN_DATADIR2_SUFFIX (   name,
do { \
OPEN_DATADIR2(name, name2); \
OPEN_DATADIR2(name, name2 suffix); \
} while (0)


#define OPEN_DATADIR_SUFFIX (   name,
do { \
OPEN_DATADIR(name suffix); \
} while (0)


#define OPEN_KEYDIR_SUFFIX (   name,
do { \
OPEN_KEYDIR(name); \
OPEN_KEYDIR(name suffix); \
} while (0)


#define RENAME_CACHEDIR_SUFFIX (   name,
sandbox_cfg_allow_rename(&cfg, \
get_cachedir_fname(name suffix), \


#define RENAME_KEYDIR_SUFFIX (   name,
sandbox_cfg_allow_rename(&cfg, \
get_keydir_fname(name suffix), \


#define RENAME_SUFFIX (   name,
sandbox_cfg_allow_rename(&cfg, \
get_datadir_fname(name suffix), \


#define RENAME_SUFFIX2 (   prefix,
sandbox_cfg_allow_rename(&cfg, \
get_datadir_fname2(prefix, name suffix), \
get_datadir_fname2(prefix, name))

Function Documentation

◆ do_dump_config()

static int do_dump_config ( void  )

Entry point for configuration dumping: write the configuration to stdout.

Definition at line 933 of file main.c.

References or_options_t::command_arg.

◆ do_hash_password()

static void do_hash_password ( void  )

Entry point for password hashing: take the desired password from the command line, and print its salted hash to stdout.

Definition at line 915 of file main.c.

References base16_encode(), DIGEST_LEN, S2K_RFC2440_SPECIFIER_LEN, and secret_to_key_rfc2440().

◆ do_hup()

static int do_hup ( void  )

Called when we get a SIGHUP: reload configuration files and keys, retry all connections, and so on.

Definition at line 141 of file main.c.

◆ do_list_fingerprint()

static int do_list_fingerprint ( void  )

Read/create keys as needed, and echo our fingerprint to stdout.

Definition at line 884 of file main.c.


◆ dumpmemusage()

static void dumpmemusage ( int  severity)

◆ dumpstats()

static void dumpstats ( int  severity)

Write all statistics to the log, with log level severity. Called in response to a SIGUSR1.

Definition at line 342 of file main.c.

References conn_state_to_string(), conn_type_to_string(), connection_is_listener(), LD_GENERAL, safe_str_client(), SMARTLIST_FOREACH_BEGIN, and tor_log().

◆ handle_signals()

void handle_signals ( void  )

Set up the signal handler events for this process, and register them with libevent if appropriate.

Definition at line 498 of file main.c.

◆ have_lockfile()

int have_lockfile ( void  )

Return true iff we've successfully acquired the lock file.

Definition at line 730 of file main.c.

References lockfile.

◆ process_signal()

static void process_signal ( int  sig)

Do the work of acting on a signal received in sig

Definition at line 242 of file main.c.

Referenced by signal_callback().

◆ release_lockfile()

void release_lockfile ( void  )

If we have successfully acquired the lock file, release it.

Definition at line 737 of file main.c.

References lockfile, and tor_lockfile_unlock().

◆ signal_callback()

static void signal_callback ( evutil_socket_t  fd,
short  events,
void *  arg 

Libevent callback: invoked when we get a signal.

Definition at line 229 of file main.c.

References process_signal(), and update_current_time().

◆ tor_cleanup()

void tor_cleanup ( void  )

◆ tor_free_all()

void tor_free_all ( int  postfork)

Free all memory that we might have allocated somewhere. If postfork, we are a worker process and we want to free only the parts of memory that we won't touch. If !postfork, Tor is shutting down and we should free everything.

Helps us find the real leaks with sanitizers and the like. Also valgrind should then report 0 reachable in its leak report (in an ideal world – in practice libevent, SSL, libc etc never quite free everything).

Definition at line 754 of file main.c.

◆ tor_init()

int tor_init ( int  argc,
char *  argv[] 

Main entry point for the Tor command-line client. Return 0 on "success", negative on "failure", and positive on "success and exit".

Definition at line 545 of file main.c.

References addressmap_init(), log_set_application_name(), rend_cache_init(), rep_hist_init(), time_of_process_start, tor_init_connection_lists(), and tor_snprintf().

◆ tor_remove_file()

void tor_remove_file ( const char *  filename)

Remove the specified file, and log a warning if the operation fails for any reason other than the file not existing. Ignores NULL filenames.

Definition at line 830 of file main.c.

Referenced by tor_cleanup().

◆ tor_run_main()

int tor_run_main ( const tor_main_configuration_t )

Run the tor process, as if from the command line.

The command line arguments from tor_main_configuration_set_command_line() are taken as if they had been passed to main().

This function will not return until Tor is done running. It returns zero on success, and nonzero on failure.

If you want to control when Tor exits, make sure to configure a control socket. The OwningControllerFD option may be helpful there.

BUG 23847: Sometimes, if you call tor_main a second time (after it has returned), Tor may crash or behave strangely. We have fixed all issues of this type that we could find, but more may remain.

LIMITATION: You cannot run more than one instance of Tor in the same process at the same time. Concurrent calls will cause undefined behavior. We do not currently have plans to change this.

LIMITATION: While we will try to fix any problems found here, you should be aware that Tor was originally written to run as its own process, and that the functionality of this file was added later. If you find any bugs or strange behavior, please report them, and we'll try to straighten them out.

Definition at line 1382 of file main.c.

References tor_main_configuration_t::argc, tor_main_configuration_t::argc_owned, init_protocol_warning_severity_level(), tor_free_(), tor_malloc_(), and tor_realloc_().

◆ try_locking()

int try_locking ( const or_options_t options,
int  err_if_locked 

Try to grab the lock file described in options, if we do not already have it. If err_if_locked is true, warn if somebody else is holding the lock, and exit if we can't get it after waiting. Otherwise, return -1 if we can't get the lockfile. Return 0 on success.

Definition at line 694 of file main.c.

References lockfile.

Variable Documentation

◆ lockfile

tor_lockfile_t* lockfile = NULL

A lockfile structure, used to prevent two Tors from messing with the data directory at once. If this variable is non-NULL, we're holding the lockfile.

Definition at line 686 of file main.c.

Referenced by have_lockfile(), release_lockfile(), tor_lockfile_unlock(), and try_locking().

◆ quiet_level

int quiet_level = 0

Decides our behavior when no logs are configured/before any logs have been configured. For 0, we log notice to stdout as normal. For 1, we log warnings only. For 2, we log nothing.

Definition at line 134 of file main.c.

◆ signal_event

struct event* signal_event

Pointer to hold the event object constructed for this signal.

Definition at line 460 of file main.c.

◆ signal_value

int signal_value

A numeric code for this signal. Must match the signal value if try_to_register is true.

Definition at line 455 of file main.c.

◆ try_to_register

int try_to_register

True if we should try to register this signal with libevent and catch corresponding posix signals. False otherwise.

Definition at line 458 of file main.c.