compat_libevent.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. /* Copyright (c) 2009-2019, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file compat_libevent.c
  5. * \brief Wrappers and utility functions for Libevent.
  6. */
  7. #include "orconfig.h"
  8. #define COMPAT_LIBEVENT_PRIVATE
  9. #include "lib/evloop/compat_libevent.h"
  10. #include "lib/crypt_ops/crypto_rand.h"
  11. #include "lib/log/log.h"
  12. #include "lib/log/util_bug.h"
  13. #include "lib/string/compat_string.h"
  14. #include <event2/thread.h>
  15. #include <string.h>
  16. /** A string which, if it appears in a libevent log, should be ignored. */
  17. static const char *suppress_msg = NULL;
  18. /** Callback function passed to event_set_log() so we can intercept
  19. * log messages from libevent. */
  20. STATIC void
  21. libevent_logging_callback(int severity, const char *msg)
  22. {
  23. char buf[1024];
  24. size_t n;
  25. if (suppress_msg && strstr(msg, suppress_msg))
  26. return;
  27. n = strlcpy(buf, msg, sizeof(buf));
  28. if (n && n < sizeof(buf) && buf[n-1] == '\n') {
  29. buf[n-1] = '\0';
  30. }
  31. switch (severity) {
  32. case _EVENT_LOG_DEBUG:
  33. log_debug(LD_NOCB|LD_NET, "Message from libevent: %s", buf);
  34. break;
  35. case _EVENT_LOG_MSG:
  36. log_info(LD_NOCB|LD_NET, "Message from libevent: %s", buf);
  37. break;
  38. case _EVENT_LOG_WARN:
  39. log_warn(LD_NOCB|LD_GENERAL, "Warning from libevent: %s", buf);
  40. break;
  41. case _EVENT_LOG_ERR:
  42. log_err(LD_NOCB|LD_GENERAL, "Error from libevent: %s", buf);
  43. break;
  44. default:
  45. log_warn(LD_NOCB|LD_GENERAL, "Message [%d] from libevent: %s",
  46. severity, buf);
  47. break;
  48. }
  49. }
  50. /** Set hook to intercept log messages from libevent. */
  51. void
  52. configure_libevent_logging(void)
  53. {
  54. event_set_log_callback(libevent_logging_callback);
  55. }
  56. /** Ignore any libevent log message that contains <b>msg</b>. */
  57. void
  58. suppress_libevent_log_msg(const char *msg)
  59. {
  60. suppress_msg = msg;
  61. }
  62. /* Wrapper for event_free() that tolerates tor_event_free(NULL) */
  63. void
  64. tor_event_free_(struct event *ev)
  65. {
  66. if (ev == NULL)
  67. return;
  68. event_free(ev);
  69. }
  70. /** Global event bases for use by the eventloop threads. */
  71. static tor_thread_t **threads = NULL;
  72. static struct event_base **eventloops = NULL;
  73. static tor_threadlocal_t eventloop_index;
  74. static int num_eventloops = -1;
  75. /**
  76. * @defgroup postloop post-loop event helpers
  77. *
  78. * If we're not careful, Libevent can susceptible to infinite event chains:
  79. * one event can activate another, whose callback activates another, whose
  80. * callback activates another, ad infinitum. While this is happening,
  81. * Libevent won't be checking timeouts, socket-based events, signals, and so
  82. * on.
  83. *
  84. * We solve this problem by marking some events as "post-loop". A post-loop
  85. * event behaves like any ordinary event, but any events that _it_ activates
  86. * cannot run until Libevent has checked for other events at least once.
  87. *
  88. * @{ */
  89. /**
  90. * Events that stop Libevent from running any more events on the current
  91. * iteration of its loop, until it has re-checked for socket events, signal
  92. * events, timeouts, etc.
  93. */
  94. struct event **rescan_eventloop_events = NULL;
  95. /**
  96. * Callback to implement rescan_mainloop_ev: it simply exits the mainloop,
  97. * and relies on Tor to re-enter the mainloop since no error has occurred.
  98. */
  99. static void
  100. rescan_mainloop_cb(evutil_socket_t fd, short events, void *arg)
  101. {
  102. (void)fd;
  103. (void)events;
  104. struct event_base *the_base = arg;
  105. event_base_loopbreak(the_base);
  106. }
  107. /** @} */
  108. struct event_base *
  109. get_eventloop(int index)
  110. {
  111. tor_assert(index >= 0 && index < num_eventloops);
  112. tor_assert(eventloops != NULL);
  113. tor_assert(eventloops[index] != NULL);
  114. return eventloops[index];
  115. }
  116. int
  117. get_local_eventloop_index(void) {
  118. int *index = tor_threadlocal_get(&eventloop_index);
  119. tor_assert(index != NULL);
  120. return *index;
  121. }
  122. int
  123. get_num_eventloops(void)
  124. {
  125. tor_assert(num_eventloops != -1);
  126. return num_eventloops;
  127. }
  128. /* Tell the eventloops to stop soon. */
  129. void
  130. rescan_eventloops(void)
  131. {
  132. tor_assert(rescan_eventloop_events != NULL);
  133. for (int i=0; i<num_eventloops; i++) {
  134. tor_assert(rescan_eventloop_events[i] != NULL);
  135. event_active(rescan_eventloop_events[i], EV_READ, 1);
  136. }
  137. }
  138. /* This is what passes for version detection on OSX. We set
  139. * MACOSX_KQUEUE_IS_BROKEN to true iff we're on a version of OSX before
  140. * 10.4.0 (aka 1040). */
  141. #ifdef __APPLE__
  142. #ifdef __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__
  143. #define MACOSX_KQUEUE_IS_BROKEN \
  144. (__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 1040)
  145. #else
  146. #define MACOSX_KQUEUE_IS_BROKEN 0
  147. #endif /* defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) */
  148. #endif /* defined(__APPLE__) */
  149. /** Initialize the threadlocals. Must be run before libevent is initialized. */
  150. void
  151. tor_evloop_init_threadlocals(void)
  152. {
  153. tor_assert(tor_threadlocal_init(&eventloop_index) == 0);
  154. }
  155. /** Destroy the threadlocals. Must be run after tor_libevent_free_all. */
  156. void
  157. tor_evloop_destroy_threadlocals(void)
  158. {
  159. tor_threadlocal_destroy(&eventloop_index);
  160. }
  161. /** Initialize the current thread. */
  162. static void
  163. init_eventloop_thread(int index)
  164. {
  165. tor_assert(index >= 0 && index < num_eventloops);
  166. tor_assert(tor_threadlocal_get(&eventloop_index) == NULL);
  167. int* this_thread_index = tor_malloc(sizeof(int));
  168. *this_thread_index = index;
  169. tor_threadlocal_set(&eventloop_index, (void *)this_thread_index);
  170. }
  171. /* Destory the current thread. */
  172. static void
  173. destroy_eventloop_thread(void)
  174. {
  175. int *this_thread_index = (int *)tor_threadlocal_get(&eventloop_index);
  176. if (this_thread_index != NULL) {
  177. tor_threadlocal_set(&eventloop_index, NULL);
  178. tor_free(this_thread_index);
  179. }
  180. }
  181. struct thread_data {
  182. void (*func)(void);
  183. int index;
  184. };
  185. static void
  186. thread_wrapper(void *data_void)
  187. {
  188. tor_assert(data_void != NULL);
  189. struct thread_data *data = (struct thread_data *)data_void;
  190. void (*func)(void) = data->func;
  191. int index = data->index;
  192. tor_free(data_void);
  193. init_eventloop_thread(index);
  194. func();
  195. destroy_eventloop_thread();
  196. }
  197. /* Start the eventloop threads and make them run 'func'. Threads will
  198. be started using 'spawn_fn'. */
  199. void
  200. start_eventloop_threads(void (*func)(void),
  201. tor_thread_t *(*spawn_fn)(void (*func)(void *),
  202. void *data))
  203. {
  204. // the first thread (i==0) should already have been initialized
  205. for (int i=1; i<num_eventloops; i++) {
  206. struct thread_data *data = tor_malloc(sizeof(struct thread_data));
  207. data->func = func;
  208. data->index = i;
  209. tor_thread_t *thread = spawn_fn(thread_wrapper, (void *)data);
  210. tor_assert(thread != NULL);
  211. threads[i] = thread;
  212. }
  213. }
  214. /* Wait for the eventloop threads to finish. */
  215. void
  216. join_eventloop_threads(void)
  217. {
  218. for (int i=1; i<num_eventloops; i++) {
  219. if (threads[i] != NULL) {
  220. tor_assert(join_thread(threads[i]) == 0);
  221. free_thread(threads[i]);
  222. threads[i] = NULL;
  223. }
  224. }
  225. }
  226. /** Initialize the Libevent library and set up the event base. */
  227. void
  228. tor_libevent_initialize(tor_libevent_cfg *torcfg,
  229. int num_additional_eventloops)
  230. {
  231. /* some paths below don't use torcfg, so avoid unused variable warnings */
  232. (void)torcfg;
  233. tor_assert(num_eventloops == -1);
  234. num_eventloops = num_additional_eventloops+1;
  235. eventloops = tor_malloc_zero(num_eventloops*sizeof(struct event_base *));
  236. threads = tor_malloc_zero(num_eventloops*sizeof(tor_thread_t *));
  237. rescan_eventloop_events = tor_malloc_zero(num_eventloops*sizeof(tor_thread_t *));
  238. /* This main thread has no tor_thread_t, so we set it to NULL. Should already
  239. be NULL from tor_malloc_zero, but we do this explicitly anyways. */
  240. threads[0] = NULL;
  241. int libevent_started_correctly = 1;
  242. {
  243. int attempts = 0;
  244. struct event_config *cfg;
  245. tor_assert(evthread_use_pthreads() == 0);
  246. ++attempts;
  247. cfg = event_config_new();
  248. tor_assert(cfg);
  249. if (torcfg->num_cpus > 0)
  250. event_config_set_num_cpus_hint(cfg, torcfg->num_cpus);
  251. /* We can enable changelist support with epoll, since we don't give
  252. * Libevent any dup'd fds. This lets us avoid some syscalls. */
  253. event_config_set_flag(cfg, EVENT_BASE_FLAG_EPOLL_USE_CHANGELIST);
  254. for (int i=0; i<num_eventloops; i++) {
  255. eventloops[i] = event_base_new_with_config(cfg);
  256. rescan_eventloop_events[i] = event_new(eventloops[i], -1, 0,
  257. rescan_mainloop_cb,
  258. eventloops[i]);
  259. libevent_started_correctly = (libevent_started_correctly &&
  260. eventloops[i] != NULL &&
  261. rescan_eventloop_events[i] != NULL);
  262. }
  263. event_config_free(cfg);
  264. }
  265. if (libevent_started_correctly == 0) {
  266. /* LCOV_EXCL_START */
  267. log_err(LD_GENERAL, "Unable to initialize Libevent: cannot continue.");
  268. exit(1); // exit ok: libevent is broken.
  269. /* LCOV_EXCL_STOP */
  270. }
  271. /* Initialize this main thread. */
  272. init_eventloop_thread(0);
  273. log_info(LD_GENERAL,
  274. "Initialized libevent version %s using method %s. Good.",
  275. event_get_version(), tor_libevent_get_method());
  276. }
  277. /**
  278. * Return true iff the libevent module has been successfully initialized,
  279. * and not subsequently shut down.
  280. **/
  281. bool
  282. tor_libevent_is_initialized(void)
  283. {
  284. return num_eventloops != -1;
  285. }
  286. /** Return the current Libevent event base that we're set up to use. */
  287. MOCK_IMPL(struct event_base *,
  288. tor_libevent_get_base, (void))
  289. {
  290. int *index = tor_threadlocal_get(&eventloop_index);
  291. tor_assert(index != NULL);
  292. struct event_base *event_base = eventloops[*index];
  293. tor_assert(event_base != NULL);
  294. return event_base;
  295. }
  296. /** Return the name of the Libevent backend we're using. */
  297. const char *
  298. tor_libevent_get_method(void)
  299. {
  300. struct event_base *event_base = tor_libevent_get_base();
  301. return event_base_get_method(event_base);
  302. }
  303. /** Return a string representation of the version of the currently running
  304. * version of Libevent. */
  305. const char *
  306. tor_libevent_get_version_str(void)
  307. {
  308. return event_get_version();
  309. }
  310. /** Return a string representation of the version of Libevent that was used
  311. * at compilation time. */
  312. const char *
  313. tor_libevent_get_header_version_str(void)
  314. {
  315. return LIBEVENT_VERSION;
  316. }
  317. /** Represents a timer that's run every N microseconds by Libevent. */
  318. struct periodic_timer_t {
  319. /** Underlying event used to implement this periodic event. */
  320. struct event *ev;
  321. /** The callback we'll be invoking whenever the event triggers */
  322. void (*cb)(struct periodic_timer_t *, void *);
  323. /** User-supplied data for the callback */
  324. void *data;
  325. };
  326. /** Libevent callback to implement a periodic event. */
  327. static void
  328. periodic_timer_cb(evutil_socket_t fd, short what, void *arg)
  329. {
  330. periodic_timer_t *timer = arg;
  331. (void) what;
  332. (void) fd;
  333. timer->cb(timer, timer->data);
  334. }
  335. /** Create and schedule a new timer that will run every <b>tv</b> in
  336. * the event loop of <b>base</b>. When the timer fires, it will
  337. * run the timer in <b>cb</b> with the user-supplied data in <b>data</b>. */
  338. periodic_timer_t *
  339. periodic_timer_new(struct event_base *base,
  340. const struct timeval *tv,
  341. void (*cb)(periodic_timer_t *timer, void *data),
  342. void *data)
  343. {
  344. periodic_timer_t *timer;
  345. tor_assert(base);
  346. tor_assert(tv);
  347. tor_assert(cb);
  348. timer = tor_malloc_zero(sizeof(periodic_timer_t));
  349. if (!(timer->ev = tor_event_new(base, -1, EV_PERSIST,
  350. periodic_timer_cb, timer))) {
  351. tor_free(timer);
  352. return NULL;
  353. }
  354. timer->cb = cb;
  355. timer->data = data;
  356. periodic_timer_launch(timer, tv);
  357. return timer;
  358. }
  359. /**
  360. * Launch the timer <b>timer</b> to run at <b>tv</b> from now, and every
  361. * <b>tv</b> thereafter.
  362. *
  363. * If the timer is already enabled, this function does nothing.
  364. */
  365. void
  366. periodic_timer_launch(periodic_timer_t *timer, const struct timeval *tv)
  367. {
  368. tor_assert(timer);
  369. if (event_pending(timer->ev, EV_TIMEOUT, NULL))
  370. return;
  371. event_add(timer->ev, tv);
  372. }
  373. /**
  374. * Disable the provided <b>timer</b>, but do not free it.
  375. *
  376. * You can reenable the same timer later with periodic_timer_launch.
  377. *
  378. * If the timer is already disabled, this function does nothing.
  379. */
  380. void
  381. periodic_timer_disable(periodic_timer_t *timer)
  382. {
  383. tor_assert(timer);
  384. (void) event_del(timer->ev);
  385. }
  386. /** Stop and free a periodic timer */
  387. void
  388. periodic_timer_free_(periodic_timer_t *timer)
  389. {
  390. if (!timer)
  391. return;
  392. tor_event_free(timer->ev);
  393. tor_free(timer);
  394. }
  395. /**
  396. * Type used to represent events that run directly from the main loop,
  397. * either because they are activated from elsewhere in the code, or
  398. * because they have a simple timeout.
  399. *
  400. * We use this type to avoid exposing Libevent's API throughout the rest
  401. * of the codebase.
  402. *
  403. * This type can't be used for all events: it doesn't handle events that
  404. * are triggered by signals or by sockets.
  405. */
  406. struct mainloop_event_t {
  407. struct event *ev;
  408. void (*cb)(mainloop_event_t *, void *);
  409. void *userdata;
  410. };
  411. /**
  412. * Internal: Implements mainloop event using a libevent event.
  413. */
  414. static void
  415. mainloop_event_cb(evutil_socket_t fd, short what, void *arg)
  416. {
  417. (void)fd;
  418. (void)what;
  419. mainloop_event_t *mev = arg;
  420. mev->cb(mev, mev->userdata);
  421. }
  422. /**
  423. * As mainloop_event_cb, but implements a post-loop event.
  424. */
  425. static void
  426. mainloop_event_postloop_cb(evutil_socket_t fd, short what, void *arg)
  427. {
  428. (void)fd;
  429. (void)what;
  430. /* Note that if rescan_mainloop_ev is already activated,
  431. * event_active() will do nothing: only the first post-loop event that
  432. * happens each time through the event loop will cause it to be
  433. * activated.
  434. *
  435. * Because event_active() puts events on a FIFO queue, every event
  436. * that is made active _after_ rescan_mainloop_ev will get its
  437. * callback run after rescan_mainloop_cb is called -- that is, on the
  438. * next iteration of the loop.
  439. */
  440. int *index = tor_threadlocal_get(&eventloop_index);
  441. tor_assert(index != NULL);
  442. struct event *rescan_event = rescan_eventloop_events[*index];
  443. tor_assert(rescan_event != NULL);
  444. event_active(rescan_event, EV_READ, 1);
  445. mainloop_event_t *mev = arg;
  446. mev->cb(mev, mev->userdata);
  447. }
  448. /**
  449. * Helper for mainloop_event_new() and mainloop_event_postloop_new().
  450. */
  451. static mainloop_event_t *
  452. mainloop_event_new_impl(int postloop,
  453. void (*cb)(mainloop_event_t *, void *),
  454. void *userdata)
  455. {
  456. tor_assert(cb);
  457. struct event_base *base = tor_libevent_get_base();
  458. mainloop_event_t *mev = tor_malloc_zero(sizeof(mainloop_event_t));
  459. mev->ev = tor_event_new(base, -1, 0,
  460. postloop ? mainloop_event_postloop_cb : mainloop_event_cb,
  461. mev);
  462. tor_assert(mev->ev);
  463. mev->cb = cb;
  464. mev->userdata = userdata;
  465. return mev;
  466. }
  467. /**
  468. * Create and return a new mainloop_event_t to run the function <b>cb</b>.
  469. *
  470. * When run, the callback function will be passed the mainloop_event_t
  471. * and <b>userdata</b> as its arguments. The <b>userdata</b> pointer
  472. * must remain valid for as long as the mainloop_event_t event exists:
  473. * it is your responsibility to free it.
  474. *
  475. * The event is not scheduled by default: Use mainloop_event_activate()
  476. * or mainloop_event_schedule() to make it run.
  477. */
  478. mainloop_event_t *
  479. mainloop_event_new(void (*cb)(mainloop_event_t *, void *),
  480. void *userdata)
  481. {
  482. return mainloop_event_new_impl(0, cb, userdata);
  483. }
  484. /**
  485. * As mainloop_event_new(), but create a post-loop event.
  486. *
  487. * A post-loop event behaves like any ordinary event, but any events
  488. * that _it_ activates cannot run until Libevent has checked for other
  489. * events at least once.
  490. */
  491. mainloop_event_t *
  492. mainloop_event_postloop_new(void (*cb)(mainloop_event_t *, void *),
  493. void *userdata)
  494. {
  495. return mainloop_event_new_impl(1, cb, userdata);
  496. }
  497. /**
  498. * Schedule <b>event</b> to run in the main loop, immediately. If it is
  499. * not scheduled, it will run anyway. If it is already scheduled to run
  500. * later, it will run now instead. This function will have no effect if
  501. * the event is already scheduled to run.
  502. *
  503. * This function may only be called from the main thread.
  504. */
  505. void
  506. mainloop_event_activate(mainloop_event_t *event)
  507. {
  508. tor_assert(event);
  509. event_active(event->ev, EV_READ, 1);
  510. }
  511. /** Schedule <b>event</b> to run in the main loop, after a delay of <b>tv</b>.
  512. *
  513. * If the event is scheduled for a different time, cancel it and run
  514. * after this delay instead. If the event is currently pending to run
  515. * <em>now</b>, has no effect.
  516. *
  517. * Do not call this function with <b>tv</b> == NULL -- use
  518. * mainloop_event_activate() instead.
  519. *
  520. * This function may only be called from the main thread.
  521. */
  522. int
  523. mainloop_event_schedule(mainloop_event_t *event, const struct timeval *tv)
  524. {
  525. tor_assert(event);
  526. if (BUG(tv == NULL)) {
  527. // LCOV_EXCL_START
  528. mainloop_event_activate(event);
  529. return 0;
  530. // LCOV_EXCL_STOP
  531. }
  532. return event_add(event->ev, tv);
  533. }
  534. /** Cancel <b>event</b> if it is currently active or pending. (Do nothing if
  535. * the event is not currently active or pending.) */
  536. void
  537. mainloop_event_cancel(mainloop_event_t *event)
  538. {
  539. if (!event)
  540. return;
  541. (void) event_del(event->ev);
  542. }
  543. /** Cancel <b>event</b> and release all storage associated with it. */
  544. void
  545. mainloop_event_free_(mainloop_event_t *event)
  546. {
  547. if (!event)
  548. return;
  549. tor_event_free(event->ev);
  550. memset(event, 0xb8, sizeof(*event));
  551. tor_free(event);
  552. }
  553. int
  554. tor_init_libevent_rng(void)
  555. {
  556. int rv = 0;
  557. char buf[256];
  558. if (evutil_secure_rng_init() < 0) {
  559. rv = -1;
  560. }
  561. crypto_rand(buf, 32);
  562. #ifdef HAVE_EVUTIL_SECURE_RNG_ADD_BYTES
  563. evutil_secure_rng_add_bytes(buf, 32);
  564. #endif
  565. evutil_secure_rng_get_bytes(buf, sizeof(buf));
  566. return rv;
  567. }
  568. /**
  569. * Un-initialize libevent in preparation for an exit
  570. */
  571. void
  572. tor_libevent_free_all(void)
  573. {
  574. /* Make sure all eventloop threads have stopped. */
  575. join_eventloop_threads();
  576. /* Destroy this main thread. Other eventloop threads should already have
  577. called this locally. */
  578. destroy_eventloop_thread();
  579. /* Destroy the rescan events since we own them. */
  580. for (int i=0; i<num_eventloops; i++) {
  581. event_free(rescan_eventloop_events[i]);
  582. rescan_eventloop_events[i] = NULL;
  583. }
  584. /* Destory the event bases since we own them. */
  585. for (int i=0; i<num_eventloops; i++) {
  586. event_base_free(eventloops[i]);
  587. eventloops[i] = NULL;
  588. }
  589. tor_free(eventloops);
  590. tor_free(threads);
  591. tor_free(rescan_eventloop_events);
  592. num_eventloops = -1;
  593. }
  594. /**
  595. * Run the event loop for the provided event_base, handling events until
  596. * something stops it. If <b>once</b> is set, then just poll-and-run
  597. * once, then exit. Return 0 on success, -1 if an error occurred, or 1
  598. * if we exited because no events were pending or active.
  599. *
  600. * This isn't reentrant or multithreaded.
  601. */
  602. int
  603. tor_libevent_run_event_loop(struct event_base *base, int once)
  604. {
  605. const int flags = once ? EVLOOP_ONCE : 0;
  606. return event_base_loop(base, flags);
  607. }
  608. /** Tell the event loop to exit after <b>delay</b>. If <b>delay</b> is NULL,
  609. * instead exit after we're done running the currently active events. */
  610. void
  611. tor_libevent_exit_loop_after_delay(struct event_base *base,
  612. const struct timeval *delay)
  613. {
  614. event_base_loopexit(base, delay);
  615. }
  616. /** Tell the event loop to exit after running whichever callback is currently
  617. * active. */
  618. void
  619. tor_libevent_exit_loop_after_callback(struct event_base *base)
  620. {
  621. event_base_loopbreak(base);
  622. }
  623. #if defined(TOR_UNIT_TESTS)
  624. /** For testing: called post-fork to make libevent reinitialize
  625. * kernel structures. */
  626. void
  627. tor_libevent_postfork(void)
  628. {
  629. int r = event_reinit(tor_libevent_get_base());
  630. tor_assert(r == 0);
  631. }
  632. #endif /* defined(TOR_UNIT_TESTS) */