main: Don't rescan main loop events if not initialized
[tor.git] / src / or / main.c
blob10f26c954e858a5022d04c37f220f43039b0fe5c
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-2017, 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.h"
51 #include "addressmap.h"
52 #include "backtrace.h"
53 #include "bridges.h"
54 #include "buffers.h"
55 #include "buffers_tls.h"
56 #include "channel.h"
57 #include "channeltls.h"
58 #include "channelpadding.h"
59 #include "circuitbuild.h"
60 #include "circuitlist.h"
61 #include "circuituse.h"
62 #include "command.h"
63 #include "compress.h"
64 #include "config.h"
65 #include "confparse.h"
66 #include "connection.h"
67 #include "connection_edge.h"
68 #include "connection_or.h"
69 #include "consdiffmgr.h"
70 #include "control.h"
71 #include "cpuworker.h"
72 #include "crypto_s2k.h"
73 #include "directory.h"
74 #include "dirserv.h"
75 #include "dirvote.h"
76 #include "dns.h"
77 #include "dnsserv.h"
78 #include "dos.h"
79 #include "entrynodes.h"
80 #include "geoip.h"
81 #include "hibernate.h"
82 #include "hs_cache.h"
83 #include "hs_circuitmap.h"
84 #include "hs_client.h"
85 #include "keypin.h"
86 #include "main.h"
87 #include "microdesc.h"
88 #include "networkstatus.h"
89 #include "nodelist.h"
90 #include "ntmain.h"
91 #include "onion.h"
92 #include "periodic.h"
93 #include "policies.h"
94 #include "protover.h"
95 #include "transports.h"
96 #include "relay.h"
97 #include "rendclient.h"
98 #include "rendcommon.h"
99 #include "rendservice.h"
100 #include "rephist.h"
101 #include "router.h"
102 #include "routerkeys.h"
103 #include "routerlist.h"
104 #include "routerparse.h"
105 #include "scheduler.h"
106 #include "shared_random.h"
107 #include "statefile.h"
108 #include "status.h"
109 #include "tor_api.h"
110 #include "tor_api_internal.h"
111 #include "util_process.h"
112 #include "ext_orport.h"
113 #ifdef USE_DMALLOC
114 #include <dmalloc.h>
115 #endif
116 #include "memarea.h"
117 #include "sandbox.h"
119 #include <event2/event.h>
121 #ifdef HAVE_SYSTEMD
122 # if defined(__COVERITY__) && !defined(__INCLUDE_LEVEL__)
123 /* Systemd's use of gcc's __INCLUDE_LEVEL__ extension macro appears to confuse
124 * Coverity. Here's a kludge to unconfuse it.
126 # define __INCLUDE_LEVEL__ 2
127 #endif /* defined(__COVERITY__) && !defined(__INCLUDE_LEVEL__) */
128 #include <systemd/sd-daemon.h>
129 #endif /* defined(HAVE_SYSTEMD) */
131 void evdns_shutdown(int);
133 #ifdef HAVE_RUST
134 // helper function defined in Rust to output a log message indicating if tor is
135 // running with Rust enabled. See src/rust/tor_util
136 void rust_log_welcome_string(void);
137 #endif
139 /********* PROTOTYPES **********/
141 static void dumpmemusage(int severity);
142 static void dumpstats(int severity); /* log stats */
143 static void conn_read_callback(evutil_socket_t fd, short event, void *_conn);
144 static void conn_write_callback(evutil_socket_t fd, short event, void *_conn);
145 static void second_elapsed_callback(periodic_timer_t *timer, void *args);
146 static int conn_close_if_marked(int i);
147 static void connection_start_reading_from_linked_conn(connection_t *conn);
148 static int connection_should_read_from_linked_conn(connection_t *conn);
149 static int run_main_loop_until_done(void);
150 static void process_signal(int sig);
151 static void shutdown_did_not_work_callback(evutil_socket_t fd, short event,
152 void *arg) ATTR_NORETURN;
154 /********* START VARIABLES **********/
156 /* Token bucket for all traffic. */
157 token_bucket_rw_t global_bucket;
159 /* Token bucket for relayed traffic. */
160 token_bucket_rw_t global_relayed_bucket;
162 /* DOCDOC stats_prev_n_read */
163 static uint64_t stats_prev_n_read = 0;
164 /* DOCDOC stats_prev_n_written */
165 static uint64_t stats_prev_n_written = 0;
167 /* XXX we might want to keep stats about global_relayed_*_bucket too. Or not.*/
168 /** How many bytes have we read since we started the process? */
169 static uint64_t stats_n_bytes_read = 0;
170 /** How many bytes have we written since we started the process? */
171 static uint64_t stats_n_bytes_written = 0;
172 /** What time did this process start up? */
173 time_t time_of_process_start = 0;
174 /** How many seconds have we been running? */
175 static long stats_n_seconds_working = 0;
176 /** How many times have we returned from the main loop successfully? */
177 static uint64_t stats_n_main_loop_successes = 0;
178 /** How many times have we received an error from the main loop? */
179 static uint64_t stats_n_main_loop_errors = 0;
180 /** How many times have we returned from the main loop with no events. */
181 static uint64_t stats_n_main_loop_idle = 0;
183 /** How often will we honor SIGNEWNYM requests? */
184 #define MAX_SIGNEWNYM_RATE 10
185 /** When did we last process a SIGNEWNYM request? */
186 static time_t time_of_last_signewnym = 0;
187 /** Is there a signewnym request we're currently waiting to handle? */
188 static int signewnym_is_pending = 0;
189 /** How many times have we called newnym? */
190 static unsigned newnym_epoch = 0;
192 /** Smartlist of all open connections. */
193 STATIC smartlist_t *connection_array = NULL;
194 /** List of connections that have been marked for close and need to be freed
195 * and removed from connection_array. */
196 static smartlist_t *closeable_connection_lst = NULL;
197 /** List of linked connections that are currently reading data into their
198 * inbuf from their partner's outbuf. */
199 static smartlist_t *active_linked_connection_lst = NULL;
200 /** Flag: Set to true iff we entered the current libevent main loop via
201 * <b>loop_once</b>. If so, there's no need to trigger a loopexit in order
202 * to handle linked connections. */
203 static int called_loop_once = 0;
204 /** Flag: if true, it's time to shut down, so the main loop should exit as
205 * soon as possible.
207 static int main_loop_should_exit = 0;
208 /** The return value that the main loop should yield when it exits, if
209 * main_loop_should_exit is true.
211 static int main_loop_exit_value = 0;
213 /** We set this to 1 when we've opened a circuit, so we can print a log
214 * entry to inform the user that Tor is working. We set it to 0 when
215 * we think the fact that we once opened a circuit doesn't mean we can do so
216 * any longer (a big time jump happened, when we notice our directory is
217 * heinously out-of-date, etc.
219 static int can_complete_circuits = 0;
221 /** How often do we check for router descriptors that we should download
222 * when we have too little directory info? */
223 #define GREEDY_DESCRIPTOR_RETRY_INTERVAL (10)
224 /** How often do we check for router descriptors that we should download
225 * when we have enough directory info? */
226 #define LAZY_DESCRIPTOR_RETRY_INTERVAL (60)
228 /** Decides our behavior when no logs are configured/before any
229 * logs have been configured. For 0, we log notice to stdout as normal.
230 * For 1, we log warnings only. For 2, we log nothing.
232 int quiet_level = 0;
234 /********* END VARIABLES ************/
236 /****************************************************************************
238 * This section contains accessors and other methods on the connection_array
239 * variables (which are global within this file and unavailable outside it).
241 ****************************************************************************/
243 /** Return 1 if we have successfully built a circuit, and nothing has changed
244 * to make us think that maybe we can't.
247 have_completed_a_circuit(void)
249 return can_complete_circuits;
252 /** Note that we have successfully built a circuit, so that reachability
253 * testing and introduction points and so on may be attempted. */
254 void
255 note_that_we_completed_a_circuit(void)
257 can_complete_circuits = 1;
260 /** Note that something has happened (like a clock jump, or DisableNetwork) to
261 * make us think that maybe we can't complete circuits. */
262 void
263 note_that_we_maybe_cant_complete_circuits(void)
265 can_complete_circuits = 0;
268 /** Add <b>conn</b> to the array of connections that we can poll on. The
269 * connection's socket must be set; the connection starts out
270 * non-reading and non-writing.
273 connection_add_impl(connection_t *conn, int is_connecting)
275 tor_assert(conn);
276 tor_assert(SOCKET_OK(conn->s) ||
277 conn->linked ||
278 (conn->type == CONN_TYPE_AP &&
279 TO_EDGE_CONN(conn)->is_dns_request));
281 tor_assert(conn->conn_array_index == -1); /* can only connection_add once */
282 conn->conn_array_index = smartlist_len(connection_array);
283 smartlist_add(connection_array, conn);
285 (void) is_connecting;
287 if (SOCKET_OK(conn->s) || conn->linked) {
288 conn->read_event = tor_event_new(tor_libevent_get_base(),
289 conn->s, EV_READ|EV_PERSIST, conn_read_callback, conn);
290 conn->write_event = tor_event_new(tor_libevent_get_base(),
291 conn->s, EV_WRITE|EV_PERSIST, conn_write_callback, conn);
292 /* XXXX CHECK FOR NULL RETURN! */
295 log_debug(LD_NET,"new conn type %s, socket %d, address %s, n_conns %d.",
296 conn_type_to_string(conn->type), (int)conn->s, conn->address,
297 smartlist_len(connection_array));
299 return 0;
302 /** Tell libevent that we don't care about <b>conn</b> any more. */
303 void
304 connection_unregister_events(connection_t *conn)
306 if (conn->read_event) {
307 if (event_del(conn->read_event))
308 log_warn(LD_BUG, "Error removing read event for %d", (int)conn->s);
309 tor_free(conn->read_event);
311 if (conn->write_event) {
312 if (event_del(conn->write_event))
313 log_warn(LD_BUG, "Error removing write event for %d", (int)conn->s);
314 tor_free(conn->write_event);
316 if (conn->type == CONN_TYPE_AP_DNS_LISTENER) {
317 dnsserv_close_listener(conn);
321 /** Remove the connection from the global list, and remove the
322 * corresponding poll entry. Calling this function will shift the last
323 * connection (if any) into the position occupied by conn.
326 connection_remove(connection_t *conn)
328 int current_index;
329 connection_t *tmp;
331 tor_assert(conn);
333 log_debug(LD_NET,"removing socket %d (type %s), n_conns now %d",
334 (int)conn->s, conn_type_to_string(conn->type),
335 smartlist_len(connection_array));
337 if (conn->type == CONN_TYPE_AP && conn->socket_family == AF_UNIX) {
338 log_info(LD_NET, "Closing SOCKS Unix socket connection");
341 control_event_conn_bandwidth(conn);
343 tor_assert(conn->conn_array_index >= 0);
344 current_index = conn->conn_array_index;
345 connection_unregister_events(conn); /* This is redundant, but cheap. */
346 if (current_index == smartlist_len(connection_array)-1) { /* at the end */
347 smartlist_del(connection_array, current_index);
348 return 0;
351 /* replace this one with the one at the end */
352 smartlist_del(connection_array, current_index);
353 tmp = smartlist_get(connection_array, current_index);
354 tmp->conn_array_index = current_index;
356 return 0;
359 /** If <b>conn</b> is an edge conn, remove it from the list
360 * of conn's on this circuit. If it's not on an edge,
361 * flush and send destroys for all circuits on this conn.
363 * Remove it from connection_array (if applicable) and
364 * from closeable_connection_list.
366 * Then free it.
368 static void
369 connection_unlink(connection_t *conn)
371 connection_about_to_close_connection(conn);
372 if (conn->conn_array_index >= 0) {
373 connection_remove(conn);
375 if (conn->linked_conn) {
376 conn->linked_conn->linked_conn = NULL;
377 if (! conn->linked_conn->marked_for_close &&
378 conn->linked_conn->reading_from_linked_conn)
379 connection_start_reading(conn->linked_conn);
380 conn->linked_conn = NULL;
382 smartlist_remove(closeable_connection_lst, conn);
383 smartlist_remove(active_linked_connection_lst, conn);
384 if (conn->type == CONN_TYPE_EXIT) {
385 assert_connection_edge_not_dns_pending(TO_EDGE_CONN(conn));
387 if (conn->type == CONN_TYPE_OR) {
388 if (!tor_digest_is_zero(TO_OR_CONN(conn)->identity_digest))
389 connection_or_clear_identity(TO_OR_CONN(conn));
390 /* connection_unlink() can only get called if the connection
391 * was already on the closeable list, and it got there by
392 * connection_mark_for_close(), which was called from
393 * connection_or_close_normally() or
394 * connection_or_close_for_error(), so the channel should
395 * already be in CHANNEL_STATE_CLOSING, and then the
396 * connection_about_to_close_connection() goes to
397 * connection_or_about_to_close(), which calls channel_closed()
398 * to notify the channel_t layer, and closed the channel, so
399 * nothing more to do here to deal with the channel associated
400 * with an orconn.
403 connection_free(conn);
407 * Callback: used to activate read events for all linked connections, so
408 * libevent knows to call their read callbacks. This callback run as a
409 * postloop event, so that the events _it_ activates don't happen until
410 * Libevent has a chance to check for other events.
412 static void
413 schedule_active_linked_connections_cb(mainloop_event_t *event, void *arg)
415 (void)event;
416 (void)arg;
418 /* All active linked conns should get their read events activated,
419 * so that libevent knows to run their callbacks. */
420 SMARTLIST_FOREACH(active_linked_connection_lst, connection_t *, conn,
421 event_active(conn->read_event, EV_READ, 1));
424 /** Event that invokes schedule_active_linked_connections_cb. */
425 static mainloop_event_t *schedule_active_linked_connections_event = NULL;
427 /** Initialize the global connection list, closeable connection list,
428 * and active connection list. */
429 STATIC void
430 init_connection_lists(void)
432 if (!connection_array)
433 connection_array = smartlist_new();
434 if (!closeable_connection_lst)
435 closeable_connection_lst = smartlist_new();
436 if (!active_linked_connection_lst)
437 active_linked_connection_lst = smartlist_new();
440 /** Schedule <b>conn</b> to be closed. **/
441 void
442 add_connection_to_closeable_list(connection_t *conn)
444 tor_assert(!smartlist_contains(closeable_connection_lst, conn));
445 tor_assert(conn->marked_for_close);
446 assert_connection_ok(conn, time(NULL));
447 smartlist_add(closeable_connection_lst, conn);
448 mainloop_schedule_postloop_cleanup();
451 /** Return 1 if conn is on the closeable list, else return 0. */
453 connection_is_on_closeable_list(connection_t *conn)
455 return smartlist_contains(closeable_connection_lst, conn);
458 /** Return true iff conn is in the current poll array. */
460 connection_in_array(connection_t *conn)
462 return smartlist_contains(connection_array, conn);
465 /** Set <b>*array</b> to an array of all connections. <b>*array</b> must not
466 * be modified.
468 MOCK_IMPL(smartlist_t *,
469 get_connection_array, (void))
471 if (!connection_array)
472 connection_array = smartlist_new();
473 return connection_array;
477 * Return the amount of network traffic read, in bytes, over the life of this
478 * process.
480 MOCK_IMPL(uint64_t,
481 get_bytes_read,(void))
483 return stats_n_bytes_read;
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_written,(void))
493 return stats_n_bytes_written;
497 * Increment the amount of network traffic read and written, over the life of
498 * this process.
500 void
501 stats_increment_bytes_read_and_written(uint64_t r, uint64_t w)
503 stats_n_bytes_read += r;
504 stats_n_bytes_written += w;
507 /** Set the event mask on <b>conn</b> to <b>events</b>. (The event
508 * mask is a bitmask whose bits are READ_EVENT and WRITE_EVENT)
510 void
511 connection_watch_events(connection_t *conn, watchable_events_t events)
513 if (events & READ_EVENT)
514 connection_start_reading(conn);
515 else
516 connection_stop_reading(conn);
518 if (events & WRITE_EVENT)
519 connection_start_writing(conn);
520 else
521 connection_stop_writing(conn);
524 /** Return true iff <b>conn</b> is listening for read events. */
526 connection_is_reading(connection_t *conn)
528 tor_assert(conn);
530 return conn->reading_from_linked_conn ||
531 (conn->read_event && event_pending(conn->read_event, EV_READ, NULL));
534 /** Reset our main loop counters. */
535 void
536 reset_main_loop_counters(void)
538 stats_n_main_loop_successes = 0;
539 stats_n_main_loop_errors = 0;
540 stats_n_main_loop_idle = 0;
543 /** Increment the main loop success counter. */
544 static void
545 increment_main_loop_success_count(void)
547 ++stats_n_main_loop_successes;
550 /** Get the main loop success counter. */
551 uint64_t
552 get_main_loop_success_count(void)
554 return stats_n_main_loop_successes;
557 /** Increment the main loop error counter. */
558 static void
559 increment_main_loop_error_count(void)
561 ++stats_n_main_loop_errors;
564 /** Get the main loop error counter. */
565 uint64_t
566 get_main_loop_error_count(void)
568 return stats_n_main_loop_errors;
571 /** Increment the main loop idle counter. */
572 static void
573 increment_main_loop_idle_count(void)
575 ++stats_n_main_loop_idle;
578 /** Get the main loop idle counter. */
579 uint64_t
580 get_main_loop_idle_count(void)
582 return stats_n_main_loop_idle;
585 /** Check whether <b>conn</b> is correct in having (or not having) a
586 * read/write event (passed in <b>ev</b>). On success, return 0. On failure,
587 * log a warning and return -1. */
588 static int
589 connection_check_event(connection_t *conn, struct event *ev)
591 int bad;
593 if (conn->type == CONN_TYPE_AP && TO_EDGE_CONN(conn)->is_dns_request) {
594 /* DNS requests which we launch through the dnsserv.c module do not have
595 * any underlying socket or any underlying linked connection, so they
596 * shouldn't have any attached events either.
598 bad = ev != NULL;
599 } else {
600 /* Everything else should have an underlying socket, or a linked
601 * connection (which is also tracked with a read_event/write_event pair).
603 bad = ev == NULL;
606 if (bad) {
607 log_warn(LD_BUG, "Event missing on connection %p [%s;%s]. "
608 "socket=%d. linked=%d. "
609 "is_dns_request=%d. Marked_for_close=%s:%d",
610 conn,
611 conn_type_to_string(conn->type),
612 conn_state_to_string(conn->type, conn->state),
613 (int)conn->s, (int)conn->linked,
614 (conn->type == CONN_TYPE_AP &&
615 TO_EDGE_CONN(conn)->is_dns_request),
616 conn->marked_for_close_file ? conn->marked_for_close_file : "-",
617 conn->marked_for_close
619 log_backtrace(LOG_WARN, LD_BUG, "Backtrace attached.");
620 return -1;
622 return 0;
625 /** Tell the main loop to stop notifying <b>conn</b> of any read events. */
626 MOCK_IMPL(void,
627 connection_stop_reading,(connection_t *conn))
629 tor_assert(conn);
631 if (connection_check_event(conn, conn->read_event) < 0) {
632 return;
635 if (conn->linked) {
636 conn->reading_from_linked_conn = 0;
637 connection_stop_reading_from_linked_conn(conn);
638 } else {
639 if (event_del(conn->read_event))
640 log_warn(LD_NET, "Error from libevent setting read event state for %d "
641 "to unwatched: %s",
642 (int)conn->s,
643 tor_socket_strerror(tor_socket_errno(conn->s)));
647 /** Tell the main loop to start notifying <b>conn</b> of any read events. */
648 MOCK_IMPL(void,
649 connection_start_reading,(connection_t *conn))
651 tor_assert(conn);
653 if (connection_check_event(conn, conn->read_event) < 0) {
654 return;
657 if (conn->linked) {
658 conn->reading_from_linked_conn = 1;
659 if (connection_should_read_from_linked_conn(conn))
660 connection_start_reading_from_linked_conn(conn);
661 } else {
662 if (event_add(conn->read_event, NULL))
663 log_warn(LD_NET, "Error from libevent setting read event state for %d "
664 "to watched: %s",
665 (int)conn->s,
666 tor_socket_strerror(tor_socket_errno(conn->s)));
670 /** Return true iff <b>conn</b> is listening for write events. */
672 connection_is_writing(connection_t *conn)
674 tor_assert(conn);
676 return conn->writing_to_linked_conn ||
677 (conn->write_event && event_pending(conn->write_event, EV_WRITE, NULL));
680 /** Tell the main loop to stop notifying <b>conn</b> of any write events. */
681 MOCK_IMPL(void,
682 connection_stop_writing,(connection_t *conn))
684 tor_assert(conn);
686 if (connection_check_event(conn, conn->write_event) < 0) {
687 return;
690 if (conn->linked) {
691 conn->writing_to_linked_conn = 0;
692 if (conn->linked_conn)
693 connection_stop_reading_from_linked_conn(conn->linked_conn);
694 } else {
695 if (event_del(conn->write_event))
696 log_warn(LD_NET, "Error from libevent setting write event state for %d "
697 "to unwatched: %s",
698 (int)conn->s,
699 tor_socket_strerror(tor_socket_errno(conn->s)));
703 /** Tell the main loop to start notifying <b>conn</b> of any write events. */
704 MOCK_IMPL(void,
705 connection_start_writing,(connection_t *conn))
707 tor_assert(conn);
709 if (connection_check_event(conn, conn->write_event) < 0) {
710 return;
713 if (conn->linked) {
714 conn->writing_to_linked_conn = 1;
715 if (conn->linked_conn &&
716 connection_should_read_from_linked_conn(conn->linked_conn))
717 connection_start_reading_from_linked_conn(conn->linked_conn);
718 } else {
719 if (event_add(conn->write_event, NULL))
720 log_warn(LD_NET, "Error from libevent setting write event state for %d "
721 "to watched: %s",
722 (int)conn->s,
723 tor_socket_strerror(tor_socket_errno(conn->s)));
727 /** Return true iff <b>conn</b> is linked conn, and reading from the conn
728 * linked to it would be good and feasible. (Reading is "feasible" if the
729 * other conn exists and has data in its outbuf, and is "good" if we have our
730 * reading_from_linked_conn flag set and the other conn has its
731 * writing_to_linked_conn flag set.)*/
732 static int
733 connection_should_read_from_linked_conn(connection_t *conn)
735 if (conn->linked && conn->reading_from_linked_conn) {
736 if (! conn->linked_conn ||
737 (conn->linked_conn->writing_to_linked_conn &&
738 buf_datalen(conn->linked_conn->outbuf)))
739 return 1;
741 return 0;
744 /** Event to run 'shutdown did not work callback'. */
745 static struct event *shutdown_did_not_work_event = NULL;
747 /** Failsafe measure that should never actually be necessary: If
748 * tor_shutdown_event_loop_and_exit() somehow doesn't successfully exit the
749 * event loop, then this callback will kill Tor with an assertion failure
750 * seconds later
752 static void
753 shutdown_did_not_work_callback(evutil_socket_t fd, short event, void *arg)
755 // LCOV_EXCL_START
756 (void) fd;
757 (void) event;
758 (void) arg;
759 tor_assert_unreached();
760 // LCOV_EXCL_STOP
763 #ifdef ENABLE_RESTART_DEBUGGING
764 static struct event *tor_shutdown_event_loop_for_restart_event = NULL;
765 static void
766 tor_shutdown_event_loop_for_restart_cb(
767 evutil_socket_t fd, short event, void *arg)
769 (void)fd;
770 (void)event;
771 (void)arg;
772 tor_event_free(tor_shutdown_event_loop_for_restart_event);
773 tor_shutdown_event_loop_and_exit(0);
775 #endif
778 * After finishing the current callback (if any), shut down the main loop,
779 * clean up the process, and exit with <b>exitcode</b>.
781 void
782 tor_shutdown_event_loop_and_exit(int exitcode)
784 if (main_loop_should_exit)
785 return; /* Ignore multiple calls to this function. */
787 main_loop_should_exit = 1;
788 main_loop_exit_value = exitcode;
790 /* Die with an assertion failure in ten seconds, if for some reason we don't
791 * exit normally. */
792 /* XXXX We should consider this code if it's never used. */
793 struct timeval ten_seconds = { 10, 0 };
794 shutdown_did_not_work_event = tor_evtimer_new(
795 tor_libevent_get_base(),
796 shutdown_did_not_work_callback, NULL);
797 event_add(shutdown_did_not_work_event, &ten_seconds);
799 /* Unlike exit_loop_after_delay(), exit_loop_after_callback
800 * prevents other callbacks from running. */
801 tor_libevent_exit_loop_after_callback(tor_libevent_get_base());
804 /** Return true iff tor_shutdown_event_loop_and_exit() has been called. */
806 tor_event_loop_shutdown_is_pending(void)
808 return main_loop_should_exit;
811 /** Helper: Tell the main loop to begin reading bytes into <b>conn</b> from
812 * its linked connection, if it is not doing so already. Called by
813 * connection_start_reading and connection_start_writing as appropriate. */
814 static void
815 connection_start_reading_from_linked_conn(connection_t *conn)
817 tor_assert(conn);
818 tor_assert(conn->linked == 1);
820 if (!conn->active_on_link) {
821 conn->active_on_link = 1;
822 smartlist_add(active_linked_connection_lst, conn);
823 mainloop_event_activate(schedule_active_linked_connections_event);
824 } else {
825 tor_assert(smartlist_contains(active_linked_connection_lst, conn));
829 /** Tell the main loop to stop reading bytes into <b>conn</b> from its linked
830 * connection, if is currently doing so. Called by connection_stop_reading,
831 * connection_stop_writing, and connection_read. */
832 void
833 connection_stop_reading_from_linked_conn(connection_t *conn)
835 tor_assert(conn);
836 tor_assert(conn->linked == 1);
838 if (conn->active_on_link) {
839 conn->active_on_link = 0;
840 /* FFFF We could keep an index here so we can smartlist_del
841 * cleanly. On the other hand, this doesn't show up on profiles,
842 * so let's leave it alone for now. */
843 smartlist_remove(active_linked_connection_lst, conn);
844 } else {
845 tor_assert(!smartlist_contains(active_linked_connection_lst, conn));
849 /** Close all connections that have been scheduled to get closed. */
850 STATIC void
851 close_closeable_connections(void)
853 int i;
854 for (i = 0; i < smartlist_len(closeable_connection_lst); ) {
855 connection_t *conn = smartlist_get(closeable_connection_lst, i);
856 if (conn->conn_array_index < 0) {
857 connection_unlink(conn); /* blow it away right now */
858 } else {
859 if (!conn_close_if_marked(conn->conn_array_index))
860 ++i;
865 /** Count moribund connections for the OOS handler */
866 MOCK_IMPL(int,
867 connection_count_moribund, (void))
869 int moribund = 0;
872 * Count things we'll try to kill when close_closeable_connections()
873 * runs next.
875 SMARTLIST_FOREACH_BEGIN(closeable_connection_lst, connection_t *, conn) {
876 if (SOCKET_OK(conn->s) && connection_is_moribund(conn)) ++moribund;
877 } SMARTLIST_FOREACH_END(conn);
879 return moribund;
882 /** Libevent callback: this gets invoked when (connection_t*)<b>conn</b> has
883 * some data to read. */
884 static void
885 conn_read_callback(evutil_socket_t fd, short event, void *_conn)
887 connection_t *conn = _conn;
888 (void)fd;
889 (void)event;
891 log_debug(LD_NET,"socket %d wants to read.",(int)conn->s);
893 /* assert_connection_ok(conn, time(NULL)); */
895 if (connection_handle_read(conn) < 0) {
896 if (!conn->marked_for_close) {
897 #ifndef _WIN32
898 log_warn(LD_BUG,"Unhandled error on read for %s connection "
899 "(fd %d); removing",
900 conn_type_to_string(conn->type), (int)conn->s);
901 tor_fragile_assert();
902 #endif /* !defined(_WIN32) */
903 if (CONN_IS_EDGE(conn))
904 connection_edge_end_errno(TO_EDGE_CONN(conn));
905 connection_mark_for_close(conn);
908 assert_connection_ok(conn, time(NULL));
910 if (smartlist_len(closeable_connection_lst))
911 close_closeable_connections();
914 /** Libevent callback: this gets invoked when (connection_t*)<b>conn</b> has
915 * some data to write. */
916 static void
917 conn_write_callback(evutil_socket_t fd, short events, void *_conn)
919 connection_t *conn = _conn;
920 (void)fd;
921 (void)events;
923 LOG_FN_CONN(conn, (LOG_DEBUG, LD_NET, "socket %d wants to write.",
924 (int)conn->s));
926 /* assert_connection_ok(conn, time(NULL)); */
928 if (connection_handle_write(conn, 0) < 0) {
929 if (!conn->marked_for_close) {
930 /* this connection is broken. remove it. */
931 log_fn(LOG_WARN,LD_BUG,
932 "unhandled error on write for %s connection (fd %d); removing",
933 conn_type_to_string(conn->type), (int)conn->s);
934 tor_fragile_assert();
935 if (CONN_IS_EDGE(conn)) {
936 /* otherwise we cry wolf about duplicate close */
937 edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
938 if (!edge_conn->end_reason)
939 edge_conn->end_reason = END_STREAM_REASON_INTERNAL;
940 edge_conn->edge_has_sent_end = 1;
942 connection_close_immediate(conn); /* So we don't try to flush. */
943 connection_mark_for_close(conn);
946 assert_connection_ok(conn, time(NULL));
948 if (smartlist_len(closeable_connection_lst))
949 close_closeable_connections();
952 /** If the connection at connection_array[i] is marked for close, then:
953 * - If it has data that it wants to flush, try to flush it.
954 * - If it _still_ has data to flush, and conn->hold_open_until_flushed is
955 * true, then leave the connection open and return.
956 * - Otherwise, remove the connection from connection_array and from
957 * all other lists, close it, and free it.
958 * Returns 1 if the connection was closed, 0 otherwise.
960 static int
961 conn_close_if_marked(int i)
963 connection_t *conn;
964 int retval;
965 time_t now;
967 conn = smartlist_get(connection_array, i);
968 if (!conn->marked_for_close)
969 return 0; /* nothing to see here, move along */
970 now = time(NULL);
971 assert_connection_ok(conn, now);
972 /* assert_all_pending_dns_resolves_ok(); */
974 log_debug(LD_NET,"Cleaning up connection (fd "TOR_SOCKET_T_FORMAT").",
975 conn->s);
977 /* If the connection we are about to close was trying to connect to
978 a proxy server and failed, the client won't be able to use that
979 proxy. We should warn the user about this. */
980 if (conn->proxy_state == PROXY_INFANT)
981 log_failed_proxy_connection(conn);
983 if ((SOCKET_OK(conn->s) || conn->linked_conn) &&
984 connection_wants_to_flush(conn)) {
985 /* s == -1 means it's an incomplete edge connection, or that the socket
986 * has already been closed as unflushable. */
987 ssize_t sz = connection_bucket_write_limit(conn, now);
988 if (!conn->hold_open_until_flushed)
989 log_info(LD_NET,
990 "Conn (addr %s, fd %d, type %s, state %d) marked, but wants "
991 "to flush %d bytes. (Marked at %s:%d)",
992 escaped_safe_str_client(conn->address),
993 (int)conn->s, conn_type_to_string(conn->type), conn->state,
994 (int)conn->outbuf_flushlen,
995 conn->marked_for_close_file, conn->marked_for_close);
996 if (conn->linked_conn) {
997 retval = buf_move_to_buf(conn->linked_conn->inbuf, conn->outbuf,
998 &conn->outbuf_flushlen);
999 if (retval >= 0) {
1000 /* The linked conn will notice that it has data when it notices that
1001 * we're gone. */
1002 connection_start_reading_from_linked_conn(conn->linked_conn);
1004 log_debug(LD_GENERAL, "Flushed last %d bytes from a linked conn; "
1005 "%d left; flushlen %d; wants-to-flush==%d", retval,
1006 (int)connection_get_outbuf_len(conn),
1007 (int)conn->outbuf_flushlen,
1008 connection_wants_to_flush(conn));
1009 } else if (connection_speaks_cells(conn)) {
1010 if (conn->state == OR_CONN_STATE_OPEN) {
1011 retval = buf_flush_to_tls(conn->outbuf, TO_OR_CONN(conn)->tls, sz,
1012 &conn->outbuf_flushlen);
1013 } else
1014 retval = -1; /* never flush non-open broken tls connections */
1015 } else {
1016 retval = buf_flush_to_socket(conn->outbuf, conn->s, sz,
1017 &conn->outbuf_flushlen);
1019 if (retval >= 0 && /* Technically, we could survive things like
1020 TLS_WANT_WRITE here. But don't bother for now. */
1021 conn->hold_open_until_flushed && connection_wants_to_flush(conn)) {
1022 if (retval > 0) {
1023 LOG_FN_CONN(conn, (LOG_INFO,LD_NET,
1024 "Holding conn (fd %d) open for more flushing.",
1025 (int)conn->s));
1026 conn->timestamp_last_write_allowed = now; /* reset so we can flush
1027 * more */
1028 } else if (sz == 0) {
1029 /* Also, retval==0. If we get here, we didn't want to write anything
1030 * (because of rate-limiting) and we didn't. */
1032 /* Connection must flush before closing, but it's being rate-limited.
1033 * Let's remove from Libevent, and mark it as blocked on bandwidth
1034 * so it will be re-added on next token bucket refill. Prevents
1035 * busy Libevent loops where we keep ending up here and returning
1036 * 0 until we are no longer blocked on bandwidth.
1038 connection_consider_empty_read_buckets(conn);
1039 connection_consider_empty_write_buckets(conn);
1041 /* Make sure that consider_empty_buckets really disabled the
1042 * connection: */
1043 if (BUG(connection_is_writing(conn))) {
1044 connection_write_bw_exhausted(conn, true);
1046 if (BUG(connection_is_reading(conn))) {
1047 /* XXXX+ We should make this code unreachable; if a connection is
1048 * marked for close and flushing, there is no point in reading to it
1049 * at all. Further, checking at this point is a bit of a hack: it
1050 * would make much more sense to react in
1051 * connection_handle_read_impl, or to just stop reading in
1052 * mark_and_flush */
1053 connection_read_bw_exhausted(conn, true/* kludge. */);
1056 return 0;
1058 if (connection_wants_to_flush(conn)) {
1059 log_fn(LOG_INFO, LD_NET, "We stalled too much while trying to write %d "
1060 "bytes to address %s. If this happens a lot, either "
1061 "something is wrong with your network connection, or "
1062 "something is wrong with theirs. "
1063 "(fd %d, type %s, state %d, marked at %s:%d).",
1064 (int)connection_get_outbuf_len(conn),
1065 escaped_safe_str_client(conn->address),
1066 (int)conn->s, conn_type_to_string(conn->type), conn->state,
1067 conn->marked_for_close_file,
1068 conn->marked_for_close);
1072 connection_unlink(conn); /* unlink, remove, free */
1073 return 1;
1076 /** Implementation for directory_all_unreachable. This is done in a callback,
1077 * since otherwise it would complicate Tor's control-flow graph beyond all
1078 * reason.
1080 static void
1081 directory_all_unreachable_cb(mainloop_event_t *event, void *arg)
1083 (void)event;
1084 (void)arg;
1086 connection_t *conn;
1088 while ((conn = connection_get_by_type_state(CONN_TYPE_AP,
1089 AP_CONN_STATE_CIRCUIT_WAIT))) {
1090 entry_connection_t *entry_conn = TO_ENTRY_CONN(conn);
1091 log_notice(LD_NET,
1092 "Is your network connection down? "
1093 "Failing connection to '%s:%d'.",
1094 safe_str_client(entry_conn->socks_request->address),
1095 entry_conn->socks_request->port);
1096 connection_mark_unattached_ap(entry_conn,
1097 END_STREAM_REASON_NET_UNREACHABLE);
1099 control_event_general_error("DIR_ALL_UNREACHABLE");
1102 static mainloop_event_t *directory_all_unreachable_cb_event = NULL;
1104 /** We've just tried every dirserver we know about, and none of
1105 * them were reachable. Assume the network is down. Change state
1106 * so next time an application connection arrives we'll delay it
1107 * and try another directory fetch. Kill off all the circuit_wait
1108 * streams that are waiting now, since they will all timeout anyway.
1110 void
1111 directory_all_unreachable(time_t now)
1113 (void)now;
1115 reset_uptime(); /* reset it */
1117 if (!directory_all_unreachable_cb_event) {
1118 directory_all_unreachable_cb_event =
1119 mainloop_event_new(directory_all_unreachable_cb, NULL);
1120 tor_assert(directory_all_unreachable_cb_event);
1123 mainloop_event_activate(directory_all_unreachable_cb_event);
1126 /** This function is called whenever we successfully pull down some new
1127 * network statuses or server descriptors. */
1128 void
1129 directory_info_has_arrived(time_t now, int from_cache, int suppress_logs)
1131 const or_options_t *options = get_options();
1133 /* if we have enough dir info, then update our guard status with
1134 * whatever we just learned. */
1135 int invalidate_circs = guards_update_all();
1137 if (invalidate_circs) {
1138 circuit_mark_all_unused_circs();
1139 circuit_mark_all_dirty_circs_as_unusable();
1142 if (!router_have_minimum_dir_info()) {
1143 int quiet = suppress_logs || from_cache ||
1144 directory_too_idle_to_fetch_descriptors(options, now);
1145 tor_log(quiet ? LOG_INFO : LOG_NOTICE, LD_DIR,
1146 "I learned some more directory information, but not enough to "
1147 "build a circuit: %s", get_dir_info_status_string());
1148 update_all_descriptor_downloads(now);
1149 return;
1150 } else {
1151 if (directory_fetches_from_authorities(options)) {
1152 update_all_descriptor_downloads(now);
1155 /* Don't even bother trying to get extrainfo until the rest of our
1156 * directory info is up-to-date */
1157 if (options->DownloadExtraInfo)
1158 update_extrainfo_downloads(now);
1161 if (server_mode(options) && !net_is_disabled() && !from_cache &&
1162 (have_completed_a_circuit() || !any_predicted_circuits(now)))
1163 router_do_reachability_checks(1, 1);
1166 /** Perform regular maintenance tasks for a single connection. This
1167 * function gets run once per second per connection by run_scheduled_events.
1169 static void
1170 run_connection_housekeeping(int i, time_t now)
1172 cell_t cell;
1173 connection_t *conn = smartlist_get(connection_array, i);
1174 const or_options_t *options = get_options();
1175 or_connection_t *or_conn;
1176 channel_t *chan = NULL;
1177 int have_any_circuits;
1178 int past_keepalive =
1179 now >= conn->timestamp_last_write_allowed + options->KeepalivePeriod;
1181 if (conn->outbuf && !connection_get_outbuf_len(conn) &&
1182 conn->type == CONN_TYPE_OR)
1183 TO_OR_CONN(conn)->timestamp_lastempty = now;
1185 if (conn->marked_for_close) {
1186 /* nothing to do here */
1187 return;
1190 /* Expire any directory connections that haven't been active (sent
1191 * if a server or received if a client) for 5 min */
1192 if (conn->type == CONN_TYPE_DIR &&
1193 ((DIR_CONN_IS_SERVER(conn) &&
1194 conn->timestamp_last_write_allowed
1195 + options->TestingDirConnectionMaxStall < now) ||
1196 (!DIR_CONN_IS_SERVER(conn) &&
1197 conn->timestamp_last_read_allowed
1198 + options->TestingDirConnectionMaxStall < now))) {
1199 log_info(LD_DIR,"Expiring wedged directory conn (fd %d, purpose %d)",
1200 (int)conn->s, conn->purpose);
1201 /* This check is temporary; it's to let us know whether we should consider
1202 * parsing partial serverdesc responses. */
1203 if (conn->purpose == DIR_PURPOSE_FETCH_SERVERDESC &&
1204 connection_get_inbuf_len(conn) >= 1024) {
1205 log_info(LD_DIR,"Trying to extract information from wedged server desc "
1206 "download.");
1207 connection_dir_reached_eof(TO_DIR_CONN(conn));
1208 } else {
1209 connection_mark_for_close(conn);
1211 return;
1214 if (!connection_speaks_cells(conn))
1215 return; /* we're all done here, the rest is just for OR conns */
1217 /* If we haven't flushed to an OR connection for a while, then either nuke
1218 the connection or send a keepalive, depending. */
1220 or_conn = TO_OR_CONN(conn);
1221 tor_assert(conn->outbuf);
1223 chan = TLS_CHAN_TO_BASE(or_conn->chan);
1224 tor_assert(chan);
1226 if (channel_num_circuits(chan) != 0) {
1227 have_any_circuits = 1;
1228 chan->timestamp_last_had_circuits = now;
1229 } else {
1230 have_any_circuits = 0;
1233 if (channel_is_bad_for_new_circs(TLS_CHAN_TO_BASE(or_conn->chan)) &&
1234 ! have_any_circuits) {
1235 /* It's bad for new circuits, and has no unmarked circuits on it:
1236 * mark it now. */
1237 log_info(LD_OR,
1238 "Expiring non-used OR connection to fd %d (%s:%d) [Too old].",
1239 (int)conn->s, conn->address, conn->port);
1240 if (conn->state == OR_CONN_STATE_CONNECTING)
1241 connection_or_connect_failed(TO_OR_CONN(conn),
1242 END_OR_CONN_REASON_TIMEOUT,
1243 "Tor gave up on the connection");
1244 connection_or_close_normally(TO_OR_CONN(conn), 1);
1245 } else if (!connection_state_is_open(conn)) {
1246 if (past_keepalive) {
1247 /* We never managed to actually get this connection open and happy. */
1248 log_info(LD_OR,"Expiring non-open OR connection to fd %d (%s:%d).",
1249 (int)conn->s,conn->address, conn->port);
1250 connection_or_close_normally(TO_OR_CONN(conn), 0);
1252 } else if (we_are_hibernating() &&
1253 ! have_any_circuits &&
1254 !connection_get_outbuf_len(conn)) {
1255 /* We're hibernating, there's no circuits, and nothing to flush.*/
1256 log_info(LD_OR,"Expiring non-used OR connection to fd %d (%s:%d) "
1257 "[Hibernating or exiting].",
1258 (int)conn->s,conn->address, conn->port);
1259 connection_or_close_normally(TO_OR_CONN(conn), 1);
1260 } else if (!have_any_circuits &&
1261 now - or_conn->idle_timeout >=
1262 chan->timestamp_last_had_circuits) {
1263 log_info(LD_OR,"Expiring non-used OR connection "U64_FORMAT" to fd %d "
1264 "(%s:%d) [no circuits for %d; timeout %d; %scanonical].",
1265 U64_PRINTF_ARG(chan->global_identifier),
1266 (int)conn->s, conn->address, conn->port,
1267 (int)(now - chan->timestamp_last_had_circuits),
1268 or_conn->idle_timeout,
1269 or_conn->is_canonical ? "" : "non");
1270 connection_or_close_normally(TO_OR_CONN(conn), 0);
1271 } else if (
1272 now >= or_conn->timestamp_lastempty + options->KeepalivePeriod*10 &&
1273 now >=
1274 conn->timestamp_last_write_allowed + options->KeepalivePeriod*10) {
1275 log_fn(LOG_PROTOCOL_WARN,LD_PROTOCOL,
1276 "Expiring stuck OR connection to fd %d (%s:%d). (%d bytes to "
1277 "flush; %d seconds since last write)",
1278 (int)conn->s, conn->address, conn->port,
1279 (int)connection_get_outbuf_len(conn),
1280 (int)(now-conn->timestamp_last_write_allowed));
1281 connection_or_close_normally(TO_OR_CONN(conn), 0);
1282 } else if (past_keepalive && !connection_get_outbuf_len(conn)) {
1283 /* send a padding cell */
1284 log_fn(LOG_DEBUG,LD_OR,"Sending keepalive to (%s:%d)",
1285 conn->address, conn->port);
1286 memset(&cell,0,sizeof(cell_t));
1287 cell.command = CELL_PADDING;
1288 connection_or_write_cell_to_buf(&cell, or_conn);
1289 } else {
1290 channelpadding_decide_to_pad_channel(chan);
1294 /** Honor a NEWNYM request: make future requests unlinkable to past
1295 * requests. */
1296 static void
1297 signewnym_impl(time_t now)
1299 const or_options_t *options = get_options();
1300 if (!proxy_mode(options)) {
1301 log_info(LD_CONTROL, "Ignoring SIGNAL NEWNYM because client functionality "
1302 "is disabled.");
1303 return;
1306 circuit_mark_all_dirty_circs_as_unusable();
1307 addressmap_clear_transient();
1308 hs_client_purge_state();
1309 time_of_last_signewnym = now;
1310 signewnym_is_pending = 0;
1312 ++newnym_epoch;
1314 control_event_signal(SIGNEWNYM);
1317 /** Return the number of times that signewnym has been called. */
1318 unsigned
1319 get_signewnym_epoch(void)
1321 return newnym_epoch;
1324 /** True iff we have initialized all the members of <b>periodic_events</b>.
1325 * Used to prevent double-initialization. */
1326 static int periodic_events_initialized = 0;
1328 /* Declare all the timer callback functions... */
1329 #undef CALLBACK
1330 #define CALLBACK(name) \
1331 static int name ## _callback(time_t, const or_options_t *)
1332 CALLBACK(add_entropy);
1333 CALLBACK(check_authority_cert);
1334 CALLBACK(check_canonical_channels);
1335 CALLBACK(check_descriptor);
1336 CALLBACK(check_dns_honesty);
1337 CALLBACK(check_ed_keys);
1338 CALLBACK(check_expired_networkstatus);
1339 CALLBACK(check_for_reachability_bw);
1340 CALLBACK(check_onion_keys_expiry_time);
1341 CALLBACK(clean_caches);
1342 CALLBACK(clean_consdiffmgr);
1343 CALLBACK(downrate_stability);
1344 CALLBACK(expire_old_ciruits_serverside);
1345 CALLBACK(fetch_networkstatus);
1346 CALLBACK(heartbeat);
1347 CALLBACK(hs_service);
1348 CALLBACK(launch_descriptor_fetches);
1349 CALLBACK(launch_reachability_tests);
1350 CALLBACK(record_bridge_stats);
1351 CALLBACK(rend_cache_failure_clean);
1352 CALLBACK(reset_padding_counts);
1353 CALLBACK(retry_dns);
1354 CALLBACK(retry_listeners);
1355 CALLBACK(rotate_onion_key);
1356 CALLBACK(rotate_x509_certificate);
1357 CALLBACK(save_stability);
1358 CALLBACK(write_bridge_ns);
1359 CALLBACK(write_stats_file);
1361 #undef CALLBACK
1363 /* Now we declare an array of periodic_event_item_t for each periodic event */
1364 #define CALLBACK(name, r, f) PERIODIC_EVENT(name, r, f)
1366 STATIC periodic_event_item_t periodic_events[] = {
1367 /* Everyone needs to run those. */
1368 CALLBACK(add_entropy, PERIODIC_EVENT_ROLE_ALL, 0),
1369 CALLBACK(check_expired_networkstatus, PERIODIC_EVENT_ROLE_ALL, 0),
1370 CALLBACK(clean_caches, PERIODIC_EVENT_ROLE_ALL, 0),
1371 CALLBACK(fetch_networkstatus, PERIODIC_EVENT_ROLE_ALL,
1372 PERIODIC_EVENT_FLAG_NEED_NET),
1373 CALLBACK(heartbeat, PERIODIC_EVENT_ROLE_ALL, 0),
1374 CALLBACK(launch_descriptor_fetches, PERIODIC_EVENT_ROLE_ALL,
1375 PERIODIC_EVENT_FLAG_NEED_NET),
1376 CALLBACK(reset_padding_counts, PERIODIC_EVENT_ROLE_ALL, 0),
1377 CALLBACK(retry_listeners, PERIODIC_EVENT_ROLE_ALL,
1378 PERIODIC_EVENT_FLAG_NEED_NET),
1379 CALLBACK(rotate_x509_certificate, PERIODIC_EVENT_ROLE_ALL, 0),
1380 CALLBACK(write_stats_file, PERIODIC_EVENT_ROLE_ALL, 0),
1382 /* Routers (bridge and relay) only. */
1383 CALLBACK(check_descriptor, PERIODIC_EVENT_ROLE_ROUTER,
1384 PERIODIC_EVENT_FLAG_NEED_NET),
1385 CALLBACK(check_ed_keys, PERIODIC_EVENT_ROLE_ROUTER, 0),
1386 CALLBACK(check_for_reachability_bw, PERIODIC_EVENT_ROLE_ROUTER,
1387 PERIODIC_EVENT_FLAG_NEED_NET),
1388 CALLBACK(check_onion_keys_expiry_time, PERIODIC_EVENT_ROLE_ROUTER, 0),
1389 CALLBACK(clean_consdiffmgr, PERIODIC_EVENT_ROLE_ROUTER, 0),
1390 CALLBACK(expire_old_ciruits_serverside, PERIODIC_EVENT_ROLE_ROUTER,
1391 PERIODIC_EVENT_FLAG_NEED_NET),
1392 CALLBACK(retry_dns, PERIODIC_EVENT_ROLE_ROUTER, 0),
1393 CALLBACK(rotate_onion_key, PERIODIC_EVENT_ROLE_ROUTER, 0),
1395 /* Authorities (bridge and directory) only. */
1396 CALLBACK(downrate_stability, PERIODIC_EVENT_ROLE_AUTHORITIES, 0),
1397 CALLBACK(launch_reachability_tests, PERIODIC_EVENT_ROLE_AUTHORITIES,
1398 PERIODIC_EVENT_FLAG_NEED_NET),
1399 CALLBACK(save_stability, PERIODIC_EVENT_ROLE_AUTHORITIES, 0),
1401 /* Directory authority only. */
1402 CALLBACK(check_authority_cert, PERIODIC_EVENT_ROLE_DIRAUTH, 0),
1404 /* Relay only. */
1405 CALLBACK(check_canonical_channels, PERIODIC_EVENT_ROLE_RELAY,
1406 PERIODIC_EVENT_FLAG_NEED_NET),
1407 CALLBACK(check_dns_honesty, PERIODIC_EVENT_ROLE_RELAY,
1408 PERIODIC_EVENT_FLAG_NEED_NET),
1410 /* Hidden Service service only. */
1411 CALLBACK(hs_service, PERIODIC_EVENT_ROLE_HS_SERVICE,
1412 PERIODIC_EVENT_FLAG_NEED_NET),
1414 /* Bridge only. */
1415 CALLBACK(record_bridge_stats, PERIODIC_EVENT_ROLE_BRIDGE, 0),
1417 /* Client only. */
1418 CALLBACK(rend_cache_failure_clean, PERIODIC_EVENT_ROLE_CLIENT, 0),
1420 /* Bridge Authority only. */
1421 CALLBACK(write_bridge_ns, PERIODIC_EVENT_ROLE_BRIDGEAUTH, 0),
1423 END_OF_PERIODIC_EVENTS
1425 #undef CALLBACK
1427 /* These are pointers to members of periodic_events[] that are used to
1428 * implement particular callbacks. We keep them separate here so that we
1429 * can access them by name. We also keep them inside periodic_events[]
1430 * so that we can implement "reset all timers" in a reasonable way. */
1431 static periodic_event_item_t *check_descriptor_event=NULL;
1432 static periodic_event_item_t *fetch_networkstatus_event=NULL;
1433 static periodic_event_item_t *launch_descriptor_fetches_event=NULL;
1434 static periodic_event_item_t *check_dns_honesty_event=NULL;
1436 /** Reset all the periodic events so we'll do all our actions again as if we
1437 * just started up.
1438 * Useful if our clock just moved back a long time from the future,
1439 * so we don't wait until that future arrives again before acting.
1441 void
1442 reset_all_main_loop_timers(void)
1444 int i;
1445 for (i = 0; periodic_events[i].name; ++i) {
1446 periodic_event_reschedule(&periodic_events[i]);
1450 /** Return the member of periodic_events[] whose name is <b>name</b>.
1451 * Return NULL if no such event is found.
1453 static periodic_event_item_t *
1454 find_periodic_event(const char *name)
1456 int i;
1457 for (i = 0; periodic_events[i].name; ++i) {
1458 if (strcmp(name, periodic_events[i].name) == 0)
1459 return &periodic_events[i];
1461 return NULL;
1464 /** Return a bitmask of the roles this tor instance is configured for using
1465 * the given options. */
1466 STATIC int
1467 get_my_roles(const or_options_t *options)
1469 tor_assert(options);
1471 int roles = 0;
1472 int is_bridge = options->BridgeRelay;
1473 int is_client = any_client_port_set(options);
1474 int is_relay = server_mode(options);
1475 int is_dirauth = authdir_mode_v3(options);
1476 int is_bridgeauth = authdir_mode_bridge(options);
1477 int is_hidden_service = !!hs_service_get_num_services() ||
1478 !!rend_num_services();
1480 if (is_bridge) roles |= PERIODIC_EVENT_ROLE_BRIDGE;
1481 if (is_client) roles |= PERIODIC_EVENT_ROLE_CLIENT;
1482 if (is_relay) roles |= PERIODIC_EVENT_ROLE_RELAY;
1483 if (is_dirauth) roles |= PERIODIC_EVENT_ROLE_DIRAUTH;
1484 if (is_bridgeauth) roles |= PERIODIC_EVENT_ROLE_BRIDGEAUTH;
1485 if (is_hidden_service) roles |= PERIODIC_EVENT_ROLE_HS_SERVICE;
1487 return roles;
1490 /** Event to run initialize_periodic_events_cb */
1491 static struct event *initialize_periodic_events_event = NULL;
1493 /** Helper, run one second after setup:
1494 * Initializes all members of periodic_events and starts them running.
1496 * (We do this one second after setup for backward-compatibility reasons;
1497 * it might not actually be necessary.) */
1498 static void
1499 initialize_periodic_events_cb(evutil_socket_t fd, short events, void *data)
1501 (void) fd;
1502 (void) events;
1503 (void) data;
1505 tor_event_free(initialize_periodic_events_event);
1507 rescan_periodic_events(get_options());
1510 /** Set up all the members of periodic_events[], and configure them all to be
1511 * launched from a callback. */
1512 STATIC void
1513 initialize_periodic_events(void)
1515 tor_assert(periodic_events_initialized == 0);
1516 periodic_events_initialized = 1;
1518 /* Set up all periodic events. We'll launch them by roles. */
1519 int i;
1520 for (i = 0; periodic_events[i].name; ++i) {
1521 periodic_event_setup(&periodic_events[i]);
1524 #define NAMED_CALLBACK(name) \
1525 STMT_BEGIN name ## _event = find_periodic_event( #name ); STMT_END
1527 NAMED_CALLBACK(check_descriptor);
1528 NAMED_CALLBACK(fetch_networkstatus);
1529 NAMED_CALLBACK(launch_descriptor_fetches);
1530 NAMED_CALLBACK(check_dns_honesty);
1532 struct timeval one_second = { 1, 0 };
1533 initialize_periodic_events_event = tor_evtimer_new(
1534 tor_libevent_get_base(),
1535 initialize_periodic_events_cb, NULL);
1536 event_add(initialize_periodic_events_event, &one_second);
1539 STATIC void
1540 teardown_periodic_events(void)
1542 int i;
1543 for (i = 0; periodic_events[i].name; ++i) {
1544 periodic_event_destroy(&periodic_events[i]);
1546 periodic_events_initialized = 0;
1549 /** Do a pass at all our periodic events, disable those we don't need anymore
1550 * and enable those we need now using the given options. */
1551 void
1552 rescan_periodic_events(const or_options_t *options)
1554 tor_assert(options);
1556 /* Avoid scanning the event list if we haven't initialized it yet. This is
1557 * particularly useful for unit tests in order to avoid initializing main
1558 * loop events everytime. */
1559 if (!periodic_events_initialized) {
1560 return;
1563 int roles = get_my_roles(options);
1565 for (int i = 0; periodic_events[i].name; ++i) {
1566 periodic_event_item_t *item = &periodic_events[i];
1568 /* Handle the event flags. */
1569 if (net_is_disabled() &&
1570 (item->flags & PERIODIC_EVENT_FLAG_NEED_NET)) {
1571 continue;
1574 /* Enable the event if needed. It is safe to enable an event that was
1575 * already enabled. Same goes for disabling it. */
1576 if (item->roles & roles) {
1577 log_debug(LD_GENERAL, "Launching periodic event %s", item->name);
1578 periodic_event_enable(item);
1579 } else {
1580 log_debug(LD_GENERAL, "Disabling periodic event %s", item->name);
1581 periodic_event_disable(item);
1586 /* We just got new options globally set, see if we need to enabled or disable
1587 * periodic events. */
1588 void
1589 periodic_events_on_new_options(const or_options_t *options)
1591 /* Only if we've already initialized the events, rescan the list which will
1592 * enable or disable events depending on our roles. This will be called at
1593 * bootup and we don't want this function to initialize the events because
1594 * they aren't set up at this stage. */
1595 if (periodic_events_initialized) {
1596 rescan_periodic_events(options);
1601 * Update our schedule so that we'll check whether we need to update our
1602 * descriptor immediately, rather than after up to CHECK_DESCRIPTOR_INTERVAL
1603 * seconds.
1605 void
1606 reschedule_descriptor_update_check(void)
1608 tor_assert(check_descriptor_event);
1609 periodic_event_reschedule(check_descriptor_event);
1613 * Update our schedule so that we'll check whether we need to fetch directory
1614 * info immediately.
1616 void
1617 reschedule_directory_downloads(void)
1619 tor_assert(fetch_networkstatus_event);
1620 tor_assert(launch_descriptor_fetches_event);
1622 periodic_event_reschedule(fetch_networkstatus_event);
1623 periodic_event_reschedule(launch_descriptor_fetches_event);
1626 /** Mainloop callback: clean up circuits, channels, and connections
1627 * that are pending close. */
1628 static void
1629 postloop_cleanup_cb(mainloop_event_t *ev, void *arg)
1631 (void)ev;
1632 (void)arg;
1633 circuit_close_all_marked();
1634 close_closeable_connections();
1635 channel_run_cleanup();
1636 channel_listener_run_cleanup();
1639 /** Event to run postloop_cleanup_cb */
1640 static mainloop_event_t *postloop_cleanup_ev=NULL;
1642 /** Schedule a post-loop event to clean up marked channels, connections, and
1643 * circuits. */
1644 void
1645 mainloop_schedule_postloop_cleanup(void)
1647 mainloop_event_activate(postloop_cleanup_ev);
1650 #define LONGEST_TIMER_PERIOD (30 * 86400)
1651 /** Helper: Return the number of seconds between <b>now</b> and <b>next</b>,
1652 * clipped to the range [1 second, LONGEST_TIMER_PERIOD]. */
1653 static inline int
1654 safe_timer_diff(time_t now, time_t next)
1656 if (next > now) {
1657 /* There were no computers at signed TIME_MIN (1902 on 32-bit systems),
1658 * and nothing that could run Tor. It's a bug if 'next' is around then.
1659 * On 64-bit systems with signed TIME_MIN, TIME_MIN is before the Big
1660 * Bang. We cannot extrapolate past a singularity, but there was probably
1661 * nothing that could run Tor then, either.
1663 tor_assert(next > TIME_MIN + LONGEST_TIMER_PERIOD);
1665 if (next - LONGEST_TIMER_PERIOD > now)
1666 return LONGEST_TIMER_PERIOD;
1667 return (int)(next - now);
1668 } else {
1669 return 1;
1673 /** Perform regular maintenance tasks. This function gets run once per
1674 * second by second_elapsed_callback().
1676 static void
1677 run_scheduled_events(time_t now)
1679 const or_options_t *options = get_options();
1681 /* 0. See if we've been asked to shut down and our timeout has
1682 * expired; or if our bandwidth limits are exhausted and we
1683 * should hibernate; or if it's time to wake up from hibernation.
1685 consider_hibernation(now);
1687 /* 0b. If we've deferred a signewnym, make sure it gets handled
1688 * eventually. */
1689 if (signewnym_is_pending &&
1690 time_of_last_signewnym + MAX_SIGNEWNYM_RATE <= now) {
1691 log_info(LD_CONTROL, "Honoring delayed NEWNYM request");
1692 signewnym_impl(now);
1695 /* 0c. If we've deferred log messages for the controller, handle them now */
1696 flush_pending_log_callbacks();
1698 /* Maybe enough time elapsed for us to reconsider a circuit. */
1699 circuit_upgrade_circuits_from_guard_wait();
1701 if (options->UseBridges && !net_is_disabled()) {
1702 /* Note: this check uses net_is_disabled(), not should_delay_dir_fetches()
1703 * -- the latter is only for fetching consensus-derived directory info. */
1704 fetch_bridge_descriptors(options, now);
1707 if (accounting_is_enabled(options)) {
1708 accounting_run_housekeeping(now);
1711 if (authdir_mode_v3(options)) {
1712 dirvote_act(options, now);
1715 /* 3a. Every second, we examine pending circuits and prune the
1716 * ones which have been pending for more than a few seconds.
1717 * We do this before step 4, so it can try building more if
1718 * it's not comfortable with the number of available circuits.
1720 /* (If our circuit build timeout can ever become lower than a second (which
1721 * it can't, currently), we should do this more often.) */
1722 circuit_expire_building();
1723 circuit_expire_waiting_for_better_guard();
1725 /* 3b. Also look at pending streams and prune the ones that 'began'
1726 * a long time ago but haven't gotten a 'connected' yet.
1727 * Do this before step 4, so we can put them back into pending
1728 * state to be picked up by the new circuit.
1730 connection_ap_expire_beginning();
1732 /* 3c. And expire connections that we've held open for too long.
1734 connection_expire_held_open();
1736 /* 4. Every second, we try a new circuit if there are no valid
1737 * circuits. Every NewCircuitPeriod seconds, we expire circuits
1738 * that became dirty more than MaxCircuitDirtiness seconds ago,
1739 * and we make a new circ if there are no clean circuits.
1741 const int have_dir_info = router_have_minimum_dir_info();
1742 if (have_dir_info && !net_is_disabled()) {
1743 circuit_build_needed_circs(now);
1744 } else {
1745 circuit_expire_old_circs_as_needed(now);
1748 /* 5. We do housekeeping for each connection... */
1749 channel_update_bad_for_new_circs(NULL, 0);
1750 int i;
1751 for (i=0;i<smartlist_len(connection_array);i++) {
1752 run_connection_housekeeping(i, now);
1755 /* 8b. And if anything in our state is ready to get flushed to disk, we
1756 * flush it. */
1757 or_state_save(now);
1759 /* 11b. check pending unconfigured managed proxies */
1760 if (!net_is_disabled() && pt_proxies_configuration_pending())
1761 pt_configure_remaining_proxies();
1764 /* Periodic callback: rotate the onion keys after the period defined by the
1765 * "onion-key-rotation-days" consensus parameter, shut down and restart all
1766 * cpuworkers, and update our descriptor if necessary.
1768 static int
1769 rotate_onion_key_callback(time_t now, const or_options_t *options)
1771 if (server_mode(options)) {
1772 int onion_key_lifetime = get_onion_key_lifetime();
1773 time_t rotation_time = get_onion_key_set_at()+onion_key_lifetime;
1774 if (rotation_time > now) {
1775 return ONION_KEY_CONSENSUS_CHECK_INTERVAL;
1778 log_info(LD_GENERAL,"Rotating onion key.");
1779 rotate_onion_key();
1780 cpuworkers_rotate_keyinfo();
1781 if (router_rebuild_descriptor(1)<0) {
1782 log_info(LD_CONFIG, "Couldn't rebuild router descriptor");
1784 if (advertised_server_mode() && !net_is_disabled())
1785 router_upload_dir_desc_to_dirservers(0);
1786 return ONION_KEY_CONSENSUS_CHECK_INTERVAL;
1788 return PERIODIC_EVENT_NO_UPDATE;
1791 /* Period callback: Check if our old onion keys are still valid after the
1792 * period of time defined by the consensus parameter
1793 * "onion-key-grace-period-days", otherwise expire them by setting them to
1794 * NULL.
1796 static int
1797 check_onion_keys_expiry_time_callback(time_t now, const or_options_t *options)
1799 if (server_mode(options)) {
1800 int onion_key_grace_period = get_onion_key_grace_period();
1801 time_t expiry_time = get_onion_key_set_at()+onion_key_grace_period;
1802 if (expiry_time > now) {
1803 return ONION_KEY_CONSENSUS_CHECK_INTERVAL;
1806 log_info(LD_GENERAL, "Expiring old onion keys.");
1807 expire_old_onion_keys();
1808 cpuworkers_rotate_keyinfo();
1809 return ONION_KEY_CONSENSUS_CHECK_INTERVAL;
1812 return PERIODIC_EVENT_NO_UPDATE;
1815 /* Periodic callback: Every 30 seconds, check whether it's time to make new
1816 * Ed25519 subkeys.
1818 static int
1819 check_ed_keys_callback(time_t now, const or_options_t *options)
1821 if (server_mode(options)) {
1822 if (should_make_new_ed_keys(options, now)) {
1823 int new_signing_key = load_ed_keys(options, now);
1824 if (new_signing_key < 0 ||
1825 generate_ed_link_cert(options, now, new_signing_key > 0)) {
1826 log_err(LD_OR, "Unable to update Ed25519 keys! Exiting.");
1827 tor_shutdown_event_loop_and_exit(1);
1830 return 30;
1832 return PERIODIC_EVENT_NO_UPDATE;
1836 * Periodic callback: Every {LAZY,GREEDY}_DESCRIPTOR_RETRY_INTERVAL,
1837 * see about fetching descriptors, microdescriptors, and extrainfo
1838 * documents.
1840 static int
1841 launch_descriptor_fetches_callback(time_t now, const or_options_t *options)
1843 if (should_delay_dir_fetches(options, NULL))
1844 return PERIODIC_EVENT_NO_UPDATE;
1846 update_all_descriptor_downloads(now);
1847 update_extrainfo_downloads(now);
1848 if (router_have_minimum_dir_info())
1849 return LAZY_DESCRIPTOR_RETRY_INTERVAL;
1850 else
1851 return GREEDY_DESCRIPTOR_RETRY_INTERVAL;
1855 * Periodic event: Rotate our X.509 certificates and TLS keys once every
1856 * MAX_SSL_KEY_LIFETIME_INTERNAL.
1858 static int
1859 rotate_x509_certificate_callback(time_t now, const or_options_t *options)
1861 static int first = 1;
1862 (void)now;
1863 (void)options;
1864 if (first) {
1865 first = 0;
1866 return MAX_SSL_KEY_LIFETIME_INTERNAL;
1869 /* 1b. Every MAX_SSL_KEY_LIFETIME_INTERNAL seconds, we change our
1870 * TLS context. */
1871 log_info(LD_GENERAL,"Rotating tls context.");
1872 if (router_initialize_tls_context() < 0) {
1873 log_err(LD_BUG, "Error reinitializing TLS context");
1874 tor_assert_unreached();
1876 if (generate_ed_link_cert(options, now, 1)) {
1877 log_err(LD_OR, "Unable to update Ed25519->TLS link certificate for "
1878 "new TLS context.");
1879 tor_assert_unreached();
1882 /* We also make sure to rotate the TLS connections themselves if they've
1883 * been up for too long -- but that's done via is_bad_for_new_circs in
1884 * run_connection_housekeeping() above. */
1885 return MAX_SSL_KEY_LIFETIME_INTERNAL;
1889 * Periodic callback: once an hour, grab some more entropy from the
1890 * kernel and feed it to our CSPRNG.
1892 static int
1893 add_entropy_callback(time_t now, const or_options_t *options)
1895 (void)now;
1896 (void)options;
1897 /* We already seeded once, so don't die on failure. */
1898 if (crypto_seed_rng() < 0) {
1899 log_warn(LD_GENERAL, "Tried to re-seed RNG, but failed. We already "
1900 "seeded once, though, so we won't exit here.");
1903 /** How often do we add more entropy to OpenSSL's RNG pool? */
1904 #define ENTROPY_INTERVAL (60*60)
1905 return ENTROPY_INTERVAL;
1909 * Periodic callback: if we're an authority, make sure we test
1910 * the routers on the network for reachability.
1912 static int
1913 launch_reachability_tests_callback(time_t now, const or_options_t *options)
1915 if (authdir_mode_tests_reachability(options) &&
1916 !net_is_disabled()) {
1917 /* try to determine reachability of the other Tor relays */
1918 dirserv_test_reachability(now);
1920 return REACHABILITY_TEST_INTERVAL;
1924 * Periodic callback: if we're an authority, discount the stability
1925 * information (and other rephist information) that's older.
1927 static int
1928 downrate_stability_callback(time_t now, const or_options_t *options)
1930 (void)options;
1931 /* 1d. Periodically, we discount older stability information so that new
1932 * stability info counts more, and save the stability information to disk as
1933 * appropriate. */
1934 time_t next = rep_hist_downrate_old_runs(now);
1935 return safe_timer_diff(now, next);
1939 * Periodic callback: if we're an authority, record our measured stability
1940 * information from rephist in an mtbf file.
1942 static int
1943 save_stability_callback(time_t now, const or_options_t *options)
1945 if (authdir_mode_tests_reachability(options)) {
1946 if (rep_hist_record_mtbf_data(now, 1)<0) {
1947 log_warn(LD_GENERAL, "Couldn't store mtbf data.");
1950 #define SAVE_STABILITY_INTERVAL (30*60)
1951 return SAVE_STABILITY_INTERVAL;
1955 * Periodic callback: if we're an authority, check on our authority
1956 * certificate (the one that authenticates our authority signing key).
1958 static int
1959 check_authority_cert_callback(time_t now, const or_options_t *options)
1961 (void)now;
1962 (void)options;
1963 /* 1e. Periodically, if we're a v3 authority, we check whether our cert is
1964 * close to expiring and warn the admin if it is. */
1965 v3_authority_check_key_expiry();
1966 #define CHECK_V3_CERTIFICATE_INTERVAL (5*60)
1967 return CHECK_V3_CERTIFICATE_INTERVAL;
1971 * Periodic callback: If our consensus is too old, recalculate whether
1972 * we can actually use it.
1974 static int
1975 check_expired_networkstatus_callback(time_t now, const or_options_t *options)
1977 (void)options;
1978 /* Check whether our networkstatus has expired. */
1979 networkstatus_t *ns = networkstatus_get_latest_consensus();
1980 /*XXXX RD: This value needs to be the same as REASONABLY_LIVE_TIME in
1981 * networkstatus_get_reasonably_live_consensus(), but that value is way
1982 * way too high. Arma: is the bridge issue there resolved yet? -NM */
1983 #define NS_EXPIRY_SLOP (24*60*60)
1984 if (ns && ns->valid_until < (now - NS_EXPIRY_SLOP) &&
1985 router_have_minimum_dir_info()) {
1986 router_dir_info_changed();
1988 #define CHECK_EXPIRED_NS_INTERVAL (2*60)
1989 return CHECK_EXPIRED_NS_INTERVAL;
1993 * Periodic callback: Write statistics to disk if appropriate.
1995 static int
1996 write_stats_file_callback(time_t now, const or_options_t *options)
1998 /* 1g. Check whether we should write statistics to disk.
2000 #define CHECK_WRITE_STATS_INTERVAL (60*60)
2001 time_t next_time_to_write_stats_files = now + CHECK_WRITE_STATS_INTERVAL;
2002 if (options->CellStatistics) {
2003 time_t next_write =
2004 rep_hist_buffer_stats_write(now);
2005 if (next_write && next_write < next_time_to_write_stats_files)
2006 next_time_to_write_stats_files = next_write;
2008 if (options->DirReqStatistics) {
2009 time_t next_write = geoip_dirreq_stats_write(now);
2010 if (next_write && next_write < next_time_to_write_stats_files)
2011 next_time_to_write_stats_files = next_write;
2013 if (options->EntryStatistics) {
2014 time_t next_write = geoip_entry_stats_write(now);
2015 if (next_write && next_write < next_time_to_write_stats_files)
2016 next_time_to_write_stats_files = next_write;
2018 if (options->HiddenServiceStatistics) {
2019 time_t next_write = rep_hist_hs_stats_write(now);
2020 if (next_write && next_write < next_time_to_write_stats_files)
2021 next_time_to_write_stats_files = next_write;
2023 if (options->ExitPortStatistics) {
2024 time_t next_write = rep_hist_exit_stats_write(now);
2025 if (next_write && next_write < next_time_to_write_stats_files)
2026 next_time_to_write_stats_files = next_write;
2028 if (options->ConnDirectionStatistics) {
2029 time_t next_write = rep_hist_conn_stats_write(now);
2030 if (next_write && next_write < next_time_to_write_stats_files)
2031 next_time_to_write_stats_files = next_write;
2033 if (options->BridgeAuthoritativeDir) {
2034 time_t next_write = rep_hist_desc_stats_write(now);
2035 if (next_write && next_write < next_time_to_write_stats_files)
2036 next_time_to_write_stats_files = next_write;
2039 return safe_timer_diff(now, next_time_to_write_stats_files);
2042 #define CHANNEL_CHECK_INTERVAL (60*60)
2043 static int
2044 check_canonical_channels_callback(time_t now, const or_options_t *options)
2046 (void)now;
2047 if (public_server_mode(options))
2048 channel_check_for_duplicates();
2050 return CHANNEL_CHECK_INTERVAL;
2053 static int
2054 reset_padding_counts_callback(time_t now, const or_options_t *options)
2056 if (options->PaddingStatistics) {
2057 rep_hist_prep_published_padding_counts(now);
2060 rep_hist_reset_padding_counts();
2061 return REPHIST_CELL_PADDING_COUNTS_INTERVAL;
2064 static int should_init_bridge_stats = 1;
2067 * Periodic callback: Write bridge statistics to disk if appropriate.
2069 static int
2070 record_bridge_stats_callback(time_t now, const or_options_t *options)
2072 /* 1h. Check whether we should write bridge statistics to disk.
2074 if (should_record_bridge_info(options)) {
2075 if (should_init_bridge_stats) {
2076 /* (Re-)initialize bridge statistics. */
2077 geoip_bridge_stats_init(now);
2078 should_init_bridge_stats = 0;
2079 return WRITE_STATS_INTERVAL;
2080 } else {
2081 /* Possibly write bridge statistics to disk and ask when to write
2082 * them next time. */
2083 time_t next = geoip_bridge_stats_write(now);
2084 return safe_timer_diff(now, next);
2086 } else if (!should_init_bridge_stats) {
2087 /* Bridge mode was turned off. Ensure that stats are re-initialized
2088 * next time bridge mode is turned on. */
2089 should_init_bridge_stats = 1;
2091 return PERIODIC_EVENT_NO_UPDATE;
2095 * Periodic callback: Clean in-memory caches every once in a while
2097 static int
2098 clean_caches_callback(time_t now, const or_options_t *options)
2100 /* Remove old information from rephist and the rend cache. */
2101 rep_history_clean(now - options->RephistTrackTime);
2102 rend_cache_clean(now, REND_CACHE_TYPE_SERVICE);
2103 hs_cache_clean_as_client(now);
2104 hs_cache_clean_as_dir(now);
2105 microdesc_cache_rebuild(NULL, 0);
2106 #define CLEAN_CACHES_INTERVAL (30*60)
2107 return CLEAN_CACHES_INTERVAL;
2111 * Periodic callback: Clean the cache of failed hidden service lookups
2112 * frequently.
2114 static int
2115 rend_cache_failure_clean_callback(time_t now, const or_options_t *options)
2117 (void)options;
2118 /* We don't keep entries that are more than five minutes old so we try to
2119 * clean it as soon as we can since we want to make sure the client waits
2120 * as little as possible for reachability reasons. */
2121 rend_cache_failure_clean(now);
2122 hs_cache_client_intro_state_clean(now);
2123 return 30;
2127 * Periodic callback: If we're a server and initializing dns failed, retry.
2129 static int
2130 retry_dns_callback(time_t now, const or_options_t *options)
2132 (void)now;
2133 #define RETRY_DNS_INTERVAL (10*60)
2134 if (server_mode(options) && has_dns_init_failed())
2135 dns_init();
2136 return RETRY_DNS_INTERVAL;
2139 /** Periodic callback: consider rebuilding or and re-uploading our descriptor
2140 * (if we've passed our internal checks). */
2141 static int
2142 check_descriptor_callback(time_t now, const or_options_t *options)
2144 /** How often do we check whether part of our router info has changed in a
2145 * way that would require an upload? That includes checking whether our IP
2146 * address has changed. */
2147 #define CHECK_DESCRIPTOR_INTERVAL (60)
2149 (void)options;
2151 /* 2b. Once per minute, regenerate and upload the descriptor if the old
2152 * one is inaccurate. */
2153 if (!net_is_disabled()) {
2154 check_descriptor_bandwidth_changed(now);
2155 check_descriptor_ipaddress_changed(now);
2156 mark_my_descriptor_dirty_if_too_old(now);
2157 consider_publishable_server(0);
2158 /* If any networkstatus documents are no longer recent, we need to
2159 * update all the descriptors' running status. */
2160 /* Remove dead routers. */
2161 /* XXXX This doesn't belong here, but it was here in the pre-
2162 * XXXX refactoring code. */
2163 routerlist_remove_old_routers();
2166 return CHECK_DESCRIPTOR_INTERVAL;
2170 * Periodic callback: check whether we're reachable (as a relay), and
2171 * whether our bandwidth has changed enough that we need to
2172 * publish a new descriptor.
2174 static int
2175 check_for_reachability_bw_callback(time_t now, const or_options_t *options)
2177 /* XXXX This whole thing was stuck in the middle of what is now
2178 * XXXX check_descriptor_callback. I'm not sure it's right. */
2180 static int dirport_reachability_count = 0;
2181 /* also, check religiously for reachability, if it's within the first
2182 * 20 minutes of our uptime. */
2183 if (server_mode(options) &&
2184 (have_completed_a_circuit() || !any_predicted_circuits(now)) &&
2185 !net_is_disabled()) {
2186 if (get_uptime() < TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT) {
2187 router_do_reachability_checks(1, dirport_reachability_count==0);
2188 if (++dirport_reachability_count > 5)
2189 dirport_reachability_count = 0;
2190 return 1;
2191 } else {
2192 /* If we haven't checked for 12 hours and our bandwidth estimate is
2193 * low, do another bandwidth test. This is especially important for
2194 * bridges, since they might go long periods without much use. */
2195 const routerinfo_t *me = router_get_my_routerinfo();
2196 static int first_time = 1;
2197 if (!first_time && me &&
2198 me->bandwidthcapacity < me->bandwidthrate &&
2199 me->bandwidthcapacity < 51200) {
2200 reset_bandwidth_test();
2202 first_time = 0;
2203 #define BANDWIDTH_RECHECK_INTERVAL (12*60*60)
2204 return BANDWIDTH_RECHECK_INTERVAL;
2207 return CHECK_DESCRIPTOR_INTERVAL;
2211 * Periodic event: once a minute, (or every second if TestingTorNetwork, or
2212 * during client bootstrap), check whether we want to download any
2213 * networkstatus documents. */
2214 static int
2215 fetch_networkstatus_callback(time_t now, const or_options_t *options)
2217 /* How often do we check whether we should download network status
2218 * documents? */
2219 const int we_are_bootstrapping = networkstatus_consensus_is_bootstrapping(
2220 now);
2221 const int prefer_mirrors = !directory_fetches_from_authorities(
2222 get_options());
2223 int networkstatus_dl_check_interval = 60;
2224 /* check more often when testing, or when bootstrapping from mirrors
2225 * (connection limits prevent too many connections being made) */
2226 if (options->TestingTorNetwork
2227 || (we_are_bootstrapping && prefer_mirrors)) {
2228 networkstatus_dl_check_interval = 1;
2231 if (should_delay_dir_fetches(options, NULL))
2232 return PERIODIC_EVENT_NO_UPDATE;
2234 update_networkstatus_downloads(now);
2235 return networkstatus_dl_check_interval;
2239 * Periodic callback: Every 60 seconds, we relaunch listeners if any died. */
2240 static int
2241 retry_listeners_callback(time_t now, const or_options_t *options)
2243 (void)now;
2244 (void)options;
2245 if (!net_is_disabled()) {
2246 retry_all_listeners(NULL, NULL, 0);
2247 return 60;
2249 return PERIODIC_EVENT_NO_UPDATE;
2253 * Periodic callback: as a server, see if we have any old unused circuits
2254 * that should be expired */
2255 static int
2256 expire_old_ciruits_serverside_callback(time_t now, const or_options_t *options)
2258 (void)options;
2259 /* every 11 seconds, so not usually the same second as other such events */
2260 circuit_expire_old_circuits_serverside(now);
2261 return 11;
2264 static int dns_honesty_first_time = 1;
2267 * Periodic event: if we're an exit, see if our DNS server is telling us
2268 * obvious lies.
2270 static int
2271 check_dns_honesty_callback(time_t now, const or_options_t *options)
2273 (void)now;
2274 /* 9. and if we're an exit node, check whether our DNS is telling stories
2275 * to us. */
2276 if (net_is_disabled() ||
2277 ! public_server_mode(options) ||
2278 router_my_exit_policy_is_reject_star())
2279 return PERIODIC_EVENT_NO_UPDATE;
2281 if (dns_honesty_first_time) {
2282 /* Don't launch right when we start */
2283 dns_honesty_first_time = 0;
2284 return crypto_rand_int_range(60, 180);
2287 dns_launch_correctness_checks();
2288 return 12*3600 + crypto_rand_int(12*3600);
2292 * Periodic callback: if we're the bridge authority, write a networkstatus
2293 * file to disk.
2295 static int
2296 write_bridge_ns_callback(time_t now, const or_options_t *options)
2298 /* 10. write bridge networkstatus file to disk */
2299 if (options->BridgeAuthoritativeDir) {
2300 networkstatus_dump_bridge_status_to_file(now);
2301 #define BRIDGE_STATUSFILE_INTERVAL (30*60)
2302 return BRIDGE_STATUSFILE_INTERVAL;
2304 return PERIODIC_EVENT_NO_UPDATE;
2307 static int heartbeat_callback_first_time = 1;
2310 * Periodic callback: write the heartbeat message in the logs.
2312 * If writing the heartbeat message to the logs fails for some reason, retry
2313 * again after <b>MIN_HEARTBEAT_PERIOD</b> seconds.
2315 static int
2316 heartbeat_callback(time_t now, const or_options_t *options)
2318 /* Check if heartbeat is disabled */
2319 if (!options->HeartbeatPeriod) {
2320 return PERIODIC_EVENT_NO_UPDATE;
2323 /* Skip the first one. */
2324 if (heartbeat_callback_first_time) {
2325 heartbeat_callback_first_time = 0;
2326 return options->HeartbeatPeriod;
2329 /* Write the heartbeat message */
2330 if (log_heartbeat(now) == 0) {
2331 return options->HeartbeatPeriod;
2332 } else {
2333 /* If we couldn't write the heartbeat log message, try again in the minimum
2334 * interval of time. */
2335 return MIN_HEARTBEAT_PERIOD;
2339 #define CDM_CLEAN_CALLBACK_INTERVAL 600
2340 static int
2341 clean_consdiffmgr_callback(time_t now, const or_options_t *options)
2343 (void)now;
2344 if (server_mode(options)) {
2345 consdiffmgr_cleanup();
2347 return CDM_CLEAN_CALLBACK_INTERVAL;
2351 * Periodic callback: Run scheduled events for HS service. This is called
2352 * every second.
2354 static int
2355 hs_service_callback(time_t now, const or_options_t *options)
2357 (void) options;
2359 /* We need to at least be able to build circuits and that we actually have
2360 * a working network. */
2361 if (!have_completed_a_circuit() || net_is_disabled() ||
2362 networkstatus_get_live_consensus(now) == NULL) {
2363 goto end;
2366 hs_service_run_scheduled_events(now);
2368 end:
2369 /* Every 1 second. */
2370 return 1;
2373 /** Timer: used to invoke second_elapsed_callback() once per second. */
2374 static periodic_timer_t *second_timer = NULL;
2375 /** Number of libevent errors in the last second: we die if we get too many. */
2376 static int n_libevent_errors = 0;
2377 /** Last time that second_elapsed_callback was called. */
2378 static time_t current_second = 0;
2380 /** Libevent callback: invoked once every second. */
2381 static void
2382 second_elapsed_callback(periodic_timer_t *timer, void *arg)
2384 /* XXXX This could be sensibly refactored into multiple callbacks, and we
2385 * could use Libevent's timers for this rather than checking the current
2386 * time against a bunch of timeouts every second. */
2387 time_t now;
2388 size_t bytes_written;
2389 size_t bytes_read;
2390 int seconds_elapsed;
2391 const or_options_t *options = get_options();
2392 (void)timer;
2393 (void)arg;
2395 n_libevent_errors = 0;
2397 /* log_notice(LD_GENERAL, "Tick."); */
2398 now = time(NULL);
2399 update_approx_time(now);
2401 /* the second has rolled over. check more stuff. */
2402 seconds_elapsed = current_second ? (int)(now - current_second) : 0;
2403 bytes_read = (size_t)(stats_n_bytes_read - stats_prev_n_read);
2404 bytes_written = (size_t)(stats_n_bytes_written - stats_prev_n_written);
2405 stats_prev_n_read = stats_n_bytes_read;
2406 stats_prev_n_written = stats_n_bytes_written;
2408 control_event_bandwidth_used((uint32_t)bytes_read,(uint32_t)bytes_written);
2409 control_event_stream_bandwidth_used();
2410 control_event_conn_bandwidth_used();
2411 control_event_circ_bandwidth_used();
2412 control_event_circuit_cell_stats();
2414 if (server_mode(options) &&
2415 !net_is_disabled() &&
2416 seconds_elapsed > 0 &&
2417 have_completed_a_circuit() &&
2418 get_uptime() / TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT !=
2419 (get_uptime()+seconds_elapsed) /
2420 TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT) {
2421 /* every 20 minutes, check and complain if necessary */
2422 const routerinfo_t *me = router_get_my_routerinfo();
2423 if (me && !check_whether_orport_reachable(options)) {
2424 char *address = tor_dup_ip(me->addr);
2425 log_warn(LD_CONFIG,"Your server (%s:%d) has not managed to confirm that "
2426 "its ORPort is reachable. Relays do not publish descriptors "
2427 "until their ORPort and DirPort are reachable. Please check "
2428 "your firewalls, ports, address, /etc/hosts file, etc.",
2429 address, me->or_port);
2430 control_event_server_status(LOG_WARN,
2431 "REACHABILITY_FAILED ORADDRESS=%s:%d",
2432 address, me->or_port);
2433 tor_free(address);
2436 if (me && !check_whether_dirport_reachable(options)) {
2437 char *address = tor_dup_ip(me->addr);
2438 log_warn(LD_CONFIG,
2439 "Your server (%s:%d) has not managed to confirm that its "
2440 "DirPort is reachable. Relays do not publish descriptors "
2441 "until their ORPort and DirPort are reachable. Please check "
2442 "your firewalls, ports, address, /etc/hosts file, etc.",
2443 address, me->dir_port);
2444 control_event_server_status(LOG_WARN,
2445 "REACHABILITY_FAILED DIRADDRESS=%s:%d",
2446 address, me->dir_port);
2447 tor_free(address);
2451 /** If more than this many seconds have elapsed, probably the clock
2452 * jumped: doesn't count. */
2453 #define NUM_JUMPED_SECONDS_BEFORE_WARN 100
2454 if (seconds_elapsed < -NUM_JUMPED_SECONDS_BEFORE_WARN ||
2455 seconds_elapsed >= NUM_JUMPED_SECONDS_BEFORE_WARN) {
2456 circuit_note_clock_jumped(seconds_elapsed);
2457 } else if (seconds_elapsed > 0)
2458 stats_n_seconds_working += seconds_elapsed;
2460 run_scheduled_events(now);
2462 current_second = now; /* remember which second it is, for next time */
2465 #ifdef HAVE_SYSTEMD_209
2466 static periodic_timer_t *systemd_watchdog_timer = NULL;
2468 /** Libevent callback: invoked to reset systemd watchdog. */
2469 static void
2470 systemd_watchdog_callback(periodic_timer_t *timer, void *arg)
2472 (void)timer;
2473 (void)arg;
2474 sd_notify(0, "WATCHDOG=1");
2476 #endif /* defined(HAVE_SYSTEMD_209) */
2478 #ifndef _WIN32
2479 /** Called when a possibly ignorable libevent error occurs; ensures that we
2480 * don't get into an infinite loop by ignoring too many errors from
2481 * libevent. */
2482 static int
2483 got_libevent_error(void)
2485 if (++n_libevent_errors > 8) {
2486 log_err(LD_NET, "Too many libevent errors in one second; dying");
2487 return -1;
2489 return 0;
2491 #endif /* !defined(_WIN32) */
2493 #define UPTIME_CUTOFF_FOR_NEW_BANDWIDTH_TEST (6*60*60)
2495 /** Called when our IP address seems to have changed. <b>at_interface</b>
2496 * should be true if we detected a change in our interface, and false if we
2497 * detected a change in our published address. */
2498 void
2499 ip_address_changed(int at_interface)
2501 const or_options_t *options = get_options();
2502 int server = server_mode(options);
2503 int exit_reject_interfaces = (server && options->ExitRelay
2504 && options->ExitPolicyRejectLocalInterfaces);
2506 if (at_interface) {
2507 if (! server) {
2508 /* Okay, change our keys. */
2509 if (init_keys_client() < 0)
2510 log_warn(LD_GENERAL, "Unable to rotate keys after IP change!");
2512 } else {
2513 if (server) {
2514 if (get_uptime() > UPTIME_CUTOFF_FOR_NEW_BANDWIDTH_TEST)
2515 reset_bandwidth_test();
2516 reset_uptime();
2517 router_reset_reachability();
2521 /* Exit relays incorporate interface addresses in their exit policies when
2522 * ExitPolicyRejectLocalInterfaces is set */
2523 if (exit_reject_interfaces || (server && !at_interface)) {
2524 mark_my_descriptor_dirty("IP address changed");
2527 dns_servers_relaunch_checks();
2530 /** Forget what we've learned about the correctness of our DNS servers, and
2531 * start learning again. */
2532 void
2533 dns_servers_relaunch_checks(void)
2535 if (server_mode(get_options())) {
2536 dns_reset_correctness_checks();
2537 if (periodic_events_initialized) {
2538 tor_assert(check_dns_honesty_event);
2539 periodic_event_reschedule(check_dns_honesty_event);
2544 /** Called when we get a SIGHUP: reload configuration files and keys,
2545 * retry all connections, and so on. */
2546 static int
2547 do_hup(void)
2549 const or_options_t *options = get_options();
2551 #ifdef USE_DMALLOC
2552 dmalloc_log_stats();
2553 dmalloc_log_changed(0, 1, 0, 0);
2554 #endif
2556 log_notice(LD_GENERAL,"Received reload signal (hup). Reloading config and "
2557 "resetting internal state.");
2558 if (accounting_is_enabled(options))
2559 accounting_record_bandwidth_usage(time(NULL), get_or_state());
2561 router_reset_warnings();
2562 routerlist_reset_warnings();
2563 /* first, reload config variables, in case they've changed */
2564 if (options->ReloadTorrcOnSIGHUP) {
2565 /* no need to provide argc/v, they've been cached in init_from_config */
2566 int init_rv = options_init_from_torrc(0, NULL);
2567 if (init_rv < 0) {
2568 log_err(LD_CONFIG,"Reading config failed--see warnings above. "
2569 "For usage, try -h.");
2570 return -1;
2571 } else if (BUG(init_rv > 0)) {
2572 // LCOV_EXCL_START
2573 /* This should be impossible: the only "return 1" cases in
2574 * options_init_from_torrc are ones caused by command-line arguments;
2575 * but they can't change while Tor is running. */
2576 return -1;
2577 // LCOV_EXCL_STOP
2579 options = get_options(); /* they have changed now */
2580 /* Logs are only truncated the first time they are opened, but were
2581 probably intended to be cleaned up on signal. */
2582 if (options->TruncateLogFile)
2583 truncate_logs();
2584 } else {
2585 char *msg = NULL;
2586 log_notice(LD_GENERAL, "Not reloading config file: the controller told "
2587 "us not to.");
2588 /* Make stuff get rescanned, reloaded, etc. */
2589 if (set_options((or_options_t*)options, &msg) < 0) {
2590 if (!msg)
2591 msg = tor_strdup("Unknown error");
2592 log_warn(LD_GENERAL, "Unable to re-set previous options: %s", msg);
2593 tor_free(msg);
2596 if (authdir_mode(options)) {
2597 /* reload the approved-routers file */
2598 if (dirserv_load_fingerprint_file() < 0) {
2599 /* warnings are logged from dirserv_load_fingerprint_file() directly */
2600 log_info(LD_GENERAL, "Error reloading fingerprints. "
2601 "Continuing with old list.");
2605 /* Rotate away from the old dirty circuits. This has to be done
2606 * after we've read the new options, but before we start using
2607 * circuits for directory fetches. */
2608 circuit_mark_all_dirty_circs_as_unusable();
2610 /* retry appropriate downloads */
2611 router_reset_status_download_failures();
2612 router_reset_descriptor_download_failures();
2613 if (!net_is_disabled())
2614 update_networkstatus_downloads(time(NULL));
2616 /* We'll retry routerstatus downloads in about 10 seconds; no need to
2617 * force a retry there. */
2619 if (server_mode(options)) {
2620 /* Maybe we've been given a new ed25519 key or certificate?
2622 time_t now = approx_time();
2623 int new_signing_key = load_ed_keys(options, now);
2624 if (new_signing_key < 0 ||
2625 generate_ed_link_cert(options, now, new_signing_key > 0)) {
2626 log_warn(LD_OR, "Problem reloading Ed25519 keys; still using old keys.");
2629 /* Update cpuworker and dnsworker processes, so they get up-to-date
2630 * configuration options. */
2631 cpuworkers_rotate_keyinfo();
2632 dns_reset();
2634 return 0;
2637 /** Initialize some mainloop_event_t objects that we require. */
2638 STATIC void
2639 initialize_mainloop_events(void)
2641 if (!schedule_active_linked_connections_event) {
2642 schedule_active_linked_connections_event =
2643 mainloop_event_postloop_new(schedule_active_linked_connections_cb, NULL);
2645 if (!postloop_cleanup_ev) {
2646 postloop_cleanup_ev =
2647 mainloop_event_postloop_new(postloop_cleanup_cb, NULL);
2651 /** Tor main loop. */
2653 do_main_loop(void)
2655 time_t now;
2657 /* initialize the periodic events first, so that code that depends on the
2658 * events being present does not assert.
2660 if (! periodic_events_initialized) {
2661 initialize_periodic_events();
2664 initialize_mainloop_events();
2666 /* initialize dns resolve map, spawn workers if needed */
2667 if (dns_init() < 0) {
2668 if (get_options()->ServerDNSAllowBrokenConfig)
2669 log_warn(LD_GENERAL, "Couldn't set up any working nameservers. "
2670 "Network not up yet? Will try again soon.");
2671 else {
2672 log_err(LD_GENERAL,"Error initializing dns subsystem; exiting. To "
2673 "retry instead, set the ServerDNSAllowBrokenResolvConf option.");
2677 handle_signals();
2678 monotime_init();
2679 timers_initialize();
2681 /* load the private keys, if we're supposed to have them, and set up the
2682 * TLS context. */
2683 if (! client_identity_key_is_set()) {
2684 if (init_keys() < 0) {
2685 log_err(LD_OR, "Error initializing keys; exiting");
2686 return -1;
2690 /* Set up our buckets */
2691 connection_bucket_init();
2693 /* initialize the bootstrap status events to know we're starting up */
2694 control_event_bootstrap(BOOTSTRAP_STATUS_STARTING, 0);
2696 /* Initialize the keypinning log. */
2697 if (authdir_mode_v3(get_options())) {
2698 char *fname = get_datadir_fname("key-pinning-journal");
2699 int r = 0;
2700 if (keypin_load_journal(fname)<0) {
2701 log_err(LD_DIR, "Error loading key-pinning journal: %s",strerror(errno));
2702 r = -1;
2704 if (keypin_open_journal(fname)<0) {
2705 log_err(LD_DIR, "Error opening key-pinning journal: %s",strerror(errno));
2706 r = -1;
2708 tor_free(fname);
2709 if (r)
2710 return r;
2713 /* This is the old name for key-pinning-journal. These got corrupted
2714 * in a couple of cases by #16530, so we started over. See #16580 for
2715 * the rationale and for other options we didn't take. We can remove
2716 * this code once all the authorities that ran 0.2.7.1-alpha-dev are
2717 * upgraded.
2719 char *fname = get_datadir_fname("key-pinning-entries");
2720 unlink(fname);
2721 tor_free(fname);
2724 if (trusted_dirs_reload_certs()) {
2725 log_warn(LD_DIR,
2726 "Couldn't load all cached v3 certificates. Starting anyway.");
2728 if (router_reload_consensus_networkstatus()) {
2729 return -1;
2731 /* load the routers file, or assign the defaults. */
2732 if (router_reload_router_list()) {
2733 return -1;
2735 /* load the networkstatuses. (This launches a download for new routers as
2736 * appropriate.)
2738 now = time(NULL);
2739 directory_info_has_arrived(now, 1, 0);
2741 if (server_mode(get_options()) || dir_server_mode(get_options())) {
2742 /* launch cpuworkers. Need to do this *after* we've read the onion key. */
2743 cpu_init();
2745 consdiffmgr_enable_background_compression();
2747 /* Setup shared random protocol subsystem. */
2748 if (authdir_mode_v3(get_options())) {
2749 if (sr_init(1) < 0) {
2750 return -1;
2754 /* set up once-a-second callback. */
2755 if (! second_timer) {
2756 struct timeval one_second;
2757 one_second.tv_sec = 1;
2758 one_second.tv_usec = 0;
2760 second_timer = periodic_timer_new(tor_libevent_get_base(),
2761 &one_second,
2762 second_elapsed_callback,
2763 NULL);
2764 tor_assert(second_timer);
2767 #ifdef HAVE_SYSTEMD_209
2768 uint64_t watchdog_delay;
2769 /* set up systemd watchdog notification. */
2770 if (sd_watchdog_enabled(1, &watchdog_delay) > 0) {
2771 if (! systemd_watchdog_timer) {
2772 struct timeval watchdog;
2773 /* The manager will "act on" us if we don't send them a notification
2774 * every 'watchdog_delay' microseconds. So, send notifications twice
2775 * that often. */
2776 watchdog_delay /= 2;
2777 watchdog.tv_sec = watchdog_delay / 1000000;
2778 watchdog.tv_usec = watchdog_delay % 1000000;
2780 systemd_watchdog_timer = periodic_timer_new(tor_libevent_get_base(),
2781 &watchdog,
2782 systemd_watchdog_callback,
2783 NULL);
2784 tor_assert(systemd_watchdog_timer);
2787 #endif /* defined(HAVE_SYSTEMD_209) */
2789 #ifdef HAVE_SYSTEMD
2791 const int r = sd_notify(0, "READY=1");
2792 if (r < 0) {
2793 log_warn(LD_GENERAL, "Unable to send readiness to systemd: %s",
2794 strerror(r));
2795 } else if (r > 0) {
2796 log_notice(LD_GENERAL, "Signaled readiness to systemd");
2797 } else {
2798 log_info(LD_GENERAL, "Systemd NOTIFY_SOCKET not present.");
2801 #endif /* defined(HAVE_SYSTEMD) */
2803 main_loop_should_exit = 0;
2804 main_loop_exit_value = 0;
2806 #ifdef ENABLE_RESTART_DEBUGGING
2808 static int first_time = 1;
2810 if (first_time && getenv("TOR_DEBUG_RESTART")) {
2811 first_time = 0;
2812 const char *sec_str = getenv("TOR_DEBUG_RESTART_AFTER_SECONDS");
2813 long sec;
2814 int sec_ok=0;
2815 if (sec_str &&
2816 (sec = tor_parse_long(sec_str, 10, 0, INT_MAX, &sec_ok, NULL)) &&
2817 sec_ok) {
2818 /* Okay, we parsed the seconds. */
2819 } else {
2820 sec = 5;
2822 struct timeval restart_after = { (time_t) sec, 0 };
2823 tor_shutdown_event_loop_for_restart_event =
2824 tor_evtimer_new(tor_libevent_get_base(),
2825 tor_shutdown_event_loop_for_restart_cb, NULL);
2826 event_add(tor_shutdown_event_loop_for_restart_event, &restart_after);
2829 #endif
2831 return run_main_loop_until_done();
2835 * Run the main loop a single time. Return 0 for "exit"; -1 for "exit with
2836 * error", and 1 for "run this again."
2838 static int
2839 run_main_loop_once(void)
2841 int loop_result;
2843 if (nt_service_is_stopping())
2844 return 0;
2846 if (main_loop_should_exit)
2847 return 0;
2849 #ifndef _WIN32
2850 /* Make it easier to tell whether libevent failure is our fault or not. */
2851 errno = 0;
2852 #endif
2854 if (get_options()->MainloopStats) {
2855 /* We always enforce that EVLOOP_ONCE is passed to event_base_loop() if we
2856 * are collecting main loop statistics. */
2857 called_loop_once = 1;
2858 } else {
2859 called_loop_once = 0;
2862 /* Make sure we know (about) what time it is. */
2863 update_approx_time(time(NULL));
2865 /* Here it is: the main loop. Here we tell Libevent to poll until we have
2866 * an event, or the second ends, or until we have some active linked
2867 * connections to trigger events for. Libevent will wait till one
2868 * of these happens, then run all the appropriate callbacks. */
2869 loop_result = tor_libevent_run_event_loop(tor_libevent_get_base(),
2870 called_loop_once);
2872 if (get_options()->MainloopStats) {
2873 /* Update our main loop counters. */
2874 if (loop_result == 0) {
2875 // The call was successful.
2876 increment_main_loop_success_count();
2877 } else if (loop_result == -1) {
2878 // The call was erroneous.
2879 increment_main_loop_error_count();
2880 } else if (loop_result == 1) {
2881 // The call didn't have any active or pending events
2882 // to handle.
2883 increment_main_loop_idle_count();
2887 /* Oh, the loop failed. That might be an error that we need to
2888 * catch, but more likely, it's just an interrupted poll() call or something,
2889 * and we should try again. */
2890 if (loop_result < 0) {
2891 int e = tor_socket_errno(-1);
2892 /* let the program survive things like ^z */
2893 if (e != EINTR && !ERRNO_IS_EINPROGRESS(e)) {
2894 log_err(LD_NET,"libevent call with %s failed: %s [%d]",
2895 tor_libevent_get_method(), tor_socket_strerror(e), e);
2896 return -1;
2897 #ifndef _WIN32
2898 } else if (e == EINVAL) {
2899 log_warn(LD_NET, "EINVAL from libevent: should you upgrade libevent?");
2900 if (got_libevent_error())
2901 return -1;
2902 #endif /* !defined(_WIN32) */
2903 } else {
2904 tor_assert_nonfatal_once(! ERRNO_IS_EINPROGRESS(e));
2905 log_debug(LD_NET,"libevent call interrupted.");
2906 /* You can't trust the results of this poll(). Go back to the
2907 * top of the big for loop. */
2908 return 1;
2912 if (main_loop_should_exit)
2913 return 0;
2915 return 1;
2918 /** Run the run_main_loop_once() function until it declares itself done,
2919 * and return its final return value.
2921 * Shadow won't invoke this function, so don't fill it up with things.
2923 static int
2924 run_main_loop_until_done(void)
2926 int loop_result = 1;
2927 do {
2928 loop_result = run_main_loop_once();
2929 } while (loop_result == 1);
2931 if (main_loop_should_exit)
2932 return main_loop_exit_value;
2933 else
2934 return loop_result;
2937 /** Libevent callback: invoked when we get a signal.
2939 static void
2940 signal_callback(evutil_socket_t fd, short events, void *arg)
2942 const int *sigptr = arg;
2943 const int sig = *sigptr;
2944 (void)fd;
2945 (void)events;
2947 process_signal(sig);
2950 /** Do the work of acting on a signal received in <b>sig</b> */
2951 static void
2952 process_signal(int sig)
2954 switch (sig)
2956 case SIGTERM:
2957 log_notice(LD_GENERAL,"Catching signal TERM, exiting cleanly.");
2958 tor_shutdown_event_loop_and_exit(0);
2959 break;
2960 case SIGINT:
2961 if (!server_mode(get_options())) { /* do it now */
2962 log_notice(LD_GENERAL,"Interrupt: exiting cleanly.");
2963 tor_shutdown_event_loop_and_exit(0);
2964 return;
2966 #ifdef HAVE_SYSTEMD
2967 sd_notify(0, "STOPPING=1");
2968 #endif
2969 hibernate_begin_shutdown();
2970 break;
2971 #ifdef SIGPIPE
2972 case SIGPIPE:
2973 log_debug(LD_GENERAL,"Caught SIGPIPE. Ignoring.");
2974 break;
2975 #endif
2976 case SIGUSR1:
2977 /* prefer to log it at INFO, but make sure we always see it */
2978 dumpstats(get_min_log_level()<LOG_INFO ? get_min_log_level() : LOG_INFO);
2979 control_event_signal(sig);
2980 break;
2981 case SIGUSR2:
2982 switch_logs_debug();
2983 log_debug(LD_GENERAL,"Caught USR2, going to loglevel debug. "
2984 "Send HUP to change back.");
2985 control_event_signal(sig);
2986 break;
2987 case SIGHUP:
2988 #ifdef HAVE_SYSTEMD
2989 sd_notify(0, "RELOADING=1");
2990 #endif
2991 if (do_hup() < 0) {
2992 log_warn(LD_CONFIG,"Restart failed (config error?). Exiting.");
2993 tor_shutdown_event_loop_and_exit(1);
2994 return;
2996 #ifdef HAVE_SYSTEMD
2997 sd_notify(0, "READY=1");
2998 #endif
2999 control_event_signal(sig);
3000 break;
3001 #ifdef SIGCHLD
3002 case SIGCHLD:
3003 notify_pending_waitpid_callbacks();
3004 break;
3005 #endif
3006 case SIGNEWNYM: {
3007 time_t now = time(NULL);
3008 if (time_of_last_signewnym + MAX_SIGNEWNYM_RATE > now) {
3009 signewnym_is_pending = 1;
3010 log_notice(LD_CONTROL,
3011 "Rate limiting NEWNYM request: delaying by %d second(s)",
3012 (int)(MAX_SIGNEWNYM_RATE+time_of_last_signewnym-now));
3013 } else {
3014 signewnym_impl(now);
3016 break;
3018 case SIGCLEARDNSCACHE:
3019 addressmap_clear_transient();
3020 control_event_signal(sig);
3021 break;
3022 case SIGHEARTBEAT:
3023 log_heartbeat(time(NULL));
3024 control_event_signal(sig);
3025 break;
3029 /** Returns Tor's uptime. */
3030 MOCK_IMPL(long,
3031 get_uptime,(void))
3033 return stats_n_seconds_working;
3036 /** Reset Tor's uptime. */
3037 MOCK_IMPL(void,
3038 reset_uptime,(void))
3040 stats_n_seconds_working = 0;
3044 * Write current memory usage information to the log.
3046 static void
3047 dumpmemusage(int severity)
3049 connection_dump_buffer_mem_stats(severity);
3050 tor_log(severity, LD_GENERAL, "In rephist: "U64_FORMAT" used by %d Tors.",
3051 U64_PRINTF_ARG(rephist_total_alloc), rephist_total_num);
3052 dump_routerlist_mem_usage(severity);
3053 dump_cell_pool_usage(severity);
3054 dump_dns_mem_usage(severity);
3055 tor_log_mallinfo(severity);
3058 /** Write all statistics to the log, with log level <b>severity</b>. Called
3059 * in response to a SIGUSR1. */
3060 static void
3061 dumpstats(int severity)
3063 time_t now = time(NULL);
3064 time_t elapsed;
3065 size_t rbuf_cap, wbuf_cap, rbuf_len, wbuf_len;
3067 tor_log(severity, LD_GENERAL, "Dumping stats:");
3069 SMARTLIST_FOREACH_BEGIN(connection_array, connection_t *, conn) {
3070 int i = conn_sl_idx;
3071 tor_log(severity, LD_GENERAL,
3072 "Conn %d (socket %d) type %d (%s), state %d (%s), created %d secs ago",
3073 i, (int)conn->s, conn->type, conn_type_to_string(conn->type),
3074 conn->state, conn_state_to_string(conn->type, conn->state),
3075 (int)(now - conn->timestamp_created));
3076 if (!connection_is_listener(conn)) {
3077 tor_log(severity,LD_GENERAL,
3078 "Conn %d is to %s:%d.", i,
3079 safe_str_client(conn->address),
3080 conn->port);
3081 tor_log(severity,LD_GENERAL,
3082 "Conn %d: %d bytes waiting on inbuf (len %d, last read %d secs ago)",
3084 (int)connection_get_inbuf_len(conn),
3085 (int)buf_allocation(conn->inbuf),
3086 (int)(now - conn->timestamp_last_read_allowed));
3087 tor_log(severity,LD_GENERAL,
3088 "Conn %d: %d bytes waiting on outbuf "
3089 "(len %d, last written %d secs ago)",i,
3090 (int)connection_get_outbuf_len(conn),
3091 (int)buf_allocation(conn->outbuf),
3092 (int)(now - conn->timestamp_last_write_allowed));
3093 if (conn->type == CONN_TYPE_OR) {
3094 or_connection_t *or_conn = TO_OR_CONN(conn);
3095 if (or_conn->tls) {
3096 if (tor_tls_get_buffer_sizes(or_conn->tls, &rbuf_cap, &rbuf_len,
3097 &wbuf_cap, &wbuf_len) == 0) {
3098 tor_log(severity, LD_GENERAL,
3099 "Conn %d: %d/%d bytes used on OpenSSL read buffer; "
3100 "%d/%d bytes used on write buffer.",
3101 i, (int)rbuf_len, (int)rbuf_cap, (int)wbuf_len, (int)wbuf_cap);
3106 circuit_dump_by_conn(conn, severity); /* dump info about all the circuits
3107 * using this conn */
3108 } SMARTLIST_FOREACH_END(conn);
3110 channel_dumpstats(severity);
3111 channel_listener_dumpstats(severity);
3113 tor_log(severity, LD_NET,
3114 "Cells processed: "U64_FORMAT" padding\n"
3115 " "U64_FORMAT" create\n"
3116 " "U64_FORMAT" created\n"
3117 " "U64_FORMAT" relay\n"
3118 " ("U64_FORMAT" relayed)\n"
3119 " ("U64_FORMAT" delivered)\n"
3120 " "U64_FORMAT" destroy",
3121 U64_PRINTF_ARG(stats_n_padding_cells_processed),
3122 U64_PRINTF_ARG(stats_n_create_cells_processed),
3123 U64_PRINTF_ARG(stats_n_created_cells_processed),
3124 U64_PRINTF_ARG(stats_n_relay_cells_processed),
3125 U64_PRINTF_ARG(stats_n_relay_cells_relayed),
3126 U64_PRINTF_ARG(stats_n_relay_cells_delivered),
3127 U64_PRINTF_ARG(stats_n_destroy_cells_processed));
3128 if (stats_n_data_cells_packaged)
3129 tor_log(severity,LD_NET,"Average packaged cell fullness: %2.3f%%",
3130 100*(U64_TO_DBL(stats_n_data_bytes_packaged) /
3131 U64_TO_DBL(stats_n_data_cells_packaged*RELAY_PAYLOAD_SIZE)) );
3132 if (stats_n_data_cells_received)
3133 tor_log(severity,LD_NET,"Average delivered cell fullness: %2.3f%%",
3134 100*(U64_TO_DBL(stats_n_data_bytes_received) /
3135 U64_TO_DBL(stats_n_data_cells_received*RELAY_PAYLOAD_SIZE)) );
3137 cpuworker_log_onionskin_overhead(severity, ONION_HANDSHAKE_TYPE_TAP, "TAP");
3138 cpuworker_log_onionskin_overhead(severity, ONION_HANDSHAKE_TYPE_NTOR,"ntor");
3140 if (now - time_of_process_start >= 0)
3141 elapsed = now - time_of_process_start;
3142 else
3143 elapsed = 0;
3145 if (elapsed) {
3146 tor_log(severity, LD_NET,
3147 "Average bandwidth: "U64_FORMAT"/%d = %d bytes/sec reading",
3148 U64_PRINTF_ARG(stats_n_bytes_read),
3149 (int)elapsed,
3150 (int) (stats_n_bytes_read/elapsed));
3151 tor_log(severity, LD_NET,
3152 "Average bandwidth: "U64_FORMAT"/%d = %d bytes/sec writing",
3153 U64_PRINTF_ARG(stats_n_bytes_written),
3154 (int)elapsed,
3155 (int) (stats_n_bytes_written/elapsed));
3158 tor_log(severity, LD_NET, "--------------- Dumping memory information:");
3159 dumpmemusage(severity);
3161 rep_hist_dump_stats(now,severity);
3162 rend_service_dump_stats(severity);
3163 dump_distinct_digest_count(severity);
3166 /** Called by exit() as we shut down the process.
3168 static void
3169 exit_function(void)
3171 /* NOTE: If we ever daemonize, this gets called immediately. That's
3172 * okay for now, because we only use this on Windows. */
3173 #ifdef _WIN32
3174 WSACleanup();
3175 #endif
3178 #ifdef _WIN32
3179 #define UNIX_ONLY 0
3180 #else
3181 #define UNIX_ONLY 1
3182 #endif
3184 static struct {
3185 /** A numeric code for this signal. Must match the signal value if
3186 * try_to_register is true. */
3187 int signal_value;
3188 /** True if we should try to register this signal with libevent and catch
3189 * corresponding posix signals. False otherwise. */
3190 int try_to_register;
3191 /** Pointer to hold the event object constructed for this signal. */
3192 struct event *signal_event;
3193 } signal_handlers[] = {
3194 #ifdef SIGINT
3195 { SIGINT, UNIX_ONLY, NULL }, /* do a controlled slow shutdown */
3196 #endif
3197 #ifdef SIGTERM
3198 { SIGTERM, UNIX_ONLY, NULL }, /* to terminate now */
3199 #endif
3200 #ifdef SIGPIPE
3201 { SIGPIPE, UNIX_ONLY, NULL }, /* otherwise SIGPIPE kills us */
3202 #endif
3203 #ifdef SIGUSR1
3204 { SIGUSR1, UNIX_ONLY, NULL }, /* dump stats */
3205 #endif
3206 #ifdef SIGUSR2
3207 { SIGUSR2, UNIX_ONLY, NULL }, /* go to loglevel debug */
3208 #endif
3209 #ifdef SIGHUP
3210 { SIGHUP, UNIX_ONLY, NULL }, /* to reload config, retry conns, etc */
3211 #endif
3212 #ifdef SIGXFSZ
3213 { SIGXFSZ, UNIX_ONLY, NULL }, /* handle file-too-big resource exhaustion */
3214 #endif
3215 #ifdef SIGCHLD
3216 { SIGCHLD, UNIX_ONLY, NULL }, /* handle dns/cpu workers that exit */
3217 #endif
3218 /* These are controller-only */
3219 { SIGNEWNYM, 0, NULL },
3220 { SIGCLEARDNSCACHE, 0, NULL },
3221 { SIGHEARTBEAT, 0, NULL },
3222 { -1, -1, NULL }
3225 /** Set up the signal handler events for this process, and register them
3226 * with libevent if appropriate. */
3227 void
3228 handle_signals(void)
3230 int i;
3231 const int enabled = !get_options()->DisableSignalHandlers;
3233 for (i = 0; signal_handlers[i].signal_value >= 0; ++i) {
3234 /* Signal handlers are only registered with libevent if they need to catch
3235 * real POSIX signals. We construct these signal handler events in either
3236 * case, though, so that controllers can activate them with the SIGNAL
3237 * command.
3239 if (enabled && signal_handlers[i].try_to_register) {
3240 signal_handlers[i].signal_event =
3241 tor_evsignal_new(tor_libevent_get_base(),
3242 signal_handlers[i].signal_value,
3243 signal_callback,
3244 &signal_handlers[i].signal_value);
3245 if (event_add(signal_handlers[i].signal_event, NULL))
3246 log_warn(LD_BUG, "Error from libevent when adding "
3247 "event for signal %d",
3248 signal_handlers[i].signal_value);
3249 } else {
3250 signal_handlers[i].signal_event =
3251 tor_event_new(tor_libevent_get_base(), -1,
3252 EV_SIGNAL, signal_callback,
3253 &signal_handlers[i].signal_value);
3258 /* Cause the signal handler for signal_num to be called in the event loop. */
3259 void
3260 activate_signal(int signal_num)
3262 int i;
3263 for (i = 0; signal_handlers[i].signal_value >= 0; ++i) {
3264 if (signal_handlers[i].signal_value == signal_num) {
3265 event_active(signal_handlers[i].signal_event, EV_SIGNAL, 1);
3266 return;
3271 /** Main entry point for the Tor command-line client. Return 0 on "success",
3272 * negative on "failure", and positive on "success and exit".
3275 tor_init(int argc, char *argv[])
3277 char progname[256];
3278 int quiet = 0;
3280 time_of_process_start = time(NULL);
3281 init_connection_lists();
3282 /* Have the log set up with our application name. */
3283 tor_snprintf(progname, sizeof(progname), "Tor %s", get_version());
3284 log_set_application_name(progname);
3286 /* Set up the crypto nice and early */
3287 if (crypto_early_init() < 0) {
3288 log_err(LD_GENERAL, "Unable to initialize the crypto subsystem!");
3289 return -1;
3292 /* Initialize the history structures. */
3293 rep_hist_init();
3294 /* Initialize the service cache. */
3295 rend_cache_init();
3296 addressmap_init(); /* Init the client dns cache. Do it always, since it's
3297 * cheap. */
3298 /* Initialize the HS subsystem. */
3299 hs_init();
3302 /* We search for the "quiet" option first, since it decides whether we
3303 * will log anything at all to the command line. */
3304 config_line_t *opts = NULL, *cmdline_opts = NULL;
3305 const config_line_t *cl;
3306 (void) config_parse_commandline(argc, argv, 1, &opts, &cmdline_opts);
3307 for (cl = cmdline_opts; cl; cl = cl->next) {
3308 if (!strcmp(cl->key, "--hush"))
3309 quiet = 1;
3310 if (!strcmp(cl->key, "--quiet") ||
3311 !strcmp(cl->key, "--dump-config"))
3312 quiet = 2;
3313 /* The following options imply --hush */
3314 if (!strcmp(cl->key, "--version") || !strcmp(cl->key, "--digests") ||
3315 !strcmp(cl->key, "--list-torrc-options") ||
3316 !strcmp(cl->key, "--library-versions") ||
3317 !strcmp(cl->key, "--hash-password") ||
3318 !strcmp(cl->key, "-h") || !strcmp(cl->key, "--help")) {
3319 if (quiet < 1)
3320 quiet = 1;
3323 config_free_lines(opts);
3324 config_free_lines(cmdline_opts);
3327 /* give it somewhere to log to initially */
3328 switch (quiet) {
3329 case 2:
3330 /* no initial logging */
3331 break;
3332 case 1:
3333 add_temp_log(LOG_WARN);
3334 break;
3335 default:
3336 add_temp_log(LOG_NOTICE);
3338 quiet_level = quiet;
3341 const char *version = get_version();
3343 log_notice(LD_GENERAL, "Tor %s running on %s with Libevent %s, "
3344 "OpenSSL %s, Zlib %s, Liblzma %s, and Libzstd %s.", version,
3345 get_uname(),
3346 tor_libevent_get_version_str(),
3347 crypto_openssl_get_version_str(),
3348 tor_compress_supports_method(ZLIB_METHOD) ?
3349 tor_compress_version_str(ZLIB_METHOD) : "N/A",
3350 tor_compress_supports_method(LZMA_METHOD) ?
3351 tor_compress_version_str(LZMA_METHOD) : "N/A",
3352 tor_compress_supports_method(ZSTD_METHOD) ?
3353 tor_compress_version_str(ZSTD_METHOD) : "N/A");
3355 log_notice(LD_GENERAL, "Tor can't help you if you use it wrong! "
3356 "Learn how to be safe at "
3357 "https://www.torproject.org/download/download#warning");
3359 if (strstr(version, "alpha") || strstr(version, "beta"))
3360 log_notice(LD_GENERAL, "This version is not a stable Tor release. "
3361 "Expect more bugs than usual.");
3363 tor_compress_log_init_warnings();
3366 #ifdef HAVE_RUST
3367 rust_log_welcome_string();
3368 #endif /* defined(HAVE_RUST) */
3370 if (network_init()<0) {
3371 log_err(LD_BUG,"Error initializing network; exiting.");
3372 return -1;
3374 atexit(exit_function);
3376 int init_rv = options_init_from_torrc(argc,argv);
3377 if (init_rv < 0) {
3378 log_err(LD_CONFIG,"Reading config failed--see warnings above.");
3379 return -1;
3380 } else if (init_rv > 0) {
3381 // We succeeded, and should exit anyway -- probably the user just said
3382 // "--version" or something like that.
3383 return 1;
3386 /* The options are now initialised */
3387 const or_options_t *options = get_options();
3389 /* Initialize channelpadding parameters to defaults until we get
3390 * a consensus */
3391 channelpadding_new_consensus_params(NULL);
3393 /* Initialize predicted ports list after loading options */
3394 predicted_ports_init();
3396 #ifndef _WIN32
3397 if (geteuid()==0)
3398 log_warn(LD_GENERAL,"You are running Tor as root. You don't need to, "
3399 "and you probably shouldn't.");
3400 #endif
3402 if (crypto_global_init(options->HardwareAccel,
3403 options->AccelName,
3404 options->AccelDir)) {
3405 log_err(LD_BUG, "Unable to initialize OpenSSL. Exiting.");
3406 return -1;
3408 stream_choice_seed_weak_rng();
3409 if (tor_init_libevent_rng() < 0) {
3410 log_warn(LD_NET, "Problem initializing libevent RNG.");
3413 /* Scan/clean unparseable descriptors; after reading config */
3414 routerparse_init();
3416 return 0;
3419 /** A lockfile structure, used to prevent two Tors from messing with the
3420 * data directory at once. If this variable is non-NULL, we're holding
3421 * the lockfile. */
3422 static tor_lockfile_t *lockfile = NULL;
3424 /** Try to grab the lock file described in <b>options</b>, if we do not
3425 * already have it. If <b>err_if_locked</b> is true, warn if somebody else is
3426 * holding the lock, and exit if we can't get it after waiting. Otherwise,
3427 * return -1 if we can't get the lockfile. Return 0 on success.
3430 try_locking(const or_options_t *options, int err_if_locked)
3432 if (lockfile)
3433 return 0;
3434 else {
3435 char *fname = options_get_datadir_fname(options, "lock");
3436 int already_locked = 0;
3437 tor_lockfile_t *lf = tor_lockfile_lock(fname, 0, &already_locked);
3438 tor_free(fname);
3439 if (!lf) {
3440 if (err_if_locked && already_locked) {
3441 int r;
3442 log_warn(LD_GENERAL, "It looks like another Tor process is running "
3443 "with the same data directory. Waiting 5 seconds to see "
3444 "if it goes away.");
3445 #ifndef _WIN32
3446 sleep(5);
3447 #else
3448 Sleep(5000);
3449 #endif
3450 r = try_locking(options, 0);
3451 if (r<0) {
3452 log_err(LD_GENERAL, "No, it's still there. Exiting.");
3453 return -1;
3455 return r;
3457 return -1;
3459 lockfile = lf;
3460 return 0;
3464 /** Return true iff we've successfully acquired the lock file. */
3466 have_lockfile(void)
3468 return lockfile != NULL;
3471 /** If we have successfully acquired the lock file, release it. */
3472 void
3473 release_lockfile(void)
3475 if (lockfile) {
3476 tor_lockfile_unlock(lockfile);
3477 lockfile = NULL;
3481 /** Free all memory that we might have allocated somewhere.
3482 * If <b>postfork</b>, we are a worker process and we want to free
3483 * only the parts of memory that we won't touch. If !<b>postfork</b>,
3484 * Tor is shutting down and we should free everything.
3486 * Helps us find the real leaks with dmalloc and the like. Also valgrind
3487 * should then report 0 reachable in its leak report (in an ideal world --
3488 * in practice libevent, SSL, libc etc never quite free everything). */
3489 void
3490 tor_free_all(int postfork)
3492 if (!postfork) {
3493 evdns_shutdown(1);
3495 geoip_free_all();
3496 dirvote_free_all();
3497 routerlist_free_all();
3498 networkstatus_free_all();
3499 addressmap_free_all();
3500 dirserv_free_all();
3501 rend_cache_free_all();
3502 rend_service_authorization_free_all();
3503 rep_hist_free_all();
3504 dns_free_all();
3505 clear_pending_onions();
3506 circuit_free_all();
3507 entry_guards_free_all();
3508 pt_free_all();
3509 channel_tls_free_all();
3510 channel_free_all();
3511 connection_free_all();
3512 connection_edge_free_all();
3513 scheduler_free_all();
3514 nodelist_free_all();
3515 microdesc_free_all();
3516 routerparse_free_all();
3517 ext_orport_free_all();
3518 control_free_all();
3519 sandbox_free_getaddrinfo_cache();
3520 protover_free_all();
3521 bridges_free_all();
3522 consdiffmgr_free_all();
3523 hs_free_all();
3524 dos_free_all();
3525 if (!postfork) {
3526 config_free_all();
3527 or_state_free_all();
3528 router_free_all();
3529 routerkeys_free_all();
3530 policies_free_all();
3532 if (!postfork) {
3533 tor_tls_free_all();
3534 #ifndef _WIN32
3535 tor_getpwnam(NULL);
3536 #endif
3538 /* stuff in main.c */
3540 smartlist_free(connection_array);
3541 smartlist_free(closeable_connection_lst);
3542 smartlist_free(active_linked_connection_lst);
3543 periodic_timer_free(second_timer);
3544 teardown_periodic_events();
3545 tor_event_free(shutdown_did_not_work_event);
3546 tor_event_free(initialize_periodic_events_event);
3547 mainloop_event_free(directory_all_unreachable_cb_event);
3548 mainloop_event_free(schedule_active_linked_connections_event);
3549 mainloop_event_free(postloop_cleanup_ev);
3551 #ifdef HAVE_SYSTEMD_209
3552 periodic_timer_free(systemd_watchdog_timer);
3553 #endif
3555 memset(&global_bucket, 0, sizeof(global_bucket));
3556 memset(&global_relayed_bucket, 0, sizeof(global_relayed_bucket));
3557 stats_prev_n_read = stats_prev_n_written = 0;
3558 stats_n_bytes_read = stats_n_bytes_written = 0;
3559 time_of_process_start = 0;
3560 time_of_last_signewnym = 0;
3561 signewnym_is_pending = 0;
3562 newnym_epoch = 0;
3563 called_loop_once = 0;
3564 main_loop_should_exit = 0;
3565 main_loop_exit_value = 0;
3566 can_complete_circuits = 0;
3567 quiet_level = 0;
3568 should_init_bridge_stats = 1;
3569 dns_honesty_first_time = 1;
3570 heartbeat_callback_first_time = 1;
3571 n_libevent_errors = 0;
3572 current_second = 0;
3574 if (!postfork) {
3575 release_lockfile();
3577 tor_libevent_free_all();
3578 /* Stuff in util.c and address.c*/
3579 if (!postfork) {
3580 escaped(NULL);
3581 esc_router_info(NULL);
3582 clean_up_backtrace_handler();
3583 logs_free_all(); /* free log strings. do this last so logs keep working. */
3588 * Remove the specified file, and log a warning if the operation fails for
3589 * any reason other than the file not existing. Ignores NULL filenames.
3591 void
3592 tor_remove_file(const char *filename)
3594 if (filename && tor_unlink(filename) != 0 && errno != ENOENT) {
3595 log_warn(LD_FS, "Couldn't unlink %s: %s",
3596 filename, strerror(errno));
3600 /** Do whatever cleanup is necessary before shutting Tor down. */
3601 void
3602 tor_cleanup(void)
3604 const or_options_t *options = get_options();
3605 if (options->command == CMD_RUN_TOR) {
3606 time_t now = time(NULL);
3607 /* Remove our pid file. We don't care if there was an error when we
3608 * unlink, nothing we could do about it anyways. */
3609 tor_remove_file(options->PidFile);
3610 /* Remove control port file */
3611 tor_remove_file(options->ControlPortWriteToFile);
3612 /* Remove cookie authentication file */
3614 char *cookie_fname = get_controller_cookie_file_name();
3615 tor_remove_file(cookie_fname);
3616 tor_free(cookie_fname);
3618 /* Remove Extended ORPort cookie authentication file */
3620 char *cookie_fname = get_ext_or_auth_cookie_file_name();
3621 tor_remove_file(cookie_fname);
3622 tor_free(cookie_fname);
3624 if (accounting_is_enabled(options))
3625 accounting_record_bandwidth_usage(now, get_or_state());
3626 or_state_mark_dirty(get_or_state(), 0); /* force an immediate save. */
3627 or_state_save(now);
3628 if (authdir_mode(options)) {
3629 sr_save_and_cleanup();
3631 if (authdir_mode_tests_reachability(options))
3632 rep_hist_record_mtbf_data(now, 0);
3633 keypin_close_journal();
3636 timers_shutdown();
3638 #ifdef USE_DMALLOC
3639 dmalloc_log_stats();
3640 #endif
3641 tor_free_all(0); /* We could move tor_free_all back into the ifdef below
3642 later, if it makes shutdown unacceptably slow. But for
3643 now, leave it here: it's helped us catch bugs in the
3644 past. */
3645 crypto_global_cleanup();
3646 #ifdef USE_DMALLOC
3647 dmalloc_log_unfreed();
3648 dmalloc_shutdown();
3649 #endif
3652 /** Read/create keys as needed, and echo our fingerprint to stdout. */
3653 static int
3654 do_list_fingerprint(void)
3656 char buf[FINGERPRINT_LEN+1];
3657 crypto_pk_t *k;
3658 const char *nickname = get_options()->Nickname;
3659 sandbox_disable_getaddrinfo_cache();
3660 if (!server_mode(get_options())) {
3661 log_err(LD_GENERAL,
3662 "Clients don't have long-term identity keys. Exiting.");
3663 return -1;
3665 tor_assert(nickname);
3666 if (init_keys() < 0) {
3667 log_err(LD_GENERAL,"Error initializing keys; exiting.");
3668 return -1;
3670 if (!(k = get_server_identity_key())) {
3671 log_err(LD_GENERAL,"Error: missing identity key.");
3672 return -1;
3674 if (crypto_pk_get_fingerprint(k, buf, 1)<0) {
3675 log_err(LD_BUG, "Error computing fingerprint");
3676 return -1;
3678 printf("%s %s\n", nickname, buf);
3679 return 0;
3682 /** Entry point for password hashing: take the desired password from
3683 * the command line, and print its salted hash to stdout. **/
3684 static void
3685 do_hash_password(void)
3688 char output[256];
3689 char key[S2K_RFC2440_SPECIFIER_LEN+DIGEST_LEN];
3691 crypto_rand(key, S2K_RFC2440_SPECIFIER_LEN-1);
3692 key[S2K_RFC2440_SPECIFIER_LEN-1] = (uint8_t)96; /* Hash 64 K of data. */
3693 secret_to_key_rfc2440(key+S2K_RFC2440_SPECIFIER_LEN, DIGEST_LEN,
3694 get_options()->command_arg, strlen(get_options()->command_arg),
3695 key);
3696 base16_encode(output, sizeof(output), key, sizeof(key));
3697 printf("16:%s\n",output);
3700 /** Entry point for configuration dumping: write the configuration to
3701 * stdout. */
3702 static int
3703 do_dump_config(void)
3705 const or_options_t *options = get_options();
3706 const char *arg = options->command_arg;
3707 int how;
3708 char *opts;
3710 if (!strcmp(arg, "short")) {
3711 how = OPTIONS_DUMP_MINIMAL;
3712 } else if (!strcmp(arg, "non-builtin")) {
3713 how = OPTIONS_DUMP_DEFAULTS;
3714 } else if (!strcmp(arg, "full")) {
3715 how = OPTIONS_DUMP_ALL;
3716 } else {
3717 fprintf(stderr, "No valid argument to --dump-config found!\n");
3718 fprintf(stderr, "Please select 'short', 'non-builtin', or 'full'.\n");
3720 return -1;
3723 opts = options_dump(options, how);
3724 printf("%s", opts);
3725 tor_free(opts);
3727 return 0;
3730 static void
3731 init_addrinfo(void)
3733 if (! server_mode(get_options()) ||
3734 (get_options()->Address && strlen(get_options()->Address) > 0)) {
3735 /* We don't need to seed our own hostname, because we won't be calling
3736 * resolve_my_address on it.
3738 return;
3740 char hname[256];
3742 // host name to sandbox
3743 gethostname(hname, sizeof(hname));
3744 sandbox_add_addrinfo(hname);
3747 static sandbox_cfg_t*
3748 sandbox_init_filter(void)
3750 const or_options_t *options = get_options();
3751 sandbox_cfg_t *cfg = sandbox_cfg_new();
3752 int i;
3754 sandbox_cfg_allow_openat_filename(&cfg,
3755 get_cachedir_fname("cached-status"));
3757 #define OPEN(name) \
3758 sandbox_cfg_allow_open_filename(&cfg, tor_strdup(name))
3760 #define OPEN_DATADIR(name) \
3761 sandbox_cfg_allow_open_filename(&cfg, get_datadir_fname(name))
3763 #define OPEN_DATADIR2(name, name2) \
3764 sandbox_cfg_allow_open_filename(&cfg, get_datadir_fname2((name), (name2)))
3766 #define OPEN_DATADIR_SUFFIX(name, suffix) do { \
3767 OPEN_DATADIR(name); \
3768 OPEN_DATADIR(name suffix); \
3769 } while (0)
3771 #define OPEN_DATADIR2_SUFFIX(name, name2, suffix) do { \
3772 OPEN_DATADIR2(name, name2); \
3773 OPEN_DATADIR2(name, name2 suffix); \
3774 } while (0)
3776 #define OPEN_KEY_DIRECTORY() \
3777 sandbox_cfg_allow_open_filename(&cfg, tor_strdup(options->KeyDirectory))
3778 #define OPEN_CACHEDIR(name) \
3779 sandbox_cfg_allow_open_filename(&cfg, get_cachedir_fname(name))
3780 #define OPEN_CACHEDIR_SUFFIX(name, suffix) do { \
3781 OPEN_CACHEDIR(name); \
3782 OPEN_CACHEDIR(name suffix); \
3783 } while (0)
3784 #define OPEN_KEYDIR(name) \
3785 sandbox_cfg_allow_open_filename(&cfg, get_keydir_fname(name))
3786 #define OPEN_KEYDIR_SUFFIX(name, suffix) do { \
3787 OPEN_KEYDIR(name); \
3788 OPEN_KEYDIR(name suffix); \
3789 } while (0)
3791 OPEN(options->DataDirectory);
3792 OPEN_KEY_DIRECTORY();
3794 OPEN_CACHEDIR_SUFFIX("cached-certs", ".tmp");
3795 OPEN_CACHEDIR_SUFFIX("cached-consensus", ".tmp");
3796 OPEN_CACHEDIR_SUFFIX("unverified-consensus", ".tmp");
3797 OPEN_CACHEDIR_SUFFIX("unverified-microdesc-consensus", ".tmp");
3798 OPEN_CACHEDIR_SUFFIX("cached-microdesc-consensus", ".tmp");
3799 OPEN_CACHEDIR_SUFFIX("cached-microdescs", ".tmp");
3800 OPEN_CACHEDIR_SUFFIX("cached-microdescs.new", ".tmp");
3801 OPEN_CACHEDIR_SUFFIX("cached-descriptors", ".tmp");
3802 OPEN_CACHEDIR_SUFFIX("cached-descriptors.new", ".tmp");
3803 OPEN_CACHEDIR("cached-descriptors.tmp.tmp");
3804 OPEN_CACHEDIR_SUFFIX("cached-extrainfo", ".tmp");
3805 OPEN_CACHEDIR_SUFFIX("cached-extrainfo.new", ".tmp");
3806 OPEN_CACHEDIR("cached-extrainfo.tmp.tmp");
3808 OPEN_DATADIR_SUFFIX("state", ".tmp");
3809 OPEN_DATADIR_SUFFIX("sr-state", ".tmp");
3810 OPEN_DATADIR_SUFFIX("unparseable-desc", ".tmp");
3811 OPEN_DATADIR_SUFFIX("v3-status-votes", ".tmp");
3812 OPEN_DATADIR("key-pinning-journal");
3813 OPEN("/dev/srandom");
3814 OPEN("/dev/urandom");
3815 OPEN("/dev/random");
3816 OPEN("/etc/hosts");
3817 OPEN("/proc/meminfo");
3819 if (options->BridgeAuthoritativeDir)
3820 OPEN_DATADIR_SUFFIX("networkstatus-bridges", ".tmp");
3822 if (authdir_mode(options))
3823 OPEN_DATADIR("approved-routers");
3825 if (options->ServerDNSResolvConfFile)
3826 sandbox_cfg_allow_open_filename(&cfg,
3827 tor_strdup(options->ServerDNSResolvConfFile));
3828 else
3829 sandbox_cfg_allow_open_filename(&cfg, tor_strdup("/etc/resolv.conf"));
3831 for (i = 0; i < 2; ++i) {
3832 if (get_torrc_fname(i)) {
3833 sandbox_cfg_allow_open_filename(&cfg, tor_strdup(get_torrc_fname(i)));
3837 SMARTLIST_FOREACH(options->FilesOpenedByIncludes, char *, f, {
3838 OPEN(f);
3841 #define RENAME_SUFFIX(name, suffix) \
3842 sandbox_cfg_allow_rename(&cfg, \
3843 get_datadir_fname(name suffix), \
3844 get_datadir_fname(name))
3846 #define RENAME_SUFFIX2(prefix, name, suffix) \
3847 sandbox_cfg_allow_rename(&cfg, \
3848 get_datadir_fname2(prefix, name suffix), \
3849 get_datadir_fname2(prefix, name))
3851 #define RENAME_CACHEDIR_SUFFIX(name, suffix) \
3852 sandbox_cfg_allow_rename(&cfg, \
3853 get_cachedir_fname(name suffix), \
3854 get_cachedir_fname(name))
3856 #define RENAME_KEYDIR_SUFFIX(name, suffix) \
3857 sandbox_cfg_allow_rename(&cfg, \
3858 get_keydir_fname(name suffix), \
3859 get_keydir_fname(name))
3861 RENAME_CACHEDIR_SUFFIX("cached-certs", ".tmp");
3862 RENAME_CACHEDIR_SUFFIX("cached-consensus", ".tmp");
3863 RENAME_CACHEDIR_SUFFIX("unverified-consensus", ".tmp");
3864 RENAME_CACHEDIR_SUFFIX("unverified-microdesc-consensus", ".tmp");
3865 RENAME_CACHEDIR_SUFFIX("cached-microdesc-consensus", ".tmp");
3866 RENAME_CACHEDIR_SUFFIX("cached-microdescs", ".tmp");
3867 RENAME_CACHEDIR_SUFFIX("cached-microdescs", ".new");
3868 RENAME_CACHEDIR_SUFFIX("cached-microdescs.new", ".tmp");
3869 RENAME_CACHEDIR_SUFFIX("cached-descriptors", ".tmp");
3870 RENAME_CACHEDIR_SUFFIX("cached-descriptors", ".new");
3871 RENAME_CACHEDIR_SUFFIX("cached-descriptors.new", ".tmp");
3872 RENAME_CACHEDIR_SUFFIX("cached-extrainfo", ".tmp");
3873 RENAME_CACHEDIR_SUFFIX("cached-extrainfo", ".new");
3874 RENAME_CACHEDIR_SUFFIX("cached-extrainfo.new", ".tmp");
3876 RENAME_SUFFIX("state", ".tmp");
3877 RENAME_SUFFIX("sr-state", ".tmp");
3878 RENAME_SUFFIX("unparseable-desc", ".tmp");
3879 RENAME_SUFFIX("v3-status-votes", ".tmp");
3881 if (options->BridgeAuthoritativeDir)
3882 RENAME_SUFFIX("networkstatus-bridges", ".tmp");
3884 #define STAT_DATADIR(name) \
3885 sandbox_cfg_allow_stat_filename(&cfg, get_datadir_fname(name))
3887 #define STAT_CACHEDIR(name) \
3888 sandbox_cfg_allow_stat_filename(&cfg, get_cachedir_fname(name))
3890 #define STAT_DATADIR2(name, name2) \
3891 sandbox_cfg_allow_stat_filename(&cfg, get_datadir_fname2((name), (name2)))
3893 #define STAT_KEY_DIRECTORY() \
3894 sandbox_cfg_allow_stat_filename(&cfg, tor_strdup(options->KeyDirectory))
3896 STAT_DATADIR(NULL);
3897 STAT_DATADIR("lock");
3898 STAT_DATADIR("state");
3899 STAT_DATADIR("router-stability");
3901 STAT_CACHEDIR("cached-extrainfo.new");
3904 smartlist_t *files = smartlist_new();
3905 tor_log_get_logfile_names(files);
3906 SMARTLIST_FOREACH(files, char *, file_name, {
3907 /* steals reference */
3908 sandbox_cfg_allow_open_filename(&cfg, file_name);
3910 smartlist_free(files);
3914 smartlist_t *files = smartlist_new();
3915 smartlist_t *dirs = smartlist_new();
3916 hs_service_lists_fnames_for_sandbox(files, dirs);
3917 SMARTLIST_FOREACH(files, char *, file_name, {
3918 char *tmp_name = NULL;
3919 tor_asprintf(&tmp_name, "%s.tmp", file_name);
3920 sandbox_cfg_allow_rename(&cfg,
3921 tor_strdup(tmp_name), tor_strdup(file_name));
3922 /* steals references */
3923 sandbox_cfg_allow_open_filename(&cfg, file_name);
3924 sandbox_cfg_allow_open_filename(&cfg, tmp_name);
3926 SMARTLIST_FOREACH(dirs, char *, dir, {
3927 /* steals reference */
3928 sandbox_cfg_allow_stat_filename(&cfg, dir);
3930 smartlist_free(files);
3931 smartlist_free(dirs);
3935 char *fname;
3936 if ((fname = get_controller_cookie_file_name())) {
3937 sandbox_cfg_allow_open_filename(&cfg, fname);
3939 if ((fname = get_ext_or_auth_cookie_file_name())) {
3940 sandbox_cfg_allow_open_filename(&cfg, fname);
3944 SMARTLIST_FOREACH_BEGIN(get_configured_ports(), port_cfg_t *, port) {
3945 if (!port->is_unix_addr)
3946 continue;
3947 /* When we open an AF_UNIX address, we want permission to open the
3948 * directory that holds it. */
3949 char *dirname = tor_strdup(port->unix_addr);
3950 if (get_parent_directory(dirname) == 0) {
3951 OPEN(dirname);
3953 tor_free(dirname);
3954 sandbox_cfg_allow_chmod_filename(&cfg, tor_strdup(port->unix_addr));
3955 sandbox_cfg_allow_chown_filename(&cfg, tor_strdup(port->unix_addr));
3956 } SMARTLIST_FOREACH_END(port);
3958 if (options->DirPortFrontPage) {
3959 sandbox_cfg_allow_open_filename(&cfg,
3960 tor_strdup(options->DirPortFrontPage));
3963 // orport
3964 if (server_mode(get_options())) {
3966 OPEN_KEYDIR_SUFFIX("secret_id_key", ".tmp");
3967 OPEN_KEYDIR_SUFFIX("secret_onion_key", ".tmp");
3968 OPEN_KEYDIR_SUFFIX("secret_onion_key_ntor", ".tmp");
3969 OPEN_KEYDIR("secret_id_key.old");
3970 OPEN_KEYDIR("secret_onion_key.old");
3971 OPEN_KEYDIR("secret_onion_key_ntor.old");
3973 OPEN_KEYDIR_SUFFIX("ed25519_master_id_secret_key", ".tmp");
3974 OPEN_KEYDIR_SUFFIX("ed25519_master_id_secret_key_encrypted", ".tmp");
3975 OPEN_KEYDIR_SUFFIX("ed25519_master_id_public_key", ".tmp");
3976 OPEN_KEYDIR_SUFFIX("ed25519_signing_secret_key", ".tmp");
3977 OPEN_KEYDIR_SUFFIX("ed25519_signing_secret_key_encrypted", ".tmp");
3978 OPEN_KEYDIR_SUFFIX("ed25519_signing_public_key", ".tmp");
3979 OPEN_KEYDIR_SUFFIX("ed25519_signing_cert", ".tmp");
3981 OPEN_DATADIR2_SUFFIX("stats", "bridge-stats", ".tmp");
3982 OPEN_DATADIR2_SUFFIX("stats", "dirreq-stats", ".tmp");
3984 OPEN_DATADIR2_SUFFIX("stats", "entry-stats", ".tmp");
3985 OPEN_DATADIR2_SUFFIX("stats", "exit-stats", ".tmp");
3986 OPEN_DATADIR2_SUFFIX("stats", "buffer-stats", ".tmp");
3987 OPEN_DATADIR2_SUFFIX("stats", "conn-stats", ".tmp");
3988 OPEN_DATADIR2_SUFFIX("stats", "hidserv-stats", ".tmp");
3990 OPEN_DATADIR("approved-routers");
3991 OPEN_DATADIR_SUFFIX("fingerprint", ".tmp");
3992 OPEN_DATADIR_SUFFIX("hashed-fingerprint", ".tmp");
3993 OPEN_DATADIR_SUFFIX("router-stability", ".tmp");
3995 OPEN("/etc/resolv.conf");
3997 RENAME_SUFFIX("fingerprint", ".tmp");
3998 RENAME_KEYDIR_SUFFIX("secret_onion_key_ntor", ".tmp");
4000 RENAME_KEYDIR_SUFFIX("secret_id_key", ".tmp");
4001 RENAME_KEYDIR_SUFFIX("secret_id_key.old", ".tmp");
4002 RENAME_KEYDIR_SUFFIX("secret_onion_key", ".tmp");
4003 RENAME_KEYDIR_SUFFIX("secret_onion_key.old", ".tmp");
4005 RENAME_SUFFIX2("stats", "bridge-stats", ".tmp");
4006 RENAME_SUFFIX2("stats", "dirreq-stats", ".tmp");
4007 RENAME_SUFFIX2("stats", "entry-stats", ".tmp");
4008 RENAME_SUFFIX2("stats", "exit-stats", ".tmp");
4009 RENAME_SUFFIX2("stats", "buffer-stats", ".tmp");
4010 RENAME_SUFFIX2("stats", "conn-stats", ".tmp");
4011 RENAME_SUFFIX2("stats", "hidserv-stats", ".tmp");
4012 RENAME_SUFFIX("hashed-fingerprint", ".tmp");
4013 RENAME_SUFFIX("router-stability", ".tmp");
4015 RENAME_KEYDIR_SUFFIX("ed25519_master_id_secret_key", ".tmp");
4016 RENAME_KEYDIR_SUFFIX("ed25519_master_id_secret_key_encrypted", ".tmp");
4017 RENAME_KEYDIR_SUFFIX("ed25519_master_id_public_key", ".tmp");
4018 RENAME_KEYDIR_SUFFIX("ed25519_signing_secret_key", ".tmp");
4019 RENAME_KEYDIR_SUFFIX("ed25519_signing_cert", ".tmp");
4021 sandbox_cfg_allow_rename(&cfg,
4022 get_keydir_fname("secret_onion_key"),
4023 get_keydir_fname("secret_onion_key.old"));
4024 sandbox_cfg_allow_rename(&cfg,
4025 get_keydir_fname("secret_onion_key_ntor"),
4026 get_keydir_fname("secret_onion_key_ntor.old"));
4028 STAT_KEY_DIRECTORY();
4029 OPEN_DATADIR("stats");
4030 STAT_DATADIR("stats");
4031 STAT_DATADIR2("stats", "dirreq-stats");
4033 consdiffmgr_register_with_sandbox(&cfg);
4036 init_addrinfo();
4038 return cfg;
4041 /* Main entry point for the Tor process. Called from tor_main(), and by
4042 * anybody embedding Tor. */
4044 tor_run_main(const tor_main_configuration_t *tor_cfg)
4046 int result = 0;
4048 int argc = tor_cfg->argc;
4049 char **argv = tor_cfg->argv;
4051 #ifdef _WIN32
4052 #ifndef HeapEnableTerminationOnCorruption
4053 #define HeapEnableTerminationOnCorruption 1
4054 #endif
4055 /* On heap corruption, just give up; don't try to play along. */
4056 HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0);
4057 /* Call SetProcessDEPPolicy to permanently enable DEP.
4058 The function will not resolve on earlier versions of Windows,
4059 and failure is not dangerous. */
4060 HMODULE hMod = GetModuleHandleA("Kernel32.dll");
4061 if (hMod) {
4062 typedef BOOL (WINAPI *PSETDEP)(DWORD);
4063 PSETDEP setdeppolicy = (PSETDEP)GetProcAddress(hMod,
4064 "SetProcessDEPPolicy");
4065 if (setdeppolicy) {
4066 /* PROCESS_DEP_ENABLE | PROCESS_DEP_DISABLE_ATL_THUNK_EMULATION */
4067 setdeppolicy(3);
4070 #endif /* defined(_WIN32) */
4072 configure_backtrace_handler(get_version());
4073 init_protocol_warning_severity_level();
4075 update_approx_time(time(NULL));
4076 tor_threads_init();
4077 tor_compress_init();
4078 init_logging(0);
4079 monotime_init();
4080 #ifdef USE_DMALLOC
4082 /* Instruct OpenSSL to use our internal wrappers for malloc,
4083 realloc and free. */
4084 int r = crypto_use_tor_alloc_functions();
4085 tor_assert(r == 0);
4087 #endif /* defined(USE_DMALLOC) */
4088 #ifdef NT_SERVICE
4090 int done = 0;
4091 result = nt_service_parse_options(argc, argv, &done);
4092 if (done) return result;
4094 #endif /* defined(NT_SERVICE) */
4096 int init_rv = tor_init(argc, argv);
4097 if (init_rv < 0)
4098 return -1;
4099 else if (init_rv > 0)
4100 return 0;
4103 if (get_options()->Sandbox && get_options()->command == CMD_RUN_TOR) {
4104 sandbox_cfg_t* cfg = sandbox_init_filter();
4106 if (sandbox_init(cfg)) {
4107 log_err(LD_BUG,"Failed to create syscall sandbox filter");
4108 return -1;
4111 // registering libevent rng
4112 #ifdef HAVE_EVUTIL_SECURE_RNG_SET_URANDOM_DEVICE_FILE
4113 evutil_secure_rng_set_urandom_device_file(
4114 (char*) sandbox_intern_string("/dev/urandom"));
4115 #endif
4118 switch (get_options()->command) {
4119 case CMD_RUN_TOR:
4120 #ifdef NT_SERVICE
4121 nt_service_set_state(SERVICE_RUNNING);
4122 #endif
4123 result = do_main_loop();
4124 break;
4125 case CMD_KEYGEN:
4126 result = load_ed_keys(get_options(), time(NULL)) < 0;
4127 break;
4128 case CMD_KEY_EXPIRATION:
4129 init_keys();
4130 result = log_cert_expiration();
4131 break;
4132 case CMD_LIST_FINGERPRINT:
4133 result = do_list_fingerprint();
4134 break;
4135 case CMD_HASH_PASSWORD:
4136 do_hash_password();
4137 result = 0;
4138 break;
4139 case CMD_VERIFY_CONFIG:
4140 if (quiet_level == 0)
4141 printf("Configuration was valid\n");
4142 result = 0;
4143 break;
4144 case CMD_DUMP_CONFIG:
4145 result = do_dump_config();
4146 break;
4147 case CMD_RUN_UNITTESTS: /* only set by test.c */
4148 default:
4149 log_warn(LD_BUG,"Illegal command number %d: internal error.",
4150 get_options()->command);
4151 result = -1;
4153 tor_cleanup();
4154 return result;