mainloop.c 87 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697
  1. /* Copyright (c) 2001 Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  4. * Copyright (c) 2007-2019, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file mainloop.c
  8. * \brief Toplevel module. Handles signals, multiplexes between
  9. * connections, implements main loop, and drives scheduled events.
  10. *
  11. * For the main loop itself; see run_main_loop_once(). It invokes the rest of
  12. * Tor mostly through Libevent callbacks. Libevent callbacks can happen when
  13. * a timer elapses, a signal is received, a socket is ready to read or write,
  14. * or an event is manually activated.
  15. *
  16. * Most events in Tor are driven from these callbacks:
  17. * <ul>
  18. * <li>conn_read_callback() and conn_write_callback() here, which are
  19. * invoked when a socket is ready to read or write respectively.
  20. * <li>signal_callback(), which handles incoming signals.
  21. * </ul>
  22. * Other events are used for specific purposes, or for building more complex
  23. * control structures. If you search for usage of tor_libevent_new(), you
  24. * will find all the events that we construct in Tor.
  25. *
  26. * Tor has numerous housekeeping operations that need to happen
  27. * regularly. They are handled in different ways:
  28. * <ul>
  29. * <li>The most frequent operations are handled after every read or write
  30. * event, at the end of connection_handle_read() and
  31. * connection_handle_write().
  32. *
  33. * <li>The next most frequent operations happen after each invocation of the
  34. * main loop, in run_main_loop_once().
  35. *
  36. * <li>Once per second, we run all of the operations listed in
  37. * second_elapsed_callback(), and in its child, run_scheduled_events().
  38. *
  39. * <li>Once-a-second operations are handled in second_elapsed_callback().
  40. *
  41. * <li>More infrequent operations take place based on the periodic event
  42. * driver in periodic.c . These are stored in the periodic_events[]
  43. * table.
  44. * </ul>
  45. *
  46. **/
  47. #define MAINLOOP_PRIVATE
  48. #include "core/or/or.h"
  49. #include "app/config/config.h"
  50. #include "app/config/statefile.h"
  51. #include "app/main/ntmain.h"
  52. #include "app/main/tor_threads.h"
  53. #include "core/mainloop/connection.h"
  54. #include "core/mainloop/cpuworker.h"
  55. #include "core/mainloop/mainloop.h"
  56. #include "core/mainloop/netstatus.h"
  57. #include "core/mainloop/periodic.h"
  58. #include "core/mainloop/throughput_logging.h"
  59. #include "core/or/channel.h"
  60. #include "core/or/channelpadding.h"
  61. #include "core/or/channeltls.h"
  62. #include "core/or/circuitbuild.h"
  63. #include "core/or/circuitlist.h"
  64. #include "core/or/circuituse.h"
  65. #include "core/or/connection_edge.h"
  66. #include "core/or/connection_or.h"
  67. #include "core/or/dos.h"
  68. #include "core/or/status.h"
  69. #include "feature/client/addressmap.h"
  70. #include "feature/client/bridges.h"
  71. #include "feature/client/dnsserv.h"
  72. #include "feature/client/entrynodes.h"
  73. #include "feature/client/transports.h"
  74. #include "feature/control/control.h"
  75. #include "feature/control/control_events.h"
  76. #include "feature/dirauth/authmode.h"
  77. #include "feature/dircache/consdiffmgr.h"
  78. #include "feature/dircache/dirserv.h"
  79. #include "feature/dircommon/directory.h"
  80. #include "feature/hibernate/hibernate.h"
  81. #include "feature/hs/hs_cache.h"
  82. #include "feature/hs/hs_client.h"
  83. #include "feature/hs/hs_service.h"
  84. #include "feature/nodelist/microdesc.h"
  85. #include "feature/nodelist/networkstatus.h"
  86. #include "feature/nodelist/nodelist.h"
  87. #include "feature/nodelist/routerlist.h"
  88. #include "feature/relay/dns.h"
  89. #include "feature/relay/routerkeys.h"
  90. #include "feature/relay/routermode.h"
  91. #include "feature/relay/selftest.h"
  92. #include "feature/rend/rendcache.h"
  93. #include "feature/rend/rendservice.h"
  94. #include "feature/stats/geoip_stats.h"
  95. #include "feature/stats/predict_ports.h"
  96. #include "feature/stats/rephist.h"
  97. #include "lib/buf/buffers.h"
  98. #include "lib/crypt_ops/crypto_rand.h"
  99. #include "lib/err/backtrace.h"
  100. #include "lib/tls/buffers_tls.h"
  101. #include "lib/net/buffers_net.h"
  102. #include "lib/evloop/compat_libevent.h"
  103. #include <event2/event.h>
  104. #include "core/or/cell_st.h"
  105. #include "core/or/entry_connection_st.h"
  106. #include "feature/nodelist/networkstatus_st.h"
  107. #include "core/or/or_connection_st.h"
  108. #include "app/config/or_state_st.h"
  109. #include "feature/nodelist/routerinfo_st.h"
  110. #include "core/or/socks_request_st.h"
  111. #ifdef HAVE_UNISTD_H
  112. #include <unistd.h>
  113. #endif
  114. #ifdef HAVE_SYSTEMD
  115. # if defined(__COVERITY__) && !defined(__INCLUDE_LEVEL__)
  116. /* Systemd's use of gcc's __INCLUDE_LEVEL__ extension macro appears to confuse
  117. * Coverity. Here's a kludge to unconfuse it.
  118. */
  119. # define __INCLUDE_LEVEL__ 2
  120. #endif /* defined(__COVERITY__) && !defined(__INCLUDE_LEVEL__) */
  121. #include <systemd/sd-daemon.h>
  122. #endif /* defined(HAVE_SYSTEMD) */
  123. /* Token bucket for all traffic. */
  124. token_bucket_rw_t global_bucket;
  125. /* Token bucket for relayed traffic. */
  126. token_bucket_rw_t global_relayed_bucket;
  127. /* XXX we might want to keep stats about global_relayed_*_bucket too. Or not.*/
  128. /** How many bytes have we read since we started the process? */
  129. static uint64_t stats_n_bytes_read = 0;
  130. /** How many bytes have we written since we started the process? */
  131. static uint64_t stats_n_bytes_written = 0;
  132. /** What time did this process start up? */
  133. time_t time_of_process_start = 0;
  134. /** How many seconds have we been running? */
  135. static long stats_n_seconds_working = 0;
  136. /** How many times have we returned from the main loop successfully? */
  137. static uint64_t stats_n_main_loop_successes = 0;
  138. /** How many times have we received an error from the main loop? */
  139. static uint64_t stats_n_main_loop_errors = 0;
  140. /** How many times have we returned from the main loop with no events. */
  141. static uint64_t stats_n_main_loop_idle = 0;
  142. /** How often will we honor SIGNEWNYM requests? */
  143. #define MAX_SIGNEWNYM_RATE 10
  144. /** When did we last process a SIGNEWNYM request? */
  145. static time_t time_of_last_signewnym = 0;
  146. /** Is there a signewnym request we're currently waiting to handle? */
  147. static int signewnym_is_pending = 0;
  148. /** Mainloop event for the deferred signewnym call. */
  149. static mainloop_event_t *handle_deferred_signewnym_ev = NULL;
  150. /** How many times have we called newnym? */
  151. static unsigned newnym_epoch = 0;
  152. /** Smartlist of all open connections. */
  153. STATIC smartlist_t *connection_array = NULL;
  154. /** List of connections that have been marked for close and need to be freed
  155. * and removed from connection_array. */
  156. static smartlist_t *closeable_connection_lst = NULL;
  157. /** List of linked connections that are currently reading data into their
  158. * inbuf from their partner's outbuf. */
  159. static smartlist_t *active_linked_connection_lst = NULL;
  160. /** Flag: Set to true iff we entered the current libevent main loop via
  161. * <b>loop_once</b>. If so, there's no need to trigger a loopexit in order
  162. * to handle linked connections. */
  163. static int called_loop_once = 0;
  164. /** Flag: if true, it's time to shut down, so the main loop should exit as
  165. * soon as possible.
  166. */
  167. static int main_loop_should_exit = 0;
  168. /** The return value that the main loop should yield when it exits, if
  169. * main_loop_should_exit is true.
  170. */
  171. static int main_loop_exit_value = 0;
  172. /** Flag: if true, it's time to shut down, so the other eventloops should
  173. * exit as soon as possible.
  174. */
  175. static int other_eventloops_should_exit = 0;
  176. /** We set this to 1 when we've opened a circuit, so we can print a log
  177. * entry to inform the user that Tor is working. We set it to 0 when
  178. * we think the fact that we once opened a circuit doesn't mean we can do so
  179. * any longer (a big time jump happened, when we notice our directory is
  180. * heinously out-of-date, etc.
  181. */
  182. static int can_complete_circuits = 0;
  183. /** How often do we check for router descriptors that we should download
  184. * when we have too little directory info? */
  185. #define GREEDY_DESCRIPTOR_RETRY_INTERVAL (10)
  186. /** How often do we check for router descriptors that we should download
  187. * when we have enough directory info? */
  188. #define LAZY_DESCRIPTOR_RETRY_INTERVAL (60)
  189. static int conn_close_if_marked(int i);
  190. static void connection_start_reading_from_linked_conn(connection_t *conn);
  191. static int connection_should_read_from_linked_conn(connection_t *conn);
  192. static void conn_read_callback(evutil_socket_t fd, short event, void *_conn);
  193. static void conn_write_callback(evutil_socket_t fd, short event, void *_conn);
  194. static void shutdown_did_not_work_callback(evutil_socket_t fd, short event,
  195. void *arg) ATTR_NORETURN;
  196. static event_registry_t *event_registry = NULL;
  197. /****************************************************************************
  198. *
  199. * This section contains accessors and other methods on the connection_array
  200. * variables (which are global within this file and unavailable outside it).
  201. *
  202. ****************************************************************************/
  203. /** Return 1 if we have successfully built a circuit, and nothing has changed
  204. * to make us think that maybe we can't.
  205. */
  206. int
  207. have_completed_a_circuit(void)
  208. {
  209. return can_complete_circuits;
  210. }
  211. /** Note that we have successfully built a circuit, so that reachability
  212. * testing and introduction points and so on may be attempted. */
  213. void
  214. note_that_we_completed_a_circuit(void)
  215. {
  216. can_complete_circuits = 1;
  217. }
  218. /** Note that something has happened (like a clock jump, or DisableNetwork) to
  219. * make us think that maybe we can't complete circuits. */
  220. void
  221. note_that_we_maybe_cant_complete_circuits(void)
  222. {
  223. can_complete_circuits = 0;
  224. }
  225. static unsigned long eventloop_counter = 0;
  226. /** Add <b>conn</b> to the array of connections that we can poll on. The
  227. * connection's socket must be set; the connection starts out
  228. * non-reading and non-writing.
  229. */
  230. int
  231. connection_add_impl(connection_t *conn, int is_connecting)
  232. {
  233. (void) is_connecting;
  234. tor_assert(conn != NULL);
  235. tor_assert(conn->conn_array_index == -1); /* can only connection_add once */
  236. conn->conn_array_index = smartlist_len(connection_array);
  237. smartlist_add(connection_array, conn);
  238. event_listener_attach(conn->event_listener, tor_libevent_get_base());
  239. // TODO: check for error
  240. if (conn->safe_conn == NULL) {
  241. tor_assert(conn->type != CONN_TYPE_OR);
  242. tor_assert(SOCKET_OK(conn->s) ||
  243. conn->linked ||
  244. (conn->type == CONN_TYPE_AP &&
  245. TO_EDGE_CONN(conn)->is_dns_request));
  246. if (SOCKET_OK(conn->s) || conn->linked) {
  247. conn->read_event = tor_event_new(tor_libevent_get_base(),
  248. conn->s, EV_READ|EV_PERSIST, conn_read_callback, conn);
  249. conn->write_event = tor_event_new(tor_libevent_get_base(),
  250. conn->s, EV_WRITE|EV_PERSIST, conn_write_callback, conn);
  251. /* XXXX CHECK FOR NULL RETURN! */
  252. }
  253. log_debug(LD_NET, "new conn type %s, socket %d, address %s, n_conns %d.",
  254. conn_type_to_string(conn->type), (int)conn->s, conn->address,
  255. smartlist_len(connection_array));
  256. } else {
  257. tor_assert(conn->type == CONN_TYPE_OR);
  258. int eventloop_index;
  259. int num_eventloops = get_num_eventloops();
  260. if (num_eventloops > 1) {
  261. // use one of the worker threads
  262. eventloop_index = 1 + (eventloop_counter%(num_eventloops-1));
  263. eventloop_counter += 1;
  264. } else {
  265. // use the main thread
  266. eventloop_index = 0;
  267. }
  268. struct event_base *base = get_eventloop(eventloop_index);
  269. error_t rv = safe_connection_register_events(conn->safe_conn, base);
  270. if (rv != E_SUCCESS) {
  271. smartlist_remove(connection_array, conn);
  272. conn->conn_array_index = -1;
  273. return 1;
  274. }
  275. log_debug(LD_NET, "new conn type %s, address %s, n_conns %d.",
  276. conn_type_to_string(conn->type), conn->address,
  277. smartlist_len(connection_array));
  278. }
  279. return 0;
  280. }
  281. /** Tell libevent that we don't care about <b>conn</b> any more. */
  282. void
  283. connection_unregister_events(connection_t *conn)
  284. {
  285. tor_assert(conn != NULL);
  286. if (conn->safe_conn == NULL) {
  287. tor_assert(conn->type != CONN_TYPE_OR);
  288. if (conn->read_event) {
  289. if (event_del(conn->read_event))
  290. log_warn(LD_BUG, "Error removing read event for %d", (int)conn->s);
  291. tor_free(conn->read_event);
  292. }
  293. if (conn->write_event) {
  294. if (event_del(conn->write_event))
  295. log_warn(LD_BUG, "Error removing write event for %d", (int)conn->s);
  296. tor_free(conn->write_event);
  297. }
  298. if (conn->type == CONN_TYPE_AP_DNS_LISTENER) {
  299. dnsserv_close_listener(conn);
  300. }
  301. } else {
  302. tor_assert(conn->type == CONN_TYPE_OR);
  303. safe_connection_unregister_events(conn->safe_conn);
  304. }
  305. event_listener_detach(conn->event_listener);
  306. // TODO: do we want to process the remaining events?
  307. }
  308. /** Remove the connection from the global list, and remove the
  309. * corresponding poll entry. Calling this function will shift the last
  310. * connection (if any) into the position occupied by conn.
  311. */
  312. int
  313. connection_remove(connection_t *conn)
  314. {
  315. int current_index;
  316. connection_t *tmp;
  317. tor_assert(conn);
  318. log_debug(LD_NET, "removing socket %d (type %s), n_conns now %d",
  319. (int)conn->s, conn_type_to_string(conn->type),
  320. smartlist_len(connection_array));
  321. if (conn->type == CONN_TYPE_AP && conn->socket_family == AF_UNIX) {
  322. log_info(LD_NET, "Closing SOCKS Unix socket connection");
  323. }
  324. control_event_conn_bandwidth(conn);
  325. tor_assert(conn->conn_array_index >= 0);
  326. current_index = conn->conn_array_index;
  327. connection_unregister_events(conn); /* This is redundant, but cheap. */
  328. if (current_index == smartlist_len(connection_array)-1) { /* at the end */
  329. smartlist_del(connection_array, current_index);
  330. return 0;
  331. }
  332. /* replace this one with the one at the end */
  333. smartlist_del(connection_array, current_index);
  334. tmp = smartlist_get(connection_array, current_index);
  335. tmp->conn_array_index = current_index;
  336. return 0;
  337. }
  338. /** If <b>conn</b> is an edge conn, remove it from the list
  339. * of conn's on this circuit. If it's not on an edge,
  340. * flush and send destroys for all circuits on this conn.
  341. *
  342. * Remove it from connection_array (if applicable) and
  343. * from closeable_connection_list.
  344. *
  345. * Then free it.
  346. */
  347. static void
  348. connection_unlink(connection_t *conn)
  349. {
  350. connection_about_to_close_connection(conn);
  351. if (conn->conn_array_index >= 0) {
  352. connection_remove(conn);
  353. }
  354. if (conn->linked_conn) {
  355. conn->linked_conn->linked_conn = NULL;
  356. if (! conn->linked_conn->marked_for_close &&
  357. conn->linked_conn->reading_from_linked_conn)
  358. connection_start_reading(conn->linked_conn);
  359. conn->linked_conn = NULL;
  360. }
  361. smartlist_remove(closeable_connection_lst, conn);
  362. smartlist_remove(active_linked_connection_lst, conn);
  363. if (conn->type == CONN_TYPE_EXIT) {
  364. assert_connection_edge_not_dns_pending(TO_EDGE_CONN(conn));
  365. }
  366. if (conn->type == CONN_TYPE_OR) {
  367. if (!tor_digest_is_zero(TO_OR_CONN(conn)->identity_digest))
  368. connection_or_clear_identity(TO_OR_CONN(conn));
  369. /* connection_unlink() can only get called if the connection
  370. * was already on the closeable list, and it got there by
  371. * connection_mark_for_close(), which was called from
  372. * connection_or_close_normally() or
  373. * connection_or_close_for_error(), so the channel should
  374. * already be in CHANNEL_STATE_CLOSING, and then the
  375. * connection_about_to_close_connection() goes to
  376. * connection_or_about_to_close(), which calls channel_closed()
  377. * to notify the channel_t layer, and closed the channel, so
  378. * nothing more to do here to deal with the channel associated
  379. * with an orconn.
  380. */
  381. }
  382. connection_free(conn);
  383. }
  384. /** Event that invokes schedule_active_linked_connections_cb. */
  385. static mainloop_event_t *schedule_active_linked_connections_event = NULL;
  386. /**
  387. * Callback: used to activate read events for all linked connections, so
  388. * libevent knows to call their read callbacks. This callback run as a
  389. * postloop event, so that the events _it_ activates don't happen until
  390. * Libevent has a chance to check for other events.
  391. */
  392. static void
  393. schedule_active_linked_connections_cb(mainloop_event_t *event, void *arg)
  394. {
  395. (void)event;
  396. (void)arg;
  397. /* All active linked conns should get their read events activated,
  398. * so that libevent knows to run their callbacks. */
  399. SMARTLIST_FOREACH(active_linked_connection_lst, connection_t *, conn,
  400. event_active(conn->read_event, EV_READ, 1));
  401. /* Reactivate the event if we still have connections in the active list.
  402. *
  403. * A linked connection doesn't get woken up by I/O but rather artificially
  404. * by this event callback. It has directory data spooled in it and it is
  405. * sent incrementally by small chunks unless spool_eagerly is true. For that
  406. * to happen, we need to induce the activation of the read event so it can
  407. * be flushed. */
  408. if (smartlist_len(active_linked_connection_lst)) {
  409. mainloop_event_activate(schedule_active_linked_connections_event);
  410. }
  411. }
  412. /** Initialize the global connection list, closeable connection list,
  413. * and active connection list. */
  414. void
  415. tor_init_connection_lists(void)
  416. {
  417. if (!connection_array)
  418. connection_array = smartlist_new();
  419. if (!closeable_connection_lst)
  420. closeable_connection_lst = smartlist_new();
  421. if (!active_linked_connection_lst)
  422. active_linked_connection_lst = smartlist_new();
  423. }
  424. /** Schedule <b>conn</b> to be closed. **/
  425. void
  426. add_connection_to_closeable_list(connection_t *conn)
  427. {
  428. tor_assert(!smartlist_contains(closeable_connection_lst, conn));
  429. tor_assert(conn->marked_for_close);
  430. assert_connection_ok(conn, time(NULL));
  431. smartlist_add(closeable_connection_lst, conn);
  432. mainloop_schedule_postloop_cleanup();
  433. }
  434. /** Return 1 if conn is on the closeable list, else return 0. */
  435. int
  436. connection_is_on_closeable_list(connection_t *conn)
  437. {
  438. return smartlist_contains(closeable_connection_lst, conn);
  439. }
  440. /** Return true iff conn is in the current poll array. */
  441. int
  442. connection_in_array(connection_t *conn)
  443. {
  444. return smartlist_contains(connection_array, conn);
  445. }
  446. /** Set <b>*array</b> to an array of all connections. <b>*array</b> must not
  447. * be modified.
  448. */
  449. MOCK_IMPL(smartlist_t *,
  450. get_connection_array, (void))
  451. {
  452. if (!connection_array)
  453. connection_array = smartlist_new();
  454. return connection_array;
  455. }
  456. /**
  457. * Return the amount of network traffic read, in bytes, over the life of this
  458. * process.
  459. */
  460. MOCK_IMPL(uint64_t,
  461. get_bytes_read,(void))
  462. {
  463. return stats_n_bytes_read;
  464. }
  465. /**
  466. * Return the amount of network traffic read, in bytes, over the life of this
  467. * process.
  468. */
  469. MOCK_IMPL(uint64_t,
  470. get_bytes_written,(void))
  471. {
  472. return stats_n_bytes_written;
  473. }
  474. /**
  475. * Increment the amount of network traffic read and written, over the life of
  476. * this process.
  477. */
  478. void
  479. stats_increment_bytes_read_and_written(uint64_t r, uint64_t w)
  480. {
  481. stats_n_bytes_read += r;
  482. stats_n_bytes_written += w;
  483. }
  484. /** Set the event mask on <b>conn</b> to <b>events</b>. (The event
  485. * mask is a bitmask whose bits are READ_EVENT and WRITE_EVENT)
  486. */
  487. void
  488. connection_watch_events(connection_t *conn, watchable_events_t events)
  489. {
  490. if (events & READ_EVENT)
  491. connection_start_reading(conn);
  492. else
  493. connection_stop_reading(conn);
  494. if (events & WRITE_EVENT)
  495. connection_start_writing(conn);
  496. else
  497. connection_stop_writing(conn);
  498. }
  499. /** Return true iff <b>conn</b> is listening for read events. */
  500. int
  501. connection_is_reading(connection_t *conn)
  502. {
  503. tor_assert(conn);
  504. return conn->reading_from_linked_conn ||
  505. (conn->read_event && event_pending(conn->read_event, EV_READ, NULL));
  506. }
  507. /** Reset our main loop counters. */
  508. void
  509. reset_main_loop_counters(void)
  510. {
  511. stats_n_main_loop_successes = 0;
  512. stats_n_main_loop_errors = 0;
  513. stats_n_main_loop_idle = 0;
  514. }
  515. /** Increment the main loop success counter. */
  516. static void
  517. increment_main_loop_success_count(void)
  518. {
  519. ++stats_n_main_loop_successes;
  520. }
  521. /** Get the main loop success counter. */
  522. uint64_t
  523. get_main_loop_success_count(void)
  524. {
  525. return stats_n_main_loop_successes;
  526. }
  527. /** Increment the main loop error counter. */
  528. static void
  529. increment_main_loop_error_count(void)
  530. {
  531. ++stats_n_main_loop_errors;
  532. }
  533. /** Get the main loop error counter. */
  534. uint64_t
  535. get_main_loop_error_count(void)
  536. {
  537. return stats_n_main_loop_errors;
  538. }
  539. /** Increment the main loop idle counter. */
  540. static void
  541. increment_main_loop_idle_count(void)
  542. {
  543. ++stats_n_main_loop_idle;
  544. }
  545. /** Get the main loop idle counter. */
  546. uint64_t
  547. get_main_loop_idle_count(void)
  548. {
  549. return stats_n_main_loop_idle;
  550. }
  551. /** Check whether <b>conn</b> is correct in having (or not having) a
  552. * read/write event (passed in <b>ev</b>). On success, return 0. On failure,
  553. * log a warning and return -1. */
  554. static int
  555. connection_check_event(connection_t *conn, struct event *ev)
  556. {
  557. int bad;
  558. if (conn->type == CONN_TYPE_AP && TO_EDGE_CONN(conn)->is_dns_request) {
  559. /* DNS requests which we launch through the dnsserv.c module do not have
  560. * any underlying socket or any underlying linked connection, so they
  561. * shouldn't have any attached events either.
  562. */
  563. bad = ev != NULL;
  564. } else {
  565. /* Everything else should have an underlying socket, or a linked
  566. * connection (which is also tracked with a read_event/write_event pair).
  567. */
  568. bad = ev == NULL;
  569. }
  570. if (bad) {
  571. log_warn(LD_BUG, "Event missing on connection %p [%s;%s]. "
  572. "socket=%d. linked=%d. "
  573. "is_dns_request=%d. Marked_for_close=%s:%d",
  574. conn,
  575. conn_type_to_string(conn->type),
  576. conn_state_to_string(conn->type, conn->state),
  577. (int)conn->s, (int)conn->linked,
  578. (conn->type == CONN_TYPE_AP &&
  579. TO_EDGE_CONN(conn)->is_dns_request),
  580. conn->marked_for_close_file ? conn->marked_for_close_file : "-",
  581. conn->marked_for_close
  582. );
  583. log_backtrace(LOG_WARN, LD_BUG, "Backtrace attached.");
  584. return -1;
  585. }
  586. return 0;
  587. }
  588. /** Tell the main loop to stop notifying <b>conn</b> of any read events. */
  589. MOCK_IMPL(void,
  590. connection_stop_reading,(connection_t *conn))
  591. {
  592. tor_assert(conn);
  593. tor_assert(conn->type != CONN_TYPE_OR);
  594. if (connection_check_event(conn, conn->read_event) < 0) {
  595. return;
  596. }
  597. if (conn->linked) {
  598. conn->reading_from_linked_conn = 0;
  599. connection_stop_reading_from_linked_conn(conn);
  600. } else {
  601. if (event_del(conn->read_event))
  602. log_warn(LD_NET, "Error from libevent setting read event state for %d "
  603. "to unwatched: %s",
  604. (int)conn->s,
  605. tor_socket_strerror(tor_socket_errno(conn->s)));
  606. }
  607. }
  608. /** Tell the main loop to start notifying <b>conn</b> of any read events. */
  609. MOCK_IMPL(void,
  610. connection_start_reading,(connection_t *conn))
  611. {
  612. tor_assert(conn);
  613. tor_assert(conn->type != CONN_TYPE_OR);
  614. if (connection_check_event(conn, conn->read_event) < 0) {
  615. return;
  616. }
  617. if (conn->linked) {
  618. conn->reading_from_linked_conn = 1;
  619. if (connection_should_read_from_linked_conn(conn))
  620. connection_start_reading_from_linked_conn(conn);
  621. } else {
  622. if (event_add(conn->read_event, NULL))
  623. log_warn(LD_NET, "Error from libevent setting read event state for %d "
  624. "to watched: %s",
  625. (int)conn->s,
  626. tor_socket_strerror(tor_socket_errno(conn->s)));
  627. }
  628. }
  629. /** Return true iff <b>conn</b> is listening for write events. */
  630. int
  631. connection_is_writing(connection_t *conn)
  632. {
  633. tor_assert(conn);
  634. tor_assert(conn->type != CONN_TYPE_OR);
  635. return conn->writing_to_linked_conn ||
  636. (conn->write_event && event_pending(conn->write_event, EV_WRITE, NULL));
  637. }
  638. /** Tell the main loop to stop notifying <b>conn</b> of any write events. */
  639. MOCK_IMPL(void,
  640. connection_stop_writing,(connection_t *conn))
  641. {
  642. tor_assert(conn);
  643. tor_assert(conn->type != CONN_TYPE_OR);
  644. if (connection_check_event(conn, conn->write_event) < 0) {
  645. return;
  646. }
  647. if (conn->linked) {
  648. conn->writing_to_linked_conn = 0;
  649. if (conn->linked_conn)
  650. connection_stop_reading_from_linked_conn(conn->linked_conn);
  651. } else {
  652. if (event_del(conn->write_event))
  653. log_warn(LD_NET, "Error from libevent setting write event state for %d "
  654. "to unwatched: %s",
  655. (int)conn->s,
  656. tor_socket_strerror(tor_socket_errno(conn->s)));
  657. }
  658. }
  659. /** Tell the main loop to start notifying <b>conn</b> of any write events. */
  660. MOCK_IMPL(void,
  661. connection_start_writing,(connection_t *conn))
  662. {
  663. tor_assert(conn);
  664. tor_assert(conn->type != CONN_TYPE_OR);
  665. if (connection_check_event(conn, conn->write_event) < 0) {
  666. return;
  667. }
  668. if (conn->linked) {
  669. conn->writing_to_linked_conn = 1;
  670. if (conn->linked_conn &&
  671. connection_should_read_from_linked_conn(conn->linked_conn))
  672. connection_start_reading_from_linked_conn(conn->linked_conn);
  673. } else {
  674. if (event_add(conn->write_event, NULL))
  675. log_warn(LD_NET, "Error from libevent setting write event state for %d "
  676. "to watched: %s",
  677. (int)conn->s,
  678. tor_socket_strerror(tor_socket_errno(conn->s)));
  679. }
  680. }
  681. /** Return true iff <b>conn</b> is linked conn, and reading from the conn
  682. * linked to it would be good and feasible. (Reading is "feasible" if the
  683. * other conn exists and has data in its outbuf, and is "good" if we have our
  684. * reading_from_linked_conn flag set and the other conn has its
  685. * writing_to_linked_conn flag set.)*/
  686. static int
  687. connection_should_read_from_linked_conn(connection_t *conn)
  688. {
  689. if (conn->linked && conn->reading_from_linked_conn) {
  690. if (! conn->linked_conn ||
  691. (conn->linked_conn->writing_to_linked_conn &&
  692. buf_datalen(conn->linked_conn->outbuf)))
  693. return 1;
  694. }
  695. return 0;
  696. }
  697. /** Event to run 'shutdown did not work callback'. */
  698. static struct event *shutdown_did_not_work_event = NULL;
  699. /** Failsafe measure that should never actually be necessary: If
  700. * tor_shutdown_event_loop_and_exit() somehow doesn't successfully exit the
  701. * event loop, then this callback will kill Tor with an assertion failure
  702. * seconds later
  703. */
  704. static void
  705. shutdown_did_not_work_callback(evutil_socket_t fd, short event, void *arg)
  706. {
  707. // LCOV_EXCL_START
  708. (void) fd;
  709. (void) event;
  710. (void) arg;
  711. tor_assert_unreached();
  712. // LCOV_EXCL_STOP
  713. }
  714. #ifdef ENABLE_RESTART_DEBUGGING
  715. static struct event *tor_shutdown_event_loop_for_restart_event = NULL;
  716. static void
  717. tor_shutdown_event_loop_for_restart_cb(
  718. evutil_socket_t fd, short event, void *arg)
  719. {
  720. (void)fd;
  721. (void)event;
  722. (void)arg;
  723. tor_event_free(tor_shutdown_event_loop_for_restart_event);
  724. tor_shutdown_event_loop_and_exit(0);
  725. }
  726. #endif /* defined(ENABLE_RESTART_DEBUGGING) */
  727. /**
  728. * After finishing the current callback (if any), shut down the main loop,
  729. * clean up the process, and exit with <b>exitcode</b>.
  730. */
  731. void
  732. tor_shutdown_event_loop_and_exit(int exitcode)
  733. {
  734. if (main_loop_should_exit)
  735. return; /* Ignore multiple calls to this function. */
  736. main_loop_should_exit = 1;
  737. main_loop_exit_value = exitcode;
  738. if (! tor_libevent_is_initialized()) {
  739. return; /* No event loop to shut down. */
  740. }
  741. /* Die with an assertion failure in ten seconds, if for some reason we don't
  742. * exit normally. */
  743. /* XXXX We should consider this code if it's never used. */
  744. struct timeval ten_seconds = { 10, 0 };
  745. shutdown_did_not_work_event = tor_evtimer_new(
  746. tor_libevent_get_base(),
  747. shutdown_did_not_work_callback, NULL);
  748. event_add(shutdown_did_not_work_event, &ten_seconds);
  749. /* Unlike exit_loop_after_delay(), exit_loop_after_callback
  750. * prevents other callbacks from running. */
  751. tor_libevent_exit_loop_after_callback(tor_libevent_get_base());
  752. }
  753. /** Return true iff tor_shutdown_event_loop_and_exit() has been called. */
  754. int
  755. tor_event_loop_shutdown_is_pending(void)
  756. {
  757. return main_loop_should_exit;
  758. }
  759. /** Helper: Tell the main loop to begin reading bytes into <b>conn</b> from
  760. * its linked connection, if it is not doing so already. Called by
  761. * connection_start_reading and connection_start_writing as appropriate. */
  762. static void
  763. connection_start_reading_from_linked_conn(connection_t *conn)
  764. {
  765. tor_assert(conn);
  766. tor_assert(conn->linked == 1);
  767. if (!conn->active_on_link) {
  768. conn->active_on_link = 1;
  769. smartlist_add(active_linked_connection_lst, conn);
  770. mainloop_event_activate(schedule_active_linked_connections_event);
  771. } else {
  772. tor_assert(smartlist_contains(active_linked_connection_lst, conn));
  773. }
  774. }
  775. /** Tell the main loop to stop reading bytes into <b>conn</b> from its linked
  776. * connection, if is currently doing so. Called by connection_stop_reading,
  777. * connection_stop_writing, and connection_read. */
  778. void
  779. connection_stop_reading_from_linked_conn(connection_t *conn)
  780. {
  781. tor_assert(conn);
  782. tor_assert(conn->linked == 1);
  783. if (conn->active_on_link) {
  784. conn->active_on_link = 0;
  785. /* FFFF We could keep an index here so we can smartlist_del
  786. * cleanly. On the other hand, this doesn't show up on profiles,
  787. * so let's leave it alone for now. */
  788. smartlist_remove(active_linked_connection_lst, conn);
  789. } else {
  790. tor_assert(!smartlist_contains(active_linked_connection_lst, conn));
  791. }
  792. }
  793. /** Close all connections that have been scheduled to get closed. */
  794. STATIC void
  795. close_closeable_connections(void)
  796. {
  797. int i;
  798. for (i = 0; i < smartlist_len(closeable_connection_lst); ) {
  799. connection_t *conn = smartlist_get(closeable_connection_lst, i);
  800. if (conn->conn_array_index < 0) {
  801. connection_unlink(conn); /* blow it away right now */
  802. } else {
  803. if (!conn_close_if_marked(conn->conn_array_index))
  804. ++i;
  805. }
  806. }
  807. }
  808. /** Count moribund connections for the OOS handler */
  809. MOCK_IMPL(int,
  810. connection_count_moribund, (void))
  811. {
  812. int moribund = 0;
  813. /*
  814. * Count things we'll try to kill when close_closeable_connections()
  815. * runs next.
  816. */
  817. SMARTLIST_FOREACH_BEGIN(closeable_connection_lst, connection_t *, conn) {
  818. if (SOCKET_OK(conn->s) && connection_is_moribund(conn)) ++moribund;
  819. } SMARTLIST_FOREACH_END(conn);
  820. return moribund;
  821. }
  822. /** Libevent callback: this gets invoked when (connection_t*)<b>conn</b> has
  823. * some data to read. */
  824. static void
  825. conn_read_callback(evutil_socket_t fd, short event, void *_conn)
  826. {
  827. connection_t *conn = _conn;
  828. (void)fd;
  829. (void)event;
  830. log_debug(LD_NET,"socket %d wants to read.",(int)conn->s);
  831. /* assert_connection_ok(conn, time(NULL)); */
  832. /* Handle marked for close connections early */
  833. if (conn->marked_for_close && connection_is_reading(conn)) {
  834. /* Libevent says we can read, but we are marked for close so we will never
  835. * try to read again. We will try to close the connection below inside of
  836. * close_closeable_connections(), but let's make sure not to cause Libevent
  837. * to spin on conn_read_callback() while we wait for the socket to let us
  838. * flush to it.*/
  839. connection_stop_reading(conn);
  840. }
  841. if (connection_handle_read(conn) < 0) {
  842. if (!conn->marked_for_close) {
  843. #ifndef _WIN32
  844. log_warn(LD_BUG,"Unhandled error on read for %s connection "
  845. "(fd %d); removing",
  846. conn_type_to_string(conn->type), (int)conn->s);
  847. tor_fragile_assert();
  848. #endif /* !defined(_WIN32) */
  849. if (CONN_IS_EDGE(conn))
  850. connection_edge_end_errno(TO_EDGE_CONN(conn));
  851. connection_mark_for_close(conn);
  852. }
  853. }
  854. assert_connection_ok(conn, time(NULL));
  855. if (smartlist_len(closeable_connection_lst))
  856. close_closeable_connections();
  857. }
  858. /** Libevent callback: this gets invoked when (connection_t*)<b>conn</b> has
  859. * some data to write. */
  860. static void
  861. conn_write_callback(evutil_socket_t fd, short events, void *_conn)
  862. {
  863. connection_t *conn = _conn;
  864. (void)fd;
  865. (void)events;
  866. LOG_FN_CONN(conn, (LOG_DEBUG, LD_NET, "socket %d wants to write.",
  867. (int)conn->s));
  868. /* assert_connection_ok(conn, time(NULL)); */
  869. if (connection_handle_write(conn, 0) < 0) {
  870. if (!conn->marked_for_close) {
  871. /* this connection is broken. remove it. */
  872. log_fn(LOG_WARN,LD_BUG,
  873. "unhandled error on write for %s connection (fd %d); removing",
  874. conn_type_to_string(conn->type), (int)conn->s);
  875. tor_fragile_assert();
  876. if (CONN_IS_EDGE(conn)) {
  877. /* otherwise we cry wolf about duplicate close */
  878. edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
  879. if (!edge_conn->end_reason)
  880. edge_conn->end_reason = END_STREAM_REASON_INTERNAL;
  881. edge_conn->edge_has_sent_end = 1;
  882. }
  883. connection_close_immediate(conn); /* So we don't try to flush. */
  884. connection_mark_for_close(conn);
  885. }
  886. }
  887. assert_connection_ok(conn, time(NULL));
  888. if (smartlist_len(closeable_connection_lst))
  889. close_closeable_connections();
  890. }
  891. /** If the connection at connection_array[i] is marked for close, then:
  892. * - If it has data that it wants to flush, try to flush it.
  893. * - If it _still_ has data to flush, and conn->hold_open_until_flushed is
  894. * true, then leave the connection open and return.
  895. * - Otherwise, remove the connection from connection_array and from
  896. * all other lists, close it, and free it.
  897. * Returns 1 if the connection was closed, 0 otherwise.
  898. */
  899. static int
  900. conn_close_if_marked(int i)
  901. {
  902. connection_t *conn;
  903. int retval;
  904. time_t now;
  905. conn = smartlist_get(connection_array, i);
  906. if (!conn->marked_for_close)
  907. return 0; /* nothing to see here, move along */
  908. now = time(NULL);
  909. assert_connection_ok(conn, now);
  910. /* assert_all_pending_dns_resolves_ok(); */
  911. log_debug(LD_NET,"Cleaning up connection (fd "TOR_SOCKET_T_FORMAT").",
  912. conn->s);
  913. /* If the connection we are about to close was trying to connect to
  914. a proxy server and failed, the client won't be able to use that
  915. proxy. We should warn the user about this. */
  916. if (conn->proxy_state == PROXY_INFANT)
  917. log_failed_proxy_connection(conn);
  918. if ((SOCKET_OK(conn->s) || conn->linked_conn) &&
  919. connection_wants_to_flush(conn)) {
  920. /* s == -1 means it's an incomplete edge connection, or that the socket
  921. * has already been closed as unflushable. */
  922. //ssize_t sz = connection_bucket_write_limit(conn, now);
  923. ssize_t sz = 0;
  924. if (!conn->hold_open_until_flushed)
  925. log_info(LD_NET,
  926. "Conn (addr %s, fd %d, type %s, state %d) marked, but wants "
  927. "to flush %d bytes. (Marked at %s:%d)",
  928. escaped_safe_str_client(conn->address),
  929. (int)conn->s, conn_type_to_string(conn->type), conn->state,
  930. (int)conn->outbuf_flushlen,
  931. conn->marked_for_close_file, conn->marked_for_close);
  932. if (conn->linked_conn) {
  933. sz = connection_bucket_write_limit(conn, now);
  934. retval = buf_move_to_buf(conn->linked_conn->inbuf, conn->outbuf,
  935. &conn->outbuf_flushlen);
  936. if (retval >= 0) {
  937. /* The linked conn will notice that it has data when it notices that
  938. * we're gone. */
  939. connection_start_reading_from_linked_conn(conn->linked_conn);
  940. }
  941. log_debug(LD_GENERAL, "Flushed last %d bytes from a linked conn; "
  942. "%d left; flushlen %d; wants-to-flush==%d", retval,
  943. (int)connection_get_outbuf_len(conn),
  944. (int)conn->outbuf_flushlen,
  945. connection_wants_to_flush(conn));
  946. } else if (connection_speaks_cells(conn)) {
  947. if (conn->state == OR_CONN_STATE_OPEN) {
  948. // TODO: force TLS flush here, need to communicate to other thread somehow
  949. // really we need to tell the thread to close, and whether it should
  950. // try to flush or not
  951. retval = -1;
  952. //retval = buf_flush_to_tls(conn->outbuf, TO_OR_CONN(conn)->tls, sz,
  953. // &conn->outbuf_flushlen);
  954. } else
  955. retval = -1; /* never flush non-open broken tls connections */
  956. } else {
  957. sz = connection_bucket_write_limit(conn, now);
  958. retval = buf_flush_to_socket(conn->outbuf, conn->s, sz,
  959. &conn->outbuf_flushlen);
  960. }
  961. if (retval >= 0 && /* Technically, we could survive things like
  962. TLS_WANT_WRITE here. But don't bother for now. */
  963. conn->hold_open_until_flushed && connection_wants_to_flush(conn)) {
  964. if (retval > 0) {
  965. LOG_FN_CONN(conn, (LOG_INFO,LD_NET,
  966. "Holding conn (fd %d) open for more flushing.",
  967. (int)conn->s));
  968. conn->timestamp_last_write_allowed = now; /* reset so we can flush
  969. * more */
  970. } else if (sz == 0) {
  971. /* Also, retval==0. If we get here, we didn't want to write anything
  972. * (because of rate-limiting) and we didn't. */
  973. /* Connection must flush before closing, but it's being rate-limited.
  974. * Let's remove from Libevent, and mark it as blocked on bandwidth
  975. * so it will be re-added on next token bucket refill. Prevents
  976. * busy Libevent loops where we keep ending up here and returning
  977. * 0 until we are no longer blocked on bandwidth.
  978. */
  979. connection_consider_empty_write_buckets(conn);
  980. /* Make sure that consider_empty_buckets really disabled the
  981. * connection: */
  982. if (BUG(connection_is_writing(conn))) {
  983. connection_write_bw_exhausted(conn, true);
  984. }
  985. /* The connection is being held due to write rate limit and thus will
  986. * flush its data later. We need to stop reading because this
  987. * connection is about to be closed once flushed. It should not
  988. * process anything more coming in at this stage. */
  989. connection_stop_reading(conn);
  990. }
  991. return 0;
  992. }
  993. if (connection_wants_to_flush(conn)) {
  994. //log_fn(LOG_INFO, LD_NET, "We stalled too much while trying to write %d "
  995. log_fn(LOG_INFO, LD_NET, "We stalled too much while trying to write <?> "
  996. "bytes to address %s. If this happens a lot, either "
  997. "something is wrong with your network connection, or "
  998. "something is wrong with theirs. "
  999. "(fd %d, type %s, state %d, marked at %s:%d).",
  1000. //(int)connection_get_outbuf_len(conn),
  1001. escaped_safe_str_client(conn->address),
  1002. (int)conn->s, conn_type_to_string(conn->type), conn->state,
  1003. conn->marked_for_close_file,
  1004. conn->marked_for_close);
  1005. }
  1006. }
  1007. connection_unlink(conn); /* unlink, remove, free */
  1008. return 1;
  1009. }
  1010. /** Implementation for directory_all_unreachable. This is done in a callback,
  1011. * since otherwise it would complicate Tor's control-flow graph beyond all
  1012. * reason.
  1013. */
  1014. static void
  1015. directory_all_unreachable_cb(mainloop_event_t *event, void *arg)
  1016. {
  1017. (void)event;
  1018. (void)arg;
  1019. connection_t *conn;
  1020. while ((conn = connection_get_by_type_state(CONN_TYPE_AP,
  1021. AP_CONN_STATE_CIRCUIT_WAIT))) {
  1022. entry_connection_t *entry_conn = TO_ENTRY_CONN(conn);
  1023. log_notice(LD_NET,
  1024. "Is your network connection down? "
  1025. "Failing connection to '%s:%d'.",
  1026. safe_str_client(entry_conn->socks_request->address),
  1027. entry_conn->socks_request->port);
  1028. connection_mark_unattached_ap(entry_conn,
  1029. END_STREAM_REASON_NET_UNREACHABLE);
  1030. }
  1031. control_event_general_error("DIR_ALL_UNREACHABLE");
  1032. }
  1033. static mainloop_event_t *directory_all_unreachable_cb_event = NULL;
  1034. /** We've just tried every dirserver we know about, and none of
  1035. * them were reachable. Assume the network is down. Change state
  1036. * so next time an application connection arrives we'll delay it
  1037. * and try another directory fetch. Kill off all the circuit_wait
  1038. * streams that are waiting now, since they will all timeout anyway.
  1039. */
  1040. void
  1041. directory_all_unreachable(time_t now)
  1042. {
  1043. (void)now;
  1044. reset_uptime(); /* reset it */
  1045. if (!directory_all_unreachable_cb_event) {
  1046. directory_all_unreachable_cb_event =
  1047. mainloop_event_new(directory_all_unreachable_cb, NULL);
  1048. tor_assert(directory_all_unreachable_cb_event);
  1049. }
  1050. mainloop_event_activate(directory_all_unreachable_cb_event);
  1051. }
  1052. /** This function is called whenever we successfully pull down some new
  1053. * network statuses or server descriptors. */
  1054. void
  1055. directory_info_has_arrived(time_t now, int from_cache, int suppress_logs)
  1056. {
  1057. const or_options_t *options = get_options();
  1058. /* if we have enough dir info, then update our guard status with
  1059. * whatever we just learned. */
  1060. int invalidate_circs = guards_update_all();
  1061. if (invalidate_circs) {
  1062. circuit_mark_all_unused_circs();
  1063. circuit_mark_all_dirty_circs_as_unusable();
  1064. }
  1065. if (!router_have_minimum_dir_info()) {
  1066. int quiet = suppress_logs || from_cache ||
  1067. directory_too_idle_to_fetch_descriptors(options, now);
  1068. tor_log(quiet ? LOG_INFO : LOG_NOTICE, LD_DIR,
  1069. "I learned some more directory information, but not enough to "
  1070. "build a circuit: %s", get_dir_info_status_string());
  1071. update_all_descriptor_downloads(now);
  1072. return;
  1073. } else {
  1074. if (directory_fetches_from_authorities(options)) {
  1075. update_all_descriptor_downloads(now);
  1076. }
  1077. /* Don't even bother trying to get extrainfo until the rest of our
  1078. * directory info is up-to-date */
  1079. if (options->DownloadExtraInfo)
  1080. update_extrainfo_downloads(now);
  1081. }
  1082. if (server_mode(options) && !net_is_disabled() && !from_cache &&
  1083. (have_completed_a_circuit() || !any_predicted_circuits(now)))
  1084. router_do_reachability_checks(1, 1);
  1085. }
  1086. /** Perform regular maintenance tasks for a single connection. This
  1087. * function gets run once per second per connection by run_scheduled_events.
  1088. */
  1089. static void
  1090. run_connection_housekeeping(int i, time_t now)
  1091. {
  1092. connection_t *conn = smartlist_get(connection_array, i);
  1093. const or_options_t *options = get_options();
  1094. or_connection_t *or_conn;
  1095. channel_t *chan = NULL;
  1096. int have_any_circuits;
  1097. int past_keepalive =
  1098. now >= conn->timestamp_last_write_allowed + options->KeepalivePeriod;
  1099. if (conn->outbuf && !connection_get_outbuf_len(conn) &&
  1100. conn->type == CONN_TYPE_OR)
  1101. TO_OR_CONN(conn)->timestamp_lastempty = now;
  1102. if (conn->marked_for_close) {
  1103. /* nothing to do here */
  1104. return;
  1105. }
  1106. /* Expire any directory connections that haven't been active (sent
  1107. * if a server or received if a client) for 5 min */
  1108. if (conn->type == CONN_TYPE_DIR &&
  1109. ((DIR_CONN_IS_SERVER(conn) &&
  1110. conn->timestamp_last_write_allowed
  1111. + options->TestingDirConnectionMaxStall < now) ||
  1112. (!DIR_CONN_IS_SERVER(conn) &&
  1113. conn->timestamp_last_read_allowed
  1114. + options->TestingDirConnectionMaxStall < now))) {
  1115. log_info(LD_DIR,"Expiring wedged directory conn (fd %d, purpose %d)",
  1116. (int)conn->s, conn->purpose);
  1117. /* This check is temporary; it's to let us know whether we should consider
  1118. * parsing partial serverdesc responses. */
  1119. if (conn->purpose == DIR_PURPOSE_FETCH_SERVERDESC &&
  1120. connection_get_inbuf_len(conn) >= 1024) {
  1121. log_info(LD_DIR,"Trying to extract information from wedged server desc "
  1122. "download.");
  1123. connection_dir_reached_eof(TO_DIR_CONN(conn));
  1124. } else {
  1125. connection_mark_for_close(conn);
  1126. }
  1127. return;
  1128. }
  1129. if (!connection_speaks_cells(conn))
  1130. return; /* we're all done here, the rest is just for OR conns */
  1131. /* If we haven't flushed to an OR connection for a while, then either nuke
  1132. the connection or send a keepalive, depending. */
  1133. or_conn = TO_OR_CONN(conn);
  1134. //tor_assert(conn->outbuf);
  1135. chan = TLS_CHAN_TO_BASE(or_conn->chan);
  1136. tor_assert(chan);
  1137. if (channel_num_circuits(chan) != 0) {
  1138. have_any_circuits = 1;
  1139. chan->timestamp_last_had_circuits = now;
  1140. } else {
  1141. have_any_circuits = 0;
  1142. }
  1143. if (channel_is_bad_for_new_circs(TLS_CHAN_TO_BASE(or_conn->chan)) &&
  1144. ! have_any_circuits) {
  1145. /* It's bad for new circuits, and has no unmarked circuits on it:
  1146. * mark it now. */
  1147. log_info(LD_OR,
  1148. "Expiring non-used OR connection to fd %d (%s:%d) [Too old].",
  1149. (int)conn->s, conn->address, conn->port);
  1150. if (conn->state == OR_CONN_STATE_CONNECTING)
  1151. connection_or_connect_failed(TO_OR_CONN(conn),
  1152. END_OR_CONN_REASON_TIMEOUT,
  1153. "Tor gave up on the connection");
  1154. connection_or_close_normally(TO_OR_CONN(conn), 1);
  1155. } else if (!connection_state_is_open(conn)) {
  1156. if (past_keepalive) {
  1157. /* We never managed to actually get this connection open and happy. */
  1158. log_info(LD_OR,"Expiring non-open OR connection to fd %d (%s:%d).",
  1159. (int)conn->s,conn->address, conn->port);
  1160. connection_or_close_normally(TO_OR_CONN(conn), 0);
  1161. }
  1162. } else if (we_are_hibernating() &&
  1163. ! have_any_circuits &&
  1164. // !connection_get_outbuf_len(conn)) {
  1165. // TODO: we should have a way to make sure the safe connection has no data to send
  1166. 1) {
  1167. /* We're hibernating or shutting down, there's no circuits, and nothing to
  1168. * flush.*/
  1169. log_info(LD_OR,"Expiring non-used OR connection to fd %d (%s:%d) "
  1170. "[Hibernating or exiting].",
  1171. (int)conn->s,conn->address, conn->port);
  1172. connection_or_close_normally(TO_OR_CONN(conn), 1);
  1173. } else if (!have_any_circuits &&
  1174. now - or_conn->idle_timeout >=
  1175. chan->timestamp_last_had_circuits) {
  1176. log_info(LD_OR,"Expiring non-used OR connection %"PRIu64" to fd %d "
  1177. "(%s:%d) [no circuits for %d; timeout %d; %scanonical].",
  1178. (chan->global_identifier),
  1179. (int)conn->s, conn->address, conn->port,
  1180. (int)(now - chan->timestamp_last_had_circuits),
  1181. or_conn->idle_timeout,
  1182. or_conn->is_canonical ? "" : "non");
  1183. connection_or_close_normally(TO_OR_CONN(conn), 0);
  1184. } else if (
  1185. now >= or_conn->timestamp_lastempty + options->KeepalivePeriod*10 &&
  1186. now >=
  1187. conn->timestamp_last_write_allowed + options->KeepalivePeriod*10) {
  1188. log_fn(LOG_PROTOCOL_WARN,LD_PROTOCOL,
  1189. "Expiring stuck OR connection to fd %d (%s:%d). (%d bytes to "
  1190. "flush; %d seconds since last write)",
  1191. (int)conn->s, conn->address, conn->port,
  1192. //(int)connection_get_outbuf_len(conn),
  1193. 12345678, // TODO: do something sensible here
  1194. (int)(now-conn->timestamp_last_write_allowed));
  1195. connection_or_close_normally(TO_OR_CONN(conn), 0);
  1196. } else if (past_keepalive && 1) { //!connection_get_outbuf_len(conn)) {
  1197. // TODO: we should have a way to make sure the safe connection has no data to send
  1198. /* send a padding cell */
  1199. log_fn(LOG_DEBUG,LD_OR,"Sending keepalive to (%s:%d)",
  1200. conn->address, conn->port);
  1201. cell_t *cell = tor_malloc_zero(sizeof(cell_t));
  1202. //memset(&cell,0,sizeof(cell_t));
  1203. cell->command = CELL_PADDING;
  1204. connection_or_write_cell_to_buf(cell, or_conn);
  1205. } else {
  1206. channelpadding_decide_to_pad_channel(chan);
  1207. }
  1208. }
  1209. /** Honor a NEWNYM request: make future requests unlinkable to past
  1210. * requests. */
  1211. static void
  1212. signewnym_impl(time_t now)
  1213. {
  1214. const or_options_t *options = get_options();
  1215. if (!proxy_mode(options)) {
  1216. log_info(LD_CONTROL, "Ignoring SIGNAL NEWNYM because client functionality "
  1217. "is disabled.");
  1218. return;
  1219. }
  1220. circuit_mark_all_dirty_circs_as_unusable();
  1221. addressmap_clear_transient();
  1222. hs_client_purge_state();
  1223. time_of_last_signewnym = now;
  1224. signewnym_is_pending = 0;
  1225. ++newnym_epoch;
  1226. control_event_signal(SIGNEWNYM);
  1227. }
  1228. /** Callback: run a deferred signewnym. */
  1229. static void
  1230. handle_deferred_signewnym_cb(mainloop_event_t *event, void *arg)
  1231. {
  1232. (void)event;
  1233. (void)arg;
  1234. log_info(LD_CONTROL, "Honoring delayed NEWNYM request");
  1235. do_signewnym(time(NULL));
  1236. }
  1237. /** Either perform a signewnym or schedule one, depending on rate limiting. */
  1238. void
  1239. do_signewnym(time_t now)
  1240. {
  1241. if (time_of_last_signewnym + MAX_SIGNEWNYM_RATE > now) {
  1242. const time_t delay_sec =
  1243. time_of_last_signewnym + MAX_SIGNEWNYM_RATE - now;
  1244. if (! signewnym_is_pending) {
  1245. signewnym_is_pending = 1;
  1246. if (!handle_deferred_signewnym_ev) {
  1247. handle_deferred_signewnym_ev =
  1248. mainloop_event_postloop_new(handle_deferred_signewnym_cb, NULL);
  1249. }
  1250. const struct timeval delay_tv = { delay_sec, 0 };
  1251. mainloop_event_schedule(handle_deferred_signewnym_ev, &delay_tv);
  1252. }
  1253. log_notice(LD_CONTROL,
  1254. "Rate limiting NEWNYM request: delaying by %d second(s)",
  1255. (int)(delay_sec));
  1256. } else {
  1257. signewnym_impl(now);
  1258. }
  1259. }
  1260. /** Return the number of times that signewnym has been called. */
  1261. unsigned
  1262. get_signewnym_epoch(void)
  1263. {
  1264. return newnym_epoch;
  1265. }
  1266. /** True iff we have initialized all the members of <b>periodic_events</b>.
  1267. * Used to prevent double-initialization. */
  1268. static int periodic_events_initialized = 0;
  1269. /* Declare all the timer callback functions... */
  1270. #undef CALLBACK
  1271. #define CALLBACK(name) \
  1272. static int name ## _callback(time_t, const or_options_t *)
  1273. CALLBACK(add_entropy);
  1274. CALLBACK(check_expired_networkstatus);
  1275. CALLBACK(clean_caches);
  1276. CALLBACK(clean_consdiffmgr);
  1277. CALLBACK(fetch_networkstatus);
  1278. CALLBACK(heartbeat);
  1279. CALLBACK(hs_service);
  1280. CALLBACK(launch_descriptor_fetches);
  1281. CALLBACK(prune_old_routers);
  1282. CALLBACK(record_bridge_stats);
  1283. CALLBACK(rend_cache_failure_clean);
  1284. CALLBACK(reset_padding_counts);
  1285. CALLBACK(retry_listeners);
  1286. CALLBACK(rotate_x509_certificate);
  1287. CALLBACK(save_state);
  1288. CALLBACK(write_stats_file);
  1289. CALLBACK(control_per_second_events);
  1290. CALLBACK(second_elapsed);
  1291. #undef CALLBACK
  1292. /* Now we declare an array of periodic_event_item_t for each periodic event */
  1293. #define CALLBACK(name, r, f) \
  1294. PERIODIC_EVENT(name, PERIODIC_EVENT_ROLE_ ## r, f)
  1295. #define FL(name) (PERIODIC_EVENT_FLAG_ ## name)
  1296. STATIC periodic_event_item_t mainloop_periodic_events[] = {
  1297. /* Everyone needs to run these. They need to have very long timeouts for
  1298. * that to be safe. */
  1299. CALLBACK(add_entropy, ALL, 0),
  1300. CALLBACK(heartbeat, ALL, 0),
  1301. CALLBACK(reset_padding_counts, ALL, 0),
  1302. /* This is a legacy catch-all callback that runs once per second if
  1303. * we are online and active. */
  1304. CALLBACK(second_elapsed, NET_PARTICIPANT,
  1305. FL(RUN_ON_DISABLE)),
  1306. /* XXXX Do we have a reason to do this on a callback? Does it do any good at
  1307. * all? For now, if we're dormant, we can let our listeners decay. */
  1308. CALLBACK(retry_listeners, NET_PARTICIPANT, FL(NEED_NET)),
  1309. /* We need to do these if we're participating in the Tor network. */
  1310. CALLBACK(check_expired_networkstatus, NET_PARTICIPANT, 0),
  1311. CALLBACK(fetch_networkstatus, NET_PARTICIPANT, 0),
  1312. CALLBACK(launch_descriptor_fetches, NET_PARTICIPANT, FL(NEED_NET)),
  1313. CALLBACK(rotate_x509_certificate, NET_PARTICIPANT, 0),
  1314. CALLBACK(check_network_participation, NET_PARTICIPANT, 0),
  1315. /* We need to do these if we're participating in the Tor network, and
  1316. * immediately before we stop. */
  1317. CALLBACK(clean_caches, NET_PARTICIPANT, FL(RUN_ON_DISABLE)),
  1318. CALLBACK(save_state, NET_PARTICIPANT, FL(RUN_ON_DISABLE)),
  1319. CALLBACK(write_stats_file, NET_PARTICIPANT, FL(RUN_ON_DISABLE)),
  1320. CALLBACK(prune_old_routers, NET_PARTICIPANT, FL(RUN_ON_DISABLE)),
  1321. /* Hidden Service service only. */
  1322. CALLBACK(hs_service, HS_SERVICE, FL(NEED_NET)), // XXXX break this down more
  1323. /* Bridge only. */
  1324. CALLBACK(record_bridge_stats, BRIDGE, 0),
  1325. /* Client only. */
  1326. /* XXXX this could be restricted to CLIENT+NET_PARTICIPANT */
  1327. CALLBACK(rend_cache_failure_clean, NET_PARTICIPANT, FL(RUN_ON_DISABLE)),
  1328. /* Directory server only. */
  1329. CALLBACK(clean_consdiffmgr, DIRSERVER, 0),
  1330. /* Controller with per-second events only. */
  1331. CALLBACK(control_per_second_events, CONTROLEV, 0),
  1332. END_OF_PERIODIC_EVENTS
  1333. };
  1334. #undef CALLBACK
  1335. #undef FL
  1336. /* These are pointers to members of periodic_events[] that are used to
  1337. * implement particular callbacks. We keep them separate here so that we
  1338. * can access them by name. We also keep them inside periodic_events[]
  1339. * so that we can implement "reset all timers" in a reasonable way. */
  1340. static periodic_event_item_t *fetch_networkstatus_event=NULL;
  1341. static periodic_event_item_t *launch_descriptor_fetches_event=NULL;
  1342. static periodic_event_item_t *check_dns_honesty_event=NULL;
  1343. static periodic_event_item_t *save_state_event=NULL;
  1344. static periodic_event_item_t *prune_old_routers_event=NULL;
  1345. /** Reset all the periodic events so we'll do all our actions again as if we
  1346. * just started up.
  1347. * Useful if our clock just moved back a long time from the future,
  1348. * so we don't wait until that future arrives again before acting.
  1349. */
  1350. void
  1351. reset_all_main_loop_timers(void)
  1352. {
  1353. periodic_events_reset_all();
  1354. }
  1355. /** Return a bitmask of the roles this tor instance is configured for using
  1356. * the given options. */
  1357. STATIC int
  1358. get_my_roles(const or_options_t *options)
  1359. {
  1360. tor_assert(options);
  1361. int roles = PERIODIC_EVENT_ROLE_ALL;
  1362. int is_bridge = options->BridgeRelay;
  1363. int is_relay = server_mode(options);
  1364. int is_dirauth = authdir_mode_v3(options);
  1365. int is_bridgeauth = authdir_mode_bridge(options);
  1366. int is_hidden_service = !!hs_service_get_num_services() ||
  1367. !!rend_num_services();
  1368. int is_dirserver = dir_server_mode(options);
  1369. int sending_control_events = control_any_per_second_event_enabled();
  1370. /* We also consider tor to have the role of a client if the ControlPort is
  1371. * set because a lot of things can be done over the control port which
  1372. * requires tor to have basic functionnalities. */
  1373. int is_client = options_any_client_port_set(options) ||
  1374. options->ControlPort_set ||
  1375. options->OwningControllerFD != UINT64_MAX;
  1376. int is_net_participant = is_participating_on_network() ||
  1377. is_relay || is_hidden_service;
  1378. if (is_bridge) roles |= PERIODIC_EVENT_ROLE_BRIDGE;
  1379. if (is_client) roles |= PERIODIC_EVENT_ROLE_CLIENT;
  1380. if (is_relay) roles |= PERIODIC_EVENT_ROLE_RELAY;
  1381. if (is_dirauth) roles |= PERIODIC_EVENT_ROLE_DIRAUTH;
  1382. if (is_bridgeauth) roles |= PERIODIC_EVENT_ROLE_BRIDGEAUTH;
  1383. if (is_hidden_service) roles |= PERIODIC_EVENT_ROLE_HS_SERVICE;
  1384. if (is_dirserver) roles |= PERIODIC_EVENT_ROLE_DIRSERVER;
  1385. if (is_net_participant) roles |= PERIODIC_EVENT_ROLE_NET_PARTICIPANT;
  1386. if (sending_control_events) roles |= PERIODIC_EVENT_ROLE_CONTROLEV;
  1387. return roles;
  1388. }
  1389. /** Event to run initialize_periodic_events_cb */
  1390. static struct event *initialize_periodic_events_event = NULL;
  1391. /** Helper, run one second after setup:
  1392. * Initializes all members of periodic_events and starts them running.
  1393. *
  1394. * (We do this one second after setup for backward-compatibility reasons;
  1395. * it might not actually be necessary.) */
  1396. static void
  1397. initialize_periodic_events_cb(evutil_socket_t fd, short events, void *data)
  1398. {
  1399. (void) fd;
  1400. (void) events;
  1401. (void) data;
  1402. tor_event_free(initialize_periodic_events_event);
  1403. rescan_periodic_events(get_options());
  1404. }
  1405. /** Set up all the members of mainloop_periodic_events[], and configure them
  1406. * all to be launched from a callback. */
  1407. void
  1408. initialize_periodic_events(void)
  1409. {
  1410. if (periodic_events_initialized)
  1411. return;
  1412. periodic_events_initialized = 1;
  1413. for (int i = 0; mainloop_periodic_events[i].name; ++i) {
  1414. periodic_events_register(&mainloop_periodic_events[i]);
  1415. }
  1416. /* Set up all periodic events. We'll launch them by roles. */
  1417. #define NAMED_CALLBACK(name) \
  1418. STMT_BEGIN name ## _event = periodic_events_find( #name ); STMT_END
  1419. NAMED_CALLBACK(prune_old_routers);
  1420. NAMED_CALLBACK(fetch_networkstatus);
  1421. NAMED_CALLBACK(launch_descriptor_fetches);
  1422. NAMED_CALLBACK(check_dns_honesty);
  1423. NAMED_CALLBACK(save_state);
  1424. }
  1425. STATIC void
  1426. teardown_periodic_events(void)
  1427. {
  1428. periodic_events_disconnect_all();
  1429. fetch_networkstatus_event = NULL;
  1430. launch_descriptor_fetches_event = NULL;
  1431. check_dns_honesty_event = NULL;
  1432. save_state_event = NULL;
  1433. prune_old_routers_event = NULL;
  1434. periodic_events_initialized = 0;
  1435. }
  1436. static mainloop_event_t *rescan_periodic_events_ev = NULL;
  1437. /** Callback: rescan the periodic event list. */
  1438. static void
  1439. rescan_periodic_events_cb(mainloop_event_t *event, void *arg)
  1440. {
  1441. (void)event;
  1442. (void)arg;
  1443. rescan_periodic_events(get_options());
  1444. }
  1445. /**
  1446. * Schedule an event that will rescan which periodic events should run.
  1447. **/
  1448. MOCK_IMPL(void,
  1449. schedule_rescan_periodic_events,(void))
  1450. {
  1451. if (!rescan_periodic_events_ev) {
  1452. rescan_periodic_events_ev =
  1453. mainloop_event_new(rescan_periodic_events_cb, NULL);
  1454. }
  1455. mainloop_event_activate(rescan_periodic_events_ev);
  1456. }
  1457. /** Do a pass at all our periodic events, disable those we don't need anymore
  1458. * and enable those we need now using the given options. */
  1459. void
  1460. rescan_periodic_events(const or_options_t *options)
  1461. {
  1462. tor_assert(options);
  1463. periodic_events_rescan_by_roles(get_my_roles(options), net_is_disabled());
  1464. }
  1465. /* We just got new options globally set, see if we need to enabled or disable
  1466. * periodic events. */
  1467. void
  1468. periodic_events_on_new_options(const or_options_t *options)
  1469. {
  1470. rescan_periodic_events(options);
  1471. }
  1472. /**
  1473. * Update our schedule so that we'll check whether we need to fetch directory
  1474. * info immediately.
  1475. */
  1476. void
  1477. reschedule_directory_downloads(void)
  1478. {
  1479. tor_assert(fetch_networkstatus_event);
  1480. tor_assert(launch_descriptor_fetches_event);
  1481. periodic_event_reschedule(fetch_networkstatus_event);
  1482. periodic_event_reschedule(launch_descriptor_fetches_event);
  1483. }
  1484. /** Mainloop callback: clean up circuits, channels, and connections
  1485. * that are pending close. */
  1486. static void
  1487. postloop_cleanup_cb(mainloop_event_t *ev, void *arg)
  1488. {
  1489. (void)ev;
  1490. (void)arg;
  1491. circuit_close_all_marked();
  1492. close_closeable_connections();
  1493. channel_run_cleanup();
  1494. channel_listener_run_cleanup();
  1495. }
  1496. /** Event to run postloop_cleanup_cb */
  1497. static mainloop_event_t *postloop_cleanup_ev=NULL;
  1498. /** Schedule a post-loop event to clean up marked channels, connections, and
  1499. * circuits. */
  1500. void
  1501. mainloop_schedule_postloop_cleanup(void)
  1502. {
  1503. if (PREDICT_UNLIKELY(postloop_cleanup_ev == NULL)) {
  1504. // (It's possible that we can get here if we decide to close a connection
  1505. // in the earliest stages of our configuration, before we create events.)
  1506. return;
  1507. }
  1508. mainloop_event_activate(postloop_cleanup_ev);
  1509. }
  1510. /** Event to run 'scheduled_shutdown_cb' */
  1511. static mainloop_event_t *scheduled_shutdown_ev=NULL;
  1512. /** Callback: run a scheduled shutdown */
  1513. static void
  1514. scheduled_shutdown_cb(mainloop_event_t *ev, void *arg)
  1515. {
  1516. (void)ev;
  1517. (void)arg;
  1518. log_notice(LD_GENERAL, "Clean shutdown finished. Exiting.");
  1519. tor_shutdown_event_loop_and_exit(0);
  1520. }
  1521. /** Schedule the mainloop to exit after <b>delay_sec</b> seconds. */
  1522. void
  1523. mainloop_schedule_shutdown(int delay_sec)
  1524. {
  1525. const struct timeval delay_tv = { delay_sec, 0 };
  1526. if (! scheduled_shutdown_ev) {
  1527. scheduled_shutdown_ev = mainloop_event_new(scheduled_shutdown_cb, NULL);
  1528. }
  1529. mainloop_event_schedule(scheduled_shutdown_ev, &delay_tv);
  1530. }
  1531. /** Perform regular maintenance tasks. This function gets run once per
  1532. * second.
  1533. */
  1534. static int
  1535. second_elapsed_callback(time_t now, const or_options_t *options)
  1536. {
  1537. /* 0. See if our bandwidth limits are exhausted and we should hibernate
  1538. *
  1539. * Note: we have redundant mechanisms to handle the case where it's
  1540. * time to wake up from hibernation; or where we have a scheduled
  1541. * shutdown and it's time to run it, but this will also handle those.
  1542. */
  1543. consider_hibernation(now);
  1544. /* Maybe enough time elapsed for us to reconsider a circuit. */
  1545. circuit_upgrade_circuits_from_guard_wait();
  1546. if (options->UseBridges && !net_is_disabled()) {
  1547. /* Note: this check uses net_is_disabled(), not should_delay_dir_fetches()
  1548. * -- the latter is only for fetching consensus-derived directory info. */
  1549. // TODO: client
  1550. // Also, schedule this rather than probing 1x / sec
  1551. fetch_bridge_descriptors(options, now);
  1552. }
  1553. if (accounting_is_enabled(options)) {
  1554. // TODO: refactor or rewrite?
  1555. accounting_run_housekeeping(now);
  1556. }
  1557. /* 3a. Every second, we examine pending circuits and prune the
  1558. * ones which have been pending for more than a few seconds.
  1559. * We do this before step 4, so it can try building more if
  1560. * it's not comfortable with the number of available circuits.
  1561. */
  1562. /* (If our circuit build timeout can ever become lower than a second (which
  1563. * it can't, currently), we should do this more often.) */
  1564. // TODO: All expire stuff can become NET_PARTICIPANT, RUN_ON_DISABLE
  1565. circuit_expire_building();
  1566. circuit_expire_waiting_for_better_guard();
  1567. /* 3b. Also look at pending streams and prune the ones that 'began'
  1568. * a long time ago but haven't gotten a 'connected' yet.
  1569. * Do this before step 4, so we can put them back into pending
  1570. * state to be picked up by the new circuit.
  1571. */
  1572. connection_ap_expire_beginning();
  1573. /* 3c. And expire connections that we've held open for too long.
  1574. */
  1575. connection_expire_held_open();
  1576. /* 4. Every second, we try a new circuit if there are no valid
  1577. * circuits. Every NewCircuitPeriod seconds, we expire circuits
  1578. * that became dirty more than MaxCircuitDirtiness seconds ago,
  1579. * and we make a new circ if there are no clean circuits.
  1580. */
  1581. const int have_dir_info = router_have_minimum_dir_info();
  1582. if (have_dir_info && !net_is_disabled()) {
  1583. circuit_build_needed_circs(now);
  1584. } else {
  1585. circuit_expire_old_circs_as_needed(now);
  1586. }
  1587. /* 5. We do housekeeping for each connection... */
  1588. channel_update_bad_for_new_circs(NULL, 0);
  1589. int i;
  1590. for (i=0;i<smartlist_len(connection_array);i++) {
  1591. run_connection_housekeeping(i, now);
  1592. }
  1593. /* Run again in a second. */
  1594. return 1;
  1595. }
  1596. /**
  1597. * Periodic callback: Every {LAZY,GREEDY}_DESCRIPTOR_RETRY_INTERVAL,
  1598. * see about fetching descriptors, microdescriptors, and extrainfo
  1599. * documents.
  1600. */
  1601. static int
  1602. launch_descriptor_fetches_callback(time_t now, const or_options_t *options)
  1603. {
  1604. if (should_delay_dir_fetches(options, NULL))
  1605. return PERIODIC_EVENT_NO_UPDATE;
  1606. update_all_descriptor_downloads(now);
  1607. update_extrainfo_downloads(now);
  1608. if (router_have_minimum_dir_info())
  1609. return LAZY_DESCRIPTOR_RETRY_INTERVAL;
  1610. else
  1611. return GREEDY_DESCRIPTOR_RETRY_INTERVAL;
  1612. }
  1613. /**
  1614. * Periodic event: Rotate our X.509 certificates and TLS keys once every
  1615. * MAX_SSL_KEY_LIFETIME_INTERNAL.
  1616. */
  1617. static int
  1618. rotate_x509_certificate_callback(time_t now, const or_options_t *options)
  1619. {
  1620. static int first = 1;
  1621. (void)now;
  1622. (void)options;
  1623. if (first) {
  1624. first = 0;
  1625. return MAX_SSL_KEY_LIFETIME_INTERNAL;
  1626. }
  1627. /* 1b. Every MAX_SSL_KEY_LIFETIME_INTERNAL seconds, we change our
  1628. * TLS context. */
  1629. log_info(LD_GENERAL,"Rotating tls context.");
  1630. if (router_initialize_tls_context() < 0) {
  1631. log_err(LD_BUG, "Error reinitializing TLS context");
  1632. tor_assert_unreached();
  1633. }
  1634. if (generate_ed_link_cert(options, now, 1)) {
  1635. log_err(LD_OR, "Unable to update Ed25519->TLS link certificate for "
  1636. "new TLS context.");
  1637. tor_assert_unreached();
  1638. }
  1639. /* We also make sure to rotate the TLS connections themselves if they've
  1640. * been up for too long -- but that's done via is_bad_for_new_circs in
  1641. * run_connection_housekeeping() above. */
  1642. return MAX_SSL_KEY_LIFETIME_INTERNAL;
  1643. }
  1644. /**
  1645. * Periodic callback: once an hour, grab some more entropy from the
  1646. * kernel and feed it to our CSPRNG.
  1647. **/
  1648. static int
  1649. add_entropy_callback(time_t now, const or_options_t *options)
  1650. {
  1651. (void)now;
  1652. (void)options;
  1653. /* We already seeded once, so don't die on failure. */
  1654. if (crypto_seed_rng() < 0) {
  1655. log_warn(LD_GENERAL, "Tried to re-seed RNG, but failed. We already "
  1656. "seeded once, though, so we won't exit here.");
  1657. }
  1658. /** How often do we add more entropy to OpenSSL's RNG pool? */
  1659. #define ENTROPY_INTERVAL (60*60)
  1660. return ENTROPY_INTERVAL;
  1661. }
  1662. /** Periodic callback: if there has been no network usage in a while,
  1663. * enter a dormant state. */
  1664. STATIC int
  1665. check_network_participation_callback(time_t now, const or_options_t *options)
  1666. {
  1667. /* If we're a server, we can't become dormant. */
  1668. if (server_mode(options)) {
  1669. goto found_activity;
  1670. }
  1671. /* If we're running an onion service, we can't become dormant. */
  1672. /* XXXX this would be nice to change, so that we can be dormant with a
  1673. * service. */
  1674. if (hs_service_get_num_services() || rend_num_services()) {
  1675. goto found_activity;
  1676. }
  1677. /* If we have any currently open entry streams other than "linked"
  1678. * connections used for directory requests, those count as user activity.
  1679. */
  1680. if (options->DormantTimeoutDisabledByIdleStreams) {
  1681. if (connection_get_by_type_nonlinked(CONN_TYPE_AP) != NULL) {
  1682. goto found_activity;
  1683. }
  1684. }
  1685. /* XXXX Make this configurable? */
  1686. /** How often do we check whether we have had network activity? */
  1687. #define CHECK_PARTICIPATION_INTERVAL (5*60)
  1688. /* Become dormant if there has been no user activity in a long time.
  1689. * (The funny checks below are in order to prevent overflow.) */
  1690. time_t time_since_last_activity = 0;
  1691. if (get_last_user_activity_time() < now)
  1692. time_since_last_activity = now - get_last_user_activity_time();
  1693. if (time_since_last_activity >= options->DormantClientTimeout) {
  1694. log_notice(LD_GENERAL, "No user activity in a long time: becoming"
  1695. " dormant.");
  1696. set_network_participation(false);
  1697. rescan_periodic_events(options);
  1698. }
  1699. return CHECK_PARTICIPATION_INTERVAL;
  1700. found_activity:
  1701. note_user_activity(now);
  1702. return CHECK_PARTICIPATION_INTERVAL;
  1703. }
  1704. /**
  1705. * Periodic callback: If our consensus is too old, recalculate whether
  1706. * we can actually use it.
  1707. */
  1708. static int
  1709. check_expired_networkstatus_callback(time_t now, const or_options_t *options)
  1710. {
  1711. (void)options;
  1712. /* Check whether our networkstatus has expired. */
  1713. networkstatus_t *ns = networkstatus_get_latest_consensus();
  1714. /* Use reasonably live consensuses until they are no longer reasonably live.
  1715. */
  1716. if (ns && !networkstatus_consensus_reasonably_live(ns, now) &&
  1717. router_have_minimum_dir_info()) {
  1718. router_dir_info_changed();
  1719. }
  1720. #define CHECK_EXPIRED_NS_INTERVAL (2*60)
  1721. return CHECK_EXPIRED_NS_INTERVAL;
  1722. }
  1723. /**
  1724. * Scheduled callback: Save the state file to disk if appropriate.
  1725. */
  1726. static int
  1727. save_state_callback(time_t now, const or_options_t *options)
  1728. {
  1729. (void) options;
  1730. (void) or_state_save(now); // only saves if appropriate
  1731. const time_t next_write = get_or_state()->next_write;
  1732. if (next_write == TIME_MAX) {
  1733. return 86400;
  1734. }
  1735. return safe_timer_diff(now, next_write);
  1736. }
  1737. /** Reschedule the event for saving the state file.
  1738. *
  1739. * Run this when the state becomes dirty. */
  1740. void
  1741. reschedule_or_state_save(void)
  1742. {
  1743. if (save_state_event == NULL) {
  1744. /* This can happen early on during startup. */
  1745. return;
  1746. }
  1747. periodic_event_reschedule(save_state_event);
  1748. }
  1749. /**
  1750. * Periodic callback: Write statistics to disk if appropriate.
  1751. */
  1752. static int
  1753. write_stats_file_callback(time_t now, const or_options_t *options)
  1754. {
  1755. /* 1g. Check whether we should write statistics to disk.
  1756. */
  1757. #define CHECK_WRITE_STATS_INTERVAL (60*60)
  1758. time_t next_time_to_write_stats_files = now + CHECK_WRITE_STATS_INTERVAL;
  1759. if (options->CellStatistics) {
  1760. time_t next_write =
  1761. rep_hist_buffer_stats_write(now);
  1762. if (next_write && next_write < next_time_to_write_stats_files)
  1763. next_time_to_write_stats_files = next_write;
  1764. }
  1765. if (options->DirReqStatistics) {
  1766. time_t next_write = geoip_dirreq_stats_write(now);
  1767. if (next_write && next_write < next_time_to_write_stats_files)
  1768. next_time_to_write_stats_files = next_write;
  1769. }
  1770. if (options->EntryStatistics) {
  1771. time_t next_write = geoip_entry_stats_write(now);
  1772. if (next_write && next_write < next_time_to_write_stats_files)
  1773. next_time_to_write_stats_files = next_write;
  1774. }
  1775. if (options->HiddenServiceStatistics) {
  1776. time_t next_write = rep_hist_hs_stats_write(now);
  1777. if (next_write && next_write < next_time_to_write_stats_files)
  1778. next_time_to_write_stats_files = next_write;
  1779. }
  1780. if (options->ExitPortStatistics) {
  1781. time_t next_write = rep_hist_exit_stats_write(now);
  1782. if (next_write && next_write < next_time_to_write_stats_files)
  1783. next_time_to_write_stats_files = next_write;
  1784. }
  1785. if (options->ConnDirectionStatistics) {
  1786. time_t next_write = rep_hist_conn_stats_write(now);
  1787. if (next_write && next_write < next_time_to_write_stats_files)
  1788. next_time_to_write_stats_files = next_write;
  1789. }
  1790. if (options->BridgeAuthoritativeDir) {
  1791. time_t next_write = rep_hist_desc_stats_write(now);
  1792. if (next_write && next_write < next_time_to_write_stats_files)
  1793. next_time_to_write_stats_files = next_write;
  1794. }
  1795. return safe_timer_diff(now, next_time_to_write_stats_files);
  1796. }
  1797. static int
  1798. reset_padding_counts_callback(time_t now, const or_options_t *options)
  1799. {
  1800. if (options->PaddingStatistics) {
  1801. rep_hist_prep_published_padding_counts(now);
  1802. }
  1803. rep_hist_reset_padding_counts();
  1804. return REPHIST_CELL_PADDING_COUNTS_INTERVAL;
  1805. }
  1806. static int should_init_bridge_stats = 1;
  1807. /**
  1808. * Periodic callback: Write bridge statistics to disk if appropriate.
  1809. */
  1810. static int
  1811. record_bridge_stats_callback(time_t now, const or_options_t *options)
  1812. {
  1813. /* 1h. Check whether we should write bridge statistics to disk.
  1814. */
  1815. if (should_record_bridge_info(options)) {
  1816. if (should_init_bridge_stats) {
  1817. /* (Re-)initialize bridge statistics. */
  1818. geoip_bridge_stats_init(now);
  1819. should_init_bridge_stats = 0;
  1820. return WRITE_STATS_INTERVAL;
  1821. } else {
  1822. /* Possibly write bridge statistics to disk and ask when to write
  1823. * them next time. */
  1824. time_t next = geoip_bridge_stats_write(now);
  1825. return safe_timer_diff(now, next);
  1826. }
  1827. } else if (!should_init_bridge_stats) {
  1828. /* Bridge mode was turned off. Ensure that stats are re-initialized
  1829. * next time bridge mode is turned on. */
  1830. should_init_bridge_stats = 1;
  1831. }
  1832. return PERIODIC_EVENT_NO_UPDATE;
  1833. }
  1834. /**
  1835. * Periodic callback: Clean in-memory caches every once in a while
  1836. */
  1837. static int
  1838. clean_caches_callback(time_t now, const or_options_t *options)
  1839. {
  1840. /* Remove old information from rephist and the rend cache. */
  1841. rep_history_clean(now - options->RephistTrackTime);
  1842. rend_cache_clean(now, REND_CACHE_TYPE_SERVICE);
  1843. hs_cache_clean_as_client(now);
  1844. hs_cache_clean_as_dir(now);
  1845. microdesc_cache_rebuild(NULL, 0);
  1846. #define CLEAN_CACHES_INTERVAL (30*60)
  1847. return CLEAN_CACHES_INTERVAL;
  1848. }
  1849. /**
  1850. * Periodic callback: Clean the cache of failed hidden service lookups
  1851. * frequently.
  1852. */
  1853. static int
  1854. rend_cache_failure_clean_callback(time_t now, const or_options_t *options)
  1855. {
  1856. (void)options;
  1857. /* We don't keep entries that are more than five minutes old so we try to
  1858. * clean it as soon as we can since we want to make sure the client waits
  1859. * as little as possible for reachability reasons. */
  1860. rend_cache_failure_clean(now);
  1861. hs_cache_client_intro_state_clean(now);
  1862. return 30;
  1863. }
  1864. /**
  1865. * Periodic callback: prune routerlist of old information about Tor network.
  1866. */
  1867. static int
  1868. prune_old_routers_callback(time_t now, const or_options_t *options)
  1869. {
  1870. #define ROUTERLIST_PRUNING_INTERVAL (60*60) // 1 hour.
  1871. (void)now;
  1872. (void)options;
  1873. if (!net_is_disabled()) {
  1874. /* If any networkstatus documents are no longer recent, we need to
  1875. * update all the descriptors' running status. */
  1876. /* Remove dead routers. */
  1877. log_debug(LD_GENERAL, "Pruning routerlist...");
  1878. routerlist_remove_old_routers();
  1879. }
  1880. return ROUTERLIST_PRUNING_INTERVAL;
  1881. }
  1882. /**
  1883. * Periodic event: once a minute, (or every second if TestingTorNetwork, or
  1884. * during client bootstrap), check whether we want to download any
  1885. * networkstatus documents. */
  1886. static int
  1887. fetch_networkstatus_callback(time_t now, const or_options_t *options)
  1888. {
  1889. /* How often do we check whether we should download network status
  1890. * documents? */
  1891. const int we_are_bootstrapping = networkstatus_consensus_is_bootstrapping(
  1892. now);
  1893. const int prefer_mirrors = !directory_fetches_from_authorities(
  1894. get_options());
  1895. int networkstatus_dl_check_interval = 60;
  1896. /* check more often when testing, or when bootstrapping from mirrors
  1897. * (connection limits prevent too many connections being made) */
  1898. if (options->TestingTorNetwork
  1899. || (we_are_bootstrapping && prefer_mirrors)) {
  1900. networkstatus_dl_check_interval = 1;
  1901. }
  1902. if (should_delay_dir_fetches(options, NULL))
  1903. return PERIODIC_EVENT_NO_UPDATE;
  1904. update_networkstatus_downloads(now);
  1905. return networkstatus_dl_check_interval;
  1906. }
  1907. /**
  1908. * Periodic callback: Every 60 seconds, we relaunch listeners if any died. */
  1909. static int
  1910. retry_listeners_callback(time_t now, const or_options_t *options)
  1911. {
  1912. (void)now;
  1913. (void)options;
  1914. if (!net_is_disabled()) {
  1915. retry_all_listeners(NULL, 0);
  1916. return 60;
  1917. }
  1918. return PERIODIC_EVENT_NO_UPDATE;
  1919. }
  1920. static int heartbeat_callback_first_time = 1;
  1921. /**
  1922. * Periodic callback: write the heartbeat message in the logs.
  1923. *
  1924. * If writing the heartbeat message to the logs fails for some reason, retry
  1925. * again after <b>MIN_HEARTBEAT_PERIOD</b> seconds.
  1926. */
  1927. static int
  1928. heartbeat_callback(time_t now, const or_options_t *options)
  1929. {
  1930. /* Check if heartbeat is disabled */
  1931. if (!options->HeartbeatPeriod) {
  1932. return PERIODIC_EVENT_NO_UPDATE;
  1933. }
  1934. /* Skip the first one. */
  1935. if (heartbeat_callback_first_time) {
  1936. heartbeat_callback_first_time = 0;
  1937. return options->HeartbeatPeriod;
  1938. }
  1939. /* Write the heartbeat message */
  1940. if (log_heartbeat(now) == 0) {
  1941. return options->HeartbeatPeriod;
  1942. } else {
  1943. /* If we couldn't write the heartbeat log message, try again in the minimum
  1944. * interval of time. */
  1945. return MIN_HEARTBEAT_PERIOD;
  1946. }
  1947. }
  1948. #define CDM_CLEAN_CALLBACK_INTERVAL 600
  1949. static int
  1950. clean_consdiffmgr_callback(time_t now, const or_options_t *options)
  1951. {
  1952. (void)now;
  1953. if (dir_server_mode(options)) {
  1954. consdiffmgr_cleanup();
  1955. }
  1956. return CDM_CLEAN_CALLBACK_INTERVAL;
  1957. }
  1958. /*
  1959. * Periodic callback: Run scheduled events for HS service. This is called
  1960. * every second.
  1961. */
  1962. static int
  1963. hs_service_callback(time_t now, const or_options_t *options)
  1964. {
  1965. (void) options;
  1966. /* We need to at least be able to build circuits and that we actually have
  1967. * a working network. */
  1968. if (!have_completed_a_circuit() || net_is_disabled() ||
  1969. networkstatus_get_live_consensus(now) == NULL) {
  1970. goto end;
  1971. }
  1972. hs_service_run_scheduled_events(now);
  1973. end:
  1974. /* Every 1 second. */
  1975. return 1;
  1976. }
  1977. /*
  1978. * Periodic callback: Send once-per-second events to the controller(s).
  1979. * This is called every second.
  1980. */
  1981. static int
  1982. control_per_second_events_callback(time_t now, const or_options_t *options)
  1983. {
  1984. (void) options;
  1985. (void) now;
  1986. control_per_second_events();
  1987. return 1;
  1988. }
  1989. /** Last time that update_current_time was called. */
  1990. static time_t current_second = 0;
  1991. /** Last time that update_current_time updated current_second. */
  1992. static monotime_coarse_t current_second_last_changed;
  1993. /**
  1994. * Set the current time to "now", which should be the value returned by
  1995. * time(). Check for clock jumps and track the total number of seconds we
  1996. * have been running.
  1997. */
  1998. void
  1999. update_current_time(time_t now)
  2000. {
  2001. if (PREDICT_LIKELY(now == current_second)) {
  2002. /* We call this function a lot. Most frequently, the current second
  2003. * will not have changed, so we just return. */
  2004. return;
  2005. }
  2006. const time_t seconds_elapsed = current_second ? (now - current_second) : 0;
  2007. /* Check the wall clock against the monotonic clock, so we can
  2008. * better tell idleness from clock jumps and/or other shenanigans. */
  2009. monotime_coarse_t last_updated;
  2010. memcpy(&last_updated, &current_second_last_changed, sizeof(last_updated));
  2011. monotime_coarse_get(&current_second_last_changed);
  2012. /** How much clock jumping means that we should adjust our idea of when
  2013. * to go dormant? */
  2014. #define NUM_JUMPED_SECONDS_BEFORE_NETSTATUS_UPDATE 20
  2015. /* Don't go dormant early or late just because we jumped in time. */
  2016. if (ABS(seconds_elapsed) >= NUM_JUMPED_SECONDS_BEFORE_NETSTATUS_UPDATE) {
  2017. if (is_participating_on_network()) {
  2018. netstatus_note_clock_jumped(seconds_elapsed);
  2019. }
  2020. }
  2021. /** How much clock jumping do we tolerate? */
  2022. #define NUM_JUMPED_SECONDS_BEFORE_WARN 100
  2023. /** How much idleness do we tolerate? */
  2024. #define NUM_IDLE_SECONDS_BEFORE_WARN 3600
  2025. if (seconds_elapsed < -NUM_JUMPED_SECONDS_BEFORE_WARN) {
  2026. // moving back in time is always a bad sign.
  2027. circuit_note_clock_jumped(seconds_elapsed, false);
  2028. } else if (seconds_elapsed >= NUM_JUMPED_SECONDS_BEFORE_WARN) {
  2029. /* Compare the monotonic clock to the result of time(). */
  2030. const int32_t monotime_msec_passed =
  2031. monotime_coarse_diff_msec32(&last_updated,
  2032. &current_second_last_changed);
  2033. const int monotime_sec_passed = monotime_msec_passed / 1000;
  2034. const int discrepancy = monotime_sec_passed - (int)seconds_elapsed;
  2035. /* If the monotonic clock deviates from time(NULL), we have a couple of
  2036. * possibilities. On some systems, this means we have been suspended or
  2037. * sleeping. Everywhere, it can mean that the wall-clock time has
  2038. * been changed -- for example, with settimeofday().
  2039. *
  2040. * On the other hand, if the monotonic time matches with the wall-clock
  2041. * time, we've probably just been idle for a while, with no events firing.
  2042. * we tolerate much more of that.
  2043. */
  2044. const bool clock_jumped = abs(discrepancy) > 2;
  2045. if (clock_jumped || seconds_elapsed >= NUM_IDLE_SECONDS_BEFORE_WARN) {
  2046. circuit_note_clock_jumped(seconds_elapsed, ! clock_jumped);
  2047. }
  2048. } else if (seconds_elapsed > 0) {
  2049. stats_n_seconds_working += seconds_elapsed;
  2050. }
  2051. update_approx_time(now);
  2052. current_second = now;
  2053. }
  2054. #ifdef HAVE_SYSTEMD_209
  2055. static periodic_timer_t *systemd_watchdog_timer = NULL;
  2056. /** Libevent callback: invoked to reset systemd watchdog. */
  2057. static void
  2058. systemd_watchdog_callback(periodic_timer_t *timer, void *arg)
  2059. {
  2060. (void)timer;
  2061. (void)arg;
  2062. sd_notify(0, "WATCHDOG=1");
  2063. }
  2064. #endif /* defined(HAVE_SYSTEMD_209) */
  2065. #define UPTIME_CUTOFF_FOR_NEW_BANDWIDTH_TEST (6*60*60)
  2066. /** Called when our IP address seems to have changed. <b>at_interface</b>
  2067. * should be true if we detected a change in our interface, and false if we
  2068. * detected a change in our published address. */
  2069. void
  2070. ip_address_changed(int at_interface)
  2071. {
  2072. const or_options_t *options = get_options();
  2073. int server = server_mode(options);
  2074. int exit_reject_interfaces = (server && options->ExitRelay
  2075. && options->ExitPolicyRejectLocalInterfaces);
  2076. if (at_interface) {
  2077. if (! server) {
  2078. /* Okay, change our keys. */
  2079. if (init_keys_client() < 0)
  2080. log_warn(LD_GENERAL, "Unable to rotate keys after IP change!");
  2081. }
  2082. } else {
  2083. if (server) {
  2084. if (get_uptime() > UPTIME_CUTOFF_FOR_NEW_BANDWIDTH_TEST)
  2085. reset_bandwidth_test();
  2086. reset_uptime();
  2087. router_reset_reachability();
  2088. }
  2089. }
  2090. /* Exit relays incorporate interface addresses in their exit policies when
  2091. * ExitPolicyRejectLocalInterfaces is set */
  2092. if (exit_reject_interfaces || (server && !at_interface)) {
  2093. mark_my_descriptor_dirty("IP address changed");
  2094. }
  2095. dns_servers_relaunch_checks();
  2096. }
  2097. /** Forget what we've learned about the correctness of our DNS servers, and
  2098. * start learning again. */
  2099. void
  2100. dns_servers_relaunch_checks(void)
  2101. {
  2102. if (server_mode(get_options())) {
  2103. dns_reset_correctness_checks();
  2104. if (check_dns_honesty_event) {
  2105. periodic_event_reschedule(check_dns_honesty_event);
  2106. }
  2107. }
  2108. }
  2109. /** Initialize some mainloop_event_t objects that we require. */
  2110. void
  2111. initialize_mainloop_events(void)
  2112. {
  2113. if (!schedule_active_linked_connections_event) {
  2114. schedule_active_linked_connections_event =
  2115. mainloop_event_postloop_new(schedule_active_linked_connections_cb, NULL);
  2116. }
  2117. if (!postloop_cleanup_ev) {
  2118. postloop_cleanup_ev =
  2119. mainloop_event_postloop_new(postloop_cleanup_cb, NULL);
  2120. }
  2121. }
  2122. static void
  2123. init_event_registry(void)
  2124. {
  2125. tor_assert(event_registry == NULL);
  2126. event_registry = event_registry_new();
  2127. safe_or_conn_register_events(event_registry);
  2128. or_conn_register_events(event_registry);
  2129. }
  2130. static void
  2131. tor_eventloop_thread(void)
  2132. {
  2133. log_debug(LD_GENERAL, "Starting eventloop thread.");
  2134. struct event_base *base = tor_libevent_get_base();
  2135. local_replyqueue_init(base);
  2136. init_thread_throughput_logging(get_local_eventloop_index());
  2137. int loop_result = 0;
  2138. while (loop_result == 0 && other_eventloops_should_exit == 0) {
  2139. loop_result = event_base_loop(base, EVLOOP_NO_EXIT_ON_EMPTY);
  2140. }
  2141. log_debug(LD_GENERAL, "Done eventloop thread.");
  2142. destroy_thread_throughput_logging();
  2143. if (other_eventloops_should_exit == 0) {
  2144. log_err(LD_GENERAL, "Eventloop thread stopped unexpectedly. (loop_result:%d, \
  2145. other_eventloops_should_exit:%d)",
  2146. loop_result, other_eventloops_should_exit);
  2147. }
  2148. }
  2149. /** Tor main loop. */
  2150. int
  2151. do_main_loop(void)
  2152. {
  2153. /* initialize the periodic events first, so that code that depends on the
  2154. * events being present does not assert.
  2155. */
  2156. tor_assert(periodic_events_initialized);
  2157. initialize_mainloop_events();
  2158. init_event_registry();
  2159. periodic_events_connect_all();
  2160. bool logging_throughput = (get_options()->ThroughputLogFile != NULL &&
  2161. strlen(get_options()->ThroughputLogFile) != 0);
  2162. if (logging_throughput) {
  2163. init_throughput_logging(get_num_eventloops());
  2164. init_thread_throughput_logging(0);
  2165. }
  2166. struct timeval one_second = { 1, 0 };
  2167. initialize_periodic_events_event = tor_evtimer_new(
  2168. tor_libevent_get_base(),
  2169. initialize_periodic_events_cb, NULL);
  2170. event_add(initialize_periodic_events_event, &one_second);
  2171. #ifdef HAVE_SYSTEMD_209
  2172. uint64_t watchdog_delay;
  2173. /* set up systemd watchdog notification. */
  2174. if (sd_watchdog_enabled(1, &watchdog_delay) > 0) {
  2175. if (! systemd_watchdog_timer) {
  2176. struct timeval watchdog;
  2177. /* The manager will "act on" us if we don't send them a notification
  2178. * every 'watchdog_delay' microseconds. So, send notifications twice
  2179. * that often. */
  2180. watchdog_delay /= 2;
  2181. watchdog.tv_sec = watchdog_delay / 1000000;
  2182. watchdog.tv_usec = watchdog_delay % 1000000;
  2183. systemd_watchdog_timer = periodic_timer_new(tor_libevent_get_base(),
  2184. &watchdog,
  2185. systemd_watchdog_callback,
  2186. NULL);
  2187. tor_assert(systemd_watchdog_timer);
  2188. }
  2189. }
  2190. #endif /* defined(HAVE_SYSTEMD_209) */
  2191. #ifdef ENABLE_RESTART_DEBUGGING
  2192. {
  2193. static int first_time = 1;
  2194. if (first_time && getenv("TOR_DEBUG_RESTART")) {
  2195. first_time = 0;
  2196. const char *sec_str = getenv("TOR_DEBUG_RESTART_AFTER_SECONDS");
  2197. long sec;
  2198. int sec_ok=0;
  2199. if (sec_str &&
  2200. (sec = tor_parse_long(sec_str, 10, 0, INT_MAX, &sec_ok, NULL)) &&
  2201. sec_ok) {
  2202. /* Okay, we parsed the seconds. */
  2203. } else {
  2204. sec = 5;
  2205. }
  2206. struct timeval restart_after = { (time_t) sec, 0 };
  2207. tor_shutdown_event_loop_for_restart_event =
  2208. tor_evtimer_new(tor_libevent_get_base(),
  2209. tor_shutdown_event_loop_for_restart_cb, NULL);
  2210. event_add(tor_shutdown_event_loop_for_restart_event, &restart_after);
  2211. }
  2212. }
  2213. #endif /* defined(ENABLE_RESTART_DEBUGGING) */
  2214. /* Start our eventloop threads, then start the main eventloop. */
  2215. other_eventloops_should_exit = 0;
  2216. start_eventloop_threads(tor_eventloop_thread, start_tor_thread);
  2217. int rv = run_main_loop_until_done();
  2218. /* Stop our eventloops in other threads. */
  2219. other_eventloops_should_exit = 1;
  2220. rescan_eventloops();
  2221. join_eventloop_threads();
  2222. if (logging_throughput) {
  2223. destroy_thread_throughput_logging();
  2224. write_throughput_log(get_options()->ThroughputLogFile);
  2225. destroy_throughput_logging();
  2226. }
  2227. return rv;
  2228. }
  2229. #ifndef _WIN32
  2230. /** Rate-limiter for EINVAL-type libevent warnings. */
  2231. static ratelim_t libevent_error_ratelim = RATELIM_INIT(10);
  2232. #endif
  2233. /**
  2234. * Run the main loop a single time. Return 0 for "exit"; -1 for "exit with
  2235. * error", and 1 for "run this again."
  2236. */
  2237. static int
  2238. run_main_loop_once(void)
  2239. {
  2240. int loop_result;
  2241. if (nt_service_is_stopping())
  2242. return 0;
  2243. if (main_loop_should_exit)
  2244. return 0;
  2245. #ifndef _WIN32
  2246. /* Make it easier to tell whether libevent failure is our fault or not. */
  2247. errno = 0;
  2248. #endif
  2249. if (get_options()->MainloopStats) {
  2250. /* We always enforce that EVLOOP_ONCE is passed to event_base_loop() if we
  2251. * are collecting main loop statistics. */
  2252. called_loop_once = 1;
  2253. } else {
  2254. called_loop_once = 0;
  2255. }
  2256. /* Make sure we know (about) what time it is. */
  2257. update_approx_time(time(NULL));
  2258. /* Here it is: the main loop. Here we tell Libevent to poll until we have
  2259. * an event, or the second ends, or until we have some active linked
  2260. * connections to trigger events for. Libevent will wait till one
  2261. * of these happens, then run all the appropriate callbacks. */
  2262. loop_result = tor_libevent_run_event_loop(tor_libevent_get_base(),
  2263. called_loop_once);
  2264. if (get_options()->MainloopStats) {
  2265. /* Update our main loop counters. */
  2266. if (loop_result == 0) {
  2267. // The call was successful.
  2268. increment_main_loop_success_count();
  2269. } else if (loop_result == -1) {
  2270. // The call was erroneous.
  2271. increment_main_loop_error_count();
  2272. } else if (loop_result == 1) {
  2273. // The call didn't have any active or pending events
  2274. // to handle.
  2275. increment_main_loop_idle_count();
  2276. }
  2277. }
  2278. /* Oh, the loop failed. That might be an error that we need to
  2279. * catch, but more likely, it's just an interrupted poll() call or something,
  2280. * and we should try again. */
  2281. if (loop_result < 0) {
  2282. int e = tor_socket_errno(-1);
  2283. /* let the program survive things like ^z */
  2284. if (e != EINTR && !ERRNO_IS_EINPROGRESS(e)) {
  2285. log_err(LD_NET,"libevent call with %s failed: %s [%d]",
  2286. tor_libevent_get_method(), tor_socket_strerror(e), e);
  2287. return -1;
  2288. #ifndef _WIN32
  2289. } else if (e == EINVAL) {
  2290. log_fn_ratelim(&libevent_error_ratelim, LOG_WARN, LD_NET,
  2291. "EINVAL from libevent: should you upgrade libevent?");
  2292. if (libevent_error_ratelim.n_calls_since_last_time > 8) {
  2293. log_err(LD_NET, "Too many libevent errors, too fast: dying");
  2294. return -1;
  2295. }
  2296. #endif /* !defined(_WIN32) */
  2297. } else {
  2298. tor_assert_nonfatal_once(! ERRNO_IS_EINPROGRESS(e));
  2299. log_debug(LD_NET,"libevent call interrupted.");
  2300. /* You can't trust the results of this poll(). Go back to the
  2301. * top of the big for loop. */
  2302. return 1;
  2303. }
  2304. }
  2305. if (main_loop_should_exit)
  2306. return 0;
  2307. return 1;
  2308. }
  2309. /** Run the run_main_loop_once() function until it declares itself done,
  2310. * and return its final return value.
  2311. *
  2312. * Shadow won't invoke this function, so don't fill it up with things.
  2313. */
  2314. STATIC int
  2315. run_main_loop_until_done(void)
  2316. {
  2317. int loop_result = 1;
  2318. main_loop_should_exit = 0;
  2319. main_loop_exit_value = 0;
  2320. do {
  2321. loop_result = run_main_loop_once();
  2322. } while (loop_result == 1);
  2323. if (main_loop_should_exit)
  2324. return main_loop_exit_value;
  2325. else
  2326. return loop_result;
  2327. }
  2328. /** Returns Tor's uptime. */
  2329. MOCK_IMPL(long,
  2330. get_uptime,(void))
  2331. {
  2332. return stats_n_seconds_working;
  2333. }
  2334. /** Reset Tor's uptime. */
  2335. MOCK_IMPL(void,
  2336. reset_uptime,(void))
  2337. {
  2338. stats_n_seconds_working = 0;
  2339. }
  2340. void
  2341. tor_mainloop_free_all(void)
  2342. {
  2343. smartlist_free(connection_array);
  2344. smartlist_free(closeable_connection_lst);
  2345. smartlist_free(active_linked_connection_lst);
  2346. teardown_periodic_events();
  2347. tor_event_free(shutdown_did_not_work_event);
  2348. tor_event_free(initialize_periodic_events_event);
  2349. mainloop_event_free(directory_all_unreachable_cb_event);
  2350. mainloop_event_free(schedule_active_linked_connections_event);
  2351. mainloop_event_free(postloop_cleanup_ev);
  2352. mainloop_event_free(handle_deferred_signewnym_ev);
  2353. mainloop_event_free(scheduled_shutdown_ev);
  2354. mainloop_event_free(rescan_periodic_events_ev);
  2355. #ifdef HAVE_SYSTEMD_209
  2356. periodic_timer_free(systemd_watchdog_timer);
  2357. #endif
  2358. stats_n_bytes_read = stats_n_bytes_written = 0;
  2359. memset(&global_bucket, 0, sizeof(global_bucket));
  2360. memset(&global_relayed_bucket, 0, sizeof(global_relayed_bucket));
  2361. time_of_process_start = 0;
  2362. time_of_last_signewnym = 0;
  2363. signewnym_is_pending = 0;
  2364. newnym_epoch = 0;
  2365. called_loop_once = 0;
  2366. main_loop_should_exit = 0;
  2367. main_loop_exit_value = 0;
  2368. other_eventloops_should_exit = 0;
  2369. can_complete_circuits = 0;
  2370. quiet_level = 0;
  2371. should_init_bridge_stats = 1;
  2372. heartbeat_callback_first_time = 1;
  2373. current_second = 0;
  2374. memset(&current_second_last_changed, 0,
  2375. sizeof(current_second_last_changed));
  2376. }