Macros | Typedefs | Enumerations | Functions | Variables
mainloop.h File Reference

Go to the source code of this file.


#define connection_add(conn)   connection_add_impl((conn), 0)
#define connection_add_connecting(conn)   connection_add_impl((conn), 1)


typedef enum watchable_events watchable_events_t


enum  watchable_events { READ_EVENT =0x02, WRITE_EVENT =0x04 }


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)
int connection_remove (connection_t *conn)
void connection_unregister_events (connection_t *conn)
int connection_in_array (connection_t *conn)
void add_connection_to_closeable_list (connection_t *conn)
int connection_is_on_closeable_list (connection_t *conn)
 MOCK_DECL (smartlist_t *, get_connection_array,(void))
 MOCK_DECL (uint64_t, get_bytes_read,(void))
 MOCK_DECL (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)
 MOCK_DECL (void, connection_stop_reading,(connection_t *conn))
 MOCK_DECL (void, connection_start_reading,(connection_t *conn))
int connection_is_writing (connection_t *conn)
 MOCK_DECL (void, connection_stop_writing,(connection_t *conn))
 MOCK_DECL (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)
 MOCK_DECL (int, connection_count_moribund,(void))
void directory_all_unreachable (time_t now)
void directory_info_has_arrived (time_t now, int from_cache, int suppress_logs)
void ip_address_changed (int at_interface)
void dns_servers_relaunch_checks (void)
void reset_all_main_loop_timers (void)
void reschedule_descriptor_update_check (void)
void reschedule_directory_downloads (void)
void reschedule_or_state_save (void)
void reschedule_dirvote (const or_options_t *options)
void mainloop_schedule_postloop_cleanup (void)
void rescan_periodic_events (const or_options_t *options)
 MOCK_DECL (void, schedule_rescan_periodic_events,(void))
void update_current_time (time_t now)
 MOCK_DECL (long, get_uptime,(void))
 MOCK_DECL (void, reset_uptime,(void))
unsigned get_signewnym_epoch (void)
int do_main_loop (void)
void reset_main_loop_counters (void)
uint64_t get_main_loop_success_count (void)
uint64_t get_main_loop_error_count (void)
uint64_t get_main_loop_idle_count (void)
void periodic_events_on_new_options (const or_options_t *options)
void do_signewnym (time_t)
time_t get_last_signewnym_time (void)
void mainloop_schedule_shutdown (int delay_sec)
void tor_init_connection_lists (void)
void initialize_mainloop_events (void)
void tor_mainloop_free_all (void)


time_t time_of_process_start
int quiet_level
struct token_bucket_rw_t global_bucket
struct token_bucket_rw_t global_relayed_bucket

Detailed Description

Header file for mainloop.c.

Definition in file mainloop.h.

Typedef Documentation

◆ watchable_events_t

Bitmask for events that we can turn on and off with connection_watch_events.

Enumeration Type Documentation

◆ watchable_events

Bitmask for events that we can turn on and off with connection_watch_events.


We want to know when a connection is readable


We want to know when a connection is writable

Definition at line 35 of file mainloop.h.

Function Documentation

◆ add_connection_to_closeable_list()

void add_connection_to_closeable_list ( connection_t conn)

◆ 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 247 of file mainloop.c.

References connection_t::conn_array_index, CONN_TYPE_AP, connection_t::linked, connection_t::s, SOCKET_OK, TO_EDGE_CONN(), tor_assert(), and connection_t::type.

◆ connection_in_array()

int connection_in_array ( connection_t conn)

Return true iff conn is in the current poll array.

Definition at line 445 of file mainloop.c.

References connection_array, and smartlist_contains().

Referenced by assert_all_pending_dns_resolves_ok(), and MOCK_IMPL().

◆ 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 438 of file mainloop.c.

References closeable_connection_lst, and smartlist_contains().

Referenced by MOCK_IMPL().

◆ connection_is_reading()

int connection_is_reading ( connection_t conn)

Return true iff conn is listening for read events.

Definition at line 511 of file mainloop.c.

References connection_t::read_event, connection_t::reading_from_linked_conn, and tor_assert().

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 657 of file mainloop.c.

References tor_assert(), connection_t::write_event, and connection_t::writing_to_linked_conn.

◆ 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 300 of file mainloop.c.

References tor_assert().

Referenced by connection_unlink().

◆ 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 818 of file mainloop.c.

References active_linked_connection_lst, connection_t::active_on_link, connection_t::linked, smartlist_contains(), smartlist_remove(), and tor_assert().

Referenced by MOCK_IMPL().

◆ connection_unregister_events()

void connection_unregister_events ( connection_t conn)

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

Definition at line 278 of file mainloop.c.

References connection_t::read_event.

◆ 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 496 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 1091 of file mainloop.c.

◆ directory_info_has_arrived()

void directory_info_has_arrived ( time_t  now,
int  from_cache,
int  suppress_logs 

◆ 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 2796 of file mainloop.c.

References dns_reset_correctness_checks(), periodic_events_initialized, and tor_assert().

◆ do_main_loop()

int do_main_loop ( void  )

◆ do_signewnym()

void do_signewnym ( time_t  now)

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

Definition at line 1310 of file mainloop.c.

References handle_deferred_signewnym_cb(), handle_deferred_signewnym_ev, mainloop_event_postloop_new(), mainloop_event_schedule(), MAX_SIGNEWNYM_RATE, signewnym_is_pending, and time_of_last_signewnym.

◆ get_main_loop_error_count()

uint64_t get_main_loop_error_count ( void  )

Get the main loop error counter.

Definition at line 551 of file mainloop.c.

References stats_n_main_loop_errors.

◆ get_main_loop_idle_count()

uint64_t get_main_loop_idle_count ( void  )

Get the main loop idle counter.

Definition at line 565 of file mainloop.c.

References stats_n_main_loop_idle.

◆ get_main_loop_success_count()

uint64_t get_main_loop_success_count ( void  )

Get the main loop success counter.

Definition at line 537 of file mainloop.c.

References stats_n_main_loop_successes.

◆ get_signewnym_epoch()

unsigned get_signewnym_epoch ( void  )

Return the number of times that signewnym has been called.

Definition at line 1334 of file mainloop.c.

References newnym_epoch.

◆ 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 221 of file mainloop.c.

References can_complete_circuits.

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

◆ initialize_mainloop_events()

void initialize_mainloop_events ( void  )

◆ ip_address_changed()

void ip_address_changed ( int  at_interface)

Called when our IP address seems to have changed. at_interface should be true if we detected a change in our interface, and false if we detected a change in our published address.

Definition at line 2762 of file mainloop.c.

References or_options_t::ExitPolicyRejectLocalInterfaces, and or_options_t::ExitRelay.

◆ 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 1738 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 1763 of file mainloop.c.

References mainloop_event_new(), mainloop_event_schedule(), scheduled_shutdown_cb(), and scheduled_shutdown_ev.

◆ 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 229 of file mainloop.c.

References can_complete_circuits.

◆ 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 237 of file mainloop.c.

References can_complete_circuits.

Referenced by circuit_note_clock_jumped().

◆ 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 1638 of file mainloop.c.

References get_my_roles(), periodic_events_initialized, and tor_assert().

Referenced by on_hibernate_state_change(), and rescan_periodic_events_cb().

◆ reschedule_descriptor_update_check()

void reschedule_descriptor_update_check ( void  )

Update our schedule so that we'll check whether we need to update our descriptor immediately, rather than after up to CHECK_DESCRIPTOR_INTERVAL seconds.

Definition at line 1698 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 1710 of file mainloop.c.

References tor_assert().

◆ reschedule_dirvote()

void reschedule_dirvote ( const or_options_t options)

Reschedule the directory-authority voting event. Run this whenever the schedule has changed.

Definition at line 2152 of file mainloop.c.

References periodic_events_initialized.

◆ 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 2198 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 1485 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 521 of file mainloop.c.

References stats_n_main_loop_errors, stats_n_main_loop_idle, and stats_n_main_loop_successes.

◆ 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 486 of file mainloop.c.

References stats_n_bytes_read, and stats_n_bytes_written.

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 791 of file mainloop.c.

References main_loop_should_exit.

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 415 of file mainloop.c.

References active_linked_connection_lst, closeable_connection_lst, and connection_array.

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 767 of file mainloop.c.

References main_loop_exit_value, main_loop_should_exit, and shutdown_did_not_work_event.

◆ 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 2677 of file mainloop.c.

Referenced by periodic_event_dispatch(), and signal_callback().

Variable Documentation

◆ quiet_level

int quiet_level

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.

◆ time_of_process_start

time_t time_of_process_start

What time did this process start up?

Definition at line 145 of file mainloop.c.

Referenced by tor_init().