Rename dirauth/mode.h to dirauth/authmode.h
[tor.git] / src / core / mainloop / mainloop.c
blob105638ffae1ec7f3ca0e3096b9409449b935daa1
1 /* Copyright (c) 2001 Matej Pfajfar.
2 * Copyright (c) 2001-2004, Roger Dingledine.
3 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
4 * Copyright (c) 2007-2018, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
7 /**
8 * \file mainloop.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 MAINLOOP_PRIVATE
50 #include "core/or/or.h"
52 #include "feature/client/addressmap.h"
53 #include "lib/err/backtrace.h"
54 #include "feature/client/bridges.h"
55 #include "lib/container/buffers.h"
56 #include "lib/tls/buffers_tls.h"
57 #include "core/or/channel.h"
58 #include "core/or/channeltls.h"
59 #include "core/or/channelpadding.h"
60 #include "core/or/circuitbuild.h"
61 #include "core/or/circuitlist.h"
62 #include "core/or/circuituse.h"
63 #include "app/config/config.h"
64 #include "core/mainloop/connection.h"
65 #include "core/or/connection_edge.h"
66 #include "core/or/connection_or.h"
67 #include "feature/dircache/consdiffmgr.h"
68 #include "feature/control/control.h"
69 #include "core/mainloop/cpuworker.h"
70 #include "lib/crypt_ops/crypto_rand.h"
71 #include "feature/dircommon/directory.h"
72 #include "feature/dircache/dirserv.h"
73 #include "feature/dirauth/reachability.h"
74 #include "feature/relay/dns.h"
75 #include "feature/client/dnsserv.h"
76 #include "core/or/dos.h"
77 #include "feature/client/entrynodes.h"
78 #include "feature/stats/geoip.h"
79 #include "feature/hibernate/hibernate.h"
80 #include "feature/hs/hs_cache.h"
81 #include "feature/hs/hs_client.h"
82 #include "feature/hs/hs_service.h"
83 #include "core/mainloop/mainloop.h"
84 #include "feature/nodelist/microdesc.h"
85 #include "feature/nodelist/networkstatus.h"
86 #include "feature/nodelist/nodelist.h"
87 #include "app/main/ntmain.h"
88 #include "core/mainloop/periodic.h"
89 #include "feature/client/transports.h"
90 #include "feature/rend/rendcache.h"
91 #include "feature/rend/rendservice.h"
92 #include "feature/stats/rephist.h"
93 #include "feature/relay/router.h"
94 #include "feature/relay/routerkeys.h"
95 #include "feature/relay/selftest.h"
96 #include "feature/nodelist/routerlist.h"
97 #include "app/config/statefile.h"
98 #include "core/or/status.h"
99 #include "lib/net/buffers_net.h"
100 #include "lib/evloop/compat_libevent.h"
102 #include <event2/event.h>
104 #include "feature/dirauth/dirvote.h"
105 #include "feature/dirauth/authmode.h"
107 #include "core/or/cell_st.h"
108 #include "core/or/entry_connection_st.h"
109 #include "feature/nodelist/networkstatus_st.h"
110 #include "core/or/or_connection_st.h"
111 #include "app/config/or_state_st.h"
112 #include "feature/nodelist/routerinfo_st.h"
113 #include "core/or/socks_request_st.h"
115 #ifdef HAVE_UNISTD_H
116 #include <unistd.h>
117 #endif
119 #ifdef HAVE_SYSTEMD
120 # if defined(__COVERITY__) && !defined(__INCLUDE_LEVEL__)
121 /* Systemd's use of gcc's __INCLUDE_LEVEL__ extension macro appears to confuse
122 * Coverity. Here's a kludge to unconfuse it.
124 # define __INCLUDE_LEVEL__ 2
125 #endif /* defined(__COVERITY__) && !defined(__INCLUDE_LEVEL__) */
126 #include <systemd/sd-daemon.h>
127 #endif /* defined(HAVE_SYSTEMD) */
129 /* Token bucket for all traffic. */
130 token_bucket_rw_t global_bucket;
132 /* Token bucket for relayed traffic. */
133 token_bucket_rw_t global_relayed_bucket;
135 /* XXX we might want to keep stats about global_relayed_*_bucket too. Or not.*/
136 /** How many bytes have we read since we started the process? */
137 static uint64_t stats_n_bytes_read = 0;
138 /** How many bytes have we written since we started the process? */
139 static uint64_t stats_n_bytes_written = 0;
140 /** What time did this process start up? */
141 time_t time_of_process_start = 0;
142 /** How many seconds have we been running? */
143 static long stats_n_seconds_working = 0;
144 /** How many times have we returned from the main loop successfully? */
145 static uint64_t stats_n_main_loop_successes = 0;
146 /** How many times have we received an error from the main loop? */
147 static uint64_t stats_n_main_loop_errors = 0;
148 /** How many times have we returned from the main loop with no events. */
149 static uint64_t stats_n_main_loop_idle = 0;
151 /** How often will we honor SIGNEWNYM requests? */
152 #define MAX_SIGNEWNYM_RATE 10
153 /** When did we last process a SIGNEWNYM request? */
154 static time_t time_of_last_signewnym = 0;
155 /** Is there a signewnym request we're currently waiting to handle? */
156 static int signewnym_is_pending = 0;
157 /** Mainloop event for the deferred signewnym call. */
158 static mainloop_event_t *handle_deferred_signewnym_ev = NULL;
159 /** How many times have we called newnym? */
160 static unsigned newnym_epoch = 0;
162 /** Smartlist of all open connections. */
163 STATIC smartlist_t *connection_array = NULL;
164 /** List of connections that have been marked for close and need to be freed
165 * and removed from connection_array. */
166 static smartlist_t *closeable_connection_lst = NULL;
167 /** List of linked connections that are currently reading data into their
168 * inbuf from their partner's outbuf. */
169 static smartlist_t *active_linked_connection_lst = NULL;
170 /** Flag: Set to true iff we entered the current libevent main loop via
171 * <b>loop_once</b>. If so, there's no need to trigger a loopexit in order
172 * to handle linked connections. */
173 static int called_loop_once = 0;
174 /** Flag: if true, it's time to shut down, so the main loop should exit as
175 * soon as possible.
177 static int main_loop_should_exit = 0;
178 /** The return value that the main loop should yield when it exits, if
179 * main_loop_should_exit is true.
181 static int main_loop_exit_value = 0;
183 /** We set this to 1 when we've opened a circuit, so we can print a log
184 * entry to inform the user that Tor is working. We set it to 0 when
185 * we think the fact that we once opened a circuit doesn't mean we can do so
186 * any longer (a big time jump happened, when we notice our directory is
187 * heinously out-of-date, etc.
189 static int can_complete_circuits = 0;
191 /** How often do we check for router descriptors that we should download
192 * when we have too little directory info? */
193 #define GREEDY_DESCRIPTOR_RETRY_INTERVAL (10)
194 /** How often do we check for router descriptors that we should download
195 * when we have enough directory info? */
196 #define LAZY_DESCRIPTOR_RETRY_INTERVAL (60)
198 static int conn_close_if_marked(int i);
199 static int run_main_loop_until_done(void);
200 static void connection_start_reading_from_linked_conn(connection_t *conn);
201 static int connection_should_read_from_linked_conn(connection_t *conn);
202 static void conn_read_callback(evutil_socket_t fd, short event, void *_conn);
203 static void conn_write_callback(evutil_socket_t fd, short event, void *_conn);
204 static void second_elapsed_callback(periodic_timer_t *timer, void *args);
205 static void shutdown_did_not_work_callback(evutil_socket_t fd, short event,
206 void *arg) ATTR_NORETURN;
208 /****************************************************************************
210 * This section contains accessors and other methods on the connection_array
211 * variables (which are global within this file and unavailable outside it).
213 ****************************************************************************/
215 /** Return 1 if we have successfully built a circuit, and nothing has changed
216 * to make us think that maybe we can't.
219 have_completed_a_circuit(void)
221 return can_complete_circuits;
224 /** Note that we have successfully built a circuit, so that reachability
225 * testing and introduction points and so on may be attempted. */
226 void
227 note_that_we_completed_a_circuit(void)
229 can_complete_circuits = 1;
232 /** Note that something has happened (like a clock jump, or DisableNetwork) to
233 * make us think that maybe we can't complete circuits. */
234 void
235 note_that_we_maybe_cant_complete_circuits(void)
237 can_complete_circuits = 0;
240 /** Add <b>conn</b> to the array of connections that we can poll on. The
241 * connection's socket must be set; the connection starts out
242 * non-reading and non-writing.
245 connection_add_impl(connection_t *conn, int is_connecting)
247 tor_assert(conn);
248 tor_assert(SOCKET_OK(conn->s) ||
249 conn->linked ||
250 (conn->type == CONN_TYPE_AP &&
251 TO_EDGE_CONN(conn)->is_dns_request));
253 tor_assert(conn->conn_array_index == -1); /* can only connection_add once */
254 conn->conn_array_index = smartlist_len(connection_array);
255 smartlist_add(connection_array, conn);
257 (void) is_connecting;
259 if (SOCKET_OK(conn->s) || conn->linked) {
260 conn->read_event = tor_event_new(tor_libevent_get_base(),
261 conn->s, EV_READ|EV_PERSIST, conn_read_callback, conn);
262 conn->write_event = tor_event_new(tor_libevent_get_base(),
263 conn->s, EV_WRITE|EV_PERSIST, conn_write_callback, conn);
264 /* XXXX CHECK FOR NULL RETURN! */
267 log_debug(LD_NET,"new conn type %s, socket %d, address %s, n_conns %d.",
268 conn_type_to_string(conn->type), (int)conn->s, conn->address,
269 smartlist_len(connection_array));
271 return 0;
274 /** Tell libevent that we don't care about <b>conn</b> any more. */
275 void
276 connection_unregister_events(connection_t *conn)
278 if (conn->read_event) {
279 if (event_del(conn->read_event))
280 log_warn(LD_BUG, "Error removing read event for %d", (int)conn->s);
281 tor_free(conn->read_event);
283 if (conn->write_event) {
284 if (event_del(conn->write_event))
285 log_warn(LD_BUG, "Error removing write event for %d", (int)conn->s);
286 tor_free(conn->write_event);
288 if (conn->type == CONN_TYPE_AP_DNS_LISTENER) {
289 dnsserv_close_listener(conn);
293 /** Remove the connection from the global list, and remove the
294 * corresponding poll entry. Calling this function will shift the last
295 * connection (if any) into the position occupied by conn.
298 connection_remove(connection_t *conn)
300 int current_index;
301 connection_t *tmp;
303 tor_assert(conn);
305 log_debug(LD_NET,"removing socket %d (type %s), n_conns now %d",
306 (int)conn->s, conn_type_to_string(conn->type),
307 smartlist_len(connection_array));
309 if (conn->type == CONN_TYPE_AP && conn->socket_family == AF_UNIX) {
310 log_info(LD_NET, "Closing SOCKS Unix socket connection");
313 control_event_conn_bandwidth(conn);
315 tor_assert(conn->conn_array_index >= 0);
316 current_index = conn->conn_array_index;
317 connection_unregister_events(conn); /* This is redundant, but cheap. */
318 if (current_index == smartlist_len(connection_array)-1) { /* at the end */
319 smartlist_del(connection_array, current_index);
320 return 0;
323 /* replace this one with the one at the end */
324 smartlist_del(connection_array, current_index);
325 tmp = smartlist_get(connection_array, current_index);
326 tmp->conn_array_index = current_index;
328 return 0;
331 /** If <b>conn</b> is an edge conn, remove it from the list
332 * of conn's on this circuit. If it's not on an edge,
333 * flush and send destroys for all circuits on this conn.
335 * Remove it from connection_array (if applicable) and
336 * from closeable_connection_list.
338 * Then free it.
340 static void
341 connection_unlink(connection_t *conn)
343 connection_about_to_close_connection(conn);
344 if (conn->conn_array_index >= 0) {
345 connection_remove(conn);
347 if (conn->linked_conn) {
348 conn->linked_conn->linked_conn = NULL;
349 if (! conn->linked_conn->marked_for_close &&
350 conn->linked_conn->reading_from_linked_conn)
351 connection_start_reading(conn->linked_conn);
352 conn->linked_conn = NULL;
354 smartlist_remove(closeable_connection_lst, conn);
355 smartlist_remove(active_linked_connection_lst, conn);
356 if (conn->type == CONN_TYPE_EXIT) {
357 assert_connection_edge_not_dns_pending(TO_EDGE_CONN(conn));
359 if (conn->type == CONN_TYPE_OR) {
360 if (!tor_digest_is_zero(TO_OR_CONN(conn)->identity_digest))
361 connection_or_clear_identity(TO_OR_CONN(conn));
362 /* connection_unlink() can only get called if the connection
363 * was already on the closeable list, and it got there by
364 * connection_mark_for_close(), which was called from
365 * connection_or_close_normally() or
366 * connection_or_close_for_error(), so the channel should
367 * already be in CHANNEL_STATE_CLOSING, and then the
368 * connection_about_to_close_connection() goes to
369 * connection_or_about_to_close(), which calls channel_closed()
370 * to notify the channel_t layer, and closed the channel, so
371 * nothing more to do here to deal with the channel associated
372 * with an orconn.
375 connection_free(conn);
379 * Callback: used to activate read events for all linked connections, so
380 * libevent knows to call their read callbacks. This callback run as a
381 * postloop event, so that the events _it_ activates don't happen until
382 * Libevent has a chance to check for other events.
384 static void
385 schedule_active_linked_connections_cb(mainloop_event_t *event, void *arg)
387 (void)event;
388 (void)arg;
390 /* All active linked conns should get their read events activated,
391 * so that libevent knows to run their callbacks. */
392 SMARTLIST_FOREACH(active_linked_connection_lst, connection_t *, conn,
393 event_active(conn->read_event, EV_READ, 1));
396 /** Event that invokes schedule_active_linked_connections_cb. */
397 static mainloop_event_t *schedule_active_linked_connections_event = NULL;
399 /** Initialize the global connection list, closeable connection list,
400 * and active connection list. */
401 void
402 tor_init_connection_lists(void)
404 if (!connection_array)
405 connection_array = smartlist_new();
406 if (!closeable_connection_lst)
407 closeable_connection_lst = smartlist_new();
408 if (!active_linked_connection_lst)
409 active_linked_connection_lst = smartlist_new();
412 /** Schedule <b>conn</b> to be closed. **/
413 void
414 add_connection_to_closeable_list(connection_t *conn)
416 tor_assert(!smartlist_contains(closeable_connection_lst, conn));
417 tor_assert(conn->marked_for_close);
418 assert_connection_ok(conn, time(NULL));
419 smartlist_add(closeable_connection_lst, conn);
420 mainloop_schedule_postloop_cleanup();
423 /** Return 1 if conn is on the closeable list, else return 0. */
425 connection_is_on_closeable_list(connection_t *conn)
427 return smartlist_contains(closeable_connection_lst, conn);
430 /** Return true iff conn is in the current poll array. */
432 connection_in_array(connection_t *conn)
434 return smartlist_contains(connection_array, conn);
437 /** Set <b>*array</b> to an array of all connections. <b>*array</b> must not
438 * be modified.
440 MOCK_IMPL(smartlist_t *,
441 get_connection_array, (void))
443 if (!connection_array)
444 connection_array = smartlist_new();
445 return connection_array;
449 * Return the amount of network traffic read, in bytes, over the life of this
450 * process.
452 MOCK_IMPL(uint64_t,
453 get_bytes_read,(void))
455 return stats_n_bytes_read;
459 * Return the amount of network traffic read, in bytes, over the life of this
460 * process.
462 MOCK_IMPL(uint64_t,
463 get_bytes_written,(void))
465 return stats_n_bytes_written;
469 * Increment the amount of network traffic read and written, over the life of
470 * this process.
472 void
473 stats_increment_bytes_read_and_written(uint64_t r, uint64_t w)
475 stats_n_bytes_read += r;
476 stats_n_bytes_written += w;
479 /** Set the event mask on <b>conn</b> to <b>events</b>. (The event
480 * mask is a bitmask whose bits are READ_EVENT and WRITE_EVENT)
482 void
483 connection_watch_events(connection_t *conn, watchable_events_t events)
485 if (events & READ_EVENT)
486 connection_start_reading(conn);
487 else
488 connection_stop_reading(conn);
490 if (events & WRITE_EVENT)
491 connection_start_writing(conn);
492 else
493 connection_stop_writing(conn);
496 /** Return true iff <b>conn</b> is listening for read events. */
498 connection_is_reading(connection_t *conn)
500 tor_assert(conn);
502 return conn->reading_from_linked_conn ||
503 (conn->read_event && event_pending(conn->read_event, EV_READ, NULL));
506 /** Reset our main loop counters. */
507 void
508 reset_main_loop_counters(void)
510 stats_n_main_loop_successes = 0;
511 stats_n_main_loop_errors = 0;
512 stats_n_main_loop_idle = 0;
515 /** Increment the main loop success counter. */
516 static void
517 increment_main_loop_success_count(void)
519 ++stats_n_main_loop_successes;
522 /** Get the main loop success counter. */
523 uint64_t
524 get_main_loop_success_count(void)
526 return stats_n_main_loop_successes;
529 /** Increment the main loop error counter. */
530 static void
531 increment_main_loop_error_count(void)
533 ++stats_n_main_loop_errors;
536 /** Get the main loop error counter. */
537 uint64_t
538 get_main_loop_error_count(void)
540 return stats_n_main_loop_errors;
543 /** Increment the main loop idle counter. */
544 static void
545 increment_main_loop_idle_count(void)
547 ++stats_n_main_loop_idle;
550 /** Get the main loop idle counter. */
551 uint64_t
552 get_main_loop_idle_count(void)
554 return stats_n_main_loop_idle;
557 /** Check whether <b>conn</b> is correct in having (or not having) a
558 * read/write event (passed in <b>ev</b>). On success, return 0. On failure,
559 * log a warning and return -1. */
560 static int
561 connection_check_event(connection_t *conn, struct event *ev)
563 int bad;
565 if (conn->type == CONN_TYPE_AP && TO_EDGE_CONN(conn)->is_dns_request) {
566 /* DNS requests which we launch through the dnsserv.c module do not have
567 * any underlying socket or any underlying linked connection, so they
568 * shouldn't have any attached events either.
570 bad = ev != NULL;
571 } else {
572 /* Everything else should have an underlying socket, or a linked
573 * connection (which is also tracked with a read_event/write_event pair).
575 bad = ev == NULL;
578 if (bad) {
579 log_warn(LD_BUG, "Event missing on connection %p [%s;%s]. "
580 "socket=%d. linked=%d. "
581 "is_dns_request=%d. Marked_for_close=%s:%d",
582 conn,
583 conn_type_to_string(conn->type),
584 conn_state_to_string(conn->type, conn->state),
585 (int)conn->s, (int)conn->linked,
586 (conn->type == CONN_TYPE_AP &&
587 TO_EDGE_CONN(conn)->is_dns_request),
588 conn->marked_for_close_file ? conn->marked_for_close_file : "-",
589 conn->marked_for_close
591 log_backtrace(LOG_WARN, LD_BUG, "Backtrace attached.");
592 return -1;
594 return 0;
597 /** Tell the main loop to stop notifying <b>conn</b> of any read events. */
598 MOCK_IMPL(void,
599 connection_stop_reading,(connection_t *conn))
601 tor_assert(conn);
603 if (connection_check_event(conn, conn->read_event) < 0) {
604 return;
607 if (conn->linked) {
608 conn->reading_from_linked_conn = 0;
609 connection_stop_reading_from_linked_conn(conn);
610 } else {
611 if (event_del(conn->read_event))
612 log_warn(LD_NET, "Error from libevent setting read event state for %d "
613 "to unwatched: %s",
614 (int)conn->s,
615 tor_socket_strerror(tor_socket_errno(conn->s)));
619 /** Tell the main loop to start notifying <b>conn</b> of any read events. */
620 MOCK_IMPL(void,
621 connection_start_reading,(connection_t *conn))
623 tor_assert(conn);
625 if (connection_check_event(conn, conn->read_event) < 0) {
626 return;
629 if (conn->linked) {
630 conn->reading_from_linked_conn = 1;
631 if (connection_should_read_from_linked_conn(conn))
632 connection_start_reading_from_linked_conn(conn);
633 } else {
634 if (event_add(conn->read_event, NULL))
635 log_warn(LD_NET, "Error from libevent setting read event state for %d "
636 "to watched: %s",
637 (int)conn->s,
638 tor_socket_strerror(tor_socket_errno(conn->s)));
642 /** Return true iff <b>conn</b> is listening for write events. */
644 connection_is_writing(connection_t *conn)
646 tor_assert(conn);
648 return conn->writing_to_linked_conn ||
649 (conn->write_event && event_pending(conn->write_event, EV_WRITE, NULL));
652 /** Tell the main loop to stop notifying <b>conn</b> of any write events. */
653 MOCK_IMPL(void,
654 connection_stop_writing,(connection_t *conn))
656 tor_assert(conn);
658 if (connection_check_event(conn, conn->write_event) < 0) {
659 return;
662 if (conn->linked) {
663 conn->writing_to_linked_conn = 0;
664 if (conn->linked_conn)
665 connection_stop_reading_from_linked_conn(conn->linked_conn);
666 } else {
667 if (event_del(conn->write_event))
668 log_warn(LD_NET, "Error from libevent setting write event state for %d "
669 "to unwatched: %s",
670 (int)conn->s,
671 tor_socket_strerror(tor_socket_errno(conn->s)));
675 /** Tell the main loop to start notifying <b>conn</b> of any write events. */
676 MOCK_IMPL(void,
677 connection_start_writing,(connection_t *conn))
679 tor_assert(conn);
681 if (connection_check_event(conn, conn->write_event) < 0) {
682 return;
685 if (conn->linked) {
686 conn->writing_to_linked_conn = 1;
687 if (conn->linked_conn &&
688 connection_should_read_from_linked_conn(conn->linked_conn))
689 connection_start_reading_from_linked_conn(conn->linked_conn);
690 } else {
691 if (event_add(conn->write_event, NULL))
692 log_warn(LD_NET, "Error from libevent setting write event state for %d "
693 "to watched: %s",
694 (int)conn->s,
695 tor_socket_strerror(tor_socket_errno(conn->s)));
699 /** Return true iff <b>conn</b> is linked conn, and reading from the conn
700 * linked to it would be good and feasible. (Reading is "feasible" if the
701 * other conn exists and has data in its outbuf, and is "good" if we have our
702 * reading_from_linked_conn flag set and the other conn has its
703 * writing_to_linked_conn flag set.)*/
704 static int
705 connection_should_read_from_linked_conn(connection_t *conn)
707 if (conn->linked && conn->reading_from_linked_conn) {
708 if (! conn->linked_conn ||
709 (conn->linked_conn->writing_to_linked_conn &&
710 buf_datalen(conn->linked_conn->outbuf)))
711 return 1;
713 return 0;
716 /** Event to run 'shutdown did not work callback'. */
717 static struct event *shutdown_did_not_work_event = NULL;
719 /** Failsafe measure that should never actually be necessary: If
720 * tor_shutdown_event_loop_and_exit() somehow doesn't successfully exit the
721 * event loop, then this callback will kill Tor with an assertion failure
722 * seconds later
724 static void
725 shutdown_did_not_work_callback(evutil_socket_t fd, short event, void *arg)
727 // LCOV_EXCL_START
728 (void) fd;
729 (void) event;
730 (void) arg;
731 tor_assert_unreached();
732 // LCOV_EXCL_STOP
735 #ifdef ENABLE_RESTART_DEBUGGING
736 static struct event *tor_shutdown_event_loop_for_restart_event = NULL;
737 static void
738 tor_shutdown_event_loop_for_restart_cb(
739 evutil_socket_t fd, short event, void *arg)
741 (void)fd;
742 (void)event;
743 (void)arg;
744 tor_event_free(tor_shutdown_event_loop_for_restart_event);
745 tor_shutdown_event_loop_and_exit(0);
747 #endif
750 * After finishing the current callback (if any), shut down the main loop,
751 * clean up the process, and exit with <b>exitcode</b>.
753 void
754 tor_shutdown_event_loop_and_exit(int exitcode)
756 if (main_loop_should_exit)
757 return; /* Ignore multiple calls to this function. */
759 main_loop_should_exit = 1;
760 main_loop_exit_value = exitcode;
762 /* Die with an assertion failure in ten seconds, if for some reason we don't
763 * exit normally. */
764 /* XXXX We should consider this code if it's never used. */
765 struct timeval ten_seconds = { 10, 0 };
766 shutdown_did_not_work_event = tor_evtimer_new(
767 tor_libevent_get_base(),
768 shutdown_did_not_work_callback, NULL);
769 event_add(shutdown_did_not_work_event, &ten_seconds);
771 /* Unlike exit_loop_after_delay(), exit_loop_after_callback
772 * prevents other callbacks from running. */
773 tor_libevent_exit_loop_after_callback(tor_libevent_get_base());
776 /** Return true iff tor_shutdown_event_loop_and_exit() has been called. */
778 tor_event_loop_shutdown_is_pending(void)
780 return main_loop_should_exit;
783 /** Helper: Tell the main loop to begin reading bytes into <b>conn</b> from
784 * its linked connection, if it is not doing so already. Called by
785 * connection_start_reading and connection_start_writing as appropriate. */
786 static void
787 connection_start_reading_from_linked_conn(connection_t *conn)
789 tor_assert(conn);
790 tor_assert(conn->linked == 1);
792 if (!conn->active_on_link) {
793 conn->active_on_link = 1;
794 smartlist_add(active_linked_connection_lst, conn);
795 mainloop_event_activate(schedule_active_linked_connections_event);
796 } else {
797 tor_assert(smartlist_contains(active_linked_connection_lst, conn));
801 /** Tell the main loop to stop reading bytes into <b>conn</b> from its linked
802 * connection, if is currently doing so. Called by connection_stop_reading,
803 * connection_stop_writing, and connection_read. */
804 void
805 connection_stop_reading_from_linked_conn(connection_t *conn)
807 tor_assert(conn);
808 tor_assert(conn->linked == 1);
810 if (conn->active_on_link) {
811 conn->active_on_link = 0;
812 /* FFFF We could keep an index here so we can smartlist_del
813 * cleanly. On the other hand, this doesn't show up on profiles,
814 * so let's leave it alone for now. */
815 smartlist_remove(active_linked_connection_lst, conn);
816 } else {
817 tor_assert(!smartlist_contains(active_linked_connection_lst, conn));
821 /** Close all connections that have been scheduled to get closed. */
822 STATIC void
823 close_closeable_connections(void)
825 int i;
826 for (i = 0; i < smartlist_len(closeable_connection_lst); ) {
827 connection_t *conn = smartlist_get(closeable_connection_lst, i);
828 if (conn->conn_array_index < 0) {
829 connection_unlink(conn); /* blow it away right now */
830 } else {
831 if (!conn_close_if_marked(conn->conn_array_index))
832 ++i;
837 /** Count moribund connections for the OOS handler */
838 MOCK_IMPL(int,
839 connection_count_moribund, (void))
841 int moribund = 0;
844 * Count things we'll try to kill when close_closeable_connections()
845 * runs next.
847 SMARTLIST_FOREACH_BEGIN(closeable_connection_lst, connection_t *, conn) {
848 if (SOCKET_OK(conn->s) && connection_is_moribund(conn)) ++moribund;
849 } SMARTLIST_FOREACH_END(conn);
851 return moribund;
854 /** Libevent callback: this gets invoked when (connection_t*)<b>conn</b> has
855 * some data to read. */
856 static void
857 conn_read_callback(evutil_socket_t fd, short event, void *_conn)
859 connection_t *conn = _conn;
860 (void)fd;
861 (void)event;
863 log_debug(LD_NET,"socket %d wants to read.",(int)conn->s);
865 /* assert_connection_ok(conn, time(NULL)); */
867 if (connection_handle_read(conn) < 0) {
868 if (!conn->marked_for_close) {
869 #ifndef _WIN32
870 log_warn(LD_BUG,"Unhandled error on read for %s connection "
871 "(fd %d); removing",
872 conn_type_to_string(conn->type), (int)conn->s);
873 tor_fragile_assert();
874 #endif /* !defined(_WIN32) */
875 if (CONN_IS_EDGE(conn))
876 connection_edge_end_errno(TO_EDGE_CONN(conn));
877 connection_mark_for_close(conn);
880 assert_connection_ok(conn, time(NULL));
882 if (smartlist_len(closeable_connection_lst))
883 close_closeable_connections();
886 /** Libevent callback: this gets invoked when (connection_t*)<b>conn</b> has
887 * some data to write. */
888 static void
889 conn_write_callback(evutil_socket_t fd, short events, void *_conn)
891 connection_t *conn = _conn;
892 (void)fd;
893 (void)events;
895 LOG_FN_CONN(conn, (LOG_DEBUG, LD_NET, "socket %d wants to write.",
896 (int)conn->s));
898 /* assert_connection_ok(conn, time(NULL)); */
900 if (connection_handle_write(conn, 0) < 0) {
901 if (!conn->marked_for_close) {
902 /* this connection is broken. remove it. */
903 log_fn(LOG_WARN,LD_BUG,
904 "unhandled error on write for %s connection (fd %d); removing",
905 conn_type_to_string(conn->type), (int)conn->s);
906 tor_fragile_assert();
907 if (CONN_IS_EDGE(conn)) {
908 /* otherwise we cry wolf about duplicate close */
909 edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
910 if (!edge_conn->end_reason)
911 edge_conn->end_reason = END_STREAM_REASON_INTERNAL;
912 edge_conn->edge_has_sent_end = 1;
914 connection_close_immediate(conn); /* So we don't try to flush. */
915 connection_mark_for_close(conn);
918 assert_connection_ok(conn, time(NULL));
920 if (smartlist_len(closeable_connection_lst))
921 close_closeable_connections();
924 /** If the connection at connection_array[i] is marked for close, then:
925 * - If it has data that it wants to flush, try to flush it.
926 * - If it _still_ has data to flush, and conn->hold_open_until_flushed is
927 * true, then leave the connection open and return.
928 * - Otherwise, remove the connection from connection_array and from
929 * all other lists, close it, and free it.
930 * Returns 1 if the connection was closed, 0 otherwise.
932 static int
933 conn_close_if_marked(int i)
935 connection_t *conn;
936 int retval;
937 time_t now;
939 conn = smartlist_get(connection_array, i);
940 if (!conn->marked_for_close)
941 return 0; /* nothing to see here, move along */
942 now = time(NULL);
943 assert_connection_ok(conn, now);
944 /* assert_all_pending_dns_resolves_ok(); */
946 log_debug(LD_NET,"Cleaning up connection (fd "TOR_SOCKET_T_FORMAT").",
947 conn->s);
949 /* If the connection we are about to close was trying to connect to
950 a proxy server and failed, the client won't be able to use that
951 proxy. We should warn the user about this. */
952 if (conn->proxy_state == PROXY_INFANT)
953 log_failed_proxy_connection(conn);
955 if ((SOCKET_OK(conn->s) || conn->linked_conn) &&
956 connection_wants_to_flush(conn)) {
957 /* s == -1 means it's an incomplete edge connection, or that the socket
958 * has already been closed as unflushable. */
959 ssize_t sz = connection_bucket_write_limit(conn, now);
960 if (!conn->hold_open_until_flushed)
961 log_info(LD_NET,
962 "Conn (addr %s, fd %d, type %s, state %d) marked, but wants "
963 "to flush %d bytes. (Marked at %s:%d)",
964 escaped_safe_str_client(conn->address),
965 (int)conn->s, conn_type_to_string(conn->type), conn->state,
966 (int)conn->outbuf_flushlen,
967 conn->marked_for_close_file, conn->marked_for_close);
968 if (conn->linked_conn) {
969 retval = buf_move_to_buf(conn->linked_conn->inbuf, conn->outbuf,
970 &conn->outbuf_flushlen);
971 if (retval >= 0) {
972 /* The linked conn will notice that it has data when it notices that
973 * we're gone. */
974 connection_start_reading_from_linked_conn(conn->linked_conn);
976 log_debug(LD_GENERAL, "Flushed last %d bytes from a linked conn; "
977 "%d left; flushlen %d; wants-to-flush==%d", retval,
978 (int)connection_get_outbuf_len(conn),
979 (int)conn->outbuf_flushlen,
980 connection_wants_to_flush(conn));
981 } else if (connection_speaks_cells(conn)) {
982 if (conn->state == OR_CONN_STATE_OPEN) {
983 retval = buf_flush_to_tls(conn->outbuf, TO_OR_CONN(conn)->tls, sz,
984 &conn->outbuf_flushlen);
985 } else
986 retval = -1; /* never flush non-open broken tls connections */
987 } else {
988 retval = buf_flush_to_socket(conn->outbuf, conn->s, sz,
989 &conn->outbuf_flushlen);
991 if (retval >= 0 && /* Technically, we could survive things like
992 TLS_WANT_WRITE here. But don't bother for now. */
993 conn->hold_open_until_flushed && connection_wants_to_flush(conn)) {
994 if (retval > 0) {
995 LOG_FN_CONN(conn, (LOG_INFO,LD_NET,
996 "Holding conn (fd %d) open for more flushing.",
997 (int)conn->s));
998 conn->timestamp_last_write_allowed = now; /* reset so we can flush
999 * more */
1000 } else if (sz == 0) {
1001 /* Also, retval==0. If we get here, we didn't want to write anything
1002 * (because of rate-limiting) and we didn't. */
1004 /* Connection must flush before closing, but it's being rate-limited.
1005 * Let's remove from Libevent, and mark it as blocked on bandwidth
1006 * so it will be re-added on next token bucket refill. Prevents
1007 * busy Libevent loops where we keep ending up here and returning
1008 * 0 until we are no longer blocked on bandwidth.
1010 connection_consider_empty_read_buckets(conn);
1011 connection_consider_empty_write_buckets(conn);
1013 /* Make sure that consider_empty_buckets really disabled the
1014 * connection: */
1015 if (BUG(connection_is_writing(conn))) {
1016 connection_write_bw_exhausted(conn, true);
1018 if (BUG(connection_is_reading(conn))) {
1019 /* XXXX+ We should make this code unreachable; if a connection is
1020 * marked for close and flushing, there is no point in reading to it
1021 * at all. Further, checking at this point is a bit of a hack: it
1022 * would make much more sense to react in
1023 * connection_handle_read_impl, or to just stop reading in
1024 * mark_and_flush */
1025 connection_read_bw_exhausted(conn, true/* kludge. */);
1028 return 0;
1030 if (connection_wants_to_flush(conn)) {
1031 log_fn(LOG_INFO, LD_NET, "We stalled too much while trying to write %d "
1032 "bytes to address %s. If this happens a lot, either "
1033 "something is wrong with your network connection, or "
1034 "something is wrong with theirs. "
1035 "(fd %d, type %s, state %d, marked at %s:%d).",
1036 (int)connection_get_outbuf_len(conn),
1037 escaped_safe_str_client(conn->address),
1038 (int)conn->s, conn_type_to_string(conn->type), conn->state,
1039 conn->marked_for_close_file,
1040 conn->marked_for_close);
1044 connection_unlink(conn); /* unlink, remove, free */
1045 return 1;
1048 /** Implementation for directory_all_unreachable. This is done in a callback,
1049 * since otherwise it would complicate Tor's control-flow graph beyond all
1050 * reason.
1052 static void
1053 directory_all_unreachable_cb(mainloop_event_t *event, void *arg)
1055 (void)event;
1056 (void)arg;
1058 connection_t *conn;
1060 while ((conn = connection_get_by_type_state(CONN_TYPE_AP,
1061 AP_CONN_STATE_CIRCUIT_WAIT))) {
1062 entry_connection_t *entry_conn = TO_ENTRY_CONN(conn);
1063 log_notice(LD_NET,
1064 "Is your network connection down? "
1065 "Failing connection to '%s:%d'.",
1066 safe_str_client(entry_conn->socks_request->address),
1067 entry_conn->socks_request->port);
1068 connection_mark_unattached_ap(entry_conn,
1069 END_STREAM_REASON_NET_UNREACHABLE);
1071 control_event_general_error("DIR_ALL_UNREACHABLE");
1074 static mainloop_event_t *directory_all_unreachable_cb_event = NULL;
1076 /** We've just tried every dirserver we know about, and none of
1077 * them were reachable. Assume the network is down. Change state
1078 * so next time an application connection arrives we'll delay it
1079 * and try another directory fetch. Kill off all the circuit_wait
1080 * streams that are waiting now, since they will all timeout anyway.
1082 void
1083 directory_all_unreachable(time_t now)
1085 (void)now;
1087 reset_uptime(); /* reset it */
1089 if (!directory_all_unreachable_cb_event) {
1090 directory_all_unreachable_cb_event =
1091 mainloop_event_new(directory_all_unreachable_cb, NULL);
1092 tor_assert(directory_all_unreachable_cb_event);
1095 mainloop_event_activate(directory_all_unreachable_cb_event);
1098 /** This function is called whenever we successfully pull down some new
1099 * network statuses or server descriptors. */
1100 void
1101 directory_info_has_arrived(time_t now, int from_cache, int suppress_logs)
1103 const or_options_t *options = get_options();
1105 /* if we have enough dir info, then update our guard status with
1106 * whatever we just learned. */
1107 int invalidate_circs = guards_update_all();
1109 if (invalidate_circs) {
1110 circuit_mark_all_unused_circs();
1111 circuit_mark_all_dirty_circs_as_unusable();
1114 if (!router_have_minimum_dir_info()) {
1115 int quiet = suppress_logs || from_cache ||
1116 directory_too_idle_to_fetch_descriptors(options, now);
1117 tor_log(quiet ? LOG_INFO : LOG_NOTICE, LD_DIR,
1118 "I learned some more directory information, but not enough to "
1119 "build a circuit: %s", get_dir_info_status_string());
1120 update_all_descriptor_downloads(now);
1121 return;
1122 } else {
1123 if (directory_fetches_from_authorities(options)) {
1124 update_all_descriptor_downloads(now);
1127 /* Don't even bother trying to get extrainfo until the rest of our
1128 * directory info is up-to-date */
1129 if (options->DownloadExtraInfo)
1130 update_extrainfo_downloads(now);
1133 if (server_mode(options) && !net_is_disabled() && !from_cache &&
1134 (have_completed_a_circuit() || !any_predicted_circuits(now)))
1135 router_do_reachability_checks(1, 1);
1138 /** Perform regular maintenance tasks for a single connection. This
1139 * function gets run once per second per connection by run_scheduled_events.
1141 static void
1142 run_connection_housekeeping(int i, time_t now)
1144 cell_t cell;
1145 connection_t *conn = smartlist_get(connection_array, i);
1146 const or_options_t *options = get_options();
1147 or_connection_t *or_conn;
1148 channel_t *chan = NULL;
1149 int have_any_circuits;
1150 int past_keepalive =
1151 now >= conn->timestamp_last_write_allowed + options->KeepalivePeriod;
1153 if (conn->outbuf && !connection_get_outbuf_len(conn) &&
1154 conn->type == CONN_TYPE_OR)
1155 TO_OR_CONN(conn)->timestamp_lastempty = now;
1157 if (conn->marked_for_close) {
1158 /* nothing to do here */
1159 return;
1162 /* Expire any directory connections that haven't been active (sent
1163 * if a server or received if a client) for 5 min */
1164 if (conn->type == CONN_TYPE_DIR &&
1165 ((DIR_CONN_IS_SERVER(conn) &&
1166 conn->timestamp_last_write_allowed
1167 + options->TestingDirConnectionMaxStall < now) ||
1168 (!DIR_CONN_IS_SERVER(conn) &&
1169 conn->timestamp_last_read_allowed
1170 + options->TestingDirConnectionMaxStall < now))) {
1171 log_info(LD_DIR,"Expiring wedged directory conn (fd %d, purpose %d)",
1172 (int)conn->s, conn->purpose);
1173 /* This check is temporary; it's to let us know whether we should consider
1174 * parsing partial serverdesc responses. */
1175 if (conn->purpose == DIR_PURPOSE_FETCH_SERVERDESC &&
1176 connection_get_inbuf_len(conn) >= 1024) {
1177 log_info(LD_DIR,"Trying to extract information from wedged server desc "
1178 "download.");
1179 connection_dir_reached_eof(TO_DIR_CONN(conn));
1180 } else {
1181 connection_mark_for_close(conn);
1183 return;
1186 if (!connection_speaks_cells(conn))
1187 return; /* we're all done here, the rest is just for OR conns */
1189 /* If we haven't flushed to an OR connection for a while, then either nuke
1190 the connection or send a keepalive, depending. */
1192 or_conn = TO_OR_CONN(conn);
1193 tor_assert(conn->outbuf);
1195 chan = TLS_CHAN_TO_BASE(or_conn->chan);
1196 tor_assert(chan);
1198 if (channel_num_circuits(chan) != 0) {
1199 have_any_circuits = 1;
1200 chan->timestamp_last_had_circuits = now;
1201 } else {
1202 have_any_circuits = 0;
1205 if (channel_is_bad_for_new_circs(TLS_CHAN_TO_BASE(or_conn->chan)) &&
1206 ! have_any_circuits) {
1207 /* It's bad for new circuits, and has no unmarked circuits on it:
1208 * mark it now. */
1209 log_info(LD_OR,
1210 "Expiring non-used OR connection to fd %d (%s:%d) [Too old].",
1211 (int)conn->s, conn->address, conn->port);
1212 if (conn->state == OR_CONN_STATE_CONNECTING)
1213 connection_or_connect_failed(TO_OR_CONN(conn),
1214 END_OR_CONN_REASON_TIMEOUT,
1215 "Tor gave up on the connection");
1216 connection_or_close_normally(TO_OR_CONN(conn), 1);
1217 } else if (!connection_state_is_open(conn)) {
1218 if (past_keepalive) {
1219 /* We never managed to actually get this connection open and happy. */
1220 log_info(LD_OR,"Expiring non-open OR connection to fd %d (%s:%d).",
1221 (int)conn->s,conn->address, conn->port);
1222 connection_or_close_normally(TO_OR_CONN(conn), 0);
1224 } else if (we_are_hibernating() &&
1225 ! have_any_circuits &&
1226 !connection_get_outbuf_len(conn)) {
1227 /* We're hibernating or shutting down, there's no circuits, and nothing to
1228 * flush.*/
1229 log_info(LD_OR,"Expiring non-used OR connection to fd %d (%s:%d) "
1230 "[Hibernating or exiting].",
1231 (int)conn->s,conn->address, conn->port);
1232 connection_or_close_normally(TO_OR_CONN(conn), 1);
1233 } else if (!have_any_circuits &&
1234 now - or_conn->idle_timeout >=
1235 chan->timestamp_last_had_circuits) {
1236 log_info(LD_OR,"Expiring non-used OR connection %"PRIu64" to fd %d "
1237 "(%s:%d) [no circuits for %d; timeout %d; %scanonical].",
1238 (chan->global_identifier),
1239 (int)conn->s, conn->address, conn->port,
1240 (int)(now - chan->timestamp_last_had_circuits),
1241 or_conn->idle_timeout,
1242 or_conn->is_canonical ? "" : "non");
1243 connection_or_close_normally(TO_OR_CONN(conn), 0);
1244 } else if (
1245 now >= or_conn->timestamp_lastempty + options->KeepalivePeriod*10 &&
1246 now >=
1247 conn->timestamp_last_write_allowed + options->KeepalivePeriod*10) {
1248 log_fn(LOG_PROTOCOL_WARN,LD_PROTOCOL,
1249 "Expiring stuck OR connection to fd %d (%s:%d). (%d bytes to "
1250 "flush; %d seconds since last write)",
1251 (int)conn->s, conn->address, conn->port,
1252 (int)connection_get_outbuf_len(conn),
1253 (int)(now-conn->timestamp_last_write_allowed));
1254 connection_or_close_normally(TO_OR_CONN(conn), 0);
1255 } else if (past_keepalive && !connection_get_outbuf_len(conn)) {
1256 /* send a padding cell */
1257 log_fn(LOG_DEBUG,LD_OR,"Sending keepalive to (%s:%d)",
1258 conn->address, conn->port);
1259 memset(&cell,0,sizeof(cell_t));
1260 cell.command = CELL_PADDING;
1261 connection_or_write_cell_to_buf(&cell, or_conn);
1262 } else {
1263 channelpadding_decide_to_pad_channel(chan);
1267 /** Honor a NEWNYM request: make future requests unlinkable to past
1268 * requests. */
1269 static void
1270 signewnym_impl(time_t now)
1272 const or_options_t *options = get_options();
1273 if (!proxy_mode(options)) {
1274 log_info(LD_CONTROL, "Ignoring SIGNAL NEWNYM because client functionality "
1275 "is disabled.");
1276 return;
1279 circuit_mark_all_dirty_circs_as_unusable();
1280 addressmap_clear_transient();
1281 hs_client_purge_state();
1282 time_of_last_signewnym = now;
1283 signewnym_is_pending = 0;
1285 ++newnym_epoch;
1287 control_event_signal(SIGNEWNYM);
1290 /** Callback: run a deferred signewnym. */
1291 static void
1292 handle_deferred_signewnym_cb(mainloop_event_t *event, void *arg)
1294 (void)event;
1295 (void)arg;
1296 log_info(LD_CONTROL, "Honoring delayed NEWNYM request");
1297 do_signewnym(time(NULL));
1300 /** Either perform a signewnym or schedule one, depending on rate limiting. */
1301 void
1302 do_signewnym(time_t now)
1304 if (time_of_last_signewnym + MAX_SIGNEWNYM_RATE > now) {
1305 const time_t delay_sec =
1306 time_of_last_signewnym + MAX_SIGNEWNYM_RATE - now;
1307 if (! signewnym_is_pending) {
1308 signewnym_is_pending = 1;
1309 if (!handle_deferred_signewnym_ev) {
1310 handle_deferred_signewnym_ev =
1311 mainloop_event_postloop_new(handle_deferred_signewnym_cb, NULL);
1313 const struct timeval delay_tv = { delay_sec, 0 };
1314 mainloop_event_schedule(handle_deferred_signewnym_ev, &delay_tv);
1316 log_notice(LD_CONTROL,
1317 "Rate limiting NEWNYM request: delaying by %d second(s)",
1318 (int)(delay_sec));
1319 } else {
1320 signewnym_impl(now);
1324 /** Return the number of times that signewnym has been called. */
1325 unsigned
1326 get_signewnym_epoch(void)
1328 return newnym_epoch;
1331 /** True iff we have initialized all the members of <b>periodic_events</b>.
1332 * Used to prevent double-initialization. */
1333 static int periodic_events_initialized = 0;
1335 /* Declare all the timer callback functions... */
1336 #undef CALLBACK
1337 #define CALLBACK(name) \
1338 static int name ## _callback(time_t, const or_options_t *)
1339 CALLBACK(add_entropy);
1340 CALLBACK(check_authority_cert);
1341 CALLBACK(check_canonical_channels);
1342 CALLBACK(check_descriptor);
1343 CALLBACK(check_dns_honesty);
1344 CALLBACK(check_ed_keys);
1345 CALLBACK(check_expired_networkstatus);
1346 CALLBACK(check_for_reachability_bw);
1347 CALLBACK(check_onion_keys_expiry_time);
1348 CALLBACK(clean_caches);
1349 CALLBACK(clean_consdiffmgr);
1350 CALLBACK(dirvote);
1351 CALLBACK(downrate_stability);
1352 CALLBACK(expire_old_ciruits_serverside);
1353 CALLBACK(fetch_networkstatus);
1354 CALLBACK(heartbeat);
1355 CALLBACK(hs_service);
1356 CALLBACK(launch_descriptor_fetches);
1357 CALLBACK(launch_reachability_tests);
1358 CALLBACK(reachability_warnings);
1359 CALLBACK(record_bridge_stats);
1360 CALLBACK(rend_cache_failure_clean);
1361 CALLBACK(reset_padding_counts);
1362 CALLBACK(retry_dns);
1363 CALLBACK(retry_listeners);
1364 CALLBACK(rotate_onion_key);
1365 CALLBACK(rotate_x509_certificate);
1366 CALLBACK(save_stability);
1367 CALLBACK(save_state);
1368 CALLBACK(write_bridge_ns);
1369 CALLBACK(write_stats_file);
1371 #undef CALLBACK
1373 /* Now we declare an array of periodic_event_item_t for each periodic event */
1374 #define CALLBACK(name, r, f) PERIODIC_EVENT(name, r, f)
1376 STATIC periodic_event_item_t periodic_events[] = {
1377 /* Everyone needs to run those. */
1378 CALLBACK(add_entropy, PERIODIC_EVENT_ROLE_ALL, 0),
1379 CALLBACK(check_expired_networkstatus, PERIODIC_EVENT_ROLE_ALL, 0),
1380 CALLBACK(clean_caches, PERIODIC_EVENT_ROLE_ALL, 0),
1381 CALLBACK(fetch_networkstatus, PERIODIC_EVENT_ROLE_ALL,
1382 PERIODIC_EVENT_FLAG_NEED_NET),
1383 CALLBACK(heartbeat, PERIODIC_EVENT_ROLE_ALL, 0),
1384 CALLBACK(launch_descriptor_fetches, PERIODIC_EVENT_ROLE_ALL,
1385 PERIODIC_EVENT_FLAG_NEED_NET),
1386 CALLBACK(reset_padding_counts, PERIODIC_EVENT_ROLE_ALL, 0),
1387 CALLBACK(retry_listeners, PERIODIC_EVENT_ROLE_ALL,
1388 PERIODIC_EVENT_FLAG_NEED_NET),
1389 CALLBACK(save_state, PERIODIC_EVENT_ROLE_ALL, 0),
1390 CALLBACK(rotate_x509_certificate, PERIODIC_EVENT_ROLE_ALL, 0),
1391 CALLBACK(write_stats_file, PERIODIC_EVENT_ROLE_ALL, 0),
1393 /* Routers (bridge and relay) only. */
1394 CALLBACK(check_descriptor, PERIODIC_EVENT_ROLE_ROUTER,
1395 PERIODIC_EVENT_FLAG_NEED_NET),
1396 CALLBACK(check_ed_keys, PERIODIC_EVENT_ROLE_ROUTER, 0),
1397 CALLBACK(check_for_reachability_bw, PERIODIC_EVENT_ROLE_ROUTER,
1398 PERIODIC_EVENT_FLAG_NEED_NET),
1399 CALLBACK(check_onion_keys_expiry_time, PERIODIC_EVENT_ROLE_ROUTER, 0),
1400 CALLBACK(expire_old_ciruits_serverside, PERIODIC_EVENT_ROLE_ROUTER,
1401 PERIODIC_EVENT_FLAG_NEED_NET),
1402 CALLBACK(reachability_warnings, PERIODIC_EVENT_ROLE_ROUTER,
1403 PERIODIC_EVENT_FLAG_NEED_NET),
1404 CALLBACK(retry_dns, PERIODIC_EVENT_ROLE_ROUTER, 0),
1405 CALLBACK(rotate_onion_key, PERIODIC_EVENT_ROLE_ROUTER, 0),
1407 /* Authorities (bridge and directory) only. */
1408 CALLBACK(downrate_stability, PERIODIC_EVENT_ROLE_AUTHORITIES, 0),
1409 CALLBACK(launch_reachability_tests, PERIODIC_EVENT_ROLE_AUTHORITIES,
1410 PERIODIC_EVENT_FLAG_NEED_NET),
1411 CALLBACK(save_stability, PERIODIC_EVENT_ROLE_AUTHORITIES, 0),
1413 /* Directory authority only. */
1414 CALLBACK(check_authority_cert, PERIODIC_EVENT_ROLE_DIRAUTH, 0),
1415 CALLBACK(dirvote, PERIODIC_EVENT_ROLE_DIRAUTH, PERIODIC_EVENT_FLAG_NEED_NET),
1417 /* Relay only. */
1418 CALLBACK(check_canonical_channels, PERIODIC_EVENT_ROLE_RELAY,
1419 PERIODIC_EVENT_FLAG_NEED_NET),
1420 CALLBACK(check_dns_honesty, PERIODIC_EVENT_ROLE_RELAY,
1421 PERIODIC_EVENT_FLAG_NEED_NET),
1423 /* Hidden Service service only. */
1424 CALLBACK(hs_service, PERIODIC_EVENT_ROLE_HS_SERVICE,
1425 PERIODIC_EVENT_FLAG_NEED_NET),
1427 /* Bridge only. */
1428 CALLBACK(record_bridge_stats, PERIODIC_EVENT_ROLE_BRIDGE, 0),
1430 /* Client only. */
1431 CALLBACK(rend_cache_failure_clean, PERIODIC_EVENT_ROLE_CLIENT, 0),
1433 /* Bridge Authority only. */
1434 CALLBACK(write_bridge_ns, PERIODIC_EVENT_ROLE_BRIDGEAUTH, 0),
1436 /* Directory server only. */
1437 CALLBACK(clean_consdiffmgr, PERIODIC_EVENT_ROLE_DIRSERVER, 0),
1439 END_OF_PERIODIC_EVENTS
1441 #undef CALLBACK
1443 /* These are pointers to members of periodic_events[] that are used to
1444 * implement particular callbacks. We keep them separate here so that we
1445 * can access them by name. We also keep them inside periodic_events[]
1446 * so that we can implement "reset all timers" in a reasonable way. */
1447 static periodic_event_item_t *check_descriptor_event=NULL;
1448 static periodic_event_item_t *dirvote_event=NULL;
1449 static periodic_event_item_t *fetch_networkstatus_event=NULL;
1450 static periodic_event_item_t *launch_descriptor_fetches_event=NULL;
1451 static periodic_event_item_t *check_dns_honesty_event=NULL;
1452 static periodic_event_item_t *save_state_event=NULL;
1454 /** Reset all the periodic events so we'll do all our actions again as if we
1455 * just started up.
1456 * Useful if our clock just moved back a long time from the future,
1457 * so we don't wait until that future arrives again before acting.
1459 void
1460 reset_all_main_loop_timers(void)
1462 int i;
1463 for (i = 0; periodic_events[i].name; ++i) {
1464 periodic_event_reschedule(&periodic_events[i]);
1468 /** Return the member of periodic_events[] whose name is <b>name</b>.
1469 * Return NULL if no such event is found.
1471 static periodic_event_item_t *
1472 find_periodic_event(const char *name)
1474 int i;
1475 for (i = 0; periodic_events[i].name; ++i) {
1476 if (strcmp(name, periodic_events[i].name) == 0)
1477 return &periodic_events[i];
1479 return NULL;
1482 /** Return a bitmask of the roles this tor instance is configured for using
1483 * the given options. */
1484 STATIC int
1485 get_my_roles(const or_options_t *options)
1487 tor_assert(options);
1489 int roles = 0;
1490 int is_bridge = options->BridgeRelay;
1491 int is_client = options_any_client_port_set(options);
1492 int is_relay = server_mode(options);
1493 int is_dirauth = authdir_mode_v3(options);
1494 int is_bridgeauth = authdir_mode_bridge(options);
1495 int is_hidden_service = !!hs_service_get_num_services() ||
1496 !!rend_num_services();
1497 int is_dirserver = dir_server_mode(options);
1499 if (is_bridge) roles |= PERIODIC_EVENT_ROLE_BRIDGE;
1500 if (is_client) roles |= PERIODIC_EVENT_ROLE_CLIENT;
1501 if (is_relay) roles |= PERIODIC_EVENT_ROLE_RELAY;
1502 if (is_dirauth) roles |= PERIODIC_EVENT_ROLE_DIRAUTH;
1503 if (is_bridgeauth) roles |= PERIODIC_EVENT_ROLE_BRIDGEAUTH;
1504 if (is_hidden_service) roles |= PERIODIC_EVENT_ROLE_HS_SERVICE;
1505 if (is_dirserver) roles |= PERIODIC_EVENT_ROLE_DIRSERVER;
1507 return roles;
1510 /** Event to run initialize_periodic_events_cb */
1511 static struct event *initialize_periodic_events_event = NULL;
1513 /** Helper, run one second after setup:
1514 * Initializes all members of periodic_events and starts them running.
1516 * (We do this one second after setup for backward-compatibility reasons;
1517 * it might not actually be necessary.) */
1518 static void
1519 initialize_periodic_events_cb(evutil_socket_t fd, short events, void *data)
1521 (void) fd;
1522 (void) events;
1523 (void) data;
1525 tor_event_free(initialize_periodic_events_event);
1527 rescan_periodic_events(get_options());
1530 /** Set up all the members of periodic_events[], and configure them all to be
1531 * launched from a callback. */
1532 STATIC void
1533 initialize_periodic_events(void)
1535 tor_assert(periodic_events_initialized == 0);
1536 periodic_events_initialized = 1;
1538 /* Set up all periodic events. We'll launch them by roles. */
1539 int i;
1540 for (i = 0; periodic_events[i].name; ++i) {
1541 periodic_event_setup(&periodic_events[i]);
1544 #define NAMED_CALLBACK(name) \
1545 STMT_BEGIN name ## _event = find_periodic_event( #name ); STMT_END
1547 NAMED_CALLBACK(check_descriptor);
1548 NAMED_CALLBACK(dirvote);
1549 NAMED_CALLBACK(fetch_networkstatus);
1550 NAMED_CALLBACK(launch_descriptor_fetches);
1551 NAMED_CALLBACK(check_dns_honesty);
1552 NAMED_CALLBACK(save_state);
1554 struct timeval one_second = { 1, 0 };
1555 initialize_periodic_events_event = tor_evtimer_new(
1556 tor_libevent_get_base(),
1557 initialize_periodic_events_cb, NULL);
1558 event_add(initialize_periodic_events_event, &one_second);
1561 STATIC void
1562 teardown_periodic_events(void)
1564 int i;
1565 for (i = 0; periodic_events[i].name; ++i) {
1566 periodic_event_destroy(&periodic_events[i]);
1568 periodic_events_initialized = 0;
1571 /** Do a pass at all our periodic events, disable those we don't need anymore
1572 * and enable those we need now using the given options. */
1573 void
1574 rescan_periodic_events(const or_options_t *options)
1576 tor_assert(options);
1578 /* Avoid scanning the event list if we haven't initialized it yet. This is
1579 * particularly useful for unit tests in order to avoid initializing main
1580 * loop events everytime. */
1581 if (!periodic_events_initialized) {
1582 return;
1585 int roles = get_my_roles(options);
1587 for (int i = 0; periodic_events[i].name; ++i) {
1588 periodic_event_item_t *item = &periodic_events[i];
1590 /* Handle the event flags. */
1591 if (net_is_disabled() &&
1592 (item->flags & PERIODIC_EVENT_FLAG_NEED_NET)) {
1593 continue;
1596 /* Enable the event if needed. It is safe to enable an event that was
1597 * already enabled. Same goes for disabling it. */
1598 if (item->roles & roles) {
1599 log_debug(LD_GENERAL, "Launching periodic event %s", item->name);
1600 periodic_event_enable(item);
1601 } else {
1602 log_debug(LD_GENERAL, "Disabling periodic event %s", item->name);
1603 periodic_event_disable(item);
1608 /* We just got new options globally set, see if we need to enabled or disable
1609 * periodic events. */
1610 void
1611 periodic_events_on_new_options(const or_options_t *options)
1613 /* Only if we've already initialized the events, rescan the list which will
1614 * enable or disable events depending on our roles. This will be called at
1615 * bootup and we don't want this function to initialize the events because
1616 * they aren't set up at this stage. */
1617 if (periodic_events_initialized) {
1618 rescan_periodic_events(options);
1623 * Update our schedule so that we'll check whether we need to update our
1624 * descriptor immediately, rather than after up to CHECK_DESCRIPTOR_INTERVAL
1625 * seconds.
1627 void
1628 reschedule_descriptor_update_check(void)
1630 if (check_descriptor_event) {
1631 periodic_event_reschedule(check_descriptor_event);
1636 * Update our schedule so that we'll check whether we need to fetch directory
1637 * info immediately.
1639 void
1640 reschedule_directory_downloads(void)
1642 tor_assert(fetch_networkstatus_event);
1643 tor_assert(launch_descriptor_fetches_event);
1645 periodic_event_reschedule(fetch_networkstatus_event);
1646 periodic_event_reschedule(launch_descriptor_fetches_event);
1649 /** Mainloop callback: clean up circuits, channels, and connections
1650 * that are pending close. */
1651 static void
1652 postloop_cleanup_cb(mainloop_event_t *ev, void *arg)
1654 (void)ev;
1655 (void)arg;
1656 circuit_close_all_marked();
1657 close_closeable_connections();
1658 channel_run_cleanup();
1659 channel_listener_run_cleanup();
1662 /** Event to run postloop_cleanup_cb */
1663 static mainloop_event_t *postloop_cleanup_ev=NULL;
1665 /** Schedule a post-loop event to clean up marked channels, connections, and
1666 * circuits. */
1667 void
1668 mainloop_schedule_postloop_cleanup(void)
1670 if (PREDICT_UNLIKELY(postloop_cleanup_ev == NULL)) {
1671 // (It's possible that we can get here if we decide to close a connection
1672 // in the earliest stages of our configuration, before we create events.)
1673 return;
1675 mainloop_event_activate(postloop_cleanup_ev);
1678 #define LONGEST_TIMER_PERIOD (30 * 86400)
1679 /** Helper: Return the number of seconds between <b>now</b> and <b>next</b>,
1680 * clipped to the range [1 second, LONGEST_TIMER_PERIOD]. */
1681 static inline int
1682 safe_timer_diff(time_t now, time_t next)
1684 if (next > now) {
1685 /* There were no computers at signed TIME_MIN (1902 on 32-bit systems),
1686 * and nothing that could run Tor. It's a bug if 'next' is around then.
1687 * On 64-bit systems with signed TIME_MIN, TIME_MIN is before the Big
1688 * Bang. We cannot extrapolate past a singularity, but there was probably
1689 * nothing that could run Tor then, either.
1691 tor_assert(next > TIME_MIN + LONGEST_TIMER_PERIOD);
1693 if (next - LONGEST_TIMER_PERIOD > now)
1694 return LONGEST_TIMER_PERIOD;
1695 return (int)(next - now);
1696 } else {
1697 return 1;
1701 /** Perform regular maintenance tasks. This function gets run once per
1702 * second by second_elapsed_callback().
1704 static void
1705 run_scheduled_events(time_t now)
1707 const or_options_t *options = get_options();
1709 /* 0. See if we've been asked to shut down and our timeout has
1710 * expired; or if our bandwidth limits are exhausted and we
1711 * should hibernate; or if it's time to wake up from hibernation.
1713 consider_hibernation(now);
1715 /* Maybe enough time elapsed for us to reconsider a circuit. */
1716 circuit_upgrade_circuits_from_guard_wait();
1718 if (options->UseBridges && !net_is_disabled()) {
1719 /* Note: this check uses net_is_disabled(), not should_delay_dir_fetches()
1720 * -- the latter is only for fetching consensus-derived directory info. */
1721 fetch_bridge_descriptors(options, now);
1724 if (accounting_is_enabled(options)) {
1725 accounting_run_housekeeping(now);
1728 /* 3a. Every second, we examine pending circuits and prune the
1729 * ones which have been pending for more than a few seconds.
1730 * We do this before step 4, so it can try building more if
1731 * it's not comfortable with the number of available circuits.
1733 /* (If our circuit build timeout can ever become lower than a second (which
1734 * it can't, currently), we should do this more often.) */
1735 circuit_expire_building();
1736 circuit_expire_waiting_for_better_guard();
1738 /* 3b. Also look at pending streams and prune the ones that 'began'
1739 * a long time ago but haven't gotten a 'connected' yet.
1740 * Do this before step 4, so we can put them back into pending
1741 * state to be picked up by the new circuit.
1743 connection_ap_expire_beginning();
1745 /* 3c. And expire connections that we've held open for too long.
1747 connection_expire_held_open();
1749 /* 4. Every second, we try a new circuit if there are no valid
1750 * circuits. Every NewCircuitPeriod seconds, we expire circuits
1751 * that became dirty more than MaxCircuitDirtiness seconds ago,
1752 * and we make a new circ if there are no clean circuits.
1754 const int have_dir_info = router_have_minimum_dir_info();
1755 if (have_dir_info && !net_is_disabled()) {
1756 circuit_build_needed_circs(now);
1757 } else {
1758 circuit_expire_old_circs_as_needed(now);
1761 /* 5. We do housekeeping for each connection... */
1762 channel_update_bad_for_new_circs(NULL, 0);
1763 int i;
1764 for (i=0;i<smartlist_len(connection_array);i++) {
1765 run_connection_housekeeping(i, now);
1768 /* 11b. check pending unconfigured managed proxies */
1769 if (!net_is_disabled() && pt_proxies_configuration_pending())
1770 pt_configure_remaining_proxies();
1773 /* Periodic callback: rotate the onion keys after the period defined by the
1774 * "onion-key-rotation-days" consensus parameter, shut down and restart all
1775 * cpuworkers, and update our descriptor if necessary.
1777 static int
1778 rotate_onion_key_callback(time_t now, const or_options_t *options)
1780 if (server_mode(options)) {
1781 int onion_key_lifetime = get_onion_key_lifetime();
1782 time_t rotation_time = get_onion_key_set_at()+onion_key_lifetime;
1783 if (rotation_time > now) {
1784 return ONION_KEY_CONSENSUS_CHECK_INTERVAL;
1787 log_info(LD_GENERAL,"Rotating onion key.");
1788 rotate_onion_key();
1789 cpuworkers_rotate_keyinfo();
1790 if (router_rebuild_descriptor(1)<0) {
1791 log_info(LD_CONFIG, "Couldn't rebuild router descriptor");
1793 if (advertised_server_mode() && !net_is_disabled())
1794 router_upload_dir_desc_to_dirservers(0);
1795 return ONION_KEY_CONSENSUS_CHECK_INTERVAL;
1797 return PERIODIC_EVENT_NO_UPDATE;
1800 /* Period callback: Check if our old onion keys are still valid after the
1801 * period of time defined by the consensus parameter
1802 * "onion-key-grace-period-days", otherwise expire them by setting them to
1803 * NULL.
1805 static int
1806 check_onion_keys_expiry_time_callback(time_t now, const or_options_t *options)
1808 if (server_mode(options)) {
1809 int onion_key_grace_period = get_onion_key_grace_period();
1810 time_t expiry_time = get_onion_key_set_at()+onion_key_grace_period;
1811 if (expiry_time > now) {
1812 return ONION_KEY_CONSENSUS_CHECK_INTERVAL;
1815 log_info(LD_GENERAL, "Expiring old onion keys.");
1816 expire_old_onion_keys();
1817 cpuworkers_rotate_keyinfo();
1818 return ONION_KEY_CONSENSUS_CHECK_INTERVAL;
1821 return PERIODIC_EVENT_NO_UPDATE;
1824 /* Periodic callback: Every 30 seconds, check whether it's time to make new
1825 * Ed25519 subkeys.
1827 static int
1828 check_ed_keys_callback(time_t now, const or_options_t *options)
1830 if (server_mode(options)) {
1831 if (should_make_new_ed_keys(options, now)) {
1832 int new_signing_key = load_ed_keys(options, now);
1833 if (new_signing_key < 0 ||
1834 generate_ed_link_cert(options, now, new_signing_key > 0)) {
1835 log_err(LD_OR, "Unable to update Ed25519 keys! Exiting.");
1836 tor_shutdown_event_loop_and_exit(1);
1839 return 30;
1841 return PERIODIC_EVENT_NO_UPDATE;
1845 * Periodic callback: Every {LAZY,GREEDY}_DESCRIPTOR_RETRY_INTERVAL,
1846 * see about fetching descriptors, microdescriptors, and extrainfo
1847 * documents.
1849 static int
1850 launch_descriptor_fetches_callback(time_t now, const or_options_t *options)
1852 if (should_delay_dir_fetches(options, NULL))
1853 return PERIODIC_EVENT_NO_UPDATE;
1855 update_all_descriptor_downloads(now);
1856 update_extrainfo_downloads(now);
1857 if (router_have_minimum_dir_info())
1858 return LAZY_DESCRIPTOR_RETRY_INTERVAL;
1859 else
1860 return GREEDY_DESCRIPTOR_RETRY_INTERVAL;
1864 * Periodic event: Rotate our X.509 certificates and TLS keys once every
1865 * MAX_SSL_KEY_LIFETIME_INTERNAL.
1867 static int
1868 rotate_x509_certificate_callback(time_t now, const or_options_t *options)
1870 static int first = 1;
1871 (void)now;
1872 (void)options;
1873 if (first) {
1874 first = 0;
1875 return MAX_SSL_KEY_LIFETIME_INTERNAL;
1878 /* 1b. Every MAX_SSL_KEY_LIFETIME_INTERNAL seconds, we change our
1879 * TLS context. */
1880 log_info(LD_GENERAL,"Rotating tls context.");
1881 if (router_initialize_tls_context() < 0) {
1882 log_err(LD_BUG, "Error reinitializing TLS context");
1883 tor_assert_unreached();
1885 if (generate_ed_link_cert(options, now, 1)) {
1886 log_err(LD_OR, "Unable to update Ed25519->TLS link certificate for "
1887 "new TLS context.");
1888 tor_assert_unreached();
1891 /* We also make sure to rotate the TLS connections themselves if they've
1892 * been up for too long -- but that's done via is_bad_for_new_circs in
1893 * run_connection_housekeeping() above. */
1894 return MAX_SSL_KEY_LIFETIME_INTERNAL;
1898 * Periodic callback: once an hour, grab some more entropy from the
1899 * kernel and feed it to our CSPRNG.
1901 static int
1902 add_entropy_callback(time_t now, const or_options_t *options)
1904 (void)now;
1905 (void)options;
1906 /* We already seeded once, so don't die on failure. */
1907 if (crypto_seed_rng() < 0) {
1908 log_warn(LD_GENERAL, "Tried to re-seed RNG, but failed. We already "
1909 "seeded once, though, so we won't exit here.");
1912 /** How often do we add more entropy to OpenSSL's RNG pool? */
1913 #define ENTROPY_INTERVAL (60*60)
1914 return ENTROPY_INTERVAL;
1918 * Periodic callback: if we're an authority, make sure we test
1919 * the routers on the network for reachability.
1921 static int
1922 launch_reachability_tests_callback(time_t now, const or_options_t *options)
1924 if (authdir_mode_tests_reachability(options) &&
1925 !net_is_disabled()) {
1926 /* try to determine reachability of the other Tor relays */
1927 dirserv_test_reachability(now);
1929 return REACHABILITY_TEST_INTERVAL;
1933 * Periodic callback: if we're an authority, discount the stability
1934 * information (and other rephist information) that's older.
1936 static int
1937 downrate_stability_callback(time_t now, const or_options_t *options)
1939 (void)options;
1940 /* 1d. Periodically, we discount older stability information so that new
1941 * stability info counts more, and save the stability information to disk as
1942 * appropriate. */
1943 time_t next = rep_hist_downrate_old_runs(now);
1944 return safe_timer_diff(now, next);
1948 * Periodic callback: if we're an authority, record our measured stability
1949 * information from rephist in an mtbf file.
1951 static int
1952 save_stability_callback(time_t now, const or_options_t *options)
1954 if (authdir_mode_tests_reachability(options)) {
1955 if (rep_hist_record_mtbf_data(now, 1)<0) {
1956 log_warn(LD_GENERAL, "Couldn't store mtbf data.");
1959 #define SAVE_STABILITY_INTERVAL (30*60)
1960 return SAVE_STABILITY_INTERVAL;
1964 * Periodic callback: if we're an authority, check on our authority
1965 * certificate (the one that authenticates our authority signing key).
1967 static int
1968 check_authority_cert_callback(time_t now, const or_options_t *options)
1970 (void)now;
1971 (void)options;
1972 /* 1e. Periodically, if we're a v3 authority, we check whether our cert is
1973 * close to expiring and warn the admin if it is. */
1974 v3_authority_check_key_expiry();
1975 #define CHECK_V3_CERTIFICATE_INTERVAL (5*60)
1976 return CHECK_V3_CERTIFICATE_INTERVAL;
1980 * Scheduled callback: Run directory-authority voting functionality.
1982 * The schedule is a bit complicated here, so dirvote_act() manages the
1983 * schedule itself.
1985 static int
1986 dirvote_callback(time_t now, const or_options_t *options)
1988 if (!authdir_mode_v3(options)) {
1989 tor_assert_nonfatal_unreached();
1990 return 3600;
1993 time_t next = dirvote_act(options, now);
1994 if (BUG(next == TIME_MAX)) {
1995 /* This shouldn't be returned unless we called dirvote_act() without
1996 * being an authority. If it happens, maybe our configuration will
1997 * fix itself in an hour or so? */
1998 return 3600;
2000 return safe_timer_diff(now, next);
2003 /** Reschedule the directory-authority voting event. Run this whenever the
2004 * schedule has changed. */
2005 void
2006 reschedule_dirvote(const or_options_t *options)
2008 if (periodic_events_initialized && authdir_mode_v3(options)) {
2009 periodic_event_reschedule(dirvote_event);
2014 * Periodic callback: If our consensus is too old, recalculate whether
2015 * we can actually use it.
2017 static int
2018 check_expired_networkstatus_callback(time_t now, const or_options_t *options)
2020 (void)options;
2021 /* Check whether our networkstatus has expired. */
2022 networkstatus_t *ns = networkstatus_get_latest_consensus();
2023 /*XXXX RD: This value needs to be the same as REASONABLY_LIVE_TIME in
2024 * networkstatus_get_reasonably_live_consensus(), but that value is way
2025 * way too high. Arma: is the bridge issue there resolved yet? -NM */
2026 #define NS_EXPIRY_SLOP (24*60*60)
2027 if (ns && ns->valid_until < (now - NS_EXPIRY_SLOP) &&
2028 router_have_minimum_dir_info()) {
2029 router_dir_info_changed();
2031 #define CHECK_EXPIRED_NS_INTERVAL (2*60)
2032 return CHECK_EXPIRED_NS_INTERVAL;
2036 * Scheduled callback: Save the state file to disk if appropriate.
2038 static int
2039 save_state_callback(time_t now, const or_options_t *options)
2041 (void) options;
2042 (void) or_state_save(now); // only saves if appropriate
2043 const time_t next_write = get_or_state()->next_write;
2044 if (next_write == TIME_MAX) {
2045 return 86400;
2047 return safe_timer_diff(now, next_write);
2050 /** Reschedule the event for saving the state file.
2052 * Run this when the state becomes dirty. */
2053 void
2054 reschedule_or_state_save(void)
2056 if (save_state_event == NULL) {
2057 /* This can happen early on during startup. */
2058 return;
2060 periodic_event_reschedule(save_state_event);
2064 * Periodic callback: Write statistics to disk if appropriate.
2066 static int
2067 write_stats_file_callback(time_t now, const or_options_t *options)
2069 /* 1g. Check whether we should write statistics to disk.
2071 #define CHECK_WRITE_STATS_INTERVAL (60*60)
2072 time_t next_time_to_write_stats_files = now + CHECK_WRITE_STATS_INTERVAL;
2073 if (options->CellStatistics) {
2074 time_t next_write =
2075 rep_hist_buffer_stats_write(now);
2076 if (next_write && next_write < next_time_to_write_stats_files)
2077 next_time_to_write_stats_files = next_write;
2079 if (options->DirReqStatistics) {
2080 time_t next_write = geoip_dirreq_stats_write(now);
2081 if (next_write && next_write < next_time_to_write_stats_files)
2082 next_time_to_write_stats_files = next_write;
2084 if (options->EntryStatistics) {
2085 time_t next_write = geoip_entry_stats_write(now);
2086 if (next_write && next_write < next_time_to_write_stats_files)
2087 next_time_to_write_stats_files = next_write;
2089 if (options->HiddenServiceStatistics) {
2090 time_t next_write = rep_hist_hs_stats_write(now);
2091 if (next_write && next_write < next_time_to_write_stats_files)
2092 next_time_to_write_stats_files = next_write;
2094 if (options->ExitPortStatistics) {
2095 time_t next_write = rep_hist_exit_stats_write(now);
2096 if (next_write && next_write < next_time_to_write_stats_files)
2097 next_time_to_write_stats_files = next_write;
2099 if (options->ConnDirectionStatistics) {
2100 time_t next_write = rep_hist_conn_stats_write(now);
2101 if (next_write && next_write < next_time_to_write_stats_files)
2102 next_time_to_write_stats_files = next_write;
2104 if (options->BridgeAuthoritativeDir) {
2105 time_t next_write = rep_hist_desc_stats_write(now);
2106 if (next_write && next_write < next_time_to_write_stats_files)
2107 next_time_to_write_stats_files = next_write;
2110 return safe_timer_diff(now, next_time_to_write_stats_files);
2113 #define CHANNEL_CHECK_INTERVAL (60*60)
2114 static int
2115 check_canonical_channels_callback(time_t now, const or_options_t *options)
2117 (void)now;
2118 if (public_server_mode(options))
2119 channel_check_for_duplicates();
2121 return CHANNEL_CHECK_INTERVAL;
2124 static int
2125 reset_padding_counts_callback(time_t now, const or_options_t *options)
2127 if (options->PaddingStatistics) {
2128 rep_hist_prep_published_padding_counts(now);
2131 rep_hist_reset_padding_counts();
2132 return REPHIST_CELL_PADDING_COUNTS_INTERVAL;
2135 static int should_init_bridge_stats = 1;
2138 * Periodic callback: Write bridge statistics to disk if appropriate.
2140 static int
2141 record_bridge_stats_callback(time_t now, const or_options_t *options)
2143 /* 1h. Check whether we should write bridge statistics to disk.
2145 if (should_record_bridge_info(options)) {
2146 if (should_init_bridge_stats) {
2147 /* (Re-)initialize bridge statistics. */
2148 geoip_bridge_stats_init(now);
2149 should_init_bridge_stats = 0;
2150 return WRITE_STATS_INTERVAL;
2151 } else {
2152 /* Possibly write bridge statistics to disk and ask when to write
2153 * them next time. */
2154 time_t next = geoip_bridge_stats_write(now);
2155 return safe_timer_diff(now, next);
2157 } else if (!should_init_bridge_stats) {
2158 /* Bridge mode was turned off. Ensure that stats are re-initialized
2159 * next time bridge mode is turned on. */
2160 should_init_bridge_stats = 1;
2162 return PERIODIC_EVENT_NO_UPDATE;
2166 * Periodic callback: Clean in-memory caches every once in a while
2168 static int
2169 clean_caches_callback(time_t now, const or_options_t *options)
2171 /* Remove old information from rephist and the rend cache. */
2172 rep_history_clean(now - options->RephistTrackTime);
2173 rend_cache_clean(now, REND_CACHE_TYPE_SERVICE);
2174 hs_cache_clean_as_client(now);
2175 hs_cache_clean_as_dir(now);
2176 microdesc_cache_rebuild(NULL, 0);
2177 #define CLEAN_CACHES_INTERVAL (30*60)
2178 return CLEAN_CACHES_INTERVAL;
2182 * Periodic callback: Clean the cache of failed hidden service lookups
2183 * frequently.
2185 static int
2186 rend_cache_failure_clean_callback(time_t now, const or_options_t *options)
2188 (void)options;
2189 /* We don't keep entries that are more than five minutes old so we try to
2190 * clean it as soon as we can since we want to make sure the client waits
2191 * as little as possible for reachability reasons. */
2192 rend_cache_failure_clean(now);
2193 hs_cache_client_intro_state_clean(now);
2194 return 30;
2198 * Periodic callback: If we're a server and initializing dns failed, retry.
2200 static int
2201 retry_dns_callback(time_t now, const or_options_t *options)
2203 (void)now;
2204 #define RETRY_DNS_INTERVAL (10*60)
2205 if (server_mode(options) && has_dns_init_failed())
2206 dns_init();
2207 return RETRY_DNS_INTERVAL;
2210 /** Periodic callback: consider rebuilding or and re-uploading our descriptor
2211 * (if we've passed our internal checks). */
2212 static int
2213 check_descriptor_callback(time_t now, const or_options_t *options)
2215 /** How often do we check whether part of our router info has changed in a
2216 * way that would require an upload? That includes checking whether our IP
2217 * address has changed. */
2218 #define CHECK_DESCRIPTOR_INTERVAL (60)
2220 (void)options;
2222 /* 2b. Once per minute, regenerate and upload the descriptor if the old
2223 * one is inaccurate. */
2224 if (!net_is_disabled()) {
2225 check_descriptor_bandwidth_changed(now);
2226 check_descriptor_ipaddress_changed(now);
2227 mark_my_descriptor_dirty_if_too_old(now);
2228 consider_publishable_server(0);
2229 /* If any networkstatus documents are no longer recent, we need to
2230 * update all the descriptors' running status. */
2231 /* Remove dead routers. */
2232 /* XXXX This doesn't belong here, but it was here in the pre-
2233 * XXXX refactoring code. */
2234 routerlist_remove_old_routers();
2237 return CHECK_DESCRIPTOR_INTERVAL;
2241 * Periodic callback: check whether we're reachable (as a relay), and
2242 * whether our bandwidth has changed enough that we need to
2243 * publish a new descriptor.
2245 static int
2246 check_for_reachability_bw_callback(time_t now, const or_options_t *options)
2248 /* XXXX This whole thing was stuck in the middle of what is now
2249 * XXXX check_descriptor_callback. I'm not sure it's right. */
2251 static int dirport_reachability_count = 0;
2252 /* also, check religiously for reachability, if it's within the first
2253 * 20 minutes of our uptime. */
2254 if (server_mode(options) &&
2255 (have_completed_a_circuit() || !any_predicted_circuits(now)) &&
2256 !net_is_disabled()) {
2257 if (get_uptime() < TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT) {
2258 router_do_reachability_checks(1, dirport_reachability_count==0);
2259 if (++dirport_reachability_count > 5)
2260 dirport_reachability_count = 0;
2261 return 1;
2262 } else {
2263 /* If we haven't checked for 12 hours and our bandwidth estimate is
2264 * low, do another bandwidth test. This is especially important for
2265 * bridges, since they might go long periods without much use. */
2266 const routerinfo_t *me = router_get_my_routerinfo();
2267 static int first_time = 1;
2268 if (!first_time && me &&
2269 me->bandwidthcapacity < me->bandwidthrate &&
2270 me->bandwidthcapacity < 51200) {
2271 reset_bandwidth_test();
2273 first_time = 0;
2274 #define BANDWIDTH_RECHECK_INTERVAL (12*60*60)
2275 return BANDWIDTH_RECHECK_INTERVAL;
2278 return CHECK_DESCRIPTOR_INTERVAL;
2282 * Periodic event: once a minute, (or every second if TestingTorNetwork, or
2283 * during client bootstrap), check whether we want to download any
2284 * networkstatus documents. */
2285 static int
2286 fetch_networkstatus_callback(time_t now, const or_options_t *options)
2288 /* How often do we check whether we should download network status
2289 * documents? */
2290 const int we_are_bootstrapping = networkstatus_consensus_is_bootstrapping(
2291 now);
2292 const int prefer_mirrors = !directory_fetches_from_authorities(
2293 get_options());
2294 int networkstatus_dl_check_interval = 60;
2295 /* check more often when testing, or when bootstrapping from mirrors
2296 * (connection limits prevent too many connections being made) */
2297 if (options->TestingTorNetwork
2298 || (we_are_bootstrapping && prefer_mirrors)) {
2299 networkstatus_dl_check_interval = 1;
2302 if (should_delay_dir_fetches(options, NULL))
2303 return PERIODIC_EVENT_NO_UPDATE;
2305 update_networkstatus_downloads(now);
2306 return networkstatus_dl_check_interval;
2310 * Periodic callback: Every 60 seconds, we relaunch listeners if any died. */
2311 static int
2312 retry_listeners_callback(time_t now, const or_options_t *options)
2314 (void)now;
2315 (void)options;
2316 if (!net_is_disabled()) {
2317 retry_all_listeners(NULL, 0);
2318 return 60;
2320 return PERIODIC_EVENT_NO_UPDATE;
2324 * Periodic callback: as a server, see if we have any old unused circuits
2325 * that should be expired */
2326 static int
2327 expire_old_ciruits_serverside_callback(time_t now, const or_options_t *options)
2329 (void)options;
2330 /* every 11 seconds, so not usually the same second as other such events */
2331 circuit_expire_old_circuits_serverside(now);
2332 return 11;
2336 * Callback: Send warnings if Tor doesn't find its ports reachable.
2338 static int
2339 reachability_warnings_callback(time_t now, const or_options_t *options)
2341 (void) now;
2343 if (get_uptime() < TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT) {
2344 return (int)(TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT - get_uptime());
2347 if (server_mode(options) &&
2348 !net_is_disabled() &&
2349 have_completed_a_circuit()) {
2350 /* every 20 minutes, check and complain if necessary */
2351 const routerinfo_t *me = router_get_my_routerinfo();
2352 if (me && !check_whether_orport_reachable(options)) {
2353 char *address = tor_dup_ip(me->addr);
2354 log_warn(LD_CONFIG,"Your server (%s:%d) has not managed to confirm that "
2355 "its ORPort is reachable. Relays do not publish descriptors "
2356 "until their ORPort and DirPort are reachable. Please check "
2357 "your firewalls, ports, address, /etc/hosts file, etc.",
2358 address, me->or_port);
2359 control_event_server_status(LOG_WARN,
2360 "REACHABILITY_FAILED ORADDRESS=%s:%d",
2361 address, me->or_port);
2362 tor_free(address);
2365 if (me && !check_whether_dirport_reachable(options)) {
2366 char *address = tor_dup_ip(me->addr);
2367 log_warn(LD_CONFIG,
2368 "Your server (%s:%d) has not managed to confirm that its "
2369 "DirPort is reachable. Relays do not publish descriptors "
2370 "until their ORPort and DirPort are reachable. Please check "
2371 "your firewalls, ports, address, /etc/hosts file, etc.",
2372 address, me->dir_port);
2373 control_event_server_status(LOG_WARN,
2374 "REACHABILITY_FAILED DIRADDRESS=%s:%d",
2375 address, me->dir_port);
2376 tor_free(address);
2380 return TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT;
2383 static int dns_honesty_first_time = 1;
2386 * Periodic event: if we're an exit, see if our DNS server is telling us
2387 * obvious lies.
2389 static int
2390 check_dns_honesty_callback(time_t now, const or_options_t *options)
2392 (void)now;
2393 /* 9. and if we're an exit node, check whether our DNS is telling stories
2394 * to us. */
2395 if (net_is_disabled() ||
2396 ! public_server_mode(options) ||
2397 router_my_exit_policy_is_reject_star())
2398 return PERIODIC_EVENT_NO_UPDATE;
2400 if (dns_honesty_first_time) {
2401 /* Don't launch right when we start */
2402 dns_honesty_first_time = 0;
2403 return crypto_rand_int_range(60, 180);
2406 dns_launch_correctness_checks();
2407 return 12*3600 + crypto_rand_int(12*3600);
2411 * Periodic callback: if we're the bridge authority, write a networkstatus
2412 * file to disk.
2414 static int
2415 write_bridge_ns_callback(time_t now, const or_options_t *options)
2417 /* 10. write bridge networkstatus file to disk */
2418 if (options->BridgeAuthoritativeDir) {
2419 networkstatus_dump_bridge_status_to_file(now);
2420 #define BRIDGE_STATUSFILE_INTERVAL (30*60)
2421 return BRIDGE_STATUSFILE_INTERVAL;
2423 return PERIODIC_EVENT_NO_UPDATE;
2426 static int heartbeat_callback_first_time = 1;
2429 * Periodic callback: write the heartbeat message in the logs.
2431 * If writing the heartbeat message to the logs fails for some reason, retry
2432 * again after <b>MIN_HEARTBEAT_PERIOD</b> seconds.
2434 static int
2435 heartbeat_callback(time_t now, const or_options_t *options)
2437 /* Check if heartbeat is disabled */
2438 if (!options->HeartbeatPeriod) {
2439 return PERIODIC_EVENT_NO_UPDATE;
2442 /* Skip the first one. */
2443 if (heartbeat_callback_first_time) {
2444 heartbeat_callback_first_time = 0;
2445 return options->HeartbeatPeriod;
2448 /* Write the heartbeat message */
2449 if (log_heartbeat(now) == 0) {
2450 return options->HeartbeatPeriod;
2451 } else {
2452 /* If we couldn't write the heartbeat log message, try again in the minimum
2453 * interval of time. */
2454 return MIN_HEARTBEAT_PERIOD;
2458 #define CDM_CLEAN_CALLBACK_INTERVAL 600
2459 static int
2460 clean_consdiffmgr_callback(time_t now, const or_options_t *options)
2462 (void)now;
2463 if (dir_server_mode(options)) {
2464 consdiffmgr_cleanup();
2466 return CDM_CLEAN_CALLBACK_INTERVAL;
2470 * Periodic callback: Run scheduled events for HS service. This is called
2471 * every second.
2473 static int
2474 hs_service_callback(time_t now, const or_options_t *options)
2476 (void) options;
2478 /* We need to at least be able to build circuits and that we actually have
2479 * a working network. */
2480 if (!have_completed_a_circuit() || net_is_disabled() ||
2481 networkstatus_get_live_consensus(now) == NULL) {
2482 goto end;
2485 hs_service_run_scheduled_events(now);
2487 end:
2488 /* Every 1 second. */
2489 return 1;
2492 /** Timer: used to invoke second_elapsed_callback() once per second. */
2493 static periodic_timer_t *second_timer = NULL;
2496 * Enable or disable the per-second timer as appropriate, creating it if
2497 * necessary.
2499 void
2500 reschedule_per_second_timer(void)
2502 struct timeval one_second;
2503 one_second.tv_sec = 1;
2504 one_second.tv_usec = 0;
2506 if (! second_timer) {
2507 second_timer = periodic_timer_new(tor_libevent_get_base(),
2508 &one_second,
2509 second_elapsed_callback,
2510 NULL);
2511 tor_assert(second_timer);
2514 const bool run_per_second_events =
2515 control_any_per_second_event_enabled() || ! net_is_completely_disabled();
2517 if (run_per_second_events) {
2518 periodic_timer_launch(second_timer, &one_second);
2519 } else {
2520 periodic_timer_disable(second_timer);
2524 /** Last time that update_current_time was called. */
2525 static time_t current_second = 0;
2526 /** Last time that update_current_time updated current_second. */
2527 static monotime_coarse_t current_second_last_changed;
2530 * Set the current time to "now", which should be the value returned by
2531 * time(). Check for clock jumps and track the total number of seconds we
2532 * have been running.
2534 void
2535 update_current_time(time_t now)
2537 if (PREDICT_LIKELY(now == current_second)) {
2538 /* We call this function a lot. Most frequently, the current second
2539 * will not have changed, so we just return. */
2540 return;
2543 const time_t seconds_elapsed = current_second ? (now - current_second) : 0;
2545 /* Check the wall clock against the monotonic clock, so we can
2546 * better tell idleness from clock jumps and/or other shenanigans. */
2547 monotime_coarse_t last_updated;
2548 memcpy(&last_updated, &current_second_last_changed, sizeof(last_updated));
2549 monotime_coarse_get(&current_second_last_changed);
2551 /** How much clock jumping do we tolerate? */
2552 #define NUM_JUMPED_SECONDS_BEFORE_WARN 100
2554 /** How much idleness do we tolerate? */
2555 #define NUM_IDLE_SECONDS_BEFORE_WARN 3600
2557 if (seconds_elapsed < -NUM_JUMPED_SECONDS_BEFORE_WARN) {
2558 // moving back in time is always a bad sign.
2559 circuit_note_clock_jumped(seconds_elapsed, false);
2560 } else if (seconds_elapsed >= NUM_JUMPED_SECONDS_BEFORE_WARN) {
2561 /* Compare the monotonic clock to the result of time(). */
2562 const int32_t monotime_msec_passed =
2563 monotime_coarse_diff_msec32(&last_updated,
2564 &current_second_last_changed);
2565 const int monotime_sec_passed = monotime_msec_passed / 1000;
2566 const int discrepancy = monotime_sec_passed - (int)seconds_elapsed;
2567 /* If the monotonic clock deviates from time(NULL), we have a couple of
2568 * possibilities. On some systems, this means we have been suspended or
2569 * sleeping. Everywhere, it can mean that the wall-clock time has
2570 * been changed -- for example, with settimeofday().
2572 * On the other hand, if the monotonic time matches with the wall-clock
2573 * time, we've probably just been idle for a while, with no events firing.
2574 * we tolerate much more of that.
2576 const bool clock_jumped = abs(discrepancy) > 2;
2578 if (clock_jumped || seconds_elapsed >= NUM_IDLE_SECONDS_BEFORE_WARN) {
2579 circuit_note_clock_jumped(seconds_elapsed, ! clock_jumped);
2581 } else if (seconds_elapsed > 0) {
2582 stats_n_seconds_working += seconds_elapsed;
2585 update_approx_time(now);
2586 current_second = now;
2589 /** Libevent callback: invoked once every second. */
2590 static void
2591 second_elapsed_callback(periodic_timer_t *timer, void *arg)
2593 /* XXXX This could be sensibly refactored into multiple callbacks, and we
2594 * could use Libevent's timers for this rather than checking the current
2595 * time against a bunch of timeouts every second. */
2596 time_t now;
2597 (void)timer;
2598 (void)arg;
2600 now = time(NULL);
2602 /* We don't need to do this once-per-second any more: time-updating is
2603 * only in this callback _because it is a callback_. It should be fine
2604 * to disable this callback, and the time will still get updated.
2606 update_current_time(now);
2608 /* Maybe some controller events are ready to fire */
2609 control_per_second_events();
2611 run_scheduled_events(now);
2614 #ifdef HAVE_SYSTEMD_209
2615 static periodic_timer_t *systemd_watchdog_timer = NULL;
2617 /** Libevent callback: invoked to reset systemd watchdog. */
2618 static void
2619 systemd_watchdog_callback(periodic_timer_t *timer, void *arg)
2621 (void)timer;
2622 (void)arg;
2623 sd_notify(0, "WATCHDOG=1");
2625 #endif /* defined(HAVE_SYSTEMD_209) */
2627 #define UPTIME_CUTOFF_FOR_NEW_BANDWIDTH_TEST (6*60*60)
2629 /** Called when our IP address seems to have changed. <b>at_interface</b>
2630 * should be true if we detected a change in our interface, and false if we
2631 * detected a change in our published address. */
2632 void
2633 ip_address_changed(int at_interface)
2635 const or_options_t *options = get_options();
2636 int server = server_mode(options);
2637 int exit_reject_interfaces = (server && options->ExitRelay
2638 && options->ExitPolicyRejectLocalInterfaces);
2640 if (at_interface) {
2641 if (! server) {
2642 /* Okay, change our keys. */
2643 if (init_keys_client() < 0)
2644 log_warn(LD_GENERAL, "Unable to rotate keys after IP change!");
2646 } else {
2647 if (server) {
2648 if (get_uptime() > UPTIME_CUTOFF_FOR_NEW_BANDWIDTH_TEST)
2649 reset_bandwidth_test();
2650 reset_uptime();
2651 router_reset_reachability();
2655 /* Exit relays incorporate interface addresses in their exit policies when
2656 * ExitPolicyRejectLocalInterfaces is set */
2657 if (exit_reject_interfaces || (server && !at_interface)) {
2658 mark_my_descriptor_dirty("IP address changed");
2661 dns_servers_relaunch_checks();
2664 /** Forget what we've learned about the correctness of our DNS servers, and
2665 * start learning again. */
2666 void
2667 dns_servers_relaunch_checks(void)
2669 if (server_mode(get_options())) {
2670 dns_reset_correctness_checks();
2671 if (periodic_events_initialized) {
2672 tor_assert(check_dns_honesty_event);
2673 periodic_event_reschedule(check_dns_honesty_event);
2678 /** Initialize some mainloop_event_t objects that we require. */
2679 void
2680 initialize_mainloop_events(void)
2682 if (!schedule_active_linked_connections_event) {
2683 schedule_active_linked_connections_event =
2684 mainloop_event_postloop_new(schedule_active_linked_connections_cb, NULL);
2686 if (!postloop_cleanup_ev) {
2687 postloop_cleanup_ev =
2688 mainloop_event_postloop_new(postloop_cleanup_cb, NULL);
2692 /** Tor main loop. */
2694 do_main_loop(void)
2696 /* initialize the periodic events first, so that code that depends on the
2697 * events being present does not assert.
2699 if (! periodic_events_initialized) {
2700 initialize_periodic_events();
2703 initialize_mainloop_events();
2705 /* set up once-a-second callback. */
2706 reschedule_per_second_timer();
2708 #ifdef HAVE_SYSTEMD_209
2709 uint64_t watchdog_delay;
2710 /* set up systemd watchdog notification. */
2711 if (sd_watchdog_enabled(1, &watchdog_delay) > 0) {
2712 if (! systemd_watchdog_timer) {
2713 struct timeval watchdog;
2714 /* The manager will "act on" us if we don't send them a notification
2715 * every 'watchdog_delay' microseconds. So, send notifications twice
2716 * that often. */
2717 watchdog_delay /= 2;
2718 watchdog.tv_sec = watchdog_delay / 1000000;
2719 watchdog.tv_usec = watchdog_delay % 1000000;
2721 systemd_watchdog_timer = periodic_timer_new(tor_libevent_get_base(),
2722 &watchdog,
2723 systemd_watchdog_callback,
2724 NULL);
2725 tor_assert(systemd_watchdog_timer);
2728 #endif /* defined(HAVE_SYSTEMD_209) */
2730 main_loop_should_exit = 0;
2731 main_loop_exit_value = 0;
2733 #ifdef ENABLE_RESTART_DEBUGGING
2735 static int first_time = 1;
2737 if (first_time && getenv("TOR_DEBUG_RESTART")) {
2738 first_time = 0;
2739 const char *sec_str = getenv("TOR_DEBUG_RESTART_AFTER_SECONDS");
2740 long sec;
2741 int sec_ok=0;
2742 if (sec_str &&
2743 (sec = tor_parse_long(sec_str, 10, 0, INT_MAX, &sec_ok, NULL)) &&
2744 sec_ok) {
2745 /* Okay, we parsed the seconds. */
2746 } else {
2747 sec = 5;
2749 struct timeval restart_after = { (time_t) sec, 0 };
2750 tor_shutdown_event_loop_for_restart_event =
2751 tor_evtimer_new(tor_libevent_get_base(),
2752 tor_shutdown_event_loop_for_restart_cb, NULL);
2753 event_add(tor_shutdown_event_loop_for_restart_event, &restart_after);
2756 #endif
2758 return run_main_loop_until_done();
2761 #ifndef _WIN32
2762 /** Rate-limiter for EINVAL-type libevent warnings. */
2763 static ratelim_t libevent_error_ratelim = RATELIM_INIT(10);
2764 #endif
2767 * Run the main loop a single time. Return 0 for "exit"; -1 for "exit with
2768 * error", and 1 for "run this again."
2770 static int
2771 run_main_loop_once(void)
2773 int loop_result;
2775 if (nt_service_is_stopping())
2776 return 0;
2778 if (main_loop_should_exit)
2779 return 0;
2781 #ifndef _WIN32
2782 /* Make it easier to tell whether libevent failure is our fault or not. */
2783 errno = 0;
2784 #endif
2786 if (get_options()->MainloopStats) {
2787 /* We always enforce that EVLOOP_ONCE is passed to event_base_loop() if we
2788 * are collecting main loop statistics. */
2789 called_loop_once = 1;
2790 } else {
2791 called_loop_once = 0;
2794 /* Make sure we know (about) what time it is. */
2795 update_approx_time(time(NULL));
2797 /* Here it is: the main loop. Here we tell Libevent to poll until we have
2798 * an event, or the second ends, or until we have some active linked
2799 * connections to trigger events for. Libevent will wait till one
2800 * of these happens, then run all the appropriate callbacks. */
2801 loop_result = tor_libevent_run_event_loop(tor_libevent_get_base(),
2802 called_loop_once);
2804 if (get_options()->MainloopStats) {
2805 /* Update our main loop counters. */
2806 if (loop_result == 0) {
2807 // The call was successful.
2808 increment_main_loop_success_count();
2809 } else if (loop_result == -1) {
2810 // The call was erroneous.
2811 increment_main_loop_error_count();
2812 } else if (loop_result == 1) {
2813 // The call didn't have any active or pending events
2814 // to handle.
2815 increment_main_loop_idle_count();
2819 /* Oh, the loop failed. That might be an error that we need to
2820 * catch, but more likely, it's just an interrupted poll() call or something,
2821 * and we should try again. */
2822 if (loop_result < 0) {
2823 int e = tor_socket_errno(-1);
2824 /* let the program survive things like ^z */
2825 if (e != EINTR && !ERRNO_IS_EINPROGRESS(e)) {
2826 log_err(LD_NET,"libevent call with %s failed: %s [%d]",
2827 tor_libevent_get_method(), tor_socket_strerror(e), e);
2828 return -1;
2829 #ifndef _WIN32
2830 } else if (e == EINVAL) {
2831 log_fn_ratelim(&libevent_error_ratelim, LOG_WARN, LD_NET,
2832 "EINVAL from libevent: should you upgrade libevent?");
2833 if (libevent_error_ratelim.n_calls_since_last_time > 8) {
2834 log_err(LD_NET, "Too many libevent errors, too fast: dying");
2835 return -1;
2837 #endif /* !defined(_WIN32) */
2838 } else {
2839 tor_assert_nonfatal_once(! ERRNO_IS_EINPROGRESS(e));
2840 log_debug(LD_NET,"libevent call interrupted.");
2841 /* You can't trust the results of this poll(). Go back to the
2842 * top of the big for loop. */
2843 return 1;
2847 if (main_loop_should_exit)
2848 return 0;
2850 return 1;
2853 /** Run the run_main_loop_once() function until it declares itself done,
2854 * and return its final return value.
2856 * Shadow won't invoke this function, so don't fill it up with things.
2858 static int
2859 run_main_loop_until_done(void)
2861 int loop_result = 1;
2862 do {
2863 loop_result = run_main_loop_once();
2864 } while (loop_result == 1);
2866 if (main_loop_should_exit)
2867 return main_loop_exit_value;
2868 else
2869 return loop_result;
2872 /** Returns Tor's uptime. */
2873 MOCK_IMPL(long,
2874 get_uptime,(void))
2876 return stats_n_seconds_working;
2879 /** Reset Tor's uptime. */
2880 MOCK_IMPL(void,
2881 reset_uptime,(void))
2883 stats_n_seconds_working = 0;
2886 void
2887 tor_mainloop_free_all(void)
2889 smartlist_free(connection_array);
2890 smartlist_free(closeable_connection_lst);
2891 smartlist_free(active_linked_connection_lst);
2892 periodic_timer_free(second_timer);
2893 teardown_periodic_events();
2894 tor_event_free(shutdown_did_not_work_event);
2895 tor_event_free(initialize_periodic_events_event);
2896 mainloop_event_free(directory_all_unreachable_cb_event);
2897 mainloop_event_free(schedule_active_linked_connections_event);
2898 mainloop_event_free(postloop_cleanup_ev);
2899 mainloop_event_free(handle_deferred_signewnym_ev);
2901 #ifdef HAVE_SYSTEMD_209
2902 periodic_timer_free(systemd_watchdog_timer);
2903 #endif
2905 stats_n_bytes_read = stats_n_bytes_written = 0;
2907 memset(&global_bucket, 0, sizeof(global_bucket));
2908 memset(&global_relayed_bucket, 0, sizeof(global_relayed_bucket));
2909 time_of_process_start = 0;
2910 time_of_last_signewnym = 0;
2911 signewnym_is_pending = 0;
2912 newnym_epoch = 0;
2913 called_loop_once = 0;
2914 main_loop_should_exit = 0;
2915 main_loop_exit_value = 0;
2916 can_complete_circuits = 0;
2917 quiet_level = 0;
2918 should_init_bridge_stats = 1;
2919 dns_honesty_first_time = 1;
2920 heartbeat_callback_first_time = 1;
2921 current_second = 0;
2922 memset(&current_second_last_changed, 0,
2923 sizeof(current_second_last_changed));