blob: 9de5c2ed181712b143f15fe580e0036e49f8102e [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"
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007#ifndef MS_WINDOWS
8#include "posixmodule.h"
9#endif
Victor Stinner11517102014-07-29 23:31:34 +020010#ifdef MS_WINDOWS
11#include "socketmodule.h" /* needed for SOCKET_T */
12#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +000013
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000014#ifdef MS_WINDOWS
Antoine Pitrou7faf7052013-03-31 22:48:04 +020015#include <windows.h>
Benjamin Peterson2614cda2010-03-21 22:36:19 +000016#ifdef HAVE_PROCESS_H
Guido van Rossum644a12b1997-04-09 19:24:53 +000017#include <process.h>
18#endif
Benjamin Peterson2614cda2010-03-21 22:36:19 +000019#endif
Guido van Rossum644a12b1997-04-09 19:24:53 +000020
Benjamin Peterson2614cda2010-03-21 22:36:19 +000021#ifdef HAVE_SIGNAL_H
Guido van Rossum398d9fe1994-05-11 08:59:13 +000022#include <signal.h>
Benjamin Peterson2614cda2010-03-21 22:36:19 +000023#endif
24#ifdef HAVE_SYS_STAT_H
Christian Heimes5fb7c2a2007-12-24 08:52:31 +000025#include <sys/stat.h>
Benjamin Peterson2614cda2010-03-21 22:36:19 +000026#endif
Martin v. Löwis3bf3cc02008-03-24 14:05:07 +000027#ifdef HAVE_SYS_TIME_H
Martin v. Löwis823725e2008-03-24 13:39:54 +000028#include <sys/time.h>
Martin v. Löwis3bf3cc02008-03-24 14:05:07 +000029#endif
Christian Heimes5fb7c2a2007-12-24 08:52:31 +000030
Victor Stinnera9293352011-04-30 15:21:58 +020031#if defined(HAVE_PTHREAD_SIGMASK) && !defined(HAVE_BROKEN_PTHREAD_SIGMASK)
32# define PYPTHREAD_SIGMASK
33#endif
34
35#if defined(PYPTHREAD_SIGMASK) && defined(HAVE_PTHREAD_H)
36# include <pthread.h>
37#endif
38
Guido van Rossumbb4ba121994-06-23 11:25:45 +000039#ifndef SIG_ERR
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +000040#define SIG_ERR ((PyOS_sighandler_t)(-1))
Guido van Rossumbb4ba121994-06-23 11:25:45 +000041#endif
42
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000043#ifndef NSIG
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000044# if defined(_NSIG)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000045# define NSIG _NSIG /* For BSD/SysV */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000046# elif defined(_SIGMAX)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000047# define NSIG (_SIGMAX + 1) /* For QNX */
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 djgpp */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000050# else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000051# define NSIG 64 /* Use a reasonable default value */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000052# endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000053#endif
54
Tal Einatc7027b72015-05-16 14:14:49 +030055#include "clinic/signalmodule.c.h"
56
57/*[clinic input]
58module signal
59[clinic start generated code]*/
60/*[clinic end generated code: output=da39a3ee5e6b4b0d input=b0301a3bde5fe9d3]*/
61
Serhiy Storchakad54cfb12018-05-08 07:48:50 +030062/*[python input]
63
64class sigset_t_converter(CConverter):
65 type = 'sigset_t'
66 converter = '_Py_Sigset_Converter'
67
68[python start generated code]*/
69/*[python end generated code: output=da39a3ee5e6b4b0d input=b5689d14466b6823]*/
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000070
Guido van Rossumbb4ba121994-06-23 11:25:45 +000071/*
72 NOTES ON THE INTERACTION BETWEEN SIGNALS AND THREADS
73
74 When threads are supported, we want the following semantics:
75
76 - only the main thread can set a signal handler
77 - any thread can get a signal handler
78 - signals are only delivered to the main thread
79
80 I.e. we don't support "synchronous signals" like SIGFPE (catching
81 this doesn't make much sense in Python anyway) nor do we support
82 signals as a means of inter-thread communication, since not all
83 thread implementations support that (at least our thread library
84 doesn't).
85
86 We still have the problem that in some implementations signals
87 generated by the keyboard (e.g. SIGINT) are delivered to all
88 threads (e.g. SGI), while in others (e.g. Solaris) such signals are
89 delivered to one random thread (an intermediate possibility would
Guido van Rossuma3c04b01995-01-12 11:29:01 +000090 be to deliver it to the main thread -- POSIX?). For now, we have
Guido van Rossumbb4ba121994-06-23 11:25:45 +000091 a working implementation that works in all three cases -- the
92 handler ignores signals if getpid() isn't the same as in the main
93 thread. XXX This is a hack.
Guido van Rossumbb4ba121994-06-23 11:25:45 +000094*/
95
Guido van Rossum295b8e51997-06-06 21:16:41 +000096#include <sys/types.h> /* For pid_t */
Guido van Rossum49b56061998-10-01 20:42:43 +000097#include "pythread.h"
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +020098static unsigned long main_thread;
Guido van Rossumbb4ba121994-06-23 11:25:45 +000099static pid_t main_pid;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000100
Victor Stinner2ec6b172011-05-15 10:21:59 +0200101static volatile struct {
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +0200102 _Py_atomic_int tripped;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000103 PyObject *func;
Barry Warsaw92971171997-01-03 00:14:25 +0000104} Handlers[NSIG];
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000105
Victor Stinner11517102014-07-29 23:31:34 +0200106#ifdef MS_WINDOWS
107#define INVALID_FD ((SOCKET_T)-1)
108
109static volatile struct {
110 SOCKET_T fd;
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800111 int warn_on_full_buffer;
Victor Stinner11517102014-07-29 23:31:34 +0200112 int use_send;
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800113} wakeup = {.fd = INVALID_FD, .warn_on_full_buffer = 1, .use_send = 0};
Victor Stinner11517102014-07-29 23:31:34 +0200114#else
115#define INVALID_FD (-1)
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800116static volatile struct {
117 sig_atomic_t fd;
118 int warn_on_full_buffer;
119} wakeup = {.fd = INVALID_FD, .warn_on_full_buffer = 1};
Victor Stinner11517102014-07-29 23:31:34 +0200120#endif
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000121
Christian Heimesb76922a2007-12-11 01:06:40 +0000122/* Speed up sigcheck() when none tripped */
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +0200123static _Py_atomic_int is_tripped;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000124
Barry Warsaw92971171997-01-03 00:14:25 +0000125static PyObject *DefaultHandler;
126static PyObject *IgnoreHandler;
127static PyObject *IntHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000128
Antoine Pitrou6dd381e2011-11-21 21:26:56 +0100129#ifdef MS_WINDOWS
130static HANDLE sigint_event = NULL;
131#endif
132
Martin v. Löwis823725e2008-03-24 13:39:54 +0000133#ifdef HAVE_GETITIMER
134static PyObject *ItimerError;
135
Victor Stinneref611c92017-10-13 13:49:43 -0700136/* auxiliary functions for setitimer */
137static int
138timeval_from_double(PyObject *obj, struct timeval *tv)
Martin v. Löwis823725e2008-03-24 13:39:54 +0000139{
Victor Stinneref611c92017-10-13 13:49:43 -0700140 if (obj == NULL) {
141 tv->tv_sec = 0;
142 tv->tv_usec = 0;
143 return 0;
Antoine Pitrou729780a2017-06-30 10:01:05 +0200144 }
Victor Stinneref611c92017-10-13 13:49:43 -0700145
146 _PyTime_t t;
147 if (_PyTime_FromSecondsObject(&t, obj, _PyTime_ROUND_CEILING) < 0) {
148 return -1;
149 }
150 return _PyTime_AsTimeval(t, tv, _PyTime_ROUND_CEILING);
Martin v. Löwis823725e2008-03-24 13:39:54 +0000151}
152
Christian Heimes1a8501c2008-10-02 19:56:01 +0000153Py_LOCAL_INLINE(double)
Martin v. Löwis823725e2008-03-24 13:39:54 +0000154double_from_timeval(struct timeval *tv)
155{
156 return tv->tv_sec + (double)(tv->tv_usec / 1000000.0);
157}
158
159static PyObject *
160itimer_retval(struct itimerval *iv)
161{
162 PyObject *r, *v;
163
164 r = PyTuple_New(2);
165 if (r == NULL)
Martin Panter6d57fe12016-09-17 03:26:16 +0000166 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000167
168 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_value)))) {
Martin Panter6d57fe12016-09-17 03:26:16 +0000169 Py_DECREF(r);
170 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000171 }
172
173 PyTuple_SET_ITEM(r, 0, v);
174
175 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_interval)))) {
Martin Panter6d57fe12016-09-17 03:26:16 +0000176 Py_DECREF(r);
177 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000178 }
179
180 PyTuple_SET_ITEM(r, 1, v);
181
182 return r;
183}
184#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000185
Guido van Rossume4485b01994-09-07 14:32:49 +0000186static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000187signal_default_int_handler(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000188{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000189 PyErr_SetNone(PyExc_KeyboardInterrupt);
190 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000191}
192
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000193PyDoc_STRVAR(default_int_handler_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000194"default_int_handler(...)\n\
195\n\
Michael W. Hudson24ec2112004-06-17 15:55:53 +0000196The default handler for SIGINT installed by Python.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000197It raises KeyboardInterrupt.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000198
Thomas Wouters0796b002000-07-22 23:49:30 +0000199
200static int
Victor Stinner11517102014-07-29 23:31:34 +0200201report_wakeup_write_error(void *data)
Antoine Pitrou6f6ec372013-08-17 20:27:56 +0200202{
203 int save_errno = errno;
Benjamin Petersonca470632016-09-06 13:47:26 -0700204 errno = (int) (intptr_t) data;
Antoine Pitrou6f6ec372013-08-17 20:27:56 +0200205 PyErr_SetFromErrno(PyExc_OSError);
206 PySys_WriteStderr("Exception ignored when trying to write to the "
207 "signal wakeup fd:\n");
208 PyErr_WriteUnraisable(NULL);
209 errno = save_errno;
210 return 0;
211}
212
Victor Stinner11517102014-07-29 23:31:34 +0200213#ifdef MS_WINDOWS
214static int
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800215report_wakeup_send_error(void* data)
Victor Stinner11517102014-07-29 23:31:34 +0200216{
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800217 /* PyErr_SetExcFromWindowsErr() invokes FormatMessage() which
218 recognizes the error codes used by both GetLastError() and
219 WSAGetLastError */
220 PyErr_SetExcFromWindowsErr(PyExc_OSError, (int) (intptr_t) data);
Victor Stinner11517102014-07-29 23:31:34 +0200221 PySys_WriteStderr("Exception ignored when trying to send to the "
222 "signal wakeup fd:\n");
223 PyErr_WriteUnraisable(NULL);
Victor Stinner11517102014-07-29 23:31:34 +0200224 return 0;
225}
226#endif /* MS_WINDOWS */
227
Tim Peters4f1b2082000-07-23 21:18:09 +0000228static void
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200229trip_signal(int sig_num)
230{
Victor Stinnerd49b1f12011-05-08 02:03:15 +0200231 unsigned char byte;
Victor Stinner11517102014-07-29 23:31:34 +0200232 int fd;
233 Py_ssize_t rc;
Victor Stinnerc13ef662011-05-25 02:35:58 +0200234
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +0200235 _Py_atomic_store_relaxed(&Handlers[sig_num].tripped, 1);
Victor Stinner11517102014-07-29 23:31:34 +0200236
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200237 /* Set is_tripped after setting .tripped, as it gets
238 cleared in PyErr_CheckSignals() before .tripped. */
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +0200239 _Py_atomic_store(&is_tripped, 1);
240
241 /* Notify ceval.c */
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200242 _PyEval_SignalReceived();
Nathaniel J. Smith4ae01492017-05-16 14:12:11 -0700243
244 /* And then write to the wakeup fd *after* setting all the globals and
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200245 doing the _PyEval_SignalReceived. We used to write to the wakeup fd
246 and then set the flag, but this allowed the following sequence of events
247 (especially on windows, where trip_signal may run in a new thread):
Nathaniel J. Smith4ae01492017-05-16 14:12:11 -0700248
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800249 - main thread blocks on select([wakeup.fd], ...)
Nathaniel J. Smith4ae01492017-05-16 14:12:11 -0700250 - signal arrives
251 - trip_signal writes to the wakeup fd
252 - the main thread wakes up
253 - the main thread checks the signal flags, sees that they're unset
254 - the main thread empties the wakeup fd
255 - the main thread goes back to sleep
256 - trip_signal sets the flags to request the Python-level signal handler
257 be run
258 - the main thread doesn't notice, because it's asleep
259
260 See bpo-30038 for more details.
261 */
262
Victor Stinner11517102014-07-29 23:31:34 +0200263#ifdef MS_WINDOWS
264 fd = Py_SAFE_DOWNCAST(wakeup.fd, SOCKET_T, int);
265#else
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800266 fd = wakeup.fd;
Victor Stinner11517102014-07-29 23:31:34 +0200267#endif
268
269 if (fd != INVALID_FD) {
Victor Stinnerc13ef662011-05-25 02:35:58 +0200270 byte = (unsigned char)sig_num;
Victor Stinner11517102014-07-29 23:31:34 +0200271#ifdef MS_WINDOWS
272 if (wakeup.use_send) {
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800273 rc = send(fd, &byte, 1, 0);
Victor Stinner11517102014-07-29 23:31:34 +0200274
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800275 if (rc < 0) {
276 int last_error = GetLastError();
277 if (wakeup.warn_on_full_buffer ||
278 last_error != WSAEWOULDBLOCK)
279 {
280 /* Py_AddPendingCall() isn't signal-safe, but we
281 still use it for this exceptional case. */
282 Py_AddPendingCall(report_wakeup_send_error,
283 (void *)(intptr_t) last_error);
284 }
Victor Stinner11517102014-07-29 23:31:34 +0200285 }
286 }
287 else
288#endif
289 {
Victor Stinnere72fe392015-04-01 18:35:22 +0200290 /* _Py_write_noraise() retries write() if write() is interrupted by
291 a signal (fails with EINTR). */
292 rc = _Py_write_noraise(fd, &byte, 1);
Victor Stinner11517102014-07-29 23:31:34 +0200293
294 if (rc < 0) {
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800295 if (wakeup.warn_on_full_buffer ||
296 (errno != EWOULDBLOCK && errno != EAGAIN))
297 {
298 /* Py_AddPendingCall() isn't signal-safe, but we
299 still use it for this exceptional case. */
300 Py_AddPendingCall(report_wakeup_write_error,
301 (void *)(intptr_t)errno);
302 }
Victor Stinner11517102014-07-29 23:31:34 +0200303 }
304 }
Victor Stinnerc13ef662011-05-25 02:35:58 +0200305 }
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200306}
307
308static void
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000309signal_handler(int sig_num)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000310{
Antoine Pitrou39a65912010-11-05 19:47:27 +0000311 int save_errno = errno;
312
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000313 /* See NOTES section above */
Antoine Pitrou39a65912010-11-05 19:47:27 +0000314 if (getpid() == main_pid)
Antoine Pitrou39a65912010-11-05 19:47:27 +0000315 {
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200316 trip_signal(sig_num);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000317 }
Antoine Pitrou39a65912010-11-05 19:47:27 +0000318
Jean-Paul Calderone6f137ca2010-05-09 03:18:57 +0000319#ifndef HAVE_SIGACTION
Antoine Pitrou39a65912010-11-05 19:47:27 +0000320#ifdef SIGCHLD
321 /* To avoid infinite recursion, this signal remains
322 reset until explicit re-instated.
323 Don't clear the 'func' field as it is our pointer
324 to the Python handler... */
325 if (sig_num != SIGCHLD)
326#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000327 /* If the handler was not set up with sigaction, reinstall it. See
Nick Coghland6009512014-11-20 21:39:37 +1000328 * Python/pylifecycle.c for the implementation of PyOS_setsig which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000329 * makes this true. See also issue8354. */
330 PyOS_setsig(sig_num, signal_handler);
Jean-Paul Calderone6f137ca2010-05-09 03:18:57 +0000331#endif
Antoine Pitrou39a65912010-11-05 19:47:27 +0000332
333 /* Issue #10311: asynchronously executing signal handlers should not
334 mutate errno under the feet of unsuspecting C code. */
335 errno = save_errno;
Antoine Pitrou6dd381e2011-11-21 21:26:56 +0100336
337#ifdef MS_WINDOWS
338 if (sig_num == SIGINT)
339 SetEvent(sigint_event);
340#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000341}
Guido van Rossume4485b01994-09-07 14:32:49 +0000342
Guido van Rossum06d511d1995-03-10 15:13:48 +0000343
Guido van Rossum1171ee61997-08-22 20:42:00 +0000344#ifdef HAVE_ALARM
Tal Einatc7027b72015-05-16 14:14:49 +0300345
346/*[clinic input]
347signal.alarm -> long
348
349 seconds: int
350 /
351
352Arrange for SIGALRM to arrive after the given number of seconds.
353[clinic start generated code]*/
354
355static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300356signal_alarm_impl(PyObject *module, int seconds)
357/*[clinic end generated code: output=144232290814c298 input=0d5e97e0e6f39e86]*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000358{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000359 /* alarm() returns the number of seconds remaining */
Tal Einatc7027b72015-05-16 14:14:49 +0300360 return (long)alarm(seconds);
Guido van Rossumaa0f4c71994-08-23 13:49:37 +0000361}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000362
Guido van Rossum06d511d1995-03-10 15:13:48 +0000363#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000364
Guido van Rossum1171ee61997-08-22 20:42:00 +0000365#ifdef HAVE_PAUSE
Tal Einatc7027b72015-05-16 14:14:49 +0300366
367/*[clinic input]
368signal.pause
369
370Wait until a signal arrives.
371[clinic start generated code]*/
372
Guido van Rossuma597dde1995-01-10 20:56:29 +0000373static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300374signal_pause_impl(PyObject *module)
375/*[clinic end generated code: output=391656788b3c3929 input=f03de0f875752062]*/
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000376{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000377 Py_BEGIN_ALLOW_THREADS
378 (void)pause();
379 Py_END_ALLOW_THREADS
380 /* make sure that any exceptions that got raised are propagated
381 * back into Python
382 */
383 if (PyErr_CheckSignals())
384 return NULL;
Barry Warsaw92971171997-01-03 00:14:25 +0000385
Tal Einatc7027b72015-05-16 14:14:49 +0300386 Py_RETURN_NONE;
Guido van Rossume4485b01994-09-07 14:32:49 +0000387}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000388
Guido van Rossum06d511d1995-03-10 15:13:48 +0000389#endif
Guido van Rossume4485b01994-09-07 14:32:49 +0000390
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000391
Tal Einatc7027b72015-05-16 14:14:49 +0300392/*[clinic input]
393signal.signal
394
395 signalnum: int
396 handler: object
397 /
398
399Set the action for the given signal.
400
401The action can be SIG_DFL, SIG_IGN, or a callable Python object.
402The previous action is returned. See getsignal() for possible return values.
403
404*** IMPORTANT NOTICE ***
405A signal handler function is called with two arguments:
406the first is the signal number, the second is the interrupted stack frame.
407[clinic start generated code]*/
408
Guido van Rossume4485b01994-09-07 14:32:49 +0000409static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300410signal_signal_impl(PyObject *module, int signalnum, PyObject *handler)
411/*[clinic end generated code: output=b44cfda43780f3a1 input=deee84af5fa0432c]*/
Guido van Rossume4485b01994-09-07 14:32:49 +0000412{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000413 PyObject *old_handler;
414 void (*func)(int);
Brian Curtinef9efbd2010-08-06 19:27:32 +0000415#ifdef MS_WINDOWS
Tal Einatc7027b72015-05-16 14:14:49 +0300416 /* Validate that signalnum is one of the allowable signals */
417 switch (signalnum) {
Brian Curtinc734b312010-09-06 16:04:10 +0000418 case SIGABRT: break;
Brian Curtin9e88b5a2010-10-01 14:49:24 +0000419#ifdef SIGBREAK
420 /* Issue #10003: SIGBREAK is not documented as permitted, but works
421 and corresponds to CTRL_BREAK_EVENT. */
422 case SIGBREAK: break;
423#endif
Brian Curtinc734b312010-09-06 16:04:10 +0000424 case SIGFPE: break;
425 case SIGILL: break;
426 case SIGINT: break;
427 case SIGSEGV: break;
428 case SIGTERM: break;
429 default:
430 PyErr_SetString(PyExc_ValueError, "invalid signal value");
431 return NULL;
Brian Curtinef9efbd2010-08-06 19:27:32 +0000432 }
433#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000434 if (PyThread_get_thread_ident() != main_thread) {
435 PyErr_SetString(PyExc_ValueError,
436 "signal only works in main thread");
437 return NULL;
438 }
Tal Einatc7027b72015-05-16 14:14:49 +0300439 if (signalnum < 1 || signalnum >= NSIG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000440 PyErr_SetString(PyExc_ValueError,
441 "signal number out of range");
442 return NULL;
443 }
Tal Einatc7027b72015-05-16 14:14:49 +0300444 if (handler == IgnoreHandler)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000445 func = SIG_IGN;
Tal Einatc7027b72015-05-16 14:14:49 +0300446 else if (handler == DefaultHandler)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000447 func = SIG_DFL;
Tal Einatc7027b72015-05-16 14:14:49 +0300448 else if (!PyCallable_Check(handler)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000449 PyErr_SetString(PyExc_TypeError,
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000450"signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000451 return NULL;
452 }
453 else
454 func = signal_handler;
Antoine Pitrouf6f90ff2017-11-03 19:58:46 +0100455 /* Check for pending signals before changing signal handler */
456 if (PyErr_CheckSignals()) {
457 return NULL;
458 }
Tal Einatc7027b72015-05-16 14:14:49 +0300459 if (PyOS_setsig(signalnum, func) == SIG_ERR) {
Victor Stinner388196e2011-05-10 17:13:00 +0200460 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000461 return NULL;
462 }
Tal Einatc7027b72015-05-16 14:14:49 +0300463 old_handler = Handlers[signalnum].func;
Tal Einatc7027b72015-05-16 14:14:49 +0300464 Py_INCREF(handler);
465 Handlers[signalnum].func = handler;
Antoine Pitrouc8c952c2013-05-04 23:16:59 +0200466 if (old_handler != NULL)
467 return old_handler;
468 else
469 Py_RETURN_NONE;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000470}
471
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000472
Tal Einatc7027b72015-05-16 14:14:49 +0300473/*[clinic input]
474signal.getsignal
475
476 signalnum: int
477 /
478
479Return the current action for the given signal.
480
481The return value can be:
482 SIG_IGN -- if the signal is being ignored
483 SIG_DFL -- if the default action for the signal is in effect
484 None -- if an unknown handler is in effect
485 anything else -- the callable Python object used as a handler
486[clinic start generated code]*/
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000487
Guido van Rossume4485b01994-09-07 14:32:49 +0000488static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300489signal_getsignal_impl(PyObject *module, int signalnum)
490/*[clinic end generated code: output=35b3e0e796fd555e input=ac23a00f19dfa509]*/
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000491{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000492 PyObject *old_handler;
Tal Einatc7027b72015-05-16 14:14:49 +0300493 if (signalnum < 1 || signalnum >= NSIG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000494 PyErr_SetString(PyExc_ValueError,
495 "signal number out of range");
496 return NULL;
497 }
Tal Einatc7027b72015-05-16 14:14:49 +0300498 old_handler = Handlers[signalnum].func;
Antoine Pitrouc8c952c2013-05-04 23:16:59 +0200499 if (old_handler != NULL) {
500 Py_INCREF(old_handler);
501 return old_handler;
502 }
503 else {
504 Py_RETURN_NONE;
505 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000506}
507
Antoine Pietri5d2a27d2018-03-12 14:42:34 +0100508
509/*[clinic input]
510signal.strsignal
511
512 signalnum: int
513 /
514
515Return the system description of the given signal.
516
517The return values can be such as "Interrupt", "Segmentation fault", etc.
518Returns None if the signal is not recognized.
519[clinic start generated code]*/
520
521static PyObject *
522signal_strsignal_impl(PyObject *module, int signalnum)
523/*[clinic end generated code: output=44e12e1e3b666261 input=b77914b03f856c74]*/
524{
525 char *res;
526
527 if (signalnum < 1 || signalnum >= NSIG) {
528 PyErr_SetString(PyExc_ValueError,
529 "signal number out of range");
530 return NULL;
531 }
532
533#ifdef MS_WINDOWS
534 /* Custom redefinition of POSIX signals allowed on Windows */
535 switch (signalnum) {
536 case SIGINT:
537 res = "Interrupt";
538 break;
539 case SIGILL:
540 res = "Illegal instruction";
541 break;
542 case SIGABRT:
543 res = "Aborted";
544 break;
545 case SIGFPE:
546 res = "Floating point exception";
547 break;
548 case SIGSEGV:
549 res = "Segmentation fault";
550 break;
551 case SIGTERM:
552 res = "Terminated";
553 break;
554 default:
555 Py_RETURN_NONE;
556 }
557#else
558 errno = 0;
559 res = strsignal(signalnum);
560
561 if (errno || res == NULL || strstr(res, "Unknown signal") != NULL)
562 Py_RETURN_NONE;
563#endif
564
565 return Py_BuildValue("s", res);
566}
567
Christian Heimes8640e742008-02-23 16:23:06 +0000568#ifdef HAVE_SIGINTERRUPT
Tal Einatc7027b72015-05-16 14:14:49 +0300569
570/*[clinic input]
571signal.siginterrupt
572
573 signalnum: int
574 flag: int
575 /
576
577Change system call restart behaviour.
578
579If flag is False, system calls will be restarted when interrupted by
580signal sig, else system calls will be interrupted.
581[clinic start generated code]*/
Christian Heimes8640e742008-02-23 16:23:06 +0000582
583static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300584signal_siginterrupt_impl(PyObject *module, int signalnum, int flag)
585/*[clinic end generated code: output=063816243d85dd19 input=4160acacca3e2099]*/
Christian Heimes8640e742008-02-23 16:23:06 +0000586{
Tal Einatc7027b72015-05-16 14:14:49 +0300587 if (signalnum < 1 || signalnum >= NSIG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000588 PyErr_SetString(PyExc_ValueError,
589 "signal number out of range");
590 return NULL;
591 }
Tal Einatc7027b72015-05-16 14:14:49 +0300592 if (siginterrupt(signalnum, flag)<0) {
Victor Stinner388196e2011-05-10 17:13:00 +0200593 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000594 return NULL;
595 }
Tal Einatc7027b72015-05-16 14:14:49 +0300596 Py_RETURN_NONE;
Christian Heimes8640e742008-02-23 16:23:06 +0000597}
598
599#endif
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000600
Tal Einatc7027b72015-05-16 14:14:49 +0300601
602static PyObject*
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800603signal_set_wakeup_fd(PyObject *self, PyObject *args, PyObject *kwds)
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000604{
Victor Stinnere134a7f2015-03-30 10:09:31 +0200605 struct _Py_stat_struct status;
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800606 static char *kwlist[] = {
607 "", "warn_on_full_buffer", NULL,
608 };
609 int warn_on_full_buffer = 1;
Victor Stinner11517102014-07-29 23:31:34 +0200610#ifdef MS_WINDOWS
611 PyObject *fdobj;
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100612 SOCKET_T sockfd, old_sockfd;
Victor Stinner11517102014-07-29 23:31:34 +0200613 int res;
614 int res_size = sizeof res;
615 PyObject *mod;
Victor Stinner11517102014-07-29 23:31:34 +0200616 int is_socket;
617
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800618 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|$p:set_wakeup_fd", kwlist,
619 &fdobj, &warn_on_full_buffer))
Victor Stinner11517102014-07-29 23:31:34 +0200620 return NULL;
621
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100622 sockfd = PyLong_AsSocket_t(fdobj);
623 if (sockfd == (SOCKET_T)(-1) && PyErr_Occurred())
Victor Stinner11517102014-07-29 23:31:34 +0200624 return NULL;
625#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000626 int fd, old_fd;
Victor Stinner11517102014-07-29 23:31:34 +0200627
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800628 if (!PyArg_ParseTupleAndKeywords(args, kwds, "i|$p:set_wakeup_fd", kwlist,
629 &fd, &warn_on_full_buffer))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000630 return NULL;
Victor Stinner11517102014-07-29 23:31:34 +0200631#endif
632
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000633 if (PyThread_get_thread_ident() != main_thread) {
634 PyErr_SetString(PyExc_ValueError,
635 "set_wakeup_fd only works in main thread");
636 return NULL;
637 }
Victor Stinner0bffc942014-07-21 16:28:54 +0200638
Victor Stinner11517102014-07-29 23:31:34 +0200639#ifdef MS_WINDOWS
640 is_socket = 0;
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100641 if (sockfd != INVALID_FD) {
Victor Stinner11517102014-07-29 23:31:34 +0200642 /* Import the _socket module to call WSAStartup() */
643 mod = PyImport_ImportModuleNoBlock("_socket");
644 if (mod == NULL)
645 return NULL;
646 Py_DECREF(mod);
647
648 /* test the socket */
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100649 if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR,
Victor Stinner11517102014-07-29 23:31:34 +0200650 (char *)&res, &res_size) != 0) {
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100651 int fd, err;
652
653 err = WSAGetLastError();
Victor Stinner11517102014-07-29 23:31:34 +0200654 if (err != WSAENOTSOCK) {
655 PyErr_SetExcFromWindowsErr(PyExc_OSError, err);
656 return NULL;
657 }
658
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100659 fd = (int)sockfd;
Steve Dower940f33a2016-09-08 11:21:54 -0700660 if ((SOCKET_T)fd != sockfd) {
Victor Stinner11517102014-07-29 23:31:34 +0200661 PyErr_SetString(PyExc_ValueError, "invalid fd");
662 return NULL;
663 }
664
Victor Stinnere134a7f2015-03-30 10:09:31 +0200665 if (_Py_fstat(fd, &status) != 0)
Victor Stinner11517102014-07-29 23:31:34 +0200666 return NULL;
Victor Stinner38227602014-08-27 12:59:44 +0200667
668 /* on Windows, a file cannot be set to non-blocking mode */
Victor Stinner11517102014-07-29 23:31:34 +0200669 }
Victor Stinner38227602014-08-27 12:59:44 +0200670 else {
Victor Stinner11517102014-07-29 23:31:34 +0200671 is_socket = 1;
Victor Stinner38227602014-08-27 12:59:44 +0200672
673 /* Windows does not provide a function to test if a socket
674 is in non-blocking mode */
675 }
Victor Stinner11517102014-07-29 23:31:34 +0200676 }
677
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100678 old_sockfd = wakeup.fd;
679 wakeup.fd = sockfd;
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800680 wakeup.warn_on_full_buffer = warn_on_full_buffer;
Victor Stinner11517102014-07-29 23:31:34 +0200681 wakeup.use_send = is_socket;
682
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100683 if (old_sockfd != INVALID_FD)
684 return PyLong_FromSocket_t(old_sockfd);
Victor Stinner11517102014-07-29 23:31:34 +0200685 else
686 return PyLong_FromLong(-1);
687#else
Victor Stinnerd18ccd12014-07-24 21:58:53 +0200688 if (fd != -1) {
Victor Stinner38227602014-08-27 12:59:44 +0200689 int blocking;
690
Victor Stinnere134a7f2015-03-30 10:09:31 +0200691 if (_Py_fstat(fd, &status) != 0)
Victor Stinner11517102014-07-29 23:31:34 +0200692 return NULL;
Victor Stinner38227602014-08-27 12:59:44 +0200693
694 blocking = _Py_get_blocking(fd);
695 if (blocking < 0)
696 return NULL;
697 if (blocking) {
698 PyErr_Format(PyExc_ValueError,
699 "the fd %i must be in non-blocking mode",
700 fd);
701 return NULL;
702 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000703 }
Victor Stinner0bffc942014-07-21 16:28:54 +0200704
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800705 old_fd = wakeup.fd;
706 wakeup.fd = fd;
707 wakeup.warn_on_full_buffer = warn_on_full_buffer;
Victor Stinner0bffc942014-07-21 16:28:54 +0200708
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000709 return PyLong_FromLong(old_fd);
Victor Stinner11517102014-07-29 23:31:34 +0200710#endif
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000711}
712
713PyDoc_STRVAR(set_wakeup_fd_doc,
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800714"set_wakeup_fd(fd, *, warn_on_full_buffer=True) -> fd\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000715\n\
Victor Stinner11517102014-07-29 23:31:34 +0200716Sets the fd to be written to (with the signal number) when a signal\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000717comes in. A library can use this to wakeup select or poll.\n\
Victor Stinner11517102014-07-29 23:31:34 +0200718The previous fd or -1 is returned.\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000719\n\
720The fd must be non-blocking.");
721
722/* C API for the same, without all the error checking */
723int
724PySignal_SetWakeupFd(int fd)
725{
Victor Stinner11517102014-07-29 23:31:34 +0200726 int old_fd;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000727 if (fd < 0)
728 fd = -1;
Victor Stinner11517102014-07-29 23:31:34 +0200729
730#ifdef MS_WINDOWS
731 old_fd = Py_SAFE_DOWNCAST(wakeup.fd, SOCKET_T, int);
Victor Stinner11517102014-07-29 23:31:34 +0200732#else
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800733 old_fd = wakeup.fd;
Victor Stinner11517102014-07-29 23:31:34 +0200734#endif
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800735 wakeup.fd = fd;
736 wakeup.warn_on_full_buffer = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000737 return old_fd;
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000738}
739
740
Martin v. Löwis823725e2008-03-24 13:39:54 +0000741#ifdef HAVE_SETITIMER
Tal Einatc7027b72015-05-16 14:14:49 +0300742
743/*[clinic input]
744signal.setitimer
745
746 which: int
Victor Stinneref611c92017-10-13 13:49:43 -0700747 seconds: object
748 interval: object(c_default="NULL") = 0.0
Tal Einatc7027b72015-05-16 14:14:49 +0300749 /
750
751Sets given itimer (one of ITIMER_REAL, ITIMER_VIRTUAL or ITIMER_PROF).
752
753The timer will fire after value seconds and after that every interval seconds.
754The itimer can be cleared by setting seconds to zero.
755
756Returns old values as a tuple: (delay, interval).
757[clinic start generated code]*/
758
Martin v. Löwis823725e2008-03-24 13:39:54 +0000759static PyObject *
Victor Stinneref611c92017-10-13 13:49:43 -0700760signal_setitimer_impl(PyObject *module, int which, PyObject *seconds,
761 PyObject *interval)
762/*[clinic end generated code: output=65f9dcbddc35527b input=de43daf194e6f66f]*/
Martin v. Löwis823725e2008-03-24 13:39:54 +0000763{
Martin v. Löwis823725e2008-03-24 13:39:54 +0000764 struct itimerval new, old;
765
Victor Stinneref611c92017-10-13 13:49:43 -0700766 if (timeval_from_double(seconds, &new.it_value) < 0) {
767 return NULL;
768 }
769 if (timeval_from_double(interval, &new.it_interval) < 0) {
770 return NULL;
771 }
772
Martin v. Löwis823725e2008-03-24 13:39:54 +0000773 /* Let OS check "which" value */
774 if (setitimer(which, &new, &old) != 0) {
Tal Einatc7027b72015-05-16 14:14:49 +0300775 PyErr_SetFromErrno(ItimerError);
776 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000777 }
778
779 return itimer_retval(&old);
780}
781
Martin v. Löwis823725e2008-03-24 13:39:54 +0000782#endif
783
784
785#ifdef HAVE_GETITIMER
Tal Einatc7027b72015-05-16 14:14:49 +0300786
787/*[clinic input]
788signal.getitimer
789
790 which: int
791 /
792
793Returns current value of given itimer.
794[clinic start generated code]*/
795
Martin v. Löwis823725e2008-03-24 13:39:54 +0000796static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300797signal_getitimer_impl(PyObject *module, int which)
798/*[clinic end generated code: output=9e053175d517db40 input=f7d21d38f3490627]*/
Martin v. Löwis823725e2008-03-24 13:39:54 +0000799{
Martin v. Löwis823725e2008-03-24 13:39:54 +0000800 struct itimerval old;
801
Martin v. Löwis823725e2008-03-24 13:39:54 +0000802 if (getitimer(which, &old) != 0) {
Tal Einatc7027b72015-05-16 14:14:49 +0300803 PyErr_SetFromErrno(ItimerError);
804 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000805 }
806
807 return itimer_retval(&old);
808}
809
Martin v. Löwis823725e2008-03-24 13:39:54 +0000810#endif
811
Victor Stinnerb3e72192011-05-08 01:46:11 +0200812#if defined(PYPTHREAD_SIGMASK) || defined(HAVE_SIGPENDING)
Victor Stinner35b300c2011-05-04 13:20:35 +0200813static PyObject*
814sigset_to_set(sigset_t mask)
815{
816 PyObject *signum, *result;
817 int sig;
818
819 result = PySet_New(0);
820 if (result == NULL)
821 return NULL;
822
823 for (sig = 1; sig < NSIG; sig++) {
824 if (sigismember(&mask, sig) != 1)
825 continue;
826
827 /* Handle the case where it is a member by adding the signal to
828 the result list. Ignore the other cases because they mean the
829 signal isn't a member of the mask or the signal was invalid,
830 and an invalid signal must have been our fault in constructing
831 the loop boundaries. */
832 signum = PyLong_FromLong(sig);
833 if (signum == NULL) {
834 Py_DECREF(result);
835 return NULL;
836 }
837 if (PySet_Add(result, signum) == -1) {
838 Py_DECREF(signum);
839 Py_DECREF(result);
840 return NULL;
841 }
842 Py_DECREF(signum);
843 }
844 return result;
845}
Victor Stinnerb3e72192011-05-08 01:46:11 +0200846#endif
Victor Stinner35b300c2011-05-04 13:20:35 +0200847
Victor Stinnerb3e72192011-05-08 01:46:11 +0200848#ifdef PYPTHREAD_SIGMASK
Tal Einatc7027b72015-05-16 14:14:49 +0300849
850/*[clinic input]
851signal.pthread_sigmask
852
853 how: int
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300854 mask: sigset_t
Tal Einatc7027b72015-05-16 14:14:49 +0300855 /
856
857Fetch and/or change the signal mask of the calling thread.
858[clinic start generated code]*/
859
Victor Stinnera9293352011-04-30 15:21:58 +0200860static PyObject *
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300861signal_pthread_sigmask_impl(PyObject *module, int how, sigset_t mask)
862/*[clinic end generated code: output=0562c0fb192981a8 input=85bcebda442fa77f]*/
Victor Stinnera9293352011-04-30 15:21:58 +0200863{
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300864 sigset_t previous;
Victor Stinnera9293352011-04-30 15:21:58 +0200865 int err;
866
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300867 err = pthread_sigmask(how, &mask, &previous);
Victor Stinnera9293352011-04-30 15:21:58 +0200868 if (err != 0) {
869 errno = err;
Victor Stinnerb3e72192011-05-08 01:46:11 +0200870 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnera9293352011-04-30 15:21:58 +0200871 return NULL;
872 }
873
Victor Stinnerd0e516d2011-05-03 14:57:12 +0200874 /* if signals was unblocked, signal handlers have been called */
875 if (PyErr_CheckSignals())
876 return NULL;
877
Victor Stinner35b300c2011-05-04 13:20:35 +0200878 return sigset_to_set(previous);
Victor Stinnera9293352011-04-30 15:21:58 +0200879}
880
Victor Stinnera9293352011-04-30 15:21:58 +0200881#endif /* #ifdef PYPTHREAD_SIGMASK */
882
Martin v. Löwis823725e2008-03-24 13:39:54 +0000883
Victor Stinnerb3e72192011-05-08 01:46:11 +0200884#ifdef HAVE_SIGPENDING
Tal Einatc7027b72015-05-16 14:14:49 +0300885
886/*[clinic input]
887signal.sigpending
888
889Examine pending signals.
890
891Returns a set of signal numbers that are pending for delivery to
892the calling thread.
893[clinic start generated code]*/
894
Victor Stinnerb3e72192011-05-08 01:46:11 +0200895static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300896signal_sigpending_impl(PyObject *module)
897/*[clinic end generated code: output=53375ffe89325022 input=e0036c016f874e29]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +0200898{
899 int err;
900 sigset_t mask;
901 err = sigpending(&mask);
902 if (err)
903 return PyErr_SetFromErrno(PyExc_OSError);
904 return sigset_to_set(mask);
905}
906
Victor Stinnerb3e72192011-05-08 01:46:11 +0200907#endif /* #ifdef HAVE_SIGPENDING */
908
909
910#ifdef HAVE_SIGWAIT
Tal Einatc7027b72015-05-16 14:14:49 +0300911
912/*[clinic input]
913signal.sigwait
914
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300915 sigset: sigset_t
Tal Einatc7027b72015-05-16 14:14:49 +0300916 /
917
918Wait for a signal.
919
920Suspend execution of the calling thread until the delivery of one of the
921signals specified in the signal set sigset. The function accepts the signal
922and returns the signal number.
923[clinic start generated code]*/
924
Victor Stinnerb3e72192011-05-08 01:46:11 +0200925static PyObject *
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300926signal_sigwait_impl(PyObject *module, sigset_t sigset)
927/*[clinic end generated code: output=f43770699d682f96 input=a6fbd47b1086d119]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +0200928{
Victor Stinnerb3e72192011-05-08 01:46:11 +0200929 int err, signum;
930
Victor Stinner10c30d62011-06-10 01:39:53 +0200931 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300932 err = sigwait(&sigset, &signum);
Victor Stinner10c30d62011-06-10 01:39:53 +0200933 Py_END_ALLOW_THREADS
Victor Stinnerb3e72192011-05-08 01:46:11 +0200934 if (err) {
935 errno = err;
936 return PyErr_SetFromErrno(PyExc_OSError);
937 }
938
939 return PyLong_FromLong(signum);
940}
941
Tal Einatc7027b72015-05-16 14:14:49 +0300942#endif /* #ifdef HAVE_SIGWAIT */
943
Victor Stinnerb3e72192011-05-08 01:46:11 +0200944
Antoine Pitrou9d3627e2018-05-04 13:00:50 +0200945#if defined(HAVE_SIGFILLSET) || defined(MS_WINDOWS)
946
947/*[clinic input]
948signal.valid_signals
949
950Return a set of valid signal numbers on this platform.
951
952The signal numbers returned by this function can be safely passed to
953functions like `pthread_sigmask`.
954[clinic start generated code]*/
955
956static PyObject *
957signal_valid_signals_impl(PyObject *module)
958/*[clinic end generated code: output=1609cffbcfcf1314 input=86a3717ff25288f2]*/
959{
960#ifdef MS_WINDOWS
961#ifdef SIGBREAK
962 PyObject *tup = Py_BuildValue("(iiiiiii)", SIGABRT, SIGBREAK, SIGFPE,
963 SIGILL, SIGINT, SIGSEGV, SIGTERM);
964#else
965 PyObject *tup = Py_BuildValue("(iiiiii)", SIGABRT, SIGFPE, SIGILL,
966 SIGINT, SIGSEGV, SIGTERM);
967#endif
968 if (tup == NULL) {
969 return NULL;
970 }
971 PyObject *set = PySet_New(tup);
972 Py_DECREF(tup);
973 return set;
974#else
975 sigset_t mask;
976 if (sigemptyset(&mask) || sigfillset(&mask)) {
977 return PyErr_SetFromErrno(PyExc_OSError);
978 }
979 return sigset_to_set(mask);
980#endif
981}
982
983#endif /* #if defined(HAVE_SIGFILLSET) || defined(MS_WINDOWS) */
984
985
Ross Lagerwallbc808222011-06-25 12:13:40 +0200986#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
987static int initialized;
988static PyStructSequence_Field struct_siginfo_fields[] = {
989 {"si_signo", "signal number"},
990 {"si_code", "signal code"},
991 {"si_errno", "errno associated with this signal"},
992 {"si_pid", "sending process ID"},
993 {"si_uid", "real user ID of sending process"},
994 {"si_status", "exit value or signal"},
995 {"si_band", "band event for SIGPOLL"},
996 {0}
997};
998
999PyDoc_STRVAR(struct_siginfo__doc__,
1000"struct_siginfo: Result from sigwaitinfo or sigtimedwait.\n\n\
1001This object may be accessed either as a tuple of\n\
1002(si_signo, si_code, si_errno, si_pid, si_uid, si_status, si_band),\n\
1003or via the attributes si_signo, si_code, and so on.");
1004
1005static PyStructSequence_Desc struct_siginfo_desc = {
1006 "signal.struct_siginfo", /* name */
1007 struct_siginfo__doc__, /* doc */
1008 struct_siginfo_fields, /* fields */
1009 7 /* n_in_sequence */
1010};
1011
1012static PyTypeObject SiginfoType;
1013
1014static PyObject *
1015fill_siginfo(siginfo_t *si)
1016{
1017 PyObject *result = PyStructSequence_New(&SiginfoType);
1018 if (!result)
1019 return NULL;
1020
1021 PyStructSequence_SET_ITEM(result, 0, PyLong_FromLong((long)(si->si_signo)));
1022 PyStructSequence_SET_ITEM(result, 1, PyLong_FromLong((long)(si->si_code)));
1023 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si->si_errno)));
1024 PyStructSequence_SET_ITEM(result, 3, PyLong_FromPid(si->si_pid));
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02001025 PyStructSequence_SET_ITEM(result, 4, _PyLong_FromUid(si->si_uid));
Ross Lagerwallbc808222011-06-25 12:13:40 +02001026 PyStructSequence_SET_ITEM(result, 5,
1027 PyLong_FromLong((long)(si->si_status)));
Zachary Ware6a6967e2016-10-01 00:47:27 -05001028#ifdef HAVE_SIGINFO_T_SI_BAND
Ross Lagerwallbc808222011-06-25 12:13:40 +02001029 PyStructSequence_SET_ITEM(result, 6, PyLong_FromLong(si->si_band));
Zachary Ware6a6967e2016-10-01 00:47:27 -05001030#else
1031 PyStructSequence_SET_ITEM(result, 6, PyLong_FromLong(0L));
1032#endif
Ross Lagerwallbc808222011-06-25 12:13:40 +02001033 if (PyErr_Occurred()) {
1034 Py_DECREF(result);
1035 return NULL;
1036 }
1037
1038 return result;
1039}
1040#endif
1041
1042#ifdef HAVE_SIGWAITINFO
Tal Einatc7027b72015-05-16 14:14:49 +03001043
1044/*[clinic input]
1045signal.sigwaitinfo
1046
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001047 sigset: sigset_t
Tal Einatc7027b72015-05-16 14:14:49 +03001048 /
1049
1050Wait synchronously until one of the signals in *sigset* is delivered.
1051
1052Returns a struct_siginfo containing information about the signal.
1053[clinic start generated code]*/
1054
Ross Lagerwallbc808222011-06-25 12:13:40 +02001055static PyObject *
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001056signal_sigwaitinfo_impl(PyObject *module, sigset_t sigset)
1057/*[clinic end generated code: output=1eb2f1fa236fdbca input=3d1a7e1f27fc664c]*/
Ross Lagerwallbc808222011-06-25 12:13:40 +02001058{
Ross Lagerwallbc808222011-06-25 12:13:40 +02001059 siginfo_t si;
1060 int err;
Victor Stinnera453cd82015-03-20 12:54:28 +01001061 int async_err = 0;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001062
Victor Stinnera453cd82015-03-20 12:54:28 +01001063 do {
1064 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001065 err = sigwaitinfo(&sigset, &si);
Victor Stinnera453cd82015-03-20 12:54:28 +01001066 Py_END_ALLOW_THREADS
1067 } while (err == -1
1068 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Ross Lagerwallbc808222011-06-25 12:13:40 +02001069 if (err == -1)
Victor Stinnera453cd82015-03-20 12:54:28 +01001070 return (!async_err) ? PyErr_SetFromErrno(PyExc_OSError) : NULL;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001071
1072 return fill_siginfo(&si);
1073}
1074
Ross Lagerwallbc808222011-06-25 12:13:40 +02001075#endif /* #ifdef HAVE_SIGWAITINFO */
1076
1077#ifdef HAVE_SIGTIMEDWAIT
Tal Einatc7027b72015-05-16 14:14:49 +03001078
1079/*[clinic input]
1080signal.sigtimedwait
1081
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001082 sigset: sigset_t
Serhiy Storchaka6b680cd2015-05-16 15:57:56 +03001083 timeout as timeout_obj: object
Tal Einatc7027b72015-05-16 14:14:49 +03001084 /
1085
1086Like sigwaitinfo(), but with a timeout.
1087
1088The timeout is specified in seconds, with floating point numbers allowed.
1089[clinic start generated code]*/
1090
Ross Lagerwallbc808222011-06-25 12:13:40 +02001091static PyObject *
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001092signal_sigtimedwait_impl(PyObject *module, sigset_t sigset,
Serhiy Storchaka6b680cd2015-05-16 15:57:56 +03001093 PyObject *timeout_obj)
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001094/*[clinic end generated code: output=59c8971e8ae18a64 input=87fd39237cf0b7ba]*/
Ross Lagerwallbc808222011-06-25 12:13:40 +02001095{
Victor Stinnera453cd82015-03-20 12:54:28 +01001096 struct timespec ts;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001097 siginfo_t si;
Victor Stinnera453cd82015-03-20 12:54:28 +01001098 int res;
Victor Stinner34dc0f42015-03-27 18:19:03 +01001099 _PyTime_t timeout, deadline, monotonic;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001100
Victor Stinner869e1772015-03-30 03:49:14 +02001101 if (_PyTime_FromSecondsObject(&timeout,
1102 timeout_obj, _PyTime_ROUND_CEILING) < 0)
Ross Lagerwallbc808222011-06-25 12:13:40 +02001103 return NULL;
1104
Victor Stinnera453cd82015-03-20 12:54:28 +01001105 if (timeout < 0) {
Ross Lagerwallbc808222011-06-25 12:13:40 +02001106 PyErr_SetString(PyExc_ValueError, "timeout must be non-negative");
1107 return NULL;
1108 }
1109
Victor Stinner34dc0f42015-03-27 18:19:03 +01001110 deadline = _PyTime_GetMonotonicClock() + timeout;
Victor Stinnera453cd82015-03-20 12:54:28 +01001111
1112 do {
Victor Stinner34dc0f42015-03-27 18:19:03 +01001113 if (_PyTime_AsTimespec(timeout, &ts) < 0)
1114 return NULL;
Victor Stinnera453cd82015-03-20 12:54:28 +01001115
1116 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001117 res = sigtimedwait(&sigset, &si, &ts);
Victor Stinnera453cd82015-03-20 12:54:28 +01001118 Py_END_ALLOW_THREADS
1119
1120 if (res != -1)
1121 break;
1122
1123 if (errno != EINTR) {
1124 if (errno == EAGAIN)
1125 Py_RETURN_NONE;
1126 else
1127 return PyErr_SetFromErrno(PyExc_OSError);
1128 }
1129
1130 /* sigtimedwait() was interrupted by a signal (EINTR) */
1131 if (PyErr_CheckSignals())
1132 return NULL;
1133
Victor Stinner34dc0f42015-03-27 18:19:03 +01001134 monotonic = _PyTime_GetMonotonicClock();
1135 timeout = deadline - monotonic;
Victor Stinner6aa446c2015-03-30 21:33:51 +02001136 if (timeout < 0)
Victor Stinnera453cd82015-03-20 12:54:28 +01001137 break;
1138 } while (1);
Ross Lagerwallbc808222011-06-25 12:13:40 +02001139
1140 return fill_siginfo(&si);
1141}
1142
Ross Lagerwallbc808222011-06-25 12:13:40 +02001143#endif /* #ifdef HAVE_SIGTIMEDWAIT */
1144
Victor Stinnerb3e72192011-05-08 01:46:11 +02001145
Antoine Pitroua6a4dc82017-09-07 18:56:24 +02001146#if defined(HAVE_PTHREAD_KILL)
Tal Einatc7027b72015-05-16 14:14:49 +03001147
1148/*[clinic input]
1149signal.pthread_kill
1150
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +02001151 thread_id: unsigned_long(bitwise=True)
Tal Einatc7027b72015-05-16 14:14:49 +03001152 signalnum: int
1153 /
1154
1155Send a signal to a thread.
1156[clinic start generated code]*/
1157
Victor Stinnerb3e72192011-05-08 01:46:11 +02001158static PyObject *
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +02001159signal_pthread_kill_impl(PyObject *module, unsigned long thread_id,
1160 int signalnum)
1161/*[clinic end generated code: output=7629919b791bc27f input=1d901f2c7bb544ff]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +02001162{
Victor Stinnerb3e72192011-05-08 01:46:11 +02001163 int err;
1164
Tal Einatc7027b72015-05-16 14:14:49 +03001165 err = pthread_kill((pthread_t)thread_id, signalnum);
Victor Stinnerb3e72192011-05-08 01:46:11 +02001166 if (err != 0) {
1167 errno = err;
1168 PyErr_SetFromErrno(PyExc_OSError);
1169 return NULL;
1170 }
1171
1172 /* the signal may have been send to the current thread */
1173 if (PyErr_CheckSignals())
1174 return NULL;
1175
1176 Py_RETURN_NONE;
1177}
1178
Antoine Pitroua6a4dc82017-09-07 18:56:24 +02001179#endif /* #if defined(HAVE_PTHREAD_KILL) */
Victor Stinnerb3e72192011-05-08 01:46:11 +02001180
1181
1182
Tal Einatc7027b72015-05-16 14:14:49 +03001183/* List of functions defined in the module -- some of the methoddefs are
1184 defined to nothing if the corresponding C function is not available. */
Barry Warsaw92971171997-01-03 00:14:25 +00001185static PyMethodDef signal_methods[] = {
Tal Einatc7027b72015-05-16 14:14:49 +03001186 {"default_int_handler", signal_default_int_handler, METH_VARARGS, default_int_handler_doc},
1187 SIGNAL_ALARM_METHODDEF
1188 SIGNAL_SETITIMER_METHODDEF
1189 SIGNAL_GETITIMER_METHODDEF
1190 SIGNAL_SIGNAL_METHODDEF
Antoine Pietri5d2a27d2018-03-12 14:42:34 +01001191 SIGNAL_STRSIGNAL_METHODDEF
Tal Einatc7027b72015-05-16 14:14:49 +03001192 SIGNAL_GETSIGNAL_METHODDEF
Nathaniel J. Smith902ab802017-12-17 20:10:18 -08001193 {"set_wakeup_fd", (PyCFunction)signal_set_wakeup_fd, METH_VARARGS | METH_KEYWORDS, set_wakeup_fd_doc},
Tal Einatc7027b72015-05-16 14:14:49 +03001194 SIGNAL_SIGINTERRUPT_METHODDEF
1195 SIGNAL_PAUSE_METHODDEF
1196 SIGNAL_PTHREAD_KILL_METHODDEF
1197 SIGNAL_PTHREAD_SIGMASK_METHODDEF
1198 SIGNAL_SIGPENDING_METHODDEF
1199 SIGNAL_SIGWAIT_METHODDEF
1200 SIGNAL_SIGWAITINFO_METHODDEF
1201 SIGNAL_SIGTIMEDWAIT_METHODDEF
Antoine Pitrou9d3627e2018-05-04 13:00:50 +02001202#if defined(HAVE_SIGFILLSET) || defined(MS_WINDOWS)
1203 SIGNAL_VALID_SIGNALS_METHODDEF
1204#endif
Tal Einatc7027b72015-05-16 14:14:49 +03001205 {NULL, NULL} /* sentinel */
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001206};
1207
Barry Warsaw92971171997-01-03 00:14:25 +00001208
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001209PyDoc_STRVAR(module_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001210"This module provides mechanisms to use signal handlers in Python.\n\
1211\n\
1212Functions:\n\
1213\n\
1214alarm() -- cause SIGALRM after a specified time [Unix only]\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001215setitimer() -- cause a signal (described below) after a specified\n\
1216 float time and the timer may restart then [Unix only]\n\
1217getitimer() -- get current value of timer [Unix only]\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001218signal() -- set the action for a given signal\n\
1219getsignal() -- get the signal action for a given signal\n\
1220pause() -- wait until a signal arrives [Unix only]\n\
1221default_int_handler() -- default SIGINT handler\n\
1222\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001223signal constants:\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001224SIG_DFL -- used to refer to the system default handler\n\
1225SIG_IGN -- used to ignore the signal\n\
1226NSIG -- number of defined signals\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001227SIGINT, SIGTERM, etc. -- signal numbers\n\
1228\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001229itimer constants:\n\
1230ITIMER_REAL -- decrements in real time, and delivers SIGALRM upon\n\
1231 expiration\n\
1232ITIMER_VIRTUAL -- decrements only when the process is executing,\n\
1233 and delivers SIGVTALRM upon expiration\n\
1234ITIMER_PROF -- decrements both when the process is executing and\n\
1235 when the system is executing on behalf of the process.\n\
1236 Coupled with ITIMER_VIRTUAL, this timer is usually\n\
1237 used to profile the time spent by the application\n\
1238 in user and kernel space. SIGPROF is delivered upon\n\
1239 expiration.\n\
1240\n\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001241*** IMPORTANT NOTICE ***\n\
1242A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001243the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001244
Martin v. Löwis1a214512008-06-11 05:26:20 +00001245static struct PyModuleDef signalmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001246 PyModuleDef_HEAD_INIT,
Brett Cannon815a6f32014-04-04 10:20:28 -04001247 "_signal",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001248 module_doc,
1249 -1,
1250 signal_methods,
1251 NULL,
1252 NULL,
1253 NULL,
1254 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001255};
1256
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001257PyMODINIT_FUNC
Giampaolo Rodola'e09fb712014-04-04 15:34:17 +02001258PyInit__signal(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001259{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001260 PyObject *m, *d, *x;
1261 int i;
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001262
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001263 main_thread = PyThread_get_thread_ident();
1264 main_pid = getpid();
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001265
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001266 /* Create the module and add the functions */
1267 m = PyModule_Create(&signalmodule);
1268 if (m == NULL)
1269 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001270
Ross Lagerwallbc808222011-06-25 12:13:40 +02001271#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
Victor Stinner1c8f0592013-07-22 22:24:54 +02001272 if (!initialized) {
1273 if (PyStructSequence_InitType2(&SiginfoType, &struct_siginfo_desc) < 0)
1274 return NULL;
1275 }
Ross Lagerwallbc808222011-06-25 12:13:40 +02001276 Py_INCREF((PyObject*) &SiginfoType);
1277 PyModule_AddObject(m, "struct_siginfo", (PyObject*) &SiginfoType);
1278 initialized = 1;
1279#endif
1280
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001281 /* Add some symbolic constants to the module */
1282 d = PyModule_GetDict(m);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001283
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001284 x = DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL);
1285 if (!x || PyDict_SetItemString(d, "SIG_DFL", x) < 0)
1286 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001287
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001288 x = IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN);
1289 if (!x || PyDict_SetItemString(d, "SIG_IGN", x) < 0)
1290 goto finally;
Barry Warsaw92971171997-01-03 00:14:25 +00001291
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001292 x = PyLong_FromLong((long)NSIG);
1293 if (!x || PyDict_SetItemString(d, "NSIG", x) < 0)
1294 goto finally;
1295 Py_DECREF(x);
Barry Warsaw92971171997-01-03 00:14:25 +00001296
Victor Stinnera9293352011-04-30 15:21:58 +02001297#ifdef SIG_BLOCK
Victor Stinner72c53b52011-05-02 16:15:43 +02001298 if (PyModule_AddIntMacro(m, SIG_BLOCK))
1299 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001300#endif
Victor Stinnera9293352011-04-30 15:21:58 +02001301#ifdef SIG_UNBLOCK
Victor Stinner72c53b52011-05-02 16:15:43 +02001302 if (PyModule_AddIntMacro(m, SIG_UNBLOCK))
1303 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001304#endif
Victor Stinnera9293352011-04-30 15:21:58 +02001305#ifdef SIG_SETMASK
Victor Stinner72c53b52011-05-02 16:15:43 +02001306 if (PyModule_AddIntMacro(m, SIG_SETMASK))
1307 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001308#endif
1309
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001310 x = IntHandler = PyDict_GetItemString(d, "default_int_handler");
1311 if (!x)
1312 goto finally;
1313 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +00001314
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001315 _Py_atomic_store_relaxed(&Handlers[0].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001316 for (i = 1; i < NSIG; i++) {
1317 void (*t)(int);
1318 t = PyOS_getsig(i);
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001319 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001320 if (t == SIG_DFL)
1321 Handlers[i].func = DefaultHandler;
1322 else if (t == SIG_IGN)
1323 Handlers[i].func = IgnoreHandler;
1324 else
1325 Handlers[i].func = Py_None; /* None of our business */
1326 Py_INCREF(Handlers[i].func);
1327 }
1328 if (Handlers[SIGINT].func == DefaultHandler) {
1329 /* Install default int handler */
1330 Py_INCREF(IntHandler);
Serhiy Storchaka57a01d32016-04-10 18:05:40 +03001331 Py_SETREF(Handlers[SIGINT].func, IntHandler);
pkerlinge905c842018-06-01 09:47:18 +00001332 PyOS_setsig(SIGINT, signal_handler);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001333 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001334
1335#ifdef SIGHUP
Christian Heimes6782b142016-09-09 00:11:45 +02001336 if (PyModule_AddIntMacro(m, SIGHUP))
1337 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001338#endif
1339#ifdef SIGINT
Christian Heimes6782b142016-09-09 00:11:45 +02001340 if (PyModule_AddIntMacro(m, SIGINT))
1341 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001342#endif
Tim Peters1ce3cf72001-10-01 17:58:40 +00001343#ifdef SIGBREAK
Christian Heimes6782b142016-09-09 00:11:45 +02001344 if (PyModule_AddIntMacro(m, SIGBREAK))
1345 goto finally;
Tim Peters1ce3cf72001-10-01 17:58:40 +00001346#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001347#ifdef SIGQUIT
Christian Heimes6782b142016-09-09 00:11:45 +02001348 if (PyModule_AddIntMacro(m, SIGQUIT))
1349 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001350#endif
1351#ifdef SIGILL
Christian Heimes6782b142016-09-09 00:11:45 +02001352 if (PyModule_AddIntMacro(m, SIGILL))
1353 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001354#endif
1355#ifdef SIGTRAP
Christian Heimes6782b142016-09-09 00:11:45 +02001356 if (PyModule_AddIntMacro(m, SIGTRAP))
1357 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001358#endif
1359#ifdef SIGIOT
Christian Heimes6782b142016-09-09 00:11:45 +02001360 if (PyModule_AddIntMacro(m, SIGIOT))
1361 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001362#endif
1363#ifdef SIGABRT
Christian Heimes6782b142016-09-09 00:11:45 +02001364 if (PyModule_AddIntMacro(m, SIGABRT))
1365 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001366#endif
1367#ifdef SIGEMT
Christian Heimes6782b142016-09-09 00:11:45 +02001368 if (PyModule_AddIntMacro(m, SIGEMT))
1369 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001370#endif
1371#ifdef SIGFPE
Christian Heimes6782b142016-09-09 00:11:45 +02001372 if (PyModule_AddIntMacro(m, SIGFPE))
1373 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001374#endif
1375#ifdef SIGKILL
Christian Heimes6782b142016-09-09 00:11:45 +02001376 if (PyModule_AddIntMacro(m, SIGKILL))
1377 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001378#endif
1379#ifdef SIGBUS
Christian Heimes6782b142016-09-09 00:11:45 +02001380 if (PyModule_AddIntMacro(m, SIGBUS))
1381 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001382#endif
1383#ifdef SIGSEGV
Christian Heimes6782b142016-09-09 00:11:45 +02001384 if (PyModule_AddIntMacro(m, SIGSEGV))
1385 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001386#endif
1387#ifdef SIGSYS
Christian Heimes6782b142016-09-09 00:11:45 +02001388 if (PyModule_AddIntMacro(m, SIGSYS))
1389 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001390#endif
1391#ifdef SIGPIPE
Christian Heimes6782b142016-09-09 00:11:45 +02001392 if (PyModule_AddIntMacro(m, SIGPIPE))
1393 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001394#endif
1395#ifdef SIGALRM
Christian Heimes6782b142016-09-09 00:11:45 +02001396 if (PyModule_AddIntMacro(m, SIGALRM))
1397 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001398#endif
1399#ifdef SIGTERM
Christian Heimes6782b142016-09-09 00:11:45 +02001400 if (PyModule_AddIntMacro(m, SIGTERM))
1401 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001402#endif
1403#ifdef SIGUSR1
Christian Heimes6782b142016-09-09 00:11:45 +02001404 if (PyModule_AddIntMacro(m, SIGUSR1))
1405 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001406#endif
1407#ifdef SIGUSR2
Christian Heimes6782b142016-09-09 00:11:45 +02001408 if (PyModule_AddIntMacro(m, SIGUSR2))
1409 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001410#endif
1411#ifdef SIGCLD
Christian Heimes6782b142016-09-09 00:11:45 +02001412 if (PyModule_AddIntMacro(m, SIGCLD))
1413 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001414#endif
1415#ifdef SIGCHLD
Christian Heimes6782b142016-09-09 00:11:45 +02001416 if (PyModule_AddIntMacro(m, SIGCHLD))
1417 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001418#endif
1419#ifdef SIGPWR
Christian Heimes6782b142016-09-09 00:11:45 +02001420 if (PyModule_AddIntMacro(m, SIGPWR))
1421 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001422#endif
1423#ifdef SIGIO
Christian Heimes6782b142016-09-09 00:11:45 +02001424 if (PyModule_AddIntMacro(m, SIGIO))
1425 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001426#endif
1427#ifdef SIGURG
Christian Heimes6782b142016-09-09 00:11:45 +02001428 if (PyModule_AddIntMacro(m, SIGURG))
1429 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001430#endif
1431#ifdef SIGWINCH
Christian Heimes6782b142016-09-09 00:11:45 +02001432 if (PyModule_AddIntMacro(m, SIGWINCH))
1433 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001434#endif
1435#ifdef SIGPOLL
Christian Heimes6782b142016-09-09 00:11:45 +02001436 if (PyModule_AddIntMacro(m, SIGPOLL))
1437 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001438#endif
1439#ifdef SIGSTOP
Christian Heimes6782b142016-09-09 00:11:45 +02001440 if (PyModule_AddIntMacro(m, SIGSTOP))
1441 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001442#endif
1443#ifdef SIGTSTP
Christian Heimes6782b142016-09-09 00:11:45 +02001444 if (PyModule_AddIntMacro(m, SIGTSTP))
1445 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001446#endif
1447#ifdef SIGCONT
Christian Heimes6782b142016-09-09 00:11:45 +02001448 if (PyModule_AddIntMacro(m, SIGCONT))
1449 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001450#endif
1451#ifdef SIGTTIN
Christian Heimes6782b142016-09-09 00:11:45 +02001452 if (PyModule_AddIntMacro(m, SIGTTIN))
1453 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001454#endif
1455#ifdef SIGTTOU
Christian Heimes6782b142016-09-09 00:11:45 +02001456 if (PyModule_AddIntMacro(m, SIGTTOU))
1457 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001458#endif
1459#ifdef SIGVTALRM
Christian Heimes6782b142016-09-09 00:11:45 +02001460 if (PyModule_AddIntMacro(m, SIGVTALRM))
1461 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001462#endif
1463#ifdef SIGPROF
Christian Heimes6782b142016-09-09 00:11:45 +02001464 if (PyModule_AddIntMacro(m, SIGPROF))
1465 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001466#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001467#ifdef SIGXCPU
Christian Heimes6782b142016-09-09 00:11:45 +02001468 if (PyModule_AddIntMacro(m, SIGXCPU))
1469 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001470#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001471#ifdef SIGXFSZ
Christian Heimes6782b142016-09-09 00:11:45 +02001472 if (PyModule_AddIntMacro(m, SIGXFSZ))
1473 goto finally;
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001474#endif
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001475#ifdef SIGRTMIN
Christian Heimes6782b142016-09-09 00:11:45 +02001476 if (PyModule_AddIntMacro(m, SIGRTMIN))
1477 goto finally;
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001478#endif
1479#ifdef SIGRTMAX
Christian Heimes6782b142016-09-09 00:11:45 +02001480 if (PyModule_AddIntMacro(m, SIGRTMAX))
1481 goto finally;
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001482#endif
Martin v. Löwis175af252002-01-12 11:43:25 +00001483#ifdef SIGINFO
Christian Heimes6782b142016-09-09 00:11:45 +02001484 if (PyModule_AddIntMacro(m, SIGINFO))
1485 goto finally;
Martin v. Löwis175af252002-01-12 11:43:25 +00001486#endif
Martin v. Löwis823725e2008-03-24 13:39:54 +00001487
1488#ifdef ITIMER_REAL
Christian Heimes6782b142016-09-09 00:11:45 +02001489 if (PyModule_AddIntMacro(m, ITIMER_REAL))
1490 goto finally;
Martin v. Löwis823725e2008-03-24 13:39:54 +00001491#endif
1492#ifdef ITIMER_VIRTUAL
Christian Heimes6782b142016-09-09 00:11:45 +02001493 if (PyModule_AddIntMacro(m, ITIMER_VIRTUAL))
1494 goto finally;
Martin v. Löwis823725e2008-03-24 13:39:54 +00001495#endif
1496#ifdef ITIMER_PROF
Christian Heimes6782b142016-09-09 00:11:45 +02001497 if (PyModule_AddIntMacro(m, ITIMER_PROF))
1498 goto finally;
Martin v. Löwis823725e2008-03-24 13:39:54 +00001499#endif
1500
1501#if defined (HAVE_SETITIMER) || defined (HAVE_GETITIMER)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001502 ItimerError = PyErr_NewException("signal.ItimerError",
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +03001503 PyExc_OSError, NULL);
Neal Norwitz2f99b242008-08-24 05:48:10 +00001504 if (ItimerError != NULL)
Martin Panter6d57fe12016-09-17 03:26:16 +00001505 PyDict_SetItemString(d, "ItimerError", ItimerError);
Martin v. Löwis823725e2008-03-24 13:39:54 +00001506#endif
1507
Brian Curtineb24d742010-04-12 17:16:38 +00001508#ifdef CTRL_C_EVENT
Christian Heimes6782b142016-09-09 00:11:45 +02001509 if (PyModule_AddIntMacro(m, CTRL_C_EVENT))
1510 goto finally;
Brian Curtineb24d742010-04-12 17:16:38 +00001511#endif
1512
1513#ifdef CTRL_BREAK_EVENT
Christian Heimes6782b142016-09-09 00:11:45 +02001514 if (PyModule_AddIntMacro(m, CTRL_BREAK_EVENT))
1515 goto finally;
Brian Curtineb24d742010-04-12 17:16:38 +00001516#endif
1517
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001518#ifdef MS_WINDOWS
1519 /* Create manual-reset event, initially unset */
1520 sigint_event = CreateEvent(NULL, TRUE, FALSE, FALSE);
1521#endif
1522
Martin v. Löwis1a214512008-06-11 05:26:20 +00001523 if (PyErr_Occurred()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001524 Py_DECREF(m);
1525 m = NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001526 }
Barry Warsaw92971171997-01-03 00:14:25 +00001527
Barry Warsaw92971171997-01-03 00:14:25 +00001528 finally:
Martin v. Löwis1a214512008-06-11 05:26:20 +00001529 return m;
Guido van Rossum08c16611997-08-02 03:01:42 +00001530}
1531
1532static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001533finisignal(void)
Guido van Rossum08c16611997-08-02 03:01:42 +00001534{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001535 int i;
1536 PyObject *func;
Guido van Rossum08c16611997-08-02 03:01:42 +00001537
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001538 for (i = 1; i < NSIG; i++) {
1539 func = Handlers[i].func;
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001540 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001541 Handlers[i].func = NULL;
pkerlinge905c842018-06-01 09:47:18 +00001542 if (func != NULL && func != Py_None &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001543 func != DefaultHandler && func != IgnoreHandler)
1544 PyOS_setsig(i, SIG_DFL);
1545 Py_XDECREF(func);
1546 }
Guido van Rossum08c16611997-08-02 03:01:42 +00001547
Serhiy Storchaka505ff752014-02-09 13:33:53 +02001548 Py_CLEAR(IntHandler);
1549 Py_CLEAR(DefaultHandler);
1550 Py_CLEAR(IgnoreHandler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001551}
1552
Barry Warsaw92971171997-01-03 00:14:25 +00001553
Barry Warsaw92971171997-01-03 00:14:25 +00001554/* Declared in pyerrors.h */
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001555int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001556PyErr_CheckSignals(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001557{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001558 int i;
1559 PyObject *f;
Barry Warsaw92971171997-01-03 00:14:25 +00001560
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001561 if (!_Py_atomic_load(&is_tripped))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001562 return 0;
Christian Heimesb76922a2007-12-11 01:06:40 +00001563
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001564 if (PyThread_get_thread_ident() != main_thread)
1565 return 0;
Christian Heimesb76922a2007-12-11 01:06:40 +00001566
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001567 /*
1568 * The is_tripped variable is meant to speed up the calls to
1569 * PyErr_CheckSignals (both directly or via pending calls) when no
1570 * signal has arrived. This variable is set to 1 when a signal arrives
1571 * and it is set to 0 here, when we know some signals arrived. This way
1572 * we can run the registered handlers with no signals blocked.
1573 *
1574 * NOTE: with this approach we can have a situation where is_tripped is
1575 * 1 but we have no more signals to handle (Handlers[i].tripped
1576 * is 0 for every signal i). This won't do us any harm (except
1577 * we're gonna spent some cycles for nothing). This happens when
1578 * we receive a signal i after we zero is_tripped and before we
1579 * check Handlers[i].tripped.
1580 */
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001581 _Py_atomic_store(&is_tripped, 0);
Christian Heimesb76922a2007-12-11 01:06:40 +00001582
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001583 if (!(f = (PyObject *)PyEval_GetFrame()))
1584 f = Py_None;
Christian Heimesb76922a2007-12-11 01:06:40 +00001585
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001586 for (i = 1; i < NSIG; i++) {
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001587 if (_Py_atomic_load_relaxed(&Handlers[i].tripped)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001588 PyObject *result = NULL;
1589 PyObject *arglist = Py_BuildValue("(iO)", i, f);
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001590 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Barry Warsaw92971171997-01-03 00:14:25 +00001591
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001592 if (arglist) {
1593 result = PyEval_CallObject(Handlers[i].func,
1594 arglist);
1595 Py_DECREF(arglist);
1596 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +02001597 if (!result) {
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001598 _Py_atomic_store(&is_tripped, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001599 return -1;
Antoine Pitrouc08177a2017-06-28 23:29:29 +02001600 }
Barry Warsaw92971171997-01-03 00:14:25 +00001601
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001602 Py_DECREF(result);
1603 }
1604 }
Christian Heimesb76922a2007-12-11 01:06:40 +00001605
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001606 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001607}
1608
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +00001609
Barry Warsaw92971171997-01-03 00:14:25 +00001610/* Replacements for intrcheck.c functionality
1611 * Declared in pyerrors.h
1612 */
1613void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001614PyErr_SetInterrupt(void)
Barry Warsaw92971171997-01-03 00:14:25 +00001615{
Victor Stinner6c9b35b2011-04-18 16:25:56 +02001616 trip_signal(SIGINT);
Barry Warsaw92971171997-01-03 00:14:25 +00001617}
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001618
1619void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001620PyOS_InitInterrupts(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001621{
Giampaolo Rodola'e09fb712014-04-04 15:34:17 +02001622 PyObject *m = PyImport_ImportModule("_signal");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001623 if (m) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001624 Py_DECREF(m);
1625 }
Guido van Rossum08c16611997-08-02 03:01:42 +00001626}
1627
1628void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001629PyOS_FiniInterrupts(void)
Guido van Rossum08c16611997-08-02 03:01:42 +00001630{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001631 finisignal();
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001632}
1633
1634int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001635PyOS_InterruptOccurred(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001636{
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001637 if (_Py_atomic_load_relaxed(&Handlers[SIGINT].tripped)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001638 if (PyThread_get_thread_ident() != main_thread)
1639 return 0;
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001640 _Py_atomic_store_relaxed(&Handlers[SIGINT].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001641 return 1;
1642 }
1643 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001644}
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001645
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001646static void
1647_clear_pending_signals(void)
1648{
1649 int i;
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001650 if (!_Py_atomic_load(&is_tripped))
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001651 return;
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001652 _Py_atomic_store(&is_tripped, 0);
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001653 for (i = 1; i < NSIG; ++i) {
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001654 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001655 }
1656}
1657
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001658void
Antoine Pitrou346cbd32017-05-27 17:50:54 +02001659_PySignal_AfterFork(void)
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001660{
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001661 /* Clear the signal flags after forking so that they aren't handled
1662 * in both processes if they came in just before the fork() but before
1663 * the interpreter had an opportunity to call the handlers. issue9535. */
1664 _clear_pending_signals();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001665 main_thread = PyThread_get_thread_ident();
1666 main_pid = getpid();
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001667}
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001668
1669int
1670_PyOS_IsMainThread(void)
1671{
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001672 return PyThread_get_thread_ident() == main_thread;
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001673}
1674
1675#ifdef MS_WINDOWS
1676void *_PyOS_SigintEvent(void)
1677{
1678 /* Returns a manual-reset event which gets tripped whenever
1679 SIGINT is received.
1680
1681 Python.h does not include windows.h so we do cannot use HANDLE
1682 as the return type of this function. We use void* instead. */
1683 return sigint_event;
1684}
1685#endif