Fix a couple of smaller issues with gathering statistics.
[tor/rransom.git] / src / or / main.c
blob25182919ae73216e0126cc908e90f3fbcdb96ed4
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-2009, 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 #ifdef HAVE_EVENT2_EVENT_H
22 #include <event2/event.h>
23 #else
24 #include <event.h>
25 #endif
27 void evdns_shutdown(int);
29 /********* PROTOTYPES **********/
31 static void dumpmemusage(int severity);
32 static void dumpstats(int severity); /* log stats */
33 static void conn_read_callback(int fd, short event, void *_conn);
34 static void conn_write_callback(int fd, short event, void *_conn);
35 static void signal_callback(int fd, short events, void *arg);
36 static void second_elapsed_callback(int fd, short event, void *args);
37 static int conn_close_if_marked(int i);
38 static void connection_start_reading_from_linked_conn(connection_t *conn);
39 static int connection_should_read_from_linked_conn(connection_t *conn);
41 /********* START VARIABLES **********/
43 int global_read_bucket; /**< Max number of bytes I can read this second. */
44 int global_write_bucket; /**< Max number of bytes I can write this second. */
46 /** Max number of relayed (bandwidth class 1) bytes I can read this second. */
47 int global_relayed_read_bucket;
48 /** Max number of relayed (bandwidth class 1) bytes I can write this second. */
49 int global_relayed_write_bucket;
51 /** What was the read bucket before the last second_elapsed_callback() call?
52 * (used to determine how many bytes we've read). */
53 static int stats_prev_global_read_bucket;
54 /** What was the write bucket before the last second_elapsed_callback() call?
55 * (used to determine how many bytes we've written). */
56 static int stats_prev_global_write_bucket;
57 /* XXX we might want to keep stats about global_relayed_*_bucket too. Or not.*/
58 /** How many bytes have we read since we started the process? */
59 static uint64_t stats_n_bytes_read = 0;
60 /** How many bytes have we written since we started the process? */
61 static uint64_t stats_n_bytes_written = 0;
62 /** What time did this process start up? */
63 time_t time_of_process_start = 0;
64 /** How many seconds have we been running? */
65 long stats_n_seconds_working = 0;
66 /** When do we next launch DNS wildcarding checks? */
67 static time_t time_to_check_for_correct_dns = 0;
69 /** How often will we honor SIGNEWNYM requests? */
70 #define MAX_SIGNEWNYM_RATE 10
71 /** When did we last process a SIGNEWNYM request? */
72 static time_t time_of_last_signewnym = 0;
73 /** Is there a signewnym request we're currently waiting to handle? */
74 static int signewnym_is_pending = 0;
76 /** Smartlist of all open connections. */
77 static smartlist_t *connection_array = NULL;
78 /** List of connections that have been marked for close and need to be freed
79 * and removed from connection_array. */
80 static smartlist_t *closeable_connection_lst = NULL;
81 /** List of linked connections that are currently reading data into their
82 * inbuf from their partner's outbuf. */
83 static smartlist_t *active_linked_connection_lst = NULL;
84 /** Flag: Set to true iff we entered the current libevent main loop via
85 * <b>loop_once</b>. If so, there's no need to trigger a loopexit in order
86 * to handle linked connections. */
87 static int called_loop_once = 0;
89 /** We set this to 1 when we've opened a circuit, so we can print a log
90 * entry to inform the user that Tor is working. */
91 int has_completed_circuit=0;
93 /** How often do we check for router descriptors that we should download
94 * when we have too little directory info? */
95 #define GREEDY_DESCRIPTOR_RETRY_INTERVAL (10)
96 /** How often do we check for router descriptors that we should download
97 * when we have enough directory info? */
98 #define LAZY_DESCRIPTOR_RETRY_INTERVAL (60)
99 /** How often do we 'forgive' undownloadable router descriptors and attempt
100 * to download them again? */
101 #define DESCRIPTOR_FAILURE_RESET_INTERVAL (60*60)
102 /** How long do we let a directory connection stall before expiring it? */
103 #define DIR_CONN_MAX_STALL (5*60)
105 /** How long do we let OR connections handshake before we decide that
106 * they are obsolete? */
107 #define TLS_HANDSHAKE_TIMEOUT (60)
109 /********* END VARIABLES ************/
111 /****************************************************************************
113 * This section contains accessors and other methods on the connection_array
114 * variables (which are global within this file and unavailable outside it).
116 ****************************************************************************/
118 /** Add <b>conn</b> to the array of connections that we can poll on. The
119 * connection's socket must be set; the connection starts out
120 * non-reading and non-writing.
123 connection_add(connection_t *conn)
125 tor_assert(conn);
126 tor_assert(conn->s >= 0 ||
127 conn->linked ||
128 (conn->type == CONN_TYPE_AP &&
129 TO_EDGE_CONN(conn)->is_dns_request));
131 tor_assert(conn->conn_array_index == -1); /* can only connection_add once */
132 conn->conn_array_index = smartlist_len(connection_array);
133 smartlist_add(connection_array, conn);
135 if (conn->s >= 0 || conn->linked) {
136 conn->read_event = tor_event_new(tor_libevent_get_base(),
137 conn->s, EV_READ|EV_PERSIST, conn_read_callback, conn);
138 conn->write_event = tor_event_new(tor_libevent_get_base(),
139 conn->s, EV_WRITE|EV_PERSIST, conn_write_callback, conn);
142 log_debug(LD_NET,"new conn type %s, socket %d, address %s, n_conns %d.",
143 conn_type_to_string(conn->type), conn->s, conn->address,
144 smartlist_len(connection_array));
146 return 0;
149 /** Tell libevent that we don't care about <b>conn</b> any more. */
150 void
151 connection_unregister_events(connection_t *conn)
153 if (conn->read_event) {
154 if (event_del(conn->read_event))
155 log_warn(LD_BUG, "Error removing read event for %d", conn->s);
156 tor_free(conn->read_event);
158 if (conn->write_event) {
159 if (event_del(conn->write_event))
160 log_warn(LD_BUG, "Error removing write event for %d", conn->s);
161 tor_free(conn->write_event);
163 if (conn->dns_server_port) {
164 dnsserv_close_listener(conn);
168 /** Remove the connection from the global list, and remove the
169 * corresponding poll entry. Calling this function will shift the last
170 * connection (if any) into the position occupied by conn.
173 connection_remove(connection_t *conn)
175 int current_index;
176 connection_t *tmp;
178 tor_assert(conn);
180 log_debug(LD_NET,"removing socket %d (type %s), n_conns now %d",
181 conn->s, conn_type_to_string(conn->type),
182 smartlist_len(connection_array));
184 tor_assert(conn->conn_array_index >= 0);
185 current_index = conn->conn_array_index;
186 connection_unregister_events(conn); /* This is redundant, but cheap. */
187 if (current_index == smartlist_len(connection_array)-1) { /* at the end */
188 smartlist_del(connection_array, current_index);
189 return 0;
192 /* replace this one with the one at the end */
193 smartlist_del(connection_array, current_index);
194 tmp = smartlist_get(connection_array, current_index);
195 tmp->conn_array_index = current_index;
197 return 0;
200 /** If <b>conn</b> is an edge conn, remove it from the list
201 * of conn's on this circuit. If it's not on an edge,
202 * flush and send destroys for all circuits on this conn.
204 * Remove it from connection_array (if applicable) and
205 * from closeable_connection_list.
207 * Then free it.
209 static void
210 connection_unlink(connection_t *conn)
212 connection_about_to_close_connection(conn);
213 if (conn->conn_array_index >= 0) {
214 connection_remove(conn);
216 if (conn->linked_conn) {
217 conn->linked_conn->linked_conn = NULL;
218 if (! conn->linked_conn->marked_for_close &&
219 conn->linked_conn->reading_from_linked_conn)
220 connection_start_reading(conn->linked_conn);
221 conn->linked_conn = NULL;
223 smartlist_remove(closeable_connection_lst, conn);
224 smartlist_remove(active_linked_connection_lst, conn);
225 if (conn->type == CONN_TYPE_EXIT) {
226 assert_connection_edge_not_dns_pending(TO_EDGE_CONN(conn));
228 if (conn->type == CONN_TYPE_OR) {
229 if (!tor_digest_is_zero(TO_OR_CONN(conn)->identity_digest))
230 connection_or_remove_from_identity_map(TO_OR_CONN(conn));
232 connection_free(conn);
235 /** Schedule <b>conn</b> to be closed. **/
236 void
237 add_connection_to_closeable_list(connection_t *conn)
239 tor_assert(!smartlist_isin(closeable_connection_lst, conn));
240 tor_assert(conn->marked_for_close);
241 assert_connection_ok(conn, time(NULL));
242 smartlist_add(closeable_connection_lst, conn);
245 /** Return 1 if conn is on the closeable list, else return 0. */
247 connection_is_on_closeable_list(connection_t *conn)
249 return smartlist_isin(closeable_connection_lst, conn);
252 /** Return true iff conn is in the current poll array. */
254 connection_in_array(connection_t *conn)
256 return smartlist_isin(connection_array, conn);
259 /** Set <b>*array</b> to an array of all connections, and <b>*n</b>
260 * to the length of the array. <b>*array</b> and <b>*n</b> must not
261 * be modified.
263 smartlist_t *
264 get_connection_array(void)
266 if (!connection_array)
267 connection_array = smartlist_create();
268 return connection_array;
271 /** Set the event mask on <b>conn</b> to <b>events</b>. (The event
272 * mask is a bitmask whose bits are READ_EVENT and WRITE_EVENT)
274 void
275 connection_watch_events(connection_t *conn, watchable_events_t events)
277 if (events & READ_EVENT)
278 connection_start_reading(conn);
279 else
280 connection_stop_reading(conn);
282 if (events & WRITE_EVENT)
283 connection_start_writing(conn);
284 else
285 connection_stop_writing(conn);
288 /** Return true iff <b>conn</b> is listening for read events. */
290 connection_is_reading(connection_t *conn)
292 tor_assert(conn);
294 return conn->reading_from_linked_conn ||
295 (conn->read_event && event_pending(conn->read_event, EV_READ, NULL));
298 /** Tell the main loop to stop notifying <b>conn</b> of any read events. */
299 void
300 connection_stop_reading(connection_t *conn)
302 tor_assert(conn);
303 tor_assert(conn->read_event);
305 if (conn->linked) {
306 conn->reading_from_linked_conn = 0;
307 connection_stop_reading_from_linked_conn(conn);
308 } else {
309 if (event_del(conn->read_event))
310 log_warn(LD_NET, "Error from libevent setting read event state for %d "
311 "to unwatched: %s",
312 conn->s,
313 tor_socket_strerror(tor_socket_errno(conn->s)));
317 /** Tell the main loop to start notifying <b>conn</b> of any read events. */
318 void
319 connection_start_reading(connection_t *conn)
321 tor_assert(conn);
322 tor_assert(conn->read_event);
324 if (conn->linked) {
325 conn->reading_from_linked_conn = 1;
326 if (connection_should_read_from_linked_conn(conn))
327 connection_start_reading_from_linked_conn(conn);
328 } else {
329 if (event_add(conn->read_event, NULL))
330 log_warn(LD_NET, "Error from libevent setting read event state for %d "
331 "to watched: %s",
332 conn->s,
333 tor_socket_strerror(tor_socket_errno(conn->s)));
337 /** Return true iff <b>conn</b> is listening for write events. */
339 connection_is_writing(connection_t *conn)
341 tor_assert(conn);
343 return conn->writing_to_linked_conn ||
344 (conn->write_event && event_pending(conn->write_event, EV_WRITE, NULL));
347 /** Tell the main loop to stop notifying <b>conn</b> of any write events. */
348 void
349 connection_stop_writing(connection_t *conn)
351 tor_assert(conn);
352 tor_assert(conn->write_event);
354 if (conn->linked) {
355 conn->writing_to_linked_conn = 0;
356 if (conn->linked_conn)
357 connection_stop_reading_from_linked_conn(conn->linked_conn);
358 } else {
359 if (event_del(conn->write_event))
360 log_warn(LD_NET, "Error from libevent setting write event state for %d "
361 "to unwatched: %s",
362 conn->s,
363 tor_socket_strerror(tor_socket_errno(conn->s)));
367 /** Tell the main loop to start notifying <b>conn</b> of any write events. */
368 void
369 connection_start_writing(connection_t *conn)
371 tor_assert(conn);
372 tor_assert(conn->write_event);
374 if (conn->linked) {
375 conn->writing_to_linked_conn = 1;
376 if (conn->linked_conn &&
377 connection_should_read_from_linked_conn(conn->linked_conn))
378 connection_start_reading_from_linked_conn(conn->linked_conn);
379 } else {
380 if (event_add(conn->write_event, NULL))
381 log_warn(LD_NET, "Error from libevent setting write event state for %d "
382 "to watched: %s",
383 conn->s,
384 tor_socket_strerror(tor_socket_errno(conn->s)));
388 /** Return true iff <b>conn</b> is linked conn, and reading from the conn
389 * linked to it would be good and feasible. (Reading is "feasible" if the
390 * other conn exists and has data in its outbuf, and is "good" if we have our
391 * reading_from_linked_conn flag set and the other conn has its
392 * writing_to_linked_conn flag set.)*/
393 static int
394 connection_should_read_from_linked_conn(connection_t *conn)
396 if (conn->linked && conn->reading_from_linked_conn) {
397 if (! conn->linked_conn ||
398 (conn->linked_conn->writing_to_linked_conn &&
399 buf_datalen(conn->linked_conn->outbuf)))
400 return 1;
402 return 0;
405 /** Helper: Tell the main loop to begin reading bytes into <b>conn</b> from
406 * its linked connection, if it is not doing so already. Called by
407 * connection_start_reading and connection_start_writing as appropriate. */
408 static void
409 connection_start_reading_from_linked_conn(connection_t *conn)
411 tor_assert(conn);
412 tor_assert(conn->linked == 1);
414 if (!conn->active_on_link) {
415 conn->active_on_link = 1;
416 smartlist_add(active_linked_connection_lst, conn);
417 if (!called_loop_once) {
418 /* This is the first event on the list; we won't be in LOOP_ONCE mode,
419 * so we need to make sure that the event_base_loop() actually exits at
420 * the end of its run through the current connections and lets us
421 * activate read events for linked connections. */
422 struct timeval tv = { 0, 0 };
423 tor_event_base_loopexit(tor_libevent_get_base(), &tv);
425 } else {
426 tor_assert(smartlist_isin(active_linked_connection_lst, conn));
430 /** Tell the main loop to stop reading bytes into <b>conn</b> from its linked
431 * connection, if is currently doing so. Called by connection_stop_reading,
432 * connection_stop_writing, and connection_read. */
433 void
434 connection_stop_reading_from_linked_conn(connection_t *conn)
436 tor_assert(conn);
437 tor_assert(conn->linked == 1);
439 if (conn->active_on_link) {
440 conn->active_on_link = 0;
441 /* FFFF We could keep an index here so we can smartlist_del
442 * cleanly. On the other hand, this doesn't show up on profiles,
443 * so let's leave it alone for now. */
444 smartlist_remove(active_linked_connection_lst, conn);
445 } else {
446 tor_assert(!smartlist_isin(active_linked_connection_lst, conn));
450 /** Close all connections that have been scheduled to get closed. */
451 static void
452 close_closeable_connections(void)
454 int i;
455 for (i = 0; i < smartlist_len(closeable_connection_lst); ) {
456 connection_t *conn = smartlist_get(closeable_connection_lst, i);
457 if (conn->conn_array_index < 0) {
458 connection_unlink(conn); /* blow it away right now */
459 } else {
460 if (!conn_close_if_marked(conn->conn_array_index))
461 ++i;
466 /** Libevent callback: this gets invoked when (connection_t*)<b>conn</b> has
467 * some data to read. */
468 static void
469 conn_read_callback(int fd, short event, void *_conn)
471 connection_t *conn = _conn;
472 (void)fd;
473 (void)event;
475 log_debug(LD_NET,"socket %d wants to read.",conn->s);
477 /* assert_connection_ok(conn, time(NULL)); */
479 if (connection_handle_read(conn) < 0) {
480 if (!conn->marked_for_close) {
481 #ifndef MS_WINDOWS
482 log_warn(LD_BUG,"Unhandled error on read for %s connection "
483 "(fd %d); removing",
484 conn_type_to_string(conn->type), conn->s);
485 tor_fragile_assert();
486 #endif
487 if (CONN_IS_EDGE(conn))
488 connection_edge_end_errno(TO_EDGE_CONN(conn));
489 connection_mark_for_close(conn);
492 assert_connection_ok(conn, time(NULL));
494 if (smartlist_len(closeable_connection_lst))
495 close_closeable_connections();
498 /** Libevent callback: this gets invoked when (connection_t*)<b>conn</b> has
499 * some data to write. */
500 static void
501 conn_write_callback(int fd, short events, void *_conn)
503 connection_t *conn = _conn;
504 (void)fd;
505 (void)events;
507 LOG_FN_CONN(conn, (LOG_DEBUG, LD_NET, "socket %d wants to write.",conn->s));
509 /* assert_connection_ok(conn, time(NULL)); */
511 if (connection_handle_write(conn, 0) < 0) {
512 if (!conn->marked_for_close) {
513 /* this connection is broken. remove it. */
514 log_fn(LOG_WARN,LD_BUG,
515 "unhandled error on write for %s connection (fd %d); removing",
516 conn_type_to_string(conn->type), conn->s);
517 tor_fragile_assert();
518 if (CONN_IS_EDGE(conn)) {
519 /* otherwise we cry wolf about duplicate close */
520 edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
521 if (!edge_conn->end_reason)
522 edge_conn->end_reason = END_STREAM_REASON_INTERNAL;
523 edge_conn->edge_has_sent_end = 1;
525 connection_close_immediate(conn); /* So we don't try to flush. */
526 connection_mark_for_close(conn);
529 assert_connection_ok(conn, time(NULL));
531 if (smartlist_len(closeable_connection_lst))
532 close_closeable_connections();
535 /** If the connection at connection_array[i] is marked for close, then:
536 * - If it has data that it wants to flush, try to flush it.
537 * - If it _still_ has data to flush, and conn->hold_open_until_flushed is
538 * true, then leave the connection open and return.
539 * - Otherwise, remove the connection from connection_array and from
540 * all other lists, close it, and free it.
541 * Returns 1 if the connection was closed, 0 otherwise.
543 static int
544 conn_close_if_marked(int i)
546 connection_t *conn;
547 int retval;
548 time_t now;
550 conn = smartlist_get(connection_array, i);
551 if (!conn->marked_for_close)
552 return 0; /* nothing to see here, move along */
553 now = time(NULL);
554 assert_connection_ok(conn, now);
555 /* assert_all_pending_dns_resolves_ok(); */
557 log_debug(LD_NET,"Cleaning up connection (fd %d).",conn->s);
558 if ((conn->s >= 0 || conn->linked_conn) && connection_wants_to_flush(conn)) {
559 /* s == -1 means it's an incomplete edge connection, or that the socket
560 * has already been closed as unflushable. */
561 ssize_t sz = connection_bucket_write_limit(conn, now);
562 if (!conn->hold_open_until_flushed)
563 log_info(LD_NET,
564 "Conn (addr %s, fd %d, type %s, state %d) marked, but wants "
565 "to flush %d bytes. (Marked at %s:%d)",
566 escaped_safe_str(conn->address),
567 conn->s, conn_type_to_string(conn->type), conn->state,
568 (int)conn->outbuf_flushlen,
569 conn->marked_for_close_file, conn->marked_for_close);
570 if (conn->linked_conn) {
571 retval = move_buf_to_buf(conn->linked_conn->inbuf, conn->outbuf,
572 &conn->outbuf_flushlen);
573 if (retval >= 0) {
574 /* The linked conn will notice that it has data when it notices that
575 * we're gone. */
576 connection_start_reading_from_linked_conn(conn->linked_conn);
578 log_debug(LD_GENERAL, "Flushed last %d bytes from a linked conn; "
579 "%d left; flushlen %d; wants-to-flush==%d", retval,
580 (int)buf_datalen(conn->outbuf),
581 (int)conn->outbuf_flushlen,
582 connection_wants_to_flush(conn));
583 } else if (connection_speaks_cells(conn)) {
584 if (conn->state == OR_CONN_STATE_OPEN) {
585 retval = flush_buf_tls(TO_OR_CONN(conn)->tls, conn->outbuf, sz,
586 &conn->outbuf_flushlen);
587 } else
588 retval = -1; /* never flush non-open broken tls connections */
589 } else {
590 retval = flush_buf(conn->s, conn->outbuf, sz, &conn->outbuf_flushlen);
592 if (retval >= 0 && /* Technically, we could survive things like
593 TLS_WANT_WRITE here. But don't bother for now. */
594 conn->hold_open_until_flushed && connection_wants_to_flush(conn)) {
595 if (retval > 0) {
596 LOG_FN_CONN(conn, (LOG_INFO,LD_NET,
597 "Holding conn (fd %d) open for more flushing.",
598 conn->s));
599 conn->timestamp_lastwritten = now; /* reset so we can flush more */
601 return 0;
603 if (connection_wants_to_flush(conn)) {
604 int severity;
605 if (conn->type == CONN_TYPE_EXIT ||
606 (conn->type == CONN_TYPE_OR && server_mode(get_options())) ||
607 (conn->type == CONN_TYPE_DIR && conn->purpose == DIR_PURPOSE_SERVER))
608 severity = LOG_INFO;
609 else
610 severity = LOG_NOTICE;
611 /* XXXX Maybe allow this to happen a certain amount per hour; it usually
612 * is meaningless. */
613 log_fn(severity, LD_NET, "We stalled too much while trying to write %d "
614 "bytes to address %s. If this happens a lot, either "
615 "something is wrong with your network connection, or "
616 "something is wrong with theirs. "
617 "(fd %d, type %s, state %d, marked at %s:%d).",
618 (int)buf_datalen(conn->outbuf),
619 escaped_safe_str(conn->address), conn->s,
620 conn_type_to_string(conn->type), conn->state,
621 conn->marked_for_close_file,
622 conn->marked_for_close);
625 connection_unlink(conn); /* unlink, remove, free */
626 return 1;
629 /** We've just tried every dirserver we know about, and none of
630 * them were reachable. Assume the network is down. Change state
631 * so next time an application connection arrives we'll delay it
632 * and try another directory fetch. Kill off all the circuit_wait
633 * streams that are waiting now, since they will all timeout anyway.
635 void
636 directory_all_unreachable(time_t now)
638 connection_t *conn;
639 (void)now;
641 stats_n_seconds_working=0; /* reset it */
643 while ((conn = connection_get_by_type_state(CONN_TYPE_AP,
644 AP_CONN_STATE_CIRCUIT_WAIT))) {
645 edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
646 log_notice(LD_NET,
647 "Is your network connection down? "
648 "Failing connection to '%s:%d'.",
649 safe_str(edge_conn->socks_request->address),
650 edge_conn->socks_request->port);
651 connection_mark_unattached_ap(edge_conn,
652 END_STREAM_REASON_NET_UNREACHABLE);
654 control_event_general_status(LOG_ERR, "DIR_ALL_UNREACHABLE");
657 /** This function is called whenever we successfully pull down some new
658 * network statuses or server descriptors. */
659 void
660 directory_info_has_arrived(time_t now, int from_cache)
662 or_options_t *options = get_options();
664 if (!router_have_minimum_dir_info()) {
665 int quiet = directory_too_idle_to_fetch_descriptors(options, now);
666 log(quiet ? LOG_INFO : LOG_NOTICE, LD_DIR,
667 "I learned some more directory information, but not enough to "
668 "build a circuit: %s", get_dir_info_status_string());
669 update_router_descriptor_downloads(now);
670 return;
671 } else {
672 if (directory_fetches_from_authorities(options))
673 update_router_descriptor_downloads(now);
675 /* if we have enough dir info, then update our guard status with
676 * whatever we just learned. */
677 entry_guards_compute_status();
678 /* Don't even bother trying to get extrainfo until the rest of our
679 * directory info is up-to-date */
680 if (options->DownloadExtraInfo)
681 update_extrainfo_downloads(now);
684 if (server_mode(options) && !we_are_hibernating() && !from_cache &&
685 (has_completed_circuit || !any_predicted_circuits(now)))
686 consider_testing_reachability(1, 1);
689 /** Perform regular maintenance tasks for a single connection. This
690 * function gets run once per second per connection by run_scheduled_events.
692 static void
693 run_connection_housekeeping(int i, time_t now)
695 cell_t cell;
696 connection_t *conn = smartlist_get(connection_array, i);
697 or_options_t *options = get_options();
698 or_connection_t *or_conn;
700 if (conn->outbuf && !buf_datalen(conn->outbuf) && conn->type == CONN_TYPE_OR)
701 TO_OR_CONN(conn)->timestamp_lastempty = now;
703 if (conn->marked_for_close) {
704 /* nothing to do here */
705 return;
708 /* Expire any directory connections that haven't been active (sent
709 * if a server or received if a client) for 5 min */
710 if (conn->type == CONN_TYPE_DIR &&
711 ((DIR_CONN_IS_SERVER(conn) &&
712 conn->timestamp_lastwritten + DIR_CONN_MAX_STALL < now) ||
713 (!DIR_CONN_IS_SERVER(conn) &&
714 conn->timestamp_lastread + DIR_CONN_MAX_STALL < now))) {
715 log_info(LD_DIR,"Expiring wedged directory conn (fd %d, purpose %d)",
716 conn->s, conn->purpose);
717 /* This check is temporary; it's to let us know whether we should consider
718 * parsing partial serverdesc responses. */
719 if (conn->purpose == DIR_PURPOSE_FETCH_SERVERDESC &&
720 buf_datalen(conn->inbuf)>=1024) {
721 log_info(LD_DIR,"Trying to extract information from wedged server desc "
722 "download.");
723 connection_dir_reached_eof(TO_DIR_CONN(conn));
724 } else {
725 connection_mark_for_close(conn);
727 return;
730 if (!connection_speaks_cells(conn))
731 return; /* we're all done here, the rest is just for OR conns */
733 or_conn = TO_OR_CONN(conn);
735 if (or_conn->is_bad_for_new_circs && !or_conn->n_circuits) {
736 /* It's bad for new circuits, and has no unmarked circuits on it:
737 * mark it now. */
738 log_info(LD_OR,
739 "Expiring non-used OR connection to fd %d (%s:%d) [Too old].",
740 conn->s, conn->address, conn->port);
741 if (conn->state == OR_CONN_STATE_CONNECTING)
742 connection_or_connect_failed(TO_OR_CONN(conn),
743 END_OR_CONN_REASON_TIMEOUT,
744 "Tor gave up on the connection");
745 connection_mark_for_close(conn);
746 conn->hold_open_until_flushed = 1;
747 return;
750 /* If we haven't written to an OR connection for a while, then either nuke
751 the connection or send a keepalive, depending. */
752 if (now >= conn->timestamp_lastwritten + options->KeepalivePeriod) {
753 routerinfo_t *router = router_get_by_digest(or_conn->identity_digest);
754 int maxCircuitlessPeriod = options->MaxCircuitDirtiness*3/2;
755 if (!connection_state_is_open(conn)) {
756 /* We never managed to actually get this connection open and happy. */
757 log_info(LD_OR,"Expiring non-open OR connection to fd %d (%s:%d).",
758 conn->s,conn->address, conn->port);
759 connection_mark_for_close(conn);
760 conn->hold_open_until_flushed = 1;
761 } else if (we_are_hibernating() && !or_conn->n_circuits &&
762 !buf_datalen(conn->outbuf)) {
763 /* We're hibernating, there's no circuits, and nothing to flush.*/
764 log_info(LD_OR,"Expiring non-used OR connection to fd %d (%s:%d) "
765 "[Hibernating or exiting].",
766 conn->s,conn->address, conn->port);
767 connection_mark_for_close(conn);
768 conn->hold_open_until_flushed = 1;
769 } else if (!clique_mode(options) && !or_conn->n_circuits &&
770 now >= or_conn->timestamp_last_added_nonpadding +
771 maxCircuitlessPeriod &&
772 (!router || !server_mode(options) ||
773 !router_is_clique_mode(router))) {
774 log_info(LD_OR,"Expiring non-used OR connection to fd %d (%s:%d) "
775 "[Not in clique mode].",
776 conn->s,conn->address, conn->port);
777 connection_mark_for_close(conn);
778 conn->hold_open_until_flushed = 1;
779 } else if (
780 now >= or_conn->timestamp_lastempty + options->KeepalivePeriod*10 &&
781 now >= conn->timestamp_lastwritten + options->KeepalivePeriod*10) {
782 log_fn(LOG_PROTOCOL_WARN,LD_PROTOCOL,
783 "Expiring stuck OR connection to fd %d (%s:%d). (%d bytes to "
784 "flush; %d seconds since last write)",
785 conn->s, conn->address, conn->port,
786 (int)buf_datalen(conn->outbuf),
787 (int)(now-conn->timestamp_lastwritten));
788 connection_mark_for_close(conn);
789 } else if (!buf_datalen(conn->outbuf)) {
790 /* either in clique mode, or we've got a circuit. send a padding cell. */
791 log_fn(LOG_DEBUG,LD_OR,"Sending keepalive to (%s:%d)",
792 conn->address, conn->port);
793 memset(&cell,0,sizeof(cell_t));
794 cell.command = CELL_PADDING;
795 connection_or_write_cell_to_buf(&cell, or_conn);
800 /** Honor a NEWNYM request: make future requests unlinkable to past
801 * requests. */
802 static void
803 signewnym_impl(time_t now)
805 circuit_expire_all_dirty_circs();
806 addressmap_clear_transient();
807 time_of_last_signewnym = now;
808 signewnym_is_pending = 0;
811 /** Perform regular maintenance tasks. This function gets run once per
812 * second by second_elapsed_callback().
814 static void
815 run_scheduled_events(time_t now)
817 static time_t last_rotated_x509_certificate = 0;
818 static time_t time_to_check_v3_certificate = 0;
819 static time_t time_to_check_listeners = 0;
820 static time_t time_to_check_descriptor = 0;
821 static time_t time_to_check_ipaddress = 0;
822 static time_t time_to_shrink_memory = 0;
823 static time_t time_to_try_getting_descriptors = 0;
824 static time_t time_to_reset_descriptor_failures = 0;
825 static time_t time_to_add_entropy = 0;
826 static time_t time_to_write_hs_statistics = 0;
827 static time_t time_to_write_bridge_status_file = 0;
828 static time_t time_to_downrate_stability = 0;
829 static time_t time_to_save_stability = 0;
830 static time_t time_to_clean_caches = 0;
831 static time_t time_to_recheck_bandwidth = 0;
832 static time_t time_to_check_for_expired_networkstatus = 0;
833 static time_t time_to_write_stats_files = 0;
834 static time_t time_to_retry_dns_init = 0;
835 or_options_t *options = get_options();
836 int i;
837 int have_dir_info;
839 /** 0. See if we've been asked to shut down and our timeout has
840 * expired; or if our bandwidth limits are exhausted and we
841 * should hibernate; or if it's time to wake up from hibernation.
843 consider_hibernation(now);
845 /* 0b. If we've deferred a signewnym, make sure it gets handled
846 * eventually. */
847 if (signewnym_is_pending &&
848 time_of_last_signewnym + MAX_SIGNEWNYM_RATE <= now) {
849 log(LOG_INFO, LD_CONTROL, "Honoring delayed NEWNYM request");
850 signewnym_impl(now);
853 /** 1a. Every MIN_ONION_KEY_LIFETIME seconds, rotate the onion keys,
854 * shut down and restart all cpuworkers, and update the directory if
855 * necessary.
857 if (server_mode(options) &&
858 get_onion_key_set_at()+MIN_ONION_KEY_LIFETIME < now) {
859 log_info(LD_GENERAL,"Rotating onion key.");
860 rotate_onion_key();
861 cpuworkers_rotate();
862 if (router_rebuild_descriptor(1)<0) {
863 log_info(LD_CONFIG, "Couldn't rebuild router descriptor");
865 if (advertised_server_mode())
866 router_upload_dir_desc_to_dirservers(0);
869 if (time_to_try_getting_descriptors < now) {
870 update_router_descriptor_downloads(now);
871 update_extrainfo_downloads(now);
872 if (options->UseBridges)
873 fetch_bridge_descriptors(now);
874 if (router_have_minimum_dir_info())
875 time_to_try_getting_descriptors = now + LAZY_DESCRIPTOR_RETRY_INTERVAL;
876 else
877 time_to_try_getting_descriptors = now + GREEDY_DESCRIPTOR_RETRY_INTERVAL;
880 if (time_to_reset_descriptor_failures < now) {
881 router_reset_descriptor_download_failures();
882 time_to_reset_descriptor_failures =
883 now + DESCRIPTOR_FAILURE_RESET_INTERVAL;
886 /** 1b. Every MAX_SSL_KEY_LIFETIME seconds, we change our TLS context. */
887 if (!last_rotated_x509_certificate)
888 last_rotated_x509_certificate = now;
889 if (last_rotated_x509_certificate+MAX_SSL_KEY_LIFETIME < now) {
890 log_info(LD_GENERAL,"Rotating tls context.");
891 if (tor_tls_context_new(get_identity_key(), MAX_SSL_KEY_LIFETIME) < 0) {
892 log_warn(LD_BUG, "Error reinitializing TLS context");
893 /* XXX is it a bug here, that we just keep going? -RD */
895 last_rotated_x509_certificate = now;
896 /* We also make sure to rotate the TLS connections themselves if they've
897 * been up for too long -- but that's done via is_bad_for_new_circs in
898 * connection_run_housekeeping() above. */
901 if (time_to_add_entropy < now) {
902 if (time_to_add_entropy) {
903 /* We already seeded once, so don't die on failure. */
904 crypto_seed_rng(0);
906 /** How often do we add more entropy to OpenSSL's RNG pool? */
907 #define ENTROPY_INTERVAL (60*60)
908 time_to_add_entropy = now + ENTROPY_INTERVAL;
911 /** 1c. If we have to change the accounting interval or record
912 * bandwidth used in this accounting interval, do so. */
913 if (accounting_is_enabled(options))
914 accounting_run_housekeeping(now);
916 if (now % 10 == 0 && (authdir_mode_tests_reachability(options)) &&
917 !we_are_hibernating()) {
918 /* try to determine reachability of the other Tor relays */
919 dirserv_test_reachability(now, 0);
922 /** 1d. Periodically, we discount older stability information so that new
923 * stability info counts more, and save the stability information to disk as
924 * appropriate. */
925 if (time_to_downrate_stability < now)
926 time_to_downrate_stability = rep_hist_downrate_old_runs(now);
927 if (authdir_mode_tests_reachability(options)) {
928 if (time_to_save_stability < now) {
929 if (time_to_save_stability && rep_hist_record_mtbf_data(now, 1)<0) {
930 log_warn(LD_GENERAL, "Couldn't store mtbf data.");
932 #define SAVE_STABILITY_INTERVAL (30*60)
933 time_to_save_stability = now + SAVE_STABILITY_INTERVAL;
937 /* 1e. Periodically, if we're a v3 authority, we check whether our cert is
938 * close to expiring and warn the admin if it is. */
939 if (time_to_check_v3_certificate < now) {
940 v3_authority_check_key_expiry();
941 #define CHECK_V3_CERTIFICATE_INTERVAL (5*60)
942 time_to_check_v3_certificate = now + CHECK_V3_CERTIFICATE_INTERVAL;
945 /* 1f. Check whether our networkstatus has expired.
947 if (time_to_check_for_expired_networkstatus < now) {
948 networkstatus_t *ns = networkstatus_get_latest_consensus();
949 /*XXXX RD: This value needs to be the same as REASONABLY_LIVE_TIME in
950 * networkstatus_get_reasonably_live_consensus(), but that value is way
951 * way too high. Arma: is the bridge issue there resolved yet? -NM */
952 #define NS_EXPIRY_SLOP (24*60*60)
953 if (ns && ns->valid_until < now+NS_EXPIRY_SLOP &&
954 router_have_minimum_dir_info()) {
955 router_dir_info_changed();
957 #define CHECK_EXPIRED_NS_INTERVAL (2*60)
958 time_to_check_for_expired_networkstatus = now + CHECK_EXPIRED_NS_INTERVAL;
961 /* 1g. Check whether we should write statistics to disk.
963 if (time_to_write_stats_files >= 0 && time_to_write_stats_files < now) {
964 #define WRITE_STATS_INTERVAL (24*60*60)
965 if (options->CellStatistics || options->DirReqStatistics ||
966 options->EntryStatistics || options->ExitPortStatistics) {
967 if (!time_to_write_stats_files) {
968 /* Initialize stats. */
969 if (options->CellStatistics)
970 rep_hist_buffer_stats_init(now);
971 if (options->DirReqStatistics)
972 geoip_dirreq_stats_init(now);
973 if (options->EntryStatistics)
974 geoip_entry_stats_init(now);
975 if (options->ExitPortStatistics)
976 rep_hist_exit_stats_init(now);
977 log_notice(LD_CONFIG, "Configured to measure statistics. Look for "
978 "the *-stats files that will first be written to the "
979 "data directory in %d hours from now.",
980 WRITE_STATS_INTERVAL / (60 * 60));
981 time_to_write_stats_files = now + WRITE_STATS_INTERVAL;
982 } else {
983 /* Write stats to disk. */
984 if (options->CellStatistics)
985 rep_hist_buffer_stats_write(time_to_write_stats_files);
986 if (options->DirReqStatistics)
987 geoip_dirreq_stats_write(time_to_write_stats_files);
988 if (options->EntryStatistics)
989 geoip_entry_stats_write(time_to_write_stats_files);
990 if (options->ExitPortStatistics)
991 rep_hist_exit_stats_write(time_to_write_stats_files);
992 time_to_write_stats_files += WRITE_STATS_INTERVAL;
994 } else {
995 /* Never write stats to disk */
996 time_to_write_stats_files = -1;
1000 /* Remove old information from rephist and the rend cache. */
1001 if (time_to_clean_caches < now) {
1002 rep_history_clean(now - options->RephistTrackTime);
1003 rend_cache_clean();
1004 rend_cache_clean_v2_descs_as_dir();
1005 #define CLEAN_CACHES_INTERVAL (30*60)
1006 time_to_clean_caches = now + CLEAN_CACHES_INTERVAL;
1009 #define RETRY_DNS_INTERVAL (10*60)
1010 /* If we're a server and initializing dns failed, retry periodically. */
1011 if (time_to_retry_dns_init < now) {
1012 time_to_retry_dns_init = now + RETRY_DNS_INTERVAL;
1013 if (server_mode(options) && has_dns_init_failed())
1014 dns_init();
1017 /** 2. Periodically, we consider force-uploading our descriptor
1018 * (if we've passed our internal checks). */
1020 /** How often do we check whether part of our router info has changed in a way
1021 * that would require an upload? */
1022 #define CHECK_DESCRIPTOR_INTERVAL (60)
1023 /** How often do we (as a router) check whether our IP address has changed? */
1024 #define CHECK_IPADDRESS_INTERVAL (15*60)
1026 /* 2b. Once per minute, regenerate and upload the descriptor if the old
1027 * one is inaccurate. */
1028 if (time_to_check_descriptor < now) {
1029 static int dirport_reachability_count = 0;
1030 time_to_check_descriptor = now + CHECK_DESCRIPTOR_INTERVAL;
1031 check_descriptor_bandwidth_changed(now);
1032 if (time_to_check_ipaddress < now) {
1033 time_to_check_ipaddress = now + CHECK_IPADDRESS_INTERVAL;
1034 check_descriptor_ipaddress_changed(now);
1036 /** If our router descriptor ever goes this long without being regenerated
1037 * because something changed, we force an immediate regenerate-and-upload. */
1038 #define FORCE_REGENERATE_DESCRIPTOR_INTERVAL (18*60*60)
1039 mark_my_descriptor_dirty_if_older_than(
1040 now - FORCE_REGENERATE_DESCRIPTOR_INTERVAL);
1041 consider_publishable_server(0);
1042 /* also, check religiously for reachability, if it's within the first
1043 * 20 minutes of our uptime. */
1044 if (server_mode(options) &&
1045 (has_completed_circuit || !any_predicted_circuits(now)) &&
1046 !we_are_hibernating()) {
1047 if (stats_n_seconds_working < TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT) {
1048 consider_testing_reachability(1, dirport_reachability_count==0);
1049 if (++dirport_reachability_count > 5)
1050 dirport_reachability_count = 0;
1051 } else if (time_to_recheck_bandwidth < now) {
1052 /* If we haven't checked for 12 hours and our bandwidth estimate is
1053 * low, do another bandwidth test. This is especially important for
1054 * bridges, since they might go long periods without much use. */
1055 routerinfo_t *me = router_get_my_routerinfo();
1056 if (time_to_recheck_bandwidth && me &&
1057 me->bandwidthcapacity < me->bandwidthrate &&
1058 me->bandwidthcapacity < 51200) {
1059 reset_bandwidth_test();
1061 #define BANDWIDTH_RECHECK_INTERVAL (12*60*60)
1062 time_to_recheck_bandwidth = now + BANDWIDTH_RECHECK_INTERVAL;
1066 /* If any networkstatus documents are no longer recent, we need to
1067 * update all the descriptors' running status. */
1068 /* purge obsolete entries */
1069 networkstatus_v2_list_clean(now);
1070 /* Remove dead routers. */
1071 routerlist_remove_old_routers();
1073 /* Also, once per minute, check whether we want to download any
1074 * networkstatus documents.
1076 update_networkstatus_downloads(now);
1079 /** 2c. Let directory voting happen. */
1080 if (authdir_mode_v3(options))
1081 dirvote_act(options, now);
1083 /** 3a. Every second, we examine pending circuits and prune the
1084 * ones which have been pending for more than a few seconds.
1085 * We do this before step 4, so it can try building more if
1086 * it's not comfortable with the number of available circuits.
1088 circuit_expire_building(now);
1090 /** 3b. Also look at pending streams and prune the ones that 'began'
1091 * a long time ago but haven't gotten a 'connected' yet.
1092 * Do this before step 4, so we can put them back into pending
1093 * state to be picked up by the new circuit.
1095 connection_ap_expire_beginning();
1097 /** 3c. And expire connections that we've held open for too long.
1099 connection_expire_held_open();
1101 /** 3d. And every 60 seconds, we relaunch listeners if any died. */
1102 if (!we_are_hibernating() && time_to_check_listeners < now) {
1103 retry_all_listeners(NULL, NULL);
1104 time_to_check_listeners = now+60;
1107 /** 4. Every second, we try a new circuit if there are no valid
1108 * circuits. Every NewCircuitPeriod seconds, we expire circuits
1109 * that became dirty more than MaxCircuitDirtiness seconds ago,
1110 * and we make a new circ if there are no clean circuits.
1112 have_dir_info = router_have_minimum_dir_info();
1113 if (have_dir_info && !we_are_hibernating())
1114 circuit_build_needed_circs(now);
1116 /** 5. We do housekeeping for each connection... */
1117 connection_or_set_bad_connections();
1118 for (i=0;i<smartlist_len(connection_array);i++) {
1119 run_connection_housekeeping(i, now);
1121 if (time_to_shrink_memory < now) {
1122 SMARTLIST_FOREACH(connection_array, connection_t *, conn, {
1123 if (conn->outbuf)
1124 buf_shrink(conn->outbuf);
1125 if (conn->inbuf)
1126 buf_shrink(conn->inbuf);
1128 clean_cell_pool();
1129 buf_shrink_freelists(0);
1130 /** How often do we check buffers and pools for empty space that can be
1131 * deallocated? */
1132 #define MEM_SHRINK_INTERVAL (60)
1133 time_to_shrink_memory = now + MEM_SHRINK_INTERVAL;
1136 /** 6. And remove any marked circuits... */
1137 circuit_close_all_marked();
1139 /** 7. And upload service descriptors if necessary. */
1140 if (has_completed_circuit && !we_are_hibernating()) {
1141 rend_consider_services_upload(now);
1142 rend_consider_descriptor_republication();
1145 /** 8. and blow away any connections that need to die. have to do this now,
1146 * because if we marked a conn for close and left its socket -1, then
1147 * we'll pass it to poll/select and bad things will happen.
1149 close_closeable_connections();
1151 /** 8b. And if anything in our state is ready to get flushed to disk, we
1152 * flush it. */
1153 or_state_save(now);
1155 /** 9. and if we're a server, check whether our DNS is telling stories to
1156 * us. */
1157 if (server_mode(options) && time_to_check_for_correct_dns < now) {
1158 if (!time_to_check_for_correct_dns) {
1159 time_to_check_for_correct_dns = now + 60 + crypto_rand_int(120);
1160 } else {
1161 dns_launch_correctness_checks();
1162 time_to_check_for_correct_dns = now + 12*3600 +
1163 crypto_rand_int(12*3600);
1167 /** 10. write hidden service usage statistic to disk */
1168 if (options->HSAuthorityRecordStats && time_to_write_hs_statistics < now) {
1169 hs_usage_write_statistics_to_file(now);
1170 #define WRITE_HSUSAGE_INTERVAL (30*60)
1171 time_to_write_hs_statistics = now+WRITE_HSUSAGE_INTERVAL;
1173 /** 10b. write bridge networkstatus file to disk */
1174 if (options->BridgeAuthoritativeDir &&
1175 time_to_write_bridge_status_file < now) {
1176 networkstatus_dump_bridge_status_to_file(now);
1177 #define BRIDGE_STATUSFILE_INTERVAL (30*60)
1178 time_to_write_bridge_status_file = now+BRIDGE_STATUSFILE_INTERVAL;
1182 /** Libevent timer: used to invoke second_elapsed_callback() once per
1183 * second. */
1184 static struct event *timeout_event = NULL;
1185 /** Number of libevent errors in the last second: we die if we get too many. */
1186 static int n_libevent_errors = 0;
1188 /** Libevent callback: invoked once every second. */
1189 static void
1190 second_elapsed_callback(int fd, short event, void *args)
1192 /* XXXX This could be sensibly refactored into multiple callbacks, and we
1193 * could use Libevent's timers for this rather than checking the current
1194 * time against a bunch of timeouts every second. */
1195 static struct timeval one_second;
1196 static time_t current_second = 0;
1197 time_t now;
1198 size_t bytes_written;
1199 size_t bytes_read;
1200 int seconds_elapsed;
1201 or_options_t *options = get_options();
1202 (void)fd;
1203 (void)event;
1204 (void)args;
1205 if (!timeout_event) {
1206 timeout_event = tor_evtimer_new(tor_libevent_get_base(),
1207 second_elapsed_callback, NULL);
1208 one_second.tv_sec = 1;
1209 one_second.tv_usec = 0;
1212 n_libevent_errors = 0;
1214 /* log_fn(LOG_NOTICE, "Tick."); */
1215 now = time(NULL);
1216 update_approx_time(now);
1218 /* the second has rolled over. check more stuff. */
1219 bytes_written = stats_prev_global_write_bucket - global_write_bucket;
1220 bytes_read = stats_prev_global_read_bucket - global_read_bucket;
1221 seconds_elapsed = current_second ? (int)(now - current_second) : 0;
1222 stats_n_bytes_read += bytes_read;
1223 stats_n_bytes_written += bytes_written;
1224 if (accounting_is_enabled(options) && seconds_elapsed >= 0)
1225 accounting_add_bytes(bytes_read, bytes_written, seconds_elapsed);
1226 control_event_bandwidth_used((uint32_t)bytes_read,(uint32_t)bytes_written);
1227 control_event_stream_bandwidth_used();
1229 if (seconds_elapsed > 0)
1230 connection_bucket_refill(seconds_elapsed, now);
1231 stats_prev_global_read_bucket = global_read_bucket;
1232 stats_prev_global_write_bucket = global_write_bucket;
1234 if (server_mode(options) &&
1235 !we_are_hibernating() &&
1236 seconds_elapsed > 0 &&
1237 has_completed_circuit &&
1238 stats_n_seconds_working / TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT !=
1239 (stats_n_seconds_working+seconds_elapsed) /
1240 TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT) {
1241 /* every 20 minutes, check and complain if necessary */
1242 routerinfo_t *me = router_get_my_routerinfo();
1243 if (me && !check_whether_orport_reachable()) {
1244 log_warn(LD_CONFIG,"Your server (%s:%d) has not managed to confirm that "
1245 "its ORPort is reachable. Please check your firewalls, ports, "
1246 "address, /etc/hosts file, etc.",
1247 me->address, me->or_port);
1248 control_event_server_status(LOG_WARN,
1249 "REACHABILITY_FAILED ORADDRESS=%s:%d",
1250 me->address, me->or_port);
1253 if (me && !check_whether_dirport_reachable()) {
1254 log_warn(LD_CONFIG,
1255 "Your server (%s:%d) has not managed to confirm that its "
1256 "DirPort is reachable. Please check your firewalls, ports, "
1257 "address, /etc/hosts file, etc.",
1258 me->address, me->dir_port);
1259 control_event_server_status(LOG_WARN,
1260 "REACHABILITY_FAILED DIRADDRESS=%s:%d",
1261 me->address, me->dir_port);
1265 /** If more than this many seconds have elapsed, probably the clock
1266 * jumped: doesn't count. */
1267 #define NUM_JUMPED_SECONDS_BEFORE_WARN 100
1268 if (seconds_elapsed < -NUM_JUMPED_SECONDS_BEFORE_WARN ||
1269 seconds_elapsed >= NUM_JUMPED_SECONDS_BEFORE_WARN) {
1270 circuit_note_clock_jumped(seconds_elapsed);
1271 /* XXX if the time jumps *back* many months, do our events in
1272 * run_scheduled_events() recover? I don't think they do. -RD */
1273 } else if (seconds_elapsed > 0)
1274 stats_n_seconds_working += seconds_elapsed;
1276 run_scheduled_events(now);
1278 current_second = now; /* remember which second it is, for next time */
1280 #if 0
1281 if (current_second % 300 == 0) {
1282 rep_history_clean(current_second - options->RephistTrackTime);
1283 dumpmemusage(get_min_log_level()<LOG_INFO ?
1284 get_min_log_level() : LOG_INFO);
1286 #endif
1288 if (event_add(timeout_event, &one_second))
1289 log_err(LD_NET,
1290 "Error from libevent when setting one-second timeout event");
1293 #ifndef MS_WINDOWS
1294 /** Called when a possibly ignorable libevent error occurs; ensures that we
1295 * don't get into an infinite loop by ignoring too many errors from
1296 * libevent. */
1297 static int
1298 got_libevent_error(void)
1300 if (++n_libevent_errors > 8) {
1301 log_err(LD_NET, "Too many libevent errors in one second; dying");
1302 return -1;
1304 return 0;
1306 #endif
1308 #define UPTIME_CUTOFF_FOR_NEW_BANDWIDTH_TEST (6*60*60)
1310 /** Called when our IP address seems to have changed. <b>at_interface</b>
1311 * should be true if we detected a change in our interface, and false if we
1312 * detected a change in our published address. */
1313 void
1314 ip_address_changed(int at_interface)
1316 int server = server_mode(get_options());
1318 if (at_interface) {
1319 if (! server) {
1320 /* Okay, change our keys. */
1321 init_keys();
1323 } else {
1324 if (server) {
1325 if (stats_n_seconds_working > UPTIME_CUTOFF_FOR_NEW_BANDWIDTH_TEST)
1326 reset_bandwidth_test();
1327 stats_n_seconds_working = 0;
1328 router_reset_reachability();
1329 mark_my_descriptor_dirty();
1333 dns_servers_relaunch_checks();
1336 /** Forget what we've learned about the correctness of our DNS servers, and
1337 * start learning again. */
1338 void
1339 dns_servers_relaunch_checks(void)
1341 if (server_mode(get_options())) {
1342 dns_reset_correctness_checks();
1343 time_to_check_for_correct_dns = 0;
1347 /** Called when we get a SIGHUP: reload configuration files and keys,
1348 * retry all connections, and so on. */
1349 static int
1350 do_hup(void)
1352 or_options_t *options = get_options();
1354 #ifdef USE_DMALLOC
1355 dmalloc_log_stats();
1356 dmalloc_log_changed(0, 1, 0, 0);
1357 #endif
1359 log_notice(LD_GENERAL,"Received reload signal (hup). Reloading config and "
1360 "resetting internal state.");
1361 if (accounting_is_enabled(options))
1362 accounting_record_bandwidth_usage(time(NULL), get_or_state());
1364 router_reset_warnings();
1365 routerlist_reset_warnings();
1366 addressmap_clear_transient();
1367 /* first, reload config variables, in case they've changed */
1368 if (options->ReloadTorrcOnSIGHUP) {
1369 /* no need to provide argc/v, they've been cached in init_from_config */
1370 if (options_init_from_torrc(0, NULL) < 0) {
1371 log_err(LD_CONFIG,"Reading config failed--see warnings above. "
1372 "For usage, try -h.");
1373 return -1;
1375 options = get_options(); /* they have changed now */
1376 } else {
1377 log_notice(LD_GENERAL, "Not reloading config file: the controller told "
1378 "us not to.");
1380 if (authdir_mode_handles_descs(options, -1)) {
1381 /* reload the approved-routers file */
1382 if (dirserv_load_fingerprint_file() < 0) {
1383 /* warnings are logged from dirserv_load_fingerprint_file() directly */
1384 log_info(LD_GENERAL, "Error reloading fingerprints. "
1385 "Continuing with old list.");
1389 /* Rotate away from the old dirty circuits. This has to be done
1390 * after we've read the new options, but before we start using
1391 * circuits for directory fetches. */
1392 circuit_expire_all_dirty_circs();
1394 /* retry appropriate downloads */
1395 router_reset_status_download_failures();
1396 router_reset_descriptor_download_failures();
1397 update_networkstatus_downloads(time(NULL));
1399 /* We'll retry routerstatus downloads in about 10 seconds; no need to
1400 * force a retry there. */
1402 if (server_mode(options)) {
1403 /* Restart cpuworker and dnsworker processes, so they get up-to-date
1404 * configuration options. */
1405 cpuworkers_rotate();
1406 dns_reset();
1408 return 0;
1411 /** Tor main loop. */
1412 /* static */ int
1413 do_main_loop(void)
1415 int loop_result;
1416 time_t now;
1418 /* initialize dns resolve map, spawn workers if needed */
1419 if (dns_init() < 0) {
1420 if (get_options()->ServerDNSAllowBrokenConfig)
1421 log_warn(LD_GENERAL, "Couldn't set up any working nameservers. "
1422 "Network not up yet? Will try again soon.");
1423 else {
1424 log_err(LD_GENERAL,"Error initializing dns subsystem; exiting. To "
1425 "retry instead, set the ServerDNSAllowBrokenResolvConf option.");
1429 handle_signals(1);
1431 /* load the private keys, if we're supposed to have them, and set up the
1432 * TLS context. */
1433 if (! identity_key_is_set()) {
1434 if (init_keys() < 0) {
1435 log_err(LD_BUG,"Error initializing keys; exiting");
1436 return -1;
1440 /* Set up the packed_cell_t memory pool. */
1441 init_cell_pool();
1443 /* Set up our buckets */
1444 connection_bucket_init();
1445 stats_prev_global_read_bucket = global_read_bucket;
1446 stats_prev_global_write_bucket = global_write_bucket;
1448 /* initialize the bootstrap status events to know we're starting up */
1449 control_event_bootstrap(BOOTSTRAP_STATUS_STARTING, 0);
1451 if (trusted_dirs_reload_certs()) {
1452 log_warn(LD_DIR,
1453 "Couldn't load all cached v3 certificates. Starting anyway.");
1455 if (router_reload_v2_networkstatus()) {
1456 return -1;
1458 if (router_reload_consensus_networkstatus()) {
1459 return -1;
1461 /* load the routers file, or assign the defaults. */
1462 if (router_reload_router_list()) {
1463 return -1;
1465 /* load the networkstatuses. (This launches a download for new routers as
1466 * appropriate.)
1468 now = time(NULL);
1469 directory_info_has_arrived(now, 1);
1471 if (authdir_mode_tests_reachability(get_options())) {
1472 /* the directory is already here, run startup things */
1473 dirserv_test_reachability(now, 1);
1476 if (server_mode(get_options())) {
1477 /* launch cpuworkers. Need to do this *after* we've read the onion key. */
1478 cpu_init();
1481 /* set up once-a-second callback. */
1482 second_elapsed_callback(0,0,NULL);
1484 for (;;) {
1485 if (nt_service_is_stopping())
1486 return 0;
1488 #ifndef MS_WINDOWS
1489 /* Make it easier to tell whether libevent failure is our fault or not. */
1490 errno = 0;
1491 #endif
1492 /* All active linked conns should get their read events activated. */
1493 SMARTLIST_FOREACH(active_linked_connection_lst, connection_t *, conn,
1494 event_active(conn->read_event, EV_READ, 1));
1495 called_loop_once = smartlist_len(active_linked_connection_lst) ? 1 : 0;
1497 update_approx_time(time(NULL));
1499 /* poll until we have an event, or the second ends, or until we have
1500 * some active linked connections to trigger events for. */
1501 loop_result = event_base_loop(tor_libevent_get_base(),
1502 called_loop_once ? EVLOOP_ONCE : 0);
1504 /* let catch() handle things like ^c, and otherwise don't worry about it */
1505 if (loop_result < 0) {
1506 int e = tor_socket_errno(-1);
1507 /* let the program survive things like ^z */
1508 if (e != EINTR && !ERRNO_IS_EINPROGRESS(e)) {
1509 log_err(LD_NET,"libevent call with %s failed: %s [%d]",
1510 tor_libevent_get_method(), tor_socket_strerror(e), e);
1511 return -1;
1512 #ifndef MS_WINDOWS
1513 } else if (e == EINVAL) {
1514 log_warn(LD_NET, "EINVAL from libevent: should you upgrade libevent?");
1515 if (got_libevent_error())
1516 return -1;
1517 #endif
1518 } else {
1519 if (ERRNO_IS_EINPROGRESS(e))
1520 log_warn(LD_BUG,
1521 "libevent call returned EINPROGRESS? Please report.");
1522 log_debug(LD_NET,"libevent call interrupted.");
1523 /* You can't trust the results of this poll(). Go back to the
1524 * top of the big for loop. */
1525 continue;
1531 /** Used to implement the SIGNAL control command: if we accept
1532 * <b>the_signal</b> as a remote pseudo-signal, act on it. */
1533 /* We don't re-use catch() here because:
1534 * 1. We handle a different set of signals than those allowed in catch.
1535 * 2. Platforms without signal() are unlikely to define SIGfoo.
1536 * 3. The control spec is defined to use fixed numeric signal values
1537 * which just happen to match the Unix values.
1539 void
1540 control_signal_act(int the_signal)
1542 switch (the_signal)
1544 case 1:
1545 signal_callback(0,0,(void*)(uintptr_t)SIGHUP);
1546 break;
1547 case 2:
1548 signal_callback(0,0,(void*)(uintptr_t)SIGINT);
1549 break;
1550 case 10:
1551 signal_callback(0,0,(void*)(uintptr_t)SIGUSR1);
1552 break;
1553 case 12:
1554 signal_callback(0,0,(void*)(uintptr_t)SIGUSR2);
1555 break;
1556 case 15:
1557 signal_callback(0,0,(void*)(uintptr_t)SIGTERM);
1558 break;
1559 case SIGNEWNYM:
1560 signal_callback(0,0,(void*)(uintptr_t)SIGNEWNYM);
1561 break;
1562 case SIGCLEARDNSCACHE:
1563 signal_callback(0,0,(void*)(uintptr_t)SIGCLEARDNSCACHE);
1564 break;
1565 default:
1566 log_warn(LD_BUG, "Unrecognized signal number %d.", the_signal);
1567 break;
1571 /** Libevent callback: invoked when we get a signal.
1573 static void
1574 signal_callback(int fd, short events, void *arg)
1576 uintptr_t sig = (uintptr_t)arg;
1577 (void)fd;
1578 (void)events;
1579 switch (sig)
1581 case SIGTERM:
1582 log_notice(LD_GENERAL,"Catching signal TERM, exiting cleanly.");
1583 tor_cleanup();
1584 exit(0);
1585 break;
1586 case SIGINT:
1587 if (!server_mode(get_options())) { /* do it now */
1588 log_notice(LD_GENERAL,"Interrupt: exiting cleanly.");
1589 tor_cleanup();
1590 exit(0);
1592 hibernate_begin_shutdown();
1593 break;
1594 #ifdef SIGPIPE
1595 case SIGPIPE:
1596 log_debug(LD_GENERAL,"Caught SIGPIPE. Ignoring.");
1597 break;
1598 #endif
1599 case SIGUSR1:
1600 /* prefer to log it at INFO, but make sure we always see it */
1601 dumpstats(get_min_log_level()<LOG_INFO ? get_min_log_level() : LOG_INFO);
1602 break;
1603 case SIGUSR2:
1604 switch_logs_debug();
1605 log_debug(LD_GENERAL,"Caught USR2, going to loglevel debug. "
1606 "Send HUP to change back.");
1607 break;
1608 case SIGHUP:
1609 if (do_hup() < 0) {
1610 log_warn(LD_CONFIG,"Restart failed (config error?). Exiting.");
1611 tor_cleanup();
1612 exit(1);
1614 break;
1615 #ifdef SIGCHLD
1616 case SIGCHLD:
1617 while (waitpid(-1,NULL,WNOHANG) > 0) ; /* keep reaping until no more
1618 zombies */
1619 break;
1620 #endif
1621 case SIGNEWNYM: {
1622 time_t now = time(NULL);
1623 if (time_of_last_signewnym + MAX_SIGNEWNYM_RATE > now) {
1624 signewnym_is_pending = 1;
1625 log(LOG_NOTICE, LD_CONTROL,
1626 "Rate limiting NEWNYM request: delaying by %d second(s)",
1627 (int)(MAX_SIGNEWNYM_RATE+time_of_last_signewnym-now));
1628 } else {
1629 signewnym_impl(now);
1631 break;
1633 case SIGCLEARDNSCACHE:
1634 addressmap_clear_transient();
1635 break;
1639 extern uint64_t rephist_total_alloc;
1640 extern uint32_t rephist_total_num;
1643 * Write current memory usage information to the log.
1645 static void
1646 dumpmemusage(int severity)
1648 connection_dump_buffer_mem_stats(severity);
1649 log(severity, LD_GENERAL, "In rephist: "U64_FORMAT" used by %d Tors.",
1650 U64_PRINTF_ARG(rephist_total_alloc), rephist_total_num);
1651 dump_routerlist_mem_usage(severity);
1652 dump_cell_pool_usage(severity);
1653 dump_dns_mem_usage(severity);
1654 buf_dump_freelist_sizes(severity);
1655 tor_log_mallinfo(severity);
1658 /** Write all statistics to the log, with log level 'severity'. Called
1659 * in response to a SIGUSR1. */
1660 static void
1661 dumpstats(int severity)
1663 time_t now = time(NULL);
1664 time_t elapsed;
1665 size_t rbuf_cap, wbuf_cap, rbuf_len, wbuf_len;
1667 log(severity, LD_GENERAL, "Dumping stats:");
1669 SMARTLIST_FOREACH(connection_array, connection_t *, conn,
1671 int i = conn_sl_idx;
1672 log(severity, LD_GENERAL,
1673 "Conn %d (socket %d) type %d (%s), state %d (%s), created %d secs ago",
1674 i, conn->s, conn->type, conn_type_to_string(conn->type),
1675 conn->state, conn_state_to_string(conn->type, conn->state),
1676 (int)(now - conn->timestamp_created));
1677 if (!connection_is_listener(conn)) {
1678 log(severity,LD_GENERAL,
1679 "Conn %d is to %s:%d.", i,
1680 safe_str(conn->address), conn->port);
1681 log(severity,LD_GENERAL,
1682 "Conn %d: %d bytes waiting on inbuf (len %d, last read %d secs ago)",
1684 (int)buf_datalen(conn->inbuf),
1685 (int)buf_allocation(conn->inbuf),
1686 (int)(now - conn->timestamp_lastread));
1687 log(severity,LD_GENERAL,
1688 "Conn %d: %d bytes waiting on outbuf "
1689 "(len %d, last written %d secs ago)",i,
1690 (int)buf_datalen(conn->outbuf),
1691 (int)buf_allocation(conn->outbuf),
1692 (int)(now - conn->timestamp_lastwritten));
1693 if (conn->type == CONN_TYPE_OR) {
1694 or_connection_t *or_conn = TO_OR_CONN(conn);
1695 if (or_conn->tls) {
1696 tor_tls_get_buffer_sizes(or_conn->tls, &rbuf_cap, &rbuf_len,
1697 &wbuf_cap, &wbuf_len);
1698 log(severity, LD_GENERAL,
1699 "Conn %d: %d/%d bytes used on OpenSSL read buffer; "
1700 "%d/%d bytes used on write buffer.",
1701 i, (int)rbuf_len, (int)rbuf_cap, (int)wbuf_len, (int)wbuf_cap);
1705 circuit_dump_by_conn(conn, severity); /* dump info about all the circuits
1706 * using this conn */
1708 log(severity, LD_NET,
1709 "Cells processed: "U64_FORMAT" padding\n"
1710 " "U64_FORMAT" create\n"
1711 " "U64_FORMAT" created\n"
1712 " "U64_FORMAT" relay\n"
1713 " ("U64_FORMAT" relayed)\n"
1714 " ("U64_FORMAT" delivered)\n"
1715 " "U64_FORMAT" destroy",
1716 U64_PRINTF_ARG(stats_n_padding_cells_processed),
1717 U64_PRINTF_ARG(stats_n_create_cells_processed),
1718 U64_PRINTF_ARG(stats_n_created_cells_processed),
1719 U64_PRINTF_ARG(stats_n_relay_cells_processed),
1720 U64_PRINTF_ARG(stats_n_relay_cells_relayed),
1721 U64_PRINTF_ARG(stats_n_relay_cells_delivered),
1722 U64_PRINTF_ARG(stats_n_destroy_cells_processed));
1723 if (stats_n_data_cells_packaged)
1724 log(severity,LD_NET,"Average packaged cell fullness: %2.3f%%",
1725 100*(U64_TO_DBL(stats_n_data_bytes_packaged) /
1726 U64_TO_DBL(stats_n_data_cells_packaged*RELAY_PAYLOAD_SIZE)) );
1727 if (stats_n_data_cells_received)
1728 log(severity,LD_NET,"Average delivered cell fullness: %2.3f%%",
1729 100*(U64_TO_DBL(stats_n_data_bytes_received) /
1730 U64_TO_DBL(stats_n_data_cells_received*RELAY_PAYLOAD_SIZE)) );
1732 if (now - time_of_process_start >= 0)
1733 elapsed = now - time_of_process_start;
1734 else
1735 elapsed = 0;
1737 if (elapsed) {
1738 log(severity, LD_NET,
1739 "Average bandwidth: "U64_FORMAT"/%d = %d bytes/sec reading",
1740 U64_PRINTF_ARG(stats_n_bytes_read),
1741 (int)elapsed,
1742 (int) (stats_n_bytes_read/elapsed));
1743 log(severity, LD_NET,
1744 "Average bandwidth: "U64_FORMAT"/%d = %d bytes/sec writing",
1745 U64_PRINTF_ARG(stats_n_bytes_written),
1746 (int)elapsed,
1747 (int) (stats_n_bytes_written/elapsed));
1750 log(severity, LD_NET, "--------------- Dumping memory information:");
1751 dumpmemusage(severity);
1753 rep_hist_dump_stats(now,severity);
1754 rend_service_dump_stats(severity);
1755 dump_pk_ops(severity);
1756 dump_distinct_digest_count(severity);
1759 /** Called by exit() as we shut down the process.
1761 static void
1762 exit_function(void)
1764 /* NOTE: If we ever daemonize, this gets called immediately. That's
1765 * okay for now, because we only use this on Windows. */
1766 #ifdef MS_WINDOWS
1767 WSACleanup();
1768 #endif
1771 /** Set up the signal handlers for either parent or child. */
1772 void
1773 handle_signals(int is_parent)
1775 #ifndef MS_WINDOWS /* do signal stuff only on Unix */
1776 int i;
1777 static const int signals[] = {
1778 SIGINT, /* do a controlled slow shutdown */
1779 SIGTERM, /* to terminate now */
1780 SIGPIPE, /* otherwise SIGPIPE kills us */
1781 SIGUSR1, /* dump stats */
1782 SIGUSR2, /* go to loglevel debug */
1783 SIGHUP, /* to reload config, retry conns, etc */
1784 #ifdef SIGXFSZ
1785 SIGXFSZ, /* handle file-too-big resource exhaustion */
1786 #endif
1787 SIGCHLD, /* handle dns/cpu workers that exit */
1788 -1 };
1789 static struct event *signal_events[16]; /* bigger than it has to be. */
1790 if (is_parent) {
1791 for (i = 0; signals[i] >= 0; ++i) {
1792 signal_events[i] = tor_evsignal_new(
1793 tor_libevent_get_base(), signals[i], signal_callback,
1794 (void*)(uintptr_t)signals[i]);
1795 if (event_add(signal_events[i], NULL))
1796 log_warn(LD_BUG, "Error from libevent when adding event for signal %d",
1797 signals[i]);
1799 } else {
1800 struct sigaction action;
1801 action.sa_flags = 0;
1802 sigemptyset(&action.sa_mask);
1803 action.sa_handler = SIG_IGN;
1804 sigaction(SIGINT, &action, NULL);
1805 sigaction(SIGTERM, &action, NULL);
1806 sigaction(SIGPIPE, &action, NULL);
1807 sigaction(SIGUSR1, &action, NULL);
1808 sigaction(SIGUSR2, &action, NULL);
1809 sigaction(SIGHUP, &action, NULL);
1810 #ifdef SIGXFSZ
1811 sigaction(SIGXFSZ, &action, NULL);
1812 #endif
1814 #else /* MS windows */
1815 (void)is_parent;
1816 #endif /* signal stuff */
1819 /** Main entry point for the Tor command-line client.
1821 /* static */ int
1822 tor_init(int argc, char *argv[])
1824 char buf[256];
1825 int i, quiet = 0;
1826 time_of_process_start = time(NULL);
1827 if (!connection_array)
1828 connection_array = smartlist_create();
1829 if (!closeable_connection_lst)
1830 closeable_connection_lst = smartlist_create();
1831 if (!active_linked_connection_lst)
1832 active_linked_connection_lst = smartlist_create();
1833 /* Have the log set up with our application name. */
1834 tor_snprintf(buf, sizeof(buf), "Tor %s", get_version());
1835 log_set_application_name(buf);
1836 /* Initialize the history structures. */
1837 rep_hist_init();
1838 /* Initialize the service cache. */
1839 rend_cache_init();
1840 addressmap_init(); /* Init the client dns cache. Do it always, since it's
1841 * cheap. */
1843 /* We search for the "quiet" option first, since it decides whether we
1844 * will log anything at all to the command line. */
1845 for (i=1;i<argc;++i) {
1846 if (!strcmp(argv[i], "--hush"))
1847 quiet = 1;
1848 if (!strcmp(argv[i], "--quiet"))
1849 quiet = 2;
1851 /* give it somewhere to log to initially */
1852 switch (quiet) {
1853 case 2:
1854 /* no initial logging */
1855 break;
1856 case 1:
1857 add_temp_log(LOG_WARN);
1858 break;
1859 default:
1860 add_temp_log(LOG_NOTICE);
1863 log(LOG_NOTICE, LD_GENERAL, "Tor v%s. This is experimental software. "
1864 "Do not rely on it for strong anonymity. (Running on %s)",get_version(),
1865 get_uname());
1867 if (network_init()<0) {
1868 log_err(LD_BUG,"Error initializing network; exiting.");
1869 return -1;
1871 atexit(exit_function);
1873 if (options_init_from_torrc(argc,argv) < 0) {
1874 log_err(LD_CONFIG,"Reading config failed--see warnings above.");
1875 return -1;
1878 #ifndef MS_WINDOWS
1879 if (geteuid()==0)
1880 log_warn(LD_GENERAL,"You are running Tor as root. You don't need to, "
1881 "and you probably shouldn't.");
1882 #endif
1884 if (crypto_global_init(get_options()->HardwareAccel,
1885 get_options()->AccelName,
1886 get_options()->AccelDir)) {
1887 log_err(LD_BUG, "Unable to initialize OpenSSL. Exiting.");
1888 return -1;
1891 return 0;
1894 /** A lockfile structure, used to prevent two Tors from messing with the
1895 * data directory at once. If this variable is non-NULL, we're holding
1896 * the lockfile. */
1897 static tor_lockfile_t *lockfile = NULL;
1899 /** Try to grab the lock file described in <b>options</b>, if we do not
1900 * already have it. If <b>err_if_locked</b> is true, warn if somebody else is
1901 * holding the lock, and exit if we can't get it after waiting. Otherwise,
1902 * return -1 if we can't get the lockfile. Return 0 on success.
1905 try_locking(or_options_t *options, int err_if_locked)
1907 if (lockfile)
1908 return 0;
1909 else {
1910 char *fname = options_get_datadir_fname2_suffix(options, "lock",NULL,NULL);
1911 int already_locked = 0;
1912 tor_lockfile_t *lf = tor_lockfile_lock(fname, 0, &already_locked);
1913 tor_free(fname);
1914 if (!lf) {
1915 if (err_if_locked && already_locked) {
1916 int r;
1917 log_warn(LD_GENERAL, "It looks like another Tor process is running "
1918 "with the same data directory. Waiting 5 seconds to see "
1919 "if it goes away.");
1920 #ifndef WIN32
1921 sleep(5);
1922 #else
1923 Sleep(5000);
1924 #endif
1925 r = try_locking(options, 0);
1926 if (r<0) {
1927 log_err(LD_GENERAL, "No, it's still there. Exiting.");
1928 exit(0);
1930 return r;
1932 return -1;
1934 lockfile = lf;
1935 return 0;
1939 /** Return true iff we've successfully acquired the lock file. */
1941 have_lockfile(void)
1943 return lockfile != NULL;
1946 /** If we have successfully acquired the lock file, release it. */
1947 void
1948 release_lockfile(void)
1950 if (lockfile) {
1951 tor_lockfile_unlock(lockfile);
1952 lockfile = NULL;
1956 /** Free all memory that we might have allocated somewhere.
1957 * If <b>postfork</b>, we are a worker process and we want to free
1958 * only the parts of memory that we won't touch. If !<b>postfork</b>,
1959 * Tor is shutting down and we should free everything.
1961 * Helps us find the real leaks with dmalloc and the like. Also valgrind
1962 * should then report 0 reachable in its leak report (in an ideal world --
1963 * in practice libevent, SSL, libc etc never quite free everything). */
1964 void
1965 tor_free_all(int postfork)
1967 if (!postfork) {
1968 evdns_shutdown(1);
1970 geoip_free_all();
1971 dirvote_free_all();
1972 routerlist_free_all();
1973 networkstatus_free_all();
1974 addressmap_free_all();
1975 dirserv_free_all();
1976 rend_service_free_all();
1977 rend_cache_free_all();
1978 rend_service_authorization_free_all();
1979 rep_hist_free_all();
1980 hs_usage_free_all();
1981 dns_free_all();
1982 clear_pending_onions();
1983 circuit_free_all();
1984 entry_guards_free_all();
1985 connection_free_all();
1986 buf_shrink_freelists(1);
1987 memarea_clear_freelist();
1988 if (!postfork) {
1989 config_free_all();
1990 router_free_all();
1991 policies_free_all();
1993 free_cell_pool();
1994 if (!postfork) {
1995 tor_tls_free_all();
1997 /* stuff in main.c */
1998 if (connection_array)
1999 smartlist_free(connection_array);
2000 if (closeable_connection_lst)
2001 smartlist_free(closeable_connection_lst);
2002 if (active_linked_connection_lst)
2003 smartlist_free(active_linked_connection_lst);
2004 tor_free(timeout_event);
2005 if (!postfork) {
2006 release_lockfile();
2008 /* Stuff in util.c and address.c*/
2009 if (!postfork) {
2010 escaped(NULL);
2011 esc_router_info(NULL);
2012 logs_free_all(); /* free log strings. do this last so logs keep working. */
2016 /** Do whatever cleanup is necessary before shutting Tor down. */
2017 void
2018 tor_cleanup(void)
2020 or_options_t *options = get_options();
2021 /* Remove our pid file. We don't care if there was an error when we
2022 * unlink, nothing we could do about it anyways. */
2023 if (options->command == CMD_RUN_TOR) {
2024 time_t now = time(NULL);
2025 if (options->PidFile)
2026 unlink(options->PidFile);
2027 if (accounting_is_enabled(options))
2028 accounting_record_bandwidth_usage(now, get_or_state());
2029 or_state_mark_dirty(get_or_state(), 0); /* force an immediate save. */
2030 or_state_save(now);
2031 if (authdir_mode_tests_reachability(options))
2032 rep_hist_record_mtbf_data(now, 0);
2034 #ifdef USE_DMALLOC
2035 dmalloc_log_stats();
2036 #endif
2037 tor_free_all(0); /* We could move tor_free_all back into the ifdef below
2038 later, if it makes shutdown unacceptably slow. But for
2039 now, leave it here: it's helped us catch bugs in the
2040 past. */
2041 crypto_global_cleanup();
2042 #ifdef USE_DMALLOC
2043 dmalloc_log_unfreed();
2044 dmalloc_shutdown();
2045 #endif
2048 /** Read/create keys as needed, and echo our fingerprint to stdout. */
2049 /* static */ int
2050 do_list_fingerprint(void)
2052 char buf[FINGERPRINT_LEN+1];
2053 crypto_pk_env_t *k;
2054 const char *nickname = get_options()->Nickname;
2055 if (!server_mode(get_options())) {
2056 log_err(LD_GENERAL,
2057 "Clients don't have long-term identity keys. Exiting.\n");
2058 return -1;
2060 tor_assert(nickname);
2061 if (init_keys() < 0) {
2062 log_err(LD_BUG,"Error initializing keys; can't display fingerprint");
2063 return -1;
2065 if (!(k = get_identity_key())) {
2066 log_err(LD_GENERAL,"Error: missing identity key.");
2067 return -1;
2069 if (crypto_pk_get_fingerprint(k, buf, 1)<0) {
2070 log_err(LD_BUG, "Error computing fingerprint");
2071 return -1;
2073 printf("%s %s\n", nickname, buf);
2074 return 0;
2077 /** Entry point for password hashing: take the desired password from
2078 * the command line, and print its salted hash to stdout. **/
2079 /* static */ void
2080 do_hash_password(void)
2083 char output[256];
2084 char key[S2K_SPECIFIER_LEN+DIGEST_LEN];
2086 crypto_rand(key, S2K_SPECIFIER_LEN-1);
2087 key[S2K_SPECIFIER_LEN-1] = (uint8_t)96; /* Hash 64 K of data. */
2088 secret_to_key(key+S2K_SPECIFIER_LEN, DIGEST_LEN,
2089 get_options()->command_arg, strlen(get_options()->command_arg),
2090 key);
2091 base16_encode(output, sizeof(output), key, sizeof(key));
2092 printf("16:%s\n",output);
2095 /** Main entry point for the Tor process. Called from main(). */
2096 /* This function is distinct from main() only so we can link main.c into
2097 * the unittest binary without conflicting with the unittests' main. */
2099 tor_main(int argc, char *argv[])
2101 int result = 0;
2102 update_approx_time(time(NULL));
2103 tor_threads_init();
2104 init_logging();
2105 #ifdef USE_DMALLOC
2107 /* Instruct OpenSSL to use our internal wrappers for malloc,
2108 realloc and free. */
2109 int r = CRYPTO_set_mem_ex_functions(_tor_malloc, _tor_realloc, _tor_free);
2110 tor_assert(r);
2112 #endif
2113 #ifdef NT_SERVICE
2115 int done = 0;
2116 result = nt_service_parse_options(argc, argv, &done);
2117 if (done) return result;
2119 #endif
2120 if (tor_init(argc, argv)<0)
2121 return -1;
2122 switch (get_options()->command) {
2123 case CMD_RUN_TOR:
2124 #ifdef NT_SERVICE
2125 nt_service_set_state(SERVICE_RUNNING);
2126 #endif
2127 result = do_main_loop();
2128 break;
2129 case CMD_LIST_FINGERPRINT:
2130 result = do_list_fingerprint();
2131 break;
2132 case CMD_HASH_PASSWORD:
2133 do_hash_password();
2134 result = 0;
2135 break;
2136 case CMD_VERIFY_CONFIG:
2137 printf("Configuration was valid\n");
2138 result = 0;
2139 break;
2140 case CMD_RUN_UNITTESTS: /* only set by test.c */
2141 default:
2142 log_warn(LD_BUG,"Illegal command number %d: internal error.",
2143 get_options()->command);
2144 result = -1;
2146 tor_cleanup();
2147 return result;