Skip to content

Commit

Permalink
Add new evsignal_add_with_flags() for signal events
Browse files Browse the repository at this point in the history
By default libevent set SA_RESTART flag for sigaction(2), which will restart
syscall automatically, and this is not always good.
(That because it need to be compitable with signal(2), while it vary
across systems)

This patch add new function evsignal_add_with_flags(), which will change
this flag from default SA_RESTART to user defined.
  • Loading branch information
azat committed Jun 29, 2014
1 parent 6ec9492 commit cee5796
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 6 deletions.
3 changes: 2 additions & 1 deletion event-internal.h
Expand Up @@ -52,6 +52,7 @@ extern "C" {
/* used only by signals */
#define ev_ncalls ev_.ev_signal.ev_ncalls
#define ev_pncalls ev_.ev_signal.ev_pncalls
#define ev_sa_flags ev_.ev_signal.sa_flags

#define ev_pri ev_evcallback.evcb_pri
#define ev_flags ev_evcallback.evcb_flags
Expand Down Expand Up @@ -400,7 +401,7 @@ struct event_config {
((base)->event_count_active)

int evsig_set_handler_(struct event_base *base, int evsignal,
void (*fn)(int));
void (*fn)(int), int);
int evsig_restore_handler_(struct event_base *base, int evsignal);

int event_add_nolock_(struct event *ev,
Expand Down
10 changes: 10 additions & 0 deletions event.c
Expand Up @@ -2090,6 +2090,8 @@ event_new(struct event_base *base, evutil_socket_t fd, short events, void (*cb)(
ev = mm_malloc(sizeof(struct event));
if (ev == NULL)
return (NULL);

ev->ev_sa_flags = SA_RESTART;
if (event_assign(ev, base, fd, events, cb, arg) < 0) {
mm_free(ev);
return (NULL);
Expand Down Expand Up @@ -2378,6 +2380,14 @@ event_add(struct event *ev, const struct timeval *tv)
return (res);
}

int
evsignal_add_with_flags(struct event *ev, const struct timeval *tv, int flags)
{
ev->ev_sa_flags = flags;

return evsignal_add(ev, tv);
}

/* Helper callback: wake an event_base from another thread. This version
* works by writing a byte to one end of a socketpair, so that the event_base
* listening on the other end will wake up as the corresponding event
Expand Down
2 changes: 1 addition & 1 deletion evmap.c
Expand Up @@ -454,7 +454,7 @@ evmap_signal_add_(struct event_base *base, int sig, struct event *ev)
base->evsigsel->fdinfo_len);

if (LIST_EMPTY(&ctx->events)) {
if (evsel->add(base, ev->ev_fd, 0, EV_SIGNAL, NULL)
if (evsel->add(base, ev->ev_fd, 0, EV_SIGNAL, (void *)&ev->ev_sa_flags)
== -1)
return (-1);
}
Expand Down
7 changes: 7 additions & 0 deletions include/event2/event.h
Expand Up @@ -968,6 +968,13 @@ int event_base_got_break(struct event_base *);
#define evtimer_initialized(ev) event_initialized(ev)
/**@}*/


/**
* Change default sa_flags for sigaction(2) from SA_RESTART
* to user-specific.
*/
int evsignal_add_with_flags(struct event *, const struct timeval *, int);

/**
@name evsignal_* macros
Expand Down
2 changes: 2 additions & 0 deletions include/event2/event_struct.h
Expand Up @@ -145,6 +145,8 @@ struct event {
short ev_ncalls;
/* Allows deletes in callback */
short *ev_pncalls;
/* Passed to sigaction(). */
int sa_flags;
} ev_signal;
} ev_;

Expand Down
9 changes: 5 additions & 4 deletions signal.c
Expand Up @@ -210,7 +210,8 @@ evsig_init_(struct event_base *base)
* we can restore the original handler when we clear the current one. */
int
evsig_set_handler_(struct event_base *base,
int evsignal, void (__cdecl *handler)(int))
int evsignal, void (__cdecl *handler)(int),
int sa_flags)
{
#ifdef EVENT__HAVE_SIGACTION
struct sigaction sa;
Expand Down Expand Up @@ -252,7 +253,7 @@ evsig_set_handler_(struct event_base *base,
#ifdef EVENT__HAVE_SIGACTION
memset(&sa, 0, sizeof(sa));
sa.sa_handler = handler;
sa.sa_flags |= SA_RESTART;
sa.sa_flags |= sa_flags;
sigfillset(&sa.sa_mask);

if (sigaction(evsignal, &sa, sig->sh_old[evsignal]) == -1) {
Expand All @@ -278,7 +279,7 @@ static int
evsig_add(struct event_base *base, evutil_socket_t evsignal, short old, short events, void *p)
{
struct evsig_info *sig = &base->sig;
(void)p;
int sa_flags = *(int *)p;

EVUTIL_ASSERT(evsignal >= 0 && evsignal < NSIG);

Expand All @@ -299,7 +300,7 @@ evsig_add(struct event_base *base, evutil_socket_t evsignal, short old, short ev
EVSIGBASE_UNLOCK();

event_debug(("%s: %d: changing signal handler", __func__, (int)evsignal));
if (evsig_set_handler_(base, (int)evsignal, evsig_handler) == -1) {
if (evsig_set_handler_(base, (int)evsignal, evsig_handler, sa_flags) == -1) {
goto err;
}

Expand Down

0 comments on commit cee5796

Please sign in to comment.