blob: e70c6fc3969adc57d47a51b2624608c369225d77 [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
Antoine Pitrou6dd381e2011-11-21 21:26:56 +0100121#ifdef MS_WINDOWS
122static HANDLE sigint_event = NULL;
123#endif
124
Martin v. Löwis823725e2008-03-24 13:39:54 +0000125#ifdef HAVE_GETITIMER
126static PyObject *ItimerError;
127
Victor Stinneref611c92017-10-13 13:49:43 -0700128/* auxiliary functions for setitimer */
129static int
130timeval_from_double(PyObject *obj, struct timeval *tv)
Martin v. Löwis823725e2008-03-24 13:39:54 +0000131{
Victor Stinneref611c92017-10-13 13:49:43 -0700132 if (obj == NULL) {
133 tv->tv_sec = 0;
134 tv->tv_usec = 0;
135 return 0;
Antoine Pitrou729780a2017-06-30 10:01:05 +0200136 }
Victor Stinneref611c92017-10-13 13:49:43 -0700137
138 _PyTime_t t;
139 if (_PyTime_FromSecondsObject(&t, obj, _PyTime_ROUND_CEILING) < 0) {
140 return -1;
141 }
142 return _PyTime_AsTimeval(t, tv, _PyTime_ROUND_CEILING);
Martin v. Löwis823725e2008-03-24 13:39:54 +0000143}
144
Christian Heimes1a8501c2008-10-02 19:56:01 +0000145Py_LOCAL_INLINE(double)
Martin v. Löwis823725e2008-03-24 13:39:54 +0000146double_from_timeval(struct timeval *tv)
147{
148 return tv->tv_sec + (double)(tv->tv_usec / 1000000.0);
149}
150
151static PyObject *
152itimer_retval(struct itimerval *iv)
153{
154 PyObject *r, *v;
155
156 r = PyTuple_New(2);
157 if (r == NULL)
Martin Panter6d57fe12016-09-17 03:26:16 +0000158 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000159
160 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_value)))) {
Martin Panter6d57fe12016-09-17 03:26:16 +0000161 Py_DECREF(r);
162 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000163 }
164
165 PyTuple_SET_ITEM(r, 0, v);
166
167 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_interval)))) {
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, 1, v);
173
174 return r;
175}
176#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000177
Guido van Rossume4485b01994-09-07 14:32:49 +0000178static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000179signal_default_int_handler(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000180{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000181 PyErr_SetNone(PyExc_KeyboardInterrupt);
182 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000183}
184
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000185PyDoc_STRVAR(default_int_handler_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000186"default_int_handler(...)\n\
187\n\
Michael W. Hudson24ec2112004-06-17 15:55:53 +0000188The default handler for SIGINT installed by Python.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000189It raises KeyboardInterrupt.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000190
Thomas Wouters0796b002000-07-22 23:49:30 +0000191
192static int
Victor Stinner11517102014-07-29 23:31:34 +0200193report_wakeup_write_error(void *data)
Antoine Pitrou6f6ec372013-08-17 20:27:56 +0200194{
195 int save_errno = errno;
Benjamin Petersonca470632016-09-06 13:47:26 -0700196 errno = (int) (intptr_t) data;
Antoine Pitrou6f6ec372013-08-17 20:27:56 +0200197 PyErr_SetFromErrno(PyExc_OSError);
198 PySys_WriteStderr("Exception ignored when trying to write to the "
199 "signal wakeup fd:\n");
200 PyErr_WriteUnraisable(NULL);
201 errno = save_errno;
202 return 0;
203}
204
Victor Stinner11517102014-07-29 23:31:34 +0200205#ifdef MS_WINDOWS
206static int
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800207report_wakeup_send_error(void* data)
Victor Stinner11517102014-07-29 23:31:34 +0200208{
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800209 /* PyErr_SetExcFromWindowsErr() invokes FormatMessage() which
210 recognizes the error codes used by both GetLastError() and
211 WSAGetLastError */
212 PyErr_SetExcFromWindowsErr(PyExc_OSError, (int) (intptr_t) data);
Victor Stinner11517102014-07-29 23:31:34 +0200213 PySys_WriteStderr("Exception ignored when trying to send to the "
214 "signal wakeup fd:\n");
215 PyErr_WriteUnraisable(NULL);
Victor Stinner11517102014-07-29 23:31:34 +0200216 return 0;
217}
218#endif /* MS_WINDOWS */
219
Tim Peters4f1b2082000-07-23 21:18:09 +0000220static void
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200221trip_signal(int sig_num)
222{
Victor Stinnerd49b1f12011-05-08 02:03:15 +0200223 unsigned char byte;
Victor Stinner11517102014-07-29 23:31:34 +0200224 int fd;
225 Py_ssize_t rc;
Victor Stinnerc13ef662011-05-25 02:35:58 +0200226
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +0200227 _Py_atomic_store_relaxed(&Handlers[sig_num].tripped, 1);
Victor Stinner11517102014-07-29 23:31:34 +0200228
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200229 /* Set is_tripped after setting .tripped, as it gets
230 cleared in PyErr_CheckSignals() before .tripped. */
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +0200231 _Py_atomic_store(&is_tripped, 1);
232
233 /* Notify ceval.c */
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200234 _PyEval_SignalReceived();
Nathaniel J. Smith4ae01492017-05-16 14:12:11 -0700235
236 /* And then write to the wakeup fd *after* setting all the globals and
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200237 doing the _PyEval_SignalReceived. We used to write to the wakeup fd
238 and then set the flag, but this allowed the following sequence of events
239 (especially on windows, where trip_signal may run in a new thread):
Nathaniel J. Smith4ae01492017-05-16 14:12:11 -0700240
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800241 - main thread blocks on select([wakeup.fd], ...)
Nathaniel J. Smith4ae01492017-05-16 14:12:11 -0700242 - signal arrives
243 - trip_signal writes to the wakeup fd
244 - the main thread wakes up
245 - the main thread checks the signal flags, sees that they're unset
246 - the main thread empties the wakeup fd
247 - the main thread goes back to sleep
248 - trip_signal sets the flags to request the Python-level signal handler
249 be run
250 - the main thread doesn't notice, because it's asleep
251
252 See bpo-30038 for more details.
253 */
254
Victor Stinner11517102014-07-29 23:31:34 +0200255#ifdef MS_WINDOWS
256 fd = Py_SAFE_DOWNCAST(wakeup.fd, SOCKET_T, int);
257#else
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800258 fd = wakeup.fd;
Victor Stinner11517102014-07-29 23:31:34 +0200259#endif
260
261 if (fd != INVALID_FD) {
Victor Stinnerc13ef662011-05-25 02:35:58 +0200262 byte = (unsigned char)sig_num;
Victor Stinner11517102014-07-29 23:31:34 +0200263#ifdef MS_WINDOWS
264 if (wakeup.use_send) {
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800265 rc = send(fd, &byte, 1, 0);
Victor Stinner11517102014-07-29 23:31:34 +0200266
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800267 if (rc < 0) {
268 int last_error = GetLastError();
269 if (wakeup.warn_on_full_buffer ||
270 last_error != WSAEWOULDBLOCK)
271 {
272 /* Py_AddPendingCall() isn't signal-safe, but we
273 still use it for this exceptional case. */
274 Py_AddPendingCall(report_wakeup_send_error,
275 (void *)(intptr_t) last_error);
276 }
Victor Stinner11517102014-07-29 23:31:34 +0200277 }
278 }
279 else
280#endif
281 {
Victor Stinnere72fe392015-04-01 18:35:22 +0200282 /* _Py_write_noraise() retries write() if write() is interrupted by
283 a signal (fails with EINTR). */
284 rc = _Py_write_noraise(fd, &byte, 1);
Victor Stinner11517102014-07-29 23:31:34 +0200285
286 if (rc < 0) {
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800287 if (wakeup.warn_on_full_buffer ||
288 (errno != EWOULDBLOCK && errno != EAGAIN))
289 {
290 /* Py_AddPendingCall() isn't signal-safe, but we
291 still use it for this exceptional case. */
292 Py_AddPendingCall(report_wakeup_write_error,
293 (void *)(intptr_t)errno);
294 }
Victor Stinner11517102014-07-29 23:31:34 +0200295 }
296 }
Victor Stinnerc13ef662011-05-25 02:35:58 +0200297 }
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200298}
299
300static void
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000301signal_handler(int sig_num)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000302{
Antoine Pitrou39a65912010-11-05 19:47:27 +0000303 int save_errno = errno;
304
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000305 /* See NOTES section above */
Antoine Pitrou39a65912010-11-05 19:47:27 +0000306 if (getpid() == main_pid)
Antoine Pitrou39a65912010-11-05 19:47:27 +0000307 {
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200308 trip_signal(sig_num);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000309 }
Antoine Pitrou39a65912010-11-05 19:47:27 +0000310
Jean-Paul Calderone6f137ca2010-05-09 03:18:57 +0000311#ifndef HAVE_SIGACTION
Antoine Pitrou39a65912010-11-05 19:47:27 +0000312#ifdef SIGCHLD
313 /* To avoid infinite recursion, this signal remains
314 reset until explicit re-instated.
315 Don't clear the 'func' field as it is our pointer
316 to the Python handler... */
317 if (sig_num != SIGCHLD)
318#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000319 /* If the handler was not set up with sigaction, reinstall it. See
Nick Coghland6009512014-11-20 21:39:37 +1000320 * Python/pylifecycle.c for the implementation of PyOS_setsig which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000321 * makes this true. See also issue8354. */
322 PyOS_setsig(sig_num, signal_handler);
Jean-Paul Calderone6f137ca2010-05-09 03:18:57 +0000323#endif
Antoine Pitrou39a65912010-11-05 19:47:27 +0000324
325 /* Issue #10311: asynchronously executing signal handlers should not
326 mutate errno under the feet of unsuspecting C code. */
327 errno = save_errno;
Antoine Pitrou6dd381e2011-11-21 21:26:56 +0100328
329#ifdef MS_WINDOWS
330 if (sig_num == SIGINT)
331 SetEvent(sigint_event);
332#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000333}
Guido van Rossume4485b01994-09-07 14:32:49 +0000334
Guido van Rossum06d511d1995-03-10 15:13:48 +0000335
Guido van Rossum1171ee61997-08-22 20:42:00 +0000336#ifdef HAVE_ALARM
Tal Einatc7027b72015-05-16 14:14:49 +0300337
338/*[clinic input]
339signal.alarm -> long
340
341 seconds: int
342 /
343
344Arrange for SIGALRM to arrive after the given number of seconds.
345[clinic start generated code]*/
346
347static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300348signal_alarm_impl(PyObject *module, int seconds)
349/*[clinic end generated code: output=144232290814c298 input=0d5e97e0e6f39e86]*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000350{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000351 /* alarm() returns the number of seconds remaining */
Tal Einatc7027b72015-05-16 14:14:49 +0300352 return (long)alarm(seconds);
Guido van Rossumaa0f4c71994-08-23 13:49:37 +0000353}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000354
Guido van Rossum06d511d1995-03-10 15:13:48 +0000355#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000356
Guido van Rossum1171ee61997-08-22 20:42:00 +0000357#ifdef HAVE_PAUSE
Tal Einatc7027b72015-05-16 14:14:49 +0300358
359/*[clinic input]
360signal.pause
361
362Wait until a signal arrives.
363[clinic start generated code]*/
364
Guido van Rossuma597dde1995-01-10 20:56:29 +0000365static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300366signal_pause_impl(PyObject *module)
367/*[clinic end generated code: output=391656788b3c3929 input=f03de0f875752062]*/
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000368{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000369 Py_BEGIN_ALLOW_THREADS
370 (void)pause();
371 Py_END_ALLOW_THREADS
372 /* make sure that any exceptions that got raised are propagated
373 * back into Python
374 */
375 if (PyErr_CheckSignals())
376 return NULL;
Barry Warsaw92971171997-01-03 00:14:25 +0000377
Tal Einatc7027b72015-05-16 14:14:49 +0300378 Py_RETURN_NONE;
Guido van Rossume4485b01994-09-07 14:32:49 +0000379}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000380
Guido van Rossum06d511d1995-03-10 15:13:48 +0000381#endif
Guido van Rossume4485b01994-09-07 14:32:49 +0000382
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000383
Tal Einatc7027b72015-05-16 14:14:49 +0300384/*[clinic input]
385signal.signal
386
387 signalnum: int
388 handler: object
389 /
390
391Set the action for the given signal.
392
393The action can be SIG_DFL, SIG_IGN, or a callable Python object.
394The previous action is returned. See getsignal() for possible return values.
395
396*** IMPORTANT NOTICE ***
397A signal handler function is called with two arguments:
398the first is the signal number, the second is the interrupted stack frame.
399[clinic start generated code]*/
400
Guido van Rossume4485b01994-09-07 14:32:49 +0000401static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300402signal_signal_impl(PyObject *module, int signalnum, PyObject *handler)
403/*[clinic end generated code: output=b44cfda43780f3a1 input=deee84af5fa0432c]*/
Guido van Rossume4485b01994-09-07 14:32:49 +0000404{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000405 PyObject *old_handler;
406 void (*func)(int);
Brian Curtinef9efbd2010-08-06 19:27:32 +0000407#ifdef MS_WINDOWS
Tal Einatc7027b72015-05-16 14:14:49 +0300408 /* Validate that signalnum is one of the allowable signals */
409 switch (signalnum) {
Brian Curtinc734b312010-09-06 16:04:10 +0000410 case SIGABRT: break;
Brian Curtin9e88b5a2010-10-01 14:49:24 +0000411#ifdef SIGBREAK
412 /* Issue #10003: SIGBREAK is not documented as permitted, but works
413 and corresponds to CTRL_BREAK_EVENT. */
414 case SIGBREAK: break;
415#endif
Brian Curtinc734b312010-09-06 16:04:10 +0000416 case SIGFPE: break;
417 case SIGILL: break;
418 case SIGINT: break;
419 case SIGSEGV: break;
420 case SIGTERM: break;
421 default:
422 PyErr_SetString(PyExc_ValueError, "invalid signal value");
423 return NULL;
Brian Curtinef9efbd2010-08-06 19:27:32 +0000424 }
425#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000426 if (PyThread_get_thread_ident() != main_thread) {
427 PyErr_SetString(PyExc_ValueError,
428 "signal only works in main thread");
429 return NULL;
430 }
Tal Einatc7027b72015-05-16 14:14:49 +0300431 if (signalnum < 1 || signalnum >= NSIG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000432 PyErr_SetString(PyExc_ValueError,
433 "signal number out of range");
434 return NULL;
435 }
Tal Einatc7027b72015-05-16 14:14:49 +0300436 if (handler == IgnoreHandler)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000437 func = SIG_IGN;
Tal Einatc7027b72015-05-16 14:14:49 +0300438 else if (handler == DefaultHandler)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000439 func = SIG_DFL;
Tal Einatc7027b72015-05-16 14:14:49 +0300440 else if (!PyCallable_Check(handler)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000441 PyErr_SetString(PyExc_TypeError,
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000442"signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000443 return NULL;
444 }
445 else
446 func = signal_handler;
Antoine Pitrouf6f90ff2017-11-03 19:58:46 +0100447 /* Check for pending signals before changing signal handler */
448 if (PyErr_CheckSignals()) {
449 return NULL;
450 }
Tal Einatc7027b72015-05-16 14:14:49 +0300451 if (PyOS_setsig(signalnum, func) == SIG_ERR) {
Victor Stinner388196e2011-05-10 17:13:00 +0200452 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000453 return NULL;
454 }
Tal Einatc7027b72015-05-16 14:14:49 +0300455 old_handler = Handlers[signalnum].func;
Tal Einatc7027b72015-05-16 14:14:49 +0300456 Py_INCREF(handler);
457 Handlers[signalnum].func = handler;
Antoine Pitrouc8c952c2013-05-04 23:16:59 +0200458 if (old_handler != NULL)
459 return old_handler;
460 else
461 Py_RETURN_NONE;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000462}
463
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000464
Tal Einatc7027b72015-05-16 14:14:49 +0300465/*[clinic input]
466signal.getsignal
467
468 signalnum: int
469 /
470
471Return the current action for the given signal.
472
473The return value can be:
474 SIG_IGN -- if the signal is being ignored
475 SIG_DFL -- if the default action for the signal is in effect
476 None -- if an unknown handler is in effect
477 anything else -- the callable Python object used as a handler
478[clinic start generated code]*/
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000479
Guido van Rossume4485b01994-09-07 14:32:49 +0000480static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300481signal_getsignal_impl(PyObject *module, int signalnum)
482/*[clinic end generated code: output=35b3e0e796fd555e input=ac23a00f19dfa509]*/
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000483{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000484 PyObject *old_handler;
Tal Einatc7027b72015-05-16 14:14:49 +0300485 if (signalnum < 1 || signalnum >= NSIG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000486 PyErr_SetString(PyExc_ValueError,
487 "signal number out of range");
488 return NULL;
489 }
Tal Einatc7027b72015-05-16 14:14:49 +0300490 old_handler = Handlers[signalnum].func;
Antoine Pitrouc8c952c2013-05-04 23:16:59 +0200491 if (old_handler != NULL) {
492 Py_INCREF(old_handler);
493 return old_handler;
494 }
495 else {
496 Py_RETURN_NONE;
497 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000498}
499
Christian Heimes8640e742008-02-23 16:23:06 +0000500#ifdef HAVE_SIGINTERRUPT
Tal Einatc7027b72015-05-16 14:14:49 +0300501
502/*[clinic input]
503signal.siginterrupt
504
505 signalnum: int
506 flag: int
507 /
508
509Change system call restart behaviour.
510
511If flag is False, system calls will be restarted when interrupted by
512signal sig, else system calls will be interrupted.
513[clinic start generated code]*/
Christian Heimes8640e742008-02-23 16:23:06 +0000514
515static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300516signal_siginterrupt_impl(PyObject *module, int signalnum, int flag)
517/*[clinic end generated code: output=063816243d85dd19 input=4160acacca3e2099]*/
Christian Heimes8640e742008-02-23 16:23:06 +0000518{
Tal Einatc7027b72015-05-16 14:14:49 +0300519 if (signalnum < 1 || signalnum >= NSIG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000520 PyErr_SetString(PyExc_ValueError,
521 "signal number out of range");
522 return NULL;
523 }
Tal Einatc7027b72015-05-16 14:14:49 +0300524 if (siginterrupt(signalnum, flag)<0) {
Victor Stinner388196e2011-05-10 17:13:00 +0200525 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000526 return NULL;
527 }
Tal Einatc7027b72015-05-16 14:14:49 +0300528 Py_RETURN_NONE;
Christian Heimes8640e742008-02-23 16:23:06 +0000529}
530
531#endif
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000532
Tal Einatc7027b72015-05-16 14:14:49 +0300533
534static PyObject*
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800535signal_set_wakeup_fd(PyObject *self, PyObject *args, PyObject *kwds)
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000536{
Victor Stinnere134a7f2015-03-30 10:09:31 +0200537 struct _Py_stat_struct status;
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800538 static char *kwlist[] = {
539 "", "warn_on_full_buffer", NULL,
540 };
541 int warn_on_full_buffer = 1;
Victor Stinner11517102014-07-29 23:31:34 +0200542#ifdef MS_WINDOWS
543 PyObject *fdobj;
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100544 SOCKET_T sockfd, old_sockfd;
Victor Stinner11517102014-07-29 23:31:34 +0200545 int res;
546 int res_size = sizeof res;
547 PyObject *mod;
Victor Stinner11517102014-07-29 23:31:34 +0200548 int is_socket;
549
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800550 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|$p:set_wakeup_fd", kwlist,
551 &fdobj, &warn_on_full_buffer))
Victor Stinner11517102014-07-29 23:31:34 +0200552 return NULL;
553
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100554 sockfd = PyLong_AsSocket_t(fdobj);
555 if (sockfd == (SOCKET_T)(-1) && PyErr_Occurred())
Victor Stinner11517102014-07-29 23:31:34 +0200556 return NULL;
557#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000558 int fd, old_fd;
Victor Stinner11517102014-07-29 23:31:34 +0200559
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800560 if (!PyArg_ParseTupleAndKeywords(args, kwds, "i|$p:set_wakeup_fd", kwlist,
561 &fd, &warn_on_full_buffer))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000562 return NULL;
Victor Stinner11517102014-07-29 23:31:34 +0200563#endif
564
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000565 if (PyThread_get_thread_ident() != main_thread) {
566 PyErr_SetString(PyExc_ValueError,
567 "set_wakeup_fd only works in main thread");
568 return NULL;
569 }
Victor Stinner0bffc942014-07-21 16:28:54 +0200570
Victor Stinner11517102014-07-29 23:31:34 +0200571#ifdef MS_WINDOWS
572 is_socket = 0;
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100573 if (sockfd != INVALID_FD) {
Victor Stinner11517102014-07-29 23:31:34 +0200574 /* Import the _socket module to call WSAStartup() */
575 mod = PyImport_ImportModuleNoBlock("_socket");
576 if (mod == NULL)
577 return NULL;
578 Py_DECREF(mod);
579
580 /* test the socket */
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100581 if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR,
Victor Stinner11517102014-07-29 23:31:34 +0200582 (char *)&res, &res_size) != 0) {
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100583 int fd, err;
584
585 err = WSAGetLastError();
Victor Stinner11517102014-07-29 23:31:34 +0200586 if (err != WSAENOTSOCK) {
587 PyErr_SetExcFromWindowsErr(PyExc_OSError, err);
588 return NULL;
589 }
590
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100591 fd = (int)sockfd;
Steve Dower940f33a2016-09-08 11:21:54 -0700592 if ((SOCKET_T)fd != sockfd) {
Victor Stinner11517102014-07-29 23:31:34 +0200593 PyErr_SetString(PyExc_ValueError, "invalid fd");
594 return NULL;
595 }
596
Victor Stinnere134a7f2015-03-30 10:09:31 +0200597 if (_Py_fstat(fd, &status) != 0)
Victor Stinner11517102014-07-29 23:31:34 +0200598 return NULL;
Victor Stinner38227602014-08-27 12:59:44 +0200599
600 /* on Windows, a file cannot be set to non-blocking mode */
Victor Stinner11517102014-07-29 23:31:34 +0200601 }
Victor Stinner38227602014-08-27 12:59:44 +0200602 else {
Victor Stinner11517102014-07-29 23:31:34 +0200603 is_socket = 1;
Victor Stinner38227602014-08-27 12:59:44 +0200604
605 /* Windows does not provide a function to test if a socket
606 is in non-blocking mode */
607 }
Victor Stinner11517102014-07-29 23:31:34 +0200608 }
609
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100610 old_sockfd = wakeup.fd;
611 wakeup.fd = sockfd;
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800612 wakeup.warn_on_full_buffer = warn_on_full_buffer;
Victor Stinner11517102014-07-29 23:31:34 +0200613 wakeup.use_send = is_socket;
614
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100615 if (old_sockfd != INVALID_FD)
616 return PyLong_FromSocket_t(old_sockfd);
Victor Stinner11517102014-07-29 23:31:34 +0200617 else
618 return PyLong_FromLong(-1);
619#else
Victor Stinnerd18ccd12014-07-24 21:58:53 +0200620 if (fd != -1) {
Victor Stinner38227602014-08-27 12:59:44 +0200621 int blocking;
622
Victor Stinnere134a7f2015-03-30 10:09:31 +0200623 if (_Py_fstat(fd, &status) != 0)
Victor Stinner11517102014-07-29 23:31:34 +0200624 return NULL;
Victor Stinner38227602014-08-27 12:59:44 +0200625
626 blocking = _Py_get_blocking(fd);
627 if (blocking < 0)
628 return NULL;
629 if (blocking) {
630 PyErr_Format(PyExc_ValueError,
631 "the fd %i must be in non-blocking mode",
632 fd);
633 return NULL;
634 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000635 }
Victor Stinner0bffc942014-07-21 16:28:54 +0200636
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800637 old_fd = wakeup.fd;
638 wakeup.fd = fd;
639 wakeup.warn_on_full_buffer = warn_on_full_buffer;
Victor Stinner0bffc942014-07-21 16:28:54 +0200640
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000641 return PyLong_FromLong(old_fd);
Victor Stinner11517102014-07-29 23:31:34 +0200642#endif
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000643}
644
645PyDoc_STRVAR(set_wakeup_fd_doc,
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800646"set_wakeup_fd(fd, *, warn_on_full_buffer=True) -> fd\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000647\n\
Victor Stinner11517102014-07-29 23:31:34 +0200648Sets the fd to be written to (with the signal number) when a signal\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000649comes in. A library can use this to wakeup select or poll.\n\
Victor Stinner11517102014-07-29 23:31:34 +0200650The previous fd or -1 is returned.\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000651\n\
652The fd must be non-blocking.");
653
654/* C API for the same, without all the error checking */
655int
656PySignal_SetWakeupFd(int fd)
657{
Victor Stinner11517102014-07-29 23:31:34 +0200658 int old_fd;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000659 if (fd < 0)
660 fd = -1;
Victor Stinner11517102014-07-29 23:31:34 +0200661
662#ifdef MS_WINDOWS
663 old_fd = Py_SAFE_DOWNCAST(wakeup.fd, SOCKET_T, int);
Victor Stinner11517102014-07-29 23:31:34 +0200664#else
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800665 old_fd = wakeup.fd;
Victor Stinner11517102014-07-29 23:31:34 +0200666#endif
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800667 wakeup.fd = fd;
668 wakeup.warn_on_full_buffer = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000669 return old_fd;
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000670}
671
672
Martin v. Löwis823725e2008-03-24 13:39:54 +0000673#ifdef HAVE_SETITIMER
Tal Einatc7027b72015-05-16 14:14:49 +0300674
675/*[clinic input]
676signal.setitimer
677
678 which: int
Victor Stinneref611c92017-10-13 13:49:43 -0700679 seconds: object
680 interval: object(c_default="NULL") = 0.0
Tal Einatc7027b72015-05-16 14:14:49 +0300681 /
682
683Sets given itimer (one of ITIMER_REAL, ITIMER_VIRTUAL or ITIMER_PROF).
684
685The timer will fire after value seconds and after that every interval seconds.
686The itimer can be cleared by setting seconds to zero.
687
688Returns old values as a tuple: (delay, interval).
689[clinic start generated code]*/
690
Martin v. Löwis823725e2008-03-24 13:39:54 +0000691static PyObject *
Victor Stinneref611c92017-10-13 13:49:43 -0700692signal_setitimer_impl(PyObject *module, int which, PyObject *seconds,
693 PyObject *interval)
694/*[clinic end generated code: output=65f9dcbddc35527b input=de43daf194e6f66f]*/
Martin v. Löwis823725e2008-03-24 13:39:54 +0000695{
Martin v. Löwis823725e2008-03-24 13:39:54 +0000696 struct itimerval new, old;
697
Victor Stinneref611c92017-10-13 13:49:43 -0700698 if (timeval_from_double(seconds, &new.it_value) < 0) {
699 return NULL;
700 }
701 if (timeval_from_double(interval, &new.it_interval) < 0) {
702 return NULL;
703 }
704
Martin v. Löwis823725e2008-03-24 13:39:54 +0000705 /* Let OS check "which" value */
706 if (setitimer(which, &new, &old) != 0) {
Tal Einatc7027b72015-05-16 14:14:49 +0300707 PyErr_SetFromErrno(ItimerError);
708 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000709 }
710
711 return itimer_retval(&old);
712}
713
Martin v. Löwis823725e2008-03-24 13:39:54 +0000714#endif
715
716
717#ifdef HAVE_GETITIMER
Tal Einatc7027b72015-05-16 14:14:49 +0300718
719/*[clinic input]
720signal.getitimer
721
722 which: int
723 /
724
725Returns current value of given itimer.
726[clinic start generated code]*/
727
Martin v. Löwis823725e2008-03-24 13:39:54 +0000728static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300729signal_getitimer_impl(PyObject *module, int which)
730/*[clinic end generated code: output=9e053175d517db40 input=f7d21d38f3490627]*/
Martin v. Löwis823725e2008-03-24 13:39:54 +0000731{
Martin v. Löwis823725e2008-03-24 13:39:54 +0000732 struct itimerval old;
733
Martin v. Löwis823725e2008-03-24 13:39:54 +0000734 if (getitimer(which, &old) != 0) {
Tal Einatc7027b72015-05-16 14:14:49 +0300735 PyErr_SetFromErrno(ItimerError);
736 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000737 }
738
739 return itimer_retval(&old);
740}
741
Martin v. Löwis823725e2008-03-24 13:39:54 +0000742#endif
743
Ross Lagerwallbc808222011-06-25 12:13:40 +0200744#if defined(PYPTHREAD_SIGMASK) || defined(HAVE_SIGWAIT) || \
745 defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
Victor Stinnera9293352011-04-30 15:21:58 +0200746/* Convert an iterable to a sigset.
747 Return 0 on success, return -1 and raise an exception on error. */
748
749static int
750iterable_to_sigset(PyObject *iterable, sigset_t *mask)
751{
752 int result = -1;
753 PyObject *iterator, *item;
754 long signum;
Victor Stinnera9293352011-04-30 15:21:58 +0200755
756 sigemptyset(mask);
757
758 iterator = PyObject_GetIter(iterable);
759 if (iterator == NULL)
760 goto error;
761
762 while (1)
763 {
764 item = PyIter_Next(iterator);
765 if (item == NULL) {
766 if (PyErr_Occurred())
767 goto error;
768 else
769 break;
770 }
771
772 signum = PyLong_AsLong(item);
773 Py_DECREF(item);
774 if (signum == -1 && PyErr_Occurred())
775 goto error;
Miss Islington (bot)75a3e3d2018-04-23 12:42:26 -0700776 if (0 < signum && signum < NSIG) {
777 /* bpo-33329: ignore sigaddset() return value as it can fail
778 * for some reserved signals, but we want the `range(1, NSIG)`
779 * idiom to allow selecting all valid signals.
780 */
781 (void) sigaddset(mask, (int)signum);
782 }
783 else {
Victor Stinnera9293352011-04-30 15:21:58 +0200784 PyErr_Format(PyExc_ValueError,
785 "signal number %ld out of range", signum);
786 goto error;
787 }
788 }
789 result = 0;
790
791error:
792 Py_XDECREF(iterator);
793 return result;
794}
Victor Stinnerb3e72192011-05-08 01:46:11 +0200795#endif
Victor Stinnera9293352011-04-30 15:21:58 +0200796
Victor Stinnerb3e72192011-05-08 01:46:11 +0200797#if defined(PYPTHREAD_SIGMASK) || defined(HAVE_SIGPENDING)
Victor Stinner35b300c2011-05-04 13:20:35 +0200798static PyObject*
799sigset_to_set(sigset_t mask)
800{
801 PyObject *signum, *result;
802 int sig;
803
804 result = PySet_New(0);
805 if (result == NULL)
806 return NULL;
807
808 for (sig = 1; sig < NSIG; sig++) {
809 if (sigismember(&mask, sig) != 1)
810 continue;
811
812 /* Handle the case where it is a member by adding the signal to
813 the result list. Ignore the other cases because they mean the
814 signal isn't a member of the mask or the signal was invalid,
815 and an invalid signal must have been our fault in constructing
816 the loop boundaries. */
817 signum = PyLong_FromLong(sig);
818 if (signum == NULL) {
819 Py_DECREF(result);
820 return NULL;
821 }
822 if (PySet_Add(result, signum) == -1) {
823 Py_DECREF(signum);
824 Py_DECREF(result);
825 return NULL;
826 }
827 Py_DECREF(signum);
828 }
829 return result;
830}
Victor Stinnerb3e72192011-05-08 01:46:11 +0200831#endif
Victor Stinner35b300c2011-05-04 13:20:35 +0200832
Victor Stinnerb3e72192011-05-08 01:46:11 +0200833#ifdef PYPTHREAD_SIGMASK
Tal Einatc7027b72015-05-16 14:14:49 +0300834
835/*[clinic input]
836signal.pthread_sigmask
837
838 how: int
839 mask: object
840 /
841
842Fetch and/or change the signal mask of the calling thread.
843[clinic start generated code]*/
844
Victor Stinnera9293352011-04-30 15:21:58 +0200845static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300846signal_pthread_sigmask_impl(PyObject *module, int how, PyObject *mask)
847/*[clinic end generated code: output=ff640fe092bc9181 input=f3b7d7a61b7b8283]*/
Victor Stinnera9293352011-04-30 15:21:58 +0200848{
Tal Einatc7027b72015-05-16 14:14:49 +0300849 sigset_t newmask, previous;
Victor Stinnera9293352011-04-30 15:21:58 +0200850 int err;
851
Tal Einatc7027b72015-05-16 14:14:49 +0300852 if (iterable_to_sigset(mask, &newmask))
Victor Stinnera9293352011-04-30 15:21:58 +0200853 return NULL;
854
Tal Einatc7027b72015-05-16 14:14:49 +0300855 err = pthread_sigmask(how, &newmask, &previous);
Victor Stinnera9293352011-04-30 15:21:58 +0200856 if (err != 0) {
857 errno = err;
Victor Stinnerb3e72192011-05-08 01:46:11 +0200858 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnera9293352011-04-30 15:21:58 +0200859 return NULL;
860 }
861
Victor Stinnerd0e516d2011-05-03 14:57:12 +0200862 /* if signals was unblocked, signal handlers have been called */
863 if (PyErr_CheckSignals())
864 return NULL;
865
Victor Stinner35b300c2011-05-04 13:20:35 +0200866 return sigset_to_set(previous);
Victor Stinnera9293352011-04-30 15:21:58 +0200867}
868
Victor Stinnera9293352011-04-30 15:21:58 +0200869#endif /* #ifdef PYPTHREAD_SIGMASK */
870
Martin v. Löwis823725e2008-03-24 13:39:54 +0000871
Victor Stinnerb3e72192011-05-08 01:46:11 +0200872#ifdef HAVE_SIGPENDING
Tal Einatc7027b72015-05-16 14:14:49 +0300873
874/*[clinic input]
875signal.sigpending
876
877Examine pending signals.
878
879Returns a set of signal numbers that are pending for delivery to
880the calling thread.
881[clinic start generated code]*/
882
Victor Stinnerb3e72192011-05-08 01:46:11 +0200883static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300884signal_sigpending_impl(PyObject *module)
885/*[clinic end generated code: output=53375ffe89325022 input=e0036c016f874e29]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +0200886{
887 int err;
888 sigset_t mask;
889 err = sigpending(&mask);
890 if (err)
891 return PyErr_SetFromErrno(PyExc_OSError);
892 return sigset_to_set(mask);
893}
894
Victor Stinnerb3e72192011-05-08 01:46:11 +0200895#endif /* #ifdef HAVE_SIGPENDING */
896
897
898#ifdef HAVE_SIGWAIT
Tal Einatc7027b72015-05-16 14:14:49 +0300899
900/*[clinic input]
901signal.sigwait
902
903 sigset: object
904 /
905
906Wait for a signal.
907
908Suspend execution of the calling thread until the delivery of one of the
909signals specified in the signal set sigset. The function accepts the signal
910and returns the signal number.
911[clinic start generated code]*/
912
Victor Stinnerb3e72192011-05-08 01:46:11 +0200913static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300914signal_sigwait(PyObject *module, PyObject *sigset)
915/*[clinic end generated code: output=557173647424f6e4 input=11af2d82d83c2e94]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +0200916{
Victor Stinnerb3e72192011-05-08 01:46:11 +0200917 sigset_t set;
918 int err, signum;
919
Tal Einatc7027b72015-05-16 14:14:49 +0300920 if (iterable_to_sigset(sigset, &set))
Victor Stinnerb3e72192011-05-08 01:46:11 +0200921 return NULL;
922
Victor Stinner10c30d62011-06-10 01:39:53 +0200923 Py_BEGIN_ALLOW_THREADS
Victor Stinnerb3e72192011-05-08 01:46:11 +0200924 err = sigwait(&set, &signum);
Victor Stinner10c30d62011-06-10 01:39:53 +0200925 Py_END_ALLOW_THREADS
Victor Stinnerb3e72192011-05-08 01:46:11 +0200926 if (err) {
927 errno = err;
928 return PyErr_SetFromErrno(PyExc_OSError);
929 }
930
931 return PyLong_FromLong(signum);
932}
933
Tal Einatc7027b72015-05-16 14:14:49 +0300934#endif /* #ifdef HAVE_SIGWAIT */
935
Victor Stinnerb3e72192011-05-08 01:46:11 +0200936
Ross Lagerwallbc808222011-06-25 12:13:40 +0200937#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
938static int initialized;
939static PyStructSequence_Field struct_siginfo_fields[] = {
940 {"si_signo", "signal number"},
941 {"si_code", "signal code"},
942 {"si_errno", "errno associated with this signal"},
943 {"si_pid", "sending process ID"},
944 {"si_uid", "real user ID of sending process"},
945 {"si_status", "exit value or signal"},
946 {"si_band", "band event for SIGPOLL"},
947 {0}
948};
949
950PyDoc_STRVAR(struct_siginfo__doc__,
951"struct_siginfo: Result from sigwaitinfo or sigtimedwait.\n\n\
952This object may be accessed either as a tuple of\n\
953(si_signo, si_code, si_errno, si_pid, si_uid, si_status, si_band),\n\
954or via the attributes si_signo, si_code, and so on.");
955
956static PyStructSequence_Desc struct_siginfo_desc = {
957 "signal.struct_siginfo", /* name */
958 struct_siginfo__doc__, /* doc */
959 struct_siginfo_fields, /* fields */
960 7 /* n_in_sequence */
961};
962
963static PyTypeObject SiginfoType;
964
965static PyObject *
966fill_siginfo(siginfo_t *si)
967{
968 PyObject *result = PyStructSequence_New(&SiginfoType);
969 if (!result)
970 return NULL;
971
972 PyStructSequence_SET_ITEM(result, 0, PyLong_FromLong((long)(si->si_signo)));
973 PyStructSequence_SET_ITEM(result, 1, PyLong_FromLong((long)(si->si_code)));
974 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si->si_errno)));
975 PyStructSequence_SET_ITEM(result, 3, PyLong_FromPid(si->si_pid));
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200976 PyStructSequence_SET_ITEM(result, 4, _PyLong_FromUid(si->si_uid));
Ross Lagerwallbc808222011-06-25 12:13:40 +0200977 PyStructSequence_SET_ITEM(result, 5,
978 PyLong_FromLong((long)(si->si_status)));
Zachary Ware6a6967e2016-10-01 00:47:27 -0500979#ifdef HAVE_SIGINFO_T_SI_BAND
Ross Lagerwallbc808222011-06-25 12:13:40 +0200980 PyStructSequence_SET_ITEM(result, 6, PyLong_FromLong(si->si_band));
Zachary Ware6a6967e2016-10-01 00:47:27 -0500981#else
982 PyStructSequence_SET_ITEM(result, 6, PyLong_FromLong(0L));
983#endif
Ross Lagerwallbc808222011-06-25 12:13:40 +0200984 if (PyErr_Occurred()) {
985 Py_DECREF(result);
986 return NULL;
987 }
988
989 return result;
990}
991#endif
992
993#ifdef HAVE_SIGWAITINFO
Tal Einatc7027b72015-05-16 14:14:49 +0300994
995/*[clinic input]
996signal.sigwaitinfo
997
998 sigset: object
999 /
1000
1001Wait synchronously until one of the signals in *sigset* is delivered.
1002
1003Returns a struct_siginfo containing information about the signal.
1004[clinic start generated code]*/
1005
Ross Lagerwallbc808222011-06-25 12:13:40 +02001006static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001007signal_sigwaitinfo(PyObject *module, PyObject *sigset)
1008/*[clinic end generated code: output=c40f27b269cd2309 input=f3779a74a991e171]*/
Ross Lagerwallbc808222011-06-25 12:13:40 +02001009{
Ross Lagerwallbc808222011-06-25 12:13:40 +02001010 sigset_t set;
1011 siginfo_t si;
1012 int err;
Victor Stinnera453cd82015-03-20 12:54:28 +01001013 int async_err = 0;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001014
Tal Einatc7027b72015-05-16 14:14:49 +03001015 if (iterable_to_sigset(sigset, &set))
Ross Lagerwallbc808222011-06-25 12:13:40 +02001016 return NULL;
1017
Victor Stinnera453cd82015-03-20 12:54:28 +01001018 do {
1019 Py_BEGIN_ALLOW_THREADS
1020 err = sigwaitinfo(&set, &si);
1021 Py_END_ALLOW_THREADS
1022 } while (err == -1
1023 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Ross Lagerwallbc808222011-06-25 12:13:40 +02001024 if (err == -1)
Victor Stinnera453cd82015-03-20 12:54:28 +01001025 return (!async_err) ? PyErr_SetFromErrno(PyExc_OSError) : NULL;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001026
1027 return fill_siginfo(&si);
1028}
1029
Ross Lagerwallbc808222011-06-25 12:13:40 +02001030#endif /* #ifdef HAVE_SIGWAITINFO */
1031
1032#ifdef HAVE_SIGTIMEDWAIT
Tal Einatc7027b72015-05-16 14:14:49 +03001033
1034/*[clinic input]
1035signal.sigtimedwait
1036
1037 sigset: object
Serhiy Storchaka6b680cd2015-05-16 15:57:56 +03001038 timeout as timeout_obj: object
Tal Einatc7027b72015-05-16 14:14:49 +03001039 /
1040
1041Like sigwaitinfo(), but with a timeout.
1042
1043The timeout is specified in seconds, with floating point numbers allowed.
1044[clinic start generated code]*/
1045
Ross Lagerwallbc808222011-06-25 12:13:40 +02001046static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001047signal_sigtimedwait_impl(PyObject *module, PyObject *sigset,
Serhiy Storchaka6b680cd2015-05-16 15:57:56 +03001048 PyObject *timeout_obj)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001049/*[clinic end generated code: output=f7eff31e679f4312 input=53fd4ea3e3724eb8]*/
Ross Lagerwallbc808222011-06-25 12:13:40 +02001050{
Victor Stinnera453cd82015-03-20 12:54:28 +01001051 struct timespec ts;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001052 sigset_t set;
1053 siginfo_t si;
Victor Stinnera453cd82015-03-20 12:54:28 +01001054 int res;
Victor Stinner34dc0f42015-03-27 18:19:03 +01001055 _PyTime_t timeout, deadline, monotonic;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001056
Victor Stinner869e1772015-03-30 03:49:14 +02001057 if (_PyTime_FromSecondsObject(&timeout,
1058 timeout_obj, _PyTime_ROUND_CEILING) < 0)
Ross Lagerwallbc808222011-06-25 12:13:40 +02001059 return NULL;
1060
Victor Stinnera453cd82015-03-20 12:54:28 +01001061 if (timeout < 0) {
Ross Lagerwallbc808222011-06-25 12:13:40 +02001062 PyErr_SetString(PyExc_ValueError, "timeout must be non-negative");
1063 return NULL;
1064 }
1065
Tal Einatc7027b72015-05-16 14:14:49 +03001066 if (iterable_to_sigset(sigset, &set))
Ross Lagerwallbc808222011-06-25 12:13:40 +02001067 return NULL;
1068
Victor Stinner34dc0f42015-03-27 18:19:03 +01001069 deadline = _PyTime_GetMonotonicClock() + timeout;
Victor Stinnera453cd82015-03-20 12:54:28 +01001070
1071 do {
Victor Stinner34dc0f42015-03-27 18:19:03 +01001072 if (_PyTime_AsTimespec(timeout, &ts) < 0)
1073 return NULL;
Victor Stinnera453cd82015-03-20 12:54:28 +01001074
1075 Py_BEGIN_ALLOW_THREADS
1076 res = sigtimedwait(&set, &si, &ts);
1077 Py_END_ALLOW_THREADS
1078
1079 if (res != -1)
1080 break;
1081
1082 if (errno != EINTR) {
1083 if (errno == EAGAIN)
1084 Py_RETURN_NONE;
1085 else
1086 return PyErr_SetFromErrno(PyExc_OSError);
1087 }
1088
1089 /* sigtimedwait() was interrupted by a signal (EINTR) */
1090 if (PyErr_CheckSignals())
1091 return NULL;
1092
Victor Stinner34dc0f42015-03-27 18:19:03 +01001093 monotonic = _PyTime_GetMonotonicClock();
1094 timeout = deadline - monotonic;
Victor Stinner6aa446c2015-03-30 21:33:51 +02001095 if (timeout < 0)
Victor Stinnera453cd82015-03-20 12:54:28 +01001096 break;
1097 } while (1);
Ross Lagerwallbc808222011-06-25 12:13:40 +02001098
1099 return fill_siginfo(&si);
1100}
1101
Ross Lagerwallbc808222011-06-25 12:13:40 +02001102#endif /* #ifdef HAVE_SIGTIMEDWAIT */
1103
Victor Stinnerb3e72192011-05-08 01:46:11 +02001104
Antoine Pitroua6a4dc82017-09-07 18:56:24 +02001105#if defined(HAVE_PTHREAD_KILL)
Tal Einatc7027b72015-05-16 14:14:49 +03001106
1107/*[clinic input]
1108signal.pthread_kill
1109
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +02001110 thread_id: unsigned_long(bitwise=True)
Tal Einatc7027b72015-05-16 14:14:49 +03001111 signalnum: int
1112 /
1113
1114Send a signal to a thread.
1115[clinic start generated code]*/
1116
Victor Stinnerb3e72192011-05-08 01:46:11 +02001117static PyObject *
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +02001118signal_pthread_kill_impl(PyObject *module, unsigned long thread_id,
1119 int signalnum)
1120/*[clinic end generated code: output=7629919b791bc27f input=1d901f2c7bb544ff]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +02001121{
Victor Stinnerb3e72192011-05-08 01:46:11 +02001122 int err;
1123
Tal Einatc7027b72015-05-16 14:14:49 +03001124 err = pthread_kill((pthread_t)thread_id, signalnum);
Victor Stinnerb3e72192011-05-08 01:46:11 +02001125 if (err != 0) {
1126 errno = err;
1127 PyErr_SetFromErrno(PyExc_OSError);
1128 return NULL;
1129 }
1130
1131 /* the signal may have been send to the current thread */
1132 if (PyErr_CheckSignals())
1133 return NULL;
1134
1135 Py_RETURN_NONE;
1136}
1137
Antoine Pitroua6a4dc82017-09-07 18:56:24 +02001138#endif /* #if defined(HAVE_PTHREAD_KILL) */
Victor Stinnerb3e72192011-05-08 01:46:11 +02001139
1140
1141
Tal Einatc7027b72015-05-16 14:14:49 +03001142/* List of functions defined in the module -- some of the methoddefs are
1143 defined to nothing if the corresponding C function is not available. */
Barry Warsaw92971171997-01-03 00:14:25 +00001144static PyMethodDef signal_methods[] = {
Tal Einatc7027b72015-05-16 14:14:49 +03001145 {"default_int_handler", signal_default_int_handler, METH_VARARGS, default_int_handler_doc},
1146 SIGNAL_ALARM_METHODDEF
1147 SIGNAL_SETITIMER_METHODDEF
1148 SIGNAL_GETITIMER_METHODDEF
1149 SIGNAL_SIGNAL_METHODDEF
1150 SIGNAL_GETSIGNAL_METHODDEF
Nathaniel J. Smith902ab802017-12-17 20:10:18 -08001151 {"set_wakeup_fd", (PyCFunction)signal_set_wakeup_fd, METH_VARARGS | METH_KEYWORDS, set_wakeup_fd_doc},
Tal Einatc7027b72015-05-16 14:14:49 +03001152 SIGNAL_SIGINTERRUPT_METHODDEF
1153 SIGNAL_PAUSE_METHODDEF
1154 SIGNAL_PTHREAD_KILL_METHODDEF
1155 SIGNAL_PTHREAD_SIGMASK_METHODDEF
1156 SIGNAL_SIGPENDING_METHODDEF
1157 SIGNAL_SIGWAIT_METHODDEF
1158 SIGNAL_SIGWAITINFO_METHODDEF
1159 SIGNAL_SIGTIMEDWAIT_METHODDEF
1160 {NULL, NULL} /* sentinel */
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001161};
1162
Barry Warsaw92971171997-01-03 00:14:25 +00001163
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001164PyDoc_STRVAR(module_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001165"This module provides mechanisms to use signal handlers in Python.\n\
1166\n\
1167Functions:\n\
1168\n\
1169alarm() -- cause SIGALRM after a specified time [Unix only]\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001170setitimer() -- cause a signal (described below) after a specified\n\
1171 float time and the timer may restart then [Unix only]\n\
1172getitimer() -- get current value of timer [Unix only]\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001173signal() -- set the action for a given signal\n\
1174getsignal() -- get the signal action for a given signal\n\
1175pause() -- wait until a signal arrives [Unix only]\n\
1176default_int_handler() -- default SIGINT handler\n\
1177\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001178signal constants:\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001179SIG_DFL -- used to refer to the system default handler\n\
1180SIG_IGN -- used to ignore the signal\n\
1181NSIG -- number of defined signals\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001182SIGINT, SIGTERM, etc. -- signal numbers\n\
1183\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001184itimer constants:\n\
1185ITIMER_REAL -- decrements in real time, and delivers SIGALRM upon\n\
1186 expiration\n\
1187ITIMER_VIRTUAL -- decrements only when the process is executing,\n\
1188 and delivers SIGVTALRM upon expiration\n\
1189ITIMER_PROF -- decrements both when the process is executing and\n\
1190 when the system is executing on behalf of the process.\n\
1191 Coupled with ITIMER_VIRTUAL, this timer is usually\n\
1192 used to profile the time spent by the application\n\
1193 in user and kernel space. SIGPROF is delivered upon\n\
1194 expiration.\n\
1195\n\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001196*** IMPORTANT NOTICE ***\n\
1197A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001198the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001199
Martin v. Löwis1a214512008-06-11 05:26:20 +00001200static struct PyModuleDef signalmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001201 PyModuleDef_HEAD_INIT,
Brett Cannon815a6f32014-04-04 10:20:28 -04001202 "_signal",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001203 module_doc,
1204 -1,
1205 signal_methods,
1206 NULL,
1207 NULL,
1208 NULL,
1209 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001210};
1211
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001212PyMODINIT_FUNC
Giampaolo Rodola'e09fb712014-04-04 15:34:17 +02001213PyInit__signal(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001214{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001215 PyObject *m, *d, *x;
1216 int i;
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001217
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001218 main_thread = PyThread_get_thread_ident();
1219 main_pid = getpid();
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001220
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001221 /* Create the module and add the functions */
1222 m = PyModule_Create(&signalmodule);
1223 if (m == NULL)
1224 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001225
Ross Lagerwallbc808222011-06-25 12:13:40 +02001226#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
Victor Stinner1c8f0592013-07-22 22:24:54 +02001227 if (!initialized) {
1228 if (PyStructSequence_InitType2(&SiginfoType, &struct_siginfo_desc) < 0)
1229 return NULL;
1230 }
Ross Lagerwallbc808222011-06-25 12:13:40 +02001231 Py_INCREF((PyObject*) &SiginfoType);
1232 PyModule_AddObject(m, "struct_siginfo", (PyObject*) &SiginfoType);
1233 initialized = 1;
1234#endif
1235
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001236 /* Add some symbolic constants to the module */
1237 d = PyModule_GetDict(m);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001238
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001239 x = DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL);
1240 if (!x || PyDict_SetItemString(d, "SIG_DFL", x) < 0)
1241 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001242
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001243 x = IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN);
1244 if (!x || PyDict_SetItemString(d, "SIG_IGN", x) < 0)
1245 goto finally;
Barry Warsaw92971171997-01-03 00:14:25 +00001246
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001247 x = PyLong_FromLong((long)NSIG);
1248 if (!x || PyDict_SetItemString(d, "NSIG", x) < 0)
1249 goto finally;
1250 Py_DECREF(x);
Barry Warsaw92971171997-01-03 00:14:25 +00001251
Victor Stinnera9293352011-04-30 15:21:58 +02001252#ifdef SIG_BLOCK
Victor Stinner72c53b52011-05-02 16:15:43 +02001253 if (PyModule_AddIntMacro(m, SIG_BLOCK))
1254 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001255#endif
Victor Stinnera9293352011-04-30 15:21:58 +02001256#ifdef SIG_UNBLOCK
Victor Stinner72c53b52011-05-02 16:15:43 +02001257 if (PyModule_AddIntMacro(m, SIG_UNBLOCK))
1258 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001259#endif
Victor Stinnera9293352011-04-30 15:21:58 +02001260#ifdef SIG_SETMASK
Victor Stinner72c53b52011-05-02 16:15:43 +02001261 if (PyModule_AddIntMacro(m, SIG_SETMASK))
1262 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001263#endif
1264
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001265 x = IntHandler = PyDict_GetItemString(d, "default_int_handler");
1266 if (!x)
1267 goto finally;
1268 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +00001269
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001270 _Py_atomic_store_relaxed(&Handlers[0].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001271 for (i = 1; i < NSIG; i++) {
1272 void (*t)(int);
1273 t = PyOS_getsig(i);
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001274 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001275 if (t == SIG_DFL)
1276 Handlers[i].func = DefaultHandler;
1277 else if (t == SIG_IGN)
1278 Handlers[i].func = IgnoreHandler;
1279 else
1280 Handlers[i].func = Py_None; /* None of our business */
1281 Py_INCREF(Handlers[i].func);
1282 }
1283 if (Handlers[SIGINT].func == DefaultHandler) {
1284 /* Install default int handler */
1285 Py_INCREF(IntHandler);
Serhiy Storchaka57a01d32016-04-10 18:05:40 +03001286 Py_SETREF(Handlers[SIGINT].func, IntHandler);
Miss Islington (bot)623b4392018-06-01 03:50:24 -07001287 PyOS_setsig(SIGINT, signal_handler);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001288 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001289
1290#ifdef SIGHUP
Christian Heimes6782b142016-09-09 00:11:45 +02001291 if (PyModule_AddIntMacro(m, SIGHUP))
1292 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001293#endif
1294#ifdef SIGINT
Christian Heimes6782b142016-09-09 00:11:45 +02001295 if (PyModule_AddIntMacro(m, SIGINT))
1296 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001297#endif
Tim Peters1ce3cf72001-10-01 17:58:40 +00001298#ifdef SIGBREAK
Christian Heimes6782b142016-09-09 00:11:45 +02001299 if (PyModule_AddIntMacro(m, SIGBREAK))
1300 goto finally;
Tim Peters1ce3cf72001-10-01 17:58:40 +00001301#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001302#ifdef SIGQUIT
Christian Heimes6782b142016-09-09 00:11:45 +02001303 if (PyModule_AddIntMacro(m, SIGQUIT))
1304 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001305#endif
1306#ifdef SIGILL
Christian Heimes6782b142016-09-09 00:11:45 +02001307 if (PyModule_AddIntMacro(m, SIGILL))
1308 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001309#endif
1310#ifdef SIGTRAP
Christian Heimes6782b142016-09-09 00:11:45 +02001311 if (PyModule_AddIntMacro(m, SIGTRAP))
1312 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001313#endif
1314#ifdef SIGIOT
Christian Heimes6782b142016-09-09 00:11:45 +02001315 if (PyModule_AddIntMacro(m, SIGIOT))
1316 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001317#endif
1318#ifdef SIGABRT
Christian Heimes6782b142016-09-09 00:11:45 +02001319 if (PyModule_AddIntMacro(m, SIGABRT))
1320 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001321#endif
1322#ifdef SIGEMT
Christian Heimes6782b142016-09-09 00:11:45 +02001323 if (PyModule_AddIntMacro(m, SIGEMT))
1324 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001325#endif
1326#ifdef SIGFPE
Christian Heimes6782b142016-09-09 00:11:45 +02001327 if (PyModule_AddIntMacro(m, SIGFPE))
1328 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001329#endif
1330#ifdef SIGKILL
Christian Heimes6782b142016-09-09 00:11:45 +02001331 if (PyModule_AddIntMacro(m, SIGKILL))
1332 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001333#endif
1334#ifdef SIGBUS
Christian Heimes6782b142016-09-09 00:11:45 +02001335 if (PyModule_AddIntMacro(m, SIGBUS))
1336 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001337#endif
1338#ifdef SIGSEGV
Christian Heimes6782b142016-09-09 00:11:45 +02001339 if (PyModule_AddIntMacro(m, SIGSEGV))
1340 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001341#endif
1342#ifdef SIGSYS
Christian Heimes6782b142016-09-09 00:11:45 +02001343 if (PyModule_AddIntMacro(m, SIGSYS))
1344 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001345#endif
1346#ifdef SIGPIPE
Christian Heimes6782b142016-09-09 00:11:45 +02001347 if (PyModule_AddIntMacro(m, SIGPIPE))
1348 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001349#endif
1350#ifdef SIGALRM
Christian Heimes6782b142016-09-09 00:11:45 +02001351 if (PyModule_AddIntMacro(m, SIGALRM))
1352 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001353#endif
1354#ifdef SIGTERM
Christian Heimes6782b142016-09-09 00:11:45 +02001355 if (PyModule_AddIntMacro(m, SIGTERM))
1356 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001357#endif
1358#ifdef SIGUSR1
Christian Heimes6782b142016-09-09 00:11:45 +02001359 if (PyModule_AddIntMacro(m, SIGUSR1))
1360 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001361#endif
1362#ifdef SIGUSR2
Christian Heimes6782b142016-09-09 00:11:45 +02001363 if (PyModule_AddIntMacro(m, SIGUSR2))
1364 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001365#endif
1366#ifdef SIGCLD
Christian Heimes6782b142016-09-09 00:11:45 +02001367 if (PyModule_AddIntMacro(m, SIGCLD))
1368 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001369#endif
1370#ifdef SIGCHLD
Christian Heimes6782b142016-09-09 00:11:45 +02001371 if (PyModule_AddIntMacro(m, SIGCHLD))
1372 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001373#endif
1374#ifdef SIGPWR
Christian Heimes6782b142016-09-09 00:11:45 +02001375 if (PyModule_AddIntMacro(m, SIGPWR))
1376 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001377#endif
1378#ifdef SIGIO
Christian Heimes6782b142016-09-09 00:11:45 +02001379 if (PyModule_AddIntMacro(m, SIGIO))
1380 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001381#endif
1382#ifdef SIGURG
Christian Heimes6782b142016-09-09 00:11:45 +02001383 if (PyModule_AddIntMacro(m, SIGURG))
1384 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001385#endif
1386#ifdef SIGWINCH
Christian Heimes6782b142016-09-09 00:11:45 +02001387 if (PyModule_AddIntMacro(m, SIGWINCH))
1388 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001389#endif
1390#ifdef SIGPOLL
Christian Heimes6782b142016-09-09 00:11:45 +02001391 if (PyModule_AddIntMacro(m, SIGPOLL))
1392 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001393#endif
1394#ifdef SIGSTOP
Christian Heimes6782b142016-09-09 00:11:45 +02001395 if (PyModule_AddIntMacro(m, SIGSTOP))
1396 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001397#endif
1398#ifdef SIGTSTP
Christian Heimes6782b142016-09-09 00:11:45 +02001399 if (PyModule_AddIntMacro(m, SIGTSTP))
1400 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001401#endif
1402#ifdef SIGCONT
Christian Heimes6782b142016-09-09 00:11:45 +02001403 if (PyModule_AddIntMacro(m, SIGCONT))
1404 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001405#endif
1406#ifdef SIGTTIN
Christian Heimes6782b142016-09-09 00:11:45 +02001407 if (PyModule_AddIntMacro(m, SIGTTIN))
1408 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001409#endif
1410#ifdef SIGTTOU
Christian Heimes6782b142016-09-09 00:11:45 +02001411 if (PyModule_AddIntMacro(m, SIGTTOU))
1412 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001413#endif
1414#ifdef SIGVTALRM
Christian Heimes6782b142016-09-09 00:11:45 +02001415 if (PyModule_AddIntMacro(m, SIGVTALRM))
1416 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001417#endif
1418#ifdef SIGPROF
Christian Heimes6782b142016-09-09 00:11:45 +02001419 if (PyModule_AddIntMacro(m, SIGPROF))
1420 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001421#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001422#ifdef SIGXCPU
Christian Heimes6782b142016-09-09 00:11:45 +02001423 if (PyModule_AddIntMacro(m, SIGXCPU))
1424 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001425#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001426#ifdef SIGXFSZ
Christian Heimes6782b142016-09-09 00:11:45 +02001427 if (PyModule_AddIntMacro(m, SIGXFSZ))
1428 goto finally;
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001429#endif
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001430#ifdef SIGRTMIN
Christian Heimes6782b142016-09-09 00:11:45 +02001431 if (PyModule_AddIntMacro(m, SIGRTMIN))
1432 goto finally;
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001433#endif
1434#ifdef SIGRTMAX
Christian Heimes6782b142016-09-09 00:11:45 +02001435 if (PyModule_AddIntMacro(m, SIGRTMAX))
1436 goto finally;
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001437#endif
Martin v. Löwis175af252002-01-12 11:43:25 +00001438#ifdef SIGINFO
Christian Heimes6782b142016-09-09 00:11:45 +02001439 if (PyModule_AddIntMacro(m, SIGINFO))
1440 goto finally;
Martin v. Löwis175af252002-01-12 11:43:25 +00001441#endif
Martin v. Löwis823725e2008-03-24 13:39:54 +00001442
1443#ifdef ITIMER_REAL
Christian Heimes6782b142016-09-09 00:11:45 +02001444 if (PyModule_AddIntMacro(m, ITIMER_REAL))
1445 goto finally;
Martin v. Löwis823725e2008-03-24 13:39:54 +00001446#endif
1447#ifdef ITIMER_VIRTUAL
Christian Heimes6782b142016-09-09 00:11:45 +02001448 if (PyModule_AddIntMacro(m, ITIMER_VIRTUAL))
1449 goto finally;
Martin v. Löwis823725e2008-03-24 13:39:54 +00001450#endif
1451#ifdef ITIMER_PROF
Christian Heimes6782b142016-09-09 00:11:45 +02001452 if (PyModule_AddIntMacro(m, ITIMER_PROF))
1453 goto finally;
Martin v. Löwis823725e2008-03-24 13:39:54 +00001454#endif
1455
1456#if defined (HAVE_SETITIMER) || defined (HAVE_GETITIMER)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001457 ItimerError = PyErr_NewException("signal.ItimerError",
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +03001458 PyExc_OSError, NULL);
Neal Norwitz2f99b242008-08-24 05:48:10 +00001459 if (ItimerError != NULL)
Martin Panter6d57fe12016-09-17 03:26:16 +00001460 PyDict_SetItemString(d, "ItimerError", ItimerError);
Martin v. Löwis823725e2008-03-24 13:39:54 +00001461#endif
1462
Brian Curtineb24d742010-04-12 17:16:38 +00001463#ifdef CTRL_C_EVENT
Christian Heimes6782b142016-09-09 00:11:45 +02001464 if (PyModule_AddIntMacro(m, CTRL_C_EVENT))
1465 goto finally;
Brian Curtineb24d742010-04-12 17:16:38 +00001466#endif
1467
1468#ifdef CTRL_BREAK_EVENT
Christian Heimes6782b142016-09-09 00:11:45 +02001469 if (PyModule_AddIntMacro(m, CTRL_BREAK_EVENT))
1470 goto finally;
Brian Curtineb24d742010-04-12 17:16:38 +00001471#endif
1472
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001473#ifdef MS_WINDOWS
1474 /* Create manual-reset event, initially unset */
1475 sigint_event = CreateEvent(NULL, TRUE, FALSE, FALSE);
1476#endif
1477
Martin v. Löwis1a214512008-06-11 05:26:20 +00001478 if (PyErr_Occurred()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001479 Py_DECREF(m);
1480 m = NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001481 }
Barry Warsaw92971171997-01-03 00:14:25 +00001482
Barry Warsaw92971171997-01-03 00:14:25 +00001483 finally:
Martin v. Löwis1a214512008-06-11 05:26:20 +00001484 return m;
Guido van Rossum08c16611997-08-02 03:01:42 +00001485}
1486
1487static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001488finisignal(void)
Guido van Rossum08c16611997-08-02 03:01:42 +00001489{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001490 int i;
1491 PyObject *func;
Guido van Rossum08c16611997-08-02 03:01:42 +00001492
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001493 for (i = 1; i < NSIG; i++) {
1494 func = Handlers[i].func;
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001495 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001496 Handlers[i].func = NULL;
Miss Islington (bot)623b4392018-06-01 03:50:24 -07001497 if (func != NULL && func != Py_None &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001498 func != DefaultHandler && func != IgnoreHandler)
1499 PyOS_setsig(i, SIG_DFL);
1500 Py_XDECREF(func);
1501 }
Guido van Rossum08c16611997-08-02 03:01:42 +00001502
Serhiy Storchaka505ff752014-02-09 13:33:53 +02001503 Py_CLEAR(IntHandler);
1504 Py_CLEAR(DefaultHandler);
1505 Py_CLEAR(IgnoreHandler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001506}
1507
Barry Warsaw92971171997-01-03 00:14:25 +00001508
Barry Warsaw92971171997-01-03 00:14:25 +00001509/* Declared in pyerrors.h */
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001510int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001511PyErr_CheckSignals(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001512{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001513 int i;
1514 PyObject *f;
Barry Warsaw92971171997-01-03 00:14:25 +00001515
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001516 if (!_Py_atomic_load(&is_tripped))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001517 return 0;
Christian Heimesb76922a2007-12-11 01:06:40 +00001518
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001519 if (PyThread_get_thread_ident() != main_thread)
1520 return 0;
Christian Heimesb76922a2007-12-11 01:06:40 +00001521
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001522 /*
1523 * The is_tripped variable is meant to speed up the calls to
1524 * PyErr_CheckSignals (both directly or via pending calls) when no
1525 * signal has arrived. This variable is set to 1 when a signal arrives
1526 * and it is set to 0 here, when we know some signals arrived. This way
1527 * we can run the registered handlers with no signals blocked.
1528 *
1529 * NOTE: with this approach we can have a situation where is_tripped is
1530 * 1 but we have no more signals to handle (Handlers[i].tripped
1531 * is 0 for every signal i). This won't do us any harm (except
1532 * we're gonna spent some cycles for nothing). This happens when
1533 * we receive a signal i after we zero is_tripped and before we
1534 * check Handlers[i].tripped.
1535 */
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001536 _Py_atomic_store(&is_tripped, 0);
Christian Heimesb76922a2007-12-11 01:06:40 +00001537
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001538 if (!(f = (PyObject *)PyEval_GetFrame()))
1539 f = Py_None;
Christian Heimesb76922a2007-12-11 01:06:40 +00001540
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001541 for (i = 1; i < NSIG; i++) {
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001542 if (_Py_atomic_load_relaxed(&Handlers[i].tripped)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001543 PyObject *result = NULL;
1544 PyObject *arglist = Py_BuildValue("(iO)", i, f);
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001545 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Barry Warsaw92971171997-01-03 00:14:25 +00001546
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001547 if (arglist) {
1548 result = PyEval_CallObject(Handlers[i].func,
1549 arglist);
1550 Py_DECREF(arglist);
1551 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +02001552 if (!result) {
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001553 _Py_atomic_store(&is_tripped, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001554 return -1;
Antoine Pitrouc08177a2017-06-28 23:29:29 +02001555 }
Barry Warsaw92971171997-01-03 00:14:25 +00001556
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001557 Py_DECREF(result);
1558 }
1559 }
Christian Heimesb76922a2007-12-11 01:06:40 +00001560
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001561 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001562}
1563
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +00001564
Barry Warsaw92971171997-01-03 00:14:25 +00001565/* Replacements for intrcheck.c functionality
1566 * Declared in pyerrors.h
1567 */
1568void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001569PyErr_SetInterrupt(void)
Barry Warsaw92971171997-01-03 00:14:25 +00001570{
Victor Stinner6c9b35b2011-04-18 16:25:56 +02001571 trip_signal(SIGINT);
Barry Warsaw92971171997-01-03 00:14:25 +00001572}
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001573
1574void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001575PyOS_InitInterrupts(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001576{
Giampaolo Rodola'e09fb712014-04-04 15:34:17 +02001577 PyObject *m = PyImport_ImportModule("_signal");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001578 if (m) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001579 Py_DECREF(m);
1580 }
Guido van Rossum08c16611997-08-02 03:01:42 +00001581}
1582
1583void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001584PyOS_FiniInterrupts(void)
Guido van Rossum08c16611997-08-02 03:01:42 +00001585{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001586 finisignal();
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001587}
1588
1589int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001590PyOS_InterruptOccurred(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001591{
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001592 if (_Py_atomic_load_relaxed(&Handlers[SIGINT].tripped)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001593 if (PyThread_get_thread_ident() != main_thread)
1594 return 0;
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001595 _Py_atomic_store_relaxed(&Handlers[SIGINT].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001596 return 1;
1597 }
1598 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001599}
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001600
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001601static void
1602_clear_pending_signals(void)
1603{
1604 int i;
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001605 if (!_Py_atomic_load(&is_tripped))
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001606 return;
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001607 _Py_atomic_store(&is_tripped, 0);
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001608 for (i = 1; i < NSIG; ++i) {
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001609 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001610 }
1611}
1612
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001613void
Antoine Pitrou346cbd32017-05-27 17:50:54 +02001614_PySignal_AfterFork(void)
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001615{
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001616 /* Clear the signal flags after forking so that they aren't handled
1617 * in both processes if they came in just before the fork() but before
1618 * the interpreter had an opportunity to call the handlers. issue9535. */
1619 _clear_pending_signals();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001620 main_thread = PyThread_get_thread_ident();
1621 main_pid = getpid();
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001622}
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001623
1624int
1625_PyOS_IsMainThread(void)
1626{
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001627 return PyThread_get_thread_ident() == main_thread;
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001628}
1629
1630#ifdef MS_WINDOWS
1631void *_PyOS_SigintEvent(void)
1632{
1633 /* Returns a manual-reset event which gets tripped whenever
1634 SIGINT is received.
1635
1636 Python.h does not include windows.h so we do cannot use HANDLE
1637 as the return type of this function. We use void* instead. */
1638 return sigint_event;
1639}
1640#endif