blob: f4fd96491f397207ca31e85fe26846bc7859dcec [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 Curtineb24d742010-04-12 17:16:38 +000010#include <Windows.h>
Benjamin Peterson2614cda2010-03-21 22:36:19 +000011#ifdef HAVE_PROCESS_H
Guido van Rossum644a12b1997-04-09 19:24:53 +000012#include <process.h>
13#endif
Benjamin Peterson2614cda2010-03-21 22:36:19 +000014#endif
Guido van Rossum644a12b1997-04-09 19:24:53 +000015
Benjamin Peterson2614cda2010-03-21 22:36:19 +000016#ifdef HAVE_SIGNAL_H
Guido van Rossum398d9fe1994-05-11 08:59:13 +000017#include <signal.h>
Benjamin Peterson2614cda2010-03-21 22:36:19 +000018#endif
19#ifdef HAVE_SYS_STAT_H
Christian Heimes5fb7c2a2007-12-24 08:52:31 +000020#include <sys/stat.h>
Benjamin Peterson2614cda2010-03-21 22:36:19 +000021#endif
Martin v. Löwis3bf3cc02008-03-24 14:05:07 +000022#ifdef HAVE_SYS_TIME_H
Martin v. Löwis823725e2008-03-24 13:39:54 +000023#include <sys/time.h>
Martin v. Löwis3bf3cc02008-03-24 14:05:07 +000024#endif
Christian Heimes5fb7c2a2007-12-24 08:52:31 +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 Pitrouf95a1b32010-05-09 15:52:27 +000037# define NSIG _NSIG /* For BSD/SysV */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000038# elif defined(_SIGMAX)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000039# define NSIG (_SIGMAX + 1) /* For QNX */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000040# elif defined(SIGMAX)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000041# define NSIG (SIGMAX + 1) /* For djgpp */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000042# else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +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
Christian Heimes5fb7c2a2007-12-24 08:52:31 +000089static sig_atomic_t wakeup_fd = -1;
90
Christian Heimesb76922a2007-12-11 01:06:40 +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öwis823725e2008-03-24 13:39:54 +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 Heimes1a8501c2008-10-02 19:56:01 +0000116Py_LOCAL_INLINE(double)
Martin v. Löwis823725e2008-03-24 13:39:54 +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 Pitrouf95a1b32010-05-09 15:52:27 +0000129 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000130
131 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_value)))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000132 Py_DECREF(r);
133 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000134 }
135
136 PyTuple_SET_ITEM(r, 0, v);
137
138 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_interval)))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000139 Py_DECREF(r);
140 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +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 Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +0000179 /* See NOTES section above */
180 if (getpid() == main_pid) {
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000181#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +0000190 }
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000191#endif
Guido van Rossum602099a1994-09-14 13:32:22 +0000192#ifdef SIGCHLD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +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 Calderone6f137ca2010-05-09 03:18:57 +0000201#ifndef HAVE_SIGACTION
Antoine Pitrouf95a1b32010-05-09 15:52:27 +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 Calderone6f137ca2010-05-09 03:18:57 +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 Pitrouf95a1b32010-05-09 15:52:27 +0000214 int t;
215 if (!PyArg_ParseTuple(args, "i:alarm", &t))
216 return NULL;
217 /* alarm() returns the number of seconds remaining */
218 return PyLong_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 Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +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;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000260#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000261 if (PyThread_get_thread_ident() != main_thread) {
262 PyErr_SetString(PyExc_ValueError,
263 "signal only works in main thread");
264 return NULL;
265 }
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000266#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000267 if (sig_num < 1 || sig_num >= NSIG) {
268 PyErr_SetString(PyExc_ValueError,
269 "signal number out of range");
270 return NULL;
271 }
272 if (obj == IgnoreHandler)
273 func = SIG_IGN;
274 else if (obj == DefaultHandler)
275 func = SIG_DFL;
276 else if (!PyCallable_Check(obj)) {
277 PyErr_SetString(PyExc_TypeError,
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000278"signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000279 return NULL;
280 }
281 else
282 func = signal_handler;
283 if (PyOS_setsig(sig_num, func) == SIG_ERR) {
284 PyErr_SetFromErrno(PyExc_RuntimeError);
285 return NULL;
286 }
287 old_handler = Handlers[sig_num].func;
288 Handlers[sig_num].tripped = 0;
289 Py_INCREF(obj);
290 Handlers[sig_num].func = obj;
291 return old_handler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000292}
293
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000294PyDoc_STRVAR(signal_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000295"signal(sig, action) -> action\n\
296\n\
297Set the action for the given signal. The action can be SIG_DFL,\n\
298SIG_IGN, or a callable Python object. The previous action is\n\
299returned. See getsignal() for possible return values.\n\
300\n\
301*** IMPORTANT NOTICE ***\n\
302A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000303the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000304
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000305
Guido van Rossume4485b01994-09-07 14:32:49 +0000306static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000307signal_getsignal(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000308{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000309 int sig_num;
310 PyObject *old_handler;
311 if (!PyArg_ParseTuple(args, "i:getsignal", &sig_num))
312 return NULL;
313 if (sig_num < 1 || sig_num >= NSIG) {
314 PyErr_SetString(PyExc_ValueError,
315 "signal number out of range");
316 return NULL;
317 }
318 old_handler = Handlers[sig_num].func;
319 Py_INCREF(old_handler);
320 return old_handler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000321}
322
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000323PyDoc_STRVAR(getsignal_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000324"getsignal(sig) -> action\n\
325\n\
326Return the current action for the given signal. The return value can be:\n\
327SIG_IGN -- if the signal is being ignored\n\
328SIG_DFL -- if the default action for the signal is in effect\n\
329None -- if an unknown handler is in effect\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000330anything else -- the callable Python object used as a handler");
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000331
Christian Heimes8640e742008-02-23 16:23:06 +0000332#ifdef HAVE_SIGINTERRUPT
333PyDoc_STRVAR(siginterrupt_doc,
334"siginterrupt(sig, flag) -> None\n\
335change system call restart behaviour: if flag is False, system calls\n\
336will be restarted when interrupted by signal sig, else system calls\n\
337will be interrupted.");
338
339static PyObject *
340signal_siginterrupt(PyObject *self, PyObject *args)
341{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000342 int sig_num;
343 int flag;
Christian Heimes8640e742008-02-23 16:23:06 +0000344
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000345 if (!PyArg_ParseTuple(args, "ii:siginterrupt", &sig_num, &flag))
346 return NULL;
347 if (sig_num < 1 || sig_num >= NSIG) {
348 PyErr_SetString(PyExc_ValueError,
349 "signal number out of range");
350 return NULL;
351 }
352 if (siginterrupt(sig_num, flag)<0) {
353 PyErr_SetFromErrno(PyExc_RuntimeError);
354 return NULL;
355 }
Christian Heimes8640e742008-02-23 16:23:06 +0000356
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000357 Py_INCREF(Py_None);
358 return Py_None;
Christian Heimes8640e742008-02-23 16:23:06 +0000359}
360
361#endif
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000362
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000363static PyObject *
364signal_set_wakeup_fd(PyObject *self, PyObject *args)
365{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000366 struct stat buf;
367 int fd, old_fd;
368 if (!PyArg_ParseTuple(args, "i:set_wakeup_fd", &fd))
369 return NULL;
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000370#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000371 if (PyThread_get_thread_ident() != main_thread) {
372 PyErr_SetString(PyExc_ValueError,
373 "set_wakeup_fd only works in main thread");
374 return NULL;
375 }
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000376#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000377 if (fd != -1 && fstat(fd, &buf) != 0) {
378 PyErr_SetString(PyExc_ValueError, "invalid fd");
379 return NULL;
380 }
381 old_fd = wakeup_fd;
382 wakeup_fd = fd;
383 return PyLong_FromLong(old_fd);
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000384}
385
386PyDoc_STRVAR(set_wakeup_fd_doc,
387"set_wakeup_fd(fd) -> fd\n\
388\n\
389Sets the fd to be written to (with '\\0') when a signal\n\
390comes in. A library can use this to wakeup select or poll.\n\
391The previous fd is returned.\n\
392\n\
393The fd must be non-blocking.");
394
395/* C API for the same, without all the error checking */
396int
397PySignal_SetWakeupFd(int fd)
398{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000399 int old_fd = wakeup_fd;
400 if (fd < 0)
401 fd = -1;
402 wakeup_fd = fd;
403 return old_fd;
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000404}
405
406
Martin v. Löwis823725e2008-03-24 13:39:54 +0000407#ifdef HAVE_SETITIMER
408static PyObject *
409signal_setitimer(PyObject *self, PyObject *args)
410{
411 double first;
412 double interval = 0;
413 int which;
414 struct itimerval new, old;
415
416 if(!PyArg_ParseTuple(args, "id|d:setitimer", &which, &first, &interval))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000417 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000418
419 timeval_from_double(first, &new.it_value);
420 timeval_from_double(interval, &new.it_interval);
421 /* Let OS check "which" value */
422 if (setitimer(which, &new, &old) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000423 PyErr_SetFromErrno(ItimerError);
424 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000425 }
426
427 return itimer_retval(&old);
428}
429
430PyDoc_STRVAR(setitimer_doc,
431"setitimer(which, seconds[, interval])\n\
432\n\
433Sets given itimer (one of ITIMER_REAL, ITIMER_VIRTUAL\n\
434or ITIMER_PROF) to fire after value seconds and after\n\
435that every interval seconds.\n\
436The itimer can be cleared by setting seconds to zero.\n\
437\n\
438Returns old values as a tuple: (delay, interval).");
439#endif
440
441
442#ifdef HAVE_GETITIMER
443static PyObject *
444signal_getitimer(PyObject *self, PyObject *args)
445{
446 int which;
447 struct itimerval old;
448
449 if (!PyArg_ParseTuple(args, "i:getitimer", &which))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000450 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000451
452 if (getitimer(which, &old) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000453 PyErr_SetFromErrno(ItimerError);
454 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000455 }
456
457 return itimer_retval(&old);
458}
459
460PyDoc_STRVAR(getitimer_doc,
461"getitimer(which)\n\
462\n\
463Returns current value of given itimer.");
464#endif
465
466
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000467/* List of functions defined in the module */
Barry Warsaw92971171997-01-03 00:14:25 +0000468static PyMethodDef signal_methods[] = {
Guido van Rossum1171ee61997-08-22 20:42:00 +0000469#ifdef HAVE_ALARM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000470 {"alarm", signal_alarm, METH_VARARGS, alarm_doc},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000471#endif
Martin v. Löwis823725e2008-03-24 13:39:54 +0000472#ifdef HAVE_SETITIMER
473 {"setitimer", signal_setitimer, METH_VARARGS, setitimer_doc},
474#endif
475#ifdef HAVE_GETITIMER
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000476 {"getitimer", signal_getitimer, METH_VARARGS, getitimer_doc},
Martin v. Löwis823725e2008-03-24 13:39:54 +0000477#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000478 {"signal", signal_signal, METH_VARARGS, signal_doc},
479 {"getsignal", signal_getsignal, METH_VARARGS, getsignal_doc},
480 {"set_wakeup_fd", signal_set_wakeup_fd, METH_VARARGS, set_wakeup_fd_doc},
Christian Heimes8640e742008-02-23 16:23:06 +0000481#ifdef HAVE_SIGINTERRUPT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000482 {"siginterrupt", signal_siginterrupt, METH_VARARGS, siginterrupt_doc},
Christian Heimes8640e742008-02-23 16:23:06 +0000483#endif
Guido van Rossum1171ee61997-08-22 20:42:00 +0000484#ifdef HAVE_PAUSE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000485 {"pause", (PyCFunction)signal_pause,
486 METH_NOARGS,pause_doc},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000487#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000488 {"default_int_handler", signal_default_int_handler,
489 METH_VARARGS, default_int_handler_doc},
490 {NULL, NULL} /* sentinel */
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000491};
492
Barry Warsaw92971171997-01-03 00:14:25 +0000493
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000494PyDoc_STRVAR(module_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000495"This module provides mechanisms to use signal handlers in Python.\n\
496\n\
497Functions:\n\
498\n\
499alarm() -- cause SIGALRM after a specified time [Unix only]\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +0000500setitimer() -- cause a signal (described below) after a specified\n\
501 float time and the timer may restart then [Unix only]\n\
502getitimer() -- get current value of timer [Unix only]\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000503signal() -- set the action for a given signal\n\
504getsignal() -- get the signal action for a given signal\n\
505pause() -- wait until a signal arrives [Unix only]\n\
506default_int_handler() -- default SIGINT handler\n\
507\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +0000508signal constants:\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000509SIG_DFL -- used to refer to the system default handler\n\
510SIG_IGN -- used to ignore the signal\n\
511NSIG -- number of defined signals\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000512SIGINT, SIGTERM, etc. -- signal numbers\n\
513\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +0000514itimer constants:\n\
515ITIMER_REAL -- decrements in real time, and delivers SIGALRM upon\n\
516 expiration\n\
517ITIMER_VIRTUAL -- decrements only when the process is executing,\n\
518 and delivers SIGVTALRM upon expiration\n\
519ITIMER_PROF -- decrements both when the process is executing and\n\
520 when the system is executing on behalf of the process.\n\
521 Coupled with ITIMER_VIRTUAL, this timer is usually\n\
522 used to profile the time spent by the application\n\
523 in user and kernel space. SIGPROF is delivered upon\n\
524 expiration.\n\
525\n\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000526*** IMPORTANT NOTICE ***\n\
527A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000528the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000529
Martin v. Löwis1a214512008-06-11 05:26:20 +0000530static struct PyModuleDef signalmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000531 PyModuleDef_HEAD_INIT,
532 "signal",
533 module_doc,
534 -1,
535 signal_methods,
536 NULL,
537 NULL,
538 NULL,
539 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000540};
541
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000542PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000543PyInit_signal(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000544{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000545 PyObject *m, *d, *x;
546 int i;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000547
548#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000549 main_thread = PyThread_get_thread_ident();
550 main_pid = getpid();
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000551#endif
552
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000553 /* Create the module and add the functions */
554 m = PyModule_Create(&signalmodule);
555 if (m == NULL)
556 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000557
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000558 /* Add some symbolic constants to the module */
559 d = PyModule_GetDict(m);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000560
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000561 x = DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL);
562 if (!x || PyDict_SetItemString(d, "SIG_DFL", x) < 0)
563 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000564
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000565 x = IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN);
566 if (!x || PyDict_SetItemString(d, "SIG_IGN", x) < 0)
567 goto finally;
Barry Warsaw92971171997-01-03 00:14:25 +0000568
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000569 x = PyLong_FromLong((long)NSIG);
570 if (!x || PyDict_SetItemString(d, "NSIG", x) < 0)
571 goto finally;
572 Py_DECREF(x);
Barry Warsaw92971171997-01-03 00:14:25 +0000573
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000574 x = IntHandler = PyDict_GetItemString(d, "default_int_handler");
575 if (!x)
576 goto finally;
577 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +0000578
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000579 Handlers[0].tripped = 0;
580 for (i = 1; i < NSIG; i++) {
581 void (*t)(int);
582 t = PyOS_getsig(i);
583 Handlers[i].tripped = 0;
584 if (t == SIG_DFL)
585 Handlers[i].func = DefaultHandler;
586 else if (t == SIG_IGN)
587 Handlers[i].func = IgnoreHandler;
588 else
589 Handlers[i].func = Py_None; /* None of our business */
590 Py_INCREF(Handlers[i].func);
591 }
592 if (Handlers[SIGINT].func == DefaultHandler) {
593 /* Install default int handler */
594 Py_INCREF(IntHandler);
595 Py_DECREF(Handlers[SIGINT].func);
596 Handlers[SIGINT].func = IntHandler;
597 old_siginthandler = PyOS_setsig(SIGINT, signal_handler);
598 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000599
600#ifdef SIGHUP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000601 x = PyLong_FromLong(SIGHUP);
602 PyDict_SetItemString(d, "SIGHUP", x);
603 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000604#endif
605#ifdef SIGINT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000606 x = PyLong_FromLong(SIGINT);
607 PyDict_SetItemString(d, "SIGINT", x);
608 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000609#endif
Tim Peters1ce3cf72001-10-01 17:58:40 +0000610#ifdef SIGBREAK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000611 x = PyLong_FromLong(SIGBREAK);
612 PyDict_SetItemString(d, "SIGBREAK", x);
613 Py_XDECREF(x);
Tim Peters1ce3cf72001-10-01 17:58:40 +0000614#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000615#ifdef SIGQUIT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000616 x = PyLong_FromLong(SIGQUIT);
617 PyDict_SetItemString(d, "SIGQUIT", x);
618 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000619#endif
620#ifdef SIGILL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000621 x = PyLong_FromLong(SIGILL);
622 PyDict_SetItemString(d, "SIGILL", x);
623 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000624#endif
625#ifdef SIGTRAP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000626 x = PyLong_FromLong(SIGTRAP);
627 PyDict_SetItemString(d, "SIGTRAP", x);
628 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000629#endif
630#ifdef SIGIOT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000631 x = PyLong_FromLong(SIGIOT);
632 PyDict_SetItemString(d, "SIGIOT", x);
633 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000634#endif
635#ifdef SIGABRT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000636 x = PyLong_FromLong(SIGABRT);
637 PyDict_SetItemString(d, "SIGABRT", x);
638 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000639#endif
640#ifdef SIGEMT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000641 x = PyLong_FromLong(SIGEMT);
642 PyDict_SetItemString(d, "SIGEMT", x);
643 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000644#endif
645#ifdef SIGFPE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000646 x = PyLong_FromLong(SIGFPE);
647 PyDict_SetItemString(d, "SIGFPE", x);
648 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000649#endif
650#ifdef SIGKILL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000651 x = PyLong_FromLong(SIGKILL);
652 PyDict_SetItemString(d, "SIGKILL", x);
653 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000654#endif
655#ifdef SIGBUS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000656 x = PyLong_FromLong(SIGBUS);
657 PyDict_SetItemString(d, "SIGBUS", x);
658 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000659#endif
660#ifdef SIGSEGV
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000661 x = PyLong_FromLong(SIGSEGV);
662 PyDict_SetItemString(d, "SIGSEGV", x);
663 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000664#endif
665#ifdef SIGSYS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000666 x = PyLong_FromLong(SIGSYS);
667 PyDict_SetItemString(d, "SIGSYS", x);
668 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000669#endif
670#ifdef SIGPIPE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000671 x = PyLong_FromLong(SIGPIPE);
672 PyDict_SetItemString(d, "SIGPIPE", x);
673 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000674#endif
675#ifdef SIGALRM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000676 x = PyLong_FromLong(SIGALRM);
677 PyDict_SetItemString(d, "SIGALRM", x);
678 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000679#endif
680#ifdef SIGTERM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000681 x = PyLong_FromLong(SIGTERM);
682 PyDict_SetItemString(d, "SIGTERM", x);
683 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000684#endif
685#ifdef SIGUSR1
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000686 x = PyLong_FromLong(SIGUSR1);
687 PyDict_SetItemString(d, "SIGUSR1", x);
688 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000689#endif
690#ifdef SIGUSR2
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000691 x = PyLong_FromLong(SIGUSR2);
692 PyDict_SetItemString(d, "SIGUSR2", x);
693 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000694#endif
695#ifdef SIGCLD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000696 x = PyLong_FromLong(SIGCLD);
697 PyDict_SetItemString(d, "SIGCLD", x);
698 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000699#endif
700#ifdef SIGCHLD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000701 x = PyLong_FromLong(SIGCHLD);
702 PyDict_SetItemString(d, "SIGCHLD", x);
703 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000704#endif
705#ifdef SIGPWR
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000706 x = PyLong_FromLong(SIGPWR);
707 PyDict_SetItemString(d, "SIGPWR", x);
708 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000709#endif
710#ifdef SIGIO
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000711 x = PyLong_FromLong(SIGIO);
712 PyDict_SetItemString(d, "SIGIO", x);
713 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000714#endif
715#ifdef SIGURG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000716 x = PyLong_FromLong(SIGURG);
717 PyDict_SetItemString(d, "SIGURG", x);
718 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000719#endif
720#ifdef SIGWINCH
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000721 x = PyLong_FromLong(SIGWINCH);
722 PyDict_SetItemString(d, "SIGWINCH", x);
723 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000724#endif
725#ifdef SIGPOLL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000726 x = PyLong_FromLong(SIGPOLL);
727 PyDict_SetItemString(d, "SIGPOLL", x);
728 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000729#endif
730#ifdef SIGSTOP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000731 x = PyLong_FromLong(SIGSTOP);
732 PyDict_SetItemString(d, "SIGSTOP", x);
733 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000734#endif
735#ifdef SIGTSTP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000736 x = PyLong_FromLong(SIGTSTP);
737 PyDict_SetItemString(d, "SIGTSTP", x);
738 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000739#endif
740#ifdef SIGCONT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000741 x = PyLong_FromLong(SIGCONT);
742 PyDict_SetItemString(d, "SIGCONT", x);
743 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000744#endif
745#ifdef SIGTTIN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000746 x = PyLong_FromLong(SIGTTIN);
747 PyDict_SetItemString(d, "SIGTTIN", x);
748 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000749#endif
750#ifdef SIGTTOU
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000751 x = PyLong_FromLong(SIGTTOU);
752 PyDict_SetItemString(d, "SIGTTOU", x);
753 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000754#endif
755#ifdef SIGVTALRM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000756 x = PyLong_FromLong(SIGVTALRM);
757 PyDict_SetItemString(d, "SIGVTALRM", x);
758 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000759#endif
760#ifdef SIGPROF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000761 x = PyLong_FromLong(SIGPROF);
762 PyDict_SetItemString(d, "SIGPROF", x);
763 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000764#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000765#ifdef SIGXCPU
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000766 x = PyLong_FromLong(SIGXCPU);
767 PyDict_SetItemString(d, "SIGXCPU", x);
768 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000769#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000770#ifdef SIGXFSZ
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000771 x = PyLong_FromLong(SIGXFSZ);
772 PyDict_SetItemString(d, "SIGXFSZ", x);
773 Py_XDECREF(x);
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000774#endif
Anthony Baxterf37f37d2003-07-31 10:35:29 +0000775#ifdef SIGRTMIN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000776 x = PyLong_FromLong(SIGRTMIN);
777 PyDict_SetItemString(d, "SIGRTMIN", x);
778 Py_XDECREF(x);
Anthony Baxterf37f37d2003-07-31 10:35:29 +0000779#endif
780#ifdef SIGRTMAX
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000781 x = PyLong_FromLong(SIGRTMAX);
782 PyDict_SetItemString(d, "SIGRTMAX", x);
783 Py_XDECREF(x);
Anthony Baxterf37f37d2003-07-31 10:35:29 +0000784#endif
Martin v. Löwis175af252002-01-12 11:43:25 +0000785#ifdef SIGINFO
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000786 x = PyLong_FromLong(SIGINFO);
787 PyDict_SetItemString(d, "SIGINFO", x);
788 Py_XDECREF(x);
Martin v. Löwis175af252002-01-12 11:43:25 +0000789#endif
Martin v. Löwis823725e2008-03-24 13:39:54 +0000790
791#ifdef ITIMER_REAL
792 x = PyLong_FromLong(ITIMER_REAL);
793 PyDict_SetItemString(d, "ITIMER_REAL", x);
794 Py_DECREF(x);
795#endif
796#ifdef ITIMER_VIRTUAL
797 x = PyLong_FromLong(ITIMER_VIRTUAL);
798 PyDict_SetItemString(d, "ITIMER_VIRTUAL", x);
799 Py_DECREF(x);
800#endif
801#ifdef ITIMER_PROF
802 x = PyLong_FromLong(ITIMER_PROF);
803 PyDict_SetItemString(d, "ITIMER_PROF", x);
804 Py_DECREF(x);
805#endif
806
807#if defined (HAVE_SETITIMER) || defined (HAVE_GETITIMER)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000808 ItimerError = PyErr_NewException("signal.ItimerError",
809 PyExc_IOError, NULL);
Neal Norwitz2f99b242008-08-24 05:48:10 +0000810 if (ItimerError != NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000811 PyDict_SetItemString(d, "ItimerError", ItimerError);
Martin v. Löwis823725e2008-03-24 13:39:54 +0000812#endif
813
Brian Curtineb24d742010-04-12 17:16:38 +0000814#ifdef CTRL_C_EVENT
815 x = PyLong_FromLong(CTRL_C_EVENT);
816 PyDict_SetItemString(d, "CTRL_C_EVENT", x);
817 Py_DECREF(x);
818#endif
819
820#ifdef CTRL_BREAK_EVENT
821 x = PyLong_FromLong(CTRL_BREAK_EVENT);
822 PyDict_SetItemString(d, "CTRL_BREAK_EVENT", x);
823 Py_DECREF(x);
824#endif
825
Martin v. Löwis1a214512008-06-11 05:26:20 +0000826 if (PyErr_Occurred()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000827 Py_DECREF(m);
828 m = NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000829 }
Barry Warsaw92971171997-01-03 00:14:25 +0000830
Barry Warsaw92971171997-01-03 00:14:25 +0000831 finally:
Martin v. Löwis1a214512008-06-11 05:26:20 +0000832 return m;
Guido van Rossum08c16611997-08-02 03:01:42 +0000833}
834
835static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000836finisignal(void)
Guido van Rossum08c16611997-08-02 03:01:42 +0000837{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000838 int i;
839 PyObject *func;
Guido van Rossum08c16611997-08-02 03:01:42 +0000840
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000841 PyOS_setsig(SIGINT, old_siginthandler);
842 old_siginthandler = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +0000843
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000844 for (i = 1; i < NSIG; i++) {
845 func = Handlers[i].func;
846 Handlers[i].tripped = 0;
847 Handlers[i].func = NULL;
848 if (i != SIGINT && func != NULL && func != Py_None &&
849 func != DefaultHandler && func != IgnoreHandler)
850 PyOS_setsig(i, SIG_DFL);
851 Py_XDECREF(func);
852 }
Guido van Rossum08c16611997-08-02 03:01:42 +0000853
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000854 Py_XDECREF(IntHandler);
855 IntHandler = NULL;
856 Py_XDECREF(DefaultHandler);
857 DefaultHandler = NULL;
858 Py_XDECREF(IgnoreHandler);
859 IgnoreHandler = NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000860}
861
Barry Warsaw92971171997-01-03 00:14:25 +0000862
Barry Warsaw92971171997-01-03 00:14:25 +0000863/* Declared in pyerrors.h */
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000864int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000865PyErr_CheckSignals(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000866{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000867 int i;
868 PyObject *f;
Barry Warsaw92971171997-01-03 00:14:25 +0000869
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000870 if (!is_tripped)
871 return 0;
Christian Heimesb76922a2007-12-11 01:06:40 +0000872
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000873#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000874 if (PyThread_get_thread_ident() != main_thread)
875 return 0;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000876#endif
Christian Heimesb76922a2007-12-11 01:06:40 +0000877
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000878 /*
879 * The is_tripped variable is meant to speed up the calls to
880 * PyErr_CheckSignals (both directly or via pending calls) when no
881 * signal has arrived. This variable is set to 1 when a signal arrives
882 * and it is set to 0 here, when we know some signals arrived. This way
883 * we can run the registered handlers with no signals blocked.
884 *
885 * NOTE: with this approach we can have a situation where is_tripped is
886 * 1 but we have no more signals to handle (Handlers[i].tripped
887 * is 0 for every signal i). This won't do us any harm (except
888 * we're gonna spent some cycles for nothing). This happens when
889 * we receive a signal i after we zero is_tripped and before we
890 * check Handlers[i].tripped.
891 */
892 is_tripped = 0;
Christian Heimesb76922a2007-12-11 01:06:40 +0000893
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000894 if (!(f = (PyObject *)PyEval_GetFrame()))
895 f = Py_None;
Christian Heimesb76922a2007-12-11 01:06:40 +0000896
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000897 for (i = 1; i < NSIG; i++) {
898 if (Handlers[i].tripped) {
899 PyObject *result = NULL;
900 PyObject *arglist = Py_BuildValue("(iO)", i, f);
901 Handlers[i].tripped = 0;
Barry Warsaw92971171997-01-03 00:14:25 +0000902
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000903 if (arglist) {
904 result = PyEval_CallObject(Handlers[i].func,
905 arglist);
906 Py_DECREF(arglist);
907 }
908 if (!result)
909 return -1;
Barry Warsaw92971171997-01-03 00:14:25 +0000910
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000911 Py_DECREF(result);
912 }
913 }
Christian Heimesb76922a2007-12-11 01:06:40 +0000914
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000915 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000916}
917
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000918
Barry Warsaw92971171997-01-03 00:14:25 +0000919/* Replacements for intrcheck.c functionality
920 * Declared in pyerrors.h
921 */
922void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000923PyErr_SetInterrupt(void)
Barry Warsaw92971171997-01-03 00:14:25 +0000924{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000925 is_tripped = 1;
926 Handlers[SIGINT].tripped = 1;
927 Py_AddPendingCall((int (*)(void *))PyErr_CheckSignals, NULL);
Barry Warsaw92971171997-01-03 00:14:25 +0000928}
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000929
930void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000931PyOS_InitInterrupts(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000932{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000933 PyObject *m = PyInit_signal();
934 if (m) {
935 _PyImport_FixupExtension(m, "signal", "signal");
936 Py_DECREF(m);
937 }
Guido van Rossum08c16611997-08-02 03:01:42 +0000938}
939
940void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000941PyOS_FiniInterrupts(void)
Guido van Rossum08c16611997-08-02 03:01:42 +0000942{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000943 finisignal();
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000944}
945
946int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000947PyOS_InterruptOccurred(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000948{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000949 if (Handlers[SIGINT].tripped) {
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000950#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000951 if (PyThread_get_thread_ident() != main_thread)
952 return 0;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000953#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000954 Handlers[SIGINT].tripped = 0;
955 return 1;
956 }
957 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000958}
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000959
960void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000961PyOS_AfterFork(void)
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000962{
963#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000964 PyEval_ReInitThreads();
965 main_thread = PyThread_get_thread_ident();
966 main_pid = getpid();
967 _PyImport_ReInitLock();
968 PyThread_ReInitTLS();
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000969#endif
970}