blob: 4590017c170a524073afb4b645290153921f7f6f [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 Stinner31368a42018-10-30 15:14:25 +01008
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02009#ifndef MS_WINDOWS
10#include "posixmodule.h"
11#endif
Victor Stinner11517102014-07-29 23:31:34 +020012#ifdef MS_WINDOWS
13#include "socketmodule.h" /* needed for SOCKET_T */
14#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +000015
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000016#ifdef MS_WINDOWS
Antoine Pitrou7faf7052013-03-31 22:48:04 +020017#include <windows.h>
Benjamin Peterson2614cda2010-03-21 22:36:19 +000018#ifdef HAVE_PROCESS_H
Guido van Rossum644a12b1997-04-09 19:24:53 +000019#include <process.h>
20#endif
Benjamin Peterson2614cda2010-03-21 22:36:19 +000021#endif
Guido van Rossum644a12b1997-04-09 19:24:53 +000022
Benjamin Peterson2614cda2010-03-21 22:36:19 +000023#ifdef HAVE_SIGNAL_H
Guido van Rossum398d9fe1994-05-11 08:59:13 +000024#include <signal.h>
Benjamin Peterson2614cda2010-03-21 22:36:19 +000025#endif
26#ifdef HAVE_SYS_STAT_H
Christian Heimes5fb7c2a2007-12-24 08:52:31 +000027#include <sys/stat.h>
Benjamin Peterson2614cda2010-03-21 22:36:19 +000028#endif
Martin v. Löwis3bf3cc02008-03-24 14:05:07 +000029#ifdef HAVE_SYS_TIME_H
Martin v. Löwis823725e2008-03-24 13:39:54 +000030#include <sys/time.h>
Martin v. Löwis3bf3cc02008-03-24 14:05:07 +000031#endif
Christian Heimes5fb7c2a2007-12-24 08:52:31 +000032
Victor Stinnera9293352011-04-30 15:21:58 +020033#if defined(HAVE_PTHREAD_SIGMASK) && !defined(HAVE_BROKEN_PTHREAD_SIGMASK)
34# define PYPTHREAD_SIGMASK
35#endif
36
37#if defined(PYPTHREAD_SIGMASK) && defined(HAVE_PTHREAD_H)
38# include <pthread.h>
39#endif
40
Guido van Rossumbb4ba121994-06-23 11:25:45 +000041#ifndef SIG_ERR
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +000042#define SIG_ERR ((PyOS_sighandler_t)(-1))
Guido van Rossumbb4ba121994-06-23 11:25:45 +000043#endif
44
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000045#ifndef NSIG
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000046# if defined(_NSIG)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000047# define NSIG _NSIG /* For BSD/SysV */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000048# elif defined(_SIGMAX)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000049# define NSIG (_SIGMAX + 1) /* For QNX */
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 djgpp */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000052# else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000053# define NSIG 64 /* Use a reasonable default value */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000054# endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000055#endif
56
Tal Einatc7027b72015-05-16 14:14:49 +030057#include "clinic/signalmodule.c.h"
58
59/*[clinic input]
60module signal
61[clinic start generated code]*/
62/*[clinic end generated code: output=da39a3ee5e6b4b0d input=b0301a3bde5fe9d3]*/
63
Serhiy Storchakad54cfb12018-05-08 07:48:50 +030064/*[python input]
65
66class sigset_t_converter(CConverter):
67 type = 'sigset_t'
68 converter = '_Py_Sigset_Converter'
69
70[python start generated code]*/
71/*[python end generated code: output=da39a3ee5e6b4b0d input=b5689d14466b6823]*/
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000072
Guido van Rossumbb4ba121994-06-23 11:25:45 +000073/*
74 NOTES ON THE INTERACTION BETWEEN SIGNALS AND THREADS
75
76 When threads are supported, we want the following semantics:
77
78 - only the main thread can set a signal handler
79 - any thread can get a signal handler
80 - signals are only delivered to the main thread
81
82 I.e. we don't support "synchronous signals" like SIGFPE (catching
83 this doesn't make much sense in Python anyway) nor do we support
84 signals as a means of inter-thread communication, since not all
85 thread implementations support that (at least our thread library
86 doesn't).
87
88 We still have the problem that in some implementations signals
89 generated by the keyboard (e.g. SIGINT) are delivered to all
90 threads (e.g. SGI), while in others (e.g. Solaris) such signals are
91 delivered to one random thread (an intermediate possibility would
Guido van Rossuma3c04b01995-01-12 11:29:01 +000092 be to deliver it to the main thread -- POSIX?). For now, we have
Guido van Rossumbb4ba121994-06-23 11:25:45 +000093 a working implementation that works in all three cases -- the
94 handler ignores signals if getpid() isn't the same as in the main
95 thread. XXX This is a hack.
Guido van Rossumbb4ba121994-06-23 11:25:45 +000096*/
97
Guido van Rossum295b8e51997-06-06 21:16:41 +000098#include <sys/types.h> /* For pid_t */
Guido van Rossum49b56061998-10-01 20:42:43 +000099#include "pythread.h"
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +0200100static unsigned long main_thread;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000101static pid_t main_pid;
Eric Snow64d6cc82019-02-23 15:40:43 -0700102static PyInterpreterState *main_interp;
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
190is_main(void)
191{
192 return PyThread_get_thread_ident() == main_thread &&
193 _PyInterpreterState_Get() == main_interp;
194}
195
Guido van Rossume4485b01994-09-07 14:32:49 +0000196static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000197signal_default_int_handler(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000198{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000199 PyErr_SetNone(PyExc_KeyboardInterrupt);
200 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000201}
202
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000203PyDoc_STRVAR(default_int_handler_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000204"default_int_handler(...)\n\
205\n\
Michael W. Hudson24ec2112004-06-17 15:55:53 +0000206The default handler for SIGINT installed by Python.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000207It raises KeyboardInterrupt.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000208
Thomas Wouters0796b002000-07-22 23:49:30 +0000209
210static int
Victor Stinner11517102014-07-29 23:31:34 +0200211report_wakeup_write_error(void *data)
Antoine Pitrou6f6ec372013-08-17 20:27:56 +0200212{
Eric Snowfdf282d2019-01-11 14:26:55 -0700213 PyObject *exc, *val, *tb;
Antoine Pitrou6f6ec372013-08-17 20:27:56 +0200214 int save_errno = errno;
Benjamin Petersonca470632016-09-06 13:47:26 -0700215 errno = (int) (intptr_t) data;
Eric Snowfdf282d2019-01-11 14:26:55 -0700216 PyErr_Fetch(&exc, &val, &tb);
Antoine Pitrou6f6ec372013-08-17 20:27:56 +0200217 PyErr_SetFromErrno(PyExc_OSError);
218 PySys_WriteStderr("Exception ignored when trying to write to the "
219 "signal wakeup fd:\n");
220 PyErr_WriteUnraisable(NULL);
Eric Snowfdf282d2019-01-11 14:26:55 -0700221 PyErr_Restore(exc, val, tb);
Antoine Pitrou6f6ec372013-08-17 20:27:56 +0200222 errno = save_errno;
223 return 0;
224}
225
Victor Stinner11517102014-07-29 23:31:34 +0200226#ifdef MS_WINDOWS
227static int
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800228report_wakeup_send_error(void* data)
Victor Stinner11517102014-07-29 23:31:34 +0200229{
Eric Snowfdf282d2019-01-11 14:26:55 -0700230 PyObject *exc, *val, *tb;
231 PyErr_Fetch(&exc, &val, &tb);
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800232 /* PyErr_SetExcFromWindowsErr() invokes FormatMessage() which
233 recognizes the error codes used by both GetLastError() and
234 WSAGetLastError */
235 PyErr_SetExcFromWindowsErr(PyExc_OSError, (int) (intptr_t) data);
Victor Stinner11517102014-07-29 23:31:34 +0200236 PySys_WriteStderr("Exception ignored when trying to send to the "
237 "signal wakeup fd:\n");
238 PyErr_WriteUnraisable(NULL);
Eric Snowfdf282d2019-01-11 14:26:55 -0700239 PyErr_Restore(exc, val, tb);
Victor Stinner11517102014-07-29 23:31:34 +0200240 return 0;
241}
242#endif /* MS_WINDOWS */
243
Tim Peters4f1b2082000-07-23 21:18:09 +0000244static void
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200245trip_signal(int sig_num)
246{
Victor Stinnerd49b1f12011-05-08 02:03:15 +0200247 unsigned char byte;
Victor Stinner11517102014-07-29 23:31:34 +0200248 int fd;
249 Py_ssize_t rc;
Victor Stinnerc13ef662011-05-25 02:35:58 +0200250
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +0200251 _Py_atomic_store_relaxed(&Handlers[sig_num].tripped, 1);
Victor Stinner11517102014-07-29 23:31:34 +0200252
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200253 /* Set is_tripped after setting .tripped, as it gets
254 cleared in PyErr_CheckSignals() before .tripped. */
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +0200255 _Py_atomic_store(&is_tripped, 1);
256
257 /* Notify ceval.c */
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200258 _PyEval_SignalReceived();
Nathaniel J. Smith4ae01492017-05-16 14:12:11 -0700259
260 /* And then write to the wakeup fd *after* setting all the globals and
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200261 doing the _PyEval_SignalReceived. We used to write to the wakeup fd
262 and then set the flag, but this allowed the following sequence of events
263 (especially on windows, where trip_signal may run in a new thread):
Nathaniel J. Smith4ae01492017-05-16 14:12:11 -0700264
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800265 - main thread blocks on select([wakeup.fd], ...)
Nathaniel J. Smith4ae01492017-05-16 14:12:11 -0700266 - signal arrives
267 - trip_signal writes to the wakeup fd
268 - the main thread wakes up
269 - the main thread checks the signal flags, sees that they're unset
270 - the main thread empties the wakeup fd
271 - the main thread goes back to sleep
272 - trip_signal sets the flags to request the Python-level signal handler
273 be run
274 - the main thread doesn't notice, because it's asleep
275
276 See bpo-30038 for more details.
277 */
278
Victor Stinner11517102014-07-29 23:31:34 +0200279#ifdef MS_WINDOWS
280 fd = Py_SAFE_DOWNCAST(wakeup.fd, SOCKET_T, int);
281#else
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800282 fd = wakeup.fd;
Victor Stinner11517102014-07-29 23:31:34 +0200283#endif
284
285 if (fd != INVALID_FD) {
Victor Stinnerc13ef662011-05-25 02:35:58 +0200286 byte = (unsigned char)sig_num;
Victor Stinner11517102014-07-29 23:31:34 +0200287#ifdef MS_WINDOWS
288 if (wakeup.use_send) {
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800289 rc = send(fd, &byte, 1, 0);
Victor Stinner11517102014-07-29 23:31:34 +0200290
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800291 if (rc < 0) {
292 int last_error = GetLastError();
293 if (wakeup.warn_on_full_buffer ||
294 last_error != WSAEWOULDBLOCK)
295 {
296 /* Py_AddPendingCall() isn't signal-safe, but we
297 still use it for this exceptional case. */
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100298 Py_AddPendingCall(report_wakeup_send_error,
299 (void *)(intptr_t) last_error);
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800300 }
Victor Stinner11517102014-07-29 23:31:34 +0200301 }
302 }
303 else
304#endif
305 {
Victor Stinnere72fe392015-04-01 18:35:22 +0200306 /* _Py_write_noraise() retries write() if write() is interrupted by
307 a signal (fails with EINTR). */
308 rc = _Py_write_noraise(fd, &byte, 1);
Victor Stinner11517102014-07-29 23:31:34 +0200309
310 if (rc < 0) {
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800311 if (wakeup.warn_on_full_buffer ||
312 (errno != EWOULDBLOCK && errno != EAGAIN))
313 {
314 /* Py_AddPendingCall() isn't signal-safe, but we
315 still use it for this exceptional case. */
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100316 Py_AddPendingCall(report_wakeup_write_error,
317 (void *)(intptr_t)errno);
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800318 }
Victor Stinner11517102014-07-29 23:31:34 +0200319 }
320 }
Victor Stinnerc13ef662011-05-25 02:35:58 +0200321 }
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200322}
323
324static void
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000325signal_handler(int sig_num)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000326{
Antoine Pitrou39a65912010-11-05 19:47:27 +0000327 int save_errno = errno;
328
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000329 /* See NOTES section above */
Antoine Pitrou39a65912010-11-05 19:47:27 +0000330 if (getpid() == main_pid)
Antoine Pitrou39a65912010-11-05 19:47:27 +0000331 {
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200332 trip_signal(sig_num);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000333 }
Antoine Pitrou39a65912010-11-05 19:47:27 +0000334
Jean-Paul Calderone6f137ca2010-05-09 03:18:57 +0000335#ifndef HAVE_SIGACTION
Antoine Pitrou39a65912010-11-05 19:47:27 +0000336#ifdef SIGCHLD
337 /* To avoid infinite recursion, this signal remains
338 reset until explicit re-instated.
339 Don't clear the 'func' field as it is our pointer
340 to the Python handler... */
341 if (sig_num != SIGCHLD)
342#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000343 /* If the handler was not set up with sigaction, reinstall it. See
Nick Coghland6009512014-11-20 21:39:37 +1000344 * Python/pylifecycle.c for the implementation of PyOS_setsig which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000345 * makes this true. See also issue8354. */
346 PyOS_setsig(sig_num, signal_handler);
Jean-Paul Calderone6f137ca2010-05-09 03:18:57 +0000347#endif
Antoine Pitrou39a65912010-11-05 19:47:27 +0000348
349 /* Issue #10311: asynchronously executing signal handlers should not
350 mutate errno under the feet of unsuspecting C code. */
351 errno = save_errno;
Antoine Pitrou6dd381e2011-11-21 21:26:56 +0100352
353#ifdef MS_WINDOWS
354 if (sig_num == SIGINT)
355 SetEvent(sigint_event);
356#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000357}
Guido van Rossume4485b01994-09-07 14:32:49 +0000358
Guido van Rossum06d511d1995-03-10 15:13:48 +0000359
Guido van Rossum1171ee61997-08-22 20:42:00 +0000360#ifdef HAVE_ALARM
Tal Einatc7027b72015-05-16 14:14:49 +0300361
362/*[clinic input]
363signal.alarm -> long
364
365 seconds: int
366 /
367
368Arrange for SIGALRM to arrive after the given number of seconds.
369[clinic start generated code]*/
370
371static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300372signal_alarm_impl(PyObject *module, int seconds)
373/*[clinic end generated code: output=144232290814c298 input=0d5e97e0e6f39e86]*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000374{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000375 /* alarm() returns the number of seconds remaining */
Tal Einatc7027b72015-05-16 14:14:49 +0300376 return (long)alarm(seconds);
Guido van Rossumaa0f4c71994-08-23 13:49:37 +0000377}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000378
Guido van Rossum06d511d1995-03-10 15:13:48 +0000379#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000380
Guido van Rossum1171ee61997-08-22 20:42:00 +0000381#ifdef HAVE_PAUSE
Tal Einatc7027b72015-05-16 14:14:49 +0300382
383/*[clinic input]
384signal.pause
385
386Wait until a signal arrives.
387[clinic start generated code]*/
388
Guido van Rossuma597dde1995-01-10 20:56:29 +0000389static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300390signal_pause_impl(PyObject *module)
391/*[clinic end generated code: output=391656788b3c3929 input=f03de0f875752062]*/
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000392{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000393 Py_BEGIN_ALLOW_THREADS
394 (void)pause();
395 Py_END_ALLOW_THREADS
396 /* make sure that any exceptions that got raised are propagated
397 * back into Python
398 */
399 if (PyErr_CheckSignals())
400 return NULL;
Barry Warsaw92971171997-01-03 00:14:25 +0000401
Tal Einatc7027b72015-05-16 14:14:49 +0300402 Py_RETURN_NONE;
Guido van Rossume4485b01994-09-07 14:32:49 +0000403}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000404
Guido van Rossum06d511d1995-03-10 15:13:48 +0000405#endif
Guido van Rossume4485b01994-09-07 14:32:49 +0000406
Vladimir Matveevc24c6c22019-01-08 01:58:25 -0800407/*[clinic input]
408signal.raise_signal
409
410 signalnum: int
411 /
412
413Send a signal to the executing process.
414[clinic start generated code]*/
415
416static PyObject *
417signal_raise_signal_impl(PyObject *module, int signalnum)
418/*[clinic end generated code: output=e2b014220aa6111d input=e90c0f9a42358de6]*/
419{
420 int err;
421 Py_BEGIN_ALLOW_THREADS
422 _Py_BEGIN_SUPPRESS_IPH
423 err = raise(signalnum);
424 _Py_END_SUPPRESS_IPH
425 Py_END_ALLOW_THREADS
426
427 if (err) {
428 return PyErr_SetFromErrno(PyExc_OSError);
429 }
430 Py_RETURN_NONE;
431}
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000432
Tal Einatc7027b72015-05-16 14:14:49 +0300433/*[clinic input]
434signal.signal
435
436 signalnum: int
437 handler: object
438 /
439
440Set the action for the given signal.
441
442The action can be SIG_DFL, SIG_IGN, or a callable Python object.
443The previous action is returned. See getsignal() for possible return values.
444
445*** IMPORTANT NOTICE ***
446A signal handler function is called with two arguments:
447the first is the signal number, the second is the interrupted stack frame.
448[clinic start generated code]*/
449
Guido van Rossume4485b01994-09-07 14:32:49 +0000450static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300451signal_signal_impl(PyObject *module, int signalnum, PyObject *handler)
452/*[clinic end generated code: output=b44cfda43780f3a1 input=deee84af5fa0432c]*/
Guido van Rossume4485b01994-09-07 14:32:49 +0000453{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000454 PyObject *old_handler;
455 void (*func)(int);
Brian Curtinef9efbd2010-08-06 19:27:32 +0000456#ifdef MS_WINDOWS
Tal Einatc7027b72015-05-16 14:14:49 +0300457 /* Validate that signalnum is one of the allowable signals */
458 switch (signalnum) {
Brian Curtinc734b312010-09-06 16:04:10 +0000459 case SIGABRT: break;
Brian Curtin9e88b5a2010-10-01 14:49:24 +0000460#ifdef SIGBREAK
461 /* Issue #10003: SIGBREAK is not documented as permitted, but works
462 and corresponds to CTRL_BREAK_EVENT. */
463 case SIGBREAK: break;
464#endif
Brian Curtinc734b312010-09-06 16:04:10 +0000465 case SIGFPE: break;
466 case SIGILL: break;
467 case SIGINT: break;
468 case SIGSEGV: break;
469 case SIGTERM: break;
470 default:
471 PyErr_SetString(PyExc_ValueError, "invalid signal value");
472 return NULL;
Brian Curtinef9efbd2010-08-06 19:27:32 +0000473 }
474#endif
Eric Snow64d6cc82019-02-23 15:40:43 -0700475 if (!is_main()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000476 PyErr_SetString(PyExc_ValueError,
477 "signal only works in main thread");
478 return NULL;
479 }
Tal Einatc7027b72015-05-16 14:14:49 +0300480 if (signalnum < 1 || signalnum >= NSIG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000481 PyErr_SetString(PyExc_ValueError,
482 "signal number out of range");
483 return NULL;
484 }
Tal Einatc7027b72015-05-16 14:14:49 +0300485 if (handler == IgnoreHandler)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000486 func = SIG_IGN;
Tal Einatc7027b72015-05-16 14:14:49 +0300487 else if (handler == DefaultHandler)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000488 func = SIG_DFL;
Tal Einatc7027b72015-05-16 14:14:49 +0300489 else if (!PyCallable_Check(handler)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000490 PyErr_SetString(PyExc_TypeError,
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000491"signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000492 return NULL;
493 }
494 else
495 func = signal_handler;
Antoine Pitrouf6f90ff2017-11-03 19:58:46 +0100496 /* Check for pending signals before changing signal handler */
Eric Snow64d6cc82019-02-23 15:40:43 -0700497 if (_PyErr_CheckSignals()) {
Antoine Pitrouf6f90ff2017-11-03 19:58:46 +0100498 return NULL;
499 }
Tal Einatc7027b72015-05-16 14:14:49 +0300500 if (PyOS_setsig(signalnum, func) == SIG_ERR) {
Victor Stinner388196e2011-05-10 17:13:00 +0200501 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000502 return NULL;
503 }
Tal Einatc7027b72015-05-16 14:14:49 +0300504 old_handler = Handlers[signalnum].func;
Tal Einatc7027b72015-05-16 14:14:49 +0300505 Py_INCREF(handler);
506 Handlers[signalnum].func = handler;
Antoine Pitrouc8c952c2013-05-04 23:16:59 +0200507 if (old_handler != NULL)
508 return old_handler;
509 else
510 Py_RETURN_NONE;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000511}
512
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000513
Tal Einatc7027b72015-05-16 14:14:49 +0300514/*[clinic input]
515signal.getsignal
516
517 signalnum: int
518 /
519
520Return the current action for the given signal.
521
522The return value can be:
523 SIG_IGN -- if the signal is being ignored
524 SIG_DFL -- if the default action for the signal is in effect
525 None -- if an unknown handler is in effect
526 anything else -- the callable Python object used as a handler
527[clinic start generated code]*/
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000528
Guido van Rossume4485b01994-09-07 14:32:49 +0000529static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300530signal_getsignal_impl(PyObject *module, int signalnum)
531/*[clinic end generated code: output=35b3e0e796fd555e input=ac23a00f19dfa509]*/
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000532{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000533 PyObject *old_handler;
Tal Einatc7027b72015-05-16 14:14:49 +0300534 if (signalnum < 1 || signalnum >= NSIG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000535 PyErr_SetString(PyExc_ValueError,
536 "signal number out of range");
537 return NULL;
538 }
Tal Einatc7027b72015-05-16 14:14:49 +0300539 old_handler = Handlers[signalnum].func;
Antoine Pitrouc8c952c2013-05-04 23:16:59 +0200540 if (old_handler != NULL) {
541 Py_INCREF(old_handler);
542 return old_handler;
543 }
544 else {
545 Py_RETURN_NONE;
546 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000547}
548
Antoine Pietri5d2a27d2018-03-12 14:42:34 +0100549
550/*[clinic input]
551signal.strsignal
552
553 signalnum: int
554 /
555
556Return the system description of the given signal.
557
558The return values can be such as "Interrupt", "Segmentation fault", etc.
559Returns None if the signal is not recognized.
560[clinic start generated code]*/
561
562static PyObject *
563signal_strsignal_impl(PyObject *module, int signalnum)
564/*[clinic end generated code: output=44e12e1e3b666261 input=b77914b03f856c74]*/
565{
566 char *res;
567
568 if (signalnum < 1 || signalnum >= NSIG) {
569 PyErr_SetString(PyExc_ValueError,
570 "signal number out of range");
571 return NULL;
572 }
573
Michael Osipov48ce4892018-08-23 15:27:19 +0200574#ifndef HAVE_STRSIGNAL
Antoine Pietri5d2a27d2018-03-12 14:42:34 +0100575 switch (signalnum) {
Michael Osipov48ce4892018-08-23 15:27:19 +0200576 /* Though being a UNIX, HP-UX does not provide strsignal(3). */
577#ifndef MS_WINDOWS
578 case SIGHUP:
579 res = "Hangup";
580 break;
581 case SIGALRM:
582 res = "Alarm clock";
583 break;
584 case SIGPIPE:
585 res = "Broken pipe";
586 break;
587 case SIGQUIT:
588 res = "Quit";
589 break;
590 case SIGCHLD:
591 res = "Child exited";
592 break;
593#endif
594 /* Custom redefinition of POSIX signals allowed on Windows. */
Antoine Pietri5d2a27d2018-03-12 14:42:34 +0100595 case SIGINT:
596 res = "Interrupt";
597 break;
598 case SIGILL:
599 res = "Illegal instruction";
600 break;
601 case SIGABRT:
602 res = "Aborted";
603 break;
604 case SIGFPE:
605 res = "Floating point exception";
606 break;
607 case SIGSEGV:
608 res = "Segmentation fault";
609 break;
610 case SIGTERM:
611 res = "Terminated";
612 break;
613 default:
614 Py_RETURN_NONE;
615 }
616#else
617 errno = 0;
618 res = strsignal(signalnum);
619
620 if (errno || res == NULL || strstr(res, "Unknown signal") != NULL)
621 Py_RETURN_NONE;
622#endif
623
624 return Py_BuildValue("s", res);
625}
626
Christian Heimes8640e742008-02-23 16:23:06 +0000627#ifdef HAVE_SIGINTERRUPT
Tal Einatc7027b72015-05-16 14:14:49 +0300628
629/*[clinic input]
630signal.siginterrupt
631
632 signalnum: int
633 flag: int
634 /
635
636Change system call restart behaviour.
637
638If flag is False, system calls will be restarted when interrupted by
639signal sig, else system calls will be interrupted.
640[clinic start generated code]*/
Christian Heimes8640e742008-02-23 16:23:06 +0000641
642static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300643signal_siginterrupt_impl(PyObject *module, int signalnum, int flag)
644/*[clinic end generated code: output=063816243d85dd19 input=4160acacca3e2099]*/
Christian Heimes8640e742008-02-23 16:23:06 +0000645{
Tal Einatc7027b72015-05-16 14:14:49 +0300646 if (signalnum < 1 || signalnum >= NSIG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000647 PyErr_SetString(PyExc_ValueError,
648 "signal number out of range");
649 return NULL;
650 }
Tal Einatc7027b72015-05-16 14:14:49 +0300651 if (siginterrupt(signalnum, flag)<0) {
Victor Stinner388196e2011-05-10 17:13:00 +0200652 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000653 return NULL;
654 }
Tal Einatc7027b72015-05-16 14:14:49 +0300655 Py_RETURN_NONE;
Christian Heimes8640e742008-02-23 16:23:06 +0000656}
657
658#endif
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000659
Tal Einatc7027b72015-05-16 14:14:49 +0300660
661static PyObject*
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800662signal_set_wakeup_fd(PyObject *self, PyObject *args, PyObject *kwds)
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000663{
Victor Stinnere134a7f2015-03-30 10:09:31 +0200664 struct _Py_stat_struct status;
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800665 static char *kwlist[] = {
666 "", "warn_on_full_buffer", NULL,
667 };
668 int warn_on_full_buffer = 1;
Victor Stinner11517102014-07-29 23:31:34 +0200669#ifdef MS_WINDOWS
670 PyObject *fdobj;
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100671 SOCKET_T sockfd, old_sockfd;
Victor Stinner11517102014-07-29 23:31:34 +0200672 int res;
673 int res_size = sizeof res;
674 PyObject *mod;
Victor Stinner11517102014-07-29 23:31:34 +0200675 int is_socket;
676
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800677 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|$p:set_wakeup_fd", kwlist,
678 &fdobj, &warn_on_full_buffer))
Victor Stinner11517102014-07-29 23:31:34 +0200679 return NULL;
680
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100681 sockfd = PyLong_AsSocket_t(fdobj);
682 if (sockfd == (SOCKET_T)(-1) && PyErr_Occurred())
Victor Stinner11517102014-07-29 23:31:34 +0200683 return NULL;
684#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000685 int fd, old_fd;
Victor Stinner11517102014-07-29 23:31:34 +0200686
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800687 if (!PyArg_ParseTupleAndKeywords(args, kwds, "i|$p:set_wakeup_fd", kwlist,
688 &fd, &warn_on_full_buffer))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000689 return NULL;
Victor Stinner11517102014-07-29 23:31:34 +0200690#endif
691
Eric Snow64d6cc82019-02-23 15:40:43 -0700692 if (!is_main()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000693 PyErr_SetString(PyExc_ValueError,
694 "set_wakeup_fd only works in main thread");
695 return NULL;
696 }
Victor Stinner0bffc942014-07-21 16:28:54 +0200697
Victor Stinner11517102014-07-29 23:31:34 +0200698#ifdef MS_WINDOWS
699 is_socket = 0;
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100700 if (sockfd != INVALID_FD) {
Victor Stinner11517102014-07-29 23:31:34 +0200701 /* Import the _socket module to call WSAStartup() */
702 mod = PyImport_ImportModuleNoBlock("_socket");
703 if (mod == NULL)
704 return NULL;
705 Py_DECREF(mod);
706
707 /* test the socket */
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100708 if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR,
Victor Stinner11517102014-07-29 23:31:34 +0200709 (char *)&res, &res_size) != 0) {
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100710 int fd, err;
711
712 err = WSAGetLastError();
Victor Stinner11517102014-07-29 23:31:34 +0200713 if (err != WSAENOTSOCK) {
714 PyErr_SetExcFromWindowsErr(PyExc_OSError, err);
715 return NULL;
716 }
717
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100718 fd = (int)sockfd;
Steve Dower940f33a2016-09-08 11:21:54 -0700719 if ((SOCKET_T)fd != sockfd) {
Victor Stinner11517102014-07-29 23:31:34 +0200720 PyErr_SetString(PyExc_ValueError, "invalid fd");
721 return NULL;
722 }
723
Victor Stinnere134a7f2015-03-30 10:09:31 +0200724 if (_Py_fstat(fd, &status) != 0)
Victor Stinner11517102014-07-29 23:31:34 +0200725 return NULL;
Victor Stinner38227602014-08-27 12:59:44 +0200726
727 /* on Windows, a file cannot be set to non-blocking mode */
Victor Stinner11517102014-07-29 23:31:34 +0200728 }
Victor Stinner38227602014-08-27 12:59:44 +0200729 else {
Victor Stinner11517102014-07-29 23:31:34 +0200730 is_socket = 1;
Victor Stinner38227602014-08-27 12:59:44 +0200731
732 /* Windows does not provide a function to test if a socket
733 is in non-blocking mode */
734 }
Victor Stinner11517102014-07-29 23:31:34 +0200735 }
736
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100737 old_sockfd = wakeup.fd;
738 wakeup.fd = sockfd;
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800739 wakeup.warn_on_full_buffer = warn_on_full_buffer;
Victor Stinner11517102014-07-29 23:31:34 +0200740 wakeup.use_send = is_socket;
741
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100742 if (old_sockfd != INVALID_FD)
743 return PyLong_FromSocket_t(old_sockfd);
Victor Stinner11517102014-07-29 23:31:34 +0200744 else
745 return PyLong_FromLong(-1);
746#else
Victor Stinnerd18ccd12014-07-24 21:58:53 +0200747 if (fd != -1) {
Victor Stinner38227602014-08-27 12:59:44 +0200748 int blocking;
749
Victor Stinnere134a7f2015-03-30 10:09:31 +0200750 if (_Py_fstat(fd, &status) != 0)
Victor Stinner11517102014-07-29 23:31:34 +0200751 return NULL;
Victor Stinner38227602014-08-27 12:59:44 +0200752
753 blocking = _Py_get_blocking(fd);
754 if (blocking < 0)
755 return NULL;
756 if (blocking) {
757 PyErr_Format(PyExc_ValueError,
758 "the fd %i must be in non-blocking mode",
759 fd);
760 return NULL;
761 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000762 }
Victor Stinner0bffc942014-07-21 16:28:54 +0200763
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800764 old_fd = wakeup.fd;
765 wakeup.fd = fd;
766 wakeup.warn_on_full_buffer = warn_on_full_buffer;
Victor Stinner0bffc942014-07-21 16:28:54 +0200767
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000768 return PyLong_FromLong(old_fd);
Victor Stinner11517102014-07-29 23:31:34 +0200769#endif
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000770}
771
772PyDoc_STRVAR(set_wakeup_fd_doc,
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800773"set_wakeup_fd(fd, *, warn_on_full_buffer=True) -> fd\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000774\n\
Victor Stinner11517102014-07-29 23:31:34 +0200775Sets the fd to be written to (with the signal number) when a signal\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000776comes in. A library can use this to wakeup select or poll.\n\
Victor Stinner11517102014-07-29 23:31:34 +0200777The previous fd or -1 is returned.\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000778\n\
779The fd must be non-blocking.");
780
781/* C API for the same, without all the error checking */
782int
783PySignal_SetWakeupFd(int fd)
784{
Victor Stinner11517102014-07-29 23:31:34 +0200785 int old_fd;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000786 if (fd < 0)
787 fd = -1;
Victor Stinner11517102014-07-29 23:31:34 +0200788
789#ifdef MS_WINDOWS
790 old_fd = Py_SAFE_DOWNCAST(wakeup.fd, SOCKET_T, int);
Victor Stinner11517102014-07-29 23:31:34 +0200791#else
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800792 old_fd = wakeup.fd;
Victor Stinner11517102014-07-29 23:31:34 +0200793#endif
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800794 wakeup.fd = fd;
795 wakeup.warn_on_full_buffer = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000796 return old_fd;
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000797}
798
799
Martin v. Löwis823725e2008-03-24 13:39:54 +0000800#ifdef HAVE_SETITIMER
Tal Einatc7027b72015-05-16 14:14:49 +0300801
802/*[clinic input]
803signal.setitimer
804
805 which: int
Victor Stinneref611c92017-10-13 13:49:43 -0700806 seconds: object
807 interval: object(c_default="NULL") = 0.0
Tal Einatc7027b72015-05-16 14:14:49 +0300808 /
809
810Sets given itimer (one of ITIMER_REAL, ITIMER_VIRTUAL or ITIMER_PROF).
811
812The timer will fire after value seconds and after that every interval seconds.
813The itimer can be cleared by setting seconds to zero.
814
815Returns old values as a tuple: (delay, interval).
816[clinic start generated code]*/
817
Martin v. Löwis823725e2008-03-24 13:39:54 +0000818static PyObject *
Victor Stinneref611c92017-10-13 13:49:43 -0700819signal_setitimer_impl(PyObject *module, int which, PyObject *seconds,
820 PyObject *interval)
821/*[clinic end generated code: output=65f9dcbddc35527b input=de43daf194e6f66f]*/
Martin v. Löwis823725e2008-03-24 13:39:54 +0000822{
Martin v. Löwis823725e2008-03-24 13:39:54 +0000823 struct itimerval new, old;
824
Victor Stinneref611c92017-10-13 13:49:43 -0700825 if (timeval_from_double(seconds, &new.it_value) < 0) {
826 return NULL;
827 }
828 if (timeval_from_double(interval, &new.it_interval) < 0) {
829 return NULL;
830 }
831
Martin v. Löwis823725e2008-03-24 13:39:54 +0000832 /* Let OS check "which" value */
833 if (setitimer(which, &new, &old) != 0) {
Tal Einatc7027b72015-05-16 14:14:49 +0300834 PyErr_SetFromErrno(ItimerError);
835 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000836 }
837
838 return itimer_retval(&old);
839}
840
Martin v. Löwis823725e2008-03-24 13:39:54 +0000841#endif
842
843
844#ifdef HAVE_GETITIMER
Tal Einatc7027b72015-05-16 14:14:49 +0300845
846/*[clinic input]
847signal.getitimer
848
849 which: int
850 /
851
852Returns current value of given itimer.
853[clinic start generated code]*/
854
Martin v. Löwis823725e2008-03-24 13:39:54 +0000855static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300856signal_getitimer_impl(PyObject *module, int which)
857/*[clinic end generated code: output=9e053175d517db40 input=f7d21d38f3490627]*/
Martin v. Löwis823725e2008-03-24 13:39:54 +0000858{
Martin v. Löwis823725e2008-03-24 13:39:54 +0000859 struct itimerval old;
860
Martin v. Löwis823725e2008-03-24 13:39:54 +0000861 if (getitimer(which, &old) != 0) {
Tal Einatc7027b72015-05-16 14:14:49 +0300862 PyErr_SetFromErrno(ItimerError);
863 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000864 }
865
866 return itimer_retval(&old);
867}
868
Martin v. Löwis823725e2008-03-24 13:39:54 +0000869#endif
870
Victor Stinnerb3e72192011-05-08 01:46:11 +0200871#if defined(PYPTHREAD_SIGMASK) || defined(HAVE_SIGPENDING)
Victor Stinner35b300c2011-05-04 13:20:35 +0200872static PyObject*
873sigset_to_set(sigset_t mask)
874{
875 PyObject *signum, *result;
876 int sig;
877
878 result = PySet_New(0);
879 if (result == NULL)
880 return NULL;
881
882 for (sig = 1; sig < NSIG; sig++) {
883 if (sigismember(&mask, sig) != 1)
884 continue;
885
886 /* Handle the case where it is a member by adding the signal to
887 the result list. Ignore the other cases because they mean the
888 signal isn't a member of the mask or the signal was invalid,
889 and an invalid signal must have been our fault in constructing
890 the loop boundaries. */
891 signum = PyLong_FromLong(sig);
892 if (signum == NULL) {
893 Py_DECREF(result);
894 return NULL;
895 }
896 if (PySet_Add(result, signum) == -1) {
897 Py_DECREF(signum);
898 Py_DECREF(result);
899 return NULL;
900 }
901 Py_DECREF(signum);
902 }
903 return result;
904}
Victor Stinnerb3e72192011-05-08 01:46:11 +0200905#endif
Victor Stinner35b300c2011-05-04 13:20:35 +0200906
Victor Stinnerb3e72192011-05-08 01:46:11 +0200907#ifdef PYPTHREAD_SIGMASK
Tal Einatc7027b72015-05-16 14:14:49 +0300908
909/*[clinic input]
910signal.pthread_sigmask
911
912 how: int
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300913 mask: sigset_t
Tal Einatc7027b72015-05-16 14:14:49 +0300914 /
915
916Fetch and/or change the signal mask of the calling thread.
917[clinic start generated code]*/
918
Victor Stinnera9293352011-04-30 15:21:58 +0200919static PyObject *
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300920signal_pthread_sigmask_impl(PyObject *module, int how, sigset_t mask)
921/*[clinic end generated code: output=0562c0fb192981a8 input=85bcebda442fa77f]*/
Victor Stinnera9293352011-04-30 15:21:58 +0200922{
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300923 sigset_t previous;
Victor Stinnera9293352011-04-30 15:21:58 +0200924 int err;
925
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300926 err = pthread_sigmask(how, &mask, &previous);
Victor Stinnera9293352011-04-30 15:21:58 +0200927 if (err != 0) {
928 errno = err;
Victor Stinnerb3e72192011-05-08 01:46:11 +0200929 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnera9293352011-04-30 15:21:58 +0200930 return NULL;
931 }
932
Victor Stinnerd0e516d2011-05-03 14:57:12 +0200933 /* if signals was unblocked, signal handlers have been called */
934 if (PyErr_CheckSignals())
935 return NULL;
936
Victor Stinner35b300c2011-05-04 13:20:35 +0200937 return sigset_to_set(previous);
Victor Stinnera9293352011-04-30 15:21:58 +0200938}
939
Victor Stinnera9293352011-04-30 15:21:58 +0200940#endif /* #ifdef PYPTHREAD_SIGMASK */
941
Martin v. Löwis823725e2008-03-24 13:39:54 +0000942
Victor Stinnerb3e72192011-05-08 01:46:11 +0200943#ifdef HAVE_SIGPENDING
Tal Einatc7027b72015-05-16 14:14:49 +0300944
945/*[clinic input]
946signal.sigpending
947
948Examine pending signals.
949
950Returns a set of signal numbers that are pending for delivery to
951the calling thread.
952[clinic start generated code]*/
953
Victor Stinnerb3e72192011-05-08 01:46:11 +0200954static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300955signal_sigpending_impl(PyObject *module)
956/*[clinic end generated code: output=53375ffe89325022 input=e0036c016f874e29]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +0200957{
958 int err;
959 sigset_t mask;
960 err = sigpending(&mask);
961 if (err)
962 return PyErr_SetFromErrno(PyExc_OSError);
963 return sigset_to_set(mask);
964}
965
Victor Stinnerb3e72192011-05-08 01:46:11 +0200966#endif /* #ifdef HAVE_SIGPENDING */
967
968
969#ifdef HAVE_SIGWAIT
Tal Einatc7027b72015-05-16 14:14:49 +0300970
971/*[clinic input]
972signal.sigwait
973
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300974 sigset: sigset_t
Tal Einatc7027b72015-05-16 14:14:49 +0300975 /
976
977Wait for a signal.
978
979Suspend execution of the calling thread until the delivery of one of the
980signals specified in the signal set sigset. The function accepts the signal
981and returns the signal number.
982[clinic start generated code]*/
983
Victor Stinnerb3e72192011-05-08 01:46:11 +0200984static PyObject *
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300985signal_sigwait_impl(PyObject *module, sigset_t sigset)
986/*[clinic end generated code: output=f43770699d682f96 input=a6fbd47b1086d119]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +0200987{
Victor Stinnerb3e72192011-05-08 01:46:11 +0200988 int err, signum;
989
Victor Stinner10c30d62011-06-10 01:39:53 +0200990 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300991 err = sigwait(&sigset, &signum);
Victor Stinner10c30d62011-06-10 01:39:53 +0200992 Py_END_ALLOW_THREADS
Victor Stinnerb3e72192011-05-08 01:46:11 +0200993 if (err) {
994 errno = err;
995 return PyErr_SetFromErrno(PyExc_OSError);
996 }
997
998 return PyLong_FromLong(signum);
999}
1000
Tal Einatc7027b72015-05-16 14:14:49 +03001001#endif /* #ifdef HAVE_SIGWAIT */
1002
Victor Stinnerb3e72192011-05-08 01:46:11 +02001003
Antoine Pitrou9d3627e2018-05-04 13:00:50 +02001004#if defined(HAVE_SIGFILLSET) || defined(MS_WINDOWS)
1005
1006/*[clinic input]
1007signal.valid_signals
1008
1009Return a set of valid signal numbers on this platform.
1010
1011The signal numbers returned by this function can be safely passed to
1012functions like `pthread_sigmask`.
1013[clinic start generated code]*/
1014
1015static PyObject *
1016signal_valid_signals_impl(PyObject *module)
1017/*[clinic end generated code: output=1609cffbcfcf1314 input=86a3717ff25288f2]*/
1018{
1019#ifdef MS_WINDOWS
1020#ifdef SIGBREAK
1021 PyObject *tup = Py_BuildValue("(iiiiiii)", SIGABRT, SIGBREAK, SIGFPE,
1022 SIGILL, SIGINT, SIGSEGV, SIGTERM);
1023#else
1024 PyObject *tup = Py_BuildValue("(iiiiii)", SIGABRT, SIGFPE, SIGILL,
1025 SIGINT, SIGSEGV, SIGTERM);
1026#endif
1027 if (tup == NULL) {
1028 return NULL;
1029 }
1030 PyObject *set = PySet_New(tup);
1031 Py_DECREF(tup);
1032 return set;
1033#else
1034 sigset_t mask;
1035 if (sigemptyset(&mask) || sigfillset(&mask)) {
1036 return PyErr_SetFromErrno(PyExc_OSError);
1037 }
1038 return sigset_to_set(mask);
1039#endif
1040}
1041
1042#endif /* #if defined(HAVE_SIGFILLSET) || defined(MS_WINDOWS) */
1043
1044
Ross Lagerwallbc808222011-06-25 12:13:40 +02001045#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
1046static int initialized;
1047static PyStructSequence_Field struct_siginfo_fields[] = {
1048 {"si_signo", "signal number"},
1049 {"si_code", "signal code"},
1050 {"si_errno", "errno associated with this signal"},
1051 {"si_pid", "sending process ID"},
1052 {"si_uid", "real user ID of sending process"},
1053 {"si_status", "exit value or signal"},
1054 {"si_band", "band event for SIGPOLL"},
1055 {0}
1056};
1057
1058PyDoc_STRVAR(struct_siginfo__doc__,
1059"struct_siginfo: Result from sigwaitinfo or sigtimedwait.\n\n\
1060This object may be accessed either as a tuple of\n\
1061(si_signo, si_code, si_errno, si_pid, si_uid, si_status, si_band),\n\
1062or via the attributes si_signo, si_code, and so on.");
1063
1064static PyStructSequence_Desc struct_siginfo_desc = {
1065 "signal.struct_siginfo", /* name */
1066 struct_siginfo__doc__, /* doc */
1067 struct_siginfo_fields, /* fields */
1068 7 /* n_in_sequence */
1069};
1070
1071static PyTypeObject SiginfoType;
1072
1073static PyObject *
1074fill_siginfo(siginfo_t *si)
1075{
1076 PyObject *result = PyStructSequence_New(&SiginfoType);
1077 if (!result)
1078 return NULL;
1079
1080 PyStructSequence_SET_ITEM(result, 0, PyLong_FromLong((long)(si->si_signo)));
1081 PyStructSequence_SET_ITEM(result, 1, PyLong_FromLong((long)(si->si_code)));
pxinwr8b5bdda2019-03-14 01:18:25 +08001082#ifdef __VXWORKS__
1083 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong(0L));
1084 PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong(0L));
1085 PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong(0L));
1086 PyStructSequence_SET_ITEM(result, 5, PyLong_FromLong(0L));
1087#else
Ross Lagerwallbc808222011-06-25 12:13:40 +02001088 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si->si_errno)));
1089 PyStructSequence_SET_ITEM(result, 3, PyLong_FromPid(si->si_pid));
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02001090 PyStructSequence_SET_ITEM(result, 4, _PyLong_FromUid(si->si_uid));
Ross Lagerwallbc808222011-06-25 12:13:40 +02001091 PyStructSequence_SET_ITEM(result, 5,
1092 PyLong_FromLong((long)(si->si_status)));
pxinwr8b5bdda2019-03-14 01:18:25 +08001093#endif
Zachary Ware6a6967e2016-10-01 00:47:27 -05001094#ifdef HAVE_SIGINFO_T_SI_BAND
Ross Lagerwallbc808222011-06-25 12:13:40 +02001095 PyStructSequence_SET_ITEM(result, 6, PyLong_FromLong(si->si_band));
Zachary Ware6a6967e2016-10-01 00:47:27 -05001096#else
1097 PyStructSequence_SET_ITEM(result, 6, PyLong_FromLong(0L));
1098#endif
Ross Lagerwallbc808222011-06-25 12:13:40 +02001099 if (PyErr_Occurred()) {
1100 Py_DECREF(result);
1101 return NULL;
1102 }
1103
1104 return result;
1105}
1106#endif
1107
1108#ifdef HAVE_SIGWAITINFO
Tal Einatc7027b72015-05-16 14:14:49 +03001109
1110/*[clinic input]
1111signal.sigwaitinfo
1112
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001113 sigset: sigset_t
Tal Einatc7027b72015-05-16 14:14:49 +03001114 /
1115
1116Wait synchronously until one of the signals in *sigset* is delivered.
1117
1118Returns a struct_siginfo containing information about the signal.
1119[clinic start generated code]*/
1120
Ross Lagerwallbc808222011-06-25 12:13:40 +02001121static PyObject *
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001122signal_sigwaitinfo_impl(PyObject *module, sigset_t sigset)
1123/*[clinic end generated code: output=1eb2f1fa236fdbca input=3d1a7e1f27fc664c]*/
Ross Lagerwallbc808222011-06-25 12:13:40 +02001124{
Ross Lagerwallbc808222011-06-25 12:13:40 +02001125 siginfo_t si;
1126 int err;
Victor Stinnera453cd82015-03-20 12:54:28 +01001127 int async_err = 0;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001128
Victor Stinnera453cd82015-03-20 12:54:28 +01001129 do {
1130 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001131 err = sigwaitinfo(&sigset, &si);
Victor Stinnera453cd82015-03-20 12:54:28 +01001132 Py_END_ALLOW_THREADS
1133 } while (err == -1
1134 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Ross Lagerwallbc808222011-06-25 12:13:40 +02001135 if (err == -1)
Victor Stinnera453cd82015-03-20 12:54:28 +01001136 return (!async_err) ? PyErr_SetFromErrno(PyExc_OSError) : NULL;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001137
1138 return fill_siginfo(&si);
1139}
1140
Ross Lagerwallbc808222011-06-25 12:13:40 +02001141#endif /* #ifdef HAVE_SIGWAITINFO */
1142
1143#ifdef HAVE_SIGTIMEDWAIT
Tal Einatc7027b72015-05-16 14:14:49 +03001144
1145/*[clinic input]
1146signal.sigtimedwait
1147
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001148 sigset: sigset_t
Serhiy Storchaka6b680cd2015-05-16 15:57:56 +03001149 timeout as timeout_obj: object
Tal Einatc7027b72015-05-16 14:14:49 +03001150 /
1151
1152Like sigwaitinfo(), but with a timeout.
1153
1154The timeout is specified in seconds, with floating point numbers allowed.
1155[clinic start generated code]*/
1156
Ross Lagerwallbc808222011-06-25 12:13:40 +02001157static PyObject *
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001158signal_sigtimedwait_impl(PyObject *module, sigset_t sigset,
Serhiy Storchaka6b680cd2015-05-16 15:57:56 +03001159 PyObject *timeout_obj)
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001160/*[clinic end generated code: output=59c8971e8ae18a64 input=87fd39237cf0b7ba]*/
Ross Lagerwallbc808222011-06-25 12:13:40 +02001161{
Victor Stinnera453cd82015-03-20 12:54:28 +01001162 struct timespec ts;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001163 siginfo_t si;
Victor Stinnera453cd82015-03-20 12:54:28 +01001164 int res;
Victor Stinner34dc0f42015-03-27 18:19:03 +01001165 _PyTime_t timeout, deadline, monotonic;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001166
Victor Stinner869e1772015-03-30 03:49:14 +02001167 if (_PyTime_FromSecondsObject(&timeout,
1168 timeout_obj, _PyTime_ROUND_CEILING) < 0)
Ross Lagerwallbc808222011-06-25 12:13:40 +02001169 return NULL;
1170
Victor Stinnera453cd82015-03-20 12:54:28 +01001171 if (timeout < 0) {
Ross Lagerwallbc808222011-06-25 12:13:40 +02001172 PyErr_SetString(PyExc_ValueError, "timeout must be non-negative");
1173 return NULL;
1174 }
1175
Victor Stinner34dc0f42015-03-27 18:19:03 +01001176 deadline = _PyTime_GetMonotonicClock() + timeout;
Victor Stinnera453cd82015-03-20 12:54:28 +01001177
1178 do {
Victor Stinner34dc0f42015-03-27 18:19:03 +01001179 if (_PyTime_AsTimespec(timeout, &ts) < 0)
1180 return NULL;
Victor Stinnera453cd82015-03-20 12:54:28 +01001181
1182 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001183 res = sigtimedwait(&sigset, &si, &ts);
Victor Stinnera453cd82015-03-20 12:54:28 +01001184 Py_END_ALLOW_THREADS
1185
1186 if (res != -1)
1187 break;
1188
1189 if (errno != EINTR) {
1190 if (errno == EAGAIN)
1191 Py_RETURN_NONE;
1192 else
1193 return PyErr_SetFromErrno(PyExc_OSError);
1194 }
1195
1196 /* sigtimedwait() was interrupted by a signal (EINTR) */
1197 if (PyErr_CheckSignals())
1198 return NULL;
1199
Victor Stinner34dc0f42015-03-27 18:19:03 +01001200 monotonic = _PyTime_GetMonotonicClock();
1201 timeout = deadline - monotonic;
Victor Stinner6aa446c2015-03-30 21:33:51 +02001202 if (timeout < 0)
Victor Stinnera453cd82015-03-20 12:54:28 +01001203 break;
1204 } while (1);
Ross Lagerwallbc808222011-06-25 12:13:40 +02001205
1206 return fill_siginfo(&si);
1207}
1208
Ross Lagerwallbc808222011-06-25 12:13:40 +02001209#endif /* #ifdef HAVE_SIGTIMEDWAIT */
1210
Victor Stinnerb3e72192011-05-08 01:46:11 +02001211
Antoine Pitroua6a4dc82017-09-07 18:56:24 +02001212#if defined(HAVE_PTHREAD_KILL)
Tal Einatc7027b72015-05-16 14:14:49 +03001213
1214/*[clinic input]
1215signal.pthread_kill
1216
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +02001217 thread_id: unsigned_long(bitwise=True)
Tal Einatc7027b72015-05-16 14:14:49 +03001218 signalnum: int
1219 /
1220
1221Send a signal to a thread.
1222[clinic start generated code]*/
1223
Victor Stinnerb3e72192011-05-08 01:46:11 +02001224static PyObject *
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +02001225signal_pthread_kill_impl(PyObject *module, unsigned long thread_id,
1226 int signalnum)
1227/*[clinic end generated code: output=7629919b791bc27f input=1d901f2c7bb544ff]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +02001228{
Victor Stinnerb3e72192011-05-08 01:46:11 +02001229 int err;
1230
Tal Einatc7027b72015-05-16 14:14:49 +03001231 err = pthread_kill((pthread_t)thread_id, signalnum);
Victor Stinnerb3e72192011-05-08 01:46:11 +02001232 if (err != 0) {
1233 errno = err;
1234 PyErr_SetFromErrno(PyExc_OSError);
1235 return NULL;
1236 }
1237
1238 /* the signal may have been send to the current thread */
1239 if (PyErr_CheckSignals())
1240 return NULL;
1241
1242 Py_RETURN_NONE;
1243}
1244
Antoine Pitroua6a4dc82017-09-07 18:56:24 +02001245#endif /* #if defined(HAVE_PTHREAD_KILL) */
Victor Stinnerb3e72192011-05-08 01:46:11 +02001246
1247
1248
Tal Einatc7027b72015-05-16 14:14:49 +03001249/* List of functions defined in the module -- some of the methoddefs are
1250 defined to nothing if the corresponding C function is not available. */
Barry Warsaw92971171997-01-03 00:14:25 +00001251static PyMethodDef signal_methods[] = {
Tal Einatc7027b72015-05-16 14:14:49 +03001252 {"default_int_handler", signal_default_int_handler, METH_VARARGS, default_int_handler_doc},
1253 SIGNAL_ALARM_METHODDEF
1254 SIGNAL_SETITIMER_METHODDEF
1255 SIGNAL_GETITIMER_METHODDEF
1256 SIGNAL_SIGNAL_METHODDEF
Vladimir Matveevc24c6c22019-01-08 01:58:25 -08001257 SIGNAL_RAISE_SIGNAL_METHODDEF
Antoine Pietri5d2a27d2018-03-12 14:42:34 +01001258 SIGNAL_STRSIGNAL_METHODDEF
Tal Einatc7027b72015-05-16 14:14:49 +03001259 SIGNAL_GETSIGNAL_METHODDEF
Serhiy Storchaka62be7422018-11-27 13:27:31 +02001260 {"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 +03001261 SIGNAL_SIGINTERRUPT_METHODDEF
1262 SIGNAL_PAUSE_METHODDEF
1263 SIGNAL_PTHREAD_KILL_METHODDEF
1264 SIGNAL_PTHREAD_SIGMASK_METHODDEF
1265 SIGNAL_SIGPENDING_METHODDEF
1266 SIGNAL_SIGWAIT_METHODDEF
1267 SIGNAL_SIGWAITINFO_METHODDEF
1268 SIGNAL_SIGTIMEDWAIT_METHODDEF
Antoine Pitrou9d3627e2018-05-04 13:00:50 +02001269#if defined(HAVE_SIGFILLSET) || defined(MS_WINDOWS)
1270 SIGNAL_VALID_SIGNALS_METHODDEF
1271#endif
Tal Einatc7027b72015-05-16 14:14:49 +03001272 {NULL, NULL} /* sentinel */
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001273};
1274
Barry Warsaw92971171997-01-03 00:14:25 +00001275
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001276PyDoc_STRVAR(module_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001277"This module provides mechanisms to use signal handlers in Python.\n\
1278\n\
1279Functions:\n\
1280\n\
1281alarm() -- cause SIGALRM after a specified time [Unix only]\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001282setitimer() -- cause a signal (described below) after a specified\n\
1283 float time and the timer may restart then [Unix only]\n\
1284getitimer() -- get current value of timer [Unix only]\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001285signal() -- set the action for a given signal\n\
1286getsignal() -- get the signal action for a given signal\n\
1287pause() -- wait until a signal arrives [Unix only]\n\
1288default_int_handler() -- default SIGINT handler\n\
1289\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001290signal constants:\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001291SIG_DFL -- used to refer to the system default handler\n\
1292SIG_IGN -- used to ignore the signal\n\
1293NSIG -- number of defined signals\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001294SIGINT, SIGTERM, etc. -- signal numbers\n\
1295\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001296itimer constants:\n\
1297ITIMER_REAL -- decrements in real time, and delivers SIGALRM upon\n\
1298 expiration\n\
1299ITIMER_VIRTUAL -- decrements only when the process is executing,\n\
1300 and delivers SIGVTALRM upon expiration\n\
1301ITIMER_PROF -- decrements both when the process is executing and\n\
1302 when the system is executing on behalf of the process.\n\
1303 Coupled with ITIMER_VIRTUAL, this timer is usually\n\
1304 used to profile the time spent by the application\n\
1305 in user and kernel space. SIGPROF is delivered upon\n\
1306 expiration.\n\
1307\n\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001308*** IMPORTANT NOTICE ***\n\
1309A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001310the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001311
Martin v. Löwis1a214512008-06-11 05:26:20 +00001312static struct PyModuleDef signalmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001313 PyModuleDef_HEAD_INIT,
Brett Cannon815a6f32014-04-04 10:20:28 -04001314 "_signal",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001315 module_doc,
1316 -1,
1317 signal_methods,
1318 NULL,
1319 NULL,
1320 NULL,
1321 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001322};
1323
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001324PyMODINIT_FUNC
Giampaolo Rodola'e09fb712014-04-04 15:34:17 +02001325PyInit__signal(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001326{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001327 PyObject *m, *d, *x;
1328 int i;
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001329
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001330 main_thread = PyThread_get_thread_ident();
1331 main_pid = getpid();
Eric Snow64d6cc82019-02-23 15:40:43 -07001332 main_interp = _PyInterpreterState_Get();
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001333
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001334 /* Create the module and add the functions */
1335 m = PyModule_Create(&signalmodule);
1336 if (m == NULL)
1337 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001338
Ross Lagerwallbc808222011-06-25 12:13:40 +02001339#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
Victor Stinner1c8f0592013-07-22 22:24:54 +02001340 if (!initialized) {
1341 if (PyStructSequence_InitType2(&SiginfoType, &struct_siginfo_desc) < 0)
1342 return NULL;
1343 }
Ross Lagerwallbc808222011-06-25 12:13:40 +02001344 Py_INCREF((PyObject*) &SiginfoType);
1345 PyModule_AddObject(m, "struct_siginfo", (PyObject*) &SiginfoType);
1346 initialized = 1;
1347#endif
1348
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001349 /* Add some symbolic constants to the module */
1350 d = PyModule_GetDict(m);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001351
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001352 x = DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL);
1353 if (!x || PyDict_SetItemString(d, "SIG_DFL", x) < 0)
1354 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001355
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001356 x = IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN);
1357 if (!x || PyDict_SetItemString(d, "SIG_IGN", x) < 0)
1358 goto finally;
Barry Warsaw92971171997-01-03 00:14:25 +00001359
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001360 x = PyLong_FromLong((long)NSIG);
1361 if (!x || PyDict_SetItemString(d, "NSIG", x) < 0)
1362 goto finally;
1363 Py_DECREF(x);
Barry Warsaw92971171997-01-03 00:14:25 +00001364
Victor Stinnera9293352011-04-30 15:21:58 +02001365#ifdef SIG_BLOCK
Victor Stinner72c53b52011-05-02 16:15:43 +02001366 if (PyModule_AddIntMacro(m, SIG_BLOCK))
1367 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001368#endif
Victor Stinnera9293352011-04-30 15:21:58 +02001369#ifdef SIG_UNBLOCK
Victor Stinner72c53b52011-05-02 16:15:43 +02001370 if (PyModule_AddIntMacro(m, SIG_UNBLOCK))
1371 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001372#endif
Victor Stinnera9293352011-04-30 15:21:58 +02001373#ifdef SIG_SETMASK
Victor Stinner72c53b52011-05-02 16:15:43 +02001374 if (PyModule_AddIntMacro(m, SIG_SETMASK))
1375 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001376#endif
1377
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001378 x = IntHandler = PyDict_GetItemString(d, "default_int_handler");
1379 if (!x)
1380 goto finally;
1381 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +00001382
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001383 _Py_atomic_store_relaxed(&Handlers[0].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001384 for (i = 1; i < NSIG; i++) {
1385 void (*t)(int);
1386 t = PyOS_getsig(i);
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001387 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001388 if (t == SIG_DFL)
1389 Handlers[i].func = DefaultHandler;
1390 else if (t == SIG_IGN)
1391 Handlers[i].func = IgnoreHandler;
1392 else
1393 Handlers[i].func = Py_None; /* None of our business */
1394 Py_INCREF(Handlers[i].func);
1395 }
1396 if (Handlers[SIGINT].func == DefaultHandler) {
1397 /* Install default int handler */
1398 Py_INCREF(IntHandler);
Serhiy Storchaka57a01d32016-04-10 18:05:40 +03001399 Py_SETREF(Handlers[SIGINT].func, IntHandler);
pkerlinge905c842018-06-01 09:47:18 +00001400 PyOS_setsig(SIGINT, signal_handler);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001401 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001402
1403#ifdef SIGHUP
Christian Heimes6782b142016-09-09 00:11:45 +02001404 if (PyModule_AddIntMacro(m, SIGHUP))
1405 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001406#endif
1407#ifdef SIGINT
Christian Heimes6782b142016-09-09 00:11:45 +02001408 if (PyModule_AddIntMacro(m, SIGINT))
1409 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001410#endif
Tim Peters1ce3cf72001-10-01 17:58:40 +00001411#ifdef SIGBREAK
Christian Heimes6782b142016-09-09 00:11:45 +02001412 if (PyModule_AddIntMacro(m, SIGBREAK))
1413 goto finally;
Tim Peters1ce3cf72001-10-01 17:58:40 +00001414#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001415#ifdef SIGQUIT
Christian Heimes6782b142016-09-09 00:11:45 +02001416 if (PyModule_AddIntMacro(m, SIGQUIT))
1417 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001418#endif
1419#ifdef SIGILL
Christian Heimes6782b142016-09-09 00:11:45 +02001420 if (PyModule_AddIntMacro(m, SIGILL))
1421 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001422#endif
1423#ifdef SIGTRAP
Christian Heimes6782b142016-09-09 00:11:45 +02001424 if (PyModule_AddIntMacro(m, SIGTRAP))
1425 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001426#endif
1427#ifdef SIGIOT
Christian Heimes6782b142016-09-09 00:11:45 +02001428 if (PyModule_AddIntMacro(m, SIGIOT))
1429 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001430#endif
1431#ifdef SIGABRT
Christian Heimes6782b142016-09-09 00:11:45 +02001432 if (PyModule_AddIntMacro(m, SIGABRT))
1433 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001434#endif
1435#ifdef SIGEMT
Christian Heimes6782b142016-09-09 00:11:45 +02001436 if (PyModule_AddIntMacro(m, SIGEMT))
1437 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001438#endif
1439#ifdef SIGFPE
Christian Heimes6782b142016-09-09 00:11:45 +02001440 if (PyModule_AddIntMacro(m, SIGFPE))
1441 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001442#endif
1443#ifdef SIGKILL
Christian Heimes6782b142016-09-09 00:11:45 +02001444 if (PyModule_AddIntMacro(m, SIGKILL))
1445 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001446#endif
1447#ifdef SIGBUS
Christian Heimes6782b142016-09-09 00:11:45 +02001448 if (PyModule_AddIntMacro(m, SIGBUS))
1449 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001450#endif
1451#ifdef SIGSEGV
Christian Heimes6782b142016-09-09 00:11:45 +02001452 if (PyModule_AddIntMacro(m, SIGSEGV))
1453 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001454#endif
1455#ifdef SIGSYS
Christian Heimes6782b142016-09-09 00:11:45 +02001456 if (PyModule_AddIntMacro(m, SIGSYS))
1457 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001458#endif
1459#ifdef SIGPIPE
Christian Heimes6782b142016-09-09 00:11:45 +02001460 if (PyModule_AddIntMacro(m, SIGPIPE))
1461 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001462#endif
1463#ifdef SIGALRM
Christian Heimes6782b142016-09-09 00:11:45 +02001464 if (PyModule_AddIntMacro(m, SIGALRM))
1465 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001466#endif
1467#ifdef SIGTERM
Christian Heimes6782b142016-09-09 00:11:45 +02001468 if (PyModule_AddIntMacro(m, SIGTERM))
1469 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001470#endif
1471#ifdef SIGUSR1
Christian Heimes6782b142016-09-09 00:11:45 +02001472 if (PyModule_AddIntMacro(m, SIGUSR1))
1473 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001474#endif
1475#ifdef SIGUSR2
Christian Heimes6782b142016-09-09 00:11:45 +02001476 if (PyModule_AddIntMacro(m, SIGUSR2))
1477 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001478#endif
1479#ifdef SIGCLD
Christian Heimes6782b142016-09-09 00:11:45 +02001480 if (PyModule_AddIntMacro(m, SIGCLD))
1481 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001482#endif
1483#ifdef SIGCHLD
Christian Heimes6782b142016-09-09 00:11:45 +02001484 if (PyModule_AddIntMacro(m, SIGCHLD))
1485 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001486#endif
1487#ifdef SIGPWR
Christian Heimes6782b142016-09-09 00:11:45 +02001488 if (PyModule_AddIntMacro(m, SIGPWR))
1489 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001490#endif
1491#ifdef SIGIO
Christian Heimes6782b142016-09-09 00:11:45 +02001492 if (PyModule_AddIntMacro(m, SIGIO))
1493 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001494#endif
1495#ifdef SIGURG
Christian Heimes6782b142016-09-09 00:11:45 +02001496 if (PyModule_AddIntMacro(m, SIGURG))
1497 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001498#endif
1499#ifdef SIGWINCH
Christian Heimes6782b142016-09-09 00:11:45 +02001500 if (PyModule_AddIntMacro(m, SIGWINCH))
1501 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001502#endif
1503#ifdef SIGPOLL
Christian Heimes6782b142016-09-09 00:11:45 +02001504 if (PyModule_AddIntMacro(m, SIGPOLL))
1505 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001506#endif
1507#ifdef SIGSTOP
Christian Heimes6782b142016-09-09 00:11:45 +02001508 if (PyModule_AddIntMacro(m, SIGSTOP))
1509 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001510#endif
1511#ifdef SIGTSTP
Christian Heimes6782b142016-09-09 00:11:45 +02001512 if (PyModule_AddIntMacro(m, SIGTSTP))
1513 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001514#endif
1515#ifdef SIGCONT
Christian Heimes6782b142016-09-09 00:11:45 +02001516 if (PyModule_AddIntMacro(m, SIGCONT))
1517 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001518#endif
1519#ifdef SIGTTIN
Christian Heimes6782b142016-09-09 00:11:45 +02001520 if (PyModule_AddIntMacro(m, SIGTTIN))
1521 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001522#endif
1523#ifdef SIGTTOU
Christian Heimes6782b142016-09-09 00:11:45 +02001524 if (PyModule_AddIntMacro(m, SIGTTOU))
1525 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001526#endif
1527#ifdef SIGVTALRM
Christian Heimes6782b142016-09-09 00:11:45 +02001528 if (PyModule_AddIntMacro(m, SIGVTALRM))
1529 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001530#endif
1531#ifdef SIGPROF
Christian Heimes6782b142016-09-09 00:11:45 +02001532 if (PyModule_AddIntMacro(m, SIGPROF))
1533 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001534#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001535#ifdef SIGXCPU
Christian Heimes6782b142016-09-09 00:11:45 +02001536 if (PyModule_AddIntMacro(m, SIGXCPU))
1537 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001538#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001539#ifdef SIGXFSZ
Christian Heimes6782b142016-09-09 00:11:45 +02001540 if (PyModule_AddIntMacro(m, SIGXFSZ))
1541 goto finally;
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001542#endif
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001543#ifdef SIGRTMIN
Christian Heimes6782b142016-09-09 00:11:45 +02001544 if (PyModule_AddIntMacro(m, SIGRTMIN))
1545 goto finally;
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001546#endif
1547#ifdef SIGRTMAX
Christian Heimes6782b142016-09-09 00:11:45 +02001548 if (PyModule_AddIntMacro(m, SIGRTMAX))
1549 goto finally;
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001550#endif
Martin v. Löwis175af252002-01-12 11:43:25 +00001551#ifdef SIGINFO
Christian Heimes6782b142016-09-09 00:11:45 +02001552 if (PyModule_AddIntMacro(m, SIGINFO))
1553 goto finally;
Martin v. Löwis175af252002-01-12 11:43:25 +00001554#endif
Martin v. Löwis823725e2008-03-24 13:39:54 +00001555
1556#ifdef ITIMER_REAL
Christian Heimes6782b142016-09-09 00:11:45 +02001557 if (PyModule_AddIntMacro(m, ITIMER_REAL))
1558 goto finally;
Martin v. Löwis823725e2008-03-24 13:39:54 +00001559#endif
1560#ifdef ITIMER_VIRTUAL
Christian Heimes6782b142016-09-09 00:11:45 +02001561 if (PyModule_AddIntMacro(m, ITIMER_VIRTUAL))
1562 goto finally;
Martin v. Löwis823725e2008-03-24 13:39:54 +00001563#endif
1564#ifdef ITIMER_PROF
Christian Heimes6782b142016-09-09 00:11:45 +02001565 if (PyModule_AddIntMacro(m, ITIMER_PROF))
1566 goto finally;
Martin v. Löwis823725e2008-03-24 13:39:54 +00001567#endif
1568
1569#if defined (HAVE_SETITIMER) || defined (HAVE_GETITIMER)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001570 ItimerError = PyErr_NewException("signal.ItimerError",
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +03001571 PyExc_OSError, NULL);
Neal Norwitz2f99b242008-08-24 05:48:10 +00001572 if (ItimerError != NULL)
Martin Panter6d57fe12016-09-17 03:26:16 +00001573 PyDict_SetItemString(d, "ItimerError", ItimerError);
Martin v. Löwis823725e2008-03-24 13:39:54 +00001574#endif
1575
Brian Curtineb24d742010-04-12 17:16:38 +00001576#ifdef CTRL_C_EVENT
Christian Heimes6782b142016-09-09 00:11:45 +02001577 if (PyModule_AddIntMacro(m, CTRL_C_EVENT))
1578 goto finally;
Brian Curtineb24d742010-04-12 17:16:38 +00001579#endif
1580
1581#ifdef CTRL_BREAK_EVENT
Christian Heimes6782b142016-09-09 00:11:45 +02001582 if (PyModule_AddIntMacro(m, CTRL_BREAK_EVENT))
1583 goto finally;
Brian Curtineb24d742010-04-12 17:16:38 +00001584#endif
1585
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001586#ifdef MS_WINDOWS
1587 /* Create manual-reset event, initially unset */
1588 sigint_event = CreateEvent(NULL, TRUE, FALSE, FALSE);
1589#endif
1590
Martin v. Löwis1a214512008-06-11 05:26:20 +00001591 if (PyErr_Occurred()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001592 Py_DECREF(m);
1593 m = NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001594 }
Barry Warsaw92971171997-01-03 00:14:25 +00001595
Barry Warsaw92971171997-01-03 00:14:25 +00001596 finally:
Martin v. Löwis1a214512008-06-11 05:26:20 +00001597 return m;
Guido van Rossum08c16611997-08-02 03:01:42 +00001598}
1599
1600static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001601finisignal(void)
Guido van Rossum08c16611997-08-02 03:01:42 +00001602{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001603 int i;
1604 PyObject *func;
Guido van Rossum08c16611997-08-02 03:01:42 +00001605
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001606 for (i = 1; i < NSIG; i++) {
1607 func = Handlers[i].func;
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001608 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001609 Handlers[i].func = NULL;
pkerlinge905c842018-06-01 09:47:18 +00001610 if (func != NULL && func != Py_None &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001611 func != DefaultHandler && func != IgnoreHandler)
1612 PyOS_setsig(i, SIG_DFL);
1613 Py_XDECREF(func);
1614 }
Guido van Rossum08c16611997-08-02 03:01:42 +00001615
Serhiy Storchaka505ff752014-02-09 13:33:53 +02001616 Py_CLEAR(IntHandler);
1617 Py_CLEAR(DefaultHandler);
1618 Py_CLEAR(IgnoreHandler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001619}
1620
Barry Warsaw92971171997-01-03 00:14:25 +00001621
Barry Warsaw92971171997-01-03 00:14:25 +00001622/* Declared in pyerrors.h */
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001623int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001624PyErr_CheckSignals(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001625{
Eric Snow64d6cc82019-02-23 15:40:43 -07001626 if (!is_main()) {
1627 return 0;
1628 }
1629
1630 return _PyErr_CheckSignals();
1631}
1632
1633
1634/* Declared in cpython/pyerrors.h */
1635int
1636_PyErr_CheckSignals(void)
1637{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001638 int i;
1639 PyObject *f;
Barry Warsaw92971171997-01-03 00:14:25 +00001640
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001641 if (!_Py_atomic_load(&is_tripped))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001642 return 0;
Christian Heimesb76922a2007-12-11 01:06:40 +00001643
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001644 /*
1645 * The is_tripped variable is meant to speed up the calls to
1646 * PyErr_CheckSignals (both directly or via pending calls) when no
1647 * signal has arrived. This variable is set to 1 when a signal arrives
1648 * and it is set to 0 here, when we know some signals arrived. This way
1649 * we can run the registered handlers with no signals blocked.
1650 *
1651 * NOTE: with this approach we can have a situation where is_tripped is
1652 * 1 but we have no more signals to handle (Handlers[i].tripped
1653 * is 0 for every signal i). This won't do us any harm (except
1654 * we're gonna spent some cycles for nothing). This happens when
1655 * we receive a signal i after we zero is_tripped and before we
1656 * check Handlers[i].tripped.
1657 */
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001658 _Py_atomic_store(&is_tripped, 0);
Christian Heimesb76922a2007-12-11 01:06:40 +00001659
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001660 if (!(f = (PyObject *)PyEval_GetFrame()))
1661 f = Py_None;
Christian Heimesb76922a2007-12-11 01:06:40 +00001662
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001663 for (i = 1; i < NSIG; i++) {
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001664 if (_Py_atomic_load_relaxed(&Handlers[i].tripped)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001665 PyObject *result = NULL;
1666 PyObject *arglist = Py_BuildValue("(iO)", i, f);
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001667 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Barry Warsaw92971171997-01-03 00:14:25 +00001668
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001669 if (arglist) {
1670 result = PyEval_CallObject(Handlers[i].func,
1671 arglist);
1672 Py_DECREF(arglist);
1673 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +02001674 if (!result) {
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001675 _Py_atomic_store(&is_tripped, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001676 return -1;
Antoine Pitrouc08177a2017-06-28 23:29:29 +02001677 }
Barry Warsaw92971171997-01-03 00:14:25 +00001678
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001679 Py_DECREF(result);
1680 }
1681 }
Christian Heimesb76922a2007-12-11 01:06:40 +00001682
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001683 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001684}
1685
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +00001686
Barry Warsaw92971171997-01-03 00:14:25 +00001687/* Replacements for intrcheck.c functionality
1688 * Declared in pyerrors.h
1689 */
1690void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001691PyErr_SetInterrupt(void)
Barry Warsaw92971171997-01-03 00:14:25 +00001692{
Victor Stinner6c9b35b2011-04-18 16:25:56 +02001693 trip_signal(SIGINT);
Barry Warsaw92971171997-01-03 00:14:25 +00001694}
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001695
1696void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001697PyOS_InitInterrupts(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001698{
Giampaolo Rodola'e09fb712014-04-04 15:34:17 +02001699 PyObject *m = PyImport_ImportModule("_signal");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001700 if (m) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001701 Py_DECREF(m);
1702 }
Guido van Rossum08c16611997-08-02 03:01:42 +00001703}
1704
1705void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001706PyOS_FiniInterrupts(void)
Guido van Rossum08c16611997-08-02 03:01:42 +00001707{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001708 finisignal();
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001709}
1710
1711int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001712PyOS_InterruptOccurred(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001713{
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001714 if (_Py_atomic_load_relaxed(&Handlers[SIGINT].tripped)) {
Eric Snow64d6cc82019-02-23 15:40:43 -07001715 if (!is_main()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001716 return 0;
Eric Snow64d6cc82019-02-23 15:40:43 -07001717 }
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001718 _Py_atomic_store_relaxed(&Handlers[SIGINT].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001719 return 1;
1720 }
1721 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001722}
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001723
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001724static void
1725_clear_pending_signals(void)
1726{
1727 int i;
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001728 if (!_Py_atomic_load(&is_tripped))
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001729 return;
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001730 _Py_atomic_store(&is_tripped, 0);
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001731 for (i = 1; i < NSIG; ++i) {
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001732 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001733 }
1734}
1735
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001736void
Antoine Pitrou346cbd32017-05-27 17:50:54 +02001737_PySignal_AfterFork(void)
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001738{
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001739 /* Clear the signal flags after forking so that they aren't handled
1740 * in both processes if they came in just before the fork() but before
1741 * the interpreter had an opportunity to call the handlers. issue9535. */
1742 _clear_pending_signals();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001743 main_thread = PyThread_get_thread_ident();
1744 main_pid = getpid();
Eric Snow64d6cc82019-02-23 15:40:43 -07001745 main_interp = _PyInterpreterState_Get();
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001746}
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001747
1748int
1749_PyOS_IsMainThread(void)
1750{
Eric Snow64d6cc82019-02-23 15:40:43 -07001751 return is_main();
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001752}
1753
1754#ifdef MS_WINDOWS
1755void *_PyOS_SigintEvent(void)
1756{
1757 /* Returns a manual-reset event which gets tripped whenever
1758 SIGINT is received.
1759
1760 Python.h does not include windows.h so we do cannot use HANDLE
1761 as the return type of this function. We use void* instead. */
1762 return sigint_event;
1763}
1764#endif