Macros | Functions | Variables
main.c File Reference

Invocation module. Initializes subsystems and runs the main loop. More...

#include "core/or/or.h"
#include "app/config/config.h"
#include "app/config/statefile.h"
#include "app/config/quiet_level.h"
#include "app/main/main.h"
#include "app/main/ntmain.h"
#include "app/main/risky_options.h"
#include "app/main/shutdown.h"
#include "app/main/subsysmgr.h"
#include "core/mainloop/connection.h"
#include "core/mainloop/cpuworker.h"
#include "core/mainloop/mainloop.h"
#include "core/mainloop/mainloop_pubsub.h"
#include "core/mainloop/netstatus.h"
#include "core/or/channel.h"
#include "core/or/channelpadding.h"
#include "core/or/circuitpadding.h"
#include "core/or/congestion_control_common.h"
#include "core/or/congestion_control_flow.h"
#include "core/or/circuitlist.h"
#include "core/or/command.h"
#include "core/or/connection_or.h"
#include "core/or/relay.h"
#include "core/or/status.h"
#include "feature/api/tor_api.h"
#include "feature/api/tor_api_internal.h"
#include "feature/client/addressmap.h"
#include "feature/control/control.h"
#include "feature/control/control_auth.h"
#include "feature/control/control_events.h"
#include "feature/dirauth/keypin.h"
#include "feature/dirauth/process_descs.h"
#include "feature/dircache/consdiffmgr.h"
#include "feature/dirparse/routerparse.h"
#include "feature/hibernate/hibernate.h"
#include "feature/hs/hs_dos.h"
#include "feature/hs/hs_service.h"
#include "feature/nodelist/authcert.h"
#include "feature/nodelist/networkstatus.h"
#include "feature/nodelist/routerlist.h"
#include "feature/relay/dns.h"
#include "feature/relay/ext_orport.h"
#include "feature/relay/routerkeys.h"
#include "feature/relay/routermode.h"
#include "feature/stats/predict_ports.h"
#include "feature/stats/bwhist.h"
#include "feature/stats/rephist.h"
#include "lib/compress/compress.h"
#include "lib/buf/buffers.h"
#include "lib/crypt_ops/crypto_format.h"
#include "lib/crypt_ops/crypto_rand.h"
#include "lib/crypt_ops/crypto_s2k.h"
#include "lib/net/resolve.h"
#include "lib/trace/trace.h"
#include "lib/process/waitpid.h"
#include "lib/pubsub/pubsub_build.h"
#include "lib/meminfo/meminfo.h"
#include "lib/osinfo/uname.h"
#include "lib/osinfo/libc.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/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 OPENDIR(dir)    sandbox_cfg_allow_opendir_dirname(&cfg, tor_strdup(dir))
#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()    OPEN(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))


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_remove_file (const char *filename)
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)
void pubsub_install (void)
void pubsub_connect (void)
int tor_run_main (const tor_main_configuration_t *tor_cfg)


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)
const char * name
Definition: config.c:2462


#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))


#define UNIX_ONLY   1

Definition at line 430 of file main.c.

Function Documentation

◆ activate_signal()

void activate_signal ( int  signal_num)

Definition at line 518 of file main.c.

◆ do_dump_config()

static int do_dump_config ( void  )

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

Definition at line 799 of file main.c.

◆ 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 781 of file main.c.

◆ 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 114 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 727 of file main.c.

◆ dumpmemusage()

static void dumpmemusage ( int  severity)

Write current memory usage information to the log.

Definition at line 314 of file main.c.

◆ 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 327 of file main.c.

◆ 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 479 of file main.c.

◆ have_lockfile()

int have_lockfile ( void  )

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

Definition at line 697 of file main.c.

◆ init_addrinfo()

static void init_addrinfo ( void  )

Definition at line 829 of file main.c.

◆ process_signal()

static void process_signal ( int  sig)

Do the work of acting on a signal received in sig

Definition at line 215 of file main.c.

Referenced by signal_callback().

◆ pubsub_connect()

void pubsub_connect ( void  )

Connect the mainloop to its publish/subscribe message delivery events if appropriate, and configure the global channels appropriately.

Definition at line 1295 of file main.c.

◆ pubsub_install()

void pubsub_install ( void  )

Install the publish/subscribe relationships for all the subsystems.

Definition at line 1283 of file main.c.

◆ release_lockfile()

void release_lockfile ( void  )

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

Definition at line 704 of file main.c.

◆ run_tor_main_loop()

int run_tor_main_loop ( void  )

Definition at line 1174 of file main.c.

◆ sandbox_init_filter()

static sandbox_cfg_t * sandbox_init_filter ( void  )

Definition at line 845 of file main.c.

◆ 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 202 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 533 of file main.c.

◆ 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 717 of file main.c.

Referenced by tor_cleanup().

◆ tor_run_main()

int tor_run_main ( const tor_main_configuration_t tor_cfg)

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 1310 of file main.c.

◆ 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 661 of file main.c.

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 653 of file main.c.

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

◆ signal_event

struct event* signal_event

Pointer to hold the event object constructed for this signal.

Definition at line 441 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 436 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 439 of file main.c.