Add an extra flush attempt when closing wedged dir conns, in an attempt to isolate...
[tor.git] / src / or / main.c
blob65be5363ef21c27bb6cddcf0c079c17930b26276
1 /* Copyright 2001,2002,2003 Roger Dingledine, Matej Pfajfar. */
2 /* See LICENSE for licensing information */
3 /* $Id$ */
5 /**
6 * \file main.c
7 * \brief Tor main loop and startup functions.
8 **/
10 #include "or.h"
12 /********* PROTOTYPES **********/
14 static void dumpstats(int severity); /* log stats */
15 static int init_from_config(int argc, char **argv);
17 /********* START VARIABLES **********/
19 /* declared in connection.c */
20 extern char *conn_state_to_string[][_CONN_TYPE_MAX+1];
22 or_options_t options; /**< Command-line and config-file options. */
23 int global_read_bucket; /**< Max number of bytes I can read this second. */
25 /** What was the read bucket before the last call to prepare_for_pool?
26 * (used to determine how many bytes we've read). */
27 static int stats_prev_global_read_bucket;
28 /** How many bytes have we read since we started the process? */
29 static uint64_t stats_n_bytes_read = 0;
30 /** How many seconds have we been running? */
31 static long stats_n_seconds_reading = 0;
33 /** Array of all open connections; each element corresponds to the element of
34 * poll_array in the same position. The first nfds elements are valid. */
35 static connection_t *connection_array[MAXCONNECTIONS] =
36 { NULL };
38 /** Array of pollfd objects for calls to poll(). */
39 static struct pollfd poll_array[MAXCONNECTIONS];
41 static int nfds=0; /**< Number of connections currently active. */
43 #ifndef MS_WINDOWS /* do signal stuff only on unix */
44 static int please_dumpstats=0; /**< Whether we should dump stats during the loop. */
45 static int please_reset=0; /**< Whether we just got a sighup. */
46 static int please_reap_children=0; /**< Whether we should waitpid for exited children. */
47 #endif /* signal stuff */
49 /** We set this to 1 when we've fetched a dir, to know whether to complain
50 * yet about unrecognized nicknames in entrynodes, exitnodes, etc.
51 * Also, we don't try building circuits unless this is 1. */
52 int has_fetched_directory=0;
54 /** We set this to 1 when we've opened a circuit, so we can print a log
55 * entry to inform the user that Tor is working. */
56 int has_completed_circuit=0;
58 /********* END VARIABLES ************/
60 /****************************************************************************
62 * This section contains accessors and other methods on the connection_array
63 * and poll_array variables (which are global within this file and unavailable
64 * outside it).
66 ****************************************************************************/
68 /** Add <b>conn</b> to the array of connections that we can poll on. The
69 * connection's socket must be set; the connection starts out
70 * non-reading and non-writing.
72 int connection_add(connection_t *conn) {
73 tor_assert(conn);
74 tor_assert(conn->s >= 0);
76 if(nfds >= options.MaxConn-1) {
77 log_fn(LOG_WARN,"failing because nfds is too high.");
78 return -1;
81 tor_assert(conn->poll_index == -1); /* can only connection_add once */
82 conn->poll_index = nfds;
83 connection_array[nfds] = conn;
85 poll_array[nfds].fd = conn->s;
87 /* zero these out here, because otherwise we'll inherit values from the previously freed one */
88 poll_array[nfds].events = 0;
89 poll_array[nfds].revents = 0;
91 nfds++;
93 log_fn(LOG_INFO,"new conn type %s, socket %d, nfds %d.",
94 CONN_TYPE_TO_STRING(conn->type), conn->s, nfds);
96 return 0;
99 /** Remove the connection from the global list, and remove the
100 * corresponding poll entry. Calling this function will shift the last
101 * connection (if any) into the position occupied by conn.
103 int connection_remove(connection_t *conn) {
104 int current_index;
106 tor_assert(conn);
107 tor_assert(nfds>0);
109 log_fn(LOG_INFO,"removing socket %d (type %s), nfds now %d",
110 conn->s, CONN_TYPE_TO_STRING(conn->type), nfds-1);
112 tor_assert(conn->poll_index >= 0);
113 current_index = conn->poll_index;
114 if(current_index == nfds-1) { /* this is the end */
115 nfds--;
116 return 0;
119 /* replace this one with the one at the end */
120 nfds--;
121 poll_array[current_index].fd = poll_array[nfds].fd;
122 poll_array[current_index].events = poll_array[nfds].events;
123 poll_array[current_index].revents = poll_array[nfds].revents;
124 connection_array[current_index] = connection_array[nfds];
125 connection_array[current_index]->poll_index = current_index;
127 return 0;
130 /** Set <b>*array</b> to an array of all connections, and <b>*n</b>
131 * to the length of the array. <b>*array</b> and <b>*n</b> must not
132 * be modified.
134 void get_connection_array(connection_t ***array, int *n) {
135 *array = connection_array;
136 *n = nfds;
139 /** Set the event mask on <b>conn</b> to <b>events</b>. (The form of
140 * the event mask is as for poll().)
142 void connection_watch_events(connection_t *conn, short events) {
144 tor_assert(conn && conn->poll_index >= 0 && conn->poll_index < nfds);
146 poll_array[conn->poll_index].events = events;
149 /** Return true iff <b>conn</b> is listening for read events. */
150 int connection_is_reading(connection_t *conn) {
151 tor_assert(conn && conn->poll_index >= 0);
152 return poll_array[conn->poll_index].events & POLLIN;
155 /** Tell the main loop to stop notifying <b>conn</b> of any read events. */
156 void connection_stop_reading(connection_t *conn) {
157 tor_assert(conn && conn->poll_index >= 0 && conn->poll_index < nfds);
159 log(LOG_DEBUG,"connection_stop_reading() called.");
160 if(poll_array[conn->poll_index].events & POLLIN)
161 poll_array[conn->poll_index].events -= POLLIN;
164 /** Tell the main loop to start notifying <b>conn</b> of any read events. */
165 void connection_start_reading(connection_t *conn) {
166 tor_assert(conn && conn->poll_index >= 0 && conn->poll_index < nfds);
167 poll_array[conn->poll_index].events |= POLLIN;
170 /** Return true iff <b>conn</b> is listening for write events. */
171 int connection_is_writing(connection_t *conn) {
172 return poll_array[conn->poll_index].events & POLLOUT;
175 /** Tell the main loop to stop notifying <b>conn</b> of any write events. */
176 void connection_stop_writing(connection_t *conn) {
177 tor_assert(conn && conn->poll_index >= 0 && conn->poll_index < nfds);
178 if(poll_array[conn->poll_index].events & POLLOUT)
179 poll_array[conn->poll_index].events -= POLLOUT;
182 /** Tell the main loop to start notifying <b>conn</b> of any write events. */
183 void connection_start_writing(connection_t *conn) {
184 tor_assert(conn && conn->poll_index >= 0 && conn->poll_index < nfds);
185 poll_array[conn->poll_index].events |= POLLOUT;
188 /** Called when the connection at connection_array[i] has a read event,
189 * or it has pending tls data waiting to be read: checks for validity,
190 * catches numerous errors, and dispatches to connection_handle_read.
192 static void conn_read(int i) {
193 connection_t *conn = connection_array[i];
195 if (conn->marked_for_close)
196 return;
198 /* see http://www.greenend.org.uk/rjk/2001/06/poll.html for
199 * discussion of POLLIN vs POLLHUP */
200 if(!(poll_array[i].revents & (POLLIN|POLLHUP|POLLERR)))
201 if(!connection_is_reading(conn) ||
202 !connection_has_pending_tls_data(conn))
203 return; /* this conn should not read */
205 log_fn(LOG_DEBUG,"socket %d wants to read.",conn->s);
207 assert_connection_ok(conn, time(NULL));
208 assert_all_pending_dns_resolves_ok();
211 /* XXX does POLLHUP also mean it's definitely broken? */
212 #ifdef MS_WINDOWS
213 (poll_array[i].revents & POLLERR) ||
214 #endif
215 connection_handle_read(conn) < 0) {
216 if (!conn->marked_for_close) {
217 /* this connection is broken. remove it */
218 /* XXX This shouldn't ever happen anymore. */
219 /* XXX but it'll clearly happen on MS_WINDOWS from POLLERR, right? */
220 log_fn(LOG_ERR,"Unhandled error on read for %s connection (fd %d); removing",
221 CONN_TYPE_TO_STRING(conn->type), conn->s);
222 connection_mark_for_close(conn,0);
225 assert_connection_ok(conn, time(NULL));
226 assert_all_pending_dns_resolves_ok();
229 /** Called when the connection at connection_array[i] has a write event:
230 * checks for validity, catches numerous errors, and dispatches to
231 * connection_handle_write.
233 static void conn_write(int i) {
234 connection_t *conn;
236 if(!(poll_array[i].revents & POLLOUT))
237 return; /* this conn doesn't want to write */
239 conn = connection_array[i];
240 log_fn(LOG_DEBUG,"socket %d wants to write.",conn->s);
241 if (conn->marked_for_close)
242 return;
244 assert_connection_ok(conn, time(NULL));
245 assert_all_pending_dns_resolves_ok();
247 if (connection_handle_write(conn) < 0) {
248 if (!conn->marked_for_close) {
249 /* this connection is broken. remove it. */
250 log_fn(LOG_WARN,"Unhandled error on read for %s connection (fd %d); removing",
251 CONN_TYPE_TO_STRING(conn->type), conn->s);
252 conn->has_sent_end = 1; /* otherwise we cry wolf about duplicate close */
253 connection_mark_for_close(conn,0);
256 assert_connection_ok(conn, time(NULL));
257 assert_all_pending_dns_resolves_ok();
260 /** If the connection at connection_array[i] is marked for close, then:
261 * - If it has data that it wants to flush, try to flush it.
262 * - If it _still_ has data to flush, and conn->hold_open_until_flushed is
263 * true, then leave the connection open and return.
264 * - Otherwise, remove the connection from connection_array and from
265 * all other lists, close it, and free it.
266 * If we remove the connection, then call conn_closed_if_marked at the new
267 * connection at position i.
269 static void conn_close_if_marked(int i) {
270 connection_t *conn;
271 int retval;
273 conn = connection_array[i];
274 assert_connection_ok(conn, time(NULL));
275 assert_all_pending_dns_resolves_ok();
276 if(!conn->marked_for_close)
277 return; /* nothing to see here, move along */
279 log_fn(LOG_INFO,"Cleaning up connection (fd %d).",conn->s);
280 if(conn->s >= 0 && connection_wants_to_flush(conn)) {
281 /* -1 means it's an incomplete edge connection, or that the socket
282 * has already been closed as unflushable. */
283 if(!conn->hold_open_until_flushed)
284 log_fn(LOG_WARN,
285 "Conn (fd %d, type %s, state %d) marked, but wants to flush %d bytes. "
286 "(Marked at %s:%d)",
287 conn->s, CONN_TYPE_TO_STRING(conn->type), conn->state,
288 conn->outbuf_flushlen, conn->marked_for_close_file, conn->marked_for_close);
289 if(connection_speaks_cells(conn)) {
290 if(conn->state == OR_CONN_STATE_OPEN) {
291 retval = flush_buf_tls(conn->tls, conn->outbuf, &conn->outbuf_flushlen);
292 /* XXX actually, some non-zero results are maybe ok. which ones? */
293 } else
294 retval = -1; /* never flush non-open broken tls connections */
295 } else {
296 retval = flush_buf(conn->s, conn->outbuf, &conn->outbuf_flushlen);
298 if(retval >= 0 &&
299 conn->hold_open_until_flushed && connection_wants_to_flush(conn)) {
300 log_fn(LOG_INFO,"Holding conn (fd %d) open for more flushing.",conn->s);
301 /* XXX should we reset timestamp_lastwritten here? */
302 return;
304 if(connection_wants_to_flush(conn)) {
305 log_fn(LOG_WARN,"Conn (fd %d, type %s, state %d) still wants to flush. Losing %d bytes! (Marked at %s:%d)",
306 conn->s, CONN_TYPE_TO_STRING(conn->type), conn->state,
307 (int)buf_datalen(conn->outbuf), conn->marked_for_close_file,
308 conn->marked_for_close);
311 /* if it's an edge conn, remove it from the list
312 * of conn's on this circuit. If it's not on an edge,
313 * flush and send destroys for all circuits on this conn
315 circuit_about_to_close_connection(conn);
316 connection_remove(conn);
317 if(conn->type == CONN_TYPE_EXIT) {
318 assert_connection_edge_not_dns_pending(conn);
320 connection_free(conn);
321 if(i<nfds) { /* we just replaced the one at i with a new one.
322 process it too. */
323 conn_close_if_marked(i);
327 /** This function is called whenever we successfully pull down a directory */
328 void directory_has_arrived(void) {
330 log_fn(LOG_INFO, "A directory has arrived.");
332 /* just for testing */
333 // directory_initiate_command(router_pick_directory_server(),
334 // DIR_PURPOSE_FETCH_RENDDESC, "foo", 3);
336 has_fetched_directory=1;
338 if(options.ORPort) { /* connect to them all */
339 router_retry_connections();
343 /** Perform regular maintenance tasks for a single connection. This
344 * function gets run once per second per connection by run_housekeeping.
346 static void run_connection_housekeeping(int i, time_t now) {
347 cell_t cell;
348 connection_t *conn = connection_array[i];
350 /* Expire any directory connections that haven't sent anything for 5 min */
351 if(conn->type == CONN_TYPE_DIR &&
352 !conn->marked_for_close &&
353 conn->timestamp_lastwritten + 5*60 < now) {
354 log_fn(LOG_WARN,"Expiring wedged directory conn (purpose %d)", conn->purpose);
355 /* XXXX This next check may help isolate where the pesky EPIPE bug
356 * really occurs. */
357 if (connection_wants_to_flush(conn)) {
358 flush_buf(conn->s, conn->outbuf, &conn->outbuf_flushlen);
360 connection_mark_for_close(conn,0);
361 /* XXXX Does this next part make sense, really? */
362 conn->hold_open_until_flushed = 1; /* give it a last chance */
363 return;
366 /* check connections to see whether we should send a keepalive, expire, or wait */
367 if(!connection_speaks_cells(conn))
368 return;
370 /* If we haven't written to an OR connection for a while, then either nuke
371 the connection or send a keepalive, depending. */
372 if(now >= conn->timestamp_lastwritten + options.KeepalivePeriod) {
373 if((!options.ORPort && !circuit_get_by_conn(conn)) ||
374 (!connection_state_is_open(conn))) {
375 /* we're an onion proxy, with no circuits; or our handshake has expired. kill it. */
376 log_fn(LOG_INFO,"Expiring connection to %d (%s:%d).",
377 i,conn->address, conn->port);
378 /* flush anything waiting, e.g. a destroy for a just-expired circ */
379 connection_mark_for_close(conn,CLOSE_REASON_UNUSED_OR_CONN);
380 conn->hold_open_until_flushed = 1;
381 } else {
382 /* either a full router, or we've got a circuit. send a padding cell. */
383 log_fn(LOG_DEBUG,"Sending keepalive to (%s:%d)",
384 conn->address, conn->port);
385 memset(&cell,0,sizeof(cell_t));
386 cell.command = CELL_PADDING;
387 connection_or_write_cell_to_buf(&cell, conn);
392 /** Perform regular maintenance tasks. This function gets run once per
393 * second by prepare_for_poll.
395 static void run_scheduled_events(time_t now) {
396 static long time_to_fetch_directory = 0;
397 static time_t last_uploaded_services = 0;
398 static time_t last_rotated_certificate = 0;
399 int i;
402 /** 1a. Every MIN_ONION_KEY_LIFETIME seconds, rotate the onion keys,
403 * shut down and restart all cpuworkers, and update the directory if
404 * necessary.
406 if (options.ORPort && get_onion_key_set_at()+MIN_ONION_KEY_LIFETIME < now) {
407 log_fn(LOG_INFO,"Rotating onion key.");
408 rotate_onion_key();
409 cpuworkers_rotate();
410 if (router_rebuild_descriptor()<0) {
411 log_fn(LOG_WARN, "Couldn't rebuild router descriptor");
413 router_upload_dir_desc_to_dirservers();
416 /** 1b. Every MAX_SSL_KEY_LIFETIME seconds, we change our TLS context. */
417 if (!last_rotated_certificate)
418 last_rotated_certificate = now;
419 if (options.ORPort && last_rotated_certificate+MAX_SSL_KEY_LIFETIME < now) {
420 log_fn(LOG_INFO,"Rotating tls context.");
421 if (tor_tls_context_new(get_identity_key(), 1, options.Nickname,
422 MAX_SSL_KEY_LIFETIME) < 0) {
423 log_fn(LOG_WARN, "Error reinitializing TLS context");
425 last_rotated_certificate = now;
426 /* XXXX We should rotate TLS connections as well; this code doesn't change
427 * XXXX them at all. */
430 /** 1c. Every DirFetchPostPeriod seconds, we get a new directory and upload
431 * our descriptor (if any). */
432 if(time_to_fetch_directory < now) {
433 /* it's time to fetch a new directory and/or post our descriptor */
434 if(options.ORPort) {
435 router_rebuild_descriptor();
436 router_upload_dir_desc_to_dirservers();
438 if(!options.DirPort) {
439 /* NOTE directory servers do not currently fetch directories.
440 * Hope this doesn't bite us later. */
441 directory_initiate_command(router_pick_directory_server(),
442 DIR_PURPOSE_FETCH_DIR, NULL, 0);
443 } else {
444 /* We're a directory; dump any old descriptors. */
445 dirserv_remove_old_servers();
447 /* Force an upload of our descriptors every DirFetchPostPeriod seconds. */
448 rend_services_upload(1);
449 last_uploaded_services = now;
450 rend_cache_clean(); /* should this go elsewhere? */
451 time_to_fetch_directory = now + options.DirFetchPostPeriod;
455 /** 2. Every second, we examine pending circuits and prune the
456 * ones which have been pending for more than a few seconds.
457 * We do this before step 3, so it can try building more if
458 * it's not comfortable with the number of available circuits.
460 circuit_expire_building(now);
462 /** 2b. Also look at pending streams and prune the ones that 'began'
463 * a long time ago but haven't gotten a 'connected' yet.
464 * Do this before step 3, so we can put them back into pending
465 * state to be picked up by the new circuit.
467 connection_ap_expire_beginning();
470 /** 2c. And expire connections that we've held open for too long.
472 connection_expire_held_open();
474 /** 3. Every second, we try a new circuit if there are no valid
475 * circuits. Every NewCircuitPeriod seconds, we expire circuits
476 * that became dirty more than NewCircuitPeriod seconds ago,
477 * and we make a new circ if there are no clean circuits.
479 if(has_fetched_directory)
480 circuit_build_needed_circs(now);
482 /** 4. We do housekeeping for each connection... */
483 for(i=0;i<nfds;i++) {
484 run_connection_housekeeping(i, now);
487 /** 5. And remove any marked circuits... */
488 circuit_close_all_marked();
490 /** 6. And upload service descriptors for any services whose intro points
491 * have changed in the last second. */
492 if (last_uploaded_services < now-5) {
493 rend_services_upload(0);
494 last_uploaded_services = now;
497 /** 7. and blow away any connections that need to die. have to do this now,
498 * because if we marked a conn for close and left its socket -1, then
499 * we'll pass it to poll/select and bad things will happen.
501 for(i=0;i<nfds;i++)
502 conn_close_if_marked(i);
505 /** Called every time we're about to call tor_poll. Increments statistics,
506 * and adjusts token buckets. Returns the number of milliseconds to use for
507 * the poll() timeout.
509 static int prepare_for_poll(void) {
510 static long current_second = 0; /* from previous calls to gettimeofday */
511 connection_t *conn;
512 struct timeval now;
513 int i;
515 tor_gettimeofday(&now);
517 /* Check how much bandwidth we've consumed, and increment the token
518 * buckets. */
519 stats_n_bytes_read += stats_prev_global_read_bucket-global_read_bucket;
520 connection_bucket_refill(&now);
521 stats_prev_global_read_bucket = global_read_bucket;
523 if(now.tv_sec > current_second) { /* the second has rolled over. check more stuff. */
525 ++stats_n_seconds_reading;
526 assert_all_pending_dns_resolves_ok();
527 run_scheduled_events(now.tv_sec);
528 assert_all_pending_dns_resolves_ok();
530 current_second = now.tv_sec; /* remember which second it is, for next time */
533 for(i=0;i<nfds;i++) {
534 conn = connection_array[i];
535 if(connection_has_pending_tls_data(conn) &&
536 connection_is_reading(conn)) {
537 log_fn(LOG_DEBUG,"sock %d has pending bytes.",conn->s);
538 return 0; /* has pending bytes to read; don't let poll wait. */
542 return (1000 - (now.tv_usec / 1000)); /* how many milliseconds til the next second? */
545 /** Configure the Tor process from the command line arguments and from the
546 * configuration file.
548 static int init_from_config(int argc, char **argv) {
549 /* read the configuration file. */
550 if(getconfig(argc,argv,&options)) {
551 log_fn(LOG_ERR,"Reading config failed. For usage, try -h.");
552 return -1;
554 close_logs(); /* we'll close, then open with correct loglevel if necessary */
556 /* Setuid/setgid as appropriate */
557 if(options.User || options.Group) {
558 if(switch_id(options.User, options.Group) != 0) {
559 return -1;
563 /* Start backgrounding the process, if requested. */
564 if (options.RunAsDaemon) {
565 start_daemon(options.DataDirectory);
568 /* Configure the log(s) */
569 if(!options.LogFile && !options.RunAsDaemon)
570 add_stream_log(options.loglevel, "<stdout>", stdout);
571 if(options.LogFile) {
572 if (add_file_log(options.loglevel, options.LogFile) != 0) {
573 /* opening the log file failed! Use stderr and log a warning */
574 add_stream_log(options.loglevel, "<stderr>", stderr);
575 log_fn(LOG_WARN, "Cannot write to LogFile '%s': %s.", options.LogFile, strerror(errno));
577 log_fn(LOG_NOTICE, "Successfully opened LogFile '%s', redirecting output.",
578 options.LogFile);
580 if(options.DebugLogFile) {
581 if (add_file_log(LOG_DEBUG, options.DebugLogFile) != 0)
582 log_fn(LOG_WARN, "Cannot write to DebugLogFile '%s': %s.", options.DebugLogFile, strerror(errno));
583 log_fn(LOG_DEBUG, "Successfully opened DebugLogFile '%s'.", options.DebugLogFile);
586 /* Set up our buckets */
587 connection_bucket_init();
588 stats_prev_global_read_bucket = global_read_bucket;
590 /* Finish backgrounding the process */
591 if(options.RunAsDaemon) {
592 /* XXXX Can we delay this any more? */
593 finish_daemon();
596 /* Write our pid to the pid file. if we do not have write permissions we
597 * will log a warning */
598 if(options.PidFile)
599 write_pidfile(options.PidFile);
601 return 0;
604 /** Called when we get a SIGHUP: reload configuration files and keys,
605 * retry all connections, re-upload all descriptors, and so on. */
606 static int do_hup(void) {
607 char keydir[512];
609 log_fn(LOG_NOTICE,"Received sighup. Reloading config.");
610 has_completed_circuit=0;
611 /* first, reload config variables, in case they've changed */
612 /* no need to provide argc/v, they've been cached inside init_from_config */
613 if (init_from_config(0, NULL) < 0) {
614 exit(1);
616 /* reload keys as needed for rendezvous services. */
617 if (rend_service_load_keys()<0) {
618 log_fn(LOG_ERR,"Error reloading rendezvous service keys");
619 exit(1);
621 if(retry_all_connections() < 0) {
622 log_fn(LOG_ERR,"Failed to bind one of the listener ports.");
623 return -1;
625 if(options.DirPort) {
626 /* reload the approved-routers file */
627 sprintf(keydir,"%s/approved-routers", options.DataDirectory);
628 log_fn(LOG_INFO,"Reloading approved fingerprints from %s...",keydir);
629 if(dirserv_parse_fingerprint_file(keydir) < 0) {
630 log_fn(LOG_WARN, "Error reloading fingerprints. Continuing with old list.");
632 /* Since we aren't fetching a directory, we won't retry rendezvous points
633 * when it gets in. Try again now. */
634 rend_services_introduce();
635 } else {
636 /* fetch a new directory */
637 directory_initiate_command(router_pick_directory_server(),
638 DIR_PURPOSE_FETCH_DIR, NULL, 0);
640 if(options.ORPort) {
641 router_rebuild_descriptor();
642 sprintf(keydir,"%s/router.desc", options.DataDirectory);
643 log_fn(LOG_INFO,"Dumping descriptor to %s...",keydir);
644 if (write_str_to_file(keydir, router_get_my_descriptor())) {
645 return -1;
648 return 0;
651 /** Tor main loop. */
652 static int do_main_loop(void) {
653 int i;
654 int timeout;
655 int poll_result;
657 /* Initialize the history structures. */
658 rep_hist_init();
659 /* Intialize the service cache. */
660 rend_cache_init();
662 /* load the private keys, if we're supposed to have them, and set up the
663 * TLS context. */
664 if (init_keys() < 0 || rend_service_load_keys() < 0) {
665 log_fn(LOG_ERR,"Error initializing keys; exiting");
666 return -1;
669 /* load the routers file */
670 if(options.RouterFile &&
671 router_set_routerlist_from_file(options.RouterFile) < 0) {
672 log_fn(LOG_ERR,"Error loading router list.");
673 return -1;
676 if(options.DirPort) { /* the directory is already here, run startup things */
677 has_fetched_directory = 1;
678 directory_has_arrived();
681 if(options.ORPort) {
682 cpu_init(); /* launch cpuworkers. Need to do this *after* we've read the onion key. */
685 /* start up the necessary connections based on which ports are
686 * non-zero. This is where we try to connect to all the other ORs,
687 * and start the listeners.
689 if(retry_all_connections() < 0) {
690 log_fn(LOG_ERR,"Failed to bind one of the listener ports.");
691 return -1;
694 for(;;) {
695 #ifndef MS_WINDOWS /* do signal stuff only on unix */
696 if(please_dumpstats) {
697 /* prefer to log it at INFO, but make sure we always see it */
698 dumpstats(options.loglevel>LOG_INFO ? options.loglevel : LOG_INFO);
699 please_dumpstats = 0;
701 if(please_reset) {
702 do_hup();
703 please_reset = 0;
705 if(please_reap_children) {
706 while(waitpid(-1,NULL,WNOHANG)) ; /* keep reaping until no more zombies */
707 please_reap_children = 0;
709 #endif /* signal stuff */
711 timeout = prepare_for_poll();
713 /* poll until we have an event, or the second ends */
714 poll_result = tor_poll(poll_array, nfds, timeout);
716 /* let catch() handle things like ^c, and otherwise don't worry about it */
717 if(poll_result < 0) {
718 /* let the program survive things like ^z */
719 if(tor_socket_errno(-1) != EINTR) {
720 log_fn(LOG_ERR,"poll failed: %s [%d]",
721 tor_socket_strerror(tor_socket_errno(-1)),
722 tor_socket_errno(-1));
723 return -1;
724 } else {
725 log_fn(LOG_DEBUG,"poll interrupted.");
729 /* do all the reads and errors first, so we can detect closed sockets */
730 for(i=0;i<nfds;i++)
731 conn_read(i); /* this also marks broken connections */
733 /* then do the writes */
734 for(i=0;i<nfds;i++)
735 conn_write(i);
737 /* any of the conns need to be closed now? */
738 for(i=0;i<nfds;i++)
739 conn_close_if_marked(i);
741 /* refilling buckets and sending cells happens at the beginning of the
742 * next iteration of the loop, inside prepare_for_poll()
747 /** Unix signal handler. */
748 static void catch(int the_signal) {
750 #ifndef MS_WINDOWS /* do signal stuff only on unix */
751 switch(the_signal) {
752 // case SIGABRT:
753 case SIGTERM:
754 case SIGINT:
755 log(LOG_ERR,"Catching signal %d, exiting cleanly.", the_signal);
756 /* we don't care if there was an error when we unlink, nothing
757 we could do about it anyways */
758 if(options.PidFile)
759 unlink(options.PidFile);
760 exit(0);
761 case SIGPIPE:
762 log(LOG_WARN,"Bug: caught sigpipe. Ignoring.");
763 break;
764 case SIGHUP:
765 please_reset = 1;
766 break;
767 case SIGUSR1:
768 please_dumpstats = 1;
769 break;
770 case SIGCHLD:
771 please_reap_children = 1;
772 break;
773 default:
774 log(LOG_WARN,"Caught signal %d that we can't handle??", the_signal);
776 #endif /* signal stuff */
779 /** Write all statistics to the log, with log level 'severity'. Called
780 * in response to a SIGUSR1. */
781 static void dumpstats(int severity) {
782 int i;
783 connection_t *conn;
784 time_t now = time(NULL);
786 log(severity, "Dumping stats:");
788 for(i=0;i<nfds;i++) {
789 conn = connection_array[i];
790 log(severity, "Conn %d (socket %d) type %d (%s), state %d (%s), created %d secs ago",
791 i, conn->s, conn->type, CONN_TYPE_TO_STRING(conn->type),
792 conn->state, conn_state_to_string[conn->type][conn->state], (int)(now - conn->timestamp_created));
793 if(!connection_is_listener(conn)) {
794 log(severity,"Conn %d is to '%s:%d'.",i,conn->address, conn->port);
795 log(severity,"Conn %d: %d bytes waiting on inbuf (last read %d secs ago)",i,
796 (int)buf_datalen(conn->inbuf),
797 (int)(now - conn->timestamp_lastread));
798 log(severity,"Conn %d: %d bytes waiting on outbuf (last written %d secs ago)",i,
799 (int)buf_datalen(conn->outbuf), (int)(now - conn->timestamp_lastwritten));
801 circuit_dump_by_conn(conn, severity); /* dump info about all the circuits using this conn */
803 log(severity,
804 "Cells processed: %10lu padding\n"
805 " %10lu create\n"
806 " %10lu created\n"
807 " %10lu relay\n"
808 " (%10lu relayed)\n"
809 " (%10lu delivered)\n"
810 " %10lu destroy",
811 stats_n_padding_cells_processed,
812 stats_n_create_cells_processed,
813 stats_n_created_cells_processed,
814 stats_n_relay_cells_processed,
815 stats_n_relay_cells_relayed,
816 stats_n_relay_cells_delivered,
817 stats_n_destroy_cells_processed);
818 if (stats_n_data_cells_packaged)
819 log(severity,"Average packaged cell fullness: %2.3f%%",
820 100*(((double)stats_n_data_bytes_packaged) /
821 (stats_n_data_cells_packaged*RELAY_PAYLOAD_SIZE)) );
822 if (stats_n_data_cells_received)
823 log(severity,"Average delivered cell fullness: %2.3f%%",
824 100*(((double)stats_n_data_bytes_received) /
825 (stats_n_data_cells_received*RELAY_PAYLOAD_SIZE)) );
827 if (stats_n_seconds_reading)
828 log(severity,"Average bandwidth used: %d bytes/sec",
829 (int) (stats_n_bytes_read/stats_n_seconds_reading));
831 rep_hist_dump_stats(now,severity);
832 rend_service_dump_stats(severity);
835 /** Called before we make any calls to network-related functions.
836 * (Some operating systems require their network libraries to be
837 * initialized.) */
838 int network_init(void)
840 #ifdef MS_WINDOWS
841 /* This silly exercise is necessary before windows will allow gethostbyname to work.
843 WSADATA WSAData;
844 int r;
845 r = WSAStartup(0x101,&WSAData);
846 if (r) {
847 log_fn(LOG_WARN,"Error initializing windows network layer: code was %d",r);
848 return -1;
850 /* XXXX We should call WSACleanup on exit, I think. */
851 #endif
852 return 0;
855 /** Called by exit() as we shut down the process.
857 void exit_function(void)
859 #ifdef MS_WINDOWS
860 WSACleanup();
861 #endif
864 /** Main entry point for the Tor command-line client.
866 int tor_main(int argc, char *argv[]) {
868 /* give it somewhere to log to initially */
869 add_stream_log(LOG_INFO, "<stdout>", stdout);
870 log_fn(LOG_NOTICE,"Tor v%s. This is experimental software. Do not use it if you need anonymity.",VERSION);
872 if (network_init()<0) {
873 log_fn(LOG_ERR,"Error initializing network; exiting.");
874 return 1;
876 atexit(exit_function);
878 if (init_from_config(argc,argv) < 0)
879 return -1;
881 #ifndef MS_WINDOWS
882 if(geteuid()==0)
883 log_fn(LOG_WARN,"You are running Tor as root. You don't need to, and you probably shouldn't.");
884 #endif
886 if(options.ORPort) { /* only spawn dns handlers if we're a router */
887 dns_init(); /* initialize the dns resolve tree, and spawn workers */
889 if(options.SocksPort) {
890 client_dns_init(); /* init the client dns cache */
893 #ifndef MS_WINDOWS /* do signal stuff only on unix */
895 struct sigaction action;
896 action.sa_flags = 0;
897 sigemptyset(&action.sa_mask);
899 action.sa_handler = catch;
900 sigaction(SIGINT, &action, NULL);
901 sigaction(SIGTERM, &action, NULL);
902 sigaction(SIGPIPE, &action, NULL);
903 sigaction(SIGUSR1, &action, NULL);
904 sigaction(SIGHUP, &action, NULL); /* to reload config, retry conns, etc */
905 sigaction(SIGCHLD, &action, NULL); /* handle dns/cpu workers that exit */
907 #endif /* signal stuff */
909 crypto_global_init();
910 crypto_seed_rng();
911 do_main_loop();
912 crypto_global_cleanup();
913 return -1;
917 Local Variables:
918 mode:c
919 indent-tabs-mode:nil
920 c-basic-offset:2
921 End: