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 Toplevel module. Handles signals, multiplexes between
11 * connections, implements main loop, and drives scheduled events.
19 /* These signals are defined to help control_signal_act work. */
36 /********* PROTOTYPES **********/
38 static void dumpmemusage(int severity
);
39 static void dumpstats(int severity
); /* log stats */
40 static void conn_read_callback(int fd
, short event
, void *_conn
);
41 static void conn_write_callback(int fd
, short event
, void *_conn
);
42 static void signal_callback(int fd
, short events
, void *arg
);
43 static void second_elapsed_callback(int fd
, short event
, void *args
);
44 static int conn_close_if_marked(int i
);
46 /********* START VARIABLES **********/
48 int global_read_bucket
; /**< Max number of bytes I can read this second. */
49 int global_write_bucket
; /**< Max number of bytes I can write this second. */
51 /** What was the read bucket before the last call to prepare_for_pool?
52 * (used to determine how many bytes we've read). */
53 static int stats_prev_global_read_bucket
;
54 /** What was the write bucket before the last call to prepare_for_pool?
55 * (used to determine how many bytes we've written). */
56 static int stats_prev_global_write_bucket
;
57 /** How many bytes have we read/written since we started the process? */
58 static uint64_t stats_n_bytes_read
= 0;
59 static uint64_t stats_n_bytes_written
= 0;
60 /** What time did this process start up? */
61 long time_of_process_start
= 0;
62 /** How many seconds have we been running? */
63 long stats_n_seconds_working
= 0;
64 /** When do we next download a directory? */
65 static time_t time_to_fetch_directory
= 0;
66 /** When do we next upload our descriptor? */
67 static time_t time_to_force_upload_descriptor
= 0;
68 /** When do we next download a running-routers summary? */
69 static time_t time_to_fetch_running_routers
= 0;
71 /** Array of all open connections; each element corresponds to the element of
72 * poll_array in the same position. The first nfds elements are valid. */
73 static connection_t
*connection_array
[MAXCONNECTIONS
+1] =
75 static smartlist_t
*closeable_connection_lst
= NULL
;
77 static int nfds
=0; /**< Number of connections currently active. */
79 /** We set this to 1 when we've fetched a dir, to know whether to complain
80 * yet about unrecognized nicknames in entrynodes, exitnodes, etc.
81 * Also, we don't try building circuits unless this is 1. */
82 int has_fetched_directory
=0;
84 /** We set this to 1 when we've opened a circuit, so we can print a log
85 * entry to inform the user that Tor is working. */
86 int has_completed_circuit
=0;
89 #define MS_WINDOWS_SERVICE
92 #ifdef MS_WINDOWS_SERVICE
94 #define GENSRV_SERVICENAME TEXT("tor")
95 #define GENSRV_DISPLAYNAME TEXT("Tor Win32 Service")
96 #define GENSRV_DESCRIPTION TEXT("Provides an anonymous Internet communication system")
97 SERVICE_STATUS service_status
;
98 SERVICE_STATUS_HANDLE hStatus
;
99 static char **backup_argv
;
100 static int backup_argc
;
101 static int nt_service_is_stopped(void);
103 #define nt_service_is_stopped() (0)
106 #define CHECK_DESCRIPTOR_INTERVAL 60 /* one minute */
107 #define BUF_SHRINK_INTERVAL 60 /* one minute */
108 #define TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT (20*60) /* 20 minutes */
110 /********* END VARIABLES ************/
112 /****************************************************************************
114 * This section contains accessors and other methods on the connection_array
115 * and poll_array variables (which are global within this file and unavailable
118 ****************************************************************************/
120 /** Add <b>conn</b> to the array of connections that we can poll on. The
121 * connection's socket must be set; the connection starts out
122 * non-reading and non-writing.
125 connection_add(connection_t
*conn
)
128 tor_assert(conn
->s
>= 0);
130 if (nfds
>= get_options()->_ConnLimit
-1) {
131 log_fn(LOG_WARN
,"Failing because we have %d connections already. Please raise your ulimit -n.", nfds
);
135 tor_assert(conn
->poll_index
== -1); /* can only connection_add once */
136 conn
->poll_index
= nfds
;
137 connection_array
[nfds
] = conn
;
139 conn
->read_event
= tor_malloc_zero(sizeof(struct event
));
140 conn
->write_event
= tor_malloc_zero(sizeof(struct event
));
141 event_set(conn
->read_event
, conn
->s
, EV_READ
|EV_PERSIST
,
142 conn_read_callback
, conn
);
143 event_set(conn
->write_event
, conn
->s
, EV_WRITE
|EV_PERSIST
,
144 conn_write_callback
, conn
);
148 log_fn(LOG_INFO
,"new conn type %s, socket %d, nfds %d.",
149 conn_type_to_string(conn
->type
), conn
->s
, nfds
);
154 /** Remove the connection from the global list, and remove the
155 * corresponding poll entry. Calling this function will shift the last
156 * connection (if any) into the position occupied by conn.
159 connection_remove(connection_t
*conn
)
166 log_fn(LOG_INFO
,"removing socket %d (type %s), nfds now %d",
167 conn
->s
, conn_type_to_string(conn
->type
), nfds
-1);
169 tor_assert(conn
->poll_index
>= 0);
170 current_index
= conn
->poll_index
;
171 if (current_index
== nfds
-1) { /* this is the end */
176 connection_unregister(conn
);
178 /* replace this one with the one at the end */
180 connection_array
[current_index
] = connection_array
[nfds
];
181 connection_array
[current_index
]->poll_index
= current_index
;
186 /** If it's an edge conn, remove it from the list
187 * of conn's on this circuit. If it's not on an edge,
188 * flush and send destroys for all circuits on this conn.
190 * If <b>remove</b> is non-zero, then remove it from the
191 * connection_array and closeable_connection_lst.
196 connection_unlink(connection_t
*conn
, int remove
)
198 circuit_about_to_close_connection(conn
);
199 connection_about_to_close_connection(conn
);
201 connection_remove(conn
);
203 smartlist_remove(closeable_connection_lst
, conn
);
204 if (conn
->type
== CONN_TYPE_EXIT
) {
205 assert_connection_edge_not_dns_pending(conn
);
207 connection_free(conn
);
210 /** Schedule <b>conn</b> to be closed. **/
212 add_connection_to_closeable_list(connection_t
*conn
)
214 tor_assert(!smartlist_isin(closeable_connection_lst
, conn
));
215 tor_assert(conn
->marked_for_close
);
216 assert_connection_ok(conn
, time(NULL
));
217 smartlist_add(closeable_connection_lst
, conn
);
220 /** Return 1 if conn is on the closeable list, else return 0. */
222 connection_is_on_closeable_list(connection_t
*conn
)
224 return smartlist_isin(closeable_connection_lst
, conn
);
227 /** Return true iff conn is in the current poll array. */
229 connection_in_array(connection_t
*conn
)
232 for (i
=0; i
<nfds
; ++i
) {
233 if (conn
==connection_array
[i
])
239 /** Set <b>*array</b> to an array of all connections, and <b>*n</b>
240 * to the length of the array. <b>*array</b> and <b>*n</b> must not
244 get_connection_array(connection_t
***array
, int *n
)
246 *array
= connection_array
;
250 /** Set the event mask on <b>conn</b> to <b>events</b>. (The event
251 * mask is a bitmask whose bits are EV_READ and EV_WRITE.)
254 connection_watch_events(connection_t
*conn
, short events
)
259 tor_assert(conn
->read_event
);
260 tor_assert(conn
->write_event
);
262 if (events
& EV_READ
) {
263 r
= event_add(conn
->read_event
, NULL
);
265 r
= event_del(conn
->read_event
);
270 "Error from libevent setting read event state for %d to %swatched.",
271 conn
->s
, (events
& EV_READ
)?"":"un");
273 if (events
& EV_WRITE
) {
274 r
= event_add(conn
->write_event
, NULL
);
276 r
= event_del(conn
->write_event
);
281 "Error from libevent setting read event state for %d to %swatched.",
282 conn
->s
, (events
& EV_WRITE
)?"":"un");
285 /** Return true iff <b>conn</b> is listening for read events. */
287 connection_is_reading(connection_t
*conn
)
291 return conn
->read_event
&& event_pending(conn
->read_event
, EV_READ
, NULL
);
294 /** Tell the main loop to stop notifying <b>conn</b> of any read events. */
296 connection_stop_reading(connection_t
*conn
)
299 tor_assert(conn
->read_event
);
301 log(LOG_DEBUG
,"connection_stop_reading() called.");
302 if (event_del(conn
->read_event
))
303 log_fn(LOG_WARN
, "Error from libevent setting read event state for %d to unwatched.",
307 /** Tell the main loop to start notifying <b>conn</b> of any read events. */
309 connection_start_reading(connection_t
*conn
)
312 tor_assert(conn
->read_event
);
314 if (event_add(conn
->read_event
, NULL
))
315 log_fn(LOG_WARN
, "Error from libevent setting read event state for %d to watched.",
319 /** Return true iff <b>conn</b> is listening for write events. */
321 connection_is_writing(connection_t
*conn
)
325 return conn
->write_event
&& event_pending(conn
->write_event
, EV_WRITE
, NULL
);
328 /** Tell the main loop to stop notifying <b>conn</b> of any write events. */
330 connection_stop_writing(connection_t
*conn
)
333 tor_assert(conn
->write_event
);
335 if (event_del(conn
->write_event
))
336 log_fn(LOG_WARN
, "Error from libevent setting write event state for %d to unwatched.",
341 /** Tell the main loop to start notifying <b>conn</b> of any write events. */
343 connection_start_writing(connection_t
*conn
)
346 tor_assert(conn
->write_event
);
348 if (event_add(conn
->write_event
, NULL
))
349 log_fn(LOG_WARN
, "Error from libevent setting write event state for %d to watched.",
353 /** Close all connections that have been scheduled to get closed */
355 close_closeable_connections(void)
358 for (i
= 0; i
< smartlist_len(closeable_connection_lst
); ) {
359 connection_t
*conn
= smartlist_get(closeable_connection_lst
, i
);
360 if (conn
->poll_index
< 0) {
361 connection_unlink(conn
, 0); /* blow it away right now */
363 if (!conn_close_if_marked(conn
->poll_index
))
369 /** Libevent callback: this gets invoked when (connection_t*)<b>conn</b> has
370 * some data to read. */
372 conn_read_callback(int fd
, short event
, void *_conn
)
374 connection_t
*conn
= _conn
;
376 log_fn(LOG_DEBUG
,"socket %d wants to read.",conn
->s
);
378 assert_connection_ok(conn
, time(NULL
));
380 if (connection_handle_read(conn
) < 0) {
381 if (!conn
->marked_for_close
) {
383 log_fn(LOG_WARN
,"Bug: unhandled error on read for %s connection (fd %d); removing",
384 conn_type_to_string(conn
->type
), conn
->s
);
385 tor_fragile_assert();
387 if (CONN_IS_EDGE(conn
))
388 connection_edge_end_errno(conn
, conn
->cpath_layer
);
389 connection_mark_for_close(conn
);
392 assert_connection_ok(conn
, time(NULL
));
394 if (smartlist_len(closeable_connection_lst
))
395 close_closeable_connections();
398 /** Libevent callback: this gets invoked when (connection_t*)<b>conn</b> has
399 * some data to write. */
401 conn_write_callback(int fd
, short events
, void *_conn
)
403 connection_t
*conn
= _conn
;
405 log_fn(LOG_DEBUG
,"socket %d wants to write.",conn
->s
);
407 assert_connection_ok(conn
, time(NULL
));
409 if (connection_handle_write(conn
) < 0) {
410 if (!conn
->marked_for_close
) {
411 /* this connection is broken. remove it. */
412 log_fn(LOG_WARN
,"Bug: unhandled error on write for %s connection (fd %d); removing",
413 conn_type_to_string(conn
->type
), conn
->s
);
414 tor_fragile_assert();
415 conn
->has_sent_end
= 1; /* otherwise we cry wolf about duplicate close */
416 /* XXX do we need a close-immediate here, so we don't try to flush? */
417 connection_mark_for_close(conn
);
420 assert_connection_ok(conn
, time(NULL
));
422 if (smartlist_len(closeable_connection_lst
))
423 close_closeable_connections();
426 /** If the connection at connection_array[i] is marked for close, then:
427 * - If it has data that it wants to flush, try to flush it.
428 * - If it _still_ has data to flush, and conn->hold_open_until_flushed is
429 * true, then leave the connection open and return.
430 * - Otherwise, remove the connection from connection_array and from
431 * all other lists, close it, and free it.
432 * Returns 1 if the connection was closed, 0 otherwise.
435 conn_close_if_marked(int i
)
440 conn
= connection_array
[i
];
441 if (!conn
->marked_for_close
)
442 return 0; /* nothing to see here, move along */
443 assert_connection_ok(conn
, time(NULL
));
444 assert_all_pending_dns_resolves_ok();
446 log_fn(LOG_INFO
,"Cleaning up connection (fd %d).",conn
->s
);
447 if (conn
->s
>= 0 && connection_wants_to_flush(conn
)) {
448 /* -1 means it's an incomplete edge connection, or that the socket
449 * has already been closed as unflushable. */
450 if (!conn
->hold_open_until_flushed
)
452 "Conn (addr %s, fd %d, type %s, state %d) marked, but wants to flush %d bytes. "
454 conn
->address
, conn
->s
, conn_type_to_string(conn
->type
), conn
->state
,
455 (int)conn
->outbuf_flushlen
, conn
->marked_for_close_file
, conn
->marked_for_close
);
456 if (connection_speaks_cells(conn
)) {
457 if (conn
->state
== OR_CONN_STATE_OPEN
) {
458 retval
= flush_buf_tls(conn
->tls
, conn
->outbuf
, &conn
->outbuf_flushlen
);
460 retval
= -1; /* never flush non-open broken tls connections */
462 retval
= flush_buf(conn
->s
, conn
->outbuf
, &conn
->outbuf_flushlen
);
465 conn
->hold_open_until_flushed
&& connection_wants_to_flush(conn
)) {
466 log_fn(LOG_INFO
,"Holding conn (fd %d) open for more flushing.",conn
->s
);
467 /* XXX should we reset timestamp_lastwritten here? */
470 if (connection_wants_to_flush(conn
)) {
471 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)",
472 safe_str(conn
->address
), conn
->s
, conn_type_to_string(conn
->type
),
474 (int)buf_datalen(conn
->outbuf
), conn
->marked_for_close_file
,
475 conn
->marked_for_close
);
478 connection_unlink(conn
, 1); /* unlink, remove, free */
482 /** We've just tried every dirserver we know about, and none of
483 * them were reachable. Assume the network is down. Change state
484 * so next time an application connection arrives we'll delay it
485 * and try another directory fetch. Kill off all the circuit_wait
486 * streams that are waiting now, since they will all timeout anyway.
489 directory_all_unreachable(time_t now
)
493 has_fetched_directory
=0;
494 stats_n_seconds_working
=0; /* reset it */
496 while ((conn
= connection_get_by_type_state(CONN_TYPE_AP
,
497 AP_CONN_STATE_CIRCUIT_WAIT
))) {
498 log_fn(LOG_NOTICE
,"Network down? Failing connection to '%s:%d'.",
499 safe_str(conn
->socks_request
->address
), conn
->socks_request
->port
);
500 connection_mark_unattached_ap(conn
, END_STREAM_REASON_NET_UNREACHABLE
);
505 * Return the interval to wait betweeen directory downloads, in seconds.
508 get_dir_fetch_period(or_options_t
*options
)
510 if (options
->DirFetchPeriod
)
511 /* Value from config file. */
512 return options
->DirFetchPeriod
;
513 else if (options
->DirPort
)
514 /* Default for directory server */
517 /* Default for average user. */
522 * Return the interval to wait betweeen router status downloads, in seconds.
525 get_status_fetch_period(or_options_t
*options
)
527 if (options
->StatusFetchPeriod
)
528 /* Value from config file. */
529 return options
->StatusFetchPeriod
;
530 else if (options
->DirPort
)
531 /* Default for directory server */
534 /* Default for average user. */
538 /** This function is called whenever we successfully pull down a directory.
539 * If <b>identity_digest</b> is defined, it contains the digest of the
540 * router that just gave us this directory. */
542 directory_has_arrived(time_t now
, char *identity_digest
)
544 or_options_t
*options
= get_options();
546 log_fn(LOG_INFO
, "A directory has arrived.");
548 has_fetched_directory
=1;
549 /* Don't try to upload or download anything for a while
550 * after the directory we had when we started.
552 if (!time_to_fetch_directory
)
553 time_to_fetch_directory
= now
+ get_dir_fetch_period(options
);
555 if (!time_to_force_upload_descriptor
)
556 time_to_force_upload_descriptor
= now
+ options
->DirPostPeriod
;
558 if (!time_to_fetch_running_routers
)
559 time_to_fetch_running_routers
= now
+ get_status_fetch_period(options
);
561 if (server_mode(options
) && identity_digest
) {
562 /* if this is us, then our dirport is reachable */
563 if (router_digest_is_me(identity_digest
))
564 router_dirport_found_reachable();
567 if (server_mode(options
) &&
568 !we_are_hibernating()) { /* connect to the appropriate routers */
569 router_retry_connections();
570 if (identity_digest
) /* we got a fresh directory */
571 consider_testing_reachability();
575 /** Perform regular maintenance tasks for a single connection. This
576 * function gets run once per second per connection by run_scheduled_events.
579 run_connection_housekeeping(int i
, time_t now
)
582 connection_t
*conn
= connection_array
[i
];
583 or_options_t
*options
= get_options();
585 if (conn
->outbuf
&& !buf_datalen(conn
->outbuf
))
586 conn
->timestamp_lastempty
= now
;
588 /* Expire any directory connections that haven't sent anything for 5 min */
589 if (conn
->type
== CONN_TYPE_DIR
&&
590 !conn
->marked_for_close
&&
591 conn
->timestamp_lastwritten
+ 5*60 < now
) {
592 log_fn(LOG_INFO
,"Expiring wedged directory conn (fd %d, purpose %d)",
593 conn
->s
, conn
->purpose
);
594 connection_mark_for_close(conn
);
598 /* If we haven't written to an OR connection for a while, then either nuke
599 the connection or send a keepalive, depending. */
600 if (connection_speaks_cells(conn
) &&
601 now
>= conn
->timestamp_lastwritten
+ options
->KeepalivePeriod
) {
602 routerinfo_t
*router
= router_get_by_digest(conn
->identity_digest
);
603 if (!connection_state_is_open(conn
)) {
604 log_fn(LOG_INFO
,"Expiring non-open OR connection to fd %d (%s:%d).",
605 conn
->s
,conn
->address
, conn
->port
);
606 connection_mark_for_close(conn
);
607 conn
->hold_open_until_flushed
= 1;
608 } else if (we_are_hibernating() && !circuit_get_by_conn(conn
) &&
609 !buf_datalen(conn
->outbuf
)) {
610 log_fn(LOG_INFO
,"Expiring non-used OR connection to fd %d (%s:%d) [Hibernating or exiting].",
611 conn
->s
,conn
->address
, conn
->port
);
612 connection_mark_for_close(conn
);
613 conn
->hold_open_until_flushed
= 1;
614 } else if (!clique_mode(options
) && !circuit_get_by_conn(conn
) &&
615 (!router
|| !server_mode(options
) || !router_is_clique_mode(router
))) {
616 log_fn(LOG_INFO
,"Expiring non-used OR connection to fd %d (%s:%d) [Not in clique mode].",
617 conn
->s
,conn
->address
, conn
->port
);
618 connection_mark_for_close(conn
);
619 conn
->hold_open_until_flushed
= 1;
621 now
>= conn
->timestamp_lastempty
+ options
->KeepalivePeriod
*10 &&
622 now
>= conn
->timestamp_lastwritten
+ options
->KeepalivePeriod
*10) {
623 log_fn(LOG_NOTICE
,"Expiring stuck OR connection to fd %d (%s:%d). (%d bytes to flush; %d seconds since last write)",
624 conn
->s
, conn
->address
, conn
->port
,
625 (int)buf_datalen(conn
->outbuf
),
626 (int)(now
-conn
->timestamp_lastwritten
));
627 connection_mark_for_close(conn
);
628 } else if (!buf_datalen(conn
->outbuf
)) {
629 /* either in clique mode, or we've got a circuit. send a padding cell. */
630 log_fn(LOG_DEBUG
,"Sending keepalive to (%s:%d)",
631 conn
->address
, conn
->port
);
632 memset(&cell
,0,sizeof(cell_t
));
633 cell
.command
= CELL_PADDING
;
634 connection_or_write_cell_to_buf(&cell
, conn
);
639 /** Perform regular maintenance tasks. This function gets run once per
640 * second by prepare_for_poll.
643 run_scheduled_events(time_t now
)
645 static time_t last_rotated_certificate
= 0;
646 static time_t time_to_check_listeners
= 0;
647 static time_t time_to_check_descriptor
= 0;
648 static time_t time_to_shrink_buffers
= 0;
649 or_options_t
*options
= get_options();
652 /** 0. See if we've been asked to shut down and our timeout has
653 * expired; or if our bandwidth limits are exhausted and we
654 * should hibernate; or if it's time to wake up from hibernation.
656 consider_hibernation(now
);
658 /** 1a. Every MIN_ONION_KEY_LIFETIME seconds, rotate the onion keys,
659 * shut down and restart all cpuworkers, and update the directory if
662 if (server_mode(options
) &&
663 get_onion_key_set_at()+MIN_ONION_KEY_LIFETIME
< now
) {
664 log_fn(LOG_INFO
,"Rotating onion key.");
667 if (router_rebuild_descriptor(1)<0) {
668 log_fn(LOG_WARN
, "Couldn't rebuild router descriptor");
670 if (advertised_server_mode())
671 router_upload_dir_desc_to_dirservers(0);
674 /** 1b. Every MAX_SSL_KEY_LIFETIME seconds, we change our TLS context. */
675 if (!last_rotated_certificate
)
676 last_rotated_certificate
= now
;
677 if (last_rotated_certificate
+MAX_SSL_KEY_LIFETIME
< now
) {
678 log_fn(LOG_INFO
,"Rotating tls context.");
679 if (tor_tls_context_new(get_identity_key(), 1, options
->Nickname
,
680 MAX_SSL_KEY_LIFETIME
) < 0) {
681 log_fn(LOG_WARN
, "Error reinitializing TLS context");
682 /* XXX is it a bug here, that we just keep going? */
684 last_rotated_certificate
= now
;
685 /* XXXX We should rotate TLS connections as well; this code doesn't change
689 /** 1c. If we have to change the accounting interval or record
690 * bandwidth used in this accounting interval, do so. */
691 if (accounting_is_enabled(options
))
692 accounting_run_housekeeping(now
);
694 /** 2. Periodically, we consider getting a new directory, getting a
695 * new running-routers list, and/or force-uploading our descriptor
696 * (if we've passed our internal checks). */
697 if (time_to_fetch_directory
< now
) {
698 time_t next_status_fetch
;
699 /* purge obsolete entries */
700 routerlist_remove_old_routers(ROUTER_MAX_AGE
);
702 if (authdir_mode(options
)) {
703 /* We're a directory; dump any old descriptors. */
704 dirserv_remove_old_servers(ROUTER_MAX_AGE
);
706 if (server_mode(options
) && !we_are_hibernating()) {
707 /* dirservers try to reconnect, in case connections have failed;
708 * and normal servers try to reconnect to dirservers */
709 router_retry_connections();
712 directory_get_from_dirserver(DIR_PURPOSE_FETCH_DIR
, NULL
, 1);
713 time_to_fetch_directory
= now
+ get_dir_fetch_period(options
);
714 next_status_fetch
= now
+ get_status_fetch_period(options
);
715 if (time_to_fetch_running_routers
< next_status_fetch
) {
716 time_to_fetch_running_routers
= next_status_fetch
;
719 /* Also, take this chance to remove old information from rephist. */
720 rep_history_clean(now
-24*60*60);
723 if (time_to_fetch_running_routers
< now
) {
724 if (!authdir_mode(options
)) {
725 directory_get_from_dirserver(DIR_PURPOSE_FETCH_RUNNING_LIST
, NULL
, 1);
727 time_to_fetch_running_routers
= now
+ get_status_fetch_period(options
);
730 if (time_to_force_upload_descriptor
< now
) {
731 consider_publishable_server(now
, 1);
733 rend_cache_clean(); /* this should go elsewhere? */
735 time_to_force_upload_descriptor
= now
+ options
->DirPostPeriod
;
738 /* 2b. Once per minute, regenerate and upload the descriptor if the old
739 * one is inaccurate. */
740 if (time_to_check_descriptor
< now
) {
741 time_to_check_descriptor
= now
+ CHECK_DESCRIPTOR_INTERVAL
;
742 consider_publishable_server(now
, 0);
743 /* also, check religiously for reachability, if it's within the first
744 * 20 minutes of our uptime. */
745 if (server_mode(options
) &&
746 stats_n_seconds_working
< TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT
&&
747 !we_are_hibernating())
748 consider_testing_reachability();
751 /** 3a. Every second, we examine pending circuits and prune the
752 * ones which have been pending for more than a few seconds.
753 * We do this before step 4, so it can try building more if
754 * it's not comfortable with the number of available circuits.
756 circuit_expire_building(now
);
758 /** 3b. Also look at pending streams and prune the ones that 'began'
759 * a long time ago but haven't gotten a 'connected' yet.
760 * Do this before step 4, so we can put them back into pending
761 * state to be picked up by the new circuit.
763 connection_ap_expire_beginning();
765 /** 3c. And expire connections that we've held open for too long.
767 connection_expire_held_open();
769 /** 3d. And every 60 seconds, we relaunch listeners if any died. */
770 if (!we_are_hibernating() && time_to_check_listeners
< now
) {
771 retry_all_listeners(0); /* 0 means "only if some died." */
772 time_to_check_listeners
= now
+60;
775 /** 4. Every second, we try a new circuit if there are no valid
776 * circuits. Every NewCircuitPeriod seconds, we expire circuits
777 * that became dirty more than MaxCircuitDirtiness seconds ago,
778 * and we make a new circ if there are no clean circuits.
780 if (has_fetched_directory
&& !we_are_hibernating())
781 circuit_build_needed_circs(now
);
783 /** 5. We do housekeeping for each connection... */
784 for (i
=0;i
<nfds
;i
++) {
785 run_connection_housekeeping(i
, now
);
787 if (time_to_shrink_buffers
< now
) {
788 for (i
=0;i
<nfds
;i
++) {
789 connection_t
*conn
= connection_array
[i
];
791 buf_shrink(conn
->outbuf
);
793 buf_shrink(conn
->inbuf
);
795 time_to_shrink_buffers
= now
+ BUF_SHRINK_INTERVAL
;
798 /** 6. And remove any marked circuits... */
799 circuit_close_all_marked();
801 /** 7. And upload service descriptors if necessary. */
802 if (has_fetched_directory
&& !we_are_hibernating())
803 rend_consider_services_upload(now
);
805 /** 8. and blow away any connections that need to die. have to do this now,
806 * because if we marked a conn for close and left its socket -1, then
807 * we'll pass it to poll/select and bad things will happen.
809 close_closeable_connections();
812 static struct event
*timeout_event
= NULL
;
813 static int n_libevent_errors
= 0;
815 /** Libevent callback: invoked once every second. */
817 second_elapsed_callback(int fd
, short event
, void *args
)
819 static struct timeval one_second
;
820 static long current_second
= 0;
822 size_t bytes_written
;
825 or_options_t
*options
= get_options();
826 if (!timeout_event
) {
827 timeout_event
= tor_malloc_zero(sizeof(struct event
));
828 evtimer_set(timeout_event
, second_elapsed_callback
, NULL
);
829 one_second
.tv_sec
= 1;
830 one_second
.tv_usec
= 0;
833 n_libevent_errors
= 0;
835 /* log_fn(LOG_NOTICE, "Tick."); */
836 tor_gettimeofday(&now
);
838 /* the second has rolled over. check more stuff. */
839 bytes_written
= stats_prev_global_write_bucket
- global_write_bucket
;
840 bytes_read
= stats_prev_global_read_bucket
- global_read_bucket
;
841 /* XXX below we get suspicious if time jumps forward more than 10
842 * seconds, but we never notice if it jumps *back* more than 10 seconds.
843 * This could be useful for detecting that we just NTP'ed to three
844 * weeks ago and it will be 3 weeks and 15 minutes until any of our
847 seconds_elapsed
= current_second
? (now
.tv_sec
- current_second
) : 0;
848 stats_n_bytes_read
+= bytes_read
;
849 stats_n_bytes_written
+= bytes_written
;
850 if (accounting_is_enabled(options
))
851 accounting_add_bytes(bytes_read
, bytes_written
, seconds_elapsed
);
852 control_event_bandwidth_used((uint32_t)bytes_read
,(uint32_t)bytes_written
);
854 connection_bucket_refill(&now
);
855 stats_prev_global_read_bucket
= global_read_bucket
;
856 stats_prev_global_write_bucket
= global_write_bucket
;
858 if (server_mode(options
) &&
859 !we_are_hibernating() &&
860 stats_n_seconds_working
/ TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT
!=
861 (stats_n_seconds_working
+seconds_elapsed
) /
862 TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT
) {
863 /* every 20 minutes, check and complain if necessary */
864 routerinfo_t
*me
= router_get_my_routerinfo();
865 if (!check_whether_orport_reachable())
866 log(LOG_WARN
,"Your server (%s:%d) has not managed to confirm that its ORPort is reachable. Please check your firewalls, ports, address, etc.",
867 me
? me
->address
: options
->Address
, options
->ORPort
);
868 if (!check_whether_dirport_reachable())
869 log(LOG_WARN
,"Your server (%s:%d) has not managed to confirm that its DirPort is reachable. Please check your firewalls, ports, address, etc.",
870 me
? me
->address
: options
->Address
, options
->DirPort
);
873 /* if more than 100s have elapsed, probably the clock jumped: doesn't count. */
874 if (seconds_elapsed
< 100)
875 stats_n_seconds_working
+= seconds_elapsed
;
877 circuit_note_clock_jumped(seconds_elapsed
);
879 run_scheduled_events(now
.tv_sec
);
881 current_second
= now
.tv_sec
; /* remember which second it is, for next time */
883 if (current_second
% 60 == 0)
884 dumpmemusage(get_min_log_level()<LOG_INFO
? get_min_log_level() : LOG_INFO
);
886 if (evtimer_add(timeout_event
, &one_second
))
888 "Error from libevent when setting one-second timeout event");
891 /** Called when a possibly ignorable libevent error occurs; ensures that we
892 * don't get into an infinite loop by ignoring too many errors from
895 got_libevent_error(void)
897 if (++n_libevent_errors
> 8) {
898 log_fn(LOG_ERR
, "Too many libevent errors in one second; dying");
904 /** Called when we get a SIGHUP: reload configuration files and keys,
905 * retry all connections, re-upload all descriptors, and so on. */
910 or_options_t
*options
= get_options();
912 log(LOG_NOTICE
,"Received sighup. Reloading config.");
913 has_completed_circuit
=0;
914 if (accounting_is_enabled(options
))
915 accounting_record_bandwidth_usage(time(NULL
));
917 addressmap_clear_transient();
918 /* first, reload config variables, in case they've changed */
919 /* no need to provide argc/v, they've been cached inside init_from_config */
920 if (init_from_config(0, NULL
) < 0) {
921 log_fn(LOG_ERR
,"Reading config failed--see warnings above. For usage, try -h.");
924 options
= get_options(); /* they have changed now */
925 if (authdir_mode(options
)) {
926 /* reload the approved-routers file */
927 tor_snprintf(keydir
,sizeof(keydir
),"%s/approved-routers", options
->DataDirectory
);
928 log_fn(LOG_INFO
,"Reloading approved fingerprints from %s...",keydir
);
929 if (dirserv_parse_fingerprint_file(keydir
) < 0) {
930 log_fn(LOG_NOTICE
, "Error reloading fingerprints. Continuing with old list.");
933 /* Fetch a new directory. Even authdirservers do this. */
934 directory_get_from_dirserver(DIR_PURPOSE_FETCH_DIR
, NULL
, 1);
935 if (server_mode(options
)) {
936 const char *descriptor
;
937 /* Restart cpuworker and dnsworker processes, so they get up-to-date
938 * configuration options. */
941 /* Rebuild fresh descriptor, but leave old one on failure. */
942 router_rebuild_descriptor(1);
943 descriptor
= router_get_my_descriptor();
945 log_fn(LOG_WARN
,"No descriptor to save.");
948 tor_snprintf(keydir
,sizeof(keydir
),"%s/router.desc",
949 options
->DataDirectory
);
950 log_fn(LOG_INFO
,"Saving descriptor to %s...",keydir
);
951 if (write_str_to_file(keydir
, descriptor
, 0)) {
958 /** Tor main loop. */
964 /* load the private keys, if we're supposed to have them, and set up the
966 if (! identity_key_is_set()) {
967 if (init_keys() < 0) {
968 log_fn(LOG_ERR
,"Error initializing keys; exiting");
973 /* Set up our buckets */
974 connection_bucket_init();
975 stats_prev_global_read_bucket
= global_read_bucket
;
976 stats_prev_global_write_bucket
= global_write_bucket
;
978 /* load the routers file, or assign the defaults. */
979 if (router_reload_router_list()) {
983 if (authdir_mode(get_options())) {
984 /* the directory is already here, run startup things */
985 router_retry_connections();
988 if (server_mode(get_options())) {
989 /* launch cpuworkers. Need to do this *after* we've read the onion key. */
993 /* set up once-a-second callback. */
994 second_elapsed_callback(0,0,NULL
);
997 if (nt_service_is_stopped())
1001 /* Make it easier to tell whether libevent failure is our fault or not. */
1004 /* poll until we have an event, or the second ends */
1005 loop_result
= event_dispatch();
1007 /* let catch() handle things like ^c, and otherwise don't worry about it */
1008 if (loop_result
< 0) {
1009 int e
= tor_socket_errno(-1);
1010 /* let the program survive things like ^z */
1011 if (e
!= EINTR
&& !ERRNO_IS_EINPROGRESS(e
)) {
1012 #ifdef HAVE_EVENT_GET_METHOD
1013 log_fn(LOG_ERR
,"libevent poll with %s failed: %s [%d]",
1014 event_get_method(), tor_socket_strerror(e
), e
);
1016 log_fn(LOG_ERR
,"libevent poll failed: %s [%d]",
1017 tor_socket_strerror(e
), e
);
1021 } else if (e
== EINVAL
) {
1022 log_fn(LOG_WARN
, "EINVAL from libevent: should you upgrade libevent?");
1023 if (got_libevent_error())
1027 if (ERRNO_IS_EINPROGRESS(e
))
1028 log_fn(LOG_WARN
,"libevent poll returned EINPROGRESS? Please report.");
1029 log_fn(LOG_DEBUG
,"event poll interrupted.");
1030 /* You can't trust the results of this poll(). Go back to the
1031 * top of the big for loop. */
1036 /* refilling buckets and sending cells happens at the beginning of the
1037 * next iteration of the loop, inside prepare_for_poll()
1038 * XXXX No longer so.
1043 /** Used to implement the SIGNAL control command: if we accept
1044 * <b>the_signal</b> as a remote pseudo-signal, then act on it and
1045 * return 0. Else return -1. */
1046 /* We don't re-use catch() here because:
1047 * 1. We handle a different set of signals than those allowed in catch.
1048 * 2. Platforms without signal() are unlikely to define SIGfoo.
1049 * 3. The control spec is defined to use fixed numeric signal values
1050 * which just happen to match the unix values.
1053 control_signal_act(int the_signal
)
1058 signal_callback(0,0,(void*)(uintptr_t)SIGHUP
);
1061 signal_callback(0,0,(void*)(uintptr_t)SIGINT
);
1064 signal_callback(0,0,(void*)(uintptr_t)SIGUSR1
);
1067 signal_callback(0,0,(void*)(uintptr_t)SIGUSR2
);
1070 signal_callback(0,0,(void*)(uintptr_t)SIGTERM
);
1078 /** Libevent callback: invoked when we get a signal.
1081 signal_callback(int fd
, short events
, void *arg
)
1083 uintptr_t sig
= (uintptr_t)arg
;
1087 log(LOG_ERR
,"Catching signal TERM, exiting cleanly.");
1092 if (!server_mode(get_options())) { /* do it now */
1093 log(LOG_NOTICE
,"Interrupt: exiting cleanly.");
1097 hibernate_begin_shutdown();
1101 log(LOG_DEBUG
,"Caught sigpipe. Ignoring.");
1105 /* prefer to log it at INFO, but make sure we always see it */
1106 dumpstats(get_min_log_level()<LOG_INFO
? get_min_log_level() : LOG_INFO
);
1109 switch_logs_debug();
1110 log(LOG_NOTICE
,"Caught USR2, going to loglevel debug. Send HUP to change back.");
1114 log_fn(LOG_WARN
,"Restart failed (config error?). Exiting.");
1121 while (waitpid(-1,NULL
,WNOHANG
) > 0) ; /* keep reaping until no more zombies */
1128 * Write current memory uusage information to the log.
1131 dumpmemusage(int severity
)
1133 extern uint64_t buf_total_used
;
1134 extern uint64_t buf_total_alloc
;
1135 extern uint64_t rephist_total_alloc
;
1137 log(severity
, "In buffers: "U64_FORMAT
" used/"U64_FORMAT
" allocated (%d conns).",
1138 U64_PRINTF_ARG(buf_total_used
), U64_PRINTF_ARG(buf_total_alloc
),
1140 log(severity
, "In rephist: "U64_FORMAT
" used.",
1141 U64_PRINTF_ARG(rephist_total_alloc
));
1144 /** Write all statistics to the log, with log level 'severity'. Called
1145 * in response to a SIGUSR1. */
1147 dumpstats(int severity
)
1151 time_t now
= time(NULL
);
1154 log(severity
, "Dumping stats:");
1156 for (i
=0;i
<nfds
;i
++) {
1157 conn
= connection_array
[i
];
1158 log(severity
, "Conn %d (socket %d) type %d (%s), state %d (%s), created %d secs ago",
1159 i
, conn
->s
, conn
->type
, conn_type_to_string(conn
->type
),
1160 conn
->state
, conn_state_to_string(conn
->type
, conn
->state
), (int)(now
- conn
->timestamp_created
));
1161 if (!connection_is_listener(conn
)) {
1162 log(severity
,"Conn %d is to '%s:%d'.",i
,safe_str(conn
->address
), conn
->port
);
1163 log(severity
,"Conn %d: %d bytes waiting on inbuf (len %d, last read %d secs ago)",i
,
1164 (int)buf_datalen(conn
->inbuf
),
1165 (int)buf_capacity(conn
->inbuf
),
1166 (int)(now
- conn
->timestamp_lastread
));
1167 log(severity
,"Conn %d: %d bytes waiting on outbuf (len %d, last written %d secs ago)",i
,
1168 (int)buf_datalen(conn
->outbuf
),
1169 (int)buf_capacity(conn
->outbuf
),
1170 (int)(now
- conn
->timestamp_lastwritten
));
1172 circuit_dump_by_conn(conn
, severity
); /* dump info about all the circuits using this conn */
1175 "Cells processed: %10lu padding\n"
1179 " (%10lu relayed)\n"
1180 " (%10lu delivered)\n"
1182 stats_n_padding_cells_processed
,
1183 stats_n_create_cells_processed
,
1184 stats_n_created_cells_processed
,
1185 stats_n_relay_cells_processed
,
1186 stats_n_relay_cells_relayed
,
1187 stats_n_relay_cells_delivered
,
1188 stats_n_destroy_cells_processed
);
1189 if (stats_n_data_cells_packaged
)
1190 log(severity
,"Average packaged cell fullness: %2.3f%%",
1191 100*(((double)stats_n_data_bytes_packaged
) /
1192 (stats_n_data_cells_packaged
*RELAY_PAYLOAD_SIZE
)) );
1193 if (stats_n_data_cells_received
)
1194 log(severity
,"Average delivered cell fullness: %2.3f%%",
1195 100*(((double)stats_n_data_bytes_received
) /
1196 (stats_n_data_cells_received
*RELAY_PAYLOAD_SIZE
)) );
1198 if (now
- time_of_process_start
>= 0)
1199 elapsed
= now
- time_of_process_start
;
1205 "Average bandwidth: "U64_FORMAT
"/%d = %d bytes/sec reading",
1206 U64_PRINTF_ARG(stats_n_bytes_read
),
1208 (int) (stats_n_bytes_read
/elapsed
));
1210 "Average bandwidth: "U64_FORMAT
"/%d = %d bytes/sec writing",
1211 U64_PRINTF_ARG(stats_n_bytes_written
),
1213 (int) (stats_n_bytes_written
/elapsed
));
1216 log(severity
, "--------------- Dumping memory information:");
1217 dumpmemusage(severity
);
1219 rep_hist_dump_stats(now
,severity
);
1220 rend_service_dump_stats(severity
);
1223 /** Called by exit() as we shut down the process.
1228 /* NOTE: If we ever daemonize, this gets called immediately. That's
1229 * okay for now, because we only use this on Windows. */
1235 /** Set up the signal handlers for either parent or child. */
1237 handle_signals(int is_parent
)
1239 #ifndef MS_WINDOWS /* do signal stuff only on unix */
1241 static int signals
[] = {
1242 SIGINT
, /* do a controlled slow shutdown */
1243 SIGTERM
, /* to terminate now */
1244 SIGPIPE
, /* otherwise sigpipe kills us */
1245 SIGUSR1
, /* dump stats */
1246 SIGUSR2
, /* go to loglevel debug */
1247 SIGHUP
, /* to reload config, retry conns, etc */
1249 SIGXFSZ
, /* handle file-too-big resource exhaustion */
1251 SIGCHLD
, /* handle dns/cpu workers that exit */
1253 static struct event signal_events
[16]; /* bigger than it has to be. */
1255 for (i
= 0; signals
[i
] >= 0; ++i
) {
1256 signal_set(&signal_events
[i
], signals
[i
], signal_callback
,
1257 (void*)(uintptr_t)signals
[i
]);
1258 if (signal_add(&signal_events
[i
], NULL
))
1259 log_fn(LOG_WARN
, "Error from libevent when adding event for signal %d",
1263 struct sigaction action
;
1264 action
.sa_flags
= 0;
1265 sigemptyset(&action
.sa_mask
);
1266 action
.sa_handler
= SIG_IGN
;
1267 sigaction(SIGINT
, &action
, NULL
);
1268 sigaction(SIGTERM
, &action
, NULL
);
1269 sigaction(SIGPIPE
, &action
, NULL
);
1270 sigaction(SIGUSR1
, &action
, NULL
);
1271 sigaction(SIGUSR2
, &action
, NULL
);
1272 sigaction(SIGHUP
, &action
, NULL
);
1274 sigaction(SIGXFSZ
, &action
, NULL
);
1277 #endif /* signal stuff */
1280 /** Main entry point for the Tor command-line client.
1283 tor_init(int argc
, char *argv
[])
1285 time_of_process_start
= time(NULL
);
1286 closeable_connection_lst
= smartlist_create();
1287 /* Initialize the history structures. */
1289 /* Initialize the service cache. */
1291 addressmap_init(); /* Init the client dns cache. Do it always, since it's cheap. */
1293 /* give it somewhere to log to initially */
1296 log(LOG_NOTICE
,"Tor v%s. This is experimental software. Do not rely on it for strong anonymity.",VERSION
);
1298 if (network_init()<0) {
1299 log_fn(LOG_ERR
,"Error initializing network; exiting.");
1302 atexit(exit_function
);
1304 if (init_from_config(argc
,argv
) < 0) {
1305 log_fn(LOG_ERR
,"Reading config failed--see warnings above. For usage, try -h.");
1311 log_fn(LOG_WARN
,"You are running Tor as root. You don't need to, and you probably shouldn't.");
1314 /* only spawn dns handlers if we're a router */
1315 if (server_mode(get_options()) && get_options()->command
== CMD_RUN_TOR
) {
1316 dns_init(); /* initialize the dns resolve tree, and spawn workers */
1317 /* XXX really, this should get moved to do_main_loop */
1322 crypto_global_init();
1323 if (crypto_seed_rng()) {
1324 log_fn(LOG_ERR
, "Unable to seed random number generator. Exiting.");
1330 /** Free all memory that we might have allocated somewhere.
1331 * Helps us find the real leaks with dmalloc and the like.
1333 * Also valgrind should then report 0 reachable in its
1336 tor_free_all(int postfork
)
1338 routerlist_free_current();
1339 free_trusted_dir_servers();
1340 addressmap_free_all();
1341 set_exit_redirects(NULL
); /* free the registered exit redirects */
1342 free_socks_policy();
1345 rend_service_free_all();
1346 rend_cache_free_all();
1347 rep_hist_free_all();
1349 clear_pending_onions();
1351 connection_free_all();
1354 router_free_all_keys();
1357 /* stuff in main.c */
1358 smartlist_free(closeable_connection_lst
);
1359 tor_free(timeout_event
);
1362 close_logs(); /* free log strings. do this last so logs keep working. */
1366 /** Do whatever cleanup is necessary before shutting Tor down. */
1369 or_options_t
*options
= get_options();
1370 /* Remove our pid file. We don't care if there was an error when we
1371 * unlink, nothing we could do about it anyways. */
1372 if (options
->PidFile
&& options
->command
== CMD_RUN_TOR
)
1373 unlink(options
->PidFile
);
1374 if (accounting_is_enabled(options
))
1375 accounting_record_bandwidth_usage(time(NULL
));
1376 tor_free_all(0); /* move tor_free_all back into the ifdef below later. XXX*/
1377 crypto_global_cleanup();
1379 dmalloc_log_unfreed();
1384 /** Read/create keys as needed, and echo our fingerprint to stdout. */
1386 do_list_fingerprint(void)
1388 char buf
[FINGERPRINT_LEN
+1];
1390 const char *nickname
= get_options()->Nickname
;
1391 if (!server_mode(get_options())) {
1392 printf("Clients don't have long-term identity keys. Exiting.\n");
1395 tor_assert(nickname
);
1396 if (init_keys() < 0) {
1397 log_fn(LOG_ERR
,"Error initializing keys; exiting");
1400 if (!(k
= get_identity_key())) {
1401 log_fn(LOG_ERR
,"Error: missing identity key.");
1404 if (crypto_pk_get_fingerprint(k
, buf
, 1)<0) {
1405 log_fn(LOG_ERR
, "Error computing fingerprint");
1408 printf("%s %s\n", nickname
, buf
);
1411 /** Entry point for password hashing: take the desired password from
1412 * the command line, and print its salted hash to stdout. **/
1414 do_hash_password(void)
1418 char key
[S2K_SPECIFIER_LEN
+DIGEST_LEN
];
1420 crypto_rand(key
, S2K_SPECIFIER_LEN
-1);
1421 key
[S2K_SPECIFIER_LEN
-1] = (uint8_t)96; /* Hash 64 K of data. */
1422 secret_to_key(key
+S2K_SPECIFIER_LEN
, DIGEST_LEN
,
1423 get_options()->command_arg
, strlen(get_options()->command_arg
),
1425 base16_encode(output
, sizeof(output
), key
, sizeof(key
));
1426 printf("16:%s\n",output
);
1429 #ifdef MS_WINDOWS_SERVICE
1430 /** If we're compile to run as an NT service, and the service has been
1431 * shut down, then change our current status and return 1. Else
1435 nt_service_is_stopped(void)
1437 if (service_status
.dwCurrentState
== SERVICE_STOP_PENDING
) {
1438 service_status
.dwWin32ExitCode
= 0;
1439 service_status
.dwCurrentState
= SERVICE_STOPPED
;
1440 SetServiceStatus(hStatus
, &service_status
);
1448 nt_service_control(DWORD request
)
1451 case SERVICE_CONTROL_STOP
:
1452 case SERVICE_CONTROL_SHUTDOWN
:
1453 log(LOG_ERR
, "Got stop/shutdown request; shutting down cleanly.");
1454 service_status
.dwCurrentState
= SERVICE_STOP_PENDING
;
1457 SetServiceStatus(hStatus
, &service_status
);
1462 nt_service_body(int argc
, char **argv
)
1465 service_status
.dwServiceType
= SERVICE_WIN32_OWN_PROCESS
;
1466 service_status
.dwCurrentState
= SERVICE_START_PENDING
;
1467 service_status
.dwControlsAccepted
=
1468 SERVICE_ACCEPT_STOP
| SERVICE_ACCEPT_SHUTDOWN
;
1469 service_status
.dwWin32ExitCode
= 0;
1470 service_status
.dwServiceSpecificExitCode
= 0;
1471 service_status
.dwCheckPoint
= 0;
1472 service_status
.dwWaitHint
= 1000;
1473 hStatus
= RegisterServiceCtrlHandler(GENSRV_SERVICENAME
, (LPHANDLER_FUNCTION
) nt_service_control
);
1480 err
= tor_init(backup_argc
, backup_argv
); // refactor this part out of tor_main and do_main_loop
1483 service_status
.dwCurrentState
= SERVICE_STOPPED
;
1484 service_status
.dwWin32ExitCode
= -1;
1485 SetServiceStatus(hStatus
, &service_status
);
1488 service_status
.dwCurrentState
= SERVICE_RUNNING
;
1489 SetServiceStatus(hStatus
, &service_status
);
1497 nt_service_main(void)
1499 SERVICE_TABLE_ENTRY table
[2];
1501 table
[0].lpServiceName
= GENSRV_SERVICENAME
;
1502 table
[0].lpServiceProc
= (LPSERVICE_MAIN_FUNCTION
)nt_service_body
;
1503 table
[1].lpServiceName
= NULL
;
1504 table
[1].lpServiceProc
= NULL
;
1506 if (!StartServiceCtrlDispatcher(table
)) {
1507 result
= GetLastError();
1508 printf("Error was %d\n",result
);
1509 if (result
== ERROR_FAILED_SERVICE_CONTROLLER_CONNECT
) {
1510 if (tor_init(backup_argc
, backup_argv
) < 0)
1512 switch (get_options()->command
) {
1516 case CMD_LIST_FINGERPRINT
:
1517 do_list_fingerprint();
1519 case CMD_HASH_PASSWORD
:
1522 case CMD_VERIFY_CONFIG
:
1523 printf("Configuration was valid\n");
1526 log_fn(LOG_ERR
, "Illegal command number %d: internal error.", get_options()->command
);
1535 nt_service_install(void)
1537 /* XXXX Problems with NT services:
1538 * 1. The configuration file needs to be in the same directory as the .exe
1540 * 2. The exe and the configuration file can't be on any directory path
1541 * that contains a space.
1542 * mje - you can quote the string (i.e., "c:\program files")
1544 * 3. Ideally, there should be one EXE that can either run as a
1545 * separate process (as now) or that can install and run itself
1546 * as an NT service. I have no idea how hard this is.
1547 * mje - should be done. It can install and run itself as a service
1549 * Notes about developing NT services:
1551 * 1. Don't count on your CWD. If an absolute path is not given, the
1552 * fopen() function goes wrong.
1553 * 2. The parameters given to the nt_service_body() function differ
1554 * from those given to main() function.
1557 SC_HANDLE hSCManager
= NULL
;
1558 SC_HANDLE hService
= NULL
;
1559 SERVICE_DESCRIPTION sdBuff
;
1560 TCHAR szPath
[_MAX_PATH
];
1561 TCHAR szDrive
[_MAX_DRIVE
];
1562 TCHAR szDir
[_MAX_DIR
];
1563 char cmd1
[] = " -f ";
1564 char cmd2
[] = "\\torrc";
1568 if (0 == GetModuleFileName(NULL
, szPath
, MAX_PATH
))
1571 _tsplitpath(szPath
, szDrive
, szDir
, NULL
, NULL
);
1573 /* Account for the extra quotes */
1574 //len = _MAX_PATH + strlen(cmd1) + _MAX_DRIVE + _MAX_DIR + strlen(cmd2);
1575 len
= _MAX_PATH
+ strlen(cmd1
) + _MAX_DRIVE
+ _MAX_DIR
+ strlen(cmd2
) + 64;
1576 command
= tor_malloc(len
);
1578 /* Create a quoted command line, like "c:\with spaces\tor.exe" -f
1579 * "c:\with spaces\tor.exe"
1581 if (tor_snprintf(command
, len
, "\"%s\" --nt-service -f \"%s%storrc\"",
1582 szPath
, szDrive
, szDir
)<0) {
1583 printf("Failed: tor_snprinf()\n");
1588 if ((hSCManager
= OpenSCManager(NULL
, NULL
, SC_MANAGER_CREATE_SERVICE
)) == NULL
) {
1589 printf("Failed: OpenSCManager()\n");
1595 * - changed the service start type to auto
1596 * - and changed the lpPassword param to "" instead of NULL as per an
1599 if ((hService
= CreateService(hSCManager
, GENSRV_SERVICENAME
, GENSRV_DISPLAYNAME
,
1600 SERVICE_ALL_ACCESS
, SERVICE_WIN32_OWN_PROCESS
,
1601 SERVICE_AUTO_START
, SERVICE_ERROR_IGNORE
, command
,
1602 NULL
, NULL
, NULL
, NULL
, "")) == NULL
) {
1603 printf("Failed: CreateService()\n");
1604 CloseServiceHandle(hSCManager
);
1609 /* Start the service initially, so you don't have to muck with it in the SCM
1611 /* Set the service's description */
1612 sdBuff
.lpDescription
= GENSRV_DESCRIPTION
;
1613 ChangeServiceConfig2(hService
, SERVICE_CONFIG_DESCRIPTION
, &sdBuff
);
1615 /* Start the service, so you don't have to muck with it in the SCM */
1616 if (StartService(hService
, 0, NULL
)) {
1617 /* Loop until the service has finished attempting to start */
1618 while (QueryServiceStatus(hService
, &service_status
) &&
1619 service_status
.dwCurrentState
== SERVICE_START_PENDING
)
1622 /* Check if it started successfully or not */
1623 if (service_status
.dwCurrentState
== SERVICE_RUNNING
)
1624 printf("Service installed and started successfully.\n");
1626 printf("Service installed, but failed to start.\n");
1628 printf("Service installed, but failed to start.\n");
1631 CloseServiceHandle(hService
);
1632 CloseServiceHandle(hSCManager
);
1640 nt_service_remove(void)
1642 SC_HANDLE hSCManager
= NULL
;
1643 SC_HANDLE hService
= NULL
;
1644 SERVICE_STATUS service_status
;
1645 BOOL result
= FALSE
;
1647 if ((hSCManager
= OpenSCManager(NULL
, NULL
, SC_MANAGER_CREATE_SERVICE
)) == NULL
) {
1648 printf("Failed: OpenSCManager()\n");
1652 if ((hService
= OpenService(hSCManager
, GENSRV_SERVICENAME
, SERVICE_ALL_ACCESS
)) == NULL
) {
1653 printf("Failed: OpenService()\n");
1654 CloseServiceHandle(hSCManager
);
1658 result
= ControlService(hService
, SERVICE_CONTROL_STOP
, &service_status
);
1660 while (QueryServiceStatus(hService
, &service_status
))
1662 if (service_status
.dwCurrentState
== SERVICE_STOP_PENDING
)
1667 if (DeleteService(hService
))
1668 printf("Removed service successfully\n");
1670 printf("Failed: DeleteService()\n");
1672 result
= DeleteService(hService
);
1674 printf("Removed service successfully\n");
1676 printf("Failed: DeleteService()\n");
1679 CloseServiceHandle(hService
);
1680 CloseServiceHandle(hSCManager
);
1688 tor_main(int argc
, char *argv
[])
1690 #ifdef MS_WINDOWS_SERVICE
1693 if ((argc
>= 2) && !strcmp(argv
[1], "-install"))
1694 return nt_service_install();
1695 if ((argc
>= 2) && !strcmp(argv
[1], "-remove"))
1696 return nt_service_remove();
1697 if ((argc
>= 2) && !strcmp(argv
[1], "--nt-service")) {
1702 if (tor_init(argc
, argv
)<0)
1704 switch (get_options()->command
) {
1706 #ifdef MS_WINDOWS_SERVICE
1707 service_status
.dwCurrentState
= SERVICE_RUNNING
;
1711 case CMD_LIST_FINGERPRINT
:
1712 do_list_fingerprint();
1714 case CMD_HASH_PASSWORD
:
1717 case CMD_VERIFY_CONFIG
:
1718 printf("Configuration was valid\n");
1721 log_fn(LOG_ERR
, "Illegal command number %d: internal error.",
1722 get_options()->command
);