blob: 9d49cbd14400972c0214e57b1818c0fadf5c749e [file] [log] [blame]
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002/* Signal module -- many thanks to Lance Ellinghaus */
Guido van Rossum398d9fe1994-05-11 08:59:13 +00003
Guido van Rossum644a12b1997-04-09 19:24:53 +00004/* XXX Signals should be recorded per thread, now we have thread state. */
5
Guido van Rossum602099a1994-09-14 13:32:22 +00006#include "Python.h"
Victor Stinner27e2d1f2018-11-01 00:52:28 +01007#include "pycore_atomic.h"
Victor Stinner31368a42018-10-30 15:14:25 +01008
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02009#ifndef MS_WINDOWS
10#include "posixmodule.h"
11#endif
Victor Stinner11517102014-07-29 23:31:34 +020012#ifdef MS_WINDOWS
13#include "socketmodule.h" /* needed for SOCKET_T */
14#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +000015
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000016#ifdef MS_WINDOWS
Antoine Pitrou7faf7052013-03-31 22:48:04 +020017#include <windows.h>
Benjamin Peterson2614cda2010-03-21 22:36:19 +000018#ifdef HAVE_PROCESS_H
Guido van Rossum644a12b1997-04-09 19:24:53 +000019#include <process.h>
20#endif
Benjamin Peterson2614cda2010-03-21 22:36:19 +000021#endif
Guido van Rossum644a12b1997-04-09 19:24:53 +000022
Benjamin Peterson2614cda2010-03-21 22:36:19 +000023#ifdef HAVE_SIGNAL_H
Guido van Rossum398d9fe1994-05-11 08:59:13 +000024#include <signal.h>
Benjamin Peterson2614cda2010-03-21 22:36:19 +000025#endif
26#ifdef HAVE_SYS_STAT_H
Christian Heimes5fb7c2a2007-12-24 08:52:31 +000027#include <sys/stat.h>
Benjamin Peterson2614cda2010-03-21 22:36:19 +000028#endif
Martin v. Löwis3bf3cc02008-03-24 14:05:07 +000029#ifdef HAVE_SYS_TIME_H
Martin v. Löwis823725e2008-03-24 13:39:54 +000030#include <sys/time.h>
Martin v. Löwis3bf3cc02008-03-24 14:05:07 +000031#endif
Christian Heimes5fb7c2a2007-12-24 08:52:31 +000032
Victor Stinnera9293352011-04-30 15:21:58 +020033#if defined(HAVE_PTHREAD_SIGMASK) && !defined(HAVE_BROKEN_PTHREAD_SIGMASK)
34# define PYPTHREAD_SIGMASK
35#endif
36
37#if defined(PYPTHREAD_SIGMASK) && defined(HAVE_PTHREAD_H)
38# include <pthread.h>
39#endif
40
Guido van Rossumbb4ba121994-06-23 11:25:45 +000041#ifndef SIG_ERR
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +000042#define SIG_ERR ((PyOS_sighandler_t)(-1))
Guido van Rossumbb4ba121994-06-23 11:25:45 +000043#endif
44
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000045#ifndef NSIG
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000046# if defined(_NSIG)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000047# define NSIG _NSIG /* For BSD/SysV */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000048# elif defined(_SIGMAX)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000049# define NSIG (_SIGMAX + 1) /* For QNX */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000050# elif defined(SIGMAX)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000051# define NSIG (SIGMAX + 1) /* For djgpp */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000052# else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000053# define NSIG 64 /* Use a reasonable default value */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000054# endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000055#endif
56
Tal Einatc7027b72015-05-16 14:14:49 +030057#include "clinic/signalmodule.c.h"
58
59/*[clinic input]
60module signal
61[clinic start generated code]*/
62/*[clinic end generated code: output=da39a3ee5e6b4b0d input=b0301a3bde5fe9d3]*/
63
Serhiy Storchakad54cfb12018-05-08 07:48:50 +030064/*[python input]
65
66class sigset_t_converter(CConverter):
67 type = 'sigset_t'
68 converter = '_Py_Sigset_Converter'
69
70[python start generated code]*/
71/*[python end generated code: output=da39a3ee5e6b4b0d input=b5689d14466b6823]*/
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000072
Guido van Rossumbb4ba121994-06-23 11:25:45 +000073/*
74 NOTES ON THE INTERACTION BETWEEN SIGNALS AND THREADS
75
76 When threads are supported, we want the following semantics:
77
78 - only the main thread can set a signal handler
79 - any thread can get a signal handler
80 - signals are only delivered to the main thread
81
82 I.e. we don't support "synchronous signals" like SIGFPE (catching
83 this doesn't make much sense in Python anyway) nor do we support
84 signals as a means of inter-thread communication, since not all
85 thread implementations support that (at least our thread library
86 doesn't).
87
88 We still have the problem that in some implementations signals
89 generated by the keyboard (e.g. SIGINT) are delivered to all
90 threads (e.g. SGI), while in others (e.g. Solaris) such signals are
91 delivered to one random thread (an intermediate possibility would
Guido van Rossuma3c04b01995-01-12 11:29:01 +000092 be to deliver it to the main thread -- POSIX?). For now, we have
Guido van Rossumbb4ba121994-06-23 11:25:45 +000093 a working implementation that works in all three cases -- the
94 handler ignores signals if getpid() isn't the same as in the main
95 thread. XXX This is a hack.
Guido van Rossumbb4ba121994-06-23 11:25:45 +000096*/
97
Guido van Rossum295b8e51997-06-06 21:16:41 +000098#include <sys/types.h> /* For pid_t */
Guido van Rossum49b56061998-10-01 20:42:43 +000099#include "pythread.h"
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +0200100static unsigned long main_thread;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000101static pid_t main_pid;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000102
Victor Stinner2ec6b172011-05-15 10:21:59 +0200103static volatile struct {
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +0200104 _Py_atomic_int tripped;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000105 PyObject *func;
Barry Warsaw92971171997-01-03 00:14:25 +0000106} Handlers[NSIG];
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000107
Victor Stinner11517102014-07-29 23:31:34 +0200108#ifdef MS_WINDOWS
109#define INVALID_FD ((SOCKET_T)-1)
110
111static volatile struct {
112 SOCKET_T fd;
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800113 int warn_on_full_buffer;
Victor Stinner11517102014-07-29 23:31:34 +0200114 int use_send;
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800115} wakeup = {.fd = INVALID_FD, .warn_on_full_buffer = 1, .use_send = 0};
Victor Stinner11517102014-07-29 23:31:34 +0200116#else
117#define INVALID_FD (-1)
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800118static volatile struct {
119 sig_atomic_t fd;
120 int warn_on_full_buffer;
121} wakeup = {.fd = INVALID_FD, .warn_on_full_buffer = 1};
Victor Stinner11517102014-07-29 23:31:34 +0200122#endif
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000123
Christian Heimesb76922a2007-12-11 01:06:40 +0000124/* Speed up sigcheck() when none tripped */
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +0200125static _Py_atomic_int is_tripped;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000126
Barry Warsaw92971171997-01-03 00:14:25 +0000127static PyObject *DefaultHandler;
128static PyObject *IgnoreHandler;
129static PyObject *IntHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000130
Antoine Pitrou6dd381e2011-11-21 21:26:56 +0100131#ifdef MS_WINDOWS
132static HANDLE sigint_event = NULL;
133#endif
134
Martin v. Löwis823725e2008-03-24 13:39:54 +0000135#ifdef HAVE_GETITIMER
136static PyObject *ItimerError;
137
Victor Stinneref611c92017-10-13 13:49:43 -0700138/* auxiliary functions for setitimer */
139static int
140timeval_from_double(PyObject *obj, struct timeval *tv)
Martin v. Löwis823725e2008-03-24 13:39:54 +0000141{
Victor Stinneref611c92017-10-13 13:49:43 -0700142 if (obj == NULL) {
143 tv->tv_sec = 0;
144 tv->tv_usec = 0;
145 return 0;
Antoine Pitrou729780a2017-06-30 10:01:05 +0200146 }
Victor Stinneref611c92017-10-13 13:49:43 -0700147
148 _PyTime_t t;
149 if (_PyTime_FromSecondsObject(&t, obj, _PyTime_ROUND_CEILING) < 0) {
150 return -1;
151 }
152 return _PyTime_AsTimeval(t, tv, _PyTime_ROUND_CEILING);
Martin v. Löwis823725e2008-03-24 13:39:54 +0000153}
154
Christian Heimes1a8501c2008-10-02 19:56:01 +0000155Py_LOCAL_INLINE(double)
Martin v. Löwis823725e2008-03-24 13:39:54 +0000156double_from_timeval(struct timeval *tv)
157{
158 return tv->tv_sec + (double)(tv->tv_usec / 1000000.0);
159}
160
161static PyObject *
162itimer_retval(struct itimerval *iv)
163{
164 PyObject *r, *v;
165
166 r = PyTuple_New(2);
167 if (r == NULL)
Martin Panter6d57fe12016-09-17 03:26:16 +0000168 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000169
170 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_value)))) {
Martin Panter6d57fe12016-09-17 03:26:16 +0000171 Py_DECREF(r);
172 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000173 }
174
175 PyTuple_SET_ITEM(r, 0, v);
176
177 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_interval)))) {
Martin Panter6d57fe12016-09-17 03:26:16 +0000178 Py_DECREF(r);
179 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000180 }
181
182 PyTuple_SET_ITEM(r, 1, v);
183
184 return r;
185}
186#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000187
Guido van Rossume4485b01994-09-07 14:32:49 +0000188static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000189signal_default_int_handler(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000190{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000191 PyErr_SetNone(PyExc_KeyboardInterrupt);
192 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000193}
194
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000195PyDoc_STRVAR(default_int_handler_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000196"default_int_handler(...)\n\
197\n\
Michael W. Hudson24ec2112004-06-17 15:55:53 +0000198The default handler for SIGINT installed by Python.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000199It raises KeyboardInterrupt.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000200
Thomas Wouters0796b002000-07-22 23:49:30 +0000201
202static int
Victor Stinner11517102014-07-29 23:31:34 +0200203report_wakeup_write_error(void *data)
Antoine Pitrou6f6ec372013-08-17 20:27:56 +0200204{
Eric Snowfdf282d2019-01-11 14:26:55 -0700205 PyObject *exc, *val, *tb;
Antoine Pitrou6f6ec372013-08-17 20:27:56 +0200206 int save_errno = errno;
Benjamin Petersonca470632016-09-06 13:47:26 -0700207 errno = (int) (intptr_t) data;
Eric Snowfdf282d2019-01-11 14:26:55 -0700208 PyErr_Fetch(&exc, &val, &tb);
Antoine Pitrou6f6ec372013-08-17 20:27:56 +0200209 PyErr_SetFromErrno(PyExc_OSError);
210 PySys_WriteStderr("Exception ignored when trying to write to the "
211 "signal wakeup fd:\n");
212 PyErr_WriteUnraisable(NULL);
Eric Snowfdf282d2019-01-11 14:26:55 -0700213 PyErr_Restore(exc, val, tb);
Antoine Pitrou6f6ec372013-08-17 20:27:56 +0200214 errno = save_errno;
215 return 0;
216}
217
Victor Stinner11517102014-07-29 23:31:34 +0200218#ifdef MS_WINDOWS
219static int
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800220report_wakeup_send_error(void* data)
Victor Stinner11517102014-07-29 23:31:34 +0200221{
Eric Snowfdf282d2019-01-11 14:26:55 -0700222 PyObject *exc, *val, *tb;
223 PyErr_Fetch(&exc, &val, &tb);
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800224 /* PyErr_SetExcFromWindowsErr() invokes FormatMessage() which
225 recognizes the error codes used by both GetLastError() and
226 WSAGetLastError */
227 PyErr_SetExcFromWindowsErr(PyExc_OSError, (int) (intptr_t) data);
Victor Stinner11517102014-07-29 23:31:34 +0200228 PySys_WriteStderr("Exception ignored when trying to send to the "
229 "signal wakeup fd:\n");
230 PyErr_WriteUnraisable(NULL);
Eric Snowfdf282d2019-01-11 14:26:55 -0700231 PyErr_Restore(exc, val, tb);
Victor Stinner11517102014-07-29 23:31:34 +0200232 return 0;
233}
234#endif /* MS_WINDOWS */
235
Tim Peters4f1b2082000-07-23 21:18:09 +0000236static void
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200237trip_signal(int sig_num)
238{
Victor Stinnerd49b1f12011-05-08 02:03:15 +0200239 unsigned char byte;
Victor Stinner11517102014-07-29 23:31:34 +0200240 int fd;
241 Py_ssize_t rc;
Victor Stinnerc13ef662011-05-25 02:35:58 +0200242
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +0200243 _Py_atomic_store_relaxed(&Handlers[sig_num].tripped, 1);
Victor Stinner11517102014-07-29 23:31:34 +0200244
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200245 /* Set is_tripped after setting .tripped, as it gets
246 cleared in PyErr_CheckSignals() before .tripped. */
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +0200247 _Py_atomic_store(&is_tripped, 1);
248
249 /* Notify ceval.c */
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200250 _PyEval_SignalReceived();
Nathaniel J. Smith4ae01492017-05-16 14:12:11 -0700251
252 /* And then write to the wakeup fd *after* setting all the globals and
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200253 doing the _PyEval_SignalReceived. We used to write to the wakeup fd
254 and then set the flag, but this allowed the following sequence of events
255 (especially on windows, where trip_signal may run in a new thread):
Nathaniel J. Smith4ae01492017-05-16 14:12:11 -0700256
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800257 - main thread blocks on select([wakeup.fd], ...)
Nathaniel J. Smith4ae01492017-05-16 14:12:11 -0700258 - signal arrives
259 - trip_signal writes to the wakeup fd
260 - the main thread wakes up
261 - the main thread checks the signal flags, sees that they're unset
262 - the main thread empties the wakeup fd
263 - the main thread goes back to sleep
264 - trip_signal sets the flags to request the Python-level signal handler
265 be run
266 - the main thread doesn't notice, because it's asleep
267
268 See bpo-30038 for more details.
269 */
270
Victor Stinner11517102014-07-29 23:31:34 +0200271#ifdef MS_WINDOWS
272 fd = Py_SAFE_DOWNCAST(wakeup.fd, SOCKET_T, int);
273#else
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800274 fd = wakeup.fd;
Victor Stinner11517102014-07-29 23:31:34 +0200275#endif
276
277 if (fd != INVALID_FD) {
Victor Stinnerc13ef662011-05-25 02:35:58 +0200278 byte = (unsigned char)sig_num;
Victor Stinner11517102014-07-29 23:31:34 +0200279#ifdef MS_WINDOWS
280 if (wakeup.use_send) {
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800281 rc = send(fd, &byte, 1, 0);
Victor Stinner11517102014-07-29 23:31:34 +0200282
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800283 if (rc < 0) {
284 int last_error = GetLastError();
285 if (wakeup.warn_on_full_buffer ||
286 last_error != WSAEWOULDBLOCK)
287 {
288 /* Py_AddPendingCall() isn't signal-safe, but we
289 still use it for this exceptional case. */
290 Py_AddPendingCall(report_wakeup_send_error,
291 (void *)(intptr_t) last_error);
292 }
Victor Stinner11517102014-07-29 23:31:34 +0200293 }
294 }
295 else
296#endif
297 {
Victor Stinnere72fe392015-04-01 18:35:22 +0200298 /* _Py_write_noraise() retries write() if write() is interrupted by
299 a signal (fails with EINTR). */
300 rc = _Py_write_noraise(fd, &byte, 1);
Victor Stinner11517102014-07-29 23:31:34 +0200301
302 if (rc < 0) {
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800303 if (wakeup.warn_on_full_buffer ||
304 (errno != EWOULDBLOCK && errno != EAGAIN))
305 {
306 /* Py_AddPendingCall() isn't signal-safe, but we
307 still use it for this exceptional case. */
308 Py_AddPendingCall(report_wakeup_write_error,
309 (void *)(intptr_t)errno);
310 }
Victor Stinner11517102014-07-29 23:31:34 +0200311 }
312 }
Victor Stinnerc13ef662011-05-25 02:35:58 +0200313 }
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200314}
315
316static void
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000317signal_handler(int sig_num)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000318{
Antoine Pitrou39a65912010-11-05 19:47:27 +0000319 int save_errno = errno;
320
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000321 /* See NOTES section above */
Antoine Pitrou39a65912010-11-05 19:47:27 +0000322 if (getpid() == main_pid)
Antoine Pitrou39a65912010-11-05 19:47:27 +0000323 {
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200324 trip_signal(sig_num);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000325 }
Antoine Pitrou39a65912010-11-05 19:47:27 +0000326
Jean-Paul Calderone6f137ca2010-05-09 03:18:57 +0000327#ifndef HAVE_SIGACTION
Antoine Pitrou39a65912010-11-05 19:47:27 +0000328#ifdef SIGCHLD
329 /* To avoid infinite recursion, this signal remains
330 reset until explicit re-instated.
331 Don't clear the 'func' field as it is our pointer
332 to the Python handler... */
333 if (sig_num != SIGCHLD)
334#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000335 /* If the handler was not set up with sigaction, reinstall it. See
Nick Coghland6009512014-11-20 21:39:37 +1000336 * Python/pylifecycle.c for the implementation of PyOS_setsig which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000337 * makes this true. See also issue8354. */
338 PyOS_setsig(sig_num, signal_handler);
Jean-Paul Calderone6f137ca2010-05-09 03:18:57 +0000339#endif
Antoine Pitrou39a65912010-11-05 19:47:27 +0000340
341 /* Issue #10311: asynchronously executing signal handlers should not
342 mutate errno under the feet of unsuspecting C code. */
343 errno = save_errno;
Antoine Pitrou6dd381e2011-11-21 21:26:56 +0100344
345#ifdef MS_WINDOWS
346 if (sig_num == SIGINT)
347 SetEvent(sigint_event);
348#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000349}
Guido van Rossume4485b01994-09-07 14:32:49 +0000350
Guido van Rossum06d511d1995-03-10 15:13:48 +0000351
Guido van Rossum1171ee61997-08-22 20:42:00 +0000352#ifdef HAVE_ALARM
Tal Einatc7027b72015-05-16 14:14:49 +0300353
354/*[clinic input]
355signal.alarm -> long
356
357 seconds: int
358 /
359
360Arrange for SIGALRM to arrive after the given number of seconds.
361[clinic start generated code]*/
362
363static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300364signal_alarm_impl(PyObject *module, int seconds)
365/*[clinic end generated code: output=144232290814c298 input=0d5e97e0e6f39e86]*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000366{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000367 /* alarm() returns the number of seconds remaining */
Tal Einatc7027b72015-05-16 14:14:49 +0300368 return (long)alarm(seconds);
Guido van Rossumaa0f4c71994-08-23 13:49:37 +0000369}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000370
Guido van Rossum06d511d1995-03-10 15:13:48 +0000371#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000372
Guido van Rossum1171ee61997-08-22 20:42:00 +0000373#ifdef HAVE_PAUSE
Tal Einatc7027b72015-05-16 14:14:49 +0300374
375/*[clinic input]
376signal.pause
377
378Wait until a signal arrives.
379[clinic start generated code]*/
380
Guido van Rossuma597dde1995-01-10 20:56:29 +0000381static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300382signal_pause_impl(PyObject *module)
383/*[clinic end generated code: output=391656788b3c3929 input=f03de0f875752062]*/
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000384{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000385 Py_BEGIN_ALLOW_THREADS
386 (void)pause();
387 Py_END_ALLOW_THREADS
388 /* make sure that any exceptions that got raised are propagated
389 * back into Python
390 */
391 if (PyErr_CheckSignals())
392 return NULL;
Barry Warsaw92971171997-01-03 00:14:25 +0000393
Tal Einatc7027b72015-05-16 14:14:49 +0300394 Py_RETURN_NONE;
Guido van Rossume4485b01994-09-07 14:32:49 +0000395}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000396
Guido van Rossum06d511d1995-03-10 15:13:48 +0000397#endif
Guido van Rossume4485b01994-09-07 14:32:49 +0000398
Vladimir Matveevc24c6c22019-01-08 01:58:25 -0800399/*[clinic input]
400signal.raise_signal
401
402 signalnum: int
403 /
404
405Send a signal to the executing process.
406[clinic start generated code]*/
407
408static PyObject *
409signal_raise_signal_impl(PyObject *module, int signalnum)
410/*[clinic end generated code: output=e2b014220aa6111d input=e90c0f9a42358de6]*/
411{
412 int err;
413 Py_BEGIN_ALLOW_THREADS
414 _Py_BEGIN_SUPPRESS_IPH
415 err = raise(signalnum);
416 _Py_END_SUPPRESS_IPH
417 Py_END_ALLOW_THREADS
418
419 if (err) {
420 return PyErr_SetFromErrno(PyExc_OSError);
421 }
422 Py_RETURN_NONE;
423}
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000424
Tal Einatc7027b72015-05-16 14:14:49 +0300425/*[clinic input]
426signal.signal
427
428 signalnum: int
429 handler: object
430 /
431
432Set the action for the given signal.
433
434The action can be SIG_DFL, SIG_IGN, or a callable Python object.
435The previous action is returned. See getsignal() for possible return values.
436
437*** IMPORTANT NOTICE ***
438A signal handler function is called with two arguments:
439the first is the signal number, the second is the interrupted stack frame.
440[clinic start generated code]*/
441
Guido van Rossume4485b01994-09-07 14:32:49 +0000442static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300443signal_signal_impl(PyObject *module, int signalnum, PyObject *handler)
444/*[clinic end generated code: output=b44cfda43780f3a1 input=deee84af5fa0432c]*/
Guido van Rossume4485b01994-09-07 14:32:49 +0000445{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000446 PyObject *old_handler;
447 void (*func)(int);
Brian Curtinef9efbd2010-08-06 19:27:32 +0000448#ifdef MS_WINDOWS
Tal Einatc7027b72015-05-16 14:14:49 +0300449 /* Validate that signalnum is one of the allowable signals */
450 switch (signalnum) {
Brian Curtinc734b312010-09-06 16:04:10 +0000451 case SIGABRT: break;
Brian Curtin9e88b5a2010-10-01 14:49:24 +0000452#ifdef SIGBREAK
453 /* Issue #10003: SIGBREAK is not documented as permitted, but works
454 and corresponds to CTRL_BREAK_EVENT. */
455 case SIGBREAK: break;
456#endif
Brian Curtinc734b312010-09-06 16:04:10 +0000457 case SIGFPE: break;
458 case SIGILL: break;
459 case SIGINT: break;
460 case SIGSEGV: break;
461 case SIGTERM: break;
462 default:
463 PyErr_SetString(PyExc_ValueError, "invalid signal value");
464 return NULL;
Brian Curtinef9efbd2010-08-06 19:27:32 +0000465 }
466#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000467 if (PyThread_get_thread_ident() != main_thread) {
468 PyErr_SetString(PyExc_ValueError,
469 "signal only works in main thread");
470 return NULL;
471 }
Tal Einatc7027b72015-05-16 14:14:49 +0300472 if (signalnum < 1 || signalnum >= NSIG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000473 PyErr_SetString(PyExc_ValueError,
474 "signal number out of range");
475 return NULL;
476 }
Tal Einatc7027b72015-05-16 14:14:49 +0300477 if (handler == IgnoreHandler)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000478 func = SIG_IGN;
Tal Einatc7027b72015-05-16 14:14:49 +0300479 else if (handler == DefaultHandler)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000480 func = SIG_DFL;
Tal Einatc7027b72015-05-16 14:14:49 +0300481 else if (!PyCallable_Check(handler)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000482 PyErr_SetString(PyExc_TypeError,
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000483"signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000484 return NULL;
485 }
486 else
487 func = signal_handler;
Antoine Pitrouf6f90ff2017-11-03 19:58:46 +0100488 /* Check for pending signals before changing signal handler */
489 if (PyErr_CheckSignals()) {
490 return NULL;
491 }
Tal Einatc7027b72015-05-16 14:14:49 +0300492 if (PyOS_setsig(signalnum, func) == SIG_ERR) {
Victor Stinner388196e2011-05-10 17:13:00 +0200493 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000494 return NULL;
495 }
Tal Einatc7027b72015-05-16 14:14:49 +0300496 old_handler = Handlers[signalnum].func;
Tal Einatc7027b72015-05-16 14:14:49 +0300497 Py_INCREF(handler);
498 Handlers[signalnum].func = handler;
Antoine Pitrouc8c952c2013-05-04 23:16:59 +0200499 if (old_handler != NULL)
500 return old_handler;
501 else
502 Py_RETURN_NONE;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000503}
504
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000505
Tal Einatc7027b72015-05-16 14:14:49 +0300506/*[clinic input]
507signal.getsignal
508
509 signalnum: int
510 /
511
512Return the current action for the given signal.
513
514The return value can be:
515 SIG_IGN -- if the signal is being ignored
516 SIG_DFL -- if the default action for the signal is in effect
517 None -- if an unknown handler is in effect
518 anything else -- the callable Python object used as a handler
519[clinic start generated code]*/
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000520
Guido van Rossume4485b01994-09-07 14:32:49 +0000521static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300522signal_getsignal_impl(PyObject *module, int signalnum)
523/*[clinic end generated code: output=35b3e0e796fd555e input=ac23a00f19dfa509]*/
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000524{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000525 PyObject *old_handler;
Tal Einatc7027b72015-05-16 14:14:49 +0300526 if (signalnum < 1 || signalnum >= NSIG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000527 PyErr_SetString(PyExc_ValueError,
528 "signal number out of range");
529 return NULL;
530 }
Tal Einatc7027b72015-05-16 14:14:49 +0300531 old_handler = Handlers[signalnum].func;
Antoine Pitrouc8c952c2013-05-04 23:16:59 +0200532 if (old_handler != NULL) {
533 Py_INCREF(old_handler);
534 return old_handler;
535 }
536 else {
537 Py_RETURN_NONE;
538 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000539}
540
Antoine Pietri5d2a27d2018-03-12 14:42:34 +0100541
542/*[clinic input]
543signal.strsignal
544
545 signalnum: int
546 /
547
548Return the system description of the given signal.
549
550The return values can be such as "Interrupt", "Segmentation fault", etc.
551Returns None if the signal is not recognized.
552[clinic start generated code]*/
553
554static PyObject *
555signal_strsignal_impl(PyObject *module, int signalnum)
556/*[clinic end generated code: output=44e12e1e3b666261 input=b77914b03f856c74]*/
557{
558 char *res;
559
560 if (signalnum < 1 || signalnum >= NSIG) {
561 PyErr_SetString(PyExc_ValueError,
562 "signal number out of range");
563 return NULL;
564 }
565
Michael Osipov48ce4892018-08-23 15:27:19 +0200566#ifndef HAVE_STRSIGNAL
Antoine Pietri5d2a27d2018-03-12 14:42:34 +0100567 switch (signalnum) {
Michael Osipov48ce4892018-08-23 15:27:19 +0200568 /* Though being a UNIX, HP-UX does not provide strsignal(3). */
569#ifndef MS_WINDOWS
570 case SIGHUP:
571 res = "Hangup";
572 break;
573 case SIGALRM:
574 res = "Alarm clock";
575 break;
576 case SIGPIPE:
577 res = "Broken pipe";
578 break;
579 case SIGQUIT:
580 res = "Quit";
581 break;
582 case SIGCHLD:
583 res = "Child exited";
584 break;
585#endif
586 /* Custom redefinition of POSIX signals allowed on Windows. */
Antoine Pietri5d2a27d2018-03-12 14:42:34 +0100587 case SIGINT:
588 res = "Interrupt";
589 break;
590 case SIGILL:
591 res = "Illegal instruction";
592 break;
593 case SIGABRT:
594 res = "Aborted";
595 break;
596 case SIGFPE:
597 res = "Floating point exception";
598 break;
599 case SIGSEGV:
600 res = "Segmentation fault";
601 break;
602 case SIGTERM:
603 res = "Terminated";
604 break;
605 default:
606 Py_RETURN_NONE;
607 }
608#else
609 errno = 0;
610 res = strsignal(signalnum);
611
612 if (errno || res == NULL || strstr(res, "Unknown signal") != NULL)
613 Py_RETURN_NONE;
614#endif
615
616 return Py_BuildValue("s", res);
617}
618
Christian Heimes8640e742008-02-23 16:23:06 +0000619#ifdef HAVE_SIGINTERRUPT
Tal Einatc7027b72015-05-16 14:14:49 +0300620
621/*[clinic input]
622signal.siginterrupt
623
624 signalnum: int
625 flag: int
626 /
627
628Change system call restart behaviour.
629
630If flag is False, system calls will be restarted when interrupted by
631signal sig, else system calls will be interrupted.
632[clinic start generated code]*/
Christian Heimes8640e742008-02-23 16:23:06 +0000633
634static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300635signal_siginterrupt_impl(PyObject *module, int signalnum, int flag)
636/*[clinic end generated code: output=063816243d85dd19 input=4160acacca3e2099]*/
Christian Heimes8640e742008-02-23 16:23:06 +0000637{
Tal Einatc7027b72015-05-16 14:14:49 +0300638 if (signalnum < 1 || signalnum >= NSIG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000639 PyErr_SetString(PyExc_ValueError,
640 "signal number out of range");
641 return NULL;
642 }
Tal Einatc7027b72015-05-16 14:14:49 +0300643 if (siginterrupt(signalnum, flag)<0) {
Victor Stinner388196e2011-05-10 17:13:00 +0200644 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000645 return NULL;
646 }
Tal Einatc7027b72015-05-16 14:14:49 +0300647 Py_RETURN_NONE;
Christian Heimes8640e742008-02-23 16:23:06 +0000648}
649
650#endif
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000651
Tal Einatc7027b72015-05-16 14:14:49 +0300652
653static PyObject*
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800654signal_set_wakeup_fd(PyObject *self, PyObject *args, PyObject *kwds)
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000655{
Victor Stinnere134a7f2015-03-30 10:09:31 +0200656 struct _Py_stat_struct status;
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800657 static char *kwlist[] = {
658 "", "warn_on_full_buffer", NULL,
659 };
660 int warn_on_full_buffer = 1;
Victor Stinner11517102014-07-29 23:31:34 +0200661#ifdef MS_WINDOWS
662 PyObject *fdobj;
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100663 SOCKET_T sockfd, old_sockfd;
Victor Stinner11517102014-07-29 23:31:34 +0200664 int res;
665 int res_size = sizeof res;
666 PyObject *mod;
Victor Stinner11517102014-07-29 23:31:34 +0200667 int is_socket;
668
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800669 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|$p:set_wakeup_fd", kwlist,
670 &fdobj, &warn_on_full_buffer))
Victor Stinner11517102014-07-29 23:31:34 +0200671 return NULL;
672
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100673 sockfd = PyLong_AsSocket_t(fdobj);
674 if (sockfd == (SOCKET_T)(-1) && PyErr_Occurred())
Victor Stinner11517102014-07-29 23:31:34 +0200675 return NULL;
676#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000677 int fd, old_fd;
Victor Stinner11517102014-07-29 23:31:34 +0200678
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800679 if (!PyArg_ParseTupleAndKeywords(args, kwds, "i|$p:set_wakeup_fd", kwlist,
680 &fd, &warn_on_full_buffer))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000681 return NULL;
Victor Stinner11517102014-07-29 23:31:34 +0200682#endif
683
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000684 if (PyThread_get_thread_ident() != main_thread) {
685 PyErr_SetString(PyExc_ValueError,
686 "set_wakeup_fd only works in main thread");
687 return NULL;
688 }
Victor Stinner0bffc942014-07-21 16:28:54 +0200689
Victor Stinner11517102014-07-29 23:31:34 +0200690#ifdef MS_WINDOWS
691 is_socket = 0;
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100692 if (sockfd != INVALID_FD) {
Victor Stinner11517102014-07-29 23:31:34 +0200693 /* Import the _socket module to call WSAStartup() */
694 mod = PyImport_ImportModuleNoBlock("_socket");
695 if (mod == NULL)
696 return NULL;
697 Py_DECREF(mod);
698
699 /* test the socket */
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100700 if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR,
Victor Stinner11517102014-07-29 23:31:34 +0200701 (char *)&res, &res_size) != 0) {
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100702 int fd, err;
703
704 err = WSAGetLastError();
Victor Stinner11517102014-07-29 23:31:34 +0200705 if (err != WSAENOTSOCK) {
706 PyErr_SetExcFromWindowsErr(PyExc_OSError, err);
707 return NULL;
708 }
709
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100710 fd = (int)sockfd;
Steve Dower940f33a2016-09-08 11:21:54 -0700711 if ((SOCKET_T)fd != sockfd) {
Victor Stinner11517102014-07-29 23:31:34 +0200712 PyErr_SetString(PyExc_ValueError, "invalid fd");
713 return NULL;
714 }
715
Victor Stinnere134a7f2015-03-30 10:09:31 +0200716 if (_Py_fstat(fd, &status) != 0)
Victor Stinner11517102014-07-29 23:31:34 +0200717 return NULL;
Victor Stinner38227602014-08-27 12:59:44 +0200718
719 /* on Windows, a file cannot be set to non-blocking mode */
Victor Stinner11517102014-07-29 23:31:34 +0200720 }
Victor Stinner38227602014-08-27 12:59:44 +0200721 else {
Victor Stinner11517102014-07-29 23:31:34 +0200722 is_socket = 1;
Victor Stinner38227602014-08-27 12:59:44 +0200723
724 /* Windows does not provide a function to test if a socket
725 is in non-blocking mode */
726 }
Victor Stinner11517102014-07-29 23:31:34 +0200727 }
728
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100729 old_sockfd = wakeup.fd;
730 wakeup.fd = sockfd;
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800731 wakeup.warn_on_full_buffer = warn_on_full_buffer;
Victor Stinner11517102014-07-29 23:31:34 +0200732 wakeup.use_send = is_socket;
733
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100734 if (old_sockfd != INVALID_FD)
735 return PyLong_FromSocket_t(old_sockfd);
Victor Stinner11517102014-07-29 23:31:34 +0200736 else
737 return PyLong_FromLong(-1);
738#else
Victor Stinnerd18ccd12014-07-24 21:58:53 +0200739 if (fd != -1) {
Victor Stinner38227602014-08-27 12:59:44 +0200740 int blocking;
741
Victor Stinnere134a7f2015-03-30 10:09:31 +0200742 if (_Py_fstat(fd, &status) != 0)
Victor Stinner11517102014-07-29 23:31:34 +0200743 return NULL;
Victor Stinner38227602014-08-27 12:59:44 +0200744
745 blocking = _Py_get_blocking(fd);
746 if (blocking < 0)
747 return NULL;
748 if (blocking) {
749 PyErr_Format(PyExc_ValueError,
750 "the fd %i must be in non-blocking mode",
751 fd);
752 return NULL;
753 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000754 }
Victor Stinner0bffc942014-07-21 16:28:54 +0200755
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800756 old_fd = wakeup.fd;
757 wakeup.fd = fd;
758 wakeup.warn_on_full_buffer = warn_on_full_buffer;
Victor Stinner0bffc942014-07-21 16:28:54 +0200759
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000760 return PyLong_FromLong(old_fd);
Victor Stinner11517102014-07-29 23:31:34 +0200761#endif
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000762}
763
764PyDoc_STRVAR(set_wakeup_fd_doc,
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800765"set_wakeup_fd(fd, *, warn_on_full_buffer=True) -> fd\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000766\n\
Victor Stinner11517102014-07-29 23:31:34 +0200767Sets the fd to be written to (with the signal number) when a signal\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000768comes in. A library can use this to wakeup select or poll.\n\
Victor Stinner11517102014-07-29 23:31:34 +0200769The previous fd or -1 is returned.\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000770\n\
771The fd must be non-blocking.");
772
773/* C API for the same, without all the error checking */
774int
775PySignal_SetWakeupFd(int fd)
776{
Victor Stinner11517102014-07-29 23:31:34 +0200777 int old_fd;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000778 if (fd < 0)
779 fd = -1;
Victor Stinner11517102014-07-29 23:31:34 +0200780
781#ifdef MS_WINDOWS
782 old_fd = Py_SAFE_DOWNCAST(wakeup.fd, SOCKET_T, int);
Victor Stinner11517102014-07-29 23:31:34 +0200783#else
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800784 old_fd = wakeup.fd;
Victor Stinner11517102014-07-29 23:31:34 +0200785#endif
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800786 wakeup.fd = fd;
787 wakeup.warn_on_full_buffer = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000788 return old_fd;
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000789}
790
791
Martin v. Löwis823725e2008-03-24 13:39:54 +0000792#ifdef HAVE_SETITIMER
Tal Einatc7027b72015-05-16 14:14:49 +0300793
794/*[clinic input]
795signal.setitimer
796
797 which: int
Victor Stinneref611c92017-10-13 13:49:43 -0700798 seconds: object
799 interval: object(c_default="NULL") = 0.0
Tal Einatc7027b72015-05-16 14:14:49 +0300800 /
801
802Sets given itimer (one of ITIMER_REAL, ITIMER_VIRTUAL or ITIMER_PROF).
803
804The timer will fire after value seconds and after that every interval seconds.
805The itimer can be cleared by setting seconds to zero.
806
807Returns old values as a tuple: (delay, interval).
808[clinic start generated code]*/
809
Martin v. Löwis823725e2008-03-24 13:39:54 +0000810static PyObject *
Victor Stinneref611c92017-10-13 13:49:43 -0700811signal_setitimer_impl(PyObject *module, int which, PyObject *seconds,
812 PyObject *interval)
813/*[clinic end generated code: output=65f9dcbddc35527b input=de43daf194e6f66f]*/
Martin v. Löwis823725e2008-03-24 13:39:54 +0000814{
Martin v. Löwis823725e2008-03-24 13:39:54 +0000815 struct itimerval new, old;
816
Victor Stinneref611c92017-10-13 13:49:43 -0700817 if (timeval_from_double(seconds, &new.it_value) < 0) {
818 return NULL;
819 }
820 if (timeval_from_double(interval, &new.it_interval) < 0) {
821 return NULL;
822 }
823
Martin v. Löwis823725e2008-03-24 13:39:54 +0000824 /* Let OS check "which" value */
825 if (setitimer(which, &new, &old) != 0) {
Tal Einatc7027b72015-05-16 14:14:49 +0300826 PyErr_SetFromErrno(ItimerError);
827 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000828 }
829
830 return itimer_retval(&old);
831}
832
Martin v. Löwis823725e2008-03-24 13:39:54 +0000833#endif
834
835
836#ifdef HAVE_GETITIMER
Tal Einatc7027b72015-05-16 14:14:49 +0300837
838/*[clinic input]
839signal.getitimer
840
841 which: int
842 /
843
844Returns current value of given itimer.
845[clinic start generated code]*/
846
Martin v. Löwis823725e2008-03-24 13:39:54 +0000847static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300848signal_getitimer_impl(PyObject *module, int which)
849/*[clinic end generated code: output=9e053175d517db40 input=f7d21d38f3490627]*/
Martin v. Löwis823725e2008-03-24 13:39:54 +0000850{
Martin v. Löwis823725e2008-03-24 13:39:54 +0000851 struct itimerval old;
852
Martin v. Löwis823725e2008-03-24 13:39:54 +0000853 if (getitimer(which, &old) != 0) {
Tal Einatc7027b72015-05-16 14:14:49 +0300854 PyErr_SetFromErrno(ItimerError);
855 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000856 }
857
858 return itimer_retval(&old);
859}
860
Martin v. Löwis823725e2008-03-24 13:39:54 +0000861#endif
862
Victor Stinnerb3e72192011-05-08 01:46:11 +0200863#if defined(PYPTHREAD_SIGMASK) || defined(HAVE_SIGPENDING)
Victor Stinner35b300c2011-05-04 13:20:35 +0200864static PyObject*
865sigset_to_set(sigset_t mask)
866{
867 PyObject *signum, *result;
868 int sig;
869
870 result = PySet_New(0);
871 if (result == NULL)
872 return NULL;
873
874 for (sig = 1; sig < NSIG; sig++) {
875 if (sigismember(&mask, sig) != 1)
876 continue;
877
878 /* Handle the case where it is a member by adding the signal to
879 the result list. Ignore the other cases because they mean the
880 signal isn't a member of the mask or the signal was invalid,
881 and an invalid signal must have been our fault in constructing
882 the loop boundaries. */
883 signum = PyLong_FromLong(sig);
884 if (signum == NULL) {
885 Py_DECREF(result);
886 return NULL;
887 }
888 if (PySet_Add(result, signum) == -1) {
889 Py_DECREF(signum);
890 Py_DECREF(result);
891 return NULL;
892 }
893 Py_DECREF(signum);
894 }
895 return result;
896}
Victor Stinnerb3e72192011-05-08 01:46:11 +0200897#endif
Victor Stinner35b300c2011-05-04 13:20:35 +0200898
Victor Stinnerb3e72192011-05-08 01:46:11 +0200899#ifdef PYPTHREAD_SIGMASK
Tal Einatc7027b72015-05-16 14:14:49 +0300900
901/*[clinic input]
902signal.pthread_sigmask
903
904 how: int
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300905 mask: sigset_t
Tal Einatc7027b72015-05-16 14:14:49 +0300906 /
907
908Fetch and/or change the signal mask of the calling thread.
909[clinic start generated code]*/
910
Victor Stinnera9293352011-04-30 15:21:58 +0200911static PyObject *
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300912signal_pthread_sigmask_impl(PyObject *module, int how, sigset_t mask)
913/*[clinic end generated code: output=0562c0fb192981a8 input=85bcebda442fa77f]*/
Victor Stinnera9293352011-04-30 15:21:58 +0200914{
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300915 sigset_t previous;
Victor Stinnera9293352011-04-30 15:21:58 +0200916 int err;
917
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300918 err = pthread_sigmask(how, &mask, &previous);
Victor Stinnera9293352011-04-30 15:21:58 +0200919 if (err != 0) {
920 errno = err;
Victor Stinnerb3e72192011-05-08 01:46:11 +0200921 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnera9293352011-04-30 15:21:58 +0200922 return NULL;
923 }
924
Victor Stinnerd0e516d2011-05-03 14:57:12 +0200925 /* if signals was unblocked, signal handlers have been called */
926 if (PyErr_CheckSignals())
927 return NULL;
928
Victor Stinner35b300c2011-05-04 13:20:35 +0200929 return sigset_to_set(previous);
Victor Stinnera9293352011-04-30 15:21:58 +0200930}
931
Victor Stinnera9293352011-04-30 15:21:58 +0200932#endif /* #ifdef PYPTHREAD_SIGMASK */
933
Martin v. Löwis823725e2008-03-24 13:39:54 +0000934
Victor Stinnerb3e72192011-05-08 01:46:11 +0200935#ifdef HAVE_SIGPENDING
Tal Einatc7027b72015-05-16 14:14:49 +0300936
937/*[clinic input]
938signal.sigpending
939
940Examine pending signals.
941
942Returns a set of signal numbers that are pending for delivery to
943the calling thread.
944[clinic start generated code]*/
945
Victor Stinnerb3e72192011-05-08 01:46:11 +0200946static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300947signal_sigpending_impl(PyObject *module)
948/*[clinic end generated code: output=53375ffe89325022 input=e0036c016f874e29]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +0200949{
950 int err;
951 sigset_t mask;
952 err = sigpending(&mask);
953 if (err)
954 return PyErr_SetFromErrno(PyExc_OSError);
955 return sigset_to_set(mask);
956}
957
Victor Stinnerb3e72192011-05-08 01:46:11 +0200958#endif /* #ifdef HAVE_SIGPENDING */
959
960
961#ifdef HAVE_SIGWAIT
Tal Einatc7027b72015-05-16 14:14:49 +0300962
963/*[clinic input]
964signal.sigwait
965
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300966 sigset: sigset_t
Tal Einatc7027b72015-05-16 14:14:49 +0300967 /
968
969Wait for a signal.
970
971Suspend execution of the calling thread until the delivery of one of the
972signals specified in the signal set sigset. The function accepts the signal
973and returns the signal number.
974[clinic start generated code]*/
975
Victor Stinnerb3e72192011-05-08 01:46:11 +0200976static PyObject *
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300977signal_sigwait_impl(PyObject *module, sigset_t sigset)
978/*[clinic end generated code: output=f43770699d682f96 input=a6fbd47b1086d119]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +0200979{
Victor Stinnerb3e72192011-05-08 01:46:11 +0200980 int err, signum;
981
Victor Stinner10c30d62011-06-10 01:39:53 +0200982 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300983 err = sigwait(&sigset, &signum);
Victor Stinner10c30d62011-06-10 01:39:53 +0200984 Py_END_ALLOW_THREADS
Victor Stinnerb3e72192011-05-08 01:46:11 +0200985 if (err) {
986 errno = err;
987 return PyErr_SetFromErrno(PyExc_OSError);
988 }
989
990 return PyLong_FromLong(signum);
991}
992
Tal Einatc7027b72015-05-16 14:14:49 +0300993#endif /* #ifdef HAVE_SIGWAIT */
994
Victor Stinnerb3e72192011-05-08 01:46:11 +0200995
Antoine Pitrou9d3627e2018-05-04 13:00:50 +0200996#if defined(HAVE_SIGFILLSET) || defined(MS_WINDOWS)
997
998/*[clinic input]
999signal.valid_signals
1000
1001Return a set of valid signal numbers on this platform.
1002
1003The signal numbers returned by this function can be safely passed to
1004functions like `pthread_sigmask`.
1005[clinic start generated code]*/
1006
1007static PyObject *
1008signal_valid_signals_impl(PyObject *module)
1009/*[clinic end generated code: output=1609cffbcfcf1314 input=86a3717ff25288f2]*/
1010{
1011#ifdef MS_WINDOWS
1012#ifdef SIGBREAK
1013 PyObject *tup = Py_BuildValue("(iiiiiii)", SIGABRT, SIGBREAK, SIGFPE,
1014 SIGILL, SIGINT, SIGSEGV, SIGTERM);
1015#else
1016 PyObject *tup = Py_BuildValue("(iiiiii)", SIGABRT, SIGFPE, SIGILL,
1017 SIGINT, SIGSEGV, SIGTERM);
1018#endif
1019 if (tup == NULL) {
1020 return NULL;
1021 }
1022 PyObject *set = PySet_New(tup);
1023 Py_DECREF(tup);
1024 return set;
1025#else
1026 sigset_t mask;
1027 if (sigemptyset(&mask) || sigfillset(&mask)) {
1028 return PyErr_SetFromErrno(PyExc_OSError);
1029 }
1030 return sigset_to_set(mask);
1031#endif
1032}
1033
1034#endif /* #if defined(HAVE_SIGFILLSET) || defined(MS_WINDOWS) */
1035
1036
Ross Lagerwallbc808222011-06-25 12:13:40 +02001037#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
1038static int initialized;
1039static PyStructSequence_Field struct_siginfo_fields[] = {
1040 {"si_signo", "signal number"},
1041 {"si_code", "signal code"},
1042 {"si_errno", "errno associated with this signal"},
1043 {"si_pid", "sending process ID"},
1044 {"si_uid", "real user ID of sending process"},
1045 {"si_status", "exit value or signal"},
1046 {"si_band", "band event for SIGPOLL"},
1047 {0}
1048};
1049
1050PyDoc_STRVAR(struct_siginfo__doc__,
1051"struct_siginfo: Result from sigwaitinfo or sigtimedwait.\n\n\
1052This object may be accessed either as a tuple of\n\
1053(si_signo, si_code, si_errno, si_pid, si_uid, si_status, si_band),\n\
1054or via the attributes si_signo, si_code, and so on.");
1055
1056static PyStructSequence_Desc struct_siginfo_desc = {
1057 "signal.struct_siginfo", /* name */
1058 struct_siginfo__doc__, /* doc */
1059 struct_siginfo_fields, /* fields */
1060 7 /* n_in_sequence */
1061};
1062
1063static PyTypeObject SiginfoType;
1064
1065static PyObject *
1066fill_siginfo(siginfo_t *si)
1067{
1068 PyObject *result = PyStructSequence_New(&SiginfoType);
1069 if (!result)
1070 return NULL;
1071
1072 PyStructSequence_SET_ITEM(result, 0, PyLong_FromLong((long)(si->si_signo)));
1073 PyStructSequence_SET_ITEM(result, 1, PyLong_FromLong((long)(si->si_code)));
1074 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si->si_errno)));
1075 PyStructSequence_SET_ITEM(result, 3, PyLong_FromPid(si->si_pid));
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02001076 PyStructSequence_SET_ITEM(result, 4, _PyLong_FromUid(si->si_uid));
Ross Lagerwallbc808222011-06-25 12:13:40 +02001077 PyStructSequence_SET_ITEM(result, 5,
1078 PyLong_FromLong((long)(si->si_status)));
Zachary Ware6a6967e2016-10-01 00:47:27 -05001079#ifdef HAVE_SIGINFO_T_SI_BAND
Ross Lagerwallbc808222011-06-25 12:13:40 +02001080 PyStructSequence_SET_ITEM(result, 6, PyLong_FromLong(si->si_band));
Zachary Ware6a6967e2016-10-01 00:47:27 -05001081#else
1082 PyStructSequence_SET_ITEM(result, 6, PyLong_FromLong(0L));
1083#endif
Ross Lagerwallbc808222011-06-25 12:13:40 +02001084 if (PyErr_Occurred()) {
1085 Py_DECREF(result);
1086 return NULL;
1087 }
1088
1089 return result;
1090}
1091#endif
1092
1093#ifdef HAVE_SIGWAITINFO
Tal Einatc7027b72015-05-16 14:14:49 +03001094
1095/*[clinic input]
1096signal.sigwaitinfo
1097
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001098 sigset: sigset_t
Tal Einatc7027b72015-05-16 14:14:49 +03001099 /
1100
1101Wait synchronously until one of the signals in *sigset* is delivered.
1102
1103Returns a struct_siginfo containing information about the signal.
1104[clinic start generated code]*/
1105
Ross Lagerwallbc808222011-06-25 12:13:40 +02001106static PyObject *
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001107signal_sigwaitinfo_impl(PyObject *module, sigset_t sigset)
1108/*[clinic end generated code: output=1eb2f1fa236fdbca input=3d1a7e1f27fc664c]*/
Ross Lagerwallbc808222011-06-25 12:13:40 +02001109{
Ross Lagerwallbc808222011-06-25 12:13:40 +02001110 siginfo_t si;
1111 int err;
Victor Stinnera453cd82015-03-20 12:54:28 +01001112 int async_err = 0;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001113
Victor Stinnera453cd82015-03-20 12:54:28 +01001114 do {
1115 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001116 err = sigwaitinfo(&sigset, &si);
Victor Stinnera453cd82015-03-20 12:54:28 +01001117 Py_END_ALLOW_THREADS
1118 } while (err == -1
1119 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Ross Lagerwallbc808222011-06-25 12:13:40 +02001120 if (err == -1)
Victor Stinnera453cd82015-03-20 12:54:28 +01001121 return (!async_err) ? PyErr_SetFromErrno(PyExc_OSError) : NULL;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001122
1123 return fill_siginfo(&si);
1124}
1125
Ross Lagerwallbc808222011-06-25 12:13:40 +02001126#endif /* #ifdef HAVE_SIGWAITINFO */
1127
1128#ifdef HAVE_SIGTIMEDWAIT
Tal Einatc7027b72015-05-16 14:14:49 +03001129
1130/*[clinic input]
1131signal.sigtimedwait
1132
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001133 sigset: sigset_t
Serhiy Storchaka6b680cd2015-05-16 15:57:56 +03001134 timeout as timeout_obj: object
Tal Einatc7027b72015-05-16 14:14:49 +03001135 /
1136
1137Like sigwaitinfo(), but with a timeout.
1138
1139The timeout is specified in seconds, with floating point numbers allowed.
1140[clinic start generated code]*/
1141
Ross Lagerwallbc808222011-06-25 12:13:40 +02001142static PyObject *
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001143signal_sigtimedwait_impl(PyObject *module, sigset_t sigset,
Serhiy Storchaka6b680cd2015-05-16 15:57:56 +03001144 PyObject *timeout_obj)
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001145/*[clinic end generated code: output=59c8971e8ae18a64 input=87fd39237cf0b7ba]*/
Ross Lagerwallbc808222011-06-25 12:13:40 +02001146{
Victor Stinnera453cd82015-03-20 12:54:28 +01001147 struct timespec ts;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001148 siginfo_t si;
Victor Stinnera453cd82015-03-20 12:54:28 +01001149 int res;
Victor Stinner34dc0f42015-03-27 18:19:03 +01001150 _PyTime_t timeout, deadline, monotonic;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001151
Victor Stinner869e1772015-03-30 03:49:14 +02001152 if (_PyTime_FromSecondsObject(&timeout,
1153 timeout_obj, _PyTime_ROUND_CEILING) < 0)
Ross Lagerwallbc808222011-06-25 12:13:40 +02001154 return NULL;
1155
Victor Stinnera453cd82015-03-20 12:54:28 +01001156 if (timeout < 0) {
Ross Lagerwallbc808222011-06-25 12:13:40 +02001157 PyErr_SetString(PyExc_ValueError, "timeout must be non-negative");
1158 return NULL;
1159 }
1160
Victor Stinner34dc0f42015-03-27 18:19:03 +01001161 deadline = _PyTime_GetMonotonicClock() + timeout;
Victor Stinnera453cd82015-03-20 12:54:28 +01001162
1163 do {
Victor Stinner34dc0f42015-03-27 18:19:03 +01001164 if (_PyTime_AsTimespec(timeout, &ts) < 0)
1165 return NULL;
Victor Stinnera453cd82015-03-20 12:54:28 +01001166
1167 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001168 res = sigtimedwait(&sigset, &si, &ts);
Victor Stinnera453cd82015-03-20 12:54:28 +01001169 Py_END_ALLOW_THREADS
1170
1171 if (res != -1)
1172 break;
1173
1174 if (errno != EINTR) {
1175 if (errno == EAGAIN)
1176 Py_RETURN_NONE;
1177 else
1178 return PyErr_SetFromErrno(PyExc_OSError);
1179 }
1180
1181 /* sigtimedwait() was interrupted by a signal (EINTR) */
1182 if (PyErr_CheckSignals())
1183 return NULL;
1184
Victor Stinner34dc0f42015-03-27 18:19:03 +01001185 monotonic = _PyTime_GetMonotonicClock();
1186 timeout = deadline - monotonic;
Victor Stinner6aa446c2015-03-30 21:33:51 +02001187 if (timeout < 0)
Victor Stinnera453cd82015-03-20 12:54:28 +01001188 break;
1189 } while (1);
Ross Lagerwallbc808222011-06-25 12:13:40 +02001190
1191 return fill_siginfo(&si);
1192}
1193
Ross Lagerwallbc808222011-06-25 12:13:40 +02001194#endif /* #ifdef HAVE_SIGTIMEDWAIT */
1195
Victor Stinnerb3e72192011-05-08 01:46:11 +02001196
Antoine Pitroua6a4dc82017-09-07 18:56:24 +02001197#if defined(HAVE_PTHREAD_KILL)
Tal Einatc7027b72015-05-16 14:14:49 +03001198
1199/*[clinic input]
1200signal.pthread_kill
1201
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +02001202 thread_id: unsigned_long(bitwise=True)
Tal Einatc7027b72015-05-16 14:14:49 +03001203 signalnum: int
1204 /
1205
1206Send a signal to a thread.
1207[clinic start generated code]*/
1208
Victor Stinnerb3e72192011-05-08 01:46:11 +02001209static PyObject *
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +02001210signal_pthread_kill_impl(PyObject *module, unsigned long thread_id,
1211 int signalnum)
1212/*[clinic end generated code: output=7629919b791bc27f input=1d901f2c7bb544ff]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +02001213{
Victor Stinnerb3e72192011-05-08 01:46:11 +02001214 int err;
1215
Tal Einatc7027b72015-05-16 14:14:49 +03001216 err = pthread_kill((pthread_t)thread_id, signalnum);
Victor Stinnerb3e72192011-05-08 01:46:11 +02001217 if (err != 0) {
1218 errno = err;
1219 PyErr_SetFromErrno(PyExc_OSError);
1220 return NULL;
1221 }
1222
1223 /* the signal may have been send to the current thread */
1224 if (PyErr_CheckSignals())
1225 return NULL;
1226
1227 Py_RETURN_NONE;
1228}
1229
Antoine Pitroua6a4dc82017-09-07 18:56:24 +02001230#endif /* #if defined(HAVE_PTHREAD_KILL) */
Victor Stinnerb3e72192011-05-08 01:46:11 +02001231
1232
1233
Tal Einatc7027b72015-05-16 14:14:49 +03001234/* List of functions defined in the module -- some of the methoddefs are
1235 defined to nothing if the corresponding C function is not available. */
Barry Warsaw92971171997-01-03 00:14:25 +00001236static PyMethodDef signal_methods[] = {
Tal Einatc7027b72015-05-16 14:14:49 +03001237 {"default_int_handler", signal_default_int_handler, METH_VARARGS, default_int_handler_doc},
1238 SIGNAL_ALARM_METHODDEF
1239 SIGNAL_SETITIMER_METHODDEF
1240 SIGNAL_GETITIMER_METHODDEF
1241 SIGNAL_SIGNAL_METHODDEF
Vladimir Matveevc24c6c22019-01-08 01:58:25 -08001242 SIGNAL_RAISE_SIGNAL_METHODDEF
Antoine Pietri5d2a27d2018-03-12 14:42:34 +01001243 SIGNAL_STRSIGNAL_METHODDEF
Tal Einatc7027b72015-05-16 14:14:49 +03001244 SIGNAL_GETSIGNAL_METHODDEF
Serhiy Storchaka62be7422018-11-27 13:27:31 +02001245 {"set_wakeup_fd", (PyCFunction)(void(*)(void))signal_set_wakeup_fd, METH_VARARGS | METH_KEYWORDS, set_wakeup_fd_doc},
Tal Einatc7027b72015-05-16 14:14:49 +03001246 SIGNAL_SIGINTERRUPT_METHODDEF
1247 SIGNAL_PAUSE_METHODDEF
1248 SIGNAL_PTHREAD_KILL_METHODDEF
1249 SIGNAL_PTHREAD_SIGMASK_METHODDEF
1250 SIGNAL_SIGPENDING_METHODDEF
1251 SIGNAL_SIGWAIT_METHODDEF
1252 SIGNAL_SIGWAITINFO_METHODDEF
1253 SIGNAL_SIGTIMEDWAIT_METHODDEF
Antoine Pitrou9d3627e2018-05-04 13:00:50 +02001254#if defined(HAVE_SIGFILLSET) || defined(MS_WINDOWS)
1255 SIGNAL_VALID_SIGNALS_METHODDEF
1256#endif
Tal Einatc7027b72015-05-16 14:14:49 +03001257 {NULL, NULL} /* sentinel */
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001258};
1259
Barry Warsaw92971171997-01-03 00:14:25 +00001260
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001261PyDoc_STRVAR(module_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001262"This module provides mechanisms to use signal handlers in Python.\n\
1263\n\
1264Functions:\n\
1265\n\
1266alarm() -- cause SIGALRM after a specified time [Unix only]\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001267setitimer() -- cause a signal (described below) after a specified\n\
1268 float time and the timer may restart then [Unix only]\n\
1269getitimer() -- get current value of timer [Unix only]\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001270signal() -- set the action for a given signal\n\
1271getsignal() -- get the signal action for a given signal\n\
1272pause() -- wait until a signal arrives [Unix only]\n\
1273default_int_handler() -- default SIGINT handler\n\
1274\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001275signal constants:\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001276SIG_DFL -- used to refer to the system default handler\n\
1277SIG_IGN -- used to ignore the signal\n\
1278NSIG -- number of defined signals\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001279SIGINT, SIGTERM, etc. -- signal numbers\n\
1280\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001281itimer constants:\n\
1282ITIMER_REAL -- decrements in real time, and delivers SIGALRM upon\n\
1283 expiration\n\
1284ITIMER_VIRTUAL -- decrements only when the process is executing,\n\
1285 and delivers SIGVTALRM upon expiration\n\
1286ITIMER_PROF -- decrements both when the process is executing and\n\
1287 when the system is executing on behalf of the process.\n\
1288 Coupled with ITIMER_VIRTUAL, this timer is usually\n\
1289 used to profile the time spent by the application\n\
1290 in user and kernel space. SIGPROF is delivered upon\n\
1291 expiration.\n\
1292\n\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001293*** IMPORTANT NOTICE ***\n\
1294A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001295the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001296
Martin v. Löwis1a214512008-06-11 05:26:20 +00001297static struct PyModuleDef signalmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001298 PyModuleDef_HEAD_INIT,
Brett Cannon815a6f32014-04-04 10:20:28 -04001299 "_signal",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001300 module_doc,
1301 -1,
1302 signal_methods,
1303 NULL,
1304 NULL,
1305 NULL,
1306 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001307};
1308
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001309PyMODINIT_FUNC
Giampaolo Rodola'e09fb712014-04-04 15:34:17 +02001310PyInit__signal(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001311{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001312 PyObject *m, *d, *x;
1313 int i;
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001314
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001315 main_thread = PyThread_get_thread_ident();
1316 main_pid = getpid();
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001317
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001318 /* Create the module and add the functions */
1319 m = PyModule_Create(&signalmodule);
1320 if (m == NULL)
1321 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001322
Ross Lagerwallbc808222011-06-25 12:13:40 +02001323#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
Victor Stinner1c8f0592013-07-22 22:24:54 +02001324 if (!initialized) {
1325 if (PyStructSequence_InitType2(&SiginfoType, &struct_siginfo_desc) < 0)
1326 return NULL;
1327 }
Ross Lagerwallbc808222011-06-25 12:13:40 +02001328 Py_INCREF((PyObject*) &SiginfoType);
1329 PyModule_AddObject(m, "struct_siginfo", (PyObject*) &SiginfoType);
1330 initialized = 1;
1331#endif
1332
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001333 /* Add some symbolic constants to the module */
1334 d = PyModule_GetDict(m);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001335
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001336 x = DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL);
1337 if (!x || PyDict_SetItemString(d, "SIG_DFL", x) < 0)
1338 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001339
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001340 x = IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN);
1341 if (!x || PyDict_SetItemString(d, "SIG_IGN", x) < 0)
1342 goto finally;
Barry Warsaw92971171997-01-03 00:14:25 +00001343
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001344 x = PyLong_FromLong((long)NSIG);
1345 if (!x || PyDict_SetItemString(d, "NSIG", x) < 0)
1346 goto finally;
1347 Py_DECREF(x);
Barry Warsaw92971171997-01-03 00:14:25 +00001348
Victor Stinnera9293352011-04-30 15:21:58 +02001349#ifdef SIG_BLOCK
Victor Stinner72c53b52011-05-02 16:15:43 +02001350 if (PyModule_AddIntMacro(m, SIG_BLOCK))
1351 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001352#endif
Victor Stinnera9293352011-04-30 15:21:58 +02001353#ifdef SIG_UNBLOCK
Victor Stinner72c53b52011-05-02 16:15:43 +02001354 if (PyModule_AddIntMacro(m, SIG_UNBLOCK))
1355 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001356#endif
Victor Stinnera9293352011-04-30 15:21:58 +02001357#ifdef SIG_SETMASK
Victor Stinner72c53b52011-05-02 16:15:43 +02001358 if (PyModule_AddIntMacro(m, SIG_SETMASK))
1359 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001360#endif
1361
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001362 x = IntHandler = PyDict_GetItemString(d, "default_int_handler");
1363 if (!x)
1364 goto finally;
1365 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +00001366
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001367 _Py_atomic_store_relaxed(&Handlers[0].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001368 for (i = 1; i < NSIG; i++) {
1369 void (*t)(int);
1370 t = PyOS_getsig(i);
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001371 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001372 if (t == SIG_DFL)
1373 Handlers[i].func = DefaultHandler;
1374 else if (t == SIG_IGN)
1375 Handlers[i].func = IgnoreHandler;
1376 else
1377 Handlers[i].func = Py_None; /* None of our business */
1378 Py_INCREF(Handlers[i].func);
1379 }
1380 if (Handlers[SIGINT].func == DefaultHandler) {
1381 /* Install default int handler */
1382 Py_INCREF(IntHandler);
Serhiy Storchaka57a01d32016-04-10 18:05:40 +03001383 Py_SETREF(Handlers[SIGINT].func, IntHandler);
pkerlinge905c842018-06-01 09:47:18 +00001384 PyOS_setsig(SIGINT, signal_handler);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001385 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001386
1387#ifdef SIGHUP
Christian Heimes6782b142016-09-09 00:11:45 +02001388 if (PyModule_AddIntMacro(m, SIGHUP))
1389 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001390#endif
1391#ifdef SIGINT
Christian Heimes6782b142016-09-09 00:11:45 +02001392 if (PyModule_AddIntMacro(m, SIGINT))
1393 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001394#endif
Tim Peters1ce3cf72001-10-01 17:58:40 +00001395#ifdef SIGBREAK
Christian Heimes6782b142016-09-09 00:11:45 +02001396 if (PyModule_AddIntMacro(m, SIGBREAK))
1397 goto finally;
Tim Peters1ce3cf72001-10-01 17:58:40 +00001398#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001399#ifdef SIGQUIT
Christian Heimes6782b142016-09-09 00:11:45 +02001400 if (PyModule_AddIntMacro(m, SIGQUIT))
1401 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001402#endif
1403#ifdef SIGILL
Christian Heimes6782b142016-09-09 00:11:45 +02001404 if (PyModule_AddIntMacro(m, SIGILL))
1405 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001406#endif
1407#ifdef SIGTRAP
Christian Heimes6782b142016-09-09 00:11:45 +02001408 if (PyModule_AddIntMacro(m, SIGTRAP))
1409 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001410#endif
1411#ifdef SIGIOT
Christian Heimes6782b142016-09-09 00:11:45 +02001412 if (PyModule_AddIntMacro(m, SIGIOT))
1413 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001414#endif
1415#ifdef SIGABRT
Christian Heimes6782b142016-09-09 00:11:45 +02001416 if (PyModule_AddIntMacro(m, SIGABRT))
1417 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001418#endif
1419#ifdef SIGEMT
Christian Heimes6782b142016-09-09 00:11:45 +02001420 if (PyModule_AddIntMacro(m, SIGEMT))
1421 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001422#endif
1423#ifdef SIGFPE
Christian Heimes6782b142016-09-09 00:11:45 +02001424 if (PyModule_AddIntMacro(m, SIGFPE))
1425 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001426#endif
1427#ifdef SIGKILL
Christian Heimes6782b142016-09-09 00:11:45 +02001428 if (PyModule_AddIntMacro(m, SIGKILL))
1429 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001430#endif
1431#ifdef SIGBUS
Christian Heimes6782b142016-09-09 00:11:45 +02001432 if (PyModule_AddIntMacro(m, SIGBUS))
1433 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001434#endif
1435#ifdef SIGSEGV
Christian Heimes6782b142016-09-09 00:11:45 +02001436 if (PyModule_AddIntMacro(m, SIGSEGV))
1437 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001438#endif
1439#ifdef SIGSYS
Christian Heimes6782b142016-09-09 00:11:45 +02001440 if (PyModule_AddIntMacro(m, SIGSYS))
1441 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001442#endif
1443#ifdef SIGPIPE
Christian Heimes6782b142016-09-09 00:11:45 +02001444 if (PyModule_AddIntMacro(m, SIGPIPE))
1445 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001446#endif
1447#ifdef SIGALRM
Christian Heimes6782b142016-09-09 00:11:45 +02001448 if (PyModule_AddIntMacro(m, SIGALRM))
1449 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001450#endif
1451#ifdef SIGTERM
Christian Heimes6782b142016-09-09 00:11:45 +02001452 if (PyModule_AddIntMacro(m, SIGTERM))
1453 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001454#endif
1455#ifdef SIGUSR1
Christian Heimes6782b142016-09-09 00:11:45 +02001456 if (PyModule_AddIntMacro(m, SIGUSR1))
1457 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001458#endif
1459#ifdef SIGUSR2
Christian Heimes6782b142016-09-09 00:11:45 +02001460 if (PyModule_AddIntMacro(m, SIGUSR2))
1461 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001462#endif
1463#ifdef SIGCLD
Christian Heimes6782b142016-09-09 00:11:45 +02001464 if (PyModule_AddIntMacro(m, SIGCLD))
1465 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001466#endif
1467#ifdef SIGCHLD
Christian Heimes6782b142016-09-09 00:11:45 +02001468 if (PyModule_AddIntMacro(m, SIGCHLD))
1469 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001470#endif
1471#ifdef SIGPWR
Christian Heimes6782b142016-09-09 00:11:45 +02001472 if (PyModule_AddIntMacro(m, SIGPWR))
1473 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001474#endif
1475#ifdef SIGIO
Christian Heimes6782b142016-09-09 00:11:45 +02001476 if (PyModule_AddIntMacro(m, SIGIO))
1477 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001478#endif
1479#ifdef SIGURG
Christian Heimes6782b142016-09-09 00:11:45 +02001480 if (PyModule_AddIntMacro(m, SIGURG))
1481 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001482#endif
1483#ifdef SIGWINCH
Christian Heimes6782b142016-09-09 00:11:45 +02001484 if (PyModule_AddIntMacro(m, SIGWINCH))
1485 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001486#endif
1487#ifdef SIGPOLL
Christian Heimes6782b142016-09-09 00:11:45 +02001488 if (PyModule_AddIntMacro(m, SIGPOLL))
1489 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001490#endif
1491#ifdef SIGSTOP
Christian Heimes6782b142016-09-09 00:11:45 +02001492 if (PyModule_AddIntMacro(m, SIGSTOP))
1493 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001494#endif
1495#ifdef SIGTSTP
Christian Heimes6782b142016-09-09 00:11:45 +02001496 if (PyModule_AddIntMacro(m, SIGTSTP))
1497 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001498#endif
1499#ifdef SIGCONT
Christian Heimes6782b142016-09-09 00:11:45 +02001500 if (PyModule_AddIntMacro(m, SIGCONT))
1501 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001502#endif
1503#ifdef SIGTTIN
Christian Heimes6782b142016-09-09 00:11:45 +02001504 if (PyModule_AddIntMacro(m, SIGTTIN))
1505 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001506#endif
1507#ifdef SIGTTOU
Christian Heimes6782b142016-09-09 00:11:45 +02001508 if (PyModule_AddIntMacro(m, SIGTTOU))
1509 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001510#endif
1511#ifdef SIGVTALRM
Christian Heimes6782b142016-09-09 00:11:45 +02001512 if (PyModule_AddIntMacro(m, SIGVTALRM))
1513 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001514#endif
1515#ifdef SIGPROF
Christian Heimes6782b142016-09-09 00:11:45 +02001516 if (PyModule_AddIntMacro(m, SIGPROF))
1517 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001518#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001519#ifdef SIGXCPU
Christian Heimes6782b142016-09-09 00:11:45 +02001520 if (PyModule_AddIntMacro(m, SIGXCPU))
1521 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001522#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001523#ifdef SIGXFSZ
Christian Heimes6782b142016-09-09 00:11:45 +02001524 if (PyModule_AddIntMacro(m, SIGXFSZ))
1525 goto finally;
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001526#endif
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001527#ifdef SIGRTMIN
Christian Heimes6782b142016-09-09 00:11:45 +02001528 if (PyModule_AddIntMacro(m, SIGRTMIN))
1529 goto finally;
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001530#endif
1531#ifdef SIGRTMAX
Christian Heimes6782b142016-09-09 00:11:45 +02001532 if (PyModule_AddIntMacro(m, SIGRTMAX))
1533 goto finally;
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001534#endif
Martin v. Löwis175af252002-01-12 11:43:25 +00001535#ifdef SIGINFO
Christian Heimes6782b142016-09-09 00:11:45 +02001536 if (PyModule_AddIntMacro(m, SIGINFO))
1537 goto finally;
Martin v. Löwis175af252002-01-12 11:43:25 +00001538#endif
Martin v. Löwis823725e2008-03-24 13:39:54 +00001539
1540#ifdef ITIMER_REAL
Christian Heimes6782b142016-09-09 00:11:45 +02001541 if (PyModule_AddIntMacro(m, ITIMER_REAL))
1542 goto finally;
Martin v. Löwis823725e2008-03-24 13:39:54 +00001543#endif
1544#ifdef ITIMER_VIRTUAL
Christian Heimes6782b142016-09-09 00:11:45 +02001545 if (PyModule_AddIntMacro(m, ITIMER_VIRTUAL))
1546 goto finally;
Martin v. Löwis823725e2008-03-24 13:39:54 +00001547#endif
1548#ifdef ITIMER_PROF
Christian Heimes6782b142016-09-09 00:11:45 +02001549 if (PyModule_AddIntMacro(m, ITIMER_PROF))
1550 goto finally;
Martin v. Löwis823725e2008-03-24 13:39:54 +00001551#endif
1552
1553#if defined (HAVE_SETITIMER) || defined (HAVE_GETITIMER)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001554 ItimerError = PyErr_NewException("signal.ItimerError",
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +03001555 PyExc_OSError, NULL);
Neal Norwitz2f99b242008-08-24 05:48:10 +00001556 if (ItimerError != NULL)
Martin Panter6d57fe12016-09-17 03:26:16 +00001557 PyDict_SetItemString(d, "ItimerError", ItimerError);
Martin v. Löwis823725e2008-03-24 13:39:54 +00001558#endif
1559
Brian Curtineb24d742010-04-12 17:16:38 +00001560#ifdef CTRL_C_EVENT
Christian Heimes6782b142016-09-09 00:11:45 +02001561 if (PyModule_AddIntMacro(m, CTRL_C_EVENT))
1562 goto finally;
Brian Curtineb24d742010-04-12 17:16:38 +00001563#endif
1564
1565#ifdef CTRL_BREAK_EVENT
Christian Heimes6782b142016-09-09 00:11:45 +02001566 if (PyModule_AddIntMacro(m, CTRL_BREAK_EVENT))
1567 goto finally;
Brian Curtineb24d742010-04-12 17:16:38 +00001568#endif
1569
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001570#ifdef MS_WINDOWS
1571 /* Create manual-reset event, initially unset */
1572 sigint_event = CreateEvent(NULL, TRUE, FALSE, FALSE);
1573#endif
1574
Martin v. Löwis1a214512008-06-11 05:26:20 +00001575 if (PyErr_Occurred()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001576 Py_DECREF(m);
1577 m = NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001578 }
Barry Warsaw92971171997-01-03 00:14:25 +00001579
Barry Warsaw92971171997-01-03 00:14:25 +00001580 finally:
Martin v. Löwis1a214512008-06-11 05:26:20 +00001581 return m;
Guido van Rossum08c16611997-08-02 03:01:42 +00001582}
1583
1584static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001585finisignal(void)
Guido van Rossum08c16611997-08-02 03:01:42 +00001586{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001587 int i;
1588 PyObject *func;
Guido van Rossum08c16611997-08-02 03:01:42 +00001589
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001590 for (i = 1; i < NSIG; i++) {
1591 func = Handlers[i].func;
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001592 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001593 Handlers[i].func = NULL;
pkerlinge905c842018-06-01 09:47:18 +00001594 if (func != NULL && func != Py_None &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001595 func != DefaultHandler && func != IgnoreHandler)
1596 PyOS_setsig(i, SIG_DFL);
1597 Py_XDECREF(func);
1598 }
Guido van Rossum08c16611997-08-02 03:01:42 +00001599
Serhiy Storchaka505ff752014-02-09 13:33:53 +02001600 Py_CLEAR(IntHandler);
1601 Py_CLEAR(DefaultHandler);
1602 Py_CLEAR(IgnoreHandler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001603}
1604
Barry Warsaw92971171997-01-03 00:14:25 +00001605
Barry Warsaw92971171997-01-03 00:14:25 +00001606/* Declared in pyerrors.h */
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001607int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001608PyErr_CheckSignals(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001609{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001610 int i;
1611 PyObject *f;
Barry Warsaw92971171997-01-03 00:14:25 +00001612
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001613 if (!_Py_atomic_load(&is_tripped))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001614 return 0;
Christian Heimesb76922a2007-12-11 01:06:40 +00001615
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001616 if (PyThread_get_thread_ident() != main_thread)
1617 return 0;
Christian Heimesb76922a2007-12-11 01:06:40 +00001618
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001619 /*
1620 * The is_tripped variable is meant to speed up the calls to
1621 * PyErr_CheckSignals (both directly or via pending calls) when no
1622 * signal has arrived. This variable is set to 1 when a signal arrives
1623 * and it is set to 0 here, when we know some signals arrived. This way
1624 * we can run the registered handlers with no signals blocked.
1625 *
1626 * NOTE: with this approach we can have a situation where is_tripped is
1627 * 1 but we have no more signals to handle (Handlers[i].tripped
1628 * is 0 for every signal i). This won't do us any harm (except
1629 * we're gonna spent some cycles for nothing). This happens when
1630 * we receive a signal i after we zero is_tripped and before we
1631 * check Handlers[i].tripped.
1632 */
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001633 _Py_atomic_store(&is_tripped, 0);
Christian Heimesb76922a2007-12-11 01:06:40 +00001634
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001635 if (!(f = (PyObject *)PyEval_GetFrame()))
1636 f = Py_None;
Christian Heimesb76922a2007-12-11 01:06:40 +00001637
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001638 for (i = 1; i < NSIG; i++) {
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001639 if (_Py_atomic_load_relaxed(&Handlers[i].tripped)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001640 PyObject *result = NULL;
1641 PyObject *arglist = Py_BuildValue("(iO)", i, f);
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001642 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Barry Warsaw92971171997-01-03 00:14:25 +00001643
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001644 if (arglist) {
1645 result = PyEval_CallObject(Handlers[i].func,
1646 arglist);
1647 Py_DECREF(arglist);
1648 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +02001649 if (!result) {
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001650 _Py_atomic_store(&is_tripped, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001651 return -1;
Antoine Pitrouc08177a2017-06-28 23:29:29 +02001652 }
Barry Warsaw92971171997-01-03 00:14:25 +00001653
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001654 Py_DECREF(result);
1655 }
1656 }
Christian Heimesb76922a2007-12-11 01:06:40 +00001657
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001658 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001659}
1660
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +00001661
Barry Warsaw92971171997-01-03 00:14:25 +00001662/* Replacements for intrcheck.c functionality
1663 * Declared in pyerrors.h
1664 */
1665void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001666PyErr_SetInterrupt(void)
Barry Warsaw92971171997-01-03 00:14:25 +00001667{
Victor Stinner6c9b35b2011-04-18 16:25:56 +02001668 trip_signal(SIGINT);
Barry Warsaw92971171997-01-03 00:14:25 +00001669}
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001670
1671void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001672PyOS_InitInterrupts(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001673{
Giampaolo Rodola'e09fb712014-04-04 15:34:17 +02001674 PyObject *m = PyImport_ImportModule("_signal");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001675 if (m) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001676 Py_DECREF(m);
1677 }
Guido van Rossum08c16611997-08-02 03:01:42 +00001678}
1679
1680void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001681PyOS_FiniInterrupts(void)
Guido van Rossum08c16611997-08-02 03:01:42 +00001682{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001683 finisignal();
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001684}
1685
1686int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001687PyOS_InterruptOccurred(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001688{
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001689 if (_Py_atomic_load_relaxed(&Handlers[SIGINT].tripped)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001690 if (PyThread_get_thread_ident() != main_thread)
1691 return 0;
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001692 _Py_atomic_store_relaxed(&Handlers[SIGINT].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001693 return 1;
1694 }
1695 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001696}
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001697
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001698static void
1699_clear_pending_signals(void)
1700{
1701 int i;
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001702 if (!_Py_atomic_load(&is_tripped))
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001703 return;
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001704 _Py_atomic_store(&is_tripped, 0);
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001705 for (i = 1; i < NSIG; ++i) {
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001706 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001707 }
1708}
1709
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001710void
Antoine Pitrou346cbd32017-05-27 17:50:54 +02001711_PySignal_AfterFork(void)
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001712{
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001713 /* Clear the signal flags after forking so that they aren't handled
1714 * in both processes if they came in just before the fork() but before
1715 * the interpreter had an opportunity to call the handlers. issue9535. */
1716 _clear_pending_signals();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001717 main_thread = PyThread_get_thread_ident();
1718 main_pid = getpid();
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001719}
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001720
1721int
1722_PyOS_IsMainThread(void)
1723{
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001724 return PyThread_get_thread_ident() == main_thread;
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001725}
1726
1727#ifdef MS_WINDOWS
1728void *_PyOS_SigintEvent(void)
1729{
1730 /* Returns a manual-reset event which gets tripped whenever
1731 SIGINT is received.
1732
1733 Python.h does not include windows.h so we do cannot use HANDLE
1734 as the return type of this function. We use void* instead. */
1735 return sigint_event;
1736}
1737#endif