close idle dir-fetch circs early
[tor.git] / src / or / main.c
blob04364bbe109eb945dd3c13e87f5b0b040fca2aa9
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-2010, 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 #ifdef USE_DMALLOC
16 #include <dmalloc.h>
17 #include <openssl/crypto.h>
18 #endif
19 #include "memarea.h"
21 void evdns_shutdown(int);
23 /********* PROTOTYPES **********/
25 static void dumpmemusage(int severity);
26 static void dumpstats(int severity); /* log stats */
27 static void conn_read_callback(int fd, short event, void *_conn);
28 static void conn_write_callback(int fd, short event, void *_conn);
29 static void signal_callback(int fd, short events, void *arg);
30 static void second_elapsed_callback(int fd, short event, void *args);
31 static int conn_close_if_marked(int i);
32 static void connection_start_reading_from_linked_conn(connection_t *conn);
33 static int connection_should_read_from_linked_conn(connection_t *conn);
35 /********* START VARIABLES **********/
37 int global_read_bucket; /**< Max number of bytes I can read this second. */
38 int global_write_bucket; /**< Max number of bytes I can write this second. */
40 /** Max number of relayed (bandwidth class 1) bytes I can read this second. */
41 int global_relayed_read_bucket;
42 /** Max number of relayed (bandwidth class 1) bytes I can write this second. */
43 int global_relayed_write_bucket;
45 /** What was the read bucket before the last second_elapsed_callback() call?
46 * (used to determine how many bytes we've read). */
47 static int stats_prev_global_read_bucket;
48 /** What was the write bucket before the last second_elapsed_callback() call?
49 * (used to determine how many bytes we've written). */
50 static int stats_prev_global_write_bucket;
51 /* XXX we might want to keep stats about global_relayed_*_bucket too. Or not.*/
52 /** How many bytes have we read since we started the process? */
53 static uint64_t stats_n_bytes_read = 0;
54 /** How many bytes have we written since we started the process? */
55 static uint64_t stats_n_bytes_written = 0;
56 /** What time did this process start up? */
57 time_t time_of_process_start = 0;
58 /** How many seconds have we been running? */
59 long stats_n_seconds_working = 0;
60 /** When do we next launch DNS wildcarding checks? */
61 static time_t time_to_check_for_correct_dns = 0;
63 /** How often will we honor SIGNEWNYM requests? */
64 #define MAX_SIGNEWNYM_RATE 10
65 /** When did we last process a SIGNEWNYM request? */
66 static time_t time_of_last_signewnym = 0;
67 /** Is there a signewnym request we're currently waiting to handle? */
68 static int signewnym_is_pending = 0;
70 /** Smartlist of all open connections. */
71 static smartlist_t *connection_array = NULL;
72 /** List of connections that have been marked for close and need to be freed
73 * and removed from connection_array. */
74 static smartlist_t *closeable_connection_lst = NULL;
75 /** List of linked connections that are currently reading data into their
76 * inbuf from their partner's outbuf. */
77 static smartlist_t *active_linked_connection_lst = NULL;
78 /** Flag: Set to true iff we entered the current libevent main loop via
79 * <b>loop_once</b>. If so, there's no need to trigger a loopexit in order
80 * to handle linked connections. */
81 static int called_loop_once = 0;
83 /** We set this to 1 when we've opened a circuit, so we can print a log
84 * entry to inform the user that Tor is working. */
85 int has_completed_circuit=0;
87 /** How often do we check for router descriptors that we should download
88 * when we have too little directory info? */
89 #define GREEDY_DESCRIPTOR_RETRY_INTERVAL (10)
90 /** How often do we check for router descriptors that we should download
91 * when we have enough directory info? */
92 #define LAZY_DESCRIPTOR_RETRY_INTERVAL (60)
93 /** How often do we 'forgive' undownloadable router descriptors and attempt
94 * to download them again? */
95 #define DESCRIPTOR_FAILURE_RESET_INTERVAL (60*60)
96 /** How long do we let a directory connection stall before expiring it? */
97 #define DIR_CONN_MAX_STALL (5*60)
99 /** How long do we let OR connections handshake before we decide that
100 * they are obsolete? */
101 #define TLS_HANDSHAKE_TIMEOUT (60)
103 /********* END VARIABLES ************/
105 /****************************************************************************
107 * This section contains accessors and other methods on the connection_array
108 * variables (which are global within this file and unavailable outside it).
110 ****************************************************************************/
112 /** Add <b>conn</b> to the array of connections that we can poll on. The
113 * connection's socket must be set; the connection starts out
114 * non-reading and non-writing.
117 connection_add(connection_t *conn)
119 tor_assert(conn);
120 tor_assert(conn->s >= 0 ||
121 conn->linked ||
122 (conn->type == CONN_TYPE_AP &&
123 TO_EDGE_CONN(conn)->is_dns_request));
125 tor_assert(conn->conn_array_index == -1); /* can only connection_add once */
126 conn->conn_array_index = smartlist_len(connection_array);
127 smartlist_add(connection_array, conn);
129 if (conn->s >= 0 || conn->linked) {
130 conn->read_event = tor_malloc_zero(sizeof(struct event));
131 conn->write_event = tor_malloc_zero(sizeof(struct event));
132 event_set(conn->read_event, conn->s, EV_READ|EV_PERSIST,
133 conn_read_callback, conn);
134 event_set(conn->write_event, conn->s, EV_WRITE|EV_PERSIST,
135 conn_write_callback, conn);
138 log_debug(LD_NET,"new conn type %s, socket %d, address %s, n_conns %d.",
139 conn_type_to_string(conn->type), conn->s, conn->address,
140 smartlist_len(connection_array));
142 return 0;
145 /** Remove the connection from the global list, and remove the
146 * corresponding poll entry. Calling this function will shift the last
147 * connection (if any) into the position occupied by conn.
150 connection_remove(connection_t *conn)
152 int current_index;
153 connection_t *tmp;
155 tor_assert(conn);
157 log_debug(LD_NET,"removing socket %d (type %s), n_conns now %d",
158 conn->s, conn_type_to_string(conn->type),
159 smartlist_len(connection_array));
161 tor_assert(conn->conn_array_index >= 0);
162 current_index = conn->conn_array_index;
163 connection_unregister_events(conn); /* This is redundant, but cheap. */
164 if (current_index == smartlist_len(connection_array)-1) { /* at the end */
165 smartlist_del(connection_array, current_index);
166 return 0;
169 /* replace this one with the one at the end */
170 smartlist_del(connection_array, current_index);
171 tmp = smartlist_get(connection_array, current_index);
172 tmp->conn_array_index = current_index;
174 return 0;
177 /** If <b>conn</b> is an edge conn, remove it from the list
178 * of conn's on this circuit. If it's not on an edge,
179 * flush and send destroys for all circuits on this conn.
181 * Remove it from connection_array (if applicable) and
182 * from closeable_connection_list.
184 * Then free it.
186 static void
187 connection_unlink(connection_t *conn)
189 connection_about_to_close_connection(conn);
190 if (conn->conn_array_index >= 0) {
191 connection_remove(conn);
193 if (conn->linked_conn) {
194 conn->linked_conn->linked_conn = NULL;
195 if (! conn->linked_conn->marked_for_close &&
196 conn->linked_conn->reading_from_linked_conn)
197 connection_start_reading(conn->linked_conn);
198 conn->linked_conn = NULL;
200 smartlist_remove(closeable_connection_lst, conn);
201 smartlist_remove(active_linked_connection_lst, conn);
202 if (conn->type == CONN_TYPE_EXIT) {
203 assert_connection_edge_not_dns_pending(TO_EDGE_CONN(conn));
205 if (conn->type == CONN_TYPE_OR) {
206 if (!tor_digest_is_zero(TO_OR_CONN(conn)->identity_digest))
207 connection_or_remove_from_identity_map(TO_OR_CONN(conn));
209 connection_free(conn);
212 /** Schedule <b>conn</b> to be closed. **/
213 void
214 add_connection_to_closeable_list(connection_t *conn)
216 tor_assert(!smartlist_isin(closeable_connection_lst, conn));
217 tor_assert(conn->marked_for_close);
218 assert_connection_ok(conn, time(NULL));
219 smartlist_add(closeable_connection_lst, conn);
222 /** Return 1 if conn is on the closeable list, else return 0. */
224 connection_is_on_closeable_list(connection_t *conn)
226 return smartlist_isin(closeable_connection_lst, conn);
229 /** Return true iff conn is in the current poll array. */
231 connection_in_array(connection_t *conn)
233 return smartlist_isin(connection_array, conn);
236 /** Set <b>*array</b> to an array of all connections, and <b>*n</b>
237 * to the length of the array. <b>*array</b> and <b>*n</b> must not
238 * be modified.
240 smartlist_t *
241 get_connection_array(void)
243 if (!connection_array)
244 connection_array = smartlist_create();
245 return connection_array;
248 /** Set the event mask on <b>conn</b> to <b>events</b>. (The event
249 * mask is a bitmask whose bits are EV_READ and EV_WRITE.)
251 void
252 connection_watch_events(connection_t *conn, short events)
254 if (events & EV_READ)
255 connection_start_reading(conn);
256 else
257 connection_stop_reading(conn);
259 if (events & EV_WRITE)
260 connection_start_writing(conn);
261 else
262 connection_stop_writing(conn);
265 /** Return true iff <b>conn</b> is listening for read events. */
267 connection_is_reading(connection_t *conn)
269 tor_assert(conn);
271 return conn->reading_from_linked_conn ||
272 (conn->read_event && event_pending(conn->read_event, EV_READ, NULL));
275 /** Tell the main loop to stop notifying <b>conn</b> of any read events. */
276 void
277 connection_stop_reading(connection_t *conn)
279 tor_assert(conn);
280 tor_assert(conn->read_event);
282 if (conn->linked) {
283 conn->reading_from_linked_conn = 0;
284 connection_stop_reading_from_linked_conn(conn);
285 } else {
286 if (event_del(conn->read_event))
287 log_warn(LD_NET, "Error from libevent setting read event state for %d "
288 "to unwatched: %s",
289 conn->s,
290 tor_socket_strerror(tor_socket_errno(conn->s)));
294 /** Tell the main loop to start notifying <b>conn</b> of any read events. */
295 void
296 connection_start_reading(connection_t *conn)
298 tor_assert(conn);
299 tor_assert(conn->read_event);
301 if (conn->linked) {
302 conn->reading_from_linked_conn = 1;
303 if (connection_should_read_from_linked_conn(conn))
304 connection_start_reading_from_linked_conn(conn);
305 } else {
306 if (event_add(conn->read_event, NULL))
307 log_warn(LD_NET, "Error from libevent setting read event state for %d "
308 "to watched: %s",
309 conn->s,
310 tor_socket_strerror(tor_socket_errno(conn->s)));
314 /** Return true iff <b>conn</b> is listening for write events. */
316 connection_is_writing(connection_t *conn)
318 tor_assert(conn);
320 return conn->writing_to_linked_conn ||
321 (conn->write_event && event_pending(conn->write_event, EV_WRITE, NULL));
324 /** Tell the main loop to stop notifying <b>conn</b> of any write events. */
325 void
326 connection_stop_writing(connection_t *conn)
328 tor_assert(conn);
329 tor_assert(conn->write_event);
331 if (conn->linked) {
332 conn->writing_to_linked_conn = 0;
333 if (conn->linked_conn)
334 connection_stop_reading_from_linked_conn(conn->linked_conn);
335 } else {
336 if (event_del(conn->write_event))
337 log_warn(LD_NET, "Error from libevent setting write event state for %d "
338 "to unwatched: %s",
339 conn->s,
340 tor_socket_strerror(tor_socket_errno(conn->s)));
344 /** Tell the main loop to start notifying <b>conn</b> of any write events. */
345 void
346 connection_start_writing(connection_t *conn)
348 tor_assert(conn);
349 tor_assert(conn->write_event);
351 if (conn->linked) {
352 conn->writing_to_linked_conn = 1;
353 if (conn->linked_conn &&
354 connection_should_read_from_linked_conn(conn->linked_conn))
355 connection_start_reading_from_linked_conn(conn->linked_conn);
356 } else {
357 if (event_add(conn->write_event, NULL))
358 log_warn(LD_NET, "Error from libevent setting write event state for %d "
359 "to watched: %s",
360 conn->s,
361 tor_socket_strerror(tor_socket_errno(conn->s)));
365 /** Return true iff <b>conn</b> is linked conn, and reading from the conn
366 * linked to it would be good and feasible. (Reading is "feasible" if the
367 * other conn exists and has data in its outbuf, and is "good" if we have our
368 * reading_from_linked_conn flag set and the other conn has its
369 * writing_to_linked_conn flag set.)*/
370 static int
371 connection_should_read_from_linked_conn(connection_t *conn)
373 if (conn->linked && conn->reading_from_linked_conn) {
374 if (! conn->linked_conn ||
375 (conn->linked_conn->writing_to_linked_conn &&
376 buf_datalen(conn->linked_conn->outbuf)))
377 return 1;
379 return 0;
382 /** Helper: Tell the main loop to begin reading bytes into <b>conn</b> from
383 * its linked connection, if it is not doing so already. Called by
384 * connection_start_reading and connection_start_writing as appropriate. */
385 static void
386 connection_start_reading_from_linked_conn(connection_t *conn)
388 tor_assert(conn);
389 tor_assert(conn->linked == 1);
391 if (!conn->active_on_link) {
392 conn->active_on_link = 1;
393 smartlist_add(active_linked_connection_lst, conn);
394 if (!called_loop_once) {
395 /* This is the first event on the list; we won't be in LOOP_ONCE mode,
396 * so we need to make sure that the event_loop() actually exits at the
397 * end of its run through the current connections and
398 * lets us activate read events for linked connections. */
399 struct timeval tv = { 0, 0 };
400 event_loopexit(&tv);
402 } else {
403 tor_assert(smartlist_isin(active_linked_connection_lst, conn));
407 /** Tell the main loop to stop reading bytes into <b>conn</b> from its linked
408 * connection, if is currently doing so. Called by connection_stop_reading,
409 * connection_stop_writing, and connection_read. */
410 void
411 connection_stop_reading_from_linked_conn(connection_t *conn)
413 tor_assert(conn);
414 tor_assert(conn->linked == 1);
416 if (conn->active_on_link) {
417 conn->active_on_link = 0;
418 /* FFFF We could keep an index here so we can smartlist_del
419 * cleanly. On the other hand, this doesn't show up on profiles,
420 * so let's leave it alone for now. */
421 smartlist_remove(active_linked_connection_lst, conn);
422 } else {
423 tor_assert(!smartlist_isin(active_linked_connection_lst, conn));
427 /** Close all connections that have been scheduled to get closed. */
428 static void
429 close_closeable_connections(void)
431 int i;
432 for (i = 0; i < smartlist_len(closeable_connection_lst); ) {
433 connection_t *conn = smartlist_get(closeable_connection_lst, i);
434 if (conn->conn_array_index < 0) {
435 connection_unlink(conn); /* blow it away right now */
436 } else {
437 if (!conn_close_if_marked(conn->conn_array_index))
438 ++i;
443 /** Libevent callback: this gets invoked when (connection_t*)<b>conn</b> has
444 * some data to read. */
445 static void
446 conn_read_callback(int fd, short event, void *_conn)
448 connection_t *conn = _conn;
449 (void)fd;
450 (void)event;
452 log_debug(LD_NET,"socket %d wants to read.",conn->s);
454 /* assert_connection_ok(conn, time(NULL)); */
456 if (connection_handle_read(conn) < 0) {
457 if (!conn->marked_for_close) {
458 #ifndef MS_WINDOWS
459 log_warn(LD_BUG,"Unhandled error on read for %s connection "
460 "(fd %d); removing",
461 conn_type_to_string(conn->type), conn->s);
462 tor_fragile_assert();
463 #endif
464 if (CONN_IS_EDGE(conn))
465 connection_edge_end_errno(TO_EDGE_CONN(conn));
466 connection_mark_for_close(conn);
469 assert_connection_ok(conn, time(NULL));
471 if (smartlist_len(closeable_connection_lst))
472 close_closeable_connections();
475 /** Libevent callback: this gets invoked when (connection_t*)<b>conn</b> has
476 * some data to write. */
477 static void
478 conn_write_callback(int fd, short events, void *_conn)
480 connection_t *conn = _conn;
481 (void)fd;
482 (void)events;
484 LOG_FN_CONN(conn, (LOG_DEBUG, LD_NET, "socket %d wants to write.",conn->s));
486 /* assert_connection_ok(conn, time(NULL)); */
488 if (connection_handle_write(conn, 0) < 0) {
489 if (!conn->marked_for_close) {
490 /* this connection is broken. remove it. */
491 log_fn(LOG_WARN,LD_BUG,
492 "unhandled error on write for %s connection (fd %d); removing",
493 conn_type_to_string(conn->type), conn->s);
494 tor_fragile_assert();
495 if (CONN_IS_EDGE(conn)) {
496 /* otherwise we cry wolf about duplicate close */
497 edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
498 if (!edge_conn->end_reason)
499 edge_conn->end_reason = END_STREAM_REASON_INTERNAL;
500 edge_conn->edge_has_sent_end = 1;
502 connection_close_immediate(conn); /* So we don't try to flush. */
503 connection_mark_for_close(conn);
506 assert_connection_ok(conn, time(NULL));
508 if (smartlist_len(closeable_connection_lst))
509 close_closeable_connections();
512 /** If the connection at connection_array[i] is marked for close, then:
513 * - If it has data that it wants to flush, try to flush it.
514 * - If it _still_ has data to flush, and conn->hold_open_until_flushed is
515 * true, then leave the connection open and return.
516 * - Otherwise, remove the connection from connection_array and from
517 * all other lists, close it, and free it.
518 * Returns 1 if the connection was closed, 0 otherwise.
520 static int
521 conn_close_if_marked(int i)
523 connection_t *conn;
524 int retval;
525 time_t now;
527 conn = smartlist_get(connection_array, i);
528 if (!conn->marked_for_close)
529 return 0; /* nothing to see here, move along */
530 now = time(NULL);
531 assert_connection_ok(conn, now);
532 /* assert_all_pending_dns_resolves_ok(); */
534 log_debug(LD_NET,"Cleaning up connection (fd %d).",conn->s);
535 if ((conn->s >= 0 || conn->linked_conn) && connection_wants_to_flush(conn)) {
536 /* s == -1 means it's an incomplete edge connection, or that the socket
537 * has already been closed as unflushable. */
538 ssize_t sz = connection_bucket_write_limit(conn, now);
539 if (!conn->hold_open_until_flushed)
540 log_info(LD_NET,
541 "Conn (addr %s, fd %d, type %s, state %d) marked, but wants "
542 "to flush %d bytes. (Marked at %s:%d)",
543 escaped_safe_str(conn->address),
544 conn->s, conn_type_to_string(conn->type), conn->state,
545 (int)conn->outbuf_flushlen,
546 conn->marked_for_close_file, conn->marked_for_close);
547 if (conn->linked_conn) {
548 retval = move_buf_to_buf(conn->linked_conn->inbuf, conn->outbuf,
549 &conn->outbuf_flushlen);
550 if (retval >= 0) {
551 /* The linked conn will notice that it has data when it notices that
552 * we're gone. */
553 connection_start_reading_from_linked_conn(conn->linked_conn);
555 log_debug(LD_GENERAL, "Flushed last %d bytes from a linked conn; "
556 "%d left; flushlen %d; wants-to-flush==%d", retval,
557 (int)buf_datalen(conn->outbuf),
558 (int)conn->outbuf_flushlen,
559 connection_wants_to_flush(conn));
560 } else if (connection_speaks_cells(conn)) {
561 if (conn->state == OR_CONN_STATE_OPEN) {
562 retval = flush_buf_tls(TO_OR_CONN(conn)->tls, conn->outbuf, sz,
563 &conn->outbuf_flushlen);
564 } else
565 retval = -1; /* never flush non-open broken tls connections */
566 } else {
567 retval = flush_buf(conn->s, conn->outbuf, sz, &conn->outbuf_flushlen);
569 if (retval >= 0 && /* Technically, we could survive things like
570 TLS_WANT_WRITE here. But don't bother for now. */
571 conn->hold_open_until_flushed && connection_wants_to_flush(conn)) {
572 if (retval > 0) {
573 LOG_FN_CONN(conn, (LOG_INFO,LD_NET,
574 "Holding conn (fd %d) open for more flushing.",
575 conn->s));
576 conn->timestamp_lastwritten = now; /* reset so we can flush more */
578 return 0;
580 if (connection_wants_to_flush(conn)) {
581 int severity;
582 if (conn->type == CONN_TYPE_EXIT ||
583 (conn->type == CONN_TYPE_OR && server_mode(get_options())) ||
584 (conn->type == CONN_TYPE_DIR && conn->purpose == DIR_PURPOSE_SERVER))
585 severity = LOG_INFO;
586 else
587 severity = LOG_NOTICE;
588 /* XXXX Maybe allow this to happen a certain amount per hour; it usually
589 * is meaningless. */
590 log_fn(severity, LD_NET, "We stalled too much while trying to write %d "
591 "bytes to address %s. If this happens a lot, either "
592 "something is wrong with your network connection, or "
593 "something is wrong with theirs. "
594 "(fd %d, type %s, state %d, marked at %s:%d).",
595 (int)buf_datalen(conn->outbuf),
596 escaped_safe_str(conn->address), conn->s,
597 conn_type_to_string(conn->type), conn->state,
598 conn->marked_for_close_file,
599 conn->marked_for_close);
602 connection_unlink(conn); /* unlink, remove, free */
603 return 1;
606 /** We've just tried every dirserver we know about, and none of
607 * them were reachable. Assume the network is down. Change state
608 * so next time an application connection arrives we'll delay it
609 * and try another directory fetch. Kill off all the circuit_wait
610 * streams that are waiting now, since they will all timeout anyway.
612 void
613 directory_all_unreachable(time_t now)
615 connection_t *conn;
616 (void)now;
618 stats_n_seconds_working=0; /* reset it */
620 while ((conn = connection_get_by_type_state(CONN_TYPE_AP,
621 AP_CONN_STATE_CIRCUIT_WAIT))) {
622 edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
623 log_notice(LD_NET,
624 "Is your network connection down? "
625 "Failing connection to '%s:%d'.",
626 safe_str(edge_conn->socks_request->address),
627 edge_conn->socks_request->port);
628 connection_mark_unattached_ap(edge_conn,
629 END_STREAM_REASON_NET_UNREACHABLE);
631 control_event_general_status(LOG_ERR, "DIR_ALL_UNREACHABLE");
634 /** This function is called whenever we successfully pull down some new
635 * network statuses or server descriptors. */
636 void
637 directory_info_has_arrived(time_t now, int from_cache)
639 or_options_t *options = get_options();
641 if (!router_have_minimum_dir_info()) {
642 int quiet = directory_too_idle_to_fetch_descriptors(options, now);
643 log(quiet ? LOG_INFO : LOG_NOTICE, LD_DIR,
644 "I learned some more directory information, but not enough to "
645 "build a circuit: %s", get_dir_info_status_string());
646 update_router_descriptor_downloads(now);
647 return;
648 } else {
649 if (directory_fetches_from_authorities(options))
650 update_router_descriptor_downloads(now);
652 /* if we have enough dir info, then update our guard status with
653 * whatever we just learned. */
654 entry_guards_compute_status();
655 /* Don't even bother trying to get extrainfo until the rest of our
656 * directory info is up-to-date */
657 if (options->DownloadExtraInfo)
658 update_extrainfo_downloads(now);
661 if (server_mode(options) && !we_are_hibernating() && !from_cache &&
662 (has_completed_circuit || !any_predicted_circuits(now)))
663 consider_testing_reachability(1, 1);
666 /** Perform regular maintenance tasks for a single connection. This
667 * function gets run once per second per connection by run_scheduled_events.
669 static void
670 run_connection_housekeeping(int i, time_t now)
672 cell_t cell;
673 connection_t *conn = smartlist_get(connection_array, i);
674 or_options_t *options = get_options();
675 or_connection_t *or_conn;
677 if (conn->outbuf && !buf_datalen(conn->outbuf) && conn->type == CONN_TYPE_OR)
678 TO_OR_CONN(conn)->timestamp_lastempty = now;
680 if (conn->marked_for_close) {
681 /* nothing to do here */
682 return;
685 /* Expire any directory connections that haven't been active (sent
686 * if a server or received if a client) for 5 min */
687 if (conn->type == CONN_TYPE_DIR &&
688 ((DIR_CONN_IS_SERVER(conn) &&
689 conn->timestamp_lastwritten + DIR_CONN_MAX_STALL < now) ||
690 (!DIR_CONN_IS_SERVER(conn) &&
691 conn->timestamp_lastread + DIR_CONN_MAX_STALL < now))) {
692 log_info(LD_DIR,"Expiring wedged directory conn (fd %d, purpose %d)",
693 conn->s, conn->purpose);
694 /* This check is temporary; it's to let us know whether we should consider
695 * parsing partial serverdesc responses. */
696 if (conn->purpose == DIR_PURPOSE_FETCH_SERVERDESC &&
697 buf_datalen(conn->inbuf)>=1024) {
698 log_info(LD_DIR,"Trying to extract information from wedged server desc "
699 "download.");
700 connection_dir_reached_eof(TO_DIR_CONN(conn));
701 } else {
702 connection_mark_for_close(conn);
704 return;
707 if (!connection_speaks_cells(conn))
708 return; /* we're all done here, the rest is just for OR conns */
710 or_conn = TO_OR_CONN(conn);
712 if (or_conn->is_bad_for_new_circs && !or_conn->n_circuits) {
713 /* It's bad for new circuits, and has no unmarked circuits on it:
714 * mark it now. */
715 log_info(LD_OR,
716 "Expiring non-used OR connection to fd %d (%s:%d) [Too old].",
717 conn->s, conn->address, conn->port);
718 if (conn->state == OR_CONN_STATE_CONNECTING)
719 connection_or_connect_failed(TO_OR_CONN(conn),
720 END_OR_CONN_REASON_TIMEOUT,
721 "Tor gave up on the connection");
722 connection_mark_for_close(conn);
723 conn->hold_open_until_flushed = 1;
724 return;
727 /* If we haven't written to an OR connection for a while, then either nuke
728 the connection or send a keepalive, depending. */
729 if (now >= conn->timestamp_lastwritten + options->KeepalivePeriod) {
730 routerinfo_t *router = router_get_by_digest(or_conn->identity_digest);
731 int maxCircuitlessPeriod = options->MaxCircuitDirtiness*3/2;
732 if (!connection_state_is_open(conn)) {
733 /* We never managed to actually get this connection open and happy. */
734 log_info(LD_OR,"Expiring non-open OR connection to fd %d (%s:%d).",
735 conn->s,conn->address, conn->port);
736 connection_mark_for_close(conn);
737 conn->hold_open_until_flushed = 1;
738 } else if (we_are_hibernating() && !or_conn->n_circuits &&
739 !buf_datalen(conn->outbuf)) {
740 /* We're hibernating, there's no circuits, and nothing to flush.*/
741 log_info(LD_OR,"Expiring non-used OR connection to fd %d (%s:%d) "
742 "[Hibernating or exiting].",
743 conn->s,conn->address, conn->port);
744 connection_mark_for_close(conn);
745 conn->hold_open_until_flushed = 1;
746 } else if (!clique_mode(options) && !or_conn->n_circuits &&
747 now >= or_conn->timestamp_last_added_nonpadding +
748 maxCircuitlessPeriod &&
749 (!router || !server_mode(options) ||
750 !router_is_clique_mode(router))) {
751 log_info(LD_OR,"Expiring non-used OR connection to fd %d (%s:%d) "
752 "[Not in clique mode].",
753 conn->s,conn->address, conn->port);
754 connection_mark_for_close(conn);
755 conn->hold_open_until_flushed = 1;
756 } else if (
757 now >= or_conn->timestamp_lastempty + options->KeepalivePeriod*10 &&
758 now >= conn->timestamp_lastwritten + options->KeepalivePeriod*10) {
759 log_fn(LOG_PROTOCOL_WARN,LD_PROTOCOL,
760 "Expiring stuck OR connection to fd %d (%s:%d). (%d bytes to "
761 "flush; %d seconds since last write)",
762 conn->s, conn->address, conn->port,
763 (int)buf_datalen(conn->outbuf),
764 (int)(now-conn->timestamp_lastwritten));
765 connection_mark_for_close(conn);
766 } else if (!buf_datalen(conn->outbuf)) {
767 /* either in clique mode, or we've got a circuit. send a padding cell. */
768 log_fn(LOG_DEBUG,LD_OR,"Sending keepalive to (%s:%d)",
769 conn->address, conn->port);
770 memset(&cell,0,sizeof(cell_t));
771 cell.command = CELL_PADDING;
772 connection_or_write_cell_to_buf(&cell, or_conn);
777 /** Honor a NEWNYM request: make future requests unlinkability to past
778 * requests. */
779 static void
780 signewnym_impl(time_t now)
782 circuit_expire_all_dirty_circs();
783 addressmap_clear_transient();
784 time_of_last_signewnym = now;
785 signewnym_is_pending = 0;
788 /** Perform regular maintenance tasks. This function gets run once per
789 * second by second_elapsed_callback().
791 static void
792 run_scheduled_events(time_t now)
794 static time_t last_rotated_x509_certificate = 0;
795 static time_t time_to_check_v3_certificate = 0;
796 static time_t time_to_check_listeners = 0;
797 static time_t time_to_check_descriptor = 0;
798 static time_t time_to_check_ipaddress = 0;
799 static time_t time_to_shrink_memory = 0;
800 static time_t time_to_try_getting_descriptors = 0;
801 static time_t time_to_reset_descriptor_failures = 0;
802 static time_t time_to_add_entropy = 0;
803 static time_t time_to_write_hs_statistics = 0;
804 static time_t time_to_write_bridge_status_file = 0;
805 static time_t time_to_downrate_stability = 0;
806 static time_t time_to_save_stability = 0;
807 static time_t time_to_clean_caches = 0;
808 static time_t time_to_recheck_bandwidth = 0;
809 static time_t time_to_check_for_expired_networkstatus = 0;
810 static time_t time_to_dump_geoip_stats = 0;
811 static time_t time_to_retry_dns_init = 0;
812 or_options_t *options = get_options();
813 int i;
814 int have_dir_info;
816 /** 0. See if we've been asked to shut down and our timeout has
817 * expired; or if our bandwidth limits are exhausted and we
818 * should hibernate; or if it's time to wake up from hibernation.
820 consider_hibernation(now);
822 /* 0b. If we've deferred a signewnym, make sure it gets handled
823 * eventually. */
824 if (signewnym_is_pending &&
825 time_of_last_signewnym + MAX_SIGNEWNYM_RATE <= now) {
826 log(LOG_INFO, LD_CONTROL, "Honoring delayed NEWNYM request");
827 signewnym_impl(now);
830 /** 1a. Every MIN_ONION_KEY_LIFETIME seconds, rotate the onion keys,
831 * shut down and restart all cpuworkers, and update the directory if
832 * necessary.
834 if (server_mode(options) &&
835 get_onion_key_set_at()+MIN_ONION_KEY_LIFETIME < now) {
836 log_info(LD_GENERAL,"Rotating onion key.");
837 rotate_onion_key();
838 cpuworkers_rotate();
839 if (router_rebuild_descriptor(1)<0) {
840 log_info(LD_CONFIG, "Couldn't rebuild router descriptor");
842 if (advertised_server_mode())
843 router_upload_dir_desc_to_dirservers(0);
846 if (time_to_try_getting_descriptors < now) {
847 update_router_descriptor_downloads(now);
848 update_extrainfo_downloads(now);
849 if (options->UseBridges)
850 fetch_bridge_descriptors(now);
851 if (router_have_minimum_dir_info())
852 time_to_try_getting_descriptors = now + LAZY_DESCRIPTOR_RETRY_INTERVAL;
853 else
854 time_to_try_getting_descriptors = now + GREEDY_DESCRIPTOR_RETRY_INTERVAL;
857 if (time_to_reset_descriptor_failures < now) {
858 router_reset_descriptor_download_failures();
859 time_to_reset_descriptor_failures =
860 now + DESCRIPTOR_FAILURE_RESET_INTERVAL;
863 /** 1b. Every MAX_SSL_KEY_LIFETIME seconds, we change our TLS context. */
864 if (!last_rotated_x509_certificate)
865 last_rotated_x509_certificate = now;
866 if (last_rotated_x509_certificate+MAX_SSL_KEY_LIFETIME < now) {
867 log_info(LD_GENERAL,"Rotating tls context.");
868 if (tor_tls_context_new(get_identity_key(), MAX_SSL_KEY_LIFETIME) < 0) {
869 log_warn(LD_BUG, "Error reinitializing TLS context");
870 /* XXX is it a bug here, that we just keep going? -RD */
872 last_rotated_x509_certificate = now;
873 /* We also make sure to rotate the TLS connections themselves if they've
874 * been up for too long -- but that's done via is_bad_for_new_circs in
875 * connection_run_housekeeping() above. */
878 if (time_to_add_entropy < now) {
879 if (time_to_add_entropy) {
880 /* We already seeded once, so don't die on failure. */
881 crypto_seed_rng(0);
883 /** How often do we add more entropy to OpenSSL's RNG pool? */
884 #define ENTROPY_INTERVAL (60*60)
885 time_to_add_entropy = now + ENTROPY_INTERVAL;
888 /** 1c. If we have to change the accounting interval or record
889 * bandwidth used in this accounting interval, do so. */
890 if (accounting_is_enabled(options))
891 accounting_run_housekeeping(now);
893 if (now % 10 == 0 && (authdir_mode_tests_reachability(options)) &&
894 !we_are_hibernating()) {
895 /* try to determine reachability of the other Tor relays */
896 dirserv_test_reachability(now, 0);
899 /** 1d. Periodically, we discount older stability information so that new
900 * stability info counts more, and save the stability information to disk as
901 * appropriate. */
902 if (time_to_downrate_stability < now)
903 time_to_downrate_stability = rep_hist_downrate_old_runs(now);
904 if (authdir_mode_tests_reachability(options)) {
905 if (time_to_save_stability < now) {
906 if (time_to_save_stability && rep_hist_record_mtbf_data(now, 1)<0) {
907 log_warn(LD_GENERAL, "Couldn't store mtbf data.");
909 #define SAVE_STABILITY_INTERVAL (30*60)
910 time_to_save_stability = now + SAVE_STABILITY_INTERVAL;
914 /* 1e. Periodically, if we're a v3 authority, we check whether our cert is
915 * close to expiring and warn the admin if it is. */
916 if (time_to_check_v3_certificate < now) {
917 v3_authority_check_key_expiry();
918 #define CHECK_V3_CERTIFICATE_INTERVAL (5*60)
919 time_to_check_v3_certificate = now + CHECK_V3_CERTIFICATE_INTERVAL;
922 /* 1f. Check whether our networkstatus has expired.
924 if (time_to_check_for_expired_networkstatus < now) {
925 networkstatus_t *ns = networkstatus_get_latest_consensus();
926 /*XXXX RD: This value needs to be the same as REASONABLY_LIVE_TIME in
927 * networkstatus_get_reasonably_live_consensus(), but that value is way
928 * way too high. Arma: is the bridge issue there resolved yet? -NM */
929 #define NS_EXPIRY_SLOP (24*60*60)
930 if (ns && ns->valid_until < now+NS_EXPIRY_SLOP &&
931 router_have_minimum_dir_info()) {
932 router_dir_info_changed();
934 #define CHECK_EXPIRED_NS_INTERVAL (2*60)
935 time_to_check_for_expired_networkstatus = now + CHECK_EXPIRED_NS_INTERVAL;
938 if (time_to_dump_geoip_stats < now) {
939 #define DUMP_GEOIP_STATS_INTERVAL (60*60);
940 if (time_to_dump_geoip_stats)
941 dump_geoip_stats();
942 time_to_dump_geoip_stats = now + DUMP_GEOIP_STATS_INTERVAL;
945 /* Remove old information from rephist and the rend cache. */
946 if (time_to_clean_caches < now) {
947 rep_history_clean(now - options->RephistTrackTime);
948 rend_cache_clean();
949 rend_cache_clean_v2_descs_as_dir();
950 #define CLEAN_CACHES_INTERVAL (30*60)
951 time_to_clean_caches = now + CLEAN_CACHES_INTERVAL;
954 #define RETRY_DNS_INTERVAL (10*60)
955 /* If we're a server and initializing dns failed, retry periodically. */
956 if (time_to_retry_dns_init < now) {
957 time_to_retry_dns_init = now + RETRY_DNS_INTERVAL;
958 if (server_mode(options) && has_dns_init_failed())
959 dns_init();
962 /** 2. Periodically, we consider force-uploading our descriptor
963 * (if we've passed our internal checks). */
965 /** How often do we check whether part of our router info has changed in a way
966 * that would require an upload? */
967 #define CHECK_DESCRIPTOR_INTERVAL (60)
968 /** How often do we (as a router) check whether our IP address has changed? */
969 #define CHECK_IPADDRESS_INTERVAL (15*60)
971 /* 2b. Once per minute, regenerate and upload the descriptor if the old
972 * one is inaccurate. */
973 if (time_to_check_descriptor < now) {
974 static int dirport_reachability_count = 0;
975 time_to_check_descriptor = now + CHECK_DESCRIPTOR_INTERVAL;
976 check_descriptor_bandwidth_changed(now);
977 if (time_to_check_ipaddress < now) {
978 time_to_check_ipaddress = now + CHECK_IPADDRESS_INTERVAL;
979 check_descriptor_ipaddress_changed(now);
981 /** If our router descriptor ever goes this long without being regenerated
982 * because something changed, we force an immediate regenerate-and-upload. */
983 #define FORCE_REGENERATE_DESCRIPTOR_INTERVAL (18*60*60)
984 mark_my_descriptor_dirty_if_older_than(
985 now - FORCE_REGENERATE_DESCRIPTOR_INTERVAL);
986 consider_publishable_server(0);
987 /* also, check religiously for reachability, if it's within the first
988 * 20 minutes of our uptime. */
989 if (server_mode(options) &&
990 (has_completed_circuit || !any_predicted_circuits(now)) &&
991 !we_are_hibernating()) {
992 if (stats_n_seconds_working < TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT) {
993 consider_testing_reachability(1, dirport_reachability_count==0);
994 if (++dirport_reachability_count > 5)
995 dirport_reachability_count = 0;
996 } else if (time_to_recheck_bandwidth < now) {
997 /* If we haven't checked for 12 hours and our bandwidth estimate is
998 * low, do another bandwidth test. This is especially important for
999 * bridges, since they might go long periods without much use. */
1000 routerinfo_t *me = router_get_my_routerinfo();
1001 if (time_to_recheck_bandwidth && me &&
1002 me->bandwidthcapacity < me->bandwidthrate &&
1003 me->bandwidthcapacity < 51200) {
1004 reset_bandwidth_test();
1006 #define BANDWIDTH_RECHECK_INTERVAL (12*60*60)
1007 time_to_recheck_bandwidth = now + BANDWIDTH_RECHECK_INTERVAL;
1011 /* If any networkstatus documents are no longer recent, we need to
1012 * update all the descriptors' running status. */
1013 /* purge obsolete entries */
1014 networkstatus_v2_list_clean(now);
1015 /* Remove dead routers. */
1016 routerlist_remove_old_routers();
1018 /* Also, once per minute, check whether we want to download any
1019 * networkstatus documents.
1021 update_networkstatus_downloads(now);
1024 /** 2c. Let directory voting happen. */
1025 if (authdir_mode_v3(options))
1026 dirvote_act(options, now);
1028 /** 3a. Every second, we examine pending circuits and prune the
1029 * ones which have been pending for more than a few seconds.
1030 * We do this before step 4, so it can try building more if
1031 * it's not comfortable with the number of available circuits.
1033 circuit_expire_building(now);
1035 /** 3b. Also look at pending streams and prune the ones that 'began'
1036 * a long time ago but haven't gotten a 'connected' yet.
1037 * Do this before step 4, so we can put them back into pending
1038 * state to be picked up by the new circuit.
1040 connection_ap_expire_beginning();
1042 /** 3c. And expire connections that we've held open for too long.
1044 connection_expire_held_open();
1046 /** 3d. And every 60 seconds, we relaunch listeners if any died. */
1047 if (!we_are_hibernating() && time_to_check_listeners < now) {
1048 retry_all_listeners(NULL, NULL);
1049 time_to_check_listeners = now+60;
1052 /** 4. Every second, we try a new circuit if there are no valid
1053 * circuits. Every NewCircuitPeriod seconds, we expire circuits
1054 * that became dirty more than MaxCircuitDirtiness seconds ago,
1055 * and we make a new circ if there are no clean circuits.
1057 have_dir_info = router_have_minimum_dir_info();
1058 if (have_dir_info && !we_are_hibernating())
1059 circuit_build_needed_circs(now);
1061 /* every 10 seconds, but not at the same second as other such events */
1062 if (now % 10 == 5)
1063 circuit_expire_old_circuits_serverside(now);
1065 /** 5. We do housekeeping for each connection... */
1066 connection_or_set_bad_connections();
1067 for (i=0;i<smartlist_len(connection_array);i++) {
1068 run_connection_housekeeping(i, now);
1070 if (time_to_shrink_memory < now) {
1071 SMARTLIST_FOREACH(connection_array, connection_t *, conn, {
1072 if (conn->outbuf)
1073 buf_shrink(conn->outbuf);
1074 if (conn->inbuf)
1075 buf_shrink(conn->inbuf);
1077 clean_cell_pool();
1078 buf_shrink_freelists(0);
1079 /** How often do we check buffers and pools for empty space that can be
1080 * deallocated? */
1081 #define MEM_SHRINK_INTERVAL (60)
1082 time_to_shrink_memory = now + MEM_SHRINK_INTERVAL;
1085 /** 6. And remove any marked circuits... */
1086 circuit_close_all_marked();
1088 /** 7. And upload service descriptors if necessary. */
1089 if (has_completed_circuit && !we_are_hibernating()) {
1090 rend_consider_services_upload(now);
1091 rend_consider_descriptor_republication();
1094 /** 8. and blow away any connections that need to die. have to do this now,
1095 * because if we marked a conn for close and left its socket -1, then
1096 * we'll pass it to poll/select and bad things will happen.
1098 close_closeable_connections();
1100 /** 8b. And if anything in our state is ready to get flushed to disk, we
1101 * flush it. */
1102 or_state_save(now);
1104 /** 9. and if we're a server, check whether our DNS is telling stories to
1105 * us. */
1106 if (server_mode(options) && time_to_check_for_correct_dns < now) {
1107 if (!time_to_check_for_correct_dns) {
1108 time_to_check_for_correct_dns = now + 60 + crypto_rand_int(120);
1109 } else {
1110 dns_launch_correctness_checks();
1111 time_to_check_for_correct_dns = now + 12*3600 +
1112 crypto_rand_int(12*3600);
1116 /** 10. write hidden service usage statistic to disk */
1117 if (options->HSAuthorityRecordStats && time_to_write_hs_statistics < now) {
1118 hs_usage_write_statistics_to_file(now);
1119 #define WRITE_HSUSAGE_INTERVAL (30*60)
1120 time_to_write_hs_statistics = now+WRITE_HSUSAGE_INTERVAL;
1122 /** 10b. write bridge networkstatus file to disk */
1123 if (options->BridgeAuthoritativeDir &&
1124 time_to_write_bridge_status_file < now) {
1125 networkstatus_dump_bridge_status_to_file(now);
1126 #define BRIDGE_STATUSFILE_INTERVAL (30*60)
1127 time_to_write_bridge_status_file = now+BRIDGE_STATUSFILE_INTERVAL;
1131 /** Libevent timer: used to invoke second_elapsed_callback() once per
1132 * second. */
1133 static struct event *timeout_event = NULL;
1134 /** Number of libevent errors in the last second: we die if we get too many. */
1135 static int n_libevent_errors = 0;
1137 /** Libevent callback: invoked once every second. */
1138 static void
1139 second_elapsed_callback(int fd, short event, void *args)
1141 /* XXXX This could be sensibly refactored into multiple callbacks, and we
1142 * could use Libevent's timers for this rather than checking the current
1143 * time against a bunch of timeouts every second. */
1144 static struct timeval one_second;
1145 static time_t current_second = 0;
1146 time_t now;
1147 size_t bytes_written;
1148 size_t bytes_read;
1149 int seconds_elapsed;
1150 or_options_t *options = get_options();
1151 (void)fd;
1152 (void)event;
1153 (void)args;
1154 if (!timeout_event) {
1155 timeout_event = tor_malloc_zero(sizeof(struct event));
1156 evtimer_set(timeout_event, second_elapsed_callback, NULL);
1157 one_second.tv_sec = 1;
1158 one_second.tv_usec = 0;
1161 n_libevent_errors = 0;
1163 /* log_fn(LOG_NOTICE, "Tick."); */
1164 now = time(NULL);
1165 update_approx_time(now);
1167 /* the second has rolled over. check more stuff. */
1168 bytes_written = stats_prev_global_write_bucket - global_write_bucket;
1169 bytes_read = stats_prev_global_read_bucket - global_read_bucket;
1170 seconds_elapsed = current_second ? (int)(now - current_second) : 0;
1171 stats_n_bytes_read += bytes_read;
1172 stats_n_bytes_written += bytes_written;
1173 if (accounting_is_enabled(options) && seconds_elapsed >= 0)
1174 accounting_add_bytes(bytes_read, bytes_written, seconds_elapsed);
1175 control_event_bandwidth_used((uint32_t)bytes_read,(uint32_t)bytes_written);
1176 control_event_stream_bandwidth_used();
1178 if (seconds_elapsed > 0)
1179 connection_bucket_refill(seconds_elapsed, now);
1180 stats_prev_global_read_bucket = global_read_bucket;
1181 stats_prev_global_write_bucket = global_write_bucket;
1183 if (server_mode(options) &&
1184 !we_are_hibernating() &&
1185 seconds_elapsed > 0 &&
1186 has_completed_circuit &&
1187 stats_n_seconds_working / TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT !=
1188 (stats_n_seconds_working+seconds_elapsed) /
1189 TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT) {
1190 /* every 20 minutes, check and complain if necessary */
1191 routerinfo_t *me = router_get_my_routerinfo();
1192 if (me && !check_whether_orport_reachable()) {
1193 log_warn(LD_CONFIG,"Your server (%s:%d) has not managed to confirm that "
1194 "its ORPort is reachable. Please check your firewalls, ports, "
1195 "address, /etc/hosts file, etc.",
1196 me->address, me->or_port);
1197 control_event_server_status(LOG_WARN,
1198 "REACHABILITY_FAILED ORADDRESS=%s:%d",
1199 me->address, me->or_port);
1202 if (me && !check_whether_dirport_reachable()) {
1203 log_warn(LD_CONFIG,
1204 "Your server (%s:%d) has not managed to confirm that its "
1205 "DirPort is reachable. Please check your firewalls, ports, "
1206 "address, /etc/hosts file, etc.",
1207 me->address, me->dir_port);
1208 control_event_server_status(LOG_WARN,
1209 "REACHABILITY_FAILED DIRADDRESS=%s:%d",
1210 me->address, me->dir_port);
1214 /** If more than this many seconds have elapsed, probably the clock
1215 * jumped: doesn't count. */
1216 #define NUM_JUMPED_SECONDS_BEFORE_WARN 100
1217 if (seconds_elapsed < -NUM_JUMPED_SECONDS_BEFORE_WARN ||
1218 seconds_elapsed >= NUM_JUMPED_SECONDS_BEFORE_WARN) {
1219 circuit_note_clock_jumped(seconds_elapsed);
1220 /* XXX if the time jumps *back* many months, do our events in
1221 * run_scheduled_events() recover? I don't think they do. -RD */
1222 } else if (seconds_elapsed > 0)
1223 stats_n_seconds_working += seconds_elapsed;
1225 run_scheduled_events(now);
1227 current_second = now; /* remember which second it is, for next time */
1229 #if 0
1230 if (current_second % 300 == 0) {
1231 rep_history_clean(current_second - options->RephistTrackTime);
1232 dumpmemusage(get_min_log_level()<LOG_INFO ?
1233 get_min_log_level() : LOG_INFO);
1235 #endif
1237 if (evtimer_add(timeout_event, &one_second))
1238 log_err(LD_NET,
1239 "Error from libevent when setting one-second timeout event");
1242 #ifndef MS_WINDOWS
1243 /** Called when a possibly ignorable libevent error occurs; ensures that we
1244 * don't get into an infinite loop by ignoring too many errors from
1245 * libevent. */
1246 static int
1247 got_libevent_error(void)
1249 if (++n_libevent_errors > 8) {
1250 log_err(LD_NET, "Too many libevent errors in one second; dying");
1251 return -1;
1253 return 0;
1255 #endif
1257 #define UPTIME_CUTOFF_FOR_NEW_BANDWIDTH_TEST (6*60*60)
1259 /** Called when our IP address seems to have changed. <b>at_interface</b>
1260 * should be true if we detected a change in our interface, and false if we
1261 * detected a change in our published address. */
1262 void
1263 ip_address_changed(int at_interface)
1265 int server = server_mode(get_options());
1267 if (at_interface) {
1268 if (! server) {
1269 /* Okay, change our keys. */
1270 init_keys();
1272 } else {
1273 if (server) {
1274 if (stats_n_seconds_working > UPTIME_CUTOFF_FOR_NEW_BANDWIDTH_TEST)
1275 reset_bandwidth_test();
1276 stats_n_seconds_working = 0;
1277 router_reset_reachability();
1278 mark_my_descriptor_dirty();
1282 dns_servers_relaunch_checks();
1285 /** Forget what we've learned about the correctness of our DNS servers, and
1286 * start learning again. */
1287 void
1288 dns_servers_relaunch_checks(void)
1290 if (server_mode(get_options())) {
1291 dns_reset_correctness_checks();
1292 time_to_check_for_correct_dns = 0;
1296 /** Called when we get a SIGHUP: reload configuration files and keys,
1297 * retry all connections, and so on. */
1298 static int
1299 do_hup(void)
1301 or_options_t *options = get_options();
1303 #ifdef USE_DMALLOC
1304 dmalloc_log_stats();
1305 dmalloc_log_changed(0, 1, 0, 0);
1306 #endif
1308 log_notice(LD_GENERAL,"Received reload signal (hup). Reloading config and "
1309 "resetting internal state.");
1310 if (accounting_is_enabled(options))
1311 accounting_record_bandwidth_usage(time(NULL), get_or_state());
1313 router_reset_warnings();
1314 routerlist_reset_warnings();
1315 addressmap_clear_transient();
1316 /* first, reload config variables, in case they've changed */
1317 if (options->ReloadTorrcOnSIGHUP) {
1318 /* no need to provide argc/v, they've been cached in init_from_config */
1319 if (options_init_from_torrc(0, NULL) < 0) {
1320 log_err(LD_CONFIG,"Reading config failed--see warnings above. "
1321 "For usage, try -h.");
1322 return -1;
1324 options = get_options(); /* they have changed now */
1325 } else {
1326 log_notice(LD_GENERAL, "Not reloading config file: the controller told "
1327 "us not to.");
1329 if (authdir_mode_handles_descs(options, -1)) {
1330 /* reload the approved-routers file */
1331 if (dirserv_load_fingerprint_file() < 0) {
1332 /* warnings are logged from dirserv_load_fingerprint_file() directly */
1333 log_info(LD_GENERAL, "Error reloading fingerprints. "
1334 "Continuing with old list.");
1338 /* Rotate away from the old dirty circuits. This has to be done
1339 * after we've read the new options, but before we start using
1340 * circuits for directory fetches. */
1341 circuit_expire_all_dirty_circs();
1343 /* retry appropriate downloads */
1344 router_reset_status_download_failures();
1345 router_reset_descriptor_download_failures();
1346 update_networkstatus_downloads(time(NULL));
1348 /* We'll retry routerstatus downloads in about 10 seconds; no need to
1349 * force a retry there. */
1351 if (server_mode(options)) {
1352 /* Restart cpuworker and dnsworker processes, so they get up-to-date
1353 * configuration options. */
1354 cpuworkers_rotate();
1355 dns_reset();
1357 return 0;
1360 /** Tor main loop. */
1361 /* static */ int
1362 do_main_loop(void)
1364 int loop_result;
1365 time_t now;
1367 /* initialize dns resolve map, spawn workers if needed */
1368 if (dns_init() < 0) {
1369 if (get_options()->ServerDNSAllowBrokenConfig)
1370 log_warn(LD_GENERAL, "Couldn't set up any working nameservers. "
1371 "Network not up yet? Will try again soon.");
1372 else {
1373 log_err(LD_GENERAL,"Error initializing dns subsystem; exiting. To "
1374 "retry instead, set the ServerDNSAllowBrokenResolvConf option.");
1378 handle_signals(1);
1380 /* load the private keys, if we're supposed to have them, and set up the
1381 * TLS context. */
1382 if (! identity_key_is_set()) {
1383 if (init_keys() < 0) {
1384 log_err(LD_BUG,"Error initializing keys; exiting");
1385 return -1;
1389 /* Set up the packed_cell_t memory pool. */
1390 init_cell_pool();
1392 /* Set up our buckets */
1393 connection_bucket_init();
1394 stats_prev_global_read_bucket = global_read_bucket;
1395 stats_prev_global_write_bucket = global_write_bucket;
1397 /* initialize the bootstrap status events to know we're starting up */
1398 control_event_bootstrap(BOOTSTRAP_STATUS_STARTING, 0);
1400 if (trusted_dirs_reload_certs())
1401 return -1;
1402 if (router_reload_v2_networkstatus()) {
1403 return -1;
1405 if (router_reload_consensus_networkstatus()) {
1406 return -1;
1408 /* load the routers file, or assign the defaults. */
1409 if (router_reload_router_list()) {
1410 return -1;
1412 /* load the networkstatuses. (This launches a download for new routers as
1413 * appropriate.)
1415 now = time(NULL);
1416 directory_info_has_arrived(now, 1);
1418 if (authdir_mode_tests_reachability(get_options())) {
1419 /* the directory is already here, run startup things */
1420 dirserv_test_reachability(now, 1);
1423 if (server_mode(get_options())) {
1424 /* launch cpuworkers. Need to do this *after* we've read the onion key. */
1425 cpu_init();
1428 /* set up once-a-second callback. */
1429 second_elapsed_callback(0,0,NULL);
1431 for (;;) {
1432 if (nt_service_is_stopping())
1433 return 0;
1435 #ifndef MS_WINDOWS
1436 /* Make it easier to tell whether libevent failure is our fault or not. */
1437 errno = 0;
1438 #endif
1439 /* All active linked conns should get their read events activated. */
1440 SMARTLIST_FOREACH(active_linked_connection_lst, connection_t *, conn,
1441 event_active(conn->read_event, EV_READ, 1));
1442 called_loop_once = smartlist_len(active_linked_connection_lst) ? 1 : 0;
1444 update_approx_time(time(NULL));
1446 /* poll until we have an event, or the second ends, or until we have
1447 * some active linked connections to trigger events for. */
1448 loop_result = event_loop(called_loop_once ? EVLOOP_ONCE : 0);
1450 /* let catch() handle things like ^c, and otherwise don't worry about it */
1451 if (loop_result < 0) {
1452 int e = tor_socket_errno(-1);
1453 /* let the program survive things like ^z */
1454 if (e != EINTR && !ERRNO_IS_EINPROGRESS(e)) {
1455 #ifdef HAVE_EVENT_GET_METHOD
1456 log_err(LD_NET,"libevent call with %s failed: %s [%d]",
1457 event_get_method(), tor_socket_strerror(e), e);
1458 #else
1459 log_err(LD_NET,"libevent call failed: %s [%d]",
1460 tor_socket_strerror(e), e);
1461 #endif
1462 return -1;
1463 #ifndef MS_WINDOWS
1464 } else if (e == EINVAL) {
1465 log_warn(LD_NET, "EINVAL from libevent: should you upgrade libevent?");
1466 if (got_libevent_error())
1467 return -1;
1468 #endif
1469 } else {
1470 if (ERRNO_IS_EINPROGRESS(e))
1471 log_warn(LD_BUG,
1472 "libevent call returned EINPROGRESS? Please report.");
1473 log_debug(LD_NET,"libevent call interrupted.");
1474 /* You can't trust the results of this poll(). Go back to the
1475 * top of the big for loop. */
1476 continue;
1482 /** Used to implement the SIGNAL control command: if we accept
1483 * <b>the_signal</b> as a remote pseudo-signal, act on it. */
1484 /* We don't re-use catch() here because:
1485 * 1. We handle a different set of signals than those allowed in catch.
1486 * 2. Platforms without signal() are unlikely to define SIGfoo.
1487 * 3. The control spec is defined to use fixed numeric signal values
1488 * which just happen to match the Unix values.
1490 void
1491 control_signal_act(int the_signal)
1493 switch (the_signal)
1495 case 1:
1496 signal_callback(0,0,(void*)(uintptr_t)SIGHUP);
1497 break;
1498 case 2:
1499 signal_callback(0,0,(void*)(uintptr_t)SIGINT);
1500 break;
1501 case 10:
1502 signal_callback(0,0,(void*)(uintptr_t)SIGUSR1);
1503 break;
1504 case 12:
1505 signal_callback(0,0,(void*)(uintptr_t)SIGUSR2);
1506 break;
1507 case 15:
1508 signal_callback(0,0,(void*)(uintptr_t)SIGTERM);
1509 break;
1510 case SIGNEWNYM:
1511 signal_callback(0,0,(void*)(uintptr_t)SIGNEWNYM);
1512 break;
1513 case SIGCLEARDNSCACHE:
1514 signal_callback(0,0,(void*)(uintptr_t)SIGCLEARDNSCACHE);
1515 break;
1516 default:
1517 log_warn(LD_BUG, "Unrecognized signal number %d.", the_signal);
1518 break;
1522 /** Libevent callback: invoked when we get a signal.
1524 static void
1525 signal_callback(int fd, short events, void *arg)
1527 uintptr_t sig = (uintptr_t)arg;
1528 (void)fd;
1529 (void)events;
1530 switch (sig)
1532 case SIGTERM:
1533 log_notice(LD_GENERAL,"Catching signal TERM, exiting cleanly.");
1534 tor_cleanup();
1535 exit(0);
1536 break;
1537 case SIGINT:
1538 if (!server_mode(get_options())) { /* do it now */
1539 log_notice(LD_GENERAL,"Interrupt: exiting cleanly.");
1540 tor_cleanup();
1541 exit(0);
1543 hibernate_begin_shutdown();
1544 break;
1545 #ifdef SIGPIPE
1546 case SIGPIPE:
1547 log_debug(LD_GENERAL,"Caught SIGPIPE. Ignoring.");
1548 break;
1549 #endif
1550 case SIGUSR1:
1551 /* prefer to log it at INFO, but make sure we always see it */
1552 dumpstats(get_min_log_level()<LOG_INFO ? get_min_log_level() : LOG_INFO);
1553 break;
1554 case SIGUSR2:
1555 switch_logs_debug();
1556 log_debug(LD_GENERAL,"Caught USR2, going to loglevel debug. "
1557 "Send HUP to change back.");
1558 break;
1559 case SIGHUP:
1560 if (do_hup() < 0) {
1561 log_warn(LD_CONFIG,"Restart failed (config error?). Exiting.");
1562 tor_cleanup();
1563 exit(1);
1565 break;
1566 #ifdef SIGCHLD
1567 case SIGCHLD:
1568 while (waitpid(-1,NULL,WNOHANG) > 0) ; /* keep reaping until no more
1569 zombies */
1570 break;
1571 #endif
1572 case SIGNEWNYM: {
1573 time_t now = time(NULL);
1574 if (time_of_last_signewnym + MAX_SIGNEWNYM_RATE > now) {
1575 signewnym_is_pending = 1;
1576 log(LOG_NOTICE, LD_CONTROL,
1577 "Rate limiting NEWNYM request: delaying by %d second(s)",
1578 (int)(MAX_SIGNEWNYM_RATE+time_of_last_signewnym-now));
1579 } else {
1580 signewnym_impl(now);
1582 break;
1584 case SIGCLEARDNSCACHE:
1585 addressmap_clear_transient();
1586 break;
1590 extern uint64_t rephist_total_alloc;
1591 extern uint32_t rephist_total_num;
1594 * Write current memory usage information to the log.
1596 static void
1597 dumpmemusage(int severity)
1599 connection_dump_buffer_mem_stats(severity);
1600 log(severity, LD_GENERAL, "In rephist: "U64_FORMAT" used by %d Tors.",
1601 U64_PRINTF_ARG(rephist_total_alloc), rephist_total_num);
1602 dump_routerlist_mem_usage(severity);
1603 dump_cell_pool_usage(severity);
1604 buf_dump_freelist_sizes(severity);
1605 tor_log_mallinfo(severity);
1608 /** Write all statistics to the log, with log level 'severity'. Called
1609 * in response to a SIGUSR1. */
1610 static void
1611 dumpstats(int severity)
1613 time_t now = time(NULL);
1614 time_t elapsed;
1615 size_t rbuf_cap, wbuf_cap, rbuf_len, wbuf_len;
1617 log(severity, LD_GENERAL, "Dumping stats:");
1619 SMARTLIST_FOREACH(connection_array, connection_t *, conn,
1621 int i = conn_sl_idx;
1622 log(severity, LD_GENERAL,
1623 "Conn %d (socket %d) type %d (%s), state %d (%s), created %d secs ago",
1624 i, conn->s, conn->type, conn_type_to_string(conn->type),
1625 conn->state, conn_state_to_string(conn->type, conn->state),
1626 (int)(now - conn->timestamp_created));
1627 if (!connection_is_listener(conn)) {
1628 log(severity,LD_GENERAL,
1629 "Conn %d is to %s:%d.", i,
1630 safe_str(conn->address), conn->port);
1631 log(severity,LD_GENERAL,
1632 "Conn %d: %d bytes waiting on inbuf (len %d, last read %d secs ago)",
1634 (int)buf_datalen(conn->inbuf),
1635 (int)buf_allocation(conn->inbuf),
1636 (int)(now - conn->timestamp_lastread));
1637 log(severity,LD_GENERAL,
1638 "Conn %d: %d bytes waiting on outbuf "
1639 "(len %d, last written %d secs ago)",i,
1640 (int)buf_datalen(conn->outbuf),
1641 (int)buf_allocation(conn->outbuf),
1642 (int)(now - conn->timestamp_lastwritten));
1643 if (conn->type == CONN_TYPE_OR) {
1644 or_connection_t *or_conn = TO_OR_CONN(conn);
1645 if (or_conn->tls) {
1646 tor_tls_get_buffer_sizes(or_conn->tls, &rbuf_cap, &rbuf_len,
1647 &wbuf_cap, &wbuf_len);
1648 log(severity, LD_GENERAL,
1649 "Conn %d: %d/%d bytes used on OpenSSL read buffer; "
1650 "%d/%d bytes used on write buffer.",
1651 i, (int)rbuf_len, (int)rbuf_cap, (int)wbuf_len, (int)wbuf_cap);
1655 circuit_dump_by_conn(conn, severity); /* dump info about all the circuits
1656 * using this conn */
1658 log(severity, LD_NET,
1659 "Cells processed: "U64_FORMAT" padding\n"
1660 " "U64_FORMAT" create\n"
1661 " "U64_FORMAT" created\n"
1662 " "U64_FORMAT" relay\n"
1663 " ("U64_FORMAT" relayed)\n"
1664 " ("U64_FORMAT" delivered)\n"
1665 " "U64_FORMAT" destroy",
1666 U64_PRINTF_ARG(stats_n_padding_cells_processed),
1667 U64_PRINTF_ARG(stats_n_create_cells_processed),
1668 U64_PRINTF_ARG(stats_n_created_cells_processed),
1669 U64_PRINTF_ARG(stats_n_relay_cells_processed),
1670 U64_PRINTF_ARG(stats_n_relay_cells_relayed),
1671 U64_PRINTF_ARG(stats_n_relay_cells_delivered),
1672 U64_PRINTF_ARG(stats_n_destroy_cells_processed));
1673 if (stats_n_data_cells_packaged)
1674 log(severity,LD_NET,"Average packaged cell fullness: %2.3f%%",
1675 100*(U64_TO_DBL(stats_n_data_bytes_packaged) /
1676 U64_TO_DBL(stats_n_data_cells_packaged*RELAY_PAYLOAD_SIZE)) );
1677 if (stats_n_data_cells_received)
1678 log(severity,LD_NET,"Average delivered cell fullness: %2.3f%%",
1679 100*(U64_TO_DBL(stats_n_data_bytes_received) /
1680 U64_TO_DBL(stats_n_data_cells_received*RELAY_PAYLOAD_SIZE)) );
1682 if (now - time_of_process_start >= 0)
1683 elapsed = now - time_of_process_start;
1684 else
1685 elapsed = 0;
1687 if (elapsed) {
1688 log(severity, LD_NET,
1689 "Average bandwidth: "U64_FORMAT"/%d = %d bytes/sec reading",
1690 U64_PRINTF_ARG(stats_n_bytes_read),
1691 (int)elapsed,
1692 (int) (stats_n_bytes_read/elapsed));
1693 log(severity, LD_NET,
1694 "Average bandwidth: "U64_FORMAT"/%d = %d bytes/sec writing",
1695 U64_PRINTF_ARG(stats_n_bytes_written),
1696 (int)elapsed,
1697 (int) (stats_n_bytes_written/elapsed));
1700 log(severity, LD_NET, "--------------- Dumping memory information:");
1701 dumpmemusage(severity);
1703 rep_hist_dump_stats(now,severity);
1704 rend_service_dump_stats(severity);
1705 dump_pk_ops(severity);
1706 dump_distinct_digest_count(severity);
1709 /** Called by exit() as we shut down the process.
1711 static void
1712 exit_function(void)
1714 /* NOTE: If we ever daemonize, this gets called immediately. That's
1715 * okay for now, because we only use this on Windows. */
1716 #ifdef MS_WINDOWS
1717 WSACleanup();
1718 #endif
1721 /** Set up the signal handlers for either parent or child. */
1722 void
1723 handle_signals(int is_parent)
1725 #ifndef MS_WINDOWS /* do signal stuff only on Unix */
1726 int i;
1727 static int signals[] = {
1728 SIGINT, /* do a controlled slow shutdown */
1729 SIGTERM, /* to terminate now */
1730 SIGPIPE, /* otherwise SIGPIPE kills us */
1731 SIGUSR1, /* dump stats */
1732 SIGUSR2, /* go to loglevel debug */
1733 SIGHUP, /* to reload config, retry conns, etc */
1734 #ifdef SIGXFSZ
1735 SIGXFSZ, /* handle file-too-big resource exhaustion */
1736 #endif
1737 SIGCHLD, /* handle dns/cpu workers that exit */
1738 -1 };
1739 static struct event signal_events[16]; /* bigger than it has to be. */
1740 if (is_parent) {
1741 for (i = 0; signals[i] >= 0; ++i) {
1742 signal_set(&signal_events[i], signals[i], signal_callback,
1743 (void*)(uintptr_t)signals[i]);
1744 if (signal_add(&signal_events[i], NULL))
1745 log_warn(LD_BUG, "Error from libevent when adding event for signal %d",
1746 signals[i]);
1748 } else {
1749 struct sigaction action;
1750 action.sa_flags = 0;
1751 sigemptyset(&action.sa_mask);
1752 action.sa_handler = SIG_IGN;
1753 sigaction(SIGINT, &action, NULL);
1754 sigaction(SIGTERM, &action, NULL);
1755 sigaction(SIGPIPE, &action, NULL);
1756 sigaction(SIGUSR1, &action, NULL);
1757 sigaction(SIGUSR2, &action, NULL);
1758 sigaction(SIGHUP, &action, NULL);
1759 #ifdef SIGXFSZ
1760 sigaction(SIGXFSZ, &action, NULL);
1761 #endif
1763 #else /* MS windows */
1764 (void)is_parent;
1765 #endif /* signal stuff */
1768 /** Main entry point for the Tor command-line client.
1770 /* static */ int
1771 tor_init(int argc, char *argv[])
1773 char buf[256];
1774 int i, quiet = 0;
1775 time_of_process_start = time(NULL);
1776 if (!connection_array)
1777 connection_array = smartlist_create();
1778 if (!closeable_connection_lst)
1779 closeable_connection_lst = smartlist_create();
1780 if (!active_linked_connection_lst)
1781 active_linked_connection_lst = smartlist_create();
1782 /* Have the log set up with our application name. */
1783 tor_snprintf(buf, sizeof(buf), "Tor %s", get_version());
1784 log_set_application_name(buf);
1785 /* Initialize the history structures. */
1786 rep_hist_init();
1787 /* Initialize the service cache. */
1788 rend_cache_init();
1789 addressmap_init(); /* Init the client dns cache. Do it always, since it's
1790 * cheap. */
1792 /* We search for the "quiet" option first, since it decides whether we
1793 * will log anything at all to the command line. */
1794 for (i=1;i<argc;++i) {
1795 if (!strcmp(argv[i], "--hush"))
1796 quiet = 1;
1797 if (!strcmp(argv[i], "--quiet"))
1798 quiet = 2;
1800 /* give it somewhere to log to initially */
1801 switch (quiet) {
1802 case 2:
1803 /* no initial logging */
1804 break;
1805 case 1:
1806 add_temp_log(LOG_WARN);
1807 break;
1808 default:
1809 add_temp_log(LOG_NOTICE);
1812 log(LOG_NOTICE, LD_GENERAL, "Tor v%s. This is experimental software. "
1813 "Do not rely on it for strong anonymity. (Running on %s)",get_version(),
1814 get_uname());
1816 if (network_init()<0) {
1817 log_err(LD_BUG,"Error initializing network; exiting.");
1818 return -1;
1820 atexit(exit_function);
1822 if (options_init_from_torrc(argc,argv) < 0) {
1823 log_err(LD_CONFIG,"Reading config failed--see warnings above.");
1824 return -1;
1827 #ifndef MS_WINDOWS
1828 if (geteuid()==0)
1829 log_warn(LD_GENERAL,"You are running Tor as root. You don't need to, "
1830 "and you probably shouldn't.");
1831 #endif
1833 if (crypto_global_init(get_options()->HardwareAccel)) {
1834 log_err(LD_BUG, "Unable to initialize OpenSSL. Exiting.");
1835 return -1;
1838 return 0;
1841 /** A lockfile structure, used to prevent two Tors from messing with the
1842 * data directory at once. If this variable is non-NULL, we're holding
1843 * the lockfile. */
1844 static tor_lockfile_t *lockfile = NULL;
1846 /** Try to grab the lock file described in <b>options</b>, if we do not
1847 * already have it. If <b>err_if_locked</b> is true, warn if somebody else is
1848 * holding the lock, and exit if we can't get it after waiting. Otherwise,
1849 * return -1 if we can't get the lockfile. Return 0 on success.
1852 try_locking(or_options_t *options, int err_if_locked)
1854 if (lockfile)
1855 return 0;
1856 else {
1857 char *fname = options_get_datadir_fname2_suffix(options, "lock",NULL,NULL);
1858 int already_locked = 0;
1859 tor_lockfile_t *lf = tor_lockfile_lock(fname, 0, &already_locked);
1860 tor_free(fname);
1861 if (!lf) {
1862 if (err_if_locked && already_locked) {
1863 int r;
1864 log_warn(LD_GENERAL, "It looks like another Tor process is running "
1865 "with the same data directory. Waiting 5 seconds to see "
1866 "if it goes away.");
1867 #ifndef WIN32
1868 sleep(5);
1869 #else
1870 Sleep(5000);
1871 #endif
1872 r = try_locking(options, 0);
1873 if (r<0) {
1874 log_err(LD_GENERAL, "No, it's still there. Exiting.");
1875 exit(0);
1877 return r;
1879 return -1;
1881 lockfile = lf;
1882 return 0;
1886 /** Return true iff we've successfully acquired the lock file. */
1888 have_lockfile(void)
1890 return lockfile != NULL;
1893 /** If we have successfully acquired the lock file, release it. */
1894 void
1895 release_lockfile(void)
1897 if (lockfile) {
1898 tor_lockfile_unlock(lockfile);
1899 lockfile = NULL;
1903 /** Free all memory that we might have allocated somewhere.
1904 * If <b>postfork</b>, we are a worker process and we want to free
1905 * only the parts of memory that we won't touch. If !<b>postfork</b>,
1906 * Tor is shutting down and we should free everything.
1908 * Helps us find the real leaks with dmalloc and the like. Also valgrind
1909 * should then report 0 reachable in its leak report (in an ideal world --
1910 * in practice libevent, SSL, libc etc never quite free everything). */
1911 void
1912 tor_free_all(int postfork)
1914 if (!postfork) {
1915 evdns_shutdown(1);
1917 geoip_free_all();
1918 dirvote_free_all();
1919 routerlist_free_all();
1920 networkstatus_free_all();
1921 addressmap_free_all();
1922 dirserv_free_all();
1923 rend_service_free_all();
1924 rend_cache_free_all();
1925 rend_service_authorization_free_all();
1926 rep_hist_free_all();
1927 hs_usage_free_all();
1928 dns_free_all();
1929 clear_pending_onions();
1930 circuit_free_all();
1931 entry_guards_free_all();
1932 connection_free_all();
1933 buf_shrink_freelists(1);
1934 memarea_clear_freelist();
1935 if (!postfork) {
1936 config_free_all();
1937 router_free_all();
1938 policies_free_all();
1940 free_cell_pool();
1941 if (!postfork) {
1942 tor_tls_free_all();
1944 /* stuff in main.c */
1945 if (connection_array)
1946 smartlist_free(connection_array);
1947 if (closeable_connection_lst)
1948 smartlist_free(closeable_connection_lst);
1949 if (active_linked_connection_lst)
1950 smartlist_free(active_linked_connection_lst);
1951 tor_free(timeout_event);
1952 if (!postfork) {
1953 release_lockfile();
1955 /* Stuff in util.c and address.c*/
1956 if (!postfork) {
1957 escaped(NULL);
1958 esc_router_info(NULL);
1959 logs_free_all(); /* free log strings. do this last so logs keep working. */
1963 /** Do whatever cleanup is necessary before shutting Tor down. */
1964 void
1965 tor_cleanup(void)
1967 or_options_t *options = get_options();
1968 /* Remove our pid file. We don't care if there was an error when we
1969 * unlink, nothing we could do about it anyways. */
1970 if (options->command == CMD_RUN_TOR) {
1971 time_t now = time(NULL);
1972 if (options->PidFile)
1973 unlink(options->PidFile);
1974 if (accounting_is_enabled(options))
1975 accounting_record_bandwidth_usage(now, get_or_state());
1976 or_state_mark_dirty(get_or_state(), 0); /* force an immediate save. */
1977 or_state_save(now);
1978 if (authdir_mode_tests_reachability(options))
1979 rep_hist_record_mtbf_data(now, 0);
1981 #ifdef USE_DMALLOC
1982 dmalloc_log_stats();
1983 #endif
1984 tor_free_all(0); /* We could move tor_free_all back into the ifdef below
1985 later, if it makes shutdown unacceptably slow. But for
1986 now, leave it here: it's helped us catch bugs in the
1987 past. */
1988 crypto_global_cleanup();
1989 #ifdef USE_DMALLOC
1990 dmalloc_log_unfreed();
1991 dmalloc_shutdown();
1992 #endif
1995 /** Read/create keys as needed, and echo our fingerprint to stdout. */
1996 /* static */ int
1997 do_list_fingerprint(void)
1999 char buf[FINGERPRINT_LEN+1];
2000 crypto_pk_env_t *k;
2001 const char *nickname = get_options()->Nickname;
2002 if (!server_mode(get_options())) {
2003 log_err(LD_GENERAL,
2004 "Clients don't have long-term identity keys. Exiting.\n");
2005 return -1;
2007 tor_assert(nickname);
2008 if (init_keys() < 0) {
2009 log_err(LD_BUG,"Error initializing keys; can't display fingerprint");
2010 return -1;
2012 if (!(k = get_identity_key())) {
2013 log_err(LD_GENERAL,"Error: missing identity key.");
2014 return -1;
2016 if (crypto_pk_get_fingerprint(k, buf, 1)<0) {
2017 log_err(LD_BUG, "Error computing fingerprint");
2018 return -1;
2020 printf("%s %s\n", nickname, buf);
2021 return 0;
2024 /** Entry point for password hashing: take the desired password from
2025 * the command line, and print its salted hash to stdout. **/
2026 /* static */ void
2027 do_hash_password(void)
2030 char output[256];
2031 char key[S2K_SPECIFIER_LEN+DIGEST_LEN];
2033 crypto_rand(key, S2K_SPECIFIER_LEN-1);
2034 key[S2K_SPECIFIER_LEN-1] = (uint8_t)96; /* Hash 64 K of data. */
2035 secret_to_key(key+S2K_SPECIFIER_LEN, DIGEST_LEN,
2036 get_options()->command_arg, strlen(get_options()->command_arg),
2037 key);
2038 base16_encode(output, sizeof(output), key, sizeof(key));
2039 printf("16:%s\n",output);
2042 /** Main entry point for the Tor process. Called from main(). */
2043 /* This function is distinct from main() only so we can link main.c into
2044 * the unittest binary without conflicting with the unittests' main. */
2046 tor_main(int argc, char *argv[])
2048 int result = 0;
2049 update_approx_time(time(NULL));
2050 tor_threads_init();
2051 init_logging();
2052 #ifdef USE_DMALLOC
2054 /* Instruct OpenSSL to use our internal wrappers for malloc,
2055 realloc and free. */
2056 int r = CRYPTO_set_mem_ex_functions(_tor_malloc, _tor_realloc, _tor_free);
2057 tor_assert(r);
2059 #endif
2060 #ifdef NT_SERVICE
2062 int done = 0;
2063 result = nt_service_parse_options(argc, argv, &done);
2064 if (done) return result;
2066 #endif
2067 if (tor_init(argc, argv)<0)
2068 return -1;
2069 switch (get_options()->command) {
2070 case CMD_RUN_TOR:
2071 #ifdef NT_SERVICE
2072 nt_service_set_state(SERVICE_RUNNING);
2073 #endif
2074 result = do_main_loop();
2075 break;
2076 case CMD_LIST_FINGERPRINT:
2077 result = do_list_fingerprint();
2078 break;
2079 case CMD_HASH_PASSWORD:
2080 do_hash_password();
2081 result = 0;
2082 break;
2083 case CMD_VERIFY_CONFIG:
2084 printf("Configuration was valid\n");
2085 result = 0;
2086 break;
2087 case CMD_RUN_UNITTESTS: /* only set by test.c */
2088 default:
2089 log_warn(LD_BUG,"Illegal command number %d: internal error.",
2090 get_options()->command);
2091 result = -1;
2093 tor_cleanup();
2094 return result;