blob: 0ab3a71b1809949d58f33992accce944cbd81660 [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 }
Pablo Galindof9c5e3f2020-09-02 15:29:12 +0100667#ifdef HAVE_SIGACTION
668 struct sigaction act;
669 (void) sigaction(signalnum, NULL, &act);
670 if (flag) {
671 act.sa_flags &= ~SA_RESTART;
672 }
673 else {
674 act.sa_flags |= SA_RESTART;
675 }
676 if (sigaction(signalnum, &act, NULL) < 0) {
677#else
678 if (siginterrupt(signalnum, flag) < 0) {
679#endif
Victor Stinner388196e2011-05-10 17:13:00 +0200680 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000681 return NULL;
682 }
Tal Einatc7027b72015-05-16 14:14:49 +0300683 Py_RETURN_NONE;
Christian Heimes8640e742008-02-23 16:23:06 +0000684}
685
686#endif
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000687
Tal Einatc7027b72015-05-16 14:14:49 +0300688
689static PyObject*
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800690signal_set_wakeup_fd(PyObject *self, PyObject *args, PyObject *kwds)
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000691{
Victor Stinnere134a7f2015-03-30 10:09:31 +0200692 struct _Py_stat_struct status;
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800693 static char *kwlist[] = {
694 "", "warn_on_full_buffer", NULL,
695 };
696 int warn_on_full_buffer = 1;
Victor Stinner11517102014-07-29 23:31:34 +0200697#ifdef MS_WINDOWS
698 PyObject *fdobj;
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100699 SOCKET_T sockfd, old_sockfd;
Victor Stinner11517102014-07-29 23:31:34 +0200700 int res;
701 int res_size = sizeof res;
702 PyObject *mod;
Victor Stinner11517102014-07-29 23:31:34 +0200703 int is_socket;
704
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800705 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|$p:set_wakeup_fd", kwlist,
706 &fdobj, &warn_on_full_buffer))
Victor Stinner11517102014-07-29 23:31:34 +0200707 return NULL;
708
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100709 sockfd = PyLong_AsSocket_t(fdobj);
710 if (sockfd == (SOCKET_T)(-1) && PyErr_Occurred())
Victor Stinner11517102014-07-29 23:31:34 +0200711 return NULL;
712#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000713 int fd, old_fd;
Victor Stinner11517102014-07-29 23:31:34 +0200714
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800715 if (!PyArg_ParseTupleAndKeywords(args, kwds, "i|$p:set_wakeup_fd", kwlist,
716 &fd, &warn_on_full_buffer))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000717 return NULL;
Victor Stinner11517102014-07-29 23:31:34 +0200718#endif
719
Victor Stinner72818982020-03-26 22:28:11 +0100720 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200721 if (!_Py_ThreadCanHandleSignals(tstate->interp)) {
Victor Stinner72818982020-03-26 22:28:11 +0100722 _PyErr_SetString(tstate, PyExc_ValueError,
723 "set_wakeup_fd only works in main thread "
724 "of the main interpreter");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000725 return NULL;
726 }
Victor Stinner0bffc942014-07-21 16:28:54 +0200727
Victor Stinner11517102014-07-29 23:31:34 +0200728#ifdef MS_WINDOWS
729 is_socket = 0;
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100730 if (sockfd != INVALID_FD) {
Victor Stinner11517102014-07-29 23:31:34 +0200731 /* Import the _socket module to call WSAStartup() */
732 mod = PyImport_ImportModuleNoBlock("_socket");
733 if (mod == NULL)
734 return NULL;
735 Py_DECREF(mod);
736
737 /* test the socket */
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100738 if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR,
Victor Stinner11517102014-07-29 23:31:34 +0200739 (char *)&res, &res_size) != 0) {
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100740 int fd, err;
741
742 err = WSAGetLastError();
Victor Stinner11517102014-07-29 23:31:34 +0200743 if (err != WSAENOTSOCK) {
744 PyErr_SetExcFromWindowsErr(PyExc_OSError, err);
745 return NULL;
746 }
747
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100748 fd = (int)sockfd;
Steve Dower940f33a2016-09-08 11:21:54 -0700749 if ((SOCKET_T)fd != sockfd) {
Victor Stinner72818982020-03-26 22:28:11 +0100750 _PyErr_SetString(tstate, PyExc_ValueError, "invalid fd");
Victor Stinner11517102014-07-29 23:31:34 +0200751 return NULL;
752 }
753
Victor Stinner72818982020-03-26 22:28:11 +0100754 if (_Py_fstat(fd, &status) != 0) {
Victor Stinner11517102014-07-29 23:31:34 +0200755 return NULL;
Victor Stinner72818982020-03-26 22:28:11 +0100756 }
Victor Stinner38227602014-08-27 12:59:44 +0200757
758 /* on Windows, a file cannot be set to non-blocking mode */
Victor Stinner11517102014-07-29 23:31:34 +0200759 }
Victor Stinner38227602014-08-27 12:59:44 +0200760 else {
Victor Stinner11517102014-07-29 23:31:34 +0200761 is_socket = 1;
Victor Stinner38227602014-08-27 12:59:44 +0200762
763 /* Windows does not provide a function to test if a socket
764 is in non-blocking mode */
765 }
Victor Stinner11517102014-07-29 23:31:34 +0200766 }
767
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100768 old_sockfd = wakeup.fd;
769 wakeup.fd = sockfd;
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800770 wakeup.warn_on_full_buffer = warn_on_full_buffer;
Victor Stinner11517102014-07-29 23:31:34 +0200771 wakeup.use_send = is_socket;
772
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100773 if (old_sockfd != INVALID_FD)
774 return PyLong_FromSocket_t(old_sockfd);
Victor Stinner11517102014-07-29 23:31:34 +0200775 else
776 return PyLong_FromLong(-1);
777#else
Victor Stinnerd18ccd12014-07-24 21:58:53 +0200778 if (fd != -1) {
Victor Stinner38227602014-08-27 12:59:44 +0200779 int blocking;
780
Victor Stinnere134a7f2015-03-30 10:09:31 +0200781 if (_Py_fstat(fd, &status) != 0)
Victor Stinner11517102014-07-29 23:31:34 +0200782 return NULL;
Victor Stinner38227602014-08-27 12:59:44 +0200783
784 blocking = _Py_get_blocking(fd);
785 if (blocking < 0)
786 return NULL;
787 if (blocking) {
Victor Stinner72818982020-03-26 22:28:11 +0100788 _PyErr_Format(tstate, PyExc_ValueError,
789 "the fd %i must be in non-blocking mode",
790 fd);
Victor Stinner38227602014-08-27 12:59:44 +0200791 return NULL;
792 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000793 }
Victor Stinner0bffc942014-07-21 16:28:54 +0200794
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800795 old_fd = wakeup.fd;
796 wakeup.fd = fd;
797 wakeup.warn_on_full_buffer = warn_on_full_buffer;
Victor Stinner0bffc942014-07-21 16:28:54 +0200798
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000799 return PyLong_FromLong(old_fd);
Victor Stinner11517102014-07-29 23:31:34 +0200800#endif
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000801}
802
803PyDoc_STRVAR(set_wakeup_fd_doc,
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800804"set_wakeup_fd(fd, *, warn_on_full_buffer=True) -> fd\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000805\n\
Victor Stinner11517102014-07-29 23:31:34 +0200806Sets the fd to be written to (with the signal number) when a signal\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000807comes in. A library can use this to wakeup select or poll.\n\
Victor Stinner11517102014-07-29 23:31:34 +0200808The previous fd or -1 is returned.\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000809\n\
810The fd must be non-blocking.");
811
812/* C API for the same, without all the error checking */
813int
814PySignal_SetWakeupFd(int fd)
815{
Victor Stinner11517102014-07-29 23:31:34 +0200816 int old_fd;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000817 if (fd < 0)
818 fd = -1;
Victor Stinner11517102014-07-29 23:31:34 +0200819
820#ifdef MS_WINDOWS
821 old_fd = Py_SAFE_DOWNCAST(wakeup.fd, SOCKET_T, int);
Victor Stinner11517102014-07-29 23:31:34 +0200822#else
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800823 old_fd = wakeup.fd;
Victor Stinner11517102014-07-29 23:31:34 +0200824#endif
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800825 wakeup.fd = fd;
826 wakeup.warn_on_full_buffer = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000827 return old_fd;
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000828}
829
830
Martin v. Löwis823725e2008-03-24 13:39:54 +0000831#ifdef HAVE_SETITIMER
Tal Einatc7027b72015-05-16 14:14:49 +0300832
833/*[clinic input]
834signal.setitimer
835
836 which: int
Victor Stinneref611c92017-10-13 13:49:43 -0700837 seconds: object
838 interval: object(c_default="NULL") = 0.0
Tal Einatc7027b72015-05-16 14:14:49 +0300839 /
840
841Sets given itimer (one of ITIMER_REAL, ITIMER_VIRTUAL or ITIMER_PROF).
842
843The timer will fire after value seconds and after that every interval seconds.
844The itimer can be cleared by setting seconds to zero.
845
846Returns old values as a tuple: (delay, interval).
847[clinic start generated code]*/
848
Martin v. Löwis823725e2008-03-24 13:39:54 +0000849static PyObject *
Victor Stinneref611c92017-10-13 13:49:43 -0700850signal_setitimer_impl(PyObject *module, int which, PyObject *seconds,
851 PyObject *interval)
852/*[clinic end generated code: output=65f9dcbddc35527b input=de43daf194e6f66f]*/
Martin v. Löwis823725e2008-03-24 13:39:54 +0000853{
Martin v. Löwis823725e2008-03-24 13:39:54 +0000854 struct itimerval new, old;
855
Victor Stinneref611c92017-10-13 13:49:43 -0700856 if (timeval_from_double(seconds, &new.it_value) < 0) {
857 return NULL;
858 }
859 if (timeval_from_double(interval, &new.it_interval) < 0) {
860 return NULL;
861 }
862
Martin v. Löwis823725e2008-03-24 13:39:54 +0000863 /* Let OS check "which" value */
864 if (setitimer(which, &new, &old) != 0) {
Tal Einatc7027b72015-05-16 14:14:49 +0300865 PyErr_SetFromErrno(ItimerError);
866 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000867 }
868
869 return itimer_retval(&old);
870}
871
Martin v. Löwis823725e2008-03-24 13:39:54 +0000872#endif
873
874
875#ifdef HAVE_GETITIMER
Tal Einatc7027b72015-05-16 14:14:49 +0300876
877/*[clinic input]
878signal.getitimer
879
880 which: int
881 /
882
883Returns current value of given itimer.
884[clinic start generated code]*/
885
Martin v. Löwis823725e2008-03-24 13:39:54 +0000886static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300887signal_getitimer_impl(PyObject *module, int which)
888/*[clinic end generated code: output=9e053175d517db40 input=f7d21d38f3490627]*/
Martin v. Löwis823725e2008-03-24 13:39:54 +0000889{
Martin v. Löwis823725e2008-03-24 13:39:54 +0000890 struct itimerval old;
891
Martin v. Löwis823725e2008-03-24 13:39:54 +0000892 if (getitimer(which, &old) != 0) {
Tal Einatc7027b72015-05-16 14:14:49 +0300893 PyErr_SetFromErrno(ItimerError);
894 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000895 }
896
897 return itimer_retval(&old);
898}
899
Martin v. Löwis823725e2008-03-24 13:39:54 +0000900#endif
901
Victor Stinnerb3e72192011-05-08 01:46:11 +0200902#if defined(PYPTHREAD_SIGMASK) || defined(HAVE_SIGPENDING)
Victor Stinner35b300c2011-05-04 13:20:35 +0200903static PyObject*
904sigset_to_set(sigset_t mask)
905{
906 PyObject *signum, *result;
907 int sig;
908
909 result = PySet_New(0);
910 if (result == NULL)
911 return NULL;
912
913 for (sig = 1; sig < NSIG; sig++) {
914 if (sigismember(&mask, sig) != 1)
915 continue;
916
917 /* Handle the case where it is a member by adding the signal to
918 the result list. Ignore the other cases because they mean the
919 signal isn't a member of the mask or the signal was invalid,
920 and an invalid signal must have been our fault in constructing
921 the loop boundaries. */
922 signum = PyLong_FromLong(sig);
923 if (signum == NULL) {
924 Py_DECREF(result);
925 return NULL;
926 }
927 if (PySet_Add(result, signum) == -1) {
928 Py_DECREF(signum);
929 Py_DECREF(result);
930 return NULL;
931 }
932 Py_DECREF(signum);
933 }
934 return result;
935}
Victor Stinnerb3e72192011-05-08 01:46:11 +0200936#endif
Victor Stinner35b300c2011-05-04 13:20:35 +0200937
Victor Stinnerb3e72192011-05-08 01:46:11 +0200938#ifdef PYPTHREAD_SIGMASK
Tal Einatc7027b72015-05-16 14:14:49 +0300939
940/*[clinic input]
941signal.pthread_sigmask
942
943 how: int
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300944 mask: sigset_t
Tal Einatc7027b72015-05-16 14:14:49 +0300945 /
946
947Fetch and/or change the signal mask of the calling thread.
948[clinic start generated code]*/
949
Victor Stinnera9293352011-04-30 15:21:58 +0200950static PyObject *
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300951signal_pthread_sigmask_impl(PyObject *module, int how, sigset_t mask)
952/*[clinic end generated code: output=0562c0fb192981a8 input=85bcebda442fa77f]*/
Victor Stinnera9293352011-04-30 15:21:58 +0200953{
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300954 sigset_t previous;
Victor Stinnera9293352011-04-30 15:21:58 +0200955 int err;
956
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300957 err = pthread_sigmask(how, &mask, &previous);
Victor Stinnera9293352011-04-30 15:21:58 +0200958 if (err != 0) {
959 errno = err;
Victor Stinnerb3e72192011-05-08 01:46:11 +0200960 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnera9293352011-04-30 15:21:58 +0200961 return NULL;
962 }
963
Victor Stinnerd0e516d2011-05-03 14:57:12 +0200964 /* if signals was unblocked, signal handlers have been called */
965 if (PyErr_CheckSignals())
966 return NULL;
967
Victor Stinner35b300c2011-05-04 13:20:35 +0200968 return sigset_to_set(previous);
Victor Stinnera9293352011-04-30 15:21:58 +0200969}
970
Victor Stinnera9293352011-04-30 15:21:58 +0200971#endif /* #ifdef PYPTHREAD_SIGMASK */
972
Martin v. Löwis823725e2008-03-24 13:39:54 +0000973
Victor Stinnerb3e72192011-05-08 01:46:11 +0200974#ifdef HAVE_SIGPENDING
Tal Einatc7027b72015-05-16 14:14:49 +0300975
976/*[clinic input]
977signal.sigpending
978
979Examine pending signals.
980
981Returns a set of signal numbers that are pending for delivery to
982the calling thread.
983[clinic start generated code]*/
984
Victor Stinnerb3e72192011-05-08 01:46:11 +0200985static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300986signal_sigpending_impl(PyObject *module)
987/*[clinic end generated code: output=53375ffe89325022 input=e0036c016f874e29]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +0200988{
989 int err;
990 sigset_t mask;
991 err = sigpending(&mask);
992 if (err)
993 return PyErr_SetFromErrno(PyExc_OSError);
994 return sigset_to_set(mask);
995}
996
Victor Stinnerb3e72192011-05-08 01:46:11 +0200997#endif /* #ifdef HAVE_SIGPENDING */
998
999
1000#ifdef HAVE_SIGWAIT
Tal Einatc7027b72015-05-16 14:14:49 +03001001
1002/*[clinic input]
1003signal.sigwait
1004
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001005 sigset: sigset_t
Tal Einatc7027b72015-05-16 14:14:49 +03001006 /
1007
1008Wait for a signal.
1009
1010Suspend execution of the calling thread until the delivery of one of the
1011signals specified in the signal set sigset. The function accepts the signal
1012and returns the signal number.
1013[clinic start generated code]*/
1014
Victor Stinnerb3e72192011-05-08 01:46:11 +02001015static PyObject *
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001016signal_sigwait_impl(PyObject *module, sigset_t sigset)
1017/*[clinic end generated code: output=f43770699d682f96 input=a6fbd47b1086d119]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +02001018{
Victor Stinnerb3e72192011-05-08 01:46:11 +02001019 int err, signum;
1020
Victor Stinner10c30d62011-06-10 01:39:53 +02001021 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001022 err = sigwait(&sigset, &signum);
Victor Stinner10c30d62011-06-10 01:39:53 +02001023 Py_END_ALLOW_THREADS
Victor Stinnerb3e72192011-05-08 01:46:11 +02001024 if (err) {
1025 errno = err;
1026 return PyErr_SetFromErrno(PyExc_OSError);
1027 }
1028
1029 return PyLong_FromLong(signum);
1030}
1031
Tal Einatc7027b72015-05-16 14:14:49 +03001032#endif /* #ifdef HAVE_SIGWAIT */
1033
Victor Stinnerb3e72192011-05-08 01:46:11 +02001034
Antoine Pitrou9d3627e2018-05-04 13:00:50 +02001035#if defined(HAVE_SIGFILLSET) || defined(MS_WINDOWS)
1036
1037/*[clinic input]
1038signal.valid_signals
1039
1040Return a set of valid signal numbers on this platform.
1041
1042The signal numbers returned by this function can be safely passed to
1043functions like `pthread_sigmask`.
1044[clinic start generated code]*/
1045
1046static PyObject *
1047signal_valid_signals_impl(PyObject *module)
1048/*[clinic end generated code: output=1609cffbcfcf1314 input=86a3717ff25288f2]*/
1049{
1050#ifdef MS_WINDOWS
1051#ifdef SIGBREAK
1052 PyObject *tup = Py_BuildValue("(iiiiiii)", SIGABRT, SIGBREAK, SIGFPE,
1053 SIGILL, SIGINT, SIGSEGV, SIGTERM);
1054#else
1055 PyObject *tup = Py_BuildValue("(iiiiii)", SIGABRT, SIGFPE, SIGILL,
1056 SIGINT, SIGSEGV, SIGTERM);
1057#endif
1058 if (tup == NULL) {
1059 return NULL;
1060 }
1061 PyObject *set = PySet_New(tup);
1062 Py_DECREF(tup);
1063 return set;
1064#else
1065 sigset_t mask;
1066 if (sigemptyset(&mask) || sigfillset(&mask)) {
1067 return PyErr_SetFromErrno(PyExc_OSError);
1068 }
1069 return sigset_to_set(mask);
1070#endif
1071}
1072
1073#endif /* #if defined(HAVE_SIGFILLSET) || defined(MS_WINDOWS) */
1074
1075
Ross Lagerwallbc808222011-06-25 12:13:40 +02001076#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
1077static int initialized;
1078static PyStructSequence_Field struct_siginfo_fields[] = {
1079 {"si_signo", "signal number"},
1080 {"si_code", "signal code"},
1081 {"si_errno", "errno associated with this signal"},
1082 {"si_pid", "sending process ID"},
1083 {"si_uid", "real user ID of sending process"},
1084 {"si_status", "exit value or signal"},
1085 {"si_band", "band event for SIGPOLL"},
1086 {0}
1087};
1088
1089PyDoc_STRVAR(struct_siginfo__doc__,
1090"struct_siginfo: Result from sigwaitinfo or sigtimedwait.\n\n\
1091This object may be accessed either as a tuple of\n\
1092(si_signo, si_code, si_errno, si_pid, si_uid, si_status, si_band),\n\
1093or via the attributes si_signo, si_code, and so on.");
1094
1095static PyStructSequence_Desc struct_siginfo_desc = {
1096 "signal.struct_siginfo", /* name */
1097 struct_siginfo__doc__, /* doc */
1098 struct_siginfo_fields, /* fields */
1099 7 /* n_in_sequence */
1100};
1101
1102static PyTypeObject SiginfoType;
1103
1104static PyObject *
1105fill_siginfo(siginfo_t *si)
1106{
1107 PyObject *result = PyStructSequence_New(&SiginfoType);
1108 if (!result)
1109 return NULL;
1110
1111 PyStructSequence_SET_ITEM(result, 0, PyLong_FromLong((long)(si->si_signo)));
1112 PyStructSequence_SET_ITEM(result, 1, PyLong_FromLong((long)(si->si_code)));
Victor Stinner09532fe2019-05-10 23:39:09 +02001113#ifdef __VXWORKS__
pxinwr8b5bdda2019-03-14 01:18:25 +08001114 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong(0L));
1115 PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong(0L));
1116 PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong(0L));
1117 PyStructSequence_SET_ITEM(result, 5, PyLong_FromLong(0L));
Victor Stinner09532fe2019-05-10 23:39:09 +02001118#else
Ross Lagerwallbc808222011-06-25 12:13:40 +02001119 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si->si_errno)));
1120 PyStructSequence_SET_ITEM(result, 3, PyLong_FromPid(si->si_pid));
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02001121 PyStructSequence_SET_ITEM(result, 4, _PyLong_FromUid(si->si_uid));
Ross Lagerwallbc808222011-06-25 12:13:40 +02001122 PyStructSequence_SET_ITEM(result, 5,
1123 PyLong_FromLong((long)(si->si_status)));
Victor Stinner09532fe2019-05-10 23:39:09 +02001124#endif
Zachary Ware6a6967e2016-10-01 00:47:27 -05001125#ifdef HAVE_SIGINFO_T_SI_BAND
Ross Lagerwallbc808222011-06-25 12:13:40 +02001126 PyStructSequence_SET_ITEM(result, 6, PyLong_FromLong(si->si_band));
Zachary Ware6a6967e2016-10-01 00:47:27 -05001127#else
1128 PyStructSequence_SET_ITEM(result, 6, PyLong_FromLong(0L));
1129#endif
Ross Lagerwallbc808222011-06-25 12:13:40 +02001130 if (PyErr_Occurred()) {
1131 Py_DECREF(result);
1132 return NULL;
1133 }
1134
1135 return result;
1136}
1137#endif
1138
1139#ifdef HAVE_SIGWAITINFO
Tal Einatc7027b72015-05-16 14:14:49 +03001140
1141/*[clinic input]
1142signal.sigwaitinfo
1143
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001144 sigset: sigset_t
Tal Einatc7027b72015-05-16 14:14:49 +03001145 /
1146
1147Wait synchronously until one of the signals in *sigset* is delivered.
1148
1149Returns a struct_siginfo containing information about the signal.
1150[clinic start generated code]*/
1151
Ross Lagerwallbc808222011-06-25 12:13:40 +02001152static PyObject *
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001153signal_sigwaitinfo_impl(PyObject *module, sigset_t sigset)
1154/*[clinic end generated code: output=1eb2f1fa236fdbca input=3d1a7e1f27fc664c]*/
Ross Lagerwallbc808222011-06-25 12:13:40 +02001155{
Ross Lagerwallbc808222011-06-25 12:13:40 +02001156 siginfo_t si;
1157 int err;
Victor Stinnera453cd82015-03-20 12:54:28 +01001158 int async_err = 0;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001159
Victor Stinnera453cd82015-03-20 12:54:28 +01001160 do {
1161 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001162 err = sigwaitinfo(&sigset, &si);
Victor Stinnera453cd82015-03-20 12:54:28 +01001163 Py_END_ALLOW_THREADS
1164 } while (err == -1
1165 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Ross Lagerwallbc808222011-06-25 12:13:40 +02001166 if (err == -1)
Victor Stinnera453cd82015-03-20 12:54:28 +01001167 return (!async_err) ? PyErr_SetFromErrno(PyExc_OSError) : NULL;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001168
1169 return fill_siginfo(&si);
1170}
1171
Ross Lagerwallbc808222011-06-25 12:13:40 +02001172#endif /* #ifdef HAVE_SIGWAITINFO */
1173
1174#ifdef HAVE_SIGTIMEDWAIT
Tal Einatc7027b72015-05-16 14:14:49 +03001175
1176/*[clinic input]
1177signal.sigtimedwait
1178
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001179 sigset: sigset_t
Serhiy Storchaka6b680cd2015-05-16 15:57:56 +03001180 timeout as timeout_obj: object
Tal Einatc7027b72015-05-16 14:14:49 +03001181 /
1182
1183Like sigwaitinfo(), but with a timeout.
1184
1185The timeout is specified in seconds, with floating point numbers allowed.
1186[clinic start generated code]*/
1187
Ross Lagerwallbc808222011-06-25 12:13:40 +02001188static PyObject *
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001189signal_sigtimedwait_impl(PyObject *module, sigset_t sigset,
Serhiy Storchaka6b680cd2015-05-16 15:57:56 +03001190 PyObject *timeout_obj)
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001191/*[clinic end generated code: output=59c8971e8ae18a64 input=87fd39237cf0b7ba]*/
Ross Lagerwallbc808222011-06-25 12:13:40 +02001192{
Victor Stinnera453cd82015-03-20 12:54:28 +01001193 struct timespec ts;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001194 siginfo_t si;
Victor Stinnera453cd82015-03-20 12:54:28 +01001195 int res;
Victor Stinner34dc0f42015-03-27 18:19:03 +01001196 _PyTime_t timeout, deadline, monotonic;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001197
Victor Stinner869e1772015-03-30 03:49:14 +02001198 if (_PyTime_FromSecondsObject(&timeout,
1199 timeout_obj, _PyTime_ROUND_CEILING) < 0)
Ross Lagerwallbc808222011-06-25 12:13:40 +02001200 return NULL;
1201
Victor Stinnera453cd82015-03-20 12:54:28 +01001202 if (timeout < 0) {
Ross Lagerwallbc808222011-06-25 12:13:40 +02001203 PyErr_SetString(PyExc_ValueError, "timeout must be non-negative");
1204 return NULL;
1205 }
1206
Victor Stinner34dc0f42015-03-27 18:19:03 +01001207 deadline = _PyTime_GetMonotonicClock() + timeout;
Victor Stinnera453cd82015-03-20 12:54:28 +01001208
1209 do {
Victor Stinner34dc0f42015-03-27 18:19:03 +01001210 if (_PyTime_AsTimespec(timeout, &ts) < 0)
1211 return NULL;
Victor Stinnera453cd82015-03-20 12:54:28 +01001212
1213 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001214 res = sigtimedwait(&sigset, &si, &ts);
Victor Stinnera453cd82015-03-20 12:54:28 +01001215 Py_END_ALLOW_THREADS
1216
1217 if (res != -1)
1218 break;
1219
1220 if (errno != EINTR) {
1221 if (errno == EAGAIN)
1222 Py_RETURN_NONE;
1223 else
1224 return PyErr_SetFromErrno(PyExc_OSError);
1225 }
1226
1227 /* sigtimedwait() was interrupted by a signal (EINTR) */
1228 if (PyErr_CheckSignals())
1229 return NULL;
1230
Victor Stinner34dc0f42015-03-27 18:19:03 +01001231 monotonic = _PyTime_GetMonotonicClock();
1232 timeout = deadline - monotonic;
Victor Stinner6aa446c2015-03-30 21:33:51 +02001233 if (timeout < 0)
Victor Stinnera453cd82015-03-20 12:54:28 +01001234 break;
1235 } while (1);
Ross Lagerwallbc808222011-06-25 12:13:40 +02001236
1237 return fill_siginfo(&si);
1238}
1239
Ross Lagerwallbc808222011-06-25 12:13:40 +02001240#endif /* #ifdef HAVE_SIGTIMEDWAIT */
1241
Victor Stinnerb3e72192011-05-08 01:46:11 +02001242
Antoine Pitroua6a4dc82017-09-07 18:56:24 +02001243#if defined(HAVE_PTHREAD_KILL)
Tal Einatc7027b72015-05-16 14:14:49 +03001244
1245/*[clinic input]
1246signal.pthread_kill
1247
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +02001248 thread_id: unsigned_long(bitwise=True)
Tal Einatc7027b72015-05-16 14:14:49 +03001249 signalnum: int
1250 /
1251
1252Send a signal to a thread.
1253[clinic start generated code]*/
1254
Victor Stinnerb3e72192011-05-08 01:46:11 +02001255static PyObject *
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +02001256signal_pthread_kill_impl(PyObject *module, unsigned long thread_id,
1257 int signalnum)
1258/*[clinic end generated code: output=7629919b791bc27f input=1d901f2c7bb544ff]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +02001259{
Victor Stinnerb3e72192011-05-08 01:46:11 +02001260 int err;
1261
Saiyang Gou7514f4f2020-02-12 23:47:42 -08001262 if (PySys_Audit("signal.pthread_kill", "ki", thread_id, signalnum) < 0) {
1263 return NULL;
1264 }
1265
Tal Einatc7027b72015-05-16 14:14:49 +03001266 err = pthread_kill((pthread_t)thread_id, signalnum);
Victor Stinnerb3e72192011-05-08 01:46:11 +02001267 if (err != 0) {
1268 errno = err;
1269 PyErr_SetFromErrno(PyExc_OSError);
1270 return NULL;
1271 }
1272
1273 /* the signal may have been send to the current thread */
1274 if (PyErr_CheckSignals())
1275 return NULL;
1276
1277 Py_RETURN_NONE;
1278}
1279
Antoine Pitroua6a4dc82017-09-07 18:56:24 +02001280#endif /* #if defined(HAVE_PTHREAD_KILL) */
Victor Stinnerb3e72192011-05-08 01:46:11 +02001281
1282
Benjamin Peterson74834512019-11-19 20:39:14 -08001283#if defined(__linux__) && defined(__NR_pidfd_send_signal)
1284/*[clinic input]
1285signal.pidfd_send_signal
1286
1287 pidfd: int
1288 signalnum: int
1289 siginfo: object = None
1290 flags: int = 0
1291 /
1292
1293Send a signal to a process referred to by a pid file descriptor.
1294[clinic start generated code]*/
1295
1296static PyObject *
1297signal_pidfd_send_signal_impl(PyObject *module, int pidfd, int signalnum,
1298 PyObject *siginfo, int flags)
1299/*[clinic end generated code: output=2d59f04a75d9cbdf input=2a6543a1f4ac2000]*/
1300
1301{
1302 if (siginfo != Py_None) {
1303 PyErr_SetString(PyExc_TypeError, "siginfo must be None");
1304 return NULL;
1305 }
1306 if (syscall(__NR_pidfd_send_signal, pidfd, signalnum, NULL, flags) < 0) {
1307 PyErr_SetFromErrno(PyExc_OSError);
1308 return NULL;
1309 }
1310 Py_RETURN_NONE;
1311}
1312#endif
1313
1314
Victor Stinnerb3e72192011-05-08 01:46:11 +02001315
Tal Einatc7027b72015-05-16 14:14:49 +03001316/* List of functions defined in the module -- some of the methoddefs are
1317 defined to nothing if the corresponding C function is not available. */
Barry Warsaw92971171997-01-03 00:14:25 +00001318static PyMethodDef signal_methods[] = {
Serhiy Storchakab0689ae2020-07-12 19:15:20 +03001319 SIGNAL_DEFAULT_INT_HANDLER_METHODDEF
Tal Einatc7027b72015-05-16 14:14:49 +03001320 SIGNAL_ALARM_METHODDEF
1321 SIGNAL_SETITIMER_METHODDEF
1322 SIGNAL_GETITIMER_METHODDEF
1323 SIGNAL_SIGNAL_METHODDEF
Vladimir Matveevc24c6c22019-01-08 01:58:25 -08001324 SIGNAL_RAISE_SIGNAL_METHODDEF
Antoine Pietri5d2a27d2018-03-12 14:42:34 +01001325 SIGNAL_STRSIGNAL_METHODDEF
Tal Einatc7027b72015-05-16 14:14:49 +03001326 SIGNAL_GETSIGNAL_METHODDEF
Serhiy Storchaka62be7422018-11-27 13:27:31 +02001327 {"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 +03001328 SIGNAL_SIGINTERRUPT_METHODDEF
1329 SIGNAL_PAUSE_METHODDEF
Benjamin Peterson74834512019-11-19 20:39:14 -08001330 SIGNAL_PIDFD_SEND_SIGNAL_METHODDEF
Tal Einatc7027b72015-05-16 14:14:49 +03001331 SIGNAL_PTHREAD_KILL_METHODDEF
1332 SIGNAL_PTHREAD_SIGMASK_METHODDEF
1333 SIGNAL_SIGPENDING_METHODDEF
1334 SIGNAL_SIGWAIT_METHODDEF
1335 SIGNAL_SIGWAITINFO_METHODDEF
1336 SIGNAL_SIGTIMEDWAIT_METHODDEF
Antoine Pitrou9d3627e2018-05-04 13:00:50 +02001337#if defined(HAVE_SIGFILLSET) || defined(MS_WINDOWS)
1338 SIGNAL_VALID_SIGNALS_METHODDEF
1339#endif
Tal Einatc7027b72015-05-16 14:14:49 +03001340 {NULL, NULL} /* sentinel */
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001341};
1342
Barry Warsaw92971171997-01-03 00:14:25 +00001343
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001344PyDoc_STRVAR(module_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001345"This module provides mechanisms to use signal handlers in Python.\n\
1346\n\
1347Functions:\n\
1348\n\
1349alarm() -- cause SIGALRM after a specified time [Unix only]\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001350setitimer() -- cause a signal (described below) after a specified\n\
1351 float time and the timer may restart then [Unix only]\n\
1352getitimer() -- get current value of timer [Unix only]\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001353signal() -- set the action for a given signal\n\
1354getsignal() -- get the signal action for a given signal\n\
1355pause() -- wait until a signal arrives [Unix only]\n\
1356default_int_handler() -- default SIGINT handler\n\
1357\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001358signal constants:\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001359SIG_DFL -- used to refer to the system default handler\n\
1360SIG_IGN -- used to ignore the signal\n\
1361NSIG -- number of defined signals\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001362SIGINT, SIGTERM, etc. -- signal numbers\n\
1363\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001364itimer constants:\n\
1365ITIMER_REAL -- decrements in real time, and delivers SIGALRM upon\n\
1366 expiration\n\
1367ITIMER_VIRTUAL -- decrements only when the process is executing,\n\
1368 and delivers SIGVTALRM upon expiration\n\
1369ITIMER_PROF -- decrements both when the process is executing and\n\
1370 when the system is executing on behalf of the process.\n\
1371 Coupled with ITIMER_VIRTUAL, this timer is usually\n\
1372 used to profile the time spent by the application\n\
1373 in user and kernel space. SIGPROF is delivered upon\n\
1374 expiration.\n\
1375\n\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001376*** IMPORTANT NOTICE ***\n\
1377A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001378the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001379
Martin v. Löwis1a214512008-06-11 05:26:20 +00001380
Mohamed Koubaa71d1bd92020-09-03 03:21:06 -05001381
1382static int
1383signal_exec(PyObject *m)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001384{
Mohamed Koubaa71d1bd92020-09-03 03:21:06 -05001385 /* add the functions */
Ross Lagerwallbc808222011-06-25 12:13:40 +02001386#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
Victor Stinner1c8f0592013-07-22 22:24:54 +02001387 if (!initialized) {
Mohamed Koubaa71d1bd92020-09-03 03:21:06 -05001388 if (PyStructSequence_InitType2(&SiginfoType, &struct_siginfo_desc) < 0) {
1389 return -1;
1390 }
Victor Stinner1c8f0592013-07-22 22:24:54 +02001391 }
Mohamed Koubaa71d1bd92020-09-03 03:21:06 -05001392
1393 if (PyModule_AddType(m, &SiginfoType) < 0) {
1394 return -1;
1395 }
Ross Lagerwallbc808222011-06-25 12:13:40 +02001396 initialized = 1;
1397#endif
1398
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001399 /* Add some symbolic constants to the module */
Mohamed Koubaa71d1bd92020-09-03 03:21:06 -05001400 PyObject *d = PyModule_GetDict(m);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001401
animalize77643c42019-09-09 21:46:26 +08001402 DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL);
1403 if (!DefaultHandler ||
1404 PyDict_SetItemString(d, "SIG_DFL", DefaultHandler) < 0) {
Mohamed Koubaa71d1bd92020-09-03 03:21:06 -05001405 return -1;
animalize77643c42019-09-09 21:46:26 +08001406 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001407
animalize77643c42019-09-09 21:46:26 +08001408 IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN);
1409 if (!IgnoreHandler ||
1410 PyDict_SetItemString(d, "SIG_IGN", IgnoreHandler) < 0) {
Mohamed Koubaa71d1bd92020-09-03 03:21:06 -05001411 return -1;
animalize77643c42019-09-09 21:46:26 +08001412 }
Barry Warsaw92971171997-01-03 00:14:25 +00001413
Joannah Nanjekye9541bd32019-04-21 21:47:06 -04001414 if (PyModule_AddIntMacro(m, NSIG))
Mohamed Koubaa71d1bd92020-09-03 03:21:06 -05001415 return -1;
Barry Warsaw92971171997-01-03 00:14:25 +00001416
Victor Stinnera9293352011-04-30 15:21:58 +02001417#ifdef SIG_BLOCK
Victor Stinner72c53b52011-05-02 16:15:43 +02001418 if (PyModule_AddIntMacro(m, SIG_BLOCK))
Mohamed Koubaa71d1bd92020-09-03 03:21:06 -05001419 return -1;
Victor Stinnera9293352011-04-30 15:21:58 +02001420#endif
Victor Stinnera9293352011-04-30 15:21:58 +02001421#ifdef SIG_UNBLOCK
Victor Stinner72c53b52011-05-02 16:15:43 +02001422 if (PyModule_AddIntMacro(m, SIG_UNBLOCK))
Mohamed Koubaa71d1bd92020-09-03 03:21:06 -05001423 return -1;
Victor Stinnera9293352011-04-30 15:21:58 +02001424#endif
Victor Stinnera9293352011-04-30 15:21:58 +02001425#ifdef SIG_SETMASK
Victor Stinner72c53b52011-05-02 16:15:43 +02001426 if (PyModule_AddIntMacro(m, SIG_SETMASK))
Mohamed Koubaa71d1bd92020-09-03 03:21:06 -05001427 return -1;
Victor Stinnera9293352011-04-30 15:21:58 +02001428#endif
1429
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02001430 IntHandler = PyMapping_GetItemString(d, "default_int_handler");
animalize77643c42019-09-09 21:46:26 +08001431 if (!IntHandler)
Mohamed Koubaa71d1bd92020-09-03 03:21:06 -05001432 return -1;
Barry Warsaw92971171997-01-03 00:14:25 +00001433
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001434 _Py_atomic_store_relaxed(&Handlers[0].tripped, 0);
Mohamed Koubaa71d1bd92020-09-03 03:21:06 -05001435 for (int i = 1; i < NSIG; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001436 void (*t)(int);
1437 t = PyOS_getsig(i);
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001438 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001439 if (t == SIG_DFL)
1440 Handlers[i].func = DefaultHandler;
1441 else if (t == SIG_IGN)
1442 Handlers[i].func = IgnoreHandler;
1443 else
1444 Handlers[i].func = Py_None; /* None of our business */
1445 Py_INCREF(Handlers[i].func);
1446 }
1447 if (Handlers[SIGINT].func == DefaultHandler) {
1448 /* Install default int handler */
1449 Py_INCREF(IntHandler);
Serhiy Storchaka57a01d32016-04-10 18:05:40 +03001450 Py_SETREF(Handlers[SIGINT].func, IntHandler);
pkerlinge905c842018-06-01 09:47:18 +00001451 PyOS_setsig(SIGINT, signal_handler);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001452 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001453
1454#ifdef SIGHUP
Christian Heimes6782b142016-09-09 00:11:45 +02001455 if (PyModule_AddIntMacro(m, SIGHUP))
Mohamed Koubaa71d1bd92020-09-03 03:21:06 -05001456 return -1;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001457#endif
1458#ifdef SIGINT
Christian Heimes6782b142016-09-09 00:11:45 +02001459 if (PyModule_AddIntMacro(m, SIGINT))
Mohamed Koubaa71d1bd92020-09-03 03:21:06 -05001460 return -1;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001461#endif
Tim Peters1ce3cf72001-10-01 17:58:40 +00001462#ifdef SIGBREAK
Christian Heimes6782b142016-09-09 00:11:45 +02001463 if (PyModule_AddIntMacro(m, SIGBREAK))
Mohamed Koubaa71d1bd92020-09-03 03:21:06 -05001464 return -1;
Tim Peters1ce3cf72001-10-01 17:58:40 +00001465#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001466#ifdef SIGQUIT
Christian Heimes6782b142016-09-09 00:11:45 +02001467 if (PyModule_AddIntMacro(m, SIGQUIT))
Mohamed Koubaa71d1bd92020-09-03 03:21:06 -05001468 return -1;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001469#endif
1470#ifdef SIGILL
Christian Heimes6782b142016-09-09 00:11:45 +02001471 if (PyModule_AddIntMacro(m, SIGILL))
Mohamed Koubaa71d1bd92020-09-03 03:21:06 -05001472 return -1;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001473#endif
1474#ifdef SIGTRAP
Christian Heimes6782b142016-09-09 00:11:45 +02001475 if (PyModule_AddIntMacro(m, SIGTRAP))
Mohamed Koubaa71d1bd92020-09-03 03:21:06 -05001476 return -1;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001477#endif
1478#ifdef SIGIOT
Christian Heimes6782b142016-09-09 00:11:45 +02001479 if (PyModule_AddIntMacro(m, SIGIOT))
Mohamed Koubaa71d1bd92020-09-03 03:21:06 -05001480 return -1;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001481#endif
1482#ifdef SIGABRT
Christian Heimes6782b142016-09-09 00:11:45 +02001483 if (PyModule_AddIntMacro(m, SIGABRT))
Mohamed Koubaa71d1bd92020-09-03 03:21:06 -05001484 return -1;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001485#endif
1486#ifdef SIGEMT
Christian Heimes6782b142016-09-09 00:11:45 +02001487 if (PyModule_AddIntMacro(m, SIGEMT))
Mohamed Koubaa71d1bd92020-09-03 03:21:06 -05001488 return -1;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001489#endif
1490#ifdef SIGFPE
Christian Heimes6782b142016-09-09 00:11:45 +02001491 if (PyModule_AddIntMacro(m, SIGFPE))
Mohamed Koubaa71d1bd92020-09-03 03:21:06 -05001492 return -1;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001493#endif
1494#ifdef SIGKILL
Christian Heimes6782b142016-09-09 00:11:45 +02001495 if (PyModule_AddIntMacro(m, SIGKILL))
Mohamed Koubaa71d1bd92020-09-03 03:21:06 -05001496 return -1;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001497#endif
1498#ifdef SIGBUS
Christian Heimes6782b142016-09-09 00:11:45 +02001499 if (PyModule_AddIntMacro(m, SIGBUS))
Mohamed Koubaa71d1bd92020-09-03 03:21:06 -05001500 return -1;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001501#endif
1502#ifdef SIGSEGV
Christian Heimes6782b142016-09-09 00:11:45 +02001503 if (PyModule_AddIntMacro(m, SIGSEGV))
Mohamed Koubaa71d1bd92020-09-03 03:21:06 -05001504 return -1;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001505#endif
1506#ifdef SIGSYS
Christian Heimes6782b142016-09-09 00:11:45 +02001507 if (PyModule_AddIntMacro(m, SIGSYS))
Mohamed Koubaa71d1bd92020-09-03 03:21:06 -05001508 return -1;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001509#endif
1510#ifdef SIGPIPE
Christian Heimes6782b142016-09-09 00:11:45 +02001511 if (PyModule_AddIntMacro(m, SIGPIPE))
Mohamed Koubaa71d1bd92020-09-03 03:21:06 -05001512 return -1;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001513#endif
1514#ifdef SIGALRM
Christian Heimes6782b142016-09-09 00:11:45 +02001515 if (PyModule_AddIntMacro(m, SIGALRM))
Mohamed Koubaa71d1bd92020-09-03 03:21:06 -05001516 return -1;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001517#endif
1518#ifdef SIGTERM
Christian Heimes6782b142016-09-09 00:11:45 +02001519 if (PyModule_AddIntMacro(m, SIGTERM))
Mohamed Koubaa71d1bd92020-09-03 03:21:06 -05001520 return -1;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001521#endif
1522#ifdef SIGUSR1
Christian Heimes6782b142016-09-09 00:11:45 +02001523 if (PyModule_AddIntMacro(m, SIGUSR1))
Mohamed Koubaa71d1bd92020-09-03 03:21:06 -05001524 return -1;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001525#endif
1526#ifdef SIGUSR2
Christian Heimes6782b142016-09-09 00:11:45 +02001527 if (PyModule_AddIntMacro(m, SIGUSR2))
Mohamed Koubaa71d1bd92020-09-03 03:21:06 -05001528 return -1;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001529#endif
1530#ifdef SIGCLD
Christian Heimes6782b142016-09-09 00:11:45 +02001531 if (PyModule_AddIntMacro(m, SIGCLD))
Mohamed Koubaa71d1bd92020-09-03 03:21:06 -05001532 return -1;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001533#endif
1534#ifdef SIGCHLD
Christian Heimes6782b142016-09-09 00:11:45 +02001535 if (PyModule_AddIntMacro(m, SIGCHLD))
Mohamed Koubaa71d1bd92020-09-03 03:21:06 -05001536 return -1;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001537#endif
1538#ifdef SIGPWR
Christian Heimes6782b142016-09-09 00:11:45 +02001539 if (PyModule_AddIntMacro(m, SIGPWR))
Mohamed Koubaa71d1bd92020-09-03 03:21:06 -05001540 return -1;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001541#endif
1542#ifdef SIGIO
Christian Heimes6782b142016-09-09 00:11:45 +02001543 if (PyModule_AddIntMacro(m, SIGIO))
Mohamed Koubaa71d1bd92020-09-03 03:21:06 -05001544 return -1;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001545#endif
1546#ifdef SIGURG
Christian Heimes6782b142016-09-09 00:11:45 +02001547 if (PyModule_AddIntMacro(m, SIGURG))
Mohamed Koubaa71d1bd92020-09-03 03:21:06 -05001548 return -1;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001549#endif
1550#ifdef SIGWINCH
Christian Heimes6782b142016-09-09 00:11:45 +02001551 if (PyModule_AddIntMacro(m, SIGWINCH))
Mohamed Koubaa71d1bd92020-09-03 03:21:06 -05001552 return -1;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001553#endif
1554#ifdef SIGPOLL
Christian Heimes6782b142016-09-09 00:11:45 +02001555 if (PyModule_AddIntMacro(m, SIGPOLL))
Mohamed Koubaa71d1bd92020-09-03 03:21:06 -05001556 return -1;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001557#endif
1558#ifdef SIGSTOP
Christian Heimes6782b142016-09-09 00:11:45 +02001559 if (PyModule_AddIntMacro(m, SIGSTOP))
Mohamed Koubaa71d1bd92020-09-03 03:21:06 -05001560 return -1;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001561#endif
1562#ifdef SIGTSTP
Christian Heimes6782b142016-09-09 00:11:45 +02001563 if (PyModule_AddIntMacro(m, SIGTSTP))
Mohamed Koubaa71d1bd92020-09-03 03:21:06 -05001564 return -1;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001565#endif
1566#ifdef SIGCONT
Christian Heimes6782b142016-09-09 00:11:45 +02001567 if (PyModule_AddIntMacro(m, SIGCONT))
Mohamed Koubaa71d1bd92020-09-03 03:21:06 -05001568 return -1;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001569#endif
1570#ifdef SIGTTIN
Christian Heimes6782b142016-09-09 00:11:45 +02001571 if (PyModule_AddIntMacro(m, SIGTTIN))
Mohamed Koubaa71d1bd92020-09-03 03:21:06 -05001572 return -1;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001573#endif
1574#ifdef SIGTTOU
Christian Heimes6782b142016-09-09 00:11:45 +02001575 if (PyModule_AddIntMacro(m, SIGTTOU))
Mohamed Koubaa71d1bd92020-09-03 03:21:06 -05001576 return -1;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001577#endif
1578#ifdef SIGVTALRM
Christian Heimes6782b142016-09-09 00:11:45 +02001579 if (PyModule_AddIntMacro(m, SIGVTALRM))
Mohamed Koubaa71d1bd92020-09-03 03:21:06 -05001580 return -1;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001581#endif
1582#ifdef SIGPROF
Christian Heimes6782b142016-09-09 00:11:45 +02001583 if (PyModule_AddIntMacro(m, SIGPROF))
Mohamed Koubaa71d1bd92020-09-03 03:21:06 -05001584 return -1;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001585#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001586#ifdef SIGXCPU
Christian Heimes6782b142016-09-09 00:11:45 +02001587 if (PyModule_AddIntMacro(m, SIGXCPU))
Mohamed Koubaa71d1bd92020-09-03 03:21:06 -05001588 return -1;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001589#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001590#ifdef SIGXFSZ
Christian Heimes6782b142016-09-09 00:11:45 +02001591 if (PyModule_AddIntMacro(m, SIGXFSZ))
Mohamed Koubaa71d1bd92020-09-03 03:21:06 -05001592 return -1;
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001593#endif
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001594#ifdef SIGRTMIN
Christian Heimes6782b142016-09-09 00:11:45 +02001595 if (PyModule_AddIntMacro(m, SIGRTMIN))
Mohamed Koubaa71d1bd92020-09-03 03:21:06 -05001596 return -1;
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001597#endif
1598#ifdef SIGRTMAX
Christian Heimes6782b142016-09-09 00:11:45 +02001599 if (PyModule_AddIntMacro(m, SIGRTMAX))
Mohamed Koubaa71d1bd92020-09-03 03:21:06 -05001600 return -1;
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001601#endif
Martin v. Löwis175af252002-01-12 11:43:25 +00001602#ifdef SIGINFO
Christian Heimes6782b142016-09-09 00:11:45 +02001603 if (PyModule_AddIntMacro(m, SIGINFO))
Mohamed Koubaa71d1bd92020-09-03 03:21:06 -05001604 return -1;
Martin v. Löwis175af252002-01-12 11:43:25 +00001605#endif
Martin v. Löwis823725e2008-03-24 13:39:54 +00001606
1607#ifdef ITIMER_REAL
Christian Heimes6782b142016-09-09 00:11:45 +02001608 if (PyModule_AddIntMacro(m, ITIMER_REAL))
Mohamed Koubaa71d1bd92020-09-03 03:21:06 -05001609 return -1;
Martin v. Löwis823725e2008-03-24 13:39:54 +00001610#endif
1611#ifdef ITIMER_VIRTUAL
Christian Heimes6782b142016-09-09 00:11:45 +02001612 if (PyModule_AddIntMacro(m, ITIMER_VIRTUAL))
Mohamed Koubaa71d1bd92020-09-03 03:21:06 -05001613 return -1;
Martin v. Löwis823725e2008-03-24 13:39:54 +00001614#endif
1615#ifdef ITIMER_PROF
Christian Heimes6782b142016-09-09 00:11:45 +02001616 if (PyModule_AddIntMacro(m, ITIMER_PROF))
Mohamed Koubaa71d1bd92020-09-03 03:21:06 -05001617 return -1;
Martin v. Löwis823725e2008-03-24 13:39:54 +00001618#endif
1619
1620#if defined (HAVE_SETITIMER) || defined (HAVE_GETITIMER)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001621 ItimerError = PyErr_NewException("signal.ItimerError",
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +03001622 PyExc_OSError, NULL);
animalize77643c42019-09-09 21:46:26 +08001623 if (!ItimerError ||
1624 PyDict_SetItemString(d, "ItimerError", ItimerError) < 0) {
Mohamed Koubaa71d1bd92020-09-03 03:21:06 -05001625 return -1;
animalize77643c42019-09-09 21:46:26 +08001626 }
Martin v. Löwis823725e2008-03-24 13:39:54 +00001627#endif
1628
Brian Curtineb24d742010-04-12 17:16:38 +00001629#ifdef CTRL_C_EVENT
Christian Heimes6782b142016-09-09 00:11:45 +02001630 if (PyModule_AddIntMacro(m, CTRL_C_EVENT))
Mohamed Koubaa71d1bd92020-09-03 03:21:06 -05001631 return -1;
Brian Curtineb24d742010-04-12 17:16:38 +00001632#endif
1633
1634#ifdef CTRL_BREAK_EVENT
Christian Heimes6782b142016-09-09 00:11:45 +02001635 if (PyModule_AddIntMacro(m, CTRL_BREAK_EVENT))
Mohamed Koubaa71d1bd92020-09-03 03:21:06 -05001636 return -1;
Brian Curtineb24d742010-04-12 17:16:38 +00001637#endif
1638
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001639#ifdef MS_WINDOWS
1640 /* Create manual-reset event, initially unset */
1641 sigint_event = CreateEvent(NULL, TRUE, FALSE, FALSE);
1642#endif
1643
Martin v. Löwis1a214512008-06-11 05:26:20 +00001644 if (PyErr_Occurred()) {
Mohamed Koubaa71d1bd92020-09-03 03:21:06 -05001645 return -1;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001646 }
Barry Warsaw92971171997-01-03 00:14:25 +00001647
Mohamed Koubaa71d1bd92020-09-03 03:21:06 -05001648 return 0;
1649}
1650
Mohamed Koubaa71d1bd92020-09-03 03:21:06 -05001651
1652static struct PyModuleDef signalmodule = {
1653 PyModuleDef_HEAD_INIT,
1654 "_signal",
1655 .m_doc = module_doc,
Victor Stinner4b8032e2020-09-04 14:51:05 +02001656 .m_size = -1,
Mohamed Koubaa71d1bd92020-09-03 03:21:06 -05001657 .m_methods = signal_methods,
Mohamed Koubaa71d1bd92020-09-03 03:21:06 -05001658};
1659
Victor Stinner4b8032e2020-09-04 14:51:05 +02001660
Mohamed Koubaa71d1bd92020-09-03 03:21:06 -05001661PyMODINIT_FUNC
1662PyInit__signal(void)
1663{
Victor Stinner4b8032e2020-09-04 14:51:05 +02001664 PyObject *mod = PyModule_Create(&signalmodule);
1665 if (mod == NULL) {
1666 return NULL;
1667 }
1668
1669 if (signal_exec(mod) < 0) {
1670 Py_DECREF(mod);
1671 return NULL;
1672 }
1673 return mod;
Guido van Rossum08c16611997-08-02 03:01:42 +00001674}
1675
Victor Stinner4b8032e2020-09-04 14:51:05 +02001676
Guido van Rossum08c16611997-08-02 03:01:42 +00001677static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001678finisignal(void)
Guido van Rossum08c16611997-08-02 03:01:42 +00001679{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001680 int i;
1681 PyObject *func;
Guido van Rossum08c16611997-08-02 03:01:42 +00001682
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001683 for (i = 1; i < NSIG; i++) {
1684 func = Handlers[i].func;
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001685 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001686 Handlers[i].func = NULL;
pkerlinge905c842018-06-01 09:47:18 +00001687 if (func != NULL && func != Py_None &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001688 func != DefaultHandler && func != IgnoreHandler)
1689 PyOS_setsig(i, SIG_DFL);
1690 Py_XDECREF(func);
1691 }
Guido van Rossum08c16611997-08-02 03:01:42 +00001692
Serhiy Storchaka505ff752014-02-09 13:33:53 +02001693 Py_CLEAR(IntHandler);
1694 Py_CLEAR(DefaultHandler);
1695 Py_CLEAR(IgnoreHandler);
animalize77643c42019-09-09 21:46:26 +08001696#ifdef HAVE_GETITIMER
1697 Py_CLEAR(ItimerError);
1698#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001699}
1700
Barry Warsaw92971171997-01-03 00:14:25 +00001701
Barry Warsaw92971171997-01-03 00:14:25 +00001702/* Declared in pyerrors.h */
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001703int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001704PyErr_CheckSignals(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001705{
Victor Stinner72818982020-03-26 22:28:11 +01001706 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerb54a99d2020-04-08 23:35:05 +02001707 if (!_Py_ThreadCanHandleSignals(tstate->interp)) {
Eric Snow64d6cc82019-02-23 15:40:43 -07001708 return 0;
1709 }
1710
Victor Stinner72818982020-03-26 22:28:11 +01001711 return _PyErr_CheckSignalsTstate(tstate);
Eric Snow64d6cc82019-02-23 15:40:43 -07001712}
1713
1714
1715/* Declared in cpython/pyerrors.h */
1716int
Victor Stinner72818982020-03-26 22:28:11 +01001717_PyErr_CheckSignalsTstate(PyThreadState *tstate)
Eric Snow64d6cc82019-02-23 15:40:43 -07001718{
Victor Stinner72818982020-03-26 22:28:11 +01001719 if (!_Py_atomic_load(&is_tripped)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001720 return 0;
Victor Stinner72818982020-03-26 22:28:11 +01001721 }
Christian Heimesb76922a2007-12-11 01:06:40 +00001722
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001723 /*
1724 * The is_tripped variable is meant to speed up the calls to
1725 * PyErr_CheckSignals (both directly or via pending calls) when no
1726 * signal has arrived. This variable is set to 1 when a signal arrives
1727 * and it is set to 0 here, when we know some signals arrived. This way
1728 * we can run the registered handlers with no signals blocked.
1729 *
1730 * NOTE: with this approach we can have a situation where is_tripped is
1731 * 1 but we have no more signals to handle (Handlers[i].tripped
1732 * is 0 for every signal i). This won't do us any harm (except
1733 * we're gonna spent some cycles for nothing). This happens when
1734 * we receive a signal i after we zero is_tripped and before we
1735 * check Handlers[i].tripped.
1736 */
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001737 _Py_atomic_store(&is_tripped, 0);
Christian Heimesb76922a2007-12-11 01:06:40 +00001738
Victor Stinner72818982020-03-26 22:28:11 +01001739 PyObject *frame = (PyObject *)tstate->frame;
1740 if (!frame) {
1741 frame = Py_None;
1742 }
Christian Heimesb76922a2007-12-11 01:06:40 +00001743
Victor Stinner72818982020-03-26 22:28:11 +01001744 for (int i = 1; i < NSIG; i++) {
1745 if (!_Py_atomic_load_relaxed(&Handlers[i].tripped)) {
1746 continue;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001747 }
Victor Stinner72818982020-03-26 22:28:11 +01001748 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
1749
1750 PyObject *arglist = Py_BuildValue("(iO)", i, frame);
1751 PyObject *result;
1752 if (arglist) {
1753 result = _PyObject_Call(tstate, Handlers[i].func, arglist, NULL);
1754 Py_DECREF(arglist);
1755 }
1756 else {
1757 result = NULL;
1758 }
1759 if (!result) {
1760 /* On error, re-schedule a call to _PyErr_CheckSignalsTstate() */
1761 _Py_atomic_store(&is_tripped, 1);
1762 return -1;
1763 }
1764
1765 Py_DECREF(result);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001766 }
Christian Heimesb76922a2007-12-11 01:06:40 +00001767
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001768 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001769}
1770
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +00001771
Victor Stinner72818982020-03-26 22:28:11 +01001772
1773int
1774_PyErr_CheckSignals(void)
1775{
1776 PyThreadState *tstate = _PyThreadState_GET();
1777 return _PyErr_CheckSignalsTstate(tstate);
1778}
1779
1780
Matěj Cepl608876b2019-05-23 22:30:00 +02001781/* Simulate the effect of a signal.SIGINT signal arriving. The next time
1782 PyErr_CheckSignals is called, the Python SIGINT signal handler will be
1783 raised.
1784
1785 Missing signal handler for the SIGINT signal is silently ignored. */
Barry Warsaw92971171997-01-03 00:14:25 +00001786void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001787PyErr_SetInterrupt(void)
Barry Warsaw92971171997-01-03 00:14:25 +00001788{
Matěj Cepl608876b2019-05-23 22:30:00 +02001789 if ((Handlers[SIGINT].func != IgnoreHandler) &&
1790 (Handlers[SIGINT].func != DefaultHandler)) {
1791 trip_signal(SIGINT);
1792 }
Barry Warsaw92971171997-01-03 00:14:25 +00001793}
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001794
1795void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001796PyOS_InitInterrupts(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001797{
Giampaolo Rodola'e09fb712014-04-04 15:34:17 +02001798 PyObject *m = PyImport_ImportModule("_signal");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001799 if (m) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001800 Py_DECREF(m);
1801 }
Guido van Rossum08c16611997-08-02 03:01:42 +00001802}
1803
1804void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001805PyOS_FiniInterrupts(void)
Guido van Rossum08c16611997-08-02 03:01:42 +00001806{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001807 finisignal();
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001808}
1809
Victor Stinnerfa7ab6a2020-06-03 14:39:59 +02001810
1811// The caller doesn't have to hold the GIL
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001812int
Victor Stinnerfa7ab6a2020-06-03 14:39:59 +02001813_PyOS_InterruptOccurred(PyThreadState *tstate)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001814{
Victor Stinnercbe12962020-06-01 20:34:15 +02001815 _Py_EnsureTstateNotNULL(tstate);
1816 if (!_Py_ThreadCanHandleSignals(tstate->interp)) {
Victor Stinner72818982020-03-26 22:28:11 +01001817 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001818 }
Victor Stinner72818982020-03-26 22:28:11 +01001819
1820 if (!_Py_atomic_load_relaxed(&Handlers[SIGINT].tripped)) {
1821 return 0;
1822 }
1823
1824 _Py_atomic_store_relaxed(&Handlers[SIGINT].tripped, 0);
1825 return 1;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001826}
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001827
Victor Stinner26881c82020-06-02 15:51:37 +02001828
Victor Stinnerfa7ab6a2020-06-03 14:39:59 +02001829// The caller must to hold the GIL
1830int
1831PyOS_InterruptOccurred(void)
1832{
1833 PyThreadState *tstate = _PyThreadState_GET();
1834 return _PyOS_InterruptOccurred(tstate);
1835}
1836
1837
Victor Stinner26881c82020-06-02 15:51:37 +02001838#ifdef HAVE_FORK
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001839static void
1840_clear_pending_signals(void)
1841{
Victor Stinner26881c82020-06-02 15:51:37 +02001842 if (!_Py_atomic_load(&is_tripped)) {
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001843 return;
Victor Stinner26881c82020-06-02 15:51:37 +02001844 }
1845
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001846 _Py_atomic_store(&is_tripped, 0);
Victor Stinner26881c82020-06-02 15:51:37 +02001847 for (int i = 1; i < NSIG; ++i) {
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001848 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001849 }
1850}
1851
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001852void
Antoine Pitrou346cbd32017-05-27 17:50:54 +02001853_PySignal_AfterFork(void)
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001854{
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001855 /* Clear the signal flags after forking so that they aren't handled
1856 * in both processes if they came in just before the fork() but before
1857 * the interpreter had an opportunity to call the handlers. issue9535. */
1858 _clear_pending_signals();
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001859}
Victor Stinner26881c82020-06-02 15:51:37 +02001860#endif /* HAVE_FORK */
1861
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001862
1863int
1864_PyOS_IsMainThread(void)
1865{
Victor Stinner81a7be32020-04-14 15:14:01 +02001866 PyInterpreterState *interp = _PyInterpreterState_GET();
Victor Stinnerb54a99d2020-04-08 23:35:05 +02001867 return _Py_ThreadCanHandleSignals(interp);
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001868}
1869
1870#ifdef MS_WINDOWS
1871void *_PyOS_SigintEvent(void)
1872{
1873 /* Returns a manual-reset event which gets tripped whenever
1874 SIGINT is received.
1875
1876 Python.h does not include windows.h so we do cannot use HANDLE
1877 as the return type of this function. We use void* instead. */
1878 return sigint_event;
1879}
1880#endif