blob: fa3b01de25a834ef9d97581efd589e1c4834b6ba [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 Stinner09532fe2019-05-10 23:39:09 +02008#include "pycore_ceval.h"
9#include "pycore_pystate.h"
Victor Stinner31368a42018-10-30 15:14:25 +010010
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020011#ifndef MS_WINDOWS
12#include "posixmodule.h"
13#endif
Victor Stinner11517102014-07-29 23:31:34 +020014#ifdef MS_WINDOWS
15#include "socketmodule.h" /* needed for SOCKET_T */
16#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +000017
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000018#ifdef MS_WINDOWS
Antoine Pitrou7faf7052013-03-31 22:48:04 +020019#include <windows.h>
Benjamin Peterson2614cda2010-03-21 22:36:19 +000020#ifdef HAVE_PROCESS_H
Guido van Rossum644a12b1997-04-09 19:24:53 +000021#include <process.h>
22#endif
Benjamin Peterson2614cda2010-03-21 22:36:19 +000023#endif
Guido van Rossum644a12b1997-04-09 19:24:53 +000024
Benjamin Peterson2614cda2010-03-21 22:36:19 +000025#ifdef HAVE_SIGNAL_H
Guido van Rossum398d9fe1994-05-11 08:59:13 +000026#include <signal.h>
Benjamin Peterson2614cda2010-03-21 22:36:19 +000027#endif
Benjamin Peterson74834512019-11-19 20:39:14 -080028#ifdef HAVE_SYS_SYSCALL_H
29#include <sys/syscall.h>
30#endif
Benjamin Peterson2614cda2010-03-21 22:36:19 +000031#ifdef HAVE_SYS_STAT_H
Christian Heimes5fb7c2a2007-12-24 08:52:31 +000032#include <sys/stat.h>
Benjamin Peterson2614cda2010-03-21 22:36:19 +000033#endif
Martin v. Löwis3bf3cc02008-03-24 14:05:07 +000034#ifdef HAVE_SYS_TIME_H
Martin v. Löwis823725e2008-03-24 13:39:54 +000035#include <sys/time.h>
Martin v. Löwis3bf3cc02008-03-24 14:05:07 +000036#endif
Christian Heimes5fb7c2a2007-12-24 08:52:31 +000037
Victor Stinnera9293352011-04-30 15:21:58 +020038#if defined(HAVE_PTHREAD_SIGMASK) && !defined(HAVE_BROKEN_PTHREAD_SIGMASK)
39# define PYPTHREAD_SIGMASK
40#endif
41
42#if defined(PYPTHREAD_SIGMASK) && defined(HAVE_PTHREAD_H)
43# include <pthread.h>
44#endif
45
Guido van Rossumbb4ba121994-06-23 11:25:45 +000046#ifndef SIG_ERR
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +000047#define SIG_ERR ((PyOS_sighandler_t)(-1))
Guido van Rossumbb4ba121994-06-23 11:25:45 +000048#endif
49
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000050#ifndef NSIG
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000051# if defined(_NSIG)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000052# define NSIG _NSIG /* For BSD/SysV */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000053# elif defined(_SIGMAX)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000054# define NSIG (_SIGMAX + 1) /* For QNX */
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 djgpp */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000057# else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000058# define NSIG 64 /* Use a reasonable default value */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000059# endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000060#endif
61
Tal Einatc7027b72015-05-16 14:14:49 +030062#include "clinic/signalmodule.c.h"
63
64/*[clinic input]
65module signal
66[clinic start generated code]*/
67/*[clinic end generated code: output=da39a3ee5e6b4b0d input=b0301a3bde5fe9d3]*/
68
Serhiy Storchakad54cfb12018-05-08 07:48:50 +030069/*[python input]
70
71class sigset_t_converter(CConverter):
72 type = 'sigset_t'
73 converter = '_Py_Sigset_Converter'
74
75[python start generated code]*/
76/*[python end generated code: output=da39a3ee5e6b4b0d input=b5689d14466b6823]*/
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000077
Guido van Rossumbb4ba121994-06-23 11:25:45 +000078/*
79 NOTES ON THE INTERACTION BETWEEN SIGNALS AND THREADS
80
Jeroen Demeyerd237b3f2019-05-10 03:28:57 +020081 We want the following semantics:
Guido van Rossumbb4ba121994-06-23 11:25:45 +000082
83 - only the main thread can set a signal handler
Jeroen Demeyerd237b3f2019-05-10 03:28:57 +020084 - only the main thread runs the signal handler
85 - signals can be delivered to any thread
Guido van Rossumbb4ba121994-06-23 11:25:45 +000086 - any thread can get a signal handler
Guido van Rossumbb4ba121994-06-23 11:25:45 +000087
88 I.e. we don't support "synchronous signals" like SIGFPE (catching
89 this doesn't make much sense in Python anyway) nor do we support
90 signals as a means of inter-thread communication, since not all
91 thread implementations support that (at least our thread library
92 doesn't).
93
94 We still have the problem that in some implementations signals
95 generated by the keyboard (e.g. SIGINT) are delivered to all
96 threads (e.g. SGI), while in others (e.g. Solaris) such signals are
Jeroen Demeyerd237b3f2019-05-10 03:28:57 +020097 delivered to one random thread. On Linux, signals are delivered to
98 the main thread (unless the main thread is blocking the signal, for
99 example because it's already handling the same signal). Since we
100 allow signals to be delivered to any thread, this works fine. The
101 only oddity is that the thread executing the Python signal handler
102 may not be the thread that received the signal.
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000103*/
104
Guido van Rossum49b56061998-10-01 20:42:43 +0000105#include "pythread.h"
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000106
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
Eric Snow64d6cc82019-02-23 15:40:43 -0700192static int
Victor Stinnerd2a8e5b2020-03-20 13:38:58 +0100193thread_can_handle_signals(void)
Eric Snow64d6cc82019-02-23 15:40:43 -0700194{
Victor Stinnerd2a8e5b2020-03-20 13:38:58 +0100195 PyThreadState *tstate = _PyThreadState_GET();
196 return _Py_ThreadCanHandleSignals(tstate);
Eric Snow64d6cc82019-02-23 15:40:43 -0700197}
198
Guido van Rossume4485b01994-09-07 14:32:49 +0000199static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000200signal_default_int_handler(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000201{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000202 PyErr_SetNone(PyExc_KeyboardInterrupt);
203 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000204}
205
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000206PyDoc_STRVAR(default_int_handler_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000207"default_int_handler(...)\n\
208\n\
Michael W. Hudson24ec2112004-06-17 15:55:53 +0000209The default handler for SIGINT installed by Python.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000210It raises KeyboardInterrupt.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000211
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 Stinner8849e592020-03-18 19:28:53 +0100260 /* Get the Python thread state using PyGILState API, since
261 _PyThreadState_GET() returns NULL if the GIL is released.
262 For example, signal.raise_signal() releases the GIL. */
263 PyThreadState *tstate = PyGILState_GetThisThreadState();
264 assert(tstate != NULL);
265
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +0200266 /* Notify ceval.c */
Victor Stinner8849e592020-03-18 19:28:53 +0100267 _PyEval_SignalReceived(tstate);
Nathaniel J. Smith4ae01492017-05-16 14:12:11 -0700268
269 /* And then write to the wakeup fd *after* setting all the globals and
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200270 doing the _PyEval_SignalReceived. We used to write to the wakeup fd
271 and then set the flag, but this allowed the following sequence of events
272 (especially on windows, where trip_signal may run in a new thread):
Nathaniel J. Smith4ae01492017-05-16 14:12:11 -0700273
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800274 - main thread blocks on select([wakeup.fd], ...)
Nathaniel J. Smith4ae01492017-05-16 14:12:11 -0700275 - signal arrives
276 - trip_signal writes to the wakeup fd
277 - the main thread wakes up
278 - the main thread checks the signal flags, sees that they're unset
279 - the main thread empties the wakeup fd
280 - the main thread goes back to sleep
281 - trip_signal sets the flags to request the Python-level signal handler
282 be run
283 - the main thread doesn't notice, because it's asleep
284
285 See bpo-30038 for more details.
286 */
287
Victor Stinner11517102014-07-29 23:31:34 +0200288#ifdef MS_WINDOWS
289 fd = Py_SAFE_DOWNCAST(wakeup.fd, SOCKET_T, int);
290#else
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800291 fd = wakeup.fd;
Victor Stinner11517102014-07-29 23:31:34 +0200292#endif
293
294 if (fd != INVALID_FD) {
Victor Stinnerc13ef662011-05-25 02:35:58 +0200295 byte = (unsigned char)sig_num;
Victor Stinner11517102014-07-29 23:31:34 +0200296#ifdef MS_WINDOWS
297 if (wakeup.use_send) {
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800298 rc = send(fd, &byte, 1, 0);
Victor Stinner11517102014-07-29 23:31:34 +0200299
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800300 if (rc < 0) {
301 int last_error = GetLastError();
302 if (wakeup.warn_on_full_buffer ||
303 last_error != WSAEWOULDBLOCK)
304 {
Victor Stinner50e6e992020-03-19 02:41:21 +0100305 /* _PyEval_AddPendingCall() isn't signal-safe, but we
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800306 still use it for this exceptional case. */
Victor Stinner8849e592020-03-18 19:28:53 +0100307 _PyEval_AddPendingCall(tstate,
Victor Stinner09532fe2019-05-10 23:39:09 +0200308 report_wakeup_send_error,
309 (void *)(intptr_t) last_error);
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800310 }
Victor Stinner11517102014-07-29 23:31:34 +0200311 }
312 }
313 else
314#endif
315 {
Victor Stinnere72fe392015-04-01 18:35:22 +0200316 /* _Py_write_noraise() retries write() if write() is interrupted by
317 a signal (fails with EINTR). */
318 rc = _Py_write_noraise(fd, &byte, 1);
Victor Stinner11517102014-07-29 23:31:34 +0200319
320 if (rc < 0) {
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800321 if (wakeup.warn_on_full_buffer ||
322 (errno != EWOULDBLOCK && errno != EAGAIN))
323 {
Victor Stinner50e6e992020-03-19 02:41:21 +0100324 /* _PyEval_AddPendingCall() isn't signal-safe, but we
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800325 still use it for this exceptional case. */
Victor Stinner8849e592020-03-18 19:28:53 +0100326 _PyEval_AddPendingCall(tstate,
Victor Stinner09532fe2019-05-10 23:39:09 +0200327 report_wakeup_write_error,
328 (void *)(intptr_t)errno);
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800329 }
Victor Stinner11517102014-07-29 23:31:34 +0200330 }
331 }
Victor Stinnerc13ef662011-05-25 02:35:58 +0200332 }
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200333}
334
335static void
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000336signal_handler(int sig_num)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000337{
Antoine Pitrou39a65912010-11-05 19:47:27 +0000338 int save_errno = errno;
339
Jeroen Demeyerd237b3f2019-05-10 03:28:57 +0200340 trip_signal(sig_num);
Antoine Pitrou39a65912010-11-05 19:47:27 +0000341
Jean-Paul Calderone6f137ca2010-05-09 03:18:57 +0000342#ifndef HAVE_SIGACTION
Antoine Pitrou39a65912010-11-05 19:47:27 +0000343#ifdef SIGCHLD
344 /* To avoid infinite recursion, this signal remains
345 reset until explicit re-instated.
346 Don't clear the 'func' field as it is our pointer
347 to the Python handler... */
348 if (sig_num != SIGCHLD)
349#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000350 /* If the handler was not set up with sigaction, reinstall it. See
Nick Coghland6009512014-11-20 21:39:37 +1000351 * Python/pylifecycle.c for the implementation of PyOS_setsig which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000352 * makes this true. See also issue8354. */
353 PyOS_setsig(sig_num, signal_handler);
Jean-Paul Calderone6f137ca2010-05-09 03:18:57 +0000354#endif
Antoine Pitrou39a65912010-11-05 19:47:27 +0000355
356 /* Issue #10311: asynchronously executing signal handlers should not
357 mutate errno under the feet of unsuspecting C code. */
358 errno = save_errno;
Antoine Pitrou6dd381e2011-11-21 21:26:56 +0100359
360#ifdef MS_WINDOWS
361 if (sig_num == SIGINT)
362 SetEvent(sigint_event);
363#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000364}
Guido van Rossume4485b01994-09-07 14:32:49 +0000365
Guido van Rossum06d511d1995-03-10 15:13:48 +0000366
Guido van Rossum1171ee61997-08-22 20:42:00 +0000367#ifdef HAVE_ALARM
Tal Einatc7027b72015-05-16 14:14:49 +0300368
369/*[clinic input]
370signal.alarm -> long
371
372 seconds: int
373 /
374
375Arrange for SIGALRM to arrive after the given number of seconds.
376[clinic start generated code]*/
377
378static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300379signal_alarm_impl(PyObject *module, int seconds)
380/*[clinic end generated code: output=144232290814c298 input=0d5e97e0e6f39e86]*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000381{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000382 /* alarm() returns the number of seconds remaining */
Tal Einatc7027b72015-05-16 14:14:49 +0300383 return (long)alarm(seconds);
Guido van Rossumaa0f4c71994-08-23 13:49:37 +0000384}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000385
Guido van Rossum06d511d1995-03-10 15:13:48 +0000386#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000387
Guido van Rossum1171ee61997-08-22 20:42:00 +0000388#ifdef HAVE_PAUSE
Tal Einatc7027b72015-05-16 14:14:49 +0300389
390/*[clinic input]
391signal.pause
392
393Wait until a signal arrives.
394[clinic start generated code]*/
395
Guido van Rossuma597dde1995-01-10 20:56:29 +0000396static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300397signal_pause_impl(PyObject *module)
398/*[clinic end generated code: output=391656788b3c3929 input=f03de0f875752062]*/
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000399{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000400 Py_BEGIN_ALLOW_THREADS
401 (void)pause();
402 Py_END_ALLOW_THREADS
403 /* make sure that any exceptions that got raised are propagated
404 * back into Python
405 */
406 if (PyErr_CheckSignals())
407 return NULL;
Barry Warsaw92971171997-01-03 00:14:25 +0000408
Tal Einatc7027b72015-05-16 14:14:49 +0300409 Py_RETURN_NONE;
Guido van Rossume4485b01994-09-07 14:32:49 +0000410}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000411
Guido van Rossum06d511d1995-03-10 15:13:48 +0000412#endif
Guido van Rossume4485b01994-09-07 14:32:49 +0000413
Vladimir Matveevc24c6c22019-01-08 01:58:25 -0800414/*[clinic input]
415signal.raise_signal
416
417 signalnum: int
418 /
419
420Send a signal to the executing process.
421[clinic start generated code]*/
422
423static PyObject *
424signal_raise_signal_impl(PyObject *module, int signalnum)
425/*[clinic end generated code: output=e2b014220aa6111d input=e90c0f9a42358de6]*/
426{
427 int err;
428 Py_BEGIN_ALLOW_THREADS
429 _Py_BEGIN_SUPPRESS_IPH
430 err = raise(signalnum);
431 _Py_END_SUPPRESS_IPH
432 Py_END_ALLOW_THREADS
Victor Stinner09532fe2019-05-10 23:39:09 +0200433
Vladimir Matveevc24c6c22019-01-08 01:58:25 -0800434 if (err) {
435 return PyErr_SetFromErrno(PyExc_OSError);
436 }
437 Py_RETURN_NONE;
438}
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000439
Tal Einatc7027b72015-05-16 14:14:49 +0300440/*[clinic input]
441signal.signal
442
443 signalnum: int
444 handler: object
445 /
446
447Set the action for the given signal.
448
449The action can be SIG_DFL, SIG_IGN, or a callable Python object.
450The previous action is returned. See getsignal() for possible return values.
451
452*** IMPORTANT NOTICE ***
453A signal handler function is called with two arguments:
454the first is the signal number, the second is the interrupted stack frame.
455[clinic start generated code]*/
456
Guido van Rossume4485b01994-09-07 14:32:49 +0000457static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300458signal_signal_impl(PyObject *module, int signalnum, PyObject *handler)
459/*[clinic end generated code: output=b44cfda43780f3a1 input=deee84af5fa0432c]*/
Guido van Rossume4485b01994-09-07 14:32:49 +0000460{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000461 PyObject *old_handler;
462 void (*func)(int);
Brian Curtinef9efbd2010-08-06 19:27:32 +0000463#ifdef MS_WINDOWS
Tal Einatc7027b72015-05-16 14:14:49 +0300464 /* Validate that signalnum is one of the allowable signals */
465 switch (signalnum) {
Brian Curtinc734b312010-09-06 16:04:10 +0000466 case SIGABRT: break;
Brian Curtin9e88b5a2010-10-01 14:49:24 +0000467#ifdef SIGBREAK
468 /* Issue #10003: SIGBREAK is not documented as permitted, but works
469 and corresponds to CTRL_BREAK_EVENT. */
470 case SIGBREAK: break;
471#endif
Brian Curtinc734b312010-09-06 16:04:10 +0000472 case SIGFPE: break;
473 case SIGILL: break;
474 case SIGINT: break;
475 case SIGSEGV: break;
476 case SIGTERM: break;
477 default:
478 PyErr_SetString(PyExc_ValueError, "invalid signal value");
479 return NULL;
Brian Curtinef9efbd2010-08-06 19:27:32 +0000480 }
481#endif
Victor Stinnerd8613dc2019-05-24 13:43:55 +0200482
Victor Stinnerd2a8e5b2020-03-20 13:38:58 +0100483 if (!thread_can_handle_signals()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000484 PyErr_SetString(PyExc_ValueError,
Victor Stinnerd2a8e5b2020-03-20 13:38:58 +0100485 "signal only works in main thread "
486 "of the main interpreter");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000487 return NULL;
488 }
Tal Einatc7027b72015-05-16 14:14:49 +0300489 if (signalnum < 1 || signalnum >= NSIG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000490 PyErr_SetString(PyExc_ValueError,
491 "signal number out of range");
492 return NULL;
493 }
Tal Einatc7027b72015-05-16 14:14:49 +0300494 if (handler == IgnoreHandler)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000495 func = SIG_IGN;
Tal Einatc7027b72015-05-16 14:14:49 +0300496 else if (handler == DefaultHandler)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 func = SIG_DFL;
Tal Einatc7027b72015-05-16 14:14:49 +0300498 else if (!PyCallable_Check(handler)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 PyErr_SetString(PyExc_TypeError,
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000500"signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000501 return NULL;
502 }
503 else
504 func = signal_handler;
Antoine Pitrouf6f90ff2017-11-03 19:58:46 +0100505 /* Check for pending signals before changing signal handler */
Eric Snow64d6cc82019-02-23 15:40:43 -0700506 if (_PyErr_CheckSignals()) {
Antoine Pitrouf6f90ff2017-11-03 19:58:46 +0100507 return NULL;
508 }
Tal Einatc7027b72015-05-16 14:14:49 +0300509 if (PyOS_setsig(signalnum, func) == SIG_ERR) {
Victor Stinner388196e2011-05-10 17:13:00 +0200510 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000511 return NULL;
512 }
Tal Einatc7027b72015-05-16 14:14:49 +0300513 old_handler = Handlers[signalnum].func;
Tal Einatc7027b72015-05-16 14:14:49 +0300514 Py_INCREF(handler);
515 Handlers[signalnum].func = handler;
Antoine Pitrouc8c952c2013-05-04 23:16:59 +0200516 if (old_handler != NULL)
517 return old_handler;
518 else
519 Py_RETURN_NONE;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000520}
521
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000522
Tal Einatc7027b72015-05-16 14:14:49 +0300523/*[clinic input]
524signal.getsignal
525
526 signalnum: int
527 /
528
529Return the current action for the given signal.
530
531The return value can be:
532 SIG_IGN -- if the signal is being ignored
533 SIG_DFL -- if the default action for the signal is in effect
534 None -- if an unknown handler is in effect
535 anything else -- the callable Python object used as a handler
536[clinic start generated code]*/
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000537
Guido van Rossume4485b01994-09-07 14:32:49 +0000538static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300539signal_getsignal_impl(PyObject *module, int signalnum)
540/*[clinic end generated code: output=35b3e0e796fd555e input=ac23a00f19dfa509]*/
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000541{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000542 PyObject *old_handler;
Tal Einatc7027b72015-05-16 14:14:49 +0300543 if (signalnum < 1 || signalnum >= NSIG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000544 PyErr_SetString(PyExc_ValueError,
545 "signal number out of range");
546 return NULL;
547 }
Tal Einatc7027b72015-05-16 14:14:49 +0300548 old_handler = Handlers[signalnum].func;
Antoine Pitrouc8c952c2013-05-04 23:16:59 +0200549 if (old_handler != NULL) {
550 Py_INCREF(old_handler);
551 return old_handler;
552 }
553 else {
554 Py_RETURN_NONE;
555 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000556}
557
Antoine Pietri5d2a27d2018-03-12 14:42:34 +0100558
559/*[clinic input]
560signal.strsignal
561
562 signalnum: int
563 /
564
565Return the system description of the given signal.
566
567The return values can be such as "Interrupt", "Segmentation fault", etc.
568Returns None if the signal is not recognized.
569[clinic start generated code]*/
570
571static PyObject *
572signal_strsignal_impl(PyObject *module, int signalnum)
573/*[clinic end generated code: output=44e12e1e3b666261 input=b77914b03f856c74]*/
574{
575 char *res;
576
577 if (signalnum < 1 || signalnum >= NSIG) {
578 PyErr_SetString(PyExc_ValueError,
579 "signal number out of range");
580 return NULL;
581 }
582
Michael Osipov48ce4892018-08-23 15:27:19 +0200583#ifndef HAVE_STRSIGNAL
Antoine Pietri5d2a27d2018-03-12 14:42:34 +0100584 switch (signalnum) {
Michael Osipov48ce4892018-08-23 15:27:19 +0200585 /* Though being a UNIX, HP-UX does not provide strsignal(3). */
586#ifndef MS_WINDOWS
587 case SIGHUP:
588 res = "Hangup";
589 break;
590 case SIGALRM:
591 res = "Alarm clock";
592 break;
593 case SIGPIPE:
594 res = "Broken pipe";
595 break;
596 case SIGQUIT:
597 res = "Quit";
598 break;
599 case SIGCHLD:
600 res = "Child exited";
601 break;
602#endif
603 /* Custom redefinition of POSIX signals allowed on Windows. */
Antoine Pietri5d2a27d2018-03-12 14:42:34 +0100604 case SIGINT:
605 res = "Interrupt";
606 break;
607 case SIGILL:
608 res = "Illegal instruction";
609 break;
610 case SIGABRT:
611 res = "Aborted";
612 break;
613 case SIGFPE:
614 res = "Floating point exception";
615 break;
616 case SIGSEGV:
617 res = "Segmentation fault";
618 break;
619 case SIGTERM:
620 res = "Terminated";
621 break;
622 default:
623 Py_RETURN_NONE;
624 }
625#else
626 errno = 0;
627 res = strsignal(signalnum);
628
629 if (errno || res == NULL || strstr(res, "Unknown signal") != NULL)
630 Py_RETURN_NONE;
631#endif
632
633 return Py_BuildValue("s", res);
634}
635
Christian Heimes8640e742008-02-23 16:23:06 +0000636#ifdef HAVE_SIGINTERRUPT
Tal Einatc7027b72015-05-16 14:14:49 +0300637
638/*[clinic input]
639signal.siginterrupt
640
641 signalnum: int
642 flag: int
643 /
644
645Change system call restart behaviour.
646
647If flag is False, system calls will be restarted when interrupted by
648signal sig, else system calls will be interrupted.
649[clinic start generated code]*/
Christian Heimes8640e742008-02-23 16:23:06 +0000650
651static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300652signal_siginterrupt_impl(PyObject *module, int signalnum, int flag)
653/*[clinic end generated code: output=063816243d85dd19 input=4160acacca3e2099]*/
Christian Heimes8640e742008-02-23 16:23:06 +0000654{
Tal Einatc7027b72015-05-16 14:14:49 +0300655 if (signalnum < 1 || signalnum >= NSIG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000656 PyErr_SetString(PyExc_ValueError,
657 "signal number out of range");
658 return NULL;
659 }
Tal Einatc7027b72015-05-16 14:14:49 +0300660 if (siginterrupt(signalnum, flag)<0) {
Victor Stinner388196e2011-05-10 17:13:00 +0200661 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000662 return NULL;
663 }
Tal Einatc7027b72015-05-16 14:14:49 +0300664 Py_RETURN_NONE;
Christian Heimes8640e742008-02-23 16:23:06 +0000665}
666
667#endif
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000668
Tal Einatc7027b72015-05-16 14:14:49 +0300669
670static PyObject*
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800671signal_set_wakeup_fd(PyObject *self, PyObject *args, PyObject *kwds)
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000672{
Victor Stinnere134a7f2015-03-30 10:09:31 +0200673 struct _Py_stat_struct status;
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800674 static char *kwlist[] = {
675 "", "warn_on_full_buffer", NULL,
676 };
677 int warn_on_full_buffer = 1;
Victor Stinner11517102014-07-29 23:31:34 +0200678#ifdef MS_WINDOWS
679 PyObject *fdobj;
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100680 SOCKET_T sockfd, old_sockfd;
Victor Stinner11517102014-07-29 23:31:34 +0200681 int res;
682 int res_size = sizeof res;
683 PyObject *mod;
Victor Stinner11517102014-07-29 23:31:34 +0200684 int is_socket;
685
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800686 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|$p:set_wakeup_fd", kwlist,
687 &fdobj, &warn_on_full_buffer))
Victor Stinner11517102014-07-29 23:31:34 +0200688 return NULL;
689
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100690 sockfd = PyLong_AsSocket_t(fdobj);
691 if (sockfd == (SOCKET_T)(-1) && PyErr_Occurred())
Victor Stinner11517102014-07-29 23:31:34 +0200692 return NULL;
693#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000694 int fd, old_fd;
Victor Stinner11517102014-07-29 23:31:34 +0200695
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800696 if (!PyArg_ParseTupleAndKeywords(args, kwds, "i|$p:set_wakeup_fd", kwlist,
697 &fd, &warn_on_full_buffer))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000698 return NULL;
Victor Stinner11517102014-07-29 23:31:34 +0200699#endif
700
Victor Stinnerd2a8e5b2020-03-20 13:38:58 +0100701 if (!thread_can_handle_signals()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000702 PyErr_SetString(PyExc_ValueError,
Victor Stinnerd2a8e5b2020-03-20 13:38:58 +0100703 "set_wakeup_fd only works in main thread "
704 "of the main interpreter");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000705 return NULL;
706 }
Victor Stinner0bffc942014-07-21 16:28:54 +0200707
Victor Stinner11517102014-07-29 23:31:34 +0200708#ifdef MS_WINDOWS
709 is_socket = 0;
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100710 if (sockfd != INVALID_FD) {
Victor Stinner11517102014-07-29 23:31:34 +0200711 /* Import the _socket module to call WSAStartup() */
712 mod = PyImport_ImportModuleNoBlock("_socket");
713 if (mod == NULL)
714 return NULL;
715 Py_DECREF(mod);
716
717 /* test the socket */
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100718 if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR,
Victor Stinner11517102014-07-29 23:31:34 +0200719 (char *)&res, &res_size) != 0) {
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100720 int fd, err;
721
722 err = WSAGetLastError();
Victor Stinner11517102014-07-29 23:31:34 +0200723 if (err != WSAENOTSOCK) {
724 PyErr_SetExcFromWindowsErr(PyExc_OSError, err);
725 return NULL;
726 }
727
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100728 fd = (int)sockfd;
Steve Dower940f33a2016-09-08 11:21:54 -0700729 if ((SOCKET_T)fd != sockfd) {
Victor Stinner11517102014-07-29 23:31:34 +0200730 PyErr_SetString(PyExc_ValueError, "invalid fd");
731 return NULL;
732 }
733
Victor Stinnere134a7f2015-03-30 10:09:31 +0200734 if (_Py_fstat(fd, &status) != 0)
Victor Stinner11517102014-07-29 23:31:34 +0200735 return NULL;
Victor Stinner38227602014-08-27 12:59:44 +0200736
737 /* on Windows, a file cannot be set to non-blocking mode */
Victor Stinner11517102014-07-29 23:31:34 +0200738 }
Victor Stinner38227602014-08-27 12:59:44 +0200739 else {
Victor Stinner11517102014-07-29 23:31:34 +0200740 is_socket = 1;
Victor Stinner38227602014-08-27 12:59:44 +0200741
742 /* Windows does not provide a function to test if a socket
743 is in non-blocking mode */
744 }
Victor Stinner11517102014-07-29 23:31:34 +0200745 }
746
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100747 old_sockfd = wakeup.fd;
748 wakeup.fd = sockfd;
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800749 wakeup.warn_on_full_buffer = warn_on_full_buffer;
Victor Stinner11517102014-07-29 23:31:34 +0200750 wakeup.use_send = is_socket;
751
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100752 if (old_sockfd != INVALID_FD)
753 return PyLong_FromSocket_t(old_sockfd);
Victor Stinner11517102014-07-29 23:31:34 +0200754 else
755 return PyLong_FromLong(-1);
756#else
Victor Stinnerd18ccd12014-07-24 21:58:53 +0200757 if (fd != -1) {
Victor Stinner38227602014-08-27 12:59:44 +0200758 int blocking;
759
Victor Stinnere134a7f2015-03-30 10:09:31 +0200760 if (_Py_fstat(fd, &status) != 0)
Victor Stinner11517102014-07-29 23:31:34 +0200761 return NULL;
Victor Stinner38227602014-08-27 12:59:44 +0200762
763 blocking = _Py_get_blocking(fd);
764 if (blocking < 0)
765 return NULL;
766 if (blocking) {
767 PyErr_Format(PyExc_ValueError,
768 "the fd %i must be in non-blocking mode",
769 fd);
770 return NULL;
771 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000772 }
Victor Stinner0bffc942014-07-21 16:28:54 +0200773
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800774 old_fd = wakeup.fd;
775 wakeup.fd = fd;
776 wakeup.warn_on_full_buffer = warn_on_full_buffer;
Victor Stinner0bffc942014-07-21 16:28:54 +0200777
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000778 return PyLong_FromLong(old_fd);
Victor Stinner11517102014-07-29 23:31:34 +0200779#endif
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000780}
781
782PyDoc_STRVAR(set_wakeup_fd_doc,
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800783"set_wakeup_fd(fd, *, warn_on_full_buffer=True) -> fd\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000784\n\
Victor Stinner11517102014-07-29 23:31:34 +0200785Sets the fd to be written to (with the signal number) when a signal\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000786comes in. A library can use this to wakeup select or poll.\n\
Victor Stinner11517102014-07-29 23:31:34 +0200787The previous fd or -1 is returned.\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000788\n\
789The fd must be non-blocking.");
790
791/* C API for the same, without all the error checking */
792int
793PySignal_SetWakeupFd(int fd)
794{
Victor Stinner11517102014-07-29 23:31:34 +0200795 int old_fd;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000796 if (fd < 0)
797 fd = -1;
Victor Stinner11517102014-07-29 23:31:34 +0200798
799#ifdef MS_WINDOWS
800 old_fd = Py_SAFE_DOWNCAST(wakeup.fd, SOCKET_T, int);
Victor Stinner11517102014-07-29 23:31:34 +0200801#else
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800802 old_fd = wakeup.fd;
Victor Stinner11517102014-07-29 23:31:34 +0200803#endif
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800804 wakeup.fd = fd;
805 wakeup.warn_on_full_buffer = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000806 return old_fd;
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000807}
808
809
Martin v. Löwis823725e2008-03-24 13:39:54 +0000810#ifdef HAVE_SETITIMER
Tal Einatc7027b72015-05-16 14:14:49 +0300811
812/*[clinic input]
813signal.setitimer
814
815 which: int
Victor Stinneref611c92017-10-13 13:49:43 -0700816 seconds: object
817 interval: object(c_default="NULL") = 0.0
Tal Einatc7027b72015-05-16 14:14:49 +0300818 /
819
820Sets given itimer (one of ITIMER_REAL, ITIMER_VIRTUAL or ITIMER_PROF).
821
822The timer will fire after value seconds and after that every interval seconds.
823The itimer can be cleared by setting seconds to zero.
824
825Returns old values as a tuple: (delay, interval).
826[clinic start generated code]*/
827
Martin v. Löwis823725e2008-03-24 13:39:54 +0000828static PyObject *
Victor Stinneref611c92017-10-13 13:49:43 -0700829signal_setitimer_impl(PyObject *module, int which, PyObject *seconds,
830 PyObject *interval)
831/*[clinic end generated code: output=65f9dcbddc35527b input=de43daf194e6f66f]*/
Martin v. Löwis823725e2008-03-24 13:39:54 +0000832{
Martin v. Löwis823725e2008-03-24 13:39:54 +0000833 struct itimerval new, old;
834
Victor Stinneref611c92017-10-13 13:49:43 -0700835 if (timeval_from_double(seconds, &new.it_value) < 0) {
836 return NULL;
837 }
838 if (timeval_from_double(interval, &new.it_interval) < 0) {
839 return NULL;
840 }
841
Martin v. Löwis823725e2008-03-24 13:39:54 +0000842 /* Let OS check "which" value */
843 if (setitimer(which, &new, &old) != 0) {
Tal Einatc7027b72015-05-16 14:14:49 +0300844 PyErr_SetFromErrno(ItimerError);
845 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000846 }
847
848 return itimer_retval(&old);
849}
850
Martin v. Löwis823725e2008-03-24 13:39:54 +0000851#endif
852
853
854#ifdef HAVE_GETITIMER
Tal Einatc7027b72015-05-16 14:14:49 +0300855
856/*[clinic input]
857signal.getitimer
858
859 which: int
860 /
861
862Returns current value of given itimer.
863[clinic start generated code]*/
864
Martin v. Löwis823725e2008-03-24 13:39:54 +0000865static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300866signal_getitimer_impl(PyObject *module, int which)
867/*[clinic end generated code: output=9e053175d517db40 input=f7d21d38f3490627]*/
Martin v. Löwis823725e2008-03-24 13:39:54 +0000868{
Martin v. Löwis823725e2008-03-24 13:39:54 +0000869 struct itimerval old;
870
Martin v. Löwis823725e2008-03-24 13:39:54 +0000871 if (getitimer(which, &old) != 0) {
Tal Einatc7027b72015-05-16 14:14:49 +0300872 PyErr_SetFromErrno(ItimerError);
873 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000874 }
875
876 return itimer_retval(&old);
877}
878
Martin v. Löwis823725e2008-03-24 13:39:54 +0000879#endif
880
Victor Stinnerb3e72192011-05-08 01:46:11 +0200881#if defined(PYPTHREAD_SIGMASK) || defined(HAVE_SIGPENDING)
Victor Stinner35b300c2011-05-04 13:20:35 +0200882static PyObject*
883sigset_to_set(sigset_t mask)
884{
885 PyObject *signum, *result;
886 int sig;
887
888 result = PySet_New(0);
889 if (result == NULL)
890 return NULL;
891
892 for (sig = 1; sig < NSIG; sig++) {
893 if (sigismember(&mask, sig) != 1)
894 continue;
895
896 /* Handle the case where it is a member by adding the signal to
897 the result list. Ignore the other cases because they mean the
898 signal isn't a member of the mask or the signal was invalid,
899 and an invalid signal must have been our fault in constructing
900 the loop boundaries. */
901 signum = PyLong_FromLong(sig);
902 if (signum == NULL) {
903 Py_DECREF(result);
904 return NULL;
905 }
906 if (PySet_Add(result, signum) == -1) {
907 Py_DECREF(signum);
908 Py_DECREF(result);
909 return NULL;
910 }
911 Py_DECREF(signum);
912 }
913 return result;
914}
Victor Stinnerb3e72192011-05-08 01:46:11 +0200915#endif
Victor Stinner35b300c2011-05-04 13:20:35 +0200916
Victor Stinnerb3e72192011-05-08 01:46:11 +0200917#ifdef PYPTHREAD_SIGMASK
Tal Einatc7027b72015-05-16 14:14:49 +0300918
919/*[clinic input]
920signal.pthread_sigmask
921
922 how: int
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300923 mask: sigset_t
Tal Einatc7027b72015-05-16 14:14:49 +0300924 /
925
926Fetch and/or change the signal mask of the calling thread.
927[clinic start generated code]*/
928
Victor Stinnera9293352011-04-30 15:21:58 +0200929static PyObject *
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300930signal_pthread_sigmask_impl(PyObject *module, int how, sigset_t mask)
931/*[clinic end generated code: output=0562c0fb192981a8 input=85bcebda442fa77f]*/
Victor Stinnera9293352011-04-30 15:21:58 +0200932{
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300933 sigset_t previous;
Victor Stinnera9293352011-04-30 15:21:58 +0200934 int err;
935
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300936 err = pthread_sigmask(how, &mask, &previous);
Victor Stinnera9293352011-04-30 15:21:58 +0200937 if (err != 0) {
938 errno = err;
Victor Stinnerb3e72192011-05-08 01:46:11 +0200939 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnera9293352011-04-30 15:21:58 +0200940 return NULL;
941 }
942
Victor Stinnerd0e516d2011-05-03 14:57:12 +0200943 /* if signals was unblocked, signal handlers have been called */
944 if (PyErr_CheckSignals())
945 return NULL;
946
Victor Stinner35b300c2011-05-04 13:20:35 +0200947 return sigset_to_set(previous);
Victor Stinnera9293352011-04-30 15:21:58 +0200948}
949
Victor Stinnera9293352011-04-30 15:21:58 +0200950#endif /* #ifdef PYPTHREAD_SIGMASK */
951
Martin v. Löwis823725e2008-03-24 13:39:54 +0000952
Victor Stinnerb3e72192011-05-08 01:46:11 +0200953#ifdef HAVE_SIGPENDING
Tal Einatc7027b72015-05-16 14:14:49 +0300954
955/*[clinic input]
956signal.sigpending
957
958Examine pending signals.
959
960Returns a set of signal numbers that are pending for delivery to
961the calling thread.
962[clinic start generated code]*/
963
Victor Stinnerb3e72192011-05-08 01:46:11 +0200964static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300965signal_sigpending_impl(PyObject *module)
966/*[clinic end generated code: output=53375ffe89325022 input=e0036c016f874e29]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +0200967{
968 int err;
969 sigset_t mask;
970 err = sigpending(&mask);
971 if (err)
972 return PyErr_SetFromErrno(PyExc_OSError);
973 return sigset_to_set(mask);
974}
975
Victor Stinnerb3e72192011-05-08 01:46:11 +0200976#endif /* #ifdef HAVE_SIGPENDING */
977
978
979#ifdef HAVE_SIGWAIT
Tal Einatc7027b72015-05-16 14:14:49 +0300980
981/*[clinic input]
982signal.sigwait
983
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300984 sigset: sigset_t
Tal Einatc7027b72015-05-16 14:14:49 +0300985 /
986
987Wait for a signal.
988
989Suspend execution of the calling thread until the delivery of one of the
990signals specified in the signal set sigset. The function accepts the signal
991and returns the signal number.
992[clinic start generated code]*/
993
Victor Stinnerb3e72192011-05-08 01:46:11 +0200994static PyObject *
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300995signal_sigwait_impl(PyObject *module, sigset_t sigset)
996/*[clinic end generated code: output=f43770699d682f96 input=a6fbd47b1086d119]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +0200997{
Victor Stinnerb3e72192011-05-08 01:46:11 +0200998 int err, signum;
999
Victor Stinner10c30d62011-06-10 01:39:53 +02001000 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001001 err = sigwait(&sigset, &signum);
Victor Stinner10c30d62011-06-10 01:39:53 +02001002 Py_END_ALLOW_THREADS
Victor Stinnerb3e72192011-05-08 01:46:11 +02001003 if (err) {
1004 errno = err;
1005 return PyErr_SetFromErrno(PyExc_OSError);
1006 }
1007
1008 return PyLong_FromLong(signum);
1009}
1010
Tal Einatc7027b72015-05-16 14:14:49 +03001011#endif /* #ifdef HAVE_SIGWAIT */
1012
Victor Stinnerb3e72192011-05-08 01:46:11 +02001013
Antoine Pitrou9d3627e2018-05-04 13:00:50 +02001014#if defined(HAVE_SIGFILLSET) || defined(MS_WINDOWS)
1015
1016/*[clinic input]
1017signal.valid_signals
1018
1019Return a set of valid signal numbers on this platform.
1020
1021The signal numbers returned by this function can be safely passed to
1022functions like `pthread_sigmask`.
1023[clinic start generated code]*/
1024
1025static PyObject *
1026signal_valid_signals_impl(PyObject *module)
1027/*[clinic end generated code: output=1609cffbcfcf1314 input=86a3717ff25288f2]*/
1028{
1029#ifdef MS_WINDOWS
1030#ifdef SIGBREAK
1031 PyObject *tup = Py_BuildValue("(iiiiiii)", SIGABRT, SIGBREAK, SIGFPE,
1032 SIGILL, SIGINT, SIGSEGV, SIGTERM);
1033#else
1034 PyObject *tup = Py_BuildValue("(iiiiii)", SIGABRT, SIGFPE, SIGILL,
1035 SIGINT, SIGSEGV, SIGTERM);
1036#endif
1037 if (tup == NULL) {
1038 return NULL;
1039 }
1040 PyObject *set = PySet_New(tup);
1041 Py_DECREF(tup);
1042 return set;
1043#else
1044 sigset_t mask;
1045 if (sigemptyset(&mask) || sigfillset(&mask)) {
1046 return PyErr_SetFromErrno(PyExc_OSError);
1047 }
1048 return sigset_to_set(mask);
1049#endif
1050}
1051
1052#endif /* #if defined(HAVE_SIGFILLSET) || defined(MS_WINDOWS) */
1053
1054
Ross Lagerwallbc808222011-06-25 12:13:40 +02001055#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
1056static int initialized;
1057static PyStructSequence_Field struct_siginfo_fields[] = {
1058 {"si_signo", "signal number"},
1059 {"si_code", "signal code"},
1060 {"si_errno", "errno associated with this signal"},
1061 {"si_pid", "sending process ID"},
1062 {"si_uid", "real user ID of sending process"},
1063 {"si_status", "exit value or signal"},
1064 {"si_band", "band event for SIGPOLL"},
1065 {0}
1066};
1067
1068PyDoc_STRVAR(struct_siginfo__doc__,
1069"struct_siginfo: Result from sigwaitinfo or sigtimedwait.\n\n\
1070This object may be accessed either as a tuple of\n\
1071(si_signo, si_code, si_errno, si_pid, si_uid, si_status, si_band),\n\
1072or via the attributes si_signo, si_code, and so on.");
1073
1074static PyStructSequence_Desc struct_siginfo_desc = {
1075 "signal.struct_siginfo", /* name */
1076 struct_siginfo__doc__, /* doc */
1077 struct_siginfo_fields, /* fields */
1078 7 /* n_in_sequence */
1079};
1080
1081static PyTypeObject SiginfoType;
1082
1083static PyObject *
1084fill_siginfo(siginfo_t *si)
1085{
1086 PyObject *result = PyStructSequence_New(&SiginfoType);
1087 if (!result)
1088 return NULL;
1089
1090 PyStructSequence_SET_ITEM(result, 0, PyLong_FromLong((long)(si->si_signo)));
1091 PyStructSequence_SET_ITEM(result, 1, PyLong_FromLong((long)(si->si_code)));
Victor Stinner09532fe2019-05-10 23:39:09 +02001092#ifdef __VXWORKS__
pxinwr8b5bdda2019-03-14 01:18:25 +08001093 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong(0L));
1094 PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong(0L));
1095 PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong(0L));
1096 PyStructSequence_SET_ITEM(result, 5, PyLong_FromLong(0L));
Victor Stinner09532fe2019-05-10 23:39:09 +02001097#else
Ross Lagerwallbc808222011-06-25 12:13:40 +02001098 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si->si_errno)));
1099 PyStructSequence_SET_ITEM(result, 3, PyLong_FromPid(si->si_pid));
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02001100 PyStructSequence_SET_ITEM(result, 4, _PyLong_FromUid(si->si_uid));
Ross Lagerwallbc808222011-06-25 12:13:40 +02001101 PyStructSequence_SET_ITEM(result, 5,
1102 PyLong_FromLong((long)(si->si_status)));
Victor Stinner09532fe2019-05-10 23:39:09 +02001103#endif
Zachary Ware6a6967e2016-10-01 00:47:27 -05001104#ifdef HAVE_SIGINFO_T_SI_BAND
Ross Lagerwallbc808222011-06-25 12:13:40 +02001105 PyStructSequence_SET_ITEM(result, 6, PyLong_FromLong(si->si_band));
Zachary Ware6a6967e2016-10-01 00:47:27 -05001106#else
1107 PyStructSequence_SET_ITEM(result, 6, PyLong_FromLong(0L));
1108#endif
Ross Lagerwallbc808222011-06-25 12:13:40 +02001109 if (PyErr_Occurred()) {
1110 Py_DECREF(result);
1111 return NULL;
1112 }
1113
1114 return result;
1115}
1116#endif
1117
1118#ifdef HAVE_SIGWAITINFO
Tal Einatc7027b72015-05-16 14:14:49 +03001119
1120/*[clinic input]
1121signal.sigwaitinfo
1122
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001123 sigset: sigset_t
Tal Einatc7027b72015-05-16 14:14:49 +03001124 /
1125
1126Wait synchronously until one of the signals in *sigset* is delivered.
1127
1128Returns a struct_siginfo containing information about the signal.
1129[clinic start generated code]*/
1130
Ross Lagerwallbc808222011-06-25 12:13:40 +02001131static PyObject *
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001132signal_sigwaitinfo_impl(PyObject *module, sigset_t sigset)
1133/*[clinic end generated code: output=1eb2f1fa236fdbca input=3d1a7e1f27fc664c]*/
Ross Lagerwallbc808222011-06-25 12:13:40 +02001134{
Ross Lagerwallbc808222011-06-25 12:13:40 +02001135 siginfo_t si;
1136 int err;
Victor Stinnera453cd82015-03-20 12:54:28 +01001137 int async_err = 0;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001138
Victor Stinnera453cd82015-03-20 12:54:28 +01001139 do {
1140 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001141 err = sigwaitinfo(&sigset, &si);
Victor Stinnera453cd82015-03-20 12:54:28 +01001142 Py_END_ALLOW_THREADS
1143 } while (err == -1
1144 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Ross Lagerwallbc808222011-06-25 12:13:40 +02001145 if (err == -1)
Victor Stinnera453cd82015-03-20 12:54:28 +01001146 return (!async_err) ? PyErr_SetFromErrno(PyExc_OSError) : NULL;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001147
1148 return fill_siginfo(&si);
1149}
1150
Ross Lagerwallbc808222011-06-25 12:13:40 +02001151#endif /* #ifdef HAVE_SIGWAITINFO */
1152
1153#ifdef HAVE_SIGTIMEDWAIT
Tal Einatc7027b72015-05-16 14:14:49 +03001154
1155/*[clinic input]
1156signal.sigtimedwait
1157
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001158 sigset: sigset_t
Serhiy Storchaka6b680cd2015-05-16 15:57:56 +03001159 timeout as timeout_obj: object
Tal Einatc7027b72015-05-16 14:14:49 +03001160 /
1161
1162Like sigwaitinfo(), but with a timeout.
1163
1164The timeout is specified in seconds, with floating point numbers allowed.
1165[clinic start generated code]*/
1166
Ross Lagerwallbc808222011-06-25 12:13:40 +02001167static PyObject *
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001168signal_sigtimedwait_impl(PyObject *module, sigset_t sigset,
Serhiy Storchaka6b680cd2015-05-16 15:57:56 +03001169 PyObject *timeout_obj)
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001170/*[clinic end generated code: output=59c8971e8ae18a64 input=87fd39237cf0b7ba]*/
Ross Lagerwallbc808222011-06-25 12:13:40 +02001171{
Victor Stinnera453cd82015-03-20 12:54:28 +01001172 struct timespec ts;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001173 siginfo_t si;
Victor Stinnera453cd82015-03-20 12:54:28 +01001174 int res;
Victor Stinner34dc0f42015-03-27 18:19:03 +01001175 _PyTime_t timeout, deadline, monotonic;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001176
Victor Stinner869e1772015-03-30 03:49:14 +02001177 if (_PyTime_FromSecondsObject(&timeout,
1178 timeout_obj, _PyTime_ROUND_CEILING) < 0)
Ross Lagerwallbc808222011-06-25 12:13:40 +02001179 return NULL;
1180
Victor Stinnera453cd82015-03-20 12:54:28 +01001181 if (timeout < 0) {
Ross Lagerwallbc808222011-06-25 12:13:40 +02001182 PyErr_SetString(PyExc_ValueError, "timeout must be non-negative");
1183 return NULL;
1184 }
1185
Victor Stinner34dc0f42015-03-27 18:19:03 +01001186 deadline = _PyTime_GetMonotonicClock() + timeout;
Victor Stinnera453cd82015-03-20 12:54:28 +01001187
1188 do {
Victor Stinner34dc0f42015-03-27 18:19:03 +01001189 if (_PyTime_AsTimespec(timeout, &ts) < 0)
1190 return NULL;
Victor Stinnera453cd82015-03-20 12:54:28 +01001191
1192 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001193 res = sigtimedwait(&sigset, &si, &ts);
Victor Stinnera453cd82015-03-20 12:54:28 +01001194 Py_END_ALLOW_THREADS
1195
1196 if (res != -1)
1197 break;
1198
1199 if (errno != EINTR) {
1200 if (errno == EAGAIN)
1201 Py_RETURN_NONE;
1202 else
1203 return PyErr_SetFromErrno(PyExc_OSError);
1204 }
1205
1206 /* sigtimedwait() was interrupted by a signal (EINTR) */
1207 if (PyErr_CheckSignals())
1208 return NULL;
1209
Victor Stinner34dc0f42015-03-27 18:19:03 +01001210 monotonic = _PyTime_GetMonotonicClock();
1211 timeout = deadline - monotonic;
Victor Stinner6aa446c2015-03-30 21:33:51 +02001212 if (timeout < 0)
Victor Stinnera453cd82015-03-20 12:54:28 +01001213 break;
1214 } while (1);
Ross Lagerwallbc808222011-06-25 12:13:40 +02001215
1216 return fill_siginfo(&si);
1217}
1218
Ross Lagerwallbc808222011-06-25 12:13:40 +02001219#endif /* #ifdef HAVE_SIGTIMEDWAIT */
1220
Victor Stinnerb3e72192011-05-08 01:46:11 +02001221
Antoine Pitroua6a4dc82017-09-07 18:56:24 +02001222#if defined(HAVE_PTHREAD_KILL)
Tal Einatc7027b72015-05-16 14:14:49 +03001223
1224/*[clinic input]
1225signal.pthread_kill
1226
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +02001227 thread_id: unsigned_long(bitwise=True)
Tal Einatc7027b72015-05-16 14:14:49 +03001228 signalnum: int
1229 /
1230
1231Send a signal to a thread.
1232[clinic start generated code]*/
1233
Victor Stinnerb3e72192011-05-08 01:46:11 +02001234static PyObject *
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +02001235signal_pthread_kill_impl(PyObject *module, unsigned long thread_id,
1236 int signalnum)
1237/*[clinic end generated code: output=7629919b791bc27f input=1d901f2c7bb544ff]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +02001238{
Victor Stinnerb3e72192011-05-08 01:46:11 +02001239 int err;
1240
Saiyang Gou7514f4f2020-02-12 23:47:42 -08001241 if (PySys_Audit("signal.pthread_kill", "ki", thread_id, signalnum) < 0) {
1242 return NULL;
1243 }
1244
Tal Einatc7027b72015-05-16 14:14:49 +03001245 err = pthread_kill((pthread_t)thread_id, signalnum);
Victor Stinnerb3e72192011-05-08 01:46:11 +02001246 if (err != 0) {
1247 errno = err;
1248 PyErr_SetFromErrno(PyExc_OSError);
1249 return NULL;
1250 }
1251
1252 /* the signal may have been send to the current thread */
1253 if (PyErr_CheckSignals())
1254 return NULL;
1255
1256 Py_RETURN_NONE;
1257}
1258
Antoine Pitroua6a4dc82017-09-07 18:56:24 +02001259#endif /* #if defined(HAVE_PTHREAD_KILL) */
Victor Stinnerb3e72192011-05-08 01:46:11 +02001260
1261
Benjamin Peterson74834512019-11-19 20:39:14 -08001262#if defined(__linux__) && defined(__NR_pidfd_send_signal)
1263/*[clinic input]
1264signal.pidfd_send_signal
1265
1266 pidfd: int
1267 signalnum: int
1268 siginfo: object = None
1269 flags: int = 0
1270 /
1271
1272Send a signal to a process referred to by a pid file descriptor.
1273[clinic start generated code]*/
1274
1275static PyObject *
1276signal_pidfd_send_signal_impl(PyObject *module, int pidfd, int signalnum,
1277 PyObject *siginfo, int flags)
1278/*[clinic end generated code: output=2d59f04a75d9cbdf input=2a6543a1f4ac2000]*/
1279
1280{
1281 if (siginfo != Py_None) {
1282 PyErr_SetString(PyExc_TypeError, "siginfo must be None");
1283 return NULL;
1284 }
1285 if (syscall(__NR_pidfd_send_signal, pidfd, signalnum, NULL, flags) < 0) {
1286 PyErr_SetFromErrno(PyExc_OSError);
1287 return NULL;
1288 }
1289 Py_RETURN_NONE;
1290}
1291#endif
1292
1293
Victor Stinnerb3e72192011-05-08 01:46:11 +02001294
Tal Einatc7027b72015-05-16 14:14:49 +03001295/* List of functions defined in the module -- some of the methoddefs are
1296 defined to nothing if the corresponding C function is not available. */
Barry Warsaw92971171997-01-03 00:14:25 +00001297static PyMethodDef signal_methods[] = {
Tal Einatc7027b72015-05-16 14:14:49 +03001298 {"default_int_handler", signal_default_int_handler, METH_VARARGS, default_int_handler_doc},
1299 SIGNAL_ALARM_METHODDEF
1300 SIGNAL_SETITIMER_METHODDEF
1301 SIGNAL_GETITIMER_METHODDEF
1302 SIGNAL_SIGNAL_METHODDEF
Vladimir Matveevc24c6c22019-01-08 01:58:25 -08001303 SIGNAL_RAISE_SIGNAL_METHODDEF
Antoine Pietri5d2a27d2018-03-12 14:42:34 +01001304 SIGNAL_STRSIGNAL_METHODDEF
Tal Einatc7027b72015-05-16 14:14:49 +03001305 SIGNAL_GETSIGNAL_METHODDEF
Serhiy Storchaka62be7422018-11-27 13:27:31 +02001306 {"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 +03001307 SIGNAL_SIGINTERRUPT_METHODDEF
1308 SIGNAL_PAUSE_METHODDEF
Benjamin Peterson74834512019-11-19 20:39:14 -08001309 SIGNAL_PIDFD_SEND_SIGNAL_METHODDEF
Tal Einatc7027b72015-05-16 14:14:49 +03001310 SIGNAL_PTHREAD_KILL_METHODDEF
1311 SIGNAL_PTHREAD_SIGMASK_METHODDEF
1312 SIGNAL_SIGPENDING_METHODDEF
1313 SIGNAL_SIGWAIT_METHODDEF
1314 SIGNAL_SIGWAITINFO_METHODDEF
1315 SIGNAL_SIGTIMEDWAIT_METHODDEF
Antoine Pitrou9d3627e2018-05-04 13:00:50 +02001316#if defined(HAVE_SIGFILLSET) || defined(MS_WINDOWS)
1317 SIGNAL_VALID_SIGNALS_METHODDEF
1318#endif
Tal Einatc7027b72015-05-16 14:14:49 +03001319 {NULL, NULL} /* sentinel */
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001320};
1321
Barry Warsaw92971171997-01-03 00:14:25 +00001322
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001323PyDoc_STRVAR(module_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001324"This module provides mechanisms to use signal handlers in Python.\n\
1325\n\
1326Functions:\n\
1327\n\
1328alarm() -- cause SIGALRM after a specified time [Unix only]\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001329setitimer() -- cause a signal (described below) after a specified\n\
1330 float time and the timer may restart then [Unix only]\n\
1331getitimer() -- get current value of timer [Unix only]\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001332signal() -- set the action for a given signal\n\
1333getsignal() -- get the signal action for a given signal\n\
1334pause() -- wait until a signal arrives [Unix only]\n\
1335default_int_handler() -- default SIGINT handler\n\
1336\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001337signal constants:\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001338SIG_DFL -- used to refer to the system default handler\n\
1339SIG_IGN -- used to ignore the signal\n\
1340NSIG -- number of defined signals\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001341SIGINT, SIGTERM, etc. -- signal numbers\n\
1342\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001343itimer constants:\n\
1344ITIMER_REAL -- decrements in real time, and delivers SIGALRM upon\n\
1345 expiration\n\
1346ITIMER_VIRTUAL -- decrements only when the process is executing,\n\
1347 and delivers SIGVTALRM upon expiration\n\
1348ITIMER_PROF -- decrements both when the process is executing and\n\
1349 when the system is executing on behalf of the process.\n\
1350 Coupled with ITIMER_VIRTUAL, this timer is usually\n\
1351 used to profile the time spent by the application\n\
1352 in user and kernel space. SIGPROF is delivered upon\n\
1353 expiration.\n\
1354\n\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001355*** IMPORTANT NOTICE ***\n\
1356A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001357the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001358
Martin v. Löwis1a214512008-06-11 05:26:20 +00001359static struct PyModuleDef signalmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001360 PyModuleDef_HEAD_INIT,
Brett Cannon815a6f32014-04-04 10:20:28 -04001361 "_signal",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001362 module_doc,
1363 -1,
1364 signal_methods,
1365 NULL,
1366 NULL,
1367 NULL,
1368 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001369};
1370
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001371PyMODINIT_FUNC
Giampaolo Rodola'e09fb712014-04-04 15:34:17 +02001372PyInit__signal(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001373{
animalize77643c42019-09-09 21:46:26 +08001374 PyObject *m, *d;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001375 int i;
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001376
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001377 /* Create the module and add the functions */
1378 m = PyModule_Create(&signalmodule);
1379 if (m == NULL)
1380 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001381
Ross Lagerwallbc808222011-06-25 12:13:40 +02001382#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
Victor Stinner1c8f0592013-07-22 22:24:54 +02001383 if (!initialized) {
1384 if (PyStructSequence_InitType2(&SiginfoType, &struct_siginfo_desc) < 0)
1385 return NULL;
1386 }
Ross Lagerwallbc808222011-06-25 12:13:40 +02001387 Py_INCREF((PyObject*) &SiginfoType);
1388 PyModule_AddObject(m, "struct_siginfo", (PyObject*) &SiginfoType);
1389 initialized = 1;
1390#endif
1391
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001392 /* Add some symbolic constants to the module */
1393 d = PyModule_GetDict(m);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001394
animalize77643c42019-09-09 21:46:26 +08001395 DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL);
1396 if (!DefaultHandler ||
1397 PyDict_SetItemString(d, "SIG_DFL", DefaultHandler) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001398 goto finally;
animalize77643c42019-09-09 21:46:26 +08001399 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001400
animalize77643c42019-09-09 21:46:26 +08001401 IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN);
1402 if (!IgnoreHandler ||
1403 PyDict_SetItemString(d, "SIG_IGN", IgnoreHandler) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001404 goto finally;
animalize77643c42019-09-09 21:46:26 +08001405 }
Barry Warsaw92971171997-01-03 00:14:25 +00001406
Joannah Nanjekye9541bd32019-04-21 21:47:06 -04001407 if (PyModule_AddIntMacro(m, NSIG))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001408 goto finally;
Barry Warsaw92971171997-01-03 00:14:25 +00001409
Victor Stinnera9293352011-04-30 15:21:58 +02001410#ifdef SIG_BLOCK
Victor Stinner72c53b52011-05-02 16:15:43 +02001411 if (PyModule_AddIntMacro(m, SIG_BLOCK))
1412 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001413#endif
Victor Stinnera9293352011-04-30 15:21:58 +02001414#ifdef SIG_UNBLOCK
Victor Stinner72c53b52011-05-02 16:15:43 +02001415 if (PyModule_AddIntMacro(m, SIG_UNBLOCK))
1416 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001417#endif
Victor Stinnera9293352011-04-30 15:21:58 +02001418#ifdef SIG_SETMASK
Victor Stinner72c53b52011-05-02 16:15:43 +02001419 if (PyModule_AddIntMacro(m, SIG_SETMASK))
1420 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001421#endif
1422
animalize77643c42019-09-09 21:46:26 +08001423 IntHandler = PyDict_GetItemString(d, "default_int_handler");
1424 if (!IntHandler)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001425 goto finally;
1426 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +00001427
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001428 _Py_atomic_store_relaxed(&Handlers[0].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001429 for (i = 1; i < NSIG; i++) {
1430 void (*t)(int);
1431 t = PyOS_getsig(i);
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001432 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001433 if (t == SIG_DFL)
1434 Handlers[i].func = DefaultHandler;
1435 else if (t == SIG_IGN)
1436 Handlers[i].func = IgnoreHandler;
1437 else
1438 Handlers[i].func = Py_None; /* None of our business */
1439 Py_INCREF(Handlers[i].func);
1440 }
1441 if (Handlers[SIGINT].func == DefaultHandler) {
1442 /* Install default int handler */
1443 Py_INCREF(IntHandler);
Serhiy Storchaka57a01d32016-04-10 18:05:40 +03001444 Py_SETREF(Handlers[SIGINT].func, IntHandler);
pkerlinge905c842018-06-01 09:47:18 +00001445 PyOS_setsig(SIGINT, signal_handler);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001446 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001447
1448#ifdef SIGHUP
Christian Heimes6782b142016-09-09 00:11:45 +02001449 if (PyModule_AddIntMacro(m, SIGHUP))
1450 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001451#endif
1452#ifdef SIGINT
Christian Heimes6782b142016-09-09 00:11:45 +02001453 if (PyModule_AddIntMacro(m, SIGINT))
1454 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001455#endif
Tim Peters1ce3cf72001-10-01 17:58:40 +00001456#ifdef SIGBREAK
Christian Heimes6782b142016-09-09 00:11:45 +02001457 if (PyModule_AddIntMacro(m, SIGBREAK))
1458 goto finally;
Tim Peters1ce3cf72001-10-01 17:58:40 +00001459#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001460#ifdef SIGQUIT
Christian Heimes6782b142016-09-09 00:11:45 +02001461 if (PyModule_AddIntMacro(m, SIGQUIT))
1462 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001463#endif
1464#ifdef SIGILL
Christian Heimes6782b142016-09-09 00:11:45 +02001465 if (PyModule_AddIntMacro(m, SIGILL))
1466 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001467#endif
1468#ifdef SIGTRAP
Christian Heimes6782b142016-09-09 00:11:45 +02001469 if (PyModule_AddIntMacro(m, SIGTRAP))
1470 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001471#endif
1472#ifdef SIGIOT
Christian Heimes6782b142016-09-09 00:11:45 +02001473 if (PyModule_AddIntMacro(m, SIGIOT))
1474 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001475#endif
1476#ifdef SIGABRT
Christian Heimes6782b142016-09-09 00:11:45 +02001477 if (PyModule_AddIntMacro(m, SIGABRT))
1478 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001479#endif
1480#ifdef SIGEMT
Christian Heimes6782b142016-09-09 00:11:45 +02001481 if (PyModule_AddIntMacro(m, SIGEMT))
1482 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001483#endif
1484#ifdef SIGFPE
Christian Heimes6782b142016-09-09 00:11:45 +02001485 if (PyModule_AddIntMacro(m, SIGFPE))
1486 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001487#endif
1488#ifdef SIGKILL
Christian Heimes6782b142016-09-09 00:11:45 +02001489 if (PyModule_AddIntMacro(m, SIGKILL))
1490 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001491#endif
1492#ifdef SIGBUS
Christian Heimes6782b142016-09-09 00:11:45 +02001493 if (PyModule_AddIntMacro(m, SIGBUS))
1494 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001495#endif
1496#ifdef SIGSEGV
Christian Heimes6782b142016-09-09 00:11:45 +02001497 if (PyModule_AddIntMacro(m, SIGSEGV))
1498 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001499#endif
1500#ifdef SIGSYS
Christian Heimes6782b142016-09-09 00:11:45 +02001501 if (PyModule_AddIntMacro(m, SIGSYS))
1502 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001503#endif
1504#ifdef SIGPIPE
Christian Heimes6782b142016-09-09 00:11:45 +02001505 if (PyModule_AddIntMacro(m, SIGPIPE))
1506 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001507#endif
1508#ifdef SIGALRM
Christian Heimes6782b142016-09-09 00:11:45 +02001509 if (PyModule_AddIntMacro(m, SIGALRM))
1510 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001511#endif
1512#ifdef SIGTERM
Christian Heimes6782b142016-09-09 00:11:45 +02001513 if (PyModule_AddIntMacro(m, SIGTERM))
1514 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001515#endif
1516#ifdef SIGUSR1
Christian Heimes6782b142016-09-09 00:11:45 +02001517 if (PyModule_AddIntMacro(m, SIGUSR1))
1518 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001519#endif
1520#ifdef SIGUSR2
Christian Heimes6782b142016-09-09 00:11:45 +02001521 if (PyModule_AddIntMacro(m, SIGUSR2))
1522 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001523#endif
1524#ifdef SIGCLD
Christian Heimes6782b142016-09-09 00:11:45 +02001525 if (PyModule_AddIntMacro(m, SIGCLD))
1526 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001527#endif
1528#ifdef SIGCHLD
Christian Heimes6782b142016-09-09 00:11:45 +02001529 if (PyModule_AddIntMacro(m, SIGCHLD))
1530 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001531#endif
1532#ifdef SIGPWR
Christian Heimes6782b142016-09-09 00:11:45 +02001533 if (PyModule_AddIntMacro(m, SIGPWR))
1534 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001535#endif
1536#ifdef SIGIO
Christian Heimes6782b142016-09-09 00:11:45 +02001537 if (PyModule_AddIntMacro(m, SIGIO))
1538 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001539#endif
1540#ifdef SIGURG
Christian Heimes6782b142016-09-09 00:11:45 +02001541 if (PyModule_AddIntMacro(m, SIGURG))
1542 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001543#endif
1544#ifdef SIGWINCH
Christian Heimes6782b142016-09-09 00:11:45 +02001545 if (PyModule_AddIntMacro(m, SIGWINCH))
1546 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001547#endif
1548#ifdef SIGPOLL
Christian Heimes6782b142016-09-09 00:11:45 +02001549 if (PyModule_AddIntMacro(m, SIGPOLL))
1550 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001551#endif
1552#ifdef SIGSTOP
Christian Heimes6782b142016-09-09 00:11:45 +02001553 if (PyModule_AddIntMacro(m, SIGSTOP))
1554 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001555#endif
1556#ifdef SIGTSTP
Christian Heimes6782b142016-09-09 00:11:45 +02001557 if (PyModule_AddIntMacro(m, SIGTSTP))
1558 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001559#endif
1560#ifdef SIGCONT
Christian Heimes6782b142016-09-09 00:11:45 +02001561 if (PyModule_AddIntMacro(m, SIGCONT))
1562 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001563#endif
1564#ifdef SIGTTIN
Christian Heimes6782b142016-09-09 00:11:45 +02001565 if (PyModule_AddIntMacro(m, SIGTTIN))
1566 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001567#endif
1568#ifdef SIGTTOU
Christian Heimes6782b142016-09-09 00:11:45 +02001569 if (PyModule_AddIntMacro(m, SIGTTOU))
1570 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001571#endif
1572#ifdef SIGVTALRM
Christian Heimes6782b142016-09-09 00:11:45 +02001573 if (PyModule_AddIntMacro(m, SIGVTALRM))
1574 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001575#endif
1576#ifdef SIGPROF
Christian Heimes6782b142016-09-09 00:11:45 +02001577 if (PyModule_AddIntMacro(m, SIGPROF))
1578 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001579#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001580#ifdef SIGXCPU
Christian Heimes6782b142016-09-09 00:11:45 +02001581 if (PyModule_AddIntMacro(m, SIGXCPU))
1582 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001583#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001584#ifdef SIGXFSZ
Christian Heimes6782b142016-09-09 00:11:45 +02001585 if (PyModule_AddIntMacro(m, SIGXFSZ))
1586 goto finally;
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001587#endif
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001588#ifdef SIGRTMIN
Christian Heimes6782b142016-09-09 00:11:45 +02001589 if (PyModule_AddIntMacro(m, SIGRTMIN))
1590 goto finally;
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001591#endif
1592#ifdef SIGRTMAX
Christian Heimes6782b142016-09-09 00:11:45 +02001593 if (PyModule_AddIntMacro(m, SIGRTMAX))
1594 goto finally;
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001595#endif
Martin v. Löwis175af252002-01-12 11:43:25 +00001596#ifdef SIGINFO
Christian Heimes6782b142016-09-09 00:11:45 +02001597 if (PyModule_AddIntMacro(m, SIGINFO))
1598 goto finally;
Martin v. Löwis175af252002-01-12 11:43:25 +00001599#endif
Martin v. Löwis823725e2008-03-24 13:39:54 +00001600
1601#ifdef ITIMER_REAL
Christian Heimes6782b142016-09-09 00:11:45 +02001602 if (PyModule_AddIntMacro(m, ITIMER_REAL))
1603 goto finally;
Martin v. Löwis823725e2008-03-24 13:39:54 +00001604#endif
1605#ifdef ITIMER_VIRTUAL
Christian Heimes6782b142016-09-09 00:11:45 +02001606 if (PyModule_AddIntMacro(m, ITIMER_VIRTUAL))
1607 goto finally;
Martin v. Löwis823725e2008-03-24 13:39:54 +00001608#endif
1609#ifdef ITIMER_PROF
Christian Heimes6782b142016-09-09 00:11:45 +02001610 if (PyModule_AddIntMacro(m, ITIMER_PROF))
1611 goto finally;
Martin v. Löwis823725e2008-03-24 13:39:54 +00001612#endif
1613
1614#if defined (HAVE_SETITIMER) || defined (HAVE_GETITIMER)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001615 ItimerError = PyErr_NewException("signal.ItimerError",
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +03001616 PyExc_OSError, NULL);
animalize77643c42019-09-09 21:46:26 +08001617 if (!ItimerError ||
1618 PyDict_SetItemString(d, "ItimerError", ItimerError) < 0) {
Joannah Nanjekye9541bd32019-04-21 21:47:06 -04001619 goto finally;
animalize77643c42019-09-09 21:46:26 +08001620 }
Martin v. Löwis823725e2008-03-24 13:39:54 +00001621#endif
1622
Brian Curtineb24d742010-04-12 17:16:38 +00001623#ifdef CTRL_C_EVENT
Christian Heimes6782b142016-09-09 00:11:45 +02001624 if (PyModule_AddIntMacro(m, CTRL_C_EVENT))
1625 goto finally;
Brian Curtineb24d742010-04-12 17:16:38 +00001626#endif
1627
1628#ifdef CTRL_BREAK_EVENT
Christian Heimes6782b142016-09-09 00:11:45 +02001629 if (PyModule_AddIntMacro(m, CTRL_BREAK_EVENT))
1630 goto finally;
Brian Curtineb24d742010-04-12 17:16:38 +00001631#endif
1632
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001633#ifdef MS_WINDOWS
1634 /* Create manual-reset event, initially unset */
1635 sigint_event = CreateEvent(NULL, TRUE, FALSE, FALSE);
1636#endif
1637
Martin v. Löwis1a214512008-06-11 05:26:20 +00001638 if (PyErr_Occurred()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001639 Py_DECREF(m);
1640 m = NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001641 }
Barry Warsaw92971171997-01-03 00:14:25 +00001642
Barry Warsaw92971171997-01-03 00:14:25 +00001643 finally:
Martin v. Löwis1a214512008-06-11 05:26:20 +00001644 return m;
Guido van Rossum08c16611997-08-02 03:01:42 +00001645}
1646
1647static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001648finisignal(void)
Guido van Rossum08c16611997-08-02 03:01:42 +00001649{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001650 int i;
1651 PyObject *func;
Guido van Rossum08c16611997-08-02 03:01:42 +00001652
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001653 for (i = 1; i < NSIG; i++) {
1654 func = Handlers[i].func;
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001655 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001656 Handlers[i].func = NULL;
pkerlinge905c842018-06-01 09:47:18 +00001657 if (func != NULL && func != Py_None &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001658 func != DefaultHandler && func != IgnoreHandler)
1659 PyOS_setsig(i, SIG_DFL);
1660 Py_XDECREF(func);
1661 }
Guido van Rossum08c16611997-08-02 03:01:42 +00001662
Serhiy Storchaka505ff752014-02-09 13:33:53 +02001663 Py_CLEAR(IntHandler);
1664 Py_CLEAR(DefaultHandler);
1665 Py_CLEAR(IgnoreHandler);
animalize77643c42019-09-09 21:46:26 +08001666#ifdef HAVE_GETITIMER
1667 Py_CLEAR(ItimerError);
1668#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001669}
1670
Barry Warsaw92971171997-01-03 00:14:25 +00001671
Barry Warsaw92971171997-01-03 00:14:25 +00001672/* Declared in pyerrors.h */
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001673int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001674PyErr_CheckSignals(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001675{
Victor Stinnerd2a8e5b2020-03-20 13:38:58 +01001676 if (!thread_can_handle_signals()) {
Eric Snow64d6cc82019-02-23 15:40:43 -07001677 return 0;
1678 }
1679
1680 return _PyErr_CheckSignals();
1681}
1682
1683
1684/* Declared in cpython/pyerrors.h */
1685int
1686_PyErr_CheckSignals(void)
1687{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001688 int i;
1689 PyObject *f;
Barry Warsaw92971171997-01-03 00:14:25 +00001690
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001691 if (!_Py_atomic_load(&is_tripped))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001692 return 0;
Christian Heimesb76922a2007-12-11 01:06:40 +00001693
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001694 /*
1695 * The is_tripped variable is meant to speed up the calls to
1696 * PyErr_CheckSignals (both directly or via pending calls) when no
1697 * signal has arrived. This variable is set to 1 when a signal arrives
1698 * and it is set to 0 here, when we know some signals arrived. This way
1699 * we can run the registered handlers with no signals blocked.
1700 *
1701 * NOTE: with this approach we can have a situation where is_tripped is
1702 * 1 but we have no more signals to handle (Handlers[i].tripped
1703 * is 0 for every signal i). This won't do us any harm (except
1704 * we're gonna spent some cycles for nothing). This happens when
1705 * we receive a signal i after we zero is_tripped and before we
1706 * check Handlers[i].tripped.
1707 */
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001708 _Py_atomic_store(&is_tripped, 0);
Christian Heimesb76922a2007-12-11 01:06:40 +00001709
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001710 if (!(f = (PyObject *)PyEval_GetFrame()))
1711 f = Py_None;
Christian Heimesb76922a2007-12-11 01:06:40 +00001712
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001713 for (i = 1; i < NSIG; i++) {
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001714 if (_Py_atomic_load_relaxed(&Handlers[i].tripped)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001715 PyObject *result = NULL;
1716 PyObject *arglist = Py_BuildValue("(iO)", i, f);
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001717 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Barry Warsaw92971171997-01-03 00:14:25 +00001718
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001719 if (arglist) {
Jeroen Demeyer1dbd0842019-07-11 17:57:32 +02001720 result = PyObject_Call(Handlers[i].func, arglist, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001721 Py_DECREF(arglist);
1722 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +02001723 if (!result) {
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001724 _Py_atomic_store(&is_tripped, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001725 return -1;
Antoine Pitrouc08177a2017-06-28 23:29:29 +02001726 }
Barry Warsaw92971171997-01-03 00:14:25 +00001727
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001728 Py_DECREF(result);
1729 }
1730 }
Christian Heimesb76922a2007-12-11 01:06:40 +00001731
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001732 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001733}
1734
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +00001735
Matěj Cepl608876b2019-05-23 22:30:00 +02001736/* Simulate the effect of a signal.SIGINT signal arriving. The next time
1737 PyErr_CheckSignals is called, the Python SIGINT signal handler will be
1738 raised.
1739
1740 Missing signal handler for the SIGINT signal is silently ignored. */
Barry Warsaw92971171997-01-03 00:14:25 +00001741void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001742PyErr_SetInterrupt(void)
Barry Warsaw92971171997-01-03 00:14:25 +00001743{
Matěj Cepl608876b2019-05-23 22:30:00 +02001744 if ((Handlers[SIGINT].func != IgnoreHandler) &&
1745 (Handlers[SIGINT].func != DefaultHandler)) {
1746 trip_signal(SIGINT);
1747 }
Barry Warsaw92971171997-01-03 00:14:25 +00001748}
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001749
1750void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001751PyOS_InitInterrupts(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001752{
Giampaolo Rodola'e09fb712014-04-04 15:34:17 +02001753 PyObject *m = PyImport_ImportModule("_signal");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001754 if (m) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001755 Py_DECREF(m);
1756 }
Guido van Rossum08c16611997-08-02 03:01:42 +00001757}
1758
1759void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001760PyOS_FiniInterrupts(void)
Guido van Rossum08c16611997-08-02 03:01:42 +00001761{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001762 finisignal();
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001763}
1764
1765int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001766PyOS_InterruptOccurred(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001767{
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001768 if (_Py_atomic_load_relaxed(&Handlers[SIGINT].tripped)) {
Victor Stinnerd2a8e5b2020-03-20 13:38:58 +01001769 if (!thread_can_handle_signals()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001770 return 0;
Eric Snow64d6cc82019-02-23 15:40:43 -07001771 }
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001772 _Py_atomic_store_relaxed(&Handlers[SIGINT].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001773 return 1;
1774 }
1775 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001776}
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001777
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001778static void
1779_clear_pending_signals(void)
1780{
1781 int i;
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001782 if (!_Py_atomic_load(&is_tripped))
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001783 return;
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001784 _Py_atomic_store(&is_tripped, 0);
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001785 for (i = 1; i < NSIG; ++i) {
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001786 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001787 }
1788}
1789
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001790void
Antoine Pitrou346cbd32017-05-27 17:50:54 +02001791_PySignal_AfterFork(void)
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001792{
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001793 /* Clear the signal flags after forking so that they aren't handled
1794 * in both processes if they came in just before the fork() but before
1795 * the interpreter had an opportunity to call the handlers. issue9535. */
1796 _clear_pending_signals();
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001797}
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001798
1799int
1800_PyOS_IsMainThread(void)
1801{
Victor Stinnerd2a8e5b2020-03-20 13:38:58 +01001802 return thread_can_handle_signals();
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001803}
1804
1805#ifdef MS_WINDOWS
1806void *_PyOS_SigintEvent(void)
1807{
1808 /* Returns a manual-reset event which gets tripped whenever
1809 SIGINT is received.
1810
1811 Python.h does not include windows.h so we do cannot use HANDLE
1812 as the return type of this function. We use void* instead. */
1813 return sigint_event;
1814}
1815#endif