main.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219
  1. /* Copyright 2001,2002,2003 Roger Dingledine, Matej Pfajfar. */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. /**
  5. * \file main.c
  6. * \brief Tor main loop and startup functions.
  7. **/
  8. #include "or.h"
  9. /********* PROTOTYPES **********/
  10. static void dumpstats(int severity); /* log stats */
  11. static int init_from_config(int argc, char **argv);
  12. /********* START VARIABLES **********/
  13. or_options_t options; /**< Command-line and config-file options. */
  14. int global_read_bucket; /**< Max number of bytes I can read this second. */
  15. int global_write_bucket; /**< Max number of bytes I can write this second. */
  16. /** What was the read bucket before the last call to prepare_for_pool?
  17. * (used to determine how many bytes we've read). */
  18. static int stats_prev_global_read_bucket;
  19. /** What was the write bucket before the last call to prepare_for_pool?
  20. * (used to determine how many bytes we've written). */
  21. static int stats_prev_global_write_bucket;
  22. /** How many bytes have we read/written since we started the process? */
  23. static uint64_t stats_n_bytes_read = 0;
  24. static uint64_t stats_n_bytes_written = 0;
  25. /** How many seconds have we been running? */
  26. long stats_n_seconds_uptime = 0;
  27. /** When do we next download a directory? */
  28. static time_t time_to_fetch_directory = 0;
  29. /** Array of all open connections; each element corresponds to the element of
  30. * poll_array in the same position. The first nfds elements are valid. */
  31. static connection_t *connection_array[MAXCONNECTIONS] =
  32. { NULL };
  33. /** Array of pollfd objects for calls to poll(). */
  34. static struct pollfd poll_array[MAXCONNECTIONS];
  35. static int nfds=0; /**< Number of connections currently active. */
  36. #ifndef MS_WINDOWS /* do signal stuff only on unix */
  37. static int please_dumpstats=0; /**< Whether we should dump stats during the loop. */
  38. static int please_reset=0; /**< Whether we just got a sighup. */
  39. static int please_reap_children=0; /**< Whether we should waitpid for exited children. */
  40. static int please_sigpipe=0; /**< Whether we've caught a sigpipe lately. */
  41. static int please_shutdown=0; /**< Whether we should shut down Tor. */
  42. #endif /* signal stuff */
  43. /** We should exit if shutting_down != 0 and now <= shutting_down.
  44. * If it's non-zero, don't accept any new circuits or connections.
  45. * This gets assigned when we receive a sig_int, and if we receive a
  46. * second one we exit immediately. */
  47. int shutting_down=0;
  48. #define SHUTDOWN_WAIT_LENGTH 30 /* seconds */
  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;
  53. /** We set this to 1 when we've opened a circuit, so we can print a log
  54. * entry to inform the user that Tor is working. */
  55. int has_completed_circuit=0;
  56. #ifdef MS_WINDOWS_SERVICE
  57. SERVICE_STATUS service_status;
  58. SERVICE_STATUS_HANDLE hStatus;
  59. #endif
  60. /********* END VARIABLES ************/
  61. /****************************************************************************
  62. *
  63. * This section contains accessors and other methods on the connection_array
  64. * and poll_array variables (which are global within this file and unavailable
  65. * outside it).
  66. *
  67. ****************************************************************************/
  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.
  71. */
  72. int connection_add(connection_t *conn) {
  73. tor_assert(conn);
  74. tor_assert(conn->s >= 0);
  75. if(nfds >= options.MaxConn-1) {
  76. log_fn(LOG_WARN,"failing because nfds is too high.");
  77. return -1;
  78. }
  79. tor_assert(conn->poll_index == -1); /* can only connection_add once */
  80. conn->poll_index = nfds;
  81. connection_array[nfds] = conn;
  82. poll_array[nfds].fd = conn->s;
  83. /* zero these out here, because otherwise we'll inherit values from the previously freed one */
  84. poll_array[nfds].events = 0;
  85. poll_array[nfds].revents = 0;
  86. nfds++;
  87. log_fn(LOG_INFO,"new conn type %s, socket %d, nfds %d.",
  88. CONN_TYPE_TO_STRING(conn->type), conn->s, nfds);
  89. return 0;
  90. }
  91. /** Remove the connection from the global list, and remove the
  92. * corresponding poll entry. Calling this function will shift the last
  93. * connection (if any) into the position occupied by conn.
  94. */
  95. int connection_remove(connection_t *conn) {
  96. int current_index;
  97. tor_assert(conn);
  98. tor_assert(nfds>0);
  99. log_fn(LOG_INFO,"removing socket %d (type %s), nfds now %d",
  100. conn->s, CONN_TYPE_TO_STRING(conn->type), nfds-1);
  101. tor_assert(conn->poll_index >= 0);
  102. current_index = conn->poll_index;
  103. if(current_index == nfds-1) { /* this is the end */
  104. nfds--;
  105. return 0;
  106. }
  107. /* replace this one with the one at the end */
  108. nfds--;
  109. poll_array[current_index].fd = poll_array[nfds].fd;
  110. poll_array[current_index].events = poll_array[nfds].events;
  111. poll_array[current_index].revents = poll_array[nfds].revents;
  112. connection_array[current_index] = connection_array[nfds];
  113. connection_array[current_index]->poll_index = current_index;
  114. return 0;
  115. }
  116. /** Return true iff conn is in the current poll array. */
  117. int connection_in_array(connection_t *conn) {
  118. int i;
  119. for (i=0; i<nfds; ++i) {
  120. if (conn==connection_array[i])
  121. return 1;
  122. }
  123. return 0;
  124. }
  125. /** Set <b>*array</b> to an array of all connections, and <b>*n</b>
  126. * to the length of the array. <b>*array</b> and <b>*n</b> must not
  127. * be modified.
  128. */
  129. void get_connection_array(connection_t ***array, int *n) {
  130. *array = connection_array;
  131. *n = nfds;
  132. }
  133. /** Set the event mask on <b>conn</b> to <b>events</b>. (The form of
  134. * the event mask is as for poll().)
  135. */
  136. void connection_watch_events(connection_t *conn, short events) {
  137. tor_assert(conn);
  138. tor_assert(conn->poll_index >= 0);
  139. tor_assert(conn->poll_index < nfds);
  140. poll_array[conn->poll_index].events = events;
  141. }
  142. /** Return true iff <b>conn</b> is listening for read events. */
  143. int connection_is_reading(connection_t *conn) {
  144. tor_assert(conn);
  145. tor_assert(conn->poll_index >= 0);
  146. return poll_array[conn->poll_index].events & POLLIN;
  147. }
  148. /** Tell the main loop to stop notifying <b>conn</b> of any read events. */
  149. void connection_stop_reading(connection_t *conn) {
  150. tor_assert(conn);
  151. tor_assert(conn->poll_index >= 0);
  152. tor_assert(conn->poll_index < nfds);
  153. log(LOG_DEBUG,"connection_stop_reading() called.");
  154. poll_array[conn->poll_index].events &= ~POLLIN;
  155. }
  156. /** Tell the main loop to start notifying <b>conn</b> of any read events. */
  157. void connection_start_reading(connection_t *conn) {
  158. tor_assert(conn);
  159. tor_assert(conn->poll_index >= 0);
  160. tor_assert(conn->poll_index < nfds);
  161. poll_array[conn->poll_index].events |= POLLIN;
  162. }
  163. /** Return true iff <b>conn</b> is listening for write events. */
  164. int connection_is_writing(connection_t *conn) {
  165. return poll_array[conn->poll_index].events & POLLOUT;
  166. }
  167. /** Tell the main loop to stop notifying <b>conn</b> of any write events. */
  168. void connection_stop_writing(connection_t *conn) {
  169. tor_assert(conn);
  170. tor_assert(conn->poll_index >= 0);
  171. tor_assert(conn->poll_index < nfds);
  172. poll_array[conn->poll_index].events &= ~POLLOUT;
  173. }
  174. /** Tell the main loop to start notifying <b>conn</b> of any write events. */
  175. void connection_start_writing(connection_t *conn) {
  176. tor_assert(conn);
  177. tor_assert(conn->poll_index >= 0);
  178. tor_assert(conn->poll_index < nfds);
  179. poll_array[conn->poll_index].events |= POLLOUT;
  180. }
  181. /** Called when the connection at connection_array[i] has a read event,
  182. * or it has pending tls data waiting to be read: checks for validity,
  183. * catches numerous errors, and dispatches to connection_handle_read.
  184. */
  185. static void conn_read(int i) {
  186. connection_t *conn = connection_array[i];
  187. if (conn->marked_for_close)
  188. return;
  189. /* see http://www.greenend.org.uk/rjk/2001/06/poll.html for
  190. * discussion of POLLIN vs POLLHUP */
  191. if(!(poll_array[i].revents & (POLLIN|POLLHUP|POLLERR)))
  192. if(!connection_is_reading(conn) ||
  193. !connection_has_pending_tls_data(conn))
  194. return; /* this conn should not read */
  195. log_fn(LOG_DEBUG,"socket %d wants to read.",conn->s);
  196. assert_connection_ok(conn, time(NULL));
  197. assert_all_pending_dns_resolves_ok();
  198. if(
  199. /* XXX does POLLHUP also mean it's definitely broken? */
  200. #ifdef MS_WINDOWS
  201. (poll_array[i].revents & POLLERR) ||
  202. #endif
  203. connection_handle_read(conn) < 0) {
  204. if (!conn->marked_for_close) {
  205. /* this connection is broken. remove it */
  206. log_fn(LOG_WARN,"Unhandled error on read for %s connection (fd %d); removing",
  207. CONN_TYPE_TO_STRING(conn->type), conn->s);
  208. connection_mark_for_close(conn);
  209. }
  210. }
  211. assert_connection_ok(conn, time(NULL));
  212. assert_all_pending_dns_resolves_ok();
  213. }
  214. /** Called when the connection at connection_array[i] has a write event:
  215. * checks for validity, catches numerous errors, and dispatches to
  216. * connection_handle_write.
  217. */
  218. static void conn_write(int i) {
  219. connection_t *conn;
  220. if(!(poll_array[i].revents & POLLOUT))
  221. return; /* this conn doesn't want to write */
  222. conn = connection_array[i];
  223. log_fn(LOG_DEBUG,"socket %d wants to write.",conn->s);
  224. if (conn->marked_for_close)
  225. return;
  226. assert_connection_ok(conn, time(NULL));
  227. assert_all_pending_dns_resolves_ok();
  228. if (connection_handle_write(conn) < 0) {
  229. if (!conn->marked_for_close) {
  230. /* this connection is broken. remove it. */
  231. log_fn(LOG_WARN,"Unhandled error on write for %s connection (fd %d); removing",
  232. CONN_TYPE_TO_STRING(conn->type), conn->s);
  233. conn->has_sent_end = 1; /* otherwise we cry wolf about duplicate close */
  234. /* XXX do we need a close-immediate here, so we don't try to flush? */
  235. connection_mark_for_close(conn);
  236. }
  237. }
  238. assert_connection_ok(conn, time(NULL));
  239. assert_all_pending_dns_resolves_ok();
  240. }
  241. /** If the connection at connection_array[i] is marked for close, then:
  242. * - If it has data that it wants to flush, try to flush it.
  243. * - If it _still_ has data to flush, and conn->hold_open_until_flushed is
  244. * true, then leave the connection open and return.
  245. * - Otherwise, remove the connection from connection_array and from
  246. * all other lists, close it, and free it.
  247. * If we remove the connection, then call conn_closed_if_marked at the new
  248. * connection at position i.
  249. */
  250. static void conn_close_if_marked(int i) {
  251. connection_t *conn;
  252. int retval;
  253. conn = connection_array[i];
  254. assert_connection_ok(conn, time(NULL));
  255. assert_all_pending_dns_resolves_ok();
  256. if(!conn->marked_for_close)
  257. return; /* nothing to see here, move along */
  258. log_fn(LOG_INFO,"Cleaning up connection (fd %d).",conn->s);
  259. if(conn->s >= 0 && connection_wants_to_flush(conn)) {
  260. /* -1 means it's an incomplete edge connection, or that the socket
  261. * has already been closed as unflushable. */
  262. if(!conn->hold_open_until_flushed)
  263. log_fn(LOG_INFO,
  264. "Conn (addr %s, fd %d, type %s, state %d) marked, but wants to flush %d bytes. "
  265. "(Marked at %s:%d)",
  266. conn->address, conn->s, CONN_TYPE_TO_STRING(conn->type), conn->state,
  267. (int)conn->outbuf_flushlen, conn->marked_for_close_file, conn->marked_for_close);
  268. if(connection_speaks_cells(conn)) {
  269. if(conn->state == OR_CONN_STATE_OPEN) {
  270. retval = flush_buf_tls(conn->tls, conn->outbuf, &conn->outbuf_flushlen);
  271. } else
  272. retval = -1; /* never flush non-open broken tls connections */
  273. } else {
  274. retval = flush_buf(conn->s, conn->outbuf, &conn->outbuf_flushlen);
  275. }
  276. if(retval >= 0 &&
  277. conn->hold_open_until_flushed && connection_wants_to_flush(conn)) {
  278. log_fn(LOG_INFO,"Holding conn (fd %d) open for more flushing.",conn->s);
  279. /* XXX should we reset timestamp_lastwritten here? */
  280. return;
  281. }
  282. if(connection_wants_to_flush(conn)) {
  283. log_fn(LOG_WARN,"Conn (addr %s, fd %d, type %s, state %d) still wants to flush. Losing %d bytes! (Marked at %s:%d)",
  284. conn->address, conn->s, CONN_TYPE_TO_STRING(conn->type), conn->state,
  285. (int)buf_datalen(conn->outbuf), conn->marked_for_close_file,
  286. conn->marked_for_close);
  287. }
  288. }
  289. /* if it's an edge conn, remove it from the list
  290. * of conn's on this circuit. If it's not on an edge,
  291. * flush and send destroys for all circuits on this conn
  292. */
  293. circuit_about_to_close_connection(conn);
  294. connection_about_to_close_connection(conn);
  295. connection_remove(conn);
  296. if(conn->type == CONN_TYPE_EXIT) {
  297. assert_connection_edge_not_dns_pending(conn);
  298. }
  299. connection_free(conn);
  300. if(i<nfds) { /* we just replaced the one at i with a new one.
  301. process it too. */
  302. conn_close_if_marked(i);
  303. }
  304. }
  305. /** This function is called whenever we successfully pull down a directory */
  306. void directory_has_arrived(time_t now) {
  307. log_fn(LOG_INFO, "A directory has arrived.");
  308. has_fetched_directory=1;
  309. /* Don't try to upload or download anything for DirFetchPostPeriod
  310. * seconds after the directory we had when we started.
  311. */
  312. if (!time_to_fetch_directory)
  313. time_to_fetch_directory = now + options.DirFetchPostPeriod;
  314. if(server_mode()) { /* connect to the appropriate routers */
  315. router_retry_connections();
  316. }
  317. }
  318. /** Perform regular maintenance tasks for a single connection. This
  319. * function gets run once per second per connection by run_housekeeping.
  320. */
  321. static void run_connection_housekeeping(int i, time_t now) {
  322. cell_t cell;
  323. connection_t *conn = connection_array[i];
  324. /* Expire any directory connections that haven't sent anything for 5 min */
  325. if(conn->type == CONN_TYPE_DIR &&
  326. !conn->marked_for_close &&
  327. conn->timestamp_lastwritten + 5*60 < now) {
  328. log_fn(LOG_INFO,"Expiring wedged directory conn (fd %d, purpose %d)", conn->s, conn->purpose);
  329. connection_mark_for_close(conn);
  330. return;
  331. }
  332. /* If we haven't written to an OR connection for a while, then either nuke
  333. the connection or send a keepalive, depending. */
  334. if(connection_speaks_cells(conn) &&
  335. now >= conn->timestamp_lastwritten + options.KeepalivePeriod) {
  336. routerinfo_t *router = router_get_by_digest(conn->identity_digest);
  337. if((!connection_state_is_open(conn)) ||
  338. (!clique_mode() && !circuit_get_by_conn(conn) &&
  339. (!router || !server_mode() || !router_is_clique_mode(router)))) {
  340. /* our handshake has expired;
  341. * or we have no circuits and we're both either OPs or normal ORs,
  342. * then kill it. */
  343. log_fn(LOG_INFO,"Expiring connection to %d (%s:%d).",
  344. i,conn->address, conn->port);
  345. /* flush anything waiting, e.g. a destroy for a just-expired circ */
  346. connection_mark_for_close(conn);
  347. conn->hold_open_until_flushed = 1;
  348. } else {
  349. /* either in clique mode, or we've got a circuit. send a padding cell. */
  350. log_fn(LOG_DEBUG,"Sending keepalive to (%s:%d)",
  351. conn->address, conn->port);
  352. memset(&cell,0,sizeof(cell_t));
  353. cell.command = CELL_PADDING;
  354. connection_or_write_cell_to_buf(&cell, conn);
  355. }
  356. }
  357. }
  358. #define MIN_BW_TO_PUBLISH_DESC 5000 /* 5000 bytes/s sustained */
  359. #define MIN_UPTIME_TO_PUBLISH_DESC (30*60) /* half an hour */
  360. /** Decide if we're a publishable server or just a client. We are a server if:
  361. * - We have the AuthoritativeDirectory option set.
  362. * or
  363. * - We don't have the ClientOnly option set; and
  364. * - We have ORPort set; and
  365. * - We have been up for at least MIN_UPTIME_TO_PUBLISH_DESC seconds; and
  366. * - We have processed some suitable minimum bandwidth recently; and
  367. * - We believe we are reachable from the outside.
  368. */
  369. static int decide_if_publishable_server(time_t now) {
  370. int bw;
  371. bw = rep_hist_bandwidth_assess();
  372. router_set_bandwidth_capacity(bw);
  373. if(options.ClientOnly)
  374. return 0;
  375. if(!options.ORPort)
  376. return 0;
  377. /* XXX008 for now, you're only a server if you're a server */
  378. return server_mode();
  379. /* here, determine if we're reachable */
  380. if(0) { /* we've recently failed to reach our IP/ORPort from the outside */
  381. return 0;
  382. }
  383. if(bw < MIN_BW_TO_PUBLISH_DESC)
  384. return 0;
  385. if(options.AuthoritativeDir)
  386. return 1;
  387. if(stats_n_seconds_uptime < MIN_UPTIME_TO_PUBLISH_DESC)
  388. return 0;
  389. return 1;
  390. }
  391. /** Return true iff we believe ourselves to be an authoritative
  392. * directory server.
  393. */
  394. int authdir_mode(void) {
  395. return (options.AuthoritativeDir != 0);
  396. }
  397. /** Return true iff we try to stay connected to all ORs at once.
  398. */
  399. int clique_mode(void) {
  400. return authdir_mode();
  401. }
  402. /** Return true iff we are trying to be a server.
  403. */
  404. int server_mode(void) {
  405. return (options.ORPort != 0 || options.ORBindAddress);
  406. }
  407. /** Remember if we've advertised ourselves to the dirservers. */
  408. static int server_is_advertised=0;
  409. /** Return true iff we have published our descriptor lately.
  410. */
  411. int advertised_server_mode(void) {
  412. return server_is_advertised;
  413. }
  414. /** Return true iff we are trying to be a socks proxy. */
  415. int proxy_mode(void) {
  416. return (options.SocksPort != 0 || options.SocksBindAddress);
  417. }
  418. /** Perform regular maintenance tasks. This function gets run once per
  419. * second by prepare_for_poll.
  420. */
  421. static void run_scheduled_events(time_t now) {
  422. static time_t last_uploaded_services = 0;
  423. static time_t last_rotated_certificate = 0;
  424. static time_t time_to_check_listeners = 0;
  425. int i;
  426. /** 0. See if we've been asked to shut down and our timeout has
  427. * expired. If so, exit now.
  428. */
  429. if(shutting_down && shutting_down <= now) {
  430. log(LOG_NOTICE,"Clean shutdown finished. Exiting.");
  431. tor_cleanup();
  432. exit(0);
  433. }
  434. /** 1a. Every MIN_ONION_KEY_LIFETIME seconds, rotate the onion keys,
  435. * shut down and restart all cpuworkers, and update the directory if
  436. * necessary.
  437. */
  438. if (server_mode() && get_onion_key_set_at()+MIN_ONION_KEY_LIFETIME < now) {
  439. log_fn(LOG_INFO,"Rotating onion key.");
  440. rotate_onion_key();
  441. cpuworkers_rotate();
  442. if (router_rebuild_descriptor()<0) {
  443. log_fn(LOG_WARN, "Couldn't rebuild router descriptor");
  444. }
  445. if(advertised_server_mode())
  446. router_upload_dir_desc_to_dirservers();
  447. }
  448. /** 1b. Every MAX_SSL_KEY_LIFETIME seconds, we change our TLS context. */
  449. if (!last_rotated_certificate)
  450. last_rotated_certificate = now;
  451. if (last_rotated_certificate+MAX_SSL_KEY_LIFETIME < now) {
  452. log_fn(LOG_INFO,"Rotating tls context.");
  453. if (tor_tls_context_new(get_identity_key(), 1, options.Nickname,
  454. MAX_SSL_KEY_LIFETIME) < 0) {
  455. log_fn(LOG_WARN, "Error reinitializing TLS context");
  456. }
  457. last_rotated_certificate = now;
  458. /* XXXX We should rotate TLS connections as well; this code doesn't change
  459. * XXXX them at all. */
  460. }
  461. /** 2. Every DirFetchPostPeriod seconds, we get a new directory and upload
  462. * our descriptor (if we've passed our internal checks). */
  463. if(time_to_fetch_directory < now) {
  464. if(decide_if_publishable_server(now)) {
  465. server_is_advertised = 1;
  466. router_rebuild_descriptor();
  467. router_upload_dir_desc_to_dirservers();
  468. } else {
  469. server_is_advertised = 0;
  470. }
  471. /* purge obsolete entries */
  472. routerlist_remove_old_routers(ROUTER_MAX_AGE);
  473. if(authdir_mode()) {
  474. /* We're a directory; dump any old descriptors. */
  475. dirserv_remove_old_servers(ROUTER_MAX_AGE);
  476. }
  477. if(server_mode()) {
  478. /* dirservers try to reconnect, in case connections have failed;
  479. * and normal servers try to reconnect to dirservers */
  480. router_retry_connections();
  481. }
  482. directory_get_from_dirserver(DIR_PURPOSE_FETCH_DIR, NULL, 0);
  483. /* Force an upload of our rend descriptors every DirFetchPostPeriod seconds. */
  484. rend_services_upload(1);
  485. last_uploaded_services = now;
  486. rend_cache_clean(); /* should this go elsewhere? */
  487. time_to_fetch_directory = now + options.DirFetchPostPeriod;
  488. }
  489. /** 3a. Every second, we examine pending circuits and prune the
  490. * ones which have been pending for more than a few seconds.
  491. * We do this before step 3, so it can try building more if
  492. * it's not comfortable with the number of available circuits.
  493. */
  494. circuit_expire_building(now);
  495. /** 3b. Also look at pending streams and prune the ones that 'began'
  496. * a long time ago but haven't gotten a 'connected' yet.
  497. * Do this before step 3, so we can put them back into pending
  498. * state to be picked up by the new circuit.
  499. */
  500. connection_ap_expire_beginning();
  501. /** 3c. And expire connections that we've held open for too long.
  502. */
  503. connection_expire_held_open();
  504. /** 3d. And every 60 secionds, we relaunch listeners if any died. */
  505. if (time_to_check_listeners < now) {
  506. retry_all_listeners(0); /* 0 means "only if some died." */
  507. time_to_check_listeners = now+60;
  508. }
  509. /** 4. Every second, we try a new circuit if there are no valid
  510. * circuits. Every NewCircuitPeriod seconds, we expire circuits
  511. * that became dirty more than NewCircuitPeriod seconds ago,
  512. * and we make a new circ if there are no clean circuits.
  513. */
  514. if(has_fetched_directory)
  515. circuit_build_needed_circs(now);
  516. /** 5. We do housekeeping for each connection... */
  517. for(i=0;i<nfds;i++) {
  518. run_connection_housekeeping(i, now);
  519. }
  520. /** 6. And remove any marked circuits... */
  521. circuit_close_all_marked();
  522. /** 7. And upload service descriptors for any services whose intro points
  523. * have changed in the last second. */
  524. if (last_uploaded_services < now-5) {
  525. rend_services_upload(0);
  526. last_uploaded_services = now;
  527. }
  528. /** 8. and blow away any connections that need to die. have to do this now,
  529. * because if we marked a conn for close and left its socket -1, then
  530. * we'll pass it to poll/select and bad things will happen.
  531. */
  532. for(i=0;i<nfds;i++)
  533. conn_close_if_marked(i);
  534. }
  535. /** Called every time we're about to call tor_poll. Increments statistics,
  536. * and adjusts token buckets. Returns the number of milliseconds to use for
  537. * the poll() timeout.
  538. */
  539. static int prepare_for_poll(void) {
  540. static long current_second = 0; /* from previous calls to gettimeofday */
  541. connection_t *conn;
  542. struct timeval now;
  543. int i;
  544. tor_gettimeofday(&now);
  545. /* Check how much bandwidth we've consumed, and increment the token
  546. * buckets. */
  547. stats_n_bytes_read += stats_prev_global_read_bucket - global_read_bucket;
  548. stats_n_bytes_written += stats_prev_global_write_bucket - global_write_bucket;
  549. connection_bucket_refill(&now);
  550. stats_prev_global_read_bucket = global_read_bucket;
  551. stats_prev_global_write_bucket = global_write_bucket;
  552. if(now.tv_sec > current_second) { /* the second has rolled over. check more stuff. */
  553. if(current_second)
  554. stats_n_seconds_uptime += (now.tv_sec - current_second);
  555. assert_all_pending_dns_resolves_ok();
  556. run_scheduled_events(now.tv_sec);
  557. assert_all_pending_dns_resolves_ok();
  558. current_second = now.tv_sec; /* remember which second it is, for next time */
  559. }
  560. for(i=0;i<nfds;i++) {
  561. conn = connection_array[i];
  562. if(connection_has_pending_tls_data(conn) &&
  563. connection_is_reading(conn)) {
  564. log_fn(LOG_DEBUG,"sock %d has pending bytes.",conn->s);
  565. return 0; /* has pending bytes to read; don't let poll wait. */
  566. }
  567. }
  568. return (1000 - (now.tv_usec / 1000)); /* how many milliseconds til the next second? */
  569. }
  570. /** Configure the Tor process from the command line arguments and from the
  571. * configuration file.
  572. */
  573. static int init_from_config(int argc, char **argv) {
  574. /* read the configuration file. */
  575. if(getconfig(argc,argv,&options)) {
  576. log_fn(LOG_ERR,"Reading config failed--see warnings above. For usage, try -h.");
  577. return -1;
  578. }
  579. /* Setuid/setgid as appropriate */
  580. if(options.User || options.Group) {
  581. if(switch_id(options.User, options.Group) != 0) {
  582. return -1;
  583. }
  584. }
  585. /* Ensure data directory is private; create if possible. */
  586. if (get_data_directory(&options) &&
  587. check_private_dir(get_data_directory(&options), 1) != 0) {
  588. log_fn(LOG_ERR, "Couldn't access/create private data directory %s",
  589. get_data_directory(&options));
  590. return -1;
  591. }
  592. /* Bail out at this point if we're not going to be a server: we want
  593. * to not fork, and to log stuff to stderr. */
  594. if (options.command != CMD_RUN_TOR)
  595. return 0;
  596. /* Configure the log(s) */
  597. if (config_init_logs(&options)<0)
  598. return -1;
  599. /* Close the temporary log we used while starting up, if it isn't already
  600. * gone. */
  601. close_temp_logs();
  602. /* Start backgrounding the process, if requested. */
  603. if (options.RunAsDaemon) {
  604. start_daemon(get_data_directory(&options));
  605. }
  606. /* Set up our buckets */
  607. connection_bucket_init();
  608. stats_prev_global_read_bucket = global_read_bucket;
  609. stats_prev_global_write_bucket = global_write_bucket;
  610. /* Finish backgrounding the process */
  611. if(options.RunAsDaemon) {
  612. /* XXXX Can we delay this any more? */
  613. finish_daemon();
  614. }
  615. /* Write our pid to the pid file. If we do not have write permissions we
  616. * will log a warning */
  617. if(options.PidFile)
  618. write_pidfile(options.PidFile);
  619. return 0;
  620. }
  621. /** Called when we get a SIGHUP: reload configuration files and keys,
  622. * retry all connections, re-upload all descriptors, and so on. */
  623. static int do_hup(void) {
  624. char keydir[512];
  625. log_fn(LOG_NOTICE,"Received sighup. Reloading config.");
  626. has_completed_circuit=0;
  627. mark_logs_temp(); /* Close current logs once new logs are open. */
  628. /* first, reload config variables, in case they've changed */
  629. /* no need to provide argc/v, they've been cached inside init_from_config */
  630. if (init_from_config(0, NULL) < 0) {
  631. return -1;
  632. }
  633. /* reload keys as needed for rendezvous services. */
  634. if (rend_service_load_keys()<0) {
  635. log_fn(LOG_ERR,"Error reloading rendezvous service keys");
  636. return -1;
  637. }
  638. if(retry_all_listeners(1) < 0) {
  639. log_fn(LOG_ERR,"Failed to bind one of the listener ports.");
  640. return -1;
  641. }
  642. if(authdir_mode()) {
  643. /* reload the approved-routers file */
  644. tor_snprintf(keydir,sizeof(keydir),"%s/approved-routers", get_data_directory(&options));
  645. log_fn(LOG_INFO,"Reloading approved fingerprints from %s...",keydir);
  646. if(dirserv_parse_fingerprint_file(keydir) < 0) {
  647. log_fn(LOG_WARN, "Error reloading fingerprints. Continuing with old list.");
  648. }
  649. }
  650. /* Fetch a new directory. Even authdirservers do this. */
  651. directory_get_from_dirserver(DIR_PURPOSE_FETCH_DIR, NULL, 0);
  652. if(server_mode()) {
  653. /* Restart cpuworker and dnsworker processes, so they get up-to-date
  654. * configuration options. */
  655. cpuworkers_rotate();
  656. dnsworkers_rotate();
  657. /* Rebuild fresh descriptor as needed. */
  658. router_rebuild_descriptor();
  659. tor_snprintf(keydir,sizeof(keydir),"%s/router.desc", get_data_directory(&options));
  660. log_fn(LOG_INFO,"Dumping descriptor to %s...",keydir);
  661. if (write_str_to_file(keydir, router_get_my_descriptor(), 0)) {
  662. return -1;
  663. }
  664. }
  665. return 0;
  666. }
  667. /** Tor main loop. */
  668. static int do_main_loop(void) {
  669. int i;
  670. int timeout;
  671. int poll_result;
  672. /* Initialize the history structures. */
  673. rep_hist_init();
  674. /* Intialize the service cache. */
  675. rend_cache_init();
  676. /* load the private keys, if we're supposed to have them, and set up the
  677. * TLS context. */
  678. if (init_keys() < 0 || rend_service_load_keys() < 0) {
  679. log_fn(LOG_ERR,"Error initializing keys; exiting");
  680. return -1;
  681. }
  682. /* load the routers file, or assign the defaults. */
  683. if(router_reload_router_list()) {
  684. return -1;
  685. }
  686. if(authdir_mode()) {
  687. /* the directory is already here, run startup things */
  688. router_retry_connections();
  689. }
  690. if(server_mode()) {
  691. /* launch cpuworkers. Need to do this *after* we've read the onion key. */
  692. cpu_init();
  693. }
  694. /* start up the necessary listeners based on which ports are non-zero. */
  695. if(retry_all_listeners(1) < 0) {
  696. log_fn(LOG_ERR,"Failed to bind one of the listener ports.");
  697. return -1;
  698. }
  699. for(;;) {
  700. #ifdef MS_WINDOWS_SERVICE /* Do service stuff only on windows. */
  701. if (service_status.dwCurrentState != SERVICE_RUNNING) {
  702. return 0;
  703. }
  704. #endif
  705. #ifndef MS_WINDOWS /* do signal stuff only on unix */
  706. if(please_shutdown) {
  707. if(!server_mode()) { /* do it now */
  708. log(LOG_NOTICE,"Interrupt: exiting cleanly.");
  709. tor_cleanup();
  710. exit(0);
  711. }
  712. if(shutting_down) { /* we've already been asked. do it now. */
  713. log(LOG_NOTICE,"Second sigint received; exiting now.");
  714. tor_cleanup();
  715. exit(0);
  716. } else {
  717. log(LOG_NOTICE,"Interrupt: will shut down in %d seconds. Interrupt again to exit now.", SHUTDOWN_WAIT_LENGTH);
  718. shutting_down = time(NULL) + SHUTDOWN_WAIT_LENGTH;
  719. }
  720. please_shutdown = 0;
  721. }
  722. if(please_sigpipe) {
  723. log(LOG_NOTICE,"Caught sigpipe. Ignoring.");
  724. please_sigpipe = 0;
  725. }
  726. if(please_dumpstats) {
  727. /* prefer to log it at INFO, but make sure we always see it */
  728. dumpstats(get_min_log_level()>LOG_INFO ? get_min_log_level() : LOG_INFO);
  729. please_dumpstats = 0;
  730. }
  731. if(please_reset) {
  732. if (do_hup() < 0) {
  733. log_fn(LOG_WARN,"Restart failed (config error?). Exiting.");
  734. tor_cleanup();
  735. exit(1);
  736. }
  737. please_reset = 0;
  738. }
  739. if(please_reap_children) {
  740. while(waitpid(-1,NULL,WNOHANG) > 0) ; /* keep reaping until no more zombies */
  741. please_reap_children = 0;
  742. }
  743. #endif /* signal stuff */
  744. timeout = prepare_for_poll();
  745. /* poll until we have an event, or the second ends */
  746. poll_result = tor_poll(poll_array, nfds, timeout);
  747. /* let catch() handle things like ^c, and otherwise don't worry about it */
  748. if(poll_result < 0) {
  749. /* let the program survive things like ^z */
  750. if(tor_socket_errno(-1) != EINTR) {
  751. log_fn(LOG_ERR,"poll failed: %s [%d]",
  752. tor_socket_strerror(tor_socket_errno(-1)),
  753. tor_socket_errno(-1));
  754. return -1;
  755. } else {
  756. log_fn(LOG_DEBUG,"poll interrupted.");
  757. }
  758. }
  759. /* do all the reads and errors first, so we can detect closed sockets */
  760. for(i=0;i<nfds;i++)
  761. conn_read(i); /* this also marks broken connections */
  762. /* then do the writes */
  763. for(i=0;i<nfds;i++)
  764. conn_write(i);
  765. /* any of the conns need to be closed now? */
  766. for(i=0;i<nfds;i++)
  767. conn_close_if_marked(i);
  768. /* refilling buckets and sending cells happens at the beginning of the
  769. * next iteration of the loop, inside prepare_for_poll()
  770. */
  771. }
  772. }
  773. /** Unix signal handler. */
  774. static void catch(int the_signal) {
  775. #ifndef MS_WINDOWS /* do signal stuff only on unix */
  776. switch(the_signal) {
  777. // case SIGABRT:
  778. case SIGTERM:
  779. log(LOG_ERR,"Catching signal %d, exiting cleanly.", the_signal);
  780. tor_cleanup();
  781. exit(0);
  782. case SIGINT:
  783. please_shutdown = 1;
  784. break;
  785. case SIGPIPE:
  786. /* don't log here, since it's possible you got the sigpipe because
  787. * your log failed! */
  788. please_sigpipe = 1;
  789. break;
  790. case SIGHUP:
  791. please_reset = 1;
  792. break;
  793. case SIGUSR1:
  794. please_dumpstats = 1;
  795. break;
  796. case SIGCHLD:
  797. please_reap_children = 1;
  798. break;
  799. default:
  800. log(LOG_WARN,"Caught signal %d that we can't handle??", the_signal);
  801. tor_cleanup();
  802. exit(1);
  803. }
  804. #endif /* signal stuff */
  805. }
  806. /** Write all statistics to the log, with log level 'severity'. Called
  807. * in response to a SIGUSR1. */
  808. static void dumpstats(int severity) {
  809. int i;
  810. connection_t *conn;
  811. time_t now = time(NULL);
  812. log(severity, "Dumping stats:");
  813. for(i=0;i<nfds;i++) {
  814. conn = connection_array[i];
  815. log(severity, "Conn %d (socket %d) type %d (%s), state %d (%s), created %d secs ago",
  816. i, conn->s, conn->type, CONN_TYPE_TO_STRING(conn->type),
  817. conn->state, conn_state_to_string[conn->type][conn->state], (int)(now - conn->timestamp_created));
  818. if(!connection_is_listener(conn)) {
  819. log(severity,"Conn %d is to '%s:%d'.",i,conn->address, conn->port);
  820. log(severity,"Conn %d: %d bytes waiting on inbuf (last read %d secs ago)",i,
  821. (int)buf_datalen(conn->inbuf),
  822. (int)(now - conn->timestamp_lastread));
  823. log(severity,"Conn %d: %d bytes waiting on outbuf (last written %d secs ago)",i,
  824. (int)buf_datalen(conn->outbuf), (int)(now - conn->timestamp_lastwritten));
  825. }
  826. circuit_dump_by_conn(conn, severity); /* dump info about all the circuits using this conn */
  827. }
  828. log(severity,
  829. "Cells processed: %10lu padding\n"
  830. " %10lu create\n"
  831. " %10lu created\n"
  832. " %10lu relay\n"
  833. " (%10lu relayed)\n"
  834. " (%10lu delivered)\n"
  835. " %10lu destroy",
  836. stats_n_padding_cells_processed,
  837. stats_n_create_cells_processed,
  838. stats_n_created_cells_processed,
  839. stats_n_relay_cells_processed,
  840. stats_n_relay_cells_relayed,
  841. stats_n_relay_cells_delivered,
  842. stats_n_destroy_cells_processed);
  843. if (stats_n_data_cells_packaged)
  844. log(severity,"Average packaged cell fullness: %2.3f%%",
  845. 100*(((double)stats_n_data_bytes_packaged) /
  846. (stats_n_data_cells_packaged*RELAY_PAYLOAD_SIZE)) );
  847. if (stats_n_data_cells_received)
  848. log(severity,"Average delivered cell fullness: %2.3f%%",
  849. 100*(((double)stats_n_data_bytes_received) /
  850. (stats_n_data_cells_received*RELAY_PAYLOAD_SIZE)) );
  851. if (stats_n_seconds_uptime)
  852. log(severity,
  853. #ifdef MS_WINDOWS
  854. "Average bandwidth used: %I64u/%ld = %d bytes/sec",
  855. stats_n_bytes_read,
  856. #else
  857. "Average bandwidth used: %llu/%ld = %d bytes/sec",
  858. (long long unsigned int)stats_n_bytes_read,
  859. #endif
  860. stats_n_seconds_uptime,
  861. (int) (stats_n_bytes_read/stats_n_seconds_uptime));
  862. rep_hist_dump_stats(now,severity);
  863. rend_service_dump_stats(severity);
  864. }
  865. /** Called before we make any calls to network-related functions.
  866. * (Some operating systems require their network libraries to be
  867. * initialized.) */
  868. static int network_init(void)
  869. {
  870. #ifdef MS_WINDOWS
  871. /* This silly exercise is necessary before windows will allow gethostbyname to work.
  872. */
  873. WSADATA WSAData;
  874. int r;
  875. r = WSAStartup(0x101,&WSAData);
  876. if (r) {
  877. log_fn(LOG_WARN,"Error initializing windows network layer: code was %d",r);
  878. return -1;
  879. }
  880. /* XXXX We should call WSACleanup on exit, I think. */
  881. #endif
  882. return 0;
  883. }
  884. /** Called by exit() as we shut down the process.
  885. */
  886. void exit_function(void)
  887. {
  888. /* XXX if we ever daemonize, this gets called immediately */
  889. #ifdef MS_WINDOWS
  890. WSACleanup();
  891. #endif
  892. }
  893. /** Set up the signal handlers for either parent or child. */
  894. void handle_signals(int is_parent)
  895. {
  896. #ifndef MS_WINDOWS /* do signal stuff only on unix */
  897. struct sigaction action;
  898. action.sa_flags = 0;
  899. sigemptyset(&action.sa_mask);
  900. action.sa_handler = is_parent ? catch : SIG_IGN;
  901. sigaction(SIGINT, &action, NULL); /* do a controlled slow shutdown */
  902. sigaction(SIGTERM, &action, NULL); /* to terminate now */
  903. sigaction(SIGPIPE, &action, NULL); /* otherwise sigpipe kills us */
  904. sigaction(SIGUSR1, &action, NULL); /* dump stats */
  905. sigaction(SIGHUP, &action, NULL); /* to reload config, retry conns, etc */
  906. if(is_parent)
  907. sigaction(SIGCHLD, &action, NULL); /* handle dns/cpu workers that exit */
  908. #endif /* signal stuff */
  909. }
  910. /** Main entry point for the Tor command-line client.
  911. */
  912. static int tor_init(int argc, char *argv[]) {
  913. /* give it somewhere to log to initially */
  914. add_temp_log();
  915. log_fn(LOG_NOTICE,"Tor v%s. This is experimental software. Do not use it if you need anonymity.",VERSION);
  916. if (network_init()<0) {
  917. log_fn(LOG_ERR,"Error initializing network; exiting.");
  918. return -1;
  919. }
  920. atexit(exit_function);
  921. if (init_from_config(argc,argv) < 0)
  922. return -1;
  923. #ifndef MS_WINDOWS
  924. if(geteuid()==0)
  925. log_fn(LOG_WARN,"You are running Tor as root. You don't need to, and you probably shouldn't.");
  926. #endif
  927. if(server_mode()) { /* only spawn dns handlers if we're a router */
  928. dns_init(); /* initialize the dns resolve tree, and spawn workers */
  929. }
  930. if(proxy_mode()) {
  931. client_dns_init(); /* init the client dns cache */
  932. }
  933. handle_signals(1);
  934. crypto_global_init();
  935. crypto_seed_rng();
  936. return 0;
  937. }
  938. /** Do whatever cleanup is necessary before shutting Tor down. */
  939. void tor_cleanup(void) {
  940. /* Remove our pid file. We don't care if there was an error when we
  941. * unlink, nothing we could do about it anyways. */
  942. if(options.PidFile && options.command == CMD_RUN_TOR)
  943. unlink(options.PidFile);
  944. crypto_global_cleanup();
  945. }
  946. /** Read/create keys as needed, and echo our fingerprint to stdout. */
  947. void do_list_fingerprint(void)
  948. {
  949. char buf[FINGERPRINT_LEN+1];
  950. crypto_pk_env_t *k;
  951. if (init_keys() < 0) {
  952. log_fn(LOG_ERR,"Error initializing keys; exiting");
  953. return;
  954. }
  955. if (!(k = get_identity_key())) {
  956. log_fn(LOG_ERR,"Error: missing identity key.");
  957. return;
  958. }
  959. if (crypto_pk_get_fingerprint(k, buf, 1)<0) {
  960. log_fn(LOG_ERR, "Error computing fingerprint");
  961. return;
  962. }
  963. printf("%s %s\n", options.Nickname, buf);
  964. }
  965. #ifdef MS_WINDOWS_SERVICE
  966. void nt_service_control(DWORD request)
  967. {
  968. switch (request) {
  969. case SERVICE_CONTROL_STOP:
  970. case SERVICE_CONTROL_SHUTDOWN:
  971. log(LOG_ERR, "Got stop/shutdown request; shutting down cleanly.");
  972. service_status.dwWin32ExitCode = 0;
  973. service_status.dwCurrentState = SERVICE_STOPPED;
  974. return;
  975. }
  976. SetServiceStatus(hStatus, &service_status);
  977. }
  978. void nt_service_body(int argc, char **argv)
  979. {
  980. int err;
  981. FILE *f;
  982. f = fopen("d:\\foo.txt", "w");
  983. fprintf(f, "POINT 1\n");
  984. fclose(f);
  985. service_status.dwServiceType = SERVICE_WIN32;
  986. service_status.dwCurrentState = SERVICE_START_PENDING;
  987. service_status.dwControlsAccepted =
  988. SERVICE_ACCEPT_STOP |
  989. SERVICE_ACCEPT_SHUTDOWN;
  990. service_status.dwWin32ExitCode = 0;
  991. service_status.dwServiceSpecificExitCode = 0;
  992. service_status.dwCheckPoint = 0;
  993. service_status.dwWaitHint = 0;
  994. hStatus = RegisterServiceCtrlHandler("Tor", (LPHANDLER_FUNCTION) nt_service_control);
  995. if (hStatus == 0) {
  996. // failed;
  997. return;
  998. }
  999. err = tor_init(argc, argv); // refactor this part out of tor_main and do_main_loop
  1000. if (err) {
  1001. // failed.
  1002. service_status.dwCurrentState = SERVICE_STOPPED;
  1003. service_status.dwWin32ExitCode = -1;
  1004. SetServiceStatus(hStatus, &service_status);
  1005. return;
  1006. }
  1007. service_status.dwCurrentState = SERVICE_RUNNING;
  1008. SetServiceStatus(hStatus, &service_status);
  1009. do_main_loop();
  1010. tor_cleanup();
  1011. return;
  1012. }
  1013. void nt_service_main(void)
  1014. {
  1015. SERVICE_TABLE_ENTRY table[2];
  1016. table[0].lpServiceName = "Tor";
  1017. table[0].lpServiceProc = (LPSERVICE_MAIN_FUNCTION)nt_service_body;
  1018. table[1].lpServiceName = NULL;
  1019. table[1].lpServiceProc = NULL;
  1020. if (!StartServiceCtrlDispatcher(table))
  1021. printf("Error was %d\n",GetLastError());
  1022. }
  1023. #endif
  1024. int tor_main(int argc, char *argv[]) {
  1025. #ifdef MS_WINDOWS_SERVICE
  1026. nt_service_main();
  1027. return 0;
  1028. #else
  1029. if (tor_init(argc, argv)<0)
  1030. return -1;
  1031. switch (options.command) {
  1032. case CMD_RUN_TOR:
  1033. do_main_loop();
  1034. break;
  1035. case CMD_LIST_FINGERPRINT:
  1036. do_list_fingerprint();
  1037. break;
  1038. default:
  1039. log_fn(LOG_ERR, "Illegal command number %d: internal error.",
  1040. options.command);
  1041. }
  1042. tor_cleanup();
  1043. return -1;
  1044. #endif
  1045. }
  1046. /*
  1047. Local Variables:
  1048. mode:c
  1049. indent-tabs-mode:nil
  1050. c-basic-offset:2
  1051. End:
  1052. */