1 | n/a | |
---|
2 | n/a | /* Signal module -- many thanks to Lance Ellinghaus */ |
---|
3 | n/a | |
---|
4 | n/a | /* XXX Signals should be recorded per thread, now we have thread state. */ |
---|
5 | n/a | |
---|
6 | n/a | #include "Python.h" |
---|
7 | n/a | #ifndef MS_WINDOWS |
---|
8 | n/a | #include "posixmodule.h" |
---|
9 | n/a | #endif |
---|
10 | n/a | #ifdef MS_WINDOWS |
---|
11 | n/a | #include "socketmodule.h" /* needed for SOCKET_T */ |
---|
12 | n/a | #endif |
---|
13 | n/a | |
---|
14 | n/a | #ifdef MS_WINDOWS |
---|
15 | n/a | #include <windows.h> |
---|
16 | n/a | #ifdef HAVE_PROCESS_H |
---|
17 | n/a | #include <process.h> |
---|
18 | n/a | #endif |
---|
19 | n/a | #endif |
---|
20 | n/a | |
---|
21 | n/a | #ifdef HAVE_SIGNAL_H |
---|
22 | n/a | #include <signal.h> |
---|
23 | n/a | #endif |
---|
24 | n/a | #ifdef HAVE_SYS_STAT_H |
---|
25 | n/a | #include <sys/stat.h> |
---|
26 | n/a | #endif |
---|
27 | n/a | #ifdef HAVE_SYS_TIME_H |
---|
28 | n/a | #include <sys/time.h> |
---|
29 | n/a | #endif |
---|
30 | n/a | |
---|
31 | n/a | #if defined(HAVE_PTHREAD_SIGMASK) && !defined(HAVE_BROKEN_PTHREAD_SIGMASK) |
---|
32 | n/a | # define PYPTHREAD_SIGMASK |
---|
33 | n/a | #endif |
---|
34 | n/a | |
---|
35 | n/a | #if defined(PYPTHREAD_SIGMASK) && defined(HAVE_PTHREAD_H) |
---|
36 | n/a | # include <pthread.h> |
---|
37 | n/a | #endif |
---|
38 | n/a | |
---|
39 | n/a | #ifndef SIG_ERR |
---|
40 | n/a | #define SIG_ERR ((PyOS_sighandler_t)(-1)) |
---|
41 | n/a | #endif |
---|
42 | n/a | |
---|
43 | n/a | #ifndef NSIG |
---|
44 | n/a | # if defined(_NSIG) |
---|
45 | n/a | # define NSIG _NSIG /* For BSD/SysV */ |
---|
46 | n/a | # elif defined(_SIGMAX) |
---|
47 | n/a | # define NSIG (_SIGMAX + 1) /* For QNX */ |
---|
48 | n/a | # elif defined(SIGMAX) |
---|
49 | n/a | # define NSIG (SIGMAX + 1) /* For djgpp */ |
---|
50 | n/a | # else |
---|
51 | n/a | # define NSIG 64 /* Use a reasonable default value */ |
---|
52 | n/a | # endif |
---|
53 | n/a | #endif |
---|
54 | n/a | |
---|
55 | n/a | #include "clinic/signalmodule.c.h" |
---|
56 | n/a | |
---|
57 | n/a | /*[clinic input] |
---|
58 | n/a | module signal |
---|
59 | n/a | [clinic start generated code]*/ |
---|
60 | n/a | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=b0301a3bde5fe9d3]*/ |
---|
61 | n/a | |
---|
62 | n/a | |
---|
63 | n/a | /* |
---|
64 | n/a | NOTES ON THE INTERACTION BETWEEN SIGNALS AND THREADS |
---|
65 | n/a | |
---|
66 | n/a | When threads are supported, we want the following semantics: |
---|
67 | n/a | |
---|
68 | n/a | - only the main thread can set a signal handler |
---|
69 | n/a | - any thread can get a signal handler |
---|
70 | n/a | - signals are only delivered to the main thread |
---|
71 | n/a | |
---|
72 | n/a | I.e. we don't support "synchronous signals" like SIGFPE (catching |
---|
73 | n/a | this doesn't make much sense in Python anyway) nor do we support |
---|
74 | n/a | signals as a means of inter-thread communication, since not all |
---|
75 | n/a | thread implementations support that (at least our thread library |
---|
76 | n/a | doesn't). |
---|
77 | n/a | |
---|
78 | n/a | We still have the problem that in some implementations signals |
---|
79 | n/a | generated by the keyboard (e.g. SIGINT) are delivered to all |
---|
80 | n/a | threads (e.g. SGI), while in others (e.g. Solaris) such signals are |
---|
81 | n/a | delivered to one random thread (an intermediate possibility would |
---|
82 | n/a | be to deliver it to the main thread -- POSIX?). For now, we have |
---|
83 | n/a | a working implementation that works in all three cases -- the |
---|
84 | n/a | handler ignores signals if getpid() isn't the same as in the main |
---|
85 | n/a | thread. XXX This is a hack. |
---|
86 | n/a | */ |
---|
87 | n/a | |
---|
88 | n/a | #ifdef WITH_THREAD |
---|
89 | n/a | #include <sys/types.h> /* For pid_t */ |
---|
90 | n/a | #include "pythread.h" |
---|
91 | n/a | static long main_thread; |
---|
92 | n/a | static pid_t main_pid; |
---|
93 | n/a | #endif |
---|
94 | n/a | |
---|
95 | n/a | static volatile struct { |
---|
96 | n/a | sig_atomic_t tripped; |
---|
97 | n/a | PyObject *func; |
---|
98 | n/a | } Handlers[NSIG]; |
---|
99 | n/a | |
---|
100 | n/a | #ifdef MS_WINDOWS |
---|
101 | n/a | #define INVALID_FD ((SOCKET_T)-1) |
---|
102 | n/a | |
---|
103 | n/a | static volatile struct { |
---|
104 | n/a | SOCKET_T fd; |
---|
105 | n/a | int use_send; |
---|
106 | n/a | int send_err_set; |
---|
107 | n/a | int send_errno; |
---|
108 | n/a | int send_win_error; |
---|
109 | n/a | } wakeup = {INVALID_FD, 0, 0}; |
---|
110 | n/a | #else |
---|
111 | n/a | #define INVALID_FD (-1) |
---|
112 | n/a | static volatile sig_atomic_t wakeup_fd = -1; |
---|
113 | n/a | #endif |
---|
114 | n/a | |
---|
115 | n/a | /* Speed up sigcheck() when none tripped */ |
---|
116 | n/a | static volatile sig_atomic_t is_tripped = 0; |
---|
117 | n/a | |
---|
118 | n/a | static PyObject *DefaultHandler; |
---|
119 | n/a | static PyObject *IgnoreHandler; |
---|
120 | n/a | static PyObject *IntHandler; |
---|
121 | n/a | |
---|
122 | n/a | /* On Solaris 8, gcc will produce a warning that the function |
---|
123 | n/a | declaration is not a prototype. This is caused by the definition of |
---|
124 | n/a | SIG_DFL as (void (*)())0; the correct declaration would have been |
---|
125 | n/a | (void (*)(int))0. */ |
---|
126 | n/a | |
---|
127 | n/a | static PyOS_sighandler_t old_siginthandler = SIG_DFL; |
---|
128 | n/a | |
---|
129 | n/a | #ifdef MS_WINDOWS |
---|
130 | n/a | static HANDLE sigint_event = NULL; |
---|
131 | n/a | #endif |
---|
132 | n/a | |
---|
133 | n/a | #ifdef HAVE_GETITIMER |
---|
134 | n/a | static PyObject *ItimerError; |
---|
135 | n/a | |
---|
136 | n/a | /* auxiliary functions for setitimer/getitimer */ |
---|
137 | n/a | static void |
---|
138 | n/a | timeval_from_double(double d, struct timeval *tv) |
---|
139 | n/a | { |
---|
140 | n/a | tv->tv_sec = floor(d); |
---|
141 | n/a | tv->tv_usec = fmod(d, 1.0) * 1000000.0; |
---|
142 | n/a | } |
---|
143 | n/a | |
---|
144 | n/a | Py_LOCAL_INLINE(double) |
---|
145 | n/a | double_from_timeval(struct timeval *tv) |
---|
146 | n/a | { |
---|
147 | n/a | return tv->tv_sec + (double)(tv->tv_usec / 1000000.0); |
---|
148 | n/a | } |
---|
149 | n/a | |
---|
150 | n/a | static PyObject * |
---|
151 | n/a | itimer_retval(struct itimerval *iv) |
---|
152 | n/a | { |
---|
153 | n/a | PyObject *r, *v; |
---|
154 | n/a | |
---|
155 | n/a | r = PyTuple_New(2); |
---|
156 | n/a | if (r == NULL) |
---|
157 | n/a | return NULL; |
---|
158 | n/a | |
---|
159 | n/a | if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_value)))) { |
---|
160 | n/a | Py_DECREF(r); |
---|
161 | n/a | return NULL; |
---|
162 | n/a | } |
---|
163 | n/a | |
---|
164 | n/a | PyTuple_SET_ITEM(r, 0, v); |
---|
165 | n/a | |
---|
166 | n/a | if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_interval)))) { |
---|
167 | n/a | Py_DECREF(r); |
---|
168 | n/a | return NULL; |
---|
169 | n/a | } |
---|
170 | n/a | |
---|
171 | n/a | PyTuple_SET_ITEM(r, 1, v); |
---|
172 | n/a | |
---|
173 | n/a | return r; |
---|
174 | n/a | } |
---|
175 | n/a | #endif |
---|
176 | n/a | |
---|
177 | n/a | static PyObject * |
---|
178 | n/a | signal_default_int_handler(PyObject *self, PyObject *args) |
---|
179 | n/a | { |
---|
180 | n/a | PyErr_SetNone(PyExc_KeyboardInterrupt); |
---|
181 | n/a | return NULL; |
---|
182 | n/a | } |
---|
183 | n/a | |
---|
184 | n/a | PyDoc_STRVAR(default_int_handler_doc, |
---|
185 | n/a | "default_int_handler(...)\n\ |
---|
186 | n/a | \n\ |
---|
187 | n/a | The default handler for SIGINT installed by Python.\n\ |
---|
188 | n/a | It raises KeyboardInterrupt."); |
---|
189 | n/a | |
---|
190 | n/a | |
---|
191 | n/a | static int |
---|
192 | n/a | checksignals_witharg(void * unused) |
---|
193 | n/a | { |
---|
194 | n/a | return PyErr_CheckSignals(); |
---|
195 | n/a | } |
---|
196 | n/a | |
---|
197 | n/a | static int |
---|
198 | n/a | report_wakeup_write_error(void *data) |
---|
199 | n/a | { |
---|
200 | n/a | int save_errno = errno; |
---|
201 | n/a | errno = (int) (intptr_t) data; |
---|
202 | n/a | PyErr_SetFromErrno(PyExc_OSError); |
---|
203 | n/a | PySys_WriteStderr("Exception ignored when trying to write to the " |
---|
204 | n/a | "signal wakeup fd:\n"); |
---|
205 | n/a | PyErr_WriteUnraisable(NULL); |
---|
206 | n/a | errno = save_errno; |
---|
207 | n/a | return 0; |
---|
208 | n/a | } |
---|
209 | n/a | |
---|
210 | n/a | #ifdef MS_WINDOWS |
---|
211 | n/a | static int |
---|
212 | n/a | report_wakeup_send_error(void* Py_UNUSED(data)) |
---|
213 | n/a | { |
---|
214 | n/a | PyObject *res; |
---|
215 | n/a | |
---|
216 | n/a | if (wakeup.send_win_error) { |
---|
217 | n/a | /* PyErr_SetExcFromWindowsErr() invokes FormatMessage() which |
---|
218 | n/a | recognizes the error codes used by both GetLastError() and |
---|
219 | n/a | WSAGetLastError */ |
---|
220 | n/a | res = PyErr_SetExcFromWindowsErr(PyExc_OSError, wakeup.send_win_error); |
---|
221 | n/a | } |
---|
222 | n/a | else { |
---|
223 | n/a | errno = wakeup.send_errno; |
---|
224 | n/a | res = PyErr_SetFromErrno(PyExc_OSError); |
---|
225 | n/a | } |
---|
226 | n/a | |
---|
227 | n/a | assert(res == NULL); |
---|
228 | n/a | wakeup.send_err_set = 0; |
---|
229 | n/a | |
---|
230 | n/a | PySys_WriteStderr("Exception ignored when trying to send to the " |
---|
231 | n/a | "signal wakeup fd:\n"); |
---|
232 | n/a | PyErr_WriteUnraisable(NULL); |
---|
233 | n/a | |
---|
234 | n/a | return 0; |
---|
235 | n/a | } |
---|
236 | n/a | #endif /* MS_WINDOWS */ |
---|
237 | n/a | |
---|
238 | n/a | static void |
---|
239 | n/a | trip_signal(int sig_num) |
---|
240 | n/a | { |
---|
241 | n/a | unsigned char byte; |
---|
242 | n/a | int fd; |
---|
243 | n/a | Py_ssize_t rc; |
---|
244 | n/a | |
---|
245 | n/a | Handlers[sig_num].tripped = 1; |
---|
246 | n/a | |
---|
247 | n/a | #ifdef MS_WINDOWS |
---|
248 | n/a | fd = Py_SAFE_DOWNCAST(wakeup.fd, SOCKET_T, int); |
---|
249 | n/a | #else |
---|
250 | n/a | fd = wakeup_fd; |
---|
251 | n/a | #endif |
---|
252 | n/a | |
---|
253 | n/a | if (fd != INVALID_FD) { |
---|
254 | n/a | byte = (unsigned char)sig_num; |
---|
255 | n/a | #ifdef MS_WINDOWS |
---|
256 | n/a | if (wakeup.use_send) { |
---|
257 | n/a | do { |
---|
258 | n/a | rc = send(fd, &byte, 1, 0); |
---|
259 | n/a | } while (rc < 0 && errno == EINTR); |
---|
260 | n/a | |
---|
261 | n/a | /* we only have a storage for one error in the wakeup structure */ |
---|
262 | n/a | if (rc < 0 && !wakeup.send_err_set) { |
---|
263 | n/a | wakeup.send_err_set = 1; |
---|
264 | n/a | wakeup.send_errno = errno; |
---|
265 | n/a | wakeup.send_win_error = GetLastError(); |
---|
266 | n/a | Py_AddPendingCall(report_wakeup_send_error, NULL); |
---|
267 | n/a | } |
---|
268 | n/a | } |
---|
269 | n/a | else |
---|
270 | n/a | #endif |
---|
271 | n/a | { |
---|
272 | n/a | byte = (unsigned char)sig_num; |
---|
273 | n/a | |
---|
274 | n/a | /* _Py_write_noraise() retries write() if write() is interrupted by |
---|
275 | n/a | a signal (fails with EINTR). */ |
---|
276 | n/a | rc = _Py_write_noraise(fd, &byte, 1); |
---|
277 | n/a | |
---|
278 | n/a | if (rc < 0) { |
---|
279 | n/a | Py_AddPendingCall(report_wakeup_write_error, |
---|
280 | n/a | (void *)(intptr_t)errno); |
---|
281 | n/a | } |
---|
282 | n/a | } |
---|
283 | n/a | } |
---|
284 | n/a | |
---|
285 | n/a | if (!is_tripped) { |
---|
286 | n/a | /* Set is_tripped after setting .tripped, as it gets |
---|
287 | n/a | cleared in PyErr_CheckSignals() before .tripped. */ |
---|
288 | n/a | is_tripped = 1; |
---|
289 | n/a | Py_AddPendingCall(checksignals_witharg, NULL); |
---|
290 | n/a | } |
---|
291 | n/a | } |
---|
292 | n/a | |
---|
293 | n/a | static void |
---|
294 | n/a | signal_handler(int sig_num) |
---|
295 | n/a | { |
---|
296 | n/a | int save_errno = errno; |
---|
297 | n/a | |
---|
298 | n/a | #ifdef WITH_THREAD |
---|
299 | n/a | /* See NOTES section above */ |
---|
300 | n/a | if (getpid() == main_pid) |
---|
301 | n/a | #endif |
---|
302 | n/a | { |
---|
303 | n/a | trip_signal(sig_num); |
---|
304 | n/a | } |
---|
305 | n/a | |
---|
306 | n/a | #ifndef HAVE_SIGACTION |
---|
307 | n/a | #ifdef SIGCHLD |
---|
308 | n/a | /* To avoid infinite recursion, this signal remains |
---|
309 | n/a | reset until explicit re-instated. |
---|
310 | n/a | Don't clear the 'func' field as it is our pointer |
---|
311 | n/a | to the Python handler... */ |
---|
312 | n/a | if (sig_num != SIGCHLD) |
---|
313 | n/a | #endif |
---|
314 | n/a | /* If the handler was not set up with sigaction, reinstall it. See |
---|
315 | n/a | * Python/pylifecycle.c for the implementation of PyOS_setsig which |
---|
316 | n/a | * makes this true. See also issue8354. */ |
---|
317 | n/a | PyOS_setsig(sig_num, signal_handler); |
---|
318 | n/a | #endif |
---|
319 | n/a | |
---|
320 | n/a | /* Issue #10311: asynchronously executing signal handlers should not |
---|
321 | n/a | mutate errno under the feet of unsuspecting C code. */ |
---|
322 | n/a | errno = save_errno; |
---|
323 | n/a | |
---|
324 | n/a | #ifdef MS_WINDOWS |
---|
325 | n/a | if (sig_num == SIGINT) |
---|
326 | n/a | SetEvent(sigint_event); |
---|
327 | n/a | #endif |
---|
328 | n/a | } |
---|
329 | n/a | |
---|
330 | n/a | |
---|
331 | n/a | #ifdef HAVE_ALARM |
---|
332 | n/a | |
---|
333 | n/a | /*[clinic input] |
---|
334 | n/a | signal.alarm -> long |
---|
335 | n/a | |
---|
336 | n/a | seconds: int |
---|
337 | n/a | / |
---|
338 | n/a | |
---|
339 | n/a | Arrange for SIGALRM to arrive after the given number of seconds. |
---|
340 | n/a | [clinic start generated code]*/ |
---|
341 | n/a | |
---|
342 | n/a | static long |
---|
343 | n/a | signal_alarm_impl(PyObject *module, int seconds) |
---|
344 | n/a | /*[clinic end generated code: output=144232290814c298 input=0d5e97e0e6f39e86]*/ |
---|
345 | n/a | { |
---|
346 | n/a | /* alarm() returns the number of seconds remaining */ |
---|
347 | n/a | return (long)alarm(seconds); |
---|
348 | n/a | } |
---|
349 | n/a | |
---|
350 | n/a | #endif |
---|
351 | n/a | |
---|
352 | n/a | #ifdef HAVE_PAUSE |
---|
353 | n/a | |
---|
354 | n/a | /*[clinic input] |
---|
355 | n/a | signal.pause |
---|
356 | n/a | |
---|
357 | n/a | Wait until a signal arrives. |
---|
358 | n/a | [clinic start generated code]*/ |
---|
359 | n/a | |
---|
360 | n/a | static PyObject * |
---|
361 | n/a | signal_pause_impl(PyObject *module) |
---|
362 | n/a | /*[clinic end generated code: output=391656788b3c3929 input=f03de0f875752062]*/ |
---|
363 | n/a | { |
---|
364 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
365 | n/a | (void)pause(); |
---|
366 | n/a | Py_END_ALLOW_THREADS |
---|
367 | n/a | /* make sure that any exceptions that got raised are propagated |
---|
368 | n/a | * back into Python |
---|
369 | n/a | */ |
---|
370 | n/a | if (PyErr_CheckSignals()) |
---|
371 | n/a | return NULL; |
---|
372 | n/a | |
---|
373 | n/a | Py_RETURN_NONE; |
---|
374 | n/a | } |
---|
375 | n/a | |
---|
376 | n/a | #endif |
---|
377 | n/a | |
---|
378 | n/a | |
---|
379 | n/a | /*[clinic input] |
---|
380 | n/a | signal.signal |
---|
381 | n/a | |
---|
382 | n/a | signalnum: int |
---|
383 | n/a | handler: object |
---|
384 | n/a | / |
---|
385 | n/a | |
---|
386 | n/a | Set the action for the given signal. |
---|
387 | n/a | |
---|
388 | n/a | The action can be SIG_DFL, SIG_IGN, or a callable Python object. |
---|
389 | n/a | The previous action is returned. See getsignal() for possible return values. |
---|
390 | n/a | |
---|
391 | n/a | *** IMPORTANT NOTICE *** |
---|
392 | n/a | A signal handler function is called with two arguments: |
---|
393 | n/a | the first is the signal number, the second is the interrupted stack frame. |
---|
394 | n/a | [clinic start generated code]*/ |
---|
395 | n/a | |
---|
396 | n/a | static PyObject * |
---|
397 | n/a | signal_signal_impl(PyObject *module, int signalnum, PyObject *handler) |
---|
398 | n/a | /*[clinic end generated code: output=b44cfda43780f3a1 input=deee84af5fa0432c]*/ |
---|
399 | n/a | { |
---|
400 | n/a | PyObject *old_handler; |
---|
401 | n/a | void (*func)(int); |
---|
402 | n/a | #ifdef MS_WINDOWS |
---|
403 | n/a | /* Validate that signalnum is one of the allowable signals */ |
---|
404 | n/a | switch (signalnum) { |
---|
405 | n/a | case SIGABRT: break; |
---|
406 | n/a | #ifdef SIGBREAK |
---|
407 | n/a | /* Issue #10003: SIGBREAK is not documented as permitted, but works |
---|
408 | n/a | and corresponds to CTRL_BREAK_EVENT. */ |
---|
409 | n/a | case SIGBREAK: break; |
---|
410 | n/a | #endif |
---|
411 | n/a | case SIGFPE: break; |
---|
412 | n/a | case SIGILL: break; |
---|
413 | n/a | case SIGINT: break; |
---|
414 | n/a | case SIGSEGV: break; |
---|
415 | n/a | case SIGTERM: break; |
---|
416 | n/a | default: |
---|
417 | n/a | PyErr_SetString(PyExc_ValueError, "invalid signal value"); |
---|
418 | n/a | return NULL; |
---|
419 | n/a | } |
---|
420 | n/a | #endif |
---|
421 | n/a | #ifdef WITH_THREAD |
---|
422 | n/a | if (PyThread_get_thread_ident() != main_thread) { |
---|
423 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
424 | n/a | "signal only works in main thread"); |
---|
425 | n/a | return NULL; |
---|
426 | n/a | } |
---|
427 | n/a | #endif |
---|
428 | n/a | if (signalnum < 1 || signalnum >= NSIG) { |
---|
429 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
430 | n/a | "signal number out of range"); |
---|
431 | n/a | return NULL; |
---|
432 | n/a | } |
---|
433 | n/a | if (handler == IgnoreHandler) |
---|
434 | n/a | func = SIG_IGN; |
---|
435 | n/a | else if (handler == DefaultHandler) |
---|
436 | n/a | func = SIG_DFL; |
---|
437 | n/a | else if (!PyCallable_Check(handler)) { |
---|
438 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
439 | n/a | "signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object"); |
---|
440 | n/a | return NULL; |
---|
441 | n/a | } |
---|
442 | n/a | else |
---|
443 | n/a | func = signal_handler; |
---|
444 | n/a | if (PyOS_setsig(signalnum, func) == SIG_ERR) { |
---|
445 | n/a | PyErr_SetFromErrno(PyExc_OSError); |
---|
446 | n/a | return NULL; |
---|
447 | n/a | } |
---|
448 | n/a | old_handler = Handlers[signalnum].func; |
---|
449 | n/a | Handlers[signalnum].tripped = 0; |
---|
450 | n/a | Py_INCREF(handler); |
---|
451 | n/a | Handlers[signalnum].func = handler; |
---|
452 | n/a | if (old_handler != NULL) |
---|
453 | n/a | return old_handler; |
---|
454 | n/a | else |
---|
455 | n/a | Py_RETURN_NONE; |
---|
456 | n/a | } |
---|
457 | n/a | |
---|
458 | n/a | |
---|
459 | n/a | /*[clinic input] |
---|
460 | n/a | signal.getsignal |
---|
461 | n/a | |
---|
462 | n/a | signalnum: int |
---|
463 | n/a | / |
---|
464 | n/a | |
---|
465 | n/a | Return the current action for the given signal. |
---|
466 | n/a | |
---|
467 | n/a | The return value can be: |
---|
468 | n/a | SIG_IGN -- if the signal is being ignored |
---|
469 | n/a | SIG_DFL -- if the default action for the signal is in effect |
---|
470 | n/a | None -- if an unknown handler is in effect |
---|
471 | n/a | anything else -- the callable Python object used as a handler |
---|
472 | n/a | [clinic start generated code]*/ |
---|
473 | n/a | |
---|
474 | n/a | static PyObject * |
---|
475 | n/a | signal_getsignal_impl(PyObject *module, int signalnum) |
---|
476 | n/a | /*[clinic end generated code: output=35b3e0e796fd555e input=ac23a00f19dfa509]*/ |
---|
477 | n/a | { |
---|
478 | n/a | PyObject *old_handler; |
---|
479 | n/a | if (signalnum < 1 || signalnum >= NSIG) { |
---|
480 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
481 | n/a | "signal number out of range"); |
---|
482 | n/a | return NULL; |
---|
483 | n/a | } |
---|
484 | n/a | old_handler = Handlers[signalnum].func; |
---|
485 | n/a | if (old_handler != NULL) { |
---|
486 | n/a | Py_INCREF(old_handler); |
---|
487 | n/a | return old_handler; |
---|
488 | n/a | } |
---|
489 | n/a | else { |
---|
490 | n/a | Py_RETURN_NONE; |
---|
491 | n/a | } |
---|
492 | n/a | } |
---|
493 | n/a | |
---|
494 | n/a | #ifdef HAVE_SIGINTERRUPT |
---|
495 | n/a | |
---|
496 | n/a | /*[clinic input] |
---|
497 | n/a | signal.siginterrupt |
---|
498 | n/a | |
---|
499 | n/a | signalnum: int |
---|
500 | n/a | flag: int |
---|
501 | n/a | / |
---|
502 | n/a | |
---|
503 | n/a | Change system call restart behaviour. |
---|
504 | n/a | |
---|
505 | n/a | If flag is False, system calls will be restarted when interrupted by |
---|
506 | n/a | signal sig, else system calls will be interrupted. |
---|
507 | n/a | [clinic start generated code]*/ |
---|
508 | n/a | |
---|
509 | n/a | static PyObject * |
---|
510 | n/a | signal_siginterrupt_impl(PyObject *module, int signalnum, int flag) |
---|
511 | n/a | /*[clinic end generated code: output=063816243d85dd19 input=4160acacca3e2099]*/ |
---|
512 | n/a | { |
---|
513 | n/a | if (signalnum < 1 || signalnum >= NSIG) { |
---|
514 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
515 | n/a | "signal number out of range"); |
---|
516 | n/a | return NULL; |
---|
517 | n/a | } |
---|
518 | n/a | if (siginterrupt(signalnum, flag)<0) { |
---|
519 | n/a | PyErr_SetFromErrno(PyExc_OSError); |
---|
520 | n/a | return NULL; |
---|
521 | n/a | } |
---|
522 | n/a | Py_RETURN_NONE; |
---|
523 | n/a | } |
---|
524 | n/a | |
---|
525 | n/a | #endif |
---|
526 | n/a | |
---|
527 | n/a | |
---|
528 | n/a | static PyObject* |
---|
529 | n/a | signal_set_wakeup_fd(PyObject *self, PyObject *args) |
---|
530 | n/a | { |
---|
531 | n/a | struct _Py_stat_struct status; |
---|
532 | n/a | #ifdef MS_WINDOWS |
---|
533 | n/a | PyObject *fdobj; |
---|
534 | n/a | SOCKET_T sockfd, old_sockfd; |
---|
535 | n/a | int res; |
---|
536 | n/a | int res_size = sizeof res; |
---|
537 | n/a | PyObject *mod; |
---|
538 | n/a | int is_socket; |
---|
539 | n/a | |
---|
540 | n/a | if (!PyArg_ParseTuple(args, "O:set_wakeup_fd", &fdobj)) |
---|
541 | n/a | return NULL; |
---|
542 | n/a | |
---|
543 | n/a | sockfd = PyLong_AsSocket_t(fdobj); |
---|
544 | n/a | if (sockfd == (SOCKET_T)(-1) && PyErr_Occurred()) |
---|
545 | n/a | return NULL; |
---|
546 | n/a | #else |
---|
547 | n/a | int fd, old_fd; |
---|
548 | n/a | |
---|
549 | n/a | if (!PyArg_ParseTuple(args, "i:set_wakeup_fd", &fd)) |
---|
550 | n/a | return NULL; |
---|
551 | n/a | #endif |
---|
552 | n/a | |
---|
553 | n/a | #ifdef WITH_THREAD |
---|
554 | n/a | if (PyThread_get_thread_ident() != main_thread) { |
---|
555 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
556 | n/a | "set_wakeup_fd only works in main thread"); |
---|
557 | n/a | return NULL; |
---|
558 | n/a | } |
---|
559 | n/a | #endif |
---|
560 | n/a | |
---|
561 | n/a | #ifdef MS_WINDOWS |
---|
562 | n/a | is_socket = 0; |
---|
563 | n/a | if (sockfd != INVALID_FD) { |
---|
564 | n/a | /* Import the _socket module to call WSAStartup() */ |
---|
565 | n/a | mod = PyImport_ImportModuleNoBlock("_socket"); |
---|
566 | n/a | if (mod == NULL) |
---|
567 | n/a | return NULL; |
---|
568 | n/a | Py_DECREF(mod); |
---|
569 | n/a | |
---|
570 | n/a | /* test the socket */ |
---|
571 | n/a | if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, |
---|
572 | n/a | (char *)&res, &res_size) != 0) { |
---|
573 | n/a | int fd, err; |
---|
574 | n/a | |
---|
575 | n/a | err = WSAGetLastError(); |
---|
576 | n/a | if (err != WSAENOTSOCK) { |
---|
577 | n/a | PyErr_SetExcFromWindowsErr(PyExc_OSError, err); |
---|
578 | n/a | return NULL; |
---|
579 | n/a | } |
---|
580 | n/a | |
---|
581 | n/a | fd = (int)sockfd; |
---|
582 | n/a | if ((SOCKET_T)fd != sockfd) { |
---|
583 | n/a | PyErr_SetString(PyExc_ValueError, "invalid fd"); |
---|
584 | n/a | return NULL; |
---|
585 | n/a | } |
---|
586 | n/a | |
---|
587 | n/a | if (_Py_fstat(fd, &status) != 0) |
---|
588 | n/a | return NULL; |
---|
589 | n/a | |
---|
590 | n/a | /* on Windows, a file cannot be set to non-blocking mode */ |
---|
591 | n/a | } |
---|
592 | n/a | else { |
---|
593 | n/a | is_socket = 1; |
---|
594 | n/a | |
---|
595 | n/a | /* Windows does not provide a function to test if a socket |
---|
596 | n/a | is in non-blocking mode */ |
---|
597 | n/a | } |
---|
598 | n/a | } |
---|
599 | n/a | |
---|
600 | n/a | old_sockfd = wakeup.fd; |
---|
601 | n/a | wakeup.fd = sockfd; |
---|
602 | n/a | wakeup.use_send = is_socket; |
---|
603 | n/a | |
---|
604 | n/a | if (old_sockfd != INVALID_FD) |
---|
605 | n/a | return PyLong_FromSocket_t(old_sockfd); |
---|
606 | n/a | else |
---|
607 | n/a | return PyLong_FromLong(-1); |
---|
608 | n/a | #else |
---|
609 | n/a | if (fd != -1) { |
---|
610 | n/a | int blocking; |
---|
611 | n/a | |
---|
612 | n/a | if (_Py_fstat(fd, &status) != 0) |
---|
613 | n/a | return NULL; |
---|
614 | n/a | |
---|
615 | n/a | blocking = _Py_get_blocking(fd); |
---|
616 | n/a | if (blocking < 0) |
---|
617 | n/a | return NULL; |
---|
618 | n/a | if (blocking) { |
---|
619 | n/a | PyErr_Format(PyExc_ValueError, |
---|
620 | n/a | "the fd %i must be in non-blocking mode", |
---|
621 | n/a | fd); |
---|
622 | n/a | return NULL; |
---|
623 | n/a | } |
---|
624 | n/a | } |
---|
625 | n/a | |
---|
626 | n/a | old_fd = wakeup_fd; |
---|
627 | n/a | wakeup_fd = fd; |
---|
628 | n/a | |
---|
629 | n/a | return PyLong_FromLong(old_fd); |
---|
630 | n/a | #endif |
---|
631 | n/a | } |
---|
632 | n/a | |
---|
633 | n/a | PyDoc_STRVAR(set_wakeup_fd_doc, |
---|
634 | n/a | "set_wakeup_fd(fd) -> fd\n\ |
---|
635 | n/a | \n\ |
---|
636 | n/a | Sets the fd to be written to (with the signal number) when a signal\n\ |
---|
637 | n/a | comes in. A library can use this to wakeup select or poll.\n\ |
---|
638 | n/a | The previous fd or -1 is returned.\n\ |
---|
639 | n/a | \n\ |
---|
640 | n/a | The fd must be non-blocking."); |
---|
641 | n/a | |
---|
642 | n/a | /* C API for the same, without all the error checking */ |
---|
643 | n/a | int |
---|
644 | n/a | PySignal_SetWakeupFd(int fd) |
---|
645 | n/a | { |
---|
646 | n/a | int old_fd; |
---|
647 | n/a | if (fd < 0) |
---|
648 | n/a | fd = -1; |
---|
649 | n/a | |
---|
650 | n/a | #ifdef MS_WINDOWS |
---|
651 | n/a | old_fd = Py_SAFE_DOWNCAST(wakeup.fd, SOCKET_T, int); |
---|
652 | n/a | wakeup.fd = fd; |
---|
653 | n/a | #else |
---|
654 | n/a | old_fd = wakeup_fd; |
---|
655 | n/a | wakeup_fd = fd; |
---|
656 | n/a | #endif |
---|
657 | n/a | return old_fd; |
---|
658 | n/a | } |
---|
659 | n/a | |
---|
660 | n/a | |
---|
661 | n/a | #ifdef HAVE_SETITIMER |
---|
662 | n/a | |
---|
663 | n/a | /*[clinic input] |
---|
664 | n/a | signal.setitimer |
---|
665 | n/a | |
---|
666 | n/a | which: int |
---|
667 | n/a | seconds: double |
---|
668 | n/a | interval: double = 0.0 |
---|
669 | n/a | / |
---|
670 | n/a | |
---|
671 | n/a | Sets given itimer (one of ITIMER_REAL, ITIMER_VIRTUAL or ITIMER_PROF). |
---|
672 | n/a | |
---|
673 | n/a | The timer will fire after value seconds and after that every interval seconds. |
---|
674 | n/a | The itimer can be cleared by setting seconds to zero. |
---|
675 | n/a | |
---|
676 | n/a | Returns old values as a tuple: (delay, interval). |
---|
677 | n/a | [clinic start generated code]*/ |
---|
678 | n/a | |
---|
679 | n/a | static PyObject * |
---|
680 | n/a | signal_setitimer_impl(PyObject *module, int which, double seconds, |
---|
681 | n/a | double interval) |
---|
682 | n/a | /*[clinic end generated code: output=6f51da0fe0787f2c input=0d27d417cfcbd51a]*/ |
---|
683 | n/a | { |
---|
684 | n/a | struct itimerval new, old; |
---|
685 | n/a | |
---|
686 | n/a | timeval_from_double(seconds, &new.it_value); |
---|
687 | n/a | timeval_from_double(interval, &new.it_interval); |
---|
688 | n/a | /* Let OS check "which" value */ |
---|
689 | n/a | if (setitimer(which, &new, &old) != 0) { |
---|
690 | n/a | PyErr_SetFromErrno(ItimerError); |
---|
691 | n/a | return NULL; |
---|
692 | n/a | } |
---|
693 | n/a | |
---|
694 | n/a | return itimer_retval(&old); |
---|
695 | n/a | } |
---|
696 | n/a | |
---|
697 | n/a | #endif |
---|
698 | n/a | |
---|
699 | n/a | |
---|
700 | n/a | #ifdef HAVE_GETITIMER |
---|
701 | n/a | |
---|
702 | n/a | /*[clinic input] |
---|
703 | n/a | signal.getitimer |
---|
704 | n/a | |
---|
705 | n/a | which: int |
---|
706 | n/a | / |
---|
707 | n/a | |
---|
708 | n/a | Returns current value of given itimer. |
---|
709 | n/a | [clinic start generated code]*/ |
---|
710 | n/a | |
---|
711 | n/a | static PyObject * |
---|
712 | n/a | signal_getitimer_impl(PyObject *module, int which) |
---|
713 | n/a | /*[clinic end generated code: output=9e053175d517db40 input=f7d21d38f3490627]*/ |
---|
714 | n/a | { |
---|
715 | n/a | struct itimerval old; |
---|
716 | n/a | |
---|
717 | n/a | if (getitimer(which, &old) != 0) { |
---|
718 | n/a | PyErr_SetFromErrno(ItimerError); |
---|
719 | n/a | return NULL; |
---|
720 | n/a | } |
---|
721 | n/a | |
---|
722 | n/a | return itimer_retval(&old); |
---|
723 | n/a | } |
---|
724 | n/a | |
---|
725 | n/a | #endif |
---|
726 | n/a | |
---|
727 | n/a | #if defined(PYPTHREAD_SIGMASK) || defined(HAVE_SIGWAIT) || \ |
---|
728 | n/a | defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT) |
---|
729 | n/a | /* Convert an iterable to a sigset. |
---|
730 | n/a | Return 0 on success, return -1 and raise an exception on error. */ |
---|
731 | n/a | |
---|
732 | n/a | static int |
---|
733 | n/a | iterable_to_sigset(PyObject *iterable, sigset_t *mask) |
---|
734 | n/a | { |
---|
735 | n/a | int result = -1; |
---|
736 | n/a | PyObject *iterator, *item; |
---|
737 | n/a | long signum; |
---|
738 | n/a | int err; |
---|
739 | n/a | |
---|
740 | n/a | sigemptyset(mask); |
---|
741 | n/a | |
---|
742 | n/a | iterator = PyObject_GetIter(iterable); |
---|
743 | n/a | if (iterator == NULL) |
---|
744 | n/a | goto error; |
---|
745 | n/a | |
---|
746 | n/a | while (1) |
---|
747 | n/a | { |
---|
748 | n/a | item = PyIter_Next(iterator); |
---|
749 | n/a | if (item == NULL) { |
---|
750 | n/a | if (PyErr_Occurred()) |
---|
751 | n/a | goto error; |
---|
752 | n/a | else |
---|
753 | n/a | break; |
---|
754 | n/a | } |
---|
755 | n/a | |
---|
756 | n/a | signum = PyLong_AsLong(item); |
---|
757 | n/a | Py_DECREF(item); |
---|
758 | n/a | if (signum == -1 && PyErr_Occurred()) |
---|
759 | n/a | goto error; |
---|
760 | n/a | if (0 < signum && signum < NSIG) |
---|
761 | n/a | err = sigaddset(mask, (int)signum); |
---|
762 | n/a | else |
---|
763 | n/a | err = 1; |
---|
764 | n/a | if (err) { |
---|
765 | n/a | PyErr_Format(PyExc_ValueError, |
---|
766 | n/a | "signal number %ld out of range", signum); |
---|
767 | n/a | goto error; |
---|
768 | n/a | } |
---|
769 | n/a | } |
---|
770 | n/a | result = 0; |
---|
771 | n/a | |
---|
772 | n/a | error: |
---|
773 | n/a | Py_XDECREF(iterator); |
---|
774 | n/a | return result; |
---|
775 | n/a | } |
---|
776 | n/a | #endif |
---|
777 | n/a | |
---|
778 | n/a | #if defined(PYPTHREAD_SIGMASK) || defined(HAVE_SIGPENDING) |
---|
779 | n/a | static PyObject* |
---|
780 | n/a | sigset_to_set(sigset_t mask) |
---|
781 | n/a | { |
---|
782 | n/a | PyObject *signum, *result; |
---|
783 | n/a | int sig; |
---|
784 | n/a | |
---|
785 | n/a | result = PySet_New(0); |
---|
786 | n/a | if (result == NULL) |
---|
787 | n/a | return NULL; |
---|
788 | n/a | |
---|
789 | n/a | for (sig = 1; sig < NSIG; sig++) { |
---|
790 | n/a | if (sigismember(&mask, sig) != 1) |
---|
791 | n/a | continue; |
---|
792 | n/a | |
---|
793 | n/a | /* Handle the case where it is a member by adding the signal to |
---|
794 | n/a | the result list. Ignore the other cases because they mean the |
---|
795 | n/a | signal isn't a member of the mask or the signal was invalid, |
---|
796 | n/a | and an invalid signal must have been our fault in constructing |
---|
797 | n/a | the loop boundaries. */ |
---|
798 | n/a | signum = PyLong_FromLong(sig); |
---|
799 | n/a | if (signum == NULL) { |
---|
800 | n/a | Py_DECREF(result); |
---|
801 | n/a | return NULL; |
---|
802 | n/a | } |
---|
803 | n/a | if (PySet_Add(result, signum) == -1) { |
---|
804 | n/a | Py_DECREF(signum); |
---|
805 | n/a | Py_DECREF(result); |
---|
806 | n/a | return NULL; |
---|
807 | n/a | } |
---|
808 | n/a | Py_DECREF(signum); |
---|
809 | n/a | } |
---|
810 | n/a | return result; |
---|
811 | n/a | } |
---|
812 | n/a | #endif |
---|
813 | n/a | |
---|
814 | n/a | #ifdef PYPTHREAD_SIGMASK |
---|
815 | n/a | |
---|
816 | n/a | /*[clinic input] |
---|
817 | n/a | signal.pthread_sigmask |
---|
818 | n/a | |
---|
819 | n/a | how: int |
---|
820 | n/a | mask: object |
---|
821 | n/a | / |
---|
822 | n/a | |
---|
823 | n/a | Fetch and/or change the signal mask of the calling thread. |
---|
824 | n/a | [clinic start generated code]*/ |
---|
825 | n/a | |
---|
826 | n/a | static PyObject * |
---|
827 | n/a | signal_pthread_sigmask_impl(PyObject *module, int how, PyObject *mask) |
---|
828 | n/a | /*[clinic end generated code: output=ff640fe092bc9181 input=f3b7d7a61b7b8283]*/ |
---|
829 | n/a | { |
---|
830 | n/a | sigset_t newmask, previous; |
---|
831 | n/a | int err; |
---|
832 | n/a | |
---|
833 | n/a | if (iterable_to_sigset(mask, &newmask)) |
---|
834 | n/a | return NULL; |
---|
835 | n/a | |
---|
836 | n/a | err = pthread_sigmask(how, &newmask, &previous); |
---|
837 | n/a | if (err != 0) { |
---|
838 | n/a | errno = err; |
---|
839 | n/a | PyErr_SetFromErrno(PyExc_OSError); |
---|
840 | n/a | return NULL; |
---|
841 | n/a | } |
---|
842 | n/a | |
---|
843 | n/a | /* if signals was unblocked, signal handlers have been called */ |
---|
844 | n/a | if (PyErr_CheckSignals()) |
---|
845 | n/a | return NULL; |
---|
846 | n/a | |
---|
847 | n/a | return sigset_to_set(previous); |
---|
848 | n/a | } |
---|
849 | n/a | |
---|
850 | n/a | #endif /* #ifdef PYPTHREAD_SIGMASK */ |
---|
851 | n/a | |
---|
852 | n/a | |
---|
853 | n/a | #ifdef HAVE_SIGPENDING |
---|
854 | n/a | |
---|
855 | n/a | /*[clinic input] |
---|
856 | n/a | signal.sigpending |
---|
857 | n/a | |
---|
858 | n/a | Examine pending signals. |
---|
859 | n/a | |
---|
860 | n/a | Returns a set of signal numbers that are pending for delivery to |
---|
861 | n/a | the calling thread. |
---|
862 | n/a | [clinic start generated code]*/ |
---|
863 | n/a | |
---|
864 | n/a | static PyObject * |
---|
865 | n/a | signal_sigpending_impl(PyObject *module) |
---|
866 | n/a | /*[clinic end generated code: output=53375ffe89325022 input=e0036c016f874e29]*/ |
---|
867 | n/a | { |
---|
868 | n/a | int err; |
---|
869 | n/a | sigset_t mask; |
---|
870 | n/a | err = sigpending(&mask); |
---|
871 | n/a | if (err) |
---|
872 | n/a | return PyErr_SetFromErrno(PyExc_OSError); |
---|
873 | n/a | return sigset_to_set(mask); |
---|
874 | n/a | } |
---|
875 | n/a | |
---|
876 | n/a | #endif /* #ifdef HAVE_SIGPENDING */ |
---|
877 | n/a | |
---|
878 | n/a | |
---|
879 | n/a | #ifdef HAVE_SIGWAIT |
---|
880 | n/a | |
---|
881 | n/a | /*[clinic input] |
---|
882 | n/a | signal.sigwait |
---|
883 | n/a | |
---|
884 | n/a | sigset: object |
---|
885 | n/a | / |
---|
886 | n/a | |
---|
887 | n/a | Wait for a signal. |
---|
888 | n/a | |
---|
889 | n/a | Suspend execution of the calling thread until the delivery of one of the |
---|
890 | n/a | signals specified in the signal set sigset. The function accepts the signal |
---|
891 | n/a | and returns the signal number. |
---|
892 | n/a | [clinic start generated code]*/ |
---|
893 | n/a | |
---|
894 | n/a | static PyObject * |
---|
895 | n/a | signal_sigwait(PyObject *module, PyObject *sigset) |
---|
896 | n/a | /*[clinic end generated code: output=557173647424f6e4 input=11af2d82d83c2e94]*/ |
---|
897 | n/a | { |
---|
898 | n/a | sigset_t set; |
---|
899 | n/a | int err, signum; |
---|
900 | n/a | |
---|
901 | n/a | if (iterable_to_sigset(sigset, &set)) |
---|
902 | n/a | return NULL; |
---|
903 | n/a | |
---|
904 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
905 | n/a | err = sigwait(&set, &signum); |
---|
906 | n/a | Py_END_ALLOW_THREADS |
---|
907 | n/a | if (err) { |
---|
908 | n/a | errno = err; |
---|
909 | n/a | return PyErr_SetFromErrno(PyExc_OSError); |
---|
910 | n/a | } |
---|
911 | n/a | |
---|
912 | n/a | return PyLong_FromLong(signum); |
---|
913 | n/a | } |
---|
914 | n/a | |
---|
915 | n/a | #endif /* #ifdef HAVE_SIGWAIT */ |
---|
916 | n/a | |
---|
917 | n/a | |
---|
918 | n/a | #if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT) |
---|
919 | n/a | static int initialized; |
---|
920 | n/a | static PyStructSequence_Field struct_siginfo_fields[] = { |
---|
921 | n/a | {"si_signo", "signal number"}, |
---|
922 | n/a | {"si_code", "signal code"}, |
---|
923 | n/a | {"si_errno", "errno associated with this signal"}, |
---|
924 | n/a | {"si_pid", "sending process ID"}, |
---|
925 | n/a | {"si_uid", "real user ID of sending process"}, |
---|
926 | n/a | {"si_status", "exit value or signal"}, |
---|
927 | n/a | {"si_band", "band event for SIGPOLL"}, |
---|
928 | n/a | {0} |
---|
929 | n/a | }; |
---|
930 | n/a | |
---|
931 | n/a | PyDoc_STRVAR(struct_siginfo__doc__, |
---|
932 | n/a | "struct_siginfo: Result from sigwaitinfo or sigtimedwait.\n\n\ |
---|
933 | n/a | This object may be accessed either as a tuple of\n\ |
---|
934 | n/a | (si_signo, si_code, si_errno, si_pid, si_uid, si_status, si_band),\n\ |
---|
935 | n/a | or via the attributes si_signo, si_code, and so on."); |
---|
936 | n/a | |
---|
937 | n/a | static PyStructSequence_Desc struct_siginfo_desc = { |
---|
938 | n/a | "signal.struct_siginfo", /* name */ |
---|
939 | n/a | struct_siginfo__doc__, /* doc */ |
---|
940 | n/a | struct_siginfo_fields, /* fields */ |
---|
941 | n/a | 7 /* n_in_sequence */ |
---|
942 | n/a | }; |
---|
943 | n/a | |
---|
944 | n/a | static PyTypeObject SiginfoType; |
---|
945 | n/a | |
---|
946 | n/a | static PyObject * |
---|
947 | n/a | fill_siginfo(siginfo_t *si) |
---|
948 | n/a | { |
---|
949 | n/a | PyObject *result = PyStructSequence_New(&SiginfoType); |
---|
950 | n/a | if (!result) |
---|
951 | n/a | return NULL; |
---|
952 | n/a | |
---|
953 | n/a | PyStructSequence_SET_ITEM(result, 0, PyLong_FromLong((long)(si->si_signo))); |
---|
954 | n/a | PyStructSequence_SET_ITEM(result, 1, PyLong_FromLong((long)(si->si_code))); |
---|
955 | n/a | PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si->si_errno))); |
---|
956 | n/a | PyStructSequence_SET_ITEM(result, 3, PyLong_FromPid(si->si_pid)); |
---|
957 | n/a | PyStructSequence_SET_ITEM(result, 4, _PyLong_FromUid(si->si_uid)); |
---|
958 | n/a | PyStructSequence_SET_ITEM(result, 5, |
---|
959 | n/a | PyLong_FromLong((long)(si->si_status))); |
---|
960 | n/a | #ifdef HAVE_SIGINFO_T_SI_BAND |
---|
961 | n/a | PyStructSequence_SET_ITEM(result, 6, PyLong_FromLong(si->si_band)); |
---|
962 | n/a | #else |
---|
963 | n/a | PyStructSequence_SET_ITEM(result, 6, PyLong_FromLong(0L)); |
---|
964 | n/a | #endif |
---|
965 | n/a | if (PyErr_Occurred()) { |
---|
966 | n/a | Py_DECREF(result); |
---|
967 | n/a | return NULL; |
---|
968 | n/a | } |
---|
969 | n/a | |
---|
970 | n/a | return result; |
---|
971 | n/a | } |
---|
972 | n/a | #endif |
---|
973 | n/a | |
---|
974 | n/a | #ifdef HAVE_SIGWAITINFO |
---|
975 | n/a | |
---|
976 | n/a | /*[clinic input] |
---|
977 | n/a | signal.sigwaitinfo |
---|
978 | n/a | |
---|
979 | n/a | sigset: object |
---|
980 | n/a | / |
---|
981 | n/a | |
---|
982 | n/a | Wait synchronously until one of the signals in *sigset* is delivered. |
---|
983 | n/a | |
---|
984 | n/a | Returns a struct_siginfo containing information about the signal. |
---|
985 | n/a | [clinic start generated code]*/ |
---|
986 | n/a | |
---|
987 | n/a | static PyObject * |
---|
988 | n/a | signal_sigwaitinfo(PyObject *module, PyObject *sigset) |
---|
989 | n/a | /*[clinic end generated code: output=c40f27b269cd2309 input=f3779a74a991e171]*/ |
---|
990 | n/a | { |
---|
991 | n/a | sigset_t set; |
---|
992 | n/a | siginfo_t si; |
---|
993 | n/a | int err; |
---|
994 | n/a | int async_err = 0; |
---|
995 | n/a | |
---|
996 | n/a | if (iterable_to_sigset(sigset, &set)) |
---|
997 | n/a | return NULL; |
---|
998 | n/a | |
---|
999 | n/a | do { |
---|
1000 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
1001 | n/a | err = sigwaitinfo(&set, &si); |
---|
1002 | n/a | Py_END_ALLOW_THREADS |
---|
1003 | n/a | } while (err == -1 |
---|
1004 | n/a | && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
---|
1005 | n/a | if (err == -1) |
---|
1006 | n/a | return (!async_err) ? PyErr_SetFromErrno(PyExc_OSError) : NULL; |
---|
1007 | n/a | |
---|
1008 | n/a | return fill_siginfo(&si); |
---|
1009 | n/a | } |
---|
1010 | n/a | |
---|
1011 | n/a | #endif /* #ifdef HAVE_SIGWAITINFO */ |
---|
1012 | n/a | |
---|
1013 | n/a | #ifdef HAVE_SIGTIMEDWAIT |
---|
1014 | n/a | |
---|
1015 | n/a | /*[clinic input] |
---|
1016 | n/a | signal.sigtimedwait |
---|
1017 | n/a | |
---|
1018 | n/a | sigset: object |
---|
1019 | n/a | timeout as timeout_obj: object |
---|
1020 | n/a | / |
---|
1021 | n/a | |
---|
1022 | n/a | Like sigwaitinfo(), but with a timeout. |
---|
1023 | n/a | |
---|
1024 | n/a | The timeout is specified in seconds, with floating point numbers allowed. |
---|
1025 | n/a | [clinic start generated code]*/ |
---|
1026 | n/a | |
---|
1027 | n/a | static PyObject * |
---|
1028 | n/a | signal_sigtimedwait_impl(PyObject *module, PyObject *sigset, |
---|
1029 | n/a | PyObject *timeout_obj) |
---|
1030 | n/a | /*[clinic end generated code: output=f7eff31e679f4312 input=53fd4ea3e3724eb8]*/ |
---|
1031 | n/a | { |
---|
1032 | n/a | struct timespec ts; |
---|
1033 | n/a | sigset_t set; |
---|
1034 | n/a | siginfo_t si; |
---|
1035 | n/a | int res; |
---|
1036 | n/a | _PyTime_t timeout, deadline, monotonic; |
---|
1037 | n/a | |
---|
1038 | n/a | if (_PyTime_FromSecondsObject(&timeout, |
---|
1039 | n/a | timeout_obj, _PyTime_ROUND_CEILING) < 0) |
---|
1040 | n/a | return NULL; |
---|
1041 | n/a | |
---|
1042 | n/a | if (timeout < 0) { |
---|
1043 | n/a | PyErr_SetString(PyExc_ValueError, "timeout must be non-negative"); |
---|
1044 | n/a | return NULL; |
---|
1045 | n/a | } |
---|
1046 | n/a | |
---|
1047 | n/a | if (iterable_to_sigset(sigset, &set)) |
---|
1048 | n/a | return NULL; |
---|
1049 | n/a | |
---|
1050 | n/a | deadline = _PyTime_GetMonotonicClock() + timeout; |
---|
1051 | n/a | |
---|
1052 | n/a | do { |
---|
1053 | n/a | if (_PyTime_AsTimespec(timeout, &ts) < 0) |
---|
1054 | n/a | return NULL; |
---|
1055 | n/a | |
---|
1056 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
1057 | n/a | res = sigtimedwait(&set, &si, &ts); |
---|
1058 | n/a | Py_END_ALLOW_THREADS |
---|
1059 | n/a | |
---|
1060 | n/a | if (res != -1) |
---|
1061 | n/a | break; |
---|
1062 | n/a | |
---|
1063 | n/a | if (errno != EINTR) { |
---|
1064 | n/a | if (errno == EAGAIN) |
---|
1065 | n/a | Py_RETURN_NONE; |
---|
1066 | n/a | else |
---|
1067 | n/a | return PyErr_SetFromErrno(PyExc_OSError); |
---|
1068 | n/a | } |
---|
1069 | n/a | |
---|
1070 | n/a | /* sigtimedwait() was interrupted by a signal (EINTR) */ |
---|
1071 | n/a | if (PyErr_CheckSignals()) |
---|
1072 | n/a | return NULL; |
---|
1073 | n/a | |
---|
1074 | n/a | monotonic = _PyTime_GetMonotonicClock(); |
---|
1075 | n/a | timeout = deadline - monotonic; |
---|
1076 | n/a | if (timeout < 0) |
---|
1077 | n/a | break; |
---|
1078 | n/a | } while (1); |
---|
1079 | n/a | |
---|
1080 | n/a | return fill_siginfo(&si); |
---|
1081 | n/a | } |
---|
1082 | n/a | |
---|
1083 | n/a | #endif /* #ifdef HAVE_SIGTIMEDWAIT */ |
---|
1084 | n/a | |
---|
1085 | n/a | |
---|
1086 | n/a | #if defined(HAVE_PTHREAD_KILL) && defined(WITH_THREAD) |
---|
1087 | n/a | |
---|
1088 | n/a | /*[clinic input] |
---|
1089 | n/a | signal.pthread_kill |
---|
1090 | n/a | |
---|
1091 | n/a | thread_id: long |
---|
1092 | n/a | signalnum: int |
---|
1093 | n/a | / |
---|
1094 | n/a | |
---|
1095 | n/a | Send a signal to a thread. |
---|
1096 | n/a | [clinic start generated code]*/ |
---|
1097 | n/a | |
---|
1098 | n/a | static PyObject * |
---|
1099 | n/a | signal_pthread_kill_impl(PyObject *module, long thread_id, int signalnum) |
---|
1100 | n/a | /*[clinic end generated code: output=2a09ce41f1c4228a input=77ed6a3b6f2a8122]*/ |
---|
1101 | n/a | { |
---|
1102 | n/a | int err; |
---|
1103 | n/a | |
---|
1104 | n/a | err = pthread_kill((pthread_t)thread_id, signalnum); |
---|
1105 | n/a | if (err != 0) { |
---|
1106 | n/a | errno = err; |
---|
1107 | n/a | PyErr_SetFromErrno(PyExc_OSError); |
---|
1108 | n/a | return NULL; |
---|
1109 | n/a | } |
---|
1110 | n/a | |
---|
1111 | n/a | /* the signal may have been send to the current thread */ |
---|
1112 | n/a | if (PyErr_CheckSignals()) |
---|
1113 | n/a | return NULL; |
---|
1114 | n/a | |
---|
1115 | n/a | Py_RETURN_NONE; |
---|
1116 | n/a | } |
---|
1117 | n/a | |
---|
1118 | n/a | #endif /* #if defined(HAVE_PTHREAD_KILL) && defined(WITH_THREAD) */ |
---|
1119 | n/a | |
---|
1120 | n/a | |
---|
1121 | n/a | |
---|
1122 | n/a | /* List of functions defined in the module -- some of the methoddefs are |
---|
1123 | n/a | defined to nothing if the corresponding C function is not available. */ |
---|
1124 | n/a | static PyMethodDef signal_methods[] = { |
---|
1125 | n/a | {"default_int_handler", signal_default_int_handler, METH_VARARGS, default_int_handler_doc}, |
---|
1126 | n/a | SIGNAL_ALARM_METHODDEF |
---|
1127 | n/a | SIGNAL_SETITIMER_METHODDEF |
---|
1128 | n/a | SIGNAL_GETITIMER_METHODDEF |
---|
1129 | n/a | SIGNAL_SIGNAL_METHODDEF |
---|
1130 | n/a | SIGNAL_GETSIGNAL_METHODDEF |
---|
1131 | n/a | {"set_wakeup_fd", signal_set_wakeup_fd, METH_VARARGS, set_wakeup_fd_doc}, |
---|
1132 | n/a | SIGNAL_SIGINTERRUPT_METHODDEF |
---|
1133 | n/a | SIGNAL_PAUSE_METHODDEF |
---|
1134 | n/a | SIGNAL_PTHREAD_KILL_METHODDEF |
---|
1135 | n/a | SIGNAL_PTHREAD_SIGMASK_METHODDEF |
---|
1136 | n/a | SIGNAL_SIGPENDING_METHODDEF |
---|
1137 | n/a | SIGNAL_SIGWAIT_METHODDEF |
---|
1138 | n/a | SIGNAL_SIGWAITINFO_METHODDEF |
---|
1139 | n/a | SIGNAL_SIGTIMEDWAIT_METHODDEF |
---|
1140 | n/a | {NULL, NULL} /* sentinel */ |
---|
1141 | n/a | }; |
---|
1142 | n/a | |
---|
1143 | n/a | |
---|
1144 | n/a | PyDoc_STRVAR(module_doc, |
---|
1145 | n/a | "This module provides mechanisms to use signal handlers in Python.\n\ |
---|
1146 | n/a | \n\ |
---|
1147 | n/a | Functions:\n\ |
---|
1148 | n/a | \n\ |
---|
1149 | n/a | alarm() -- cause SIGALRM after a specified time [Unix only]\n\ |
---|
1150 | n/a | setitimer() -- cause a signal (described below) after a specified\n\ |
---|
1151 | n/a | float time and the timer may restart then [Unix only]\n\ |
---|
1152 | n/a | getitimer() -- get current value of timer [Unix only]\n\ |
---|
1153 | n/a | signal() -- set the action for a given signal\n\ |
---|
1154 | n/a | getsignal() -- get the signal action for a given signal\n\ |
---|
1155 | n/a | pause() -- wait until a signal arrives [Unix only]\n\ |
---|
1156 | n/a | default_int_handler() -- default SIGINT handler\n\ |
---|
1157 | n/a | \n\ |
---|
1158 | n/a | signal constants:\n\ |
---|
1159 | n/a | SIG_DFL -- used to refer to the system default handler\n\ |
---|
1160 | n/a | SIG_IGN -- used to ignore the signal\n\ |
---|
1161 | n/a | NSIG -- number of defined signals\n\ |
---|
1162 | n/a | SIGINT, SIGTERM, etc. -- signal numbers\n\ |
---|
1163 | n/a | \n\ |
---|
1164 | n/a | itimer constants:\n\ |
---|
1165 | n/a | ITIMER_REAL -- decrements in real time, and delivers SIGALRM upon\n\ |
---|
1166 | n/a | expiration\n\ |
---|
1167 | n/a | ITIMER_VIRTUAL -- decrements only when the process is executing,\n\ |
---|
1168 | n/a | and delivers SIGVTALRM upon expiration\n\ |
---|
1169 | n/a | ITIMER_PROF -- decrements both when the process is executing and\n\ |
---|
1170 | n/a | when the system is executing on behalf of the process.\n\ |
---|
1171 | n/a | Coupled with ITIMER_VIRTUAL, this timer is usually\n\ |
---|
1172 | n/a | used to profile the time spent by the application\n\ |
---|
1173 | n/a | in user and kernel space. SIGPROF is delivered upon\n\ |
---|
1174 | n/a | expiration.\n\ |
---|
1175 | n/a | \n\n\ |
---|
1176 | n/a | *** IMPORTANT NOTICE ***\n\ |
---|
1177 | n/a | A signal handler function is called with two arguments:\n\ |
---|
1178 | n/a | the first is the signal number, the second is the interrupted stack frame."); |
---|
1179 | n/a | |
---|
1180 | n/a | static struct PyModuleDef signalmodule = { |
---|
1181 | n/a | PyModuleDef_HEAD_INIT, |
---|
1182 | n/a | "_signal", |
---|
1183 | n/a | module_doc, |
---|
1184 | n/a | -1, |
---|
1185 | n/a | signal_methods, |
---|
1186 | n/a | NULL, |
---|
1187 | n/a | NULL, |
---|
1188 | n/a | NULL, |
---|
1189 | n/a | NULL |
---|
1190 | n/a | }; |
---|
1191 | n/a | |
---|
1192 | n/a | PyMODINIT_FUNC |
---|
1193 | n/a | PyInit__signal(void) |
---|
1194 | n/a | { |
---|
1195 | n/a | PyObject *m, *d, *x; |
---|
1196 | n/a | int i; |
---|
1197 | n/a | |
---|
1198 | n/a | #ifdef WITH_THREAD |
---|
1199 | n/a | main_thread = PyThread_get_thread_ident(); |
---|
1200 | n/a | main_pid = getpid(); |
---|
1201 | n/a | #endif |
---|
1202 | n/a | |
---|
1203 | n/a | /* Create the module and add the functions */ |
---|
1204 | n/a | m = PyModule_Create(&signalmodule); |
---|
1205 | n/a | if (m == NULL) |
---|
1206 | n/a | return NULL; |
---|
1207 | n/a | |
---|
1208 | n/a | #if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT) |
---|
1209 | n/a | if (!initialized) { |
---|
1210 | n/a | if (PyStructSequence_InitType2(&SiginfoType, &struct_siginfo_desc) < 0) |
---|
1211 | n/a | return NULL; |
---|
1212 | n/a | } |
---|
1213 | n/a | Py_INCREF((PyObject*) &SiginfoType); |
---|
1214 | n/a | PyModule_AddObject(m, "struct_siginfo", (PyObject*) &SiginfoType); |
---|
1215 | n/a | initialized = 1; |
---|
1216 | n/a | #endif |
---|
1217 | n/a | |
---|
1218 | n/a | /* Add some symbolic constants to the module */ |
---|
1219 | n/a | d = PyModule_GetDict(m); |
---|
1220 | n/a | |
---|
1221 | n/a | x = DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL); |
---|
1222 | n/a | if (!x || PyDict_SetItemString(d, "SIG_DFL", x) < 0) |
---|
1223 | n/a | goto finally; |
---|
1224 | n/a | |
---|
1225 | n/a | x = IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN); |
---|
1226 | n/a | if (!x || PyDict_SetItemString(d, "SIG_IGN", x) < 0) |
---|
1227 | n/a | goto finally; |
---|
1228 | n/a | |
---|
1229 | n/a | x = PyLong_FromLong((long)NSIG); |
---|
1230 | n/a | if (!x || PyDict_SetItemString(d, "NSIG", x) < 0) |
---|
1231 | n/a | goto finally; |
---|
1232 | n/a | Py_DECREF(x); |
---|
1233 | n/a | |
---|
1234 | n/a | #ifdef SIG_BLOCK |
---|
1235 | n/a | if (PyModule_AddIntMacro(m, SIG_BLOCK)) |
---|
1236 | n/a | goto finally; |
---|
1237 | n/a | #endif |
---|
1238 | n/a | #ifdef SIG_UNBLOCK |
---|
1239 | n/a | if (PyModule_AddIntMacro(m, SIG_UNBLOCK)) |
---|
1240 | n/a | goto finally; |
---|
1241 | n/a | #endif |
---|
1242 | n/a | #ifdef SIG_SETMASK |
---|
1243 | n/a | if (PyModule_AddIntMacro(m, SIG_SETMASK)) |
---|
1244 | n/a | goto finally; |
---|
1245 | n/a | #endif |
---|
1246 | n/a | |
---|
1247 | n/a | x = IntHandler = PyDict_GetItemString(d, "default_int_handler"); |
---|
1248 | n/a | if (!x) |
---|
1249 | n/a | goto finally; |
---|
1250 | n/a | Py_INCREF(IntHandler); |
---|
1251 | n/a | |
---|
1252 | n/a | Handlers[0].tripped = 0; |
---|
1253 | n/a | for (i = 1; i < NSIG; i++) { |
---|
1254 | n/a | void (*t)(int); |
---|
1255 | n/a | t = PyOS_getsig(i); |
---|
1256 | n/a | Handlers[i].tripped = 0; |
---|
1257 | n/a | if (t == SIG_DFL) |
---|
1258 | n/a | Handlers[i].func = DefaultHandler; |
---|
1259 | n/a | else if (t == SIG_IGN) |
---|
1260 | n/a | Handlers[i].func = IgnoreHandler; |
---|
1261 | n/a | else |
---|
1262 | n/a | Handlers[i].func = Py_None; /* None of our business */ |
---|
1263 | n/a | Py_INCREF(Handlers[i].func); |
---|
1264 | n/a | } |
---|
1265 | n/a | if (Handlers[SIGINT].func == DefaultHandler) { |
---|
1266 | n/a | /* Install default int handler */ |
---|
1267 | n/a | Py_INCREF(IntHandler); |
---|
1268 | n/a | Py_SETREF(Handlers[SIGINT].func, IntHandler); |
---|
1269 | n/a | old_siginthandler = PyOS_setsig(SIGINT, signal_handler); |
---|
1270 | n/a | } |
---|
1271 | n/a | |
---|
1272 | n/a | #ifdef SIGHUP |
---|
1273 | n/a | if (PyModule_AddIntMacro(m, SIGHUP)) |
---|
1274 | n/a | goto finally; |
---|
1275 | n/a | #endif |
---|
1276 | n/a | #ifdef SIGINT |
---|
1277 | n/a | if (PyModule_AddIntMacro(m, SIGINT)) |
---|
1278 | n/a | goto finally; |
---|
1279 | n/a | #endif |
---|
1280 | n/a | #ifdef SIGBREAK |
---|
1281 | n/a | if (PyModule_AddIntMacro(m, SIGBREAK)) |
---|
1282 | n/a | goto finally; |
---|
1283 | n/a | #endif |
---|
1284 | n/a | #ifdef SIGQUIT |
---|
1285 | n/a | if (PyModule_AddIntMacro(m, SIGQUIT)) |
---|
1286 | n/a | goto finally; |
---|
1287 | n/a | #endif |
---|
1288 | n/a | #ifdef SIGILL |
---|
1289 | n/a | if (PyModule_AddIntMacro(m, SIGILL)) |
---|
1290 | n/a | goto finally; |
---|
1291 | n/a | #endif |
---|
1292 | n/a | #ifdef SIGTRAP |
---|
1293 | n/a | if (PyModule_AddIntMacro(m, SIGTRAP)) |
---|
1294 | n/a | goto finally; |
---|
1295 | n/a | #endif |
---|
1296 | n/a | #ifdef SIGIOT |
---|
1297 | n/a | if (PyModule_AddIntMacro(m, SIGIOT)) |
---|
1298 | n/a | goto finally; |
---|
1299 | n/a | #endif |
---|
1300 | n/a | #ifdef SIGABRT |
---|
1301 | n/a | if (PyModule_AddIntMacro(m, SIGABRT)) |
---|
1302 | n/a | goto finally; |
---|
1303 | n/a | #endif |
---|
1304 | n/a | #ifdef SIGEMT |
---|
1305 | n/a | if (PyModule_AddIntMacro(m, SIGEMT)) |
---|
1306 | n/a | goto finally; |
---|
1307 | n/a | #endif |
---|
1308 | n/a | #ifdef SIGFPE |
---|
1309 | n/a | if (PyModule_AddIntMacro(m, SIGFPE)) |
---|
1310 | n/a | goto finally; |
---|
1311 | n/a | #endif |
---|
1312 | n/a | #ifdef SIGKILL |
---|
1313 | n/a | if (PyModule_AddIntMacro(m, SIGKILL)) |
---|
1314 | n/a | goto finally; |
---|
1315 | n/a | #endif |
---|
1316 | n/a | #ifdef SIGBUS |
---|
1317 | n/a | if (PyModule_AddIntMacro(m, SIGBUS)) |
---|
1318 | n/a | goto finally; |
---|
1319 | n/a | #endif |
---|
1320 | n/a | #ifdef SIGSEGV |
---|
1321 | n/a | if (PyModule_AddIntMacro(m, SIGSEGV)) |
---|
1322 | n/a | goto finally; |
---|
1323 | n/a | #endif |
---|
1324 | n/a | #ifdef SIGSYS |
---|
1325 | n/a | if (PyModule_AddIntMacro(m, SIGSYS)) |
---|
1326 | n/a | goto finally; |
---|
1327 | n/a | #endif |
---|
1328 | n/a | #ifdef SIGPIPE |
---|
1329 | n/a | if (PyModule_AddIntMacro(m, SIGPIPE)) |
---|
1330 | n/a | goto finally; |
---|
1331 | n/a | #endif |
---|
1332 | n/a | #ifdef SIGALRM |
---|
1333 | n/a | if (PyModule_AddIntMacro(m, SIGALRM)) |
---|
1334 | n/a | goto finally; |
---|
1335 | n/a | #endif |
---|
1336 | n/a | #ifdef SIGTERM |
---|
1337 | n/a | if (PyModule_AddIntMacro(m, SIGTERM)) |
---|
1338 | n/a | goto finally; |
---|
1339 | n/a | #endif |
---|
1340 | n/a | #ifdef SIGUSR1 |
---|
1341 | n/a | if (PyModule_AddIntMacro(m, SIGUSR1)) |
---|
1342 | n/a | goto finally; |
---|
1343 | n/a | #endif |
---|
1344 | n/a | #ifdef SIGUSR2 |
---|
1345 | n/a | if (PyModule_AddIntMacro(m, SIGUSR2)) |
---|
1346 | n/a | goto finally; |
---|
1347 | n/a | #endif |
---|
1348 | n/a | #ifdef SIGCLD |
---|
1349 | n/a | if (PyModule_AddIntMacro(m, SIGCLD)) |
---|
1350 | n/a | goto finally; |
---|
1351 | n/a | #endif |
---|
1352 | n/a | #ifdef SIGCHLD |
---|
1353 | n/a | if (PyModule_AddIntMacro(m, SIGCHLD)) |
---|
1354 | n/a | goto finally; |
---|
1355 | n/a | #endif |
---|
1356 | n/a | #ifdef SIGPWR |
---|
1357 | n/a | if (PyModule_AddIntMacro(m, SIGPWR)) |
---|
1358 | n/a | goto finally; |
---|
1359 | n/a | #endif |
---|
1360 | n/a | #ifdef SIGIO |
---|
1361 | n/a | if (PyModule_AddIntMacro(m, SIGIO)) |
---|
1362 | n/a | goto finally; |
---|
1363 | n/a | #endif |
---|
1364 | n/a | #ifdef SIGURG |
---|
1365 | n/a | if (PyModule_AddIntMacro(m, SIGURG)) |
---|
1366 | n/a | goto finally; |
---|
1367 | n/a | #endif |
---|
1368 | n/a | #ifdef SIGWINCH |
---|
1369 | n/a | if (PyModule_AddIntMacro(m, SIGWINCH)) |
---|
1370 | n/a | goto finally; |
---|
1371 | n/a | #endif |
---|
1372 | n/a | #ifdef SIGPOLL |
---|
1373 | n/a | if (PyModule_AddIntMacro(m, SIGPOLL)) |
---|
1374 | n/a | goto finally; |
---|
1375 | n/a | #endif |
---|
1376 | n/a | #ifdef SIGSTOP |
---|
1377 | n/a | if (PyModule_AddIntMacro(m, SIGSTOP)) |
---|
1378 | n/a | goto finally; |
---|
1379 | n/a | #endif |
---|
1380 | n/a | #ifdef SIGTSTP |
---|
1381 | n/a | if (PyModule_AddIntMacro(m, SIGTSTP)) |
---|
1382 | n/a | goto finally; |
---|
1383 | n/a | #endif |
---|
1384 | n/a | #ifdef SIGCONT |
---|
1385 | n/a | if (PyModule_AddIntMacro(m, SIGCONT)) |
---|
1386 | n/a | goto finally; |
---|
1387 | n/a | #endif |
---|
1388 | n/a | #ifdef SIGTTIN |
---|
1389 | n/a | if (PyModule_AddIntMacro(m, SIGTTIN)) |
---|
1390 | n/a | goto finally; |
---|
1391 | n/a | #endif |
---|
1392 | n/a | #ifdef SIGTTOU |
---|
1393 | n/a | if (PyModule_AddIntMacro(m, SIGTTOU)) |
---|
1394 | n/a | goto finally; |
---|
1395 | n/a | #endif |
---|
1396 | n/a | #ifdef SIGVTALRM |
---|
1397 | n/a | if (PyModule_AddIntMacro(m, SIGVTALRM)) |
---|
1398 | n/a | goto finally; |
---|
1399 | n/a | #endif |
---|
1400 | n/a | #ifdef SIGPROF |
---|
1401 | n/a | if (PyModule_AddIntMacro(m, SIGPROF)) |
---|
1402 | n/a | goto finally; |
---|
1403 | n/a | #endif |
---|
1404 | n/a | #ifdef SIGXCPU |
---|
1405 | n/a | if (PyModule_AddIntMacro(m, SIGXCPU)) |
---|
1406 | n/a | goto finally; |
---|
1407 | n/a | #endif |
---|
1408 | n/a | #ifdef SIGXFSZ |
---|
1409 | n/a | if (PyModule_AddIntMacro(m, SIGXFSZ)) |
---|
1410 | n/a | goto finally; |
---|
1411 | n/a | #endif |
---|
1412 | n/a | #ifdef SIGRTMIN |
---|
1413 | n/a | if (PyModule_AddIntMacro(m, SIGRTMIN)) |
---|
1414 | n/a | goto finally; |
---|
1415 | n/a | #endif |
---|
1416 | n/a | #ifdef SIGRTMAX |
---|
1417 | n/a | if (PyModule_AddIntMacro(m, SIGRTMAX)) |
---|
1418 | n/a | goto finally; |
---|
1419 | n/a | #endif |
---|
1420 | n/a | #ifdef SIGINFO |
---|
1421 | n/a | if (PyModule_AddIntMacro(m, SIGINFO)) |
---|
1422 | n/a | goto finally; |
---|
1423 | n/a | #endif |
---|
1424 | n/a | |
---|
1425 | n/a | #ifdef ITIMER_REAL |
---|
1426 | n/a | if (PyModule_AddIntMacro(m, ITIMER_REAL)) |
---|
1427 | n/a | goto finally; |
---|
1428 | n/a | #endif |
---|
1429 | n/a | #ifdef ITIMER_VIRTUAL |
---|
1430 | n/a | if (PyModule_AddIntMacro(m, ITIMER_VIRTUAL)) |
---|
1431 | n/a | goto finally; |
---|
1432 | n/a | #endif |
---|
1433 | n/a | #ifdef ITIMER_PROF |
---|
1434 | n/a | if (PyModule_AddIntMacro(m, ITIMER_PROF)) |
---|
1435 | n/a | goto finally; |
---|
1436 | n/a | #endif |
---|
1437 | n/a | |
---|
1438 | n/a | #if defined (HAVE_SETITIMER) || defined (HAVE_GETITIMER) |
---|
1439 | n/a | ItimerError = PyErr_NewException("signal.ItimerError", |
---|
1440 | n/a | PyExc_IOError, NULL); |
---|
1441 | n/a | if (ItimerError != NULL) |
---|
1442 | n/a | PyDict_SetItemString(d, "ItimerError", ItimerError); |
---|
1443 | n/a | #endif |
---|
1444 | n/a | |
---|
1445 | n/a | #ifdef CTRL_C_EVENT |
---|
1446 | n/a | if (PyModule_AddIntMacro(m, CTRL_C_EVENT)) |
---|
1447 | n/a | goto finally; |
---|
1448 | n/a | #endif |
---|
1449 | n/a | |
---|
1450 | n/a | #ifdef CTRL_BREAK_EVENT |
---|
1451 | n/a | if (PyModule_AddIntMacro(m, CTRL_BREAK_EVENT)) |
---|
1452 | n/a | goto finally; |
---|
1453 | n/a | #endif |
---|
1454 | n/a | |
---|
1455 | n/a | #ifdef MS_WINDOWS |
---|
1456 | n/a | /* Create manual-reset event, initially unset */ |
---|
1457 | n/a | sigint_event = CreateEvent(NULL, TRUE, FALSE, FALSE); |
---|
1458 | n/a | #endif |
---|
1459 | n/a | |
---|
1460 | n/a | if (PyErr_Occurred()) { |
---|
1461 | n/a | Py_DECREF(m); |
---|
1462 | n/a | m = NULL; |
---|
1463 | n/a | } |
---|
1464 | n/a | |
---|
1465 | n/a | finally: |
---|
1466 | n/a | return m; |
---|
1467 | n/a | } |
---|
1468 | n/a | |
---|
1469 | n/a | static void |
---|
1470 | n/a | finisignal(void) |
---|
1471 | n/a | { |
---|
1472 | n/a | int i; |
---|
1473 | n/a | PyObject *func; |
---|
1474 | n/a | |
---|
1475 | n/a | PyOS_setsig(SIGINT, old_siginthandler); |
---|
1476 | n/a | old_siginthandler = SIG_DFL; |
---|
1477 | n/a | |
---|
1478 | n/a | for (i = 1; i < NSIG; i++) { |
---|
1479 | n/a | func = Handlers[i].func; |
---|
1480 | n/a | Handlers[i].tripped = 0; |
---|
1481 | n/a | Handlers[i].func = NULL; |
---|
1482 | n/a | if (i != SIGINT && func != NULL && func != Py_None && |
---|
1483 | n/a | func != DefaultHandler && func != IgnoreHandler) |
---|
1484 | n/a | PyOS_setsig(i, SIG_DFL); |
---|
1485 | n/a | Py_XDECREF(func); |
---|
1486 | n/a | } |
---|
1487 | n/a | |
---|
1488 | n/a | Py_CLEAR(IntHandler); |
---|
1489 | n/a | Py_CLEAR(DefaultHandler); |
---|
1490 | n/a | Py_CLEAR(IgnoreHandler); |
---|
1491 | n/a | } |
---|
1492 | n/a | |
---|
1493 | n/a | |
---|
1494 | n/a | /* Declared in pyerrors.h */ |
---|
1495 | n/a | int |
---|
1496 | n/a | PyErr_CheckSignals(void) |
---|
1497 | n/a | { |
---|
1498 | n/a | int i; |
---|
1499 | n/a | PyObject *f; |
---|
1500 | n/a | |
---|
1501 | n/a | if (!is_tripped) |
---|
1502 | n/a | return 0; |
---|
1503 | n/a | |
---|
1504 | n/a | #ifdef WITH_THREAD |
---|
1505 | n/a | if (PyThread_get_thread_ident() != main_thread) |
---|
1506 | n/a | return 0; |
---|
1507 | n/a | #endif |
---|
1508 | n/a | |
---|
1509 | n/a | /* |
---|
1510 | n/a | * The is_tripped variable is meant to speed up the calls to |
---|
1511 | n/a | * PyErr_CheckSignals (both directly or via pending calls) when no |
---|
1512 | n/a | * signal has arrived. This variable is set to 1 when a signal arrives |
---|
1513 | n/a | * and it is set to 0 here, when we know some signals arrived. This way |
---|
1514 | n/a | * we can run the registered handlers with no signals blocked. |
---|
1515 | n/a | * |
---|
1516 | n/a | * NOTE: with this approach we can have a situation where is_tripped is |
---|
1517 | n/a | * 1 but we have no more signals to handle (Handlers[i].tripped |
---|
1518 | n/a | * is 0 for every signal i). This won't do us any harm (except |
---|
1519 | n/a | * we're gonna spent some cycles for nothing). This happens when |
---|
1520 | n/a | * we receive a signal i after we zero is_tripped and before we |
---|
1521 | n/a | * check Handlers[i].tripped. |
---|
1522 | n/a | */ |
---|
1523 | n/a | is_tripped = 0; |
---|
1524 | n/a | |
---|
1525 | n/a | if (!(f = (PyObject *)PyEval_GetFrame())) |
---|
1526 | n/a | f = Py_None; |
---|
1527 | n/a | |
---|
1528 | n/a | for (i = 1; i < NSIG; i++) { |
---|
1529 | n/a | if (Handlers[i].tripped) { |
---|
1530 | n/a | PyObject *result = NULL; |
---|
1531 | n/a | PyObject *arglist = Py_BuildValue("(iO)", i, f); |
---|
1532 | n/a | Handlers[i].tripped = 0; |
---|
1533 | n/a | |
---|
1534 | n/a | if (arglist) { |
---|
1535 | n/a | result = PyEval_CallObject(Handlers[i].func, |
---|
1536 | n/a | arglist); |
---|
1537 | n/a | Py_DECREF(arglist); |
---|
1538 | n/a | } |
---|
1539 | n/a | if (!result) |
---|
1540 | n/a | return -1; |
---|
1541 | n/a | |
---|
1542 | n/a | Py_DECREF(result); |
---|
1543 | n/a | } |
---|
1544 | n/a | } |
---|
1545 | n/a | |
---|
1546 | n/a | return 0; |
---|
1547 | n/a | } |
---|
1548 | n/a | |
---|
1549 | n/a | |
---|
1550 | n/a | /* Replacements for intrcheck.c functionality |
---|
1551 | n/a | * Declared in pyerrors.h |
---|
1552 | n/a | */ |
---|
1553 | n/a | void |
---|
1554 | n/a | PyErr_SetInterrupt(void) |
---|
1555 | n/a | { |
---|
1556 | n/a | trip_signal(SIGINT); |
---|
1557 | n/a | } |
---|
1558 | n/a | |
---|
1559 | n/a | void |
---|
1560 | n/a | PyOS_InitInterrupts(void) |
---|
1561 | n/a | { |
---|
1562 | n/a | PyObject *m = PyImport_ImportModule("_signal"); |
---|
1563 | n/a | if (m) { |
---|
1564 | n/a | Py_DECREF(m); |
---|
1565 | n/a | } |
---|
1566 | n/a | } |
---|
1567 | n/a | |
---|
1568 | n/a | void |
---|
1569 | n/a | PyOS_FiniInterrupts(void) |
---|
1570 | n/a | { |
---|
1571 | n/a | finisignal(); |
---|
1572 | n/a | } |
---|
1573 | n/a | |
---|
1574 | n/a | int |
---|
1575 | n/a | PyOS_InterruptOccurred(void) |
---|
1576 | n/a | { |
---|
1577 | n/a | if (Handlers[SIGINT].tripped) { |
---|
1578 | n/a | #ifdef WITH_THREAD |
---|
1579 | n/a | if (PyThread_get_thread_ident() != main_thread) |
---|
1580 | n/a | return 0; |
---|
1581 | n/a | #endif |
---|
1582 | n/a | Handlers[SIGINT].tripped = 0; |
---|
1583 | n/a | return 1; |
---|
1584 | n/a | } |
---|
1585 | n/a | return 0; |
---|
1586 | n/a | } |
---|
1587 | n/a | |
---|
1588 | n/a | static void |
---|
1589 | n/a | _clear_pending_signals(void) |
---|
1590 | n/a | { |
---|
1591 | n/a | int i; |
---|
1592 | n/a | if (!is_tripped) |
---|
1593 | n/a | return; |
---|
1594 | n/a | is_tripped = 0; |
---|
1595 | n/a | for (i = 1; i < NSIG; ++i) { |
---|
1596 | n/a | Handlers[i].tripped = 0; |
---|
1597 | n/a | } |
---|
1598 | n/a | } |
---|
1599 | n/a | |
---|
1600 | n/a | void |
---|
1601 | n/a | PyOS_AfterFork(void) |
---|
1602 | n/a | { |
---|
1603 | n/a | /* Clear the signal flags after forking so that they aren't handled |
---|
1604 | n/a | * in both processes if they came in just before the fork() but before |
---|
1605 | n/a | * the interpreter had an opportunity to call the handlers. issue9535. */ |
---|
1606 | n/a | _clear_pending_signals(); |
---|
1607 | n/a | #ifdef WITH_THREAD |
---|
1608 | n/a | /* PyThread_ReInitTLS() must be called early, to make sure that the TLS API |
---|
1609 | n/a | * can be called safely. */ |
---|
1610 | n/a | PyThread_ReInitTLS(); |
---|
1611 | n/a | _PyGILState_Reinit(); |
---|
1612 | n/a | PyEval_ReInitThreads(); |
---|
1613 | n/a | main_thread = PyThread_get_thread_ident(); |
---|
1614 | n/a | main_pid = getpid(); |
---|
1615 | n/a | _PyImport_ReInitLock(); |
---|
1616 | n/a | #endif |
---|
1617 | n/a | } |
---|
1618 | n/a | |
---|
1619 | n/a | int |
---|
1620 | n/a | _PyOS_IsMainThread(void) |
---|
1621 | n/a | { |
---|
1622 | n/a | #ifdef WITH_THREAD |
---|
1623 | n/a | return PyThread_get_thread_ident() == main_thread; |
---|
1624 | n/a | #else |
---|
1625 | n/a | return 1; |
---|
1626 | n/a | #endif |
---|
1627 | n/a | } |
---|
1628 | n/a | |
---|
1629 | n/a | #ifdef MS_WINDOWS |
---|
1630 | n/a | void *_PyOS_SigintEvent(void) |
---|
1631 | n/a | { |
---|
1632 | n/a | /* Returns a manual-reset event which gets tripped whenever |
---|
1633 | n/a | SIGINT is received. |
---|
1634 | n/a | |
---|
1635 | n/a | Python.h does not include windows.h so we do cannot use HANDLE |
---|
1636 | n/a | as the return type of this function. We use void* instead. */ |
---|
1637 | n/a | return sigint_event; |
---|
1638 | n/a | } |
---|
1639 | n/a | #endif |
---|