blob: 35fd87e2d1e711ffc6f4debab4a49950cfe4f9f0 [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
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000062
Guido van Rossumbb4ba121994-06-23 11:25:45 +000063/*
64 NOTES ON THE INTERACTION BETWEEN SIGNALS AND THREADS
65
66 When threads are supported, we want the following semantics:
67
68 - only the main thread can set a signal handler
69 - any thread can get a signal handler
70 - signals are only delivered to the main thread
71
72 I.e. we don't support "synchronous signals" like SIGFPE (catching
73 this doesn't make much sense in Python anyway) nor do we support
74 signals as a means of inter-thread communication, since not all
75 thread implementations support that (at least our thread library
76 doesn't).
77
78 We still have the problem that in some implementations signals
79 generated by the keyboard (e.g. SIGINT) are delivered to all
80 threads (e.g. SGI), while in others (e.g. Solaris) such signals are
81 delivered to one random thread (an intermediate possibility would
Guido van Rossuma3c04b01995-01-12 11:29:01 +000082 be to deliver it to the main thread -- POSIX?). For now, we have
Guido van Rossumbb4ba121994-06-23 11:25:45 +000083 a working implementation that works in all three cases -- the
84 handler ignores signals if getpid() isn't the same as in the main
85 thread. XXX This is a hack.
Guido van Rossumbb4ba121994-06-23 11:25:45 +000086*/
87
Guido van Rossum295b8e51997-06-06 21:16:41 +000088#include <sys/types.h> /* For pid_t */
Guido van Rossum49b56061998-10-01 20:42:43 +000089#include "pythread.h"
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +020090static unsigned long main_thread;
Guido van Rossumbb4ba121994-06-23 11:25:45 +000091static pid_t main_pid;
Guido van Rossumbb4ba121994-06-23 11:25:45 +000092
Victor Stinner2ec6b172011-05-15 10:21:59 +020093static volatile struct {
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +020094 _Py_atomic_int tripped;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000095 PyObject *func;
Barry Warsaw92971171997-01-03 00:14:25 +000096} Handlers[NSIG];
Guido van Rossum398d9fe1994-05-11 08:59:13 +000097
Victor Stinner11517102014-07-29 23:31:34 +020098#ifdef MS_WINDOWS
99#define INVALID_FD ((SOCKET_T)-1)
100
101static volatile struct {
102 SOCKET_T fd;
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800103 int warn_on_full_buffer;
Victor Stinner11517102014-07-29 23:31:34 +0200104 int use_send;
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800105} wakeup = {.fd = INVALID_FD, .warn_on_full_buffer = 1, .use_send = 0};
Victor Stinner11517102014-07-29 23:31:34 +0200106#else
107#define INVALID_FD (-1)
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800108static volatile struct {
109 sig_atomic_t fd;
110 int warn_on_full_buffer;
111} wakeup = {.fd = INVALID_FD, .warn_on_full_buffer = 1};
Victor Stinner11517102014-07-29 23:31:34 +0200112#endif
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000113
Christian Heimesb76922a2007-12-11 01:06:40 +0000114/* Speed up sigcheck() when none tripped */
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +0200115static _Py_atomic_int is_tripped;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000116
Barry Warsaw92971171997-01-03 00:14:25 +0000117static PyObject *DefaultHandler;
118static PyObject *IgnoreHandler;
119static PyObject *IntHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000120
Martin v. Löwisf58de1b2001-03-06 12:13:56 +0000121/* On Solaris 8, gcc will produce a warning that the function
122 declaration is not a prototype. This is caused by the definition of
123 SIG_DFL as (void (*)())0; the correct declaration would have been
124 (void (*)(int))0. */
125
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000126static PyOS_sighandler_t old_siginthandler = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +0000127
Antoine Pitrou6dd381e2011-11-21 21:26:56 +0100128#ifdef MS_WINDOWS
129static HANDLE sigint_event = NULL;
130#endif
131
Martin v. Löwis823725e2008-03-24 13:39:54 +0000132#ifdef HAVE_GETITIMER
133static PyObject *ItimerError;
134
Victor Stinneref611c92017-10-13 13:49:43 -0700135/* auxiliary functions for setitimer */
136static int
137timeval_from_double(PyObject *obj, struct timeval *tv)
Martin v. Löwis823725e2008-03-24 13:39:54 +0000138{
Victor Stinneref611c92017-10-13 13:49:43 -0700139 if (obj == NULL) {
140 tv->tv_sec = 0;
141 tv->tv_usec = 0;
142 return 0;
Antoine Pitrou729780a2017-06-30 10:01:05 +0200143 }
Victor Stinneref611c92017-10-13 13:49:43 -0700144
145 _PyTime_t t;
146 if (_PyTime_FromSecondsObject(&t, obj, _PyTime_ROUND_CEILING) < 0) {
147 return -1;
148 }
149 return _PyTime_AsTimeval(t, tv, _PyTime_ROUND_CEILING);
Martin v. Löwis823725e2008-03-24 13:39:54 +0000150}
151
Christian Heimes1a8501c2008-10-02 19:56:01 +0000152Py_LOCAL_INLINE(double)
Martin v. Löwis823725e2008-03-24 13:39:54 +0000153double_from_timeval(struct timeval *tv)
154{
155 return tv->tv_sec + (double)(tv->tv_usec / 1000000.0);
156}
157
158static PyObject *
159itimer_retval(struct itimerval *iv)
160{
161 PyObject *r, *v;
162
163 r = PyTuple_New(2);
164 if (r == NULL)
Martin Panter6d57fe12016-09-17 03:26:16 +0000165 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000166
167 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_value)))) {
Martin Panter6d57fe12016-09-17 03:26:16 +0000168 Py_DECREF(r);
169 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000170 }
171
172 PyTuple_SET_ITEM(r, 0, v);
173
174 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_interval)))) {
Martin Panter6d57fe12016-09-17 03:26:16 +0000175 Py_DECREF(r);
176 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000177 }
178
179 PyTuple_SET_ITEM(r, 1, v);
180
181 return r;
182}
183#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000184
Guido van Rossume4485b01994-09-07 14:32:49 +0000185static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000186signal_default_int_handler(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000187{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000188 PyErr_SetNone(PyExc_KeyboardInterrupt);
189 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000190}
191
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000192PyDoc_STRVAR(default_int_handler_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000193"default_int_handler(...)\n\
194\n\
Michael W. Hudson24ec2112004-06-17 15:55:53 +0000195The default handler for SIGINT installed by Python.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000196It raises KeyboardInterrupt.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000197
Thomas Wouters0796b002000-07-22 23:49:30 +0000198
199static int
Victor Stinner11517102014-07-29 23:31:34 +0200200report_wakeup_write_error(void *data)
Antoine Pitrou6f6ec372013-08-17 20:27:56 +0200201{
202 int save_errno = errno;
Benjamin Petersonca470632016-09-06 13:47:26 -0700203 errno = (int) (intptr_t) data;
Antoine Pitrou6f6ec372013-08-17 20:27:56 +0200204 PyErr_SetFromErrno(PyExc_OSError);
205 PySys_WriteStderr("Exception ignored when trying to write to the "
206 "signal wakeup fd:\n");
207 PyErr_WriteUnraisable(NULL);
208 errno = save_errno;
209 return 0;
210}
211
Victor Stinner11517102014-07-29 23:31:34 +0200212#ifdef MS_WINDOWS
213static int
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800214report_wakeup_send_error(void* data)
Victor Stinner11517102014-07-29 23:31:34 +0200215{
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800216 /* PyErr_SetExcFromWindowsErr() invokes FormatMessage() which
217 recognizes the error codes used by both GetLastError() and
218 WSAGetLastError */
219 PyErr_SetExcFromWindowsErr(PyExc_OSError, (int) (intptr_t) data);
Victor Stinner11517102014-07-29 23:31:34 +0200220 PySys_WriteStderr("Exception ignored when trying to send to the "
221 "signal wakeup fd:\n");
222 PyErr_WriteUnraisable(NULL);
Victor Stinner11517102014-07-29 23:31:34 +0200223 return 0;
224}
225#endif /* MS_WINDOWS */
226
Tim Peters4f1b2082000-07-23 21:18:09 +0000227static void
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200228trip_signal(int sig_num)
229{
Victor Stinnerd49b1f12011-05-08 02:03:15 +0200230 unsigned char byte;
Victor Stinner11517102014-07-29 23:31:34 +0200231 int fd;
232 Py_ssize_t rc;
Victor Stinnerc13ef662011-05-25 02:35:58 +0200233
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +0200234 _Py_atomic_store_relaxed(&Handlers[sig_num].tripped, 1);
Victor Stinner11517102014-07-29 23:31:34 +0200235
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200236 /* Set is_tripped after setting .tripped, as it gets
237 cleared in PyErr_CheckSignals() before .tripped. */
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +0200238 _Py_atomic_store(&is_tripped, 1);
239
240 /* Notify ceval.c */
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200241 _PyEval_SignalReceived();
Nathaniel J. Smith4ae01492017-05-16 14:12:11 -0700242
243 /* And then write to the wakeup fd *after* setting all the globals and
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200244 doing the _PyEval_SignalReceived. We used to write to the wakeup fd
245 and then set the flag, but this allowed the following sequence of events
246 (especially on windows, where trip_signal may run in a new thread):
Nathaniel J. Smith4ae01492017-05-16 14:12:11 -0700247
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800248 - main thread blocks on select([wakeup.fd], ...)
Nathaniel J. Smith4ae01492017-05-16 14:12:11 -0700249 - signal arrives
250 - trip_signal writes to the wakeup fd
251 - the main thread wakes up
252 - the main thread checks the signal flags, sees that they're unset
253 - the main thread empties the wakeup fd
254 - the main thread goes back to sleep
255 - trip_signal sets the flags to request the Python-level signal handler
256 be run
257 - the main thread doesn't notice, because it's asleep
258
259 See bpo-30038 for more details.
260 */
261
Victor Stinner11517102014-07-29 23:31:34 +0200262#ifdef MS_WINDOWS
263 fd = Py_SAFE_DOWNCAST(wakeup.fd, SOCKET_T, int);
264#else
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800265 fd = wakeup.fd;
Victor Stinner11517102014-07-29 23:31:34 +0200266#endif
267
268 if (fd != INVALID_FD) {
Victor Stinnerc13ef662011-05-25 02:35:58 +0200269 byte = (unsigned char)sig_num;
Victor Stinner11517102014-07-29 23:31:34 +0200270#ifdef MS_WINDOWS
271 if (wakeup.use_send) {
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800272 rc = send(fd, &byte, 1, 0);
Victor Stinner11517102014-07-29 23:31:34 +0200273
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800274 if (rc < 0) {
275 int last_error = GetLastError();
276 if (wakeup.warn_on_full_buffer ||
277 last_error != WSAEWOULDBLOCK)
278 {
279 /* Py_AddPendingCall() isn't signal-safe, but we
280 still use it for this exceptional case. */
281 Py_AddPendingCall(report_wakeup_send_error,
282 (void *)(intptr_t) last_error);
283 }
Victor Stinner11517102014-07-29 23:31:34 +0200284 }
285 }
286 else
287#endif
288 {
Victor Stinnere72fe392015-04-01 18:35:22 +0200289 /* _Py_write_noraise() retries write() if write() is interrupted by
290 a signal (fails with EINTR). */
291 rc = _Py_write_noraise(fd, &byte, 1);
Victor Stinner11517102014-07-29 23:31:34 +0200292
293 if (rc < 0) {
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800294 if (wakeup.warn_on_full_buffer ||
295 (errno != EWOULDBLOCK && errno != EAGAIN))
296 {
297 /* Py_AddPendingCall() isn't signal-safe, but we
298 still use it for this exceptional case. */
299 Py_AddPendingCall(report_wakeup_write_error,
300 (void *)(intptr_t)errno);
301 }
Victor Stinner11517102014-07-29 23:31:34 +0200302 }
303 }
Victor Stinnerc13ef662011-05-25 02:35:58 +0200304 }
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200305}
306
307static void
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000308signal_handler(int sig_num)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000309{
Antoine Pitrou39a65912010-11-05 19:47:27 +0000310 int save_errno = errno;
311
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000312 /* See NOTES section above */
Antoine Pitrou39a65912010-11-05 19:47:27 +0000313 if (getpid() == main_pid)
Antoine Pitrou39a65912010-11-05 19:47:27 +0000314 {
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200315 trip_signal(sig_num);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000316 }
Antoine Pitrou39a65912010-11-05 19:47:27 +0000317
Jean-Paul Calderone6f137ca2010-05-09 03:18:57 +0000318#ifndef HAVE_SIGACTION
Antoine Pitrou39a65912010-11-05 19:47:27 +0000319#ifdef SIGCHLD
320 /* To avoid infinite recursion, this signal remains
321 reset until explicit re-instated.
322 Don't clear the 'func' field as it is our pointer
323 to the Python handler... */
324 if (sig_num != SIGCHLD)
325#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000326 /* If the handler was not set up with sigaction, reinstall it. See
Nick Coghland6009512014-11-20 21:39:37 +1000327 * Python/pylifecycle.c for the implementation of PyOS_setsig which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000328 * makes this true. See also issue8354. */
329 PyOS_setsig(sig_num, signal_handler);
Jean-Paul Calderone6f137ca2010-05-09 03:18:57 +0000330#endif
Antoine Pitrou39a65912010-11-05 19:47:27 +0000331
332 /* Issue #10311: asynchronously executing signal handlers should not
333 mutate errno under the feet of unsuspecting C code. */
334 errno = save_errno;
Antoine Pitrou6dd381e2011-11-21 21:26:56 +0100335
336#ifdef MS_WINDOWS
337 if (sig_num == SIGINT)
338 SetEvent(sigint_event);
339#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000340}
Guido van Rossume4485b01994-09-07 14:32:49 +0000341
Guido van Rossum06d511d1995-03-10 15:13:48 +0000342
Guido van Rossum1171ee61997-08-22 20:42:00 +0000343#ifdef HAVE_ALARM
Tal Einatc7027b72015-05-16 14:14:49 +0300344
345/*[clinic input]
346signal.alarm -> long
347
348 seconds: int
349 /
350
351Arrange for SIGALRM to arrive after the given number of seconds.
352[clinic start generated code]*/
353
354static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300355signal_alarm_impl(PyObject *module, int seconds)
356/*[clinic end generated code: output=144232290814c298 input=0d5e97e0e6f39e86]*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000357{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000358 /* alarm() returns the number of seconds remaining */
Tal Einatc7027b72015-05-16 14:14:49 +0300359 return (long)alarm(seconds);
Guido van Rossumaa0f4c71994-08-23 13:49:37 +0000360}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000361
Guido van Rossum06d511d1995-03-10 15:13:48 +0000362#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000363
Guido van Rossum1171ee61997-08-22 20:42:00 +0000364#ifdef HAVE_PAUSE
Tal Einatc7027b72015-05-16 14:14:49 +0300365
366/*[clinic input]
367signal.pause
368
369Wait until a signal arrives.
370[clinic start generated code]*/
371
Guido van Rossuma597dde1995-01-10 20:56:29 +0000372static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300373signal_pause_impl(PyObject *module)
374/*[clinic end generated code: output=391656788b3c3929 input=f03de0f875752062]*/
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000375{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000376 Py_BEGIN_ALLOW_THREADS
377 (void)pause();
378 Py_END_ALLOW_THREADS
379 /* make sure that any exceptions that got raised are propagated
380 * back into Python
381 */
382 if (PyErr_CheckSignals())
383 return NULL;
Barry Warsaw92971171997-01-03 00:14:25 +0000384
Tal Einatc7027b72015-05-16 14:14:49 +0300385 Py_RETURN_NONE;
Guido van Rossume4485b01994-09-07 14:32:49 +0000386}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000387
Guido van Rossum06d511d1995-03-10 15:13:48 +0000388#endif
Guido van Rossume4485b01994-09-07 14:32:49 +0000389
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000390
Tal Einatc7027b72015-05-16 14:14:49 +0300391/*[clinic input]
392signal.signal
393
394 signalnum: int
395 handler: object
396 /
397
398Set the action for the given signal.
399
400The action can be SIG_DFL, SIG_IGN, or a callable Python object.
401The previous action is returned. See getsignal() for possible return values.
402
403*** IMPORTANT NOTICE ***
404A signal handler function is called with two arguments:
405the first is the signal number, the second is the interrupted stack frame.
406[clinic start generated code]*/
407
Guido van Rossume4485b01994-09-07 14:32:49 +0000408static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300409signal_signal_impl(PyObject *module, int signalnum, PyObject *handler)
410/*[clinic end generated code: output=b44cfda43780f3a1 input=deee84af5fa0432c]*/
Guido van Rossume4485b01994-09-07 14:32:49 +0000411{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000412 PyObject *old_handler;
413 void (*func)(int);
Brian Curtinef9efbd2010-08-06 19:27:32 +0000414#ifdef MS_WINDOWS
Tal Einatc7027b72015-05-16 14:14:49 +0300415 /* Validate that signalnum is one of the allowable signals */
416 switch (signalnum) {
Brian Curtinc734b312010-09-06 16:04:10 +0000417 case SIGABRT: break;
Brian Curtin9e88b5a2010-10-01 14:49:24 +0000418#ifdef SIGBREAK
419 /* Issue #10003: SIGBREAK is not documented as permitted, but works
420 and corresponds to CTRL_BREAK_EVENT. */
421 case SIGBREAK: break;
422#endif
Brian Curtinc734b312010-09-06 16:04:10 +0000423 case SIGFPE: break;
424 case SIGILL: break;
425 case SIGINT: break;
426 case SIGSEGV: break;
427 case SIGTERM: break;
428 default:
429 PyErr_SetString(PyExc_ValueError, "invalid signal value");
430 return NULL;
Brian Curtinef9efbd2010-08-06 19:27:32 +0000431 }
432#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000433 if (PyThread_get_thread_ident() != main_thread) {
434 PyErr_SetString(PyExc_ValueError,
435 "signal only works in main thread");
436 return NULL;
437 }
Tal Einatc7027b72015-05-16 14:14:49 +0300438 if (signalnum < 1 || signalnum >= NSIG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000439 PyErr_SetString(PyExc_ValueError,
440 "signal number out of range");
441 return NULL;
442 }
Tal Einatc7027b72015-05-16 14:14:49 +0300443 if (handler == IgnoreHandler)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000444 func = SIG_IGN;
Tal Einatc7027b72015-05-16 14:14:49 +0300445 else if (handler == DefaultHandler)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000446 func = SIG_DFL;
Tal Einatc7027b72015-05-16 14:14:49 +0300447 else if (!PyCallable_Check(handler)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000448 PyErr_SetString(PyExc_TypeError,
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000449"signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000450 return NULL;
451 }
452 else
453 func = signal_handler;
Antoine Pitrouf6f90ff2017-11-03 19:58:46 +0100454 /* Check for pending signals before changing signal handler */
455 if (PyErr_CheckSignals()) {
456 return NULL;
457 }
Tal Einatc7027b72015-05-16 14:14:49 +0300458 if (PyOS_setsig(signalnum, func) == SIG_ERR) {
Victor Stinner388196e2011-05-10 17:13:00 +0200459 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000460 return NULL;
461 }
Tal Einatc7027b72015-05-16 14:14:49 +0300462 old_handler = Handlers[signalnum].func;
Tal Einatc7027b72015-05-16 14:14:49 +0300463 Py_INCREF(handler);
464 Handlers[signalnum].func = handler;
Antoine Pitrouc8c952c2013-05-04 23:16:59 +0200465 if (old_handler != NULL)
466 return old_handler;
467 else
468 Py_RETURN_NONE;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000469}
470
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000471
Tal Einatc7027b72015-05-16 14:14:49 +0300472/*[clinic input]
473signal.getsignal
474
475 signalnum: int
476 /
477
478Return the current action for the given signal.
479
480The return value can be:
481 SIG_IGN -- if the signal is being ignored
482 SIG_DFL -- if the default action for the signal is in effect
483 None -- if an unknown handler is in effect
484 anything else -- the callable Python object used as a handler
485[clinic start generated code]*/
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000486
Guido van Rossume4485b01994-09-07 14:32:49 +0000487static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300488signal_getsignal_impl(PyObject *module, int signalnum)
489/*[clinic end generated code: output=35b3e0e796fd555e input=ac23a00f19dfa509]*/
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000490{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000491 PyObject *old_handler;
Tal Einatc7027b72015-05-16 14:14:49 +0300492 if (signalnum < 1 || signalnum >= NSIG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000493 PyErr_SetString(PyExc_ValueError,
494 "signal number out of range");
495 return NULL;
496 }
Tal Einatc7027b72015-05-16 14:14:49 +0300497 old_handler = Handlers[signalnum].func;
Antoine Pitrouc8c952c2013-05-04 23:16:59 +0200498 if (old_handler != NULL) {
499 Py_INCREF(old_handler);
500 return old_handler;
501 }
502 else {
503 Py_RETURN_NONE;
504 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000505}
506
Antoine Pietri5d2a27d2018-03-12 14:42:34 +0100507
508/*[clinic input]
509signal.strsignal
510
511 signalnum: int
512 /
513
514Return the system description of the given signal.
515
516The return values can be such as "Interrupt", "Segmentation fault", etc.
517Returns None if the signal is not recognized.
518[clinic start generated code]*/
519
520static PyObject *
521signal_strsignal_impl(PyObject *module, int signalnum)
522/*[clinic end generated code: output=44e12e1e3b666261 input=b77914b03f856c74]*/
523{
524 char *res;
525
526 if (signalnum < 1 || signalnum >= NSIG) {
527 PyErr_SetString(PyExc_ValueError,
528 "signal number out of range");
529 return NULL;
530 }
531
532#ifdef MS_WINDOWS
533 /* Custom redefinition of POSIX signals allowed on Windows */
534 switch (signalnum) {
535 case SIGINT:
536 res = "Interrupt";
537 break;
538 case SIGILL:
539 res = "Illegal instruction";
540 break;
541 case SIGABRT:
542 res = "Aborted";
543 break;
544 case SIGFPE:
545 res = "Floating point exception";
546 break;
547 case SIGSEGV:
548 res = "Segmentation fault";
549 break;
550 case SIGTERM:
551 res = "Terminated";
552 break;
553 default:
554 Py_RETURN_NONE;
555 }
556#else
557 errno = 0;
558 res = strsignal(signalnum);
559
560 if (errno || res == NULL || strstr(res, "Unknown signal") != NULL)
561 Py_RETURN_NONE;
562#endif
563
564 return Py_BuildValue("s", res);
565}
566
Christian Heimes8640e742008-02-23 16:23:06 +0000567#ifdef HAVE_SIGINTERRUPT
Tal Einatc7027b72015-05-16 14:14:49 +0300568
569/*[clinic input]
570signal.siginterrupt
571
572 signalnum: int
573 flag: int
574 /
575
576Change system call restart behaviour.
577
578If flag is False, system calls will be restarted when interrupted by
579signal sig, else system calls will be interrupted.
580[clinic start generated code]*/
Christian Heimes8640e742008-02-23 16:23:06 +0000581
582static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300583signal_siginterrupt_impl(PyObject *module, int signalnum, int flag)
584/*[clinic end generated code: output=063816243d85dd19 input=4160acacca3e2099]*/
Christian Heimes8640e742008-02-23 16:23:06 +0000585{
Tal Einatc7027b72015-05-16 14:14:49 +0300586 if (signalnum < 1 || signalnum >= NSIG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000587 PyErr_SetString(PyExc_ValueError,
588 "signal number out of range");
589 return NULL;
590 }
Tal Einatc7027b72015-05-16 14:14:49 +0300591 if (siginterrupt(signalnum, flag)<0) {
Victor Stinner388196e2011-05-10 17:13:00 +0200592 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000593 return NULL;
594 }
Tal Einatc7027b72015-05-16 14:14:49 +0300595 Py_RETURN_NONE;
Christian Heimes8640e742008-02-23 16:23:06 +0000596}
597
598#endif
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000599
Tal Einatc7027b72015-05-16 14:14:49 +0300600
601static PyObject*
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800602signal_set_wakeup_fd(PyObject *self, PyObject *args, PyObject *kwds)
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000603{
Victor Stinnere134a7f2015-03-30 10:09:31 +0200604 struct _Py_stat_struct status;
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800605 static char *kwlist[] = {
606 "", "warn_on_full_buffer", NULL,
607 };
608 int warn_on_full_buffer = 1;
Victor Stinner11517102014-07-29 23:31:34 +0200609#ifdef MS_WINDOWS
610 PyObject *fdobj;
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100611 SOCKET_T sockfd, old_sockfd;
Victor Stinner11517102014-07-29 23:31:34 +0200612 int res;
613 int res_size = sizeof res;
614 PyObject *mod;
Victor Stinner11517102014-07-29 23:31:34 +0200615 int is_socket;
616
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800617 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|$p:set_wakeup_fd", kwlist,
618 &fdobj, &warn_on_full_buffer))
Victor Stinner11517102014-07-29 23:31:34 +0200619 return NULL;
620
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100621 sockfd = PyLong_AsSocket_t(fdobj);
622 if (sockfd == (SOCKET_T)(-1) && PyErr_Occurred())
Victor Stinner11517102014-07-29 23:31:34 +0200623 return NULL;
624#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000625 int fd, old_fd;
Victor Stinner11517102014-07-29 23:31:34 +0200626
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800627 if (!PyArg_ParseTupleAndKeywords(args, kwds, "i|$p:set_wakeup_fd", kwlist,
628 &fd, &warn_on_full_buffer))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000629 return NULL;
Victor Stinner11517102014-07-29 23:31:34 +0200630#endif
631
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000632 if (PyThread_get_thread_ident() != main_thread) {
633 PyErr_SetString(PyExc_ValueError,
634 "set_wakeup_fd only works in main thread");
635 return NULL;
636 }
Victor Stinner0bffc942014-07-21 16:28:54 +0200637
Victor Stinner11517102014-07-29 23:31:34 +0200638#ifdef MS_WINDOWS
639 is_socket = 0;
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100640 if (sockfd != INVALID_FD) {
Victor Stinner11517102014-07-29 23:31:34 +0200641 /* Import the _socket module to call WSAStartup() */
642 mod = PyImport_ImportModuleNoBlock("_socket");
643 if (mod == NULL)
644 return NULL;
645 Py_DECREF(mod);
646
647 /* test the socket */
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100648 if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR,
Victor Stinner11517102014-07-29 23:31:34 +0200649 (char *)&res, &res_size) != 0) {
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100650 int fd, err;
651
652 err = WSAGetLastError();
Victor Stinner11517102014-07-29 23:31:34 +0200653 if (err != WSAENOTSOCK) {
654 PyErr_SetExcFromWindowsErr(PyExc_OSError, err);
655 return NULL;
656 }
657
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100658 fd = (int)sockfd;
Steve Dower940f33a2016-09-08 11:21:54 -0700659 if ((SOCKET_T)fd != sockfd) {
Victor Stinner11517102014-07-29 23:31:34 +0200660 PyErr_SetString(PyExc_ValueError, "invalid fd");
661 return NULL;
662 }
663
Victor Stinnere134a7f2015-03-30 10:09:31 +0200664 if (_Py_fstat(fd, &status) != 0)
Victor Stinner11517102014-07-29 23:31:34 +0200665 return NULL;
Victor Stinner38227602014-08-27 12:59:44 +0200666
667 /* on Windows, a file cannot be set to non-blocking mode */
Victor Stinner11517102014-07-29 23:31:34 +0200668 }
Victor Stinner38227602014-08-27 12:59:44 +0200669 else {
Victor Stinner11517102014-07-29 23:31:34 +0200670 is_socket = 1;
Victor Stinner38227602014-08-27 12:59:44 +0200671
672 /* Windows does not provide a function to test if a socket
673 is in non-blocking mode */
674 }
Victor Stinner11517102014-07-29 23:31:34 +0200675 }
676
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100677 old_sockfd = wakeup.fd;
678 wakeup.fd = sockfd;
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800679 wakeup.warn_on_full_buffer = warn_on_full_buffer;
Victor Stinner11517102014-07-29 23:31:34 +0200680 wakeup.use_send = is_socket;
681
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100682 if (old_sockfd != INVALID_FD)
683 return PyLong_FromSocket_t(old_sockfd);
Victor Stinner11517102014-07-29 23:31:34 +0200684 else
685 return PyLong_FromLong(-1);
686#else
Victor Stinnerd18ccd12014-07-24 21:58:53 +0200687 if (fd != -1) {
Victor Stinner38227602014-08-27 12:59:44 +0200688 int blocking;
689
Victor Stinnere134a7f2015-03-30 10:09:31 +0200690 if (_Py_fstat(fd, &status) != 0)
Victor Stinner11517102014-07-29 23:31:34 +0200691 return NULL;
Victor Stinner38227602014-08-27 12:59:44 +0200692
693 blocking = _Py_get_blocking(fd);
694 if (blocking < 0)
695 return NULL;
696 if (blocking) {
697 PyErr_Format(PyExc_ValueError,
698 "the fd %i must be in non-blocking mode",
699 fd);
700 return NULL;
701 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000702 }
Victor Stinner0bffc942014-07-21 16:28:54 +0200703
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800704 old_fd = wakeup.fd;
705 wakeup.fd = fd;
706 wakeup.warn_on_full_buffer = warn_on_full_buffer;
Victor Stinner0bffc942014-07-21 16:28:54 +0200707
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000708 return PyLong_FromLong(old_fd);
Victor Stinner11517102014-07-29 23:31:34 +0200709#endif
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000710}
711
712PyDoc_STRVAR(set_wakeup_fd_doc,
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800713"set_wakeup_fd(fd, *, warn_on_full_buffer=True) -> fd\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000714\n\
Victor Stinner11517102014-07-29 23:31:34 +0200715Sets the fd to be written to (with the signal number) when a signal\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000716comes in. A library can use this to wakeup select or poll.\n\
Victor Stinner11517102014-07-29 23:31:34 +0200717The previous fd or -1 is returned.\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000718\n\
719The fd must be non-blocking.");
720
721/* C API for the same, without all the error checking */
722int
723PySignal_SetWakeupFd(int fd)
724{
Victor Stinner11517102014-07-29 23:31:34 +0200725 int old_fd;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000726 if (fd < 0)
727 fd = -1;
Victor Stinner11517102014-07-29 23:31:34 +0200728
729#ifdef MS_WINDOWS
730 old_fd = Py_SAFE_DOWNCAST(wakeup.fd, SOCKET_T, int);
Victor Stinner11517102014-07-29 23:31:34 +0200731#else
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800732 old_fd = wakeup.fd;
Victor Stinner11517102014-07-29 23:31:34 +0200733#endif
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800734 wakeup.fd = fd;
735 wakeup.warn_on_full_buffer = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000736 return old_fd;
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000737}
738
739
Martin v. Löwis823725e2008-03-24 13:39:54 +0000740#ifdef HAVE_SETITIMER
Tal Einatc7027b72015-05-16 14:14:49 +0300741
742/*[clinic input]
743signal.setitimer
744
745 which: int
Victor Stinneref611c92017-10-13 13:49:43 -0700746 seconds: object
747 interval: object(c_default="NULL") = 0.0
Tal Einatc7027b72015-05-16 14:14:49 +0300748 /
749
750Sets given itimer (one of ITIMER_REAL, ITIMER_VIRTUAL or ITIMER_PROF).
751
752The timer will fire after value seconds and after that every interval seconds.
753The itimer can be cleared by setting seconds to zero.
754
755Returns old values as a tuple: (delay, interval).
756[clinic start generated code]*/
757
Martin v. Löwis823725e2008-03-24 13:39:54 +0000758static PyObject *
Victor Stinneref611c92017-10-13 13:49:43 -0700759signal_setitimer_impl(PyObject *module, int which, PyObject *seconds,
760 PyObject *interval)
761/*[clinic end generated code: output=65f9dcbddc35527b input=de43daf194e6f66f]*/
Martin v. Löwis823725e2008-03-24 13:39:54 +0000762{
Martin v. Löwis823725e2008-03-24 13:39:54 +0000763 struct itimerval new, old;
764
Victor Stinneref611c92017-10-13 13:49:43 -0700765 if (timeval_from_double(seconds, &new.it_value) < 0) {
766 return NULL;
767 }
768 if (timeval_from_double(interval, &new.it_interval) < 0) {
769 return NULL;
770 }
771
Martin v. Löwis823725e2008-03-24 13:39:54 +0000772 /* Let OS check "which" value */
773 if (setitimer(which, &new, &old) != 0) {
Tal Einatc7027b72015-05-16 14:14:49 +0300774 PyErr_SetFromErrno(ItimerError);
775 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000776 }
777
778 return itimer_retval(&old);
779}
780
Martin v. Löwis823725e2008-03-24 13:39:54 +0000781#endif
782
783
784#ifdef HAVE_GETITIMER
Tal Einatc7027b72015-05-16 14:14:49 +0300785
786/*[clinic input]
787signal.getitimer
788
789 which: int
790 /
791
792Returns current value of given itimer.
793[clinic start generated code]*/
794
Martin v. Löwis823725e2008-03-24 13:39:54 +0000795static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300796signal_getitimer_impl(PyObject *module, int which)
797/*[clinic end generated code: output=9e053175d517db40 input=f7d21d38f3490627]*/
Martin v. Löwis823725e2008-03-24 13:39:54 +0000798{
Martin v. Löwis823725e2008-03-24 13:39:54 +0000799 struct itimerval old;
800
Martin v. Löwis823725e2008-03-24 13:39:54 +0000801 if (getitimer(which, &old) != 0) {
Tal Einatc7027b72015-05-16 14:14:49 +0300802 PyErr_SetFromErrno(ItimerError);
803 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000804 }
805
806 return itimer_retval(&old);
807}
808
Martin v. Löwis823725e2008-03-24 13:39:54 +0000809#endif
810
Ross Lagerwallbc808222011-06-25 12:13:40 +0200811#if defined(PYPTHREAD_SIGMASK) || defined(HAVE_SIGWAIT) || \
812 defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
Victor Stinnera9293352011-04-30 15:21:58 +0200813/* Convert an iterable to a sigset.
814 Return 0 on success, return -1 and raise an exception on error. */
815
816static int
817iterable_to_sigset(PyObject *iterable, sigset_t *mask)
818{
819 int result = -1;
820 PyObject *iterator, *item;
821 long signum;
Victor Stinnera9293352011-04-30 15:21:58 +0200822
823 sigemptyset(mask);
824
825 iterator = PyObject_GetIter(iterable);
826 if (iterator == NULL)
827 goto error;
828
829 while (1)
830 {
831 item = PyIter_Next(iterator);
832 if (item == NULL) {
833 if (PyErr_Occurred())
834 goto error;
835 else
836 break;
837 }
838
839 signum = PyLong_AsLong(item);
840 Py_DECREF(item);
841 if (signum == -1 && PyErr_Occurred())
842 goto error;
Antoine Pitrou25038ec2018-04-23 20:53:33 +0200843 if (0 < signum && signum < NSIG) {
844 /* bpo-33329: ignore sigaddset() return value as it can fail
845 * for some reserved signals, but we want the `range(1, NSIG)`
846 * idiom to allow selecting all valid signals.
847 */
848 (void) sigaddset(mask, (int)signum);
849 }
850 else {
Victor Stinnera9293352011-04-30 15:21:58 +0200851 PyErr_Format(PyExc_ValueError,
852 "signal number %ld out of range", signum);
853 goto error;
854 }
855 }
856 result = 0;
857
858error:
859 Py_XDECREF(iterator);
860 return result;
861}
Victor Stinnerb3e72192011-05-08 01:46:11 +0200862#endif
Victor Stinnera9293352011-04-30 15:21:58 +0200863
Victor Stinnerb3e72192011-05-08 01:46:11 +0200864#if defined(PYPTHREAD_SIGMASK) || defined(HAVE_SIGPENDING)
Victor Stinner35b300c2011-05-04 13:20:35 +0200865static PyObject*
866sigset_to_set(sigset_t mask)
867{
868 PyObject *signum, *result;
869 int sig;
870
871 result = PySet_New(0);
872 if (result == NULL)
873 return NULL;
874
875 for (sig = 1; sig < NSIG; sig++) {
876 if (sigismember(&mask, sig) != 1)
877 continue;
878
879 /* Handle the case where it is a member by adding the signal to
880 the result list. Ignore the other cases because they mean the
881 signal isn't a member of the mask or the signal was invalid,
882 and an invalid signal must have been our fault in constructing
883 the loop boundaries. */
884 signum = PyLong_FromLong(sig);
885 if (signum == NULL) {
886 Py_DECREF(result);
887 return NULL;
888 }
889 if (PySet_Add(result, signum) == -1) {
890 Py_DECREF(signum);
891 Py_DECREF(result);
892 return NULL;
893 }
894 Py_DECREF(signum);
895 }
896 return result;
897}
Victor Stinnerb3e72192011-05-08 01:46:11 +0200898#endif
Victor Stinner35b300c2011-05-04 13:20:35 +0200899
Victor Stinnerb3e72192011-05-08 01:46:11 +0200900#ifdef PYPTHREAD_SIGMASK
Tal Einatc7027b72015-05-16 14:14:49 +0300901
902/*[clinic input]
903signal.pthread_sigmask
904
905 how: int
906 mask: object
907 /
908
909Fetch and/or change the signal mask of the calling thread.
910[clinic start generated code]*/
911
Victor Stinnera9293352011-04-30 15:21:58 +0200912static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300913signal_pthread_sigmask_impl(PyObject *module, int how, PyObject *mask)
914/*[clinic end generated code: output=ff640fe092bc9181 input=f3b7d7a61b7b8283]*/
Victor Stinnera9293352011-04-30 15:21:58 +0200915{
Tal Einatc7027b72015-05-16 14:14:49 +0300916 sigset_t newmask, previous;
Victor Stinnera9293352011-04-30 15:21:58 +0200917 int err;
918
Tal Einatc7027b72015-05-16 14:14:49 +0300919 if (iterable_to_sigset(mask, &newmask))
Victor Stinnera9293352011-04-30 15:21:58 +0200920 return NULL;
921
Tal Einatc7027b72015-05-16 14:14:49 +0300922 err = pthread_sigmask(how, &newmask, &previous);
Victor Stinnera9293352011-04-30 15:21:58 +0200923 if (err != 0) {
924 errno = err;
Victor Stinnerb3e72192011-05-08 01:46:11 +0200925 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnera9293352011-04-30 15:21:58 +0200926 return NULL;
927 }
928
Victor Stinnerd0e516d2011-05-03 14:57:12 +0200929 /* if signals was unblocked, signal handlers have been called */
930 if (PyErr_CheckSignals())
931 return NULL;
932
Victor Stinner35b300c2011-05-04 13:20:35 +0200933 return sigset_to_set(previous);
Victor Stinnera9293352011-04-30 15:21:58 +0200934}
935
Victor Stinnera9293352011-04-30 15:21:58 +0200936#endif /* #ifdef PYPTHREAD_SIGMASK */
937
Martin v. Löwis823725e2008-03-24 13:39:54 +0000938
Victor Stinnerb3e72192011-05-08 01:46:11 +0200939#ifdef HAVE_SIGPENDING
Tal Einatc7027b72015-05-16 14:14:49 +0300940
941/*[clinic input]
942signal.sigpending
943
944Examine pending signals.
945
946Returns a set of signal numbers that are pending for delivery to
947the calling thread.
948[clinic start generated code]*/
949
Victor Stinnerb3e72192011-05-08 01:46:11 +0200950static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300951signal_sigpending_impl(PyObject *module)
952/*[clinic end generated code: output=53375ffe89325022 input=e0036c016f874e29]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +0200953{
954 int err;
955 sigset_t mask;
956 err = sigpending(&mask);
957 if (err)
958 return PyErr_SetFromErrno(PyExc_OSError);
959 return sigset_to_set(mask);
960}
961
Victor Stinnerb3e72192011-05-08 01:46:11 +0200962#endif /* #ifdef HAVE_SIGPENDING */
963
964
965#ifdef HAVE_SIGWAIT
Tal Einatc7027b72015-05-16 14:14:49 +0300966
967/*[clinic input]
968signal.sigwait
969
970 sigset: object
971 /
972
973Wait for a signal.
974
975Suspend execution of the calling thread until the delivery of one of the
976signals specified in the signal set sigset. The function accepts the signal
977and returns the signal number.
978[clinic start generated code]*/
979
Victor Stinnerb3e72192011-05-08 01:46:11 +0200980static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300981signal_sigwait(PyObject *module, PyObject *sigset)
982/*[clinic end generated code: output=557173647424f6e4 input=11af2d82d83c2e94]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +0200983{
Victor Stinnerb3e72192011-05-08 01:46:11 +0200984 sigset_t set;
985 int err, signum;
986
Tal Einatc7027b72015-05-16 14:14:49 +0300987 if (iterable_to_sigset(sigset, &set))
Victor Stinnerb3e72192011-05-08 01:46:11 +0200988 return NULL;
989
Victor Stinner10c30d62011-06-10 01:39:53 +0200990 Py_BEGIN_ALLOW_THREADS
Victor Stinnerb3e72192011-05-08 01:46:11 +0200991 err = sigwait(&set, &signum);
Victor Stinner10c30d62011-06-10 01:39:53 +0200992 Py_END_ALLOW_THREADS
Victor Stinnerb3e72192011-05-08 01:46:11 +0200993 if (err) {
994 errno = err;
995 return PyErr_SetFromErrno(PyExc_OSError);
996 }
997
998 return PyLong_FromLong(signum);
999}
1000
Tal Einatc7027b72015-05-16 14:14:49 +03001001#endif /* #ifdef HAVE_SIGWAIT */
1002
Victor Stinnerb3e72192011-05-08 01:46:11 +02001003
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
1065 sigset: object
1066 /
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 Storchaka1a2b24f2016-07-07 17:35:15 +03001074signal_sigwaitinfo(PyObject *module, PyObject *sigset)
1075/*[clinic end generated code: output=c40f27b269cd2309 input=f3779a74a991e171]*/
Ross Lagerwallbc808222011-06-25 12:13:40 +02001076{
Ross Lagerwallbc808222011-06-25 12:13:40 +02001077 sigset_t set;
1078 siginfo_t si;
1079 int err;
Victor Stinnera453cd82015-03-20 12:54:28 +01001080 int async_err = 0;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001081
Tal Einatc7027b72015-05-16 14:14:49 +03001082 if (iterable_to_sigset(sigset, &set))
Ross Lagerwallbc808222011-06-25 12:13:40 +02001083 return NULL;
1084
Victor Stinnera453cd82015-03-20 12:54:28 +01001085 do {
1086 Py_BEGIN_ALLOW_THREADS
1087 err = sigwaitinfo(&set, &si);
1088 Py_END_ALLOW_THREADS
1089 } while (err == -1
1090 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Ross Lagerwallbc808222011-06-25 12:13:40 +02001091 if (err == -1)
Victor Stinnera453cd82015-03-20 12:54:28 +01001092 return (!async_err) ? PyErr_SetFromErrno(PyExc_OSError) : NULL;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001093
1094 return fill_siginfo(&si);
1095}
1096
Ross Lagerwallbc808222011-06-25 12:13:40 +02001097#endif /* #ifdef HAVE_SIGWAITINFO */
1098
1099#ifdef HAVE_SIGTIMEDWAIT
Tal Einatc7027b72015-05-16 14:14:49 +03001100
1101/*[clinic input]
1102signal.sigtimedwait
1103
1104 sigset: object
Serhiy Storchaka6b680cd2015-05-16 15:57:56 +03001105 timeout as timeout_obj: object
Tal Einatc7027b72015-05-16 14:14:49 +03001106 /
1107
1108Like sigwaitinfo(), but with a timeout.
1109
1110The timeout is specified in seconds, with floating point numbers allowed.
1111[clinic start generated code]*/
1112
Ross Lagerwallbc808222011-06-25 12:13:40 +02001113static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001114signal_sigtimedwait_impl(PyObject *module, PyObject *sigset,
Serhiy Storchaka6b680cd2015-05-16 15:57:56 +03001115 PyObject *timeout_obj)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001116/*[clinic end generated code: output=f7eff31e679f4312 input=53fd4ea3e3724eb8]*/
Ross Lagerwallbc808222011-06-25 12:13:40 +02001117{
Victor Stinnera453cd82015-03-20 12:54:28 +01001118 struct timespec ts;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001119 sigset_t set;
1120 siginfo_t si;
Victor Stinnera453cd82015-03-20 12:54:28 +01001121 int res;
Victor Stinner34dc0f42015-03-27 18:19:03 +01001122 _PyTime_t timeout, deadline, monotonic;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001123
Victor Stinner869e1772015-03-30 03:49:14 +02001124 if (_PyTime_FromSecondsObject(&timeout,
1125 timeout_obj, _PyTime_ROUND_CEILING) < 0)
Ross Lagerwallbc808222011-06-25 12:13:40 +02001126 return NULL;
1127
Victor Stinnera453cd82015-03-20 12:54:28 +01001128 if (timeout < 0) {
Ross Lagerwallbc808222011-06-25 12:13:40 +02001129 PyErr_SetString(PyExc_ValueError, "timeout must be non-negative");
1130 return NULL;
1131 }
1132
Tal Einatc7027b72015-05-16 14:14:49 +03001133 if (iterable_to_sigset(sigset, &set))
Ross Lagerwallbc808222011-06-25 12:13:40 +02001134 return NULL;
1135
Victor Stinner34dc0f42015-03-27 18:19:03 +01001136 deadline = _PyTime_GetMonotonicClock() + timeout;
Victor Stinnera453cd82015-03-20 12:54:28 +01001137
1138 do {
Victor Stinner34dc0f42015-03-27 18:19:03 +01001139 if (_PyTime_AsTimespec(timeout, &ts) < 0)
1140 return NULL;
Victor Stinnera453cd82015-03-20 12:54:28 +01001141
1142 Py_BEGIN_ALLOW_THREADS
1143 res = sigtimedwait(&set, &si, &ts);
1144 Py_END_ALLOW_THREADS
1145
1146 if (res != -1)
1147 break;
1148
1149 if (errno != EINTR) {
1150 if (errno == EAGAIN)
1151 Py_RETURN_NONE;
1152 else
1153 return PyErr_SetFromErrno(PyExc_OSError);
1154 }
1155
1156 /* sigtimedwait() was interrupted by a signal (EINTR) */
1157 if (PyErr_CheckSignals())
1158 return NULL;
1159
Victor Stinner34dc0f42015-03-27 18:19:03 +01001160 monotonic = _PyTime_GetMonotonicClock();
1161 timeout = deadline - monotonic;
Victor Stinner6aa446c2015-03-30 21:33:51 +02001162 if (timeout < 0)
Victor Stinnera453cd82015-03-20 12:54:28 +01001163 break;
1164 } while (1);
Ross Lagerwallbc808222011-06-25 12:13:40 +02001165
1166 return fill_siginfo(&si);
1167}
1168
Ross Lagerwallbc808222011-06-25 12:13:40 +02001169#endif /* #ifdef HAVE_SIGTIMEDWAIT */
1170
Victor Stinnerb3e72192011-05-08 01:46:11 +02001171
Antoine Pitroua6a4dc82017-09-07 18:56:24 +02001172#if defined(HAVE_PTHREAD_KILL)
Tal Einatc7027b72015-05-16 14:14:49 +03001173
1174/*[clinic input]
1175signal.pthread_kill
1176
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +02001177 thread_id: unsigned_long(bitwise=True)
Tal Einatc7027b72015-05-16 14:14:49 +03001178 signalnum: int
1179 /
1180
1181Send a signal to a thread.
1182[clinic start generated code]*/
1183
Victor Stinnerb3e72192011-05-08 01:46:11 +02001184static PyObject *
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +02001185signal_pthread_kill_impl(PyObject *module, unsigned long thread_id,
1186 int signalnum)
1187/*[clinic end generated code: output=7629919b791bc27f input=1d901f2c7bb544ff]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +02001188{
Victor Stinnerb3e72192011-05-08 01:46:11 +02001189 int err;
1190
Tal Einatc7027b72015-05-16 14:14:49 +03001191 err = pthread_kill((pthread_t)thread_id, signalnum);
Victor Stinnerb3e72192011-05-08 01:46:11 +02001192 if (err != 0) {
1193 errno = err;
1194 PyErr_SetFromErrno(PyExc_OSError);
1195 return NULL;
1196 }
1197
1198 /* the signal may have been send to the current thread */
1199 if (PyErr_CheckSignals())
1200 return NULL;
1201
1202 Py_RETURN_NONE;
1203}
1204
Antoine Pitroua6a4dc82017-09-07 18:56:24 +02001205#endif /* #if defined(HAVE_PTHREAD_KILL) */
Victor Stinnerb3e72192011-05-08 01:46:11 +02001206
1207
1208
Tal Einatc7027b72015-05-16 14:14:49 +03001209/* List of functions defined in the module -- some of the methoddefs are
1210 defined to nothing if the corresponding C function is not available. */
Barry Warsaw92971171997-01-03 00:14:25 +00001211static PyMethodDef signal_methods[] = {
Tal Einatc7027b72015-05-16 14:14:49 +03001212 {"default_int_handler", signal_default_int_handler, METH_VARARGS, default_int_handler_doc},
1213 SIGNAL_ALARM_METHODDEF
1214 SIGNAL_SETITIMER_METHODDEF
1215 SIGNAL_GETITIMER_METHODDEF
1216 SIGNAL_SIGNAL_METHODDEF
Antoine Pietri5d2a27d2018-03-12 14:42:34 +01001217 SIGNAL_STRSIGNAL_METHODDEF
Tal Einatc7027b72015-05-16 14:14:49 +03001218 SIGNAL_GETSIGNAL_METHODDEF
Nathaniel J. Smith902ab802017-12-17 20:10:18 -08001219 {"set_wakeup_fd", (PyCFunction)signal_set_wakeup_fd, METH_VARARGS | METH_KEYWORDS, set_wakeup_fd_doc},
Tal Einatc7027b72015-05-16 14:14:49 +03001220 SIGNAL_SIGINTERRUPT_METHODDEF
1221 SIGNAL_PAUSE_METHODDEF
1222 SIGNAL_PTHREAD_KILL_METHODDEF
1223 SIGNAL_PTHREAD_SIGMASK_METHODDEF
1224 SIGNAL_SIGPENDING_METHODDEF
1225 SIGNAL_SIGWAIT_METHODDEF
1226 SIGNAL_SIGWAITINFO_METHODDEF
1227 SIGNAL_SIGTIMEDWAIT_METHODDEF
1228 {NULL, NULL} /* sentinel */
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001229};
1230
Barry Warsaw92971171997-01-03 00:14:25 +00001231
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001232PyDoc_STRVAR(module_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001233"This module provides mechanisms to use signal handlers in Python.\n\
1234\n\
1235Functions:\n\
1236\n\
1237alarm() -- cause SIGALRM after a specified time [Unix only]\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001238setitimer() -- cause a signal (described below) after a specified\n\
1239 float time and the timer may restart then [Unix only]\n\
1240getitimer() -- get current value of timer [Unix only]\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001241signal() -- set the action for a given signal\n\
1242getsignal() -- get the signal action for a given signal\n\
1243pause() -- wait until a signal arrives [Unix only]\n\
1244default_int_handler() -- default SIGINT handler\n\
1245\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001246signal constants:\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001247SIG_DFL -- used to refer to the system default handler\n\
1248SIG_IGN -- used to ignore the signal\n\
1249NSIG -- number of defined signals\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001250SIGINT, SIGTERM, etc. -- signal numbers\n\
1251\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001252itimer constants:\n\
1253ITIMER_REAL -- decrements in real time, and delivers SIGALRM upon\n\
1254 expiration\n\
1255ITIMER_VIRTUAL -- decrements only when the process is executing,\n\
1256 and delivers SIGVTALRM upon expiration\n\
1257ITIMER_PROF -- decrements both when the process is executing and\n\
1258 when the system is executing on behalf of the process.\n\
1259 Coupled with ITIMER_VIRTUAL, this timer is usually\n\
1260 used to profile the time spent by the application\n\
1261 in user and kernel space. SIGPROF is delivered upon\n\
1262 expiration.\n\
1263\n\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001264*** IMPORTANT NOTICE ***\n\
1265A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001266the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001267
Martin v. Löwis1a214512008-06-11 05:26:20 +00001268static struct PyModuleDef signalmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001269 PyModuleDef_HEAD_INIT,
Brett Cannon815a6f32014-04-04 10:20:28 -04001270 "_signal",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001271 module_doc,
1272 -1,
1273 signal_methods,
1274 NULL,
1275 NULL,
1276 NULL,
1277 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001278};
1279
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001280PyMODINIT_FUNC
Giampaolo Rodola'e09fb712014-04-04 15:34:17 +02001281PyInit__signal(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001282{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001283 PyObject *m, *d, *x;
1284 int i;
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001285
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001286 main_thread = PyThread_get_thread_ident();
1287 main_pid = getpid();
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001288
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001289 /* Create the module and add the functions */
1290 m = PyModule_Create(&signalmodule);
1291 if (m == NULL)
1292 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001293
Ross Lagerwallbc808222011-06-25 12:13:40 +02001294#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
Victor Stinner1c8f0592013-07-22 22:24:54 +02001295 if (!initialized) {
1296 if (PyStructSequence_InitType2(&SiginfoType, &struct_siginfo_desc) < 0)
1297 return NULL;
1298 }
Ross Lagerwallbc808222011-06-25 12:13:40 +02001299 Py_INCREF((PyObject*) &SiginfoType);
1300 PyModule_AddObject(m, "struct_siginfo", (PyObject*) &SiginfoType);
1301 initialized = 1;
1302#endif
1303
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001304 /* Add some symbolic constants to the module */
1305 d = PyModule_GetDict(m);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001306
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001307 x = DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL);
1308 if (!x || PyDict_SetItemString(d, "SIG_DFL", x) < 0)
1309 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001310
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001311 x = IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN);
1312 if (!x || PyDict_SetItemString(d, "SIG_IGN", x) < 0)
1313 goto finally;
Barry Warsaw92971171997-01-03 00:14:25 +00001314
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001315 x = PyLong_FromLong((long)NSIG);
1316 if (!x || PyDict_SetItemString(d, "NSIG", x) < 0)
1317 goto finally;
1318 Py_DECREF(x);
Barry Warsaw92971171997-01-03 00:14:25 +00001319
Victor Stinnera9293352011-04-30 15:21:58 +02001320#ifdef SIG_BLOCK
Victor Stinner72c53b52011-05-02 16:15:43 +02001321 if (PyModule_AddIntMacro(m, SIG_BLOCK))
1322 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001323#endif
Victor Stinnera9293352011-04-30 15:21:58 +02001324#ifdef SIG_UNBLOCK
Victor Stinner72c53b52011-05-02 16:15:43 +02001325 if (PyModule_AddIntMacro(m, SIG_UNBLOCK))
1326 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001327#endif
Victor Stinnera9293352011-04-30 15:21:58 +02001328#ifdef SIG_SETMASK
Victor Stinner72c53b52011-05-02 16:15:43 +02001329 if (PyModule_AddIntMacro(m, SIG_SETMASK))
1330 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001331#endif
1332
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001333 x = IntHandler = PyDict_GetItemString(d, "default_int_handler");
1334 if (!x)
1335 goto finally;
1336 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +00001337
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001338 _Py_atomic_store_relaxed(&Handlers[0].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001339 for (i = 1; i < NSIG; i++) {
1340 void (*t)(int);
1341 t = PyOS_getsig(i);
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001342 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001343 if (t == SIG_DFL)
1344 Handlers[i].func = DefaultHandler;
1345 else if (t == SIG_IGN)
1346 Handlers[i].func = IgnoreHandler;
1347 else
1348 Handlers[i].func = Py_None; /* None of our business */
1349 Py_INCREF(Handlers[i].func);
1350 }
1351 if (Handlers[SIGINT].func == DefaultHandler) {
1352 /* Install default int handler */
1353 Py_INCREF(IntHandler);
Serhiy Storchaka57a01d32016-04-10 18:05:40 +03001354 Py_SETREF(Handlers[SIGINT].func, IntHandler);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001355 old_siginthandler = PyOS_setsig(SIGINT, signal_handler);
1356 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001357
1358#ifdef SIGHUP
Christian Heimes6782b142016-09-09 00:11:45 +02001359 if (PyModule_AddIntMacro(m, SIGHUP))
1360 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001361#endif
1362#ifdef SIGINT
Christian Heimes6782b142016-09-09 00:11:45 +02001363 if (PyModule_AddIntMacro(m, SIGINT))
1364 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001365#endif
Tim Peters1ce3cf72001-10-01 17:58:40 +00001366#ifdef SIGBREAK
Christian Heimes6782b142016-09-09 00:11:45 +02001367 if (PyModule_AddIntMacro(m, SIGBREAK))
1368 goto finally;
Tim Peters1ce3cf72001-10-01 17:58:40 +00001369#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001370#ifdef SIGQUIT
Christian Heimes6782b142016-09-09 00:11:45 +02001371 if (PyModule_AddIntMacro(m, SIGQUIT))
1372 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001373#endif
1374#ifdef SIGILL
Christian Heimes6782b142016-09-09 00:11:45 +02001375 if (PyModule_AddIntMacro(m, SIGILL))
1376 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001377#endif
1378#ifdef SIGTRAP
Christian Heimes6782b142016-09-09 00:11:45 +02001379 if (PyModule_AddIntMacro(m, SIGTRAP))
1380 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001381#endif
1382#ifdef SIGIOT
Christian Heimes6782b142016-09-09 00:11:45 +02001383 if (PyModule_AddIntMacro(m, SIGIOT))
1384 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001385#endif
1386#ifdef SIGABRT
Christian Heimes6782b142016-09-09 00:11:45 +02001387 if (PyModule_AddIntMacro(m, SIGABRT))
1388 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001389#endif
1390#ifdef SIGEMT
Christian Heimes6782b142016-09-09 00:11:45 +02001391 if (PyModule_AddIntMacro(m, SIGEMT))
1392 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001393#endif
1394#ifdef SIGFPE
Christian Heimes6782b142016-09-09 00:11:45 +02001395 if (PyModule_AddIntMacro(m, SIGFPE))
1396 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001397#endif
1398#ifdef SIGKILL
Christian Heimes6782b142016-09-09 00:11:45 +02001399 if (PyModule_AddIntMacro(m, SIGKILL))
1400 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001401#endif
1402#ifdef SIGBUS
Christian Heimes6782b142016-09-09 00:11:45 +02001403 if (PyModule_AddIntMacro(m, SIGBUS))
1404 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001405#endif
1406#ifdef SIGSEGV
Christian Heimes6782b142016-09-09 00:11:45 +02001407 if (PyModule_AddIntMacro(m, SIGSEGV))
1408 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001409#endif
1410#ifdef SIGSYS
Christian Heimes6782b142016-09-09 00:11:45 +02001411 if (PyModule_AddIntMacro(m, SIGSYS))
1412 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001413#endif
1414#ifdef SIGPIPE
Christian Heimes6782b142016-09-09 00:11:45 +02001415 if (PyModule_AddIntMacro(m, SIGPIPE))
1416 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001417#endif
1418#ifdef SIGALRM
Christian Heimes6782b142016-09-09 00:11:45 +02001419 if (PyModule_AddIntMacro(m, SIGALRM))
1420 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001421#endif
1422#ifdef SIGTERM
Christian Heimes6782b142016-09-09 00:11:45 +02001423 if (PyModule_AddIntMacro(m, SIGTERM))
1424 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001425#endif
1426#ifdef SIGUSR1
Christian Heimes6782b142016-09-09 00:11:45 +02001427 if (PyModule_AddIntMacro(m, SIGUSR1))
1428 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001429#endif
1430#ifdef SIGUSR2
Christian Heimes6782b142016-09-09 00:11:45 +02001431 if (PyModule_AddIntMacro(m, SIGUSR2))
1432 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001433#endif
1434#ifdef SIGCLD
Christian Heimes6782b142016-09-09 00:11:45 +02001435 if (PyModule_AddIntMacro(m, SIGCLD))
1436 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001437#endif
1438#ifdef SIGCHLD
Christian Heimes6782b142016-09-09 00:11:45 +02001439 if (PyModule_AddIntMacro(m, SIGCHLD))
1440 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001441#endif
1442#ifdef SIGPWR
Christian Heimes6782b142016-09-09 00:11:45 +02001443 if (PyModule_AddIntMacro(m, SIGPWR))
1444 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001445#endif
1446#ifdef SIGIO
Christian Heimes6782b142016-09-09 00:11:45 +02001447 if (PyModule_AddIntMacro(m, SIGIO))
1448 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001449#endif
1450#ifdef SIGURG
Christian Heimes6782b142016-09-09 00:11:45 +02001451 if (PyModule_AddIntMacro(m, SIGURG))
1452 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001453#endif
1454#ifdef SIGWINCH
Christian Heimes6782b142016-09-09 00:11:45 +02001455 if (PyModule_AddIntMacro(m, SIGWINCH))
1456 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001457#endif
1458#ifdef SIGPOLL
Christian Heimes6782b142016-09-09 00:11:45 +02001459 if (PyModule_AddIntMacro(m, SIGPOLL))
1460 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001461#endif
1462#ifdef SIGSTOP
Christian Heimes6782b142016-09-09 00:11:45 +02001463 if (PyModule_AddIntMacro(m, SIGSTOP))
1464 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001465#endif
1466#ifdef SIGTSTP
Christian Heimes6782b142016-09-09 00:11:45 +02001467 if (PyModule_AddIntMacro(m, SIGTSTP))
1468 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001469#endif
1470#ifdef SIGCONT
Christian Heimes6782b142016-09-09 00:11:45 +02001471 if (PyModule_AddIntMacro(m, SIGCONT))
1472 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001473#endif
1474#ifdef SIGTTIN
Christian Heimes6782b142016-09-09 00:11:45 +02001475 if (PyModule_AddIntMacro(m, SIGTTIN))
1476 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001477#endif
1478#ifdef SIGTTOU
Christian Heimes6782b142016-09-09 00:11:45 +02001479 if (PyModule_AddIntMacro(m, SIGTTOU))
1480 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001481#endif
1482#ifdef SIGVTALRM
Christian Heimes6782b142016-09-09 00:11:45 +02001483 if (PyModule_AddIntMacro(m, SIGVTALRM))
1484 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001485#endif
1486#ifdef SIGPROF
Christian Heimes6782b142016-09-09 00:11:45 +02001487 if (PyModule_AddIntMacro(m, SIGPROF))
1488 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001489#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001490#ifdef SIGXCPU
Christian Heimes6782b142016-09-09 00:11:45 +02001491 if (PyModule_AddIntMacro(m, SIGXCPU))
1492 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001493#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001494#ifdef SIGXFSZ
Christian Heimes6782b142016-09-09 00:11:45 +02001495 if (PyModule_AddIntMacro(m, SIGXFSZ))
1496 goto finally;
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001497#endif
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001498#ifdef SIGRTMIN
Christian Heimes6782b142016-09-09 00:11:45 +02001499 if (PyModule_AddIntMacro(m, SIGRTMIN))
1500 goto finally;
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001501#endif
1502#ifdef SIGRTMAX
Christian Heimes6782b142016-09-09 00:11:45 +02001503 if (PyModule_AddIntMacro(m, SIGRTMAX))
1504 goto finally;
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001505#endif
Martin v. Löwis175af252002-01-12 11:43:25 +00001506#ifdef SIGINFO
Christian Heimes6782b142016-09-09 00:11:45 +02001507 if (PyModule_AddIntMacro(m, SIGINFO))
1508 goto finally;
Martin v. Löwis175af252002-01-12 11:43:25 +00001509#endif
Martin v. Löwis823725e2008-03-24 13:39:54 +00001510
1511#ifdef ITIMER_REAL
Christian Heimes6782b142016-09-09 00:11:45 +02001512 if (PyModule_AddIntMacro(m, ITIMER_REAL))
1513 goto finally;
Martin v. Löwis823725e2008-03-24 13:39:54 +00001514#endif
1515#ifdef ITIMER_VIRTUAL
Christian Heimes6782b142016-09-09 00:11:45 +02001516 if (PyModule_AddIntMacro(m, ITIMER_VIRTUAL))
1517 goto finally;
Martin v. Löwis823725e2008-03-24 13:39:54 +00001518#endif
1519#ifdef ITIMER_PROF
Christian Heimes6782b142016-09-09 00:11:45 +02001520 if (PyModule_AddIntMacro(m, ITIMER_PROF))
1521 goto finally;
Martin v. Löwis823725e2008-03-24 13:39:54 +00001522#endif
1523
1524#if defined (HAVE_SETITIMER) || defined (HAVE_GETITIMER)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001525 ItimerError = PyErr_NewException("signal.ItimerError",
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +03001526 PyExc_OSError, NULL);
Neal Norwitz2f99b242008-08-24 05:48:10 +00001527 if (ItimerError != NULL)
Martin Panter6d57fe12016-09-17 03:26:16 +00001528 PyDict_SetItemString(d, "ItimerError", ItimerError);
Martin v. Löwis823725e2008-03-24 13:39:54 +00001529#endif
1530
Brian Curtineb24d742010-04-12 17:16:38 +00001531#ifdef CTRL_C_EVENT
Christian Heimes6782b142016-09-09 00:11:45 +02001532 if (PyModule_AddIntMacro(m, CTRL_C_EVENT))
1533 goto finally;
Brian Curtineb24d742010-04-12 17:16:38 +00001534#endif
1535
1536#ifdef CTRL_BREAK_EVENT
Christian Heimes6782b142016-09-09 00:11:45 +02001537 if (PyModule_AddIntMacro(m, CTRL_BREAK_EVENT))
1538 goto finally;
Brian Curtineb24d742010-04-12 17:16:38 +00001539#endif
1540
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001541#ifdef MS_WINDOWS
1542 /* Create manual-reset event, initially unset */
1543 sigint_event = CreateEvent(NULL, TRUE, FALSE, FALSE);
1544#endif
1545
Martin v. Löwis1a214512008-06-11 05:26:20 +00001546 if (PyErr_Occurred()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001547 Py_DECREF(m);
1548 m = NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001549 }
Barry Warsaw92971171997-01-03 00:14:25 +00001550
Barry Warsaw92971171997-01-03 00:14:25 +00001551 finally:
Martin v. Löwis1a214512008-06-11 05:26:20 +00001552 return m;
Guido van Rossum08c16611997-08-02 03:01:42 +00001553}
1554
1555static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001556finisignal(void)
Guido van Rossum08c16611997-08-02 03:01:42 +00001557{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001558 int i;
1559 PyObject *func;
Guido van Rossum08c16611997-08-02 03:01:42 +00001560
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001561 PyOS_setsig(SIGINT, old_siginthandler);
1562 old_siginthandler = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +00001563
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001564 for (i = 1; i < NSIG; i++) {
1565 func = Handlers[i].func;
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001566 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001567 Handlers[i].func = NULL;
1568 if (i != SIGINT && func != NULL && func != Py_None &&
1569 func != DefaultHandler && func != IgnoreHandler)
1570 PyOS_setsig(i, SIG_DFL);
1571 Py_XDECREF(func);
1572 }
Guido van Rossum08c16611997-08-02 03:01:42 +00001573
Serhiy Storchaka505ff752014-02-09 13:33:53 +02001574 Py_CLEAR(IntHandler);
1575 Py_CLEAR(DefaultHandler);
1576 Py_CLEAR(IgnoreHandler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001577}
1578
Barry Warsaw92971171997-01-03 00:14:25 +00001579
Barry Warsaw92971171997-01-03 00:14:25 +00001580/* Declared in pyerrors.h */
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001581int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001582PyErr_CheckSignals(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001583{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001584 int i;
1585 PyObject *f;
Barry Warsaw92971171997-01-03 00:14:25 +00001586
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001587 if (!_Py_atomic_load(&is_tripped))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001588 return 0;
Christian Heimesb76922a2007-12-11 01:06:40 +00001589
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001590 if (PyThread_get_thread_ident() != main_thread)
1591 return 0;
Christian Heimesb76922a2007-12-11 01:06:40 +00001592
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001593 /*
1594 * The is_tripped variable is meant to speed up the calls to
1595 * PyErr_CheckSignals (both directly or via pending calls) when no
1596 * signal has arrived. This variable is set to 1 when a signal arrives
1597 * and it is set to 0 here, when we know some signals arrived. This way
1598 * we can run the registered handlers with no signals blocked.
1599 *
1600 * NOTE: with this approach we can have a situation where is_tripped is
1601 * 1 but we have no more signals to handle (Handlers[i].tripped
1602 * is 0 for every signal i). This won't do us any harm (except
1603 * we're gonna spent some cycles for nothing). This happens when
1604 * we receive a signal i after we zero is_tripped and before we
1605 * check Handlers[i].tripped.
1606 */
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001607 _Py_atomic_store(&is_tripped, 0);
Christian Heimesb76922a2007-12-11 01:06:40 +00001608
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001609 if (!(f = (PyObject *)PyEval_GetFrame()))
1610 f = Py_None;
Christian Heimesb76922a2007-12-11 01:06:40 +00001611
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001612 for (i = 1; i < NSIG; i++) {
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001613 if (_Py_atomic_load_relaxed(&Handlers[i].tripped)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001614 PyObject *result = NULL;
1615 PyObject *arglist = Py_BuildValue("(iO)", i, f);
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001616 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Barry Warsaw92971171997-01-03 00:14:25 +00001617
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001618 if (arglist) {
1619 result = PyEval_CallObject(Handlers[i].func,
1620 arglist);
1621 Py_DECREF(arglist);
1622 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +02001623 if (!result) {
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001624 _Py_atomic_store(&is_tripped, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001625 return -1;
Antoine Pitrouc08177a2017-06-28 23:29:29 +02001626 }
Barry Warsaw92971171997-01-03 00:14:25 +00001627
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001628 Py_DECREF(result);
1629 }
1630 }
Christian Heimesb76922a2007-12-11 01:06:40 +00001631
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001632 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001633}
1634
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +00001635
Barry Warsaw92971171997-01-03 00:14:25 +00001636/* Replacements for intrcheck.c functionality
1637 * Declared in pyerrors.h
1638 */
1639void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001640PyErr_SetInterrupt(void)
Barry Warsaw92971171997-01-03 00:14:25 +00001641{
Victor Stinner6c9b35b2011-04-18 16:25:56 +02001642 trip_signal(SIGINT);
Barry Warsaw92971171997-01-03 00:14:25 +00001643}
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001644
1645void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001646PyOS_InitInterrupts(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001647{
Giampaolo Rodola'e09fb712014-04-04 15:34:17 +02001648 PyObject *m = PyImport_ImportModule("_signal");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001649 if (m) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001650 Py_DECREF(m);
1651 }
Guido van Rossum08c16611997-08-02 03:01:42 +00001652}
1653
1654void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001655PyOS_FiniInterrupts(void)
Guido van Rossum08c16611997-08-02 03:01:42 +00001656{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001657 finisignal();
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001658}
1659
1660int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001661PyOS_InterruptOccurred(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001662{
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001663 if (_Py_atomic_load_relaxed(&Handlers[SIGINT].tripped)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001664 if (PyThread_get_thread_ident() != main_thread)
1665 return 0;
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001666 _Py_atomic_store_relaxed(&Handlers[SIGINT].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001667 return 1;
1668 }
1669 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001670}
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001671
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001672static void
1673_clear_pending_signals(void)
1674{
1675 int i;
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001676 if (!_Py_atomic_load(&is_tripped))
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001677 return;
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001678 _Py_atomic_store(&is_tripped, 0);
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001679 for (i = 1; i < NSIG; ++i) {
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001680 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001681 }
1682}
1683
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001684void
Antoine Pitrou346cbd32017-05-27 17:50:54 +02001685_PySignal_AfterFork(void)
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001686{
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001687 /* Clear the signal flags after forking so that they aren't handled
1688 * in both processes if they came in just before the fork() but before
1689 * the interpreter had an opportunity to call the handlers. issue9535. */
1690 _clear_pending_signals();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001691 main_thread = PyThread_get_thread_ident();
1692 main_pid = getpid();
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001693}
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001694
1695int
1696_PyOS_IsMainThread(void)
1697{
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001698 return PyThread_get_thread_ident() == main_thread;
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001699}
1700
1701#ifdef MS_WINDOWS
1702void *_PyOS_SigintEvent(void)
1703{
1704 /* Returns a manual-reset event which gets tripped whenever
1705 SIGINT is received.
1706
1707 Python.h does not include windows.h so we do cannot use HANDLE
1708 as the return type of this function. We use void* instead. */
1709 return sigint_event;
1710}
1711#endif