blob: 119fc355ff1fd5d3b21c3de9bdcef29a1635c679 [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 Stinner6f7346b2020-06-03 18:28:18 +0200190is_main_interp(_PyRuntimeState *runtime, PyInterpreterState *interp)
Eric Snow64d6cc82019-02-23 15:40:43 -0700191{
Victor Stinnerd8613dc2019-05-24 13:43:55 +0200192 unsigned long thread = PyThread_get_thread_ident();
Victor Stinnerd8613dc2019-05-24 13:43:55 +0200193 return (thread == runtime->main_thread
194 && interp == runtime->interpreters.main);
Eric Snow64d6cc82019-02-23 15:40:43 -0700195}
196
Victor Stinner6f7346b2020-06-03 18:28:18 +0200197static int
198is_main(_PyRuntimeState *runtime)
199{
200 PyInterpreterState *interp = _PyRuntimeState_GetThreadState(runtime)->interp;
201 return is_main_interp(runtime, interp);
202}
203
Guido van Rossume4485b01994-09-07 14:32:49 +0000204static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000205signal_default_int_handler(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000206{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000207 PyErr_SetNone(PyExc_KeyboardInterrupt);
208 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000209}
210
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000211PyDoc_STRVAR(default_int_handler_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000212"default_int_handler(...)\n\
213\n\
Michael W. Hudson24ec2112004-06-17 15:55:53 +0000214The default handler for SIGINT installed by Python.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000215It raises KeyboardInterrupt.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000216
Thomas Wouters0796b002000-07-22 23:49:30 +0000217
218static int
Victor Stinner11517102014-07-29 23:31:34 +0200219report_wakeup_write_error(void *data)
Antoine Pitrou6f6ec372013-08-17 20:27:56 +0200220{
Eric Snowfdf282d2019-01-11 14:26:55 -0700221 PyObject *exc, *val, *tb;
Antoine Pitrou6f6ec372013-08-17 20:27:56 +0200222 int save_errno = errno;
Benjamin Petersonca470632016-09-06 13:47:26 -0700223 errno = (int) (intptr_t) data;
Eric Snowfdf282d2019-01-11 14:26:55 -0700224 PyErr_Fetch(&exc, &val, &tb);
Antoine Pitrou6f6ec372013-08-17 20:27:56 +0200225 PyErr_SetFromErrno(PyExc_OSError);
226 PySys_WriteStderr("Exception ignored when trying to write to the "
227 "signal wakeup fd:\n");
228 PyErr_WriteUnraisable(NULL);
Eric Snowfdf282d2019-01-11 14:26:55 -0700229 PyErr_Restore(exc, val, tb);
Antoine Pitrou6f6ec372013-08-17 20:27:56 +0200230 errno = save_errno;
231 return 0;
232}
233
Victor Stinner11517102014-07-29 23:31:34 +0200234#ifdef MS_WINDOWS
235static int
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800236report_wakeup_send_error(void* data)
Victor Stinner11517102014-07-29 23:31:34 +0200237{
Eric Snowfdf282d2019-01-11 14:26:55 -0700238 PyObject *exc, *val, *tb;
239 PyErr_Fetch(&exc, &val, &tb);
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800240 /* PyErr_SetExcFromWindowsErr() invokes FormatMessage() which
241 recognizes the error codes used by both GetLastError() and
242 WSAGetLastError */
243 PyErr_SetExcFromWindowsErr(PyExc_OSError, (int) (intptr_t) data);
Victor Stinner11517102014-07-29 23:31:34 +0200244 PySys_WriteStderr("Exception ignored when trying to send to the "
245 "signal wakeup fd:\n");
246 PyErr_WriteUnraisable(NULL);
Eric Snowfdf282d2019-01-11 14:26:55 -0700247 PyErr_Restore(exc, val, tb);
Victor Stinner11517102014-07-29 23:31:34 +0200248 return 0;
249}
250#endif /* MS_WINDOWS */
251
Tim Peters4f1b2082000-07-23 21:18:09 +0000252static void
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200253trip_signal(int sig_num)
254{
Victor Stinnerd49b1f12011-05-08 02:03:15 +0200255 unsigned char byte;
Victor Stinner11517102014-07-29 23:31:34 +0200256 int fd;
257 Py_ssize_t rc;
Victor Stinnerc13ef662011-05-25 02:35:58 +0200258
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +0200259 _Py_atomic_store_relaxed(&Handlers[sig_num].tripped, 1);
Victor Stinner11517102014-07-29 23:31:34 +0200260
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200261 /* Set is_tripped after setting .tripped, as it gets
262 cleared in PyErr_CheckSignals() before .tripped. */
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +0200263 _Py_atomic_store(&is_tripped, 1);
264
265 /* Notify ceval.c */
Victor Stinner09532fe2019-05-10 23:39:09 +0200266 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinner438a12d2019-05-24 17:01:38 +0200267 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Victor Stinner09532fe2019-05-10 23:39:09 +0200268 _PyEval_SignalReceived(&runtime->ceval);
Nathaniel J. Smith4ae01492017-05-16 14:12:11 -0700269
270 /* And then write to the wakeup fd *after* setting all the globals and
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200271 doing the _PyEval_SignalReceived. We used to write to the wakeup fd
272 and then set the flag, but this allowed the following sequence of events
273 (especially on windows, where trip_signal may run in a new thread):
Nathaniel J. Smith4ae01492017-05-16 14:12:11 -0700274
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800275 - main thread blocks on select([wakeup.fd], ...)
Nathaniel J. Smith4ae01492017-05-16 14:12:11 -0700276 - signal arrives
277 - trip_signal writes to the wakeup fd
278 - the main thread wakes up
279 - the main thread checks the signal flags, sees that they're unset
280 - the main thread empties the wakeup fd
281 - the main thread goes back to sleep
282 - trip_signal sets the flags to request the Python-level signal handler
283 be run
284 - the main thread doesn't notice, because it's asleep
285
286 See bpo-30038 for more details.
287 */
288
Victor Stinner11517102014-07-29 23:31:34 +0200289#ifdef MS_WINDOWS
290 fd = Py_SAFE_DOWNCAST(wakeup.fd, SOCKET_T, int);
291#else
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800292 fd = wakeup.fd;
Victor Stinner11517102014-07-29 23:31:34 +0200293#endif
294
295 if (fd != INVALID_FD) {
Victor Stinnerc13ef662011-05-25 02:35:58 +0200296 byte = (unsigned char)sig_num;
Victor Stinner11517102014-07-29 23:31:34 +0200297#ifdef MS_WINDOWS
298 if (wakeup.use_send) {
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800299 rc = send(fd, &byte, 1, 0);
Victor Stinner11517102014-07-29 23:31:34 +0200300
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800301 if (rc < 0) {
302 int last_error = GetLastError();
303 if (wakeup.warn_on_full_buffer ||
304 last_error != WSAEWOULDBLOCK)
305 {
306 /* Py_AddPendingCall() isn't signal-safe, but we
307 still use it for this exceptional case. */
Victor Stinnere225beb2019-06-03 18:14:24 +0200308 _PyEval_AddPendingCall(tstate, &runtime->ceval,
Victor Stinner09532fe2019-05-10 23:39:09 +0200309 report_wakeup_send_error,
310 (void *)(intptr_t) last_error);
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800311 }
Victor Stinner11517102014-07-29 23:31:34 +0200312 }
313 }
314 else
315#endif
316 {
Victor Stinnere72fe392015-04-01 18:35:22 +0200317 /* _Py_write_noraise() retries write() if write() is interrupted by
318 a signal (fails with EINTR). */
319 rc = _Py_write_noraise(fd, &byte, 1);
Victor Stinner11517102014-07-29 23:31:34 +0200320
321 if (rc < 0) {
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800322 if (wakeup.warn_on_full_buffer ||
323 (errno != EWOULDBLOCK && errno != EAGAIN))
324 {
325 /* Py_AddPendingCall() isn't signal-safe, but we
326 still use it for this exceptional case. */
Victor Stinnere225beb2019-06-03 18:14:24 +0200327 _PyEval_AddPendingCall(tstate, &runtime->ceval,
Victor Stinner09532fe2019-05-10 23:39:09 +0200328 report_wakeup_write_error,
329 (void *)(intptr_t)errno);
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800330 }
Victor Stinner11517102014-07-29 23:31:34 +0200331 }
332 }
Victor Stinnerc13ef662011-05-25 02:35:58 +0200333 }
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200334}
335
336static void
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000337signal_handler(int sig_num)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000338{
Antoine Pitrou39a65912010-11-05 19:47:27 +0000339 int save_errno = errno;
340
Jeroen Demeyerd237b3f2019-05-10 03:28:57 +0200341 trip_signal(sig_num);
Antoine Pitrou39a65912010-11-05 19:47:27 +0000342
Jean-Paul Calderone6f137ca2010-05-09 03:18:57 +0000343#ifndef HAVE_SIGACTION
Antoine Pitrou39a65912010-11-05 19:47:27 +0000344#ifdef SIGCHLD
345 /* To avoid infinite recursion, this signal remains
346 reset until explicit re-instated.
347 Don't clear the 'func' field as it is our pointer
348 to the Python handler... */
349 if (sig_num != SIGCHLD)
350#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000351 /* If the handler was not set up with sigaction, reinstall it. See
Nick Coghland6009512014-11-20 21:39:37 +1000352 * Python/pylifecycle.c for the implementation of PyOS_setsig which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000353 * makes this true. See also issue8354. */
354 PyOS_setsig(sig_num, signal_handler);
Jean-Paul Calderone6f137ca2010-05-09 03:18:57 +0000355#endif
Antoine Pitrou39a65912010-11-05 19:47:27 +0000356
357 /* Issue #10311: asynchronously executing signal handlers should not
358 mutate errno under the feet of unsuspecting C code. */
359 errno = save_errno;
Antoine Pitrou6dd381e2011-11-21 21:26:56 +0100360
361#ifdef MS_WINDOWS
362 if (sig_num == SIGINT)
363 SetEvent(sigint_event);
364#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000365}
Guido van Rossume4485b01994-09-07 14:32:49 +0000366
Guido van Rossum06d511d1995-03-10 15:13:48 +0000367
Guido van Rossum1171ee61997-08-22 20:42:00 +0000368#ifdef HAVE_ALARM
Tal Einatc7027b72015-05-16 14:14:49 +0300369
370/*[clinic input]
371signal.alarm -> long
372
373 seconds: int
374 /
375
376Arrange for SIGALRM to arrive after the given number of seconds.
377[clinic start generated code]*/
378
379static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300380signal_alarm_impl(PyObject *module, int seconds)
381/*[clinic end generated code: output=144232290814c298 input=0d5e97e0e6f39e86]*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000382{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000383 /* alarm() returns the number of seconds remaining */
Tal Einatc7027b72015-05-16 14:14:49 +0300384 return (long)alarm(seconds);
Guido van Rossumaa0f4c71994-08-23 13:49:37 +0000385}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000386
Guido van Rossum06d511d1995-03-10 15:13:48 +0000387#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000388
Guido van Rossum1171ee61997-08-22 20:42:00 +0000389#ifdef HAVE_PAUSE
Tal Einatc7027b72015-05-16 14:14:49 +0300390
391/*[clinic input]
392signal.pause
393
394Wait until a signal arrives.
395[clinic start generated code]*/
396
Guido van Rossuma597dde1995-01-10 20:56:29 +0000397static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300398signal_pause_impl(PyObject *module)
399/*[clinic end generated code: output=391656788b3c3929 input=f03de0f875752062]*/
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000400{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000401 Py_BEGIN_ALLOW_THREADS
402 (void)pause();
403 Py_END_ALLOW_THREADS
404 /* make sure that any exceptions that got raised are propagated
405 * back into Python
406 */
407 if (PyErr_CheckSignals())
408 return NULL;
Barry Warsaw92971171997-01-03 00:14:25 +0000409
Tal Einatc7027b72015-05-16 14:14:49 +0300410 Py_RETURN_NONE;
Guido van Rossume4485b01994-09-07 14:32:49 +0000411}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000412
Guido van Rossum06d511d1995-03-10 15:13:48 +0000413#endif
Guido van Rossume4485b01994-09-07 14:32:49 +0000414
Vladimir Matveevc24c6c22019-01-08 01:58:25 -0800415/*[clinic input]
416signal.raise_signal
417
418 signalnum: int
419 /
420
421Send a signal to the executing process.
422[clinic start generated code]*/
423
424static PyObject *
425signal_raise_signal_impl(PyObject *module, int signalnum)
426/*[clinic end generated code: output=e2b014220aa6111d input=e90c0f9a42358de6]*/
427{
428 int err;
429 Py_BEGIN_ALLOW_THREADS
430 _Py_BEGIN_SUPPRESS_IPH
431 err = raise(signalnum);
432 _Py_END_SUPPRESS_IPH
433 Py_END_ALLOW_THREADS
Victor Stinner09532fe2019-05-10 23:39:09 +0200434
Vladimir Matveevc24c6c22019-01-08 01:58:25 -0800435 if (err) {
436 return PyErr_SetFromErrno(PyExc_OSError);
437 }
438 Py_RETURN_NONE;
439}
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000440
Tal Einatc7027b72015-05-16 14:14:49 +0300441/*[clinic input]
442signal.signal
443
444 signalnum: int
445 handler: object
446 /
447
448Set the action for the given signal.
449
450The action can be SIG_DFL, SIG_IGN, or a callable Python object.
451The previous action is returned. See getsignal() for possible return values.
452
453*** IMPORTANT NOTICE ***
454A signal handler function is called with two arguments:
455the first is the signal number, the second is the interrupted stack frame.
456[clinic start generated code]*/
457
Guido van Rossume4485b01994-09-07 14:32:49 +0000458static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300459signal_signal_impl(PyObject *module, int signalnum, PyObject *handler)
460/*[clinic end generated code: output=b44cfda43780f3a1 input=deee84af5fa0432c]*/
Guido van Rossume4485b01994-09-07 14:32:49 +0000461{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000462 PyObject *old_handler;
463 void (*func)(int);
Brian Curtinef9efbd2010-08-06 19:27:32 +0000464#ifdef MS_WINDOWS
Tal Einatc7027b72015-05-16 14:14:49 +0300465 /* Validate that signalnum is one of the allowable signals */
466 switch (signalnum) {
Brian Curtinc734b312010-09-06 16:04:10 +0000467 case SIGABRT: break;
Brian Curtin9e88b5a2010-10-01 14:49:24 +0000468#ifdef SIGBREAK
469 /* Issue #10003: SIGBREAK is not documented as permitted, but works
470 and corresponds to CTRL_BREAK_EVENT. */
471 case SIGBREAK: break;
472#endif
Brian Curtinc734b312010-09-06 16:04:10 +0000473 case SIGFPE: break;
474 case SIGILL: break;
475 case SIGINT: break;
476 case SIGSEGV: break;
477 case SIGTERM: break;
478 default:
479 PyErr_SetString(PyExc_ValueError, "invalid signal value");
480 return NULL;
Brian Curtinef9efbd2010-08-06 19:27:32 +0000481 }
482#endif
Victor Stinnerd8613dc2019-05-24 13:43:55 +0200483
484 _PyRuntimeState *runtime = &_PyRuntime;
485 if (!is_main(runtime)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000486 PyErr_SetString(PyExc_ValueError,
487 "signal only works in main thread");
488 return NULL;
489 }
Tal Einatc7027b72015-05-16 14:14:49 +0300490 if (signalnum < 1 || signalnum >= NSIG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000491 PyErr_SetString(PyExc_ValueError,
492 "signal number out of range");
493 return NULL;
494 }
Tal Einatc7027b72015-05-16 14:14:49 +0300495 if (handler == IgnoreHandler)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000496 func = SIG_IGN;
Tal Einatc7027b72015-05-16 14:14:49 +0300497 else if (handler == DefaultHandler)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000498 func = SIG_DFL;
Tal Einatc7027b72015-05-16 14:14:49 +0300499 else if (!PyCallable_Check(handler)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000500 PyErr_SetString(PyExc_TypeError,
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000501"signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000502 return NULL;
503 }
504 else
505 func = signal_handler;
Antoine Pitrouf6f90ff2017-11-03 19:58:46 +0100506 /* Check for pending signals before changing signal handler */
Eric Snow64d6cc82019-02-23 15:40:43 -0700507 if (_PyErr_CheckSignals()) {
Antoine Pitrouf6f90ff2017-11-03 19:58:46 +0100508 return NULL;
509 }
Tal Einatc7027b72015-05-16 14:14:49 +0300510 if (PyOS_setsig(signalnum, func) == SIG_ERR) {
Victor Stinner388196e2011-05-10 17:13:00 +0200511 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000512 return NULL;
513 }
Tal Einatc7027b72015-05-16 14:14:49 +0300514 old_handler = Handlers[signalnum].func;
Tal Einatc7027b72015-05-16 14:14:49 +0300515 Py_INCREF(handler);
516 Handlers[signalnum].func = handler;
Antoine Pitrouc8c952c2013-05-04 23:16:59 +0200517 if (old_handler != NULL)
518 return old_handler;
519 else
520 Py_RETURN_NONE;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000521}
522
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000523
Tal Einatc7027b72015-05-16 14:14:49 +0300524/*[clinic input]
525signal.getsignal
526
527 signalnum: int
528 /
529
530Return the current action for the given signal.
531
532The return value can be:
533 SIG_IGN -- if the signal is being ignored
534 SIG_DFL -- if the default action for the signal is in effect
535 None -- if an unknown handler is in effect
536 anything else -- the callable Python object used as a handler
537[clinic start generated code]*/
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000538
Guido van Rossume4485b01994-09-07 14:32:49 +0000539static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300540signal_getsignal_impl(PyObject *module, int signalnum)
541/*[clinic end generated code: output=35b3e0e796fd555e input=ac23a00f19dfa509]*/
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000542{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000543 PyObject *old_handler;
Tal Einatc7027b72015-05-16 14:14:49 +0300544 if (signalnum < 1 || signalnum >= NSIG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000545 PyErr_SetString(PyExc_ValueError,
546 "signal number out of range");
547 return NULL;
548 }
Tal Einatc7027b72015-05-16 14:14:49 +0300549 old_handler = Handlers[signalnum].func;
Antoine Pitrouc8c952c2013-05-04 23:16:59 +0200550 if (old_handler != NULL) {
551 Py_INCREF(old_handler);
552 return old_handler;
553 }
554 else {
555 Py_RETURN_NONE;
556 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000557}
558
Antoine Pietri5d2a27d2018-03-12 14:42:34 +0100559
560/*[clinic input]
561signal.strsignal
562
563 signalnum: int
564 /
565
566Return the system description of the given signal.
567
568The return values can be such as "Interrupt", "Segmentation fault", etc.
569Returns None if the signal is not recognized.
570[clinic start generated code]*/
571
572static PyObject *
573signal_strsignal_impl(PyObject *module, int signalnum)
574/*[clinic end generated code: output=44e12e1e3b666261 input=b77914b03f856c74]*/
575{
576 char *res;
577
578 if (signalnum < 1 || signalnum >= NSIG) {
579 PyErr_SetString(PyExc_ValueError,
580 "signal number out of range");
581 return NULL;
582 }
583
Michael Osipov48ce4892018-08-23 15:27:19 +0200584#ifndef HAVE_STRSIGNAL
Antoine Pietri5d2a27d2018-03-12 14:42:34 +0100585 switch (signalnum) {
Michael Osipov48ce4892018-08-23 15:27:19 +0200586 /* Though being a UNIX, HP-UX does not provide strsignal(3). */
587#ifndef MS_WINDOWS
588 case SIGHUP:
589 res = "Hangup";
590 break;
591 case SIGALRM:
592 res = "Alarm clock";
593 break;
594 case SIGPIPE:
595 res = "Broken pipe";
596 break;
597 case SIGQUIT:
598 res = "Quit";
599 break;
600 case SIGCHLD:
601 res = "Child exited";
602 break;
603#endif
604 /* Custom redefinition of POSIX signals allowed on Windows. */
Antoine Pietri5d2a27d2018-03-12 14:42:34 +0100605 case SIGINT:
606 res = "Interrupt";
607 break;
608 case SIGILL:
609 res = "Illegal instruction";
610 break;
611 case SIGABRT:
612 res = "Aborted";
613 break;
614 case SIGFPE:
615 res = "Floating point exception";
616 break;
617 case SIGSEGV:
618 res = "Segmentation fault";
619 break;
620 case SIGTERM:
621 res = "Terminated";
622 break;
623 default:
624 Py_RETURN_NONE;
625 }
626#else
627 errno = 0;
628 res = strsignal(signalnum);
629
630 if (errno || res == NULL || strstr(res, "Unknown signal") != NULL)
631 Py_RETURN_NONE;
632#endif
633
634 return Py_BuildValue("s", res);
635}
636
Christian Heimes8640e742008-02-23 16:23:06 +0000637#ifdef HAVE_SIGINTERRUPT
Tal Einatc7027b72015-05-16 14:14:49 +0300638
639/*[clinic input]
640signal.siginterrupt
641
642 signalnum: int
643 flag: int
644 /
645
646Change system call restart behaviour.
647
648If flag is False, system calls will be restarted when interrupted by
649signal sig, else system calls will be interrupted.
650[clinic start generated code]*/
Christian Heimes8640e742008-02-23 16:23:06 +0000651
652static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300653signal_siginterrupt_impl(PyObject *module, int signalnum, int flag)
654/*[clinic end generated code: output=063816243d85dd19 input=4160acacca3e2099]*/
Christian Heimes8640e742008-02-23 16:23:06 +0000655{
Tal Einatc7027b72015-05-16 14:14:49 +0300656 if (signalnum < 1 || signalnum >= NSIG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000657 PyErr_SetString(PyExc_ValueError,
658 "signal number out of range");
659 return NULL;
660 }
Tal Einatc7027b72015-05-16 14:14:49 +0300661 if (siginterrupt(signalnum, flag)<0) {
Victor Stinner388196e2011-05-10 17:13:00 +0200662 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000663 return NULL;
664 }
Tal Einatc7027b72015-05-16 14:14:49 +0300665 Py_RETURN_NONE;
Christian Heimes8640e742008-02-23 16:23:06 +0000666}
667
668#endif
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000669
Tal Einatc7027b72015-05-16 14:14:49 +0300670
671static PyObject*
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800672signal_set_wakeup_fd(PyObject *self, PyObject *args, PyObject *kwds)
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000673{
Victor Stinnere134a7f2015-03-30 10:09:31 +0200674 struct _Py_stat_struct status;
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800675 static char *kwlist[] = {
676 "", "warn_on_full_buffer", NULL,
677 };
678 int warn_on_full_buffer = 1;
Victor Stinner11517102014-07-29 23:31:34 +0200679#ifdef MS_WINDOWS
680 PyObject *fdobj;
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100681 SOCKET_T sockfd, old_sockfd;
Victor Stinner11517102014-07-29 23:31:34 +0200682 int res;
683 int res_size = sizeof res;
684 PyObject *mod;
Victor Stinner11517102014-07-29 23:31:34 +0200685 int is_socket;
686
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800687 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|$p:set_wakeup_fd", kwlist,
688 &fdobj, &warn_on_full_buffer))
Victor Stinner11517102014-07-29 23:31:34 +0200689 return NULL;
690
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100691 sockfd = PyLong_AsSocket_t(fdobj);
692 if (sockfd == (SOCKET_T)(-1) && PyErr_Occurred())
Victor Stinner11517102014-07-29 23:31:34 +0200693 return NULL;
694#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000695 int fd, old_fd;
Victor Stinner11517102014-07-29 23:31:34 +0200696
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800697 if (!PyArg_ParseTupleAndKeywords(args, kwds, "i|$p:set_wakeup_fd", kwlist,
698 &fd, &warn_on_full_buffer))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000699 return NULL;
Victor Stinner11517102014-07-29 23:31:34 +0200700#endif
701
Victor Stinnerd8613dc2019-05-24 13:43:55 +0200702 _PyRuntimeState *runtime = &_PyRuntime;
703 if (!is_main(runtime)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000704 PyErr_SetString(PyExc_ValueError,
705 "set_wakeup_fd only works in main thread");
706 return NULL;
707 }
Victor Stinner0bffc942014-07-21 16:28:54 +0200708
Victor Stinner11517102014-07-29 23:31:34 +0200709#ifdef MS_WINDOWS
710 is_socket = 0;
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100711 if (sockfd != INVALID_FD) {
Victor Stinner11517102014-07-29 23:31:34 +0200712 /* Import the _socket module to call WSAStartup() */
713 mod = PyImport_ImportModuleNoBlock("_socket");
714 if (mod == NULL)
715 return NULL;
716 Py_DECREF(mod);
717
718 /* test the socket */
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100719 if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR,
Victor Stinner11517102014-07-29 23:31:34 +0200720 (char *)&res, &res_size) != 0) {
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100721 int fd, err;
722
723 err = WSAGetLastError();
Victor Stinner11517102014-07-29 23:31:34 +0200724 if (err != WSAENOTSOCK) {
725 PyErr_SetExcFromWindowsErr(PyExc_OSError, err);
726 return NULL;
727 }
728
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100729 fd = (int)sockfd;
Steve Dower940f33a2016-09-08 11:21:54 -0700730 if ((SOCKET_T)fd != sockfd) {
Victor Stinner11517102014-07-29 23:31:34 +0200731 PyErr_SetString(PyExc_ValueError, "invalid fd");
732 return NULL;
733 }
734
Victor Stinnere134a7f2015-03-30 10:09:31 +0200735 if (_Py_fstat(fd, &status) != 0)
Victor Stinner11517102014-07-29 23:31:34 +0200736 return NULL;
Victor Stinner38227602014-08-27 12:59:44 +0200737
738 /* on Windows, a file cannot be set to non-blocking mode */
Victor Stinner11517102014-07-29 23:31:34 +0200739 }
Victor Stinner38227602014-08-27 12:59:44 +0200740 else {
Victor Stinner11517102014-07-29 23:31:34 +0200741 is_socket = 1;
Victor Stinner38227602014-08-27 12:59:44 +0200742
743 /* Windows does not provide a function to test if a socket
744 is in non-blocking mode */
745 }
Victor Stinner11517102014-07-29 23:31:34 +0200746 }
747
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100748 old_sockfd = wakeup.fd;
749 wakeup.fd = sockfd;
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800750 wakeup.warn_on_full_buffer = warn_on_full_buffer;
Victor Stinner11517102014-07-29 23:31:34 +0200751 wakeup.use_send = is_socket;
752
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100753 if (old_sockfd != INVALID_FD)
754 return PyLong_FromSocket_t(old_sockfd);
Victor Stinner11517102014-07-29 23:31:34 +0200755 else
756 return PyLong_FromLong(-1);
757#else
Victor Stinnerd18ccd12014-07-24 21:58:53 +0200758 if (fd != -1) {
Victor Stinner38227602014-08-27 12:59:44 +0200759 int blocking;
760
Victor Stinnere134a7f2015-03-30 10:09:31 +0200761 if (_Py_fstat(fd, &status) != 0)
Victor Stinner11517102014-07-29 23:31:34 +0200762 return NULL;
Victor Stinner38227602014-08-27 12:59:44 +0200763
764 blocking = _Py_get_blocking(fd);
765 if (blocking < 0)
766 return NULL;
767 if (blocking) {
768 PyErr_Format(PyExc_ValueError,
769 "the fd %i must be in non-blocking mode",
770 fd);
771 return NULL;
772 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000773 }
Victor Stinner0bffc942014-07-21 16:28:54 +0200774
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800775 old_fd = wakeup.fd;
776 wakeup.fd = fd;
777 wakeup.warn_on_full_buffer = warn_on_full_buffer;
Victor Stinner0bffc942014-07-21 16:28:54 +0200778
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000779 return PyLong_FromLong(old_fd);
Victor Stinner11517102014-07-29 23:31:34 +0200780#endif
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000781}
782
783PyDoc_STRVAR(set_wakeup_fd_doc,
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800784"set_wakeup_fd(fd, *, warn_on_full_buffer=True) -> fd\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000785\n\
Victor Stinner11517102014-07-29 23:31:34 +0200786Sets the fd to be written to (with the signal number) when a signal\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000787comes in. A library can use this to wakeup select or poll.\n\
Victor Stinner11517102014-07-29 23:31:34 +0200788The previous fd or -1 is returned.\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000789\n\
790The fd must be non-blocking.");
791
792/* C API for the same, without all the error checking */
793int
794PySignal_SetWakeupFd(int fd)
795{
Victor Stinner11517102014-07-29 23:31:34 +0200796 int old_fd;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000797 if (fd < 0)
798 fd = -1;
Victor Stinner11517102014-07-29 23:31:34 +0200799
800#ifdef MS_WINDOWS
801 old_fd = Py_SAFE_DOWNCAST(wakeup.fd, SOCKET_T, int);
Victor Stinner11517102014-07-29 23:31:34 +0200802#else
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800803 old_fd = wakeup.fd;
Victor Stinner11517102014-07-29 23:31:34 +0200804#endif
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800805 wakeup.fd = fd;
806 wakeup.warn_on_full_buffer = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000807 return old_fd;
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000808}
809
810
Martin v. Löwis823725e2008-03-24 13:39:54 +0000811#ifdef HAVE_SETITIMER
Tal Einatc7027b72015-05-16 14:14:49 +0300812
813/*[clinic input]
814signal.setitimer
815
816 which: int
Victor Stinneref611c92017-10-13 13:49:43 -0700817 seconds: object
818 interval: object(c_default="NULL") = 0.0
Tal Einatc7027b72015-05-16 14:14:49 +0300819 /
820
821Sets given itimer (one of ITIMER_REAL, ITIMER_VIRTUAL or ITIMER_PROF).
822
823The timer will fire after value seconds and after that every interval seconds.
824The itimer can be cleared by setting seconds to zero.
825
826Returns old values as a tuple: (delay, interval).
827[clinic start generated code]*/
828
Martin v. Löwis823725e2008-03-24 13:39:54 +0000829static PyObject *
Victor Stinneref611c92017-10-13 13:49:43 -0700830signal_setitimer_impl(PyObject *module, int which, PyObject *seconds,
831 PyObject *interval)
832/*[clinic end generated code: output=65f9dcbddc35527b input=de43daf194e6f66f]*/
Martin v. Löwis823725e2008-03-24 13:39:54 +0000833{
Martin v. Löwis823725e2008-03-24 13:39:54 +0000834 struct itimerval new, old;
835
Victor Stinneref611c92017-10-13 13:49:43 -0700836 if (timeval_from_double(seconds, &new.it_value) < 0) {
837 return NULL;
838 }
839 if (timeval_from_double(interval, &new.it_interval) < 0) {
840 return NULL;
841 }
842
Martin v. Löwis823725e2008-03-24 13:39:54 +0000843 /* Let OS check "which" value */
844 if (setitimer(which, &new, &old) != 0) {
Tal Einatc7027b72015-05-16 14:14:49 +0300845 PyErr_SetFromErrno(ItimerError);
846 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000847 }
848
849 return itimer_retval(&old);
850}
851
Martin v. Löwis823725e2008-03-24 13:39:54 +0000852#endif
853
854
855#ifdef HAVE_GETITIMER
Tal Einatc7027b72015-05-16 14:14:49 +0300856
857/*[clinic input]
858signal.getitimer
859
860 which: int
861 /
862
863Returns current value of given itimer.
864[clinic start generated code]*/
865
Martin v. Löwis823725e2008-03-24 13:39:54 +0000866static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300867signal_getitimer_impl(PyObject *module, int which)
868/*[clinic end generated code: output=9e053175d517db40 input=f7d21d38f3490627]*/
Martin v. Löwis823725e2008-03-24 13:39:54 +0000869{
Martin v. Löwis823725e2008-03-24 13:39:54 +0000870 struct itimerval old;
871
Martin v. Löwis823725e2008-03-24 13:39:54 +0000872 if (getitimer(which, &old) != 0) {
Tal Einatc7027b72015-05-16 14:14:49 +0300873 PyErr_SetFromErrno(ItimerError);
874 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000875 }
876
877 return itimer_retval(&old);
878}
879
Martin v. Löwis823725e2008-03-24 13:39:54 +0000880#endif
881
Victor Stinnerb3e72192011-05-08 01:46:11 +0200882#if defined(PYPTHREAD_SIGMASK) || defined(HAVE_SIGPENDING)
Victor Stinner35b300c2011-05-04 13:20:35 +0200883static PyObject*
884sigset_to_set(sigset_t mask)
885{
886 PyObject *signum, *result;
887 int sig;
888
889 result = PySet_New(0);
890 if (result == NULL)
891 return NULL;
892
893 for (sig = 1; sig < NSIG; sig++) {
894 if (sigismember(&mask, sig) != 1)
895 continue;
896
897 /* Handle the case where it is a member by adding the signal to
898 the result list. Ignore the other cases because they mean the
899 signal isn't a member of the mask or the signal was invalid,
900 and an invalid signal must have been our fault in constructing
901 the loop boundaries. */
902 signum = PyLong_FromLong(sig);
903 if (signum == NULL) {
904 Py_DECREF(result);
905 return NULL;
906 }
907 if (PySet_Add(result, signum) == -1) {
908 Py_DECREF(signum);
909 Py_DECREF(result);
910 return NULL;
911 }
912 Py_DECREF(signum);
913 }
914 return result;
915}
Victor Stinnerb3e72192011-05-08 01:46:11 +0200916#endif
Victor Stinner35b300c2011-05-04 13:20:35 +0200917
Victor Stinnerb3e72192011-05-08 01:46:11 +0200918#ifdef PYPTHREAD_SIGMASK
Tal Einatc7027b72015-05-16 14:14:49 +0300919
920/*[clinic input]
921signal.pthread_sigmask
922
923 how: int
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300924 mask: sigset_t
Tal Einatc7027b72015-05-16 14:14:49 +0300925 /
926
927Fetch and/or change the signal mask of the calling thread.
928[clinic start generated code]*/
929
Victor Stinnera9293352011-04-30 15:21:58 +0200930static PyObject *
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300931signal_pthread_sigmask_impl(PyObject *module, int how, sigset_t mask)
932/*[clinic end generated code: output=0562c0fb192981a8 input=85bcebda442fa77f]*/
Victor Stinnera9293352011-04-30 15:21:58 +0200933{
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300934 sigset_t previous;
Victor Stinnera9293352011-04-30 15:21:58 +0200935 int err;
936
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300937 err = pthread_sigmask(how, &mask, &previous);
Victor Stinnera9293352011-04-30 15:21:58 +0200938 if (err != 0) {
939 errno = err;
Victor Stinnerb3e72192011-05-08 01:46:11 +0200940 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnera9293352011-04-30 15:21:58 +0200941 return NULL;
942 }
943
Victor Stinnerd0e516d2011-05-03 14:57:12 +0200944 /* if signals was unblocked, signal handlers have been called */
945 if (PyErr_CheckSignals())
946 return NULL;
947
Victor Stinner35b300c2011-05-04 13:20:35 +0200948 return sigset_to_set(previous);
Victor Stinnera9293352011-04-30 15:21:58 +0200949}
950
Victor Stinnera9293352011-04-30 15:21:58 +0200951#endif /* #ifdef PYPTHREAD_SIGMASK */
952
Martin v. Löwis823725e2008-03-24 13:39:54 +0000953
Victor Stinnerb3e72192011-05-08 01:46:11 +0200954#ifdef HAVE_SIGPENDING
Tal Einatc7027b72015-05-16 14:14:49 +0300955
956/*[clinic input]
957signal.sigpending
958
959Examine pending signals.
960
961Returns a set of signal numbers that are pending for delivery to
962the calling thread.
963[clinic start generated code]*/
964
Victor Stinnerb3e72192011-05-08 01:46:11 +0200965static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300966signal_sigpending_impl(PyObject *module)
967/*[clinic end generated code: output=53375ffe89325022 input=e0036c016f874e29]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +0200968{
969 int err;
970 sigset_t mask;
971 err = sigpending(&mask);
972 if (err)
973 return PyErr_SetFromErrno(PyExc_OSError);
974 return sigset_to_set(mask);
975}
976
Victor Stinnerb3e72192011-05-08 01:46:11 +0200977#endif /* #ifdef HAVE_SIGPENDING */
978
979
980#ifdef HAVE_SIGWAIT
Tal Einatc7027b72015-05-16 14:14:49 +0300981
982/*[clinic input]
983signal.sigwait
984
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300985 sigset: sigset_t
Tal Einatc7027b72015-05-16 14:14:49 +0300986 /
987
988Wait for a signal.
989
990Suspend execution of the calling thread until the delivery of one of the
991signals specified in the signal set sigset. The function accepts the signal
992and returns the signal number.
993[clinic start generated code]*/
994
Victor Stinnerb3e72192011-05-08 01:46:11 +0200995static PyObject *
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300996signal_sigwait_impl(PyObject *module, sigset_t sigset)
997/*[clinic end generated code: output=f43770699d682f96 input=a6fbd47b1086d119]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +0200998{
Victor Stinnerb3e72192011-05-08 01:46:11 +0200999 int err, signum;
1000
Victor Stinner10c30d62011-06-10 01:39:53 +02001001 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001002 err = sigwait(&sigset, &signum);
Victor Stinner10c30d62011-06-10 01:39:53 +02001003 Py_END_ALLOW_THREADS
Victor Stinnerb3e72192011-05-08 01:46:11 +02001004 if (err) {
1005 errno = err;
1006 return PyErr_SetFromErrno(PyExc_OSError);
1007 }
1008
1009 return PyLong_FromLong(signum);
1010}
1011
Tal Einatc7027b72015-05-16 14:14:49 +03001012#endif /* #ifdef HAVE_SIGWAIT */
1013
Victor Stinnerb3e72192011-05-08 01:46:11 +02001014
Antoine Pitrou9d3627e2018-05-04 13:00:50 +02001015#if defined(HAVE_SIGFILLSET) || defined(MS_WINDOWS)
1016
1017/*[clinic input]
1018signal.valid_signals
1019
1020Return a set of valid signal numbers on this platform.
1021
1022The signal numbers returned by this function can be safely passed to
1023functions like `pthread_sigmask`.
1024[clinic start generated code]*/
1025
1026static PyObject *
1027signal_valid_signals_impl(PyObject *module)
1028/*[clinic end generated code: output=1609cffbcfcf1314 input=86a3717ff25288f2]*/
1029{
1030#ifdef MS_WINDOWS
1031#ifdef SIGBREAK
1032 PyObject *tup = Py_BuildValue("(iiiiiii)", SIGABRT, SIGBREAK, SIGFPE,
1033 SIGILL, SIGINT, SIGSEGV, SIGTERM);
1034#else
1035 PyObject *tup = Py_BuildValue("(iiiiii)", SIGABRT, SIGFPE, SIGILL,
1036 SIGINT, SIGSEGV, SIGTERM);
1037#endif
1038 if (tup == NULL) {
1039 return NULL;
1040 }
1041 PyObject *set = PySet_New(tup);
1042 Py_DECREF(tup);
1043 return set;
1044#else
1045 sigset_t mask;
1046 if (sigemptyset(&mask) || sigfillset(&mask)) {
1047 return PyErr_SetFromErrno(PyExc_OSError);
1048 }
1049 return sigset_to_set(mask);
1050#endif
1051}
1052
1053#endif /* #if defined(HAVE_SIGFILLSET) || defined(MS_WINDOWS) */
1054
1055
Ross Lagerwallbc808222011-06-25 12:13:40 +02001056#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
1057static int initialized;
1058static PyStructSequence_Field struct_siginfo_fields[] = {
1059 {"si_signo", "signal number"},
1060 {"si_code", "signal code"},
1061 {"si_errno", "errno associated with this signal"},
1062 {"si_pid", "sending process ID"},
1063 {"si_uid", "real user ID of sending process"},
1064 {"si_status", "exit value or signal"},
1065 {"si_band", "band event for SIGPOLL"},
1066 {0}
1067};
1068
1069PyDoc_STRVAR(struct_siginfo__doc__,
1070"struct_siginfo: Result from sigwaitinfo or sigtimedwait.\n\n\
1071This object may be accessed either as a tuple of\n\
1072(si_signo, si_code, si_errno, si_pid, si_uid, si_status, si_band),\n\
1073or via the attributes si_signo, si_code, and so on.");
1074
1075static PyStructSequence_Desc struct_siginfo_desc = {
1076 "signal.struct_siginfo", /* name */
1077 struct_siginfo__doc__, /* doc */
1078 struct_siginfo_fields, /* fields */
1079 7 /* n_in_sequence */
1080};
1081
1082static PyTypeObject SiginfoType;
1083
1084static PyObject *
1085fill_siginfo(siginfo_t *si)
1086{
1087 PyObject *result = PyStructSequence_New(&SiginfoType);
1088 if (!result)
1089 return NULL;
1090
1091 PyStructSequence_SET_ITEM(result, 0, PyLong_FromLong((long)(si->si_signo)));
1092 PyStructSequence_SET_ITEM(result, 1, PyLong_FromLong((long)(si->si_code)));
Victor Stinner09532fe2019-05-10 23:39:09 +02001093#ifdef __VXWORKS__
pxinwr8b5bdda2019-03-14 01:18:25 +08001094 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong(0L));
1095 PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong(0L));
1096 PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong(0L));
1097 PyStructSequence_SET_ITEM(result, 5, PyLong_FromLong(0L));
Victor Stinner09532fe2019-05-10 23:39:09 +02001098#else
Ross Lagerwallbc808222011-06-25 12:13:40 +02001099 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si->si_errno)));
1100 PyStructSequence_SET_ITEM(result, 3, PyLong_FromPid(si->si_pid));
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02001101 PyStructSequence_SET_ITEM(result, 4, _PyLong_FromUid(si->si_uid));
Ross Lagerwallbc808222011-06-25 12:13:40 +02001102 PyStructSequence_SET_ITEM(result, 5,
1103 PyLong_FromLong((long)(si->si_status)));
Victor Stinner09532fe2019-05-10 23:39:09 +02001104#endif
Zachary Ware6a6967e2016-10-01 00:47:27 -05001105#ifdef HAVE_SIGINFO_T_SI_BAND
Ross Lagerwallbc808222011-06-25 12:13:40 +02001106 PyStructSequence_SET_ITEM(result, 6, PyLong_FromLong(si->si_band));
Zachary Ware6a6967e2016-10-01 00:47:27 -05001107#else
1108 PyStructSequence_SET_ITEM(result, 6, PyLong_FromLong(0L));
1109#endif
Ross Lagerwallbc808222011-06-25 12:13:40 +02001110 if (PyErr_Occurred()) {
1111 Py_DECREF(result);
1112 return NULL;
1113 }
1114
1115 return result;
1116}
1117#endif
1118
1119#ifdef HAVE_SIGWAITINFO
Tal Einatc7027b72015-05-16 14:14:49 +03001120
1121/*[clinic input]
1122signal.sigwaitinfo
1123
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001124 sigset: sigset_t
Tal Einatc7027b72015-05-16 14:14:49 +03001125 /
1126
1127Wait synchronously until one of the signals in *sigset* is delivered.
1128
1129Returns a struct_siginfo containing information about the signal.
1130[clinic start generated code]*/
1131
Ross Lagerwallbc808222011-06-25 12:13:40 +02001132static PyObject *
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001133signal_sigwaitinfo_impl(PyObject *module, sigset_t sigset)
1134/*[clinic end generated code: output=1eb2f1fa236fdbca input=3d1a7e1f27fc664c]*/
Ross Lagerwallbc808222011-06-25 12:13:40 +02001135{
Ross Lagerwallbc808222011-06-25 12:13:40 +02001136 siginfo_t si;
1137 int err;
Victor Stinnera453cd82015-03-20 12:54:28 +01001138 int async_err = 0;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001139
Victor Stinnera453cd82015-03-20 12:54:28 +01001140 do {
1141 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001142 err = sigwaitinfo(&sigset, &si);
Victor Stinnera453cd82015-03-20 12:54:28 +01001143 Py_END_ALLOW_THREADS
1144 } while (err == -1
1145 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Ross Lagerwallbc808222011-06-25 12:13:40 +02001146 if (err == -1)
Victor Stinnera453cd82015-03-20 12:54:28 +01001147 return (!async_err) ? PyErr_SetFromErrno(PyExc_OSError) : NULL;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001148
1149 return fill_siginfo(&si);
1150}
1151
Ross Lagerwallbc808222011-06-25 12:13:40 +02001152#endif /* #ifdef HAVE_SIGWAITINFO */
1153
1154#ifdef HAVE_SIGTIMEDWAIT
Tal Einatc7027b72015-05-16 14:14:49 +03001155
1156/*[clinic input]
1157signal.sigtimedwait
1158
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001159 sigset: sigset_t
Serhiy Storchaka6b680cd2015-05-16 15:57:56 +03001160 timeout as timeout_obj: object
Tal Einatc7027b72015-05-16 14:14:49 +03001161 /
1162
1163Like sigwaitinfo(), but with a timeout.
1164
1165The timeout is specified in seconds, with floating point numbers allowed.
1166[clinic start generated code]*/
1167
Ross Lagerwallbc808222011-06-25 12:13:40 +02001168static PyObject *
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001169signal_sigtimedwait_impl(PyObject *module, sigset_t sigset,
Serhiy Storchaka6b680cd2015-05-16 15:57:56 +03001170 PyObject *timeout_obj)
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001171/*[clinic end generated code: output=59c8971e8ae18a64 input=87fd39237cf0b7ba]*/
Ross Lagerwallbc808222011-06-25 12:13:40 +02001172{
Victor Stinnera453cd82015-03-20 12:54:28 +01001173 struct timespec ts;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001174 siginfo_t si;
Victor Stinnera453cd82015-03-20 12:54:28 +01001175 int res;
Victor Stinner34dc0f42015-03-27 18:19:03 +01001176 _PyTime_t timeout, deadline, monotonic;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001177
Victor Stinner869e1772015-03-30 03:49:14 +02001178 if (_PyTime_FromSecondsObject(&timeout,
1179 timeout_obj, _PyTime_ROUND_CEILING) < 0)
Ross Lagerwallbc808222011-06-25 12:13:40 +02001180 return NULL;
1181
Victor Stinnera453cd82015-03-20 12:54:28 +01001182 if (timeout < 0) {
Ross Lagerwallbc808222011-06-25 12:13:40 +02001183 PyErr_SetString(PyExc_ValueError, "timeout must be non-negative");
1184 return NULL;
1185 }
1186
Victor Stinner34dc0f42015-03-27 18:19:03 +01001187 deadline = _PyTime_GetMonotonicClock() + timeout;
Victor Stinnera453cd82015-03-20 12:54:28 +01001188
1189 do {
Victor Stinner34dc0f42015-03-27 18:19:03 +01001190 if (_PyTime_AsTimespec(timeout, &ts) < 0)
1191 return NULL;
Victor Stinnera453cd82015-03-20 12:54:28 +01001192
1193 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001194 res = sigtimedwait(&sigset, &si, &ts);
Victor Stinnera453cd82015-03-20 12:54:28 +01001195 Py_END_ALLOW_THREADS
1196
1197 if (res != -1)
1198 break;
1199
1200 if (errno != EINTR) {
1201 if (errno == EAGAIN)
1202 Py_RETURN_NONE;
1203 else
1204 return PyErr_SetFromErrno(PyExc_OSError);
1205 }
1206
1207 /* sigtimedwait() was interrupted by a signal (EINTR) */
1208 if (PyErr_CheckSignals())
1209 return NULL;
1210
Victor Stinner34dc0f42015-03-27 18:19:03 +01001211 monotonic = _PyTime_GetMonotonicClock();
1212 timeout = deadline - monotonic;
Victor Stinner6aa446c2015-03-30 21:33:51 +02001213 if (timeout < 0)
Victor Stinnera453cd82015-03-20 12:54:28 +01001214 break;
1215 } while (1);
Ross Lagerwallbc808222011-06-25 12:13:40 +02001216
1217 return fill_siginfo(&si);
1218}
1219
Ross Lagerwallbc808222011-06-25 12:13:40 +02001220#endif /* #ifdef HAVE_SIGTIMEDWAIT */
1221
Victor Stinnerb3e72192011-05-08 01:46:11 +02001222
Antoine Pitroua6a4dc82017-09-07 18:56:24 +02001223#if defined(HAVE_PTHREAD_KILL)
Tal Einatc7027b72015-05-16 14:14:49 +03001224
1225/*[clinic input]
1226signal.pthread_kill
1227
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +02001228 thread_id: unsigned_long(bitwise=True)
Tal Einatc7027b72015-05-16 14:14:49 +03001229 signalnum: int
1230 /
1231
1232Send a signal to a thread.
1233[clinic start generated code]*/
1234
Victor Stinnerb3e72192011-05-08 01:46:11 +02001235static PyObject *
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +02001236signal_pthread_kill_impl(PyObject *module, unsigned long thread_id,
1237 int signalnum)
1238/*[clinic end generated code: output=7629919b791bc27f input=1d901f2c7bb544ff]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +02001239{
Victor Stinnerb3e72192011-05-08 01:46:11 +02001240 int err;
1241
Steve Dowera00b5be2020-02-13 08:30:27 +00001242 if (PySys_Audit("signal.pthread_kill", "ki", thread_id, signalnum) < 0) {
1243 return NULL;
1244 }
1245
Tal Einatc7027b72015-05-16 14:14:49 +03001246 err = pthread_kill((pthread_t)thread_id, signalnum);
Victor Stinnerb3e72192011-05-08 01:46:11 +02001247 if (err != 0) {
1248 errno = err;
1249 PyErr_SetFromErrno(PyExc_OSError);
1250 return NULL;
1251 }
1252
1253 /* the signal may have been send to the current thread */
1254 if (PyErr_CheckSignals())
1255 return NULL;
1256
1257 Py_RETURN_NONE;
1258}
1259
Antoine Pitroua6a4dc82017-09-07 18:56:24 +02001260#endif /* #if defined(HAVE_PTHREAD_KILL) */
Victor Stinnerb3e72192011-05-08 01:46:11 +02001261
1262
1263
Tal Einatc7027b72015-05-16 14:14:49 +03001264/* List of functions defined in the module -- some of the methoddefs are
1265 defined to nothing if the corresponding C function is not available. */
Barry Warsaw92971171997-01-03 00:14:25 +00001266static PyMethodDef signal_methods[] = {
Tal Einatc7027b72015-05-16 14:14:49 +03001267 {"default_int_handler", signal_default_int_handler, METH_VARARGS, default_int_handler_doc},
1268 SIGNAL_ALARM_METHODDEF
1269 SIGNAL_SETITIMER_METHODDEF
1270 SIGNAL_GETITIMER_METHODDEF
1271 SIGNAL_SIGNAL_METHODDEF
Vladimir Matveevc24c6c22019-01-08 01:58:25 -08001272 SIGNAL_RAISE_SIGNAL_METHODDEF
Antoine Pietri5d2a27d2018-03-12 14:42:34 +01001273 SIGNAL_STRSIGNAL_METHODDEF
Tal Einatc7027b72015-05-16 14:14:49 +03001274 SIGNAL_GETSIGNAL_METHODDEF
Serhiy Storchaka62be7422018-11-27 13:27:31 +02001275 {"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 +03001276 SIGNAL_SIGINTERRUPT_METHODDEF
1277 SIGNAL_PAUSE_METHODDEF
1278 SIGNAL_PTHREAD_KILL_METHODDEF
1279 SIGNAL_PTHREAD_SIGMASK_METHODDEF
1280 SIGNAL_SIGPENDING_METHODDEF
1281 SIGNAL_SIGWAIT_METHODDEF
1282 SIGNAL_SIGWAITINFO_METHODDEF
1283 SIGNAL_SIGTIMEDWAIT_METHODDEF
Antoine Pitrou9d3627e2018-05-04 13:00:50 +02001284#if defined(HAVE_SIGFILLSET) || defined(MS_WINDOWS)
1285 SIGNAL_VALID_SIGNALS_METHODDEF
1286#endif
Tal Einatc7027b72015-05-16 14:14:49 +03001287 {NULL, NULL} /* sentinel */
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001288};
1289
Barry Warsaw92971171997-01-03 00:14:25 +00001290
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001291PyDoc_STRVAR(module_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001292"This module provides mechanisms to use signal handlers in Python.\n\
1293\n\
1294Functions:\n\
1295\n\
1296alarm() -- cause SIGALRM after a specified time [Unix only]\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001297setitimer() -- cause a signal (described below) after a specified\n\
1298 float time and the timer may restart then [Unix only]\n\
1299getitimer() -- get current value of timer [Unix only]\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001300signal() -- set the action for a given signal\n\
1301getsignal() -- get the signal action for a given signal\n\
1302pause() -- wait until a signal arrives [Unix only]\n\
1303default_int_handler() -- default SIGINT handler\n\
1304\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001305signal constants:\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001306SIG_DFL -- used to refer to the system default handler\n\
1307SIG_IGN -- used to ignore the signal\n\
1308NSIG -- number of defined signals\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001309SIGINT, SIGTERM, etc. -- signal numbers\n\
1310\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001311itimer constants:\n\
1312ITIMER_REAL -- decrements in real time, and delivers SIGALRM upon\n\
1313 expiration\n\
1314ITIMER_VIRTUAL -- decrements only when the process is executing,\n\
1315 and delivers SIGVTALRM upon expiration\n\
1316ITIMER_PROF -- decrements both when the process is executing and\n\
1317 when the system is executing on behalf of the process.\n\
1318 Coupled with ITIMER_VIRTUAL, this timer is usually\n\
1319 used to profile the time spent by the application\n\
1320 in user and kernel space. SIGPROF is delivered upon\n\
1321 expiration.\n\
1322\n\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001323*** IMPORTANT NOTICE ***\n\
1324A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001325the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001326
Martin v. Löwis1a214512008-06-11 05:26:20 +00001327static struct PyModuleDef signalmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001328 PyModuleDef_HEAD_INIT,
Brett Cannon815a6f32014-04-04 10:20:28 -04001329 "_signal",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001330 module_doc,
1331 -1,
1332 signal_methods,
1333 NULL,
1334 NULL,
1335 NULL,
1336 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001337};
1338
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001339PyMODINIT_FUNC
Giampaolo Rodola'e09fb712014-04-04 15:34:17 +02001340PyInit__signal(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001341{
Miss Islington (bot)b150d0b2019-09-09 07:42:35 -07001342 PyObject *m, *d;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001343 int i;
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001344
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001345 /* Create the module and add the functions */
1346 m = PyModule_Create(&signalmodule);
1347 if (m == NULL)
1348 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001349
Ross Lagerwallbc808222011-06-25 12:13:40 +02001350#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
Victor Stinner1c8f0592013-07-22 22:24:54 +02001351 if (!initialized) {
1352 if (PyStructSequence_InitType2(&SiginfoType, &struct_siginfo_desc) < 0)
1353 return NULL;
1354 }
Ross Lagerwallbc808222011-06-25 12:13:40 +02001355 Py_INCREF((PyObject*) &SiginfoType);
1356 PyModule_AddObject(m, "struct_siginfo", (PyObject*) &SiginfoType);
1357 initialized = 1;
1358#endif
1359
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001360 /* Add some symbolic constants to the module */
1361 d = PyModule_GetDict(m);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001362
Miss Islington (bot)b150d0b2019-09-09 07:42:35 -07001363 DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL);
1364 if (!DefaultHandler ||
1365 PyDict_SetItemString(d, "SIG_DFL", DefaultHandler) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001366 goto finally;
Miss Islington (bot)b150d0b2019-09-09 07:42:35 -07001367 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001368
Miss Islington (bot)b150d0b2019-09-09 07:42:35 -07001369 IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN);
1370 if (!IgnoreHandler ||
1371 PyDict_SetItemString(d, "SIG_IGN", IgnoreHandler) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001372 goto finally;
Miss Islington (bot)b150d0b2019-09-09 07:42:35 -07001373 }
Barry Warsaw92971171997-01-03 00:14:25 +00001374
Joannah Nanjekye9541bd32019-04-21 21:47:06 -04001375 if (PyModule_AddIntMacro(m, NSIG))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001376 goto finally;
Barry Warsaw92971171997-01-03 00:14:25 +00001377
Victor Stinnera9293352011-04-30 15:21:58 +02001378#ifdef SIG_BLOCK
Victor Stinner72c53b52011-05-02 16:15:43 +02001379 if (PyModule_AddIntMacro(m, SIG_BLOCK))
1380 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001381#endif
Victor Stinnera9293352011-04-30 15:21:58 +02001382#ifdef SIG_UNBLOCK
Victor Stinner72c53b52011-05-02 16:15:43 +02001383 if (PyModule_AddIntMacro(m, SIG_UNBLOCK))
1384 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001385#endif
Victor Stinnera9293352011-04-30 15:21:58 +02001386#ifdef SIG_SETMASK
Victor Stinner72c53b52011-05-02 16:15:43 +02001387 if (PyModule_AddIntMacro(m, SIG_SETMASK))
1388 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001389#endif
1390
Miss Islington (bot)b150d0b2019-09-09 07:42:35 -07001391 IntHandler = PyDict_GetItemString(d, "default_int_handler");
1392 if (!IntHandler)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001393 goto finally;
1394 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +00001395
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001396 _Py_atomic_store_relaxed(&Handlers[0].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001397 for (i = 1; i < NSIG; i++) {
1398 void (*t)(int);
1399 t = PyOS_getsig(i);
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001400 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001401 if (t == SIG_DFL)
1402 Handlers[i].func = DefaultHandler;
1403 else if (t == SIG_IGN)
1404 Handlers[i].func = IgnoreHandler;
1405 else
1406 Handlers[i].func = Py_None; /* None of our business */
1407 Py_INCREF(Handlers[i].func);
1408 }
1409 if (Handlers[SIGINT].func == DefaultHandler) {
1410 /* Install default int handler */
1411 Py_INCREF(IntHandler);
Serhiy Storchaka57a01d32016-04-10 18:05:40 +03001412 Py_SETREF(Handlers[SIGINT].func, IntHandler);
pkerlinge905c842018-06-01 09:47:18 +00001413 PyOS_setsig(SIGINT, signal_handler);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001414 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001415
1416#ifdef SIGHUP
Christian Heimes6782b142016-09-09 00:11:45 +02001417 if (PyModule_AddIntMacro(m, SIGHUP))
1418 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001419#endif
1420#ifdef SIGINT
Christian Heimes6782b142016-09-09 00:11:45 +02001421 if (PyModule_AddIntMacro(m, SIGINT))
1422 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001423#endif
Tim Peters1ce3cf72001-10-01 17:58:40 +00001424#ifdef SIGBREAK
Christian Heimes6782b142016-09-09 00:11:45 +02001425 if (PyModule_AddIntMacro(m, SIGBREAK))
1426 goto finally;
Tim Peters1ce3cf72001-10-01 17:58:40 +00001427#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001428#ifdef SIGQUIT
Christian Heimes6782b142016-09-09 00:11:45 +02001429 if (PyModule_AddIntMacro(m, SIGQUIT))
1430 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001431#endif
1432#ifdef SIGILL
Christian Heimes6782b142016-09-09 00:11:45 +02001433 if (PyModule_AddIntMacro(m, SIGILL))
1434 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001435#endif
1436#ifdef SIGTRAP
Christian Heimes6782b142016-09-09 00:11:45 +02001437 if (PyModule_AddIntMacro(m, SIGTRAP))
1438 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001439#endif
1440#ifdef SIGIOT
Christian Heimes6782b142016-09-09 00:11:45 +02001441 if (PyModule_AddIntMacro(m, SIGIOT))
1442 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001443#endif
1444#ifdef SIGABRT
Christian Heimes6782b142016-09-09 00:11:45 +02001445 if (PyModule_AddIntMacro(m, SIGABRT))
1446 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001447#endif
1448#ifdef SIGEMT
Christian Heimes6782b142016-09-09 00:11:45 +02001449 if (PyModule_AddIntMacro(m, SIGEMT))
1450 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001451#endif
1452#ifdef SIGFPE
Christian Heimes6782b142016-09-09 00:11:45 +02001453 if (PyModule_AddIntMacro(m, SIGFPE))
1454 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001455#endif
1456#ifdef SIGKILL
Christian Heimes6782b142016-09-09 00:11:45 +02001457 if (PyModule_AddIntMacro(m, SIGKILL))
1458 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001459#endif
1460#ifdef SIGBUS
Christian Heimes6782b142016-09-09 00:11:45 +02001461 if (PyModule_AddIntMacro(m, SIGBUS))
1462 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001463#endif
1464#ifdef SIGSEGV
Christian Heimes6782b142016-09-09 00:11:45 +02001465 if (PyModule_AddIntMacro(m, SIGSEGV))
1466 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001467#endif
1468#ifdef SIGSYS
Christian Heimes6782b142016-09-09 00:11:45 +02001469 if (PyModule_AddIntMacro(m, SIGSYS))
1470 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001471#endif
1472#ifdef SIGPIPE
Christian Heimes6782b142016-09-09 00:11:45 +02001473 if (PyModule_AddIntMacro(m, SIGPIPE))
1474 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001475#endif
1476#ifdef SIGALRM
Christian Heimes6782b142016-09-09 00:11:45 +02001477 if (PyModule_AddIntMacro(m, SIGALRM))
1478 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001479#endif
1480#ifdef SIGTERM
Christian Heimes6782b142016-09-09 00:11:45 +02001481 if (PyModule_AddIntMacro(m, SIGTERM))
1482 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001483#endif
1484#ifdef SIGUSR1
Christian Heimes6782b142016-09-09 00:11:45 +02001485 if (PyModule_AddIntMacro(m, SIGUSR1))
1486 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001487#endif
1488#ifdef SIGUSR2
Christian Heimes6782b142016-09-09 00:11:45 +02001489 if (PyModule_AddIntMacro(m, SIGUSR2))
1490 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001491#endif
1492#ifdef SIGCLD
Christian Heimes6782b142016-09-09 00:11:45 +02001493 if (PyModule_AddIntMacro(m, SIGCLD))
1494 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001495#endif
1496#ifdef SIGCHLD
Christian Heimes6782b142016-09-09 00:11:45 +02001497 if (PyModule_AddIntMacro(m, SIGCHLD))
1498 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001499#endif
1500#ifdef SIGPWR
Christian Heimes6782b142016-09-09 00:11:45 +02001501 if (PyModule_AddIntMacro(m, SIGPWR))
1502 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001503#endif
1504#ifdef SIGIO
Christian Heimes6782b142016-09-09 00:11:45 +02001505 if (PyModule_AddIntMacro(m, SIGIO))
1506 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001507#endif
1508#ifdef SIGURG
Christian Heimes6782b142016-09-09 00:11:45 +02001509 if (PyModule_AddIntMacro(m, SIGURG))
1510 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001511#endif
1512#ifdef SIGWINCH
Christian Heimes6782b142016-09-09 00:11:45 +02001513 if (PyModule_AddIntMacro(m, SIGWINCH))
1514 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001515#endif
1516#ifdef SIGPOLL
Christian Heimes6782b142016-09-09 00:11:45 +02001517 if (PyModule_AddIntMacro(m, SIGPOLL))
1518 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001519#endif
1520#ifdef SIGSTOP
Christian Heimes6782b142016-09-09 00:11:45 +02001521 if (PyModule_AddIntMacro(m, SIGSTOP))
1522 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001523#endif
1524#ifdef SIGTSTP
Christian Heimes6782b142016-09-09 00:11:45 +02001525 if (PyModule_AddIntMacro(m, SIGTSTP))
1526 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001527#endif
1528#ifdef SIGCONT
Christian Heimes6782b142016-09-09 00:11:45 +02001529 if (PyModule_AddIntMacro(m, SIGCONT))
1530 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001531#endif
1532#ifdef SIGTTIN
Christian Heimes6782b142016-09-09 00:11:45 +02001533 if (PyModule_AddIntMacro(m, SIGTTIN))
1534 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001535#endif
1536#ifdef SIGTTOU
Christian Heimes6782b142016-09-09 00:11:45 +02001537 if (PyModule_AddIntMacro(m, SIGTTOU))
1538 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001539#endif
1540#ifdef SIGVTALRM
Christian Heimes6782b142016-09-09 00:11:45 +02001541 if (PyModule_AddIntMacro(m, SIGVTALRM))
1542 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001543#endif
1544#ifdef SIGPROF
Christian Heimes6782b142016-09-09 00:11:45 +02001545 if (PyModule_AddIntMacro(m, SIGPROF))
1546 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001547#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001548#ifdef SIGXCPU
Christian Heimes6782b142016-09-09 00:11:45 +02001549 if (PyModule_AddIntMacro(m, SIGXCPU))
1550 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001551#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001552#ifdef SIGXFSZ
Christian Heimes6782b142016-09-09 00:11:45 +02001553 if (PyModule_AddIntMacro(m, SIGXFSZ))
1554 goto finally;
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001555#endif
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001556#ifdef SIGRTMIN
Christian Heimes6782b142016-09-09 00:11:45 +02001557 if (PyModule_AddIntMacro(m, SIGRTMIN))
1558 goto finally;
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001559#endif
1560#ifdef SIGRTMAX
Christian Heimes6782b142016-09-09 00:11:45 +02001561 if (PyModule_AddIntMacro(m, SIGRTMAX))
1562 goto finally;
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001563#endif
Martin v. Löwis175af252002-01-12 11:43:25 +00001564#ifdef SIGINFO
Christian Heimes6782b142016-09-09 00:11:45 +02001565 if (PyModule_AddIntMacro(m, SIGINFO))
1566 goto finally;
Martin v. Löwis175af252002-01-12 11:43:25 +00001567#endif
Martin v. Löwis823725e2008-03-24 13:39:54 +00001568
1569#ifdef ITIMER_REAL
Christian Heimes6782b142016-09-09 00:11:45 +02001570 if (PyModule_AddIntMacro(m, ITIMER_REAL))
1571 goto finally;
Martin v. Löwis823725e2008-03-24 13:39:54 +00001572#endif
1573#ifdef ITIMER_VIRTUAL
Christian Heimes6782b142016-09-09 00:11:45 +02001574 if (PyModule_AddIntMacro(m, ITIMER_VIRTUAL))
1575 goto finally;
Martin v. Löwis823725e2008-03-24 13:39:54 +00001576#endif
1577#ifdef ITIMER_PROF
Christian Heimes6782b142016-09-09 00:11:45 +02001578 if (PyModule_AddIntMacro(m, ITIMER_PROF))
1579 goto finally;
Martin v. Löwis823725e2008-03-24 13:39:54 +00001580#endif
1581
1582#if defined (HAVE_SETITIMER) || defined (HAVE_GETITIMER)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001583 ItimerError = PyErr_NewException("signal.ItimerError",
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +03001584 PyExc_OSError, NULL);
Miss Islington (bot)b150d0b2019-09-09 07:42:35 -07001585 if (!ItimerError ||
1586 PyDict_SetItemString(d, "ItimerError", ItimerError) < 0) {
Joannah Nanjekye9541bd32019-04-21 21:47:06 -04001587 goto finally;
Miss Islington (bot)b150d0b2019-09-09 07:42:35 -07001588 }
Martin v. Löwis823725e2008-03-24 13:39:54 +00001589#endif
1590
Brian Curtineb24d742010-04-12 17:16:38 +00001591#ifdef CTRL_C_EVENT
Christian Heimes6782b142016-09-09 00:11:45 +02001592 if (PyModule_AddIntMacro(m, CTRL_C_EVENT))
1593 goto finally;
Brian Curtineb24d742010-04-12 17:16:38 +00001594#endif
1595
1596#ifdef CTRL_BREAK_EVENT
Christian Heimes6782b142016-09-09 00:11:45 +02001597 if (PyModule_AddIntMacro(m, CTRL_BREAK_EVENT))
1598 goto finally;
Brian Curtineb24d742010-04-12 17:16:38 +00001599#endif
1600
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001601#ifdef MS_WINDOWS
1602 /* Create manual-reset event, initially unset */
1603 sigint_event = CreateEvent(NULL, TRUE, FALSE, FALSE);
1604#endif
1605
Martin v. Löwis1a214512008-06-11 05:26:20 +00001606 if (PyErr_Occurred()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001607 Py_DECREF(m);
1608 m = NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001609 }
Barry Warsaw92971171997-01-03 00:14:25 +00001610
Barry Warsaw92971171997-01-03 00:14:25 +00001611 finally:
Martin v. Löwis1a214512008-06-11 05:26:20 +00001612 return m;
Guido van Rossum08c16611997-08-02 03:01:42 +00001613}
1614
1615static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001616finisignal(void)
Guido van Rossum08c16611997-08-02 03:01:42 +00001617{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001618 int i;
1619 PyObject *func;
Guido van Rossum08c16611997-08-02 03:01:42 +00001620
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001621 for (i = 1; i < NSIG; i++) {
1622 func = Handlers[i].func;
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001623 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001624 Handlers[i].func = NULL;
pkerlinge905c842018-06-01 09:47:18 +00001625 if (func != NULL && func != Py_None &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001626 func != DefaultHandler && func != IgnoreHandler)
1627 PyOS_setsig(i, SIG_DFL);
1628 Py_XDECREF(func);
1629 }
Guido van Rossum08c16611997-08-02 03:01:42 +00001630
Serhiy Storchaka505ff752014-02-09 13:33:53 +02001631 Py_CLEAR(IntHandler);
1632 Py_CLEAR(DefaultHandler);
1633 Py_CLEAR(IgnoreHandler);
Miss Islington (bot)b150d0b2019-09-09 07:42:35 -07001634#ifdef HAVE_GETITIMER
1635 Py_CLEAR(ItimerError);
1636#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001637}
1638
Barry Warsaw92971171997-01-03 00:14:25 +00001639
Barry Warsaw92971171997-01-03 00:14:25 +00001640/* Declared in pyerrors.h */
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001641int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001642PyErr_CheckSignals(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001643{
Victor Stinnerd8613dc2019-05-24 13:43:55 +02001644 _PyRuntimeState *runtime = &_PyRuntime;
1645 if (!is_main(runtime)) {
Eric Snow64d6cc82019-02-23 15:40:43 -07001646 return 0;
1647 }
1648
1649 return _PyErr_CheckSignals();
1650}
1651
1652
1653/* Declared in cpython/pyerrors.h */
1654int
1655_PyErr_CheckSignals(void)
1656{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001657 int i;
1658 PyObject *f;
Barry Warsaw92971171997-01-03 00:14:25 +00001659
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001660 if (!_Py_atomic_load(&is_tripped))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001661 return 0;
Christian Heimesb76922a2007-12-11 01:06:40 +00001662
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001663 /*
1664 * The is_tripped variable is meant to speed up the calls to
1665 * PyErr_CheckSignals (both directly or via pending calls) when no
1666 * signal has arrived. This variable is set to 1 when a signal arrives
1667 * and it is set to 0 here, when we know some signals arrived. This way
1668 * we can run the registered handlers with no signals blocked.
1669 *
1670 * NOTE: with this approach we can have a situation where is_tripped is
1671 * 1 but we have no more signals to handle (Handlers[i].tripped
1672 * is 0 for every signal i). This won't do us any harm (except
1673 * we're gonna spent some cycles for nothing). This happens when
1674 * we receive a signal i after we zero is_tripped and before we
1675 * check Handlers[i].tripped.
1676 */
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001677 _Py_atomic_store(&is_tripped, 0);
Christian Heimesb76922a2007-12-11 01:06:40 +00001678
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001679 if (!(f = (PyObject *)PyEval_GetFrame()))
1680 f = Py_None;
Christian Heimesb76922a2007-12-11 01:06:40 +00001681
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001682 for (i = 1; i < NSIG; i++) {
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001683 if (_Py_atomic_load_relaxed(&Handlers[i].tripped)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001684 PyObject *result = NULL;
1685 PyObject *arglist = Py_BuildValue("(iO)", i, f);
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001686 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Barry Warsaw92971171997-01-03 00:14:25 +00001687
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001688 if (arglist) {
1689 result = PyEval_CallObject(Handlers[i].func,
1690 arglist);
1691 Py_DECREF(arglist);
1692 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +02001693 if (!result) {
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001694 _Py_atomic_store(&is_tripped, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001695 return -1;
Antoine Pitrouc08177a2017-06-28 23:29:29 +02001696 }
Barry Warsaw92971171997-01-03 00:14:25 +00001697
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001698 Py_DECREF(result);
1699 }
1700 }
Christian Heimesb76922a2007-12-11 01:06:40 +00001701
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001702 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001703}
1704
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +00001705
Matěj Cepl608876b2019-05-23 22:30:00 +02001706/* Simulate the effect of a signal.SIGINT signal arriving. The next time
1707 PyErr_CheckSignals is called, the Python SIGINT signal handler will be
1708 raised.
1709
1710 Missing signal handler for the SIGINT signal is silently ignored. */
Barry Warsaw92971171997-01-03 00:14:25 +00001711void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001712PyErr_SetInterrupt(void)
Barry Warsaw92971171997-01-03 00:14:25 +00001713{
Matěj Cepl608876b2019-05-23 22:30:00 +02001714 if ((Handlers[SIGINT].func != IgnoreHandler) &&
1715 (Handlers[SIGINT].func != DefaultHandler)) {
1716 trip_signal(SIGINT);
1717 }
Barry Warsaw92971171997-01-03 00:14:25 +00001718}
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001719
1720void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001721PyOS_InitInterrupts(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001722{
Giampaolo Rodola'e09fb712014-04-04 15:34:17 +02001723 PyObject *m = PyImport_ImportModule("_signal");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001724 if (m) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001725 Py_DECREF(m);
1726 }
Guido van Rossum08c16611997-08-02 03:01:42 +00001727}
1728
1729void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001730PyOS_FiniInterrupts(void)
Guido van Rossum08c16611997-08-02 03:01:42 +00001731{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001732 finisignal();
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001733}
1734
Victor Stinner6f7346b2020-06-03 18:28:18 +02001735
1736// The caller doesn't have to hold the GIL
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001737int
Victor Stinner6f7346b2020-06-03 18:28:18 +02001738_PyOS_InterruptOccurred(PyThreadState *tstate)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001739{
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001740 if (_Py_atomic_load_relaxed(&Handlers[SIGINT].tripped)) {
Victor Stinnerd8613dc2019-05-24 13:43:55 +02001741 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinner6f7346b2020-06-03 18:28:18 +02001742 if (!is_main_interp(runtime, tstate->interp)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001743 return 0;
Eric Snow64d6cc82019-02-23 15:40:43 -07001744 }
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001745 _Py_atomic_store_relaxed(&Handlers[SIGINT].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001746 return 1;
1747 }
1748 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001749}
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001750
Victor Stinner6f7346b2020-06-03 18:28:18 +02001751
1752// The caller must to hold the GIL
1753int
1754PyOS_InterruptOccurred(void)
1755{
1756 PyThreadState *tstate = _PyThreadState_GET();
1757 return _PyOS_InterruptOccurred(tstate);
1758}
1759
1760
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001761static void
1762_clear_pending_signals(void)
1763{
1764 int i;
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001765 if (!_Py_atomic_load(&is_tripped))
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001766 return;
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001767 _Py_atomic_store(&is_tripped, 0);
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001768 for (i = 1; i < NSIG; ++i) {
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001769 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001770 }
1771}
1772
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001773void
Antoine Pitrou346cbd32017-05-27 17:50:54 +02001774_PySignal_AfterFork(void)
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001775{
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001776 /* Clear the signal flags after forking so that they aren't handled
1777 * in both processes if they came in just before the fork() but before
1778 * the interpreter had an opportunity to call the handlers. issue9535. */
1779 _clear_pending_signals();
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001780}
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001781
1782int
1783_PyOS_IsMainThread(void)
1784{
Victor Stinnerd8613dc2019-05-24 13:43:55 +02001785 _PyRuntimeState *runtime = &_PyRuntime;
1786 return is_main(runtime);
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001787}
1788
1789#ifdef MS_WINDOWS
1790void *_PyOS_SigintEvent(void)
1791{
1792 /* Returns a manual-reset event which gets tripped whenever
1793 SIGINT is received.
1794
1795 Python.h does not include windows.h so we do cannot use HANDLE
1796 as the return type of this function. We use void* instead. */
1797 return sigint_event;
1798}
1799#endif