blob: d1209485827331f92c211dc1072ba44140a0ee16 [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
Michael Osipov48ce4892018-08-23 15:27:19 +0200533#ifndef HAVE_STRSIGNAL
Antoine Pietri5d2a27d2018-03-12 14:42:34 +0100534 switch (signalnum) {
Michael Osipov48ce4892018-08-23 15:27:19 +0200535 /* Though being a UNIX, HP-UX does not provide strsignal(3). */
536#ifndef MS_WINDOWS
537 case SIGHUP:
538 res = "Hangup";
539 break;
540 case SIGALRM:
541 res = "Alarm clock";
542 break;
543 case SIGPIPE:
544 res = "Broken pipe";
545 break;
546 case SIGQUIT:
547 res = "Quit";
548 break;
549 case SIGCHLD:
550 res = "Child exited";
551 break;
552#endif
553 /* Custom redefinition of POSIX signals allowed on Windows. */
Antoine Pietri5d2a27d2018-03-12 14:42:34 +0100554 case SIGINT:
555 res = "Interrupt";
556 break;
557 case SIGILL:
558 res = "Illegal instruction";
559 break;
560 case SIGABRT:
561 res = "Aborted";
562 break;
563 case SIGFPE:
564 res = "Floating point exception";
565 break;
566 case SIGSEGV:
567 res = "Segmentation fault";
568 break;
569 case SIGTERM:
570 res = "Terminated";
571 break;
572 default:
573 Py_RETURN_NONE;
574 }
575#else
576 errno = 0;
577 res = strsignal(signalnum);
578
579 if (errno || res == NULL || strstr(res, "Unknown signal") != NULL)
580 Py_RETURN_NONE;
581#endif
582
583 return Py_BuildValue("s", res);
584}
585
Christian Heimes8640e742008-02-23 16:23:06 +0000586#ifdef HAVE_SIGINTERRUPT
Tal Einatc7027b72015-05-16 14:14:49 +0300587
588/*[clinic input]
589signal.siginterrupt
590
591 signalnum: int
592 flag: int
593 /
594
595Change system call restart behaviour.
596
597If flag is False, system calls will be restarted when interrupted by
598signal sig, else system calls will be interrupted.
599[clinic start generated code]*/
Christian Heimes8640e742008-02-23 16:23:06 +0000600
601static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300602signal_siginterrupt_impl(PyObject *module, int signalnum, int flag)
603/*[clinic end generated code: output=063816243d85dd19 input=4160acacca3e2099]*/
Christian Heimes8640e742008-02-23 16:23:06 +0000604{
Tal Einatc7027b72015-05-16 14:14:49 +0300605 if (signalnum < 1 || signalnum >= NSIG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000606 PyErr_SetString(PyExc_ValueError,
607 "signal number out of range");
608 return NULL;
609 }
Tal Einatc7027b72015-05-16 14:14:49 +0300610 if (siginterrupt(signalnum, flag)<0) {
Victor Stinner388196e2011-05-10 17:13:00 +0200611 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000612 return NULL;
613 }
Tal Einatc7027b72015-05-16 14:14:49 +0300614 Py_RETURN_NONE;
Christian Heimes8640e742008-02-23 16:23:06 +0000615}
616
617#endif
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000618
Tal Einatc7027b72015-05-16 14:14:49 +0300619
620static PyObject*
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800621signal_set_wakeup_fd(PyObject *self, PyObject *args, PyObject *kwds)
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000622{
Victor Stinnere134a7f2015-03-30 10:09:31 +0200623 struct _Py_stat_struct status;
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800624 static char *kwlist[] = {
625 "", "warn_on_full_buffer", NULL,
626 };
627 int warn_on_full_buffer = 1;
Victor Stinner11517102014-07-29 23:31:34 +0200628#ifdef MS_WINDOWS
629 PyObject *fdobj;
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100630 SOCKET_T sockfd, old_sockfd;
Victor Stinner11517102014-07-29 23:31:34 +0200631 int res;
632 int res_size = sizeof res;
633 PyObject *mod;
Victor Stinner11517102014-07-29 23:31:34 +0200634 int is_socket;
635
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800636 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|$p:set_wakeup_fd", kwlist,
637 &fdobj, &warn_on_full_buffer))
Victor Stinner11517102014-07-29 23:31:34 +0200638 return NULL;
639
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100640 sockfd = PyLong_AsSocket_t(fdobj);
641 if (sockfd == (SOCKET_T)(-1) && PyErr_Occurred())
Victor Stinner11517102014-07-29 23:31:34 +0200642 return NULL;
643#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000644 int fd, old_fd;
Victor Stinner11517102014-07-29 23:31:34 +0200645
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800646 if (!PyArg_ParseTupleAndKeywords(args, kwds, "i|$p:set_wakeup_fd", kwlist,
647 &fd, &warn_on_full_buffer))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000648 return NULL;
Victor Stinner11517102014-07-29 23:31:34 +0200649#endif
650
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000651 if (PyThread_get_thread_ident() != main_thread) {
652 PyErr_SetString(PyExc_ValueError,
653 "set_wakeup_fd only works in main thread");
654 return NULL;
655 }
Victor Stinner0bffc942014-07-21 16:28:54 +0200656
Victor Stinner11517102014-07-29 23:31:34 +0200657#ifdef MS_WINDOWS
658 is_socket = 0;
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100659 if (sockfd != INVALID_FD) {
Victor Stinner11517102014-07-29 23:31:34 +0200660 /* Import the _socket module to call WSAStartup() */
661 mod = PyImport_ImportModuleNoBlock("_socket");
662 if (mod == NULL)
663 return NULL;
664 Py_DECREF(mod);
665
666 /* test the socket */
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100667 if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR,
Victor Stinner11517102014-07-29 23:31:34 +0200668 (char *)&res, &res_size) != 0) {
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100669 int fd, err;
670
671 err = WSAGetLastError();
Victor Stinner11517102014-07-29 23:31:34 +0200672 if (err != WSAENOTSOCK) {
673 PyErr_SetExcFromWindowsErr(PyExc_OSError, err);
674 return NULL;
675 }
676
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100677 fd = (int)sockfd;
Steve Dower940f33a2016-09-08 11:21:54 -0700678 if ((SOCKET_T)fd != sockfd) {
Victor Stinner11517102014-07-29 23:31:34 +0200679 PyErr_SetString(PyExc_ValueError, "invalid fd");
680 return NULL;
681 }
682
Victor Stinnere134a7f2015-03-30 10:09:31 +0200683 if (_Py_fstat(fd, &status) != 0)
Victor Stinner11517102014-07-29 23:31:34 +0200684 return NULL;
Victor Stinner38227602014-08-27 12:59:44 +0200685
686 /* on Windows, a file cannot be set to non-blocking mode */
Victor Stinner11517102014-07-29 23:31:34 +0200687 }
Victor Stinner38227602014-08-27 12:59:44 +0200688 else {
Victor Stinner11517102014-07-29 23:31:34 +0200689 is_socket = 1;
Victor Stinner38227602014-08-27 12:59:44 +0200690
691 /* Windows does not provide a function to test if a socket
692 is in non-blocking mode */
693 }
Victor Stinner11517102014-07-29 23:31:34 +0200694 }
695
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100696 old_sockfd = wakeup.fd;
697 wakeup.fd = sockfd;
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800698 wakeup.warn_on_full_buffer = warn_on_full_buffer;
Victor Stinner11517102014-07-29 23:31:34 +0200699 wakeup.use_send = is_socket;
700
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100701 if (old_sockfd != INVALID_FD)
702 return PyLong_FromSocket_t(old_sockfd);
Victor Stinner11517102014-07-29 23:31:34 +0200703 else
704 return PyLong_FromLong(-1);
705#else
Victor Stinnerd18ccd12014-07-24 21:58:53 +0200706 if (fd != -1) {
Victor Stinner38227602014-08-27 12:59:44 +0200707 int blocking;
708
Victor Stinnere134a7f2015-03-30 10:09:31 +0200709 if (_Py_fstat(fd, &status) != 0)
Victor Stinner11517102014-07-29 23:31:34 +0200710 return NULL;
Victor Stinner38227602014-08-27 12:59:44 +0200711
712 blocking = _Py_get_blocking(fd);
713 if (blocking < 0)
714 return NULL;
715 if (blocking) {
716 PyErr_Format(PyExc_ValueError,
717 "the fd %i must be in non-blocking mode",
718 fd);
719 return NULL;
720 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000721 }
Victor Stinner0bffc942014-07-21 16:28:54 +0200722
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800723 old_fd = wakeup.fd;
724 wakeup.fd = fd;
725 wakeup.warn_on_full_buffer = warn_on_full_buffer;
Victor Stinner0bffc942014-07-21 16:28:54 +0200726
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000727 return PyLong_FromLong(old_fd);
Victor Stinner11517102014-07-29 23:31:34 +0200728#endif
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000729}
730
731PyDoc_STRVAR(set_wakeup_fd_doc,
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800732"set_wakeup_fd(fd, *, warn_on_full_buffer=True) -> fd\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000733\n\
Victor Stinner11517102014-07-29 23:31:34 +0200734Sets the fd to be written to (with the signal number) when a signal\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000735comes in. A library can use this to wakeup select or poll.\n\
Victor Stinner11517102014-07-29 23:31:34 +0200736The previous fd or -1 is returned.\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000737\n\
738The fd must be non-blocking.");
739
740/* C API for the same, without all the error checking */
741int
742PySignal_SetWakeupFd(int fd)
743{
Victor Stinner11517102014-07-29 23:31:34 +0200744 int old_fd;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000745 if (fd < 0)
746 fd = -1;
Victor Stinner11517102014-07-29 23:31:34 +0200747
748#ifdef MS_WINDOWS
749 old_fd = Py_SAFE_DOWNCAST(wakeup.fd, SOCKET_T, int);
Victor Stinner11517102014-07-29 23:31:34 +0200750#else
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800751 old_fd = wakeup.fd;
Victor Stinner11517102014-07-29 23:31:34 +0200752#endif
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800753 wakeup.fd = fd;
754 wakeup.warn_on_full_buffer = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000755 return old_fd;
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000756}
757
758
Martin v. Löwis823725e2008-03-24 13:39:54 +0000759#ifdef HAVE_SETITIMER
Tal Einatc7027b72015-05-16 14:14:49 +0300760
761/*[clinic input]
762signal.setitimer
763
764 which: int
Victor Stinneref611c92017-10-13 13:49:43 -0700765 seconds: object
766 interval: object(c_default="NULL") = 0.0
Tal Einatc7027b72015-05-16 14:14:49 +0300767 /
768
769Sets given itimer (one of ITIMER_REAL, ITIMER_VIRTUAL or ITIMER_PROF).
770
771The timer will fire after value seconds and after that every interval seconds.
772The itimer can be cleared by setting seconds to zero.
773
774Returns old values as a tuple: (delay, interval).
775[clinic start generated code]*/
776
Martin v. Löwis823725e2008-03-24 13:39:54 +0000777static PyObject *
Victor Stinneref611c92017-10-13 13:49:43 -0700778signal_setitimer_impl(PyObject *module, int which, PyObject *seconds,
779 PyObject *interval)
780/*[clinic end generated code: output=65f9dcbddc35527b input=de43daf194e6f66f]*/
Martin v. Löwis823725e2008-03-24 13:39:54 +0000781{
Martin v. Löwis823725e2008-03-24 13:39:54 +0000782 struct itimerval new, old;
783
Victor Stinneref611c92017-10-13 13:49:43 -0700784 if (timeval_from_double(seconds, &new.it_value) < 0) {
785 return NULL;
786 }
787 if (timeval_from_double(interval, &new.it_interval) < 0) {
788 return NULL;
789 }
790
Martin v. Löwis823725e2008-03-24 13:39:54 +0000791 /* Let OS check "which" value */
792 if (setitimer(which, &new, &old) != 0) {
Tal Einatc7027b72015-05-16 14:14:49 +0300793 PyErr_SetFromErrno(ItimerError);
794 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000795 }
796
797 return itimer_retval(&old);
798}
799
Martin v. Löwis823725e2008-03-24 13:39:54 +0000800#endif
801
802
803#ifdef HAVE_GETITIMER
Tal Einatc7027b72015-05-16 14:14:49 +0300804
805/*[clinic input]
806signal.getitimer
807
808 which: int
809 /
810
811Returns current value of given itimer.
812[clinic start generated code]*/
813
Martin v. Löwis823725e2008-03-24 13:39:54 +0000814static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300815signal_getitimer_impl(PyObject *module, int which)
816/*[clinic end generated code: output=9e053175d517db40 input=f7d21d38f3490627]*/
Martin v. Löwis823725e2008-03-24 13:39:54 +0000817{
Martin v. Löwis823725e2008-03-24 13:39:54 +0000818 struct itimerval old;
819
Martin v. Löwis823725e2008-03-24 13:39:54 +0000820 if (getitimer(which, &old) != 0) {
Tal Einatc7027b72015-05-16 14:14:49 +0300821 PyErr_SetFromErrno(ItimerError);
822 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000823 }
824
825 return itimer_retval(&old);
826}
827
Martin v. Löwis823725e2008-03-24 13:39:54 +0000828#endif
829
Victor Stinnerb3e72192011-05-08 01:46:11 +0200830#if defined(PYPTHREAD_SIGMASK) || defined(HAVE_SIGPENDING)
Victor Stinner35b300c2011-05-04 13:20:35 +0200831static PyObject*
832sigset_to_set(sigset_t mask)
833{
834 PyObject *signum, *result;
835 int sig;
836
837 result = PySet_New(0);
838 if (result == NULL)
839 return NULL;
840
841 for (sig = 1; sig < NSIG; sig++) {
842 if (sigismember(&mask, sig) != 1)
843 continue;
844
845 /* Handle the case where it is a member by adding the signal to
846 the result list. Ignore the other cases because they mean the
847 signal isn't a member of the mask or the signal was invalid,
848 and an invalid signal must have been our fault in constructing
849 the loop boundaries. */
850 signum = PyLong_FromLong(sig);
851 if (signum == NULL) {
852 Py_DECREF(result);
853 return NULL;
854 }
855 if (PySet_Add(result, signum) == -1) {
856 Py_DECREF(signum);
857 Py_DECREF(result);
858 return NULL;
859 }
860 Py_DECREF(signum);
861 }
862 return result;
863}
Victor Stinnerb3e72192011-05-08 01:46:11 +0200864#endif
Victor Stinner35b300c2011-05-04 13:20:35 +0200865
Victor Stinnerb3e72192011-05-08 01:46:11 +0200866#ifdef PYPTHREAD_SIGMASK
Tal Einatc7027b72015-05-16 14:14:49 +0300867
868/*[clinic input]
869signal.pthread_sigmask
870
871 how: int
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300872 mask: sigset_t
Tal Einatc7027b72015-05-16 14:14:49 +0300873 /
874
875Fetch and/or change the signal mask of the calling thread.
876[clinic start generated code]*/
877
Victor Stinnera9293352011-04-30 15:21:58 +0200878static PyObject *
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300879signal_pthread_sigmask_impl(PyObject *module, int how, sigset_t mask)
880/*[clinic end generated code: output=0562c0fb192981a8 input=85bcebda442fa77f]*/
Victor Stinnera9293352011-04-30 15:21:58 +0200881{
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300882 sigset_t previous;
Victor Stinnera9293352011-04-30 15:21:58 +0200883 int err;
884
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300885 err = pthread_sigmask(how, &mask, &previous);
Victor Stinnera9293352011-04-30 15:21:58 +0200886 if (err != 0) {
887 errno = err;
Victor Stinnerb3e72192011-05-08 01:46:11 +0200888 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnera9293352011-04-30 15:21:58 +0200889 return NULL;
890 }
891
Victor Stinnerd0e516d2011-05-03 14:57:12 +0200892 /* if signals was unblocked, signal handlers have been called */
893 if (PyErr_CheckSignals())
894 return NULL;
895
Victor Stinner35b300c2011-05-04 13:20:35 +0200896 return sigset_to_set(previous);
Victor Stinnera9293352011-04-30 15:21:58 +0200897}
898
Victor Stinnera9293352011-04-30 15:21:58 +0200899#endif /* #ifdef PYPTHREAD_SIGMASK */
900
Martin v. Löwis823725e2008-03-24 13:39:54 +0000901
Victor Stinnerb3e72192011-05-08 01:46:11 +0200902#ifdef HAVE_SIGPENDING
Tal Einatc7027b72015-05-16 14:14:49 +0300903
904/*[clinic input]
905signal.sigpending
906
907Examine pending signals.
908
909Returns a set of signal numbers that are pending for delivery to
910the calling thread.
911[clinic start generated code]*/
912
Victor Stinnerb3e72192011-05-08 01:46:11 +0200913static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300914signal_sigpending_impl(PyObject *module)
915/*[clinic end generated code: output=53375ffe89325022 input=e0036c016f874e29]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +0200916{
917 int err;
918 sigset_t mask;
919 err = sigpending(&mask);
920 if (err)
921 return PyErr_SetFromErrno(PyExc_OSError);
922 return sigset_to_set(mask);
923}
924
Victor Stinnerb3e72192011-05-08 01:46:11 +0200925#endif /* #ifdef HAVE_SIGPENDING */
926
927
928#ifdef HAVE_SIGWAIT
Tal Einatc7027b72015-05-16 14:14:49 +0300929
930/*[clinic input]
931signal.sigwait
932
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300933 sigset: sigset_t
Tal Einatc7027b72015-05-16 14:14:49 +0300934 /
935
936Wait for a signal.
937
938Suspend execution of the calling thread until the delivery of one of the
939signals specified in the signal set sigset. The function accepts the signal
940and returns the signal number.
941[clinic start generated code]*/
942
Victor Stinnerb3e72192011-05-08 01:46:11 +0200943static PyObject *
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300944signal_sigwait_impl(PyObject *module, sigset_t sigset)
945/*[clinic end generated code: output=f43770699d682f96 input=a6fbd47b1086d119]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +0200946{
Victor Stinnerb3e72192011-05-08 01:46:11 +0200947 int err, signum;
948
Victor Stinner10c30d62011-06-10 01:39:53 +0200949 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300950 err = sigwait(&sigset, &signum);
Victor Stinner10c30d62011-06-10 01:39:53 +0200951 Py_END_ALLOW_THREADS
Victor Stinnerb3e72192011-05-08 01:46:11 +0200952 if (err) {
953 errno = err;
954 return PyErr_SetFromErrno(PyExc_OSError);
955 }
956
957 return PyLong_FromLong(signum);
958}
959
Tal Einatc7027b72015-05-16 14:14:49 +0300960#endif /* #ifdef HAVE_SIGWAIT */
961
Victor Stinnerb3e72192011-05-08 01:46:11 +0200962
Antoine Pitrou9d3627e2018-05-04 13:00:50 +0200963#if defined(HAVE_SIGFILLSET) || defined(MS_WINDOWS)
964
965/*[clinic input]
966signal.valid_signals
967
968Return a set of valid signal numbers on this platform.
969
970The signal numbers returned by this function can be safely passed to
971functions like `pthread_sigmask`.
972[clinic start generated code]*/
973
974static PyObject *
975signal_valid_signals_impl(PyObject *module)
976/*[clinic end generated code: output=1609cffbcfcf1314 input=86a3717ff25288f2]*/
977{
978#ifdef MS_WINDOWS
979#ifdef SIGBREAK
980 PyObject *tup = Py_BuildValue("(iiiiiii)", SIGABRT, SIGBREAK, SIGFPE,
981 SIGILL, SIGINT, SIGSEGV, SIGTERM);
982#else
983 PyObject *tup = Py_BuildValue("(iiiiii)", SIGABRT, SIGFPE, SIGILL,
984 SIGINT, SIGSEGV, SIGTERM);
985#endif
986 if (tup == NULL) {
987 return NULL;
988 }
989 PyObject *set = PySet_New(tup);
990 Py_DECREF(tup);
991 return set;
992#else
993 sigset_t mask;
994 if (sigemptyset(&mask) || sigfillset(&mask)) {
995 return PyErr_SetFromErrno(PyExc_OSError);
996 }
997 return sigset_to_set(mask);
998#endif
999}
1000
1001#endif /* #if defined(HAVE_SIGFILLSET) || defined(MS_WINDOWS) */
1002
1003
Ross Lagerwallbc808222011-06-25 12:13:40 +02001004#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
1005static int initialized;
1006static PyStructSequence_Field struct_siginfo_fields[] = {
1007 {"si_signo", "signal number"},
1008 {"si_code", "signal code"},
1009 {"si_errno", "errno associated with this signal"},
1010 {"si_pid", "sending process ID"},
1011 {"si_uid", "real user ID of sending process"},
1012 {"si_status", "exit value or signal"},
1013 {"si_band", "band event for SIGPOLL"},
1014 {0}
1015};
1016
1017PyDoc_STRVAR(struct_siginfo__doc__,
1018"struct_siginfo: Result from sigwaitinfo or sigtimedwait.\n\n\
1019This object may be accessed either as a tuple of\n\
1020(si_signo, si_code, si_errno, si_pid, si_uid, si_status, si_band),\n\
1021or via the attributes si_signo, si_code, and so on.");
1022
1023static PyStructSequence_Desc struct_siginfo_desc = {
1024 "signal.struct_siginfo", /* name */
1025 struct_siginfo__doc__, /* doc */
1026 struct_siginfo_fields, /* fields */
1027 7 /* n_in_sequence */
1028};
1029
1030static PyTypeObject SiginfoType;
1031
1032static PyObject *
1033fill_siginfo(siginfo_t *si)
1034{
1035 PyObject *result = PyStructSequence_New(&SiginfoType);
1036 if (!result)
1037 return NULL;
1038
1039 PyStructSequence_SET_ITEM(result, 0, PyLong_FromLong((long)(si->si_signo)));
1040 PyStructSequence_SET_ITEM(result, 1, PyLong_FromLong((long)(si->si_code)));
1041 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si->si_errno)));
1042 PyStructSequence_SET_ITEM(result, 3, PyLong_FromPid(si->si_pid));
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02001043 PyStructSequence_SET_ITEM(result, 4, _PyLong_FromUid(si->si_uid));
Ross Lagerwallbc808222011-06-25 12:13:40 +02001044 PyStructSequence_SET_ITEM(result, 5,
1045 PyLong_FromLong((long)(si->si_status)));
Zachary Ware6a6967e2016-10-01 00:47:27 -05001046#ifdef HAVE_SIGINFO_T_SI_BAND
Ross Lagerwallbc808222011-06-25 12:13:40 +02001047 PyStructSequence_SET_ITEM(result, 6, PyLong_FromLong(si->si_band));
Zachary Ware6a6967e2016-10-01 00:47:27 -05001048#else
1049 PyStructSequence_SET_ITEM(result, 6, PyLong_FromLong(0L));
1050#endif
Ross Lagerwallbc808222011-06-25 12:13:40 +02001051 if (PyErr_Occurred()) {
1052 Py_DECREF(result);
1053 return NULL;
1054 }
1055
1056 return result;
1057}
1058#endif
1059
1060#ifdef HAVE_SIGWAITINFO
Tal Einatc7027b72015-05-16 14:14:49 +03001061
1062/*[clinic input]
1063signal.sigwaitinfo
1064
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001065 sigset: sigset_t
Tal Einatc7027b72015-05-16 14:14:49 +03001066 /
1067
1068Wait synchronously until one of the signals in *sigset* is delivered.
1069
1070Returns a struct_siginfo containing information about the signal.
1071[clinic start generated code]*/
1072
Ross Lagerwallbc808222011-06-25 12:13:40 +02001073static PyObject *
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001074signal_sigwaitinfo_impl(PyObject *module, sigset_t sigset)
1075/*[clinic end generated code: output=1eb2f1fa236fdbca input=3d1a7e1f27fc664c]*/
Ross Lagerwallbc808222011-06-25 12:13:40 +02001076{
Ross Lagerwallbc808222011-06-25 12:13:40 +02001077 siginfo_t si;
1078 int err;
Victor Stinnera453cd82015-03-20 12:54:28 +01001079 int async_err = 0;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001080
Victor Stinnera453cd82015-03-20 12:54:28 +01001081 do {
1082 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001083 err = sigwaitinfo(&sigset, &si);
Victor Stinnera453cd82015-03-20 12:54:28 +01001084 Py_END_ALLOW_THREADS
1085 } while (err == -1
1086 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Ross Lagerwallbc808222011-06-25 12:13:40 +02001087 if (err == -1)
Victor Stinnera453cd82015-03-20 12:54:28 +01001088 return (!async_err) ? PyErr_SetFromErrno(PyExc_OSError) : NULL;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001089
1090 return fill_siginfo(&si);
1091}
1092
Ross Lagerwallbc808222011-06-25 12:13:40 +02001093#endif /* #ifdef HAVE_SIGWAITINFO */
1094
1095#ifdef HAVE_SIGTIMEDWAIT
Tal Einatc7027b72015-05-16 14:14:49 +03001096
1097/*[clinic input]
1098signal.sigtimedwait
1099
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001100 sigset: sigset_t
Serhiy Storchaka6b680cd2015-05-16 15:57:56 +03001101 timeout as timeout_obj: object
Tal Einatc7027b72015-05-16 14:14:49 +03001102 /
1103
1104Like sigwaitinfo(), but with a timeout.
1105
1106The timeout is specified in seconds, with floating point numbers allowed.
1107[clinic start generated code]*/
1108
Ross Lagerwallbc808222011-06-25 12:13:40 +02001109static PyObject *
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001110signal_sigtimedwait_impl(PyObject *module, sigset_t sigset,
Serhiy Storchaka6b680cd2015-05-16 15:57:56 +03001111 PyObject *timeout_obj)
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001112/*[clinic end generated code: output=59c8971e8ae18a64 input=87fd39237cf0b7ba]*/
Ross Lagerwallbc808222011-06-25 12:13:40 +02001113{
Victor Stinnera453cd82015-03-20 12:54:28 +01001114 struct timespec ts;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001115 siginfo_t si;
Victor Stinnera453cd82015-03-20 12:54:28 +01001116 int res;
Victor Stinner34dc0f42015-03-27 18:19:03 +01001117 _PyTime_t timeout, deadline, monotonic;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001118
Victor Stinner869e1772015-03-30 03:49:14 +02001119 if (_PyTime_FromSecondsObject(&timeout,
1120 timeout_obj, _PyTime_ROUND_CEILING) < 0)
Ross Lagerwallbc808222011-06-25 12:13:40 +02001121 return NULL;
1122
Victor Stinnera453cd82015-03-20 12:54:28 +01001123 if (timeout < 0) {
Ross Lagerwallbc808222011-06-25 12:13:40 +02001124 PyErr_SetString(PyExc_ValueError, "timeout must be non-negative");
1125 return NULL;
1126 }
1127
Victor Stinner34dc0f42015-03-27 18:19:03 +01001128 deadline = _PyTime_GetMonotonicClock() + timeout;
Victor Stinnera453cd82015-03-20 12:54:28 +01001129
1130 do {
Victor Stinner34dc0f42015-03-27 18:19:03 +01001131 if (_PyTime_AsTimespec(timeout, &ts) < 0)
1132 return NULL;
Victor Stinnera453cd82015-03-20 12:54:28 +01001133
1134 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001135 res = sigtimedwait(&sigset, &si, &ts);
Victor Stinnera453cd82015-03-20 12:54:28 +01001136 Py_END_ALLOW_THREADS
1137
1138 if (res != -1)
1139 break;
1140
1141 if (errno != EINTR) {
1142 if (errno == EAGAIN)
1143 Py_RETURN_NONE;
1144 else
1145 return PyErr_SetFromErrno(PyExc_OSError);
1146 }
1147
1148 /* sigtimedwait() was interrupted by a signal (EINTR) */
1149 if (PyErr_CheckSignals())
1150 return NULL;
1151
Victor Stinner34dc0f42015-03-27 18:19:03 +01001152 monotonic = _PyTime_GetMonotonicClock();
1153 timeout = deadline - monotonic;
Victor Stinner6aa446c2015-03-30 21:33:51 +02001154 if (timeout < 0)
Victor Stinnera453cd82015-03-20 12:54:28 +01001155 break;
1156 } while (1);
Ross Lagerwallbc808222011-06-25 12:13:40 +02001157
1158 return fill_siginfo(&si);
1159}
1160
Ross Lagerwallbc808222011-06-25 12:13:40 +02001161#endif /* #ifdef HAVE_SIGTIMEDWAIT */
1162
Victor Stinnerb3e72192011-05-08 01:46:11 +02001163
Antoine Pitroua6a4dc82017-09-07 18:56:24 +02001164#if defined(HAVE_PTHREAD_KILL)
Tal Einatc7027b72015-05-16 14:14:49 +03001165
1166/*[clinic input]
1167signal.pthread_kill
1168
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +02001169 thread_id: unsigned_long(bitwise=True)
Tal Einatc7027b72015-05-16 14:14:49 +03001170 signalnum: int
1171 /
1172
1173Send a signal to a thread.
1174[clinic start generated code]*/
1175
Victor Stinnerb3e72192011-05-08 01:46:11 +02001176static PyObject *
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +02001177signal_pthread_kill_impl(PyObject *module, unsigned long thread_id,
1178 int signalnum)
1179/*[clinic end generated code: output=7629919b791bc27f input=1d901f2c7bb544ff]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +02001180{
Victor Stinnerb3e72192011-05-08 01:46:11 +02001181 int err;
1182
Tal Einatc7027b72015-05-16 14:14:49 +03001183 err = pthread_kill((pthread_t)thread_id, signalnum);
Victor Stinnerb3e72192011-05-08 01:46:11 +02001184 if (err != 0) {
1185 errno = err;
1186 PyErr_SetFromErrno(PyExc_OSError);
1187 return NULL;
1188 }
1189
1190 /* the signal may have been send to the current thread */
1191 if (PyErr_CheckSignals())
1192 return NULL;
1193
1194 Py_RETURN_NONE;
1195}
1196
Antoine Pitroua6a4dc82017-09-07 18:56:24 +02001197#endif /* #if defined(HAVE_PTHREAD_KILL) */
Victor Stinnerb3e72192011-05-08 01:46:11 +02001198
1199
1200
Tal Einatc7027b72015-05-16 14:14:49 +03001201/* List of functions defined in the module -- some of the methoddefs are
1202 defined to nothing if the corresponding C function is not available. */
Barry Warsaw92971171997-01-03 00:14:25 +00001203static PyMethodDef signal_methods[] = {
Tal Einatc7027b72015-05-16 14:14:49 +03001204 {"default_int_handler", signal_default_int_handler, METH_VARARGS, default_int_handler_doc},
1205 SIGNAL_ALARM_METHODDEF
1206 SIGNAL_SETITIMER_METHODDEF
1207 SIGNAL_GETITIMER_METHODDEF
1208 SIGNAL_SIGNAL_METHODDEF
Antoine Pietri5d2a27d2018-03-12 14:42:34 +01001209 SIGNAL_STRSIGNAL_METHODDEF
Tal Einatc7027b72015-05-16 14:14:49 +03001210 SIGNAL_GETSIGNAL_METHODDEF
Nathaniel J. Smith902ab802017-12-17 20:10:18 -08001211 {"set_wakeup_fd", (PyCFunction)signal_set_wakeup_fd, METH_VARARGS | METH_KEYWORDS, set_wakeup_fd_doc},
Tal Einatc7027b72015-05-16 14:14:49 +03001212 SIGNAL_SIGINTERRUPT_METHODDEF
1213 SIGNAL_PAUSE_METHODDEF
1214 SIGNAL_PTHREAD_KILL_METHODDEF
1215 SIGNAL_PTHREAD_SIGMASK_METHODDEF
1216 SIGNAL_SIGPENDING_METHODDEF
1217 SIGNAL_SIGWAIT_METHODDEF
1218 SIGNAL_SIGWAITINFO_METHODDEF
1219 SIGNAL_SIGTIMEDWAIT_METHODDEF
Antoine Pitrou9d3627e2018-05-04 13:00:50 +02001220#if defined(HAVE_SIGFILLSET) || defined(MS_WINDOWS)
1221 SIGNAL_VALID_SIGNALS_METHODDEF
1222#endif
Tal Einatc7027b72015-05-16 14:14:49 +03001223 {NULL, NULL} /* sentinel */
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001224};
1225
Barry Warsaw92971171997-01-03 00:14:25 +00001226
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001227PyDoc_STRVAR(module_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001228"This module provides mechanisms to use signal handlers in Python.\n\
1229\n\
1230Functions:\n\
1231\n\
1232alarm() -- cause SIGALRM after a specified time [Unix only]\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001233setitimer() -- cause a signal (described below) after a specified\n\
1234 float time and the timer may restart then [Unix only]\n\
1235getitimer() -- get current value of timer [Unix only]\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001236signal() -- set the action for a given signal\n\
1237getsignal() -- get the signal action for a given signal\n\
1238pause() -- wait until a signal arrives [Unix only]\n\
1239default_int_handler() -- default SIGINT handler\n\
1240\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001241signal constants:\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001242SIG_DFL -- used to refer to the system default handler\n\
1243SIG_IGN -- used to ignore the signal\n\
1244NSIG -- number of defined signals\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001245SIGINT, SIGTERM, etc. -- signal numbers\n\
1246\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001247itimer constants:\n\
1248ITIMER_REAL -- decrements in real time, and delivers SIGALRM upon\n\
1249 expiration\n\
1250ITIMER_VIRTUAL -- decrements only when the process is executing,\n\
1251 and delivers SIGVTALRM upon expiration\n\
1252ITIMER_PROF -- decrements both when the process is executing and\n\
1253 when the system is executing on behalf of the process.\n\
1254 Coupled with ITIMER_VIRTUAL, this timer is usually\n\
1255 used to profile the time spent by the application\n\
1256 in user and kernel space. SIGPROF is delivered upon\n\
1257 expiration.\n\
1258\n\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001259*** IMPORTANT NOTICE ***\n\
1260A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001261the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001262
Martin v. Löwis1a214512008-06-11 05:26:20 +00001263static struct PyModuleDef signalmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001264 PyModuleDef_HEAD_INIT,
Brett Cannon815a6f32014-04-04 10:20:28 -04001265 "_signal",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001266 module_doc,
1267 -1,
1268 signal_methods,
1269 NULL,
1270 NULL,
1271 NULL,
1272 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001273};
1274
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001275PyMODINIT_FUNC
Giampaolo Rodola'e09fb712014-04-04 15:34:17 +02001276PyInit__signal(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001277{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001278 PyObject *m, *d, *x;
1279 int i;
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001280
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001281 main_thread = PyThread_get_thread_ident();
1282 main_pid = getpid();
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001283
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001284 /* Create the module and add the functions */
1285 m = PyModule_Create(&signalmodule);
1286 if (m == NULL)
1287 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001288
Ross Lagerwallbc808222011-06-25 12:13:40 +02001289#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
Victor Stinner1c8f0592013-07-22 22:24:54 +02001290 if (!initialized) {
1291 if (PyStructSequence_InitType2(&SiginfoType, &struct_siginfo_desc) < 0)
1292 return NULL;
1293 }
Ross Lagerwallbc808222011-06-25 12:13:40 +02001294 Py_INCREF((PyObject*) &SiginfoType);
1295 PyModule_AddObject(m, "struct_siginfo", (PyObject*) &SiginfoType);
1296 initialized = 1;
1297#endif
1298
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001299 /* Add some symbolic constants to the module */
1300 d = PyModule_GetDict(m);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001301
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001302 x = DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL);
1303 if (!x || PyDict_SetItemString(d, "SIG_DFL", x) < 0)
1304 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001305
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001306 x = IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN);
1307 if (!x || PyDict_SetItemString(d, "SIG_IGN", x) < 0)
1308 goto finally;
Barry Warsaw92971171997-01-03 00:14:25 +00001309
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001310 x = PyLong_FromLong((long)NSIG);
1311 if (!x || PyDict_SetItemString(d, "NSIG", x) < 0)
1312 goto finally;
1313 Py_DECREF(x);
Barry Warsaw92971171997-01-03 00:14:25 +00001314
Victor Stinnera9293352011-04-30 15:21:58 +02001315#ifdef SIG_BLOCK
Victor Stinner72c53b52011-05-02 16:15:43 +02001316 if (PyModule_AddIntMacro(m, SIG_BLOCK))
1317 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001318#endif
Victor Stinnera9293352011-04-30 15:21:58 +02001319#ifdef SIG_UNBLOCK
Victor Stinner72c53b52011-05-02 16:15:43 +02001320 if (PyModule_AddIntMacro(m, SIG_UNBLOCK))
1321 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001322#endif
Victor Stinnera9293352011-04-30 15:21:58 +02001323#ifdef SIG_SETMASK
Victor Stinner72c53b52011-05-02 16:15:43 +02001324 if (PyModule_AddIntMacro(m, SIG_SETMASK))
1325 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001326#endif
1327
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001328 x = IntHandler = PyDict_GetItemString(d, "default_int_handler");
1329 if (!x)
1330 goto finally;
1331 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +00001332
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001333 _Py_atomic_store_relaxed(&Handlers[0].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001334 for (i = 1; i < NSIG; i++) {
1335 void (*t)(int);
1336 t = PyOS_getsig(i);
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001337 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001338 if (t == SIG_DFL)
1339 Handlers[i].func = DefaultHandler;
1340 else if (t == SIG_IGN)
1341 Handlers[i].func = IgnoreHandler;
1342 else
1343 Handlers[i].func = Py_None; /* None of our business */
1344 Py_INCREF(Handlers[i].func);
1345 }
1346 if (Handlers[SIGINT].func == DefaultHandler) {
1347 /* Install default int handler */
1348 Py_INCREF(IntHandler);
Serhiy Storchaka57a01d32016-04-10 18:05:40 +03001349 Py_SETREF(Handlers[SIGINT].func, IntHandler);
pkerlinge905c842018-06-01 09:47:18 +00001350 PyOS_setsig(SIGINT, signal_handler);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001351 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001352
1353#ifdef SIGHUP
Christian Heimes6782b142016-09-09 00:11:45 +02001354 if (PyModule_AddIntMacro(m, SIGHUP))
1355 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001356#endif
1357#ifdef SIGINT
Christian Heimes6782b142016-09-09 00:11:45 +02001358 if (PyModule_AddIntMacro(m, SIGINT))
1359 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001360#endif
Tim Peters1ce3cf72001-10-01 17:58:40 +00001361#ifdef SIGBREAK
Christian Heimes6782b142016-09-09 00:11:45 +02001362 if (PyModule_AddIntMacro(m, SIGBREAK))
1363 goto finally;
Tim Peters1ce3cf72001-10-01 17:58:40 +00001364#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001365#ifdef SIGQUIT
Christian Heimes6782b142016-09-09 00:11:45 +02001366 if (PyModule_AddIntMacro(m, SIGQUIT))
1367 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001368#endif
1369#ifdef SIGILL
Christian Heimes6782b142016-09-09 00:11:45 +02001370 if (PyModule_AddIntMacro(m, SIGILL))
1371 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001372#endif
1373#ifdef SIGTRAP
Christian Heimes6782b142016-09-09 00:11:45 +02001374 if (PyModule_AddIntMacro(m, SIGTRAP))
1375 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001376#endif
1377#ifdef SIGIOT
Christian Heimes6782b142016-09-09 00:11:45 +02001378 if (PyModule_AddIntMacro(m, SIGIOT))
1379 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001380#endif
1381#ifdef SIGABRT
Christian Heimes6782b142016-09-09 00:11:45 +02001382 if (PyModule_AddIntMacro(m, SIGABRT))
1383 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001384#endif
1385#ifdef SIGEMT
Christian Heimes6782b142016-09-09 00:11:45 +02001386 if (PyModule_AddIntMacro(m, SIGEMT))
1387 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001388#endif
1389#ifdef SIGFPE
Christian Heimes6782b142016-09-09 00:11:45 +02001390 if (PyModule_AddIntMacro(m, SIGFPE))
1391 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001392#endif
1393#ifdef SIGKILL
Christian Heimes6782b142016-09-09 00:11:45 +02001394 if (PyModule_AddIntMacro(m, SIGKILL))
1395 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001396#endif
1397#ifdef SIGBUS
Christian Heimes6782b142016-09-09 00:11:45 +02001398 if (PyModule_AddIntMacro(m, SIGBUS))
1399 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001400#endif
1401#ifdef SIGSEGV
Christian Heimes6782b142016-09-09 00:11:45 +02001402 if (PyModule_AddIntMacro(m, SIGSEGV))
1403 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001404#endif
1405#ifdef SIGSYS
Christian Heimes6782b142016-09-09 00:11:45 +02001406 if (PyModule_AddIntMacro(m, SIGSYS))
1407 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001408#endif
1409#ifdef SIGPIPE
Christian Heimes6782b142016-09-09 00:11:45 +02001410 if (PyModule_AddIntMacro(m, SIGPIPE))
1411 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001412#endif
1413#ifdef SIGALRM
Christian Heimes6782b142016-09-09 00:11:45 +02001414 if (PyModule_AddIntMacro(m, SIGALRM))
1415 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001416#endif
1417#ifdef SIGTERM
Christian Heimes6782b142016-09-09 00:11:45 +02001418 if (PyModule_AddIntMacro(m, SIGTERM))
1419 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001420#endif
1421#ifdef SIGUSR1
Christian Heimes6782b142016-09-09 00:11:45 +02001422 if (PyModule_AddIntMacro(m, SIGUSR1))
1423 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001424#endif
1425#ifdef SIGUSR2
Christian Heimes6782b142016-09-09 00:11:45 +02001426 if (PyModule_AddIntMacro(m, SIGUSR2))
1427 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001428#endif
1429#ifdef SIGCLD
Christian Heimes6782b142016-09-09 00:11:45 +02001430 if (PyModule_AddIntMacro(m, SIGCLD))
1431 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001432#endif
1433#ifdef SIGCHLD
Christian Heimes6782b142016-09-09 00:11:45 +02001434 if (PyModule_AddIntMacro(m, SIGCHLD))
1435 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001436#endif
1437#ifdef SIGPWR
Christian Heimes6782b142016-09-09 00:11:45 +02001438 if (PyModule_AddIntMacro(m, SIGPWR))
1439 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001440#endif
1441#ifdef SIGIO
Christian Heimes6782b142016-09-09 00:11:45 +02001442 if (PyModule_AddIntMacro(m, SIGIO))
1443 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001444#endif
1445#ifdef SIGURG
Christian Heimes6782b142016-09-09 00:11:45 +02001446 if (PyModule_AddIntMacro(m, SIGURG))
1447 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001448#endif
1449#ifdef SIGWINCH
Christian Heimes6782b142016-09-09 00:11:45 +02001450 if (PyModule_AddIntMacro(m, SIGWINCH))
1451 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001452#endif
1453#ifdef SIGPOLL
Christian Heimes6782b142016-09-09 00:11:45 +02001454 if (PyModule_AddIntMacro(m, SIGPOLL))
1455 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001456#endif
1457#ifdef SIGSTOP
Christian Heimes6782b142016-09-09 00:11:45 +02001458 if (PyModule_AddIntMacro(m, SIGSTOP))
1459 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001460#endif
1461#ifdef SIGTSTP
Christian Heimes6782b142016-09-09 00:11:45 +02001462 if (PyModule_AddIntMacro(m, SIGTSTP))
1463 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001464#endif
1465#ifdef SIGCONT
Christian Heimes6782b142016-09-09 00:11:45 +02001466 if (PyModule_AddIntMacro(m, SIGCONT))
1467 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001468#endif
1469#ifdef SIGTTIN
Christian Heimes6782b142016-09-09 00:11:45 +02001470 if (PyModule_AddIntMacro(m, SIGTTIN))
1471 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001472#endif
1473#ifdef SIGTTOU
Christian Heimes6782b142016-09-09 00:11:45 +02001474 if (PyModule_AddIntMacro(m, SIGTTOU))
1475 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001476#endif
1477#ifdef SIGVTALRM
Christian Heimes6782b142016-09-09 00:11:45 +02001478 if (PyModule_AddIntMacro(m, SIGVTALRM))
1479 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001480#endif
1481#ifdef SIGPROF
Christian Heimes6782b142016-09-09 00:11:45 +02001482 if (PyModule_AddIntMacro(m, SIGPROF))
1483 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001484#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001485#ifdef SIGXCPU
Christian Heimes6782b142016-09-09 00:11:45 +02001486 if (PyModule_AddIntMacro(m, SIGXCPU))
1487 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001488#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001489#ifdef SIGXFSZ
Christian Heimes6782b142016-09-09 00:11:45 +02001490 if (PyModule_AddIntMacro(m, SIGXFSZ))
1491 goto finally;
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001492#endif
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001493#ifdef SIGRTMIN
Christian Heimes6782b142016-09-09 00:11:45 +02001494 if (PyModule_AddIntMacro(m, SIGRTMIN))
1495 goto finally;
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001496#endif
1497#ifdef SIGRTMAX
Christian Heimes6782b142016-09-09 00:11:45 +02001498 if (PyModule_AddIntMacro(m, SIGRTMAX))
1499 goto finally;
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001500#endif
Martin v. Löwis175af252002-01-12 11:43:25 +00001501#ifdef SIGINFO
Christian Heimes6782b142016-09-09 00:11:45 +02001502 if (PyModule_AddIntMacro(m, SIGINFO))
1503 goto finally;
Martin v. Löwis175af252002-01-12 11:43:25 +00001504#endif
Martin v. Löwis823725e2008-03-24 13:39:54 +00001505
1506#ifdef ITIMER_REAL
Christian Heimes6782b142016-09-09 00:11:45 +02001507 if (PyModule_AddIntMacro(m, ITIMER_REAL))
1508 goto finally;
Martin v. Löwis823725e2008-03-24 13:39:54 +00001509#endif
1510#ifdef ITIMER_VIRTUAL
Christian Heimes6782b142016-09-09 00:11:45 +02001511 if (PyModule_AddIntMacro(m, ITIMER_VIRTUAL))
1512 goto finally;
Martin v. Löwis823725e2008-03-24 13:39:54 +00001513#endif
1514#ifdef ITIMER_PROF
Christian Heimes6782b142016-09-09 00:11:45 +02001515 if (PyModule_AddIntMacro(m, ITIMER_PROF))
1516 goto finally;
Martin v. Löwis823725e2008-03-24 13:39:54 +00001517#endif
1518
1519#if defined (HAVE_SETITIMER) || defined (HAVE_GETITIMER)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001520 ItimerError = PyErr_NewException("signal.ItimerError",
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +03001521 PyExc_OSError, NULL);
Neal Norwitz2f99b242008-08-24 05:48:10 +00001522 if (ItimerError != NULL)
Martin Panter6d57fe12016-09-17 03:26:16 +00001523 PyDict_SetItemString(d, "ItimerError", ItimerError);
Martin v. Löwis823725e2008-03-24 13:39:54 +00001524#endif
1525
Brian Curtineb24d742010-04-12 17:16:38 +00001526#ifdef CTRL_C_EVENT
Christian Heimes6782b142016-09-09 00:11:45 +02001527 if (PyModule_AddIntMacro(m, CTRL_C_EVENT))
1528 goto finally;
Brian Curtineb24d742010-04-12 17:16:38 +00001529#endif
1530
1531#ifdef CTRL_BREAK_EVENT
Christian Heimes6782b142016-09-09 00:11:45 +02001532 if (PyModule_AddIntMacro(m, CTRL_BREAK_EVENT))
1533 goto finally;
Brian Curtineb24d742010-04-12 17:16:38 +00001534#endif
1535
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001536#ifdef MS_WINDOWS
1537 /* Create manual-reset event, initially unset */
1538 sigint_event = CreateEvent(NULL, TRUE, FALSE, FALSE);
1539#endif
1540
Martin v. Löwis1a214512008-06-11 05:26:20 +00001541 if (PyErr_Occurred()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001542 Py_DECREF(m);
1543 m = NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001544 }
Barry Warsaw92971171997-01-03 00:14:25 +00001545
Barry Warsaw92971171997-01-03 00:14:25 +00001546 finally:
Martin v. Löwis1a214512008-06-11 05:26:20 +00001547 return m;
Guido van Rossum08c16611997-08-02 03:01:42 +00001548}
1549
1550static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001551finisignal(void)
Guido van Rossum08c16611997-08-02 03:01:42 +00001552{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001553 int i;
1554 PyObject *func;
Guido van Rossum08c16611997-08-02 03:01:42 +00001555
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001556 for (i = 1; i < NSIG; i++) {
1557 func = Handlers[i].func;
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001558 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001559 Handlers[i].func = NULL;
pkerlinge905c842018-06-01 09:47:18 +00001560 if (func != NULL && func != Py_None &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001561 func != DefaultHandler && func != IgnoreHandler)
1562 PyOS_setsig(i, SIG_DFL);
1563 Py_XDECREF(func);
1564 }
Guido van Rossum08c16611997-08-02 03:01:42 +00001565
Serhiy Storchaka505ff752014-02-09 13:33:53 +02001566 Py_CLEAR(IntHandler);
1567 Py_CLEAR(DefaultHandler);
1568 Py_CLEAR(IgnoreHandler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001569}
1570
Barry Warsaw92971171997-01-03 00:14:25 +00001571
Barry Warsaw92971171997-01-03 00:14:25 +00001572/* Declared in pyerrors.h */
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001573int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001574PyErr_CheckSignals(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001575{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001576 int i;
1577 PyObject *f;
Barry Warsaw92971171997-01-03 00:14:25 +00001578
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001579 if (!_Py_atomic_load(&is_tripped))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001580 return 0;
Christian Heimesb76922a2007-12-11 01:06:40 +00001581
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001582 if (PyThread_get_thread_ident() != main_thread)
1583 return 0;
Christian Heimesb76922a2007-12-11 01:06:40 +00001584
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001585 /*
1586 * The is_tripped variable is meant to speed up the calls to
1587 * PyErr_CheckSignals (both directly or via pending calls) when no
1588 * signal has arrived. This variable is set to 1 when a signal arrives
1589 * and it is set to 0 here, when we know some signals arrived. This way
1590 * we can run the registered handlers with no signals blocked.
1591 *
1592 * NOTE: with this approach we can have a situation where is_tripped is
1593 * 1 but we have no more signals to handle (Handlers[i].tripped
1594 * is 0 for every signal i). This won't do us any harm (except
1595 * we're gonna spent some cycles for nothing). This happens when
1596 * we receive a signal i after we zero is_tripped and before we
1597 * check Handlers[i].tripped.
1598 */
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001599 _Py_atomic_store(&is_tripped, 0);
Christian Heimesb76922a2007-12-11 01:06:40 +00001600
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001601 if (!(f = (PyObject *)PyEval_GetFrame()))
1602 f = Py_None;
Christian Heimesb76922a2007-12-11 01:06:40 +00001603
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001604 for (i = 1; i < NSIG; i++) {
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001605 if (_Py_atomic_load_relaxed(&Handlers[i].tripped)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001606 PyObject *result = NULL;
1607 PyObject *arglist = Py_BuildValue("(iO)", i, f);
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001608 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Barry Warsaw92971171997-01-03 00:14:25 +00001609
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001610 if (arglist) {
1611 result = PyEval_CallObject(Handlers[i].func,
1612 arglist);
1613 Py_DECREF(arglist);
1614 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +02001615 if (!result) {
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001616 _Py_atomic_store(&is_tripped, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001617 return -1;
Antoine Pitrouc08177a2017-06-28 23:29:29 +02001618 }
Barry Warsaw92971171997-01-03 00:14:25 +00001619
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001620 Py_DECREF(result);
1621 }
1622 }
Christian Heimesb76922a2007-12-11 01:06:40 +00001623
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001624 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001625}
1626
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +00001627
Barry Warsaw92971171997-01-03 00:14:25 +00001628/* Replacements for intrcheck.c functionality
1629 * Declared in pyerrors.h
1630 */
1631void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001632PyErr_SetInterrupt(void)
Barry Warsaw92971171997-01-03 00:14:25 +00001633{
Victor Stinner6c9b35b2011-04-18 16:25:56 +02001634 trip_signal(SIGINT);
Barry Warsaw92971171997-01-03 00:14:25 +00001635}
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001636
1637void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001638PyOS_InitInterrupts(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001639{
Giampaolo Rodola'e09fb712014-04-04 15:34:17 +02001640 PyObject *m = PyImport_ImportModule("_signal");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001641 if (m) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001642 Py_DECREF(m);
1643 }
Guido van Rossum08c16611997-08-02 03:01:42 +00001644}
1645
1646void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001647PyOS_FiniInterrupts(void)
Guido van Rossum08c16611997-08-02 03:01:42 +00001648{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001649 finisignal();
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001650}
1651
1652int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001653PyOS_InterruptOccurred(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001654{
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001655 if (_Py_atomic_load_relaxed(&Handlers[SIGINT].tripped)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001656 if (PyThread_get_thread_ident() != main_thread)
1657 return 0;
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001658 _Py_atomic_store_relaxed(&Handlers[SIGINT].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001659 return 1;
1660 }
1661 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001662}
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001663
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001664static void
1665_clear_pending_signals(void)
1666{
1667 int i;
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001668 if (!_Py_atomic_load(&is_tripped))
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001669 return;
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001670 _Py_atomic_store(&is_tripped, 0);
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001671 for (i = 1; i < NSIG; ++i) {
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001672 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001673 }
1674}
1675
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001676void
Antoine Pitrou346cbd32017-05-27 17:50:54 +02001677_PySignal_AfterFork(void)
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001678{
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001679 /* Clear the signal flags after forking so that they aren't handled
1680 * in both processes if they came in just before the fork() but before
1681 * the interpreter had an opportunity to call the handlers. issue9535. */
1682 _clear_pending_signals();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001683 main_thread = PyThread_get_thread_ident();
1684 main_pid = getpid();
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001685}
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001686
1687int
1688_PyOS_IsMainThread(void)
1689{
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001690 return PyThread_get_thread_ident() == main_thread;
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001691}
1692
1693#ifdef MS_WINDOWS
1694void *_PyOS_SigintEvent(void)
1695{
1696 /* Returns a manual-reset event which gets tripped whenever
1697 SIGINT is received.
1698
1699 Python.h does not include windows.h so we do cannot use HANDLE
1700 as the return type of this function. We use void* instead. */
1701 return sigint_event;
1702}
1703#endif