summaryrefslogtreecommitdiff
path: root/src/libstddjb/selfpipe_trapset.c
blob: ad5ba10c6b80021ec26012dc3accc50eb02dac01 (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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/* ISC license. */

#include <skalibs/sysdeps.h>

#ifdef SKALIBS_HASSIGNALFD

#include <errno.h>
#include <signal.h>
#include <sys/signalfd.h>
#include <skalibs/selfpipe.h>
#include "selfpipe-internal.h"

int selfpipe_trapset (sigset_t const *set)
{
  sigset_t old ;
  if (selfpipe_fd < 0) return (errno = EBADF, -1) ;
  if (sigprocmask(SIG_SETMASK, set, &old) < 0) return -1 ;
  if (signalfd(selfpipe_fd, set, SFD_NONBLOCK | SFD_CLOEXEC) < 0)
  {
    int e = errno ;
    sigprocmask(SIG_SETMASK, &old, 0) ;
    errno = e ;
    return -1 ;
  }
  selfpipe_caught = *set ;
  return 0 ;
}

#else

#include <skalibs/nonposix.h>
#include <errno.h>
#include <signal.h>
#include <skalibs/sig.h>
#include <skalibs/nsig.h>
#include <skalibs/selfpipe.h>
#include "selfpipe-internal.h"

int selfpipe_trapset (sigset_t const *set)
{
  unsigned int i = 1 ;
  if (selfpipe_fd < 0) return (errno = EBADF, -1) ;
  for (; i < SKALIBS_NSIG ; i++)
  {
    int h = sigismember(set, i) ;
    if (h < 0) continue ;
    if (h)
    {
      if (sig_catcha(i, &selfpipe_ssa) < 0) break ;
    }
    else if (sigismember(&selfpipe_caught, i))
    {
      if (sig_restore(i) < 0) break ;
    }
  }
  if (i < SKALIBS_NSIG)
  {
    int e = errno ;
    sig_restoreto(set, i) ;
    errno = e ;
    return -1 ;
  }
  selfpipe_caught = *set ;
  return 0 ;
}

#endif