blob: 1c827ac26da647776869c66edd709384e0767ead [file] [log] [blame]
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002/* Signal module -- many thanks to Lance Ellinghaus */
Guido van Rossum398d9fe1994-05-11 08:59:13 +00003
Guido van Rossum644a12b1997-04-09 19:24:53 +00004/* XXX Signals should be recorded per thread, now we have thread state. */
5
Guido van Rossum602099a1994-09-14 13:32:22 +00006#include "Python.h"
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007#ifndef MS_WINDOWS
8#include "posixmodule.h"
9#endif
Victor Stinner11517102014-07-29 23:31:34 +020010#ifdef MS_WINDOWS
11#include "socketmodule.h" /* needed for SOCKET_T */
12#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +000013
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000014#ifdef MS_WINDOWS
Antoine Pitrou7faf7052013-03-31 22:48:04 +020015#include <windows.h>
Benjamin Peterson2614cda2010-03-21 22:36:19 +000016#ifdef HAVE_PROCESS_H
Guido van Rossum644a12b1997-04-09 19:24:53 +000017#include <process.h>
18#endif
Benjamin Peterson2614cda2010-03-21 22:36:19 +000019#endif
Guido van Rossum644a12b1997-04-09 19:24:53 +000020
Benjamin Peterson2614cda2010-03-21 22:36:19 +000021#ifdef HAVE_SIGNAL_H
Guido van Rossum398d9fe1994-05-11 08:59:13 +000022#include <signal.h>
Benjamin Peterson2614cda2010-03-21 22:36:19 +000023#endif
24#ifdef HAVE_SYS_STAT_H
Christian Heimes5fb7c2a2007-12-24 08:52:31 +000025#include <sys/stat.h>
Benjamin Peterson2614cda2010-03-21 22:36:19 +000026#endif
Martin v. Löwis3bf3cc02008-03-24 14:05:07 +000027#ifdef HAVE_SYS_TIME_H
Martin v. Löwis823725e2008-03-24 13:39:54 +000028#include <sys/time.h>
Martin v. Löwis3bf3cc02008-03-24 14:05:07 +000029#endif
Christian Heimes5fb7c2a2007-12-24 08:52:31 +000030
Victor Stinnera9293352011-04-30 15:21:58 +020031#if defined(HAVE_PTHREAD_SIGMASK) && !defined(HAVE_BROKEN_PTHREAD_SIGMASK)
32# define PYPTHREAD_SIGMASK
33#endif
34
35#if defined(PYPTHREAD_SIGMASK) && defined(HAVE_PTHREAD_H)
36# include <pthread.h>
37#endif
38
Guido van Rossumbb4ba121994-06-23 11:25:45 +000039#ifndef SIG_ERR
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +000040#define SIG_ERR ((PyOS_sighandler_t)(-1))
Guido van Rossumbb4ba121994-06-23 11:25:45 +000041#endif
42
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000043#ifndef NSIG
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000044# if defined(_NSIG)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000045# define NSIG _NSIG /* For BSD/SysV */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000046# elif defined(_SIGMAX)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000047# define NSIG (_SIGMAX + 1) /* For QNX */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000048# elif defined(SIGMAX)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000049# define NSIG (SIGMAX + 1) /* For djgpp */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000050# else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000051# define NSIG 64 /* Use a reasonable default value */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000052# endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000053#endif
54
Tal Einatc7027b72015-05-16 14:14:49 +030055#include "clinic/signalmodule.c.h"
56
57/*[clinic input]
58module signal
59[clinic start generated code]*/
60/*[clinic end generated code: output=da39a3ee5e6b4b0d input=b0301a3bde5fe9d3]*/
61
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000062
Guido van Rossumbb4ba121994-06-23 11:25:45 +000063/*
64 NOTES ON THE INTERACTION BETWEEN SIGNALS AND THREADS
65
66 When threads are supported, we want the following semantics:
67
68 - only the main thread can set a signal handler
69 - any thread can get a signal handler
70 - signals are only delivered to the main thread
71
72 I.e. we don't support "synchronous signals" like SIGFPE (catching
73 this doesn't make much sense in Python anyway) nor do we support
74 signals as a means of inter-thread communication, since not all
75 thread implementations support that (at least our thread library
76 doesn't).
77
78 We still have the problem that in some implementations signals
79 generated by the keyboard (e.g. SIGINT) are delivered to all
80 threads (e.g. SGI), while in others (e.g. Solaris) such signals are
81 delivered to one random thread (an intermediate possibility would
Guido van Rossuma3c04b01995-01-12 11:29:01 +000082 be to deliver it to the main thread -- POSIX?). For now, we have
Guido van Rossumbb4ba121994-06-23 11:25:45 +000083 a working implementation that works in all three cases -- the
84 handler ignores signals if getpid() isn't the same as in the main
85 thread. XXX This is a hack.
Guido van Rossumbb4ba121994-06-23 11:25:45 +000086*/
87
88#ifdef WITH_THREAD
Guido van Rossum295b8e51997-06-06 21:16:41 +000089#include <sys/types.h> /* For pid_t */
Guido van Rossum49b56061998-10-01 20:42:43 +000090#include "pythread.h"
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +020091static unsigned long main_thread;
Guido van Rossumbb4ba121994-06-23 11:25:45 +000092static pid_t main_pid;
93#endif
94
Victor Stinner2ec6b172011-05-15 10:21:59 +020095static volatile struct {
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +020096 _Py_atomic_int tripped;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000097 PyObject *func;
Barry Warsaw92971171997-01-03 00:14:25 +000098} Handlers[NSIG];
Guido van Rossum398d9fe1994-05-11 08:59:13 +000099
Victor Stinner11517102014-07-29 23:31:34 +0200100#ifdef MS_WINDOWS
101#define INVALID_FD ((SOCKET_T)-1)
102
103static volatile struct {
104 SOCKET_T fd;
105 int use_send;
106 int send_err_set;
107 int send_errno;
108 int send_win_error;
109} wakeup = {INVALID_FD, 0, 0};
110#else
111#define INVALID_FD (-1)
Victor Stinner2ec6b172011-05-15 10:21:59 +0200112static volatile sig_atomic_t wakeup_fd = -1;
Victor Stinner11517102014-07-29 23:31:34 +0200113#endif
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000114
Christian Heimesb76922a2007-12-11 01:06:40 +0000115/* Speed up sigcheck() when none tripped */
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +0200116static _Py_atomic_int is_tripped;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000117
Barry Warsaw92971171997-01-03 00:14:25 +0000118static PyObject *DefaultHandler;
119static PyObject *IgnoreHandler;
120static PyObject *IntHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000121
Martin v. Löwisf58de1b2001-03-06 12:13:56 +0000122/* On Solaris 8, gcc will produce a warning that the function
123 declaration is not a prototype. This is caused by the definition of
124 SIG_DFL as (void (*)())0; the correct declaration would have been
125 (void (*)(int))0. */
126
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000127static PyOS_sighandler_t old_siginthandler = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +0000128
Antoine Pitrou6dd381e2011-11-21 21:26:56 +0100129#ifdef MS_WINDOWS
130static HANDLE sigint_event = NULL;
131#endif
132
Martin v. Löwis823725e2008-03-24 13:39:54 +0000133#ifdef HAVE_GETITIMER
134static PyObject *ItimerError;
135
136/* auxiliary functions for setitimer/getitimer */
137static void
138timeval_from_double(double d, struct timeval *tv)
139{
140 tv->tv_sec = floor(d);
141 tv->tv_usec = fmod(d, 1.0) * 1000000.0;
Antoine Pitrou729780a2017-06-30 10:01:05 +0200142 /* Don't disable the timer if the computation above rounds down to zero. */
143 if (d > 0.0 && tv->tv_sec == 0 && tv->tv_usec == 0) {
144 tv->tv_usec = 1;
145 }
Martin v. Löwis823725e2008-03-24 13:39:54 +0000146}
147
Christian Heimes1a8501c2008-10-02 19:56:01 +0000148Py_LOCAL_INLINE(double)
Martin v. Löwis823725e2008-03-24 13:39:54 +0000149double_from_timeval(struct timeval *tv)
150{
151 return tv->tv_sec + (double)(tv->tv_usec / 1000000.0);
152}
153
154static PyObject *
155itimer_retval(struct itimerval *iv)
156{
157 PyObject *r, *v;
158
159 r = PyTuple_New(2);
160 if (r == NULL)
Martin Panter6d57fe12016-09-17 03:26:16 +0000161 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000162
163 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_value)))) {
Martin Panter6d57fe12016-09-17 03:26:16 +0000164 Py_DECREF(r);
165 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000166 }
167
168 PyTuple_SET_ITEM(r, 0, v);
169
170 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_interval)))) {
Martin Panter6d57fe12016-09-17 03:26:16 +0000171 Py_DECREF(r);
172 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000173 }
174
175 PyTuple_SET_ITEM(r, 1, v);
176
177 return r;
178}
179#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000180
Guido van Rossume4485b01994-09-07 14:32:49 +0000181static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000182signal_default_int_handler(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000183{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000184 PyErr_SetNone(PyExc_KeyboardInterrupt);
185 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000186}
187
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000188PyDoc_STRVAR(default_int_handler_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000189"default_int_handler(...)\n\
190\n\
Michael W. Hudson24ec2112004-06-17 15:55:53 +0000191The default handler for SIGINT installed by Python.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000192It raises KeyboardInterrupt.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000193
Thomas Wouters0796b002000-07-22 23:49:30 +0000194
195static int
Victor Stinner11517102014-07-29 23:31:34 +0200196report_wakeup_write_error(void *data)
Antoine Pitrou6f6ec372013-08-17 20:27:56 +0200197{
198 int save_errno = errno;
Benjamin Petersonca470632016-09-06 13:47:26 -0700199 errno = (int) (intptr_t) data;
Antoine Pitrou6f6ec372013-08-17 20:27:56 +0200200 PyErr_SetFromErrno(PyExc_OSError);
201 PySys_WriteStderr("Exception ignored when trying to write to the "
202 "signal wakeup fd:\n");
203 PyErr_WriteUnraisable(NULL);
204 errno = save_errno;
205 return 0;
206}
207
Victor Stinner11517102014-07-29 23:31:34 +0200208#ifdef MS_WINDOWS
209static int
210report_wakeup_send_error(void* Py_UNUSED(data))
211{
212 PyObject *res;
213
214 if (wakeup.send_win_error) {
215 /* PyErr_SetExcFromWindowsErr() invokes FormatMessage() which
216 recognizes the error codes used by both GetLastError() and
217 WSAGetLastError */
218 res = PyErr_SetExcFromWindowsErr(PyExc_OSError, wakeup.send_win_error);
219 }
220 else {
221 errno = wakeup.send_errno;
222 res = PyErr_SetFromErrno(PyExc_OSError);
223 }
224
225 assert(res == NULL);
226 wakeup.send_err_set = 0;
227
228 PySys_WriteStderr("Exception ignored when trying to send to the "
229 "signal wakeup fd:\n");
230 PyErr_WriteUnraisable(NULL);
231
232 return 0;
233}
234#endif /* MS_WINDOWS */
235
Tim Peters4f1b2082000-07-23 21:18:09 +0000236static void
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200237trip_signal(int sig_num)
238{
Victor Stinnerd49b1f12011-05-08 02:03:15 +0200239 unsigned char byte;
Victor Stinner11517102014-07-29 23:31:34 +0200240 int fd;
241 Py_ssize_t rc;
Victor Stinnerc13ef662011-05-25 02:35:58 +0200242
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +0200243 _Py_atomic_store_relaxed(&Handlers[sig_num].tripped, 1);
Victor Stinner11517102014-07-29 23:31:34 +0200244
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200245 /* Set is_tripped after setting .tripped, as it gets
246 cleared in PyErr_CheckSignals() before .tripped. */
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +0200247 _Py_atomic_store(&is_tripped, 1);
248
249 /* Notify ceval.c */
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200250 _PyEval_SignalReceived();
Nathaniel J. Smith4ae01492017-05-16 14:12:11 -0700251
252 /* And then write to the wakeup fd *after* setting all the globals and
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200253 doing the _PyEval_SignalReceived. We used to write to the wakeup fd
254 and then set the flag, but this allowed the following sequence of events
255 (especially on windows, where trip_signal may run in a new thread):
Nathaniel J. Smith4ae01492017-05-16 14:12:11 -0700256
257 - main thread blocks on select([wakeup_fd], ...)
258 - signal arrives
259 - trip_signal writes to the wakeup fd
260 - the main thread wakes up
261 - the main thread checks the signal flags, sees that they're unset
262 - the main thread empties the wakeup fd
263 - the main thread goes back to sleep
264 - trip_signal sets the flags to request the Python-level signal handler
265 be run
266 - the main thread doesn't notice, because it's asleep
267
268 See bpo-30038 for more details.
269 */
270
Victor Stinner11517102014-07-29 23:31:34 +0200271#ifdef MS_WINDOWS
272 fd = Py_SAFE_DOWNCAST(wakeup.fd, SOCKET_T, int);
273#else
274 fd = wakeup_fd;
275#endif
276
277 if (fd != INVALID_FD) {
Victor Stinnerc13ef662011-05-25 02:35:58 +0200278 byte = (unsigned char)sig_num;
Victor Stinner11517102014-07-29 23:31:34 +0200279#ifdef MS_WINDOWS
280 if (wakeup.use_send) {
281 do {
282 rc = send(fd, &byte, 1, 0);
283 } while (rc < 0 && errno == EINTR);
284
285 /* we only have a storage for one error in the wakeup structure */
286 if (rc < 0 && !wakeup.send_err_set) {
287 wakeup.send_err_set = 1;
288 wakeup.send_errno = errno;
289 wakeup.send_win_error = GetLastError();
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200290 /* Py_AddPendingCall() isn't signal-safe, but we
291 still use it for this exceptional case. */
Victor Stinner11517102014-07-29 23:31:34 +0200292 Py_AddPendingCall(report_wakeup_send_error, NULL);
293 }
294 }
295 else
296#endif
297 {
298 byte = (unsigned char)sig_num;
Victor Stinnere72fe392015-04-01 18:35:22 +0200299
300 /* _Py_write_noraise() retries write() if write() is interrupted by
301 a signal (fails with EINTR). */
302 rc = _Py_write_noraise(fd, &byte, 1);
Victor Stinner11517102014-07-29 23:31:34 +0200303
304 if (rc < 0) {
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200305 /* Py_AddPendingCall() isn't signal-safe, but we
306 still use it for this exceptional case. */
Victor Stinner11517102014-07-29 23:31:34 +0200307 Py_AddPendingCall(report_wakeup_write_error,
Benjamin Petersonca470632016-09-06 13:47:26 -0700308 (void *)(intptr_t)errno);
Victor Stinner11517102014-07-29 23:31:34 +0200309 }
310 }
Victor Stinnerc13ef662011-05-25 02:35:58 +0200311 }
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200312}
313
314static void
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000315signal_handler(int sig_num)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000316{
Antoine Pitrou39a65912010-11-05 19:47:27 +0000317 int save_errno = errno;
318
Antoine Pitrou39a65912010-11-05 19:47:27 +0000319#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000320 /* See NOTES section above */
Antoine Pitrou39a65912010-11-05 19:47:27 +0000321 if (getpid() == main_pid)
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000322#endif
Antoine Pitrou39a65912010-11-05 19:47:27 +0000323 {
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200324 trip_signal(sig_num);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000325 }
Antoine Pitrou39a65912010-11-05 19:47:27 +0000326
Jean-Paul Calderone6f137ca2010-05-09 03:18:57 +0000327#ifndef HAVE_SIGACTION
Antoine Pitrou39a65912010-11-05 19:47:27 +0000328#ifdef SIGCHLD
329 /* To avoid infinite recursion, this signal remains
330 reset until explicit re-instated.
331 Don't clear the 'func' field as it is our pointer
332 to the Python handler... */
333 if (sig_num != SIGCHLD)
334#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000335 /* If the handler was not set up with sigaction, reinstall it. See
Nick Coghland6009512014-11-20 21:39:37 +1000336 * Python/pylifecycle.c for the implementation of PyOS_setsig which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000337 * makes this true. See also issue8354. */
338 PyOS_setsig(sig_num, signal_handler);
Jean-Paul Calderone6f137ca2010-05-09 03:18:57 +0000339#endif
Antoine Pitrou39a65912010-11-05 19:47:27 +0000340
341 /* Issue #10311: asynchronously executing signal handlers should not
342 mutate errno under the feet of unsuspecting C code. */
343 errno = save_errno;
Antoine Pitrou6dd381e2011-11-21 21:26:56 +0100344
345#ifdef MS_WINDOWS
346 if (sig_num == SIGINT)
347 SetEvent(sigint_event);
348#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000349}
Guido van Rossume4485b01994-09-07 14:32:49 +0000350
Guido van Rossum06d511d1995-03-10 15:13:48 +0000351
Guido van Rossum1171ee61997-08-22 20:42:00 +0000352#ifdef HAVE_ALARM
Tal Einatc7027b72015-05-16 14:14:49 +0300353
354/*[clinic input]
355signal.alarm -> long
356
357 seconds: int
358 /
359
360Arrange for SIGALRM to arrive after the given number of seconds.
361[clinic start generated code]*/
362
363static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300364signal_alarm_impl(PyObject *module, int seconds)
365/*[clinic end generated code: output=144232290814c298 input=0d5e97e0e6f39e86]*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000366{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000367 /* alarm() returns the number of seconds remaining */
Tal Einatc7027b72015-05-16 14:14:49 +0300368 return (long)alarm(seconds);
Guido van Rossumaa0f4c71994-08-23 13:49:37 +0000369}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000370
Guido van Rossum06d511d1995-03-10 15:13:48 +0000371#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000372
Guido van Rossum1171ee61997-08-22 20:42:00 +0000373#ifdef HAVE_PAUSE
Tal Einatc7027b72015-05-16 14:14:49 +0300374
375/*[clinic input]
376signal.pause
377
378Wait until a signal arrives.
379[clinic start generated code]*/
380
Guido van Rossuma597dde1995-01-10 20:56:29 +0000381static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300382signal_pause_impl(PyObject *module)
383/*[clinic end generated code: output=391656788b3c3929 input=f03de0f875752062]*/
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000384{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000385 Py_BEGIN_ALLOW_THREADS
386 (void)pause();
387 Py_END_ALLOW_THREADS
388 /* make sure that any exceptions that got raised are propagated
389 * back into Python
390 */
391 if (PyErr_CheckSignals())
392 return NULL;
Barry Warsaw92971171997-01-03 00:14:25 +0000393
Tal Einatc7027b72015-05-16 14:14:49 +0300394 Py_RETURN_NONE;
Guido van Rossume4485b01994-09-07 14:32:49 +0000395}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000396
Guido van Rossum06d511d1995-03-10 15:13:48 +0000397#endif
Guido van Rossume4485b01994-09-07 14:32:49 +0000398
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000399
Tal Einatc7027b72015-05-16 14:14:49 +0300400/*[clinic input]
401signal.signal
402
403 signalnum: int
404 handler: object
405 /
406
407Set the action for the given signal.
408
409The action can be SIG_DFL, SIG_IGN, or a callable Python object.
410The previous action is returned. See getsignal() for possible return values.
411
412*** IMPORTANT NOTICE ***
413A signal handler function is called with two arguments:
414the first is the signal number, the second is the interrupted stack frame.
415[clinic start generated code]*/
416
Guido van Rossume4485b01994-09-07 14:32:49 +0000417static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300418signal_signal_impl(PyObject *module, int signalnum, PyObject *handler)
419/*[clinic end generated code: output=b44cfda43780f3a1 input=deee84af5fa0432c]*/
Guido van Rossume4485b01994-09-07 14:32:49 +0000420{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000421 PyObject *old_handler;
422 void (*func)(int);
Brian Curtinef9efbd2010-08-06 19:27:32 +0000423#ifdef MS_WINDOWS
Tal Einatc7027b72015-05-16 14:14:49 +0300424 /* Validate that signalnum is one of the allowable signals */
425 switch (signalnum) {
Brian Curtinc734b312010-09-06 16:04:10 +0000426 case SIGABRT: break;
Brian Curtin9e88b5a2010-10-01 14:49:24 +0000427#ifdef SIGBREAK
428 /* Issue #10003: SIGBREAK is not documented as permitted, but works
429 and corresponds to CTRL_BREAK_EVENT. */
430 case SIGBREAK: break;
431#endif
Brian Curtinc734b312010-09-06 16:04:10 +0000432 case SIGFPE: break;
433 case SIGILL: break;
434 case SIGINT: break;
435 case SIGSEGV: break;
436 case SIGTERM: break;
437 default:
438 PyErr_SetString(PyExc_ValueError, "invalid signal value");
439 return NULL;
Brian Curtinef9efbd2010-08-06 19:27:32 +0000440 }
441#endif
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000442#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000443 if (PyThread_get_thread_ident() != main_thread) {
444 PyErr_SetString(PyExc_ValueError,
445 "signal only works in main thread");
446 return NULL;
447 }
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000448#endif
Tal Einatc7027b72015-05-16 14:14:49 +0300449 if (signalnum < 1 || signalnum >= NSIG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000450 PyErr_SetString(PyExc_ValueError,
451 "signal number out of range");
452 return NULL;
453 }
Tal Einatc7027b72015-05-16 14:14:49 +0300454 if (handler == IgnoreHandler)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000455 func = SIG_IGN;
Tal Einatc7027b72015-05-16 14:14:49 +0300456 else if (handler == DefaultHandler)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000457 func = SIG_DFL;
Tal Einatc7027b72015-05-16 14:14:49 +0300458 else if (!PyCallable_Check(handler)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000459 PyErr_SetString(PyExc_TypeError,
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000460"signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000461 return NULL;
462 }
463 else
464 func = signal_handler;
Tal Einatc7027b72015-05-16 14:14:49 +0300465 if (PyOS_setsig(signalnum, func) == SIG_ERR) {
Victor Stinner388196e2011-05-10 17:13:00 +0200466 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000467 return NULL;
468 }
Tal Einatc7027b72015-05-16 14:14:49 +0300469 old_handler = Handlers[signalnum].func;
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +0200470 _Py_atomic_store_relaxed(&Handlers[signalnum].tripped, 0);
Tal Einatc7027b72015-05-16 14:14:49 +0300471 Py_INCREF(handler);
472 Handlers[signalnum].func = handler;
Antoine Pitrouc8c952c2013-05-04 23:16:59 +0200473 if (old_handler != NULL)
474 return old_handler;
475 else
476 Py_RETURN_NONE;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000477}
478
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000479
Tal Einatc7027b72015-05-16 14:14:49 +0300480/*[clinic input]
481signal.getsignal
482
483 signalnum: int
484 /
485
486Return the current action for the given signal.
487
488The return value can be:
489 SIG_IGN -- if the signal is being ignored
490 SIG_DFL -- if the default action for the signal is in effect
491 None -- if an unknown handler is in effect
492 anything else -- the callable Python object used as a handler
493[clinic start generated code]*/
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000494
Guido van Rossume4485b01994-09-07 14:32:49 +0000495static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300496signal_getsignal_impl(PyObject *module, int signalnum)
497/*[clinic end generated code: output=35b3e0e796fd555e input=ac23a00f19dfa509]*/
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000498{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 PyObject *old_handler;
Tal Einatc7027b72015-05-16 14:14:49 +0300500 if (signalnum < 1 || signalnum >= NSIG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000501 PyErr_SetString(PyExc_ValueError,
502 "signal number out of range");
503 return NULL;
504 }
Tal Einatc7027b72015-05-16 14:14:49 +0300505 old_handler = Handlers[signalnum].func;
Antoine Pitrouc8c952c2013-05-04 23:16:59 +0200506 if (old_handler != NULL) {
507 Py_INCREF(old_handler);
508 return old_handler;
509 }
510 else {
511 Py_RETURN_NONE;
512 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000513}
514
Christian Heimes8640e742008-02-23 16:23:06 +0000515#ifdef HAVE_SIGINTERRUPT
Tal Einatc7027b72015-05-16 14:14:49 +0300516
517/*[clinic input]
518signal.siginterrupt
519
520 signalnum: int
521 flag: int
522 /
523
524Change system call restart behaviour.
525
526If flag is False, system calls will be restarted when interrupted by
527signal sig, else system calls will be interrupted.
528[clinic start generated code]*/
Christian Heimes8640e742008-02-23 16:23:06 +0000529
530static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300531signal_siginterrupt_impl(PyObject *module, int signalnum, int flag)
532/*[clinic end generated code: output=063816243d85dd19 input=4160acacca3e2099]*/
Christian Heimes8640e742008-02-23 16:23:06 +0000533{
Tal Einatc7027b72015-05-16 14:14:49 +0300534 if (signalnum < 1 || signalnum >= NSIG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000535 PyErr_SetString(PyExc_ValueError,
536 "signal number out of range");
537 return NULL;
538 }
Tal Einatc7027b72015-05-16 14:14:49 +0300539 if (siginterrupt(signalnum, flag)<0) {
Victor Stinner388196e2011-05-10 17:13:00 +0200540 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000541 return NULL;
542 }
Tal Einatc7027b72015-05-16 14:14:49 +0300543 Py_RETURN_NONE;
Christian Heimes8640e742008-02-23 16:23:06 +0000544}
545
546#endif
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000547
Tal Einatc7027b72015-05-16 14:14:49 +0300548
549static PyObject*
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000550signal_set_wakeup_fd(PyObject *self, PyObject *args)
551{
Victor Stinnere134a7f2015-03-30 10:09:31 +0200552 struct _Py_stat_struct status;
Victor Stinner11517102014-07-29 23:31:34 +0200553#ifdef MS_WINDOWS
554 PyObject *fdobj;
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100555 SOCKET_T sockfd, old_sockfd;
Victor Stinner11517102014-07-29 23:31:34 +0200556 int res;
557 int res_size = sizeof res;
558 PyObject *mod;
Victor Stinner11517102014-07-29 23:31:34 +0200559 int is_socket;
560
561 if (!PyArg_ParseTuple(args, "O:set_wakeup_fd", &fdobj))
562 return NULL;
563
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100564 sockfd = PyLong_AsSocket_t(fdobj);
565 if (sockfd == (SOCKET_T)(-1) && PyErr_Occurred())
Victor Stinner11517102014-07-29 23:31:34 +0200566 return NULL;
567#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000568 int fd, old_fd;
Victor Stinner11517102014-07-29 23:31:34 +0200569
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000570 if (!PyArg_ParseTuple(args, "i:set_wakeup_fd", &fd))
571 return NULL;
Victor Stinner11517102014-07-29 23:31:34 +0200572#endif
573
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000574#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000575 if (PyThread_get_thread_ident() != main_thread) {
576 PyErr_SetString(PyExc_ValueError,
577 "set_wakeup_fd only works in main thread");
578 return NULL;
579 }
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000580#endif
Victor Stinner0bffc942014-07-21 16:28:54 +0200581
Victor Stinner11517102014-07-29 23:31:34 +0200582#ifdef MS_WINDOWS
583 is_socket = 0;
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100584 if (sockfd != INVALID_FD) {
Victor Stinner11517102014-07-29 23:31:34 +0200585 /* Import the _socket module to call WSAStartup() */
586 mod = PyImport_ImportModuleNoBlock("_socket");
587 if (mod == NULL)
588 return NULL;
589 Py_DECREF(mod);
590
591 /* test the socket */
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100592 if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR,
Victor Stinner11517102014-07-29 23:31:34 +0200593 (char *)&res, &res_size) != 0) {
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100594 int fd, err;
595
596 err = WSAGetLastError();
Victor Stinner11517102014-07-29 23:31:34 +0200597 if (err != WSAENOTSOCK) {
598 PyErr_SetExcFromWindowsErr(PyExc_OSError, err);
599 return NULL;
600 }
601
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100602 fd = (int)sockfd;
Steve Dower940f33a2016-09-08 11:21:54 -0700603 if ((SOCKET_T)fd != sockfd) {
Victor Stinner11517102014-07-29 23:31:34 +0200604 PyErr_SetString(PyExc_ValueError, "invalid fd");
605 return NULL;
606 }
607
Victor Stinnere134a7f2015-03-30 10:09:31 +0200608 if (_Py_fstat(fd, &status) != 0)
Victor Stinner11517102014-07-29 23:31:34 +0200609 return NULL;
Victor Stinner38227602014-08-27 12:59:44 +0200610
611 /* on Windows, a file cannot be set to non-blocking mode */
Victor Stinner11517102014-07-29 23:31:34 +0200612 }
Victor Stinner38227602014-08-27 12:59:44 +0200613 else {
Victor Stinner11517102014-07-29 23:31:34 +0200614 is_socket = 1;
Victor Stinner38227602014-08-27 12:59:44 +0200615
616 /* Windows does not provide a function to test if a socket
617 is in non-blocking mode */
618 }
Victor Stinner11517102014-07-29 23:31:34 +0200619 }
620
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100621 old_sockfd = wakeup.fd;
622 wakeup.fd = sockfd;
Victor Stinner11517102014-07-29 23:31:34 +0200623 wakeup.use_send = is_socket;
624
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100625 if (old_sockfd != INVALID_FD)
626 return PyLong_FromSocket_t(old_sockfd);
Victor Stinner11517102014-07-29 23:31:34 +0200627 else
628 return PyLong_FromLong(-1);
629#else
Victor Stinnerd18ccd12014-07-24 21:58:53 +0200630 if (fd != -1) {
Victor Stinner38227602014-08-27 12:59:44 +0200631 int blocking;
632
Victor Stinnere134a7f2015-03-30 10:09:31 +0200633 if (_Py_fstat(fd, &status) != 0)
Victor Stinner11517102014-07-29 23:31:34 +0200634 return NULL;
Victor Stinner38227602014-08-27 12:59:44 +0200635
636 blocking = _Py_get_blocking(fd);
637 if (blocking < 0)
638 return NULL;
639 if (blocking) {
640 PyErr_Format(PyExc_ValueError,
641 "the fd %i must be in non-blocking mode",
642 fd);
643 return NULL;
644 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000645 }
Victor Stinner0bffc942014-07-21 16:28:54 +0200646
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000647 old_fd = wakeup_fd;
648 wakeup_fd = fd;
Victor Stinner0bffc942014-07-21 16:28:54 +0200649
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000650 return PyLong_FromLong(old_fd);
Victor Stinner11517102014-07-29 23:31:34 +0200651#endif
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000652}
653
654PyDoc_STRVAR(set_wakeup_fd_doc,
655"set_wakeup_fd(fd) -> fd\n\
656\n\
Victor Stinner11517102014-07-29 23:31:34 +0200657Sets the fd to be written to (with the signal number) when a signal\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000658comes in. A library can use this to wakeup select or poll.\n\
Victor Stinner11517102014-07-29 23:31:34 +0200659The previous fd or -1 is returned.\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000660\n\
661The fd must be non-blocking.");
662
663/* C API for the same, without all the error checking */
664int
665PySignal_SetWakeupFd(int fd)
666{
Victor Stinner11517102014-07-29 23:31:34 +0200667 int old_fd;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000668 if (fd < 0)
669 fd = -1;
Victor Stinner11517102014-07-29 23:31:34 +0200670
671#ifdef MS_WINDOWS
672 old_fd = Py_SAFE_DOWNCAST(wakeup.fd, SOCKET_T, int);
673 wakeup.fd = fd;
674#else
675 old_fd = wakeup_fd;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000676 wakeup_fd = fd;
Victor Stinner11517102014-07-29 23:31:34 +0200677#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000678 return old_fd;
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000679}
680
681
Martin v. Löwis823725e2008-03-24 13:39:54 +0000682#ifdef HAVE_SETITIMER
Tal Einatc7027b72015-05-16 14:14:49 +0300683
684/*[clinic input]
685signal.setitimer
686
687 which: int
688 seconds: double
689 interval: double = 0.0
690 /
691
692Sets given itimer (one of ITIMER_REAL, ITIMER_VIRTUAL or ITIMER_PROF).
693
694The timer will fire after value seconds and after that every interval seconds.
695The itimer can be cleared by setting seconds to zero.
696
697Returns old values as a tuple: (delay, interval).
698[clinic start generated code]*/
699
Martin v. Löwis823725e2008-03-24 13:39:54 +0000700static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300701signal_setitimer_impl(PyObject *module, int which, double seconds,
Tal Einatc7027b72015-05-16 14:14:49 +0300702 double interval)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300703/*[clinic end generated code: output=6f51da0fe0787f2c input=0d27d417cfcbd51a]*/
Martin v. Löwis823725e2008-03-24 13:39:54 +0000704{
Martin v. Löwis823725e2008-03-24 13:39:54 +0000705 struct itimerval new, old;
706
Tal Einatc7027b72015-05-16 14:14:49 +0300707 timeval_from_double(seconds, &new.it_value);
Martin v. Löwis823725e2008-03-24 13:39:54 +0000708 timeval_from_double(interval, &new.it_interval);
709 /* Let OS check "which" value */
710 if (setitimer(which, &new, &old) != 0) {
Tal Einatc7027b72015-05-16 14:14:49 +0300711 PyErr_SetFromErrno(ItimerError);
712 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000713 }
714
715 return itimer_retval(&old);
716}
717
Martin v. Löwis823725e2008-03-24 13:39:54 +0000718#endif
719
720
721#ifdef HAVE_GETITIMER
Tal Einatc7027b72015-05-16 14:14:49 +0300722
723/*[clinic input]
724signal.getitimer
725
726 which: int
727 /
728
729Returns current value of given itimer.
730[clinic start generated code]*/
731
Martin v. Löwis823725e2008-03-24 13:39:54 +0000732static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300733signal_getitimer_impl(PyObject *module, int which)
734/*[clinic end generated code: output=9e053175d517db40 input=f7d21d38f3490627]*/
Martin v. Löwis823725e2008-03-24 13:39:54 +0000735{
Martin v. Löwis823725e2008-03-24 13:39:54 +0000736 struct itimerval old;
737
Martin v. Löwis823725e2008-03-24 13:39:54 +0000738 if (getitimer(which, &old) != 0) {
Tal Einatc7027b72015-05-16 14:14:49 +0300739 PyErr_SetFromErrno(ItimerError);
740 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000741 }
742
743 return itimer_retval(&old);
744}
745
Martin v. Löwis823725e2008-03-24 13:39:54 +0000746#endif
747
Ross Lagerwallbc808222011-06-25 12:13:40 +0200748#if defined(PYPTHREAD_SIGMASK) || defined(HAVE_SIGWAIT) || \
749 defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
Victor Stinnera9293352011-04-30 15:21:58 +0200750/* Convert an iterable to a sigset.
751 Return 0 on success, return -1 and raise an exception on error. */
752
753static int
754iterable_to_sigset(PyObject *iterable, sigset_t *mask)
755{
756 int result = -1;
757 PyObject *iterator, *item;
758 long signum;
759 int err;
760
761 sigemptyset(mask);
762
763 iterator = PyObject_GetIter(iterable);
764 if (iterator == NULL)
765 goto error;
766
767 while (1)
768 {
769 item = PyIter_Next(iterator);
770 if (item == NULL) {
771 if (PyErr_Occurred())
772 goto error;
773 else
774 break;
775 }
776
777 signum = PyLong_AsLong(item);
778 Py_DECREF(item);
779 if (signum == -1 && PyErr_Occurred())
780 goto error;
781 if (0 < signum && signum < NSIG)
782 err = sigaddset(mask, (int)signum);
783 else
784 err = 1;
785 if (err) {
786 PyErr_Format(PyExc_ValueError,
787 "signal number %ld out of range", signum);
788 goto error;
789 }
790 }
791 result = 0;
792
793error:
794 Py_XDECREF(iterator);
795 return result;
796}
Victor Stinnerb3e72192011-05-08 01:46:11 +0200797#endif
Victor Stinnera9293352011-04-30 15:21:58 +0200798
Victor Stinnerb3e72192011-05-08 01:46:11 +0200799#if defined(PYPTHREAD_SIGMASK) || defined(HAVE_SIGPENDING)
Victor Stinner35b300c2011-05-04 13:20:35 +0200800static PyObject*
801sigset_to_set(sigset_t mask)
802{
803 PyObject *signum, *result;
804 int sig;
805
806 result = PySet_New(0);
807 if (result == NULL)
808 return NULL;
809
810 for (sig = 1; sig < NSIG; sig++) {
811 if (sigismember(&mask, sig) != 1)
812 continue;
813
814 /* Handle the case where it is a member by adding the signal to
815 the result list. Ignore the other cases because they mean the
816 signal isn't a member of the mask or the signal was invalid,
817 and an invalid signal must have been our fault in constructing
818 the loop boundaries. */
819 signum = PyLong_FromLong(sig);
820 if (signum == NULL) {
821 Py_DECREF(result);
822 return NULL;
823 }
824 if (PySet_Add(result, signum) == -1) {
825 Py_DECREF(signum);
826 Py_DECREF(result);
827 return NULL;
828 }
829 Py_DECREF(signum);
830 }
831 return result;
832}
Victor Stinnerb3e72192011-05-08 01:46:11 +0200833#endif
Victor Stinner35b300c2011-05-04 13:20:35 +0200834
Victor Stinnerb3e72192011-05-08 01:46:11 +0200835#ifdef PYPTHREAD_SIGMASK
Tal Einatc7027b72015-05-16 14:14:49 +0300836
837/*[clinic input]
838signal.pthread_sigmask
839
840 how: int
841 mask: object
842 /
843
844Fetch and/or change the signal mask of the calling thread.
845[clinic start generated code]*/
846
Victor Stinnera9293352011-04-30 15:21:58 +0200847static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300848signal_pthread_sigmask_impl(PyObject *module, int how, PyObject *mask)
849/*[clinic end generated code: output=ff640fe092bc9181 input=f3b7d7a61b7b8283]*/
Victor Stinnera9293352011-04-30 15:21:58 +0200850{
Tal Einatc7027b72015-05-16 14:14:49 +0300851 sigset_t newmask, previous;
Victor Stinnera9293352011-04-30 15:21:58 +0200852 int err;
853
Tal Einatc7027b72015-05-16 14:14:49 +0300854 if (iterable_to_sigset(mask, &newmask))
Victor Stinnera9293352011-04-30 15:21:58 +0200855 return NULL;
856
Tal Einatc7027b72015-05-16 14:14:49 +0300857 err = pthread_sigmask(how, &newmask, &previous);
Victor Stinnera9293352011-04-30 15:21:58 +0200858 if (err != 0) {
859 errno = err;
Victor Stinnerb3e72192011-05-08 01:46:11 +0200860 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnera9293352011-04-30 15:21:58 +0200861 return NULL;
862 }
863
Victor Stinnerd0e516d2011-05-03 14:57:12 +0200864 /* if signals was unblocked, signal handlers have been called */
865 if (PyErr_CheckSignals())
866 return NULL;
867
Victor Stinner35b300c2011-05-04 13:20:35 +0200868 return sigset_to_set(previous);
Victor Stinnera9293352011-04-30 15:21:58 +0200869}
870
Victor Stinnera9293352011-04-30 15:21:58 +0200871#endif /* #ifdef PYPTHREAD_SIGMASK */
872
Martin v. Löwis823725e2008-03-24 13:39:54 +0000873
Victor Stinnerb3e72192011-05-08 01:46:11 +0200874#ifdef HAVE_SIGPENDING
Tal Einatc7027b72015-05-16 14:14:49 +0300875
876/*[clinic input]
877signal.sigpending
878
879Examine pending signals.
880
881Returns a set of signal numbers that are pending for delivery to
882the calling thread.
883[clinic start generated code]*/
884
Victor Stinnerb3e72192011-05-08 01:46:11 +0200885static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300886signal_sigpending_impl(PyObject *module)
887/*[clinic end generated code: output=53375ffe89325022 input=e0036c016f874e29]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +0200888{
889 int err;
890 sigset_t mask;
891 err = sigpending(&mask);
892 if (err)
893 return PyErr_SetFromErrno(PyExc_OSError);
894 return sigset_to_set(mask);
895}
896
Victor Stinnerb3e72192011-05-08 01:46:11 +0200897#endif /* #ifdef HAVE_SIGPENDING */
898
899
900#ifdef HAVE_SIGWAIT
Tal Einatc7027b72015-05-16 14:14:49 +0300901
902/*[clinic input]
903signal.sigwait
904
905 sigset: object
906 /
907
908Wait for a signal.
909
910Suspend execution of the calling thread until the delivery of one of the
911signals specified in the signal set sigset. The function accepts the signal
912and returns the signal number.
913[clinic start generated code]*/
914
Victor Stinnerb3e72192011-05-08 01:46:11 +0200915static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300916signal_sigwait(PyObject *module, PyObject *sigset)
917/*[clinic end generated code: output=557173647424f6e4 input=11af2d82d83c2e94]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +0200918{
Victor Stinnerb3e72192011-05-08 01:46:11 +0200919 sigset_t set;
920 int err, signum;
921
Tal Einatc7027b72015-05-16 14:14:49 +0300922 if (iterable_to_sigset(sigset, &set))
Victor Stinnerb3e72192011-05-08 01:46:11 +0200923 return NULL;
924
Victor Stinner10c30d62011-06-10 01:39:53 +0200925 Py_BEGIN_ALLOW_THREADS
Victor Stinnerb3e72192011-05-08 01:46:11 +0200926 err = sigwait(&set, &signum);
Victor Stinner10c30d62011-06-10 01:39:53 +0200927 Py_END_ALLOW_THREADS
Victor Stinnerb3e72192011-05-08 01:46:11 +0200928 if (err) {
929 errno = err;
930 return PyErr_SetFromErrno(PyExc_OSError);
931 }
932
933 return PyLong_FromLong(signum);
934}
935
Tal Einatc7027b72015-05-16 14:14:49 +0300936#endif /* #ifdef HAVE_SIGWAIT */
937
Victor Stinnerb3e72192011-05-08 01:46:11 +0200938
Ross Lagerwallbc808222011-06-25 12:13:40 +0200939#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
940static int initialized;
941static PyStructSequence_Field struct_siginfo_fields[] = {
942 {"si_signo", "signal number"},
943 {"si_code", "signal code"},
944 {"si_errno", "errno associated with this signal"},
945 {"si_pid", "sending process ID"},
946 {"si_uid", "real user ID of sending process"},
947 {"si_status", "exit value or signal"},
948 {"si_band", "band event for SIGPOLL"},
949 {0}
950};
951
952PyDoc_STRVAR(struct_siginfo__doc__,
953"struct_siginfo: Result from sigwaitinfo or sigtimedwait.\n\n\
954This object may be accessed either as a tuple of\n\
955(si_signo, si_code, si_errno, si_pid, si_uid, si_status, si_band),\n\
956or via the attributes si_signo, si_code, and so on.");
957
958static PyStructSequence_Desc struct_siginfo_desc = {
959 "signal.struct_siginfo", /* name */
960 struct_siginfo__doc__, /* doc */
961 struct_siginfo_fields, /* fields */
962 7 /* n_in_sequence */
963};
964
965static PyTypeObject SiginfoType;
966
967static PyObject *
968fill_siginfo(siginfo_t *si)
969{
970 PyObject *result = PyStructSequence_New(&SiginfoType);
971 if (!result)
972 return NULL;
973
974 PyStructSequence_SET_ITEM(result, 0, PyLong_FromLong((long)(si->si_signo)));
975 PyStructSequence_SET_ITEM(result, 1, PyLong_FromLong((long)(si->si_code)));
976 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si->si_errno)));
977 PyStructSequence_SET_ITEM(result, 3, PyLong_FromPid(si->si_pid));
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200978 PyStructSequence_SET_ITEM(result, 4, _PyLong_FromUid(si->si_uid));
Ross Lagerwallbc808222011-06-25 12:13:40 +0200979 PyStructSequence_SET_ITEM(result, 5,
980 PyLong_FromLong((long)(si->si_status)));
Zachary Ware6a6967e2016-10-01 00:47:27 -0500981#ifdef HAVE_SIGINFO_T_SI_BAND
Ross Lagerwallbc808222011-06-25 12:13:40 +0200982 PyStructSequence_SET_ITEM(result, 6, PyLong_FromLong(si->si_band));
Zachary Ware6a6967e2016-10-01 00:47:27 -0500983#else
984 PyStructSequence_SET_ITEM(result, 6, PyLong_FromLong(0L));
985#endif
Ross Lagerwallbc808222011-06-25 12:13:40 +0200986 if (PyErr_Occurred()) {
987 Py_DECREF(result);
988 return NULL;
989 }
990
991 return result;
992}
993#endif
994
995#ifdef HAVE_SIGWAITINFO
Tal Einatc7027b72015-05-16 14:14:49 +0300996
997/*[clinic input]
998signal.sigwaitinfo
999
1000 sigset: object
1001 /
1002
1003Wait synchronously until one of the signals in *sigset* is delivered.
1004
1005Returns a struct_siginfo containing information about the signal.
1006[clinic start generated code]*/
1007
Ross Lagerwallbc808222011-06-25 12:13:40 +02001008static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001009signal_sigwaitinfo(PyObject *module, PyObject *sigset)
1010/*[clinic end generated code: output=c40f27b269cd2309 input=f3779a74a991e171]*/
Ross Lagerwallbc808222011-06-25 12:13:40 +02001011{
Ross Lagerwallbc808222011-06-25 12:13:40 +02001012 sigset_t set;
1013 siginfo_t si;
1014 int err;
Victor Stinnera453cd82015-03-20 12:54:28 +01001015 int async_err = 0;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001016
Tal Einatc7027b72015-05-16 14:14:49 +03001017 if (iterable_to_sigset(sigset, &set))
Ross Lagerwallbc808222011-06-25 12:13:40 +02001018 return NULL;
1019
Victor Stinnera453cd82015-03-20 12:54:28 +01001020 do {
1021 Py_BEGIN_ALLOW_THREADS
1022 err = sigwaitinfo(&set, &si);
1023 Py_END_ALLOW_THREADS
1024 } while (err == -1
1025 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Ross Lagerwallbc808222011-06-25 12:13:40 +02001026 if (err == -1)
Victor Stinnera453cd82015-03-20 12:54:28 +01001027 return (!async_err) ? PyErr_SetFromErrno(PyExc_OSError) : NULL;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001028
1029 return fill_siginfo(&si);
1030}
1031
Ross Lagerwallbc808222011-06-25 12:13:40 +02001032#endif /* #ifdef HAVE_SIGWAITINFO */
1033
1034#ifdef HAVE_SIGTIMEDWAIT
Tal Einatc7027b72015-05-16 14:14:49 +03001035
1036/*[clinic input]
1037signal.sigtimedwait
1038
1039 sigset: object
Serhiy Storchaka6b680cd2015-05-16 15:57:56 +03001040 timeout as timeout_obj: object
Tal Einatc7027b72015-05-16 14:14:49 +03001041 /
1042
1043Like sigwaitinfo(), but with a timeout.
1044
1045The timeout is specified in seconds, with floating point numbers allowed.
1046[clinic start generated code]*/
1047
Ross Lagerwallbc808222011-06-25 12:13:40 +02001048static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001049signal_sigtimedwait_impl(PyObject *module, PyObject *sigset,
Serhiy Storchaka6b680cd2015-05-16 15:57:56 +03001050 PyObject *timeout_obj)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001051/*[clinic end generated code: output=f7eff31e679f4312 input=53fd4ea3e3724eb8]*/
Ross Lagerwallbc808222011-06-25 12:13:40 +02001052{
Victor Stinnera453cd82015-03-20 12:54:28 +01001053 struct timespec ts;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001054 sigset_t set;
1055 siginfo_t si;
Victor Stinnera453cd82015-03-20 12:54:28 +01001056 int res;
Victor Stinner34dc0f42015-03-27 18:19:03 +01001057 _PyTime_t timeout, deadline, monotonic;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001058
Victor Stinner869e1772015-03-30 03:49:14 +02001059 if (_PyTime_FromSecondsObject(&timeout,
1060 timeout_obj, _PyTime_ROUND_CEILING) < 0)
Ross Lagerwallbc808222011-06-25 12:13:40 +02001061 return NULL;
1062
Victor Stinnera453cd82015-03-20 12:54:28 +01001063 if (timeout < 0) {
Ross Lagerwallbc808222011-06-25 12:13:40 +02001064 PyErr_SetString(PyExc_ValueError, "timeout must be non-negative");
1065 return NULL;
1066 }
1067
Tal Einatc7027b72015-05-16 14:14:49 +03001068 if (iterable_to_sigset(sigset, &set))
Ross Lagerwallbc808222011-06-25 12:13:40 +02001069 return NULL;
1070
Victor Stinner34dc0f42015-03-27 18:19:03 +01001071 deadline = _PyTime_GetMonotonicClock() + timeout;
Victor Stinnera453cd82015-03-20 12:54:28 +01001072
1073 do {
Victor Stinner34dc0f42015-03-27 18:19:03 +01001074 if (_PyTime_AsTimespec(timeout, &ts) < 0)
1075 return NULL;
Victor Stinnera453cd82015-03-20 12:54:28 +01001076
1077 Py_BEGIN_ALLOW_THREADS
1078 res = sigtimedwait(&set, &si, &ts);
1079 Py_END_ALLOW_THREADS
1080
1081 if (res != -1)
1082 break;
1083
1084 if (errno != EINTR) {
1085 if (errno == EAGAIN)
1086 Py_RETURN_NONE;
1087 else
1088 return PyErr_SetFromErrno(PyExc_OSError);
1089 }
1090
1091 /* sigtimedwait() was interrupted by a signal (EINTR) */
1092 if (PyErr_CheckSignals())
1093 return NULL;
1094
Victor Stinner34dc0f42015-03-27 18:19:03 +01001095 monotonic = _PyTime_GetMonotonicClock();
1096 timeout = deadline - monotonic;
Victor Stinner6aa446c2015-03-30 21:33:51 +02001097 if (timeout < 0)
Victor Stinnera453cd82015-03-20 12:54:28 +01001098 break;
1099 } while (1);
Ross Lagerwallbc808222011-06-25 12:13:40 +02001100
1101 return fill_siginfo(&si);
1102}
1103
Ross Lagerwallbc808222011-06-25 12:13:40 +02001104#endif /* #ifdef HAVE_SIGTIMEDWAIT */
1105
Victor Stinnerb3e72192011-05-08 01:46:11 +02001106
1107#if defined(HAVE_PTHREAD_KILL) && defined(WITH_THREAD)
Tal Einatc7027b72015-05-16 14:14:49 +03001108
1109/*[clinic input]
1110signal.pthread_kill
1111
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +02001112 thread_id: unsigned_long(bitwise=True)
Tal Einatc7027b72015-05-16 14:14:49 +03001113 signalnum: int
1114 /
1115
1116Send a signal to a thread.
1117[clinic start generated code]*/
1118
Victor Stinnerb3e72192011-05-08 01:46:11 +02001119static PyObject *
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +02001120signal_pthread_kill_impl(PyObject *module, unsigned long thread_id,
1121 int signalnum)
1122/*[clinic end generated code: output=7629919b791bc27f input=1d901f2c7bb544ff]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +02001123{
Victor Stinnerb3e72192011-05-08 01:46:11 +02001124 int err;
1125
Tal Einatc7027b72015-05-16 14:14:49 +03001126 err = pthread_kill((pthread_t)thread_id, signalnum);
Victor Stinnerb3e72192011-05-08 01:46:11 +02001127 if (err != 0) {
1128 errno = err;
1129 PyErr_SetFromErrno(PyExc_OSError);
1130 return NULL;
1131 }
1132
1133 /* the signal may have been send to the current thread */
1134 if (PyErr_CheckSignals())
1135 return NULL;
1136
1137 Py_RETURN_NONE;
1138}
1139
Victor Stinnerb3e72192011-05-08 01:46:11 +02001140#endif /* #if defined(HAVE_PTHREAD_KILL) && defined(WITH_THREAD) */
1141
1142
1143
Tal Einatc7027b72015-05-16 14:14:49 +03001144/* List of functions defined in the module -- some of the methoddefs are
1145 defined to nothing if the corresponding C function is not available. */
Barry Warsaw92971171997-01-03 00:14:25 +00001146static PyMethodDef signal_methods[] = {
Tal Einatc7027b72015-05-16 14:14:49 +03001147 {"default_int_handler", signal_default_int_handler, METH_VARARGS, default_int_handler_doc},
1148 SIGNAL_ALARM_METHODDEF
1149 SIGNAL_SETITIMER_METHODDEF
1150 SIGNAL_GETITIMER_METHODDEF
1151 SIGNAL_SIGNAL_METHODDEF
1152 SIGNAL_GETSIGNAL_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001153 {"set_wakeup_fd", signal_set_wakeup_fd, METH_VARARGS, set_wakeup_fd_doc},
Tal Einatc7027b72015-05-16 14:14:49 +03001154 SIGNAL_SIGINTERRUPT_METHODDEF
1155 SIGNAL_PAUSE_METHODDEF
1156 SIGNAL_PTHREAD_KILL_METHODDEF
1157 SIGNAL_PTHREAD_SIGMASK_METHODDEF
1158 SIGNAL_SIGPENDING_METHODDEF
1159 SIGNAL_SIGWAIT_METHODDEF
1160 SIGNAL_SIGWAITINFO_METHODDEF
1161 SIGNAL_SIGTIMEDWAIT_METHODDEF
1162 {NULL, NULL} /* sentinel */
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001163};
1164
Barry Warsaw92971171997-01-03 00:14:25 +00001165
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001166PyDoc_STRVAR(module_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001167"This module provides mechanisms to use signal handlers in Python.\n\
1168\n\
1169Functions:\n\
1170\n\
1171alarm() -- cause SIGALRM after a specified time [Unix only]\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001172setitimer() -- cause a signal (described below) after a specified\n\
1173 float time and the timer may restart then [Unix only]\n\
1174getitimer() -- get current value of timer [Unix only]\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001175signal() -- set the action for a given signal\n\
1176getsignal() -- get the signal action for a given signal\n\
1177pause() -- wait until a signal arrives [Unix only]\n\
1178default_int_handler() -- default SIGINT handler\n\
1179\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001180signal constants:\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001181SIG_DFL -- used to refer to the system default handler\n\
1182SIG_IGN -- used to ignore the signal\n\
1183NSIG -- number of defined signals\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001184SIGINT, SIGTERM, etc. -- signal numbers\n\
1185\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001186itimer constants:\n\
1187ITIMER_REAL -- decrements in real time, and delivers SIGALRM upon\n\
1188 expiration\n\
1189ITIMER_VIRTUAL -- decrements only when the process is executing,\n\
1190 and delivers SIGVTALRM upon expiration\n\
1191ITIMER_PROF -- decrements both when the process is executing and\n\
1192 when the system is executing on behalf of the process.\n\
1193 Coupled with ITIMER_VIRTUAL, this timer is usually\n\
1194 used to profile the time spent by the application\n\
1195 in user and kernel space. SIGPROF is delivered upon\n\
1196 expiration.\n\
1197\n\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001198*** IMPORTANT NOTICE ***\n\
1199A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001200the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001201
Martin v. Löwis1a214512008-06-11 05:26:20 +00001202static struct PyModuleDef signalmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001203 PyModuleDef_HEAD_INIT,
Brett Cannon815a6f32014-04-04 10:20:28 -04001204 "_signal",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001205 module_doc,
1206 -1,
1207 signal_methods,
1208 NULL,
1209 NULL,
1210 NULL,
1211 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001212};
1213
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001214PyMODINIT_FUNC
Giampaolo Rodola'e09fb712014-04-04 15:34:17 +02001215PyInit__signal(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001216{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001217 PyObject *m, *d, *x;
1218 int i;
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001219
1220#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001221 main_thread = PyThread_get_thread_ident();
1222 main_pid = getpid();
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001223#endif
1224
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001225 /* Create the module and add the functions */
1226 m = PyModule_Create(&signalmodule);
1227 if (m == NULL)
1228 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001229
Ross Lagerwallbc808222011-06-25 12:13:40 +02001230#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
Victor Stinner1c8f0592013-07-22 22:24:54 +02001231 if (!initialized) {
1232 if (PyStructSequence_InitType2(&SiginfoType, &struct_siginfo_desc) < 0)
1233 return NULL;
1234 }
Ross Lagerwallbc808222011-06-25 12:13:40 +02001235 Py_INCREF((PyObject*) &SiginfoType);
1236 PyModule_AddObject(m, "struct_siginfo", (PyObject*) &SiginfoType);
1237 initialized = 1;
1238#endif
1239
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001240 /* Add some symbolic constants to the module */
1241 d = PyModule_GetDict(m);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001242
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001243 x = DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL);
1244 if (!x || PyDict_SetItemString(d, "SIG_DFL", x) < 0)
1245 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001246
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001247 x = IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN);
1248 if (!x || PyDict_SetItemString(d, "SIG_IGN", x) < 0)
1249 goto finally;
Barry Warsaw92971171997-01-03 00:14:25 +00001250
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001251 x = PyLong_FromLong((long)NSIG);
1252 if (!x || PyDict_SetItemString(d, "NSIG", x) < 0)
1253 goto finally;
1254 Py_DECREF(x);
Barry Warsaw92971171997-01-03 00:14:25 +00001255
Victor Stinnera9293352011-04-30 15:21:58 +02001256#ifdef SIG_BLOCK
Victor Stinner72c53b52011-05-02 16:15:43 +02001257 if (PyModule_AddIntMacro(m, SIG_BLOCK))
1258 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001259#endif
Victor Stinnera9293352011-04-30 15:21:58 +02001260#ifdef SIG_UNBLOCK
Victor Stinner72c53b52011-05-02 16:15:43 +02001261 if (PyModule_AddIntMacro(m, SIG_UNBLOCK))
1262 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001263#endif
Victor Stinnera9293352011-04-30 15:21:58 +02001264#ifdef SIG_SETMASK
Victor Stinner72c53b52011-05-02 16:15:43 +02001265 if (PyModule_AddIntMacro(m, SIG_SETMASK))
1266 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001267#endif
1268
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001269 x = IntHandler = PyDict_GetItemString(d, "default_int_handler");
1270 if (!x)
1271 goto finally;
1272 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +00001273
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001274 _Py_atomic_store_relaxed(&Handlers[0].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001275 for (i = 1; i < NSIG; i++) {
1276 void (*t)(int);
1277 t = PyOS_getsig(i);
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001278 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001279 if (t == SIG_DFL)
1280 Handlers[i].func = DefaultHandler;
1281 else if (t == SIG_IGN)
1282 Handlers[i].func = IgnoreHandler;
1283 else
1284 Handlers[i].func = Py_None; /* None of our business */
1285 Py_INCREF(Handlers[i].func);
1286 }
1287 if (Handlers[SIGINT].func == DefaultHandler) {
1288 /* Install default int handler */
1289 Py_INCREF(IntHandler);
Serhiy Storchaka57a01d32016-04-10 18:05:40 +03001290 Py_SETREF(Handlers[SIGINT].func, IntHandler);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001291 old_siginthandler = PyOS_setsig(SIGINT, signal_handler);
1292 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001293
1294#ifdef SIGHUP
Christian Heimes6782b142016-09-09 00:11:45 +02001295 if (PyModule_AddIntMacro(m, SIGHUP))
1296 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001297#endif
1298#ifdef SIGINT
Christian Heimes6782b142016-09-09 00:11:45 +02001299 if (PyModule_AddIntMacro(m, SIGINT))
1300 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001301#endif
Tim Peters1ce3cf72001-10-01 17:58:40 +00001302#ifdef SIGBREAK
Christian Heimes6782b142016-09-09 00:11:45 +02001303 if (PyModule_AddIntMacro(m, SIGBREAK))
1304 goto finally;
Tim Peters1ce3cf72001-10-01 17:58:40 +00001305#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001306#ifdef SIGQUIT
Christian Heimes6782b142016-09-09 00:11:45 +02001307 if (PyModule_AddIntMacro(m, SIGQUIT))
1308 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001309#endif
1310#ifdef SIGILL
Christian Heimes6782b142016-09-09 00:11:45 +02001311 if (PyModule_AddIntMacro(m, SIGILL))
1312 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001313#endif
1314#ifdef SIGTRAP
Christian Heimes6782b142016-09-09 00:11:45 +02001315 if (PyModule_AddIntMacro(m, SIGTRAP))
1316 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001317#endif
1318#ifdef SIGIOT
Christian Heimes6782b142016-09-09 00:11:45 +02001319 if (PyModule_AddIntMacro(m, SIGIOT))
1320 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001321#endif
1322#ifdef SIGABRT
Christian Heimes6782b142016-09-09 00:11:45 +02001323 if (PyModule_AddIntMacro(m, SIGABRT))
1324 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001325#endif
1326#ifdef SIGEMT
Christian Heimes6782b142016-09-09 00:11:45 +02001327 if (PyModule_AddIntMacro(m, SIGEMT))
1328 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001329#endif
1330#ifdef SIGFPE
Christian Heimes6782b142016-09-09 00:11:45 +02001331 if (PyModule_AddIntMacro(m, SIGFPE))
1332 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001333#endif
1334#ifdef SIGKILL
Christian Heimes6782b142016-09-09 00:11:45 +02001335 if (PyModule_AddIntMacro(m, SIGKILL))
1336 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001337#endif
1338#ifdef SIGBUS
Christian Heimes6782b142016-09-09 00:11:45 +02001339 if (PyModule_AddIntMacro(m, SIGBUS))
1340 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001341#endif
1342#ifdef SIGSEGV
Christian Heimes6782b142016-09-09 00:11:45 +02001343 if (PyModule_AddIntMacro(m, SIGSEGV))
1344 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001345#endif
1346#ifdef SIGSYS
Christian Heimes6782b142016-09-09 00:11:45 +02001347 if (PyModule_AddIntMacro(m, SIGSYS))
1348 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001349#endif
1350#ifdef SIGPIPE
Christian Heimes6782b142016-09-09 00:11:45 +02001351 if (PyModule_AddIntMacro(m, SIGPIPE))
1352 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001353#endif
1354#ifdef SIGALRM
Christian Heimes6782b142016-09-09 00:11:45 +02001355 if (PyModule_AddIntMacro(m, SIGALRM))
1356 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001357#endif
1358#ifdef SIGTERM
Christian Heimes6782b142016-09-09 00:11:45 +02001359 if (PyModule_AddIntMacro(m, SIGTERM))
1360 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001361#endif
1362#ifdef SIGUSR1
Christian Heimes6782b142016-09-09 00:11:45 +02001363 if (PyModule_AddIntMacro(m, SIGUSR1))
1364 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001365#endif
1366#ifdef SIGUSR2
Christian Heimes6782b142016-09-09 00:11:45 +02001367 if (PyModule_AddIntMacro(m, SIGUSR2))
1368 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001369#endif
1370#ifdef SIGCLD
Christian Heimes6782b142016-09-09 00:11:45 +02001371 if (PyModule_AddIntMacro(m, SIGCLD))
1372 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001373#endif
1374#ifdef SIGCHLD
Christian Heimes6782b142016-09-09 00:11:45 +02001375 if (PyModule_AddIntMacro(m, SIGCHLD))
1376 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001377#endif
1378#ifdef SIGPWR
Christian Heimes6782b142016-09-09 00:11:45 +02001379 if (PyModule_AddIntMacro(m, SIGPWR))
1380 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001381#endif
1382#ifdef SIGIO
Christian Heimes6782b142016-09-09 00:11:45 +02001383 if (PyModule_AddIntMacro(m, SIGIO))
1384 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001385#endif
1386#ifdef SIGURG
Christian Heimes6782b142016-09-09 00:11:45 +02001387 if (PyModule_AddIntMacro(m, SIGURG))
1388 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001389#endif
1390#ifdef SIGWINCH
Christian Heimes6782b142016-09-09 00:11:45 +02001391 if (PyModule_AddIntMacro(m, SIGWINCH))
1392 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001393#endif
1394#ifdef SIGPOLL
Christian Heimes6782b142016-09-09 00:11:45 +02001395 if (PyModule_AddIntMacro(m, SIGPOLL))
1396 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001397#endif
1398#ifdef SIGSTOP
Christian Heimes6782b142016-09-09 00:11:45 +02001399 if (PyModule_AddIntMacro(m, SIGSTOP))
1400 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001401#endif
1402#ifdef SIGTSTP
Christian Heimes6782b142016-09-09 00:11:45 +02001403 if (PyModule_AddIntMacro(m, SIGTSTP))
1404 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001405#endif
1406#ifdef SIGCONT
Christian Heimes6782b142016-09-09 00:11:45 +02001407 if (PyModule_AddIntMacro(m, SIGCONT))
1408 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001409#endif
1410#ifdef SIGTTIN
Christian Heimes6782b142016-09-09 00:11:45 +02001411 if (PyModule_AddIntMacro(m, SIGTTIN))
1412 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001413#endif
1414#ifdef SIGTTOU
Christian Heimes6782b142016-09-09 00:11:45 +02001415 if (PyModule_AddIntMacro(m, SIGTTOU))
1416 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001417#endif
1418#ifdef SIGVTALRM
Christian Heimes6782b142016-09-09 00:11:45 +02001419 if (PyModule_AddIntMacro(m, SIGVTALRM))
1420 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001421#endif
1422#ifdef SIGPROF
Christian Heimes6782b142016-09-09 00:11:45 +02001423 if (PyModule_AddIntMacro(m, SIGPROF))
1424 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001425#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001426#ifdef SIGXCPU
Christian Heimes6782b142016-09-09 00:11:45 +02001427 if (PyModule_AddIntMacro(m, SIGXCPU))
1428 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001429#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001430#ifdef SIGXFSZ
Christian Heimes6782b142016-09-09 00:11:45 +02001431 if (PyModule_AddIntMacro(m, SIGXFSZ))
1432 goto finally;
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001433#endif
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001434#ifdef SIGRTMIN
Christian Heimes6782b142016-09-09 00:11:45 +02001435 if (PyModule_AddIntMacro(m, SIGRTMIN))
1436 goto finally;
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001437#endif
1438#ifdef SIGRTMAX
Christian Heimes6782b142016-09-09 00:11:45 +02001439 if (PyModule_AddIntMacro(m, SIGRTMAX))
1440 goto finally;
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001441#endif
Martin v. Löwis175af252002-01-12 11:43:25 +00001442#ifdef SIGINFO
Christian Heimes6782b142016-09-09 00:11:45 +02001443 if (PyModule_AddIntMacro(m, SIGINFO))
1444 goto finally;
Martin v. Löwis175af252002-01-12 11:43:25 +00001445#endif
Martin v. Löwis823725e2008-03-24 13:39:54 +00001446
1447#ifdef ITIMER_REAL
Christian Heimes6782b142016-09-09 00:11:45 +02001448 if (PyModule_AddIntMacro(m, ITIMER_REAL))
1449 goto finally;
Martin v. Löwis823725e2008-03-24 13:39:54 +00001450#endif
1451#ifdef ITIMER_VIRTUAL
Christian Heimes6782b142016-09-09 00:11:45 +02001452 if (PyModule_AddIntMacro(m, ITIMER_VIRTUAL))
1453 goto finally;
Martin v. Löwis823725e2008-03-24 13:39:54 +00001454#endif
1455#ifdef ITIMER_PROF
Christian Heimes6782b142016-09-09 00:11:45 +02001456 if (PyModule_AddIntMacro(m, ITIMER_PROF))
1457 goto finally;
Martin v. Löwis823725e2008-03-24 13:39:54 +00001458#endif
1459
1460#if defined (HAVE_SETITIMER) || defined (HAVE_GETITIMER)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001461 ItimerError = PyErr_NewException("signal.ItimerError",
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +03001462 PyExc_OSError, NULL);
Neal Norwitz2f99b242008-08-24 05:48:10 +00001463 if (ItimerError != NULL)
Martin Panter6d57fe12016-09-17 03:26:16 +00001464 PyDict_SetItemString(d, "ItimerError", ItimerError);
Martin v. Löwis823725e2008-03-24 13:39:54 +00001465#endif
1466
Brian Curtineb24d742010-04-12 17:16:38 +00001467#ifdef CTRL_C_EVENT
Christian Heimes6782b142016-09-09 00:11:45 +02001468 if (PyModule_AddIntMacro(m, CTRL_C_EVENT))
1469 goto finally;
Brian Curtineb24d742010-04-12 17:16:38 +00001470#endif
1471
1472#ifdef CTRL_BREAK_EVENT
Christian Heimes6782b142016-09-09 00:11:45 +02001473 if (PyModule_AddIntMacro(m, CTRL_BREAK_EVENT))
1474 goto finally;
Brian Curtineb24d742010-04-12 17:16:38 +00001475#endif
1476
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001477#ifdef MS_WINDOWS
1478 /* Create manual-reset event, initially unset */
1479 sigint_event = CreateEvent(NULL, TRUE, FALSE, FALSE);
1480#endif
1481
Martin v. Löwis1a214512008-06-11 05:26:20 +00001482 if (PyErr_Occurred()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001483 Py_DECREF(m);
1484 m = NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001485 }
Barry Warsaw92971171997-01-03 00:14:25 +00001486
Barry Warsaw92971171997-01-03 00:14:25 +00001487 finally:
Martin v. Löwis1a214512008-06-11 05:26:20 +00001488 return m;
Guido van Rossum08c16611997-08-02 03:01:42 +00001489}
1490
1491static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001492finisignal(void)
Guido van Rossum08c16611997-08-02 03:01:42 +00001493{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001494 int i;
1495 PyObject *func;
Guido van Rossum08c16611997-08-02 03:01:42 +00001496
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001497 PyOS_setsig(SIGINT, old_siginthandler);
1498 old_siginthandler = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +00001499
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001500 for (i = 1; i < NSIG; i++) {
1501 func = Handlers[i].func;
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001502 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001503 Handlers[i].func = NULL;
1504 if (i != SIGINT && func != NULL && func != Py_None &&
1505 func != DefaultHandler && func != IgnoreHandler)
1506 PyOS_setsig(i, SIG_DFL);
1507 Py_XDECREF(func);
1508 }
Guido van Rossum08c16611997-08-02 03:01:42 +00001509
Serhiy Storchaka505ff752014-02-09 13:33:53 +02001510 Py_CLEAR(IntHandler);
1511 Py_CLEAR(DefaultHandler);
1512 Py_CLEAR(IgnoreHandler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001513}
1514
Barry Warsaw92971171997-01-03 00:14:25 +00001515
Barry Warsaw92971171997-01-03 00:14:25 +00001516/* Declared in pyerrors.h */
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001517int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001518PyErr_CheckSignals(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001519{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001520 int i;
1521 PyObject *f;
Barry Warsaw92971171997-01-03 00:14:25 +00001522
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001523 if (!_Py_atomic_load(&is_tripped))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001524 return 0;
Christian Heimesb76922a2007-12-11 01:06:40 +00001525
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001526#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001527 if (PyThread_get_thread_ident() != main_thread)
1528 return 0;
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001529#endif
Christian Heimesb76922a2007-12-11 01:06:40 +00001530
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001531 /*
1532 * The is_tripped variable is meant to speed up the calls to
1533 * PyErr_CheckSignals (both directly or via pending calls) when no
1534 * signal has arrived. This variable is set to 1 when a signal arrives
1535 * and it is set to 0 here, when we know some signals arrived. This way
1536 * we can run the registered handlers with no signals blocked.
1537 *
1538 * NOTE: with this approach we can have a situation where is_tripped is
1539 * 1 but we have no more signals to handle (Handlers[i].tripped
1540 * is 0 for every signal i). This won't do us any harm (except
1541 * we're gonna spent some cycles for nothing). This happens when
1542 * we receive a signal i after we zero is_tripped and before we
1543 * check Handlers[i].tripped.
1544 */
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001545 _Py_atomic_store(&is_tripped, 0);
Christian Heimesb76922a2007-12-11 01:06:40 +00001546
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001547 if (!(f = (PyObject *)PyEval_GetFrame()))
1548 f = Py_None;
Christian Heimesb76922a2007-12-11 01:06:40 +00001549
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001550 for (i = 1; i < NSIG; i++) {
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001551 if (_Py_atomic_load_relaxed(&Handlers[i].tripped)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001552 PyObject *result = NULL;
1553 PyObject *arglist = Py_BuildValue("(iO)", i, f);
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001554 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Barry Warsaw92971171997-01-03 00:14:25 +00001555
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001556 if (arglist) {
1557 result = PyEval_CallObject(Handlers[i].func,
1558 arglist);
1559 Py_DECREF(arglist);
1560 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +02001561 if (!result) {
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001562 _Py_atomic_store(&is_tripped, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001563 return -1;
Antoine Pitrouc08177a2017-06-28 23:29:29 +02001564 }
Barry Warsaw92971171997-01-03 00:14:25 +00001565
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001566 Py_DECREF(result);
1567 }
1568 }
Christian Heimesb76922a2007-12-11 01:06:40 +00001569
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001570 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001571}
1572
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +00001573
Barry Warsaw92971171997-01-03 00:14:25 +00001574/* Replacements for intrcheck.c functionality
1575 * Declared in pyerrors.h
1576 */
1577void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001578PyErr_SetInterrupt(void)
Barry Warsaw92971171997-01-03 00:14:25 +00001579{
Victor Stinner6c9b35b2011-04-18 16:25:56 +02001580 trip_signal(SIGINT);
Barry Warsaw92971171997-01-03 00:14:25 +00001581}
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001582
1583void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001584PyOS_InitInterrupts(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001585{
Giampaolo Rodola'e09fb712014-04-04 15:34:17 +02001586 PyObject *m = PyImport_ImportModule("_signal");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001587 if (m) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001588 Py_DECREF(m);
1589 }
Guido van Rossum08c16611997-08-02 03:01:42 +00001590}
1591
1592void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001593PyOS_FiniInterrupts(void)
Guido van Rossum08c16611997-08-02 03:01:42 +00001594{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001595 finisignal();
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001596}
1597
1598int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001599PyOS_InterruptOccurred(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001600{
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001601 if (_Py_atomic_load_relaxed(&Handlers[SIGINT].tripped)) {
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001602#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001603 if (PyThread_get_thread_ident() != main_thread)
1604 return 0;
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001605#endif
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001606 _Py_atomic_store_relaxed(&Handlers[SIGINT].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001607 return 1;
1608 }
1609 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001610}
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001611
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001612static void
1613_clear_pending_signals(void)
1614{
1615 int i;
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001616 if (!_Py_atomic_load(&is_tripped))
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001617 return;
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001618 _Py_atomic_store(&is_tripped, 0);
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001619 for (i = 1; i < NSIG; ++i) {
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001620 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001621 }
1622}
1623
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001624void
Antoine Pitrou346cbd32017-05-27 17:50:54 +02001625_PySignal_AfterFork(void)
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001626{
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001627 /* Clear the signal flags after forking so that they aren't handled
1628 * in both processes if they came in just before the fork() but before
1629 * the interpreter had an opportunity to call the handlers. issue9535. */
1630 _clear_pending_signals();
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001631#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001632 main_thread = PyThread_get_thread_ident();
1633 main_pid = getpid();
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001634#endif
1635}
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001636
1637int
1638_PyOS_IsMainThread(void)
1639{
1640#ifdef WITH_THREAD
1641 return PyThread_get_thread_ident() == main_thread;
1642#else
1643 return 1;
1644#endif
1645}
1646
1647#ifdef MS_WINDOWS
1648void *_PyOS_SigintEvent(void)
1649{
1650 /* Returns a manual-reset event which gets tripped whenever
1651 SIGINT is received.
1652
1653 Python.h does not include windows.h so we do cannot use HANDLE
1654 as the return type of this function. We use void* instead. */
1655 return sigint_event;
1656}
1657#endif