blob: 8628f7a8005ff7d305de58014fb914fce714af51 [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"
Guido van Rossum398d9fe1994-05-11 08:59:13 +00007#include "intrcheck.h"
8
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00009#ifdef MS_WINDOWS
Brian Curtine5aa8862010-04-02 23:26:06 +000010#include <Windows.h>
Andrew M. Kuchling4b81bc72010-02-22 23:12:00 +000011#ifdef HAVE_PROCESS_H
Guido van Rossum644a12b1997-04-09 19:24:53 +000012#include <process.h>
13#endif
Andrew M. Kuchling4b81bc72010-02-22 23:12:00 +000014#endif
Guido van Rossum644a12b1997-04-09 19:24:53 +000015
Andrew M. Kuchling4b81bc72010-02-22 23:12:00 +000016#ifdef HAVE_SIGNAL_H
Guido van Rossum398d9fe1994-05-11 08:59:13 +000017#include <signal.h>
Andrew M. Kuchling4b81bc72010-02-22 23:12:00 +000018#endif
19#ifdef HAVE_SYS_STAT_H
Guido van Rossum02de8972007-12-19 19:41:06 +000020#include <sys/stat.h>
Andrew M. Kuchling4b81bc72010-02-22 23:12:00 +000021#endif
Martin v. Löwisb74d0842008-03-24 13:54:23 +000022#ifdef HAVE_SYS_TIME_H
Martin v. Löwisaef18b12008-03-24 13:31:16 +000023#include <sys/time.h>
Martin v. Löwisb74d0842008-03-24 13:54:23 +000024#endif
Guido van Rossum02de8972007-12-19 19:41:06 +000025
Guido van Rossumbb4ba121994-06-23 11:25:45 +000026#ifndef SIG_ERR
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +000027#define SIG_ERR ((PyOS_sighandler_t)(-1))
Guido van Rossumbb4ba121994-06-23 11:25:45 +000028#endif
29
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000030#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000031#define NSIG 12
32#include <process.h>
33#endif
34
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000035#ifndef NSIG
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000036# if defined(_NSIG)
Antoine Pitrouc83ea132010-05-09 14:46:46 +000037# define NSIG _NSIG /* For BSD/SysV */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000038# elif defined(_SIGMAX)
Antoine Pitrouc83ea132010-05-09 14:46:46 +000039# define NSIG (_SIGMAX + 1) /* For QNX */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000040# elif defined(SIGMAX)
Antoine Pitrouc83ea132010-05-09 14:46:46 +000041# define NSIG (SIGMAX + 1) /* For djgpp */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000042# else
Antoine Pitrouc83ea132010-05-09 14:46:46 +000043# define NSIG 64 /* Use a reasonable default value */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000044# endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000045#endif
46
47
Guido van Rossumbb4ba121994-06-23 11:25:45 +000048/*
49 NOTES ON THE INTERACTION BETWEEN SIGNALS AND THREADS
50
51 When threads are supported, we want the following semantics:
52
53 - only the main thread can set a signal handler
54 - any thread can get a signal handler
55 - signals are only delivered to the main thread
56
57 I.e. we don't support "synchronous signals" like SIGFPE (catching
58 this doesn't make much sense in Python anyway) nor do we support
59 signals as a means of inter-thread communication, since not all
60 thread implementations support that (at least our thread library
61 doesn't).
62
63 We still have the problem that in some implementations signals
64 generated by the keyboard (e.g. SIGINT) are delivered to all
65 threads (e.g. SGI), while in others (e.g. Solaris) such signals are
66 delivered to one random thread (an intermediate possibility would
Guido van Rossuma3c04b01995-01-12 11:29:01 +000067 be to deliver it to the main thread -- POSIX?). For now, we have
Guido van Rossumbb4ba121994-06-23 11:25:45 +000068 a working implementation that works in all three cases -- the
69 handler ignores signals if getpid() isn't the same as in the main
70 thread. XXX This is a hack.
71
Guido van Rossum9e8181b2000-09-19 00:46:46 +000072 GNU pth is a user-space threading library, and as such, all threads
73 run within the same process. In this case, if the currently running
74 thread is not the main_thread, send the signal to the main_thread.
Guido van Rossumbb4ba121994-06-23 11:25:45 +000075*/
76
77#ifdef WITH_THREAD
Guido van Rossum295b8e51997-06-06 21:16:41 +000078#include <sys/types.h> /* For pid_t */
Guido van Rossum49b56061998-10-01 20:42:43 +000079#include "pythread.h"
Guido van Rossumbb4ba121994-06-23 11:25:45 +000080static long main_thread;
81static pid_t main_pid;
82#endif
83
Barry Warsaw92971171997-01-03 00:14:25 +000084static struct {
Antoine Pitrouc83ea132010-05-09 14:46:46 +000085 int tripped;
86 PyObject *func;
Barry Warsaw92971171997-01-03 00:14:25 +000087} Handlers[NSIG];
Guido van Rossum398d9fe1994-05-11 08:59:13 +000088
Guido van Rossum02de8972007-12-19 19:41:06 +000089static sig_atomic_t wakeup_fd = -1;
90
Guido van Rossum137c49c2007-12-10 23:00:12 +000091/* Speed up sigcheck() when none tripped */
92static volatile sig_atomic_t is_tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +000093
Barry Warsaw92971171997-01-03 00:14:25 +000094static PyObject *DefaultHandler;
95static PyObject *IgnoreHandler;
96static PyObject *IntHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +000097
Martin v. Löwisaef18b12008-03-24 13:31:16 +000098#ifdef HAVE_GETITIMER
99static PyObject *ItimerError;
100
101/* auxiliary functions for setitimer/getitimer */
102static void
103timeval_from_double(double d, struct timeval *tv)
104{
105 tv->tv_sec = floor(d);
106 tv->tv_usec = fmod(d, 1.0) * 1000000.0;
Antoine Pitroua45a99b2017-06-30 10:54:54 +0200107 /* Don't disable the timer if the computation above rounds down to zero. */
108 if (d > 0.0 && tv->tv_sec == 0 && tv->tv_usec == 0) {
109 tv->tv_usec = 1;
110 }
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000111}
112
Christian Heimes32a66a02008-10-02 19:47:50 +0000113Py_LOCAL_INLINE(double)
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000114double_from_timeval(struct timeval *tv)
115{
116 return tv->tv_sec + (double)(tv->tv_usec / 1000000.0);
117}
118
119static PyObject *
120itimer_retval(struct itimerval *iv)
121{
122 PyObject *r, *v;
123
124 r = PyTuple_New(2);
125 if (r == NULL)
Martin Panterca56dd42016-09-17 07:54:55 +0000126 return NULL;
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000127
128 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_value)))) {
Martin Panterca56dd42016-09-17 07:54:55 +0000129 Py_DECREF(r);
130 return NULL;
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000131 }
132
133 PyTuple_SET_ITEM(r, 0, v);
134
135 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_interval)))) {
Martin Panterca56dd42016-09-17 07:54:55 +0000136 Py_DECREF(r);
137 return NULL;
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000138 }
139
140 PyTuple_SET_ITEM(r, 1, v);
141
142 return r;
143}
144#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000145
Guido van Rossume4485b01994-09-07 14:32:49 +0000146static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000147signal_default_int_handler(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000148{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000149 PyErr_SetNone(PyExc_KeyboardInterrupt);
150 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000151}
152
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000153PyDoc_STRVAR(default_int_handler_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000154"default_int_handler(...)\n\
155\n\
Michael W. Hudson24ec2112004-06-17 15:55:53 +0000156The default handler for SIGINT installed by Python.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000157It raises KeyboardInterrupt.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000158
Thomas Wouters0796b002000-07-22 23:49:30 +0000159
160static int
161checksignals_witharg(void * unused)
162{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000163 return PyErr_CheckSignals();
Thomas Wouters0796b002000-07-22 23:49:30 +0000164}
165
Tim Peters4f1b2082000-07-23 21:18:09 +0000166static void
Victor Stinner33feeab2011-04-18 16:33:28 +0200167trip_signal(int sig_num)
168{
169 Handlers[sig_num].tripped = 1;
170 if (is_tripped)
171 return;
172 /* Set is_tripped after setting .tripped, as it gets
173 cleared in PyErr_CheckSignals() before .tripped. */
174 is_tripped = 1;
175 Py_AddPendingCall(checksignals_witharg, NULL);
176 if (wakeup_fd != -1)
177 write(wakeup_fd, "\0", 1);
178}
179
180static void
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000181signal_handler(int sig_num)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000182{
Antoine Pitrou6b1167c2010-11-05 19:55:02 +0000183 int save_errno = errno;
184
185#if defined(WITH_THREAD) && defined(WITH_PTH)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000186 if (PyThread_get_thread_ident() != main_thread) {
187 pth_raise(*(pth_t *) main_thread, sig_num);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000188 }
Antoine Pitrou6b1167c2010-11-05 19:55:02 +0000189 else
Guido van Rossum9e8181b2000-09-19 00:46:46 +0000190#endif
Antoine Pitrou6b1167c2010-11-05 19:55:02 +0000191 {
192#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000193 /* See NOTES section above */
Antoine Pitrou6b1167c2010-11-05 19:55:02 +0000194 if (getpid() == main_pid)
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000195#endif
Antoine Pitrou6b1167c2010-11-05 19:55:02 +0000196 {
Victor Stinner33feeab2011-04-18 16:33:28 +0200197 trip_signal(sig_num);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000198 }
Antoine Pitrou6b1167c2010-11-05 19:55:02 +0000199
Jean-Paul Calderonee54ddf12010-05-08 20:06:02 +0000200#ifndef HAVE_SIGACTION
Antoine Pitrou6b1167c2010-11-05 19:55:02 +0000201#ifdef SIGCHLD
202 /* To avoid infinite recursion, this signal remains
203 reset until explicit re-instated.
204 Don't clear the 'func' field as it is our pointer
205 to the Python handler... */
206 if (sig_num != SIGCHLD)
207#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000208 /* If the handler was not set up with sigaction, reinstall it. See
209 * Python/pythonrun.c for the implementation of PyOS_setsig which
210 * makes this true. See also issue8354. */
211 PyOS_setsig(sig_num, signal_handler);
Jean-Paul Calderonee54ddf12010-05-08 20:06:02 +0000212#endif
Antoine Pitrou6b1167c2010-11-05 19:55:02 +0000213 }
214
215 /* Issue #10311: asynchronously executing signal handlers should not
216 mutate errno under the feet of unsuspecting C code. */
217 errno = save_errno;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000218}
Guido van Rossume4485b01994-09-07 14:32:49 +0000219
Guido van Rossum06d511d1995-03-10 15:13:48 +0000220
Guido van Rossum1171ee61997-08-22 20:42:00 +0000221#ifdef HAVE_ALARM
Guido van Rossume4485b01994-09-07 14:32:49 +0000222static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000223signal_alarm(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000224{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000225 int t;
226 if (!PyArg_ParseTuple(args, "i:alarm", &t))
227 return NULL;
228 /* alarm() returns the number of seconds remaining */
229 return PyInt_FromLong((long)alarm(t));
Guido van Rossumaa0f4c71994-08-23 13:49:37 +0000230}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000231
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000232PyDoc_STRVAR(alarm_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000233"alarm(seconds)\n\
234\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000235Arrange for SIGALRM to arrive after the given number of seconds.");
Guido van Rossum06d511d1995-03-10 15:13:48 +0000236#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000237
Guido van Rossum1171ee61997-08-22 20:42:00 +0000238#ifdef HAVE_PAUSE
Guido van Rossuma597dde1995-01-10 20:56:29 +0000239static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000240signal_pause(PyObject *self)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000241{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000242 Py_BEGIN_ALLOW_THREADS
243 (void)pause();
244 Py_END_ALLOW_THREADS
245 /* make sure that any exceptions that got raised are propagated
246 * back into Python
247 */
248 if (PyErr_CheckSignals())
249 return NULL;
Barry Warsaw92971171997-01-03 00:14:25 +0000250
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000251 Py_INCREF(Py_None);
252 return Py_None;
Guido van Rossume4485b01994-09-07 14:32:49 +0000253}
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000254PyDoc_STRVAR(pause_doc,
Barry Warsaw1ee36ff1998-07-21 22:41:18 +0000255"pause()\n\
256\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000257Wait until a signal arrives.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000258
Guido van Rossum06d511d1995-03-10 15:13:48 +0000259#endif
Guido van Rossume4485b01994-09-07 14:32:49 +0000260
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000261
Guido van Rossume4485b01994-09-07 14:32:49 +0000262static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000263signal_signal(PyObject *self, PyObject *args)
Guido van Rossume4485b01994-09-07 14:32:49 +0000264{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000265 PyObject *obj;
266 int sig_num;
267 PyObject *old_handler;
268 void (*func)(int);
269 if (!PyArg_ParseTuple(args, "iO:signal", &sig_num, &obj))
270 return NULL;
Brian Curtin24af0e92010-08-06 19:41:01 +0000271#ifdef MS_WINDOWS
272 /* Validate that sig_num is one of the allowable signals */
Brian Curtin84263332010-09-06 16:17:50 +0000273 switch (sig_num) {
274 case SIGABRT: break;
Brian Curtin1390dd72010-10-01 16:44:03 +0000275#ifdef SIGBREAK
276 /* Issue #10003: SIGBREAK is not documented as permitted, but works
277 and corresponds to CTRL_BREAK_EVENT. */
278 case SIGBREAK: break;
279#endif
Brian Curtin84263332010-09-06 16:17:50 +0000280 case SIGFPE: break;
281 case SIGILL: break;
282 case SIGINT: break;
283 case SIGSEGV: break;
284 case SIGTERM: break;
285 default:
286 PyErr_SetString(PyExc_ValueError, "invalid signal value");
287 return NULL;
Brian Curtin24af0e92010-08-06 19:41:01 +0000288 }
289#endif
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000290#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000291 if (PyThread_get_thread_ident() != main_thread) {
292 PyErr_SetString(PyExc_ValueError,
293 "signal only works in main thread");
294 return NULL;
295 }
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000296#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000297 if (sig_num < 1 || sig_num >= NSIG) {
298 PyErr_SetString(PyExc_ValueError,
299 "signal number out of range");
300 return NULL;
301 }
302 if (obj == IgnoreHandler)
303 func = SIG_IGN;
304 else if (obj == DefaultHandler)
305 func = SIG_DFL;
306 else if (!PyCallable_Check(obj)) {
307 PyErr_SetString(PyExc_TypeError,
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000308"signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object");
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000309 return NULL;
310 }
311 else
312 func = signal_handler;
Antoine Pitrouc7138372017-11-03 20:36:39 +0100313 /* Check for pending signals before changing signal handler */
314 if (PyErr_CheckSignals()) {
315 return NULL;
316 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000317 if (PyOS_setsig(sig_num, func) == SIG_ERR) {
318 PyErr_SetFromErrno(PyExc_RuntimeError);
319 return NULL;
320 }
321 old_handler = Handlers[sig_num].func;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000322 Py_INCREF(obj);
323 Handlers[sig_num].func = obj;
Antoine Pitroud8931c32013-05-04 23:16:59 +0200324 if (old_handler != NULL)
325 return old_handler;
326 else
327 Py_RETURN_NONE;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000328}
329
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000330PyDoc_STRVAR(signal_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000331"signal(sig, action) -> action\n\
332\n\
333Set the action for the given signal. The action can be SIG_DFL,\n\
334SIG_IGN, or a callable Python object. The previous action is\n\
335returned. See getsignal() for possible return values.\n\
336\n\
337*** IMPORTANT NOTICE ***\n\
338A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000339the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000340
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000341
Guido van Rossume4485b01994-09-07 14:32:49 +0000342static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000343signal_getsignal(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000344{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000345 int sig_num;
346 PyObject *old_handler;
347 if (!PyArg_ParseTuple(args, "i:getsignal", &sig_num))
348 return NULL;
349 if (sig_num < 1 || sig_num >= NSIG) {
350 PyErr_SetString(PyExc_ValueError,
351 "signal number out of range");
352 return NULL;
353 }
354 old_handler = Handlers[sig_num].func;
Antoine Pitroud8931c32013-05-04 23:16:59 +0200355 if (old_handler != NULL) {
356 Py_INCREF(old_handler);
357 return old_handler;
358 }
359 else {
360 Py_RETURN_NONE;
361 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000362}
363
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000364PyDoc_STRVAR(getsignal_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000365"getsignal(sig) -> action\n\
366\n\
367Return the current action for the given signal. The return value can be:\n\
368SIG_IGN -- if the signal is being ignored\n\
369SIG_DFL -- if the default action for the signal is in effect\n\
370None -- if an unknown handler is in effect\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000371anything else -- the callable Python object used as a handler");
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000372
Facundo Batista7e251e82008-02-23 15:07:35 +0000373#ifdef HAVE_SIGINTERRUPT
374PyDoc_STRVAR(siginterrupt_doc,
375"siginterrupt(sig, flag) -> None\n\
376change system call restart behaviour: if flag is False, system calls\n\
377will be restarted when interrupted by signal sig, else system calls\n\
378will be interrupted.");
379
380static PyObject *
381signal_siginterrupt(PyObject *self, PyObject *args)
382{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000383 int sig_num;
384 int flag;
Facundo Batista7e251e82008-02-23 15:07:35 +0000385
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000386 if (!PyArg_ParseTuple(args, "ii:siginterrupt", &sig_num, &flag))
387 return NULL;
388 if (sig_num < 1 || sig_num >= NSIG) {
389 PyErr_SetString(PyExc_ValueError,
390 "signal number out of range");
391 return NULL;
392 }
393 if (siginterrupt(sig_num, flag)<0) {
394 PyErr_SetFromErrno(PyExc_RuntimeError);
395 return NULL;
396 }
Facundo Batista7e251e82008-02-23 15:07:35 +0000397
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000398 Py_INCREF(Py_None);
399 return Py_None;
Facundo Batista7e251e82008-02-23 15:07:35 +0000400}
401
402#endif
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000403
Guido van Rossum02de8972007-12-19 19:41:06 +0000404static PyObject *
405signal_set_wakeup_fd(PyObject *self, PyObject *args)
406{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000407 struct stat buf;
408 int fd, old_fd;
409 if (!PyArg_ParseTuple(args, "i:set_wakeup_fd", &fd))
410 return NULL;
Guido van Rossum02de8972007-12-19 19:41:06 +0000411#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000412 if (PyThread_get_thread_ident() != main_thread) {
413 PyErr_SetString(PyExc_ValueError,
414 "set_wakeup_fd only works in main thread");
415 return NULL;
416 }
Guido van Rossum02de8972007-12-19 19:41:06 +0000417#endif
Benjamin Peterson08e153a2013-01-18 00:10:24 -0500418 if (fd != -1 && (!_PyVerify_fd(fd) || fstat(fd, &buf) != 0)) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000419 PyErr_SetString(PyExc_ValueError, "invalid fd");
420 return NULL;
421 }
422 old_fd = wakeup_fd;
423 wakeup_fd = fd;
424 return PyLong_FromLong(old_fd);
Guido van Rossum02de8972007-12-19 19:41:06 +0000425}
426
427PyDoc_STRVAR(set_wakeup_fd_doc,
428"set_wakeup_fd(fd) -> fd\n\
429\n\
430Sets the fd to be written to (with '\\0') when a signal\n\
431comes in. A library can use this to wakeup select or poll.\n\
432The previous fd is returned.\n\
433\n\
434The fd must be non-blocking.");
435
436/* C API for the same, without all the error checking */
437int
438PySignal_SetWakeupFd(int fd)
439{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000440 int old_fd = wakeup_fd;
441 if (fd < 0)
442 fd = -1;
443 wakeup_fd = fd;
444 return old_fd;
Guido van Rossum02de8972007-12-19 19:41:06 +0000445}
446
447
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000448#ifdef HAVE_SETITIMER
449static PyObject *
450signal_setitimer(PyObject *self, PyObject *args)
451{
452 double first;
453 double interval = 0;
454 int which;
455 struct itimerval new, old;
456
457 if(!PyArg_ParseTuple(args, "id|d:setitimer", &which, &first, &interval))
Martin Panterca56dd42016-09-17 07:54:55 +0000458 return NULL;
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000459
460 timeval_from_double(first, &new.it_value);
461 timeval_from_double(interval, &new.it_interval);
462 /* Let OS check "which" value */
463 if (setitimer(which, &new, &old) != 0) {
Martin Panterca56dd42016-09-17 07:54:55 +0000464 PyErr_SetFromErrno(ItimerError);
465 return NULL;
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000466 }
467
468 return itimer_retval(&old);
469}
470
471PyDoc_STRVAR(setitimer_doc,
472"setitimer(which, seconds[, interval])\n\
473\n\
474Sets given itimer (one of ITIMER_REAL, ITIMER_VIRTUAL\n\
475or ITIMER_PROF) to fire after value seconds and after\n\
476that every interval seconds.\n\
477The itimer can be cleared by setting seconds to zero.\n\
478\n\
479Returns old values as a tuple: (delay, interval).");
480#endif
481
482
483#ifdef HAVE_GETITIMER
484static PyObject *
485signal_getitimer(PyObject *self, PyObject *args)
486{
487 int which;
488 struct itimerval old;
489
490 if (!PyArg_ParseTuple(args, "i:getitimer", &which))
Martin Panterca56dd42016-09-17 07:54:55 +0000491 return NULL;
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000492
493 if (getitimer(which, &old) != 0) {
Martin Panterca56dd42016-09-17 07:54:55 +0000494 PyErr_SetFromErrno(ItimerError);
495 return NULL;
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000496 }
497
498 return itimer_retval(&old);
499}
500
501PyDoc_STRVAR(getitimer_doc,
502"getitimer(which)\n\
503\n\
504Returns current value of given itimer.");
505#endif
506
507
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000508/* List of functions defined in the module */
Barry Warsaw92971171997-01-03 00:14:25 +0000509static PyMethodDef signal_methods[] = {
Guido van Rossum1171ee61997-08-22 20:42:00 +0000510#ifdef HAVE_ALARM
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000511 {"alarm", signal_alarm, METH_VARARGS, alarm_doc},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000512#endif
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000513#ifdef HAVE_SETITIMER
514 {"setitimer", signal_setitimer, METH_VARARGS, setitimer_doc},
515#endif
516#ifdef HAVE_GETITIMER
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000517 {"getitimer", signal_getitimer, METH_VARARGS, getitimer_doc},
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000518#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000519 {"signal", signal_signal, METH_VARARGS, signal_doc},
520 {"getsignal", signal_getsignal, METH_VARARGS, getsignal_doc},
521 {"set_wakeup_fd", signal_set_wakeup_fd, METH_VARARGS, set_wakeup_fd_doc},
Facundo Batista7e251e82008-02-23 15:07:35 +0000522#ifdef HAVE_SIGINTERRUPT
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000523 {"siginterrupt", signal_siginterrupt, METH_VARARGS, siginterrupt_doc},
Facundo Batista7e251e82008-02-23 15:07:35 +0000524#endif
Guido van Rossum1171ee61997-08-22 20:42:00 +0000525#ifdef HAVE_PAUSE
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000526 {"pause", (PyCFunction)signal_pause,
527 METH_NOARGS,pause_doc},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000528#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000529 {"default_int_handler", signal_default_int_handler,
530 METH_VARARGS, default_int_handler_doc},
531 {NULL, NULL} /* sentinel */
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000532};
533
Barry Warsaw92971171997-01-03 00:14:25 +0000534
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000535PyDoc_STRVAR(module_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000536"This module provides mechanisms to use signal handlers in Python.\n\
537\n\
538Functions:\n\
539\n\
540alarm() -- cause SIGALRM after a specified time [Unix only]\n\
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000541setitimer() -- cause a signal (described below) after a specified\n\
542 float time and the timer may restart then [Unix only]\n\
543getitimer() -- get current value of timer [Unix only]\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000544signal() -- set the action for a given signal\n\
545getsignal() -- get the signal action for a given signal\n\
546pause() -- wait until a signal arrives [Unix only]\n\
547default_int_handler() -- default SIGINT handler\n\
548\n\
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000549signal constants:\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000550SIG_DFL -- used to refer to the system default handler\n\
551SIG_IGN -- used to ignore the signal\n\
552NSIG -- number of defined signals\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000553SIGINT, SIGTERM, etc. -- signal numbers\n\
554\n\
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000555itimer constants:\n\
556ITIMER_REAL -- decrements in real time, and delivers SIGALRM upon\n\
557 expiration\n\
558ITIMER_VIRTUAL -- decrements only when the process is executing,\n\
559 and delivers SIGVTALRM upon expiration\n\
560ITIMER_PROF -- decrements both when the process is executing and\n\
561 when the system is executing on behalf of the process.\n\
562 Coupled with ITIMER_VIRTUAL, this timer is usually\n\
563 used to profile the time spent by the application\n\
564 in user and kernel space. SIGPROF is delivered upon\n\
565 expiration.\n\
566\n\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000567*** IMPORTANT NOTICE ***\n\
568A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000569the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000570
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000571PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000572initsignal(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000573{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000574 PyObject *m, *d, *x;
575 int i;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000576
577#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000578 main_thread = PyThread_get_thread_ident();
579 main_pid = getpid();
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000580#endif
581
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000582 /* Create the module and add the functions */
583 m = Py_InitModule3("signal", signal_methods, module_doc);
584 if (m == NULL)
585 return;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000586
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000587 /* Add some symbolic constants to the module */
588 d = PyModule_GetDict(m);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000589
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000590 x = DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL);
591 if (!x || PyDict_SetItemString(d, "SIG_DFL", x) < 0)
592 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000593
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000594 x = IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN);
595 if (!x || PyDict_SetItemString(d, "SIG_IGN", x) < 0)
596 goto finally;
Barry Warsaw92971171997-01-03 00:14:25 +0000597
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000598 x = PyInt_FromLong((long)NSIG);
599 if (!x || PyDict_SetItemString(d, "NSIG", x) < 0)
600 goto finally;
601 Py_DECREF(x);
Barry Warsaw92971171997-01-03 00:14:25 +0000602
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000603 x = IntHandler = PyDict_GetItemString(d, "default_int_handler");
604 if (!x)
605 goto finally;
606 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +0000607
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000608 Handlers[0].tripped = 0;
609 for (i = 1; i < NSIG; i++) {
610 void (*t)(int);
611 t = PyOS_getsig(i);
612 Handlers[i].tripped = 0;
613 if (t == SIG_DFL)
614 Handlers[i].func = DefaultHandler;
615 else if (t == SIG_IGN)
616 Handlers[i].func = IgnoreHandler;
617 else
618 Handlers[i].func = Py_None; /* None of our business */
619 Py_INCREF(Handlers[i].func);
620 }
621 if (Handlers[SIGINT].func == DefaultHandler) {
622 /* Install default int handler */
623 Py_INCREF(IntHandler);
Serhiy Storchaka763a61c2016-04-10 18:05:12 +0300624 Py_SETREF(Handlers[SIGINT].func, IntHandler);
Antoine Pitrouded666f2018-06-03 20:46:43 +0200625 PyOS_setsig(SIGINT, signal_handler);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000626 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000627
628#ifdef SIGHUP
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000629 x = PyInt_FromLong(SIGHUP);
630 PyDict_SetItemString(d, "SIGHUP", x);
631 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000632#endif
633#ifdef SIGINT
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000634 x = PyInt_FromLong(SIGINT);
635 PyDict_SetItemString(d, "SIGINT", x);
636 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000637#endif
Tim Peters1ce3cf72001-10-01 17:58:40 +0000638#ifdef SIGBREAK
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000639 x = PyInt_FromLong(SIGBREAK);
640 PyDict_SetItemString(d, "SIGBREAK", x);
641 Py_XDECREF(x);
Tim Peters1ce3cf72001-10-01 17:58:40 +0000642#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000643#ifdef SIGQUIT
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000644 x = PyInt_FromLong(SIGQUIT);
645 PyDict_SetItemString(d, "SIGQUIT", x);
646 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000647#endif
648#ifdef SIGILL
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000649 x = PyInt_FromLong(SIGILL);
650 PyDict_SetItemString(d, "SIGILL", x);
651 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000652#endif
653#ifdef SIGTRAP
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000654 x = PyInt_FromLong(SIGTRAP);
655 PyDict_SetItemString(d, "SIGTRAP", x);
656 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000657#endif
658#ifdef SIGIOT
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000659 x = PyInt_FromLong(SIGIOT);
660 PyDict_SetItemString(d, "SIGIOT", x);
661 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000662#endif
663#ifdef SIGABRT
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000664 x = PyInt_FromLong(SIGABRT);
665 PyDict_SetItemString(d, "SIGABRT", x);
666 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000667#endif
668#ifdef SIGEMT
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000669 x = PyInt_FromLong(SIGEMT);
670 PyDict_SetItemString(d, "SIGEMT", x);
671 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000672#endif
673#ifdef SIGFPE
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000674 x = PyInt_FromLong(SIGFPE);
675 PyDict_SetItemString(d, "SIGFPE", x);
676 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000677#endif
678#ifdef SIGKILL
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000679 x = PyInt_FromLong(SIGKILL);
680 PyDict_SetItemString(d, "SIGKILL", x);
681 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000682#endif
683#ifdef SIGBUS
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000684 x = PyInt_FromLong(SIGBUS);
685 PyDict_SetItemString(d, "SIGBUS", x);
686 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000687#endif
688#ifdef SIGSEGV
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000689 x = PyInt_FromLong(SIGSEGV);
690 PyDict_SetItemString(d, "SIGSEGV", x);
691 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000692#endif
693#ifdef SIGSYS
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000694 x = PyInt_FromLong(SIGSYS);
695 PyDict_SetItemString(d, "SIGSYS", x);
696 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000697#endif
698#ifdef SIGPIPE
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000699 x = PyInt_FromLong(SIGPIPE);
700 PyDict_SetItemString(d, "SIGPIPE", x);
701 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000702#endif
703#ifdef SIGALRM
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000704 x = PyInt_FromLong(SIGALRM);
705 PyDict_SetItemString(d, "SIGALRM", x);
706 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000707#endif
708#ifdef SIGTERM
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000709 x = PyInt_FromLong(SIGTERM);
710 PyDict_SetItemString(d, "SIGTERM", x);
711 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000712#endif
713#ifdef SIGUSR1
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000714 x = PyInt_FromLong(SIGUSR1);
715 PyDict_SetItemString(d, "SIGUSR1", x);
716 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000717#endif
718#ifdef SIGUSR2
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000719 x = PyInt_FromLong(SIGUSR2);
720 PyDict_SetItemString(d, "SIGUSR2", x);
721 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000722#endif
723#ifdef SIGCLD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000724 x = PyInt_FromLong(SIGCLD);
725 PyDict_SetItemString(d, "SIGCLD", x);
726 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000727#endif
728#ifdef SIGCHLD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000729 x = PyInt_FromLong(SIGCHLD);
730 PyDict_SetItemString(d, "SIGCHLD", x);
731 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000732#endif
733#ifdef SIGPWR
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000734 x = PyInt_FromLong(SIGPWR);
735 PyDict_SetItemString(d, "SIGPWR", x);
736 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000737#endif
738#ifdef SIGIO
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000739 x = PyInt_FromLong(SIGIO);
740 PyDict_SetItemString(d, "SIGIO", x);
741 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000742#endif
743#ifdef SIGURG
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000744 x = PyInt_FromLong(SIGURG);
745 PyDict_SetItemString(d, "SIGURG", x);
746 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000747#endif
748#ifdef SIGWINCH
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000749 x = PyInt_FromLong(SIGWINCH);
750 PyDict_SetItemString(d, "SIGWINCH", x);
751 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000752#endif
753#ifdef SIGPOLL
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000754 x = PyInt_FromLong(SIGPOLL);
755 PyDict_SetItemString(d, "SIGPOLL", x);
756 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000757#endif
758#ifdef SIGSTOP
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000759 x = PyInt_FromLong(SIGSTOP);
760 PyDict_SetItemString(d, "SIGSTOP", x);
761 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000762#endif
763#ifdef SIGTSTP
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000764 x = PyInt_FromLong(SIGTSTP);
765 PyDict_SetItemString(d, "SIGTSTP", x);
766 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000767#endif
768#ifdef SIGCONT
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000769 x = PyInt_FromLong(SIGCONT);
770 PyDict_SetItemString(d, "SIGCONT", x);
771 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000772#endif
773#ifdef SIGTTIN
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000774 x = PyInt_FromLong(SIGTTIN);
775 PyDict_SetItemString(d, "SIGTTIN", x);
776 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000777#endif
778#ifdef SIGTTOU
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000779 x = PyInt_FromLong(SIGTTOU);
780 PyDict_SetItemString(d, "SIGTTOU", x);
781 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000782#endif
783#ifdef SIGVTALRM
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000784 x = PyInt_FromLong(SIGVTALRM);
785 PyDict_SetItemString(d, "SIGVTALRM", x);
786 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000787#endif
788#ifdef SIGPROF
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000789 x = PyInt_FromLong(SIGPROF);
790 PyDict_SetItemString(d, "SIGPROF", x);
791 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000792#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000793#ifdef SIGXCPU
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000794 x = PyInt_FromLong(SIGXCPU);
795 PyDict_SetItemString(d, "SIGXCPU", x);
796 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000797#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000798#ifdef SIGXFSZ
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000799 x = PyInt_FromLong(SIGXFSZ);
800 PyDict_SetItemString(d, "SIGXFSZ", x);
801 Py_XDECREF(x);
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000802#endif
Anthony Baxterf37f37d2003-07-31 10:35:29 +0000803#ifdef SIGRTMIN
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000804 x = PyInt_FromLong(SIGRTMIN);
805 PyDict_SetItemString(d, "SIGRTMIN", x);
806 Py_XDECREF(x);
Anthony Baxterf37f37d2003-07-31 10:35:29 +0000807#endif
808#ifdef SIGRTMAX
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000809 x = PyInt_FromLong(SIGRTMAX);
810 PyDict_SetItemString(d, "SIGRTMAX", x);
811 Py_XDECREF(x);
Anthony Baxterf37f37d2003-07-31 10:35:29 +0000812#endif
Martin v. Löwis175af252002-01-12 11:43:25 +0000813#ifdef SIGINFO
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000814 x = PyInt_FromLong(SIGINFO);
815 PyDict_SetItemString(d, "SIGINFO", x);
816 Py_XDECREF(x);
Martin v. Löwis175af252002-01-12 11:43:25 +0000817#endif
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000818
819#ifdef ITIMER_REAL
820 x = PyLong_FromLong(ITIMER_REAL);
821 PyDict_SetItemString(d, "ITIMER_REAL", x);
822 Py_DECREF(x);
823#endif
824#ifdef ITIMER_VIRTUAL
825 x = PyLong_FromLong(ITIMER_VIRTUAL);
826 PyDict_SetItemString(d, "ITIMER_VIRTUAL", x);
827 Py_DECREF(x);
828#endif
829#ifdef ITIMER_PROF
830 x = PyLong_FromLong(ITIMER_PROF);
831 PyDict_SetItemString(d, "ITIMER_PROF", x);
832 Py_DECREF(x);
833#endif
834
835#if defined (HAVE_SETITIMER) || defined (HAVE_GETITIMER)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000836 ItimerError = PyErr_NewException("signal.ItimerError",
Martin Panterca56dd42016-09-17 07:54:55 +0000837 PyExc_IOError, NULL);
Neal Norwitz18aa3882008-08-24 05:04:52 +0000838 if (ItimerError != NULL)
Martin Panterca56dd42016-09-17 07:54:55 +0000839 PyDict_SetItemString(d, "ItimerError", ItimerError);
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000840#endif
841
Brian Curtine5aa8862010-04-02 23:26:06 +0000842#ifdef CTRL_C_EVENT
843 x = PyInt_FromLong(CTRL_C_EVENT);
844 PyDict_SetItemString(d, "CTRL_C_EVENT", x);
845 Py_DECREF(x);
846#endif
847
848#ifdef CTRL_BREAK_EVENT
849 x = PyInt_FromLong(CTRL_BREAK_EVENT);
850 PyDict_SetItemString(d, "CTRL_BREAK_EVENT", x);
851 Py_DECREF(x);
852#endif
853
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000854 if (!PyErr_Occurred())
Guido van Rossum08c16611997-08-02 03:01:42 +0000855 return;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000856
857 /* Check for errors */
858 finally:
859 return;
Guido van Rossum08c16611997-08-02 03:01:42 +0000860}
861
862static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000863finisignal(void)
Guido van Rossum08c16611997-08-02 03:01:42 +0000864{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000865 int i;
866 PyObject *func;
Guido van Rossum08c16611997-08-02 03:01:42 +0000867
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000868 for (i = 1; i < NSIG; i++) {
869 func = Handlers[i].func;
870 Handlers[i].tripped = 0;
871 Handlers[i].func = NULL;
Antoine Pitrouded666f2018-06-03 20:46:43 +0200872 if (func != NULL && func != Py_None &&
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000873 func != DefaultHandler && func != IgnoreHandler)
874 PyOS_setsig(i, SIG_DFL);
875 Py_XDECREF(func);
876 }
Guido van Rossum08c16611997-08-02 03:01:42 +0000877
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000878 Py_XDECREF(IntHandler);
879 IntHandler = NULL;
880 Py_XDECREF(DefaultHandler);
881 DefaultHandler = NULL;
882 Py_XDECREF(IgnoreHandler);
883 IgnoreHandler = NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000884}
885
Barry Warsaw92971171997-01-03 00:14:25 +0000886
Barry Warsaw92971171997-01-03 00:14:25 +0000887/* Declared in pyerrors.h */
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000888int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000889PyErr_CheckSignals(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000890{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000891 int i;
892 PyObject *f;
Barry Warsaw92971171997-01-03 00:14:25 +0000893
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000894 if (!is_tripped)
895 return 0;
Guido van Rossum137c49c2007-12-10 23:00:12 +0000896
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000897#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000898 if (PyThread_get_thread_ident() != main_thread)
899 return 0;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000900#endif
Guido van Rossum137c49c2007-12-10 23:00:12 +0000901
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000902 /*
903 * The is_tripped variable is meant to speed up the calls to
904 * PyErr_CheckSignals (both directly or via pending calls) when no
905 * signal has arrived. This variable is set to 1 when a signal arrives
906 * and it is set to 0 here, when we know some signals arrived. This way
907 * we can run the registered handlers with no signals blocked.
908 *
909 * NOTE: with this approach we can have a situation where is_tripped is
910 * 1 but we have no more signals to handle (Handlers[i].tripped
911 * is 0 for every signal i). This won't do us any harm (except
912 * we're gonna spent some cycles for nothing). This happens when
913 * we receive a signal i after we zero is_tripped and before we
914 * check Handlers[i].tripped.
915 */
916 is_tripped = 0;
Guido van Rossum137c49c2007-12-10 23:00:12 +0000917
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000918 if (!(f = (PyObject *)PyEval_GetFrame()))
919 f = Py_None;
Guido van Rossum137c49c2007-12-10 23:00:12 +0000920
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000921 for (i = 1; i < NSIG; i++) {
922 if (Handlers[i].tripped) {
923 PyObject *result = NULL;
924 PyObject *arglist = Py_BuildValue("(iO)", i, f);
925 Handlers[i].tripped = 0;
Barry Warsaw92971171997-01-03 00:14:25 +0000926
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000927 if (arglist) {
928 result = PyEval_CallObject(Handlers[i].func,
929 arglist);
930 Py_DECREF(arglist);
931 }
932 if (!result)
933 return -1;
Barry Warsaw92971171997-01-03 00:14:25 +0000934
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000935 Py_DECREF(result);
936 }
937 }
Guido van Rossum137c49c2007-12-10 23:00:12 +0000938
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000939 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000940}
941
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000942
Barry Warsaw92971171997-01-03 00:14:25 +0000943/* Replacements for intrcheck.c functionality
944 * Declared in pyerrors.h
945 */
946void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000947PyErr_SetInterrupt(void)
Barry Warsaw92971171997-01-03 00:14:25 +0000948{
Victor Stinner33feeab2011-04-18 16:33:28 +0200949 trip_signal(SIGINT);
Barry Warsaw92971171997-01-03 00:14:25 +0000950}
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000951
952void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000953PyOS_InitInterrupts(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000954{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000955 initsignal();
956 _PyImport_FixupExtension("signal", "signal");
Guido van Rossum08c16611997-08-02 03:01:42 +0000957}
958
959void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000960PyOS_FiniInterrupts(void)
Guido van Rossum08c16611997-08-02 03:01:42 +0000961{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000962 finisignal();
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000963}
964
965int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000966PyOS_InterruptOccurred(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000967{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000968 if (Handlers[SIGINT].tripped) {
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000969#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000970 if (PyThread_get_thread_ident() != main_thread)
971 return 0;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000972#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000973 Handlers[SIGINT].tripped = 0;
974 return 1;
975 }
976 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000977}
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000978
Gregory P. Smithc1ce93a2012-11-10 20:38:17 -0800979static void
980_clear_pending_signals(void)
981{
982 int i;
983 if (!is_tripped)
984 return;
985 is_tripped = 0;
986 for (i = 1; i < NSIG; ++i) {
987 Handlers[i].tripped = 0;
988 }
989}
990
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000991void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000992PyOS_AfterFork(void)
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000993{
Gregory P. Smithc1ce93a2012-11-10 20:38:17 -0800994 /* Clear the signal flags after forking so that they aren't handled
995 * in both processes if they came in just before the fork() but before
996 * the interpreter had an opportunity to call the handlers. issue9535. */
997 _clear_pending_signals();
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000998#ifdef WITH_THREAD
Charles-François Natalie0e88b02012-02-02 19:57:19 +0100999 /* PyThread_ReInitTLS() must be called early, to make sure that the TLS API
1000 * can be called safely. */
1001 PyThread_ReInitTLS();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001002 PyEval_ReInitThreads();
1003 main_thread = PyThread_get_thread_ident();
1004 main_pid = getpid();
1005 _PyImport_ReInitLock();
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001006#endif
1007}