i screwed up the dirport reachability testing when we don't yet
[tor.git] / src / or / main.c
blobd80ab6fb7bf1ac815d5ca5d189068dc468b9a01b
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 Tor main loop and startup functions.
11 **/
13 #include "or.h"
14 #ifdef USE_DMALLOC
15 #include <dmalloc.h>
16 #endif
18 /* These signals are defined to help control_signal_act work. */
19 #ifndef SIGHUP
20 #define SIGHUP 1
21 #endif
22 #ifndef SIGINT
23 #define SIGINT 2
24 #endif
25 #ifndef SIGUSR1
26 #define SIGUSR1 10
27 #endif
28 #ifndef SIGUSR2
29 #define SIGUSR2 12
30 #endif
31 #ifndef SIGTERM
32 #define SIGTERM 15
33 #endif
35 /********* PROTOTYPES **********/
37 static void dumpstats(int severity); /* log stats */
38 static void conn_read_callback(int fd, short event, void *_conn);
39 static void conn_write_callback(int fd, short event, void *_conn);
40 static void signal_callback(int fd, short events, void *arg);
41 static void second_elapsed_callback(int fd, short event, void *args);
42 static int conn_close_if_marked(int i);
44 /********* START VARIABLES **********/
46 int global_read_bucket; /**< Max number of bytes I can read this second. */
47 int global_write_bucket; /**< Max number of bytes I can write this second. */
49 /** What was the read bucket before the last call to prepare_for_pool?
50 * (used to determine how many bytes we've read). */
51 static int stats_prev_global_read_bucket;
52 /** What was the write bucket before the last call to prepare_for_pool?
53 * (used to determine how many bytes we've written). */
54 static int stats_prev_global_write_bucket;
55 /** How many bytes have we read/written since we started the process? */
56 static uint64_t stats_n_bytes_read = 0;
57 static uint64_t stats_n_bytes_written = 0;
58 /** What time did this process start up? */
59 long time_of_process_start = 0;
60 /** How many seconds have we been running? */
61 long stats_n_seconds_working = 0;
62 /** When do we next download a directory? */
63 static time_t time_to_fetch_directory = 0;
64 /** When do we next upload our descriptor? */
65 static time_t time_to_force_upload_descriptor = 0;
66 /** When do we next download a running-routers summary? */
67 static time_t time_to_fetch_running_routers = 0;
69 /** Array of all open connections; each element corresponds to the element of
70 * poll_array in the same position. The first nfds elements are valid. */
71 static connection_t *connection_array[MAXCONNECTIONS+1] =
72 { NULL };
73 static smartlist_t *closeable_connection_lst = NULL;
75 static int nfds=0; /**< Number of connections currently active. */
77 /** We set this to 1 when we've fetched a dir, to know whether to complain
78 * yet about unrecognized nicknames in entrynodes, exitnodes, etc.
79 * Also, we don't try building circuits unless this is 1. */
80 int has_fetched_directory=0;
82 /** We set this to 1 when we've opened a circuit, so we can print a log
83 * entry to inform the user that Tor is working. */
84 int has_completed_circuit=0;
86 #ifdef MS_WINDOWS
87 #define MS_WINDOWS_SERVICE
88 #endif
90 #ifdef MS_WINDOWS_SERVICE
91 #include <tchar.h>
92 #define GENSRV_SERVICENAME TEXT("tor")
93 #define GENSRV_DISPLAYNAME TEXT("Tor Win32 Service")
94 #define GENSRV_DESCRIPTION TEXT("Provides an anonymous Internet communication system")
95 SERVICE_STATUS service_status;
96 SERVICE_STATUS_HANDLE hStatus;
97 static char **backup_argv;
98 static int backup_argc;
99 static int nt_service_is_stopped(void);
100 #else
101 #define nt_service_is_stopped() (0)
102 #endif
104 #define CHECK_DESCRIPTOR_INTERVAL 60 /* one minute */
105 #define BUF_SHRINK_INTERVAL 60 /* one minute */
106 #define TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT (20*60) /* 20 minutes */
108 /********* END VARIABLES ************/
110 /****************************************************************************
112 * This section contains accessors and other methods on the connection_array
113 * and poll_array variables (which are global within this file and unavailable
114 * 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.
122 int connection_add(connection_t *conn) {
123 tor_assert(conn);
124 tor_assert(conn->s >= 0);
126 if (nfds >= get_options()->_ConnLimit-1) {
127 log_fn(LOG_WARN,"Failing because we have %d connections already. Please raise your ulimit -n.", nfds);
128 return -1;
131 tor_assert(conn->poll_index == -1); /* can only connection_add once */
132 conn->poll_index = nfds;
133 connection_array[nfds] = conn;
135 conn->read_event = tor_malloc_zero(sizeof(struct event));
136 conn->write_event = tor_malloc_zero(sizeof(struct event));
137 event_set(conn->read_event, conn->s, EV_READ|EV_PERSIST,
138 conn_read_callback, conn);
139 event_set(conn->write_event, conn->s, EV_WRITE|EV_PERSIST,
140 conn_write_callback, conn);
142 nfds++;
144 log_fn(LOG_INFO,"new conn type %s, socket %d, nfds %d.",
145 conn_type_to_string(conn->type), conn->s, nfds);
147 return 0;
150 /** Remove the connection from the global list, and remove the
151 * corresponding poll entry. Calling this function will shift the last
152 * connection (if any) into the position occupied by conn.
154 int connection_remove(connection_t *conn) {
155 int current_index;
157 tor_assert(conn);
158 tor_assert(nfds>0);
160 log_fn(LOG_INFO,"removing socket %d (type %s), nfds now %d",
161 conn->s, conn_type_to_string(conn->type), nfds-1);
163 tor_assert(conn->poll_index >= 0);
164 current_index = conn->poll_index;
165 if (current_index == nfds-1) { /* this is the end */
166 nfds--;
167 return 0;
170 connection_unregister(conn);
172 /* replace this one with the one at the end */
173 nfds--;
174 connection_array[current_index] = connection_array[nfds];
175 connection_array[current_index]->poll_index = current_index;
177 return 0;
180 /** If it's an edge conn, remove it from the list
181 * of conn's on this circuit. If it's not on an edge,
182 * flush and send destroys for all circuits on this conn.
184 * If <b>remove</b> is non-zero, then remove it from the
185 * connection_array and closeable_connection_lst.
187 * Then free it.
189 static void connection_unlink(connection_t *conn, int remove) {
190 circuit_about_to_close_connection(conn);
191 connection_about_to_close_connection(conn);
192 if (remove) {
193 connection_remove(conn);
195 smartlist_remove(closeable_connection_lst, conn);
196 if (conn->type == CONN_TYPE_EXIT) {
197 assert_connection_edge_not_dns_pending(conn);
199 connection_free(conn);
202 /** Schedule <b>conn</b> to be closed. **/
203 void
204 add_connection_to_closeable_list(connection_t *conn)
206 tor_assert(!smartlist_isin(closeable_connection_lst, conn));
207 tor_assert(conn->marked_for_close);
208 assert_connection_ok(conn, time(NULL));
209 smartlist_add(closeable_connection_lst, conn);
212 /** Return 1 if conn is on the closeable list, else return 0. */
213 int connection_is_on_closeable_list(connection_t *conn) {
214 return smartlist_isin(closeable_connection_lst, conn);
217 /** Return true iff conn is in the current poll array. */
218 int connection_in_array(connection_t *conn) {
219 int i;
220 for (i=0; i<nfds; ++i) {
221 if (conn==connection_array[i])
222 return 1;
224 return 0;
227 /** Set <b>*array</b> to an array of all connections, and <b>*n</b>
228 * to the length of the array. <b>*array</b> and <b>*n</b> must not
229 * be modified.
231 void get_connection_array(connection_t ***array, int *n) {
232 *array = connection_array;
233 *n = nfds;
236 /** Set the event mask on <b>conn</b> to <b>events</b>. (The event
237 * mask is a bitmask whose bits are EV_READ and EV_WRITE.)
239 void connection_watch_events(connection_t *conn, short events) {
240 int r;
242 tor_assert(conn);
243 tor_assert(conn->read_event);
244 tor_assert(conn->write_event);
246 if (events & EV_READ) {
247 r = event_add(conn->read_event, NULL);
248 } else {
249 r = event_del(conn->read_event);
252 if (r<0)
253 log_fn(LOG_WARN,
254 "Error from libevent setting read event state for %d to %swatched.",
255 conn->s, (events & EV_READ)?"":"un");
257 if (events & EV_WRITE) {
258 r = event_add(conn->write_event, NULL);
259 } else {
260 r = event_del(conn->write_event);
263 if (r<0)
264 log_fn(LOG_WARN,
265 "Error from libevent setting read event state for %d to %swatched.",
266 conn->s, (events & EV_WRITE)?"":"un");
269 /** Return true iff <b>conn</b> is listening for read events. */
270 int connection_is_reading(connection_t *conn) {
271 tor_assert(conn);
273 return conn->read_event && event_pending(conn->read_event, EV_READ, NULL);
276 /** Tell the main loop to stop notifying <b>conn</b> of any read events. */
277 void connection_stop_reading(connection_t *conn) {
278 tor_assert(conn);
279 tor_assert(conn->read_event);
281 log(LOG_DEBUG,"connection_stop_reading() called.");
282 if (event_del(conn->read_event))
283 log_fn(LOG_WARN, "Error from libevent setting read event state for %d to unwatched.",
284 conn->s);
287 /** Tell the main loop to start notifying <b>conn</b> of any read events. */
288 void connection_start_reading(connection_t *conn) {
289 tor_assert(conn);
290 tor_assert(conn->read_event);
292 if (event_add(conn->read_event, NULL))
293 log_fn(LOG_WARN, "Error from libevent setting read event state for %d to watched.",
294 conn->s);
297 /** Return true iff <b>conn</b> is listening for write events. */
298 int connection_is_writing(connection_t *conn) {
299 tor_assert(conn);
301 return conn->write_event && event_pending(conn->write_event, EV_WRITE, NULL);
304 /** Tell the main loop to stop notifying <b>conn</b> of any write events. */
305 void connection_stop_writing(connection_t *conn) {
306 tor_assert(conn);
307 tor_assert(conn->write_event);
309 if (event_del(conn->write_event))
310 log_fn(LOG_WARN, "Error from libevent setting write event state for %d to unwatched.",
311 conn->s);
315 /** Tell the main loop to start notifying <b>conn</b> of any write events. */
316 void connection_start_writing(connection_t *conn) {
317 tor_assert(conn);
318 tor_assert(conn->write_event);
320 if (event_add(conn->write_event, NULL))
321 log_fn(LOG_WARN, "Error from libevent setting write event state for %d to watched.",
322 conn->s);
325 /** Close all connections that have been scheduled to get closed */
326 static void
327 close_closeable_connections(void)
329 int i;
330 for (i = 0; i < smartlist_len(closeable_connection_lst); ) {
331 connection_t *conn = smartlist_get(closeable_connection_lst, i);
332 if (conn->poll_index < 0) {
333 connection_unlink(conn, 0); /* blow it away right now */
334 } else {
335 if (!conn_close_if_marked(conn->poll_index))
336 ++i;
341 /** Libevent callback: this gets invoked when (connection_t*)<b>conn</b> has
342 * some data to read. */
343 static void
344 conn_read_callback(int fd, short event, void *_conn)
346 connection_t *conn = _conn;
347 if (conn->marked_for_close)
348 return;
350 log_fn(LOG_DEBUG,"socket %d wants to read.",conn->s);
352 assert_connection_ok(conn, time(NULL));
354 if (connection_handle_read(conn) < 0) {
355 if (!conn->marked_for_close) {
356 #ifndef MS_WINDOWS
357 log_fn(LOG_WARN,"Bug: unhandled error on read for %s connection (fd %d); removing",
358 conn_type_to_string(conn->type), conn->s);
359 tor_fragile_assert();
360 #endif
361 if (CONN_IS_EDGE(conn))
362 connection_edge_end_errno(conn, conn->cpath_layer);
363 connection_mark_for_close(conn);
366 assert_connection_ok(conn, time(NULL));
368 if (smartlist_len(closeable_connection_lst))
369 close_closeable_connections();
372 /** Libevent callback: this gets invoked when (connection_t*)<b>conn</b> has
373 * some data to write. */
374 static void conn_write_callback(int fd, short events, void *_conn)
376 connection_t *conn = _conn;
378 log_fn(LOG_DEBUG,"socket %d wants to write.",conn->s);
379 if (conn->marked_for_close)
380 return;
382 assert_connection_ok(conn, time(NULL));
384 if (connection_handle_write(conn) < 0) {
385 if (!conn->marked_for_close) {
386 /* this connection is broken. remove it. */
387 log_fn(LOG_WARN,"Bug: unhandled error on write for %s connection (fd %d); removing",
388 conn_type_to_string(conn->type), conn->s);
389 tor_fragile_assert();
390 conn->has_sent_end = 1; /* otherwise we cry wolf about duplicate close */
391 /* XXX do we need a close-immediate here, so we don't try to flush? */
392 connection_mark_for_close(conn);
395 assert_connection_ok(conn, time(NULL));
397 if (smartlist_len(closeable_connection_lst))
398 close_closeable_connections();
401 /** If the connection at connection_array[i] is marked for close, then:
402 * - If it has data that it wants to flush, try to flush it.
403 * - If it _still_ has data to flush, and conn->hold_open_until_flushed is
404 * true, then leave the connection open and return.
405 * - Otherwise, remove the connection from connection_array and from
406 * all other lists, close it, and free it.
407 * Returns 1 if the connection was closed, 0 otherwise.
409 static int conn_close_if_marked(int i) {
410 connection_t *conn;
411 int retval;
413 conn = connection_array[i];
414 if (!conn->marked_for_close)
415 return 0; /* nothing to see here, move along */
416 assert_connection_ok(conn, time(NULL));
417 assert_all_pending_dns_resolves_ok();
419 log_fn(LOG_INFO,"Cleaning up connection (fd %d).",conn->s);
420 if (conn->s >= 0 && connection_wants_to_flush(conn)) {
421 /* -1 means it's an incomplete edge connection, or that the socket
422 * has already been closed as unflushable. */
423 if (!conn->hold_open_until_flushed)
424 log_fn(LOG_INFO,
425 "Conn (addr %s, fd %d, type %s, state %d) marked, but wants to flush %d bytes. "
426 "(Marked at %s:%d)",
427 conn->address, conn->s, conn_type_to_string(conn->type), conn->state,
428 (int)conn->outbuf_flushlen, conn->marked_for_close_file, conn->marked_for_close);
429 if (connection_speaks_cells(conn)) {
430 if (conn->state == OR_CONN_STATE_OPEN) {
431 retval = flush_buf_tls(conn->tls, conn->outbuf, &conn->outbuf_flushlen);
432 } else
433 retval = -1; /* never flush non-open broken tls connections */
434 } else {
435 retval = flush_buf(conn->s, conn->outbuf, &conn->outbuf_flushlen);
437 if (retval >= 0 &&
438 conn->hold_open_until_flushed && connection_wants_to_flush(conn)) {
439 log_fn(LOG_INFO,"Holding conn (fd %d) open for more flushing.",conn->s);
440 /* XXX should we reset timestamp_lastwritten here? */
441 return 0;
443 if (connection_wants_to_flush(conn)) {
444 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)",
445 safe_str(conn->address), conn->s, conn_type_to_string(conn->type),
446 conn->state,
447 (int)buf_datalen(conn->outbuf), conn->marked_for_close_file,
448 conn->marked_for_close);
451 connection_unlink(conn, 1); /* unlink, remove, free */
452 return 1;
455 /** We've just tried every dirserver we know about, and none of
456 * them were reachable. Assume the network is down. Change state
457 * so next time an application connection arrives we'll delay it
458 * and try another directory fetch. Kill off all the circuit_wait
459 * streams that are waiting now, since they will all timeout anyway.
461 void directory_all_unreachable(time_t now) {
462 connection_t *conn;
464 has_fetched_directory=0;
465 stats_n_seconds_working=0; /* reset it */
467 while ((conn = connection_get_by_type_state(CONN_TYPE_AP,
468 AP_CONN_STATE_CIRCUIT_WAIT))) {
469 log_fn(LOG_NOTICE,"Network down? Failing connection to '%s:%d'.",
470 safe_str(conn->socks_request->address), conn->socks_request->port);
471 connection_mark_unattached_ap(conn, END_STREAM_REASON_NET_UNREACHABLE);
475 static INLINE int
476 get_dir_fetch_period(or_options_t *options)
478 if (options->DirFetchPeriod)
479 /* Value from config file. */
480 return options->DirFetchPeriod;
481 else if (options->DirPort)
482 /* Default for directory server */
483 return 20*60;
484 else
485 /* Default for average user. */
486 return 40*60;
489 static INLINE int
490 get_status_fetch_period(or_options_t *options)
492 if (options->StatusFetchPeriod)
493 /* Value from config file. */
494 return options->StatusFetchPeriod;
495 else if (options->DirPort)
496 /* Default for directory server */
497 return 15*60;
498 else
499 /* Default for average user. */
500 return 30*60;
503 /** This function is called whenever we successfully pull down a directory.
504 * If <b>identity_digest</b> is defined, it contains the digest of the
505 * router that just gave us this directory. */
506 void directory_has_arrived(time_t now, char *identity_digest) {
507 or_options_t *options = get_options();
509 log_fn(LOG_INFO, "A directory has arrived.");
511 has_fetched_directory=1;
512 /* Don't try to upload or download anything for a while
513 * after the directory we had when we started.
515 if (!time_to_fetch_directory)
516 time_to_fetch_directory = now + get_dir_fetch_period(options);
518 if (!time_to_force_upload_descriptor)
519 time_to_force_upload_descriptor = now + options->DirPostPeriod;
521 if (!time_to_fetch_running_routers)
522 time_to_fetch_running_routers = now + get_status_fetch_period(options);
524 if (server_mode(options) && identity_digest) {
525 /* if this is us, then our dirport is reachable */
526 if (router_digest_is_me(identity_digest))
527 router_dirport_found_reachable();
530 if (server_mode(options) &&
531 !we_are_hibernating()) { /* connect to the appropriate routers */
532 router_retry_connections();
533 if (identity_digest) /* we got a fresh directory */
534 consider_testing_reachability();
538 /** Perform regular maintenance tasks for a single connection. This
539 * function gets run once per second per connection by run_scheduled_events.
541 static void run_connection_housekeeping(int i, time_t now) {
542 cell_t cell;
543 connection_t *conn = connection_array[i];
544 or_options_t *options = get_options();
546 if (conn->outbuf && !buf_datalen(conn->outbuf))
547 conn->timestamp_lastempty = now;
549 /* Expire any directory connections that haven't sent anything for 5 min */
550 if (conn->type == CONN_TYPE_DIR &&
551 !conn->marked_for_close &&
552 conn->timestamp_lastwritten + 5*60 < now) {
553 log_fn(LOG_INFO,"Expiring wedged directory conn (fd %d, purpose %d)",
554 conn->s, conn->purpose);
555 connection_mark_for_close(conn);
556 return;
559 /* If we haven't written to an OR connection for a while, then either nuke
560 the connection or send a keepalive, depending. */
561 if (connection_speaks_cells(conn) &&
562 now >= conn->timestamp_lastwritten + options->KeepalivePeriod) {
563 routerinfo_t *router = router_get_by_digest(conn->identity_digest);
564 if (!connection_state_is_open(conn)) {
565 log_fn(LOG_INFO,"Expiring non-open OR connection to fd %d (%s:%d).",
566 conn->s,conn->address, conn->port);
567 connection_mark_for_close(conn);
568 conn->hold_open_until_flushed = 1;
569 } else if (we_are_hibernating() && !circuit_get_by_conn(conn) &&
570 !buf_datalen(conn->outbuf)) {
571 log_fn(LOG_INFO,"Expiring non-used OR connection to fd %d (%s:%d) [Hibernating or exiting].",
572 conn->s,conn->address, conn->port);
573 connection_mark_for_close(conn);
574 conn->hold_open_until_flushed = 1;
575 } else if (!clique_mode(options) && !circuit_get_by_conn(conn) &&
576 (!router || !server_mode(options) || !router_is_clique_mode(router))) {
577 log_fn(LOG_INFO,"Expiring non-used OR connection to fd %d (%s:%d) [Not in clique mode].",
578 conn->s,conn->address, conn->port);
579 connection_mark_for_close(conn);
580 conn->hold_open_until_flushed = 1;
581 } else if (
582 now >= conn->timestamp_lastempty + options->KeepalivePeriod*10 &&
583 now >= conn->timestamp_lastwritten + options->KeepalivePeriod*10) {
584 log_fn(LOG_NOTICE,"Expiring stuck OR connection to fd %d (%s:%d). (%d bytes to flush; %d seconds since last write)",
585 conn->s, conn->address, conn->port,
586 (int)buf_datalen(conn->outbuf),
587 (int)(now-conn->timestamp_lastwritten));
588 connection_mark_for_close(conn);
589 } else {
590 /* either in clique mode, or we've got a circuit. send a padding cell. */
591 log_fn(LOG_DEBUG,"Sending keepalive to (%s:%d)",
592 conn->address, conn->port);
593 memset(&cell,0,sizeof(cell_t));
594 cell.command = CELL_PADDING;
595 connection_or_write_cell_to_buf(&cell, conn);
600 /** Perform regular maintenance tasks. This function gets run once per
601 * second by prepare_for_poll.
603 static void run_scheduled_events(time_t now) {
604 static time_t last_rotated_certificate = 0;
605 static time_t time_to_check_listeners = 0;
606 static time_t time_to_check_descriptor = 0;
607 static time_t time_to_shrink_buffers = 0;
608 or_options_t *options = get_options();
609 int i;
611 /** 0. See if we've been asked to shut down and our timeout has
612 * expired; or if our bandwidth limits are exhausted and we
613 * should hibernate; or if it's time to wake up from hibernation.
615 consider_hibernation(now);
617 /** 1a. Every MIN_ONION_KEY_LIFETIME seconds, rotate the onion keys,
618 * shut down and restart all cpuworkers, and update the directory if
619 * necessary.
621 if (server_mode(options) &&
622 get_onion_key_set_at()+MIN_ONION_KEY_LIFETIME < now) {
623 log_fn(LOG_INFO,"Rotating onion key.");
624 rotate_onion_key();
625 cpuworkers_rotate();
626 if (router_rebuild_descriptor(1)<0) {
627 log_fn(LOG_WARN, "Couldn't rebuild router descriptor");
629 if (advertised_server_mode())
630 router_upload_dir_desc_to_dirservers(0);
633 /** 1b. Every MAX_SSL_KEY_LIFETIME seconds, we change our TLS context. */
634 if (!last_rotated_certificate)
635 last_rotated_certificate = now;
636 if (last_rotated_certificate+MAX_SSL_KEY_LIFETIME < now) {
637 log_fn(LOG_INFO,"Rotating tls context.");
638 if (tor_tls_context_new(get_identity_key(), 1, options->Nickname,
639 MAX_SSL_KEY_LIFETIME) < 0) {
640 log_fn(LOG_WARN, "Error reinitializing TLS context");
641 /* XXX is it a bug here, that we just keep going? */
643 last_rotated_certificate = now;
644 /* XXXX We should rotate TLS connections as well; this code doesn't change
645 * them at all. */
648 /** 1c. If we have to change the accounting interval or record
649 * bandwidth used in this accounting interval, do so. */
650 if (accounting_is_enabled(options))
651 accounting_run_housekeeping(now);
653 /** 2. Periodically, we consider getting a new directory, getting a
654 * new running-routers list, and/or force-uploading our descriptor
655 * (if we've passed our internal checks). */
656 if (time_to_fetch_directory < now) {
657 time_t next_status_fetch;
658 /* purge obsolete entries */
659 routerlist_remove_old_routers(ROUTER_MAX_AGE);
661 if (authdir_mode(options)) {
662 /* We're a directory; dump any old descriptors. */
663 dirserv_remove_old_servers(ROUTER_MAX_AGE);
665 if (server_mode(options) && !we_are_hibernating()) {
666 /* dirservers try to reconnect, in case connections have failed;
667 * and normal servers try to reconnect to dirservers */
668 router_retry_connections();
671 directory_get_from_dirserver(DIR_PURPOSE_FETCH_DIR, NULL, 1);
672 time_to_fetch_directory = now + get_dir_fetch_period(options);
673 next_status_fetch = now + get_status_fetch_period(options);
674 if (time_to_fetch_running_routers < next_status_fetch) {
675 time_to_fetch_running_routers = next_status_fetch;
678 /* Also, take this chance to remove old information from rephist. */
679 rep_history_clean(now-24*60*60);
682 if (time_to_fetch_running_routers < now) {
683 if (!authdir_mode(options)) {
684 directory_get_from_dirserver(DIR_PURPOSE_FETCH_RUNNING_LIST, NULL, 1);
686 time_to_fetch_running_routers = now + get_status_fetch_period(options);
689 if (time_to_force_upload_descriptor < now) {
690 consider_publishable_server(now, 1);
692 rend_cache_clean(); /* this should go elsewhere? */
694 time_to_force_upload_descriptor = now + options->DirPostPeriod;
697 /* 2b. Once per minute, regenerate and upload the descriptor if the old
698 * one is inaccurate. */
699 if (time_to_check_descriptor < now) {
700 time_to_check_descriptor = now + CHECK_DESCRIPTOR_INTERVAL;
701 consider_publishable_server(now, 0);
702 /* also, check religiously for reachability, if it's within the first
703 * 20 minutes of our uptime. */
704 if (server_mode(options) &&
705 stats_n_seconds_working < TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT &&
706 !we_are_hibernating())
707 consider_testing_reachability();
710 /** 3a. Every second, we examine pending circuits and prune the
711 * ones which have been pending for more than a few seconds.
712 * We do this before step 4, so it can try building more if
713 * it's not comfortable with the number of available circuits.
715 circuit_expire_building(now);
717 /** 3b. Also look at pending streams and prune the ones that 'began'
718 * a long time ago but haven't gotten a 'connected' yet.
719 * Do this before step 4, so we can put them back into pending
720 * state to be picked up by the new circuit.
722 connection_ap_expire_beginning();
724 /** 3c. And expire connections that we've held open for too long.
726 connection_expire_held_open();
728 /** 3d. And every 60 seconds, we relaunch listeners if any died. */
729 if (!we_are_hibernating() && time_to_check_listeners < now) {
730 retry_all_listeners(0); /* 0 means "only if some died." */
731 time_to_check_listeners = now+60;
734 /** 4. Every second, we try a new circuit if there are no valid
735 * circuits. Every NewCircuitPeriod seconds, we expire circuits
736 * that became dirty more than MaxCircuitDirtiness seconds ago,
737 * and we make a new circ if there are no clean circuits.
739 if (has_fetched_directory && !we_are_hibernating())
740 circuit_build_needed_circs(now);
742 /** 5. We do housekeeping for each connection... */
743 for (i=0;i<nfds;i++) {
744 run_connection_housekeeping(i, now);
746 if (time_to_shrink_buffers < now) {
747 for (i=0;i<nfds;i++) {
748 connection_t *conn = connection_array[i];
749 if (conn->outbuf)
750 buf_shrink(conn->outbuf);
751 if (conn->inbuf)
752 buf_shrink(conn->inbuf);
754 time_to_shrink_buffers = now + BUF_SHRINK_INTERVAL;
757 /** 6. And remove any marked circuits... */
758 circuit_close_all_marked();
760 /** 7. And upload service descriptors if necessary. */
761 if (has_fetched_directory && !we_are_hibernating())
762 rend_consider_services_upload(now);
764 /** 8. and blow away any connections that need to die. have to do this now,
765 * because if we marked a conn for close and left its socket -1, then
766 * we'll pass it to poll/select and bad things will happen.
768 close_closeable_connections();
771 /** Libevent callback: invoked once every second. */
772 static void second_elapsed_callback(int fd, short event, void *args)
774 static struct event *timeout_event = NULL;
775 static struct timeval one_second;
776 static long current_second = 0;
777 struct timeval now;
778 size_t bytes_written;
779 size_t bytes_read;
780 int seconds_elapsed;
781 or_options_t *options = get_options();
782 if (!timeout_event) {
783 /* XXX NM: We don't free timeout_event on exit. */
784 timeout_event = tor_malloc_zero(sizeof(struct event));
785 evtimer_set(timeout_event, second_elapsed_callback, NULL);
786 one_second.tv_sec = 1;
787 one_second.tv_usec = 0;
790 /* log_fn(LOG_NOTICE, "Tick."); */
791 tor_gettimeofday(&now);
793 /* the second has rolled over. check more stuff. */
794 bytes_written = stats_prev_global_write_bucket - global_write_bucket;
795 bytes_read = stats_prev_global_read_bucket - global_read_bucket;
796 /* XXX below we get suspicious if time jumps forward more than 10
797 * seconds, but we never notice if it jumps *back* more than 10 seconds.
798 * This could be useful for detecting that we just NTP'ed to three
799 * weeks ago and it will be 3 weeks and 15 minutes until any of our
800 * events trigger.
802 seconds_elapsed = current_second ? (now.tv_sec - current_second) : 0;
803 stats_n_bytes_read += bytes_read;
804 stats_n_bytes_written += bytes_written;
805 if (accounting_is_enabled(options))
806 accounting_add_bytes(bytes_read, bytes_written, seconds_elapsed);
807 control_event_bandwidth_used((uint32_t)bytes_read,(uint32_t)bytes_written);
809 connection_bucket_refill(&now);
810 stats_prev_global_read_bucket = global_read_bucket;
811 stats_prev_global_write_bucket = global_write_bucket;
813 if (server_mode(options) &&
814 !we_are_hibernating() &&
815 stats_n_seconds_working / TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT !=
816 (stats_n_seconds_working+seconds_elapsed) /
817 TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT) {
818 /* every 20 minutes, check and complain if necessary */
819 routerinfo_t *me = router_get_my_routerinfo();
820 if (!check_whether_orport_reachable())
821 log_fn(LOG_WARN,"Your server (%s:%d) has not managed to confirm that its ORPort is reachable. Please check your firewalls, ports, address, etc.",
822 me ? me->address : options->Address, options->ORPort);
823 if (!check_whether_dirport_reachable())
824 log_fn(LOG_WARN,"Your server (%s:%d) has not managed to confirm that its DirPort is reachable. Please check your firewalls, ports, address, etc.",
825 me ? me->address : options->Address, options->DirPort);
828 /* if more than 10s have elapsed, probably the clock jumped: doesn't count. */
829 if (seconds_elapsed < 100)
830 stats_n_seconds_working += seconds_elapsed;
831 else
832 circuit_note_clock_jumped(seconds_elapsed);
834 run_scheduled_events(now.tv_sec);
836 current_second = now.tv_sec; /* remember which second it is, for next time */
838 if (evtimer_add(timeout_event, &one_second))
839 log_fn(LOG_ERR,
840 "Error from libevent when setting one-second timeout event");
843 /** Called when we get a SIGHUP: reload configuration files and keys,
844 * retry all connections, re-upload all descriptors, and so on. */
845 static int do_hup(void) {
846 char keydir[512];
847 or_options_t *options = get_options();
849 log(LOG_NOTICE,"Received sighup. Reloading config.");
850 has_completed_circuit=0;
851 if (accounting_is_enabled(options))
852 accounting_record_bandwidth_usage(time(NULL));
854 addressmap_clear_transient();
855 /* first, reload config variables, in case they've changed */
856 /* no need to provide argc/v, they've been cached inside init_from_config */
857 if (init_from_config(0, NULL) < 0) {
858 log_fn(LOG_ERR,"Reading config failed--see warnings above. For usage, try -h.");
859 return -1;
861 options = get_options(); /* they have changed now */
862 if (authdir_mode(options)) {
863 /* reload the approved-routers file */
864 tor_snprintf(keydir,sizeof(keydir),"%s/approved-routers", options->DataDirectory);
865 log_fn(LOG_INFO,"Reloading approved fingerprints from %s...",keydir);
866 if (dirserv_parse_fingerprint_file(keydir) < 0) {
867 log_fn(LOG_NOTICE, "Error reloading fingerprints. Continuing with old list.");
870 /* Fetch a new directory. Even authdirservers do this. */
871 directory_get_from_dirserver(DIR_PURPOSE_FETCH_DIR, NULL, 1);
872 if (server_mode(options)) {
873 const char *descriptor;
874 /* Restart cpuworker and dnsworker processes, so they get up-to-date
875 * configuration options. */
876 cpuworkers_rotate();
877 dnsworkers_rotate();
878 /* Rebuild fresh descriptor, but leave old one on failure. */
879 router_rebuild_descriptor(1);
880 descriptor = router_get_my_descriptor();
881 if (!descriptor) {
882 log_fn(LOG_WARN,"No descriptor to save.");
883 return 0;
885 tor_snprintf(keydir,sizeof(keydir),"%s/router.desc",
886 options->DataDirectory);
887 log_fn(LOG_INFO,"Saving descriptor to %s...",keydir);
888 if (write_str_to_file(keydir, descriptor, 0)) {
889 return 0;
892 return 0;
895 /** Tor main loop. */
896 static int do_main_loop(void) {
897 int loop_result;
899 /* load the private keys, if we're supposed to have them, and set up the
900 * TLS context. */
901 if (! identity_key_is_set()) {
902 if (init_keys() < 0) {
903 log_fn(LOG_ERR,"Error initializing keys; exiting");
904 return -1;
908 /* Set up our buckets */
909 connection_bucket_init();
910 stats_prev_global_read_bucket = global_read_bucket;
911 stats_prev_global_write_bucket = global_write_bucket;
913 /* load the routers file, or assign the defaults. */
914 if (router_reload_router_list()) {
915 return -1;
918 if (authdir_mode(get_options())) {
919 /* the directory is already here, run startup things */
920 router_retry_connections();
923 if (server_mode(get_options())) {
924 /* launch cpuworkers. Need to do this *after* we've read the onion key. */
925 cpu_init();
928 /* set up once-a-second callback. */
929 second_elapsed_callback(0,0,NULL);
931 for (;;) {
932 if (nt_service_is_stopped())
933 return 0;
935 #ifndef MS_WINDOWS
936 /* Make it easier to tell whether libevent failure is our fault or not. */
937 errno = 0;
938 #endif
939 /* poll until we have an event, or the second ends */
940 loop_result = event_dispatch();
942 /* let catch() handle things like ^c, and otherwise don't worry about it */
943 if (loop_result < 0) {
944 int e = errno;
945 /* let the program survive things like ^z */
946 if (e != EINTR) {
947 #ifdef HAVE_EVENT_GET_METHOD
948 log_fn(LOG_ERR,"libevent poll with %s failed: %s [%d]",
949 event_get_method(), tor_socket_strerror(e), e);
950 #else
951 log_fn(LOG_ERR,"libevent poll failed: %s [%d]",
952 tor_socket_strerror(e), e);
953 #endif
954 return -1;
955 } else {
956 log_fn(LOG_DEBUG,"event poll interrupted.");
957 /* You can't trust the results of this poll(). Go back to the
958 * top of the big for loop. */
959 continue;
963 /* refilling buckets and sending cells happens at the beginning of the
964 * next iteration of the loop, inside prepare_for_poll()
965 * XXXX No longer so.
970 /** Used to implement the SIGNAL control command: if we accept
971 * <b>the_signal</b> as a remote pseudo-signal, then act on it and
972 * return 0. Else return -1. */
973 /* We don't re-use catch() here because:
974 * 1. We handle a different set of signals than those allowed in catch.
975 * 2. Platforms without signal() are unlikely to define SIGfoo.
976 * 3. The control spec is defined to use fixed numeric signal values
977 * which just happen to match the unix values.
980 control_signal_act(int the_signal)
982 switch (the_signal)
984 case 1:
985 signal_callback(0,0,(void*)(uintptr_t)SIGHUP);
986 break;
987 case 2:
988 signal_callback(0,0,(void*)(uintptr_t)SIGINT);
989 break;
990 case 10:
991 signal_callback(0,0,(void*)(uintptr_t)SIGUSR1);
992 break;
993 case 12:
994 signal_callback(0,0,(void*)(uintptr_t)SIGUSR2);
995 break;
996 case 15:
997 signal_callback(0,0,(void*)(uintptr_t)SIGTERM);
998 break;
999 default:
1000 return -1;
1002 return 0;
1005 static void signal_callback(int fd, short events, void *arg)
1007 uintptr_t sig = (uintptr_t)arg;
1008 switch (sig)
1010 case SIGTERM:
1011 log(LOG_ERR,"Catching signal TERM, exiting cleanly.");
1012 tor_cleanup();
1013 exit(0);
1014 break;
1015 case SIGINT:
1016 if (!server_mode(get_options())) { /* do it now */
1017 log(LOG_NOTICE,"Interrupt: exiting cleanly.");
1018 tor_cleanup();
1019 exit(0);
1021 hibernate_begin_shutdown();
1022 break;
1023 #ifdef SIGPIPE
1024 case SIGPIPE:
1025 log(LOG_NOTICE,"Caught sigpipe. Ignoring.");
1026 break;
1027 #endif
1028 case SIGUSR1:
1029 /* prefer to log it at INFO, but make sure we always see it */
1030 dumpstats(get_min_log_level()<LOG_INFO ? get_min_log_level() : LOG_INFO);
1031 break;
1032 case SIGUSR2:
1033 switch_logs_debug();
1034 log(LOG_NOTICE,"Caught USR2. Going to loglevel debug.");
1035 break;
1036 case SIGHUP:
1037 if (do_hup() < 0) {
1038 log_fn(LOG_WARN,"Restart failed (config error?). Exiting.");
1039 tor_cleanup();
1040 exit(1);
1042 break;
1043 #ifdef SIGCHLD
1044 case SIGCHLD:
1045 while (waitpid(-1,NULL,WNOHANG) > 0) ; /* keep reaping until no more zombies */
1046 break;
1047 #endif
1051 /** Write all statistics to the log, with log level 'severity'. Called
1052 * in response to a SIGUSR1. */
1053 static void
1054 dumpstats(int severity) {
1055 int i;
1056 connection_t *conn;
1057 time_t now = time(NULL);
1058 time_t elapsed;
1060 log(severity, "Dumping stats:");
1062 for (i=0;i<nfds;i++) {
1063 conn = connection_array[i];
1064 log(severity, "Conn %d (socket %d) type %d (%s), state %d (%s), created %d secs ago",
1065 i, conn->s, conn->type, conn_type_to_string(conn->type),
1066 conn->state, conn_state_to_string(conn->type, conn->state), (int)(now - conn->timestamp_created));
1067 if (!connection_is_listener(conn)) {
1068 log(severity,"Conn %d is to '%s:%d'.",i,safe_str(conn->address), conn->port);
1069 log(severity,"Conn %d: %d bytes waiting on inbuf (len %d, last read %d secs ago)",i,
1070 (int)buf_datalen(conn->inbuf),
1071 (int)buf_capacity(conn->inbuf),
1072 (int)(now - conn->timestamp_lastread));
1073 log(severity,"Conn %d: %d bytes waiting on outbuf (len %d, last written %d secs ago)",i,
1074 (int)buf_datalen(conn->outbuf),
1075 (int)buf_capacity(conn->outbuf),
1076 (int)(now - conn->timestamp_lastwritten));
1078 circuit_dump_by_conn(conn, severity); /* dump info about all the circuits using this conn */
1080 log(severity,
1081 "Cells processed: %10lu padding\n"
1082 " %10lu create\n"
1083 " %10lu created\n"
1084 " %10lu relay\n"
1085 " (%10lu relayed)\n"
1086 " (%10lu delivered)\n"
1087 " %10lu destroy",
1088 stats_n_padding_cells_processed,
1089 stats_n_create_cells_processed,
1090 stats_n_created_cells_processed,
1091 stats_n_relay_cells_processed,
1092 stats_n_relay_cells_relayed,
1093 stats_n_relay_cells_delivered,
1094 stats_n_destroy_cells_processed);
1095 if (stats_n_data_cells_packaged)
1096 log(severity,"Average packaged cell fullness: %2.3f%%",
1097 100*(((double)stats_n_data_bytes_packaged) /
1098 (stats_n_data_cells_packaged*RELAY_PAYLOAD_SIZE)) );
1099 if (stats_n_data_cells_received)
1100 log(severity,"Average delivered cell fullness: %2.3f%%",
1101 100*(((double)stats_n_data_bytes_received) /
1102 (stats_n_data_cells_received*RELAY_PAYLOAD_SIZE)) );
1104 if (now - time_of_process_start >= 0)
1105 elapsed = now - time_of_process_start;
1106 else
1107 elapsed = 0;
1109 if (elapsed) {
1110 log(severity,
1111 "Average bandwidth: "U64_FORMAT"/%d = %d bytes/sec reading",
1112 U64_PRINTF_ARG(stats_n_bytes_read),
1113 (int)elapsed,
1114 (int) (stats_n_bytes_read/elapsed));
1115 log(severity,
1116 "Average bandwidth: "U64_FORMAT"/%d = %d bytes/sec writing",
1117 U64_PRINTF_ARG(stats_n_bytes_written),
1118 (int)elapsed,
1119 (int) (stats_n_bytes_written/elapsed));
1122 rep_hist_dump_stats(now,severity);
1123 rend_service_dump_stats(severity);
1126 /** Called by exit() as we shut down the process.
1128 static void exit_function(void)
1130 /* NOTE: If we ever daemonize, this gets called immediately. That's
1131 * okay for now, because we only use this on Windows. */
1132 #ifdef MS_WINDOWS
1133 WSACleanup();
1134 #endif
1137 /** Set up the signal handlers for either parent or child. */
1138 void handle_signals(int is_parent)
1140 #ifndef MS_WINDOWS /* do signal stuff only on unix */
1141 int i;
1142 static int signals[] = {
1143 SIGINT, /* do a controlled slow shutdown */
1144 SIGTERM, /* to terminate now */
1145 SIGPIPE, /* otherwise sigpipe kills us */
1146 SIGUSR1, /* dump stats */
1147 SIGUSR2, /* go to loglevel debug */
1148 SIGHUP, /* to reload config, retry conns, etc */
1149 #ifdef SIGXFSZ
1150 SIGXFSZ, /* handle file-too-big resource exhaustion */
1151 #endif
1152 SIGCHLD, /* handle dns/cpu workers that exit */
1153 -1 };
1154 static struct event signal_events[16]; /* bigger than it has to be. */
1155 if (is_parent) {
1156 for (i = 0; signals[i] >= 0; ++i) {
1157 signal_set(&signal_events[i], signals[i], signal_callback,
1158 (void*)(uintptr_t)signals[i]);
1159 if (signal_add(&signal_events[i], NULL))
1160 log_fn(LOG_WARN, "Error from libevent when adding event for signal %d",
1161 signals[i]);
1163 } else {
1164 struct sigaction action;
1165 action.sa_flags = 0;
1166 sigemptyset(&action.sa_mask);
1167 action.sa_handler = SIG_IGN;
1168 sigaction(SIGINT, &action, NULL);
1169 sigaction(SIGTERM, &action, NULL);
1170 sigaction(SIGPIPE, &action, NULL);
1171 sigaction(SIGUSR1, &action, NULL);
1172 sigaction(SIGUSR2, &action, NULL);
1173 sigaction(SIGHUP, &action, NULL);
1174 #ifdef SIGXFSZ
1175 sigaction(SIGXFSZ, &action, NULL);
1176 #endif
1178 #endif /* signal stuff */
1181 /** Main entry point for the Tor command-line client.
1183 static int tor_init(int argc, char *argv[]) {
1184 time_of_process_start = time(NULL);
1185 closeable_connection_lst = smartlist_create();
1186 /* Initialize the history structures. */
1187 rep_hist_init();
1188 /* Initialize the service cache. */
1189 rend_cache_init();
1190 addressmap_init(); /* Init the client dns cache. Do it always, since it's cheap. */
1192 /* give it somewhere to log to initially */
1193 add_temp_log();
1195 log(LOG_NOTICE,"Tor v%s. This is experimental software. Do not rely on it for strong anonymity.",VERSION);
1197 if (network_init()<0) {
1198 log_fn(LOG_ERR,"Error initializing network; exiting.");
1199 return -1;
1201 atexit(exit_function);
1203 if (init_from_config(argc,argv) < 0) {
1204 log_fn(LOG_ERR,"Reading config failed--see warnings above. For usage, try -h.");
1205 return -1;
1208 #ifndef MS_WINDOWS
1209 if (geteuid()==0)
1210 log_fn(LOG_WARN,"You are running Tor as root. You don't need to, and you probably shouldn't.");
1211 #endif
1213 /* only spawn dns handlers if we're a router */
1214 if (server_mode(get_options()) && get_options()->command == CMD_RUN_TOR) {
1215 dns_init(); /* initialize the dns resolve tree, and spawn workers */
1216 /* XXX really, this should get moved to do_main_loop */
1219 handle_signals(1);
1221 crypto_global_init();
1222 crypto_seed_rng();
1223 return 0;
1226 /** Free all memory that we might have allocated somewhere.
1227 * Helps us find the real leaks with dmalloc and the like.
1229 * Also valgrind should then report 0 reachable in its
1230 * leak report */
1231 void tor_free_all(int postfork)
1233 routerlist_free_current();
1234 free_trusted_dir_servers();
1235 addressmap_free_all();
1236 set_exit_redirects(NULL); /* free the registered exit redirects */
1237 free_socks_policy();
1238 free_dir_policy();
1239 dirserv_free_all();
1240 rend_service_free_all();
1241 rend_cache_free_all();
1242 rep_hist_free_all();
1243 dns_free_all();
1244 clear_pending_onions();
1245 circuit_free_all();
1246 connection_free_all();
1247 if (!postfork) {
1248 config_free_all();
1249 router_free_all_keys();
1251 tor_tls_free_all();
1252 /* stuff in main.c */
1253 smartlist_free(closeable_connection_lst);
1255 if (!postfork) {
1256 close_logs(); /* free log strings. do this last so logs keep working. */
1260 /** Do whatever cleanup is necessary before shutting Tor down. */
1261 void tor_cleanup(void) {
1262 or_options_t *options = get_options();
1263 /* Remove our pid file. We don't care if there was an error when we
1264 * unlink, nothing we could do about it anyways. */
1265 if (options->PidFile && options->command == CMD_RUN_TOR)
1266 unlink(options->PidFile);
1267 if (accounting_is_enabled(options))
1268 accounting_record_bandwidth_usage(time(NULL));
1269 tor_free_all(0); /* move tor_free_all back into the ifdef below later. XXX*/
1270 crypto_global_cleanup();
1271 #ifdef USE_DMALLOC
1272 dmalloc_log_unfreed();
1273 dmalloc_shutdown();
1274 #endif
1277 /** Read/create keys as needed, and echo our fingerprint to stdout. */
1278 static void do_list_fingerprint(void)
1280 char buf[FINGERPRINT_LEN+1];
1281 crypto_pk_env_t *k;
1282 const char *nickname = get_options()->Nickname;
1283 if (!server_mode(get_options())) {
1284 printf("Clients don't have long-term identity keys. Exiting.\n");
1285 return;
1287 tor_assert(nickname);
1288 if (init_keys() < 0) {
1289 log_fn(LOG_ERR,"Error initializing keys; exiting");
1290 return;
1292 if (!(k = get_identity_key())) {
1293 log_fn(LOG_ERR,"Error: missing identity key.");
1294 return;
1296 if (crypto_pk_get_fingerprint(k, buf, 1)<0) {
1297 log_fn(LOG_ERR, "Error computing fingerprint");
1298 return;
1300 printf("%s %s\n", nickname, buf);
1303 /** Entry point for password hashing: take the desired password from
1304 * the command line, and print its salted hash to stdout. **/
1305 static void do_hash_password(void)
1308 char output[256];
1309 char key[S2K_SPECIFIER_LEN+DIGEST_LEN];
1311 crypto_rand(key, S2K_SPECIFIER_LEN-1);
1312 key[S2K_SPECIFIER_LEN-1] = (uint8_t)96; /* Hash 64 K of data. */
1313 secret_to_key(key+S2K_SPECIFIER_LEN, DIGEST_LEN,
1314 get_options()->command_arg, strlen(get_options()->command_arg),
1315 key);
1316 base16_encode(output, sizeof(output), key, sizeof(key));
1317 printf("16:%s\n",output);
1320 #ifdef MS_WINDOWS_SERVICE
1321 /** If we're compile to run as an NT service, and the service has been
1322 * shut down, then change our current status and return 1. Else
1323 * return 0.
1325 static int
1326 nt_service_is_stopped(void)
1328 if (service_status.dwCurrentState == SERVICE_STOP_PENDING) {
1329 service_status.dwWin32ExitCode = 0;
1330 service_status.dwCurrentState = SERVICE_STOPPED;
1331 SetServiceStatus(hStatus, &service_status);
1332 return 1;
1334 return 0;
1337 void nt_service_control(DWORD request)
1339 switch (request) {
1340 case SERVICE_CONTROL_STOP:
1341 case SERVICE_CONTROL_SHUTDOWN:
1342 log(LOG_ERR, "Got stop/shutdown request; shutting down cleanly.");
1343 service_status.dwCurrentState = SERVICE_STOP_PENDING;
1344 return;
1346 SetServiceStatus(hStatus, &service_status);
1349 void nt_service_body(int argc, char **argv)
1351 int err;
1352 service_status.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
1353 service_status.dwCurrentState = SERVICE_START_PENDING;
1354 service_status.dwControlsAccepted =
1355 SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
1356 service_status.dwWin32ExitCode = 0;
1357 service_status.dwServiceSpecificExitCode = 0;
1358 service_status.dwCheckPoint = 0;
1359 service_status.dwWaitHint = 1000;
1360 hStatus = RegisterServiceCtrlHandler(GENSRV_SERVICENAME, (LPHANDLER_FUNCTION) nt_service_control);
1362 if (hStatus == 0) {
1363 // failed;
1364 return;
1367 err = tor_init(backup_argc, backup_argv); // refactor this part out of tor_main and do_main_loop
1368 if (err) {
1369 // failed.
1370 service_status.dwCurrentState = SERVICE_STOPPED;
1371 service_status.dwWin32ExitCode = -1;
1372 SetServiceStatus(hStatus, &service_status);
1373 return;
1375 service_status.dwCurrentState = SERVICE_RUNNING;
1376 SetServiceStatus(hStatus, &service_status);
1377 do_main_loop();
1378 tor_cleanup();
1379 return;
1382 void nt_service_main(void)
1384 SERVICE_TABLE_ENTRY table[2];
1385 DWORD result = 0;
1386 table[0].lpServiceName = GENSRV_SERVICENAME;
1387 table[0].lpServiceProc = (LPSERVICE_MAIN_FUNCTION)nt_service_body;
1388 table[1].lpServiceName = NULL;
1389 table[1].lpServiceProc = NULL;
1391 if (!StartServiceCtrlDispatcher(table)) {
1392 result = GetLastError();
1393 printf("Error was %d\n",result);
1394 if (result == ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) {
1395 if (tor_init(backup_argc, backup_argv) < 0)
1396 return;
1397 switch (get_options()->command) {
1398 case CMD_RUN_TOR:
1399 do_main_loop();
1400 break;
1401 case CMD_LIST_FINGERPRINT:
1402 do_list_fingerprint();
1403 break;
1404 case CMD_HASH_PASSWORD:
1405 do_hash_password();
1406 break;
1407 case CMD_VERIFY_CONFIG:
1408 printf("Configuration was valid\n");
1409 break;
1410 default:
1411 log_fn(LOG_ERR, "Illegal command number %d: internal error.", get_options()->command);
1413 tor_cleanup();
1418 int nt_service_install()
1420 /* XXXX Problems with NT services:
1421 * 1. The configuration file needs to be in the same directory as the .exe
1423 * 2. The exe and the configuration file can't be on any directory path
1424 * that contains a space.
1425 * mje - you can quote the string (i.e., "c:\program files")
1427 * 3. Ideally, there should be one EXE that can either run as a
1428 * separate process (as now) or that can install and run itself
1429 * as an NT service. I have no idea how hard this is.
1430 * mje - should be done. It can install and run itself as a service
1432 * Notes about developing NT services:
1434 * 1. Don't count on your CWD. If an absolute path is not given, the
1435 * fopen() function goes wrong.
1436 * 2. The parameters given to the nt_service_body() function differ
1437 * from those given to main() function.
1440 SC_HANDLE hSCManager = NULL;
1441 SC_HANDLE hService = NULL;
1442 SERVICE_DESCRIPTION sdBuff;
1443 TCHAR szPath[_MAX_PATH];
1444 TCHAR szDrive[_MAX_DRIVE];
1445 TCHAR szDir[_MAX_DIR];
1446 char cmd1[] = " -f ";
1447 char cmd2[] = "\\torrc";
1448 char *command;
1449 int len = 0;
1451 if (0 == GetModuleFileName(NULL, szPath, MAX_PATH))
1452 return 0;
1454 _tsplitpath(szPath, szDrive, szDir, NULL, NULL);
1456 /* Account for the extra quotes */
1457 //len = _MAX_PATH + strlen(cmd1) + _MAX_DRIVE + _MAX_DIR + strlen(cmd2);
1458 len = _MAX_PATH + strlen(cmd1) + _MAX_DRIVE + _MAX_DIR + strlen(cmd2) + 64;
1459 command = tor_malloc(len);
1461 /* Create a quoted command line, like "c:\with spaces\tor.exe" -f
1462 * "c:\with spaces\tor.exe"
1464 if (tor_snprintf(command, len, "\"%s\" --nt-service -f \"%s%storrc\"",
1465 szPath, szDrive, szDir)<0) {
1466 printf("Failed: tor_snprinf()\n");
1467 free(command);
1468 return 0;
1471 if ((hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CREATE_SERVICE)) == NULL) {
1472 printf("Failed: OpenSCManager()\n");
1473 free(command);
1474 return 0;
1477 /* 1/26/2005 mje
1478 * - changed the service start type to auto
1479 * - and changed the lpPassword param to "" instead of NULL as per an
1480 * MSDN article.
1482 if ((hService = CreateService(hSCManager, GENSRV_SERVICENAME, GENSRV_DISPLAYNAME,
1483 SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
1484 SERVICE_AUTO_START, SERVICE_ERROR_IGNORE, command,
1485 NULL, NULL, NULL, NULL, "")) == NULL) {
1486 printf("Failed: CreateService()\n");
1487 CloseServiceHandle(hSCManager);
1488 free(command);
1489 return 0;
1492 /* Start the service initially, so you don't have to muck with it in the SCM
1494 /* Set the service's description */
1495 sdBuff.lpDescription = GENSRV_DESCRIPTION;
1496 ChangeServiceConfig2(hService, SERVICE_CONFIG_DESCRIPTION, &sdBuff);
1498 /* Start the service, so you don't have to muck with it in the SCM */
1499 if (StartService(hService, 0, NULL)) {
1500 /* Loop until the service has finished attempting to start */
1501 while (QueryServiceStatus(hService, &service_status) &&
1502 service_status.dwCurrentState == SERVICE_START_PENDING)
1503 Sleep(500);
1505 /* Check if it started successfully or not */
1506 if (service_status.dwCurrentState == SERVICE_RUNNING)
1507 printf("Service installed and started successfully.\n");
1508 else
1509 printf("Service installed, but failed to start.\n");
1510 } else {
1511 printf("Service installed, but failed to start.\n");
1514 CloseServiceHandle(hService);
1515 CloseServiceHandle(hSCManager);
1516 tor_free(command);
1518 return 0;
1521 int nt_service_remove()
1523 SC_HANDLE hSCManager = NULL;
1524 SC_HANDLE hService = NULL;
1525 SERVICE_STATUS service_status;
1526 BOOL result = FALSE;
1528 if ((hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CREATE_SERVICE)) == NULL) {
1529 printf("Failed: OpenSCManager()\n");
1530 return 0;
1533 if ((hService = OpenService(hSCManager, GENSRV_SERVICENAME, SERVICE_ALL_ACCESS)) == NULL) {
1534 printf("Failed: OpenService()\n");
1535 CloseServiceHandle(hSCManager);
1536 return 0;
1539 result = ControlService(hService, SERVICE_CONTROL_STOP, &service_status);
1540 if (result) {
1541 while (QueryServiceStatus(hService, &service_status))
1543 if (service_status.dwCurrentState == SERVICE_STOP_PENDING)
1544 Sleep(500);
1545 else
1546 break;
1548 if (DeleteService(hService))
1549 printf("Removed service successfully\n");
1550 else
1551 printf("Failed: DeleteService()\n");
1552 } else {
1553 result = DeleteService(hService);
1554 if (result)
1555 printf("Removed service successfully\n");
1556 else
1557 printf("Failed: DeleteService()\n");
1560 CloseServiceHandle(hService);
1561 CloseServiceHandle(hSCManager);
1563 return 0;
1565 #endif
1567 int tor_main(int argc, char *argv[]) {
1568 #ifdef MS_WINDOWS_SERVICE
1569 backup_argv = argv;
1570 backup_argc = argc;
1571 if ((argc >= 2) && !strcmp(argv[1], "-install"))
1572 return nt_service_install();
1573 if ((argc >= 2) && !strcmp(argv[1], "-remove"))
1574 return nt_service_remove();
1575 if ((argc >= 2) && !strcmp(argv[1], "--nt-service")) {
1576 nt_service_main();
1577 return 0;
1579 #endif
1580 if (tor_init(argc, argv)<0)
1581 return -1;
1582 switch (get_options()->command) {
1583 case CMD_RUN_TOR:
1584 #ifdef MS_WINDOWS_SERVICE
1585 service_status.dwCurrentState = SERVICE_RUNNING;
1586 #endif
1587 do_main_loop();
1588 break;
1589 case CMD_LIST_FINGERPRINT:
1590 do_list_fingerprint();
1591 break;
1592 case CMD_HASH_PASSWORD:
1593 do_hash_password();
1594 break;
1595 case CMD_VERIFY_CONFIG:
1596 printf("Configuration was valid\n");
1597 break;
1598 default:
1599 log_fn(LOG_ERR, "Illegal command number %d: internal error.",
1600 get_options()->command);
1602 tor_cleanup();
1603 return -1;