blob: 7ac797a3aa3f872680dfe8e82a1b39aad6ea6714 [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 {
pxinwr1244c812020-12-01 05:48:33 +0800123#ifdef __VXWORKS__
124 int fd;
125#else
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800126 sig_atomic_t fd;
pxinwr1244c812020-12-01 05:48:33 +0800127#endif
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800128 int warn_on_full_buffer;
129} wakeup = {.fd = INVALID_FD, .warn_on_full_buffer = 1};
Victor Stinner11517102014-07-29 23:31:34 +0200130#endif
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000131
Christian Heimesb76922a2007-12-11 01:06:40 +0000132/* Speed up sigcheck() when none tripped */
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +0200133static _Py_atomic_int is_tripped;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000134
Barry Warsaw92971171997-01-03 00:14:25 +0000135static PyObject *DefaultHandler;
136static PyObject *IgnoreHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000137
Antoine Pitrou6dd381e2011-11-21 21:26:56 +0100138#ifdef MS_WINDOWS
139static HANDLE sigint_event = NULL;
140#endif
141
Victor Stinner0ae323b2020-11-17 18:15:20 +0100142#if defined(HAVE_GETITIMER) || defined(HAVE_SETITIMER)
Martin v. Löwis823725e2008-03-24 13:39:54 +0000143static PyObject *ItimerError;
Victor Stinner0ae323b2020-11-17 18:15:20 +0100144#endif
Martin v. Löwis823725e2008-03-24 13:39:54 +0000145
Victor Stinner0ae323b2020-11-17 18:15:20 +0100146#ifdef HAVE_GETITIMER
Victor Stinneref611c92017-10-13 13:49:43 -0700147/* auxiliary functions for setitimer */
148static int
149timeval_from_double(PyObject *obj, struct timeval *tv)
Martin v. Löwis823725e2008-03-24 13:39:54 +0000150{
Victor Stinneref611c92017-10-13 13:49:43 -0700151 if (obj == NULL) {
152 tv->tv_sec = 0;
153 tv->tv_usec = 0;
154 return 0;
Antoine Pitrou729780a2017-06-30 10:01:05 +0200155 }
Victor Stinneref611c92017-10-13 13:49:43 -0700156
157 _PyTime_t t;
158 if (_PyTime_FromSecondsObject(&t, obj, _PyTime_ROUND_CEILING) < 0) {
159 return -1;
160 }
161 return _PyTime_AsTimeval(t, tv, _PyTime_ROUND_CEILING);
Martin v. Löwis823725e2008-03-24 13:39:54 +0000162}
163
Christian Heimes1a8501c2008-10-02 19:56:01 +0000164Py_LOCAL_INLINE(double)
Martin v. Löwis823725e2008-03-24 13:39:54 +0000165double_from_timeval(struct timeval *tv)
166{
167 return tv->tv_sec + (double)(tv->tv_usec / 1000000.0);
168}
169
170static PyObject *
171itimer_retval(struct itimerval *iv)
172{
173 PyObject *r, *v;
174
175 r = PyTuple_New(2);
176 if (r == NULL)
Martin Panter6d57fe12016-09-17 03:26:16 +0000177 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000178
179 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_value)))) {
Martin Panter6d57fe12016-09-17 03:26:16 +0000180 Py_DECREF(r);
181 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000182 }
183
184 PyTuple_SET_ITEM(r, 0, v);
185
186 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_interval)))) {
Martin Panter6d57fe12016-09-17 03:26:16 +0000187 Py_DECREF(r);
188 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000189 }
190
191 PyTuple_SET_ITEM(r, 1, v);
192
193 return r;
194}
195#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000196
Serhiy Storchakab0689ae2020-07-12 19:15:20 +0300197/*[clinic input]
198signal.default_int_handler
199 signalnum: int
200 frame: object
201 /
202
203The default handler for SIGINT installed by Python.
204
205It raises KeyboardInterrupt.
206[clinic start generated code]*/
207
Guido van Rossume4485b01994-09-07 14:32:49 +0000208static PyObject *
Serhiy Storchakab0689ae2020-07-12 19:15:20 +0300209signal_default_int_handler_impl(PyObject *module, int signalnum,
210 PyObject *frame)
211/*[clinic end generated code: output=bb11c2eb115ace4e input=efcd4a56a207acfd]*/
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000212{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000213 PyErr_SetNone(PyExc_KeyboardInterrupt);
214 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000215}
216
Thomas Wouters0796b002000-07-22 23:49:30 +0000217
218static int
Victor Stinner11517102014-07-29 23:31:34 +0200219report_wakeup_write_error(void *data)
Antoine Pitrou6f6ec372013-08-17 20:27:56 +0200220{
Eric Snowfdf282d2019-01-11 14:26:55 -0700221 PyObject *exc, *val, *tb;
Antoine Pitrou6f6ec372013-08-17 20:27:56 +0200222 int save_errno = errno;
Benjamin Petersonca470632016-09-06 13:47:26 -0700223 errno = (int) (intptr_t) data;
Eric Snowfdf282d2019-01-11 14:26:55 -0700224 PyErr_Fetch(&exc, &val, &tb);
Antoine Pitrou6f6ec372013-08-17 20:27:56 +0200225 PyErr_SetFromErrno(PyExc_OSError);
226 PySys_WriteStderr("Exception ignored when trying to write to the "
227 "signal wakeup fd:\n");
228 PyErr_WriteUnraisable(NULL);
Eric Snowfdf282d2019-01-11 14:26:55 -0700229 PyErr_Restore(exc, val, tb);
Antoine Pitrou6f6ec372013-08-17 20:27:56 +0200230 errno = save_errno;
231 return 0;
232}
233
Victor Stinner11517102014-07-29 23:31:34 +0200234#ifdef MS_WINDOWS
235static int
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800236report_wakeup_send_error(void* data)
Victor Stinner11517102014-07-29 23:31:34 +0200237{
Eric Snowfdf282d2019-01-11 14:26:55 -0700238 PyObject *exc, *val, *tb;
239 PyErr_Fetch(&exc, &val, &tb);
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800240 /* PyErr_SetExcFromWindowsErr() invokes FormatMessage() which
241 recognizes the error codes used by both GetLastError() and
242 WSAGetLastError */
243 PyErr_SetExcFromWindowsErr(PyExc_OSError, (int) (intptr_t) data);
Victor Stinner11517102014-07-29 23:31:34 +0200244 PySys_WriteStderr("Exception ignored when trying to send to the "
245 "signal wakeup fd:\n");
246 PyErr_WriteUnraisable(NULL);
Eric Snowfdf282d2019-01-11 14:26:55 -0700247 PyErr_Restore(exc, val, tb);
Victor Stinner11517102014-07-29 23:31:34 +0200248 return 0;
249}
250#endif /* MS_WINDOWS */
251
Tim Peters4f1b2082000-07-23 21:18:09 +0000252static void
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200253trip_signal(int sig_num)
254{
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +0200255 _Py_atomic_store_relaxed(&Handlers[sig_num].tripped, 1);
Victor Stinner11517102014-07-29 23:31:34 +0200256
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200257 /* Set is_tripped after setting .tripped, as it gets
258 cleared in PyErr_CheckSignals() before .tripped. */
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +0200259 _Py_atomic_store(&is_tripped, 1);
260
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200261 /* Signals are always handled by the main interpreter */
262 PyInterpreterState *interp = _PyRuntime.interpreters.main;
Victor Stinner8849e592020-03-18 19:28:53 +0100263
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +0200264 /* Notify ceval.c */
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200265 _PyEval_SignalReceived(interp);
Nathaniel J. Smith4ae01492017-05-16 14:12:11 -0700266
267 /* And then write to the wakeup fd *after* setting all the globals and
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200268 doing the _PyEval_SignalReceived. We used to write to the wakeup fd
269 and then set the flag, but this allowed the following sequence of events
270 (especially on windows, where trip_signal may run in a new thread):
Nathaniel J. Smith4ae01492017-05-16 14:12:11 -0700271
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800272 - main thread blocks on select([wakeup.fd], ...)
Nathaniel J. Smith4ae01492017-05-16 14:12:11 -0700273 - signal arrives
274 - trip_signal writes to the wakeup fd
275 - the main thread wakes up
276 - the main thread checks the signal flags, sees that they're unset
277 - the main thread empties the wakeup fd
278 - the main thread goes back to sleep
279 - trip_signal sets the flags to request the Python-level signal handler
280 be run
281 - the main thread doesn't notice, because it's asleep
282
283 See bpo-30038 for more details.
284 */
285
Victor Stinnercda23be2020-11-17 18:57:32 +0100286 int fd;
Victor Stinner11517102014-07-29 23:31:34 +0200287#ifdef MS_WINDOWS
288 fd = Py_SAFE_DOWNCAST(wakeup.fd, SOCKET_T, int);
289#else
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800290 fd = wakeup.fd;
Victor Stinner11517102014-07-29 23:31:34 +0200291#endif
292
293 if (fd != INVALID_FD) {
Victor Stinnercda23be2020-11-17 18:57:32 +0100294 unsigned char byte = (unsigned char)sig_num;
Victor Stinner11517102014-07-29 23:31:34 +0200295#ifdef MS_WINDOWS
296 if (wakeup.use_send) {
Victor Stinnercda23be2020-11-17 18:57:32 +0100297 Py_ssize_t rc = send(fd, &byte, 1, 0);
Victor Stinner11517102014-07-29 23:31:34 +0200298
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800299 if (rc < 0) {
300 int last_error = GetLastError();
301 if (wakeup.warn_on_full_buffer ||
302 last_error != WSAEWOULDBLOCK)
303 {
Victor Stinner50e6e992020-03-19 02:41:21 +0100304 /* _PyEval_AddPendingCall() isn't signal-safe, but we
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800305 still use it for this exceptional case. */
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200306 _PyEval_AddPendingCall(interp,
Victor Stinner09532fe2019-05-10 23:39:09 +0200307 report_wakeup_send_error,
308 (void *)(intptr_t) last_error);
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800309 }
Victor Stinner11517102014-07-29 23:31:34 +0200310 }
311 }
312 else
313#endif
314 {
Victor Stinnere72fe392015-04-01 18:35:22 +0200315 /* _Py_write_noraise() retries write() if write() is interrupted by
316 a signal (fails with EINTR). */
Victor Stinnercda23be2020-11-17 18:57:32 +0100317 Py_ssize_t rc = _Py_write_noraise(fd, &byte, 1);
Victor Stinner11517102014-07-29 23:31:34 +0200318
319 if (rc < 0) {
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800320 if (wakeup.warn_on_full_buffer ||
321 (errno != EWOULDBLOCK && errno != EAGAIN))
322 {
Victor Stinner50e6e992020-03-19 02:41:21 +0100323 /* _PyEval_AddPendingCall() isn't signal-safe, but we
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800324 still use it for this exceptional case. */
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200325 _PyEval_AddPendingCall(interp,
Victor Stinner09532fe2019-05-10 23:39:09 +0200326 report_wakeup_write_error,
327 (void *)(intptr_t)errno);
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800328 }
Victor Stinner11517102014-07-29 23:31:34 +0200329 }
330 }
Victor Stinnerc13ef662011-05-25 02:35:58 +0200331 }
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200332}
333
334static void
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000335signal_handler(int sig_num)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000336{
Antoine Pitrou39a65912010-11-05 19:47:27 +0000337 int save_errno = errno;
338
Jeroen Demeyerd237b3f2019-05-10 03:28:57 +0200339 trip_signal(sig_num);
Antoine Pitrou39a65912010-11-05 19:47:27 +0000340
Jean-Paul Calderone6f137ca2010-05-09 03:18:57 +0000341#ifndef HAVE_SIGACTION
Antoine Pitrou39a65912010-11-05 19:47:27 +0000342#ifdef SIGCHLD
343 /* To avoid infinite recursion, this signal remains
344 reset until explicit re-instated.
345 Don't clear the 'func' field as it is our pointer
346 to the Python handler... */
347 if (sig_num != SIGCHLD)
348#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000349 /* If the handler was not set up with sigaction, reinstall it. See
Nick Coghland6009512014-11-20 21:39:37 +1000350 * Python/pylifecycle.c for the implementation of PyOS_setsig which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000351 * makes this true. See also issue8354. */
352 PyOS_setsig(sig_num, signal_handler);
Jean-Paul Calderone6f137ca2010-05-09 03:18:57 +0000353#endif
Antoine Pitrou39a65912010-11-05 19:47:27 +0000354
355 /* Issue #10311: asynchronously executing signal handlers should not
356 mutate errno under the feet of unsuspecting C code. */
357 errno = save_errno;
Antoine Pitrou6dd381e2011-11-21 21:26:56 +0100358
359#ifdef MS_WINDOWS
360 if (sig_num == SIGINT)
361 SetEvent(sigint_event);
362#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000363}
Guido van Rossume4485b01994-09-07 14:32:49 +0000364
Guido van Rossum06d511d1995-03-10 15:13:48 +0000365
Guido van Rossum1171ee61997-08-22 20:42:00 +0000366#ifdef HAVE_ALARM
Tal Einatc7027b72015-05-16 14:14:49 +0300367
368/*[clinic input]
369signal.alarm -> long
370
371 seconds: int
372 /
373
374Arrange for SIGALRM to arrive after the given number of seconds.
375[clinic start generated code]*/
376
377static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300378signal_alarm_impl(PyObject *module, int seconds)
379/*[clinic end generated code: output=144232290814c298 input=0d5e97e0e6f39e86]*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000380{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000381 /* alarm() returns the number of seconds remaining */
Tal Einatc7027b72015-05-16 14:14:49 +0300382 return (long)alarm(seconds);
Guido van Rossumaa0f4c71994-08-23 13:49:37 +0000383}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000384
Guido van Rossum06d511d1995-03-10 15:13:48 +0000385#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000386
Guido van Rossum1171ee61997-08-22 20:42:00 +0000387#ifdef HAVE_PAUSE
Tal Einatc7027b72015-05-16 14:14:49 +0300388
389/*[clinic input]
390signal.pause
391
392Wait until a signal arrives.
393[clinic start generated code]*/
394
Guido van Rossuma597dde1995-01-10 20:56:29 +0000395static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300396signal_pause_impl(PyObject *module)
397/*[clinic end generated code: output=391656788b3c3929 input=f03de0f875752062]*/
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000398{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000399 Py_BEGIN_ALLOW_THREADS
400 (void)pause();
401 Py_END_ALLOW_THREADS
402 /* make sure that any exceptions that got raised are propagated
403 * back into Python
404 */
405 if (PyErr_CheckSignals())
406 return NULL;
Barry Warsaw92971171997-01-03 00:14:25 +0000407
Tal Einatc7027b72015-05-16 14:14:49 +0300408 Py_RETURN_NONE;
Guido van Rossume4485b01994-09-07 14:32:49 +0000409}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000410
Guido van Rossum06d511d1995-03-10 15:13:48 +0000411#endif
Guido van Rossume4485b01994-09-07 14:32:49 +0000412
Vladimir Matveevc24c6c22019-01-08 01:58:25 -0800413/*[clinic input]
414signal.raise_signal
415
416 signalnum: int
417 /
418
419Send a signal to the executing process.
420[clinic start generated code]*/
421
422static PyObject *
423signal_raise_signal_impl(PyObject *module, int signalnum)
424/*[clinic end generated code: output=e2b014220aa6111d input=e90c0f9a42358de6]*/
425{
426 int err;
427 Py_BEGIN_ALLOW_THREADS
428 _Py_BEGIN_SUPPRESS_IPH
429 err = raise(signalnum);
430 _Py_END_SUPPRESS_IPH
431 Py_END_ALLOW_THREADS
Victor Stinner09532fe2019-05-10 23:39:09 +0200432
Vladimir Matveevc24c6c22019-01-08 01:58:25 -0800433 if (err) {
434 return PyErr_SetFromErrno(PyExc_OSError);
435 }
436 Py_RETURN_NONE;
437}
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000438
Tal Einatc7027b72015-05-16 14:14:49 +0300439/*[clinic input]
440signal.signal
441
442 signalnum: int
443 handler: object
444 /
445
446Set the action for the given signal.
447
448The action can be SIG_DFL, SIG_IGN, or a callable Python object.
449The previous action is returned. See getsignal() for possible return values.
450
451*** IMPORTANT NOTICE ***
452A signal handler function is called with two arguments:
453the first is the signal number, the second is the interrupted stack frame.
454[clinic start generated code]*/
455
Guido van Rossume4485b01994-09-07 14:32:49 +0000456static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300457signal_signal_impl(PyObject *module, int signalnum, PyObject *handler)
458/*[clinic end generated code: output=b44cfda43780f3a1 input=deee84af5fa0432c]*/
Guido van Rossume4485b01994-09-07 14:32:49 +0000459{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000460 PyObject *old_handler;
461 void (*func)(int);
Brian Curtinef9efbd2010-08-06 19:27:32 +0000462#ifdef MS_WINDOWS
Tal Einatc7027b72015-05-16 14:14:49 +0300463 /* Validate that signalnum is one of the allowable signals */
464 switch (signalnum) {
Brian Curtinc734b312010-09-06 16:04:10 +0000465 case SIGABRT: break;
Brian Curtin9e88b5a2010-10-01 14:49:24 +0000466#ifdef SIGBREAK
467 /* Issue #10003: SIGBREAK is not documented as permitted, but works
468 and corresponds to CTRL_BREAK_EVENT. */
469 case SIGBREAK: break;
470#endif
Brian Curtinc734b312010-09-06 16:04:10 +0000471 case SIGFPE: break;
472 case SIGILL: break;
473 case SIGINT: break;
474 case SIGSEGV: break;
475 case SIGTERM: break;
476 default:
477 PyErr_SetString(PyExc_ValueError, "invalid signal value");
478 return NULL;
Brian Curtinef9efbd2010-08-06 19:27:32 +0000479 }
480#endif
Victor Stinnerd8613dc2019-05-24 13:43:55 +0200481
Victor Stinner72818982020-03-26 22:28:11 +0100482 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200483 if (!_Py_ThreadCanHandleSignals(tstate->interp)) {
Victor Stinner72818982020-03-26 22:28:11 +0100484 _PyErr_SetString(tstate, PyExc_ValueError,
485 "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) {
Victor Stinner72818982020-03-26 22:28:11 +0100490 _PyErr_SetString(tstate, PyExc_ValueError,
491 "signal number out of range");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000492 return NULL;
493 }
Victor Stinner72818982020-03-26 22:28:11 +0100494 if (handler == IgnoreHandler) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000495 func = SIG_IGN;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000496 }
Victor Stinner72818982020-03-26 22:28:11 +0100497 else if (handler == DefaultHandler) {
498 func = SIG_DFL;
499 }
500 else if (!PyCallable_Check(handler)) {
501 _PyErr_SetString(tstate, PyExc_TypeError,
502 "signal handler must be signal.SIG_IGN, "
503 "signal.SIG_DFL, or a callable object");
504 return NULL;
505 }
506 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000507 func = signal_handler;
Victor Stinner72818982020-03-26 22:28:11 +0100508 }
509
Antoine Pitrouf6f90ff2017-11-03 19:58:46 +0100510 /* Check for pending signals before changing signal handler */
Victor Stinner72818982020-03-26 22:28:11 +0100511 if (_PyErr_CheckSignalsTstate(tstate)) {
Antoine Pitrouf6f90ff2017-11-03 19:58:46 +0100512 return NULL;
513 }
Tal Einatc7027b72015-05-16 14:14:49 +0300514 if (PyOS_setsig(signalnum, func) == SIG_ERR) {
Victor Stinner388196e2011-05-10 17:13:00 +0200515 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000516 return NULL;
517 }
Victor Stinner72818982020-03-26 22:28:11 +0100518
Tal Einatc7027b72015-05-16 14:14:49 +0300519 old_handler = Handlers[signalnum].func;
Victor Stinnercda23be2020-11-17 18:57:32 +0100520 Handlers[signalnum].func = Py_NewRef(handler);
Victor Stinner72818982020-03-26 22:28:11 +0100521
522 if (old_handler != NULL) {
Antoine Pitrouc8c952c2013-05-04 23:16:59 +0200523 return old_handler;
Victor Stinner72818982020-03-26 22:28:11 +0100524 }
525 else {
Antoine Pitrouc8c952c2013-05-04 23:16:59 +0200526 Py_RETURN_NONE;
Victor Stinner72818982020-03-26 22:28:11 +0100527 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000528}
529
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000530
Tal Einatc7027b72015-05-16 14:14:49 +0300531/*[clinic input]
532signal.getsignal
533
534 signalnum: int
535 /
536
537Return the current action for the given signal.
538
539The return value can be:
540 SIG_IGN -- if the signal is being ignored
541 SIG_DFL -- if the default action for the signal is in effect
542 None -- if an unknown handler is in effect
543 anything else -- the callable Python object used as a handler
544[clinic start generated code]*/
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000545
Guido van Rossume4485b01994-09-07 14:32:49 +0000546static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300547signal_getsignal_impl(PyObject *module, int signalnum)
548/*[clinic end generated code: output=35b3e0e796fd555e input=ac23a00f19dfa509]*/
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000549{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000550 PyObject *old_handler;
Tal Einatc7027b72015-05-16 14:14:49 +0300551 if (signalnum < 1 || signalnum >= NSIG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000552 PyErr_SetString(PyExc_ValueError,
553 "signal number out of range");
554 return NULL;
555 }
Tal Einatc7027b72015-05-16 14:14:49 +0300556 old_handler = Handlers[signalnum].func;
Antoine Pitrouc8c952c2013-05-04 23:16:59 +0200557 if (old_handler != NULL) {
Victor Stinnercda23be2020-11-17 18:57:32 +0100558 return Py_NewRef(old_handler);
Antoine Pitrouc8c952c2013-05-04 23:16:59 +0200559 }
560 else {
561 Py_RETURN_NONE;
562 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000563}
564
Antoine Pietri5d2a27d2018-03-12 14:42:34 +0100565
566/*[clinic input]
567signal.strsignal
568
569 signalnum: int
570 /
571
572Return the system description of the given signal.
573
574The return values can be such as "Interrupt", "Segmentation fault", etc.
575Returns None if the signal is not recognized.
576[clinic start generated code]*/
577
578static PyObject *
579signal_strsignal_impl(PyObject *module, int signalnum)
580/*[clinic end generated code: output=44e12e1e3b666261 input=b77914b03f856c74]*/
581{
582 char *res;
583
584 if (signalnum < 1 || signalnum >= NSIG) {
585 PyErr_SetString(PyExc_ValueError,
586 "signal number out of range");
587 return NULL;
588 }
589
Michael Osipov48ce4892018-08-23 15:27:19 +0200590#ifndef HAVE_STRSIGNAL
Antoine Pietri5d2a27d2018-03-12 14:42:34 +0100591 switch (signalnum) {
Michael Osipov48ce4892018-08-23 15:27:19 +0200592 /* Though being a UNIX, HP-UX does not provide strsignal(3). */
593#ifndef MS_WINDOWS
594 case SIGHUP:
595 res = "Hangup";
596 break;
597 case SIGALRM:
598 res = "Alarm clock";
599 break;
600 case SIGPIPE:
601 res = "Broken pipe";
602 break;
603 case SIGQUIT:
604 res = "Quit";
605 break;
606 case SIGCHLD:
607 res = "Child exited";
608 break;
609#endif
610 /* Custom redefinition of POSIX signals allowed on Windows. */
Antoine Pietri5d2a27d2018-03-12 14:42:34 +0100611 case SIGINT:
612 res = "Interrupt";
613 break;
614 case SIGILL:
615 res = "Illegal instruction";
616 break;
617 case SIGABRT:
618 res = "Aborted";
619 break;
620 case SIGFPE:
621 res = "Floating point exception";
622 break;
623 case SIGSEGV:
624 res = "Segmentation fault";
625 break;
626 case SIGTERM:
627 res = "Terminated";
628 break;
629 default:
630 Py_RETURN_NONE;
631 }
632#else
633 errno = 0;
634 res = strsignal(signalnum);
635
636 if (errno || res == NULL || strstr(res, "Unknown signal") != NULL)
637 Py_RETURN_NONE;
638#endif
639
640 return Py_BuildValue("s", res);
641}
642
Christian Heimes8640e742008-02-23 16:23:06 +0000643#ifdef HAVE_SIGINTERRUPT
Tal Einatc7027b72015-05-16 14:14:49 +0300644
645/*[clinic input]
646signal.siginterrupt
647
648 signalnum: int
649 flag: int
650 /
651
652Change system call restart behaviour.
653
654If flag is False, system calls will be restarted when interrupted by
655signal sig, else system calls will be interrupted.
656[clinic start generated code]*/
Christian Heimes8640e742008-02-23 16:23:06 +0000657
658static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300659signal_siginterrupt_impl(PyObject *module, int signalnum, int flag)
660/*[clinic end generated code: output=063816243d85dd19 input=4160acacca3e2099]*/
Christian Heimes8640e742008-02-23 16:23:06 +0000661{
Tal Einatc7027b72015-05-16 14:14:49 +0300662 if (signalnum < 1 || signalnum >= NSIG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000663 PyErr_SetString(PyExc_ValueError,
664 "signal number out of range");
665 return NULL;
666 }
Pablo Galindof9c5e3f2020-09-02 15:29:12 +0100667#ifdef HAVE_SIGACTION
668 struct sigaction act;
669 (void) sigaction(signalnum, NULL, &act);
670 if (flag) {
671 act.sa_flags &= ~SA_RESTART;
672 }
673 else {
674 act.sa_flags |= SA_RESTART;
675 }
676 if (sigaction(signalnum, &act, NULL) < 0) {
677#else
678 if (siginterrupt(signalnum, flag) < 0) {
679#endif
Victor Stinner388196e2011-05-10 17:13:00 +0200680 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000681 return NULL;
682 }
Tal Einatc7027b72015-05-16 14:14:49 +0300683 Py_RETURN_NONE;
Christian Heimes8640e742008-02-23 16:23:06 +0000684}
685
686#endif
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000687
Tal Einatc7027b72015-05-16 14:14:49 +0300688
689static PyObject*
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800690signal_set_wakeup_fd(PyObject *self, PyObject *args, PyObject *kwds)
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000691{
Victor Stinnere134a7f2015-03-30 10:09:31 +0200692 struct _Py_stat_struct status;
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800693 static char *kwlist[] = {
694 "", "warn_on_full_buffer", NULL,
695 };
696 int warn_on_full_buffer = 1;
Victor Stinner11517102014-07-29 23:31:34 +0200697#ifdef MS_WINDOWS
698 PyObject *fdobj;
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100699 SOCKET_T sockfd, old_sockfd;
Victor Stinner11517102014-07-29 23:31:34 +0200700 int res;
701 int res_size = sizeof res;
702 PyObject *mod;
Victor Stinner11517102014-07-29 23:31:34 +0200703 int is_socket;
704
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800705 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|$p:set_wakeup_fd", kwlist,
706 &fdobj, &warn_on_full_buffer))
Victor Stinner11517102014-07-29 23:31:34 +0200707 return NULL;
708
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100709 sockfd = PyLong_AsSocket_t(fdobj);
710 if (sockfd == (SOCKET_T)(-1) && PyErr_Occurred())
Victor Stinner11517102014-07-29 23:31:34 +0200711 return NULL;
712#else
Victor Stinnercda23be2020-11-17 18:57:32 +0100713 int fd;
Victor Stinner11517102014-07-29 23:31:34 +0200714
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800715 if (!PyArg_ParseTupleAndKeywords(args, kwds, "i|$p:set_wakeup_fd", kwlist,
716 &fd, &warn_on_full_buffer))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000717 return NULL;
Victor Stinner11517102014-07-29 23:31:34 +0200718#endif
719
Victor Stinner72818982020-03-26 22:28:11 +0100720 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200721 if (!_Py_ThreadCanHandleSignals(tstate->interp)) {
Victor Stinner72818982020-03-26 22:28:11 +0100722 _PyErr_SetString(tstate, PyExc_ValueError,
723 "set_wakeup_fd only works in main thread "
724 "of the main interpreter");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000725 return NULL;
726 }
Victor Stinner0bffc942014-07-21 16:28:54 +0200727
Victor Stinner11517102014-07-29 23:31:34 +0200728#ifdef MS_WINDOWS
729 is_socket = 0;
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100730 if (sockfd != INVALID_FD) {
Victor Stinner11517102014-07-29 23:31:34 +0200731 /* Import the _socket module to call WSAStartup() */
732 mod = PyImport_ImportModuleNoBlock("_socket");
733 if (mod == NULL)
734 return NULL;
735 Py_DECREF(mod);
736
737 /* test the socket */
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100738 if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR,
Victor Stinner11517102014-07-29 23:31:34 +0200739 (char *)&res, &res_size) != 0) {
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100740 int fd, err;
741
742 err = WSAGetLastError();
Victor Stinner11517102014-07-29 23:31:34 +0200743 if (err != WSAENOTSOCK) {
744 PyErr_SetExcFromWindowsErr(PyExc_OSError, err);
745 return NULL;
746 }
747
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100748 fd = (int)sockfd;
Steve Dower940f33a2016-09-08 11:21:54 -0700749 if ((SOCKET_T)fd != sockfd) {
Victor Stinner72818982020-03-26 22:28:11 +0100750 _PyErr_SetString(tstate, PyExc_ValueError, "invalid fd");
Victor Stinner11517102014-07-29 23:31:34 +0200751 return NULL;
752 }
753
Victor Stinner72818982020-03-26 22:28:11 +0100754 if (_Py_fstat(fd, &status) != 0) {
Victor Stinner11517102014-07-29 23:31:34 +0200755 return NULL;
Victor Stinner72818982020-03-26 22:28:11 +0100756 }
Victor Stinner38227602014-08-27 12:59:44 +0200757
758 /* on Windows, a file cannot be set to non-blocking mode */
Victor Stinner11517102014-07-29 23:31:34 +0200759 }
Victor Stinner38227602014-08-27 12:59:44 +0200760 else {
Victor Stinner11517102014-07-29 23:31:34 +0200761 is_socket = 1;
Victor Stinner38227602014-08-27 12:59:44 +0200762
763 /* Windows does not provide a function to test if a socket
764 is in non-blocking mode */
765 }
Victor Stinner11517102014-07-29 23:31:34 +0200766 }
767
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100768 old_sockfd = wakeup.fd;
769 wakeup.fd = sockfd;
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800770 wakeup.warn_on_full_buffer = warn_on_full_buffer;
Victor Stinner11517102014-07-29 23:31:34 +0200771 wakeup.use_send = is_socket;
772
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100773 if (old_sockfd != INVALID_FD)
774 return PyLong_FromSocket_t(old_sockfd);
Victor Stinner11517102014-07-29 23:31:34 +0200775 else
776 return PyLong_FromLong(-1);
777#else
Victor Stinnerd18ccd12014-07-24 21:58:53 +0200778 if (fd != -1) {
Victor Stinner38227602014-08-27 12:59:44 +0200779 int blocking;
780
Victor Stinnere134a7f2015-03-30 10:09:31 +0200781 if (_Py_fstat(fd, &status) != 0)
Victor Stinner11517102014-07-29 23:31:34 +0200782 return NULL;
Victor Stinner38227602014-08-27 12:59:44 +0200783
784 blocking = _Py_get_blocking(fd);
785 if (blocking < 0)
786 return NULL;
787 if (blocking) {
Victor Stinner72818982020-03-26 22:28:11 +0100788 _PyErr_Format(tstate, PyExc_ValueError,
789 "the fd %i must be in non-blocking mode",
790 fd);
Victor Stinner38227602014-08-27 12:59:44 +0200791 return NULL;
792 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000793 }
Victor Stinner0bffc942014-07-21 16:28:54 +0200794
Victor Stinnercda23be2020-11-17 18:57:32 +0100795 int old_fd = wakeup.fd;
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800796 wakeup.fd = fd;
797 wakeup.warn_on_full_buffer = warn_on_full_buffer;
Victor Stinner0bffc942014-07-21 16:28:54 +0200798
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000799 return PyLong_FromLong(old_fd);
Victor Stinner11517102014-07-29 23:31:34 +0200800#endif
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000801}
802
803PyDoc_STRVAR(set_wakeup_fd_doc,
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800804"set_wakeup_fd(fd, *, warn_on_full_buffer=True) -> fd\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000805\n\
Victor Stinner11517102014-07-29 23:31:34 +0200806Sets the fd to be written to (with the signal number) when a signal\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000807comes in. A library can use this to wakeup select or poll.\n\
Victor Stinner11517102014-07-29 23:31:34 +0200808The previous fd or -1 is returned.\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000809\n\
810The fd must be non-blocking.");
811
812/* C API for the same, without all the error checking */
813int
814PySignal_SetWakeupFd(int fd)
815{
Victor Stinnercda23be2020-11-17 18:57:32 +0100816 if (fd < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000817 fd = -1;
Victor Stinnercda23be2020-11-17 18:57:32 +0100818 }
Victor Stinner11517102014-07-29 23:31:34 +0200819
820#ifdef MS_WINDOWS
Victor Stinnercda23be2020-11-17 18:57:32 +0100821 int old_fd = Py_SAFE_DOWNCAST(wakeup.fd, SOCKET_T, int);
Victor Stinner11517102014-07-29 23:31:34 +0200822#else
Victor Stinnercda23be2020-11-17 18:57:32 +0100823 int old_fd = wakeup.fd;
Victor Stinner11517102014-07-29 23:31:34 +0200824#endif
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800825 wakeup.fd = fd;
826 wakeup.warn_on_full_buffer = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000827 return old_fd;
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000828}
829
830
Martin v. Löwis823725e2008-03-24 13:39:54 +0000831#ifdef HAVE_SETITIMER
Tal Einatc7027b72015-05-16 14:14:49 +0300832
833/*[clinic input]
834signal.setitimer
835
836 which: int
Victor Stinneref611c92017-10-13 13:49:43 -0700837 seconds: object
838 interval: object(c_default="NULL") = 0.0
Tal Einatc7027b72015-05-16 14:14:49 +0300839 /
840
841Sets given itimer (one of ITIMER_REAL, ITIMER_VIRTUAL or ITIMER_PROF).
842
843The timer will fire after value seconds and after that every interval seconds.
844The itimer can be cleared by setting seconds to zero.
845
846Returns old values as a tuple: (delay, interval).
847[clinic start generated code]*/
848
Martin v. Löwis823725e2008-03-24 13:39:54 +0000849static PyObject *
Victor Stinneref611c92017-10-13 13:49:43 -0700850signal_setitimer_impl(PyObject *module, int which, PyObject *seconds,
851 PyObject *interval)
852/*[clinic end generated code: output=65f9dcbddc35527b input=de43daf194e6f66f]*/
Martin v. Löwis823725e2008-03-24 13:39:54 +0000853{
Victor Stinnercda23be2020-11-17 18:57:32 +0100854 struct itimerval new;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000855
Victor Stinneref611c92017-10-13 13:49:43 -0700856 if (timeval_from_double(seconds, &new.it_value) < 0) {
857 return NULL;
858 }
859 if (timeval_from_double(interval, &new.it_interval) < 0) {
860 return NULL;
861 }
862
Martin v. Löwis823725e2008-03-24 13:39:54 +0000863 /* Let OS check "which" value */
Victor Stinnercda23be2020-11-17 18:57:32 +0100864 struct itimerval old;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000865 if (setitimer(which, &new, &old) != 0) {
Tal Einatc7027b72015-05-16 14:14:49 +0300866 PyErr_SetFromErrno(ItimerError);
867 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000868 }
869
870 return itimer_retval(&old);
871}
872
Martin v. Löwis823725e2008-03-24 13:39:54 +0000873#endif
874
875
876#ifdef HAVE_GETITIMER
Tal Einatc7027b72015-05-16 14:14:49 +0300877
878/*[clinic input]
879signal.getitimer
880
881 which: int
882 /
883
884Returns current value of given itimer.
885[clinic start generated code]*/
886
Martin v. Löwis823725e2008-03-24 13:39:54 +0000887static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300888signal_getitimer_impl(PyObject *module, int which)
889/*[clinic end generated code: output=9e053175d517db40 input=f7d21d38f3490627]*/
Martin v. Löwis823725e2008-03-24 13:39:54 +0000890{
Martin v. Löwis823725e2008-03-24 13:39:54 +0000891 struct itimerval old;
892
Martin v. Löwis823725e2008-03-24 13:39:54 +0000893 if (getitimer(which, &old) != 0) {
Tal Einatc7027b72015-05-16 14:14:49 +0300894 PyErr_SetFromErrno(ItimerError);
895 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000896 }
897
898 return itimer_retval(&old);
899}
900
Martin v. Löwis823725e2008-03-24 13:39:54 +0000901#endif
902
Victor Stinnerb3e72192011-05-08 01:46:11 +0200903#if defined(PYPTHREAD_SIGMASK) || defined(HAVE_SIGPENDING)
Victor Stinner35b300c2011-05-04 13:20:35 +0200904static PyObject*
905sigset_to_set(sigset_t mask)
906{
907 PyObject *signum, *result;
908 int sig;
909
910 result = PySet_New(0);
911 if (result == NULL)
912 return NULL;
913
914 for (sig = 1; sig < NSIG; sig++) {
915 if (sigismember(&mask, sig) != 1)
916 continue;
917
918 /* Handle the case where it is a member by adding the signal to
919 the result list. Ignore the other cases because they mean the
920 signal isn't a member of the mask or the signal was invalid,
921 and an invalid signal must have been our fault in constructing
922 the loop boundaries. */
923 signum = PyLong_FromLong(sig);
924 if (signum == NULL) {
925 Py_DECREF(result);
926 return NULL;
927 }
928 if (PySet_Add(result, signum) == -1) {
929 Py_DECREF(signum);
930 Py_DECREF(result);
931 return NULL;
932 }
933 Py_DECREF(signum);
934 }
935 return result;
936}
Victor Stinnerb3e72192011-05-08 01:46:11 +0200937#endif
Victor Stinner35b300c2011-05-04 13:20:35 +0200938
Victor Stinnerb3e72192011-05-08 01:46:11 +0200939#ifdef PYPTHREAD_SIGMASK
Tal Einatc7027b72015-05-16 14:14:49 +0300940
941/*[clinic input]
942signal.pthread_sigmask
943
944 how: int
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300945 mask: sigset_t
Tal Einatc7027b72015-05-16 14:14:49 +0300946 /
947
948Fetch and/or change the signal mask of the calling thread.
949[clinic start generated code]*/
950
Victor Stinnera9293352011-04-30 15:21:58 +0200951static PyObject *
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300952signal_pthread_sigmask_impl(PyObject *module, int how, sigset_t mask)
953/*[clinic end generated code: output=0562c0fb192981a8 input=85bcebda442fa77f]*/
Victor Stinnera9293352011-04-30 15:21:58 +0200954{
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300955 sigset_t previous;
Victor Stinnera9293352011-04-30 15:21:58 +0200956 int err;
957
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300958 err = pthread_sigmask(how, &mask, &previous);
Victor Stinnera9293352011-04-30 15:21:58 +0200959 if (err != 0) {
960 errno = err;
Victor Stinnerb3e72192011-05-08 01:46:11 +0200961 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnera9293352011-04-30 15:21:58 +0200962 return NULL;
963 }
964
Victor Stinnerd0e516d2011-05-03 14:57:12 +0200965 /* if signals was unblocked, signal handlers have been called */
966 if (PyErr_CheckSignals())
967 return NULL;
968
Victor Stinner35b300c2011-05-04 13:20:35 +0200969 return sigset_to_set(previous);
Victor Stinnera9293352011-04-30 15:21:58 +0200970}
971
Victor Stinnera9293352011-04-30 15:21:58 +0200972#endif /* #ifdef PYPTHREAD_SIGMASK */
973
Martin v. Löwis823725e2008-03-24 13:39:54 +0000974
Victor Stinnerb3e72192011-05-08 01:46:11 +0200975#ifdef HAVE_SIGPENDING
Tal Einatc7027b72015-05-16 14:14:49 +0300976
977/*[clinic input]
978signal.sigpending
979
980Examine pending signals.
981
982Returns a set of signal numbers that are pending for delivery to
983the calling thread.
984[clinic start generated code]*/
985
Victor Stinnerb3e72192011-05-08 01:46:11 +0200986static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300987signal_sigpending_impl(PyObject *module)
988/*[clinic end generated code: output=53375ffe89325022 input=e0036c016f874e29]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +0200989{
990 int err;
991 sigset_t mask;
992 err = sigpending(&mask);
993 if (err)
994 return PyErr_SetFromErrno(PyExc_OSError);
995 return sigset_to_set(mask);
996}
997
Victor Stinnerb3e72192011-05-08 01:46:11 +0200998#endif /* #ifdef HAVE_SIGPENDING */
999
1000
1001#ifdef HAVE_SIGWAIT
Tal Einatc7027b72015-05-16 14:14:49 +03001002
1003/*[clinic input]
1004signal.sigwait
1005
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001006 sigset: sigset_t
Tal Einatc7027b72015-05-16 14:14:49 +03001007 /
1008
1009Wait for a signal.
1010
1011Suspend execution of the calling thread until the delivery of one of the
1012signals specified in the signal set sigset. The function accepts the signal
1013and returns the signal number.
1014[clinic start generated code]*/
1015
Victor Stinnerb3e72192011-05-08 01:46:11 +02001016static PyObject *
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001017signal_sigwait_impl(PyObject *module, sigset_t sigset)
1018/*[clinic end generated code: output=f43770699d682f96 input=a6fbd47b1086d119]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +02001019{
Victor Stinnerb3e72192011-05-08 01:46:11 +02001020 int err, signum;
1021
Victor Stinner10c30d62011-06-10 01:39:53 +02001022 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001023 err = sigwait(&sigset, &signum);
Victor Stinner10c30d62011-06-10 01:39:53 +02001024 Py_END_ALLOW_THREADS
Victor Stinnerb3e72192011-05-08 01:46:11 +02001025 if (err) {
1026 errno = err;
1027 return PyErr_SetFromErrno(PyExc_OSError);
1028 }
1029
1030 return PyLong_FromLong(signum);
1031}
1032
Tal Einatc7027b72015-05-16 14:14:49 +03001033#endif /* #ifdef HAVE_SIGWAIT */
1034
Victor Stinnerb3e72192011-05-08 01:46:11 +02001035
Antoine Pitrou9d3627e2018-05-04 13:00:50 +02001036#if defined(HAVE_SIGFILLSET) || defined(MS_WINDOWS)
1037
1038/*[clinic input]
1039signal.valid_signals
1040
1041Return a set of valid signal numbers on this platform.
1042
1043The signal numbers returned by this function can be safely passed to
1044functions like `pthread_sigmask`.
1045[clinic start generated code]*/
1046
1047static PyObject *
1048signal_valid_signals_impl(PyObject *module)
1049/*[clinic end generated code: output=1609cffbcfcf1314 input=86a3717ff25288f2]*/
1050{
1051#ifdef MS_WINDOWS
1052#ifdef SIGBREAK
1053 PyObject *tup = Py_BuildValue("(iiiiiii)", SIGABRT, SIGBREAK, SIGFPE,
1054 SIGILL, SIGINT, SIGSEGV, SIGTERM);
1055#else
1056 PyObject *tup = Py_BuildValue("(iiiiii)", SIGABRT, SIGFPE, SIGILL,
1057 SIGINT, SIGSEGV, SIGTERM);
1058#endif
1059 if (tup == NULL) {
1060 return NULL;
1061 }
1062 PyObject *set = PySet_New(tup);
1063 Py_DECREF(tup);
1064 return set;
1065#else
1066 sigset_t mask;
1067 if (sigemptyset(&mask) || sigfillset(&mask)) {
1068 return PyErr_SetFromErrno(PyExc_OSError);
1069 }
1070 return sigset_to_set(mask);
1071#endif
1072}
1073
1074#endif /* #if defined(HAVE_SIGFILLSET) || defined(MS_WINDOWS) */
1075
1076
Ross Lagerwallbc808222011-06-25 12:13:40 +02001077#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
Ross Lagerwallbc808222011-06-25 12:13:40 +02001078static PyStructSequence_Field struct_siginfo_fields[] = {
1079 {"si_signo", "signal number"},
1080 {"si_code", "signal code"},
1081 {"si_errno", "errno associated with this signal"},
1082 {"si_pid", "sending process ID"},
1083 {"si_uid", "real user ID of sending process"},
1084 {"si_status", "exit value or signal"},
1085 {"si_band", "band event for SIGPOLL"},
1086 {0}
1087};
1088
1089PyDoc_STRVAR(struct_siginfo__doc__,
1090"struct_siginfo: Result from sigwaitinfo or sigtimedwait.\n\n\
1091This object may be accessed either as a tuple of\n\
1092(si_signo, si_code, si_errno, si_pid, si_uid, si_status, si_band),\n\
1093or via the attributes si_signo, si_code, and so on.");
1094
1095static PyStructSequence_Desc struct_siginfo_desc = {
1096 "signal.struct_siginfo", /* name */
1097 struct_siginfo__doc__, /* doc */
1098 struct_siginfo_fields, /* fields */
1099 7 /* n_in_sequence */
1100};
1101
1102static PyTypeObject SiginfoType;
1103
1104static PyObject *
1105fill_siginfo(siginfo_t *si)
1106{
1107 PyObject *result = PyStructSequence_New(&SiginfoType);
1108 if (!result)
1109 return NULL;
1110
1111 PyStructSequence_SET_ITEM(result, 0, PyLong_FromLong((long)(si->si_signo)));
1112 PyStructSequence_SET_ITEM(result, 1, PyLong_FromLong((long)(si->si_code)));
Victor Stinner09532fe2019-05-10 23:39:09 +02001113#ifdef __VXWORKS__
pxinwr8b5bdda2019-03-14 01:18:25 +08001114 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong(0L));
1115 PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong(0L));
1116 PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong(0L));
1117 PyStructSequence_SET_ITEM(result, 5, PyLong_FromLong(0L));
Victor Stinner09532fe2019-05-10 23:39:09 +02001118#else
Ross Lagerwallbc808222011-06-25 12:13:40 +02001119 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si->si_errno)));
1120 PyStructSequence_SET_ITEM(result, 3, PyLong_FromPid(si->si_pid));
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02001121 PyStructSequence_SET_ITEM(result, 4, _PyLong_FromUid(si->si_uid));
Ross Lagerwallbc808222011-06-25 12:13:40 +02001122 PyStructSequence_SET_ITEM(result, 5,
1123 PyLong_FromLong((long)(si->si_status)));
Victor Stinner09532fe2019-05-10 23:39:09 +02001124#endif
Zachary Ware6a6967e2016-10-01 00:47:27 -05001125#ifdef HAVE_SIGINFO_T_SI_BAND
Ross Lagerwallbc808222011-06-25 12:13:40 +02001126 PyStructSequence_SET_ITEM(result, 6, PyLong_FromLong(si->si_band));
Zachary Ware6a6967e2016-10-01 00:47:27 -05001127#else
1128 PyStructSequence_SET_ITEM(result, 6, PyLong_FromLong(0L));
1129#endif
Ross Lagerwallbc808222011-06-25 12:13:40 +02001130 if (PyErr_Occurred()) {
1131 Py_DECREF(result);
1132 return NULL;
1133 }
1134
1135 return result;
1136}
1137#endif
1138
1139#ifdef HAVE_SIGWAITINFO
Tal Einatc7027b72015-05-16 14:14:49 +03001140
1141/*[clinic input]
1142signal.sigwaitinfo
1143
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001144 sigset: sigset_t
Tal Einatc7027b72015-05-16 14:14:49 +03001145 /
1146
1147Wait synchronously until one of the signals in *sigset* is delivered.
1148
1149Returns a struct_siginfo containing information about the signal.
1150[clinic start generated code]*/
1151
Ross Lagerwallbc808222011-06-25 12:13:40 +02001152static PyObject *
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001153signal_sigwaitinfo_impl(PyObject *module, sigset_t sigset)
1154/*[clinic end generated code: output=1eb2f1fa236fdbca input=3d1a7e1f27fc664c]*/
Ross Lagerwallbc808222011-06-25 12:13:40 +02001155{
Ross Lagerwallbc808222011-06-25 12:13:40 +02001156 siginfo_t si;
1157 int err;
Victor Stinnera453cd82015-03-20 12:54:28 +01001158 int async_err = 0;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001159
Victor Stinnera453cd82015-03-20 12:54:28 +01001160 do {
1161 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001162 err = sigwaitinfo(&sigset, &si);
Victor Stinnera453cd82015-03-20 12:54:28 +01001163 Py_END_ALLOW_THREADS
1164 } while (err == -1
1165 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Ross Lagerwallbc808222011-06-25 12:13:40 +02001166 if (err == -1)
Victor Stinnera453cd82015-03-20 12:54:28 +01001167 return (!async_err) ? PyErr_SetFromErrno(PyExc_OSError) : NULL;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001168
1169 return fill_siginfo(&si);
1170}
1171
Ross Lagerwallbc808222011-06-25 12:13:40 +02001172#endif /* #ifdef HAVE_SIGWAITINFO */
1173
1174#ifdef HAVE_SIGTIMEDWAIT
Tal Einatc7027b72015-05-16 14:14:49 +03001175
1176/*[clinic input]
1177signal.sigtimedwait
1178
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001179 sigset: sigset_t
Serhiy Storchaka6b680cd2015-05-16 15:57:56 +03001180 timeout as timeout_obj: object
Tal Einatc7027b72015-05-16 14:14:49 +03001181 /
1182
1183Like sigwaitinfo(), but with a timeout.
1184
1185The timeout is specified in seconds, with floating point numbers allowed.
1186[clinic start generated code]*/
1187
Ross Lagerwallbc808222011-06-25 12:13:40 +02001188static PyObject *
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001189signal_sigtimedwait_impl(PyObject *module, sigset_t sigset,
Serhiy Storchaka6b680cd2015-05-16 15:57:56 +03001190 PyObject *timeout_obj)
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001191/*[clinic end generated code: output=59c8971e8ae18a64 input=87fd39237cf0b7ba]*/
Ross Lagerwallbc808222011-06-25 12:13:40 +02001192{
Victor Stinnera453cd82015-03-20 12:54:28 +01001193 struct timespec ts;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001194 siginfo_t si;
Victor Stinnera453cd82015-03-20 12:54:28 +01001195 int res;
Victor Stinner34dc0f42015-03-27 18:19:03 +01001196 _PyTime_t timeout, deadline, monotonic;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001197
Victor Stinner869e1772015-03-30 03:49:14 +02001198 if (_PyTime_FromSecondsObject(&timeout,
1199 timeout_obj, _PyTime_ROUND_CEILING) < 0)
Ross Lagerwallbc808222011-06-25 12:13:40 +02001200 return NULL;
1201
Victor Stinnera453cd82015-03-20 12:54:28 +01001202 if (timeout < 0) {
Ross Lagerwallbc808222011-06-25 12:13:40 +02001203 PyErr_SetString(PyExc_ValueError, "timeout must be non-negative");
1204 return NULL;
1205 }
1206
Victor Stinner34dc0f42015-03-27 18:19:03 +01001207 deadline = _PyTime_GetMonotonicClock() + timeout;
Victor Stinnera453cd82015-03-20 12:54:28 +01001208
1209 do {
Victor Stinner34dc0f42015-03-27 18:19:03 +01001210 if (_PyTime_AsTimespec(timeout, &ts) < 0)
1211 return NULL;
Victor Stinnera453cd82015-03-20 12:54:28 +01001212
1213 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001214 res = sigtimedwait(&sigset, &si, &ts);
Victor Stinnera453cd82015-03-20 12:54:28 +01001215 Py_END_ALLOW_THREADS
1216
1217 if (res != -1)
1218 break;
1219
1220 if (errno != EINTR) {
1221 if (errno == EAGAIN)
1222 Py_RETURN_NONE;
1223 else
1224 return PyErr_SetFromErrno(PyExc_OSError);
1225 }
1226
1227 /* sigtimedwait() was interrupted by a signal (EINTR) */
1228 if (PyErr_CheckSignals())
1229 return NULL;
1230
Victor Stinner34dc0f42015-03-27 18:19:03 +01001231 monotonic = _PyTime_GetMonotonicClock();
1232 timeout = deadline - monotonic;
Victor Stinner6aa446c2015-03-30 21:33:51 +02001233 if (timeout < 0)
Victor Stinnera453cd82015-03-20 12:54:28 +01001234 break;
1235 } while (1);
Ross Lagerwallbc808222011-06-25 12:13:40 +02001236
1237 return fill_siginfo(&si);
1238}
1239
Ross Lagerwallbc808222011-06-25 12:13:40 +02001240#endif /* #ifdef HAVE_SIGTIMEDWAIT */
1241
Victor Stinnerb3e72192011-05-08 01:46:11 +02001242
Antoine Pitroua6a4dc82017-09-07 18:56:24 +02001243#if defined(HAVE_PTHREAD_KILL)
Tal Einatc7027b72015-05-16 14:14:49 +03001244
1245/*[clinic input]
1246signal.pthread_kill
1247
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +02001248 thread_id: unsigned_long(bitwise=True)
Tal Einatc7027b72015-05-16 14:14:49 +03001249 signalnum: int
1250 /
1251
1252Send a signal to a thread.
1253[clinic start generated code]*/
1254
Victor Stinnerb3e72192011-05-08 01:46:11 +02001255static PyObject *
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +02001256signal_pthread_kill_impl(PyObject *module, unsigned long thread_id,
1257 int signalnum)
1258/*[clinic end generated code: output=7629919b791bc27f input=1d901f2c7bb544ff]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +02001259{
Victor Stinnerb3e72192011-05-08 01:46:11 +02001260 int err;
1261
Saiyang Gou7514f4f2020-02-12 23:47:42 -08001262 if (PySys_Audit("signal.pthread_kill", "ki", thread_id, signalnum) < 0) {
1263 return NULL;
1264 }
1265
Tal Einatc7027b72015-05-16 14:14:49 +03001266 err = pthread_kill((pthread_t)thread_id, signalnum);
Victor Stinnerb3e72192011-05-08 01:46:11 +02001267 if (err != 0) {
1268 errno = err;
1269 PyErr_SetFromErrno(PyExc_OSError);
1270 return NULL;
1271 }
1272
1273 /* the signal may have been send to the current thread */
1274 if (PyErr_CheckSignals())
1275 return NULL;
1276
1277 Py_RETURN_NONE;
1278}
1279
Antoine Pitroua6a4dc82017-09-07 18:56:24 +02001280#endif /* #if defined(HAVE_PTHREAD_KILL) */
Victor Stinnerb3e72192011-05-08 01:46:11 +02001281
1282
Benjamin Peterson74834512019-11-19 20:39:14 -08001283#if defined(__linux__) && defined(__NR_pidfd_send_signal)
1284/*[clinic input]
1285signal.pidfd_send_signal
1286
1287 pidfd: int
1288 signalnum: int
1289 siginfo: object = None
1290 flags: int = 0
1291 /
1292
1293Send a signal to a process referred to by a pid file descriptor.
1294[clinic start generated code]*/
1295
1296static PyObject *
1297signal_pidfd_send_signal_impl(PyObject *module, int pidfd, int signalnum,
1298 PyObject *siginfo, int flags)
1299/*[clinic end generated code: output=2d59f04a75d9cbdf input=2a6543a1f4ac2000]*/
1300
1301{
1302 if (siginfo != Py_None) {
1303 PyErr_SetString(PyExc_TypeError, "siginfo must be None");
1304 return NULL;
1305 }
1306 if (syscall(__NR_pidfd_send_signal, pidfd, signalnum, NULL, flags) < 0) {
1307 PyErr_SetFromErrno(PyExc_OSError);
1308 return NULL;
1309 }
1310 Py_RETURN_NONE;
1311}
1312#endif
1313
1314
Victor Stinnerb3e72192011-05-08 01:46:11 +02001315
Tal Einatc7027b72015-05-16 14:14:49 +03001316/* List of functions defined in the module -- some of the methoddefs are
1317 defined to nothing if the corresponding C function is not available. */
Barry Warsaw92971171997-01-03 00:14:25 +00001318static PyMethodDef signal_methods[] = {
Serhiy Storchakab0689ae2020-07-12 19:15:20 +03001319 SIGNAL_DEFAULT_INT_HANDLER_METHODDEF
Tal Einatc7027b72015-05-16 14:14:49 +03001320 SIGNAL_ALARM_METHODDEF
1321 SIGNAL_SETITIMER_METHODDEF
1322 SIGNAL_GETITIMER_METHODDEF
1323 SIGNAL_SIGNAL_METHODDEF
Vladimir Matveevc24c6c22019-01-08 01:58:25 -08001324 SIGNAL_RAISE_SIGNAL_METHODDEF
Antoine Pietri5d2a27d2018-03-12 14:42:34 +01001325 SIGNAL_STRSIGNAL_METHODDEF
Tal Einatc7027b72015-05-16 14:14:49 +03001326 SIGNAL_GETSIGNAL_METHODDEF
Serhiy Storchaka62be7422018-11-27 13:27:31 +02001327 {"set_wakeup_fd", (PyCFunction)(void(*)(void))signal_set_wakeup_fd, METH_VARARGS | METH_KEYWORDS, set_wakeup_fd_doc},
Tal Einatc7027b72015-05-16 14:14:49 +03001328 SIGNAL_SIGINTERRUPT_METHODDEF
1329 SIGNAL_PAUSE_METHODDEF
Benjamin Peterson74834512019-11-19 20:39:14 -08001330 SIGNAL_PIDFD_SEND_SIGNAL_METHODDEF
Tal Einatc7027b72015-05-16 14:14:49 +03001331 SIGNAL_PTHREAD_KILL_METHODDEF
1332 SIGNAL_PTHREAD_SIGMASK_METHODDEF
1333 SIGNAL_SIGPENDING_METHODDEF
1334 SIGNAL_SIGWAIT_METHODDEF
1335 SIGNAL_SIGWAITINFO_METHODDEF
1336 SIGNAL_SIGTIMEDWAIT_METHODDEF
Antoine Pitrou9d3627e2018-05-04 13:00:50 +02001337#if defined(HAVE_SIGFILLSET) || defined(MS_WINDOWS)
1338 SIGNAL_VALID_SIGNALS_METHODDEF
1339#endif
Tal Einatc7027b72015-05-16 14:14:49 +03001340 {NULL, NULL} /* sentinel */
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001341};
1342
Barry Warsaw92971171997-01-03 00:14:25 +00001343
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001344PyDoc_STRVAR(module_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001345"This module provides mechanisms to use signal handlers in Python.\n\
1346\n\
1347Functions:\n\
1348\n\
1349alarm() -- cause SIGALRM after a specified time [Unix only]\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001350setitimer() -- cause a signal (described below) after a specified\n\
1351 float time and the timer may restart then [Unix only]\n\
1352getitimer() -- get current value of timer [Unix only]\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001353signal() -- set the action for a given signal\n\
1354getsignal() -- get the signal action for a given signal\n\
1355pause() -- wait until a signal arrives [Unix only]\n\
1356default_int_handler() -- default SIGINT handler\n\
1357\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001358signal constants:\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001359SIG_DFL -- used to refer to the system default handler\n\
1360SIG_IGN -- used to ignore the signal\n\
1361NSIG -- number of defined signals\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001362SIGINT, SIGTERM, etc. -- signal numbers\n\
1363\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001364itimer constants:\n\
1365ITIMER_REAL -- decrements in real time, and delivers SIGALRM upon\n\
1366 expiration\n\
1367ITIMER_VIRTUAL -- decrements only when the process is executing,\n\
1368 and delivers SIGVTALRM upon expiration\n\
1369ITIMER_PROF -- decrements both when the process is executing and\n\
1370 when the system is executing on behalf of the process.\n\
1371 Coupled with ITIMER_VIRTUAL, this timer is usually\n\
1372 used to profile the time spent by the application\n\
1373 in user and kernel space. SIGPROF is delivered upon\n\
1374 expiration.\n\
1375\n\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001376*** IMPORTANT NOTICE ***\n\
1377A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001378the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001379
Martin v. Löwis1a214512008-06-11 05:26:20 +00001380
Mohamed Koubaa71d1bd92020-09-03 03:21:06 -05001381
1382static int
Victor Stinnercda23be2020-11-17 18:57:32 +01001383signal_add_constants(PyObject *module)
1384{
1385#define ADD_INT_MACRO(macro) \
1386 if (PyModule_AddIntConstant(module, #macro, macro) < 0) { \
1387 return -1; \
1388 }
1389
1390 ADD_INT_MACRO(NSIG);
1391
1392 // SIG_xxx pthread_sigmask() constants
1393#ifdef SIG_BLOCK
1394 ADD_INT_MACRO(SIG_BLOCK);
1395#endif
1396#ifdef SIG_UNBLOCK
1397 ADD_INT_MACRO(SIG_UNBLOCK);
1398#endif
1399#ifdef SIG_SETMASK
1400 ADD_INT_MACRO(SIG_SETMASK);
1401#endif
1402
1403 // SIGxxx signal number constants
1404#ifdef SIGHUP
1405 ADD_INT_MACRO(SIGHUP);
1406#endif
1407#ifdef SIGINT
1408 ADD_INT_MACRO(SIGINT);
1409#endif
1410#ifdef SIGBREAK
1411 ADD_INT_MACRO(SIGBREAK);
1412#endif
1413#ifdef SIGQUIT
1414 ADD_INT_MACRO(SIGQUIT);
1415#endif
1416#ifdef SIGILL
1417 ADD_INT_MACRO(SIGILL);
1418#endif
1419#ifdef SIGTRAP
1420 ADD_INT_MACRO(SIGTRAP);
1421#endif
1422#ifdef SIGIOT
1423 ADD_INT_MACRO(SIGIOT);
1424#endif
1425#ifdef SIGABRT
1426 ADD_INT_MACRO(SIGABRT);
1427#endif
1428#ifdef SIGEMT
1429 ADD_INT_MACRO(SIGEMT);
1430#endif
1431#ifdef SIGFPE
1432 ADD_INT_MACRO(SIGFPE);
1433#endif
1434#ifdef SIGKILL
1435 ADD_INT_MACRO(SIGKILL);
1436#endif
1437#ifdef SIGBUS
1438 ADD_INT_MACRO(SIGBUS);
1439#endif
1440#ifdef SIGSEGV
1441 ADD_INT_MACRO(SIGSEGV);
1442#endif
1443#ifdef SIGSYS
1444 ADD_INT_MACRO(SIGSYS);
1445#endif
1446#ifdef SIGPIPE
1447 ADD_INT_MACRO(SIGPIPE);
1448#endif
1449#ifdef SIGALRM
1450 ADD_INT_MACRO(SIGALRM);
1451#endif
1452#ifdef SIGTERM
1453 ADD_INT_MACRO(SIGTERM);
1454#endif
1455#ifdef SIGUSR1
1456 ADD_INT_MACRO(SIGUSR1);
1457#endif
1458#ifdef SIGUSR2
1459 ADD_INT_MACRO(SIGUSR2);
1460#endif
1461#ifdef SIGCLD
1462 ADD_INT_MACRO(SIGCLD);
1463#endif
1464#ifdef SIGCHLD
1465 ADD_INT_MACRO(SIGCHLD);
1466#endif
1467#ifdef SIGPWR
1468 ADD_INT_MACRO(SIGPWR);
1469#endif
1470#ifdef SIGIO
1471 ADD_INT_MACRO(SIGIO);
1472#endif
1473#ifdef SIGURG
1474 ADD_INT_MACRO(SIGURG);
1475#endif
1476#ifdef SIGWINCH
1477 ADD_INT_MACRO(SIGWINCH);
1478#endif
1479#ifdef SIGPOLL
1480 ADD_INT_MACRO(SIGPOLL);
1481#endif
1482#ifdef SIGSTOP
1483 ADD_INT_MACRO(SIGSTOP);
1484#endif
1485#ifdef SIGTSTP
1486 ADD_INT_MACRO(SIGTSTP);
1487#endif
1488#ifdef SIGCONT
1489 ADD_INT_MACRO(SIGCONT);
1490#endif
1491#ifdef SIGTTIN
1492 ADD_INT_MACRO(SIGTTIN);
1493#endif
1494#ifdef SIGTTOU
1495 ADD_INT_MACRO(SIGTTOU);
1496#endif
1497#ifdef SIGVTALRM
1498 ADD_INT_MACRO(SIGVTALRM);
1499#endif
1500#ifdef SIGPROF
1501 ADD_INT_MACRO(SIGPROF);
1502#endif
1503#ifdef SIGXCPU
1504 ADD_INT_MACRO(SIGXCPU);
1505#endif
1506#ifdef SIGXFSZ
1507 ADD_INT_MACRO(SIGXFSZ);
1508#endif
1509#ifdef SIGRTMIN
1510 ADD_INT_MACRO(SIGRTMIN);
1511#endif
1512#ifdef SIGRTMAX
1513 ADD_INT_MACRO(SIGRTMAX);
1514#endif
1515#ifdef SIGINFO
1516 ADD_INT_MACRO(SIGINFO);
1517#endif
1518
1519 // ITIMER_xxx constants
1520#ifdef ITIMER_REAL
1521 ADD_INT_MACRO(ITIMER_REAL);
1522#endif
1523#ifdef ITIMER_VIRTUAL
1524 ADD_INT_MACRO(ITIMER_VIRTUAL);
1525#endif
1526#ifdef ITIMER_PROF
1527 ADD_INT_MACRO(ITIMER_PROF);
1528#endif
1529
1530 // CTRL_xxx Windows signals
1531#ifdef CTRL_C_EVENT
1532 ADD_INT_MACRO(CTRL_C_EVENT);
1533#endif
1534#ifdef CTRL_BREAK_EVENT
1535 ADD_INT_MACRO(CTRL_BREAK_EVENT);
1536#endif
1537
1538 return 0;
1539
1540#undef ADD_INT_MACRO
1541}
1542
1543
1544static int
Victor Stinner7f9b25a2020-11-17 23:28:25 +01001545signal_module_exec(PyObject *m)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001546{
Victor Stinnercda23be2020-11-17 18:57:32 +01001547 assert(!PyErr_Occurred());
1548
1549 if (signal_add_constants(m) < 0) {
1550 return -1;
1551 }
1552
1553 /* Add some symbolic constants to the module */
1554 PyObject *d = PyModule_GetDict(m);
1555 if (PyDict_SetItemString(d, "SIG_DFL", DefaultHandler) < 0) {
1556 return -1;
1557 }
1558 if (PyDict_SetItemString(d, "SIG_IGN", IgnoreHandler) < 0) {
1559 return -1;
1560 }
1561#if defined(HAVE_GETITIMER) || defined(HAVE_SETITIMER)
1562 if (PyDict_SetItemString(d, "ItimerError", ItimerError) < 0) {
1563 return -1;
1564 }
1565#endif
Ross Lagerwallbc808222011-06-25 12:13:40 +02001566#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
Mohamed Koubaa71d1bd92020-09-03 03:21:06 -05001567 if (PyModule_AddType(m, &SiginfoType) < 0) {
1568 return -1;
1569 }
Ross Lagerwallbc808222011-06-25 12:13:40 +02001570#endif
1571
Victor Stinnercda23be2020-11-17 18:57:32 +01001572 // Get signal handlers
1573 for (int signum = 1; signum < NSIG; signum++) {
1574 void (*c_handler)(int) = PyOS_getsig(signum);
Victor Stinner7f9b25a2020-11-17 23:28:25 +01001575 PyObject *func;
Victor Stinnercda23be2020-11-17 18:57:32 +01001576 if (c_handler == SIG_DFL) {
Victor Stinner7f9b25a2020-11-17 23:28:25 +01001577 func = DefaultHandler;
Victor Stinnercda23be2020-11-17 18:57:32 +01001578 }
1579 else if (c_handler == SIG_IGN) {
Victor Stinner7f9b25a2020-11-17 23:28:25 +01001580 func = IgnoreHandler;
Victor Stinnercda23be2020-11-17 18:57:32 +01001581 }
1582 else {
Victor Stinner7f9b25a2020-11-17 23:28:25 +01001583 func = Py_None; // None of our business
Victor Stinnercda23be2020-11-17 18:57:32 +01001584 }
Victor Stinner7f9b25a2020-11-17 23:28:25 +01001585 // If signal_module_exec() is called more than one, we must
1586 // clear the strong reference to the previous function.
1587 Py_XSETREF(Handlers[signum].func, Py_NewRef(func));
animalize77643c42019-09-09 21:46:26 +08001588 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001589
Victor Stinnercda23be2020-11-17 18:57:32 +01001590 // Instal Python SIGINT handler which raises KeyboardInterrupt
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001591 if (Handlers[SIGINT].func == DefaultHandler) {
Victor Stinner0ae323b2020-11-17 18:15:20 +01001592 PyObject *int_handler = PyMapping_GetItemString(d, "default_int_handler");
1593 if (!int_handler) {
1594 return -1;
1595 }
1596
Victor Stinner0ae323b2020-11-17 18:15:20 +01001597 Py_SETREF(Handlers[SIGINT].func, int_handler);
pkerlinge905c842018-06-01 09:47:18 +00001598 PyOS_setsig(SIGINT, signal_handler);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001599 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001600
Victor Stinnercda23be2020-11-17 18:57:32 +01001601 assert(!PyErr_Occurred());
1602 return 0;
Mohamed Koubaa71d1bd92020-09-03 03:21:06 -05001603}
1604
Mohamed Koubaa71d1bd92020-09-03 03:21:06 -05001605
Victor Stinner7f9b25a2020-11-17 23:28:25 +01001606static PyModuleDef_Slot signal_slots[] = {
1607 {Py_mod_exec, signal_module_exec},
1608 {0, NULL}
1609};
1610
1611static struct PyModuleDef signal_module = {
Mohamed Koubaa71d1bd92020-09-03 03:21:06 -05001612 PyModuleDef_HEAD_INIT,
1613 "_signal",
1614 .m_doc = module_doc,
Victor Stinner7f9b25a2020-11-17 23:28:25 +01001615 .m_size = 0,
Mohamed Koubaa71d1bd92020-09-03 03:21:06 -05001616 .m_methods = signal_methods,
Victor Stinner7f9b25a2020-11-17 23:28:25 +01001617 .m_slots = signal_slots,
Mohamed Koubaa71d1bd92020-09-03 03:21:06 -05001618};
1619
Victor Stinner4b8032e2020-09-04 14:51:05 +02001620
Mohamed Koubaa71d1bd92020-09-03 03:21:06 -05001621PyMODINIT_FUNC
1622PyInit__signal(void)
1623{
Victor Stinner7f9b25a2020-11-17 23:28:25 +01001624 return PyModuleDef_Init(&signal_module);
Guido van Rossum08c16611997-08-02 03:01:42 +00001625}
1626
Victor Stinner4b8032e2020-09-04 14:51:05 +02001627
Victor Stinner296a7962020-11-17 16:22:23 +01001628void
1629_PySignal_Fini(void)
Guido van Rossum08c16611997-08-02 03:01:42 +00001630{
Victor Stinner0ae323b2020-11-17 18:15:20 +01001631 // Restore default signals and clear handlers
1632 for (int signum = 1; signum < NSIG; signum++) {
1633 PyObject *func = Handlers[signum].func;
1634 _Py_atomic_store_relaxed(&Handlers[signum].tripped, 0);
1635 Handlers[signum].func = NULL;
1636 if (func != NULL
1637 && func != Py_None
1638 && func != DefaultHandler
1639 && func != IgnoreHandler)
1640 {
1641 PyOS_setsig(signum, SIG_DFL);
1642 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001643 Py_XDECREF(func);
1644 }
Guido van Rossum08c16611997-08-02 03:01:42 +00001645
Victor Stinner0ae323b2020-11-17 18:15:20 +01001646#ifdef MS_WINDOWS
1647 if (sigint_event != NULL) {
1648 CloseHandle(sigint_event);
1649 sigint_event = NULL;
1650 }
1651#endif
1652
Serhiy Storchaka505ff752014-02-09 13:33:53 +02001653 Py_CLEAR(DefaultHandler);
1654 Py_CLEAR(IgnoreHandler);
Victor Stinner0ae323b2020-11-17 18:15:20 +01001655#if defined(HAVE_GETITIMER) || defined(HAVE_SETITIMER)
animalize77643c42019-09-09 21:46:26 +08001656 Py_CLEAR(ItimerError);
1657#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001658}
1659
Barry Warsaw92971171997-01-03 00:14:25 +00001660
Barry Warsaw92971171997-01-03 00:14:25 +00001661/* Declared in pyerrors.h */
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001662int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001663PyErr_CheckSignals(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001664{
Victor Stinner72818982020-03-26 22:28:11 +01001665 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerb54a99d2020-04-08 23:35:05 +02001666 if (!_Py_ThreadCanHandleSignals(tstate->interp)) {
Eric Snow64d6cc82019-02-23 15:40:43 -07001667 return 0;
1668 }
1669
Victor Stinner72818982020-03-26 22:28:11 +01001670 return _PyErr_CheckSignalsTstate(tstate);
Eric Snow64d6cc82019-02-23 15:40:43 -07001671}
1672
1673
1674/* Declared in cpython/pyerrors.h */
1675int
Victor Stinner72818982020-03-26 22:28:11 +01001676_PyErr_CheckSignalsTstate(PyThreadState *tstate)
Eric Snow64d6cc82019-02-23 15:40:43 -07001677{
Victor Stinner72818982020-03-26 22:28:11 +01001678 if (!_Py_atomic_load(&is_tripped)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001679 return 0;
Victor Stinner72818982020-03-26 22:28:11 +01001680 }
Christian Heimesb76922a2007-12-11 01:06:40 +00001681
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001682 /*
1683 * The is_tripped variable is meant to speed up the calls to
1684 * PyErr_CheckSignals (both directly or via pending calls) when no
1685 * signal has arrived. This variable is set to 1 when a signal arrives
1686 * and it is set to 0 here, when we know some signals arrived. This way
1687 * we can run the registered handlers with no signals blocked.
1688 *
1689 * NOTE: with this approach we can have a situation where is_tripped is
1690 * 1 but we have no more signals to handle (Handlers[i].tripped
1691 * is 0 for every signal i). This won't do us any harm (except
1692 * we're gonna spent some cycles for nothing). This happens when
1693 * we receive a signal i after we zero is_tripped and before we
1694 * check Handlers[i].tripped.
1695 */
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001696 _Py_atomic_store(&is_tripped, 0);
Christian Heimesb76922a2007-12-11 01:06:40 +00001697
Victor Stinner72818982020-03-26 22:28:11 +01001698 PyObject *frame = (PyObject *)tstate->frame;
1699 if (!frame) {
1700 frame = Py_None;
1701 }
Christian Heimesb76922a2007-12-11 01:06:40 +00001702
Victor Stinner72818982020-03-26 22:28:11 +01001703 for (int i = 1; i < NSIG; i++) {
1704 if (!_Py_atomic_load_relaxed(&Handlers[i].tripped)) {
1705 continue;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001706 }
Victor Stinner72818982020-03-26 22:28:11 +01001707 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
1708
1709 PyObject *arglist = Py_BuildValue("(iO)", i, frame);
1710 PyObject *result;
1711 if (arglist) {
1712 result = _PyObject_Call(tstate, Handlers[i].func, arglist, NULL);
1713 Py_DECREF(arglist);
1714 }
1715 else {
1716 result = NULL;
1717 }
1718 if (!result) {
1719 /* On error, re-schedule a call to _PyErr_CheckSignalsTstate() */
1720 _Py_atomic_store(&is_tripped, 1);
1721 return -1;
1722 }
1723
1724 Py_DECREF(result);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001725 }
Christian Heimesb76922a2007-12-11 01:06:40 +00001726
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001727 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001728}
1729
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +00001730
Victor Stinner72818982020-03-26 22:28:11 +01001731
1732int
1733_PyErr_CheckSignals(void)
1734{
1735 PyThreadState *tstate = _PyThreadState_GET();
1736 return _PyErr_CheckSignalsTstate(tstate);
1737}
1738
1739
Matěj Cepl608876b2019-05-23 22:30:00 +02001740/* Simulate the effect of a signal.SIGINT signal arriving. The next time
1741 PyErr_CheckSignals is called, the Python SIGINT signal handler will be
1742 raised.
1743
1744 Missing signal handler for the SIGINT signal is silently ignored. */
Barry Warsaw92971171997-01-03 00:14:25 +00001745void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001746PyErr_SetInterrupt(void)
Barry Warsaw92971171997-01-03 00:14:25 +00001747{
Matěj Cepl608876b2019-05-23 22:30:00 +02001748 if ((Handlers[SIGINT].func != IgnoreHandler) &&
1749 (Handlers[SIGINT].func != DefaultHandler)) {
1750 trip_signal(SIGINT);
1751 }
Barry Warsaw92971171997-01-03 00:14:25 +00001752}
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001753
Victor Stinner0ae323b2020-11-17 18:15:20 +01001754static int
1755signal_install_handlers(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001756{
Victor Stinner296a7962020-11-17 16:22:23 +01001757#ifdef SIGPIPE
1758 PyOS_setsig(SIGPIPE, SIG_IGN);
1759#endif
1760#ifdef SIGXFZ
1761 PyOS_setsig(SIGXFZ, SIG_IGN);
1762#endif
1763#ifdef SIGXFSZ
1764 PyOS_setsig(SIGXFSZ, SIG_IGN);
1765#endif
1766
1767 // Import _signal to install the Python SIGINT handler
1768 PyObject *module = PyImport_ImportModule("_signal");
1769 if (!module) {
1770 return -1;
1771 }
1772 Py_DECREF(module);
1773
1774 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001775}
1776
Victor Stinnerfa7ab6a2020-06-03 14:39:59 +02001777
Victor Stinner29aa6242020-11-17 22:55:30 +01001778/* Restore signals that the interpreter has called SIG_IGN on to SIG_DFL.
1779 *
1780 * All of the code in this function must only use async-signal-safe functions,
1781 * listed at `man 7 signal` or
1782 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
1783 *
1784 * If this function is updated, update also _posix_spawn() of subprocess.py.
1785 */
1786void
1787_Py_RestoreSignals(void)
1788{
1789#ifdef SIGPIPE
1790 PyOS_setsig(SIGPIPE, SIG_DFL);
1791#endif
1792#ifdef SIGXFZ
1793 PyOS_setsig(SIGXFZ, SIG_DFL);
1794#endif
1795#ifdef SIGXFSZ
1796 PyOS_setsig(SIGXFSZ, SIG_DFL);
1797#endif
1798}
1799
1800
Victor Stinner0ae323b2020-11-17 18:15:20 +01001801int
1802_PySignal_Init(int install_signal_handlers)
1803{
1804 DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL);
1805 if (!DefaultHandler) {
1806 return -1;
1807 }
1808
1809 IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN);
1810 if (!IgnoreHandler) {
1811 return -1;
1812 }
1813
1814#if defined(HAVE_GETITIMER) || defined(HAVE_SETITIMER)
1815 ItimerError = PyErr_NewException("signal.ItimerError",
1816 PyExc_OSError, NULL);
1817 if (!ItimerError) {
1818 return -1;
1819 }
1820#endif
1821
1822#ifdef MS_WINDOWS
1823 /* Create manual-reset event, initially unset */
1824 sigint_event = CreateEvent(NULL, TRUE, FALSE, FALSE);
1825 if (sigint_event == NULL) {
1826 PyErr_SetFromWindowsErr(0);
1827 return -1;
1828 }
1829#endif
1830
1831#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
1832 if (SiginfoType.tp_name == NULL) {
1833 if (PyStructSequence_InitType2(&SiginfoType, &struct_siginfo_desc) < 0) {
1834 return -1;
1835 }
1836 }
1837#endif
1838
1839 for (int signum = 1; signum < NSIG; signum++) {
1840 _Py_atomic_store_relaxed(&Handlers[signum].tripped, 0);
1841 }
1842
1843 if (install_signal_handlers) {
1844 if (signal_install_handlers() < 0) {
1845 return -1;
1846 }
1847 }
1848
1849 return 0;
1850}
1851
1852
Victor Stinnerfa7ab6a2020-06-03 14:39:59 +02001853// The caller doesn't have to hold the GIL
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001854int
Victor Stinnerfa7ab6a2020-06-03 14:39:59 +02001855_PyOS_InterruptOccurred(PyThreadState *tstate)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001856{
Victor Stinnercbe12962020-06-01 20:34:15 +02001857 _Py_EnsureTstateNotNULL(tstate);
1858 if (!_Py_ThreadCanHandleSignals(tstate->interp)) {
Victor Stinner72818982020-03-26 22:28:11 +01001859 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001860 }
Victor Stinner72818982020-03-26 22:28:11 +01001861
1862 if (!_Py_atomic_load_relaxed(&Handlers[SIGINT].tripped)) {
1863 return 0;
1864 }
1865
1866 _Py_atomic_store_relaxed(&Handlers[SIGINT].tripped, 0);
1867 return 1;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001868}
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001869
Victor Stinner26881c82020-06-02 15:51:37 +02001870
Victor Stinnerfa7ab6a2020-06-03 14:39:59 +02001871// The caller must to hold the GIL
1872int
1873PyOS_InterruptOccurred(void)
1874{
1875 PyThreadState *tstate = _PyThreadState_GET();
1876 return _PyOS_InterruptOccurred(tstate);
1877}
1878
1879
Victor Stinner26881c82020-06-02 15:51:37 +02001880#ifdef HAVE_FORK
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001881static void
1882_clear_pending_signals(void)
1883{
Victor Stinner26881c82020-06-02 15:51:37 +02001884 if (!_Py_atomic_load(&is_tripped)) {
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001885 return;
Victor Stinner26881c82020-06-02 15:51:37 +02001886 }
1887
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001888 _Py_atomic_store(&is_tripped, 0);
Victor Stinner26881c82020-06-02 15:51:37 +02001889 for (int i = 1; i < NSIG; ++i) {
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001890 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001891 }
1892}
1893
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001894void
Antoine Pitrou346cbd32017-05-27 17:50:54 +02001895_PySignal_AfterFork(void)
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001896{
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001897 /* Clear the signal flags after forking so that they aren't handled
1898 * in both processes if they came in just before the fork() but before
1899 * the interpreter had an opportunity to call the handlers. issue9535. */
1900 _clear_pending_signals();
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001901}
Victor Stinner26881c82020-06-02 15:51:37 +02001902#endif /* HAVE_FORK */
1903
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001904
1905int
1906_PyOS_IsMainThread(void)
1907{
Victor Stinner81a7be32020-04-14 15:14:01 +02001908 PyInterpreterState *interp = _PyInterpreterState_GET();
Victor Stinnerb54a99d2020-04-08 23:35:05 +02001909 return _Py_ThreadCanHandleSignals(interp);
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001910}
1911
1912#ifdef MS_WINDOWS
1913void *_PyOS_SigintEvent(void)
1914{
1915 /* Returns a manual-reset event which gets tripped whenever
1916 SIGINT is received.
1917
1918 Python.h does not include windows.h so we do cannot use HANDLE
1919 as the return type of this function. We use void* instead. */
1920 return sigint_event;
1921}
1922#endif