Remove v0 hidden service statistics code.
[tor/rransom.git] / src / or / main.c
blob443e35eab77630a5ba0e953aea4e0ad64bc11ff9
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_client(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_client(conn->address),
620 conn->s, 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_client(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);
734 tor_assert(conn->outbuf);
736 if (or_conn->is_bad_for_new_circs && !or_conn->n_circuits) {
737 /* It's bad for new circuits, and has no unmarked circuits on it:
738 * mark it now. */
739 log_info(LD_OR,
740 "Expiring non-used OR connection to fd %d (%s:%d) [Too old].",
741 conn->s, conn->address, conn->port);
742 if (conn->state == OR_CONN_STATE_CONNECTING)
743 connection_or_connect_failed(TO_OR_CONN(conn),
744 END_OR_CONN_REASON_TIMEOUT,
745 "Tor gave up on the connection");
746 connection_mark_for_close(conn);
747 conn->hold_open_until_flushed = 1;
748 return;
751 /* If we haven't written to an OR connection for a while, then either nuke
752 the connection or send a keepalive, depending. */
753 if (now >= conn->timestamp_lastwritten + options->KeepalivePeriod) {
754 routerinfo_t *router = router_get_by_digest(or_conn->identity_digest);
755 int maxCircuitlessPeriod = options->MaxCircuitDirtiness*3/2;
756 if (!connection_state_is_open(conn)) {
757 /* We never managed to actually get this connection open and happy. */
758 log_info(LD_OR,"Expiring non-open OR connection to fd %d (%s:%d).",
759 conn->s,conn->address, conn->port);
760 connection_mark_for_close(conn);
761 conn->hold_open_until_flushed = 1;
762 } else if (we_are_hibernating() && !or_conn->n_circuits &&
763 !buf_datalen(conn->outbuf)) {
764 /* We're hibernating, there's no circuits, and nothing to flush.*/
765 log_info(LD_OR,"Expiring non-used OR connection to fd %d (%s:%d) "
766 "[Hibernating or exiting].",
767 conn->s,conn->address, conn->port);
768 connection_mark_for_close(conn);
769 conn->hold_open_until_flushed = 1;
770 } else if (!clique_mode(options) && !or_conn->n_circuits &&
771 now >= or_conn->timestamp_last_added_nonpadding +
772 maxCircuitlessPeriod &&
773 (!router || !server_mode(options) ||
774 !router_is_clique_mode(router))) {
775 log_info(LD_OR,"Expiring non-used OR connection to fd %d (%s:%d) "
776 "[Not in clique mode].",
777 conn->s,conn->address, conn->port);
778 connection_mark_for_close(conn);
779 conn->hold_open_until_flushed = 1;
780 } else if (
781 now >= or_conn->timestamp_lastempty + options->KeepalivePeriod*10 &&
782 now >= conn->timestamp_lastwritten + options->KeepalivePeriod*10) {
783 log_fn(LOG_PROTOCOL_WARN,LD_PROTOCOL,
784 "Expiring stuck OR connection to fd %d (%s:%d). (%d bytes to "
785 "flush; %d seconds since last write)",
786 conn->s, conn->address, conn->port,
787 (int)buf_datalen(conn->outbuf),
788 (int)(now-conn->timestamp_lastwritten));
789 connection_mark_for_close(conn);
790 } else if (!buf_datalen(conn->outbuf)) {
791 /* either in clique mode, or we've got a circuit. send a padding cell. */
792 log_fn(LOG_DEBUG,LD_OR,"Sending keepalive to (%s:%d)",
793 conn->address, conn->port);
794 memset(&cell,0,sizeof(cell_t));
795 cell.command = CELL_PADDING;
796 connection_or_write_cell_to_buf(&cell, or_conn);
801 /** Honor a NEWNYM request: make future requests unlinkable to past
802 * requests. */
803 static void
804 signewnym_impl(time_t now)
806 circuit_expire_all_dirty_circs();
807 addressmap_clear_transient();
808 time_of_last_signewnym = now;
809 signewnym_is_pending = 0;
812 /** Perform regular maintenance tasks. This function gets run once per
813 * second by second_elapsed_callback().
815 static void
816 run_scheduled_events(time_t now)
818 static time_t last_rotated_x509_certificate = 0;
819 static time_t time_to_check_v3_certificate = 0;
820 static time_t time_to_check_listeners = 0;
821 static time_t time_to_check_descriptor = 0;
822 static time_t time_to_check_ipaddress = 0;
823 static time_t time_to_shrink_memory = 0;
824 static time_t time_to_try_getting_descriptors = 0;
825 static time_t time_to_reset_descriptor_failures = 0;
826 static time_t time_to_add_entropy = 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 /** 10b. write bridge networkstatus file to disk */
1168 if (options->BridgeAuthoritativeDir &&
1169 time_to_write_bridge_status_file < now) {
1170 networkstatus_dump_bridge_status_to_file(now);
1171 #define BRIDGE_STATUSFILE_INTERVAL (30*60)
1172 time_to_write_bridge_status_file = now+BRIDGE_STATUSFILE_INTERVAL;
1176 /** Libevent timer: used to invoke second_elapsed_callback() once per
1177 * second. */
1178 static struct event *timeout_event = NULL;
1179 /** Number of libevent errors in the last second: we die if we get too many. */
1180 static int n_libevent_errors = 0;
1182 /** Libevent callback: invoked once every second. */
1183 static void
1184 second_elapsed_callback(int fd, short event, void *args)
1186 /* XXXX This could be sensibly refactored into multiple callbacks, and we
1187 * could use Libevent's timers for this rather than checking the current
1188 * time against a bunch of timeouts every second. */
1189 static struct timeval one_second;
1190 static time_t current_second = 0;
1191 time_t now;
1192 size_t bytes_written;
1193 size_t bytes_read;
1194 int seconds_elapsed;
1195 or_options_t *options = get_options();
1196 (void)fd;
1197 (void)event;
1198 (void)args;
1199 if (!timeout_event) {
1200 timeout_event = tor_evtimer_new(tor_libevent_get_base(),
1201 second_elapsed_callback, NULL);
1202 one_second.tv_sec = 1;
1203 one_second.tv_usec = 0;
1206 n_libevent_errors = 0;
1208 /* log_fn(LOG_NOTICE, "Tick."); */
1209 now = time(NULL);
1210 update_approx_time(now);
1212 /* the second has rolled over. check more stuff. */
1213 bytes_written = stats_prev_global_write_bucket - global_write_bucket;
1214 bytes_read = stats_prev_global_read_bucket - global_read_bucket;
1215 seconds_elapsed = current_second ? (int)(now - current_second) : 0;
1216 stats_n_bytes_read += bytes_read;
1217 stats_n_bytes_written += bytes_written;
1218 if (accounting_is_enabled(options) && seconds_elapsed >= 0)
1219 accounting_add_bytes(bytes_read, bytes_written, seconds_elapsed);
1220 control_event_bandwidth_used((uint32_t)bytes_read,(uint32_t)bytes_written);
1221 control_event_stream_bandwidth_used();
1223 if (seconds_elapsed > 0)
1224 connection_bucket_refill(seconds_elapsed, now);
1225 stats_prev_global_read_bucket = global_read_bucket;
1226 stats_prev_global_write_bucket = global_write_bucket;
1228 if (server_mode(options) &&
1229 !we_are_hibernating() &&
1230 seconds_elapsed > 0 &&
1231 has_completed_circuit &&
1232 stats_n_seconds_working / TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT !=
1233 (stats_n_seconds_working+seconds_elapsed) /
1234 TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT) {
1235 /* every 20 minutes, check and complain if necessary */
1236 routerinfo_t *me = router_get_my_routerinfo();
1237 if (me && !check_whether_orport_reachable()) {
1238 log_warn(LD_CONFIG,"Your server (%s:%d) has not managed to confirm that "
1239 "its ORPort is reachable. Please check your firewalls, ports, "
1240 "address, /etc/hosts file, etc.",
1241 me->address, me->or_port);
1242 control_event_server_status(LOG_WARN,
1243 "REACHABILITY_FAILED ORADDRESS=%s:%d",
1244 me->address, me->or_port);
1247 if (me && !check_whether_dirport_reachable()) {
1248 log_warn(LD_CONFIG,
1249 "Your server (%s:%d) has not managed to confirm that its "
1250 "DirPort is reachable. Please check your firewalls, ports, "
1251 "address, /etc/hosts file, etc.",
1252 me->address, me->dir_port);
1253 control_event_server_status(LOG_WARN,
1254 "REACHABILITY_FAILED DIRADDRESS=%s:%d",
1255 me->address, me->dir_port);
1259 /** If more than this many seconds have elapsed, probably the clock
1260 * jumped: doesn't count. */
1261 #define NUM_JUMPED_SECONDS_BEFORE_WARN 100
1262 if (seconds_elapsed < -NUM_JUMPED_SECONDS_BEFORE_WARN ||
1263 seconds_elapsed >= NUM_JUMPED_SECONDS_BEFORE_WARN) {
1264 circuit_note_clock_jumped(seconds_elapsed);
1265 /* XXX if the time jumps *back* many months, do our events in
1266 * run_scheduled_events() recover? I don't think they do. -RD */
1267 } else if (seconds_elapsed > 0)
1268 stats_n_seconds_working += seconds_elapsed;
1270 run_scheduled_events(now);
1272 current_second = now; /* remember which second it is, for next time */
1274 #if 0
1275 if (current_second % 300 == 0) {
1276 rep_history_clean(current_second - options->RephistTrackTime);
1277 dumpmemusage(get_min_log_level()<LOG_INFO ?
1278 get_min_log_level() : LOG_INFO);
1280 #endif
1282 if (event_add(timeout_event, &one_second))
1283 log_err(LD_NET,
1284 "Error from libevent when setting one-second timeout event");
1287 #ifndef MS_WINDOWS
1288 /** Called when a possibly ignorable libevent error occurs; ensures that we
1289 * don't get into an infinite loop by ignoring too many errors from
1290 * libevent. */
1291 static int
1292 got_libevent_error(void)
1294 if (++n_libevent_errors > 8) {
1295 log_err(LD_NET, "Too many libevent errors in one second; dying");
1296 return -1;
1298 return 0;
1300 #endif
1302 #define UPTIME_CUTOFF_FOR_NEW_BANDWIDTH_TEST (6*60*60)
1304 /** Called when our IP address seems to have changed. <b>at_interface</b>
1305 * should be true if we detected a change in our interface, and false if we
1306 * detected a change in our published address. */
1307 void
1308 ip_address_changed(int at_interface)
1310 int server = server_mode(get_options());
1312 if (at_interface) {
1313 if (! server) {
1314 /* Okay, change our keys. */
1315 init_keys();
1317 } else {
1318 if (server) {
1319 if (stats_n_seconds_working > UPTIME_CUTOFF_FOR_NEW_BANDWIDTH_TEST)
1320 reset_bandwidth_test();
1321 stats_n_seconds_working = 0;
1322 router_reset_reachability();
1323 mark_my_descriptor_dirty();
1327 dns_servers_relaunch_checks();
1330 /** Forget what we've learned about the correctness of our DNS servers, and
1331 * start learning again. */
1332 void
1333 dns_servers_relaunch_checks(void)
1335 if (server_mode(get_options())) {
1336 dns_reset_correctness_checks();
1337 time_to_check_for_correct_dns = 0;
1341 /** Called when we get a SIGHUP: reload configuration files and keys,
1342 * retry all connections, and so on. */
1343 static int
1344 do_hup(void)
1346 or_options_t *options = get_options();
1348 #ifdef USE_DMALLOC
1349 dmalloc_log_stats();
1350 dmalloc_log_changed(0, 1, 0, 0);
1351 #endif
1353 log_notice(LD_GENERAL,"Received reload signal (hup). Reloading config and "
1354 "resetting internal state.");
1355 if (accounting_is_enabled(options))
1356 accounting_record_bandwidth_usage(time(NULL), get_or_state());
1358 router_reset_warnings();
1359 routerlist_reset_warnings();
1360 addressmap_clear_transient();
1361 /* first, reload config variables, in case they've changed */
1362 if (options->ReloadTorrcOnSIGHUP) {
1363 /* no need to provide argc/v, they've been cached in init_from_config */
1364 if (options_init_from_torrc(0, NULL) < 0) {
1365 log_err(LD_CONFIG,"Reading config failed--see warnings above. "
1366 "For usage, try -h.");
1367 return -1;
1369 options = get_options(); /* they have changed now */
1370 } else {
1371 log_notice(LD_GENERAL, "Not reloading config file: the controller told "
1372 "us not to.");
1374 if (authdir_mode_handles_descs(options, -1)) {
1375 /* reload the approved-routers file */
1376 if (dirserv_load_fingerprint_file() < 0) {
1377 /* warnings are logged from dirserv_load_fingerprint_file() directly */
1378 log_info(LD_GENERAL, "Error reloading fingerprints. "
1379 "Continuing with old list.");
1383 /* Rotate away from the old dirty circuits. This has to be done
1384 * after we've read the new options, but before we start using
1385 * circuits for directory fetches. */
1386 circuit_expire_all_dirty_circs();
1388 /* retry appropriate downloads */
1389 router_reset_status_download_failures();
1390 router_reset_descriptor_download_failures();
1391 update_networkstatus_downloads(time(NULL));
1393 /* We'll retry routerstatus downloads in about 10 seconds; no need to
1394 * force a retry there. */
1396 if (server_mode(options)) {
1397 /* Restart cpuworker and dnsworker processes, so they get up-to-date
1398 * configuration options. */
1399 cpuworkers_rotate();
1400 dns_reset();
1402 return 0;
1405 /** Tor main loop. */
1406 /* static */ int
1407 do_main_loop(void)
1409 int loop_result;
1410 time_t now;
1412 /* initialize dns resolve map, spawn workers if needed */
1413 if (dns_init() < 0) {
1414 if (get_options()->ServerDNSAllowBrokenConfig)
1415 log_warn(LD_GENERAL, "Couldn't set up any working nameservers. "
1416 "Network not up yet? Will try again soon.");
1417 else {
1418 log_err(LD_GENERAL,"Error initializing dns subsystem; exiting. To "
1419 "retry instead, set the ServerDNSAllowBrokenResolvConf option.");
1423 handle_signals(1);
1425 /* load the private keys, if we're supposed to have them, and set up the
1426 * TLS context. */
1427 if (! identity_key_is_set()) {
1428 if (init_keys() < 0) {
1429 log_err(LD_BUG,"Error initializing keys; exiting");
1430 return -1;
1434 /* Set up the packed_cell_t memory pool. */
1435 init_cell_pool();
1437 /* Set up our buckets */
1438 connection_bucket_init();
1439 stats_prev_global_read_bucket = global_read_bucket;
1440 stats_prev_global_write_bucket = global_write_bucket;
1442 /* initialize the bootstrap status events to know we're starting up */
1443 control_event_bootstrap(BOOTSTRAP_STATUS_STARTING, 0);
1445 if (trusted_dirs_reload_certs()) {
1446 log_warn(LD_DIR,
1447 "Couldn't load all cached v3 certificates. Starting anyway.");
1449 if (router_reload_v2_networkstatus()) {
1450 return -1;
1452 if (router_reload_consensus_networkstatus()) {
1453 return -1;
1455 /* load the routers file, or assign the defaults. */
1456 if (router_reload_router_list()) {
1457 return -1;
1459 /* load the networkstatuses. (This launches a download for new routers as
1460 * appropriate.)
1462 now = time(NULL);
1463 directory_info_has_arrived(now, 1);
1465 if (authdir_mode_tests_reachability(get_options())) {
1466 /* the directory is already here, run startup things */
1467 dirserv_test_reachability(now, 1);
1470 if (server_mode(get_options())) {
1471 /* launch cpuworkers. Need to do this *after* we've read the onion key. */
1472 cpu_init();
1475 /* set up once-a-second callback. */
1476 second_elapsed_callback(0,0,NULL);
1478 for (;;) {
1479 if (nt_service_is_stopping())
1480 return 0;
1482 #ifndef MS_WINDOWS
1483 /* Make it easier to tell whether libevent failure is our fault or not. */
1484 errno = 0;
1485 #endif
1486 /* All active linked conns should get their read events activated. */
1487 SMARTLIST_FOREACH(active_linked_connection_lst, connection_t *, conn,
1488 event_active(conn->read_event, EV_READ, 1));
1489 called_loop_once = smartlist_len(active_linked_connection_lst) ? 1 : 0;
1491 update_approx_time(time(NULL));
1493 /* poll until we have an event, or the second ends, or until we have
1494 * some active linked connections to trigger events for. */
1495 loop_result = event_base_loop(tor_libevent_get_base(),
1496 called_loop_once ? EVLOOP_ONCE : 0);
1498 /* let catch() handle things like ^c, and otherwise don't worry about it */
1499 if (loop_result < 0) {
1500 int e = tor_socket_errno(-1);
1501 /* let the program survive things like ^z */
1502 if (e != EINTR && !ERRNO_IS_EINPROGRESS(e)) {
1503 log_err(LD_NET,"libevent call with %s failed: %s [%d]",
1504 tor_libevent_get_method(), tor_socket_strerror(e), e);
1505 return -1;
1506 #ifndef MS_WINDOWS
1507 } else if (e == EINVAL) {
1508 log_warn(LD_NET, "EINVAL from libevent: should you upgrade libevent?");
1509 if (got_libevent_error())
1510 return -1;
1511 #endif
1512 } else {
1513 if (ERRNO_IS_EINPROGRESS(e))
1514 log_warn(LD_BUG,
1515 "libevent call returned EINPROGRESS? Please report.");
1516 log_debug(LD_NET,"libevent call interrupted.");
1517 /* You can't trust the results of this poll(). Go back to the
1518 * top of the big for loop. */
1519 continue;
1525 /** Used to implement the SIGNAL control command: if we accept
1526 * <b>the_signal</b> as a remote pseudo-signal, act on it. */
1527 /* We don't re-use catch() here because:
1528 * 1. We handle a different set of signals than those allowed in catch.
1529 * 2. Platforms without signal() are unlikely to define SIGfoo.
1530 * 3. The control spec is defined to use fixed numeric signal values
1531 * which just happen to match the Unix values.
1533 void
1534 control_signal_act(int the_signal)
1536 switch (the_signal)
1538 case 1:
1539 signal_callback(0,0,(void*)(uintptr_t)SIGHUP);
1540 break;
1541 case 2:
1542 signal_callback(0,0,(void*)(uintptr_t)SIGINT);
1543 break;
1544 case 10:
1545 signal_callback(0,0,(void*)(uintptr_t)SIGUSR1);
1546 break;
1547 case 12:
1548 signal_callback(0,0,(void*)(uintptr_t)SIGUSR2);
1549 break;
1550 case 15:
1551 signal_callback(0,0,(void*)(uintptr_t)SIGTERM);
1552 break;
1553 case SIGNEWNYM:
1554 signal_callback(0,0,(void*)(uintptr_t)SIGNEWNYM);
1555 break;
1556 case SIGCLEARDNSCACHE:
1557 signal_callback(0,0,(void*)(uintptr_t)SIGCLEARDNSCACHE);
1558 break;
1559 default:
1560 log_warn(LD_BUG, "Unrecognized signal number %d.", the_signal);
1561 break;
1565 /** Libevent callback: invoked when we get a signal.
1567 static void
1568 signal_callback(int fd, short events, void *arg)
1570 uintptr_t sig = (uintptr_t)arg;
1571 (void)fd;
1572 (void)events;
1573 switch (sig)
1575 case SIGTERM:
1576 log_notice(LD_GENERAL,"Catching signal TERM, exiting cleanly.");
1577 tor_cleanup();
1578 exit(0);
1579 break;
1580 case SIGINT:
1581 if (!server_mode(get_options())) { /* do it now */
1582 log_notice(LD_GENERAL,"Interrupt: exiting cleanly.");
1583 tor_cleanup();
1584 exit(0);
1586 hibernate_begin_shutdown();
1587 break;
1588 #ifdef SIGPIPE
1589 case SIGPIPE:
1590 log_debug(LD_GENERAL,"Caught SIGPIPE. Ignoring.");
1591 break;
1592 #endif
1593 case SIGUSR1:
1594 /* prefer to log it at INFO, but make sure we always see it */
1595 dumpstats(get_min_log_level()<LOG_INFO ? get_min_log_level() : LOG_INFO);
1596 break;
1597 case SIGUSR2:
1598 switch_logs_debug();
1599 log_debug(LD_GENERAL,"Caught USR2, going to loglevel debug. "
1600 "Send HUP to change back.");
1601 break;
1602 case SIGHUP:
1603 if (do_hup() < 0) {
1604 log_warn(LD_CONFIG,"Restart failed (config error?). Exiting.");
1605 tor_cleanup();
1606 exit(1);
1608 break;
1609 #ifdef SIGCHLD
1610 case SIGCHLD:
1611 while (waitpid(-1,NULL,WNOHANG) > 0) ; /* keep reaping until no more
1612 zombies */
1613 break;
1614 #endif
1615 case SIGNEWNYM: {
1616 time_t now = time(NULL);
1617 if (time_of_last_signewnym + MAX_SIGNEWNYM_RATE > now) {
1618 signewnym_is_pending = 1;
1619 log(LOG_NOTICE, LD_CONTROL,
1620 "Rate limiting NEWNYM request: delaying by %d second(s)",
1621 (int)(MAX_SIGNEWNYM_RATE+time_of_last_signewnym-now));
1622 } else {
1623 signewnym_impl(now);
1625 break;
1627 case SIGCLEARDNSCACHE:
1628 addressmap_clear_transient();
1629 break;
1633 extern uint64_t rephist_total_alloc;
1634 extern uint32_t rephist_total_num;
1637 * Write current memory usage information to the log.
1639 static void
1640 dumpmemusage(int severity)
1642 connection_dump_buffer_mem_stats(severity);
1643 log(severity, LD_GENERAL, "In rephist: "U64_FORMAT" used by %d Tors.",
1644 U64_PRINTF_ARG(rephist_total_alloc), rephist_total_num);
1645 dump_routerlist_mem_usage(severity);
1646 dump_cell_pool_usage(severity);
1647 dump_dns_mem_usage(severity);
1648 buf_dump_freelist_sizes(severity);
1649 tor_log_mallinfo(severity);
1652 /** Write all statistics to the log, with log level 'severity'. Called
1653 * in response to a SIGUSR1. */
1654 static void
1655 dumpstats(int severity)
1657 time_t now = time(NULL);
1658 time_t elapsed;
1659 size_t rbuf_cap, wbuf_cap, rbuf_len, wbuf_len;
1661 log(severity, LD_GENERAL, "Dumping stats:");
1663 SMARTLIST_FOREACH(connection_array, connection_t *, conn,
1665 int i = conn_sl_idx;
1666 log(severity, LD_GENERAL,
1667 "Conn %d (socket %d) type %d (%s), state %d (%s), created %d secs ago",
1668 i, conn->s, conn->type, conn_type_to_string(conn->type),
1669 conn->state, conn_state_to_string(conn->type, conn->state),
1670 (int)(now - conn->timestamp_created));
1671 if (!connection_is_listener(conn)) {
1672 log(severity,LD_GENERAL,
1673 "Conn %d is to %s:%d.", i,
1674 safe_str_client(conn->address),
1675 conn->port);
1676 log(severity,LD_GENERAL,
1677 "Conn %d: %d bytes waiting on inbuf (len %d, last read %d secs ago)",
1679 (int)buf_datalen(conn->inbuf),
1680 (int)buf_allocation(conn->inbuf),
1681 (int)(now - conn->timestamp_lastread));
1682 log(severity,LD_GENERAL,
1683 "Conn %d: %d bytes waiting on outbuf "
1684 "(len %d, last written %d secs ago)",i,
1685 (int)buf_datalen(conn->outbuf),
1686 (int)buf_allocation(conn->outbuf),
1687 (int)(now - conn->timestamp_lastwritten));
1688 if (conn->type == CONN_TYPE_OR) {
1689 or_connection_t *or_conn = TO_OR_CONN(conn);
1690 if (or_conn->tls) {
1691 tor_tls_get_buffer_sizes(or_conn->tls, &rbuf_cap, &rbuf_len,
1692 &wbuf_cap, &wbuf_len);
1693 log(severity, LD_GENERAL,
1694 "Conn %d: %d/%d bytes used on OpenSSL read buffer; "
1695 "%d/%d bytes used on write buffer.",
1696 i, (int)rbuf_len, (int)rbuf_cap, (int)wbuf_len, (int)wbuf_cap);
1700 circuit_dump_by_conn(conn, severity); /* dump info about all the circuits
1701 * using this conn */
1703 log(severity, LD_NET,
1704 "Cells processed: "U64_FORMAT" padding\n"
1705 " "U64_FORMAT" create\n"
1706 " "U64_FORMAT" created\n"
1707 " "U64_FORMAT" relay\n"
1708 " ("U64_FORMAT" relayed)\n"
1709 " ("U64_FORMAT" delivered)\n"
1710 " "U64_FORMAT" destroy",
1711 U64_PRINTF_ARG(stats_n_padding_cells_processed),
1712 U64_PRINTF_ARG(stats_n_create_cells_processed),
1713 U64_PRINTF_ARG(stats_n_created_cells_processed),
1714 U64_PRINTF_ARG(stats_n_relay_cells_processed),
1715 U64_PRINTF_ARG(stats_n_relay_cells_relayed),
1716 U64_PRINTF_ARG(stats_n_relay_cells_delivered),
1717 U64_PRINTF_ARG(stats_n_destroy_cells_processed));
1718 if (stats_n_data_cells_packaged)
1719 log(severity,LD_NET,"Average packaged cell fullness: %2.3f%%",
1720 100*(U64_TO_DBL(stats_n_data_bytes_packaged) /
1721 U64_TO_DBL(stats_n_data_cells_packaged*RELAY_PAYLOAD_SIZE)) );
1722 if (stats_n_data_cells_received)
1723 log(severity,LD_NET,"Average delivered cell fullness: %2.3f%%",
1724 100*(U64_TO_DBL(stats_n_data_bytes_received) /
1725 U64_TO_DBL(stats_n_data_cells_received*RELAY_PAYLOAD_SIZE)) );
1727 if (now - time_of_process_start >= 0)
1728 elapsed = now - time_of_process_start;
1729 else
1730 elapsed = 0;
1732 if (elapsed) {
1733 log(severity, LD_NET,
1734 "Average bandwidth: "U64_FORMAT"/%d = %d bytes/sec reading",
1735 U64_PRINTF_ARG(stats_n_bytes_read),
1736 (int)elapsed,
1737 (int) (stats_n_bytes_read/elapsed));
1738 log(severity, LD_NET,
1739 "Average bandwidth: "U64_FORMAT"/%d = %d bytes/sec writing",
1740 U64_PRINTF_ARG(stats_n_bytes_written),
1741 (int)elapsed,
1742 (int) (stats_n_bytes_written/elapsed));
1745 log(severity, LD_NET, "--------------- Dumping memory information:");
1746 dumpmemusage(severity);
1748 rep_hist_dump_stats(now,severity);
1749 rend_service_dump_stats(severity);
1750 dump_pk_ops(severity);
1751 dump_distinct_digest_count(severity);
1754 /** Called by exit() as we shut down the process.
1756 static void
1757 exit_function(void)
1759 /* NOTE: If we ever daemonize, this gets called immediately. That's
1760 * okay for now, because we only use this on Windows. */
1761 #ifdef MS_WINDOWS
1762 WSACleanup();
1763 #endif
1766 /** Set up the signal handlers for either parent or child. */
1767 void
1768 handle_signals(int is_parent)
1770 #ifndef MS_WINDOWS /* do signal stuff only on Unix */
1771 int i;
1772 static const int signals[] = {
1773 SIGINT, /* do a controlled slow shutdown */
1774 SIGTERM, /* to terminate now */
1775 SIGPIPE, /* otherwise SIGPIPE kills us */
1776 SIGUSR1, /* dump stats */
1777 SIGUSR2, /* go to loglevel debug */
1778 SIGHUP, /* to reload config, retry conns, etc */
1779 #ifdef SIGXFSZ
1780 SIGXFSZ, /* handle file-too-big resource exhaustion */
1781 #endif
1782 SIGCHLD, /* handle dns/cpu workers that exit */
1783 -1 };
1784 static struct event *signal_events[16]; /* bigger than it has to be. */
1785 if (is_parent) {
1786 for (i = 0; signals[i] >= 0; ++i) {
1787 signal_events[i] = tor_evsignal_new(
1788 tor_libevent_get_base(), signals[i], signal_callback,
1789 (void*)(uintptr_t)signals[i]);
1790 if (event_add(signal_events[i], NULL))
1791 log_warn(LD_BUG, "Error from libevent when adding event for signal %d",
1792 signals[i]);
1794 } else {
1795 struct sigaction action;
1796 action.sa_flags = 0;
1797 sigemptyset(&action.sa_mask);
1798 action.sa_handler = SIG_IGN;
1799 sigaction(SIGINT, &action, NULL);
1800 sigaction(SIGTERM, &action, NULL);
1801 sigaction(SIGPIPE, &action, NULL);
1802 sigaction(SIGUSR1, &action, NULL);
1803 sigaction(SIGUSR2, &action, NULL);
1804 sigaction(SIGHUP, &action, NULL);
1805 #ifdef SIGXFSZ
1806 sigaction(SIGXFSZ, &action, NULL);
1807 #endif
1809 #else /* MS windows */
1810 (void)is_parent;
1811 #endif /* signal stuff */
1814 /** Main entry point for the Tor command-line client.
1816 /* static */ int
1817 tor_init(int argc, char *argv[])
1819 char buf[256];
1820 int i, quiet = 0;
1821 time_of_process_start = time(NULL);
1822 if (!connection_array)
1823 connection_array = smartlist_create();
1824 if (!closeable_connection_lst)
1825 closeable_connection_lst = smartlist_create();
1826 if (!active_linked_connection_lst)
1827 active_linked_connection_lst = smartlist_create();
1828 /* Have the log set up with our application name. */
1829 tor_snprintf(buf, sizeof(buf), "Tor %s", get_version());
1830 log_set_application_name(buf);
1831 /* Initialize the history structures. */
1832 rep_hist_init();
1833 /* Initialize the service cache. */
1834 rend_cache_init();
1835 addressmap_init(); /* Init the client dns cache. Do it always, since it's
1836 * cheap. */
1838 /* We search for the "quiet" option first, since it decides whether we
1839 * will log anything at all to the command line. */
1840 for (i=1;i<argc;++i) {
1841 if (!strcmp(argv[i], "--hush"))
1842 quiet = 1;
1843 if (!strcmp(argv[i], "--quiet"))
1844 quiet = 2;
1846 /* give it somewhere to log to initially */
1847 switch (quiet) {
1848 case 2:
1849 /* no initial logging */
1850 break;
1851 case 1:
1852 add_temp_log(LOG_WARN);
1853 break;
1854 default:
1855 add_temp_log(LOG_NOTICE);
1858 log(LOG_NOTICE, LD_GENERAL, "Tor v%s. This is experimental software. "
1859 "Do not rely on it for strong anonymity. (Running on %s)",get_version(),
1860 get_uname());
1862 if (network_init()<0) {
1863 log_err(LD_BUG,"Error initializing network; exiting.");
1864 return -1;
1866 atexit(exit_function);
1868 if (options_init_from_torrc(argc,argv) < 0) {
1869 log_err(LD_CONFIG,"Reading config failed--see warnings above.");
1870 return -1;
1873 #ifndef MS_WINDOWS
1874 if (geteuid()==0)
1875 log_warn(LD_GENERAL,"You are running Tor as root. You don't need to, "
1876 "and you probably shouldn't.");
1877 #endif
1879 if (crypto_global_init(get_options()->HardwareAccel,
1880 get_options()->AccelName,
1881 get_options()->AccelDir)) {
1882 log_err(LD_BUG, "Unable to initialize OpenSSL. Exiting.");
1883 return -1;
1886 return 0;
1889 /** A lockfile structure, used to prevent two Tors from messing with the
1890 * data directory at once. If this variable is non-NULL, we're holding
1891 * the lockfile. */
1892 static tor_lockfile_t *lockfile = NULL;
1894 /** Try to grab the lock file described in <b>options</b>, if we do not
1895 * already have it. If <b>err_if_locked</b> is true, warn if somebody else is
1896 * holding the lock, and exit if we can't get it after waiting. Otherwise,
1897 * return -1 if we can't get the lockfile. Return 0 on success.
1900 try_locking(or_options_t *options, int err_if_locked)
1902 if (lockfile)
1903 return 0;
1904 else {
1905 char *fname = options_get_datadir_fname2_suffix(options, "lock",NULL,NULL);
1906 int already_locked = 0;
1907 tor_lockfile_t *lf = tor_lockfile_lock(fname, 0, &already_locked);
1908 tor_free(fname);
1909 if (!lf) {
1910 if (err_if_locked && already_locked) {
1911 int r;
1912 log_warn(LD_GENERAL, "It looks like another Tor process is running "
1913 "with the same data directory. Waiting 5 seconds to see "
1914 "if it goes away.");
1915 #ifndef WIN32
1916 sleep(5);
1917 #else
1918 Sleep(5000);
1919 #endif
1920 r = try_locking(options, 0);
1921 if (r<0) {
1922 log_err(LD_GENERAL, "No, it's still there. Exiting.");
1923 exit(0);
1925 return r;
1927 return -1;
1929 lockfile = lf;
1930 return 0;
1934 /** Return true iff we've successfully acquired the lock file. */
1936 have_lockfile(void)
1938 return lockfile != NULL;
1941 /** If we have successfully acquired the lock file, release it. */
1942 void
1943 release_lockfile(void)
1945 if (lockfile) {
1946 tor_lockfile_unlock(lockfile);
1947 lockfile = NULL;
1951 /** Free all memory that we might have allocated somewhere.
1952 * If <b>postfork</b>, we are a worker process and we want to free
1953 * only the parts of memory that we won't touch. If !<b>postfork</b>,
1954 * Tor is shutting down and we should free everything.
1956 * Helps us find the real leaks with dmalloc and the like. Also valgrind
1957 * should then report 0 reachable in its leak report (in an ideal world --
1958 * in practice libevent, SSL, libc etc never quite free everything). */
1959 void
1960 tor_free_all(int postfork)
1962 if (!postfork) {
1963 evdns_shutdown(1);
1965 geoip_free_all();
1966 dirvote_free_all();
1967 routerlist_free_all();
1968 networkstatus_free_all();
1969 addressmap_free_all();
1970 dirserv_free_all();
1971 rend_service_free_all();
1972 rend_cache_free_all();
1973 rend_service_authorization_free_all();
1974 rep_hist_free_all();
1975 dns_free_all();
1976 clear_pending_onions();
1977 circuit_free_all();
1978 entry_guards_free_all();
1979 connection_free_all();
1980 buf_shrink_freelists(1);
1981 memarea_clear_freelist();
1982 microdesc_free_all();
1983 if (!postfork) {
1984 config_free_all();
1985 router_free_all();
1986 policies_free_all();
1988 free_cell_pool();
1989 if (!postfork) {
1990 tor_tls_free_all();
1992 /* stuff in main.c */
1994 smartlist_free(connection_array);
1995 smartlist_free(closeable_connection_lst);
1996 smartlist_free(active_linked_connection_lst);
1997 tor_free(timeout_event);
1998 if (!postfork) {
1999 release_lockfile();
2001 /* Stuff in util.c and address.c*/
2002 if (!postfork) {
2003 escaped(NULL);
2004 esc_router_info(NULL);
2005 logs_free_all(); /* free log strings. do this last so logs keep working. */
2009 /** Do whatever cleanup is necessary before shutting Tor down. */
2010 void
2011 tor_cleanup(void)
2013 or_options_t *options = get_options();
2014 /* Remove our pid file. We don't care if there was an error when we
2015 * unlink, nothing we could do about it anyways. */
2016 if (options->command == CMD_RUN_TOR) {
2017 time_t now = time(NULL);
2018 if (options->PidFile)
2019 unlink(options->PidFile);
2020 if (accounting_is_enabled(options))
2021 accounting_record_bandwidth_usage(now, get_or_state());
2022 or_state_mark_dirty(get_or_state(), 0); /* force an immediate save. */
2023 or_state_save(now);
2024 if (authdir_mode_tests_reachability(options))
2025 rep_hist_record_mtbf_data(now, 0);
2027 #ifdef USE_DMALLOC
2028 dmalloc_log_stats();
2029 #endif
2030 tor_free_all(0); /* We could move tor_free_all back into the ifdef below
2031 later, if it makes shutdown unacceptably slow. But for
2032 now, leave it here: it's helped us catch bugs in the
2033 past. */
2034 crypto_global_cleanup();
2035 #ifdef USE_DMALLOC
2036 dmalloc_log_unfreed();
2037 dmalloc_shutdown();
2038 #endif
2041 /** Read/create keys as needed, and echo our fingerprint to stdout. */
2042 /* static */ int
2043 do_list_fingerprint(void)
2045 char buf[FINGERPRINT_LEN+1];
2046 crypto_pk_env_t *k;
2047 const char *nickname = get_options()->Nickname;
2048 if (!server_mode(get_options())) {
2049 log_err(LD_GENERAL,
2050 "Clients don't have long-term identity keys. Exiting.\n");
2051 return -1;
2053 tor_assert(nickname);
2054 if (init_keys() < 0) {
2055 log_err(LD_BUG,"Error initializing keys; can't display fingerprint");
2056 return -1;
2058 if (!(k = get_identity_key())) {
2059 log_err(LD_GENERAL,"Error: missing identity key.");
2060 return -1;
2062 if (crypto_pk_get_fingerprint(k, buf, 1)<0) {
2063 log_err(LD_BUG, "Error computing fingerprint");
2064 return -1;
2066 printf("%s %s\n", nickname, buf);
2067 return 0;
2070 /** Entry point for password hashing: take the desired password from
2071 * the command line, and print its salted hash to stdout. **/
2072 /* static */ void
2073 do_hash_password(void)
2076 char output[256];
2077 char key[S2K_SPECIFIER_LEN+DIGEST_LEN];
2079 crypto_rand(key, S2K_SPECIFIER_LEN-1);
2080 key[S2K_SPECIFIER_LEN-1] = (uint8_t)96; /* Hash 64 K of data. */
2081 secret_to_key(key+S2K_SPECIFIER_LEN, DIGEST_LEN,
2082 get_options()->command_arg, strlen(get_options()->command_arg),
2083 key);
2084 base16_encode(output, sizeof(output), key, sizeof(key));
2085 printf("16:%s\n",output);
2088 /** Main entry point for the Tor process. Called from main(). */
2089 /* This function is distinct from main() only so we can link main.c into
2090 * the unittest binary without conflicting with the unittests' main. */
2092 tor_main(int argc, char *argv[])
2094 int result = 0;
2095 update_approx_time(time(NULL));
2096 tor_threads_init();
2097 init_logging();
2098 #ifdef USE_DMALLOC
2100 /* Instruct OpenSSL to use our internal wrappers for malloc,
2101 realloc and free. */
2102 int r = CRYPTO_set_mem_ex_functions(_tor_malloc, _tor_realloc, _tor_free);
2103 tor_assert(r);
2105 #endif
2106 #ifdef NT_SERVICE
2108 int done = 0;
2109 result = nt_service_parse_options(argc, argv, &done);
2110 if (done) return result;
2112 #endif
2113 if (tor_init(argc, argv)<0)
2114 return -1;
2115 switch (get_options()->command) {
2116 case CMD_RUN_TOR:
2117 #ifdef NT_SERVICE
2118 nt_service_set_state(SERVICE_RUNNING);
2119 #endif
2120 result = do_main_loop();
2121 break;
2122 case CMD_LIST_FINGERPRINT:
2123 result = do_list_fingerprint();
2124 break;
2125 case CMD_HASH_PASSWORD:
2126 do_hash_password();
2127 result = 0;
2128 break;
2129 case CMD_VERIFY_CONFIG:
2130 printf("Configuration was valid\n");
2131 result = 0;
2132 break;
2133 case CMD_RUN_UNITTESTS: /* only set by test.c */
2134 default:
2135 log_warn(LD_BUG,"Illegal command number %d: internal error.",
2136 get_options()->command);
2137 result = -1;
2139 tor_cleanup();
2140 return result;