blob: 52ab4e998a9778ce419fe64d54321fb21e69bf87 [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;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000102
Victor Stinner2ec6b172011-05-15 10:21:59 +0200103static volatile struct {
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +0200104 _Py_atomic_int tripped;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000105 PyObject *func;
Barry Warsaw92971171997-01-03 00:14:25 +0000106} Handlers[NSIG];
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000107
Victor Stinner11517102014-07-29 23:31:34 +0200108#ifdef MS_WINDOWS
109#define INVALID_FD ((SOCKET_T)-1)
110
111static volatile struct {
112 SOCKET_T fd;
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800113 int warn_on_full_buffer;
Victor Stinner11517102014-07-29 23:31:34 +0200114 int use_send;
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800115} wakeup = {.fd = INVALID_FD, .warn_on_full_buffer = 1, .use_send = 0};
Victor Stinner11517102014-07-29 23:31:34 +0200116#else
117#define INVALID_FD (-1)
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800118static volatile struct {
119 sig_atomic_t fd;
120 int warn_on_full_buffer;
121} wakeup = {.fd = INVALID_FD, .warn_on_full_buffer = 1};
Victor Stinner11517102014-07-29 23:31:34 +0200122#endif
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000123
Christian Heimesb76922a2007-12-11 01:06:40 +0000124/* Speed up sigcheck() when none tripped */
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +0200125static _Py_atomic_int is_tripped;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000126
Barry Warsaw92971171997-01-03 00:14:25 +0000127static PyObject *DefaultHandler;
128static PyObject *IgnoreHandler;
129static PyObject *IntHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000130
Antoine Pitrou6dd381e2011-11-21 21:26:56 +0100131#ifdef MS_WINDOWS
132static HANDLE sigint_event = NULL;
133#endif
134
Martin v. Löwis823725e2008-03-24 13:39:54 +0000135#ifdef HAVE_GETITIMER
136static PyObject *ItimerError;
137
Victor Stinneref611c92017-10-13 13:49:43 -0700138/* auxiliary functions for setitimer */
139static int
140timeval_from_double(PyObject *obj, struct timeval *tv)
Martin v. Löwis823725e2008-03-24 13:39:54 +0000141{
Victor Stinneref611c92017-10-13 13:49:43 -0700142 if (obj == NULL) {
143 tv->tv_sec = 0;
144 tv->tv_usec = 0;
145 return 0;
Antoine Pitrou729780a2017-06-30 10:01:05 +0200146 }
Victor Stinneref611c92017-10-13 13:49:43 -0700147
148 _PyTime_t t;
149 if (_PyTime_FromSecondsObject(&t, obj, _PyTime_ROUND_CEILING) < 0) {
150 return -1;
151 }
152 return _PyTime_AsTimeval(t, tv, _PyTime_ROUND_CEILING);
Martin v. Löwis823725e2008-03-24 13:39:54 +0000153}
154
Christian Heimes1a8501c2008-10-02 19:56:01 +0000155Py_LOCAL_INLINE(double)
Martin v. Löwis823725e2008-03-24 13:39:54 +0000156double_from_timeval(struct timeval *tv)
157{
158 return tv->tv_sec + (double)(tv->tv_usec / 1000000.0);
159}
160
161static PyObject *
162itimer_retval(struct itimerval *iv)
163{
164 PyObject *r, *v;
165
166 r = PyTuple_New(2);
167 if (r == NULL)
Martin Panter6d57fe12016-09-17 03:26:16 +0000168 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000169
170 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_value)))) {
Martin Panter6d57fe12016-09-17 03:26:16 +0000171 Py_DECREF(r);
172 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000173 }
174
175 PyTuple_SET_ITEM(r, 0, v);
176
177 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_interval)))) {
Martin Panter6d57fe12016-09-17 03:26:16 +0000178 Py_DECREF(r);
179 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000180 }
181
182 PyTuple_SET_ITEM(r, 1, v);
183
184 return r;
185}
186#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000187
Guido van Rossume4485b01994-09-07 14:32:49 +0000188static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000189signal_default_int_handler(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000190{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000191 PyErr_SetNone(PyExc_KeyboardInterrupt);
192 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000193}
194
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000195PyDoc_STRVAR(default_int_handler_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000196"default_int_handler(...)\n\
197\n\
Michael W. Hudson24ec2112004-06-17 15:55:53 +0000198The default handler for SIGINT installed by Python.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000199It raises KeyboardInterrupt.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000200
Thomas Wouters0796b002000-07-22 23:49:30 +0000201
202static int
Victor Stinner11517102014-07-29 23:31:34 +0200203report_wakeup_write_error(void *data)
Antoine Pitrou6f6ec372013-08-17 20:27:56 +0200204{
205 int save_errno = errno;
Benjamin Petersonca470632016-09-06 13:47:26 -0700206 errno = (int) (intptr_t) data;
Antoine Pitrou6f6ec372013-08-17 20:27:56 +0200207 PyErr_SetFromErrno(PyExc_OSError);
208 PySys_WriteStderr("Exception ignored when trying to write to the "
209 "signal wakeup fd:\n");
210 PyErr_WriteUnraisable(NULL);
211 errno = save_errno;
212 return 0;
213}
214
Victor Stinner11517102014-07-29 23:31:34 +0200215#ifdef MS_WINDOWS
216static int
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800217report_wakeup_send_error(void* data)
Victor Stinner11517102014-07-29 23:31:34 +0200218{
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800219 /* PyErr_SetExcFromWindowsErr() invokes FormatMessage() which
220 recognizes the error codes used by both GetLastError() and
221 WSAGetLastError */
222 PyErr_SetExcFromWindowsErr(PyExc_OSError, (int) (intptr_t) data);
Victor Stinner11517102014-07-29 23:31:34 +0200223 PySys_WriteStderr("Exception ignored when trying to send to the "
224 "signal wakeup fd:\n");
225 PyErr_WriteUnraisable(NULL);
Victor Stinner11517102014-07-29 23:31:34 +0200226 return 0;
227}
228#endif /* MS_WINDOWS */
229
Tim Peters4f1b2082000-07-23 21:18:09 +0000230static void
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200231trip_signal(int sig_num)
232{
Victor Stinnerd49b1f12011-05-08 02:03:15 +0200233 unsigned char byte;
Victor Stinner11517102014-07-29 23:31:34 +0200234 int fd;
235 Py_ssize_t rc;
Victor Stinnerc13ef662011-05-25 02:35:58 +0200236
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +0200237 _Py_atomic_store_relaxed(&Handlers[sig_num].tripped, 1);
Victor Stinner11517102014-07-29 23:31:34 +0200238
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200239 /* Set is_tripped after setting .tripped, as it gets
240 cleared in PyErr_CheckSignals() before .tripped. */
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +0200241 _Py_atomic_store(&is_tripped, 1);
242
243 /* Notify ceval.c */
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200244 _PyEval_SignalReceived();
Nathaniel J. Smith4ae01492017-05-16 14:12:11 -0700245
246 /* And then write to the wakeup fd *after* setting all the globals and
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200247 doing the _PyEval_SignalReceived. We used to write to the wakeup fd
248 and then set the flag, but this allowed the following sequence of events
249 (especially on windows, where trip_signal may run in a new thread):
Nathaniel J. Smith4ae01492017-05-16 14:12:11 -0700250
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800251 - main thread blocks on select([wakeup.fd], ...)
Nathaniel J. Smith4ae01492017-05-16 14:12:11 -0700252 - signal arrives
253 - trip_signal writes to the wakeup fd
254 - the main thread wakes up
255 - the main thread checks the signal flags, sees that they're unset
256 - the main thread empties the wakeup fd
257 - the main thread goes back to sleep
258 - trip_signal sets the flags to request the Python-level signal handler
259 be run
260 - the main thread doesn't notice, because it's asleep
261
262 See bpo-30038 for more details.
263 */
264
Victor Stinner11517102014-07-29 23:31:34 +0200265#ifdef MS_WINDOWS
266 fd = Py_SAFE_DOWNCAST(wakeup.fd, SOCKET_T, int);
267#else
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800268 fd = wakeup.fd;
Victor Stinner11517102014-07-29 23:31:34 +0200269#endif
270
271 if (fd != INVALID_FD) {
Victor Stinnerc13ef662011-05-25 02:35:58 +0200272 byte = (unsigned char)sig_num;
Victor Stinner11517102014-07-29 23:31:34 +0200273#ifdef MS_WINDOWS
274 if (wakeup.use_send) {
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800275 rc = send(fd, &byte, 1, 0);
Victor Stinner11517102014-07-29 23:31:34 +0200276
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800277 if (rc < 0) {
278 int last_error = GetLastError();
279 if (wakeup.warn_on_full_buffer ||
280 last_error != WSAEWOULDBLOCK)
281 {
282 /* Py_AddPendingCall() isn't signal-safe, but we
283 still use it for this exceptional case. */
284 Py_AddPendingCall(report_wakeup_send_error,
285 (void *)(intptr_t) last_error);
286 }
Victor Stinner11517102014-07-29 23:31:34 +0200287 }
288 }
289 else
290#endif
291 {
Victor Stinnere72fe392015-04-01 18:35:22 +0200292 /* _Py_write_noraise() retries write() if write() is interrupted by
293 a signal (fails with EINTR). */
294 rc = _Py_write_noraise(fd, &byte, 1);
Victor Stinner11517102014-07-29 23:31:34 +0200295
296 if (rc < 0) {
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800297 if (wakeup.warn_on_full_buffer ||
298 (errno != EWOULDBLOCK && errno != EAGAIN))
299 {
300 /* Py_AddPendingCall() isn't signal-safe, but we
301 still use it for this exceptional case. */
302 Py_AddPendingCall(report_wakeup_write_error,
303 (void *)(intptr_t)errno);
304 }
Victor Stinner11517102014-07-29 23:31:34 +0200305 }
306 }
Victor Stinnerc13ef662011-05-25 02:35:58 +0200307 }
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200308}
309
310static void
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000311signal_handler(int sig_num)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000312{
Antoine Pitrou39a65912010-11-05 19:47:27 +0000313 int save_errno = errno;
314
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000315 /* See NOTES section above */
Antoine Pitrou39a65912010-11-05 19:47:27 +0000316 if (getpid() == main_pid)
Antoine Pitrou39a65912010-11-05 19:47:27 +0000317 {
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200318 trip_signal(sig_num);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000319 }
Antoine Pitrou39a65912010-11-05 19:47:27 +0000320
Jean-Paul Calderone6f137ca2010-05-09 03:18:57 +0000321#ifndef HAVE_SIGACTION
Antoine Pitrou39a65912010-11-05 19:47:27 +0000322#ifdef SIGCHLD
323 /* To avoid infinite recursion, this signal remains
324 reset until explicit re-instated.
325 Don't clear the 'func' field as it is our pointer
326 to the Python handler... */
327 if (sig_num != SIGCHLD)
328#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000329 /* If the handler was not set up with sigaction, reinstall it. See
Nick Coghland6009512014-11-20 21:39:37 +1000330 * Python/pylifecycle.c for the implementation of PyOS_setsig which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000331 * makes this true. See also issue8354. */
332 PyOS_setsig(sig_num, signal_handler);
Jean-Paul Calderone6f137ca2010-05-09 03:18:57 +0000333#endif
Antoine Pitrou39a65912010-11-05 19:47:27 +0000334
335 /* Issue #10311: asynchronously executing signal handlers should not
336 mutate errno under the feet of unsuspecting C code. */
337 errno = save_errno;
Antoine Pitrou6dd381e2011-11-21 21:26:56 +0100338
339#ifdef MS_WINDOWS
340 if (sig_num == SIGINT)
341 SetEvent(sigint_event);
342#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000343}
Guido van Rossume4485b01994-09-07 14:32:49 +0000344
Guido van Rossum06d511d1995-03-10 15:13:48 +0000345
Guido van Rossum1171ee61997-08-22 20:42:00 +0000346#ifdef HAVE_ALARM
Tal Einatc7027b72015-05-16 14:14:49 +0300347
348/*[clinic input]
349signal.alarm -> long
350
351 seconds: int
352 /
353
354Arrange for SIGALRM to arrive after the given number of seconds.
355[clinic start generated code]*/
356
357static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300358signal_alarm_impl(PyObject *module, int seconds)
359/*[clinic end generated code: output=144232290814c298 input=0d5e97e0e6f39e86]*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000360{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000361 /* alarm() returns the number of seconds remaining */
Tal Einatc7027b72015-05-16 14:14:49 +0300362 return (long)alarm(seconds);
Guido van Rossumaa0f4c71994-08-23 13:49:37 +0000363}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000364
Guido van Rossum06d511d1995-03-10 15:13:48 +0000365#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000366
Guido van Rossum1171ee61997-08-22 20:42:00 +0000367#ifdef HAVE_PAUSE
Tal Einatc7027b72015-05-16 14:14:49 +0300368
369/*[clinic input]
370signal.pause
371
372Wait until a signal arrives.
373[clinic start generated code]*/
374
Guido van Rossuma597dde1995-01-10 20:56:29 +0000375static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300376signal_pause_impl(PyObject *module)
377/*[clinic end generated code: output=391656788b3c3929 input=f03de0f875752062]*/
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000378{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000379 Py_BEGIN_ALLOW_THREADS
380 (void)pause();
381 Py_END_ALLOW_THREADS
382 /* make sure that any exceptions that got raised are propagated
383 * back into Python
384 */
385 if (PyErr_CheckSignals())
386 return NULL;
Barry Warsaw92971171997-01-03 00:14:25 +0000387
Tal Einatc7027b72015-05-16 14:14:49 +0300388 Py_RETURN_NONE;
Guido van Rossume4485b01994-09-07 14:32:49 +0000389}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000390
Guido van Rossum06d511d1995-03-10 15:13:48 +0000391#endif
Guido van Rossume4485b01994-09-07 14:32:49 +0000392
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000393
Tal Einatc7027b72015-05-16 14:14:49 +0300394/*[clinic input]
395signal.signal
396
397 signalnum: int
398 handler: object
399 /
400
401Set the action for the given signal.
402
403The action can be SIG_DFL, SIG_IGN, or a callable Python object.
404The previous action is returned. See getsignal() for possible return values.
405
406*** IMPORTANT NOTICE ***
407A signal handler function is called with two arguments:
408the first is the signal number, the second is the interrupted stack frame.
409[clinic start generated code]*/
410
Guido van Rossume4485b01994-09-07 14:32:49 +0000411static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300412signal_signal_impl(PyObject *module, int signalnum, PyObject *handler)
413/*[clinic end generated code: output=b44cfda43780f3a1 input=deee84af5fa0432c]*/
Guido van Rossume4485b01994-09-07 14:32:49 +0000414{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000415 PyObject *old_handler;
416 void (*func)(int);
Brian Curtinef9efbd2010-08-06 19:27:32 +0000417#ifdef MS_WINDOWS
Tal Einatc7027b72015-05-16 14:14:49 +0300418 /* Validate that signalnum is one of the allowable signals */
419 switch (signalnum) {
Brian Curtinc734b312010-09-06 16:04:10 +0000420 case SIGABRT: break;
Brian Curtin9e88b5a2010-10-01 14:49:24 +0000421#ifdef SIGBREAK
422 /* Issue #10003: SIGBREAK is not documented as permitted, but works
423 and corresponds to CTRL_BREAK_EVENT. */
424 case SIGBREAK: break;
425#endif
Brian Curtinc734b312010-09-06 16:04:10 +0000426 case SIGFPE: break;
427 case SIGILL: break;
428 case SIGINT: break;
429 case SIGSEGV: break;
430 case SIGTERM: break;
431 default:
432 PyErr_SetString(PyExc_ValueError, "invalid signal value");
433 return NULL;
Brian Curtinef9efbd2010-08-06 19:27:32 +0000434 }
435#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000436 if (PyThread_get_thread_ident() != main_thread) {
437 PyErr_SetString(PyExc_ValueError,
438 "signal only works in main thread");
439 return NULL;
440 }
Tal Einatc7027b72015-05-16 14:14:49 +0300441 if (signalnum < 1 || signalnum >= NSIG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000442 PyErr_SetString(PyExc_ValueError,
443 "signal number out of range");
444 return NULL;
445 }
Tal Einatc7027b72015-05-16 14:14:49 +0300446 if (handler == IgnoreHandler)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000447 func = SIG_IGN;
Tal Einatc7027b72015-05-16 14:14:49 +0300448 else if (handler == DefaultHandler)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000449 func = SIG_DFL;
Tal Einatc7027b72015-05-16 14:14:49 +0300450 else if (!PyCallable_Check(handler)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000451 PyErr_SetString(PyExc_TypeError,
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000452"signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000453 return NULL;
454 }
455 else
456 func = signal_handler;
Antoine Pitrouf6f90ff2017-11-03 19:58:46 +0100457 /* Check for pending signals before changing signal handler */
458 if (PyErr_CheckSignals()) {
459 return NULL;
460 }
Tal Einatc7027b72015-05-16 14:14:49 +0300461 if (PyOS_setsig(signalnum, func) == SIG_ERR) {
Victor Stinner388196e2011-05-10 17:13:00 +0200462 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000463 return NULL;
464 }
Tal Einatc7027b72015-05-16 14:14:49 +0300465 old_handler = Handlers[signalnum].func;
Tal Einatc7027b72015-05-16 14:14:49 +0300466 Py_INCREF(handler);
467 Handlers[signalnum].func = handler;
Antoine Pitrouc8c952c2013-05-04 23:16:59 +0200468 if (old_handler != NULL)
469 return old_handler;
470 else
471 Py_RETURN_NONE;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000472}
473
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000474
Tal Einatc7027b72015-05-16 14:14:49 +0300475/*[clinic input]
476signal.getsignal
477
478 signalnum: int
479 /
480
481Return the current action for the given signal.
482
483The return value can be:
484 SIG_IGN -- if the signal is being ignored
485 SIG_DFL -- if the default action for the signal is in effect
486 None -- if an unknown handler is in effect
487 anything else -- the callable Python object used as a handler
488[clinic start generated code]*/
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000489
Guido van Rossume4485b01994-09-07 14:32:49 +0000490static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300491signal_getsignal_impl(PyObject *module, int signalnum)
492/*[clinic end generated code: output=35b3e0e796fd555e input=ac23a00f19dfa509]*/
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000493{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000494 PyObject *old_handler;
Tal Einatc7027b72015-05-16 14:14:49 +0300495 if (signalnum < 1 || signalnum >= NSIG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000496 PyErr_SetString(PyExc_ValueError,
497 "signal number out of range");
498 return NULL;
499 }
Tal Einatc7027b72015-05-16 14:14:49 +0300500 old_handler = Handlers[signalnum].func;
Antoine Pitrouc8c952c2013-05-04 23:16:59 +0200501 if (old_handler != NULL) {
502 Py_INCREF(old_handler);
503 return old_handler;
504 }
505 else {
506 Py_RETURN_NONE;
507 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000508}
509
Antoine Pietri5d2a27d2018-03-12 14:42:34 +0100510
511/*[clinic input]
512signal.strsignal
513
514 signalnum: int
515 /
516
517Return the system description of the given signal.
518
519The return values can be such as "Interrupt", "Segmentation fault", etc.
520Returns None if the signal is not recognized.
521[clinic start generated code]*/
522
523static PyObject *
524signal_strsignal_impl(PyObject *module, int signalnum)
525/*[clinic end generated code: output=44e12e1e3b666261 input=b77914b03f856c74]*/
526{
527 char *res;
528
529 if (signalnum < 1 || signalnum >= NSIG) {
530 PyErr_SetString(PyExc_ValueError,
531 "signal number out of range");
532 return NULL;
533 }
534
Michael Osipov48ce4892018-08-23 15:27:19 +0200535#ifndef HAVE_STRSIGNAL
Antoine Pietri5d2a27d2018-03-12 14:42:34 +0100536 switch (signalnum) {
Michael Osipov48ce4892018-08-23 15:27:19 +0200537 /* Though being a UNIX, HP-UX does not provide strsignal(3). */
538#ifndef MS_WINDOWS
539 case SIGHUP:
540 res = "Hangup";
541 break;
542 case SIGALRM:
543 res = "Alarm clock";
544 break;
545 case SIGPIPE:
546 res = "Broken pipe";
547 break;
548 case SIGQUIT:
549 res = "Quit";
550 break;
551 case SIGCHLD:
552 res = "Child exited";
553 break;
554#endif
555 /* Custom redefinition of POSIX signals allowed on Windows. */
Antoine Pietri5d2a27d2018-03-12 14:42:34 +0100556 case SIGINT:
557 res = "Interrupt";
558 break;
559 case SIGILL:
560 res = "Illegal instruction";
561 break;
562 case SIGABRT:
563 res = "Aborted";
564 break;
565 case SIGFPE:
566 res = "Floating point exception";
567 break;
568 case SIGSEGV:
569 res = "Segmentation fault";
570 break;
571 case SIGTERM:
572 res = "Terminated";
573 break;
574 default:
575 Py_RETURN_NONE;
576 }
577#else
578 errno = 0;
579 res = strsignal(signalnum);
580
581 if (errno || res == NULL || strstr(res, "Unknown signal") != NULL)
582 Py_RETURN_NONE;
583#endif
584
585 return Py_BuildValue("s", res);
586}
587
Christian Heimes8640e742008-02-23 16:23:06 +0000588#ifdef HAVE_SIGINTERRUPT
Tal Einatc7027b72015-05-16 14:14:49 +0300589
590/*[clinic input]
591signal.siginterrupt
592
593 signalnum: int
594 flag: int
595 /
596
597Change system call restart behaviour.
598
599If flag is False, system calls will be restarted when interrupted by
600signal sig, else system calls will be interrupted.
601[clinic start generated code]*/
Christian Heimes8640e742008-02-23 16:23:06 +0000602
603static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300604signal_siginterrupt_impl(PyObject *module, int signalnum, int flag)
605/*[clinic end generated code: output=063816243d85dd19 input=4160acacca3e2099]*/
Christian Heimes8640e742008-02-23 16:23:06 +0000606{
Tal Einatc7027b72015-05-16 14:14:49 +0300607 if (signalnum < 1 || signalnum >= NSIG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000608 PyErr_SetString(PyExc_ValueError,
609 "signal number out of range");
610 return NULL;
611 }
Tal Einatc7027b72015-05-16 14:14:49 +0300612 if (siginterrupt(signalnum, flag)<0) {
Victor Stinner388196e2011-05-10 17:13:00 +0200613 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000614 return NULL;
615 }
Tal Einatc7027b72015-05-16 14:14:49 +0300616 Py_RETURN_NONE;
Christian Heimes8640e742008-02-23 16:23:06 +0000617}
618
619#endif
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000620
Tal Einatc7027b72015-05-16 14:14:49 +0300621
622static PyObject*
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800623signal_set_wakeup_fd(PyObject *self, PyObject *args, PyObject *kwds)
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000624{
Victor Stinnere134a7f2015-03-30 10:09:31 +0200625 struct _Py_stat_struct status;
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800626 static char *kwlist[] = {
627 "", "warn_on_full_buffer", NULL,
628 };
629 int warn_on_full_buffer = 1;
Victor Stinner11517102014-07-29 23:31:34 +0200630#ifdef MS_WINDOWS
631 PyObject *fdobj;
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100632 SOCKET_T sockfd, old_sockfd;
Victor Stinner11517102014-07-29 23:31:34 +0200633 int res;
634 int res_size = sizeof res;
635 PyObject *mod;
Victor Stinner11517102014-07-29 23:31:34 +0200636 int is_socket;
637
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800638 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|$p:set_wakeup_fd", kwlist,
639 &fdobj, &warn_on_full_buffer))
Victor Stinner11517102014-07-29 23:31:34 +0200640 return NULL;
641
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100642 sockfd = PyLong_AsSocket_t(fdobj);
643 if (sockfd == (SOCKET_T)(-1) && PyErr_Occurred())
Victor Stinner11517102014-07-29 23:31:34 +0200644 return NULL;
645#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000646 int fd, old_fd;
Victor Stinner11517102014-07-29 23:31:34 +0200647
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800648 if (!PyArg_ParseTupleAndKeywords(args, kwds, "i|$p:set_wakeup_fd", kwlist,
649 &fd, &warn_on_full_buffer))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000650 return NULL;
Victor Stinner11517102014-07-29 23:31:34 +0200651#endif
652
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000653 if (PyThread_get_thread_ident() != main_thread) {
654 PyErr_SetString(PyExc_ValueError,
655 "set_wakeup_fd only works in main thread");
656 return NULL;
657 }
Victor Stinner0bffc942014-07-21 16:28:54 +0200658
Victor Stinner11517102014-07-29 23:31:34 +0200659#ifdef MS_WINDOWS
660 is_socket = 0;
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100661 if (sockfd != INVALID_FD) {
Victor Stinner11517102014-07-29 23:31:34 +0200662 /* Import the _socket module to call WSAStartup() */
663 mod = PyImport_ImportModuleNoBlock("_socket");
664 if (mod == NULL)
665 return NULL;
666 Py_DECREF(mod);
667
668 /* test the socket */
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100669 if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR,
Victor Stinner11517102014-07-29 23:31:34 +0200670 (char *)&res, &res_size) != 0) {
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100671 int fd, err;
672
673 err = WSAGetLastError();
Victor Stinner11517102014-07-29 23:31:34 +0200674 if (err != WSAENOTSOCK) {
675 PyErr_SetExcFromWindowsErr(PyExc_OSError, err);
676 return NULL;
677 }
678
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100679 fd = (int)sockfd;
Steve Dower940f33a2016-09-08 11:21:54 -0700680 if ((SOCKET_T)fd != sockfd) {
Victor Stinner11517102014-07-29 23:31:34 +0200681 PyErr_SetString(PyExc_ValueError, "invalid fd");
682 return NULL;
683 }
684
Victor Stinnere134a7f2015-03-30 10:09:31 +0200685 if (_Py_fstat(fd, &status) != 0)
Victor Stinner11517102014-07-29 23:31:34 +0200686 return NULL;
Victor Stinner38227602014-08-27 12:59:44 +0200687
688 /* on Windows, a file cannot be set to non-blocking mode */
Victor Stinner11517102014-07-29 23:31:34 +0200689 }
Victor Stinner38227602014-08-27 12:59:44 +0200690 else {
Victor Stinner11517102014-07-29 23:31:34 +0200691 is_socket = 1;
Victor Stinner38227602014-08-27 12:59:44 +0200692
693 /* Windows does not provide a function to test if a socket
694 is in non-blocking mode */
695 }
Victor Stinner11517102014-07-29 23:31:34 +0200696 }
697
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100698 old_sockfd = wakeup.fd;
699 wakeup.fd = sockfd;
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800700 wakeup.warn_on_full_buffer = warn_on_full_buffer;
Victor Stinner11517102014-07-29 23:31:34 +0200701 wakeup.use_send = is_socket;
702
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100703 if (old_sockfd != INVALID_FD)
704 return PyLong_FromSocket_t(old_sockfd);
Victor Stinner11517102014-07-29 23:31:34 +0200705 else
706 return PyLong_FromLong(-1);
707#else
Victor Stinnerd18ccd12014-07-24 21:58:53 +0200708 if (fd != -1) {
Victor Stinner38227602014-08-27 12:59:44 +0200709 int blocking;
710
Victor Stinnere134a7f2015-03-30 10:09:31 +0200711 if (_Py_fstat(fd, &status) != 0)
Victor Stinner11517102014-07-29 23:31:34 +0200712 return NULL;
Victor Stinner38227602014-08-27 12:59:44 +0200713
714 blocking = _Py_get_blocking(fd);
715 if (blocking < 0)
716 return NULL;
717 if (blocking) {
718 PyErr_Format(PyExc_ValueError,
719 "the fd %i must be in non-blocking mode",
720 fd);
721 return NULL;
722 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000723 }
Victor Stinner0bffc942014-07-21 16:28:54 +0200724
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800725 old_fd = wakeup.fd;
726 wakeup.fd = fd;
727 wakeup.warn_on_full_buffer = warn_on_full_buffer;
Victor Stinner0bffc942014-07-21 16:28:54 +0200728
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000729 return PyLong_FromLong(old_fd);
Victor Stinner11517102014-07-29 23:31:34 +0200730#endif
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000731}
732
733PyDoc_STRVAR(set_wakeup_fd_doc,
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800734"set_wakeup_fd(fd, *, warn_on_full_buffer=True) -> fd\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000735\n\
Victor Stinner11517102014-07-29 23:31:34 +0200736Sets the fd to be written to (with the signal number) when a signal\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000737comes in. A library can use this to wakeup select or poll.\n\
Victor Stinner11517102014-07-29 23:31:34 +0200738The previous fd or -1 is returned.\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000739\n\
740The fd must be non-blocking.");
741
742/* C API for the same, without all the error checking */
743int
744PySignal_SetWakeupFd(int fd)
745{
Victor Stinner11517102014-07-29 23:31:34 +0200746 int old_fd;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000747 if (fd < 0)
748 fd = -1;
Victor Stinner11517102014-07-29 23:31:34 +0200749
750#ifdef MS_WINDOWS
751 old_fd = Py_SAFE_DOWNCAST(wakeup.fd, SOCKET_T, int);
Victor Stinner11517102014-07-29 23:31:34 +0200752#else
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800753 old_fd = wakeup.fd;
Victor Stinner11517102014-07-29 23:31:34 +0200754#endif
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800755 wakeup.fd = fd;
756 wakeup.warn_on_full_buffer = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000757 return old_fd;
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000758}
759
760
Martin v. Löwis823725e2008-03-24 13:39:54 +0000761#ifdef HAVE_SETITIMER
Tal Einatc7027b72015-05-16 14:14:49 +0300762
763/*[clinic input]
764signal.setitimer
765
766 which: int
Victor Stinneref611c92017-10-13 13:49:43 -0700767 seconds: object
768 interval: object(c_default="NULL") = 0.0
Tal Einatc7027b72015-05-16 14:14:49 +0300769 /
770
771Sets given itimer (one of ITIMER_REAL, ITIMER_VIRTUAL or ITIMER_PROF).
772
773The timer will fire after value seconds and after that every interval seconds.
774The itimer can be cleared by setting seconds to zero.
775
776Returns old values as a tuple: (delay, interval).
777[clinic start generated code]*/
778
Martin v. Löwis823725e2008-03-24 13:39:54 +0000779static PyObject *
Victor Stinneref611c92017-10-13 13:49:43 -0700780signal_setitimer_impl(PyObject *module, int which, PyObject *seconds,
781 PyObject *interval)
782/*[clinic end generated code: output=65f9dcbddc35527b input=de43daf194e6f66f]*/
Martin v. Löwis823725e2008-03-24 13:39:54 +0000783{
Martin v. Löwis823725e2008-03-24 13:39:54 +0000784 struct itimerval new, old;
785
Victor Stinneref611c92017-10-13 13:49:43 -0700786 if (timeval_from_double(seconds, &new.it_value) < 0) {
787 return NULL;
788 }
789 if (timeval_from_double(interval, &new.it_interval) < 0) {
790 return NULL;
791 }
792
Martin v. Löwis823725e2008-03-24 13:39:54 +0000793 /* Let OS check "which" value */
794 if (setitimer(which, &new, &old) != 0) {
Tal Einatc7027b72015-05-16 14:14:49 +0300795 PyErr_SetFromErrno(ItimerError);
796 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000797 }
798
799 return itimer_retval(&old);
800}
801
Martin v. Löwis823725e2008-03-24 13:39:54 +0000802#endif
803
804
805#ifdef HAVE_GETITIMER
Tal Einatc7027b72015-05-16 14:14:49 +0300806
807/*[clinic input]
808signal.getitimer
809
810 which: int
811 /
812
813Returns current value of given itimer.
814[clinic start generated code]*/
815
Martin v. Löwis823725e2008-03-24 13:39:54 +0000816static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300817signal_getitimer_impl(PyObject *module, int which)
818/*[clinic end generated code: output=9e053175d517db40 input=f7d21d38f3490627]*/
Martin v. Löwis823725e2008-03-24 13:39:54 +0000819{
Martin v. Löwis823725e2008-03-24 13:39:54 +0000820 struct itimerval old;
821
Martin v. Löwis823725e2008-03-24 13:39:54 +0000822 if (getitimer(which, &old) != 0) {
Tal Einatc7027b72015-05-16 14:14:49 +0300823 PyErr_SetFromErrno(ItimerError);
824 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000825 }
826
827 return itimer_retval(&old);
828}
829
Martin v. Löwis823725e2008-03-24 13:39:54 +0000830#endif
831
Victor Stinnerb3e72192011-05-08 01:46:11 +0200832#if defined(PYPTHREAD_SIGMASK) || defined(HAVE_SIGPENDING)
Victor Stinner35b300c2011-05-04 13:20:35 +0200833static PyObject*
834sigset_to_set(sigset_t mask)
835{
836 PyObject *signum, *result;
837 int sig;
838
839 result = PySet_New(0);
840 if (result == NULL)
841 return NULL;
842
843 for (sig = 1; sig < NSIG; sig++) {
844 if (sigismember(&mask, sig) != 1)
845 continue;
846
847 /* Handle the case where it is a member by adding the signal to
848 the result list. Ignore the other cases because they mean the
849 signal isn't a member of the mask or the signal was invalid,
850 and an invalid signal must have been our fault in constructing
851 the loop boundaries. */
852 signum = PyLong_FromLong(sig);
853 if (signum == NULL) {
854 Py_DECREF(result);
855 return NULL;
856 }
857 if (PySet_Add(result, signum) == -1) {
858 Py_DECREF(signum);
859 Py_DECREF(result);
860 return NULL;
861 }
862 Py_DECREF(signum);
863 }
864 return result;
865}
Victor Stinnerb3e72192011-05-08 01:46:11 +0200866#endif
Victor Stinner35b300c2011-05-04 13:20:35 +0200867
Victor Stinnerb3e72192011-05-08 01:46:11 +0200868#ifdef PYPTHREAD_SIGMASK
Tal Einatc7027b72015-05-16 14:14:49 +0300869
870/*[clinic input]
871signal.pthread_sigmask
872
873 how: int
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300874 mask: sigset_t
Tal Einatc7027b72015-05-16 14:14:49 +0300875 /
876
877Fetch and/or change the signal mask of the calling thread.
878[clinic start generated code]*/
879
Victor Stinnera9293352011-04-30 15:21:58 +0200880static PyObject *
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300881signal_pthread_sigmask_impl(PyObject *module, int how, sigset_t mask)
882/*[clinic end generated code: output=0562c0fb192981a8 input=85bcebda442fa77f]*/
Victor Stinnera9293352011-04-30 15:21:58 +0200883{
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300884 sigset_t previous;
Victor Stinnera9293352011-04-30 15:21:58 +0200885 int err;
886
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300887 err = pthread_sigmask(how, &mask, &previous);
Victor Stinnera9293352011-04-30 15:21:58 +0200888 if (err != 0) {
889 errno = err;
Victor Stinnerb3e72192011-05-08 01:46:11 +0200890 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnera9293352011-04-30 15:21:58 +0200891 return NULL;
892 }
893
Victor Stinnerd0e516d2011-05-03 14:57:12 +0200894 /* if signals was unblocked, signal handlers have been called */
895 if (PyErr_CheckSignals())
896 return NULL;
897
Victor Stinner35b300c2011-05-04 13:20:35 +0200898 return sigset_to_set(previous);
Victor Stinnera9293352011-04-30 15:21:58 +0200899}
900
Victor Stinnera9293352011-04-30 15:21:58 +0200901#endif /* #ifdef PYPTHREAD_SIGMASK */
902
Martin v. Löwis823725e2008-03-24 13:39:54 +0000903
Victor Stinnerb3e72192011-05-08 01:46:11 +0200904#ifdef HAVE_SIGPENDING
Tal Einatc7027b72015-05-16 14:14:49 +0300905
906/*[clinic input]
907signal.sigpending
908
909Examine pending signals.
910
911Returns a set of signal numbers that are pending for delivery to
912the calling thread.
913[clinic start generated code]*/
914
Victor Stinnerb3e72192011-05-08 01:46:11 +0200915static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300916signal_sigpending_impl(PyObject *module)
917/*[clinic end generated code: output=53375ffe89325022 input=e0036c016f874e29]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +0200918{
919 int err;
920 sigset_t mask;
921 err = sigpending(&mask);
922 if (err)
923 return PyErr_SetFromErrno(PyExc_OSError);
924 return sigset_to_set(mask);
925}
926
Victor Stinnerb3e72192011-05-08 01:46:11 +0200927#endif /* #ifdef HAVE_SIGPENDING */
928
929
930#ifdef HAVE_SIGWAIT
Tal Einatc7027b72015-05-16 14:14:49 +0300931
932/*[clinic input]
933signal.sigwait
934
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300935 sigset: sigset_t
Tal Einatc7027b72015-05-16 14:14:49 +0300936 /
937
938Wait for a signal.
939
940Suspend execution of the calling thread until the delivery of one of the
941signals specified in the signal set sigset. The function accepts the signal
942and returns the signal number.
943[clinic start generated code]*/
944
Victor Stinnerb3e72192011-05-08 01:46:11 +0200945static PyObject *
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300946signal_sigwait_impl(PyObject *module, sigset_t sigset)
947/*[clinic end generated code: output=f43770699d682f96 input=a6fbd47b1086d119]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +0200948{
Victor Stinnerb3e72192011-05-08 01:46:11 +0200949 int err, signum;
950
Victor Stinner10c30d62011-06-10 01:39:53 +0200951 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300952 err = sigwait(&sigset, &signum);
Victor Stinner10c30d62011-06-10 01:39:53 +0200953 Py_END_ALLOW_THREADS
Victor Stinnerb3e72192011-05-08 01:46:11 +0200954 if (err) {
955 errno = err;
956 return PyErr_SetFromErrno(PyExc_OSError);
957 }
958
959 return PyLong_FromLong(signum);
960}
961
Tal Einatc7027b72015-05-16 14:14:49 +0300962#endif /* #ifdef HAVE_SIGWAIT */
963
Victor Stinnerb3e72192011-05-08 01:46:11 +0200964
Antoine Pitrou9d3627e2018-05-04 13:00:50 +0200965#if defined(HAVE_SIGFILLSET) || defined(MS_WINDOWS)
966
967/*[clinic input]
968signal.valid_signals
969
970Return a set of valid signal numbers on this platform.
971
972The signal numbers returned by this function can be safely passed to
973functions like `pthread_sigmask`.
974[clinic start generated code]*/
975
976static PyObject *
977signal_valid_signals_impl(PyObject *module)
978/*[clinic end generated code: output=1609cffbcfcf1314 input=86a3717ff25288f2]*/
979{
980#ifdef MS_WINDOWS
981#ifdef SIGBREAK
982 PyObject *tup = Py_BuildValue("(iiiiiii)", SIGABRT, SIGBREAK, SIGFPE,
983 SIGILL, SIGINT, SIGSEGV, SIGTERM);
984#else
985 PyObject *tup = Py_BuildValue("(iiiiii)", SIGABRT, SIGFPE, SIGILL,
986 SIGINT, SIGSEGV, SIGTERM);
987#endif
988 if (tup == NULL) {
989 return NULL;
990 }
991 PyObject *set = PySet_New(tup);
992 Py_DECREF(tup);
993 return set;
994#else
995 sigset_t mask;
996 if (sigemptyset(&mask) || sigfillset(&mask)) {
997 return PyErr_SetFromErrno(PyExc_OSError);
998 }
999 return sigset_to_set(mask);
1000#endif
1001}
1002
1003#endif /* #if defined(HAVE_SIGFILLSET) || defined(MS_WINDOWS) */
1004
1005
Ross Lagerwallbc808222011-06-25 12:13:40 +02001006#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
1007static int initialized;
1008static PyStructSequence_Field struct_siginfo_fields[] = {
1009 {"si_signo", "signal number"},
1010 {"si_code", "signal code"},
1011 {"si_errno", "errno associated with this signal"},
1012 {"si_pid", "sending process ID"},
1013 {"si_uid", "real user ID of sending process"},
1014 {"si_status", "exit value or signal"},
1015 {"si_band", "band event for SIGPOLL"},
1016 {0}
1017};
1018
1019PyDoc_STRVAR(struct_siginfo__doc__,
1020"struct_siginfo: Result from sigwaitinfo or sigtimedwait.\n\n\
1021This object may be accessed either as a tuple of\n\
1022(si_signo, si_code, si_errno, si_pid, si_uid, si_status, si_band),\n\
1023or via the attributes si_signo, si_code, and so on.");
1024
1025static PyStructSequence_Desc struct_siginfo_desc = {
1026 "signal.struct_siginfo", /* name */
1027 struct_siginfo__doc__, /* doc */
1028 struct_siginfo_fields, /* fields */
1029 7 /* n_in_sequence */
1030};
1031
1032static PyTypeObject SiginfoType;
1033
1034static PyObject *
1035fill_siginfo(siginfo_t *si)
1036{
1037 PyObject *result = PyStructSequence_New(&SiginfoType);
1038 if (!result)
1039 return NULL;
1040
1041 PyStructSequence_SET_ITEM(result, 0, PyLong_FromLong((long)(si->si_signo)));
1042 PyStructSequence_SET_ITEM(result, 1, PyLong_FromLong((long)(si->si_code)));
1043 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si->si_errno)));
1044 PyStructSequence_SET_ITEM(result, 3, PyLong_FromPid(si->si_pid));
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02001045 PyStructSequence_SET_ITEM(result, 4, _PyLong_FromUid(si->si_uid));
Ross Lagerwallbc808222011-06-25 12:13:40 +02001046 PyStructSequence_SET_ITEM(result, 5,
1047 PyLong_FromLong((long)(si->si_status)));
Zachary Ware6a6967e2016-10-01 00:47:27 -05001048#ifdef HAVE_SIGINFO_T_SI_BAND
Ross Lagerwallbc808222011-06-25 12:13:40 +02001049 PyStructSequence_SET_ITEM(result, 6, PyLong_FromLong(si->si_band));
Zachary Ware6a6967e2016-10-01 00:47:27 -05001050#else
1051 PyStructSequence_SET_ITEM(result, 6, PyLong_FromLong(0L));
1052#endif
Ross Lagerwallbc808222011-06-25 12:13:40 +02001053 if (PyErr_Occurred()) {
1054 Py_DECREF(result);
1055 return NULL;
1056 }
1057
1058 return result;
1059}
1060#endif
1061
1062#ifdef HAVE_SIGWAITINFO
Tal Einatc7027b72015-05-16 14:14:49 +03001063
1064/*[clinic input]
1065signal.sigwaitinfo
1066
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001067 sigset: sigset_t
Tal Einatc7027b72015-05-16 14:14:49 +03001068 /
1069
1070Wait synchronously until one of the signals in *sigset* is delivered.
1071
1072Returns a struct_siginfo containing information about the signal.
1073[clinic start generated code]*/
1074
Ross Lagerwallbc808222011-06-25 12:13:40 +02001075static PyObject *
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001076signal_sigwaitinfo_impl(PyObject *module, sigset_t sigset)
1077/*[clinic end generated code: output=1eb2f1fa236fdbca input=3d1a7e1f27fc664c]*/
Ross Lagerwallbc808222011-06-25 12:13:40 +02001078{
Ross Lagerwallbc808222011-06-25 12:13:40 +02001079 siginfo_t si;
1080 int err;
Victor Stinnera453cd82015-03-20 12:54:28 +01001081 int async_err = 0;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001082
Victor Stinnera453cd82015-03-20 12:54:28 +01001083 do {
1084 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001085 err = sigwaitinfo(&sigset, &si);
Victor Stinnera453cd82015-03-20 12:54:28 +01001086 Py_END_ALLOW_THREADS
1087 } while (err == -1
1088 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Ross Lagerwallbc808222011-06-25 12:13:40 +02001089 if (err == -1)
Victor Stinnera453cd82015-03-20 12:54:28 +01001090 return (!async_err) ? PyErr_SetFromErrno(PyExc_OSError) : NULL;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001091
1092 return fill_siginfo(&si);
1093}
1094
Ross Lagerwallbc808222011-06-25 12:13:40 +02001095#endif /* #ifdef HAVE_SIGWAITINFO */
1096
1097#ifdef HAVE_SIGTIMEDWAIT
Tal Einatc7027b72015-05-16 14:14:49 +03001098
1099/*[clinic input]
1100signal.sigtimedwait
1101
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001102 sigset: sigset_t
Serhiy Storchaka6b680cd2015-05-16 15:57:56 +03001103 timeout as timeout_obj: object
Tal Einatc7027b72015-05-16 14:14:49 +03001104 /
1105
1106Like sigwaitinfo(), but with a timeout.
1107
1108The timeout is specified in seconds, with floating point numbers allowed.
1109[clinic start generated code]*/
1110
Ross Lagerwallbc808222011-06-25 12:13:40 +02001111static PyObject *
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001112signal_sigtimedwait_impl(PyObject *module, sigset_t sigset,
Serhiy Storchaka6b680cd2015-05-16 15:57:56 +03001113 PyObject *timeout_obj)
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001114/*[clinic end generated code: output=59c8971e8ae18a64 input=87fd39237cf0b7ba]*/
Ross Lagerwallbc808222011-06-25 12:13:40 +02001115{
Victor Stinnera453cd82015-03-20 12:54:28 +01001116 struct timespec ts;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001117 siginfo_t si;
Victor Stinnera453cd82015-03-20 12:54:28 +01001118 int res;
Victor Stinner34dc0f42015-03-27 18:19:03 +01001119 _PyTime_t timeout, deadline, monotonic;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001120
Victor Stinner869e1772015-03-30 03:49:14 +02001121 if (_PyTime_FromSecondsObject(&timeout,
1122 timeout_obj, _PyTime_ROUND_CEILING) < 0)
Ross Lagerwallbc808222011-06-25 12:13:40 +02001123 return NULL;
1124
Victor Stinnera453cd82015-03-20 12:54:28 +01001125 if (timeout < 0) {
Ross Lagerwallbc808222011-06-25 12:13:40 +02001126 PyErr_SetString(PyExc_ValueError, "timeout must be non-negative");
1127 return NULL;
1128 }
1129
Victor Stinner34dc0f42015-03-27 18:19:03 +01001130 deadline = _PyTime_GetMonotonicClock() + timeout;
Victor Stinnera453cd82015-03-20 12:54:28 +01001131
1132 do {
Victor Stinner34dc0f42015-03-27 18:19:03 +01001133 if (_PyTime_AsTimespec(timeout, &ts) < 0)
1134 return NULL;
Victor Stinnera453cd82015-03-20 12:54:28 +01001135
1136 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001137 res = sigtimedwait(&sigset, &si, &ts);
Victor Stinnera453cd82015-03-20 12:54:28 +01001138 Py_END_ALLOW_THREADS
1139
1140 if (res != -1)
1141 break;
1142
1143 if (errno != EINTR) {
1144 if (errno == EAGAIN)
1145 Py_RETURN_NONE;
1146 else
1147 return PyErr_SetFromErrno(PyExc_OSError);
1148 }
1149
1150 /* sigtimedwait() was interrupted by a signal (EINTR) */
1151 if (PyErr_CheckSignals())
1152 return NULL;
1153
Victor Stinner34dc0f42015-03-27 18:19:03 +01001154 monotonic = _PyTime_GetMonotonicClock();
1155 timeout = deadline - monotonic;
Victor Stinner6aa446c2015-03-30 21:33:51 +02001156 if (timeout < 0)
Victor Stinnera453cd82015-03-20 12:54:28 +01001157 break;
1158 } while (1);
Ross Lagerwallbc808222011-06-25 12:13:40 +02001159
1160 return fill_siginfo(&si);
1161}
1162
Ross Lagerwallbc808222011-06-25 12:13:40 +02001163#endif /* #ifdef HAVE_SIGTIMEDWAIT */
1164
Victor Stinnerb3e72192011-05-08 01:46:11 +02001165
Antoine Pitroua6a4dc82017-09-07 18:56:24 +02001166#if defined(HAVE_PTHREAD_KILL)
Tal Einatc7027b72015-05-16 14:14:49 +03001167
1168/*[clinic input]
1169signal.pthread_kill
1170
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +02001171 thread_id: unsigned_long(bitwise=True)
Tal Einatc7027b72015-05-16 14:14:49 +03001172 signalnum: int
1173 /
1174
1175Send a signal to a thread.
1176[clinic start generated code]*/
1177
Victor Stinnerb3e72192011-05-08 01:46:11 +02001178static PyObject *
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +02001179signal_pthread_kill_impl(PyObject *module, unsigned long thread_id,
1180 int signalnum)
1181/*[clinic end generated code: output=7629919b791bc27f input=1d901f2c7bb544ff]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +02001182{
Victor Stinnerb3e72192011-05-08 01:46:11 +02001183 int err;
1184
Tal Einatc7027b72015-05-16 14:14:49 +03001185 err = pthread_kill((pthread_t)thread_id, signalnum);
Victor Stinnerb3e72192011-05-08 01:46:11 +02001186 if (err != 0) {
1187 errno = err;
1188 PyErr_SetFromErrno(PyExc_OSError);
1189 return NULL;
1190 }
1191
1192 /* the signal may have been send to the current thread */
1193 if (PyErr_CheckSignals())
1194 return NULL;
1195
1196 Py_RETURN_NONE;
1197}
1198
Antoine Pitroua6a4dc82017-09-07 18:56:24 +02001199#endif /* #if defined(HAVE_PTHREAD_KILL) */
Victor Stinnerb3e72192011-05-08 01:46:11 +02001200
1201
1202
Tal Einatc7027b72015-05-16 14:14:49 +03001203/* List of functions defined in the module -- some of the methoddefs are
1204 defined to nothing if the corresponding C function is not available. */
Barry Warsaw92971171997-01-03 00:14:25 +00001205static PyMethodDef signal_methods[] = {
Tal Einatc7027b72015-05-16 14:14:49 +03001206 {"default_int_handler", signal_default_int_handler, METH_VARARGS, default_int_handler_doc},
1207 SIGNAL_ALARM_METHODDEF
1208 SIGNAL_SETITIMER_METHODDEF
1209 SIGNAL_GETITIMER_METHODDEF
1210 SIGNAL_SIGNAL_METHODDEF
Antoine Pietri5d2a27d2018-03-12 14:42:34 +01001211 SIGNAL_STRSIGNAL_METHODDEF
Tal Einatc7027b72015-05-16 14:14:49 +03001212 SIGNAL_GETSIGNAL_METHODDEF
Serhiy Storchaka62be7422018-11-27 13:27:31 +02001213 {"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 +03001214 SIGNAL_SIGINTERRUPT_METHODDEF
1215 SIGNAL_PAUSE_METHODDEF
1216 SIGNAL_PTHREAD_KILL_METHODDEF
1217 SIGNAL_PTHREAD_SIGMASK_METHODDEF
1218 SIGNAL_SIGPENDING_METHODDEF
1219 SIGNAL_SIGWAIT_METHODDEF
1220 SIGNAL_SIGWAITINFO_METHODDEF
1221 SIGNAL_SIGTIMEDWAIT_METHODDEF
Antoine Pitrou9d3627e2018-05-04 13:00:50 +02001222#if defined(HAVE_SIGFILLSET) || defined(MS_WINDOWS)
1223 SIGNAL_VALID_SIGNALS_METHODDEF
1224#endif
Tal Einatc7027b72015-05-16 14:14:49 +03001225 {NULL, NULL} /* sentinel */
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001226};
1227
Barry Warsaw92971171997-01-03 00:14:25 +00001228
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001229PyDoc_STRVAR(module_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001230"This module provides mechanisms to use signal handlers in Python.\n\
1231\n\
1232Functions:\n\
1233\n\
1234alarm() -- cause SIGALRM after a specified time [Unix only]\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001235setitimer() -- cause a signal (described below) after a specified\n\
1236 float time and the timer may restart then [Unix only]\n\
1237getitimer() -- get current value of timer [Unix only]\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001238signal() -- set the action for a given signal\n\
1239getsignal() -- get the signal action for a given signal\n\
1240pause() -- wait until a signal arrives [Unix only]\n\
1241default_int_handler() -- default SIGINT handler\n\
1242\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001243signal constants:\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001244SIG_DFL -- used to refer to the system default handler\n\
1245SIG_IGN -- used to ignore the signal\n\
1246NSIG -- number of defined signals\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001247SIGINT, SIGTERM, etc. -- signal numbers\n\
1248\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001249itimer constants:\n\
1250ITIMER_REAL -- decrements in real time, and delivers SIGALRM upon\n\
1251 expiration\n\
1252ITIMER_VIRTUAL -- decrements only when the process is executing,\n\
1253 and delivers SIGVTALRM upon expiration\n\
1254ITIMER_PROF -- decrements both when the process is executing and\n\
1255 when the system is executing on behalf of the process.\n\
1256 Coupled with ITIMER_VIRTUAL, this timer is usually\n\
1257 used to profile the time spent by the application\n\
1258 in user and kernel space. SIGPROF is delivered upon\n\
1259 expiration.\n\
1260\n\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001261*** IMPORTANT NOTICE ***\n\
1262A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001263the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001264
Martin v. Löwis1a214512008-06-11 05:26:20 +00001265static struct PyModuleDef signalmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001266 PyModuleDef_HEAD_INIT,
Brett Cannon815a6f32014-04-04 10:20:28 -04001267 "_signal",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001268 module_doc,
1269 -1,
1270 signal_methods,
1271 NULL,
1272 NULL,
1273 NULL,
1274 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001275};
1276
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001277PyMODINIT_FUNC
Giampaolo Rodola'e09fb712014-04-04 15:34:17 +02001278PyInit__signal(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001279{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001280 PyObject *m, *d, *x;
1281 int i;
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001282
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001283 main_thread = PyThread_get_thread_ident();
1284 main_pid = getpid();
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001285
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001286 /* Create the module and add the functions */
1287 m = PyModule_Create(&signalmodule);
1288 if (m == NULL)
1289 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001290
Ross Lagerwallbc808222011-06-25 12:13:40 +02001291#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
Victor Stinner1c8f0592013-07-22 22:24:54 +02001292 if (!initialized) {
1293 if (PyStructSequence_InitType2(&SiginfoType, &struct_siginfo_desc) < 0)
1294 return NULL;
1295 }
Ross Lagerwallbc808222011-06-25 12:13:40 +02001296 Py_INCREF((PyObject*) &SiginfoType);
1297 PyModule_AddObject(m, "struct_siginfo", (PyObject*) &SiginfoType);
1298 initialized = 1;
1299#endif
1300
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001301 /* Add some symbolic constants to the module */
1302 d = PyModule_GetDict(m);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001303
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001304 x = DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL);
1305 if (!x || PyDict_SetItemString(d, "SIG_DFL", x) < 0)
1306 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001307
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001308 x = IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN);
1309 if (!x || PyDict_SetItemString(d, "SIG_IGN", x) < 0)
1310 goto finally;
Barry Warsaw92971171997-01-03 00:14:25 +00001311
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001312 x = PyLong_FromLong((long)NSIG);
1313 if (!x || PyDict_SetItemString(d, "NSIG", x) < 0)
1314 goto finally;
1315 Py_DECREF(x);
Barry Warsaw92971171997-01-03 00:14:25 +00001316
Victor Stinnera9293352011-04-30 15:21:58 +02001317#ifdef SIG_BLOCK
Victor Stinner72c53b52011-05-02 16:15:43 +02001318 if (PyModule_AddIntMacro(m, SIG_BLOCK))
1319 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001320#endif
Victor Stinnera9293352011-04-30 15:21:58 +02001321#ifdef SIG_UNBLOCK
Victor Stinner72c53b52011-05-02 16:15:43 +02001322 if (PyModule_AddIntMacro(m, SIG_UNBLOCK))
1323 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001324#endif
Victor Stinnera9293352011-04-30 15:21:58 +02001325#ifdef SIG_SETMASK
Victor Stinner72c53b52011-05-02 16:15:43 +02001326 if (PyModule_AddIntMacro(m, SIG_SETMASK))
1327 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001328#endif
1329
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001330 x = IntHandler = PyDict_GetItemString(d, "default_int_handler");
1331 if (!x)
1332 goto finally;
1333 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +00001334
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001335 _Py_atomic_store_relaxed(&Handlers[0].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001336 for (i = 1; i < NSIG; i++) {
1337 void (*t)(int);
1338 t = PyOS_getsig(i);
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001339 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001340 if (t == SIG_DFL)
1341 Handlers[i].func = DefaultHandler;
1342 else if (t == SIG_IGN)
1343 Handlers[i].func = IgnoreHandler;
1344 else
1345 Handlers[i].func = Py_None; /* None of our business */
1346 Py_INCREF(Handlers[i].func);
1347 }
1348 if (Handlers[SIGINT].func == DefaultHandler) {
1349 /* Install default int handler */
1350 Py_INCREF(IntHandler);
Serhiy Storchaka57a01d32016-04-10 18:05:40 +03001351 Py_SETREF(Handlers[SIGINT].func, IntHandler);
pkerlinge905c842018-06-01 09:47:18 +00001352 PyOS_setsig(SIGINT, signal_handler);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001353 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001354
1355#ifdef SIGHUP
Christian Heimes6782b142016-09-09 00:11:45 +02001356 if (PyModule_AddIntMacro(m, SIGHUP))
1357 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001358#endif
1359#ifdef SIGINT
Christian Heimes6782b142016-09-09 00:11:45 +02001360 if (PyModule_AddIntMacro(m, SIGINT))
1361 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001362#endif
Tim Peters1ce3cf72001-10-01 17:58:40 +00001363#ifdef SIGBREAK
Christian Heimes6782b142016-09-09 00:11:45 +02001364 if (PyModule_AddIntMacro(m, SIGBREAK))
1365 goto finally;
Tim Peters1ce3cf72001-10-01 17:58:40 +00001366#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001367#ifdef SIGQUIT
Christian Heimes6782b142016-09-09 00:11:45 +02001368 if (PyModule_AddIntMacro(m, SIGQUIT))
1369 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001370#endif
1371#ifdef SIGILL
Christian Heimes6782b142016-09-09 00:11:45 +02001372 if (PyModule_AddIntMacro(m, SIGILL))
1373 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001374#endif
1375#ifdef SIGTRAP
Christian Heimes6782b142016-09-09 00:11:45 +02001376 if (PyModule_AddIntMacro(m, SIGTRAP))
1377 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001378#endif
1379#ifdef SIGIOT
Christian Heimes6782b142016-09-09 00:11:45 +02001380 if (PyModule_AddIntMacro(m, SIGIOT))
1381 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001382#endif
1383#ifdef SIGABRT
Christian Heimes6782b142016-09-09 00:11:45 +02001384 if (PyModule_AddIntMacro(m, SIGABRT))
1385 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001386#endif
1387#ifdef SIGEMT
Christian Heimes6782b142016-09-09 00:11:45 +02001388 if (PyModule_AddIntMacro(m, SIGEMT))
1389 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001390#endif
1391#ifdef SIGFPE
Christian Heimes6782b142016-09-09 00:11:45 +02001392 if (PyModule_AddIntMacro(m, SIGFPE))
1393 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001394#endif
1395#ifdef SIGKILL
Christian Heimes6782b142016-09-09 00:11:45 +02001396 if (PyModule_AddIntMacro(m, SIGKILL))
1397 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001398#endif
1399#ifdef SIGBUS
Christian Heimes6782b142016-09-09 00:11:45 +02001400 if (PyModule_AddIntMacro(m, SIGBUS))
1401 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001402#endif
1403#ifdef SIGSEGV
Christian Heimes6782b142016-09-09 00:11:45 +02001404 if (PyModule_AddIntMacro(m, SIGSEGV))
1405 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001406#endif
1407#ifdef SIGSYS
Christian Heimes6782b142016-09-09 00:11:45 +02001408 if (PyModule_AddIntMacro(m, SIGSYS))
1409 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001410#endif
1411#ifdef SIGPIPE
Christian Heimes6782b142016-09-09 00:11:45 +02001412 if (PyModule_AddIntMacro(m, SIGPIPE))
1413 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001414#endif
1415#ifdef SIGALRM
Christian Heimes6782b142016-09-09 00:11:45 +02001416 if (PyModule_AddIntMacro(m, SIGALRM))
1417 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001418#endif
1419#ifdef SIGTERM
Christian Heimes6782b142016-09-09 00:11:45 +02001420 if (PyModule_AddIntMacro(m, SIGTERM))
1421 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001422#endif
1423#ifdef SIGUSR1
Christian Heimes6782b142016-09-09 00:11:45 +02001424 if (PyModule_AddIntMacro(m, SIGUSR1))
1425 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001426#endif
1427#ifdef SIGUSR2
Christian Heimes6782b142016-09-09 00:11:45 +02001428 if (PyModule_AddIntMacro(m, SIGUSR2))
1429 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001430#endif
1431#ifdef SIGCLD
Christian Heimes6782b142016-09-09 00:11:45 +02001432 if (PyModule_AddIntMacro(m, SIGCLD))
1433 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001434#endif
1435#ifdef SIGCHLD
Christian Heimes6782b142016-09-09 00:11:45 +02001436 if (PyModule_AddIntMacro(m, SIGCHLD))
1437 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001438#endif
1439#ifdef SIGPWR
Christian Heimes6782b142016-09-09 00:11:45 +02001440 if (PyModule_AddIntMacro(m, SIGPWR))
1441 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001442#endif
1443#ifdef SIGIO
Christian Heimes6782b142016-09-09 00:11:45 +02001444 if (PyModule_AddIntMacro(m, SIGIO))
1445 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001446#endif
1447#ifdef SIGURG
Christian Heimes6782b142016-09-09 00:11:45 +02001448 if (PyModule_AddIntMacro(m, SIGURG))
1449 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001450#endif
1451#ifdef SIGWINCH
Christian Heimes6782b142016-09-09 00:11:45 +02001452 if (PyModule_AddIntMacro(m, SIGWINCH))
1453 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001454#endif
1455#ifdef SIGPOLL
Christian Heimes6782b142016-09-09 00:11:45 +02001456 if (PyModule_AddIntMacro(m, SIGPOLL))
1457 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001458#endif
1459#ifdef SIGSTOP
Christian Heimes6782b142016-09-09 00:11:45 +02001460 if (PyModule_AddIntMacro(m, SIGSTOP))
1461 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001462#endif
1463#ifdef SIGTSTP
Christian Heimes6782b142016-09-09 00:11:45 +02001464 if (PyModule_AddIntMacro(m, SIGTSTP))
1465 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001466#endif
1467#ifdef SIGCONT
Christian Heimes6782b142016-09-09 00:11:45 +02001468 if (PyModule_AddIntMacro(m, SIGCONT))
1469 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001470#endif
1471#ifdef SIGTTIN
Christian Heimes6782b142016-09-09 00:11:45 +02001472 if (PyModule_AddIntMacro(m, SIGTTIN))
1473 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001474#endif
1475#ifdef SIGTTOU
Christian Heimes6782b142016-09-09 00:11:45 +02001476 if (PyModule_AddIntMacro(m, SIGTTOU))
1477 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001478#endif
1479#ifdef SIGVTALRM
Christian Heimes6782b142016-09-09 00:11:45 +02001480 if (PyModule_AddIntMacro(m, SIGVTALRM))
1481 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001482#endif
1483#ifdef SIGPROF
Christian Heimes6782b142016-09-09 00:11:45 +02001484 if (PyModule_AddIntMacro(m, SIGPROF))
1485 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001486#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001487#ifdef SIGXCPU
Christian Heimes6782b142016-09-09 00:11:45 +02001488 if (PyModule_AddIntMacro(m, SIGXCPU))
1489 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001490#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001491#ifdef SIGXFSZ
Christian Heimes6782b142016-09-09 00:11:45 +02001492 if (PyModule_AddIntMacro(m, SIGXFSZ))
1493 goto finally;
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001494#endif
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001495#ifdef SIGRTMIN
Christian Heimes6782b142016-09-09 00:11:45 +02001496 if (PyModule_AddIntMacro(m, SIGRTMIN))
1497 goto finally;
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001498#endif
1499#ifdef SIGRTMAX
Christian Heimes6782b142016-09-09 00:11:45 +02001500 if (PyModule_AddIntMacro(m, SIGRTMAX))
1501 goto finally;
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001502#endif
Martin v. Löwis175af252002-01-12 11:43:25 +00001503#ifdef SIGINFO
Christian Heimes6782b142016-09-09 00:11:45 +02001504 if (PyModule_AddIntMacro(m, SIGINFO))
1505 goto finally;
Martin v. Löwis175af252002-01-12 11:43:25 +00001506#endif
Martin v. Löwis823725e2008-03-24 13:39:54 +00001507
1508#ifdef ITIMER_REAL
Christian Heimes6782b142016-09-09 00:11:45 +02001509 if (PyModule_AddIntMacro(m, ITIMER_REAL))
1510 goto finally;
Martin v. Löwis823725e2008-03-24 13:39:54 +00001511#endif
1512#ifdef ITIMER_VIRTUAL
Christian Heimes6782b142016-09-09 00:11:45 +02001513 if (PyModule_AddIntMacro(m, ITIMER_VIRTUAL))
1514 goto finally;
Martin v. Löwis823725e2008-03-24 13:39:54 +00001515#endif
1516#ifdef ITIMER_PROF
Christian Heimes6782b142016-09-09 00:11:45 +02001517 if (PyModule_AddIntMacro(m, ITIMER_PROF))
1518 goto finally;
Martin v. Löwis823725e2008-03-24 13:39:54 +00001519#endif
1520
1521#if defined (HAVE_SETITIMER) || defined (HAVE_GETITIMER)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001522 ItimerError = PyErr_NewException("signal.ItimerError",
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +03001523 PyExc_OSError, NULL);
Neal Norwitz2f99b242008-08-24 05:48:10 +00001524 if (ItimerError != NULL)
Martin Panter6d57fe12016-09-17 03:26:16 +00001525 PyDict_SetItemString(d, "ItimerError", ItimerError);
Martin v. Löwis823725e2008-03-24 13:39:54 +00001526#endif
1527
Brian Curtineb24d742010-04-12 17:16:38 +00001528#ifdef CTRL_C_EVENT
Christian Heimes6782b142016-09-09 00:11:45 +02001529 if (PyModule_AddIntMacro(m, CTRL_C_EVENT))
1530 goto finally;
Brian Curtineb24d742010-04-12 17:16:38 +00001531#endif
1532
1533#ifdef CTRL_BREAK_EVENT
Christian Heimes6782b142016-09-09 00:11:45 +02001534 if (PyModule_AddIntMacro(m, CTRL_BREAK_EVENT))
1535 goto finally;
Brian Curtineb24d742010-04-12 17:16:38 +00001536#endif
1537
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001538#ifdef MS_WINDOWS
1539 /* Create manual-reset event, initially unset */
1540 sigint_event = CreateEvent(NULL, TRUE, FALSE, FALSE);
1541#endif
1542
Martin v. Löwis1a214512008-06-11 05:26:20 +00001543 if (PyErr_Occurred()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001544 Py_DECREF(m);
1545 m = NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001546 }
Barry Warsaw92971171997-01-03 00:14:25 +00001547
Barry Warsaw92971171997-01-03 00:14:25 +00001548 finally:
Martin v. Löwis1a214512008-06-11 05:26:20 +00001549 return m;
Guido van Rossum08c16611997-08-02 03:01:42 +00001550}
1551
1552static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001553finisignal(void)
Guido van Rossum08c16611997-08-02 03:01:42 +00001554{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001555 int i;
1556 PyObject *func;
Guido van Rossum08c16611997-08-02 03:01:42 +00001557
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001558 for (i = 1; i < NSIG; i++) {
1559 func = Handlers[i].func;
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001560 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001561 Handlers[i].func = NULL;
pkerlinge905c842018-06-01 09:47:18 +00001562 if (func != NULL && func != Py_None &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001563 func != DefaultHandler && func != IgnoreHandler)
1564 PyOS_setsig(i, SIG_DFL);
1565 Py_XDECREF(func);
1566 }
Guido van Rossum08c16611997-08-02 03:01:42 +00001567
Serhiy Storchaka505ff752014-02-09 13:33:53 +02001568 Py_CLEAR(IntHandler);
1569 Py_CLEAR(DefaultHandler);
1570 Py_CLEAR(IgnoreHandler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001571}
1572
Barry Warsaw92971171997-01-03 00:14:25 +00001573
Barry Warsaw92971171997-01-03 00:14:25 +00001574/* Declared in pyerrors.h */
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001575int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001576PyErr_CheckSignals(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001577{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001578 int i;
1579 PyObject *f;
Barry Warsaw92971171997-01-03 00:14:25 +00001580
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001581 if (!_Py_atomic_load(&is_tripped))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001582 return 0;
Christian Heimesb76922a2007-12-11 01:06:40 +00001583
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001584 if (PyThread_get_thread_ident() != main_thread)
1585 return 0;
Christian Heimesb76922a2007-12-11 01:06:40 +00001586
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001587 /*
1588 * The is_tripped variable is meant to speed up the calls to
1589 * PyErr_CheckSignals (both directly or via pending calls) when no
1590 * signal has arrived. This variable is set to 1 when a signal arrives
1591 * and it is set to 0 here, when we know some signals arrived. This way
1592 * we can run the registered handlers with no signals blocked.
1593 *
1594 * NOTE: with this approach we can have a situation where is_tripped is
1595 * 1 but we have no more signals to handle (Handlers[i].tripped
1596 * is 0 for every signal i). This won't do us any harm (except
1597 * we're gonna spent some cycles for nothing). This happens when
1598 * we receive a signal i after we zero is_tripped and before we
1599 * check Handlers[i].tripped.
1600 */
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001601 _Py_atomic_store(&is_tripped, 0);
Christian Heimesb76922a2007-12-11 01:06:40 +00001602
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001603 if (!(f = (PyObject *)PyEval_GetFrame()))
1604 f = Py_None;
Christian Heimesb76922a2007-12-11 01:06:40 +00001605
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001606 for (i = 1; i < NSIG; i++) {
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001607 if (_Py_atomic_load_relaxed(&Handlers[i].tripped)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001608 PyObject *result = NULL;
1609 PyObject *arglist = Py_BuildValue("(iO)", i, f);
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001610 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Barry Warsaw92971171997-01-03 00:14:25 +00001611
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001612 if (arglist) {
1613 result = PyEval_CallObject(Handlers[i].func,
1614 arglist);
1615 Py_DECREF(arglist);
1616 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +02001617 if (!result) {
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001618 _Py_atomic_store(&is_tripped, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001619 return -1;
Antoine Pitrouc08177a2017-06-28 23:29:29 +02001620 }
Barry Warsaw92971171997-01-03 00:14:25 +00001621
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001622 Py_DECREF(result);
1623 }
1624 }
Christian Heimesb76922a2007-12-11 01:06:40 +00001625
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001626 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001627}
1628
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +00001629
Barry Warsaw92971171997-01-03 00:14:25 +00001630/* Replacements for intrcheck.c functionality
1631 * Declared in pyerrors.h
1632 */
1633void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001634PyErr_SetInterrupt(void)
Barry Warsaw92971171997-01-03 00:14:25 +00001635{
Victor Stinner6c9b35b2011-04-18 16:25:56 +02001636 trip_signal(SIGINT);
Barry Warsaw92971171997-01-03 00:14:25 +00001637}
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001638
1639void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001640PyOS_InitInterrupts(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001641{
Giampaolo Rodola'e09fb712014-04-04 15:34:17 +02001642 PyObject *m = PyImport_ImportModule("_signal");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001643 if (m) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001644 Py_DECREF(m);
1645 }
Guido van Rossum08c16611997-08-02 03:01:42 +00001646}
1647
1648void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001649PyOS_FiniInterrupts(void)
Guido van Rossum08c16611997-08-02 03:01:42 +00001650{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001651 finisignal();
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001652}
1653
1654int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001655PyOS_InterruptOccurred(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001656{
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001657 if (_Py_atomic_load_relaxed(&Handlers[SIGINT].tripped)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001658 if (PyThread_get_thread_ident() != main_thread)
1659 return 0;
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001660 _Py_atomic_store_relaxed(&Handlers[SIGINT].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001661 return 1;
1662 }
1663 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001664}
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001665
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001666static void
1667_clear_pending_signals(void)
1668{
1669 int i;
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001670 if (!_Py_atomic_load(&is_tripped))
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001671 return;
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001672 _Py_atomic_store(&is_tripped, 0);
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001673 for (i = 1; i < NSIG; ++i) {
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001674 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001675 }
1676}
1677
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001678void
Antoine Pitrou346cbd32017-05-27 17:50:54 +02001679_PySignal_AfterFork(void)
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001680{
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001681 /* Clear the signal flags after forking so that they aren't handled
1682 * in both processes if they came in just before the fork() but before
1683 * the interpreter had an opportunity to call the handlers. issue9535. */
1684 _clear_pending_signals();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001685 main_thread = PyThread_get_thread_ident();
1686 main_pid = getpid();
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001687}
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001688
1689int
1690_PyOS_IsMainThread(void)
1691{
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001692 return PyThread_get_thread_ident() == main_thread;
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001693}
1694
1695#ifdef MS_WINDOWS
1696void *_PyOS_SigintEvent(void)
1697{
1698 /* Returns a manual-reset event which gets tripped whenever
1699 SIGINT is received.
1700
1701 Python.h does not include windows.h so we do cannot use HANDLE
1702 as the return type of this function. We use void* instead. */
1703 return sigint_event;
1704}
1705#endif