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 */
6 const char main_c_id
[] = "$Id$";
10 * \brief Tor main loop and startup functions.
18 /* These signals are defined to help control_signal_act work. */
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] =
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;
88 #define MS_WINDOWS_SERVICE
91 #ifdef MS_WINDOWS_SERVICE
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);
102 #define nt_service_is_stopped() (0)
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
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
) {
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
);
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
);
144 log_fn(LOG_INFO
,"new conn type %s, socket %d, nfds %d.",
145 conn_type_to_string(conn
->type
), conn
->s
, nfds
);
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
) {
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 */
170 connection_unregister(conn
);
172 /* replace this one with the one at the end */
174 connection_array
[current_index
] = connection_array
[nfds
];
175 connection_array
[current_index
]->poll_index
= current_index
;
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.
189 static void connection_unlink(connection_t
*conn
, int remove
) {
190 circuit_about_to_close_connection(conn
);
191 connection_about_to_close_connection(conn
);
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. **/
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
) {
220 for (i
=0; i
<nfds
; ++i
) {
221 if (conn
==connection_array
[i
])
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
231 void get_connection_array(connection_t
***array
, int *n
) {
232 *array
= connection_array
;
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
) {
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
);
249 r
= event_del(conn
->read_event
);
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
);
260 r
= event_del(conn
->write_event
);
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
) {
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
) {
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.",
287 /** Tell the main loop to start notifying <b>conn</b> of any read events. */
288 void connection_start_reading(connection_t
*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.",
297 /** Return true iff <b>conn</b> is listening for write events. */
298 int connection_is_writing(connection_t
*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
) {
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.",
315 /** Tell the main loop to start notifying <b>conn</b> of any write events. */
316 void connection_start_writing(connection_t
*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.",
325 /** Close all connections that have been scheduled to get closed */
327 close_closeable_connections(void)
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 */
335 if (!conn_close_if_marked(conn
->poll_index
))
341 /** Libevent callback: this gets invoked when (connection_t*)<b>conn</b> has
342 * some data to read. */
344 conn_read_callback(int fd
, short event
, void *_conn
)
346 connection_t
*conn
= _conn
;
347 if (conn
->marked_for_close
)
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
) {
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();
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
)
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
) {
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
)
425 "Conn (addr %s, fd %d, type %s, state %d) marked, but wants to flush %d bytes. "
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
);
433 retval
= -1; /* never flush non-open broken tls connections */
435 retval
= flush_buf(conn
->s
, conn
->outbuf
, &conn
->outbuf_flushlen
);
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? */
443 if (connection_wants_to_flush(conn
)) {
444 log_fn(LOG_NOTICE
,"Conn (addr %s, fd %d, type %s, state %d) is being closed, but there are still %d bytes we can't write. (Marked at %s:%d)",
445 conn
->address
, conn
->s
, conn_type_to_string(conn
->type
), conn
->state
,
446 (int)buf_datalen(conn
->outbuf
), conn
->marked_for_close_file
,
447 conn
->marked_for_close
);
450 connection_unlink(conn
, 1); /* unlink, remove, free */
454 /** We've just tried every dirserver we know about, and none of
455 * them were reachable. Assume the network is down. Change state
456 * so next time an application connection arrives we'll delay it
457 * and try another directory fetch. Kill off all the circuit_wait
458 * streams that are waiting now, since they will all timeout anyway.
460 void directory_all_unreachable(time_t now
) {
463 has_fetched_directory
=0;
464 stats_n_seconds_working
=0; /* reset it */
466 while ((conn
= connection_get_by_type_state(CONN_TYPE_AP
,
467 AP_CONN_STATE_CIRCUIT_WAIT
))) {
468 log_fn(LOG_NOTICE
,"Network down? Failing connection to '%s:%d'.",
469 conn
->socks_request
->address
, conn
->socks_request
->port
);
470 connection_mark_unattached_ap(conn
, END_STREAM_REASON_NET_UNREACHABLE
);
475 get_dir_fetch_period(or_options_t
*options
)
477 if (options
->DirFetchPeriod
)
478 /* Value from config file. */
479 return options
->DirFetchPeriod
;
480 else if (options
->DirPort
)
481 /* Default for directory server */
484 /* Default for average user. */
489 get_status_fetch_period(or_options_t
*options
)
491 if (options
->StatusFetchPeriod
)
492 /* Value from config file. */
493 return options
->StatusFetchPeriod
;
494 else if (options
->DirPort
)
495 /* Default for directory server */
498 /* Default for average user. */
502 /** This function is called whenever we successfully pull down a directory.
503 * If <b>identity_digest</b> is defined, it contains the digest of the
504 * router that just gave us this directory. */
505 void directory_has_arrived(time_t now
, char *identity_digest
) {
506 or_options_t
*options
= get_options();
508 log_fn(LOG_INFO
, "A directory has arrived.");
510 has_fetched_directory
=1;
511 /* Don't try to upload or download anything for a while
512 * after the directory we had when we started.
514 if (!time_to_fetch_directory
)
515 time_to_fetch_directory
= now
+ get_dir_fetch_period(options
);
517 if (!time_to_force_upload_descriptor
)
518 time_to_force_upload_descriptor
= now
+ options
->DirPostPeriod
;
520 if (!time_to_fetch_running_routers
)
521 time_to_fetch_running_routers
= now
+ get_status_fetch_period(options
);
523 if (server_mode(options
) && identity_digest
) {
524 /* if this is us, then our dirport is reachable */
525 routerinfo_t
*router
= router_get_by_digest(identity_digest
);
527 log_fn(LOG_WARN
,"Bug: router_get_by_digest doesn't find me.");
528 if (router
&& router_is_me(router
)) {
529 router_dirport_found_reachable();
533 if (server_mode(options
) &&
534 !we_are_hibernating()) { /* connect to the appropriate routers */
535 router_retry_connections();
536 if (identity_digest
) /* we got a fresh directory */
537 consider_testing_reachability();
541 /** Perform regular maintenance tasks for a single connection. This
542 * function gets run once per second per connection by run_scheduled_events.
544 static void run_connection_housekeeping(int i
, time_t now
) {
546 connection_t
*conn
= connection_array
[i
];
547 or_options_t
*options
= get_options();
549 if (conn
->outbuf
&& !buf_datalen(conn
->outbuf
))
550 conn
->timestamp_lastempty
= now
;
552 /* Expire any directory connections that haven't sent anything for 5 min */
553 if (conn
->type
== CONN_TYPE_DIR
&&
554 !conn
->marked_for_close
&&
555 conn
->timestamp_lastwritten
+ 5*60 < now
) {
556 log_fn(LOG_INFO
,"Expiring wedged directory conn (fd %d, purpose %d)", conn
->s
, conn
->purpose
);
557 connection_mark_for_close(conn
);
561 /* If we haven't written to an OR connection for a while, then either nuke
562 the connection or send a keepalive, depending. */
563 if (connection_speaks_cells(conn
) &&
564 now
>= conn
->timestamp_lastwritten
+ options
->KeepalivePeriod
) {
565 routerinfo_t
*router
= router_get_by_digest(conn
->identity_digest
);
566 if (!connection_state_is_open(conn
)) {
567 log_fn(LOG_INFO
,"Expiring non-open OR connection to fd %d (%s:%d).",
568 conn
->s
,conn
->address
, conn
->port
);
569 connection_mark_for_close(conn
);
570 conn
->hold_open_until_flushed
= 1;
571 } else if (we_are_hibernating() && !circuit_get_by_conn(conn
) &&
572 !buf_datalen(conn
->outbuf
)) {
573 log_fn(LOG_INFO
,"Expiring non-used OR connection to fd %d (%s:%d) [Hibernating or exiting].",
574 conn
->s
,conn
->address
, conn
->port
);
575 connection_mark_for_close(conn
);
576 conn
->hold_open_until_flushed
= 1;
577 } else if (!clique_mode(options
) && !circuit_get_by_conn(conn
) &&
578 (!router
|| !server_mode(options
) || !router_is_clique_mode(router
))) {
579 log_fn(LOG_INFO
,"Expiring non-used OR connection to fd %d (%s:%d) [Not in clique mode].",
580 conn
->s
,conn
->address
, conn
->port
);
581 connection_mark_for_close(conn
);
582 conn
->hold_open_until_flushed
= 1;
584 now
>= conn
->timestamp_lastempty
+ options
->KeepalivePeriod
*10 &&
585 now
>= conn
->timestamp_lastwritten
+ options
->KeepalivePeriod
*10) {
586 log_fn(LOG_NOTICE
,"Expiring stuck OR connection to fd %d (%s:%d). (%d bytes to flush; %d seconds since last write)",
587 conn
->s
, conn
->address
, conn
->port
,
588 (int)buf_datalen(conn
->outbuf
),
589 (int)(now
-conn
->timestamp_lastwritten
));
590 connection_mark_for_close(conn
);
592 /* either in clique mode, or we've got a circuit. send a padding cell. */
593 log_fn(LOG_DEBUG
,"Sending keepalive to (%s:%d)",
594 conn
->address
, conn
->port
);
595 memset(&cell
,0,sizeof(cell_t
));
596 cell
.command
= CELL_PADDING
;
597 connection_or_write_cell_to_buf(&cell
, conn
);
602 /** Perform regular maintenance tasks. This function gets run once per
603 * second by prepare_for_poll.
605 static void run_scheduled_events(time_t now
) {
606 static time_t last_rotated_certificate
= 0;
607 static time_t time_to_check_listeners
= 0;
608 static time_t time_to_check_descriptor
= 0;
609 or_options_t
*options
= get_options();
612 /** 0. See if we've been asked to shut down and our timeout has
613 * expired; or if our bandwidth limits are exhausted and we
614 * should hibernate; or if it's time to wake up from hibernation.
616 consider_hibernation(now
);
618 /** 1a. Every MIN_ONION_KEY_LIFETIME seconds, rotate the onion keys,
619 * shut down and restart all cpuworkers, and update the directory if
622 if (server_mode(options
) &&
623 get_onion_key_set_at()+MIN_ONION_KEY_LIFETIME
< now
) {
624 log_fn(LOG_INFO
,"Rotating onion key.");
627 if (router_rebuild_descriptor(1)<0) {
628 log_fn(LOG_WARN
, "Couldn't rebuild router descriptor");
630 if (advertised_server_mode())
631 router_upload_dir_desc_to_dirservers(0);
634 /** 1b. Every MAX_SSL_KEY_LIFETIME seconds, we change our TLS context. */
635 if (!last_rotated_certificate
)
636 last_rotated_certificate
= now
;
637 if (last_rotated_certificate
+MAX_SSL_KEY_LIFETIME
< now
) {
638 log_fn(LOG_INFO
,"Rotating tls context.");
639 if (tor_tls_context_new(get_identity_key(), 1, options
->Nickname
,
640 MAX_SSL_KEY_LIFETIME
) < 0) {
641 log_fn(LOG_WARN
, "Error reinitializing TLS context");
642 /* XXX is it a bug here, that we just keep going? */
644 last_rotated_certificate
= now
;
645 /* XXXX We should rotate TLS connections as well; this code doesn't change
649 /** 1c. If we have to change the accounting interval or record
650 * bandwidth used in this accounting interval, do so. */
651 if (accounting_is_enabled(options
))
652 accounting_run_housekeeping(now
);
654 /** 2. Periodically, we consider getting a new directory, getting a
655 * new running-routers list, and/or force-uploading our descriptor
656 * (if we've passed our internal checks). */
657 if (time_to_fetch_directory
< now
) {
658 time_t next_status_fetch
;
659 /* purge obsolete entries */
660 routerlist_remove_old_routers(ROUTER_MAX_AGE
);
662 if (authdir_mode(options
)) {
663 /* We're a directory; dump any old descriptors. */
664 dirserv_remove_old_servers(ROUTER_MAX_AGE
);
666 if (server_mode(options
) && !we_are_hibernating()) {
667 /* dirservers try to reconnect, in case connections have failed;
668 * and normal servers try to reconnect to dirservers */
669 router_retry_connections();
672 directory_get_from_dirserver(DIR_PURPOSE_FETCH_DIR
, NULL
, 1);
673 time_to_fetch_directory
= now
+ get_dir_fetch_period(options
);
674 next_status_fetch
= now
+ get_status_fetch_period(options
);
675 if (time_to_fetch_running_routers
< next_status_fetch
) {
676 time_to_fetch_running_routers
= next_status_fetch
;
679 /* Also, take this chance to remove old information from rephist. */
680 rep_history_clean(now
-24*60*60);
683 if (time_to_fetch_running_routers
< now
) {
684 if (!authdir_mode(options
)) {
685 directory_get_from_dirserver(DIR_PURPOSE_FETCH_RUNNING_LIST
, NULL
, 1);
687 time_to_fetch_running_routers
= now
+ get_status_fetch_period(options
);
690 if (time_to_force_upload_descriptor
< now
) {
691 consider_publishable_server(now
, 1);
693 rend_cache_clean(); /* this should go elsewhere? */
695 time_to_force_upload_descriptor
= now
+ options
->DirPostPeriod
;
698 /* 2b. Once per minute, regenerate and upload the descriptor if the old
699 * one is inaccurate. */
700 if (time_to_check_descriptor
< now
) {
701 time_to_check_descriptor
= now
+ CHECK_DESCRIPTOR_INTERVAL
;
702 consider_publishable_server(now
, 0);
703 /* also, check religiously for reachability, if it's within the first
704 * 20 minutes of our uptime. */
705 if (server_mode(options
) &&
706 stats_n_seconds_working
< TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT
&&
707 !we_are_hibernating())
708 consider_testing_reachability();
711 /** 3a. Every second, we examine pending circuits and prune the
712 * ones which have been pending for more than a few seconds.
713 * We do this before step 4, so it can try building more if
714 * it's not comfortable with the number of available circuits.
716 circuit_expire_building(now
);
718 /** 3b. Also look at pending streams and prune the ones that 'began'
719 * a long time ago but haven't gotten a 'connected' yet.
720 * Do this before step 4, so we can put them back into pending
721 * state to be picked up by the new circuit.
723 connection_ap_expire_beginning();
725 /** 3c. And expire connections that we've held open for too long.
727 connection_expire_held_open();
729 /** 3d. And every 60 seconds, we relaunch listeners if any died. */
730 if (!we_are_hibernating() && time_to_check_listeners
< now
) {
731 retry_all_listeners(0); /* 0 means "only if some died." */
732 time_to_check_listeners
= now
+60;
735 /** 4. Every second, we try a new circuit if there are no valid
736 * circuits. Every NewCircuitPeriod seconds, we expire circuits
737 * that became dirty more than MaxCircuitDirtiness seconds ago,
738 * and we make a new circ if there are no clean circuits.
740 if (has_fetched_directory
&& !we_are_hibernating())
741 circuit_build_needed_circs(now
);
743 /** 5. We do housekeeping for each connection... */
744 for (i
=0;i
<nfds
;i
++) {
745 run_connection_housekeeping(i
, now
);
748 /** 6. And remove any marked circuits... */
749 circuit_close_all_marked();
751 /** 7. And upload service descriptors if necessary. */
752 if (has_fetched_directory
&& !we_are_hibernating())
753 rend_consider_services_upload(now
);
755 /** 8. and blow away any connections that need to die. have to do this now,
756 * because if we marked a conn for close and left its socket -1, then
757 * we'll pass it to poll/select and bad things will happen.
759 close_closeable_connections();
762 /** Libevent callback: invoked once every second. */
763 static void second_elapsed_callback(int fd
, short event
, void *args
)
765 static struct event
*timeout_event
= NULL
;
766 static struct timeval one_second
;
767 static long current_second
= 0;
769 size_t bytes_written
;
772 or_options_t
*options
= get_options();
773 if (!timeout_event
) {
774 timeout_event
= tor_malloc_zero(sizeof(struct event
));
775 evtimer_set(timeout_event
, second_elapsed_callback
, NULL
);
776 one_second
.tv_sec
= 1;
777 one_second
.tv_usec
= 0;
780 /* log_fn(LOG_NOTICE, "Tick."); */
781 tor_gettimeofday(&now
);
783 /* the second has rolled over. check more stuff. */
784 bytes_written
= stats_prev_global_write_bucket
- global_write_bucket
;
785 bytes_read
= stats_prev_global_read_bucket
- global_read_bucket
;
786 /* XXX below we get suspicious if time jumps forward more than 10
787 * seconds, but we never notice if it jumps *back* more than 10 seconds.
788 * This could be useful for detecting that we just NTP'ed to three
789 * weeks ago and it will be 3 weeks and 15 minutes until any of our
792 seconds_elapsed
= current_second
? (now
.tv_sec
- current_second
) : 0;
793 stats_n_bytes_read
+= bytes_read
;
794 stats_n_bytes_written
+= bytes_written
;
795 if (accounting_is_enabled(options
))
796 accounting_add_bytes(bytes_read
, bytes_written
, seconds_elapsed
);
797 control_event_bandwidth_used((uint32_t)bytes_read
,(uint32_t)bytes_written
);
799 connection_bucket_refill(&now
);
800 stats_prev_global_read_bucket
= global_read_bucket
;
801 stats_prev_global_write_bucket
= global_write_bucket
;
803 if (server_mode(options
) &&
804 !we_are_hibernating() &&
805 stats_n_seconds_working
/ TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT
!=
806 (stats_n_seconds_working
+seconds_elapsed
) /
807 TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT
) {
808 /* every 20 minutes, check and complain if necessary */
809 routerinfo_t
*me
= router_get_my_routerinfo();
810 if (!check_whether_orport_reachable())
811 log_fn(LOG_WARN
,"Your server (%s:%d) has not managed to confirm that its ORPort is reachable. Please check your firewalls, ports, address, etc.",
812 me
? me
->address
: options
->Address
, options
->ORPort
);
813 if (!check_whether_dirport_reachable())
814 log_fn(LOG_WARN
,"Your server (%s:%d) has not managed to confirm that its DirPort is reachable. Please check your firewalls, ports, address, etc.",
815 me
? me
->address
: options
->Address
, options
->DirPort
);
818 /* if more than 10s have elapsed, probably the clock jumped: doesn't count. */
819 if (seconds_elapsed
< 100)
820 stats_n_seconds_working
+= seconds_elapsed
;
822 circuit_note_clock_jumped(seconds_elapsed
);
824 run_scheduled_events(now
.tv_sec
);
826 current_second
= now
.tv_sec
; /* remember which second it is, for next time */
828 if (evtimer_add(timeout_event
, &one_second
))
830 "Error from libevent when setting one-second timeout event");
833 /** Called when we get a SIGHUP: reload configuration files and keys,
834 * retry all connections, re-upload all descriptors, and so on. */
835 static int do_hup(void) {
837 or_options_t
*options
= get_options();
839 log_fn(LOG_NOTICE
,"Received sighup. Reloading config.");
840 has_completed_circuit
=0;
841 if (accounting_is_enabled(options
))
842 accounting_record_bandwidth_usage(time(NULL
));
844 addressmap_clear_transient();
845 /* first, reload config variables, in case they've changed */
846 /* no need to provide argc/v, they've been cached inside init_from_config */
847 if (init_from_config(0, NULL
) < 0) {
848 log_fn(LOG_ERR
,"Reading config failed--see warnings above. For usage, try -h.");
851 options
= get_options(); /* they have changed now */
852 if (authdir_mode(options
)) {
853 /* reload the approved-routers file */
854 tor_snprintf(keydir
,sizeof(keydir
),"%s/approved-routers", options
->DataDirectory
);
855 log_fn(LOG_INFO
,"Reloading approved fingerprints from %s...",keydir
);
856 if (dirserv_parse_fingerprint_file(keydir
) < 0) {
857 log_fn(LOG_NOTICE
, "Error reloading fingerprints. Continuing with old list.");
860 /* Fetch a new directory. Even authdirservers do this. */
861 directory_get_from_dirserver(DIR_PURPOSE_FETCH_DIR
, NULL
, 1);
862 if (server_mode(options
)) {
863 const char *descriptor
;
864 /* Restart cpuworker and dnsworker processes, so they get up-to-date
865 * configuration options. */
868 /* Rebuild fresh descriptor, but leave old one on failure. */
869 router_rebuild_descriptor(1);
870 descriptor
= router_get_my_descriptor();
872 log_fn(LOG_WARN
,"No descriptor to save.");
875 tor_snprintf(keydir
,sizeof(keydir
),"%s/router.desc",
876 options
->DataDirectory
);
877 log_fn(LOG_INFO
,"Saving descriptor to %s...",keydir
);
878 if (write_str_to_file(keydir
, descriptor
, 0)) {
885 /** Tor main loop. */
886 static int do_main_loop(void) {
889 /* load the private keys, if we're supposed to have them, and set up the
891 if (! identity_key_is_set()) {
892 if (init_keys() < 0) {
893 log_fn(LOG_ERR
,"Error initializing keys; exiting");
898 /* Set up our buckets */
899 connection_bucket_init();
900 stats_prev_global_read_bucket
= global_read_bucket
;
901 stats_prev_global_write_bucket
= global_write_bucket
;
903 /* load the routers file, or assign the defaults. */
904 if (router_reload_router_list()) {
908 if (authdir_mode(get_options())) {
909 /* the directory is already here, run startup things */
910 router_retry_connections();
913 if (server_mode(get_options())) {
914 /* launch cpuworkers. Need to do this *after* we've read the onion key. */
918 /* set up once-a-second callback. */
919 second_elapsed_callback(0,0,NULL
);
922 if (nt_service_is_stopped())
926 /* Make it easier to tell whether libevent failure is our fault or not. */
929 /* poll until we have an event, or the second ends */
930 loop_result
= event_dispatch();
932 /* let catch() handle things like ^c, and otherwise don't worry about it */
933 if (loop_result
< 0) {
935 /* let the program survive things like ^z */
937 #ifdef HAVE_EVENT_GET_METHOD
938 log_fn(LOG_ERR
,"libevent poll with %s failed: %s [%d]",
939 event_get_method(), tor_socket_strerror(e
), e
);
941 log_fn(LOG_ERR
,"libevent poll failed: %s [%d]",
942 tor_socket_strerror(e
), e
);
946 log_fn(LOG_DEBUG
,"event poll interrupted.");
947 /* You can't trust the results of this poll(). Go back to the
948 * top of the big for loop. */
953 /* refilling buckets and sending cells happens at the beginning of the
954 * next iteration of the loop, inside prepare_for_poll()
960 /** Used to implement the SIGNAL control command: if we accept
961 * <b>the_signal</b> as a remote pseudo-signal, then act on it and
962 * return 0. Else return -1. */
963 /* We don't re-use catch() here because:
964 * 1. We handle a different set of signals than those allowed in catch.
965 * 2. Platforms without signal() are unlikely to define SIGfoo.
966 * 3. The control spec is defined to use fixed numeric signal values
967 * which just happen to match the unix values.
970 control_signal_act(int the_signal
)
975 signal_callback(0,0,(void*)(uintptr_t)SIGHUP
);
978 signal_callback(0,0,(void*)(uintptr_t)SIGINT
);
981 signal_callback(0,0,(void*)(uintptr_t)SIGUSR1
);
984 signal_callback(0,0,(void*)(uintptr_t)SIGUSR2
);
987 signal_callback(0,0,(void*)(uintptr_t)SIGTERM
);
995 static void signal_callback(int fd
, short events
, void *arg
)
997 uintptr_t sig
= (uintptr_t)arg
;
1001 log(LOG_ERR
,"Catching signal TERM, exiting cleanly.");
1006 if (!server_mode(get_options())) { /* do it now */
1007 log(LOG_NOTICE
,"Interrupt: exiting cleanly.");
1011 hibernate_begin_shutdown();
1015 log(LOG_NOTICE
,"Caught sigpipe. Ignoring.");
1019 /* prefer to log it at INFO, but make sure we always see it */
1020 dumpstats(get_min_log_level()<LOG_INFO
? get_min_log_level() : LOG_INFO
);
1023 switch_logs_debug();
1024 log(LOG_NOTICE
,"Caught USR2. Going to loglevel debug.");
1028 log_fn(LOG_WARN
,"Restart failed (config error?). Exiting.");
1035 while (waitpid(-1,NULL
,WNOHANG
) > 0) ; /* keep reaping until no more zombies */
1041 /** Write all statistics to the log, with log level 'severity'. Called
1042 * in response to a SIGUSR1. */
1044 dumpstats(int severity
) {
1047 time_t now
= time(NULL
);
1050 log(severity
, "Dumping stats:");
1052 for (i
=0;i
<nfds
;i
++) {
1053 conn
= connection_array
[i
];
1054 log(severity
, "Conn %d (socket %d) type %d (%s), state %d (%s), created %d secs ago",
1055 i
, conn
->s
, conn
->type
, conn_type_to_string(conn
->type
),
1056 conn
->state
, conn_state_to_string(conn
->type
, conn
->state
), (int)(now
- conn
->timestamp_created
));
1057 if (!connection_is_listener(conn
)) {
1058 log(severity
,"Conn %d is to '%s:%d'.",i
,conn
->address
, conn
->port
);
1059 log(severity
,"Conn %d: %d bytes waiting on inbuf (last read %d secs ago)",i
,
1060 (int)buf_datalen(conn
->inbuf
),
1061 (int)(now
- conn
->timestamp_lastread
));
1062 log(severity
,"Conn %d: %d bytes waiting on outbuf (last written %d secs ago)",i
,
1063 (int)buf_datalen(conn
->outbuf
), (int)(now
- conn
->timestamp_lastwritten
));
1065 circuit_dump_by_conn(conn
, severity
); /* dump info about all the circuits using this conn */
1068 "Cells processed: %10lu padding\n"
1072 " (%10lu relayed)\n"
1073 " (%10lu delivered)\n"
1075 stats_n_padding_cells_processed
,
1076 stats_n_create_cells_processed
,
1077 stats_n_created_cells_processed
,
1078 stats_n_relay_cells_processed
,
1079 stats_n_relay_cells_relayed
,
1080 stats_n_relay_cells_delivered
,
1081 stats_n_destroy_cells_processed
);
1082 if (stats_n_data_cells_packaged
)
1083 log(severity
,"Average packaged cell fullness: %2.3f%%",
1084 100*(((double)stats_n_data_bytes_packaged
) /
1085 (stats_n_data_cells_packaged
*RELAY_PAYLOAD_SIZE
)) );
1086 if (stats_n_data_cells_received
)
1087 log(severity
,"Average delivered cell fullness: %2.3f%%",
1088 100*(((double)stats_n_data_bytes_received
) /
1089 (stats_n_data_cells_received
*RELAY_PAYLOAD_SIZE
)) );
1091 if (now
- time_of_process_start
>= 0)
1092 elapsed
= now
- time_of_process_start
;
1098 "Average bandwidth: "U64_FORMAT
"/%d = %d bytes/sec reading",
1099 U64_PRINTF_ARG(stats_n_bytes_read
),
1101 (int) (stats_n_bytes_read
/elapsed
));
1103 "Average bandwidth: "U64_FORMAT
"/%d = %d bytes/sec writing",
1104 U64_PRINTF_ARG(stats_n_bytes_written
),
1106 (int) (stats_n_bytes_written
/elapsed
));
1109 rep_hist_dump_stats(now
,severity
);
1110 rend_service_dump_stats(severity
);
1113 /** Called by exit() as we shut down the process.
1115 static void exit_function(void)
1117 /* NOTE: If we ever daemonize, this gets called immediately. That's
1118 * okay for now, because we only use this on Windows. */
1124 /** Set up the signal handlers for either parent or child. */
1125 void handle_signals(int is_parent
)
1127 #ifndef MS_WINDOWS /* do signal stuff only on unix */
1129 static int signals
[] = {
1130 SIGINT
, /* do a controlled slow shutdown */
1131 SIGTERM
, /* to terminate now */
1132 SIGPIPE
, /* otherwise sigpipe kills us */
1133 SIGUSR1
, /* dump stats */
1134 SIGUSR2
, /* go to loglevel debug */
1135 SIGHUP
, /* to reload config, retry conns, etc */
1137 SIGXFSZ
, /* handle file-too-big resource exhaustion */
1139 SIGCHLD
, /* handle dns/cpu workers that exit */
1141 static struct event signal_events
[16]; /* bigger than it has to be. */
1143 for (i
= 0; signals
[i
] >= 0; ++i
) {
1144 signal_set(&signal_events
[i
], signals
[i
], signal_callback
,
1145 (void*)(uintptr_t)signals
[i
]);
1146 if (signal_add(&signal_events
[i
], NULL
))
1147 log_fn(LOG_WARN
, "Error from libevent when adding event for signal %d",
1151 struct sigaction action
;
1152 action
.sa_flags
= 0;
1153 sigemptyset(&action
.sa_mask
);
1154 action
.sa_handler
= SIG_IGN
;
1155 sigaction(SIGINT
, &action
, NULL
);
1156 sigaction(SIGTERM
, &action
, NULL
);
1157 sigaction(SIGPIPE
, &action
, NULL
);
1158 sigaction(SIGUSR1
, &action
, NULL
);
1159 sigaction(SIGUSR2
, &action
, NULL
);
1160 sigaction(SIGHUP
, &action
, NULL
);
1162 sigaction(SIGXFSZ
, &action
, NULL
);
1165 #endif /* signal stuff */
1168 /** Main entry point for the Tor command-line client.
1170 static int tor_init(int argc
, char *argv
[]) {
1171 time_of_process_start
= time(NULL
);
1172 closeable_connection_lst
= smartlist_create();
1173 /* Initialize the history structures. */
1175 /* Initialize the service cache. */
1177 addressmap_init(); /* Init the client dns cache. Do it always, since it's cheap. */
1179 /* give it somewhere to log to initially */
1182 log_fn(LOG_NOTICE
,"Tor v%s. This is experimental software. Do not rely on it for strong anonymity.",VERSION
);
1184 if (network_init()<0) {
1185 log_fn(LOG_ERR
,"Error initializing network; exiting.");
1188 atexit(exit_function
);
1190 if (init_from_config(argc
,argv
) < 0) {
1191 log_fn(LOG_ERR
,"Reading config failed--see warnings above. For usage, try -h.");
1197 log_fn(LOG_WARN
,"You are running Tor as root. You don't need to, and you probably shouldn't.");
1200 /* only spawn dns handlers if we're a router */
1201 if (server_mode(get_options()) && get_options()->command
== CMD_RUN_TOR
) {
1202 dns_init(); /* initialize the dns resolve tree, and spawn workers */
1203 /* XXX really, this should get moved to do_main_loop */
1208 crypto_global_init();
1213 /** Free all memory that we might have allocated somewhere.
1214 * Helps us find the real leaks with dmalloc and the like.
1216 * Also valgrind should then report 0 reachable in its
1218 void tor_free_all(void)
1220 routerlist_free_current();
1221 free_trusted_dir_servers();
1222 addressmap_free_all();
1223 free_socks_policy();
1226 rend_service_free_all();
1227 rend_cache_free_all();
1228 rep_hist_free_all();
1230 clear_pending_onions();
1232 connection_free_all();
1234 router_free_all_keys();
1236 /* stuff in main.c */
1237 smartlist_free(closeable_connection_lst
);
1239 close_logs(); /* free log strings. do this last so logs keep working. */
1242 /** Do whatever cleanup is necessary before shutting Tor down. */
1243 void tor_cleanup(void) {
1244 or_options_t
*options
= get_options();
1245 /* Remove our pid file. We don't care if there was an error when we
1246 * unlink, nothing we could do about it anyways. */
1247 if (options
->PidFile
&& options
->command
== CMD_RUN_TOR
)
1248 unlink(options
->PidFile
);
1249 if (accounting_is_enabled(options
))
1250 accounting_record_bandwidth_usage(time(NULL
));
1251 tor_free_all(); /* move tor_free_all back into the ifdef below later. XXX*/
1252 crypto_global_cleanup();
1254 dmalloc_log_unfreed();
1259 /** Read/create keys as needed, and echo our fingerprint to stdout. */
1260 static void do_list_fingerprint(void)
1262 char buf
[FINGERPRINT_LEN
+1];
1264 const char *nickname
= get_options()->Nickname
;
1265 if (!server_mode(get_options())) {
1266 printf("Clients don't have long-term identity keys. Exiting.\n");
1269 tor_assert(nickname
);
1270 if (init_keys() < 0) {
1271 log_fn(LOG_ERR
,"Error initializing keys; exiting");
1274 if (!(k
= get_identity_key())) {
1275 log_fn(LOG_ERR
,"Error: missing identity key.");
1278 if (crypto_pk_get_fingerprint(k
, buf
, 1)<0) {
1279 log_fn(LOG_ERR
, "Error computing fingerprint");
1282 printf("%s %s\n", nickname
, buf
);
1285 /** Entry point for password hashing: take the desired password from
1286 * the command line, and print its salted hash to stdout. **/
1287 static void do_hash_password(void)
1291 char key
[S2K_SPECIFIER_LEN
+DIGEST_LEN
];
1293 crypto_rand(key
, S2K_SPECIFIER_LEN
-1);
1294 key
[S2K_SPECIFIER_LEN
-1] = (uint8_t)96; /* Hash 64 K of data. */
1295 secret_to_key(key
+S2K_SPECIFIER_LEN
, DIGEST_LEN
,
1296 get_options()->command_arg
, strlen(get_options()->command_arg
),
1298 if (base64_encode(output
, sizeof(output
), key
, sizeof(key
))<0) {
1299 log_fn(LOG_ERR
, "Unable to compute base64");
1301 printf("%s",output
);
1305 #ifdef MS_WINDOWS_SERVICE
1306 /** If we're compile to run as an NT service, and the service has been
1307 * shut down, then change our current status and return 1. Else
1311 nt_service_is_stopped(void)
1313 if (service_status
.dwCurrentState
== SERVICE_STOP_PENDING
) {
1314 service_status
.dwWin32ExitCode
= 0;
1315 service_status
.dwCurrentState
= SERVICE_STOPPED
;
1316 SetServiceStatus(hStatus
, &service_status
);
1322 void nt_service_control(DWORD request
)
1325 case SERVICE_CONTROL_STOP
:
1326 case SERVICE_CONTROL_SHUTDOWN
:
1327 log(LOG_ERR
, "Got stop/shutdown request; shutting down cleanly.");
1328 service_status
.dwCurrentState
= SERVICE_STOP_PENDING
;
1331 SetServiceStatus(hStatus
, &service_status
);
1334 void nt_service_body(int argc
, char **argv
)
1337 service_status
.dwServiceType
= SERVICE_WIN32_OWN_PROCESS
;
1338 service_status
.dwCurrentState
= SERVICE_START_PENDING
;
1339 service_status
.dwControlsAccepted
=
1340 SERVICE_ACCEPT_STOP
| SERVICE_ACCEPT_SHUTDOWN
;
1341 service_status
.dwWin32ExitCode
= 0;
1342 service_status
.dwServiceSpecificExitCode
= 0;
1343 service_status
.dwCheckPoint
= 0;
1344 service_status
.dwWaitHint
= 1000;
1345 hStatus
= RegisterServiceCtrlHandler(GENSRV_SERVICENAME
, (LPHANDLER_FUNCTION
) nt_service_control
);
1352 err
= tor_init(backup_argc
, backup_argv
); // refactor this part out of tor_main and do_main_loop
1355 service_status
.dwCurrentState
= SERVICE_STOPPED
;
1356 service_status
.dwWin32ExitCode
= -1;
1357 SetServiceStatus(hStatus
, &service_status
);
1360 service_status
.dwCurrentState
= SERVICE_RUNNING
;
1361 SetServiceStatus(hStatus
, &service_status
);
1367 void nt_service_main(void)
1369 SERVICE_TABLE_ENTRY table
[2];
1371 table
[0].lpServiceName
= GENSRV_SERVICENAME
;
1372 table
[0].lpServiceProc
= (LPSERVICE_MAIN_FUNCTION
)nt_service_body
;
1373 table
[1].lpServiceName
= NULL
;
1374 table
[1].lpServiceProc
= NULL
;
1376 if (!StartServiceCtrlDispatcher(table
)) {
1377 result
= GetLastError();
1378 printf("Error was %d\n",result
);
1379 if (result
== ERROR_FAILED_SERVICE_CONTROLLER_CONNECT
) {
1380 if (tor_init(backup_argc
, backup_argv
) < 0)
1382 switch (get_options()->command
) {
1386 case CMD_LIST_FINGERPRINT
:
1387 do_list_fingerprint();
1389 case CMD_HASH_PASSWORD
:
1393 log_fn(LOG_ERR
, "Illegal command number %d: internal error.", get_options()->command
);
1400 int nt_service_install()
1402 /* XXXX Problems with NT services:
1403 * 1. The configuration file needs to be in the same directory as the .exe
1405 * 2. The exe and the configuration file can't be on any directory path
1406 * that contains a space.
1407 * mje - you can quote the string (i.e., "c:\program files")
1409 * 3. Ideally, there should be one EXE that can either run as a
1410 * separate process (as now) or that can install and run itself
1411 * as an NT service. I have no idea how hard this is.
1412 * mje - should be done. It can install and run itself as a service
1414 * Notes about developing NT services:
1416 * 1. Don't count on your CWD. If an absolute path is not given, the
1417 * fopen() function goes wrong.
1418 * 2. The parameters given to the nt_service_body() function differ
1419 * from those given to main() function.
1422 SC_HANDLE hSCManager
= NULL
;
1423 SC_HANDLE hService
= NULL
;
1424 SERVICE_DESCRIPTION sdBuff
;
1425 TCHAR szPath
[_MAX_PATH
];
1426 TCHAR szDrive
[_MAX_DRIVE
];
1427 TCHAR szDir
[_MAX_DIR
];
1428 char cmd1
[] = " -f ";
1429 char cmd2
[] = "\\torrc";
1433 if (0 == GetModuleFileName(NULL
, szPath
, MAX_PATH
))
1436 _tsplitpath(szPath
, szDrive
, szDir
, NULL
, NULL
);
1438 /* Account for the extra quotes */
1439 //len = _MAX_PATH + strlen(cmd1) + _MAX_DRIVE + _MAX_DIR + strlen(cmd2);
1440 len
= _MAX_PATH
+ strlen(cmd1
) + _MAX_DRIVE
+ _MAX_DIR
+ strlen(cmd2
) + 64;
1441 command
= tor_malloc(len
);
1443 /* Create a quoted command line, like "c:\with spaces\tor.exe" -f
1444 * "c:\with spaces\tor.exe"
1446 if (tor_snprintf(command
, len
, "\"%s\" --nt-service -f \"%s%storrc\"",
1447 szPath
, szDrive
, szDir
)<0) {
1448 printf("Failed: tor_snprinf()\n");
1453 if ((hSCManager
= OpenSCManager(NULL
, NULL
, SC_MANAGER_CREATE_SERVICE
)) == NULL
) {
1454 printf("Failed: OpenSCManager()\n");
1460 * - changed the service start type to auto
1461 * - and changed the lpPassword param to "" instead of NULL as per an
1464 if ((hService
= CreateService(hSCManager
, GENSRV_SERVICENAME
, GENSRV_DISPLAYNAME
,
1465 SERVICE_ALL_ACCESS
, SERVICE_WIN32_OWN_PROCESS
,
1466 SERVICE_AUTO_START
, SERVICE_ERROR_IGNORE
, command
,
1467 NULL
, NULL
, NULL
, NULL
, "")) == NULL
) {
1468 printf("Failed: CreateService()\n");
1469 CloseServiceHandle(hSCManager
);
1474 /* Start the service initially, so you don't have to muck with it in the SCM
1476 /* Set the service's description */
1477 sdBuff
.lpDescription
= GENSRV_DESCRIPTION
;
1478 ChangeServiceConfig2(hService
, SERVICE_CONFIG_DESCRIPTION
, &sdBuff
);
1480 /* Start the service, so you don't have to muck with it in the SCM */
1481 if (StartService(hService
, 0, NULL
)) {
1482 /* Loop until the service has finished attempting to start */
1483 while (QueryServiceStatus(hService
, &service_status
) &&
1484 service_status
.dwCurrentState
== SERVICE_START_PENDING
)
1487 /* Check if it started successfully or not */
1488 if (service_status
.dwCurrentState
== SERVICE_RUNNING
)
1489 printf("Service installed and started successfully.\n");
1491 printf("Service installed, but failed to start.\n");
1493 printf("Service installed, but failed to start.\n");
1496 CloseServiceHandle(hService
);
1497 CloseServiceHandle(hSCManager
);
1503 int nt_service_remove()
1505 SC_HANDLE hSCManager
= NULL
;
1506 SC_HANDLE hService
= NULL
;
1507 SERVICE_STATUS service_status
;
1508 BOOL result
= FALSE
;
1510 if ((hSCManager
= OpenSCManager(NULL
, NULL
, SC_MANAGER_CREATE_SERVICE
)) == NULL
) {
1511 printf("Failed: OpenSCManager()\n");
1515 if ((hService
= OpenService(hSCManager
, GENSRV_SERVICENAME
, SERVICE_ALL_ACCESS
)) == NULL
) {
1516 printf("Failed: OpenService()\n");
1517 CloseServiceHandle(hSCManager
);
1521 result
= ControlService(hService
, SERVICE_CONTROL_STOP
, &service_status
);
1523 while (QueryServiceStatus(hService
, &service_status
))
1525 if (service_status
.dwCurrentState
== SERVICE_STOP_PENDING
)
1530 if (DeleteService(hService
))
1531 printf("Removed service successfully\n");
1533 printf("Failed: DeleteService()\n");
1535 result
= DeleteService(hService
);
1537 printf("Removed service successfully\n");
1539 printf("Failed: DeleteService()\n");
1542 CloseServiceHandle(hService
);
1543 CloseServiceHandle(hSCManager
);
1549 int tor_main(int argc
, char *argv
[]) {
1550 #ifdef MS_WINDOWS_SERVICE
1553 if ((argc
>= 2) && !strcmp(argv
[1], "-install"))
1554 return nt_service_install();
1555 if ((argc
>= 2) && !strcmp(argv
[1], "-remove"))
1556 return nt_service_remove();
1557 if ((argc
>= 2) && !strcmp(argv
[1], "--nt-service")) {
1562 if (tor_init(argc
, argv
)<0)
1564 switch (get_options()->command
) {
1566 #ifdef MS_WINDOWS_SERVICE
1567 service_status
.dwCurrentState
= SERVICE_RUNNING
;
1571 case CMD_LIST_FINGERPRINT
:
1572 do_list_fingerprint();
1574 case CMD_HASH_PASSWORD
:
1578 log_fn(LOG_ERR
, "Illegal command number %d: internal error.",
1579 get_options()->command
);