blob: 9041848cadf07da05cb6b5b56919b8cff3ee1b2c [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
Guido van Rossume4485b01994-09-07 14:32:49 +0000192static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000193signal_default_int_handler(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000194{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000195 PyErr_SetNone(PyExc_KeyboardInterrupt);
196 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000197}
198
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000199PyDoc_STRVAR(default_int_handler_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000200"default_int_handler(...)\n\
201\n\
Michael W. Hudson24ec2112004-06-17 15:55:53 +0000202The default handler for SIGINT installed by Python.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000203It raises KeyboardInterrupt.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000204
Thomas Wouters0796b002000-07-22 23:49:30 +0000205
206static int
Victor Stinner11517102014-07-29 23:31:34 +0200207report_wakeup_write_error(void *data)
Antoine Pitrou6f6ec372013-08-17 20:27:56 +0200208{
Eric Snowfdf282d2019-01-11 14:26:55 -0700209 PyObject *exc, *val, *tb;
Antoine Pitrou6f6ec372013-08-17 20:27:56 +0200210 int save_errno = errno;
Benjamin Petersonca470632016-09-06 13:47:26 -0700211 errno = (int) (intptr_t) data;
Eric Snowfdf282d2019-01-11 14:26:55 -0700212 PyErr_Fetch(&exc, &val, &tb);
Antoine Pitrou6f6ec372013-08-17 20:27:56 +0200213 PyErr_SetFromErrno(PyExc_OSError);
214 PySys_WriteStderr("Exception ignored when trying to write to the "
215 "signal wakeup fd:\n");
216 PyErr_WriteUnraisable(NULL);
Eric Snowfdf282d2019-01-11 14:26:55 -0700217 PyErr_Restore(exc, val, tb);
Antoine Pitrou6f6ec372013-08-17 20:27:56 +0200218 errno = save_errno;
219 return 0;
220}
221
Victor Stinner11517102014-07-29 23:31:34 +0200222#ifdef MS_WINDOWS
223static int
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800224report_wakeup_send_error(void* data)
Victor Stinner11517102014-07-29 23:31:34 +0200225{
Eric Snowfdf282d2019-01-11 14:26:55 -0700226 PyObject *exc, *val, *tb;
227 PyErr_Fetch(&exc, &val, &tb);
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800228 /* PyErr_SetExcFromWindowsErr() invokes FormatMessage() which
229 recognizes the error codes used by both GetLastError() and
230 WSAGetLastError */
231 PyErr_SetExcFromWindowsErr(PyExc_OSError, (int) (intptr_t) data);
Victor Stinner11517102014-07-29 23:31:34 +0200232 PySys_WriteStderr("Exception ignored when trying to send to the "
233 "signal wakeup fd:\n");
234 PyErr_WriteUnraisable(NULL);
Eric Snowfdf282d2019-01-11 14:26:55 -0700235 PyErr_Restore(exc, val, tb);
Victor Stinner11517102014-07-29 23:31:34 +0200236 return 0;
237}
238#endif /* MS_WINDOWS */
239
Tim Peters4f1b2082000-07-23 21:18:09 +0000240static void
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200241trip_signal(int sig_num)
242{
Victor Stinnerd49b1f12011-05-08 02:03:15 +0200243 unsigned char byte;
Victor Stinner11517102014-07-29 23:31:34 +0200244 int fd;
245 Py_ssize_t rc;
Victor Stinnerc13ef662011-05-25 02:35:58 +0200246
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +0200247 _Py_atomic_store_relaxed(&Handlers[sig_num].tripped, 1);
Victor Stinner11517102014-07-29 23:31:34 +0200248
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200249 /* Set is_tripped after setting .tripped, as it gets
250 cleared in PyErr_CheckSignals() before .tripped. */
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +0200251 _Py_atomic_store(&is_tripped, 1);
252
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200253 /* Signals are always handled by the main interpreter */
254 PyInterpreterState *interp = _PyRuntime.interpreters.main;
Victor Stinner8849e592020-03-18 19:28:53 +0100255
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +0200256 /* Notify ceval.c */
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200257 _PyEval_SignalReceived(interp);
Nathaniel J. Smith4ae01492017-05-16 14:12:11 -0700258
259 /* And then write to the wakeup fd *after* setting all the globals and
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200260 doing the _PyEval_SignalReceived. We used to write to the wakeup fd
261 and then set the flag, but this allowed the following sequence of events
262 (especially on windows, where trip_signal may run in a new thread):
Nathaniel J. Smith4ae01492017-05-16 14:12:11 -0700263
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800264 - main thread blocks on select([wakeup.fd], ...)
Nathaniel J. Smith4ae01492017-05-16 14:12:11 -0700265 - signal arrives
266 - trip_signal writes to the wakeup fd
267 - the main thread wakes up
268 - the main thread checks the signal flags, sees that they're unset
269 - the main thread empties the wakeup fd
270 - the main thread goes back to sleep
271 - trip_signal sets the flags to request the Python-level signal handler
272 be run
273 - the main thread doesn't notice, because it's asleep
274
275 See bpo-30038 for more details.
276 */
277
Victor Stinner11517102014-07-29 23:31:34 +0200278#ifdef MS_WINDOWS
279 fd = Py_SAFE_DOWNCAST(wakeup.fd, SOCKET_T, int);
280#else
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800281 fd = wakeup.fd;
Victor Stinner11517102014-07-29 23:31:34 +0200282#endif
283
284 if (fd != INVALID_FD) {
Victor Stinnerc13ef662011-05-25 02:35:58 +0200285 byte = (unsigned char)sig_num;
Victor Stinner11517102014-07-29 23:31:34 +0200286#ifdef MS_WINDOWS
287 if (wakeup.use_send) {
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800288 rc = send(fd, &byte, 1, 0);
Victor Stinner11517102014-07-29 23:31:34 +0200289
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800290 if (rc < 0) {
291 int last_error = GetLastError();
292 if (wakeup.warn_on_full_buffer ||
293 last_error != WSAEWOULDBLOCK)
294 {
Victor Stinner50e6e992020-03-19 02:41:21 +0100295 /* _PyEval_AddPendingCall() isn't signal-safe, but we
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800296 still use it for this exceptional case. */
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200297 _PyEval_AddPendingCall(interp,
Victor Stinner09532fe2019-05-10 23:39:09 +0200298 report_wakeup_send_error,
299 (void *)(intptr_t) last_error);
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800300 }
Victor Stinner11517102014-07-29 23:31:34 +0200301 }
302 }
303 else
304#endif
305 {
Victor Stinnere72fe392015-04-01 18:35:22 +0200306 /* _Py_write_noraise() retries write() if write() is interrupted by
307 a signal (fails with EINTR). */
308 rc = _Py_write_noraise(fd, &byte, 1);
Victor Stinner11517102014-07-29 23:31:34 +0200309
310 if (rc < 0) {
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800311 if (wakeup.warn_on_full_buffer ||
312 (errno != EWOULDBLOCK && errno != EAGAIN))
313 {
Victor Stinner50e6e992020-03-19 02:41:21 +0100314 /* _PyEval_AddPendingCall() isn't signal-safe, but we
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800315 still use it for this exceptional case. */
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200316 _PyEval_AddPendingCall(interp,
Victor Stinner09532fe2019-05-10 23:39:09 +0200317 report_wakeup_write_error,
318 (void *)(intptr_t)errno);
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800319 }
Victor Stinner11517102014-07-29 23:31:34 +0200320 }
321 }
Victor Stinnerc13ef662011-05-25 02:35:58 +0200322 }
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200323}
324
325static void
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000326signal_handler(int sig_num)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000327{
Antoine Pitrou39a65912010-11-05 19:47:27 +0000328 int save_errno = errno;
329
Jeroen Demeyerd237b3f2019-05-10 03:28:57 +0200330 trip_signal(sig_num);
Antoine Pitrou39a65912010-11-05 19:47:27 +0000331
Jean-Paul Calderone6f137ca2010-05-09 03:18:57 +0000332#ifndef HAVE_SIGACTION
Antoine Pitrou39a65912010-11-05 19:47:27 +0000333#ifdef SIGCHLD
334 /* To avoid infinite recursion, this signal remains
335 reset until explicit re-instated.
336 Don't clear the 'func' field as it is our pointer
337 to the Python handler... */
338 if (sig_num != SIGCHLD)
339#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000340 /* If the handler was not set up with sigaction, reinstall it. See
Nick Coghland6009512014-11-20 21:39:37 +1000341 * Python/pylifecycle.c for the implementation of PyOS_setsig which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000342 * makes this true. See also issue8354. */
343 PyOS_setsig(sig_num, signal_handler);
Jean-Paul Calderone6f137ca2010-05-09 03:18:57 +0000344#endif
Antoine Pitrou39a65912010-11-05 19:47:27 +0000345
346 /* Issue #10311: asynchronously executing signal handlers should not
347 mutate errno under the feet of unsuspecting C code. */
348 errno = save_errno;
Antoine Pitrou6dd381e2011-11-21 21:26:56 +0100349
350#ifdef MS_WINDOWS
351 if (sig_num == SIGINT)
352 SetEvent(sigint_event);
353#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000354}
Guido van Rossume4485b01994-09-07 14:32:49 +0000355
Guido van Rossum06d511d1995-03-10 15:13:48 +0000356
Guido van Rossum1171ee61997-08-22 20:42:00 +0000357#ifdef HAVE_ALARM
Tal Einatc7027b72015-05-16 14:14:49 +0300358
359/*[clinic input]
360signal.alarm -> long
361
362 seconds: int
363 /
364
365Arrange for SIGALRM to arrive after the given number of seconds.
366[clinic start generated code]*/
367
368static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300369signal_alarm_impl(PyObject *module, int seconds)
370/*[clinic end generated code: output=144232290814c298 input=0d5e97e0e6f39e86]*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000371{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000372 /* alarm() returns the number of seconds remaining */
Tal Einatc7027b72015-05-16 14:14:49 +0300373 return (long)alarm(seconds);
Guido van Rossumaa0f4c71994-08-23 13:49:37 +0000374}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000375
Guido van Rossum06d511d1995-03-10 15:13:48 +0000376#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000377
Guido van Rossum1171ee61997-08-22 20:42:00 +0000378#ifdef HAVE_PAUSE
Tal Einatc7027b72015-05-16 14:14:49 +0300379
380/*[clinic input]
381signal.pause
382
383Wait until a signal arrives.
384[clinic start generated code]*/
385
Guido van Rossuma597dde1995-01-10 20:56:29 +0000386static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300387signal_pause_impl(PyObject *module)
388/*[clinic end generated code: output=391656788b3c3929 input=f03de0f875752062]*/
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000389{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000390 Py_BEGIN_ALLOW_THREADS
391 (void)pause();
392 Py_END_ALLOW_THREADS
393 /* make sure that any exceptions that got raised are propagated
394 * back into Python
395 */
396 if (PyErr_CheckSignals())
397 return NULL;
Barry Warsaw92971171997-01-03 00:14:25 +0000398
Tal Einatc7027b72015-05-16 14:14:49 +0300399 Py_RETURN_NONE;
Guido van Rossume4485b01994-09-07 14:32:49 +0000400}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000401
Guido van Rossum06d511d1995-03-10 15:13:48 +0000402#endif
Guido van Rossume4485b01994-09-07 14:32:49 +0000403
Vladimir Matveevc24c6c22019-01-08 01:58:25 -0800404/*[clinic input]
405signal.raise_signal
406
407 signalnum: int
408 /
409
410Send a signal to the executing process.
411[clinic start generated code]*/
412
413static PyObject *
414signal_raise_signal_impl(PyObject *module, int signalnum)
415/*[clinic end generated code: output=e2b014220aa6111d input=e90c0f9a42358de6]*/
416{
417 int err;
418 Py_BEGIN_ALLOW_THREADS
419 _Py_BEGIN_SUPPRESS_IPH
420 err = raise(signalnum);
421 _Py_END_SUPPRESS_IPH
422 Py_END_ALLOW_THREADS
Victor Stinner09532fe2019-05-10 23:39:09 +0200423
Vladimir Matveevc24c6c22019-01-08 01:58:25 -0800424 if (err) {
425 return PyErr_SetFromErrno(PyExc_OSError);
426 }
427 Py_RETURN_NONE;
428}
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000429
Tal Einatc7027b72015-05-16 14:14:49 +0300430/*[clinic input]
431signal.signal
432
433 signalnum: int
434 handler: object
435 /
436
437Set the action for the given signal.
438
439The action can be SIG_DFL, SIG_IGN, or a callable Python object.
440The previous action is returned. See getsignal() for possible return values.
441
442*** IMPORTANT NOTICE ***
443A signal handler function is called with two arguments:
444the first is the signal number, the second is the interrupted stack frame.
445[clinic start generated code]*/
446
Guido van Rossume4485b01994-09-07 14:32:49 +0000447static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300448signal_signal_impl(PyObject *module, int signalnum, PyObject *handler)
449/*[clinic end generated code: output=b44cfda43780f3a1 input=deee84af5fa0432c]*/
Guido van Rossume4485b01994-09-07 14:32:49 +0000450{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000451 PyObject *old_handler;
452 void (*func)(int);
Brian Curtinef9efbd2010-08-06 19:27:32 +0000453#ifdef MS_WINDOWS
Tal Einatc7027b72015-05-16 14:14:49 +0300454 /* Validate that signalnum is one of the allowable signals */
455 switch (signalnum) {
Brian Curtinc734b312010-09-06 16:04:10 +0000456 case SIGABRT: break;
Brian Curtin9e88b5a2010-10-01 14:49:24 +0000457#ifdef SIGBREAK
458 /* Issue #10003: SIGBREAK is not documented as permitted, but works
459 and corresponds to CTRL_BREAK_EVENT. */
460 case SIGBREAK: break;
461#endif
Brian Curtinc734b312010-09-06 16:04:10 +0000462 case SIGFPE: break;
463 case SIGILL: break;
464 case SIGINT: break;
465 case SIGSEGV: break;
466 case SIGTERM: break;
467 default:
468 PyErr_SetString(PyExc_ValueError, "invalid signal value");
469 return NULL;
Brian Curtinef9efbd2010-08-06 19:27:32 +0000470 }
471#endif
Victor Stinnerd8613dc2019-05-24 13:43:55 +0200472
Victor Stinner72818982020-03-26 22:28:11 +0100473 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200474 if (!_Py_ThreadCanHandleSignals(tstate->interp)) {
Victor Stinner72818982020-03-26 22:28:11 +0100475 _PyErr_SetString(tstate, PyExc_ValueError,
476 "signal only works in main thread "
477 "of the main interpreter");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000478 return NULL;
479 }
Tal Einatc7027b72015-05-16 14:14:49 +0300480 if (signalnum < 1 || signalnum >= NSIG) {
Victor Stinner72818982020-03-26 22:28:11 +0100481 _PyErr_SetString(tstate, PyExc_ValueError,
482 "signal number out of range");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000483 return NULL;
484 }
Victor Stinner72818982020-03-26 22:28:11 +0100485 if (handler == IgnoreHandler) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000486 func = SIG_IGN;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000487 }
Victor Stinner72818982020-03-26 22:28:11 +0100488 else if (handler == DefaultHandler) {
489 func = SIG_DFL;
490 }
491 else if (!PyCallable_Check(handler)) {
492 _PyErr_SetString(tstate, PyExc_TypeError,
493 "signal handler must be signal.SIG_IGN, "
494 "signal.SIG_DFL, or a callable object");
495 return NULL;
496 }
497 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000498 func = signal_handler;
Victor Stinner72818982020-03-26 22:28:11 +0100499 }
500
Antoine Pitrouf6f90ff2017-11-03 19:58:46 +0100501 /* Check for pending signals before changing signal handler */
Victor Stinner72818982020-03-26 22:28:11 +0100502 if (_PyErr_CheckSignalsTstate(tstate)) {
Antoine Pitrouf6f90ff2017-11-03 19:58:46 +0100503 return NULL;
504 }
Tal Einatc7027b72015-05-16 14:14:49 +0300505 if (PyOS_setsig(signalnum, func) == SIG_ERR) {
Victor Stinner388196e2011-05-10 17:13:00 +0200506 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000507 return NULL;
508 }
Victor Stinner72818982020-03-26 22:28:11 +0100509
Tal Einatc7027b72015-05-16 14:14:49 +0300510 old_handler = Handlers[signalnum].func;
Tal Einatc7027b72015-05-16 14:14:49 +0300511 Py_INCREF(handler);
512 Handlers[signalnum].func = handler;
Victor Stinner72818982020-03-26 22:28:11 +0100513
514 if (old_handler != NULL) {
Antoine Pitrouc8c952c2013-05-04 23:16:59 +0200515 return old_handler;
Victor Stinner72818982020-03-26 22:28:11 +0100516 }
517 else {
Antoine Pitrouc8c952c2013-05-04 23:16:59 +0200518 Py_RETURN_NONE;
Victor Stinner72818982020-03-26 22:28:11 +0100519 }
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 Stinner72818982020-03-26 22:28:11 +0100701 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200702 if (!_Py_ThreadCanHandleSignals(tstate->interp)) {
Victor Stinner72818982020-03-26 22:28:11 +0100703 _PyErr_SetString(tstate, PyExc_ValueError,
704 "set_wakeup_fd only works in main thread "
705 "of the main interpreter");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000706 return NULL;
707 }
Victor Stinner0bffc942014-07-21 16:28:54 +0200708
Victor Stinner11517102014-07-29 23:31:34 +0200709#ifdef MS_WINDOWS
710 is_socket = 0;
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100711 if (sockfd != INVALID_FD) {
Victor Stinner11517102014-07-29 23:31:34 +0200712 /* Import the _socket module to call WSAStartup() */
713 mod = PyImport_ImportModuleNoBlock("_socket");
714 if (mod == NULL)
715 return NULL;
716 Py_DECREF(mod);
717
718 /* test the socket */
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100719 if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR,
Victor Stinner11517102014-07-29 23:31:34 +0200720 (char *)&res, &res_size) != 0) {
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100721 int fd, err;
722
723 err = WSAGetLastError();
Victor Stinner11517102014-07-29 23:31:34 +0200724 if (err != WSAENOTSOCK) {
725 PyErr_SetExcFromWindowsErr(PyExc_OSError, err);
726 return NULL;
727 }
728
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100729 fd = (int)sockfd;
Steve Dower940f33a2016-09-08 11:21:54 -0700730 if ((SOCKET_T)fd != sockfd) {
Victor Stinner72818982020-03-26 22:28:11 +0100731 _PyErr_SetString(tstate, PyExc_ValueError, "invalid fd");
Victor Stinner11517102014-07-29 23:31:34 +0200732 return NULL;
733 }
734
Victor Stinner72818982020-03-26 22:28:11 +0100735 if (_Py_fstat(fd, &status) != 0) {
Victor Stinner11517102014-07-29 23:31:34 +0200736 return NULL;
Victor Stinner72818982020-03-26 22:28:11 +0100737 }
Victor Stinner38227602014-08-27 12:59:44 +0200738
739 /* on Windows, a file cannot be set to non-blocking mode */
Victor Stinner11517102014-07-29 23:31:34 +0200740 }
Victor Stinner38227602014-08-27 12:59:44 +0200741 else {
Victor Stinner11517102014-07-29 23:31:34 +0200742 is_socket = 1;
Victor Stinner38227602014-08-27 12:59:44 +0200743
744 /* Windows does not provide a function to test if a socket
745 is in non-blocking mode */
746 }
Victor Stinner11517102014-07-29 23:31:34 +0200747 }
748
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100749 old_sockfd = wakeup.fd;
750 wakeup.fd = sockfd;
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800751 wakeup.warn_on_full_buffer = warn_on_full_buffer;
Victor Stinner11517102014-07-29 23:31:34 +0200752 wakeup.use_send = is_socket;
753
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100754 if (old_sockfd != INVALID_FD)
755 return PyLong_FromSocket_t(old_sockfd);
Victor Stinner11517102014-07-29 23:31:34 +0200756 else
757 return PyLong_FromLong(-1);
758#else
Victor Stinnerd18ccd12014-07-24 21:58:53 +0200759 if (fd != -1) {
Victor Stinner38227602014-08-27 12:59:44 +0200760 int blocking;
761
Victor Stinnere134a7f2015-03-30 10:09:31 +0200762 if (_Py_fstat(fd, &status) != 0)
Victor Stinner11517102014-07-29 23:31:34 +0200763 return NULL;
Victor Stinner38227602014-08-27 12:59:44 +0200764
765 blocking = _Py_get_blocking(fd);
766 if (blocking < 0)
767 return NULL;
768 if (blocking) {
Victor Stinner72818982020-03-26 22:28:11 +0100769 _PyErr_Format(tstate, PyExc_ValueError,
770 "the fd %i must be in non-blocking mode",
771 fd);
Victor Stinner38227602014-08-27 12:59:44 +0200772 return NULL;
773 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000774 }
Victor Stinner0bffc942014-07-21 16:28:54 +0200775
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800776 old_fd = wakeup.fd;
777 wakeup.fd = fd;
778 wakeup.warn_on_full_buffer = warn_on_full_buffer;
Victor Stinner0bffc942014-07-21 16:28:54 +0200779
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000780 return PyLong_FromLong(old_fd);
Victor Stinner11517102014-07-29 23:31:34 +0200781#endif
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000782}
783
784PyDoc_STRVAR(set_wakeup_fd_doc,
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800785"set_wakeup_fd(fd, *, warn_on_full_buffer=True) -> fd\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000786\n\
Victor Stinner11517102014-07-29 23:31:34 +0200787Sets the fd to be written to (with the signal number) when a signal\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000788comes in. A library can use this to wakeup select or poll.\n\
Victor Stinner11517102014-07-29 23:31:34 +0200789The previous fd or -1 is returned.\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000790\n\
791The fd must be non-blocking.");
792
793/* C API for the same, without all the error checking */
794int
795PySignal_SetWakeupFd(int fd)
796{
Victor Stinner11517102014-07-29 23:31:34 +0200797 int old_fd;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000798 if (fd < 0)
799 fd = -1;
Victor Stinner11517102014-07-29 23:31:34 +0200800
801#ifdef MS_WINDOWS
802 old_fd = Py_SAFE_DOWNCAST(wakeup.fd, SOCKET_T, int);
Victor Stinner11517102014-07-29 23:31:34 +0200803#else
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800804 old_fd = wakeup.fd;
Victor Stinner11517102014-07-29 23:31:34 +0200805#endif
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800806 wakeup.fd = fd;
807 wakeup.warn_on_full_buffer = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000808 return old_fd;
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000809}
810
811
Martin v. Löwis823725e2008-03-24 13:39:54 +0000812#ifdef HAVE_SETITIMER
Tal Einatc7027b72015-05-16 14:14:49 +0300813
814/*[clinic input]
815signal.setitimer
816
817 which: int
Victor Stinneref611c92017-10-13 13:49:43 -0700818 seconds: object
819 interval: object(c_default="NULL") = 0.0
Tal Einatc7027b72015-05-16 14:14:49 +0300820 /
821
822Sets given itimer (one of ITIMER_REAL, ITIMER_VIRTUAL or ITIMER_PROF).
823
824The timer will fire after value seconds and after that every interval seconds.
825The itimer can be cleared by setting seconds to zero.
826
827Returns old values as a tuple: (delay, interval).
828[clinic start generated code]*/
829
Martin v. Löwis823725e2008-03-24 13:39:54 +0000830static PyObject *
Victor Stinneref611c92017-10-13 13:49:43 -0700831signal_setitimer_impl(PyObject *module, int which, PyObject *seconds,
832 PyObject *interval)
833/*[clinic end generated code: output=65f9dcbddc35527b input=de43daf194e6f66f]*/
Martin v. Löwis823725e2008-03-24 13:39:54 +0000834{
Martin v. Löwis823725e2008-03-24 13:39:54 +0000835 struct itimerval new, old;
836
Victor Stinneref611c92017-10-13 13:49:43 -0700837 if (timeval_from_double(seconds, &new.it_value) < 0) {
838 return NULL;
839 }
840 if (timeval_from_double(interval, &new.it_interval) < 0) {
841 return NULL;
842 }
843
Martin v. Löwis823725e2008-03-24 13:39:54 +0000844 /* Let OS check "which" value */
845 if (setitimer(which, &new, &old) != 0) {
Tal Einatc7027b72015-05-16 14:14:49 +0300846 PyErr_SetFromErrno(ItimerError);
847 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000848 }
849
850 return itimer_retval(&old);
851}
852
Martin v. Löwis823725e2008-03-24 13:39:54 +0000853#endif
854
855
856#ifdef HAVE_GETITIMER
Tal Einatc7027b72015-05-16 14:14:49 +0300857
858/*[clinic input]
859signal.getitimer
860
861 which: int
862 /
863
864Returns current value of given itimer.
865[clinic start generated code]*/
866
Martin v. Löwis823725e2008-03-24 13:39:54 +0000867static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300868signal_getitimer_impl(PyObject *module, int which)
869/*[clinic end generated code: output=9e053175d517db40 input=f7d21d38f3490627]*/
Martin v. Löwis823725e2008-03-24 13:39:54 +0000870{
Martin v. Löwis823725e2008-03-24 13:39:54 +0000871 struct itimerval old;
872
Martin v. Löwis823725e2008-03-24 13:39:54 +0000873 if (getitimer(which, &old) != 0) {
Tal Einatc7027b72015-05-16 14:14:49 +0300874 PyErr_SetFromErrno(ItimerError);
875 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000876 }
877
878 return itimer_retval(&old);
879}
880
Martin v. Löwis823725e2008-03-24 13:39:54 +0000881#endif
882
Victor Stinnerb3e72192011-05-08 01:46:11 +0200883#if defined(PYPTHREAD_SIGMASK) || defined(HAVE_SIGPENDING)
Victor Stinner35b300c2011-05-04 13:20:35 +0200884static PyObject*
885sigset_to_set(sigset_t mask)
886{
887 PyObject *signum, *result;
888 int sig;
889
890 result = PySet_New(0);
891 if (result == NULL)
892 return NULL;
893
894 for (sig = 1; sig < NSIG; sig++) {
895 if (sigismember(&mask, sig) != 1)
896 continue;
897
898 /* Handle the case where it is a member by adding the signal to
899 the result list. Ignore the other cases because they mean the
900 signal isn't a member of the mask or the signal was invalid,
901 and an invalid signal must have been our fault in constructing
902 the loop boundaries. */
903 signum = PyLong_FromLong(sig);
904 if (signum == NULL) {
905 Py_DECREF(result);
906 return NULL;
907 }
908 if (PySet_Add(result, signum) == -1) {
909 Py_DECREF(signum);
910 Py_DECREF(result);
911 return NULL;
912 }
913 Py_DECREF(signum);
914 }
915 return result;
916}
Victor Stinnerb3e72192011-05-08 01:46:11 +0200917#endif
Victor Stinner35b300c2011-05-04 13:20:35 +0200918
Victor Stinnerb3e72192011-05-08 01:46:11 +0200919#ifdef PYPTHREAD_SIGMASK
Tal Einatc7027b72015-05-16 14:14:49 +0300920
921/*[clinic input]
922signal.pthread_sigmask
923
924 how: int
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300925 mask: sigset_t
Tal Einatc7027b72015-05-16 14:14:49 +0300926 /
927
928Fetch and/or change the signal mask of the calling thread.
929[clinic start generated code]*/
930
Victor Stinnera9293352011-04-30 15:21:58 +0200931static PyObject *
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300932signal_pthread_sigmask_impl(PyObject *module, int how, sigset_t mask)
933/*[clinic end generated code: output=0562c0fb192981a8 input=85bcebda442fa77f]*/
Victor Stinnera9293352011-04-30 15:21:58 +0200934{
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300935 sigset_t previous;
Victor Stinnera9293352011-04-30 15:21:58 +0200936 int err;
937
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300938 err = pthread_sigmask(how, &mask, &previous);
Victor Stinnera9293352011-04-30 15:21:58 +0200939 if (err != 0) {
940 errno = err;
Victor Stinnerb3e72192011-05-08 01:46:11 +0200941 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnera9293352011-04-30 15:21:58 +0200942 return NULL;
943 }
944
Victor Stinnerd0e516d2011-05-03 14:57:12 +0200945 /* if signals was unblocked, signal handlers have been called */
946 if (PyErr_CheckSignals())
947 return NULL;
948
Victor Stinner35b300c2011-05-04 13:20:35 +0200949 return sigset_to_set(previous);
Victor Stinnera9293352011-04-30 15:21:58 +0200950}
951
Victor Stinnera9293352011-04-30 15:21:58 +0200952#endif /* #ifdef PYPTHREAD_SIGMASK */
953
Martin v. Löwis823725e2008-03-24 13:39:54 +0000954
Victor Stinnerb3e72192011-05-08 01:46:11 +0200955#ifdef HAVE_SIGPENDING
Tal Einatc7027b72015-05-16 14:14:49 +0300956
957/*[clinic input]
958signal.sigpending
959
960Examine pending signals.
961
962Returns a set of signal numbers that are pending for delivery to
963the calling thread.
964[clinic start generated code]*/
965
Victor Stinnerb3e72192011-05-08 01:46:11 +0200966static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300967signal_sigpending_impl(PyObject *module)
968/*[clinic end generated code: output=53375ffe89325022 input=e0036c016f874e29]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +0200969{
970 int err;
971 sigset_t mask;
972 err = sigpending(&mask);
973 if (err)
974 return PyErr_SetFromErrno(PyExc_OSError);
975 return sigset_to_set(mask);
976}
977
Victor Stinnerb3e72192011-05-08 01:46:11 +0200978#endif /* #ifdef HAVE_SIGPENDING */
979
980
981#ifdef HAVE_SIGWAIT
Tal Einatc7027b72015-05-16 14:14:49 +0300982
983/*[clinic input]
984signal.sigwait
985
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300986 sigset: sigset_t
Tal Einatc7027b72015-05-16 14:14:49 +0300987 /
988
989Wait for a signal.
990
991Suspend execution of the calling thread until the delivery of one of the
992signals specified in the signal set sigset. The function accepts the signal
993and returns the signal number.
994[clinic start generated code]*/
995
Victor Stinnerb3e72192011-05-08 01:46:11 +0200996static PyObject *
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300997signal_sigwait_impl(PyObject *module, sigset_t sigset)
998/*[clinic end generated code: output=f43770699d682f96 input=a6fbd47b1086d119]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +0200999{
Victor Stinnerb3e72192011-05-08 01:46:11 +02001000 int err, signum;
1001
Victor Stinner10c30d62011-06-10 01:39:53 +02001002 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001003 err = sigwait(&sigset, &signum);
Victor Stinner10c30d62011-06-10 01:39:53 +02001004 Py_END_ALLOW_THREADS
Victor Stinnerb3e72192011-05-08 01:46:11 +02001005 if (err) {
1006 errno = err;
1007 return PyErr_SetFromErrno(PyExc_OSError);
1008 }
1009
1010 return PyLong_FromLong(signum);
1011}
1012
Tal Einatc7027b72015-05-16 14:14:49 +03001013#endif /* #ifdef HAVE_SIGWAIT */
1014
Victor Stinnerb3e72192011-05-08 01:46:11 +02001015
Antoine Pitrou9d3627e2018-05-04 13:00:50 +02001016#if defined(HAVE_SIGFILLSET) || defined(MS_WINDOWS)
1017
1018/*[clinic input]
1019signal.valid_signals
1020
1021Return a set of valid signal numbers on this platform.
1022
1023The signal numbers returned by this function can be safely passed to
1024functions like `pthread_sigmask`.
1025[clinic start generated code]*/
1026
1027static PyObject *
1028signal_valid_signals_impl(PyObject *module)
1029/*[clinic end generated code: output=1609cffbcfcf1314 input=86a3717ff25288f2]*/
1030{
1031#ifdef MS_WINDOWS
1032#ifdef SIGBREAK
1033 PyObject *tup = Py_BuildValue("(iiiiiii)", SIGABRT, SIGBREAK, SIGFPE,
1034 SIGILL, SIGINT, SIGSEGV, SIGTERM);
1035#else
1036 PyObject *tup = Py_BuildValue("(iiiiii)", SIGABRT, SIGFPE, SIGILL,
1037 SIGINT, SIGSEGV, SIGTERM);
1038#endif
1039 if (tup == NULL) {
1040 return NULL;
1041 }
1042 PyObject *set = PySet_New(tup);
1043 Py_DECREF(tup);
1044 return set;
1045#else
1046 sigset_t mask;
1047 if (sigemptyset(&mask) || sigfillset(&mask)) {
1048 return PyErr_SetFromErrno(PyExc_OSError);
1049 }
1050 return sigset_to_set(mask);
1051#endif
1052}
1053
1054#endif /* #if defined(HAVE_SIGFILLSET) || defined(MS_WINDOWS) */
1055
1056
Ross Lagerwallbc808222011-06-25 12:13:40 +02001057#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
1058static int initialized;
1059static PyStructSequence_Field struct_siginfo_fields[] = {
1060 {"si_signo", "signal number"},
1061 {"si_code", "signal code"},
1062 {"si_errno", "errno associated with this signal"},
1063 {"si_pid", "sending process ID"},
1064 {"si_uid", "real user ID of sending process"},
1065 {"si_status", "exit value or signal"},
1066 {"si_band", "band event for SIGPOLL"},
1067 {0}
1068};
1069
1070PyDoc_STRVAR(struct_siginfo__doc__,
1071"struct_siginfo: Result from sigwaitinfo or sigtimedwait.\n\n\
1072This object may be accessed either as a tuple of\n\
1073(si_signo, si_code, si_errno, si_pid, si_uid, si_status, si_band),\n\
1074or via the attributes si_signo, si_code, and so on.");
1075
1076static PyStructSequence_Desc struct_siginfo_desc = {
1077 "signal.struct_siginfo", /* name */
1078 struct_siginfo__doc__, /* doc */
1079 struct_siginfo_fields, /* fields */
1080 7 /* n_in_sequence */
1081};
1082
1083static PyTypeObject SiginfoType;
1084
1085static PyObject *
1086fill_siginfo(siginfo_t *si)
1087{
1088 PyObject *result = PyStructSequence_New(&SiginfoType);
1089 if (!result)
1090 return NULL;
1091
1092 PyStructSequence_SET_ITEM(result, 0, PyLong_FromLong((long)(si->si_signo)));
1093 PyStructSequence_SET_ITEM(result, 1, PyLong_FromLong((long)(si->si_code)));
Victor Stinner09532fe2019-05-10 23:39:09 +02001094#ifdef __VXWORKS__
pxinwr8b5bdda2019-03-14 01:18:25 +08001095 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong(0L));
1096 PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong(0L));
1097 PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong(0L));
1098 PyStructSequence_SET_ITEM(result, 5, PyLong_FromLong(0L));
Victor Stinner09532fe2019-05-10 23:39:09 +02001099#else
Ross Lagerwallbc808222011-06-25 12:13:40 +02001100 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si->si_errno)));
1101 PyStructSequence_SET_ITEM(result, 3, PyLong_FromPid(si->si_pid));
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02001102 PyStructSequence_SET_ITEM(result, 4, _PyLong_FromUid(si->si_uid));
Ross Lagerwallbc808222011-06-25 12:13:40 +02001103 PyStructSequence_SET_ITEM(result, 5,
1104 PyLong_FromLong((long)(si->si_status)));
Victor Stinner09532fe2019-05-10 23:39:09 +02001105#endif
Zachary Ware6a6967e2016-10-01 00:47:27 -05001106#ifdef HAVE_SIGINFO_T_SI_BAND
Ross Lagerwallbc808222011-06-25 12:13:40 +02001107 PyStructSequence_SET_ITEM(result, 6, PyLong_FromLong(si->si_band));
Zachary Ware6a6967e2016-10-01 00:47:27 -05001108#else
1109 PyStructSequence_SET_ITEM(result, 6, PyLong_FromLong(0L));
1110#endif
Ross Lagerwallbc808222011-06-25 12:13:40 +02001111 if (PyErr_Occurred()) {
1112 Py_DECREF(result);
1113 return NULL;
1114 }
1115
1116 return result;
1117}
1118#endif
1119
1120#ifdef HAVE_SIGWAITINFO
Tal Einatc7027b72015-05-16 14:14:49 +03001121
1122/*[clinic input]
1123signal.sigwaitinfo
1124
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001125 sigset: sigset_t
Tal Einatc7027b72015-05-16 14:14:49 +03001126 /
1127
1128Wait synchronously until one of the signals in *sigset* is delivered.
1129
1130Returns a struct_siginfo containing information about the signal.
1131[clinic start generated code]*/
1132
Ross Lagerwallbc808222011-06-25 12:13:40 +02001133static PyObject *
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001134signal_sigwaitinfo_impl(PyObject *module, sigset_t sigset)
1135/*[clinic end generated code: output=1eb2f1fa236fdbca input=3d1a7e1f27fc664c]*/
Ross Lagerwallbc808222011-06-25 12:13:40 +02001136{
Ross Lagerwallbc808222011-06-25 12:13:40 +02001137 siginfo_t si;
1138 int err;
Victor Stinnera453cd82015-03-20 12:54:28 +01001139 int async_err = 0;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001140
Victor Stinnera453cd82015-03-20 12:54:28 +01001141 do {
1142 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001143 err = sigwaitinfo(&sigset, &si);
Victor Stinnera453cd82015-03-20 12:54:28 +01001144 Py_END_ALLOW_THREADS
1145 } while (err == -1
1146 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Ross Lagerwallbc808222011-06-25 12:13:40 +02001147 if (err == -1)
Victor Stinnera453cd82015-03-20 12:54:28 +01001148 return (!async_err) ? PyErr_SetFromErrno(PyExc_OSError) : NULL;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001149
1150 return fill_siginfo(&si);
1151}
1152
Ross Lagerwallbc808222011-06-25 12:13:40 +02001153#endif /* #ifdef HAVE_SIGWAITINFO */
1154
1155#ifdef HAVE_SIGTIMEDWAIT
Tal Einatc7027b72015-05-16 14:14:49 +03001156
1157/*[clinic input]
1158signal.sigtimedwait
1159
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001160 sigset: sigset_t
Serhiy Storchaka6b680cd2015-05-16 15:57:56 +03001161 timeout as timeout_obj: object
Tal Einatc7027b72015-05-16 14:14:49 +03001162 /
1163
1164Like sigwaitinfo(), but with a timeout.
1165
1166The timeout is specified in seconds, with floating point numbers allowed.
1167[clinic start generated code]*/
1168
Ross Lagerwallbc808222011-06-25 12:13:40 +02001169static PyObject *
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001170signal_sigtimedwait_impl(PyObject *module, sigset_t sigset,
Serhiy Storchaka6b680cd2015-05-16 15:57:56 +03001171 PyObject *timeout_obj)
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001172/*[clinic end generated code: output=59c8971e8ae18a64 input=87fd39237cf0b7ba]*/
Ross Lagerwallbc808222011-06-25 12:13:40 +02001173{
Victor Stinnera453cd82015-03-20 12:54:28 +01001174 struct timespec ts;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001175 siginfo_t si;
Victor Stinnera453cd82015-03-20 12:54:28 +01001176 int res;
Victor Stinner34dc0f42015-03-27 18:19:03 +01001177 _PyTime_t timeout, deadline, monotonic;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001178
Victor Stinner869e1772015-03-30 03:49:14 +02001179 if (_PyTime_FromSecondsObject(&timeout,
1180 timeout_obj, _PyTime_ROUND_CEILING) < 0)
Ross Lagerwallbc808222011-06-25 12:13:40 +02001181 return NULL;
1182
Victor Stinnera453cd82015-03-20 12:54:28 +01001183 if (timeout < 0) {
Ross Lagerwallbc808222011-06-25 12:13:40 +02001184 PyErr_SetString(PyExc_ValueError, "timeout must be non-negative");
1185 return NULL;
1186 }
1187
Victor Stinner34dc0f42015-03-27 18:19:03 +01001188 deadline = _PyTime_GetMonotonicClock() + timeout;
Victor Stinnera453cd82015-03-20 12:54:28 +01001189
1190 do {
Victor Stinner34dc0f42015-03-27 18:19:03 +01001191 if (_PyTime_AsTimespec(timeout, &ts) < 0)
1192 return NULL;
Victor Stinnera453cd82015-03-20 12:54:28 +01001193
1194 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001195 res = sigtimedwait(&sigset, &si, &ts);
Victor Stinnera453cd82015-03-20 12:54:28 +01001196 Py_END_ALLOW_THREADS
1197
1198 if (res != -1)
1199 break;
1200
1201 if (errno != EINTR) {
1202 if (errno == EAGAIN)
1203 Py_RETURN_NONE;
1204 else
1205 return PyErr_SetFromErrno(PyExc_OSError);
1206 }
1207
1208 /* sigtimedwait() was interrupted by a signal (EINTR) */
1209 if (PyErr_CheckSignals())
1210 return NULL;
1211
Victor Stinner34dc0f42015-03-27 18:19:03 +01001212 monotonic = _PyTime_GetMonotonicClock();
1213 timeout = deadline - monotonic;
Victor Stinner6aa446c2015-03-30 21:33:51 +02001214 if (timeout < 0)
Victor Stinnera453cd82015-03-20 12:54:28 +01001215 break;
1216 } while (1);
Ross Lagerwallbc808222011-06-25 12:13:40 +02001217
1218 return fill_siginfo(&si);
1219}
1220
Ross Lagerwallbc808222011-06-25 12:13:40 +02001221#endif /* #ifdef HAVE_SIGTIMEDWAIT */
1222
Victor Stinnerb3e72192011-05-08 01:46:11 +02001223
Antoine Pitroua6a4dc82017-09-07 18:56:24 +02001224#if defined(HAVE_PTHREAD_KILL)
Tal Einatc7027b72015-05-16 14:14:49 +03001225
1226/*[clinic input]
1227signal.pthread_kill
1228
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +02001229 thread_id: unsigned_long(bitwise=True)
Tal Einatc7027b72015-05-16 14:14:49 +03001230 signalnum: int
1231 /
1232
1233Send a signal to a thread.
1234[clinic start generated code]*/
1235
Victor Stinnerb3e72192011-05-08 01:46:11 +02001236static PyObject *
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +02001237signal_pthread_kill_impl(PyObject *module, unsigned long thread_id,
1238 int signalnum)
1239/*[clinic end generated code: output=7629919b791bc27f input=1d901f2c7bb544ff]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +02001240{
Victor Stinnerb3e72192011-05-08 01:46:11 +02001241 int err;
1242
Saiyang Gou7514f4f2020-02-12 23:47:42 -08001243 if (PySys_Audit("signal.pthread_kill", "ki", thread_id, signalnum) < 0) {
1244 return NULL;
1245 }
1246
Tal Einatc7027b72015-05-16 14:14:49 +03001247 err = pthread_kill((pthread_t)thread_id, signalnum);
Victor Stinnerb3e72192011-05-08 01:46:11 +02001248 if (err != 0) {
1249 errno = err;
1250 PyErr_SetFromErrno(PyExc_OSError);
1251 return NULL;
1252 }
1253
1254 /* the signal may have been send to the current thread */
1255 if (PyErr_CheckSignals())
1256 return NULL;
1257
1258 Py_RETURN_NONE;
1259}
1260
Antoine Pitroua6a4dc82017-09-07 18:56:24 +02001261#endif /* #if defined(HAVE_PTHREAD_KILL) */
Victor Stinnerb3e72192011-05-08 01:46:11 +02001262
1263
Benjamin Peterson74834512019-11-19 20:39:14 -08001264#if defined(__linux__) && defined(__NR_pidfd_send_signal)
1265/*[clinic input]
1266signal.pidfd_send_signal
1267
1268 pidfd: int
1269 signalnum: int
1270 siginfo: object = None
1271 flags: int = 0
1272 /
1273
1274Send a signal to a process referred to by a pid file descriptor.
1275[clinic start generated code]*/
1276
1277static PyObject *
1278signal_pidfd_send_signal_impl(PyObject *module, int pidfd, int signalnum,
1279 PyObject *siginfo, int flags)
1280/*[clinic end generated code: output=2d59f04a75d9cbdf input=2a6543a1f4ac2000]*/
1281
1282{
1283 if (siginfo != Py_None) {
1284 PyErr_SetString(PyExc_TypeError, "siginfo must be None");
1285 return NULL;
1286 }
1287 if (syscall(__NR_pidfd_send_signal, pidfd, signalnum, NULL, flags) < 0) {
1288 PyErr_SetFromErrno(PyExc_OSError);
1289 return NULL;
1290 }
1291 Py_RETURN_NONE;
1292}
1293#endif
1294
1295
Victor Stinnerb3e72192011-05-08 01:46:11 +02001296
Tal Einatc7027b72015-05-16 14:14:49 +03001297/* List of functions defined in the module -- some of the methoddefs are
1298 defined to nothing if the corresponding C function is not available. */
Barry Warsaw92971171997-01-03 00:14:25 +00001299static PyMethodDef signal_methods[] = {
Tal Einatc7027b72015-05-16 14:14:49 +03001300 {"default_int_handler", signal_default_int_handler, METH_VARARGS, default_int_handler_doc},
1301 SIGNAL_ALARM_METHODDEF
1302 SIGNAL_SETITIMER_METHODDEF
1303 SIGNAL_GETITIMER_METHODDEF
1304 SIGNAL_SIGNAL_METHODDEF
Vladimir Matveevc24c6c22019-01-08 01:58:25 -08001305 SIGNAL_RAISE_SIGNAL_METHODDEF
Antoine Pietri5d2a27d2018-03-12 14:42:34 +01001306 SIGNAL_STRSIGNAL_METHODDEF
Tal Einatc7027b72015-05-16 14:14:49 +03001307 SIGNAL_GETSIGNAL_METHODDEF
Serhiy Storchaka62be7422018-11-27 13:27:31 +02001308 {"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 +03001309 SIGNAL_SIGINTERRUPT_METHODDEF
1310 SIGNAL_PAUSE_METHODDEF
Benjamin Peterson74834512019-11-19 20:39:14 -08001311 SIGNAL_PIDFD_SEND_SIGNAL_METHODDEF
Tal Einatc7027b72015-05-16 14:14:49 +03001312 SIGNAL_PTHREAD_KILL_METHODDEF
1313 SIGNAL_PTHREAD_SIGMASK_METHODDEF
1314 SIGNAL_SIGPENDING_METHODDEF
1315 SIGNAL_SIGWAIT_METHODDEF
1316 SIGNAL_SIGWAITINFO_METHODDEF
1317 SIGNAL_SIGTIMEDWAIT_METHODDEF
Antoine Pitrou9d3627e2018-05-04 13:00:50 +02001318#if defined(HAVE_SIGFILLSET) || defined(MS_WINDOWS)
1319 SIGNAL_VALID_SIGNALS_METHODDEF
1320#endif
Tal Einatc7027b72015-05-16 14:14:49 +03001321 {NULL, NULL} /* sentinel */
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001322};
1323
Barry Warsaw92971171997-01-03 00:14:25 +00001324
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001325PyDoc_STRVAR(module_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001326"This module provides mechanisms to use signal handlers in Python.\n\
1327\n\
1328Functions:\n\
1329\n\
1330alarm() -- cause SIGALRM after a specified time [Unix only]\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001331setitimer() -- cause a signal (described below) after a specified\n\
1332 float time and the timer may restart then [Unix only]\n\
1333getitimer() -- get current value of timer [Unix only]\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001334signal() -- set the action for a given signal\n\
1335getsignal() -- get the signal action for a given signal\n\
1336pause() -- wait until a signal arrives [Unix only]\n\
1337default_int_handler() -- default SIGINT handler\n\
1338\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001339signal constants:\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001340SIG_DFL -- used to refer to the system default handler\n\
1341SIG_IGN -- used to ignore the signal\n\
1342NSIG -- number of defined signals\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001343SIGINT, SIGTERM, etc. -- signal numbers\n\
1344\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001345itimer constants:\n\
1346ITIMER_REAL -- decrements in real time, and delivers SIGALRM upon\n\
1347 expiration\n\
1348ITIMER_VIRTUAL -- decrements only when the process is executing,\n\
1349 and delivers SIGVTALRM upon expiration\n\
1350ITIMER_PROF -- decrements both when the process is executing and\n\
1351 when the system is executing on behalf of the process.\n\
1352 Coupled with ITIMER_VIRTUAL, this timer is usually\n\
1353 used to profile the time spent by the application\n\
1354 in user and kernel space. SIGPROF is delivered upon\n\
1355 expiration.\n\
1356\n\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001357*** IMPORTANT NOTICE ***\n\
1358A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001359the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001360
Martin v. Löwis1a214512008-06-11 05:26:20 +00001361static struct PyModuleDef signalmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001362 PyModuleDef_HEAD_INIT,
Brett Cannon815a6f32014-04-04 10:20:28 -04001363 "_signal",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001364 module_doc,
1365 -1,
1366 signal_methods,
1367 NULL,
1368 NULL,
1369 NULL,
1370 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001371};
1372
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001373PyMODINIT_FUNC
Giampaolo Rodola'e09fb712014-04-04 15:34:17 +02001374PyInit__signal(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001375{
animalize77643c42019-09-09 21:46:26 +08001376 PyObject *m, *d;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001377 int i;
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001378
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001379 /* Create the module and add the functions */
1380 m = PyModule_Create(&signalmodule);
1381 if (m == NULL)
1382 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001383
Ross Lagerwallbc808222011-06-25 12:13:40 +02001384#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
Victor Stinner1c8f0592013-07-22 22:24:54 +02001385 if (!initialized) {
1386 if (PyStructSequence_InitType2(&SiginfoType, &struct_siginfo_desc) < 0)
1387 return NULL;
1388 }
Ross Lagerwallbc808222011-06-25 12:13:40 +02001389 Py_INCREF((PyObject*) &SiginfoType);
1390 PyModule_AddObject(m, "struct_siginfo", (PyObject*) &SiginfoType);
1391 initialized = 1;
1392#endif
1393
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001394 /* Add some symbolic constants to the module */
1395 d = PyModule_GetDict(m);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001396
animalize77643c42019-09-09 21:46:26 +08001397 DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL);
1398 if (!DefaultHandler ||
1399 PyDict_SetItemString(d, "SIG_DFL", DefaultHandler) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001400 goto finally;
animalize77643c42019-09-09 21:46:26 +08001401 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001402
animalize77643c42019-09-09 21:46:26 +08001403 IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN);
1404 if (!IgnoreHandler ||
1405 PyDict_SetItemString(d, "SIG_IGN", IgnoreHandler) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001406 goto finally;
animalize77643c42019-09-09 21:46:26 +08001407 }
Barry Warsaw92971171997-01-03 00:14:25 +00001408
Joannah Nanjekye9541bd32019-04-21 21:47:06 -04001409 if (PyModule_AddIntMacro(m, NSIG))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001410 goto finally;
Barry Warsaw92971171997-01-03 00:14:25 +00001411
Victor Stinnera9293352011-04-30 15:21:58 +02001412#ifdef SIG_BLOCK
Victor Stinner72c53b52011-05-02 16:15:43 +02001413 if (PyModule_AddIntMacro(m, SIG_BLOCK))
1414 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001415#endif
Victor Stinnera9293352011-04-30 15:21:58 +02001416#ifdef SIG_UNBLOCK
Victor Stinner72c53b52011-05-02 16:15:43 +02001417 if (PyModule_AddIntMacro(m, SIG_UNBLOCK))
1418 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001419#endif
Victor Stinnera9293352011-04-30 15:21:58 +02001420#ifdef SIG_SETMASK
Victor Stinner72c53b52011-05-02 16:15:43 +02001421 if (PyModule_AddIntMacro(m, SIG_SETMASK))
1422 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001423#endif
1424
animalize77643c42019-09-09 21:46:26 +08001425 IntHandler = PyDict_GetItemString(d, "default_int_handler");
1426 if (!IntHandler)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001427 goto finally;
1428 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +00001429
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001430 _Py_atomic_store_relaxed(&Handlers[0].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001431 for (i = 1; i < NSIG; i++) {
1432 void (*t)(int);
1433 t = PyOS_getsig(i);
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001434 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001435 if (t == SIG_DFL)
1436 Handlers[i].func = DefaultHandler;
1437 else if (t == SIG_IGN)
1438 Handlers[i].func = IgnoreHandler;
1439 else
1440 Handlers[i].func = Py_None; /* None of our business */
1441 Py_INCREF(Handlers[i].func);
1442 }
1443 if (Handlers[SIGINT].func == DefaultHandler) {
1444 /* Install default int handler */
1445 Py_INCREF(IntHandler);
Serhiy Storchaka57a01d32016-04-10 18:05:40 +03001446 Py_SETREF(Handlers[SIGINT].func, IntHandler);
pkerlinge905c842018-06-01 09:47:18 +00001447 PyOS_setsig(SIGINT, signal_handler);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001448 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001449
1450#ifdef SIGHUP
Christian Heimes6782b142016-09-09 00:11:45 +02001451 if (PyModule_AddIntMacro(m, SIGHUP))
1452 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001453#endif
1454#ifdef SIGINT
Christian Heimes6782b142016-09-09 00:11:45 +02001455 if (PyModule_AddIntMacro(m, SIGINT))
1456 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001457#endif
Tim Peters1ce3cf72001-10-01 17:58:40 +00001458#ifdef SIGBREAK
Christian Heimes6782b142016-09-09 00:11:45 +02001459 if (PyModule_AddIntMacro(m, SIGBREAK))
1460 goto finally;
Tim Peters1ce3cf72001-10-01 17:58:40 +00001461#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001462#ifdef SIGQUIT
Christian Heimes6782b142016-09-09 00:11:45 +02001463 if (PyModule_AddIntMacro(m, SIGQUIT))
1464 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001465#endif
1466#ifdef SIGILL
Christian Heimes6782b142016-09-09 00:11:45 +02001467 if (PyModule_AddIntMacro(m, SIGILL))
1468 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001469#endif
1470#ifdef SIGTRAP
Christian Heimes6782b142016-09-09 00:11:45 +02001471 if (PyModule_AddIntMacro(m, SIGTRAP))
1472 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001473#endif
1474#ifdef SIGIOT
Christian Heimes6782b142016-09-09 00:11:45 +02001475 if (PyModule_AddIntMacro(m, SIGIOT))
1476 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001477#endif
1478#ifdef SIGABRT
Christian Heimes6782b142016-09-09 00:11:45 +02001479 if (PyModule_AddIntMacro(m, SIGABRT))
1480 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001481#endif
1482#ifdef SIGEMT
Christian Heimes6782b142016-09-09 00:11:45 +02001483 if (PyModule_AddIntMacro(m, SIGEMT))
1484 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001485#endif
1486#ifdef SIGFPE
Christian Heimes6782b142016-09-09 00:11:45 +02001487 if (PyModule_AddIntMacro(m, SIGFPE))
1488 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001489#endif
1490#ifdef SIGKILL
Christian Heimes6782b142016-09-09 00:11:45 +02001491 if (PyModule_AddIntMacro(m, SIGKILL))
1492 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001493#endif
1494#ifdef SIGBUS
Christian Heimes6782b142016-09-09 00:11:45 +02001495 if (PyModule_AddIntMacro(m, SIGBUS))
1496 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001497#endif
1498#ifdef SIGSEGV
Christian Heimes6782b142016-09-09 00:11:45 +02001499 if (PyModule_AddIntMacro(m, SIGSEGV))
1500 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001501#endif
1502#ifdef SIGSYS
Christian Heimes6782b142016-09-09 00:11:45 +02001503 if (PyModule_AddIntMacro(m, SIGSYS))
1504 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001505#endif
1506#ifdef SIGPIPE
Christian Heimes6782b142016-09-09 00:11:45 +02001507 if (PyModule_AddIntMacro(m, SIGPIPE))
1508 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001509#endif
1510#ifdef SIGALRM
Christian Heimes6782b142016-09-09 00:11:45 +02001511 if (PyModule_AddIntMacro(m, SIGALRM))
1512 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001513#endif
1514#ifdef SIGTERM
Christian Heimes6782b142016-09-09 00:11:45 +02001515 if (PyModule_AddIntMacro(m, SIGTERM))
1516 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001517#endif
1518#ifdef SIGUSR1
Christian Heimes6782b142016-09-09 00:11:45 +02001519 if (PyModule_AddIntMacro(m, SIGUSR1))
1520 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001521#endif
1522#ifdef SIGUSR2
Christian Heimes6782b142016-09-09 00:11:45 +02001523 if (PyModule_AddIntMacro(m, SIGUSR2))
1524 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001525#endif
1526#ifdef SIGCLD
Christian Heimes6782b142016-09-09 00:11:45 +02001527 if (PyModule_AddIntMacro(m, SIGCLD))
1528 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001529#endif
1530#ifdef SIGCHLD
Christian Heimes6782b142016-09-09 00:11:45 +02001531 if (PyModule_AddIntMacro(m, SIGCHLD))
1532 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001533#endif
1534#ifdef SIGPWR
Christian Heimes6782b142016-09-09 00:11:45 +02001535 if (PyModule_AddIntMacro(m, SIGPWR))
1536 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001537#endif
1538#ifdef SIGIO
Christian Heimes6782b142016-09-09 00:11:45 +02001539 if (PyModule_AddIntMacro(m, SIGIO))
1540 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001541#endif
1542#ifdef SIGURG
Christian Heimes6782b142016-09-09 00:11:45 +02001543 if (PyModule_AddIntMacro(m, SIGURG))
1544 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001545#endif
1546#ifdef SIGWINCH
Christian Heimes6782b142016-09-09 00:11:45 +02001547 if (PyModule_AddIntMacro(m, SIGWINCH))
1548 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001549#endif
1550#ifdef SIGPOLL
Christian Heimes6782b142016-09-09 00:11:45 +02001551 if (PyModule_AddIntMacro(m, SIGPOLL))
1552 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001553#endif
1554#ifdef SIGSTOP
Christian Heimes6782b142016-09-09 00:11:45 +02001555 if (PyModule_AddIntMacro(m, SIGSTOP))
1556 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001557#endif
1558#ifdef SIGTSTP
Christian Heimes6782b142016-09-09 00:11:45 +02001559 if (PyModule_AddIntMacro(m, SIGTSTP))
1560 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001561#endif
1562#ifdef SIGCONT
Christian Heimes6782b142016-09-09 00:11:45 +02001563 if (PyModule_AddIntMacro(m, SIGCONT))
1564 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001565#endif
1566#ifdef SIGTTIN
Christian Heimes6782b142016-09-09 00:11:45 +02001567 if (PyModule_AddIntMacro(m, SIGTTIN))
1568 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001569#endif
1570#ifdef SIGTTOU
Christian Heimes6782b142016-09-09 00:11:45 +02001571 if (PyModule_AddIntMacro(m, SIGTTOU))
1572 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001573#endif
1574#ifdef SIGVTALRM
Christian Heimes6782b142016-09-09 00:11:45 +02001575 if (PyModule_AddIntMacro(m, SIGVTALRM))
1576 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001577#endif
1578#ifdef SIGPROF
Christian Heimes6782b142016-09-09 00:11:45 +02001579 if (PyModule_AddIntMacro(m, SIGPROF))
1580 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001581#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001582#ifdef SIGXCPU
Christian Heimes6782b142016-09-09 00:11:45 +02001583 if (PyModule_AddIntMacro(m, SIGXCPU))
1584 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001585#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001586#ifdef SIGXFSZ
Christian Heimes6782b142016-09-09 00:11:45 +02001587 if (PyModule_AddIntMacro(m, SIGXFSZ))
1588 goto finally;
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001589#endif
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001590#ifdef SIGRTMIN
Christian Heimes6782b142016-09-09 00:11:45 +02001591 if (PyModule_AddIntMacro(m, SIGRTMIN))
1592 goto finally;
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001593#endif
1594#ifdef SIGRTMAX
Christian Heimes6782b142016-09-09 00:11:45 +02001595 if (PyModule_AddIntMacro(m, SIGRTMAX))
1596 goto finally;
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001597#endif
Martin v. Löwis175af252002-01-12 11:43:25 +00001598#ifdef SIGINFO
Christian Heimes6782b142016-09-09 00:11:45 +02001599 if (PyModule_AddIntMacro(m, SIGINFO))
1600 goto finally;
Martin v. Löwis175af252002-01-12 11:43:25 +00001601#endif
Martin v. Löwis823725e2008-03-24 13:39:54 +00001602
1603#ifdef ITIMER_REAL
Christian Heimes6782b142016-09-09 00:11:45 +02001604 if (PyModule_AddIntMacro(m, ITIMER_REAL))
1605 goto finally;
Martin v. Löwis823725e2008-03-24 13:39:54 +00001606#endif
1607#ifdef ITIMER_VIRTUAL
Christian Heimes6782b142016-09-09 00:11:45 +02001608 if (PyModule_AddIntMacro(m, ITIMER_VIRTUAL))
1609 goto finally;
Martin v. Löwis823725e2008-03-24 13:39:54 +00001610#endif
1611#ifdef ITIMER_PROF
Christian Heimes6782b142016-09-09 00:11:45 +02001612 if (PyModule_AddIntMacro(m, ITIMER_PROF))
1613 goto finally;
Martin v. Löwis823725e2008-03-24 13:39:54 +00001614#endif
1615
1616#if defined (HAVE_SETITIMER) || defined (HAVE_GETITIMER)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001617 ItimerError = PyErr_NewException("signal.ItimerError",
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +03001618 PyExc_OSError, NULL);
animalize77643c42019-09-09 21:46:26 +08001619 if (!ItimerError ||
1620 PyDict_SetItemString(d, "ItimerError", ItimerError) < 0) {
Joannah Nanjekye9541bd32019-04-21 21:47:06 -04001621 goto finally;
animalize77643c42019-09-09 21:46:26 +08001622 }
Martin v. Löwis823725e2008-03-24 13:39:54 +00001623#endif
1624
Brian Curtineb24d742010-04-12 17:16:38 +00001625#ifdef CTRL_C_EVENT
Christian Heimes6782b142016-09-09 00:11:45 +02001626 if (PyModule_AddIntMacro(m, CTRL_C_EVENT))
1627 goto finally;
Brian Curtineb24d742010-04-12 17:16:38 +00001628#endif
1629
1630#ifdef CTRL_BREAK_EVENT
Christian Heimes6782b142016-09-09 00:11:45 +02001631 if (PyModule_AddIntMacro(m, CTRL_BREAK_EVENT))
1632 goto finally;
Brian Curtineb24d742010-04-12 17:16:38 +00001633#endif
1634
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001635#ifdef MS_WINDOWS
1636 /* Create manual-reset event, initially unset */
1637 sigint_event = CreateEvent(NULL, TRUE, FALSE, FALSE);
1638#endif
1639
Martin v. Löwis1a214512008-06-11 05:26:20 +00001640 if (PyErr_Occurred()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001641 Py_DECREF(m);
1642 m = NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001643 }
Barry Warsaw92971171997-01-03 00:14:25 +00001644
Barry Warsaw92971171997-01-03 00:14:25 +00001645 finally:
Martin v. Löwis1a214512008-06-11 05:26:20 +00001646 return m;
Guido van Rossum08c16611997-08-02 03:01:42 +00001647}
1648
1649static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001650finisignal(void)
Guido van Rossum08c16611997-08-02 03:01:42 +00001651{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001652 int i;
1653 PyObject *func;
Guido van Rossum08c16611997-08-02 03:01:42 +00001654
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001655 for (i = 1; i < NSIG; i++) {
1656 func = Handlers[i].func;
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001657 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001658 Handlers[i].func = NULL;
pkerlinge905c842018-06-01 09:47:18 +00001659 if (func != NULL && func != Py_None &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001660 func != DefaultHandler && func != IgnoreHandler)
1661 PyOS_setsig(i, SIG_DFL);
1662 Py_XDECREF(func);
1663 }
Guido van Rossum08c16611997-08-02 03:01:42 +00001664
Serhiy Storchaka505ff752014-02-09 13:33:53 +02001665 Py_CLEAR(IntHandler);
1666 Py_CLEAR(DefaultHandler);
1667 Py_CLEAR(IgnoreHandler);
animalize77643c42019-09-09 21:46:26 +08001668#ifdef HAVE_GETITIMER
1669 Py_CLEAR(ItimerError);
1670#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001671}
1672
Barry Warsaw92971171997-01-03 00:14:25 +00001673
Barry Warsaw92971171997-01-03 00:14:25 +00001674/* Declared in pyerrors.h */
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001675int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001676PyErr_CheckSignals(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001677{
Victor Stinner72818982020-03-26 22:28:11 +01001678 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerb54a99d2020-04-08 23:35:05 +02001679 if (!_Py_ThreadCanHandleSignals(tstate->interp)) {
Eric Snow64d6cc82019-02-23 15:40:43 -07001680 return 0;
1681 }
1682
Victor Stinner72818982020-03-26 22:28:11 +01001683 return _PyErr_CheckSignalsTstate(tstate);
Eric Snow64d6cc82019-02-23 15:40:43 -07001684}
1685
1686
1687/* Declared in cpython/pyerrors.h */
1688int
Victor Stinner72818982020-03-26 22:28:11 +01001689_PyErr_CheckSignalsTstate(PyThreadState *tstate)
Eric Snow64d6cc82019-02-23 15:40:43 -07001690{
Victor Stinner72818982020-03-26 22:28:11 +01001691 if (!_Py_atomic_load(&is_tripped)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001692 return 0;
Victor Stinner72818982020-03-26 22:28:11 +01001693 }
Christian Heimesb76922a2007-12-11 01:06:40 +00001694
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001695 /*
1696 * The is_tripped variable is meant to speed up the calls to
1697 * PyErr_CheckSignals (both directly or via pending calls) when no
1698 * signal has arrived. This variable is set to 1 when a signal arrives
1699 * and it is set to 0 here, when we know some signals arrived. This way
1700 * we can run the registered handlers with no signals blocked.
1701 *
1702 * NOTE: with this approach we can have a situation where is_tripped is
1703 * 1 but we have no more signals to handle (Handlers[i].tripped
1704 * is 0 for every signal i). This won't do us any harm (except
1705 * we're gonna spent some cycles for nothing). This happens when
1706 * we receive a signal i after we zero is_tripped and before we
1707 * check Handlers[i].tripped.
1708 */
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001709 _Py_atomic_store(&is_tripped, 0);
Christian Heimesb76922a2007-12-11 01:06:40 +00001710
Victor Stinner72818982020-03-26 22:28:11 +01001711 PyObject *frame = (PyObject *)tstate->frame;
1712 if (!frame) {
1713 frame = Py_None;
1714 }
Christian Heimesb76922a2007-12-11 01:06:40 +00001715
Victor Stinner72818982020-03-26 22:28:11 +01001716 for (int i = 1; i < NSIG; i++) {
1717 if (!_Py_atomic_load_relaxed(&Handlers[i].tripped)) {
1718 continue;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001719 }
Victor Stinner72818982020-03-26 22:28:11 +01001720 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
1721
1722 PyObject *arglist = Py_BuildValue("(iO)", i, frame);
1723 PyObject *result;
1724 if (arglist) {
1725 result = _PyObject_Call(tstate, Handlers[i].func, arglist, NULL);
1726 Py_DECREF(arglist);
1727 }
1728 else {
1729 result = NULL;
1730 }
1731 if (!result) {
1732 /* On error, re-schedule a call to _PyErr_CheckSignalsTstate() */
1733 _Py_atomic_store(&is_tripped, 1);
1734 return -1;
1735 }
1736
1737 Py_DECREF(result);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001738 }
Christian Heimesb76922a2007-12-11 01:06:40 +00001739
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001740 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001741}
1742
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +00001743
Victor Stinner72818982020-03-26 22:28:11 +01001744
1745int
1746_PyErr_CheckSignals(void)
1747{
1748 PyThreadState *tstate = _PyThreadState_GET();
1749 return _PyErr_CheckSignalsTstate(tstate);
1750}
1751
1752
Matěj Cepl608876b2019-05-23 22:30:00 +02001753/* Simulate the effect of a signal.SIGINT signal arriving. The next time
1754 PyErr_CheckSignals is called, the Python SIGINT signal handler will be
1755 raised.
1756
1757 Missing signal handler for the SIGINT signal is silently ignored. */
Barry Warsaw92971171997-01-03 00:14:25 +00001758void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001759PyErr_SetInterrupt(void)
Barry Warsaw92971171997-01-03 00:14:25 +00001760{
Matěj Cepl608876b2019-05-23 22:30:00 +02001761 if ((Handlers[SIGINT].func != IgnoreHandler) &&
1762 (Handlers[SIGINT].func != DefaultHandler)) {
1763 trip_signal(SIGINT);
1764 }
Barry Warsaw92971171997-01-03 00:14:25 +00001765}
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001766
1767void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001768PyOS_InitInterrupts(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001769{
Giampaolo Rodola'e09fb712014-04-04 15:34:17 +02001770 PyObject *m = PyImport_ImportModule("_signal");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001771 if (m) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001772 Py_DECREF(m);
1773 }
Guido van Rossum08c16611997-08-02 03:01:42 +00001774}
1775
1776void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001777PyOS_FiniInterrupts(void)
Guido van Rossum08c16611997-08-02 03:01:42 +00001778{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001779 finisignal();
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001780}
1781
Victor Stinner5d2396c2020-06-03 17:49:25 +02001782
1783// The caller doesn't have to hold the GIL
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001784int
Victor Stinner5d2396c2020-06-03 17:49:25 +02001785_PyOS_InterruptOccurred(PyThreadState *tstate)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001786{
Victor Stinner6d62dc12020-06-03 20:16:39 +02001787 _Py_EnsureTstateNotNULL(tstate);
Victor Stinner5d2396c2020-06-03 17:49:25 +02001788 if (!_Py_ThreadCanHandleSignals(tstate->interp)) {
Victor Stinner72818982020-03-26 22:28:11 +01001789 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001790 }
Victor Stinner72818982020-03-26 22:28:11 +01001791
1792 if (!_Py_atomic_load_relaxed(&Handlers[SIGINT].tripped)) {
1793 return 0;
1794 }
1795
1796 _Py_atomic_store_relaxed(&Handlers[SIGINT].tripped, 0);
1797 return 1;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001798}
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001799
Victor Stinner5d2396c2020-06-03 17:49:25 +02001800
1801// The caller must to hold the GIL
1802int
1803PyOS_InterruptOccurred(void)
1804{
1805 PyThreadState *tstate = _PyThreadState_GET();
1806 return _PyOS_InterruptOccurred(tstate);
1807}
1808
1809
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001810static void
1811_clear_pending_signals(void)
1812{
1813 int i;
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001814 if (!_Py_atomic_load(&is_tripped))
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001815 return;
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001816 _Py_atomic_store(&is_tripped, 0);
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001817 for (i = 1; i < NSIG; ++i) {
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001818 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001819 }
1820}
1821
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001822void
Antoine Pitrou346cbd32017-05-27 17:50:54 +02001823_PySignal_AfterFork(void)
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001824{
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001825 /* Clear the signal flags after forking so that they aren't handled
1826 * in both processes if they came in just before the fork() but before
1827 * the interpreter had an opportunity to call the handlers. issue9535. */
1828 _clear_pending_signals();
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001829}
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001830
1831int
1832_PyOS_IsMainThread(void)
1833{
Victor Stinner81a7be32020-04-14 15:14:01 +02001834 PyInterpreterState *interp = _PyInterpreterState_GET();
Victor Stinnerb54a99d2020-04-08 23:35:05 +02001835 return _Py_ThreadCanHandleSignals(interp);
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001836}
1837
1838#ifdef MS_WINDOWS
1839void *_PyOS_SigintEvent(void)
1840{
1841 /* Returns a manual-reset event which gets tripped whenever
1842 SIGINT is received.
1843
1844 Python.h does not include windows.h so we do cannot use HANDLE
1845 as the return type of this function. We use void* instead. */
1846 return sigint_event;
1847}
1848#endif