blob: ef70d10bb1840e98bc66828dade44b3b78dc6b7a [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öwisf58de1b2001-03-06 12:13:56 +000098/* On Solaris 8, gcc will produce a warning that the function
99 declaration is not a prototype. This is caused by the definition of
100 SIG_DFL as (void (*)())0; the correct declaration would have been
101 (void (*)(int))0. */
102
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000103static PyOS_sighandler_t old_siginthandler = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +0000104
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000105#ifdef HAVE_GETITIMER
106static PyObject *ItimerError;
107
108/* auxiliary functions for setitimer/getitimer */
109static void
110timeval_from_double(double d, struct timeval *tv)
111{
112 tv->tv_sec = floor(d);
113 tv->tv_usec = fmod(d, 1.0) * 1000000.0;
Antoine Pitroua45a99b2017-06-30 10:54:54 +0200114 /* Don't disable the timer if the computation above rounds down to zero. */
115 if (d > 0.0 && tv->tv_sec == 0 && tv->tv_usec == 0) {
116 tv->tv_usec = 1;
117 }
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000118}
119
Christian Heimes32a66a02008-10-02 19:47:50 +0000120Py_LOCAL_INLINE(double)
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000121double_from_timeval(struct timeval *tv)
122{
123 return tv->tv_sec + (double)(tv->tv_usec / 1000000.0);
124}
125
126static PyObject *
127itimer_retval(struct itimerval *iv)
128{
129 PyObject *r, *v;
130
131 r = PyTuple_New(2);
132 if (r == NULL)
Martin Panterca56dd42016-09-17 07:54:55 +0000133 return NULL;
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000134
135 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_value)))) {
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, 0, v);
141
142 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_interval)))) {
Martin Panterca56dd42016-09-17 07:54:55 +0000143 Py_DECREF(r);
144 return NULL;
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000145 }
146
147 PyTuple_SET_ITEM(r, 1, v);
148
149 return r;
150}
151#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000152
Guido van Rossume4485b01994-09-07 14:32:49 +0000153static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000154signal_default_int_handler(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000155{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000156 PyErr_SetNone(PyExc_KeyboardInterrupt);
157 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000158}
159
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000160PyDoc_STRVAR(default_int_handler_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000161"default_int_handler(...)\n\
162\n\
Michael W. Hudson24ec2112004-06-17 15:55:53 +0000163The default handler for SIGINT installed by Python.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000164It raises KeyboardInterrupt.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000165
Thomas Wouters0796b002000-07-22 23:49:30 +0000166
167static int
168checksignals_witharg(void * unused)
169{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000170 return PyErr_CheckSignals();
Thomas Wouters0796b002000-07-22 23:49:30 +0000171}
172
Tim Peters4f1b2082000-07-23 21:18:09 +0000173static void
Victor Stinner33feeab2011-04-18 16:33:28 +0200174trip_signal(int sig_num)
175{
176 Handlers[sig_num].tripped = 1;
177 if (is_tripped)
178 return;
179 /* Set is_tripped after setting .tripped, as it gets
180 cleared in PyErr_CheckSignals() before .tripped. */
181 is_tripped = 1;
182 Py_AddPendingCall(checksignals_witharg, NULL);
183 if (wakeup_fd != -1)
184 write(wakeup_fd, "\0", 1);
185}
186
187static void
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000188signal_handler(int sig_num)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000189{
Antoine Pitrou6b1167c2010-11-05 19:55:02 +0000190 int save_errno = errno;
191
192#if defined(WITH_THREAD) && defined(WITH_PTH)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000193 if (PyThread_get_thread_ident() != main_thread) {
194 pth_raise(*(pth_t *) main_thread, sig_num);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000195 }
Antoine Pitrou6b1167c2010-11-05 19:55:02 +0000196 else
Guido van Rossum9e8181b2000-09-19 00:46:46 +0000197#endif
Antoine Pitrou6b1167c2010-11-05 19:55:02 +0000198 {
199#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000200 /* See NOTES section above */
Antoine Pitrou6b1167c2010-11-05 19:55:02 +0000201 if (getpid() == main_pid)
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000202#endif
Antoine Pitrou6b1167c2010-11-05 19:55:02 +0000203 {
Victor Stinner33feeab2011-04-18 16:33:28 +0200204 trip_signal(sig_num);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000205 }
Antoine Pitrou6b1167c2010-11-05 19:55:02 +0000206
Jean-Paul Calderonee54ddf12010-05-08 20:06:02 +0000207#ifndef HAVE_SIGACTION
Antoine Pitrou6b1167c2010-11-05 19:55:02 +0000208#ifdef SIGCHLD
209 /* To avoid infinite recursion, this signal remains
210 reset until explicit re-instated.
211 Don't clear the 'func' field as it is our pointer
212 to the Python handler... */
213 if (sig_num != SIGCHLD)
214#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000215 /* If the handler was not set up with sigaction, reinstall it. See
216 * Python/pythonrun.c for the implementation of PyOS_setsig which
217 * makes this true. See also issue8354. */
218 PyOS_setsig(sig_num, signal_handler);
Jean-Paul Calderonee54ddf12010-05-08 20:06:02 +0000219#endif
Antoine Pitrou6b1167c2010-11-05 19:55:02 +0000220 }
221
222 /* Issue #10311: asynchronously executing signal handlers should not
223 mutate errno under the feet of unsuspecting C code. */
224 errno = save_errno;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000225}
Guido van Rossume4485b01994-09-07 14:32:49 +0000226
Guido van Rossum06d511d1995-03-10 15:13:48 +0000227
Guido van Rossum1171ee61997-08-22 20:42:00 +0000228#ifdef HAVE_ALARM
Guido van Rossume4485b01994-09-07 14:32:49 +0000229static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000230signal_alarm(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000231{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000232 int t;
233 if (!PyArg_ParseTuple(args, "i:alarm", &t))
234 return NULL;
235 /* alarm() returns the number of seconds remaining */
236 return PyInt_FromLong((long)alarm(t));
Guido van Rossumaa0f4c71994-08-23 13:49:37 +0000237}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000238
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000239PyDoc_STRVAR(alarm_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000240"alarm(seconds)\n\
241\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000242Arrange for SIGALRM to arrive after the given number of seconds.");
Guido van Rossum06d511d1995-03-10 15:13:48 +0000243#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000244
Guido van Rossum1171ee61997-08-22 20:42:00 +0000245#ifdef HAVE_PAUSE
Guido van Rossuma597dde1995-01-10 20:56:29 +0000246static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000247signal_pause(PyObject *self)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000248{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000249 Py_BEGIN_ALLOW_THREADS
250 (void)pause();
251 Py_END_ALLOW_THREADS
252 /* make sure that any exceptions that got raised are propagated
253 * back into Python
254 */
255 if (PyErr_CheckSignals())
256 return NULL;
Barry Warsaw92971171997-01-03 00:14:25 +0000257
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000258 Py_INCREF(Py_None);
259 return Py_None;
Guido van Rossume4485b01994-09-07 14:32:49 +0000260}
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000261PyDoc_STRVAR(pause_doc,
Barry Warsaw1ee36ff1998-07-21 22:41:18 +0000262"pause()\n\
263\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000264Wait until a signal arrives.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000265
Guido van Rossum06d511d1995-03-10 15:13:48 +0000266#endif
Guido van Rossume4485b01994-09-07 14:32:49 +0000267
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000268
Guido van Rossume4485b01994-09-07 14:32:49 +0000269static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000270signal_signal(PyObject *self, PyObject *args)
Guido van Rossume4485b01994-09-07 14:32:49 +0000271{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000272 PyObject *obj;
273 int sig_num;
274 PyObject *old_handler;
275 void (*func)(int);
276 if (!PyArg_ParseTuple(args, "iO:signal", &sig_num, &obj))
277 return NULL;
Brian Curtin24af0e92010-08-06 19:41:01 +0000278#ifdef MS_WINDOWS
279 /* Validate that sig_num is one of the allowable signals */
Brian Curtin84263332010-09-06 16:17:50 +0000280 switch (sig_num) {
281 case SIGABRT: break;
Brian Curtin1390dd72010-10-01 16:44:03 +0000282#ifdef SIGBREAK
283 /* Issue #10003: SIGBREAK is not documented as permitted, but works
284 and corresponds to CTRL_BREAK_EVENT. */
285 case SIGBREAK: break;
286#endif
Brian Curtin84263332010-09-06 16:17:50 +0000287 case SIGFPE: break;
288 case SIGILL: break;
289 case SIGINT: break;
290 case SIGSEGV: break;
291 case SIGTERM: break;
292 default:
293 PyErr_SetString(PyExc_ValueError, "invalid signal value");
294 return NULL;
Brian Curtin24af0e92010-08-06 19:41:01 +0000295 }
296#endif
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000297#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000298 if (PyThread_get_thread_ident() != main_thread) {
299 PyErr_SetString(PyExc_ValueError,
300 "signal only works in main thread");
301 return NULL;
302 }
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000303#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000304 if (sig_num < 1 || sig_num >= NSIG) {
305 PyErr_SetString(PyExc_ValueError,
306 "signal number out of range");
307 return NULL;
308 }
309 if (obj == IgnoreHandler)
310 func = SIG_IGN;
311 else if (obj == DefaultHandler)
312 func = SIG_DFL;
313 else if (!PyCallable_Check(obj)) {
314 PyErr_SetString(PyExc_TypeError,
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000315"signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object");
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000316 return NULL;
317 }
318 else
319 func = signal_handler;
Antoine Pitrouc7138372017-11-03 20:36:39 +0100320 /* Check for pending signals before changing signal handler */
321 if (PyErr_CheckSignals()) {
322 return NULL;
323 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000324 if (PyOS_setsig(sig_num, func) == SIG_ERR) {
325 PyErr_SetFromErrno(PyExc_RuntimeError);
326 return NULL;
327 }
328 old_handler = Handlers[sig_num].func;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000329 Py_INCREF(obj);
330 Handlers[sig_num].func = obj;
Antoine Pitroud8931c32013-05-04 23:16:59 +0200331 if (old_handler != NULL)
332 return old_handler;
333 else
334 Py_RETURN_NONE;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000335}
336
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000337PyDoc_STRVAR(signal_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000338"signal(sig, action) -> action\n\
339\n\
340Set the action for the given signal. The action can be SIG_DFL,\n\
341SIG_IGN, or a callable Python object. The previous action is\n\
342returned. See getsignal() for possible return values.\n\
343\n\
344*** IMPORTANT NOTICE ***\n\
345A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000346the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000347
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000348
Guido van Rossume4485b01994-09-07 14:32:49 +0000349static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000350signal_getsignal(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000351{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000352 int sig_num;
353 PyObject *old_handler;
354 if (!PyArg_ParseTuple(args, "i:getsignal", &sig_num))
355 return NULL;
356 if (sig_num < 1 || sig_num >= NSIG) {
357 PyErr_SetString(PyExc_ValueError,
358 "signal number out of range");
359 return NULL;
360 }
361 old_handler = Handlers[sig_num].func;
Antoine Pitroud8931c32013-05-04 23:16:59 +0200362 if (old_handler != NULL) {
363 Py_INCREF(old_handler);
364 return old_handler;
365 }
366 else {
367 Py_RETURN_NONE;
368 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000369}
370
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000371PyDoc_STRVAR(getsignal_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000372"getsignal(sig) -> action\n\
373\n\
374Return the current action for the given signal. The return value can be:\n\
375SIG_IGN -- if the signal is being ignored\n\
376SIG_DFL -- if the default action for the signal is in effect\n\
377None -- if an unknown handler is in effect\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000378anything else -- the callable Python object used as a handler");
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000379
Facundo Batista7e251e82008-02-23 15:07:35 +0000380#ifdef HAVE_SIGINTERRUPT
381PyDoc_STRVAR(siginterrupt_doc,
382"siginterrupt(sig, flag) -> None\n\
383change system call restart behaviour: if flag is False, system calls\n\
384will be restarted when interrupted by signal sig, else system calls\n\
385will be interrupted.");
386
387static PyObject *
388signal_siginterrupt(PyObject *self, PyObject *args)
389{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000390 int sig_num;
391 int flag;
Facundo Batista7e251e82008-02-23 15:07:35 +0000392
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000393 if (!PyArg_ParseTuple(args, "ii:siginterrupt", &sig_num, &flag))
394 return NULL;
395 if (sig_num < 1 || sig_num >= NSIG) {
396 PyErr_SetString(PyExc_ValueError,
397 "signal number out of range");
398 return NULL;
399 }
400 if (siginterrupt(sig_num, flag)<0) {
401 PyErr_SetFromErrno(PyExc_RuntimeError);
402 return NULL;
403 }
Facundo Batista7e251e82008-02-23 15:07:35 +0000404
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000405 Py_INCREF(Py_None);
406 return Py_None;
Facundo Batista7e251e82008-02-23 15:07:35 +0000407}
408
409#endif
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000410
Guido van Rossum02de8972007-12-19 19:41:06 +0000411static PyObject *
412signal_set_wakeup_fd(PyObject *self, PyObject *args)
413{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000414 struct stat buf;
415 int fd, old_fd;
416 if (!PyArg_ParseTuple(args, "i:set_wakeup_fd", &fd))
417 return NULL;
Guido van Rossum02de8972007-12-19 19:41:06 +0000418#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000419 if (PyThread_get_thread_ident() != main_thread) {
420 PyErr_SetString(PyExc_ValueError,
421 "set_wakeup_fd only works in main thread");
422 return NULL;
423 }
Guido van Rossum02de8972007-12-19 19:41:06 +0000424#endif
Benjamin Peterson08e153a2013-01-18 00:10:24 -0500425 if (fd != -1 && (!_PyVerify_fd(fd) || fstat(fd, &buf) != 0)) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000426 PyErr_SetString(PyExc_ValueError, "invalid fd");
427 return NULL;
428 }
429 old_fd = wakeup_fd;
430 wakeup_fd = fd;
431 return PyLong_FromLong(old_fd);
Guido van Rossum02de8972007-12-19 19:41:06 +0000432}
433
434PyDoc_STRVAR(set_wakeup_fd_doc,
435"set_wakeup_fd(fd) -> fd\n\
436\n\
437Sets the fd to be written to (with '\\0') when a signal\n\
438comes in. A library can use this to wakeup select or poll.\n\
439The previous fd is returned.\n\
440\n\
441The fd must be non-blocking.");
442
443/* C API for the same, without all the error checking */
444int
445PySignal_SetWakeupFd(int fd)
446{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000447 int old_fd = wakeup_fd;
448 if (fd < 0)
449 fd = -1;
450 wakeup_fd = fd;
451 return old_fd;
Guido van Rossum02de8972007-12-19 19:41:06 +0000452}
453
454
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000455#ifdef HAVE_SETITIMER
456static PyObject *
457signal_setitimer(PyObject *self, PyObject *args)
458{
459 double first;
460 double interval = 0;
461 int which;
462 struct itimerval new, old;
463
464 if(!PyArg_ParseTuple(args, "id|d:setitimer", &which, &first, &interval))
Martin Panterca56dd42016-09-17 07:54:55 +0000465 return NULL;
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000466
467 timeval_from_double(first, &new.it_value);
468 timeval_from_double(interval, &new.it_interval);
469 /* Let OS check "which" value */
470 if (setitimer(which, &new, &old) != 0) {
Martin Panterca56dd42016-09-17 07:54:55 +0000471 PyErr_SetFromErrno(ItimerError);
472 return NULL;
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000473 }
474
475 return itimer_retval(&old);
476}
477
478PyDoc_STRVAR(setitimer_doc,
479"setitimer(which, seconds[, interval])\n\
480\n\
481Sets given itimer (one of ITIMER_REAL, ITIMER_VIRTUAL\n\
482or ITIMER_PROF) to fire after value seconds and after\n\
483that every interval seconds.\n\
484The itimer can be cleared by setting seconds to zero.\n\
485\n\
486Returns old values as a tuple: (delay, interval).");
487#endif
488
489
490#ifdef HAVE_GETITIMER
491static PyObject *
492signal_getitimer(PyObject *self, PyObject *args)
493{
494 int which;
495 struct itimerval old;
496
497 if (!PyArg_ParseTuple(args, "i:getitimer", &which))
Martin Panterca56dd42016-09-17 07:54:55 +0000498 return NULL;
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000499
500 if (getitimer(which, &old) != 0) {
Martin Panterca56dd42016-09-17 07:54:55 +0000501 PyErr_SetFromErrno(ItimerError);
502 return NULL;
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000503 }
504
505 return itimer_retval(&old);
506}
507
508PyDoc_STRVAR(getitimer_doc,
509"getitimer(which)\n\
510\n\
511Returns current value of given itimer.");
512#endif
513
514
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000515/* List of functions defined in the module */
Barry Warsaw92971171997-01-03 00:14:25 +0000516static PyMethodDef signal_methods[] = {
Guido van Rossum1171ee61997-08-22 20:42:00 +0000517#ifdef HAVE_ALARM
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000518 {"alarm", signal_alarm, METH_VARARGS, alarm_doc},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000519#endif
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000520#ifdef HAVE_SETITIMER
521 {"setitimer", signal_setitimer, METH_VARARGS, setitimer_doc},
522#endif
523#ifdef HAVE_GETITIMER
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000524 {"getitimer", signal_getitimer, METH_VARARGS, getitimer_doc},
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000525#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000526 {"signal", signal_signal, METH_VARARGS, signal_doc},
527 {"getsignal", signal_getsignal, METH_VARARGS, getsignal_doc},
528 {"set_wakeup_fd", signal_set_wakeup_fd, METH_VARARGS, set_wakeup_fd_doc},
Facundo Batista7e251e82008-02-23 15:07:35 +0000529#ifdef HAVE_SIGINTERRUPT
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000530 {"siginterrupt", signal_siginterrupt, METH_VARARGS, siginterrupt_doc},
Facundo Batista7e251e82008-02-23 15:07:35 +0000531#endif
Guido van Rossum1171ee61997-08-22 20:42:00 +0000532#ifdef HAVE_PAUSE
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000533 {"pause", (PyCFunction)signal_pause,
534 METH_NOARGS,pause_doc},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000535#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000536 {"default_int_handler", signal_default_int_handler,
537 METH_VARARGS, default_int_handler_doc},
538 {NULL, NULL} /* sentinel */
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000539};
540
Barry Warsaw92971171997-01-03 00:14:25 +0000541
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000542PyDoc_STRVAR(module_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000543"This module provides mechanisms to use signal handlers in Python.\n\
544\n\
545Functions:\n\
546\n\
547alarm() -- cause SIGALRM after a specified time [Unix only]\n\
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000548setitimer() -- cause a signal (described below) after a specified\n\
549 float time and the timer may restart then [Unix only]\n\
550getitimer() -- get current value of timer [Unix only]\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000551signal() -- set the action for a given signal\n\
552getsignal() -- get the signal action for a given signal\n\
553pause() -- wait until a signal arrives [Unix only]\n\
554default_int_handler() -- default SIGINT handler\n\
555\n\
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000556signal constants:\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000557SIG_DFL -- used to refer to the system default handler\n\
558SIG_IGN -- used to ignore the signal\n\
559NSIG -- number of defined signals\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000560SIGINT, SIGTERM, etc. -- signal numbers\n\
561\n\
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000562itimer constants:\n\
563ITIMER_REAL -- decrements in real time, and delivers SIGALRM upon\n\
564 expiration\n\
565ITIMER_VIRTUAL -- decrements only when the process is executing,\n\
566 and delivers SIGVTALRM upon expiration\n\
567ITIMER_PROF -- decrements both when the process is executing and\n\
568 when the system is executing on behalf of the process.\n\
569 Coupled with ITIMER_VIRTUAL, this timer is usually\n\
570 used to profile the time spent by the application\n\
571 in user and kernel space. SIGPROF is delivered upon\n\
572 expiration.\n\
573\n\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000574*** IMPORTANT NOTICE ***\n\
575A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000576the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000577
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000578PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000579initsignal(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000580{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000581 PyObject *m, *d, *x;
582 int i;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000583
584#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000585 main_thread = PyThread_get_thread_ident();
586 main_pid = getpid();
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000587#endif
588
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000589 /* Create the module and add the functions */
590 m = Py_InitModule3("signal", signal_methods, module_doc);
591 if (m == NULL)
592 return;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000593
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000594 /* Add some symbolic constants to the module */
595 d = PyModule_GetDict(m);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000596
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000597 x = DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL);
598 if (!x || PyDict_SetItemString(d, "SIG_DFL", x) < 0)
599 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000600
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000601 x = IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN);
602 if (!x || PyDict_SetItemString(d, "SIG_IGN", x) < 0)
603 goto finally;
Barry Warsaw92971171997-01-03 00:14:25 +0000604
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000605 x = PyInt_FromLong((long)NSIG);
606 if (!x || PyDict_SetItemString(d, "NSIG", x) < 0)
607 goto finally;
608 Py_DECREF(x);
Barry Warsaw92971171997-01-03 00:14:25 +0000609
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000610 x = IntHandler = PyDict_GetItemString(d, "default_int_handler");
611 if (!x)
612 goto finally;
613 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +0000614
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000615 Handlers[0].tripped = 0;
616 for (i = 1; i < NSIG; i++) {
617 void (*t)(int);
618 t = PyOS_getsig(i);
619 Handlers[i].tripped = 0;
620 if (t == SIG_DFL)
621 Handlers[i].func = DefaultHandler;
622 else if (t == SIG_IGN)
623 Handlers[i].func = IgnoreHandler;
624 else
625 Handlers[i].func = Py_None; /* None of our business */
626 Py_INCREF(Handlers[i].func);
627 }
628 if (Handlers[SIGINT].func == DefaultHandler) {
629 /* Install default int handler */
630 Py_INCREF(IntHandler);
Serhiy Storchaka763a61c2016-04-10 18:05:12 +0300631 Py_SETREF(Handlers[SIGINT].func, IntHandler);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000632 old_siginthandler = PyOS_setsig(SIGINT, signal_handler);
633 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000634
635#ifdef SIGHUP
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000636 x = PyInt_FromLong(SIGHUP);
637 PyDict_SetItemString(d, "SIGHUP", x);
638 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000639#endif
640#ifdef SIGINT
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000641 x = PyInt_FromLong(SIGINT);
642 PyDict_SetItemString(d, "SIGINT", x);
643 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000644#endif
Tim Peters1ce3cf72001-10-01 17:58:40 +0000645#ifdef SIGBREAK
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000646 x = PyInt_FromLong(SIGBREAK);
647 PyDict_SetItemString(d, "SIGBREAK", x);
648 Py_XDECREF(x);
Tim Peters1ce3cf72001-10-01 17:58:40 +0000649#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000650#ifdef SIGQUIT
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000651 x = PyInt_FromLong(SIGQUIT);
652 PyDict_SetItemString(d, "SIGQUIT", x);
653 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000654#endif
655#ifdef SIGILL
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000656 x = PyInt_FromLong(SIGILL);
657 PyDict_SetItemString(d, "SIGILL", x);
658 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000659#endif
660#ifdef SIGTRAP
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000661 x = PyInt_FromLong(SIGTRAP);
662 PyDict_SetItemString(d, "SIGTRAP", x);
663 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000664#endif
665#ifdef SIGIOT
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000666 x = PyInt_FromLong(SIGIOT);
667 PyDict_SetItemString(d, "SIGIOT", x);
668 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000669#endif
670#ifdef SIGABRT
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000671 x = PyInt_FromLong(SIGABRT);
672 PyDict_SetItemString(d, "SIGABRT", x);
673 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000674#endif
675#ifdef SIGEMT
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000676 x = PyInt_FromLong(SIGEMT);
677 PyDict_SetItemString(d, "SIGEMT", x);
678 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000679#endif
680#ifdef SIGFPE
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000681 x = PyInt_FromLong(SIGFPE);
682 PyDict_SetItemString(d, "SIGFPE", x);
683 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000684#endif
685#ifdef SIGKILL
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000686 x = PyInt_FromLong(SIGKILL);
687 PyDict_SetItemString(d, "SIGKILL", x);
688 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000689#endif
690#ifdef SIGBUS
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000691 x = PyInt_FromLong(SIGBUS);
692 PyDict_SetItemString(d, "SIGBUS", x);
693 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000694#endif
695#ifdef SIGSEGV
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000696 x = PyInt_FromLong(SIGSEGV);
697 PyDict_SetItemString(d, "SIGSEGV", x);
698 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000699#endif
700#ifdef SIGSYS
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000701 x = PyInt_FromLong(SIGSYS);
702 PyDict_SetItemString(d, "SIGSYS", x);
703 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000704#endif
705#ifdef SIGPIPE
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000706 x = PyInt_FromLong(SIGPIPE);
707 PyDict_SetItemString(d, "SIGPIPE", x);
708 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000709#endif
710#ifdef SIGALRM
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000711 x = PyInt_FromLong(SIGALRM);
712 PyDict_SetItemString(d, "SIGALRM", x);
713 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000714#endif
715#ifdef SIGTERM
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000716 x = PyInt_FromLong(SIGTERM);
717 PyDict_SetItemString(d, "SIGTERM", x);
718 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000719#endif
720#ifdef SIGUSR1
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000721 x = PyInt_FromLong(SIGUSR1);
722 PyDict_SetItemString(d, "SIGUSR1", x);
723 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000724#endif
725#ifdef SIGUSR2
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000726 x = PyInt_FromLong(SIGUSR2);
727 PyDict_SetItemString(d, "SIGUSR2", x);
728 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000729#endif
730#ifdef SIGCLD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000731 x = PyInt_FromLong(SIGCLD);
732 PyDict_SetItemString(d, "SIGCLD", x);
733 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000734#endif
735#ifdef SIGCHLD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000736 x = PyInt_FromLong(SIGCHLD);
737 PyDict_SetItemString(d, "SIGCHLD", x);
738 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000739#endif
740#ifdef SIGPWR
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000741 x = PyInt_FromLong(SIGPWR);
742 PyDict_SetItemString(d, "SIGPWR", x);
743 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000744#endif
745#ifdef SIGIO
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000746 x = PyInt_FromLong(SIGIO);
747 PyDict_SetItemString(d, "SIGIO", x);
748 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000749#endif
750#ifdef SIGURG
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000751 x = PyInt_FromLong(SIGURG);
752 PyDict_SetItemString(d, "SIGURG", x);
753 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000754#endif
755#ifdef SIGWINCH
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000756 x = PyInt_FromLong(SIGWINCH);
757 PyDict_SetItemString(d, "SIGWINCH", x);
758 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000759#endif
760#ifdef SIGPOLL
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000761 x = PyInt_FromLong(SIGPOLL);
762 PyDict_SetItemString(d, "SIGPOLL", x);
763 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000764#endif
765#ifdef SIGSTOP
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000766 x = PyInt_FromLong(SIGSTOP);
767 PyDict_SetItemString(d, "SIGSTOP", x);
768 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000769#endif
770#ifdef SIGTSTP
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000771 x = PyInt_FromLong(SIGTSTP);
772 PyDict_SetItemString(d, "SIGTSTP", x);
773 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000774#endif
775#ifdef SIGCONT
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000776 x = PyInt_FromLong(SIGCONT);
777 PyDict_SetItemString(d, "SIGCONT", x);
778 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000779#endif
780#ifdef SIGTTIN
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000781 x = PyInt_FromLong(SIGTTIN);
782 PyDict_SetItemString(d, "SIGTTIN", x);
783 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000784#endif
785#ifdef SIGTTOU
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000786 x = PyInt_FromLong(SIGTTOU);
787 PyDict_SetItemString(d, "SIGTTOU", x);
788 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000789#endif
790#ifdef SIGVTALRM
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000791 x = PyInt_FromLong(SIGVTALRM);
792 PyDict_SetItemString(d, "SIGVTALRM", x);
793 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000794#endif
795#ifdef SIGPROF
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000796 x = PyInt_FromLong(SIGPROF);
797 PyDict_SetItemString(d, "SIGPROF", x);
798 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000799#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000800#ifdef SIGXCPU
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000801 x = PyInt_FromLong(SIGXCPU);
802 PyDict_SetItemString(d, "SIGXCPU", x);
803 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000804#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000805#ifdef SIGXFSZ
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000806 x = PyInt_FromLong(SIGXFSZ);
807 PyDict_SetItemString(d, "SIGXFSZ", x);
808 Py_XDECREF(x);
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000809#endif
Anthony Baxterf37f37d2003-07-31 10:35:29 +0000810#ifdef SIGRTMIN
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000811 x = PyInt_FromLong(SIGRTMIN);
812 PyDict_SetItemString(d, "SIGRTMIN", x);
813 Py_XDECREF(x);
Anthony Baxterf37f37d2003-07-31 10:35:29 +0000814#endif
815#ifdef SIGRTMAX
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000816 x = PyInt_FromLong(SIGRTMAX);
817 PyDict_SetItemString(d, "SIGRTMAX", x);
818 Py_XDECREF(x);
Anthony Baxterf37f37d2003-07-31 10:35:29 +0000819#endif
Martin v. Löwis175af252002-01-12 11:43:25 +0000820#ifdef SIGINFO
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000821 x = PyInt_FromLong(SIGINFO);
822 PyDict_SetItemString(d, "SIGINFO", x);
823 Py_XDECREF(x);
Martin v. Löwis175af252002-01-12 11:43:25 +0000824#endif
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000825
826#ifdef ITIMER_REAL
827 x = PyLong_FromLong(ITIMER_REAL);
828 PyDict_SetItemString(d, "ITIMER_REAL", x);
829 Py_DECREF(x);
830#endif
831#ifdef ITIMER_VIRTUAL
832 x = PyLong_FromLong(ITIMER_VIRTUAL);
833 PyDict_SetItemString(d, "ITIMER_VIRTUAL", x);
834 Py_DECREF(x);
835#endif
836#ifdef ITIMER_PROF
837 x = PyLong_FromLong(ITIMER_PROF);
838 PyDict_SetItemString(d, "ITIMER_PROF", x);
839 Py_DECREF(x);
840#endif
841
842#if defined (HAVE_SETITIMER) || defined (HAVE_GETITIMER)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000843 ItimerError = PyErr_NewException("signal.ItimerError",
Martin Panterca56dd42016-09-17 07:54:55 +0000844 PyExc_IOError, NULL);
Neal Norwitz18aa3882008-08-24 05:04:52 +0000845 if (ItimerError != NULL)
Martin Panterca56dd42016-09-17 07:54:55 +0000846 PyDict_SetItemString(d, "ItimerError", ItimerError);
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000847#endif
848
Brian Curtine5aa8862010-04-02 23:26:06 +0000849#ifdef CTRL_C_EVENT
850 x = PyInt_FromLong(CTRL_C_EVENT);
851 PyDict_SetItemString(d, "CTRL_C_EVENT", x);
852 Py_DECREF(x);
853#endif
854
855#ifdef CTRL_BREAK_EVENT
856 x = PyInt_FromLong(CTRL_BREAK_EVENT);
857 PyDict_SetItemString(d, "CTRL_BREAK_EVENT", x);
858 Py_DECREF(x);
859#endif
860
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000861 if (!PyErr_Occurred())
Guido van Rossum08c16611997-08-02 03:01:42 +0000862 return;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000863
864 /* Check for errors */
865 finally:
866 return;
Guido van Rossum08c16611997-08-02 03:01:42 +0000867}
868
869static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000870finisignal(void)
Guido van Rossum08c16611997-08-02 03:01:42 +0000871{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000872 int i;
873 PyObject *func;
Guido van Rossum08c16611997-08-02 03:01:42 +0000874
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000875 PyOS_setsig(SIGINT, old_siginthandler);
876 old_siginthandler = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +0000877
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000878 for (i = 1; i < NSIG; i++) {
879 func = Handlers[i].func;
880 Handlers[i].tripped = 0;
881 Handlers[i].func = NULL;
882 if (i != SIGINT && func != NULL && func != Py_None &&
883 func != DefaultHandler && func != IgnoreHandler)
884 PyOS_setsig(i, SIG_DFL);
885 Py_XDECREF(func);
886 }
Guido van Rossum08c16611997-08-02 03:01:42 +0000887
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000888 Py_XDECREF(IntHandler);
889 IntHandler = NULL;
890 Py_XDECREF(DefaultHandler);
891 DefaultHandler = NULL;
892 Py_XDECREF(IgnoreHandler);
893 IgnoreHandler = NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000894}
895
Barry Warsaw92971171997-01-03 00:14:25 +0000896
Barry Warsaw92971171997-01-03 00:14:25 +0000897/* Declared in pyerrors.h */
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000898int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000899PyErr_CheckSignals(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000900{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000901 int i;
902 PyObject *f;
Barry Warsaw92971171997-01-03 00:14:25 +0000903
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000904 if (!is_tripped)
905 return 0;
Guido van Rossum137c49c2007-12-10 23:00:12 +0000906
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000907#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000908 if (PyThread_get_thread_ident() != main_thread)
909 return 0;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000910#endif
Guido van Rossum137c49c2007-12-10 23:00:12 +0000911
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000912 /*
913 * The is_tripped variable is meant to speed up the calls to
914 * PyErr_CheckSignals (both directly or via pending calls) when no
915 * signal has arrived. This variable is set to 1 when a signal arrives
916 * and it is set to 0 here, when we know some signals arrived. This way
917 * we can run the registered handlers with no signals blocked.
918 *
919 * NOTE: with this approach we can have a situation where is_tripped is
920 * 1 but we have no more signals to handle (Handlers[i].tripped
921 * is 0 for every signal i). This won't do us any harm (except
922 * we're gonna spent some cycles for nothing). This happens when
923 * we receive a signal i after we zero is_tripped and before we
924 * check Handlers[i].tripped.
925 */
926 is_tripped = 0;
Guido van Rossum137c49c2007-12-10 23:00:12 +0000927
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000928 if (!(f = (PyObject *)PyEval_GetFrame()))
929 f = Py_None;
Guido van Rossum137c49c2007-12-10 23:00:12 +0000930
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000931 for (i = 1; i < NSIG; i++) {
932 if (Handlers[i].tripped) {
933 PyObject *result = NULL;
934 PyObject *arglist = Py_BuildValue("(iO)", i, f);
935 Handlers[i].tripped = 0;
Barry Warsaw92971171997-01-03 00:14:25 +0000936
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000937 if (arglist) {
938 result = PyEval_CallObject(Handlers[i].func,
939 arglist);
940 Py_DECREF(arglist);
941 }
942 if (!result)
943 return -1;
Barry Warsaw92971171997-01-03 00:14:25 +0000944
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000945 Py_DECREF(result);
946 }
947 }
Guido van Rossum137c49c2007-12-10 23:00:12 +0000948
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000949 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000950}
951
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000952
Barry Warsaw92971171997-01-03 00:14:25 +0000953/* Replacements for intrcheck.c functionality
954 * Declared in pyerrors.h
955 */
956void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000957PyErr_SetInterrupt(void)
Barry Warsaw92971171997-01-03 00:14:25 +0000958{
Victor Stinner33feeab2011-04-18 16:33:28 +0200959 trip_signal(SIGINT);
Barry Warsaw92971171997-01-03 00:14:25 +0000960}
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000961
962void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000963PyOS_InitInterrupts(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000964{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000965 initsignal();
966 _PyImport_FixupExtension("signal", "signal");
Guido van Rossum08c16611997-08-02 03:01:42 +0000967}
968
969void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000970PyOS_FiniInterrupts(void)
Guido van Rossum08c16611997-08-02 03:01:42 +0000971{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000972 finisignal();
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000973}
974
975int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000976PyOS_InterruptOccurred(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000977{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000978 if (Handlers[SIGINT].tripped) {
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000979#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000980 if (PyThread_get_thread_ident() != main_thread)
981 return 0;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000982#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000983 Handlers[SIGINT].tripped = 0;
984 return 1;
985 }
986 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000987}
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000988
Gregory P. Smithc1ce93a2012-11-10 20:38:17 -0800989static void
990_clear_pending_signals(void)
991{
992 int i;
993 if (!is_tripped)
994 return;
995 is_tripped = 0;
996 for (i = 1; i < NSIG; ++i) {
997 Handlers[i].tripped = 0;
998 }
999}
1000
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001001void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001002PyOS_AfterFork(void)
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001003{
Gregory P. Smithc1ce93a2012-11-10 20:38:17 -08001004 /* Clear the signal flags after forking so that they aren't handled
1005 * in both processes if they came in just before the fork() but before
1006 * the interpreter had an opportunity to call the handlers. issue9535. */
1007 _clear_pending_signals();
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001008#ifdef WITH_THREAD
Charles-François Natalie0e88b02012-02-02 19:57:19 +01001009 /* PyThread_ReInitTLS() must be called early, to make sure that the TLS API
1010 * can be called safely. */
1011 PyThread_ReInitTLS();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001012 PyEval_ReInitThreads();
1013 main_thread = PyThread_get_thread_ident();
1014 main_pid = getpid();
1015 _PyImport_ReInitLock();
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001016#endif
1017}