blob: b5e6250b1b4059301fd644c0a59523518842bde3 [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
28#ifdef HAVE_SYS_STAT_H
Christian Heimes5fb7c2a2007-12-24 08:52:31 +000029#include <sys/stat.h>
Benjamin Peterson2614cda2010-03-21 22:36:19 +000030#endif
Martin v. Löwis3bf3cc02008-03-24 14:05:07 +000031#ifdef HAVE_SYS_TIME_H
Martin v. Löwis823725e2008-03-24 13:39:54 +000032#include <sys/time.h>
Martin v. Löwis3bf3cc02008-03-24 14:05:07 +000033#endif
Christian Heimes5fb7c2a2007-12-24 08:52:31 +000034
Victor Stinnera9293352011-04-30 15:21:58 +020035#if defined(HAVE_PTHREAD_SIGMASK) && !defined(HAVE_BROKEN_PTHREAD_SIGMASK)
36# define PYPTHREAD_SIGMASK
37#endif
38
39#if defined(PYPTHREAD_SIGMASK) && defined(HAVE_PTHREAD_H)
40# include <pthread.h>
41#endif
42
Guido van Rossumbb4ba121994-06-23 11:25:45 +000043#ifndef SIG_ERR
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +000044#define SIG_ERR ((PyOS_sighandler_t)(-1))
Guido van Rossumbb4ba121994-06-23 11:25:45 +000045#endif
46
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000047#ifndef NSIG
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000048# if defined(_NSIG)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000049# define NSIG _NSIG /* For BSD/SysV */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000050# elif defined(_SIGMAX)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000051# define NSIG (_SIGMAX + 1) /* For QNX */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000052# elif defined(SIGMAX)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000053# define NSIG (SIGMAX + 1) /* For djgpp */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000054# else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000055# define NSIG 64 /* Use a reasonable default value */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000056# endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000057#endif
58
Tal Einatc7027b72015-05-16 14:14:49 +030059#include "clinic/signalmodule.c.h"
60
61/*[clinic input]
62module signal
63[clinic start generated code]*/
64/*[clinic end generated code: output=da39a3ee5e6b4b0d input=b0301a3bde5fe9d3]*/
65
Serhiy Storchakad54cfb12018-05-08 07:48:50 +030066/*[python input]
67
68class sigset_t_converter(CConverter):
69 type = 'sigset_t'
70 converter = '_Py_Sigset_Converter'
71
72[python start generated code]*/
73/*[python end generated code: output=da39a3ee5e6b4b0d input=b5689d14466b6823]*/
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000074
Guido van Rossumbb4ba121994-06-23 11:25:45 +000075/*
76 NOTES ON THE INTERACTION BETWEEN SIGNALS AND THREADS
77
Jeroen Demeyerd237b3f2019-05-10 03:28:57 +020078 We want the following semantics:
Guido van Rossumbb4ba121994-06-23 11:25:45 +000079
80 - only the main thread can set a signal handler
Jeroen Demeyerd237b3f2019-05-10 03:28:57 +020081 - only the main thread runs the signal handler
82 - signals can be delivered to any thread
Guido van Rossumbb4ba121994-06-23 11:25:45 +000083 - any thread can get a signal handler
Guido van Rossumbb4ba121994-06-23 11:25:45 +000084
85 I.e. we don't support "synchronous signals" like SIGFPE (catching
86 this doesn't make much sense in Python anyway) nor do we support
87 signals as a means of inter-thread communication, since not all
88 thread implementations support that (at least our thread library
89 doesn't).
90
91 We still have the problem that in some implementations signals
92 generated by the keyboard (e.g. SIGINT) are delivered to all
93 threads (e.g. SGI), while in others (e.g. Solaris) such signals are
Jeroen Demeyerd237b3f2019-05-10 03:28:57 +020094 delivered to one random thread. On Linux, signals are delivered to
95 the main thread (unless the main thread is blocking the signal, for
96 example because it's already handling the same signal). Since we
97 allow signals to be delivered to any thread, this works fine. The
98 only oddity is that the thread executing the Python signal handler
99 may not be the thread that received the signal.
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000100*/
101
Guido van Rossum295b8e51997-06-06 21:16:41 +0000102#include <sys/types.h> /* For pid_t */
Guido van Rossum49b56061998-10-01 20:42:43 +0000103#include "pythread.h"
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +0200104static unsigned long main_thread;
Eric Snow64d6cc82019-02-23 15:40:43 -0700105static PyInterpreterState *main_interp;
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
193is_main(void)
194{
195 return PyThread_get_thread_ident() == main_thread &&
196 _PyInterpreterState_Get() == main_interp;
197}
198
Guido van Rossume4485b01994-09-07 14:32:49 +0000199static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000200signal_default_int_handler(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000201{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000202 PyErr_SetNone(PyExc_KeyboardInterrupt);
203 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000204}
205
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000206PyDoc_STRVAR(default_int_handler_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000207"default_int_handler(...)\n\
208\n\
Michael W. Hudson24ec2112004-06-17 15:55:53 +0000209The default handler for SIGINT installed by Python.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000210It raises KeyboardInterrupt.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000211
Thomas Wouters0796b002000-07-22 23:49:30 +0000212
213static int
Victor Stinner11517102014-07-29 23:31:34 +0200214report_wakeup_write_error(void *data)
Antoine Pitrou6f6ec372013-08-17 20:27:56 +0200215{
Eric Snowfdf282d2019-01-11 14:26:55 -0700216 PyObject *exc, *val, *tb;
Antoine Pitrou6f6ec372013-08-17 20:27:56 +0200217 int save_errno = errno;
Benjamin Petersonca470632016-09-06 13:47:26 -0700218 errno = (int) (intptr_t) data;
Eric Snowfdf282d2019-01-11 14:26:55 -0700219 PyErr_Fetch(&exc, &val, &tb);
Antoine Pitrou6f6ec372013-08-17 20:27:56 +0200220 PyErr_SetFromErrno(PyExc_OSError);
221 PySys_WriteStderr("Exception ignored when trying to write to the "
222 "signal wakeup fd:\n");
223 PyErr_WriteUnraisable(NULL);
Eric Snowfdf282d2019-01-11 14:26:55 -0700224 PyErr_Restore(exc, val, tb);
Antoine Pitrou6f6ec372013-08-17 20:27:56 +0200225 errno = save_errno;
226 return 0;
227}
228
Victor Stinner11517102014-07-29 23:31:34 +0200229#ifdef MS_WINDOWS
230static int
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800231report_wakeup_send_error(void* data)
Victor Stinner11517102014-07-29 23:31:34 +0200232{
Eric Snowfdf282d2019-01-11 14:26:55 -0700233 PyObject *exc, *val, *tb;
234 PyErr_Fetch(&exc, &val, &tb);
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800235 /* PyErr_SetExcFromWindowsErr() invokes FormatMessage() which
236 recognizes the error codes used by both GetLastError() and
237 WSAGetLastError */
238 PyErr_SetExcFromWindowsErr(PyExc_OSError, (int) (intptr_t) data);
Victor Stinner11517102014-07-29 23:31:34 +0200239 PySys_WriteStderr("Exception ignored when trying to send to the "
240 "signal wakeup fd:\n");
241 PyErr_WriteUnraisable(NULL);
Eric Snowfdf282d2019-01-11 14:26:55 -0700242 PyErr_Restore(exc, val, tb);
Victor Stinner11517102014-07-29 23:31:34 +0200243 return 0;
244}
245#endif /* MS_WINDOWS */
246
Tim Peters4f1b2082000-07-23 21:18:09 +0000247static void
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200248trip_signal(int sig_num)
249{
Victor Stinnerd49b1f12011-05-08 02:03:15 +0200250 unsigned char byte;
Victor Stinner11517102014-07-29 23:31:34 +0200251 int fd;
252 Py_ssize_t rc;
Victor Stinnerc13ef662011-05-25 02:35:58 +0200253
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +0200254 _Py_atomic_store_relaxed(&Handlers[sig_num].tripped, 1);
Victor Stinner11517102014-07-29 23:31:34 +0200255
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200256 /* Set is_tripped after setting .tripped, as it gets
257 cleared in PyErr_CheckSignals() before .tripped. */
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +0200258 _Py_atomic_store(&is_tripped, 1);
259
260 /* Notify ceval.c */
Victor Stinner09532fe2019-05-10 23:39:09 +0200261 _PyRuntimeState *runtime = &_PyRuntime;
262 _PyEval_SignalReceived(&runtime->ceval);
Nathaniel J. Smith4ae01492017-05-16 14:12:11 -0700263
264 /* And then write to the wakeup fd *after* setting all the globals and
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200265 doing the _PyEval_SignalReceived. We used to write to the wakeup fd
266 and then set the flag, but this allowed the following sequence of events
267 (especially on windows, where trip_signal may run in a new thread):
Nathaniel J. Smith4ae01492017-05-16 14:12:11 -0700268
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800269 - main thread blocks on select([wakeup.fd], ...)
Nathaniel J. Smith4ae01492017-05-16 14:12:11 -0700270 - signal arrives
271 - trip_signal writes to the wakeup fd
272 - the main thread wakes up
273 - the main thread checks the signal flags, sees that they're unset
274 - the main thread empties the wakeup fd
275 - the main thread goes back to sleep
276 - trip_signal sets the flags to request the Python-level signal handler
277 be run
278 - the main thread doesn't notice, because it's asleep
279
280 See bpo-30038 for more details.
281 */
282
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 Stinnerc13ef662011-05-25 02:35:58 +0200290 byte = (unsigned char)sig_num;
Victor Stinner11517102014-07-29 23:31:34 +0200291#ifdef MS_WINDOWS
292 if (wakeup.use_send) {
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800293 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 {
300 /* Py_AddPendingCall() isn't signal-safe, but we
301 still use it for this exceptional case. */
Victor Stinner09532fe2019-05-10 23:39:09 +0200302 _PyEval_AddPendingCall(&runtime->ceval,
303 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). */
313 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 {
319 /* Py_AddPendingCall() isn't signal-safe, but we
320 still use it for this exceptional case. */
Victor Stinner09532fe2019-05-10 23:39:09 +0200321 _PyEval_AddPendingCall(&runtime->ceval,
322 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
Eric Snow64d6cc82019-02-23 15:40:43 -0700477 if (!is_main()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000478 PyErr_SetString(PyExc_ValueError,
479 "signal only works in main thread");
480 return NULL;
481 }
Tal Einatc7027b72015-05-16 14:14:49 +0300482 if (signalnum < 1 || signalnum >= NSIG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000483 PyErr_SetString(PyExc_ValueError,
484 "signal number out of range");
485 return NULL;
486 }
Tal Einatc7027b72015-05-16 14:14:49 +0300487 if (handler == IgnoreHandler)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000488 func = SIG_IGN;
Tal Einatc7027b72015-05-16 14:14:49 +0300489 else if (handler == DefaultHandler)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000490 func = SIG_DFL;
Tal Einatc7027b72015-05-16 14:14:49 +0300491 else if (!PyCallable_Check(handler)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000492 PyErr_SetString(PyExc_TypeError,
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000493"signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000494 return NULL;
495 }
496 else
497 func = signal_handler;
Antoine Pitrouf6f90ff2017-11-03 19:58:46 +0100498 /* Check for pending signals before changing signal handler */
Eric Snow64d6cc82019-02-23 15:40:43 -0700499 if (_PyErr_CheckSignals()) {
Antoine Pitrouf6f90ff2017-11-03 19:58:46 +0100500 return NULL;
501 }
Tal Einatc7027b72015-05-16 14:14:49 +0300502 if (PyOS_setsig(signalnum, func) == SIG_ERR) {
Victor Stinner388196e2011-05-10 17:13:00 +0200503 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000504 return NULL;
505 }
Tal Einatc7027b72015-05-16 14:14:49 +0300506 old_handler = Handlers[signalnum].func;
Tal Einatc7027b72015-05-16 14:14:49 +0300507 Py_INCREF(handler);
508 Handlers[signalnum].func = handler;
Antoine Pitrouc8c952c2013-05-04 23:16:59 +0200509 if (old_handler != NULL)
510 return old_handler;
511 else
512 Py_RETURN_NONE;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000513}
514
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000515
Tal Einatc7027b72015-05-16 14:14:49 +0300516/*[clinic input]
517signal.getsignal
518
519 signalnum: int
520 /
521
522Return the current action for the given signal.
523
524The return value can be:
525 SIG_IGN -- if the signal is being ignored
526 SIG_DFL -- if the default action for the signal is in effect
527 None -- if an unknown handler is in effect
528 anything else -- the callable Python object used as a handler
529[clinic start generated code]*/
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000530
Guido van Rossume4485b01994-09-07 14:32:49 +0000531static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300532signal_getsignal_impl(PyObject *module, int signalnum)
533/*[clinic end generated code: output=35b3e0e796fd555e input=ac23a00f19dfa509]*/
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000534{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000535 PyObject *old_handler;
Tal Einatc7027b72015-05-16 14:14:49 +0300536 if (signalnum < 1 || signalnum >= NSIG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000537 PyErr_SetString(PyExc_ValueError,
538 "signal number out of range");
539 return NULL;
540 }
Tal Einatc7027b72015-05-16 14:14:49 +0300541 old_handler = Handlers[signalnum].func;
Antoine Pitrouc8c952c2013-05-04 23:16:59 +0200542 if (old_handler != NULL) {
543 Py_INCREF(old_handler);
544 return old_handler;
545 }
546 else {
547 Py_RETURN_NONE;
548 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000549}
550
Antoine Pietri5d2a27d2018-03-12 14:42:34 +0100551
552/*[clinic input]
553signal.strsignal
554
555 signalnum: int
556 /
557
558Return the system description of the given signal.
559
560The return values can be such as "Interrupt", "Segmentation fault", etc.
561Returns None if the signal is not recognized.
562[clinic start generated code]*/
563
564static PyObject *
565signal_strsignal_impl(PyObject *module, int signalnum)
566/*[clinic end generated code: output=44e12e1e3b666261 input=b77914b03f856c74]*/
567{
568 char *res;
569
570 if (signalnum < 1 || signalnum >= NSIG) {
571 PyErr_SetString(PyExc_ValueError,
572 "signal number out of range");
573 return NULL;
574 }
575
Michael Osipov48ce4892018-08-23 15:27:19 +0200576#ifndef HAVE_STRSIGNAL
Antoine Pietri5d2a27d2018-03-12 14:42:34 +0100577 switch (signalnum) {
Michael Osipov48ce4892018-08-23 15:27:19 +0200578 /* Though being a UNIX, HP-UX does not provide strsignal(3). */
579#ifndef MS_WINDOWS
580 case SIGHUP:
581 res = "Hangup";
582 break;
583 case SIGALRM:
584 res = "Alarm clock";
585 break;
586 case SIGPIPE:
587 res = "Broken pipe";
588 break;
589 case SIGQUIT:
590 res = "Quit";
591 break;
592 case SIGCHLD:
593 res = "Child exited";
594 break;
595#endif
596 /* Custom redefinition of POSIX signals allowed on Windows. */
Antoine Pietri5d2a27d2018-03-12 14:42:34 +0100597 case SIGINT:
598 res = "Interrupt";
599 break;
600 case SIGILL:
601 res = "Illegal instruction";
602 break;
603 case SIGABRT:
604 res = "Aborted";
605 break;
606 case SIGFPE:
607 res = "Floating point exception";
608 break;
609 case SIGSEGV:
610 res = "Segmentation fault";
611 break;
612 case SIGTERM:
613 res = "Terminated";
614 break;
615 default:
616 Py_RETURN_NONE;
617 }
618#else
619 errno = 0;
620 res = strsignal(signalnum);
621
622 if (errno || res == NULL || strstr(res, "Unknown signal") != NULL)
623 Py_RETURN_NONE;
624#endif
625
626 return Py_BuildValue("s", res);
627}
628
Christian Heimes8640e742008-02-23 16:23:06 +0000629#ifdef HAVE_SIGINTERRUPT
Tal Einatc7027b72015-05-16 14:14:49 +0300630
631/*[clinic input]
632signal.siginterrupt
633
634 signalnum: int
635 flag: int
636 /
637
638Change system call restart behaviour.
639
640If flag is False, system calls will be restarted when interrupted by
641signal sig, else system calls will be interrupted.
642[clinic start generated code]*/
Christian Heimes8640e742008-02-23 16:23:06 +0000643
644static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300645signal_siginterrupt_impl(PyObject *module, int signalnum, int flag)
646/*[clinic end generated code: output=063816243d85dd19 input=4160acacca3e2099]*/
Christian Heimes8640e742008-02-23 16:23:06 +0000647{
Tal Einatc7027b72015-05-16 14:14:49 +0300648 if (signalnum < 1 || signalnum >= NSIG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000649 PyErr_SetString(PyExc_ValueError,
650 "signal number out of range");
651 return NULL;
652 }
Tal Einatc7027b72015-05-16 14:14:49 +0300653 if (siginterrupt(signalnum, flag)<0) {
Victor Stinner388196e2011-05-10 17:13:00 +0200654 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000655 return NULL;
656 }
Tal Einatc7027b72015-05-16 14:14:49 +0300657 Py_RETURN_NONE;
Christian Heimes8640e742008-02-23 16:23:06 +0000658}
659
660#endif
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000661
Tal Einatc7027b72015-05-16 14:14:49 +0300662
663static PyObject*
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800664signal_set_wakeup_fd(PyObject *self, PyObject *args, PyObject *kwds)
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000665{
Victor Stinnere134a7f2015-03-30 10:09:31 +0200666 struct _Py_stat_struct status;
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800667 static char *kwlist[] = {
668 "", "warn_on_full_buffer", NULL,
669 };
670 int warn_on_full_buffer = 1;
Victor Stinner11517102014-07-29 23:31:34 +0200671#ifdef MS_WINDOWS
672 PyObject *fdobj;
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100673 SOCKET_T sockfd, old_sockfd;
Victor Stinner11517102014-07-29 23:31:34 +0200674 int res;
675 int res_size = sizeof res;
676 PyObject *mod;
Victor Stinner11517102014-07-29 23:31:34 +0200677 int is_socket;
678
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800679 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|$p:set_wakeup_fd", kwlist,
680 &fdobj, &warn_on_full_buffer))
Victor Stinner11517102014-07-29 23:31:34 +0200681 return NULL;
682
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100683 sockfd = PyLong_AsSocket_t(fdobj);
684 if (sockfd == (SOCKET_T)(-1) && PyErr_Occurred())
Victor Stinner11517102014-07-29 23:31:34 +0200685 return NULL;
686#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000687 int fd, old_fd;
Victor Stinner11517102014-07-29 23:31:34 +0200688
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800689 if (!PyArg_ParseTupleAndKeywords(args, kwds, "i|$p:set_wakeup_fd", kwlist,
690 &fd, &warn_on_full_buffer))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000691 return NULL;
Victor Stinner11517102014-07-29 23:31:34 +0200692#endif
693
Eric Snow64d6cc82019-02-23 15:40:43 -0700694 if (!is_main()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000695 PyErr_SetString(PyExc_ValueError,
696 "set_wakeup_fd only works in main thread");
697 return NULL;
698 }
Victor Stinner0bffc942014-07-21 16:28:54 +0200699
Victor Stinner11517102014-07-29 23:31:34 +0200700#ifdef MS_WINDOWS
701 is_socket = 0;
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100702 if (sockfd != INVALID_FD) {
Victor Stinner11517102014-07-29 23:31:34 +0200703 /* Import the _socket module to call WSAStartup() */
704 mod = PyImport_ImportModuleNoBlock("_socket");
705 if (mod == NULL)
706 return NULL;
707 Py_DECREF(mod);
708
709 /* test the socket */
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100710 if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR,
Victor Stinner11517102014-07-29 23:31:34 +0200711 (char *)&res, &res_size) != 0) {
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100712 int fd, err;
713
714 err = WSAGetLastError();
Victor Stinner11517102014-07-29 23:31:34 +0200715 if (err != WSAENOTSOCK) {
716 PyErr_SetExcFromWindowsErr(PyExc_OSError, err);
717 return NULL;
718 }
719
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100720 fd = (int)sockfd;
Steve Dower940f33a2016-09-08 11:21:54 -0700721 if ((SOCKET_T)fd != sockfd) {
Victor Stinner11517102014-07-29 23:31:34 +0200722 PyErr_SetString(PyExc_ValueError, "invalid fd");
723 return NULL;
724 }
725
Victor Stinnere134a7f2015-03-30 10:09:31 +0200726 if (_Py_fstat(fd, &status) != 0)
Victor Stinner11517102014-07-29 23:31:34 +0200727 return NULL;
Victor Stinner38227602014-08-27 12:59:44 +0200728
729 /* on Windows, a file cannot be set to non-blocking mode */
Victor Stinner11517102014-07-29 23:31:34 +0200730 }
Victor Stinner38227602014-08-27 12:59:44 +0200731 else {
Victor Stinner11517102014-07-29 23:31:34 +0200732 is_socket = 1;
Victor Stinner38227602014-08-27 12:59:44 +0200733
734 /* Windows does not provide a function to test if a socket
735 is in non-blocking mode */
736 }
Victor Stinner11517102014-07-29 23:31:34 +0200737 }
738
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100739 old_sockfd = wakeup.fd;
740 wakeup.fd = sockfd;
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800741 wakeup.warn_on_full_buffer = warn_on_full_buffer;
Victor Stinner11517102014-07-29 23:31:34 +0200742 wakeup.use_send = is_socket;
743
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100744 if (old_sockfd != INVALID_FD)
745 return PyLong_FromSocket_t(old_sockfd);
Victor Stinner11517102014-07-29 23:31:34 +0200746 else
747 return PyLong_FromLong(-1);
748#else
Victor Stinnerd18ccd12014-07-24 21:58:53 +0200749 if (fd != -1) {
Victor Stinner38227602014-08-27 12:59:44 +0200750 int blocking;
751
Victor Stinnere134a7f2015-03-30 10:09:31 +0200752 if (_Py_fstat(fd, &status) != 0)
Victor Stinner11517102014-07-29 23:31:34 +0200753 return NULL;
Victor Stinner38227602014-08-27 12:59:44 +0200754
755 blocking = _Py_get_blocking(fd);
756 if (blocking < 0)
757 return NULL;
758 if (blocking) {
759 PyErr_Format(PyExc_ValueError,
760 "the fd %i must be in non-blocking mode",
761 fd);
762 return NULL;
763 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000764 }
Victor Stinner0bffc942014-07-21 16:28:54 +0200765
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800766 old_fd = wakeup.fd;
767 wakeup.fd = fd;
768 wakeup.warn_on_full_buffer = warn_on_full_buffer;
Victor Stinner0bffc942014-07-21 16:28:54 +0200769
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000770 return PyLong_FromLong(old_fd);
Victor Stinner11517102014-07-29 23:31:34 +0200771#endif
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000772}
773
774PyDoc_STRVAR(set_wakeup_fd_doc,
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800775"set_wakeup_fd(fd, *, warn_on_full_buffer=True) -> fd\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000776\n\
Victor Stinner11517102014-07-29 23:31:34 +0200777Sets the fd to be written to (with the signal number) when a signal\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000778comes in. A library can use this to wakeup select or poll.\n\
Victor Stinner11517102014-07-29 23:31:34 +0200779The previous fd or -1 is returned.\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000780\n\
781The fd must be non-blocking.");
782
783/* C API for the same, without all the error checking */
784int
785PySignal_SetWakeupFd(int fd)
786{
Victor Stinner11517102014-07-29 23:31:34 +0200787 int old_fd;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000788 if (fd < 0)
789 fd = -1;
Victor Stinner11517102014-07-29 23:31:34 +0200790
791#ifdef MS_WINDOWS
792 old_fd = Py_SAFE_DOWNCAST(wakeup.fd, SOCKET_T, int);
Victor Stinner11517102014-07-29 23:31:34 +0200793#else
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800794 old_fd = wakeup.fd;
Victor Stinner11517102014-07-29 23:31:34 +0200795#endif
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800796 wakeup.fd = fd;
797 wakeup.warn_on_full_buffer = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000798 return old_fd;
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000799}
800
801
Martin v. Löwis823725e2008-03-24 13:39:54 +0000802#ifdef HAVE_SETITIMER
Tal Einatc7027b72015-05-16 14:14:49 +0300803
804/*[clinic input]
805signal.setitimer
806
807 which: int
Victor Stinneref611c92017-10-13 13:49:43 -0700808 seconds: object
809 interval: object(c_default="NULL") = 0.0
Tal Einatc7027b72015-05-16 14:14:49 +0300810 /
811
812Sets given itimer (one of ITIMER_REAL, ITIMER_VIRTUAL or ITIMER_PROF).
813
814The timer will fire after value seconds and after that every interval seconds.
815The itimer can be cleared by setting seconds to zero.
816
817Returns old values as a tuple: (delay, interval).
818[clinic start generated code]*/
819
Martin v. Löwis823725e2008-03-24 13:39:54 +0000820static PyObject *
Victor Stinneref611c92017-10-13 13:49:43 -0700821signal_setitimer_impl(PyObject *module, int which, PyObject *seconds,
822 PyObject *interval)
823/*[clinic end generated code: output=65f9dcbddc35527b input=de43daf194e6f66f]*/
Martin v. Löwis823725e2008-03-24 13:39:54 +0000824{
Martin v. Löwis823725e2008-03-24 13:39:54 +0000825 struct itimerval new, old;
826
Victor Stinneref611c92017-10-13 13:49:43 -0700827 if (timeval_from_double(seconds, &new.it_value) < 0) {
828 return NULL;
829 }
830 if (timeval_from_double(interval, &new.it_interval) < 0) {
831 return NULL;
832 }
833
Martin v. Löwis823725e2008-03-24 13:39:54 +0000834 /* Let OS check "which" value */
835 if (setitimer(which, &new, &old) != 0) {
Tal Einatc7027b72015-05-16 14:14:49 +0300836 PyErr_SetFromErrno(ItimerError);
837 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000838 }
839
840 return itimer_retval(&old);
841}
842
Martin v. Löwis823725e2008-03-24 13:39:54 +0000843#endif
844
845
846#ifdef HAVE_GETITIMER
Tal Einatc7027b72015-05-16 14:14:49 +0300847
848/*[clinic input]
849signal.getitimer
850
851 which: int
852 /
853
854Returns current value of given itimer.
855[clinic start generated code]*/
856
Martin v. Löwis823725e2008-03-24 13:39:54 +0000857static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300858signal_getitimer_impl(PyObject *module, int which)
859/*[clinic end generated code: output=9e053175d517db40 input=f7d21d38f3490627]*/
Martin v. Löwis823725e2008-03-24 13:39:54 +0000860{
Martin v. Löwis823725e2008-03-24 13:39:54 +0000861 struct itimerval old;
862
Martin v. Löwis823725e2008-03-24 13:39:54 +0000863 if (getitimer(which, &old) != 0) {
Tal Einatc7027b72015-05-16 14:14:49 +0300864 PyErr_SetFromErrno(ItimerError);
865 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000866 }
867
868 return itimer_retval(&old);
869}
870
Martin v. Löwis823725e2008-03-24 13:39:54 +0000871#endif
872
Victor Stinnerb3e72192011-05-08 01:46:11 +0200873#if defined(PYPTHREAD_SIGMASK) || defined(HAVE_SIGPENDING)
Victor Stinner35b300c2011-05-04 13:20:35 +0200874static PyObject*
875sigset_to_set(sigset_t mask)
876{
877 PyObject *signum, *result;
878 int sig;
879
880 result = PySet_New(0);
881 if (result == NULL)
882 return NULL;
883
884 for (sig = 1; sig < NSIG; sig++) {
885 if (sigismember(&mask, sig) != 1)
886 continue;
887
888 /* Handle the case where it is a member by adding the signal to
889 the result list. Ignore the other cases because they mean the
890 signal isn't a member of the mask or the signal was invalid,
891 and an invalid signal must have been our fault in constructing
892 the loop boundaries. */
893 signum = PyLong_FromLong(sig);
894 if (signum == NULL) {
895 Py_DECREF(result);
896 return NULL;
897 }
898 if (PySet_Add(result, signum) == -1) {
899 Py_DECREF(signum);
900 Py_DECREF(result);
901 return NULL;
902 }
903 Py_DECREF(signum);
904 }
905 return result;
906}
Victor Stinnerb3e72192011-05-08 01:46:11 +0200907#endif
Victor Stinner35b300c2011-05-04 13:20:35 +0200908
Victor Stinnerb3e72192011-05-08 01:46:11 +0200909#ifdef PYPTHREAD_SIGMASK
Tal Einatc7027b72015-05-16 14:14:49 +0300910
911/*[clinic input]
912signal.pthread_sigmask
913
914 how: int
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300915 mask: sigset_t
Tal Einatc7027b72015-05-16 14:14:49 +0300916 /
917
918Fetch and/or change the signal mask of the calling thread.
919[clinic start generated code]*/
920
Victor Stinnera9293352011-04-30 15:21:58 +0200921static PyObject *
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300922signal_pthread_sigmask_impl(PyObject *module, int how, sigset_t mask)
923/*[clinic end generated code: output=0562c0fb192981a8 input=85bcebda442fa77f]*/
Victor Stinnera9293352011-04-30 15:21:58 +0200924{
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300925 sigset_t previous;
Victor Stinnera9293352011-04-30 15:21:58 +0200926 int err;
927
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300928 err = pthread_sigmask(how, &mask, &previous);
Victor Stinnera9293352011-04-30 15:21:58 +0200929 if (err != 0) {
930 errno = err;
Victor Stinnerb3e72192011-05-08 01:46:11 +0200931 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnera9293352011-04-30 15:21:58 +0200932 return NULL;
933 }
934
Victor Stinnerd0e516d2011-05-03 14:57:12 +0200935 /* if signals was unblocked, signal handlers have been called */
936 if (PyErr_CheckSignals())
937 return NULL;
938
Victor Stinner35b300c2011-05-04 13:20:35 +0200939 return sigset_to_set(previous);
Victor Stinnera9293352011-04-30 15:21:58 +0200940}
941
Victor Stinnera9293352011-04-30 15:21:58 +0200942#endif /* #ifdef PYPTHREAD_SIGMASK */
943
Martin v. Löwis823725e2008-03-24 13:39:54 +0000944
Victor Stinnerb3e72192011-05-08 01:46:11 +0200945#ifdef HAVE_SIGPENDING
Tal Einatc7027b72015-05-16 14:14:49 +0300946
947/*[clinic input]
948signal.sigpending
949
950Examine pending signals.
951
952Returns a set of signal numbers that are pending for delivery to
953the calling thread.
954[clinic start generated code]*/
955
Victor Stinnerb3e72192011-05-08 01:46:11 +0200956static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300957signal_sigpending_impl(PyObject *module)
958/*[clinic end generated code: output=53375ffe89325022 input=e0036c016f874e29]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +0200959{
960 int err;
961 sigset_t mask;
962 err = sigpending(&mask);
963 if (err)
964 return PyErr_SetFromErrno(PyExc_OSError);
965 return sigset_to_set(mask);
966}
967
Victor Stinnerb3e72192011-05-08 01:46:11 +0200968#endif /* #ifdef HAVE_SIGPENDING */
969
970
971#ifdef HAVE_SIGWAIT
Tal Einatc7027b72015-05-16 14:14:49 +0300972
973/*[clinic input]
974signal.sigwait
975
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300976 sigset: sigset_t
Tal Einatc7027b72015-05-16 14:14:49 +0300977 /
978
979Wait for a signal.
980
981Suspend execution of the calling thread until the delivery of one of the
982signals specified in the signal set sigset. The function accepts the signal
983and returns the signal number.
984[clinic start generated code]*/
985
Victor Stinnerb3e72192011-05-08 01:46:11 +0200986static PyObject *
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300987signal_sigwait_impl(PyObject *module, sigset_t sigset)
988/*[clinic end generated code: output=f43770699d682f96 input=a6fbd47b1086d119]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +0200989{
Victor Stinnerb3e72192011-05-08 01:46:11 +0200990 int err, signum;
991
Victor Stinner10c30d62011-06-10 01:39:53 +0200992 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300993 err = sigwait(&sigset, &signum);
Victor Stinner10c30d62011-06-10 01:39:53 +0200994 Py_END_ALLOW_THREADS
Victor Stinnerb3e72192011-05-08 01:46:11 +0200995 if (err) {
996 errno = err;
997 return PyErr_SetFromErrno(PyExc_OSError);
998 }
999
1000 return PyLong_FromLong(signum);
1001}
1002
Tal Einatc7027b72015-05-16 14:14:49 +03001003#endif /* #ifdef HAVE_SIGWAIT */
1004
Victor Stinnerb3e72192011-05-08 01:46:11 +02001005
Antoine Pitrou9d3627e2018-05-04 13:00:50 +02001006#if defined(HAVE_SIGFILLSET) || defined(MS_WINDOWS)
1007
1008/*[clinic input]
1009signal.valid_signals
1010
1011Return a set of valid signal numbers on this platform.
1012
1013The signal numbers returned by this function can be safely passed to
1014functions like `pthread_sigmask`.
1015[clinic start generated code]*/
1016
1017static PyObject *
1018signal_valid_signals_impl(PyObject *module)
1019/*[clinic end generated code: output=1609cffbcfcf1314 input=86a3717ff25288f2]*/
1020{
1021#ifdef MS_WINDOWS
1022#ifdef SIGBREAK
1023 PyObject *tup = Py_BuildValue("(iiiiiii)", SIGABRT, SIGBREAK, SIGFPE,
1024 SIGILL, SIGINT, SIGSEGV, SIGTERM);
1025#else
1026 PyObject *tup = Py_BuildValue("(iiiiii)", SIGABRT, SIGFPE, SIGILL,
1027 SIGINT, SIGSEGV, SIGTERM);
1028#endif
1029 if (tup == NULL) {
1030 return NULL;
1031 }
1032 PyObject *set = PySet_New(tup);
1033 Py_DECREF(tup);
1034 return set;
1035#else
1036 sigset_t mask;
1037 if (sigemptyset(&mask) || sigfillset(&mask)) {
1038 return PyErr_SetFromErrno(PyExc_OSError);
1039 }
1040 return sigset_to_set(mask);
1041#endif
1042}
1043
1044#endif /* #if defined(HAVE_SIGFILLSET) || defined(MS_WINDOWS) */
1045
1046
Ross Lagerwallbc808222011-06-25 12:13:40 +02001047#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
1048static int initialized;
1049static PyStructSequence_Field struct_siginfo_fields[] = {
1050 {"si_signo", "signal number"},
1051 {"si_code", "signal code"},
1052 {"si_errno", "errno associated with this signal"},
1053 {"si_pid", "sending process ID"},
1054 {"si_uid", "real user ID of sending process"},
1055 {"si_status", "exit value or signal"},
1056 {"si_band", "band event for SIGPOLL"},
1057 {0}
1058};
1059
1060PyDoc_STRVAR(struct_siginfo__doc__,
1061"struct_siginfo: Result from sigwaitinfo or sigtimedwait.\n\n\
1062This object may be accessed either as a tuple of\n\
1063(si_signo, si_code, si_errno, si_pid, si_uid, si_status, si_band),\n\
1064or via the attributes si_signo, si_code, and so on.");
1065
1066static PyStructSequence_Desc struct_siginfo_desc = {
1067 "signal.struct_siginfo", /* name */
1068 struct_siginfo__doc__, /* doc */
1069 struct_siginfo_fields, /* fields */
1070 7 /* n_in_sequence */
1071};
1072
1073static PyTypeObject SiginfoType;
1074
1075static PyObject *
1076fill_siginfo(siginfo_t *si)
1077{
1078 PyObject *result = PyStructSequence_New(&SiginfoType);
1079 if (!result)
1080 return NULL;
1081
1082 PyStructSequence_SET_ITEM(result, 0, PyLong_FromLong((long)(si->si_signo)));
1083 PyStructSequence_SET_ITEM(result, 1, PyLong_FromLong((long)(si->si_code)));
Victor Stinner09532fe2019-05-10 23:39:09 +02001084#ifdef __VXWORKS__
pxinwr8b5bdda2019-03-14 01:18:25 +08001085 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong(0L));
1086 PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong(0L));
1087 PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong(0L));
1088 PyStructSequence_SET_ITEM(result, 5, PyLong_FromLong(0L));
Victor Stinner09532fe2019-05-10 23:39:09 +02001089#else
Ross Lagerwallbc808222011-06-25 12:13:40 +02001090 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si->si_errno)));
1091 PyStructSequence_SET_ITEM(result, 3, PyLong_FromPid(si->si_pid));
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02001092 PyStructSequence_SET_ITEM(result, 4, _PyLong_FromUid(si->si_uid));
Ross Lagerwallbc808222011-06-25 12:13:40 +02001093 PyStructSequence_SET_ITEM(result, 5,
1094 PyLong_FromLong((long)(si->si_status)));
Victor Stinner09532fe2019-05-10 23:39:09 +02001095#endif
Zachary Ware6a6967e2016-10-01 00:47:27 -05001096#ifdef HAVE_SIGINFO_T_SI_BAND
Ross Lagerwallbc808222011-06-25 12:13:40 +02001097 PyStructSequence_SET_ITEM(result, 6, PyLong_FromLong(si->si_band));
Zachary Ware6a6967e2016-10-01 00:47:27 -05001098#else
1099 PyStructSequence_SET_ITEM(result, 6, PyLong_FromLong(0L));
1100#endif
Ross Lagerwallbc808222011-06-25 12:13:40 +02001101 if (PyErr_Occurred()) {
1102 Py_DECREF(result);
1103 return NULL;
1104 }
1105
1106 return result;
1107}
1108#endif
1109
1110#ifdef HAVE_SIGWAITINFO
Tal Einatc7027b72015-05-16 14:14:49 +03001111
1112/*[clinic input]
1113signal.sigwaitinfo
1114
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001115 sigset: sigset_t
Tal Einatc7027b72015-05-16 14:14:49 +03001116 /
1117
1118Wait synchronously until one of the signals in *sigset* is delivered.
1119
1120Returns a struct_siginfo containing information about the signal.
1121[clinic start generated code]*/
1122
Ross Lagerwallbc808222011-06-25 12:13:40 +02001123static PyObject *
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001124signal_sigwaitinfo_impl(PyObject *module, sigset_t sigset)
1125/*[clinic end generated code: output=1eb2f1fa236fdbca input=3d1a7e1f27fc664c]*/
Ross Lagerwallbc808222011-06-25 12:13:40 +02001126{
Ross Lagerwallbc808222011-06-25 12:13:40 +02001127 siginfo_t si;
1128 int err;
Victor Stinnera453cd82015-03-20 12:54:28 +01001129 int async_err = 0;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001130
Victor Stinnera453cd82015-03-20 12:54:28 +01001131 do {
1132 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001133 err = sigwaitinfo(&sigset, &si);
Victor Stinnera453cd82015-03-20 12:54:28 +01001134 Py_END_ALLOW_THREADS
1135 } while (err == -1
1136 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Ross Lagerwallbc808222011-06-25 12:13:40 +02001137 if (err == -1)
Victor Stinnera453cd82015-03-20 12:54:28 +01001138 return (!async_err) ? PyErr_SetFromErrno(PyExc_OSError) : NULL;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001139
1140 return fill_siginfo(&si);
1141}
1142
Ross Lagerwallbc808222011-06-25 12:13:40 +02001143#endif /* #ifdef HAVE_SIGWAITINFO */
1144
1145#ifdef HAVE_SIGTIMEDWAIT
Tal Einatc7027b72015-05-16 14:14:49 +03001146
1147/*[clinic input]
1148signal.sigtimedwait
1149
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001150 sigset: sigset_t
Serhiy Storchaka6b680cd2015-05-16 15:57:56 +03001151 timeout as timeout_obj: object
Tal Einatc7027b72015-05-16 14:14:49 +03001152 /
1153
1154Like sigwaitinfo(), but with a timeout.
1155
1156The timeout is specified in seconds, with floating point numbers allowed.
1157[clinic start generated code]*/
1158
Ross Lagerwallbc808222011-06-25 12:13:40 +02001159static PyObject *
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001160signal_sigtimedwait_impl(PyObject *module, sigset_t sigset,
Serhiy Storchaka6b680cd2015-05-16 15:57:56 +03001161 PyObject *timeout_obj)
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001162/*[clinic end generated code: output=59c8971e8ae18a64 input=87fd39237cf0b7ba]*/
Ross Lagerwallbc808222011-06-25 12:13:40 +02001163{
Victor Stinnera453cd82015-03-20 12:54:28 +01001164 struct timespec ts;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001165 siginfo_t si;
Victor Stinnera453cd82015-03-20 12:54:28 +01001166 int res;
Victor Stinner34dc0f42015-03-27 18:19:03 +01001167 _PyTime_t timeout, deadline, monotonic;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001168
Victor Stinner869e1772015-03-30 03:49:14 +02001169 if (_PyTime_FromSecondsObject(&timeout,
1170 timeout_obj, _PyTime_ROUND_CEILING) < 0)
Ross Lagerwallbc808222011-06-25 12:13:40 +02001171 return NULL;
1172
Victor Stinnera453cd82015-03-20 12:54:28 +01001173 if (timeout < 0) {
Ross Lagerwallbc808222011-06-25 12:13:40 +02001174 PyErr_SetString(PyExc_ValueError, "timeout must be non-negative");
1175 return NULL;
1176 }
1177
Victor Stinner34dc0f42015-03-27 18:19:03 +01001178 deadline = _PyTime_GetMonotonicClock() + timeout;
Victor Stinnera453cd82015-03-20 12:54:28 +01001179
1180 do {
Victor Stinner34dc0f42015-03-27 18:19:03 +01001181 if (_PyTime_AsTimespec(timeout, &ts) < 0)
1182 return NULL;
Victor Stinnera453cd82015-03-20 12:54:28 +01001183
1184 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001185 res = sigtimedwait(&sigset, &si, &ts);
Victor Stinnera453cd82015-03-20 12:54:28 +01001186 Py_END_ALLOW_THREADS
1187
1188 if (res != -1)
1189 break;
1190
1191 if (errno != EINTR) {
1192 if (errno == EAGAIN)
1193 Py_RETURN_NONE;
1194 else
1195 return PyErr_SetFromErrno(PyExc_OSError);
1196 }
1197
1198 /* sigtimedwait() was interrupted by a signal (EINTR) */
1199 if (PyErr_CheckSignals())
1200 return NULL;
1201
Victor Stinner34dc0f42015-03-27 18:19:03 +01001202 monotonic = _PyTime_GetMonotonicClock();
1203 timeout = deadline - monotonic;
Victor Stinner6aa446c2015-03-30 21:33:51 +02001204 if (timeout < 0)
Victor Stinnera453cd82015-03-20 12:54:28 +01001205 break;
1206 } while (1);
Ross Lagerwallbc808222011-06-25 12:13:40 +02001207
1208 return fill_siginfo(&si);
1209}
1210
Ross Lagerwallbc808222011-06-25 12:13:40 +02001211#endif /* #ifdef HAVE_SIGTIMEDWAIT */
1212
Victor Stinnerb3e72192011-05-08 01:46:11 +02001213
Antoine Pitroua6a4dc82017-09-07 18:56:24 +02001214#if defined(HAVE_PTHREAD_KILL)
Tal Einatc7027b72015-05-16 14:14:49 +03001215
1216/*[clinic input]
1217signal.pthread_kill
1218
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +02001219 thread_id: unsigned_long(bitwise=True)
Tal Einatc7027b72015-05-16 14:14:49 +03001220 signalnum: int
1221 /
1222
1223Send a signal to a thread.
1224[clinic start generated code]*/
1225
Victor Stinnerb3e72192011-05-08 01:46:11 +02001226static PyObject *
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +02001227signal_pthread_kill_impl(PyObject *module, unsigned long thread_id,
1228 int signalnum)
1229/*[clinic end generated code: output=7629919b791bc27f input=1d901f2c7bb544ff]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +02001230{
Victor Stinnerb3e72192011-05-08 01:46:11 +02001231 int err;
1232
Tal Einatc7027b72015-05-16 14:14:49 +03001233 err = pthread_kill((pthread_t)thread_id, signalnum);
Victor Stinnerb3e72192011-05-08 01:46:11 +02001234 if (err != 0) {
1235 errno = err;
1236 PyErr_SetFromErrno(PyExc_OSError);
1237 return NULL;
1238 }
1239
1240 /* the signal may have been send to the current thread */
1241 if (PyErr_CheckSignals())
1242 return NULL;
1243
1244 Py_RETURN_NONE;
1245}
1246
Antoine Pitroua6a4dc82017-09-07 18:56:24 +02001247#endif /* #if defined(HAVE_PTHREAD_KILL) */
Victor Stinnerb3e72192011-05-08 01:46:11 +02001248
1249
1250
Tal Einatc7027b72015-05-16 14:14:49 +03001251/* List of functions defined in the module -- some of the methoddefs are
1252 defined to nothing if the corresponding C function is not available. */
Barry Warsaw92971171997-01-03 00:14:25 +00001253static PyMethodDef signal_methods[] = {
Tal Einatc7027b72015-05-16 14:14:49 +03001254 {"default_int_handler", signal_default_int_handler, METH_VARARGS, default_int_handler_doc},
1255 SIGNAL_ALARM_METHODDEF
1256 SIGNAL_SETITIMER_METHODDEF
1257 SIGNAL_GETITIMER_METHODDEF
1258 SIGNAL_SIGNAL_METHODDEF
Vladimir Matveevc24c6c22019-01-08 01:58:25 -08001259 SIGNAL_RAISE_SIGNAL_METHODDEF
Antoine Pietri5d2a27d2018-03-12 14:42:34 +01001260 SIGNAL_STRSIGNAL_METHODDEF
Tal Einatc7027b72015-05-16 14:14:49 +03001261 SIGNAL_GETSIGNAL_METHODDEF
Serhiy Storchaka62be7422018-11-27 13:27:31 +02001262 {"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 +03001263 SIGNAL_SIGINTERRUPT_METHODDEF
1264 SIGNAL_PAUSE_METHODDEF
1265 SIGNAL_PTHREAD_KILL_METHODDEF
1266 SIGNAL_PTHREAD_SIGMASK_METHODDEF
1267 SIGNAL_SIGPENDING_METHODDEF
1268 SIGNAL_SIGWAIT_METHODDEF
1269 SIGNAL_SIGWAITINFO_METHODDEF
1270 SIGNAL_SIGTIMEDWAIT_METHODDEF
Antoine Pitrou9d3627e2018-05-04 13:00:50 +02001271#if defined(HAVE_SIGFILLSET) || defined(MS_WINDOWS)
1272 SIGNAL_VALID_SIGNALS_METHODDEF
1273#endif
Tal Einatc7027b72015-05-16 14:14:49 +03001274 {NULL, NULL} /* sentinel */
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001275};
1276
Barry Warsaw92971171997-01-03 00:14:25 +00001277
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001278PyDoc_STRVAR(module_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001279"This module provides mechanisms to use signal handlers in Python.\n\
1280\n\
1281Functions:\n\
1282\n\
1283alarm() -- cause SIGALRM after a specified time [Unix only]\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001284setitimer() -- cause a signal (described below) after a specified\n\
1285 float time and the timer may restart then [Unix only]\n\
1286getitimer() -- get current value of timer [Unix only]\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001287signal() -- set the action for a given signal\n\
1288getsignal() -- get the signal action for a given signal\n\
1289pause() -- wait until a signal arrives [Unix only]\n\
1290default_int_handler() -- default SIGINT handler\n\
1291\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001292signal constants:\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001293SIG_DFL -- used to refer to the system default handler\n\
1294SIG_IGN -- used to ignore the signal\n\
1295NSIG -- number of defined signals\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001296SIGINT, SIGTERM, etc. -- signal numbers\n\
1297\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001298itimer constants:\n\
1299ITIMER_REAL -- decrements in real time, and delivers SIGALRM upon\n\
1300 expiration\n\
1301ITIMER_VIRTUAL -- decrements only when the process is executing,\n\
1302 and delivers SIGVTALRM upon expiration\n\
1303ITIMER_PROF -- decrements both when the process is executing and\n\
1304 when the system is executing on behalf of the process.\n\
1305 Coupled with ITIMER_VIRTUAL, this timer is usually\n\
1306 used to profile the time spent by the application\n\
1307 in user and kernel space. SIGPROF is delivered upon\n\
1308 expiration.\n\
1309\n\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001310*** IMPORTANT NOTICE ***\n\
1311A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001312the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001313
Martin v. Löwis1a214512008-06-11 05:26:20 +00001314static struct PyModuleDef signalmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001315 PyModuleDef_HEAD_INIT,
Brett Cannon815a6f32014-04-04 10:20:28 -04001316 "_signal",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001317 module_doc,
1318 -1,
1319 signal_methods,
1320 NULL,
1321 NULL,
1322 NULL,
1323 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001324};
1325
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001326PyMODINIT_FUNC
Giampaolo Rodola'e09fb712014-04-04 15:34:17 +02001327PyInit__signal(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001328{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001329 PyObject *m, *d, *x;
1330 int i;
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001331
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001332 main_thread = PyThread_get_thread_ident();
Eric Snow64d6cc82019-02-23 15:40:43 -07001333 main_interp = _PyInterpreterState_Get();
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001334
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001335 /* Create the module and add the functions */
1336 m = PyModule_Create(&signalmodule);
1337 if (m == NULL)
1338 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001339
Ross Lagerwallbc808222011-06-25 12:13:40 +02001340#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
Victor Stinner1c8f0592013-07-22 22:24:54 +02001341 if (!initialized) {
1342 if (PyStructSequence_InitType2(&SiginfoType, &struct_siginfo_desc) < 0)
1343 return NULL;
1344 }
Ross Lagerwallbc808222011-06-25 12:13:40 +02001345 Py_INCREF((PyObject*) &SiginfoType);
1346 PyModule_AddObject(m, "struct_siginfo", (PyObject*) &SiginfoType);
1347 initialized = 1;
1348#endif
1349
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001350 /* Add some symbolic constants to the module */
1351 d = PyModule_GetDict(m);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001352
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001353 x = DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL);
Joannah Nanjekye9541bd32019-04-21 21:47:06 -04001354 if (PyModule_AddObject(m, "SIG_DFL", x))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001355 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001356
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001357 x = IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN);
Joannah Nanjekye9541bd32019-04-21 21:47:06 -04001358 if (PyModule_AddObject(m, "SIG_IGN", x))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001359 goto finally;
Barry Warsaw92971171997-01-03 00:14:25 +00001360
Joannah Nanjekye9541bd32019-04-21 21:47:06 -04001361 if (PyModule_AddIntMacro(m, NSIG))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001362 goto finally;
Barry Warsaw92971171997-01-03 00:14:25 +00001363
Victor Stinnera9293352011-04-30 15:21:58 +02001364#ifdef SIG_BLOCK
Victor Stinner72c53b52011-05-02 16:15:43 +02001365 if (PyModule_AddIntMacro(m, SIG_BLOCK))
1366 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001367#endif
Victor Stinnera9293352011-04-30 15:21:58 +02001368#ifdef SIG_UNBLOCK
Victor Stinner72c53b52011-05-02 16:15:43 +02001369 if (PyModule_AddIntMacro(m, SIG_UNBLOCK))
1370 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001371#endif
Victor Stinnera9293352011-04-30 15:21:58 +02001372#ifdef SIG_SETMASK
Victor Stinner72c53b52011-05-02 16:15:43 +02001373 if (PyModule_AddIntMacro(m, SIG_SETMASK))
1374 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001375#endif
1376
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001377 x = IntHandler = PyDict_GetItemString(d, "default_int_handler");
1378 if (!x)
1379 goto finally;
1380 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +00001381
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001382 _Py_atomic_store_relaxed(&Handlers[0].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001383 for (i = 1; i < NSIG; i++) {
1384 void (*t)(int);
1385 t = PyOS_getsig(i);
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001386 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001387 if (t == SIG_DFL)
1388 Handlers[i].func = DefaultHandler;
1389 else if (t == SIG_IGN)
1390 Handlers[i].func = IgnoreHandler;
1391 else
1392 Handlers[i].func = Py_None; /* None of our business */
1393 Py_INCREF(Handlers[i].func);
1394 }
1395 if (Handlers[SIGINT].func == DefaultHandler) {
1396 /* Install default int handler */
1397 Py_INCREF(IntHandler);
Serhiy Storchaka57a01d32016-04-10 18:05:40 +03001398 Py_SETREF(Handlers[SIGINT].func, IntHandler);
pkerlinge905c842018-06-01 09:47:18 +00001399 PyOS_setsig(SIGINT, signal_handler);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001400 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001401
1402#ifdef SIGHUP
Christian Heimes6782b142016-09-09 00:11:45 +02001403 if (PyModule_AddIntMacro(m, SIGHUP))
1404 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001405#endif
1406#ifdef SIGINT
Christian Heimes6782b142016-09-09 00:11:45 +02001407 if (PyModule_AddIntMacro(m, SIGINT))
1408 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001409#endif
Tim Peters1ce3cf72001-10-01 17:58:40 +00001410#ifdef SIGBREAK
Christian Heimes6782b142016-09-09 00:11:45 +02001411 if (PyModule_AddIntMacro(m, SIGBREAK))
1412 goto finally;
Tim Peters1ce3cf72001-10-01 17:58:40 +00001413#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001414#ifdef SIGQUIT
Christian Heimes6782b142016-09-09 00:11:45 +02001415 if (PyModule_AddIntMacro(m, SIGQUIT))
1416 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001417#endif
1418#ifdef SIGILL
Christian Heimes6782b142016-09-09 00:11:45 +02001419 if (PyModule_AddIntMacro(m, SIGILL))
1420 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001421#endif
1422#ifdef SIGTRAP
Christian Heimes6782b142016-09-09 00:11:45 +02001423 if (PyModule_AddIntMacro(m, SIGTRAP))
1424 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001425#endif
1426#ifdef SIGIOT
Christian Heimes6782b142016-09-09 00:11:45 +02001427 if (PyModule_AddIntMacro(m, SIGIOT))
1428 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001429#endif
1430#ifdef SIGABRT
Christian Heimes6782b142016-09-09 00:11:45 +02001431 if (PyModule_AddIntMacro(m, SIGABRT))
1432 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001433#endif
1434#ifdef SIGEMT
Christian Heimes6782b142016-09-09 00:11:45 +02001435 if (PyModule_AddIntMacro(m, SIGEMT))
1436 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001437#endif
1438#ifdef SIGFPE
Christian Heimes6782b142016-09-09 00:11:45 +02001439 if (PyModule_AddIntMacro(m, SIGFPE))
1440 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001441#endif
1442#ifdef SIGKILL
Christian Heimes6782b142016-09-09 00:11:45 +02001443 if (PyModule_AddIntMacro(m, SIGKILL))
1444 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001445#endif
1446#ifdef SIGBUS
Christian Heimes6782b142016-09-09 00:11:45 +02001447 if (PyModule_AddIntMacro(m, SIGBUS))
1448 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001449#endif
1450#ifdef SIGSEGV
Christian Heimes6782b142016-09-09 00:11:45 +02001451 if (PyModule_AddIntMacro(m, SIGSEGV))
1452 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001453#endif
1454#ifdef SIGSYS
Christian Heimes6782b142016-09-09 00:11:45 +02001455 if (PyModule_AddIntMacro(m, SIGSYS))
1456 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001457#endif
1458#ifdef SIGPIPE
Christian Heimes6782b142016-09-09 00:11:45 +02001459 if (PyModule_AddIntMacro(m, SIGPIPE))
1460 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001461#endif
1462#ifdef SIGALRM
Christian Heimes6782b142016-09-09 00:11:45 +02001463 if (PyModule_AddIntMacro(m, SIGALRM))
1464 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001465#endif
1466#ifdef SIGTERM
Christian Heimes6782b142016-09-09 00:11:45 +02001467 if (PyModule_AddIntMacro(m, SIGTERM))
1468 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001469#endif
1470#ifdef SIGUSR1
Christian Heimes6782b142016-09-09 00:11:45 +02001471 if (PyModule_AddIntMacro(m, SIGUSR1))
1472 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001473#endif
1474#ifdef SIGUSR2
Christian Heimes6782b142016-09-09 00:11:45 +02001475 if (PyModule_AddIntMacro(m, SIGUSR2))
1476 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001477#endif
1478#ifdef SIGCLD
Christian Heimes6782b142016-09-09 00:11:45 +02001479 if (PyModule_AddIntMacro(m, SIGCLD))
1480 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001481#endif
1482#ifdef SIGCHLD
Christian Heimes6782b142016-09-09 00:11:45 +02001483 if (PyModule_AddIntMacro(m, SIGCHLD))
1484 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001485#endif
1486#ifdef SIGPWR
Christian Heimes6782b142016-09-09 00:11:45 +02001487 if (PyModule_AddIntMacro(m, SIGPWR))
1488 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001489#endif
1490#ifdef SIGIO
Christian Heimes6782b142016-09-09 00:11:45 +02001491 if (PyModule_AddIntMacro(m, SIGIO))
1492 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001493#endif
1494#ifdef SIGURG
Christian Heimes6782b142016-09-09 00:11:45 +02001495 if (PyModule_AddIntMacro(m, SIGURG))
1496 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001497#endif
1498#ifdef SIGWINCH
Christian Heimes6782b142016-09-09 00:11:45 +02001499 if (PyModule_AddIntMacro(m, SIGWINCH))
1500 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001501#endif
1502#ifdef SIGPOLL
Christian Heimes6782b142016-09-09 00:11:45 +02001503 if (PyModule_AddIntMacro(m, SIGPOLL))
1504 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001505#endif
1506#ifdef SIGSTOP
Christian Heimes6782b142016-09-09 00:11:45 +02001507 if (PyModule_AddIntMacro(m, SIGSTOP))
1508 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001509#endif
1510#ifdef SIGTSTP
Christian Heimes6782b142016-09-09 00:11:45 +02001511 if (PyModule_AddIntMacro(m, SIGTSTP))
1512 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001513#endif
1514#ifdef SIGCONT
Christian Heimes6782b142016-09-09 00:11:45 +02001515 if (PyModule_AddIntMacro(m, SIGCONT))
1516 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001517#endif
1518#ifdef SIGTTIN
Christian Heimes6782b142016-09-09 00:11:45 +02001519 if (PyModule_AddIntMacro(m, SIGTTIN))
1520 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001521#endif
1522#ifdef SIGTTOU
Christian Heimes6782b142016-09-09 00:11:45 +02001523 if (PyModule_AddIntMacro(m, SIGTTOU))
1524 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001525#endif
1526#ifdef SIGVTALRM
Christian Heimes6782b142016-09-09 00:11:45 +02001527 if (PyModule_AddIntMacro(m, SIGVTALRM))
1528 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001529#endif
1530#ifdef SIGPROF
Christian Heimes6782b142016-09-09 00:11:45 +02001531 if (PyModule_AddIntMacro(m, SIGPROF))
1532 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001533#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001534#ifdef SIGXCPU
Christian Heimes6782b142016-09-09 00:11:45 +02001535 if (PyModule_AddIntMacro(m, SIGXCPU))
1536 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001537#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001538#ifdef SIGXFSZ
Christian Heimes6782b142016-09-09 00:11:45 +02001539 if (PyModule_AddIntMacro(m, SIGXFSZ))
1540 goto finally;
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001541#endif
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001542#ifdef SIGRTMIN
Christian Heimes6782b142016-09-09 00:11:45 +02001543 if (PyModule_AddIntMacro(m, SIGRTMIN))
1544 goto finally;
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001545#endif
1546#ifdef SIGRTMAX
Christian Heimes6782b142016-09-09 00:11:45 +02001547 if (PyModule_AddIntMacro(m, SIGRTMAX))
1548 goto finally;
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001549#endif
Martin v. Löwis175af252002-01-12 11:43:25 +00001550#ifdef SIGINFO
Christian Heimes6782b142016-09-09 00:11:45 +02001551 if (PyModule_AddIntMacro(m, SIGINFO))
1552 goto finally;
Martin v. Löwis175af252002-01-12 11:43:25 +00001553#endif
Martin v. Löwis823725e2008-03-24 13:39:54 +00001554
1555#ifdef ITIMER_REAL
Christian Heimes6782b142016-09-09 00:11:45 +02001556 if (PyModule_AddIntMacro(m, ITIMER_REAL))
1557 goto finally;
Martin v. Löwis823725e2008-03-24 13:39:54 +00001558#endif
1559#ifdef ITIMER_VIRTUAL
Christian Heimes6782b142016-09-09 00:11:45 +02001560 if (PyModule_AddIntMacro(m, ITIMER_VIRTUAL))
1561 goto finally;
Martin v. Löwis823725e2008-03-24 13:39:54 +00001562#endif
1563#ifdef ITIMER_PROF
Christian Heimes6782b142016-09-09 00:11:45 +02001564 if (PyModule_AddIntMacro(m, ITIMER_PROF))
1565 goto finally;
Martin v. Löwis823725e2008-03-24 13:39:54 +00001566#endif
1567
1568#if defined (HAVE_SETITIMER) || defined (HAVE_GETITIMER)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001569 ItimerError = PyErr_NewException("signal.ItimerError",
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +03001570 PyExc_OSError, NULL);
Joannah Nanjekye9541bd32019-04-21 21:47:06 -04001571 if (PyModule_AddObject(m, "ItimerError", ItimerError))
1572 goto finally;
Martin v. Löwis823725e2008-03-24 13:39:54 +00001573#endif
1574
Brian Curtineb24d742010-04-12 17:16:38 +00001575#ifdef CTRL_C_EVENT
Christian Heimes6782b142016-09-09 00:11:45 +02001576 if (PyModule_AddIntMacro(m, CTRL_C_EVENT))
1577 goto finally;
Brian Curtineb24d742010-04-12 17:16:38 +00001578#endif
1579
1580#ifdef CTRL_BREAK_EVENT
Christian Heimes6782b142016-09-09 00:11:45 +02001581 if (PyModule_AddIntMacro(m, CTRL_BREAK_EVENT))
1582 goto finally;
Brian Curtineb24d742010-04-12 17:16:38 +00001583#endif
1584
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001585#ifdef MS_WINDOWS
1586 /* Create manual-reset event, initially unset */
1587 sigint_event = CreateEvent(NULL, TRUE, FALSE, FALSE);
1588#endif
1589
Martin v. Löwis1a214512008-06-11 05:26:20 +00001590 if (PyErr_Occurred()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001591 Py_DECREF(m);
1592 m = NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001593 }
Barry Warsaw92971171997-01-03 00:14:25 +00001594
Barry Warsaw92971171997-01-03 00:14:25 +00001595 finally:
Martin v. Löwis1a214512008-06-11 05:26:20 +00001596 return m;
Guido van Rossum08c16611997-08-02 03:01:42 +00001597}
1598
1599static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001600finisignal(void)
Guido van Rossum08c16611997-08-02 03:01:42 +00001601{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001602 int i;
1603 PyObject *func;
Guido van Rossum08c16611997-08-02 03:01:42 +00001604
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001605 for (i = 1; i < NSIG; i++) {
1606 func = Handlers[i].func;
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001607 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001608 Handlers[i].func = NULL;
pkerlinge905c842018-06-01 09:47:18 +00001609 if (func != NULL && func != Py_None &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001610 func != DefaultHandler && func != IgnoreHandler)
1611 PyOS_setsig(i, SIG_DFL);
1612 Py_XDECREF(func);
1613 }
Guido van Rossum08c16611997-08-02 03:01:42 +00001614
Serhiy Storchaka505ff752014-02-09 13:33:53 +02001615 Py_CLEAR(IntHandler);
1616 Py_CLEAR(DefaultHandler);
1617 Py_CLEAR(IgnoreHandler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001618}
1619
Barry Warsaw92971171997-01-03 00:14:25 +00001620
Barry Warsaw92971171997-01-03 00:14:25 +00001621/* Declared in pyerrors.h */
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001622int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001623PyErr_CheckSignals(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001624{
Eric Snow64d6cc82019-02-23 15:40:43 -07001625 if (!is_main()) {
1626 return 0;
1627 }
1628
1629 return _PyErr_CheckSignals();
1630}
1631
1632
1633/* Declared in cpython/pyerrors.h */
1634int
1635_PyErr_CheckSignals(void)
1636{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001637 int i;
1638 PyObject *f;
Barry Warsaw92971171997-01-03 00:14:25 +00001639
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001640 if (!_Py_atomic_load(&is_tripped))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001641 return 0;
Christian Heimesb76922a2007-12-11 01:06:40 +00001642
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001643 /*
1644 * The is_tripped variable is meant to speed up the calls to
1645 * PyErr_CheckSignals (both directly or via pending calls) when no
1646 * signal has arrived. This variable is set to 1 when a signal arrives
1647 * and it is set to 0 here, when we know some signals arrived. This way
1648 * we can run the registered handlers with no signals blocked.
1649 *
1650 * NOTE: with this approach we can have a situation where is_tripped is
1651 * 1 but we have no more signals to handle (Handlers[i].tripped
1652 * is 0 for every signal i). This won't do us any harm (except
1653 * we're gonna spent some cycles for nothing). This happens when
1654 * we receive a signal i after we zero is_tripped and before we
1655 * check Handlers[i].tripped.
1656 */
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001657 _Py_atomic_store(&is_tripped, 0);
Christian Heimesb76922a2007-12-11 01:06:40 +00001658
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001659 if (!(f = (PyObject *)PyEval_GetFrame()))
1660 f = Py_None;
Christian Heimesb76922a2007-12-11 01:06:40 +00001661
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001662 for (i = 1; i < NSIG; i++) {
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001663 if (_Py_atomic_load_relaxed(&Handlers[i].tripped)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001664 PyObject *result = NULL;
1665 PyObject *arglist = Py_BuildValue("(iO)", i, f);
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001666 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Barry Warsaw92971171997-01-03 00:14:25 +00001667
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001668 if (arglist) {
1669 result = PyEval_CallObject(Handlers[i].func,
1670 arglist);
1671 Py_DECREF(arglist);
1672 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +02001673 if (!result) {
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001674 _Py_atomic_store(&is_tripped, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001675 return -1;
Antoine Pitrouc08177a2017-06-28 23:29:29 +02001676 }
Barry Warsaw92971171997-01-03 00:14:25 +00001677
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001678 Py_DECREF(result);
1679 }
1680 }
Christian Heimesb76922a2007-12-11 01:06:40 +00001681
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001682 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001683}
1684
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +00001685
Barry Warsaw92971171997-01-03 00:14:25 +00001686/* Replacements for intrcheck.c functionality
1687 * Declared in pyerrors.h
1688 */
1689void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001690PyErr_SetInterrupt(void)
Barry Warsaw92971171997-01-03 00:14:25 +00001691{
Victor Stinner6c9b35b2011-04-18 16:25:56 +02001692 trip_signal(SIGINT);
Barry Warsaw92971171997-01-03 00:14:25 +00001693}
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001694
1695void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001696PyOS_InitInterrupts(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001697{
Giampaolo Rodola'e09fb712014-04-04 15:34:17 +02001698 PyObject *m = PyImport_ImportModule("_signal");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001699 if (m) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001700 Py_DECREF(m);
1701 }
Guido van Rossum08c16611997-08-02 03:01:42 +00001702}
1703
1704void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001705PyOS_FiniInterrupts(void)
Guido van Rossum08c16611997-08-02 03:01:42 +00001706{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001707 finisignal();
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001708}
1709
1710int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001711PyOS_InterruptOccurred(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001712{
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001713 if (_Py_atomic_load_relaxed(&Handlers[SIGINT].tripped)) {
Eric Snow64d6cc82019-02-23 15:40:43 -07001714 if (!is_main()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001715 return 0;
Eric Snow64d6cc82019-02-23 15:40:43 -07001716 }
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001717 _Py_atomic_store_relaxed(&Handlers[SIGINT].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001718 return 1;
1719 }
1720 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001721}
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001722
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001723static void
1724_clear_pending_signals(void)
1725{
1726 int i;
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001727 if (!_Py_atomic_load(&is_tripped))
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001728 return;
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001729 _Py_atomic_store(&is_tripped, 0);
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001730 for (i = 1; i < NSIG; ++i) {
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001731 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001732 }
1733}
1734
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001735void
Antoine Pitrou346cbd32017-05-27 17:50:54 +02001736_PySignal_AfterFork(void)
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001737{
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001738 /* Clear the signal flags after forking so that they aren't handled
1739 * in both processes if they came in just before the fork() but before
1740 * the interpreter had an opportunity to call the handlers. issue9535. */
1741 _clear_pending_signals();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001742 main_thread = PyThread_get_thread_ident();
Eric Snow64d6cc82019-02-23 15:40:43 -07001743 main_interp = _PyInterpreterState_Get();
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001744}
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001745
1746int
1747_PyOS_IsMainThread(void)
1748{
Eric Snow64d6cc82019-02-23 15:40:43 -07001749 return is_main();
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001750}
1751
1752#ifdef MS_WINDOWS
1753void *_PyOS_SigintEvent(void)
1754{
1755 /* Returns a manual-reset event which gets tripped whenever
1756 SIGINT is received.
1757
1758 Python.h does not include windows.h so we do cannot use HANDLE
1759 as the return type of this function. We use void* instead. */
1760 return sigint_event;
1761}
1762#endif