From: Mark Stapp Date: Wed, 2 Sep 2020 20:25:00 +0000 (-0400) Subject: lib: add sigevent_check api X-Git-Tag: base_7.6~336^2~2 X-Git-Url: https://git.puffer.fish/?a=commitdiff_plain;h=976c5cc134bb96f309479d281d771d70dbe41f13;p=mirror%2Ffrr.git lib: add sigevent_check api Add an api that blocks application-handled signals (SIGINT, SIGTERM, e.g.) then tests whether any signals have been received. This helps to manage a race between signal reception and the poll call in the main event loop. Signed-off-by: Mark Stapp --- diff --git a/lib/sigevent.c b/lib/sigevent.c index 04fcc814ef..de9e1f5410 100644 --- a/lib/sigevent.c +++ b/lib/sigevent.c @@ -63,6 +63,33 @@ static void quagga_signal_handler(int signo) sigmaster.caught = 1; } +/* + * Check whether any signals have been received and are pending. This is done + * with the application's key signals blocked. The complete set of signals + * is returned in 'setp', so the caller can restore them when appropriate. + * If there are pending signals, returns 'true', 'false' otherwise. + */ +bool frr_sigevent_check(sigset_t *setp) +{ + sigset_t blocked; + int i; + bool ret; + + sigemptyset(setp); + sigemptyset(&blocked); + + /* Set up mask of application's signals */ + for (i = 0; i < sigmaster.sigc; i++) + sigaddset(&blocked, sigmaster.signals[i].signal); + + pthread_sigmask(SIG_BLOCK, &blocked, setp); + + /* Now that the application's signals are blocked, test. */ + ret = (sigmaster.caught != 0); + + return ret; +} + /* check if signals have been caught and run appropriate handlers */ int quagga_sigevent_process(void) { diff --git a/lib/sigevent.h b/lib/sigevent.h index a0ad88fcaa..4a39b22889 100644 --- a/lib/sigevent.h +++ b/lib/sigevent.h @@ -48,6 +48,15 @@ struct quagga_signal_t { extern void signal_init(struct thread_master *m, int sigc, struct quagga_signal_t *signals); + +/* + * Check whether any signals have been received and are pending. This is done + * with the application's key signals blocked. The complete set of signals + * is returned in 'setp', so the caller can restore them when appropriate. + * If there are pending signals, returns 'true', 'false' otherwise. + */ +bool frr_sigevent_check(sigset_t *setp); + /* check whether there are signals to handle, process any found */ extern int quagga_sigevent_process(void);