Rename sandbox_getaddrinfo() et. al.
[tor.git] / src / or / main.c
blobd24f5a7ff2cdef34833fdd3aa81552a30404109d
1 /* Copyright (c) 2001 Matej Pfajfar.
2 * Copyright (c) 2001-2004, Roger Dingledine.
3 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
4 * Copyright (c) 2007-2018, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
7 /**
8 * \file main.c
9 * \brief Toplevel module. Handles signals, multiplexes between
10 * connections, implements main loop, and drives scheduled events.
12 * For the main loop itself; see run_main_loop_once(). It invokes the rest of
13 * Tor mostly through Libevent callbacks. Libevent callbacks can happen when
14 * a timer elapses, a signal is received, a socket is ready to read or write,
15 * or an event is manually activated.
17 * Most events in Tor are driven from these callbacks:
18 * <ul>
19 * <li>conn_read_callback() and conn_write_callback() here, which are
20 * invoked when a socket is ready to read or write respectively.
21 * <li>signal_callback(), which handles incoming signals.
22 * </ul>
23 * Other events are used for specific purposes, or for building more complex
24 * control structures. If you search for usage of tor_libevent_new(), you
25 * will find all the events that we construct in Tor.
27 * Tor has numerous housekeeping operations that need to happen
28 * regularly. They are handled in different ways:
29 * <ul>
30 * <li>The most frequent operations are handled after every read or write
31 * event, at the end of connection_handle_read() and
32 * connection_handle_write().
34 * <li>The next most frequent operations happen after each invocation of the
35 * main loop, in run_main_loop_once().
37 * <li>Once per second, we run all of the operations listed in
38 * second_elapsed_callback(), and in its child, run_scheduled_events().
40 * <li>Once-a-second operations are handled in second_elapsed_callback().
42 * <li>More infrequent operations take place based on the periodic event
43 * driver in periodic.c . These are stored in the periodic_events[]
44 * table.
45 * </ul>
47 **/
49 #define MAIN_PRIVATE
50 #include "or/or.h"
51 #include "or/addressmap.h"
52 #include "lib/err/backtrace.h"
53 #include "or/bridges.h"
54 #include "lib/container/buffers.h"
55 #include "lib/tls/buffers_tls.h"
56 #include "or/channel.h"
57 #include "or/channeltls.h"
58 #include "or/channelpadding.h"
59 #include "or/circuitbuild.h"
60 #include "or/circuitlist.h"
61 #include "or/circuituse.h"
62 #include "or/circuitmux_ewma.h"
63 #include "or/command.h"
64 #include "lib/compress/compress.h"
65 #include "or/config.h"
66 #include "or/confparse.h"
67 #include "or/connection.h"
68 #include "or/connection_edge.h"
69 #include "or/connection_or.h"
70 #include "or/consdiffmgr.h"
71 #include "or/control.h"
72 #include "or/cpuworker.h"
73 #include "lib/crypt_ops/crypto_s2k.h"
74 #include "lib/crypt_ops/crypto_rand.h"
75 #include "or/directory.h"
76 #include "or/dirserv.h"
77 #include "or/dns.h"
78 #include "or/dnsserv.h"
79 #include "or/dos.h"
80 #include "or/entrynodes.h"
81 #include "or/geoip.h"
82 #include "or/hibernate.h"
83 #include "or/hs_cache.h"
84 #include "or/hs_circuitmap.h"
85 #include "or/hs_client.h"
86 #include "or/keypin.h"
87 #include "or/main.h"
88 #include "or/microdesc.h"
89 #include "or/networkstatus.h"
90 #include "or/nodelist.h"
91 #include "or/ntmain.h"
92 #include "or/onion.h"
93 #include "or/periodic.h"
94 #include "or/policies.h"
95 #include "or/protover.h"
96 #include "or/transports.h"
97 #include "or/relay.h"
98 #include "or/rendclient.h"
99 #include "or/rendcommon.h"
100 #include "or/rendservice.h"
101 #include "or/rephist.h"
102 #include "or/router.h"
103 #include "or/routerkeys.h"
104 #include "or/routerlist.h"
105 #include "or/routerparse.h"
106 #include "or/scheduler.h"
107 #include "or/statefile.h"
108 #include "or/status.h"
109 #include "or/tor_api.h"
110 #include "or/tor_api_internal.h"
111 #include "lib/process/waitpid.h"
112 #include "or/ext_orport.h"
113 #include "lib/memarea/memarea.h"
114 #include "lib/meminfo/meminfo.h"
115 #include "lib/osinfo/uname.h"
116 #include "lib/sandbox/sandbox.h"
117 #include "lib/fs/lockfile.h"
118 #include "lib/net/buffers_net.h"
120 #include <event2/event.h>
122 #include "or/dirauth/dirvote.h"
123 #include "or/dirauth/mode.h"
124 #include "or/dirauth/shared_random.h"
126 #include "or/cell_st.h"
127 #include "or/entry_connection_st.h"
128 #include "or/networkstatus_st.h"
129 #include "or/or_connection_st.h"
130 #include "or/port_cfg_st.h"
131 #include "or/routerinfo_st.h"
132 #include "or/socks_request_st.h"
134 #ifdef HAVE_SYSTEMD
135 # if defined(__COVERITY__) && !defined(__INCLUDE_LEVEL__)
136 /* Systemd's use of gcc's __INCLUDE_LEVEL__ extension macro appears to confuse
137 * Coverity. Here's a kludge to unconfuse it.
139 # define __INCLUDE_LEVEL__ 2
140 #endif /* defined(__COVERITY__) && !defined(__INCLUDE_LEVEL__) */
141 #include <systemd/sd-daemon.h>
142 #endif /* defined(HAVE_SYSTEMD) */
144 void evdns_shutdown(int);
146 #ifdef HAVE_RUST
147 // helper function defined in Rust to output a log message indicating if tor is
148 // running with Rust enabled. See src/rust/tor_util
149 void rust_log_welcome_string(void);
150 #endif
152 /********* PROTOTYPES **********/
154 static void dumpmemusage(int severity);
155 static void dumpstats(int severity); /* log stats */
156 static void conn_read_callback(evutil_socket_t fd, short event, void *_conn);
157 static void conn_write_callback(evutil_socket_t fd, short event, void *_conn);
158 static void second_elapsed_callback(periodic_timer_t *timer, void *args);
159 static int conn_close_if_marked(int i);
160 static void connection_start_reading_from_linked_conn(connection_t *conn);
161 static int connection_should_read_from_linked_conn(connection_t *conn);
162 static int run_main_loop_until_done(void);
163 static void process_signal(int sig);
164 static void shutdown_did_not_work_callback(evutil_socket_t fd, short event,
165 void *arg) ATTR_NORETURN;
167 /********* START VARIABLES **********/
169 /* Token bucket for all traffic. */
170 token_bucket_rw_t global_bucket;
172 /* Token bucket for relayed traffic. */
173 token_bucket_rw_t global_relayed_bucket;
175 /* XXX we might want to keep stats about global_relayed_*_bucket too. Or not.*/
176 /** How many bytes have we read since we started the process? */
177 static uint64_t stats_n_bytes_read = 0;
178 /** How many bytes have we written since we started the process? */
179 static uint64_t stats_n_bytes_written = 0;
180 /** What time did this process start up? */
181 time_t time_of_process_start = 0;
182 /** How many seconds have we been running? */
183 static long stats_n_seconds_working = 0;
184 /** How many times have we returned from the main loop successfully? */
185 static uint64_t stats_n_main_loop_successes = 0;
186 /** How many times have we received an error from the main loop? */
187 static uint64_t stats_n_main_loop_errors = 0;
188 /** How many times have we returned from the main loop with no events. */
189 static uint64_t stats_n_main_loop_idle = 0;
191 /** How often will we honor SIGNEWNYM requests? */
192 #define MAX_SIGNEWNYM_RATE 10
193 /** When did we last process a SIGNEWNYM request? */
194 static time_t time_of_last_signewnym = 0;
195 /** Is there a signewnym request we're currently waiting to handle? */
196 static int signewnym_is_pending = 0;
197 /** Mainloop event for the deferred signewnym call. */
198 static mainloop_event_t *handle_deferred_signewnym_ev = NULL;
199 /** How many times have we called newnym? */
200 static unsigned newnym_epoch = 0;
202 /** Smartlist of all open connections. */
203 STATIC smartlist_t *connection_array = NULL;
204 /** List of connections that have been marked for close and need to be freed
205 * and removed from connection_array. */
206 static smartlist_t *closeable_connection_lst = NULL;
207 /** List of linked connections that are currently reading data into their
208 * inbuf from their partner's outbuf. */
209 static smartlist_t *active_linked_connection_lst = NULL;
210 /** Flag: Set to true iff we entered the current libevent main loop via
211 * <b>loop_once</b>. If so, there's no need to trigger a loopexit in order
212 * to handle linked connections. */
213 static int called_loop_once = 0;
214 /** Flag: if true, it's time to shut down, so the main loop should exit as
215 * soon as possible.
217 static int main_loop_should_exit = 0;
218 /** The return value that the main loop should yield when it exits, if
219 * main_loop_should_exit is true.
221 static int main_loop_exit_value = 0;
223 /** We set this to 1 when we've opened a circuit, so we can print a log
224 * entry to inform the user that Tor is working. We set it to 0 when
225 * we think the fact that we once opened a circuit doesn't mean we can do so
226 * any longer (a big time jump happened, when we notice our directory is
227 * heinously out-of-date, etc.
229 static int can_complete_circuits = 0;
231 /** How often do we check for router descriptors that we should download
232 * when we have too little directory info? */
233 #define GREEDY_DESCRIPTOR_RETRY_INTERVAL (10)
234 /** How often do we check for router descriptors that we should download
235 * when we have enough directory info? */
236 #define LAZY_DESCRIPTOR_RETRY_INTERVAL (60)
238 /** Decides our behavior when no logs are configured/before any
239 * logs have been configured. For 0, we log notice to stdout as normal.
240 * For 1, we log warnings only. For 2, we log nothing.
242 int quiet_level = 0;
244 /********* END VARIABLES ************/
246 /****************************************************************************
248 * This section contains accessors and other methods on the connection_array
249 * variables (which are global within this file and unavailable outside it).
251 ****************************************************************************/
253 /** Return 1 if we have successfully built a circuit, and nothing has changed
254 * to make us think that maybe we can't.
257 have_completed_a_circuit(void)
259 return can_complete_circuits;
262 /** Note that we have successfully built a circuit, so that reachability
263 * testing and introduction points and so on may be attempted. */
264 void
265 note_that_we_completed_a_circuit(void)
267 can_complete_circuits = 1;
270 /** Note that something has happened (like a clock jump, or DisableNetwork) to
271 * make us think that maybe we can't complete circuits. */
272 void
273 note_that_we_maybe_cant_complete_circuits(void)
275 can_complete_circuits = 0;
278 /** Add <b>conn</b> to the array of connections that we can poll on. The
279 * connection's socket must be set; the connection starts out
280 * non-reading and non-writing.
283 connection_add_impl(connection_t *conn, int is_connecting)
285 tor_assert(conn);
286 tor_assert(SOCKET_OK(conn->s) ||
287 conn->linked ||
288 (conn->type == CONN_TYPE_AP &&
289 TO_EDGE_CONN(conn)->is_dns_request));
291 tor_assert(conn->conn_array_index == -1); /* can only connection_add once */
292 conn->conn_array_index = smartlist_len(connection_array);
293 smartlist_add(connection_array, conn);
295 (void) is_connecting;
297 if (SOCKET_OK(conn->s) || conn->linked) {
298 conn->read_event = tor_event_new(tor_libevent_get_base(),
299 conn->s, EV_READ|EV_PERSIST, conn_read_callback, conn);
300 conn->write_event = tor_event_new(tor_libevent_get_base(),
301 conn->s, EV_WRITE|EV_PERSIST, conn_write_callback, conn);
302 /* XXXX CHECK FOR NULL RETURN! */
305 log_debug(LD_NET,"new conn type %s, socket %d, address %s, n_conns %d.",
306 conn_type_to_string(conn->type), (int)conn->s, conn->address,
307 smartlist_len(connection_array));
309 return 0;
312 /** Tell libevent that we don't care about <b>conn</b> any more. */
313 void
314 connection_unregister_events(connection_t *conn)
316 if (conn->read_event) {
317 if (event_del(conn->read_event))
318 log_warn(LD_BUG, "Error removing read event for %d", (int)conn->s);
319 tor_free(conn->read_event);
321 if (conn->write_event) {
322 if (event_del(conn->write_event))
323 log_warn(LD_BUG, "Error removing write event for %d", (int)conn->s);
324 tor_free(conn->write_event);
326 if (conn->type == CONN_TYPE_AP_DNS_LISTENER) {
327 dnsserv_close_listener(conn);
331 /** Remove the connection from the global list, and remove the
332 * corresponding poll entry. Calling this function will shift the last
333 * connection (if any) into the position occupied by conn.
336 connection_remove(connection_t *conn)
338 int current_index;
339 connection_t *tmp;
341 tor_assert(conn);
343 log_debug(LD_NET,"removing socket %d (type %s), n_conns now %d",
344 (int)conn->s, conn_type_to_string(conn->type),
345 smartlist_len(connection_array));
347 if (conn->type == CONN_TYPE_AP && conn->socket_family == AF_UNIX) {
348 log_info(LD_NET, "Closing SOCKS Unix socket connection");
351 control_event_conn_bandwidth(conn);
353 tor_assert(conn->conn_array_index >= 0);
354 current_index = conn->conn_array_index;
355 connection_unregister_events(conn); /* This is redundant, but cheap. */
356 if (current_index == smartlist_len(connection_array)-1) { /* at the end */
357 smartlist_del(connection_array, current_index);
358 return 0;
361 /* replace this one with the one at the end */
362 smartlist_del(connection_array, current_index);
363 tmp = smartlist_get(connection_array, current_index);
364 tmp->conn_array_index = current_index;
366 return 0;
369 /** If <b>conn</b> is an edge conn, remove it from the list
370 * of conn's on this circuit. If it's not on an edge,
371 * flush and send destroys for all circuits on this conn.
373 * Remove it from connection_array (if applicable) and
374 * from closeable_connection_list.
376 * Then free it.
378 static void
379 connection_unlink(connection_t *conn)
381 connection_about_to_close_connection(conn);
382 if (conn->conn_array_index >= 0) {
383 connection_remove(conn);
385 if (conn->linked_conn) {
386 conn->linked_conn->linked_conn = NULL;
387 if (! conn->linked_conn->marked_for_close &&
388 conn->linked_conn->reading_from_linked_conn)
389 connection_start_reading(conn->linked_conn);
390 conn->linked_conn = NULL;
392 smartlist_remove(closeable_connection_lst, conn);
393 smartlist_remove(active_linked_connection_lst, conn);
394 if (conn->type == CONN_TYPE_EXIT) {
395 assert_connection_edge_not_dns_pending(TO_EDGE_CONN(conn));
397 if (conn->type == CONN_TYPE_OR) {
398 if (!tor_digest_is_zero(TO_OR_CONN(conn)->identity_digest))
399 connection_or_clear_identity(TO_OR_CONN(conn));
400 /* connection_unlink() can only get called if the connection
401 * was already on the closeable list, and it got there by
402 * connection_mark_for_close(), which was called from
403 * connection_or_close_normally() or
404 * connection_or_close_for_error(), so the channel should
405 * already be in CHANNEL_STATE_CLOSING, and then the
406 * connection_about_to_close_connection() goes to
407 * connection_or_about_to_close(), which calls channel_closed()
408 * to notify the channel_t layer, and closed the channel, so
409 * nothing more to do here to deal with the channel associated
410 * with an orconn.
413 connection_free(conn);
417 * Callback: used to activate read events for all linked connections, so
418 * libevent knows to call their read callbacks. This callback run as a
419 * postloop event, so that the events _it_ activates don't happen until
420 * Libevent has a chance to check for other events.
422 static void
423 schedule_active_linked_connections_cb(mainloop_event_t *event, void *arg)
425 (void)event;
426 (void)arg;
428 /* All active linked conns should get their read events activated,
429 * so that libevent knows to run their callbacks. */
430 SMARTLIST_FOREACH(active_linked_connection_lst, connection_t *, conn,
431 event_active(conn->read_event, EV_READ, 1));
434 /** Event that invokes schedule_active_linked_connections_cb. */
435 static mainloop_event_t *schedule_active_linked_connections_event = NULL;
437 /** Initialize the global connection list, closeable connection list,
438 * and active connection list. */
439 STATIC void
440 init_connection_lists(void)
442 if (!connection_array)
443 connection_array = smartlist_new();
444 if (!closeable_connection_lst)
445 closeable_connection_lst = smartlist_new();
446 if (!active_linked_connection_lst)
447 active_linked_connection_lst = smartlist_new();
450 /** Schedule <b>conn</b> to be closed. **/
451 void
452 add_connection_to_closeable_list(connection_t *conn)
454 tor_assert(!smartlist_contains(closeable_connection_lst, conn));
455 tor_assert(conn->marked_for_close);
456 assert_connection_ok(conn, time(NULL));
457 smartlist_add(closeable_connection_lst, conn);
458 mainloop_schedule_postloop_cleanup();
461 /** Return 1 if conn is on the closeable list, else return 0. */
463 connection_is_on_closeable_list(connection_t *conn)
465 return smartlist_contains(closeable_connection_lst, conn);
468 /** Return true iff conn is in the current poll array. */
470 connection_in_array(connection_t *conn)
472 return smartlist_contains(connection_array, conn);
475 /** Set <b>*array</b> to an array of all connections. <b>*array</b> must not
476 * be modified.
478 MOCK_IMPL(smartlist_t *,
479 get_connection_array, (void))
481 if (!connection_array)
482 connection_array = smartlist_new();
483 return connection_array;
487 * Return the amount of network traffic read, in bytes, over the life of this
488 * process.
490 MOCK_IMPL(uint64_t,
491 get_bytes_read,(void))
493 return stats_n_bytes_read;
497 * Return the amount of network traffic read, in bytes, over the life of this
498 * process.
500 MOCK_IMPL(uint64_t,
501 get_bytes_written,(void))
503 return stats_n_bytes_written;
507 * Increment the amount of network traffic read and written, over the life of
508 * this process.
510 void
511 stats_increment_bytes_read_and_written(uint64_t r, uint64_t w)
513 stats_n_bytes_read += r;
514 stats_n_bytes_written += w;
517 /** Set the event mask on <b>conn</b> to <b>events</b>. (The event
518 * mask is a bitmask whose bits are READ_EVENT and WRITE_EVENT)
520 void
521 connection_watch_events(connection_t *conn, watchable_events_t events)
523 if (events & READ_EVENT)
524 connection_start_reading(conn);
525 else
526 connection_stop_reading(conn);
528 if (events & WRITE_EVENT)
529 connection_start_writing(conn);
530 else
531 connection_stop_writing(conn);
534 /** Return true iff <b>conn</b> is listening for read events. */
536 connection_is_reading(connection_t *conn)
538 tor_assert(conn);
540 return conn->reading_from_linked_conn ||
541 (conn->read_event && event_pending(conn->read_event, EV_READ, NULL));
544 /** Reset our main loop counters. */
545 void
546 reset_main_loop_counters(void)
548 stats_n_main_loop_successes = 0;
549 stats_n_main_loop_errors = 0;
550 stats_n_main_loop_idle = 0;
553 /** Increment the main loop success counter. */
554 static void
555 increment_main_loop_success_count(void)
557 ++stats_n_main_loop_successes;
560 /** Get the main loop success counter. */
561 uint64_t
562 get_main_loop_success_count(void)
564 return stats_n_main_loop_successes;
567 /** Increment the main loop error counter. */
568 static void
569 increment_main_loop_error_count(void)
571 ++stats_n_main_loop_errors;
574 /** Get the main loop error counter. */
575 uint64_t
576 get_main_loop_error_count(void)
578 return stats_n_main_loop_errors;
581 /** Increment the main loop idle counter. */
582 static void
583 increment_main_loop_idle_count(void)
585 ++stats_n_main_loop_idle;
588 /** Get the main loop idle counter. */
589 uint64_t
590 get_main_loop_idle_count(void)
592 return stats_n_main_loop_idle;
595 /** Check whether <b>conn</b> is correct in having (or not having) a
596 * read/write event (passed in <b>ev</b>). On success, return 0. On failure,
597 * log a warning and return -1. */
598 static int
599 connection_check_event(connection_t *conn, struct event *ev)
601 int bad;
603 if (conn->type == CONN_TYPE_AP && TO_EDGE_CONN(conn)->is_dns_request) {
604 /* DNS requests which we launch through the dnsserv.c module do not have
605 * any underlying socket or any underlying linked connection, so they
606 * shouldn't have any attached events either.
608 bad = ev != NULL;
609 } else {
610 /* Everything else should have an underlying socket, or a linked
611 * connection (which is also tracked with a read_event/write_event pair).
613 bad = ev == NULL;
616 if (bad) {
617 log_warn(LD_BUG, "Event missing on connection %p [%s;%s]. "
618 "socket=%d. linked=%d. "
619 "is_dns_request=%d. Marked_for_close=%s:%d",
620 conn,
621 conn_type_to_string(conn->type),
622 conn_state_to_string(conn->type, conn->state),
623 (int)conn->s, (int)conn->linked,
624 (conn->type == CONN_TYPE_AP &&
625 TO_EDGE_CONN(conn)->is_dns_request),
626 conn->marked_for_close_file ? conn->marked_for_close_file : "-",
627 conn->marked_for_close
629 log_backtrace(LOG_WARN, LD_BUG, "Backtrace attached.");
630 return -1;
632 return 0;
635 /** Tell the main loop to stop notifying <b>conn</b> of any read events. */
636 MOCK_IMPL(void,
637 connection_stop_reading,(connection_t *conn))
639 tor_assert(conn);
641 if (connection_check_event(conn, conn->read_event) < 0) {
642 return;
645 if (conn->linked) {
646 conn->reading_from_linked_conn = 0;
647 connection_stop_reading_from_linked_conn(conn);
648 } else {
649 if (event_del(conn->read_event))
650 log_warn(LD_NET, "Error from libevent setting read event state for %d "
651 "to unwatched: %s",
652 (int)conn->s,
653 tor_socket_strerror(tor_socket_errno(conn->s)));
657 /** Tell the main loop to start notifying <b>conn</b> of any read events. */
658 MOCK_IMPL(void,
659 connection_start_reading,(connection_t *conn))
661 tor_assert(conn);
663 if (connection_check_event(conn, conn->read_event) < 0) {
664 return;
667 if (conn->linked) {
668 conn->reading_from_linked_conn = 1;
669 if (connection_should_read_from_linked_conn(conn))
670 connection_start_reading_from_linked_conn(conn);
671 } else {
672 if (event_add(conn->read_event, NULL))
673 log_warn(LD_NET, "Error from libevent setting read event state for %d "
674 "to watched: %s",
675 (int)conn->s,
676 tor_socket_strerror(tor_socket_errno(conn->s)));
680 /** Return true iff <b>conn</b> is listening for write events. */
682 connection_is_writing(connection_t *conn)
684 tor_assert(conn);
686 return conn->writing_to_linked_conn ||
687 (conn->write_event && event_pending(conn->write_event, EV_WRITE, NULL));
690 /** Tell the main loop to stop notifying <b>conn</b> of any write events. */
691 MOCK_IMPL(void,
692 connection_stop_writing,(connection_t *conn))
694 tor_assert(conn);
696 if (connection_check_event(conn, conn->write_event) < 0) {
697 return;
700 if (conn->linked) {
701 conn->writing_to_linked_conn = 0;
702 if (conn->linked_conn)
703 connection_stop_reading_from_linked_conn(conn->linked_conn);
704 } else {
705 if (event_del(conn->write_event))
706 log_warn(LD_NET, "Error from libevent setting write event state for %d "
707 "to unwatched: %s",
708 (int)conn->s,
709 tor_socket_strerror(tor_socket_errno(conn->s)));
713 /** Tell the main loop to start notifying <b>conn</b> of any write events. */
714 MOCK_IMPL(void,
715 connection_start_writing,(connection_t *conn))
717 tor_assert(conn);
719 if (connection_check_event(conn, conn->write_event) < 0) {
720 return;
723 if (conn->linked) {
724 conn->writing_to_linked_conn = 1;
725 if (conn->linked_conn &&
726 connection_should_read_from_linked_conn(conn->linked_conn))
727 connection_start_reading_from_linked_conn(conn->linked_conn);
728 } else {
729 if (event_add(conn->write_event, NULL))
730 log_warn(LD_NET, "Error from libevent setting write event state for %d "
731 "to watched: %s",
732 (int)conn->s,
733 tor_socket_strerror(tor_socket_errno(conn->s)));
737 /** Return true iff <b>conn</b> is linked conn, and reading from the conn
738 * linked to it would be good and feasible. (Reading is "feasible" if the
739 * other conn exists and has data in its outbuf, and is "good" if we have our
740 * reading_from_linked_conn flag set and the other conn has its
741 * writing_to_linked_conn flag set.)*/
742 static int
743 connection_should_read_from_linked_conn(connection_t *conn)
745 if (conn->linked && conn->reading_from_linked_conn) {
746 if (! conn->linked_conn ||
747 (conn->linked_conn->writing_to_linked_conn &&
748 buf_datalen(conn->linked_conn->outbuf)))
749 return 1;
751 return 0;
754 /** Event to run 'shutdown did not work callback'. */
755 static struct event *shutdown_did_not_work_event = NULL;
757 /** Failsafe measure that should never actually be necessary: If
758 * tor_shutdown_event_loop_and_exit() somehow doesn't successfully exit the
759 * event loop, then this callback will kill Tor with an assertion failure
760 * seconds later
762 static void
763 shutdown_did_not_work_callback(evutil_socket_t fd, short event, void *arg)
765 // LCOV_EXCL_START
766 (void) fd;
767 (void) event;
768 (void) arg;
769 tor_assert_unreached();
770 // LCOV_EXCL_STOP
773 #ifdef ENABLE_RESTART_DEBUGGING
774 static struct event *tor_shutdown_event_loop_for_restart_event = NULL;
775 static void
776 tor_shutdown_event_loop_for_restart_cb(
777 evutil_socket_t fd, short event, void *arg)
779 (void)fd;
780 (void)event;
781 (void)arg;
782 tor_event_free(tor_shutdown_event_loop_for_restart_event);
783 tor_shutdown_event_loop_and_exit(0);
785 #endif
788 * After finishing the current callback (if any), shut down the main loop,
789 * clean up the process, and exit with <b>exitcode</b>.
791 void
792 tor_shutdown_event_loop_and_exit(int exitcode)
794 if (main_loop_should_exit)
795 return; /* Ignore multiple calls to this function. */
797 main_loop_should_exit = 1;
798 main_loop_exit_value = exitcode;
800 /* Die with an assertion failure in ten seconds, if for some reason we don't
801 * exit normally. */
802 /* XXXX We should consider this code if it's never used. */
803 struct timeval ten_seconds = { 10, 0 };
804 shutdown_did_not_work_event = tor_evtimer_new(
805 tor_libevent_get_base(),
806 shutdown_did_not_work_callback, NULL);
807 event_add(shutdown_did_not_work_event, &ten_seconds);
809 /* Unlike exit_loop_after_delay(), exit_loop_after_callback
810 * prevents other callbacks from running. */
811 tor_libevent_exit_loop_after_callback(tor_libevent_get_base());
814 /** Return true iff tor_shutdown_event_loop_and_exit() has been called. */
816 tor_event_loop_shutdown_is_pending(void)
818 return main_loop_should_exit;
821 /** Helper: Tell the main loop to begin reading bytes into <b>conn</b> from
822 * its linked connection, if it is not doing so already. Called by
823 * connection_start_reading and connection_start_writing as appropriate. */
824 static void
825 connection_start_reading_from_linked_conn(connection_t *conn)
827 tor_assert(conn);
828 tor_assert(conn->linked == 1);
830 if (!conn->active_on_link) {
831 conn->active_on_link = 1;
832 smartlist_add(active_linked_connection_lst, conn);
833 mainloop_event_activate(schedule_active_linked_connections_event);
834 } else {
835 tor_assert(smartlist_contains(active_linked_connection_lst, conn));
839 /** Tell the main loop to stop reading bytes into <b>conn</b> from its linked
840 * connection, if is currently doing so. Called by connection_stop_reading,
841 * connection_stop_writing, and connection_read. */
842 void
843 connection_stop_reading_from_linked_conn(connection_t *conn)
845 tor_assert(conn);
846 tor_assert(conn->linked == 1);
848 if (conn->active_on_link) {
849 conn->active_on_link = 0;
850 /* FFFF We could keep an index here so we can smartlist_del
851 * cleanly. On the other hand, this doesn't show up on profiles,
852 * so let's leave it alone for now. */
853 smartlist_remove(active_linked_connection_lst, conn);
854 } else {
855 tor_assert(!smartlist_contains(active_linked_connection_lst, conn));
859 /** Close all connections that have been scheduled to get closed. */
860 STATIC void
861 close_closeable_connections(void)
863 int i;
864 for (i = 0; i < smartlist_len(closeable_connection_lst); ) {
865 connection_t *conn = smartlist_get(closeable_connection_lst, i);
866 if (conn->conn_array_index < 0) {
867 connection_unlink(conn); /* blow it away right now */
868 } else {
869 if (!conn_close_if_marked(conn->conn_array_index))
870 ++i;
875 /** Count moribund connections for the OOS handler */
876 MOCK_IMPL(int,
877 connection_count_moribund, (void))
879 int moribund = 0;
882 * Count things we'll try to kill when close_closeable_connections()
883 * runs next.
885 SMARTLIST_FOREACH_BEGIN(closeable_connection_lst, connection_t *, conn) {
886 if (SOCKET_OK(conn->s) && connection_is_moribund(conn)) ++moribund;
887 } SMARTLIST_FOREACH_END(conn);
889 return moribund;
892 /** Libevent callback: this gets invoked when (connection_t*)<b>conn</b> has
893 * some data to read. */
894 static void
895 conn_read_callback(evutil_socket_t fd, short event, void *_conn)
897 connection_t *conn = _conn;
898 (void)fd;
899 (void)event;
901 log_debug(LD_NET,"socket %d wants to read.",(int)conn->s);
903 /* assert_connection_ok(conn, time(NULL)); */
905 if (connection_handle_read(conn) < 0) {
906 if (!conn->marked_for_close) {
907 #ifndef _WIN32
908 log_warn(LD_BUG,"Unhandled error on read for %s connection "
909 "(fd %d); removing",
910 conn_type_to_string(conn->type), (int)conn->s);
911 tor_fragile_assert();
912 #endif /* !defined(_WIN32) */
913 if (CONN_IS_EDGE(conn))
914 connection_edge_end_errno(TO_EDGE_CONN(conn));
915 connection_mark_for_close(conn);
918 assert_connection_ok(conn, time(NULL));
920 if (smartlist_len(closeable_connection_lst))
921 close_closeable_connections();
924 /** Libevent callback: this gets invoked when (connection_t*)<b>conn</b> has
925 * some data to write. */
926 static void
927 conn_write_callback(evutil_socket_t fd, short events, void *_conn)
929 connection_t *conn = _conn;
930 (void)fd;
931 (void)events;
933 LOG_FN_CONN(conn, (LOG_DEBUG, LD_NET, "socket %d wants to write.",
934 (int)conn->s));
936 /* assert_connection_ok(conn, time(NULL)); */
938 if (connection_handle_write(conn, 0) < 0) {
939 if (!conn->marked_for_close) {
940 /* this connection is broken. remove it. */
941 log_fn(LOG_WARN,LD_BUG,
942 "unhandled error on write for %s connection (fd %d); removing",
943 conn_type_to_string(conn->type), (int)conn->s);
944 tor_fragile_assert();
945 if (CONN_IS_EDGE(conn)) {
946 /* otherwise we cry wolf about duplicate close */
947 edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
948 if (!edge_conn->end_reason)
949 edge_conn->end_reason = END_STREAM_REASON_INTERNAL;
950 edge_conn->edge_has_sent_end = 1;
952 connection_close_immediate(conn); /* So we don't try to flush. */
953 connection_mark_for_close(conn);
956 assert_connection_ok(conn, time(NULL));
958 if (smartlist_len(closeable_connection_lst))
959 close_closeable_connections();
962 /** If the connection at connection_array[i] is marked for close, then:
963 * - If it has data that it wants to flush, try to flush it.
964 * - If it _still_ has data to flush, and conn->hold_open_until_flushed is
965 * true, then leave the connection open and return.
966 * - Otherwise, remove the connection from connection_array and from
967 * all other lists, close it, and free it.
968 * Returns 1 if the connection was closed, 0 otherwise.
970 static int
971 conn_close_if_marked(int i)
973 connection_t *conn;
974 int retval;
975 time_t now;
977 conn = smartlist_get(connection_array, i);
978 if (!conn->marked_for_close)
979 return 0; /* nothing to see here, move along */
980 now = time(NULL);
981 assert_connection_ok(conn, now);
982 /* assert_all_pending_dns_resolves_ok(); */
984 log_debug(LD_NET,"Cleaning up connection (fd "TOR_SOCKET_T_FORMAT").",
985 conn->s);
987 /* If the connection we are about to close was trying to connect to
988 a proxy server and failed, the client won't be able to use that
989 proxy. We should warn the user about this. */
990 if (conn->proxy_state == PROXY_INFANT)
991 log_failed_proxy_connection(conn);
993 if ((SOCKET_OK(conn->s) || conn->linked_conn) &&
994 connection_wants_to_flush(conn)) {
995 /* s == -1 means it's an incomplete edge connection, or that the socket
996 * has already been closed as unflushable. */
997 ssize_t sz = connection_bucket_write_limit(conn, now);
998 if (!conn->hold_open_until_flushed)
999 log_info(LD_NET,
1000 "Conn (addr %s, fd %d, type %s, state %d) marked, but wants "
1001 "to flush %d bytes. (Marked at %s:%d)",
1002 escaped_safe_str_client(conn->address),
1003 (int)conn->s, conn_type_to_string(conn->type), conn->state,
1004 (int)conn->outbuf_flushlen,
1005 conn->marked_for_close_file, conn->marked_for_close);
1006 if (conn->linked_conn) {
1007 retval = buf_move_to_buf(conn->linked_conn->inbuf, conn->outbuf,
1008 &conn->outbuf_flushlen);
1009 if (retval >= 0) {
1010 /* The linked conn will notice that it has data when it notices that
1011 * we're gone. */
1012 connection_start_reading_from_linked_conn(conn->linked_conn);
1014 log_debug(LD_GENERAL, "Flushed last %d bytes from a linked conn; "
1015 "%d left; flushlen %d; wants-to-flush==%d", retval,
1016 (int)connection_get_outbuf_len(conn),
1017 (int)conn->outbuf_flushlen,
1018 connection_wants_to_flush(conn));
1019 } else if (connection_speaks_cells(conn)) {
1020 if (conn->state == OR_CONN_STATE_OPEN) {
1021 retval = buf_flush_to_tls(conn->outbuf, TO_OR_CONN(conn)->tls, sz,
1022 &conn->outbuf_flushlen);
1023 } else
1024 retval = -1; /* never flush non-open broken tls connections */
1025 } else {
1026 retval = buf_flush_to_socket(conn->outbuf, conn->s, sz,
1027 &conn->outbuf_flushlen);
1029 if (retval >= 0 && /* Technically, we could survive things like
1030 TLS_WANT_WRITE here. But don't bother for now. */
1031 conn->hold_open_until_flushed && connection_wants_to_flush(conn)) {
1032 if (retval > 0) {
1033 LOG_FN_CONN(conn, (LOG_INFO,LD_NET,
1034 "Holding conn (fd %d) open for more flushing.",
1035 (int)conn->s));
1036 conn->timestamp_last_write_allowed = now; /* reset so we can flush
1037 * more */
1038 } else if (sz == 0) {
1039 /* Also, retval==0. If we get here, we didn't want to write anything
1040 * (because of rate-limiting) and we didn't. */
1042 /* Connection must flush before closing, but it's being rate-limited.
1043 * Let's remove from Libevent, and mark it as blocked on bandwidth
1044 * so it will be re-added on next token bucket refill. Prevents
1045 * busy Libevent loops where we keep ending up here and returning
1046 * 0 until we are no longer blocked on bandwidth.
1048 connection_consider_empty_read_buckets(conn);
1049 connection_consider_empty_write_buckets(conn);
1051 /* Make sure that consider_empty_buckets really disabled the
1052 * connection: */
1053 if (BUG(connection_is_writing(conn))) {
1054 connection_write_bw_exhausted(conn, true);
1056 if (BUG(connection_is_reading(conn))) {
1057 /* XXXX+ We should make this code unreachable; if a connection is
1058 * marked for close and flushing, there is no point in reading to it
1059 * at all. Further, checking at this point is a bit of a hack: it
1060 * would make much more sense to react in
1061 * connection_handle_read_impl, or to just stop reading in
1062 * mark_and_flush */
1063 connection_read_bw_exhausted(conn, true/* kludge. */);
1066 return 0;
1068 if (connection_wants_to_flush(conn)) {
1069 log_fn(LOG_INFO, LD_NET, "We stalled too much while trying to write %d "
1070 "bytes to address %s. If this happens a lot, either "
1071 "something is wrong with your network connection, or "
1072 "something is wrong with theirs. "
1073 "(fd %d, type %s, state %d, marked at %s:%d).",
1074 (int)connection_get_outbuf_len(conn),
1075 escaped_safe_str_client(conn->address),
1076 (int)conn->s, conn_type_to_string(conn->type), conn->state,
1077 conn->marked_for_close_file,
1078 conn->marked_for_close);
1082 connection_unlink(conn); /* unlink, remove, free */
1083 return 1;
1086 /** Implementation for directory_all_unreachable. This is done in a callback,
1087 * since otherwise it would complicate Tor's control-flow graph beyond all
1088 * reason.
1090 static void
1091 directory_all_unreachable_cb(mainloop_event_t *event, void *arg)
1093 (void)event;
1094 (void)arg;
1096 connection_t *conn;
1098 while ((conn = connection_get_by_type_state(CONN_TYPE_AP,
1099 AP_CONN_STATE_CIRCUIT_WAIT))) {
1100 entry_connection_t *entry_conn = TO_ENTRY_CONN(conn);
1101 log_notice(LD_NET,
1102 "Is your network connection down? "
1103 "Failing connection to '%s:%d'.",
1104 safe_str_client(entry_conn->socks_request->address),
1105 entry_conn->socks_request->port);
1106 connection_mark_unattached_ap(entry_conn,
1107 END_STREAM_REASON_NET_UNREACHABLE);
1109 control_event_general_error("DIR_ALL_UNREACHABLE");
1112 static mainloop_event_t *directory_all_unreachable_cb_event = NULL;
1114 /** We've just tried every dirserver we know about, and none of
1115 * them were reachable. Assume the network is down. Change state
1116 * so next time an application connection arrives we'll delay it
1117 * and try another directory fetch. Kill off all the circuit_wait
1118 * streams that are waiting now, since they will all timeout anyway.
1120 void
1121 directory_all_unreachable(time_t now)
1123 (void)now;
1125 reset_uptime(); /* reset it */
1127 if (!directory_all_unreachable_cb_event) {
1128 directory_all_unreachable_cb_event =
1129 mainloop_event_new(directory_all_unreachable_cb, NULL);
1130 tor_assert(directory_all_unreachable_cb_event);
1133 mainloop_event_activate(directory_all_unreachable_cb_event);
1136 /** This function is called whenever we successfully pull down some new
1137 * network statuses or server descriptors. */
1138 void
1139 directory_info_has_arrived(time_t now, int from_cache, int suppress_logs)
1141 const or_options_t *options = get_options();
1143 /* if we have enough dir info, then update our guard status with
1144 * whatever we just learned. */
1145 int invalidate_circs = guards_update_all();
1147 if (invalidate_circs) {
1148 circuit_mark_all_unused_circs();
1149 circuit_mark_all_dirty_circs_as_unusable();
1152 if (!router_have_minimum_dir_info()) {
1153 int quiet = suppress_logs || from_cache ||
1154 directory_too_idle_to_fetch_descriptors(options, now);
1155 tor_log(quiet ? LOG_INFO : LOG_NOTICE, LD_DIR,
1156 "I learned some more directory information, but not enough to "
1157 "build a circuit: %s", get_dir_info_status_string());
1158 update_all_descriptor_downloads(now);
1159 return;
1160 } else {
1161 if (directory_fetches_from_authorities(options)) {
1162 update_all_descriptor_downloads(now);
1165 /* Don't even bother trying to get extrainfo until the rest of our
1166 * directory info is up-to-date */
1167 if (options->DownloadExtraInfo)
1168 update_extrainfo_downloads(now);
1171 if (server_mode(options) && !net_is_disabled() && !from_cache &&
1172 (have_completed_a_circuit() || !any_predicted_circuits(now)))
1173 router_do_reachability_checks(1, 1);
1176 /** Perform regular maintenance tasks for a single connection. This
1177 * function gets run once per second per connection by run_scheduled_events.
1179 static void
1180 run_connection_housekeeping(int i, time_t now)
1182 cell_t cell;
1183 connection_t *conn = smartlist_get(connection_array, i);
1184 const or_options_t *options = get_options();
1185 or_connection_t *or_conn;
1186 channel_t *chan = NULL;
1187 int have_any_circuits;
1188 int past_keepalive =
1189 now >= conn->timestamp_last_write_allowed + options->KeepalivePeriod;
1191 if (conn->outbuf && !connection_get_outbuf_len(conn) &&
1192 conn->type == CONN_TYPE_OR)
1193 TO_OR_CONN(conn)->timestamp_lastempty = now;
1195 if (conn->marked_for_close) {
1196 /* nothing to do here */
1197 return;
1200 /* Expire any directory connections that haven't been active (sent
1201 * if a server or received if a client) for 5 min */
1202 if (conn->type == CONN_TYPE_DIR &&
1203 ((DIR_CONN_IS_SERVER(conn) &&
1204 conn->timestamp_last_write_allowed
1205 + options->TestingDirConnectionMaxStall < now) ||
1206 (!DIR_CONN_IS_SERVER(conn) &&
1207 conn->timestamp_last_read_allowed
1208 + options->TestingDirConnectionMaxStall < now))) {
1209 log_info(LD_DIR,"Expiring wedged directory conn (fd %d, purpose %d)",
1210 (int)conn->s, conn->purpose);
1211 /* This check is temporary; it's to let us know whether we should consider
1212 * parsing partial serverdesc responses. */
1213 if (conn->purpose == DIR_PURPOSE_FETCH_SERVERDESC &&
1214 connection_get_inbuf_len(conn) >= 1024) {
1215 log_info(LD_DIR,"Trying to extract information from wedged server desc "
1216 "download.");
1217 connection_dir_reached_eof(TO_DIR_CONN(conn));
1218 } else {
1219 connection_mark_for_close(conn);
1221 return;
1224 if (!connection_speaks_cells(conn))
1225 return; /* we're all done here, the rest is just for OR conns */
1227 /* If we haven't flushed to an OR connection for a while, then either nuke
1228 the connection or send a keepalive, depending. */
1230 or_conn = TO_OR_CONN(conn);
1231 tor_assert(conn->outbuf);
1233 chan = TLS_CHAN_TO_BASE(or_conn->chan);
1234 tor_assert(chan);
1236 if (channel_num_circuits(chan) != 0) {
1237 have_any_circuits = 1;
1238 chan->timestamp_last_had_circuits = now;
1239 } else {
1240 have_any_circuits = 0;
1243 if (channel_is_bad_for_new_circs(TLS_CHAN_TO_BASE(or_conn->chan)) &&
1244 ! have_any_circuits) {
1245 /* It's bad for new circuits, and has no unmarked circuits on it:
1246 * mark it now. */
1247 log_info(LD_OR,
1248 "Expiring non-used OR connection to fd %d (%s:%d) [Too old].",
1249 (int)conn->s, conn->address, conn->port);
1250 if (conn->state == OR_CONN_STATE_CONNECTING)
1251 connection_or_connect_failed(TO_OR_CONN(conn),
1252 END_OR_CONN_REASON_TIMEOUT,
1253 "Tor gave up on the connection");
1254 connection_or_close_normally(TO_OR_CONN(conn), 1);
1255 } else if (!connection_state_is_open(conn)) {
1256 if (past_keepalive) {
1257 /* We never managed to actually get this connection open and happy. */
1258 log_info(LD_OR,"Expiring non-open OR connection to fd %d (%s:%d).",
1259 (int)conn->s,conn->address, conn->port);
1260 connection_or_close_normally(TO_OR_CONN(conn), 0);
1262 } else if (we_are_hibernating() &&
1263 ! have_any_circuits &&
1264 !connection_get_outbuf_len(conn)) {
1265 /* We're hibernating or shutting down, there's no circuits, and nothing to
1266 * flush.*/
1267 log_info(LD_OR,"Expiring non-used OR connection to fd %d (%s:%d) "
1268 "[Hibernating or exiting].",
1269 (int)conn->s,conn->address, conn->port);
1270 connection_or_close_normally(TO_OR_CONN(conn), 1);
1271 } else if (!have_any_circuits &&
1272 now - or_conn->idle_timeout >=
1273 chan->timestamp_last_had_circuits) {
1274 log_info(LD_OR,"Expiring non-used OR connection "U64_FORMAT" to fd %d "
1275 "(%s:%d) [no circuits for %d; timeout %d; %scanonical].",
1276 U64_PRINTF_ARG(chan->global_identifier),
1277 (int)conn->s, conn->address, conn->port,
1278 (int)(now - chan->timestamp_last_had_circuits),
1279 or_conn->idle_timeout,
1280 or_conn->is_canonical ? "" : "non");
1281 connection_or_close_normally(TO_OR_CONN(conn), 0);
1282 } else if (
1283 now >= or_conn->timestamp_lastempty + options->KeepalivePeriod*10 &&
1284 now >=
1285 conn->timestamp_last_write_allowed + options->KeepalivePeriod*10) {
1286 log_fn(LOG_PROTOCOL_WARN,LD_PROTOCOL,
1287 "Expiring stuck OR connection to fd %d (%s:%d). (%d bytes to "
1288 "flush; %d seconds since last write)",
1289 (int)conn->s, conn->address, conn->port,
1290 (int)connection_get_outbuf_len(conn),
1291 (int)(now-conn->timestamp_last_write_allowed));
1292 connection_or_close_normally(TO_OR_CONN(conn), 0);
1293 } else if (past_keepalive && !connection_get_outbuf_len(conn)) {
1294 /* send a padding cell */
1295 log_fn(LOG_DEBUG,LD_OR,"Sending keepalive to (%s:%d)",
1296 conn->address, conn->port);
1297 memset(&cell,0,sizeof(cell_t));
1298 cell.command = CELL_PADDING;
1299 connection_or_write_cell_to_buf(&cell, or_conn);
1300 } else {
1301 channelpadding_decide_to_pad_channel(chan);
1305 /** Honor a NEWNYM request: make future requests unlinkable to past
1306 * requests. */
1307 static void
1308 signewnym_impl(time_t now)
1310 const or_options_t *options = get_options();
1311 if (!proxy_mode(options)) {
1312 log_info(LD_CONTROL, "Ignoring SIGNAL NEWNYM because client functionality "
1313 "is disabled.");
1314 return;
1317 circuit_mark_all_dirty_circs_as_unusable();
1318 addressmap_clear_transient();
1319 hs_client_purge_state();
1320 time_of_last_signewnym = now;
1321 signewnym_is_pending = 0;
1323 ++newnym_epoch;
1325 control_event_signal(SIGNEWNYM);
1328 /** Callback: run a deferred signewnym. */
1329 static void
1330 handle_deferred_signewnym_cb(mainloop_event_t *event, void *arg)
1332 (void)event;
1333 (void)arg;
1334 log_info(LD_CONTROL, "Honoring delayed NEWNYM request");
1335 signewnym_impl(time(NULL));
1338 /** Return the number of times that signewnym has been called. */
1339 unsigned
1340 get_signewnym_epoch(void)
1342 return newnym_epoch;
1345 /** True iff we have initialized all the members of <b>periodic_events</b>.
1346 * Used to prevent double-initialization. */
1347 static int periodic_events_initialized = 0;
1349 /* Declare all the timer callback functions... */
1350 #undef CALLBACK
1351 #define CALLBACK(name) \
1352 static int name ## _callback(time_t, const or_options_t *)
1353 CALLBACK(add_entropy);
1354 CALLBACK(check_authority_cert);
1355 CALLBACK(check_canonical_channels);
1356 CALLBACK(check_descriptor);
1357 CALLBACK(check_dns_honesty);
1358 CALLBACK(check_ed_keys);
1359 CALLBACK(check_expired_networkstatus);
1360 CALLBACK(check_for_reachability_bw);
1361 CALLBACK(check_onion_keys_expiry_time);
1362 CALLBACK(clean_caches);
1363 CALLBACK(clean_consdiffmgr);
1364 CALLBACK(dirvote);
1365 CALLBACK(downrate_stability);
1366 CALLBACK(expire_old_ciruits_serverside);
1367 CALLBACK(fetch_networkstatus);
1368 CALLBACK(heartbeat);
1369 CALLBACK(hs_service);
1370 CALLBACK(launch_descriptor_fetches);
1371 CALLBACK(launch_reachability_tests);
1372 CALLBACK(reachability_warnings);
1373 CALLBACK(record_bridge_stats);
1374 CALLBACK(rend_cache_failure_clean);
1375 CALLBACK(reset_padding_counts);
1376 CALLBACK(retry_dns);
1377 CALLBACK(retry_listeners);
1378 CALLBACK(rotate_onion_key);
1379 CALLBACK(rotate_x509_certificate);
1380 CALLBACK(save_stability);
1381 CALLBACK(save_state);
1382 CALLBACK(write_bridge_ns);
1383 CALLBACK(write_stats_file);
1385 #undef CALLBACK
1387 /* Now we declare an array of periodic_event_item_t for each periodic event */
1388 #define CALLBACK(name, r, f) PERIODIC_EVENT(name, r, f)
1390 STATIC periodic_event_item_t periodic_events[] = {
1391 /* Everyone needs to run those. */
1392 CALLBACK(add_entropy, PERIODIC_EVENT_ROLE_ALL, 0),
1393 CALLBACK(check_expired_networkstatus, PERIODIC_EVENT_ROLE_ALL, 0),
1394 CALLBACK(clean_caches, PERIODIC_EVENT_ROLE_ALL, 0),
1395 CALLBACK(fetch_networkstatus, PERIODIC_EVENT_ROLE_ALL,
1396 PERIODIC_EVENT_FLAG_NEED_NET),
1397 CALLBACK(heartbeat, PERIODIC_EVENT_ROLE_ALL, 0),
1398 CALLBACK(launch_descriptor_fetches, PERIODIC_EVENT_ROLE_ALL,
1399 PERIODIC_EVENT_FLAG_NEED_NET),
1400 CALLBACK(reset_padding_counts, PERIODIC_EVENT_ROLE_ALL, 0),
1401 CALLBACK(retry_listeners, PERIODIC_EVENT_ROLE_ALL,
1402 PERIODIC_EVENT_FLAG_NEED_NET),
1403 CALLBACK(save_state, PERIODIC_EVENT_ROLE_ALL, 0),
1404 CALLBACK(rotate_x509_certificate, PERIODIC_EVENT_ROLE_ALL, 0),
1405 CALLBACK(write_stats_file, PERIODIC_EVENT_ROLE_ALL, 0),
1407 /* Routers (bridge and relay) only. */
1408 CALLBACK(check_descriptor, PERIODIC_EVENT_ROLE_ROUTER,
1409 PERIODIC_EVENT_FLAG_NEED_NET),
1410 CALLBACK(check_ed_keys, PERIODIC_EVENT_ROLE_ROUTER, 0),
1411 CALLBACK(check_for_reachability_bw, PERIODIC_EVENT_ROLE_ROUTER,
1412 PERIODIC_EVENT_FLAG_NEED_NET),
1413 CALLBACK(check_onion_keys_expiry_time, PERIODIC_EVENT_ROLE_ROUTER, 0),
1414 CALLBACK(expire_old_ciruits_serverside, PERIODIC_EVENT_ROLE_ROUTER,
1415 PERIODIC_EVENT_FLAG_NEED_NET),
1416 CALLBACK(reachability_warnings, PERIODIC_EVENT_ROLE_ROUTER,
1417 PERIODIC_EVENT_FLAG_NEED_NET),
1418 CALLBACK(retry_dns, PERIODIC_EVENT_ROLE_ROUTER, 0),
1419 CALLBACK(rotate_onion_key, PERIODIC_EVENT_ROLE_ROUTER, 0),
1421 /* Authorities (bridge and directory) only. */
1422 CALLBACK(downrate_stability, PERIODIC_EVENT_ROLE_AUTHORITIES, 0),
1423 CALLBACK(launch_reachability_tests, PERIODIC_EVENT_ROLE_AUTHORITIES,
1424 PERIODIC_EVENT_FLAG_NEED_NET),
1425 CALLBACK(save_stability, PERIODIC_EVENT_ROLE_AUTHORITIES, 0),
1427 /* Directory authority only. */
1428 CALLBACK(check_authority_cert, PERIODIC_EVENT_ROLE_DIRAUTH, 0),
1429 CALLBACK(dirvote, PERIODIC_EVENT_ROLE_DIRAUTH, PERIODIC_EVENT_FLAG_NEED_NET),
1431 /* Relay only. */
1432 CALLBACK(check_canonical_channels, PERIODIC_EVENT_ROLE_RELAY,
1433 PERIODIC_EVENT_FLAG_NEED_NET),
1434 CALLBACK(check_dns_honesty, PERIODIC_EVENT_ROLE_RELAY,
1435 PERIODIC_EVENT_FLAG_NEED_NET),
1437 /* Hidden Service service only. */
1438 CALLBACK(hs_service, PERIODIC_EVENT_ROLE_HS_SERVICE,
1439 PERIODIC_EVENT_FLAG_NEED_NET),
1441 /* Bridge only. */
1442 CALLBACK(record_bridge_stats, PERIODIC_EVENT_ROLE_BRIDGE, 0),
1444 /* Client only. */
1445 CALLBACK(rend_cache_failure_clean, PERIODIC_EVENT_ROLE_CLIENT, 0),
1447 /* Bridge Authority only. */
1448 CALLBACK(write_bridge_ns, PERIODIC_EVENT_ROLE_BRIDGEAUTH, 0),
1450 /* Directory server only. */
1451 CALLBACK(clean_consdiffmgr, PERIODIC_EVENT_ROLE_DIRSERVER, 0),
1453 END_OF_PERIODIC_EVENTS
1455 #undef CALLBACK
1457 /* These are pointers to members of periodic_events[] that are used to
1458 * implement particular callbacks. We keep them separate here so that we
1459 * can access them by name. We also keep them inside periodic_events[]
1460 * so that we can implement "reset all timers" in a reasonable way. */
1461 static periodic_event_item_t *check_descriptor_event=NULL;
1462 static periodic_event_item_t *dirvote_event=NULL;
1463 static periodic_event_item_t *fetch_networkstatus_event=NULL;
1464 static periodic_event_item_t *launch_descriptor_fetches_event=NULL;
1465 static periodic_event_item_t *check_dns_honesty_event=NULL;
1466 static periodic_event_item_t *save_state_event=NULL;
1468 /** Reset all the periodic events so we'll do all our actions again as if we
1469 * just started up.
1470 * Useful if our clock just moved back a long time from the future,
1471 * so we don't wait until that future arrives again before acting.
1473 void
1474 reset_all_main_loop_timers(void)
1476 int i;
1477 for (i = 0; periodic_events[i].name; ++i) {
1478 periodic_event_reschedule(&periodic_events[i]);
1482 /** Return the member of periodic_events[] whose name is <b>name</b>.
1483 * Return NULL if no such event is found.
1485 static periodic_event_item_t *
1486 find_periodic_event(const char *name)
1488 int i;
1489 for (i = 0; periodic_events[i].name; ++i) {
1490 if (strcmp(name, periodic_events[i].name) == 0)
1491 return &periodic_events[i];
1493 return NULL;
1496 /** Return a bitmask of the roles this tor instance is configured for using
1497 * the given options. */
1498 STATIC int
1499 get_my_roles(const or_options_t *options)
1501 tor_assert(options);
1503 int roles = 0;
1504 int is_bridge = options->BridgeRelay;
1505 int is_client = options_any_client_port_set(options);
1506 int is_relay = server_mode(options);
1507 int is_dirauth = authdir_mode_v3(options);
1508 int is_bridgeauth = authdir_mode_bridge(options);
1509 int is_hidden_service = !!hs_service_get_num_services() ||
1510 !!rend_num_services();
1511 int is_dirserver = dir_server_mode(options);
1513 if (is_bridge) roles |= PERIODIC_EVENT_ROLE_BRIDGE;
1514 if (is_client) roles |= PERIODIC_EVENT_ROLE_CLIENT;
1515 if (is_relay) roles |= PERIODIC_EVENT_ROLE_RELAY;
1516 if (is_dirauth) roles |= PERIODIC_EVENT_ROLE_DIRAUTH;
1517 if (is_bridgeauth) roles |= PERIODIC_EVENT_ROLE_BRIDGEAUTH;
1518 if (is_hidden_service) roles |= PERIODIC_EVENT_ROLE_HS_SERVICE;
1519 if (is_dirserver) roles |= PERIODIC_EVENT_ROLE_DIRSERVER;
1521 return roles;
1524 /** Event to run initialize_periodic_events_cb */
1525 static struct event *initialize_periodic_events_event = NULL;
1527 /** Helper, run one second after setup:
1528 * Initializes all members of periodic_events and starts them running.
1530 * (We do this one second after setup for backward-compatibility reasons;
1531 * it might not actually be necessary.) */
1532 static void
1533 initialize_periodic_events_cb(evutil_socket_t fd, short events, void *data)
1535 (void) fd;
1536 (void) events;
1537 (void) data;
1539 tor_event_free(initialize_periodic_events_event);
1541 rescan_periodic_events(get_options());
1544 /** Set up all the members of periodic_events[], and configure them all to be
1545 * launched from a callback. */
1546 STATIC void
1547 initialize_periodic_events(void)
1549 tor_assert(periodic_events_initialized == 0);
1550 periodic_events_initialized = 1;
1552 /* Set up all periodic events. We'll launch them by roles. */
1553 int i;
1554 for (i = 0; periodic_events[i].name; ++i) {
1555 periodic_event_setup(&periodic_events[i]);
1558 #define NAMED_CALLBACK(name) \
1559 STMT_BEGIN name ## _event = find_periodic_event( #name ); STMT_END
1561 NAMED_CALLBACK(check_descriptor);
1562 NAMED_CALLBACK(dirvote);
1563 NAMED_CALLBACK(fetch_networkstatus);
1564 NAMED_CALLBACK(launch_descriptor_fetches);
1565 NAMED_CALLBACK(check_dns_honesty);
1566 NAMED_CALLBACK(save_state);
1568 struct timeval one_second = { 1, 0 };
1569 initialize_periodic_events_event = tor_evtimer_new(
1570 tor_libevent_get_base(),
1571 initialize_periodic_events_cb, NULL);
1572 event_add(initialize_periodic_events_event, &one_second);
1575 STATIC void
1576 teardown_periodic_events(void)
1578 int i;
1579 for (i = 0; periodic_events[i].name; ++i) {
1580 periodic_event_destroy(&periodic_events[i]);
1582 periodic_events_initialized = 0;
1585 /** Do a pass at all our periodic events, disable those we don't need anymore
1586 * and enable those we need now using the given options. */
1587 void
1588 rescan_periodic_events(const or_options_t *options)
1590 tor_assert(options);
1592 /* Avoid scanning the event list if we haven't initialized it yet. This is
1593 * particularly useful for unit tests in order to avoid initializing main
1594 * loop events everytime. */
1595 if (!periodic_events_initialized) {
1596 return;
1599 int roles = get_my_roles(options);
1601 for (int i = 0; periodic_events[i].name; ++i) {
1602 periodic_event_item_t *item = &periodic_events[i];
1604 /* Handle the event flags. */
1605 if (net_is_disabled() &&
1606 (item->flags & PERIODIC_EVENT_FLAG_NEED_NET)) {
1607 continue;
1610 /* Enable the event if needed. It is safe to enable an event that was
1611 * already enabled. Same goes for disabling it. */
1612 if (item->roles & roles) {
1613 log_debug(LD_GENERAL, "Launching periodic event %s", item->name);
1614 periodic_event_enable(item);
1615 } else {
1616 log_debug(LD_GENERAL, "Disabling periodic event %s", item->name);
1617 periodic_event_disable(item);
1622 /* We just got new options globally set, see if we need to enabled or disable
1623 * periodic events. */
1624 void
1625 periodic_events_on_new_options(const or_options_t *options)
1627 /* Only if we've already initialized the events, rescan the list which will
1628 * enable or disable events depending on our roles. This will be called at
1629 * bootup and we don't want this function to initialize the events because
1630 * they aren't set up at this stage. */
1631 if (periodic_events_initialized) {
1632 rescan_periodic_events(options);
1637 * Update our schedule so that we'll check whether we need to update our
1638 * descriptor immediately, rather than after up to CHECK_DESCRIPTOR_INTERVAL
1639 * seconds.
1641 void
1642 reschedule_descriptor_update_check(void)
1644 if (check_descriptor_event) {
1645 periodic_event_reschedule(check_descriptor_event);
1650 * Update our schedule so that we'll check whether we need to fetch directory
1651 * info immediately.
1653 void
1654 reschedule_directory_downloads(void)
1656 tor_assert(fetch_networkstatus_event);
1657 tor_assert(launch_descriptor_fetches_event);
1659 periodic_event_reschedule(fetch_networkstatus_event);
1660 periodic_event_reschedule(launch_descriptor_fetches_event);
1663 /** Mainloop callback: clean up circuits, channels, and connections
1664 * that are pending close. */
1665 static void
1666 postloop_cleanup_cb(mainloop_event_t *ev, void *arg)
1668 (void)ev;
1669 (void)arg;
1670 circuit_close_all_marked();
1671 close_closeable_connections();
1672 channel_run_cleanup();
1673 channel_listener_run_cleanup();
1676 /** Event to run postloop_cleanup_cb */
1677 static mainloop_event_t *postloop_cleanup_ev=NULL;
1679 /** Schedule a post-loop event to clean up marked channels, connections, and
1680 * circuits. */
1681 void
1682 mainloop_schedule_postloop_cleanup(void)
1684 if (PREDICT_UNLIKELY(postloop_cleanup_ev == NULL)) {
1685 // (It's possible that we can get here if we decide to close a connection
1686 // in the earliest stages of our configuration, before we create events.)
1687 return;
1689 mainloop_event_activate(postloop_cleanup_ev);
1692 #define LONGEST_TIMER_PERIOD (30 * 86400)
1693 /** Helper: Return the number of seconds between <b>now</b> and <b>next</b>,
1694 * clipped to the range [1 second, LONGEST_TIMER_PERIOD]. */
1695 static inline int
1696 safe_timer_diff(time_t now, time_t next)
1698 if (next > now) {
1699 /* There were no computers at signed TIME_MIN (1902 on 32-bit systems),
1700 * and nothing that could run Tor. It's a bug if 'next' is around then.
1701 * On 64-bit systems with signed TIME_MIN, TIME_MIN is before the Big
1702 * Bang. We cannot extrapolate past a singularity, but there was probably
1703 * nothing that could run Tor then, either.
1705 tor_assert(next > TIME_MIN + LONGEST_TIMER_PERIOD);
1707 if (next - LONGEST_TIMER_PERIOD > now)
1708 return LONGEST_TIMER_PERIOD;
1709 return (int)(next - now);
1710 } else {
1711 return 1;
1715 /** Perform regular maintenance tasks. This function gets run once per
1716 * second by second_elapsed_callback().
1718 static void
1719 run_scheduled_events(time_t now)
1721 const or_options_t *options = get_options();
1723 /* 0. See if we've been asked to shut down and our timeout has
1724 * expired; or if our bandwidth limits are exhausted and we
1725 * should hibernate; or if it's time to wake up from hibernation.
1727 consider_hibernation(now);
1729 /* Maybe enough time elapsed for us to reconsider a circuit. */
1730 circuit_upgrade_circuits_from_guard_wait();
1732 if (options->UseBridges && !net_is_disabled()) {
1733 /* Note: this check uses net_is_disabled(), not should_delay_dir_fetches()
1734 * -- the latter is only for fetching consensus-derived directory info. */
1735 fetch_bridge_descriptors(options, now);
1738 if (accounting_is_enabled(options)) {
1739 accounting_run_housekeeping(now);
1742 /* 3a. Every second, we examine pending circuits and prune the
1743 * ones which have been pending for more than a few seconds.
1744 * We do this before step 4, so it can try building more if
1745 * it's not comfortable with the number of available circuits.
1747 /* (If our circuit build timeout can ever become lower than a second (which
1748 * it can't, currently), we should do this more often.) */
1749 circuit_expire_building();
1750 circuit_expire_waiting_for_better_guard();
1752 /* 3b. Also look at pending streams and prune the ones that 'began'
1753 * a long time ago but haven't gotten a 'connected' yet.
1754 * Do this before step 4, so we can put them back into pending
1755 * state to be picked up by the new circuit.
1757 connection_ap_expire_beginning();
1759 /* 3c. And expire connections that we've held open for too long.
1761 connection_expire_held_open();
1763 /* 4. Every second, we try a new circuit if there are no valid
1764 * circuits. Every NewCircuitPeriod seconds, we expire circuits
1765 * that became dirty more than MaxCircuitDirtiness seconds ago,
1766 * and we make a new circ if there are no clean circuits.
1768 const int have_dir_info = router_have_minimum_dir_info();
1769 if (have_dir_info && !net_is_disabled()) {
1770 circuit_build_needed_circs(now);
1771 } else {
1772 circuit_expire_old_circs_as_needed(now);
1775 /* 5. We do housekeeping for each connection... */
1776 channel_update_bad_for_new_circs(NULL, 0);
1777 int i;
1778 for (i=0;i<smartlist_len(connection_array);i++) {
1779 run_connection_housekeeping(i, now);
1782 /* 11b. check pending unconfigured managed proxies */
1783 if (!net_is_disabled() && pt_proxies_configuration_pending())
1784 pt_configure_remaining_proxies();
1787 /* Periodic callback: rotate the onion keys after the period defined by the
1788 * "onion-key-rotation-days" consensus parameter, shut down and restart all
1789 * cpuworkers, and update our descriptor if necessary.
1791 static int
1792 rotate_onion_key_callback(time_t now, const or_options_t *options)
1794 if (server_mode(options)) {
1795 int onion_key_lifetime = get_onion_key_lifetime();
1796 time_t rotation_time = get_onion_key_set_at()+onion_key_lifetime;
1797 if (rotation_time > now) {
1798 return ONION_KEY_CONSENSUS_CHECK_INTERVAL;
1801 log_info(LD_GENERAL,"Rotating onion key.");
1802 rotate_onion_key();
1803 cpuworkers_rotate_keyinfo();
1804 if (router_rebuild_descriptor(1)<0) {
1805 log_info(LD_CONFIG, "Couldn't rebuild router descriptor");
1807 if (advertised_server_mode() && !net_is_disabled())
1808 router_upload_dir_desc_to_dirservers(0);
1809 return ONION_KEY_CONSENSUS_CHECK_INTERVAL;
1811 return PERIODIC_EVENT_NO_UPDATE;
1814 /* Period callback: Check if our old onion keys are still valid after the
1815 * period of time defined by the consensus parameter
1816 * "onion-key-grace-period-days", otherwise expire them by setting them to
1817 * NULL.
1819 static int
1820 check_onion_keys_expiry_time_callback(time_t now, const or_options_t *options)
1822 if (server_mode(options)) {
1823 int onion_key_grace_period = get_onion_key_grace_period();
1824 time_t expiry_time = get_onion_key_set_at()+onion_key_grace_period;
1825 if (expiry_time > now) {
1826 return ONION_KEY_CONSENSUS_CHECK_INTERVAL;
1829 log_info(LD_GENERAL, "Expiring old onion keys.");
1830 expire_old_onion_keys();
1831 cpuworkers_rotate_keyinfo();
1832 return ONION_KEY_CONSENSUS_CHECK_INTERVAL;
1835 return PERIODIC_EVENT_NO_UPDATE;
1838 /* Periodic callback: Every 30 seconds, check whether it's time to make new
1839 * Ed25519 subkeys.
1841 static int
1842 check_ed_keys_callback(time_t now, const or_options_t *options)
1844 if (server_mode(options)) {
1845 if (should_make_new_ed_keys(options, now)) {
1846 int new_signing_key = load_ed_keys(options, now);
1847 if (new_signing_key < 0 ||
1848 generate_ed_link_cert(options, now, new_signing_key > 0)) {
1849 log_err(LD_OR, "Unable to update Ed25519 keys! Exiting.");
1850 tor_shutdown_event_loop_and_exit(1);
1853 return 30;
1855 return PERIODIC_EVENT_NO_UPDATE;
1859 * Periodic callback: Every {LAZY,GREEDY}_DESCRIPTOR_RETRY_INTERVAL,
1860 * see about fetching descriptors, microdescriptors, and extrainfo
1861 * documents.
1863 static int
1864 launch_descriptor_fetches_callback(time_t now, const or_options_t *options)
1866 if (should_delay_dir_fetches(options, NULL))
1867 return PERIODIC_EVENT_NO_UPDATE;
1869 update_all_descriptor_downloads(now);
1870 update_extrainfo_downloads(now);
1871 if (router_have_minimum_dir_info())
1872 return LAZY_DESCRIPTOR_RETRY_INTERVAL;
1873 else
1874 return GREEDY_DESCRIPTOR_RETRY_INTERVAL;
1878 * Periodic event: Rotate our X.509 certificates and TLS keys once every
1879 * MAX_SSL_KEY_LIFETIME_INTERNAL.
1881 static int
1882 rotate_x509_certificate_callback(time_t now, const or_options_t *options)
1884 static int first = 1;
1885 (void)now;
1886 (void)options;
1887 if (first) {
1888 first = 0;
1889 return MAX_SSL_KEY_LIFETIME_INTERNAL;
1892 /* 1b. Every MAX_SSL_KEY_LIFETIME_INTERNAL seconds, we change our
1893 * TLS context. */
1894 log_info(LD_GENERAL,"Rotating tls context.");
1895 if (router_initialize_tls_context() < 0) {
1896 log_err(LD_BUG, "Error reinitializing TLS context");
1897 tor_assert_unreached();
1899 if (generate_ed_link_cert(options, now, 1)) {
1900 log_err(LD_OR, "Unable to update Ed25519->TLS link certificate for "
1901 "new TLS context.");
1902 tor_assert_unreached();
1905 /* We also make sure to rotate the TLS connections themselves if they've
1906 * been up for too long -- but that's done via is_bad_for_new_circs in
1907 * run_connection_housekeeping() above. */
1908 return MAX_SSL_KEY_LIFETIME_INTERNAL;
1912 * Periodic callback: once an hour, grab some more entropy from the
1913 * kernel and feed it to our CSPRNG.
1915 static int
1916 add_entropy_callback(time_t now, const or_options_t *options)
1918 (void)now;
1919 (void)options;
1920 /* We already seeded once, so don't die on failure. */
1921 if (crypto_seed_rng() < 0) {
1922 log_warn(LD_GENERAL, "Tried to re-seed RNG, but failed. We already "
1923 "seeded once, though, so we won't exit here.");
1926 /** How often do we add more entropy to OpenSSL's RNG pool? */
1927 #define ENTROPY_INTERVAL (60*60)
1928 return ENTROPY_INTERVAL;
1932 * Periodic callback: if we're an authority, make sure we test
1933 * the routers on the network for reachability.
1935 static int
1936 launch_reachability_tests_callback(time_t now, const or_options_t *options)
1938 if (authdir_mode_tests_reachability(options) &&
1939 !net_is_disabled()) {
1940 /* try to determine reachability of the other Tor relays */
1941 dirserv_test_reachability(now);
1943 return REACHABILITY_TEST_INTERVAL;
1947 * Periodic callback: if we're an authority, discount the stability
1948 * information (and other rephist information) that's older.
1950 static int
1951 downrate_stability_callback(time_t now, const or_options_t *options)
1953 (void)options;
1954 /* 1d. Periodically, we discount older stability information so that new
1955 * stability info counts more, and save the stability information to disk as
1956 * appropriate. */
1957 time_t next = rep_hist_downrate_old_runs(now);
1958 return safe_timer_diff(now, next);
1962 * Periodic callback: if we're an authority, record our measured stability
1963 * information from rephist in an mtbf file.
1965 static int
1966 save_stability_callback(time_t now, const or_options_t *options)
1968 if (authdir_mode_tests_reachability(options)) {
1969 if (rep_hist_record_mtbf_data(now, 1)<0) {
1970 log_warn(LD_GENERAL, "Couldn't store mtbf data.");
1973 #define SAVE_STABILITY_INTERVAL (30*60)
1974 return SAVE_STABILITY_INTERVAL;
1978 * Periodic callback: if we're an authority, check on our authority
1979 * certificate (the one that authenticates our authority signing key).
1981 static int
1982 check_authority_cert_callback(time_t now, const or_options_t *options)
1984 (void)now;
1985 (void)options;
1986 /* 1e. Periodically, if we're a v3 authority, we check whether our cert is
1987 * close to expiring and warn the admin if it is. */
1988 v3_authority_check_key_expiry();
1989 #define CHECK_V3_CERTIFICATE_INTERVAL (5*60)
1990 return CHECK_V3_CERTIFICATE_INTERVAL;
1994 * Scheduled callback: Run directory-authority voting functionality.
1996 * The schedule is a bit complicated here, so dirvote_act() manages the
1997 * schedule itself.
1999 static int
2000 dirvote_callback(time_t now, const or_options_t *options)
2002 if (!authdir_mode_v3(options)) {
2003 tor_assert_nonfatal_unreached();
2004 return 3600;
2007 time_t next = dirvote_act(options, now);
2008 if (BUG(next == TIME_MAX)) {
2009 /* This shouldn't be returned unless we called dirvote_act() without
2010 * being an authority. If it happens, maybe our configuration will
2011 * fix itself in an hour or so? */
2012 return 3600;
2014 return safe_timer_diff(now, next);
2017 /** Reschedule the directory-authority voting event. Run this whenever the
2018 * schedule has changed. */
2019 void
2020 reschedule_dirvote(const or_options_t *options)
2022 if (periodic_events_initialized && authdir_mode_v3(options)) {
2023 periodic_event_reschedule(dirvote_event);
2028 * Periodic callback: If our consensus is too old, recalculate whether
2029 * we can actually use it.
2031 static int
2032 check_expired_networkstatus_callback(time_t now, const or_options_t *options)
2034 (void)options;
2035 /* Check whether our networkstatus has expired. */
2036 networkstatus_t *ns = networkstatus_get_latest_consensus();
2037 /*XXXX RD: This value needs to be the same as REASONABLY_LIVE_TIME in
2038 * networkstatus_get_reasonably_live_consensus(), but that value is way
2039 * way too high. Arma: is the bridge issue there resolved yet? -NM */
2040 #define NS_EXPIRY_SLOP (24*60*60)
2041 if (ns && ns->valid_until < (now - NS_EXPIRY_SLOP) &&
2042 router_have_minimum_dir_info()) {
2043 router_dir_info_changed();
2045 #define CHECK_EXPIRED_NS_INTERVAL (2*60)
2046 return CHECK_EXPIRED_NS_INTERVAL;
2050 * Scheduled callback: Save the state file to disk if appropriate.
2052 static int
2053 save_state_callback(time_t now, const or_options_t *options)
2055 (void) options;
2056 (void) or_state_save(now); // only saves if appropriate
2057 const time_t next_write = get_or_state()->next_write;
2058 if (next_write == TIME_MAX) {
2059 return 86400;
2061 return safe_timer_diff(now, next_write);
2064 /** Reschedule the event for saving the state file.
2066 * Run this when the state becomes dirty. */
2067 void
2068 reschedule_or_state_save(void)
2070 if (save_state_event == NULL) {
2071 /* This can happen early on during startup. */
2072 return;
2074 periodic_event_reschedule(save_state_event);
2078 * Periodic callback: Write statistics to disk if appropriate.
2080 static int
2081 write_stats_file_callback(time_t now, const or_options_t *options)
2083 /* 1g. Check whether we should write statistics to disk.
2085 #define CHECK_WRITE_STATS_INTERVAL (60*60)
2086 time_t next_time_to_write_stats_files = now + CHECK_WRITE_STATS_INTERVAL;
2087 if (options->CellStatistics) {
2088 time_t next_write =
2089 rep_hist_buffer_stats_write(now);
2090 if (next_write && next_write < next_time_to_write_stats_files)
2091 next_time_to_write_stats_files = next_write;
2093 if (options->DirReqStatistics) {
2094 time_t next_write = geoip_dirreq_stats_write(now);
2095 if (next_write && next_write < next_time_to_write_stats_files)
2096 next_time_to_write_stats_files = next_write;
2098 if (options->EntryStatistics) {
2099 time_t next_write = geoip_entry_stats_write(now);
2100 if (next_write && next_write < next_time_to_write_stats_files)
2101 next_time_to_write_stats_files = next_write;
2103 if (options->HiddenServiceStatistics) {
2104 time_t next_write = rep_hist_hs_stats_write(now);
2105 if (next_write && next_write < next_time_to_write_stats_files)
2106 next_time_to_write_stats_files = next_write;
2108 if (options->ExitPortStatistics) {
2109 time_t next_write = rep_hist_exit_stats_write(now);
2110 if (next_write && next_write < next_time_to_write_stats_files)
2111 next_time_to_write_stats_files = next_write;
2113 if (options->ConnDirectionStatistics) {
2114 time_t next_write = rep_hist_conn_stats_write(now);
2115 if (next_write && next_write < next_time_to_write_stats_files)
2116 next_time_to_write_stats_files = next_write;
2118 if (options->BridgeAuthoritativeDir) {
2119 time_t next_write = rep_hist_desc_stats_write(now);
2120 if (next_write && next_write < next_time_to_write_stats_files)
2121 next_time_to_write_stats_files = next_write;
2124 return safe_timer_diff(now, next_time_to_write_stats_files);
2127 #define CHANNEL_CHECK_INTERVAL (60*60)
2128 static int
2129 check_canonical_channels_callback(time_t now, const or_options_t *options)
2131 (void)now;
2132 if (public_server_mode(options))
2133 channel_check_for_duplicates();
2135 return CHANNEL_CHECK_INTERVAL;
2138 static int
2139 reset_padding_counts_callback(time_t now, const or_options_t *options)
2141 if (options->PaddingStatistics) {
2142 rep_hist_prep_published_padding_counts(now);
2145 rep_hist_reset_padding_counts();
2146 return REPHIST_CELL_PADDING_COUNTS_INTERVAL;
2149 static int should_init_bridge_stats = 1;
2152 * Periodic callback: Write bridge statistics to disk if appropriate.
2154 static int
2155 record_bridge_stats_callback(time_t now, const or_options_t *options)
2157 /* 1h. Check whether we should write bridge statistics to disk.
2159 if (should_record_bridge_info(options)) {
2160 if (should_init_bridge_stats) {
2161 /* (Re-)initialize bridge statistics. */
2162 geoip_bridge_stats_init(now);
2163 should_init_bridge_stats = 0;
2164 return WRITE_STATS_INTERVAL;
2165 } else {
2166 /* Possibly write bridge statistics to disk and ask when to write
2167 * them next time. */
2168 time_t next = geoip_bridge_stats_write(now);
2169 return safe_timer_diff(now, next);
2171 } else if (!should_init_bridge_stats) {
2172 /* Bridge mode was turned off. Ensure that stats are re-initialized
2173 * next time bridge mode is turned on. */
2174 should_init_bridge_stats = 1;
2176 return PERIODIC_EVENT_NO_UPDATE;
2180 * Periodic callback: Clean in-memory caches every once in a while
2182 static int
2183 clean_caches_callback(time_t now, const or_options_t *options)
2185 /* Remove old information from rephist and the rend cache. */
2186 rep_history_clean(now - options->RephistTrackTime);
2187 rend_cache_clean(now, REND_CACHE_TYPE_SERVICE);
2188 hs_cache_clean_as_client(now);
2189 hs_cache_clean_as_dir(now);
2190 microdesc_cache_rebuild(NULL, 0);
2191 #define CLEAN_CACHES_INTERVAL (30*60)
2192 return CLEAN_CACHES_INTERVAL;
2196 * Periodic callback: Clean the cache of failed hidden service lookups
2197 * frequently.
2199 static int
2200 rend_cache_failure_clean_callback(time_t now, const or_options_t *options)
2202 (void)options;
2203 /* We don't keep entries that are more than five minutes old so we try to
2204 * clean it as soon as we can since we want to make sure the client waits
2205 * as little as possible for reachability reasons. */
2206 rend_cache_failure_clean(now);
2207 hs_cache_client_intro_state_clean(now);
2208 return 30;
2212 * Periodic callback: If we're a server and initializing dns failed, retry.
2214 static int
2215 retry_dns_callback(time_t now, const or_options_t *options)
2217 (void)now;
2218 #define RETRY_DNS_INTERVAL (10*60)
2219 if (server_mode(options) && has_dns_init_failed())
2220 dns_init();
2221 return RETRY_DNS_INTERVAL;
2224 /** Periodic callback: consider rebuilding or and re-uploading our descriptor
2225 * (if we've passed our internal checks). */
2226 static int
2227 check_descriptor_callback(time_t now, const or_options_t *options)
2229 /** How often do we check whether part of our router info has changed in a
2230 * way that would require an upload? That includes checking whether our IP
2231 * address has changed. */
2232 #define CHECK_DESCRIPTOR_INTERVAL (60)
2234 (void)options;
2236 /* 2b. Once per minute, regenerate and upload the descriptor if the old
2237 * one is inaccurate. */
2238 if (!net_is_disabled()) {
2239 check_descriptor_bandwidth_changed(now);
2240 check_descriptor_ipaddress_changed(now);
2241 mark_my_descriptor_dirty_if_too_old(now);
2242 consider_publishable_server(0);
2243 /* If any networkstatus documents are no longer recent, we need to
2244 * update all the descriptors' running status. */
2245 /* Remove dead routers. */
2246 /* XXXX This doesn't belong here, but it was here in the pre-
2247 * XXXX refactoring code. */
2248 routerlist_remove_old_routers();
2251 return CHECK_DESCRIPTOR_INTERVAL;
2255 * Periodic callback: check whether we're reachable (as a relay), and
2256 * whether our bandwidth has changed enough that we need to
2257 * publish a new descriptor.
2259 static int
2260 check_for_reachability_bw_callback(time_t now, const or_options_t *options)
2262 /* XXXX This whole thing was stuck in the middle of what is now
2263 * XXXX check_descriptor_callback. I'm not sure it's right. */
2265 static int dirport_reachability_count = 0;
2266 /* also, check religiously for reachability, if it's within the first
2267 * 20 minutes of our uptime. */
2268 if (server_mode(options) &&
2269 (have_completed_a_circuit() || !any_predicted_circuits(now)) &&
2270 !net_is_disabled()) {
2271 if (get_uptime() < TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT) {
2272 router_do_reachability_checks(1, dirport_reachability_count==0);
2273 if (++dirport_reachability_count > 5)
2274 dirport_reachability_count = 0;
2275 return 1;
2276 } else {
2277 /* If we haven't checked for 12 hours and our bandwidth estimate is
2278 * low, do another bandwidth test. This is especially important for
2279 * bridges, since they might go long periods without much use. */
2280 const routerinfo_t *me = router_get_my_routerinfo();
2281 static int first_time = 1;
2282 if (!first_time && me &&
2283 me->bandwidthcapacity < me->bandwidthrate &&
2284 me->bandwidthcapacity < 51200) {
2285 reset_bandwidth_test();
2287 first_time = 0;
2288 #define BANDWIDTH_RECHECK_INTERVAL (12*60*60)
2289 return BANDWIDTH_RECHECK_INTERVAL;
2292 return CHECK_DESCRIPTOR_INTERVAL;
2296 * Periodic event: once a minute, (or every second if TestingTorNetwork, or
2297 * during client bootstrap), check whether we want to download any
2298 * networkstatus documents. */
2299 static int
2300 fetch_networkstatus_callback(time_t now, const or_options_t *options)
2302 /* How often do we check whether we should download network status
2303 * documents? */
2304 const int we_are_bootstrapping = networkstatus_consensus_is_bootstrapping(
2305 now);
2306 const int prefer_mirrors = !directory_fetches_from_authorities(
2307 get_options());
2308 int networkstatus_dl_check_interval = 60;
2309 /* check more often when testing, or when bootstrapping from mirrors
2310 * (connection limits prevent too many connections being made) */
2311 if (options->TestingTorNetwork
2312 || (we_are_bootstrapping && prefer_mirrors)) {
2313 networkstatus_dl_check_interval = 1;
2316 if (should_delay_dir_fetches(options, NULL))
2317 return PERIODIC_EVENT_NO_UPDATE;
2319 update_networkstatus_downloads(now);
2320 return networkstatus_dl_check_interval;
2324 * Periodic callback: Every 60 seconds, we relaunch listeners if any died. */
2325 static int
2326 retry_listeners_callback(time_t now, const or_options_t *options)
2328 (void)now;
2329 (void)options;
2330 if (!net_is_disabled()) {
2331 retry_all_listeners(NULL, NULL, 0);
2332 return 60;
2334 return PERIODIC_EVENT_NO_UPDATE;
2338 * Periodic callback: as a server, see if we have any old unused circuits
2339 * that should be expired */
2340 static int
2341 expire_old_ciruits_serverside_callback(time_t now, const or_options_t *options)
2343 (void)options;
2344 /* every 11 seconds, so not usually the same second as other such events */
2345 circuit_expire_old_circuits_serverside(now);
2346 return 11;
2350 * Callback: Send warnings if Tor doesn't find its ports reachable.
2352 static int
2353 reachability_warnings_callback(time_t now, const or_options_t *options)
2355 (void) now;
2357 if (get_uptime() < TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT) {
2358 return (int)(TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT - get_uptime());
2361 if (server_mode(options) &&
2362 !net_is_disabled() &&
2363 have_completed_a_circuit()) {
2364 /* every 20 minutes, check and complain if necessary */
2365 const routerinfo_t *me = router_get_my_routerinfo();
2366 if (me && !check_whether_orport_reachable(options)) {
2367 char *address = tor_dup_ip(me->addr);
2368 log_warn(LD_CONFIG,"Your server (%s:%d) has not managed to confirm that "
2369 "its ORPort is reachable. Relays do not publish descriptors "
2370 "until their ORPort and DirPort are reachable. Please check "
2371 "your firewalls, ports, address, /etc/hosts file, etc.",
2372 address, me->or_port);
2373 control_event_server_status(LOG_WARN,
2374 "REACHABILITY_FAILED ORADDRESS=%s:%d",
2375 address, me->or_port);
2376 tor_free(address);
2379 if (me && !check_whether_dirport_reachable(options)) {
2380 char *address = tor_dup_ip(me->addr);
2381 log_warn(LD_CONFIG,
2382 "Your server (%s:%d) has not managed to confirm that its "
2383 "DirPort is reachable. Relays do not publish descriptors "
2384 "until their ORPort and DirPort are reachable. Please check "
2385 "your firewalls, ports, address, /etc/hosts file, etc.",
2386 address, me->dir_port);
2387 control_event_server_status(LOG_WARN,
2388 "REACHABILITY_FAILED DIRADDRESS=%s:%d",
2389 address, me->dir_port);
2390 tor_free(address);
2394 return TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT;
2397 static int dns_honesty_first_time = 1;
2400 * Periodic event: if we're an exit, see if our DNS server is telling us
2401 * obvious lies.
2403 static int
2404 check_dns_honesty_callback(time_t now, const or_options_t *options)
2406 (void)now;
2407 /* 9. and if we're an exit node, check whether our DNS is telling stories
2408 * to us. */
2409 if (net_is_disabled() ||
2410 ! public_server_mode(options) ||
2411 router_my_exit_policy_is_reject_star())
2412 return PERIODIC_EVENT_NO_UPDATE;
2414 if (dns_honesty_first_time) {
2415 /* Don't launch right when we start */
2416 dns_honesty_first_time = 0;
2417 return crypto_rand_int_range(60, 180);
2420 dns_launch_correctness_checks();
2421 return 12*3600 + crypto_rand_int(12*3600);
2425 * Periodic callback: if we're the bridge authority, write a networkstatus
2426 * file to disk.
2428 static int
2429 write_bridge_ns_callback(time_t now, const or_options_t *options)
2431 /* 10. write bridge networkstatus file to disk */
2432 if (options->BridgeAuthoritativeDir) {
2433 networkstatus_dump_bridge_status_to_file(now);
2434 #define BRIDGE_STATUSFILE_INTERVAL (30*60)
2435 return BRIDGE_STATUSFILE_INTERVAL;
2437 return PERIODIC_EVENT_NO_UPDATE;
2440 static int heartbeat_callback_first_time = 1;
2443 * Periodic callback: write the heartbeat message in the logs.
2445 * If writing the heartbeat message to the logs fails for some reason, retry
2446 * again after <b>MIN_HEARTBEAT_PERIOD</b> seconds.
2448 static int
2449 heartbeat_callback(time_t now, const or_options_t *options)
2451 /* Check if heartbeat is disabled */
2452 if (!options->HeartbeatPeriod) {
2453 return PERIODIC_EVENT_NO_UPDATE;
2456 /* Skip the first one. */
2457 if (heartbeat_callback_first_time) {
2458 heartbeat_callback_first_time = 0;
2459 return options->HeartbeatPeriod;
2462 /* Write the heartbeat message */
2463 if (log_heartbeat(now) == 0) {
2464 return options->HeartbeatPeriod;
2465 } else {
2466 /* If we couldn't write the heartbeat log message, try again in the minimum
2467 * interval of time. */
2468 return MIN_HEARTBEAT_PERIOD;
2472 #define CDM_CLEAN_CALLBACK_INTERVAL 600
2473 static int
2474 clean_consdiffmgr_callback(time_t now, const or_options_t *options)
2476 (void)now;
2477 if (dir_server_mode(options)) {
2478 consdiffmgr_cleanup();
2480 return CDM_CLEAN_CALLBACK_INTERVAL;
2484 * Periodic callback: Run scheduled events for HS service. This is called
2485 * every second.
2487 static int
2488 hs_service_callback(time_t now, const or_options_t *options)
2490 (void) options;
2492 /* We need to at least be able to build circuits and that we actually have
2493 * a working network. */
2494 if (!have_completed_a_circuit() || net_is_disabled() ||
2495 networkstatus_get_live_consensus(now) == NULL) {
2496 goto end;
2499 hs_service_run_scheduled_events(now);
2501 end:
2502 /* Every 1 second. */
2503 return 1;
2506 /** Timer: used to invoke second_elapsed_callback() once per second. */
2507 static periodic_timer_t *second_timer = NULL;
2510 * Enable or disable the per-second timer as appropriate, creating it if
2511 * necessary.
2513 void
2514 reschedule_per_second_timer(void)
2516 struct timeval one_second;
2517 one_second.tv_sec = 1;
2518 one_second.tv_usec = 0;
2520 if (! second_timer) {
2521 second_timer = periodic_timer_new(tor_libevent_get_base(),
2522 &one_second,
2523 second_elapsed_callback,
2524 NULL);
2525 tor_assert(second_timer);
2528 const bool run_per_second_events =
2529 control_any_per_second_event_enabled() || ! net_is_completely_disabled();
2531 if (run_per_second_events) {
2532 periodic_timer_launch(second_timer, &one_second);
2533 } else {
2534 periodic_timer_disable(second_timer);
2538 /** Last time that update_current_time was called. */
2539 static time_t current_second = 0;
2540 /** Last time that update_current_time updated current_second. */
2541 static monotime_coarse_t current_second_last_changed;
2544 * Set the current time to "now", which should be the value returned by
2545 * time(). Check for clock jumps and track the total number of seconds we
2546 * have been running.
2548 void
2549 update_current_time(time_t now)
2551 if (PREDICT_LIKELY(now == current_second)) {
2552 /* We call this function a lot. Most frequently, the current second
2553 * will not have changed, so we just return. */
2554 return;
2557 const time_t seconds_elapsed = current_second ? (now - current_second) : 0;
2559 /* Check the wall clock against the monotonic clock, so we can
2560 * better tell idleness from clock jumps and/or other shenanigans. */
2561 monotime_coarse_t last_updated;
2562 memcpy(&last_updated, &current_second_last_changed, sizeof(last_updated));
2563 monotime_coarse_get(&current_second_last_changed);
2565 /** How much clock jumping do we tolerate? */
2566 #define NUM_JUMPED_SECONDS_BEFORE_WARN 100
2568 /** How much idleness do we tolerate? */
2569 #define NUM_IDLE_SECONDS_BEFORE_WARN 3600
2571 if (seconds_elapsed < -NUM_JUMPED_SECONDS_BEFORE_WARN) {
2572 // moving back in time is always a bad sign.
2573 circuit_note_clock_jumped(seconds_elapsed, false);
2574 } else if (seconds_elapsed >= NUM_JUMPED_SECONDS_BEFORE_WARN) {
2575 /* Compare the monotonic clock to the result of time(). */
2576 const int32_t monotime_msec_passed =
2577 monotime_coarse_diff_msec32(&last_updated,
2578 &current_second_last_changed);
2579 const int monotime_sec_passed = monotime_msec_passed / 1000;
2580 const int discrepancy = monotime_sec_passed - (int)seconds_elapsed;
2581 /* If the monotonic clock deviates from time(NULL), we have a couple of
2582 * possibilities. On some systems, this means we have been suspended or
2583 * sleeping. Everywhere, it can mean that the wall-clock time has
2584 * been changed -- for example, with settimeofday().
2586 * On the other hand, if the monotonic time matches with the wall-clock
2587 * time, we've probably just been idle for a while, with no events firing.
2588 * we tolerate much more of that.
2590 const bool clock_jumped = abs(discrepancy) > 2;
2592 if (clock_jumped || seconds_elapsed >= NUM_IDLE_SECONDS_BEFORE_WARN) {
2593 circuit_note_clock_jumped(seconds_elapsed, ! clock_jumped);
2595 } else if (seconds_elapsed > 0) {
2596 stats_n_seconds_working += seconds_elapsed;
2599 update_approx_time(now);
2600 current_second = now;
2603 /** Libevent callback: invoked once every second. */
2604 static void
2605 second_elapsed_callback(periodic_timer_t *timer, void *arg)
2607 /* XXXX This could be sensibly refactored into multiple callbacks, and we
2608 * could use Libevent's timers for this rather than checking the current
2609 * time against a bunch of timeouts every second. */
2610 time_t now;
2611 (void)timer;
2612 (void)arg;
2614 now = time(NULL);
2616 /* We don't need to do this once-per-second any more: time-updating is
2617 * only in this callback _because it is a callback_. It should be fine
2618 * to disable this callback, and the time will still get updated.
2620 update_current_time(now);
2622 /* Maybe some controller events are ready to fire */
2623 control_per_second_events();
2625 run_scheduled_events(now);
2628 #ifdef HAVE_SYSTEMD_209
2629 static periodic_timer_t *systemd_watchdog_timer = NULL;
2631 /** Libevent callback: invoked to reset systemd watchdog. */
2632 static void
2633 systemd_watchdog_callback(periodic_timer_t *timer, void *arg)
2635 (void)timer;
2636 (void)arg;
2637 sd_notify(0, "WATCHDOG=1");
2639 #endif /* defined(HAVE_SYSTEMD_209) */
2641 #define UPTIME_CUTOFF_FOR_NEW_BANDWIDTH_TEST (6*60*60)
2643 /** Called when our IP address seems to have changed. <b>at_interface</b>
2644 * should be true if we detected a change in our interface, and false if we
2645 * detected a change in our published address. */
2646 void
2647 ip_address_changed(int at_interface)
2649 const or_options_t *options = get_options();
2650 int server = server_mode(options);
2651 int exit_reject_interfaces = (server && options->ExitRelay
2652 && options->ExitPolicyRejectLocalInterfaces);
2654 if (at_interface) {
2655 if (! server) {
2656 /* Okay, change our keys. */
2657 if (init_keys_client() < 0)
2658 log_warn(LD_GENERAL, "Unable to rotate keys after IP change!");
2660 } else {
2661 if (server) {
2662 if (get_uptime() > UPTIME_CUTOFF_FOR_NEW_BANDWIDTH_TEST)
2663 reset_bandwidth_test();
2664 reset_uptime();
2665 router_reset_reachability();
2669 /* Exit relays incorporate interface addresses in their exit policies when
2670 * ExitPolicyRejectLocalInterfaces is set */
2671 if (exit_reject_interfaces || (server && !at_interface)) {
2672 mark_my_descriptor_dirty("IP address changed");
2675 dns_servers_relaunch_checks();
2678 /** Forget what we've learned about the correctness of our DNS servers, and
2679 * start learning again. */
2680 void
2681 dns_servers_relaunch_checks(void)
2683 if (server_mode(get_options())) {
2684 dns_reset_correctness_checks();
2685 if (periodic_events_initialized) {
2686 tor_assert(check_dns_honesty_event);
2687 periodic_event_reschedule(check_dns_honesty_event);
2692 /** Called when we get a SIGHUP: reload configuration files and keys,
2693 * retry all connections, and so on. */
2694 static int
2695 do_hup(void)
2697 const or_options_t *options = get_options();
2699 log_notice(LD_GENERAL,"Received reload signal (hup). Reloading config and "
2700 "resetting internal state.");
2701 if (accounting_is_enabled(options))
2702 accounting_record_bandwidth_usage(time(NULL), get_or_state());
2704 router_reset_warnings();
2705 routerlist_reset_warnings();
2706 /* first, reload config variables, in case they've changed */
2707 if (options->ReloadTorrcOnSIGHUP) {
2708 /* no need to provide argc/v, they've been cached in init_from_config */
2709 int init_rv = options_init_from_torrc(0, NULL);
2710 if (init_rv < 0) {
2711 log_err(LD_CONFIG,"Reading config failed--see warnings above. "
2712 "For usage, try -h.");
2713 return -1;
2714 } else if (BUG(init_rv > 0)) {
2715 // LCOV_EXCL_START
2716 /* This should be impossible: the only "return 1" cases in
2717 * options_init_from_torrc are ones caused by command-line arguments;
2718 * but they can't change while Tor is running. */
2719 return -1;
2720 // LCOV_EXCL_STOP
2722 options = get_options(); /* they have changed now */
2723 /* Logs are only truncated the first time they are opened, but were
2724 probably intended to be cleaned up on signal. */
2725 if (options->TruncateLogFile)
2726 truncate_logs();
2727 } else {
2728 char *msg = NULL;
2729 log_notice(LD_GENERAL, "Not reloading config file: the controller told "
2730 "us not to.");
2731 /* Make stuff get rescanned, reloaded, etc. */
2732 if (set_options((or_options_t*)options, &msg) < 0) {
2733 if (!msg)
2734 msg = tor_strdup("Unknown error");
2735 log_warn(LD_GENERAL, "Unable to re-set previous options: %s", msg);
2736 tor_free(msg);
2739 if (authdir_mode(options)) {
2740 /* reload the approved-routers file */
2741 if (dirserv_load_fingerprint_file() < 0) {
2742 /* warnings are logged from dirserv_load_fingerprint_file() directly */
2743 log_info(LD_GENERAL, "Error reloading fingerprints. "
2744 "Continuing with old list.");
2748 /* Rotate away from the old dirty circuits. This has to be done
2749 * after we've read the new options, but before we start using
2750 * circuits for directory fetches. */
2751 circuit_mark_all_dirty_circs_as_unusable();
2753 /* retry appropriate downloads */
2754 router_reset_status_download_failures();
2755 router_reset_descriptor_download_failures();
2756 if (!net_is_disabled())
2757 update_networkstatus_downloads(time(NULL));
2759 /* We'll retry routerstatus downloads in about 10 seconds; no need to
2760 * force a retry there. */
2762 if (server_mode(options)) {
2763 /* Maybe we've been given a new ed25519 key or certificate?
2765 time_t now = approx_time();
2766 int new_signing_key = load_ed_keys(options, now);
2767 if (new_signing_key < 0 ||
2768 generate_ed_link_cert(options, now, new_signing_key > 0)) {
2769 log_warn(LD_OR, "Problem reloading Ed25519 keys; still using old keys.");
2772 /* Update cpuworker and dnsworker processes, so they get up-to-date
2773 * configuration options. */
2774 cpuworkers_rotate_keyinfo();
2775 dns_reset();
2777 return 0;
2780 /** Initialize some mainloop_event_t objects that we require. */
2781 STATIC void
2782 initialize_mainloop_events(void)
2784 if (!schedule_active_linked_connections_event) {
2785 schedule_active_linked_connections_event =
2786 mainloop_event_postloop_new(schedule_active_linked_connections_cb, NULL);
2788 if (!postloop_cleanup_ev) {
2789 postloop_cleanup_ev =
2790 mainloop_event_postloop_new(postloop_cleanup_cb, NULL);
2794 /** Tor main loop. */
2796 do_main_loop(void)
2798 time_t now;
2800 /* initialize the periodic events first, so that code that depends on the
2801 * events being present does not assert.
2803 if (! periodic_events_initialized) {
2804 initialize_periodic_events();
2807 initialize_mainloop_events();
2809 /* initialize dns resolve map, spawn workers if needed */
2810 if (dns_init() < 0) {
2811 if (get_options()->ServerDNSAllowBrokenConfig)
2812 log_warn(LD_GENERAL, "Couldn't set up any working nameservers. "
2813 "Network not up yet? Will try again soon.");
2814 else {
2815 log_err(LD_GENERAL,"Error initializing dns subsystem; exiting. To "
2816 "retry instead, set the ServerDNSAllowBrokenResolvConf option.");
2820 handle_signals();
2821 monotime_init();
2822 timers_initialize();
2824 /* load the private keys, if we're supposed to have them, and set up the
2825 * TLS context. */
2826 if (! client_identity_key_is_set()) {
2827 if (init_keys() < 0) {
2828 log_err(LD_OR, "Error initializing keys; exiting");
2829 return -1;
2833 /* Set up our buckets */
2834 connection_bucket_init();
2836 /* initialize the bootstrap status events to know we're starting up */
2837 control_event_bootstrap(BOOTSTRAP_STATUS_STARTING, 0);
2839 /* Initialize the keypinning log. */
2840 if (authdir_mode_v3(get_options())) {
2841 char *fname = get_datadir_fname("key-pinning-journal");
2842 int r = 0;
2843 if (keypin_load_journal(fname)<0) {
2844 log_err(LD_DIR, "Error loading key-pinning journal: %s",strerror(errno));
2845 r = -1;
2847 if (keypin_open_journal(fname)<0) {
2848 log_err(LD_DIR, "Error opening key-pinning journal: %s",strerror(errno));
2849 r = -1;
2851 tor_free(fname);
2852 if (r)
2853 return r;
2856 /* This is the old name for key-pinning-journal. These got corrupted
2857 * in a couple of cases by #16530, so we started over. See #16580 for
2858 * the rationale and for other options we didn't take. We can remove
2859 * this code once all the authorities that ran 0.2.7.1-alpha-dev are
2860 * upgraded.
2862 char *fname = get_datadir_fname("key-pinning-entries");
2863 unlink(fname);
2864 tor_free(fname);
2867 if (trusted_dirs_reload_certs()) {
2868 log_warn(LD_DIR,
2869 "Couldn't load all cached v3 certificates. Starting anyway.");
2871 if (router_reload_consensus_networkstatus()) {
2872 return -1;
2874 /* load the routers file, or assign the defaults. */
2875 if (router_reload_router_list()) {
2876 return -1;
2878 /* load the networkstatuses. (This launches a download for new routers as
2879 * appropriate.)
2881 now = time(NULL);
2882 directory_info_has_arrived(now, 1, 0);
2884 if (server_mode(get_options()) || dir_server_mode(get_options())) {
2885 /* launch cpuworkers. Need to do this *after* we've read the onion key. */
2886 cpu_init();
2888 consdiffmgr_enable_background_compression();
2890 /* Setup shared random protocol subsystem. */
2891 if (authdir_mode_v3(get_options())) {
2892 if (sr_init(1) < 0) {
2893 return -1;
2897 /* set up once-a-second callback. */
2898 reschedule_per_second_timer();
2900 #ifdef HAVE_SYSTEMD_209
2901 uint64_t watchdog_delay;
2902 /* set up systemd watchdog notification. */
2903 if (sd_watchdog_enabled(1, &watchdog_delay) > 0) {
2904 if (! systemd_watchdog_timer) {
2905 struct timeval watchdog;
2906 /* The manager will "act on" us if we don't send them a notification
2907 * every 'watchdog_delay' microseconds. So, send notifications twice
2908 * that often. */
2909 watchdog_delay /= 2;
2910 watchdog.tv_sec = watchdog_delay / 1000000;
2911 watchdog.tv_usec = watchdog_delay % 1000000;
2913 systemd_watchdog_timer = periodic_timer_new(tor_libevent_get_base(),
2914 &watchdog,
2915 systemd_watchdog_callback,
2916 NULL);
2917 tor_assert(systemd_watchdog_timer);
2920 #endif /* defined(HAVE_SYSTEMD_209) */
2922 #ifdef HAVE_SYSTEMD
2924 const int r = sd_notify(0, "READY=1");
2925 if (r < 0) {
2926 log_warn(LD_GENERAL, "Unable to send readiness to systemd: %s",
2927 strerror(r));
2928 } else if (r > 0) {
2929 log_notice(LD_GENERAL, "Signaled readiness to systemd");
2930 } else {
2931 log_info(LD_GENERAL, "Systemd NOTIFY_SOCKET not present.");
2934 #endif /* defined(HAVE_SYSTEMD) */
2936 main_loop_should_exit = 0;
2937 main_loop_exit_value = 0;
2939 #ifdef ENABLE_RESTART_DEBUGGING
2941 static int first_time = 1;
2943 if (first_time && getenv("TOR_DEBUG_RESTART")) {
2944 first_time = 0;
2945 const char *sec_str = getenv("TOR_DEBUG_RESTART_AFTER_SECONDS");
2946 long sec;
2947 int sec_ok=0;
2948 if (sec_str &&
2949 (sec = tor_parse_long(sec_str, 10, 0, INT_MAX, &sec_ok, NULL)) &&
2950 sec_ok) {
2951 /* Okay, we parsed the seconds. */
2952 } else {
2953 sec = 5;
2955 struct timeval restart_after = { (time_t) sec, 0 };
2956 tor_shutdown_event_loop_for_restart_event =
2957 tor_evtimer_new(tor_libevent_get_base(),
2958 tor_shutdown_event_loop_for_restart_cb, NULL);
2959 event_add(tor_shutdown_event_loop_for_restart_event, &restart_after);
2962 #endif
2964 return run_main_loop_until_done();
2967 #ifndef _WIN32
2968 /** Rate-limiter for EINVAL-type libevent warnings. */
2969 static ratelim_t libevent_error_ratelim = RATELIM_INIT(10);
2970 #endif
2973 * Run the main loop a single time. Return 0 for "exit"; -1 for "exit with
2974 * error", and 1 for "run this again."
2976 static int
2977 run_main_loop_once(void)
2979 int loop_result;
2981 if (nt_service_is_stopping())
2982 return 0;
2984 if (main_loop_should_exit)
2985 return 0;
2987 #ifndef _WIN32
2988 /* Make it easier to tell whether libevent failure is our fault or not. */
2989 errno = 0;
2990 #endif
2992 if (get_options()->MainloopStats) {
2993 /* We always enforce that EVLOOP_ONCE is passed to event_base_loop() if we
2994 * are collecting main loop statistics. */
2995 called_loop_once = 1;
2996 } else {
2997 called_loop_once = 0;
3000 /* Make sure we know (about) what time it is. */
3001 update_approx_time(time(NULL));
3003 /* Here it is: the main loop. Here we tell Libevent to poll until we have
3004 * an event, or the second ends, or until we have some active linked
3005 * connections to trigger events for. Libevent will wait till one
3006 * of these happens, then run all the appropriate callbacks. */
3007 loop_result = tor_libevent_run_event_loop(tor_libevent_get_base(),
3008 called_loop_once);
3010 if (get_options()->MainloopStats) {
3011 /* Update our main loop counters. */
3012 if (loop_result == 0) {
3013 // The call was successful.
3014 increment_main_loop_success_count();
3015 } else if (loop_result == -1) {
3016 // The call was erroneous.
3017 increment_main_loop_error_count();
3018 } else if (loop_result == 1) {
3019 // The call didn't have any active or pending events
3020 // to handle.
3021 increment_main_loop_idle_count();
3025 /* Oh, the loop failed. That might be an error that we need to
3026 * catch, but more likely, it's just an interrupted poll() call or something,
3027 * and we should try again. */
3028 if (loop_result < 0) {
3029 int e = tor_socket_errno(-1);
3030 /* let the program survive things like ^z */
3031 if (e != EINTR && !ERRNO_IS_EINPROGRESS(e)) {
3032 log_err(LD_NET,"libevent call with %s failed: %s [%d]",
3033 tor_libevent_get_method(), tor_socket_strerror(e), e);
3034 return -1;
3035 #ifndef _WIN32
3036 } else if (e == EINVAL) {
3037 log_fn_ratelim(&libevent_error_ratelim, LOG_WARN, LD_NET,
3038 "EINVAL from libevent: should you upgrade libevent?");
3039 if (libevent_error_ratelim.n_calls_since_last_time > 8) {
3040 log_err(LD_NET, "Too many libevent errors, too fast: dying");
3041 return -1;
3043 #endif /* !defined(_WIN32) */
3044 } else {
3045 tor_assert_nonfatal_once(! ERRNO_IS_EINPROGRESS(e));
3046 log_debug(LD_NET,"libevent call interrupted.");
3047 /* You can't trust the results of this poll(). Go back to the
3048 * top of the big for loop. */
3049 return 1;
3053 if (main_loop_should_exit)
3054 return 0;
3056 return 1;
3059 /** Run the run_main_loop_once() function until it declares itself done,
3060 * and return its final return value.
3062 * Shadow won't invoke this function, so don't fill it up with things.
3064 static int
3065 run_main_loop_until_done(void)
3067 int loop_result = 1;
3068 do {
3069 loop_result = run_main_loop_once();
3070 } while (loop_result == 1);
3072 if (main_loop_should_exit)
3073 return main_loop_exit_value;
3074 else
3075 return loop_result;
3078 /** Libevent callback: invoked when we get a signal.
3080 static void
3081 signal_callback(evutil_socket_t fd, short events, void *arg)
3083 const int *sigptr = arg;
3084 const int sig = *sigptr;
3085 (void)fd;
3086 (void)events;
3088 update_current_time(time(NULL));
3089 process_signal(sig);
3092 /** Do the work of acting on a signal received in <b>sig</b> */
3093 static void
3094 process_signal(int sig)
3096 switch (sig)
3098 case SIGTERM:
3099 log_notice(LD_GENERAL,"Catching signal TERM, exiting cleanly.");
3100 tor_shutdown_event_loop_and_exit(0);
3101 break;
3102 case SIGINT:
3103 if (!server_mode(get_options())) { /* do it now */
3104 log_notice(LD_GENERAL,"Interrupt: exiting cleanly.");
3105 tor_shutdown_event_loop_and_exit(0);
3106 return;
3108 #ifdef HAVE_SYSTEMD
3109 sd_notify(0, "STOPPING=1");
3110 #endif
3111 hibernate_begin_shutdown();
3112 break;
3113 #ifdef SIGPIPE
3114 case SIGPIPE:
3115 log_debug(LD_GENERAL,"Caught SIGPIPE. Ignoring.");
3116 break;
3117 #endif
3118 case SIGUSR1:
3119 /* prefer to log it at INFO, but make sure we always see it */
3120 dumpstats(get_min_log_level()<LOG_INFO ? get_min_log_level() : LOG_INFO);
3121 control_event_signal(sig);
3122 break;
3123 case SIGUSR2:
3124 switch_logs_debug();
3125 log_debug(LD_GENERAL,"Caught USR2, going to loglevel debug. "
3126 "Send HUP to change back.");
3127 control_event_signal(sig);
3128 break;
3129 case SIGHUP:
3130 #ifdef HAVE_SYSTEMD
3131 sd_notify(0, "RELOADING=1");
3132 #endif
3133 if (do_hup() < 0) {
3134 log_warn(LD_CONFIG,"Restart failed (config error?). Exiting.");
3135 tor_shutdown_event_loop_and_exit(1);
3136 return;
3138 #ifdef HAVE_SYSTEMD
3139 sd_notify(0, "READY=1");
3140 #endif
3141 control_event_signal(sig);
3142 break;
3143 #ifdef SIGCHLD
3144 case SIGCHLD:
3145 notify_pending_waitpid_callbacks();
3146 break;
3147 #endif
3148 case SIGNEWNYM: {
3149 time_t now = time(NULL);
3150 if (time_of_last_signewnym + MAX_SIGNEWNYM_RATE > now) {
3151 const time_t delay_sec =
3152 time_of_last_signewnym + MAX_SIGNEWNYM_RATE - now;
3153 if (! signewnym_is_pending) {
3154 signewnym_is_pending = 1;
3155 if (!handle_deferred_signewnym_ev) {
3156 handle_deferred_signewnym_ev =
3157 mainloop_event_postloop_new(handle_deferred_signewnym_cb, NULL);
3159 const struct timeval delay_tv = { delay_sec, 0 };
3160 mainloop_event_schedule(handle_deferred_signewnym_ev, &delay_tv);
3162 log_notice(LD_CONTROL,
3163 "Rate limiting NEWNYM request: delaying by %d second(s)",
3164 (int)(delay_sec));
3165 } else {
3166 signewnym_impl(now);
3168 break;
3170 case SIGCLEARDNSCACHE:
3171 addressmap_clear_transient();
3172 control_event_signal(sig);
3173 break;
3174 case SIGHEARTBEAT:
3175 log_heartbeat(time(NULL));
3176 control_event_signal(sig);
3177 break;
3181 /** Returns Tor's uptime. */
3182 MOCK_IMPL(long,
3183 get_uptime,(void))
3185 return stats_n_seconds_working;
3188 /** Reset Tor's uptime. */
3189 MOCK_IMPL(void,
3190 reset_uptime,(void))
3192 stats_n_seconds_working = 0;
3196 * Write current memory usage information to the log.
3198 static void
3199 dumpmemusage(int severity)
3201 connection_dump_buffer_mem_stats(severity);
3202 tor_log(severity, LD_GENERAL, "In rephist: "U64_FORMAT" used by %d Tors.",
3203 U64_PRINTF_ARG(rephist_total_alloc), rephist_total_num);
3204 dump_routerlist_mem_usage(severity);
3205 dump_cell_pool_usage(severity);
3206 dump_dns_mem_usage(severity);
3207 tor_log_mallinfo(severity);
3210 /** Write all statistics to the log, with log level <b>severity</b>. Called
3211 * in response to a SIGUSR1. */
3212 static void
3213 dumpstats(int severity)
3215 time_t now = time(NULL);
3216 time_t elapsed;
3217 size_t rbuf_cap, wbuf_cap, rbuf_len, wbuf_len;
3219 tor_log(severity, LD_GENERAL, "Dumping stats:");
3221 SMARTLIST_FOREACH_BEGIN(connection_array, connection_t *, conn) {
3222 int i = conn_sl_idx;
3223 tor_log(severity, LD_GENERAL,
3224 "Conn %d (socket %d) type %d (%s), state %d (%s), created %d secs ago",
3225 i, (int)conn->s, conn->type, conn_type_to_string(conn->type),
3226 conn->state, conn_state_to_string(conn->type, conn->state),
3227 (int)(now - conn->timestamp_created));
3228 if (!connection_is_listener(conn)) {
3229 tor_log(severity,LD_GENERAL,
3230 "Conn %d is to %s:%d.", i,
3231 safe_str_client(conn->address),
3232 conn->port);
3233 tor_log(severity,LD_GENERAL,
3234 "Conn %d: %d bytes waiting on inbuf (len %d, last read %d secs ago)",
3236 (int)connection_get_inbuf_len(conn),
3237 (int)buf_allocation(conn->inbuf),
3238 (int)(now - conn->timestamp_last_read_allowed));
3239 tor_log(severity,LD_GENERAL,
3240 "Conn %d: %d bytes waiting on outbuf "
3241 "(len %d, last written %d secs ago)",i,
3242 (int)connection_get_outbuf_len(conn),
3243 (int)buf_allocation(conn->outbuf),
3244 (int)(now - conn->timestamp_last_write_allowed));
3245 if (conn->type == CONN_TYPE_OR) {
3246 or_connection_t *or_conn = TO_OR_CONN(conn);
3247 if (or_conn->tls) {
3248 if (tor_tls_get_buffer_sizes(or_conn->tls, &rbuf_cap, &rbuf_len,
3249 &wbuf_cap, &wbuf_len) == 0) {
3250 tor_log(severity, LD_GENERAL,
3251 "Conn %d: %d/%d bytes used on OpenSSL read buffer; "
3252 "%d/%d bytes used on write buffer.",
3253 i, (int)rbuf_len, (int)rbuf_cap, (int)wbuf_len, (int)wbuf_cap);
3258 circuit_dump_by_conn(conn, severity); /* dump info about all the circuits
3259 * using this conn */
3260 } SMARTLIST_FOREACH_END(conn);
3262 channel_dumpstats(severity);
3263 channel_listener_dumpstats(severity);
3265 tor_log(severity, LD_NET,
3266 "Cells processed: "U64_FORMAT" padding\n"
3267 " "U64_FORMAT" create\n"
3268 " "U64_FORMAT" created\n"
3269 " "U64_FORMAT" relay\n"
3270 " ("U64_FORMAT" relayed)\n"
3271 " ("U64_FORMAT" delivered)\n"
3272 " "U64_FORMAT" destroy",
3273 U64_PRINTF_ARG(stats_n_padding_cells_processed),
3274 U64_PRINTF_ARG(stats_n_create_cells_processed),
3275 U64_PRINTF_ARG(stats_n_created_cells_processed),
3276 U64_PRINTF_ARG(stats_n_relay_cells_processed),
3277 U64_PRINTF_ARG(stats_n_relay_cells_relayed),
3278 U64_PRINTF_ARG(stats_n_relay_cells_delivered),
3279 U64_PRINTF_ARG(stats_n_destroy_cells_processed));
3280 if (stats_n_data_cells_packaged)
3281 tor_log(severity,LD_NET,"Average packaged cell fullness: %2.3f%%",
3282 100*(U64_TO_DBL(stats_n_data_bytes_packaged) /
3283 U64_TO_DBL(stats_n_data_cells_packaged*RELAY_PAYLOAD_SIZE)) );
3284 if (stats_n_data_cells_received)
3285 tor_log(severity,LD_NET,"Average delivered cell fullness: %2.3f%%",
3286 100*(U64_TO_DBL(stats_n_data_bytes_received) /
3287 U64_TO_DBL(stats_n_data_cells_received*RELAY_PAYLOAD_SIZE)) );
3289 cpuworker_log_onionskin_overhead(severity, ONION_HANDSHAKE_TYPE_TAP, "TAP");
3290 cpuworker_log_onionskin_overhead(severity, ONION_HANDSHAKE_TYPE_NTOR,"ntor");
3292 if (now - time_of_process_start >= 0)
3293 elapsed = now - time_of_process_start;
3294 else
3295 elapsed = 0;
3297 if (elapsed) {
3298 tor_log(severity, LD_NET,
3299 "Average bandwidth: "U64_FORMAT"/%d = %d bytes/sec reading",
3300 U64_PRINTF_ARG(stats_n_bytes_read),
3301 (int)elapsed,
3302 (int) (stats_n_bytes_read/elapsed));
3303 tor_log(severity, LD_NET,
3304 "Average bandwidth: "U64_FORMAT"/%d = %d bytes/sec writing",
3305 U64_PRINTF_ARG(stats_n_bytes_written),
3306 (int)elapsed,
3307 (int) (stats_n_bytes_written/elapsed));
3310 tor_log(severity, LD_NET, "--------------- Dumping memory information:");
3311 dumpmemusage(severity);
3313 rep_hist_dump_stats(now,severity);
3314 rend_service_dump_stats(severity);
3315 dump_distinct_digest_count(severity);
3318 /** Called by exit() as we shut down the process.
3320 static void
3321 exit_function(void)
3323 /* NOTE: If we ever daemonize, this gets called immediately. That's
3324 * okay for now, because we only use this on Windows. */
3325 #ifdef _WIN32
3326 WSACleanup();
3327 #endif
3330 #ifdef _WIN32
3331 #define UNIX_ONLY 0
3332 #else
3333 #define UNIX_ONLY 1
3334 #endif
3336 static struct {
3337 /** A numeric code for this signal. Must match the signal value if
3338 * try_to_register is true. */
3339 int signal_value;
3340 /** True if we should try to register this signal with libevent and catch
3341 * corresponding posix signals. False otherwise. */
3342 int try_to_register;
3343 /** Pointer to hold the event object constructed for this signal. */
3344 struct event *signal_event;
3345 } signal_handlers[] = {
3346 #ifdef SIGINT
3347 { SIGINT, UNIX_ONLY, NULL }, /* do a controlled slow shutdown */
3348 #endif
3349 #ifdef SIGTERM
3350 { SIGTERM, UNIX_ONLY, NULL }, /* to terminate now */
3351 #endif
3352 #ifdef SIGPIPE
3353 { SIGPIPE, UNIX_ONLY, NULL }, /* otherwise SIGPIPE kills us */
3354 #endif
3355 #ifdef SIGUSR1
3356 { SIGUSR1, UNIX_ONLY, NULL }, /* dump stats */
3357 #endif
3358 #ifdef SIGUSR2
3359 { SIGUSR2, UNIX_ONLY, NULL }, /* go to loglevel debug */
3360 #endif
3361 #ifdef SIGHUP
3362 { SIGHUP, UNIX_ONLY, NULL }, /* to reload config, retry conns, etc */
3363 #endif
3364 #ifdef SIGXFSZ
3365 { SIGXFSZ, UNIX_ONLY, NULL }, /* handle file-too-big resource exhaustion */
3366 #endif
3367 #ifdef SIGCHLD
3368 { SIGCHLD, UNIX_ONLY, NULL }, /* handle dns/cpu workers that exit */
3369 #endif
3370 /* These are controller-only */
3371 { SIGNEWNYM, 0, NULL },
3372 { SIGCLEARDNSCACHE, 0, NULL },
3373 { SIGHEARTBEAT, 0, NULL },
3374 { -1, -1, NULL }
3377 /** Set up the signal handler events for this process, and register them
3378 * with libevent if appropriate. */
3379 void
3380 handle_signals(void)
3382 int i;
3383 const int enabled = !get_options()->DisableSignalHandlers;
3385 for (i = 0; signal_handlers[i].signal_value >= 0; ++i) {
3386 /* Signal handlers are only registered with libevent if they need to catch
3387 * real POSIX signals. We construct these signal handler events in either
3388 * case, though, so that controllers can activate them with the SIGNAL
3389 * command.
3391 if (enabled && signal_handlers[i].try_to_register) {
3392 signal_handlers[i].signal_event =
3393 tor_evsignal_new(tor_libevent_get_base(),
3394 signal_handlers[i].signal_value,
3395 signal_callback,
3396 &signal_handlers[i].signal_value);
3397 if (event_add(signal_handlers[i].signal_event, NULL))
3398 log_warn(LD_BUG, "Error from libevent when adding "
3399 "event for signal %d",
3400 signal_handlers[i].signal_value);
3401 } else {
3402 signal_handlers[i].signal_event =
3403 tor_event_new(tor_libevent_get_base(), -1,
3404 EV_SIGNAL, signal_callback,
3405 &signal_handlers[i].signal_value);
3410 /* Cause the signal handler for signal_num to be called in the event loop. */
3411 void
3412 activate_signal(int signal_num)
3414 int i;
3415 for (i = 0; signal_handlers[i].signal_value >= 0; ++i) {
3416 if (signal_handlers[i].signal_value == signal_num) {
3417 event_active(signal_handlers[i].signal_event, EV_SIGNAL, 1);
3418 return;
3423 /** Main entry point for the Tor command-line client. Return 0 on "success",
3424 * negative on "failure", and positive on "success and exit".
3427 tor_init(int argc, char *argv[])
3429 char progname[256];
3430 int quiet = 0;
3432 time_of_process_start = time(NULL);
3433 init_connection_lists();
3434 /* Have the log set up with our application name. */
3435 tor_snprintf(progname, sizeof(progname), "Tor %s", get_version());
3436 log_set_application_name(progname);
3438 /* Set up the crypto nice and early */
3439 if (crypto_early_init() < 0) {
3440 log_err(LD_GENERAL, "Unable to initialize the crypto subsystem!");
3441 return -1;
3444 /* Initialize the history structures. */
3445 rep_hist_init();
3446 /* Initialize the service cache. */
3447 rend_cache_init();
3448 addressmap_init(); /* Init the client dns cache. Do it always, since it's
3449 * cheap. */
3450 /* Initialize the HS subsystem. */
3451 hs_init();
3454 /* We search for the "quiet" option first, since it decides whether we
3455 * will log anything at all to the command line. */
3456 config_line_t *opts = NULL, *cmdline_opts = NULL;
3457 const config_line_t *cl;
3458 (void) config_parse_commandline(argc, argv, 1, &opts, &cmdline_opts);
3459 for (cl = cmdline_opts; cl; cl = cl->next) {
3460 if (!strcmp(cl->key, "--hush"))
3461 quiet = 1;
3462 if (!strcmp(cl->key, "--quiet") ||
3463 !strcmp(cl->key, "--dump-config"))
3464 quiet = 2;
3465 /* The following options imply --hush */
3466 if (!strcmp(cl->key, "--version") || !strcmp(cl->key, "--digests") ||
3467 !strcmp(cl->key, "--list-torrc-options") ||
3468 !strcmp(cl->key, "--library-versions") ||
3469 !strcmp(cl->key, "--hash-password") ||
3470 !strcmp(cl->key, "-h") || !strcmp(cl->key, "--help")) {
3471 if (quiet < 1)
3472 quiet = 1;
3475 config_free_lines(opts);
3476 config_free_lines(cmdline_opts);
3479 /* give it somewhere to log to initially */
3480 switch (quiet) {
3481 case 2:
3482 /* no initial logging */
3483 break;
3484 case 1:
3485 add_temp_log(LOG_WARN);
3486 break;
3487 default:
3488 add_temp_log(LOG_NOTICE);
3490 quiet_level = quiet;
3493 const char *version = get_version();
3495 log_notice(LD_GENERAL, "Tor %s running on %s with Libevent %s, "
3496 "OpenSSL %s, Zlib %s, Liblzma %s, and Libzstd %s.", version,
3497 get_uname(),
3498 tor_libevent_get_version_str(),
3499 crypto_openssl_get_version_str(),
3500 tor_compress_supports_method(ZLIB_METHOD) ?
3501 tor_compress_version_str(ZLIB_METHOD) : "N/A",
3502 tor_compress_supports_method(LZMA_METHOD) ?
3503 tor_compress_version_str(LZMA_METHOD) : "N/A",
3504 tor_compress_supports_method(ZSTD_METHOD) ?
3505 tor_compress_version_str(ZSTD_METHOD) : "N/A");
3507 log_notice(LD_GENERAL, "Tor can't help you if you use it wrong! "
3508 "Learn how to be safe at "
3509 "https://www.torproject.org/download/download#warning");
3511 if (strstr(version, "alpha") || strstr(version, "beta"))
3512 log_notice(LD_GENERAL, "This version is not a stable Tor release. "
3513 "Expect more bugs than usual.");
3515 tor_compress_log_init_warnings();
3518 #ifdef HAVE_RUST
3519 rust_log_welcome_string();
3520 #endif /* defined(HAVE_RUST) */
3522 if (network_init()<0) {
3523 log_err(LD_BUG,"Error initializing network; exiting.");
3524 return -1;
3526 atexit(exit_function);
3528 int init_rv = options_init_from_torrc(argc,argv);
3529 if (init_rv < 0) {
3530 log_err(LD_CONFIG,"Reading config failed--see warnings above.");
3531 return -1;
3532 } else if (init_rv > 0) {
3533 // We succeeded, and should exit anyway -- probably the user just said
3534 // "--version" or something like that.
3535 return 1;
3538 /* The options are now initialised */
3539 const or_options_t *options = get_options();
3541 /* Initialize channelpadding parameters to defaults until we get
3542 * a consensus */
3543 channelpadding_new_consensus_params(NULL);
3545 /* Initialize predicted ports list after loading options */
3546 predicted_ports_init();
3548 #ifndef _WIN32
3549 if (geteuid()==0)
3550 log_warn(LD_GENERAL,"You are running Tor as root. You don't need to, "
3551 "and you probably shouldn't.");
3552 #endif
3554 if (crypto_global_init(options->HardwareAccel,
3555 options->AccelName,
3556 options->AccelDir)) {
3557 log_err(LD_BUG, "Unable to initialize OpenSSL. Exiting.");
3558 return -1;
3560 stream_choice_seed_weak_rng();
3561 if (tor_init_libevent_rng() < 0) {
3562 log_warn(LD_NET, "Problem initializing libevent RNG.");
3565 /* Scan/clean unparseable descriptors; after reading config */
3566 routerparse_init();
3568 return 0;
3571 /** A lockfile structure, used to prevent two Tors from messing with the
3572 * data directory at once. If this variable is non-NULL, we're holding
3573 * the lockfile. */
3574 static tor_lockfile_t *lockfile = NULL;
3576 /** Try to grab the lock file described in <b>options</b>, if we do not
3577 * already have it. If <b>err_if_locked</b> is true, warn if somebody else is
3578 * holding the lock, and exit if we can't get it after waiting. Otherwise,
3579 * return -1 if we can't get the lockfile. Return 0 on success.
3582 try_locking(const or_options_t *options, int err_if_locked)
3584 if (lockfile)
3585 return 0;
3586 else {
3587 char *fname = options_get_datadir_fname(options, "lock");
3588 int already_locked = 0;
3589 tor_lockfile_t *lf = tor_lockfile_lock(fname, 0, &already_locked);
3590 tor_free(fname);
3591 if (!lf) {
3592 if (err_if_locked && already_locked) {
3593 int r;
3594 log_warn(LD_GENERAL, "It looks like another Tor process is running "
3595 "with the same data directory. Waiting 5 seconds to see "
3596 "if it goes away.");
3597 #ifndef _WIN32
3598 sleep(5);
3599 #else
3600 Sleep(5000);
3601 #endif
3602 r = try_locking(options, 0);
3603 if (r<0) {
3604 log_err(LD_GENERAL, "No, it's still there. Exiting.");
3605 return -1;
3607 return r;
3609 return -1;
3611 lockfile = lf;
3612 return 0;
3616 /** Return true iff we've successfully acquired the lock file. */
3618 have_lockfile(void)
3620 return lockfile != NULL;
3623 /** If we have successfully acquired the lock file, release it. */
3624 void
3625 release_lockfile(void)
3627 if (lockfile) {
3628 tor_lockfile_unlock(lockfile);
3629 lockfile = NULL;
3633 /** Free all memory that we might have allocated somewhere.
3634 * If <b>postfork</b>, we are a worker process and we want to free
3635 * only the parts of memory that we won't touch. If !<b>postfork</b>,
3636 * Tor is shutting down and we should free everything.
3638 * Helps us find the real leaks with sanitizers and the like. Also valgrind
3639 * should then report 0 reachable in its leak report (in an ideal world --
3640 * in practice libevent, SSL, libc etc never quite free everything). */
3641 void
3642 tor_free_all(int postfork)
3644 if (!postfork) {
3645 evdns_shutdown(1);
3647 geoip_free_all();
3648 dirvote_free_all();
3649 routerlist_free_all();
3650 networkstatus_free_all();
3651 addressmap_free_all();
3652 dirserv_free_all();
3653 rend_cache_free_all();
3654 rend_service_authorization_free_all();
3655 rep_hist_free_all();
3656 dns_free_all();
3657 clear_pending_onions();
3658 circuit_free_all();
3659 entry_guards_free_all();
3660 pt_free_all();
3661 channel_tls_free_all();
3662 channel_free_all();
3663 connection_free_all();
3664 connection_edge_free_all();
3665 scheduler_free_all();
3666 nodelist_free_all();
3667 microdesc_free_all();
3668 routerparse_free_all();
3669 ext_orport_free_all();
3670 control_free_all();
3671 tor_free_getaddrinfo_cache();
3672 protover_free_all();
3673 bridges_free_all();
3674 consdiffmgr_free_all();
3675 hs_free_all();
3676 dos_free_all();
3677 circuitmux_ewma_free_all();
3678 accounting_free_all();
3680 if (!postfork) {
3681 config_free_all();
3682 or_state_free_all();
3683 router_free_all();
3684 routerkeys_free_all();
3685 policies_free_all();
3687 if (!postfork) {
3688 tor_tls_free_all();
3689 #ifndef _WIN32
3690 tor_getpwnam(NULL);
3691 #endif
3693 /* stuff in main.c */
3695 smartlist_free(connection_array);
3696 smartlist_free(closeable_connection_lst);
3697 smartlist_free(active_linked_connection_lst);
3698 periodic_timer_free(second_timer);
3699 teardown_periodic_events();
3700 tor_event_free(shutdown_did_not_work_event);
3701 tor_event_free(initialize_periodic_events_event);
3702 mainloop_event_free(directory_all_unreachable_cb_event);
3703 mainloop_event_free(schedule_active_linked_connections_event);
3704 mainloop_event_free(postloop_cleanup_ev);
3705 mainloop_event_free(handle_deferred_signewnym_ev);
3707 #ifdef HAVE_SYSTEMD_209
3708 periodic_timer_free(systemd_watchdog_timer);
3709 #endif
3711 memset(&global_bucket, 0, sizeof(global_bucket));
3712 memset(&global_relayed_bucket, 0, sizeof(global_relayed_bucket));
3713 stats_n_bytes_read = stats_n_bytes_written = 0;
3714 time_of_process_start = 0;
3715 time_of_last_signewnym = 0;
3716 signewnym_is_pending = 0;
3717 newnym_epoch = 0;
3718 called_loop_once = 0;
3719 main_loop_should_exit = 0;
3720 main_loop_exit_value = 0;
3721 can_complete_circuits = 0;
3722 quiet_level = 0;
3723 should_init_bridge_stats = 1;
3724 dns_honesty_first_time = 1;
3725 heartbeat_callback_first_time = 1;
3726 current_second = 0;
3727 memset(&current_second_last_changed, 0,
3728 sizeof(current_second_last_changed));
3730 if (!postfork) {
3731 release_lockfile();
3733 tor_libevent_free_all();
3734 /* Stuff in util.c and address.c*/
3735 if (!postfork) {
3736 escaped(NULL);
3737 esc_router_info(NULL);
3738 clean_up_backtrace_handler();
3739 logs_free_all(); /* free log strings. do this last so logs keep working. */
3744 * Remove the specified file, and log a warning if the operation fails for
3745 * any reason other than the file not existing. Ignores NULL filenames.
3747 void
3748 tor_remove_file(const char *filename)
3750 if (filename && tor_unlink(filename) != 0 && errno != ENOENT) {
3751 log_warn(LD_FS, "Couldn't unlink %s: %s",
3752 filename, strerror(errno));
3756 /** Do whatever cleanup is necessary before shutting Tor down. */
3757 void
3758 tor_cleanup(void)
3760 const or_options_t *options = get_options();
3761 if (options->command == CMD_RUN_TOR) {
3762 time_t now = time(NULL);
3763 /* Remove our pid file. We don't care if there was an error when we
3764 * unlink, nothing we could do about it anyways. */
3765 tor_remove_file(options->PidFile);
3766 /* Remove control port file */
3767 tor_remove_file(options->ControlPortWriteToFile);
3768 /* Remove cookie authentication file */
3770 char *cookie_fname = get_controller_cookie_file_name();
3771 tor_remove_file(cookie_fname);
3772 tor_free(cookie_fname);
3774 /* Remove Extended ORPort cookie authentication file */
3776 char *cookie_fname = get_ext_or_auth_cookie_file_name();
3777 tor_remove_file(cookie_fname);
3778 tor_free(cookie_fname);
3780 if (accounting_is_enabled(options))
3781 accounting_record_bandwidth_usage(now, get_or_state());
3782 or_state_mark_dirty(get_or_state(), 0); /* force an immediate save. */
3783 or_state_save(now);
3784 if (authdir_mode(options)) {
3785 sr_save_and_cleanup();
3787 if (authdir_mode_tests_reachability(options))
3788 rep_hist_record_mtbf_data(now, 0);
3789 keypin_close_journal();
3792 timers_shutdown();
3794 tor_free_all(0); /* We could move tor_free_all back into the ifdef below
3795 later, if it makes shutdown unacceptably slow. But for
3796 now, leave it here: it's helped us catch bugs in the
3797 past. */
3798 crypto_global_cleanup();
3801 /** Read/create keys as needed, and echo our fingerprint to stdout. */
3802 static int
3803 do_list_fingerprint(void)
3805 char buf[FINGERPRINT_LEN+1];
3806 crypto_pk_t *k;
3807 const char *nickname = get_options()->Nickname;
3808 sandbox_disable_getaddrinfo_cache();
3809 if (!server_mode(get_options())) {
3810 log_err(LD_GENERAL,
3811 "Clients don't have long-term identity keys. Exiting.");
3812 return -1;
3814 tor_assert(nickname);
3815 if (init_keys() < 0) {
3816 log_err(LD_GENERAL,"Error initializing keys; exiting.");
3817 return -1;
3819 if (!(k = get_server_identity_key())) {
3820 log_err(LD_GENERAL,"Error: missing identity key.");
3821 return -1;
3823 if (crypto_pk_get_fingerprint(k, buf, 1)<0) {
3824 log_err(LD_BUG, "Error computing fingerprint");
3825 return -1;
3827 printf("%s %s\n", nickname, buf);
3828 return 0;
3831 /** Entry point for password hashing: take the desired password from
3832 * the command line, and print its salted hash to stdout. **/
3833 static void
3834 do_hash_password(void)
3837 char output[256];
3838 char key[S2K_RFC2440_SPECIFIER_LEN+DIGEST_LEN];
3840 crypto_rand(key, S2K_RFC2440_SPECIFIER_LEN-1);
3841 key[S2K_RFC2440_SPECIFIER_LEN-1] = (uint8_t)96; /* Hash 64 K of data. */
3842 secret_to_key_rfc2440(key+S2K_RFC2440_SPECIFIER_LEN, DIGEST_LEN,
3843 get_options()->command_arg, strlen(get_options()->command_arg),
3844 key);
3845 base16_encode(output, sizeof(output), key, sizeof(key));
3846 printf("16:%s\n",output);
3849 /** Entry point for configuration dumping: write the configuration to
3850 * stdout. */
3851 static int
3852 do_dump_config(void)
3854 const or_options_t *options = get_options();
3855 const char *arg = options->command_arg;
3856 int how;
3857 char *opts;
3859 if (!strcmp(arg, "short")) {
3860 how = OPTIONS_DUMP_MINIMAL;
3861 } else if (!strcmp(arg, "non-builtin")) {
3862 how = OPTIONS_DUMP_DEFAULTS;
3863 } else if (!strcmp(arg, "full")) {
3864 how = OPTIONS_DUMP_ALL;
3865 } else {
3866 fprintf(stderr, "No valid argument to --dump-config found!\n");
3867 fprintf(stderr, "Please select 'short', 'non-builtin', or 'full'.\n");
3869 return -1;
3872 opts = options_dump(options, how);
3873 printf("%s", opts);
3874 tor_free(opts);
3876 return 0;
3879 static void
3880 init_addrinfo(void)
3882 if (! server_mode(get_options()) ||
3883 (get_options()->Address && strlen(get_options()->Address) > 0)) {
3884 /* We don't need to seed our own hostname, because we won't be calling
3885 * resolve_my_address on it.
3887 return;
3889 char hname[256];
3891 // host name to sandbox
3892 gethostname(hname, sizeof(hname));
3893 tor_add_addrinfo(hname);
3896 static sandbox_cfg_t*
3897 sandbox_init_filter(void)
3899 const or_options_t *options = get_options();
3900 sandbox_cfg_t *cfg = sandbox_cfg_new();
3901 int i;
3903 sandbox_cfg_allow_openat_filename(&cfg,
3904 get_cachedir_fname("cached-status"));
3906 #define OPEN(name) \
3907 sandbox_cfg_allow_open_filename(&cfg, tor_strdup(name))
3909 #define OPEN_DATADIR(name) \
3910 sandbox_cfg_allow_open_filename(&cfg, get_datadir_fname(name))
3912 #define OPEN_DATADIR2(name, name2) \
3913 sandbox_cfg_allow_open_filename(&cfg, get_datadir_fname2((name), (name2)))
3915 #define OPEN_DATADIR_SUFFIX(name, suffix) do { \
3916 OPEN_DATADIR(name); \
3917 OPEN_DATADIR(name suffix); \
3918 } while (0)
3920 #define OPEN_DATADIR2_SUFFIX(name, name2, suffix) do { \
3921 OPEN_DATADIR2(name, name2); \
3922 OPEN_DATADIR2(name, name2 suffix); \
3923 } while (0)
3925 #define OPEN_KEY_DIRECTORY() \
3926 sandbox_cfg_allow_open_filename(&cfg, tor_strdup(options->KeyDirectory))
3927 #define OPEN_CACHEDIR(name) \
3928 sandbox_cfg_allow_open_filename(&cfg, get_cachedir_fname(name))
3929 #define OPEN_CACHEDIR_SUFFIX(name, suffix) do { \
3930 OPEN_CACHEDIR(name); \
3931 OPEN_CACHEDIR(name suffix); \
3932 } while (0)
3933 #define OPEN_KEYDIR(name) \
3934 sandbox_cfg_allow_open_filename(&cfg, get_keydir_fname(name))
3935 #define OPEN_KEYDIR_SUFFIX(name, suffix) do { \
3936 OPEN_KEYDIR(name); \
3937 OPEN_KEYDIR(name suffix); \
3938 } while (0)
3940 OPEN(options->DataDirectory);
3941 OPEN_KEY_DIRECTORY();
3943 OPEN_CACHEDIR_SUFFIX("cached-certs", ".tmp");
3944 OPEN_CACHEDIR_SUFFIX("cached-consensus", ".tmp");
3945 OPEN_CACHEDIR_SUFFIX("unverified-consensus", ".tmp");
3946 OPEN_CACHEDIR_SUFFIX("unverified-microdesc-consensus", ".tmp");
3947 OPEN_CACHEDIR_SUFFIX("cached-microdesc-consensus", ".tmp");
3948 OPEN_CACHEDIR_SUFFIX("cached-microdescs", ".tmp");
3949 OPEN_CACHEDIR_SUFFIX("cached-microdescs.new", ".tmp");
3950 OPEN_CACHEDIR_SUFFIX("cached-descriptors", ".tmp");
3951 OPEN_CACHEDIR_SUFFIX("cached-descriptors.new", ".tmp");
3952 OPEN_CACHEDIR("cached-descriptors.tmp.tmp");
3953 OPEN_CACHEDIR_SUFFIX("cached-extrainfo", ".tmp");
3954 OPEN_CACHEDIR_SUFFIX("cached-extrainfo.new", ".tmp");
3955 OPEN_CACHEDIR("cached-extrainfo.tmp.tmp");
3957 OPEN_DATADIR_SUFFIX("state", ".tmp");
3958 OPEN_DATADIR_SUFFIX("sr-state", ".tmp");
3959 OPEN_DATADIR_SUFFIX("unparseable-desc", ".tmp");
3960 OPEN_DATADIR_SUFFIX("v3-status-votes", ".tmp");
3961 OPEN_DATADIR("key-pinning-journal");
3962 OPEN("/dev/srandom");
3963 OPEN("/dev/urandom");
3964 OPEN("/dev/random");
3965 OPEN("/etc/hosts");
3966 OPEN("/proc/meminfo");
3968 if (options->BridgeAuthoritativeDir)
3969 OPEN_DATADIR_SUFFIX("networkstatus-bridges", ".tmp");
3971 if (authdir_mode(options))
3972 OPEN_DATADIR("approved-routers");
3974 if (options->ServerDNSResolvConfFile)
3975 sandbox_cfg_allow_open_filename(&cfg,
3976 tor_strdup(options->ServerDNSResolvConfFile));
3977 else
3978 sandbox_cfg_allow_open_filename(&cfg, tor_strdup("/etc/resolv.conf"));
3980 for (i = 0; i < 2; ++i) {
3981 if (get_torrc_fname(i)) {
3982 sandbox_cfg_allow_open_filename(&cfg, tor_strdup(get_torrc_fname(i)));
3986 SMARTLIST_FOREACH(options->FilesOpenedByIncludes, char *, f, {
3987 OPEN(f);
3990 #define RENAME_SUFFIX(name, suffix) \
3991 sandbox_cfg_allow_rename(&cfg, \
3992 get_datadir_fname(name suffix), \
3993 get_datadir_fname(name))
3995 #define RENAME_SUFFIX2(prefix, name, suffix) \
3996 sandbox_cfg_allow_rename(&cfg, \
3997 get_datadir_fname2(prefix, name suffix), \
3998 get_datadir_fname2(prefix, name))
4000 #define RENAME_CACHEDIR_SUFFIX(name, suffix) \
4001 sandbox_cfg_allow_rename(&cfg, \
4002 get_cachedir_fname(name suffix), \
4003 get_cachedir_fname(name))
4005 #define RENAME_KEYDIR_SUFFIX(name, suffix) \
4006 sandbox_cfg_allow_rename(&cfg, \
4007 get_keydir_fname(name suffix), \
4008 get_keydir_fname(name))
4010 RENAME_CACHEDIR_SUFFIX("cached-certs", ".tmp");
4011 RENAME_CACHEDIR_SUFFIX("cached-consensus", ".tmp");
4012 RENAME_CACHEDIR_SUFFIX("unverified-consensus", ".tmp");
4013 RENAME_CACHEDIR_SUFFIX("unverified-microdesc-consensus", ".tmp");
4014 RENAME_CACHEDIR_SUFFIX("cached-microdesc-consensus", ".tmp");
4015 RENAME_CACHEDIR_SUFFIX("cached-microdescs", ".tmp");
4016 RENAME_CACHEDIR_SUFFIX("cached-microdescs", ".new");
4017 RENAME_CACHEDIR_SUFFIX("cached-microdescs.new", ".tmp");
4018 RENAME_CACHEDIR_SUFFIX("cached-descriptors", ".tmp");
4019 RENAME_CACHEDIR_SUFFIX("cached-descriptors", ".new");
4020 RENAME_CACHEDIR_SUFFIX("cached-descriptors.new", ".tmp");
4021 RENAME_CACHEDIR_SUFFIX("cached-extrainfo", ".tmp");
4022 RENAME_CACHEDIR_SUFFIX("cached-extrainfo", ".new");
4023 RENAME_CACHEDIR_SUFFIX("cached-extrainfo.new", ".tmp");
4025 RENAME_SUFFIX("state", ".tmp");
4026 RENAME_SUFFIX("sr-state", ".tmp");
4027 RENAME_SUFFIX("unparseable-desc", ".tmp");
4028 RENAME_SUFFIX("v3-status-votes", ".tmp");
4030 if (options->BridgeAuthoritativeDir)
4031 RENAME_SUFFIX("networkstatus-bridges", ".tmp");
4033 #define STAT_DATADIR(name) \
4034 sandbox_cfg_allow_stat_filename(&cfg, get_datadir_fname(name))
4036 #define STAT_CACHEDIR(name) \
4037 sandbox_cfg_allow_stat_filename(&cfg, get_cachedir_fname(name))
4039 #define STAT_DATADIR2(name, name2) \
4040 sandbox_cfg_allow_stat_filename(&cfg, get_datadir_fname2((name), (name2)))
4042 #define STAT_KEY_DIRECTORY() \
4043 sandbox_cfg_allow_stat_filename(&cfg, tor_strdup(options->KeyDirectory))
4045 STAT_DATADIR(NULL);
4046 STAT_DATADIR("lock");
4047 STAT_DATADIR("state");
4048 STAT_DATADIR("router-stability");
4050 STAT_CACHEDIR("cached-extrainfo.new");
4053 smartlist_t *files = smartlist_new();
4054 tor_log_get_logfile_names(files);
4055 SMARTLIST_FOREACH(files, char *, file_name, {
4056 /* steals reference */
4057 sandbox_cfg_allow_open_filename(&cfg, file_name);
4059 smartlist_free(files);
4063 smartlist_t *files = smartlist_new();
4064 smartlist_t *dirs = smartlist_new();
4065 hs_service_lists_fnames_for_sandbox(files, dirs);
4066 SMARTLIST_FOREACH(files, char *, file_name, {
4067 char *tmp_name = NULL;
4068 tor_asprintf(&tmp_name, "%s.tmp", file_name);
4069 sandbox_cfg_allow_rename(&cfg,
4070 tor_strdup(tmp_name), tor_strdup(file_name));
4071 /* steals references */
4072 sandbox_cfg_allow_open_filename(&cfg, file_name);
4073 sandbox_cfg_allow_open_filename(&cfg, tmp_name);
4075 SMARTLIST_FOREACH(dirs, char *, dir, {
4076 /* steals reference */
4077 sandbox_cfg_allow_stat_filename(&cfg, dir);
4079 smartlist_free(files);
4080 smartlist_free(dirs);
4084 char *fname;
4085 if ((fname = get_controller_cookie_file_name())) {
4086 sandbox_cfg_allow_open_filename(&cfg, fname);
4088 if ((fname = get_ext_or_auth_cookie_file_name())) {
4089 sandbox_cfg_allow_open_filename(&cfg, fname);
4093 SMARTLIST_FOREACH_BEGIN(get_configured_ports(), port_cfg_t *, port) {
4094 if (!port->is_unix_addr)
4095 continue;
4096 /* When we open an AF_UNIX address, we want permission to open the
4097 * directory that holds it. */
4098 char *dirname = tor_strdup(port->unix_addr);
4099 if (get_parent_directory(dirname) == 0) {
4100 OPEN(dirname);
4102 tor_free(dirname);
4103 sandbox_cfg_allow_chmod_filename(&cfg, tor_strdup(port->unix_addr));
4104 sandbox_cfg_allow_chown_filename(&cfg, tor_strdup(port->unix_addr));
4105 } SMARTLIST_FOREACH_END(port);
4107 if (options->DirPortFrontPage) {
4108 sandbox_cfg_allow_open_filename(&cfg,
4109 tor_strdup(options->DirPortFrontPage));
4112 // orport
4113 if (server_mode(get_options())) {
4115 OPEN_KEYDIR_SUFFIX("secret_id_key", ".tmp");
4116 OPEN_KEYDIR_SUFFIX("secret_onion_key", ".tmp");
4117 OPEN_KEYDIR_SUFFIX("secret_onion_key_ntor", ".tmp");
4118 OPEN_KEYDIR("secret_id_key.old");
4119 OPEN_KEYDIR("secret_onion_key.old");
4120 OPEN_KEYDIR("secret_onion_key_ntor.old");
4122 OPEN_KEYDIR_SUFFIX("ed25519_master_id_secret_key", ".tmp");
4123 OPEN_KEYDIR_SUFFIX("ed25519_master_id_secret_key_encrypted", ".tmp");
4124 OPEN_KEYDIR_SUFFIX("ed25519_master_id_public_key", ".tmp");
4125 OPEN_KEYDIR_SUFFIX("ed25519_signing_secret_key", ".tmp");
4126 OPEN_KEYDIR_SUFFIX("ed25519_signing_secret_key_encrypted", ".tmp");
4127 OPEN_KEYDIR_SUFFIX("ed25519_signing_public_key", ".tmp");
4128 OPEN_KEYDIR_SUFFIX("ed25519_signing_cert", ".tmp");
4130 OPEN_DATADIR2_SUFFIX("stats", "bridge-stats", ".tmp");
4131 OPEN_DATADIR2_SUFFIX("stats", "dirreq-stats", ".tmp");
4133 OPEN_DATADIR2_SUFFIX("stats", "entry-stats", ".tmp");
4134 OPEN_DATADIR2_SUFFIX("stats", "exit-stats", ".tmp");
4135 OPEN_DATADIR2_SUFFIX("stats", "buffer-stats", ".tmp");
4136 OPEN_DATADIR2_SUFFIX("stats", "conn-stats", ".tmp");
4137 OPEN_DATADIR2_SUFFIX("stats", "hidserv-stats", ".tmp");
4139 OPEN_DATADIR("approved-routers");
4140 OPEN_DATADIR_SUFFIX("fingerprint", ".tmp");
4141 OPEN_DATADIR_SUFFIX("hashed-fingerprint", ".tmp");
4142 OPEN_DATADIR_SUFFIX("router-stability", ".tmp");
4144 OPEN("/etc/resolv.conf");
4146 RENAME_SUFFIX("fingerprint", ".tmp");
4147 RENAME_KEYDIR_SUFFIX("secret_onion_key_ntor", ".tmp");
4149 RENAME_KEYDIR_SUFFIX("secret_id_key", ".tmp");
4150 RENAME_KEYDIR_SUFFIX("secret_id_key.old", ".tmp");
4151 RENAME_KEYDIR_SUFFIX("secret_onion_key", ".tmp");
4152 RENAME_KEYDIR_SUFFIX("secret_onion_key.old", ".tmp");
4154 RENAME_SUFFIX2("stats", "bridge-stats", ".tmp");
4155 RENAME_SUFFIX2("stats", "dirreq-stats", ".tmp");
4156 RENAME_SUFFIX2("stats", "entry-stats", ".tmp");
4157 RENAME_SUFFIX2("stats", "exit-stats", ".tmp");
4158 RENAME_SUFFIX2("stats", "buffer-stats", ".tmp");
4159 RENAME_SUFFIX2("stats", "conn-stats", ".tmp");
4160 RENAME_SUFFIX2("stats", "hidserv-stats", ".tmp");
4161 RENAME_SUFFIX("hashed-fingerprint", ".tmp");
4162 RENAME_SUFFIX("router-stability", ".tmp");
4164 RENAME_KEYDIR_SUFFIX("ed25519_master_id_secret_key", ".tmp");
4165 RENAME_KEYDIR_SUFFIX("ed25519_master_id_secret_key_encrypted", ".tmp");
4166 RENAME_KEYDIR_SUFFIX("ed25519_master_id_public_key", ".tmp");
4167 RENAME_KEYDIR_SUFFIX("ed25519_signing_secret_key", ".tmp");
4168 RENAME_KEYDIR_SUFFIX("ed25519_signing_cert", ".tmp");
4170 sandbox_cfg_allow_rename(&cfg,
4171 get_keydir_fname("secret_onion_key"),
4172 get_keydir_fname("secret_onion_key.old"));
4173 sandbox_cfg_allow_rename(&cfg,
4174 get_keydir_fname("secret_onion_key_ntor"),
4175 get_keydir_fname("secret_onion_key_ntor.old"));
4177 STAT_KEY_DIRECTORY();
4178 OPEN_DATADIR("stats");
4179 STAT_DATADIR("stats");
4180 STAT_DATADIR2("stats", "dirreq-stats");
4182 consdiffmgr_register_with_sandbox(&cfg);
4185 init_addrinfo();
4187 return cfg;
4190 /* Main entry point for the Tor process. Called from tor_main(), and by
4191 * anybody embedding Tor. */
4193 tor_run_main(const tor_main_configuration_t *tor_cfg)
4195 int result = 0;
4197 int argc = tor_cfg->argc;
4198 char **argv = tor_cfg->argv;
4200 #ifdef _WIN32
4201 #ifndef HeapEnableTerminationOnCorruption
4202 #define HeapEnableTerminationOnCorruption 1
4203 #endif
4204 /* On heap corruption, just give up; don't try to play along. */
4205 HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0);
4206 /* Call SetProcessDEPPolicy to permanently enable DEP.
4207 The function will not resolve on earlier versions of Windows,
4208 and failure is not dangerous. */
4209 HMODULE hMod = GetModuleHandleA("Kernel32.dll");
4210 if (hMod) {
4211 typedef BOOL (WINAPI *PSETDEP)(DWORD);
4212 PSETDEP setdeppolicy = (PSETDEP)GetProcAddress(hMod,
4213 "SetProcessDEPPolicy");
4214 if (setdeppolicy) {
4215 /* PROCESS_DEP_ENABLE | PROCESS_DEP_DISABLE_ATL_THUNK_EMULATION */
4216 setdeppolicy(3);
4219 #endif /* defined(_WIN32) */
4222 int bt_err = configure_backtrace_handler(get_version());
4223 if (bt_err < 0) {
4224 log_warn(LD_BUG, "Unable to install backtrace handler: %s",
4225 strerror(-bt_err));
4228 init_protocol_warning_severity_level();
4230 update_approx_time(time(NULL));
4231 tor_threads_init();
4232 tor_compress_init();
4233 init_logging(0);
4234 monotime_init();
4235 #ifdef NT_SERVICE
4237 int done = 0;
4238 result = nt_service_parse_options(argc, argv, &done);
4239 if (done) return result;
4241 #endif /* defined(NT_SERVICE) */
4243 int init_rv = tor_init(argc, argv);
4244 if (init_rv < 0)
4245 return -1;
4246 else if (init_rv > 0)
4247 return 0;
4250 if (get_options()->Sandbox && get_options()->command == CMD_RUN_TOR) {
4251 sandbox_cfg_t* cfg = sandbox_init_filter();
4253 if (sandbox_init(cfg)) {
4254 log_err(LD_BUG,"Failed to create syscall sandbox filter");
4255 return -1;
4258 // registering libevent rng
4259 #ifdef HAVE_EVUTIL_SECURE_RNG_SET_URANDOM_DEVICE_FILE
4260 evutil_secure_rng_set_urandom_device_file(
4261 (char*) sandbox_intern_string("/dev/urandom"));
4262 #endif
4265 switch (get_options()->command) {
4266 case CMD_RUN_TOR:
4267 #ifdef NT_SERVICE
4268 nt_service_set_state(SERVICE_RUNNING);
4269 #endif
4270 result = do_main_loop();
4271 break;
4272 case CMD_KEYGEN:
4273 result = load_ed_keys(get_options(), time(NULL)) < 0;
4274 break;
4275 case CMD_KEY_EXPIRATION:
4276 init_keys();
4277 result = log_cert_expiration();
4278 break;
4279 case CMD_LIST_FINGERPRINT:
4280 result = do_list_fingerprint();
4281 break;
4282 case CMD_HASH_PASSWORD:
4283 do_hash_password();
4284 result = 0;
4285 break;
4286 case CMD_VERIFY_CONFIG:
4287 if (quiet_level == 0)
4288 printf("Configuration was valid\n");
4289 result = 0;
4290 break;
4291 case CMD_DUMP_CONFIG:
4292 result = do_dump_config();
4293 break;
4294 case CMD_RUN_UNITTESTS: /* only set by test.c */
4295 default:
4296 log_warn(LD_BUG,"Illegal command number %d: internal error.",
4297 get_options()->command);
4298 result = -1;
4300 tor_cleanup();
4301 return result;