blob: ac3b6c7c561740ab95126c90f4f806cea08183ec [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
Eric Snowef4ac962019-02-24 15:40:47 -080022#include "internal/pycore_pystate.h"
Guido van Rossum644a12b1997-04-09 19:24:53 +000023
Benjamin Peterson2614cda2010-03-21 22:36:19 +000024#ifdef HAVE_SIGNAL_H
Guido van Rossum398d9fe1994-05-11 08:59:13 +000025#include <signal.h>
Benjamin Peterson2614cda2010-03-21 22:36:19 +000026#endif
27#ifdef HAVE_SYS_STAT_H
Christian Heimes5fb7c2a2007-12-24 08:52:31 +000028#include <sys/stat.h>
Benjamin Peterson2614cda2010-03-21 22:36:19 +000029#endif
Martin v. Löwis3bf3cc02008-03-24 14:05:07 +000030#ifdef HAVE_SYS_TIME_H
Martin v. Löwis823725e2008-03-24 13:39:54 +000031#include <sys/time.h>
Martin v. Löwis3bf3cc02008-03-24 14:05:07 +000032#endif
Christian Heimes5fb7c2a2007-12-24 08:52:31 +000033
Victor Stinnera9293352011-04-30 15:21:58 +020034#if defined(HAVE_PTHREAD_SIGMASK) && !defined(HAVE_BROKEN_PTHREAD_SIGMASK)
35# define PYPTHREAD_SIGMASK
36#endif
37
38#if defined(PYPTHREAD_SIGMASK) && defined(HAVE_PTHREAD_H)
39# include <pthread.h>
40#endif
41
Guido van Rossumbb4ba121994-06-23 11:25:45 +000042#ifndef SIG_ERR
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +000043#define SIG_ERR ((PyOS_sighandler_t)(-1))
Guido van Rossumbb4ba121994-06-23 11:25:45 +000044#endif
45
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000046#ifndef NSIG
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000047# if defined(_NSIG)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000048# define NSIG _NSIG /* For BSD/SysV */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000049# elif defined(_SIGMAX)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000050# define NSIG (_SIGMAX + 1) /* For QNX */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000051# elif defined(SIGMAX)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000052# define NSIG (SIGMAX + 1) /* For djgpp */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000053# else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000054# define NSIG 64 /* Use a reasonable default value */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000055# endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000056#endif
57
Tal Einatc7027b72015-05-16 14:14:49 +030058#include "clinic/signalmodule.c.h"
59
60/*[clinic input]
61module signal
62[clinic start generated code]*/
63/*[clinic end generated code: output=da39a3ee5e6b4b0d input=b0301a3bde5fe9d3]*/
64
Serhiy Storchakad54cfb12018-05-08 07:48:50 +030065/*[python input]
66
67class sigset_t_converter(CConverter):
68 type = 'sigset_t'
69 converter = '_Py_Sigset_Converter'
70
71[python start generated code]*/
72/*[python end generated code: output=da39a3ee5e6b4b0d input=b5689d14466b6823]*/
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000073
Guido van Rossumbb4ba121994-06-23 11:25:45 +000074/*
75 NOTES ON THE INTERACTION BETWEEN SIGNALS AND THREADS
76
77 When threads are supported, we want the following semantics:
78
79 - only the main thread can set a signal handler
80 - any thread can get a signal handler
81 - signals are only delivered to the main thread
82
83 I.e. we don't support "synchronous signals" like SIGFPE (catching
84 this doesn't make much sense in Python anyway) nor do we support
85 signals as a means of inter-thread communication, since not all
86 thread implementations support that (at least our thread library
87 doesn't).
88
89 We still have the problem that in some implementations signals
90 generated by the keyboard (e.g. SIGINT) are delivered to all
91 threads (e.g. SGI), while in others (e.g. Solaris) such signals are
92 delivered to one random thread (an intermediate possibility would
Guido van Rossuma3c04b01995-01-12 11:29:01 +000093 be to deliver it to the main thread -- POSIX?). For now, we have
Guido van Rossumbb4ba121994-06-23 11:25:45 +000094 a working implementation that works in all three cases -- the
95 handler ignores signals if getpid() isn't the same as in the main
96 thread. XXX This is a hack.
Guido van Rossumbb4ba121994-06-23 11:25:45 +000097*/
98
Guido van Rossum295b8e51997-06-06 21:16:41 +000099#include <sys/types.h> /* For pid_t */
Guido van Rossum49b56061998-10-01 20:42:43 +0000100#include "pythread.h"
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +0200101static unsigned long main_thread;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000102static pid_t main_pid;
Eric Snow64d6cc82019-02-23 15:40:43 -0700103static PyInterpreterState *main_interp;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000104
Victor Stinner2ec6b172011-05-15 10:21:59 +0200105static volatile struct {
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +0200106 _Py_atomic_int tripped;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000107 PyObject *func;
Barry Warsaw92971171997-01-03 00:14:25 +0000108} Handlers[NSIG];
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000109
Victor Stinner11517102014-07-29 23:31:34 +0200110#ifdef MS_WINDOWS
111#define INVALID_FD ((SOCKET_T)-1)
112
113static volatile struct {
114 SOCKET_T fd;
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800115 int warn_on_full_buffer;
Victor Stinner11517102014-07-29 23:31:34 +0200116 int use_send;
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800117} wakeup = {.fd = INVALID_FD, .warn_on_full_buffer = 1, .use_send = 0};
Victor Stinner11517102014-07-29 23:31:34 +0200118#else
119#define INVALID_FD (-1)
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800120static volatile struct {
121 sig_atomic_t fd;
122 int warn_on_full_buffer;
123} wakeup = {.fd = INVALID_FD, .warn_on_full_buffer = 1};
Victor Stinner11517102014-07-29 23:31:34 +0200124#endif
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000125
Christian Heimesb76922a2007-12-11 01:06:40 +0000126/* Speed up sigcheck() when none tripped */
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +0200127static _Py_atomic_int is_tripped;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000128
Barry Warsaw92971171997-01-03 00:14:25 +0000129static PyObject *DefaultHandler;
130static PyObject *IgnoreHandler;
131static PyObject *IntHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000132
Antoine Pitrou6dd381e2011-11-21 21:26:56 +0100133#ifdef MS_WINDOWS
134static HANDLE sigint_event = NULL;
135#endif
136
Martin v. Löwis823725e2008-03-24 13:39:54 +0000137#ifdef HAVE_GETITIMER
138static PyObject *ItimerError;
139
Victor Stinneref611c92017-10-13 13:49:43 -0700140/* auxiliary functions for setitimer */
141static int
142timeval_from_double(PyObject *obj, struct timeval *tv)
Martin v. Löwis823725e2008-03-24 13:39:54 +0000143{
Victor Stinneref611c92017-10-13 13:49:43 -0700144 if (obj == NULL) {
145 tv->tv_sec = 0;
146 tv->tv_usec = 0;
147 return 0;
Antoine Pitrou729780a2017-06-30 10:01:05 +0200148 }
Victor Stinneref611c92017-10-13 13:49:43 -0700149
150 _PyTime_t t;
151 if (_PyTime_FromSecondsObject(&t, obj, _PyTime_ROUND_CEILING) < 0) {
152 return -1;
153 }
154 return _PyTime_AsTimeval(t, tv, _PyTime_ROUND_CEILING);
Martin v. Löwis823725e2008-03-24 13:39:54 +0000155}
156
Christian Heimes1a8501c2008-10-02 19:56:01 +0000157Py_LOCAL_INLINE(double)
Martin v. Löwis823725e2008-03-24 13:39:54 +0000158double_from_timeval(struct timeval *tv)
159{
160 return tv->tv_sec + (double)(tv->tv_usec / 1000000.0);
161}
162
163static PyObject *
164itimer_retval(struct itimerval *iv)
165{
166 PyObject *r, *v;
167
168 r = PyTuple_New(2);
169 if (r == NULL)
Martin Panter6d57fe12016-09-17 03:26:16 +0000170 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000171
172 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_value)))) {
Martin Panter6d57fe12016-09-17 03:26:16 +0000173 Py_DECREF(r);
174 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000175 }
176
177 PyTuple_SET_ITEM(r, 0, v);
178
179 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_interval)))) {
Martin Panter6d57fe12016-09-17 03:26:16 +0000180 Py_DECREF(r);
181 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000182 }
183
184 PyTuple_SET_ITEM(r, 1, v);
185
186 return r;
187}
188#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000189
Eric Snow64d6cc82019-02-23 15:40:43 -0700190static int
191is_main(void)
192{
193 return PyThread_get_thread_ident() == main_thread &&
194 _PyInterpreterState_Get() == main_interp;
195}
196
Guido van Rossume4485b01994-09-07 14:32:49 +0000197static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000198signal_default_int_handler(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000199{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000200 PyErr_SetNone(PyExc_KeyboardInterrupt);
201 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000202}
203
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000204PyDoc_STRVAR(default_int_handler_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000205"default_int_handler(...)\n\
206\n\
Michael W. Hudson24ec2112004-06-17 15:55:53 +0000207The default handler for SIGINT installed by Python.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000208It raises KeyboardInterrupt.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000209
Thomas Wouters0796b002000-07-22 23:49:30 +0000210
211static int
Victor Stinner11517102014-07-29 23:31:34 +0200212report_wakeup_write_error(void *data)
Antoine Pitrou6f6ec372013-08-17 20:27:56 +0200213{
Eric Snowfdf282d2019-01-11 14:26:55 -0700214 PyObject *exc, *val, *tb;
Antoine Pitrou6f6ec372013-08-17 20:27:56 +0200215 int save_errno = errno;
Benjamin Petersonca470632016-09-06 13:47:26 -0700216 errno = (int) (intptr_t) data;
Eric Snowfdf282d2019-01-11 14:26:55 -0700217 PyErr_Fetch(&exc, &val, &tb);
Antoine Pitrou6f6ec372013-08-17 20:27:56 +0200218 PyErr_SetFromErrno(PyExc_OSError);
219 PySys_WriteStderr("Exception ignored when trying to write to the "
220 "signal wakeup fd:\n");
221 PyErr_WriteUnraisable(NULL);
Eric Snowfdf282d2019-01-11 14:26:55 -0700222 PyErr_Restore(exc, val, tb);
Antoine Pitrou6f6ec372013-08-17 20:27:56 +0200223 errno = save_errno;
224 return 0;
225}
226
Victor Stinner11517102014-07-29 23:31:34 +0200227#ifdef MS_WINDOWS
228static int
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800229report_wakeup_send_error(void* data)
Victor Stinner11517102014-07-29 23:31:34 +0200230{
Eric Snowfdf282d2019-01-11 14:26:55 -0700231 PyObject *exc, *val, *tb;
232 PyErr_Fetch(&exc, &val, &tb);
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800233 /* PyErr_SetExcFromWindowsErr() invokes FormatMessage() which
234 recognizes the error codes used by both GetLastError() and
235 WSAGetLastError */
236 PyErr_SetExcFromWindowsErr(PyExc_OSError, (int) (intptr_t) data);
Victor Stinner11517102014-07-29 23:31:34 +0200237 PySys_WriteStderr("Exception ignored when trying to send to the "
238 "signal wakeup fd:\n");
239 PyErr_WriteUnraisable(NULL);
Eric Snowfdf282d2019-01-11 14:26:55 -0700240 PyErr_Restore(exc, val, tb);
Victor Stinner11517102014-07-29 23:31:34 +0200241 return 0;
242}
243#endif /* MS_WINDOWS */
244
Tim Peters4f1b2082000-07-23 21:18:09 +0000245static void
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200246trip_signal(int sig_num)
247{
Victor Stinnerd49b1f12011-05-08 02:03:15 +0200248 unsigned char byte;
Victor Stinner11517102014-07-29 23:31:34 +0200249 int fd;
250 Py_ssize_t rc;
Victor Stinnerc13ef662011-05-25 02:35:58 +0200251
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +0200252 _Py_atomic_store_relaxed(&Handlers[sig_num].tripped, 1);
Victor Stinner11517102014-07-29 23:31:34 +0200253
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200254 /* Set is_tripped after setting .tripped, as it gets
255 cleared in PyErr_CheckSignals() before .tripped. */
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +0200256 _Py_atomic_store(&is_tripped, 1);
257
258 /* Notify ceval.c */
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200259 _PyEval_SignalReceived();
Nathaniel J. Smith4ae01492017-05-16 14:12:11 -0700260
261 /* And then write to the wakeup fd *after* setting all the globals and
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200262 doing the _PyEval_SignalReceived. We used to write to the wakeup fd
263 and then set the flag, but this allowed the following sequence of events
264 (especially on windows, where trip_signal may run in a new thread):
Nathaniel J. Smith4ae01492017-05-16 14:12:11 -0700265
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800266 - main thread blocks on select([wakeup.fd], ...)
Nathaniel J. Smith4ae01492017-05-16 14:12:11 -0700267 - signal arrives
268 - trip_signal writes to the wakeup fd
269 - the main thread wakes up
270 - the main thread checks the signal flags, sees that they're unset
271 - the main thread empties the wakeup fd
272 - the main thread goes back to sleep
273 - trip_signal sets the flags to request the Python-level signal handler
274 be run
275 - the main thread doesn't notice, because it's asleep
276
277 See bpo-30038 for more details.
278 */
279
Victor Stinner11517102014-07-29 23:31:34 +0200280#ifdef MS_WINDOWS
281 fd = Py_SAFE_DOWNCAST(wakeup.fd, SOCKET_T, int);
282#else
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800283 fd = wakeup.fd;
Victor Stinner11517102014-07-29 23:31:34 +0200284#endif
285
286 if (fd != INVALID_FD) {
Victor Stinnerc13ef662011-05-25 02:35:58 +0200287 byte = (unsigned char)sig_num;
Victor Stinner11517102014-07-29 23:31:34 +0200288#ifdef MS_WINDOWS
289 if (wakeup.use_send) {
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800290 rc = send(fd, &byte, 1, 0);
Victor Stinner11517102014-07-29 23:31:34 +0200291
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800292 if (rc < 0) {
293 int last_error = GetLastError();
294 if (wakeup.warn_on_full_buffer ||
295 last_error != WSAEWOULDBLOCK)
296 {
297 /* Py_AddPendingCall() isn't signal-safe, but we
298 still use it for this exceptional case. */
Eric Snowef4ac962019-02-24 15:40:47 -0800299 _Py_AddPendingCall(_PyRuntime.interpreters.main,
300 main_thread,
301 report_wakeup_send_error,
302 (void *)(intptr_t) last_error);
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800303 }
Victor Stinner11517102014-07-29 23:31:34 +0200304 }
305 }
306 else
307#endif
308 {
Victor Stinnere72fe392015-04-01 18:35:22 +0200309 /* _Py_write_noraise() retries write() if write() is interrupted by
310 a signal (fails with EINTR). */
311 rc = _Py_write_noraise(fd, &byte, 1);
Victor Stinner11517102014-07-29 23:31:34 +0200312
313 if (rc < 0) {
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800314 if (wakeup.warn_on_full_buffer ||
315 (errno != EWOULDBLOCK && errno != EAGAIN))
316 {
317 /* Py_AddPendingCall() isn't signal-safe, but we
318 still use it for this exceptional case. */
Eric Snowef4ac962019-02-24 15:40:47 -0800319 _Py_AddPendingCall(_PyRuntime.interpreters.main,
320 main_thread,
321 report_wakeup_write_error,
322 (void *)(intptr_t)errno);
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800323 }
Victor Stinner11517102014-07-29 23:31:34 +0200324 }
325 }
Victor Stinnerc13ef662011-05-25 02:35:58 +0200326 }
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200327}
328
329static void
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000330signal_handler(int sig_num)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000331{
Antoine Pitrou39a65912010-11-05 19:47:27 +0000332 int save_errno = errno;
333
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000334 /* See NOTES section above */
Antoine Pitrou39a65912010-11-05 19:47:27 +0000335 if (getpid() == main_pid)
Antoine Pitrou39a65912010-11-05 19:47:27 +0000336 {
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200337 trip_signal(sig_num);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000338 }
Antoine Pitrou39a65912010-11-05 19:47:27 +0000339
Jean-Paul Calderone6f137ca2010-05-09 03:18:57 +0000340#ifndef HAVE_SIGACTION
Antoine Pitrou39a65912010-11-05 19:47:27 +0000341#ifdef SIGCHLD
342 /* To avoid infinite recursion, this signal remains
343 reset until explicit re-instated.
344 Don't clear the 'func' field as it is our pointer
345 to the Python handler... */
346 if (sig_num != SIGCHLD)
347#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000348 /* If the handler was not set up with sigaction, reinstall it. See
Nick Coghland6009512014-11-20 21:39:37 +1000349 * Python/pylifecycle.c for the implementation of PyOS_setsig which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000350 * makes this true. See also issue8354. */
351 PyOS_setsig(sig_num, signal_handler);
Jean-Paul Calderone6f137ca2010-05-09 03:18:57 +0000352#endif
Antoine Pitrou39a65912010-11-05 19:47:27 +0000353
354 /* Issue #10311: asynchronously executing signal handlers should not
355 mutate errno under the feet of unsuspecting C code. */
356 errno = save_errno;
Antoine Pitrou6dd381e2011-11-21 21:26:56 +0100357
358#ifdef MS_WINDOWS
359 if (sig_num == SIGINT)
360 SetEvent(sigint_event);
361#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000362}
Guido van Rossume4485b01994-09-07 14:32:49 +0000363
Guido van Rossum06d511d1995-03-10 15:13:48 +0000364
Guido van Rossum1171ee61997-08-22 20:42:00 +0000365#ifdef HAVE_ALARM
Tal Einatc7027b72015-05-16 14:14:49 +0300366
367/*[clinic input]
368signal.alarm -> long
369
370 seconds: int
371 /
372
373Arrange for SIGALRM to arrive after the given number of seconds.
374[clinic start generated code]*/
375
376static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300377signal_alarm_impl(PyObject *module, int seconds)
378/*[clinic end generated code: output=144232290814c298 input=0d5e97e0e6f39e86]*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000379{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000380 /* alarm() returns the number of seconds remaining */
Tal Einatc7027b72015-05-16 14:14:49 +0300381 return (long)alarm(seconds);
Guido van Rossumaa0f4c71994-08-23 13:49:37 +0000382}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000383
Guido van Rossum06d511d1995-03-10 15:13:48 +0000384#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000385
Guido van Rossum1171ee61997-08-22 20:42:00 +0000386#ifdef HAVE_PAUSE
Tal Einatc7027b72015-05-16 14:14:49 +0300387
388/*[clinic input]
389signal.pause
390
391Wait until a signal arrives.
392[clinic start generated code]*/
393
Guido van Rossuma597dde1995-01-10 20:56:29 +0000394static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300395signal_pause_impl(PyObject *module)
396/*[clinic end generated code: output=391656788b3c3929 input=f03de0f875752062]*/
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000397{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000398 Py_BEGIN_ALLOW_THREADS
399 (void)pause();
400 Py_END_ALLOW_THREADS
401 /* make sure that any exceptions that got raised are propagated
402 * back into Python
403 */
404 if (PyErr_CheckSignals())
405 return NULL;
Barry Warsaw92971171997-01-03 00:14:25 +0000406
Tal Einatc7027b72015-05-16 14:14:49 +0300407 Py_RETURN_NONE;
Guido van Rossume4485b01994-09-07 14:32:49 +0000408}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000409
Guido van Rossum06d511d1995-03-10 15:13:48 +0000410#endif
Guido van Rossume4485b01994-09-07 14:32:49 +0000411
Vladimir Matveevc24c6c22019-01-08 01:58:25 -0800412/*[clinic input]
413signal.raise_signal
414
415 signalnum: int
416 /
417
418Send a signal to the executing process.
419[clinic start generated code]*/
420
421static PyObject *
422signal_raise_signal_impl(PyObject *module, int signalnum)
423/*[clinic end generated code: output=e2b014220aa6111d input=e90c0f9a42358de6]*/
424{
425 int err;
426 Py_BEGIN_ALLOW_THREADS
427 _Py_BEGIN_SUPPRESS_IPH
428 err = raise(signalnum);
429 _Py_END_SUPPRESS_IPH
430 Py_END_ALLOW_THREADS
431
432 if (err) {
433 return PyErr_SetFromErrno(PyExc_OSError);
434 }
435 Py_RETURN_NONE;
436}
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000437
Tal Einatc7027b72015-05-16 14:14:49 +0300438/*[clinic input]
439signal.signal
440
441 signalnum: int
442 handler: object
443 /
444
445Set the action for the given signal.
446
447The action can be SIG_DFL, SIG_IGN, or a callable Python object.
448The previous action is returned. See getsignal() for possible return values.
449
450*** IMPORTANT NOTICE ***
451A signal handler function is called with two arguments:
452the first is the signal number, the second is the interrupted stack frame.
453[clinic start generated code]*/
454
Guido van Rossume4485b01994-09-07 14:32:49 +0000455static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300456signal_signal_impl(PyObject *module, int signalnum, PyObject *handler)
457/*[clinic end generated code: output=b44cfda43780f3a1 input=deee84af5fa0432c]*/
Guido van Rossume4485b01994-09-07 14:32:49 +0000458{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000459 PyObject *old_handler;
460 void (*func)(int);
Brian Curtinef9efbd2010-08-06 19:27:32 +0000461#ifdef MS_WINDOWS
Tal Einatc7027b72015-05-16 14:14:49 +0300462 /* Validate that signalnum is one of the allowable signals */
463 switch (signalnum) {
Brian Curtinc734b312010-09-06 16:04:10 +0000464 case SIGABRT: break;
Brian Curtin9e88b5a2010-10-01 14:49:24 +0000465#ifdef SIGBREAK
466 /* Issue #10003: SIGBREAK is not documented as permitted, but works
467 and corresponds to CTRL_BREAK_EVENT. */
468 case SIGBREAK: break;
469#endif
Brian Curtinc734b312010-09-06 16:04:10 +0000470 case SIGFPE: break;
471 case SIGILL: break;
472 case SIGINT: break;
473 case SIGSEGV: break;
474 case SIGTERM: break;
475 default:
476 PyErr_SetString(PyExc_ValueError, "invalid signal value");
477 return NULL;
Brian Curtinef9efbd2010-08-06 19:27:32 +0000478 }
479#endif
Eric Snow64d6cc82019-02-23 15:40:43 -0700480 if (!is_main()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000481 PyErr_SetString(PyExc_ValueError,
482 "signal only works in main thread");
483 return NULL;
484 }
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 if (handler == IgnoreHandler)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000491 func = SIG_IGN;
Tal Einatc7027b72015-05-16 14:14:49 +0300492 else if (handler == DefaultHandler)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000493 func = SIG_DFL;
Tal Einatc7027b72015-05-16 14:14:49 +0300494 else if (!PyCallable_Check(handler)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000495 PyErr_SetString(PyExc_TypeError,
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000496"signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 return NULL;
498 }
499 else
500 func = signal_handler;
Antoine Pitrouf6f90ff2017-11-03 19:58:46 +0100501 /* Check for pending signals before changing signal handler */
Eric Snow64d6cc82019-02-23 15:40:43 -0700502 if (_PyErr_CheckSignals()) {
Antoine Pitrouf6f90ff2017-11-03 19:58:46 +0100503 return NULL;
504 }
Tal Einatc7027b72015-05-16 14:14:49 +0300505 if (PyOS_setsig(signalnum, func) == SIG_ERR) {
Victor Stinner388196e2011-05-10 17:13:00 +0200506 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000507 return NULL;
508 }
Tal Einatc7027b72015-05-16 14:14:49 +0300509 old_handler = Handlers[signalnum].func;
Tal Einatc7027b72015-05-16 14:14:49 +0300510 Py_INCREF(handler);
511 Handlers[signalnum].func = handler;
Antoine Pitrouc8c952c2013-05-04 23:16:59 +0200512 if (old_handler != NULL)
513 return old_handler;
514 else
515 Py_RETURN_NONE;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000516}
517
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000518
Tal Einatc7027b72015-05-16 14:14:49 +0300519/*[clinic input]
520signal.getsignal
521
522 signalnum: int
523 /
524
525Return the current action for the given signal.
526
527The return value can be:
528 SIG_IGN -- if the signal is being ignored
529 SIG_DFL -- if the default action for the signal is in effect
530 None -- if an unknown handler is in effect
531 anything else -- the callable Python object used as a handler
532[clinic start generated code]*/
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000533
Guido van Rossume4485b01994-09-07 14:32:49 +0000534static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300535signal_getsignal_impl(PyObject *module, int signalnum)
536/*[clinic end generated code: output=35b3e0e796fd555e input=ac23a00f19dfa509]*/
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000537{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000538 PyObject *old_handler;
Tal Einatc7027b72015-05-16 14:14:49 +0300539 if (signalnum < 1 || signalnum >= NSIG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000540 PyErr_SetString(PyExc_ValueError,
541 "signal number out of range");
542 return NULL;
543 }
Tal Einatc7027b72015-05-16 14:14:49 +0300544 old_handler = Handlers[signalnum].func;
Antoine Pitrouc8c952c2013-05-04 23:16:59 +0200545 if (old_handler != NULL) {
546 Py_INCREF(old_handler);
547 return old_handler;
548 }
549 else {
550 Py_RETURN_NONE;
551 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000552}
553
Antoine Pietri5d2a27d2018-03-12 14:42:34 +0100554
555/*[clinic input]
556signal.strsignal
557
558 signalnum: int
559 /
560
561Return the system description of the given signal.
562
563The return values can be such as "Interrupt", "Segmentation fault", etc.
564Returns None if the signal is not recognized.
565[clinic start generated code]*/
566
567static PyObject *
568signal_strsignal_impl(PyObject *module, int signalnum)
569/*[clinic end generated code: output=44e12e1e3b666261 input=b77914b03f856c74]*/
570{
571 char *res;
572
573 if (signalnum < 1 || signalnum >= NSIG) {
574 PyErr_SetString(PyExc_ValueError,
575 "signal number out of range");
576 return NULL;
577 }
578
Michael Osipov48ce4892018-08-23 15:27:19 +0200579#ifndef HAVE_STRSIGNAL
Antoine Pietri5d2a27d2018-03-12 14:42:34 +0100580 switch (signalnum) {
Michael Osipov48ce4892018-08-23 15:27:19 +0200581 /* Though being a UNIX, HP-UX does not provide strsignal(3). */
582#ifndef MS_WINDOWS
583 case SIGHUP:
584 res = "Hangup";
585 break;
586 case SIGALRM:
587 res = "Alarm clock";
588 break;
589 case SIGPIPE:
590 res = "Broken pipe";
591 break;
592 case SIGQUIT:
593 res = "Quit";
594 break;
595 case SIGCHLD:
596 res = "Child exited";
597 break;
598#endif
599 /* Custom redefinition of POSIX signals allowed on Windows. */
Antoine Pietri5d2a27d2018-03-12 14:42:34 +0100600 case SIGINT:
601 res = "Interrupt";
602 break;
603 case SIGILL:
604 res = "Illegal instruction";
605 break;
606 case SIGABRT:
607 res = "Aborted";
608 break;
609 case SIGFPE:
610 res = "Floating point exception";
611 break;
612 case SIGSEGV:
613 res = "Segmentation fault";
614 break;
615 case SIGTERM:
616 res = "Terminated";
617 break;
618 default:
619 Py_RETURN_NONE;
620 }
621#else
622 errno = 0;
623 res = strsignal(signalnum);
624
625 if (errno || res == NULL || strstr(res, "Unknown signal") != NULL)
626 Py_RETURN_NONE;
627#endif
628
629 return Py_BuildValue("s", res);
630}
631
Christian Heimes8640e742008-02-23 16:23:06 +0000632#ifdef HAVE_SIGINTERRUPT
Tal Einatc7027b72015-05-16 14:14:49 +0300633
634/*[clinic input]
635signal.siginterrupt
636
637 signalnum: int
638 flag: int
639 /
640
641Change system call restart behaviour.
642
643If flag is False, system calls will be restarted when interrupted by
644signal sig, else system calls will be interrupted.
645[clinic start generated code]*/
Christian Heimes8640e742008-02-23 16:23:06 +0000646
647static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300648signal_siginterrupt_impl(PyObject *module, int signalnum, int flag)
649/*[clinic end generated code: output=063816243d85dd19 input=4160acacca3e2099]*/
Christian Heimes8640e742008-02-23 16:23:06 +0000650{
Tal Einatc7027b72015-05-16 14:14:49 +0300651 if (signalnum < 1 || signalnum >= NSIG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000652 PyErr_SetString(PyExc_ValueError,
653 "signal number out of range");
654 return NULL;
655 }
Tal Einatc7027b72015-05-16 14:14:49 +0300656 if (siginterrupt(signalnum, flag)<0) {
Victor Stinner388196e2011-05-10 17:13:00 +0200657 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000658 return NULL;
659 }
Tal Einatc7027b72015-05-16 14:14:49 +0300660 Py_RETURN_NONE;
Christian Heimes8640e742008-02-23 16:23:06 +0000661}
662
663#endif
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000664
Tal Einatc7027b72015-05-16 14:14:49 +0300665
666static PyObject*
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800667signal_set_wakeup_fd(PyObject *self, PyObject *args, PyObject *kwds)
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000668{
Victor Stinnere134a7f2015-03-30 10:09:31 +0200669 struct _Py_stat_struct status;
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800670 static char *kwlist[] = {
671 "", "warn_on_full_buffer", NULL,
672 };
673 int warn_on_full_buffer = 1;
Victor Stinner11517102014-07-29 23:31:34 +0200674#ifdef MS_WINDOWS
675 PyObject *fdobj;
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100676 SOCKET_T sockfd, old_sockfd;
Victor Stinner11517102014-07-29 23:31:34 +0200677 int res;
678 int res_size = sizeof res;
679 PyObject *mod;
Victor Stinner11517102014-07-29 23:31:34 +0200680 int is_socket;
681
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800682 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|$p:set_wakeup_fd", kwlist,
683 &fdobj, &warn_on_full_buffer))
Victor Stinner11517102014-07-29 23:31:34 +0200684 return NULL;
685
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100686 sockfd = PyLong_AsSocket_t(fdobj);
687 if (sockfd == (SOCKET_T)(-1) && PyErr_Occurred())
Victor Stinner11517102014-07-29 23:31:34 +0200688 return NULL;
689#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000690 int fd, old_fd;
Victor Stinner11517102014-07-29 23:31:34 +0200691
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800692 if (!PyArg_ParseTupleAndKeywords(args, kwds, "i|$p:set_wakeup_fd", kwlist,
693 &fd, &warn_on_full_buffer))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000694 return NULL;
Victor Stinner11517102014-07-29 23:31:34 +0200695#endif
696
Eric Snow64d6cc82019-02-23 15:40:43 -0700697 if (!is_main()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000698 PyErr_SetString(PyExc_ValueError,
699 "set_wakeup_fd only works in main thread");
700 return NULL;
701 }
Victor Stinner0bffc942014-07-21 16:28:54 +0200702
Victor Stinner11517102014-07-29 23:31:34 +0200703#ifdef MS_WINDOWS
704 is_socket = 0;
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100705 if (sockfd != INVALID_FD) {
Victor Stinner11517102014-07-29 23:31:34 +0200706 /* Import the _socket module to call WSAStartup() */
707 mod = PyImport_ImportModuleNoBlock("_socket");
708 if (mod == NULL)
709 return NULL;
710 Py_DECREF(mod);
711
712 /* test the socket */
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100713 if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR,
Victor Stinner11517102014-07-29 23:31:34 +0200714 (char *)&res, &res_size) != 0) {
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100715 int fd, err;
716
717 err = WSAGetLastError();
Victor Stinner11517102014-07-29 23:31:34 +0200718 if (err != WSAENOTSOCK) {
719 PyErr_SetExcFromWindowsErr(PyExc_OSError, err);
720 return NULL;
721 }
722
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100723 fd = (int)sockfd;
Steve Dower940f33a2016-09-08 11:21:54 -0700724 if ((SOCKET_T)fd != sockfd) {
Victor Stinner11517102014-07-29 23:31:34 +0200725 PyErr_SetString(PyExc_ValueError, "invalid fd");
726 return NULL;
727 }
728
Victor Stinnere134a7f2015-03-30 10:09:31 +0200729 if (_Py_fstat(fd, &status) != 0)
Victor Stinner11517102014-07-29 23:31:34 +0200730 return NULL;
Victor Stinner38227602014-08-27 12:59:44 +0200731
732 /* on Windows, a file cannot be set to non-blocking mode */
Victor Stinner11517102014-07-29 23:31:34 +0200733 }
Victor Stinner38227602014-08-27 12:59:44 +0200734 else {
Victor Stinner11517102014-07-29 23:31:34 +0200735 is_socket = 1;
Victor Stinner38227602014-08-27 12:59:44 +0200736
737 /* Windows does not provide a function to test if a socket
738 is in non-blocking mode */
739 }
Victor Stinner11517102014-07-29 23:31:34 +0200740 }
741
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100742 old_sockfd = wakeup.fd;
743 wakeup.fd = sockfd;
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800744 wakeup.warn_on_full_buffer = warn_on_full_buffer;
Victor Stinner11517102014-07-29 23:31:34 +0200745 wakeup.use_send = is_socket;
746
Victor Stinnercf40a9e2015-02-12 16:34:54 +0100747 if (old_sockfd != INVALID_FD)
748 return PyLong_FromSocket_t(old_sockfd);
Victor Stinner11517102014-07-29 23:31:34 +0200749 else
750 return PyLong_FromLong(-1);
751#else
Victor Stinnerd18ccd12014-07-24 21:58:53 +0200752 if (fd != -1) {
Victor Stinner38227602014-08-27 12:59:44 +0200753 int blocking;
754
Victor Stinnere134a7f2015-03-30 10:09:31 +0200755 if (_Py_fstat(fd, &status) != 0)
Victor Stinner11517102014-07-29 23:31:34 +0200756 return NULL;
Victor Stinner38227602014-08-27 12:59:44 +0200757
758 blocking = _Py_get_blocking(fd);
759 if (blocking < 0)
760 return NULL;
761 if (blocking) {
762 PyErr_Format(PyExc_ValueError,
763 "the fd %i must be in non-blocking mode",
764 fd);
765 return NULL;
766 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000767 }
Victor Stinner0bffc942014-07-21 16:28:54 +0200768
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800769 old_fd = wakeup.fd;
770 wakeup.fd = fd;
771 wakeup.warn_on_full_buffer = warn_on_full_buffer;
Victor Stinner0bffc942014-07-21 16:28:54 +0200772
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000773 return PyLong_FromLong(old_fd);
Victor Stinner11517102014-07-29 23:31:34 +0200774#endif
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000775}
776
777PyDoc_STRVAR(set_wakeup_fd_doc,
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800778"set_wakeup_fd(fd, *, warn_on_full_buffer=True) -> fd\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000779\n\
Victor Stinner11517102014-07-29 23:31:34 +0200780Sets the fd to be written to (with the signal number) when a signal\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000781comes in. A library can use this to wakeup select or poll.\n\
Victor Stinner11517102014-07-29 23:31:34 +0200782The previous fd or -1 is returned.\n\
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000783\n\
784The fd must be non-blocking.");
785
786/* C API for the same, without all the error checking */
787int
788PySignal_SetWakeupFd(int fd)
789{
Victor Stinner11517102014-07-29 23:31:34 +0200790 int old_fd;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000791 if (fd < 0)
792 fd = -1;
Victor Stinner11517102014-07-29 23:31:34 +0200793
794#ifdef MS_WINDOWS
795 old_fd = Py_SAFE_DOWNCAST(wakeup.fd, SOCKET_T, int);
Victor Stinner11517102014-07-29 23:31:34 +0200796#else
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800797 old_fd = wakeup.fd;
Victor Stinner11517102014-07-29 23:31:34 +0200798#endif
Nathaniel J. Smith902ab802017-12-17 20:10:18 -0800799 wakeup.fd = fd;
800 wakeup.warn_on_full_buffer = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000801 return old_fd;
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000802}
803
804
Martin v. Löwis823725e2008-03-24 13:39:54 +0000805#ifdef HAVE_SETITIMER
Tal Einatc7027b72015-05-16 14:14:49 +0300806
807/*[clinic input]
808signal.setitimer
809
810 which: int
Victor Stinneref611c92017-10-13 13:49:43 -0700811 seconds: object
812 interval: object(c_default="NULL") = 0.0
Tal Einatc7027b72015-05-16 14:14:49 +0300813 /
814
815Sets given itimer (one of ITIMER_REAL, ITIMER_VIRTUAL or ITIMER_PROF).
816
817The timer will fire after value seconds and after that every interval seconds.
818The itimer can be cleared by setting seconds to zero.
819
820Returns old values as a tuple: (delay, interval).
821[clinic start generated code]*/
822
Martin v. Löwis823725e2008-03-24 13:39:54 +0000823static PyObject *
Victor Stinneref611c92017-10-13 13:49:43 -0700824signal_setitimer_impl(PyObject *module, int which, PyObject *seconds,
825 PyObject *interval)
826/*[clinic end generated code: output=65f9dcbddc35527b input=de43daf194e6f66f]*/
Martin v. Löwis823725e2008-03-24 13:39:54 +0000827{
Martin v. Löwis823725e2008-03-24 13:39:54 +0000828 struct itimerval new, old;
829
Victor Stinneref611c92017-10-13 13:49:43 -0700830 if (timeval_from_double(seconds, &new.it_value) < 0) {
831 return NULL;
832 }
833 if (timeval_from_double(interval, &new.it_interval) < 0) {
834 return NULL;
835 }
836
Martin v. Löwis823725e2008-03-24 13:39:54 +0000837 /* Let OS check "which" value */
838 if (setitimer(which, &new, &old) != 0) {
Tal Einatc7027b72015-05-16 14:14:49 +0300839 PyErr_SetFromErrno(ItimerError);
840 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000841 }
842
843 return itimer_retval(&old);
844}
845
Martin v. Löwis823725e2008-03-24 13:39:54 +0000846#endif
847
848
849#ifdef HAVE_GETITIMER
Tal Einatc7027b72015-05-16 14:14:49 +0300850
851/*[clinic input]
852signal.getitimer
853
854 which: int
855 /
856
857Returns current value of given itimer.
858[clinic start generated code]*/
859
Martin v. Löwis823725e2008-03-24 13:39:54 +0000860static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300861signal_getitimer_impl(PyObject *module, int which)
862/*[clinic end generated code: output=9e053175d517db40 input=f7d21d38f3490627]*/
Martin v. Löwis823725e2008-03-24 13:39:54 +0000863{
Martin v. Löwis823725e2008-03-24 13:39:54 +0000864 struct itimerval old;
865
Martin v. Löwis823725e2008-03-24 13:39:54 +0000866 if (getitimer(which, &old) != 0) {
Tal Einatc7027b72015-05-16 14:14:49 +0300867 PyErr_SetFromErrno(ItimerError);
868 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000869 }
870
871 return itimer_retval(&old);
872}
873
Martin v. Löwis823725e2008-03-24 13:39:54 +0000874#endif
875
Victor Stinnerb3e72192011-05-08 01:46:11 +0200876#if defined(PYPTHREAD_SIGMASK) || defined(HAVE_SIGPENDING)
Victor Stinner35b300c2011-05-04 13:20:35 +0200877static PyObject*
878sigset_to_set(sigset_t mask)
879{
880 PyObject *signum, *result;
881 int sig;
882
883 result = PySet_New(0);
884 if (result == NULL)
885 return NULL;
886
887 for (sig = 1; sig < NSIG; sig++) {
888 if (sigismember(&mask, sig) != 1)
889 continue;
890
891 /* Handle the case where it is a member by adding the signal to
892 the result list. Ignore the other cases because they mean the
893 signal isn't a member of the mask or the signal was invalid,
894 and an invalid signal must have been our fault in constructing
895 the loop boundaries. */
896 signum = PyLong_FromLong(sig);
897 if (signum == NULL) {
898 Py_DECREF(result);
899 return NULL;
900 }
901 if (PySet_Add(result, signum) == -1) {
902 Py_DECREF(signum);
903 Py_DECREF(result);
904 return NULL;
905 }
906 Py_DECREF(signum);
907 }
908 return result;
909}
Victor Stinnerb3e72192011-05-08 01:46:11 +0200910#endif
Victor Stinner35b300c2011-05-04 13:20:35 +0200911
Victor Stinnerb3e72192011-05-08 01:46:11 +0200912#ifdef PYPTHREAD_SIGMASK
Tal Einatc7027b72015-05-16 14:14:49 +0300913
914/*[clinic input]
915signal.pthread_sigmask
916
917 how: int
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300918 mask: sigset_t
Tal Einatc7027b72015-05-16 14:14:49 +0300919 /
920
921Fetch and/or change the signal mask of the calling thread.
922[clinic start generated code]*/
923
Victor Stinnera9293352011-04-30 15:21:58 +0200924static PyObject *
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300925signal_pthread_sigmask_impl(PyObject *module, int how, sigset_t mask)
926/*[clinic end generated code: output=0562c0fb192981a8 input=85bcebda442fa77f]*/
Victor Stinnera9293352011-04-30 15:21:58 +0200927{
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300928 sigset_t previous;
Victor Stinnera9293352011-04-30 15:21:58 +0200929 int err;
930
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300931 err = pthread_sigmask(how, &mask, &previous);
Victor Stinnera9293352011-04-30 15:21:58 +0200932 if (err != 0) {
933 errno = err;
Victor Stinnerb3e72192011-05-08 01:46:11 +0200934 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnera9293352011-04-30 15:21:58 +0200935 return NULL;
936 }
937
Victor Stinnerd0e516d2011-05-03 14:57:12 +0200938 /* if signals was unblocked, signal handlers have been called */
939 if (PyErr_CheckSignals())
940 return NULL;
941
Victor Stinner35b300c2011-05-04 13:20:35 +0200942 return sigset_to_set(previous);
Victor Stinnera9293352011-04-30 15:21:58 +0200943}
944
Victor Stinnera9293352011-04-30 15:21:58 +0200945#endif /* #ifdef PYPTHREAD_SIGMASK */
946
Martin v. Löwis823725e2008-03-24 13:39:54 +0000947
Victor Stinnerb3e72192011-05-08 01:46:11 +0200948#ifdef HAVE_SIGPENDING
Tal Einatc7027b72015-05-16 14:14:49 +0300949
950/*[clinic input]
951signal.sigpending
952
953Examine pending signals.
954
955Returns a set of signal numbers that are pending for delivery to
956the calling thread.
957[clinic start generated code]*/
958
Victor Stinnerb3e72192011-05-08 01:46:11 +0200959static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300960signal_sigpending_impl(PyObject *module)
961/*[clinic end generated code: output=53375ffe89325022 input=e0036c016f874e29]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +0200962{
963 int err;
964 sigset_t mask;
965 err = sigpending(&mask);
966 if (err)
967 return PyErr_SetFromErrno(PyExc_OSError);
968 return sigset_to_set(mask);
969}
970
Victor Stinnerb3e72192011-05-08 01:46:11 +0200971#endif /* #ifdef HAVE_SIGPENDING */
972
973
974#ifdef HAVE_SIGWAIT
Tal Einatc7027b72015-05-16 14:14:49 +0300975
976/*[clinic input]
977signal.sigwait
978
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300979 sigset: sigset_t
Tal Einatc7027b72015-05-16 14:14:49 +0300980 /
981
982Wait for a signal.
983
984Suspend execution of the calling thread until the delivery of one of the
985signals specified in the signal set sigset. The function accepts the signal
986and returns the signal number.
987[clinic start generated code]*/
988
Victor Stinnerb3e72192011-05-08 01:46:11 +0200989static PyObject *
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300990signal_sigwait_impl(PyObject *module, sigset_t sigset)
991/*[clinic end generated code: output=f43770699d682f96 input=a6fbd47b1086d119]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +0200992{
Victor Stinnerb3e72192011-05-08 01:46:11 +0200993 int err, signum;
994
Victor Stinner10c30d62011-06-10 01:39:53 +0200995 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakad54cfb12018-05-08 07:48:50 +0300996 err = sigwait(&sigset, &signum);
Victor Stinner10c30d62011-06-10 01:39:53 +0200997 Py_END_ALLOW_THREADS
Victor Stinnerb3e72192011-05-08 01:46:11 +0200998 if (err) {
999 errno = err;
1000 return PyErr_SetFromErrno(PyExc_OSError);
1001 }
1002
1003 return PyLong_FromLong(signum);
1004}
1005
Tal Einatc7027b72015-05-16 14:14:49 +03001006#endif /* #ifdef HAVE_SIGWAIT */
1007
Victor Stinnerb3e72192011-05-08 01:46:11 +02001008
Antoine Pitrou9d3627e2018-05-04 13:00:50 +02001009#if defined(HAVE_SIGFILLSET) || defined(MS_WINDOWS)
1010
1011/*[clinic input]
1012signal.valid_signals
1013
1014Return a set of valid signal numbers on this platform.
1015
1016The signal numbers returned by this function can be safely passed to
1017functions like `pthread_sigmask`.
1018[clinic start generated code]*/
1019
1020static PyObject *
1021signal_valid_signals_impl(PyObject *module)
1022/*[clinic end generated code: output=1609cffbcfcf1314 input=86a3717ff25288f2]*/
1023{
1024#ifdef MS_WINDOWS
1025#ifdef SIGBREAK
1026 PyObject *tup = Py_BuildValue("(iiiiiii)", SIGABRT, SIGBREAK, SIGFPE,
1027 SIGILL, SIGINT, SIGSEGV, SIGTERM);
1028#else
1029 PyObject *tup = Py_BuildValue("(iiiiii)", SIGABRT, SIGFPE, SIGILL,
1030 SIGINT, SIGSEGV, SIGTERM);
1031#endif
1032 if (tup == NULL) {
1033 return NULL;
1034 }
1035 PyObject *set = PySet_New(tup);
1036 Py_DECREF(tup);
1037 return set;
1038#else
1039 sigset_t mask;
1040 if (sigemptyset(&mask) || sigfillset(&mask)) {
1041 return PyErr_SetFromErrno(PyExc_OSError);
1042 }
1043 return sigset_to_set(mask);
1044#endif
1045}
1046
1047#endif /* #if defined(HAVE_SIGFILLSET) || defined(MS_WINDOWS) */
1048
1049
Ross Lagerwallbc808222011-06-25 12:13:40 +02001050#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
1051static int initialized;
1052static PyStructSequence_Field struct_siginfo_fields[] = {
1053 {"si_signo", "signal number"},
1054 {"si_code", "signal code"},
1055 {"si_errno", "errno associated with this signal"},
1056 {"si_pid", "sending process ID"},
1057 {"si_uid", "real user ID of sending process"},
1058 {"si_status", "exit value or signal"},
1059 {"si_band", "band event for SIGPOLL"},
1060 {0}
1061};
1062
1063PyDoc_STRVAR(struct_siginfo__doc__,
1064"struct_siginfo: Result from sigwaitinfo or sigtimedwait.\n\n\
1065This object may be accessed either as a tuple of\n\
1066(si_signo, si_code, si_errno, si_pid, si_uid, si_status, si_band),\n\
1067or via the attributes si_signo, si_code, and so on.");
1068
1069static PyStructSequence_Desc struct_siginfo_desc = {
1070 "signal.struct_siginfo", /* name */
1071 struct_siginfo__doc__, /* doc */
1072 struct_siginfo_fields, /* fields */
1073 7 /* n_in_sequence */
1074};
1075
1076static PyTypeObject SiginfoType;
1077
1078static PyObject *
1079fill_siginfo(siginfo_t *si)
1080{
1081 PyObject *result = PyStructSequence_New(&SiginfoType);
1082 if (!result)
1083 return NULL;
1084
1085 PyStructSequence_SET_ITEM(result, 0, PyLong_FromLong((long)(si->si_signo)));
1086 PyStructSequence_SET_ITEM(result, 1, PyLong_FromLong((long)(si->si_code)));
1087 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si->si_errno)));
1088 PyStructSequence_SET_ITEM(result, 3, PyLong_FromPid(si->si_pid));
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02001089 PyStructSequence_SET_ITEM(result, 4, _PyLong_FromUid(si->si_uid));
Ross Lagerwallbc808222011-06-25 12:13:40 +02001090 PyStructSequence_SET_ITEM(result, 5,
1091 PyLong_FromLong((long)(si->si_status)));
Zachary Ware6a6967e2016-10-01 00:47:27 -05001092#ifdef HAVE_SIGINFO_T_SI_BAND
Ross Lagerwallbc808222011-06-25 12:13:40 +02001093 PyStructSequence_SET_ITEM(result, 6, PyLong_FromLong(si->si_band));
Zachary Ware6a6967e2016-10-01 00:47:27 -05001094#else
1095 PyStructSequence_SET_ITEM(result, 6, PyLong_FromLong(0L));
1096#endif
Ross Lagerwallbc808222011-06-25 12:13:40 +02001097 if (PyErr_Occurred()) {
1098 Py_DECREF(result);
1099 return NULL;
1100 }
1101
1102 return result;
1103}
1104#endif
1105
1106#ifdef HAVE_SIGWAITINFO
Tal Einatc7027b72015-05-16 14:14:49 +03001107
1108/*[clinic input]
1109signal.sigwaitinfo
1110
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001111 sigset: sigset_t
Tal Einatc7027b72015-05-16 14:14:49 +03001112 /
1113
1114Wait synchronously until one of the signals in *sigset* is delivered.
1115
1116Returns a struct_siginfo containing information about the signal.
1117[clinic start generated code]*/
1118
Ross Lagerwallbc808222011-06-25 12:13:40 +02001119static PyObject *
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001120signal_sigwaitinfo_impl(PyObject *module, sigset_t sigset)
1121/*[clinic end generated code: output=1eb2f1fa236fdbca input=3d1a7e1f27fc664c]*/
Ross Lagerwallbc808222011-06-25 12:13:40 +02001122{
Ross Lagerwallbc808222011-06-25 12:13:40 +02001123 siginfo_t si;
1124 int err;
Victor Stinnera453cd82015-03-20 12:54:28 +01001125 int async_err = 0;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001126
Victor Stinnera453cd82015-03-20 12:54:28 +01001127 do {
1128 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001129 err = sigwaitinfo(&sigset, &si);
Victor Stinnera453cd82015-03-20 12:54:28 +01001130 Py_END_ALLOW_THREADS
1131 } while (err == -1
1132 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Ross Lagerwallbc808222011-06-25 12:13:40 +02001133 if (err == -1)
Victor Stinnera453cd82015-03-20 12:54:28 +01001134 return (!async_err) ? PyErr_SetFromErrno(PyExc_OSError) : NULL;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001135
1136 return fill_siginfo(&si);
1137}
1138
Ross Lagerwallbc808222011-06-25 12:13:40 +02001139#endif /* #ifdef HAVE_SIGWAITINFO */
1140
1141#ifdef HAVE_SIGTIMEDWAIT
Tal Einatc7027b72015-05-16 14:14:49 +03001142
1143/*[clinic input]
1144signal.sigtimedwait
1145
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001146 sigset: sigset_t
Serhiy Storchaka6b680cd2015-05-16 15:57:56 +03001147 timeout as timeout_obj: object
Tal Einatc7027b72015-05-16 14:14:49 +03001148 /
1149
1150Like sigwaitinfo(), but with a timeout.
1151
1152The timeout is specified in seconds, with floating point numbers allowed.
1153[clinic start generated code]*/
1154
Ross Lagerwallbc808222011-06-25 12:13:40 +02001155static PyObject *
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001156signal_sigtimedwait_impl(PyObject *module, sigset_t sigset,
Serhiy Storchaka6b680cd2015-05-16 15:57:56 +03001157 PyObject *timeout_obj)
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001158/*[clinic end generated code: output=59c8971e8ae18a64 input=87fd39237cf0b7ba]*/
Ross Lagerwallbc808222011-06-25 12:13:40 +02001159{
Victor Stinnera453cd82015-03-20 12:54:28 +01001160 struct timespec ts;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001161 siginfo_t si;
Victor Stinnera453cd82015-03-20 12:54:28 +01001162 int res;
Victor Stinner34dc0f42015-03-27 18:19:03 +01001163 _PyTime_t timeout, deadline, monotonic;
Ross Lagerwallbc808222011-06-25 12:13:40 +02001164
Victor Stinner869e1772015-03-30 03:49:14 +02001165 if (_PyTime_FromSecondsObject(&timeout,
1166 timeout_obj, _PyTime_ROUND_CEILING) < 0)
Ross Lagerwallbc808222011-06-25 12:13:40 +02001167 return NULL;
1168
Victor Stinnera453cd82015-03-20 12:54:28 +01001169 if (timeout < 0) {
Ross Lagerwallbc808222011-06-25 12:13:40 +02001170 PyErr_SetString(PyExc_ValueError, "timeout must be non-negative");
1171 return NULL;
1172 }
1173
Victor Stinner34dc0f42015-03-27 18:19:03 +01001174 deadline = _PyTime_GetMonotonicClock() + timeout;
Victor Stinnera453cd82015-03-20 12:54:28 +01001175
1176 do {
Victor Stinner34dc0f42015-03-27 18:19:03 +01001177 if (_PyTime_AsTimespec(timeout, &ts) < 0)
1178 return NULL;
Victor Stinnera453cd82015-03-20 12:54:28 +01001179
1180 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001181 res = sigtimedwait(&sigset, &si, &ts);
Victor Stinnera453cd82015-03-20 12:54:28 +01001182 Py_END_ALLOW_THREADS
1183
1184 if (res != -1)
1185 break;
1186
1187 if (errno != EINTR) {
1188 if (errno == EAGAIN)
1189 Py_RETURN_NONE;
1190 else
1191 return PyErr_SetFromErrno(PyExc_OSError);
1192 }
1193
1194 /* sigtimedwait() was interrupted by a signal (EINTR) */
1195 if (PyErr_CheckSignals())
1196 return NULL;
1197
Victor Stinner34dc0f42015-03-27 18:19:03 +01001198 monotonic = _PyTime_GetMonotonicClock();
1199 timeout = deadline - monotonic;
Victor Stinner6aa446c2015-03-30 21:33:51 +02001200 if (timeout < 0)
Victor Stinnera453cd82015-03-20 12:54:28 +01001201 break;
1202 } while (1);
Ross Lagerwallbc808222011-06-25 12:13:40 +02001203
1204 return fill_siginfo(&si);
1205}
1206
Ross Lagerwallbc808222011-06-25 12:13:40 +02001207#endif /* #ifdef HAVE_SIGTIMEDWAIT */
1208
Victor Stinnerb3e72192011-05-08 01:46:11 +02001209
Antoine Pitroua6a4dc82017-09-07 18:56:24 +02001210#if defined(HAVE_PTHREAD_KILL)
Tal Einatc7027b72015-05-16 14:14:49 +03001211
1212/*[clinic input]
1213signal.pthread_kill
1214
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +02001215 thread_id: unsigned_long(bitwise=True)
Tal Einatc7027b72015-05-16 14:14:49 +03001216 signalnum: int
1217 /
1218
1219Send a signal to a thread.
1220[clinic start generated code]*/
1221
Victor Stinnerb3e72192011-05-08 01:46:11 +02001222static PyObject *
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +02001223signal_pthread_kill_impl(PyObject *module, unsigned long thread_id,
1224 int signalnum)
1225/*[clinic end generated code: output=7629919b791bc27f input=1d901f2c7bb544ff]*/
Victor Stinnerb3e72192011-05-08 01:46:11 +02001226{
Victor Stinnerb3e72192011-05-08 01:46:11 +02001227 int err;
1228
Tal Einatc7027b72015-05-16 14:14:49 +03001229 err = pthread_kill((pthread_t)thread_id, signalnum);
Victor Stinnerb3e72192011-05-08 01:46:11 +02001230 if (err != 0) {
1231 errno = err;
1232 PyErr_SetFromErrno(PyExc_OSError);
1233 return NULL;
1234 }
1235
1236 /* the signal may have been send to the current thread */
1237 if (PyErr_CheckSignals())
1238 return NULL;
1239
1240 Py_RETURN_NONE;
1241}
1242
Antoine Pitroua6a4dc82017-09-07 18:56:24 +02001243#endif /* #if defined(HAVE_PTHREAD_KILL) */
Victor Stinnerb3e72192011-05-08 01:46:11 +02001244
1245
1246
Tal Einatc7027b72015-05-16 14:14:49 +03001247/* List of functions defined in the module -- some of the methoddefs are
1248 defined to nothing if the corresponding C function is not available. */
Barry Warsaw92971171997-01-03 00:14:25 +00001249static PyMethodDef signal_methods[] = {
Tal Einatc7027b72015-05-16 14:14:49 +03001250 {"default_int_handler", signal_default_int_handler, METH_VARARGS, default_int_handler_doc},
1251 SIGNAL_ALARM_METHODDEF
1252 SIGNAL_SETITIMER_METHODDEF
1253 SIGNAL_GETITIMER_METHODDEF
1254 SIGNAL_SIGNAL_METHODDEF
Vladimir Matveevc24c6c22019-01-08 01:58:25 -08001255 SIGNAL_RAISE_SIGNAL_METHODDEF
Antoine Pietri5d2a27d2018-03-12 14:42:34 +01001256 SIGNAL_STRSIGNAL_METHODDEF
Tal Einatc7027b72015-05-16 14:14:49 +03001257 SIGNAL_GETSIGNAL_METHODDEF
Serhiy Storchaka62be7422018-11-27 13:27:31 +02001258 {"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 +03001259 SIGNAL_SIGINTERRUPT_METHODDEF
1260 SIGNAL_PAUSE_METHODDEF
1261 SIGNAL_PTHREAD_KILL_METHODDEF
1262 SIGNAL_PTHREAD_SIGMASK_METHODDEF
1263 SIGNAL_SIGPENDING_METHODDEF
1264 SIGNAL_SIGWAIT_METHODDEF
1265 SIGNAL_SIGWAITINFO_METHODDEF
1266 SIGNAL_SIGTIMEDWAIT_METHODDEF
Antoine Pitrou9d3627e2018-05-04 13:00:50 +02001267#if defined(HAVE_SIGFILLSET) || defined(MS_WINDOWS)
1268 SIGNAL_VALID_SIGNALS_METHODDEF
1269#endif
Tal Einatc7027b72015-05-16 14:14:49 +03001270 {NULL, NULL} /* sentinel */
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001271};
1272
Barry Warsaw92971171997-01-03 00:14:25 +00001273
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001274PyDoc_STRVAR(module_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001275"This module provides mechanisms to use signal handlers in Python.\n\
1276\n\
1277Functions:\n\
1278\n\
1279alarm() -- cause SIGALRM after a specified time [Unix only]\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001280setitimer() -- cause a signal (described below) after a specified\n\
1281 float time and the timer may restart then [Unix only]\n\
1282getitimer() -- get current value of timer [Unix only]\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001283signal() -- set the action for a given signal\n\
1284getsignal() -- get the signal action for a given signal\n\
1285pause() -- wait until a signal arrives [Unix only]\n\
1286default_int_handler() -- default SIGINT handler\n\
1287\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001288signal constants:\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001289SIG_DFL -- used to refer to the system default handler\n\
1290SIG_IGN -- used to ignore the signal\n\
1291NSIG -- number of defined signals\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001292SIGINT, SIGTERM, etc. -- signal numbers\n\
1293\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +00001294itimer constants:\n\
1295ITIMER_REAL -- decrements in real time, and delivers SIGALRM upon\n\
1296 expiration\n\
1297ITIMER_VIRTUAL -- decrements only when the process is executing,\n\
1298 and delivers SIGVTALRM upon expiration\n\
1299ITIMER_PROF -- decrements both when the process is executing and\n\
1300 when the system is executing on behalf of the process.\n\
1301 Coupled with ITIMER_VIRTUAL, this timer is usually\n\
1302 used to profile the time spent by the application\n\
1303 in user and kernel space. SIGPROF is delivered upon\n\
1304 expiration.\n\
1305\n\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001306*** IMPORTANT NOTICE ***\n\
1307A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001308the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001309
Martin v. Löwis1a214512008-06-11 05:26:20 +00001310static struct PyModuleDef signalmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001311 PyModuleDef_HEAD_INIT,
Brett Cannon815a6f32014-04-04 10:20:28 -04001312 "_signal",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001313 module_doc,
1314 -1,
1315 signal_methods,
1316 NULL,
1317 NULL,
1318 NULL,
1319 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001320};
1321
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001322PyMODINIT_FUNC
Giampaolo Rodola'e09fb712014-04-04 15:34:17 +02001323PyInit__signal(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001324{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001325 PyObject *m, *d, *x;
1326 int i;
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001327
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001328 main_thread = PyThread_get_thread_ident();
1329 main_pid = getpid();
Eric Snow64d6cc82019-02-23 15:40:43 -07001330 main_interp = _PyInterpreterState_Get();
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001331
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001332 /* Create the module and add the functions */
1333 m = PyModule_Create(&signalmodule);
1334 if (m == NULL)
1335 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001336
Ross Lagerwallbc808222011-06-25 12:13:40 +02001337#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
Victor Stinner1c8f0592013-07-22 22:24:54 +02001338 if (!initialized) {
1339 if (PyStructSequence_InitType2(&SiginfoType, &struct_siginfo_desc) < 0)
1340 return NULL;
1341 }
Ross Lagerwallbc808222011-06-25 12:13:40 +02001342 Py_INCREF((PyObject*) &SiginfoType);
1343 PyModule_AddObject(m, "struct_siginfo", (PyObject*) &SiginfoType);
1344 initialized = 1;
1345#endif
1346
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001347 /* Add some symbolic constants to the module */
1348 d = PyModule_GetDict(m);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001349
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001350 x = DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL);
1351 if (!x || PyDict_SetItemString(d, "SIG_DFL", x) < 0)
1352 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001353
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001354 x = IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN);
1355 if (!x || PyDict_SetItemString(d, "SIG_IGN", x) < 0)
1356 goto finally;
Barry Warsaw92971171997-01-03 00:14:25 +00001357
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001358 x = PyLong_FromLong((long)NSIG);
1359 if (!x || PyDict_SetItemString(d, "NSIG", x) < 0)
1360 goto finally;
1361 Py_DECREF(x);
Barry Warsaw92971171997-01-03 00:14:25 +00001362
Victor Stinnera9293352011-04-30 15:21:58 +02001363#ifdef SIG_BLOCK
Victor Stinner72c53b52011-05-02 16:15:43 +02001364 if (PyModule_AddIntMacro(m, SIG_BLOCK))
1365 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001366#endif
Victor Stinnera9293352011-04-30 15:21:58 +02001367#ifdef SIG_UNBLOCK
Victor Stinner72c53b52011-05-02 16:15:43 +02001368 if (PyModule_AddIntMacro(m, SIG_UNBLOCK))
1369 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001370#endif
Victor Stinnera9293352011-04-30 15:21:58 +02001371#ifdef SIG_SETMASK
Victor Stinner72c53b52011-05-02 16:15:43 +02001372 if (PyModule_AddIntMacro(m, SIG_SETMASK))
1373 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001374#endif
1375
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001376 x = IntHandler = PyDict_GetItemString(d, "default_int_handler");
1377 if (!x)
1378 goto finally;
1379 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +00001380
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001381 _Py_atomic_store_relaxed(&Handlers[0].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001382 for (i = 1; i < NSIG; i++) {
1383 void (*t)(int);
1384 t = PyOS_getsig(i);
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001385 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001386 if (t == SIG_DFL)
1387 Handlers[i].func = DefaultHandler;
1388 else if (t == SIG_IGN)
1389 Handlers[i].func = IgnoreHandler;
1390 else
1391 Handlers[i].func = Py_None; /* None of our business */
1392 Py_INCREF(Handlers[i].func);
1393 }
1394 if (Handlers[SIGINT].func == DefaultHandler) {
1395 /* Install default int handler */
1396 Py_INCREF(IntHandler);
Serhiy Storchaka57a01d32016-04-10 18:05:40 +03001397 Py_SETREF(Handlers[SIGINT].func, IntHandler);
pkerlinge905c842018-06-01 09:47:18 +00001398 PyOS_setsig(SIGINT, signal_handler);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001399 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001400
1401#ifdef SIGHUP
Christian Heimes6782b142016-09-09 00:11:45 +02001402 if (PyModule_AddIntMacro(m, SIGHUP))
1403 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001404#endif
1405#ifdef SIGINT
Christian Heimes6782b142016-09-09 00:11:45 +02001406 if (PyModule_AddIntMacro(m, SIGINT))
1407 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001408#endif
Tim Peters1ce3cf72001-10-01 17:58:40 +00001409#ifdef SIGBREAK
Christian Heimes6782b142016-09-09 00:11:45 +02001410 if (PyModule_AddIntMacro(m, SIGBREAK))
1411 goto finally;
Tim Peters1ce3cf72001-10-01 17:58:40 +00001412#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001413#ifdef SIGQUIT
Christian Heimes6782b142016-09-09 00:11:45 +02001414 if (PyModule_AddIntMacro(m, SIGQUIT))
1415 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001416#endif
1417#ifdef SIGILL
Christian Heimes6782b142016-09-09 00:11:45 +02001418 if (PyModule_AddIntMacro(m, SIGILL))
1419 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001420#endif
1421#ifdef SIGTRAP
Christian Heimes6782b142016-09-09 00:11:45 +02001422 if (PyModule_AddIntMacro(m, SIGTRAP))
1423 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001424#endif
1425#ifdef SIGIOT
Christian Heimes6782b142016-09-09 00:11:45 +02001426 if (PyModule_AddIntMacro(m, SIGIOT))
1427 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001428#endif
1429#ifdef SIGABRT
Christian Heimes6782b142016-09-09 00:11:45 +02001430 if (PyModule_AddIntMacro(m, SIGABRT))
1431 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001432#endif
1433#ifdef SIGEMT
Christian Heimes6782b142016-09-09 00:11:45 +02001434 if (PyModule_AddIntMacro(m, SIGEMT))
1435 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001436#endif
1437#ifdef SIGFPE
Christian Heimes6782b142016-09-09 00:11:45 +02001438 if (PyModule_AddIntMacro(m, SIGFPE))
1439 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001440#endif
1441#ifdef SIGKILL
Christian Heimes6782b142016-09-09 00:11:45 +02001442 if (PyModule_AddIntMacro(m, SIGKILL))
1443 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001444#endif
1445#ifdef SIGBUS
Christian Heimes6782b142016-09-09 00:11:45 +02001446 if (PyModule_AddIntMacro(m, SIGBUS))
1447 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001448#endif
1449#ifdef SIGSEGV
Christian Heimes6782b142016-09-09 00:11:45 +02001450 if (PyModule_AddIntMacro(m, SIGSEGV))
1451 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001452#endif
1453#ifdef SIGSYS
Christian Heimes6782b142016-09-09 00:11:45 +02001454 if (PyModule_AddIntMacro(m, SIGSYS))
1455 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001456#endif
1457#ifdef SIGPIPE
Christian Heimes6782b142016-09-09 00:11:45 +02001458 if (PyModule_AddIntMacro(m, SIGPIPE))
1459 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001460#endif
1461#ifdef SIGALRM
Christian Heimes6782b142016-09-09 00:11:45 +02001462 if (PyModule_AddIntMacro(m, SIGALRM))
1463 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001464#endif
1465#ifdef SIGTERM
Christian Heimes6782b142016-09-09 00:11:45 +02001466 if (PyModule_AddIntMacro(m, SIGTERM))
1467 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001468#endif
1469#ifdef SIGUSR1
Christian Heimes6782b142016-09-09 00:11:45 +02001470 if (PyModule_AddIntMacro(m, SIGUSR1))
1471 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001472#endif
1473#ifdef SIGUSR2
Christian Heimes6782b142016-09-09 00:11:45 +02001474 if (PyModule_AddIntMacro(m, SIGUSR2))
1475 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001476#endif
1477#ifdef SIGCLD
Christian Heimes6782b142016-09-09 00:11:45 +02001478 if (PyModule_AddIntMacro(m, SIGCLD))
1479 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001480#endif
1481#ifdef SIGCHLD
Christian Heimes6782b142016-09-09 00:11:45 +02001482 if (PyModule_AddIntMacro(m, SIGCHLD))
1483 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001484#endif
1485#ifdef SIGPWR
Christian Heimes6782b142016-09-09 00:11:45 +02001486 if (PyModule_AddIntMacro(m, SIGPWR))
1487 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001488#endif
1489#ifdef SIGIO
Christian Heimes6782b142016-09-09 00:11:45 +02001490 if (PyModule_AddIntMacro(m, SIGIO))
1491 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001492#endif
1493#ifdef SIGURG
Christian Heimes6782b142016-09-09 00:11:45 +02001494 if (PyModule_AddIntMacro(m, SIGURG))
1495 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001496#endif
1497#ifdef SIGWINCH
Christian Heimes6782b142016-09-09 00:11:45 +02001498 if (PyModule_AddIntMacro(m, SIGWINCH))
1499 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001500#endif
1501#ifdef SIGPOLL
Christian Heimes6782b142016-09-09 00:11:45 +02001502 if (PyModule_AddIntMacro(m, SIGPOLL))
1503 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001504#endif
1505#ifdef SIGSTOP
Christian Heimes6782b142016-09-09 00:11:45 +02001506 if (PyModule_AddIntMacro(m, SIGSTOP))
1507 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001508#endif
1509#ifdef SIGTSTP
Christian Heimes6782b142016-09-09 00:11:45 +02001510 if (PyModule_AddIntMacro(m, SIGTSTP))
1511 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001512#endif
1513#ifdef SIGCONT
Christian Heimes6782b142016-09-09 00:11:45 +02001514 if (PyModule_AddIntMacro(m, SIGCONT))
1515 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001516#endif
1517#ifdef SIGTTIN
Christian Heimes6782b142016-09-09 00:11:45 +02001518 if (PyModule_AddIntMacro(m, SIGTTIN))
1519 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001520#endif
1521#ifdef SIGTTOU
Christian Heimes6782b142016-09-09 00:11:45 +02001522 if (PyModule_AddIntMacro(m, SIGTTOU))
1523 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001524#endif
1525#ifdef SIGVTALRM
Christian Heimes6782b142016-09-09 00:11:45 +02001526 if (PyModule_AddIntMacro(m, SIGVTALRM))
1527 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001528#endif
1529#ifdef SIGPROF
Christian Heimes6782b142016-09-09 00:11:45 +02001530 if (PyModule_AddIntMacro(m, SIGPROF))
1531 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001532#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001533#ifdef SIGXCPU
Christian Heimes6782b142016-09-09 00:11:45 +02001534 if (PyModule_AddIntMacro(m, SIGXCPU))
1535 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001536#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001537#ifdef SIGXFSZ
Christian Heimes6782b142016-09-09 00:11:45 +02001538 if (PyModule_AddIntMacro(m, SIGXFSZ))
1539 goto finally;
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001540#endif
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001541#ifdef SIGRTMIN
Christian Heimes6782b142016-09-09 00:11:45 +02001542 if (PyModule_AddIntMacro(m, SIGRTMIN))
1543 goto finally;
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001544#endif
1545#ifdef SIGRTMAX
Christian Heimes6782b142016-09-09 00:11:45 +02001546 if (PyModule_AddIntMacro(m, SIGRTMAX))
1547 goto finally;
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001548#endif
Martin v. Löwis175af252002-01-12 11:43:25 +00001549#ifdef SIGINFO
Christian Heimes6782b142016-09-09 00:11:45 +02001550 if (PyModule_AddIntMacro(m, SIGINFO))
1551 goto finally;
Martin v. Löwis175af252002-01-12 11:43:25 +00001552#endif
Martin v. Löwis823725e2008-03-24 13:39:54 +00001553
1554#ifdef ITIMER_REAL
Christian Heimes6782b142016-09-09 00:11:45 +02001555 if (PyModule_AddIntMacro(m, ITIMER_REAL))
1556 goto finally;
Martin v. Löwis823725e2008-03-24 13:39:54 +00001557#endif
1558#ifdef ITIMER_VIRTUAL
Christian Heimes6782b142016-09-09 00:11:45 +02001559 if (PyModule_AddIntMacro(m, ITIMER_VIRTUAL))
1560 goto finally;
Martin v. Löwis823725e2008-03-24 13:39:54 +00001561#endif
1562#ifdef ITIMER_PROF
Christian Heimes6782b142016-09-09 00:11:45 +02001563 if (PyModule_AddIntMacro(m, ITIMER_PROF))
1564 goto finally;
Martin v. Löwis823725e2008-03-24 13:39:54 +00001565#endif
1566
1567#if defined (HAVE_SETITIMER) || defined (HAVE_GETITIMER)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001568 ItimerError = PyErr_NewException("signal.ItimerError",
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +03001569 PyExc_OSError, NULL);
Neal Norwitz2f99b242008-08-24 05:48:10 +00001570 if (ItimerError != NULL)
Martin Panter6d57fe12016-09-17 03:26:16 +00001571 PyDict_SetItemString(d, "ItimerError", ItimerError);
Martin v. Löwis823725e2008-03-24 13:39:54 +00001572#endif
1573
Brian Curtineb24d742010-04-12 17:16:38 +00001574#ifdef CTRL_C_EVENT
Christian Heimes6782b142016-09-09 00:11:45 +02001575 if (PyModule_AddIntMacro(m, CTRL_C_EVENT))
1576 goto finally;
Brian Curtineb24d742010-04-12 17:16:38 +00001577#endif
1578
1579#ifdef CTRL_BREAK_EVENT
Christian Heimes6782b142016-09-09 00:11:45 +02001580 if (PyModule_AddIntMacro(m, CTRL_BREAK_EVENT))
1581 goto finally;
Brian Curtineb24d742010-04-12 17:16:38 +00001582#endif
1583
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001584#ifdef MS_WINDOWS
1585 /* Create manual-reset event, initially unset */
1586 sigint_event = CreateEvent(NULL, TRUE, FALSE, FALSE);
1587#endif
1588
Martin v. Löwis1a214512008-06-11 05:26:20 +00001589 if (PyErr_Occurred()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001590 Py_DECREF(m);
1591 m = NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001592 }
Barry Warsaw92971171997-01-03 00:14:25 +00001593
Barry Warsaw92971171997-01-03 00:14:25 +00001594 finally:
Martin v. Löwis1a214512008-06-11 05:26:20 +00001595 return m;
Guido van Rossum08c16611997-08-02 03:01:42 +00001596}
1597
1598static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001599finisignal(void)
Guido van Rossum08c16611997-08-02 03:01:42 +00001600{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001601 int i;
1602 PyObject *func;
Guido van Rossum08c16611997-08-02 03:01:42 +00001603
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001604 for (i = 1; i < NSIG; i++) {
1605 func = Handlers[i].func;
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001606 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001607 Handlers[i].func = NULL;
pkerlinge905c842018-06-01 09:47:18 +00001608 if (func != NULL && func != Py_None &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001609 func != DefaultHandler && func != IgnoreHandler)
1610 PyOS_setsig(i, SIG_DFL);
1611 Py_XDECREF(func);
1612 }
Guido van Rossum08c16611997-08-02 03:01:42 +00001613
Serhiy Storchaka505ff752014-02-09 13:33:53 +02001614 Py_CLEAR(IntHandler);
1615 Py_CLEAR(DefaultHandler);
1616 Py_CLEAR(IgnoreHandler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001617}
1618
Barry Warsaw92971171997-01-03 00:14:25 +00001619
Barry Warsaw92971171997-01-03 00:14:25 +00001620/* Declared in pyerrors.h */
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001621int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001622PyErr_CheckSignals(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001623{
Eric Snow64d6cc82019-02-23 15:40:43 -07001624 if (!is_main()) {
1625 return 0;
1626 }
1627
1628 return _PyErr_CheckSignals();
1629}
1630
1631
1632/* Declared in cpython/pyerrors.h */
1633int
1634_PyErr_CheckSignals(void)
1635{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001636 int i;
1637 PyObject *f;
Barry Warsaw92971171997-01-03 00:14:25 +00001638
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001639 if (!_Py_atomic_load(&is_tripped))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001640 return 0;
Christian Heimesb76922a2007-12-11 01:06:40 +00001641
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001642 /*
1643 * The is_tripped variable is meant to speed up the calls to
1644 * PyErr_CheckSignals (both directly or via pending calls) when no
1645 * signal has arrived. This variable is set to 1 when a signal arrives
1646 * and it is set to 0 here, when we know some signals arrived. This way
1647 * we can run the registered handlers with no signals blocked.
1648 *
1649 * NOTE: with this approach we can have a situation where is_tripped is
1650 * 1 but we have no more signals to handle (Handlers[i].tripped
1651 * is 0 for every signal i). This won't do us any harm (except
1652 * we're gonna spent some cycles for nothing). This happens when
1653 * we receive a signal i after we zero is_tripped and before we
1654 * check Handlers[i].tripped.
1655 */
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001656 _Py_atomic_store(&is_tripped, 0);
Christian Heimesb76922a2007-12-11 01:06:40 +00001657
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001658 if (!(f = (PyObject *)PyEval_GetFrame()))
1659 f = Py_None;
Christian Heimesb76922a2007-12-11 01:06:40 +00001660
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001661 for (i = 1; i < NSIG; i++) {
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001662 if (_Py_atomic_load_relaxed(&Handlers[i].tripped)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001663 PyObject *result = NULL;
1664 PyObject *arglist = Py_BuildValue("(iO)", i, f);
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001665 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Barry Warsaw92971171997-01-03 00:14:25 +00001666
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001667 if (arglist) {
1668 result = PyEval_CallObject(Handlers[i].func,
1669 arglist);
1670 Py_DECREF(arglist);
1671 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +02001672 if (!result) {
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001673 _Py_atomic_store(&is_tripped, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001674 return -1;
Antoine Pitrouc08177a2017-06-28 23:29:29 +02001675 }
Barry Warsaw92971171997-01-03 00:14:25 +00001676
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001677 Py_DECREF(result);
1678 }
1679 }
Christian Heimesb76922a2007-12-11 01:06:40 +00001680
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001681 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001682}
1683
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +00001684
Barry Warsaw92971171997-01-03 00:14:25 +00001685/* Replacements for intrcheck.c functionality
1686 * Declared in pyerrors.h
1687 */
1688void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001689PyErr_SetInterrupt(void)
Barry Warsaw92971171997-01-03 00:14:25 +00001690{
Victor Stinner6c9b35b2011-04-18 16:25:56 +02001691 trip_signal(SIGINT);
Barry Warsaw92971171997-01-03 00:14:25 +00001692}
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001693
1694void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001695PyOS_InitInterrupts(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001696{
Giampaolo Rodola'e09fb712014-04-04 15:34:17 +02001697 PyObject *m = PyImport_ImportModule("_signal");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001698 if (m) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001699 Py_DECREF(m);
1700 }
Guido van Rossum08c16611997-08-02 03:01:42 +00001701}
1702
1703void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001704PyOS_FiniInterrupts(void)
Guido van Rossum08c16611997-08-02 03:01:42 +00001705{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001706 finisignal();
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001707}
1708
1709int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001710PyOS_InterruptOccurred(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001711{
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001712 if (_Py_atomic_load_relaxed(&Handlers[SIGINT].tripped)) {
Eric Snow64d6cc82019-02-23 15:40:43 -07001713 if (!is_main()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001714 return 0;
Eric Snow64d6cc82019-02-23 15:40:43 -07001715 }
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001716 _Py_atomic_store_relaxed(&Handlers[SIGINT].tripped, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001717 return 1;
1718 }
1719 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001720}
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001721
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001722static void
1723_clear_pending_signals(void)
1724{
1725 int i;
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001726 if (!_Py_atomic_load(&is_tripped))
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001727 return;
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001728 _Py_atomic_store(&is_tripped, 0);
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001729 for (i = 1; i < NSIG; ++i) {
Antoine Pitrou2c8a5e42017-07-17 12:25:19 +02001730 _Py_atomic_store_relaxed(&Handlers[i].tripped, 0);
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001731 }
1732}
1733
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001734void
Antoine Pitrou346cbd32017-05-27 17:50:54 +02001735_PySignal_AfterFork(void)
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001736{
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001737 /* Clear the signal flags after forking so that they aren't handled
1738 * in both processes if they came in just before the fork() but before
1739 * the interpreter had an opportunity to call the handlers. issue9535. */
1740 _clear_pending_signals();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001741 main_thread = PyThread_get_thread_ident();
1742 main_pid = getpid();
Eric Snow64d6cc82019-02-23 15:40:43 -07001743 main_interp = _PyInterpreterState_Get();
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001744}
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001745
1746int
1747_PyOS_IsMainThread(void)
1748{
Eric Snow64d6cc82019-02-23 15:40:43 -07001749 return is_main();
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001750}
1751
1752#ifdef MS_WINDOWS
1753void *_PyOS_SigintEvent(void)
1754{
1755 /* Returns a manual-reset event which gets tripped whenever
1756 SIGINT is received.
1757
1758 Python.h does not include windows.h so we do cannot use HANDLE
1759 as the return type of this function. We use void* instead. */
1760 return sigint_event;
1761}
1762#endif