Tor  0.4.7.0-alpha-dev
Macros | Functions | Variables
mainloop.c File Reference

Toplevel module. Handles signals, multiplexes between connections, implements main loop, and drives scheduled events. More...

#include "core/or/or.h"
#include "app/config/config.h"
#include "app/config/statefile.h"
#include "app/main/ntmain.h"
#include "core/mainloop/connection.h"
#include "core/mainloop/cpuworker.h"
#include "core/mainloop/mainloop.h"
#include "core/mainloop/netstatus.h"
#include "core/mainloop/periodic.h"
#include "core/or/channel.h"
#include "core/or/channelpadding.h"
#include "core/or/channeltls.h"
#include "core/or/circuitbuild.h"
#include "core/or/circuitlist.h"
#include "core/or/circuituse.h"
#include "core/or/connection_edge.h"
#include "core/or/connection_or.h"
#include "core/or/dos.h"
#include "core/or/status.h"
#include "feature/client/addressmap.h"
#include "feature/client/bridges.h"
#include "feature/client/dnsserv.h"
#include "feature/client/entrynodes.h"
#include "feature/client/proxymode.h"
#include "feature/client/transports.h"
#include "feature/control/control.h"
#include "feature/control/control_events.h"
#include "feature/dirauth/authmode.h"
#include "feature/dircache/consdiffmgr.h"
#include "feature/dirclient/dirclient_modes.h"
#include "feature/dircommon/directory.h"
#include "feature/hibernate/hibernate.h"
#include "feature/hs/hs_cache.h"
#include "feature/hs/hs_client.h"
#include "feature/hs/hs_service.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/routerkeys.h"
#include "feature/relay/routermode.h"
#include "feature/relay/selftest.h"
#include "feature/stats/geoip_stats.h"
#include "feature/stats/predict_ports.h"
#include "feature/stats/connstats.h"
#include "feature/stats/rephist.h"
#include "lib/buf/buffers.h"
#include "lib/crypt_ops/crypto_rand.h"
#include "lib/err/backtrace.h"
#include "lib/tls/buffers_tls.h"
#include "lib/net/buffers_net.h"
#include "lib/evloop/compat_libevent.h"
#include <event2/event.h>
#include "core/or/cell_st.h"
#include "core/or/entry_connection_st.h"
#include "feature/nodelist/networkstatus_st.h"
#include "core/or/or_connection_st.h"
#include "app/config/or_state_st.h"
#include "feature/nodelist/routerinfo_st.h"
#include "core/or/socks_request_st.h"

Go to the source code of this file.

Macros

#define MAINLOOP_PRIVATE
 
#define MAX_SIGNEWNYM_RATE   10
 
#define GREEDY_DESCRIPTOR_RETRY_INTERVAL   (10)
 
#define LAZY_DESCRIPTOR_RETRY_INTERVAL   (60)
 
#define CALLBACK(name)    static int name ## _callback(time_t, const or_options_t *)
 
#define CALLBACK(name, r, f)    PERIODIC_EVENT(name, PERIODIC_EVENT_ROLE_ ## r, f)
 
#define FL(name)   (PERIODIC_EVENT_FLAG_ ## name)
 
#define NAMED_CALLBACK(name)    STMT_BEGIN name ## _event = periodic_events_find( #name ); STMT_END
 
#define ENTROPY_INTERVAL   (60*60)
 
#define CHECK_PARTICIPATION_INTERVAL   (5*60)
 
#define CHECK_EXPIRED_NS_INTERVAL   (2*60)
 
#define CHECK_WRITE_STATS_INTERVAL   (60*60)
 
#define CLEAN_CACHES_INTERVAL   (30*60)
 
#define ROUTERLIST_PRUNING_INTERVAL   (60*60)
 
#define CDM_CLEAN_CALLBACK_INTERVAL   600
 
#define NUM_JUMPED_SECONDS_BEFORE_NETSTATUS_UPDATE   20
 
#define NUM_JUMPED_SECONDS_BEFORE_WARN   100
 
#define NUM_IDLE_SECONDS_BEFORE_WARN   3600
 
#define UPTIME_CUTOFF_FOR_NEW_BANDWIDTH_TEST   (6*60*60)
 

Functions

static int conn_close_if_marked (int i)
 
static void connection_start_reading_from_linked_conn (connection_t *conn)
 
static int connection_should_read_from_linked_conn (connection_t *conn)
 
static void conn_read_callback (evutil_socket_t fd, short event, void *_conn)
 
static void conn_write_callback (evutil_socket_t fd, short event, void *_conn)
 
static void shutdown_did_not_work_callback (evutil_socket_t fd, short event, void *arg) ATTR_NORETURN
 
int have_completed_a_circuit (void)
 
void note_that_we_completed_a_circuit (void)
 
void note_that_we_maybe_cant_complete_circuits (void)
 
int connection_add_impl (connection_t *conn, int is_connecting)
 
void connection_unregister_events (connection_t *conn)
 
int connection_remove (connection_t *conn)
 
static void connection_unlink (connection_t *conn)
 
static void schedule_active_linked_connections_cb (mainloop_event_t *event, void *arg)
 
void tor_init_connection_lists (void)
 
void add_connection_to_closeable_list (connection_t *conn)
 
int connection_is_on_closeable_list (connection_t *conn)
 
int connection_in_array (connection_t *conn)
 
smartlist_tget_connection_array (void)
 
uint64_t get_bytes_read (void)
 
uint64_t get_bytes_written (void)
 
void stats_increment_bytes_read_and_written (uint64_t r, uint64_t w)
 
void connection_watch_events (connection_t *conn, watchable_events_t events)
 
int connection_is_reading (connection_t *conn)
 
void reset_main_loop_counters (void)
 
static void increment_main_loop_success_count (void)
 
uint64_t get_main_loop_success_count (void)
 
static void increment_main_loop_error_count (void)
 
uint64_t get_main_loop_error_count (void)
 
static void increment_main_loop_idle_count (void)
 
uint64_t get_main_loop_idle_count (void)
 
static int connection_check_event (connection_t *conn, struct event *ev)
 
void connection_stop_reading (connection_t *conn)
 
void connection_start_reading (connection_t *conn)
 
int connection_is_writing (connection_t *conn)
 
void connection_stop_writing (connection_t *conn)
 
void connection_start_writing (connection_t *conn)
 
void tor_shutdown_event_loop_and_exit (int exitcode)
 
int tor_event_loop_shutdown_is_pending (void)
 
void connection_stop_reading_from_linked_conn (connection_t *conn)
 
STATIC void close_closeable_connections (void)
 
int connection_count_moribund (void)
 
static void directory_all_unreachable_cb (mainloop_event_t *event, void *arg)
 
void directory_all_unreachable (time_t now)
 
void directory_info_has_arrived (time_t now, int from_cache, int suppress_logs)
 
static void run_connection_housekeeping (int i, time_t now)
 
static void signewnym_impl (time_t now)
 
static void handle_deferred_signewnym_cb (mainloop_event_t *event, void *arg)
 
void do_signewnym (time_t now)
 
unsigned get_signewnym_epoch (void)
 
 CALLBACK (add_entropy)
 
 CALLBACK (check_expired_networkstatus)
 
 CALLBACK (clean_caches)
 
 CALLBACK (clean_consdiffmgr)
 
 CALLBACK (fetch_networkstatus)
 
 CALLBACK (heartbeat)
 
 CALLBACK (hs_service)
 
 CALLBACK (launch_descriptor_fetches)
 
 CALLBACK (prune_old_routers)
 
 CALLBACK (record_bridge_stats)
 
 CALLBACK (rend_cache_failure_clean)
 
 CALLBACK (reset_padding_counts)
 
 CALLBACK (retry_listeners)
 
 CALLBACK (rotate_x509_certificate)
 
 CALLBACK (save_state)
 
 CALLBACK (write_stats_file)
 
 CALLBACK (control_per_second_events)
 
 CALLBACK (second_elapsed)
 
void reset_all_main_loop_timers (void)
 
STATIC int get_my_roles (const or_options_t *options)
 
static void initialize_periodic_events_cb (evutil_socket_t fd, short events, void *data)
 
void initialize_periodic_events (void)
 
STATIC void teardown_periodic_events (void)
 
static void rescan_periodic_events_cb (mainloop_event_t *event, void *arg)
 
void schedule_rescan_periodic_events (void)
 
void rescan_periodic_events (const or_options_t *options)
 
void periodic_events_on_new_options (const or_options_t *options)
 
void reschedule_directory_downloads (void)
 
static void postloop_cleanup_cb (mainloop_event_t *ev, void *arg)
 
void mainloop_schedule_postloop_cleanup (void)
 
static void scheduled_shutdown_cb (mainloop_event_t *ev, void *arg)
 
void mainloop_schedule_shutdown (int delay_sec)
 
static int second_elapsed_callback (time_t now, const or_options_t *options)
 
static int launch_descriptor_fetches_callback (time_t now, const or_options_t *options)
 
static int rotate_x509_certificate_callback (time_t now, const or_options_t *options)
 
static int add_entropy_callback (time_t now, const or_options_t *options)
 
STATIC int check_network_participation_callback (time_t now, const or_options_t *options)
 
static int check_expired_networkstatus_callback (time_t now, const or_options_t *options)
 
static int save_state_callback (time_t now, const or_options_t *options)
 
void reschedule_or_state_save (void)
 
static int write_stats_file_callback (time_t now, const or_options_t *options)
 
static int reset_padding_counts_callback (time_t now, const or_options_t *options)
 
static int record_bridge_stats_callback (time_t now, const or_options_t *options)
 
static int clean_caches_callback (time_t now, const or_options_t *options)
 
static int rend_cache_failure_clean_callback (time_t now, const or_options_t *options)
 
static int prune_old_routers_callback (time_t now, const or_options_t *options)
 
static int fetch_networkstatus_callback (time_t now, const or_options_t *options)
 
static int retry_listeners_callback (time_t now, const or_options_t *options)
 
static int heartbeat_callback (time_t now, const or_options_t *options)
 
static int clean_consdiffmgr_callback (time_t now, const or_options_t *options)
 
static int hs_service_callback (time_t now, const or_options_t *options)
 
static int control_per_second_events_callback (time_t now, const or_options_t *options)
 
void update_current_time (time_t now)
 
void ip_address_changed (int on_client_conn)
 
void dns_servers_relaunch_checks (void)
 
void initialize_mainloop_events (void)
 
int do_main_loop (void)
 
static int run_main_loop_once (void)
 
STATIC int run_main_loop_until_done (void)
 
long get_uptime (void)
 
void reset_uptime (void)
 
void tor_mainloop_free_all (void)
 

Variables

token_bucket_rw_t global_bucket
 
token_bucket_rw_t global_relayed_bucket
 
static uint64_t stats_n_bytes_read = 0
 
static uint64_t stats_n_bytes_written = 0
 
time_t time_of_process_start = 0
 
static long stats_n_seconds_working = 0
 
static uint64_t stats_n_main_loop_successes = 0
 
static uint64_t stats_n_main_loop_errors = 0
 
static uint64_t stats_n_main_loop_idle = 0
 
static time_t time_of_last_signewnym = 0
 
static int signewnym_is_pending = 0
 
static mainloop_event_thandle_deferred_signewnym_ev = NULL
 
static unsigned newnym_epoch = 0
 
STATIC smartlist_tconnection_array = NULL
 
static smartlist_tcloseable_connection_lst = NULL
 
static smartlist_tactive_linked_connection_lst = NULL
 
static int called_loop_once = 0
 
static int main_loop_should_exit = 0
 
static int main_loop_exit_value = 0
 
static int can_complete_circuits = 0
 
static mainloop_event_tschedule_active_linked_connections_event = NULL
 
static struct event * shutdown_did_not_work_event = NULL
 
static mainloop_event_tdirectory_all_unreachable_cb_event = NULL
 
static int periodic_events_initialized = 0
 
STATIC periodic_event_item_t mainloop_periodic_events []
 
static periodic_event_item_tfetch_networkstatus_event =NULL
 
static periodic_event_item_tlaunch_descriptor_fetches_event =NULL
 
static periodic_event_item_tcheck_dns_honesty_event =NULL
 
static periodic_event_item_tsave_state_event =NULL
 
static periodic_event_item_tprune_old_routers_event =NULL
 
static struct event * initialize_periodic_events_event = NULL
 
static mainloop_event_trescan_periodic_events_ev = NULL
 
static mainloop_event_tpostloop_cleanup_ev =NULL
 
static mainloop_event_tscheduled_shutdown_ev =NULL
 
static int should_init_bridge_stats = 1
 
static int heartbeat_callback_first_time = 1
 
static time_t current_second = 0
 
static monotime_coarse_t current_second_last_changed
 
static ratelim_t libevent_error_ratelim = RATELIM_INIT(10)
 

Detailed Description

Toplevel module. Handles signals, multiplexes between connections, implements main loop, and drives scheduled events.

For the main loop itself; see run_main_loop_once(). It invokes the rest of Tor mostly through Libevent callbacks. Libevent callbacks can happen when a timer elapses, a signal is received, a socket is ready to read or write, or an event is manually activated.

Most events in Tor are driven from these callbacks:

Other events are used for specific purposes, or for building more complex control structures. If you search for usage of tor_libevent_new(), you will find all the events that we construct in Tor.

Tor has numerous housekeeping operations that need to happen regularly. They are handled in different ways:

Definition in file mainloop.c.

Macro Definition Documentation

◆ GREEDY_DESCRIPTOR_RETRY_INTERVAL

#define GREEDY_DESCRIPTOR_RETRY_INTERVAL   (10)

How often do we check for router descriptors that we should download when we have too little directory info?

Definition at line 194 of file mainloop.c.

◆ LAZY_DESCRIPTOR_RETRY_INTERVAL

#define LAZY_DESCRIPTOR_RETRY_INTERVAL   (60)

How often do we check for router descriptors that we should download when we have enough directory info?

Definition at line 197 of file mainloop.c.

◆ MAX_SIGNEWNYM_RATE

#define MAX_SIGNEWNYM_RATE   10

How often will we honor SIGNEWNYM requests?

Definition at line 153 of file mainloop.c.

Function Documentation

◆ add_connection_to_closeable_list()

void add_connection_to_closeable_list ( connection_t conn)

Schedule conn to be closed.

Definition at line 424 of file mainloop.c.

◆ add_entropy_callback()

static int add_entropy_callback ( time_t  now,
const or_options_t options 
)
static

Periodic callback: once an hour, grab some more entropy from the kernel and feed it to our CSPRNG.

How often do we add more entropy to OpenSSL's RNG pool?

Definition at line 1798 of file mainloop.c.

◆ check_expired_networkstatus_callback()

static int check_expired_networkstatus_callback ( time_t  now,
const or_options_t options 
)
static

Periodic callback: If our consensus is too old, recalculate whether we can actually use it.

Definition at line 1873 of file mainloop.c.

◆ check_network_participation_callback()

STATIC int check_network_participation_callback ( time_t  now,
const or_options_t options 
)

Periodic callback: if there has been no network usage in a while, enter a dormant state.

How often do we check whether we have had network activity?

Definition at line 1816 of file mainloop.c.

◆ clean_caches_callback()

static int clean_caches_callback ( time_t  now,
const or_options_t options 
)
static

Periodic callback: Clean in-memory caches every once in a while

Definition at line 2015 of file mainloop.c.

◆ close_closeable_connections()

STATIC void close_closeable_connections ( void  )

Close all connections that have been scheduled to get closed.

Definition at line 837 of file mainloop.c.

Referenced by postloop_cleanup_cb().

◆ conn_close_if_marked()

static int conn_close_if_marked ( int  i)
static

If the connection at connection_array[i] is marked for close, then:

  • If it has data that it wants to flush, try to flush it.
  • If it still has data to flush, and conn->hold_open_until_flushed is true, then leave the connection open and return.
  • Otherwise, remove the connection from connection_array and from all other lists, close it, and free it. Returns 1 if the connection was closed, 0 otherwise.

Definition at line 957 of file mainloop.c.

◆ conn_read_callback()

static void conn_read_callback ( evutil_socket_t  fd,
short  event,
void *  _conn 
)
static

Libevent callback: this gets invoked when (connection_t*)conn has some data to read.

Definition at line 871 of file mainloop.c.

◆ conn_write_callback()

static void conn_write_callback ( evutil_socket_t  fd,
short  events,
void *  _conn 
)
static

Libevent callback: this gets invoked when (connection_t*)conn has some data to write.

Definition at line 913 of file mainloop.c.

◆ connection_add_impl()

int connection_add_impl ( connection_t conn,
int  is_connecting 
)

Add conn to the array of connections that we can poll on. The connection's socket must be set; the connection starts out non-reading and non-writing.

Definition at line 244 of file mainloop.c.

◆ connection_check_event()

static int connection_check_event ( connection_t conn,
struct event *  ev 
)
static

Check whether conn is correct in having (or not having) a read/write event (passed in ev). On success, return 0. On failure, log a warning and return -1.

Definition at line 571 of file mainloop.c.

Referenced by connection_start_reading(), connection_start_writing(), connection_stop_reading(), and connection_stop_writing().

◆ connection_count_moribund()

int connection_count_moribund ( void  )

Count moribund connections for the OOS handler

Definition at line 853 of file mainloop.c.

◆ connection_in_array()

int connection_in_array ( connection_t conn)

Return true iff conn is in the current poll array.

Definition at line 442 of file mainloop.c.

Referenced by connection_free_().

◆ connection_is_on_closeable_list()

int connection_is_on_closeable_list ( connection_t conn)

Return 1 if conn is on the closeable list, else return 0.

Definition at line 435 of file mainloop.c.

Referenced by connection_free_().

◆ connection_is_reading()

int connection_is_reading ( connection_t conn)

Return true iff conn is listening for read events.

Definition at line 508 of file mainloop.c.

Referenced by set_streams_blocked_on_circ().

◆ connection_is_writing()

int connection_is_writing ( connection_t conn)

Return true iff conn is listening for write events.

Definition at line 654 of file mainloop.c.

◆ connection_remove()

int connection_remove ( connection_t conn)

Remove the connection from the global list, and remove the corresponding poll entry. Calling this function will shift the last connection (if any) into the position occupied by conn.

Definition at line 297 of file mainloop.c.

Referenced by connection_unlink().

◆ connection_should_read_from_linked_conn()

static int connection_should_read_from_linked_conn ( connection_t conn)
static

Return true iff conn is linked conn, and reading from the conn linked to it would be good and feasible. (Reading is "feasible" if the other conn exists and has data in its outbuf, and is "good" if we have our reading_from_linked_conn flag set and the other conn has its writing_to_linked_conn flag set.)

Definition at line 715 of file mainloop.c.

Referenced by connection_start_reading(), and connection_start_writing().

◆ connection_start_reading()

void connection_start_reading ( connection_t conn)

◆ connection_start_reading_from_linked_conn()

static void connection_start_reading_from_linked_conn ( connection_t conn)
static

Helper: Tell the main loop to begin reading bytes into conn from its linked connection, if it is not doing so already. Called by connection_start_reading and connection_start_writing as appropriate.

Definition at line 801 of file mainloop.c.

Referenced by connection_start_reading(), and connection_start_writing().

◆ connection_start_writing()

void connection_start_writing ( connection_t conn)

Tell the main loop to start notifying conn of any write events.

Definition at line 687 of file mainloop.c.

Referenced by connection_watch_events(), connection_write_to_buf_commit(), and reenable_blocked_connections_cb().

◆ connection_stop_reading()

void connection_stop_reading ( connection_t conn)

Tell the main loop to stop notifying conn of any read events.

Definition at line 609 of file mainloop.c.

Referenced by connection_read_bw_exhausted(), connection_watch_events(), and set_streams_blocked_on_circ().

◆ connection_stop_reading_from_linked_conn()

void connection_stop_reading_from_linked_conn ( connection_t conn)

Tell the main loop to stop reading bytes into conn from its linked connection, if is currently doing so. Called by connection_stop_reading, connection_stop_writing, and connection_read.

Definition at line 819 of file mainloop.c.

Referenced by connection_stop_reading(), and connection_stop_writing().

◆ connection_stop_writing()

void connection_stop_writing ( connection_t conn)

Tell the main loop to stop notifying conn of any write events.

Definition at line 664 of file mainloop.c.

Referenced by connection_finished_flushing(), connection_watch_events(), and connection_write_bw_exhausted().

◆ connection_unlink()

static void connection_unlink ( connection_t conn)
static

If conn is an edge conn, remove it from the list of conn's on this circuit. If it's not on an edge, flush and send destroys for all circuits on this conn.

Remove it from connection_array (if applicable) and from closeable_connection_list.

Then free it.

Definition at line 340 of file mainloop.c.

◆ connection_unregister_events()

void connection_unregister_events ( connection_t conn)

Tell libevent that we don't care about conn any more.

Definition at line 275 of file mainloop.c.

◆ connection_watch_events()

void connection_watch_events ( connection_t conn,
watchable_events_t  events 
)

Set the event mask on conn to events. (The event mask is a bitmask whose bits are READ_EVENT and WRITE_EVENT)

Definition at line 493 of file mainloop.c.

◆ directory_all_unreachable()

void directory_all_unreachable ( time_t  now)

We've just tried every dirserver we know about, and none of them were reachable. Assume the network is down. Change state so next time an application connection arrives we'll delay it and try another directory fetch. Kill off all the circuit_wait streams that are waiting now, since they will all timeout anyway.

Definition at line 1097 of file mainloop.c.

◆ directory_all_unreachable_cb()

static void directory_all_unreachable_cb ( mainloop_event_t event,
void *  arg 
)
static

Implementation for directory_all_unreachable. This is done in a callback, since otherwise it would complicate Tor's control-flow graph beyond all reason.

Definition at line 1067 of file mainloop.c.

◆ directory_info_has_arrived()

void directory_info_has_arrived ( time_t  now,
int  from_cache,
int  suppress_logs 
)

This function is called whenever we successfully pull down some new network statuses or server descriptors.

Definition at line 1115 of file mainloop.c.

◆ dns_servers_relaunch_checks()

void dns_servers_relaunch_checks ( void  )

Forget what we've learned about the correctness of our DNS servers, and start learning again.

Definition at line 2320 of file mainloop.c.

◆ do_main_loop()

int do_main_loop ( void  )

Tor main loop.

Definition at line 2346 of file mainloop.c.

◆ do_signewnym()

void do_signewnym ( time_t  now)

Either perform a signewnym or schedule one, depending on rate limiting.

Definition at line 1316 of file mainloop.c.

◆ fetch_networkstatus_callback()

static int fetch_networkstatus_callback ( time_t  now,
const or_options_t options 
)
static

Periodic event: once a minute, (or every second if TestingTorNetwork, or during client bootstrap), check whether we want to download any networkstatus documents.

Definition at line 2067 of file mainloop.c.

◆ get_bytes_read()

uint64_t get_bytes_read ( void  )

Return the amount of network traffic read, in bytes, over the life of this process.

Definition at line 463 of file mainloop.c.

Referenced by control_get_bytes_rw_last_sec(), and log_heartbeat().

◆ get_bytes_written()

uint64_t get_bytes_written ( void  )

Return the amount of network traffic read, in bytes, over the life of this process.

Definition at line 473 of file mainloop.c.

Referenced by control_get_bytes_rw_last_sec(), and log_heartbeat().

◆ get_connection_array()

smartlist_t* get_connection_array ( void  )

◆ get_main_loop_error_count()

uint64_t get_main_loop_error_count ( void  )

Get the main loop error counter.

Definition at line 548 of file mainloop.c.

◆ get_main_loop_idle_count()

uint64_t get_main_loop_idle_count ( void  )

Get the main loop idle counter.

Definition at line 562 of file mainloop.c.

◆ get_main_loop_success_count()

uint64_t get_main_loop_success_count ( void  )

Get the main loop success counter.

Definition at line 534 of file mainloop.c.

◆ get_my_roles()

STATIC int get_my_roles ( const or_options_t options)

Return a bitmask of the roles this tor instance is configured for using the given options.

Definition at line 1460 of file mainloop.c.

Referenced by rescan_periodic_events().

◆ get_signewnym_epoch()

unsigned get_signewnym_epoch ( void  )

Return the number of times that signewnym has been called.

Definition at line 1340 of file mainloop.c.

◆ get_uptime()

long get_uptime ( void  )

◆ handle_deferred_signewnym_cb()

static void handle_deferred_signewnym_cb ( mainloop_event_t event,
void *  arg 
)
static

Callback: run a deferred signewnym.

Definition at line 1306 of file mainloop.c.

Referenced by do_signewnym().

◆ have_completed_a_circuit()

int have_completed_a_circuit ( void  )

Return 1 if we have successfully built a circuit, and nothing has changed to make us think that maybe we can't.

Definition at line 218 of file mainloop.c.

Referenced by check_for_reachability_bw_callback(), directory_info_has_arrived(), and reachability_warnings_callback().

◆ heartbeat_callback()

static int heartbeat_callback ( time_t  now,
const or_options_t options 
)
static

Periodic callback: write the heartbeat message in the logs.

If writing the heartbeat message to the logs fails for some reason, retry again after MIN_HEARTBEAT_PERIOD seconds.

Definition at line 2113 of file mainloop.c.

◆ increment_main_loop_error_count()

static void increment_main_loop_error_count ( void  )
static

Increment the main loop error counter.

Definition at line 541 of file mainloop.c.

◆ increment_main_loop_idle_count()

static void increment_main_loop_idle_count ( void  )
static

Increment the main loop idle counter.

Definition at line 555 of file mainloop.c.

◆ increment_main_loop_success_count()

static void increment_main_loop_success_count ( void  )
static

Increment the main loop success counter.

Definition at line 527 of file mainloop.c.

◆ initialize_mainloop_events()

void initialize_mainloop_events ( void  )

Initialize some mainloop_event_t objects that we require.

Definition at line 2332 of file mainloop.c.

Referenced by do_main_loop().

◆ initialize_periodic_events()

void initialize_periodic_events ( void  )

Set up all the members of mainloop_periodic_events[], and configure them all to be launched from a callback.

Definition at line 1519 of file mainloop.c.

◆ initialize_periodic_events_cb()

static void initialize_periodic_events_cb ( evutil_socket_t  fd,
short  events,
void *  data 
)
static

Helper, run one second after setup: Initializes all members of periodic_events and starts them running.

(We do this one second after setup for backward-compatibility reasons; it might not actually be necessary.)

Definition at line 1505 of file mainloop.c.

◆ ip_address_changed()

void ip_address_changed ( int  on_client_conn)

Called when our IP address seems to have changed. on_client_conn should be true if:

  • we detected a change in our interface address, using an outbound connection, and therefore
  • our client TLS keys need to be rotated. Otherwise, it should be false, and:
  • we detected a change in our published address (using some other method), and therefore
  • the published addresses in our descriptor need to change.

Definition at line 2289 of file mainloop.c.

◆ launch_descriptor_fetches_callback()

static int launch_descriptor_fetches_callback ( time_t  now,
const or_options_t options 
)
static

Periodic callback: Every {LAZY,GREEDY}_DESCRIPTOR_RETRY_INTERVAL, see about fetching descriptors, microdescriptors, and extrainfo documents.

Definition at line 1746 of file mainloop.c.

◆ mainloop_schedule_postloop_cleanup()

void mainloop_schedule_postloop_cleanup ( void  )

Schedule a post-loop event to clean up marked channels, connections, and circuits.

Definition at line 1631 of file mainloop.c.

Referenced by add_connection_to_closeable_list().

◆ mainloop_schedule_shutdown()

void mainloop_schedule_shutdown ( int  delay_sec)

Schedule the mainloop to exit after delay_sec seconds.

Definition at line 1656 of file mainloop.c.

◆ note_that_we_completed_a_circuit()

void note_that_we_completed_a_circuit ( void  )

Note that we have successfully built a circuit, so that reachability testing and introduction points and so on may be attempted.

Definition at line 226 of file mainloop.c.

◆ note_that_we_maybe_cant_complete_circuits()

void note_that_we_maybe_cant_complete_circuits ( void  )

Note that something has happened (like a clock jump, or DisableNetwork) to make us think that maybe we can't complete circuits.

Definition at line 234 of file mainloop.c.

Referenced by circuit_note_clock_jumped().

◆ postloop_cleanup_cb()

static void postloop_cleanup_cb ( mainloop_event_t ev,
void *  arg 
)
static

Mainloop callback: clean up circuits, channels, and connections that are pending close.

Definition at line 1615 of file mainloop.c.

Referenced by initialize_mainloop_events().

◆ prune_old_routers_callback()

static int prune_old_routers_callback ( time_t  now,
const or_options_t options 
)
static

Periodic callback: prune routerlist of old information about Tor network.

Definition at line 2045 of file mainloop.c.

◆ record_bridge_stats_callback()

static int record_bridge_stats_callback ( time_t  now,
const or_options_t options 
)
static

Periodic callback: Write bridge statistics to disk if appropriate.

Definition at line 1987 of file mainloop.c.

◆ rend_cache_failure_clean_callback()

static int rend_cache_failure_clean_callback ( time_t  now,
const or_options_t options 
)
static

Periodic callback: Clean the cache of failed hidden service lookups frequently.

Definition at line 2031 of file mainloop.c.

◆ rescan_periodic_events()

void rescan_periodic_events ( const or_options_t options)

Do a pass at all our periodic events, disable those we don't need anymore and enable those we need now using the given options.

Definition at line 1583 of file mainloop.c.

Referenced by hs_service_map_has_changed(), on_hibernate_state_change(), and rescan_periodic_events_cb().

◆ rescan_periodic_events_cb()

static void rescan_periodic_events_cb ( mainloop_event_t event,
void *  arg 
)
static

Callback: rescan the periodic event list.

Definition at line 1560 of file mainloop.c.

◆ reschedule_directory_downloads()

void reschedule_directory_downloads ( void  )

Update our schedule so that we'll check whether we need to fetch directory info immediately.

Definition at line 1603 of file mainloop.c.

◆ reschedule_or_state_save()

void reschedule_or_state_save ( void  )

Reschedule the event for saving the state file.

Run this when the state becomes dirty.

Definition at line 1907 of file mainloop.c.

Referenced by or_state_mark_dirty().

◆ reset_all_main_loop_timers()

void reset_all_main_loop_timers ( void  )

Reset all the periodic events so we'll do all our actions again as if we just started up. Useful if our clock just moved back a long time from the future, so we don't wait until that future arrives again before acting.

Definition at line 1452 of file mainloop.c.

Referenced by circuit_note_clock_jumped().

◆ reset_main_loop_counters()

void reset_main_loop_counters ( void  )

Reset our main loop counters.

Definition at line 518 of file mainloop.c.

◆ reset_uptime()

void reset_uptime ( void  )

Reset Tor's uptime.

Definition at line 2535 of file mainloop.c.

Referenced by directory_all_unreachable().

◆ retry_listeners_callback()

static int retry_listeners_callback ( time_t  now,
const or_options_t options 
)
static

Periodic callback: Every 60 seconds, we relaunch listeners if any died.

Definition at line 2093 of file mainloop.c.

◆ rotate_x509_certificate_callback()

static int rotate_x509_certificate_callback ( time_t  now,
const or_options_t options 
)
static

Periodic event: Rotate our X.509 certificates and TLS keys once every MAX_SSL_KEY_LIFETIME_INTERNAL.

Definition at line 1764 of file mainloop.c.

◆ run_connection_housekeeping()

static void run_connection_housekeeping ( int  i,
time_t  now 
)
static

Perform regular maintenance tasks for a single connection. This function gets run once per second per connection by run_scheduled_events.

Definition at line 1156 of file mainloop.c.

◆ run_main_loop_once()

static int run_main_loop_once ( void  )
static

Run the main loop a single time. Return 0 for "exit"; -1 for "exit with error", and 1 for "run this again."

Definition at line 2421 of file mainloop.c.

Referenced by run_main_loop_until_done().

◆ run_main_loop_until_done()

STATIC int run_main_loop_until_done ( void  )

Run the run_main_loop_once() function until it declares itself done, and return its final return value.

Shadow won't invoke this function, so don't fill it up with things.

Definition at line 2509 of file mainloop.c.

◆ save_state_callback()

static int save_state_callback ( time_t  now,
const or_options_t options 
)
static

Scheduled callback: Save the state file to disk if appropriate.

Definition at line 1892 of file mainloop.c.

◆ schedule_active_linked_connections_cb()

static void schedule_active_linked_connections_cb ( mainloop_event_t event,
void *  arg 
)
static

Callback: used to activate read events for all linked connections, so libevent knows to call their read callbacks. This callback run as a postloop event, so that the events it activates don't happen until Libevent has a chance to check for other events.

Definition at line 387 of file mainloop.c.

Referenced by initialize_mainloop_events().

◆ schedule_rescan_periodic_events()

void schedule_rescan_periodic_events ( void  )

Schedule an event that will rescan which periodic events should run.

Definition at line 1571 of file mainloop.c.

◆ scheduled_shutdown_cb()

static void scheduled_shutdown_cb ( mainloop_event_t ev,
void *  arg 
)
static

Callback: run a scheduled shutdown

Definition at line 1646 of file mainloop.c.

Referenced by mainloop_schedule_shutdown().

◆ second_elapsed_callback()

static int second_elapsed_callback ( time_t  now,
const or_options_t options 
)
static

Perform regular maintenance tasks. This function gets run once per second.

Definition at line 1669 of file mainloop.c.

◆ shutdown_did_not_work_callback()

static void shutdown_did_not_work_callback ( evutil_socket_t  fd,
short  event,
void *  arg 
)
static

Failsafe measure that should never actually be necessary: If tor_shutdown_event_loop_and_exit() somehow doesn't successfully exit the event loop, then this callback will kill Tor with an assertion failure seconds later

Definition at line 735 of file mainloop.c.

◆ signewnym_impl()

static void signewnym_impl ( time_t  now)
static

Honor a NEWNYM request: make future requests unlinkable to past requests.

Definition at line 1284 of file mainloop.c.

◆ stats_increment_bytes_read_and_written()

void stats_increment_bytes_read_and_written ( uint64_t  r,
uint64_t  w 
)

Increment the amount of network traffic read and written, over the life of this process.

Definition at line 483 of file mainloop.c.

Referenced by record_num_bytes_transferred_impl().

◆ tor_event_loop_shutdown_is_pending()

int tor_event_loop_shutdown_is_pending ( void  )

Return true iff tor_shutdown_event_loop_and_exit() has been called.

Definition at line 792 of file mainloop.c.

Referenced by set_options().

◆ tor_init_connection_lists()

void tor_init_connection_lists ( void  )

Initialize the global connection list, closeable connection list, and active connection list.

Definition at line 412 of file mainloop.c.

Referenced by tor_init().

◆ tor_shutdown_event_loop_and_exit()

void tor_shutdown_event_loop_and_exit ( int  exitcode)

After finishing the current callback (if any), shut down the main loop, clean up the process, and exit with exitcode.

Definition at line 764 of file mainloop.c.

◆ update_current_time()

void update_current_time ( time_t  now)

Set the current time to "now", which should be the value returned by time(). Check for clock jumps and track the total number of seconds we have been running.

How much clock jumping means that we should adjust our idea of when to go dormant?

How much clock jumping do we tolerate?

How much idleness do we tolerate?

Definition at line 2197 of file mainloop.c.

Referenced by periodic_event_dispatch(), and signal_callback().

◆ write_stats_file_callback()

static int write_stats_file_callback ( time_t  now,
const or_options_t options 
)
static

Periodic callback: Write statistics to disk if appropriate.

Definition at line 1920 of file mainloop.c.

Variable Documentation

◆ active_linked_connection_lst

smartlist_t* active_linked_connection_lst = NULL
static

List of linked connections that are currently reading data into their inbuf from their partner's outbuf.

Definition at line 170 of file mainloop.c.

Referenced by connection_start_reading_from_linked_conn(), connection_stop_reading_from_linked_conn(), connection_unlink(), schedule_active_linked_connections_cb(), and tor_init_connection_lists().

◆ called_loop_once

int called_loop_once = 0
static

Flag: Set to true iff we entered the current libevent main loop via loop_once. If so, there's no need to trigger a loopexit in order to handle linked connections.

Definition at line 174 of file mainloop.c.

◆ can_complete_circuits

int can_complete_circuits = 0
static

We set this to 1 when we've opened a circuit, so we can print a log entry to inform the user that Tor is working. We set it to 0 when we think the fact that we once opened a circuit doesn't mean we can do so any longer (a big time jump happened, when we notice our directory is heinously out-of-date, etc.

Definition at line 190 of file mainloop.c.

Referenced by have_completed_a_circuit(), note_that_we_completed_a_circuit(), and note_that_we_maybe_cant_complete_circuits().

◆ closeable_connection_lst

smartlist_t* closeable_connection_lst = NULL
static

List of connections that have been marked for close and need to be freed and removed from connection_array.

Definition at line 167 of file mainloop.c.

Referenced by add_connection_to_closeable_list(), connection_count_moribund(), connection_is_on_closeable_list(), connection_unlink(), and tor_init_connection_lists().

◆ connection_array

STATIC smartlist_t* connection_array = NULL

Smartlist of all open connections.

Definition at line 164 of file mainloop.c.

Referenced by circuits_handle_oom(), connection_in_array(), get_connection_array(), and tor_init_connection_lists().

◆ current_second

time_t current_second = 0
static

Last time that update_current_time was called.

Definition at line 2187 of file mainloop.c.

Referenced by channel_tls_handle_var_cell(), and command_process_cell().

◆ current_second_last_changed

monotime_coarse_t current_second_last_changed
static

Last time that update_current_time updated current_second.

Definition at line 2189 of file mainloop.c.

◆ handle_deferred_signewnym_ev

mainloop_event_t* handle_deferred_signewnym_ev = NULL
static

Mainloop event for the deferred signewnym call.

Definition at line 159 of file mainloop.c.

Referenced by do_signewnym().

◆ initialize_periodic_events_event

struct event* initialize_periodic_events_event = NULL
static

Event to run initialize_periodic_events_cb

Definition at line 1497 of file mainloop.c.

Referenced by do_main_loop().

◆ libevent_error_ratelim

ratelim_t libevent_error_ratelim = RATELIM_INIT(10)
static

Rate-limiter for EINVAL-type libevent warnings.

Definition at line 2413 of file mainloop.c.

◆ main_loop_exit_value

int main_loop_exit_value = 0
static

The return value that the main loop should yield when it exits, if main_loop_should_exit is true.

Definition at line 182 of file mainloop.c.

Referenced by run_main_loop_until_done(), and tor_shutdown_event_loop_and_exit().

◆ main_loop_should_exit

int main_loop_should_exit = 0
static

Flag: if true, it's time to shut down, so the main loop should exit as soon as possible.

Definition at line 178 of file mainloop.c.

Referenced by run_main_loop_until_done(), tor_event_loop_shutdown_is_pending(), and tor_shutdown_event_loop_and_exit().

◆ newnym_epoch

unsigned newnym_epoch = 0
static

How many times have we called newnym?

Definition at line 161 of file mainloop.c.

Referenced by get_signewnym_epoch().

◆ periodic_events_initialized

int periodic_events_initialized = 0
static

True iff we have initialized all the members of periodic_events. Used to prevent double-initialization.

Definition at line 1347 of file mainloop.c.

Referenced by do_main_loop(), and initialize_periodic_events().

◆ postloop_cleanup_ev

mainloop_event_t* postloop_cleanup_ev =NULL
static

Event to run postloop_cleanup_cb

Definition at line 1626 of file mainloop.c.

Referenced by initialize_mainloop_events().

◆ schedule_active_linked_connections_event

mainloop_event_t* schedule_active_linked_connections_event = NULL
static

Event that invokes schedule_active_linked_connections_cb.

Definition at line 378 of file mainloop.c.

Referenced by connection_start_reading_from_linked_conn(), and initialize_mainloop_events().

◆ scheduled_shutdown_ev

mainloop_event_t* scheduled_shutdown_ev =NULL
static

Event to run 'scheduled_shutdown_cb'

Definition at line 1642 of file mainloop.c.

Referenced by mainloop_schedule_shutdown().

◆ shutdown_did_not_work_event

struct event* shutdown_did_not_work_event = NULL
static

Event to run 'shutdown did not work callback'.

Definition at line 727 of file mainloop.c.

Referenced by tor_shutdown_event_loop_and_exit().

◆ signewnym_is_pending

int signewnym_is_pending = 0
static

Is there a signewnym request we're currently waiting to handle?

Definition at line 157 of file mainloop.c.

Referenced by do_signewnym().

◆ stats_n_bytes_read

uint64_t stats_n_bytes_read = 0
static

How many bytes have we read since we started the process?

Definition at line 138 of file mainloop.c.

Referenced by control_get_bytes_rw_last_sec(), get_bytes_read(), and stats_increment_bytes_read_and_written().

◆ stats_n_bytes_written

uint64_t stats_n_bytes_written = 0
static

How many bytes have we written since we started the process?

Definition at line 140 of file mainloop.c.

Referenced by control_get_bytes_rw_last_sec(), get_bytes_written(), and stats_increment_bytes_read_and_written().

◆ stats_n_main_loop_errors

uint64_t stats_n_main_loop_errors = 0
static

How many times have we received an error from the main loop?

Definition at line 148 of file mainloop.c.

Referenced by get_main_loop_error_count(), increment_main_loop_error_count(), and reset_main_loop_counters().

◆ stats_n_main_loop_idle

uint64_t stats_n_main_loop_idle = 0
static

How many times have we returned from the main loop with no events.

Definition at line 150 of file mainloop.c.

Referenced by get_main_loop_idle_count(), increment_main_loop_idle_count(), and reset_main_loop_counters().

◆ stats_n_main_loop_successes

uint64_t stats_n_main_loop_successes = 0
static

How many times have we returned from the main loop successfully?

Definition at line 146 of file mainloop.c.

Referenced by get_main_loop_success_count(), increment_main_loop_success_count(), and reset_main_loop_counters().

◆ stats_n_seconds_working

long stats_n_seconds_working = 0
static

How many seconds have we been running?

Definition at line 144 of file mainloop.c.

Referenced by get_uptime(), and reset_uptime().

◆ time_of_last_signewnym

time_t time_of_last_signewnym = 0
static

When did we last process a SIGNEWNYM request?

Definition at line 155 of file mainloop.c.

Referenced by do_signewnym().

◆ time_of_process_start

time_t time_of_process_start = 0

What time did this process start up?

Definition at line 142 of file mainloop.c.

Referenced by tor_init().