blob: f29720bcaf3637e18dec23c914c00dd33cacb994 [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"
Victor Stinner27e2d1f2018-11-01 00:52:28 +01007#include "pycore_atomic.h"
Victor Stinner31368a42018-10-30 15:14:25 +01008
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02009#ifndef MS_WINDOWS
10#include "posixmodule.h"
11#endif
Victor Stinner11517102014-07-29 23:31:34 +020012#ifdef MS_WINDOWS
13#include "socketmodule.h" /* needed for SOCKET_T */
14#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +000015
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000016#ifdef MS_WINDOWS
Antoine Pitrou7faf7052013-03-31 22:48:04 +020017#include <windows.h>
Benjamin Peterson2614cda2010-03-21 22:36:19 +000018#ifdef HAVE_PROCESS_H
Guido van Rossum644a12b1997-04-09 19:24:53 +000019#include <process.h>
20#endif
Benjamin Peterson2614cda2010-03-21 22:36:19 +000021#endif
Guido van Rossum644a12b1997-04-09 19:24:53 +000022
Benjamin Peterson2614cda2010-03-21 22:36:19 +000023#ifdef HAVE_SIGNAL_H
Guido van Rossum398d9fe1994-05-11 08:59:13 +000024#include <signal.h>
Benjamin Peterson2614cda2010-03-21 22:36:19 +000025#endif
26#ifdef HAVE_SYS_STAT_H
Christian Heimes5fb7c2a2007-12-24 08:52:31 +000027#include <sys/stat.h>
Benjamin Peterson2614cda2010-03-21 22:36:19 +000028#endif
Martin v. Löwis3bf3cc02008-03-24 14:05:07 +000029#ifdef HAVE_SYS_TIME_H
Martin v. Löwis823725e2008-03-24 13:39:54 +000030#include <sys/time.h>
Martin v. Löwis3bf3cc02008-03-24 14:05:07 +000031#endif
Christian Heimes5fb7c2a2007-12-24 08:52:31 +000032
Victor Stinnera9293352011-04-30 15:21:58 +020033#if defined(HAVE_PTHREAD_SIGMASK) && !defined(HAVE_BROKEN_PTHREAD_SIGMASK)
34# define PYPTHREAD_SIGMASK
35#endif
36
37#if defined(PYPTHREAD_SIGMASK) && defined(HAVE_PTHREAD_H)
38# include <pthread.h>
39#endif
40
Guido van Rossumbb4ba121994-06-23 11:25:45 +000041#ifndef SIG_ERR
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +000042#define SIG_ERR ((PyOS_sighandler_t)(-1))
Guido van Rossumbb4ba121994-06-23 11:25:45 +000043#endif
44
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000045#ifndef NSIG
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000046# if defined(_NSIG)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000047# define NSIG _NSIG /* For BSD/SysV */
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 QNX */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000050# elif defined(SIGMAX)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000051# define NSIG (SIGMAX + 1) /* For djgpp */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000052# else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000053# define NSIG 64 /* Use a reasonable default value */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000054# endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000055#endif
56
Tal Einatc7027b72015-05-16 14:14:49 +030057#include "clinic/signalmodule.c.h"
58
59/*[clinic input]
60module signal
61[clinic start generated code]*/
62/*[clinic end generated code: output=da39a3ee5e6b4b0d input=b0301a3bde5fe9d3]*/
63
Serhiy Storchakad54cfb12018-05-08 07:48:50 +030064/*[python input]
65
66class sigset_t_converter(CConverter):
67 type = 'sigset_t'
68 converter = '_Py_Sigset_Converter'
69
70[python start generated code]*/
71/*[python end generated code: output=da39a3ee5e6b4b0d input=b5689d14466b6823]*/
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000072
Guido van Rossumbb4ba121994-06-23 11:25:45 +000073/*
74 NOTES ON THE INTERACTION BETWEEN SIGNALS AND THREADS
75
76 When threads are supported, we want the following semantics:
77
78 - only the main thread can set a signal handler
79 - any thread can get a signal handler
80 - signals are only delivered to the main thread
81
82 I.e. we don't support "synchronous signals" like SIGFPE (catching
83 this doesn't make much sense in Python anyway) nor do we support
84 signals as a means of inter-thread communication, since not all
85 thread implementations support that (at least our thread library
86 doesn't).
87
88 We still have the problem that in some implementations signals
89 generated by the keyboard (e.g. SIGINT) are delivered to all
90 threads (e.g. SGI), while in others (e.g. Solaris) such signals are
91 delivered to one random thread (an intermediate possibility would
Guido van Rossuma3c04b01995-01-12 11:29:01 +000092 be to deliver it to the main thread -- POSIX?). For now, we have
Guido van Rossumbb4ba121994-06-23 11:25:45 +000093 a working implementation that works in all three cases -- the
94 handler ignores signals if getpid() isn't the same as in the main
95 thread. XXX This is a hack.
Guido van Rossumbb4ba121994-06-23 11:25:45 +000096*/
97
Guido van Rossum295b8e51997-06-06 21:16:41 +000098#include <sys/types.h> /* For pid_t */
Guido van Rossum49b56061998-10-01 20:42:43 +000099#include "pythread.h"
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +0200100static unsigned long main_thread;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000101static pid_t main_pid;
Eric Snow64d6cc82019-02-23 15:40:43 -0700102static PyInterpreterState *main_interp;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000103
Victor Stinner2ec6b172011-05-15 10:21:59 +0200104static volatile struct {
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +0200105 _Py_atomic_int tripped;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000106 PyObject *func;
Barry Warsaw92971171997-01-03 00:14:25 +0000107} Handlers[NSIG];
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000108
Victor Stinner11517102014-07-29 23:31:34 +0200109#ifdef MS_WINDOWS
110#define INVALID_FD ((SOCKET_T)-1)
111
112static volatile struct {
113 SOCKET_T fd;
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800114 int warn_on_full_buffer;
Victor Stinner11517102014-07-29 23:31:34 +0200115 int use_send;
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800116} wakeup = {.fd = INVALID_FD, .warn_on_full_buffer = 1, .use_send = 0};
Victor Stinner11517102014-07-29 23:31:34 +0200117#else
118#define INVALID_FD (-1)
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800119static volatile struct {
120 sig_atomic_t fd;
121 int warn_on_full_buffer;
122} wakeup = {.fd = INVALID_FD, .warn_on_full_buffer = 1};
Victor Stinner11517102014-07-29 23:31:34 +0200123#endif
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000124
Christian Heimesb76922a2007-12-11 01:06:40 +0000125/* Speed up sigcheck() when none tripped */
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +0200126static _Py_atomic_int is_tripped;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000127
Barry Warsaw92971171997-01-03 00:14:25 +0000128static PyObject *DefaultHandler;
129static PyObject *IgnoreHandler;
130static PyObject *IntHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000131
Antoine Pitrou6dd381e2011-11-21 21:26:56 +0100132#ifdef MS_WINDOWS
133static HANDLE sigint_event = NULL;
134#endif
135
Martin v. Löwis823725e2008-03-24 13:39:54 +0000136#ifdef HAVE_GETITIMER
137static PyObject *ItimerError;
138
Victor Stinneref611c92017-10-13 13:49:43 -0700139/* auxiliary functions for setitimer */
140static int
141timeval_from_double(PyObject *obj, struct timeval *tv)
Martin v. Löwis823725e2008-03-24 13:39:54 +0000142{
Victor Stinneref611c92017-10-13 13:49:43 -0700143 if (obj == NULL) {
144 tv->tv_sec = 0;
145 tv->tv_usec = 0;
146 return 0;
Antoine Pitrou729780a2017-06-30 10:01:05 +0200147 }
Victor Stinneref611c92017-10-13 13:49:43 -0700148
149 _PyTime_t t;
150 if (_PyTime_FromSecondsObject(&t, obj, _PyTime_ROUND_CEILING) < 0) {
151 return -1;
152 }
153 return _PyTime_AsTimeval(t, tv, _PyTime_ROUND_CEILING);
Martin v. Löwis823725e2008-03-24 13:39:54 +0000154}
155
Christian Heimes1a8501c2008-10-02 19:56:01 +0000156Py_LOCAL_INLINE(double)
Martin v. Löwis823725e2008-03-24 13:39:54 +0000157double_from_timeval(struct timeval *tv)
158{
159 return tv->tv_sec + (double)(tv->tv_usec / 1000000.0);
160}
161
162static PyObject *
163itimer_retval(struct itimerval *iv)
164{
165 PyObject *r, *v;
166
167 r = PyTuple_New(2);
168 if (r == NULL)
Martin Panter6d57fe12016-09-17 03:26:16 +0000169 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000170
171 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_value)))) {
Martin Panter6d57fe12016-09-17 03:26:16 +0000172 Py_DECREF(r);
173 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000174 }
175
176 PyTuple_SET_ITEM(r, 0, v);
177
178 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_interval)))) {
Martin Panter6d57fe12016-09-17 03:26:16 +0000179 Py_DECREF(r);
180 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000181 }
182
183 PyTuple_SET_ITEM(r, 1, v);
184
185 return r;
186}
187#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000188
Eric Snow64d6cc82019-02-23 15:40:43 -0700189static int
190is_main(void)
191{
192 return PyThread_get_thread_ident() == main_thread &&
193 _PyInterpreterState_Get() == main_interp;
194}
195
Guido van Rossume4485b01994-09-07 14:32:49 +0000196static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000197signal_default_int_handler(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000198{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000199 PyErr_SetNone(PyExc_KeyboardInterrupt);
200 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000201}
202
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000203PyDoc_STRVAR(default_int_handler_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000204"default_int_handler(...)\n\
205\n\
Michael W. Hudson24ec2112004-06-17 15:55:53 +0000206The default handler for SIGINT installed by Python.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000207It raises KeyboardInterrupt.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000208
Thomas Wouters0796b002000-07-22 23:49:30 +0000209
210static int
Victor Stinner11517102014-07-29 23:31:34 +0200211report_wakeup_write_error(void *data)
Antoine Pitrou6f6ec372013-08-17 20:27:56 +0200212{
Eric Snowfdf282d2019-01-11 14:26:55 -0700213 PyObject *exc, *val, *tb;
Antoine Pitrou6f6ec372013-08-17 20:27:56 +0200214 int save_errno = errno;
Benjamin Petersonca470632016-09-06 13:47:26 -0700215 errno = (int) (intptr_t) data;
Eric Snowfdf282d2019-01-11 14:26:55 -0700216 PyErr_Fetch(&exc, &val, &tb);
Antoine Pitrou6f6ec372013-08-17 20:27:56 +0200217 PyErr_SetFromErrno(PyExc_OSError);
218 PySys_WriteStderr("Exception ignored when trying to write to the "
219 "signal wakeup fd:\n");
220 PyErr_WriteUnraisable(NULL);
Eric Snowfdf282d2019-01-11 14:26:55 -0700221 PyErr_Restore(exc, val, tb);
Antoine Pitrou6f6ec372013-08-17 20:27:56 +0200222 errno = save_errno;
223 return 0;
224}
225
Victor Stinner11517102014-07-29 23:31:34 +0200226#ifdef MS_WINDOWS
227static int
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800228report_wakeup_send_error(void* data)
Victor Stinner11517102014-07-29 23:31:34 +0200229{
Eric Snowfdf282d2019-01-11 14:26:55 -0700230 PyObject *exc, *val, *tb;
231 PyErr_Fetch(&exc, &val, &tb);
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800232 /* PyErr_SetExcFromWindowsErr() invokes FormatMessage() which
233 recognizes the error codes used by both GetLastError() and
234 WSAGetLastError */
235 PyErr_SetExcFromWindowsErr(PyExc_OSError, (int) (intptr_t) data);
Victor Stinner11517102014-07-29 23:31:34 +0200236 PySys_WriteStderr("Exception ignored when trying to send to the "
237 "signal wakeup fd:\n");
238 PyErr_WriteUnraisable(NULL);
Eric Snowfdf282d2019-01-11 14:26:55 -0700239 PyErr_Restore(exc, val, tb);
Victor Stinner11517102014-07-29 23:31:34 +0200240 return 0;
241}
242#endif /* MS_WINDOWS */
243
Tim Peters4f1b2082000-07-23 21:18:09 +0000244static void
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200245trip_signal(int sig_num)
246{
Victor Stinnerd49b1f12011-05-08 02:03:15 +0200247 unsigned char byte;
Victor Stinner11517102014-07-29 23:31:34 +0200248 int fd;
249 Py_ssize_t rc;
Victor Stinnerc13ef662011-05-25 02:35:58 +0200250
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +0200251 _Py_atomic_store_relaxed(&Handlers[sig_num].tripped, 1);
Victor Stinner11517102014-07-29 23:31:34 +0200252
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200253 /* Set is_tripped after setting .tripped, as it gets
254 cleared in PyErr_CheckSignals() before .tripped. */
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +0200255 _Py_atomic_store(&is_tripped, 1);
256
257 /* Notify ceval.c */
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200258 _PyEval_SignalReceived();
Nathaniel J. Smith4ae01492017-05-16 14:12:11 -0700259
260 /* And then write to the wakeup fd *after* setting all the globals and
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200261 doing the _PyEval_SignalReceived. We used to write to the wakeup fd
262 and then set the flag, but this allowed the following sequence of events
263 (especially on windows, where trip_signal may run in a new thread):
Nathaniel J. Smith4ae01492017-05-16 14:12:11 -0700264
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800265 - main thread blocks on select([wakeup.fd], ...)
Nathaniel J. Smith4ae01492017-05-16 14:12:11 -0700266 - signal arrives
267 - trip_signal writes to the wakeup fd
268 - the main thread wakes up
269 - the main thread checks the signal flags, sees that they're unset
270 - the main thread empties the wakeup fd
271 - the main thread goes back to sleep
272 - trip_signal sets the flags to request the Python-level signal handler
273 be run
274 - the main thread doesn't notice, because it's asleep
275
276 See bpo-30038 for more details.
277 */
278
Victor Stinner11517102014-07-29 23:31:34 +0200279#ifdef MS_WINDOWS
280 fd = Py_SAFE_DOWNCAST(wakeup.fd, SOCKET_T, int);
281#else
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800282 fd = wakeup.fd;
Victor Stinner11517102014-07-29 23:31:34 +0200283#endif
284
285 if (fd != INVALID_FD) {
Victor Stinnerc13ef662011-05-25 02:35:58 +0200286 byte = (unsigned char)sig_num;
Victor Stinner11517102014-07-29 23:31:34 +0200287#ifdef MS_WINDOWS
288 if (wakeup.use_send) {
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800289 rc = send(fd, &byte, 1, 0);
Victor Stinner11517102014-07-29 23:31:34 +0200290
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800291 if (rc < 0) {
292 int last_error = GetLastError();
293 if (wakeup.warn_on_full_buffer ||
294 last_error != WSAEWOULDBLOCK)
295 {
296 /* Py_AddPendingCall() isn't signal-safe, but we
297 still use it for this exceptional case. */
298 Py_AddPendingCall(report_wakeup_send_error,
299 (void *)(intptr_t) last_error);
300 }
Victor Stinner11517102014-07-29 23:31:34 +0200301 }
302 }
303 else
304#endif
305 {
Victor Stinnere72fe392015-04-01 18:35:22 +0200306 /* _Py_write_noraise() retries write() if write() is interrupted by
307 a signal (fails with EINTR). */
308 rc = _Py_write_noraise(fd, &byte, 1);
Victor Stinner11517102014-07-29 23:31:34 +0200309
310 if (rc < 0) {
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800311 if (wakeup.warn_on_full_buffer ||
312 (errno != EWOULDBLOCK && errno != EAGAIN))
313 {
314 /* Py_AddPendingCall() isn't signal-safe, but we
315 still use it for this exceptional case. */
316 Py_AddPendingCall(report_wakeup_write_error,
317 (void *)(intptr_t)errno);
318 }
Victor Stinner11517102014-07-29 23:31:34 +0200319 }
320 }
Victor Stinnerc13ef662011-05-25 02:35:58 +0200321 }
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200322}
323
324static void
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000325signal_handler(int sig_num)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000326{
Antoine Pitrou39a65912010-11-05 19:47:27 +0000327 int save_errno = errno;
328
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000329 /* See NOTES section above */
Antoine Pitrou39a65912010-11-05 19:47:27 +0000330 if (getpid() == main_pid)
Antoine Pitrou39a65912010-11-05 19:47:27 +0000331 {
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200332 trip_signal(sig_num);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000333 }
Antoine Pitrou39a65912010-11-05 19:47:27 +0000334
Jean-Paul Calderone6f137ca2010-05-09 03:18:57 +0000335#ifndef HAVE_SIGACTION
Antoine Pitrou39a65912010-11-05 19:47:27 +0000336#ifdef SIGCHLD
337 /* To avoid infinite recursion, this signal remains
338 reset until explicit re-instated.
339 Don't clear the 'func' field as it is our pointer
340 to the Python handler... */
341 if (sig_num != SIGCHLD)
342#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000343 /* If the handler was not set up with sigaction, reinstall it. See
Nick Coghland6009512014-11-20 21:39:37 +1000344 * Python/pylifecycle.c for the implementation of PyOS_setsig which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000345 * makes this true. See also issue8354. */
346 PyOS_setsig(sig_num, signal_handler);
Jean-Paul Calderone6f137ca2010-05-09 03:18:57 +0000347#endif
Antoine Pitrou39a65912010-11-05 19:47:27 +0000348
349 /* Issue #10311: asynchronously executing signal handlers should not
350 mutate errno under the feet of unsuspecting C code. */
351 errno = save_errno;
Antoine Pitrou6dd381e2011-11-21 21:26:56 +0100352
353#ifdef MS_WINDOWS
354 if (sig_num == SIGINT)
355 SetEvent(sigint_event);
356#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000357}
Guido van Rossume4485b01994-09-07 14:32:49 +0000358
Guido van Rossum06d511d1995-03-10 15:13:48 +0000359
Guido van Rossum1171ee61997-08-22 20:42:00 +0000360#ifdef HAVE_ALARM
Tal Einatc7027b72015-05-16 14:14:49 +0300361
362/*[clinic input]
363signal.alarm -> long
364
365 seconds: int
366 /
367
368Arrange for SIGALRM to arrive after the given number of seconds.
369[clinic start generated code]*/
370
371static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300372signal_alarm_impl(PyObject *module, int seconds)
373/*[clinic end generated code: output=144232290814c298 input=0d5e97e0e6f39e86]*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000374{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000375 /* alarm() returns the number of seconds remaining */
Tal Einatc7027b72015-05-16 14:14:49 +0300376 return (long)alarm(seconds);
Guido van Rossumaa0f4c71994-08-23 13:49:37 +0000377}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000378
Guido van Rossum06d511d1995-03-10 15:13:48 +0000379#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000380
Guido van Rossum1171ee61997-08-22 20:42:00 +0000381#ifdef HAVE_PAUSE
Tal Einatc7027b72015-05-16 14:14:49 +0300382
383/*[clinic input]
384signal.pause
385
386Wait until a signal arrives.
387[clinic start generated code]*/
388
Guido van Rossuma597dde1995-01-10 20:56:29 +0000389static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300390signal_pause_impl(PyObject *module)
391/*[clinic end generated code: output=391656788b3c3929 input=f03de0f875752062]*/
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000392{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000393 Py_BEGIN_ALLOW_THREADS
394 (void)pause();
395 Py_END_ALLOW_THREADS
396 /* make sure that any exceptions that got raised are propagated
397 * back into Python
398 */
399 if (PyErr_CheckSignals())
400 return NULL;
Barry Warsaw92971171997-01-03 00:14:25 +0000401
Tal Einatc7027b72015-05-16 14:14:49 +0300402 Py_RETURN_NONE;
Guido van Rossume4485b01994-09-07 14:32:49 +0000403}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000404
Guido van Rossum06d511d1995-03-10 15:13:48 +0000405#endif
Guido van Rossume4485b01994-09-07 14:32:49 +0000406
Vladimir Matveevc24c6c22019-01-08 01:58:25 -0800407/*[clinic input]
408signal.raise_signal
409
410 signalnum: int
411 /
412
413Send a signal to the executing process.
414[clinic start generated code]*/
415
416static PyObject *
417signal_raise_signal_impl(PyObject *module, int signalnum)
418/*[clinic end generated code: output=e2b014220aa6111d input=e90c0f9a42358de6]*/
419{
420 int err;
421 Py_BEGIN_ALLOW_THREADS
422 _Py_BEGIN_SUPPRESS_IPH
423 err = raise(signalnum);
424 _Py_END_SUPPRESS_IPH
425 Py_END_ALLOW_THREADS
426
427 if (err) {
428 return PyErr_SetFromErrno(PyExc_OSError);
429 }
430 Py_RETURN_NONE;
431}
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000432
Tal Einatc7027b72015-05-16 14:14:49 +0300433/*[clinic input]
434signal.signal
435
436 signalnum: int
437 handler: object
438 /
439
440Set the action for the given signal.
441
442The action can be SIG_DFL, SIG_IGN, or a callable Python object.
443The previous action is returned. See getsignal() for possible return values.
444
445*** IMPORTANT NOTICE ***
446A signal handler function is called with two arguments:
447the first is the signal number, the second is the interrupted stack frame.
448[clinic start generated code]*/
449
Guido van Rossume4485b01994-09-07 14:32:49 +0000450static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300451signal_signal_impl(PyObject *module, int signalnum, PyObject *handler)
452/*[clinic end generated code: output=b44cfda43780f3a1 input=deee84af5fa0432c]*/
Guido van Rossume4485b01994-09-07 14:32:49 +0000453{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000454 PyObject *old_handler;
455 void (*func)(int);
Brian Curtinef9efbd2010-08-06 19:27:32 +0000456#ifdef MS_WINDOWS
Tal Einatc7027b72015-05-16 14:14:49 +0300457 /* Validate that signalnum is one of the allowable signals */
458 switch (signalnum) {
Brian Curtinc734b312010-09-06 16:04:10 +0000459 case SIGABRT: break;
Brian Curtin9e88b5a2010-10-01 14:49:24 +0000460#ifdef SIGBREAK
461 /* Issue #10003: SIGBREAK is not documented as permitted, but works
462 and corresponds to CTRL_BREAK_EVENT. */
463 case SIGBREAK: break;
464#endif
Brian Curtinc734b312010-09-06 16:04:10 +0000465 case SIGFPE: break;
466 case SIGILL: break;
467 case SIGINT: break;
468 case SIGSEGV: break;
469 case SIGTERM: break;
470 default:
471 PyErr_SetString(PyExc_ValueError, "invalid signal value");
472 return NULL;
Brian Curtinef9efbd2010-08-06 19:27:32 +0000473 }
474#endif
Eric Snow64d6cc82019-02-23 15:40:43 -0700475 if (!is_main()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000476 PyErr_SetString(PyExc_ValueError,
477 "signal only works in main thread");
478 return NULL;
479 }
Tal Einatc7027b72015-05-16 14:14:49 +0300480 if (signalnum < 1 || signalnum >= NSIG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000481 PyErr_SetString(PyExc_ValueError,
482 "signal number out of range");
483 return NULL;
484 }
Tal Einatc7027b72015-05-16 14:14:49 +0300485 if (handler == IgnoreHandler)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000486 func = SIG_IGN;
Tal Einatc7027b72015-05-16 14:14:49 +0300487 else if (handler == DefaultHandler)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000488 func = SIG_DFL;
Tal Einatc7027b72015-05-16 14:14:49 +0300489 else if (!PyCallable_Check(handler)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000490 PyErr_SetString(PyExc_TypeError,
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000491"signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000492 return NULL;
493 }
494 else
495 func = signal_handler;
Antoine Pitrouf6f90ff2017-11-03 19:58:46 +0100496 /* Check for pending signals before changing signal handler */
Eric Snow64d6cc82019-02-23 15:40:43 -0700497 if (_PyErr_CheckSignals()) {
Antoine Pitrouf6f90ff2017-11-03 19:58:46 +0100498 return NULL;
499 }
Tal Einatc7027b72015-05-16 14:14:49 +0300500 if (PyOS_setsig(signalnum, func) == SIG_ERR) {
Victor Stinner388196e2011-05-10 17:13:00 +0200501 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000502 return NULL;
503 }
Tal Einatc7027b72015-05-16 14:14:49 +0300504 old_handler = Handlers[signalnum].func;
Tal Einatc7027b72015-05-16 14:14:49 +0300505 Py_INCREF(handler);
506 Handlers[signalnum].func = handler;
Antoine Pitrouc8c952c2013-05-04 23:16:59 +0200507 if (old_handler != NULL)
508 return old_handler;
509 else
510 Py_RETURN_NONE;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000511}
512
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000513
Tal Einatc7027b72015-05-16 14:14:49 +0300514/*[clinic input]
515signal.getsignal
516
517 signalnum: int
518 /
519
520Return the current action for the given signal.
521
522The return value can be:
523 SIG_IGN -- if the signal is being ignored
524 SIG_DFL -- if the default action for the signal is in effect
525 None -- if an unknown handler is in effect
526 anything else -- the callable Python object used as a handler
527[clinic start generated code]*/
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000528
Guido van Rossume4485b01994-09-07 14:32:49 +0000529static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300530signal_getsignal_impl(PyObject *module, int signalnum)
531/*[clinic end generated code: output=35b3e0e796fd555e input=ac23a00f19dfa509]*/
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000532{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000533 PyObject *old_handler;
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 old_handler = Handlers[signalnum].func;
Antoine Pitrouc8c952c2013-05-04 23:16:59 +0200540 if (old_handler != NULL) {
541 Py_INCREF(old_handler);
542 return old_handler;
543 }
544 else {
545 Py_RETURN_NONE;
546 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000547}
548
Antoine Pietri5d2a27d2018-03-12 14:42:34 +0100549
550/*[clinic input]
551signal.strsignal
552
553 signalnum: int
554 /
555
556Return the system description of the given signal.
557
558The return values can be such as "Interrupt", "Segmentation fault", etc.
559Returns None if the signal is not recognized.
560[clinic start generated code]*/
561
562static PyObject *
563signal_strsignal_impl(PyObject *module, int signalnum)
564/*[clinic end generated code: output=44e12e1e3b666261 input=b77914b03f856c74]*/
565{
566 char *res;
567
568 if (signalnum < 1 || signalnum >= NSIG) {
569 PyErr_SetString(PyExc_ValueError,
570 "signal number out of range");
571 return NULL;
572 }
573
Michael Osipov48ce4892018-08-23 15:27:19 +0200574#ifndef HAVE_STRSIGNAL
Antoine Pietri5d2a27d2018-03-12 14:42:34 +0100575 switch (signalnum) {
Michael Osipov48ce4892018-08-23 15:27:19 +0200576 /* Though being a UNIX, HP-UX does not provide strsignal(3). */
577#ifndef MS_WINDOWS
578 case SIGHUP:
579 res = "Hangup";
580 break;
581 case SIGALRM:
582 res = "Alarm clock";
583 break;
584 case SIGPIPE:
585 res = "Broken pipe";
586 break;
587 case SIGQUIT:
588 res = "Quit";
589 break;
590 case SIGCHLD:
591 res = "Child exited";
592 break;
593#endif
594 /* Custom redefinition of POSIX signals allowed on Windows. */
Antoine Pietri5d2a27d2018-03-12 14:42:34 +0100595 case SIGINT:
596 res = "Interrupt";
597 break;
598 case SIGILL:
599 res = "Illegal instruction";
600 break;
601 case SIGABRT:
602 res = "Aborted";
603 break;
604 case SIGFPE:
605 res = "Floating point exception";
606 break;
607 case SIGSEGV:
608 res = "Segmentation fault";
609 break;
610 case SIGTERM:
611 res = "Terminated";
612 break;
613 default:
614 Py_RETURN_NONE;
615 }
616#else
617 errno = 0;
618 res = strsignal(signalnum);
619
620 if (errno || res == NULL || strstr(res, "Unknown signal") != NULL)
621 Py_RETURN_NONE;
622#endif
623
624 return Py_BuildValue("s", res);
625}
626
Christian Heimes8640e742008-02-23 16:23:06 +0000627#ifdef HAVE_SIGINTERRUPT
Tal Einatc7027b72015-05-16 14:14:49 +0300628
629/*[clinic input]
630signal.siginterrupt
631
632 signalnum: int
633 flag: int
634 /
635
636Change system call restart behaviour.
637
638If flag is False, system calls will be restarted when interrupted by
639signal sig, else system calls will be interrupted.
640[clinic start generated code]*/
Christian Heimes8640e742008-02-23 16:23:06 +0000641
642static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300643signal_siginterrupt_impl(PyObject *module, int signalnum, int flag)
644/*[clinic end generated code: output=063816243d85dd19 input=4160acacca3e2099]*/
Christian Heimes8640e742008-02-23 16:23:06 +0000645{
Tal Einatc7027b72015-05-16 14:14:49 +0300646 if (signalnum < 1 || signalnum >= NSIG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000647 PyErr_SetString(PyExc_ValueError,
648 "signal number out of range");
649 return NULL;
650 }
Tal Einatc7027b72015-05-16 14:14:49 +0300651 if (siginterrupt(signalnum, flag)<0) {
Victor Stinner388196e2011-05-10 17:13:00 +0200652 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000653 return NULL;
654 }
Tal Einatc7027b72015-05-16 14:14:49 +0300655 Py_RETURN_NONE;
Christian Heimes8640e742008-02-23 16:23:06 +0000656}
657
658#endif
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000659
Tal Einatc7027b72015-05-16 14:14:49 +0300660
661static PyObject*
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800662signal_set_wakeup_fd(PyObject *self, PyObject *args, PyObject *kwds)
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000663{
Victor Stinnere134a7f2015-03-30 10:09:31 +0200664 struct _Py_stat_struct status;
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800665 static char *kwlist[] = {
666 "", "warn_on_full_buffer", NULL,
667 };
668 int warn_on_full_buffer = 1;
Victor Stinner11517102014-07-29 23:31:34 +0200669#ifdef MS_WINDOWS
670 PyObject *fdobj;
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100671 SOCKET_T sockfd, old_sockfd;
Victor Stinner11517102014-07-29 23:31:34 +0200672 int res;
673 int res_size = sizeof res;
674 PyObject *mod;
Victor Stinner11517102014-07-29 23:31:34 +0200675 int is_socket;
676
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800677 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|$p:set_wakeup_fd", kwlist,
678 &fdobj, &warn_on_full_buffer))
Victor Stinner11517102014-07-29 23:31:34 +0200679 return NULL;
680
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100681 sockfd = PyLong_AsSocket_t(fdobj);
682 if (sockfd == (SOCKET_T)(-1) && PyErr_Occurred())
Victor Stinner11517102014-07-29 23:31:34 +0200683 return NULL;
684#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000685 int fd, old_fd;
Victor Stinner11517102014-07-29 23:31:34 +0200686
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800687 if (!PyArg_ParseTupleAndKeywords(args, kwds, "i|$p:set_wakeup_fd", kwlist,
688 &fd, &warn_on_full_buffer))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000689 return NULL;
Victor Stinner11517102014-07-29 23:31:34 +0200690#endif
691
Eric Snow64d6cc82019-02-23 15:40:43 -0700692 if (!is_main()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000693 PyErr_SetString(PyExc_ValueError,
694 "set_wakeup_fd only works in main thread");
695 return NULL;
696 }
Victor Stinner0bffc942014-07-21 16:28:54 +0200697
Victor Stinner11517102014-07-29 23:31:34 +0200698#ifdef MS_WINDOWS
699 is_socket = 0;
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100700 if (sockfd != INVALID_FD) {
Victor Stinner11517102014-07-29 23:31:34 +0200701 /* Import the _socket module to call WSAStartup() */
702 mod = PyImport_ImportModuleNoBlock("_socket");
703 if (mod == NULL)
704 return NULL;
705 Py_DECREF(mod);
706
707 /* test the socket */
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100708 if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR,
Victor Stinner11517102014-07-29 23:31:34 +0200709 (char *)&res, &res_size) != 0) {
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100710 int fd, err;
711
712 err = WSAGetLastError();
Victor Stinner11517102014-07-29 23:31:34 +0200713 if (err != WSAENOTSOCK) {
714 PyErr_SetExcFromWindowsErr(PyExc_OSError, err);
715 return NULL;
716 }
717
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100718 fd = (int)sockfd;
Steve Dower940f33a2016-09-08 11:21:54 -0700719 if ((SOCKET_T)fd != sockfd) {
Victor Stinner11517102014-07-29 23:31:34 +0200720 PyErr_SetString(PyExc_ValueError, "invalid fd");
721 return NULL;
722 }
723
Victor Stinnere134a7f2015-03-30 10:09:31 +0200724 if (_Py_fstat(fd, &status) != 0)
Victor Stinner11517102014-07-29 23:31:34 +0200725 return NULL;
Victor Stinner38227602014-08-27 12:59:44 +0200726
727 /* on Windows, a file cannot be set to non-blocking mode */
Victor Stinner11517102014-07-29 23:31:34 +0200728 }
Victor Stinner38227602014-08-27 12:59:44 +0200729 else {
Victor Stinner11517102014-07-29 23:31:34 +0200730 is_socket = 1;
Victor Stinner38227602014-08-27 12:59:44 +0200731
732 /* Windows does not provide a function to test if a socket
733 is in non-blocking mode */
734 }
Victor Stinner11517102014-07-29 23:31:34 +0200735 }
736
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100737 old_sockfd = wakeup.fd;
738 wakeup.fd = sockfd;
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800739 wakeup.warn_on_full_buffer = warn_on_full_buffer;
Victor Stinner11517102014-07-29 23:31:34 +0200740 wakeup.use_send = is_socket;
741
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100742 if (old_sockfd != INVALID_FD)
743 return PyLong_FromSocket_t(old_sockfd);
Victor Stinner11517102014-07-29 23:31:34 +0200744 else
745 return PyLong_FromLong(-1);
746#else
Victor Stinnerd18ccd12014-07-24 21:58:53 +0200747 if (fd != -1) {
Victor Stinner38227602014-08-27 12:59:44 +0200748 int blocking;
749
Victor Stinnere134a7f2015-03-30 10:09:31 +0200750 if (_Py_fstat(fd, &status) != 0)
Victor Stinner11517102014-07-29 23:31:34 +0200751 return NULL;
Victor Stinner38227602014-08-27 12:59:44 +0200752
753 blocking = _Py_get_blocking(fd);
754 if (blocking < 0)
755 return NULL;
756 if (blocking) {
757 PyErr_Format(PyExc_ValueError,
758 "the fd %i must be in non-blocking mode",
759 fd);
760 return NULL;
761 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000762 }
Victor Stinner0bffc942014-07-21 16:28:54 +0200763
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800764 old_fd = wakeup.fd;
765 wakeup.fd = fd;
766 wakeup.warn_on_full_buffer = warn_on_full_buffer;
Victor Stinner0bffc942014-07-21 16:28:54 +0200767
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000768 return PyLong_FromLong(old_fd);
Victor Stinner11517102014-07-29 23:31:34 +0200769#endif
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000770}
771
772PyDoc_STRVAR(set_wakeup_fd_doc,
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800773"set_wakeup_fd(fd, *, warn_on_full_buffer=True) -> fd\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000774\n\
Victor Stinner11517102014-07-29 23:31:34 +0200775Sets the fd to be written to (with the signal number) when a signal\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000776comes in. A library can use this to wakeup select or poll.\n\
Victor Stinner11517102014-07-29 23:31:34 +0200777The previous fd or -1 is returned.\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000778\n\
779The fd must be non-blocking.");
780
781/* C API for the same, without all the error checking */
782int
783PySignal_SetWakeupFd(int fd)
784{
Victor Stinner11517102014-07-29 23:31:34 +0200785 int old_fd;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000786 if (fd < 0)
787 fd = -1;
Victor Stinner11517102014-07-29 23:31:34 +0200788
789#ifdef MS_WINDOWS
790 old_fd = Py_SAFE_DOWNCAST(wakeup.fd, SOCKET_T, int);
Victor Stinner11517102014-07-29 23:31:34 +0200791#else
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800792 old_fd = wakeup.fd;
Victor Stinner11517102014-07-29 23:31:34 +0200793#endif
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800794 wakeup.fd = fd;
795 wakeup.warn_on_full_buffer = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000796 return old_fd;
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000797}
798
799
Martin v. Löwis823725e2008-03-24 13:39:54 +0000800#ifdef HAVE_SETITIMER
Tal Einatc7027b72015-05-16 14:14:49 +0300801
802/*[clinic input]
803signal.setitimer
804
805 which: int
Victor Stinneref611c92017-10-13 13:49:43 -0700806 seconds: object
807 interval: object(c_default="NULL") = 0.0
Tal Einatc7027b72015-05-16 14:14:49 +0300808 /
809
810Sets given itimer (one of ITIMER_REAL, ITIMER_VIRTUAL or ITIMER_PROF).
811
812The timer will fire after value seconds and after that every interval seconds.
813The itimer can be cleared by setting seconds to zero.
814
815Returns old values as a tuple: (delay, interval).
816[clinic start generated code]*/
817
Martin v. Löwis823725e2008-03-24 13:39:54 +0000818static PyObject *
Victor Stinneref611c92017-10-13 13:49:43 -0700819signal_setitimer_impl(PyObject *module, int which, PyObject *seconds,
820 PyObject *interval)
821/*[clinic end generated code: output=65f9dcbddc35527b input=de43daf194e6f66f]*/
Martin v. Löwis823725e2008-03-24 13:39:54 +0000822{
Martin v. Löwis823725e2008-03-24 13:39:54 +0000823 struct itimerval new, old;
824
Victor Stinneref611c92017-10-13 13:49:43 -0700825 if (timeval_from_double(seconds, &new.it_value) < 0) {
826 return NULL;
827 }
828 if (timeval_from_double(interval, &new.it_interval) < 0) {
829 return NULL;
830 }
831
Martin v. Löwis823725e2008-03-24 13:39:54 +0000832 /* Let OS check "which" value */
833 if (setitimer(which, &new, &old) != 0) {
Tal Einatc7027b72015-05-16 14:14:49 +0300834 PyErr_SetFromErrno(ItimerError);
835 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000836 }
837
838 return itimer_retval(&old);
839}
840
Martin v. Löwis823725e2008-03-24 13:39:54 +0000841#endif
842
843
844#ifdef HAVE_GETITIMER
Tal Einatc7027b72015-05-16 14:14:49 +0300845
846/*[clinic input]
847signal.getitimer
848
849 which: int
850 /
851
852Returns current value of given itimer.
853[clinic start generated code]*/
854
Martin v. Löwis823725e2008-03-24 13:39:54 +0000855static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300856signal_getitimer_impl(PyObject *module, int which)
857/*[clinic end generated code: output=9e053175d517db40 input=f7d21d38f3490627]*/
Martin v. Löwis823725e2008-03-24 13:39:54 +0000858{
Martin v. Löwis823725e2008-03-24 13:39:54 +0000859 struct itimerval old;
860
Martin v. Löwis823725e2008-03-24 13:39:54 +0000861 if (getitimer(which, &old) != 0) {
Tal Einatc7027b72015-05-16 14:14:49 +0300862 PyErr_SetFromErrno(ItimerError);
863 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000864 }
865
866 return itimer_retval(&old);
867}
868
Martin v. Löwis823725e2008-03-24 13:39:54 +0000869#endif
870
Victor Stinnerb3e72192011-05-08 01:46:11 +0200871#if defined(PYPTHREAD_SIGMASK) || defined(HAVE_SIGPENDING)
Victor Stinner35b300c2011-05-04 13:20:35 +0200872static PyObject*
873sigset_to_set(sigset_t mask)
874{
875 PyObject *signum, *result;
876 int sig;
877
878 result = PySet_New(0);
879 if (result == NULL)
880 return NULL;
881
882 for (sig = 1; sig < NSIG; sig++) {
883 if (sigismember(&mask, sig) != 1)
884 continue;
885
886 /* Handle the case where it is a member by adding the signal to
887 the result list. Ignore the other cases because they mean the
888 signal isn't a member of the mask or the signal was invalid,
889 and an invalid signal must have been our fault in constructing
890 the loop boundaries. */
891 signum = PyLong_FromLong(sig);
892 if (signum == NULL) {
893 Py_DECREF(result);
894 return NULL;
895 }
896 if (PySet_Add(result, signum) == -1) {
897 Py_DECREF(signum);
898 Py_DECREF(result);
899 return NULL;
900 }
901 Py_DECREF(signum);
902 }
903 return result;
904}
Victor Stinnerb3e72192011-05-08 01:46:11 +0200905#endif
Victor Stinner35b300c2011-05-04 13:20:35 +0200906
Victor Stinnerb3e72192011-05-08 01:46:11 +0200907#ifdef PYPTHREAD_SIGMASK
Tal Einatc7027b72015-05-16 14:14:49 +0300908
909/*[clinic input]
910signal.pthread_sigmask
911
912 how: int
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300913 mask: sigset_t
Tal Einatc7027b72015-05-16 14:14:49 +0300914 /
915
916Fetch and/or change the signal mask of the calling thread.
917[clinic start generated code]*/
918
Victor Stinnera9293352011-04-30 15:21:58 +0200919static PyObject *
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300920signal_pthread_sigmask_impl(PyObject *module, int how, sigset_t mask)
921/*[clinic end generated code: output=0562c0fb192981a8 input=85bcebda442fa77f]*/
Victor Stinnera9293352011-04-30 15:21:58 +0200922{
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300923 sigset_t previous;
Victor Stinnera9293352011-04-30 15:21:58 +0200924 int err;
925
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300926 err = pthread_sigmask(how, &mask, &previous);
Victor Stinnera9293352011-04-30 15:21:58 +0200927 if (err != 0) {
928 errno = err;
Victor Stinnerb3e72192011-05-08 01:46:11 +0200929 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnera9293352011-04-30 15:21:58 +0200930 return NULL;
931 }
932
Victor Stinnerd0e516d2011-05-03 14:57:12 +0200933 /* if signals was unblocked, signal handlers have been called */
934 if (PyErr_CheckSignals())
935 return NULL;
936
Victor Stinner35b300c2011-05-04 13:20:35 +0200937 return sigset_to_set(previous);
Victor Stinnera9293352011-04-30 15:21:58 +0200938}
939
Victor Stinnera9293352011-04-30 15:21:58 +0200940#endif /* #ifdef PYPTHREAD_SIGMASK */
941
Martin v. Löwis823725e2008-03-24 13:39:54 +0000942
Victor Stinnerb3e72192011-05-08 01:46:11 +0200943#ifdef HAVE_SIGPENDING
Tal Einatc7027b72015-05-16 14:14:49 +0300944
945/*[clinic input]
946signal.sigpending
947
948Examine pending signals.
949
950Returns a set of signal numbers that are pending for delivery to
951the calling thread.
952[clinic start generated code]*/
953
Victor Stinnerb3e72192011-05-08 01:46:11 +0200954static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300955signal_sigpending_impl(PyObject *module)
956/*[clinic end generated code: output=53375ffe89325022 input=e0036c016f874e29]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +0200957{
958 int err;
959 sigset_t mask;
960 err = sigpending(&mask);
961 if (err)
962 return PyErr_SetFromErrno(PyExc_OSError);
963 return sigset_to_set(mask);
964}
965
Victor Stinnerb3e72192011-05-08 01:46:11 +0200966#endif /* #ifdef HAVE_SIGPENDING */
967
968
969#ifdef HAVE_SIGWAIT
Tal Einatc7027b72015-05-16 14:14:49 +0300970
971/*[clinic input]
972signal.sigwait
973
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300974 sigset: sigset_t
Tal Einatc7027b72015-05-16 14:14:49 +0300975 /
976
977Wait for a signal.
978
979Suspend execution of the calling thread until the delivery of one of the
980signals specified in the signal set sigset. The function accepts the signal
981and returns the signal number.
982[clinic start generated code]*/
983
Victor Stinnerb3e72192011-05-08 01:46:11 +0200984static PyObject *
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300985signal_sigwait_impl(PyObject *module, sigset_t sigset)
986/*[clinic end generated code: output=f43770699d682f96 input=a6fbd47b1086d119]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +0200987{
Victor Stinnerb3e72192011-05-08 01:46:11 +0200988 int err, signum;
989
Victor Stinner10c30d62011-06-10 01:39:53 +0200990 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300991 err = sigwait(&sigset, &signum);
Victor Stinner10c30d62011-06-10 01:39:53 +0200992 Py_END_ALLOW_THREADS
Victor Stinnerb3e72192011-05-08 01:46:11 +0200993 if (err) {
994 errno = err;
995 return PyErr_SetFromErrno(PyExc_OSError);
996 }
997
998 return PyLong_FromLong(signum);
999}
1000
Tal Einatc7027b72015-05-16 14:14:49 +03001001#endif /* #ifdef HAVE_SIGWAIT */
1002
Victor Stinnerb3e72192011-05-08 01:46:11 +02001003
Antoine Pitrou9d3627e2018-05-04 13:00:50 +02001004#if defined(HAVE_SIGFILLSET) || defined(MS_WINDOWS)
1005
1006/*[clinic input]
1007signal.valid_signals
1008
1009Return a set of valid signal numbers on this platform.
1010
1011The signal numbers returned by this function can be safely passed to
1012functions like `pthread_sigmask`.
1013[clinic start generated code]*/
1014
1015static PyObject *
1016signal_valid_signals_impl(PyObject *module)
1017/*[clinic end generated code: output=1609cffbcfcf1314 input=86a3717ff25288f2]*/
1018{
1019#ifdef MS_WINDOWS
1020#ifdef SIGBREAK
1021 PyObject *tup = Py_BuildValue("(iiiiiii)", SIGABRT, SIGBREAK, SIGFPE,
1022 SIGILL, SIGINT, SIGSEGV, SIGTERM);
1023#else
1024 PyObject *tup = Py_BuildValue("(iiiiii)", SIGABRT, SIGFPE, SIGILL,
1025 SIGINT, SIGSEGV, SIGTERM);
1026#endif
1027 if (tup == NULL) {
1028 return NULL;
1029 }
1030 PyObject *set = PySet_New(tup);
1031 Py_DECREF(tup);
1032 return set;
1033#else
1034 sigset_t mask;
1035 if (sigemptyset(&mask) || sigfillset(&mask)) {
1036 return PyErr_SetFromErrno(PyExc_OSError);
1037 }
1038 return sigset_to_set(mask);
1039#endif
1040}
1041
1042#endif /* #if defined(HAVE_SIGFILLSET) || defined(MS_WINDOWS) */
1043
1044
Ross Lagerwallbc808222011-06-25 12:13:40 +02001045#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
1046static int initialized;
1047static PyStructSequence_Field struct_siginfo_fields[] = {
1048 {"si_signo", "signal number"},
1049 {"si_code", "signal code"},
1050 {"si_errno", "errno associated with this signal"},
1051 {"si_pid", "sending process ID"},
1052 {"si_uid", "real user ID of sending process"},
1053 {"si_status", "exit value or signal"},
1054 {"si_band", "band event for SIGPOLL"},
1055 {0}
1056};
1057
1058PyDoc_STRVAR(struct_siginfo__doc__,
1059"struct_siginfo: Result from sigwaitinfo or sigtimedwait.\n\n\
1060This object may be accessed either as a tuple of\n\
1061(si_signo, si_code, si_errno, si_pid, si_uid, si_status, si_band),\n\
1062or via the attributes si_signo, si_code, and so on.");
1063
1064static PyStructSequence_Desc struct_siginfo_desc = {
1065 "signal.struct_siginfo", /* name */
1066 struct_siginfo__doc__, /* doc */
1067 struct_siginfo_fields, /* fields */
1068 7 /* n_in_sequence */
1069};
1070
1071static PyTypeObject SiginfoType;
1072
1073static PyObject *
1074fill_siginfo(siginfo_t *si)
1075{
1076 PyObject *result = PyStructSequence_New(&SiginfoType);
1077 if (!result)
1078 return NULL;
1079
1080 PyStructSequence_SET_ITEM(result, 0, PyLong_FromLong((long)(si->si_signo)));
1081 PyStructSequence_SET_ITEM(result, 1, PyLong_FromLong((long)(si->si_code)));
1082 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si->si_errno)));
1083 PyStructSequence_SET_ITEM(result, 3, PyLong_FromPid(si->si_pid));
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02001084 PyStructSequence_SET_ITEM(result, 4, _PyLong_FromUid(si->si_uid));
Ross Lagerwallbc808222011-06-25 12:13:40 +02001085 PyStructSequence_SET_ITEM(result, 5,
1086 PyLong_FromLong((long)(si->si_status)));
Zachary Ware6a6967e2016-10-01 00:47:27 -05001087#ifdef HAVE_SIGINFO_T_SI_BAND
Ross Lagerwallbc808222011-06-25 12:13:40 +02001088 PyStructSequence_SET_ITEM(result, 6, PyLong_FromLong(si->si_band));
Zachary Ware6a6967e2016-10-01 00:47:27 -05001089#else
1090 PyStructSequence_SET_ITEM(result, 6, PyLong_FromLong(0L));
1091#endif
Ross Lagerwallbc808222011-06-25 12:13:40 +02001092 if (PyErr_Occurred()) {
1093 Py_DECREF(result);
1094 return NULL;
1095 }
1096
1097 return result;
1098}
1099#endif
1100
1101#ifdef HAVE_SIGWAITINFO
Tal Einatc7027b72015-05-16 14:14:49 +03001102
1103/*[clinic input]
1104signal.sigwaitinfo
1105
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001106 sigset: sigset_t
Tal Einatc7027b72015-05-16 14:14:49 +03001107 /
1108
1109Wait synchronously until one of the signals in *sigset* is delivered.
1110
1111Returns a struct_siginfo containing information about the signal.
1112[clinic start generated code]*/
1113
Ross Lagerwallbc808222011-06-25 12:13:40 +02001114static PyObject *
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001115signal_sigwaitinfo_impl(PyObject *module, sigset_t sigset)
1116/*[clinic end generated code: output=1eb2f1fa236fdbca input=3d1a7e1f27fc664c]*/
Ross Lagerwallbc808222011-06-25 12:13:40 +02001117{
Ross Lagerwallbc808222011-06-25 12:13:40 +02001118 siginfo_t si;
1119 int err;
Victor Stinnera453cd82015-03-20 12:54:28 +01001120 int async_err = 0;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001121
Victor Stinnera453cd82015-03-20 12:54:28 +01001122 do {
1123 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001124 err = sigwaitinfo(&sigset, &si);
Victor Stinnera453cd82015-03-20 12:54:28 +01001125 Py_END_ALLOW_THREADS
1126 } while (err == -1
1127 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Ross Lagerwallbc808222011-06-25 12:13:40 +02001128 if (err == -1)
Victor Stinnera453cd82015-03-20 12:54:28 +01001129 return (!async_err) ? PyErr_SetFromErrno(PyExc_OSError) : NULL;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001130
1131 return fill_siginfo(&si);
1132}
1133
Ross Lagerwallbc808222011-06-25 12:13:40 +02001134#endif /* #ifdef HAVE_SIGWAITINFO */
1135
1136#ifdef HAVE_SIGTIMEDWAIT
Tal Einatc7027b72015-05-16 14:14:49 +03001137
1138/*[clinic input]
1139signal.sigtimedwait
1140
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001141 sigset: sigset_t
Serhiy Storchaka6b680cd2015-05-16 15:57:56 +03001142 timeout as timeout_obj: object
Tal Einatc7027b72015-05-16 14:14:49 +03001143 /
1144
1145Like sigwaitinfo(), but with a timeout.
1146
1147The timeout is specified in seconds, with floating point numbers allowed.
1148[clinic start generated code]*/
1149
Ross Lagerwallbc808222011-06-25 12:13:40 +02001150static PyObject *
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001151signal_sigtimedwait_impl(PyObject *module, sigset_t sigset,
Serhiy Storchaka6b680cd2015-05-16 15:57:56 +03001152 PyObject *timeout_obj)
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001153/*[clinic end generated code: output=59c8971e8ae18a64 input=87fd39237cf0b7ba]*/
Ross Lagerwallbc808222011-06-25 12:13:40 +02001154{
Victor Stinnera453cd82015-03-20 12:54:28 +01001155 struct timespec ts;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001156 siginfo_t si;
Victor Stinnera453cd82015-03-20 12:54:28 +01001157 int res;
Victor Stinner34dc0f42015-03-27 18:19:03 +01001158 _PyTime_t timeout, deadline, monotonic;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001159
Victor Stinner869e1772015-03-30 03:49:14 +02001160 if (_PyTime_FromSecondsObject(&timeout,
1161 timeout_obj, _PyTime_ROUND_CEILING) < 0)
Ross Lagerwallbc808222011-06-25 12:13:40 +02001162 return NULL;
1163
Victor Stinnera453cd82015-03-20 12:54:28 +01001164 if (timeout < 0) {
Ross Lagerwallbc808222011-06-25 12:13:40 +02001165 PyErr_SetString(PyExc_ValueError, "timeout must be non-negative");
1166 return NULL;
1167 }
1168
Victor Stinner34dc0f42015-03-27 18:19:03 +01001169 deadline = _PyTime_GetMonotonicClock() + timeout;
Victor Stinnera453cd82015-03-20 12:54:28 +01001170
1171 do {
Victor Stinner34dc0f42015-03-27 18:19:03 +01001172 if (_PyTime_AsTimespec(timeout, &ts) < 0)
1173 return NULL;
Victor Stinnera453cd82015-03-20 12:54:28 +01001174
1175 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001176 res = sigtimedwait(&sigset, &si, &ts);
Victor Stinnera453cd82015-03-20 12:54:28 +01001177 Py_END_ALLOW_THREADS
1178
1179 if (res != -1)
1180 break;
1181
1182 if (errno != EINTR) {
1183 if (errno == EAGAIN)
1184 Py_RETURN_NONE;
1185 else
1186 return PyErr_SetFromErrno(PyExc_OSError);
1187 }
1188
1189 /* sigtimedwait() was interrupted by a signal (EINTR) */
1190 if (PyErr_CheckSignals())
1191 return NULL;
1192
Victor Stinner34dc0f42015-03-27 18:19:03 +01001193 monotonic = _PyTime_GetMonotonicClock();
1194 timeout = deadline - monotonic;
Victor Stinner6aa446c2015-03-30 21:33:51 +02001195 if (timeout < 0)
Victor Stinnera453cd82015-03-20 12:54:28 +01001196 break;
1197 } while (1);
Ross Lagerwallbc808222011-06-25 12:13:40 +02001198
1199 return fill_siginfo(&si);
1200}
1201
Ross Lagerwallbc808222011-06-25 12:13:40 +02001202#endif /* #ifdef HAVE_SIGTIMEDWAIT */
1203
Victor Stinnerb3e72192011-05-08 01:46:11 +02001204
Antoine Pitroua6a4dc82017-09-07 18:56:24 +02001205#if defined(HAVE_PTHREAD_KILL)
Tal Einatc7027b72015-05-16 14:14:49 +03001206
1207/*[clinic input]
1208signal.pthread_kill
1209
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +02001210 thread_id: unsigned_long(bitwise=True)
Tal Einatc7027b72015-05-16 14:14:49 +03001211 signalnum: int
1212 /
1213
1214Send a signal to a thread.
1215[clinic start generated code]*/
1216
Victor Stinnerb3e72192011-05-08 01:46:11 +02001217static PyObject *
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +02001218signal_pthread_kill_impl(PyObject *module, unsigned long thread_id,
1219 int signalnum)
1220/*[clinic end generated code: output=7629919b791bc27f input=1d901f2c7bb544ff]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +02001221{
Victor Stinnerb3e72192011-05-08 01:46:11 +02001222 int err;
1223
Tal Einatc7027b72015-05-16 14:14:49 +03001224 err = pthread_kill((pthread_t)thread_id, signalnum);
Victor Stinnerb3e72192011-05-08 01:46:11 +02001225 if (err != 0) {
1226 errno = err;
1227 PyErr_SetFromErrno(PyExc_OSError);
1228 return NULL;
1229 }
1230
1231 /* the signal may have been send to the current thread */
1232 if (PyErr_CheckSignals())
1233 return NULL;
1234
1235 Py_RETURN_NONE;
1236}
1237
Antoine Pitroua6a4dc82017-09-07 18:56:24 +02001238#endif /* #if defined(HAVE_PTHREAD_KILL) */
Victor Stinnerb3e72192011-05-08 01:46:11 +02001239
1240
1241
Tal Einatc7027b72015-05-16 14:14:49 +03001242/* List of functions defined in the module -- some of the methoddefs are
1243 defined to nothing if the corresponding C function is not available. */
Barry Warsaw92971171997-01-03 00:14:25 +00001244static PyMethodDef signal_methods[] = {
Tal Einatc7027b72015-05-16 14:14:49 +03001245 {"default_int_handler", signal_default_int_handler, METH_VARARGS, default_int_handler_doc},
1246 SIGNAL_ALARM_METHODDEF
1247 SIGNAL_SETITIMER_METHODDEF
1248 SIGNAL_GETITIMER_METHODDEF
1249 SIGNAL_SIGNAL_METHODDEF
Vladimir Matveevc24c6c22019-01-08 01:58:25 -08001250 SIGNAL_RAISE_SIGNAL_METHODDEF
Antoine Pietri5d2a27d2018-03-12 14:42:34 +01001251 SIGNAL_STRSIGNAL_METHODDEF
Tal Einatc7027b72015-05-16 14:14:49 +03001252 SIGNAL_GETSIGNAL_METHODDEF
Serhiy Storchaka62be7422018-11-27 13:27:31 +02001253 {"set_wakeup_fd", (PyCFunction)(void(*)(void))signal_set_wakeup_fd, METH_VARARGS | METH_KEYWORDS, set_wakeup_fd_doc},
Tal Einatc7027b72015-05-16 14:14:49 +03001254 SIGNAL_SIGINTERRUPT_METHODDEF
1255 SIGNAL_PAUSE_METHODDEF
1256 SIGNAL_PTHREAD_KILL_METHODDEF
1257 SIGNAL_PTHREAD_SIGMASK_METHODDEF
1258 SIGNAL_SIGPENDING_METHODDEF
1259 SIGNAL_SIGWAIT_METHODDEF
1260 SIGNAL_SIGWAITINFO_METHODDEF
1261 SIGNAL_SIGTIMEDWAIT_METHODDEF
Antoine Pitrou9d3627e2018-05-04 13:00:50 +02001262#if defined(HAVE_SIGFILLSET) || defined(MS_WINDOWS)
1263 SIGNAL_VALID_SIGNALS_METHODDEF
1264#endif
Tal Einatc7027b72015-05-16 14:14:49 +03001265 {NULL, NULL} /* sentinel */
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001266};
1267
Barry Warsaw92971171997-01-03 00:14:25 +00001268
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001269PyDoc_STRVAR(module_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001270"This module provides mechanisms to use signal handlers in Python.\n\
1271\n\
1272Functions:\n\
1273\n\
1274alarm() -- cause SIGALRM after a specified time [Unix only]\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001275setitimer() -- cause a signal (described below) after a specified\n\
1276 float time and the timer may restart then [Unix only]\n\
1277getitimer() -- get current value of timer [Unix only]\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001278signal() -- set the action for a given signal\n\
1279getsignal() -- get the signal action for a given signal\n\
1280pause() -- wait until a signal arrives [Unix only]\n\
1281default_int_handler() -- default SIGINT handler\n\
1282\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001283signal constants:\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001284SIG_DFL -- used to refer to the system default handler\n\
1285SIG_IGN -- used to ignore the signal\n\
1286NSIG -- number of defined signals\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001287SIGINT, SIGTERM, etc. -- signal numbers\n\
1288\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001289itimer constants:\n\
1290ITIMER_REAL -- decrements in real time, and delivers SIGALRM upon\n\
1291 expiration\n\
1292ITIMER_VIRTUAL -- decrements only when the process is executing,\n\
1293 and delivers SIGVTALRM upon expiration\n\
1294ITIMER_PROF -- decrements both when the process is executing and\n\
1295 when the system is executing on behalf of the process.\n\
1296 Coupled with ITIMER_VIRTUAL, this timer is usually\n\
1297 used to profile the time spent by the application\n\
1298 in user and kernel space. SIGPROF is delivered upon\n\
1299 expiration.\n\
1300\n\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001301*** IMPORTANT NOTICE ***\n\
1302A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001303the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001304
Martin v. Löwis1a214512008-06-11 05:26:20 +00001305static struct PyModuleDef signalmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001306 PyModuleDef_HEAD_INIT,
Brett Cannon815a6f32014-04-04 10:20:28 -04001307 "_signal",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001308 module_doc,
1309 -1,
1310 signal_methods,
1311 NULL,
1312 NULL,
1313 NULL,
1314 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001315};
1316
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001317PyMODINIT_FUNC
Giampaolo Rodola'e09fb712014-04-04 15:34:17 +02001318PyInit__signal(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001319{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001320 PyObject *m, *d, *x;
1321 int i;
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001322
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001323 main_thread = PyThread_get_thread_ident();
1324 main_pid = getpid();
Eric Snow64d6cc82019-02-23 15:40:43 -07001325 main_interp = _PyInterpreterState_Get();
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001326
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001327 /* Create the module and add the functions */
1328 m = PyModule_Create(&signalmodule);
1329 if (m == NULL)
1330 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001331
Ross Lagerwallbc808222011-06-25 12:13:40 +02001332#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
Victor Stinner1c8f0592013-07-22 22:24:54 +02001333 if (!initialized) {
1334 if (PyStructSequence_InitType2(&SiginfoType, &struct_siginfo_desc) < 0)
1335 return NULL;
1336 }
Ross Lagerwallbc808222011-06-25 12:13:40 +02001337 Py_INCREF((PyObject*) &SiginfoType);
1338 PyModule_AddObject(m, "struct_siginfo", (PyObject*) &SiginfoType);
1339 initialized = 1;
1340#endif
1341
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001342 /* Add some symbolic constants to the module */
1343 d = PyModule_GetDict(m);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001344
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001345 x = DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL);
1346 if (!x || PyDict_SetItemString(d, "SIG_DFL", x) < 0)
1347 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001348
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001349 x = IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN);
1350 if (!x || PyDict_SetItemString(d, "SIG_IGN", x) < 0)
1351 goto finally;
Barry Warsaw92971171997-01-03 00:14:25 +00001352
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001353 x = PyLong_FromLong((long)NSIG);
1354 if (!x || PyDict_SetItemString(d, "NSIG", x) < 0)
1355 goto finally;
1356 Py_DECREF(x);
Barry Warsaw92971171997-01-03 00:14:25 +00001357
Victor Stinnera9293352011-04-30 15:21:58 +02001358#ifdef SIG_BLOCK
Victor Stinner72c53b52011-05-02 16:15:43 +02001359 if (PyModule_AddIntMacro(m, SIG_BLOCK))
1360 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001361#endif
Victor Stinnera9293352011-04-30 15:21:58 +02001362#ifdef SIG_UNBLOCK
Victor Stinner72c53b52011-05-02 16:15:43 +02001363 if (PyModule_AddIntMacro(m, SIG_UNBLOCK))
1364 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001365#endif
Victor Stinnera9293352011-04-30 15:21:58 +02001366#ifdef SIG_SETMASK
Victor Stinner72c53b52011-05-02 16:15:43 +02001367 if (PyModule_AddIntMacro(m, SIG_SETMASK))
1368 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001369#endif
1370
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001371 x = IntHandler = PyDict_GetItemString(d, "default_int_handler");
1372 if (!x)
1373 goto finally;
1374 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +00001375
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001376 _Py_atomic_store_relaxed(&Handlers[0].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001377 for (i = 1; i < NSIG; i++) {
1378 void (*t)(int);
1379 t = PyOS_getsig(i);
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001380 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001381 if (t == SIG_DFL)
1382 Handlers[i].func = DefaultHandler;
1383 else if (t == SIG_IGN)
1384 Handlers[i].func = IgnoreHandler;
1385 else
1386 Handlers[i].func = Py_None; /* None of our business */
1387 Py_INCREF(Handlers[i].func);
1388 }
1389 if (Handlers[SIGINT].func == DefaultHandler) {
1390 /* Install default int handler */
1391 Py_INCREF(IntHandler);
Serhiy Storchaka57a01d32016-04-10 18:05:40 +03001392 Py_SETREF(Handlers[SIGINT].func, IntHandler);
pkerlinge905c842018-06-01 09:47:18 +00001393 PyOS_setsig(SIGINT, signal_handler);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001394 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001395
1396#ifdef SIGHUP
Christian Heimes6782b142016-09-09 00:11:45 +02001397 if (PyModule_AddIntMacro(m, SIGHUP))
1398 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001399#endif
1400#ifdef SIGINT
Christian Heimes6782b142016-09-09 00:11:45 +02001401 if (PyModule_AddIntMacro(m, SIGINT))
1402 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001403#endif
Tim Peters1ce3cf72001-10-01 17:58:40 +00001404#ifdef SIGBREAK
Christian Heimes6782b142016-09-09 00:11:45 +02001405 if (PyModule_AddIntMacro(m, SIGBREAK))
1406 goto finally;
Tim Peters1ce3cf72001-10-01 17:58:40 +00001407#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001408#ifdef SIGQUIT
Christian Heimes6782b142016-09-09 00:11:45 +02001409 if (PyModule_AddIntMacro(m, SIGQUIT))
1410 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001411#endif
1412#ifdef SIGILL
Christian Heimes6782b142016-09-09 00:11:45 +02001413 if (PyModule_AddIntMacro(m, SIGILL))
1414 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001415#endif
1416#ifdef SIGTRAP
Christian Heimes6782b142016-09-09 00:11:45 +02001417 if (PyModule_AddIntMacro(m, SIGTRAP))
1418 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001419#endif
1420#ifdef SIGIOT
Christian Heimes6782b142016-09-09 00:11:45 +02001421 if (PyModule_AddIntMacro(m, SIGIOT))
1422 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001423#endif
1424#ifdef SIGABRT
Christian Heimes6782b142016-09-09 00:11:45 +02001425 if (PyModule_AddIntMacro(m, SIGABRT))
1426 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001427#endif
1428#ifdef SIGEMT
Christian Heimes6782b142016-09-09 00:11:45 +02001429 if (PyModule_AddIntMacro(m, SIGEMT))
1430 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001431#endif
1432#ifdef SIGFPE
Christian Heimes6782b142016-09-09 00:11:45 +02001433 if (PyModule_AddIntMacro(m, SIGFPE))
1434 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001435#endif
1436#ifdef SIGKILL
Christian Heimes6782b142016-09-09 00:11:45 +02001437 if (PyModule_AddIntMacro(m, SIGKILL))
1438 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001439#endif
1440#ifdef SIGBUS
Christian Heimes6782b142016-09-09 00:11:45 +02001441 if (PyModule_AddIntMacro(m, SIGBUS))
1442 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001443#endif
1444#ifdef SIGSEGV
Christian Heimes6782b142016-09-09 00:11:45 +02001445 if (PyModule_AddIntMacro(m, SIGSEGV))
1446 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001447#endif
1448#ifdef SIGSYS
Christian Heimes6782b142016-09-09 00:11:45 +02001449 if (PyModule_AddIntMacro(m, SIGSYS))
1450 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001451#endif
1452#ifdef SIGPIPE
Christian Heimes6782b142016-09-09 00:11:45 +02001453 if (PyModule_AddIntMacro(m, SIGPIPE))
1454 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001455#endif
1456#ifdef SIGALRM
Christian Heimes6782b142016-09-09 00:11:45 +02001457 if (PyModule_AddIntMacro(m, SIGALRM))
1458 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001459#endif
1460#ifdef SIGTERM
Christian Heimes6782b142016-09-09 00:11:45 +02001461 if (PyModule_AddIntMacro(m, SIGTERM))
1462 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001463#endif
1464#ifdef SIGUSR1
Christian Heimes6782b142016-09-09 00:11:45 +02001465 if (PyModule_AddIntMacro(m, SIGUSR1))
1466 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001467#endif
1468#ifdef SIGUSR2
Christian Heimes6782b142016-09-09 00:11:45 +02001469 if (PyModule_AddIntMacro(m, SIGUSR2))
1470 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001471#endif
1472#ifdef SIGCLD
Christian Heimes6782b142016-09-09 00:11:45 +02001473 if (PyModule_AddIntMacro(m, SIGCLD))
1474 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001475#endif
1476#ifdef SIGCHLD
Christian Heimes6782b142016-09-09 00:11:45 +02001477 if (PyModule_AddIntMacro(m, SIGCHLD))
1478 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001479#endif
1480#ifdef SIGPWR
Christian Heimes6782b142016-09-09 00:11:45 +02001481 if (PyModule_AddIntMacro(m, SIGPWR))
1482 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001483#endif
1484#ifdef SIGIO
Christian Heimes6782b142016-09-09 00:11:45 +02001485 if (PyModule_AddIntMacro(m, SIGIO))
1486 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001487#endif
1488#ifdef SIGURG
Christian Heimes6782b142016-09-09 00:11:45 +02001489 if (PyModule_AddIntMacro(m, SIGURG))
1490 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001491#endif
1492#ifdef SIGWINCH
Christian Heimes6782b142016-09-09 00:11:45 +02001493 if (PyModule_AddIntMacro(m, SIGWINCH))
1494 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001495#endif
1496#ifdef SIGPOLL
Christian Heimes6782b142016-09-09 00:11:45 +02001497 if (PyModule_AddIntMacro(m, SIGPOLL))
1498 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001499#endif
1500#ifdef SIGSTOP
Christian Heimes6782b142016-09-09 00:11:45 +02001501 if (PyModule_AddIntMacro(m, SIGSTOP))
1502 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001503#endif
1504#ifdef SIGTSTP
Christian Heimes6782b142016-09-09 00:11:45 +02001505 if (PyModule_AddIntMacro(m, SIGTSTP))
1506 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001507#endif
1508#ifdef SIGCONT
Christian Heimes6782b142016-09-09 00:11:45 +02001509 if (PyModule_AddIntMacro(m, SIGCONT))
1510 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001511#endif
1512#ifdef SIGTTIN
Christian Heimes6782b142016-09-09 00:11:45 +02001513 if (PyModule_AddIntMacro(m, SIGTTIN))
1514 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001515#endif
1516#ifdef SIGTTOU
Christian Heimes6782b142016-09-09 00:11:45 +02001517 if (PyModule_AddIntMacro(m, SIGTTOU))
1518 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001519#endif
1520#ifdef SIGVTALRM
Christian Heimes6782b142016-09-09 00:11:45 +02001521 if (PyModule_AddIntMacro(m, SIGVTALRM))
1522 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001523#endif
1524#ifdef SIGPROF
Christian Heimes6782b142016-09-09 00:11:45 +02001525 if (PyModule_AddIntMacro(m, SIGPROF))
1526 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001527#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001528#ifdef SIGXCPU
Christian Heimes6782b142016-09-09 00:11:45 +02001529 if (PyModule_AddIntMacro(m, SIGXCPU))
1530 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001531#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001532#ifdef SIGXFSZ
Christian Heimes6782b142016-09-09 00:11:45 +02001533 if (PyModule_AddIntMacro(m, SIGXFSZ))
1534 goto finally;
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001535#endif
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001536#ifdef SIGRTMIN
Christian Heimes6782b142016-09-09 00:11:45 +02001537 if (PyModule_AddIntMacro(m, SIGRTMIN))
1538 goto finally;
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001539#endif
1540#ifdef SIGRTMAX
Christian Heimes6782b142016-09-09 00:11:45 +02001541 if (PyModule_AddIntMacro(m, SIGRTMAX))
1542 goto finally;
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001543#endif
Martin v. Löwis175af252002-01-12 11:43:25 +00001544#ifdef SIGINFO
Christian Heimes6782b142016-09-09 00:11:45 +02001545 if (PyModule_AddIntMacro(m, SIGINFO))
1546 goto finally;
Martin v. Löwis175af252002-01-12 11:43:25 +00001547#endif
Martin v. Löwis823725e2008-03-24 13:39:54 +00001548
1549#ifdef ITIMER_REAL
Christian Heimes6782b142016-09-09 00:11:45 +02001550 if (PyModule_AddIntMacro(m, ITIMER_REAL))
1551 goto finally;
Martin v. Löwis823725e2008-03-24 13:39:54 +00001552#endif
1553#ifdef ITIMER_VIRTUAL
Christian Heimes6782b142016-09-09 00:11:45 +02001554 if (PyModule_AddIntMacro(m, ITIMER_VIRTUAL))
1555 goto finally;
Martin v. Löwis823725e2008-03-24 13:39:54 +00001556#endif
1557#ifdef ITIMER_PROF
Christian Heimes6782b142016-09-09 00:11:45 +02001558 if (PyModule_AddIntMacro(m, ITIMER_PROF))
1559 goto finally;
Martin v. Löwis823725e2008-03-24 13:39:54 +00001560#endif
1561
1562#if defined (HAVE_SETITIMER) || defined (HAVE_GETITIMER)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001563 ItimerError = PyErr_NewException("signal.ItimerError",
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +03001564 PyExc_OSError, NULL);
Neal Norwitz2f99b242008-08-24 05:48:10 +00001565 if (ItimerError != NULL)
Martin Panter6d57fe12016-09-17 03:26:16 +00001566 PyDict_SetItemString(d, "ItimerError", ItimerError);
Martin v. Löwis823725e2008-03-24 13:39:54 +00001567#endif
1568
Brian Curtineb24d742010-04-12 17:16:38 +00001569#ifdef CTRL_C_EVENT
Christian Heimes6782b142016-09-09 00:11:45 +02001570 if (PyModule_AddIntMacro(m, CTRL_C_EVENT))
1571 goto finally;
Brian Curtineb24d742010-04-12 17:16:38 +00001572#endif
1573
1574#ifdef CTRL_BREAK_EVENT
Christian Heimes6782b142016-09-09 00:11:45 +02001575 if (PyModule_AddIntMacro(m, CTRL_BREAK_EVENT))
1576 goto finally;
Brian Curtineb24d742010-04-12 17:16:38 +00001577#endif
1578
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001579#ifdef MS_WINDOWS
1580 /* Create manual-reset event, initially unset */
1581 sigint_event = CreateEvent(NULL, TRUE, FALSE, FALSE);
1582#endif
1583
Martin v. Löwis1a214512008-06-11 05:26:20 +00001584 if (PyErr_Occurred()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001585 Py_DECREF(m);
1586 m = NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001587 }
Barry Warsaw92971171997-01-03 00:14:25 +00001588
Barry Warsaw92971171997-01-03 00:14:25 +00001589 finally:
Martin v. Löwis1a214512008-06-11 05:26:20 +00001590 return m;
Guido van Rossum08c16611997-08-02 03:01:42 +00001591}
1592
1593static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001594finisignal(void)
Guido van Rossum08c16611997-08-02 03:01:42 +00001595{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001596 int i;
1597 PyObject *func;
Guido van Rossum08c16611997-08-02 03:01:42 +00001598
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001599 for (i = 1; i < NSIG; i++) {
1600 func = Handlers[i].func;
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001601 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001602 Handlers[i].func = NULL;
pkerlinge905c842018-06-01 09:47:18 +00001603 if (func != NULL && func != Py_None &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001604 func != DefaultHandler && func != IgnoreHandler)
1605 PyOS_setsig(i, SIG_DFL);
1606 Py_XDECREF(func);
1607 }
Guido van Rossum08c16611997-08-02 03:01:42 +00001608
Serhiy Storchaka505ff752014-02-09 13:33:53 +02001609 Py_CLEAR(IntHandler);
1610 Py_CLEAR(DefaultHandler);
1611 Py_CLEAR(IgnoreHandler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001612}
1613
Barry Warsaw92971171997-01-03 00:14:25 +00001614
Barry Warsaw92971171997-01-03 00:14:25 +00001615/* Declared in pyerrors.h */
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001616int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001617PyErr_CheckSignals(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001618{
Eric Snow64d6cc82019-02-23 15:40:43 -07001619 if (!is_main()) {
1620 return 0;
1621 }
1622
1623 return _PyErr_CheckSignals();
1624}
1625
1626
1627/* Declared in cpython/pyerrors.h */
1628int
1629_PyErr_CheckSignals(void)
1630{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001631 int i;
1632 PyObject *f;
Barry Warsaw92971171997-01-03 00:14:25 +00001633
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001634 if (!_Py_atomic_load(&is_tripped))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001635 return 0;
Christian Heimesb76922a2007-12-11 01:06:40 +00001636
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001637 /*
1638 * The is_tripped variable is meant to speed up the calls to
1639 * PyErr_CheckSignals (both directly or via pending calls) when no
1640 * signal has arrived. This variable is set to 1 when a signal arrives
1641 * and it is set to 0 here, when we know some signals arrived. This way
1642 * we can run the registered handlers with no signals blocked.
1643 *
1644 * NOTE: with this approach we can have a situation where is_tripped is
1645 * 1 but we have no more signals to handle (Handlers[i].tripped
1646 * is 0 for every signal i). This won't do us any harm (except
1647 * we're gonna spent some cycles for nothing). This happens when
1648 * we receive a signal i after we zero is_tripped and before we
1649 * check Handlers[i].tripped.
1650 */
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001651 _Py_atomic_store(&is_tripped, 0);
Christian Heimesb76922a2007-12-11 01:06:40 +00001652
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001653 if (!(f = (PyObject *)PyEval_GetFrame()))
1654 f = Py_None;
Christian Heimesb76922a2007-12-11 01:06:40 +00001655
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001656 for (i = 1; i < NSIG; i++) {
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001657 if (_Py_atomic_load_relaxed(&Handlers[i].tripped)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001658 PyObject *result = NULL;
1659 PyObject *arglist = Py_BuildValue("(iO)", i, f);
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001660 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Barry Warsaw92971171997-01-03 00:14:25 +00001661
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001662 if (arglist) {
1663 result = PyEval_CallObject(Handlers[i].func,
1664 arglist);
1665 Py_DECREF(arglist);
1666 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +02001667 if (!result) {
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001668 _Py_atomic_store(&is_tripped, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001669 return -1;
Antoine Pitrouc08177a2017-06-28 23:29:29 +02001670 }
Barry Warsaw92971171997-01-03 00:14:25 +00001671
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001672 Py_DECREF(result);
1673 }
1674 }
Christian Heimesb76922a2007-12-11 01:06:40 +00001675
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001676 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001677}
1678
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +00001679
Barry Warsaw92971171997-01-03 00:14:25 +00001680/* Replacements for intrcheck.c functionality
1681 * Declared in pyerrors.h
1682 */
1683void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001684PyErr_SetInterrupt(void)
Barry Warsaw92971171997-01-03 00:14:25 +00001685{
Victor Stinner6c9b35b2011-04-18 16:25:56 +02001686 trip_signal(SIGINT);
Barry Warsaw92971171997-01-03 00:14:25 +00001687}
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001688
1689void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001690PyOS_InitInterrupts(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001691{
Giampaolo Rodola'e09fb712014-04-04 15:34:17 +02001692 PyObject *m = PyImport_ImportModule("_signal");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001693 if (m) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001694 Py_DECREF(m);
1695 }
Guido van Rossum08c16611997-08-02 03:01:42 +00001696}
1697
1698void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001699PyOS_FiniInterrupts(void)
Guido van Rossum08c16611997-08-02 03:01:42 +00001700{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001701 finisignal();
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001702}
1703
1704int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001705PyOS_InterruptOccurred(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001706{
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001707 if (_Py_atomic_load_relaxed(&Handlers[SIGINT].tripped)) {
Eric Snow64d6cc82019-02-23 15:40:43 -07001708 if (!is_main()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001709 return 0;
Eric Snow64d6cc82019-02-23 15:40:43 -07001710 }
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001711 _Py_atomic_store_relaxed(&Handlers[SIGINT].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001712 return 1;
1713 }
1714 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001715}
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001716
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001717static void
1718_clear_pending_signals(void)
1719{
1720 int i;
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001721 if (!_Py_atomic_load(&is_tripped))
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001722 return;
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001723 _Py_atomic_store(&is_tripped, 0);
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001724 for (i = 1; i < NSIG; ++i) {
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001725 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001726 }
1727}
1728
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001729void
Antoine Pitrou346cbd32017-05-27 17:50:54 +02001730_PySignal_AfterFork(void)
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001731{
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001732 /* Clear the signal flags after forking so that they aren't handled
1733 * in both processes if they came in just before the fork() but before
1734 * the interpreter had an opportunity to call the handlers. issue9535. */
1735 _clear_pending_signals();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001736 main_thread = PyThread_get_thread_ident();
1737 main_pid = getpid();
Eric Snow64d6cc82019-02-23 15:40:43 -07001738 main_interp = _PyInterpreterState_Get();
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001739}
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001740
1741int
1742_PyOS_IsMainThread(void)
1743{
Eric Snow64d6cc82019-02-23 15:40:43 -07001744 return is_main();
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001745}
1746
1747#ifdef MS_WINDOWS
1748void *_PyOS_SigintEvent(void)
1749{
1750 /* Returns a manual-reset event which gets tripped whenever
1751 SIGINT is received.
1752
1753 Python.h does not include windows.h so we do cannot use HANDLE
1754 as the return type of this function. We use void* instead. */
1755 return sigint_event;
1756}
1757#endif