blob: a1976737462404dff1aea7f9e86545ca89721fe6 [file] [log] [blame]
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002/* Signal module -- many thanks to Lance Ellinghaus */
Guido van Rossum398d9fe1994-05-11 08:59:13 +00003
Guido van Rossum644a12b1997-04-09 19:24:53 +00004/* XXX Signals should be recorded per thread, now we have thread state. */
5
Guido van Rossum602099a1994-09-14 13:32:22 +00006#include "Python.h"
Victor Stinner27e2d1f2018-11-01 00:52:28 +01007#include "pycore_atomic.h"
Victor Stinner09532fe2019-05-10 23:39:09 +02008#include "pycore_ceval.h"
9#include "pycore_pystate.h"
Victor Stinner31368a42018-10-30 15:14:25 +010010
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020011#ifndef MS_WINDOWS
12#include "posixmodule.h"
13#endif
Victor Stinner11517102014-07-29 23:31:34 +020014#ifdef MS_WINDOWS
15#include "socketmodule.h" /* needed for SOCKET_T */
16#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +000017
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000018#ifdef MS_WINDOWS
Antoine Pitrou7faf7052013-03-31 22:48:04 +020019#include <windows.h>
Benjamin Peterson2614cda2010-03-21 22:36:19 +000020#ifdef HAVE_PROCESS_H
Guido van Rossum644a12b1997-04-09 19:24:53 +000021#include <process.h>
22#endif
Benjamin Peterson2614cda2010-03-21 22:36:19 +000023#endif
Guido van Rossum644a12b1997-04-09 19:24:53 +000024
Benjamin Peterson2614cda2010-03-21 22:36:19 +000025#ifdef HAVE_SIGNAL_H
Guido van Rossum398d9fe1994-05-11 08:59:13 +000026#include <signal.h>
Benjamin Peterson2614cda2010-03-21 22:36:19 +000027#endif
Benjamin Peterson74834512019-11-19 20:39:14 -080028#ifdef HAVE_SYS_SYSCALL_H
29#include <sys/syscall.h>
30#endif
Benjamin Peterson2614cda2010-03-21 22:36:19 +000031#ifdef HAVE_SYS_STAT_H
Christian Heimes5fb7c2a2007-12-24 08:52:31 +000032#include <sys/stat.h>
Benjamin Peterson2614cda2010-03-21 22:36:19 +000033#endif
Martin v. Löwis3bf3cc02008-03-24 14:05:07 +000034#ifdef HAVE_SYS_TIME_H
Martin v. Löwis823725e2008-03-24 13:39:54 +000035#include <sys/time.h>
Martin v. Löwis3bf3cc02008-03-24 14:05:07 +000036#endif
Christian Heimes5fb7c2a2007-12-24 08:52:31 +000037
Victor Stinnera9293352011-04-30 15:21:58 +020038#if defined(HAVE_PTHREAD_SIGMASK) && !defined(HAVE_BROKEN_PTHREAD_SIGMASK)
39# define PYPTHREAD_SIGMASK
40#endif
41
42#if defined(PYPTHREAD_SIGMASK) && defined(HAVE_PTHREAD_H)
43# include <pthread.h>
44#endif
45
Guido van Rossumbb4ba121994-06-23 11:25:45 +000046#ifndef SIG_ERR
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +000047#define SIG_ERR ((PyOS_sighandler_t)(-1))
Guido van Rossumbb4ba121994-06-23 11:25:45 +000048#endif
49
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000050#ifndef NSIG
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000051# if defined(_NSIG)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000052# define NSIG _NSIG /* For BSD/SysV */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000053# elif defined(_SIGMAX)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000054# define NSIG (_SIGMAX + 1) /* For QNX */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000055# elif defined(SIGMAX)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000056# define NSIG (SIGMAX + 1) /* For djgpp */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000057# else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000058# define NSIG 64 /* Use a reasonable default value */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000059# endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000060#endif
61
Tal Einatc7027b72015-05-16 14:14:49 +030062#include "clinic/signalmodule.c.h"
63
64/*[clinic input]
65module signal
66[clinic start generated code]*/
67/*[clinic end generated code: output=da39a3ee5e6b4b0d input=b0301a3bde5fe9d3]*/
68
Serhiy Storchakad54cfb12018-05-08 07:48:50 +030069/*[python input]
70
71class sigset_t_converter(CConverter):
72 type = 'sigset_t'
73 converter = '_Py_Sigset_Converter'
74
75[python start generated code]*/
76/*[python end generated code: output=da39a3ee5e6b4b0d input=b5689d14466b6823]*/
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000077
Guido van Rossumbb4ba121994-06-23 11:25:45 +000078/*
79 NOTES ON THE INTERACTION BETWEEN SIGNALS AND THREADS
80
Jeroen Demeyerd237b3f2019-05-10 03:28:57 +020081 We want the following semantics:
Guido van Rossumbb4ba121994-06-23 11:25:45 +000082
83 - only the main thread can set a signal handler
Jeroen Demeyerd237b3f2019-05-10 03:28:57 +020084 - only the main thread runs the signal handler
85 - signals can be delivered to any thread
Guido van Rossumbb4ba121994-06-23 11:25:45 +000086 - any thread can get a signal handler
Guido van Rossumbb4ba121994-06-23 11:25:45 +000087
88 I.e. we don't support "synchronous signals" like SIGFPE (catching
89 this doesn't make much sense in Python anyway) nor do we support
90 signals as a means of inter-thread communication, since not all
91 thread implementations support that (at least our thread library
92 doesn't).
93
94 We still have the problem that in some implementations signals
95 generated by the keyboard (e.g. SIGINT) are delivered to all
96 threads (e.g. SGI), while in others (e.g. Solaris) such signals are
Jeroen Demeyerd237b3f2019-05-10 03:28:57 +020097 delivered to one random thread. On Linux, signals are delivered to
98 the main thread (unless the main thread is blocking the signal, for
99 example because it's already handling the same signal). Since we
100 allow signals to be delivered to any thread, this works fine. The
101 only oddity is that the thread executing the Python signal handler
102 may not be the thread that received the signal.
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000103*/
104
Guido van Rossum49b56061998-10-01 20:42:43 +0000105#include "pythread.h"
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000106
Victor Stinner2ec6b172011-05-15 10:21:59 +0200107static volatile struct {
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +0200108 _Py_atomic_int tripped;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000109 PyObject *func;
Barry Warsaw92971171997-01-03 00:14:25 +0000110} Handlers[NSIG];
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000111
Victor Stinner11517102014-07-29 23:31:34 +0200112#ifdef MS_WINDOWS
113#define INVALID_FD ((SOCKET_T)-1)
114
115static volatile struct {
116 SOCKET_T fd;
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800117 int warn_on_full_buffer;
Victor Stinner11517102014-07-29 23:31:34 +0200118 int use_send;
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800119} wakeup = {.fd = INVALID_FD, .warn_on_full_buffer = 1, .use_send = 0};
Victor Stinner11517102014-07-29 23:31:34 +0200120#else
121#define INVALID_FD (-1)
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800122static volatile struct {
123 sig_atomic_t fd;
124 int warn_on_full_buffer;
125} wakeup = {.fd = INVALID_FD, .warn_on_full_buffer = 1};
Victor Stinner11517102014-07-29 23:31:34 +0200126#endif
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000127
Christian Heimesb76922a2007-12-11 01:06:40 +0000128/* Speed up sigcheck() when none tripped */
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +0200129static _Py_atomic_int is_tripped;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000130
Barry Warsaw92971171997-01-03 00:14:25 +0000131static PyObject *DefaultHandler;
132static PyObject *IgnoreHandler;
133static PyObject *IntHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000134
Antoine Pitrou6dd381e2011-11-21 21:26:56 +0100135#ifdef MS_WINDOWS
136static HANDLE sigint_event = NULL;
137#endif
138
Martin v. Löwis823725e2008-03-24 13:39:54 +0000139#ifdef HAVE_GETITIMER
140static PyObject *ItimerError;
141
Victor Stinneref611c92017-10-13 13:49:43 -0700142/* auxiliary functions for setitimer */
143static int
144timeval_from_double(PyObject *obj, struct timeval *tv)
Martin v. Löwis823725e2008-03-24 13:39:54 +0000145{
Victor Stinneref611c92017-10-13 13:49:43 -0700146 if (obj == NULL) {
147 tv->tv_sec = 0;
148 tv->tv_usec = 0;
149 return 0;
Antoine Pitrou729780a2017-06-30 10:01:05 +0200150 }
Victor Stinneref611c92017-10-13 13:49:43 -0700151
152 _PyTime_t t;
153 if (_PyTime_FromSecondsObject(&t, obj, _PyTime_ROUND_CEILING) < 0) {
154 return -1;
155 }
156 return _PyTime_AsTimeval(t, tv, _PyTime_ROUND_CEILING);
Martin v. Löwis823725e2008-03-24 13:39:54 +0000157}
158
Christian Heimes1a8501c2008-10-02 19:56:01 +0000159Py_LOCAL_INLINE(double)
Martin v. Löwis823725e2008-03-24 13:39:54 +0000160double_from_timeval(struct timeval *tv)
161{
162 return tv->tv_sec + (double)(tv->tv_usec / 1000000.0);
163}
164
165static PyObject *
166itimer_retval(struct itimerval *iv)
167{
168 PyObject *r, *v;
169
170 r = PyTuple_New(2);
171 if (r == NULL)
Martin Panter6d57fe12016-09-17 03:26:16 +0000172 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000173
174 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_value)))) {
Martin Panter6d57fe12016-09-17 03:26:16 +0000175 Py_DECREF(r);
176 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000177 }
178
179 PyTuple_SET_ITEM(r, 0, v);
180
181 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_interval)))) {
Martin Panter6d57fe12016-09-17 03:26:16 +0000182 Py_DECREF(r);
183 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000184 }
185
186 PyTuple_SET_ITEM(r, 1, v);
187
188 return r;
189}
190#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000191
Eric Snow64d6cc82019-02-23 15:40:43 -0700192static int
Victor Stinnerd8613dc2019-05-24 13:43:55 +0200193is_main(_PyRuntimeState *runtime)
Eric Snow64d6cc82019-02-23 15:40:43 -0700194{
Victor Stinnerd8613dc2019-05-24 13:43:55 +0200195 unsigned long thread = PyThread_get_thread_ident();
196 PyInterpreterState *interp = _PyRuntimeState_GetThreadState(runtime)->interp;
197 return (thread == runtime->main_thread
198 && interp == runtime->interpreters.main);
Eric Snow64d6cc82019-02-23 15:40:43 -0700199}
200
Guido van Rossume4485b01994-09-07 14:32:49 +0000201static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000202signal_default_int_handler(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000203{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000204 PyErr_SetNone(PyExc_KeyboardInterrupt);
205 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000206}
207
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000208PyDoc_STRVAR(default_int_handler_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000209"default_int_handler(...)\n\
210\n\
Michael W. Hudson24ec2112004-06-17 15:55:53 +0000211The default handler for SIGINT installed by Python.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000212It raises KeyboardInterrupt.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000213
Thomas Wouters0796b002000-07-22 23:49:30 +0000214
215static int
Victor Stinner11517102014-07-29 23:31:34 +0200216report_wakeup_write_error(void *data)
Antoine Pitrou6f6ec372013-08-17 20:27:56 +0200217{
Eric Snowfdf282d2019-01-11 14:26:55 -0700218 PyObject *exc, *val, *tb;
Antoine Pitrou6f6ec372013-08-17 20:27:56 +0200219 int save_errno = errno;
Benjamin Petersonca470632016-09-06 13:47:26 -0700220 errno = (int) (intptr_t) data;
Eric Snowfdf282d2019-01-11 14:26:55 -0700221 PyErr_Fetch(&exc, &val, &tb);
Antoine Pitrou6f6ec372013-08-17 20:27:56 +0200222 PyErr_SetFromErrno(PyExc_OSError);
223 PySys_WriteStderr("Exception ignored when trying to write to the "
224 "signal wakeup fd:\n");
225 PyErr_WriteUnraisable(NULL);
Eric Snowfdf282d2019-01-11 14:26:55 -0700226 PyErr_Restore(exc, val, tb);
Antoine Pitrou6f6ec372013-08-17 20:27:56 +0200227 errno = save_errno;
228 return 0;
229}
230
Victor Stinner11517102014-07-29 23:31:34 +0200231#ifdef MS_WINDOWS
232static int
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800233report_wakeup_send_error(void* data)
Victor Stinner11517102014-07-29 23:31:34 +0200234{
Eric Snowfdf282d2019-01-11 14:26:55 -0700235 PyObject *exc, *val, *tb;
236 PyErr_Fetch(&exc, &val, &tb);
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800237 /* PyErr_SetExcFromWindowsErr() invokes FormatMessage() which
238 recognizes the error codes used by both GetLastError() and
239 WSAGetLastError */
240 PyErr_SetExcFromWindowsErr(PyExc_OSError, (int) (intptr_t) data);
Victor Stinner11517102014-07-29 23:31:34 +0200241 PySys_WriteStderr("Exception ignored when trying to send to the "
242 "signal wakeup fd:\n");
243 PyErr_WriteUnraisable(NULL);
Eric Snowfdf282d2019-01-11 14:26:55 -0700244 PyErr_Restore(exc, val, tb);
Victor Stinner11517102014-07-29 23:31:34 +0200245 return 0;
246}
247#endif /* MS_WINDOWS */
248
Tim Peters4f1b2082000-07-23 21:18:09 +0000249static void
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200250trip_signal(int sig_num)
251{
Victor Stinnerd49b1f12011-05-08 02:03:15 +0200252 unsigned char byte;
Victor Stinner11517102014-07-29 23:31:34 +0200253 int fd;
254 Py_ssize_t rc;
Victor Stinnerc13ef662011-05-25 02:35:58 +0200255
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +0200256 _Py_atomic_store_relaxed(&Handlers[sig_num].tripped, 1);
Victor Stinner11517102014-07-29 23:31:34 +0200257
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200258 /* Set is_tripped after setting .tripped, as it gets
259 cleared in PyErr_CheckSignals() before .tripped. */
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +0200260 _Py_atomic_store(&is_tripped, 1);
261
262 /* Notify ceval.c */
Victor Stinner09532fe2019-05-10 23:39:09 +0200263 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinner438a12d2019-05-24 17:01:38 +0200264 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Victor Stinner09532fe2019-05-10 23:39:09 +0200265 _PyEval_SignalReceived(&runtime->ceval);
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 Stinner11517102014-07-29 23:31:34 +0200286#ifdef MS_WINDOWS
287 fd = Py_SAFE_DOWNCAST(wakeup.fd, SOCKET_T, int);
288#else
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800289 fd = wakeup.fd;
Victor Stinner11517102014-07-29 23:31:34 +0200290#endif
291
292 if (fd != INVALID_FD) {
Victor Stinnerc13ef662011-05-25 02:35:58 +0200293 byte = (unsigned char)sig_num;
Victor Stinner11517102014-07-29 23:31:34 +0200294#ifdef MS_WINDOWS
295 if (wakeup.use_send) {
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800296 rc = send(fd, &byte, 1, 0);
Victor Stinner11517102014-07-29 23:31:34 +0200297
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800298 if (rc < 0) {
299 int last_error = GetLastError();
300 if (wakeup.warn_on_full_buffer ||
301 last_error != WSAEWOULDBLOCK)
302 {
303 /* Py_AddPendingCall() isn't signal-safe, but we
304 still use it for this exceptional case. */
Victor Stinnere225beb2019-06-03 18:14:24 +0200305 _PyEval_AddPendingCall(tstate, &runtime->ceval,
Victor Stinner09532fe2019-05-10 23:39:09 +0200306 report_wakeup_send_error,
307 (void *)(intptr_t) last_error);
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800308 }
Victor Stinner11517102014-07-29 23:31:34 +0200309 }
310 }
311 else
312#endif
313 {
Victor Stinnere72fe392015-04-01 18:35:22 +0200314 /* _Py_write_noraise() retries write() if write() is interrupted by
315 a signal (fails with EINTR). */
316 rc = _Py_write_noraise(fd, &byte, 1);
Victor Stinner11517102014-07-29 23:31:34 +0200317
318 if (rc < 0) {
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800319 if (wakeup.warn_on_full_buffer ||
320 (errno != EWOULDBLOCK && errno != EAGAIN))
321 {
322 /* Py_AddPendingCall() isn't signal-safe, but we
323 still use it for this exceptional case. */
Victor Stinnere225beb2019-06-03 18:14:24 +0200324 _PyEval_AddPendingCall(tstate, &runtime->ceval,
Victor Stinner09532fe2019-05-10 23:39:09 +0200325 report_wakeup_write_error,
326 (void *)(intptr_t)errno);
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800327 }
Victor Stinner11517102014-07-29 23:31:34 +0200328 }
329 }
Victor Stinnerc13ef662011-05-25 02:35:58 +0200330 }
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200331}
332
333static void
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000334signal_handler(int sig_num)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000335{
Antoine Pitrou39a65912010-11-05 19:47:27 +0000336 int save_errno = errno;
337
Jeroen Demeyerd237b3f2019-05-10 03:28:57 +0200338 trip_signal(sig_num);
Antoine Pitrou39a65912010-11-05 19:47:27 +0000339
Jean-Paul Calderone6f137ca2010-05-09 03:18:57 +0000340#ifndef HAVE_SIGACTION
Antoine Pitrou39a65912010-11-05 19:47:27 +0000341#ifdef SIGCHLD
342 /* To avoid infinite recursion, this signal remains
343 reset until explicit re-instated.
344 Don't clear the 'func' field as it is our pointer
345 to the Python handler... */
346 if (sig_num != SIGCHLD)
347#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000348 /* If the handler was not set up with sigaction, reinstall it. See
Nick Coghland6009512014-11-20 21:39:37 +1000349 * Python/pylifecycle.c for the implementation of PyOS_setsig which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000350 * makes this true. See also issue8354. */
351 PyOS_setsig(sig_num, signal_handler);
Jean-Paul Calderone6f137ca2010-05-09 03:18:57 +0000352#endif
Antoine Pitrou39a65912010-11-05 19:47:27 +0000353
354 /* Issue #10311: asynchronously executing signal handlers should not
355 mutate errno under the feet of unsuspecting C code. */
356 errno = save_errno;
Antoine Pitrou6dd381e2011-11-21 21:26:56 +0100357
358#ifdef MS_WINDOWS
359 if (sig_num == SIGINT)
360 SetEvent(sigint_event);
361#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000362}
Guido van Rossume4485b01994-09-07 14:32:49 +0000363
Guido van Rossum06d511d1995-03-10 15:13:48 +0000364
Guido van Rossum1171ee61997-08-22 20:42:00 +0000365#ifdef HAVE_ALARM
Tal Einatc7027b72015-05-16 14:14:49 +0300366
367/*[clinic input]
368signal.alarm -> long
369
370 seconds: int
371 /
372
373Arrange for SIGALRM to arrive after the given number of seconds.
374[clinic start generated code]*/
375
376static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300377signal_alarm_impl(PyObject *module, int seconds)
378/*[clinic end generated code: output=144232290814c298 input=0d5e97e0e6f39e86]*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000379{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000380 /* alarm() returns the number of seconds remaining */
Tal Einatc7027b72015-05-16 14:14:49 +0300381 return (long)alarm(seconds);
Guido van Rossumaa0f4c71994-08-23 13:49:37 +0000382}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000383
Guido van Rossum06d511d1995-03-10 15:13:48 +0000384#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000385
Guido van Rossum1171ee61997-08-22 20:42:00 +0000386#ifdef HAVE_PAUSE
Tal Einatc7027b72015-05-16 14:14:49 +0300387
388/*[clinic input]
389signal.pause
390
391Wait until a signal arrives.
392[clinic start generated code]*/
393
Guido van Rossuma597dde1995-01-10 20:56:29 +0000394static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300395signal_pause_impl(PyObject *module)
396/*[clinic end generated code: output=391656788b3c3929 input=f03de0f875752062]*/
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000397{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000398 Py_BEGIN_ALLOW_THREADS
399 (void)pause();
400 Py_END_ALLOW_THREADS
401 /* make sure that any exceptions that got raised are propagated
402 * back into Python
403 */
404 if (PyErr_CheckSignals())
405 return NULL;
Barry Warsaw92971171997-01-03 00:14:25 +0000406
Tal Einatc7027b72015-05-16 14:14:49 +0300407 Py_RETURN_NONE;
Guido van Rossume4485b01994-09-07 14:32:49 +0000408}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000409
Guido van Rossum06d511d1995-03-10 15:13:48 +0000410#endif
Guido van Rossume4485b01994-09-07 14:32:49 +0000411
Vladimir Matveevc24c6c22019-01-08 01:58:25 -0800412/*[clinic input]
413signal.raise_signal
414
415 signalnum: int
416 /
417
418Send a signal to the executing process.
419[clinic start generated code]*/
420
421static PyObject *
422signal_raise_signal_impl(PyObject *module, int signalnum)
423/*[clinic end generated code: output=e2b014220aa6111d input=e90c0f9a42358de6]*/
424{
425 int err;
426 Py_BEGIN_ALLOW_THREADS
427 _Py_BEGIN_SUPPRESS_IPH
428 err = raise(signalnum);
429 _Py_END_SUPPRESS_IPH
430 Py_END_ALLOW_THREADS
Victor Stinner09532fe2019-05-10 23:39:09 +0200431
Vladimir Matveevc24c6c22019-01-08 01:58:25 -0800432 if (err) {
433 return PyErr_SetFromErrno(PyExc_OSError);
434 }
435 Py_RETURN_NONE;
436}
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000437
Tal Einatc7027b72015-05-16 14:14:49 +0300438/*[clinic input]
439signal.signal
440
441 signalnum: int
442 handler: object
443 /
444
445Set the action for the given signal.
446
447The action can be SIG_DFL, SIG_IGN, or a callable Python object.
448The previous action is returned. See getsignal() for possible return values.
449
450*** IMPORTANT NOTICE ***
451A signal handler function is called with two arguments:
452the first is the signal number, the second is the interrupted stack frame.
453[clinic start generated code]*/
454
Guido van Rossume4485b01994-09-07 14:32:49 +0000455static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300456signal_signal_impl(PyObject *module, int signalnum, PyObject *handler)
457/*[clinic end generated code: output=b44cfda43780f3a1 input=deee84af5fa0432c]*/
Guido van Rossume4485b01994-09-07 14:32:49 +0000458{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000459 PyObject *old_handler;
460 void (*func)(int);
Brian Curtinef9efbd2010-08-06 19:27:32 +0000461#ifdef MS_WINDOWS
Tal Einatc7027b72015-05-16 14:14:49 +0300462 /* Validate that signalnum is one of the allowable signals */
463 switch (signalnum) {
Brian Curtinc734b312010-09-06 16:04:10 +0000464 case SIGABRT: break;
Brian Curtin9e88b5a2010-10-01 14:49:24 +0000465#ifdef SIGBREAK
466 /* Issue #10003: SIGBREAK is not documented as permitted, but works
467 and corresponds to CTRL_BREAK_EVENT. */
468 case SIGBREAK: break;
469#endif
Brian Curtinc734b312010-09-06 16:04:10 +0000470 case SIGFPE: break;
471 case SIGILL: break;
472 case SIGINT: break;
473 case SIGSEGV: break;
474 case SIGTERM: break;
475 default:
476 PyErr_SetString(PyExc_ValueError, "invalid signal value");
477 return NULL;
Brian Curtinef9efbd2010-08-06 19:27:32 +0000478 }
479#endif
Victor Stinnerd8613dc2019-05-24 13:43:55 +0200480
481 _PyRuntimeState *runtime = &_PyRuntime;
482 if (!is_main(runtime)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000483 PyErr_SetString(PyExc_ValueError,
484 "signal only works in main thread");
485 return NULL;
486 }
Tal Einatc7027b72015-05-16 14:14:49 +0300487 if (signalnum < 1 || signalnum >= NSIG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000488 PyErr_SetString(PyExc_ValueError,
489 "signal number out of range");
490 return NULL;
491 }
Tal Einatc7027b72015-05-16 14:14:49 +0300492 if (handler == IgnoreHandler)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000493 func = SIG_IGN;
Tal Einatc7027b72015-05-16 14:14:49 +0300494 else if (handler == DefaultHandler)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000495 func = SIG_DFL;
Tal Einatc7027b72015-05-16 14:14:49 +0300496 else if (!PyCallable_Check(handler)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 PyErr_SetString(PyExc_TypeError,
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000498"signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 return NULL;
500 }
501 else
502 func = signal_handler;
Antoine Pitrouf6f90ff2017-11-03 19:58:46 +0100503 /* Check for pending signals before changing signal handler */
Eric Snow64d6cc82019-02-23 15:40:43 -0700504 if (_PyErr_CheckSignals()) {
Antoine Pitrouf6f90ff2017-11-03 19:58:46 +0100505 return NULL;
506 }
Tal Einatc7027b72015-05-16 14:14:49 +0300507 if (PyOS_setsig(signalnum, func) == SIG_ERR) {
Victor Stinner388196e2011-05-10 17:13:00 +0200508 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000509 return NULL;
510 }
Tal Einatc7027b72015-05-16 14:14:49 +0300511 old_handler = Handlers[signalnum].func;
Tal Einatc7027b72015-05-16 14:14:49 +0300512 Py_INCREF(handler);
513 Handlers[signalnum].func = handler;
Antoine Pitrouc8c952c2013-05-04 23:16:59 +0200514 if (old_handler != NULL)
515 return old_handler;
516 else
517 Py_RETURN_NONE;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000518}
519
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000520
Tal Einatc7027b72015-05-16 14:14:49 +0300521/*[clinic input]
522signal.getsignal
523
524 signalnum: int
525 /
526
527Return the current action for the given signal.
528
529The return value can be:
530 SIG_IGN -- if the signal is being ignored
531 SIG_DFL -- if the default action for the signal is in effect
532 None -- if an unknown handler is in effect
533 anything else -- the callable Python object used as a handler
534[clinic start generated code]*/
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000535
Guido van Rossume4485b01994-09-07 14:32:49 +0000536static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300537signal_getsignal_impl(PyObject *module, int signalnum)
538/*[clinic end generated code: output=35b3e0e796fd555e input=ac23a00f19dfa509]*/
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000539{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000540 PyObject *old_handler;
Tal Einatc7027b72015-05-16 14:14:49 +0300541 if (signalnum < 1 || signalnum >= NSIG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000542 PyErr_SetString(PyExc_ValueError,
543 "signal number out of range");
544 return NULL;
545 }
Tal Einatc7027b72015-05-16 14:14:49 +0300546 old_handler = Handlers[signalnum].func;
Antoine Pitrouc8c952c2013-05-04 23:16:59 +0200547 if (old_handler != NULL) {
548 Py_INCREF(old_handler);
549 return old_handler;
550 }
551 else {
552 Py_RETURN_NONE;
553 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000554}
555
Antoine Pietri5d2a27d2018-03-12 14:42:34 +0100556
557/*[clinic input]
558signal.strsignal
559
560 signalnum: int
561 /
562
563Return the system description of the given signal.
564
565The return values can be such as "Interrupt", "Segmentation fault", etc.
566Returns None if the signal is not recognized.
567[clinic start generated code]*/
568
569static PyObject *
570signal_strsignal_impl(PyObject *module, int signalnum)
571/*[clinic end generated code: output=44e12e1e3b666261 input=b77914b03f856c74]*/
572{
573 char *res;
574
575 if (signalnum < 1 || signalnum >= NSIG) {
576 PyErr_SetString(PyExc_ValueError,
577 "signal number out of range");
578 return NULL;
579 }
580
Michael Osipov48ce4892018-08-23 15:27:19 +0200581#ifndef HAVE_STRSIGNAL
Antoine Pietri5d2a27d2018-03-12 14:42:34 +0100582 switch (signalnum) {
Michael Osipov48ce4892018-08-23 15:27:19 +0200583 /* Though being a UNIX, HP-UX does not provide strsignal(3). */
584#ifndef MS_WINDOWS
585 case SIGHUP:
586 res = "Hangup";
587 break;
588 case SIGALRM:
589 res = "Alarm clock";
590 break;
591 case SIGPIPE:
592 res = "Broken pipe";
593 break;
594 case SIGQUIT:
595 res = "Quit";
596 break;
597 case SIGCHLD:
598 res = "Child exited";
599 break;
600#endif
601 /* Custom redefinition of POSIX signals allowed on Windows. */
Antoine Pietri5d2a27d2018-03-12 14:42:34 +0100602 case SIGINT:
603 res = "Interrupt";
604 break;
605 case SIGILL:
606 res = "Illegal instruction";
607 break;
608 case SIGABRT:
609 res = "Aborted";
610 break;
611 case SIGFPE:
612 res = "Floating point exception";
613 break;
614 case SIGSEGV:
615 res = "Segmentation fault";
616 break;
617 case SIGTERM:
618 res = "Terminated";
619 break;
620 default:
621 Py_RETURN_NONE;
622 }
623#else
624 errno = 0;
625 res = strsignal(signalnum);
626
627 if (errno || res == NULL || strstr(res, "Unknown signal") != NULL)
628 Py_RETURN_NONE;
629#endif
630
631 return Py_BuildValue("s", res);
632}
633
Christian Heimes8640e742008-02-23 16:23:06 +0000634#ifdef HAVE_SIGINTERRUPT
Tal Einatc7027b72015-05-16 14:14:49 +0300635
636/*[clinic input]
637signal.siginterrupt
638
639 signalnum: int
640 flag: int
641 /
642
643Change system call restart behaviour.
644
645If flag is False, system calls will be restarted when interrupted by
646signal sig, else system calls will be interrupted.
647[clinic start generated code]*/
Christian Heimes8640e742008-02-23 16:23:06 +0000648
649static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300650signal_siginterrupt_impl(PyObject *module, int signalnum, int flag)
651/*[clinic end generated code: output=063816243d85dd19 input=4160acacca3e2099]*/
Christian Heimes8640e742008-02-23 16:23:06 +0000652{
Tal Einatc7027b72015-05-16 14:14:49 +0300653 if (signalnum < 1 || signalnum >= NSIG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000654 PyErr_SetString(PyExc_ValueError,
655 "signal number out of range");
656 return NULL;
657 }
Tal Einatc7027b72015-05-16 14:14:49 +0300658 if (siginterrupt(signalnum, flag)<0) {
Victor Stinner388196e2011-05-10 17:13:00 +0200659 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000660 return NULL;
661 }
Tal Einatc7027b72015-05-16 14:14:49 +0300662 Py_RETURN_NONE;
Christian Heimes8640e742008-02-23 16:23:06 +0000663}
664
665#endif
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000666
Tal Einatc7027b72015-05-16 14:14:49 +0300667
668static PyObject*
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800669signal_set_wakeup_fd(PyObject *self, PyObject *args, PyObject *kwds)
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000670{
Victor Stinnere134a7f2015-03-30 10:09:31 +0200671 struct _Py_stat_struct status;
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800672 static char *kwlist[] = {
673 "", "warn_on_full_buffer", NULL,
674 };
675 int warn_on_full_buffer = 1;
Victor Stinner11517102014-07-29 23:31:34 +0200676#ifdef MS_WINDOWS
677 PyObject *fdobj;
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100678 SOCKET_T sockfd, old_sockfd;
Victor Stinner11517102014-07-29 23:31:34 +0200679 int res;
680 int res_size = sizeof res;
681 PyObject *mod;
Victor Stinner11517102014-07-29 23:31:34 +0200682 int is_socket;
683
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800684 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|$p:set_wakeup_fd", kwlist,
685 &fdobj, &warn_on_full_buffer))
Victor Stinner11517102014-07-29 23:31:34 +0200686 return NULL;
687
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100688 sockfd = PyLong_AsSocket_t(fdobj);
689 if (sockfd == (SOCKET_T)(-1) && PyErr_Occurred())
Victor Stinner11517102014-07-29 23:31:34 +0200690 return NULL;
691#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000692 int fd, old_fd;
Victor Stinner11517102014-07-29 23:31:34 +0200693
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800694 if (!PyArg_ParseTupleAndKeywords(args, kwds, "i|$p:set_wakeup_fd", kwlist,
695 &fd, &warn_on_full_buffer))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000696 return NULL;
Victor Stinner11517102014-07-29 23:31:34 +0200697#endif
698
Victor Stinnerd8613dc2019-05-24 13:43:55 +0200699 _PyRuntimeState *runtime = &_PyRuntime;
700 if (!is_main(runtime)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000701 PyErr_SetString(PyExc_ValueError,
702 "set_wakeup_fd only works in main thread");
703 return NULL;
704 }
Victor Stinner0bffc942014-07-21 16:28:54 +0200705
Victor Stinner11517102014-07-29 23:31:34 +0200706#ifdef MS_WINDOWS
707 is_socket = 0;
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100708 if (sockfd != INVALID_FD) {
Victor Stinner11517102014-07-29 23:31:34 +0200709 /* Import the _socket module to call WSAStartup() */
710 mod = PyImport_ImportModuleNoBlock("_socket");
711 if (mod == NULL)
712 return NULL;
713 Py_DECREF(mod);
714
715 /* test the socket */
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100716 if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR,
Victor Stinner11517102014-07-29 23:31:34 +0200717 (char *)&res, &res_size) != 0) {
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100718 int fd, err;
719
720 err = WSAGetLastError();
Victor Stinner11517102014-07-29 23:31:34 +0200721 if (err != WSAENOTSOCK) {
722 PyErr_SetExcFromWindowsErr(PyExc_OSError, err);
723 return NULL;
724 }
725
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100726 fd = (int)sockfd;
Steve Dower940f33a2016-09-08 11:21:54 -0700727 if ((SOCKET_T)fd != sockfd) {
Victor Stinner11517102014-07-29 23:31:34 +0200728 PyErr_SetString(PyExc_ValueError, "invalid fd");
729 return NULL;
730 }
731
Victor Stinnere134a7f2015-03-30 10:09:31 +0200732 if (_Py_fstat(fd, &status) != 0)
Victor Stinner11517102014-07-29 23:31:34 +0200733 return NULL;
Victor Stinner38227602014-08-27 12:59:44 +0200734
735 /* on Windows, a file cannot be set to non-blocking mode */
Victor Stinner11517102014-07-29 23:31:34 +0200736 }
Victor Stinner38227602014-08-27 12:59:44 +0200737 else {
Victor Stinner11517102014-07-29 23:31:34 +0200738 is_socket = 1;
Victor Stinner38227602014-08-27 12:59:44 +0200739
740 /* Windows does not provide a function to test if a socket
741 is in non-blocking mode */
742 }
Victor Stinner11517102014-07-29 23:31:34 +0200743 }
744
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100745 old_sockfd = wakeup.fd;
746 wakeup.fd = sockfd;
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800747 wakeup.warn_on_full_buffer = warn_on_full_buffer;
Victor Stinner11517102014-07-29 23:31:34 +0200748 wakeup.use_send = is_socket;
749
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100750 if (old_sockfd != INVALID_FD)
751 return PyLong_FromSocket_t(old_sockfd);
Victor Stinner11517102014-07-29 23:31:34 +0200752 else
753 return PyLong_FromLong(-1);
754#else
Victor Stinnerd18ccd12014-07-24 21:58:53 +0200755 if (fd != -1) {
Victor Stinner38227602014-08-27 12:59:44 +0200756 int blocking;
757
Victor Stinnere134a7f2015-03-30 10:09:31 +0200758 if (_Py_fstat(fd, &status) != 0)
Victor Stinner11517102014-07-29 23:31:34 +0200759 return NULL;
Victor Stinner38227602014-08-27 12:59:44 +0200760
761 blocking = _Py_get_blocking(fd);
762 if (blocking < 0)
763 return NULL;
764 if (blocking) {
765 PyErr_Format(PyExc_ValueError,
766 "the fd %i must be in non-blocking mode",
767 fd);
768 return NULL;
769 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000770 }
Victor Stinner0bffc942014-07-21 16:28:54 +0200771
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800772 old_fd = wakeup.fd;
773 wakeup.fd = fd;
774 wakeup.warn_on_full_buffer = warn_on_full_buffer;
Victor Stinner0bffc942014-07-21 16:28:54 +0200775
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000776 return PyLong_FromLong(old_fd);
Victor Stinner11517102014-07-29 23:31:34 +0200777#endif
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000778}
779
780PyDoc_STRVAR(set_wakeup_fd_doc,
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800781"set_wakeup_fd(fd, *, warn_on_full_buffer=True) -> fd\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000782\n\
Victor Stinner11517102014-07-29 23:31:34 +0200783Sets the fd to be written to (with the signal number) when a signal\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000784comes in. A library can use this to wakeup select or poll.\n\
Victor Stinner11517102014-07-29 23:31:34 +0200785The previous fd or -1 is returned.\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000786\n\
787The fd must be non-blocking.");
788
789/* C API for the same, without all the error checking */
790int
791PySignal_SetWakeupFd(int fd)
792{
Victor Stinner11517102014-07-29 23:31:34 +0200793 int old_fd;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000794 if (fd < 0)
795 fd = -1;
Victor Stinner11517102014-07-29 23:31:34 +0200796
797#ifdef MS_WINDOWS
798 old_fd = Py_SAFE_DOWNCAST(wakeup.fd, SOCKET_T, int);
Victor Stinner11517102014-07-29 23:31:34 +0200799#else
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800800 old_fd = wakeup.fd;
Victor Stinner11517102014-07-29 23:31:34 +0200801#endif
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800802 wakeup.fd = fd;
803 wakeup.warn_on_full_buffer = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000804 return old_fd;
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000805}
806
807
Martin v. Löwis823725e2008-03-24 13:39:54 +0000808#ifdef HAVE_SETITIMER
Tal Einatc7027b72015-05-16 14:14:49 +0300809
810/*[clinic input]
811signal.setitimer
812
813 which: int
Victor Stinneref611c92017-10-13 13:49:43 -0700814 seconds: object
815 interval: object(c_default="NULL") = 0.0
Tal Einatc7027b72015-05-16 14:14:49 +0300816 /
817
818Sets given itimer (one of ITIMER_REAL, ITIMER_VIRTUAL or ITIMER_PROF).
819
820The timer will fire after value seconds and after that every interval seconds.
821The itimer can be cleared by setting seconds to zero.
822
823Returns old values as a tuple: (delay, interval).
824[clinic start generated code]*/
825
Martin v. Löwis823725e2008-03-24 13:39:54 +0000826static PyObject *
Victor Stinneref611c92017-10-13 13:49:43 -0700827signal_setitimer_impl(PyObject *module, int which, PyObject *seconds,
828 PyObject *interval)
829/*[clinic end generated code: output=65f9dcbddc35527b input=de43daf194e6f66f]*/
Martin v. Löwis823725e2008-03-24 13:39:54 +0000830{
Martin v. Löwis823725e2008-03-24 13:39:54 +0000831 struct itimerval new, old;
832
Victor Stinneref611c92017-10-13 13:49:43 -0700833 if (timeval_from_double(seconds, &new.it_value) < 0) {
834 return NULL;
835 }
836 if (timeval_from_double(interval, &new.it_interval) < 0) {
837 return NULL;
838 }
839
Martin v. Löwis823725e2008-03-24 13:39:54 +0000840 /* Let OS check "which" value */
841 if (setitimer(which, &new, &old) != 0) {
Tal Einatc7027b72015-05-16 14:14:49 +0300842 PyErr_SetFromErrno(ItimerError);
843 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000844 }
845
846 return itimer_retval(&old);
847}
848
Martin v. Löwis823725e2008-03-24 13:39:54 +0000849#endif
850
851
852#ifdef HAVE_GETITIMER
Tal Einatc7027b72015-05-16 14:14:49 +0300853
854/*[clinic input]
855signal.getitimer
856
857 which: int
858 /
859
860Returns current value of given itimer.
861[clinic start generated code]*/
862
Martin v. Löwis823725e2008-03-24 13:39:54 +0000863static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300864signal_getitimer_impl(PyObject *module, int which)
865/*[clinic end generated code: output=9e053175d517db40 input=f7d21d38f3490627]*/
Martin v. Löwis823725e2008-03-24 13:39:54 +0000866{
Martin v. Löwis823725e2008-03-24 13:39:54 +0000867 struct itimerval old;
868
Martin v. Löwis823725e2008-03-24 13:39:54 +0000869 if (getitimer(which, &old) != 0) {
Tal Einatc7027b72015-05-16 14:14:49 +0300870 PyErr_SetFromErrno(ItimerError);
871 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000872 }
873
874 return itimer_retval(&old);
875}
876
Martin v. Löwis823725e2008-03-24 13:39:54 +0000877#endif
878
Victor Stinnerb3e72192011-05-08 01:46:11 +0200879#if defined(PYPTHREAD_SIGMASK) || defined(HAVE_SIGPENDING)
Victor Stinner35b300c2011-05-04 13:20:35 +0200880static PyObject*
881sigset_to_set(sigset_t mask)
882{
883 PyObject *signum, *result;
884 int sig;
885
886 result = PySet_New(0);
887 if (result == NULL)
888 return NULL;
889
890 for (sig = 1; sig < NSIG; sig++) {
891 if (sigismember(&mask, sig) != 1)
892 continue;
893
894 /* Handle the case where it is a member by adding the signal to
895 the result list. Ignore the other cases because they mean the
896 signal isn't a member of the mask or the signal was invalid,
897 and an invalid signal must have been our fault in constructing
898 the loop boundaries. */
899 signum = PyLong_FromLong(sig);
900 if (signum == NULL) {
901 Py_DECREF(result);
902 return NULL;
903 }
904 if (PySet_Add(result, signum) == -1) {
905 Py_DECREF(signum);
906 Py_DECREF(result);
907 return NULL;
908 }
909 Py_DECREF(signum);
910 }
911 return result;
912}
Victor Stinnerb3e72192011-05-08 01:46:11 +0200913#endif
Victor Stinner35b300c2011-05-04 13:20:35 +0200914
Victor Stinnerb3e72192011-05-08 01:46:11 +0200915#ifdef PYPTHREAD_SIGMASK
Tal Einatc7027b72015-05-16 14:14:49 +0300916
917/*[clinic input]
918signal.pthread_sigmask
919
920 how: int
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300921 mask: sigset_t
Tal Einatc7027b72015-05-16 14:14:49 +0300922 /
923
924Fetch and/or change the signal mask of the calling thread.
925[clinic start generated code]*/
926
Victor Stinnera9293352011-04-30 15:21:58 +0200927static PyObject *
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300928signal_pthread_sigmask_impl(PyObject *module, int how, sigset_t mask)
929/*[clinic end generated code: output=0562c0fb192981a8 input=85bcebda442fa77f]*/
Victor Stinnera9293352011-04-30 15:21:58 +0200930{
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300931 sigset_t previous;
Victor Stinnera9293352011-04-30 15:21:58 +0200932 int err;
933
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300934 err = pthread_sigmask(how, &mask, &previous);
Victor Stinnera9293352011-04-30 15:21:58 +0200935 if (err != 0) {
936 errno = err;
Victor Stinnerb3e72192011-05-08 01:46:11 +0200937 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnera9293352011-04-30 15:21:58 +0200938 return NULL;
939 }
940
Victor Stinnerd0e516d2011-05-03 14:57:12 +0200941 /* if signals was unblocked, signal handlers have been called */
942 if (PyErr_CheckSignals())
943 return NULL;
944
Victor Stinner35b300c2011-05-04 13:20:35 +0200945 return sigset_to_set(previous);
Victor Stinnera9293352011-04-30 15:21:58 +0200946}
947
Victor Stinnera9293352011-04-30 15:21:58 +0200948#endif /* #ifdef PYPTHREAD_SIGMASK */
949
Martin v. Löwis823725e2008-03-24 13:39:54 +0000950
Victor Stinnerb3e72192011-05-08 01:46:11 +0200951#ifdef HAVE_SIGPENDING
Tal Einatc7027b72015-05-16 14:14:49 +0300952
953/*[clinic input]
954signal.sigpending
955
956Examine pending signals.
957
958Returns a set of signal numbers that are pending for delivery to
959the calling thread.
960[clinic start generated code]*/
961
Victor Stinnerb3e72192011-05-08 01:46:11 +0200962static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300963signal_sigpending_impl(PyObject *module)
964/*[clinic end generated code: output=53375ffe89325022 input=e0036c016f874e29]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +0200965{
966 int err;
967 sigset_t mask;
968 err = sigpending(&mask);
969 if (err)
970 return PyErr_SetFromErrno(PyExc_OSError);
971 return sigset_to_set(mask);
972}
973
Victor Stinnerb3e72192011-05-08 01:46:11 +0200974#endif /* #ifdef HAVE_SIGPENDING */
975
976
977#ifdef HAVE_SIGWAIT
Tal Einatc7027b72015-05-16 14:14:49 +0300978
979/*[clinic input]
980signal.sigwait
981
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300982 sigset: sigset_t
Tal Einatc7027b72015-05-16 14:14:49 +0300983 /
984
985Wait for a signal.
986
987Suspend execution of the calling thread until the delivery of one of the
988signals specified in the signal set sigset. The function accepts the signal
989and returns the signal number.
990[clinic start generated code]*/
991
Victor Stinnerb3e72192011-05-08 01:46:11 +0200992static PyObject *
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300993signal_sigwait_impl(PyObject *module, sigset_t sigset)
994/*[clinic end generated code: output=f43770699d682f96 input=a6fbd47b1086d119]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +0200995{
Victor Stinnerb3e72192011-05-08 01:46:11 +0200996 int err, signum;
997
Victor Stinner10c30d62011-06-10 01:39:53 +0200998 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300999 err = sigwait(&sigset, &signum);
Victor Stinner10c30d62011-06-10 01:39:53 +02001000 Py_END_ALLOW_THREADS
Victor Stinnerb3e72192011-05-08 01:46:11 +02001001 if (err) {
1002 errno = err;
1003 return PyErr_SetFromErrno(PyExc_OSError);
1004 }
1005
1006 return PyLong_FromLong(signum);
1007}
1008
Tal Einatc7027b72015-05-16 14:14:49 +03001009#endif /* #ifdef HAVE_SIGWAIT */
1010
Victor Stinnerb3e72192011-05-08 01:46:11 +02001011
Antoine Pitrou9d3627e2018-05-04 13:00:50 +02001012#if defined(HAVE_SIGFILLSET) || defined(MS_WINDOWS)
1013
1014/*[clinic input]
1015signal.valid_signals
1016
1017Return a set of valid signal numbers on this platform.
1018
1019The signal numbers returned by this function can be safely passed to
1020functions like `pthread_sigmask`.
1021[clinic start generated code]*/
1022
1023static PyObject *
1024signal_valid_signals_impl(PyObject *module)
1025/*[clinic end generated code: output=1609cffbcfcf1314 input=86a3717ff25288f2]*/
1026{
1027#ifdef MS_WINDOWS
1028#ifdef SIGBREAK
1029 PyObject *tup = Py_BuildValue("(iiiiiii)", SIGABRT, SIGBREAK, SIGFPE,
1030 SIGILL, SIGINT, SIGSEGV, SIGTERM);
1031#else
1032 PyObject *tup = Py_BuildValue("(iiiiii)", SIGABRT, SIGFPE, SIGILL,
1033 SIGINT, SIGSEGV, SIGTERM);
1034#endif
1035 if (tup == NULL) {
1036 return NULL;
1037 }
1038 PyObject *set = PySet_New(tup);
1039 Py_DECREF(tup);
1040 return set;
1041#else
1042 sigset_t mask;
1043 if (sigemptyset(&mask) || sigfillset(&mask)) {
1044 return PyErr_SetFromErrno(PyExc_OSError);
1045 }
1046 return sigset_to_set(mask);
1047#endif
1048}
1049
1050#endif /* #if defined(HAVE_SIGFILLSET) || defined(MS_WINDOWS) */
1051
1052
Ross Lagerwallbc808222011-06-25 12:13:40 +02001053#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
1054static int initialized;
1055static PyStructSequence_Field struct_siginfo_fields[] = {
1056 {"si_signo", "signal number"},
1057 {"si_code", "signal code"},
1058 {"si_errno", "errno associated with this signal"},
1059 {"si_pid", "sending process ID"},
1060 {"si_uid", "real user ID of sending process"},
1061 {"si_status", "exit value or signal"},
1062 {"si_band", "band event for SIGPOLL"},
1063 {0}
1064};
1065
1066PyDoc_STRVAR(struct_siginfo__doc__,
1067"struct_siginfo: Result from sigwaitinfo or sigtimedwait.\n\n\
1068This object may be accessed either as a tuple of\n\
1069(si_signo, si_code, si_errno, si_pid, si_uid, si_status, si_band),\n\
1070or via the attributes si_signo, si_code, and so on.");
1071
1072static PyStructSequence_Desc struct_siginfo_desc = {
1073 "signal.struct_siginfo", /* name */
1074 struct_siginfo__doc__, /* doc */
1075 struct_siginfo_fields, /* fields */
1076 7 /* n_in_sequence */
1077};
1078
1079static PyTypeObject SiginfoType;
1080
1081static PyObject *
1082fill_siginfo(siginfo_t *si)
1083{
1084 PyObject *result = PyStructSequence_New(&SiginfoType);
1085 if (!result)
1086 return NULL;
1087
1088 PyStructSequence_SET_ITEM(result, 0, PyLong_FromLong((long)(si->si_signo)));
1089 PyStructSequence_SET_ITEM(result, 1, PyLong_FromLong((long)(si->si_code)));
Victor Stinner09532fe2019-05-10 23:39:09 +02001090#ifdef __VXWORKS__
pxinwr8b5bdda2019-03-14 01:18:25 +08001091 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong(0L));
1092 PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong(0L));
1093 PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong(0L));
1094 PyStructSequence_SET_ITEM(result, 5, PyLong_FromLong(0L));
Victor Stinner09532fe2019-05-10 23:39:09 +02001095#else
Ross Lagerwallbc808222011-06-25 12:13:40 +02001096 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si->si_errno)));
1097 PyStructSequence_SET_ITEM(result, 3, PyLong_FromPid(si->si_pid));
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02001098 PyStructSequence_SET_ITEM(result, 4, _PyLong_FromUid(si->si_uid));
Ross Lagerwallbc808222011-06-25 12:13:40 +02001099 PyStructSequence_SET_ITEM(result, 5,
1100 PyLong_FromLong((long)(si->si_status)));
Victor Stinner09532fe2019-05-10 23:39:09 +02001101#endif
Zachary Ware6a6967e2016-10-01 00:47:27 -05001102#ifdef HAVE_SIGINFO_T_SI_BAND
Ross Lagerwallbc808222011-06-25 12:13:40 +02001103 PyStructSequence_SET_ITEM(result, 6, PyLong_FromLong(si->si_band));
Zachary Ware6a6967e2016-10-01 00:47:27 -05001104#else
1105 PyStructSequence_SET_ITEM(result, 6, PyLong_FromLong(0L));
1106#endif
Ross Lagerwallbc808222011-06-25 12:13:40 +02001107 if (PyErr_Occurred()) {
1108 Py_DECREF(result);
1109 return NULL;
1110 }
1111
1112 return result;
1113}
1114#endif
1115
1116#ifdef HAVE_SIGWAITINFO
Tal Einatc7027b72015-05-16 14:14:49 +03001117
1118/*[clinic input]
1119signal.sigwaitinfo
1120
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001121 sigset: sigset_t
Tal Einatc7027b72015-05-16 14:14:49 +03001122 /
1123
1124Wait synchronously until one of the signals in *sigset* is delivered.
1125
1126Returns a struct_siginfo containing information about the signal.
1127[clinic start generated code]*/
1128
Ross Lagerwallbc808222011-06-25 12:13:40 +02001129static PyObject *
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001130signal_sigwaitinfo_impl(PyObject *module, sigset_t sigset)
1131/*[clinic end generated code: output=1eb2f1fa236fdbca input=3d1a7e1f27fc664c]*/
Ross Lagerwallbc808222011-06-25 12:13:40 +02001132{
Ross Lagerwallbc808222011-06-25 12:13:40 +02001133 siginfo_t si;
1134 int err;
Victor Stinnera453cd82015-03-20 12:54:28 +01001135 int async_err = 0;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001136
Victor Stinnera453cd82015-03-20 12:54:28 +01001137 do {
1138 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001139 err = sigwaitinfo(&sigset, &si);
Victor Stinnera453cd82015-03-20 12:54:28 +01001140 Py_END_ALLOW_THREADS
1141 } while (err == -1
1142 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Ross Lagerwallbc808222011-06-25 12:13:40 +02001143 if (err == -1)
Victor Stinnera453cd82015-03-20 12:54:28 +01001144 return (!async_err) ? PyErr_SetFromErrno(PyExc_OSError) : NULL;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001145
1146 return fill_siginfo(&si);
1147}
1148
Ross Lagerwallbc808222011-06-25 12:13:40 +02001149#endif /* #ifdef HAVE_SIGWAITINFO */
1150
1151#ifdef HAVE_SIGTIMEDWAIT
Tal Einatc7027b72015-05-16 14:14:49 +03001152
1153/*[clinic input]
1154signal.sigtimedwait
1155
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001156 sigset: sigset_t
Serhiy Storchaka6b680cd2015-05-16 15:57:56 +03001157 timeout as timeout_obj: object
Tal Einatc7027b72015-05-16 14:14:49 +03001158 /
1159
1160Like sigwaitinfo(), but with a timeout.
1161
1162The timeout is specified in seconds, with floating point numbers allowed.
1163[clinic start generated code]*/
1164
Ross Lagerwallbc808222011-06-25 12:13:40 +02001165static PyObject *
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001166signal_sigtimedwait_impl(PyObject *module, sigset_t sigset,
Serhiy Storchaka6b680cd2015-05-16 15:57:56 +03001167 PyObject *timeout_obj)
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001168/*[clinic end generated code: output=59c8971e8ae18a64 input=87fd39237cf0b7ba]*/
Ross Lagerwallbc808222011-06-25 12:13:40 +02001169{
Victor Stinnera453cd82015-03-20 12:54:28 +01001170 struct timespec ts;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001171 siginfo_t si;
Victor Stinnera453cd82015-03-20 12:54:28 +01001172 int res;
Victor Stinner34dc0f42015-03-27 18:19:03 +01001173 _PyTime_t timeout, deadline, monotonic;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001174
Victor Stinner869e1772015-03-30 03:49:14 +02001175 if (_PyTime_FromSecondsObject(&timeout,
1176 timeout_obj, _PyTime_ROUND_CEILING) < 0)
Ross Lagerwallbc808222011-06-25 12:13:40 +02001177 return NULL;
1178
Victor Stinnera453cd82015-03-20 12:54:28 +01001179 if (timeout < 0) {
Ross Lagerwallbc808222011-06-25 12:13:40 +02001180 PyErr_SetString(PyExc_ValueError, "timeout must be non-negative");
1181 return NULL;
1182 }
1183
Victor Stinner34dc0f42015-03-27 18:19:03 +01001184 deadline = _PyTime_GetMonotonicClock() + timeout;
Victor Stinnera453cd82015-03-20 12:54:28 +01001185
1186 do {
Victor Stinner34dc0f42015-03-27 18:19:03 +01001187 if (_PyTime_AsTimespec(timeout, &ts) < 0)
1188 return NULL;
Victor Stinnera453cd82015-03-20 12:54:28 +01001189
1190 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001191 res = sigtimedwait(&sigset, &si, &ts);
Victor Stinnera453cd82015-03-20 12:54:28 +01001192 Py_END_ALLOW_THREADS
1193
1194 if (res != -1)
1195 break;
1196
1197 if (errno != EINTR) {
1198 if (errno == EAGAIN)
1199 Py_RETURN_NONE;
1200 else
1201 return PyErr_SetFromErrno(PyExc_OSError);
1202 }
1203
1204 /* sigtimedwait() was interrupted by a signal (EINTR) */
1205 if (PyErr_CheckSignals())
1206 return NULL;
1207
Victor Stinner34dc0f42015-03-27 18:19:03 +01001208 monotonic = _PyTime_GetMonotonicClock();
1209 timeout = deadline - monotonic;
Victor Stinner6aa446c2015-03-30 21:33:51 +02001210 if (timeout < 0)
Victor Stinnera453cd82015-03-20 12:54:28 +01001211 break;
1212 } while (1);
Ross Lagerwallbc808222011-06-25 12:13:40 +02001213
1214 return fill_siginfo(&si);
1215}
1216
Ross Lagerwallbc808222011-06-25 12:13:40 +02001217#endif /* #ifdef HAVE_SIGTIMEDWAIT */
1218
Victor Stinnerb3e72192011-05-08 01:46:11 +02001219
Antoine Pitroua6a4dc82017-09-07 18:56:24 +02001220#if defined(HAVE_PTHREAD_KILL)
Tal Einatc7027b72015-05-16 14:14:49 +03001221
1222/*[clinic input]
1223signal.pthread_kill
1224
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +02001225 thread_id: unsigned_long(bitwise=True)
Tal Einatc7027b72015-05-16 14:14:49 +03001226 signalnum: int
1227 /
1228
1229Send a signal to a thread.
1230[clinic start generated code]*/
1231
Victor Stinnerb3e72192011-05-08 01:46:11 +02001232static PyObject *
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +02001233signal_pthread_kill_impl(PyObject *module, unsigned long thread_id,
1234 int signalnum)
1235/*[clinic end generated code: output=7629919b791bc27f input=1d901f2c7bb544ff]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +02001236{
Victor Stinnerb3e72192011-05-08 01:46:11 +02001237 int err;
1238
Saiyang Gou7514f4f2020-02-12 23:47:42 -08001239 if (PySys_Audit("signal.pthread_kill", "ki", thread_id, signalnum) < 0) {
1240 return NULL;
1241 }
1242
Tal Einatc7027b72015-05-16 14:14:49 +03001243 err = pthread_kill((pthread_t)thread_id, signalnum);
Victor Stinnerb3e72192011-05-08 01:46:11 +02001244 if (err != 0) {
1245 errno = err;
1246 PyErr_SetFromErrno(PyExc_OSError);
1247 return NULL;
1248 }
1249
1250 /* the signal may have been send to the current thread */
1251 if (PyErr_CheckSignals())
1252 return NULL;
1253
1254 Py_RETURN_NONE;
1255}
1256
Antoine Pitroua6a4dc82017-09-07 18:56:24 +02001257#endif /* #if defined(HAVE_PTHREAD_KILL) */
Victor Stinnerb3e72192011-05-08 01:46:11 +02001258
1259
Benjamin Peterson74834512019-11-19 20:39:14 -08001260#if defined(__linux__) && defined(__NR_pidfd_send_signal)
1261/*[clinic input]
1262signal.pidfd_send_signal
1263
1264 pidfd: int
1265 signalnum: int
1266 siginfo: object = None
1267 flags: int = 0
1268 /
1269
1270Send a signal to a process referred to by a pid file descriptor.
1271[clinic start generated code]*/
1272
1273static PyObject *
1274signal_pidfd_send_signal_impl(PyObject *module, int pidfd, int signalnum,
1275 PyObject *siginfo, int flags)
1276/*[clinic end generated code: output=2d59f04a75d9cbdf input=2a6543a1f4ac2000]*/
1277
1278{
1279 if (siginfo != Py_None) {
1280 PyErr_SetString(PyExc_TypeError, "siginfo must be None");
1281 return NULL;
1282 }
1283 if (syscall(__NR_pidfd_send_signal, pidfd, signalnum, NULL, flags) < 0) {
1284 PyErr_SetFromErrno(PyExc_OSError);
1285 return NULL;
1286 }
1287 Py_RETURN_NONE;
1288}
1289#endif
1290
1291
Victor Stinnerb3e72192011-05-08 01:46:11 +02001292
Tal Einatc7027b72015-05-16 14:14:49 +03001293/* List of functions defined in the module -- some of the methoddefs are
1294 defined to nothing if the corresponding C function is not available. */
Barry Warsaw92971171997-01-03 00:14:25 +00001295static PyMethodDef signal_methods[] = {
Tal Einatc7027b72015-05-16 14:14:49 +03001296 {"default_int_handler", signal_default_int_handler, METH_VARARGS, default_int_handler_doc},
1297 SIGNAL_ALARM_METHODDEF
1298 SIGNAL_SETITIMER_METHODDEF
1299 SIGNAL_GETITIMER_METHODDEF
1300 SIGNAL_SIGNAL_METHODDEF
Vladimir Matveevc24c6c22019-01-08 01:58:25 -08001301 SIGNAL_RAISE_SIGNAL_METHODDEF
Antoine Pietri5d2a27d2018-03-12 14:42:34 +01001302 SIGNAL_STRSIGNAL_METHODDEF
Tal Einatc7027b72015-05-16 14:14:49 +03001303 SIGNAL_GETSIGNAL_METHODDEF
Serhiy Storchaka62be7422018-11-27 13:27:31 +02001304 {"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 +03001305 SIGNAL_SIGINTERRUPT_METHODDEF
1306 SIGNAL_PAUSE_METHODDEF
Benjamin Peterson74834512019-11-19 20:39:14 -08001307 SIGNAL_PIDFD_SEND_SIGNAL_METHODDEF
Tal Einatc7027b72015-05-16 14:14:49 +03001308 SIGNAL_PTHREAD_KILL_METHODDEF
1309 SIGNAL_PTHREAD_SIGMASK_METHODDEF
1310 SIGNAL_SIGPENDING_METHODDEF
1311 SIGNAL_SIGWAIT_METHODDEF
1312 SIGNAL_SIGWAITINFO_METHODDEF
1313 SIGNAL_SIGTIMEDWAIT_METHODDEF
Antoine Pitrou9d3627e2018-05-04 13:00:50 +02001314#if defined(HAVE_SIGFILLSET) || defined(MS_WINDOWS)
1315 SIGNAL_VALID_SIGNALS_METHODDEF
1316#endif
Tal Einatc7027b72015-05-16 14:14:49 +03001317 {NULL, NULL} /* sentinel */
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001318};
1319
Barry Warsaw92971171997-01-03 00:14:25 +00001320
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001321PyDoc_STRVAR(module_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001322"This module provides mechanisms to use signal handlers in Python.\n\
1323\n\
1324Functions:\n\
1325\n\
1326alarm() -- cause SIGALRM after a specified time [Unix only]\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001327setitimer() -- cause a signal (described below) after a specified\n\
1328 float time and the timer may restart then [Unix only]\n\
1329getitimer() -- get current value of timer [Unix only]\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001330signal() -- set the action for a given signal\n\
1331getsignal() -- get the signal action for a given signal\n\
1332pause() -- wait until a signal arrives [Unix only]\n\
1333default_int_handler() -- default SIGINT handler\n\
1334\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001335signal constants:\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001336SIG_DFL -- used to refer to the system default handler\n\
1337SIG_IGN -- used to ignore the signal\n\
1338NSIG -- number of defined signals\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001339SIGINT, SIGTERM, etc. -- signal numbers\n\
1340\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001341itimer constants:\n\
1342ITIMER_REAL -- decrements in real time, and delivers SIGALRM upon\n\
1343 expiration\n\
1344ITIMER_VIRTUAL -- decrements only when the process is executing,\n\
1345 and delivers SIGVTALRM upon expiration\n\
1346ITIMER_PROF -- decrements both when the process is executing and\n\
1347 when the system is executing on behalf of the process.\n\
1348 Coupled with ITIMER_VIRTUAL, this timer is usually\n\
1349 used to profile the time spent by the application\n\
1350 in user and kernel space. SIGPROF is delivered upon\n\
1351 expiration.\n\
1352\n\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001353*** IMPORTANT NOTICE ***\n\
1354A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001355the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001356
Martin v. Löwis1a214512008-06-11 05:26:20 +00001357static struct PyModuleDef signalmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001358 PyModuleDef_HEAD_INIT,
Brett Cannon815a6f32014-04-04 10:20:28 -04001359 "_signal",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001360 module_doc,
1361 -1,
1362 signal_methods,
1363 NULL,
1364 NULL,
1365 NULL,
1366 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001367};
1368
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001369PyMODINIT_FUNC
Giampaolo Rodola'e09fb712014-04-04 15:34:17 +02001370PyInit__signal(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001371{
animalize77643c42019-09-09 21:46:26 +08001372 PyObject *m, *d;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001373 int i;
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001374
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001375 /* Create the module and add the functions */
1376 m = PyModule_Create(&signalmodule);
1377 if (m == NULL)
1378 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001379
Ross Lagerwallbc808222011-06-25 12:13:40 +02001380#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
Victor Stinner1c8f0592013-07-22 22:24:54 +02001381 if (!initialized) {
1382 if (PyStructSequence_InitType2(&SiginfoType, &struct_siginfo_desc) < 0)
1383 return NULL;
1384 }
Ross Lagerwallbc808222011-06-25 12:13:40 +02001385 Py_INCREF((PyObject*) &SiginfoType);
1386 PyModule_AddObject(m, "struct_siginfo", (PyObject*) &SiginfoType);
1387 initialized = 1;
1388#endif
1389
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001390 /* Add some symbolic constants to the module */
1391 d = PyModule_GetDict(m);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001392
animalize77643c42019-09-09 21:46:26 +08001393 DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL);
1394 if (!DefaultHandler ||
1395 PyDict_SetItemString(d, "SIG_DFL", DefaultHandler) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001396 goto finally;
animalize77643c42019-09-09 21:46:26 +08001397 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001398
animalize77643c42019-09-09 21:46:26 +08001399 IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN);
1400 if (!IgnoreHandler ||
1401 PyDict_SetItemString(d, "SIG_IGN", IgnoreHandler) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001402 goto finally;
animalize77643c42019-09-09 21:46:26 +08001403 }
Barry Warsaw92971171997-01-03 00:14:25 +00001404
Joannah Nanjekye9541bd32019-04-21 21:47:06 -04001405 if (PyModule_AddIntMacro(m, NSIG))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001406 goto finally;
Barry Warsaw92971171997-01-03 00:14:25 +00001407
Victor Stinnera9293352011-04-30 15:21:58 +02001408#ifdef SIG_BLOCK
Victor Stinner72c53b52011-05-02 16:15:43 +02001409 if (PyModule_AddIntMacro(m, SIG_BLOCK))
1410 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001411#endif
Victor Stinnera9293352011-04-30 15:21:58 +02001412#ifdef SIG_UNBLOCK
Victor Stinner72c53b52011-05-02 16:15:43 +02001413 if (PyModule_AddIntMacro(m, SIG_UNBLOCK))
1414 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001415#endif
Victor Stinnera9293352011-04-30 15:21:58 +02001416#ifdef SIG_SETMASK
Victor Stinner72c53b52011-05-02 16:15:43 +02001417 if (PyModule_AddIntMacro(m, SIG_SETMASK))
1418 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001419#endif
1420
animalize77643c42019-09-09 21:46:26 +08001421 IntHandler = PyDict_GetItemString(d, "default_int_handler");
1422 if (!IntHandler)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001423 goto finally;
1424 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +00001425
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001426 _Py_atomic_store_relaxed(&Handlers[0].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001427 for (i = 1; i < NSIG; i++) {
1428 void (*t)(int);
1429 t = PyOS_getsig(i);
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001430 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001431 if (t == SIG_DFL)
1432 Handlers[i].func = DefaultHandler;
1433 else if (t == SIG_IGN)
1434 Handlers[i].func = IgnoreHandler;
1435 else
1436 Handlers[i].func = Py_None; /* None of our business */
1437 Py_INCREF(Handlers[i].func);
1438 }
1439 if (Handlers[SIGINT].func == DefaultHandler) {
1440 /* Install default int handler */
1441 Py_INCREF(IntHandler);
Serhiy Storchaka57a01d32016-04-10 18:05:40 +03001442 Py_SETREF(Handlers[SIGINT].func, IntHandler);
pkerlinge905c842018-06-01 09:47:18 +00001443 PyOS_setsig(SIGINT, signal_handler);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001444 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001445
1446#ifdef SIGHUP
Christian Heimes6782b142016-09-09 00:11:45 +02001447 if (PyModule_AddIntMacro(m, SIGHUP))
1448 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001449#endif
1450#ifdef SIGINT
Christian Heimes6782b142016-09-09 00:11:45 +02001451 if (PyModule_AddIntMacro(m, SIGINT))
1452 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001453#endif
Tim Peters1ce3cf72001-10-01 17:58:40 +00001454#ifdef SIGBREAK
Christian Heimes6782b142016-09-09 00:11:45 +02001455 if (PyModule_AddIntMacro(m, SIGBREAK))
1456 goto finally;
Tim Peters1ce3cf72001-10-01 17:58:40 +00001457#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001458#ifdef SIGQUIT
Christian Heimes6782b142016-09-09 00:11:45 +02001459 if (PyModule_AddIntMacro(m, SIGQUIT))
1460 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001461#endif
1462#ifdef SIGILL
Christian Heimes6782b142016-09-09 00:11:45 +02001463 if (PyModule_AddIntMacro(m, SIGILL))
1464 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001465#endif
1466#ifdef SIGTRAP
Christian Heimes6782b142016-09-09 00:11:45 +02001467 if (PyModule_AddIntMacro(m, SIGTRAP))
1468 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001469#endif
1470#ifdef SIGIOT
Christian Heimes6782b142016-09-09 00:11:45 +02001471 if (PyModule_AddIntMacro(m, SIGIOT))
1472 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001473#endif
1474#ifdef SIGABRT
Christian Heimes6782b142016-09-09 00:11:45 +02001475 if (PyModule_AddIntMacro(m, SIGABRT))
1476 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001477#endif
1478#ifdef SIGEMT
Christian Heimes6782b142016-09-09 00:11:45 +02001479 if (PyModule_AddIntMacro(m, SIGEMT))
1480 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001481#endif
1482#ifdef SIGFPE
Christian Heimes6782b142016-09-09 00:11:45 +02001483 if (PyModule_AddIntMacro(m, SIGFPE))
1484 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001485#endif
1486#ifdef SIGKILL
Christian Heimes6782b142016-09-09 00:11:45 +02001487 if (PyModule_AddIntMacro(m, SIGKILL))
1488 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001489#endif
1490#ifdef SIGBUS
Christian Heimes6782b142016-09-09 00:11:45 +02001491 if (PyModule_AddIntMacro(m, SIGBUS))
1492 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001493#endif
1494#ifdef SIGSEGV
Christian Heimes6782b142016-09-09 00:11:45 +02001495 if (PyModule_AddIntMacro(m, SIGSEGV))
1496 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001497#endif
1498#ifdef SIGSYS
Christian Heimes6782b142016-09-09 00:11:45 +02001499 if (PyModule_AddIntMacro(m, SIGSYS))
1500 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001501#endif
1502#ifdef SIGPIPE
Christian Heimes6782b142016-09-09 00:11:45 +02001503 if (PyModule_AddIntMacro(m, SIGPIPE))
1504 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001505#endif
1506#ifdef SIGALRM
Christian Heimes6782b142016-09-09 00:11:45 +02001507 if (PyModule_AddIntMacro(m, SIGALRM))
1508 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001509#endif
1510#ifdef SIGTERM
Christian Heimes6782b142016-09-09 00:11:45 +02001511 if (PyModule_AddIntMacro(m, SIGTERM))
1512 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001513#endif
1514#ifdef SIGUSR1
Christian Heimes6782b142016-09-09 00:11:45 +02001515 if (PyModule_AddIntMacro(m, SIGUSR1))
1516 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001517#endif
1518#ifdef SIGUSR2
Christian Heimes6782b142016-09-09 00:11:45 +02001519 if (PyModule_AddIntMacro(m, SIGUSR2))
1520 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001521#endif
1522#ifdef SIGCLD
Christian Heimes6782b142016-09-09 00:11:45 +02001523 if (PyModule_AddIntMacro(m, SIGCLD))
1524 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001525#endif
1526#ifdef SIGCHLD
Christian Heimes6782b142016-09-09 00:11:45 +02001527 if (PyModule_AddIntMacro(m, SIGCHLD))
1528 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001529#endif
1530#ifdef SIGPWR
Christian Heimes6782b142016-09-09 00:11:45 +02001531 if (PyModule_AddIntMacro(m, SIGPWR))
1532 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001533#endif
1534#ifdef SIGIO
Christian Heimes6782b142016-09-09 00:11:45 +02001535 if (PyModule_AddIntMacro(m, SIGIO))
1536 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001537#endif
1538#ifdef SIGURG
Christian Heimes6782b142016-09-09 00:11:45 +02001539 if (PyModule_AddIntMacro(m, SIGURG))
1540 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001541#endif
1542#ifdef SIGWINCH
Christian Heimes6782b142016-09-09 00:11:45 +02001543 if (PyModule_AddIntMacro(m, SIGWINCH))
1544 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001545#endif
1546#ifdef SIGPOLL
Christian Heimes6782b142016-09-09 00:11:45 +02001547 if (PyModule_AddIntMacro(m, SIGPOLL))
1548 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001549#endif
1550#ifdef SIGSTOP
Christian Heimes6782b142016-09-09 00:11:45 +02001551 if (PyModule_AddIntMacro(m, SIGSTOP))
1552 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001553#endif
1554#ifdef SIGTSTP
Christian Heimes6782b142016-09-09 00:11:45 +02001555 if (PyModule_AddIntMacro(m, SIGTSTP))
1556 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001557#endif
1558#ifdef SIGCONT
Christian Heimes6782b142016-09-09 00:11:45 +02001559 if (PyModule_AddIntMacro(m, SIGCONT))
1560 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001561#endif
1562#ifdef SIGTTIN
Christian Heimes6782b142016-09-09 00:11:45 +02001563 if (PyModule_AddIntMacro(m, SIGTTIN))
1564 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001565#endif
1566#ifdef SIGTTOU
Christian Heimes6782b142016-09-09 00:11:45 +02001567 if (PyModule_AddIntMacro(m, SIGTTOU))
1568 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001569#endif
1570#ifdef SIGVTALRM
Christian Heimes6782b142016-09-09 00:11:45 +02001571 if (PyModule_AddIntMacro(m, SIGVTALRM))
1572 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001573#endif
1574#ifdef SIGPROF
Christian Heimes6782b142016-09-09 00:11:45 +02001575 if (PyModule_AddIntMacro(m, SIGPROF))
1576 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001577#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001578#ifdef SIGXCPU
Christian Heimes6782b142016-09-09 00:11:45 +02001579 if (PyModule_AddIntMacro(m, SIGXCPU))
1580 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001581#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001582#ifdef SIGXFSZ
Christian Heimes6782b142016-09-09 00:11:45 +02001583 if (PyModule_AddIntMacro(m, SIGXFSZ))
1584 goto finally;
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001585#endif
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001586#ifdef SIGRTMIN
Christian Heimes6782b142016-09-09 00:11:45 +02001587 if (PyModule_AddIntMacro(m, SIGRTMIN))
1588 goto finally;
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001589#endif
1590#ifdef SIGRTMAX
Christian Heimes6782b142016-09-09 00:11:45 +02001591 if (PyModule_AddIntMacro(m, SIGRTMAX))
1592 goto finally;
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001593#endif
Martin v. Löwis175af252002-01-12 11:43:25 +00001594#ifdef SIGINFO
Christian Heimes6782b142016-09-09 00:11:45 +02001595 if (PyModule_AddIntMacro(m, SIGINFO))
1596 goto finally;
Martin v. Löwis175af252002-01-12 11:43:25 +00001597#endif
Martin v. Löwis823725e2008-03-24 13:39:54 +00001598
1599#ifdef ITIMER_REAL
Christian Heimes6782b142016-09-09 00:11:45 +02001600 if (PyModule_AddIntMacro(m, ITIMER_REAL))
1601 goto finally;
Martin v. Löwis823725e2008-03-24 13:39:54 +00001602#endif
1603#ifdef ITIMER_VIRTUAL
Christian Heimes6782b142016-09-09 00:11:45 +02001604 if (PyModule_AddIntMacro(m, ITIMER_VIRTUAL))
1605 goto finally;
Martin v. Löwis823725e2008-03-24 13:39:54 +00001606#endif
1607#ifdef ITIMER_PROF
Christian Heimes6782b142016-09-09 00:11:45 +02001608 if (PyModule_AddIntMacro(m, ITIMER_PROF))
1609 goto finally;
Martin v. Löwis823725e2008-03-24 13:39:54 +00001610#endif
1611
1612#if defined (HAVE_SETITIMER) || defined (HAVE_GETITIMER)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001613 ItimerError = PyErr_NewException("signal.ItimerError",
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +03001614 PyExc_OSError, NULL);
animalize77643c42019-09-09 21:46:26 +08001615 if (!ItimerError ||
1616 PyDict_SetItemString(d, "ItimerError", ItimerError) < 0) {
Joannah Nanjekye9541bd32019-04-21 21:47:06 -04001617 goto finally;
animalize77643c42019-09-09 21:46:26 +08001618 }
Martin v. Löwis823725e2008-03-24 13:39:54 +00001619#endif
1620
Brian Curtineb24d742010-04-12 17:16:38 +00001621#ifdef CTRL_C_EVENT
Christian Heimes6782b142016-09-09 00:11:45 +02001622 if (PyModule_AddIntMacro(m, CTRL_C_EVENT))
1623 goto finally;
Brian Curtineb24d742010-04-12 17:16:38 +00001624#endif
1625
1626#ifdef CTRL_BREAK_EVENT
Christian Heimes6782b142016-09-09 00:11:45 +02001627 if (PyModule_AddIntMacro(m, CTRL_BREAK_EVENT))
1628 goto finally;
Brian Curtineb24d742010-04-12 17:16:38 +00001629#endif
1630
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001631#ifdef MS_WINDOWS
1632 /* Create manual-reset event, initially unset */
1633 sigint_event = CreateEvent(NULL, TRUE, FALSE, FALSE);
1634#endif
1635
Martin v. Löwis1a214512008-06-11 05:26:20 +00001636 if (PyErr_Occurred()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001637 Py_DECREF(m);
1638 m = NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001639 }
Barry Warsaw92971171997-01-03 00:14:25 +00001640
Barry Warsaw92971171997-01-03 00:14:25 +00001641 finally:
Martin v. Löwis1a214512008-06-11 05:26:20 +00001642 return m;
Guido van Rossum08c16611997-08-02 03:01:42 +00001643}
1644
1645static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001646finisignal(void)
Guido van Rossum08c16611997-08-02 03:01:42 +00001647{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001648 int i;
1649 PyObject *func;
Guido van Rossum08c16611997-08-02 03:01:42 +00001650
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001651 for (i = 1; i < NSIG; i++) {
1652 func = Handlers[i].func;
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001653 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001654 Handlers[i].func = NULL;
pkerlinge905c842018-06-01 09:47:18 +00001655 if (func != NULL && func != Py_None &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001656 func != DefaultHandler && func != IgnoreHandler)
1657 PyOS_setsig(i, SIG_DFL);
1658 Py_XDECREF(func);
1659 }
Guido van Rossum08c16611997-08-02 03:01:42 +00001660
Serhiy Storchaka505ff752014-02-09 13:33:53 +02001661 Py_CLEAR(IntHandler);
1662 Py_CLEAR(DefaultHandler);
1663 Py_CLEAR(IgnoreHandler);
animalize77643c42019-09-09 21:46:26 +08001664#ifdef HAVE_GETITIMER
1665 Py_CLEAR(ItimerError);
1666#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001667}
1668
Barry Warsaw92971171997-01-03 00:14:25 +00001669
Barry Warsaw92971171997-01-03 00:14:25 +00001670/* Declared in pyerrors.h */
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001671int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001672PyErr_CheckSignals(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001673{
Victor Stinnerd8613dc2019-05-24 13:43:55 +02001674 _PyRuntimeState *runtime = &_PyRuntime;
1675 if (!is_main(runtime)) {
Eric Snow64d6cc82019-02-23 15:40:43 -07001676 return 0;
1677 }
1678
1679 return _PyErr_CheckSignals();
1680}
1681
1682
1683/* Declared in cpython/pyerrors.h */
1684int
1685_PyErr_CheckSignals(void)
1686{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001687 int i;
1688 PyObject *f;
Barry Warsaw92971171997-01-03 00:14:25 +00001689
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001690 if (!_Py_atomic_load(&is_tripped))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001691 return 0;
Christian Heimesb76922a2007-12-11 01:06:40 +00001692
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001693 /*
1694 * The is_tripped variable is meant to speed up the calls to
1695 * PyErr_CheckSignals (both directly or via pending calls) when no
1696 * signal has arrived. This variable is set to 1 when a signal arrives
1697 * and it is set to 0 here, when we know some signals arrived. This way
1698 * we can run the registered handlers with no signals blocked.
1699 *
1700 * NOTE: with this approach we can have a situation where is_tripped is
1701 * 1 but we have no more signals to handle (Handlers[i].tripped
1702 * is 0 for every signal i). This won't do us any harm (except
1703 * we're gonna spent some cycles for nothing). This happens when
1704 * we receive a signal i after we zero is_tripped and before we
1705 * check Handlers[i].tripped.
1706 */
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001707 _Py_atomic_store(&is_tripped, 0);
Christian Heimesb76922a2007-12-11 01:06:40 +00001708
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001709 if (!(f = (PyObject *)PyEval_GetFrame()))
1710 f = Py_None;
Christian Heimesb76922a2007-12-11 01:06:40 +00001711
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001712 for (i = 1; i < NSIG; i++) {
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001713 if (_Py_atomic_load_relaxed(&Handlers[i].tripped)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001714 PyObject *result = NULL;
1715 PyObject *arglist = Py_BuildValue("(iO)", i, f);
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001716 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Barry Warsaw92971171997-01-03 00:14:25 +00001717
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001718 if (arglist) {
Jeroen Demeyer1dbd0842019-07-11 17:57:32 +02001719 result = PyObject_Call(Handlers[i].func, arglist, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001720 Py_DECREF(arglist);
1721 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +02001722 if (!result) {
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001723 _Py_atomic_store(&is_tripped, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001724 return -1;
Antoine Pitrouc08177a2017-06-28 23:29:29 +02001725 }
Barry Warsaw92971171997-01-03 00:14:25 +00001726
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001727 Py_DECREF(result);
1728 }
1729 }
Christian Heimesb76922a2007-12-11 01:06:40 +00001730
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001731 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001732}
1733
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +00001734
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
1749void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001750PyOS_InitInterrupts(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001751{
Giampaolo Rodola'e09fb712014-04-04 15:34:17 +02001752 PyObject *m = PyImport_ImportModule("_signal");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001753 if (m) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001754 Py_DECREF(m);
1755 }
Guido van Rossum08c16611997-08-02 03:01:42 +00001756}
1757
1758void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001759PyOS_FiniInterrupts(void)
Guido van Rossum08c16611997-08-02 03:01:42 +00001760{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001761 finisignal();
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001762}
1763
1764int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001765PyOS_InterruptOccurred(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001766{
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001767 if (_Py_atomic_load_relaxed(&Handlers[SIGINT].tripped)) {
Victor Stinnerd8613dc2019-05-24 13:43:55 +02001768 _PyRuntimeState *runtime = &_PyRuntime;
1769 if (!is_main(runtime)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001770 return 0;
Eric Snow64d6cc82019-02-23 15:40:43 -07001771 }
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001772 _Py_atomic_store_relaxed(&Handlers[SIGINT].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001773 return 1;
1774 }
1775 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001776}
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001777
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001778static void
1779_clear_pending_signals(void)
1780{
1781 int i;
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001782 if (!_Py_atomic_load(&is_tripped))
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001783 return;
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001784 _Py_atomic_store(&is_tripped, 0);
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001785 for (i = 1; i < NSIG; ++i) {
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001786 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001787 }
1788}
1789
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001790void
Antoine Pitrou346cbd32017-05-27 17:50:54 +02001791_PySignal_AfterFork(void)
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001792{
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001793 /* Clear the signal flags after forking so that they aren't handled
1794 * in both processes if they came in just before the fork() but before
1795 * the interpreter had an opportunity to call the handlers. issue9535. */
1796 _clear_pending_signals();
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001797}
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001798
1799int
1800_PyOS_IsMainThread(void)
1801{
Victor Stinnerd8613dc2019-05-24 13:43:55 +02001802 _PyRuntimeState *runtime = &_PyRuntime;
1803 return is_main(runtime);
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001804}
1805
1806#ifdef MS_WINDOWS
1807void *_PyOS_SigintEvent(void)
1808{
1809 /* Returns a manual-reset event which gets tripped whenever
1810 SIGINT is received.
1811
1812 Python.h does not include windows.h so we do cannot use HANDLE
1813 as the return type of this function. We use void* instead. */
1814 return sigint_event;
1815}
1816#endif