blob: 1964646da2522deb86aa4708fd344c752f47ad1e [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
Eric Snow6a150bc2019-06-01 15:39:46 -060024#include "internal/pycore_pystate.h"
Guido van Rossum644a12b1997-04-09 19:24:53 +000025
Benjamin Peterson2614cda2010-03-21 22:36:19 +000026#ifdef HAVE_SIGNAL_H
Guido van Rossum398d9fe1994-05-11 08:59:13 +000027#include <signal.h>
Benjamin Peterson2614cda2010-03-21 22:36:19 +000028#endif
29#ifdef HAVE_SYS_STAT_H
Christian Heimes5fb7c2a2007-12-24 08:52:31 +000030#include <sys/stat.h>
Benjamin Peterson2614cda2010-03-21 22:36:19 +000031#endif
Martin v. Löwis3bf3cc02008-03-24 14:05:07 +000032#ifdef HAVE_SYS_TIME_H
Martin v. Löwis823725e2008-03-24 13:39:54 +000033#include <sys/time.h>
Martin v. Löwis3bf3cc02008-03-24 14:05:07 +000034#endif
Christian Heimes5fb7c2a2007-12-24 08:52:31 +000035
Victor Stinnera9293352011-04-30 15:21:58 +020036#if defined(HAVE_PTHREAD_SIGMASK) && !defined(HAVE_BROKEN_PTHREAD_SIGMASK)
37# define PYPTHREAD_SIGMASK
38#endif
39
40#if defined(PYPTHREAD_SIGMASK) && defined(HAVE_PTHREAD_H)
41# include <pthread.h>
42#endif
43
Guido van Rossumbb4ba121994-06-23 11:25:45 +000044#ifndef SIG_ERR
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +000045#define SIG_ERR ((PyOS_sighandler_t)(-1))
Guido van Rossumbb4ba121994-06-23 11:25:45 +000046#endif
47
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000048#ifndef NSIG
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000049# if defined(_NSIG)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000050# define NSIG _NSIG /* For BSD/SysV */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000051# elif defined(_SIGMAX)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000052# define NSIG (_SIGMAX + 1) /* For QNX */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000053# elif defined(SIGMAX)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000054# define NSIG (SIGMAX + 1) /* For djgpp */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000055# else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000056# define NSIG 64 /* Use a reasonable default value */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000057# endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000058#endif
59
Tal Einatc7027b72015-05-16 14:14:49 +030060#include "clinic/signalmodule.c.h"
61
62/*[clinic input]
63module signal
64[clinic start generated code]*/
65/*[clinic end generated code: output=da39a3ee5e6b4b0d input=b0301a3bde5fe9d3]*/
66
Serhiy Storchakad54cfb12018-05-08 07:48:50 +030067/*[python input]
68
69class sigset_t_converter(CConverter):
70 type = 'sigset_t'
71 converter = '_Py_Sigset_Converter'
72
73[python start generated code]*/
74/*[python end generated code: output=da39a3ee5e6b4b0d input=b5689d14466b6823]*/
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000075
Guido van Rossumbb4ba121994-06-23 11:25:45 +000076/*
77 NOTES ON THE INTERACTION BETWEEN SIGNALS AND THREADS
78
Jeroen Demeyerd237b3f2019-05-10 03:28:57 +020079 We want the following semantics:
Guido van Rossumbb4ba121994-06-23 11:25:45 +000080
81 - only the main thread can set a signal handler
Jeroen Demeyerd237b3f2019-05-10 03:28:57 +020082 - only the main thread runs the signal handler
83 - signals can be delivered to any thread
Guido van Rossumbb4ba121994-06-23 11:25:45 +000084 - any thread can get a signal handler
Guido van Rossumbb4ba121994-06-23 11:25:45 +000085
86 I.e. we don't support "synchronous signals" like SIGFPE (catching
87 this doesn't make much sense in Python anyway) nor do we support
88 signals as a means of inter-thread communication, since not all
89 thread implementations support that (at least our thread library
90 doesn't).
91
92 We still have the problem that in some implementations signals
93 generated by the keyboard (e.g. SIGINT) are delivered to all
94 threads (e.g. SGI), while in others (e.g. Solaris) such signals are
Jeroen Demeyerd237b3f2019-05-10 03:28:57 +020095 delivered to one random thread. On Linux, signals are delivered to
96 the main thread (unless the main thread is blocking the signal, for
97 example because it's already handling the same signal). Since we
98 allow signals to be delivered to any thread, this works fine. The
99 only oddity is that the thread executing the Python signal handler
100 may not be the thread that received the signal.
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000101*/
102
Guido van Rossum49b56061998-10-01 20:42:43 +0000103#include "pythread.h"
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000104
Victor Stinner2ec6b172011-05-15 10:21:59 +0200105static volatile struct {
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +0200106 _Py_atomic_int tripped;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000107 PyObject *func;
Barry Warsaw92971171997-01-03 00:14:25 +0000108} Handlers[NSIG];
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000109
Victor Stinner11517102014-07-29 23:31:34 +0200110#ifdef MS_WINDOWS
111#define INVALID_FD ((SOCKET_T)-1)
112
113static volatile struct {
114 SOCKET_T fd;
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800115 int warn_on_full_buffer;
Victor Stinner11517102014-07-29 23:31:34 +0200116 int use_send;
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800117} wakeup = {.fd = INVALID_FD, .warn_on_full_buffer = 1, .use_send = 0};
Victor Stinner11517102014-07-29 23:31:34 +0200118#else
119#define INVALID_FD (-1)
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800120static volatile struct {
121 sig_atomic_t fd;
122 int warn_on_full_buffer;
123} wakeup = {.fd = INVALID_FD, .warn_on_full_buffer = 1};
Victor Stinner11517102014-07-29 23:31:34 +0200124#endif
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000125
Christian Heimesb76922a2007-12-11 01:06:40 +0000126/* Speed up sigcheck() when none tripped */
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +0200127static _Py_atomic_int is_tripped;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000128
Barry Warsaw92971171997-01-03 00:14:25 +0000129static PyObject *DefaultHandler;
130static PyObject *IgnoreHandler;
131static PyObject *IntHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000132
Antoine Pitrou6dd381e2011-11-21 21:26:56 +0100133#ifdef MS_WINDOWS
134static HANDLE sigint_event = NULL;
135#endif
136
Martin v. Löwis823725e2008-03-24 13:39:54 +0000137#ifdef HAVE_GETITIMER
138static PyObject *ItimerError;
139
Victor Stinneref611c92017-10-13 13:49:43 -0700140/* auxiliary functions for setitimer */
141static int
142timeval_from_double(PyObject *obj, struct timeval *tv)
Martin v. Löwis823725e2008-03-24 13:39:54 +0000143{
Victor Stinneref611c92017-10-13 13:49:43 -0700144 if (obj == NULL) {
145 tv->tv_sec = 0;
146 tv->tv_usec = 0;
147 return 0;
Antoine Pitrou729780a2017-06-30 10:01:05 +0200148 }
Victor Stinneref611c92017-10-13 13:49:43 -0700149
150 _PyTime_t t;
151 if (_PyTime_FromSecondsObject(&t, obj, _PyTime_ROUND_CEILING) < 0) {
152 return -1;
153 }
154 return _PyTime_AsTimeval(t, tv, _PyTime_ROUND_CEILING);
Martin v. Löwis823725e2008-03-24 13:39:54 +0000155}
156
Christian Heimes1a8501c2008-10-02 19:56:01 +0000157Py_LOCAL_INLINE(double)
Martin v. Löwis823725e2008-03-24 13:39:54 +0000158double_from_timeval(struct timeval *tv)
159{
160 return tv->tv_sec + (double)(tv->tv_usec / 1000000.0);
161}
162
163static PyObject *
164itimer_retval(struct itimerval *iv)
165{
166 PyObject *r, *v;
167
168 r = PyTuple_New(2);
169 if (r == NULL)
Martin Panter6d57fe12016-09-17 03:26:16 +0000170 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000171
172 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_value)))) {
Martin Panter6d57fe12016-09-17 03:26:16 +0000173 Py_DECREF(r);
174 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000175 }
176
177 PyTuple_SET_ITEM(r, 0, v);
178
179 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_interval)))) {
Martin Panter6d57fe12016-09-17 03:26:16 +0000180 Py_DECREF(r);
181 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000182 }
183
184 PyTuple_SET_ITEM(r, 1, v);
185
186 return r;
187}
188#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000189
Eric Snow64d6cc82019-02-23 15:40:43 -0700190static int
Victor Stinnerd8613dc2019-05-24 13:43:55 +0200191is_main(_PyRuntimeState *runtime)
Eric Snow64d6cc82019-02-23 15:40:43 -0700192{
Victor Stinnerd8613dc2019-05-24 13:43:55 +0200193 unsigned long thread = PyThread_get_thread_ident();
194 PyInterpreterState *interp = _PyRuntimeState_GetThreadState(runtime)->interp;
195 return (thread == runtime->main_thread
196 && interp == runtime->interpreters.main);
Eric Snow64d6cc82019-02-23 15:40:43 -0700197}
198
Guido van Rossume4485b01994-09-07 14:32:49 +0000199static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000200signal_default_int_handler(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000201{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000202 PyErr_SetNone(PyExc_KeyboardInterrupt);
203 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000204}
205
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000206PyDoc_STRVAR(default_int_handler_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000207"default_int_handler(...)\n\
208\n\
Michael W. Hudson24ec2112004-06-17 15:55:53 +0000209The default handler for SIGINT installed by Python.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000210It raises KeyboardInterrupt.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000211
Thomas Wouters0796b002000-07-22 23:49:30 +0000212
213static int
Victor Stinner11517102014-07-29 23:31:34 +0200214report_wakeup_write_error(void *data)
Antoine Pitrou6f6ec372013-08-17 20:27:56 +0200215{
Eric Snowfdf282d2019-01-11 14:26:55 -0700216 PyObject *exc, *val, *tb;
Antoine Pitrou6f6ec372013-08-17 20:27:56 +0200217 int save_errno = errno;
Benjamin Petersonca470632016-09-06 13:47:26 -0700218 errno = (int) (intptr_t) data;
Eric Snowfdf282d2019-01-11 14:26:55 -0700219 PyErr_Fetch(&exc, &val, &tb);
Antoine Pitrou6f6ec372013-08-17 20:27:56 +0200220 PyErr_SetFromErrno(PyExc_OSError);
221 PySys_WriteStderr("Exception ignored when trying to write to the "
222 "signal wakeup fd:\n");
223 PyErr_WriteUnraisable(NULL);
Eric Snowfdf282d2019-01-11 14:26:55 -0700224 PyErr_Restore(exc, val, tb);
Antoine Pitrou6f6ec372013-08-17 20:27:56 +0200225 errno = save_errno;
226 return 0;
227}
228
Victor Stinner11517102014-07-29 23:31:34 +0200229#ifdef MS_WINDOWS
230static int
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800231report_wakeup_send_error(void* data)
Victor Stinner11517102014-07-29 23:31:34 +0200232{
Eric Snowfdf282d2019-01-11 14:26:55 -0700233 PyObject *exc, *val, *tb;
234 PyErr_Fetch(&exc, &val, &tb);
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800235 /* PyErr_SetExcFromWindowsErr() invokes FormatMessage() which
236 recognizes the error codes used by both GetLastError() and
237 WSAGetLastError */
238 PyErr_SetExcFromWindowsErr(PyExc_OSError, (int) (intptr_t) data);
Victor Stinner11517102014-07-29 23:31:34 +0200239 PySys_WriteStderr("Exception ignored when trying to send to the "
240 "signal wakeup fd:\n");
241 PyErr_WriteUnraisable(NULL);
Eric Snowfdf282d2019-01-11 14:26:55 -0700242 PyErr_Restore(exc, val, tb);
Victor Stinner11517102014-07-29 23:31:34 +0200243 return 0;
244}
245#endif /* MS_WINDOWS */
246
Tim Peters4f1b2082000-07-23 21:18:09 +0000247static void
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200248trip_signal(int sig_num)
249{
Victor Stinnerd49b1f12011-05-08 02:03:15 +0200250 unsigned char byte;
Victor Stinner11517102014-07-29 23:31:34 +0200251 int fd;
252 Py_ssize_t rc;
Victor Stinnerc13ef662011-05-25 02:35:58 +0200253
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +0200254 _Py_atomic_store_relaxed(&Handlers[sig_num].tripped, 1);
Victor Stinner11517102014-07-29 23:31:34 +0200255
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200256 /* Set is_tripped after setting .tripped, as it gets
257 cleared in PyErr_CheckSignals() before .tripped. */
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +0200258 _Py_atomic_store(&is_tripped, 1);
259
260 /* Notify ceval.c */
Victor Stinner09532fe2019-05-10 23:39:09 +0200261 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinner438a12d2019-05-24 17:01:38 +0200262 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Eric Snow6a150bc2019-06-01 15:39:46 -0600263 PyInterpreterState *interp = runtime->interpreters.main;
Victor Stinner09532fe2019-05-10 23:39:09 +0200264 _PyEval_SignalReceived(&runtime->ceval);
Nathaniel J. Smith4ae01492017-05-16 14:12:11 -0700265
266 /* And then write to the wakeup fd *after* setting all the globals and
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200267 doing the _PyEval_SignalReceived. We used to write to the wakeup fd
268 and then set the flag, but this allowed the following sequence of events
269 (especially on windows, where trip_signal may run in a new thread):
Nathaniel J. Smith4ae01492017-05-16 14:12:11 -0700270
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800271 - main thread blocks on select([wakeup.fd], ...)
Nathaniel J. Smith4ae01492017-05-16 14:12:11 -0700272 - signal arrives
273 - trip_signal writes to the wakeup fd
274 - the main thread wakes up
275 - the main thread checks the signal flags, sees that they're unset
276 - the main thread empties the wakeup fd
277 - the main thread goes back to sleep
278 - trip_signal sets the flags to request the Python-level signal handler
279 be run
280 - the main thread doesn't notice, because it's asleep
281
282 See bpo-30038 for more details.
283 */
284
Victor Stinner11517102014-07-29 23:31:34 +0200285#ifdef MS_WINDOWS
286 fd = Py_SAFE_DOWNCAST(wakeup.fd, SOCKET_T, int);
287#else
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800288 fd = wakeup.fd;
Victor Stinner11517102014-07-29 23:31:34 +0200289#endif
290
291 if (fd != INVALID_FD) {
Victor Stinnerc13ef662011-05-25 02:35:58 +0200292 byte = (unsigned char)sig_num;
Victor Stinner11517102014-07-29 23:31:34 +0200293#ifdef MS_WINDOWS
294 if (wakeup.use_send) {
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800295 rc = send(fd, &byte, 1, 0);
Victor Stinner11517102014-07-29 23:31:34 +0200296
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800297 if (rc < 0) {
298 int last_error = GetLastError();
299 if (wakeup.warn_on_full_buffer ||
300 last_error != WSAEWOULDBLOCK)
301 {
302 /* Py_AddPendingCall() isn't signal-safe, but we
303 still use it for this exceptional case. */
Eric Snow6a150bc2019-06-01 15:39:46 -0600304 _PyEval_AddPendingCall(tstate,
305 &runtime->ceval,
306 &interp->ceval,
307 runtime->main_thread,
Victor Stinner09532fe2019-05-10 23:39:09 +0200308 report_wakeup_send_error,
309 (void *)(intptr_t) last_error);
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800310 }
Victor Stinner11517102014-07-29 23:31:34 +0200311 }
312 }
313 else
314#endif
315 {
Victor Stinnere72fe392015-04-01 18:35:22 +0200316 /* _Py_write_noraise() retries write() if write() is interrupted by
317 a signal (fails with EINTR). */
318 rc = _Py_write_noraise(fd, &byte, 1);
Victor Stinner11517102014-07-29 23:31:34 +0200319
320 if (rc < 0) {
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800321 if (wakeup.warn_on_full_buffer ||
322 (errno != EWOULDBLOCK && errno != EAGAIN))
323 {
324 /* Py_AddPendingCall() isn't signal-safe, but we
325 still use it for this exceptional case. */
Eric Snow6a150bc2019-06-01 15:39:46 -0600326 _PyEval_AddPendingCall(tstate,
327 &runtime->ceval,
328 &interp->ceval,
329 runtime->main_thread,
Victor Stinner09532fe2019-05-10 23:39:09 +0200330 report_wakeup_write_error,
331 (void *)(intptr_t)errno);
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800332 }
Victor Stinner11517102014-07-29 23:31:34 +0200333 }
334 }
Victor Stinnerc13ef662011-05-25 02:35:58 +0200335 }
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200336}
337
338static void
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000339signal_handler(int sig_num)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000340{
Antoine Pitrou39a65912010-11-05 19:47:27 +0000341 int save_errno = errno;
342
Jeroen Demeyerd237b3f2019-05-10 03:28:57 +0200343 trip_signal(sig_num);
Antoine Pitrou39a65912010-11-05 19:47:27 +0000344
Jean-Paul Calderone6f137ca2010-05-09 03:18:57 +0000345#ifndef HAVE_SIGACTION
Antoine Pitrou39a65912010-11-05 19:47:27 +0000346#ifdef SIGCHLD
347 /* To avoid infinite recursion, this signal remains
348 reset until explicit re-instated.
349 Don't clear the 'func' field as it is our pointer
350 to the Python handler... */
351 if (sig_num != SIGCHLD)
352#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000353 /* If the handler was not set up with sigaction, reinstall it. See
Nick Coghland6009512014-11-20 21:39:37 +1000354 * Python/pylifecycle.c for the implementation of PyOS_setsig which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000355 * makes this true. See also issue8354. */
356 PyOS_setsig(sig_num, signal_handler);
Jean-Paul Calderone6f137ca2010-05-09 03:18:57 +0000357#endif
Antoine Pitrou39a65912010-11-05 19:47:27 +0000358
359 /* Issue #10311: asynchronously executing signal handlers should not
360 mutate errno under the feet of unsuspecting C code. */
361 errno = save_errno;
Antoine Pitrou6dd381e2011-11-21 21:26:56 +0100362
363#ifdef MS_WINDOWS
364 if (sig_num == SIGINT)
365 SetEvent(sigint_event);
366#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000367}
Guido van Rossume4485b01994-09-07 14:32:49 +0000368
Guido van Rossum06d511d1995-03-10 15:13:48 +0000369
Guido van Rossum1171ee61997-08-22 20:42:00 +0000370#ifdef HAVE_ALARM
Tal Einatc7027b72015-05-16 14:14:49 +0300371
372/*[clinic input]
373signal.alarm -> long
374
375 seconds: int
376 /
377
378Arrange for SIGALRM to arrive after the given number of seconds.
379[clinic start generated code]*/
380
381static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300382signal_alarm_impl(PyObject *module, int seconds)
383/*[clinic end generated code: output=144232290814c298 input=0d5e97e0e6f39e86]*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000384{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000385 /* alarm() returns the number of seconds remaining */
Tal Einatc7027b72015-05-16 14:14:49 +0300386 return (long)alarm(seconds);
Guido van Rossumaa0f4c71994-08-23 13:49:37 +0000387}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000388
Guido van Rossum06d511d1995-03-10 15:13:48 +0000389#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000390
Guido van Rossum1171ee61997-08-22 20:42:00 +0000391#ifdef HAVE_PAUSE
Tal Einatc7027b72015-05-16 14:14:49 +0300392
393/*[clinic input]
394signal.pause
395
396Wait until a signal arrives.
397[clinic start generated code]*/
398
Guido van Rossuma597dde1995-01-10 20:56:29 +0000399static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300400signal_pause_impl(PyObject *module)
401/*[clinic end generated code: output=391656788b3c3929 input=f03de0f875752062]*/
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000402{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000403 Py_BEGIN_ALLOW_THREADS
404 (void)pause();
405 Py_END_ALLOW_THREADS
406 /* make sure that any exceptions that got raised are propagated
407 * back into Python
408 */
409 if (PyErr_CheckSignals())
410 return NULL;
Barry Warsaw92971171997-01-03 00:14:25 +0000411
Tal Einatc7027b72015-05-16 14:14:49 +0300412 Py_RETURN_NONE;
Guido van Rossume4485b01994-09-07 14:32:49 +0000413}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000414
Guido van Rossum06d511d1995-03-10 15:13:48 +0000415#endif
Guido van Rossume4485b01994-09-07 14:32:49 +0000416
Vladimir Matveevc24c6c22019-01-08 01:58:25 -0800417/*[clinic input]
418signal.raise_signal
419
420 signalnum: int
421 /
422
423Send a signal to the executing process.
424[clinic start generated code]*/
425
426static PyObject *
427signal_raise_signal_impl(PyObject *module, int signalnum)
428/*[clinic end generated code: output=e2b014220aa6111d input=e90c0f9a42358de6]*/
429{
430 int err;
431 Py_BEGIN_ALLOW_THREADS
432 _Py_BEGIN_SUPPRESS_IPH
433 err = raise(signalnum);
434 _Py_END_SUPPRESS_IPH
435 Py_END_ALLOW_THREADS
Victor Stinner09532fe2019-05-10 23:39:09 +0200436
Vladimir Matveevc24c6c22019-01-08 01:58:25 -0800437 if (err) {
438 return PyErr_SetFromErrno(PyExc_OSError);
439 }
440 Py_RETURN_NONE;
441}
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000442
Tal Einatc7027b72015-05-16 14:14:49 +0300443/*[clinic input]
444signal.signal
445
446 signalnum: int
447 handler: object
448 /
449
450Set the action for the given signal.
451
452The action can be SIG_DFL, SIG_IGN, or a callable Python object.
453The previous action is returned. See getsignal() for possible return values.
454
455*** IMPORTANT NOTICE ***
456A signal handler function is called with two arguments:
457the first is the signal number, the second is the interrupted stack frame.
458[clinic start generated code]*/
459
Guido van Rossume4485b01994-09-07 14:32:49 +0000460static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300461signal_signal_impl(PyObject *module, int signalnum, PyObject *handler)
462/*[clinic end generated code: output=b44cfda43780f3a1 input=deee84af5fa0432c]*/
Guido van Rossume4485b01994-09-07 14:32:49 +0000463{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000464 PyObject *old_handler;
465 void (*func)(int);
Brian Curtinef9efbd2010-08-06 19:27:32 +0000466#ifdef MS_WINDOWS
Tal Einatc7027b72015-05-16 14:14:49 +0300467 /* Validate that signalnum is one of the allowable signals */
468 switch (signalnum) {
Brian Curtinc734b312010-09-06 16:04:10 +0000469 case SIGABRT: break;
Brian Curtin9e88b5a2010-10-01 14:49:24 +0000470#ifdef SIGBREAK
471 /* Issue #10003: SIGBREAK is not documented as permitted, but works
472 and corresponds to CTRL_BREAK_EVENT. */
473 case SIGBREAK: break;
474#endif
Brian Curtinc734b312010-09-06 16:04:10 +0000475 case SIGFPE: break;
476 case SIGILL: break;
477 case SIGINT: break;
478 case SIGSEGV: break;
479 case SIGTERM: break;
480 default:
481 PyErr_SetString(PyExc_ValueError, "invalid signal value");
482 return NULL;
Brian Curtinef9efbd2010-08-06 19:27:32 +0000483 }
484#endif
Victor Stinnerd8613dc2019-05-24 13:43:55 +0200485
486 _PyRuntimeState *runtime = &_PyRuntime;
487 if (!is_main(runtime)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000488 PyErr_SetString(PyExc_ValueError,
489 "signal only works in main thread");
490 return NULL;
491 }
Tal Einatc7027b72015-05-16 14:14:49 +0300492 if (signalnum < 1 || signalnum >= NSIG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000493 PyErr_SetString(PyExc_ValueError,
494 "signal number out of range");
495 return NULL;
496 }
Tal Einatc7027b72015-05-16 14:14:49 +0300497 if (handler == IgnoreHandler)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000498 func = SIG_IGN;
Tal Einatc7027b72015-05-16 14:14:49 +0300499 else if (handler == DefaultHandler)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000500 func = SIG_DFL;
Tal Einatc7027b72015-05-16 14:14:49 +0300501 else if (!PyCallable_Check(handler)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000502 PyErr_SetString(PyExc_TypeError,
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000503"signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000504 return NULL;
505 }
506 else
507 func = signal_handler;
Antoine Pitrouf6f90ff2017-11-03 19:58:46 +0100508 /* Check for pending signals before changing signal handler */
Eric Snow64d6cc82019-02-23 15:40:43 -0700509 if (_PyErr_CheckSignals()) {
Antoine Pitrouf6f90ff2017-11-03 19:58:46 +0100510 return NULL;
511 }
Tal Einatc7027b72015-05-16 14:14:49 +0300512 if (PyOS_setsig(signalnum, func) == SIG_ERR) {
Victor Stinner388196e2011-05-10 17:13:00 +0200513 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000514 return NULL;
515 }
Tal Einatc7027b72015-05-16 14:14:49 +0300516 old_handler = Handlers[signalnum].func;
Tal Einatc7027b72015-05-16 14:14:49 +0300517 Py_INCREF(handler);
518 Handlers[signalnum].func = handler;
Antoine Pitrouc8c952c2013-05-04 23:16:59 +0200519 if (old_handler != NULL)
520 return old_handler;
521 else
522 Py_RETURN_NONE;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000523}
524
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000525
Tal Einatc7027b72015-05-16 14:14:49 +0300526/*[clinic input]
527signal.getsignal
528
529 signalnum: int
530 /
531
532Return the current action for the given signal.
533
534The return value can be:
535 SIG_IGN -- if the signal is being ignored
536 SIG_DFL -- if the default action for the signal is in effect
537 None -- if an unknown handler is in effect
538 anything else -- the callable Python object used as a handler
539[clinic start generated code]*/
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000540
Guido van Rossume4485b01994-09-07 14:32:49 +0000541static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300542signal_getsignal_impl(PyObject *module, int signalnum)
543/*[clinic end generated code: output=35b3e0e796fd555e input=ac23a00f19dfa509]*/
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000544{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000545 PyObject *old_handler;
Tal Einatc7027b72015-05-16 14:14:49 +0300546 if (signalnum < 1 || signalnum >= NSIG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000547 PyErr_SetString(PyExc_ValueError,
548 "signal number out of range");
549 return NULL;
550 }
Tal Einatc7027b72015-05-16 14:14:49 +0300551 old_handler = Handlers[signalnum].func;
Antoine Pitrouc8c952c2013-05-04 23:16:59 +0200552 if (old_handler != NULL) {
553 Py_INCREF(old_handler);
554 return old_handler;
555 }
556 else {
557 Py_RETURN_NONE;
558 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000559}
560
Antoine Pietri5d2a27d2018-03-12 14:42:34 +0100561
562/*[clinic input]
563signal.strsignal
564
565 signalnum: int
566 /
567
568Return the system description of the given signal.
569
570The return values can be such as "Interrupt", "Segmentation fault", etc.
571Returns None if the signal is not recognized.
572[clinic start generated code]*/
573
574static PyObject *
575signal_strsignal_impl(PyObject *module, int signalnum)
576/*[clinic end generated code: output=44e12e1e3b666261 input=b77914b03f856c74]*/
577{
578 char *res;
579
580 if (signalnum < 1 || signalnum >= NSIG) {
581 PyErr_SetString(PyExc_ValueError,
582 "signal number out of range");
583 return NULL;
584 }
585
Michael Osipov48ce4892018-08-23 15:27:19 +0200586#ifndef HAVE_STRSIGNAL
Antoine Pietri5d2a27d2018-03-12 14:42:34 +0100587 switch (signalnum) {
Michael Osipov48ce4892018-08-23 15:27:19 +0200588 /* Though being a UNIX, HP-UX does not provide strsignal(3). */
589#ifndef MS_WINDOWS
590 case SIGHUP:
591 res = "Hangup";
592 break;
593 case SIGALRM:
594 res = "Alarm clock";
595 break;
596 case SIGPIPE:
597 res = "Broken pipe";
598 break;
599 case SIGQUIT:
600 res = "Quit";
601 break;
602 case SIGCHLD:
603 res = "Child exited";
604 break;
605#endif
606 /* Custom redefinition of POSIX signals allowed on Windows. */
Antoine Pietri5d2a27d2018-03-12 14:42:34 +0100607 case SIGINT:
608 res = "Interrupt";
609 break;
610 case SIGILL:
611 res = "Illegal instruction";
612 break;
613 case SIGABRT:
614 res = "Aborted";
615 break;
616 case SIGFPE:
617 res = "Floating point exception";
618 break;
619 case SIGSEGV:
620 res = "Segmentation fault";
621 break;
622 case SIGTERM:
623 res = "Terminated";
624 break;
625 default:
626 Py_RETURN_NONE;
627 }
628#else
629 errno = 0;
630 res = strsignal(signalnum);
631
632 if (errno || res == NULL || strstr(res, "Unknown signal") != NULL)
633 Py_RETURN_NONE;
634#endif
635
636 return Py_BuildValue("s", res);
637}
638
Christian Heimes8640e742008-02-23 16:23:06 +0000639#ifdef HAVE_SIGINTERRUPT
Tal Einatc7027b72015-05-16 14:14:49 +0300640
641/*[clinic input]
642signal.siginterrupt
643
644 signalnum: int
645 flag: int
646 /
647
648Change system call restart behaviour.
649
650If flag is False, system calls will be restarted when interrupted by
651signal sig, else system calls will be interrupted.
652[clinic start generated code]*/
Christian Heimes8640e742008-02-23 16:23:06 +0000653
654static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300655signal_siginterrupt_impl(PyObject *module, int signalnum, int flag)
656/*[clinic end generated code: output=063816243d85dd19 input=4160acacca3e2099]*/
Christian Heimes8640e742008-02-23 16:23:06 +0000657{
Tal Einatc7027b72015-05-16 14:14:49 +0300658 if (signalnum < 1 || signalnum >= NSIG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000659 PyErr_SetString(PyExc_ValueError,
660 "signal number out of range");
661 return NULL;
662 }
Tal Einatc7027b72015-05-16 14:14:49 +0300663 if (siginterrupt(signalnum, flag)<0) {
Victor Stinner388196e2011-05-10 17:13:00 +0200664 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000665 return NULL;
666 }
Tal Einatc7027b72015-05-16 14:14:49 +0300667 Py_RETURN_NONE;
Christian Heimes8640e742008-02-23 16:23:06 +0000668}
669
670#endif
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000671
Tal Einatc7027b72015-05-16 14:14:49 +0300672
673static PyObject*
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800674signal_set_wakeup_fd(PyObject *self, PyObject *args, PyObject *kwds)
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000675{
Victor Stinnere134a7f2015-03-30 10:09:31 +0200676 struct _Py_stat_struct status;
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800677 static char *kwlist[] = {
678 "", "warn_on_full_buffer", NULL,
679 };
680 int warn_on_full_buffer = 1;
Victor Stinner11517102014-07-29 23:31:34 +0200681#ifdef MS_WINDOWS
682 PyObject *fdobj;
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100683 SOCKET_T sockfd, old_sockfd;
Victor Stinner11517102014-07-29 23:31:34 +0200684 int res;
685 int res_size = sizeof res;
686 PyObject *mod;
Victor Stinner11517102014-07-29 23:31:34 +0200687 int is_socket;
688
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800689 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|$p:set_wakeup_fd", kwlist,
690 &fdobj, &warn_on_full_buffer))
Victor Stinner11517102014-07-29 23:31:34 +0200691 return NULL;
692
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100693 sockfd = PyLong_AsSocket_t(fdobj);
694 if (sockfd == (SOCKET_T)(-1) && PyErr_Occurred())
Victor Stinner11517102014-07-29 23:31:34 +0200695 return NULL;
696#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000697 int fd, old_fd;
Victor Stinner11517102014-07-29 23:31:34 +0200698
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800699 if (!PyArg_ParseTupleAndKeywords(args, kwds, "i|$p:set_wakeup_fd", kwlist,
700 &fd, &warn_on_full_buffer))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000701 return NULL;
Victor Stinner11517102014-07-29 23:31:34 +0200702#endif
703
Victor Stinnerd8613dc2019-05-24 13:43:55 +0200704 _PyRuntimeState *runtime = &_PyRuntime;
705 if (!is_main(runtime)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000706 PyErr_SetString(PyExc_ValueError,
707 "set_wakeup_fd only works in main thread");
708 return NULL;
709 }
Victor Stinner0bffc942014-07-21 16:28:54 +0200710
Victor Stinner11517102014-07-29 23:31:34 +0200711#ifdef MS_WINDOWS
712 is_socket = 0;
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100713 if (sockfd != INVALID_FD) {
Victor Stinner11517102014-07-29 23:31:34 +0200714 /* Import the _socket module to call WSAStartup() */
715 mod = PyImport_ImportModuleNoBlock("_socket");
716 if (mod == NULL)
717 return NULL;
718 Py_DECREF(mod);
719
720 /* test the socket */
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100721 if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR,
Victor Stinner11517102014-07-29 23:31:34 +0200722 (char *)&res, &res_size) != 0) {
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100723 int fd, err;
724
725 err = WSAGetLastError();
Victor Stinner11517102014-07-29 23:31:34 +0200726 if (err != WSAENOTSOCK) {
727 PyErr_SetExcFromWindowsErr(PyExc_OSError, err);
728 return NULL;
729 }
730
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100731 fd = (int)sockfd;
Steve Dower940f33a2016-09-08 11:21:54 -0700732 if ((SOCKET_T)fd != sockfd) {
Victor Stinner11517102014-07-29 23:31:34 +0200733 PyErr_SetString(PyExc_ValueError, "invalid fd");
734 return NULL;
735 }
736
Victor Stinnere134a7f2015-03-30 10:09:31 +0200737 if (_Py_fstat(fd, &status) != 0)
Victor Stinner11517102014-07-29 23:31:34 +0200738 return NULL;
Victor Stinner38227602014-08-27 12:59:44 +0200739
740 /* on Windows, a file cannot be set to non-blocking mode */
Victor Stinner11517102014-07-29 23:31:34 +0200741 }
Victor Stinner38227602014-08-27 12:59:44 +0200742 else {
Victor Stinner11517102014-07-29 23:31:34 +0200743 is_socket = 1;
Victor Stinner38227602014-08-27 12:59:44 +0200744
745 /* Windows does not provide a function to test if a socket
746 is in non-blocking mode */
747 }
Victor Stinner11517102014-07-29 23:31:34 +0200748 }
749
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100750 old_sockfd = wakeup.fd;
751 wakeup.fd = sockfd;
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800752 wakeup.warn_on_full_buffer = warn_on_full_buffer;
Victor Stinner11517102014-07-29 23:31:34 +0200753 wakeup.use_send = is_socket;
754
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100755 if (old_sockfd != INVALID_FD)
756 return PyLong_FromSocket_t(old_sockfd);
Victor Stinner11517102014-07-29 23:31:34 +0200757 else
758 return PyLong_FromLong(-1);
759#else
Victor Stinnerd18ccd12014-07-24 21:58:53 +0200760 if (fd != -1) {
Victor Stinner38227602014-08-27 12:59:44 +0200761 int blocking;
762
Victor Stinnere134a7f2015-03-30 10:09:31 +0200763 if (_Py_fstat(fd, &status) != 0)
Victor Stinner11517102014-07-29 23:31:34 +0200764 return NULL;
Victor Stinner38227602014-08-27 12:59:44 +0200765
766 blocking = _Py_get_blocking(fd);
767 if (blocking < 0)
768 return NULL;
769 if (blocking) {
770 PyErr_Format(PyExc_ValueError,
771 "the fd %i must be in non-blocking mode",
772 fd);
773 return NULL;
774 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000775 }
Victor Stinner0bffc942014-07-21 16:28:54 +0200776
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800777 old_fd = wakeup.fd;
778 wakeup.fd = fd;
779 wakeup.warn_on_full_buffer = warn_on_full_buffer;
Victor Stinner0bffc942014-07-21 16:28:54 +0200780
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000781 return PyLong_FromLong(old_fd);
Victor Stinner11517102014-07-29 23:31:34 +0200782#endif
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000783}
784
785PyDoc_STRVAR(set_wakeup_fd_doc,
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800786"set_wakeup_fd(fd, *, warn_on_full_buffer=True) -> fd\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000787\n\
Victor Stinner11517102014-07-29 23:31:34 +0200788Sets the fd to be written to (with the signal number) when a signal\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000789comes in. A library can use this to wakeup select or poll.\n\
Victor Stinner11517102014-07-29 23:31:34 +0200790The previous fd or -1 is returned.\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000791\n\
792The fd must be non-blocking.");
793
794/* C API for the same, without all the error checking */
795int
796PySignal_SetWakeupFd(int fd)
797{
Victor Stinner11517102014-07-29 23:31:34 +0200798 int old_fd;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000799 if (fd < 0)
800 fd = -1;
Victor Stinner11517102014-07-29 23:31:34 +0200801
802#ifdef MS_WINDOWS
803 old_fd = Py_SAFE_DOWNCAST(wakeup.fd, SOCKET_T, int);
Victor Stinner11517102014-07-29 23:31:34 +0200804#else
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800805 old_fd = wakeup.fd;
Victor Stinner11517102014-07-29 23:31:34 +0200806#endif
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800807 wakeup.fd = fd;
808 wakeup.warn_on_full_buffer = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000809 return old_fd;
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000810}
811
812
Martin v. Löwis823725e2008-03-24 13:39:54 +0000813#ifdef HAVE_SETITIMER
Tal Einatc7027b72015-05-16 14:14:49 +0300814
815/*[clinic input]
816signal.setitimer
817
818 which: int
Victor Stinneref611c92017-10-13 13:49:43 -0700819 seconds: object
820 interval: object(c_default="NULL") = 0.0
Tal Einatc7027b72015-05-16 14:14:49 +0300821 /
822
823Sets given itimer (one of ITIMER_REAL, ITIMER_VIRTUAL or ITIMER_PROF).
824
825The timer will fire after value seconds and after that every interval seconds.
826The itimer can be cleared by setting seconds to zero.
827
828Returns old values as a tuple: (delay, interval).
829[clinic start generated code]*/
830
Martin v. Löwis823725e2008-03-24 13:39:54 +0000831static PyObject *
Victor Stinneref611c92017-10-13 13:49:43 -0700832signal_setitimer_impl(PyObject *module, int which, PyObject *seconds,
833 PyObject *interval)
834/*[clinic end generated code: output=65f9dcbddc35527b input=de43daf194e6f66f]*/
Martin v. Löwis823725e2008-03-24 13:39:54 +0000835{
Martin v. Löwis823725e2008-03-24 13:39:54 +0000836 struct itimerval new, old;
837
Victor Stinneref611c92017-10-13 13:49:43 -0700838 if (timeval_from_double(seconds, &new.it_value) < 0) {
839 return NULL;
840 }
841 if (timeval_from_double(interval, &new.it_interval) < 0) {
842 return NULL;
843 }
844
Martin v. Löwis823725e2008-03-24 13:39:54 +0000845 /* Let OS check "which" value */
846 if (setitimer(which, &new, &old) != 0) {
Tal Einatc7027b72015-05-16 14:14:49 +0300847 PyErr_SetFromErrno(ItimerError);
848 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000849 }
850
851 return itimer_retval(&old);
852}
853
Martin v. Löwis823725e2008-03-24 13:39:54 +0000854#endif
855
856
857#ifdef HAVE_GETITIMER
Tal Einatc7027b72015-05-16 14:14:49 +0300858
859/*[clinic input]
860signal.getitimer
861
862 which: int
863 /
864
865Returns current value of given itimer.
866[clinic start generated code]*/
867
Martin v. Löwis823725e2008-03-24 13:39:54 +0000868static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300869signal_getitimer_impl(PyObject *module, int which)
870/*[clinic end generated code: output=9e053175d517db40 input=f7d21d38f3490627]*/
Martin v. Löwis823725e2008-03-24 13:39:54 +0000871{
Martin v. Löwis823725e2008-03-24 13:39:54 +0000872 struct itimerval old;
873
Martin v. Löwis823725e2008-03-24 13:39:54 +0000874 if (getitimer(which, &old) != 0) {
Tal Einatc7027b72015-05-16 14:14:49 +0300875 PyErr_SetFromErrno(ItimerError);
876 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000877 }
878
879 return itimer_retval(&old);
880}
881
Martin v. Löwis823725e2008-03-24 13:39:54 +0000882#endif
883
Victor Stinnerb3e72192011-05-08 01:46:11 +0200884#if defined(PYPTHREAD_SIGMASK) || defined(HAVE_SIGPENDING)
Victor Stinner35b300c2011-05-04 13:20:35 +0200885static PyObject*
886sigset_to_set(sigset_t mask)
887{
888 PyObject *signum, *result;
889 int sig;
890
891 result = PySet_New(0);
892 if (result == NULL)
893 return NULL;
894
895 for (sig = 1; sig < NSIG; sig++) {
896 if (sigismember(&mask, sig) != 1)
897 continue;
898
899 /* Handle the case where it is a member by adding the signal to
900 the result list. Ignore the other cases because they mean the
901 signal isn't a member of the mask or the signal was invalid,
902 and an invalid signal must have been our fault in constructing
903 the loop boundaries. */
904 signum = PyLong_FromLong(sig);
905 if (signum == NULL) {
906 Py_DECREF(result);
907 return NULL;
908 }
909 if (PySet_Add(result, signum) == -1) {
910 Py_DECREF(signum);
911 Py_DECREF(result);
912 return NULL;
913 }
914 Py_DECREF(signum);
915 }
916 return result;
917}
Victor Stinnerb3e72192011-05-08 01:46:11 +0200918#endif
Victor Stinner35b300c2011-05-04 13:20:35 +0200919
Victor Stinnerb3e72192011-05-08 01:46:11 +0200920#ifdef PYPTHREAD_SIGMASK
Tal Einatc7027b72015-05-16 14:14:49 +0300921
922/*[clinic input]
923signal.pthread_sigmask
924
925 how: int
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300926 mask: sigset_t
Tal Einatc7027b72015-05-16 14:14:49 +0300927 /
928
929Fetch and/or change the signal mask of the calling thread.
930[clinic start generated code]*/
931
Victor Stinnera9293352011-04-30 15:21:58 +0200932static PyObject *
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300933signal_pthread_sigmask_impl(PyObject *module, int how, sigset_t mask)
934/*[clinic end generated code: output=0562c0fb192981a8 input=85bcebda442fa77f]*/
Victor Stinnera9293352011-04-30 15:21:58 +0200935{
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300936 sigset_t previous;
Victor Stinnera9293352011-04-30 15:21:58 +0200937 int err;
938
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300939 err = pthread_sigmask(how, &mask, &previous);
Victor Stinnera9293352011-04-30 15:21:58 +0200940 if (err != 0) {
941 errno = err;
Victor Stinnerb3e72192011-05-08 01:46:11 +0200942 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnera9293352011-04-30 15:21:58 +0200943 return NULL;
944 }
945
Victor Stinnerd0e516d2011-05-03 14:57:12 +0200946 /* if signals was unblocked, signal handlers have been called */
947 if (PyErr_CheckSignals())
948 return NULL;
949
Victor Stinner35b300c2011-05-04 13:20:35 +0200950 return sigset_to_set(previous);
Victor Stinnera9293352011-04-30 15:21:58 +0200951}
952
Victor Stinnera9293352011-04-30 15:21:58 +0200953#endif /* #ifdef PYPTHREAD_SIGMASK */
954
Martin v. Löwis823725e2008-03-24 13:39:54 +0000955
Victor Stinnerb3e72192011-05-08 01:46:11 +0200956#ifdef HAVE_SIGPENDING
Tal Einatc7027b72015-05-16 14:14:49 +0300957
958/*[clinic input]
959signal.sigpending
960
961Examine pending signals.
962
963Returns a set of signal numbers that are pending for delivery to
964the calling thread.
965[clinic start generated code]*/
966
Victor Stinnerb3e72192011-05-08 01:46:11 +0200967static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300968signal_sigpending_impl(PyObject *module)
969/*[clinic end generated code: output=53375ffe89325022 input=e0036c016f874e29]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +0200970{
971 int err;
972 sigset_t mask;
973 err = sigpending(&mask);
974 if (err)
975 return PyErr_SetFromErrno(PyExc_OSError);
976 return sigset_to_set(mask);
977}
978
Victor Stinnerb3e72192011-05-08 01:46:11 +0200979#endif /* #ifdef HAVE_SIGPENDING */
980
981
982#ifdef HAVE_SIGWAIT
Tal Einatc7027b72015-05-16 14:14:49 +0300983
984/*[clinic input]
985signal.sigwait
986
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300987 sigset: sigset_t
Tal Einatc7027b72015-05-16 14:14:49 +0300988 /
989
990Wait for a signal.
991
992Suspend execution of the calling thread until the delivery of one of the
993signals specified in the signal set sigset. The function accepts the signal
994and returns the signal number.
995[clinic start generated code]*/
996
Victor Stinnerb3e72192011-05-08 01:46:11 +0200997static PyObject *
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300998signal_sigwait_impl(PyObject *module, sigset_t sigset)
999/*[clinic end generated code: output=f43770699d682f96 input=a6fbd47b1086d119]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +02001000{
Victor Stinnerb3e72192011-05-08 01:46:11 +02001001 int err, signum;
1002
Victor Stinner10c30d62011-06-10 01:39:53 +02001003 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001004 err = sigwait(&sigset, &signum);
Victor Stinner10c30d62011-06-10 01:39:53 +02001005 Py_END_ALLOW_THREADS
Victor Stinnerb3e72192011-05-08 01:46:11 +02001006 if (err) {
1007 errno = err;
1008 return PyErr_SetFromErrno(PyExc_OSError);
1009 }
1010
1011 return PyLong_FromLong(signum);
1012}
1013
Tal Einatc7027b72015-05-16 14:14:49 +03001014#endif /* #ifdef HAVE_SIGWAIT */
1015
Victor Stinnerb3e72192011-05-08 01:46:11 +02001016
Antoine Pitrou9d3627e2018-05-04 13:00:50 +02001017#if defined(HAVE_SIGFILLSET) || defined(MS_WINDOWS)
1018
1019/*[clinic input]
1020signal.valid_signals
1021
1022Return a set of valid signal numbers on this platform.
1023
1024The signal numbers returned by this function can be safely passed to
1025functions like `pthread_sigmask`.
1026[clinic start generated code]*/
1027
1028static PyObject *
1029signal_valid_signals_impl(PyObject *module)
1030/*[clinic end generated code: output=1609cffbcfcf1314 input=86a3717ff25288f2]*/
1031{
1032#ifdef MS_WINDOWS
1033#ifdef SIGBREAK
1034 PyObject *tup = Py_BuildValue("(iiiiiii)", SIGABRT, SIGBREAK, SIGFPE,
1035 SIGILL, SIGINT, SIGSEGV, SIGTERM);
1036#else
1037 PyObject *tup = Py_BuildValue("(iiiiii)", SIGABRT, SIGFPE, SIGILL,
1038 SIGINT, SIGSEGV, SIGTERM);
1039#endif
1040 if (tup == NULL) {
1041 return NULL;
1042 }
1043 PyObject *set = PySet_New(tup);
1044 Py_DECREF(tup);
1045 return set;
1046#else
1047 sigset_t mask;
1048 if (sigemptyset(&mask) || sigfillset(&mask)) {
1049 return PyErr_SetFromErrno(PyExc_OSError);
1050 }
1051 return sigset_to_set(mask);
1052#endif
1053}
1054
1055#endif /* #if defined(HAVE_SIGFILLSET) || defined(MS_WINDOWS) */
1056
1057
Ross Lagerwallbc808222011-06-25 12:13:40 +02001058#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
1059static int initialized;
1060static PyStructSequence_Field struct_siginfo_fields[] = {
1061 {"si_signo", "signal number"},
1062 {"si_code", "signal code"},
1063 {"si_errno", "errno associated with this signal"},
1064 {"si_pid", "sending process ID"},
1065 {"si_uid", "real user ID of sending process"},
1066 {"si_status", "exit value or signal"},
1067 {"si_band", "band event for SIGPOLL"},
1068 {0}
1069};
1070
1071PyDoc_STRVAR(struct_siginfo__doc__,
1072"struct_siginfo: Result from sigwaitinfo or sigtimedwait.\n\n\
1073This object may be accessed either as a tuple of\n\
1074(si_signo, si_code, si_errno, si_pid, si_uid, si_status, si_band),\n\
1075or via the attributes si_signo, si_code, and so on.");
1076
1077static PyStructSequence_Desc struct_siginfo_desc = {
1078 "signal.struct_siginfo", /* name */
1079 struct_siginfo__doc__, /* doc */
1080 struct_siginfo_fields, /* fields */
1081 7 /* n_in_sequence */
1082};
1083
1084static PyTypeObject SiginfoType;
1085
1086static PyObject *
1087fill_siginfo(siginfo_t *si)
1088{
1089 PyObject *result = PyStructSequence_New(&SiginfoType);
1090 if (!result)
1091 return NULL;
1092
1093 PyStructSequence_SET_ITEM(result, 0, PyLong_FromLong((long)(si->si_signo)));
1094 PyStructSequence_SET_ITEM(result, 1, PyLong_FromLong((long)(si->si_code)));
Victor Stinner09532fe2019-05-10 23:39:09 +02001095#ifdef __VXWORKS__
pxinwr8b5bdda2019-03-14 01:18:25 +08001096 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong(0L));
1097 PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong(0L));
1098 PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong(0L));
1099 PyStructSequence_SET_ITEM(result, 5, PyLong_FromLong(0L));
Victor Stinner09532fe2019-05-10 23:39:09 +02001100#else
Ross Lagerwallbc808222011-06-25 12:13:40 +02001101 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si->si_errno)));
1102 PyStructSequence_SET_ITEM(result, 3, PyLong_FromPid(si->si_pid));
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02001103 PyStructSequence_SET_ITEM(result, 4, _PyLong_FromUid(si->si_uid));
Ross Lagerwallbc808222011-06-25 12:13:40 +02001104 PyStructSequence_SET_ITEM(result, 5,
1105 PyLong_FromLong((long)(si->si_status)));
Victor Stinner09532fe2019-05-10 23:39:09 +02001106#endif
Zachary Ware6a6967e2016-10-01 00:47:27 -05001107#ifdef HAVE_SIGINFO_T_SI_BAND
Ross Lagerwallbc808222011-06-25 12:13:40 +02001108 PyStructSequence_SET_ITEM(result, 6, PyLong_FromLong(si->si_band));
Zachary Ware6a6967e2016-10-01 00:47:27 -05001109#else
1110 PyStructSequence_SET_ITEM(result, 6, PyLong_FromLong(0L));
1111#endif
Ross Lagerwallbc808222011-06-25 12:13:40 +02001112 if (PyErr_Occurred()) {
1113 Py_DECREF(result);
1114 return NULL;
1115 }
1116
1117 return result;
1118}
1119#endif
1120
1121#ifdef HAVE_SIGWAITINFO
Tal Einatc7027b72015-05-16 14:14:49 +03001122
1123/*[clinic input]
1124signal.sigwaitinfo
1125
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001126 sigset: sigset_t
Tal Einatc7027b72015-05-16 14:14:49 +03001127 /
1128
1129Wait synchronously until one of the signals in *sigset* is delivered.
1130
1131Returns a struct_siginfo containing information about the signal.
1132[clinic start generated code]*/
1133
Ross Lagerwallbc808222011-06-25 12:13:40 +02001134static PyObject *
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001135signal_sigwaitinfo_impl(PyObject *module, sigset_t sigset)
1136/*[clinic end generated code: output=1eb2f1fa236fdbca input=3d1a7e1f27fc664c]*/
Ross Lagerwallbc808222011-06-25 12:13:40 +02001137{
Ross Lagerwallbc808222011-06-25 12:13:40 +02001138 siginfo_t si;
1139 int err;
Victor Stinnera453cd82015-03-20 12:54:28 +01001140 int async_err = 0;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001141
Victor Stinnera453cd82015-03-20 12:54:28 +01001142 do {
1143 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001144 err = sigwaitinfo(&sigset, &si);
Victor Stinnera453cd82015-03-20 12:54:28 +01001145 Py_END_ALLOW_THREADS
1146 } while (err == -1
1147 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Ross Lagerwallbc808222011-06-25 12:13:40 +02001148 if (err == -1)
Victor Stinnera453cd82015-03-20 12:54:28 +01001149 return (!async_err) ? PyErr_SetFromErrno(PyExc_OSError) : NULL;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001150
1151 return fill_siginfo(&si);
1152}
1153
Ross Lagerwallbc808222011-06-25 12:13:40 +02001154#endif /* #ifdef HAVE_SIGWAITINFO */
1155
1156#ifdef HAVE_SIGTIMEDWAIT
Tal Einatc7027b72015-05-16 14:14:49 +03001157
1158/*[clinic input]
1159signal.sigtimedwait
1160
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001161 sigset: sigset_t
Serhiy Storchaka6b680cd2015-05-16 15:57:56 +03001162 timeout as timeout_obj: object
Tal Einatc7027b72015-05-16 14:14:49 +03001163 /
1164
1165Like sigwaitinfo(), but with a timeout.
1166
1167The timeout is specified in seconds, with floating point numbers allowed.
1168[clinic start generated code]*/
1169
Ross Lagerwallbc808222011-06-25 12:13:40 +02001170static PyObject *
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001171signal_sigtimedwait_impl(PyObject *module, sigset_t sigset,
Serhiy Storchaka6b680cd2015-05-16 15:57:56 +03001172 PyObject *timeout_obj)
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001173/*[clinic end generated code: output=59c8971e8ae18a64 input=87fd39237cf0b7ba]*/
Ross Lagerwallbc808222011-06-25 12:13:40 +02001174{
Victor Stinnera453cd82015-03-20 12:54:28 +01001175 struct timespec ts;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001176 siginfo_t si;
Victor Stinnera453cd82015-03-20 12:54:28 +01001177 int res;
Victor Stinner34dc0f42015-03-27 18:19:03 +01001178 _PyTime_t timeout, deadline, monotonic;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001179
Victor Stinner869e1772015-03-30 03:49:14 +02001180 if (_PyTime_FromSecondsObject(&timeout,
1181 timeout_obj, _PyTime_ROUND_CEILING) < 0)
Ross Lagerwallbc808222011-06-25 12:13:40 +02001182 return NULL;
1183
Victor Stinnera453cd82015-03-20 12:54:28 +01001184 if (timeout < 0) {
Ross Lagerwallbc808222011-06-25 12:13:40 +02001185 PyErr_SetString(PyExc_ValueError, "timeout must be non-negative");
1186 return NULL;
1187 }
1188
Victor Stinner34dc0f42015-03-27 18:19:03 +01001189 deadline = _PyTime_GetMonotonicClock() + timeout;
Victor Stinnera453cd82015-03-20 12:54:28 +01001190
1191 do {
Victor Stinner34dc0f42015-03-27 18:19:03 +01001192 if (_PyTime_AsTimespec(timeout, &ts) < 0)
1193 return NULL;
Victor Stinnera453cd82015-03-20 12:54:28 +01001194
1195 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001196 res = sigtimedwait(&sigset, &si, &ts);
Victor Stinnera453cd82015-03-20 12:54:28 +01001197 Py_END_ALLOW_THREADS
1198
1199 if (res != -1)
1200 break;
1201
1202 if (errno != EINTR) {
1203 if (errno == EAGAIN)
1204 Py_RETURN_NONE;
1205 else
1206 return PyErr_SetFromErrno(PyExc_OSError);
1207 }
1208
1209 /* sigtimedwait() was interrupted by a signal (EINTR) */
1210 if (PyErr_CheckSignals())
1211 return NULL;
1212
Victor Stinner34dc0f42015-03-27 18:19:03 +01001213 monotonic = _PyTime_GetMonotonicClock();
1214 timeout = deadline - monotonic;
Victor Stinner6aa446c2015-03-30 21:33:51 +02001215 if (timeout < 0)
Victor Stinnera453cd82015-03-20 12:54:28 +01001216 break;
1217 } while (1);
Ross Lagerwallbc808222011-06-25 12:13:40 +02001218
1219 return fill_siginfo(&si);
1220}
1221
Ross Lagerwallbc808222011-06-25 12:13:40 +02001222#endif /* #ifdef HAVE_SIGTIMEDWAIT */
1223
Victor Stinnerb3e72192011-05-08 01:46:11 +02001224
Antoine Pitroua6a4dc82017-09-07 18:56:24 +02001225#if defined(HAVE_PTHREAD_KILL)
Tal Einatc7027b72015-05-16 14:14:49 +03001226
1227/*[clinic input]
1228signal.pthread_kill
1229
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +02001230 thread_id: unsigned_long(bitwise=True)
Tal Einatc7027b72015-05-16 14:14:49 +03001231 signalnum: int
1232 /
1233
1234Send a signal to a thread.
1235[clinic start generated code]*/
1236
Victor Stinnerb3e72192011-05-08 01:46:11 +02001237static PyObject *
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +02001238signal_pthread_kill_impl(PyObject *module, unsigned long thread_id,
1239 int signalnum)
1240/*[clinic end generated code: output=7629919b791bc27f input=1d901f2c7bb544ff]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +02001241{
Victor Stinnerb3e72192011-05-08 01:46:11 +02001242 int err;
1243
Tal Einatc7027b72015-05-16 14:14:49 +03001244 err = pthread_kill((pthread_t)thread_id, signalnum);
Victor Stinnerb3e72192011-05-08 01:46:11 +02001245 if (err != 0) {
1246 errno = err;
1247 PyErr_SetFromErrno(PyExc_OSError);
1248 return NULL;
1249 }
1250
1251 /* the signal may have been send to the current thread */
1252 if (PyErr_CheckSignals())
1253 return NULL;
1254
1255 Py_RETURN_NONE;
1256}
1257
Antoine Pitroua6a4dc82017-09-07 18:56:24 +02001258#endif /* #if defined(HAVE_PTHREAD_KILL) */
Victor Stinnerb3e72192011-05-08 01:46:11 +02001259
1260
1261
Tal Einatc7027b72015-05-16 14:14:49 +03001262/* List of functions defined in the module -- some of the methoddefs are
1263 defined to nothing if the corresponding C function is not available. */
Barry Warsaw92971171997-01-03 00:14:25 +00001264static PyMethodDef signal_methods[] = {
Tal Einatc7027b72015-05-16 14:14:49 +03001265 {"default_int_handler", signal_default_int_handler, METH_VARARGS, default_int_handler_doc},
1266 SIGNAL_ALARM_METHODDEF
1267 SIGNAL_SETITIMER_METHODDEF
1268 SIGNAL_GETITIMER_METHODDEF
1269 SIGNAL_SIGNAL_METHODDEF
Vladimir Matveevc24c6c22019-01-08 01:58:25 -08001270 SIGNAL_RAISE_SIGNAL_METHODDEF
Antoine Pietri5d2a27d2018-03-12 14:42:34 +01001271 SIGNAL_STRSIGNAL_METHODDEF
Tal Einatc7027b72015-05-16 14:14:49 +03001272 SIGNAL_GETSIGNAL_METHODDEF
Serhiy Storchaka62be7422018-11-27 13:27:31 +02001273 {"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 +03001274 SIGNAL_SIGINTERRUPT_METHODDEF
1275 SIGNAL_PAUSE_METHODDEF
1276 SIGNAL_PTHREAD_KILL_METHODDEF
1277 SIGNAL_PTHREAD_SIGMASK_METHODDEF
1278 SIGNAL_SIGPENDING_METHODDEF
1279 SIGNAL_SIGWAIT_METHODDEF
1280 SIGNAL_SIGWAITINFO_METHODDEF
1281 SIGNAL_SIGTIMEDWAIT_METHODDEF
Antoine Pitrou9d3627e2018-05-04 13:00:50 +02001282#if defined(HAVE_SIGFILLSET) || defined(MS_WINDOWS)
1283 SIGNAL_VALID_SIGNALS_METHODDEF
1284#endif
Tal Einatc7027b72015-05-16 14:14:49 +03001285 {NULL, NULL} /* sentinel */
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001286};
1287
Barry Warsaw92971171997-01-03 00:14:25 +00001288
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001289PyDoc_STRVAR(module_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001290"This module provides mechanisms to use signal handlers in Python.\n\
1291\n\
1292Functions:\n\
1293\n\
1294alarm() -- cause SIGALRM after a specified time [Unix only]\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001295setitimer() -- cause a signal (described below) after a specified\n\
1296 float time and the timer may restart then [Unix only]\n\
1297getitimer() -- get current value of timer [Unix only]\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001298signal() -- set the action for a given signal\n\
1299getsignal() -- get the signal action for a given signal\n\
1300pause() -- wait until a signal arrives [Unix only]\n\
1301default_int_handler() -- default SIGINT handler\n\
1302\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001303signal constants:\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001304SIG_DFL -- used to refer to the system default handler\n\
1305SIG_IGN -- used to ignore the signal\n\
1306NSIG -- number of defined signals\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001307SIGINT, SIGTERM, etc. -- signal numbers\n\
1308\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001309itimer constants:\n\
1310ITIMER_REAL -- decrements in real time, and delivers SIGALRM upon\n\
1311 expiration\n\
1312ITIMER_VIRTUAL -- decrements only when the process is executing,\n\
1313 and delivers SIGVTALRM upon expiration\n\
1314ITIMER_PROF -- decrements both when the process is executing and\n\
1315 when the system is executing on behalf of the process.\n\
1316 Coupled with ITIMER_VIRTUAL, this timer is usually\n\
1317 used to profile the time spent by the application\n\
1318 in user and kernel space. SIGPROF is delivered upon\n\
1319 expiration.\n\
1320\n\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001321*** IMPORTANT NOTICE ***\n\
1322A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001323the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001324
Martin v. Löwis1a214512008-06-11 05:26:20 +00001325static struct PyModuleDef signalmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001326 PyModuleDef_HEAD_INIT,
Brett Cannon815a6f32014-04-04 10:20:28 -04001327 "_signal",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001328 module_doc,
1329 -1,
1330 signal_methods,
1331 NULL,
1332 NULL,
1333 NULL,
1334 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001335};
1336
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001337PyMODINIT_FUNC
Giampaolo Rodola'e09fb712014-04-04 15:34:17 +02001338PyInit__signal(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001339{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001340 PyObject *m, *d, *x;
1341 int i;
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001342
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001343 /* Create the module and add the functions */
1344 m = PyModule_Create(&signalmodule);
1345 if (m == NULL)
1346 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001347
Ross Lagerwallbc808222011-06-25 12:13:40 +02001348#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
Victor Stinner1c8f0592013-07-22 22:24:54 +02001349 if (!initialized) {
1350 if (PyStructSequence_InitType2(&SiginfoType, &struct_siginfo_desc) < 0)
1351 return NULL;
1352 }
Ross Lagerwallbc808222011-06-25 12:13:40 +02001353 Py_INCREF((PyObject*) &SiginfoType);
1354 PyModule_AddObject(m, "struct_siginfo", (PyObject*) &SiginfoType);
1355 initialized = 1;
1356#endif
1357
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001358 /* Add some symbolic constants to the module */
1359 d = PyModule_GetDict(m);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001360
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001361 x = DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL);
Joannah Nanjekye9541bd32019-04-21 21:47:06 -04001362 if (PyModule_AddObject(m, "SIG_DFL", x))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001363 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001364
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001365 x = IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN);
Joannah Nanjekye9541bd32019-04-21 21:47:06 -04001366 if (PyModule_AddObject(m, "SIG_IGN", x))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001367 goto finally;
Barry Warsaw92971171997-01-03 00:14:25 +00001368
Joannah Nanjekye9541bd32019-04-21 21:47:06 -04001369 if (PyModule_AddIntMacro(m, NSIG))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001370 goto finally;
Barry Warsaw92971171997-01-03 00:14:25 +00001371
Victor Stinnera9293352011-04-30 15:21:58 +02001372#ifdef SIG_BLOCK
Victor Stinner72c53b52011-05-02 16:15:43 +02001373 if (PyModule_AddIntMacro(m, SIG_BLOCK))
1374 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001375#endif
Victor Stinnera9293352011-04-30 15:21:58 +02001376#ifdef SIG_UNBLOCK
Victor Stinner72c53b52011-05-02 16:15:43 +02001377 if (PyModule_AddIntMacro(m, SIG_UNBLOCK))
1378 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001379#endif
Victor Stinnera9293352011-04-30 15:21:58 +02001380#ifdef SIG_SETMASK
Victor Stinner72c53b52011-05-02 16:15:43 +02001381 if (PyModule_AddIntMacro(m, SIG_SETMASK))
1382 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001383#endif
1384
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001385 x = IntHandler = PyDict_GetItemString(d, "default_int_handler");
1386 if (!x)
1387 goto finally;
1388 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +00001389
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001390 _Py_atomic_store_relaxed(&Handlers[0].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001391 for (i = 1; i < NSIG; i++) {
1392 void (*t)(int);
1393 t = PyOS_getsig(i);
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001394 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001395 if (t == SIG_DFL)
1396 Handlers[i].func = DefaultHandler;
1397 else if (t == SIG_IGN)
1398 Handlers[i].func = IgnoreHandler;
1399 else
1400 Handlers[i].func = Py_None; /* None of our business */
1401 Py_INCREF(Handlers[i].func);
1402 }
1403 if (Handlers[SIGINT].func == DefaultHandler) {
1404 /* Install default int handler */
1405 Py_INCREF(IntHandler);
Serhiy Storchaka57a01d32016-04-10 18:05:40 +03001406 Py_SETREF(Handlers[SIGINT].func, IntHandler);
pkerlinge905c842018-06-01 09:47:18 +00001407 PyOS_setsig(SIGINT, signal_handler);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001408 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001409
1410#ifdef SIGHUP
Christian Heimes6782b142016-09-09 00:11:45 +02001411 if (PyModule_AddIntMacro(m, SIGHUP))
1412 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001413#endif
1414#ifdef SIGINT
Christian Heimes6782b142016-09-09 00:11:45 +02001415 if (PyModule_AddIntMacro(m, SIGINT))
1416 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001417#endif
Tim Peters1ce3cf72001-10-01 17:58:40 +00001418#ifdef SIGBREAK
Christian Heimes6782b142016-09-09 00:11:45 +02001419 if (PyModule_AddIntMacro(m, SIGBREAK))
1420 goto finally;
Tim Peters1ce3cf72001-10-01 17:58:40 +00001421#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001422#ifdef SIGQUIT
Christian Heimes6782b142016-09-09 00:11:45 +02001423 if (PyModule_AddIntMacro(m, SIGQUIT))
1424 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001425#endif
1426#ifdef SIGILL
Christian Heimes6782b142016-09-09 00:11:45 +02001427 if (PyModule_AddIntMacro(m, SIGILL))
1428 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001429#endif
1430#ifdef SIGTRAP
Christian Heimes6782b142016-09-09 00:11:45 +02001431 if (PyModule_AddIntMacro(m, SIGTRAP))
1432 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001433#endif
1434#ifdef SIGIOT
Christian Heimes6782b142016-09-09 00:11:45 +02001435 if (PyModule_AddIntMacro(m, SIGIOT))
1436 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001437#endif
1438#ifdef SIGABRT
Christian Heimes6782b142016-09-09 00:11:45 +02001439 if (PyModule_AddIntMacro(m, SIGABRT))
1440 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001441#endif
1442#ifdef SIGEMT
Christian Heimes6782b142016-09-09 00:11:45 +02001443 if (PyModule_AddIntMacro(m, SIGEMT))
1444 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001445#endif
1446#ifdef SIGFPE
Christian Heimes6782b142016-09-09 00:11:45 +02001447 if (PyModule_AddIntMacro(m, SIGFPE))
1448 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001449#endif
1450#ifdef SIGKILL
Christian Heimes6782b142016-09-09 00:11:45 +02001451 if (PyModule_AddIntMacro(m, SIGKILL))
1452 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001453#endif
1454#ifdef SIGBUS
Christian Heimes6782b142016-09-09 00:11:45 +02001455 if (PyModule_AddIntMacro(m, SIGBUS))
1456 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001457#endif
1458#ifdef SIGSEGV
Christian Heimes6782b142016-09-09 00:11:45 +02001459 if (PyModule_AddIntMacro(m, SIGSEGV))
1460 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001461#endif
1462#ifdef SIGSYS
Christian Heimes6782b142016-09-09 00:11:45 +02001463 if (PyModule_AddIntMacro(m, SIGSYS))
1464 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001465#endif
1466#ifdef SIGPIPE
Christian Heimes6782b142016-09-09 00:11:45 +02001467 if (PyModule_AddIntMacro(m, SIGPIPE))
1468 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001469#endif
1470#ifdef SIGALRM
Christian Heimes6782b142016-09-09 00:11:45 +02001471 if (PyModule_AddIntMacro(m, SIGALRM))
1472 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001473#endif
1474#ifdef SIGTERM
Christian Heimes6782b142016-09-09 00:11:45 +02001475 if (PyModule_AddIntMacro(m, SIGTERM))
1476 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001477#endif
1478#ifdef SIGUSR1
Christian Heimes6782b142016-09-09 00:11:45 +02001479 if (PyModule_AddIntMacro(m, SIGUSR1))
1480 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001481#endif
1482#ifdef SIGUSR2
Christian Heimes6782b142016-09-09 00:11:45 +02001483 if (PyModule_AddIntMacro(m, SIGUSR2))
1484 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001485#endif
1486#ifdef SIGCLD
Christian Heimes6782b142016-09-09 00:11:45 +02001487 if (PyModule_AddIntMacro(m, SIGCLD))
1488 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001489#endif
1490#ifdef SIGCHLD
Christian Heimes6782b142016-09-09 00:11:45 +02001491 if (PyModule_AddIntMacro(m, SIGCHLD))
1492 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001493#endif
1494#ifdef SIGPWR
Christian Heimes6782b142016-09-09 00:11:45 +02001495 if (PyModule_AddIntMacro(m, SIGPWR))
1496 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001497#endif
1498#ifdef SIGIO
Christian Heimes6782b142016-09-09 00:11:45 +02001499 if (PyModule_AddIntMacro(m, SIGIO))
1500 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001501#endif
1502#ifdef SIGURG
Christian Heimes6782b142016-09-09 00:11:45 +02001503 if (PyModule_AddIntMacro(m, SIGURG))
1504 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001505#endif
1506#ifdef SIGWINCH
Christian Heimes6782b142016-09-09 00:11:45 +02001507 if (PyModule_AddIntMacro(m, SIGWINCH))
1508 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001509#endif
1510#ifdef SIGPOLL
Christian Heimes6782b142016-09-09 00:11:45 +02001511 if (PyModule_AddIntMacro(m, SIGPOLL))
1512 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001513#endif
1514#ifdef SIGSTOP
Christian Heimes6782b142016-09-09 00:11:45 +02001515 if (PyModule_AddIntMacro(m, SIGSTOP))
1516 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001517#endif
1518#ifdef SIGTSTP
Christian Heimes6782b142016-09-09 00:11:45 +02001519 if (PyModule_AddIntMacro(m, SIGTSTP))
1520 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001521#endif
1522#ifdef SIGCONT
Christian Heimes6782b142016-09-09 00:11:45 +02001523 if (PyModule_AddIntMacro(m, SIGCONT))
1524 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001525#endif
1526#ifdef SIGTTIN
Christian Heimes6782b142016-09-09 00:11:45 +02001527 if (PyModule_AddIntMacro(m, SIGTTIN))
1528 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001529#endif
1530#ifdef SIGTTOU
Christian Heimes6782b142016-09-09 00:11:45 +02001531 if (PyModule_AddIntMacro(m, SIGTTOU))
1532 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001533#endif
1534#ifdef SIGVTALRM
Christian Heimes6782b142016-09-09 00:11:45 +02001535 if (PyModule_AddIntMacro(m, SIGVTALRM))
1536 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001537#endif
1538#ifdef SIGPROF
Christian Heimes6782b142016-09-09 00:11:45 +02001539 if (PyModule_AddIntMacro(m, SIGPROF))
1540 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001541#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001542#ifdef SIGXCPU
Christian Heimes6782b142016-09-09 00:11:45 +02001543 if (PyModule_AddIntMacro(m, SIGXCPU))
1544 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001545#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001546#ifdef SIGXFSZ
Christian Heimes6782b142016-09-09 00:11:45 +02001547 if (PyModule_AddIntMacro(m, SIGXFSZ))
1548 goto finally;
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001549#endif
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001550#ifdef SIGRTMIN
Christian Heimes6782b142016-09-09 00:11:45 +02001551 if (PyModule_AddIntMacro(m, SIGRTMIN))
1552 goto finally;
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001553#endif
1554#ifdef SIGRTMAX
Christian Heimes6782b142016-09-09 00:11:45 +02001555 if (PyModule_AddIntMacro(m, SIGRTMAX))
1556 goto finally;
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001557#endif
Martin v. Löwis175af252002-01-12 11:43:25 +00001558#ifdef SIGINFO
Christian Heimes6782b142016-09-09 00:11:45 +02001559 if (PyModule_AddIntMacro(m, SIGINFO))
1560 goto finally;
Martin v. Löwis175af252002-01-12 11:43:25 +00001561#endif
Martin v. Löwis823725e2008-03-24 13:39:54 +00001562
1563#ifdef ITIMER_REAL
Christian Heimes6782b142016-09-09 00:11:45 +02001564 if (PyModule_AddIntMacro(m, ITIMER_REAL))
1565 goto finally;
Martin v. Löwis823725e2008-03-24 13:39:54 +00001566#endif
1567#ifdef ITIMER_VIRTUAL
Christian Heimes6782b142016-09-09 00:11:45 +02001568 if (PyModule_AddIntMacro(m, ITIMER_VIRTUAL))
1569 goto finally;
Martin v. Löwis823725e2008-03-24 13:39:54 +00001570#endif
1571#ifdef ITIMER_PROF
Christian Heimes6782b142016-09-09 00:11:45 +02001572 if (PyModule_AddIntMacro(m, ITIMER_PROF))
1573 goto finally;
Martin v. Löwis823725e2008-03-24 13:39:54 +00001574#endif
1575
1576#if defined (HAVE_SETITIMER) || defined (HAVE_GETITIMER)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001577 ItimerError = PyErr_NewException("signal.ItimerError",
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +03001578 PyExc_OSError, NULL);
Joannah Nanjekye9541bd32019-04-21 21:47:06 -04001579 if (PyModule_AddObject(m, "ItimerError", ItimerError))
1580 goto finally;
Martin v. Löwis823725e2008-03-24 13:39:54 +00001581#endif
1582
Brian Curtineb24d742010-04-12 17:16:38 +00001583#ifdef CTRL_C_EVENT
Christian Heimes6782b142016-09-09 00:11:45 +02001584 if (PyModule_AddIntMacro(m, CTRL_C_EVENT))
1585 goto finally;
Brian Curtineb24d742010-04-12 17:16:38 +00001586#endif
1587
1588#ifdef CTRL_BREAK_EVENT
Christian Heimes6782b142016-09-09 00:11:45 +02001589 if (PyModule_AddIntMacro(m, CTRL_BREAK_EVENT))
1590 goto finally;
Brian Curtineb24d742010-04-12 17:16:38 +00001591#endif
1592
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001593#ifdef MS_WINDOWS
1594 /* Create manual-reset event, initially unset */
1595 sigint_event = CreateEvent(NULL, TRUE, FALSE, FALSE);
1596#endif
1597
Martin v. Löwis1a214512008-06-11 05:26:20 +00001598 if (PyErr_Occurred()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001599 Py_DECREF(m);
1600 m = NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001601 }
Barry Warsaw92971171997-01-03 00:14:25 +00001602
Barry Warsaw92971171997-01-03 00:14:25 +00001603 finally:
Martin v. Löwis1a214512008-06-11 05:26:20 +00001604 return m;
Guido van Rossum08c16611997-08-02 03:01:42 +00001605}
1606
1607static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001608finisignal(void)
Guido van Rossum08c16611997-08-02 03:01:42 +00001609{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001610 int i;
1611 PyObject *func;
Guido van Rossum08c16611997-08-02 03:01:42 +00001612
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001613 for (i = 1; i < NSIG; i++) {
1614 func = Handlers[i].func;
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001615 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001616 Handlers[i].func = NULL;
pkerlinge905c842018-06-01 09:47:18 +00001617 if (func != NULL && func != Py_None &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001618 func != DefaultHandler && func != IgnoreHandler)
1619 PyOS_setsig(i, SIG_DFL);
1620 Py_XDECREF(func);
1621 }
Guido van Rossum08c16611997-08-02 03:01:42 +00001622
Serhiy Storchaka505ff752014-02-09 13:33:53 +02001623 Py_CLEAR(IntHandler);
1624 Py_CLEAR(DefaultHandler);
1625 Py_CLEAR(IgnoreHandler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001626}
1627
Barry Warsaw92971171997-01-03 00:14:25 +00001628
Barry Warsaw92971171997-01-03 00:14:25 +00001629/* Declared in pyerrors.h */
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001630int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001631PyErr_CheckSignals(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001632{
Victor Stinnerd8613dc2019-05-24 13:43:55 +02001633 _PyRuntimeState *runtime = &_PyRuntime;
1634 if (!is_main(runtime)) {
Eric Snow64d6cc82019-02-23 15:40:43 -07001635 return 0;
1636 }
1637
1638 return _PyErr_CheckSignals();
1639}
1640
1641
1642/* Declared in cpython/pyerrors.h */
1643int
1644_PyErr_CheckSignals(void)
1645{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001646 int i;
1647 PyObject *f;
Barry Warsaw92971171997-01-03 00:14:25 +00001648
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001649 if (!_Py_atomic_load(&is_tripped))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001650 return 0;
Christian Heimesb76922a2007-12-11 01:06:40 +00001651
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001652 /*
1653 * The is_tripped variable is meant to speed up the calls to
1654 * PyErr_CheckSignals (both directly or via pending calls) when no
1655 * signal has arrived. This variable is set to 1 when a signal arrives
1656 * and it is set to 0 here, when we know some signals arrived. This way
1657 * we can run the registered handlers with no signals blocked.
1658 *
1659 * NOTE: with this approach we can have a situation where is_tripped is
1660 * 1 but we have no more signals to handle (Handlers[i].tripped
1661 * is 0 for every signal i). This won't do us any harm (except
1662 * we're gonna spent some cycles for nothing). This happens when
1663 * we receive a signal i after we zero is_tripped and before we
1664 * check Handlers[i].tripped.
1665 */
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001666 _Py_atomic_store(&is_tripped, 0);
Christian Heimesb76922a2007-12-11 01:06:40 +00001667
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001668 if (!(f = (PyObject *)PyEval_GetFrame()))
1669 f = Py_None;
Christian Heimesb76922a2007-12-11 01:06:40 +00001670
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001671 for (i = 1; i < NSIG; i++) {
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001672 if (_Py_atomic_load_relaxed(&Handlers[i].tripped)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001673 PyObject *result = NULL;
1674 PyObject *arglist = Py_BuildValue("(iO)", i, f);
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001675 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Barry Warsaw92971171997-01-03 00:14:25 +00001676
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001677 if (arglist) {
1678 result = PyEval_CallObject(Handlers[i].func,
1679 arglist);
1680 Py_DECREF(arglist);
1681 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +02001682 if (!result) {
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001683 _Py_atomic_store(&is_tripped, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001684 return -1;
Antoine Pitrouc08177a2017-06-28 23:29:29 +02001685 }
Barry Warsaw92971171997-01-03 00:14:25 +00001686
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001687 Py_DECREF(result);
1688 }
1689 }
Christian Heimesb76922a2007-12-11 01:06:40 +00001690
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001691 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001692}
1693
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +00001694
Matěj Cepl608876b2019-05-23 22:30:00 +02001695/* Simulate the effect of a signal.SIGINT signal arriving. The next time
1696 PyErr_CheckSignals is called, the Python SIGINT signal handler will be
1697 raised.
1698
1699 Missing signal handler for the SIGINT signal is silently ignored. */
Barry Warsaw92971171997-01-03 00:14:25 +00001700void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001701PyErr_SetInterrupt(void)
Barry Warsaw92971171997-01-03 00:14:25 +00001702{
Matěj Cepl608876b2019-05-23 22:30:00 +02001703 if ((Handlers[SIGINT].func != IgnoreHandler) &&
1704 (Handlers[SIGINT].func != DefaultHandler)) {
1705 trip_signal(SIGINT);
1706 }
Barry Warsaw92971171997-01-03 00:14:25 +00001707}
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001708
1709void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001710PyOS_InitInterrupts(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001711{
Giampaolo Rodola'e09fb712014-04-04 15:34:17 +02001712 PyObject *m = PyImport_ImportModule("_signal");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001713 if (m) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001714 Py_DECREF(m);
1715 }
Guido van Rossum08c16611997-08-02 03:01:42 +00001716}
1717
1718void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001719PyOS_FiniInterrupts(void)
Guido van Rossum08c16611997-08-02 03:01:42 +00001720{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001721 finisignal();
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001722}
1723
1724int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001725PyOS_InterruptOccurred(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001726{
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001727 if (_Py_atomic_load_relaxed(&Handlers[SIGINT].tripped)) {
Victor Stinnerd8613dc2019-05-24 13:43:55 +02001728 _PyRuntimeState *runtime = &_PyRuntime;
1729 if (!is_main(runtime)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001730 return 0;
Eric Snow64d6cc82019-02-23 15:40:43 -07001731 }
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001732 _Py_atomic_store_relaxed(&Handlers[SIGINT].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001733 return 1;
1734 }
1735 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001736}
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001737
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001738static void
1739_clear_pending_signals(void)
1740{
1741 int i;
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001742 if (!_Py_atomic_load(&is_tripped))
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001743 return;
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001744 _Py_atomic_store(&is_tripped, 0);
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001745 for (i = 1; i < NSIG; ++i) {
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001746 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001747 }
1748}
1749
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001750void
Antoine Pitrou346cbd32017-05-27 17:50:54 +02001751_PySignal_AfterFork(void)
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001752{
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001753 /* Clear the signal flags after forking so that they aren't handled
1754 * in both processes if they came in just before the fork() but before
1755 * the interpreter had an opportunity to call the handlers. issue9535. */
1756 _clear_pending_signals();
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001757}
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001758
1759int
1760_PyOS_IsMainThread(void)
1761{
Victor Stinnerd8613dc2019-05-24 13:43:55 +02001762 _PyRuntimeState *runtime = &_PyRuntime;
1763 return is_main(runtime);
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001764}
1765
1766#ifdef MS_WINDOWS
1767void *_PyOS_SigintEvent(void)
1768{
1769 /* Returns a manual-reset event which gets tripped whenever
1770 SIGINT is received.
1771
1772 Python.h does not include windows.h so we do cannot use HANDLE
1773 as the return type of this function. We use void* instead. */
1774 return sigint_event;
1775}
1776#endif