blob: 4f8f71a0a1df1337752ec47d6d930738bbac063a [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
Vladimir Matveevc24c6c22019-01-08 01:58:25 -0800393/*[clinic input]
394signal.raise_signal
395
396 signalnum: int
397 /
398
399Send a signal to the executing process.
400[clinic start generated code]*/
401
402static PyObject *
403signal_raise_signal_impl(PyObject *module, int signalnum)
404/*[clinic end generated code: output=e2b014220aa6111d input=e90c0f9a42358de6]*/
405{
406 int err;
407 Py_BEGIN_ALLOW_THREADS
408 _Py_BEGIN_SUPPRESS_IPH
409 err = raise(signalnum);
410 _Py_END_SUPPRESS_IPH
411 Py_END_ALLOW_THREADS
412
413 if (err) {
414 return PyErr_SetFromErrno(PyExc_OSError);
415 }
416 Py_RETURN_NONE;
417}
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000418
Tal Einatc7027b72015-05-16 14:14:49 +0300419/*[clinic input]
420signal.signal
421
422 signalnum: int
423 handler: object
424 /
425
426Set the action for the given signal.
427
428The action can be SIG_DFL, SIG_IGN, or a callable Python object.
429The previous action is returned. See getsignal() for possible return values.
430
431*** IMPORTANT NOTICE ***
432A signal handler function is called with two arguments:
433the first is the signal number, the second is the interrupted stack frame.
434[clinic start generated code]*/
435
Guido van Rossume4485b01994-09-07 14:32:49 +0000436static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300437signal_signal_impl(PyObject *module, int signalnum, PyObject *handler)
438/*[clinic end generated code: output=b44cfda43780f3a1 input=deee84af5fa0432c]*/
Guido van Rossume4485b01994-09-07 14:32:49 +0000439{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000440 PyObject *old_handler;
441 void (*func)(int);
Brian Curtinef9efbd2010-08-06 19:27:32 +0000442#ifdef MS_WINDOWS
Tal Einatc7027b72015-05-16 14:14:49 +0300443 /* Validate that signalnum is one of the allowable signals */
444 switch (signalnum) {
Brian Curtinc734b312010-09-06 16:04:10 +0000445 case SIGABRT: break;
Brian Curtin9e88b5a2010-10-01 14:49:24 +0000446#ifdef SIGBREAK
447 /* Issue #10003: SIGBREAK is not documented as permitted, but works
448 and corresponds to CTRL_BREAK_EVENT. */
449 case SIGBREAK: break;
450#endif
Brian Curtinc734b312010-09-06 16:04:10 +0000451 case SIGFPE: break;
452 case SIGILL: break;
453 case SIGINT: break;
454 case SIGSEGV: break;
455 case SIGTERM: break;
456 default:
457 PyErr_SetString(PyExc_ValueError, "invalid signal value");
458 return NULL;
Brian Curtinef9efbd2010-08-06 19:27:32 +0000459 }
460#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000461 if (PyThread_get_thread_ident() != main_thread) {
462 PyErr_SetString(PyExc_ValueError,
463 "signal only works in main thread");
464 return NULL;
465 }
Tal Einatc7027b72015-05-16 14:14:49 +0300466 if (signalnum < 1 || signalnum >= NSIG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000467 PyErr_SetString(PyExc_ValueError,
468 "signal number out of range");
469 return NULL;
470 }
Tal Einatc7027b72015-05-16 14:14:49 +0300471 if (handler == IgnoreHandler)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000472 func = SIG_IGN;
Tal Einatc7027b72015-05-16 14:14:49 +0300473 else if (handler == DefaultHandler)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000474 func = SIG_DFL;
Tal Einatc7027b72015-05-16 14:14:49 +0300475 else if (!PyCallable_Check(handler)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000476 PyErr_SetString(PyExc_TypeError,
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000477"signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000478 return NULL;
479 }
480 else
481 func = signal_handler;
Antoine Pitrouf6f90ff2017-11-03 19:58:46 +0100482 /* Check for pending signals before changing signal handler */
483 if (PyErr_CheckSignals()) {
484 return NULL;
485 }
Tal Einatc7027b72015-05-16 14:14:49 +0300486 if (PyOS_setsig(signalnum, func) == SIG_ERR) {
Victor Stinner388196e2011-05-10 17:13:00 +0200487 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000488 return NULL;
489 }
Tal Einatc7027b72015-05-16 14:14:49 +0300490 old_handler = Handlers[signalnum].func;
Tal Einatc7027b72015-05-16 14:14:49 +0300491 Py_INCREF(handler);
492 Handlers[signalnum].func = handler;
Antoine Pitrouc8c952c2013-05-04 23:16:59 +0200493 if (old_handler != NULL)
494 return old_handler;
495 else
496 Py_RETURN_NONE;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000497}
498
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000499
Tal Einatc7027b72015-05-16 14:14:49 +0300500/*[clinic input]
501signal.getsignal
502
503 signalnum: int
504 /
505
506Return the current action for the given signal.
507
508The return value can be:
509 SIG_IGN -- if the signal is being ignored
510 SIG_DFL -- if the default action for the signal is in effect
511 None -- if an unknown handler is in effect
512 anything else -- the callable Python object used as a handler
513[clinic start generated code]*/
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000514
Guido van Rossume4485b01994-09-07 14:32:49 +0000515static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300516signal_getsignal_impl(PyObject *module, int signalnum)
517/*[clinic end generated code: output=35b3e0e796fd555e input=ac23a00f19dfa509]*/
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000518{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000519 PyObject *old_handler;
Tal Einatc7027b72015-05-16 14:14:49 +0300520 if (signalnum < 1 || signalnum >= NSIG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000521 PyErr_SetString(PyExc_ValueError,
522 "signal number out of range");
523 return NULL;
524 }
Tal Einatc7027b72015-05-16 14:14:49 +0300525 old_handler = Handlers[signalnum].func;
Antoine Pitrouc8c952c2013-05-04 23:16:59 +0200526 if (old_handler != NULL) {
527 Py_INCREF(old_handler);
528 return old_handler;
529 }
530 else {
531 Py_RETURN_NONE;
532 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000533}
534
Antoine Pietri5d2a27d2018-03-12 14:42:34 +0100535
536/*[clinic input]
537signal.strsignal
538
539 signalnum: int
540 /
541
542Return the system description of the given signal.
543
544The return values can be such as "Interrupt", "Segmentation fault", etc.
545Returns None if the signal is not recognized.
546[clinic start generated code]*/
547
548static PyObject *
549signal_strsignal_impl(PyObject *module, int signalnum)
550/*[clinic end generated code: output=44e12e1e3b666261 input=b77914b03f856c74]*/
551{
552 char *res;
553
554 if (signalnum < 1 || signalnum >= NSIG) {
555 PyErr_SetString(PyExc_ValueError,
556 "signal number out of range");
557 return NULL;
558 }
559
Michael Osipov48ce4892018-08-23 15:27:19 +0200560#ifndef HAVE_STRSIGNAL
Antoine Pietri5d2a27d2018-03-12 14:42:34 +0100561 switch (signalnum) {
Michael Osipov48ce4892018-08-23 15:27:19 +0200562 /* Though being a UNIX, HP-UX does not provide strsignal(3). */
563#ifndef MS_WINDOWS
564 case SIGHUP:
565 res = "Hangup";
566 break;
567 case SIGALRM:
568 res = "Alarm clock";
569 break;
570 case SIGPIPE:
571 res = "Broken pipe";
572 break;
573 case SIGQUIT:
574 res = "Quit";
575 break;
576 case SIGCHLD:
577 res = "Child exited";
578 break;
579#endif
580 /* Custom redefinition of POSIX signals allowed on Windows. */
Antoine Pietri5d2a27d2018-03-12 14:42:34 +0100581 case SIGINT:
582 res = "Interrupt";
583 break;
584 case SIGILL:
585 res = "Illegal instruction";
586 break;
587 case SIGABRT:
588 res = "Aborted";
589 break;
590 case SIGFPE:
591 res = "Floating point exception";
592 break;
593 case SIGSEGV:
594 res = "Segmentation fault";
595 break;
596 case SIGTERM:
597 res = "Terminated";
598 break;
599 default:
600 Py_RETURN_NONE;
601 }
602#else
603 errno = 0;
604 res = strsignal(signalnum);
605
606 if (errno || res == NULL || strstr(res, "Unknown signal") != NULL)
607 Py_RETURN_NONE;
608#endif
609
610 return Py_BuildValue("s", res);
611}
612
Christian Heimes8640e742008-02-23 16:23:06 +0000613#ifdef HAVE_SIGINTERRUPT
Tal Einatc7027b72015-05-16 14:14:49 +0300614
615/*[clinic input]
616signal.siginterrupt
617
618 signalnum: int
619 flag: int
620 /
621
622Change system call restart behaviour.
623
624If flag is False, system calls will be restarted when interrupted by
625signal sig, else system calls will be interrupted.
626[clinic start generated code]*/
Christian Heimes8640e742008-02-23 16:23:06 +0000627
628static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300629signal_siginterrupt_impl(PyObject *module, int signalnum, int flag)
630/*[clinic end generated code: output=063816243d85dd19 input=4160acacca3e2099]*/
Christian Heimes8640e742008-02-23 16:23:06 +0000631{
Tal Einatc7027b72015-05-16 14:14:49 +0300632 if (signalnum < 1 || signalnum >= NSIG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000633 PyErr_SetString(PyExc_ValueError,
634 "signal number out of range");
635 return NULL;
636 }
Tal Einatc7027b72015-05-16 14:14:49 +0300637 if (siginterrupt(signalnum, flag)<0) {
Victor Stinner388196e2011-05-10 17:13:00 +0200638 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000639 return NULL;
640 }
Tal Einatc7027b72015-05-16 14:14:49 +0300641 Py_RETURN_NONE;
Christian Heimes8640e742008-02-23 16:23:06 +0000642}
643
644#endif
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000645
Tal Einatc7027b72015-05-16 14:14:49 +0300646
647static PyObject*
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800648signal_set_wakeup_fd(PyObject *self, PyObject *args, PyObject *kwds)
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000649{
Victor Stinnere134a7f2015-03-30 10:09:31 +0200650 struct _Py_stat_struct status;
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800651 static char *kwlist[] = {
652 "", "warn_on_full_buffer", NULL,
653 };
654 int warn_on_full_buffer = 1;
Victor Stinner11517102014-07-29 23:31:34 +0200655#ifdef MS_WINDOWS
656 PyObject *fdobj;
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100657 SOCKET_T sockfd, old_sockfd;
Victor Stinner11517102014-07-29 23:31:34 +0200658 int res;
659 int res_size = sizeof res;
660 PyObject *mod;
Victor Stinner11517102014-07-29 23:31:34 +0200661 int is_socket;
662
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800663 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|$p:set_wakeup_fd", kwlist,
664 &fdobj, &warn_on_full_buffer))
Victor Stinner11517102014-07-29 23:31:34 +0200665 return NULL;
666
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100667 sockfd = PyLong_AsSocket_t(fdobj);
668 if (sockfd == (SOCKET_T)(-1) && PyErr_Occurred())
Victor Stinner11517102014-07-29 23:31:34 +0200669 return NULL;
670#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000671 int fd, old_fd;
Victor Stinner11517102014-07-29 23:31:34 +0200672
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800673 if (!PyArg_ParseTupleAndKeywords(args, kwds, "i|$p:set_wakeup_fd", kwlist,
674 &fd, &warn_on_full_buffer))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000675 return NULL;
Victor Stinner11517102014-07-29 23:31:34 +0200676#endif
677
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000678 if (PyThread_get_thread_ident() != main_thread) {
679 PyErr_SetString(PyExc_ValueError,
680 "set_wakeup_fd only works in main thread");
681 return NULL;
682 }
Victor Stinner0bffc942014-07-21 16:28:54 +0200683
Victor Stinner11517102014-07-29 23:31:34 +0200684#ifdef MS_WINDOWS
685 is_socket = 0;
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100686 if (sockfd != INVALID_FD) {
Victor Stinner11517102014-07-29 23:31:34 +0200687 /* Import the _socket module to call WSAStartup() */
688 mod = PyImport_ImportModuleNoBlock("_socket");
689 if (mod == NULL)
690 return NULL;
691 Py_DECREF(mod);
692
693 /* test the socket */
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100694 if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR,
Victor Stinner11517102014-07-29 23:31:34 +0200695 (char *)&res, &res_size) != 0) {
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100696 int fd, err;
697
698 err = WSAGetLastError();
Victor Stinner11517102014-07-29 23:31:34 +0200699 if (err != WSAENOTSOCK) {
700 PyErr_SetExcFromWindowsErr(PyExc_OSError, err);
701 return NULL;
702 }
703
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100704 fd = (int)sockfd;
Steve Dower940f33a2016-09-08 11:21:54 -0700705 if ((SOCKET_T)fd != sockfd) {
Victor Stinner11517102014-07-29 23:31:34 +0200706 PyErr_SetString(PyExc_ValueError, "invalid fd");
707 return NULL;
708 }
709
Victor Stinnere134a7f2015-03-30 10:09:31 +0200710 if (_Py_fstat(fd, &status) != 0)
Victor Stinner11517102014-07-29 23:31:34 +0200711 return NULL;
Victor Stinner38227602014-08-27 12:59:44 +0200712
713 /* on Windows, a file cannot be set to non-blocking mode */
Victor Stinner11517102014-07-29 23:31:34 +0200714 }
Victor Stinner38227602014-08-27 12:59:44 +0200715 else {
Victor Stinner11517102014-07-29 23:31:34 +0200716 is_socket = 1;
Victor Stinner38227602014-08-27 12:59:44 +0200717
718 /* Windows does not provide a function to test if a socket
719 is in non-blocking mode */
720 }
Victor Stinner11517102014-07-29 23:31:34 +0200721 }
722
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100723 old_sockfd = wakeup.fd;
724 wakeup.fd = sockfd;
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800725 wakeup.warn_on_full_buffer = warn_on_full_buffer;
Victor Stinner11517102014-07-29 23:31:34 +0200726 wakeup.use_send = is_socket;
727
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100728 if (old_sockfd != INVALID_FD)
729 return PyLong_FromSocket_t(old_sockfd);
Victor Stinner11517102014-07-29 23:31:34 +0200730 else
731 return PyLong_FromLong(-1);
732#else
Victor Stinnerd18ccd12014-07-24 21:58:53 +0200733 if (fd != -1) {
Victor Stinner38227602014-08-27 12:59:44 +0200734 int blocking;
735
Victor Stinnere134a7f2015-03-30 10:09:31 +0200736 if (_Py_fstat(fd, &status) != 0)
Victor Stinner11517102014-07-29 23:31:34 +0200737 return NULL;
Victor Stinner38227602014-08-27 12:59:44 +0200738
739 blocking = _Py_get_blocking(fd);
740 if (blocking < 0)
741 return NULL;
742 if (blocking) {
743 PyErr_Format(PyExc_ValueError,
744 "the fd %i must be in non-blocking mode",
745 fd);
746 return NULL;
747 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000748 }
Victor Stinner0bffc942014-07-21 16:28:54 +0200749
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800750 old_fd = wakeup.fd;
751 wakeup.fd = fd;
752 wakeup.warn_on_full_buffer = warn_on_full_buffer;
Victor Stinner0bffc942014-07-21 16:28:54 +0200753
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000754 return PyLong_FromLong(old_fd);
Victor Stinner11517102014-07-29 23:31:34 +0200755#endif
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000756}
757
758PyDoc_STRVAR(set_wakeup_fd_doc,
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800759"set_wakeup_fd(fd, *, warn_on_full_buffer=True) -> fd\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000760\n\
Victor Stinner11517102014-07-29 23:31:34 +0200761Sets the fd to be written to (with the signal number) when a signal\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000762comes in. A library can use this to wakeup select or poll.\n\
Victor Stinner11517102014-07-29 23:31:34 +0200763The previous fd or -1 is returned.\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000764\n\
765The fd must be non-blocking.");
766
767/* C API for the same, without all the error checking */
768int
769PySignal_SetWakeupFd(int fd)
770{
Victor Stinner11517102014-07-29 23:31:34 +0200771 int old_fd;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000772 if (fd < 0)
773 fd = -1;
Victor Stinner11517102014-07-29 23:31:34 +0200774
775#ifdef MS_WINDOWS
776 old_fd = Py_SAFE_DOWNCAST(wakeup.fd, SOCKET_T, int);
Victor Stinner11517102014-07-29 23:31:34 +0200777#else
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800778 old_fd = wakeup.fd;
Victor Stinner11517102014-07-29 23:31:34 +0200779#endif
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800780 wakeup.fd = fd;
781 wakeup.warn_on_full_buffer = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000782 return old_fd;
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000783}
784
785
Martin v. Löwis823725e2008-03-24 13:39:54 +0000786#ifdef HAVE_SETITIMER
Tal Einatc7027b72015-05-16 14:14:49 +0300787
788/*[clinic input]
789signal.setitimer
790
791 which: int
Victor Stinneref611c92017-10-13 13:49:43 -0700792 seconds: object
793 interval: object(c_default="NULL") = 0.0
Tal Einatc7027b72015-05-16 14:14:49 +0300794 /
795
796Sets given itimer (one of ITIMER_REAL, ITIMER_VIRTUAL or ITIMER_PROF).
797
798The timer will fire after value seconds and after that every interval seconds.
799The itimer can be cleared by setting seconds to zero.
800
801Returns old values as a tuple: (delay, interval).
802[clinic start generated code]*/
803
Martin v. Löwis823725e2008-03-24 13:39:54 +0000804static PyObject *
Victor Stinneref611c92017-10-13 13:49:43 -0700805signal_setitimer_impl(PyObject *module, int which, PyObject *seconds,
806 PyObject *interval)
807/*[clinic end generated code: output=65f9dcbddc35527b input=de43daf194e6f66f]*/
Martin v. Löwis823725e2008-03-24 13:39:54 +0000808{
Martin v. Löwis823725e2008-03-24 13:39:54 +0000809 struct itimerval new, old;
810
Victor Stinneref611c92017-10-13 13:49:43 -0700811 if (timeval_from_double(seconds, &new.it_value) < 0) {
812 return NULL;
813 }
814 if (timeval_from_double(interval, &new.it_interval) < 0) {
815 return NULL;
816 }
817
Martin v. Löwis823725e2008-03-24 13:39:54 +0000818 /* Let OS check "which" value */
819 if (setitimer(which, &new, &old) != 0) {
Tal Einatc7027b72015-05-16 14:14:49 +0300820 PyErr_SetFromErrno(ItimerError);
821 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000822 }
823
824 return itimer_retval(&old);
825}
826
Martin v. Löwis823725e2008-03-24 13:39:54 +0000827#endif
828
829
830#ifdef HAVE_GETITIMER
Tal Einatc7027b72015-05-16 14:14:49 +0300831
832/*[clinic input]
833signal.getitimer
834
835 which: int
836 /
837
838Returns current value of given itimer.
839[clinic start generated code]*/
840
Martin v. Löwis823725e2008-03-24 13:39:54 +0000841static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300842signal_getitimer_impl(PyObject *module, int which)
843/*[clinic end generated code: output=9e053175d517db40 input=f7d21d38f3490627]*/
Martin v. Löwis823725e2008-03-24 13:39:54 +0000844{
Martin v. Löwis823725e2008-03-24 13:39:54 +0000845 struct itimerval old;
846
Martin v. Löwis823725e2008-03-24 13:39:54 +0000847 if (getitimer(which, &old) != 0) {
Tal Einatc7027b72015-05-16 14:14:49 +0300848 PyErr_SetFromErrno(ItimerError);
849 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000850 }
851
852 return itimer_retval(&old);
853}
854
Martin v. Löwis823725e2008-03-24 13:39:54 +0000855#endif
856
Victor Stinnerb3e72192011-05-08 01:46:11 +0200857#if defined(PYPTHREAD_SIGMASK) || defined(HAVE_SIGPENDING)
Victor Stinner35b300c2011-05-04 13:20:35 +0200858static PyObject*
859sigset_to_set(sigset_t mask)
860{
861 PyObject *signum, *result;
862 int sig;
863
864 result = PySet_New(0);
865 if (result == NULL)
866 return NULL;
867
868 for (sig = 1; sig < NSIG; sig++) {
869 if (sigismember(&mask, sig) != 1)
870 continue;
871
872 /* Handle the case where it is a member by adding the signal to
873 the result list. Ignore the other cases because they mean the
874 signal isn't a member of the mask or the signal was invalid,
875 and an invalid signal must have been our fault in constructing
876 the loop boundaries. */
877 signum = PyLong_FromLong(sig);
878 if (signum == NULL) {
879 Py_DECREF(result);
880 return NULL;
881 }
882 if (PySet_Add(result, signum) == -1) {
883 Py_DECREF(signum);
884 Py_DECREF(result);
885 return NULL;
886 }
887 Py_DECREF(signum);
888 }
889 return result;
890}
Victor Stinnerb3e72192011-05-08 01:46:11 +0200891#endif
Victor Stinner35b300c2011-05-04 13:20:35 +0200892
Victor Stinnerb3e72192011-05-08 01:46:11 +0200893#ifdef PYPTHREAD_SIGMASK
Tal Einatc7027b72015-05-16 14:14:49 +0300894
895/*[clinic input]
896signal.pthread_sigmask
897
898 how: int
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300899 mask: sigset_t
Tal Einatc7027b72015-05-16 14:14:49 +0300900 /
901
902Fetch and/or change the signal mask of the calling thread.
903[clinic start generated code]*/
904
Victor Stinnera9293352011-04-30 15:21:58 +0200905static PyObject *
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300906signal_pthread_sigmask_impl(PyObject *module, int how, sigset_t mask)
907/*[clinic end generated code: output=0562c0fb192981a8 input=85bcebda442fa77f]*/
Victor Stinnera9293352011-04-30 15:21:58 +0200908{
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300909 sigset_t previous;
Victor Stinnera9293352011-04-30 15:21:58 +0200910 int err;
911
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300912 err = pthread_sigmask(how, &mask, &previous);
Victor Stinnera9293352011-04-30 15:21:58 +0200913 if (err != 0) {
914 errno = err;
Victor Stinnerb3e72192011-05-08 01:46:11 +0200915 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnera9293352011-04-30 15:21:58 +0200916 return NULL;
917 }
918
Victor Stinnerd0e516d2011-05-03 14:57:12 +0200919 /* if signals was unblocked, signal handlers have been called */
920 if (PyErr_CheckSignals())
921 return NULL;
922
Victor Stinner35b300c2011-05-04 13:20:35 +0200923 return sigset_to_set(previous);
Victor Stinnera9293352011-04-30 15:21:58 +0200924}
925
Victor Stinnera9293352011-04-30 15:21:58 +0200926#endif /* #ifdef PYPTHREAD_SIGMASK */
927
Martin v. Löwis823725e2008-03-24 13:39:54 +0000928
Victor Stinnerb3e72192011-05-08 01:46:11 +0200929#ifdef HAVE_SIGPENDING
Tal Einatc7027b72015-05-16 14:14:49 +0300930
931/*[clinic input]
932signal.sigpending
933
934Examine pending signals.
935
936Returns a set of signal numbers that are pending for delivery to
937the calling thread.
938[clinic start generated code]*/
939
Victor Stinnerb3e72192011-05-08 01:46:11 +0200940static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300941signal_sigpending_impl(PyObject *module)
942/*[clinic end generated code: output=53375ffe89325022 input=e0036c016f874e29]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +0200943{
944 int err;
945 sigset_t mask;
946 err = sigpending(&mask);
947 if (err)
948 return PyErr_SetFromErrno(PyExc_OSError);
949 return sigset_to_set(mask);
950}
951
Victor Stinnerb3e72192011-05-08 01:46:11 +0200952#endif /* #ifdef HAVE_SIGPENDING */
953
954
955#ifdef HAVE_SIGWAIT
Tal Einatc7027b72015-05-16 14:14:49 +0300956
957/*[clinic input]
958signal.sigwait
959
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300960 sigset: sigset_t
Tal Einatc7027b72015-05-16 14:14:49 +0300961 /
962
963Wait for a signal.
964
965Suspend execution of the calling thread until the delivery of one of the
966signals specified in the signal set sigset. The function accepts the signal
967and returns the signal number.
968[clinic start generated code]*/
969
Victor Stinnerb3e72192011-05-08 01:46:11 +0200970static PyObject *
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300971signal_sigwait_impl(PyObject *module, sigset_t sigset)
972/*[clinic end generated code: output=f43770699d682f96 input=a6fbd47b1086d119]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +0200973{
Victor Stinnerb3e72192011-05-08 01:46:11 +0200974 int err, signum;
975
Victor Stinner10c30d62011-06-10 01:39:53 +0200976 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300977 err = sigwait(&sigset, &signum);
Victor Stinner10c30d62011-06-10 01:39:53 +0200978 Py_END_ALLOW_THREADS
Victor Stinnerb3e72192011-05-08 01:46:11 +0200979 if (err) {
980 errno = err;
981 return PyErr_SetFromErrno(PyExc_OSError);
982 }
983
984 return PyLong_FromLong(signum);
985}
986
Tal Einatc7027b72015-05-16 14:14:49 +0300987#endif /* #ifdef HAVE_SIGWAIT */
988
Victor Stinnerb3e72192011-05-08 01:46:11 +0200989
Antoine Pitrou9d3627e2018-05-04 13:00:50 +0200990#if defined(HAVE_SIGFILLSET) || defined(MS_WINDOWS)
991
992/*[clinic input]
993signal.valid_signals
994
995Return a set of valid signal numbers on this platform.
996
997The signal numbers returned by this function can be safely passed to
998functions like `pthread_sigmask`.
999[clinic start generated code]*/
1000
1001static PyObject *
1002signal_valid_signals_impl(PyObject *module)
1003/*[clinic end generated code: output=1609cffbcfcf1314 input=86a3717ff25288f2]*/
1004{
1005#ifdef MS_WINDOWS
1006#ifdef SIGBREAK
1007 PyObject *tup = Py_BuildValue("(iiiiiii)", SIGABRT, SIGBREAK, SIGFPE,
1008 SIGILL, SIGINT, SIGSEGV, SIGTERM);
1009#else
1010 PyObject *tup = Py_BuildValue("(iiiiii)", SIGABRT, SIGFPE, SIGILL,
1011 SIGINT, SIGSEGV, SIGTERM);
1012#endif
1013 if (tup == NULL) {
1014 return NULL;
1015 }
1016 PyObject *set = PySet_New(tup);
1017 Py_DECREF(tup);
1018 return set;
1019#else
1020 sigset_t mask;
1021 if (sigemptyset(&mask) || sigfillset(&mask)) {
1022 return PyErr_SetFromErrno(PyExc_OSError);
1023 }
1024 return sigset_to_set(mask);
1025#endif
1026}
1027
1028#endif /* #if defined(HAVE_SIGFILLSET) || defined(MS_WINDOWS) */
1029
1030
Ross Lagerwallbc808222011-06-25 12:13:40 +02001031#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
1032static int initialized;
1033static PyStructSequence_Field struct_siginfo_fields[] = {
1034 {"si_signo", "signal number"},
1035 {"si_code", "signal code"},
1036 {"si_errno", "errno associated with this signal"},
1037 {"si_pid", "sending process ID"},
1038 {"si_uid", "real user ID of sending process"},
1039 {"si_status", "exit value or signal"},
1040 {"si_band", "band event for SIGPOLL"},
1041 {0}
1042};
1043
1044PyDoc_STRVAR(struct_siginfo__doc__,
1045"struct_siginfo: Result from sigwaitinfo or sigtimedwait.\n\n\
1046This object may be accessed either as a tuple of\n\
1047(si_signo, si_code, si_errno, si_pid, si_uid, si_status, si_band),\n\
1048or via the attributes si_signo, si_code, and so on.");
1049
1050static PyStructSequence_Desc struct_siginfo_desc = {
1051 "signal.struct_siginfo", /* name */
1052 struct_siginfo__doc__, /* doc */
1053 struct_siginfo_fields, /* fields */
1054 7 /* n_in_sequence */
1055};
1056
1057static PyTypeObject SiginfoType;
1058
1059static PyObject *
1060fill_siginfo(siginfo_t *si)
1061{
1062 PyObject *result = PyStructSequence_New(&SiginfoType);
1063 if (!result)
1064 return NULL;
1065
1066 PyStructSequence_SET_ITEM(result, 0, PyLong_FromLong((long)(si->si_signo)));
1067 PyStructSequence_SET_ITEM(result, 1, PyLong_FromLong((long)(si->si_code)));
1068 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si->si_errno)));
1069 PyStructSequence_SET_ITEM(result, 3, PyLong_FromPid(si->si_pid));
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02001070 PyStructSequence_SET_ITEM(result, 4, _PyLong_FromUid(si->si_uid));
Ross Lagerwallbc808222011-06-25 12:13:40 +02001071 PyStructSequence_SET_ITEM(result, 5,
1072 PyLong_FromLong((long)(si->si_status)));
Zachary Ware6a6967e2016-10-01 00:47:27 -05001073#ifdef HAVE_SIGINFO_T_SI_BAND
Ross Lagerwallbc808222011-06-25 12:13:40 +02001074 PyStructSequence_SET_ITEM(result, 6, PyLong_FromLong(si->si_band));
Zachary Ware6a6967e2016-10-01 00:47:27 -05001075#else
1076 PyStructSequence_SET_ITEM(result, 6, PyLong_FromLong(0L));
1077#endif
Ross Lagerwallbc808222011-06-25 12:13:40 +02001078 if (PyErr_Occurred()) {
1079 Py_DECREF(result);
1080 return NULL;
1081 }
1082
1083 return result;
1084}
1085#endif
1086
1087#ifdef HAVE_SIGWAITINFO
Tal Einatc7027b72015-05-16 14:14:49 +03001088
1089/*[clinic input]
1090signal.sigwaitinfo
1091
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001092 sigset: sigset_t
Tal Einatc7027b72015-05-16 14:14:49 +03001093 /
1094
1095Wait synchronously until one of the signals in *sigset* is delivered.
1096
1097Returns a struct_siginfo containing information about the signal.
1098[clinic start generated code]*/
1099
Ross Lagerwallbc808222011-06-25 12:13:40 +02001100static PyObject *
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001101signal_sigwaitinfo_impl(PyObject *module, sigset_t sigset)
1102/*[clinic end generated code: output=1eb2f1fa236fdbca input=3d1a7e1f27fc664c]*/
Ross Lagerwallbc808222011-06-25 12:13:40 +02001103{
Ross Lagerwallbc808222011-06-25 12:13:40 +02001104 siginfo_t si;
1105 int err;
Victor Stinnera453cd82015-03-20 12:54:28 +01001106 int async_err = 0;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001107
Victor Stinnera453cd82015-03-20 12:54:28 +01001108 do {
1109 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001110 err = sigwaitinfo(&sigset, &si);
Victor Stinnera453cd82015-03-20 12:54:28 +01001111 Py_END_ALLOW_THREADS
1112 } while (err == -1
1113 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Ross Lagerwallbc808222011-06-25 12:13:40 +02001114 if (err == -1)
Victor Stinnera453cd82015-03-20 12:54:28 +01001115 return (!async_err) ? PyErr_SetFromErrno(PyExc_OSError) : NULL;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001116
1117 return fill_siginfo(&si);
1118}
1119
Ross Lagerwallbc808222011-06-25 12:13:40 +02001120#endif /* #ifdef HAVE_SIGWAITINFO */
1121
1122#ifdef HAVE_SIGTIMEDWAIT
Tal Einatc7027b72015-05-16 14:14:49 +03001123
1124/*[clinic input]
1125signal.sigtimedwait
1126
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001127 sigset: sigset_t
Serhiy Storchaka6b680cd2015-05-16 15:57:56 +03001128 timeout as timeout_obj: object
Tal Einatc7027b72015-05-16 14:14:49 +03001129 /
1130
1131Like sigwaitinfo(), but with a timeout.
1132
1133The timeout is specified in seconds, with floating point numbers allowed.
1134[clinic start generated code]*/
1135
Ross Lagerwallbc808222011-06-25 12:13:40 +02001136static PyObject *
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001137signal_sigtimedwait_impl(PyObject *module, sigset_t sigset,
Serhiy Storchaka6b680cd2015-05-16 15:57:56 +03001138 PyObject *timeout_obj)
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001139/*[clinic end generated code: output=59c8971e8ae18a64 input=87fd39237cf0b7ba]*/
Ross Lagerwallbc808222011-06-25 12:13:40 +02001140{
Victor Stinnera453cd82015-03-20 12:54:28 +01001141 struct timespec ts;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001142 siginfo_t si;
Victor Stinnera453cd82015-03-20 12:54:28 +01001143 int res;
Victor Stinner34dc0f42015-03-27 18:19:03 +01001144 _PyTime_t timeout, deadline, monotonic;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001145
Victor Stinner869e1772015-03-30 03:49:14 +02001146 if (_PyTime_FromSecondsObject(&timeout,
1147 timeout_obj, _PyTime_ROUND_CEILING) < 0)
Ross Lagerwallbc808222011-06-25 12:13:40 +02001148 return NULL;
1149
Victor Stinnera453cd82015-03-20 12:54:28 +01001150 if (timeout < 0) {
Ross Lagerwallbc808222011-06-25 12:13:40 +02001151 PyErr_SetString(PyExc_ValueError, "timeout must be non-negative");
1152 return NULL;
1153 }
1154
Victor Stinner34dc0f42015-03-27 18:19:03 +01001155 deadline = _PyTime_GetMonotonicClock() + timeout;
Victor Stinnera453cd82015-03-20 12:54:28 +01001156
1157 do {
Victor Stinner34dc0f42015-03-27 18:19:03 +01001158 if (_PyTime_AsTimespec(timeout, &ts) < 0)
1159 return NULL;
Victor Stinnera453cd82015-03-20 12:54:28 +01001160
1161 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001162 res = sigtimedwait(&sigset, &si, &ts);
Victor Stinnera453cd82015-03-20 12:54:28 +01001163 Py_END_ALLOW_THREADS
1164
1165 if (res != -1)
1166 break;
1167
1168 if (errno != EINTR) {
1169 if (errno == EAGAIN)
1170 Py_RETURN_NONE;
1171 else
1172 return PyErr_SetFromErrno(PyExc_OSError);
1173 }
1174
1175 /* sigtimedwait() was interrupted by a signal (EINTR) */
1176 if (PyErr_CheckSignals())
1177 return NULL;
1178
Victor Stinner34dc0f42015-03-27 18:19:03 +01001179 monotonic = _PyTime_GetMonotonicClock();
1180 timeout = deadline - monotonic;
Victor Stinner6aa446c2015-03-30 21:33:51 +02001181 if (timeout < 0)
Victor Stinnera453cd82015-03-20 12:54:28 +01001182 break;
1183 } while (1);
Ross Lagerwallbc808222011-06-25 12:13:40 +02001184
1185 return fill_siginfo(&si);
1186}
1187
Ross Lagerwallbc808222011-06-25 12:13:40 +02001188#endif /* #ifdef HAVE_SIGTIMEDWAIT */
1189
Victor Stinnerb3e72192011-05-08 01:46:11 +02001190
Antoine Pitroua6a4dc82017-09-07 18:56:24 +02001191#if defined(HAVE_PTHREAD_KILL)
Tal Einatc7027b72015-05-16 14:14:49 +03001192
1193/*[clinic input]
1194signal.pthread_kill
1195
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +02001196 thread_id: unsigned_long(bitwise=True)
Tal Einatc7027b72015-05-16 14:14:49 +03001197 signalnum: int
1198 /
1199
1200Send a signal to a thread.
1201[clinic start generated code]*/
1202
Victor Stinnerb3e72192011-05-08 01:46:11 +02001203static PyObject *
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +02001204signal_pthread_kill_impl(PyObject *module, unsigned long thread_id,
1205 int signalnum)
1206/*[clinic end generated code: output=7629919b791bc27f input=1d901f2c7bb544ff]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +02001207{
Victor Stinnerb3e72192011-05-08 01:46:11 +02001208 int err;
1209
Tal Einatc7027b72015-05-16 14:14:49 +03001210 err = pthread_kill((pthread_t)thread_id, signalnum);
Victor Stinnerb3e72192011-05-08 01:46:11 +02001211 if (err != 0) {
1212 errno = err;
1213 PyErr_SetFromErrno(PyExc_OSError);
1214 return NULL;
1215 }
1216
1217 /* the signal may have been send to the current thread */
1218 if (PyErr_CheckSignals())
1219 return NULL;
1220
1221 Py_RETURN_NONE;
1222}
1223
Antoine Pitroua6a4dc82017-09-07 18:56:24 +02001224#endif /* #if defined(HAVE_PTHREAD_KILL) */
Victor Stinnerb3e72192011-05-08 01:46:11 +02001225
1226
1227
Tal Einatc7027b72015-05-16 14:14:49 +03001228/* List of functions defined in the module -- some of the methoddefs are
1229 defined to nothing if the corresponding C function is not available. */
Barry Warsaw92971171997-01-03 00:14:25 +00001230static PyMethodDef signal_methods[] = {
Tal Einatc7027b72015-05-16 14:14:49 +03001231 {"default_int_handler", signal_default_int_handler, METH_VARARGS, default_int_handler_doc},
1232 SIGNAL_ALARM_METHODDEF
1233 SIGNAL_SETITIMER_METHODDEF
1234 SIGNAL_GETITIMER_METHODDEF
1235 SIGNAL_SIGNAL_METHODDEF
Vladimir Matveevc24c6c22019-01-08 01:58:25 -08001236 SIGNAL_RAISE_SIGNAL_METHODDEF
Antoine Pietri5d2a27d2018-03-12 14:42:34 +01001237 SIGNAL_STRSIGNAL_METHODDEF
Tal Einatc7027b72015-05-16 14:14:49 +03001238 SIGNAL_GETSIGNAL_METHODDEF
Serhiy Storchaka62be7422018-11-27 13:27:31 +02001239 {"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 +03001240 SIGNAL_SIGINTERRUPT_METHODDEF
1241 SIGNAL_PAUSE_METHODDEF
1242 SIGNAL_PTHREAD_KILL_METHODDEF
1243 SIGNAL_PTHREAD_SIGMASK_METHODDEF
1244 SIGNAL_SIGPENDING_METHODDEF
1245 SIGNAL_SIGWAIT_METHODDEF
1246 SIGNAL_SIGWAITINFO_METHODDEF
1247 SIGNAL_SIGTIMEDWAIT_METHODDEF
Antoine Pitrou9d3627e2018-05-04 13:00:50 +02001248#if defined(HAVE_SIGFILLSET) || defined(MS_WINDOWS)
1249 SIGNAL_VALID_SIGNALS_METHODDEF
1250#endif
Tal Einatc7027b72015-05-16 14:14:49 +03001251 {NULL, NULL} /* sentinel */
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001252};
1253
Barry Warsaw92971171997-01-03 00:14:25 +00001254
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001255PyDoc_STRVAR(module_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001256"This module provides mechanisms to use signal handlers in Python.\n\
1257\n\
1258Functions:\n\
1259\n\
1260alarm() -- cause SIGALRM after a specified time [Unix only]\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001261setitimer() -- cause a signal (described below) after a specified\n\
1262 float time and the timer may restart then [Unix only]\n\
1263getitimer() -- get current value of timer [Unix only]\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001264signal() -- set the action for a given signal\n\
1265getsignal() -- get the signal action for a given signal\n\
1266pause() -- wait until a signal arrives [Unix only]\n\
1267default_int_handler() -- default SIGINT handler\n\
1268\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001269signal constants:\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001270SIG_DFL -- used to refer to the system default handler\n\
1271SIG_IGN -- used to ignore the signal\n\
1272NSIG -- number of defined signals\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001273SIGINT, SIGTERM, etc. -- signal numbers\n\
1274\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001275itimer constants:\n\
1276ITIMER_REAL -- decrements in real time, and delivers SIGALRM upon\n\
1277 expiration\n\
1278ITIMER_VIRTUAL -- decrements only when the process is executing,\n\
1279 and delivers SIGVTALRM upon expiration\n\
1280ITIMER_PROF -- decrements both when the process is executing and\n\
1281 when the system is executing on behalf of the process.\n\
1282 Coupled with ITIMER_VIRTUAL, this timer is usually\n\
1283 used to profile the time spent by the application\n\
1284 in user and kernel space. SIGPROF is delivered upon\n\
1285 expiration.\n\
1286\n\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001287*** IMPORTANT NOTICE ***\n\
1288A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001289the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001290
Martin v. Löwis1a214512008-06-11 05:26:20 +00001291static struct PyModuleDef signalmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001292 PyModuleDef_HEAD_INIT,
Brett Cannon815a6f32014-04-04 10:20:28 -04001293 "_signal",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001294 module_doc,
1295 -1,
1296 signal_methods,
1297 NULL,
1298 NULL,
1299 NULL,
1300 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001301};
1302
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001303PyMODINIT_FUNC
Giampaolo Rodola'e09fb712014-04-04 15:34:17 +02001304PyInit__signal(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001305{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001306 PyObject *m, *d, *x;
1307 int i;
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001308
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001309 main_thread = PyThread_get_thread_ident();
1310 main_pid = getpid();
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001311
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001312 /* Create the module and add the functions */
1313 m = PyModule_Create(&signalmodule);
1314 if (m == NULL)
1315 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001316
Ross Lagerwallbc808222011-06-25 12:13:40 +02001317#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
Victor Stinner1c8f0592013-07-22 22:24:54 +02001318 if (!initialized) {
1319 if (PyStructSequence_InitType2(&SiginfoType, &struct_siginfo_desc) < 0)
1320 return NULL;
1321 }
Ross Lagerwallbc808222011-06-25 12:13:40 +02001322 Py_INCREF((PyObject*) &SiginfoType);
1323 PyModule_AddObject(m, "struct_siginfo", (PyObject*) &SiginfoType);
1324 initialized = 1;
1325#endif
1326
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001327 /* Add some symbolic constants to the module */
1328 d = PyModule_GetDict(m);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001329
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001330 x = DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL);
1331 if (!x || PyDict_SetItemString(d, "SIG_DFL", x) < 0)
1332 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001333
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001334 x = IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN);
1335 if (!x || PyDict_SetItemString(d, "SIG_IGN", x) < 0)
1336 goto finally;
Barry Warsaw92971171997-01-03 00:14:25 +00001337
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001338 x = PyLong_FromLong((long)NSIG);
1339 if (!x || PyDict_SetItemString(d, "NSIG", x) < 0)
1340 goto finally;
1341 Py_DECREF(x);
Barry Warsaw92971171997-01-03 00:14:25 +00001342
Victor Stinnera9293352011-04-30 15:21:58 +02001343#ifdef SIG_BLOCK
Victor Stinner72c53b52011-05-02 16:15:43 +02001344 if (PyModule_AddIntMacro(m, SIG_BLOCK))
1345 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001346#endif
Victor Stinnera9293352011-04-30 15:21:58 +02001347#ifdef SIG_UNBLOCK
Victor Stinner72c53b52011-05-02 16:15:43 +02001348 if (PyModule_AddIntMacro(m, SIG_UNBLOCK))
1349 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001350#endif
Victor Stinnera9293352011-04-30 15:21:58 +02001351#ifdef SIG_SETMASK
Victor Stinner72c53b52011-05-02 16:15:43 +02001352 if (PyModule_AddIntMacro(m, SIG_SETMASK))
1353 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001354#endif
1355
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001356 x = IntHandler = PyDict_GetItemString(d, "default_int_handler");
1357 if (!x)
1358 goto finally;
1359 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +00001360
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001361 _Py_atomic_store_relaxed(&Handlers[0].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001362 for (i = 1; i < NSIG; i++) {
1363 void (*t)(int);
1364 t = PyOS_getsig(i);
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001365 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001366 if (t == SIG_DFL)
1367 Handlers[i].func = DefaultHandler;
1368 else if (t == SIG_IGN)
1369 Handlers[i].func = IgnoreHandler;
1370 else
1371 Handlers[i].func = Py_None; /* None of our business */
1372 Py_INCREF(Handlers[i].func);
1373 }
1374 if (Handlers[SIGINT].func == DefaultHandler) {
1375 /* Install default int handler */
1376 Py_INCREF(IntHandler);
Serhiy Storchaka57a01d32016-04-10 18:05:40 +03001377 Py_SETREF(Handlers[SIGINT].func, IntHandler);
pkerlinge905c842018-06-01 09:47:18 +00001378 PyOS_setsig(SIGINT, signal_handler);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001379 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001380
1381#ifdef SIGHUP
Christian Heimes6782b142016-09-09 00:11:45 +02001382 if (PyModule_AddIntMacro(m, SIGHUP))
1383 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001384#endif
1385#ifdef SIGINT
Christian Heimes6782b142016-09-09 00:11:45 +02001386 if (PyModule_AddIntMacro(m, SIGINT))
1387 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001388#endif
Tim Peters1ce3cf72001-10-01 17:58:40 +00001389#ifdef SIGBREAK
Christian Heimes6782b142016-09-09 00:11:45 +02001390 if (PyModule_AddIntMacro(m, SIGBREAK))
1391 goto finally;
Tim Peters1ce3cf72001-10-01 17:58:40 +00001392#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001393#ifdef SIGQUIT
Christian Heimes6782b142016-09-09 00:11:45 +02001394 if (PyModule_AddIntMacro(m, SIGQUIT))
1395 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001396#endif
1397#ifdef SIGILL
Christian Heimes6782b142016-09-09 00:11:45 +02001398 if (PyModule_AddIntMacro(m, SIGILL))
1399 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001400#endif
1401#ifdef SIGTRAP
Christian Heimes6782b142016-09-09 00:11:45 +02001402 if (PyModule_AddIntMacro(m, SIGTRAP))
1403 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001404#endif
1405#ifdef SIGIOT
Christian Heimes6782b142016-09-09 00:11:45 +02001406 if (PyModule_AddIntMacro(m, SIGIOT))
1407 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001408#endif
1409#ifdef SIGABRT
Christian Heimes6782b142016-09-09 00:11:45 +02001410 if (PyModule_AddIntMacro(m, SIGABRT))
1411 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001412#endif
1413#ifdef SIGEMT
Christian Heimes6782b142016-09-09 00:11:45 +02001414 if (PyModule_AddIntMacro(m, SIGEMT))
1415 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001416#endif
1417#ifdef SIGFPE
Christian Heimes6782b142016-09-09 00:11:45 +02001418 if (PyModule_AddIntMacro(m, SIGFPE))
1419 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001420#endif
1421#ifdef SIGKILL
Christian Heimes6782b142016-09-09 00:11:45 +02001422 if (PyModule_AddIntMacro(m, SIGKILL))
1423 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001424#endif
1425#ifdef SIGBUS
Christian Heimes6782b142016-09-09 00:11:45 +02001426 if (PyModule_AddIntMacro(m, SIGBUS))
1427 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001428#endif
1429#ifdef SIGSEGV
Christian Heimes6782b142016-09-09 00:11:45 +02001430 if (PyModule_AddIntMacro(m, SIGSEGV))
1431 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001432#endif
1433#ifdef SIGSYS
Christian Heimes6782b142016-09-09 00:11:45 +02001434 if (PyModule_AddIntMacro(m, SIGSYS))
1435 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001436#endif
1437#ifdef SIGPIPE
Christian Heimes6782b142016-09-09 00:11:45 +02001438 if (PyModule_AddIntMacro(m, SIGPIPE))
1439 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001440#endif
1441#ifdef SIGALRM
Christian Heimes6782b142016-09-09 00:11:45 +02001442 if (PyModule_AddIntMacro(m, SIGALRM))
1443 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001444#endif
1445#ifdef SIGTERM
Christian Heimes6782b142016-09-09 00:11:45 +02001446 if (PyModule_AddIntMacro(m, SIGTERM))
1447 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001448#endif
1449#ifdef SIGUSR1
Christian Heimes6782b142016-09-09 00:11:45 +02001450 if (PyModule_AddIntMacro(m, SIGUSR1))
1451 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001452#endif
1453#ifdef SIGUSR2
Christian Heimes6782b142016-09-09 00:11:45 +02001454 if (PyModule_AddIntMacro(m, SIGUSR2))
1455 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001456#endif
1457#ifdef SIGCLD
Christian Heimes6782b142016-09-09 00:11:45 +02001458 if (PyModule_AddIntMacro(m, SIGCLD))
1459 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001460#endif
1461#ifdef SIGCHLD
Christian Heimes6782b142016-09-09 00:11:45 +02001462 if (PyModule_AddIntMacro(m, SIGCHLD))
1463 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001464#endif
1465#ifdef SIGPWR
Christian Heimes6782b142016-09-09 00:11:45 +02001466 if (PyModule_AddIntMacro(m, SIGPWR))
1467 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001468#endif
1469#ifdef SIGIO
Christian Heimes6782b142016-09-09 00:11:45 +02001470 if (PyModule_AddIntMacro(m, SIGIO))
1471 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001472#endif
1473#ifdef SIGURG
Christian Heimes6782b142016-09-09 00:11:45 +02001474 if (PyModule_AddIntMacro(m, SIGURG))
1475 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001476#endif
1477#ifdef SIGWINCH
Christian Heimes6782b142016-09-09 00:11:45 +02001478 if (PyModule_AddIntMacro(m, SIGWINCH))
1479 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001480#endif
1481#ifdef SIGPOLL
Christian Heimes6782b142016-09-09 00:11:45 +02001482 if (PyModule_AddIntMacro(m, SIGPOLL))
1483 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001484#endif
1485#ifdef SIGSTOP
Christian Heimes6782b142016-09-09 00:11:45 +02001486 if (PyModule_AddIntMacro(m, SIGSTOP))
1487 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001488#endif
1489#ifdef SIGTSTP
Christian Heimes6782b142016-09-09 00:11:45 +02001490 if (PyModule_AddIntMacro(m, SIGTSTP))
1491 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001492#endif
1493#ifdef SIGCONT
Christian Heimes6782b142016-09-09 00:11:45 +02001494 if (PyModule_AddIntMacro(m, SIGCONT))
1495 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001496#endif
1497#ifdef SIGTTIN
Christian Heimes6782b142016-09-09 00:11:45 +02001498 if (PyModule_AddIntMacro(m, SIGTTIN))
1499 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001500#endif
1501#ifdef SIGTTOU
Christian Heimes6782b142016-09-09 00:11:45 +02001502 if (PyModule_AddIntMacro(m, SIGTTOU))
1503 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001504#endif
1505#ifdef SIGVTALRM
Christian Heimes6782b142016-09-09 00:11:45 +02001506 if (PyModule_AddIntMacro(m, SIGVTALRM))
1507 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001508#endif
1509#ifdef SIGPROF
Christian Heimes6782b142016-09-09 00:11:45 +02001510 if (PyModule_AddIntMacro(m, SIGPROF))
1511 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001512#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001513#ifdef SIGXCPU
Christian Heimes6782b142016-09-09 00:11:45 +02001514 if (PyModule_AddIntMacro(m, SIGXCPU))
1515 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001516#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001517#ifdef SIGXFSZ
Christian Heimes6782b142016-09-09 00:11:45 +02001518 if (PyModule_AddIntMacro(m, SIGXFSZ))
1519 goto finally;
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001520#endif
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001521#ifdef SIGRTMIN
Christian Heimes6782b142016-09-09 00:11:45 +02001522 if (PyModule_AddIntMacro(m, SIGRTMIN))
1523 goto finally;
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001524#endif
1525#ifdef SIGRTMAX
Christian Heimes6782b142016-09-09 00:11:45 +02001526 if (PyModule_AddIntMacro(m, SIGRTMAX))
1527 goto finally;
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001528#endif
Martin v. Löwis175af252002-01-12 11:43:25 +00001529#ifdef SIGINFO
Christian Heimes6782b142016-09-09 00:11:45 +02001530 if (PyModule_AddIntMacro(m, SIGINFO))
1531 goto finally;
Martin v. Löwis175af252002-01-12 11:43:25 +00001532#endif
Martin v. Löwis823725e2008-03-24 13:39:54 +00001533
1534#ifdef ITIMER_REAL
Christian Heimes6782b142016-09-09 00:11:45 +02001535 if (PyModule_AddIntMacro(m, ITIMER_REAL))
1536 goto finally;
Martin v. Löwis823725e2008-03-24 13:39:54 +00001537#endif
1538#ifdef ITIMER_VIRTUAL
Christian Heimes6782b142016-09-09 00:11:45 +02001539 if (PyModule_AddIntMacro(m, ITIMER_VIRTUAL))
1540 goto finally;
Martin v. Löwis823725e2008-03-24 13:39:54 +00001541#endif
1542#ifdef ITIMER_PROF
Christian Heimes6782b142016-09-09 00:11:45 +02001543 if (PyModule_AddIntMacro(m, ITIMER_PROF))
1544 goto finally;
Martin v. Löwis823725e2008-03-24 13:39:54 +00001545#endif
1546
1547#if defined (HAVE_SETITIMER) || defined (HAVE_GETITIMER)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001548 ItimerError = PyErr_NewException("signal.ItimerError",
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +03001549 PyExc_OSError, NULL);
Neal Norwitz2f99b242008-08-24 05:48:10 +00001550 if (ItimerError != NULL)
Martin Panter6d57fe12016-09-17 03:26:16 +00001551 PyDict_SetItemString(d, "ItimerError", ItimerError);
Martin v. Löwis823725e2008-03-24 13:39:54 +00001552#endif
1553
Brian Curtineb24d742010-04-12 17:16:38 +00001554#ifdef CTRL_C_EVENT
Christian Heimes6782b142016-09-09 00:11:45 +02001555 if (PyModule_AddIntMacro(m, CTRL_C_EVENT))
1556 goto finally;
Brian Curtineb24d742010-04-12 17:16:38 +00001557#endif
1558
1559#ifdef CTRL_BREAK_EVENT
Christian Heimes6782b142016-09-09 00:11:45 +02001560 if (PyModule_AddIntMacro(m, CTRL_BREAK_EVENT))
1561 goto finally;
Brian Curtineb24d742010-04-12 17:16:38 +00001562#endif
1563
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001564#ifdef MS_WINDOWS
1565 /* Create manual-reset event, initially unset */
1566 sigint_event = CreateEvent(NULL, TRUE, FALSE, FALSE);
1567#endif
1568
Martin v. Löwis1a214512008-06-11 05:26:20 +00001569 if (PyErr_Occurred()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001570 Py_DECREF(m);
1571 m = NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001572 }
Barry Warsaw92971171997-01-03 00:14:25 +00001573
Barry Warsaw92971171997-01-03 00:14:25 +00001574 finally:
Martin v. Löwis1a214512008-06-11 05:26:20 +00001575 return m;
Guido van Rossum08c16611997-08-02 03:01:42 +00001576}
1577
1578static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001579finisignal(void)
Guido van Rossum08c16611997-08-02 03:01:42 +00001580{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001581 int i;
1582 PyObject *func;
Guido van Rossum08c16611997-08-02 03:01:42 +00001583
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001584 for (i = 1; i < NSIG; i++) {
1585 func = Handlers[i].func;
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001586 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001587 Handlers[i].func = NULL;
pkerlinge905c842018-06-01 09:47:18 +00001588 if (func != NULL && func != Py_None &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001589 func != DefaultHandler && func != IgnoreHandler)
1590 PyOS_setsig(i, SIG_DFL);
1591 Py_XDECREF(func);
1592 }
Guido van Rossum08c16611997-08-02 03:01:42 +00001593
Serhiy Storchaka505ff752014-02-09 13:33:53 +02001594 Py_CLEAR(IntHandler);
1595 Py_CLEAR(DefaultHandler);
1596 Py_CLEAR(IgnoreHandler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001597}
1598
Barry Warsaw92971171997-01-03 00:14:25 +00001599
Barry Warsaw92971171997-01-03 00:14:25 +00001600/* Declared in pyerrors.h */
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001601int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001602PyErr_CheckSignals(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001603{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001604 int i;
1605 PyObject *f;
Barry Warsaw92971171997-01-03 00:14:25 +00001606
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001607 if (!_Py_atomic_load(&is_tripped))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001608 return 0;
Christian Heimesb76922a2007-12-11 01:06:40 +00001609
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001610 if (PyThread_get_thread_ident() != main_thread)
1611 return 0;
Christian Heimesb76922a2007-12-11 01:06:40 +00001612
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001613 /*
1614 * The is_tripped variable is meant to speed up the calls to
1615 * PyErr_CheckSignals (both directly or via pending calls) when no
1616 * signal has arrived. This variable is set to 1 when a signal arrives
1617 * and it is set to 0 here, when we know some signals arrived. This way
1618 * we can run the registered handlers with no signals blocked.
1619 *
1620 * NOTE: with this approach we can have a situation where is_tripped is
1621 * 1 but we have no more signals to handle (Handlers[i].tripped
1622 * is 0 for every signal i). This won't do us any harm (except
1623 * we're gonna spent some cycles for nothing). This happens when
1624 * we receive a signal i after we zero is_tripped and before we
1625 * check Handlers[i].tripped.
1626 */
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001627 _Py_atomic_store(&is_tripped, 0);
Christian Heimesb76922a2007-12-11 01:06:40 +00001628
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001629 if (!(f = (PyObject *)PyEval_GetFrame()))
1630 f = Py_None;
Christian Heimesb76922a2007-12-11 01:06:40 +00001631
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001632 for (i = 1; i < NSIG; i++) {
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001633 if (_Py_atomic_load_relaxed(&Handlers[i].tripped)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001634 PyObject *result = NULL;
1635 PyObject *arglist = Py_BuildValue("(iO)", i, f);
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001636 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Barry Warsaw92971171997-01-03 00:14:25 +00001637
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001638 if (arglist) {
1639 result = PyEval_CallObject(Handlers[i].func,
1640 arglist);
1641 Py_DECREF(arglist);
1642 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +02001643 if (!result) {
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001644 _Py_atomic_store(&is_tripped, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001645 return -1;
Antoine Pitrouc08177a2017-06-28 23:29:29 +02001646 }
Barry Warsaw92971171997-01-03 00:14:25 +00001647
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001648 Py_DECREF(result);
1649 }
1650 }
Christian Heimesb76922a2007-12-11 01:06:40 +00001651
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001652 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001653}
1654
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +00001655
Barry Warsaw92971171997-01-03 00:14:25 +00001656/* Replacements for intrcheck.c functionality
1657 * Declared in pyerrors.h
1658 */
1659void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001660PyErr_SetInterrupt(void)
Barry Warsaw92971171997-01-03 00:14:25 +00001661{
Victor Stinner6c9b35b2011-04-18 16:25:56 +02001662 trip_signal(SIGINT);
Barry Warsaw92971171997-01-03 00:14:25 +00001663}
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001664
1665void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001666PyOS_InitInterrupts(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001667{
Giampaolo Rodola'e09fb712014-04-04 15:34:17 +02001668 PyObject *m = PyImport_ImportModule("_signal");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001669 if (m) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001670 Py_DECREF(m);
1671 }
Guido van Rossum08c16611997-08-02 03:01:42 +00001672}
1673
1674void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001675PyOS_FiniInterrupts(void)
Guido van Rossum08c16611997-08-02 03:01:42 +00001676{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001677 finisignal();
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001678}
1679
1680int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001681PyOS_InterruptOccurred(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001682{
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001683 if (_Py_atomic_load_relaxed(&Handlers[SIGINT].tripped)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001684 if (PyThread_get_thread_ident() != main_thread)
1685 return 0;
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001686 _Py_atomic_store_relaxed(&Handlers[SIGINT].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001687 return 1;
1688 }
1689 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001690}
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001691
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001692static void
1693_clear_pending_signals(void)
1694{
1695 int i;
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001696 if (!_Py_atomic_load(&is_tripped))
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001697 return;
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001698 _Py_atomic_store(&is_tripped, 0);
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001699 for (i = 1; i < NSIG; ++i) {
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001700 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001701 }
1702}
1703
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001704void
Antoine Pitrou346cbd32017-05-27 17:50:54 +02001705_PySignal_AfterFork(void)
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001706{
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001707 /* Clear the signal flags after forking so that they aren't handled
1708 * in both processes if they came in just before the fork() but before
1709 * the interpreter had an opportunity to call the handlers. issue9535. */
1710 _clear_pending_signals();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001711 main_thread = PyThread_get_thread_ident();
1712 main_pid = getpid();
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001713}
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001714
1715int
1716_PyOS_IsMainThread(void)
1717{
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001718 return PyThread_get_thread_ident() == main_thread;
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001719}
1720
1721#ifdef MS_WINDOWS
1722void *_PyOS_SigintEvent(void)
1723{
1724 /* Returns a manual-reset event which gets tripped whenever
1725 SIGINT is received.
1726
1727 Python.h does not include windows.h so we do cannot use HANDLE
1728 as the return type of this function. We use void* instead. */
1729 return sigint_event;
1730}
1731#endif