blob: e420a1b82c4f5c054d0576d34a3aad036c0a5f66 [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
Guido van Rossum644a12b1997-04-09 19:24:53 +000010#include <process.h>
11#endif
12
Guido van Rossum398d9fe1994-05-11 08:59:13 +000013#include <signal.h>
14
Christian Heimes5fb7c2a2007-12-24 08:52:31 +000015#include <sys/stat.h>
Martin v. Löwis3bf3cc02008-03-24 14:05:07 +000016#ifdef HAVE_SYS_TIME_H
Martin v. Löwis823725e2008-03-24 13:39:54 +000017#include <sys/time.h>
Martin v. Löwis3bf3cc02008-03-24 14:05:07 +000018#endif
Christian Heimes5fb7c2a2007-12-24 08:52:31 +000019
Guido van Rossumbb4ba121994-06-23 11:25:45 +000020#ifndef SIG_ERR
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +000021#define SIG_ERR ((PyOS_sighandler_t)(-1))
Guido van Rossumbb4ba121994-06-23 11:25:45 +000022#endif
23
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000024#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000025#define NSIG 12
26#include <process.h>
27#endif
28
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000029#ifndef NSIG
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000030# if defined(_NSIG)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000031# define NSIG _NSIG /* For BSD/SysV */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000032# elif defined(_SIGMAX)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000033# define NSIG (_SIGMAX + 1) /* For QNX */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000034# elif defined(SIGMAX)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000035# define NSIG (SIGMAX + 1) /* For djgpp */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000036# else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000037# define NSIG 64 /* Use a reasonable default value */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000038# endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000039#endif
40
41
Guido van Rossumbb4ba121994-06-23 11:25:45 +000042/*
43 NOTES ON THE INTERACTION BETWEEN SIGNALS AND THREADS
44
45 When threads are supported, we want the following semantics:
46
47 - only the main thread can set a signal handler
48 - any thread can get a signal handler
49 - signals are only delivered to the main thread
50
51 I.e. we don't support "synchronous signals" like SIGFPE (catching
52 this doesn't make much sense in Python anyway) nor do we support
53 signals as a means of inter-thread communication, since not all
54 thread implementations support that (at least our thread library
55 doesn't).
56
57 We still have the problem that in some implementations signals
58 generated by the keyboard (e.g. SIGINT) are delivered to all
59 threads (e.g. SGI), while in others (e.g. Solaris) such signals are
60 delivered to one random thread (an intermediate possibility would
Guido van Rossuma3c04b01995-01-12 11:29:01 +000061 be to deliver it to the main thread -- POSIX?). For now, we have
Guido van Rossumbb4ba121994-06-23 11:25:45 +000062 a working implementation that works in all three cases -- the
63 handler ignores signals if getpid() isn't the same as in the main
64 thread. XXX This is a hack.
65
Guido van Rossum9e8181b2000-09-19 00:46:46 +000066 GNU pth is a user-space threading library, and as such, all threads
67 run within the same process. In this case, if the currently running
68 thread is not the main_thread, send the signal to the main_thread.
Guido van Rossumbb4ba121994-06-23 11:25:45 +000069*/
70
71#ifdef WITH_THREAD
Guido van Rossum295b8e51997-06-06 21:16:41 +000072#include <sys/types.h> /* For pid_t */
Guido van Rossum49b56061998-10-01 20:42:43 +000073#include "pythread.h"
Guido van Rossumbb4ba121994-06-23 11:25:45 +000074static long main_thread;
75static pid_t main_pid;
76#endif
77
Barry Warsaw92971171997-01-03 00:14:25 +000078static struct {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000079 int tripped;
80 PyObject *func;
Barry Warsaw92971171997-01-03 00:14:25 +000081} Handlers[NSIG];
Guido van Rossum398d9fe1994-05-11 08:59:13 +000082
Christian Heimes5fb7c2a2007-12-24 08:52:31 +000083static sig_atomic_t wakeup_fd = -1;
84
Christian Heimesb76922a2007-12-11 01:06:40 +000085/* Speed up sigcheck() when none tripped */
86static volatile sig_atomic_t is_tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +000087
Barry Warsaw92971171997-01-03 00:14:25 +000088static PyObject *DefaultHandler;
89static PyObject *IgnoreHandler;
90static PyObject *IntHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +000091
Martin v. Löwisf58de1b2001-03-06 12:13:56 +000092/* On Solaris 8, gcc will produce a warning that the function
93 declaration is not a prototype. This is caused by the definition of
94 SIG_DFL as (void (*)())0; the correct declaration would have been
95 (void (*)(int))0. */
96
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +000097static PyOS_sighandler_t old_siginthandler = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +000098
Martin v. Löwis823725e2008-03-24 13:39:54 +000099#ifdef HAVE_GETITIMER
100static PyObject *ItimerError;
101
102/* auxiliary functions for setitimer/getitimer */
103static void
104timeval_from_double(double d, struct timeval *tv)
105{
106 tv->tv_sec = floor(d);
107 tv->tv_usec = fmod(d, 1.0) * 1000000.0;
108}
109
Christian Heimes1a8501c2008-10-02 19:56:01 +0000110Py_LOCAL_INLINE(double)
Martin v. Löwis823725e2008-03-24 13:39:54 +0000111double_from_timeval(struct timeval *tv)
112{
113 return tv->tv_sec + (double)(tv->tv_usec / 1000000.0);
114}
115
116static PyObject *
117itimer_retval(struct itimerval *iv)
118{
119 PyObject *r, *v;
120
121 r = PyTuple_New(2);
122 if (r == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000123 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000124
125 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_value)))) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000126 Py_DECREF(r);
127 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000128 }
129
130 PyTuple_SET_ITEM(r, 0, v);
131
132 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_interval)))) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000133 Py_DECREF(r);
134 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000135 }
136
137 PyTuple_SET_ITEM(r, 1, v);
138
139 return r;
140}
141#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000142
Guido van Rossume4485b01994-09-07 14:32:49 +0000143static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000144signal_default_int_handler(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000145{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000146 PyErr_SetNone(PyExc_KeyboardInterrupt);
147 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000148}
149
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000150PyDoc_STRVAR(default_int_handler_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000151"default_int_handler(...)\n\
152\n\
Michael W. Hudson24ec2112004-06-17 15:55:53 +0000153The default handler for SIGINT installed by Python.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000154It raises KeyboardInterrupt.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000155
Thomas Wouters0796b002000-07-22 23:49:30 +0000156
157static int
158checksignals_witharg(void * unused)
159{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000160 return PyErr_CheckSignals();
Thomas Wouters0796b002000-07-22 23:49:30 +0000161}
162
Tim Peters4f1b2082000-07-23 21:18:09 +0000163static void
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000164signal_handler(int sig_num)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000165{
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000166#ifdef WITH_THREAD
Guido van Rossum9e8181b2000-09-19 00:46:46 +0000167#ifdef WITH_PTH
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000168 if (PyThread_get_thread_ident() != main_thread) {
169 pth_raise(*(pth_t *) main_thread, sig_num);
170 return;
171 }
Guido van Rossum9e8181b2000-09-19 00:46:46 +0000172#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000173 /* See NOTES section above */
174 if (getpid() == main_pid) {
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000175#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000176 Handlers[sig_num].tripped = 1;
177 /* Set is_tripped after setting .tripped, as it gets
178 cleared in PyErr_CheckSignals() before .tripped. */
179 is_tripped = 1;
180 Py_AddPendingCall(checksignals_witharg, NULL);
181 if (wakeup_fd != -1)
182 write(wakeup_fd, "\0", 1);
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000183#ifdef WITH_THREAD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000184 }
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000185#endif
Guido van Rossum602099a1994-09-14 13:32:22 +0000186#ifdef SIGCHLD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000187 if (sig_num == SIGCHLD) {
188 /* To avoid infinite recursion, this signal remains
189 reset until explicit re-instated.
190 Don't clear the 'func' field as it is our pointer
191 to the Python handler... */
192 return;
193 }
Guido van Rossum602099a1994-09-14 13:32:22 +0000194#endif
Jean-Paul Calderone9c39bc72010-05-09 03:25:16 +0000195#ifndef HAVE_SIGACTION
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000196 /* If the handler was not set up with sigaction, reinstall it. See
197 * Python/pythonrun.c for the implementation of PyOS_setsig which
198 * makes this true. See also issue8354. */
199 PyOS_setsig(sig_num, signal_handler);
Jean-Paul Calderone9c39bc72010-05-09 03:25:16 +0000200#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000201}
Guido van Rossume4485b01994-09-07 14:32:49 +0000202
Guido van Rossum06d511d1995-03-10 15:13:48 +0000203
Guido van Rossum1171ee61997-08-22 20:42:00 +0000204#ifdef HAVE_ALARM
Guido van Rossume4485b01994-09-07 14:32:49 +0000205static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000206signal_alarm(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000207{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000208 int t;
209 if (!PyArg_ParseTuple(args, "i:alarm", &t))
210 return NULL;
211 /* alarm() returns the number of seconds remaining */
212 return PyLong_FromLong((long)alarm(t));
Guido van Rossumaa0f4c71994-08-23 13:49:37 +0000213}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000214
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000215PyDoc_STRVAR(alarm_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000216"alarm(seconds)\n\
217\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000218Arrange for SIGALRM to arrive after the given number of seconds.");
Guido van Rossum06d511d1995-03-10 15:13:48 +0000219#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000220
Guido van Rossum1171ee61997-08-22 20:42:00 +0000221#ifdef HAVE_PAUSE
Guido van Rossuma597dde1995-01-10 20:56:29 +0000222static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000223signal_pause(PyObject *self)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000224{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000225 Py_BEGIN_ALLOW_THREADS
226 (void)pause();
227 Py_END_ALLOW_THREADS
228 /* make sure that any exceptions that got raised are propagated
229 * back into Python
230 */
231 if (PyErr_CheckSignals())
232 return NULL;
Barry Warsaw92971171997-01-03 00:14:25 +0000233
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000234 Py_INCREF(Py_None);
235 return Py_None;
Guido van Rossume4485b01994-09-07 14:32:49 +0000236}
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000237PyDoc_STRVAR(pause_doc,
Barry Warsaw1ee36ff1998-07-21 22:41:18 +0000238"pause()\n\
239\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000240Wait until a signal arrives.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000241
Guido van Rossum06d511d1995-03-10 15:13:48 +0000242#endif
Guido van Rossume4485b01994-09-07 14:32:49 +0000243
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000244
Guido van Rossume4485b01994-09-07 14:32:49 +0000245static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000246signal_signal(PyObject *self, PyObject *args)
Guido van Rossume4485b01994-09-07 14:32:49 +0000247{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000248 PyObject *obj;
249 int sig_num;
250 PyObject *old_handler;
251 void (*func)(int);
252 if (!PyArg_ParseTuple(args, "iO:signal", &sig_num, &obj))
253 return NULL;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000254#ifdef WITH_THREAD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000255 if (PyThread_get_thread_ident() != main_thread) {
256 PyErr_SetString(PyExc_ValueError,
257 "signal only works in main thread");
258 return NULL;
259 }
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000260#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000261 if (sig_num < 1 || sig_num >= NSIG) {
262 PyErr_SetString(PyExc_ValueError,
263 "signal number out of range");
264 return NULL;
265 }
266 if (obj == IgnoreHandler)
267 func = SIG_IGN;
268 else if (obj == DefaultHandler)
269 func = SIG_DFL;
270 else if (!PyCallable_Check(obj)) {
271 PyErr_SetString(PyExc_TypeError,
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000272"signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000273 return NULL;
274 }
275 else
276 func = signal_handler;
277 if (PyOS_setsig(sig_num, func) == SIG_ERR) {
278 PyErr_SetFromErrno(PyExc_RuntimeError);
279 return NULL;
280 }
281 old_handler = Handlers[sig_num].func;
282 Handlers[sig_num].tripped = 0;
283 Py_INCREF(obj);
284 Handlers[sig_num].func = obj;
285 return old_handler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000286}
287
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000288PyDoc_STRVAR(signal_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000289"signal(sig, action) -> action\n\
290\n\
291Set the action for the given signal. The action can be SIG_DFL,\n\
292SIG_IGN, or a callable Python object. The previous action is\n\
293returned. See getsignal() for possible return values.\n\
294\n\
295*** IMPORTANT NOTICE ***\n\
296A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000297the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000298
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000299
Guido van Rossume4485b01994-09-07 14:32:49 +0000300static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000301signal_getsignal(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000302{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000303 int sig_num;
304 PyObject *old_handler;
305 if (!PyArg_ParseTuple(args, "i:getsignal", &sig_num))
306 return NULL;
307 if (sig_num < 1 || sig_num >= NSIG) {
308 PyErr_SetString(PyExc_ValueError,
309 "signal number out of range");
310 return NULL;
311 }
312 old_handler = Handlers[sig_num].func;
313 Py_INCREF(old_handler);
314 return old_handler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000315}
316
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000317PyDoc_STRVAR(getsignal_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000318"getsignal(sig) -> action\n\
319\n\
320Return the current action for the given signal. The return value can be:\n\
321SIG_IGN -- if the signal is being ignored\n\
322SIG_DFL -- if the default action for the signal is in effect\n\
323None -- if an unknown handler is in effect\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000324anything else -- the callable Python object used as a handler");
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000325
Christian Heimes8640e742008-02-23 16:23:06 +0000326#ifdef HAVE_SIGINTERRUPT
327PyDoc_STRVAR(siginterrupt_doc,
328"siginterrupt(sig, flag) -> None\n\
329change system call restart behaviour: if flag is False, system calls\n\
330will be restarted when interrupted by signal sig, else system calls\n\
331will be interrupted.");
332
333static PyObject *
334signal_siginterrupt(PyObject *self, PyObject *args)
335{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000336 int sig_num;
337 int flag;
Christian Heimes8640e742008-02-23 16:23:06 +0000338
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000339 if (!PyArg_ParseTuple(args, "ii:siginterrupt", &sig_num, &flag))
340 return NULL;
341 if (sig_num < 1 || sig_num >= NSIG) {
342 PyErr_SetString(PyExc_ValueError,
343 "signal number out of range");
344 return NULL;
345 }
346 if (siginterrupt(sig_num, flag)<0) {
347 PyErr_SetFromErrno(PyExc_RuntimeError);
348 return NULL;
349 }
Christian Heimes8640e742008-02-23 16:23:06 +0000350
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000351 Py_INCREF(Py_None);
352 return Py_None;
Christian Heimes8640e742008-02-23 16:23:06 +0000353}
354
355#endif
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000356
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000357static PyObject *
358signal_set_wakeup_fd(PyObject *self, PyObject *args)
359{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000360 struct stat buf;
361 int fd, old_fd;
362 if (!PyArg_ParseTuple(args, "i:set_wakeup_fd", &fd))
363 return NULL;
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000364#ifdef WITH_THREAD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000365 if (PyThread_get_thread_ident() != main_thread) {
366 PyErr_SetString(PyExc_ValueError,
367 "set_wakeup_fd only works in main thread");
368 return NULL;
369 }
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000370#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000371 if (fd != -1 && fstat(fd, &buf) != 0) {
372 PyErr_SetString(PyExc_ValueError, "invalid fd");
373 return NULL;
374 }
375 old_fd = wakeup_fd;
376 wakeup_fd = fd;
377 return PyLong_FromLong(old_fd);
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000378}
379
380PyDoc_STRVAR(set_wakeup_fd_doc,
381"set_wakeup_fd(fd) -> fd\n\
382\n\
383Sets the fd to be written to (with '\\0') when a signal\n\
384comes in. A library can use this to wakeup select or poll.\n\
385The previous fd is returned.\n\
386\n\
387The fd must be non-blocking.");
388
389/* C API for the same, without all the error checking */
390int
391PySignal_SetWakeupFd(int fd)
392{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000393 int old_fd = wakeup_fd;
394 if (fd < 0)
395 fd = -1;
396 wakeup_fd = fd;
397 return old_fd;
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000398}
399
400
Martin v. Löwis823725e2008-03-24 13:39:54 +0000401#ifdef HAVE_SETITIMER
402static PyObject *
403signal_setitimer(PyObject *self, PyObject *args)
404{
405 double first;
406 double interval = 0;
407 int which;
408 struct itimerval new, old;
409
410 if(!PyArg_ParseTuple(args, "id|d:setitimer", &which, &first, &interval))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000411 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000412
413 timeval_from_double(first, &new.it_value);
414 timeval_from_double(interval, &new.it_interval);
415 /* Let OS check "which" value */
416 if (setitimer(which, &new, &old) != 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000417 PyErr_SetFromErrno(ItimerError);
418 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000419 }
420
421 return itimer_retval(&old);
422}
423
424PyDoc_STRVAR(setitimer_doc,
425"setitimer(which, seconds[, interval])\n\
426\n\
427Sets given itimer (one of ITIMER_REAL, ITIMER_VIRTUAL\n\
428or ITIMER_PROF) to fire after value seconds and after\n\
429that every interval seconds.\n\
430The itimer can be cleared by setting seconds to zero.\n\
431\n\
432Returns old values as a tuple: (delay, interval).");
433#endif
434
435
436#ifdef HAVE_GETITIMER
437static PyObject *
438signal_getitimer(PyObject *self, PyObject *args)
439{
440 int which;
441 struct itimerval old;
442
443 if (!PyArg_ParseTuple(args, "i:getitimer", &which))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000444 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000445
446 if (getitimer(which, &old) != 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000447 PyErr_SetFromErrno(ItimerError);
448 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000449 }
450
451 return itimer_retval(&old);
452}
453
454PyDoc_STRVAR(getitimer_doc,
455"getitimer(which)\n\
456\n\
457Returns current value of given itimer.");
458#endif
459
460
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000461/* List of functions defined in the module */
Barry Warsaw92971171997-01-03 00:14:25 +0000462static PyMethodDef signal_methods[] = {
Guido van Rossum1171ee61997-08-22 20:42:00 +0000463#ifdef HAVE_ALARM
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000464 {"alarm", signal_alarm, METH_VARARGS, alarm_doc},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000465#endif
Martin v. Löwis823725e2008-03-24 13:39:54 +0000466#ifdef HAVE_SETITIMER
467 {"setitimer", signal_setitimer, METH_VARARGS, setitimer_doc},
468#endif
469#ifdef HAVE_GETITIMER
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000470 {"getitimer", signal_getitimer, METH_VARARGS, getitimer_doc},
Martin v. Löwis823725e2008-03-24 13:39:54 +0000471#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000472 {"signal", signal_signal, METH_VARARGS, signal_doc},
473 {"getsignal", signal_getsignal, METH_VARARGS, getsignal_doc},
474 {"set_wakeup_fd", signal_set_wakeup_fd, METH_VARARGS, set_wakeup_fd_doc},
Christian Heimes8640e742008-02-23 16:23:06 +0000475#ifdef HAVE_SIGINTERRUPT
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000476 {"siginterrupt", signal_siginterrupt, METH_VARARGS, siginterrupt_doc},
Christian Heimes8640e742008-02-23 16:23:06 +0000477#endif
Guido van Rossum1171ee61997-08-22 20:42:00 +0000478#ifdef HAVE_PAUSE
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000479 {"pause", (PyCFunction)signal_pause,
480 METH_NOARGS,pause_doc},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000481#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000482 {"default_int_handler", signal_default_int_handler,
483 METH_VARARGS, default_int_handler_doc},
484 {NULL, NULL} /* sentinel */
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000485};
486
Barry Warsaw92971171997-01-03 00:14:25 +0000487
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000488PyDoc_STRVAR(module_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000489"This module provides mechanisms to use signal handlers in Python.\n\
490\n\
491Functions:\n\
492\n\
493alarm() -- cause SIGALRM after a specified time [Unix only]\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +0000494setitimer() -- cause a signal (described below) after a specified\n\
495 float time and the timer may restart then [Unix only]\n\
496getitimer() -- get current value of timer [Unix only]\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000497signal() -- set the action for a given signal\n\
498getsignal() -- get the signal action for a given signal\n\
499pause() -- wait until a signal arrives [Unix only]\n\
500default_int_handler() -- default SIGINT handler\n\
501\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +0000502signal constants:\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000503SIG_DFL -- used to refer to the system default handler\n\
504SIG_IGN -- used to ignore the signal\n\
505NSIG -- number of defined signals\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000506SIGINT, SIGTERM, etc. -- signal numbers\n\
507\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +0000508itimer constants:\n\
509ITIMER_REAL -- decrements in real time, and delivers SIGALRM upon\n\
510 expiration\n\
511ITIMER_VIRTUAL -- decrements only when the process is executing,\n\
512 and delivers SIGVTALRM upon expiration\n\
513ITIMER_PROF -- decrements both when the process is executing and\n\
514 when the system is executing on behalf of the process.\n\
515 Coupled with ITIMER_VIRTUAL, this timer is usually\n\
516 used to profile the time spent by the application\n\
517 in user and kernel space. SIGPROF is delivered upon\n\
518 expiration.\n\
519\n\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000520*** IMPORTANT NOTICE ***\n\
521A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000522the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000523
Martin v. Löwis1a214512008-06-11 05:26:20 +0000524static struct PyModuleDef signalmodule = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000525 PyModuleDef_HEAD_INIT,
526 "signal",
527 module_doc,
528 -1,
529 signal_methods,
530 NULL,
531 NULL,
532 NULL,
533 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000534};
535
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000536PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000537PyInit_signal(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000538{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000539 PyObject *m, *d, *x;
540 int i;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000541
542#ifdef WITH_THREAD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000543 main_thread = PyThread_get_thread_ident();
544 main_pid = getpid();
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000545#endif
546
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000547 /* Create the module and add the functions */
548 m = PyModule_Create(&signalmodule);
549 if (m == NULL)
550 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000551
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000552 /* Add some symbolic constants to the module */
553 d = PyModule_GetDict(m);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000554
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000555 x = DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL);
556 if (!x || PyDict_SetItemString(d, "SIG_DFL", x) < 0)
557 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000558
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000559 x = IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN);
560 if (!x || PyDict_SetItemString(d, "SIG_IGN", x) < 0)
561 goto finally;
Barry Warsaw92971171997-01-03 00:14:25 +0000562
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000563 x = PyLong_FromLong((long)NSIG);
564 if (!x || PyDict_SetItemString(d, "NSIG", x) < 0)
565 goto finally;
566 Py_DECREF(x);
Barry Warsaw92971171997-01-03 00:14:25 +0000567
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000568 x = IntHandler = PyDict_GetItemString(d, "default_int_handler");
569 if (!x)
570 goto finally;
571 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +0000572
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000573 Handlers[0].tripped = 0;
574 for (i = 1; i < NSIG; i++) {
575 void (*t)(int);
576 t = PyOS_getsig(i);
577 Handlers[i].tripped = 0;
578 if (t == SIG_DFL)
579 Handlers[i].func = DefaultHandler;
580 else if (t == SIG_IGN)
581 Handlers[i].func = IgnoreHandler;
582 else
583 Handlers[i].func = Py_None; /* None of our business */
584 Py_INCREF(Handlers[i].func);
585 }
586 if (Handlers[SIGINT].func == DefaultHandler) {
587 /* Install default int handler */
588 Py_INCREF(IntHandler);
589 Py_DECREF(Handlers[SIGINT].func);
590 Handlers[SIGINT].func = IntHandler;
591 old_siginthandler = PyOS_setsig(SIGINT, signal_handler);
592 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000593
594#ifdef SIGHUP
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000595 x = PyLong_FromLong(SIGHUP);
596 PyDict_SetItemString(d, "SIGHUP", x);
597 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000598#endif
599#ifdef SIGINT
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000600 x = PyLong_FromLong(SIGINT);
601 PyDict_SetItemString(d, "SIGINT", x);
602 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000603#endif
Tim Peters1ce3cf72001-10-01 17:58:40 +0000604#ifdef SIGBREAK
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000605 x = PyLong_FromLong(SIGBREAK);
606 PyDict_SetItemString(d, "SIGBREAK", x);
607 Py_XDECREF(x);
Tim Peters1ce3cf72001-10-01 17:58:40 +0000608#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000609#ifdef SIGQUIT
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000610 x = PyLong_FromLong(SIGQUIT);
611 PyDict_SetItemString(d, "SIGQUIT", x);
612 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000613#endif
614#ifdef SIGILL
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000615 x = PyLong_FromLong(SIGILL);
616 PyDict_SetItemString(d, "SIGILL", x);
617 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000618#endif
619#ifdef SIGTRAP
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000620 x = PyLong_FromLong(SIGTRAP);
621 PyDict_SetItemString(d, "SIGTRAP", x);
622 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000623#endif
624#ifdef SIGIOT
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000625 x = PyLong_FromLong(SIGIOT);
626 PyDict_SetItemString(d, "SIGIOT", x);
627 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000628#endif
629#ifdef SIGABRT
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000630 x = PyLong_FromLong(SIGABRT);
631 PyDict_SetItemString(d, "SIGABRT", x);
632 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000633#endif
634#ifdef SIGEMT
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000635 x = PyLong_FromLong(SIGEMT);
636 PyDict_SetItemString(d, "SIGEMT", x);
637 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000638#endif
639#ifdef SIGFPE
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000640 x = PyLong_FromLong(SIGFPE);
641 PyDict_SetItemString(d, "SIGFPE", x);
642 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000643#endif
644#ifdef SIGKILL
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000645 x = PyLong_FromLong(SIGKILL);
646 PyDict_SetItemString(d, "SIGKILL", x);
647 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000648#endif
649#ifdef SIGBUS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000650 x = PyLong_FromLong(SIGBUS);
651 PyDict_SetItemString(d, "SIGBUS", x);
652 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000653#endif
654#ifdef SIGSEGV
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000655 x = PyLong_FromLong(SIGSEGV);
656 PyDict_SetItemString(d, "SIGSEGV", x);
657 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000658#endif
659#ifdef SIGSYS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000660 x = PyLong_FromLong(SIGSYS);
661 PyDict_SetItemString(d, "SIGSYS", x);
662 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000663#endif
664#ifdef SIGPIPE
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000665 x = PyLong_FromLong(SIGPIPE);
666 PyDict_SetItemString(d, "SIGPIPE", x);
667 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000668#endif
669#ifdef SIGALRM
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000670 x = PyLong_FromLong(SIGALRM);
671 PyDict_SetItemString(d, "SIGALRM", x);
672 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000673#endif
674#ifdef SIGTERM
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000675 x = PyLong_FromLong(SIGTERM);
676 PyDict_SetItemString(d, "SIGTERM", x);
677 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000678#endif
679#ifdef SIGUSR1
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000680 x = PyLong_FromLong(SIGUSR1);
681 PyDict_SetItemString(d, "SIGUSR1", x);
682 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000683#endif
684#ifdef SIGUSR2
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000685 x = PyLong_FromLong(SIGUSR2);
686 PyDict_SetItemString(d, "SIGUSR2", x);
687 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000688#endif
689#ifdef SIGCLD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000690 x = PyLong_FromLong(SIGCLD);
691 PyDict_SetItemString(d, "SIGCLD", x);
692 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000693#endif
694#ifdef SIGCHLD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000695 x = PyLong_FromLong(SIGCHLD);
696 PyDict_SetItemString(d, "SIGCHLD", x);
697 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000698#endif
699#ifdef SIGPWR
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000700 x = PyLong_FromLong(SIGPWR);
701 PyDict_SetItemString(d, "SIGPWR", x);
702 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000703#endif
704#ifdef SIGIO
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000705 x = PyLong_FromLong(SIGIO);
706 PyDict_SetItemString(d, "SIGIO", x);
707 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000708#endif
709#ifdef SIGURG
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000710 x = PyLong_FromLong(SIGURG);
711 PyDict_SetItemString(d, "SIGURG", x);
712 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000713#endif
714#ifdef SIGWINCH
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000715 x = PyLong_FromLong(SIGWINCH);
716 PyDict_SetItemString(d, "SIGWINCH", x);
717 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000718#endif
719#ifdef SIGPOLL
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000720 x = PyLong_FromLong(SIGPOLL);
721 PyDict_SetItemString(d, "SIGPOLL", x);
722 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000723#endif
724#ifdef SIGSTOP
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000725 x = PyLong_FromLong(SIGSTOP);
726 PyDict_SetItemString(d, "SIGSTOP", x);
727 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000728#endif
729#ifdef SIGTSTP
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000730 x = PyLong_FromLong(SIGTSTP);
731 PyDict_SetItemString(d, "SIGTSTP", x);
732 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000733#endif
734#ifdef SIGCONT
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000735 x = PyLong_FromLong(SIGCONT);
736 PyDict_SetItemString(d, "SIGCONT", x);
737 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000738#endif
739#ifdef SIGTTIN
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000740 x = PyLong_FromLong(SIGTTIN);
741 PyDict_SetItemString(d, "SIGTTIN", x);
742 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000743#endif
744#ifdef SIGTTOU
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000745 x = PyLong_FromLong(SIGTTOU);
746 PyDict_SetItemString(d, "SIGTTOU", x);
747 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000748#endif
749#ifdef SIGVTALRM
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000750 x = PyLong_FromLong(SIGVTALRM);
751 PyDict_SetItemString(d, "SIGVTALRM", x);
752 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000753#endif
754#ifdef SIGPROF
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000755 x = PyLong_FromLong(SIGPROF);
756 PyDict_SetItemString(d, "SIGPROF", x);
757 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000758#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000759#ifdef SIGXCPU
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000760 x = PyLong_FromLong(SIGXCPU);
761 PyDict_SetItemString(d, "SIGXCPU", x);
762 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000763#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000764#ifdef SIGXFSZ
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000765 x = PyLong_FromLong(SIGXFSZ);
766 PyDict_SetItemString(d, "SIGXFSZ", x);
767 Py_XDECREF(x);
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000768#endif
Anthony Baxterf37f37d2003-07-31 10:35:29 +0000769#ifdef SIGRTMIN
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000770 x = PyLong_FromLong(SIGRTMIN);
771 PyDict_SetItemString(d, "SIGRTMIN", x);
772 Py_XDECREF(x);
Anthony Baxterf37f37d2003-07-31 10:35:29 +0000773#endif
774#ifdef SIGRTMAX
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000775 x = PyLong_FromLong(SIGRTMAX);
776 PyDict_SetItemString(d, "SIGRTMAX", x);
777 Py_XDECREF(x);
Anthony Baxterf37f37d2003-07-31 10:35:29 +0000778#endif
Martin v. Löwis175af252002-01-12 11:43:25 +0000779#ifdef SIGINFO
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000780 x = PyLong_FromLong(SIGINFO);
781 PyDict_SetItemString(d, "SIGINFO", x);
782 Py_XDECREF(x);
Martin v. Löwis175af252002-01-12 11:43:25 +0000783#endif
Martin v. Löwis823725e2008-03-24 13:39:54 +0000784
785#ifdef ITIMER_REAL
786 x = PyLong_FromLong(ITIMER_REAL);
787 PyDict_SetItemString(d, "ITIMER_REAL", x);
788 Py_DECREF(x);
789#endif
790#ifdef ITIMER_VIRTUAL
791 x = PyLong_FromLong(ITIMER_VIRTUAL);
792 PyDict_SetItemString(d, "ITIMER_VIRTUAL", x);
793 Py_DECREF(x);
794#endif
795#ifdef ITIMER_PROF
796 x = PyLong_FromLong(ITIMER_PROF);
797 PyDict_SetItemString(d, "ITIMER_PROF", x);
798 Py_DECREF(x);
799#endif
800
801#if defined (HAVE_SETITIMER) || defined (HAVE_GETITIMER)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000802 ItimerError = PyErr_NewException("signal.ItimerError",
803 PyExc_IOError, NULL);
Neal Norwitz2f99b242008-08-24 05:48:10 +0000804 if (ItimerError != NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000805 PyDict_SetItemString(d, "ItimerError", ItimerError);
Martin v. Löwis823725e2008-03-24 13:39:54 +0000806#endif
807
Martin v. Löwis1a214512008-06-11 05:26:20 +0000808 if (PyErr_Occurred()) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000809 Py_DECREF(m);
810 m = NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000811 }
Barry Warsaw92971171997-01-03 00:14:25 +0000812
Barry Warsaw92971171997-01-03 00:14:25 +0000813 finally:
Martin v. Löwis1a214512008-06-11 05:26:20 +0000814 return m;
Guido van Rossum08c16611997-08-02 03:01:42 +0000815}
816
817static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000818finisignal(void)
Guido van Rossum08c16611997-08-02 03:01:42 +0000819{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000820 int i;
821 PyObject *func;
Guido van Rossum08c16611997-08-02 03:01:42 +0000822
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000823 PyOS_setsig(SIGINT, old_siginthandler);
824 old_siginthandler = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +0000825
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000826 for (i = 1; i < NSIG; i++) {
827 func = Handlers[i].func;
828 Handlers[i].tripped = 0;
829 Handlers[i].func = NULL;
830 if (i != SIGINT && func != NULL && func != Py_None &&
831 func != DefaultHandler && func != IgnoreHandler)
832 PyOS_setsig(i, SIG_DFL);
833 Py_XDECREF(func);
834 }
Guido van Rossum08c16611997-08-02 03:01:42 +0000835
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000836 Py_XDECREF(IntHandler);
837 IntHandler = NULL;
838 Py_XDECREF(DefaultHandler);
839 DefaultHandler = NULL;
840 Py_XDECREF(IgnoreHandler);
841 IgnoreHandler = NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000842}
843
Barry Warsaw92971171997-01-03 00:14:25 +0000844
Barry Warsaw92971171997-01-03 00:14:25 +0000845/* Declared in pyerrors.h */
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000846int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000847PyErr_CheckSignals(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000848{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000849 int i;
850 PyObject *f;
Barry Warsaw92971171997-01-03 00:14:25 +0000851
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000852 if (!is_tripped)
853 return 0;
Christian Heimesb76922a2007-12-11 01:06:40 +0000854
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000855#ifdef WITH_THREAD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000856 if (PyThread_get_thread_ident() != main_thread)
857 return 0;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000858#endif
Christian Heimesb76922a2007-12-11 01:06:40 +0000859
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000860 /*
861 * The is_tripped variable is meant to speed up the calls to
862 * PyErr_CheckSignals (both directly or via pending calls) when no
863 * signal has arrived. This variable is set to 1 when a signal arrives
864 * and it is set to 0 here, when we know some signals arrived. This way
865 * we can run the registered handlers with no signals blocked.
866 *
867 * NOTE: with this approach we can have a situation where is_tripped is
868 * 1 but we have no more signals to handle (Handlers[i].tripped
869 * is 0 for every signal i). This won't do us any harm (except
870 * we're gonna spent some cycles for nothing). This happens when
871 * we receive a signal i after we zero is_tripped and before we
872 * check Handlers[i].tripped.
873 */
874 is_tripped = 0;
Christian Heimesb76922a2007-12-11 01:06:40 +0000875
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000876 if (!(f = (PyObject *)PyEval_GetFrame()))
877 f = Py_None;
Christian Heimesb76922a2007-12-11 01:06:40 +0000878
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000879 for (i = 1; i < NSIG; i++) {
880 if (Handlers[i].tripped) {
881 PyObject *result = NULL;
882 PyObject *arglist = Py_BuildValue("(iO)", i, f);
883 Handlers[i].tripped = 0;
Barry Warsaw92971171997-01-03 00:14:25 +0000884
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000885 if (arglist) {
886 result = PyEval_CallObject(Handlers[i].func,
887 arglist);
888 Py_DECREF(arglist);
889 }
890 if (!result)
891 return -1;
Barry Warsaw92971171997-01-03 00:14:25 +0000892
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000893 Py_DECREF(result);
894 }
895 }
Christian Heimesb76922a2007-12-11 01:06:40 +0000896
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000897 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000898}
899
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000900
Barry Warsaw92971171997-01-03 00:14:25 +0000901/* Replacements for intrcheck.c functionality
902 * Declared in pyerrors.h
903 */
904void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000905PyErr_SetInterrupt(void)
Barry Warsaw92971171997-01-03 00:14:25 +0000906{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000907 is_tripped = 1;
908 Handlers[SIGINT].tripped = 1;
909 Py_AddPendingCall((int (*)(void *))PyErr_CheckSignals, NULL);
Barry Warsaw92971171997-01-03 00:14:25 +0000910}
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000911
912void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000913PyOS_InitInterrupts(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000914{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000915 PyObject *m = PyInit_signal();
916 if (m) {
917 _PyImport_FixupExtension(m, "signal", "signal");
918 Py_DECREF(m);
919 }
Guido van Rossum08c16611997-08-02 03:01:42 +0000920}
921
922void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000923PyOS_FiniInterrupts(void)
Guido van Rossum08c16611997-08-02 03:01:42 +0000924{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000925 finisignal();
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000926}
927
928int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000929PyOS_InterruptOccurred(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000930{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000931 if (Handlers[SIGINT].tripped) {
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000932#ifdef WITH_THREAD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000933 if (PyThread_get_thread_ident() != main_thread)
934 return 0;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000935#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000936 Handlers[SIGINT].tripped = 0;
937 return 1;
938 }
939 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000940}
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000941
942void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000943PyOS_AfterFork(void)
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000944{
945#ifdef WITH_THREAD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000946 PyEval_ReInitThreads();
947 main_thread = PyThread_get_thread_ident();
948 main_pid = getpid();
949 _PyImport_ReInitLock();
950 PyThread_ReInitTLS();
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000951#endif
952}