blob: b49e13c9368e404bf07fa0964a57ff4df55fc7e1 [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
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000170signal_handler(int sig_num)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000171{
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000172#ifdef WITH_THREAD
Guido van Rossum9e8181b2000-09-19 00:46:46 +0000173#ifdef WITH_PTH
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000174 if (PyThread_get_thread_ident() != main_thread) {
175 pth_raise(*(pth_t *) main_thread, sig_num);
176 return;
177 }
Guido van Rossum9e8181b2000-09-19 00:46:46 +0000178#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000179 /* See NOTES section above */
180 if (getpid() == main_pid) {
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000181#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000182 Handlers[sig_num].tripped = 1;
183 /* Set is_tripped after setting .tripped, as it gets
184 cleared in PyErr_CheckSignals() before .tripped. */
185 is_tripped = 1;
186 Py_AddPendingCall(checksignals_witharg, NULL);
187 if (wakeup_fd != -1)
188 write(wakeup_fd, "\0", 1);
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000189#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000190 }
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000191#endif
Guido van Rossum602099a1994-09-14 13:32:22 +0000192#ifdef SIGCHLD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000193 if (sig_num == SIGCHLD) {
194 /* To avoid infinite recursion, this signal remains
195 reset until explicit re-instated.
196 Don't clear the 'func' field as it is our pointer
197 to the Python handler... */
198 return;
199 }
Guido van Rossum602099a1994-09-14 13:32:22 +0000200#endif
Jean-Paul Calderonee54ddf12010-05-08 20:06:02 +0000201#ifndef HAVE_SIGACTION
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000202 /* If the handler was not set up with sigaction, reinstall it. See
203 * Python/pythonrun.c for the implementation of PyOS_setsig which
204 * makes this true. See also issue8354. */
205 PyOS_setsig(sig_num, signal_handler);
Jean-Paul Calderonee54ddf12010-05-08 20:06:02 +0000206#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000207}
Guido van Rossume4485b01994-09-07 14:32:49 +0000208
Guido van Rossum06d511d1995-03-10 15:13:48 +0000209
Guido van Rossum1171ee61997-08-22 20:42:00 +0000210#ifdef HAVE_ALARM
Guido van Rossume4485b01994-09-07 14:32:49 +0000211static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000212signal_alarm(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000213{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000214 int t;
215 if (!PyArg_ParseTuple(args, "i:alarm", &t))
216 return NULL;
217 /* alarm() returns the number of seconds remaining */
218 return PyInt_FromLong((long)alarm(t));
Guido van Rossumaa0f4c71994-08-23 13:49:37 +0000219}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000220
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000221PyDoc_STRVAR(alarm_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000222"alarm(seconds)\n\
223\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000224Arrange for SIGALRM to arrive after the given number of seconds.");
Guido van Rossum06d511d1995-03-10 15:13:48 +0000225#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000226
Guido van Rossum1171ee61997-08-22 20:42:00 +0000227#ifdef HAVE_PAUSE
Guido van Rossuma597dde1995-01-10 20:56:29 +0000228static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000229signal_pause(PyObject *self)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000230{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000231 Py_BEGIN_ALLOW_THREADS
232 (void)pause();
233 Py_END_ALLOW_THREADS
234 /* make sure that any exceptions that got raised are propagated
235 * back into Python
236 */
237 if (PyErr_CheckSignals())
238 return NULL;
Barry Warsaw92971171997-01-03 00:14:25 +0000239
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000240 Py_INCREF(Py_None);
241 return Py_None;
Guido van Rossume4485b01994-09-07 14:32:49 +0000242}
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000243PyDoc_STRVAR(pause_doc,
Barry Warsaw1ee36ff1998-07-21 22:41:18 +0000244"pause()\n\
245\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000246Wait until a signal arrives.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000247
Guido van Rossum06d511d1995-03-10 15:13:48 +0000248#endif
Guido van Rossume4485b01994-09-07 14:32:49 +0000249
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000250
Guido van Rossume4485b01994-09-07 14:32:49 +0000251static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000252signal_signal(PyObject *self, PyObject *args)
Guido van Rossume4485b01994-09-07 14:32:49 +0000253{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000254 PyObject *obj;
255 int sig_num;
256 PyObject *old_handler;
257 void (*func)(int);
258 if (!PyArg_ParseTuple(args, "iO:signal", &sig_num, &obj))
259 return NULL;
Brian Curtin24af0e92010-08-06 19:41:01 +0000260#ifdef MS_WINDOWS
261 /* Validate that sig_num is one of the allowable signals */
Brian Curtin84263332010-09-06 16:17:50 +0000262 switch (sig_num) {
263 case SIGABRT: break;
Brian Curtin1390dd72010-10-01 16:44:03 +0000264#ifdef SIGBREAK
265 /* Issue #10003: SIGBREAK is not documented as permitted, but works
266 and corresponds to CTRL_BREAK_EVENT. */
267 case SIGBREAK: break;
268#endif
Brian Curtin84263332010-09-06 16:17:50 +0000269 case SIGFPE: break;
270 case SIGILL: break;
271 case SIGINT: break;
272 case SIGSEGV: break;
273 case SIGTERM: break;
274 default:
275 PyErr_SetString(PyExc_ValueError, "invalid signal value");
276 return NULL;
Brian Curtin24af0e92010-08-06 19:41:01 +0000277 }
278#endif
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000279#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000280 if (PyThread_get_thread_ident() != main_thread) {
281 PyErr_SetString(PyExc_ValueError,
282 "signal only works in main thread");
283 return NULL;
284 }
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000285#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000286 if (sig_num < 1 || sig_num >= NSIG) {
287 PyErr_SetString(PyExc_ValueError,
288 "signal number out of range");
289 return NULL;
290 }
291 if (obj == IgnoreHandler)
292 func = SIG_IGN;
293 else if (obj == DefaultHandler)
294 func = SIG_DFL;
295 else if (!PyCallable_Check(obj)) {
296 PyErr_SetString(PyExc_TypeError,
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000297"signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object");
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000298 return NULL;
299 }
300 else
301 func = signal_handler;
302 if (PyOS_setsig(sig_num, func) == SIG_ERR) {
303 PyErr_SetFromErrno(PyExc_RuntimeError);
304 return NULL;
305 }
306 old_handler = Handlers[sig_num].func;
307 Handlers[sig_num].tripped = 0;
308 Py_INCREF(obj);
309 Handlers[sig_num].func = obj;
310 return old_handler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000311}
312
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000313PyDoc_STRVAR(signal_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000314"signal(sig, action) -> action\n\
315\n\
316Set the action for the given signal. The action can be SIG_DFL,\n\
317SIG_IGN, or a callable Python object. The previous action is\n\
318returned. See getsignal() for possible return values.\n\
319\n\
320*** IMPORTANT NOTICE ***\n\
321A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000322the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000323
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000324
Guido van Rossume4485b01994-09-07 14:32:49 +0000325static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000326signal_getsignal(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000327{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000328 int sig_num;
329 PyObject *old_handler;
330 if (!PyArg_ParseTuple(args, "i:getsignal", &sig_num))
331 return NULL;
332 if (sig_num < 1 || sig_num >= NSIG) {
333 PyErr_SetString(PyExc_ValueError,
334 "signal number out of range");
335 return NULL;
336 }
337 old_handler = Handlers[sig_num].func;
338 Py_INCREF(old_handler);
339 return old_handler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000340}
341
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000342PyDoc_STRVAR(getsignal_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000343"getsignal(sig) -> action\n\
344\n\
345Return the current action for the given signal. The return value can be:\n\
346SIG_IGN -- if the signal is being ignored\n\
347SIG_DFL -- if the default action for the signal is in effect\n\
348None -- if an unknown handler is in effect\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000349anything else -- the callable Python object used as a handler");
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000350
Facundo Batista7e251e82008-02-23 15:07:35 +0000351#ifdef HAVE_SIGINTERRUPT
352PyDoc_STRVAR(siginterrupt_doc,
353"siginterrupt(sig, flag) -> None\n\
354change system call restart behaviour: if flag is False, system calls\n\
355will be restarted when interrupted by signal sig, else system calls\n\
356will be interrupted.");
357
358static PyObject *
359signal_siginterrupt(PyObject *self, PyObject *args)
360{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000361 int sig_num;
362 int flag;
Facundo Batista7e251e82008-02-23 15:07:35 +0000363
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000364 if (!PyArg_ParseTuple(args, "ii:siginterrupt", &sig_num, &flag))
365 return NULL;
366 if (sig_num < 1 || sig_num >= NSIG) {
367 PyErr_SetString(PyExc_ValueError,
368 "signal number out of range");
369 return NULL;
370 }
371 if (siginterrupt(sig_num, flag)<0) {
372 PyErr_SetFromErrno(PyExc_RuntimeError);
373 return NULL;
374 }
Facundo Batista7e251e82008-02-23 15:07:35 +0000375
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000376 Py_INCREF(Py_None);
377 return Py_None;
Facundo Batista7e251e82008-02-23 15:07:35 +0000378}
379
380#endif
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000381
Guido van Rossum02de8972007-12-19 19:41:06 +0000382static PyObject *
383signal_set_wakeup_fd(PyObject *self, PyObject *args)
384{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000385 struct stat buf;
386 int fd, old_fd;
387 if (!PyArg_ParseTuple(args, "i:set_wakeup_fd", &fd))
388 return NULL;
Guido van Rossum02de8972007-12-19 19:41:06 +0000389#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000390 if (PyThread_get_thread_ident() != main_thread) {
391 PyErr_SetString(PyExc_ValueError,
392 "set_wakeup_fd only works in main thread");
393 return NULL;
394 }
Guido van Rossum02de8972007-12-19 19:41:06 +0000395#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000396 if (fd != -1 && fstat(fd, &buf) != 0) {
397 PyErr_SetString(PyExc_ValueError, "invalid fd");
398 return NULL;
399 }
400 old_fd = wakeup_fd;
401 wakeup_fd = fd;
402 return PyLong_FromLong(old_fd);
Guido van Rossum02de8972007-12-19 19:41:06 +0000403}
404
405PyDoc_STRVAR(set_wakeup_fd_doc,
406"set_wakeup_fd(fd) -> fd\n\
407\n\
408Sets the fd to be written to (with '\\0') when a signal\n\
409comes in. A library can use this to wakeup select or poll.\n\
410The previous fd is returned.\n\
411\n\
412The fd must be non-blocking.");
413
414/* C API for the same, without all the error checking */
415int
416PySignal_SetWakeupFd(int fd)
417{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000418 int old_fd = wakeup_fd;
419 if (fd < 0)
420 fd = -1;
421 wakeup_fd = fd;
422 return old_fd;
Guido van Rossum02de8972007-12-19 19:41:06 +0000423}
424
425
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000426#ifdef HAVE_SETITIMER
427static PyObject *
428signal_setitimer(PyObject *self, PyObject *args)
429{
430 double first;
431 double interval = 0;
432 int which;
433 struct itimerval new, old;
434
435 if(!PyArg_ParseTuple(args, "id|d:setitimer", &which, &first, &interval))
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000436 return NULL;
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000437
438 timeval_from_double(first, &new.it_value);
439 timeval_from_double(interval, &new.it_interval);
440 /* Let OS check "which" value */
441 if (setitimer(which, &new, &old) != 0) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000442 PyErr_SetFromErrno(ItimerError);
443 return NULL;
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000444 }
445
446 return itimer_retval(&old);
447}
448
449PyDoc_STRVAR(setitimer_doc,
450"setitimer(which, seconds[, interval])\n\
451\n\
452Sets given itimer (one of ITIMER_REAL, ITIMER_VIRTUAL\n\
453or ITIMER_PROF) to fire after value seconds and after\n\
454that every interval seconds.\n\
455The itimer can be cleared by setting seconds to zero.\n\
456\n\
457Returns old values as a tuple: (delay, interval).");
458#endif
459
460
461#ifdef HAVE_GETITIMER
462static PyObject *
463signal_getitimer(PyObject *self, PyObject *args)
464{
465 int which;
466 struct itimerval old;
467
468 if (!PyArg_ParseTuple(args, "i:getitimer", &which))
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000469 return NULL;
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000470
471 if (getitimer(which, &old) != 0) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000472 PyErr_SetFromErrno(ItimerError);
473 return NULL;
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000474 }
475
476 return itimer_retval(&old);
477}
478
479PyDoc_STRVAR(getitimer_doc,
480"getitimer(which)\n\
481\n\
482Returns current value of given itimer.");
483#endif
484
485
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000486/* List of functions defined in the module */
Barry Warsaw92971171997-01-03 00:14:25 +0000487static PyMethodDef signal_methods[] = {
Guido van Rossum1171ee61997-08-22 20:42:00 +0000488#ifdef HAVE_ALARM
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000489 {"alarm", signal_alarm, METH_VARARGS, alarm_doc},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000490#endif
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000491#ifdef HAVE_SETITIMER
492 {"setitimer", signal_setitimer, METH_VARARGS, setitimer_doc},
493#endif
494#ifdef HAVE_GETITIMER
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000495 {"getitimer", signal_getitimer, METH_VARARGS, getitimer_doc},
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000496#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000497 {"signal", signal_signal, METH_VARARGS, signal_doc},
498 {"getsignal", signal_getsignal, METH_VARARGS, getsignal_doc},
499 {"set_wakeup_fd", signal_set_wakeup_fd, METH_VARARGS, set_wakeup_fd_doc},
Facundo Batista7e251e82008-02-23 15:07:35 +0000500#ifdef HAVE_SIGINTERRUPT
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000501 {"siginterrupt", signal_siginterrupt, METH_VARARGS, siginterrupt_doc},
Facundo Batista7e251e82008-02-23 15:07:35 +0000502#endif
Guido van Rossum1171ee61997-08-22 20:42:00 +0000503#ifdef HAVE_PAUSE
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000504 {"pause", (PyCFunction)signal_pause,
505 METH_NOARGS,pause_doc},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000506#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000507 {"default_int_handler", signal_default_int_handler,
508 METH_VARARGS, default_int_handler_doc},
509 {NULL, NULL} /* sentinel */
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000510};
511
Barry Warsaw92971171997-01-03 00:14:25 +0000512
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000513PyDoc_STRVAR(module_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000514"This module provides mechanisms to use signal handlers in Python.\n\
515\n\
516Functions:\n\
517\n\
518alarm() -- cause SIGALRM after a specified time [Unix only]\n\
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000519setitimer() -- cause a signal (described below) after a specified\n\
520 float time and the timer may restart then [Unix only]\n\
521getitimer() -- get current value of timer [Unix only]\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000522signal() -- set the action for a given signal\n\
523getsignal() -- get the signal action for a given signal\n\
524pause() -- wait until a signal arrives [Unix only]\n\
525default_int_handler() -- default SIGINT handler\n\
526\n\
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000527signal constants:\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000528SIG_DFL -- used to refer to the system default handler\n\
529SIG_IGN -- used to ignore the signal\n\
530NSIG -- number of defined signals\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000531SIGINT, SIGTERM, etc. -- signal numbers\n\
532\n\
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000533itimer constants:\n\
534ITIMER_REAL -- decrements in real time, and delivers SIGALRM upon\n\
535 expiration\n\
536ITIMER_VIRTUAL -- decrements only when the process is executing,\n\
537 and delivers SIGVTALRM upon expiration\n\
538ITIMER_PROF -- decrements both when the process is executing and\n\
539 when the system is executing on behalf of the process.\n\
540 Coupled with ITIMER_VIRTUAL, this timer is usually\n\
541 used to profile the time spent by the application\n\
542 in user and kernel space. SIGPROF is delivered upon\n\
543 expiration.\n\
544\n\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000545*** IMPORTANT NOTICE ***\n\
546A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000547the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000548
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000549PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000550initsignal(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000551{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000552 PyObject *m, *d, *x;
553 int i;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000554
555#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000556 main_thread = PyThread_get_thread_ident();
557 main_pid = getpid();
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000558#endif
559
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000560 /* Create the module and add the functions */
561 m = Py_InitModule3("signal", signal_methods, module_doc);
562 if (m == NULL)
563 return;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000564
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000565 /* Add some symbolic constants to the module */
566 d = PyModule_GetDict(m);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000567
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000568 x = DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL);
569 if (!x || PyDict_SetItemString(d, "SIG_DFL", x) < 0)
570 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000571
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000572 x = IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN);
573 if (!x || PyDict_SetItemString(d, "SIG_IGN", x) < 0)
574 goto finally;
Barry Warsaw92971171997-01-03 00:14:25 +0000575
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000576 x = PyInt_FromLong((long)NSIG);
577 if (!x || PyDict_SetItemString(d, "NSIG", x) < 0)
578 goto finally;
579 Py_DECREF(x);
Barry Warsaw92971171997-01-03 00:14:25 +0000580
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000581 x = IntHandler = PyDict_GetItemString(d, "default_int_handler");
582 if (!x)
583 goto finally;
584 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +0000585
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000586 Handlers[0].tripped = 0;
587 for (i = 1; i < NSIG; i++) {
588 void (*t)(int);
589 t = PyOS_getsig(i);
590 Handlers[i].tripped = 0;
591 if (t == SIG_DFL)
592 Handlers[i].func = DefaultHandler;
593 else if (t == SIG_IGN)
594 Handlers[i].func = IgnoreHandler;
595 else
596 Handlers[i].func = Py_None; /* None of our business */
597 Py_INCREF(Handlers[i].func);
598 }
599 if (Handlers[SIGINT].func == DefaultHandler) {
600 /* Install default int handler */
601 Py_INCREF(IntHandler);
602 Py_DECREF(Handlers[SIGINT].func);
603 Handlers[SIGINT].func = IntHandler;
604 old_siginthandler = PyOS_setsig(SIGINT, signal_handler);
605 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000606
607#ifdef SIGHUP
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000608 x = PyInt_FromLong(SIGHUP);
609 PyDict_SetItemString(d, "SIGHUP", x);
610 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000611#endif
612#ifdef SIGINT
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000613 x = PyInt_FromLong(SIGINT);
614 PyDict_SetItemString(d, "SIGINT", x);
615 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000616#endif
Tim Peters1ce3cf72001-10-01 17:58:40 +0000617#ifdef SIGBREAK
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000618 x = PyInt_FromLong(SIGBREAK);
619 PyDict_SetItemString(d, "SIGBREAK", x);
620 Py_XDECREF(x);
Tim Peters1ce3cf72001-10-01 17:58:40 +0000621#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000622#ifdef SIGQUIT
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000623 x = PyInt_FromLong(SIGQUIT);
624 PyDict_SetItemString(d, "SIGQUIT", x);
625 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000626#endif
627#ifdef SIGILL
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000628 x = PyInt_FromLong(SIGILL);
629 PyDict_SetItemString(d, "SIGILL", x);
630 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000631#endif
632#ifdef SIGTRAP
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000633 x = PyInt_FromLong(SIGTRAP);
634 PyDict_SetItemString(d, "SIGTRAP", x);
635 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000636#endif
637#ifdef SIGIOT
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000638 x = PyInt_FromLong(SIGIOT);
639 PyDict_SetItemString(d, "SIGIOT", x);
640 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000641#endif
642#ifdef SIGABRT
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000643 x = PyInt_FromLong(SIGABRT);
644 PyDict_SetItemString(d, "SIGABRT", x);
645 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000646#endif
647#ifdef SIGEMT
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000648 x = PyInt_FromLong(SIGEMT);
649 PyDict_SetItemString(d, "SIGEMT", x);
650 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000651#endif
652#ifdef SIGFPE
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000653 x = PyInt_FromLong(SIGFPE);
654 PyDict_SetItemString(d, "SIGFPE", x);
655 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000656#endif
657#ifdef SIGKILL
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000658 x = PyInt_FromLong(SIGKILL);
659 PyDict_SetItemString(d, "SIGKILL", x);
660 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000661#endif
662#ifdef SIGBUS
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000663 x = PyInt_FromLong(SIGBUS);
664 PyDict_SetItemString(d, "SIGBUS", x);
665 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000666#endif
667#ifdef SIGSEGV
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000668 x = PyInt_FromLong(SIGSEGV);
669 PyDict_SetItemString(d, "SIGSEGV", x);
670 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000671#endif
672#ifdef SIGSYS
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000673 x = PyInt_FromLong(SIGSYS);
674 PyDict_SetItemString(d, "SIGSYS", x);
675 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000676#endif
677#ifdef SIGPIPE
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000678 x = PyInt_FromLong(SIGPIPE);
679 PyDict_SetItemString(d, "SIGPIPE", x);
680 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000681#endif
682#ifdef SIGALRM
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000683 x = PyInt_FromLong(SIGALRM);
684 PyDict_SetItemString(d, "SIGALRM", x);
685 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000686#endif
687#ifdef SIGTERM
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000688 x = PyInt_FromLong(SIGTERM);
689 PyDict_SetItemString(d, "SIGTERM", x);
690 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000691#endif
692#ifdef SIGUSR1
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000693 x = PyInt_FromLong(SIGUSR1);
694 PyDict_SetItemString(d, "SIGUSR1", x);
695 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000696#endif
697#ifdef SIGUSR2
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000698 x = PyInt_FromLong(SIGUSR2);
699 PyDict_SetItemString(d, "SIGUSR2", x);
700 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000701#endif
702#ifdef SIGCLD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000703 x = PyInt_FromLong(SIGCLD);
704 PyDict_SetItemString(d, "SIGCLD", x);
705 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000706#endif
707#ifdef SIGCHLD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000708 x = PyInt_FromLong(SIGCHLD);
709 PyDict_SetItemString(d, "SIGCHLD", x);
710 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000711#endif
712#ifdef SIGPWR
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000713 x = PyInt_FromLong(SIGPWR);
714 PyDict_SetItemString(d, "SIGPWR", x);
715 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000716#endif
717#ifdef SIGIO
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000718 x = PyInt_FromLong(SIGIO);
719 PyDict_SetItemString(d, "SIGIO", x);
720 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000721#endif
722#ifdef SIGURG
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000723 x = PyInt_FromLong(SIGURG);
724 PyDict_SetItemString(d, "SIGURG", x);
725 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000726#endif
727#ifdef SIGWINCH
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000728 x = PyInt_FromLong(SIGWINCH);
729 PyDict_SetItemString(d, "SIGWINCH", x);
730 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000731#endif
732#ifdef SIGPOLL
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000733 x = PyInt_FromLong(SIGPOLL);
734 PyDict_SetItemString(d, "SIGPOLL", x);
735 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000736#endif
737#ifdef SIGSTOP
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000738 x = PyInt_FromLong(SIGSTOP);
739 PyDict_SetItemString(d, "SIGSTOP", x);
740 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000741#endif
742#ifdef SIGTSTP
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000743 x = PyInt_FromLong(SIGTSTP);
744 PyDict_SetItemString(d, "SIGTSTP", x);
745 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000746#endif
747#ifdef SIGCONT
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000748 x = PyInt_FromLong(SIGCONT);
749 PyDict_SetItemString(d, "SIGCONT", x);
750 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000751#endif
752#ifdef SIGTTIN
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000753 x = PyInt_FromLong(SIGTTIN);
754 PyDict_SetItemString(d, "SIGTTIN", x);
755 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000756#endif
757#ifdef SIGTTOU
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000758 x = PyInt_FromLong(SIGTTOU);
759 PyDict_SetItemString(d, "SIGTTOU", x);
760 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000761#endif
762#ifdef SIGVTALRM
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000763 x = PyInt_FromLong(SIGVTALRM);
764 PyDict_SetItemString(d, "SIGVTALRM", x);
765 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000766#endif
767#ifdef SIGPROF
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000768 x = PyInt_FromLong(SIGPROF);
769 PyDict_SetItemString(d, "SIGPROF", x);
770 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000771#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000772#ifdef SIGXCPU
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000773 x = PyInt_FromLong(SIGXCPU);
774 PyDict_SetItemString(d, "SIGXCPU", x);
775 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000776#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000777#ifdef SIGXFSZ
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000778 x = PyInt_FromLong(SIGXFSZ);
779 PyDict_SetItemString(d, "SIGXFSZ", x);
780 Py_XDECREF(x);
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000781#endif
Anthony Baxterf37f37d2003-07-31 10:35:29 +0000782#ifdef SIGRTMIN
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000783 x = PyInt_FromLong(SIGRTMIN);
784 PyDict_SetItemString(d, "SIGRTMIN", x);
785 Py_XDECREF(x);
Anthony Baxterf37f37d2003-07-31 10:35:29 +0000786#endif
787#ifdef SIGRTMAX
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000788 x = PyInt_FromLong(SIGRTMAX);
789 PyDict_SetItemString(d, "SIGRTMAX", x);
790 Py_XDECREF(x);
Anthony Baxterf37f37d2003-07-31 10:35:29 +0000791#endif
Martin v. Löwis175af252002-01-12 11:43:25 +0000792#ifdef SIGINFO
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000793 x = PyInt_FromLong(SIGINFO);
794 PyDict_SetItemString(d, "SIGINFO", x);
795 Py_XDECREF(x);
Martin v. Löwis175af252002-01-12 11:43:25 +0000796#endif
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000797
798#ifdef ITIMER_REAL
799 x = PyLong_FromLong(ITIMER_REAL);
800 PyDict_SetItemString(d, "ITIMER_REAL", x);
801 Py_DECREF(x);
802#endif
803#ifdef ITIMER_VIRTUAL
804 x = PyLong_FromLong(ITIMER_VIRTUAL);
805 PyDict_SetItemString(d, "ITIMER_VIRTUAL", x);
806 Py_DECREF(x);
807#endif
808#ifdef ITIMER_PROF
809 x = PyLong_FromLong(ITIMER_PROF);
810 PyDict_SetItemString(d, "ITIMER_PROF", x);
811 Py_DECREF(x);
812#endif
813
814#if defined (HAVE_SETITIMER) || defined (HAVE_GETITIMER)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000815 ItimerError = PyErr_NewException("signal.ItimerError",
816 PyExc_IOError, NULL);
Neal Norwitz18aa3882008-08-24 05:04:52 +0000817 if (ItimerError != NULL)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000818 PyDict_SetItemString(d, "ItimerError", ItimerError);
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000819#endif
820
Brian Curtine5aa8862010-04-02 23:26:06 +0000821#ifdef CTRL_C_EVENT
822 x = PyInt_FromLong(CTRL_C_EVENT);
823 PyDict_SetItemString(d, "CTRL_C_EVENT", x);
824 Py_DECREF(x);
825#endif
826
827#ifdef CTRL_BREAK_EVENT
828 x = PyInt_FromLong(CTRL_BREAK_EVENT);
829 PyDict_SetItemString(d, "CTRL_BREAK_EVENT", x);
830 Py_DECREF(x);
831#endif
832
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000833 if (!PyErr_Occurred())
Guido van Rossum08c16611997-08-02 03:01:42 +0000834 return;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000835
836 /* Check for errors */
837 finally:
838 return;
Guido van Rossum08c16611997-08-02 03:01:42 +0000839}
840
841static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000842finisignal(void)
Guido van Rossum08c16611997-08-02 03:01:42 +0000843{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000844 int i;
845 PyObject *func;
Guido van Rossum08c16611997-08-02 03:01:42 +0000846
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000847 PyOS_setsig(SIGINT, old_siginthandler);
848 old_siginthandler = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +0000849
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000850 for (i = 1; i < NSIG; i++) {
851 func = Handlers[i].func;
852 Handlers[i].tripped = 0;
853 Handlers[i].func = NULL;
854 if (i != SIGINT && func != NULL && func != Py_None &&
855 func != DefaultHandler && func != IgnoreHandler)
856 PyOS_setsig(i, SIG_DFL);
857 Py_XDECREF(func);
858 }
Guido van Rossum08c16611997-08-02 03:01:42 +0000859
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000860 Py_XDECREF(IntHandler);
861 IntHandler = NULL;
862 Py_XDECREF(DefaultHandler);
863 DefaultHandler = NULL;
864 Py_XDECREF(IgnoreHandler);
865 IgnoreHandler = NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000866}
867
Barry Warsaw92971171997-01-03 00:14:25 +0000868
Barry Warsaw92971171997-01-03 00:14:25 +0000869/* Declared in pyerrors.h */
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000870int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000871PyErr_CheckSignals(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000872{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000873 int i;
874 PyObject *f;
Barry Warsaw92971171997-01-03 00:14:25 +0000875
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000876 if (!is_tripped)
877 return 0;
Guido van Rossum137c49c2007-12-10 23:00:12 +0000878
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000879#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000880 if (PyThread_get_thread_ident() != main_thread)
881 return 0;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000882#endif
Guido van Rossum137c49c2007-12-10 23:00:12 +0000883
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000884 /*
885 * The is_tripped variable is meant to speed up the calls to
886 * PyErr_CheckSignals (both directly or via pending calls) when no
887 * signal has arrived. This variable is set to 1 when a signal arrives
888 * and it is set to 0 here, when we know some signals arrived. This way
889 * we can run the registered handlers with no signals blocked.
890 *
891 * NOTE: with this approach we can have a situation where is_tripped is
892 * 1 but we have no more signals to handle (Handlers[i].tripped
893 * is 0 for every signal i). This won't do us any harm (except
894 * we're gonna spent some cycles for nothing). This happens when
895 * we receive a signal i after we zero is_tripped and before we
896 * check Handlers[i].tripped.
897 */
898 is_tripped = 0;
Guido van Rossum137c49c2007-12-10 23:00:12 +0000899
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000900 if (!(f = (PyObject *)PyEval_GetFrame()))
901 f = Py_None;
Guido van Rossum137c49c2007-12-10 23:00:12 +0000902
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000903 for (i = 1; i < NSIG; i++) {
904 if (Handlers[i].tripped) {
905 PyObject *result = NULL;
906 PyObject *arglist = Py_BuildValue("(iO)", i, f);
907 Handlers[i].tripped = 0;
Barry Warsaw92971171997-01-03 00:14:25 +0000908
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000909 if (arglist) {
910 result = PyEval_CallObject(Handlers[i].func,
911 arglist);
912 Py_DECREF(arglist);
913 }
914 if (!result)
915 return -1;
Barry Warsaw92971171997-01-03 00:14:25 +0000916
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000917 Py_DECREF(result);
918 }
919 }
Guido van Rossum137c49c2007-12-10 23:00:12 +0000920
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000921 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000922}
923
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000924
Barry Warsaw92971171997-01-03 00:14:25 +0000925/* Replacements for intrcheck.c functionality
926 * Declared in pyerrors.h
927 */
928void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000929PyErr_SetInterrupt(void)
Barry Warsaw92971171997-01-03 00:14:25 +0000930{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000931 is_tripped = 1;
932 Handlers[SIGINT].tripped = 1;
933 Py_AddPendingCall((int (*)(void *))PyErr_CheckSignals, NULL);
Barry Warsaw92971171997-01-03 00:14:25 +0000934}
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000935
936void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000937PyOS_InitInterrupts(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000938{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000939 initsignal();
940 _PyImport_FixupExtension("signal", "signal");
Guido van Rossum08c16611997-08-02 03:01:42 +0000941}
942
943void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000944PyOS_FiniInterrupts(void)
Guido van Rossum08c16611997-08-02 03:01:42 +0000945{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000946 finisignal();
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000947}
948
949int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000950PyOS_InterruptOccurred(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000951{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000952 if (Handlers[SIGINT].tripped) {
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000953#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000954 if (PyThread_get_thread_ident() != main_thread)
955 return 0;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000956#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000957 Handlers[SIGINT].tripped = 0;
958 return 1;
959 }
960 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000961}
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000962
963void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000964PyOS_AfterFork(void)
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000965{
966#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000967 PyEval_ReInitThreads();
968 main_thread = PyThread_get_thread_ident();
969 main_pid = getpid();
970 _PyImport_ReInitLock();
971 PyThread_ReInitTLS();
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000972#endif
973}