Debian Codegraph @

rpython/translator/c/src/signals.c

raw symbols SCIP
1 #include "src/signals.h"
2
3 #include <limits.h>
4 #include <stdlib.h>
5 #include <errno.h>
6 #ifdef _WIN32
7 #include <process.h>
8 #include <io.h>
9 #include <winsock2.h>
10 #pragma comment(lib,"ws2_32.lib") /* for getsockopt() and send() */
11 #else
12 #include <unistd.h>
13 #include <sys/types.h>
14 #include <sys/socket.h>
15 #endif
16 #include <signal.h>
17
18
19 /* some ifdefs from CPython's signalmodule.c... */
20
21 #if defined(PYOS_OS2) && !defined(PYCC_GCC)
22 #define NSIG 12
23 #include <process.h>
24 #endif
25
26 #ifndef NSIG
27 # if defined(_NSIG)
28 # define NSIG _NSIG /* For BSD/SysV */
29 # elif defined(_SIGMAX)
30 # define NSIG (_SIGMAX + 1) /* For QNX */
31 # elif defined(SIGMAX)
32 # define NSIG (SIGMAX + 1) /* For djgpp */
33 # else
34 # define NSIG 64 /* Use a reasonable default value */
35 # endif
36 #endif
37
38 #define N_LONGBITS (8 * sizeof(long))
39 #define N_LONGSIG ((NSIG - 1) / N_LONGBITS + 1)
40
41 #define PYPYSIG_WITH_NUL_BYTE 0x01
42 #define PYPYSIG_USE_SEND 0x02
43 #define PYPYSIG_NO_WARN_FULL 0x04
44
45 struct pypysig_long_struct pypysig_counter = {
46 { 0L, },
47 "pypysigs",
48 0L,
49 };
50
51 static long volatile pypysig_flags_bits[N_LONGSIG];
52 static int wakeup_fd = -1;
53 static int wakeup_send_flags = PYPYSIG_WITH_NUL_BYTE;
54
55 #undef pypysig_getaddr_occurred
56 void *pypysig_getaddr_occurred(void)
57 {
58 return (void *)(&pypysig_counter);
59 }
60
61 void pypysig_ignore(int signum)
62 {
63 #ifdef SA_RESTART
64 /* assume sigaction exists */
65 struct sigaction context;
66 context.sa_handler = SIG_IGN;
67 sigemptyset(&context.sa_mask);
68 context.sa_flags = 0;
69 sigaction(signum, &context, NULL);
70 #else
71 signal(signum, SIG_IGN);
72 #endif
73 }
74
75 void pypysig_default(int signum)
76 {
77 #ifdef SA_RESTART
78 /* assume sigaction exists */
79 struct sigaction context;
80 context.sa_handler = SIG_DFL;
81 sigemptyset(&context.sa_mask);
82 context.sa_flags = 0;
83 sigaction(signum, &context, NULL);
84 #else
85 signal(signum, SIG_DFL);
86 #endif
87 }
88
89 #ifdef _WIN32
90 #include <Windows.h>
91 #define atomic_cas(ptr, oldv, newv) (InterlockedCompareExchange(ptr, \
92 newv, oldv) == (oldv))
93 #else
94 #define atomic_cas(ptr, oldv, newv) __sync_bool_compare_and_swap(ptr, \
95 oldv, newv)
96 #endif
97
98 void pypysig_pushback(int signum)
99 {
100 if (0 <= signum && signum < NSIG)
101 {
102 int ok, index = signum / N_LONGBITS;
103 unsigned long bitmask = 1UL << (signum % N_LONGBITS);
104 do
105 {
106 long value = pypysig_flags_bits[index];
107 if (value & bitmask)
108 break; /* already set */
109 ok = atomic_cas(&pypysig_flags_bits[index], value, value | bitmask);
110 } while (!ok);
111
112 pypysig_counter.inner.value = -1;
113 }
114 }
115
116 static void write_str(int fd, const char *p)
117 {
118 int i = 0;
119 int res RPY_UNUSED;
120 while (p[i] != '\x00')
121 i++;
122 res = write(fd, p, i);
123 }
124
125 #ifdef _WIN32
126 /* this is set manually from pypy3's module/time/interp_time */
127 HANDLE pypy_sigint_interrupt_event = NULL;
128 #endif
129
130 /* Stores the errno from a failed wakeup-fd write so it can be reported
131 at the next Python checkpoint via sys.stderr rather than directly to fd 2. */
132 static volatile int pypysig_wakeup_fd_write_errno = 0;
133
134 int pypysig_get_wakeup_fd_write_errno(void) {
135 int e = pypysig_wakeup_fd_write_errno;
136 pypysig_wakeup_fd_write_errno = 0;
137 return e;
138 }
139
140 static void signal_setflag_handler(int signum)
141 {
142 pypysig_pushback(signum);
143
144 /* Warning, this logic needs to be async-signal-safe */
145 if (wakeup_fd != -1) {
146 #ifndef _WIN32
147 ssize_t res;
148 #else
149 int res;
150 #endif
151 int old_errno = errno;
152 unsigned char byte = (unsigned char)signum;
153 if (wakeup_send_flags & PYPYSIG_WITH_NUL_BYTE)
154 byte = 0;
155 retry:
156 if (wakeup_send_flags & PYPYSIG_USE_SEND)
157 res = send(wakeup_fd, &byte, 1, 0);
158 else
159 res = write(wakeup_fd, &byte, 1);
160 if (res < 0 && ((wakeup_send_flags & PYPYSIG_NO_WARN_FULL) == 0 ||
161 errno != EAGAIN)) {
162 if (errno == EINTR)
163 goto retry;
164 /* Store errno for reporting at the next Python checkpoint */
165 pypysig_wakeup_fd_write_errno = errno;
166 }
167 errno = old_errno;
168 }
169
170 #ifdef _WIN32
171 if (signum == SIGINT && pypy_sigint_interrupt_event)
172 SetEvent(pypy_sigint_interrupt_event);
173 #endif
174 }
175
176 void pypysig_setflag(int signum)
177 {
178 #ifdef SA_RESTART
179 /* assume sigaction exists */
180 struct sigaction context;
181 context.sa_handler = signal_setflag_handler;
182 sigemptyset(&context.sa_mask);
183 context.sa_flags = 0;
184 sigaction(signum, &context, NULL);
185 #else
186 signal(signum, signal_setflag_handler);
187 #endif
188 }
189
190 void pypysig_reinstall(int signum)
191 {
192 #ifdef SA_RESTART
193 /* Assume sigaction was used. We did not pass SA_RESETHAND to
194 sa_flags, so there is nothing to do here. */
195 #else
196 # ifdef SIGCHLD
197 /* To avoid infinite recursion, this signal remains
198 reset until explicitly re-instated. (Copied from CPython) */
199 if (signum != SIGCHLD)
200 # endif
201 pypysig_setflag(signum);
202 #endif
203 }
204
205 int pypysig_poll(void)
206 {
207 int index;
208 for (index = 0; index < N_LONGSIG; index++) {
209 long value;
210 retry:
211 value = pypysig_flags_bits[index];
212 if (value != 0L) {
213 int j = 0;
214 while ((value & (1UL << j)) == 0)
215 j++;
216 if (!atomic_cas(&pypysig_flags_bits[index], value,
217 value & ~(1UL << j)))
218 goto retry;
219 return index * N_LONGBITS + j;
220 }
221 }
222 return -1; /* no pending signal */
223 }
224
225 #ifndef _WIN32
226 /* siginterrupt(2) is deprecated on Linux (glibc 2.21+); implement the
227 * same semantics via sigaction so we avoid the -Wdeprecated-declarations
228 * warning. Falls back to the raw syscall on platforms without SA_RESTART. */
229 int pypysig_siginterrupt(int sig, int flag)
230 {
231 #ifdef SA_RESTART
232 struct sigaction act;
233 if (sigaction(sig, NULL, &act) < 0)
234 return -1;
235 if (flag)
236 act.sa_flags &= ~SA_RESTART;
237 else
238 act.sa_flags |= SA_RESTART;
239 return sigaction(sig, &act, NULL);
240 #else
241 return siginterrupt(sig, flag);
242 #endif
243 }
244 #endif
245
246 int pypysig_set_wakeup_fd(int fd, int send_flags)
247 {
248 int old_fd = wakeup_fd;
249 #ifdef _WIN32
250 /* check if 'fd' seems to be a socket handle instead of a file handle.
251 The call to getsockopt() can fail for a number of reasons, starting
252 from WSAStartup() never called. CPython is more careful about
253 distinguishing error cases. We just assume that a failure means
254 that the fd is not a socket descriptor but a file descriptor.
255
256 Win64 note: in theory, socket descriptors could be 64-bit there,
257 but in practice they always fit in 32-bit so it shouldn't really
258 be a problem according to:
259 https://stackoverflow.com/questions/1953639/
260 is-it-safe-to-cast-socket-to-int-under-win64
261 */
262 if (fd != -1) {
263 int res;
264 int res_size = sizeof res;
265 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, (char *)&res, &res_size) == 0)
266 send_flags |= PYPYSIG_USE_SEND;
267 }
268 #endif
269 wakeup_fd = fd;
270 wakeup_send_flags = send_flags;
271 return old_fd;
272 }