clean up this TOR_FRAGILE business
[tor.git] / src / or / main.c
blobe2abfaeddc61ffd68d13abb626f3719f42bccf07
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);
43 void tor_free_all(void);
45 /********* START VARIABLES **********/
47 int global_read_bucket; /**< Max number of bytes I can read this second. */
48 int global_write_bucket; /**< Max number of bytes I can write this second. */
50 /** What was the read bucket before the last call to prepare_for_pool?
51 * (used to determine how many bytes we've read). */
52 static int stats_prev_global_read_bucket;
53 /** What was the write bucket before the last call to prepare_for_pool?
54 * (used to determine how many bytes we've written). */
55 static int stats_prev_global_write_bucket;
56 /** How many bytes have we read/written since we started the process? */
57 static uint64_t stats_n_bytes_read = 0;
58 static uint64_t stats_n_bytes_written = 0;
59 /** What time did this process start up? */
60 long time_of_process_start = 0;
61 /** How many seconds have we been running? */
62 long stats_n_seconds_working = 0;
63 /** When do we next download a directory? */
64 static time_t time_to_fetch_directory = 0;
65 /** When do we next upload our descriptor? */
66 static time_t time_to_force_upload_descriptor = 0;
67 /** When do we next download a running-routers summary? */
68 static time_t time_to_fetch_running_routers = 0;
70 /** Array of all open connections; each element corresponds to the element of
71 * poll_array in the same position. The first nfds elements are valid. */
72 static connection_t *connection_array[MAXCONNECTIONS+1] =
73 { NULL };
74 static smartlist_t *closeable_connection_lst = NULL;
76 static int nfds=0; /**< Number of connections currently active. */
78 /** We set this to 1 when we've fetched a dir, to know whether to complain
79 * yet about unrecognized nicknames in entrynodes, exitnodes, etc.
80 * Also, we don't try building circuits unless this is 1. */
81 int has_fetched_directory=0;
83 /** We set this to 1 when we've opened a circuit, so we can print a log
84 * entry to inform the user that Tor is working. */
85 int has_completed_circuit=0;
87 #ifdef MS_WINDOWS
88 #define MS_WINDOWS_SERVICE
89 #endif
91 #ifdef MS_WINDOWS_SERVICE
92 #include <tchar.h>
93 #define GENSRV_SERVICENAME TEXT("tor")
94 #define GENSRV_DISPLAYNAME TEXT("Tor Win32 Service")
95 #define GENSRV_DESCRIPTION TEXT("Provides an anonymous Internet communication system")
96 SERVICE_STATUS service_status;
97 SERVICE_STATUS_HANDLE hStatus;
98 static char **backup_argv;
99 static int backup_argc;
100 static int nt_service_is_stopped(void);
101 #else
102 #define nt_service_is_stopped() (0)
103 #endif
105 #define CHECK_DESCRIPTOR_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 (int)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 (int)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 (int)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 (int)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 (int)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 (int)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 conn->address, conn->s, conn_type_to_string(conn->type), conn->state,
446 (int)buf_datalen(conn->outbuf), conn->marked_for_close_file,
447 conn->marked_for_close);
450 connection_unlink(conn, 1); /* unlink, remove, free */
451 return 1;
454 /** We've just tried every dirserver we know about, and none of
455 * them were reachable. Assume the network is down. Change state
456 * so next time an application connection arrives we'll delay it
457 * and try another directory fetch. Kill off all the circuit_wait
458 * streams that are waiting now, since they will all timeout anyway.
460 void directory_all_unreachable(time_t now) {
461 connection_t *conn;
463 has_fetched_directory=0;
464 stats_n_seconds_working=0; /* reset it */
466 while ((conn = connection_get_by_type_state(CONN_TYPE_AP,
467 AP_CONN_STATE_CIRCUIT_WAIT))) {
468 log_fn(LOG_NOTICE,"Network down? Failing connection to '%s:%d'.",
469 conn->socks_request->address, conn->socks_request->port);
470 connection_mark_unattached_ap(conn, END_STREAM_REASON_NET_UNREACHABLE);
474 static INLINE int
475 get_dir_fetch_period(or_options_t *options)
477 if (options->DirFetchPeriod)
478 /* Value from config file. */
479 return options->DirFetchPeriod;
480 else if (options->DirPort)
481 /* Default for directory server */
482 return 20*60;
483 else
484 /* Default for average user. */
485 return 40*60;
488 static INLINE int
489 get_status_fetch_period(or_options_t *options)
491 if (options->StatusFetchPeriod)
492 /* Value from config file. */
493 return options->StatusFetchPeriod;
494 else if (options->DirPort)
495 /* Default for directory server */
496 return 15*60;
497 else
498 /* Default for average user. */
499 return 30*60;
502 /** This function is called whenever we successfully pull down a directory.
503 * If <b>identity_digest</b> is defined, it contains the digest of the
504 * router that just gave us this directory. */
505 void directory_has_arrived(time_t now, char *identity_digest) {
506 or_options_t *options = get_options();
508 log_fn(LOG_INFO, "A directory has arrived.");
510 has_fetched_directory=1;
511 /* Don't try to upload or download anything for a while
512 * after the directory we had when we started.
514 if (!time_to_fetch_directory)
515 time_to_fetch_directory = now + get_dir_fetch_period(options);
517 if (!time_to_force_upload_descriptor)
518 time_to_force_upload_descriptor = now + options->DirPostPeriod;
520 if (!time_to_fetch_running_routers)
521 time_to_fetch_running_routers = now + get_status_fetch_period(options);
523 if (server_mode(options) && identity_digest) {
524 /* if this is us, then our dirport is reachable */
525 routerinfo_t *router = router_get_by_digest(identity_digest);
526 if (!router) // XXX
527 log_fn(LOG_WARN,"Bug: router_get_by_digest doesn't find me.");
528 if (router && router_is_me(router)) {
529 router_dirport_found_reachable();
533 if (server_mode(options) &&
534 !we_are_hibernating()) { /* connect to the appropriate routers */
535 router_retry_connections();
536 if (identity_digest) /* we got a fresh directory */
537 consider_testing_reachability();
541 /** Perform regular maintenance tasks for a single connection. This
542 * function gets run once per second per connection by run_scheduled_events.
544 static void run_connection_housekeeping(int i, time_t now) {
545 cell_t cell;
546 connection_t *conn = connection_array[i];
547 or_options_t *options = get_options();
549 if (conn->outbuf && !buf_datalen(conn->outbuf))
550 conn->timestamp_lastempty = now;
552 /* Expire any directory connections that haven't sent anything for 5 min */
553 if (conn->type == CONN_TYPE_DIR &&
554 !conn->marked_for_close &&
555 conn->timestamp_lastwritten + 5*60 < now) {
556 log_fn(LOG_INFO,"Expiring wedged directory conn (fd %d, purpose %d)", conn->s, conn->purpose);
557 connection_mark_for_close(conn);
558 return;
561 /* If we haven't written to an OR connection for a while, then either nuke
562 the connection or send a keepalive, depending. */
563 if (connection_speaks_cells(conn) &&
564 now >= conn->timestamp_lastwritten + options->KeepalivePeriod) {
565 routerinfo_t *router = router_get_by_digest(conn->identity_digest);
566 if (!connection_state_is_open(conn)) {
567 log_fn(LOG_INFO,"Expiring non-open OR connection to fd %d (%s:%d).",
568 conn->s,conn->address, conn->port);
569 connection_mark_for_close(conn);
570 conn->hold_open_until_flushed = 1;
571 } else if (we_are_hibernating() && !circuit_get_by_conn(conn) &&
572 !buf_datalen(conn->outbuf)) {
573 log_fn(LOG_INFO,"Expiring non-used OR connection to fd %d (%s:%d) [Hibernating or exiting].",
574 conn->s,conn->address, conn->port);
575 connection_mark_for_close(conn);
576 conn->hold_open_until_flushed = 1;
577 } else if (!clique_mode(options) && !circuit_get_by_conn(conn) &&
578 (!router || !server_mode(options) || !router_is_clique_mode(router))) {
579 log_fn(LOG_INFO,"Expiring non-used OR connection to fd %d (%s:%d) [Not in clique mode].",
580 conn->s,conn->address, conn->port);
581 connection_mark_for_close(conn);
582 conn->hold_open_until_flushed = 1;
583 } else if (
584 now >= conn->timestamp_lastempty + options->KeepalivePeriod*10 &&
585 now >= conn->timestamp_lastwritten + options->KeepalivePeriod*10) {
586 log_fn(LOG_NOTICE,"Expiring stuck OR connection to fd %d (%s:%d). (%d bytes to flush; %d seconds since last write)",
587 conn->s, conn->address, conn->port,
588 (int)buf_datalen(conn->outbuf),
589 (int)(now-conn->timestamp_lastwritten));
590 connection_mark_for_close(conn);
591 } else {
592 /* either in clique mode, or we've got a circuit. send a padding cell. */
593 log_fn(LOG_DEBUG,"Sending keepalive to (%s:%d)",
594 conn->address, conn->port);
595 memset(&cell,0,sizeof(cell_t));
596 cell.command = CELL_PADDING;
597 connection_or_write_cell_to_buf(&cell, conn);
602 /** Perform regular maintenance tasks. This function gets run once per
603 * second by prepare_for_poll.
605 static void run_scheduled_events(time_t now) {
606 static time_t last_rotated_certificate = 0;
607 static time_t time_to_check_listeners = 0;
608 static time_t time_to_check_descriptor = 0;
609 or_options_t *options = get_options();
610 int i;
612 /** 0. See if we've been asked to shut down and our timeout has
613 * expired; or if our bandwidth limits are exhausted and we
614 * should hibernate; or if it's time to wake up from hibernation.
616 consider_hibernation(now);
618 /** 1a. Every MIN_ONION_KEY_LIFETIME seconds, rotate the onion keys,
619 * shut down and restart all cpuworkers, and update the directory if
620 * necessary.
622 if (server_mode(options) &&
623 get_onion_key_set_at()+MIN_ONION_KEY_LIFETIME < now) {
624 log_fn(LOG_INFO,"Rotating onion key.");
625 rotate_onion_key();
626 cpuworkers_rotate();
627 if (router_rebuild_descriptor(1)<0) {
628 log_fn(LOG_WARN, "Couldn't rebuild router descriptor");
630 if (advertised_server_mode())
631 router_upload_dir_desc_to_dirservers(0);
634 /** 1b. Every MAX_SSL_KEY_LIFETIME seconds, we change our TLS context. */
635 if (!last_rotated_certificate)
636 last_rotated_certificate = now;
637 if (last_rotated_certificate+MAX_SSL_KEY_LIFETIME < now) {
638 log_fn(LOG_INFO,"Rotating tls context.");
639 if (tor_tls_context_new(get_identity_key(), 1, options->Nickname,
640 MAX_SSL_KEY_LIFETIME) < 0) {
641 log_fn(LOG_WARN, "Error reinitializing TLS context");
642 /* XXX is it a bug here, that we just keep going? */
644 last_rotated_certificate = now;
645 /* XXXX We should rotate TLS connections as well; this code doesn't change
646 * them at all. */
649 /** 1c. If we have to change the accounting interval or record
650 * bandwidth used in this accounting interval, do so. */
651 if (accounting_is_enabled(options))
652 accounting_run_housekeeping(now);
654 /** 2. Periodically, we consider getting a new directory, getting a
655 * new running-routers list, and/or force-uploading our descriptor
656 * (if we've passed our internal checks). */
657 if (time_to_fetch_directory < now) {
658 time_t next_status_fetch;
659 /* purge obsolete entries */
660 routerlist_remove_old_routers(ROUTER_MAX_AGE);
662 if (authdir_mode(options)) {
663 /* We're a directory; dump any old descriptors. */
664 dirserv_remove_old_servers(ROUTER_MAX_AGE);
666 if (server_mode(options) && !we_are_hibernating()) {
667 /* dirservers try to reconnect, in case connections have failed;
668 * and normal servers try to reconnect to dirservers */
669 router_retry_connections();
672 directory_get_from_dirserver(DIR_PURPOSE_FETCH_DIR, NULL, 1);
673 time_to_fetch_directory = now + get_dir_fetch_period(options);
674 next_status_fetch = now + get_status_fetch_period(options);
675 if (time_to_fetch_running_routers < next_status_fetch) {
676 time_to_fetch_running_routers = next_status_fetch;
679 /* Also, take this chance to remove old information from rephist. */
680 rep_history_clean(now-24*60*60);
683 if (time_to_fetch_running_routers < now) {
684 if (!authdir_mode(options)) {
685 directory_get_from_dirserver(DIR_PURPOSE_FETCH_RUNNING_LIST, NULL, 1);
687 time_to_fetch_running_routers = now + get_status_fetch_period(options);
690 if (time_to_force_upload_descriptor < now) {
691 consider_publishable_server(now, 1);
693 rend_cache_clean(); /* this should go elsewhere? */
695 time_to_force_upload_descriptor = now + options->DirPostPeriod;
698 /* 2b. Once per minute, regenerate and upload the descriptor if the old
699 * one is inaccurate. */
700 if (time_to_check_descriptor < now) {
701 time_to_check_descriptor = now + CHECK_DESCRIPTOR_INTERVAL;
702 consider_publishable_server(now, 0);
703 /* also, check religiously for reachability, if it's within the first
704 * 20 minutes of our uptime. */
705 if (server_mode(options) &&
706 stats_n_seconds_working < TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT &&
707 !we_are_hibernating())
708 consider_testing_reachability();
711 /** 3a. Every second, we examine pending circuits and prune the
712 * ones which have been pending for more than a few seconds.
713 * We do this before step 4, so it can try building more if
714 * it's not comfortable with the number of available circuits.
716 circuit_expire_building(now);
718 /** 3b. Also look at pending streams and prune the ones that 'began'
719 * a long time ago but haven't gotten a 'connected' yet.
720 * Do this before step 4, so we can put them back into pending
721 * state to be picked up by the new circuit.
723 connection_ap_expire_beginning();
725 /** 3c. And expire connections that we've held open for too long.
727 connection_expire_held_open();
729 /** 3d. And every 60 seconds, we relaunch listeners if any died. */
730 if (!we_are_hibernating() && time_to_check_listeners < now) {
731 retry_all_listeners(0); /* 0 means "only if some died." */
732 time_to_check_listeners = now+60;
735 /** 4. Every second, we try a new circuit if there are no valid
736 * circuits. Every NewCircuitPeriod seconds, we expire circuits
737 * that became dirty more than MaxCircuitDirtiness seconds ago,
738 * and we make a new circ if there are no clean circuits.
740 if (has_fetched_directory && !we_are_hibernating())
741 circuit_build_needed_circs(now);
743 /** 5. We do housekeeping for each connection... */
744 for (i=0;i<nfds;i++) {
745 run_connection_housekeeping(i, now);
748 /** 6. And remove any marked circuits... */
749 circuit_close_all_marked();
751 /** 7. And upload service descriptors if necessary. */
752 if (has_fetched_directory && !we_are_hibernating())
753 rend_consider_services_upload(now);
755 /** 8. and blow away any connections that need to die. have to do this now,
756 * because if we marked a conn for close and left its socket -1, then
757 * we'll pass it to poll/select and bad things will happen.
759 close_closeable_connections();
762 /** Libevent callback: invoked once every second. */
763 static void second_elapsed_callback(int fd, short event, void *args)
765 static struct event *timeout_event = NULL;
766 static struct timeval one_second;
767 static long current_second = 0;
768 struct timeval now;
769 size_t bytes_written;
770 size_t bytes_read;
771 int seconds_elapsed;
772 or_options_t *options = get_options();
773 if (!timeout_event) {
774 timeout_event = tor_malloc_zero(sizeof(struct event));
775 evtimer_set(timeout_event, second_elapsed_callback, NULL);
776 one_second.tv_sec = 1;
777 one_second.tv_usec = 0;
780 /* log_fn(LOG_NOTICE, "Tick."); */
781 tor_gettimeofday(&now);
783 /* the second has rolled over. check more stuff. */
784 bytes_written = stats_prev_global_write_bucket - global_write_bucket;
785 bytes_read = stats_prev_global_read_bucket - global_read_bucket;
786 /* XXX below we get suspicious if time jumps forward more than 10
787 * seconds, but we never notice if it jumps *back* more than 10 seconds.
788 * This could be useful for detecting that we just NTP'ed to three
789 * weeks ago and it will be 3 weeks and 15 minutes until any of our
790 * events trigger.
792 seconds_elapsed = current_second ? (now.tv_sec - current_second) : 0;
793 stats_n_bytes_read += bytes_read;
794 stats_n_bytes_written += bytes_written;
795 if (accounting_is_enabled(options))
796 accounting_add_bytes(bytes_read, bytes_written, seconds_elapsed);
797 control_event_bandwidth_used((uint32_t)bytes_read,(uint32_t)bytes_written);
799 connection_bucket_refill(&now);
800 stats_prev_global_read_bucket = global_read_bucket;
801 stats_prev_global_write_bucket = global_write_bucket;
803 if (server_mode(options) &&
804 !we_are_hibernating() &&
805 stats_n_seconds_working / TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT !=
806 (stats_n_seconds_working+seconds_elapsed) /
807 TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT) {
808 /* every 20 minutes, check and complain if necessary */
809 routerinfo_t *me = router_get_my_routerinfo();
810 if (!check_whether_orport_reachable())
811 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.",
812 me ? me->address : options->Address, options->ORPort);
813 if (!check_whether_dirport_reachable())
814 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.",
815 me ? me->address : options->Address, options->DirPort);
818 /* if more than 10s have elapsed, probably the clock jumped: doesn't count. */
819 if (seconds_elapsed < 100)
820 stats_n_seconds_working += seconds_elapsed;
821 else
822 circuit_note_clock_jumped(seconds_elapsed);
824 run_scheduled_events(now.tv_sec);
826 current_second = now.tv_sec; /* remember which second it is, for next time */
828 if (evtimer_add(timeout_event, &one_second))
829 log_fn(LOG_ERR,
830 "Error from libevent when setting one-second timeout event");
833 /** Called when we get a SIGHUP: reload configuration files and keys,
834 * retry all connections, re-upload all descriptors, and so on. */
835 static int do_hup(void) {
836 char keydir[512];
837 or_options_t *options = get_options();
839 log_fn(LOG_NOTICE,"Received sighup. Reloading config.");
840 has_completed_circuit=0;
841 if (accounting_is_enabled(options))
842 accounting_record_bandwidth_usage(time(NULL));
844 addressmap_clear_transient();
845 /* first, reload config variables, in case they've changed */
846 /* no need to provide argc/v, they've been cached inside init_from_config */
847 if (init_from_config(0, NULL) < 0) {
848 log_fn(LOG_ERR,"Reading config failed--see warnings above. For usage, try -h.");
849 return -1;
851 options = get_options(); /* they have changed now */
852 if (authdir_mode(options)) {
853 /* reload the approved-routers file */
854 tor_snprintf(keydir,sizeof(keydir),"%s/approved-routers", options->DataDirectory);
855 log_fn(LOG_INFO,"Reloading approved fingerprints from %s...",keydir);
856 if (dirserv_parse_fingerprint_file(keydir) < 0) {
857 log_fn(LOG_NOTICE, "Error reloading fingerprints. Continuing with old list.");
860 /* Fetch a new directory. Even authdirservers do this. */
861 directory_get_from_dirserver(DIR_PURPOSE_FETCH_DIR, NULL, 1);
862 if (server_mode(options)) {
863 const char *descriptor;
864 /* Restart cpuworker and dnsworker processes, so they get up-to-date
865 * configuration options. */
866 cpuworkers_rotate();
867 dnsworkers_rotate();
868 /* Rebuild fresh descriptor, but leave old one on failure. */
869 router_rebuild_descriptor(1);
870 descriptor = router_get_my_descriptor();
871 if (!descriptor) {
872 log_fn(LOG_WARN,"No descriptor to save.");
873 return 0;
875 tor_snprintf(keydir,sizeof(keydir),"%s/router.desc",
876 options->DataDirectory);
877 log_fn(LOG_INFO,"Saving descriptor to %s...",keydir);
878 if (write_str_to_file(keydir, descriptor, 0)) {
879 return 0;
882 return 0;
885 /** Tor main loop. */
886 static int do_main_loop(void) {
887 int loop_result;
889 /* load the private keys, if we're supposed to have them, and set up the
890 * TLS context. */
891 if (! identity_key_is_set()) {
892 if (init_keys() < 0) {
893 log_fn(LOG_ERR,"Error initializing keys; exiting");
894 return -1;
898 /* Set up our buckets */
899 connection_bucket_init();
900 stats_prev_global_read_bucket = global_read_bucket;
901 stats_prev_global_write_bucket = global_write_bucket;
903 /* load the routers file, or assign the defaults. */
904 if (router_reload_router_list()) {
905 return -1;
908 if (authdir_mode(get_options())) {
909 /* the directory is already here, run startup things */
910 router_retry_connections();
913 if (server_mode(get_options())) {
914 /* launch cpuworkers. Need to do this *after* we've read the onion key. */
915 cpu_init();
918 /* set up once-a-second callback. */
919 second_elapsed_callback(0,0,NULL);
921 for (;;) {
922 if (nt_service_is_stopped())
923 return 0;
925 #ifndef MS_WINDOWS
926 /* Make it easier to tell whether libevent failure is our fault or not. */
927 errno = 0;
928 #endif
929 /* poll until we have an event, or the second ends */
930 loop_result = event_dispatch();
932 /* let catch() handle things like ^c, and otherwise don't worry about it */
933 if (loop_result < 0) {
934 int e = errno;
935 /* let the program survive things like ^z */
936 if (e != EINTR) {
937 #ifdef HAVE_EVENT_GET_METHOD
938 log_fn(LOG_ERR,"libevent poll with %s failed: %s [%d]",
939 event_get_method(), tor_socket_strerror(e), e);
940 #else
941 log_fn(LOG_ERR,"libevent poll failed: %s [%d]",
942 tor_socket_strerror(e), e);
943 #endif
944 return -1;
945 } else {
946 log_fn(LOG_DEBUG,"event poll interrupted.");
947 /* You can't trust the results of this poll(). Go back to the
948 * top of the big for loop. */
949 continue;
953 /* refilling buckets and sending cells happens at the beginning of the
954 * next iteration of the loop, inside prepare_for_poll()
955 * XXXX No longer so.
960 /** Used to implement the SIGNAL control command: if we accept
961 * <b>the_signal</b> as a remote pseudo-signal, then act on it and
962 * return 0. Else return -1. */
963 /* We don't re-use catch() here because:
964 * 1. We handle a different set of signals than those allowed in catch.
965 * 2. Platforms without signal() are unlikely to define SIGfoo.
966 * 3. The control spec is defined to use fixed numeric signal values
967 * which just happen to match the unix values.
970 control_signal_act(int the_signal)
972 switch (the_signal)
974 case 1:
975 signal_callback(0,0,(void*)(uintptr_t)SIGHUP);
976 break;
977 case 2:
978 signal_callback(0,0,(void*)(uintptr_t)SIGINT);
979 break;
980 case 10:
981 signal_callback(0,0,(void*)(uintptr_t)SIGUSR1);
982 break;
983 case 12:
984 signal_callback(0,0,(void*)(uintptr_t)SIGUSR2);
985 break;
986 case 15:
987 signal_callback(0,0,(void*)(uintptr_t)SIGTERM);
988 break;
989 default:
990 return -1;
992 return 0;
995 static void signal_callback(int fd, short events, void *arg)
997 uintptr_t sig = (uintptr_t)arg;
998 switch (sig)
1000 case SIGTERM:
1001 log(LOG_ERR,"Catching signal TERM, exiting cleanly.");
1002 tor_cleanup();
1003 exit(0);
1004 break;
1005 case SIGINT:
1006 if (!server_mode(get_options())) { /* do it now */
1007 log(LOG_NOTICE,"Interrupt: exiting cleanly.");
1008 tor_cleanup();
1009 exit(0);
1011 hibernate_begin_shutdown();
1012 break;
1013 #ifdef SIGPIPE
1014 case SIGPIPE:
1015 log(LOG_NOTICE,"Caught sigpipe. Ignoring.");
1016 break;
1017 #endif
1018 case SIGUSR1:
1019 /* prefer to log it at INFO, but make sure we always see it */
1020 dumpstats(get_min_log_level()<LOG_INFO ? get_min_log_level() : LOG_INFO);
1021 break;
1022 case SIGUSR2:
1023 switch_logs_debug();
1024 log(LOG_NOTICE,"Caught USR2. Going to loglevel debug.");
1025 break;
1026 case SIGHUP:
1027 if (do_hup() < 0) {
1028 log_fn(LOG_WARN,"Restart failed (config error?). Exiting.");
1029 tor_cleanup();
1030 exit(1);
1032 break;
1033 #ifdef SIGCHLD
1034 case SIGCHLD:
1035 while (waitpid(-1,NULL,WNOHANG) > 0) ; /* keep reaping until no more zombies */
1036 break;
1037 #endif
1041 /** Write all statistics to the log, with log level 'severity'. Called
1042 * in response to a SIGUSR1. */
1043 static void
1044 dumpstats(int severity) {
1045 int i;
1046 connection_t *conn;
1047 time_t now = time(NULL);
1048 time_t elapsed;
1050 log(severity, "Dumping stats:");
1052 for (i=0;i<nfds;i++) {
1053 conn = connection_array[i];
1054 log(severity, "Conn %d (socket %d) type %d (%s), state %d (%s), created %d secs ago",
1055 i, conn->s, conn->type, conn_type_to_string(conn->type),
1056 conn->state, conn_state_to_string(conn->type, conn->state), (int)(now - conn->timestamp_created));
1057 if (!connection_is_listener(conn)) {
1058 log(severity,"Conn %d is to '%s:%d'.",i,conn->address, conn->port);
1059 log(severity,"Conn %d: %d bytes waiting on inbuf (last read %d secs ago)",i,
1060 (int)buf_datalen(conn->inbuf),
1061 (int)(now - conn->timestamp_lastread));
1062 log(severity,"Conn %d: %d bytes waiting on outbuf (last written %d secs ago)",i,
1063 (int)buf_datalen(conn->outbuf), (int)(now - conn->timestamp_lastwritten));
1065 circuit_dump_by_conn(conn, severity); /* dump info about all the circuits using this conn */
1067 log(severity,
1068 "Cells processed: %10lu padding\n"
1069 " %10lu create\n"
1070 " %10lu created\n"
1071 " %10lu relay\n"
1072 " (%10lu relayed)\n"
1073 " (%10lu delivered)\n"
1074 " %10lu destroy",
1075 stats_n_padding_cells_processed,
1076 stats_n_create_cells_processed,
1077 stats_n_created_cells_processed,
1078 stats_n_relay_cells_processed,
1079 stats_n_relay_cells_relayed,
1080 stats_n_relay_cells_delivered,
1081 stats_n_destroy_cells_processed);
1082 if (stats_n_data_cells_packaged)
1083 log(severity,"Average packaged cell fullness: %2.3f%%",
1084 100*(((double)stats_n_data_bytes_packaged) /
1085 (stats_n_data_cells_packaged*RELAY_PAYLOAD_SIZE)) );
1086 if (stats_n_data_cells_received)
1087 log(severity,"Average delivered cell fullness: %2.3f%%",
1088 100*(((double)stats_n_data_bytes_received) /
1089 (stats_n_data_cells_received*RELAY_PAYLOAD_SIZE)) );
1091 if (now - time_of_process_start >= 0)
1092 elapsed = now - time_of_process_start;
1093 else
1094 elapsed = 0;
1096 if (elapsed) {
1097 log(severity,
1098 "Average bandwidth: "U64_FORMAT"/%d = %d bytes/sec reading",
1099 U64_PRINTF_ARG(stats_n_bytes_read),
1100 (int)elapsed,
1101 (int) (stats_n_bytes_read/elapsed));
1102 log(severity,
1103 "Average bandwidth: "U64_FORMAT"/%d = %d bytes/sec writing",
1104 U64_PRINTF_ARG(stats_n_bytes_written),
1105 (int)elapsed,
1106 (int) (stats_n_bytes_written/elapsed));
1109 rep_hist_dump_stats(now,severity);
1110 rend_service_dump_stats(severity);
1113 /** Called by exit() as we shut down the process.
1115 static void exit_function(void)
1117 /* NOTE: If we ever daemonize, this gets called immediately. That's
1118 * okay for now, because we only use this on Windows. */
1119 #ifdef MS_WINDOWS
1120 WSACleanup();
1121 #endif
1124 /** Set up the signal handlers for either parent or child. */
1125 void handle_signals(int is_parent)
1127 #ifndef MS_WINDOWS /* do signal stuff only on unix */
1128 int i;
1129 static int signals[] = {
1130 SIGINT, /* do a controlled slow shutdown */
1131 SIGTERM, /* to terminate now */
1132 SIGPIPE, /* otherwise sigpipe kills us */
1133 SIGUSR1, /* dump stats */
1134 SIGUSR2, /* go to loglevel debug */
1135 SIGHUP, /* to reload config, retry conns, etc */
1136 #ifdef SIGXFSZ
1137 SIGXFSZ, /* handle file-too-big resource exhaustion */
1138 #endif
1139 SIGCHLD, /* handle dns/cpu workers that exit */
1140 -1 };
1141 static struct event signal_events[16]; /* bigger than it has to be. */
1142 if (is_parent) {
1143 for (i = 0; signals[i] >= 0; ++i) {
1144 signal_set(&signal_events[i], signals[i], signal_callback,
1145 (void*)(uintptr_t)signals[i]);
1146 if (signal_add(&signal_events[i], NULL))
1147 log_fn(LOG_WARN, "Error from libevent when adding event for signal %d",
1148 signals[i]);
1150 } else {
1151 struct sigaction action;
1152 action.sa_flags = 0;
1153 sigemptyset(&action.sa_mask);
1154 action.sa_handler = SIG_IGN;
1155 sigaction(SIGINT, &action, NULL);
1156 sigaction(SIGTERM, &action, NULL);
1157 sigaction(SIGPIPE, &action, NULL);
1158 sigaction(SIGUSR1, &action, NULL);
1159 sigaction(SIGUSR2, &action, NULL);
1160 sigaction(SIGHUP, &action, NULL);
1161 #ifdef SIGXFSZ
1162 sigaction(SIGXFSZ, &action, NULL);
1163 #endif
1165 #endif /* signal stuff */
1168 /** Main entry point for the Tor command-line client.
1170 static int tor_init(int argc, char *argv[]) {
1171 time_of_process_start = time(NULL);
1172 closeable_connection_lst = smartlist_create();
1173 /* Initialize the history structures. */
1174 rep_hist_init();
1175 /* Initialize the service cache. */
1176 rend_cache_init();
1177 addressmap_init(); /* Init the client dns cache. Do it always, since it's cheap. */
1179 /* give it somewhere to log to initially */
1180 add_temp_log();
1182 log_fn(LOG_NOTICE,"Tor v%s. This is experimental software. Do not rely on it for strong anonymity.",VERSION);
1184 if (network_init()<0) {
1185 log_fn(LOG_ERR,"Error initializing network; exiting.");
1186 return -1;
1188 atexit(exit_function);
1190 if (init_from_config(argc,argv) < 0) {
1191 log_fn(LOG_ERR,"Reading config failed--see warnings above. For usage, try -h.");
1192 return -1;
1195 #ifndef MS_WINDOWS
1196 if (geteuid()==0)
1197 log_fn(LOG_WARN,"You are running Tor as root. You don't need to, and you probably shouldn't.");
1198 #endif
1200 /* only spawn dns handlers if we're a router */
1201 if (server_mode(get_options()) && get_options()->command == CMD_RUN_TOR) {
1202 dns_init(); /* initialize the dns resolve tree, and spawn workers */
1203 /* XXX really, this should get moved to do_main_loop */
1206 handle_signals(1);
1208 crypto_global_init();
1209 crypto_seed_rng();
1210 return 0;
1213 /** Free all memory that we might have allocated somewhere.
1214 * Helps us find the real leaks with dmalloc and the like.
1216 * Also valgrind should then report 0 reachable in its
1217 * leak report */
1218 void tor_free_all(void)
1220 routerlist_free_current();
1221 free_trusted_dir_servers();
1222 addressmap_free_all();
1223 free_socks_policy();
1224 free_dir_policy();
1225 dirserv_free_all();
1226 rend_service_free_all();
1227 rend_cache_free_all();
1228 rep_hist_free_all();
1229 dns_free_all();
1230 clear_pending_onions();
1231 circuit_free_all();
1232 connection_free_all();
1233 config_free_all();
1234 router_free_all_keys();
1235 tor_tls_free_all();
1236 /* stuff in main.c */
1237 smartlist_free(closeable_connection_lst);
1239 close_logs(); /* free log strings. do this last so logs keep working. */
1242 /** Do whatever cleanup is necessary before shutting Tor down. */
1243 void tor_cleanup(void) {
1244 or_options_t *options = get_options();
1245 /* Remove our pid file. We don't care if there was an error when we
1246 * unlink, nothing we could do about it anyways. */
1247 if (options->PidFile && options->command == CMD_RUN_TOR)
1248 unlink(options->PidFile);
1249 if (accounting_is_enabled(options))
1250 accounting_record_bandwidth_usage(time(NULL));
1251 tor_free_all(); /* move tor_free_all back into the ifdef below later. XXX*/
1252 crypto_global_cleanup();
1253 #ifdef USE_DMALLOC
1254 dmalloc_log_unfreed();
1255 dmalloc_shutdown();
1256 #endif
1259 /** Read/create keys as needed, and echo our fingerprint to stdout. */
1260 static void do_list_fingerprint(void)
1262 char buf[FINGERPRINT_LEN+1];
1263 crypto_pk_env_t *k;
1264 const char *nickname = get_options()->Nickname;
1265 if (!server_mode(get_options())) {
1266 printf("Clients don't have long-term identity keys. Exiting.\n");
1267 return;
1269 tor_assert(nickname);
1270 if (init_keys() < 0) {
1271 log_fn(LOG_ERR,"Error initializing keys; exiting");
1272 return;
1274 if (!(k = get_identity_key())) {
1275 log_fn(LOG_ERR,"Error: missing identity key.");
1276 return;
1278 if (crypto_pk_get_fingerprint(k, buf, 1)<0) {
1279 log_fn(LOG_ERR, "Error computing fingerprint");
1280 return;
1282 printf("%s %s\n", nickname, buf);
1285 /** Entry point for password hashing: take the desired password from
1286 * the command line, and print its salted hash to stdout. **/
1287 static void do_hash_password(void)
1290 char output[256];
1291 char key[S2K_SPECIFIER_LEN+DIGEST_LEN];
1293 crypto_rand(key, S2K_SPECIFIER_LEN-1);
1294 key[S2K_SPECIFIER_LEN-1] = (uint8_t)96; /* Hash 64 K of data. */
1295 secret_to_key(key+S2K_SPECIFIER_LEN, DIGEST_LEN,
1296 get_options()->command_arg, strlen(get_options()->command_arg),
1297 key);
1298 if (base64_encode(output, sizeof(output), key, sizeof(key))<0) {
1299 log_fn(LOG_ERR, "Unable to compute base64");
1300 } else {
1301 printf("%s",output);
1305 #ifdef MS_WINDOWS_SERVICE
1306 /** If we're compile to run as an NT service, and the service has been
1307 * shut down, then change our current status and return 1. Else
1308 * return 0.
1310 static int
1311 nt_service_is_stopped(void)
1313 if (service_status.dwCurrentState == SERVICE_STOP_PENDING) {
1314 service_status.dwWin32ExitCode = 0;
1315 service_status.dwCurrentState = SERVICE_STOPPED;
1316 SetServiceStatus(hStatus, &service_status);
1317 return 1;
1319 return 0;
1322 void nt_service_control(DWORD request)
1324 switch (request) {
1325 case SERVICE_CONTROL_STOP:
1326 case SERVICE_CONTROL_SHUTDOWN:
1327 log(LOG_ERR, "Got stop/shutdown request; shutting down cleanly.");
1328 service_status.dwCurrentState = SERVICE_STOP_PENDING;
1329 return;
1331 SetServiceStatus(hStatus, &service_status);
1334 void nt_service_body(int argc, char **argv)
1336 int err;
1337 service_status.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
1338 service_status.dwCurrentState = SERVICE_START_PENDING;
1339 service_status.dwControlsAccepted =
1340 SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
1341 service_status.dwWin32ExitCode = 0;
1342 service_status.dwServiceSpecificExitCode = 0;
1343 service_status.dwCheckPoint = 0;
1344 service_status.dwWaitHint = 1000;
1345 hStatus = RegisterServiceCtrlHandler(GENSRV_SERVICENAME, (LPHANDLER_FUNCTION) nt_service_control);
1347 if (hStatus == 0) {
1348 // failed;
1349 return;
1352 err = tor_init(backup_argc, backup_argv); // refactor this part out of tor_main and do_main_loop
1353 if (err) {
1354 // failed.
1355 service_status.dwCurrentState = SERVICE_STOPPED;
1356 service_status.dwWin32ExitCode = -1;
1357 SetServiceStatus(hStatus, &service_status);
1358 return;
1360 service_status.dwCurrentState = SERVICE_RUNNING;
1361 SetServiceStatus(hStatus, &service_status);
1362 do_main_loop();
1363 tor_cleanup();
1364 return;
1367 void nt_service_main(void)
1369 SERVICE_TABLE_ENTRY table[2];
1370 DWORD result = 0;
1371 table[0].lpServiceName = GENSRV_SERVICENAME;
1372 table[0].lpServiceProc = (LPSERVICE_MAIN_FUNCTION)nt_service_body;
1373 table[1].lpServiceName = NULL;
1374 table[1].lpServiceProc = NULL;
1376 if (!StartServiceCtrlDispatcher(table)) {
1377 result = GetLastError();
1378 printf("Error was %d\n",result);
1379 if (result == ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) {
1380 if (tor_init(backup_argc, backup_argv) < 0)
1381 return;
1382 switch (get_options()->command) {
1383 case CMD_RUN_TOR:
1384 do_main_loop();
1385 break;
1386 case CMD_LIST_FINGERPRINT:
1387 do_list_fingerprint();
1388 break;
1389 case CMD_HASH_PASSWORD:
1390 do_hash_password();
1391 break;
1392 default:
1393 log_fn(LOG_ERR, "Illegal command number %d: internal error.", get_options()->command);
1395 tor_cleanup();
1400 int nt_service_install()
1402 /* XXXX Problems with NT services:
1403 * 1. The configuration file needs to be in the same directory as the .exe
1405 * 2. The exe and the configuration file can't be on any directory path
1406 * that contains a space.
1407 * mje - you can quote the string (i.e., "c:\program files")
1409 * 3. Ideally, there should be one EXE that can either run as a
1410 * separate process (as now) or that can install and run itself
1411 * as an NT service. I have no idea how hard this is.
1412 * mje - should be done. It can install and run itself as a service
1414 * Notes about developing NT services:
1416 * 1. Don't count on your CWD. If an absolute path is not given, the
1417 * fopen() function goes wrong.
1418 * 2. The parameters given to the nt_service_body() function differ
1419 * from those given to main() function.
1422 SC_HANDLE hSCManager = NULL;
1423 SC_HANDLE hService = NULL;
1424 SERVICE_DESCRIPTION sdBuff;
1425 TCHAR szPath[_MAX_PATH];
1426 TCHAR szDrive[_MAX_DRIVE];
1427 TCHAR szDir[_MAX_DIR];
1428 char cmd1[] = " -f ";
1429 char cmd2[] = "\\torrc";
1430 char *command;
1431 int len = 0;
1433 if (0 == GetModuleFileName(NULL, szPath, MAX_PATH))
1434 return 0;
1436 _tsplitpath(szPath, szDrive, szDir, NULL, NULL);
1438 /* Account for the extra quotes */
1439 //len = _MAX_PATH + strlen(cmd1) + _MAX_DRIVE + _MAX_DIR + strlen(cmd2);
1440 len = _MAX_PATH + strlen(cmd1) + _MAX_DRIVE + _MAX_DIR + strlen(cmd2) + 64;
1441 command = tor_malloc(len);
1443 /* Create a quoted command line, like "c:\with spaces\tor.exe" -f
1444 * "c:\with spaces\tor.exe"
1446 if (tor_snprintf(command, len, "\"%s\" --nt-service -f \"%s%storrc\"",
1447 szPath, szDrive, szDir)<0) {
1448 printf("Failed: tor_snprinf()\n");
1449 free(command);
1450 return 0;
1453 if ((hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CREATE_SERVICE)) == NULL) {
1454 printf("Failed: OpenSCManager()\n");
1455 free(command);
1456 return 0;
1459 /* 1/26/2005 mje
1460 * - changed the service start type to auto
1461 * - and changed the lpPassword param to "" instead of NULL as per an
1462 * MSDN article.
1464 if ((hService = CreateService(hSCManager, GENSRV_SERVICENAME, GENSRV_DISPLAYNAME,
1465 SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
1466 SERVICE_AUTO_START, SERVICE_ERROR_IGNORE, command,
1467 NULL, NULL, NULL, NULL, "")) == NULL) {
1468 printf("Failed: CreateService()\n");
1469 CloseServiceHandle(hSCManager);
1470 free(command);
1471 return 0;
1474 /* Start the service initially, so you don't have to muck with it in the SCM
1476 /* Set the service's description */
1477 sdBuff.lpDescription = GENSRV_DESCRIPTION;
1478 ChangeServiceConfig2(hService, SERVICE_CONFIG_DESCRIPTION, &sdBuff);
1480 /* Start the service, so you don't have to muck with it in the SCM */
1481 if (StartService(hService, 0, NULL)) {
1482 /* Loop until the service has finished attempting to start */
1483 while (QueryServiceStatus(hService, &service_status) &&
1484 service_status.dwCurrentState == SERVICE_START_PENDING)
1485 Sleep(500);
1487 /* Check if it started successfully or not */
1488 if (service_status.dwCurrentState == SERVICE_RUNNING)
1489 printf("Service installed and started successfully.\n");
1490 else
1491 printf("Service installed, but failed to start.\n");
1492 } else {
1493 printf("Service installed, but failed to start.\n");
1496 CloseServiceHandle(hService);
1497 CloseServiceHandle(hSCManager);
1498 tor_free(command);
1500 return 0;
1503 int nt_service_remove()
1505 SC_HANDLE hSCManager = NULL;
1506 SC_HANDLE hService = NULL;
1507 SERVICE_STATUS service_status;
1508 BOOL result = FALSE;
1510 if ((hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CREATE_SERVICE)) == NULL) {
1511 printf("Failed: OpenSCManager()\n");
1512 return 0;
1515 if ((hService = OpenService(hSCManager, GENSRV_SERVICENAME, SERVICE_ALL_ACCESS)) == NULL) {
1516 printf("Failed: OpenService()\n");
1517 CloseServiceHandle(hSCManager);
1518 return 0;
1521 result = ControlService(hService, SERVICE_CONTROL_STOP, &service_status);
1522 if (result) {
1523 while (QueryServiceStatus(hService, &service_status))
1525 if (service_status.dwCurrentState == SERVICE_STOP_PENDING)
1526 Sleep(500);
1527 else
1528 break;
1530 if (DeleteService(hService))
1531 printf("Removed service successfully\n");
1532 else
1533 printf("Failed: DeleteService()\n");
1534 } else {
1535 result = DeleteService(hService);
1536 if (result)
1537 printf("Removed service successfully\n");
1538 else
1539 printf("Failed: DeleteService()\n");
1542 CloseServiceHandle(hService);
1543 CloseServiceHandle(hSCManager);
1545 return 0;
1547 #endif
1549 int tor_main(int argc, char *argv[]) {
1550 #ifdef MS_WINDOWS_SERVICE
1551 backup_argv = argv;
1552 backup_argc = argc;
1553 if ((argc >= 2) && !strcmp(argv[1], "-install"))
1554 return nt_service_install();
1555 if ((argc >= 2) && !strcmp(argv[1], "-remove"))
1556 return nt_service_remove();
1557 if ((argc >= 2) && !strcmp(argv[1], "--nt-service")) {
1558 nt_service_main();
1559 return 0;
1561 #endif
1562 if (tor_init(argc, argv)<0)
1563 return -1;
1564 switch (get_options()->command) {
1565 case CMD_RUN_TOR:
1566 #ifdef MS_WINDOWS_SERVICE
1567 service_status.dwCurrentState = SERVICE_RUNNING;
1568 #endif
1569 do_main_loop();
1570 break;
1571 case CMD_LIST_FINGERPRINT:
1572 do_list_fingerprint();
1573 break;
1574 case CMD_HASH_PASSWORD:
1575 do_hash_password();
1576 break;
1577 default:
1578 log_fn(LOG_ERR, "Illegal command number %d: internal error.",
1579 get_options()->command);
1581 tor_cleanup();
1582 return -1;