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