blob: 7bc1b535e6e2caae8f382e30e84f55f5d610c552 [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 Stinner72818982020-03-26 22:28:11 +01008#include "pycore_call.h"
Victor Stinner09532fe2019-05-10 23:39:09 +02009#include "pycore_ceval.h"
Victor Stinner72818982020-03-26 22:28:11 +010010#include "pycore_pyerrors.h"
Victor Stinnere5014be2020-04-14 17:52:15 +020011#include "pycore_pystate.h" // _PyThreadState_GET()
Victor Stinner31368a42018-10-30 15:14:25 +010012
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020013#ifndef MS_WINDOWS
14#include "posixmodule.h"
15#endif
Victor Stinner11517102014-07-29 23:31:34 +020016#ifdef MS_WINDOWS
17#include "socketmodule.h" /* needed for SOCKET_T */
18#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +000019
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000020#ifdef MS_WINDOWS
Antoine Pitrou7faf7052013-03-31 22:48:04 +020021#include <windows.h>
Benjamin Peterson2614cda2010-03-21 22:36:19 +000022#ifdef HAVE_PROCESS_H
Guido van Rossum644a12b1997-04-09 19:24:53 +000023#include <process.h>
24#endif
Benjamin Peterson2614cda2010-03-21 22:36:19 +000025#endif
Guido van Rossum644a12b1997-04-09 19:24:53 +000026
Benjamin Peterson2614cda2010-03-21 22:36:19 +000027#ifdef HAVE_SIGNAL_H
Guido van Rossum398d9fe1994-05-11 08:59:13 +000028#include <signal.h>
Benjamin Peterson2614cda2010-03-21 22:36:19 +000029#endif
Benjamin Peterson74834512019-11-19 20:39:14 -080030#ifdef HAVE_SYS_SYSCALL_H
31#include <sys/syscall.h>
32#endif
Benjamin Peterson2614cda2010-03-21 22:36:19 +000033#ifdef HAVE_SYS_STAT_H
Christian Heimes5fb7c2a2007-12-24 08:52:31 +000034#include <sys/stat.h>
Benjamin Peterson2614cda2010-03-21 22:36:19 +000035#endif
Martin v. Löwis3bf3cc02008-03-24 14:05:07 +000036#ifdef HAVE_SYS_TIME_H
Martin v. Löwis823725e2008-03-24 13:39:54 +000037#include <sys/time.h>
Martin v. Löwis3bf3cc02008-03-24 14:05:07 +000038#endif
Christian Heimes5fb7c2a2007-12-24 08:52:31 +000039
Victor Stinnera9293352011-04-30 15:21:58 +020040#if defined(HAVE_PTHREAD_SIGMASK) && !defined(HAVE_BROKEN_PTHREAD_SIGMASK)
41# define PYPTHREAD_SIGMASK
42#endif
43
44#if defined(PYPTHREAD_SIGMASK) && defined(HAVE_PTHREAD_H)
45# include <pthread.h>
46#endif
47
Guido van Rossumbb4ba121994-06-23 11:25:45 +000048#ifndef SIG_ERR
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +000049#define SIG_ERR ((PyOS_sighandler_t)(-1))
Guido van Rossumbb4ba121994-06-23 11:25:45 +000050#endif
51
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000052#ifndef NSIG
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000053# if defined(_NSIG)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000054# define NSIG _NSIG /* For BSD/SysV */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000055# elif defined(_SIGMAX)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000056# define NSIG (_SIGMAX + 1) /* For QNX */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000057# elif defined(SIGMAX)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000058# define NSIG (SIGMAX + 1) /* For djgpp */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000059# else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000060# define NSIG 64 /* Use a reasonable default value */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000061# endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000062#endif
63
Tal Einatc7027b72015-05-16 14:14:49 +030064#include "clinic/signalmodule.c.h"
65
66/*[clinic input]
67module signal
68[clinic start generated code]*/
69/*[clinic end generated code: output=da39a3ee5e6b4b0d input=b0301a3bde5fe9d3]*/
70
Serhiy Storchakad54cfb12018-05-08 07:48:50 +030071/*[python input]
72
73class sigset_t_converter(CConverter):
74 type = 'sigset_t'
75 converter = '_Py_Sigset_Converter'
76
77[python start generated code]*/
78/*[python end generated code: output=da39a3ee5e6b4b0d input=b5689d14466b6823]*/
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000079
Guido van Rossumbb4ba121994-06-23 11:25:45 +000080/*
81 NOTES ON THE INTERACTION BETWEEN SIGNALS AND THREADS
82
Jeroen Demeyerd237b3f2019-05-10 03:28:57 +020083 We want the following semantics:
Guido van Rossumbb4ba121994-06-23 11:25:45 +000084
85 - only the main thread can set a signal handler
Jeroen Demeyerd237b3f2019-05-10 03:28:57 +020086 - only the main thread runs the signal handler
87 - signals can be delivered to any thread
Guido van Rossumbb4ba121994-06-23 11:25:45 +000088 - any thread can get a signal handler
Guido van Rossumbb4ba121994-06-23 11:25:45 +000089
90 I.e. we don't support "synchronous signals" like SIGFPE (catching
91 this doesn't make much sense in Python anyway) nor do we support
92 signals as a means of inter-thread communication, since not all
93 thread implementations support that (at least our thread library
94 doesn't).
95
96 We still have the problem that in some implementations signals
97 generated by the keyboard (e.g. SIGINT) are delivered to all
98 threads (e.g. SGI), while in others (e.g. Solaris) such signals are
Jeroen Demeyerd237b3f2019-05-10 03:28:57 +020099 delivered to one random thread. On Linux, signals are delivered to
100 the main thread (unless the main thread is blocking the signal, for
101 example because it's already handling the same signal). Since we
102 allow signals to be delivered to any thread, this works fine. The
103 only oddity is that the thread executing the Python signal handler
104 may not be the thread that received the signal.
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000105*/
106
Victor Stinner2ec6b172011-05-15 10:21:59 +0200107static volatile struct {
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +0200108 _Py_atomic_int tripped;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000109 PyObject *func;
Barry Warsaw92971171997-01-03 00:14:25 +0000110} Handlers[NSIG];
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000111
Victor Stinner11517102014-07-29 23:31:34 +0200112#ifdef MS_WINDOWS
113#define INVALID_FD ((SOCKET_T)-1)
114
115static volatile struct {
116 SOCKET_T fd;
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800117 int warn_on_full_buffer;
Victor Stinner11517102014-07-29 23:31:34 +0200118 int use_send;
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800119} wakeup = {.fd = INVALID_FD, .warn_on_full_buffer = 1, .use_send = 0};
Victor Stinner11517102014-07-29 23:31:34 +0200120#else
121#define INVALID_FD (-1)
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800122static volatile struct {
123 sig_atomic_t fd;
124 int warn_on_full_buffer;
125} wakeup = {.fd = INVALID_FD, .warn_on_full_buffer = 1};
Victor Stinner11517102014-07-29 23:31:34 +0200126#endif
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000127
Christian Heimesb76922a2007-12-11 01:06:40 +0000128/* Speed up sigcheck() when none tripped */
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +0200129static _Py_atomic_int is_tripped;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000130
Barry Warsaw92971171997-01-03 00:14:25 +0000131static PyObject *DefaultHandler;
132static PyObject *IgnoreHandler;
133static PyObject *IntHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000134
Antoine Pitrou6dd381e2011-11-21 21:26:56 +0100135#ifdef MS_WINDOWS
136static HANDLE sigint_event = NULL;
137#endif
138
Martin v. Löwis823725e2008-03-24 13:39:54 +0000139#ifdef HAVE_GETITIMER
140static PyObject *ItimerError;
141
Victor Stinneref611c92017-10-13 13:49:43 -0700142/* auxiliary functions for setitimer */
143static int
144timeval_from_double(PyObject *obj, struct timeval *tv)
Martin v. Löwis823725e2008-03-24 13:39:54 +0000145{
Victor Stinneref611c92017-10-13 13:49:43 -0700146 if (obj == NULL) {
147 tv->tv_sec = 0;
148 tv->tv_usec = 0;
149 return 0;
Antoine Pitrou729780a2017-06-30 10:01:05 +0200150 }
Victor Stinneref611c92017-10-13 13:49:43 -0700151
152 _PyTime_t t;
153 if (_PyTime_FromSecondsObject(&t, obj, _PyTime_ROUND_CEILING) < 0) {
154 return -1;
155 }
156 return _PyTime_AsTimeval(t, tv, _PyTime_ROUND_CEILING);
Martin v. Löwis823725e2008-03-24 13:39:54 +0000157}
158
Christian Heimes1a8501c2008-10-02 19:56:01 +0000159Py_LOCAL_INLINE(double)
Martin v. Löwis823725e2008-03-24 13:39:54 +0000160double_from_timeval(struct timeval *tv)
161{
162 return tv->tv_sec + (double)(tv->tv_usec / 1000000.0);
163}
164
165static PyObject *
166itimer_retval(struct itimerval *iv)
167{
168 PyObject *r, *v;
169
170 r = PyTuple_New(2);
171 if (r == NULL)
Martin Panter6d57fe12016-09-17 03:26:16 +0000172 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000173
174 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_value)))) {
Martin Panter6d57fe12016-09-17 03:26:16 +0000175 Py_DECREF(r);
176 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000177 }
178
179 PyTuple_SET_ITEM(r, 0, v);
180
181 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_interval)))) {
Martin Panter6d57fe12016-09-17 03:26:16 +0000182 Py_DECREF(r);
183 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000184 }
185
186 PyTuple_SET_ITEM(r, 1, v);
187
188 return r;
189}
190#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000191
Serhiy Storchakab0689ae2020-07-12 19:15:20 +0300192/*[clinic input]
193signal.default_int_handler
194 signalnum: int
195 frame: object
196 /
197
198The default handler for SIGINT installed by Python.
199
200It raises KeyboardInterrupt.
201[clinic start generated code]*/
202
Guido van Rossume4485b01994-09-07 14:32:49 +0000203static PyObject *
Serhiy Storchakab0689ae2020-07-12 19:15:20 +0300204signal_default_int_handler_impl(PyObject *module, int signalnum,
205 PyObject *frame)
206/*[clinic end generated code: output=bb11c2eb115ace4e input=efcd4a56a207acfd]*/
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000207{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000208 PyErr_SetNone(PyExc_KeyboardInterrupt);
209 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000210}
211
Thomas Wouters0796b002000-07-22 23:49:30 +0000212
213static int
Victor Stinner11517102014-07-29 23:31:34 +0200214report_wakeup_write_error(void *data)
Antoine Pitrou6f6ec372013-08-17 20:27:56 +0200215{
Eric Snowfdf282d2019-01-11 14:26:55 -0700216 PyObject *exc, *val, *tb;
Antoine Pitrou6f6ec372013-08-17 20:27:56 +0200217 int save_errno = errno;
Benjamin Petersonca470632016-09-06 13:47:26 -0700218 errno = (int) (intptr_t) data;
Eric Snowfdf282d2019-01-11 14:26:55 -0700219 PyErr_Fetch(&exc, &val, &tb);
Antoine Pitrou6f6ec372013-08-17 20:27:56 +0200220 PyErr_SetFromErrno(PyExc_OSError);
221 PySys_WriteStderr("Exception ignored when trying to write to the "
222 "signal wakeup fd:\n");
223 PyErr_WriteUnraisable(NULL);
Eric Snowfdf282d2019-01-11 14:26:55 -0700224 PyErr_Restore(exc, val, tb);
Antoine Pitrou6f6ec372013-08-17 20:27:56 +0200225 errno = save_errno;
226 return 0;
227}
228
Victor Stinner11517102014-07-29 23:31:34 +0200229#ifdef MS_WINDOWS
230static int
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800231report_wakeup_send_error(void* data)
Victor Stinner11517102014-07-29 23:31:34 +0200232{
Eric Snowfdf282d2019-01-11 14:26:55 -0700233 PyObject *exc, *val, *tb;
234 PyErr_Fetch(&exc, &val, &tb);
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800235 /* PyErr_SetExcFromWindowsErr() invokes FormatMessage() which
236 recognizes the error codes used by both GetLastError() and
237 WSAGetLastError */
238 PyErr_SetExcFromWindowsErr(PyExc_OSError, (int) (intptr_t) data);
Victor Stinner11517102014-07-29 23:31:34 +0200239 PySys_WriteStderr("Exception ignored when trying to send to the "
240 "signal wakeup fd:\n");
241 PyErr_WriteUnraisable(NULL);
Eric Snowfdf282d2019-01-11 14:26:55 -0700242 PyErr_Restore(exc, val, tb);
Victor Stinner11517102014-07-29 23:31:34 +0200243 return 0;
244}
245#endif /* MS_WINDOWS */
246
Tim Peters4f1b2082000-07-23 21:18:09 +0000247static void
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200248trip_signal(int sig_num)
249{
Victor Stinnerd49b1f12011-05-08 02:03:15 +0200250 unsigned char byte;
Victor Stinner11517102014-07-29 23:31:34 +0200251 int fd;
252 Py_ssize_t rc;
Victor Stinnerc13ef662011-05-25 02:35:58 +0200253
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +0200254 _Py_atomic_store_relaxed(&Handlers[sig_num].tripped, 1);
Victor Stinner11517102014-07-29 23:31:34 +0200255
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200256 /* Set is_tripped after setting .tripped, as it gets
257 cleared in PyErr_CheckSignals() before .tripped. */
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +0200258 _Py_atomic_store(&is_tripped, 1);
259
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200260 /* Signals are always handled by the main interpreter */
261 PyInterpreterState *interp = _PyRuntime.interpreters.main;
Victor Stinner8849e592020-03-18 19:28:53 +0100262
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +0200263 /* Notify ceval.c */
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200264 _PyEval_SignalReceived(interp);
Nathaniel J. Smith4ae01492017-05-16 14:12:11 -0700265
266 /* And then write to the wakeup fd *after* setting all the globals and
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200267 doing the _PyEval_SignalReceived. We used to write to the wakeup fd
268 and then set the flag, but this allowed the following sequence of events
269 (especially on windows, where trip_signal may run in a new thread):
Nathaniel J. Smith4ae01492017-05-16 14:12:11 -0700270
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800271 - main thread blocks on select([wakeup.fd], ...)
Nathaniel J. Smith4ae01492017-05-16 14:12:11 -0700272 - signal arrives
273 - trip_signal writes to the wakeup fd
274 - the main thread wakes up
275 - the main thread checks the signal flags, sees that they're unset
276 - the main thread empties the wakeup fd
277 - the main thread goes back to sleep
278 - trip_signal sets the flags to request the Python-level signal handler
279 be run
280 - the main thread doesn't notice, because it's asleep
281
282 See bpo-30038 for more details.
283 */
284
Victor Stinner11517102014-07-29 23:31:34 +0200285#ifdef MS_WINDOWS
286 fd = Py_SAFE_DOWNCAST(wakeup.fd, SOCKET_T, int);
287#else
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800288 fd = wakeup.fd;
Victor Stinner11517102014-07-29 23:31:34 +0200289#endif
290
291 if (fd != INVALID_FD) {
Victor Stinnerc13ef662011-05-25 02:35:58 +0200292 byte = (unsigned char)sig_num;
Victor Stinner11517102014-07-29 23:31:34 +0200293#ifdef MS_WINDOWS
294 if (wakeup.use_send) {
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800295 rc = send(fd, &byte, 1, 0);
Victor Stinner11517102014-07-29 23:31:34 +0200296
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800297 if (rc < 0) {
298 int last_error = GetLastError();
299 if (wakeup.warn_on_full_buffer ||
300 last_error != WSAEWOULDBLOCK)
301 {
Victor Stinner50e6e992020-03-19 02:41:21 +0100302 /* _PyEval_AddPendingCall() isn't signal-safe, but we
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800303 still use it for this exceptional case. */
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200304 _PyEval_AddPendingCall(interp,
Victor Stinner09532fe2019-05-10 23:39:09 +0200305 report_wakeup_send_error,
306 (void *)(intptr_t) last_error);
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800307 }
Victor Stinner11517102014-07-29 23:31:34 +0200308 }
309 }
310 else
311#endif
312 {
Victor Stinnere72fe392015-04-01 18:35:22 +0200313 /* _Py_write_noraise() retries write() if write() is interrupted by
314 a signal (fails with EINTR). */
315 rc = _Py_write_noraise(fd, &byte, 1);
Victor Stinner11517102014-07-29 23:31:34 +0200316
317 if (rc < 0) {
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800318 if (wakeup.warn_on_full_buffer ||
319 (errno != EWOULDBLOCK && errno != EAGAIN))
320 {
Victor Stinner50e6e992020-03-19 02:41:21 +0100321 /* _PyEval_AddPendingCall() isn't signal-safe, but we
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800322 still use it for this exceptional case. */
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200323 _PyEval_AddPendingCall(interp,
Victor Stinner09532fe2019-05-10 23:39:09 +0200324 report_wakeup_write_error,
325 (void *)(intptr_t)errno);
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800326 }
Victor Stinner11517102014-07-29 23:31:34 +0200327 }
328 }
Victor Stinnerc13ef662011-05-25 02:35:58 +0200329 }
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200330}
331
332static void
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000333signal_handler(int sig_num)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000334{
Antoine Pitrou39a65912010-11-05 19:47:27 +0000335 int save_errno = errno;
336
Jeroen Demeyerd237b3f2019-05-10 03:28:57 +0200337 trip_signal(sig_num);
Antoine Pitrou39a65912010-11-05 19:47:27 +0000338
Jean-Paul Calderone6f137ca2010-05-09 03:18:57 +0000339#ifndef HAVE_SIGACTION
Antoine Pitrou39a65912010-11-05 19:47:27 +0000340#ifdef SIGCHLD
341 /* To avoid infinite recursion, this signal remains
342 reset until explicit re-instated.
343 Don't clear the 'func' field as it is our pointer
344 to the Python handler... */
345 if (sig_num != SIGCHLD)
346#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000347 /* If the handler was not set up with sigaction, reinstall it. See
Nick Coghland6009512014-11-20 21:39:37 +1000348 * Python/pylifecycle.c for the implementation of PyOS_setsig which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000349 * makes this true. See also issue8354. */
350 PyOS_setsig(sig_num, signal_handler);
Jean-Paul Calderone6f137ca2010-05-09 03:18:57 +0000351#endif
Antoine Pitrou39a65912010-11-05 19:47:27 +0000352
353 /* Issue #10311: asynchronously executing signal handlers should not
354 mutate errno under the feet of unsuspecting C code. */
355 errno = save_errno;
Antoine Pitrou6dd381e2011-11-21 21:26:56 +0100356
357#ifdef MS_WINDOWS
358 if (sig_num == SIGINT)
359 SetEvent(sigint_event);
360#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000361}
Guido van Rossume4485b01994-09-07 14:32:49 +0000362
Guido van Rossum06d511d1995-03-10 15:13:48 +0000363
Guido van Rossum1171ee61997-08-22 20:42:00 +0000364#ifdef HAVE_ALARM
Tal Einatc7027b72015-05-16 14:14:49 +0300365
366/*[clinic input]
367signal.alarm -> long
368
369 seconds: int
370 /
371
372Arrange for SIGALRM to arrive after the given number of seconds.
373[clinic start generated code]*/
374
375static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300376signal_alarm_impl(PyObject *module, int seconds)
377/*[clinic end generated code: output=144232290814c298 input=0d5e97e0e6f39e86]*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000378{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000379 /* alarm() returns the number of seconds remaining */
Tal Einatc7027b72015-05-16 14:14:49 +0300380 return (long)alarm(seconds);
Guido van Rossumaa0f4c71994-08-23 13:49:37 +0000381}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000382
Guido van Rossum06d511d1995-03-10 15:13:48 +0000383#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000384
Guido van Rossum1171ee61997-08-22 20:42:00 +0000385#ifdef HAVE_PAUSE
Tal Einatc7027b72015-05-16 14:14:49 +0300386
387/*[clinic input]
388signal.pause
389
390Wait until a signal arrives.
391[clinic start generated code]*/
392
Guido van Rossuma597dde1995-01-10 20:56:29 +0000393static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300394signal_pause_impl(PyObject *module)
395/*[clinic end generated code: output=391656788b3c3929 input=f03de0f875752062]*/
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000396{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000397 Py_BEGIN_ALLOW_THREADS
398 (void)pause();
399 Py_END_ALLOW_THREADS
400 /* make sure that any exceptions that got raised are propagated
401 * back into Python
402 */
403 if (PyErr_CheckSignals())
404 return NULL;
Barry Warsaw92971171997-01-03 00:14:25 +0000405
Tal Einatc7027b72015-05-16 14:14:49 +0300406 Py_RETURN_NONE;
Guido van Rossume4485b01994-09-07 14:32:49 +0000407}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000408
Guido van Rossum06d511d1995-03-10 15:13:48 +0000409#endif
Guido van Rossume4485b01994-09-07 14:32:49 +0000410
Vladimir Matveevc24c6c22019-01-08 01:58:25 -0800411/*[clinic input]
412signal.raise_signal
413
414 signalnum: int
415 /
416
417Send a signal to the executing process.
418[clinic start generated code]*/
419
420static PyObject *
421signal_raise_signal_impl(PyObject *module, int signalnum)
422/*[clinic end generated code: output=e2b014220aa6111d input=e90c0f9a42358de6]*/
423{
424 int err;
425 Py_BEGIN_ALLOW_THREADS
426 _Py_BEGIN_SUPPRESS_IPH
427 err = raise(signalnum);
428 _Py_END_SUPPRESS_IPH
429 Py_END_ALLOW_THREADS
Victor Stinner09532fe2019-05-10 23:39:09 +0200430
Vladimir Matveevc24c6c22019-01-08 01:58:25 -0800431 if (err) {
432 return PyErr_SetFromErrno(PyExc_OSError);
433 }
434 Py_RETURN_NONE;
435}
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000436
Tal Einatc7027b72015-05-16 14:14:49 +0300437/*[clinic input]
438signal.signal
439
440 signalnum: int
441 handler: object
442 /
443
444Set the action for the given signal.
445
446The action can be SIG_DFL, SIG_IGN, or a callable Python object.
447The previous action is returned. See getsignal() for possible return values.
448
449*** IMPORTANT NOTICE ***
450A signal handler function is called with two arguments:
451the first is the signal number, the second is the interrupted stack frame.
452[clinic start generated code]*/
453
Guido van Rossume4485b01994-09-07 14:32:49 +0000454static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300455signal_signal_impl(PyObject *module, int signalnum, PyObject *handler)
456/*[clinic end generated code: output=b44cfda43780f3a1 input=deee84af5fa0432c]*/
Guido van Rossume4485b01994-09-07 14:32:49 +0000457{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000458 PyObject *old_handler;
459 void (*func)(int);
Brian Curtinef9efbd2010-08-06 19:27:32 +0000460#ifdef MS_WINDOWS
Tal Einatc7027b72015-05-16 14:14:49 +0300461 /* Validate that signalnum is one of the allowable signals */
462 switch (signalnum) {
Brian Curtinc734b312010-09-06 16:04:10 +0000463 case SIGABRT: break;
Brian Curtin9e88b5a2010-10-01 14:49:24 +0000464#ifdef SIGBREAK
465 /* Issue #10003: SIGBREAK is not documented as permitted, but works
466 and corresponds to CTRL_BREAK_EVENT. */
467 case SIGBREAK: break;
468#endif
Brian Curtinc734b312010-09-06 16:04:10 +0000469 case SIGFPE: break;
470 case SIGILL: break;
471 case SIGINT: break;
472 case SIGSEGV: break;
473 case SIGTERM: break;
474 default:
475 PyErr_SetString(PyExc_ValueError, "invalid signal value");
476 return NULL;
Brian Curtinef9efbd2010-08-06 19:27:32 +0000477 }
478#endif
Victor Stinnerd8613dc2019-05-24 13:43:55 +0200479
Victor Stinner72818982020-03-26 22:28:11 +0100480 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200481 if (!_Py_ThreadCanHandleSignals(tstate->interp)) {
Victor Stinner72818982020-03-26 22:28:11 +0100482 _PyErr_SetString(tstate, PyExc_ValueError,
483 "signal only works in main thread "
484 "of the main interpreter");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000485 return NULL;
486 }
Tal Einatc7027b72015-05-16 14:14:49 +0300487 if (signalnum < 1 || signalnum >= NSIG) {
Victor Stinner72818982020-03-26 22:28:11 +0100488 _PyErr_SetString(tstate, PyExc_ValueError,
489 "signal number out of range");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000490 return NULL;
491 }
Victor Stinner72818982020-03-26 22:28:11 +0100492 if (handler == IgnoreHandler) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000493 func = SIG_IGN;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000494 }
Victor Stinner72818982020-03-26 22:28:11 +0100495 else if (handler == DefaultHandler) {
496 func = SIG_DFL;
497 }
498 else if (!PyCallable_Check(handler)) {
499 _PyErr_SetString(tstate, PyExc_TypeError,
500 "signal handler must be signal.SIG_IGN, "
501 "signal.SIG_DFL, or a callable object");
502 return NULL;
503 }
504 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000505 func = signal_handler;
Victor Stinner72818982020-03-26 22:28:11 +0100506 }
507
Antoine Pitrouf6f90ff2017-11-03 19:58:46 +0100508 /* Check for pending signals before changing signal handler */
Victor Stinner72818982020-03-26 22:28:11 +0100509 if (_PyErr_CheckSignalsTstate(tstate)) {
Antoine Pitrouf6f90ff2017-11-03 19:58:46 +0100510 return NULL;
511 }
Tal Einatc7027b72015-05-16 14:14:49 +0300512 if (PyOS_setsig(signalnum, func) == SIG_ERR) {
Victor Stinner388196e2011-05-10 17:13:00 +0200513 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000514 return NULL;
515 }
Victor Stinner72818982020-03-26 22:28:11 +0100516
Tal Einatc7027b72015-05-16 14:14:49 +0300517 old_handler = Handlers[signalnum].func;
Tal Einatc7027b72015-05-16 14:14:49 +0300518 Py_INCREF(handler);
519 Handlers[signalnum].func = handler;
Victor Stinner72818982020-03-26 22:28:11 +0100520
521 if (old_handler != NULL) {
Antoine Pitrouc8c952c2013-05-04 23:16:59 +0200522 return old_handler;
Victor Stinner72818982020-03-26 22:28:11 +0100523 }
524 else {
Antoine Pitrouc8c952c2013-05-04 23:16:59 +0200525 Py_RETURN_NONE;
Victor Stinner72818982020-03-26 22:28:11 +0100526 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000527}
528
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000529
Tal Einatc7027b72015-05-16 14:14:49 +0300530/*[clinic input]
531signal.getsignal
532
533 signalnum: int
534 /
535
536Return the current action for the given signal.
537
538The return value can be:
539 SIG_IGN -- if the signal is being ignored
540 SIG_DFL -- if the default action for the signal is in effect
541 None -- if an unknown handler is in effect
542 anything else -- the callable Python object used as a handler
543[clinic start generated code]*/
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000544
Guido van Rossume4485b01994-09-07 14:32:49 +0000545static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300546signal_getsignal_impl(PyObject *module, int signalnum)
547/*[clinic end generated code: output=35b3e0e796fd555e input=ac23a00f19dfa509]*/
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000548{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000549 PyObject *old_handler;
Tal Einatc7027b72015-05-16 14:14:49 +0300550 if (signalnum < 1 || signalnum >= NSIG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000551 PyErr_SetString(PyExc_ValueError,
552 "signal number out of range");
553 return NULL;
554 }
Tal Einatc7027b72015-05-16 14:14:49 +0300555 old_handler = Handlers[signalnum].func;
Antoine Pitrouc8c952c2013-05-04 23:16:59 +0200556 if (old_handler != NULL) {
557 Py_INCREF(old_handler);
558 return old_handler;
559 }
560 else {
561 Py_RETURN_NONE;
562 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000563}
564
Antoine Pietri5d2a27d2018-03-12 14:42:34 +0100565
566/*[clinic input]
567signal.strsignal
568
569 signalnum: int
570 /
571
572Return the system description of the given signal.
573
574The return values can be such as "Interrupt", "Segmentation fault", etc.
575Returns None if the signal is not recognized.
576[clinic start generated code]*/
577
578static PyObject *
579signal_strsignal_impl(PyObject *module, int signalnum)
580/*[clinic end generated code: output=44e12e1e3b666261 input=b77914b03f856c74]*/
581{
582 char *res;
583
584 if (signalnum < 1 || signalnum >= NSIG) {
585 PyErr_SetString(PyExc_ValueError,
586 "signal number out of range");
587 return NULL;
588 }
589
Michael Osipov48ce4892018-08-23 15:27:19 +0200590#ifndef HAVE_STRSIGNAL
Antoine Pietri5d2a27d2018-03-12 14:42:34 +0100591 switch (signalnum) {
Michael Osipov48ce4892018-08-23 15:27:19 +0200592 /* Though being a UNIX, HP-UX does not provide strsignal(3). */
593#ifndef MS_WINDOWS
594 case SIGHUP:
595 res = "Hangup";
596 break;
597 case SIGALRM:
598 res = "Alarm clock";
599 break;
600 case SIGPIPE:
601 res = "Broken pipe";
602 break;
603 case SIGQUIT:
604 res = "Quit";
605 break;
606 case SIGCHLD:
607 res = "Child exited";
608 break;
609#endif
610 /* Custom redefinition of POSIX signals allowed on Windows. */
Antoine Pietri5d2a27d2018-03-12 14:42:34 +0100611 case SIGINT:
612 res = "Interrupt";
613 break;
614 case SIGILL:
615 res = "Illegal instruction";
616 break;
617 case SIGABRT:
618 res = "Aborted";
619 break;
620 case SIGFPE:
621 res = "Floating point exception";
622 break;
623 case SIGSEGV:
624 res = "Segmentation fault";
625 break;
626 case SIGTERM:
627 res = "Terminated";
628 break;
629 default:
630 Py_RETURN_NONE;
631 }
632#else
633 errno = 0;
634 res = strsignal(signalnum);
635
636 if (errno || res == NULL || strstr(res, "Unknown signal") != NULL)
637 Py_RETURN_NONE;
638#endif
639
640 return Py_BuildValue("s", res);
641}
642
Christian Heimes8640e742008-02-23 16:23:06 +0000643#ifdef HAVE_SIGINTERRUPT
Tal Einatc7027b72015-05-16 14:14:49 +0300644
645/*[clinic input]
646signal.siginterrupt
647
648 signalnum: int
649 flag: int
650 /
651
652Change system call restart behaviour.
653
654If flag is False, system calls will be restarted when interrupted by
655signal sig, else system calls will be interrupted.
656[clinic start generated code]*/
Christian Heimes8640e742008-02-23 16:23:06 +0000657
658static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300659signal_siginterrupt_impl(PyObject *module, int signalnum, int flag)
660/*[clinic end generated code: output=063816243d85dd19 input=4160acacca3e2099]*/
Christian Heimes8640e742008-02-23 16:23:06 +0000661{
Tal Einatc7027b72015-05-16 14:14:49 +0300662 if (signalnum < 1 || signalnum >= NSIG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000663 PyErr_SetString(PyExc_ValueError,
664 "signal number out of range");
665 return NULL;
666 }
Tal Einatc7027b72015-05-16 14:14:49 +0300667 if (siginterrupt(signalnum, flag)<0) {
Victor Stinner388196e2011-05-10 17:13:00 +0200668 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000669 return NULL;
670 }
Tal Einatc7027b72015-05-16 14:14:49 +0300671 Py_RETURN_NONE;
Christian Heimes8640e742008-02-23 16:23:06 +0000672}
673
674#endif
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000675
Tal Einatc7027b72015-05-16 14:14:49 +0300676
677static PyObject*
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800678signal_set_wakeup_fd(PyObject *self, PyObject *args, PyObject *kwds)
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000679{
Victor Stinnere134a7f2015-03-30 10:09:31 +0200680 struct _Py_stat_struct status;
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800681 static char *kwlist[] = {
682 "", "warn_on_full_buffer", NULL,
683 };
684 int warn_on_full_buffer = 1;
Victor Stinner11517102014-07-29 23:31:34 +0200685#ifdef MS_WINDOWS
686 PyObject *fdobj;
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100687 SOCKET_T sockfd, old_sockfd;
Victor Stinner11517102014-07-29 23:31:34 +0200688 int res;
689 int res_size = sizeof res;
690 PyObject *mod;
Victor Stinner11517102014-07-29 23:31:34 +0200691 int is_socket;
692
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800693 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|$p:set_wakeup_fd", kwlist,
694 &fdobj, &warn_on_full_buffer))
Victor Stinner11517102014-07-29 23:31:34 +0200695 return NULL;
696
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100697 sockfd = PyLong_AsSocket_t(fdobj);
698 if (sockfd == (SOCKET_T)(-1) && PyErr_Occurred())
Victor Stinner11517102014-07-29 23:31:34 +0200699 return NULL;
700#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000701 int fd, old_fd;
Victor Stinner11517102014-07-29 23:31:34 +0200702
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800703 if (!PyArg_ParseTupleAndKeywords(args, kwds, "i|$p:set_wakeup_fd", kwlist,
704 &fd, &warn_on_full_buffer))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000705 return NULL;
Victor Stinner11517102014-07-29 23:31:34 +0200706#endif
707
Victor Stinner72818982020-03-26 22:28:11 +0100708 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200709 if (!_Py_ThreadCanHandleSignals(tstate->interp)) {
Victor Stinner72818982020-03-26 22:28:11 +0100710 _PyErr_SetString(tstate, PyExc_ValueError,
711 "set_wakeup_fd only works in main thread "
712 "of the main interpreter");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000713 return NULL;
714 }
Victor Stinner0bffc942014-07-21 16:28:54 +0200715
Victor Stinner11517102014-07-29 23:31:34 +0200716#ifdef MS_WINDOWS
717 is_socket = 0;
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100718 if (sockfd != INVALID_FD) {
Victor Stinner11517102014-07-29 23:31:34 +0200719 /* Import the _socket module to call WSAStartup() */
720 mod = PyImport_ImportModuleNoBlock("_socket");
721 if (mod == NULL)
722 return NULL;
723 Py_DECREF(mod);
724
725 /* test the socket */
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100726 if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR,
Victor Stinner11517102014-07-29 23:31:34 +0200727 (char *)&res, &res_size) != 0) {
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100728 int fd, err;
729
730 err = WSAGetLastError();
Victor Stinner11517102014-07-29 23:31:34 +0200731 if (err != WSAENOTSOCK) {
732 PyErr_SetExcFromWindowsErr(PyExc_OSError, err);
733 return NULL;
734 }
735
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100736 fd = (int)sockfd;
Steve Dower940f33a2016-09-08 11:21:54 -0700737 if ((SOCKET_T)fd != sockfd) {
Victor Stinner72818982020-03-26 22:28:11 +0100738 _PyErr_SetString(tstate, PyExc_ValueError, "invalid fd");
Victor Stinner11517102014-07-29 23:31:34 +0200739 return NULL;
740 }
741
Victor Stinner72818982020-03-26 22:28:11 +0100742 if (_Py_fstat(fd, &status) != 0) {
Victor Stinner11517102014-07-29 23:31:34 +0200743 return NULL;
Victor Stinner72818982020-03-26 22:28:11 +0100744 }
Victor Stinner38227602014-08-27 12:59:44 +0200745
746 /* on Windows, a file cannot be set to non-blocking mode */
Victor Stinner11517102014-07-29 23:31:34 +0200747 }
Victor Stinner38227602014-08-27 12:59:44 +0200748 else {
Victor Stinner11517102014-07-29 23:31:34 +0200749 is_socket = 1;
Victor Stinner38227602014-08-27 12:59:44 +0200750
751 /* Windows does not provide a function to test if a socket
752 is in non-blocking mode */
753 }
Victor Stinner11517102014-07-29 23:31:34 +0200754 }
755
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100756 old_sockfd = wakeup.fd;
757 wakeup.fd = sockfd;
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800758 wakeup.warn_on_full_buffer = warn_on_full_buffer;
Victor Stinner11517102014-07-29 23:31:34 +0200759 wakeup.use_send = is_socket;
760
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100761 if (old_sockfd != INVALID_FD)
762 return PyLong_FromSocket_t(old_sockfd);
Victor Stinner11517102014-07-29 23:31:34 +0200763 else
764 return PyLong_FromLong(-1);
765#else
Victor Stinnerd18ccd12014-07-24 21:58:53 +0200766 if (fd != -1) {
Victor Stinner38227602014-08-27 12:59:44 +0200767 int blocking;
768
Victor Stinnere134a7f2015-03-30 10:09:31 +0200769 if (_Py_fstat(fd, &status) != 0)
Victor Stinner11517102014-07-29 23:31:34 +0200770 return NULL;
Victor Stinner38227602014-08-27 12:59:44 +0200771
772 blocking = _Py_get_blocking(fd);
773 if (blocking < 0)
774 return NULL;
775 if (blocking) {
Victor Stinner72818982020-03-26 22:28:11 +0100776 _PyErr_Format(tstate, PyExc_ValueError,
777 "the fd %i must be in non-blocking mode",
778 fd);
Victor Stinner38227602014-08-27 12:59:44 +0200779 return NULL;
780 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000781 }
Victor Stinner0bffc942014-07-21 16:28:54 +0200782
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800783 old_fd = wakeup.fd;
784 wakeup.fd = fd;
785 wakeup.warn_on_full_buffer = warn_on_full_buffer;
Victor Stinner0bffc942014-07-21 16:28:54 +0200786
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000787 return PyLong_FromLong(old_fd);
Victor Stinner11517102014-07-29 23:31:34 +0200788#endif
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000789}
790
791PyDoc_STRVAR(set_wakeup_fd_doc,
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800792"set_wakeup_fd(fd, *, warn_on_full_buffer=True) -> fd\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000793\n\
Victor Stinner11517102014-07-29 23:31:34 +0200794Sets the fd to be written to (with the signal number) when a signal\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000795comes in. A library can use this to wakeup select or poll.\n\
Victor Stinner11517102014-07-29 23:31:34 +0200796The previous fd or -1 is returned.\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000797\n\
798The fd must be non-blocking.");
799
800/* C API for the same, without all the error checking */
801int
802PySignal_SetWakeupFd(int fd)
803{
Victor Stinner11517102014-07-29 23:31:34 +0200804 int old_fd;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000805 if (fd < 0)
806 fd = -1;
Victor Stinner11517102014-07-29 23:31:34 +0200807
808#ifdef MS_WINDOWS
809 old_fd = Py_SAFE_DOWNCAST(wakeup.fd, SOCKET_T, int);
Victor Stinner11517102014-07-29 23:31:34 +0200810#else
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800811 old_fd = wakeup.fd;
Victor Stinner11517102014-07-29 23:31:34 +0200812#endif
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800813 wakeup.fd = fd;
814 wakeup.warn_on_full_buffer = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000815 return old_fd;
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000816}
817
818
Martin v. Löwis823725e2008-03-24 13:39:54 +0000819#ifdef HAVE_SETITIMER
Tal Einatc7027b72015-05-16 14:14:49 +0300820
821/*[clinic input]
822signal.setitimer
823
824 which: int
Victor Stinneref611c92017-10-13 13:49:43 -0700825 seconds: object
826 interval: object(c_default="NULL") = 0.0
Tal Einatc7027b72015-05-16 14:14:49 +0300827 /
828
829Sets given itimer (one of ITIMER_REAL, ITIMER_VIRTUAL or ITIMER_PROF).
830
831The timer will fire after value seconds and after that every interval seconds.
832The itimer can be cleared by setting seconds to zero.
833
834Returns old values as a tuple: (delay, interval).
835[clinic start generated code]*/
836
Martin v. Löwis823725e2008-03-24 13:39:54 +0000837static PyObject *
Victor Stinneref611c92017-10-13 13:49:43 -0700838signal_setitimer_impl(PyObject *module, int which, PyObject *seconds,
839 PyObject *interval)
840/*[clinic end generated code: output=65f9dcbddc35527b input=de43daf194e6f66f]*/
Martin v. Löwis823725e2008-03-24 13:39:54 +0000841{
Martin v. Löwis823725e2008-03-24 13:39:54 +0000842 struct itimerval new, old;
843
Victor Stinneref611c92017-10-13 13:49:43 -0700844 if (timeval_from_double(seconds, &new.it_value) < 0) {
845 return NULL;
846 }
847 if (timeval_from_double(interval, &new.it_interval) < 0) {
848 return NULL;
849 }
850
Martin v. Löwis823725e2008-03-24 13:39:54 +0000851 /* Let OS check "which" value */
852 if (setitimer(which, &new, &old) != 0) {
Tal Einatc7027b72015-05-16 14:14:49 +0300853 PyErr_SetFromErrno(ItimerError);
854 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000855 }
856
857 return itimer_retval(&old);
858}
859
Martin v. Löwis823725e2008-03-24 13:39:54 +0000860#endif
861
862
863#ifdef HAVE_GETITIMER
Tal Einatc7027b72015-05-16 14:14:49 +0300864
865/*[clinic input]
866signal.getitimer
867
868 which: int
869 /
870
871Returns current value of given itimer.
872[clinic start generated code]*/
873
Martin v. Löwis823725e2008-03-24 13:39:54 +0000874static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300875signal_getitimer_impl(PyObject *module, int which)
876/*[clinic end generated code: output=9e053175d517db40 input=f7d21d38f3490627]*/
Martin v. Löwis823725e2008-03-24 13:39:54 +0000877{
Martin v. Löwis823725e2008-03-24 13:39:54 +0000878 struct itimerval old;
879
Martin v. Löwis823725e2008-03-24 13:39:54 +0000880 if (getitimer(which, &old) != 0) {
Tal Einatc7027b72015-05-16 14:14:49 +0300881 PyErr_SetFromErrno(ItimerError);
882 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000883 }
884
885 return itimer_retval(&old);
886}
887
Martin v. Löwis823725e2008-03-24 13:39:54 +0000888#endif
889
Victor Stinnerb3e72192011-05-08 01:46:11 +0200890#if defined(PYPTHREAD_SIGMASK) || defined(HAVE_SIGPENDING)
Victor Stinner35b300c2011-05-04 13:20:35 +0200891static PyObject*
892sigset_to_set(sigset_t mask)
893{
894 PyObject *signum, *result;
895 int sig;
896
897 result = PySet_New(0);
898 if (result == NULL)
899 return NULL;
900
901 for (sig = 1; sig < NSIG; sig++) {
902 if (sigismember(&mask, sig) != 1)
903 continue;
904
905 /* Handle the case where it is a member by adding the signal to
906 the result list. Ignore the other cases because they mean the
907 signal isn't a member of the mask or the signal was invalid,
908 and an invalid signal must have been our fault in constructing
909 the loop boundaries. */
910 signum = PyLong_FromLong(sig);
911 if (signum == NULL) {
912 Py_DECREF(result);
913 return NULL;
914 }
915 if (PySet_Add(result, signum) == -1) {
916 Py_DECREF(signum);
917 Py_DECREF(result);
918 return NULL;
919 }
920 Py_DECREF(signum);
921 }
922 return result;
923}
Victor Stinnerb3e72192011-05-08 01:46:11 +0200924#endif
Victor Stinner35b300c2011-05-04 13:20:35 +0200925
Victor Stinnerb3e72192011-05-08 01:46:11 +0200926#ifdef PYPTHREAD_SIGMASK
Tal Einatc7027b72015-05-16 14:14:49 +0300927
928/*[clinic input]
929signal.pthread_sigmask
930
931 how: int
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300932 mask: sigset_t
Tal Einatc7027b72015-05-16 14:14:49 +0300933 /
934
935Fetch and/or change the signal mask of the calling thread.
936[clinic start generated code]*/
937
Victor Stinnera9293352011-04-30 15:21:58 +0200938static PyObject *
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300939signal_pthread_sigmask_impl(PyObject *module, int how, sigset_t mask)
940/*[clinic end generated code: output=0562c0fb192981a8 input=85bcebda442fa77f]*/
Victor Stinnera9293352011-04-30 15:21:58 +0200941{
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300942 sigset_t previous;
Victor Stinnera9293352011-04-30 15:21:58 +0200943 int err;
944
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300945 err = pthread_sigmask(how, &mask, &previous);
Victor Stinnera9293352011-04-30 15:21:58 +0200946 if (err != 0) {
947 errno = err;
Victor Stinnerb3e72192011-05-08 01:46:11 +0200948 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnera9293352011-04-30 15:21:58 +0200949 return NULL;
950 }
951
Victor Stinnerd0e516d2011-05-03 14:57:12 +0200952 /* if signals was unblocked, signal handlers have been called */
953 if (PyErr_CheckSignals())
954 return NULL;
955
Victor Stinner35b300c2011-05-04 13:20:35 +0200956 return sigset_to_set(previous);
Victor Stinnera9293352011-04-30 15:21:58 +0200957}
958
Victor Stinnera9293352011-04-30 15:21:58 +0200959#endif /* #ifdef PYPTHREAD_SIGMASK */
960
Martin v. Löwis823725e2008-03-24 13:39:54 +0000961
Victor Stinnerb3e72192011-05-08 01:46:11 +0200962#ifdef HAVE_SIGPENDING
Tal Einatc7027b72015-05-16 14:14:49 +0300963
964/*[clinic input]
965signal.sigpending
966
967Examine pending signals.
968
969Returns a set of signal numbers that are pending for delivery to
970the calling thread.
971[clinic start generated code]*/
972
Victor Stinnerb3e72192011-05-08 01:46:11 +0200973static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300974signal_sigpending_impl(PyObject *module)
975/*[clinic end generated code: output=53375ffe89325022 input=e0036c016f874e29]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +0200976{
977 int err;
978 sigset_t mask;
979 err = sigpending(&mask);
980 if (err)
981 return PyErr_SetFromErrno(PyExc_OSError);
982 return sigset_to_set(mask);
983}
984
Victor Stinnerb3e72192011-05-08 01:46:11 +0200985#endif /* #ifdef HAVE_SIGPENDING */
986
987
988#ifdef HAVE_SIGWAIT
Tal Einatc7027b72015-05-16 14:14:49 +0300989
990/*[clinic input]
991signal.sigwait
992
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300993 sigset: sigset_t
Tal Einatc7027b72015-05-16 14:14:49 +0300994 /
995
996Wait for a signal.
997
998Suspend execution of the calling thread until the delivery of one of the
999signals specified in the signal set sigset. The function accepts the signal
1000and returns the signal number.
1001[clinic start generated code]*/
1002
Victor Stinnerb3e72192011-05-08 01:46:11 +02001003static PyObject *
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001004signal_sigwait_impl(PyObject *module, sigset_t sigset)
1005/*[clinic end generated code: output=f43770699d682f96 input=a6fbd47b1086d119]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +02001006{
Victor Stinnerb3e72192011-05-08 01:46:11 +02001007 int err, signum;
1008
Victor Stinner10c30d62011-06-10 01:39:53 +02001009 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001010 err = sigwait(&sigset, &signum);
Victor Stinner10c30d62011-06-10 01:39:53 +02001011 Py_END_ALLOW_THREADS
Victor Stinnerb3e72192011-05-08 01:46:11 +02001012 if (err) {
1013 errno = err;
1014 return PyErr_SetFromErrno(PyExc_OSError);
1015 }
1016
1017 return PyLong_FromLong(signum);
1018}
1019
Tal Einatc7027b72015-05-16 14:14:49 +03001020#endif /* #ifdef HAVE_SIGWAIT */
1021
Victor Stinnerb3e72192011-05-08 01:46:11 +02001022
Antoine Pitrou9d3627e2018-05-04 13:00:50 +02001023#if defined(HAVE_SIGFILLSET) || defined(MS_WINDOWS)
1024
1025/*[clinic input]
1026signal.valid_signals
1027
1028Return a set of valid signal numbers on this platform.
1029
1030The signal numbers returned by this function can be safely passed to
1031functions like `pthread_sigmask`.
1032[clinic start generated code]*/
1033
1034static PyObject *
1035signal_valid_signals_impl(PyObject *module)
1036/*[clinic end generated code: output=1609cffbcfcf1314 input=86a3717ff25288f2]*/
1037{
1038#ifdef MS_WINDOWS
1039#ifdef SIGBREAK
1040 PyObject *tup = Py_BuildValue("(iiiiiii)", SIGABRT, SIGBREAK, SIGFPE,
1041 SIGILL, SIGINT, SIGSEGV, SIGTERM);
1042#else
1043 PyObject *tup = Py_BuildValue("(iiiiii)", SIGABRT, SIGFPE, SIGILL,
1044 SIGINT, SIGSEGV, SIGTERM);
1045#endif
1046 if (tup == NULL) {
1047 return NULL;
1048 }
1049 PyObject *set = PySet_New(tup);
1050 Py_DECREF(tup);
1051 return set;
1052#else
1053 sigset_t mask;
1054 if (sigemptyset(&mask) || sigfillset(&mask)) {
1055 return PyErr_SetFromErrno(PyExc_OSError);
1056 }
1057 return sigset_to_set(mask);
1058#endif
1059}
1060
1061#endif /* #if defined(HAVE_SIGFILLSET) || defined(MS_WINDOWS) */
1062
1063
Ross Lagerwallbc808222011-06-25 12:13:40 +02001064#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
1065static int initialized;
1066static PyStructSequence_Field struct_siginfo_fields[] = {
1067 {"si_signo", "signal number"},
1068 {"si_code", "signal code"},
1069 {"si_errno", "errno associated with this signal"},
1070 {"si_pid", "sending process ID"},
1071 {"si_uid", "real user ID of sending process"},
1072 {"si_status", "exit value or signal"},
1073 {"si_band", "band event for SIGPOLL"},
1074 {0}
1075};
1076
1077PyDoc_STRVAR(struct_siginfo__doc__,
1078"struct_siginfo: Result from sigwaitinfo or sigtimedwait.\n\n\
1079This object may be accessed either as a tuple of\n\
1080(si_signo, si_code, si_errno, si_pid, si_uid, si_status, si_band),\n\
1081or via the attributes si_signo, si_code, and so on.");
1082
1083static PyStructSequence_Desc struct_siginfo_desc = {
1084 "signal.struct_siginfo", /* name */
1085 struct_siginfo__doc__, /* doc */
1086 struct_siginfo_fields, /* fields */
1087 7 /* n_in_sequence */
1088};
1089
1090static PyTypeObject SiginfoType;
1091
1092static PyObject *
1093fill_siginfo(siginfo_t *si)
1094{
1095 PyObject *result = PyStructSequence_New(&SiginfoType);
1096 if (!result)
1097 return NULL;
1098
1099 PyStructSequence_SET_ITEM(result, 0, PyLong_FromLong((long)(si->si_signo)));
1100 PyStructSequence_SET_ITEM(result, 1, PyLong_FromLong((long)(si->si_code)));
Victor Stinner09532fe2019-05-10 23:39:09 +02001101#ifdef __VXWORKS__
pxinwr8b5bdda2019-03-14 01:18:25 +08001102 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong(0L));
1103 PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong(0L));
1104 PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong(0L));
1105 PyStructSequence_SET_ITEM(result, 5, PyLong_FromLong(0L));
Victor Stinner09532fe2019-05-10 23:39:09 +02001106#else
Ross Lagerwallbc808222011-06-25 12:13:40 +02001107 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si->si_errno)));
1108 PyStructSequence_SET_ITEM(result, 3, PyLong_FromPid(si->si_pid));
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02001109 PyStructSequence_SET_ITEM(result, 4, _PyLong_FromUid(si->si_uid));
Ross Lagerwallbc808222011-06-25 12:13:40 +02001110 PyStructSequence_SET_ITEM(result, 5,
1111 PyLong_FromLong((long)(si->si_status)));
Victor Stinner09532fe2019-05-10 23:39:09 +02001112#endif
Zachary Ware6a6967e2016-10-01 00:47:27 -05001113#ifdef HAVE_SIGINFO_T_SI_BAND
Ross Lagerwallbc808222011-06-25 12:13:40 +02001114 PyStructSequence_SET_ITEM(result, 6, PyLong_FromLong(si->si_band));
Zachary Ware6a6967e2016-10-01 00:47:27 -05001115#else
1116 PyStructSequence_SET_ITEM(result, 6, PyLong_FromLong(0L));
1117#endif
Ross Lagerwallbc808222011-06-25 12:13:40 +02001118 if (PyErr_Occurred()) {
1119 Py_DECREF(result);
1120 return NULL;
1121 }
1122
1123 return result;
1124}
1125#endif
1126
1127#ifdef HAVE_SIGWAITINFO
Tal Einatc7027b72015-05-16 14:14:49 +03001128
1129/*[clinic input]
1130signal.sigwaitinfo
1131
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001132 sigset: sigset_t
Tal Einatc7027b72015-05-16 14:14:49 +03001133 /
1134
1135Wait synchronously until one of the signals in *sigset* is delivered.
1136
1137Returns a struct_siginfo containing information about the signal.
1138[clinic start generated code]*/
1139
Ross Lagerwallbc808222011-06-25 12:13:40 +02001140static PyObject *
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001141signal_sigwaitinfo_impl(PyObject *module, sigset_t sigset)
1142/*[clinic end generated code: output=1eb2f1fa236fdbca input=3d1a7e1f27fc664c]*/
Ross Lagerwallbc808222011-06-25 12:13:40 +02001143{
Ross Lagerwallbc808222011-06-25 12:13:40 +02001144 siginfo_t si;
1145 int err;
Victor Stinnera453cd82015-03-20 12:54:28 +01001146 int async_err = 0;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001147
Victor Stinnera453cd82015-03-20 12:54:28 +01001148 do {
1149 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001150 err = sigwaitinfo(&sigset, &si);
Victor Stinnera453cd82015-03-20 12:54:28 +01001151 Py_END_ALLOW_THREADS
1152 } while (err == -1
1153 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Ross Lagerwallbc808222011-06-25 12:13:40 +02001154 if (err == -1)
Victor Stinnera453cd82015-03-20 12:54:28 +01001155 return (!async_err) ? PyErr_SetFromErrno(PyExc_OSError) : NULL;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001156
1157 return fill_siginfo(&si);
1158}
1159
Ross Lagerwallbc808222011-06-25 12:13:40 +02001160#endif /* #ifdef HAVE_SIGWAITINFO */
1161
1162#ifdef HAVE_SIGTIMEDWAIT
Tal Einatc7027b72015-05-16 14:14:49 +03001163
1164/*[clinic input]
1165signal.sigtimedwait
1166
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001167 sigset: sigset_t
Serhiy Storchaka6b680cd2015-05-16 15:57:56 +03001168 timeout as timeout_obj: object
Tal Einatc7027b72015-05-16 14:14:49 +03001169 /
1170
1171Like sigwaitinfo(), but with a timeout.
1172
1173The timeout is specified in seconds, with floating point numbers allowed.
1174[clinic start generated code]*/
1175
Ross Lagerwallbc808222011-06-25 12:13:40 +02001176static PyObject *
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001177signal_sigtimedwait_impl(PyObject *module, sigset_t sigset,
Serhiy Storchaka6b680cd2015-05-16 15:57:56 +03001178 PyObject *timeout_obj)
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001179/*[clinic end generated code: output=59c8971e8ae18a64 input=87fd39237cf0b7ba]*/
Ross Lagerwallbc808222011-06-25 12:13:40 +02001180{
Victor Stinnera453cd82015-03-20 12:54:28 +01001181 struct timespec ts;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001182 siginfo_t si;
Victor Stinnera453cd82015-03-20 12:54:28 +01001183 int res;
Victor Stinner34dc0f42015-03-27 18:19:03 +01001184 _PyTime_t timeout, deadline, monotonic;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001185
Victor Stinner869e1772015-03-30 03:49:14 +02001186 if (_PyTime_FromSecondsObject(&timeout,
1187 timeout_obj, _PyTime_ROUND_CEILING) < 0)
Ross Lagerwallbc808222011-06-25 12:13:40 +02001188 return NULL;
1189
Victor Stinnera453cd82015-03-20 12:54:28 +01001190 if (timeout < 0) {
Ross Lagerwallbc808222011-06-25 12:13:40 +02001191 PyErr_SetString(PyExc_ValueError, "timeout must be non-negative");
1192 return NULL;
1193 }
1194
Victor Stinner34dc0f42015-03-27 18:19:03 +01001195 deadline = _PyTime_GetMonotonicClock() + timeout;
Victor Stinnera453cd82015-03-20 12:54:28 +01001196
1197 do {
Victor Stinner34dc0f42015-03-27 18:19:03 +01001198 if (_PyTime_AsTimespec(timeout, &ts) < 0)
1199 return NULL;
Victor Stinnera453cd82015-03-20 12:54:28 +01001200
1201 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001202 res = sigtimedwait(&sigset, &si, &ts);
Victor Stinnera453cd82015-03-20 12:54:28 +01001203 Py_END_ALLOW_THREADS
1204
1205 if (res != -1)
1206 break;
1207
1208 if (errno != EINTR) {
1209 if (errno == EAGAIN)
1210 Py_RETURN_NONE;
1211 else
1212 return PyErr_SetFromErrno(PyExc_OSError);
1213 }
1214
1215 /* sigtimedwait() was interrupted by a signal (EINTR) */
1216 if (PyErr_CheckSignals())
1217 return NULL;
1218
Victor Stinner34dc0f42015-03-27 18:19:03 +01001219 monotonic = _PyTime_GetMonotonicClock();
1220 timeout = deadline - monotonic;
Victor Stinner6aa446c2015-03-30 21:33:51 +02001221 if (timeout < 0)
Victor Stinnera453cd82015-03-20 12:54:28 +01001222 break;
1223 } while (1);
Ross Lagerwallbc808222011-06-25 12:13:40 +02001224
1225 return fill_siginfo(&si);
1226}
1227
Ross Lagerwallbc808222011-06-25 12:13:40 +02001228#endif /* #ifdef HAVE_SIGTIMEDWAIT */
1229
Victor Stinnerb3e72192011-05-08 01:46:11 +02001230
Antoine Pitroua6a4dc82017-09-07 18:56:24 +02001231#if defined(HAVE_PTHREAD_KILL)
Tal Einatc7027b72015-05-16 14:14:49 +03001232
1233/*[clinic input]
1234signal.pthread_kill
1235
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +02001236 thread_id: unsigned_long(bitwise=True)
Tal Einatc7027b72015-05-16 14:14:49 +03001237 signalnum: int
1238 /
1239
1240Send a signal to a thread.
1241[clinic start generated code]*/
1242
Victor Stinnerb3e72192011-05-08 01:46:11 +02001243static PyObject *
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +02001244signal_pthread_kill_impl(PyObject *module, unsigned long thread_id,
1245 int signalnum)
1246/*[clinic end generated code: output=7629919b791bc27f input=1d901f2c7bb544ff]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +02001247{
Victor Stinnerb3e72192011-05-08 01:46:11 +02001248 int err;
1249
Saiyang Gou7514f4f2020-02-12 23:47:42 -08001250 if (PySys_Audit("signal.pthread_kill", "ki", thread_id, signalnum) < 0) {
1251 return NULL;
1252 }
1253
Tal Einatc7027b72015-05-16 14:14:49 +03001254 err = pthread_kill((pthread_t)thread_id, signalnum);
Victor Stinnerb3e72192011-05-08 01:46:11 +02001255 if (err != 0) {
1256 errno = err;
1257 PyErr_SetFromErrno(PyExc_OSError);
1258 return NULL;
1259 }
1260
1261 /* the signal may have been send to the current thread */
1262 if (PyErr_CheckSignals())
1263 return NULL;
1264
1265 Py_RETURN_NONE;
1266}
1267
Antoine Pitroua6a4dc82017-09-07 18:56:24 +02001268#endif /* #if defined(HAVE_PTHREAD_KILL) */
Victor Stinnerb3e72192011-05-08 01:46:11 +02001269
1270
Benjamin Peterson74834512019-11-19 20:39:14 -08001271#if defined(__linux__) && defined(__NR_pidfd_send_signal)
1272/*[clinic input]
1273signal.pidfd_send_signal
1274
1275 pidfd: int
1276 signalnum: int
1277 siginfo: object = None
1278 flags: int = 0
1279 /
1280
1281Send a signal to a process referred to by a pid file descriptor.
1282[clinic start generated code]*/
1283
1284static PyObject *
1285signal_pidfd_send_signal_impl(PyObject *module, int pidfd, int signalnum,
1286 PyObject *siginfo, int flags)
1287/*[clinic end generated code: output=2d59f04a75d9cbdf input=2a6543a1f4ac2000]*/
1288
1289{
1290 if (siginfo != Py_None) {
1291 PyErr_SetString(PyExc_TypeError, "siginfo must be None");
1292 return NULL;
1293 }
1294 if (syscall(__NR_pidfd_send_signal, pidfd, signalnum, NULL, flags) < 0) {
1295 PyErr_SetFromErrno(PyExc_OSError);
1296 return NULL;
1297 }
1298 Py_RETURN_NONE;
1299}
1300#endif
1301
1302
Victor Stinnerb3e72192011-05-08 01:46:11 +02001303
Tal Einatc7027b72015-05-16 14:14:49 +03001304/* List of functions defined in the module -- some of the methoddefs are
1305 defined to nothing if the corresponding C function is not available. */
Barry Warsaw92971171997-01-03 00:14:25 +00001306static PyMethodDef signal_methods[] = {
Serhiy Storchakab0689ae2020-07-12 19:15:20 +03001307 SIGNAL_DEFAULT_INT_HANDLER_METHODDEF
Tal Einatc7027b72015-05-16 14:14:49 +03001308 SIGNAL_ALARM_METHODDEF
1309 SIGNAL_SETITIMER_METHODDEF
1310 SIGNAL_GETITIMER_METHODDEF
1311 SIGNAL_SIGNAL_METHODDEF
Vladimir Matveevc24c6c22019-01-08 01:58:25 -08001312 SIGNAL_RAISE_SIGNAL_METHODDEF
Antoine Pietri5d2a27d2018-03-12 14:42:34 +01001313 SIGNAL_STRSIGNAL_METHODDEF
Tal Einatc7027b72015-05-16 14:14:49 +03001314 SIGNAL_GETSIGNAL_METHODDEF
Serhiy Storchaka62be7422018-11-27 13:27:31 +02001315 {"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 +03001316 SIGNAL_SIGINTERRUPT_METHODDEF
1317 SIGNAL_PAUSE_METHODDEF
Benjamin Peterson74834512019-11-19 20:39:14 -08001318 SIGNAL_PIDFD_SEND_SIGNAL_METHODDEF
Tal Einatc7027b72015-05-16 14:14:49 +03001319 SIGNAL_PTHREAD_KILL_METHODDEF
1320 SIGNAL_PTHREAD_SIGMASK_METHODDEF
1321 SIGNAL_SIGPENDING_METHODDEF
1322 SIGNAL_SIGWAIT_METHODDEF
1323 SIGNAL_SIGWAITINFO_METHODDEF
1324 SIGNAL_SIGTIMEDWAIT_METHODDEF
Antoine Pitrou9d3627e2018-05-04 13:00:50 +02001325#if defined(HAVE_SIGFILLSET) || defined(MS_WINDOWS)
1326 SIGNAL_VALID_SIGNALS_METHODDEF
1327#endif
Tal Einatc7027b72015-05-16 14:14:49 +03001328 {NULL, NULL} /* sentinel */
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001329};
1330
Barry Warsaw92971171997-01-03 00:14:25 +00001331
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001332PyDoc_STRVAR(module_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001333"This module provides mechanisms to use signal handlers in Python.\n\
1334\n\
1335Functions:\n\
1336\n\
1337alarm() -- cause SIGALRM after a specified time [Unix only]\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001338setitimer() -- cause a signal (described below) after a specified\n\
1339 float time and the timer may restart then [Unix only]\n\
1340getitimer() -- get current value of timer [Unix only]\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001341signal() -- set the action for a given signal\n\
1342getsignal() -- get the signal action for a given signal\n\
1343pause() -- wait until a signal arrives [Unix only]\n\
1344default_int_handler() -- default SIGINT handler\n\
1345\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001346signal constants:\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001347SIG_DFL -- used to refer to the system default handler\n\
1348SIG_IGN -- used to ignore the signal\n\
1349NSIG -- number of defined signals\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001350SIGINT, SIGTERM, etc. -- signal numbers\n\
1351\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001352itimer constants:\n\
1353ITIMER_REAL -- decrements in real time, and delivers SIGALRM upon\n\
1354 expiration\n\
1355ITIMER_VIRTUAL -- decrements only when the process is executing,\n\
1356 and delivers SIGVTALRM upon expiration\n\
1357ITIMER_PROF -- decrements both when the process is executing and\n\
1358 when the system is executing on behalf of the process.\n\
1359 Coupled with ITIMER_VIRTUAL, this timer is usually\n\
1360 used to profile the time spent by the application\n\
1361 in user and kernel space. SIGPROF is delivered upon\n\
1362 expiration.\n\
1363\n\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001364*** IMPORTANT NOTICE ***\n\
1365A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001366the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001367
Martin v. Löwis1a214512008-06-11 05:26:20 +00001368static struct PyModuleDef signalmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001369 PyModuleDef_HEAD_INIT,
Brett Cannon815a6f32014-04-04 10:20:28 -04001370 "_signal",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001371 module_doc,
1372 -1,
1373 signal_methods,
1374 NULL,
1375 NULL,
1376 NULL,
1377 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001378};
1379
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001380PyMODINIT_FUNC
Giampaolo Rodola'e09fb712014-04-04 15:34:17 +02001381PyInit__signal(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001382{
animalize77643c42019-09-09 21:46:26 +08001383 PyObject *m, *d;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001384 int i;
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001385
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001386 /* Create the module and add the functions */
1387 m = PyModule_Create(&signalmodule);
1388 if (m == NULL)
1389 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001390
Ross Lagerwallbc808222011-06-25 12:13:40 +02001391#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
Victor Stinner1c8f0592013-07-22 22:24:54 +02001392 if (!initialized) {
1393 if (PyStructSequence_InitType2(&SiginfoType, &struct_siginfo_desc) < 0)
1394 return NULL;
1395 }
Ross Lagerwallbc808222011-06-25 12:13:40 +02001396 Py_INCREF((PyObject*) &SiginfoType);
1397 PyModule_AddObject(m, "struct_siginfo", (PyObject*) &SiginfoType);
1398 initialized = 1;
1399#endif
1400
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001401 /* Add some symbolic constants to the module */
1402 d = PyModule_GetDict(m);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001403
animalize77643c42019-09-09 21:46:26 +08001404 DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL);
1405 if (!DefaultHandler ||
1406 PyDict_SetItemString(d, "SIG_DFL", DefaultHandler) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001407 goto finally;
animalize77643c42019-09-09 21:46:26 +08001408 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001409
animalize77643c42019-09-09 21:46:26 +08001410 IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN);
1411 if (!IgnoreHandler ||
1412 PyDict_SetItemString(d, "SIG_IGN", IgnoreHandler) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001413 goto finally;
animalize77643c42019-09-09 21:46:26 +08001414 }
Barry Warsaw92971171997-01-03 00:14:25 +00001415
Joannah Nanjekye9541bd32019-04-21 21:47:06 -04001416 if (PyModule_AddIntMacro(m, NSIG))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001417 goto finally;
Barry Warsaw92971171997-01-03 00:14:25 +00001418
Victor Stinnera9293352011-04-30 15:21:58 +02001419#ifdef SIG_BLOCK
Victor Stinner72c53b52011-05-02 16:15:43 +02001420 if (PyModule_AddIntMacro(m, SIG_BLOCK))
1421 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001422#endif
Victor Stinnera9293352011-04-30 15:21:58 +02001423#ifdef SIG_UNBLOCK
Victor Stinner72c53b52011-05-02 16:15:43 +02001424 if (PyModule_AddIntMacro(m, SIG_UNBLOCK))
1425 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001426#endif
Victor Stinnera9293352011-04-30 15:21:58 +02001427#ifdef SIG_SETMASK
Victor Stinner72c53b52011-05-02 16:15:43 +02001428 if (PyModule_AddIntMacro(m, SIG_SETMASK))
1429 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001430#endif
1431
animalize77643c42019-09-09 21:46:26 +08001432 IntHandler = PyDict_GetItemString(d, "default_int_handler");
1433 if (!IntHandler)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001434 goto finally;
1435 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +00001436
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001437 _Py_atomic_store_relaxed(&Handlers[0].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001438 for (i = 1; i < NSIG; i++) {
1439 void (*t)(int);
1440 t = PyOS_getsig(i);
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001441 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001442 if (t == SIG_DFL)
1443 Handlers[i].func = DefaultHandler;
1444 else if (t == SIG_IGN)
1445 Handlers[i].func = IgnoreHandler;
1446 else
1447 Handlers[i].func = Py_None; /* None of our business */
1448 Py_INCREF(Handlers[i].func);
1449 }
1450 if (Handlers[SIGINT].func == DefaultHandler) {
1451 /* Install default int handler */
1452 Py_INCREF(IntHandler);
Serhiy Storchaka57a01d32016-04-10 18:05:40 +03001453 Py_SETREF(Handlers[SIGINT].func, IntHandler);
pkerlinge905c842018-06-01 09:47:18 +00001454 PyOS_setsig(SIGINT, signal_handler);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001455 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001456
1457#ifdef SIGHUP
Christian Heimes6782b142016-09-09 00:11:45 +02001458 if (PyModule_AddIntMacro(m, SIGHUP))
1459 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001460#endif
1461#ifdef SIGINT
Christian Heimes6782b142016-09-09 00:11:45 +02001462 if (PyModule_AddIntMacro(m, SIGINT))
1463 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001464#endif
Tim Peters1ce3cf72001-10-01 17:58:40 +00001465#ifdef SIGBREAK
Christian Heimes6782b142016-09-09 00:11:45 +02001466 if (PyModule_AddIntMacro(m, SIGBREAK))
1467 goto finally;
Tim Peters1ce3cf72001-10-01 17:58:40 +00001468#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001469#ifdef SIGQUIT
Christian Heimes6782b142016-09-09 00:11:45 +02001470 if (PyModule_AddIntMacro(m, SIGQUIT))
1471 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001472#endif
1473#ifdef SIGILL
Christian Heimes6782b142016-09-09 00:11:45 +02001474 if (PyModule_AddIntMacro(m, SIGILL))
1475 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001476#endif
1477#ifdef SIGTRAP
Christian Heimes6782b142016-09-09 00:11:45 +02001478 if (PyModule_AddIntMacro(m, SIGTRAP))
1479 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001480#endif
1481#ifdef SIGIOT
Christian Heimes6782b142016-09-09 00:11:45 +02001482 if (PyModule_AddIntMacro(m, SIGIOT))
1483 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001484#endif
1485#ifdef SIGABRT
Christian Heimes6782b142016-09-09 00:11:45 +02001486 if (PyModule_AddIntMacro(m, SIGABRT))
1487 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001488#endif
1489#ifdef SIGEMT
Christian Heimes6782b142016-09-09 00:11:45 +02001490 if (PyModule_AddIntMacro(m, SIGEMT))
1491 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001492#endif
1493#ifdef SIGFPE
Christian Heimes6782b142016-09-09 00:11:45 +02001494 if (PyModule_AddIntMacro(m, SIGFPE))
1495 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001496#endif
1497#ifdef SIGKILL
Christian Heimes6782b142016-09-09 00:11:45 +02001498 if (PyModule_AddIntMacro(m, SIGKILL))
1499 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001500#endif
1501#ifdef SIGBUS
Christian Heimes6782b142016-09-09 00:11:45 +02001502 if (PyModule_AddIntMacro(m, SIGBUS))
1503 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001504#endif
1505#ifdef SIGSEGV
Christian Heimes6782b142016-09-09 00:11:45 +02001506 if (PyModule_AddIntMacro(m, SIGSEGV))
1507 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001508#endif
1509#ifdef SIGSYS
Christian Heimes6782b142016-09-09 00:11:45 +02001510 if (PyModule_AddIntMacro(m, SIGSYS))
1511 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001512#endif
1513#ifdef SIGPIPE
Christian Heimes6782b142016-09-09 00:11:45 +02001514 if (PyModule_AddIntMacro(m, SIGPIPE))
1515 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001516#endif
1517#ifdef SIGALRM
Christian Heimes6782b142016-09-09 00:11:45 +02001518 if (PyModule_AddIntMacro(m, SIGALRM))
1519 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001520#endif
1521#ifdef SIGTERM
Christian Heimes6782b142016-09-09 00:11:45 +02001522 if (PyModule_AddIntMacro(m, SIGTERM))
1523 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001524#endif
1525#ifdef SIGUSR1
Christian Heimes6782b142016-09-09 00:11:45 +02001526 if (PyModule_AddIntMacro(m, SIGUSR1))
1527 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001528#endif
1529#ifdef SIGUSR2
Christian Heimes6782b142016-09-09 00:11:45 +02001530 if (PyModule_AddIntMacro(m, SIGUSR2))
1531 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001532#endif
1533#ifdef SIGCLD
Christian Heimes6782b142016-09-09 00:11:45 +02001534 if (PyModule_AddIntMacro(m, SIGCLD))
1535 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001536#endif
1537#ifdef SIGCHLD
Christian Heimes6782b142016-09-09 00:11:45 +02001538 if (PyModule_AddIntMacro(m, SIGCHLD))
1539 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001540#endif
1541#ifdef SIGPWR
Christian Heimes6782b142016-09-09 00:11:45 +02001542 if (PyModule_AddIntMacro(m, SIGPWR))
1543 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001544#endif
1545#ifdef SIGIO
Christian Heimes6782b142016-09-09 00:11:45 +02001546 if (PyModule_AddIntMacro(m, SIGIO))
1547 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001548#endif
1549#ifdef SIGURG
Christian Heimes6782b142016-09-09 00:11:45 +02001550 if (PyModule_AddIntMacro(m, SIGURG))
1551 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001552#endif
1553#ifdef SIGWINCH
Christian Heimes6782b142016-09-09 00:11:45 +02001554 if (PyModule_AddIntMacro(m, SIGWINCH))
1555 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001556#endif
1557#ifdef SIGPOLL
Christian Heimes6782b142016-09-09 00:11:45 +02001558 if (PyModule_AddIntMacro(m, SIGPOLL))
1559 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001560#endif
1561#ifdef SIGSTOP
Christian Heimes6782b142016-09-09 00:11:45 +02001562 if (PyModule_AddIntMacro(m, SIGSTOP))
1563 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001564#endif
1565#ifdef SIGTSTP
Christian Heimes6782b142016-09-09 00:11:45 +02001566 if (PyModule_AddIntMacro(m, SIGTSTP))
1567 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001568#endif
1569#ifdef SIGCONT
Christian Heimes6782b142016-09-09 00:11:45 +02001570 if (PyModule_AddIntMacro(m, SIGCONT))
1571 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001572#endif
1573#ifdef SIGTTIN
Christian Heimes6782b142016-09-09 00:11:45 +02001574 if (PyModule_AddIntMacro(m, SIGTTIN))
1575 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001576#endif
1577#ifdef SIGTTOU
Christian Heimes6782b142016-09-09 00:11:45 +02001578 if (PyModule_AddIntMacro(m, SIGTTOU))
1579 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001580#endif
1581#ifdef SIGVTALRM
Christian Heimes6782b142016-09-09 00:11:45 +02001582 if (PyModule_AddIntMacro(m, SIGVTALRM))
1583 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001584#endif
1585#ifdef SIGPROF
Christian Heimes6782b142016-09-09 00:11:45 +02001586 if (PyModule_AddIntMacro(m, SIGPROF))
1587 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001588#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001589#ifdef SIGXCPU
Christian Heimes6782b142016-09-09 00:11:45 +02001590 if (PyModule_AddIntMacro(m, SIGXCPU))
1591 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001592#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001593#ifdef SIGXFSZ
Christian Heimes6782b142016-09-09 00:11:45 +02001594 if (PyModule_AddIntMacro(m, SIGXFSZ))
1595 goto finally;
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001596#endif
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001597#ifdef SIGRTMIN
Christian Heimes6782b142016-09-09 00:11:45 +02001598 if (PyModule_AddIntMacro(m, SIGRTMIN))
1599 goto finally;
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001600#endif
1601#ifdef SIGRTMAX
Christian Heimes6782b142016-09-09 00:11:45 +02001602 if (PyModule_AddIntMacro(m, SIGRTMAX))
1603 goto finally;
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001604#endif
Martin v. Löwis175af252002-01-12 11:43:25 +00001605#ifdef SIGINFO
Christian Heimes6782b142016-09-09 00:11:45 +02001606 if (PyModule_AddIntMacro(m, SIGINFO))
1607 goto finally;
Martin v. Löwis175af252002-01-12 11:43:25 +00001608#endif
Martin v. Löwis823725e2008-03-24 13:39:54 +00001609
1610#ifdef ITIMER_REAL
Christian Heimes6782b142016-09-09 00:11:45 +02001611 if (PyModule_AddIntMacro(m, ITIMER_REAL))
1612 goto finally;
Martin v. Löwis823725e2008-03-24 13:39:54 +00001613#endif
1614#ifdef ITIMER_VIRTUAL
Christian Heimes6782b142016-09-09 00:11:45 +02001615 if (PyModule_AddIntMacro(m, ITIMER_VIRTUAL))
1616 goto finally;
Martin v. Löwis823725e2008-03-24 13:39:54 +00001617#endif
1618#ifdef ITIMER_PROF
Christian Heimes6782b142016-09-09 00:11:45 +02001619 if (PyModule_AddIntMacro(m, ITIMER_PROF))
1620 goto finally;
Martin v. Löwis823725e2008-03-24 13:39:54 +00001621#endif
1622
1623#if defined (HAVE_SETITIMER) || defined (HAVE_GETITIMER)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001624 ItimerError = PyErr_NewException("signal.ItimerError",
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +03001625 PyExc_OSError, NULL);
animalize77643c42019-09-09 21:46:26 +08001626 if (!ItimerError ||
1627 PyDict_SetItemString(d, "ItimerError", ItimerError) < 0) {
Joannah Nanjekye9541bd32019-04-21 21:47:06 -04001628 goto finally;
animalize77643c42019-09-09 21:46:26 +08001629 }
Martin v. Löwis823725e2008-03-24 13:39:54 +00001630#endif
1631
Brian Curtineb24d742010-04-12 17:16:38 +00001632#ifdef CTRL_C_EVENT
Christian Heimes6782b142016-09-09 00:11:45 +02001633 if (PyModule_AddIntMacro(m, CTRL_C_EVENT))
1634 goto finally;
Brian Curtineb24d742010-04-12 17:16:38 +00001635#endif
1636
1637#ifdef CTRL_BREAK_EVENT
Christian Heimes6782b142016-09-09 00:11:45 +02001638 if (PyModule_AddIntMacro(m, CTRL_BREAK_EVENT))
1639 goto finally;
Brian Curtineb24d742010-04-12 17:16:38 +00001640#endif
1641
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001642#ifdef MS_WINDOWS
1643 /* Create manual-reset event, initially unset */
1644 sigint_event = CreateEvent(NULL, TRUE, FALSE, FALSE);
1645#endif
1646
Martin v. Löwis1a214512008-06-11 05:26:20 +00001647 if (PyErr_Occurred()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001648 Py_DECREF(m);
1649 m = NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001650 }
Barry Warsaw92971171997-01-03 00:14:25 +00001651
Barry Warsaw92971171997-01-03 00:14:25 +00001652 finally:
Martin v. Löwis1a214512008-06-11 05:26:20 +00001653 return m;
Guido van Rossum08c16611997-08-02 03:01:42 +00001654}
1655
1656static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001657finisignal(void)
Guido van Rossum08c16611997-08-02 03:01:42 +00001658{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001659 int i;
1660 PyObject *func;
Guido van Rossum08c16611997-08-02 03:01:42 +00001661
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001662 for (i = 1; i < NSIG; i++) {
1663 func = Handlers[i].func;
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001664 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001665 Handlers[i].func = NULL;
pkerlinge905c842018-06-01 09:47:18 +00001666 if (func != NULL && func != Py_None &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001667 func != DefaultHandler && func != IgnoreHandler)
1668 PyOS_setsig(i, SIG_DFL);
1669 Py_XDECREF(func);
1670 }
Guido van Rossum08c16611997-08-02 03:01:42 +00001671
Serhiy Storchaka505ff752014-02-09 13:33:53 +02001672 Py_CLEAR(IntHandler);
1673 Py_CLEAR(DefaultHandler);
1674 Py_CLEAR(IgnoreHandler);
animalize77643c42019-09-09 21:46:26 +08001675#ifdef HAVE_GETITIMER
1676 Py_CLEAR(ItimerError);
1677#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001678}
1679
Barry Warsaw92971171997-01-03 00:14:25 +00001680
Barry Warsaw92971171997-01-03 00:14:25 +00001681/* Declared in pyerrors.h */
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001682int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001683PyErr_CheckSignals(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001684{
Victor Stinner72818982020-03-26 22:28:11 +01001685 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerb54a99d2020-04-08 23:35:05 +02001686 if (!_Py_ThreadCanHandleSignals(tstate->interp)) {
Eric Snow64d6cc82019-02-23 15:40:43 -07001687 return 0;
1688 }
1689
Victor Stinner72818982020-03-26 22:28:11 +01001690 return _PyErr_CheckSignalsTstate(tstate);
Eric Snow64d6cc82019-02-23 15:40:43 -07001691}
1692
1693
1694/* Declared in cpython/pyerrors.h */
1695int
Victor Stinner72818982020-03-26 22:28:11 +01001696_PyErr_CheckSignalsTstate(PyThreadState *tstate)
Eric Snow64d6cc82019-02-23 15:40:43 -07001697{
Victor Stinner72818982020-03-26 22:28:11 +01001698 if (!_Py_atomic_load(&is_tripped)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001699 return 0;
Victor Stinner72818982020-03-26 22:28:11 +01001700 }
Christian Heimesb76922a2007-12-11 01:06:40 +00001701
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001702 /*
1703 * The is_tripped variable is meant to speed up the calls to
1704 * PyErr_CheckSignals (both directly or via pending calls) when no
1705 * signal has arrived. This variable is set to 1 when a signal arrives
1706 * and it is set to 0 here, when we know some signals arrived. This way
1707 * we can run the registered handlers with no signals blocked.
1708 *
1709 * NOTE: with this approach we can have a situation where is_tripped is
1710 * 1 but we have no more signals to handle (Handlers[i].tripped
1711 * is 0 for every signal i). This won't do us any harm (except
1712 * we're gonna spent some cycles for nothing). This happens when
1713 * we receive a signal i after we zero is_tripped and before we
1714 * check Handlers[i].tripped.
1715 */
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001716 _Py_atomic_store(&is_tripped, 0);
Christian Heimesb76922a2007-12-11 01:06:40 +00001717
Victor Stinner72818982020-03-26 22:28:11 +01001718 PyObject *frame = (PyObject *)tstate->frame;
1719 if (!frame) {
1720 frame = Py_None;
1721 }
Christian Heimesb76922a2007-12-11 01:06:40 +00001722
Victor Stinner72818982020-03-26 22:28:11 +01001723 for (int i = 1; i < NSIG; i++) {
1724 if (!_Py_atomic_load_relaxed(&Handlers[i].tripped)) {
1725 continue;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001726 }
Victor Stinner72818982020-03-26 22:28:11 +01001727 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
1728
1729 PyObject *arglist = Py_BuildValue("(iO)", i, frame);
1730 PyObject *result;
1731 if (arglist) {
1732 result = _PyObject_Call(tstate, Handlers[i].func, arglist, NULL);
1733 Py_DECREF(arglist);
1734 }
1735 else {
1736 result = NULL;
1737 }
1738 if (!result) {
1739 /* On error, re-schedule a call to _PyErr_CheckSignalsTstate() */
1740 _Py_atomic_store(&is_tripped, 1);
1741 return -1;
1742 }
1743
1744 Py_DECREF(result);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001745 }
Christian Heimesb76922a2007-12-11 01:06:40 +00001746
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001747 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001748}
1749
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +00001750
Victor Stinner72818982020-03-26 22:28:11 +01001751
1752int
1753_PyErr_CheckSignals(void)
1754{
1755 PyThreadState *tstate = _PyThreadState_GET();
1756 return _PyErr_CheckSignalsTstate(tstate);
1757}
1758
1759
Matěj Cepl608876b2019-05-23 22:30:00 +02001760/* Simulate the effect of a signal.SIGINT signal arriving. The next time
1761 PyErr_CheckSignals is called, the Python SIGINT signal handler will be
1762 raised.
1763
1764 Missing signal handler for the SIGINT signal is silently ignored. */
Barry Warsaw92971171997-01-03 00:14:25 +00001765void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001766PyErr_SetInterrupt(void)
Barry Warsaw92971171997-01-03 00:14:25 +00001767{
Matěj Cepl608876b2019-05-23 22:30:00 +02001768 if ((Handlers[SIGINT].func != IgnoreHandler) &&
1769 (Handlers[SIGINT].func != DefaultHandler)) {
1770 trip_signal(SIGINT);
1771 }
Barry Warsaw92971171997-01-03 00:14:25 +00001772}
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001773
1774void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001775PyOS_InitInterrupts(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001776{
Giampaolo Rodola'e09fb712014-04-04 15:34:17 +02001777 PyObject *m = PyImport_ImportModule("_signal");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001778 if (m) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001779 Py_DECREF(m);
1780 }
Guido van Rossum08c16611997-08-02 03:01:42 +00001781}
1782
1783void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001784PyOS_FiniInterrupts(void)
Guido van Rossum08c16611997-08-02 03:01:42 +00001785{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001786 finisignal();
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001787}
1788
Victor Stinnerfa7ab6a2020-06-03 14:39:59 +02001789
1790// The caller doesn't have to hold the GIL
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001791int
Victor Stinnerfa7ab6a2020-06-03 14:39:59 +02001792_PyOS_InterruptOccurred(PyThreadState *tstate)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001793{
Victor Stinnercbe12962020-06-01 20:34:15 +02001794 _Py_EnsureTstateNotNULL(tstate);
1795 if (!_Py_ThreadCanHandleSignals(tstate->interp)) {
Victor Stinner72818982020-03-26 22:28:11 +01001796 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001797 }
Victor Stinner72818982020-03-26 22:28:11 +01001798
1799 if (!_Py_atomic_load_relaxed(&Handlers[SIGINT].tripped)) {
1800 return 0;
1801 }
1802
1803 _Py_atomic_store_relaxed(&Handlers[SIGINT].tripped, 0);
1804 return 1;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001805}
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001806
Victor Stinner26881c82020-06-02 15:51:37 +02001807
Victor Stinnerfa7ab6a2020-06-03 14:39:59 +02001808// The caller must to hold the GIL
1809int
1810PyOS_InterruptOccurred(void)
1811{
1812 PyThreadState *tstate = _PyThreadState_GET();
1813 return _PyOS_InterruptOccurred(tstate);
1814}
1815
1816
Victor Stinner26881c82020-06-02 15:51:37 +02001817#ifdef HAVE_FORK
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001818static void
1819_clear_pending_signals(void)
1820{
Victor Stinner26881c82020-06-02 15:51:37 +02001821 if (!_Py_atomic_load(&is_tripped)) {
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001822 return;
Victor Stinner26881c82020-06-02 15:51:37 +02001823 }
1824
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001825 _Py_atomic_store(&is_tripped, 0);
Victor Stinner26881c82020-06-02 15:51:37 +02001826 for (int i = 1; i < NSIG; ++i) {
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001827 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001828 }
1829}
1830
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001831void
Antoine Pitrou346cbd32017-05-27 17:50:54 +02001832_PySignal_AfterFork(void)
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001833{
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001834 /* Clear the signal flags after forking so that they aren't handled
1835 * in both processes if they came in just before the fork() but before
1836 * the interpreter had an opportunity to call the handlers. issue9535. */
1837 _clear_pending_signals();
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001838}
Victor Stinner26881c82020-06-02 15:51:37 +02001839#endif /* HAVE_FORK */
1840
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001841
1842int
1843_PyOS_IsMainThread(void)
1844{
Victor Stinner81a7be32020-04-14 15:14:01 +02001845 PyInterpreterState *interp = _PyInterpreterState_GET();
Victor Stinnerb54a99d2020-04-08 23:35:05 +02001846 return _Py_ThreadCanHandleSignals(interp);
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001847}
1848
1849#ifdef MS_WINDOWS
1850void *_PyOS_SigintEvent(void)
1851{
1852 /* Returns a manual-reset event which gets tripped whenever
1853 SIGINT is received.
1854
1855 Python.h does not include windows.h so we do cannot use HANDLE
1856 as the return type of this function. We use void* instead. */
1857 return sigint_event;
1858}
1859#endif