summaryrefslogtreecommitdiff
path: root/src/libstddjb/sig_catch.c
blob: a11e317c7624c408b4452facf68aea29e6caf596 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/* ISC license. */

/* MT-unsafe */

#include <skalibs/nonposix.h>
#include <skalibs/bsdsnowflake.h>

#include <signal.h>
#include <errno.h>

#include <skalibs/functypes.h>
#include <skalibs/sig.h>
#include <skalibs/nsig.h>

 /*
   BSD fails sigaction() with EINVAL on non-catchable signals, whereas
   every reasonable OS succeeds and simply ignores the signal handler.
 */

int sig_catch (int sig, sig_func_ref f)
{
  struct sigaction action = { .sa_handler = f, .sa_flags = SA_RESTART | SA_NOCLDSTOP } ;
  sigfillset(&action.sa_mask) ;
  return sigaction(sig, &action, 0) >= 0
#ifdef SKALIBS_BSD_SUCKS
 || (errno == EINVAL && sig >= 1 && sig <= NSIG)
#endif
 ;
}