update copyright notices.
[tor.git] / src / or / main.c
blob393e5a69e269d01fb69350f30ba07d5e916ec8ef
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 schedule to get closed */
326 static void
327 close_closeable_connections(void)
329 int i;
330 if (!smartlist_len(closeable_connection_lst))
331 return;
333 for (i = 0; i < smartlist_len(closeable_connection_lst); ) {
334 connection_t *conn = smartlist_get(closeable_connection_lst, i);
335 if (conn->poll_index < 0) {
336 connection_unlink(conn, 0); /* blow it away right now */
337 } else {
338 if (!conn_close_if_marked(conn->poll_index))
339 ++i;
344 /** Libevent callback: this gets invoked when (connection_t*)<b>conn</b> has
345 * some data to read. */
346 static void
347 conn_read_callback(int fd, short event, void *_conn)
349 connection_t *conn = _conn;
350 if (conn->marked_for_close)
351 return;
353 log_fn(LOG_DEBUG,"socket %d wants to read.",conn->s);
355 assert_connection_ok(conn, time(NULL));
356 assert_all_pending_dns_resolves_ok();
358 if (connection_handle_read(conn) < 0) {
359 if (!conn->marked_for_close) {
360 #ifndef MS_WINDOWS
361 log_fn(LOG_WARN,"Bug: unhandled error on read for %s connection (fd %d); removing",
362 CONN_TYPE_TO_STRING(conn->type), conn->s);
363 #ifdef TOR_FRAGILE
364 tor_assert(0);
365 #endif
366 #endif
367 if (CONN_IS_EDGE(conn))
368 connection_edge_end_errno(conn, conn->cpath_layer);
369 connection_mark_for_close(conn);
372 assert_connection_ok(conn, time(NULL));
373 assert_all_pending_dns_resolves_ok();
375 if (smartlist_len(closeable_connection_lst))
376 close_closeable_connections();
379 /** Libevent callback: this gets invoked when (connection_t*)<b>conn</b> has
380 * some data to write. */
381 static void conn_write_callback(int fd, short events, void *_conn)
383 connection_t *conn = _conn;
385 log_fn(LOG_DEBUG,"socket %d wants to write.",conn->s);
386 if (conn->marked_for_close)
387 return;
389 assert_connection_ok(conn, time(NULL));
390 assert_all_pending_dns_resolves_ok();
392 if (connection_handle_write(conn) < 0) {
393 if (!conn->marked_for_close) {
394 /* this connection is broken. remove it. */
395 log_fn(LOG_WARN,"Bug: unhandled error on write for %s connection (fd %d); removing",
396 CONN_TYPE_TO_STRING(conn->type), conn->s);
397 #ifdef TOR_FRAGILE
398 tor_assert(0);
399 #endif
400 conn->has_sent_end = 1; /* otherwise we cry wolf about duplicate close */
401 /* XXX do we need a close-immediate here, so we don't try to flush? */
402 connection_mark_for_close(conn);
405 assert_connection_ok(conn, time(NULL));
406 assert_all_pending_dns_resolves_ok();
408 if (smartlist_len(closeable_connection_lst))
409 close_closeable_connections();
412 /** If the connection at connection_array[i] is marked for close, then:
413 * - If it has data that it wants to flush, try to flush it.
414 * - If it _still_ has data to flush, and conn->hold_open_until_flushed is
415 * true, then leave the connection open and return.
416 * - Otherwise, remove the connection from connection_array and from
417 * all other lists, close it, and free it.
418 * Returns 1 if the connection was closed, 0 otherwise.
420 static int conn_close_if_marked(int i) {
421 connection_t *conn;
422 int retval;
424 conn = connection_array[i];
425 if (!conn->marked_for_close)
426 return 0; /* nothing to see here, move along */
427 assert_connection_ok(conn, time(NULL));
428 assert_all_pending_dns_resolves_ok();
430 log_fn(LOG_INFO,"Cleaning up connection (fd %d).",conn->s);
431 if (conn->s >= 0 && connection_wants_to_flush(conn)) {
432 /* -1 means it's an incomplete edge connection, or that the socket
433 * has already been closed as unflushable. */
434 if (!conn->hold_open_until_flushed)
435 log_fn(LOG_INFO,
436 "Conn (addr %s, fd %d, type %s, state %d) marked, but wants to flush %d bytes. "
437 "(Marked at %s:%d)",
438 conn->address, conn->s, CONN_TYPE_TO_STRING(conn->type), conn->state,
439 (int)conn->outbuf_flushlen, conn->marked_for_close_file, conn->marked_for_close);
440 if (connection_speaks_cells(conn)) {
441 if (conn->state == OR_CONN_STATE_OPEN) {
442 retval = flush_buf_tls(conn->tls, conn->outbuf, &conn->outbuf_flushlen);
443 } else
444 retval = -1; /* never flush non-open broken tls connections */
445 } else {
446 retval = flush_buf(conn->s, conn->outbuf, &conn->outbuf_flushlen);
448 if (retval >= 0 &&
449 conn->hold_open_until_flushed && connection_wants_to_flush(conn)) {
450 log_fn(LOG_INFO,"Holding conn (fd %d) open for more flushing.",conn->s);
451 /* XXX should we reset timestamp_lastwritten here? */
452 return 0;
454 if (connection_wants_to_flush(conn)) {
455 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)",
456 conn->address, conn->s, CONN_TYPE_TO_STRING(conn->type), conn->state,
457 (int)buf_datalen(conn->outbuf), conn->marked_for_close_file,
458 conn->marked_for_close);
461 connection_unlink(conn, 1); /* unlink, remove, free */
462 return 1;
465 /** We've just tried every dirserver we know about, and none of
466 * them were reachable. Assume the network is down. Change state
467 * so next time an application connection arrives we'll delay it
468 * and try another directory fetch. Kill off all the circuit_wait
469 * streams that are waiting now, since they will all timeout anyway.
471 void directory_all_unreachable(time_t now) {
472 connection_t *conn;
474 has_fetched_directory=0;
475 stats_n_seconds_working=0; /* reset it */
477 while ((conn = connection_get_by_type_state(CONN_TYPE_AP,
478 AP_CONN_STATE_CIRCUIT_WAIT))) {
479 log_fn(LOG_NOTICE,"Network down? Failing connection to '%s:%d'.",
480 conn->socks_request->address, conn->socks_request->port);
481 connection_close_unattached_ap(conn, END_STREAM_REASON_NET_UNREACHABLE);
485 static INLINE int
486 get_dir_fetch_period(or_options_t *options)
488 if (options->DirFetchPeriod)
489 /* Value from config file. */
490 return options->DirFetchPeriod;
491 else if (options->DirPort)
492 /* Default for directory server */
493 return 20*60;
494 else
495 /* Default for average user. */
496 return 40*60;
499 static INLINE int
500 get_status_fetch_period(or_options_t *options)
502 if (options->StatusFetchPeriod)
503 /* Value from config file. */
504 return options->StatusFetchPeriod;
505 else if (options->DirPort)
506 /* Default for directory server */
507 return 15*60;
508 else
509 /* Default for average user. */
510 return 30*60;
513 /** This function is called whenever we successfully pull down a directory.
514 * If <b>identity_digest</b> is defined, it contains the digest of the
515 * router that just gave us this directory. */
516 void directory_has_arrived(time_t now, char *identity_digest) {
517 or_options_t *options = get_options();
519 log_fn(LOG_INFO, "A directory has arrived.");
521 has_fetched_directory=1;
522 /* Don't try to upload or download anything for a while
523 * after the directory we had when we started.
525 if (!time_to_fetch_directory)
526 time_to_fetch_directory = now + get_dir_fetch_period(options);
528 if (!time_to_force_upload_descriptor)
529 time_to_force_upload_descriptor = now + options->DirPostPeriod;
531 if (!time_to_fetch_running_routers)
532 time_to_fetch_running_routers = now + get_status_fetch_period(options);
534 if (server_mode(options) && identity_digest) {
535 /* if this is us, then our dirport is reachable */
536 routerinfo_t *router = router_get_by_digest(identity_digest);
537 if (!router) // XXX
538 log_fn(LOG_WARN,"Bug: router_get_by_digest doesn't find me.");
539 if (router && router_is_me(router)) {
540 router_dirport_found_reachable();
544 if (server_mode(options) &&
545 !we_are_hibernating()) { /* connect to the appropriate routers */
546 router_retry_connections();
547 if (identity_digest) /* we got a fresh directory */
548 consider_testing_reachability();
552 /** Perform regular maintenance tasks for a single connection. This
553 * function gets run once per second per connection by run_scheduled_events.
555 static void run_connection_housekeeping(int i, time_t now) {
556 cell_t cell;
557 connection_t *conn = connection_array[i];
558 or_options_t *options = get_options();
560 if (conn->outbuf && !buf_datalen(conn->outbuf))
561 conn->timestamp_lastempty = now;
563 /* Expire any directory connections that haven't sent anything for 5 min */
564 if (conn->type == CONN_TYPE_DIR &&
565 !conn->marked_for_close &&
566 conn->timestamp_lastwritten + 5*60 < now) {
567 log_fn(LOG_INFO,"Expiring wedged directory conn (fd %d, purpose %d)", conn->s, conn->purpose);
568 connection_mark_for_close(conn);
569 return;
572 /* If we haven't written to an OR connection for a while, then either nuke
573 the connection or send a keepalive, depending. */
574 if (connection_speaks_cells(conn) &&
575 now >= conn->timestamp_lastwritten + options->KeepalivePeriod) {
576 routerinfo_t *router = router_get_by_digest(conn->identity_digest);
577 if (!connection_state_is_open(conn)) {
578 log_fn(LOG_INFO,"Expiring non-open OR connection to %d (%s:%d).",
579 i,conn->address, conn->port);
580 connection_mark_for_close(conn);
581 conn->hold_open_until_flushed = 1;
582 } else if (we_are_hibernating() && !circuit_get_by_conn(conn) &&
583 !buf_datalen(conn->outbuf)) {
584 log_fn(LOG_INFO,"Expiring non-used OR connection to %d (%s:%d) [Hibernating or exiting].",
585 i,conn->address, conn->port);
586 connection_mark_for_close(conn);
587 conn->hold_open_until_flushed = 1;
588 } else if (!clique_mode(options) && !circuit_get_by_conn(conn) &&
589 (!router || !server_mode(options) || !router_is_clique_mode(router))) {
590 log_fn(LOG_INFO,"Expiring non-used OR connection to %d (%s:%d) [Not in clique mode].",
591 i,conn->address, conn->port);
592 connection_mark_for_close(conn);
593 conn->hold_open_until_flushed = 1;
594 } else if (
595 now >= conn->timestamp_lastempty + options->KeepalivePeriod*10 &&
596 now >= conn->timestamp_lastwritten + options->KeepalivePeriod*10) {
597 log_fn(LOG_NOTICE,"Expiring stuck OR connection to %d (%s:%d). (%d bytes to flush; %d seconds since last write)",
598 i, conn->address, conn->port,
599 (int)buf_datalen(conn->outbuf),
600 (int)(now-conn->timestamp_lastwritten));
601 connection_mark_for_close(conn);
602 } else {
603 /* either in clique mode, or we've got a circuit. send a padding cell. */
604 log_fn(LOG_DEBUG,"Sending keepalive to (%s:%d)",
605 conn->address, conn->port);
606 memset(&cell,0,sizeof(cell_t));
607 cell.command = CELL_PADDING;
608 connection_or_write_cell_to_buf(&cell, conn);
613 /** Perform regular maintenance tasks. This function gets run once per
614 * second by prepare_for_poll.
616 static void run_scheduled_events(time_t now) {
617 static time_t last_rotated_certificate = 0;
618 static time_t time_to_check_listeners = 0;
619 static time_t time_to_check_descriptor = 0;
620 or_options_t *options = get_options();
621 int i;
623 /** 0. See if we've been asked to shut down and our timeout has
624 * expired; or if our bandwidth limits are exhausted and we
625 * should hibernate; or if it's time to wake up from hibernation.
627 consider_hibernation(now);
629 /** 1a. Every MIN_ONION_KEY_LIFETIME seconds, rotate the onion keys,
630 * shut down and restart all cpuworkers, and update the directory if
631 * necessary.
633 if (server_mode(options) &&
634 get_onion_key_set_at()+MIN_ONION_KEY_LIFETIME < now) {
635 log_fn(LOG_INFO,"Rotating onion key.");
636 rotate_onion_key();
637 cpuworkers_rotate();
638 if (router_rebuild_descriptor(1)<0) {
639 log_fn(LOG_WARN, "Couldn't rebuild router descriptor");
641 if (advertised_server_mode())
642 router_upload_dir_desc_to_dirservers(0);
645 /** 1b. Every MAX_SSL_KEY_LIFETIME seconds, we change our TLS context. */
646 if (!last_rotated_certificate)
647 last_rotated_certificate = now;
648 if (last_rotated_certificate+MAX_SSL_KEY_LIFETIME < now) {
649 log_fn(LOG_INFO,"Rotating tls context.");
650 if (tor_tls_context_new(get_identity_key(), 1, options->Nickname,
651 MAX_SSL_KEY_LIFETIME) < 0) {
652 log_fn(LOG_WARN, "Error reinitializing TLS context");
653 /* XXX is it a bug here, that we just keep going? */
655 last_rotated_certificate = now;
656 /* XXXX We should rotate TLS connections as well; this code doesn't change
657 * them at all. */
660 /** 1c. If we have to change the accounting interval or record
661 * bandwidth used in this accounting interval, do so. */
662 if (accounting_is_enabled(options))
663 accounting_run_housekeeping(now);
665 /** 2. Periodically, we consider getting a new directory, getting a
666 * new running-routers list, and/or force-uploading our descriptor
667 * (if we've passed our internal checks). */
668 if (time_to_fetch_directory < now) {
669 time_t next_status_fetch;
670 /* purge obsolete entries */
671 routerlist_remove_old_routers(ROUTER_MAX_AGE);
673 if (authdir_mode(options)) {
674 /* We're a directory; dump any old descriptors. */
675 dirserv_remove_old_servers(ROUTER_MAX_AGE);
677 if (server_mode(options) && !we_are_hibernating()) {
678 /* dirservers try to reconnect, in case connections have failed;
679 * and normal servers try to reconnect to dirservers */
680 router_retry_connections();
683 directory_get_from_dirserver(DIR_PURPOSE_FETCH_DIR, NULL, 1);
684 time_to_fetch_directory = now + get_dir_fetch_period(options);
685 next_status_fetch = now + get_status_fetch_period(options);
686 if (time_to_fetch_running_routers < next_status_fetch) {
687 time_to_fetch_running_routers = next_status_fetch;
690 /* Also, take this chance to remove old information from rephist. */
691 rep_history_clean(now-24*60*60);
694 if (time_to_fetch_running_routers < now) {
695 if (!authdir_mode(options)) {
696 directory_get_from_dirserver(DIR_PURPOSE_FETCH_RUNNING_LIST, NULL, 1);
698 time_to_fetch_running_routers = now + get_status_fetch_period(options);
701 if (time_to_force_upload_descriptor < now) {
702 consider_publishable_server(now, 1);
704 rend_cache_clean(); /* this should go elsewhere? */
706 time_to_force_upload_descriptor = now + options->DirPostPeriod;
709 /* 2b. Once per minute, regenerate and upload the descriptor if the old
710 * one is inaccurate. */
711 if (time_to_check_descriptor < now) {
712 time_to_check_descriptor = now + CHECK_DESCRIPTOR_INTERVAL;
713 consider_publishable_server(now, 0);
714 /* also, check religiously for reachability, if it's within the first
715 * 20 minutes of our uptime. */
716 if (server_mode(options) &&
717 stats_n_seconds_working < TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT &&
718 !we_are_hibernating())
719 consider_testing_reachability();
722 /** 3a. Every second, we examine pending circuits and prune the
723 * ones which have been pending for more than a few seconds.
724 * We do this before step 4, so it can try building more if
725 * it's not comfortable with the number of available circuits.
727 circuit_expire_building(now);
729 /** 3b. Also look at pending streams and prune the ones that 'began'
730 * a long time ago but haven't gotten a 'connected' yet.
731 * Do this before step 4, so we can put them back into pending
732 * state to be picked up by the new circuit.
734 connection_ap_expire_beginning();
736 /** 3c. And expire connections that we've held open for too long.
738 connection_expire_held_open();
740 /** 3d. And every 60 seconds, we relaunch listeners if any died. */
741 if (!we_are_hibernating() && time_to_check_listeners < now) {
742 retry_all_listeners(0); /* 0 means "only if some died." */
743 time_to_check_listeners = now+60;
746 /** 4. Every second, we try a new circuit if there are no valid
747 * circuits. Every NewCircuitPeriod seconds, we expire circuits
748 * that became dirty more than MaxCircuitDirtiness seconds ago,
749 * and we make a new circ if there are no clean circuits.
751 if (has_fetched_directory && !we_are_hibernating())
752 circuit_build_needed_circs(now);
754 /** 5. We do housekeeping for each connection... */
755 for (i=0;i<nfds;i++) {
756 run_connection_housekeeping(i, now);
759 /** 6. And remove any marked circuits... */
760 circuit_close_all_marked();
762 /** 7. And upload service descriptors if necessary. */
763 if (has_fetched_directory && !we_are_hibernating())
764 rend_consider_services_upload(now);
766 /** 8. and blow away any connections that need to die. have to do this now,
767 * because if we marked a conn for close and left its socket -1, then
768 * we'll pass it to poll/select and bad things will happen.
770 close_closeable_connections();
773 /** Libevent callback: invoked once every second. */
774 static void second_elapsed_callback(int fd, short event, void *args)
776 static struct event *timeout_event = NULL;
777 static struct timeval one_second;
778 static long current_second = 0;
779 struct timeval now;
780 size_t bytes_written;
781 size_t bytes_read;
782 int seconds_elapsed;
783 or_options_t *options = get_options();
784 if (!timeout_event) {
785 timeout_event = tor_malloc_zero(sizeof(struct event));
786 evtimer_set(timeout_event, second_elapsed_callback, NULL);
787 one_second.tv_sec = 1;
788 one_second.tv_usec = 0;
791 /* log_fn(LOG_NOTICE, "Tick."); */
792 tor_gettimeofday(&now);
794 /* the second has rolled over. check more stuff. */
795 bytes_written = stats_prev_global_write_bucket - global_write_bucket;
796 bytes_read = stats_prev_global_read_bucket - global_read_bucket;
797 /* XXX below we get suspicious if time jumps forward more than 10
798 * seconds, but we never notice if it jumps *back* more than 10 seconds.
799 * This could be useful for detecting that we just NTP'ed to three
800 * weeks ago and it will be 3 weeks and 15 minutes until any of our
801 * events trigger.
803 seconds_elapsed = current_second ? (now.tv_sec - current_second) : 0;
804 stats_n_bytes_read += bytes_read;
805 stats_n_bytes_written += bytes_written;
806 if (accounting_is_enabled(options))
807 accounting_add_bytes(bytes_read, bytes_written, seconds_elapsed);
808 control_event_bandwidth_used((uint32_t)bytes_read,(uint32_t)bytes_written);
810 connection_bucket_refill(&now);
811 stats_prev_global_read_bucket = global_read_bucket;
812 stats_prev_global_write_bucket = global_write_bucket;
814 if (server_mode(options) &&
815 !we_are_hibernating() &&
816 stats_n_seconds_working / TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT !=
817 (stats_n_seconds_working+seconds_elapsed) /
818 TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT) {
819 /* every 20 minutes, check and complain if necessary */
820 routerinfo_t *me = router_get_my_routerinfo();
821 if (!check_whether_orport_reachable())
822 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.",
823 me ? me->address : options->Address, options->ORPort);
824 if (!check_whether_dirport_reachable())
825 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.",
826 me ? me->address : options->Address, options->DirPort);
829 /* if more than 10s have elapsed, probably the clock jumped: doesn't count. */
830 if (seconds_elapsed < 100)
831 stats_n_seconds_working += seconds_elapsed;
832 else
833 circuit_note_clock_jumped(seconds_elapsed);
835 assert_all_pending_dns_resolves_ok();
836 run_scheduled_events(now.tv_sec);
837 assert_all_pending_dns_resolves_ok();
839 current_second = now.tv_sec; /* remember which second it is, for next time */
841 #if 0
842 for (i=0;i<nfds;i++) {
843 conn = connection_array[i];
844 if (connection_has_pending_tls_data(conn) &&
845 connection_is_reading(conn)) {
846 log_fn(LOG_DEBUG,"sock %d has pending bytes.",conn->s);
847 return; /* has pending bytes to read; don't let poll wait. */
850 #endif
852 if (evtimer_add(timeout_event, &one_second))
853 log_fn(LOG_ERR,
854 "Error from libevent when setting one-second timeout event");
857 /** Called when we get a SIGHUP: reload configuration files and keys,
858 * retry all connections, re-upload all descriptors, and so on. */
859 static int do_hup(void) {
860 char keydir[512];
861 or_options_t *options = get_options();
863 log_fn(LOG_NOTICE,"Received sighup. Reloading config.");
864 has_completed_circuit=0;
865 if (accounting_is_enabled(options))
866 accounting_record_bandwidth_usage(time(NULL));
868 addressmap_clear_transient();
869 /* first, reload config variables, in case they've changed */
870 /* no need to provide argc/v, they've been cached inside init_from_config */
871 if (init_from_config(0, NULL) < 0) {
872 log_fn(LOG_ERR,"Reading config failed--see warnings above. For usage, try -h.");
873 return -1;
875 options = get_options(); /* they have changed now */
876 if (authdir_mode(options)) {
877 /* reload the approved-routers file */
878 tor_snprintf(keydir,sizeof(keydir),"%s/approved-routers", options->DataDirectory);
879 log_fn(LOG_INFO,"Reloading approved fingerprints from %s...",keydir);
880 if (dirserv_parse_fingerprint_file(keydir) < 0) {
881 log_fn(LOG_NOTICE, "Error reloading fingerprints. Continuing with old list.");
884 /* Fetch a new directory. Even authdirservers do this. */
885 directory_get_from_dirserver(DIR_PURPOSE_FETCH_DIR, NULL, 1);
886 if (server_mode(options)) {
887 const char *descriptor;
888 /* Restart cpuworker and dnsworker processes, so they get up-to-date
889 * configuration options. */
890 cpuworkers_rotate();
891 dnsworkers_rotate();
892 /* Rebuild fresh descriptor, but leave old one on failure. */
893 router_rebuild_descriptor(1);
894 descriptor = router_get_my_descriptor();
895 if (!descriptor) {
896 log_fn(LOG_WARN,"No descriptor to save.");
897 return 0;
899 tor_snprintf(keydir,sizeof(keydir),"%s/router.desc",
900 options->DataDirectory);
901 log_fn(LOG_INFO,"Saving descriptor to %s...",keydir);
902 if (write_str_to_file(keydir, descriptor, 0)) {
903 return 0;
906 return 0;
909 /** Tor main loop. */
910 static int do_main_loop(void) {
911 int loop_result;
913 /* load the private keys, if we're supposed to have them, and set up the
914 * TLS context. */
915 if (! identity_key_is_set()) {
916 if (init_keys() < 0) {
917 log_fn(LOG_ERR,"Error initializing keys; exiting");
918 return -1;
922 /* Set up our buckets */
923 connection_bucket_init();
924 stats_prev_global_read_bucket = global_read_bucket;
925 stats_prev_global_write_bucket = global_write_bucket;
927 /* load the routers file, or assign the defaults. */
928 if (router_reload_router_list()) {
929 return -1;
932 if (authdir_mode(get_options())) {
933 /* the directory is already here, run startup things */
934 router_retry_connections();
937 if (server_mode(get_options())) {
938 /* launch cpuworkers. Need to do this *after* we've read the onion key. */
939 cpu_init();
942 /* set up once-a-second callback. */
943 second_elapsed_callback(0,0,NULL);
945 for (;;) {
946 if (nt_service_is_stopped())
947 return 0;
948 /* poll until we have an event, or the second ends */
949 loop_result = event_dispatch();
951 /* let catch() handle things like ^c, and otherwise don't worry about it */
952 if (loop_result < 0) {
953 int e = errno;
954 /* let the program survive things like ^z */
955 if (e != EINTR) {
956 #ifdef HAVE_EVENT_GET_METHOD
957 log_fn(LOG_ERR,"libevent poll with %s failed: %s [%d]",
958 event_get_method(), tor_socket_strerror(e), e);
959 #else
960 log_fn(LOG_ERR,"libevent poll failed: %s [%d]",
961 tor_socket_strerror(e), e);
962 #endif
963 return -1;
964 } else {
965 log_fn(LOG_DEBUG,"event poll interrupted.");
966 /* You can't trust the results of this poll(). Go back to the
967 * top of the big for loop. */
968 continue;
972 /* refilling buckets and sending cells happens at the beginning of the
973 * next iteration of the loop, inside prepare_for_poll()
974 * XXXX No longer so.
979 /** Used to implement the SIGNAL control command: if we accept
980 * <b>the_signal</b> as a remote pseudo-signal, then act on it and
981 * return 0. Else return -1. */
982 /* We don't re-use catch() here because:
983 * 1. We handle a different set of signals than those allowed in catch.
984 * 2. Platforms without signal() are unlikely to define SIGfoo.
985 * 3. The control spec is defined to use fixed numeric signal values
986 * which just happen to match the unix values.
989 control_signal_act(int the_signal)
991 switch (the_signal)
993 case 1:
994 signal_callback(0,0,(void*)(uintptr_t)SIGHUP);
995 break;
996 case 2:
997 signal_callback(0,0,(void*)(uintptr_t)SIGINT);
998 break;
999 case 10:
1000 signal_callback(0,0,(void*)(uintptr_t)SIGUSR1);
1001 break;
1002 case 12:
1003 signal_callback(0,0,(void*)(uintptr_t)SIGUSR2);
1004 break;
1005 case 15:
1006 signal_callback(0,0,(void*)(uintptr_t)SIGTERM);
1007 break;
1008 default:
1009 return -1;
1011 return 0;
1014 static void signal_callback(int fd, short events, void *arg)
1016 uintptr_t sig = (uintptr_t)arg;
1017 switch (sig)
1019 case SIGTERM:
1020 log(LOG_ERR,"Catching signal TERM, exiting cleanly.");
1021 tor_cleanup();
1022 exit(0);
1023 break;
1024 case SIGINT:
1025 if (!server_mode(get_options())) { /* do it now */
1026 log(LOG_NOTICE,"Interrupt: exiting cleanly.");
1027 tor_cleanup();
1028 exit(0);
1030 hibernate_begin_shutdown();
1031 break;
1032 #ifdef SIGPIPE
1033 case SIGPIPE:
1034 log(LOG_NOTICE,"Caught sigpipe. Ignoring.");
1035 break;
1036 #endif
1037 case SIGUSR1:
1038 /* prefer to log it at INFO, but make sure we always see it */
1039 dumpstats(get_min_log_level()<LOG_INFO ? get_min_log_level() : LOG_INFO);
1040 break;
1041 case SIGUSR2:
1042 switch_logs_debug();
1043 log(LOG_NOTICE,"Caught USR2. Going to loglevel debug.");
1044 break;
1045 case SIGHUP:
1046 if (do_hup() < 0) {
1047 log_fn(LOG_WARN,"Restart failed (config error?). Exiting.");
1048 tor_cleanup();
1049 exit(1);
1051 break;
1052 #ifdef SIGCHLD
1053 case SIGCHLD:
1054 while (waitpid(-1,NULL,WNOHANG) > 0) ; /* keep reaping until no more zombies */
1055 break;
1056 #endif
1060 /** Write all statistics to the log, with log level 'severity'. Called
1061 * in response to a SIGUSR1. */
1062 static void
1063 dumpstats(int severity) {
1064 int i;
1065 connection_t *conn;
1066 time_t now = time(NULL);
1067 time_t elapsed;
1069 log(severity, "Dumping stats:");
1071 for (i=0;i<nfds;i++) {
1072 conn = connection_array[i];
1073 log(severity, "Conn %d (socket %d) type %d (%s), state %d (%s), created %d secs ago",
1074 i, conn->s, conn->type, CONN_TYPE_TO_STRING(conn->type),
1075 conn->state, conn_state_to_string[conn->type][conn->state], (int)(now - conn->timestamp_created));
1076 if (!connection_is_listener(conn)) {
1077 log(severity,"Conn %d is to '%s:%d'.",i,conn->address, conn->port);
1078 log(severity,"Conn %d: %d bytes waiting on inbuf (last read %d secs ago)",i,
1079 (int)buf_datalen(conn->inbuf),
1080 (int)(now - conn->timestamp_lastread));
1081 log(severity,"Conn %d: %d bytes waiting on outbuf (last written %d secs ago)",i,
1082 (int)buf_datalen(conn->outbuf), (int)(now - conn->timestamp_lastwritten));
1084 circuit_dump_by_conn(conn, severity); /* dump info about all the circuits using this conn */
1086 log(severity,
1087 "Cells processed: %10lu padding\n"
1088 " %10lu create\n"
1089 " %10lu created\n"
1090 " %10lu relay\n"
1091 " (%10lu relayed)\n"
1092 " (%10lu delivered)\n"
1093 " %10lu destroy",
1094 stats_n_padding_cells_processed,
1095 stats_n_create_cells_processed,
1096 stats_n_created_cells_processed,
1097 stats_n_relay_cells_processed,
1098 stats_n_relay_cells_relayed,
1099 stats_n_relay_cells_delivered,
1100 stats_n_destroy_cells_processed);
1101 if (stats_n_data_cells_packaged)
1102 log(severity,"Average packaged cell fullness: %2.3f%%",
1103 100*(((double)stats_n_data_bytes_packaged) /
1104 (stats_n_data_cells_packaged*RELAY_PAYLOAD_SIZE)) );
1105 if (stats_n_data_cells_received)
1106 log(severity,"Average delivered cell fullness: %2.3f%%",
1107 100*(((double)stats_n_data_bytes_received) /
1108 (stats_n_data_cells_received*RELAY_PAYLOAD_SIZE)) );
1110 if (now - time_of_process_start >= 0)
1111 elapsed = now - time_of_process_start;
1112 else
1113 elapsed = 0;
1115 if (elapsed) {
1116 log(severity,
1117 "Average bandwidth: "U64_FORMAT"/%d = %d bytes/sec reading",
1118 U64_PRINTF_ARG(stats_n_bytes_read),
1119 (int)elapsed,
1120 (int) (stats_n_bytes_read/elapsed));
1121 log(severity,
1122 "Average bandwidth: "U64_FORMAT"/%d = %d bytes/sec writing",
1123 U64_PRINTF_ARG(stats_n_bytes_written),
1124 (int)elapsed,
1125 (int) (stats_n_bytes_written/elapsed));
1128 rep_hist_dump_stats(now,severity);
1129 rend_service_dump_stats(severity);
1132 /** Called by exit() as we shut down the process.
1134 static void exit_function(void)
1136 /* NOTE: If we ever daemonize, this gets called immediately. That's
1137 * okay for now, because we only use this on Windows. */
1138 #ifdef MS_WINDOWS
1139 WSACleanup();
1140 #endif
1143 /** Set up the signal handlers for either parent or child. */
1144 void handle_signals(int is_parent)
1146 #ifndef MS_WINDOWS /* do signal stuff only on unix */
1147 int i;
1148 static int signals[] = {
1149 SIGINT, /* do a controlled slow shutdown */
1150 SIGTERM, /* to terminate now */
1151 SIGPIPE, /* otherwise sigpipe kills us */
1152 SIGUSR1, /* dump stats */
1153 SIGUSR2, /* go to loglevel debug */
1154 SIGHUP, /* to reload config, retry conns, etc */
1155 #ifdef SIGXFSZ
1156 SIGXFSZ, /* handle file-too-big resource exhaustion */
1157 #endif
1158 SIGCHLD, /* handle dns/cpu workers that exit */
1159 -1 };
1160 static struct event signal_events[16]; /* bigger than it has to be. */
1161 if (is_parent) {
1162 for (i = 0; signals[i] >= 0; ++i) {
1163 signal_set(&signal_events[i], signals[i], signal_callback,
1164 (void*)(uintptr_t)signals[i]);
1165 if (signal_add(&signal_events[i], NULL))
1166 log_fn(LOG_WARN, "Error from libevent when adding event for signal %d",
1167 signals[i]);
1169 } else {
1170 struct sigaction action;
1171 action.sa_flags = 0;
1172 sigemptyset(&action.sa_mask);
1173 action.sa_handler = SIG_IGN;
1174 sigaction(SIGINT, &action, NULL);
1175 sigaction(SIGTERM, &action, NULL);
1176 sigaction(SIGPIPE, &action, NULL);
1177 sigaction(SIGUSR1, &action, NULL);
1178 sigaction(SIGUSR2, &action, NULL);
1179 sigaction(SIGHUP, &action, NULL);
1180 #ifdef SIGXFSZ
1181 sigaction(SIGXFSZ, &action, NULL);
1182 #endif
1184 #endif /* signal stuff */
1187 /** Main entry point for the Tor command-line client.
1189 static int tor_init(int argc, char *argv[]) {
1190 time_of_process_start = time(NULL);
1191 closeable_connection_lst = smartlist_create();
1192 /* Initialize the history structures. */
1193 rep_hist_init();
1194 /* Initialize the service cache. */
1195 rend_cache_init();
1196 addressmap_init(); /* Init the client dns cache. Do it always, since it's cheap. */
1198 /* give it somewhere to log to initially */
1199 add_temp_log();
1201 log_fn(LOG_NOTICE,"Tor v%s. This is experimental software. Do not rely on it for strong anonymity.",VERSION);
1203 if (network_init()<0) {
1204 log_fn(LOG_ERR,"Error initializing network; exiting.");
1205 return -1;
1207 atexit(exit_function);
1209 if (init_from_config(argc,argv) < 0) {
1210 log_fn(LOG_ERR,"Reading config failed--see warnings above. For usage, try -h.");
1211 return -1;
1214 #ifndef MS_WINDOWS
1215 if (geteuid()==0)
1216 log_fn(LOG_WARN,"You are running Tor as root. You don't need to, and you probably shouldn't.");
1217 #endif
1219 /* only spawn dns handlers if we're a router */
1220 if (server_mode(get_options()) && get_options()->command == CMD_RUN_TOR) {
1221 dns_init(); /* initialize the dns resolve tree, and spawn workers */
1222 /* XXX really, this should get moved to do_main_loop */
1225 handle_signals(1);
1227 crypto_global_init();
1228 crypto_seed_rng();
1229 return 0;
1232 /** Free all memory that we might have allocated somewhere.
1233 * Helps us find the real leaks with dmalloc and the like.
1235 * Also valgrind should then report 0 reachable in its
1236 * leak report */
1237 void tor_free_all(void)
1239 routerlist_free_current();
1240 free_trusted_dir_servers();
1241 addressmap_free_all();
1242 free_socks_policy();
1243 free_dir_policy();
1244 dirserv_free_all();
1245 rend_service_free_all();
1246 rend_cache_free_all();
1247 rep_hist_free_all();
1248 dns_free_all();
1249 clear_pending_onions();
1250 circuit_free_all();
1251 connection_free_all();
1252 config_free_all();
1253 router_free_all_keys();
1254 tor_tls_free_all();
1255 /* stuff in main.c */
1256 smartlist_free(closeable_connection_lst);
1258 close_logs(); /* free log strings. do this last so logs keep working. */
1261 /** Do whatever cleanup is necessary before shutting Tor down. */
1262 void tor_cleanup(void) {
1263 or_options_t *options = get_options();
1264 /* Remove our pid file. We don't care if there was an error when we
1265 * unlink, nothing we could do about it anyways. */
1266 if (options->PidFile && options->command == CMD_RUN_TOR)
1267 unlink(options->PidFile);
1268 if (accounting_is_enabled(options))
1269 accounting_record_bandwidth_usage(time(NULL));
1270 tor_free_all(); /* move tor_free_all back into the ifdef below later. XXX*/
1271 crypto_global_cleanup();
1272 #ifdef USE_DMALLOC
1273 dmalloc_log_unfreed();
1274 dmalloc_shutdown();
1275 #endif
1278 /** Read/create keys as needed, and echo our fingerprint to stdout. */
1279 static void do_list_fingerprint(void)
1281 char buf[FINGERPRINT_LEN+1];
1282 crypto_pk_env_t *k;
1283 const char *nickname = get_options()->Nickname;
1284 if (!server_mode(get_options())) {
1285 printf("Clients don't have long-term identity keys. Exiting.\n");
1286 return;
1288 tor_assert(nickname);
1289 if (init_keys() < 0) {
1290 log_fn(LOG_ERR,"Error initializing keys; exiting");
1291 return;
1293 if (!(k = get_identity_key())) {
1294 log_fn(LOG_ERR,"Error: missing identity key.");
1295 return;
1297 if (crypto_pk_get_fingerprint(k, buf, 1)<0) {
1298 log_fn(LOG_ERR, "Error computing fingerprint");
1299 return;
1301 printf("%s %s\n", nickname, buf);
1304 /** Entry point for password hashing: take the desired password from
1305 * the command line, and print its salted hash to stdout. **/
1306 static void do_hash_password(void)
1309 char output[256];
1310 char key[S2K_SPECIFIER_LEN+DIGEST_LEN];
1312 crypto_rand(key, S2K_SPECIFIER_LEN-1);
1313 key[S2K_SPECIFIER_LEN-1] = (uint8_t)96; /* Hash 64 K of data. */
1314 secret_to_key(key+S2K_SPECIFIER_LEN, DIGEST_LEN,
1315 get_options()->command_arg, strlen(get_options()->command_arg),
1316 key);
1317 if (base64_encode(output, sizeof(output), key, sizeof(key))<0) {
1318 log_fn(LOG_ERR, "Unable to compute base64");
1319 } else {
1320 printf("%s",output);
1324 #ifdef MS_WINDOWS_SERVICE
1325 /** If we're compile to run as an NT service, and the service has been
1326 * shut down, then change our current status and return 1. Else
1327 * return 0.
1329 static int
1330 nt_service_is_stopped(void)
1332 if (service_status.dwCurrentState == SERVICE_STOP_PENDING) {
1333 service_status.dwWin32ExitCode = 0;
1334 service_status.dwCurrentState = SERVICE_STOPPED;
1335 SetServiceStatus(hStatus, &service_status);
1336 return 1;
1338 return 0;
1341 void nt_service_control(DWORD request)
1343 switch (request) {
1344 case SERVICE_CONTROL_STOP:
1345 case SERVICE_CONTROL_SHUTDOWN:
1346 log(LOG_ERR, "Got stop/shutdown request; shutting down cleanly.");
1347 service_status.dwCurrentState = SERVICE_STOP_PENDING;
1348 return;
1350 SetServiceStatus(hStatus, &service_status);
1353 void nt_service_body(int argc, char **argv)
1355 int err;
1356 service_status.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
1357 service_status.dwCurrentState = SERVICE_START_PENDING;
1358 service_status.dwControlsAccepted =
1359 SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
1360 service_status.dwWin32ExitCode = 0;
1361 service_status.dwServiceSpecificExitCode = 0;
1362 service_status.dwCheckPoint = 0;
1363 service_status.dwWaitHint = 1000;
1364 hStatus = RegisterServiceCtrlHandler(GENSRV_SERVICENAME, (LPHANDLER_FUNCTION) nt_service_control);
1366 if (hStatus == 0) {
1367 // failed;
1368 return;
1371 err = tor_init(backup_argc, backup_argv); // refactor this part out of tor_main and do_main_loop
1372 if (err) {
1373 // failed.
1374 service_status.dwCurrentState = SERVICE_STOPPED;
1375 service_status.dwWin32ExitCode = -1;
1376 SetServiceStatus(hStatus, &service_status);
1377 return;
1379 service_status.dwCurrentState = SERVICE_RUNNING;
1380 SetServiceStatus(hStatus, &service_status);
1381 do_main_loop();
1382 tor_cleanup();
1383 return;
1386 void nt_service_main(void)
1388 SERVICE_TABLE_ENTRY table[2];
1389 DWORD result = 0;
1390 table[0].lpServiceName = GENSRV_SERVICENAME;
1391 table[0].lpServiceProc = (LPSERVICE_MAIN_FUNCTION)nt_service_body;
1392 table[1].lpServiceName = NULL;
1393 table[1].lpServiceProc = NULL;
1395 if (!StartServiceCtrlDispatcher(table)) {
1396 result = GetLastError();
1397 printf("Error was %d\n",result);
1398 if (result == ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) {
1399 if (tor_init(backup_argc, backup_argv) < 0)
1400 return;
1401 switch (get_options()->command) {
1402 case CMD_RUN_TOR:
1403 do_main_loop();
1404 break;
1405 case CMD_LIST_FINGERPRINT:
1406 do_list_fingerprint();
1407 break;
1408 case CMD_HASH_PASSWORD:
1409 do_hash_password();
1410 break;
1411 default:
1412 log_fn(LOG_ERR, "Illegal command number %d: internal error.", get_options()->command);
1414 tor_cleanup();
1419 int nt_service_install()
1421 /* XXXX Problems with NT services:
1422 * 1. The configuration file needs to be in the same directory as the .exe
1424 * 2. The exe and the configuration file can't be on any directory path
1425 * that contains a space.
1426 * mje - you can quote the string (i.e., "c:\program files")
1428 * 3. Ideally, there should be one EXE that can either run as a
1429 * separate process (as now) or that can install and run itself
1430 * as an NT service. I have no idea how hard this is.
1431 * mje - should be done. It can install and run itself as a service
1433 * Notes about developing NT services:
1435 * 1. Don't count on your CWD. If an absolute path is not given, the
1436 * fopen() function goes wrong.
1437 * 2. The parameters given to the nt_service_body() function differ
1438 * from those given to main() function.
1441 SC_HANDLE hSCManager = NULL;
1442 SC_HANDLE hService = NULL;
1443 SERVICE_DESCRIPTION sdBuff;
1444 TCHAR szPath[_MAX_PATH];
1445 TCHAR szDrive[_MAX_DRIVE];
1446 TCHAR szDir[_MAX_DIR];
1447 char cmd1[] = " -f ";
1448 char cmd2[] = "\\torrc";
1449 char *command;
1450 int len = 0;
1452 if (0 == GetModuleFileName(NULL, szPath, MAX_PATH))
1453 return 0;
1455 _tsplitpath(szPath, szDrive, szDir, NULL, NULL);
1457 /* Account for the extra quotes */
1458 //len = _MAX_PATH + strlen(cmd1) + _MAX_DRIVE + _MAX_DIR + strlen(cmd2);
1459 len = _MAX_PATH + strlen(cmd1) + _MAX_DRIVE + _MAX_DIR + strlen(cmd2) + 64;
1460 command = tor_malloc(len);
1462 /* Create a quoted command line, like "c:\with spaces\tor.exe" -f
1463 * "c:\with spaces\tor.exe"
1465 if (tor_snprintf(command, len, "\"%s\" --nt-service -f \"%s%storrc\"",
1466 szPath, szDrive, szDir)<0) {
1467 printf("Failed: tor_snprinf()\n");
1468 free(command);
1469 return 0;
1472 if ((hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CREATE_SERVICE)) == NULL) {
1473 printf("Failed: OpenSCManager()\n");
1474 free(command);
1475 return 0;
1478 /* 1/26/2005 mje
1479 * - changed the service start type to auto
1480 * - and changed the lpPassword param to "" instead of NULL as per an
1481 * MSDN article.
1483 if ((hService = CreateService(hSCManager, GENSRV_SERVICENAME, GENSRV_DISPLAYNAME,
1484 SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
1485 SERVICE_AUTO_START, SERVICE_ERROR_IGNORE, command,
1486 NULL, NULL, NULL, NULL, "")) == NULL) {
1487 printf("Failed: CreateService()\n");
1488 CloseServiceHandle(hSCManager);
1489 free(command);
1490 return 0;
1493 /* Start the service initially, so you don't have to muck with it in the SCM
1495 /* Set the service's description */
1496 sdBuff.lpDescription = GENSRV_DESCRIPTION;
1497 ChangeServiceConfig2(hService, SERVICE_CONFIG_DESCRIPTION, &sdBuff);
1499 /* Start the service, so you don't have to muck with it in the SCM */
1500 if (StartService(hService, 0, NULL)) {
1501 /* Loop until the service has finished attempting to start */
1502 while (QueryServiceStatus(hService, &service_status) &&
1503 service_status.dwCurrentState == SERVICE_START_PENDING)
1504 Sleep(500);
1506 /* Check if it started successfully or not */
1507 if (service_status.dwCurrentState == SERVICE_RUNNING)
1508 printf("Service installed and started successfully.\n");
1509 else
1510 printf("Service installed, but failed to start.\n");
1511 } else {
1512 printf("Service installed, but failed to start.\n");
1515 CloseServiceHandle(hService);
1516 CloseServiceHandle(hSCManager);
1517 tor_free(command);
1519 return 0;
1522 int nt_service_remove()
1524 SC_HANDLE hSCManager = NULL;
1525 SC_HANDLE hService = NULL;
1526 SERVICE_STATUS service_status;
1527 BOOL result = FALSE;
1529 if ((hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CREATE_SERVICE)) == NULL) {
1530 printf("Failed: OpenSCManager()\n");
1531 return 0;
1534 if ((hService = OpenService(hSCManager, GENSRV_SERVICENAME, SERVICE_ALL_ACCESS)) == NULL) {
1535 printf("Failed: OpenService()\n");
1536 CloseServiceHandle(hSCManager);
1537 return 0;
1540 result = ControlService(hService, SERVICE_CONTROL_STOP, &service_status);
1541 if (result) {
1542 while (QueryServiceStatus(hService, &service_status))
1544 if (service_status.dwCurrentState == SERVICE_STOP_PENDING)
1545 Sleep(500);
1546 else
1547 break;
1549 if (DeleteService(hService))
1550 printf("Removed service successfully\n");
1551 else
1552 printf("Failed: DeleteService()\n");
1553 } else {
1554 result = DeleteService(hService);
1555 if (result)
1556 printf("Removed service successfully\n");
1557 else
1558 printf("Failed: DeleteService()\n");
1561 CloseServiceHandle(hService);
1562 CloseServiceHandle(hSCManager);
1564 return 0;
1566 #endif
1568 int tor_main(int argc, char *argv[]) {
1569 #ifdef MS_WINDOWS_SERVICE
1570 backup_argv = argv;
1571 backup_argc = argc;
1572 if ((argc >= 2) && !strcmp(argv[1], "-install"))
1573 return nt_service_install();
1574 if ((argc >= 2) && !strcmp(argv[1], "-remove"))
1575 return nt_service_remove();
1576 if ((argc >= 2) && !strcmp(argv[1], "--nt-service")) {
1577 nt_service_main();
1578 return 0;
1580 #endif
1581 if (tor_init(argc, argv)<0)
1582 return -1;
1583 switch (get_options()->command) {
1584 case CMD_RUN_TOR:
1585 #ifdef MS_WINDOWS_SERVICE
1586 service_status.dwCurrentState = SERVICE_RUNNING;
1587 #endif
1588 do_main_loop();
1589 break;
1590 case CMD_LIST_FINGERPRINT:
1591 do_list_fingerprint();
1592 break;
1593 case CMD_HASH_PASSWORD:
1594 do_hash_password();
1595 break;
1596 default:
1597 log_fn(LOG_ERR, "Illegal command number %d: internal error.",
1598 get_options()->command);
1600 tor_cleanup();
1601 return -1;