Merge remote-tracking branch 'public/decouple_init_keys'
[tor.git] / src / or / main.c
blob092014f7fac17f6608f13a59dc90088b952bdd4d
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-2015, 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.
11 **/
13 #define MAIN_PRIVATE
14 #include "or.h"
15 #include "addressmap.h"
16 #include "backtrace.h"
17 #include "buffers.h"
18 #include "channel.h"
19 #include "channeltls.h"
20 #include "circuitbuild.h"
21 #include "circuitlist.h"
22 #include "circuituse.h"
23 #include "command.h"
24 #include "config.h"
25 #include "confparse.h"
26 #include "connection.h"
27 #include "connection_edge.h"
28 #include "connection_or.h"
29 #include "control.h"
30 #include "cpuworker.h"
31 #include "crypto_s2k.h"
32 #include "directory.h"
33 #include "dirserv.h"
34 #include "dirvote.h"
35 #include "dns.h"
36 #include "dnsserv.h"
37 #include "entrynodes.h"
38 #include "geoip.h"
39 #include "hibernate.h"
40 #include "keypin.h"
41 #include "main.h"
42 #include "microdesc.h"
43 #include "networkstatus.h"
44 #include "nodelist.h"
45 #include "ntmain.h"
46 #include "onion.h"
47 #include "policies.h"
48 #include "transports.h"
49 #include "relay.h"
50 #include "rendclient.h"
51 #include "rendcommon.h"
52 #include "rendservice.h"
53 #include "rephist.h"
54 #include "router.h"
55 #include "routerkeys.h"
56 #include "routerlist.h"
57 #include "routerparse.h"
58 #include "scheduler.h"
59 #include "statefile.h"
60 #include "status.h"
61 #include "util_process.h"
62 #include "ext_orport.h"
63 #ifdef USE_DMALLOC
64 #include <dmalloc.h>
65 #include <openssl/crypto.h>
66 #endif
67 #include "memarea.h"
68 #include "sandbox.h"
70 #ifdef HAVE_EVENT2_EVENT_H
71 #include <event2/event.h>
72 #else
73 #include <event.h>
74 #endif
76 #ifdef USE_BUFFEREVENTS
77 #include <event2/bufferevent.h>
78 #endif
80 #ifdef HAVE_SYSTEMD
81 # if defined(__COVERITY__) && !defined(__INCLUDE_LEVEL__)
82 /* Systemd's use of gcc's __INCLUDE_LEVEL__ extension macro appears to confuse
83 * Coverity. Here's a kludge to unconfuse it.
85 # define __INCLUDE_LEVEL__ 2
86 # endif
87 #include <systemd/sd-daemon.h>
88 #endif
90 void evdns_shutdown(int);
92 /********* PROTOTYPES **********/
94 static void dumpmemusage(int severity);
95 static void dumpstats(int severity); /* log stats */
96 static void conn_read_callback(evutil_socket_t fd, short event, void *_conn);
97 static void conn_write_callback(evutil_socket_t fd, short event, void *_conn);
98 static void second_elapsed_callback(periodic_timer_t *timer, void *args);
99 static int conn_close_if_marked(int i);
100 static void connection_start_reading_from_linked_conn(connection_t *conn);
101 static int connection_should_read_from_linked_conn(connection_t *conn);
102 static int run_main_loop_until_done(void);
104 /********* START VARIABLES **********/
106 #ifndef USE_BUFFEREVENTS
107 int global_read_bucket; /**< Max number of bytes I can read this second. */
108 int global_write_bucket; /**< Max number of bytes I can write this second. */
110 /** Max number of relayed (bandwidth class 1) bytes I can read this second. */
111 int global_relayed_read_bucket;
112 /** Max number of relayed (bandwidth class 1) bytes I can write this second. */
113 int global_relayed_write_bucket;
114 /** What was the read bucket before the last second_elapsed_callback() call?
115 * (used to determine how many bytes we've read). */
116 static int stats_prev_global_read_bucket;
117 /** What was the write bucket before the last second_elapsed_callback() call?
118 * (used to determine how many bytes we've written). */
119 static int stats_prev_global_write_bucket;
120 #endif
122 /* DOCDOC stats_prev_n_read */
123 static uint64_t stats_prev_n_read = 0;
124 /* DOCDOC stats_prev_n_written */
125 static uint64_t stats_prev_n_written = 0;
127 /* XXX we might want to keep stats about global_relayed_*_bucket too. Or not.*/
128 /** How many bytes have we read since we started the process? */
129 static uint64_t stats_n_bytes_read = 0;
130 /** How many bytes have we written since we started the process? */
131 static uint64_t stats_n_bytes_written = 0;
132 /** What time did this process start up? */
133 time_t time_of_process_start = 0;
134 /** How many seconds have we been running? */
135 long stats_n_seconds_working = 0;
137 /** How often will we honor SIGNEWNYM requests? */
138 #define MAX_SIGNEWNYM_RATE 10
139 /** When did we last process a SIGNEWNYM request? */
140 static time_t time_of_last_signewnym = 0;
141 /** Is there a signewnym request we're currently waiting to handle? */
142 static int signewnym_is_pending = 0;
143 /** How many times have we called newnym? */
144 static unsigned newnym_epoch = 0;
146 /** Smartlist of all open connections. */
147 static smartlist_t *connection_array = NULL;
148 /** List of connections that have been marked for close and need to be freed
149 * and removed from connection_array. */
150 static smartlist_t *closeable_connection_lst = NULL;
151 /** List of linked connections that are currently reading data into their
152 * inbuf from their partner's outbuf. */
153 static smartlist_t *active_linked_connection_lst = NULL;
154 /** Flag: Set to true iff we entered the current libevent main loop via
155 * <b>loop_once</b>. If so, there's no need to trigger a loopexit in order
156 * to handle linked connections. */
157 static int called_loop_once = 0;
159 /** We set this to 1 when we've opened a circuit, so we can print a log
160 * entry to inform the user that Tor is working. We set it to 0 when
161 * we think the fact that we once opened a circuit doesn't mean we can do so
162 * any longer (a big time jump happened, when we notice our directory is
163 * heinously out-of-date, etc.
165 static int can_complete_circuits = 0;
167 /** How often do we check for router descriptors that we should download
168 * when we have too little directory info? */
169 #define GREEDY_DESCRIPTOR_RETRY_INTERVAL (10)
170 /** How often do we check for router descriptors that we should download
171 * when we have enough directory info? */
172 #define LAZY_DESCRIPTOR_RETRY_INTERVAL (60)
173 /** How often do we 'forgive' undownloadable router descriptors and attempt
174 * to download them again? */
175 #define DESCRIPTOR_FAILURE_RESET_INTERVAL (60*60)
177 /** Decides our behavior when no logs are configured/before any
178 * logs have been configured. For 0, we log notice to stdout as normal.
179 * For 1, we log warnings only. For 2, we log nothing.
181 int quiet_level = 0;
183 /********* END VARIABLES ************/
185 /****************************************************************************
187 * This section contains accessors and other methods on the connection_array
188 * variables (which are global within this file and unavailable outside it).
190 ****************************************************************************/
192 #if 0 && defined(USE_BUFFEREVENTS)
193 static void
194 free_old_inbuf(connection_t *conn)
196 if (! conn->inbuf)
197 return;
199 tor_assert(conn->outbuf);
200 tor_assert(buf_datalen(conn->inbuf) == 0);
201 tor_assert(buf_datalen(conn->outbuf) == 0);
202 buf_free(conn->inbuf);
203 buf_free(conn->outbuf);
204 conn->inbuf = conn->outbuf = NULL;
206 if (conn->read_event) {
207 event_del(conn->read_event);
208 tor_event_free(conn->read_event);
210 if (conn->write_event) {
211 event_del(conn->read_event);
212 tor_event_free(conn->write_event);
214 conn->read_event = conn->write_event = NULL;
216 #endif
218 #if defined(_WIN32) && defined(USE_BUFFEREVENTS)
219 /** Remove the kernel-space send and receive buffers for <b>s</b>. For use
220 * with IOCP only. */
221 static int
222 set_buffer_lengths_to_zero(tor_socket_t s)
224 int zero = 0;
225 int r = 0;
226 if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, (void*)&zero, sizeof(zero))) {
227 log_warn(LD_NET, "Unable to clear SO_SNDBUF");
228 r = -1;
230 if (setsockopt(s, SOL_SOCKET, SO_RCVBUF, (void*)&zero, sizeof(zero))) {
231 log_warn(LD_NET, "Unable to clear SO_RCVBUF");
232 r = -1;
234 return r;
236 #endif
238 /** Return 1 if we have successfully built a circuit, and nothing has changed
239 * to make us think that maybe we can't.
242 have_completed_a_circuit(void)
244 return can_complete_circuits;
247 /** Note that we have successfully built a circuit, so that reachability
248 * testing and introduction points and so on may be attempted. */
249 void
250 note_that_we_completed_a_circuit(void)
252 can_complete_circuits = 1;
255 /** Note that something has happened (like a clock jump, or DisableNetwork) to
256 * make us think that maybe we can't complete circuits. */
257 void
258 note_that_we_maybe_cant_complete_circuits(void)
260 can_complete_circuits = 0;
263 /** Add <b>conn</b> to the array of connections that we can poll on. The
264 * connection's socket must be set; the connection starts out
265 * non-reading and non-writing.
268 connection_add_impl(connection_t *conn, int is_connecting)
270 tor_assert(conn);
271 tor_assert(SOCKET_OK(conn->s) ||
272 conn->linked ||
273 (conn->type == CONN_TYPE_AP &&
274 TO_EDGE_CONN(conn)->is_dns_request));
276 tor_assert(conn->conn_array_index == -1); /* can only connection_add once */
277 conn->conn_array_index = smartlist_len(connection_array);
278 smartlist_add(connection_array, conn);
280 #ifdef USE_BUFFEREVENTS
281 if (connection_type_uses_bufferevent(conn)) {
282 if (SOCKET_OK(conn->s) && !conn->linked) {
284 #ifdef _WIN32
285 if (tor_libevent_using_iocp_bufferevents() &&
286 get_options()->UserspaceIOCPBuffers) {
287 set_buffer_lengths_to_zero(conn->s);
289 #endif
291 conn->bufev = bufferevent_socket_new(
292 tor_libevent_get_base(),
293 conn->s,
294 BEV_OPT_DEFER_CALLBACKS);
295 if (!conn->bufev) {
296 log_warn(LD_BUG, "Unable to create socket bufferevent");
297 smartlist_del(connection_array, conn->conn_array_index);
298 conn->conn_array_index = -1;
299 return -1;
301 if (is_connecting) {
302 /* Put the bufferevent into a "connecting" state so that we'll get
303 * a "connected" event callback on successful write. */
304 bufferevent_socket_connect(conn->bufev, NULL, 0);
306 connection_configure_bufferevent_callbacks(conn);
307 } else if (conn->linked && conn->linked_conn &&
308 connection_type_uses_bufferevent(conn->linked_conn)) {
309 tor_assert(!(SOCKET_OK(conn->s)));
310 if (!conn->bufev) {
311 struct bufferevent *pair[2] = { NULL, NULL };
312 if (bufferevent_pair_new(tor_libevent_get_base(),
313 BEV_OPT_DEFER_CALLBACKS,
314 pair) < 0) {
315 log_warn(LD_BUG, "Unable to create bufferevent pair");
316 smartlist_del(connection_array, conn->conn_array_index);
317 conn->conn_array_index = -1;
318 return -1;
320 tor_assert(pair[0]);
321 conn->bufev = pair[0];
322 conn->linked_conn->bufev = pair[1];
323 } /* else the other side already was added, and got a bufferevent_pair */
324 connection_configure_bufferevent_callbacks(conn);
325 } else {
326 tor_assert(!conn->linked);
329 if (conn->bufev)
330 tor_assert(conn->inbuf == NULL);
332 if (conn->linked_conn && conn->linked_conn->bufev)
333 tor_assert(conn->linked_conn->inbuf == NULL);
335 #else
336 (void) is_connecting;
337 #endif
339 if (!HAS_BUFFEREVENT(conn) && (SOCKET_OK(conn->s) || conn->linked)) {
340 conn->read_event = tor_event_new(tor_libevent_get_base(),
341 conn->s, EV_READ|EV_PERSIST, conn_read_callback, conn);
342 conn->write_event = tor_event_new(tor_libevent_get_base(),
343 conn->s, EV_WRITE|EV_PERSIST, conn_write_callback, conn);
344 /* XXXX CHECK FOR NULL RETURN! */
347 log_debug(LD_NET,"new conn type %s, socket %d, address %s, n_conns %d.",
348 conn_type_to_string(conn->type), (int)conn->s, conn->address,
349 smartlist_len(connection_array));
351 return 0;
354 /** Tell libevent that we don't care about <b>conn</b> any more. */
355 void
356 connection_unregister_events(connection_t *conn)
358 if (conn->read_event) {
359 if (event_del(conn->read_event))
360 log_warn(LD_BUG, "Error removing read event for %d", (int)conn->s);
361 tor_free(conn->read_event);
363 if (conn->write_event) {
364 if (event_del(conn->write_event))
365 log_warn(LD_BUG, "Error removing write event for %d", (int)conn->s);
366 tor_free(conn->write_event);
368 #ifdef USE_BUFFEREVENTS
369 if (conn->bufev) {
370 bufferevent_free(conn->bufev);
371 conn->bufev = NULL;
373 #endif
374 if (conn->type == CONN_TYPE_AP_DNS_LISTENER) {
375 dnsserv_close_listener(conn);
379 /** Remove the connection from the global list, and remove the
380 * corresponding poll entry. Calling this function will shift the last
381 * connection (if any) into the position occupied by conn.
384 connection_remove(connection_t *conn)
386 int current_index;
387 connection_t *tmp;
389 tor_assert(conn);
391 log_debug(LD_NET,"removing socket %d (type %s), n_conns now %d",
392 (int)conn->s, conn_type_to_string(conn->type),
393 smartlist_len(connection_array));
395 if (conn->type == CONN_TYPE_AP && conn->socket_family == AF_UNIX) {
396 log_info(LD_NET, "Closing SOCKS SocksSocket connection");
399 control_event_conn_bandwidth(conn);
401 tor_assert(conn->conn_array_index >= 0);
402 current_index = conn->conn_array_index;
403 connection_unregister_events(conn); /* This is redundant, but cheap. */
404 if (current_index == smartlist_len(connection_array)-1) { /* at the end */
405 smartlist_del(connection_array, current_index);
406 return 0;
409 /* replace this one with the one at the end */
410 smartlist_del(connection_array, current_index);
411 tmp = smartlist_get(connection_array, current_index);
412 tmp->conn_array_index = current_index;
414 return 0;
417 /** If <b>conn</b> is an edge conn, remove it from the list
418 * of conn's on this circuit. If it's not on an edge,
419 * flush and send destroys for all circuits on this conn.
421 * Remove it from connection_array (if applicable) and
422 * from closeable_connection_list.
424 * Then free it.
426 static void
427 connection_unlink(connection_t *conn)
429 connection_about_to_close_connection(conn);
430 if (conn->conn_array_index >= 0) {
431 connection_remove(conn);
433 if (conn->linked_conn) {
434 conn->linked_conn->linked_conn = NULL;
435 if (! conn->linked_conn->marked_for_close &&
436 conn->linked_conn->reading_from_linked_conn)
437 connection_start_reading(conn->linked_conn);
438 conn->linked_conn = NULL;
440 smartlist_remove(closeable_connection_lst, conn);
441 smartlist_remove(active_linked_connection_lst, conn);
442 if (conn->type == CONN_TYPE_EXIT) {
443 assert_connection_edge_not_dns_pending(TO_EDGE_CONN(conn));
445 if (conn->type == CONN_TYPE_OR) {
446 if (!tor_digest_is_zero(TO_OR_CONN(conn)->identity_digest))
447 connection_or_remove_from_identity_map(TO_OR_CONN(conn));
448 /* connection_unlink() can only get called if the connection
449 * was already on the closeable list, and it got there by
450 * connection_mark_for_close(), which was called from
451 * connection_or_close_normally() or
452 * connection_or_close_for_error(), so the channel should
453 * already be in CHANNEL_STATE_CLOSING, and then the
454 * connection_about_to_close_connection() goes to
455 * connection_or_about_to_close(), which calls channel_closed()
456 * to notify the channel_t layer, and closed the channel, so
457 * nothing more to do here to deal with the channel associated
458 * with an orconn.
461 connection_free(conn);
464 /** Initialize the global connection list, closeable connection list,
465 * and active connection list. */
466 STATIC void
467 init_connection_lists(void)
469 if (!connection_array)
470 connection_array = smartlist_new();
471 if (!closeable_connection_lst)
472 closeable_connection_lst = smartlist_new();
473 if (!active_linked_connection_lst)
474 active_linked_connection_lst = smartlist_new();
477 /** Schedule <b>conn</b> to be closed. **/
478 void
479 add_connection_to_closeable_list(connection_t *conn)
481 tor_assert(!smartlist_contains(closeable_connection_lst, conn));
482 tor_assert(conn->marked_for_close);
483 assert_connection_ok(conn, time(NULL));
484 smartlist_add(closeable_connection_lst, conn);
487 /** Return 1 if conn is on the closeable list, else return 0. */
489 connection_is_on_closeable_list(connection_t *conn)
491 return smartlist_contains(closeable_connection_lst, conn);
494 /** Return true iff conn is in the current poll array. */
496 connection_in_array(connection_t *conn)
498 return smartlist_contains(connection_array, conn);
501 /** Set <b>*array</b> to an array of all connections, and <b>*n</b>
502 * to the length of the array. <b>*array</b> and <b>*n</b> must not
503 * be modified.
505 smartlist_t *
506 get_connection_array(void)
508 if (!connection_array)
509 connection_array = smartlist_new();
510 return connection_array;
513 /** Provides the traffic read and written over the life of the process. */
515 MOCK_IMPL(uint64_t,
516 get_bytes_read,(void))
518 return stats_n_bytes_read;
521 /* DOCDOC get_bytes_written */
522 MOCK_IMPL(uint64_t,
523 get_bytes_written,(void))
525 return stats_n_bytes_written;
528 /** Set the event mask on <b>conn</b> to <b>events</b>. (The event
529 * mask is a bitmask whose bits are READ_EVENT and WRITE_EVENT)
531 void
532 connection_watch_events(connection_t *conn, watchable_events_t events)
534 IF_HAS_BUFFEREVENT(conn, {
535 short ev = ((short)events) & (EV_READ|EV_WRITE);
536 short old_ev = bufferevent_get_enabled(conn->bufev);
537 if ((ev & ~old_ev) != 0) {
538 bufferevent_enable(conn->bufev, ev);
540 if ((old_ev & ~ev) != 0) {
541 bufferevent_disable(conn->bufev, old_ev & ~ev);
543 return;
545 if (events & READ_EVENT)
546 connection_start_reading(conn);
547 else
548 connection_stop_reading(conn);
550 if (events & WRITE_EVENT)
551 connection_start_writing(conn);
552 else
553 connection_stop_writing(conn);
556 /** Return true iff <b>conn</b> is listening for read events. */
558 connection_is_reading(connection_t *conn)
560 tor_assert(conn);
562 IF_HAS_BUFFEREVENT(conn,
563 return (bufferevent_get_enabled(conn->bufev) & EV_READ) != 0;
565 return conn->reading_from_linked_conn ||
566 (conn->read_event && event_pending(conn->read_event, EV_READ, NULL));
569 /** Tell the main loop to stop notifying <b>conn</b> of any read events. */
570 MOCK_IMPL(void,
571 connection_stop_reading,(connection_t *conn))
573 tor_assert(conn);
575 IF_HAS_BUFFEREVENT(conn, {
576 bufferevent_disable(conn->bufev, EV_READ);
577 return;
580 tor_assert(conn->read_event);
582 if (conn->linked) {
583 conn->reading_from_linked_conn = 0;
584 connection_stop_reading_from_linked_conn(conn);
585 } else {
586 if (event_del(conn->read_event))
587 log_warn(LD_NET, "Error from libevent setting read event state for %d "
588 "to unwatched: %s",
589 (int)conn->s,
590 tor_socket_strerror(tor_socket_errno(conn->s)));
594 /** Tell the main loop to start notifying <b>conn</b> of any read events. */
595 MOCK_IMPL(void,
596 connection_start_reading,(connection_t *conn))
598 tor_assert(conn);
600 IF_HAS_BUFFEREVENT(conn, {
601 bufferevent_enable(conn->bufev, EV_READ);
602 return;
605 tor_assert(conn->read_event);
607 if (conn->linked) {
608 conn->reading_from_linked_conn = 1;
609 if (connection_should_read_from_linked_conn(conn))
610 connection_start_reading_from_linked_conn(conn);
611 } else {
612 if (event_add(conn->read_event, NULL))
613 log_warn(LD_NET, "Error from libevent setting read event state for %d "
614 "to watched: %s",
615 (int)conn->s,
616 tor_socket_strerror(tor_socket_errno(conn->s)));
620 /** Return true iff <b>conn</b> is listening for write events. */
622 connection_is_writing(connection_t *conn)
624 tor_assert(conn);
626 IF_HAS_BUFFEREVENT(conn,
627 return (bufferevent_get_enabled(conn->bufev) & EV_WRITE) != 0;
630 return conn->writing_to_linked_conn ||
631 (conn->write_event && event_pending(conn->write_event, EV_WRITE, NULL));
634 /** Tell the main loop to stop notifying <b>conn</b> of any write events. */
635 MOCK_IMPL(void,
636 connection_stop_writing,(connection_t *conn))
638 tor_assert(conn);
640 IF_HAS_BUFFEREVENT(conn, {
641 bufferevent_disable(conn->bufev, EV_WRITE);
642 return;
645 tor_assert(conn->write_event);
647 if (conn->linked) {
648 conn->writing_to_linked_conn = 0;
649 if (conn->linked_conn)
650 connection_stop_reading_from_linked_conn(conn->linked_conn);
651 } else {
652 if (event_del(conn->write_event))
653 log_warn(LD_NET, "Error from libevent setting write event state for %d "
654 "to unwatched: %s",
655 (int)conn->s,
656 tor_socket_strerror(tor_socket_errno(conn->s)));
660 /** Tell the main loop to start notifying <b>conn</b> of any write events. */
661 MOCK_IMPL(void,
662 connection_start_writing,(connection_t *conn))
664 tor_assert(conn);
666 IF_HAS_BUFFEREVENT(conn, {
667 bufferevent_enable(conn->bufev, EV_WRITE);
668 return;
671 tor_assert(conn->write_event);
673 if (conn->linked) {
674 conn->writing_to_linked_conn = 1;
675 if (conn->linked_conn &&
676 connection_should_read_from_linked_conn(conn->linked_conn))
677 connection_start_reading_from_linked_conn(conn->linked_conn);
678 } else {
679 if (event_add(conn->write_event, NULL))
680 log_warn(LD_NET, "Error from libevent setting write event state for %d "
681 "to watched: %s",
682 (int)conn->s,
683 tor_socket_strerror(tor_socket_errno(conn->s)));
687 /** Return true iff <b>conn</b> is linked conn, and reading from the conn
688 * linked to it would be good and feasible. (Reading is "feasible" if the
689 * other conn exists and has data in its outbuf, and is "good" if we have our
690 * reading_from_linked_conn flag set and the other conn has its
691 * writing_to_linked_conn flag set.)*/
692 static int
693 connection_should_read_from_linked_conn(connection_t *conn)
695 if (conn->linked && conn->reading_from_linked_conn) {
696 if (! conn->linked_conn ||
697 (conn->linked_conn->writing_to_linked_conn &&
698 buf_datalen(conn->linked_conn->outbuf)))
699 return 1;
701 return 0;
704 /** Helper: Tell the main loop to begin reading bytes into <b>conn</b> from
705 * its linked connection, if it is not doing so already. Called by
706 * connection_start_reading and connection_start_writing as appropriate. */
707 static void
708 connection_start_reading_from_linked_conn(connection_t *conn)
710 tor_assert(conn);
711 tor_assert(conn->linked == 1);
713 if (!conn->active_on_link) {
714 conn->active_on_link = 1;
715 smartlist_add(active_linked_connection_lst, conn);
716 if (!called_loop_once) {
717 /* This is the first event on the list; we won't be in LOOP_ONCE mode,
718 * so we need to make sure that the event_base_loop() actually exits at
719 * the end of its run through the current connections and lets us
720 * activate read events for linked connections. */
721 struct timeval tv = { 0, 0 };
722 tor_event_base_loopexit(tor_libevent_get_base(), &tv);
724 } else {
725 tor_assert(smartlist_contains(active_linked_connection_lst, conn));
729 /** Tell the main loop to stop reading bytes into <b>conn</b> from its linked
730 * connection, if is currently doing so. Called by connection_stop_reading,
731 * connection_stop_writing, and connection_read. */
732 void
733 connection_stop_reading_from_linked_conn(connection_t *conn)
735 tor_assert(conn);
736 tor_assert(conn->linked == 1);
738 if (conn->active_on_link) {
739 conn->active_on_link = 0;
740 /* FFFF We could keep an index here so we can smartlist_del
741 * cleanly. On the other hand, this doesn't show up on profiles,
742 * so let's leave it alone for now. */
743 smartlist_remove(active_linked_connection_lst, conn);
744 } else {
745 tor_assert(!smartlist_contains(active_linked_connection_lst, conn));
749 /** Close all connections that have been scheduled to get closed. */
750 STATIC void
751 close_closeable_connections(void)
753 int i;
754 for (i = 0; i < smartlist_len(closeable_connection_lst); ) {
755 connection_t *conn = smartlist_get(closeable_connection_lst, i);
756 if (conn->conn_array_index < 0) {
757 connection_unlink(conn); /* blow it away right now */
758 } else {
759 if (!conn_close_if_marked(conn->conn_array_index))
760 ++i;
765 /** Libevent callback: this gets invoked when (connection_t*)<b>conn</b> has
766 * some data to read. */
767 static void
768 conn_read_callback(evutil_socket_t fd, short event, void *_conn)
770 connection_t *conn = _conn;
771 (void)fd;
772 (void)event;
774 log_debug(LD_NET,"socket %d wants to read.",(int)conn->s);
776 /* assert_connection_ok(conn, time(NULL)); */
778 if (connection_handle_read(conn) < 0) {
779 if (!conn->marked_for_close) {
780 #ifndef _WIN32
781 log_warn(LD_BUG,"Unhandled error on read for %s connection "
782 "(fd %d); removing",
783 conn_type_to_string(conn->type), (int)conn->s);
784 tor_fragile_assert();
785 #endif
786 if (CONN_IS_EDGE(conn))
787 connection_edge_end_errno(TO_EDGE_CONN(conn));
788 connection_mark_for_close(conn);
791 assert_connection_ok(conn, time(NULL));
793 if (smartlist_len(closeable_connection_lst))
794 close_closeable_connections();
797 /** Libevent callback: this gets invoked when (connection_t*)<b>conn</b> has
798 * some data to write. */
799 static void
800 conn_write_callback(evutil_socket_t fd, short events, void *_conn)
802 connection_t *conn = _conn;
803 (void)fd;
804 (void)events;
806 LOG_FN_CONN(conn, (LOG_DEBUG, LD_NET, "socket %d wants to write.",
807 (int)conn->s));
809 /* assert_connection_ok(conn, time(NULL)); */
811 if (connection_handle_write(conn, 0) < 0) {
812 if (!conn->marked_for_close) {
813 /* this connection is broken. remove it. */
814 log_fn(LOG_WARN,LD_BUG,
815 "unhandled error on write for %s connection (fd %d); removing",
816 conn_type_to_string(conn->type), (int)conn->s);
817 tor_fragile_assert();
818 if (CONN_IS_EDGE(conn)) {
819 /* otherwise we cry wolf about duplicate close */
820 edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
821 if (!edge_conn->end_reason)
822 edge_conn->end_reason = END_STREAM_REASON_INTERNAL;
823 edge_conn->edge_has_sent_end = 1;
825 connection_close_immediate(conn); /* So we don't try to flush. */
826 connection_mark_for_close(conn);
829 assert_connection_ok(conn, time(NULL));
831 if (smartlist_len(closeable_connection_lst))
832 close_closeable_connections();
835 /** If the connection at connection_array[i] is marked for close, then:
836 * - If it has data that it wants to flush, try to flush it.
837 * - If it _still_ has data to flush, and conn->hold_open_until_flushed is
838 * true, then leave the connection open and return.
839 * - Otherwise, remove the connection from connection_array and from
840 * all other lists, close it, and free it.
841 * Returns 1 if the connection was closed, 0 otherwise.
843 static int
844 conn_close_if_marked(int i)
846 connection_t *conn;
847 int retval;
848 time_t now;
850 conn = smartlist_get(connection_array, i);
851 if (!conn->marked_for_close)
852 return 0; /* nothing to see here, move along */
853 now = time(NULL);
854 assert_connection_ok(conn, now);
855 /* assert_all_pending_dns_resolves_ok(); */
857 #ifdef USE_BUFFEREVENTS
858 if (conn->bufev) {
859 if (conn->hold_open_until_flushed &&
860 evbuffer_get_length(bufferevent_get_output(conn->bufev))) {
861 /* don't close yet. */
862 return 0;
864 if (conn->linked_conn && ! conn->linked_conn->marked_for_close) {
865 /* We need to do this explicitly so that the linked connection
866 * notices that there was an EOF. */
867 bufferevent_flush(conn->bufev, EV_WRITE, BEV_FINISHED);
870 #endif
872 log_debug(LD_NET,"Cleaning up connection (fd "TOR_SOCKET_T_FORMAT").",
873 conn->s);
875 /* If the connection we are about to close was trying to connect to
876 a proxy server and failed, the client won't be able to use that
877 proxy. We should warn the user about this. */
878 if (conn->proxy_state == PROXY_INFANT)
879 log_failed_proxy_connection(conn);
881 IF_HAS_BUFFEREVENT(conn, goto unlink);
882 if ((SOCKET_OK(conn->s) || conn->linked_conn) &&
883 connection_wants_to_flush(conn)) {
884 /* s == -1 means it's an incomplete edge connection, or that the socket
885 * has already been closed as unflushable. */
886 ssize_t sz = connection_bucket_write_limit(conn, now);
887 if (!conn->hold_open_until_flushed)
888 log_info(LD_NET,
889 "Conn (addr %s, fd %d, type %s, state %d) marked, but wants "
890 "to flush %d bytes. (Marked at %s:%d)",
891 escaped_safe_str_client(conn->address),
892 (int)conn->s, conn_type_to_string(conn->type), conn->state,
893 (int)conn->outbuf_flushlen,
894 conn->marked_for_close_file, conn->marked_for_close);
895 if (conn->linked_conn) {
896 retval = move_buf_to_buf(conn->linked_conn->inbuf, conn->outbuf,
897 &conn->outbuf_flushlen);
898 if (retval >= 0) {
899 /* The linked conn will notice that it has data when it notices that
900 * we're gone. */
901 connection_start_reading_from_linked_conn(conn->linked_conn);
903 log_debug(LD_GENERAL, "Flushed last %d bytes from a linked conn; "
904 "%d left; flushlen %d; wants-to-flush==%d", retval,
905 (int)connection_get_outbuf_len(conn),
906 (int)conn->outbuf_flushlen,
907 connection_wants_to_flush(conn));
908 } else if (connection_speaks_cells(conn)) {
909 if (conn->state == OR_CONN_STATE_OPEN) {
910 retval = flush_buf_tls(TO_OR_CONN(conn)->tls, conn->outbuf, sz,
911 &conn->outbuf_flushlen);
912 } else
913 retval = -1; /* never flush non-open broken tls connections */
914 } else {
915 retval = flush_buf(conn->s, conn->outbuf, sz, &conn->outbuf_flushlen);
917 if (retval >= 0 && /* Technically, we could survive things like
918 TLS_WANT_WRITE here. But don't bother for now. */
919 conn->hold_open_until_flushed && connection_wants_to_flush(conn)) {
920 if (retval > 0) {
921 LOG_FN_CONN(conn, (LOG_INFO,LD_NET,
922 "Holding conn (fd %d) open for more flushing.",
923 (int)conn->s));
924 conn->timestamp_lastwritten = now; /* reset so we can flush more */
925 } else if (sz == 0) {
926 /* Also, retval==0. If we get here, we didn't want to write anything
927 * (because of rate-limiting) and we didn't. */
929 /* Connection must flush before closing, but it's being rate-limited.
930 * Let's remove from Libevent, and mark it as blocked on bandwidth
931 * so it will be re-added on next token bucket refill. Prevents
932 * busy Libevent loops where we keep ending up here and returning
933 * 0 until we are no longer blocked on bandwidth.
935 if (connection_is_writing(conn)) {
936 conn->write_blocked_on_bw = 1;
937 connection_stop_writing(conn);
939 if (connection_is_reading(conn)) {
940 /* XXXX024 We should make this code unreachable; if a connection is
941 * marked for close and flushing, there is no point in reading to it
942 * at all. Further, checking at this point is a bit of a hack: it
943 * would make much more sense to react in
944 * connection_handle_read_impl, or to just stop reading in
945 * mark_and_flush */
946 #if 0
947 #define MARKED_READING_RATE 180
948 static ratelim_t marked_read_lim = RATELIM_INIT(MARKED_READING_RATE);
949 char *m;
950 if ((m = rate_limit_log(&marked_read_lim, now))) {
951 log_warn(LD_BUG, "Marked connection (fd %d, type %s, state %s) "
952 "is still reading; that shouldn't happen.%s",
953 (int)conn->s, conn_type_to_string(conn->type),
954 conn_state_to_string(conn->type, conn->state), m);
955 tor_free(m);
957 #endif
958 conn->read_blocked_on_bw = 1;
959 connection_stop_reading(conn);
962 return 0;
964 if (connection_wants_to_flush(conn)) {
965 log_fn(LOG_INFO, LD_NET, "We stalled too much while trying to write %d "
966 "bytes to address %s. If this happens a lot, either "
967 "something is wrong with your network connection, or "
968 "something is wrong with theirs. "
969 "(fd %d, type %s, state %d, marked at %s:%d).",
970 (int)connection_get_outbuf_len(conn),
971 escaped_safe_str_client(conn->address),
972 (int)conn->s, conn_type_to_string(conn->type), conn->state,
973 conn->marked_for_close_file,
974 conn->marked_for_close);
978 #ifdef USE_BUFFEREVENTS
979 unlink:
980 #endif
981 connection_unlink(conn); /* unlink, remove, free */
982 return 1;
985 /** Implementation for directory_all_unreachable. This is done in a callback,
986 * since otherwise it would complicate Tor's control-flow graph beyond all
987 * reason.
989 static void
990 directory_all_unreachable_cb(evutil_socket_t fd, short event, void *arg)
992 (void)fd;
993 (void)event;
994 (void)arg;
996 connection_t *conn;
998 while ((conn = connection_get_by_type_state(CONN_TYPE_AP,
999 AP_CONN_STATE_CIRCUIT_WAIT))) {
1000 entry_connection_t *entry_conn = TO_ENTRY_CONN(conn);
1001 log_notice(LD_NET,
1002 "Is your network connection down? "
1003 "Failing connection to '%s:%d'.",
1004 safe_str_client(entry_conn->socks_request->address),
1005 entry_conn->socks_request->port);
1006 connection_mark_unattached_ap(entry_conn,
1007 END_STREAM_REASON_NET_UNREACHABLE);
1009 control_event_general_status(LOG_ERR, "DIR_ALL_UNREACHABLE");
1012 static struct event *directory_all_unreachable_cb_event = NULL;
1014 /** We've just tried every dirserver we know about, and none of
1015 * them were reachable. Assume the network is down. Change state
1016 * so next time an application connection arrives we'll delay it
1017 * and try another directory fetch. Kill off all the circuit_wait
1018 * streams that are waiting now, since they will all timeout anyway.
1020 void
1021 directory_all_unreachable(time_t now)
1023 (void)now;
1025 stats_n_seconds_working=0; /* reset it */
1027 if (!directory_all_unreachable_cb_event) {
1028 directory_all_unreachable_cb_event =
1029 tor_event_new(tor_libevent_get_base(),
1030 -1, EV_READ, directory_all_unreachable_cb, NULL);
1031 tor_assert(directory_all_unreachable_cb_event);
1034 event_active(directory_all_unreachable_cb_event, EV_READ, 1);
1037 /** This function is called whenever we successfully pull down some new
1038 * network statuses or server descriptors. */
1039 void
1040 directory_info_has_arrived(time_t now, int from_cache)
1042 const or_options_t *options = get_options();
1044 if (!router_have_minimum_dir_info()) {
1045 int quiet = from_cache ||
1046 directory_too_idle_to_fetch_descriptors(options, now);
1047 tor_log(quiet ? LOG_INFO : LOG_NOTICE, LD_DIR,
1048 "I learned some more directory information, but not enough to "
1049 "build a circuit: %s", get_dir_info_status_string());
1050 update_all_descriptor_downloads(now);
1051 return;
1052 } else {
1053 if (directory_fetches_from_authorities(options)) {
1054 update_all_descriptor_downloads(now);
1057 /* if we have enough dir info, then update our guard status with
1058 * whatever we just learned. */
1059 entry_guards_compute_status(options, now);
1060 /* Don't even bother trying to get extrainfo until the rest of our
1061 * directory info is up-to-date */
1062 if (options->DownloadExtraInfo)
1063 update_extrainfo_downloads(now);
1066 if (server_mode(options) && !net_is_disabled() && !from_cache &&
1067 (have_completed_a_circuit() || !any_predicted_circuits(now)))
1068 consider_testing_reachability(1, 1);
1071 /** Perform regular maintenance tasks for a single connection. This
1072 * function gets run once per second per connection by run_scheduled_events.
1074 static void
1075 run_connection_housekeeping(int i, time_t now)
1077 cell_t cell;
1078 connection_t *conn = smartlist_get(connection_array, i);
1079 const or_options_t *options = get_options();
1080 or_connection_t *or_conn;
1081 channel_t *chan = NULL;
1082 int have_any_circuits;
1083 int past_keepalive =
1084 now >= conn->timestamp_lastwritten + options->KeepalivePeriod;
1086 if (conn->outbuf && !connection_get_outbuf_len(conn) &&
1087 conn->type == CONN_TYPE_OR)
1088 TO_OR_CONN(conn)->timestamp_lastempty = now;
1090 if (conn->marked_for_close) {
1091 /* nothing to do here */
1092 return;
1095 /* Expire any directory connections that haven't been active (sent
1096 * if a server or received if a client) for 5 min */
1097 if (conn->type == CONN_TYPE_DIR &&
1098 ((DIR_CONN_IS_SERVER(conn) &&
1099 conn->timestamp_lastwritten
1100 + options->TestingDirConnectionMaxStall < now) ||
1101 (!DIR_CONN_IS_SERVER(conn) &&
1102 conn->timestamp_lastread
1103 + options->TestingDirConnectionMaxStall < now))) {
1104 log_info(LD_DIR,"Expiring wedged directory conn (fd %d, purpose %d)",
1105 (int)conn->s, conn->purpose);
1106 /* This check is temporary; it's to let us know whether we should consider
1107 * parsing partial serverdesc responses. */
1108 if (conn->purpose == DIR_PURPOSE_FETCH_SERVERDESC &&
1109 connection_get_inbuf_len(conn) >= 1024) {
1110 log_info(LD_DIR,"Trying to extract information from wedged server desc "
1111 "download.");
1112 connection_dir_reached_eof(TO_DIR_CONN(conn));
1113 } else {
1114 connection_mark_for_close(conn);
1116 return;
1119 if (!connection_speaks_cells(conn))
1120 return; /* we're all done here, the rest is just for OR conns */
1122 /* If we haven't written to an OR connection for a while, then either nuke
1123 the connection or send a keepalive, depending. */
1125 or_conn = TO_OR_CONN(conn);
1126 #ifdef USE_BUFFEREVENTS
1127 tor_assert(conn->bufev);
1128 #else
1129 tor_assert(conn->outbuf);
1130 #endif
1132 chan = TLS_CHAN_TO_BASE(or_conn->chan);
1133 tor_assert(chan);
1135 if (channel_num_circuits(chan) != 0) {
1136 have_any_circuits = 1;
1137 chan->timestamp_last_had_circuits = now;
1138 } else {
1139 have_any_circuits = 0;
1142 if (channel_is_bad_for_new_circs(TLS_CHAN_TO_BASE(or_conn->chan)) &&
1143 ! have_any_circuits) {
1144 /* It's bad for new circuits, and has no unmarked circuits on it:
1145 * mark it now. */
1146 log_info(LD_OR,
1147 "Expiring non-used OR connection to fd %d (%s:%d) [Too old].",
1148 (int)conn->s, conn->address, conn->port);
1149 if (conn->state == OR_CONN_STATE_CONNECTING)
1150 connection_or_connect_failed(TO_OR_CONN(conn),
1151 END_OR_CONN_REASON_TIMEOUT,
1152 "Tor gave up on the connection");
1153 connection_or_close_normally(TO_OR_CONN(conn), 1);
1154 } else if (!connection_state_is_open(conn)) {
1155 if (past_keepalive) {
1156 /* We never managed to actually get this connection open and happy. */
1157 log_info(LD_OR,"Expiring non-open OR connection to fd %d (%s:%d).",
1158 (int)conn->s,conn->address, conn->port);
1159 connection_or_close_normally(TO_OR_CONN(conn), 0);
1161 } else if (we_are_hibernating() &&
1162 ! have_any_circuits &&
1163 !connection_get_outbuf_len(conn)) {
1164 /* We're hibernating, there's no circuits, and nothing to flush.*/
1165 log_info(LD_OR,"Expiring non-used OR connection to fd %d (%s:%d) "
1166 "[Hibernating or exiting].",
1167 (int)conn->s,conn->address, conn->port);
1168 connection_or_close_normally(TO_OR_CONN(conn), 1);
1169 } else if (!have_any_circuits &&
1170 now - or_conn->idle_timeout >=
1171 chan->timestamp_last_had_circuits) {
1172 log_info(LD_OR,"Expiring non-used OR connection to fd %d (%s:%d) "
1173 "[no circuits for %d; timeout %d; %scanonical].",
1174 (int)conn->s, conn->address, conn->port,
1175 (int)(now - chan->timestamp_last_had_circuits),
1176 or_conn->idle_timeout,
1177 or_conn->is_canonical ? "" : "non");
1178 connection_or_close_normally(TO_OR_CONN(conn), 0);
1179 } else if (
1180 now >= or_conn->timestamp_lastempty + options->KeepalivePeriod*10 &&
1181 now >= conn->timestamp_lastwritten + options->KeepalivePeriod*10) {
1182 log_fn(LOG_PROTOCOL_WARN,LD_PROTOCOL,
1183 "Expiring stuck OR connection to fd %d (%s:%d). (%d bytes to "
1184 "flush; %d seconds since last write)",
1185 (int)conn->s, conn->address, conn->port,
1186 (int)connection_get_outbuf_len(conn),
1187 (int)(now-conn->timestamp_lastwritten));
1188 connection_or_close_normally(TO_OR_CONN(conn), 0);
1189 } else if (past_keepalive && !connection_get_outbuf_len(conn)) {
1190 /* send a padding cell */
1191 log_fn(LOG_DEBUG,LD_OR,"Sending keepalive to (%s:%d)",
1192 conn->address, conn->port);
1193 memset(&cell,0,sizeof(cell_t));
1194 cell.command = CELL_PADDING;
1195 connection_or_write_cell_to_buf(&cell, or_conn);
1199 /** Honor a NEWNYM request: make future requests unlinkable to past
1200 * requests. */
1201 static void
1202 signewnym_impl(time_t now)
1204 const or_options_t *options = get_options();
1205 if (!proxy_mode(options)) {
1206 log_info(LD_CONTROL, "Ignoring SIGNAL NEWNYM because client functionality "
1207 "is disabled.");
1208 return;
1211 circuit_mark_all_dirty_circs_as_unusable();
1212 addressmap_clear_transient();
1213 rend_client_purge_state();
1214 time_of_last_signewnym = now;
1215 signewnym_is_pending = 0;
1217 ++newnym_epoch;
1219 control_event_signal(SIGNEWNYM);
1222 /** Return the number of times that signewnym has been called. */
1223 unsigned
1224 get_signewnym_epoch(void)
1226 return newnym_epoch;
1229 typedef struct {
1230 time_t last_rotated_x509_certificate;
1231 time_t check_v3_certificate;
1232 time_t check_listeners;
1233 time_t download_networkstatus;
1234 time_t try_getting_descriptors;
1235 time_t reset_descriptor_failures;
1236 time_t add_entropy;
1237 time_t write_bridge_status_file;
1238 time_t downrate_stability;
1239 time_t save_stability;
1240 time_t clean_caches;
1241 time_t recheck_bandwidth;
1242 time_t check_for_expired_networkstatus;
1243 time_t write_stats_files;
1244 time_t write_bridge_stats;
1245 time_t check_port_forwarding;
1246 time_t launch_reachability_tests;
1247 time_t retry_dns_init;
1248 time_t next_heartbeat;
1249 time_t check_descriptor;
1250 /** When do we next launch DNS wildcarding checks? */
1251 time_t check_for_correct_dns;
1252 /** When do we next make sure our Ed25519 keys aren't about to expire? */
1253 time_t check_ed_keys;
1255 } time_to_t;
1257 static time_to_t time_to = {
1258 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
1261 /** Reset all the time_to's so we'll do all our actions again as if we
1262 * just started up.
1263 * Useful if our clock just moved back a long time from the future,
1264 * so we don't wait until that future arrives again before acting.
1266 void
1267 reset_all_main_loop_timers(void)
1269 memset(&time_to, 0, sizeof(time_to));
1273 * Update our schedule so that we'll check whether we need to update our
1274 * descriptor immediately, rather than after up to CHECK_DESCRIPTOR_INTERVAL
1275 * seconds.
1277 void
1278 reschedule_descriptor_update_check(void)
1280 time_to.check_descriptor = 0;
1283 /** Perform regular maintenance tasks. This function gets run once per
1284 * second by second_elapsed_callback().
1286 static void
1287 run_scheduled_events(time_t now)
1289 static int should_init_bridge_stats = 1;
1290 const or_options_t *options = get_options();
1292 int is_server = server_mode(options);
1293 int i;
1294 int have_dir_info;
1296 /* 0. See if we've been asked to shut down and our timeout has
1297 * expired; or if our bandwidth limits are exhausted and we
1298 * should hibernate; or if it's time to wake up from hibernation.
1300 consider_hibernation(now);
1302 /* 0b. If we've deferred a signewnym, make sure it gets handled
1303 * eventually. */
1304 if (signewnym_is_pending &&
1305 time_of_last_signewnym + MAX_SIGNEWNYM_RATE <= now) {
1306 log_info(LD_CONTROL, "Honoring delayed NEWNYM request");
1307 signewnym_impl(now);
1310 /* 0c. If we've deferred log messages for the controller, handle them now */
1311 flush_pending_log_callbacks();
1313 /* 1a. Every MIN_ONION_KEY_LIFETIME seconds, rotate the onion keys,
1314 * shut down and restart all cpuworkers, and update the directory if
1315 * necessary.
1317 if (is_server &&
1318 get_onion_key_set_at()+MIN_ONION_KEY_LIFETIME < now) {
1319 log_info(LD_GENERAL,"Rotating onion key.");
1320 rotate_onion_key();
1321 cpuworkers_rotate_keyinfo();
1322 if (router_rebuild_descriptor(1)<0) {
1323 log_info(LD_CONFIG, "Couldn't rebuild router descriptor");
1325 if (advertised_server_mode() && !options->DisableNetwork)
1326 router_upload_dir_desc_to_dirservers(0);
1329 if (is_server && time_to.check_ed_keys < now) {
1330 if (should_make_new_ed_keys(options, now)) {
1331 if (load_ed_keys(options, now) < 0 ||
1332 generate_ed_link_cert(options, now)) {
1333 log_err(LD_OR, "Unable to update Ed25519 keys! Exiting.");
1334 tor_cleanup();
1335 exit(0);
1338 time_to.check_ed_keys = now + 30;
1341 if (!should_delay_dir_fetches(options, NULL) &&
1342 time_to.try_getting_descriptors < now) {
1343 update_all_descriptor_downloads(now);
1344 update_extrainfo_downloads(now);
1345 if (router_have_minimum_dir_info())
1346 time_to.try_getting_descriptors = now + LAZY_DESCRIPTOR_RETRY_INTERVAL;
1347 else
1348 time_to.try_getting_descriptors = now + GREEDY_DESCRIPTOR_RETRY_INTERVAL;
1351 if (time_to.reset_descriptor_failures < now) {
1352 router_reset_descriptor_download_failures();
1353 time_to.reset_descriptor_failures =
1354 now + DESCRIPTOR_FAILURE_RESET_INTERVAL;
1357 if (options->UseBridges && !options->DisableNetwork)
1358 fetch_bridge_descriptors(options, now);
1360 /* 1b. Every MAX_SSL_KEY_LIFETIME_INTERNAL seconds, we change our
1361 * TLS context. */
1362 if (!time_to.last_rotated_x509_certificate)
1363 time_to.last_rotated_x509_certificate = now;
1364 if (time_to.last_rotated_x509_certificate +
1365 MAX_SSL_KEY_LIFETIME_INTERNAL < now) {
1366 log_info(LD_GENERAL,"Rotating tls context.");
1367 if (router_initialize_tls_context() < 0) {
1368 log_warn(LD_BUG, "Error reinitializing TLS context");
1369 /* XXX is it a bug here, that we just keep going? -RD */
1371 time_to.last_rotated_x509_certificate = now;
1372 /* We also make sure to rotate the TLS connections themselves if they've
1373 * been up for too long -- but that's done via is_bad_for_new_circs in
1374 * connection_run_housekeeping() above. */
1377 if (time_to.add_entropy < now) {
1378 if (time_to.add_entropy) {
1379 /* We already seeded once, so don't die on failure. */
1380 crypto_seed_rng();
1382 /** How often do we add more entropy to OpenSSL's RNG pool? */
1383 #define ENTROPY_INTERVAL (60*60)
1384 time_to.add_entropy = now + ENTROPY_INTERVAL;
1387 /* 1c. If we have to change the accounting interval or record
1388 * bandwidth used in this accounting interval, do so. */
1389 if (accounting_is_enabled(options))
1390 accounting_run_housekeeping(now);
1392 if (time_to.launch_reachability_tests < now &&
1393 (authdir_mode_tests_reachability(options)) &&
1394 !net_is_disabled()) {
1395 time_to.launch_reachability_tests = now + REACHABILITY_TEST_INTERVAL;
1396 /* try to determine reachability of the other Tor relays */
1397 dirserv_test_reachability(now);
1400 /* 1d. Periodically, we discount older stability information so that new
1401 * stability info counts more, and save the stability information to disk as
1402 * appropriate. */
1403 if (time_to.downrate_stability < now)
1404 time_to.downrate_stability = rep_hist_downrate_old_runs(now);
1405 if (authdir_mode_tests_reachability(options)) {
1406 if (time_to.save_stability < now) {
1407 if (time_to.save_stability && rep_hist_record_mtbf_data(now, 1)<0) {
1408 log_warn(LD_GENERAL, "Couldn't store mtbf data.");
1410 #define SAVE_STABILITY_INTERVAL (30*60)
1411 time_to.save_stability = now + SAVE_STABILITY_INTERVAL;
1415 /* 1e. Periodically, if we're a v3 authority, we check whether our cert is
1416 * close to expiring and warn the admin if it is. */
1417 if (time_to.check_v3_certificate < now) {
1418 v3_authority_check_key_expiry();
1419 #define CHECK_V3_CERTIFICATE_INTERVAL (5*60)
1420 time_to.check_v3_certificate = now + CHECK_V3_CERTIFICATE_INTERVAL;
1423 /* 1f. Check whether our networkstatus has expired.
1425 if (time_to.check_for_expired_networkstatus < now) {
1426 networkstatus_t *ns = networkstatus_get_latest_consensus();
1427 /*XXXX RD: This value needs to be the same as REASONABLY_LIVE_TIME in
1428 * networkstatus_get_reasonably_live_consensus(), but that value is way
1429 * way too high. Arma: is the bridge issue there resolved yet? -NM */
1430 #define NS_EXPIRY_SLOP (24*60*60)
1431 if (ns && ns->valid_until < now+NS_EXPIRY_SLOP &&
1432 router_have_minimum_dir_info()) {
1433 router_dir_info_changed();
1435 #define CHECK_EXPIRED_NS_INTERVAL (2*60)
1436 time_to.check_for_expired_networkstatus = now + CHECK_EXPIRED_NS_INTERVAL;
1439 /* 1g. Check whether we should write statistics to disk.
1441 if (time_to.write_stats_files < now) {
1442 #define CHECK_WRITE_STATS_INTERVAL (60*60)
1443 time_t next_time_to_write_stats_files = (time_to.write_stats_files > 0 ?
1444 time_to.write_stats_files : now) + CHECK_WRITE_STATS_INTERVAL;
1445 if (options->CellStatistics) {
1446 time_t next_write =
1447 rep_hist_buffer_stats_write(time_to.write_stats_files);
1448 if (next_write && next_write < next_time_to_write_stats_files)
1449 next_time_to_write_stats_files = next_write;
1451 if (options->DirReqStatistics) {
1452 time_t next_write = geoip_dirreq_stats_write(time_to.write_stats_files);
1453 if (next_write && next_write < next_time_to_write_stats_files)
1454 next_time_to_write_stats_files = next_write;
1456 if (options->EntryStatistics) {
1457 time_t next_write = geoip_entry_stats_write(time_to.write_stats_files);
1458 if (next_write && next_write < next_time_to_write_stats_files)
1459 next_time_to_write_stats_files = next_write;
1461 if (options->HiddenServiceStatistics) {
1462 time_t next_write = rep_hist_hs_stats_write(time_to.write_stats_files);
1463 if (next_write && next_write < next_time_to_write_stats_files)
1464 next_time_to_write_stats_files = next_write;
1466 if (options->ExitPortStatistics) {
1467 time_t next_write = rep_hist_exit_stats_write(time_to.write_stats_files);
1468 if (next_write && next_write < next_time_to_write_stats_files)
1469 next_time_to_write_stats_files = next_write;
1471 if (options->ConnDirectionStatistics) {
1472 time_t next_write = rep_hist_conn_stats_write(time_to.write_stats_files);
1473 if (next_write && next_write < next_time_to_write_stats_files)
1474 next_time_to_write_stats_files = next_write;
1476 if (options->BridgeAuthoritativeDir) {
1477 time_t next_write = rep_hist_desc_stats_write(time_to.write_stats_files);
1478 if (next_write && next_write < next_time_to_write_stats_files)
1479 next_time_to_write_stats_files = next_write;
1481 time_to.write_stats_files = next_time_to_write_stats_files;
1484 /* 1h. Check whether we should write bridge statistics to disk.
1486 if (should_record_bridge_info(options)) {
1487 if (time_to.write_bridge_stats < now) {
1488 if (should_init_bridge_stats) {
1489 /* (Re-)initialize bridge statistics. */
1490 geoip_bridge_stats_init(now);
1491 time_to.write_bridge_stats = now + WRITE_STATS_INTERVAL;
1492 should_init_bridge_stats = 0;
1493 } else {
1494 /* Possibly write bridge statistics to disk and ask when to write
1495 * them next time. */
1496 time_to.write_bridge_stats = geoip_bridge_stats_write(
1497 time_to.write_bridge_stats);
1500 } else if (!should_init_bridge_stats) {
1501 /* Bridge mode was turned off. Ensure that stats are re-initialized
1502 * next time bridge mode is turned on. */
1503 should_init_bridge_stats = 1;
1506 /* Remove old information from rephist and the rend cache. */
1507 if (time_to.clean_caches < now) {
1508 rep_history_clean(now - options->RephistTrackTime);
1509 rend_cache_clean(now);
1510 rend_cache_clean_v2_descs_as_dir(now, 0);
1511 microdesc_cache_rebuild(NULL, 0);
1512 #define CLEAN_CACHES_INTERVAL (30*60)
1513 time_to.clean_caches = now + CLEAN_CACHES_INTERVAL;
1515 /* We don't keep entries that are more than five minutes old so we try to
1516 * clean it as soon as we can since we want to make sure the client waits
1517 * as little as possible for reachability reasons. */
1518 rend_cache_failure_clean(now);
1520 #define RETRY_DNS_INTERVAL (10*60)
1521 /* If we're a server and initializing dns failed, retry periodically. */
1522 if (time_to.retry_dns_init < now) {
1523 time_to.retry_dns_init = now + RETRY_DNS_INTERVAL;
1524 if (is_server && has_dns_init_failed())
1525 dns_init();
1528 /* 2. Periodically, we consider force-uploading our descriptor
1529 * (if we've passed our internal checks). */
1531 /** How often do we check whether part of our router info has changed in a
1532 * way that would require an upload? That includes checking whether our IP
1533 * address has changed. */
1534 #define CHECK_DESCRIPTOR_INTERVAL (60)
1536 /* 2b. Once per minute, regenerate and upload the descriptor if the old
1537 * one is inaccurate. */
1538 if (time_to.check_descriptor < now && !options->DisableNetwork) {
1539 static int dirport_reachability_count = 0;
1540 time_to.check_descriptor = now + CHECK_DESCRIPTOR_INTERVAL;
1541 check_descriptor_bandwidth_changed(now);
1542 check_descriptor_ipaddress_changed(now);
1543 mark_my_descriptor_dirty_if_too_old(now);
1544 consider_publishable_server(0);
1545 /* also, check religiously for reachability, if it's within the first
1546 * 20 minutes of our uptime. */
1547 if (is_server &&
1548 (have_completed_a_circuit() || !any_predicted_circuits(now)) &&
1549 !we_are_hibernating()) {
1550 if (stats_n_seconds_working < TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT) {
1551 consider_testing_reachability(1, dirport_reachability_count==0);
1552 if (++dirport_reachability_count > 5)
1553 dirport_reachability_count = 0;
1554 } else if (time_to.recheck_bandwidth < now) {
1555 /* If we haven't checked for 12 hours and our bandwidth estimate is
1556 * low, do another bandwidth test. This is especially important for
1557 * bridges, since they might go long periods without much use. */
1558 const routerinfo_t *me = router_get_my_routerinfo();
1559 if (time_to.recheck_bandwidth && me &&
1560 me->bandwidthcapacity < me->bandwidthrate &&
1561 me->bandwidthcapacity < 51200) {
1562 reset_bandwidth_test();
1564 #define BANDWIDTH_RECHECK_INTERVAL (12*60*60)
1565 time_to.recheck_bandwidth = now + BANDWIDTH_RECHECK_INTERVAL;
1569 /* If any networkstatus documents are no longer recent, we need to
1570 * update all the descriptors' running status. */
1571 /* Remove dead routers. */
1572 routerlist_remove_old_routers();
1575 /* 2c. Every minute (or every second if TestingTorNetwork), check
1576 * whether we want to download any networkstatus documents. */
1578 /* How often do we check whether we should download network status
1579 * documents? */
1580 #define networkstatus_dl_check_interval(o) ((o)->TestingTorNetwork ? 1 : 60)
1582 if (!should_delay_dir_fetches(options, NULL) &&
1583 time_to.download_networkstatus < now) {
1584 time_to.download_networkstatus =
1585 now + networkstatus_dl_check_interval(options);
1586 update_networkstatus_downloads(now);
1589 /* 2c. Let directory voting happen. */
1590 if (authdir_mode_v3(options))
1591 dirvote_act(options, now);
1593 /* 3a. Every second, we examine pending circuits and prune the
1594 * ones which have been pending for more than a few seconds.
1595 * We do this before step 4, so it can try building more if
1596 * it's not comfortable with the number of available circuits.
1598 /* (If our circuit build timeout can ever become lower than a second (which
1599 * it can't, currently), we should do this more often.) */
1600 circuit_expire_building();
1602 /* 3b. Also look at pending streams and prune the ones that 'began'
1603 * a long time ago but haven't gotten a 'connected' yet.
1604 * Do this before step 4, so we can put them back into pending
1605 * state to be picked up by the new circuit.
1607 connection_ap_expire_beginning();
1609 /* 3c. And expire connections that we've held open for too long.
1611 connection_expire_held_open();
1613 /* 3d. And every 60 seconds, we relaunch listeners if any died. */
1614 if (!net_is_disabled() && time_to.check_listeners < now) {
1615 retry_all_listeners(NULL, NULL, 0);
1616 time_to.check_listeners = now+60;
1619 /* 4. Every second, we try a new circuit if there are no valid
1620 * circuits. Every NewCircuitPeriod seconds, we expire circuits
1621 * that became dirty more than MaxCircuitDirtiness seconds ago,
1622 * and we make a new circ if there are no clean circuits.
1624 have_dir_info = router_have_minimum_dir_info();
1625 if (have_dir_info && !net_is_disabled()) {
1626 circuit_build_needed_circs(now);
1627 } else {
1628 circuit_expire_old_circs_as_needed(now);
1631 /* every 10 seconds, but not at the same second as other such events */
1632 if (now % 10 == 5)
1633 circuit_expire_old_circuits_serverside(now);
1635 /* 5. We do housekeeping for each connection... */
1636 connection_or_set_bad_connections(NULL, 0);
1637 for (i=0;i<smartlist_len(connection_array);i++) {
1638 run_connection_housekeeping(i, now);
1641 /* 6. And remove any marked circuits... */
1642 circuit_close_all_marked();
1644 /* 7. And upload service descriptors if necessary. */
1645 if (have_completed_a_circuit() && !net_is_disabled()) {
1646 rend_consider_services_upload(now);
1647 rend_consider_descriptor_republication();
1650 /* 8. and blow away any connections that need to die. have to do this now,
1651 * because if we marked a conn for close and left its socket -1, then
1652 * we'll pass it to poll/select and bad things will happen.
1654 close_closeable_connections();
1656 /* 8b. And if anything in our state is ready to get flushed to disk, we
1657 * flush it. */
1658 or_state_save(now);
1660 /* 8c. Do channel cleanup just like for connections */
1661 channel_run_cleanup();
1662 channel_listener_run_cleanup();
1664 /* 9. and if we're an exit node, check whether our DNS is telling stories
1665 * to us. */
1666 if (!net_is_disabled() &&
1667 public_server_mode(options) &&
1668 time_to.check_for_correct_dns < now &&
1669 ! router_my_exit_policy_is_reject_star()) {
1670 if (!time_to.check_for_correct_dns) {
1671 time_to.check_for_correct_dns =
1672 crypto_rand_time_range(now + 60, now + 180);
1673 } else {
1674 dns_launch_correctness_checks();
1675 time_to.check_for_correct_dns = now + 12*3600 +
1676 crypto_rand_int(12*3600);
1680 /* 10. write bridge networkstatus file to disk */
1681 if (options->BridgeAuthoritativeDir &&
1682 time_to.write_bridge_status_file < now) {
1683 networkstatus_dump_bridge_status_to_file(now);
1684 #define BRIDGE_STATUSFILE_INTERVAL (30*60)
1685 time_to.write_bridge_status_file = now+BRIDGE_STATUSFILE_INTERVAL;
1688 /* 11. check the port forwarding app */
1689 if (!net_is_disabled() &&
1690 time_to.check_port_forwarding < now &&
1691 options->PortForwarding &&
1692 is_server) {
1693 #define PORT_FORWARDING_CHECK_INTERVAL 5
1694 smartlist_t *ports_to_forward = get_list_of_ports_to_forward();
1695 if (ports_to_forward) {
1696 tor_check_port_forwarding(options->PortForwardingHelper,
1697 ports_to_forward,
1698 now);
1700 SMARTLIST_FOREACH(ports_to_forward, char *, cp, tor_free(cp));
1701 smartlist_free(ports_to_forward);
1703 time_to.check_port_forwarding = now+PORT_FORWARDING_CHECK_INTERVAL;
1706 /* 11b. check pending unconfigured managed proxies */
1707 if (!net_is_disabled() && pt_proxies_configuration_pending())
1708 pt_configure_remaining_proxies();
1710 /* 12. write the heartbeat message */
1711 if (options->HeartbeatPeriod &&
1712 time_to.next_heartbeat <= now) {
1713 if (time_to.next_heartbeat) /* don't log the first heartbeat */
1714 log_heartbeat(now);
1715 time_to.next_heartbeat = now+options->HeartbeatPeriod;
1719 /** Timer: used to invoke second_elapsed_callback() once per second. */
1720 static periodic_timer_t *second_timer = NULL;
1721 /** Number of libevent errors in the last second: we die if we get too many. */
1722 static int n_libevent_errors = 0;
1724 /** Libevent callback: invoked once every second. */
1725 static void
1726 second_elapsed_callback(periodic_timer_t *timer, void *arg)
1728 /* XXXX This could be sensibly refactored into multiple callbacks, and we
1729 * could use Libevent's timers for this rather than checking the current
1730 * time against a bunch of timeouts every second. */
1731 static time_t current_second = 0;
1732 time_t now;
1733 size_t bytes_written;
1734 size_t bytes_read;
1735 int seconds_elapsed;
1736 const or_options_t *options = get_options();
1737 (void)timer;
1738 (void)arg;
1740 n_libevent_errors = 0;
1742 /* log_notice(LD_GENERAL, "Tick."); */
1743 now = time(NULL);
1744 update_approx_time(now);
1746 /* the second has rolled over. check more stuff. */
1747 seconds_elapsed = current_second ? (int)(now - current_second) : 0;
1748 #ifdef USE_BUFFEREVENTS
1750 uint64_t cur_read,cur_written;
1751 connection_get_rate_limit_totals(&cur_read, &cur_written);
1752 bytes_written = (size_t)(cur_written - stats_prev_n_written);
1753 bytes_read = (size_t)(cur_read - stats_prev_n_read);
1754 stats_n_bytes_read += bytes_read;
1755 stats_n_bytes_written += bytes_written;
1756 if (accounting_is_enabled(options) && seconds_elapsed >= 0)
1757 accounting_add_bytes(bytes_read, bytes_written, seconds_elapsed);
1758 stats_prev_n_written = cur_written;
1759 stats_prev_n_read = cur_read;
1761 #else
1762 bytes_read = (size_t)(stats_n_bytes_read - stats_prev_n_read);
1763 bytes_written = (size_t)(stats_n_bytes_written - stats_prev_n_written);
1764 stats_prev_n_read = stats_n_bytes_read;
1765 stats_prev_n_written = stats_n_bytes_written;
1766 #endif
1768 control_event_bandwidth_used((uint32_t)bytes_read,(uint32_t)bytes_written);
1769 control_event_stream_bandwidth_used();
1770 control_event_conn_bandwidth_used();
1771 control_event_circ_bandwidth_used();
1772 control_event_circuit_cell_stats();
1774 if (server_mode(options) &&
1775 !net_is_disabled() &&
1776 seconds_elapsed > 0 &&
1777 have_completed_a_circuit() &&
1778 stats_n_seconds_working / TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT !=
1779 (stats_n_seconds_working+seconds_elapsed) /
1780 TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT) {
1781 /* every 20 minutes, check and complain if necessary */
1782 const routerinfo_t *me = router_get_my_routerinfo();
1783 if (me && !check_whether_orport_reachable()) {
1784 char *address = tor_dup_ip(me->addr);
1785 log_warn(LD_CONFIG,"Your server (%s:%d) has not managed to confirm that "
1786 "its ORPort is reachable. Please check your firewalls, ports, "
1787 "address, /etc/hosts file, etc.",
1788 address, me->or_port);
1789 control_event_server_status(LOG_WARN,
1790 "REACHABILITY_FAILED ORADDRESS=%s:%d",
1791 address, me->or_port);
1792 tor_free(address);
1795 if (me && !check_whether_dirport_reachable()) {
1796 char *address = tor_dup_ip(me->addr);
1797 log_warn(LD_CONFIG,
1798 "Your server (%s:%d) has not managed to confirm that its "
1799 "DirPort is reachable. Please check your firewalls, ports, "
1800 "address, /etc/hosts file, etc.",
1801 address, me->dir_port);
1802 control_event_server_status(LOG_WARN,
1803 "REACHABILITY_FAILED DIRADDRESS=%s:%d",
1804 address, me->dir_port);
1805 tor_free(address);
1809 /** If more than this many seconds have elapsed, probably the clock
1810 * jumped: doesn't count. */
1811 #define NUM_JUMPED_SECONDS_BEFORE_WARN 100
1812 if (seconds_elapsed < -NUM_JUMPED_SECONDS_BEFORE_WARN ||
1813 seconds_elapsed >= NUM_JUMPED_SECONDS_BEFORE_WARN) {
1814 circuit_note_clock_jumped(seconds_elapsed);
1815 } else if (seconds_elapsed > 0)
1816 stats_n_seconds_working += seconds_elapsed;
1818 run_scheduled_events(now);
1820 current_second = now; /* remember which second it is, for next time */
1823 #ifdef HAVE_SYSTEMD_209
1824 static periodic_timer_t *systemd_watchdog_timer = NULL;
1826 /** Libevent callback: invoked to reset systemd watchdog. */
1827 static void
1828 systemd_watchdog_callback(periodic_timer_t *timer, void *arg)
1830 (void)timer;
1831 (void)arg;
1832 sd_notify(0, "WATCHDOG=1");
1834 #endif
1836 #ifndef USE_BUFFEREVENTS
1837 /** Timer: used to invoke refill_callback(). */
1838 static periodic_timer_t *refill_timer = NULL;
1840 /** Libevent callback: invoked periodically to refill token buckets
1841 * and count r/w bytes. It is only used when bufferevents are disabled. */
1842 static void
1843 refill_callback(periodic_timer_t *timer, void *arg)
1845 static struct timeval current_millisecond;
1846 struct timeval now;
1848 size_t bytes_written;
1849 size_t bytes_read;
1850 int milliseconds_elapsed = 0;
1851 int seconds_rolled_over = 0;
1853 const or_options_t *options = get_options();
1855 (void)timer;
1856 (void)arg;
1858 tor_gettimeofday(&now);
1860 /* If this is our first time, no time has passed. */
1861 if (current_millisecond.tv_sec) {
1862 long mdiff = tv_mdiff(&current_millisecond, &now);
1863 if (mdiff > INT_MAX)
1864 mdiff = INT_MAX;
1865 milliseconds_elapsed = (int)mdiff;
1866 seconds_rolled_over = (int)(now.tv_sec - current_millisecond.tv_sec);
1869 bytes_written = stats_prev_global_write_bucket - global_write_bucket;
1870 bytes_read = stats_prev_global_read_bucket - global_read_bucket;
1872 stats_n_bytes_read += bytes_read;
1873 stats_n_bytes_written += bytes_written;
1874 if (accounting_is_enabled(options) && milliseconds_elapsed >= 0)
1875 accounting_add_bytes(bytes_read, bytes_written, seconds_rolled_over);
1877 if (milliseconds_elapsed > 0)
1878 connection_bucket_refill(milliseconds_elapsed, (time_t)now.tv_sec);
1880 stats_prev_global_read_bucket = global_read_bucket;
1881 stats_prev_global_write_bucket = global_write_bucket;
1883 current_millisecond = now; /* remember what time it is, for next time */
1885 #endif
1887 #ifndef _WIN32
1888 /** Called when a possibly ignorable libevent error occurs; ensures that we
1889 * don't get into an infinite loop by ignoring too many errors from
1890 * libevent. */
1891 static int
1892 got_libevent_error(void)
1894 if (++n_libevent_errors > 8) {
1895 log_err(LD_NET, "Too many libevent errors in one second; dying");
1896 return -1;
1898 return 0;
1900 #endif
1902 #define UPTIME_CUTOFF_FOR_NEW_BANDWIDTH_TEST (6*60*60)
1904 /** Called when our IP address seems to have changed. <b>at_interface</b>
1905 * should be true if we detected a change in our interface, and false if we
1906 * detected a change in our published address. */
1907 void
1908 ip_address_changed(int at_interface)
1910 int server = server_mode(get_options());
1912 if (at_interface) {
1913 if (! server) {
1914 /* Okay, change our keys. */
1915 if (init_keys_client() < 0)
1916 log_warn(LD_GENERAL, "Unable to rotate keys after IP change!");
1918 } else {
1919 if (server) {
1920 if (stats_n_seconds_working > UPTIME_CUTOFF_FOR_NEW_BANDWIDTH_TEST)
1921 reset_bandwidth_test();
1922 stats_n_seconds_working = 0;
1923 router_reset_reachability();
1924 mark_my_descriptor_dirty("IP address changed");
1928 dns_servers_relaunch_checks();
1931 /** Forget what we've learned about the correctness of our DNS servers, and
1932 * start learning again. */
1933 void
1934 dns_servers_relaunch_checks(void)
1936 if (server_mode(get_options())) {
1937 dns_reset_correctness_checks();
1938 time_to.check_for_correct_dns = 0;
1942 /** Called when we get a SIGHUP: reload configuration files and keys,
1943 * retry all connections, and so on. */
1944 static int
1945 do_hup(void)
1947 const or_options_t *options = get_options();
1949 #ifdef USE_DMALLOC
1950 dmalloc_log_stats();
1951 dmalloc_log_changed(0, 1, 0, 0);
1952 #endif
1954 log_notice(LD_GENERAL,"Received reload signal (hup). Reloading config and "
1955 "resetting internal state.");
1956 if (accounting_is_enabled(options))
1957 accounting_record_bandwidth_usage(time(NULL), get_or_state());
1959 router_reset_warnings();
1960 routerlist_reset_warnings();
1961 /* first, reload config variables, in case they've changed */
1962 if (options->ReloadTorrcOnSIGHUP) {
1963 /* no need to provide argc/v, they've been cached in init_from_config */
1964 if (options_init_from_torrc(0, NULL) < 0) {
1965 log_err(LD_CONFIG,"Reading config failed--see warnings above. "
1966 "For usage, try -h.");
1967 return -1;
1969 options = get_options(); /* they have changed now */
1970 /* Logs are only truncated the first time they are opened, but were
1971 probably intended to be cleaned up on signal. */
1972 if (options->TruncateLogFile)
1973 truncate_logs();
1974 } else {
1975 char *msg = NULL;
1976 log_notice(LD_GENERAL, "Not reloading config file: the controller told "
1977 "us not to.");
1978 /* Make stuff get rescanned, reloaded, etc. */
1979 if (set_options((or_options_t*)options, &msg) < 0) {
1980 if (!msg)
1981 msg = tor_strdup("Unknown error");
1982 log_warn(LD_GENERAL, "Unable to re-set previous options: %s", msg);
1983 tor_free(msg);
1986 if (authdir_mode_handles_descs(options, -1)) {
1987 /* reload the approved-routers file */
1988 if (dirserv_load_fingerprint_file() < 0) {
1989 /* warnings are logged from dirserv_load_fingerprint_file() directly */
1990 log_info(LD_GENERAL, "Error reloading fingerprints. "
1991 "Continuing with old list.");
1995 /* Rotate away from the old dirty circuits. This has to be done
1996 * after we've read the new options, but before we start using
1997 * circuits for directory fetches. */
1998 circuit_mark_all_dirty_circs_as_unusable();
2000 /* retry appropriate downloads */
2001 router_reset_status_download_failures();
2002 router_reset_descriptor_download_failures();
2003 if (!options->DisableNetwork)
2004 update_networkstatus_downloads(time(NULL));
2006 /* We'll retry routerstatus downloads in about 10 seconds; no need to
2007 * force a retry there. */
2009 if (server_mode(options)) {
2010 /* Update cpuworker and dnsworker processes, so they get up-to-date
2011 * configuration options. */
2012 cpuworkers_rotate_keyinfo();
2013 dns_reset();
2015 return 0;
2018 /** Tor main loop. */
2020 do_main_loop(void)
2022 time_t now;
2024 /* initialize dns resolve map, spawn workers if needed */
2025 if (dns_init() < 0) {
2026 if (get_options()->ServerDNSAllowBrokenConfig)
2027 log_warn(LD_GENERAL, "Couldn't set up any working nameservers. "
2028 "Network not up yet? Will try again soon.");
2029 else {
2030 log_err(LD_GENERAL,"Error initializing dns subsystem; exiting. To "
2031 "retry instead, set the ServerDNSAllowBrokenResolvConf option.");
2035 #ifdef USE_BUFFEREVENTS
2036 log_warn(LD_GENERAL, "Tor was compiled with the --enable-bufferevents "
2037 "option. This is still experimental, and might cause strange "
2038 "bugs. If you want a more stable Tor, be sure to build without "
2039 "--enable-bufferevents.");
2040 #endif
2042 handle_signals(1);
2044 /* load the private keys, if we're supposed to have them, and set up the
2045 * TLS context. */
2046 if (! client_identity_key_is_set()) {
2047 if (init_keys() < 0) {
2048 log_err(LD_BUG,"Error initializing keys; exiting");
2049 return -1;
2053 /* Set up our buckets */
2054 connection_bucket_init();
2055 #ifndef USE_BUFFEREVENTS
2056 stats_prev_global_read_bucket = global_read_bucket;
2057 stats_prev_global_write_bucket = global_write_bucket;
2058 #endif
2060 /* initialize the bootstrap status events to know we're starting up */
2061 control_event_bootstrap(BOOTSTRAP_STATUS_STARTING, 0);
2063 /* Initialize the keypinning log. */
2064 if (authdir_mode_v3(get_options())) {
2065 char *fname = get_datadir_fname("key-pinning-journal");
2066 int r = 0;
2067 if (keypin_load_journal(fname)<0) {
2068 log_err(LD_DIR, "Error loading key-pinning journal: %s",strerror(errno));
2069 r = -1;
2071 if (keypin_open_journal(fname)<0) {
2072 log_err(LD_DIR, "Error opening key-pinning journal: %s",strerror(errno));
2073 r = -1;
2075 tor_free(fname);
2076 if (r)
2077 return r;
2080 /* This is the old name for key-pinning-journal. These got corrupted
2081 * in a couple of cases by #16530, so we started over. See #16580 for
2082 * the rationale and for other options we didn't take. We can remove
2083 * this code once all the authorities that ran 0.2.7.1-alpha-dev are
2084 * upgraded.
2086 char *fname = get_datadir_fname("key-pinning-entries");
2087 unlink(fname);
2088 tor_free(fname);
2091 if (trusted_dirs_reload_certs()) {
2092 log_warn(LD_DIR,
2093 "Couldn't load all cached v3 certificates. Starting anyway.");
2095 if (router_reload_consensus_networkstatus()) {
2096 return -1;
2098 /* load the routers file, or assign the defaults. */
2099 if (router_reload_router_list()) {
2100 return -1;
2102 /* load the networkstatuses. (This launches a download for new routers as
2103 * appropriate.)
2105 now = time(NULL);
2106 directory_info_has_arrived(now, 1);
2108 if (server_mode(get_options())) {
2109 /* launch cpuworkers. Need to do this *after* we've read the onion key. */
2110 cpu_init();
2113 /* set up once-a-second callback. */
2114 if (! second_timer) {
2115 struct timeval one_second;
2116 one_second.tv_sec = 1;
2117 one_second.tv_usec = 0;
2119 second_timer = periodic_timer_new(tor_libevent_get_base(),
2120 &one_second,
2121 second_elapsed_callback,
2122 NULL);
2123 tor_assert(second_timer);
2126 #ifdef HAVE_SYSTEMD_209
2127 uint64_t watchdog_delay;
2128 /* set up systemd watchdog notification. */
2129 if (sd_watchdog_enabled(1, &watchdog_delay) > 0) {
2130 if (! systemd_watchdog_timer) {
2131 struct timeval watchdog;
2132 /* The manager will "act on" us if we don't send them a notification
2133 * every 'watchdog_delay' microseconds. So, send notifications twice
2134 * that often. */
2135 watchdog_delay /= 2;
2136 watchdog.tv_sec = watchdog_delay / 1000000;
2137 watchdog.tv_usec = watchdog_delay % 1000000;
2139 systemd_watchdog_timer = periodic_timer_new(tor_libevent_get_base(),
2140 &watchdog,
2141 systemd_watchdog_callback,
2142 NULL);
2143 tor_assert(systemd_watchdog_timer);
2146 #endif
2148 #ifndef USE_BUFFEREVENTS
2149 if (!refill_timer) {
2150 struct timeval refill_interval;
2151 int msecs = get_options()->TokenBucketRefillInterval;
2153 refill_interval.tv_sec = msecs/1000;
2154 refill_interval.tv_usec = (msecs%1000)*1000;
2156 refill_timer = periodic_timer_new(tor_libevent_get_base(),
2157 &refill_interval,
2158 refill_callback,
2159 NULL);
2160 tor_assert(refill_timer);
2162 #endif
2164 #ifdef HAVE_SYSTEMD
2166 const int r = sd_notify(0, "READY=1");
2167 if (r < 0) {
2168 log_warn(LD_GENERAL, "Unable to send readiness to systemd: %s",
2169 strerror(r));
2170 } else if (r > 0) {
2171 log_notice(LD_GENERAL, "Signaled readiness to systemd");
2172 } else {
2173 log_info(LD_GENERAL, "Systemd NOTIFY_SOCKET not present.");
2176 #endif
2178 return run_main_loop_until_done();
2182 * Run the main loop a single time. Return 0 for "exit"; -1 for "exit with
2183 * error", and 1 for "run this again."
2185 static int
2186 run_main_loop_once(void)
2188 int loop_result;
2190 if (nt_service_is_stopping())
2191 return 0;
2193 #ifndef _WIN32
2194 /* Make it easier to tell whether libevent failure is our fault or not. */
2195 errno = 0;
2196 #endif
2197 /* All active linked conns should get their read events activated. */
2198 SMARTLIST_FOREACH(active_linked_connection_lst, connection_t *, conn,
2199 event_active(conn->read_event, EV_READ, 1));
2200 called_loop_once = smartlist_len(active_linked_connection_lst) ? 1 : 0;
2202 update_approx_time(time(NULL));
2204 /* poll until we have an event, or the second ends, or until we have
2205 * some active linked connections to trigger events for. */
2206 loop_result = event_base_loop(tor_libevent_get_base(),
2207 called_loop_once ? EVLOOP_ONCE : 0);
2209 /* let catch() handle things like ^c, and otherwise don't worry about it */
2210 if (loop_result < 0) {
2211 int e = tor_socket_errno(-1);
2212 /* let the program survive things like ^z */
2213 if (e != EINTR && !ERRNO_IS_EINPROGRESS(e)) {
2214 log_err(LD_NET,"libevent call with %s failed: %s [%d]",
2215 tor_libevent_get_method(), tor_socket_strerror(e), e);
2216 return -1;
2217 #ifndef _WIN32
2218 } else if (e == EINVAL) {
2219 log_warn(LD_NET, "EINVAL from libevent: should you upgrade libevent?");
2220 if (got_libevent_error())
2221 return -1;
2222 #endif
2223 } else {
2224 if (ERRNO_IS_EINPROGRESS(e))
2225 log_warn(LD_BUG,
2226 "libevent call returned EINPROGRESS? Please report.");
2227 log_debug(LD_NET,"libevent call interrupted.");
2228 /* You can't trust the results of this poll(). Go back to the
2229 * top of the big for loop. */
2230 return 1;
2234 return 1;
2237 /** Run the run_main_loop_once() function until it declares itself done,
2238 * and return its final return value.
2240 * Shadow won't invoke this function, so don't fill it up with things.
2242 static int
2243 run_main_loop_until_done(void)
2245 int loop_result = 1;
2246 do {
2247 loop_result = run_main_loop_once();
2248 } while (loop_result == 1);
2249 return loop_result;
2252 #ifndef _WIN32 /* Only called when we're willing to use signals */
2253 /** Libevent callback: invoked when we get a signal.
2255 static void
2256 signal_callback(int fd, short events, void *arg)
2258 const int *sigptr = arg;
2259 const int sig = *sigptr;
2260 (void)fd;
2261 (void)events;
2263 process_signal(sig);
2265 #endif
2267 /** Do the work of acting on a signal received in <b>sig</b> */
2268 void
2269 process_signal(int sig)
2271 switch (sig)
2273 case SIGTERM:
2274 log_notice(LD_GENERAL,"Catching signal TERM, exiting cleanly.");
2275 tor_cleanup();
2276 exit(0);
2277 break;
2278 case SIGINT:
2279 if (!server_mode(get_options())) { /* do it now */
2280 log_notice(LD_GENERAL,"Interrupt: exiting cleanly.");
2281 tor_cleanup();
2282 exit(0);
2284 #ifdef HAVE_SYSTEMD
2285 sd_notify(0, "STOPPING=1");
2286 #endif
2287 hibernate_begin_shutdown();
2288 break;
2289 #ifdef SIGPIPE
2290 case SIGPIPE:
2291 log_debug(LD_GENERAL,"Caught SIGPIPE. Ignoring.");
2292 break;
2293 #endif
2294 case SIGUSR1:
2295 /* prefer to log it at INFO, but make sure we always see it */
2296 dumpstats(get_min_log_level()<LOG_INFO ? get_min_log_level() : LOG_INFO);
2297 control_event_signal(sig);
2298 break;
2299 case SIGUSR2:
2300 switch_logs_debug();
2301 log_debug(LD_GENERAL,"Caught USR2, going to loglevel debug. "
2302 "Send HUP to change back.");
2303 control_event_signal(sig);
2304 break;
2305 case SIGHUP:
2306 #ifdef HAVE_SYSTEMD
2307 sd_notify(0, "RELOADING=1");
2308 #endif
2309 if (do_hup() < 0) {
2310 log_warn(LD_CONFIG,"Restart failed (config error?). Exiting.");
2311 tor_cleanup();
2312 exit(1);
2314 #ifdef HAVE_SYSTEMD
2315 sd_notify(0, "READY=1");
2316 #endif
2317 control_event_signal(sig);
2318 break;
2319 #ifdef SIGCHLD
2320 case SIGCHLD:
2321 notify_pending_waitpid_callbacks();
2322 break;
2323 #endif
2324 case SIGNEWNYM: {
2325 time_t now = time(NULL);
2326 if (time_of_last_signewnym + MAX_SIGNEWNYM_RATE > now) {
2327 signewnym_is_pending = 1;
2328 log_notice(LD_CONTROL,
2329 "Rate limiting NEWNYM request: delaying by %d second(s)",
2330 (int)(MAX_SIGNEWNYM_RATE+time_of_last_signewnym-now));
2331 } else {
2332 signewnym_impl(now);
2334 break;
2336 case SIGCLEARDNSCACHE:
2337 addressmap_clear_transient();
2338 control_event_signal(sig);
2339 break;
2340 case SIGHEARTBEAT:
2341 log_heartbeat(time(NULL));
2342 control_event_signal(sig);
2343 break;
2347 /** Returns Tor's uptime. */
2348 MOCK_IMPL(long,
2349 get_uptime,(void))
2351 return stats_n_seconds_working;
2354 extern uint64_t rephist_total_alloc;
2355 extern uint32_t rephist_total_num;
2358 * Write current memory usage information to the log.
2360 static void
2361 dumpmemusage(int severity)
2363 connection_dump_buffer_mem_stats(severity);
2364 tor_log(severity, LD_GENERAL, "In rephist: "U64_FORMAT" used by %d Tors.",
2365 U64_PRINTF_ARG(rephist_total_alloc), rephist_total_num);
2366 dump_routerlist_mem_usage(severity);
2367 dump_cell_pool_usage(severity);
2368 dump_dns_mem_usage(severity);
2369 tor_log_mallinfo(severity);
2372 /** Write all statistics to the log, with log level <b>severity</b>. Called
2373 * in response to a SIGUSR1. */
2374 static void
2375 dumpstats(int severity)
2377 time_t now = time(NULL);
2378 time_t elapsed;
2379 size_t rbuf_cap, wbuf_cap, rbuf_len, wbuf_len;
2381 tor_log(severity, LD_GENERAL, "Dumping stats:");
2383 SMARTLIST_FOREACH_BEGIN(connection_array, connection_t *, conn) {
2384 int i = conn_sl_idx;
2385 tor_log(severity, LD_GENERAL,
2386 "Conn %d (socket %d) type %d (%s), state %d (%s), created %d secs ago",
2387 i, (int)conn->s, conn->type, conn_type_to_string(conn->type),
2388 conn->state, conn_state_to_string(conn->type, conn->state),
2389 (int)(now - conn->timestamp_created));
2390 if (!connection_is_listener(conn)) {
2391 tor_log(severity,LD_GENERAL,
2392 "Conn %d is to %s:%d.", i,
2393 safe_str_client(conn->address),
2394 conn->port);
2395 tor_log(severity,LD_GENERAL,
2396 "Conn %d: %d bytes waiting on inbuf (len %d, last read %d secs ago)",
2398 (int)connection_get_inbuf_len(conn),
2399 (int)buf_allocation(conn->inbuf),
2400 (int)(now - conn->timestamp_lastread));
2401 tor_log(severity,LD_GENERAL,
2402 "Conn %d: %d bytes waiting on outbuf "
2403 "(len %d, last written %d secs ago)",i,
2404 (int)connection_get_outbuf_len(conn),
2405 (int)buf_allocation(conn->outbuf),
2406 (int)(now - conn->timestamp_lastwritten));
2407 if (conn->type == CONN_TYPE_OR) {
2408 or_connection_t *or_conn = TO_OR_CONN(conn);
2409 if (or_conn->tls) {
2410 if (tor_tls_get_buffer_sizes(or_conn->tls, &rbuf_cap, &rbuf_len,
2411 &wbuf_cap, &wbuf_len) == 0) {
2412 tor_log(severity, LD_GENERAL,
2413 "Conn %d: %d/%d bytes used on OpenSSL read buffer; "
2414 "%d/%d bytes used on write buffer.",
2415 i, (int)rbuf_len, (int)rbuf_cap, (int)wbuf_len, (int)wbuf_cap);
2420 circuit_dump_by_conn(conn, severity); /* dump info about all the circuits
2421 * using this conn */
2422 } SMARTLIST_FOREACH_END(conn);
2424 channel_dumpstats(severity);
2425 channel_listener_dumpstats(severity);
2427 tor_log(severity, LD_NET,
2428 "Cells processed: "U64_FORMAT" padding\n"
2429 " "U64_FORMAT" create\n"
2430 " "U64_FORMAT" created\n"
2431 " "U64_FORMAT" relay\n"
2432 " ("U64_FORMAT" relayed)\n"
2433 " ("U64_FORMAT" delivered)\n"
2434 " "U64_FORMAT" destroy",
2435 U64_PRINTF_ARG(stats_n_padding_cells_processed),
2436 U64_PRINTF_ARG(stats_n_create_cells_processed),
2437 U64_PRINTF_ARG(stats_n_created_cells_processed),
2438 U64_PRINTF_ARG(stats_n_relay_cells_processed),
2439 U64_PRINTF_ARG(stats_n_relay_cells_relayed),
2440 U64_PRINTF_ARG(stats_n_relay_cells_delivered),
2441 U64_PRINTF_ARG(stats_n_destroy_cells_processed));
2442 if (stats_n_data_cells_packaged)
2443 tor_log(severity,LD_NET,"Average packaged cell fullness: %2.3f%%",
2444 100*(U64_TO_DBL(stats_n_data_bytes_packaged) /
2445 U64_TO_DBL(stats_n_data_cells_packaged*RELAY_PAYLOAD_SIZE)) );
2446 if (stats_n_data_cells_received)
2447 tor_log(severity,LD_NET,"Average delivered cell fullness: %2.3f%%",
2448 100*(U64_TO_DBL(stats_n_data_bytes_received) /
2449 U64_TO_DBL(stats_n_data_cells_received*RELAY_PAYLOAD_SIZE)) );
2451 cpuworker_log_onionskin_overhead(severity, ONION_HANDSHAKE_TYPE_TAP, "TAP");
2452 cpuworker_log_onionskin_overhead(severity, ONION_HANDSHAKE_TYPE_NTOR,"ntor");
2454 if (now - time_of_process_start >= 0)
2455 elapsed = now - time_of_process_start;
2456 else
2457 elapsed = 0;
2459 if (elapsed) {
2460 tor_log(severity, LD_NET,
2461 "Average bandwidth: "U64_FORMAT"/%d = %d bytes/sec reading",
2462 U64_PRINTF_ARG(stats_n_bytes_read),
2463 (int)elapsed,
2464 (int) (stats_n_bytes_read/elapsed));
2465 tor_log(severity, LD_NET,
2466 "Average bandwidth: "U64_FORMAT"/%d = %d bytes/sec writing",
2467 U64_PRINTF_ARG(stats_n_bytes_written),
2468 (int)elapsed,
2469 (int) (stats_n_bytes_written/elapsed));
2472 tor_log(severity, LD_NET, "--------------- Dumping memory information:");
2473 dumpmemusage(severity);
2475 rep_hist_dump_stats(now,severity);
2476 rend_service_dump_stats(severity);
2477 dump_pk_ops(severity);
2478 dump_distinct_digest_count(severity);
2481 /** Called by exit() as we shut down the process.
2483 static void
2484 exit_function(void)
2486 /* NOTE: If we ever daemonize, this gets called immediately. That's
2487 * okay for now, because we only use this on Windows. */
2488 #ifdef _WIN32
2489 WSACleanup();
2490 #endif
2493 /** Set up the signal handlers for either parent or child. */
2494 void
2495 handle_signals(int is_parent)
2497 #ifndef _WIN32 /* do signal stuff only on Unix */
2498 int i;
2499 static const int signals[] = {
2500 SIGINT, /* do a controlled slow shutdown */
2501 SIGTERM, /* to terminate now */
2502 SIGPIPE, /* otherwise SIGPIPE kills us */
2503 SIGUSR1, /* dump stats */
2504 SIGUSR2, /* go to loglevel debug */
2505 SIGHUP, /* to reload config, retry conns, etc */
2506 #ifdef SIGXFSZ
2507 SIGXFSZ, /* handle file-too-big resource exhaustion */
2508 #endif
2509 SIGCHLD, /* handle dns/cpu workers that exit */
2510 -1 };
2511 static struct event *signal_events[16]; /* bigger than it has to be. */
2512 if (is_parent) {
2513 for (i = 0; signals[i] >= 0; ++i) {
2514 signal_events[i] = tor_evsignal_new(tor_libevent_get_base(), signals[i],
2515 signal_callback,
2516 /* Cast away const */
2517 (int*)&signals[i]);
2518 if (event_add(signal_events[i], NULL))
2519 log_warn(LD_BUG, "Error from libevent when adding event for signal %d",
2520 signals[i]);
2522 } else {
2523 struct sigaction action;
2524 action.sa_flags = 0;
2525 sigemptyset(&action.sa_mask);
2526 action.sa_handler = SIG_IGN;
2527 sigaction(SIGINT, &action, NULL);
2528 sigaction(SIGTERM, &action, NULL);
2529 sigaction(SIGPIPE, &action, NULL);
2530 sigaction(SIGUSR1, &action, NULL);
2531 sigaction(SIGUSR2, &action, NULL);
2532 sigaction(SIGHUP, &action, NULL);
2533 #ifdef SIGXFSZ
2534 sigaction(SIGXFSZ, &action, NULL);
2535 #endif
2537 #else /* MS windows */
2538 (void)is_parent;
2539 #endif /* signal stuff */
2542 /** Main entry point for the Tor command-line client.
2545 tor_init(int argc, char *argv[])
2547 char progname[256];
2548 int quiet = 0;
2550 time_of_process_start = time(NULL);
2551 init_connection_lists();
2552 /* Have the log set up with our application name. */
2553 tor_snprintf(progname, sizeof(progname), "Tor %s", get_version());
2554 log_set_application_name(progname);
2556 /* Set up the crypto nice and early */
2557 if (crypto_early_init() < 0) {
2558 log_err(LD_GENERAL, "Unable to initialize the crypto subsystem!");
2559 return -1;
2562 /* Initialize the history structures. */
2563 rep_hist_init();
2564 /* Initialize the service cache. */
2565 rend_cache_init();
2566 addressmap_init(); /* Init the client dns cache. Do it always, since it's
2567 * cheap. */
2570 /* We search for the "quiet" option first, since it decides whether we
2571 * will log anything at all to the command line. */
2572 config_line_t *opts = NULL, *cmdline_opts = NULL;
2573 const config_line_t *cl;
2574 (void) config_parse_commandline(argc, argv, 1, &opts, &cmdline_opts);
2575 for (cl = cmdline_opts; cl; cl = cl->next) {
2576 if (!strcmp(cl->key, "--hush"))
2577 quiet = 1;
2578 if (!strcmp(cl->key, "--quiet") ||
2579 !strcmp(cl->key, "--dump-config"))
2580 quiet = 2;
2581 /* The following options imply --hush */
2582 if (!strcmp(cl->key, "--version") || !strcmp(cl->key, "--digests") ||
2583 !strcmp(cl->key, "--list-torrc-options") ||
2584 !strcmp(cl->key, "--library-versions") ||
2585 !strcmp(cl->key, "--hash-password") ||
2586 !strcmp(cl->key, "-h") || !strcmp(cl->key, "--help")) {
2587 if (quiet < 1)
2588 quiet = 1;
2591 config_free_lines(opts);
2592 config_free_lines(cmdline_opts);
2595 /* give it somewhere to log to initially */
2596 switch (quiet) {
2597 case 2:
2598 /* no initial logging */
2599 break;
2600 case 1:
2601 add_temp_log(LOG_WARN);
2602 break;
2603 default:
2604 add_temp_log(LOG_NOTICE);
2606 quiet_level = quiet;
2609 const char *version = get_version();
2610 const char *bev_str =
2611 #ifdef USE_BUFFEREVENTS
2612 "(with bufferevents) ";
2613 #else
2615 #endif
2616 log_notice(LD_GENERAL, "Tor v%s %srunning on %s with Libevent %s, "
2617 "OpenSSL %s and Zlib %s.", version, bev_str,
2618 get_uname(),
2619 tor_libevent_get_version_str(),
2620 crypto_openssl_get_version_str(),
2621 tor_zlib_get_version_str());
2623 log_notice(LD_GENERAL, "Tor can't help you if you use it wrong! "
2624 "Learn how to be safe at "
2625 "https://www.torproject.org/download/download#warning");
2627 if (strstr(version, "alpha") || strstr(version, "beta"))
2628 log_notice(LD_GENERAL, "This version is not a stable Tor release. "
2629 "Expect more bugs than usual.");
2632 #ifdef NON_ANONYMOUS_MODE_ENABLED
2633 log_warn(LD_GENERAL, "This copy of Tor was compiled to run in a "
2634 "non-anonymous mode. It will provide NO ANONYMITY.");
2635 #endif
2637 if (network_init()<0) {
2638 log_err(LD_BUG,"Error initializing network; exiting.");
2639 return -1;
2641 atexit(exit_function);
2643 if (options_init_from_torrc(argc,argv) < 0) {
2644 log_err(LD_CONFIG,"Reading config failed--see warnings above.");
2645 return -1;
2648 #ifndef _WIN32
2649 if (geteuid()==0)
2650 log_warn(LD_GENERAL,"You are running Tor as root. You don't need to, "
2651 "and you probably shouldn't.");
2652 #endif
2654 if (crypto_global_init(get_options()->HardwareAccel,
2655 get_options()->AccelName,
2656 get_options()->AccelDir)) {
2657 log_err(LD_BUG, "Unable to initialize OpenSSL. Exiting.");
2658 return -1;
2660 stream_choice_seed_weak_rng();
2661 if (tor_init_libevent_rng() < 0) {
2662 log_warn(LD_NET, "Problem initializing libevent RNG.");
2665 return 0;
2668 /** A lockfile structure, used to prevent two Tors from messing with the
2669 * data directory at once. If this variable is non-NULL, we're holding
2670 * the lockfile. */
2671 static tor_lockfile_t *lockfile = NULL;
2673 /** Try to grab the lock file described in <b>options</b>, if we do not
2674 * already have it. If <b>err_if_locked</b> is true, warn if somebody else is
2675 * holding the lock, and exit if we can't get it after waiting. Otherwise,
2676 * return -1 if we can't get the lockfile. Return 0 on success.
2679 try_locking(const or_options_t *options, int err_if_locked)
2681 if (lockfile)
2682 return 0;
2683 else {
2684 char *fname = options_get_datadir_fname2_suffix(options, "lock",NULL,NULL);
2685 int already_locked = 0;
2686 tor_lockfile_t *lf = tor_lockfile_lock(fname, 0, &already_locked);
2687 tor_free(fname);
2688 if (!lf) {
2689 if (err_if_locked && already_locked) {
2690 int r;
2691 log_warn(LD_GENERAL, "It looks like another Tor process is running "
2692 "with the same data directory. Waiting 5 seconds to see "
2693 "if it goes away.");
2694 #ifndef _WIN32
2695 sleep(5);
2696 #else
2697 Sleep(5000);
2698 #endif
2699 r = try_locking(options, 0);
2700 if (r<0) {
2701 log_err(LD_GENERAL, "No, it's still there. Exiting.");
2702 exit(0);
2704 return r;
2706 return -1;
2708 lockfile = lf;
2709 return 0;
2713 /** Return true iff we've successfully acquired the lock file. */
2715 have_lockfile(void)
2717 return lockfile != NULL;
2720 /** If we have successfully acquired the lock file, release it. */
2721 void
2722 release_lockfile(void)
2724 if (lockfile) {
2725 tor_lockfile_unlock(lockfile);
2726 lockfile = NULL;
2730 /** Free all memory that we might have allocated somewhere.
2731 * If <b>postfork</b>, we are a worker process and we want to free
2732 * only the parts of memory that we won't touch. If !<b>postfork</b>,
2733 * Tor is shutting down and we should free everything.
2735 * Helps us find the real leaks with dmalloc and the like. Also valgrind
2736 * should then report 0 reachable in its leak report (in an ideal world --
2737 * in practice libevent, SSL, libc etc never quite free everything). */
2738 void
2739 tor_free_all(int postfork)
2741 if (!postfork) {
2742 evdns_shutdown(1);
2744 geoip_free_all();
2745 dirvote_free_all();
2746 routerlist_free_all();
2747 networkstatus_free_all();
2748 addressmap_free_all();
2749 dirserv_free_all();
2750 rend_service_free_all();
2751 rend_cache_free_all();
2752 rend_service_authorization_free_all();
2753 rep_hist_free_all();
2754 dns_free_all();
2755 clear_pending_onions();
2756 circuit_free_all();
2757 entry_guards_free_all();
2758 pt_free_all();
2759 channel_tls_free_all();
2760 channel_free_all();
2761 connection_free_all();
2762 scheduler_free_all();
2763 memarea_clear_freelist();
2764 nodelist_free_all();
2765 microdesc_free_all();
2766 ext_orport_free_all();
2767 control_free_all();
2768 sandbox_free_getaddrinfo_cache();
2769 if (!postfork) {
2770 config_free_all();
2771 or_state_free_all();
2772 router_free_all();
2773 routerkeys_free_all();
2774 policies_free_all();
2776 if (!postfork) {
2777 tor_tls_free_all();
2778 #ifndef _WIN32
2779 tor_getpwnam(NULL);
2780 #endif
2782 /* stuff in main.c */
2784 smartlist_free(connection_array);
2785 smartlist_free(closeable_connection_lst);
2786 smartlist_free(active_linked_connection_lst);
2787 periodic_timer_free(second_timer);
2788 #ifndef USE_BUFFEREVENTS
2789 periodic_timer_free(refill_timer);
2790 #endif
2792 if (!postfork) {
2793 release_lockfile();
2795 /* Stuff in util.c and address.c*/
2796 if (!postfork) {
2797 escaped(NULL);
2798 esc_router_info(NULL);
2799 logs_free_all(); /* free log strings. do this last so logs keep working. */
2803 /** Do whatever cleanup is necessary before shutting Tor down. */
2804 void
2805 tor_cleanup(void)
2807 const or_options_t *options = get_options();
2808 if (options->command == CMD_RUN_TOR) {
2809 time_t now = time(NULL);
2810 /* Remove our pid file. We don't care if there was an error when we
2811 * unlink, nothing we could do about it anyways. */
2812 if (options->PidFile) {
2813 if (unlink(options->PidFile) != 0) {
2814 log_warn(LD_FS, "Couldn't unlink pid file %s: %s",
2815 options->PidFile, strerror(errno));
2818 if (options->ControlPortWriteToFile) {
2819 if (unlink(options->ControlPortWriteToFile) != 0) {
2820 log_warn(LD_FS, "Couldn't unlink control port file %s: %s",
2821 options->ControlPortWriteToFile,
2822 strerror(errno));
2825 if (accounting_is_enabled(options))
2826 accounting_record_bandwidth_usage(now, get_or_state());
2827 or_state_mark_dirty(get_or_state(), 0); /* force an immediate save. */
2828 or_state_save(now);
2829 if (authdir_mode_tests_reachability(options))
2830 rep_hist_record_mtbf_data(now, 0);
2831 keypin_close_journal();
2833 #ifdef USE_DMALLOC
2834 dmalloc_log_stats();
2835 #endif
2836 tor_free_all(0); /* We could move tor_free_all back into the ifdef below
2837 later, if it makes shutdown unacceptably slow. But for
2838 now, leave it here: it's helped us catch bugs in the
2839 past. */
2840 crypto_global_cleanup();
2841 #ifdef USE_DMALLOC
2842 dmalloc_log_unfreed();
2843 dmalloc_shutdown();
2844 #endif
2847 /** Read/create keys as needed, and echo our fingerprint to stdout. */
2848 static int
2849 do_list_fingerprint(void)
2851 char buf[FINGERPRINT_LEN+1];
2852 crypto_pk_t *k;
2853 const char *nickname = get_options()->Nickname;
2854 if (!server_mode(get_options())) {
2855 log_err(LD_GENERAL,
2856 "Clients don't have long-term identity keys. Exiting.");
2857 return -1;
2859 tor_assert(nickname);
2860 if (init_keys() < 0) {
2861 log_err(LD_BUG,"Error initializing keys; can't display fingerprint");
2862 return -1;
2864 if (!(k = get_server_identity_key())) {
2865 log_err(LD_GENERAL,"Error: missing identity key.");
2866 return -1;
2868 if (crypto_pk_get_fingerprint(k, buf, 1)<0) {
2869 log_err(LD_BUG, "Error computing fingerprint");
2870 return -1;
2872 printf("%s %s\n", nickname, buf);
2873 return 0;
2876 /** Entry point for password hashing: take the desired password from
2877 * the command line, and print its salted hash to stdout. **/
2878 static void
2879 do_hash_password(void)
2882 char output[256];
2883 char key[S2K_RFC2440_SPECIFIER_LEN+DIGEST_LEN];
2885 crypto_rand(key, S2K_RFC2440_SPECIFIER_LEN-1);
2886 key[S2K_RFC2440_SPECIFIER_LEN-1] = (uint8_t)96; /* Hash 64 K of data. */
2887 secret_to_key_rfc2440(key+S2K_RFC2440_SPECIFIER_LEN, DIGEST_LEN,
2888 get_options()->command_arg, strlen(get_options()->command_arg),
2889 key);
2890 base16_encode(output, sizeof(output), key, sizeof(key));
2891 printf("16:%s\n",output);
2894 /** Entry point for configuration dumping: write the configuration to
2895 * stdout. */
2896 static int
2897 do_dump_config(void)
2899 const or_options_t *options = get_options();
2900 const char *arg = options->command_arg;
2901 int how;
2902 char *opts;
2904 if (!strcmp(arg, "short")) {
2905 how = OPTIONS_DUMP_MINIMAL;
2906 } else if (!strcmp(arg, "non-builtin")) {
2907 how = OPTIONS_DUMP_DEFAULTS;
2908 } else if (!strcmp(arg, "full")) {
2909 how = OPTIONS_DUMP_ALL;
2910 } else {
2911 fprintf(stderr, "No valid argument to --dump-config found!\n");
2912 fprintf(stderr, "Please select 'short', 'non-builtin', or 'full'.\n");
2914 return -1;
2917 opts = options_dump(options, how);
2918 printf("%s", opts);
2919 tor_free(opts);
2921 return 0;
2924 static void
2925 init_addrinfo(void)
2927 char hname[256];
2929 // host name to sandbox
2930 gethostname(hname, sizeof(hname));
2931 sandbox_add_addrinfo(hname);
2934 static sandbox_cfg_t*
2935 sandbox_init_filter(void)
2937 const or_options_t *options = get_options();
2938 sandbox_cfg_t *cfg = sandbox_cfg_new();
2939 int i;
2941 sandbox_cfg_allow_openat_filename(&cfg,
2942 get_datadir_fname("cached-status"));
2944 #define OPEN(name) \
2945 sandbox_cfg_allow_open_filename(&cfg, tor_strdup(name))
2947 #define OPEN_DATADIR(name) \
2948 sandbox_cfg_allow_open_filename(&cfg, get_datadir_fname(name))
2950 #define OPEN_DATADIR2(name, name2) \
2951 sandbox_cfg_allow_open_filename(&cfg, get_datadir_fname2((name), (name2)))
2953 #define OPEN_DATADIR_SUFFIX(name, suffix) do { \
2954 OPEN_DATADIR(name); \
2955 OPEN_DATADIR(name suffix); \
2956 } while (0)
2958 #define OPEN_DATADIR2_SUFFIX(name, name2, suffix) do { \
2959 OPEN_DATADIR2(name, name2); \
2960 OPEN_DATADIR2(name, name2 suffix); \
2961 } while (0)
2963 OPEN_DATADIR_SUFFIX("cached-certs", ".tmp");
2964 OPEN_DATADIR_SUFFIX("cached-consensus", ".tmp");
2965 OPEN_DATADIR_SUFFIX("unverified-consensus", ".tmp");
2966 OPEN_DATADIR_SUFFIX("unverified-microdesc-consensus", ".tmp");
2967 OPEN_DATADIR_SUFFIX("cached-microdesc-consensus", ".tmp");
2968 OPEN_DATADIR_SUFFIX("cached-microdescs", ".tmp");
2969 OPEN_DATADIR_SUFFIX("cached-microdescs.new", ".tmp");
2970 OPEN_DATADIR_SUFFIX("cached-descriptors", ".tmp");
2971 OPEN_DATADIR_SUFFIX("cached-descriptors.new", ".tmp");
2972 OPEN_DATADIR("cached-descriptors.tmp.tmp");
2973 OPEN_DATADIR_SUFFIX("cached-extrainfo", ".tmp");
2974 OPEN_DATADIR_SUFFIX("cached-extrainfo.new", ".tmp");
2975 OPEN_DATADIR("cached-extrainfo.tmp.tmp");
2976 OPEN_DATADIR_SUFFIX("state", ".tmp");
2977 OPEN_DATADIR_SUFFIX("unparseable-desc", ".tmp");
2978 OPEN_DATADIR_SUFFIX("v3-status-votes", ".tmp");
2979 OPEN("/dev/srandom");
2980 OPEN("/dev/urandom");
2981 OPEN("/dev/random");
2982 OPEN("/etc/hosts");
2983 OPEN("/proc/meminfo");
2985 if (options->ServerDNSResolvConfFile)
2986 sandbox_cfg_allow_open_filename(&cfg,
2987 tor_strdup(options->ServerDNSResolvConfFile));
2988 else
2989 sandbox_cfg_allow_open_filename(&cfg, tor_strdup("/etc/resolv.conf"));
2991 for (i = 0; i < 2; ++i) {
2992 if (get_torrc_fname(i)) {
2993 sandbox_cfg_allow_open_filename(&cfg, tor_strdup(get_torrc_fname(i)));
2997 #define RENAME_SUFFIX(name, suffix) \
2998 sandbox_cfg_allow_rename(&cfg, \
2999 get_datadir_fname(name suffix), \
3000 get_datadir_fname(name))
3002 #define RENAME_SUFFIX2(prefix, name, suffix) \
3003 sandbox_cfg_allow_rename(&cfg, \
3004 get_datadir_fname2(prefix, name suffix), \
3005 get_datadir_fname2(prefix, name))
3007 RENAME_SUFFIX("cached-certs", ".tmp");
3008 RENAME_SUFFIX("cached-consensus", ".tmp");
3009 RENAME_SUFFIX("unverified-consensus", ".tmp");
3010 RENAME_SUFFIX("unverified-microdesc-consensus", ".tmp");
3011 RENAME_SUFFIX("cached-microdesc-consensus", ".tmp");
3012 RENAME_SUFFIX("cached-microdescs", ".tmp");
3013 RENAME_SUFFIX("cached-microdescs", ".new");
3014 RENAME_SUFFIX("cached-microdescs.new", ".tmp");
3015 RENAME_SUFFIX("cached-descriptors", ".tmp");
3016 RENAME_SUFFIX("cached-descriptors", ".new");
3017 RENAME_SUFFIX("cached-descriptors.new", ".tmp");
3018 RENAME_SUFFIX("cached-extrainfo", ".tmp");
3019 RENAME_SUFFIX("cached-extrainfo", ".new");
3020 RENAME_SUFFIX("cached-extrainfo.new", ".tmp");
3021 RENAME_SUFFIX("state", ".tmp");
3022 RENAME_SUFFIX("unparseable-desc", ".tmp");
3023 RENAME_SUFFIX("v3-status-votes", ".tmp");
3025 #define STAT_DATADIR(name) \
3026 sandbox_cfg_allow_stat_filename(&cfg, get_datadir_fname(name))
3028 #define STAT_DATADIR2(name, name2) \
3029 sandbox_cfg_allow_stat_filename(&cfg, get_datadir_fname2((name), (name2)))
3031 STAT_DATADIR(NULL);
3032 STAT_DATADIR("lock");
3033 STAT_DATADIR("state");
3034 STAT_DATADIR("router-stability");
3035 STAT_DATADIR("cached-extrainfo.new");
3038 smartlist_t *files = smartlist_new();
3039 tor_log_get_logfile_names(files);
3040 SMARTLIST_FOREACH(files, char *, file_name, {
3041 /* steals reference */
3042 sandbox_cfg_allow_open_filename(&cfg, file_name);
3044 smartlist_free(files);
3048 smartlist_t *files = smartlist_new();
3049 smartlist_t *dirs = smartlist_new();
3050 rend_services_add_filenames_to_lists(files, dirs);
3051 SMARTLIST_FOREACH(files, char *, file_name, {
3052 char *tmp_name = NULL;
3053 tor_asprintf(&tmp_name, "%s.tmp", file_name);
3054 sandbox_cfg_allow_rename(&cfg,
3055 tor_strdup(tmp_name), tor_strdup(file_name));
3056 /* steals references */
3057 sandbox_cfg_allow_open_filename(&cfg, file_name);
3058 sandbox_cfg_allow_open_filename(&cfg, tmp_name);
3060 SMARTLIST_FOREACH(dirs, char *, dir, {
3061 /* steals reference */
3062 sandbox_cfg_allow_stat_filename(&cfg, dir);
3064 smartlist_free(files);
3065 smartlist_free(dirs);
3069 char *fname;
3070 if ((fname = get_controller_cookie_file_name())) {
3071 sandbox_cfg_allow_open_filename(&cfg, fname);
3073 if ((fname = get_ext_or_auth_cookie_file_name())) {
3074 sandbox_cfg_allow_open_filename(&cfg, fname);
3078 if (options->DirPortFrontPage) {
3079 sandbox_cfg_allow_open_filename(&cfg,
3080 tor_strdup(options->DirPortFrontPage));
3083 // orport
3084 if (server_mode(get_options())) {
3086 OPEN_DATADIR2_SUFFIX("keys", "secret_id_key", ".tmp");
3087 OPEN_DATADIR2_SUFFIX("keys", "secret_onion_key", ".tmp");
3088 OPEN_DATADIR2_SUFFIX("keys", "secret_onion_key_ntor", ".tmp");
3089 OPEN_DATADIR2("keys", "secret_id_key.old");
3090 OPEN_DATADIR2("keys", "secret_onion_key.old");
3091 OPEN_DATADIR2("keys", "secret_onion_key_ntor.old");
3093 OPEN_DATADIR2_SUFFIX("stats", "bridge-stats", ".tmp");
3094 OPEN_DATADIR2_SUFFIX("stats", "dirreq-stats", ".tmp");
3096 OPEN_DATADIR2_SUFFIX("stats", "entry-stats", ".tmp");
3097 OPEN_DATADIR2_SUFFIX("stats", "exit-stats", ".tmp");
3098 OPEN_DATADIR2_SUFFIX("stats", "buffer-stats", ".tmp");
3099 OPEN_DATADIR2_SUFFIX("stats", "conn-stats", ".tmp");
3101 OPEN_DATADIR("approved-routers");
3102 OPEN_DATADIR_SUFFIX("fingerprint", ".tmp");
3103 OPEN_DATADIR_SUFFIX("hashed-fingerprint", ".tmp");
3104 OPEN_DATADIR_SUFFIX("router-stability", ".tmp");
3106 OPEN("/etc/resolv.conf");
3108 RENAME_SUFFIX("fingerprint", ".tmp");
3109 RENAME_SUFFIX2("keys", "secret_onion_key_ntor", ".tmp");
3110 RENAME_SUFFIX2("keys", "secret_id_key", ".tmp");
3111 RENAME_SUFFIX2("keys", "secret_id_key.old", ".tmp");
3112 RENAME_SUFFIX2("keys", "secret_onion_key", ".tmp");
3113 RENAME_SUFFIX2("keys", "secret_onion_key.old", ".tmp");
3114 RENAME_SUFFIX2("stats", "bridge-stats", ".tmp");
3115 RENAME_SUFFIX2("stats", "dirreq-stats", ".tmp");
3116 RENAME_SUFFIX2("stats", "entry-stats", ".tmp");
3117 RENAME_SUFFIX2("stats", "exit-stats", ".tmp");
3118 RENAME_SUFFIX2("stats", "buffer-stats", ".tmp");
3119 RENAME_SUFFIX2("stats", "conn-stats", ".tmp");
3120 RENAME_SUFFIX("hashed-fingerprint", ".tmp");
3121 RENAME_SUFFIX("router-stability", ".tmp");
3123 sandbox_cfg_allow_rename(&cfg,
3124 get_datadir_fname2("keys", "secret_onion_key"),
3125 get_datadir_fname2("keys", "secret_onion_key.old"));
3126 sandbox_cfg_allow_rename(&cfg,
3127 get_datadir_fname2("keys", "secret_onion_key_ntor"),
3128 get_datadir_fname2("keys", "secret_onion_key_ntor.old"));
3130 STAT_DATADIR("keys");
3131 STAT_DATADIR("stats");
3132 STAT_DATADIR2("stats", "dirreq-stats");
3135 init_addrinfo();
3137 return cfg;
3140 /** Main entry point for the Tor process. Called from main(). */
3141 /* This function is distinct from main() only so we can link main.c into
3142 * the unittest binary without conflicting with the unittests' main. */
3144 tor_main(int argc, char *argv[])
3146 int result = 0;
3148 #ifdef _WIN32
3149 /* Call SetProcessDEPPolicy to permanently enable DEP.
3150 The function will not resolve on earlier versions of Windows,
3151 and failure is not dangerous. */
3152 HMODULE hMod = GetModuleHandleA("Kernel32.dll");
3153 if (hMod) {
3154 typedef BOOL (WINAPI *PSETDEP)(DWORD);
3155 PSETDEP setdeppolicy = (PSETDEP)GetProcAddress(hMod,
3156 "SetProcessDEPPolicy");
3157 if (setdeppolicy) setdeppolicy(1); /* PROCESS_DEP_ENABLE */
3159 #endif
3161 configure_backtrace_handler(get_version());
3163 update_approx_time(time(NULL));
3164 tor_threads_init();
3165 init_logging(0);
3166 #ifdef USE_DMALLOC
3168 /* Instruct OpenSSL to use our internal wrappers for malloc,
3169 realloc and free. */
3170 int r = CRYPTO_set_mem_ex_functions(tor_malloc_, tor_realloc_, tor_free_);
3171 tor_assert(r);
3173 #endif
3174 #ifdef NT_SERVICE
3176 int done = 0;
3177 result = nt_service_parse_options(argc, argv, &done);
3178 if (done) return result;
3180 #endif
3181 if (tor_init(argc, argv)<0)
3182 return -1;
3184 if (get_options()->Sandbox && get_options()->command == CMD_RUN_TOR) {
3185 sandbox_cfg_t* cfg = sandbox_init_filter();
3187 if (sandbox_init(cfg)) {
3188 log_err(LD_BUG,"Failed to create syscall sandbox filter");
3189 return -1;
3192 // registering libevent rng
3193 #ifdef HAVE_EVUTIL_SECURE_RNG_SET_URANDOM_DEVICE_FILE
3194 evutil_secure_rng_set_urandom_device_file(
3195 (char*) sandbox_intern_string("/dev/urandom"));
3196 #endif
3199 switch (get_options()->command) {
3200 case CMD_RUN_TOR:
3201 #ifdef NT_SERVICE
3202 nt_service_set_state(SERVICE_RUNNING);
3203 #endif
3204 result = do_main_loop();
3205 break;
3206 case CMD_KEYGEN:
3207 result = load_ed_keys(get_options(), time(NULL));
3208 break;
3209 case CMD_LIST_FINGERPRINT:
3210 result = do_list_fingerprint();
3211 break;
3212 case CMD_HASH_PASSWORD:
3213 do_hash_password();
3214 result = 0;
3215 break;
3216 case CMD_VERIFY_CONFIG:
3217 if (quiet_level == 0)
3218 printf("Configuration was valid\n");
3219 result = 0;
3220 break;
3221 case CMD_DUMP_CONFIG:
3222 result = do_dump_config();
3223 break;
3224 case CMD_RUN_UNITTESTS: /* only set by test.c */
3225 default:
3226 log_warn(LD_BUG,"Illegal command number %d: internal error.",
3227 get_options()->command);
3228 result = -1;
3230 tor_cleanup();
3231 return result;