blob: 908c2ee994044c0f968b52ba78154b6dedefe32b [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;
114}
115
Christian Heimes32a66a02008-10-02 19:47:50 +0000116Py_LOCAL_INLINE(double)
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000117double_from_timeval(struct timeval *tv)
118{
119 return tv->tv_sec + (double)(tv->tv_usec / 1000000.0);
120}
121
122static PyObject *
123itimer_retval(struct itimerval *iv)
124{
125 PyObject *r, *v;
126
127 r = PyTuple_New(2);
128 if (r == NULL)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000129 return NULL;
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000130
131 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_value)))) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000132 Py_DECREF(r);
133 return NULL;
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000134 }
135
136 PyTuple_SET_ITEM(r, 0, v);
137
138 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_interval)))) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000139 Py_DECREF(r);
140 return NULL;
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000141 }
142
143 PyTuple_SET_ITEM(r, 1, v);
144
145 return r;
146}
147#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000148
Guido van Rossume4485b01994-09-07 14:32:49 +0000149static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000150signal_default_int_handler(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000151{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000152 PyErr_SetNone(PyExc_KeyboardInterrupt);
153 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000154}
155
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000156PyDoc_STRVAR(default_int_handler_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000157"default_int_handler(...)\n\
158\n\
Michael W. Hudson24ec2112004-06-17 15:55:53 +0000159The default handler for SIGINT installed by Python.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000160It raises KeyboardInterrupt.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000161
Thomas Wouters0796b002000-07-22 23:49:30 +0000162
163static int
164checksignals_witharg(void * unused)
165{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000166 return PyErr_CheckSignals();
Thomas Wouters0796b002000-07-22 23:49:30 +0000167}
168
Tim Peters4f1b2082000-07-23 21:18:09 +0000169static void
Victor Stinner33feeab2011-04-18 16:33:28 +0200170trip_signal(int sig_num)
171{
172 Handlers[sig_num].tripped = 1;
173 if (is_tripped)
174 return;
175 /* Set is_tripped after setting .tripped, as it gets
176 cleared in PyErr_CheckSignals() before .tripped. */
177 is_tripped = 1;
178 Py_AddPendingCall(checksignals_witharg, NULL);
179 if (wakeup_fd != -1)
180 write(wakeup_fd, "\0", 1);
181}
182
183static void
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000184signal_handler(int sig_num)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000185{
Antoine Pitrou6b1167c2010-11-05 19:55:02 +0000186 int save_errno = errno;
187
188#if defined(WITH_THREAD) && defined(WITH_PTH)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000189 if (PyThread_get_thread_ident() != main_thread) {
190 pth_raise(*(pth_t *) main_thread, sig_num);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000191 }
Antoine Pitrou6b1167c2010-11-05 19:55:02 +0000192 else
Guido van Rossum9e8181b2000-09-19 00:46:46 +0000193#endif
Antoine Pitrou6b1167c2010-11-05 19:55:02 +0000194 {
195#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000196 /* See NOTES section above */
Antoine Pitrou6b1167c2010-11-05 19:55:02 +0000197 if (getpid() == main_pid)
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000198#endif
Antoine Pitrou6b1167c2010-11-05 19:55:02 +0000199 {
Victor Stinner33feeab2011-04-18 16:33:28 +0200200 trip_signal(sig_num);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000201 }
Antoine Pitrou6b1167c2010-11-05 19:55:02 +0000202
Jean-Paul Calderonee54ddf12010-05-08 20:06:02 +0000203#ifndef HAVE_SIGACTION
Antoine Pitrou6b1167c2010-11-05 19:55:02 +0000204#ifdef SIGCHLD
205 /* To avoid infinite recursion, this signal remains
206 reset until explicit re-instated.
207 Don't clear the 'func' field as it is our pointer
208 to the Python handler... */
209 if (sig_num != SIGCHLD)
210#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000211 /* If the handler was not set up with sigaction, reinstall it. See
212 * Python/pythonrun.c for the implementation of PyOS_setsig which
213 * makes this true. See also issue8354. */
214 PyOS_setsig(sig_num, signal_handler);
Jean-Paul Calderonee54ddf12010-05-08 20:06:02 +0000215#endif
Antoine Pitrou6b1167c2010-11-05 19:55:02 +0000216 }
217
218 /* Issue #10311: asynchronously executing signal handlers should not
219 mutate errno under the feet of unsuspecting C code. */
220 errno = save_errno;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000221}
Guido van Rossume4485b01994-09-07 14:32:49 +0000222
Guido van Rossum06d511d1995-03-10 15:13:48 +0000223
Guido van Rossum1171ee61997-08-22 20:42:00 +0000224#ifdef HAVE_ALARM
Guido van Rossume4485b01994-09-07 14:32:49 +0000225static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000226signal_alarm(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000227{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000228 int t;
229 if (!PyArg_ParseTuple(args, "i:alarm", &t))
230 return NULL;
231 /* alarm() returns the number of seconds remaining */
232 return PyInt_FromLong((long)alarm(t));
Guido van Rossumaa0f4c71994-08-23 13:49:37 +0000233}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000234
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000235PyDoc_STRVAR(alarm_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000236"alarm(seconds)\n\
237\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000238Arrange for SIGALRM to arrive after the given number of seconds.");
Guido van Rossum06d511d1995-03-10 15:13:48 +0000239#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000240
Guido van Rossum1171ee61997-08-22 20:42:00 +0000241#ifdef HAVE_PAUSE
Guido van Rossuma597dde1995-01-10 20:56:29 +0000242static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000243signal_pause(PyObject *self)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000244{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000245 Py_BEGIN_ALLOW_THREADS
246 (void)pause();
247 Py_END_ALLOW_THREADS
248 /* make sure that any exceptions that got raised are propagated
249 * back into Python
250 */
251 if (PyErr_CheckSignals())
252 return NULL;
Barry Warsaw92971171997-01-03 00:14:25 +0000253
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000254 Py_INCREF(Py_None);
255 return Py_None;
Guido van Rossume4485b01994-09-07 14:32:49 +0000256}
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000257PyDoc_STRVAR(pause_doc,
Barry Warsaw1ee36ff1998-07-21 22:41:18 +0000258"pause()\n\
259\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000260Wait until a signal arrives.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000261
Guido van Rossum06d511d1995-03-10 15:13:48 +0000262#endif
Guido van Rossume4485b01994-09-07 14:32:49 +0000263
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000264
Guido van Rossume4485b01994-09-07 14:32:49 +0000265static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000266signal_signal(PyObject *self, PyObject *args)
Guido van Rossume4485b01994-09-07 14:32:49 +0000267{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000268 PyObject *obj;
269 int sig_num;
270 PyObject *old_handler;
271 void (*func)(int);
272 if (!PyArg_ParseTuple(args, "iO:signal", &sig_num, &obj))
273 return NULL;
Brian Curtin24af0e92010-08-06 19:41:01 +0000274#ifdef MS_WINDOWS
275 /* Validate that sig_num is one of the allowable signals */
Brian Curtin84263332010-09-06 16:17:50 +0000276 switch (sig_num) {
277 case SIGABRT: break;
Brian Curtin1390dd72010-10-01 16:44:03 +0000278#ifdef SIGBREAK
279 /* Issue #10003: SIGBREAK is not documented as permitted, but works
280 and corresponds to CTRL_BREAK_EVENT. */
281 case SIGBREAK: break;
282#endif
Brian Curtin84263332010-09-06 16:17:50 +0000283 case SIGFPE: break;
284 case SIGILL: break;
285 case SIGINT: break;
286 case SIGSEGV: break;
287 case SIGTERM: break;
288 default:
289 PyErr_SetString(PyExc_ValueError, "invalid signal value");
290 return NULL;
Brian Curtin24af0e92010-08-06 19:41:01 +0000291 }
292#endif
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000293#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000294 if (PyThread_get_thread_ident() != main_thread) {
295 PyErr_SetString(PyExc_ValueError,
296 "signal only works in main thread");
297 return NULL;
298 }
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000299#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000300 if (sig_num < 1 || sig_num >= NSIG) {
301 PyErr_SetString(PyExc_ValueError,
302 "signal number out of range");
303 return NULL;
304 }
305 if (obj == IgnoreHandler)
306 func = SIG_IGN;
307 else if (obj == DefaultHandler)
308 func = SIG_DFL;
309 else if (!PyCallable_Check(obj)) {
310 PyErr_SetString(PyExc_TypeError,
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000311"signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object");
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000312 return NULL;
313 }
314 else
315 func = signal_handler;
316 if (PyOS_setsig(sig_num, func) == SIG_ERR) {
317 PyErr_SetFromErrno(PyExc_RuntimeError);
318 return NULL;
319 }
320 old_handler = Handlers[sig_num].func;
321 Handlers[sig_num].tripped = 0;
322 Py_INCREF(obj);
323 Handlers[sig_num].func = obj;
324 return old_handler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000325}
326
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000327PyDoc_STRVAR(signal_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000328"signal(sig, action) -> action\n\
329\n\
330Set the action for the given signal. The action can be SIG_DFL,\n\
331SIG_IGN, or a callable Python object. The previous action is\n\
332returned. See getsignal() for possible return values.\n\
333\n\
334*** IMPORTANT NOTICE ***\n\
335A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000336the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000337
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000338
Guido van Rossume4485b01994-09-07 14:32:49 +0000339static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000340signal_getsignal(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000341{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000342 int sig_num;
343 PyObject *old_handler;
344 if (!PyArg_ParseTuple(args, "i:getsignal", &sig_num))
345 return NULL;
346 if (sig_num < 1 || sig_num >= NSIG) {
347 PyErr_SetString(PyExc_ValueError,
348 "signal number out of range");
349 return NULL;
350 }
351 old_handler = Handlers[sig_num].func;
352 Py_INCREF(old_handler);
353 return old_handler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000354}
355
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000356PyDoc_STRVAR(getsignal_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000357"getsignal(sig) -> action\n\
358\n\
359Return the current action for the given signal. The return value can be:\n\
360SIG_IGN -- if the signal is being ignored\n\
361SIG_DFL -- if the default action for the signal is in effect\n\
362None -- if an unknown handler is in effect\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000363anything else -- the callable Python object used as a handler");
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000364
Facundo Batista7e251e82008-02-23 15:07:35 +0000365#ifdef HAVE_SIGINTERRUPT
366PyDoc_STRVAR(siginterrupt_doc,
367"siginterrupt(sig, flag) -> None\n\
368change system call restart behaviour: if flag is False, system calls\n\
369will be restarted when interrupted by signal sig, else system calls\n\
370will be interrupted.");
371
372static PyObject *
373signal_siginterrupt(PyObject *self, PyObject *args)
374{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000375 int sig_num;
376 int flag;
Facundo Batista7e251e82008-02-23 15:07:35 +0000377
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000378 if (!PyArg_ParseTuple(args, "ii:siginterrupt", &sig_num, &flag))
379 return NULL;
380 if (sig_num < 1 || sig_num >= NSIG) {
381 PyErr_SetString(PyExc_ValueError,
382 "signal number out of range");
383 return NULL;
384 }
385 if (siginterrupt(sig_num, flag)<0) {
386 PyErr_SetFromErrno(PyExc_RuntimeError);
387 return NULL;
388 }
Facundo Batista7e251e82008-02-23 15:07:35 +0000389
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000390 Py_INCREF(Py_None);
391 return Py_None;
Facundo Batista7e251e82008-02-23 15:07:35 +0000392}
393
394#endif
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000395
Guido van Rossum02de8972007-12-19 19:41:06 +0000396static PyObject *
397signal_set_wakeup_fd(PyObject *self, PyObject *args)
398{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000399 struct stat buf;
400 int fd, old_fd;
401 if (!PyArg_ParseTuple(args, "i:set_wakeup_fd", &fd))
402 return NULL;
Guido van Rossum02de8972007-12-19 19:41:06 +0000403#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000404 if (PyThread_get_thread_ident() != main_thread) {
405 PyErr_SetString(PyExc_ValueError,
406 "set_wakeup_fd only works in main thread");
407 return NULL;
408 }
Guido van Rossum02de8972007-12-19 19:41:06 +0000409#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000410 if (fd != -1 && fstat(fd, &buf) != 0) {
411 PyErr_SetString(PyExc_ValueError, "invalid fd");
412 return NULL;
413 }
414 old_fd = wakeup_fd;
415 wakeup_fd = fd;
416 return PyLong_FromLong(old_fd);
Guido van Rossum02de8972007-12-19 19:41:06 +0000417}
418
419PyDoc_STRVAR(set_wakeup_fd_doc,
420"set_wakeup_fd(fd) -> fd\n\
421\n\
422Sets the fd to be written to (with '\\0') when a signal\n\
423comes in. A library can use this to wakeup select or poll.\n\
424The previous fd is returned.\n\
425\n\
426The fd must be non-blocking.");
427
428/* C API for the same, without all the error checking */
429int
430PySignal_SetWakeupFd(int fd)
431{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000432 int old_fd = wakeup_fd;
433 if (fd < 0)
434 fd = -1;
435 wakeup_fd = fd;
436 return old_fd;
Guido van Rossum02de8972007-12-19 19:41:06 +0000437}
438
439
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000440#ifdef HAVE_SETITIMER
441static PyObject *
442signal_setitimer(PyObject *self, PyObject *args)
443{
444 double first;
445 double interval = 0;
446 int which;
447 struct itimerval new, old;
448
449 if(!PyArg_ParseTuple(args, "id|d:setitimer", &which, &first, &interval))
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000450 return NULL;
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000451
452 timeval_from_double(first, &new.it_value);
453 timeval_from_double(interval, &new.it_interval);
454 /* Let OS check "which" value */
455 if (setitimer(which, &new, &old) != 0) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000456 PyErr_SetFromErrno(ItimerError);
457 return NULL;
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000458 }
459
460 return itimer_retval(&old);
461}
462
463PyDoc_STRVAR(setitimer_doc,
464"setitimer(which, seconds[, interval])\n\
465\n\
466Sets given itimer (one of ITIMER_REAL, ITIMER_VIRTUAL\n\
467or ITIMER_PROF) to fire after value seconds and after\n\
468that every interval seconds.\n\
469The itimer can be cleared by setting seconds to zero.\n\
470\n\
471Returns old values as a tuple: (delay, interval).");
472#endif
473
474
475#ifdef HAVE_GETITIMER
476static PyObject *
477signal_getitimer(PyObject *self, PyObject *args)
478{
479 int which;
480 struct itimerval old;
481
482 if (!PyArg_ParseTuple(args, "i:getitimer", &which))
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000483 return NULL;
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000484
485 if (getitimer(which, &old) != 0) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000486 PyErr_SetFromErrno(ItimerError);
487 return NULL;
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000488 }
489
490 return itimer_retval(&old);
491}
492
493PyDoc_STRVAR(getitimer_doc,
494"getitimer(which)\n\
495\n\
496Returns current value of given itimer.");
497#endif
498
499
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000500/* List of functions defined in the module */
Barry Warsaw92971171997-01-03 00:14:25 +0000501static PyMethodDef signal_methods[] = {
Guido van Rossum1171ee61997-08-22 20:42:00 +0000502#ifdef HAVE_ALARM
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000503 {"alarm", signal_alarm, METH_VARARGS, alarm_doc},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000504#endif
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000505#ifdef HAVE_SETITIMER
506 {"setitimer", signal_setitimer, METH_VARARGS, setitimer_doc},
507#endif
508#ifdef HAVE_GETITIMER
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000509 {"getitimer", signal_getitimer, METH_VARARGS, getitimer_doc},
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000510#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000511 {"signal", signal_signal, METH_VARARGS, signal_doc},
512 {"getsignal", signal_getsignal, METH_VARARGS, getsignal_doc},
513 {"set_wakeup_fd", signal_set_wakeup_fd, METH_VARARGS, set_wakeup_fd_doc},
Facundo Batista7e251e82008-02-23 15:07:35 +0000514#ifdef HAVE_SIGINTERRUPT
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000515 {"siginterrupt", signal_siginterrupt, METH_VARARGS, siginterrupt_doc},
Facundo Batista7e251e82008-02-23 15:07:35 +0000516#endif
Guido van Rossum1171ee61997-08-22 20:42:00 +0000517#ifdef HAVE_PAUSE
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000518 {"pause", (PyCFunction)signal_pause,
519 METH_NOARGS,pause_doc},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000520#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000521 {"default_int_handler", signal_default_int_handler,
522 METH_VARARGS, default_int_handler_doc},
523 {NULL, NULL} /* sentinel */
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000524};
525
Barry Warsaw92971171997-01-03 00:14:25 +0000526
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000527PyDoc_STRVAR(module_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000528"This module provides mechanisms to use signal handlers in Python.\n\
529\n\
530Functions:\n\
531\n\
532alarm() -- cause SIGALRM after a specified time [Unix only]\n\
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000533setitimer() -- cause a signal (described below) after a specified\n\
534 float time and the timer may restart then [Unix only]\n\
535getitimer() -- get current value of timer [Unix only]\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000536signal() -- set the action for a given signal\n\
537getsignal() -- get the signal action for a given signal\n\
538pause() -- wait until a signal arrives [Unix only]\n\
539default_int_handler() -- default SIGINT handler\n\
540\n\
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000541signal constants:\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000542SIG_DFL -- used to refer to the system default handler\n\
543SIG_IGN -- used to ignore the signal\n\
544NSIG -- number of defined signals\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000545SIGINT, SIGTERM, etc. -- signal numbers\n\
546\n\
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000547itimer constants:\n\
548ITIMER_REAL -- decrements in real time, and delivers SIGALRM upon\n\
549 expiration\n\
550ITIMER_VIRTUAL -- decrements only when the process is executing,\n\
551 and delivers SIGVTALRM upon expiration\n\
552ITIMER_PROF -- decrements both when the process is executing and\n\
553 when the system is executing on behalf of the process.\n\
554 Coupled with ITIMER_VIRTUAL, this timer is usually\n\
555 used to profile the time spent by the application\n\
556 in user and kernel space. SIGPROF is delivered upon\n\
557 expiration.\n\
558\n\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000559*** IMPORTANT NOTICE ***\n\
560A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000561the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000562
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000563PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000564initsignal(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000565{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000566 PyObject *m, *d, *x;
567 int i;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000568
569#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000570 main_thread = PyThread_get_thread_ident();
571 main_pid = getpid();
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000572#endif
573
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000574 /* Create the module and add the functions */
575 m = Py_InitModule3("signal", signal_methods, module_doc);
576 if (m == NULL)
577 return;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000578
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000579 /* Add some symbolic constants to the module */
580 d = PyModule_GetDict(m);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000581
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000582 x = DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL);
583 if (!x || PyDict_SetItemString(d, "SIG_DFL", x) < 0)
584 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000585
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000586 x = IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN);
587 if (!x || PyDict_SetItemString(d, "SIG_IGN", x) < 0)
588 goto finally;
Barry Warsaw92971171997-01-03 00:14:25 +0000589
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000590 x = PyInt_FromLong((long)NSIG);
591 if (!x || PyDict_SetItemString(d, "NSIG", x) < 0)
592 goto finally;
593 Py_DECREF(x);
Barry Warsaw92971171997-01-03 00:14:25 +0000594
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000595 x = IntHandler = PyDict_GetItemString(d, "default_int_handler");
596 if (!x)
597 goto finally;
598 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +0000599
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000600 Handlers[0].tripped = 0;
601 for (i = 1; i < NSIG; i++) {
602 void (*t)(int);
603 t = PyOS_getsig(i);
604 Handlers[i].tripped = 0;
605 if (t == SIG_DFL)
606 Handlers[i].func = DefaultHandler;
607 else if (t == SIG_IGN)
608 Handlers[i].func = IgnoreHandler;
609 else
610 Handlers[i].func = Py_None; /* None of our business */
611 Py_INCREF(Handlers[i].func);
612 }
613 if (Handlers[SIGINT].func == DefaultHandler) {
614 /* Install default int handler */
615 Py_INCREF(IntHandler);
616 Py_DECREF(Handlers[SIGINT].func);
617 Handlers[SIGINT].func = IntHandler;
618 old_siginthandler = PyOS_setsig(SIGINT, signal_handler);
619 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000620
621#ifdef SIGHUP
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000622 x = PyInt_FromLong(SIGHUP);
623 PyDict_SetItemString(d, "SIGHUP", x);
624 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000625#endif
626#ifdef SIGINT
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000627 x = PyInt_FromLong(SIGINT);
628 PyDict_SetItemString(d, "SIGINT", x);
629 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000630#endif
Tim Peters1ce3cf72001-10-01 17:58:40 +0000631#ifdef SIGBREAK
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000632 x = PyInt_FromLong(SIGBREAK);
633 PyDict_SetItemString(d, "SIGBREAK", x);
634 Py_XDECREF(x);
Tim Peters1ce3cf72001-10-01 17:58:40 +0000635#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000636#ifdef SIGQUIT
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000637 x = PyInt_FromLong(SIGQUIT);
638 PyDict_SetItemString(d, "SIGQUIT", x);
639 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000640#endif
641#ifdef SIGILL
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000642 x = PyInt_FromLong(SIGILL);
643 PyDict_SetItemString(d, "SIGILL", x);
644 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000645#endif
646#ifdef SIGTRAP
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000647 x = PyInt_FromLong(SIGTRAP);
648 PyDict_SetItemString(d, "SIGTRAP", x);
649 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000650#endif
651#ifdef SIGIOT
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000652 x = PyInt_FromLong(SIGIOT);
653 PyDict_SetItemString(d, "SIGIOT", x);
654 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000655#endif
656#ifdef SIGABRT
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000657 x = PyInt_FromLong(SIGABRT);
658 PyDict_SetItemString(d, "SIGABRT", x);
659 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000660#endif
661#ifdef SIGEMT
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000662 x = PyInt_FromLong(SIGEMT);
663 PyDict_SetItemString(d, "SIGEMT", x);
664 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000665#endif
666#ifdef SIGFPE
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000667 x = PyInt_FromLong(SIGFPE);
668 PyDict_SetItemString(d, "SIGFPE", x);
669 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000670#endif
671#ifdef SIGKILL
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000672 x = PyInt_FromLong(SIGKILL);
673 PyDict_SetItemString(d, "SIGKILL", x);
674 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000675#endif
676#ifdef SIGBUS
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000677 x = PyInt_FromLong(SIGBUS);
678 PyDict_SetItemString(d, "SIGBUS", x);
679 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000680#endif
681#ifdef SIGSEGV
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000682 x = PyInt_FromLong(SIGSEGV);
683 PyDict_SetItemString(d, "SIGSEGV", x);
684 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000685#endif
686#ifdef SIGSYS
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000687 x = PyInt_FromLong(SIGSYS);
688 PyDict_SetItemString(d, "SIGSYS", x);
689 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000690#endif
691#ifdef SIGPIPE
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000692 x = PyInt_FromLong(SIGPIPE);
693 PyDict_SetItemString(d, "SIGPIPE", x);
694 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000695#endif
696#ifdef SIGALRM
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000697 x = PyInt_FromLong(SIGALRM);
698 PyDict_SetItemString(d, "SIGALRM", x);
699 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000700#endif
701#ifdef SIGTERM
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000702 x = PyInt_FromLong(SIGTERM);
703 PyDict_SetItemString(d, "SIGTERM", x);
704 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000705#endif
706#ifdef SIGUSR1
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000707 x = PyInt_FromLong(SIGUSR1);
708 PyDict_SetItemString(d, "SIGUSR1", x);
709 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000710#endif
711#ifdef SIGUSR2
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000712 x = PyInt_FromLong(SIGUSR2);
713 PyDict_SetItemString(d, "SIGUSR2", x);
714 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000715#endif
716#ifdef SIGCLD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000717 x = PyInt_FromLong(SIGCLD);
718 PyDict_SetItemString(d, "SIGCLD", x);
719 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000720#endif
721#ifdef SIGCHLD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000722 x = PyInt_FromLong(SIGCHLD);
723 PyDict_SetItemString(d, "SIGCHLD", x);
724 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000725#endif
726#ifdef SIGPWR
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000727 x = PyInt_FromLong(SIGPWR);
728 PyDict_SetItemString(d, "SIGPWR", x);
729 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000730#endif
731#ifdef SIGIO
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000732 x = PyInt_FromLong(SIGIO);
733 PyDict_SetItemString(d, "SIGIO", x);
734 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000735#endif
736#ifdef SIGURG
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000737 x = PyInt_FromLong(SIGURG);
738 PyDict_SetItemString(d, "SIGURG", x);
739 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000740#endif
741#ifdef SIGWINCH
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000742 x = PyInt_FromLong(SIGWINCH);
743 PyDict_SetItemString(d, "SIGWINCH", x);
744 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000745#endif
746#ifdef SIGPOLL
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000747 x = PyInt_FromLong(SIGPOLL);
748 PyDict_SetItemString(d, "SIGPOLL", x);
749 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000750#endif
751#ifdef SIGSTOP
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000752 x = PyInt_FromLong(SIGSTOP);
753 PyDict_SetItemString(d, "SIGSTOP", x);
754 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000755#endif
756#ifdef SIGTSTP
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000757 x = PyInt_FromLong(SIGTSTP);
758 PyDict_SetItemString(d, "SIGTSTP", x);
759 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000760#endif
761#ifdef SIGCONT
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000762 x = PyInt_FromLong(SIGCONT);
763 PyDict_SetItemString(d, "SIGCONT", x);
764 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000765#endif
766#ifdef SIGTTIN
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000767 x = PyInt_FromLong(SIGTTIN);
768 PyDict_SetItemString(d, "SIGTTIN", x);
769 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000770#endif
771#ifdef SIGTTOU
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000772 x = PyInt_FromLong(SIGTTOU);
773 PyDict_SetItemString(d, "SIGTTOU", x);
774 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000775#endif
776#ifdef SIGVTALRM
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000777 x = PyInt_FromLong(SIGVTALRM);
778 PyDict_SetItemString(d, "SIGVTALRM", x);
779 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000780#endif
781#ifdef SIGPROF
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000782 x = PyInt_FromLong(SIGPROF);
783 PyDict_SetItemString(d, "SIGPROF", x);
784 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000785#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000786#ifdef SIGXCPU
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000787 x = PyInt_FromLong(SIGXCPU);
788 PyDict_SetItemString(d, "SIGXCPU", x);
789 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000790#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000791#ifdef SIGXFSZ
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000792 x = PyInt_FromLong(SIGXFSZ);
793 PyDict_SetItemString(d, "SIGXFSZ", x);
794 Py_XDECREF(x);
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000795#endif
Anthony Baxterf37f37d2003-07-31 10:35:29 +0000796#ifdef SIGRTMIN
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000797 x = PyInt_FromLong(SIGRTMIN);
798 PyDict_SetItemString(d, "SIGRTMIN", x);
799 Py_XDECREF(x);
Anthony Baxterf37f37d2003-07-31 10:35:29 +0000800#endif
801#ifdef SIGRTMAX
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000802 x = PyInt_FromLong(SIGRTMAX);
803 PyDict_SetItemString(d, "SIGRTMAX", x);
804 Py_XDECREF(x);
Anthony Baxterf37f37d2003-07-31 10:35:29 +0000805#endif
Martin v. Löwis175af252002-01-12 11:43:25 +0000806#ifdef SIGINFO
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000807 x = PyInt_FromLong(SIGINFO);
808 PyDict_SetItemString(d, "SIGINFO", x);
809 Py_XDECREF(x);
Martin v. Löwis175af252002-01-12 11:43:25 +0000810#endif
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000811
812#ifdef ITIMER_REAL
813 x = PyLong_FromLong(ITIMER_REAL);
814 PyDict_SetItemString(d, "ITIMER_REAL", x);
815 Py_DECREF(x);
816#endif
817#ifdef ITIMER_VIRTUAL
818 x = PyLong_FromLong(ITIMER_VIRTUAL);
819 PyDict_SetItemString(d, "ITIMER_VIRTUAL", x);
820 Py_DECREF(x);
821#endif
822#ifdef ITIMER_PROF
823 x = PyLong_FromLong(ITIMER_PROF);
824 PyDict_SetItemString(d, "ITIMER_PROF", x);
825 Py_DECREF(x);
826#endif
827
828#if defined (HAVE_SETITIMER) || defined (HAVE_GETITIMER)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000829 ItimerError = PyErr_NewException("signal.ItimerError",
830 PyExc_IOError, NULL);
Neal Norwitz18aa3882008-08-24 05:04:52 +0000831 if (ItimerError != NULL)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000832 PyDict_SetItemString(d, "ItimerError", ItimerError);
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000833#endif
834
Brian Curtine5aa8862010-04-02 23:26:06 +0000835#ifdef CTRL_C_EVENT
836 x = PyInt_FromLong(CTRL_C_EVENT);
837 PyDict_SetItemString(d, "CTRL_C_EVENT", x);
838 Py_DECREF(x);
839#endif
840
841#ifdef CTRL_BREAK_EVENT
842 x = PyInt_FromLong(CTRL_BREAK_EVENT);
843 PyDict_SetItemString(d, "CTRL_BREAK_EVENT", x);
844 Py_DECREF(x);
845#endif
846
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000847 if (!PyErr_Occurred())
Guido van Rossum08c16611997-08-02 03:01:42 +0000848 return;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000849
850 /* Check for errors */
851 finally:
852 return;
Guido van Rossum08c16611997-08-02 03:01:42 +0000853}
854
855static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000856finisignal(void)
Guido van Rossum08c16611997-08-02 03:01:42 +0000857{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000858 int i;
859 PyObject *func;
Guido van Rossum08c16611997-08-02 03:01:42 +0000860
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000861 PyOS_setsig(SIGINT, old_siginthandler);
862 old_siginthandler = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +0000863
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000864 for (i = 1; i < NSIG; i++) {
865 func = Handlers[i].func;
866 Handlers[i].tripped = 0;
867 Handlers[i].func = NULL;
868 if (i != SIGINT && func != NULL && func != Py_None &&
869 func != DefaultHandler && func != IgnoreHandler)
870 PyOS_setsig(i, SIG_DFL);
871 Py_XDECREF(func);
872 }
Guido van Rossum08c16611997-08-02 03:01:42 +0000873
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000874 Py_XDECREF(IntHandler);
875 IntHandler = NULL;
876 Py_XDECREF(DefaultHandler);
877 DefaultHandler = NULL;
878 Py_XDECREF(IgnoreHandler);
879 IgnoreHandler = NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000880}
881
Barry Warsaw92971171997-01-03 00:14:25 +0000882
Barry Warsaw92971171997-01-03 00:14:25 +0000883/* Declared in pyerrors.h */
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000884int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000885PyErr_CheckSignals(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000886{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000887 int i;
888 PyObject *f;
Barry Warsaw92971171997-01-03 00:14:25 +0000889
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000890 if (!is_tripped)
891 return 0;
Guido van Rossum137c49c2007-12-10 23:00:12 +0000892
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000893#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000894 if (PyThread_get_thread_ident() != main_thread)
895 return 0;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000896#endif
Guido van Rossum137c49c2007-12-10 23:00:12 +0000897
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000898 /*
899 * The is_tripped variable is meant to speed up the calls to
900 * PyErr_CheckSignals (both directly or via pending calls) when no
901 * signal has arrived. This variable is set to 1 when a signal arrives
902 * and it is set to 0 here, when we know some signals arrived. This way
903 * we can run the registered handlers with no signals blocked.
904 *
905 * NOTE: with this approach we can have a situation where is_tripped is
906 * 1 but we have no more signals to handle (Handlers[i].tripped
907 * is 0 for every signal i). This won't do us any harm (except
908 * we're gonna spent some cycles for nothing). This happens when
909 * we receive a signal i after we zero is_tripped and before we
910 * check Handlers[i].tripped.
911 */
912 is_tripped = 0;
Guido van Rossum137c49c2007-12-10 23:00:12 +0000913
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000914 if (!(f = (PyObject *)PyEval_GetFrame()))
915 f = Py_None;
Guido van Rossum137c49c2007-12-10 23:00:12 +0000916
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000917 for (i = 1; i < NSIG; i++) {
918 if (Handlers[i].tripped) {
919 PyObject *result = NULL;
920 PyObject *arglist = Py_BuildValue("(iO)", i, f);
921 Handlers[i].tripped = 0;
Barry Warsaw92971171997-01-03 00:14:25 +0000922
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000923 if (arglist) {
924 result = PyEval_CallObject(Handlers[i].func,
925 arglist);
926 Py_DECREF(arglist);
927 }
928 if (!result)
929 return -1;
Barry Warsaw92971171997-01-03 00:14:25 +0000930
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000931 Py_DECREF(result);
932 }
933 }
Guido van Rossum137c49c2007-12-10 23:00:12 +0000934
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000935 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000936}
937
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000938
Barry Warsaw92971171997-01-03 00:14:25 +0000939/* Replacements for intrcheck.c functionality
940 * Declared in pyerrors.h
941 */
942void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000943PyErr_SetInterrupt(void)
Barry Warsaw92971171997-01-03 00:14:25 +0000944{
Victor Stinner33feeab2011-04-18 16:33:28 +0200945 trip_signal(SIGINT);
Barry Warsaw92971171997-01-03 00:14:25 +0000946}
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000947
948void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000949PyOS_InitInterrupts(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000950{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000951 initsignal();
952 _PyImport_FixupExtension("signal", "signal");
Guido van Rossum08c16611997-08-02 03:01:42 +0000953}
954
955void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000956PyOS_FiniInterrupts(void)
Guido van Rossum08c16611997-08-02 03:01:42 +0000957{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000958 finisignal();
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000959}
960
961int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000962PyOS_InterruptOccurred(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000963{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000964 if (Handlers[SIGINT].tripped) {
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000965#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000966 if (PyThread_get_thread_ident() != main_thread)
967 return 0;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000968#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000969 Handlers[SIGINT].tripped = 0;
970 return 1;
971 }
972 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000973}
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000974
975void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000976PyOS_AfterFork(void)
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000977{
978#ifdef WITH_THREAD
Charles-François Natalie0e88b02012-02-02 19:57:19 +0100979 /* PyThread_ReInitTLS() must be called early, to make sure that the TLS API
980 * can be called safely. */
981 PyThread_ReInitTLS();
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000982 PyEval_ReInitThreads();
983 main_thread = PyThread_get_thread_ident();
984 main_pid = getpid();
985 _PyImport_ReInitLock();
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000986#endif
987}