#include "orconfig.h"
#define COMPAT_LIBEVENT_PRIVATE
#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>
static const char *suppress_msg = NULL;
STATIC void
libevent_logging_callback(int severity, const char *msg)
{
char buf[1024];
size_t n;
if (suppress_msg && strstr(msg, suppress_msg))
return;
n = strlcpy(buf, msg, sizeof(buf));
if (n && n < sizeof(buf) && buf[n-1] == '\n') {
buf[n-1] = '\0';
}
switch (severity) {
case _EVENT_LOG_DEBUG:
log_debug(LD_NOCB|LD_NET, "Message from libevent: %s", buf);
break;
case _EVENT_LOG_MSG:
log_info(LD_NOCB|LD_NET, "Message from libevent: %s", buf);
break;
case _EVENT_LOG_WARN:
log_warn(LD_NOCB|LD_GENERAL, "Warning from libevent: %s", buf);
break;
case _EVENT_LOG_ERR:
log_err(LD_NOCB|LD_GENERAL, "Error from libevent: %s", buf);
break;
default:
log_warn(LD_NOCB|LD_GENERAL, "Message [%d] from libevent: %s",
severity, buf);
break;
}
}
void
configure_libevent_logging(void)
{
event_set_log_callback(libevent_logging_callback);
}
void
suppress_libevent_log_msg(const char *msg)
{
suppress_msg = msg;
}
void
tor_event_free_(struct event *ev)
{
if (ev == NULL)
return;
event_free(ev);
}
static struct event_base *the_event_base = NULL;
static struct event *rescan_mainloop_ev = NULL;
static void
rescan_mainloop_cb(evutil_socket_t fd, short events, void *arg)
{
(void)fd;
(void)events;
struct event_base *the_base = arg;
event_base_loopbreak(the_base);
}
#ifdef __APPLE__
#ifdef __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__
#define MACOSX_KQUEUE_IS_BROKEN \
(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 1040)
#else
#define MACOSX_KQUEUE_IS_BROKEN 0
#endif
#endif
void
tor_libevent_initialize(tor_libevent_cfg_t *torcfg)
{
tor_assert(the_event_base == NULL);
(void)torcfg;
{
int attempts = 0;
struct event_config *cfg;
++attempts;
cfg = event_config_new();
tor_assert(cfg);
event_config_set_flag(cfg, EVENT_BASE_FLAG_NOLOCK);
if (torcfg->num_cpus > 0)
event_config_set_num_cpus_hint(cfg, torcfg->num_cpus);
event_config_set_flag(cfg, EVENT_BASE_FLAG_EPOLL_USE_CHANGELIST);
the_event_base = event_base_new_with_config(cfg);
event_config_free(cfg);
}
if (!the_event_base) {
log_err(LD_GENERAL, "Unable to initialize Libevent: cannot continue.");
exit(1);
}
rescan_mainloop_ev = event_new(the_event_base, -1, 0,
rescan_mainloop_cb, the_event_base);
if (!rescan_mainloop_ev) {
log_err(LD_GENERAL, "Unable to create rescan event: cannot continue.");
exit(1);
}
log_info(LD_GENERAL,
"Initialized libevent version %s using method %s. Good.",
event_get_version(), tor_libevent_get_method());
}
bool
tor_libevent_is_initialized(void)
{
return the_event_base != NULL;
}
MOCK_IMPL(struct event_base *,
tor_libevent_get_base, (void))
{
tor_assert(the_event_base != NULL);
return the_event_base;
}
const char *
tor_libevent_get_method(void)
{
return event_base_get_method(the_event_base);
}
const char *
tor_libevent_get_version_str(void)
{
return event_get_version();
}
const char *
tor_libevent_get_header_version_str(void)
{
return LIBEVENT_VERSION;
}
struct periodic_timer_t {
struct event *ev;
void (*cb)(struct periodic_timer_t *, void *);
void *data;
};
static void
periodic_timer_cb(evutil_socket_t fd, short what, void *arg)
{
periodic_timer_t *timer = arg;
(void) what;
(void) fd;
timer->cb(timer, timer->data);
}
periodic_timer_t *
periodic_timer_new(struct event_base *base,
const struct timeval *tv,
void (*cb)(periodic_timer_t *timer, void *data),
void *data)
{
periodic_timer_t *timer;
tor_assert(base);
tor_assert(tv);
tor_assert(cb);
timer = tor_malloc_zero(sizeof(periodic_timer_t));
if (!(timer->ev = tor_event_new(base, -1, EV_PERSIST,
periodic_timer_cb, timer))) {
tor_free(timer);
return NULL;
}
timer->cb = cb;
timer->data = data;
periodic_timer_launch(timer, tv);
return timer;
}
void
periodic_timer_launch(periodic_timer_t *timer, const struct timeval *tv)
{
tor_assert(timer);
if (event_pending(timer->ev, EV_TIMEOUT, NULL))
return;
event_add(timer->ev, tv);
}
void
periodic_timer_disable(periodic_timer_t *timer)
{
tor_assert(timer);
(void) event_del(timer->ev);
}
void
periodic_timer_free_(periodic_timer_t *timer)
{
if (!timer)
return;
tor_event_free(timer->ev);
tor_free(timer);
}
struct mainloop_event_t {
struct event *ev;
void (*cb)(mainloop_event_t *, void *);
void *userdata;
};
static void
mainloop_event_cb(evutil_socket_t fd, short what, void *arg)
{
(void)fd;
(void)what;
mainloop_event_t *mev = arg;
mev->cb(mev, mev->userdata);
}
static void
mainloop_event_postloop_cb(evutil_socket_t fd, short what, void *arg)
{
(void)fd;
(void)what;
event_active(rescan_mainloop_ev, EV_READ, 1);
mainloop_event_t *mev = arg;
mev->cb(mev, mev->userdata);
}
static mainloop_event_t *
mainloop_event_new_impl(int postloop,
void (*cb)(mainloop_event_t *, void *),
void *userdata)
{
tor_assert(cb);
struct event_base *base = tor_libevent_get_base();
mainloop_event_t *mev = tor_malloc_zero(sizeof(mainloop_event_t));
mev->ev = tor_event_new(base, -1, 0,
postloop ? mainloop_event_postloop_cb : mainloop_event_cb,
mev);
tor_assert(mev->ev);
mev->cb = cb;
mev->userdata = userdata;
return mev;
}
mainloop_event_t *
mainloop_event_new(void (*cb)(mainloop_event_t *, void *),
void *userdata)
{
return mainloop_event_new_impl(0, cb, userdata);
}
mainloop_event_t *
mainloop_event_postloop_new(void (*cb)(mainloop_event_t *, void *),
void *userdata)
{
return mainloop_event_new_impl(1, cb, userdata);
}
void
mainloop_event_activate(mainloop_event_t *event)
{
tor_assert(event);
event_active(event->ev, EV_READ, 1);
}
int
mainloop_event_schedule(mainloop_event_t *event, const struct timeval *tv)
{
tor_assert(event);
if (BUG(tv == NULL)) {
mainloop_event_activate(event);
return 0;
}
return event_add(event->ev, tv);
}
void
mainloop_event_cancel(mainloop_event_t *event)
{
if (!event)
return;
(void) event_del(event->ev);
}
void
mainloop_event_free_(mainloop_event_t *event)
{
if (!event)
return;
tor_event_free(event->ev);
memset(event, 0xb8, sizeof(*event));
tor_free(event);
}
int
tor_init_libevent_rng(void)
{
int rv = 0;
char buf[256];
if (evutil_secure_rng_init() < 0) {
rv = -1;
}
crypto_rand(buf, 32);
#ifdef HAVE_EVUTIL_SECURE_RNG_ADD_BYTES
evutil_secure_rng_add_bytes(buf, 32);
#endif
evutil_secure_rng_get_bytes(buf, sizeof(buf));
return rv;
}
void
tor_libevent_free_all(void)
{
tor_event_free(rescan_mainloop_ev);
if (the_event_base)
event_base_free(the_event_base);
the_event_base = NULL;
}
int
tor_libevent_run_event_loop(struct event_base *base, int once)
{
const int flags = once ? EVLOOP_ONCE : 0;
return event_base_loop(base, flags);
}
void
tor_libevent_exit_loop_after_delay(struct event_base *base,
const struct timeval *delay)
{
event_base_loopexit(base, delay);
}
void
tor_libevent_exit_loop_after_callback(struct event_base *base)
{
event_base_loopbreak(base);
}
#if defined(TOR_UNIT_TESTS)
void
tor_libevent_postfork(void)
{
int r = event_reinit(tor_libevent_get_base());
tor_assert(r == 0);
}
#endif