blob: acaaafe89d1242cda2ac082d52cba8ee5d92342e [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;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000133
Antoine Pitrou6dd381e2011-11-21 21:26:56 +0100134#ifdef MS_WINDOWS
135static HANDLE sigint_event = NULL;
136#endif
137
Victor Stinner0ae323b2020-11-17 18:15:20 +0100138#if defined(HAVE_GETITIMER) || defined(HAVE_SETITIMER)
Martin v. Löwis823725e2008-03-24 13:39:54 +0000139static PyObject *ItimerError;
Victor Stinner0ae323b2020-11-17 18:15:20 +0100140#endif
Martin v. Löwis823725e2008-03-24 13:39:54 +0000141
Victor Stinner0ae323b2020-11-17 18:15:20 +0100142#ifdef HAVE_GETITIMER
Victor Stinneref611c92017-10-13 13:49:43 -0700143/* auxiliary functions for setitimer */
144static int
145timeval_from_double(PyObject *obj, struct timeval *tv)
Martin v. Löwis823725e2008-03-24 13:39:54 +0000146{
Victor Stinneref611c92017-10-13 13:49:43 -0700147 if (obj == NULL) {
148 tv->tv_sec = 0;
149 tv->tv_usec = 0;
150 return 0;
Antoine Pitrou729780a2017-06-30 10:01:05 +0200151 }
Victor Stinneref611c92017-10-13 13:49:43 -0700152
153 _PyTime_t t;
154 if (_PyTime_FromSecondsObject(&t, obj, _PyTime_ROUND_CEILING) < 0) {
155 return -1;
156 }
157 return _PyTime_AsTimeval(t, tv, _PyTime_ROUND_CEILING);
Martin v. Löwis823725e2008-03-24 13:39:54 +0000158}
159
Christian Heimes1a8501c2008-10-02 19:56:01 +0000160Py_LOCAL_INLINE(double)
Martin v. Löwis823725e2008-03-24 13:39:54 +0000161double_from_timeval(struct timeval *tv)
162{
163 return tv->tv_sec + (double)(tv->tv_usec / 1000000.0);
164}
165
166static PyObject *
167itimer_retval(struct itimerval *iv)
168{
169 PyObject *r, *v;
170
171 r = PyTuple_New(2);
172 if (r == NULL)
Martin Panter6d57fe12016-09-17 03:26:16 +0000173 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000174
175 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_value)))) {
Martin Panter6d57fe12016-09-17 03:26:16 +0000176 Py_DECREF(r);
177 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000178 }
179
180 PyTuple_SET_ITEM(r, 0, v);
181
182 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_interval)))) {
Martin Panter6d57fe12016-09-17 03:26:16 +0000183 Py_DECREF(r);
184 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000185 }
186
187 PyTuple_SET_ITEM(r, 1, v);
188
189 return r;
190}
191#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000192
Serhiy Storchakab0689ae2020-07-12 19:15:20 +0300193/*[clinic input]
194signal.default_int_handler
195 signalnum: int
196 frame: object
197 /
198
199The default handler for SIGINT installed by Python.
200
201It raises KeyboardInterrupt.
202[clinic start generated code]*/
203
Guido van Rossume4485b01994-09-07 14:32:49 +0000204static PyObject *
Serhiy Storchakab0689ae2020-07-12 19:15:20 +0300205signal_default_int_handler_impl(PyObject *module, int signalnum,
206 PyObject *frame)
207/*[clinic end generated code: output=bb11c2eb115ace4e input=efcd4a56a207acfd]*/
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000208{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000209 PyErr_SetNone(PyExc_KeyboardInterrupt);
210 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000211}
212
Thomas Wouters0796b002000-07-22 23:49:30 +0000213
214static int
Victor Stinner11517102014-07-29 23:31:34 +0200215report_wakeup_write_error(void *data)
Antoine Pitrou6f6ec372013-08-17 20:27:56 +0200216{
Eric Snowfdf282d2019-01-11 14:26:55 -0700217 PyObject *exc, *val, *tb;
Antoine Pitrou6f6ec372013-08-17 20:27:56 +0200218 int save_errno = errno;
Benjamin Petersonca470632016-09-06 13:47:26 -0700219 errno = (int) (intptr_t) data;
Eric Snowfdf282d2019-01-11 14:26:55 -0700220 PyErr_Fetch(&exc, &val, &tb);
Antoine Pitrou6f6ec372013-08-17 20:27:56 +0200221 PyErr_SetFromErrno(PyExc_OSError);
222 PySys_WriteStderr("Exception ignored when trying to write to the "
223 "signal wakeup fd:\n");
224 PyErr_WriteUnraisable(NULL);
Eric Snowfdf282d2019-01-11 14:26:55 -0700225 PyErr_Restore(exc, val, tb);
Antoine Pitrou6f6ec372013-08-17 20:27:56 +0200226 errno = save_errno;
227 return 0;
228}
229
Victor Stinner11517102014-07-29 23:31:34 +0200230#ifdef MS_WINDOWS
231static int
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800232report_wakeup_send_error(void* data)
Victor Stinner11517102014-07-29 23:31:34 +0200233{
Eric Snowfdf282d2019-01-11 14:26:55 -0700234 PyObject *exc, *val, *tb;
235 PyErr_Fetch(&exc, &val, &tb);
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800236 /* PyErr_SetExcFromWindowsErr() invokes FormatMessage() which
237 recognizes the error codes used by both GetLastError() and
238 WSAGetLastError */
239 PyErr_SetExcFromWindowsErr(PyExc_OSError, (int) (intptr_t) data);
Victor Stinner11517102014-07-29 23:31:34 +0200240 PySys_WriteStderr("Exception ignored when trying to send to the "
241 "signal wakeup fd:\n");
242 PyErr_WriteUnraisable(NULL);
Eric Snowfdf282d2019-01-11 14:26:55 -0700243 PyErr_Restore(exc, val, tb);
Victor Stinner11517102014-07-29 23:31:34 +0200244 return 0;
245}
246#endif /* MS_WINDOWS */
247
Tim Peters4f1b2082000-07-23 21:18:09 +0000248static void
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200249trip_signal(int sig_num)
250{
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +0200251 _Py_atomic_store_relaxed(&Handlers[sig_num].tripped, 1);
Victor Stinner11517102014-07-29 23:31:34 +0200252
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200253 /* Set is_tripped after setting .tripped, as it gets
254 cleared in PyErr_CheckSignals() before .tripped. */
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +0200255 _Py_atomic_store(&is_tripped, 1);
256
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200257 /* Signals are always handled by the main interpreter */
258 PyInterpreterState *interp = _PyRuntime.interpreters.main;
Victor Stinner8849e592020-03-18 19:28:53 +0100259
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +0200260 /* Notify ceval.c */
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200261 _PyEval_SignalReceived(interp);
Nathaniel J. Smith4ae01492017-05-16 14:12:11 -0700262
263 /* And then write to the wakeup fd *after* setting all the globals and
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200264 doing the _PyEval_SignalReceived. We used to write to the wakeup fd
265 and then set the flag, but this allowed the following sequence of events
266 (especially on windows, where trip_signal may run in a new thread):
Nathaniel J. Smith4ae01492017-05-16 14:12:11 -0700267
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800268 - main thread blocks on select([wakeup.fd], ...)
Nathaniel J. Smith4ae01492017-05-16 14:12:11 -0700269 - signal arrives
270 - trip_signal writes to the wakeup fd
271 - the main thread wakes up
272 - the main thread checks the signal flags, sees that they're unset
273 - the main thread empties the wakeup fd
274 - the main thread goes back to sleep
275 - trip_signal sets the flags to request the Python-level signal handler
276 be run
277 - the main thread doesn't notice, because it's asleep
278
279 See bpo-30038 for more details.
280 */
281
Victor Stinnercda23be2020-11-17 18:57:32 +0100282 int fd;
Victor Stinner11517102014-07-29 23:31:34 +0200283#ifdef MS_WINDOWS
284 fd = Py_SAFE_DOWNCAST(wakeup.fd, SOCKET_T, int);
285#else
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800286 fd = wakeup.fd;
Victor Stinner11517102014-07-29 23:31:34 +0200287#endif
288
289 if (fd != INVALID_FD) {
Victor Stinnercda23be2020-11-17 18:57:32 +0100290 unsigned char byte = (unsigned char)sig_num;
Victor Stinner11517102014-07-29 23:31:34 +0200291#ifdef MS_WINDOWS
292 if (wakeup.use_send) {
Victor Stinnercda23be2020-11-17 18:57:32 +0100293 Py_ssize_t rc = send(fd, &byte, 1, 0);
Victor Stinner11517102014-07-29 23:31:34 +0200294
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800295 if (rc < 0) {
296 int last_error = GetLastError();
297 if (wakeup.warn_on_full_buffer ||
298 last_error != WSAEWOULDBLOCK)
299 {
Victor Stinner50e6e992020-03-19 02:41:21 +0100300 /* _PyEval_AddPendingCall() isn't signal-safe, but we
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800301 still use it for this exceptional case. */
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200302 _PyEval_AddPendingCall(interp,
Victor Stinner09532fe2019-05-10 23:39:09 +0200303 report_wakeup_send_error,
304 (void *)(intptr_t) last_error);
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800305 }
Victor Stinner11517102014-07-29 23:31:34 +0200306 }
307 }
308 else
309#endif
310 {
Victor Stinnere72fe392015-04-01 18:35:22 +0200311 /* _Py_write_noraise() retries write() if write() is interrupted by
312 a signal (fails with EINTR). */
Victor Stinnercda23be2020-11-17 18:57:32 +0100313 Py_ssize_t rc = _Py_write_noraise(fd, &byte, 1);
Victor Stinner11517102014-07-29 23:31:34 +0200314
315 if (rc < 0) {
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800316 if (wakeup.warn_on_full_buffer ||
317 (errno != EWOULDBLOCK && errno != EAGAIN))
318 {
Victor Stinner50e6e992020-03-19 02:41:21 +0100319 /* _PyEval_AddPendingCall() isn't signal-safe, but we
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800320 still use it for this exceptional case. */
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200321 _PyEval_AddPendingCall(interp,
Victor Stinner09532fe2019-05-10 23:39:09 +0200322 report_wakeup_write_error,
323 (void *)(intptr_t)errno);
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800324 }
Victor Stinner11517102014-07-29 23:31:34 +0200325 }
326 }
Victor Stinnerc13ef662011-05-25 02:35:58 +0200327 }
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200328}
329
330static void
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000331signal_handler(int sig_num)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000332{
Antoine Pitrou39a65912010-11-05 19:47:27 +0000333 int save_errno = errno;
334
Jeroen Demeyerd237b3f2019-05-10 03:28:57 +0200335 trip_signal(sig_num);
Antoine Pitrou39a65912010-11-05 19:47:27 +0000336
Jean-Paul Calderone6f137ca2010-05-09 03:18:57 +0000337#ifndef HAVE_SIGACTION
Antoine Pitrou39a65912010-11-05 19:47:27 +0000338#ifdef SIGCHLD
339 /* To avoid infinite recursion, this signal remains
340 reset until explicit re-instated.
341 Don't clear the 'func' field as it is our pointer
342 to the Python handler... */
343 if (sig_num != SIGCHLD)
344#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000345 /* If the handler was not set up with sigaction, reinstall it. See
Nick Coghland6009512014-11-20 21:39:37 +1000346 * Python/pylifecycle.c for the implementation of PyOS_setsig which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000347 * makes this true. See also issue8354. */
348 PyOS_setsig(sig_num, signal_handler);
Jean-Paul Calderone6f137ca2010-05-09 03:18:57 +0000349#endif
Antoine Pitrou39a65912010-11-05 19:47:27 +0000350
351 /* Issue #10311: asynchronously executing signal handlers should not
352 mutate errno under the feet of unsuspecting C code. */
353 errno = save_errno;
Antoine Pitrou6dd381e2011-11-21 21:26:56 +0100354
355#ifdef MS_WINDOWS
356 if (sig_num == SIGINT)
357 SetEvent(sigint_event);
358#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000359}
Guido van Rossume4485b01994-09-07 14:32:49 +0000360
Guido van Rossum06d511d1995-03-10 15:13:48 +0000361
Guido van Rossum1171ee61997-08-22 20:42:00 +0000362#ifdef HAVE_ALARM
Tal Einatc7027b72015-05-16 14:14:49 +0300363
364/*[clinic input]
365signal.alarm -> long
366
367 seconds: int
368 /
369
370Arrange for SIGALRM to arrive after the given number of seconds.
371[clinic start generated code]*/
372
373static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300374signal_alarm_impl(PyObject *module, int seconds)
375/*[clinic end generated code: output=144232290814c298 input=0d5e97e0e6f39e86]*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000376{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000377 /* alarm() returns the number of seconds remaining */
Tal Einatc7027b72015-05-16 14:14:49 +0300378 return (long)alarm(seconds);
Guido van Rossumaa0f4c71994-08-23 13:49:37 +0000379}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000380
Guido van Rossum06d511d1995-03-10 15:13:48 +0000381#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000382
Guido van Rossum1171ee61997-08-22 20:42:00 +0000383#ifdef HAVE_PAUSE
Tal Einatc7027b72015-05-16 14:14:49 +0300384
385/*[clinic input]
386signal.pause
387
388Wait until a signal arrives.
389[clinic start generated code]*/
390
Guido van Rossuma597dde1995-01-10 20:56:29 +0000391static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300392signal_pause_impl(PyObject *module)
393/*[clinic end generated code: output=391656788b3c3929 input=f03de0f875752062]*/
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000394{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000395 Py_BEGIN_ALLOW_THREADS
396 (void)pause();
397 Py_END_ALLOW_THREADS
398 /* make sure that any exceptions that got raised are propagated
399 * back into Python
400 */
401 if (PyErr_CheckSignals())
402 return NULL;
Barry Warsaw92971171997-01-03 00:14:25 +0000403
Tal Einatc7027b72015-05-16 14:14:49 +0300404 Py_RETURN_NONE;
Guido van Rossume4485b01994-09-07 14:32:49 +0000405}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000406
Guido van Rossum06d511d1995-03-10 15:13:48 +0000407#endif
Guido van Rossume4485b01994-09-07 14:32:49 +0000408
Vladimir Matveevc24c6c22019-01-08 01:58:25 -0800409/*[clinic input]
410signal.raise_signal
411
412 signalnum: int
413 /
414
415Send a signal to the executing process.
416[clinic start generated code]*/
417
418static PyObject *
419signal_raise_signal_impl(PyObject *module, int signalnum)
420/*[clinic end generated code: output=e2b014220aa6111d input=e90c0f9a42358de6]*/
421{
422 int err;
423 Py_BEGIN_ALLOW_THREADS
424 _Py_BEGIN_SUPPRESS_IPH
425 err = raise(signalnum);
426 _Py_END_SUPPRESS_IPH
427 Py_END_ALLOW_THREADS
Victor Stinner09532fe2019-05-10 23:39:09 +0200428
Vladimir Matveevc24c6c22019-01-08 01:58:25 -0800429 if (err) {
430 return PyErr_SetFromErrno(PyExc_OSError);
431 }
432 Py_RETURN_NONE;
433}
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000434
Tal Einatc7027b72015-05-16 14:14:49 +0300435/*[clinic input]
436signal.signal
437
438 signalnum: int
439 handler: object
440 /
441
442Set the action for the given signal.
443
444The action can be SIG_DFL, SIG_IGN, or a callable Python object.
445The previous action is returned. See getsignal() for possible return values.
446
447*** IMPORTANT NOTICE ***
448A signal handler function is called with two arguments:
449the first is the signal number, the second is the interrupted stack frame.
450[clinic start generated code]*/
451
Guido van Rossume4485b01994-09-07 14:32:49 +0000452static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300453signal_signal_impl(PyObject *module, int signalnum, PyObject *handler)
454/*[clinic end generated code: output=b44cfda43780f3a1 input=deee84af5fa0432c]*/
Guido van Rossume4485b01994-09-07 14:32:49 +0000455{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000456 PyObject *old_handler;
457 void (*func)(int);
Brian Curtinef9efbd2010-08-06 19:27:32 +0000458#ifdef MS_WINDOWS
Tal Einatc7027b72015-05-16 14:14:49 +0300459 /* Validate that signalnum is one of the allowable signals */
460 switch (signalnum) {
Brian Curtinc734b312010-09-06 16:04:10 +0000461 case SIGABRT: break;
Brian Curtin9e88b5a2010-10-01 14:49:24 +0000462#ifdef SIGBREAK
463 /* Issue #10003: SIGBREAK is not documented as permitted, but works
464 and corresponds to CTRL_BREAK_EVENT. */
465 case SIGBREAK: break;
466#endif
Brian Curtinc734b312010-09-06 16:04:10 +0000467 case SIGFPE: break;
468 case SIGILL: break;
469 case SIGINT: break;
470 case SIGSEGV: break;
471 case SIGTERM: break;
472 default:
473 PyErr_SetString(PyExc_ValueError, "invalid signal value");
474 return NULL;
Brian Curtinef9efbd2010-08-06 19:27:32 +0000475 }
476#endif
Victor Stinnerd8613dc2019-05-24 13:43:55 +0200477
Victor Stinner72818982020-03-26 22:28:11 +0100478 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200479 if (!_Py_ThreadCanHandleSignals(tstate->interp)) {
Victor Stinner72818982020-03-26 22:28:11 +0100480 _PyErr_SetString(tstate, PyExc_ValueError,
481 "signal only works in main thread "
482 "of the main interpreter");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000483 return NULL;
484 }
Tal Einatc7027b72015-05-16 14:14:49 +0300485 if (signalnum < 1 || signalnum >= NSIG) {
Victor Stinner72818982020-03-26 22:28:11 +0100486 _PyErr_SetString(tstate, PyExc_ValueError,
487 "signal number out of range");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000488 return NULL;
489 }
Victor Stinner72818982020-03-26 22:28:11 +0100490 if (handler == IgnoreHandler) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000491 func = SIG_IGN;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000492 }
Victor Stinner72818982020-03-26 22:28:11 +0100493 else if (handler == DefaultHandler) {
494 func = SIG_DFL;
495 }
496 else if (!PyCallable_Check(handler)) {
497 _PyErr_SetString(tstate, PyExc_TypeError,
498 "signal handler must be signal.SIG_IGN, "
499 "signal.SIG_DFL, or a callable object");
500 return NULL;
501 }
502 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503 func = signal_handler;
Victor Stinner72818982020-03-26 22:28:11 +0100504 }
505
Antoine Pitrouf6f90ff2017-11-03 19:58:46 +0100506 /* Check for pending signals before changing signal handler */
Victor Stinner72818982020-03-26 22:28:11 +0100507 if (_PyErr_CheckSignalsTstate(tstate)) {
Antoine Pitrouf6f90ff2017-11-03 19:58:46 +0100508 return NULL;
509 }
Tal Einatc7027b72015-05-16 14:14:49 +0300510 if (PyOS_setsig(signalnum, func) == SIG_ERR) {
Victor Stinner388196e2011-05-10 17:13:00 +0200511 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000512 return NULL;
513 }
Victor Stinner72818982020-03-26 22:28:11 +0100514
Tal Einatc7027b72015-05-16 14:14:49 +0300515 old_handler = Handlers[signalnum].func;
Victor Stinnercda23be2020-11-17 18:57:32 +0100516 Handlers[signalnum].func = Py_NewRef(handler);
Victor Stinner72818982020-03-26 22:28:11 +0100517
518 if (old_handler != NULL) {
Antoine Pitrouc8c952c2013-05-04 23:16:59 +0200519 return old_handler;
Victor Stinner72818982020-03-26 22:28:11 +0100520 }
521 else {
Antoine Pitrouc8c952c2013-05-04 23:16:59 +0200522 Py_RETURN_NONE;
Victor Stinner72818982020-03-26 22:28:11 +0100523 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000524}
525
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000526
Tal Einatc7027b72015-05-16 14:14:49 +0300527/*[clinic input]
528signal.getsignal
529
530 signalnum: int
531 /
532
533Return the current action for the given signal.
534
535The return value can be:
536 SIG_IGN -- if the signal is being ignored
537 SIG_DFL -- if the default action for the signal is in effect
538 None -- if an unknown handler is in effect
539 anything else -- the callable Python object used as a handler
540[clinic start generated code]*/
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000541
Guido van Rossume4485b01994-09-07 14:32:49 +0000542static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300543signal_getsignal_impl(PyObject *module, int signalnum)
544/*[clinic end generated code: output=35b3e0e796fd555e input=ac23a00f19dfa509]*/
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000545{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000546 PyObject *old_handler;
Tal Einatc7027b72015-05-16 14:14:49 +0300547 if (signalnum < 1 || signalnum >= NSIG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000548 PyErr_SetString(PyExc_ValueError,
549 "signal number out of range");
550 return NULL;
551 }
Tal Einatc7027b72015-05-16 14:14:49 +0300552 old_handler = Handlers[signalnum].func;
Antoine Pitrouc8c952c2013-05-04 23:16:59 +0200553 if (old_handler != NULL) {
Victor Stinnercda23be2020-11-17 18:57:32 +0100554 return Py_NewRef(old_handler);
Antoine Pitrouc8c952c2013-05-04 23:16:59 +0200555 }
556 else {
557 Py_RETURN_NONE;
558 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000559}
560
Antoine Pietri5d2a27d2018-03-12 14:42:34 +0100561
562/*[clinic input]
563signal.strsignal
564
565 signalnum: int
566 /
567
568Return the system description of the given signal.
569
570The return values can be such as "Interrupt", "Segmentation fault", etc.
571Returns None if the signal is not recognized.
572[clinic start generated code]*/
573
574static PyObject *
575signal_strsignal_impl(PyObject *module, int signalnum)
576/*[clinic end generated code: output=44e12e1e3b666261 input=b77914b03f856c74]*/
577{
578 char *res;
579
580 if (signalnum < 1 || signalnum >= NSIG) {
581 PyErr_SetString(PyExc_ValueError,
582 "signal number out of range");
583 return NULL;
584 }
585
Michael Osipov48ce4892018-08-23 15:27:19 +0200586#ifndef HAVE_STRSIGNAL
Antoine Pietri5d2a27d2018-03-12 14:42:34 +0100587 switch (signalnum) {
Michael Osipov48ce4892018-08-23 15:27:19 +0200588 /* Though being a UNIX, HP-UX does not provide strsignal(3). */
589#ifndef MS_WINDOWS
590 case SIGHUP:
591 res = "Hangup";
592 break;
593 case SIGALRM:
594 res = "Alarm clock";
595 break;
596 case SIGPIPE:
597 res = "Broken pipe";
598 break;
599 case SIGQUIT:
600 res = "Quit";
601 break;
602 case SIGCHLD:
603 res = "Child exited";
604 break;
605#endif
606 /* Custom redefinition of POSIX signals allowed on Windows. */
Antoine Pietri5d2a27d2018-03-12 14:42:34 +0100607 case SIGINT:
608 res = "Interrupt";
609 break;
610 case SIGILL:
611 res = "Illegal instruction";
612 break;
613 case SIGABRT:
614 res = "Aborted";
615 break;
616 case SIGFPE:
617 res = "Floating point exception";
618 break;
619 case SIGSEGV:
620 res = "Segmentation fault";
621 break;
622 case SIGTERM:
623 res = "Terminated";
624 break;
625 default:
626 Py_RETURN_NONE;
627 }
628#else
629 errno = 0;
630 res = strsignal(signalnum);
631
632 if (errno || res == NULL || strstr(res, "Unknown signal") != NULL)
633 Py_RETURN_NONE;
634#endif
635
636 return Py_BuildValue("s", res);
637}
638
Christian Heimes8640e742008-02-23 16:23:06 +0000639#ifdef HAVE_SIGINTERRUPT
Tal Einatc7027b72015-05-16 14:14:49 +0300640
641/*[clinic input]
642signal.siginterrupt
643
644 signalnum: int
645 flag: int
646 /
647
648Change system call restart behaviour.
649
650If flag is False, system calls will be restarted when interrupted by
651signal sig, else system calls will be interrupted.
652[clinic start generated code]*/
Christian Heimes8640e742008-02-23 16:23:06 +0000653
654static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300655signal_siginterrupt_impl(PyObject *module, int signalnum, int flag)
656/*[clinic end generated code: output=063816243d85dd19 input=4160acacca3e2099]*/
Christian Heimes8640e742008-02-23 16:23:06 +0000657{
Tal Einatc7027b72015-05-16 14:14:49 +0300658 if (signalnum < 1 || signalnum >= NSIG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000659 PyErr_SetString(PyExc_ValueError,
660 "signal number out of range");
661 return NULL;
662 }
Pablo Galindof9c5e3f2020-09-02 15:29:12 +0100663#ifdef HAVE_SIGACTION
664 struct sigaction act;
665 (void) sigaction(signalnum, NULL, &act);
666 if (flag) {
667 act.sa_flags &= ~SA_RESTART;
668 }
669 else {
670 act.sa_flags |= SA_RESTART;
671 }
672 if (sigaction(signalnum, &act, NULL) < 0) {
673#else
674 if (siginterrupt(signalnum, flag) < 0) {
675#endif
Victor Stinner388196e2011-05-10 17:13:00 +0200676 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000677 return NULL;
678 }
Tal Einatc7027b72015-05-16 14:14:49 +0300679 Py_RETURN_NONE;
Christian Heimes8640e742008-02-23 16:23:06 +0000680}
681
682#endif
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000683
Tal Einatc7027b72015-05-16 14:14:49 +0300684
685static PyObject*
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800686signal_set_wakeup_fd(PyObject *self, PyObject *args, PyObject *kwds)
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000687{
Victor Stinnere134a7f2015-03-30 10:09:31 +0200688 struct _Py_stat_struct status;
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800689 static char *kwlist[] = {
690 "", "warn_on_full_buffer", NULL,
691 };
692 int warn_on_full_buffer = 1;
Victor Stinner11517102014-07-29 23:31:34 +0200693#ifdef MS_WINDOWS
694 PyObject *fdobj;
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100695 SOCKET_T sockfd, old_sockfd;
Victor Stinner11517102014-07-29 23:31:34 +0200696 int res;
697 int res_size = sizeof res;
698 PyObject *mod;
Victor Stinner11517102014-07-29 23:31:34 +0200699 int is_socket;
700
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800701 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|$p:set_wakeup_fd", kwlist,
702 &fdobj, &warn_on_full_buffer))
Victor Stinner11517102014-07-29 23:31:34 +0200703 return NULL;
704
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100705 sockfd = PyLong_AsSocket_t(fdobj);
706 if (sockfd == (SOCKET_T)(-1) && PyErr_Occurred())
Victor Stinner11517102014-07-29 23:31:34 +0200707 return NULL;
708#else
Victor Stinnercda23be2020-11-17 18:57:32 +0100709 int fd;
Victor Stinner11517102014-07-29 23:31:34 +0200710
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800711 if (!PyArg_ParseTupleAndKeywords(args, kwds, "i|$p:set_wakeup_fd", kwlist,
712 &fd, &warn_on_full_buffer))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000713 return NULL;
Victor Stinner11517102014-07-29 23:31:34 +0200714#endif
715
Victor Stinner72818982020-03-26 22:28:11 +0100716 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200717 if (!_Py_ThreadCanHandleSignals(tstate->interp)) {
Victor Stinner72818982020-03-26 22:28:11 +0100718 _PyErr_SetString(tstate, PyExc_ValueError,
719 "set_wakeup_fd only works in main thread "
720 "of the main interpreter");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000721 return NULL;
722 }
Victor Stinner0bffc942014-07-21 16:28:54 +0200723
Victor Stinner11517102014-07-29 23:31:34 +0200724#ifdef MS_WINDOWS
725 is_socket = 0;
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100726 if (sockfd != INVALID_FD) {
Victor Stinner11517102014-07-29 23:31:34 +0200727 /* Import the _socket module to call WSAStartup() */
728 mod = PyImport_ImportModuleNoBlock("_socket");
729 if (mod == NULL)
730 return NULL;
731 Py_DECREF(mod);
732
733 /* test the socket */
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100734 if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR,
Victor Stinner11517102014-07-29 23:31:34 +0200735 (char *)&res, &res_size) != 0) {
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100736 int fd, err;
737
738 err = WSAGetLastError();
Victor Stinner11517102014-07-29 23:31:34 +0200739 if (err != WSAENOTSOCK) {
740 PyErr_SetExcFromWindowsErr(PyExc_OSError, err);
741 return NULL;
742 }
743
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100744 fd = (int)sockfd;
Steve Dower940f33a2016-09-08 11:21:54 -0700745 if ((SOCKET_T)fd != sockfd) {
Victor Stinner72818982020-03-26 22:28:11 +0100746 _PyErr_SetString(tstate, PyExc_ValueError, "invalid fd");
Victor Stinner11517102014-07-29 23:31:34 +0200747 return NULL;
748 }
749
Victor Stinner72818982020-03-26 22:28:11 +0100750 if (_Py_fstat(fd, &status) != 0) {
Victor Stinner11517102014-07-29 23:31:34 +0200751 return NULL;
Victor Stinner72818982020-03-26 22:28:11 +0100752 }
Victor Stinner38227602014-08-27 12:59:44 +0200753
754 /* on Windows, a file cannot be set to non-blocking mode */
Victor Stinner11517102014-07-29 23:31:34 +0200755 }
Victor Stinner38227602014-08-27 12:59:44 +0200756 else {
Victor Stinner11517102014-07-29 23:31:34 +0200757 is_socket = 1;
Victor Stinner38227602014-08-27 12:59:44 +0200758
759 /* Windows does not provide a function to test if a socket
760 is in non-blocking mode */
761 }
Victor Stinner11517102014-07-29 23:31:34 +0200762 }
763
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100764 old_sockfd = wakeup.fd;
765 wakeup.fd = sockfd;
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800766 wakeup.warn_on_full_buffer = warn_on_full_buffer;
Victor Stinner11517102014-07-29 23:31:34 +0200767 wakeup.use_send = is_socket;
768
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100769 if (old_sockfd != INVALID_FD)
770 return PyLong_FromSocket_t(old_sockfd);
Victor Stinner11517102014-07-29 23:31:34 +0200771 else
772 return PyLong_FromLong(-1);
773#else
Victor Stinnerd18ccd12014-07-24 21:58:53 +0200774 if (fd != -1) {
Victor Stinner38227602014-08-27 12:59:44 +0200775 int blocking;
776
Victor Stinnere134a7f2015-03-30 10:09:31 +0200777 if (_Py_fstat(fd, &status) != 0)
Victor Stinner11517102014-07-29 23:31:34 +0200778 return NULL;
Victor Stinner38227602014-08-27 12:59:44 +0200779
780 blocking = _Py_get_blocking(fd);
781 if (blocking < 0)
782 return NULL;
783 if (blocking) {
Victor Stinner72818982020-03-26 22:28:11 +0100784 _PyErr_Format(tstate, PyExc_ValueError,
785 "the fd %i must be in non-blocking mode",
786 fd);
Victor Stinner38227602014-08-27 12:59:44 +0200787 return NULL;
788 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000789 }
Victor Stinner0bffc942014-07-21 16:28:54 +0200790
Victor Stinnercda23be2020-11-17 18:57:32 +0100791 int old_fd = wakeup.fd;
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800792 wakeup.fd = fd;
793 wakeup.warn_on_full_buffer = warn_on_full_buffer;
Victor Stinner0bffc942014-07-21 16:28:54 +0200794
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000795 return PyLong_FromLong(old_fd);
Victor Stinner11517102014-07-29 23:31:34 +0200796#endif
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000797}
798
799PyDoc_STRVAR(set_wakeup_fd_doc,
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800800"set_wakeup_fd(fd, *, warn_on_full_buffer=True) -> fd\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000801\n\
Victor Stinner11517102014-07-29 23:31:34 +0200802Sets the fd to be written to (with the signal number) when a signal\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000803comes in. A library can use this to wakeup select or poll.\n\
Victor Stinner11517102014-07-29 23:31:34 +0200804The previous fd or -1 is returned.\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000805\n\
806The fd must be non-blocking.");
807
808/* C API for the same, without all the error checking */
809int
810PySignal_SetWakeupFd(int fd)
811{
Victor Stinnercda23be2020-11-17 18:57:32 +0100812 if (fd < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000813 fd = -1;
Victor Stinnercda23be2020-11-17 18:57:32 +0100814 }
Victor Stinner11517102014-07-29 23:31:34 +0200815
816#ifdef MS_WINDOWS
Victor Stinnercda23be2020-11-17 18:57:32 +0100817 int old_fd = Py_SAFE_DOWNCAST(wakeup.fd, SOCKET_T, int);
Victor Stinner11517102014-07-29 23:31:34 +0200818#else
Victor Stinnercda23be2020-11-17 18:57:32 +0100819 int old_fd = wakeup.fd;
Victor Stinner11517102014-07-29 23:31:34 +0200820#endif
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800821 wakeup.fd = fd;
822 wakeup.warn_on_full_buffer = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000823 return old_fd;
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000824}
825
826
Martin v. Löwis823725e2008-03-24 13:39:54 +0000827#ifdef HAVE_SETITIMER
Tal Einatc7027b72015-05-16 14:14:49 +0300828
829/*[clinic input]
830signal.setitimer
831
832 which: int
Victor Stinneref611c92017-10-13 13:49:43 -0700833 seconds: object
834 interval: object(c_default="NULL") = 0.0
Tal Einatc7027b72015-05-16 14:14:49 +0300835 /
836
837Sets given itimer (one of ITIMER_REAL, ITIMER_VIRTUAL or ITIMER_PROF).
838
839The timer will fire after value seconds and after that every interval seconds.
840The itimer can be cleared by setting seconds to zero.
841
842Returns old values as a tuple: (delay, interval).
843[clinic start generated code]*/
844
Martin v. Löwis823725e2008-03-24 13:39:54 +0000845static PyObject *
Victor Stinneref611c92017-10-13 13:49:43 -0700846signal_setitimer_impl(PyObject *module, int which, PyObject *seconds,
847 PyObject *interval)
848/*[clinic end generated code: output=65f9dcbddc35527b input=de43daf194e6f66f]*/
Martin v. Löwis823725e2008-03-24 13:39:54 +0000849{
Victor Stinnercda23be2020-11-17 18:57:32 +0100850 struct itimerval new;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000851
Victor Stinneref611c92017-10-13 13:49:43 -0700852 if (timeval_from_double(seconds, &new.it_value) < 0) {
853 return NULL;
854 }
855 if (timeval_from_double(interval, &new.it_interval) < 0) {
856 return NULL;
857 }
858
Martin v. Löwis823725e2008-03-24 13:39:54 +0000859 /* Let OS check "which" value */
Victor Stinnercda23be2020-11-17 18:57:32 +0100860 struct itimerval old;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000861 if (setitimer(which, &new, &old) != 0) {
Tal Einatc7027b72015-05-16 14:14:49 +0300862 PyErr_SetFromErrno(ItimerError);
863 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000864 }
865
866 return itimer_retval(&old);
867}
868
Martin v. Löwis823725e2008-03-24 13:39:54 +0000869#endif
870
871
872#ifdef HAVE_GETITIMER
Tal Einatc7027b72015-05-16 14:14:49 +0300873
874/*[clinic input]
875signal.getitimer
876
877 which: int
878 /
879
880Returns current value of given itimer.
881[clinic start generated code]*/
882
Martin v. Löwis823725e2008-03-24 13:39:54 +0000883static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300884signal_getitimer_impl(PyObject *module, int which)
885/*[clinic end generated code: output=9e053175d517db40 input=f7d21d38f3490627]*/
Martin v. Löwis823725e2008-03-24 13:39:54 +0000886{
Martin v. Löwis823725e2008-03-24 13:39:54 +0000887 struct itimerval old;
888
Martin v. Löwis823725e2008-03-24 13:39:54 +0000889 if (getitimer(which, &old) != 0) {
Tal Einatc7027b72015-05-16 14:14:49 +0300890 PyErr_SetFromErrno(ItimerError);
891 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000892 }
893
894 return itimer_retval(&old);
895}
896
Martin v. Löwis823725e2008-03-24 13:39:54 +0000897#endif
898
Victor Stinnerb3e72192011-05-08 01:46:11 +0200899#if defined(PYPTHREAD_SIGMASK) || defined(HAVE_SIGPENDING)
Victor Stinner35b300c2011-05-04 13:20:35 +0200900static PyObject*
901sigset_to_set(sigset_t mask)
902{
903 PyObject *signum, *result;
904 int sig;
905
906 result = PySet_New(0);
907 if (result == NULL)
908 return NULL;
909
910 for (sig = 1; sig < NSIG; sig++) {
911 if (sigismember(&mask, sig) != 1)
912 continue;
913
914 /* Handle the case where it is a member by adding the signal to
915 the result list. Ignore the other cases because they mean the
916 signal isn't a member of the mask or the signal was invalid,
917 and an invalid signal must have been our fault in constructing
918 the loop boundaries. */
919 signum = PyLong_FromLong(sig);
920 if (signum == NULL) {
921 Py_DECREF(result);
922 return NULL;
923 }
924 if (PySet_Add(result, signum) == -1) {
925 Py_DECREF(signum);
926 Py_DECREF(result);
927 return NULL;
928 }
929 Py_DECREF(signum);
930 }
931 return result;
932}
Victor Stinnerb3e72192011-05-08 01:46:11 +0200933#endif
Victor Stinner35b300c2011-05-04 13:20:35 +0200934
Victor Stinnerb3e72192011-05-08 01:46:11 +0200935#ifdef PYPTHREAD_SIGMASK
Tal Einatc7027b72015-05-16 14:14:49 +0300936
937/*[clinic input]
938signal.pthread_sigmask
939
940 how: int
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300941 mask: sigset_t
Tal Einatc7027b72015-05-16 14:14:49 +0300942 /
943
944Fetch and/or change the signal mask of the calling thread.
945[clinic start generated code]*/
946
Victor Stinnera9293352011-04-30 15:21:58 +0200947static PyObject *
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300948signal_pthread_sigmask_impl(PyObject *module, int how, sigset_t mask)
949/*[clinic end generated code: output=0562c0fb192981a8 input=85bcebda442fa77f]*/
Victor Stinnera9293352011-04-30 15:21:58 +0200950{
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300951 sigset_t previous;
Victor Stinnera9293352011-04-30 15:21:58 +0200952 int err;
953
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300954 err = pthread_sigmask(how, &mask, &previous);
Victor Stinnera9293352011-04-30 15:21:58 +0200955 if (err != 0) {
956 errno = err;
Victor Stinnerb3e72192011-05-08 01:46:11 +0200957 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnera9293352011-04-30 15:21:58 +0200958 return NULL;
959 }
960
Victor Stinnerd0e516d2011-05-03 14:57:12 +0200961 /* if signals was unblocked, signal handlers have been called */
962 if (PyErr_CheckSignals())
963 return NULL;
964
Victor Stinner35b300c2011-05-04 13:20:35 +0200965 return sigset_to_set(previous);
Victor Stinnera9293352011-04-30 15:21:58 +0200966}
967
Victor Stinnera9293352011-04-30 15:21:58 +0200968#endif /* #ifdef PYPTHREAD_SIGMASK */
969
Martin v. Löwis823725e2008-03-24 13:39:54 +0000970
Victor Stinnerb3e72192011-05-08 01:46:11 +0200971#ifdef HAVE_SIGPENDING
Tal Einatc7027b72015-05-16 14:14:49 +0300972
973/*[clinic input]
974signal.sigpending
975
976Examine pending signals.
977
978Returns a set of signal numbers that are pending for delivery to
979the calling thread.
980[clinic start generated code]*/
981
Victor Stinnerb3e72192011-05-08 01:46:11 +0200982static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300983signal_sigpending_impl(PyObject *module)
984/*[clinic end generated code: output=53375ffe89325022 input=e0036c016f874e29]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +0200985{
986 int err;
987 sigset_t mask;
988 err = sigpending(&mask);
989 if (err)
990 return PyErr_SetFromErrno(PyExc_OSError);
991 return sigset_to_set(mask);
992}
993
Victor Stinnerb3e72192011-05-08 01:46:11 +0200994#endif /* #ifdef HAVE_SIGPENDING */
995
996
997#ifdef HAVE_SIGWAIT
Tal Einatc7027b72015-05-16 14:14:49 +0300998
999/*[clinic input]
1000signal.sigwait
1001
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001002 sigset: sigset_t
Tal Einatc7027b72015-05-16 14:14:49 +03001003 /
1004
1005Wait for a signal.
1006
1007Suspend execution of the calling thread until the delivery of one of the
1008signals specified in the signal set sigset. The function accepts the signal
1009and returns the signal number.
1010[clinic start generated code]*/
1011
Victor Stinnerb3e72192011-05-08 01:46:11 +02001012static PyObject *
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001013signal_sigwait_impl(PyObject *module, sigset_t sigset)
1014/*[clinic end generated code: output=f43770699d682f96 input=a6fbd47b1086d119]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +02001015{
Victor Stinnerb3e72192011-05-08 01:46:11 +02001016 int err, signum;
1017
Victor Stinner10c30d62011-06-10 01:39:53 +02001018 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001019 err = sigwait(&sigset, &signum);
Victor Stinner10c30d62011-06-10 01:39:53 +02001020 Py_END_ALLOW_THREADS
Victor Stinnerb3e72192011-05-08 01:46:11 +02001021 if (err) {
1022 errno = err;
1023 return PyErr_SetFromErrno(PyExc_OSError);
1024 }
1025
1026 return PyLong_FromLong(signum);
1027}
1028
Tal Einatc7027b72015-05-16 14:14:49 +03001029#endif /* #ifdef HAVE_SIGWAIT */
1030
Victor Stinnerb3e72192011-05-08 01:46:11 +02001031
Antoine Pitrou9d3627e2018-05-04 13:00:50 +02001032#if defined(HAVE_SIGFILLSET) || defined(MS_WINDOWS)
1033
1034/*[clinic input]
1035signal.valid_signals
1036
1037Return a set of valid signal numbers on this platform.
1038
1039The signal numbers returned by this function can be safely passed to
1040functions like `pthread_sigmask`.
1041[clinic start generated code]*/
1042
1043static PyObject *
1044signal_valid_signals_impl(PyObject *module)
1045/*[clinic end generated code: output=1609cffbcfcf1314 input=86a3717ff25288f2]*/
1046{
1047#ifdef MS_WINDOWS
1048#ifdef SIGBREAK
1049 PyObject *tup = Py_BuildValue("(iiiiiii)", SIGABRT, SIGBREAK, SIGFPE,
1050 SIGILL, SIGINT, SIGSEGV, SIGTERM);
1051#else
1052 PyObject *tup = Py_BuildValue("(iiiiii)", SIGABRT, SIGFPE, SIGILL,
1053 SIGINT, SIGSEGV, SIGTERM);
1054#endif
1055 if (tup == NULL) {
1056 return NULL;
1057 }
1058 PyObject *set = PySet_New(tup);
1059 Py_DECREF(tup);
1060 return set;
1061#else
1062 sigset_t mask;
1063 if (sigemptyset(&mask) || sigfillset(&mask)) {
1064 return PyErr_SetFromErrno(PyExc_OSError);
1065 }
1066 return sigset_to_set(mask);
1067#endif
1068}
1069
1070#endif /* #if defined(HAVE_SIGFILLSET) || defined(MS_WINDOWS) */
1071
1072
Ross Lagerwallbc808222011-06-25 12:13:40 +02001073#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
Ross Lagerwallbc808222011-06-25 12:13:40 +02001074static PyStructSequence_Field struct_siginfo_fields[] = {
1075 {"si_signo", "signal number"},
1076 {"si_code", "signal code"},
1077 {"si_errno", "errno associated with this signal"},
1078 {"si_pid", "sending process ID"},
1079 {"si_uid", "real user ID of sending process"},
1080 {"si_status", "exit value or signal"},
1081 {"si_band", "band event for SIGPOLL"},
1082 {0}
1083};
1084
1085PyDoc_STRVAR(struct_siginfo__doc__,
1086"struct_siginfo: Result from sigwaitinfo or sigtimedwait.\n\n\
1087This object may be accessed either as a tuple of\n\
1088(si_signo, si_code, si_errno, si_pid, si_uid, si_status, si_band),\n\
1089or via the attributes si_signo, si_code, and so on.");
1090
1091static PyStructSequence_Desc struct_siginfo_desc = {
1092 "signal.struct_siginfo", /* name */
1093 struct_siginfo__doc__, /* doc */
1094 struct_siginfo_fields, /* fields */
1095 7 /* n_in_sequence */
1096};
1097
1098static PyTypeObject SiginfoType;
1099
1100static PyObject *
1101fill_siginfo(siginfo_t *si)
1102{
1103 PyObject *result = PyStructSequence_New(&SiginfoType);
1104 if (!result)
1105 return NULL;
1106
1107 PyStructSequence_SET_ITEM(result, 0, PyLong_FromLong((long)(si->si_signo)));
1108 PyStructSequence_SET_ITEM(result, 1, PyLong_FromLong((long)(si->si_code)));
Victor Stinner09532fe2019-05-10 23:39:09 +02001109#ifdef __VXWORKS__
pxinwr8b5bdda2019-03-14 01:18:25 +08001110 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong(0L));
1111 PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong(0L));
1112 PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong(0L));
1113 PyStructSequence_SET_ITEM(result, 5, PyLong_FromLong(0L));
Victor Stinner09532fe2019-05-10 23:39:09 +02001114#else
Ross Lagerwallbc808222011-06-25 12:13:40 +02001115 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si->si_errno)));
1116 PyStructSequence_SET_ITEM(result, 3, PyLong_FromPid(si->si_pid));
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02001117 PyStructSequence_SET_ITEM(result, 4, _PyLong_FromUid(si->si_uid));
Ross Lagerwallbc808222011-06-25 12:13:40 +02001118 PyStructSequence_SET_ITEM(result, 5,
1119 PyLong_FromLong((long)(si->si_status)));
Victor Stinner09532fe2019-05-10 23:39:09 +02001120#endif
Zachary Ware6a6967e2016-10-01 00:47:27 -05001121#ifdef HAVE_SIGINFO_T_SI_BAND
Ross Lagerwallbc808222011-06-25 12:13:40 +02001122 PyStructSequence_SET_ITEM(result, 6, PyLong_FromLong(si->si_band));
Zachary Ware6a6967e2016-10-01 00:47:27 -05001123#else
1124 PyStructSequence_SET_ITEM(result, 6, PyLong_FromLong(0L));
1125#endif
Ross Lagerwallbc808222011-06-25 12:13:40 +02001126 if (PyErr_Occurred()) {
1127 Py_DECREF(result);
1128 return NULL;
1129 }
1130
1131 return result;
1132}
1133#endif
1134
1135#ifdef HAVE_SIGWAITINFO
Tal Einatc7027b72015-05-16 14:14:49 +03001136
1137/*[clinic input]
1138signal.sigwaitinfo
1139
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001140 sigset: sigset_t
Tal Einatc7027b72015-05-16 14:14:49 +03001141 /
1142
1143Wait synchronously until one of the signals in *sigset* is delivered.
1144
1145Returns a struct_siginfo containing information about the signal.
1146[clinic start generated code]*/
1147
Ross Lagerwallbc808222011-06-25 12:13:40 +02001148static PyObject *
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001149signal_sigwaitinfo_impl(PyObject *module, sigset_t sigset)
1150/*[clinic end generated code: output=1eb2f1fa236fdbca input=3d1a7e1f27fc664c]*/
Ross Lagerwallbc808222011-06-25 12:13:40 +02001151{
Ross Lagerwallbc808222011-06-25 12:13:40 +02001152 siginfo_t si;
1153 int err;
Victor Stinnera453cd82015-03-20 12:54:28 +01001154 int async_err = 0;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001155
Victor Stinnera453cd82015-03-20 12:54:28 +01001156 do {
1157 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001158 err = sigwaitinfo(&sigset, &si);
Victor Stinnera453cd82015-03-20 12:54:28 +01001159 Py_END_ALLOW_THREADS
1160 } while (err == -1
1161 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Ross Lagerwallbc808222011-06-25 12:13:40 +02001162 if (err == -1)
Victor Stinnera453cd82015-03-20 12:54:28 +01001163 return (!async_err) ? PyErr_SetFromErrno(PyExc_OSError) : NULL;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001164
1165 return fill_siginfo(&si);
1166}
1167
Ross Lagerwallbc808222011-06-25 12:13:40 +02001168#endif /* #ifdef HAVE_SIGWAITINFO */
1169
1170#ifdef HAVE_SIGTIMEDWAIT
Tal Einatc7027b72015-05-16 14:14:49 +03001171
1172/*[clinic input]
1173signal.sigtimedwait
1174
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001175 sigset: sigset_t
Serhiy Storchaka6b680cd2015-05-16 15:57:56 +03001176 timeout as timeout_obj: object
Tal Einatc7027b72015-05-16 14:14:49 +03001177 /
1178
1179Like sigwaitinfo(), but with a timeout.
1180
1181The timeout is specified in seconds, with floating point numbers allowed.
1182[clinic start generated code]*/
1183
Ross Lagerwallbc808222011-06-25 12:13:40 +02001184static PyObject *
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001185signal_sigtimedwait_impl(PyObject *module, sigset_t sigset,
Serhiy Storchaka6b680cd2015-05-16 15:57:56 +03001186 PyObject *timeout_obj)
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001187/*[clinic end generated code: output=59c8971e8ae18a64 input=87fd39237cf0b7ba]*/
Ross Lagerwallbc808222011-06-25 12:13:40 +02001188{
Victor Stinnera453cd82015-03-20 12:54:28 +01001189 struct timespec ts;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001190 siginfo_t si;
Victor Stinnera453cd82015-03-20 12:54:28 +01001191 int res;
Victor Stinner34dc0f42015-03-27 18:19:03 +01001192 _PyTime_t timeout, deadline, monotonic;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001193
Victor Stinner869e1772015-03-30 03:49:14 +02001194 if (_PyTime_FromSecondsObject(&timeout,
1195 timeout_obj, _PyTime_ROUND_CEILING) < 0)
Ross Lagerwallbc808222011-06-25 12:13:40 +02001196 return NULL;
1197
Victor Stinnera453cd82015-03-20 12:54:28 +01001198 if (timeout < 0) {
Ross Lagerwallbc808222011-06-25 12:13:40 +02001199 PyErr_SetString(PyExc_ValueError, "timeout must be non-negative");
1200 return NULL;
1201 }
1202
Victor Stinner34dc0f42015-03-27 18:19:03 +01001203 deadline = _PyTime_GetMonotonicClock() + timeout;
Victor Stinnera453cd82015-03-20 12:54:28 +01001204
1205 do {
Victor Stinner34dc0f42015-03-27 18:19:03 +01001206 if (_PyTime_AsTimespec(timeout, &ts) < 0)
1207 return NULL;
Victor Stinnera453cd82015-03-20 12:54:28 +01001208
1209 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001210 res = sigtimedwait(&sigset, &si, &ts);
Victor Stinnera453cd82015-03-20 12:54:28 +01001211 Py_END_ALLOW_THREADS
1212
1213 if (res != -1)
1214 break;
1215
1216 if (errno != EINTR) {
1217 if (errno == EAGAIN)
1218 Py_RETURN_NONE;
1219 else
1220 return PyErr_SetFromErrno(PyExc_OSError);
1221 }
1222
1223 /* sigtimedwait() was interrupted by a signal (EINTR) */
1224 if (PyErr_CheckSignals())
1225 return NULL;
1226
Victor Stinner34dc0f42015-03-27 18:19:03 +01001227 monotonic = _PyTime_GetMonotonicClock();
1228 timeout = deadline - monotonic;
Victor Stinner6aa446c2015-03-30 21:33:51 +02001229 if (timeout < 0)
Victor Stinnera453cd82015-03-20 12:54:28 +01001230 break;
1231 } while (1);
Ross Lagerwallbc808222011-06-25 12:13:40 +02001232
1233 return fill_siginfo(&si);
1234}
1235
Ross Lagerwallbc808222011-06-25 12:13:40 +02001236#endif /* #ifdef HAVE_SIGTIMEDWAIT */
1237
Victor Stinnerb3e72192011-05-08 01:46:11 +02001238
Antoine Pitroua6a4dc82017-09-07 18:56:24 +02001239#if defined(HAVE_PTHREAD_KILL)
Tal Einatc7027b72015-05-16 14:14:49 +03001240
1241/*[clinic input]
1242signal.pthread_kill
1243
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +02001244 thread_id: unsigned_long(bitwise=True)
Tal Einatc7027b72015-05-16 14:14:49 +03001245 signalnum: int
1246 /
1247
1248Send a signal to a thread.
1249[clinic start generated code]*/
1250
Victor Stinnerb3e72192011-05-08 01:46:11 +02001251static PyObject *
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +02001252signal_pthread_kill_impl(PyObject *module, unsigned long thread_id,
1253 int signalnum)
1254/*[clinic end generated code: output=7629919b791bc27f input=1d901f2c7bb544ff]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +02001255{
Victor Stinnerb3e72192011-05-08 01:46:11 +02001256 int err;
1257
Saiyang Gou7514f4f2020-02-12 23:47:42 -08001258 if (PySys_Audit("signal.pthread_kill", "ki", thread_id, signalnum) < 0) {
1259 return NULL;
1260 }
1261
Tal Einatc7027b72015-05-16 14:14:49 +03001262 err = pthread_kill((pthread_t)thread_id, signalnum);
Victor Stinnerb3e72192011-05-08 01:46:11 +02001263 if (err != 0) {
1264 errno = err;
1265 PyErr_SetFromErrno(PyExc_OSError);
1266 return NULL;
1267 }
1268
1269 /* the signal may have been send to the current thread */
1270 if (PyErr_CheckSignals())
1271 return NULL;
1272
1273 Py_RETURN_NONE;
1274}
1275
Antoine Pitroua6a4dc82017-09-07 18:56:24 +02001276#endif /* #if defined(HAVE_PTHREAD_KILL) */
Victor Stinnerb3e72192011-05-08 01:46:11 +02001277
1278
Benjamin Peterson74834512019-11-19 20:39:14 -08001279#if defined(__linux__) && defined(__NR_pidfd_send_signal)
1280/*[clinic input]
1281signal.pidfd_send_signal
1282
1283 pidfd: int
1284 signalnum: int
1285 siginfo: object = None
1286 flags: int = 0
1287 /
1288
1289Send a signal to a process referred to by a pid file descriptor.
1290[clinic start generated code]*/
1291
1292static PyObject *
1293signal_pidfd_send_signal_impl(PyObject *module, int pidfd, int signalnum,
1294 PyObject *siginfo, int flags)
1295/*[clinic end generated code: output=2d59f04a75d9cbdf input=2a6543a1f4ac2000]*/
1296
1297{
1298 if (siginfo != Py_None) {
1299 PyErr_SetString(PyExc_TypeError, "siginfo must be None");
1300 return NULL;
1301 }
1302 if (syscall(__NR_pidfd_send_signal, pidfd, signalnum, NULL, flags) < 0) {
1303 PyErr_SetFromErrno(PyExc_OSError);
1304 return NULL;
1305 }
1306 Py_RETURN_NONE;
1307}
1308#endif
1309
1310
Victor Stinnerb3e72192011-05-08 01:46:11 +02001311
Tal Einatc7027b72015-05-16 14:14:49 +03001312/* List of functions defined in the module -- some of the methoddefs are
1313 defined to nothing if the corresponding C function is not available. */
Barry Warsaw92971171997-01-03 00:14:25 +00001314static PyMethodDef signal_methods[] = {
Serhiy Storchakab0689ae2020-07-12 19:15:20 +03001315 SIGNAL_DEFAULT_INT_HANDLER_METHODDEF
Tal Einatc7027b72015-05-16 14:14:49 +03001316 SIGNAL_ALARM_METHODDEF
1317 SIGNAL_SETITIMER_METHODDEF
1318 SIGNAL_GETITIMER_METHODDEF
1319 SIGNAL_SIGNAL_METHODDEF
Vladimir Matveevc24c6c22019-01-08 01:58:25 -08001320 SIGNAL_RAISE_SIGNAL_METHODDEF
Antoine Pietri5d2a27d2018-03-12 14:42:34 +01001321 SIGNAL_STRSIGNAL_METHODDEF
Tal Einatc7027b72015-05-16 14:14:49 +03001322 SIGNAL_GETSIGNAL_METHODDEF
Serhiy Storchaka62be7422018-11-27 13:27:31 +02001323 {"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 +03001324 SIGNAL_SIGINTERRUPT_METHODDEF
1325 SIGNAL_PAUSE_METHODDEF
Benjamin Peterson74834512019-11-19 20:39:14 -08001326 SIGNAL_PIDFD_SEND_SIGNAL_METHODDEF
Tal Einatc7027b72015-05-16 14:14:49 +03001327 SIGNAL_PTHREAD_KILL_METHODDEF
1328 SIGNAL_PTHREAD_SIGMASK_METHODDEF
1329 SIGNAL_SIGPENDING_METHODDEF
1330 SIGNAL_SIGWAIT_METHODDEF
1331 SIGNAL_SIGWAITINFO_METHODDEF
1332 SIGNAL_SIGTIMEDWAIT_METHODDEF
Antoine Pitrou9d3627e2018-05-04 13:00:50 +02001333#if defined(HAVE_SIGFILLSET) || defined(MS_WINDOWS)
1334 SIGNAL_VALID_SIGNALS_METHODDEF
1335#endif
Tal Einatc7027b72015-05-16 14:14:49 +03001336 {NULL, NULL} /* sentinel */
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001337};
1338
Barry Warsaw92971171997-01-03 00:14:25 +00001339
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001340PyDoc_STRVAR(module_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001341"This module provides mechanisms to use signal handlers in Python.\n\
1342\n\
1343Functions:\n\
1344\n\
1345alarm() -- cause SIGALRM after a specified time [Unix only]\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001346setitimer() -- cause a signal (described below) after a specified\n\
1347 float time and the timer may restart then [Unix only]\n\
1348getitimer() -- get current value of timer [Unix only]\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001349signal() -- set the action for a given signal\n\
1350getsignal() -- get the signal action for a given signal\n\
1351pause() -- wait until a signal arrives [Unix only]\n\
1352default_int_handler() -- default SIGINT handler\n\
1353\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001354signal constants:\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001355SIG_DFL -- used to refer to the system default handler\n\
1356SIG_IGN -- used to ignore the signal\n\
1357NSIG -- number of defined signals\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001358SIGINT, SIGTERM, etc. -- signal numbers\n\
1359\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001360itimer constants:\n\
1361ITIMER_REAL -- decrements in real time, and delivers SIGALRM upon\n\
1362 expiration\n\
1363ITIMER_VIRTUAL -- decrements only when the process is executing,\n\
1364 and delivers SIGVTALRM upon expiration\n\
1365ITIMER_PROF -- decrements both when the process is executing and\n\
1366 when the system is executing on behalf of the process.\n\
1367 Coupled with ITIMER_VIRTUAL, this timer is usually\n\
1368 used to profile the time spent by the application\n\
1369 in user and kernel space. SIGPROF is delivered upon\n\
1370 expiration.\n\
1371\n\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001372*** IMPORTANT NOTICE ***\n\
1373A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001374the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001375
Martin v. Löwis1a214512008-06-11 05:26:20 +00001376
Mohamed Koubaa71d1bd92020-09-03 03:21:06 -05001377
1378static int
Victor Stinnercda23be2020-11-17 18:57:32 +01001379signal_add_constants(PyObject *module)
1380{
1381#define ADD_INT_MACRO(macro) \
1382 if (PyModule_AddIntConstant(module, #macro, macro) < 0) { \
1383 return -1; \
1384 }
1385
1386 ADD_INT_MACRO(NSIG);
1387
1388 // SIG_xxx pthread_sigmask() constants
1389#ifdef SIG_BLOCK
1390 ADD_INT_MACRO(SIG_BLOCK);
1391#endif
1392#ifdef SIG_UNBLOCK
1393 ADD_INT_MACRO(SIG_UNBLOCK);
1394#endif
1395#ifdef SIG_SETMASK
1396 ADD_INT_MACRO(SIG_SETMASK);
1397#endif
1398
1399 // SIGxxx signal number constants
1400#ifdef SIGHUP
1401 ADD_INT_MACRO(SIGHUP);
1402#endif
1403#ifdef SIGINT
1404 ADD_INT_MACRO(SIGINT);
1405#endif
1406#ifdef SIGBREAK
1407 ADD_INT_MACRO(SIGBREAK);
1408#endif
1409#ifdef SIGQUIT
1410 ADD_INT_MACRO(SIGQUIT);
1411#endif
1412#ifdef SIGILL
1413 ADD_INT_MACRO(SIGILL);
1414#endif
1415#ifdef SIGTRAP
1416 ADD_INT_MACRO(SIGTRAP);
1417#endif
1418#ifdef SIGIOT
1419 ADD_INT_MACRO(SIGIOT);
1420#endif
1421#ifdef SIGABRT
1422 ADD_INT_MACRO(SIGABRT);
1423#endif
1424#ifdef SIGEMT
1425 ADD_INT_MACRO(SIGEMT);
1426#endif
1427#ifdef SIGFPE
1428 ADD_INT_MACRO(SIGFPE);
1429#endif
1430#ifdef SIGKILL
1431 ADD_INT_MACRO(SIGKILL);
1432#endif
1433#ifdef SIGBUS
1434 ADD_INT_MACRO(SIGBUS);
1435#endif
1436#ifdef SIGSEGV
1437 ADD_INT_MACRO(SIGSEGV);
1438#endif
1439#ifdef SIGSYS
1440 ADD_INT_MACRO(SIGSYS);
1441#endif
1442#ifdef SIGPIPE
1443 ADD_INT_MACRO(SIGPIPE);
1444#endif
1445#ifdef SIGALRM
1446 ADD_INT_MACRO(SIGALRM);
1447#endif
1448#ifdef SIGTERM
1449 ADD_INT_MACRO(SIGTERM);
1450#endif
1451#ifdef SIGUSR1
1452 ADD_INT_MACRO(SIGUSR1);
1453#endif
1454#ifdef SIGUSR2
1455 ADD_INT_MACRO(SIGUSR2);
1456#endif
1457#ifdef SIGCLD
1458 ADD_INT_MACRO(SIGCLD);
1459#endif
1460#ifdef SIGCHLD
1461 ADD_INT_MACRO(SIGCHLD);
1462#endif
1463#ifdef SIGPWR
1464 ADD_INT_MACRO(SIGPWR);
1465#endif
1466#ifdef SIGIO
1467 ADD_INT_MACRO(SIGIO);
1468#endif
1469#ifdef SIGURG
1470 ADD_INT_MACRO(SIGURG);
1471#endif
1472#ifdef SIGWINCH
1473 ADD_INT_MACRO(SIGWINCH);
1474#endif
1475#ifdef SIGPOLL
1476 ADD_INT_MACRO(SIGPOLL);
1477#endif
1478#ifdef SIGSTOP
1479 ADD_INT_MACRO(SIGSTOP);
1480#endif
1481#ifdef SIGTSTP
1482 ADD_INT_MACRO(SIGTSTP);
1483#endif
1484#ifdef SIGCONT
1485 ADD_INT_MACRO(SIGCONT);
1486#endif
1487#ifdef SIGTTIN
1488 ADD_INT_MACRO(SIGTTIN);
1489#endif
1490#ifdef SIGTTOU
1491 ADD_INT_MACRO(SIGTTOU);
1492#endif
1493#ifdef SIGVTALRM
1494 ADD_INT_MACRO(SIGVTALRM);
1495#endif
1496#ifdef SIGPROF
1497 ADD_INT_MACRO(SIGPROF);
1498#endif
1499#ifdef SIGXCPU
1500 ADD_INT_MACRO(SIGXCPU);
1501#endif
1502#ifdef SIGXFSZ
1503 ADD_INT_MACRO(SIGXFSZ);
1504#endif
1505#ifdef SIGRTMIN
1506 ADD_INT_MACRO(SIGRTMIN);
1507#endif
1508#ifdef SIGRTMAX
1509 ADD_INT_MACRO(SIGRTMAX);
1510#endif
1511#ifdef SIGINFO
1512 ADD_INT_MACRO(SIGINFO);
1513#endif
1514
1515 // ITIMER_xxx constants
1516#ifdef ITIMER_REAL
1517 ADD_INT_MACRO(ITIMER_REAL);
1518#endif
1519#ifdef ITIMER_VIRTUAL
1520 ADD_INT_MACRO(ITIMER_VIRTUAL);
1521#endif
1522#ifdef ITIMER_PROF
1523 ADD_INT_MACRO(ITIMER_PROF);
1524#endif
1525
1526 // CTRL_xxx Windows signals
1527#ifdef CTRL_C_EVENT
1528 ADD_INT_MACRO(CTRL_C_EVENT);
1529#endif
1530#ifdef CTRL_BREAK_EVENT
1531 ADD_INT_MACRO(CTRL_BREAK_EVENT);
1532#endif
1533
1534 return 0;
1535
1536#undef ADD_INT_MACRO
1537}
1538
1539
1540static int
Mohamed Koubaa71d1bd92020-09-03 03:21:06 -05001541signal_exec(PyObject *m)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001542{
Victor Stinnercda23be2020-11-17 18:57:32 +01001543 assert(!PyErr_Occurred());
1544
1545 if (signal_add_constants(m) < 0) {
1546 return -1;
1547 }
1548
1549 /* Add some symbolic constants to the module */
1550 PyObject *d = PyModule_GetDict(m);
1551 if (PyDict_SetItemString(d, "SIG_DFL", DefaultHandler) < 0) {
1552 return -1;
1553 }
1554 if (PyDict_SetItemString(d, "SIG_IGN", IgnoreHandler) < 0) {
1555 return -1;
1556 }
1557#if defined(HAVE_GETITIMER) || defined(HAVE_SETITIMER)
1558 if (PyDict_SetItemString(d, "ItimerError", ItimerError) < 0) {
1559 return -1;
1560 }
1561#endif
Ross Lagerwallbc808222011-06-25 12:13:40 +02001562#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
Mohamed Koubaa71d1bd92020-09-03 03:21:06 -05001563 if (PyModule_AddType(m, &SiginfoType) < 0) {
1564 return -1;
1565 }
Ross Lagerwallbc808222011-06-25 12:13:40 +02001566#endif
1567
Victor Stinnercda23be2020-11-17 18:57:32 +01001568 // Get signal handlers
1569 for (int signum = 1; signum < NSIG; signum++) {
1570 void (*c_handler)(int) = PyOS_getsig(signum);
1571 if (c_handler == SIG_DFL) {
1572 Handlers[signum].func = Py_NewRef(DefaultHandler);
1573 }
1574 else if (c_handler == SIG_IGN) {
1575 Handlers[signum].func = Py_NewRef(IgnoreHandler);
1576 }
1577 else {
1578 Handlers[signum].func = Py_NewRef(Py_None); // None of our business
1579 }
animalize77643c42019-09-09 21:46:26 +08001580 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001581
Victor Stinnercda23be2020-11-17 18:57:32 +01001582 // Instal Python SIGINT handler which raises KeyboardInterrupt
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001583 if (Handlers[SIGINT].func == DefaultHandler) {
Victor Stinner0ae323b2020-11-17 18:15:20 +01001584 PyObject *int_handler = PyMapping_GetItemString(d, "default_int_handler");
1585 if (!int_handler) {
1586 return -1;
1587 }
1588
Victor Stinner0ae323b2020-11-17 18:15:20 +01001589 Py_SETREF(Handlers[SIGINT].func, int_handler);
pkerlinge905c842018-06-01 09:47:18 +00001590 PyOS_setsig(SIGINT, signal_handler);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001591 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001592
Victor Stinnercda23be2020-11-17 18:57:32 +01001593 assert(!PyErr_Occurred());
1594 return 0;
Mohamed Koubaa71d1bd92020-09-03 03:21:06 -05001595}
1596
Mohamed Koubaa71d1bd92020-09-03 03:21:06 -05001597
1598static struct PyModuleDef signalmodule = {
1599 PyModuleDef_HEAD_INIT,
1600 "_signal",
1601 .m_doc = module_doc,
Victor Stinner4b8032e2020-09-04 14:51:05 +02001602 .m_size = -1,
Mohamed Koubaa71d1bd92020-09-03 03:21:06 -05001603 .m_methods = signal_methods,
Mohamed Koubaa71d1bd92020-09-03 03:21:06 -05001604};
1605
Victor Stinner4b8032e2020-09-04 14:51:05 +02001606
Mohamed Koubaa71d1bd92020-09-03 03:21:06 -05001607PyMODINIT_FUNC
1608PyInit__signal(void)
1609{
Victor Stinner4b8032e2020-09-04 14:51:05 +02001610 PyObject *mod = PyModule_Create(&signalmodule);
1611 if (mod == NULL) {
1612 return NULL;
1613 }
1614
1615 if (signal_exec(mod) < 0) {
1616 Py_DECREF(mod);
1617 return NULL;
1618 }
1619 return mod;
Guido van Rossum08c16611997-08-02 03:01:42 +00001620}
1621
Victor Stinner4b8032e2020-09-04 14:51:05 +02001622
Victor Stinner296a7962020-11-17 16:22:23 +01001623void
1624_PySignal_Fini(void)
Guido van Rossum08c16611997-08-02 03:01:42 +00001625{
Victor Stinner0ae323b2020-11-17 18:15:20 +01001626 // Restore default signals and clear handlers
1627 for (int signum = 1; signum < NSIG; signum++) {
1628 PyObject *func = Handlers[signum].func;
1629 _Py_atomic_store_relaxed(&Handlers[signum].tripped, 0);
1630 Handlers[signum].func = NULL;
1631 if (func != NULL
1632 && func != Py_None
1633 && func != DefaultHandler
1634 && func != IgnoreHandler)
1635 {
1636 PyOS_setsig(signum, SIG_DFL);
1637 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001638 Py_XDECREF(func);
1639 }
Guido van Rossum08c16611997-08-02 03:01:42 +00001640
Victor Stinner0ae323b2020-11-17 18:15:20 +01001641#ifdef MS_WINDOWS
1642 if (sigint_event != NULL) {
1643 CloseHandle(sigint_event);
1644 sigint_event = NULL;
1645 }
1646#endif
1647
Serhiy Storchaka505ff752014-02-09 13:33:53 +02001648 Py_CLEAR(DefaultHandler);
1649 Py_CLEAR(IgnoreHandler);
Victor Stinner0ae323b2020-11-17 18:15:20 +01001650#if defined(HAVE_GETITIMER) || defined(HAVE_SETITIMER)
animalize77643c42019-09-09 21:46:26 +08001651 Py_CLEAR(ItimerError);
1652#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001653}
1654
Barry Warsaw92971171997-01-03 00:14:25 +00001655
Barry Warsaw92971171997-01-03 00:14:25 +00001656/* Declared in pyerrors.h */
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001657int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001658PyErr_CheckSignals(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001659{
Victor Stinner72818982020-03-26 22:28:11 +01001660 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerb54a99d2020-04-08 23:35:05 +02001661 if (!_Py_ThreadCanHandleSignals(tstate->interp)) {
Eric Snow64d6cc82019-02-23 15:40:43 -07001662 return 0;
1663 }
1664
Victor Stinner72818982020-03-26 22:28:11 +01001665 return _PyErr_CheckSignalsTstate(tstate);
Eric Snow64d6cc82019-02-23 15:40:43 -07001666}
1667
1668
1669/* Declared in cpython/pyerrors.h */
1670int
Victor Stinner72818982020-03-26 22:28:11 +01001671_PyErr_CheckSignalsTstate(PyThreadState *tstate)
Eric Snow64d6cc82019-02-23 15:40:43 -07001672{
Victor Stinner72818982020-03-26 22:28:11 +01001673 if (!_Py_atomic_load(&is_tripped)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001674 return 0;
Victor Stinner72818982020-03-26 22:28:11 +01001675 }
Christian Heimesb76922a2007-12-11 01:06:40 +00001676
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001677 /*
1678 * The is_tripped variable is meant to speed up the calls to
1679 * PyErr_CheckSignals (both directly or via pending calls) when no
1680 * signal has arrived. This variable is set to 1 when a signal arrives
1681 * and it is set to 0 here, when we know some signals arrived. This way
1682 * we can run the registered handlers with no signals blocked.
1683 *
1684 * NOTE: with this approach we can have a situation where is_tripped is
1685 * 1 but we have no more signals to handle (Handlers[i].tripped
1686 * is 0 for every signal i). This won't do us any harm (except
1687 * we're gonna spent some cycles for nothing). This happens when
1688 * we receive a signal i after we zero is_tripped and before we
1689 * check Handlers[i].tripped.
1690 */
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001691 _Py_atomic_store(&is_tripped, 0);
Christian Heimesb76922a2007-12-11 01:06:40 +00001692
Victor Stinner72818982020-03-26 22:28:11 +01001693 PyObject *frame = (PyObject *)tstate->frame;
1694 if (!frame) {
1695 frame = Py_None;
1696 }
Christian Heimesb76922a2007-12-11 01:06:40 +00001697
Victor Stinner72818982020-03-26 22:28:11 +01001698 for (int i = 1; i < NSIG; i++) {
1699 if (!_Py_atomic_load_relaxed(&Handlers[i].tripped)) {
1700 continue;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001701 }
Victor Stinner72818982020-03-26 22:28:11 +01001702 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
1703
1704 PyObject *arglist = Py_BuildValue("(iO)", i, frame);
1705 PyObject *result;
1706 if (arglist) {
1707 result = _PyObject_Call(tstate, Handlers[i].func, arglist, NULL);
1708 Py_DECREF(arglist);
1709 }
1710 else {
1711 result = NULL;
1712 }
1713 if (!result) {
1714 /* On error, re-schedule a call to _PyErr_CheckSignalsTstate() */
1715 _Py_atomic_store(&is_tripped, 1);
1716 return -1;
1717 }
1718
1719 Py_DECREF(result);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001720 }
Christian Heimesb76922a2007-12-11 01:06:40 +00001721
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001722 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001723}
1724
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +00001725
Victor Stinner72818982020-03-26 22:28:11 +01001726
1727int
1728_PyErr_CheckSignals(void)
1729{
1730 PyThreadState *tstate = _PyThreadState_GET();
1731 return _PyErr_CheckSignalsTstate(tstate);
1732}
1733
1734
Matěj Cepl608876b2019-05-23 22:30:00 +02001735/* Simulate the effect of a signal.SIGINT signal arriving. The next time
1736 PyErr_CheckSignals is called, the Python SIGINT signal handler will be
1737 raised.
1738
1739 Missing signal handler for the SIGINT signal is silently ignored. */
Barry Warsaw92971171997-01-03 00:14:25 +00001740void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001741PyErr_SetInterrupt(void)
Barry Warsaw92971171997-01-03 00:14:25 +00001742{
Matěj Cepl608876b2019-05-23 22:30:00 +02001743 if ((Handlers[SIGINT].func != IgnoreHandler) &&
1744 (Handlers[SIGINT].func != DefaultHandler)) {
1745 trip_signal(SIGINT);
1746 }
Barry Warsaw92971171997-01-03 00:14:25 +00001747}
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001748
Victor Stinner0ae323b2020-11-17 18:15:20 +01001749static int
1750signal_install_handlers(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001751{
Victor Stinner296a7962020-11-17 16:22:23 +01001752#ifdef SIGPIPE
1753 PyOS_setsig(SIGPIPE, SIG_IGN);
1754#endif
1755#ifdef SIGXFZ
1756 PyOS_setsig(SIGXFZ, SIG_IGN);
1757#endif
1758#ifdef SIGXFSZ
1759 PyOS_setsig(SIGXFSZ, SIG_IGN);
1760#endif
1761
1762 // Import _signal to install the Python SIGINT handler
1763 PyObject *module = PyImport_ImportModule("_signal");
1764 if (!module) {
1765 return -1;
1766 }
1767 Py_DECREF(module);
1768
1769 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001770}
1771
Victor Stinnerfa7ab6a2020-06-03 14:39:59 +02001772
Victor Stinner29aa6242020-11-17 22:55:30 +01001773/* Restore signals that the interpreter has called SIG_IGN on to SIG_DFL.
1774 *
1775 * All of the code in this function must only use async-signal-safe functions,
1776 * listed at `man 7 signal` or
1777 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
1778 *
1779 * If this function is updated, update also _posix_spawn() of subprocess.py.
1780 */
1781void
1782_Py_RestoreSignals(void)
1783{
1784#ifdef SIGPIPE
1785 PyOS_setsig(SIGPIPE, SIG_DFL);
1786#endif
1787#ifdef SIGXFZ
1788 PyOS_setsig(SIGXFZ, SIG_DFL);
1789#endif
1790#ifdef SIGXFSZ
1791 PyOS_setsig(SIGXFSZ, SIG_DFL);
1792#endif
1793}
1794
1795
Victor Stinner0ae323b2020-11-17 18:15:20 +01001796int
1797_PySignal_Init(int install_signal_handlers)
1798{
1799 DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL);
1800 if (!DefaultHandler) {
1801 return -1;
1802 }
1803
1804 IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN);
1805 if (!IgnoreHandler) {
1806 return -1;
1807 }
1808
1809#if defined(HAVE_GETITIMER) || defined(HAVE_SETITIMER)
1810 ItimerError = PyErr_NewException("signal.ItimerError",
1811 PyExc_OSError, NULL);
1812 if (!ItimerError) {
1813 return -1;
1814 }
1815#endif
1816
1817#ifdef MS_WINDOWS
1818 /* Create manual-reset event, initially unset */
1819 sigint_event = CreateEvent(NULL, TRUE, FALSE, FALSE);
1820 if (sigint_event == NULL) {
1821 PyErr_SetFromWindowsErr(0);
1822 return -1;
1823 }
1824#endif
1825
1826#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
1827 if (SiginfoType.tp_name == NULL) {
1828 if (PyStructSequence_InitType2(&SiginfoType, &struct_siginfo_desc) < 0) {
1829 return -1;
1830 }
1831 }
1832#endif
1833
1834 for (int signum = 1; signum < NSIG; signum++) {
1835 _Py_atomic_store_relaxed(&Handlers[signum].tripped, 0);
1836 }
1837
1838 if (install_signal_handlers) {
1839 if (signal_install_handlers() < 0) {
1840 return -1;
1841 }
1842 }
1843
1844 return 0;
1845}
1846
1847
Victor Stinnerfa7ab6a2020-06-03 14:39:59 +02001848// The caller doesn't have to hold the GIL
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001849int
Victor Stinnerfa7ab6a2020-06-03 14:39:59 +02001850_PyOS_InterruptOccurred(PyThreadState *tstate)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001851{
Victor Stinnercbe12962020-06-01 20:34:15 +02001852 _Py_EnsureTstateNotNULL(tstate);
1853 if (!_Py_ThreadCanHandleSignals(tstate->interp)) {
Victor Stinner72818982020-03-26 22:28:11 +01001854 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001855 }
Victor Stinner72818982020-03-26 22:28:11 +01001856
1857 if (!_Py_atomic_load_relaxed(&Handlers[SIGINT].tripped)) {
1858 return 0;
1859 }
1860
1861 _Py_atomic_store_relaxed(&Handlers[SIGINT].tripped, 0);
1862 return 1;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001863}
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001864
Victor Stinner26881c82020-06-02 15:51:37 +02001865
Victor Stinnerfa7ab6a2020-06-03 14:39:59 +02001866// The caller must to hold the GIL
1867int
1868PyOS_InterruptOccurred(void)
1869{
1870 PyThreadState *tstate = _PyThreadState_GET();
1871 return _PyOS_InterruptOccurred(tstate);
1872}
1873
1874
Victor Stinner26881c82020-06-02 15:51:37 +02001875#ifdef HAVE_FORK
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001876static void
1877_clear_pending_signals(void)
1878{
Victor Stinner26881c82020-06-02 15:51:37 +02001879 if (!_Py_atomic_load(&is_tripped)) {
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001880 return;
Victor Stinner26881c82020-06-02 15:51:37 +02001881 }
1882
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001883 _Py_atomic_store(&is_tripped, 0);
Victor Stinner26881c82020-06-02 15:51:37 +02001884 for (int i = 1; i < NSIG; ++i) {
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001885 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001886 }
1887}
1888
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001889void
Antoine Pitrou346cbd32017-05-27 17:50:54 +02001890_PySignal_AfterFork(void)
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001891{
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001892 /* Clear the signal flags after forking so that they aren't handled
1893 * in both processes if they came in just before the fork() but before
1894 * the interpreter had an opportunity to call the handlers. issue9535. */
1895 _clear_pending_signals();
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001896}
Victor Stinner26881c82020-06-02 15:51:37 +02001897#endif /* HAVE_FORK */
1898
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001899
1900int
1901_PyOS_IsMainThread(void)
1902{
Victor Stinner81a7be32020-04-14 15:14:01 +02001903 PyInterpreterState *interp = _PyInterpreterState_GET();
Victor Stinnerb54a99d2020-04-08 23:35:05 +02001904 return _Py_ThreadCanHandleSignals(interp);
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001905}
1906
1907#ifdef MS_WINDOWS
1908void *_PyOS_SigintEvent(void)
1909{
1910 /* Returns a manual-reset event which gets tripped whenever
1911 SIGINT is received.
1912
1913 Python.h does not include windows.h so we do cannot use HANDLE
1914 as the return type of this function. We use void* instead. */
1915 return sigint_event;
1916}
1917#endif