Tor 0.4.9.8
Loading...
Searching...
No Matches
compat_libevent.c File Reference

Wrappers and utility functions for Libevent. More...

#include "orconfig.h"
#include "lib/evloop/compat_libevent.h"
#include "lib/crypt_ops/crypto_rand.h"
#include "lib/log/log.h"
#include "lib/log/util_bug.h"
#include "lib/string/compat_string.h"
#include <event2/event.h>
#include <event2/thread.h>
#include <string.h>

Go to the source code of this file.

Data Structures

struct  periodic_timer_t
 
struct  mainloop_event_t
 

Functions

STATIC void libevent_logging_callback (int severity, const char *msg)
 
void configure_libevent_logging (void)
 
void suppress_libevent_log_msg (const char *msg)
 
void tor_event_free_ (struct event *ev)
 
static void rescan_mainloop_cb (evutil_socket_t fd, short events, void *arg)
 
void tor_libevent_initialize (tor_libevent_cfg_t *torcfg)
 
bool tor_libevent_is_initialized (void)
 
struct event_base * tor_libevent_get_base (void)
 
const char * tor_libevent_get_method (void)
 
const char * tor_libevent_get_version_str (void)
 
const char * tor_libevent_get_header_version_str (void)
 
static void periodic_timer_cb (evutil_socket_t fd, short what, void *arg)
 
periodic_timer_tperiodic_timer_new (struct event_base *base, const struct timeval *tv, void(*cb)(periodic_timer_t *timer, void *data), void *data)
 
void periodic_timer_launch (periodic_timer_t *timer, const struct timeval *tv)
 
void periodic_timer_disable (periodic_timer_t *timer)
 
void periodic_timer_free_ (periodic_timer_t *timer)
 
static void mainloop_event_cb (evutil_socket_t fd, short what, void *arg)
 
static void mainloop_event_postloop_cb (evutil_socket_t fd, short what, void *arg)
 
static mainloop_event_tmainloop_event_new_impl (int postloop, void(*cb)(mainloop_event_t *, void *), void *userdata)
 
mainloop_event_tmainloop_event_new (void(*cb)(mainloop_event_t *, void *), void *userdata)
 
mainloop_event_tmainloop_event_postloop_new (void(*cb)(mainloop_event_t *, void *), void *userdata)
 
void mainloop_event_activate (mainloop_event_t *event)
 
int mainloop_event_schedule (mainloop_event_t *event, const struct timeval *tv)
 
void mainloop_event_cancel (mainloop_event_t *event)
 
void mainloop_event_free_ (mainloop_event_t *event)
 
int tor_init_libevent_rng (void)
 
void tor_libevent_free_all (void)
 
int tor_libevent_run_event_loop (struct event_base *base, int once)
 
void tor_libevent_exit_loop_after_delay (struct event_base *base, const struct timeval *delay)
 
void tor_libevent_exit_loop_after_callback (struct event_base *base)
 

Variables

static const char * suppress_msg = NULL
 
static struct event_base * the_event_base = NULL
 
static struct event * rescan_mainloop_ev = NULL
 

Detailed Description

Wrappers and utility functions for Libevent.

Definition in file compat_libevent.c.

Macro Definition Documentation

◆ COMPAT_LIBEVENT_PRIVATE

#define COMPAT_LIBEVENT_PRIVATE

Definition at line 10 of file compat_libevent.c.

Function Documentation

◆ configure_libevent_logging()

void configure_libevent_logging ( void )

Set hook to intercept log messages from libevent.

Definition at line 58 of file compat_libevent.c.

Referenced by init_libevent().

◆ libevent_logging_callback()

STATIC void libevent_logging_callback ( int severity,
const char * msg )

Callback function passed to event_set_log() so we can intercept log messages from libevent.

Definition at line 27 of file compat_libevent.c.

Referenced by configure_libevent_logging().

◆ mainloop_event_activate()

void mainloop_event_activate ( mainloop_event_t * event)

Schedule event to run in the main loop, immediately. If it is not scheduled, it will run anyway. If it is already scheduled to run later, it will run now instead. This function will have no effect if the event is already scheduled to run.

This function may only be called from the main thread.

Definition at line 423 of file compat_libevent.c.

Referenced by alertfn_prompt(), connection_ap_mark_as_pending_circuit_(), connection_start_reading_from_linked_conn(), control_event_logmsg_pending(), directory_all_unreachable(), enqueue_rend_request(), hs_client_dir_info_changed(), mainloop_event_schedule(), mainloop_schedule_postloop_cleanup(), mark_cdm_cache_dirty(), periodic_event_enable(), periodic_event_schedule_and_disable(), queue_control_event_string(), schedule_active_linked_connections_cb(), schedule_rescan_periodic_events(), and scheduler_ev_active().

◆ mainloop_event_cancel()

void mainloop_event_cancel ( mainloop_event_t * event)

Cancel event if it is currently active or pending. (Do nothing if the event is not currently active or pending.)

Definition at line 456 of file compat_libevent.c.

Referenced by periodic_event_disable().

◆ mainloop_event_cb()

static void mainloop_event_cb ( evutil_socket_t fd,
short what,
void * arg )
static

Internal: Implements mainloop event using a libevent event.

Definition at line 328 of file compat_libevent.c.

Referenced by mainloop_event_new_impl().

◆ mainloop_event_free_()

void mainloop_event_free_ ( mainloop_event_t * event)

Cancel event and release all storage associated with it.

Definition at line 465 of file compat_libevent.c.

◆ mainloop_event_new()

mainloop_event_t * mainloop_event_new ( void(* cb )(mainloop_event_t *, void *),
void * userdata )

Create and return a new mainloop_event_t to run the function cb.

When run, the callback function will be passed the mainloop_event_t and userdata as its arguments. The userdata pointer must remain valid for as long as the mainloop_event_t event exists: it is your responsibility to free it.

The event is not scheduled by default: Use mainloop_event_activate() or mainloop_event_schedule() to make it run.

Definition at line 394 of file compat_libevent.c.

Referenced by configure_proxy(), directory_all_unreachable(), hs_client_dir_info_changed(), mainloop_schedule_shutdown(), periodic_event_connect(), reenable_blocked_connection_init(), schedule_rescan_periodic_events(), and timers_initialize().

◆ mainloop_event_new_impl()

static mainloop_event_t * mainloop_event_new_impl ( int postloop,
void(* cb )(mainloop_event_t *, void *),
void * userdata )
static

◆ mainloop_event_postloop_cb()

static void mainloop_event_postloop_cb ( evutil_socket_t fd,
short what,
void * arg )
static

As mainloop_event_cb, but implements a post-loop event.

Definition at line 340 of file compat_libevent.c.

Referenced by mainloop_event_new_impl().

◆ mainloop_event_postloop_new()

mainloop_event_t * mainloop_event_postloop_new ( void(* cb )(mainloop_event_t *, void *),
void * userdata )

As mainloop_event_new(), but create a post-loop event.

A post-loop event behaves like any ordinary event, but any events that it activates cannot run until Libevent has checked for other events at least once.

Definition at line 408 of file compat_libevent.c.

Referenced by cdm_cache_init(), connection_ap_mark_as_pending_circuit_(), do_signewnym(), enqueue_rend_request(), hibernate_schedule_wakeup_event(), initialize_mainloop_events(), and tor_mainloop_connect_pubsub_events().

◆ mainloop_event_schedule()

int mainloop_event_schedule ( mainloop_event_t * event,
const struct timeval * tv )

Schedule event to run in the main loop, after a delay of tv.

If the event is scheduled for a different time, cancel it and run after this delay instead. If the event is currently pending to run now, has no effect.

Do not call this function with tv == NULL – use mainloop_event_activate() instead.

This function may only be called from the main thread.

Definition at line 441 of file compat_libevent.c.

Referenced by configure_proxy(), do_signewnym(), hibernate_schedule_wakeup_event(), libevent_timer_reschedule(), mainloop_schedule_shutdown(), periodic_event_dispatch(), periodic_event_set_interval(), reenable_blocked_connection_schedule(), and scheduler_ev_add().

◆ periodic_timer_cb()

static void periodic_timer_cb ( evutil_socket_t fd,
short what,
void * arg )
static

Libevent callback to implement a periodic event.

Definition at line 235 of file compat_libevent.c.

Referenced by periodic_timer_new().

◆ periodic_timer_disable()

void periodic_timer_disable ( periodic_timer_t * timer)

Disable the provided timer, but do not free it.

You can reenable the same timer later with periodic_timer_launch.

If the timer is already disabled, this function does nothing.

Definition at line 291 of file compat_libevent.c.

◆ periodic_timer_free_()

void periodic_timer_free_ ( periodic_timer_t * timer)

Stop and free a periodic timer

Definition at line 299 of file compat_libevent.c.

◆ periodic_timer_launch()

void periodic_timer_launch ( periodic_timer_t * timer,
const struct timeval * tv )

Launch the timer timer to run at tv from now, and every tv thereafter.

If the timer is already enabled, this function does nothing.

Definition at line 275 of file compat_libevent.c.

Referenced by periodic_timer_new().

◆ periodic_timer_new()

periodic_timer_t * periodic_timer_new ( struct event_base * base,
const struct timeval * tv,
void(* cb )(periodic_timer_t *timer, void *data),
void * data )

Create and schedule a new timer that will run every tv in the event loop of base. When the timer fires, it will run the timer in cb with the user-supplied data in data.

Definition at line 247 of file compat_libevent.c.

Referenced by do_main_loop(), and tor_process_monitor_new().

◆ suppress_libevent_log_msg()

void suppress_libevent_log_msg ( const char * msg)

Ignore any libevent log message that contains msg.

Definition at line 65 of file compat_libevent.c.

Referenced by init_libevent().

◆ tor_event_free_()

void tor_event_free_ ( struct event * ev)

Definition at line 72 of file compat_libevent.c.

◆ tor_init_libevent_rng()

int tor_init_libevent_rng ( void )

Definition at line 475 of file compat_libevent.c.

◆ tor_libevent_exit_loop_after_callback()

void tor_libevent_exit_loop_after_callback ( struct event_base * base)

Tell the event loop to exit after running whichever callback is currently active.

Definition at line 529 of file compat_libevent.c.

Referenced by tor_shutdown_event_loop_and_exit().

◆ tor_libevent_exit_loop_after_delay()

void tor_libevent_exit_loop_after_delay ( struct event_base * base,
const struct timeval * delay )

Tell the event loop to exit after delay. If delay is NULL, instead exit after we're done running the currently active events.

Definition at line 520 of file compat_libevent.c.

◆ tor_libevent_free_all()

void tor_libevent_free_all ( void )

Un-initialize libevent in preparation for an exit

Definition at line 494 of file compat_libevent.c.

◆ tor_libevent_get_base()

◆ tor_libevent_get_header_version_str()

const char * tor_libevent_get_header_version_str ( void )

Return a string representation of the version of Libevent that was used at compilation time.

Definition at line 218 of file compat_libevent.c.

Referenced by print_library_versions().

◆ tor_libevent_get_method()

const char * tor_libevent_get_method ( void )

Return the name of the Libevent backend we're using.

Definition at line 202 of file compat_libevent.c.

Referenced by run_main_loop_once(), and tor_libevent_initialize().

◆ tor_libevent_get_version_str()

const char * tor_libevent_get_version_str ( void )

Return a string representation of the version of the currently running version of Libevent.

Definition at line 210 of file compat_libevent.c.

Referenced by options_init_from_torrc(), print_library_versions(), and tor_init().

◆ tor_libevent_initialize()

void tor_libevent_initialize ( tor_libevent_cfg_t * torcfg)

Initialize the Libevent library and set up the event base.

Definition at line 133 of file compat_libevent.c.

Referenced by init_libevent().

◆ tor_libevent_is_initialized()

bool tor_libevent_is_initialized ( void )

Return true iff the libevent module has been successfully initialized, and not subsequently shut down.

Definition at line 187 of file compat_libevent.c.

Referenced by hs_client_dir_info_changed(), and tor_shutdown_event_loop_and_exit().

◆ tor_libevent_run_event_loop()

int tor_libevent_run_event_loop ( struct event_base * base,
int once )

Run the event loop for the provided event_base, handling events until something stops it. If once is set, then just poll-and-run once, then exit. Return 0 on success, -1 if an error occurred, or 1 if we exited because no events were pending or active.

This isn't reentrant or multithreaded.

Definition at line 511 of file compat_libevent.c.

Referenced by run_main_loop_once().

Variable Documentation

◆ suppress_msg

const char* suppress_msg = NULL
static

A string which, if it appears in a libevent log, should be ignored.

Definition at line 23 of file compat_libevent.c.

Referenced by libevent_logging_callback(), and suppress_libevent_log_msg().

◆ the_event_base

struct event_base* the_event_base = NULL
static

Global event base for use by the main thread.

Definition at line 80 of file compat_libevent.c.

Referenced by tor_libevent_free_all(), tor_libevent_get_base(), tor_libevent_get_method(), tor_libevent_initialize(), and tor_libevent_is_initialized().