Rebuild the guard lists as appropriate on torrc change.
[tor.git] / src / or / main.c
blob16106612a96c38308125bf6bffb749eecb673dea
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-2016, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
7 /**
8 * \file main.c
9 * \brief Toplevel module. Handles signals, multiplexes between
10 * connections, implements main loop, and drives scheduled events.
12 * For the main loop itself; see run_main_loop_once(). It invokes the rest of
13 * Tor mostly through Libevent callbacks. Libevent callbacks can happen when
14 * a timer elapses, a signal is received, a socket is ready to read or write,
15 * or an event is manually activated.
17 * Most events in Tor are driven from these callbacks:
18 * <ul>
19 * <li>conn_read_callback() and conn_write_callback() here, which are
20 * invoked when a socket is ready to read or write respectively.
21 * <li>signal_callback(), which handles incoming signals.
22 * </ul>
23 * Other events are used for specific purposes, or for building more complex
24 * control structures. If you search for usage of tor_libevent_new(), you
25 * will find all the events that we construct in Tor.
27 * Tor has numerous housekeeping operations that need to happen
28 * regularly. They are handled in different ways:
29 * <ul>
30 * <li>The most frequent operations are handled after every read or write
31 * event, at the end of connection_handle_read() and
32 * connection_handle_write().
34 * <li>The next most frequent operations happen after each invocation of the
35 * main loop, in run_main_loop_once().
37 * <li>Once per second, we run all of the operations listed in
38 * second_elapsed_callback(), and in its child, run_scheduled_events().
40 * <li>Once-a-second operations are handled in second_elapsed_callback().
42 * <li>More infrequent operations take place based on the periodic event
43 * driver in periodic.c . These are stored in the periodic_events[]
44 * table.
45 * </ul>
47 **/
49 #define MAIN_PRIVATE
50 #include "or.h"
51 #include "addressmap.h"
52 #include "backtrace.h"
53 #include "bridges.h"
54 #include "buffers.h"
55 #include "channel.h"
56 #include "channeltls.h"
57 #include "circuitbuild.h"
58 #include "circuitlist.h"
59 #include "circuituse.h"
60 #include "command.h"
61 #include "config.h"
62 #include "confparse.h"
63 #include "connection.h"
64 #include "connection_edge.h"
65 #include "connection_or.h"
66 #include "control.h"
67 #include "cpuworker.h"
68 #include "crypto_s2k.h"
69 #include "directory.h"
70 #include "dirserv.h"
71 #include "dirvote.h"
72 #include "dns.h"
73 #include "dnsserv.h"
74 #include "entrynodes.h"
75 #include "geoip.h"
76 #include "hibernate.h"
77 #include "hs_cache.h"
78 #include "keypin.h"
79 #include "main.h"
80 #include "microdesc.h"
81 #include "networkstatus.h"
82 #include "nodelist.h"
83 #include "ntmain.h"
84 #include "onion.h"
85 #include "periodic.h"
86 #include "policies.h"
87 #include "protover.h"
88 #include "transports.h"
89 #include "relay.h"
90 #include "rendclient.h"
91 #include "rendcommon.h"
92 #include "rendservice.h"
93 #include "rephist.h"
94 #include "router.h"
95 #include "routerkeys.h"
96 #include "routerlist.h"
97 #include "routerparse.h"
98 #include "scheduler.h"
99 #include "shared_random.h"
100 #include "statefile.h"
101 #include "status.h"
102 #include "util_process.h"
103 #include "ext_orport.h"
104 #ifdef USE_DMALLOC
105 #include <dmalloc.h>
106 #include <openssl/crypto.h>
107 #endif
108 #include "memarea.h"
109 #include "sandbox.h"
111 #include <event2/event.h>
113 #ifdef HAVE_SYSTEMD
114 # if defined(__COVERITY__) && !defined(__INCLUDE_LEVEL__)
115 /* Systemd's use of gcc's __INCLUDE_LEVEL__ extension macro appears to confuse
116 * Coverity. Here's a kludge to unconfuse it.
118 # define __INCLUDE_LEVEL__ 2
119 # endif
120 #include <systemd/sd-daemon.h>
121 #endif
123 void evdns_shutdown(int);
125 /********* PROTOTYPES **********/
127 static void dumpmemusage(int severity);
128 static void dumpstats(int severity); /* log stats */
129 static void conn_read_callback(evutil_socket_t fd, short event, void *_conn);
130 static void conn_write_callback(evutil_socket_t fd, short event, void *_conn);
131 static void second_elapsed_callback(periodic_timer_t *timer, void *args);
132 static int conn_close_if_marked(int i);
133 static void connection_start_reading_from_linked_conn(connection_t *conn);
134 static int connection_should_read_from_linked_conn(connection_t *conn);
135 static int run_main_loop_until_done(void);
136 static void process_signal(int sig);
138 /********* START VARIABLES **********/
139 int global_read_bucket; /**< Max number of bytes I can read this second. */
140 int global_write_bucket; /**< Max number of bytes I can write this second. */
142 /** Max number of relayed (bandwidth class 1) bytes I can read this second. */
143 int global_relayed_read_bucket;
144 /** Max number of relayed (bandwidth class 1) bytes I can write this second. */
145 int global_relayed_write_bucket;
146 /** What was the read bucket before the last second_elapsed_callback() call?
147 * (used to determine how many bytes we've read). */
148 static int stats_prev_global_read_bucket;
149 /** What was the write bucket before the last second_elapsed_callback() call?
150 * (used to determine how many bytes we've written). */
151 static int stats_prev_global_write_bucket;
153 /* DOCDOC stats_prev_n_read */
154 static uint64_t stats_prev_n_read = 0;
155 /* DOCDOC stats_prev_n_written */
156 static uint64_t stats_prev_n_written = 0;
158 /* XXX we might want to keep stats about global_relayed_*_bucket too. Or not.*/
159 /** How many bytes have we read since we started the process? */
160 static uint64_t stats_n_bytes_read = 0;
161 /** How many bytes have we written since we started the process? */
162 static uint64_t stats_n_bytes_written = 0;
163 /** What time did this process start up? */
164 time_t time_of_process_start = 0;
165 /** How many seconds have we been running? */
166 long stats_n_seconds_working = 0;
168 /** How often will we honor SIGNEWNYM requests? */
169 #define MAX_SIGNEWNYM_RATE 10
170 /** When did we last process a SIGNEWNYM request? */
171 static time_t time_of_last_signewnym = 0;
172 /** Is there a signewnym request we're currently waiting to handle? */
173 static int signewnym_is_pending = 0;
174 /** How many times have we called newnym? */
175 static unsigned newnym_epoch = 0;
177 /** Smartlist of all open connections. */
178 static smartlist_t *connection_array = NULL;
179 /** List of connections that have been marked for close and need to be freed
180 * and removed from connection_array. */
181 static smartlist_t *closeable_connection_lst = NULL;
182 /** List of linked connections that are currently reading data into their
183 * inbuf from their partner's outbuf. */
184 static smartlist_t *active_linked_connection_lst = NULL;
185 /** Flag: Set to true iff we entered the current libevent main loop via
186 * <b>loop_once</b>. If so, there's no need to trigger a loopexit in order
187 * to handle linked connections. */
188 static int called_loop_once = 0;
190 /** We set this to 1 when we've opened a circuit, so we can print a log
191 * entry to inform the user that Tor is working. We set it to 0 when
192 * we think the fact that we once opened a circuit doesn't mean we can do so
193 * any longer (a big time jump happened, when we notice our directory is
194 * heinously out-of-date, etc.
196 static int can_complete_circuits = 0;
198 /** How often do we check for router descriptors that we should download
199 * when we have too little directory info? */
200 #define GREEDY_DESCRIPTOR_RETRY_INTERVAL (10)
201 /** How often do we check for router descriptors that we should download
202 * when we have enough directory info? */
203 #define LAZY_DESCRIPTOR_RETRY_INTERVAL (60)
205 /** Decides our behavior when no logs are configured/before any
206 * logs have been configured. For 0, we log notice to stdout as normal.
207 * For 1, we log warnings only. For 2, we log nothing.
209 int quiet_level = 0;
211 /********* END VARIABLES ************/
213 /****************************************************************************
215 * This section contains accessors and other methods on the connection_array
216 * variables (which are global within this file and unavailable outside it).
218 ****************************************************************************/
220 /** Return 1 if we have successfully built a circuit, and nothing has changed
221 * to make us think that maybe we can't.
224 have_completed_a_circuit(void)
226 return can_complete_circuits;
229 /** Note that we have successfully built a circuit, so that reachability
230 * testing and introduction points and so on may be attempted. */
231 void
232 note_that_we_completed_a_circuit(void)
234 can_complete_circuits = 1;
237 /** Note that something has happened (like a clock jump, or DisableNetwork) to
238 * make us think that maybe we can't complete circuits. */
239 void
240 note_that_we_maybe_cant_complete_circuits(void)
242 can_complete_circuits = 0;
245 /** Add <b>conn</b> to the array of connections that we can poll on. The
246 * connection's socket must be set; the connection starts out
247 * non-reading and non-writing.
250 connection_add_impl(connection_t *conn, int is_connecting)
252 tor_assert(conn);
253 tor_assert(SOCKET_OK(conn->s) ||
254 conn->linked ||
255 (conn->type == CONN_TYPE_AP &&
256 TO_EDGE_CONN(conn)->is_dns_request));
258 tor_assert(conn->conn_array_index == -1); /* can only connection_add once */
259 conn->conn_array_index = smartlist_len(connection_array);
260 smartlist_add(connection_array, conn);
262 (void) is_connecting;
264 if (SOCKET_OK(conn->s) || conn->linked) {
265 conn->read_event = tor_event_new(tor_libevent_get_base(),
266 conn->s, EV_READ|EV_PERSIST, conn_read_callback, conn);
267 conn->write_event = tor_event_new(tor_libevent_get_base(),
268 conn->s, EV_WRITE|EV_PERSIST, conn_write_callback, conn);
269 /* XXXX CHECK FOR NULL RETURN! */
272 log_debug(LD_NET,"new conn type %s, socket %d, address %s, n_conns %d.",
273 conn_type_to_string(conn->type), (int)conn->s, conn->address,
274 smartlist_len(connection_array));
276 return 0;
279 /** Tell libevent that we don't care about <b>conn</b> any more. */
280 void
281 connection_unregister_events(connection_t *conn)
283 if (conn->read_event) {
284 if (event_del(conn->read_event))
285 log_warn(LD_BUG, "Error removing read event for %d", (int)conn->s);
286 tor_free(conn->read_event);
288 if (conn->write_event) {
289 if (event_del(conn->write_event))
290 log_warn(LD_BUG, "Error removing write event for %d", (int)conn->s);
291 tor_free(conn->write_event);
293 if (conn->type == CONN_TYPE_AP_DNS_LISTENER) {
294 dnsserv_close_listener(conn);
298 /** Remove the connection from the global list, and remove the
299 * corresponding poll entry. Calling this function will shift the last
300 * connection (if any) into the position occupied by conn.
303 connection_remove(connection_t *conn)
305 int current_index;
306 connection_t *tmp;
308 tor_assert(conn);
310 log_debug(LD_NET,"removing socket %d (type %s), n_conns now %d",
311 (int)conn->s, conn_type_to_string(conn->type),
312 smartlist_len(connection_array));
314 if (conn->type == CONN_TYPE_AP && conn->socket_family == AF_UNIX) {
315 log_info(LD_NET, "Closing SOCKS SocksSocket connection");
318 control_event_conn_bandwidth(conn);
320 tor_assert(conn->conn_array_index >= 0);
321 current_index = conn->conn_array_index;
322 connection_unregister_events(conn); /* This is redundant, but cheap. */
323 if (current_index == smartlist_len(connection_array)-1) { /* at the end */
324 smartlist_del(connection_array, current_index);
325 return 0;
328 /* replace this one with the one at the end */
329 smartlist_del(connection_array, current_index);
330 tmp = smartlist_get(connection_array, current_index);
331 tmp->conn_array_index = current_index;
333 return 0;
336 /** If <b>conn</b> is an edge conn, remove it from the list
337 * of conn's on this circuit. If it's not on an edge,
338 * flush and send destroys for all circuits on this conn.
340 * Remove it from connection_array (if applicable) and
341 * from closeable_connection_list.
343 * Then free it.
345 static void
346 connection_unlink(connection_t *conn)
348 connection_about_to_close_connection(conn);
349 if (conn->conn_array_index >= 0) {
350 connection_remove(conn);
352 if (conn->linked_conn) {
353 conn->linked_conn->linked_conn = NULL;
354 if (! conn->linked_conn->marked_for_close &&
355 conn->linked_conn->reading_from_linked_conn)
356 connection_start_reading(conn->linked_conn);
357 conn->linked_conn = NULL;
359 smartlist_remove(closeable_connection_lst, conn);
360 smartlist_remove(active_linked_connection_lst, conn);
361 if (conn->type == CONN_TYPE_EXIT) {
362 assert_connection_edge_not_dns_pending(TO_EDGE_CONN(conn));
364 if (conn->type == CONN_TYPE_OR) {
365 if (!tor_digest_is_zero(TO_OR_CONN(conn)->identity_digest))
366 connection_or_remove_from_identity_map(TO_OR_CONN(conn));
367 /* connection_unlink() can only get called if the connection
368 * was already on the closeable list, and it got there by
369 * connection_mark_for_close(), which was called from
370 * connection_or_close_normally() or
371 * connection_or_close_for_error(), so the channel should
372 * already be in CHANNEL_STATE_CLOSING, and then the
373 * connection_about_to_close_connection() goes to
374 * connection_or_about_to_close(), which calls channel_closed()
375 * to notify the channel_t layer, and closed the channel, so
376 * nothing more to do here to deal with the channel associated
377 * with an orconn.
380 connection_free(conn);
383 /** Initialize the global connection list, closeable connection list,
384 * and active connection list. */
385 STATIC void
386 init_connection_lists(void)
388 if (!connection_array)
389 connection_array = smartlist_new();
390 if (!closeable_connection_lst)
391 closeable_connection_lst = smartlist_new();
392 if (!active_linked_connection_lst)
393 active_linked_connection_lst = smartlist_new();
396 /** Schedule <b>conn</b> to be closed. **/
397 void
398 add_connection_to_closeable_list(connection_t *conn)
400 tor_assert(!smartlist_contains(closeable_connection_lst, conn));
401 tor_assert(conn->marked_for_close);
402 assert_connection_ok(conn, time(NULL));
403 smartlist_add(closeable_connection_lst, conn);
406 /** Return 1 if conn is on the closeable list, else return 0. */
408 connection_is_on_closeable_list(connection_t *conn)
410 return smartlist_contains(closeable_connection_lst, conn);
413 /** Return true iff conn is in the current poll array. */
415 connection_in_array(connection_t *conn)
417 return smartlist_contains(connection_array, conn);
420 /** Set <b>*array</b> to an array of all connections. <b>*array</b> must not
421 * be modified.
423 MOCK_IMPL(smartlist_t *,
424 get_connection_array, (void))
426 if (!connection_array)
427 connection_array = smartlist_new();
428 return connection_array;
431 /** Provides the traffic read and written over the life of the process. */
433 MOCK_IMPL(uint64_t,
434 get_bytes_read,(void))
436 return stats_n_bytes_read;
439 /* DOCDOC get_bytes_written */
440 MOCK_IMPL(uint64_t,
441 get_bytes_written,(void))
443 return stats_n_bytes_written;
446 /** Set the event mask on <b>conn</b> to <b>events</b>. (The event
447 * mask is a bitmask whose bits are READ_EVENT and WRITE_EVENT)
449 void
450 connection_watch_events(connection_t *conn, watchable_events_t events)
452 if (events & READ_EVENT)
453 connection_start_reading(conn);
454 else
455 connection_stop_reading(conn);
457 if (events & WRITE_EVENT)
458 connection_start_writing(conn);
459 else
460 connection_stop_writing(conn);
463 /** Return true iff <b>conn</b> is listening for read events. */
465 connection_is_reading(connection_t *conn)
467 tor_assert(conn);
469 return conn->reading_from_linked_conn ||
470 (conn->read_event && event_pending(conn->read_event, EV_READ, NULL));
473 /** Check whether <b>conn</b> is correct in having (or not having) a
474 * read/write event (passed in <b>ev</b>). On success, return 0. On failure,
475 * log a warning and return -1. */
476 static int
477 connection_check_event(connection_t *conn, struct event *ev)
479 int bad;
481 if (conn->type == CONN_TYPE_AP && TO_EDGE_CONN(conn)->is_dns_request) {
482 /* DNS requests which we launch through the dnsserv.c module do not have
483 * any underlying socket or any underlying linked connection, so they
484 * shouldn't have any attached events either.
486 bad = ev != NULL;
487 } else {
488 /* Everytyhing else should have an underlying socket, or a linked
489 * connection (which is also tracked with a read_event/write_event pair).
491 bad = ev == NULL;
494 if (bad) {
495 log_warn(LD_BUG, "Event missing on connection %p [%s;%s]. "
496 "socket=%d. linked=%d. "
497 "is_dns_request=%d. Marked_for_close=%s:%d",
498 conn,
499 conn_type_to_string(conn->type),
500 conn_state_to_string(conn->type, conn->state),
501 (int)conn->s, (int)conn->linked,
502 (conn->type == CONN_TYPE_AP &&
503 TO_EDGE_CONN(conn)->is_dns_request),
504 conn->marked_for_close_file ? conn->marked_for_close_file : "-",
505 conn->marked_for_close
507 log_backtrace(LOG_WARN, LD_BUG, "Backtrace attached.");
508 return -1;
510 return 0;
513 /** Tell the main loop to stop notifying <b>conn</b> of any read events. */
514 MOCK_IMPL(void,
515 connection_stop_reading,(connection_t *conn))
517 tor_assert(conn);
519 if (connection_check_event(conn, conn->read_event) < 0) {
520 return;
523 if (conn->linked) {
524 conn->reading_from_linked_conn = 0;
525 connection_stop_reading_from_linked_conn(conn);
526 } else {
527 if (event_del(conn->read_event))
528 log_warn(LD_NET, "Error from libevent setting read event state for %d "
529 "to unwatched: %s",
530 (int)conn->s,
531 tor_socket_strerror(tor_socket_errno(conn->s)));
535 /** Tell the main loop to start notifying <b>conn</b> of any read events. */
536 MOCK_IMPL(void,
537 connection_start_reading,(connection_t *conn))
539 tor_assert(conn);
541 if (connection_check_event(conn, conn->read_event) < 0) {
542 return;
545 if (conn->linked) {
546 conn->reading_from_linked_conn = 1;
547 if (connection_should_read_from_linked_conn(conn))
548 connection_start_reading_from_linked_conn(conn);
549 } else {
550 if (event_add(conn->read_event, NULL))
551 log_warn(LD_NET, "Error from libevent setting read event state for %d "
552 "to watched: %s",
553 (int)conn->s,
554 tor_socket_strerror(tor_socket_errno(conn->s)));
558 /** Return true iff <b>conn</b> is listening for write events. */
560 connection_is_writing(connection_t *conn)
562 tor_assert(conn);
564 return conn->writing_to_linked_conn ||
565 (conn->write_event && event_pending(conn->write_event, EV_WRITE, NULL));
568 /** Tell the main loop to stop notifying <b>conn</b> of any write events. */
569 MOCK_IMPL(void,
570 connection_stop_writing,(connection_t *conn))
572 tor_assert(conn);
574 if (connection_check_event(conn, conn->write_event) < 0) {
575 return;
578 if (conn->linked) {
579 conn->writing_to_linked_conn = 0;
580 if (conn->linked_conn)
581 connection_stop_reading_from_linked_conn(conn->linked_conn);
582 } else {
583 if (event_del(conn->write_event))
584 log_warn(LD_NET, "Error from libevent setting write event state for %d "
585 "to unwatched: %s",
586 (int)conn->s,
587 tor_socket_strerror(tor_socket_errno(conn->s)));
591 /** Tell the main loop to start notifying <b>conn</b> of any write events. */
592 MOCK_IMPL(void,
593 connection_start_writing,(connection_t *conn))
595 tor_assert(conn);
597 if (connection_check_event(conn, conn->write_event) < 0) {
598 return;
601 if (conn->linked) {
602 conn->writing_to_linked_conn = 1;
603 if (conn->linked_conn &&
604 connection_should_read_from_linked_conn(conn->linked_conn))
605 connection_start_reading_from_linked_conn(conn->linked_conn);
606 } else {
607 if (event_add(conn->write_event, NULL))
608 log_warn(LD_NET, "Error from libevent setting write event state for %d "
609 "to watched: %s",
610 (int)conn->s,
611 tor_socket_strerror(tor_socket_errno(conn->s)));
615 /** Return true iff <b>conn</b> is linked conn, and reading from the conn
616 * linked to it would be good and feasible. (Reading is "feasible" if the
617 * other conn exists and has data in its outbuf, and is "good" if we have our
618 * reading_from_linked_conn flag set and the other conn has its
619 * writing_to_linked_conn flag set.)*/
620 static int
621 connection_should_read_from_linked_conn(connection_t *conn)
623 if (conn->linked && conn->reading_from_linked_conn) {
624 if (! conn->linked_conn ||
625 (conn->linked_conn->writing_to_linked_conn &&
626 buf_datalen(conn->linked_conn->outbuf)))
627 return 1;
629 return 0;
632 /** If we called event_base_loop() and told it to never stop until it
633 * runs out of events, now we've changed our mind: tell it we want it to
634 * finish. */
635 void
636 tell_event_loop_to_finish(void)
638 if (!called_loop_once) {
639 struct timeval tv = { 0, 0 };
640 tor_event_base_loopexit(tor_libevent_get_base(), &tv);
641 called_loop_once = 1; /* hack to avoid adding more exit events */
645 /** Helper: Tell the main loop to begin reading bytes into <b>conn</b> from
646 * its linked connection, if it is not doing so already. Called by
647 * connection_start_reading and connection_start_writing as appropriate. */
648 static void
649 connection_start_reading_from_linked_conn(connection_t *conn)
651 tor_assert(conn);
652 tor_assert(conn->linked == 1);
654 if (!conn->active_on_link) {
655 conn->active_on_link = 1;
656 smartlist_add(active_linked_connection_lst, conn);
657 /* make sure that the event_base_loop() function exits at
658 * the end of its run through the current connections, so we can
659 * activate read events for linked connections. */
660 tell_event_loop_to_finish();
661 } else {
662 tor_assert(smartlist_contains(active_linked_connection_lst, conn));
666 /** Tell the main loop to stop reading bytes into <b>conn</b> from its linked
667 * connection, if is currently doing so. Called by connection_stop_reading,
668 * connection_stop_writing, and connection_read. */
669 void
670 connection_stop_reading_from_linked_conn(connection_t *conn)
672 tor_assert(conn);
673 tor_assert(conn->linked == 1);
675 if (conn->active_on_link) {
676 conn->active_on_link = 0;
677 /* FFFF We could keep an index here so we can smartlist_del
678 * cleanly. On the other hand, this doesn't show up on profiles,
679 * so let's leave it alone for now. */
680 smartlist_remove(active_linked_connection_lst, conn);
681 } else {
682 tor_assert(!smartlist_contains(active_linked_connection_lst, conn));
686 /** Close all connections that have been scheduled to get closed. */
687 STATIC void
688 close_closeable_connections(void)
690 int i;
691 for (i = 0; i < smartlist_len(closeable_connection_lst); ) {
692 connection_t *conn = smartlist_get(closeable_connection_lst, i);
693 if (conn->conn_array_index < 0) {
694 connection_unlink(conn); /* blow it away right now */
695 } else {
696 if (!conn_close_if_marked(conn->conn_array_index))
697 ++i;
702 /** Count moribund connections for the OOS handler */
703 MOCK_IMPL(int,
704 connection_count_moribund, (void))
706 int moribund = 0;
709 * Count things we'll try to kill when close_closeable_connections()
710 * runs next.
712 SMARTLIST_FOREACH_BEGIN(closeable_connection_lst, connection_t *, conn) {
713 if (SOCKET_OK(conn->s) && connection_is_moribund(conn)) ++moribund;
714 } SMARTLIST_FOREACH_END(conn);
716 return moribund;
719 /** Libevent callback: this gets invoked when (connection_t*)<b>conn</b> has
720 * some data to read. */
721 static void
722 conn_read_callback(evutil_socket_t fd, short event, void *_conn)
724 connection_t *conn = _conn;
725 (void)fd;
726 (void)event;
728 log_debug(LD_NET,"socket %d wants to read.",(int)conn->s);
730 /* assert_connection_ok(conn, time(NULL)); */
732 if (connection_handle_read(conn) < 0) {
733 if (!conn->marked_for_close) {
734 #ifndef _WIN32
735 log_warn(LD_BUG,"Unhandled error on read for %s connection "
736 "(fd %d); removing",
737 conn_type_to_string(conn->type), (int)conn->s);
738 tor_fragile_assert();
739 #endif
740 if (CONN_IS_EDGE(conn))
741 connection_edge_end_errno(TO_EDGE_CONN(conn));
742 connection_mark_for_close(conn);
745 assert_connection_ok(conn, time(NULL));
747 if (smartlist_len(closeable_connection_lst))
748 close_closeable_connections();
751 /** Libevent callback: this gets invoked when (connection_t*)<b>conn</b> has
752 * some data to write. */
753 static void
754 conn_write_callback(evutil_socket_t fd, short events, void *_conn)
756 connection_t *conn = _conn;
757 (void)fd;
758 (void)events;
760 LOG_FN_CONN(conn, (LOG_DEBUG, LD_NET, "socket %d wants to write.",
761 (int)conn->s));
763 /* assert_connection_ok(conn, time(NULL)); */
765 if (connection_handle_write(conn, 0) < 0) {
766 if (!conn->marked_for_close) {
767 /* this connection is broken. remove it. */
768 log_fn(LOG_WARN,LD_BUG,
769 "unhandled error on write for %s connection (fd %d); removing",
770 conn_type_to_string(conn->type), (int)conn->s);
771 tor_fragile_assert();
772 if (CONN_IS_EDGE(conn)) {
773 /* otherwise we cry wolf about duplicate close */
774 edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
775 if (!edge_conn->end_reason)
776 edge_conn->end_reason = END_STREAM_REASON_INTERNAL;
777 edge_conn->edge_has_sent_end = 1;
779 connection_close_immediate(conn); /* So we don't try to flush. */
780 connection_mark_for_close(conn);
783 assert_connection_ok(conn, time(NULL));
785 if (smartlist_len(closeable_connection_lst))
786 close_closeable_connections();
789 /** If the connection at connection_array[i] is marked for close, then:
790 * - If it has data that it wants to flush, try to flush it.
791 * - If it _still_ has data to flush, and conn->hold_open_until_flushed is
792 * true, then leave the connection open and return.
793 * - Otherwise, remove the connection from connection_array and from
794 * all other lists, close it, and free it.
795 * Returns 1 if the connection was closed, 0 otherwise.
797 static int
798 conn_close_if_marked(int i)
800 connection_t *conn;
801 int retval;
802 time_t now;
804 conn = smartlist_get(connection_array, i);
805 if (!conn->marked_for_close)
806 return 0; /* nothing to see here, move along */
807 now = time(NULL);
808 assert_connection_ok(conn, now);
809 /* assert_all_pending_dns_resolves_ok(); */
811 log_debug(LD_NET,"Cleaning up connection (fd "TOR_SOCKET_T_FORMAT").",
812 conn->s);
814 /* If the connection we are about to close was trying to connect to
815 a proxy server and failed, the client won't be able to use that
816 proxy. We should warn the user about this. */
817 if (conn->proxy_state == PROXY_INFANT)
818 log_failed_proxy_connection(conn);
820 if ((SOCKET_OK(conn->s) || conn->linked_conn) &&
821 connection_wants_to_flush(conn)) {
822 /* s == -1 means it's an incomplete edge connection, or that the socket
823 * has already been closed as unflushable. */
824 ssize_t sz = connection_bucket_write_limit(conn, now);
825 if (!conn->hold_open_until_flushed)
826 log_info(LD_NET,
827 "Conn (addr %s, fd %d, type %s, state %d) marked, but wants "
828 "to flush %d bytes. (Marked at %s:%d)",
829 escaped_safe_str_client(conn->address),
830 (int)conn->s, conn_type_to_string(conn->type), conn->state,
831 (int)conn->outbuf_flushlen,
832 conn->marked_for_close_file, conn->marked_for_close);
833 if (conn->linked_conn) {
834 retval = move_buf_to_buf(conn->linked_conn->inbuf, conn->outbuf,
835 &conn->outbuf_flushlen);
836 if (retval >= 0) {
837 /* The linked conn will notice that it has data when it notices that
838 * we're gone. */
839 connection_start_reading_from_linked_conn(conn->linked_conn);
841 log_debug(LD_GENERAL, "Flushed last %d bytes from a linked conn; "
842 "%d left; flushlen %d; wants-to-flush==%d", retval,
843 (int)connection_get_outbuf_len(conn),
844 (int)conn->outbuf_flushlen,
845 connection_wants_to_flush(conn));
846 } else if (connection_speaks_cells(conn)) {
847 if (conn->state == OR_CONN_STATE_OPEN) {
848 retval = flush_buf_tls(TO_OR_CONN(conn)->tls, conn->outbuf, sz,
849 &conn->outbuf_flushlen);
850 } else
851 retval = -1; /* never flush non-open broken tls connections */
852 } else {
853 retval = flush_buf(conn->s, conn->outbuf, sz, &conn->outbuf_flushlen);
855 if (retval >= 0 && /* Technically, we could survive things like
856 TLS_WANT_WRITE here. But don't bother for now. */
857 conn->hold_open_until_flushed && connection_wants_to_flush(conn)) {
858 if (retval > 0) {
859 LOG_FN_CONN(conn, (LOG_INFO,LD_NET,
860 "Holding conn (fd %d) open for more flushing.",
861 (int)conn->s));
862 conn->timestamp_lastwritten = now; /* reset so we can flush more */
863 } else if (sz == 0) {
864 /* Also, retval==0. If we get here, we didn't want to write anything
865 * (because of rate-limiting) and we didn't. */
867 /* Connection must flush before closing, but it's being rate-limited.
868 * Let's remove from Libevent, and mark it as blocked on bandwidth
869 * so it will be re-added on next token bucket refill. Prevents
870 * busy Libevent loops where we keep ending up here and returning
871 * 0 until we are no longer blocked on bandwidth.
873 if (connection_is_writing(conn)) {
874 conn->write_blocked_on_bw = 1;
875 connection_stop_writing(conn);
877 if (connection_is_reading(conn)) {
878 /* XXXX+ We should make this code unreachable; if a connection is
879 * marked for close and flushing, there is no point in reading to it
880 * at all. Further, checking at this point is a bit of a hack: it
881 * would make much more sense to react in
882 * connection_handle_read_impl, or to just stop reading in
883 * mark_and_flush */
884 conn->read_blocked_on_bw = 1;
885 connection_stop_reading(conn);
888 return 0;
890 if (connection_wants_to_flush(conn)) {
891 log_fn(LOG_INFO, LD_NET, "We stalled too much while trying to write %d "
892 "bytes to address %s. If this happens a lot, either "
893 "something is wrong with your network connection, or "
894 "something is wrong with theirs. "
895 "(fd %d, type %s, state %d, marked at %s:%d).",
896 (int)connection_get_outbuf_len(conn),
897 escaped_safe_str_client(conn->address),
898 (int)conn->s, conn_type_to_string(conn->type), conn->state,
899 conn->marked_for_close_file,
900 conn->marked_for_close);
904 connection_unlink(conn); /* unlink, remove, free */
905 return 1;
908 /** Implementation for directory_all_unreachable. This is done in a callback,
909 * since otherwise it would complicate Tor's control-flow graph beyond all
910 * reason.
912 static void
913 directory_all_unreachable_cb(evutil_socket_t fd, short event, void *arg)
915 (void)fd;
916 (void)event;
917 (void)arg;
919 connection_t *conn;
921 while ((conn = connection_get_by_type_state(CONN_TYPE_AP,
922 AP_CONN_STATE_CIRCUIT_WAIT))) {
923 entry_connection_t *entry_conn = TO_ENTRY_CONN(conn);
924 log_notice(LD_NET,
925 "Is your network connection down? "
926 "Failing connection to '%s:%d'.",
927 safe_str_client(entry_conn->socks_request->address),
928 entry_conn->socks_request->port);
929 connection_mark_unattached_ap(entry_conn,
930 END_STREAM_REASON_NET_UNREACHABLE);
932 control_event_general_error("DIR_ALL_UNREACHABLE");
935 static struct event *directory_all_unreachable_cb_event = NULL;
937 /** We've just tried every dirserver we know about, and none of
938 * them were reachable. Assume the network is down. Change state
939 * so next time an application connection arrives we'll delay it
940 * and try another directory fetch. Kill off all the circuit_wait
941 * streams that are waiting now, since they will all timeout anyway.
943 void
944 directory_all_unreachable(time_t now)
946 (void)now;
948 stats_n_seconds_working=0; /* reset it */
950 if (!directory_all_unreachable_cb_event) {
951 directory_all_unreachable_cb_event =
952 tor_event_new(tor_libevent_get_base(),
953 -1, EV_READ, directory_all_unreachable_cb, NULL);
954 tor_assert(directory_all_unreachable_cb_event);
957 event_active(directory_all_unreachable_cb_event, EV_READ, 1);
960 /** This function is called whenever we successfully pull down some new
961 * network statuses or server descriptors. */
962 void
963 directory_info_has_arrived(time_t now, int from_cache, int suppress_logs)
965 const or_options_t *options = get_options();
967 if (!router_have_minimum_dir_info()) {
968 int quiet = suppress_logs || from_cache ||
969 directory_too_idle_to_fetch_descriptors(options, now);
970 tor_log(quiet ? LOG_INFO : LOG_NOTICE, LD_DIR,
971 "I learned some more directory information, but not enough to "
972 "build a circuit: %s", get_dir_info_status_string());
973 update_all_descriptor_downloads(now);
974 return;
975 } else {
976 if (directory_fetches_from_authorities(options)) {
977 update_all_descriptor_downloads(now);
980 /* if we have enough dir info, then update our guard status with
981 * whatever we just learned. */
982 int invalidate_circs = guards_update_all();
983 // This shouldn't be able to occur at this point.
984 tor_assert_nonfatal(! invalidate_circs);
986 /* Don't even bother trying to get extrainfo until the rest of our
987 * directory info is up-to-date */
988 if (options->DownloadExtraInfo)
989 update_extrainfo_downloads(now);
992 if (server_mode(options) && !net_is_disabled() && !from_cache &&
993 (have_completed_a_circuit() || !any_predicted_circuits(now)))
994 consider_testing_reachability(1, 1);
997 /** Perform regular maintenance tasks for a single connection. This
998 * function gets run once per second per connection by run_scheduled_events.
1000 static void
1001 run_connection_housekeeping(int i, time_t now)
1003 cell_t cell;
1004 connection_t *conn = smartlist_get(connection_array, i);
1005 const or_options_t *options = get_options();
1006 or_connection_t *or_conn;
1007 channel_t *chan = NULL;
1008 int have_any_circuits;
1009 int past_keepalive =
1010 now >= conn->timestamp_lastwritten + options->KeepalivePeriod;
1012 if (conn->outbuf && !connection_get_outbuf_len(conn) &&
1013 conn->type == CONN_TYPE_OR)
1014 TO_OR_CONN(conn)->timestamp_lastempty = now;
1016 if (conn->marked_for_close) {
1017 /* nothing to do here */
1018 return;
1021 /* Expire any directory connections that haven't been active (sent
1022 * if a server or received if a client) for 5 min */
1023 if (conn->type == CONN_TYPE_DIR &&
1024 ((DIR_CONN_IS_SERVER(conn) &&
1025 conn->timestamp_lastwritten
1026 + options->TestingDirConnectionMaxStall < now) ||
1027 (!DIR_CONN_IS_SERVER(conn) &&
1028 conn->timestamp_lastread
1029 + options->TestingDirConnectionMaxStall < now))) {
1030 log_info(LD_DIR,"Expiring wedged directory conn (fd %d, purpose %d)",
1031 (int)conn->s, conn->purpose);
1032 /* This check is temporary; it's to let us know whether we should consider
1033 * parsing partial serverdesc responses. */
1034 if (conn->purpose == DIR_PURPOSE_FETCH_SERVERDESC &&
1035 connection_get_inbuf_len(conn) >= 1024) {
1036 log_info(LD_DIR,"Trying to extract information from wedged server desc "
1037 "download.");
1038 connection_dir_reached_eof(TO_DIR_CONN(conn));
1039 } else {
1040 connection_mark_for_close(conn);
1042 return;
1045 if (!connection_speaks_cells(conn))
1046 return; /* we're all done here, the rest is just for OR conns */
1048 /* If we haven't written to an OR connection for a while, then either nuke
1049 the connection or send a keepalive, depending. */
1051 or_conn = TO_OR_CONN(conn);
1052 tor_assert(conn->outbuf);
1054 chan = TLS_CHAN_TO_BASE(or_conn->chan);
1055 tor_assert(chan);
1057 if (channel_num_circuits(chan) != 0) {
1058 have_any_circuits = 1;
1059 chan->timestamp_last_had_circuits = now;
1060 } else {
1061 have_any_circuits = 0;
1064 if (channel_is_bad_for_new_circs(TLS_CHAN_TO_BASE(or_conn->chan)) &&
1065 ! have_any_circuits) {
1066 /* It's bad for new circuits, and has no unmarked circuits on it:
1067 * mark it now. */
1068 log_info(LD_OR,
1069 "Expiring non-used OR connection to fd %d (%s:%d) [Too old].",
1070 (int)conn->s, conn->address, conn->port);
1071 if (conn->state == OR_CONN_STATE_CONNECTING)
1072 connection_or_connect_failed(TO_OR_CONN(conn),
1073 END_OR_CONN_REASON_TIMEOUT,
1074 "Tor gave up on the connection");
1075 connection_or_close_normally(TO_OR_CONN(conn), 1);
1076 } else if (!connection_state_is_open(conn)) {
1077 if (past_keepalive) {
1078 /* We never managed to actually get this connection open and happy. */
1079 log_info(LD_OR,"Expiring non-open OR connection to fd %d (%s:%d).",
1080 (int)conn->s,conn->address, conn->port);
1081 connection_or_close_normally(TO_OR_CONN(conn), 0);
1083 } else if (we_are_hibernating() &&
1084 ! have_any_circuits &&
1085 !connection_get_outbuf_len(conn)) {
1086 /* We're hibernating, there's no circuits, and nothing to flush.*/
1087 log_info(LD_OR,"Expiring non-used OR connection to fd %d (%s:%d) "
1088 "[Hibernating or exiting].",
1089 (int)conn->s,conn->address, conn->port);
1090 connection_or_close_normally(TO_OR_CONN(conn), 1);
1091 } else if (!have_any_circuits &&
1092 now - or_conn->idle_timeout >=
1093 chan->timestamp_last_had_circuits) {
1094 log_info(LD_OR,"Expiring non-used OR connection to fd %d (%s:%d) "
1095 "[no circuits for %d; timeout %d; %scanonical].",
1096 (int)conn->s, conn->address, conn->port,
1097 (int)(now - chan->timestamp_last_had_circuits),
1098 or_conn->idle_timeout,
1099 or_conn->is_canonical ? "" : "non");
1100 connection_or_close_normally(TO_OR_CONN(conn), 0);
1101 } else if (
1102 now >= or_conn->timestamp_lastempty + options->KeepalivePeriod*10 &&
1103 now >= conn->timestamp_lastwritten + options->KeepalivePeriod*10) {
1104 log_fn(LOG_PROTOCOL_WARN,LD_PROTOCOL,
1105 "Expiring stuck OR connection to fd %d (%s:%d). (%d bytes to "
1106 "flush; %d seconds since last write)",
1107 (int)conn->s, conn->address, conn->port,
1108 (int)connection_get_outbuf_len(conn),
1109 (int)(now-conn->timestamp_lastwritten));
1110 connection_or_close_normally(TO_OR_CONN(conn), 0);
1111 } else if (past_keepalive && !connection_get_outbuf_len(conn)) {
1112 /* send a padding cell */
1113 log_fn(LOG_DEBUG,LD_OR,"Sending keepalive to (%s:%d)",
1114 conn->address, conn->port);
1115 memset(&cell,0,sizeof(cell_t));
1116 cell.command = CELL_PADDING;
1117 connection_or_write_cell_to_buf(&cell, or_conn);
1121 /** Honor a NEWNYM request: make future requests unlinkable to past
1122 * requests. */
1123 static void
1124 signewnym_impl(time_t now)
1126 const or_options_t *options = get_options();
1127 if (!proxy_mode(options)) {
1128 log_info(LD_CONTROL, "Ignoring SIGNAL NEWNYM because client functionality "
1129 "is disabled.");
1130 return;
1133 circuit_mark_all_dirty_circs_as_unusable();
1134 addressmap_clear_transient();
1135 rend_client_purge_state();
1136 time_of_last_signewnym = now;
1137 signewnym_is_pending = 0;
1139 ++newnym_epoch;
1141 control_event_signal(SIGNEWNYM);
1144 /** Return the number of times that signewnym has been called. */
1145 unsigned
1146 get_signewnym_epoch(void)
1148 return newnym_epoch;
1151 /** True iff we have initialized all the members of <b>periodic_events</b>.
1152 * Used to prevent double-initialization. */
1153 static int periodic_events_initialized = 0;
1155 /* Declare all the timer callback functions... */
1156 #undef CALLBACK
1157 #define CALLBACK(name) \
1158 static int name ## _callback(time_t, const or_options_t *)
1159 CALLBACK(rotate_onion_key);
1160 CALLBACK(check_ed_keys);
1161 CALLBACK(launch_descriptor_fetches);
1162 CALLBACK(rotate_x509_certificate);
1163 CALLBACK(add_entropy);
1164 CALLBACK(launch_reachability_tests);
1165 CALLBACK(downrate_stability);
1166 CALLBACK(save_stability);
1167 CALLBACK(check_authority_cert);
1168 CALLBACK(check_expired_networkstatus);
1169 CALLBACK(write_stats_file);
1170 CALLBACK(record_bridge_stats);
1171 CALLBACK(clean_caches);
1172 CALLBACK(rend_cache_failure_clean);
1173 CALLBACK(retry_dns);
1174 CALLBACK(check_descriptor);
1175 CALLBACK(check_for_reachability_bw);
1176 CALLBACK(fetch_networkstatus);
1177 CALLBACK(retry_listeners);
1178 CALLBACK(expire_old_ciruits_serverside);
1179 CALLBACK(check_dns_honesty);
1180 CALLBACK(write_bridge_ns);
1181 CALLBACK(check_fw_helper_app);
1182 CALLBACK(heartbeat);
1184 #undef CALLBACK
1186 /* Now we declare an array of periodic_event_item_t for each periodic event */
1187 #define CALLBACK(name) PERIODIC_EVENT(name)
1189 static periodic_event_item_t periodic_events[] = {
1190 CALLBACK(rotate_onion_key),
1191 CALLBACK(check_ed_keys),
1192 CALLBACK(launch_descriptor_fetches),
1193 CALLBACK(rotate_x509_certificate),
1194 CALLBACK(add_entropy),
1195 CALLBACK(launch_reachability_tests),
1196 CALLBACK(downrate_stability),
1197 CALLBACK(save_stability),
1198 CALLBACK(check_authority_cert),
1199 CALLBACK(check_expired_networkstatus),
1200 CALLBACK(write_stats_file),
1201 CALLBACK(record_bridge_stats),
1202 CALLBACK(clean_caches),
1203 CALLBACK(rend_cache_failure_clean),
1204 CALLBACK(retry_dns),
1205 CALLBACK(check_descriptor),
1206 CALLBACK(check_for_reachability_bw),
1207 CALLBACK(fetch_networkstatus),
1208 CALLBACK(retry_listeners),
1209 CALLBACK(expire_old_ciruits_serverside),
1210 CALLBACK(check_dns_honesty),
1211 CALLBACK(write_bridge_ns),
1212 CALLBACK(check_fw_helper_app),
1213 CALLBACK(heartbeat),
1214 END_OF_PERIODIC_EVENTS
1216 #undef CALLBACK
1218 /* These are pointers to members of periodic_events[] that are used to
1219 * implement particular callbacks. We keep them separate here so that we
1220 * can access them by name. We also keep them inside periodic_events[]
1221 * so that we can implement "reset all timers" in a reasonable way. */
1222 static periodic_event_item_t *check_descriptor_event=NULL;
1223 static periodic_event_item_t *fetch_networkstatus_event=NULL;
1224 static periodic_event_item_t *launch_descriptor_fetches_event=NULL;
1225 static periodic_event_item_t *check_dns_honesty_event=NULL;
1227 /** Reset all the periodic events so we'll do all our actions again as if we
1228 * just started up.
1229 * Useful if our clock just moved back a long time from the future,
1230 * so we don't wait until that future arrives again before acting.
1232 void
1233 reset_all_main_loop_timers(void)
1235 int i;
1236 for (i = 0; periodic_events[i].name; ++i) {
1237 periodic_event_reschedule(&periodic_events[i]);
1241 /** Return the member of periodic_events[] whose name is <b>name</b>.
1242 * Return NULL if no such event is found.
1244 static periodic_event_item_t *
1245 find_periodic_event(const char *name)
1247 int i;
1248 for (i = 0; periodic_events[i].name; ++i) {
1249 if (strcmp(name, periodic_events[i].name) == 0)
1250 return &periodic_events[i];
1252 return NULL;
1255 /** Helper, run one second after setup:
1256 * Initializes all members of periodic_events and starts them running.
1258 * (We do this one second after setup for backward-compatibility reasons;
1259 * it might not actually be necessary.) */
1260 static void
1261 initialize_periodic_events_cb(evutil_socket_t fd, short events, void *data)
1263 (void) fd;
1264 (void) events;
1265 (void) data;
1266 int i;
1267 for (i = 0; periodic_events[i].name; ++i) {
1268 periodic_event_launch(&periodic_events[i]);
1272 /** Set up all the members of periodic_events[], and configure them all to be
1273 * launched from a callback. */
1274 STATIC void
1275 initialize_periodic_events(void)
1277 tor_assert(periodic_events_initialized == 0);
1278 periodic_events_initialized = 1;
1280 int i;
1281 for (i = 0; periodic_events[i].name; ++i) {
1282 periodic_event_setup(&periodic_events[i]);
1285 #define NAMED_CALLBACK(name) \
1286 STMT_BEGIN name ## _event = find_periodic_event( #name ); STMT_END
1288 NAMED_CALLBACK(check_descriptor);
1289 NAMED_CALLBACK(fetch_networkstatus);
1290 NAMED_CALLBACK(launch_descriptor_fetches);
1291 NAMED_CALLBACK(check_dns_honesty);
1293 struct timeval one_second = { 1, 0 };
1294 event_base_once(tor_libevent_get_base(), -1, EV_TIMEOUT,
1295 initialize_periodic_events_cb, NULL,
1296 &one_second);
1299 STATIC void
1300 teardown_periodic_events(void)
1302 int i;
1303 for (i = 0; periodic_events[i].name; ++i) {
1304 periodic_event_destroy(&periodic_events[i]);
1309 * Update our schedule so that we'll check whether we need to update our
1310 * descriptor immediately, rather than after up to CHECK_DESCRIPTOR_INTERVAL
1311 * seconds.
1313 void
1314 reschedule_descriptor_update_check(void)
1316 tor_assert(check_descriptor_event);
1317 periodic_event_reschedule(check_descriptor_event);
1321 * Update our schedule so that we'll check whether we need to fetch directory
1322 * info immediately.
1324 void
1325 reschedule_directory_downloads(void)
1327 tor_assert(fetch_networkstatus_event);
1328 tor_assert(launch_descriptor_fetches_event);
1330 periodic_event_reschedule(fetch_networkstatus_event);
1331 periodic_event_reschedule(launch_descriptor_fetches_event);
1334 #define LONGEST_TIMER_PERIOD (30 * 86400)
1335 /** Helper: Return the number of seconds between <b>now</b> and <b>next</b>,
1336 * clipped to the range [1 second, LONGEST_TIMER_PERIOD]. */
1337 static inline int
1338 safe_timer_diff(time_t now, time_t next)
1340 if (next > now) {
1341 /* There were no computers at signed TIME_MIN (1902 on 32-bit systems),
1342 * and nothing that could run Tor. It's a bug if 'next' is around then.
1343 * On 64-bit systems with signed TIME_MIN, TIME_MIN is before the Big
1344 * Bang. We cannot extrapolate past a singularity, but there was probably
1345 * nothing that could run Tor then, either.
1347 tor_assert(next > TIME_MIN + LONGEST_TIMER_PERIOD);
1349 if (next - LONGEST_TIMER_PERIOD > now)
1350 return LONGEST_TIMER_PERIOD;
1351 return (int)(next - now);
1352 } else {
1353 return 1;
1357 /** Perform regular maintenance tasks. This function gets run once per
1358 * second by second_elapsed_callback().
1360 static void
1361 run_scheduled_events(time_t now)
1363 const or_options_t *options = get_options();
1365 /* 0. See if we've been asked to shut down and our timeout has
1366 * expired; or if our bandwidth limits are exhausted and we
1367 * should hibernate; or if it's time to wake up from hibernation.
1369 consider_hibernation(now);
1371 /* 0b. If we've deferred a signewnym, make sure it gets handled
1372 * eventually. */
1373 if (signewnym_is_pending &&
1374 time_of_last_signewnym + MAX_SIGNEWNYM_RATE <= now) {
1375 log_info(LD_CONTROL, "Honoring delayed NEWNYM request");
1376 signewnym_impl(now);
1379 /* 0c. If we've deferred log messages for the controller, handle them now */
1380 flush_pending_log_callbacks();
1382 /* Maybe enough time elapsed for us to reconsider a circuit. */
1383 circuit_upgrade_circuits_from_guard_wait();
1385 if (options->UseBridges && !options->DisableNetwork) {
1386 fetch_bridge_descriptors(options, now);
1389 if (accounting_is_enabled(options)) {
1390 accounting_run_housekeeping(now);
1393 if (authdir_mode_v3(options)) {
1394 dirvote_act(options, now);
1397 /* 3a. Every second, we examine pending circuits and prune the
1398 * ones which have been pending for more than a few seconds.
1399 * We do this before step 4, so it can try building more if
1400 * it's not comfortable with the number of available circuits.
1402 /* (If our circuit build timeout can ever become lower than a second (which
1403 * it can't, currently), we should do this more often.) */
1404 circuit_expire_building();
1406 /* 3b. Also look at pending streams and prune the ones that 'began'
1407 * a long time ago but haven't gotten a 'connected' yet.
1408 * Do this before step 4, so we can put them back into pending
1409 * state to be picked up by the new circuit.
1411 connection_ap_expire_beginning();
1413 /* 3c. And expire connections that we've held open for too long.
1415 connection_expire_held_open();
1417 /* 4. Every second, we try a new circuit if there are no valid
1418 * circuits. Every NewCircuitPeriod seconds, we expire circuits
1419 * that became dirty more than MaxCircuitDirtiness seconds ago,
1420 * and we make a new circ if there are no clean circuits.
1422 const int have_dir_info = router_have_minimum_dir_info();
1423 if (have_dir_info && !net_is_disabled()) {
1424 circuit_build_needed_circs(now);
1425 } else {
1426 circuit_expire_old_circs_as_needed(now);
1429 if (!net_is_disabled()) {
1430 /* This is usually redundant with circuit_build_needed_circs() above,
1431 * but it is very fast when there is no work to do. */
1432 connection_ap_attach_pending(0);
1435 /* 5. We do housekeeping for each connection... */
1436 connection_or_set_bad_connections(NULL, 0);
1437 int i;
1438 for (i=0;i<smartlist_len(connection_array);i++) {
1439 run_connection_housekeeping(i, now);
1442 /* 6. And remove any marked circuits... */
1443 circuit_close_all_marked();
1445 /* 7. And upload service descriptors if necessary. */
1446 if (have_completed_a_circuit() && !net_is_disabled()) {
1447 rend_consider_services_upload(now);
1448 rend_consider_descriptor_republication();
1451 /* 8. and blow away any connections that need to die. have to do this now,
1452 * because if we marked a conn for close and left its socket -1, then
1453 * we'll pass it to poll/select and bad things will happen.
1455 close_closeable_connections();
1457 /* 8b. And if anything in our state is ready to get flushed to disk, we
1458 * flush it. */
1459 or_state_save(now);
1461 /* 8c. Do channel cleanup just like for connections */
1462 channel_run_cleanup();
1463 channel_listener_run_cleanup();
1465 /* 11b. check pending unconfigured managed proxies */
1466 if (!net_is_disabled() && pt_proxies_configuration_pending())
1467 pt_configure_remaining_proxies();
1470 /* Periodic callback: Every MIN_ONION_KEY_LIFETIME seconds, rotate the onion
1471 * keys, shut down and restart all cpuworkers, and update our descriptor if
1472 * necessary.
1474 static int
1475 rotate_onion_key_callback(time_t now, const or_options_t *options)
1477 if (server_mode(options)) {
1478 time_t rotation_time = get_onion_key_set_at()+MIN_ONION_KEY_LIFETIME;
1479 if (rotation_time > now) {
1480 return safe_timer_diff(now, rotation_time);
1483 log_info(LD_GENERAL,"Rotating onion key.");
1484 rotate_onion_key();
1485 cpuworkers_rotate_keyinfo();
1486 if (router_rebuild_descriptor(1)<0) {
1487 log_info(LD_CONFIG, "Couldn't rebuild router descriptor");
1489 if (advertised_server_mode() && !options->DisableNetwork)
1490 router_upload_dir_desc_to_dirservers(0);
1491 return MIN_ONION_KEY_LIFETIME;
1493 return PERIODIC_EVENT_NO_UPDATE;
1496 /* Periodic callback: Every 30 seconds, check whether it's time to make new
1497 * Ed25519 subkeys.
1499 static int
1500 check_ed_keys_callback(time_t now, const or_options_t *options)
1502 if (server_mode(options)) {
1503 if (should_make_new_ed_keys(options, now)) {
1504 if (load_ed_keys(options, now) < 0 ||
1505 generate_ed_link_cert(options, now)) {
1506 log_err(LD_OR, "Unable to update Ed25519 keys! Exiting.");
1507 tor_cleanup();
1508 exit(0);
1511 return 30;
1513 return PERIODIC_EVENT_NO_UPDATE;
1517 * Periodic callback: Every {LAZY,GREEDY}_DESCRIPTOR_RETRY_INTERVAL,
1518 * see about fetching descriptors, microdescriptors, and extrainfo
1519 * documents.
1521 static int
1522 launch_descriptor_fetches_callback(time_t now, const or_options_t *options)
1524 if (should_delay_dir_fetches(options, NULL))
1525 return PERIODIC_EVENT_NO_UPDATE;
1527 update_all_descriptor_downloads(now);
1528 update_extrainfo_downloads(now);
1529 if (router_have_minimum_dir_info())
1530 return LAZY_DESCRIPTOR_RETRY_INTERVAL;
1531 else
1532 return GREEDY_DESCRIPTOR_RETRY_INTERVAL;
1536 * Periodic event: Rotate our X.509 certificates and TLS keys once every
1537 * MAX_SSL_KEY_LIFETIME_INTERNAL.
1539 static int
1540 rotate_x509_certificate_callback(time_t now, const or_options_t *options)
1542 static int first = 1;
1543 (void)now;
1544 (void)options;
1545 if (first) {
1546 first = 0;
1547 return MAX_SSL_KEY_LIFETIME_INTERNAL;
1550 /* 1b. Every MAX_SSL_KEY_LIFETIME_INTERNAL seconds, we change our
1551 * TLS context. */
1552 log_info(LD_GENERAL,"Rotating tls context.");
1553 if (router_initialize_tls_context() < 0) {
1554 log_err(LD_BUG, "Error reinitializing TLS context");
1555 tor_assert_unreached();
1558 /* We also make sure to rotate the TLS connections themselves if they've
1559 * been up for too long -- but that's done via is_bad_for_new_circs in
1560 * run_connection_housekeeping() above. */
1561 return MAX_SSL_KEY_LIFETIME_INTERNAL;
1565 * Periodic callback: once an hour, grab some more entropy from the
1566 * kernel and feed it to our CSPRNG.
1568 static int
1569 add_entropy_callback(time_t now, const or_options_t *options)
1571 (void)now;
1572 (void)options;
1573 /* We already seeded once, so don't die on failure. */
1574 if (crypto_seed_rng() < 0) {
1575 log_warn(LD_GENERAL, "Tried to re-seed RNG, but failed. We already "
1576 "seeded once, though, so we won't exit here.");
1579 /** How often do we add more entropy to OpenSSL's RNG pool? */
1580 #define ENTROPY_INTERVAL (60*60)
1581 return ENTROPY_INTERVAL;
1585 * Periodic callback: if we're an authority, make sure we test
1586 * the routers on the network for reachability.
1588 static int
1589 launch_reachability_tests_callback(time_t now, const or_options_t *options)
1591 if (authdir_mode_tests_reachability(options) &&
1592 !net_is_disabled()) {
1593 /* try to determine reachability of the other Tor relays */
1594 dirserv_test_reachability(now);
1596 return REACHABILITY_TEST_INTERVAL;
1600 * Periodic callback: if we're an authority, discount the stability
1601 * information (and other rephist information) that's older.
1603 static int
1604 downrate_stability_callback(time_t now, const or_options_t *options)
1606 (void)options;
1607 /* 1d. Periodically, we discount older stability information so that new
1608 * stability info counts more, and save the stability information to disk as
1609 * appropriate. */
1610 time_t next = rep_hist_downrate_old_runs(now);
1611 return safe_timer_diff(now, next);
1615 * Periodic callback: if we're an authority, record our measured stability
1616 * information from rephist in an mtbf file.
1618 static int
1619 save_stability_callback(time_t now, const or_options_t *options)
1621 if (authdir_mode_tests_reachability(options)) {
1622 if (rep_hist_record_mtbf_data(now, 1)<0) {
1623 log_warn(LD_GENERAL, "Couldn't store mtbf data.");
1626 #define SAVE_STABILITY_INTERVAL (30*60)
1627 return SAVE_STABILITY_INTERVAL;
1631 * Periodic callback: if we're an authority, check on our authority
1632 * certificate (the one that authenticates our authority signing key).
1634 static int
1635 check_authority_cert_callback(time_t now, const or_options_t *options)
1637 (void)now;
1638 (void)options;
1639 /* 1e. Periodically, if we're a v3 authority, we check whether our cert is
1640 * close to expiring and warn the admin if it is. */
1641 v3_authority_check_key_expiry();
1642 #define CHECK_V3_CERTIFICATE_INTERVAL (5*60)
1643 return CHECK_V3_CERTIFICATE_INTERVAL;
1647 * Periodic callback: If our consensus is too old, recalculate whether
1648 * we can actually use it.
1650 static int
1651 check_expired_networkstatus_callback(time_t now, const or_options_t *options)
1653 (void)options;
1654 /* 1f. Check whether our networkstatus has expired.
1656 networkstatus_t *ns = networkstatus_get_latest_consensus();
1657 /*XXXX RD: This value needs to be the same as REASONABLY_LIVE_TIME in
1658 * networkstatus_get_reasonably_live_consensus(), but that value is way
1659 * way too high. Arma: is the bridge issue there resolved yet? -NM */
1660 #define NS_EXPIRY_SLOP (24*60*60)
1661 if (ns && ns->valid_until < now+NS_EXPIRY_SLOP &&
1662 router_have_minimum_dir_info()) {
1663 router_dir_info_changed();
1665 #define CHECK_EXPIRED_NS_INTERVAL (2*60)
1666 return CHECK_EXPIRED_NS_INTERVAL;
1670 * Periodic callback: Write statistics to disk if appropriate.
1672 static int
1673 write_stats_file_callback(time_t now, const or_options_t *options)
1675 /* 1g. Check whether we should write statistics to disk.
1677 #define CHECK_WRITE_STATS_INTERVAL (60*60)
1678 time_t next_time_to_write_stats_files = now + CHECK_WRITE_STATS_INTERVAL;
1679 if (options->CellStatistics) {
1680 time_t next_write =
1681 rep_hist_buffer_stats_write(now);
1682 if (next_write && next_write < next_time_to_write_stats_files)
1683 next_time_to_write_stats_files = next_write;
1685 if (options->DirReqStatistics) {
1686 time_t next_write = geoip_dirreq_stats_write(now);
1687 if (next_write && next_write < next_time_to_write_stats_files)
1688 next_time_to_write_stats_files = next_write;
1690 if (options->EntryStatistics) {
1691 time_t next_write = geoip_entry_stats_write(now);
1692 if (next_write && next_write < next_time_to_write_stats_files)
1693 next_time_to_write_stats_files = next_write;
1695 if (options->HiddenServiceStatistics) {
1696 time_t next_write = rep_hist_hs_stats_write(now);
1697 if (next_write && next_write < next_time_to_write_stats_files)
1698 next_time_to_write_stats_files = next_write;
1700 if (options->ExitPortStatistics) {
1701 time_t next_write = rep_hist_exit_stats_write(now);
1702 if (next_write && next_write < next_time_to_write_stats_files)
1703 next_time_to_write_stats_files = next_write;
1705 if (options->ConnDirectionStatistics) {
1706 time_t next_write = rep_hist_conn_stats_write(now);
1707 if (next_write && next_write < next_time_to_write_stats_files)
1708 next_time_to_write_stats_files = next_write;
1710 if (options->BridgeAuthoritativeDir) {
1711 time_t next_write = rep_hist_desc_stats_write(now);
1712 if (next_write && next_write < next_time_to_write_stats_files)
1713 next_time_to_write_stats_files = next_write;
1716 return safe_timer_diff(now, next_time_to_write_stats_files);
1720 * Periodic callback: Write bridge statistics to disk if appropriate.
1722 static int
1723 record_bridge_stats_callback(time_t now, const or_options_t *options)
1725 static int should_init_bridge_stats = 1;
1727 /* 1h. Check whether we should write bridge statistics to disk.
1729 if (should_record_bridge_info(options)) {
1730 if (should_init_bridge_stats) {
1731 /* (Re-)initialize bridge statistics. */
1732 geoip_bridge_stats_init(now);
1733 should_init_bridge_stats = 0;
1734 return WRITE_STATS_INTERVAL;
1735 } else {
1736 /* Possibly write bridge statistics to disk and ask when to write
1737 * them next time. */
1738 time_t next = geoip_bridge_stats_write(now);
1739 return safe_timer_diff(now, next);
1741 } else if (!should_init_bridge_stats) {
1742 /* Bridge mode was turned off. Ensure that stats are re-initialized
1743 * next time bridge mode is turned on. */
1744 should_init_bridge_stats = 1;
1746 return PERIODIC_EVENT_NO_UPDATE;
1750 * Periodic callback: Clean in-memory caches every once in a while
1752 static int
1753 clean_caches_callback(time_t now, const or_options_t *options)
1755 /* Remove old information from rephist and the rend cache. */
1756 rep_history_clean(now - options->RephistTrackTime);
1757 rend_cache_clean(now, REND_CACHE_TYPE_CLIENT);
1758 rend_cache_clean(now, REND_CACHE_TYPE_SERVICE);
1759 hs_cache_clean_as_dir(now);
1760 microdesc_cache_rebuild(NULL, 0);
1761 #define CLEAN_CACHES_INTERVAL (30*60)
1762 return CLEAN_CACHES_INTERVAL;
1766 * Periodic callback: Clean the cache of failed hidden service lookups
1767 * frequently frequently.
1769 static int
1770 rend_cache_failure_clean_callback(time_t now, const or_options_t *options)
1772 (void)options;
1773 /* We don't keep entries that are more than five minutes old so we try to
1774 * clean it as soon as we can since we want to make sure the client waits
1775 * as little as possible for reachability reasons. */
1776 rend_cache_failure_clean(now);
1777 return 30;
1781 * Periodic callback: If we're a server and initializing dns failed, retry.
1783 static int
1784 retry_dns_callback(time_t now, const or_options_t *options)
1786 (void)now;
1787 #define RETRY_DNS_INTERVAL (10*60)
1788 if (server_mode(options) && has_dns_init_failed())
1789 dns_init();
1790 return RETRY_DNS_INTERVAL;
1793 /** Periodic callback: consider rebuilding or and re-uploading our descriptor
1794 * (if we've passed our internal checks). */
1795 static int
1796 check_descriptor_callback(time_t now, const or_options_t *options)
1798 /** How often do we check whether part of our router info has changed in a
1799 * way that would require an upload? That includes checking whether our IP
1800 * address has changed. */
1801 #define CHECK_DESCRIPTOR_INTERVAL (60)
1803 /* 2b. Once per minute, regenerate and upload the descriptor if the old
1804 * one is inaccurate. */
1805 if (!options->DisableNetwork) {
1806 check_descriptor_bandwidth_changed(now);
1807 check_descriptor_ipaddress_changed(now);
1808 mark_my_descriptor_dirty_if_too_old(now);
1809 consider_publishable_server(0);
1810 /* If any networkstatus documents are no longer recent, we need to
1811 * update all the descriptors' running status. */
1812 /* Remove dead routers. */
1813 /* XXXX This doesn't belong here, but it was here in the pre-
1814 * XXXX refactoring code. */
1815 routerlist_remove_old_routers();
1818 return CHECK_DESCRIPTOR_INTERVAL;
1822 * Periodic callback: check whether we're reachable (as a relay), and
1823 * whether our bandwidth has changed enough that we need to
1824 * publish a new descriptor.
1826 static int
1827 check_for_reachability_bw_callback(time_t now, const or_options_t *options)
1829 /* XXXX This whole thing was stuck in the middle of what is now
1830 * XXXX check_descriptor_callback. I'm not sure it's right. */
1832 static int dirport_reachability_count = 0;
1833 /* also, check religiously for reachability, if it's within the first
1834 * 20 minutes of our uptime. */
1835 if (server_mode(options) &&
1836 (have_completed_a_circuit() || !any_predicted_circuits(now)) &&
1837 !we_are_hibernating()) {
1838 if (stats_n_seconds_working < TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT) {
1839 consider_testing_reachability(1, dirport_reachability_count==0);
1840 if (++dirport_reachability_count > 5)
1841 dirport_reachability_count = 0;
1842 return 1;
1843 } else {
1844 /* If we haven't checked for 12 hours and our bandwidth estimate is
1845 * low, do another bandwidth test. This is especially important for
1846 * bridges, since they might go long periods without much use. */
1847 const routerinfo_t *me = router_get_my_routerinfo();
1848 static int first_time = 1;
1849 if (!first_time && me &&
1850 me->bandwidthcapacity < me->bandwidthrate &&
1851 me->bandwidthcapacity < 51200) {
1852 reset_bandwidth_test();
1854 first_time = 0;
1855 #define BANDWIDTH_RECHECK_INTERVAL (12*60*60)
1856 return BANDWIDTH_RECHECK_INTERVAL;
1859 return CHECK_DESCRIPTOR_INTERVAL;
1863 * Periodic event: once a minute, (or every second if TestingTorNetwork, or
1864 * during client bootstrap), check whether we want to download any
1865 * networkstatus documents. */
1866 static int
1867 fetch_networkstatus_callback(time_t now, const or_options_t *options)
1869 /* How often do we check whether we should download network status
1870 * documents? */
1871 const int we_are_bootstrapping = networkstatus_consensus_is_bootstrapping(
1872 now);
1873 const int prefer_mirrors = !directory_fetches_from_authorities(
1874 get_options());
1875 int networkstatus_dl_check_interval = 60;
1876 /* check more often when testing, or when bootstrapping from mirrors
1877 * (connection limits prevent too many connections being made) */
1878 if (options->TestingTorNetwork
1879 || (we_are_bootstrapping && prefer_mirrors)) {
1880 networkstatus_dl_check_interval = 1;
1883 if (should_delay_dir_fetches(options, NULL))
1884 return PERIODIC_EVENT_NO_UPDATE;
1886 update_networkstatus_downloads(now);
1887 return networkstatus_dl_check_interval;
1891 * Periodic callback: Every 60 seconds, we relaunch listeners if any died. */
1892 static int
1893 retry_listeners_callback(time_t now, const or_options_t *options)
1895 (void)now;
1896 (void)options;
1897 if (!net_is_disabled()) {
1898 retry_all_listeners(NULL, NULL, 0);
1899 return 60;
1901 return PERIODIC_EVENT_NO_UPDATE;
1905 * Periodic callback: as a server, see if we have any old unused circuits
1906 * that should be expired */
1907 static int
1908 expire_old_ciruits_serverside_callback(time_t now, const or_options_t *options)
1910 (void)options;
1911 /* every 11 seconds, so not usually the same second as other such events */
1912 circuit_expire_old_circuits_serverside(now);
1913 return 11;
1917 * Periodic event: if we're an exit, see if our DNS server is telling us
1918 * obvious lies.
1920 static int
1921 check_dns_honesty_callback(time_t now, const or_options_t *options)
1923 (void)now;
1924 /* 9. and if we're an exit node, check whether our DNS is telling stories
1925 * to us. */
1926 if (net_is_disabled() ||
1927 ! public_server_mode(options) ||
1928 router_my_exit_policy_is_reject_star())
1929 return PERIODIC_EVENT_NO_UPDATE;
1931 static int first_time = 1;
1932 if (first_time) {
1933 /* Don't launch right when we start */
1934 first_time = 0;
1935 return crypto_rand_int_range(60, 180);
1938 dns_launch_correctness_checks();
1939 return 12*3600 + crypto_rand_int(12*3600);
1943 * Periodic callback: if we're the bridge authority, write a networkstatus
1944 * file to disk.
1946 static int
1947 write_bridge_ns_callback(time_t now, const or_options_t *options)
1949 /* 10. write bridge networkstatus file to disk */
1950 if (options->BridgeAuthoritativeDir) {
1951 networkstatus_dump_bridge_status_to_file(now);
1952 #define BRIDGE_STATUSFILE_INTERVAL (30*60)
1953 return BRIDGE_STATUSFILE_INTERVAL;
1955 return PERIODIC_EVENT_NO_UPDATE;
1959 * Periodic callback: poke the tor-fw-helper app if we're using one.
1961 static int
1962 check_fw_helper_app_callback(time_t now, const or_options_t *options)
1964 if (net_is_disabled() ||
1965 ! server_mode(options) ||
1966 ! options->PortForwarding) {
1967 return PERIODIC_EVENT_NO_UPDATE;
1969 /* 11. check the port forwarding app */
1971 #define PORT_FORWARDING_CHECK_INTERVAL 5
1972 smartlist_t *ports_to_forward = get_list_of_ports_to_forward();
1973 if (ports_to_forward) {
1974 tor_check_port_forwarding(options->PortForwardingHelper,
1975 ports_to_forward,
1976 now);
1978 SMARTLIST_FOREACH(ports_to_forward, char *, cp, tor_free(cp));
1979 smartlist_free(ports_to_forward);
1981 return PORT_FORWARDING_CHECK_INTERVAL;
1985 * Periodic callback: write the heartbeat message in the logs.
1987 static int
1988 heartbeat_callback(time_t now, const or_options_t *options)
1990 static int first = 1;
1992 /* Check if heartbeat is disabled */
1993 if (!options->HeartbeatPeriod) {
1994 return PERIODIC_EVENT_NO_UPDATE;
1997 /* Write the heartbeat message */
1998 if (first) {
1999 first = 0; /* Skip the first one. */
2000 } else {
2001 log_heartbeat(now);
2004 return options->HeartbeatPeriod;
2007 /** Timer: used to invoke second_elapsed_callback() once per second. */
2008 static periodic_timer_t *second_timer = NULL;
2009 /** Number of libevent errors in the last second: we die if we get too many. */
2010 static int n_libevent_errors = 0;
2012 /** Libevent callback: invoked once every second. */
2013 static void
2014 second_elapsed_callback(periodic_timer_t *timer, void *arg)
2016 /* XXXX This could be sensibly refactored into multiple callbacks, and we
2017 * could use Libevent's timers for this rather than checking the current
2018 * time against a bunch of timeouts every second. */
2019 static time_t current_second = 0;
2020 time_t now;
2021 size_t bytes_written;
2022 size_t bytes_read;
2023 int seconds_elapsed;
2024 const or_options_t *options = get_options();
2025 (void)timer;
2026 (void)arg;
2028 n_libevent_errors = 0;
2030 /* log_notice(LD_GENERAL, "Tick."); */
2031 now = time(NULL);
2032 update_approx_time(now);
2034 /* the second has rolled over. check more stuff. */
2035 seconds_elapsed = current_second ? (int)(now - current_second) : 0;
2036 bytes_read = (size_t)(stats_n_bytes_read - stats_prev_n_read);
2037 bytes_written = (size_t)(stats_n_bytes_written - stats_prev_n_written);
2038 stats_prev_n_read = stats_n_bytes_read;
2039 stats_prev_n_written = stats_n_bytes_written;
2041 control_event_bandwidth_used((uint32_t)bytes_read,(uint32_t)bytes_written);
2042 control_event_stream_bandwidth_used();
2043 control_event_conn_bandwidth_used();
2044 control_event_circ_bandwidth_used();
2045 control_event_circuit_cell_stats();
2047 if (server_mode(options) &&
2048 !net_is_disabled() &&
2049 seconds_elapsed > 0 &&
2050 have_completed_a_circuit() &&
2051 stats_n_seconds_working / TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT !=
2052 (stats_n_seconds_working+seconds_elapsed) /
2053 TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT) {
2054 /* every 20 minutes, check and complain if necessary */
2055 const routerinfo_t *me = router_get_my_routerinfo();
2056 if (me && !check_whether_orport_reachable(options)) {
2057 char *address = tor_dup_ip(me->addr);
2058 log_warn(LD_CONFIG,"Your server (%s:%d) has not managed to confirm that "
2059 "its ORPort is reachable. Relays do not publish descriptors "
2060 "until their ORPort and DirPort are reachable. Please check "
2061 "your firewalls, ports, address, /etc/hosts file, etc.",
2062 address, me->or_port);
2063 control_event_server_status(LOG_WARN,
2064 "REACHABILITY_FAILED ORADDRESS=%s:%d",
2065 address, me->or_port);
2066 tor_free(address);
2069 if (me && !check_whether_dirport_reachable(options)) {
2070 char *address = tor_dup_ip(me->addr);
2071 log_warn(LD_CONFIG,
2072 "Your server (%s:%d) has not managed to confirm that its "
2073 "DirPort is reachable. Relays do not publish descriptors "
2074 "until their ORPort and DirPort are reachable. Please check "
2075 "your firewalls, ports, address, /etc/hosts file, etc.",
2076 address, me->dir_port);
2077 control_event_server_status(LOG_WARN,
2078 "REACHABILITY_FAILED DIRADDRESS=%s:%d",
2079 address, me->dir_port);
2080 tor_free(address);
2084 /** If more than this many seconds have elapsed, probably the clock
2085 * jumped: doesn't count. */
2086 #define NUM_JUMPED_SECONDS_BEFORE_WARN 100
2087 if (seconds_elapsed < -NUM_JUMPED_SECONDS_BEFORE_WARN ||
2088 seconds_elapsed >= NUM_JUMPED_SECONDS_BEFORE_WARN) {
2089 circuit_note_clock_jumped(seconds_elapsed);
2090 } else if (seconds_elapsed > 0)
2091 stats_n_seconds_working += seconds_elapsed;
2093 run_scheduled_events(now);
2095 current_second = now; /* remember which second it is, for next time */
2098 #ifdef HAVE_SYSTEMD_209
2099 static periodic_timer_t *systemd_watchdog_timer = NULL;
2101 /** Libevent callback: invoked to reset systemd watchdog. */
2102 static void
2103 systemd_watchdog_callback(periodic_timer_t *timer, void *arg)
2105 (void)timer;
2106 (void)arg;
2107 sd_notify(0, "WATCHDOG=1");
2109 #endif
2111 /** Timer: used to invoke refill_callback(). */
2112 static periodic_timer_t *refill_timer = NULL;
2114 /** Libevent callback: invoked periodically to refill token buckets
2115 * and count r/w bytes. */
2116 static void
2117 refill_callback(periodic_timer_t *timer, void *arg)
2119 static struct timeval current_millisecond;
2120 struct timeval now;
2122 size_t bytes_written;
2123 size_t bytes_read;
2124 int milliseconds_elapsed = 0;
2125 int seconds_rolled_over = 0;
2127 const or_options_t *options = get_options();
2129 (void)timer;
2130 (void)arg;
2132 tor_gettimeofday(&now);
2134 /* If this is our first time, no time has passed. */
2135 if (current_millisecond.tv_sec) {
2136 long mdiff = tv_mdiff(&current_millisecond, &now);
2137 if (mdiff > INT_MAX)
2138 mdiff = INT_MAX;
2139 milliseconds_elapsed = (int)mdiff;
2140 seconds_rolled_over = (int)(now.tv_sec - current_millisecond.tv_sec);
2143 bytes_written = stats_prev_global_write_bucket - global_write_bucket;
2144 bytes_read = stats_prev_global_read_bucket - global_read_bucket;
2146 stats_n_bytes_read += bytes_read;
2147 stats_n_bytes_written += bytes_written;
2148 if (accounting_is_enabled(options) && milliseconds_elapsed >= 0)
2149 accounting_add_bytes(bytes_read, bytes_written, seconds_rolled_over);
2151 if (milliseconds_elapsed > 0)
2152 connection_bucket_refill(milliseconds_elapsed, (time_t)now.tv_sec);
2154 stats_prev_global_read_bucket = global_read_bucket;
2155 stats_prev_global_write_bucket = global_write_bucket;
2157 current_millisecond = now; /* remember what time it is, for next time */
2160 #ifndef _WIN32
2161 /** Called when a possibly ignorable libevent error occurs; ensures that we
2162 * don't get into an infinite loop by ignoring too many errors from
2163 * libevent. */
2164 static int
2165 got_libevent_error(void)
2167 if (++n_libevent_errors > 8) {
2168 log_err(LD_NET, "Too many libevent errors in one second; dying");
2169 return -1;
2171 return 0;
2173 #endif
2175 #define UPTIME_CUTOFF_FOR_NEW_BANDWIDTH_TEST (6*60*60)
2177 /** Called when our IP address seems to have changed. <b>at_interface</b>
2178 * should be true if we detected a change in our interface, and false if we
2179 * detected a change in our published address. */
2180 void
2181 ip_address_changed(int at_interface)
2183 const or_options_t *options = get_options();
2184 int server = server_mode(options);
2185 int exit_reject_interfaces = (server && options->ExitRelay
2186 && options->ExitPolicyRejectLocalInterfaces);
2188 if (at_interface) {
2189 if (! server) {
2190 /* Okay, change our keys. */
2191 if (init_keys_client() < 0)
2192 log_warn(LD_GENERAL, "Unable to rotate keys after IP change!");
2194 } else {
2195 if (server) {
2196 if (stats_n_seconds_working > UPTIME_CUTOFF_FOR_NEW_BANDWIDTH_TEST)
2197 reset_bandwidth_test();
2198 stats_n_seconds_working = 0;
2199 router_reset_reachability();
2203 /* Exit relays incorporate interface addresses in their exit policies when
2204 * ExitPolicyRejectLocalInterfaces is set */
2205 if (exit_reject_interfaces || (server && !at_interface)) {
2206 mark_my_descriptor_dirty("IP address changed");
2209 dns_servers_relaunch_checks();
2212 /** Forget what we've learned about the correctness of our DNS servers, and
2213 * start learning again. */
2214 void
2215 dns_servers_relaunch_checks(void)
2217 if (server_mode(get_options())) {
2218 dns_reset_correctness_checks();
2219 if (periodic_events_initialized) {
2220 tor_assert(check_dns_honesty_event);
2221 periodic_event_reschedule(check_dns_honesty_event);
2226 /** Called when we get a SIGHUP: reload configuration files and keys,
2227 * retry all connections, and so on. */
2228 static int
2229 do_hup(void)
2231 const or_options_t *options = get_options();
2233 #ifdef USE_DMALLOC
2234 dmalloc_log_stats();
2235 dmalloc_log_changed(0, 1, 0, 0);
2236 #endif
2238 log_notice(LD_GENERAL,"Received reload signal (hup). Reloading config and "
2239 "resetting internal state.");
2240 if (accounting_is_enabled(options))
2241 accounting_record_bandwidth_usage(time(NULL), get_or_state());
2243 router_reset_warnings();
2244 routerlist_reset_warnings();
2245 /* first, reload config variables, in case they've changed */
2246 if (options->ReloadTorrcOnSIGHUP) {
2247 /* no need to provide argc/v, they've been cached in init_from_config */
2248 if (options_init_from_torrc(0, NULL) < 0) {
2249 log_err(LD_CONFIG,"Reading config failed--see warnings above. "
2250 "For usage, try -h.");
2251 return -1;
2253 options = get_options(); /* they have changed now */
2254 /* Logs are only truncated the first time they are opened, but were
2255 probably intended to be cleaned up on signal. */
2256 if (options->TruncateLogFile)
2257 truncate_logs();
2258 } else {
2259 char *msg = NULL;
2260 log_notice(LD_GENERAL, "Not reloading config file: the controller told "
2261 "us not to.");
2262 /* Make stuff get rescanned, reloaded, etc. */
2263 if (set_options((or_options_t*)options, &msg) < 0) {
2264 if (!msg)
2265 msg = tor_strdup("Unknown error");
2266 log_warn(LD_GENERAL, "Unable to re-set previous options: %s", msg);
2267 tor_free(msg);
2270 if (authdir_mode_handles_descs(options, -1)) {
2271 /* reload the approved-routers file */
2272 if (dirserv_load_fingerprint_file() < 0) {
2273 /* warnings are logged from dirserv_load_fingerprint_file() directly */
2274 log_info(LD_GENERAL, "Error reloading fingerprints. "
2275 "Continuing with old list.");
2279 /* Rotate away from the old dirty circuits. This has to be done
2280 * after we've read the new options, but before we start using
2281 * circuits for directory fetches. */
2282 circuit_mark_all_dirty_circs_as_unusable();
2284 /* retry appropriate downloads */
2285 router_reset_status_download_failures();
2286 router_reset_descriptor_download_failures();
2287 if (!options->DisableNetwork)
2288 update_networkstatus_downloads(time(NULL));
2290 /* We'll retry routerstatus downloads in about 10 seconds; no need to
2291 * force a retry there. */
2293 if (server_mode(options)) {
2294 /* Maybe we've been given a new ed25519 key or certificate?
2296 time_t now = approx_time();
2297 if (load_ed_keys(options, now) < 0 ||
2298 generate_ed_link_cert(options, now)) {
2299 log_warn(LD_OR, "Problem reloading Ed25519 keys; still using old keys.");
2302 /* Update cpuworker and dnsworker processes, so they get up-to-date
2303 * configuration options. */
2304 cpuworkers_rotate_keyinfo();
2305 dns_reset();
2307 return 0;
2310 /** Tor main loop. */
2312 do_main_loop(void)
2314 time_t now;
2316 /* initialize the periodic events first, so that code that depends on the
2317 * events being present does not assert.
2319 if (! periodic_events_initialized) {
2320 initialize_periodic_events();
2323 /* initialize dns resolve map, spawn workers if needed */
2324 if (dns_init() < 0) {
2325 if (get_options()->ServerDNSAllowBrokenConfig)
2326 log_warn(LD_GENERAL, "Couldn't set up any working nameservers. "
2327 "Network not up yet? Will try again soon.");
2328 else {
2329 log_err(LD_GENERAL,"Error initializing dns subsystem; exiting. To "
2330 "retry instead, set the ServerDNSAllowBrokenResolvConf option.");
2334 handle_signals(1);
2336 /* load the private keys, if we're supposed to have them, and set up the
2337 * TLS context. */
2338 if (! client_identity_key_is_set()) {
2339 if (init_keys() < 0) {
2340 log_err(LD_OR, "Error initializing keys; exiting");
2341 return -1;
2345 /* Set up our buckets */
2346 connection_bucket_init();
2347 stats_prev_global_read_bucket = global_read_bucket;
2348 stats_prev_global_write_bucket = global_write_bucket;
2350 /* initialize the bootstrap status events to know we're starting up */
2351 control_event_bootstrap(BOOTSTRAP_STATUS_STARTING, 0);
2353 /* Initialize the keypinning log. */
2354 if (authdir_mode_v3(get_options())) {
2355 char *fname = get_datadir_fname("key-pinning-journal");
2356 int r = 0;
2357 if (keypin_load_journal(fname)<0) {
2358 log_err(LD_DIR, "Error loading key-pinning journal: %s",strerror(errno));
2359 r = -1;
2361 if (keypin_open_journal(fname)<0) {
2362 log_err(LD_DIR, "Error opening key-pinning journal: %s",strerror(errno));
2363 r = -1;
2365 tor_free(fname);
2366 if (r)
2367 return r;
2370 /* This is the old name for key-pinning-journal. These got corrupted
2371 * in a couple of cases by #16530, so we started over. See #16580 for
2372 * the rationale and for other options we didn't take. We can remove
2373 * this code once all the authorities that ran 0.2.7.1-alpha-dev are
2374 * upgraded.
2376 char *fname = get_datadir_fname("key-pinning-entries");
2377 unlink(fname);
2378 tor_free(fname);
2381 if (trusted_dirs_reload_certs()) {
2382 log_warn(LD_DIR,
2383 "Couldn't load all cached v3 certificates. Starting anyway.");
2385 if (router_reload_consensus_networkstatus()) {
2386 return -1;
2388 /* load the routers file, or assign the defaults. */
2389 if (router_reload_router_list()) {
2390 return -1;
2392 /* load the networkstatuses. (This launches a download for new routers as
2393 * appropriate.)
2395 now = time(NULL);
2396 directory_info_has_arrived(now, 1, 0);
2398 if (server_mode(get_options())) {
2399 /* launch cpuworkers. Need to do this *after* we've read the onion key. */
2400 cpu_init();
2403 /* Setup shared random protocol subsystem. */
2404 if (authdir_mode_publishes_statuses(get_options())) {
2405 if (sr_init(1) < 0) {
2406 return -1;
2410 /* set up once-a-second callback. */
2411 if (! second_timer) {
2412 struct timeval one_second;
2413 one_second.tv_sec = 1;
2414 one_second.tv_usec = 0;
2416 second_timer = periodic_timer_new(tor_libevent_get_base(),
2417 &one_second,
2418 second_elapsed_callback,
2419 NULL);
2420 tor_assert(second_timer);
2423 #ifdef HAVE_SYSTEMD_209
2424 uint64_t watchdog_delay;
2425 /* set up systemd watchdog notification. */
2426 if (sd_watchdog_enabled(1, &watchdog_delay) > 0) {
2427 if (! systemd_watchdog_timer) {
2428 struct timeval watchdog;
2429 /* The manager will "act on" us if we don't send them a notification
2430 * every 'watchdog_delay' microseconds. So, send notifications twice
2431 * that often. */
2432 watchdog_delay /= 2;
2433 watchdog.tv_sec = watchdog_delay / 1000000;
2434 watchdog.tv_usec = watchdog_delay % 1000000;
2436 systemd_watchdog_timer = periodic_timer_new(tor_libevent_get_base(),
2437 &watchdog,
2438 systemd_watchdog_callback,
2439 NULL);
2440 tor_assert(systemd_watchdog_timer);
2443 #endif
2445 if (!refill_timer) {
2446 struct timeval refill_interval;
2447 int msecs = get_options()->TokenBucketRefillInterval;
2449 refill_interval.tv_sec = msecs/1000;
2450 refill_interval.tv_usec = (msecs%1000)*1000;
2452 refill_timer = periodic_timer_new(tor_libevent_get_base(),
2453 &refill_interval,
2454 refill_callback,
2455 NULL);
2456 tor_assert(refill_timer);
2459 #ifdef HAVE_SYSTEMD
2461 const int r = sd_notify(0, "READY=1");
2462 if (r < 0) {
2463 log_warn(LD_GENERAL, "Unable to send readiness to systemd: %s",
2464 strerror(r));
2465 } else if (r > 0) {
2466 log_notice(LD_GENERAL, "Signaled readiness to systemd");
2467 } else {
2468 log_info(LD_GENERAL, "Systemd NOTIFY_SOCKET not present.");
2471 #endif
2473 return run_main_loop_until_done();
2477 * Run the main loop a single time. Return 0 for "exit"; -1 for "exit with
2478 * error", and 1 for "run this again."
2480 static int
2481 run_main_loop_once(void)
2483 int loop_result;
2485 if (nt_service_is_stopping())
2486 return 0;
2488 #ifndef _WIN32
2489 /* Make it easier to tell whether libevent failure is our fault or not. */
2490 errno = 0;
2491 #endif
2493 /* All active linked conns should get their read events activated,
2494 * so that libevent knows to run their callbacks. */
2495 SMARTLIST_FOREACH(active_linked_connection_lst, connection_t *, conn,
2496 event_active(conn->read_event, EV_READ, 1));
2497 called_loop_once = smartlist_len(active_linked_connection_lst) ? 1 : 0;
2499 /* Make sure we know (about) what time it is. */
2500 update_approx_time(time(NULL));
2502 /* Here it is: the main loop. Here we tell Libevent to poll until we have
2503 * an event, or the second ends, or until we have some active linked
2504 * connections to trigger events for. Libevent will wait till one
2505 * of these happens, then run all the appropriate callbacks. */
2506 loop_result = event_base_loop(tor_libevent_get_base(),
2507 called_loop_once ? EVLOOP_ONCE : 0);
2509 /* Oh, the loop failed. That might be an error that we need to
2510 * catch, but more likely, it's just an interrupted poll() call or something,
2511 * and we should try again. */
2512 if (loop_result < 0) {
2513 int e = tor_socket_errno(-1);
2514 /* let the program survive things like ^z */
2515 if (e != EINTR && !ERRNO_IS_EINPROGRESS(e)) {
2516 log_err(LD_NET,"libevent call with %s failed: %s [%d]",
2517 tor_libevent_get_method(), tor_socket_strerror(e), e);
2518 return -1;
2519 #ifndef _WIN32
2520 } else if (e == EINVAL) {
2521 log_warn(LD_NET, "EINVAL from libevent: should you upgrade libevent?");
2522 if (got_libevent_error())
2523 return -1;
2524 #endif
2525 } else {
2526 tor_assert_nonfatal_once(! ERRNO_IS_EINPROGRESS(e));
2527 log_debug(LD_NET,"libevent call interrupted.");
2528 /* You can't trust the results of this poll(). Go back to the
2529 * top of the big for loop. */
2530 return 1;
2534 /* And here is where we put callbacks that happen "every time the event loop
2535 * runs." They must be very fast, or else the whole Tor process will get
2536 * slowed down.
2538 * Note that this gets called once per libevent loop, which will make it
2539 * happen once per group of events that fire, or once per second. */
2541 /* If there are any pending client connections, try attaching them to
2542 * circuits (if we can.) This will be pretty fast if nothing new is
2543 * pending.
2545 connection_ap_attach_pending(0);
2547 return 1;
2550 /** Run the run_main_loop_once() function until it declares itself done,
2551 * and return its final return value.
2553 * Shadow won't invoke this function, so don't fill it up with things.
2555 static int
2556 run_main_loop_until_done(void)
2558 int loop_result = 1;
2559 do {
2560 loop_result = run_main_loop_once();
2561 } while (loop_result == 1);
2562 return loop_result;
2565 /** Libevent callback: invoked when we get a signal.
2567 static void
2568 signal_callback(evutil_socket_t fd, short events, void *arg)
2570 const int *sigptr = arg;
2571 const int sig = *sigptr;
2572 (void)fd;
2573 (void)events;
2575 process_signal(sig);
2578 /** Do the work of acting on a signal received in <b>sig</b> */
2579 static void
2580 process_signal(int sig)
2582 switch (sig)
2584 case SIGTERM:
2585 log_notice(LD_GENERAL,"Catching signal TERM, exiting cleanly.");
2586 tor_cleanup();
2587 exit(0);
2588 break;
2589 case SIGINT:
2590 if (!server_mode(get_options())) { /* do it now */
2591 log_notice(LD_GENERAL,"Interrupt: exiting cleanly.");
2592 tor_cleanup();
2593 exit(0);
2595 #ifdef HAVE_SYSTEMD
2596 sd_notify(0, "STOPPING=1");
2597 #endif
2598 hibernate_begin_shutdown();
2599 break;
2600 #ifdef SIGPIPE
2601 case SIGPIPE:
2602 log_debug(LD_GENERAL,"Caught SIGPIPE. Ignoring.");
2603 break;
2604 #endif
2605 case SIGUSR1:
2606 /* prefer to log it at INFO, but make sure we always see it */
2607 dumpstats(get_min_log_level()<LOG_INFO ? get_min_log_level() : LOG_INFO);
2608 control_event_signal(sig);
2609 break;
2610 case SIGUSR2:
2611 switch_logs_debug();
2612 log_debug(LD_GENERAL,"Caught USR2, going to loglevel debug. "
2613 "Send HUP to change back.");
2614 control_event_signal(sig);
2615 break;
2616 case SIGHUP:
2617 #ifdef HAVE_SYSTEMD
2618 sd_notify(0, "RELOADING=1");
2619 #endif
2620 if (do_hup() < 0) {
2621 log_warn(LD_CONFIG,"Restart failed (config error?). Exiting.");
2622 tor_cleanup();
2623 exit(1);
2625 #ifdef HAVE_SYSTEMD
2626 sd_notify(0, "READY=1");
2627 #endif
2628 control_event_signal(sig);
2629 break;
2630 #ifdef SIGCHLD
2631 case SIGCHLD:
2632 notify_pending_waitpid_callbacks();
2633 break;
2634 #endif
2635 case SIGNEWNYM: {
2636 time_t now = time(NULL);
2637 if (time_of_last_signewnym + MAX_SIGNEWNYM_RATE > now) {
2638 signewnym_is_pending = 1;
2639 log_notice(LD_CONTROL,
2640 "Rate limiting NEWNYM request: delaying by %d second(s)",
2641 (int)(MAX_SIGNEWNYM_RATE+time_of_last_signewnym-now));
2642 } else {
2643 signewnym_impl(now);
2645 break;
2647 case SIGCLEARDNSCACHE:
2648 addressmap_clear_transient();
2649 control_event_signal(sig);
2650 break;
2651 case SIGHEARTBEAT:
2652 log_heartbeat(time(NULL));
2653 control_event_signal(sig);
2654 break;
2658 /** Returns Tor's uptime. */
2659 MOCK_IMPL(long,
2660 get_uptime,(void))
2662 return stats_n_seconds_working;
2666 * Write current memory usage information to the log.
2668 static void
2669 dumpmemusage(int severity)
2671 connection_dump_buffer_mem_stats(severity);
2672 tor_log(severity, LD_GENERAL, "In rephist: "U64_FORMAT" used by %d Tors.",
2673 U64_PRINTF_ARG(rephist_total_alloc), rephist_total_num);
2674 dump_routerlist_mem_usage(severity);
2675 dump_cell_pool_usage(severity);
2676 dump_dns_mem_usage(severity);
2677 tor_log_mallinfo(severity);
2680 /** Write all statistics to the log, with log level <b>severity</b>. Called
2681 * in response to a SIGUSR1. */
2682 static void
2683 dumpstats(int severity)
2685 time_t now = time(NULL);
2686 time_t elapsed;
2687 size_t rbuf_cap, wbuf_cap, rbuf_len, wbuf_len;
2689 tor_log(severity, LD_GENERAL, "Dumping stats:");
2691 SMARTLIST_FOREACH_BEGIN(connection_array, connection_t *, conn) {
2692 int i = conn_sl_idx;
2693 tor_log(severity, LD_GENERAL,
2694 "Conn %d (socket %d) type %d (%s), state %d (%s), created %d secs ago",
2695 i, (int)conn->s, conn->type, conn_type_to_string(conn->type),
2696 conn->state, conn_state_to_string(conn->type, conn->state),
2697 (int)(now - conn->timestamp_created));
2698 if (!connection_is_listener(conn)) {
2699 tor_log(severity,LD_GENERAL,
2700 "Conn %d is to %s:%d.", i,
2701 safe_str_client(conn->address),
2702 conn->port);
2703 tor_log(severity,LD_GENERAL,
2704 "Conn %d: %d bytes waiting on inbuf (len %d, last read %d secs ago)",
2706 (int)connection_get_inbuf_len(conn),
2707 (int)buf_allocation(conn->inbuf),
2708 (int)(now - conn->timestamp_lastread));
2709 tor_log(severity,LD_GENERAL,
2710 "Conn %d: %d bytes waiting on outbuf "
2711 "(len %d, last written %d secs ago)",i,
2712 (int)connection_get_outbuf_len(conn),
2713 (int)buf_allocation(conn->outbuf),
2714 (int)(now - conn->timestamp_lastwritten));
2715 if (conn->type == CONN_TYPE_OR) {
2716 or_connection_t *or_conn = TO_OR_CONN(conn);
2717 if (or_conn->tls) {
2718 if (tor_tls_get_buffer_sizes(or_conn->tls, &rbuf_cap, &rbuf_len,
2719 &wbuf_cap, &wbuf_len) == 0) {
2720 tor_log(severity, LD_GENERAL,
2721 "Conn %d: %d/%d bytes used on OpenSSL read buffer; "
2722 "%d/%d bytes used on write buffer.",
2723 i, (int)rbuf_len, (int)rbuf_cap, (int)wbuf_len, (int)wbuf_cap);
2728 circuit_dump_by_conn(conn, severity); /* dump info about all the circuits
2729 * using this conn */
2730 } SMARTLIST_FOREACH_END(conn);
2732 channel_dumpstats(severity);
2733 channel_listener_dumpstats(severity);
2735 tor_log(severity, LD_NET,
2736 "Cells processed: "U64_FORMAT" padding\n"
2737 " "U64_FORMAT" create\n"
2738 " "U64_FORMAT" created\n"
2739 " "U64_FORMAT" relay\n"
2740 " ("U64_FORMAT" relayed)\n"
2741 " ("U64_FORMAT" delivered)\n"
2742 " "U64_FORMAT" destroy",
2743 U64_PRINTF_ARG(stats_n_padding_cells_processed),
2744 U64_PRINTF_ARG(stats_n_create_cells_processed),
2745 U64_PRINTF_ARG(stats_n_created_cells_processed),
2746 U64_PRINTF_ARG(stats_n_relay_cells_processed),
2747 U64_PRINTF_ARG(stats_n_relay_cells_relayed),
2748 U64_PRINTF_ARG(stats_n_relay_cells_delivered),
2749 U64_PRINTF_ARG(stats_n_destroy_cells_processed));
2750 if (stats_n_data_cells_packaged)
2751 tor_log(severity,LD_NET,"Average packaged cell fullness: %2.3f%%",
2752 100*(U64_TO_DBL(stats_n_data_bytes_packaged) /
2753 U64_TO_DBL(stats_n_data_cells_packaged*RELAY_PAYLOAD_SIZE)) );
2754 if (stats_n_data_cells_received)
2755 tor_log(severity,LD_NET,"Average delivered cell fullness: %2.3f%%",
2756 100*(U64_TO_DBL(stats_n_data_bytes_received) /
2757 U64_TO_DBL(stats_n_data_cells_received*RELAY_PAYLOAD_SIZE)) );
2759 cpuworker_log_onionskin_overhead(severity, ONION_HANDSHAKE_TYPE_TAP, "TAP");
2760 cpuworker_log_onionskin_overhead(severity, ONION_HANDSHAKE_TYPE_NTOR,"ntor");
2762 if (now - time_of_process_start >= 0)
2763 elapsed = now - time_of_process_start;
2764 else
2765 elapsed = 0;
2767 if (elapsed) {
2768 tor_log(severity, LD_NET,
2769 "Average bandwidth: "U64_FORMAT"/%d = %d bytes/sec reading",
2770 U64_PRINTF_ARG(stats_n_bytes_read),
2771 (int)elapsed,
2772 (int) (stats_n_bytes_read/elapsed));
2773 tor_log(severity, LD_NET,
2774 "Average bandwidth: "U64_FORMAT"/%d = %d bytes/sec writing",
2775 U64_PRINTF_ARG(stats_n_bytes_written),
2776 (int)elapsed,
2777 (int) (stats_n_bytes_written/elapsed));
2780 tor_log(severity, LD_NET, "--------------- Dumping memory information:");
2781 dumpmemusage(severity);
2783 rep_hist_dump_stats(now,severity);
2784 rend_service_dump_stats(severity);
2785 dump_pk_ops(severity);
2786 dump_distinct_digest_count(severity);
2789 /** Called by exit() as we shut down the process.
2791 static void
2792 exit_function(void)
2794 /* NOTE: If we ever daemonize, this gets called immediately. That's
2795 * okay for now, because we only use this on Windows. */
2796 #ifdef _WIN32
2797 WSACleanup();
2798 #endif
2801 #ifdef _WIN32
2802 #define UNIX_ONLY 0
2803 #else
2804 #define UNIX_ONLY 1
2805 #endif
2806 static struct {
2807 int signal_value;
2808 int try_to_register;
2809 struct event *signal_event;
2810 } signal_handlers[] = {
2811 #ifdef SIGINT
2812 { SIGINT, UNIX_ONLY, NULL }, /* do a controlled slow shutdown */
2813 #endif
2814 #ifdef SIGTERM
2815 { SIGTERM, UNIX_ONLY, NULL }, /* to terminate now */
2816 #endif
2817 #ifdef SIGPIPE
2818 { SIGPIPE, UNIX_ONLY, NULL }, /* otherwise SIGPIPE kills us */
2819 #endif
2820 #ifdef SIGUSR1
2821 { SIGUSR1, UNIX_ONLY, NULL }, /* dump stats */
2822 #endif
2823 #ifdef SIGUSR2
2824 { SIGUSR2, UNIX_ONLY, NULL }, /* go to loglevel debug */
2825 #endif
2826 #ifdef SIGHUP
2827 { SIGHUP, UNIX_ONLY, NULL }, /* to reload config, retry conns, etc */
2828 #endif
2829 #ifdef SIGXFSZ
2830 { SIGXFSZ, UNIX_ONLY, NULL }, /* handle file-too-big resource exhaustion */
2831 #endif
2832 #ifdef SIGCHLD
2833 { SIGCHLD, UNIX_ONLY, NULL }, /* handle dns/cpu workers that exit */
2834 #endif
2835 /* These are controller-only */
2836 { SIGNEWNYM, 0, NULL },
2837 { SIGCLEARDNSCACHE, 0, NULL },
2838 { SIGHEARTBEAT, 0, NULL },
2839 { -1, -1, NULL }
2842 /** Set up the signal handlers for either parent or child process */
2843 void
2844 handle_signals(int is_parent)
2846 int i;
2847 if (is_parent) {
2848 for (i = 0; signal_handlers[i].signal_value >= 0; ++i) {
2849 if (signal_handlers[i].try_to_register) {
2850 signal_handlers[i].signal_event =
2851 tor_evsignal_new(tor_libevent_get_base(),
2852 signal_handlers[i].signal_value,
2853 signal_callback,
2854 &signal_handlers[i].signal_value);
2855 if (event_add(signal_handlers[i].signal_event, NULL))
2856 log_warn(LD_BUG, "Error from libevent when adding "
2857 "event for signal %d",
2858 signal_handlers[i].signal_value);
2859 } else {
2860 signal_handlers[i].signal_event =
2861 tor_event_new(tor_libevent_get_base(), -1,
2862 EV_SIGNAL, signal_callback,
2863 &signal_handlers[i].signal_value);
2866 } else {
2867 #ifndef _WIN32
2868 struct sigaction action;
2869 action.sa_flags = 0;
2870 sigemptyset(&action.sa_mask);
2871 action.sa_handler = SIG_IGN;
2872 sigaction(SIGINT, &action, NULL);
2873 sigaction(SIGTERM, &action, NULL);
2874 sigaction(SIGPIPE, &action, NULL);
2875 sigaction(SIGUSR1, &action, NULL);
2876 sigaction(SIGUSR2, &action, NULL);
2877 sigaction(SIGHUP, &action, NULL);
2878 #ifdef SIGXFSZ
2879 sigaction(SIGXFSZ, &action, NULL);
2880 #endif
2881 #endif
2885 /* Make sure the signal handler for signal_num will be called. */
2886 void
2887 activate_signal(int signal_num)
2889 int i;
2890 for (i = 0; signal_handlers[i].signal_value >= 0; ++i) {
2891 if (signal_handlers[i].signal_value == signal_num) {
2892 event_active(signal_handlers[i].signal_event, EV_SIGNAL, 1);
2893 return;
2898 /** Main entry point for the Tor command-line client.
2901 tor_init(int argc, char *argv[])
2903 char progname[256];
2904 int quiet = 0;
2906 time_of_process_start = time(NULL);
2907 init_connection_lists();
2908 /* Have the log set up with our application name. */
2909 tor_snprintf(progname, sizeof(progname), "Tor %s", get_version());
2910 log_set_application_name(progname);
2912 /* Set up the crypto nice and early */
2913 if (crypto_early_init() < 0) {
2914 log_err(LD_GENERAL, "Unable to initialize the crypto subsystem!");
2915 return -1;
2918 /* Initialize the history structures. */
2919 rep_hist_init();
2920 /* Initialize the service cache. */
2921 rend_cache_init();
2922 addressmap_init(); /* Init the client dns cache. Do it always, since it's
2923 * cheap. */
2926 /* We search for the "quiet" option first, since it decides whether we
2927 * will log anything at all to the command line. */
2928 config_line_t *opts = NULL, *cmdline_opts = NULL;
2929 const config_line_t *cl;
2930 (void) config_parse_commandline(argc, argv, 1, &opts, &cmdline_opts);
2931 for (cl = cmdline_opts; cl; cl = cl->next) {
2932 if (!strcmp(cl->key, "--hush"))
2933 quiet = 1;
2934 if (!strcmp(cl->key, "--quiet") ||
2935 !strcmp(cl->key, "--dump-config"))
2936 quiet = 2;
2937 /* The following options imply --hush */
2938 if (!strcmp(cl->key, "--version") || !strcmp(cl->key, "--digests") ||
2939 !strcmp(cl->key, "--list-torrc-options") ||
2940 !strcmp(cl->key, "--library-versions") ||
2941 !strcmp(cl->key, "--hash-password") ||
2942 !strcmp(cl->key, "-h") || !strcmp(cl->key, "--help")) {
2943 if (quiet < 1)
2944 quiet = 1;
2947 config_free_lines(opts);
2948 config_free_lines(cmdline_opts);
2951 /* give it somewhere to log to initially */
2952 switch (quiet) {
2953 case 2:
2954 /* no initial logging */
2955 break;
2956 case 1:
2957 add_temp_log(LOG_WARN);
2958 break;
2959 default:
2960 add_temp_log(LOG_NOTICE);
2962 quiet_level = quiet;
2965 const char *version = get_version();
2967 log_notice(LD_GENERAL, "Tor %s running on %s with Libevent %s, "
2968 "OpenSSL %s and Zlib %s.", version,
2969 get_uname(),
2970 tor_libevent_get_version_str(),
2971 crypto_openssl_get_version_str(),
2972 tor_zlib_get_version_str());
2974 log_notice(LD_GENERAL, "Tor can't help you if you use it wrong! "
2975 "Learn how to be safe at "
2976 "https://www.torproject.org/download/download#warning");
2978 if (strstr(version, "alpha") || strstr(version, "beta"))
2979 log_notice(LD_GENERAL, "This version is not a stable Tor release. "
2980 "Expect more bugs than usual.");
2983 if (network_init()<0) {
2984 log_err(LD_BUG,"Error initializing network; exiting.");
2985 return -1;
2987 atexit(exit_function);
2989 if (options_init_from_torrc(argc,argv) < 0) {
2990 log_err(LD_CONFIG,"Reading config failed--see warnings above.");
2991 return -1;
2994 /* The options are now initialised */
2995 const or_options_t *options = get_options();
2997 #ifndef _WIN32
2998 if (geteuid()==0)
2999 log_warn(LD_GENERAL,"You are running Tor as root. You don't need to, "
3000 "and you probably shouldn't.");
3001 #endif
3003 if (crypto_global_init(options->HardwareAccel,
3004 options->AccelName,
3005 options->AccelDir)) {
3006 log_err(LD_BUG, "Unable to initialize OpenSSL. Exiting.");
3007 return -1;
3009 stream_choice_seed_weak_rng();
3010 if (tor_init_libevent_rng() < 0) {
3011 log_warn(LD_NET, "Problem initializing libevent RNG.");
3014 /* Scan/clean unparseable descroptors; after reading config */
3015 routerparse_init();
3017 return 0;
3020 /** A lockfile structure, used to prevent two Tors from messing with the
3021 * data directory at once. If this variable is non-NULL, we're holding
3022 * the lockfile. */
3023 static tor_lockfile_t *lockfile = NULL;
3025 /** Try to grab the lock file described in <b>options</b>, if we do not
3026 * already have it. If <b>err_if_locked</b> is true, warn if somebody else is
3027 * holding the lock, and exit if we can't get it after waiting. Otherwise,
3028 * return -1 if we can't get the lockfile. Return 0 on success.
3031 try_locking(const or_options_t *options, int err_if_locked)
3033 if (lockfile)
3034 return 0;
3035 else {
3036 char *fname = options_get_datadir_fname2_suffix(options, "lock",NULL,NULL);
3037 int already_locked = 0;
3038 tor_lockfile_t *lf = tor_lockfile_lock(fname, 0, &already_locked);
3039 tor_free(fname);
3040 if (!lf) {
3041 if (err_if_locked && already_locked) {
3042 int r;
3043 log_warn(LD_GENERAL, "It looks like another Tor process is running "
3044 "with the same data directory. Waiting 5 seconds to see "
3045 "if it goes away.");
3046 #ifndef _WIN32
3047 sleep(5);
3048 #else
3049 Sleep(5000);
3050 #endif
3051 r = try_locking(options, 0);
3052 if (r<0) {
3053 log_err(LD_GENERAL, "No, it's still there. Exiting.");
3054 exit(0);
3056 return r;
3058 return -1;
3060 lockfile = lf;
3061 return 0;
3065 /** Return true iff we've successfully acquired the lock file. */
3067 have_lockfile(void)
3069 return lockfile != NULL;
3072 /** If we have successfully acquired the lock file, release it. */
3073 void
3074 release_lockfile(void)
3076 if (lockfile) {
3077 tor_lockfile_unlock(lockfile);
3078 lockfile = NULL;
3082 /** Free all memory that we might have allocated somewhere.
3083 * If <b>postfork</b>, we are a worker process and we want to free
3084 * only the parts of memory that we won't touch. If !<b>postfork</b>,
3085 * Tor is shutting down and we should free everything.
3087 * Helps us find the real leaks with dmalloc and the like. Also valgrind
3088 * should then report 0 reachable in its leak report (in an ideal world --
3089 * in practice libevent, SSL, libc etc never quite free everything). */
3090 void
3091 tor_free_all(int postfork)
3093 if (!postfork) {
3094 evdns_shutdown(1);
3096 geoip_free_all();
3097 dirvote_free_all();
3098 routerlist_free_all();
3099 networkstatus_free_all();
3100 addressmap_free_all();
3101 dirserv_free_all();
3102 rend_service_free_all();
3103 rend_cache_free_all();
3104 rend_service_authorization_free_all();
3105 hs_cache_free_all();
3106 rep_hist_free_all();
3107 dns_free_all();
3108 clear_pending_onions();
3109 circuit_free_all();
3110 entry_guards_free_all();
3111 pt_free_all();
3112 channel_tls_free_all();
3113 channel_free_all();
3114 connection_free_all();
3115 connection_edge_free_all();
3116 scheduler_free_all();
3117 nodelist_free_all();
3118 microdesc_free_all();
3119 routerparse_free_all();
3120 ext_orport_free_all();
3121 control_free_all();
3122 sandbox_free_getaddrinfo_cache();
3123 protover_free_all();
3124 bridges_free_all();
3125 if (!postfork) {
3126 config_free_all();
3127 or_state_free_all();
3128 router_free_all();
3129 routerkeys_free_all();
3130 policies_free_all();
3132 if (!postfork) {
3133 tor_tls_free_all();
3134 #ifndef _WIN32
3135 tor_getpwnam(NULL);
3136 #endif
3138 /* stuff in main.c */
3140 smartlist_free(connection_array);
3141 smartlist_free(closeable_connection_lst);
3142 smartlist_free(active_linked_connection_lst);
3143 periodic_timer_free(second_timer);
3144 teardown_periodic_events();
3145 periodic_timer_free(refill_timer);
3147 if (!postfork) {
3148 release_lockfile();
3150 /* Stuff in util.c and address.c*/
3151 if (!postfork) {
3152 escaped(NULL);
3153 esc_router_info(NULL);
3154 logs_free_all(); /* free log strings. do this last so logs keep working. */
3158 /** Do whatever cleanup is necessary before shutting Tor down. */
3159 void
3160 tor_cleanup(void)
3162 const or_options_t *options = get_options();
3163 if (options->command == CMD_RUN_TOR) {
3164 time_t now = time(NULL);
3165 /* Remove our pid file. We don't care if there was an error when we
3166 * unlink, nothing we could do about it anyways. */
3167 if (options->PidFile) {
3168 if (unlink(options->PidFile) != 0) {
3169 log_warn(LD_FS, "Couldn't unlink pid file %s: %s",
3170 options->PidFile, strerror(errno));
3173 if (options->ControlPortWriteToFile) {
3174 if (unlink(options->ControlPortWriteToFile) != 0) {
3175 log_warn(LD_FS, "Couldn't unlink control port file %s: %s",
3176 options->ControlPortWriteToFile,
3177 strerror(errno));
3180 if (accounting_is_enabled(options))
3181 accounting_record_bandwidth_usage(now, get_or_state());
3182 or_state_mark_dirty(get_or_state(), 0); /* force an immediate save. */
3183 or_state_save(now);
3184 if (authdir_mode(options)) {
3185 sr_save_and_cleanup();
3187 if (authdir_mode_tests_reachability(options))
3188 rep_hist_record_mtbf_data(now, 0);
3189 keypin_close_journal();
3191 #ifdef USE_DMALLOC
3192 dmalloc_log_stats();
3193 #endif
3194 tor_free_all(0); /* We could move tor_free_all back into the ifdef below
3195 later, if it makes shutdown unacceptably slow. But for
3196 now, leave it here: it's helped us catch bugs in the
3197 past. */
3198 crypto_global_cleanup();
3199 #ifdef USE_DMALLOC
3200 dmalloc_log_unfreed();
3201 dmalloc_shutdown();
3202 #endif
3205 /** Read/create keys as needed, and echo our fingerprint to stdout. */
3206 static int
3207 do_list_fingerprint(void)
3209 char buf[FINGERPRINT_LEN+1];
3210 crypto_pk_t *k;
3211 const char *nickname = get_options()->Nickname;
3212 sandbox_disable_getaddrinfo_cache();
3213 if (!server_mode(get_options())) {
3214 log_err(LD_GENERAL,
3215 "Clients don't have long-term identity keys. Exiting.");
3216 return -1;
3218 tor_assert(nickname);
3219 if (init_keys() < 0) {
3220 log_err(LD_GENERAL,"Error initializing keys; exiting.");
3221 return -1;
3223 if (!(k = get_server_identity_key())) {
3224 log_err(LD_GENERAL,"Error: missing identity key.");
3225 return -1;
3227 if (crypto_pk_get_fingerprint(k, buf, 1)<0) {
3228 log_err(LD_BUG, "Error computing fingerprint");
3229 return -1;
3231 printf("%s %s\n", nickname, buf);
3232 return 0;
3235 /** Entry point for password hashing: take the desired password from
3236 * the command line, and print its salted hash to stdout. **/
3237 static void
3238 do_hash_password(void)
3241 char output[256];
3242 char key[S2K_RFC2440_SPECIFIER_LEN+DIGEST_LEN];
3244 crypto_rand(key, S2K_RFC2440_SPECIFIER_LEN-1);
3245 key[S2K_RFC2440_SPECIFIER_LEN-1] = (uint8_t)96; /* Hash 64 K of data. */
3246 secret_to_key_rfc2440(key+S2K_RFC2440_SPECIFIER_LEN, DIGEST_LEN,
3247 get_options()->command_arg, strlen(get_options()->command_arg),
3248 key);
3249 base16_encode(output, sizeof(output), key, sizeof(key));
3250 printf("16:%s\n",output);
3253 /** Entry point for configuration dumping: write the configuration to
3254 * stdout. */
3255 static int
3256 do_dump_config(void)
3258 const or_options_t *options = get_options();
3259 const char *arg = options->command_arg;
3260 int how;
3261 char *opts;
3263 if (!strcmp(arg, "short")) {
3264 how = OPTIONS_DUMP_MINIMAL;
3265 } else if (!strcmp(arg, "non-builtin")) {
3266 how = OPTIONS_DUMP_DEFAULTS;
3267 } else if (!strcmp(arg, "full")) {
3268 how = OPTIONS_DUMP_ALL;
3269 } else {
3270 fprintf(stderr, "No valid argument to --dump-config found!\n");
3271 fprintf(stderr, "Please select 'short', 'non-builtin', or 'full'.\n");
3273 return -1;
3276 opts = options_dump(options, how);
3277 printf("%s", opts);
3278 tor_free(opts);
3280 return 0;
3283 static void
3284 init_addrinfo(void)
3286 if (! server_mode(get_options()) ||
3287 (get_options()->Address && strlen(get_options()->Address) > 0)) {
3288 /* We don't need to seed our own hostname, because we won't be calling
3289 * resolve_my_address on it.
3291 return;
3293 char hname[256];
3295 // host name to sandbox
3296 gethostname(hname, sizeof(hname));
3297 sandbox_add_addrinfo(hname);
3300 static sandbox_cfg_t*
3301 sandbox_init_filter(void)
3303 const or_options_t *options = get_options();
3304 sandbox_cfg_t *cfg = sandbox_cfg_new();
3305 int i;
3307 sandbox_cfg_allow_openat_filename(&cfg,
3308 get_datadir_fname("cached-status"));
3310 #define OPEN(name) \
3311 sandbox_cfg_allow_open_filename(&cfg, tor_strdup(name))
3313 #define OPEN_DATADIR(name) \
3314 sandbox_cfg_allow_open_filename(&cfg, get_datadir_fname(name))
3316 #define OPEN_DATADIR2(name, name2) \
3317 sandbox_cfg_allow_open_filename(&cfg, get_datadir_fname2((name), (name2)))
3319 #define OPEN_DATADIR_SUFFIX(name, suffix) do { \
3320 OPEN_DATADIR(name); \
3321 OPEN_DATADIR(name suffix); \
3322 } while (0)
3324 #define OPEN_DATADIR2_SUFFIX(name, name2, suffix) do { \
3325 OPEN_DATADIR2(name, name2); \
3326 OPEN_DATADIR2(name, name2 suffix); \
3327 } while (0)
3329 OPEN(options->DataDirectory);
3330 OPEN_DATADIR("keys");
3331 OPEN_DATADIR_SUFFIX("cached-certs", ".tmp");
3332 OPEN_DATADIR_SUFFIX("cached-consensus", ".tmp");
3333 OPEN_DATADIR_SUFFIX("unverified-consensus", ".tmp");
3334 OPEN_DATADIR_SUFFIX("unverified-microdesc-consensus", ".tmp");
3335 OPEN_DATADIR_SUFFIX("cached-microdesc-consensus", ".tmp");
3336 OPEN_DATADIR_SUFFIX("cached-microdescs", ".tmp");
3337 OPEN_DATADIR_SUFFIX("cached-microdescs.new", ".tmp");
3338 OPEN_DATADIR_SUFFIX("cached-descriptors", ".tmp");
3339 OPEN_DATADIR_SUFFIX("cached-descriptors.new", ".tmp");
3340 OPEN_DATADIR("cached-descriptors.tmp.tmp");
3341 OPEN_DATADIR_SUFFIX("cached-extrainfo", ".tmp");
3342 OPEN_DATADIR_SUFFIX("cached-extrainfo.new", ".tmp");
3343 OPEN_DATADIR("cached-extrainfo.tmp.tmp");
3344 OPEN_DATADIR_SUFFIX("state", ".tmp");
3345 OPEN_DATADIR_SUFFIX("sr-state", ".tmp");
3346 OPEN_DATADIR_SUFFIX("unparseable-desc", ".tmp");
3347 OPEN_DATADIR_SUFFIX("v3-status-votes", ".tmp");
3348 OPEN_DATADIR("key-pinning-journal");
3349 OPEN("/dev/srandom");
3350 OPEN("/dev/urandom");
3351 OPEN("/dev/random");
3352 OPEN("/etc/hosts");
3353 OPEN("/proc/meminfo");
3355 if (options->BridgeAuthoritativeDir)
3356 OPEN_DATADIR_SUFFIX("networkstatus-bridges", ".tmp");
3358 if (authdir_mode_handles_descs(options, -1))
3359 OPEN_DATADIR("approved-routers");
3361 if (options->ServerDNSResolvConfFile)
3362 sandbox_cfg_allow_open_filename(&cfg,
3363 tor_strdup(options->ServerDNSResolvConfFile));
3364 else
3365 sandbox_cfg_allow_open_filename(&cfg, tor_strdup("/etc/resolv.conf"));
3367 for (i = 0; i < 2; ++i) {
3368 if (get_torrc_fname(i)) {
3369 sandbox_cfg_allow_open_filename(&cfg, tor_strdup(get_torrc_fname(i)));
3373 #define RENAME_SUFFIX(name, suffix) \
3374 sandbox_cfg_allow_rename(&cfg, \
3375 get_datadir_fname(name suffix), \
3376 get_datadir_fname(name))
3378 #define RENAME_SUFFIX2(prefix, name, suffix) \
3379 sandbox_cfg_allow_rename(&cfg, \
3380 get_datadir_fname2(prefix, name suffix), \
3381 get_datadir_fname2(prefix, name))
3383 RENAME_SUFFIX("cached-certs", ".tmp");
3384 RENAME_SUFFIX("cached-consensus", ".tmp");
3385 RENAME_SUFFIX("unverified-consensus", ".tmp");
3386 RENAME_SUFFIX("unverified-microdesc-consensus", ".tmp");
3387 RENAME_SUFFIX("cached-microdesc-consensus", ".tmp");
3388 RENAME_SUFFIX("cached-microdescs", ".tmp");
3389 RENAME_SUFFIX("cached-microdescs", ".new");
3390 RENAME_SUFFIX("cached-microdescs.new", ".tmp");
3391 RENAME_SUFFIX("cached-descriptors", ".tmp");
3392 RENAME_SUFFIX("cached-descriptors", ".new");
3393 RENAME_SUFFIX("cached-descriptors.new", ".tmp");
3394 RENAME_SUFFIX("cached-extrainfo", ".tmp");
3395 RENAME_SUFFIX("cached-extrainfo", ".new");
3396 RENAME_SUFFIX("cached-extrainfo.new", ".tmp");
3397 RENAME_SUFFIX("state", ".tmp");
3398 RENAME_SUFFIX("sr-state", ".tmp");
3399 RENAME_SUFFIX("unparseable-desc", ".tmp");
3400 RENAME_SUFFIX("v3-status-votes", ".tmp");
3402 if (options->BridgeAuthoritativeDir)
3403 RENAME_SUFFIX("networkstatus-bridges", ".tmp");
3405 #define STAT_DATADIR(name) \
3406 sandbox_cfg_allow_stat_filename(&cfg, get_datadir_fname(name))
3408 #define STAT_DATADIR2(name, name2) \
3409 sandbox_cfg_allow_stat_filename(&cfg, get_datadir_fname2((name), (name2)))
3411 STAT_DATADIR(NULL);
3412 STAT_DATADIR("lock");
3413 STAT_DATADIR("state");
3414 STAT_DATADIR("router-stability");
3415 STAT_DATADIR("cached-extrainfo.new");
3418 smartlist_t *files = smartlist_new();
3419 tor_log_get_logfile_names(files);
3420 SMARTLIST_FOREACH(files, char *, file_name, {
3421 /* steals reference */
3422 sandbox_cfg_allow_open_filename(&cfg, file_name);
3424 smartlist_free(files);
3428 smartlist_t *files = smartlist_new();
3429 smartlist_t *dirs = smartlist_new();
3430 rend_services_add_filenames_to_lists(files, dirs);
3431 SMARTLIST_FOREACH(files, char *, file_name, {
3432 char *tmp_name = NULL;
3433 tor_asprintf(&tmp_name, "%s.tmp", file_name);
3434 sandbox_cfg_allow_rename(&cfg,
3435 tor_strdup(tmp_name), tor_strdup(file_name));
3436 /* steals references */
3437 sandbox_cfg_allow_open_filename(&cfg, file_name);
3438 sandbox_cfg_allow_open_filename(&cfg, tmp_name);
3440 SMARTLIST_FOREACH(dirs, char *, dir, {
3441 /* steals reference */
3442 sandbox_cfg_allow_stat_filename(&cfg, dir);
3444 smartlist_free(files);
3445 smartlist_free(dirs);
3449 char *fname;
3450 if ((fname = get_controller_cookie_file_name())) {
3451 sandbox_cfg_allow_open_filename(&cfg, fname);
3453 if ((fname = get_ext_or_auth_cookie_file_name())) {
3454 sandbox_cfg_allow_open_filename(&cfg, fname);
3458 SMARTLIST_FOREACH_BEGIN(get_configured_ports(), port_cfg_t *, port) {
3459 if (!port->is_unix_addr)
3460 continue;
3461 /* When we open an AF_UNIX address, we want permission to open the
3462 * directory that holds it. */
3463 char *dirname = tor_strdup(port->unix_addr);
3464 if (get_parent_directory(dirname) == 0) {
3465 OPEN(dirname);
3467 tor_free(dirname);
3468 sandbox_cfg_allow_chmod_filename(&cfg, tor_strdup(port->unix_addr));
3469 sandbox_cfg_allow_chown_filename(&cfg, tor_strdup(port->unix_addr));
3470 } SMARTLIST_FOREACH_END(port);
3472 if (options->DirPortFrontPage) {
3473 sandbox_cfg_allow_open_filename(&cfg,
3474 tor_strdup(options->DirPortFrontPage));
3477 // orport
3478 if (server_mode(get_options())) {
3480 OPEN_DATADIR2_SUFFIX("keys", "secret_id_key", ".tmp");
3481 OPEN_DATADIR2_SUFFIX("keys", "secret_onion_key", ".tmp");
3482 OPEN_DATADIR2_SUFFIX("keys", "secret_onion_key_ntor", ".tmp");
3483 OPEN_DATADIR2("keys", "secret_id_key.old");
3484 OPEN_DATADIR2("keys", "secret_onion_key.old");
3485 OPEN_DATADIR2("keys", "secret_onion_key_ntor.old");
3487 OPEN_DATADIR2_SUFFIX("keys", "ed25519_master_id_secret_key", ".tmp");
3488 OPEN_DATADIR2_SUFFIX("keys", "ed25519_master_id_secret_key_encrypted",
3489 ".tmp");
3490 OPEN_DATADIR2_SUFFIX("keys", "ed25519_master_id_public_key", ".tmp");
3491 OPEN_DATADIR2_SUFFIX("keys", "ed25519_signing_secret_key", ".tmp");
3492 OPEN_DATADIR2_SUFFIX("keys", "ed25519_signing_secret_key_encrypted",
3493 ".tmp");
3494 OPEN_DATADIR2_SUFFIX("keys", "ed25519_signing_public_key", ".tmp");
3495 OPEN_DATADIR2_SUFFIX("keys", "ed25519_signing_cert", ".tmp");
3497 OPEN_DATADIR2_SUFFIX("stats", "bridge-stats", ".tmp");
3498 OPEN_DATADIR2_SUFFIX("stats", "dirreq-stats", ".tmp");
3500 OPEN_DATADIR2_SUFFIX("stats", "entry-stats", ".tmp");
3501 OPEN_DATADIR2_SUFFIX("stats", "exit-stats", ".tmp");
3502 OPEN_DATADIR2_SUFFIX("stats", "buffer-stats", ".tmp");
3503 OPEN_DATADIR2_SUFFIX("stats", "conn-stats", ".tmp");
3504 OPEN_DATADIR2_SUFFIX("stats", "hidserv-stats", ".tmp");
3506 OPEN_DATADIR("approved-routers");
3507 OPEN_DATADIR_SUFFIX("fingerprint", ".tmp");
3508 OPEN_DATADIR_SUFFIX("hashed-fingerprint", ".tmp");
3509 OPEN_DATADIR_SUFFIX("router-stability", ".tmp");
3511 OPEN("/etc/resolv.conf");
3513 RENAME_SUFFIX("fingerprint", ".tmp");
3514 RENAME_SUFFIX2("keys", "secret_onion_key_ntor", ".tmp");
3515 RENAME_SUFFIX2("keys", "secret_id_key", ".tmp");
3516 RENAME_SUFFIX2("keys", "secret_id_key.old", ".tmp");
3517 RENAME_SUFFIX2("keys", "secret_onion_key", ".tmp");
3518 RENAME_SUFFIX2("keys", "secret_onion_key.old", ".tmp");
3519 RENAME_SUFFIX2("stats", "bridge-stats", ".tmp");
3520 RENAME_SUFFIX2("stats", "dirreq-stats", ".tmp");
3521 RENAME_SUFFIX2("stats", "entry-stats", ".tmp");
3522 RENAME_SUFFIX2("stats", "exit-stats", ".tmp");
3523 RENAME_SUFFIX2("stats", "buffer-stats", ".tmp");
3524 RENAME_SUFFIX2("stats", "conn-stats", ".tmp");
3525 RENAME_SUFFIX2("stats", "hidserv-stats", ".tmp");
3526 RENAME_SUFFIX("hashed-fingerprint", ".tmp");
3527 RENAME_SUFFIX("router-stability", ".tmp");
3529 RENAME_SUFFIX2("keys", "ed25519_master_id_secret_key", ".tmp");
3530 RENAME_SUFFIX2("keys", "ed25519_master_id_secret_key_encrypted", ".tmp");
3531 RENAME_SUFFIX2("keys", "ed25519_master_id_public_key", ".tmp");
3532 RENAME_SUFFIX2("keys", "ed25519_signing_secret_key", ".tmp");
3533 RENAME_SUFFIX2("keys", "ed25519_signing_cert", ".tmp");
3535 sandbox_cfg_allow_rename(&cfg,
3536 get_datadir_fname2("keys", "secret_onion_key"),
3537 get_datadir_fname2("keys", "secret_onion_key.old"));
3538 sandbox_cfg_allow_rename(&cfg,
3539 get_datadir_fname2("keys", "secret_onion_key_ntor"),
3540 get_datadir_fname2("keys", "secret_onion_key_ntor.old"));
3542 STAT_DATADIR("keys");
3543 OPEN_DATADIR("stats");
3544 STAT_DATADIR("stats");
3545 STAT_DATADIR2("stats", "dirreq-stats");
3548 init_addrinfo();
3550 return cfg;
3553 /** Main entry point for the Tor process. Called from main(). */
3554 /* This function is distinct from main() only so we can link main.c into
3555 * the unittest binary without conflicting with the unittests' main. */
3557 tor_main(int argc, char *argv[])
3559 int result = 0;
3561 #ifdef _WIN32
3562 /* Call SetProcessDEPPolicy to permanently enable DEP.
3563 The function will not resolve on earlier versions of Windows,
3564 and failure is not dangerous. */
3565 HMODULE hMod = GetModuleHandleA("Kernel32.dll");
3566 if (hMod) {
3567 typedef BOOL (WINAPI *PSETDEP)(DWORD);
3568 PSETDEP setdeppolicy = (PSETDEP)GetProcAddress(hMod,
3569 "SetProcessDEPPolicy");
3570 if (setdeppolicy) setdeppolicy(1); /* PROCESS_DEP_ENABLE */
3572 #endif
3574 configure_backtrace_handler(get_version());
3576 update_approx_time(time(NULL));
3577 tor_threads_init();
3578 init_logging(0);
3579 #ifdef USE_DMALLOC
3581 /* Instruct OpenSSL to use our internal wrappers for malloc,
3582 realloc and free. */
3583 int r = CRYPTO_set_mem_ex_functions(tor_malloc_, tor_realloc_, tor_free_);
3584 tor_assert(r);
3586 #endif
3587 #ifdef NT_SERVICE
3589 int done = 0;
3590 result = nt_service_parse_options(argc, argv, &done);
3591 if (done) return result;
3593 #endif
3594 if (tor_init(argc, argv)<0)
3595 return -1;
3597 if (get_options()->Sandbox && get_options()->command == CMD_RUN_TOR) {
3598 sandbox_cfg_t* cfg = sandbox_init_filter();
3600 if (sandbox_init(cfg)) {
3601 log_err(LD_BUG,"Failed to create syscall sandbox filter");
3602 return -1;
3605 // registering libevent rng
3606 #ifdef HAVE_EVUTIL_SECURE_RNG_SET_URANDOM_DEVICE_FILE
3607 evutil_secure_rng_set_urandom_device_file(
3608 (char*) sandbox_intern_string("/dev/urandom"));
3609 #endif
3612 monotime_init();
3614 switch (get_options()->command) {
3615 case CMD_RUN_TOR:
3616 #ifdef NT_SERVICE
3617 nt_service_set_state(SERVICE_RUNNING);
3618 #endif
3619 result = do_main_loop();
3620 break;
3621 case CMD_KEYGEN:
3622 result = load_ed_keys(get_options(), time(NULL));
3623 break;
3624 case CMD_LIST_FINGERPRINT:
3625 result = do_list_fingerprint();
3626 break;
3627 case CMD_HASH_PASSWORD:
3628 do_hash_password();
3629 result = 0;
3630 break;
3631 case CMD_VERIFY_CONFIG:
3632 if (quiet_level == 0)
3633 printf("Configuration was valid\n");
3634 result = 0;
3635 break;
3636 case CMD_DUMP_CONFIG:
3637 result = do_dump_config();
3638 break;
3639 case CMD_RUN_UNITTESTS: /* only set by test.c */
3640 default:
3641 log_warn(LD_BUG,"Illegal command number %d: internal error.",
3642 get_options()->command);
3643 result = -1;
3645 tor_cleanup();
3646 return result;