mainloop.c 86 KB

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