Allow a few EINVALs from libevent before dying. Warn on kqueue with libevent before...
[tor.git] / src / or / main.c
blobe855c06ee1d94780cc2e9875beb09dc80b9ba0f4
1 /* Copyright 2001 Matej Pfajfar.
2 * Copyright 2001-2004 Roger Dingledine.
3 * Copyright 2004-2005 Roger Dingledine, Nick Mathewson. */
4 /* See LICENSE for licensing information */
5 /* $Id$ */
6 const char main_c_id[] = "$Id$";
8 /**
9 * \file main.c
10 * \brief Toplevel module. Handles signals, multiplexes between
11 * connections, implements main loop, and drives scheduled events.
12 **/
14 #include "or.h"
15 #ifdef USE_DMALLOC
16 #include <dmalloc.h>
17 #endif
19 /* These signals are defined to help control_signal_act work. */
20 #ifndef SIGHUP
21 #define SIGHUP 1
22 #endif
23 #ifndef SIGINT
24 #define SIGINT 2
25 #endif
26 #ifndef SIGUSR1
27 #define SIGUSR1 10
28 #endif
29 #ifndef SIGUSR2
30 #define SIGUSR2 12
31 #endif
32 #ifndef SIGTERM
33 #define SIGTERM 15
34 #endif
36 /********* PROTOTYPES **********/
38 static void dumpmemusage(int severity);
39 static void dumpstats(int severity); /* log stats */
40 static void conn_read_callback(int fd, short event, void *_conn);
41 static void conn_write_callback(int fd, short event, void *_conn);
42 static void signal_callback(int fd, short events, void *arg);
43 static void second_elapsed_callback(int fd, short event, void *args);
44 static int conn_close_if_marked(int i);
46 /********* START VARIABLES **********/
48 int global_read_bucket; /**< Max number of bytes I can read this second. */
49 int global_write_bucket; /**< Max number of bytes I can write this second. */
51 /** What was the read bucket before the last call to prepare_for_pool?
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 call to prepare_for_pool?
55 * (used to determine how many bytes we've written). */
56 static int stats_prev_global_write_bucket;
57 /** How many bytes have we read/written since we started the process? */
58 static uint64_t stats_n_bytes_read = 0;
59 static uint64_t stats_n_bytes_written = 0;
60 /** What time did this process start up? */
61 long time_of_process_start = 0;
62 /** How many seconds have we been running? */
63 long stats_n_seconds_working = 0;
64 /** When do we next download a directory? */
65 static time_t time_to_fetch_directory = 0;
66 /** When do we next upload our descriptor? */
67 static time_t time_to_force_upload_descriptor = 0;
68 /** When do we next download a running-routers summary? */
69 static time_t time_to_fetch_running_routers = 0;
71 /** Array of all open connections; each element corresponds to the element of
72 * poll_array in the same position. The first nfds elements are valid. */
73 static connection_t *connection_array[MAXCONNECTIONS+1] =
74 { NULL };
75 static smartlist_t *closeable_connection_lst = NULL;
77 static int nfds=0; /**< Number of connections currently active. */
79 /** We set this to 1 when we've fetched a dir, to know whether to complain
80 * yet about unrecognized nicknames in entrynodes, exitnodes, etc.
81 * Also, we don't try building circuits unless this is 1. */
82 int has_fetched_directory=0;
84 /** We set this to 1 when we've opened a circuit, so we can print a log
85 * entry to inform the user that Tor is working. */
86 int has_completed_circuit=0;
88 #ifdef MS_WINDOWS
89 #define MS_WINDOWS_SERVICE
90 #endif
92 #ifdef MS_WINDOWS_SERVICE
93 #include <tchar.h>
94 #define GENSRV_SERVICENAME TEXT("tor")
95 #define GENSRV_DISPLAYNAME TEXT("Tor Win32 Service")
96 #define GENSRV_DESCRIPTION TEXT("Provides an anonymous Internet communication system")
97 SERVICE_STATUS service_status;
98 SERVICE_STATUS_HANDLE hStatus;
99 static char **backup_argv;
100 static int backup_argc;
101 static int nt_service_is_stopped(void);
102 #else
103 #define nt_service_is_stopped() (0)
104 #endif
106 #define CHECK_DESCRIPTOR_INTERVAL 60 /* one minute */
107 #define BUF_SHRINK_INTERVAL 60 /* one minute */
108 #define TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT (20*60) /* 20 minutes */
110 /********* END VARIABLES ************/
112 /****************************************************************************
114 * This section contains accessors and other methods on the connection_array
115 * and poll_array variables (which are global within this file and unavailable
116 * outside it).
118 ****************************************************************************/
120 /** Add <b>conn</b> to the array of connections that we can poll on. The
121 * connection's socket must be set; the connection starts out
122 * non-reading and non-writing.
125 connection_add(connection_t *conn)
127 tor_assert(conn);
128 tor_assert(conn->s >= 0);
130 if (nfds >= get_options()->_ConnLimit-1) {
131 log_fn(LOG_WARN,"Failing because we have %d connections already. Please raise your ulimit -n.", nfds);
132 return -1;
135 tor_assert(conn->poll_index == -1); /* can only connection_add once */
136 conn->poll_index = nfds;
137 connection_array[nfds] = conn;
139 conn->read_event = tor_malloc_zero(sizeof(struct event));
140 conn->write_event = tor_malloc_zero(sizeof(struct event));
141 event_set(conn->read_event, conn->s, EV_READ|EV_PERSIST,
142 conn_read_callback, conn);
143 event_set(conn->write_event, conn->s, EV_WRITE|EV_PERSIST,
144 conn_write_callback, conn);
146 nfds++;
148 log_fn(LOG_INFO,"new conn type %s, socket %d, nfds %d.",
149 conn_type_to_string(conn->type), conn->s, nfds);
151 return 0;
154 /** Remove the connection from the global list, and remove the
155 * corresponding poll entry. Calling this function will shift the last
156 * connection (if any) into the position occupied by conn.
159 connection_remove(connection_t *conn)
161 int current_index;
163 tor_assert(conn);
164 tor_assert(nfds>0);
166 log_fn(LOG_INFO,"removing socket %d (type %s), nfds now %d",
167 conn->s, conn_type_to_string(conn->type), nfds-1);
169 tor_assert(conn->poll_index >= 0);
170 current_index = conn->poll_index;
171 if (current_index == nfds-1) { /* this is the end */
172 nfds--;
173 return 0;
176 connection_unregister(conn);
178 /* replace this one with the one at the end */
179 nfds--;
180 connection_array[current_index] = connection_array[nfds];
181 connection_array[current_index]->poll_index = current_index;
183 return 0;
186 /** If it's an edge conn, remove it from the list
187 * of conn's on this circuit. If it's not on an edge,
188 * flush and send destroys for all circuits on this conn.
190 * If <b>remove</b> is non-zero, then remove it from the
191 * connection_array and closeable_connection_lst.
193 * Then free it.
195 static void
196 connection_unlink(connection_t *conn, int remove)
198 circuit_about_to_close_connection(conn);
199 connection_about_to_close_connection(conn);
200 if (remove) {
201 connection_remove(conn);
203 smartlist_remove(closeable_connection_lst, conn);
204 if (conn->type == CONN_TYPE_EXIT) {
205 assert_connection_edge_not_dns_pending(conn);
207 connection_free(conn);
210 /** Schedule <b>conn</b> to be closed. **/
211 void
212 add_connection_to_closeable_list(connection_t *conn)
214 tor_assert(!smartlist_isin(closeable_connection_lst, conn));
215 tor_assert(conn->marked_for_close);
216 assert_connection_ok(conn, time(NULL));
217 smartlist_add(closeable_connection_lst, conn);
220 /** Return 1 if conn is on the closeable list, else return 0. */
222 connection_is_on_closeable_list(connection_t *conn)
224 return smartlist_isin(closeable_connection_lst, conn);
227 /** Return true iff conn is in the current poll array. */
229 connection_in_array(connection_t *conn)
231 int i;
232 for (i=0; i<nfds; ++i) {
233 if (conn==connection_array[i])
234 return 1;
236 return 0;
239 /** Set <b>*array</b> to an array of all connections, and <b>*n</b>
240 * to the length of the array. <b>*array</b> and <b>*n</b> must not
241 * be modified.
243 void
244 get_connection_array(connection_t ***array, int *n)
246 *array = connection_array;
247 *n = nfds;
250 /** Set the event mask on <b>conn</b> to <b>events</b>. (The event
251 * mask is a bitmask whose bits are EV_READ and EV_WRITE.)
253 void
254 connection_watch_events(connection_t *conn, short events)
256 int r;
258 tor_assert(conn);
259 tor_assert(conn->read_event);
260 tor_assert(conn->write_event);
262 if (events & EV_READ) {
263 r = event_add(conn->read_event, NULL);
264 } else {
265 r = event_del(conn->read_event);
268 if (r<0)
269 log_fn(LOG_WARN,
270 "Error from libevent setting read event state for %d to %swatched.",
271 conn->s, (events & EV_READ)?"":"un");
273 if (events & EV_WRITE) {
274 r = event_add(conn->write_event, NULL);
275 } else {
276 r = event_del(conn->write_event);
279 if (r<0)
280 log_fn(LOG_WARN,
281 "Error from libevent setting read event state for %d to %swatched.",
282 conn->s, (events & EV_WRITE)?"":"un");
285 /** Return true iff <b>conn</b> is listening for read events. */
287 connection_is_reading(connection_t *conn)
289 tor_assert(conn);
291 return conn->read_event && event_pending(conn->read_event, EV_READ, NULL);
294 /** Tell the main loop to stop notifying <b>conn</b> of any read events. */
295 void
296 connection_stop_reading(connection_t *conn)
298 tor_assert(conn);
299 tor_assert(conn->read_event);
301 log(LOG_DEBUG,"connection_stop_reading() called.");
302 if (event_del(conn->read_event))
303 log_fn(LOG_WARN, "Error from libevent setting read event state for %d to unwatched.",
304 conn->s);
307 /** Tell the main loop to start notifying <b>conn</b> of any read events. */
308 void
309 connection_start_reading(connection_t *conn)
311 tor_assert(conn);
312 tor_assert(conn->read_event);
314 if (event_add(conn->read_event, NULL))
315 log_fn(LOG_WARN, "Error from libevent setting read event state for %d to watched.",
316 conn->s);
319 /** Return true iff <b>conn</b> is listening for write events. */
321 connection_is_writing(connection_t *conn)
323 tor_assert(conn);
325 return conn->write_event && event_pending(conn->write_event, EV_WRITE, NULL);
328 /** Tell the main loop to stop notifying <b>conn</b> of any write events. */
329 void
330 connection_stop_writing(connection_t *conn)
332 tor_assert(conn);
333 tor_assert(conn->write_event);
335 if (event_del(conn->write_event))
336 log_fn(LOG_WARN, "Error from libevent setting write event state for %d to unwatched.",
337 conn->s);
341 /** Tell the main loop to start notifying <b>conn</b> of any write events. */
342 void
343 connection_start_writing(connection_t *conn)
345 tor_assert(conn);
346 tor_assert(conn->write_event);
348 if (event_add(conn->write_event, NULL))
349 log_fn(LOG_WARN, "Error from libevent setting write event state for %d to watched.",
350 conn->s);
353 /** Close all connections that have been scheduled to get closed */
354 static void
355 close_closeable_connections(void)
357 int i;
358 for (i = 0; i < smartlist_len(closeable_connection_lst); ) {
359 connection_t *conn = smartlist_get(closeable_connection_lst, i);
360 if (conn->poll_index < 0) {
361 connection_unlink(conn, 0); /* blow it away right now */
362 } else {
363 if (!conn_close_if_marked(conn->poll_index))
364 ++i;
369 /** Libevent callback: this gets invoked when (connection_t*)<b>conn</b> has
370 * some data to read. */
371 static void
372 conn_read_callback(int fd, short event, void *_conn)
374 connection_t *conn = _conn;
376 log_fn(LOG_DEBUG,"socket %d wants to read.",conn->s);
378 assert_connection_ok(conn, time(NULL));
380 if (connection_handle_read(conn) < 0) {
381 if (!conn->marked_for_close) {
382 #ifndef MS_WINDOWS
383 log_fn(LOG_WARN,"Bug: unhandled error on read for %s connection (fd %d); removing",
384 conn_type_to_string(conn->type), conn->s);
385 tor_fragile_assert();
386 #endif
387 if (CONN_IS_EDGE(conn))
388 connection_edge_end_errno(conn, conn->cpath_layer);
389 connection_mark_for_close(conn);
392 assert_connection_ok(conn, time(NULL));
394 if (smartlist_len(closeable_connection_lst))
395 close_closeable_connections();
398 /** Libevent callback: this gets invoked when (connection_t*)<b>conn</b> has
399 * some data to write. */
400 static void
401 conn_write_callback(int fd, short events, void *_conn)
403 connection_t *conn = _conn;
405 log_fn(LOG_DEBUG,"socket %d wants to write.",conn->s);
407 assert_connection_ok(conn, time(NULL));
409 if (connection_handle_write(conn) < 0) {
410 if (!conn->marked_for_close) {
411 /* this connection is broken. remove it. */
412 log_fn(LOG_WARN,"Bug: unhandled error on write for %s connection (fd %d); removing",
413 conn_type_to_string(conn->type), conn->s);
414 tor_fragile_assert();
415 conn->has_sent_end = 1; /* otherwise we cry wolf about duplicate close */
416 /* XXX do we need a close-immediate here, so we don't try to flush? */
417 connection_mark_for_close(conn);
420 assert_connection_ok(conn, time(NULL));
422 if (smartlist_len(closeable_connection_lst))
423 close_closeable_connections();
426 /** If the connection at connection_array[i] is marked for close, then:
427 * - If it has data that it wants to flush, try to flush it.
428 * - If it _still_ has data to flush, and conn->hold_open_until_flushed is
429 * true, then leave the connection open and return.
430 * - Otherwise, remove the connection from connection_array and from
431 * all other lists, close it, and free it.
432 * Returns 1 if the connection was closed, 0 otherwise.
434 static int
435 conn_close_if_marked(int i)
437 connection_t *conn;
438 int retval;
440 conn = connection_array[i];
441 if (!conn->marked_for_close)
442 return 0; /* nothing to see here, move along */
443 assert_connection_ok(conn, time(NULL));
444 assert_all_pending_dns_resolves_ok();
446 log_fn(LOG_INFO,"Cleaning up connection (fd %d).",conn->s);
447 if (conn->s >= 0 && connection_wants_to_flush(conn)) {
448 /* -1 means it's an incomplete edge connection, or that the socket
449 * has already been closed as unflushable. */
450 if (!conn->hold_open_until_flushed)
451 log_fn(LOG_INFO,
452 "Conn (addr %s, fd %d, type %s, state %d) marked, but wants to flush %d bytes. "
453 "(Marked at %s:%d)",
454 conn->address, conn->s, conn_type_to_string(conn->type), conn->state,
455 (int)conn->outbuf_flushlen, conn->marked_for_close_file, conn->marked_for_close);
456 if (connection_speaks_cells(conn)) {
457 if (conn->state == OR_CONN_STATE_OPEN) {
458 retval = flush_buf_tls(conn->tls, conn->outbuf, &conn->outbuf_flushlen);
459 } else
460 retval = -1; /* never flush non-open broken tls connections */
461 } else {
462 retval = flush_buf(conn->s, conn->outbuf, &conn->outbuf_flushlen);
464 if (retval >= 0 &&
465 conn->hold_open_until_flushed && connection_wants_to_flush(conn)) {
466 log_fn(LOG_INFO,"Holding conn (fd %d) open for more flushing.",conn->s);
467 /* XXX should we reset timestamp_lastwritten here? */
468 return 0;
470 if (connection_wants_to_flush(conn)) {
471 log_fn(LOG_NOTICE,"Conn (addr %s, fd %d, type %s, state %d) is being closed, but there are still %d bytes we can't write. (Marked at %s:%d)",
472 safe_str(conn->address), conn->s, conn_type_to_string(conn->type),
473 conn->state,
474 (int)buf_datalen(conn->outbuf), conn->marked_for_close_file,
475 conn->marked_for_close);
478 connection_unlink(conn, 1); /* unlink, remove, free */
479 return 1;
482 /** We've just tried every dirserver we know about, and none of
483 * them were reachable. Assume the network is down. Change state
484 * so next time an application connection arrives we'll delay it
485 * and try another directory fetch. Kill off all the circuit_wait
486 * streams that are waiting now, since they will all timeout anyway.
488 void
489 directory_all_unreachable(time_t now)
491 connection_t *conn;
493 has_fetched_directory=0;
494 stats_n_seconds_working=0; /* reset it */
496 while ((conn = connection_get_by_type_state(CONN_TYPE_AP,
497 AP_CONN_STATE_CIRCUIT_WAIT))) {
498 log_fn(LOG_NOTICE,"Network down? Failing connection to '%s:%d'.",
499 safe_str(conn->socks_request->address), conn->socks_request->port);
500 connection_mark_unattached_ap(conn, END_STREAM_REASON_NET_UNREACHABLE);
505 * Return the interval to wait betweeen directory downloads, in seconds.
507 static INLINE int
508 get_dir_fetch_period(or_options_t *options)
510 if (options->DirFetchPeriod)
511 /* Value from config file. */
512 return options->DirFetchPeriod;
513 else if (options->DirPort)
514 /* Default for directory server */
515 return 20*60;
516 else
517 /* Default for average user. */
518 return 40*60;
522 * Return the interval to wait betweeen router status downloads, in seconds.
524 static INLINE int
525 get_status_fetch_period(or_options_t *options)
527 if (options->StatusFetchPeriod)
528 /* Value from config file. */
529 return options->StatusFetchPeriod;
530 else if (options->DirPort)
531 /* Default for directory server */
532 return 15*60;
533 else
534 /* Default for average user. */
535 return 30*60;
538 /** This function is called whenever we successfully pull down a directory.
539 * If <b>identity_digest</b> is defined, it contains the digest of the
540 * router that just gave us this directory. */
541 void
542 directory_has_arrived(time_t now, char *identity_digest)
544 or_options_t *options = get_options();
546 log_fn(LOG_INFO, "A directory has arrived.");
548 has_fetched_directory=1;
549 /* Don't try to upload or download anything for a while
550 * after the directory we had when we started.
552 if (!time_to_fetch_directory)
553 time_to_fetch_directory = now + get_dir_fetch_period(options);
555 if (!time_to_force_upload_descriptor)
556 time_to_force_upload_descriptor = now + options->DirPostPeriod;
558 if (!time_to_fetch_running_routers)
559 time_to_fetch_running_routers = now + get_status_fetch_period(options);
561 if (server_mode(options) && identity_digest) {
562 /* if this is us, then our dirport is reachable */
563 if (router_digest_is_me(identity_digest))
564 router_dirport_found_reachable();
567 if (server_mode(options) &&
568 !we_are_hibernating()) { /* connect to the appropriate routers */
569 router_retry_connections();
570 if (identity_digest) /* we got a fresh directory */
571 consider_testing_reachability();
575 /** Perform regular maintenance tasks for a single connection. This
576 * function gets run once per second per connection by run_scheduled_events.
578 static void
579 run_connection_housekeeping(int i, time_t now)
581 cell_t cell;
582 connection_t *conn = connection_array[i];
583 or_options_t *options = get_options();
585 if (conn->outbuf && !buf_datalen(conn->outbuf))
586 conn->timestamp_lastempty = now;
588 /* Expire any directory connections that haven't sent anything for 5 min */
589 if (conn->type == CONN_TYPE_DIR &&
590 !conn->marked_for_close &&
591 conn->timestamp_lastwritten + 5*60 < now) {
592 log_fn(LOG_INFO,"Expiring wedged directory conn (fd %d, purpose %d)",
593 conn->s, conn->purpose);
594 connection_mark_for_close(conn);
595 return;
598 /* If we haven't written to an OR connection for a while, then either nuke
599 the connection or send a keepalive, depending. */
600 if (connection_speaks_cells(conn) &&
601 now >= conn->timestamp_lastwritten + options->KeepalivePeriod) {
602 routerinfo_t *router = router_get_by_digest(conn->identity_digest);
603 if (!connection_state_is_open(conn)) {
604 log_fn(LOG_INFO,"Expiring non-open OR connection to fd %d (%s:%d).",
605 conn->s,conn->address, conn->port);
606 connection_mark_for_close(conn);
607 conn->hold_open_until_flushed = 1;
608 } else if (we_are_hibernating() && !circuit_get_by_conn(conn) &&
609 !buf_datalen(conn->outbuf)) {
610 log_fn(LOG_INFO,"Expiring non-used OR connection to fd %d (%s:%d) [Hibernating or exiting].",
611 conn->s,conn->address, conn->port);
612 connection_mark_for_close(conn);
613 conn->hold_open_until_flushed = 1;
614 } else if (!clique_mode(options) && !circuit_get_by_conn(conn) &&
615 (!router || !server_mode(options) || !router_is_clique_mode(router))) {
616 log_fn(LOG_INFO,"Expiring non-used OR connection to fd %d (%s:%d) [Not in clique mode].",
617 conn->s,conn->address, conn->port);
618 connection_mark_for_close(conn);
619 conn->hold_open_until_flushed = 1;
620 } else if (
621 now >= conn->timestamp_lastempty + options->KeepalivePeriod*10 &&
622 now >= conn->timestamp_lastwritten + options->KeepalivePeriod*10) {
623 log_fn(LOG_NOTICE,"Expiring stuck OR connection to fd %d (%s:%d). (%d bytes to flush; %d seconds since last write)",
624 conn->s, conn->address, conn->port,
625 (int)buf_datalen(conn->outbuf),
626 (int)(now-conn->timestamp_lastwritten));
627 connection_mark_for_close(conn);
628 } else if (!buf_datalen(conn->outbuf)) {
629 /* either in clique mode, or we've got a circuit. send a padding cell. */
630 log_fn(LOG_DEBUG,"Sending keepalive to (%s:%d)",
631 conn->address, conn->port);
632 memset(&cell,0,sizeof(cell_t));
633 cell.command = CELL_PADDING;
634 connection_or_write_cell_to_buf(&cell, conn);
639 /** Perform regular maintenance tasks. This function gets run once per
640 * second by prepare_for_poll.
642 static void
643 run_scheduled_events(time_t now)
645 static time_t last_rotated_certificate = 0;
646 static time_t time_to_check_listeners = 0;
647 static time_t time_to_check_descriptor = 0;
648 static time_t time_to_shrink_buffers = 0;
649 or_options_t *options = get_options();
650 int i;
652 /** 0. See if we've been asked to shut down and our timeout has
653 * expired; or if our bandwidth limits are exhausted and we
654 * should hibernate; or if it's time to wake up from hibernation.
656 consider_hibernation(now);
658 /** 1a. Every MIN_ONION_KEY_LIFETIME seconds, rotate the onion keys,
659 * shut down and restart all cpuworkers, and update the directory if
660 * necessary.
662 if (server_mode(options) &&
663 get_onion_key_set_at()+MIN_ONION_KEY_LIFETIME < now) {
664 log_fn(LOG_INFO,"Rotating onion key.");
665 rotate_onion_key();
666 cpuworkers_rotate();
667 if (router_rebuild_descriptor(1)<0) {
668 log_fn(LOG_WARN, "Couldn't rebuild router descriptor");
670 if (advertised_server_mode())
671 router_upload_dir_desc_to_dirservers(0);
674 /** 1b. Every MAX_SSL_KEY_LIFETIME seconds, we change our TLS context. */
675 if (!last_rotated_certificate)
676 last_rotated_certificate = now;
677 if (last_rotated_certificate+MAX_SSL_KEY_LIFETIME < now) {
678 log_fn(LOG_INFO,"Rotating tls context.");
679 if (tor_tls_context_new(get_identity_key(), 1, options->Nickname,
680 MAX_SSL_KEY_LIFETIME) < 0) {
681 log_fn(LOG_WARN, "Error reinitializing TLS context");
682 /* XXX is it a bug here, that we just keep going? */
684 last_rotated_certificate = now;
685 /* XXXX We should rotate TLS connections as well; this code doesn't change
686 * them at all. */
689 /** 1c. If we have to change the accounting interval or record
690 * bandwidth used in this accounting interval, do so. */
691 if (accounting_is_enabled(options))
692 accounting_run_housekeeping(now);
694 /** 2. Periodically, we consider getting a new directory, getting a
695 * new running-routers list, and/or force-uploading our descriptor
696 * (if we've passed our internal checks). */
697 if (time_to_fetch_directory < now) {
698 time_t next_status_fetch;
699 /* purge obsolete entries */
700 routerlist_remove_old_routers(ROUTER_MAX_AGE);
702 if (authdir_mode(options)) {
703 /* We're a directory; dump any old descriptors. */
704 dirserv_remove_old_servers(ROUTER_MAX_AGE);
706 if (server_mode(options) && !we_are_hibernating()) {
707 /* dirservers try to reconnect, in case connections have failed;
708 * and normal servers try to reconnect to dirservers */
709 router_retry_connections();
712 directory_get_from_dirserver(DIR_PURPOSE_FETCH_DIR, NULL, 1);
713 time_to_fetch_directory = now + get_dir_fetch_period(options);
714 next_status_fetch = now + get_status_fetch_period(options);
715 if (time_to_fetch_running_routers < next_status_fetch) {
716 time_to_fetch_running_routers = next_status_fetch;
719 /* Also, take this chance to remove old information from rephist. */
720 rep_history_clean(now-24*60*60);
723 if (time_to_fetch_running_routers < now) {
724 if (!authdir_mode(options)) {
725 directory_get_from_dirserver(DIR_PURPOSE_FETCH_RUNNING_LIST, NULL, 1);
727 time_to_fetch_running_routers = now + get_status_fetch_period(options);
730 if (time_to_force_upload_descriptor < now) {
731 consider_publishable_server(now, 1);
733 rend_cache_clean(); /* this should go elsewhere? */
735 time_to_force_upload_descriptor = now + options->DirPostPeriod;
738 /* 2b. Once per minute, regenerate and upload the descriptor if the old
739 * one is inaccurate. */
740 if (time_to_check_descriptor < now) {
741 time_to_check_descriptor = now + CHECK_DESCRIPTOR_INTERVAL;
742 consider_publishable_server(now, 0);
743 /* also, check religiously for reachability, if it's within the first
744 * 20 minutes of our uptime. */
745 if (server_mode(options) &&
746 stats_n_seconds_working < TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT &&
747 !we_are_hibernating())
748 consider_testing_reachability();
751 /** 3a. Every second, we examine pending circuits and prune the
752 * ones which have been pending for more than a few seconds.
753 * We do this before step 4, so it can try building more if
754 * it's not comfortable with the number of available circuits.
756 circuit_expire_building(now);
758 /** 3b. Also look at pending streams and prune the ones that 'began'
759 * a long time ago but haven't gotten a 'connected' yet.
760 * Do this before step 4, so we can put them back into pending
761 * state to be picked up by the new circuit.
763 connection_ap_expire_beginning();
765 /** 3c. And expire connections that we've held open for too long.
767 connection_expire_held_open();
769 /** 3d. And every 60 seconds, we relaunch listeners if any died. */
770 if (!we_are_hibernating() && time_to_check_listeners < now) {
771 retry_all_listeners(0); /* 0 means "only if some died." */
772 time_to_check_listeners = now+60;
775 /** 4. Every second, we try a new circuit if there are no valid
776 * circuits. Every NewCircuitPeriod seconds, we expire circuits
777 * that became dirty more than MaxCircuitDirtiness seconds ago,
778 * and we make a new circ if there are no clean circuits.
780 if (has_fetched_directory && !we_are_hibernating())
781 circuit_build_needed_circs(now);
783 /** 5. We do housekeeping for each connection... */
784 for (i=0;i<nfds;i++) {
785 run_connection_housekeeping(i, now);
787 if (time_to_shrink_buffers < now) {
788 for (i=0;i<nfds;i++) {
789 connection_t *conn = connection_array[i];
790 if (conn->outbuf)
791 buf_shrink(conn->outbuf);
792 if (conn->inbuf)
793 buf_shrink(conn->inbuf);
795 time_to_shrink_buffers = now + BUF_SHRINK_INTERVAL;
798 /** 6. And remove any marked circuits... */
799 circuit_close_all_marked();
801 /** 7. And upload service descriptors if necessary. */
802 if (has_fetched_directory && !we_are_hibernating())
803 rend_consider_services_upload(now);
805 /** 8. and blow away any connections that need to die. have to do this now,
806 * because if we marked a conn for close and left its socket -1, then
807 * we'll pass it to poll/select and bad things will happen.
809 close_closeable_connections();
812 static struct event *timeout_event = NULL;
813 static int n_libevent_errors = 0;
815 /** Libevent callback: invoked once every second. */
816 static void
817 second_elapsed_callback(int fd, short event, void *args)
819 static struct timeval one_second;
820 static long current_second = 0;
821 struct timeval now;
822 size_t bytes_written;
823 size_t bytes_read;
824 int seconds_elapsed;
825 or_options_t *options = get_options();
826 if (!timeout_event) {
827 timeout_event = tor_malloc_zero(sizeof(struct event));
828 evtimer_set(timeout_event, second_elapsed_callback, NULL);
829 one_second.tv_sec = 1;
830 one_second.tv_usec = 0;
833 n_libevent_errors = 0;
835 /* log_fn(LOG_NOTICE, "Tick."); */
836 tor_gettimeofday(&now);
838 /* the second has rolled over. check more stuff. */
839 bytes_written = stats_prev_global_write_bucket - global_write_bucket;
840 bytes_read = stats_prev_global_read_bucket - global_read_bucket;
841 /* XXX below we get suspicious if time jumps forward more than 10
842 * seconds, but we never notice if it jumps *back* more than 10 seconds.
843 * This could be useful for detecting that we just NTP'ed to three
844 * weeks ago and it will be 3 weeks and 15 minutes until any of our
845 * events trigger.
847 seconds_elapsed = current_second ? (now.tv_sec - current_second) : 0;
848 stats_n_bytes_read += bytes_read;
849 stats_n_bytes_written += bytes_written;
850 if (accounting_is_enabled(options))
851 accounting_add_bytes(bytes_read, bytes_written, seconds_elapsed);
852 control_event_bandwidth_used((uint32_t)bytes_read,(uint32_t)bytes_written);
854 connection_bucket_refill(&now);
855 stats_prev_global_read_bucket = global_read_bucket;
856 stats_prev_global_write_bucket = global_write_bucket;
858 if (server_mode(options) &&
859 !we_are_hibernating() &&
860 stats_n_seconds_working / TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT !=
861 (stats_n_seconds_working+seconds_elapsed) /
862 TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT) {
863 /* every 20 minutes, check and complain if necessary */
864 routerinfo_t *me = router_get_my_routerinfo();
865 if (!check_whether_orport_reachable())
866 log(LOG_WARN,"Your server (%s:%d) has not managed to confirm that its ORPort is reachable. Please check your firewalls, ports, address, etc.",
867 me ? me->address : options->Address, options->ORPort);
868 if (!check_whether_dirport_reachable())
869 log(LOG_WARN,"Your server (%s:%d) has not managed to confirm that its DirPort is reachable. Please check your firewalls, ports, address, etc.",
870 me ? me->address : options->Address, options->DirPort);
873 /* if more than 100s have elapsed, probably the clock jumped: doesn't count. */
874 if (seconds_elapsed < 100)
875 stats_n_seconds_working += seconds_elapsed;
876 else
877 circuit_note_clock_jumped(seconds_elapsed);
879 run_scheduled_events(now.tv_sec);
881 current_second = now.tv_sec; /* remember which second it is, for next time */
883 if (current_second % 60 == 0)
884 dumpmemusage(get_min_log_level()<LOG_INFO ? get_min_log_level() : LOG_INFO);
886 if (evtimer_add(timeout_event, &one_second))
887 log_fn(LOG_ERR,
888 "Error from libevent when setting one-second timeout event");
891 /** Called when a possibly ignorable libevent error occurs; ensures that we
892 * don't get into an infinite loop by ignoring too many errors from
893 * libevent. */
895 got_libevent_error(void)
897 if (++n_libevent_errors > 8) {
898 log_fn(LOG_ERR, "Too many libevent errors in one second; dying");
899 return -1;
901 return 0;
904 /** Called when we get a SIGHUP: reload configuration files and keys,
905 * retry all connections, re-upload all descriptors, and so on. */
906 static int
907 do_hup(void)
909 char keydir[512];
910 or_options_t *options = get_options();
912 log(LOG_NOTICE,"Received sighup. Reloading config.");
913 has_completed_circuit=0;
914 if (accounting_is_enabled(options))
915 accounting_record_bandwidth_usage(time(NULL));
917 addressmap_clear_transient();
918 /* first, reload config variables, in case they've changed */
919 /* no need to provide argc/v, they've been cached inside init_from_config */
920 if (init_from_config(0, NULL) < 0) {
921 log_fn(LOG_ERR,"Reading config failed--see warnings above. For usage, try -h.");
922 return -1;
924 options = get_options(); /* they have changed now */
925 if (authdir_mode(options)) {
926 /* reload the approved-routers file */
927 tor_snprintf(keydir,sizeof(keydir),"%s/approved-routers", options->DataDirectory);
928 log_fn(LOG_INFO,"Reloading approved fingerprints from %s...",keydir);
929 if (dirserv_parse_fingerprint_file(keydir) < 0) {
930 log_fn(LOG_NOTICE, "Error reloading fingerprints. Continuing with old list.");
933 /* Fetch a new directory. Even authdirservers do this. */
934 directory_get_from_dirserver(DIR_PURPOSE_FETCH_DIR, NULL, 1);
935 if (server_mode(options)) {
936 const char *descriptor;
937 /* Restart cpuworker and dnsworker processes, so they get up-to-date
938 * configuration options. */
939 cpuworkers_rotate();
940 dnsworkers_rotate();
941 /* Rebuild fresh descriptor, but leave old one on failure. */
942 router_rebuild_descriptor(1);
943 descriptor = router_get_my_descriptor();
944 if (!descriptor) {
945 log_fn(LOG_WARN,"No descriptor to save.");
946 return 0;
948 tor_snprintf(keydir,sizeof(keydir),"%s/router.desc",
949 options->DataDirectory);
950 log_fn(LOG_INFO,"Saving descriptor to %s...",keydir);
951 if (write_str_to_file(keydir, descriptor, 0)) {
952 return 0;
955 return 0;
958 /** Tor main loop. */
959 static int
960 do_main_loop(void)
962 int loop_result;
964 /* load the private keys, if we're supposed to have them, and set up the
965 * TLS context. */
966 if (! identity_key_is_set()) {
967 if (init_keys() < 0) {
968 log_fn(LOG_ERR,"Error initializing keys; exiting");
969 return -1;
973 /* Set up our buckets */
974 connection_bucket_init();
975 stats_prev_global_read_bucket = global_read_bucket;
976 stats_prev_global_write_bucket = global_write_bucket;
978 /* load the routers file, or assign the defaults. */
979 if (router_reload_router_list()) {
980 return -1;
983 if (authdir_mode(get_options())) {
984 /* the directory is already here, run startup things */
985 router_retry_connections();
988 if (server_mode(get_options())) {
989 /* launch cpuworkers. Need to do this *after* we've read the onion key. */
990 cpu_init();
993 /* set up once-a-second callback. */
994 second_elapsed_callback(0,0,NULL);
996 for (;;) {
997 if (nt_service_is_stopped())
998 return 0;
1000 #ifndef MS_WINDOWS
1001 /* Make it easier to tell whether libevent failure is our fault or not. */
1002 errno = 0;
1003 #endif
1004 /* poll until we have an event, or the second ends */
1005 loop_result = event_dispatch();
1007 /* let catch() handle things like ^c, and otherwise don't worry about it */
1008 if (loop_result < 0) {
1009 int e = tor_socket_errno(-1);
1010 /* let the program survive things like ^z */
1011 if (e != EINTR && !ERRNO_IS_EINPROGRESS(e)) {
1012 #ifdef HAVE_EVENT_GET_METHOD
1013 log_fn(LOG_ERR,"libevent poll with %s failed: %s [%d]",
1014 event_get_method(), tor_socket_strerror(e), e);
1015 #else
1016 log_fn(LOG_ERR,"libevent poll failed: %s [%d]",
1017 tor_socket_strerror(e), e);
1018 #endif
1019 return -1;
1020 #ifndef MS_WINDOWS
1021 } else if (e == EINVAL) {
1022 log_fn(LOG_WARN, "EINVAL from libevent: should you upgrade libevent?");
1023 if (got_libevent_error())
1024 return -1;
1025 #endif
1026 } else {
1027 if (ERRNO_IS_EINPROGRESS(e))
1028 log_fn(LOG_WARN,"libevent poll returned EINPROGRESS? Please report.");
1029 log_fn(LOG_DEBUG,"event poll interrupted.");
1030 /* You can't trust the results of this poll(). Go back to the
1031 * top of the big for loop. */
1032 continue;
1036 /* refilling buckets and sending cells happens at the beginning of the
1037 * next iteration of the loop, inside prepare_for_poll()
1038 * XXXX No longer so.
1043 /** Used to implement the SIGNAL control command: if we accept
1044 * <b>the_signal</b> as a remote pseudo-signal, then act on it and
1045 * return 0. Else return -1. */
1046 /* We don't re-use catch() here because:
1047 * 1. We handle a different set of signals than those allowed in catch.
1048 * 2. Platforms without signal() are unlikely to define SIGfoo.
1049 * 3. The control spec is defined to use fixed numeric signal values
1050 * which just happen to match the unix values.
1053 control_signal_act(int the_signal)
1055 switch (the_signal)
1057 case 1:
1058 signal_callback(0,0,(void*)(uintptr_t)SIGHUP);
1059 break;
1060 case 2:
1061 signal_callback(0,0,(void*)(uintptr_t)SIGINT);
1062 break;
1063 case 10:
1064 signal_callback(0,0,(void*)(uintptr_t)SIGUSR1);
1065 break;
1066 case 12:
1067 signal_callback(0,0,(void*)(uintptr_t)SIGUSR2);
1068 break;
1069 case 15:
1070 signal_callback(0,0,(void*)(uintptr_t)SIGTERM);
1071 break;
1072 default:
1073 return -1;
1075 return 0;
1078 /** Libevent callback: invoked when we get a signal.
1080 static void
1081 signal_callback(int fd, short events, void *arg)
1083 uintptr_t sig = (uintptr_t)arg;
1084 switch (sig)
1086 case SIGTERM:
1087 log(LOG_ERR,"Catching signal TERM, exiting cleanly.");
1088 tor_cleanup();
1089 exit(0);
1090 break;
1091 case SIGINT:
1092 if (!server_mode(get_options())) { /* do it now */
1093 log(LOG_NOTICE,"Interrupt: exiting cleanly.");
1094 tor_cleanup();
1095 exit(0);
1097 hibernate_begin_shutdown();
1098 break;
1099 #ifdef SIGPIPE
1100 case SIGPIPE:
1101 log(LOG_DEBUG,"Caught sigpipe. Ignoring.");
1102 break;
1103 #endif
1104 case SIGUSR1:
1105 /* prefer to log it at INFO, but make sure we always see it */
1106 dumpstats(get_min_log_level()<LOG_INFO ? get_min_log_level() : LOG_INFO);
1107 break;
1108 case SIGUSR2:
1109 switch_logs_debug();
1110 log(LOG_NOTICE,"Caught USR2, going to loglevel debug. Send HUP to change back.");
1111 break;
1112 case SIGHUP:
1113 if (do_hup() < 0) {
1114 log_fn(LOG_WARN,"Restart failed (config error?). Exiting.");
1115 tor_cleanup();
1116 exit(1);
1118 break;
1119 #ifdef SIGCHLD
1120 case SIGCHLD:
1121 while (waitpid(-1,NULL,WNOHANG) > 0) ; /* keep reaping until no more zombies */
1122 break;
1123 #endif
1128 * Write current memory uusage information to the log.
1130 static void
1131 dumpmemusage(int severity)
1133 extern uint64_t buf_total_used;
1134 extern uint64_t buf_total_alloc;
1135 extern uint64_t rephist_total_alloc;
1137 log(severity, "In buffers: "U64_FORMAT" used/"U64_FORMAT" allocated (%d conns).",
1138 U64_PRINTF_ARG(buf_total_used), U64_PRINTF_ARG(buf_total_alloc),
1139 nfds);
1140 log(severity, "In rephist: "U64_FORMAT" used.",
1141 U64_PRINTF_ARG(rephist_total_alloc));
1144 /** Write all statistics to the log, with log level 'severity'. Called
1145 * in response to a SIGUSR1. */
1146 static void
1147 dumpstats(int severity)
1149 int i;
1150 connection_t *conn;
1151 time_t now = time(NULL);
1152 time_t elapsed;
1154 log(severity, "Dumping stats:");
1156 for (i=0;i<nfds;i++) {
1157 conn = connection_array[i];
1158 log(severity, "Conn %d (socket %d) type %d (%s), state %d (%s), created %d secs ago",
1159 i, conn->s, conn->type, conn_type_to_string(conn->type),
1160 conn->state, conn_state_to_string(conn->type, conn->state), (int)(now - conn->timestamp_created));
1161 if (!connection_is_listener(conn)) {
1162 log(severity,"Conn %d is to '%s:%d'.",i,safe_str(conn->address), conn->port);
1163 log(severity,"Conn %d: %d bytes waiting on inbuf (len %d, last read %d secs ago)",i,
1164 (int)buf_datalen(conn->inbuf),
1165 (int)buf_capacity(conn->inbuf),
1166 (int)(now - conn->timestamp_lastread));
1167 log(severity,"Conn %d: %d bytes waiting on outbuf (len %d, last written %d secs ago)",i,
1168 (int)buf_datalen(conn->outbuf),
1169 (int)buf_capacity(conn->outbuf),
1170 (int)(now - conn->timestamp_lastwritten));
1172 circuit_dump_by_conn(conn, severity); /* dump info about all the circuits using this conn */
1174 log(severity,
1175 "Cells processed: %10lu padding\n"
1176 " %10lu create\n"
1177 " %10lu created\n"
1178 " %10lu relay\n"
1179 " (%10lu relayed)\n"
1180 " (%10lu delivered)\n"
1181 " %10lu destroy",
1182 stats_n_padding_cells_processed,
1183 stats_n_create_cells_processed,
1184 stats_n_created_cells_processed,
1185 stats_n_relay_cells_processed,
1186 stats_n_relay_cells_relayed,
1187 stats_n_relay_cells_delivered,
1188 stats_n_destroy_cells_processed);
1189 if (stats_n_data_cells_packaged)
1190 log(severity,"Average packaged cell fullness: %2.3f%%",
1191 100*(((double)stats_n_data_bytes_packaged) /
1192 (stats_n_data_cells_packaged*RELAY_PAYLOAD_SIZE)) );
1193 if (stats_n_data_cells_received)
1194 log(severity,"Average delivered cell fullness: %2.3f%%",
1195 100*(((double)stats_n_data_bytes_received) /
1196 (stats_n_data_cells_received*RELAY_PAYLOAD_SIZE)) );
1198 if (now - time_of_process_start >= 0)
1199 elapsed = now - time_of_process_start;
1200 else
1201 elapsed = 0;
1203 if (elapsed) {
1204 log(severity,
1205 "Average bandwidth: "U64_FORMAT"/%d = %d bytes/sec reading",
1206 U64_PRINTF_ARG(stats_n_bytes_read),
1207 (int)elapsed,
1208 (int) (stats_n_bytes_read/elapsed));
1209 log(severity,
1210 "Average bandwidth: "U64_FORMAT"/%d = %d bytes/sec writing",
1211 U64_PRINTF_ARG(stats_n_bytes_written),
1212 (int)elapsed,
1213 (int) (stats_n_bytes_written/elapsed));
1216 log(severity, "--------------- Dumping memory information:");
1217 dumpmemusage(severity);
1219 rep_hist_dump_stats(now,severity);
1220 rend_service_dump_stats(severity);
1223 /** Called by exit() as we shut down the process.
1225 static void
1226 exit_function(void)
1228 /* NOTE: If we ever daemonize, this gets called immediately. That's
1229 * okay for now, because we only use this on Windows. */
1230 #ifdef MS_WINDOWS
1231 WSACleanup();
1232 #endif
1235 /** Set up the signal handlers for either parent or child. */
1236 void
1237 handle_signals(int is_parent)
1239 #ifndef MS_WINDOWS /* do signal stuff only on unix */
1240 int i;
1241 static int signals[] = {
1242 SIGINT, /* do a controlled slow shutdown */
1243 SIGTERM, /* to terminate now */
1244 SIGPIPE, /* otherwise sigpipe kills us */
1245 SIGUSR1, /* dump stats */
1246 SIGUSR2, /* go to loglevel debug */
1247 SIGHUP, /* to reload config, retry conns, etc */
1248 #ifdef SIGXFSZ
1249 SIGXFSZ, /* handle file-too-big resource exhaustion */
1250 #endif
1251 SIGCHLD, /* handle dns/cpu workers that exit */
1252 -1 };
1253 static struct event signal_events[16]; /* bigger than it has to be. */
1254 if (is_parent) {
1255 for (i = 0; signals[i] >= 0; ++i) {
1256 signal_set(&signal_events[i], signals[i], signal_callback,
1257 (void*)(uintptr_t)signals[i]);
1258 if (signal_add(&signal_events[i], NULL))
1259 log_fn(LOG_WARN, "Error from libevent when adding event for signal %d",
1260 signals[i]);
1262 } else {
1263 struct sigaction action;
1264 action.sa_flags = 0;
1265 sigemptyset(&action.sa_mask);
1266 action.sa_handler = SIG_IGN;
1267 sigaction(SIGINT, &action, NULL);
1268 sigaction(SIGTERM, &action, NULL);
1269 sigaction(SIGPIPE, &action, NULL);
1270 sigaction(SIGUSR1, &action, NULL);
1271 sigaction(SIGUSR2, &action, NULL);
1272 sigaction(SIGHUP, &action, NULL);
1273 #ifdef SIGXFSZ
1274 sigaction(SIGXFSZ, &action, NULL);
1275 #endif
1277 #endif /* signal stuff */
1280 /** Main entry point for the Tor command-line client.
1282 static int
1283 tor_init(int argc, char *argv[])
1285 time_of_process_start = time(NULL);
1286 closeable_connection_lst = smartlist_create();
1287 /* Initialize the history structures. */
1288 rep_hist_init();
1289 /* Initialize the service cache. */
1290 rend_cache_init();
1291 addressmap_init(); /* Init the client dns cache. Do it always, since it's cheap. */
1293 /* give it somewhere to log to initially */
1294 add_temp_log();
1296 log(LOG_NOTICE,"Tor v%s. This is experimental software. Do not rely on it for strong anonymity.",VERSION);
1298 if (network_init()<0) {
1299 log_fn(LOG_ERR,"Error initializing network; exiting.");
1300 return -1;
1302 atexit(exit_function);
1304 if (init_from_config(argc,argv) < 0) {
1305 log_fn(LOG_ERR,"Reading config failed--see warnings above. For usage, try -h.");
1306 return -1;
1309 #ifndef MS_WINDOWS
1310 if (geteuid()==0)
1311 log_fn(LOG_WARN,"You are running Tor as root. You don't need to, and you probably shouldn't.");
1312 #endif
1314 /* only spawn dns handlers if we're a router */
1315 if (server_mode(get_options()) && get_options()->command == CMD_RUN_TOR) {
1316 dns_init(); /* initialize the dns resolve tree, and spawn workers */
1317 /* XXX really, this should get moved to do_main_loop */
1320 handle_signals(1);
1322 crypto_global_init();
1323 if (crypto_seed_rng()) {
1324 log_fn(LOG_ERR, "Unable to seed random number generator. Exiting.");
1325 return -1;
1327 return 0;
1330 /** Free all memory that we might have allocated somewhere.
1331 * Helps us find the real leaks with dmalloc and the like.
1333 * Also valgrind should then report 0 reachable in its
1334 * leak report */
1335 void
1336 tor_free_all(int postfork)
1338 routerlist_free_current();
1339 free_trusted_dir_servers();
1340 addressmap_free_all();
1341 set_exit_redirects(NULL); /* free the registered exit redirects */
1342 free_socks_policy();
1343 free_dir_policy();
1344 dirserv_free_all();
1345 rend_service_free_all();
1346 rend_cache_free_all();
1347 rep_hist_free_all();
1348 dns_free_all();
1349 clear_pending_onions();
1350 circuit_free_all();
1351 connection_free_all();
1352 if (!postfork) {
1353 config_free_all();
1354 router_free_all_keys();
1356 tor_tls_free_all();
1357 /* stuff in main.c */
1358 smartlist_free(closeable_connection_lst);
1359 tor_free(timeout_event);
1361 if (!postfork) {
1362 close_logs(); /* free log strings. do this last so logs keep working. */
1366 /** Do whatever cleanup is necessary before shutting Tor down. */
1367 void
1368 tor_cleanup(void) {
1369 or_options_t *options = get_options();
1370 /* Remove our pid file. We don't care if there was an error when we
1371 * unlink, nothing we could do about it anyways. */
1372 if (options->PidFile && options->command == CMD_RUN_TOR)
1373 unlink(options->PidFile);
1374 if (accounting_is_enabled(options))
1375 accounting_record_bandwidth_usage(time(NULL));
1376 tor_free_all(0); /* move tor_free_all back into the ifdef below later. XXX*/
1377 crypto_global_cleanup();
1378 #ifdef USE_DMALLOC
1379 dmalloc_log_unfreed();
1380 dmalloc_shutdown();
1381 #endif
1384 /** Read/create keys as needed, and echo our fingerprint to stdout. */
1385 static void
1386 do_list_fingerprint(void)
1388 char buf[FINGERPRINT_LEN+1];
1389 crypto_pk_env_t *k;
1390 const char *nickname = get_options()->Nickname;
1391 if (!server_mode(get_options())) {
1392 printf("Clients don't have long-term identity keys. Exiting.\n");
1393 return;
1395 tor_assert(nickname);
1396 if (init_keys() < 0) {
1397 log_fn(LOG_ERR,"Error initializing keys; exiting");
1398 return;
1400 if (!(k = get_identity_key())) {
1401 log_fn(LOG_ERR,"Error: missing identity key.");
1402 return;
1404 if (crypto_pk_get_fingerprint(k, buf, 1)<0) {
1405 log_fn(LOG_ERR, "Error computing fingerprint");
1406 return;
1408 printf("%s %s\n", nickname, buf);
1411 /** Entry point for password hashing: take the desired password from
1412 * the command line, and print its salted hash to stdout. **/
1413 static void
1414 do_hash_password(void)
1417 char output[256];
1418 char key[S2K_SPECIFIER_LEN+DIGEST_LEN];
1420 crypto_rand(key, S2K_SPECIFIER_LEN-1);
1421 key[S2K_SPECIFIER_LEN-1] = (uint8_t)96; /* Hash 64 K of data. */
1422 secret_to_key(key+S2K_SPECIFIER_LEN, DIGEST_LEN,
1423 get_options()->command_arg, strlen(get_options()->command_arg),
1424 key);
1425 base16_encode(output, sizeof(output), key, sizeof(key));
1426 printf("16:%s\n",output);
1429 #ifdef MS_WINDOWS_SERVICE
1430 /** If we're compile to run as an NT service, and the service has been
1431 * shut down, then change our current status and return 1. Else
1432 * return 0.
1434 static int
1435 nt_service_is_stopped(void)
1437 if (service_status.dwCurrentState == SERVICE_STOP_PENDING) {
1438 service_status.dwWin32ExitCode = 0;
1439 service_status.dwCurrentState = SERVICE_STOPPED;
1440 SetServiceStatus(hStatus, &service_status);
1441 return 1;
1443 return 0;
1446 /** DOCDOC */
1447 void
1448 nt_service_control(DWORD request)
1450 switch (request) {
1451 case SERVICE_CONTROL_STOP:
1452 case SERVICE_CONTROL_SHUTDOWN:
1453 log(LOG_ERR, "Got stop/shutdown request; shutting down cleanly.");
1454 service_status.dwCurrentState = SERVICE_STOP_PENDING;
1455 return;
1457 SetServiceStatus(hStatus, &service_status);
1460 /** DOCDOC */
1461 void
1462 nt_service_body(int argc, char **argv)
1464 int err;
1465 service_status.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
1466 service_status.dwCurrentState = SERVICE_START_PENDING;
1467 service_status.dwControlsAccepted =
1468 SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
1469 service_status.dwWin32ExitCode = 0;
1470 service_status.dwServiceSpecificExitCode = 0;
1471 service_status.dwCheckPoint = 0;
1472 service_status.dwWaitHint = 1000;
1473 hStatus = RegisterServiceCtrlHandler(GENSRV_SERVICENAME, (LPHANDLER_FUNCTION) nt_service_control);
1475 if (hStatus == 0) {
1476 // failed;
1477 return;
1480 err = tor_init(backup_argc, backup_argv); // refactor this part out of tor_main and do_main_loop
1481 if (err) {
1482 // failed.
1483 service_status.dwCurrentState = SERVICE_STOPPED;
1484 service_status.dwWin32ExitCode = -1;
1485 SetServiceStatus(hStatus, &service_status);
1486 return;
1488 service_status.dwCurrentState = SERVICE_RUNNING;
1489 SetServiceStatus(hStatus, &service_status);
1490 do_main_loop();
1491 tor_cleanup();
1492 return;
1495 /** DOCDOC */
1496 void
1497 nt_service_main(void)
1499 SERVICE_TABLE_ENTRY table[2];
1500 DWORD result = 0;
1501 table[0].lpServiceName = GENSRV_SERVICENAME;
1502 table[0].lpServiceProc = (LPSERVICE_MAIN_FUNCTION)nt_service_body;
1503 table[1].lpServiceName = NULL;
1504 table[1].lpServiceProc = NULL;
1506 if (!StartServiceCtrlDispatcher(table)) {
1507 result = GetLastError();
1508 printf("Error was %d\n",result);
1509 if (result == ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) {
1510 if (tor_init(backup_argc, backup_argv) < 0)
1511 return;
1512 switch (get_options()->command) {
1513 case CMD_RUN_TOR:
1514 do_main_loop();
1515 break;
1516 case CMD_LIST_FINGERPRINT:
1517 do_list_fingerprint();
1518 break;
1519 case CMD_HASH_PASSWORD:
1520 do_hash_password();
1521 break;
1522 case CMD_VERIFY_CONFIG:
1523 printf("Configuration was valid\n");
1524 break;
1525 default:
1526 log_fn(LOG_ERR, "Illegal command number %d: internal error.", get_options()->command);
1528 tor_cleanup();
1533 /** DOCDOC */
1535 nt_service_install(void)
1537 /* XXXX Problems with NT services:
1538 * 1. The configuration file needs to be in the same directory as the .exe
1540 * 2. The exe and the configuration file can't be on any directory path
1541 * that contains a space.
1542 * mje - you can quote the string (i.e., "c:\program files")
1544 * 3. Ideally, there should be one EXE that can either run as a
1545 * separate process (as now) or that can install and run itself
1546 * as an NT service. I have no idea how hard this is.
1547 * mje - should be done. It can install and run itself as a service
1549 * Notes about developing NT services:
1551 * 1. Don't count on your CWD. If an absolute path is not given, the
1552 * fopen() function goes wrong.
1553 * 2. The parameters given to the nt_service_body() function differ
1554 * from those given to main() function.
1557 SC_HANDLE hSCManager = NULL;
1558 SC_HANDLE hService = NULL;
1559 SERVICE_DESCRIPTION sdBuff;
1560 TCHAR szPath[_MAX_PATH];
1561 TCHAR szDrive[_MAX_DRIVE];
1562 TCHAR szDir[_MAX_DIR];
1563 char cmd1[] = " -f ";
1564 char cmd2[] = "\\torrc";
1565 char *command;
1566 int len = 0;
1568 if (0 == GetModuleFileName(NULL, szPath, MAX_PATH))
1569 return 0;
1571 _tsplitpath(szPath, szDrive, szDir, NULL, NULL);
1573 /* Account for the extra quotes */
1574 //len = _MAX_PATH + strlen(cmd1) + _MAX_DRIVE + _MAX_DIR + strlen(cmd2);
1575 len = _MAX_PATH + strlen(cmd1) + _MAX_DRIVE + _MAX_DIR + strlen(cmd2) + 64;
1576 command = tor_malloc(len);
1578 /* Create a quoted command line, like "c:\with spaces\tor.exe" -f
1579 * "c:\with spaces\tor.exe"
1581 if (tor_snprintf(command, len, "\"%s\" --nt-service -f \"%s%storrc\"",
1582 szPath, szDrive, szDir)<0) {
1583 printf("Failed: tor_snprinf()\n");
1584 free(command);
1585 return 0;
1588 if ((hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CREATE_SERVICE)) == NULL) {
1589 printf("Failed: OpenSCManager()\n");
1590 free(command);
1591 return 0;
1594 /* 1/26/2005 mje
1595 * - changed the service start type to auto
1596 * - and changed the lpPassword param to "" instead of NULL as per an
1597 * MSDN article.
1599 if ((hService = CreateService(hSCManager, GENSRV_SERVICENAME, GENSRV_DISPLAYNAME,
1600 SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
1601 SERVICE_AUTO_START, SERVICE_ERROR_IGNORE, command,
1602 NULL, NULL, NULL, NULL, "")) == NULL) {
1603 printf("Failed: CreateService()\n");
1604 CloseServiceHandle(hSCManager);
1605 free(command);
1606 return 0;
1609 /* Start the service initially, so you don't have to muck with it in the SCM
1611 /* Set the service's description */
1612 sdBuff.lpDescription = GENSRV_DESCRIPTION;
1613 ChangeServiceConfig2(hService, SERVICE_CONFIG_DESCRIPTION, &sdBuff);
1615 /* Start the service, so you don't have to muck with it in the SCM */
1616 if (StartService(hService, 0, NULL)) {
1617 /* Loop until the service has finished attempting to start */
1618 while (QueryServiceStatus(hService, &service_status) &&
1619 service_status.dwCurrentState == SERVICE_START_PENDING)
1620 Sleep(500);
1622 /* Check if it started successfully or not */
1623 if (service_status.dwCurrentState == SERVICE_RUNNING)
1624 printf("Service installed and started successfully.\n");
1625 else
1626 printf("Service installed, but failed to start.\n");
1627 } else {
1628 printf("Service installed, but failed to start.\n");
1631 CloseServiceHandle(hService);
1632 CloseServiceHandle(hSCManager);
1633 tor_free(command);
1635 return 0;
1638 /** DOCDOC */
1640 nt_service_remove(void)
1642 SC_HANDLE hSCManager = NULL;
1643 SC_HANDLE hService = NULL;
1644 SERVICE_STATUS service_status;
1645 BOOL result = FALSE;
1647 if ((hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CREATE_SERVICE)) == NULL) {
1648 printf("Failed: OpenSCManager()\n");
1649 return 0;
1652 if ((hService = OpenService(hSCManager, GENSRV_SERVICENAME, SERVICE_ALL_ACCESS)) == NULL) {
1653 printf("Failed: OpenService()\n");
1654 CloseServiceHandle(hSCManager);
1655 return 0;
1658 result = ControlService(hService, SERVICE_CONTROL_STOP, &service_status);
1659 if (result) {
1660 while (QueryServiceStatus(hService, &service_status))
1662 if (service_status.dwCurrentState == SERVICE_STOP_PENDING)
1663 Sleep(500);
1664 else
1665 break;
1667 if (DeleteService(hService))
1668 printf("Removed service successfully\n");
1669 else
1670 printf("Failed: DeleteService()\n");
1671 } else {
1672 result = DeleteService(hService);
1673 if (result)
1674 printf("Removed service successfully\n");
1675 else
1676 printf("Failed: DeleteService()\n");
1679 CloseServiceHandle(hService);
1680 CloseServiceHandle(hSCManager);
1682 return 0;
1684 #endif
1686 /** DOCDOC */
1688 tor_main(int argc, char *argv[])
1690 #ifdef MS_WINDOWS_SERVICE
1691 backup_argv = argv;
1692 backup_argc = argc;
1693 if ((argc >= 2) && !strcmp(argv[1], "-install"))
1694 return nt_service_install();
1695 if ((argc >= 2) && !strcmp(argv[1], "-remove"))
1696 return nt_service_remove();
1697 if ((argc >= 2) && !strcmp(argv[1], "--nt-service")) {
1698 nt_service_main();
1699 return 0;
1701 #endif
1702 if (tor_init(argc, argv)<0)
1703 return -1;
1704 switch (get_options()->command) {
1705 case CMD_RUN_TOR:
1706 #ifdef MS_WINDOWS_SERVICE
1707 service_status.dwCurrentState = SERVICE_RUNNING;
1708 #endif
1709 do_main_loop();
1710 break;
1711 case CMD_LIST_FINGERPRINT:
1712 do_list_fingerprint();
1713 break;
1714 case CMD_HASH_PASSWORD:
1715 do_hash_password();
1716 break;
1717 case CMD_VERIFY_CONFIG:
1718 printf("Configuration was valid\n");
1719 break;
1720 default:
1721 log_fn(LOG_ERR, "Illegal command number %d: internal error.",
1722 get_options()->command);
1724 tor_cleanup();
1725 return -1;