Add the ability to set a default event base, to be used by plugins

which don't have access to the event base.
This commit is contained in:
Todd C. Miller
2017-07-13 13:59:31 -06:00
parent 9a76678317
commit d2a0bfbb12
6 changed files with 40 additions and 4 deletions

View File

@@ -111,6 +111,10 @@ __dso_public struct sudo_event_base *sudo_ev_base_alloc_v1(void);
__dso_public void sudo_ev_base_free_v1(struct sudo_event_base *base);
#define sudo_ev_base_free(_a) sudo_ev_base_free_v1((_a))
/* Set the default event base. */
__dso_public void sudo_ev_base_setdef_v1(struct sudo_event_base *base);
#define sudo_ev_base_setdef(_a) sudo_ev_base_setdef_v1((_a))
/* Allocate a new event. */
__dso_public struct sudo_event *sudo_ev_alloc_v1(int fd, short events, sudo_ev_callback_t callback, void *closure);
#define sudo_ev_alloc(_a, _b, _c, _d) sudo_ev_alloc_v1((_a), (_b), (_c), (_d))

View File

@@ -44,6 +44,9 @@
static void sudo_ev_init(struct sudo_event *ev, int fd, short events,
sudo_ev_callback_t callback, void *closure);
/* Default event base when none is specified. */
static struct sudo_event_base *default_base;
/* We need the event base to be available from the signal handler. */
static struct sudo_event_base *signal_base;
@@ -237,6 +240,16 @@ sudo_ev_base_free_v1(struct sudo_event_base *base)
debug_return;
}
void
sudo_ev_base_setdef_v1(struct sudo_event_base *base)
{
debug_decl(sudo_ev_base_setdef, SUDO_DEBUG_EVENT)
default_base = base;
debug_return;
}
/*
* Clear and fill in a struct sudo_event.
*/
@@ -328,7 +341,7 @@ sudo_ev_handler(int signo, siginfo_t *info, void *context)
}
}
int
static int
sudo_ev_add_signal(struct sudo_event_base *base, struct sudo_event *ev,
bool tohead)
{
@@ -416,14 +429,17 @@ sudo_ev_add_v1(struct sudo_event_base *base, struct sudo_event *ev,
{
debug_decl(sudo_ev_add, SUDO_DEBUG_EVENT)
/* If no base specified, use existing one. */
/* If no base specified, use existing or default base. */
if (base == NULL) {
if (ev->base == NULL) {
if (ev->base != NULL) {
base = ev->base;
} else if (default_base != NULL) {
base = default_base;
} else {
sudo_debug_printf(SUDO_DEBUG_ERROR, "%s: no base specified",
__func__);
debug_return_int(-1);
}
base = ev->base;
}
/* Only add new events to the events list. */

View File

@@ -46,6 +46,7 @@ sudo_ev_add_v1
sudo_ev_alloc_v1
sudo_ev_base_alloc_v1
sudo_ev_base_free_v1
sudo_ev_base_setdef_v1
sudo_ev_del_v1
sudo_ev_free_v1
sudo_ev_get_timeleft_v1

View File

@@ -486,6 +486,11 @@ fill_exec_closure_monitor(struct monitor_closure *mc,
sudo_fatal(NULL);
if (sudo_ev_add(mc->evbase, mc->sigchld_event, NULL, false) == -1)
sudo_fatal(U_("unable to add event to queue"));
/* Clear the default event base. */
sudo_ev_base_setdef(NULL);
debug_return;
}
/*

View File

@@ -302,6 +302,9 @@ fill_exec_closure_nopty(struct exec_closure_nopty *ec,
sudo_fatal(U_("unable to add event to queue"));
#endif
/* Set the default event base. */
sudo_ev_base_setdef(ec->evbase);
debug_return;
}
@@ -313,6 +316,7 @@ free_exec_closure_nopty(struct exec_closure_nopty *ec)
{
debug_decl(free_exec_closure_nopty, SUDO_DEBUG_EXEC)
sudo_ev_base_setdef(NULL);
sudo_ev_base_free(ec->evbase);
sudo_ev_free(ec->errpipe_event);
sudo_ev_free(ec->sigint_event);

View File

@@ -1103,6 +1103,11 @@ fill_exec_closure_pty(struct exec_closure_pty *ec, struct command_status *cstat,
SUDO_EV_WRITE, sigfwd_cb, ec);
if (ec->sigfwd_event == NULL)
sudo_fatal(NULL);
/* Set the default event base. */
sudo_ev_base_setdef(ec->evbase);
debug_return;
}
/*
@@ -1113,6 +1118,7 @@ free_exec_closure_pty(struct exec_closure_pty *ec)
{
debug_decl(free_exec_closure_pty, SUDO_DEBUG_EXEC)
sudo_ev_base_setdef(NULL);
sudo_ev_base_free(ec->evbase);
sudo_ev_free(ec->backchannel_event);
sudo_ev_free(ec->sigint_event);