summaryrefslogtreecommitdiff
path: root/src/libstddjb/selfpipe.c
blob: c4609c84c6a4b9bd6eb5ec8fc8113519b9a1a67a (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/* ISC license. */

/* MT-unsafe */

#include <skalibs/sysdeps.h>

#ifdef SKALIBS_HASSIGNALFD

#include <errno.h>
#include <signal.h>
#include <sys/signalfd.h>

#include <skalibs/allreadwrite.h>
#include <skalibs/sig.h>
#include <skalibs/djbunix.h>

struct selfpipe_s
{
  sigset_t caught ;
  int fd ;
} ;

static struct selfpipe_s sp = { .fd = -1 } ;

int selfpipe_fd ()
{
  return sp.fd ;
}

int selfpipe_read ()
{
  struct signalfd_siginfo buf ;
  ssize_t r = sanitize_read(fd_read(sp.fd, (char *)&buf, sizeof(struct signalfd_siginfo))) ;
  return (r <= 0) ? r : buf.ssi_signo ;
}

int selfpipe_trap (int sig)
{
  sigset_t set = sp.caught ;
  sigset_t old ;
  if (sp.fd == -1) return (errno = EBADF, 0) ;
  if (sigaddset(&set, sig) == -1 || sigprocmask(SIG_BLOCK, &set, &old) == -1) return 0 ;
  if (signalfd(sp.fd, &set, SFD_NONBLOCK | SFD_CLOEXEC) == -1)
  {
    int e = errno ;
    sigprocmask(SIG_SETMASK, &old, 0) ;
    errno = e ;
    return 0 ;
  }
  sp.caught = set ;
  return 1 ;
}

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

void selfpipe_finish ()
{
  int e = errno ;
  fd_close(sp.fd) ; sp.fd = -1 ;
  sigprocmask(SIG_UNBLOCK, &sp.caught, 0) ;
  sigemptyset(&sp.caught) ;
  errno = e ;
}

int selfpipe_init ()
{
  sigemptyset(&sp.caught) ;
  sig_blocknone() ;
  sp.fd = signalfd(sp.fd, &sp.caught, SFD_NONBLOCK | SFD_CLOEXEC) ;
  return sp.fd ;
}

#else

#include <skalibs/nonposix.h>

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

#include <skalibs/nsig.h>
#include <skalibs/sig.h>
#include <skalibs/allreadwrite.h>
#include <skalibs/djbunix.h>

struct selfpipe_s
{
  sigset_t caught ;
  int fd[2] ;
} ;

static struct selfpipe_s sp = { .fd = { -1, -1 } } ;

static void selfpipe_tophalf (int s)
{
  int e = errno ;
  unsigned char c = (unsigned char)s ;
  write(sp.fd[1], (char *)&c, 1) ;
  errno = e ;
}

int selfpipe_fd ()
{
  return sp.fd[0] ;
}

int selfpipe_read ()
{
  char c ;
  ssize_t r = sanitize_read((fd_read(sp.fd[0], &c, 1))) ;
  return (r <= 0) ? r : c ;
}

int selfpipe_trap (int sig)
{
  if (sp.fd[0] == -1) return (errno = EBADF, 0) ;
  if (!sig_catch(sig, &selfpipe_tophalf)) return 0 ;
  sigaddset(&sp.caught, sig) ;
  sig_unblock(sig) ;
  return 1 ;
}

int selfpipe_trapset (sigset_t const *set)
{
  unsigned int i = 1 ;
  if (sp.fd[0] == -1) return (errno = EBADF, 0) ;
  for (; i < SKALIBS_NSIG ; i++)
  {
    int h = sigismember(set, i) ;
    if (h < 0) continue ;
    if (h)
    {
      if (!sig_catch(i, &selfpipe_tophalf)) goto err ;
    }
    else if (sigismember(&sp.caught, i))
    {
      if (!sig_restore(i)) goto err ;
    }
  }
  sig_blocknone() ;
  sp.caught = *set ;
  return 1 ;

 err:
  sig_restoreto(set, i) ;
  return 0 ;
}

void selfpipe_finish ()
{
  int e = errno ;
  sigprocmask(SIG_BLOCK, &sp.caught, 0) ;
  sig_restoreto(&sp.caught, SKALIBS_NSIG) ;
  fd_close(sp.fd[1]) ;
  fd_close(sp.fd[0]) ;
  sigprocmask(SIG_UNBLOCK, &sp.caught, 0) ;
  sigemptyset(&sp.caught) ;
  sp.fd[0] = sp.fd[1] = -1 ;
  errno = e ;
}

int selfpipe_init ()
{
  if (sp.fd[0] >= 0) selfpipe_finish() ;
  else sigemptyset(&sp.caught) ;
  sig_blocknone() ;
  return pipenbcoe(sp.fd) < 0 ? -1 : sp.fd[0] ;
}

#endif