blob: 7698984ff3afe16b5c58eefee67c51bd691c7d15 [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 Rossum49b56061998-10-01 20:42:43 +0000102#include "pythread.h"
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000103
Victor Stinner2ec6b172011-05-15 10:21:59 +0200104static volatile struct {
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +0200105 _Py_atomic_int tripped;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000106 PyObject *func;
Barry Warsaw92971171997-01-03 00:14:25 +0000107} Handlers[NSIG];
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000108
Victor Stinner11517102014-07-29 23:31:34 +0200109#ifdef MS_WINDOWS
110#define INVALID_FD ((SOCKET_T)-1)
111
112static volatile struct {
113 SOCKET_T fd;
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800114 int warn_on_full_buffer;
Victor Stinner11517102014-07-29 23:31:34 +0200115 int use_send;
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800116} wakeup = {.fd = INVALID_FD, .warn_on_full_buffer = 1, .use_send = 0};
Victor Stinner11517102014-07-29 23:31:34 +0200117#else
118#define INVALID_FD (-1)
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800119static volatile struct {
120 sig_atomic_t fd;
121 int warn_on_full_buffer;
122} wakeup = {.fd = INVALID_FD, .warn_on_full_buffer = 1};
Victor Stinner11517102014-07-29 23:31:34 +0200123#endif
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000124
Christian Heimesb76922a2007-12-11 01:06:40 +0000125/* Speed up sigcheck() when none tripped */
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +0200126static _Py_atomic_int is_tripped;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000127
Barry Warsaw92971171997-01-03 00:14:25 +0000128static PyObject *DefaultHandler;
129static PyObject *IgnoreHandler;
130static PyObject *IntHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000131
Antoine Pitrou6dd381e2011-11-21 21:26:56 +0100132#ifdef MS_WINDOWS
133static HANDLE sigint_event = NULL;
134#endif
135
Martin v. Löwis823725e2008-03-24 13:39:54 +0000136#ifdef HAVE_GETITIMER
137static PyObject *ItimerError;
138
Victor Stinneref611c92017-10-13 13:49:43 -0700139/* auxiliary functions for setitimer */
140static int
141timeval_from_double(PyObject *obj, struct timeval *tv)
Martin v. Löwis823725e2008-03-24 13:39:54 +0000142{
Victor Stinneref611c92017-10-13 13:49:43 -0700143 if (obj == NULL) {
144 tv->tv_sec = 0;
145 tv->tv_usec = 0;
146 return 0;
Antoine Pitrou729780a2017-06-30 10:01:05 +0200147 }
Victor Stinneref611c92017-10-13 13:49:43 -0700148
149 _PyTime_t t;
150 if (_PyTime_FromSecondsObject(&t, obj, _PyTime_ROUND_CEILING) < 0) {
151 return -1;
152 }
153 return _PyTime_AsTimeval(t, tv, _PyTime_ROUND_CEILING);
Martin v. Löwis823725e2008-03-24 13:39:54 +0000154}
155
Christian Heimes1a8501c2008-10-02 19:56:01 +0000156Py_LOCAL_INLINE(double)
Martin v. Löwis823725e2008-03-24 13:39:54 +0000157double_from_timeval(struct timeval *tv)
158{
159 return tv->tv_sec + (double)(tv->tv_usec / 1000000.0);
160}
161
162static PyObject *
163itimer_retval(struct itimerval *iv)
164{
165 PyObject *r, *v;
166
167 r = PyTuple_New(2);
168 if (r == NULL)
Martin Panter6d57fe12016-09-17 03:26:16 +0000169 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000170
171 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_value)))) {
Martin Panter6d57fe12016-09-17 03:26:16 +0000172 Py_DECREF(r);
173 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000174 }
175
176 PyTuple_SET_ITEM(r, 0, v);
177
178 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_interval)))) {
Martin Panter6d57fe12016-09-17 03:26:16 +0000179 Py_DECREF(r);
180 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000181 }
182
183 PyTuple_SET_ITEM(r, 1, v);
184
185 return r;
186}
187#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000188
Eric Snow64d6cc82019-02-23 15:40:43 -0700189static int
Victor Stinnerd8613dc2019-05-24 13:43:55 +0200190is_main(_PyRuntimeState *runtime)
Eric Snow64d6cc82019-02-23 15:40:43 -0700191{
Victor Stinnerd8613dc2019-05-24 13:43:55 +0200192 unsigned long thread = PyThread_get_thread_ident();
193 PyInterpreterState *interp = _PyRuntimeState_GetThreadState(runtime)->interp;
194 return (thread == runtime->main_thread
195 && interp == runtime->interpreters.main);
Eric Snow64d6cc82019-02-23 15:40:43 -0700196}
197
Guido van Rossume4485b01994-09-07 14:32:49 +0000198static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000199signal_default_int_handler(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000200{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000201 PyErr_SetNone(PyExc_KeyboardInterrupt);
202 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000203}
204
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000205PyDoc_STRVAR(default_int_handler_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000206"default_int_handler(...)\n\
207\n\
Michael W. Hudson24ec2112004-06-17 15:55:53 +0000208The default handler for SIGINT installed by Python.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000209It raises KeyboardInterrupt.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000210
Thomas Wouters0796b002000-07-22 23:49:30 +0000211
212static int
Victor Stinner11517102014-07-29 23:31:34 +0200213report_wakeup_write_error(void *data)
Antoine Pitrou6f6ec372013-08-17 20:27:56 +0200214{
Eric Snowfdf282d2019-01-11 14:26:55 -0700215 PyObject *exc, *val, *tb;
Antoine Pitrou6f6ec372013-08-17 20:27:56 +0200216 int save_errno = errno;
Benjamin Petersonca470632016-09-06 13:47:26 -0700217 errno = (int) (intptr_t) data;
Eric Snowfdf282d2019-01-11 14:26:55 -0700218 PyErr_Fetch(&exc, &val, &tb);
Antoine Pitrou6f6ec372013-08-17 20:27:56 +0200219 PyErr_SetFromErrno(PyExc_OSError);
220 PySys_WriteStderr("Exception ignored when trying to write to the "
221 "signal wakeup fd:\n");
222 PyErr_WriteUnraisable(NULL);
Eric Snowfdf282d2019-01-11 14:26:55 -0700223 PyErr_Restore(exc, val, tb);
Antoine Pitrou6f6ec372013-08-17 20:27:56 +0200224 errno = save_errno;
225 return 0;
226}
227
Victor Stinner11517102014-07-29 23:31:34 +0200228#ifdef MS_WINDOWS
229static int
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800230report_wakeup_send_error(void* data)
Victor Stinner11517102014-07-29 23:31:34 +0200231{
Eric Snowfdf282d2019-01-11 14:26:55 -0700232 PyObject *exc, *val, *tb;
233 PyErr_Fetch(&exc, &val, &tb);
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800234 /* PyErr_SetExcFromWindowsErr() invokes FormatMessage() which
235 recognizes the error codes used by both GetLastError() and
236 WSAGetLastError */
237 PyErr_SetExcFromWindowsErr(PyExc_OSError, (int) (intptr_t) data);
Victor Stinner11517102014-07-29 23:31:34 +0200238 PySys_WriteStderr("Exception ignored when trying to send to the "
239 "signal wakeup fd:\n");
240 PyErr_WriteUnraisable(NULL);
Eric Snowfdf282d2019-01-11 14:26:55 -0700241 PyErr_Restore(exc, val, tb);
Victor Stinner11517102014-07-29 23:31:34 +0200242 return 0;
243}
244#endif /* MS_WINDOWS */
245
Tim Peters4f1b2082000-07-23 21:18:09 +0000246static void
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200247trip_signal(int sig_num)
248{
Victor Stinnerd49b1f12011-05-08 02:03:15 +0200249 unsigned char byte;
Victor Stinner11517102014-07-29 23:31:34 +0200250 int fd;
251 Py_ssize_t rc;
Victor Stinnerc13ef662011-05-25 02:35:58 +0200252
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +0200253 _Py_atomic_store_relaxed(&Handlers[sig_num].tripped, 1);
Victor Stinner11517102014-07-29 23:31:34 +0200254
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200255 /* Set is_tripped after setting .tripped, as it gets
256 cleared in PyErr_CheckSignals() before .tripped. */
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +0200257 _Py_atomic_store(&is_tripped, 1);
258
259 /* Notify ceval.c */
Victor Stinner09532fe2019-05-10 23:39:09 +0200260 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinner438a12d2019-05-24 17:01:38 +0200261 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Victor Stinner09532fe2019-05-10 23:39:09 +0200262 _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 Stinner438a12d2019-05-24 17:01:38 +0200302 _PyEval_AddPendingCall(tstate, &runtime->ceval,
Victor Stinner09532fe2019-05-10 23:39:09 +0200303 report_wakeup_send_error,
304 (void *)(intptr_t) last_error);
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800305 }
Victor Stinner11517102014-07-29 23:31:34 +0200306 }
307 }
308 else
309#endif
310 {
Victor Stinnere72fe392015-04-01 18:35:22 +0200311 /* _Py_write_noraise() retries write() if write() is interrupted by
312 a signal (fails with EINTR). */
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 Stinner438a12d2019-05-24 17:01:38 +0200321 _PyEval_AddPendingCall(tstate, &runtime->ceval,
Victor Stinner09532fe2019-05-10 23:39:09 +0200322 report_wakeup_write_error,
323 (void *)(intptr_t)errno);
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800324 }
Victor Stinner11517102014-07-29 23:31:34 +0200325 }
326 }
Victor Stinnerc13ef662011-05-25 02:35:58 +0200327 }
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200328}
329
330static void
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000331signal_handler(int sig_num)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000332{
Antoine Pitrou39a65912010-11-05 19:47:27 +0000333 int save_errno = errno;
334
Jeroen Demeyerd237b3f2019-05-10 03:28:57 +0200335 trip_signal(sig_num);
Antoine Pitrou39a65912010-11-05 19:47:27 +0000336
Jean-Paul Calderone6f137ca2010-05-09 03:18:57 +0000337#ifndef HAVE_SIGACTION
Antoine Pitrou39a65912010-11-05 19:47:27 +0000338#ifdef SIGCHLD
339 /* To avoid infinite recursion, this signal remains
340 reset until explicit re-instated.
341 Don't clear the 'func' field as it is our pointer
342 to the Python handler... */
343 if (sig_num != SIGCHLD)
344#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000345 /* If the handler was not set up with sigaction, reinstall it. See
Nick Coghland6009512014-11-20 21:39:37 +1000346 * Python/pylifecycle.c for the implementation of PyOS_setsig which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000347 * makes this true. See also issue8354. */
348 PyOS_setsig(sig_num, signal_handler);
Jean-Paul Calderone6f137ca2010-05-09 03:18:57 +0000349#endif
Antoine Pitrou39a65912010-11-05 19:47:27 +0000350
351 /* Issue #10311: asynchronously executing signal handlers should not
352 mutate errno under the feet of unsuspecting C code. */
353 errno = save_errno;
Antoine Pitrou6dd381e2011-11-21 21:26:56 +0100354
355#ifdef MS_WINDOWS
356 if (sig_num == SIGINT)
357 SetEvent(sigint_event);
358#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000359}
Guido van Rossume4485b01994-09-07 14:32:49 +0000360
Guido van Rossum06d511d1995-03-10 15:13:48 +0000361
Guido van Rossum1171ee61997-08-22 20:42:00 +0000362#ifdef HAVE_ALARM
Tal Einatc7027b72015-05-16 14:14:49 +0300363
364/*[clinic input]
365signal.alarm -> long
366
367 seconds: int
368 /
369
370Arrange for SIGALRM to arrive after the given number of seconds.
371[clinic start generated code]*/
372
373static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300374signal_alarm_impl(PyObject *module, int seconds)
375/*[clinic end generated code: output=144232290814c298 input=0d5e97e0e6f39e86]*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000376{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000377 /* alarm() returns the number of seconds remaining */
Tal Einatc7027b72015-05-16 14:14:49 +0300378 return (long)alarm(seconds);
Guido van Rossumaa0f4c71994-08-23 13:49:37 +0000379}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000380
Guido van Rossum06d511d1995-03-10 15:13:48 +0000381#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000382
Guido van Rossum1171ee61997-08-22 20:42:00 +0000383#ifdef HAVE_PAUSE
Tal Einatc7027b72015-05-16 14:14:49 +0300384
385/*[clinic input]
386signal.pause
387
388Wait until a signal arrives.
389[clinic start generated code]*/
390
Guido van Rossuma597dde1995-01-10 20:56:29 +0000391static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300392signal_pause_impl(PyObject *module)
393/*[clinic end generated code: output=391656788b3c3929 input=f03de0f875752062]*/
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000394{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000395 Py_BEGIN_ALLOW_THREADS
396 (void)pause();
397 Py_END_ALLOW_THREADS
398 /* make sure that any exceptions that got raised are propagated
399 * back into Python
400 */
401 if (PyErr_CheckSignals())
402 return NULL;
Barry Warsaw92971171997-01-03 00:14:25 +0000403
Tal Einatc7027b72015-05-16 14:14:49 +0300404 Py_RETURN_NONE;
Guido van Rossume4485b01994-09-07 14:32:49 +0000405}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000406
Guido van Rossum06d511d1995-03-10 15:13:48 +0000407#endif
Guido van Rossume4485b01994-09-07 14:32:49 +0000408
Vladimir Matveevc24c6c22019-01-08 01:58:25 -0800409/*[clinic input]
410signal.raise_signal
411
412 signalnum: int
413 /
414
415Send a signal to the executing process.
416[clinic start generated code]*/
417
418static PyObject *
419signal_raise_signal_impl(PyObject *module, int signalnum)
420/*[clinic end generated code: output=e2b014220aa6111d input=e90c0f9a42358de6]*/
421{
422 int err;
423 Py_BEGIN_ALLOW_THREADS
424 _Py_BEGIN_SUPPRESS_IPH
425 err = raise(signalnum);
426 _Py_END_SUPPRESS_IPH
427 Py_END_ALLOW_THREADS
Victor Stinner09532fe2019-05-10 23:39:09 +0200428
Vladimir Matveevc24c6c22019-01-08 01:58:25 -0800429 if (err) {
430 return PyErr_SetFromErrno(PyExc_OSError);
431 }
432 Py_RETURN_NONE;
433}
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000434
Tal Einatc7027b72015-05-16 14:14:49 +0300435/*[clinic input]
436signal.signal
437
438 signalnum: int
439 handler: object
440 /
441
442Set the action for the given signal.
443
444The action can be SIG_DFL, SIG_IGN, or a callable Python object.
445The previous action is returned. See getsignal() for possible return values.
446
447*** IMPORTANT NOTICE ***
448A signal handler function is called with two arguments:
449the first is the signal number, the second is the interrupted stack frame.
450[clinic start generated code]*/
451
Guido van Rossume4485b01994-09-07 14:32:49 +0000452static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300453signal_signal_impl(PyObject *module, int signalnum, PyObject *handler)
454/*[clinic end generated code: output=b44cfda43780f3a1 input=deee84af5fa0432c]*/
Guido van Rossume4485b01994-09-07 14:32:49 +0000455{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000456 PyObject *old_handler;
457 void (*func)(int);
Brian Curtinef9efbd2010-08-06 19:27:32 +0000458#ifdef MS_WINDOWS
Tal Einatc7027b72015-05-16 14:14:49 +0300459 /* Validate that signalnum is one of the allowable signals */
460 switch (signalnum) {
Brian Curtinc734b312010-09-06 16:04:10 +0000461 case SIGABRT: break;
Brian Curtin9e88b5a2010-10-01 14:49:24 +0000462#ifdef SIGBREAK
463 /* Issue #10003: SIGBREAK is not documented as permitted, but works
464 and corresponds to CTRL_BREAK_EVENT. */
465 case SIGBREAK: break;
466#endif
Brian Curtinc734b312010-09-06 16:04:10 +0000467 case SIGFPE: break;
468 case SIGILL: break;
469 case SIGINT: break;
470 case SIGSEGV: break;
471 case SIGTERM: break;
472 default:
473 PyErr_SetString(PyExc_ValueError, "invalid signal value");
474 return NULL;
Brian Curtinef9efbd2010-08-06 19:27:32 +0000475 }
476#endif
Victor Stinnerd8613dc2019-05-24 13:43:55 +0200477
478 _PyRuntimeState *runtime = &_PyRuntime;
479 if (!is_main(runtime)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000480 PyErr_SetString(PyExc_ValueError,
481 "signal only works in main thread");
482 return NULL;
483 }
Tal Einatc7027b72015-05-16 14:14:49 +0300484 if (signalnum < 1 || signalnum >= NSIG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000485 PyErr_SetString(PyExc_ValueError,
486 "signal number out of range");
487 return NULL;
488 }
Tal Einatc7027b72015-05-16 14:14:49 +0300489 if (handler == IgnoreHandler)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000490 func = SIG_IGN;
Tal Einatc7027b72015-05-16 14:14:49 +0300491 else if (handler == DefaultHandler)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000492 func = SIG_DFL;
Tal Einatc7027b72015-05-16 14:14:49 +0300493 else if (!PyCallable_Check(handler)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000494 PyErr_SetString(PyExc_TypeError,
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000495"signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000496 return NULL;
497 }
498 else
499 func = signal_handler;
Antoine Pitrouf6f90ff2017-11-03 19:58:46 +0100500 /* Check for pending signals before changing signal handler */
Eric Snow64d6cc82019-02-23 15:40:43 -0700501 if (_PyErr_CheckSignals()) {
Antoine Pitrouf6f90ff2017-11-03 19:58:46 +0100502 return NULL;
503 }
Tal Einatc7027b72015-05-16 14:14:49 +0300504 if (PyOS_setsig(signalnum, func) == SIG_ERR) {
Victor Stinner388196e2011-05-10 17:13:00 +0200505 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000506 return NULL;
507 }
Tal Einatc7027b72015-05-16 14:14:49 +0300508 old_handler = Handlers[signalnum].func;
Tal Einatc7027b72015-05-16 14:14:49 +0300509 Py_INCREF(handler);
510 Handlers[signalnum].func = handler;
Antoine Pitrouc8c952c2013-05-04 23:16:59 +0200511 if (old_handler != NULL)
512 return old_handler;
513 else
514 Py_RETURN_NONE;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000515}
516
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000517
Tal Einatc7027b72015-05-16 14:14:49 +0300518/*[clinic input]
519signal.getsignal
520
521 signalnum: int
522 /
523
524Return the current action for the given signal.
525
526The return value can be:
527 SIG_IGN -- if the signal is being ignored
528 SIG_DFL -- if the default action for the signal is in effect
529 None -- if an unknown handler is in effect
530 anything else -- the callable Python object used as a handler
531[clinic start generated code]*/
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000532
Guido van Rossume4485b01994-09-07 14:32:49 +0000533static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300534signal_getsignal_impl(PyObject *module, int signalnum)
535/*[clinic end generated code: output=35b3e0e796fd555e input=ac23a00f19dfa509]*/
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000536{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000537 PyObject *old_handler;
Tal Einatc7027b72015-05-16 14:14:49 +0300538 if (signalnum < 1 || signalnum >= NSIG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000539 PyErr_SetString(PyExc_ValueError,
540 "signal number out of range");
541 return NULL;
542 }
Tal Einatc7027b72015-05-16 14:14:49 +0300543 old_handler = Handlers[signalnum].func;
Antoine Pitrouc8c952c2013-05-04 23:16:59 +0200544 if (old_handler != NULL) {
545 Py_INCREF(old_handler);
546 return old_handler;
547 }
548 else {
549 Py_RETURN_NONE;
550 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000551}
552
Antoine Pietri5d2a27d2018-03-12 14:42:34 +0100553
554/*[clinic input]
555signal.strsignal
556
557 signalnum: int
558 /
559
560Return the system description of the given signal.
561
562The return values can be such as "Interrupt", "Segmentation fault", etc.
563Returns None if the signal is not recognized.
564[clinic start generated code]*/
565
566static PyObject *
567signal_strsignal_impl(PyObject *module, int signalnum)
568/*[clinic end generated code: output=44e12e1e3b666261 input=b77914b03f856c74]*/
569{
570 char *res;
571
572 if (signalnum < 1 || signalnum >= NSIG) {
573 PyErr_SetString(PyExc_ValueError,
574 "signal number out of range");
575 return NULL;
576 }
577
Michael Osipov48ce4892018-08-23 15:27:19 +0200578#ifndef HAVE_STRSIGNAL
Antoine Pietri5d2a27d2018-03-12 14:42:34 +0100579 switch (signalnum) {
Michael Osipov48ce4892018-08-23 15:27:19 +0200580 /* Though being a UNIX, HP-UX does not provide strsignal(3). */
581#ifndef MS_WINDOWS
582 case SIGHUP:
583 res = "Hangup";
584 break;
585 case SIGALRM:
586 res = "Alarm clock";
587 break;
588 case SIGPIPE:
589 res = "Broken pipe";
590 break;
591 case SIGQUIT:
592 res = "Quit";
593 break;
594 case SIGCHLD:
595 res = "Child exited";
596 break;
597#endif
598 /* Custom redefinition of POSIX signals allowed on Windows. */
Antoine Pietri5d2a27d2018-03-12 14:42:34 +0100599 case SIGINT:
600 res = "Interrupt";
601 break;
602 case SIGILL:
603 res = "Illegal instruction";
604 break;
605 case SIGABRT:
606 res = "Aborted";
607 break;
608 case SIGFPE:
609 res = "Floating point exception";
610 break;
611 case SIGSEGV:
612 res = "Segmentation fault";
613 break;
614 case SIGTERM:
615 res = "Terminated";
616 break;
617 default:
618 Py_RETURN_NONE;
619 }
620#else
621 errno = 0;
622 res = strsignal(signalnum);
623
624 if (errno || res == NULL || strstr(res, "Unknown signal") != NULL)
625 Py_RETURN_NONE;
626#endif
627
628 return Py_BuildValue("s", res);
629}
630
Christian Heimes8640e742008-02-23 16:23:06 +0000631#ifdef HAVE_SIGINTERRUPT
Tal Einatc7027b72015-05-16 14:14:49 +0300632
633/*[clinic input]
634signal.siginterrupt
635
636 signalnum: int
637 flag: int
638 /
639
640Change system call restart behaviour.
641
642If flag is False, system calls will be restarted when interrupted by
643signal sig, else system calls will be interrupted.
644[clinic start generated code]*/
Christian Heimes8640e742008-02-23 16:23:06 +0000645
646static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300647signal_siginterrupt_impl(PyObject *module, int signalnum, int flag)
648/*[clinic end generated code: output=063816243d85dd19 input=4160acacca3e2099]*/
Christian Heimes8640e742008-02-23 16:23:06 +0000649{
Tal Einatc7027b72015-05-16 14:14:49 +0300650 if (signalnum < 1 || signalnum >= NSIG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000651 PyErr_SetString(PyExc_ValueError,
652 "signal number out of range");
653 return NULL;
654 }
Tal Einatc7027b72015-05-16 14:14:49 +0300655 if (siginterrupt(signalnum, flag)<0) {
Victor Stinner388196e2011-05-10 17:13:00 +0200656 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000657 return NULL;
658 }
Tal Einatc7027b72015-05-16 14:14:49 +0300659 Py_RETURN_NONE;
Christian Heimes8640e742008-02-23 16:23:06 +0000660}
661
662#endif
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000663
Tal Einatc7027b72015-05-16 14:14:49 +0300664
665static PyObject*
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800666signal_set_wakeup_fd(PyObject *self, PyObject *args, PyObject *kwds)
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000667{
Victor Stinnere134a7f2015-03-30 10:09:31 +0200668 struct _Py_stat_struct status;
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800669 static char *kwlist[] = {
670 "", "warn_on_full_buffer", NULL,
671 };
672 int warn_on_full_buffer = 1;
Victor Stinner11517102014-07-29 23:31:34 +0200673#ifdef MS_WINDOWS
674 PyObject *fdobj;
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100675 SOCKET_T sockfd, old_sockfd;
Victor Stinner11517102014-07-29 23:31:34 +0200676 int res;
677 int res_size = sizeof res;
678 PyObject *mod;
Victor Stinner11517102014-07-29 23:31:34 +0200679 int is_socket;
680
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800681 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|$p:set_wakeup_fd", kwlist,
682 &fdobj, &warn_on_full_buffer))
Victor Stinner11517102014-07-29 23:31:34 +0200683 return NULL;
684
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100685 sockfd = PyLong_AsSocket_t(fdobj);
686 if (sockfd == (SOCKET_T)(-1) && PyErr_Occurred())
Victor Stinner11517102014-07-29 23:31:34 +0200687 return NULL;
688#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000689 int fd, old_fd;
Victor Stinner11517102014-07-29 23:31:34 +0200690
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800691 if (!PyArg_ParseTupleAndKeywords(args, kwds, "i|$p:set_wakeup_fd", kwlist,
692 &fd, &warn_on_full_buffer))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000693 return NULL;
Victor Stinner11517102014-07-29 23:31:34 +0200694#endif
695
Victor Stinnerd8613dc2019-05-24 13:43:55 +0200696 _PyRuntimeState *runtime = &_PyRuntime;
697 if (!is_main(runtime)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000698 PyErr_SetString(PyExc_ValueError,
699 "set_wakeup_fd only works in main thread");
700 return NULL;
701 }
Victor Stinner0bffc942014-07-21 16:28:54 +0200702
Victor Stinner11517102014-07-29 23:31:34 +0200703#ifdef MS_WINDOWS
704 is_socket = 0;
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100705 if (sockfd != INVALID_FD) {
Victor Stinner11517102014-07-29 23:31:34 +0200706 /* Import the _socket module to call WSAStartup() */
707 mod = PyImport_ImportModuleNoBlock("_socket");
708 if (mod == NULL)
709 return NULL;
710 Py_DECREF(mod);
711
712 /* test the socket */
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100713 if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR,
Victor Stinner11517102014-07-29 23:31:34 +0200714 (char *)&res, &res_size) != 0) {
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100715 int fd, err;
716
717 err = WSAGetLastError();
Victor Stinner11517102014-07-29 23:31:34 +0200718 if (err != WSAENOTSOCK) {
719 PyErr_SetExcFromWindowsErr(PyExc_OSError, err);
720 return NULL;
721 }
722
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100723 fd = (int)sockfd;
Steve Dower940f33a2016-09-08 11:21:54 -0700724 if ((SOCKET_T)fd != sockfd) {
Victor Stinner11517102014-07-29 23:31:34 +0200725 PyErr_SetString(PyExc_ValueError, "invalid fd");
726 return NULL;
727 }
728
Victor Stinnere134a7f2015-03-30 10:09:31 +0200729 if (_Py_fstat(fd, &status) != 0)
Victor Stinner11517102014-07-29 23:31:34 +0200730 return NULL;
Victor Stinner38227602014-08-27 12:59:44 +0200731
732 /* on Windows, a file cannot be set to non-blocking mode */
Victor Stinner11517102014-07-29 23:31:34 +0200733 }
Victor Stinner38227602014-08-27 12:59:44 +0200734 else {
Victor Stinner11517102014-07-29 23:31:34 +0200735 is_socket = 1;
Victor Stinner38227602014-08-27 12:59:44 +0200736
737 /* Windows does not provide a function to test if a socket
738 is in non-blocking mode */
739 }
Victor Stinner11517102014-07-29 23:31:34 +0200740 }
741
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100742 old_sockfd = wakeup.fd;
743 wakeup.fd = sockfd;
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800744 wakeup.warn_on_full_buffer = warn_on_full_buffer;
Victor Stinner11517102014-07-29 23:31:34 +0200745 wakeup.use_send = is_socket;
746
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100747 if (old_sockfd != INVALID_FD)
748 return PyLong_FromSocket_t(old_sockfd);
Victor Stinner11517102014-07-29 23:31:34 +0200749 else
750 return PyLong_FromLong(-1);
751#else
Victor Stinnerd18ccd12014-07-24 21:58:53 +0200752 if (fd != -1) {
Victor Stinner38227602014-08-27 12:59:44 +0200753 int blocking;
754
Victor Stinnere134a7f2015-03-30 10:09:31 +0200755 if (_Py_fstat(fd, &status) != 0)
Victor Stinner11517102014-07-29 23:31:34 +0200756 return NULL;
Victor Stinner38227602014-08-27 12:59:44 +0200757
758 blocking = _Py_get_blocking(fd);
759 if (blocking < 0)
760 return NULL;
761 if (blocking) {
762 PyErr_Format(PyExc_ValueError,
763 "the fd %i must be in non-blocking mode",
764 fd);
765 return NULL;
766 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000767 }
Victor Stinner0bffc942014-07-21 16:28:54 +0200768
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800769 old_fd = wakeup.fd;
770 wakeup.fd = fd;
771 wakeup.warn_on_full_buffer = warn_on_full_buffer;
Victor Stinner0bffc942014-07-21 16:28:54 +0200772
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000773 return PyLong_FromLong(old_fd);
Victor Stinner11517102014-07-29 23:31:34 +0200774#endif
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000775}
776
777PyDoc_STRVAR(set_wakeup_fd_doc,
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800778"set_wakeup_fd(fd, *, warn_on_full_buffer=True) -> fd\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000779\n\
Victor Stinner11517102014-07-29 23:31:34 +0200780Sets the fd to be written to (with the signal number) when a signal\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000781comes in. A library can use this to wakeup select or poll.\n\
Victor Stinner11517102014-07-29 23:31:34 +0200782The previous fd or -1 is returned.\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000783\n\
784The fd must be non-blocking.");
785
786/* C API for the same, without all the error checking */
787int
788PySignal_SetWakeupFd(int fd)
789{
Victor Stinner11517102014-07-29 23:31:34 +0200790 int old_fd;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000791 if (fd < 0)
792 fd = -1;
Victor Stinner11517102014-07-29 23:31:34 +0200793
794#ifdef MS_WINDOWS
795 old_fd = Py_SAFE_DOWNCAST(wakeup.fd, SOCKET_T, int);
Victor Stinner11517102014-07-29 23:31:34 +0200796#else
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800797 old_fd = wakeup.fd;
Victor Stinner11517102014-07-29 23:31:34 +0200798#endif
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800799 wakeup.fd = fd;
800 wakeup.warn_on_full_buffer = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000801 return old_fd;
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000802}
803
804
Martin v. Löwis823725e2008-03-24 13:39:54 +0000805#ifdef HAVE_SETITIMER
Tal Einatc7027b72015-05-16 14:14:49 +0300806
807/*[clinic input]
808signal.setitimer
809
810 which: int
Victor Stinneref611c92017-10-13 13:49:43 -0700811 seconds: object
812 interval: object(c_default="NULL") = 0.0
Tal Einatc7027b72015-05-16 14:14:49 +0300813 /
814
815Sets given itimer (one of ITIMER_REAL, ITIMER_VIRTUAL or ITIMER_PROF).
816
817The timer will fire after value seconds and after that every interval seconds.
818The itimer can be cleared by setting seconds to zero.
819
820Returns old values as a tuple: (delay, interval).
821[clinic start generated code]*/
822
Martin v. Löwis823725e2008-03-24 13:39:54 +0000823static PyObject *
Victor Stinneref611c92017-10-13 13:49:43 -0700824signal_setitimer_impl(PyObject *module, int which, PyObject *seconds,
825 PyObject *interval)
826/*[clinic end generated code: output=65f9dcbddc35527b input=de43daf194e6f66f]*/
Martin v. Löwis823725e2008-03-24 13:39:54 +0000827{
Martin v. Löwis823725e2008-03-24 13:39:54 +0000828 struct itimerval new, old;
829
Victor Stinneref611c92017-10-13 13:49:43 -0700830 if (timeval_from_double(seconds, &new.it_value) < 0) {
831 return NULL;
832 }
833 if (timeval_from_double(interval, &new.it_interval) < 0) {
834 return NULL;
835 }
836
Martin v. Löwis823725e2008-03-24 13:39:54 +0000837 /* Let OS check "which" value */
838 if (setitimer(which, &new, &old) != 0) {
Tal Einatc7027b72015-05-16 14:14:49 +0300839 PyErr_SetFromErrno(ItimerError);
840 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000841 }
842
843 return itimer_retval(&old);
844}
845
Martin v. Löwis823725e2008-03-24 13:39:54 +0000846#endif
847
848
849#ifdef HAVE_GETITIMER
Tal Einatc7027b72015-05-16 14:14:49 +0300850
851/*[clinic input]
852signal.getitimer
853
854 which: int
855 /
856
857Returns current value of given itimer.
858[clinic start generated code]*/
859
Martin v. Löwis823725e2008-03-24 13:39:54 +0000860static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300861signal_getitimer_impl(PyObject *module, int which)
862/*[clinic end generated code: output=9e053175d517db40 input=f7d21d38f3490627]*/
Martin v. Löwis823725e2008-03-24 13:39:54 +0000863{
Martin v. Löwis823725e2008-03-24 13:39:54 +0000864 struct itimerval old;
865
Martin v. Löwis823725e2008-03-24 13:39:54 +0000866 if (getitimer(which, &old) != 0) {
Tal Einatc7027b72015-05-16 14:14:49 +0300867 PyErr_SetFromErrno(ItimerError);
868 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000869 }
870
871 return itimer_retval(&old);
872}
873
Martin v. Löwis823725e2008-03-24 13:39:54 +0000874#endif
875
Victor Stinnerb3e72192011-05-08 01:46:11 +0200876#if defined(PYPTHREAD_SIGMASK) || defined(HAVE_SIGPENDING)
Victor Stinner35b300c2011-05-04 13:20:35 +0200877static PyObject*
878sigset_to_set(sigset_t mask)
879{
880 PyObject *signum, *result;
881 int sig;
882
883 result = PySet_New(0);
884 if (result == NULL)
885 return NULL;
886
887 for (sig = 1; sig < NSIG; sig++) {
888 if (sigismember(&mask, sig) != 1)
889 continue;
890
891 /* Handle the case where it is a member by adding the signal to
892 the result list. Ignore the other cases because they mean the
893 signal isn't a member of the mask or the signal was invalid,
894 and an invalid signal must have been our fault in constructing
895 the loop boundaries. */
896 signum = PyLong_FromLong(sig);
897 if (signum == NULL) {
898 Py_DECREF(result);
899 return NULL;
900 }
901 if (PySet_Add(result, signum) == -1) {
902 Py_DECREF(signum);
903 Py_DECREF(result);
904 return NULL;
905 }
906 Py_DECREF(signum);
907 }
908 return result;
909}
Victor Stinnerb3e72192011-05-08 01:46:11 +0200910#endif
Victor Stinner35b300c2011-05-04 13:20:35 +0200911
Victor Stinnerb3e72192011-05-08 01:46:11 +0200912#ifdef PYPTHREAD_SIGMASK
Tal Einatc7027b72015-05-16 14:14:49 +0300913
914/*[clinic input]
915signal.pthread_sigmask
916
917 how: int
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300918 mask: sigset_t
Tal Einatc7027b72015-05-16 14:14:49 +0300919 /
920
921Fetch and/or change the signal mask of the calling thread.
922[clinic start generated code]*/
923
Victor Stinnera9293352011-04-30 15:21:58 +0200924static PyObject *
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300925signal_pthread_sigmask_impl(PyObject *module, int how, sigset_t mask)
926/*[clinic end generated code: output=0562c0fb192981a8 input=85bcebda442fa77f]*/
Victor Stinnera9293352011-04-30 15:21:58 +0200927{
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300928 sigset_t previous;
Victor Stinnera9293352011-04-30 15:21:58 +0200929 int err;
930
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300931 err = pthread_sigmask(how, &mask, &previous);
Victor Stinnera9293352011-04-30 15:21:58 +0200932 if (err != 0) {
933 errno = err;
Victor Stinnerb3e72192011-05-08 01:46:11 +0200934 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnera9293352011-04-30 15:21:58 +0200935 return NULL;
936 }
937
Victor Stinnerd0e516d2011-05-03 14:57:12 +0200938 /* if signals was unblocked, signal handlers have been called */
939 if (PyErr_CheckSignals())
940 return NULL;
941
Victor Stinner35b300c2011-05-04 13:20:35 +0200942 return sigset_to_set(previous);
Victor Stinnera9293352011-04-30 15:21:58 +0200943}
944
Victor Stinnera9293352011-04-30 15:21:58 +0200945#endif /* #ifdef PYPTHREAD_SIGMASK */
946
Martin v. Löwis823725e2008-03-24 13:39:54 +0000947
Victor Stinnerb3e72192011-05-08 01:46:11 +0200948#ifdef HAVE_SIGPENDING
Tal Einatc7027b72015-05-16 14:14:49 +0300949
950/*[clinic input]
951signal.sigpending
952
953Examine pending signals.
954
955Returns a set of signal numbers that are pending for delivery to
956the calling thread.
957[clinic start generated code]*/
958
Victor Stinnerb3e72192011-05-08 01:46:11 +0200959static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300960signal_sigpending_impl(PyObject *module)
961/*[clinic end generated code: output=53375ffe89325022 input=e0036c016f874e29]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +0200962{
963 int err;
964 sigset_t mask;
965 err = sigpending(&mask);
966 if (err)
967 return PyErr_SetFromErrno(PyExc_OSError);
968 return sigset_to_set(mask);
969}
970
Victor Stinnerb3e72192011-05-08 01:46:11 +0200971#endif /* #ifdef HAVE_SIGPENDING */
972
973
974#ifdef HAVE_SIGWAIT
Tal Einatc7027b72015-05-16 14:14:49 +0300975
976/*[clinic input]
977signal.sigwait
978
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300979 sigset: sigset_t
Tal Einatc7027b72015-05-16 14:14:49 +0300980 /
981
982Wait for a signal.
983
984Suspend execution of the calling thread until the delivery of one of the
985signals specified in the signal set sigset. The function accepts the signal
986and returns the signal number.
987[clinic start generated code]*/
988
Victor Stinnerb3e72192011-05-08 01:46:11 +0200989static PyObject *
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300990signal_sigwait_impl(PyObject *module, sigset_t sigset)
991/*[clinic end generated code: output=f43770699d682f96 input=a6fbd47b1086d119]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +0200992{
Victor Stinnerb3e72192011-05-08 01:46:11 +0200993 int err, signum;
994
Victor Stinner10c30d62011-06-10 01:39:53 +0200995 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300996 err = sigwait(&sigset, &signum);
Victor Stinner10c30d62011-06-10 01:39:53 +0200997 Py_END_ALLOW_THREADS
Victor Stinnerb3e72192011-05-08 01:46:11 +0200998 if (err) {
999 errno = err;
1000 return PyErr_SetFromErrno(PyExc_OSError);
1001 }
1002
1003 return PyLong_FromLong(signum);
1004}
1005
Tal Einatc7027b72015-05-16 14:14:49 +03001006#endif /* #ifdef HAVE_SIGWAIT */
1007
Victor Stinnerb3e72192011-05-08 01:46:11 +02001008
Antoine Pitrou9d3627e2018-05-04 13:00:50 +02001009#if defined(HAVE_SIGFILLSET) || defined(MS_WINDOWS)
1010
1011/*[clinic input]
1012signal.valid_signals
1013
1014Return a set of valid signal numbers on this platform.
1015
1016The signal numbers returned by this function can be safely passed to
1017functions like `pthread_sigmask`.
1018[clinic start generated code]*/
1019
1020static PyObject *
1021signal_valid_signals_impl(PyObject *module)
1022/*[clinic end generated code: output=1609cffbcfcf1314 input=86a3717ff25288f2]*/
1023{
1024#ifdef MS_WINDOWS
1025#ifdef SIGBREAK
1026 PyObject *tup = Py_BuildValue("(iiiiiii)", SIGABRT, SIGBREAK, SIGFPE,
1027 SIGILL, SIGINT, SIGSEGV, SIGTERM);
1028#else
1029 PyObject *tup = Py_BuildValue("(iiiiii)", SIGABRT, SIGFPE, SIGILL,
1030 SIGINT, SIGSEGV, SIGTERM);
1031#endif
1032 if (tup == NULL) {
1033 return NULL;
1034 }
1035 PyObject *set = PySet_New(tup);
1036 Py_DECREF(tup);
1037 return set;
1038#else
1039 sigset_t mask;
1040 if (sigemptyset(&mask) || sigfillset(&mask)) {
1041 return PyErr_SetFromErrno(PyExc_OSError);
1042 }
1043 return sigset_to_set(mask);
1044#endif
1045}
1046
1047#endif /* #if defined(HAVE_SIGFILLSET) || defined(MS_WINDOWS) */
1048
1049
Ross Lagerwallbc808222011-06-25 12:13:40 +02001050#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
1051static int initialized;
1052static PyStructSequence_Field struct_siginfo_fields[] = {
1053 {"si_signo", "signal number"},
1054 {"si_code", "signal code"},
1055 {"si_errno", "errno associated with this signal"},
1056 {"si_pid", "sending process ID"},
1057 {"si_uid", "real user ID of sending process"},
1058 {"si_status", "exit value or signal"},
1059 {"si_band", "band event for SIGPOLL"},
1060 {0}
1061};
1062
1063PyDoc_STRVAR(struct_siginfo__doc__,
1064"struct_siginfo: Result from sigwaitinfo or sigtimedwait.\n\n\
1065This object may be accessed either as a tuple of\n\
1066(si_signo, si_code, si_errno, si_pid, si_uid, si_status, si_band),\n\
1067or via the attributes si_signo, si_code, and so on.");
1068
1069static PyStructSequence_Desc struct_siginfo_desc = {
1070 "signal.struct_siginfo", /* name */
1071 struct_siginfo__doc__, /* doc */
1072 struct_siginfo_fields, /* fields */
1073 7 /* n_in_sequence */
1074};
1075
1076static PyTypeObject SiginfoType;
1077
1078static PyObject *
1079fill_siginfo(siginfo_t *si)
1080{
1081 PyObject *result = PyStructSequence_New(&SiginfoType);
1082 if (!result)
1083 return NULL;
1084
1085 PyStructSequence_SET_ITEM(result, 0, PyLong_FromLong((long)(si->si_signo)));
1086 PyStructSequence_SET_ITEM(result, 1, PyLong_FromLong((long)(si->si_code)));
Victor Stinner09532fe2019-05-10 23:39:09 +02001087#ifdef __VXWORKS__
pxinwr8b5bdda2019-03-14 01:18:25 +08001088 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong(0L));
1089 PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong(0L));
1090 PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong(0L));
1091 PyStructSequence_SET_ITEM(result, 5, PyLong_FromLong(0L));
Victor Stinner09532fe2019-05-10 23:39:09 +02001092#else
Ross Lagerwallbc808222011-06-25 12:13:40 +02001093 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si->si_errno)));
1094 PyStructSequence_SET_ITEM(result, 3, PyLong_FromPid(si->si_pid));
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02001095 PyStructSequence_SET_ITEM(result, 4, _PyLong_FromUid(si->si_uid));
Ross Lagerwallbc808222011-06-25 12:13:40 +02001096 PyStructSequence_SET_ITEM(result, 5,
1097 PyLong_FromLong((long)(si->si_status)));
Victor Stinner09532fe2019-05-10 23:39:09 +02001098#endif
Zachary Ware6a6967e2016-10-01 00:47:27 -05001099#ifdef HAVE_SIGINFO_T_SI_BAND
Ross Lagerwallbc808222011-06-25 12:13:40 +02001100 PyStructSequence_SET_ITEM(result, 6, PyLong_FromLong(si->si_band));
Zachary Ware6a6967e2016-10-01 00:47:27 -05001101#else
1102 PyStructSequence_SET_ITEM(result, 6, PyLong_FromLong(0L));
1103#endif
Ross Lagerwallbc808222011-06-25 12:13:40 +02001104 if (PyErr_Occurred()) {
1105 Py_DECREF(result);
1106 return NULL;
1107 }
1108
1109 return result;
1110}
1111#endif
1112
1113#ifdef HAVE_SIGWAITINFO
Tal Einatc7027b72015-05-16 14:14:49 +03001114
1115/*[clinic input]
1116signal.sigwaitinfo
1117
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001118 sigset: sigset_t
Tal Einatc7027b72015-05-16 14:14:49 +03001119 /
1120
1121Wait synchronously until one of the signals in *sigset* is delivered.
1122
1123Returns a struct_siginfo containing information about the signal.
1124[clinic start generated code]*/
1125
Ross Lagerwallbc808222011-06-25 12:13:40 +02001126static PyObject *
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001127signal_sigwaitinfo_impl(PyObject *module, sigset_t sigset)
1128/*[clinic end generated code: output=1eb2f1fa236fdbca input=3d1a7e1f27fc664c]*/
Ross Lagerwallbc808222011-06-25 12:13:40 +02001129{
Ross Lagerwallbc808222011-06-25 12:13:40 +02001130 siginfo_t si;
1131 int err;
Victor Stinnera453cd82015-03-20 12:54:28 +01001132 int async_err = 0;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001133
Victor Stinnera453cd82015-03-20 12:54:28 +01001134 do {
1135 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001136 err = sigwaitinfo(&sigset, &si);
Victor Stinnera453cd82015-03-20 12:54:28 +01001137 Py_END_ALLOW_THREADS
1138 } while (err == -1
1139 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Ross Lagerwallbc808222011-06-25 12:13:40 +02001140 if (err == -1)
Victor Stinnera453cd82015-03-20 12:54:28 +01001141 return (!async_err) ? PyErr_SetFromErrno(PyExc_OSError) : NULL;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001142
1143 return fill_siginfo(&si);
1144}
1145
Ross Lagerwallbc808222011-06-25 12:13:40 +02001146#endif /* #ifdef HAVE_SIGWAITINFO */
1147
1148#ifdef HAVE_SIGTIMEDWAIT
Tal Einatc7027b72015-05-16 14:14:49 +03001149
1150/*[clinic input]
1151signal.sigtimedwait
1152
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001153 sigset: sigset_t
Serhiy Storchaka6b680cd2015-05-16 15:57:56 +03001154 timeout as timeout_obj: object
Tal Einatc7027b72015-05-16 14:14:49 +03001155 /
1156
1157Like sigwaitinfo(), but with a timeout.
1158
1159The timeout is specified in seconds, with floating point numbers allowed.
1160[clinic start generated code]*/
1161
Ross Lagerwallbc808222011-06-25 12:13:40 +02001162static PyObject *
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001163signal_sigtimedwait_impl(PyObject *module, sigset_t sigset,
Serhiy Storchaka6b680cd2015-05-16 15:57:56 +03001164 PyObject *timeout_obj)
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001165/*[clinic end generated code: output=59c8971e8ae18a64 input=87fd39237cf0b7ba]*/
Ross Lagerwallbc808222011-06-25 12:13:40 +02001166{
Victor Stinnera453cd82015-03-20 12:54:28 +01001167 struct timespec ts;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001168 siginfo_t si;
Victor Stinnera453cd82015-03-20 12:54:28 +01001169 int res;
Victor Stinner34dc0f42015-03-27 18:19:03 +01001170 _PyTime_t timeout, deadline, monotonic;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001171
Victor Stinner869e1772015-03-30 03:49:14 +02001172 if (_PyTime_FromSecondsObject(&timeout,
1173 timeout_obj, _PyTime_ROUND_CEILING) < 0)
Ross Lagerwallbc808222011-06-25 12:13:40 +02001174 return NULL;
1175
Victor Stinnera453cd82015-03-20 12:54:28 +01001176 if (timeout < 0) {
Ross Lagerwallbc808222011-06-25 12:13:40 +02001177 PyErr_SetString(PyExc_ValueError, "timeout must be non-negative");
1178 return NULL;
1179 }
1180
Victor Stinner34dc0f42015-03-27 18:19:03 +01001181 deadline = _PyTime_GetMonotonicClock() + timeout;
Victor Stinnera453cd82015-03-20 12:54:28 +01001182
1183 do {
Victor Stinner34dc0f42015-03-27 18:19:03 +01001184 if (_PyTime_AsTimespec(timeout, &ts) < 0)
1185 return NULL;
Victor Stinnera453cd82015-03-20 12:54:28 +01001186
1187 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001188 res = sigtimedwait(&sigset, &si, &ts);
Victor Stinnera453cd82015-03-20 12:54:28 +01001189 Py_END_ALLOW_THREADS
1190
1191 if (res != -1)
1192 break;
1193
1194 if (errno != EINTR) {
1195 if (errno == EAGAIN)
1196 Py_RETURN_NONE;
1197 else
1198 return PyErr_SetFromErrno(PyExc_OSError);
1199 }
1200
1201 /* sigtimedwait() was interrupted by a signal (EINTR) */
1202 if (PyErr_CheckSignals())
1203 return NULL;
1204
Victor Stinner34dc0f42015-03-27 18:19:03 +01001205 monotonic = _PyTime_GetMonotonicClock();
1206 timeout = deadline - monotonic;
Victor Stinner6aa446c2015-03-30 21:33:51 +02001207 if (timeout < 0)
Victor Stinnera453cd82015-03-20 12:54:28 +01001208 break;
1209 } while (1);
Ross Lagerwallbc808222011-06-25 12:13:40 +02001210
1211 return fill_siginfo(&si);
1212}
1213
Ross Lagerwallbc808222011-06-25 12:13:40 +02001214#endif /* #ifdef HAVE_SIGTIMEDWAIT */
1215
Victor Stinnerb3e72192011-05-08 01:46:11 +02001216
Antoine Pitroua6a4dc82017-09-07 18:56:24 +02001217#if defined(HAVE_PTHREAD_KILL)
Tal Einatc7027b72015-05-16 14:14:49 +03001218
1219/*[clinic input]
1220signal.pthread_kill
1221
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +02001222 thread_id: unsigned_long(bitwise=True)
Tal Einatc7027b72015-05-16 14:14:49 +03001223 signalnum: int
1224 /
1225
1226Send a signal to a thread.
1227[clinic start generated code]*/
1228
Victor Stinnerb3e72192011-05-08 01:46:11 +02001229static PyObject *
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +02001230signal_pthread_kill_impl(PyObject *module, unsigned long thread_id,
1231 int signalnum)
1232/*[clinic end generated code: output=7629919b791bc27f input=1d901f2c7bb544ff]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +02001233{
Victor Stinnerb3e72192011-05-08 01:46:11 +02001234 int err;
1235
Tal Einatc7027b72015-05-16 14:14:49 +03001236 err = pthread_kill((pthread_t)thread_id, signalnum);
Victor Stinnerb3e72192011-05-08 01:46:11 +02001237 if (err != 0) {
1238 errno = err;
1239 PyErr_SetFromErrno(PyExc_OSError);
1240 return NULL;
1241 }
1242
1243 /* the signal may have been send to the current thread */
1244 if (PyErr_CheckSignals())
1245 return NULL;
1246
1247 Py_RETURN_NONE;
1248}
1249
Antoine Pitroua6a4dc82017-09-07 18:56:24 +02001250#endif /* #if defined(HAVE_PTHREAD_KILL) */
Victor Stinnerb3e72192011-05-08 01:46:11 +02001251
1252
1253
Tal Einatc7027b72015-05-16 14:14:49 +03001254/* List of functions defined in the module -- some of the methoddefs are
1255 defined to nothing if the corresponding C function is not available. */
Barry Warsaw92971171997-01-03 00:14:25 +00001256static PyMethodDef signal_methods[] = {
Tal Einatc7027b72015-05-16 14:14:49 +03001257 {"default_int_handler", signal_default_int_handler, METH_VARARGS, default_int_handler_doc},
1258 SIGNAL_ALARM_METHODDEF
1259 SIGNAL_SETITIMER_METHODDEF
1260 SIGNAL_GETITIMER_METHODDEF
1261 SIGNAL_SIGNAL_METHODDEF
Vladimir Matveevc24c6c22019-01-08 01:58:25 -08001262 SIGNAL_RAISE_SIGNAL_METHODDEF
Antoine Pietri5d2a27d2018-03-12 14:42:34 +01001263 SIGNAL_STRSIGNAL_METHODDEF
Tal Einatc7027b72015-05-16 14:14:49 +03001264 SIGNAL_GETSIGNAL_METHODDEF
Serhiy Storchaka62be7422018-11-27 13:27:31 +02001265 {"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 +03001266 SIGNAL_SIGINTERRUPT_METHODDEF
1267 SIGNAL_PAUSE_METHODDEF
1268 SIGNAL_PTHREAD_KILL_METHODDEF
1269 SIGNAL_PTHREAD_SIGMASK_METHODDEF
1270 SIGNAL_SIGPENDING_METHODDEF
1271 SIGNAL_SIGWAIT_METHODDEF
1272 SIGNAL_SIGWAITINFO_METHODDEF
1273 SIGNAL_SIGTIMEDWAIT_METHODDEF
Antoine Pitrou9d3627e2018-05-04 13:00:50 +02001274#if defined(HAVE_SIGFILLSET) || defined(MS_WINDOWS)
1275 SIGNAL_VALID_SIGNALS_METHODDEF
1276#endif
Tal Einatc7027b72015-05-16 14:14:49 +03001277 {NULL, NULL} /* sentinel */
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001278};
1279
Barry Warsaw92971171997-01-03 00:14:25 +00001280
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001281PyDoc_STRVAR(module_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001282"This module provides mechanisms to use signal handlers in Python.\n\
1283\n\
1284Functions:\n\
1285\n\
1286alarm() -- cause SIGALRM after a specified time [Unix only]\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001287setitimer() -- cause a signal (described below) after a specified\n\
1288 float time and the timer may restart then [Unix only]\n\
1289getitimer() -- get current value of timer [Unix only]\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001290signal() -- set the action for a given signal\n\
1291getsignal() -- get the signal action for a given signal\n\
1292pause() -- wait until a signal arrives [Unix only]\n\
1293default_int_handler() -- default SIGINT handler\n\
1294\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001295signal constants:\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001296SIG_DFL -- used to refer to the system default handler\n\
1297SIG_IGN -- used to ignore the signal\n\
1298NSIG -- number of defined signals\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001299SIGINT, SIGTERM, etc. -- signal numbers\n\
1300\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001301itimer constants:\n\
1302ITIMER_REAL -- decrements in real time, and delivers SIGALRM upon\n\
1303 expiration\n\
1304ITIMER_VIRTUAL -- decrements only when the process is executing,\n\
1305 and delivers SIGVTALRM upon expiration\n\
1306ITIMER_PROF -- decrements both when the process is executing and\n\
1307 when the system is executing on behalf of the process.\n\
1308 Coupled with ITIMER_VIRTUAL, this timer is usually\n\
1309 used to profile the time spent by the application\n\
1310 in user and kernel space. SIGPROF is delivered upon\n\
1311 expiration.\n\
1312\n\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001313*** IMPORTANT NOTICE ***\n\
1314A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001315the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001316
Martin v. Löwis1a214512008-06-11 05:26:20 +00001317static struct PyModuleDef signalmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001318 PyModuleDef_HEAD_INIT,
Brett Cannon815a6f32014-04-04 10:20:28 -04001319 "_signal",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001320 module_doc,
1321 -1,
1322 signal_methods,
1323 NULL,
1324 NULL,
1325 NULL,
1326 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001327};
1328
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001329PyMODINIT_FUNC
Giampaolo Rodola'e09fb712014-04-04 15:34:17 +02001330PyInit__signal(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001331{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001332 PyObject *m, *d, *x;
1333 int i;
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{
Victor Stinnerd8613dc2019-05-24 13:43:55 +02001625 _PyRuntimeState *runtime = &_PyRuntime;
1626 if (!is_main(runtime)) {
Eric Snow64d6cc82019-02-23 15:40:43 -07001627 return 0;
1628 }
1629
1630 return _PyErr_CheckSignals();
1631}
1632
1633
1634/* Declared in cpython/pyerrors.h */
1635int
1636_PyErr_CheckSignals(void)
1637{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001638 int i;
1639 PyObject *f;
Barry Warsaw92971171997-01-03 00:14:25 +00001640
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001641 if (!_Py_atomic_load(&is_tripped))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001642 return 0;
Christian Heimesb76922a2007-12-11 01:06:40 +00001643
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001644 /*
1645 * The is_tripped variable is meant to speed up the calls to
1646 * PyErr_CheckSignals (both directly or via pending calls) when no
1647 * signal has arrived. This variable is set to 1 when a signal arrives
1648 * and it is set to 0 here, when we know some signals arrived. This way
1649 * we can run the registered handlers with no signals blocked.
1650 *
1651 * NOTE: with this approach we can have a situation where is_tripped is
1652 * 1 but we have no more signals to handle (Handlers[i].tripped
1653 * is 0 for every signal i). This won't do us any harm (except
1654 * we're gonna spent some cycles for nothing). This happens when
1655 * we receive a signal i after we zero is_tripped and before we
1656 * check Handlers[i].tripped.
1657 */
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001658 _Py_atomic_store(&is_tripped, 0);
Christian Heimesb76922a2007-12-11 01:06:40 +00001659
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001660 if (!(f = (PyObject *)PyEval_GetFrame()))
1661 f = Py_None;
Christian Heimesb76922a2007-12-11 01:06:40 +00001662
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001663 for (i = 1; i < NSIG; i++) {
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001664 if (_Py_atomic_load_relaxed(&Handlers[i].tripped)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001665 PyObject *result = NULL;
1666 PyObject *arglist = Py_BuildValue("(iO)", i, f);
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001667 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Barry Warsaw92971171997-01-03 00:14:25 +00001668
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001669 if (arglist) {
1670 result = PyEval_CallObject(Handlers[i].func,
1671 arglist);
1672 Py_DECREF(arglist);
1673 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +02001674 if (!result) {
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001675 _Py_atomic_store(&is_tripped, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001676 return -1;
Antoine Pitrouc08177a2017-06-28 23:29:29 +02001677 }
Barry Warsaw92971171997-01-03 00:14:25 +00001678
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001679 Py_DECREF(result);
1680 }
1681 }
Christian Heimesb76922a2007-12-11 01:06:40 +00001682
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001683 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001684}
1685
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +00001686
Matěj Cepl608876b2019-05-23 22:30:00 +02001687/* Simulate the effect of a signal.SIGINT signal arriving. The next time
1688 PyErr_CheckSignals is called, the Python SIGINT signal handler will be
1689 raised.
1690
1691 Missing signal handler for the SIGINT signal is silently ignored. */
Barry Warsaw92971171997-01-03 00:14:25 +00001692void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001693PyErr_SetInterrupt(void)
Barry Warsaw92971171997-01-03 00:14:25 +00001694{
Matěj Cepl608876b2019-05-23 22:30:00 +02001695 if ((Handlers[SIGINT].func != IgnoreHandler) &&
1696 (Handlers[SIGINT].func != DefaultHandler)) {
1697 trip_signal(SIGINT);
1698 }
Barry Warsaw92971171997-01-03 00:14:25 +00001699}
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001700
1701void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001702PyOS_InitInterrupts(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001703{
Giampaolo Rodola'e09fb712014-04-04 15:34:17 +02001704 PyObject *m = PyImport_ImportModule("_signal");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001705 if (m) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001706 Py_DECREF(m);
1707 }
Guido van Rossum08c16611997-08-02 03:01:42 +00001708}
1709
1710void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001711PyOS_FiniInterrupts(void)
Guido van Rossum08c16611997-08-02 03:01:42 +00001712{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001713 finisignal();
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001714}
1715
1716int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001717PyOS_InterruptOccurred(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001718{
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001719 if (_Py_atomic_load_relaxed(&Handlers[SIGINT].tripped)) {
Victor Stinnerd8613dc2019-05-24 13:43:55 +02001720 _PyRuntimeState *runtime = &_PyRuntime;
1721 if (!is_main(runtime)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001722 return 0;
Eric Snow64d6cc82019-02-23 15:40:43 -07001723 }
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001724 _Py_atomic_store_relaxed(&Handlers[SIGINT].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001725 return 1;
1726 }
1727 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001728}
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001729
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001730static void
1731_clear_pending_signals(void)
1732{
1733 int i;
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001734 if (!_Py_atomic_load(&is_tripped))
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001735 return;
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001736 _Py_atomic_store(&is_tripped, 0);
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001737 for (i = 1; i < NSIG; ++i) {
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001738 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001739 }
1740}
1741
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001742void
Antoine Pitrou346cbd32017-05-27 17:50:54 +02001743_PySignal_AfterFork(void)
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001744{
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001745 /* Clear the signal flags after forking so that they aren't handled
1746 * in both processes if they came in just before the fork() but before
1747 * the interpreter had an opportunity to call the handlers. issue9535. */
1748 _clear_pending_signals();
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001749}
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001750
1751int
1752_PyOS_IsMainThread(void)
1753{
Victor Stinnerd8613dc2019-05-24 13:43:55 +02001754 _PyRuntimeState *runtime = &_PyRuntime;
1755 return is_main(runtime);
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001756}
1757
1758#ifdef MS_WINDOWS
1759void *_PyOS_SigintEvent(void)
1760{
1761 /* Returns a manual-reset event which gets tripped whenever
1762 SIGINT is received.
1763
1764 Python.h does not include windows.h so we do cannot use HANDLE
1765 as the return type of this function. We use void* instead. */
1766 return sigint_event;
1767}
1768#endif