blob: c2e762abbe8bf89a76694165cf512bf6e101c419 [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 Curtinb42a21d2010-08-06 19:52:50 +000010#include <Windows.h>
11#ifdef HAVE_PROCESS_H
Guido van Rossum644a12b1997-04-09 19:24:53 +000012#include <process.h>
13#endif
Brian Curtinb42a21d2010-08-06 19:52:50 +000014#endif
Guido van Rossum644a12b1997-04-09 19:24:53 +000015
Guido van Rossum398d9fe1994-05-11 08:59:13 +000016#include <signal.h>
17
Christian Heimes5fb7c2a2007-12-24 08:52:31 +000018#include <sys/stat.h>
Martin v. Löwis3bf3cc02008-03-24 14:05:07 +000019#ifdef HAVE_SYS_TIME_H
Martin v. Löwis823725e2008-03-24 13:39:54 +000020#include <sys/time.h>
Martin v. Löwis3bf3cc02008-03-24 14:05:07 +000021#endif
Christian Heimes5fb7c2a2007-12-24 08:52:31 +000022
Guido van Rossumbb4ba121994-06-23 11:25:45 +000023#ifndef SIG_ERR
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +000024#define SIG_ERR ((PyOS_sighandler_t)(-1))
Guido van Rossumbb4ba121994-06-23 11:25:45 +000025#endif
26
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000027#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000028#define NSIG 12
29#include <process.h>
30#endif
31
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000032#ifndef NSIG
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000033# if defined(_NSIG)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000034# define NSIG _NSIG /* For BSD/SysV */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000035# elif defined(_SIGMAX)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000036# define NSIG (_SIGMAX + 1) /* For QNX */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000037# elif defined(SIGMAX)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000038# define NSIG (SIGMAX + 1) /* For djgpp */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000039# else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000040# define NSIG 64 /* Use a reasonable default value */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000041# endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000042#endif
43
44
Guido van Rossumbb4ba121994-06-23 11:25:45 +000045/*
46 NOTES ON THE INTERACTION BETWEEN SIGNALS AND THREADS
47
48 When threads are supported, we want the following semantics:
49
50 - only the main thread can set a signal handler
51 - any thread can get a signal handler
52 - signals are only delivered to the main thread
53
54 I.e. we don't support "synchronous signals" like SIGFPE (catching
55 this doesn't make much sense in Python anyway) nor do we support
56 signals as a means of inter-thread communication, since not all
57 thread implementations support that (at least our thread library
58 doesn't).
59
60 We still have the problem that in some implementations signals
61 generated by the keyboard (e.g. SIGINT) are delivered to all
62 threads (e.g. SGI), while in others (e.g. Solaris) such signals are
63 delivered to one random thread (an intermediate possibility would
Guido van Rossuma3c04b01995-01-12 11:29:01 +000064 be to deliver it to the main thread -- POSIX?). For now, we have
Guido van Rossumbb4ba121994-06-23 11:25:45 +000065 a working implementation that works in all three cases -- the
66 handler ignores signals if getpid() isn't the same as in the main
67 thread. XXX This is a hack.
68
Guido van Rossum9e8181b2000-09-19 00:46:46 +000069 GNU pth is a user-space threading library, and as such, all threads
70 run within the same process. In this case, if the currently running
71 thread is not the main_thread, send the signal to the main_thread.
Guido van Rossumbb4ba121994-06-23 11:25:45 +000072*/
73
74#ifdef WITH_THREAD
Guido van Rossum295b8e51997-06-06 21:16:41 +000075#include <sys/types.h> /* For pid_t */
Guido van Rossum49b56061998-10-01 20:42:43 +000076#include "pythread.h"
Guido van Rossumbb4ba121994-06-23 11:25:45 +000077static long main_thread;
78static pid_t main_pid;
79#endif
80
Barry Warsaw92971171997-01-03 00:14:25 +000081static struct {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000082 int tripped;
83 PyObject *func;
Barry Warsaw92971171997-01-03 00:14:25 +000084} Handlers[NSIG];
Guido van Rossum398d9fe1994-05-11 08:59:13 +000085
Christian Heimes5fb7c2a2007-12-24 08:52:31 +000086static sig_atomic_t wakeup_fd = -1;
87
Christian Heimesb76922a2007-12-11 01:06:40 +000088/* Speed up sigcheck() when none tripped */
89static volatile sig_atomic_t is_tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +000090
Barry Warsaw92971171997-01-03 00:14:25 +000091static PyObject *DefaultHandler;
92static PyObject *IgnoreHandler;
93static PyObject *IntHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +000094
Martin v. Löwisf58de1b2001-03-06 12:13:56 +000095/* On Solaris 8, gcc will produce a warning that the function
96 declaration is not a prototype. This is caused by the definition of
97 SIG_DFL as (void (*)())0; the correct declaration would have been
98 (void (*)(int))0. */
99
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000100static PyOS_sighandler_t old_siginthandler = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +0000101
Martin v. Löwis823725e2008-03-24 13:39:54 +0000102#ifdef HAVE_GETITIMER
103static PyObject *ItimerError;
104
105/* auxiliary functions for setitimer/getitimer */
106static void
107timeval_from_double(double d, struct timeval *tv)
108{
109 tv->tv_sec = floor(d);
110 tv->tv_usec = fmod(d, 1.0) * 1000000.0;
111}
112
Christian Heimes1a8501c2008-10-02 19:56:01 +0000113Py_LOCAL_INLINE(double)
Martin v. Löwis823725e2008-03-24 13:39:54 +0000114double_from_timeval(struct timeval *tv)
115{
116 return tv->tv_sec + (double)(tv->tv_usec / 1000000.0);
117}
118
119static PyObject *
120itimer_retval(struct itimerval *iv)
121{
122 PyObject *r, *v;
123
124 r = PyTuple_New(2);
125 if (r == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000126 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000127
128 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_value)))) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000129 Py_DECREF(r);
130 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000131 }
132
133 PyTuple_SET_ITEM(r, 0, v);
134
135 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_interval)))) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000136 Py_DECREF(r);
137 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000138 }
139
140 PyTuple_SET_ITEM(r, 1, v);
141
142 return r;
143}
144#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000145
Guido van Rossume4485b01994-09-07 14:32:49 +0000146static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000147signal_default_int_handler(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000148{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000149 PyErr_SetNone(PyExc_KeyboardInterrupt);
150 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000151}
152
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000153PyDoc_STRVAR(default_int_handler_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000154"default_int_handler(...)\n\
155\n\
Michael W. Hudson24ec2112004-06-17 15:55:53 +0000156The default handler for SIGINT installed by Python.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000157It raises KeyboardInterrupt.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000158
Thomas Wouters0796b002000-07-22 23:49:30 +0000159
160static int
161checksignals_witharg(void * unused)
162{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000163 return PyErr_CheckSignals();
Thomas Wouters0796b002000-07-22 23:49:30 +0000164}
165
Tim Peters4f1b2082000-07-23 21:18:09 +0000166static void
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000167signal_handler(int sig_num)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000168{
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000169#ifdef WITH_THREAD
Guido van Rossum9e8181b2000-09-19 00:46:46 +0000170#ifdef WITH_PTH
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000171 if (PyThread_get_thread_ident() != main_thread) {
172 pth_raise(*(pth_t *) main_thread, sig_num);
173 return;
174 }
Guido van Rossum9e8181b2000-09-19 00:46:46 +0000175#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000176 /* See NOTES section above */
177 if (getpid() == main_pid) {
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000178#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000179 Handlers[sig_num].tripped = 1;
180 /* Set is_tripped after setting .tripped, as it gets
181 cleared in PyErr_CheckSignals() before .tripped. */
182 is_tripped = 1;
183 Py_AddPendingCall(checksignals_witharg, NULL);
184 if (wakeup_fd != -1)
185 write(wakeup_fd, "\0", 1);
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000186#ifdef WITH_THREAD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000187 }
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000188#endif
Guido van Rossum602099a1994-09-14 13:32:22 +0000189#ifdef SIGCHLD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000190 if (sig_num == SIGCHLD) {
191 /* To avoid infinite recursion, this signal remains
192 reset until explicit re-instated.
193 Don't clear the 'func' field as it is our pointer
194 to the Python handler... */
195 return;
196 }
Guido van Rossum602099a1994-09-14 13:32:22 +0000197#endif
Jean-Paul Calderone9c39bc72010-05-09 03:25:16 +0000198#ifndef HAVE_SIGACTION
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000199 /* If the handler was not set up with sigaction, reinstall it. See
200 * Python/pythonrun.c for the implementation of PyOS_setsig which
201 * makes this true. See also issue8354. */
202 PyOS_setsig(sig_num, signal_handler);
Jean-Paul Calderone9c39bc72010-05-09 03:25:16 +0000203#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000204}
Guido van Rossume4485b01994-09-07 14:32:49 +0000205
Guido van Rossum06d511d1995-03-10 15:13:48 +0000206
Guido van Rossum1171ee61997-08-22 20:42:00 +0000207#ifdef HAVE_ALARM
Guido van Rossume4485b01994-09-07 14:32:49 +0000208static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000209signal_alarm(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000210{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000211 int t;
212 if (!PyArg_ParseTuple(args, "i:alarm", &t))
213 return NULL;
214 /* alarm() returns the number of seconds remaining */
215 return PyLong_FromLong((long)alarm(t));
Guido van Rossumaa0f4c71994-08-23 13:49:37 +0000216}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000217
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000218PyDoc_STRVAR(alarm_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000219"alarm(seconds)\n\
220\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000221Arrange for SIGALRM to arrive after the given number of seconds.");
Guido van Rossum06d511d1995-03-10 15:13:48 +0000222#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000223
Guido van Rossum1171ee61997-08-22 20:42:00 +0000224#ifdef HAVE_PAUSE
Guido van Rossuma597dde1995-01-10 20:56:29 +0000225static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000226signal_pause(PyObject *self)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000227{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000228 Py_BEGIN_ALLOW_THREADS
229 (void)pause();
230 Py_END_ALLOW_THREADS
231 /* make sure that any exceptions that got raised are propagated
232 * back into Python
233 */
234 if (PyErr_CheckSignals())
235 return NULL;
Barry Warsaw92971171997-01-03 00:14:25 +0000236
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000237 Py_INCREF(Py_None);
238 return Py_None;
Guido van Rossume4485b01994-09-07 14:32:49 +0000239}
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000240PyDoc_STRVAR(pause_doc,
Barry Warsaw1ee36ff1998-07-21 22:41:18 +0000241"pause()\n\
242\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000243Wait until a signal arrives.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000244
Guido van Rossum06d511d1995-03-10 15:13:48 +0000245#endif
Guido van Rossume4485b01994-09-07 14:32:49 +0000246
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000247
Guido van Rossume4485b01994-09-07 14:32:49 +0000248static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000249signal_signal(PyObject *self, PyObject *args)
Guido van Rossume4485b01994-09-07 14:32:49 +0000250{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000251 PyObject *obj;
252 int sig_num;
253 PyObject *old_handler;
254 void (*func)(int);
255 if (!PyArg_ParseTuple(args, "iO:signal", &sig_num, &obj))
256 return NULL;
Brian Curtin3f004b12010-08-06 19:34:52 +0000257#ifdef MS_WINDOWS
258 /* Validate that sig_num is one of the allowable signals */
Brian Curtin912443c2010-09-06 16:10:04 +0000259 switch (sig_num) {
260 case SIGABRT: break;
261 case SIGFPE: break;
262 case SIGILL: break;
263 case SIGINT: break;
264 case SIGSEGV: break;
265 case SIGTERM: break;
266 default:
267 PyErr_SetString(PyExc_ValueError, "invalid signal value");
268 return NULL;
Brian Curtin3f004b12010-08-06 19:34:52 +0000269 }
270#endif
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000271#ifdef WITH_THREAD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000272 if (PyThread_get_thread_ident() != main_thread) {
273 PyErr_SetString(PyExc_ValueError,
274 "signal only works in main thread");
275 return NULL;
276 }
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000277#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000278 if (sig_num < 1 || sig_num >= NSIG) {
279 PyErr_SetString(PyExc_ValueError,
280 "signal number out of range");
281 return NULL;
282 }
283 if (obj == IgnoreHandler)
284 func = SIG_IGN;
285 else if (obj == DefaultHandler)
286 func = SIG_DFL;
287 else if (!PyCallable_Check(obj)) {
288 PyErr_SetString(PyExc_TypeError,
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000289"signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000290 return NULL;
291 }
292 else
293 func = signal_handler;
294 if (PyOS_setsig(sig_num, func) == SIG_ERR) {
295 PyErr_SetFromErrno(PyExc_RuntimeError);
296 return NULL;
297 }
298 old_handler = Handlers[sig_num].func;
299 Handlers[sig_num].tripped = 0;
300 Py_INCREF(obj);
301 Handlers[sig_num].func = obj;
302 return old_handler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000303}
304
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000305PyDoc_STRVAR(signal_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000306"signal(sig, action) -> action\n\
307\n\
308Set the action for the given signal. The action can be SIG_DFL,\n\
309SIG_IGN, or a callable Python object. The previous action is\n\
310returned. See getsignal() for possible return values.\n\
311\n\
312*** IMPORTANT NOTICE ***\n\
313A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000314the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000315
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000316
Guido van Rossume4485b01994-09-07 14:32:49 +0000317static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000318signal_getsignal(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000319{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000320 int sig_num;
321 PyObject *old_handler;
322 if (!PyArg_ParseTuple(args, "i:getsignal", &sig_num))
323 return NULL;
324 if (sig_num < 1 || sig_num >= NSIG) {
325 PyErr_SetString(PyExc_ValueError,
326 "signal number out of range");
327 return NULL;
328 }
329 old_handler = Handlers[sig_num].func;
330 Py_INCREF(old_handler);
331 return old_handler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000332}
333
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000334PyDoc_STRVAR(getsignal_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000335"getsignal(sig) -> action\n\
336\n\
337Return the current action for the given signal. The return value can be:\n\
338SIG_IGN -- if the signal is being ignored\n\
339SIG_DFL -- if the default action for the signal is in effect\n\
340None -- if an unknown handler is in effect\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000341anything else -- the callable Python object used as a handler");
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000342
Christian Heimes8640e742008-02-23 16:23:06 +0000343#ifdef HAVE_SIGINTERRUPT
344PyDoc_STRVAR(siginterrupt_doc,
345"siginterrupt(sig, flag) -> None\n\
346change system call restart behaviour: if flag is False, system calls\n\
347will be restarted when interrupted by signal sig, else system calls\n\
348will be interrupted.");
349
350static PyObject *
351signal_siginterrupt(PyObject *self, PyObject *args)
352{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000353 int sig_num;
354 int flag;
Christian Heimes8640e742008-02-23 16:23:06 +0000355
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000356 if (!PyArg_ParseTuple(args, "ii:siginterrupt", &sig_num, &flag))
357 return NULL;
358 if (sig_num < 1 || sig_num >= NSIG) {
359 PyErr_SetString(PyExc_ValueError,
360 "signal number out of range");
361 return NULL;
362 }
363 if (siginterrupt(sig_num, flag)<0) {
364 PyErr_SetFromErrno(PyExc_RuntimeError);
365 return NULL;
366 }
Christian Heimes8640e742008-02-23 16:23:06 +0000367
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000368 Py_INCREF(Py_None);
369 return Py_None;
Christian Heimes8640e742008-02-23 16:23:06 +0000370}
371
372#endif
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000373
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000374static PyObject *
375signal_set_wakeup_fd(PyObject *self, PyObject *args)
376{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000377 struct stat buf;
378 int fd, old_fd;
379 if (!PyArg_ParseTuple(args, "i:set_wakeup_fd", &fd))
380 return NULL;
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000381#ifdef WITH_THREAD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000382 if (PyThread_get_thread_ident() != main_thread) {
383 PyErr_SetString(PyExc_ValueError,
384 "set_wakeup_fd only works in main thread");
385 return NULL;
386 }
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000387#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000388 if (fd != -1 && fstat(fd, &buf) != 0) {
389 PyErr_SetString(PyExc_ValueError, "invalid fd");
390 return NULL;
391 }
392 old_fd = wakeup_fd;
393 wakeup_fd = fd;
394 return PyLong_FromLong(old_fd);
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000395}
396
397PyDoc_STRVAR(set_wakeup_fd_doc,
398"set_wakeup_fd(fd) -> fd\n\
399\n\
400Sets the fd to be written to (with '\\0') when a signal\n\
401comes in. A library can use this to wakeup select or poll.\n\
402The previous fd is returned.\n\
403\n\
404The fd must be non-blocking.");
405
406/* C API for the same, without all the error checking */
407int
408PySignal_SetWakeupFd(int fd)
409{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000410 int old_fd = wakeup_fd;
411 if (fd < 0)
412 fd = -1;
413 wakeup_fd = fd;
414 return old_fd;
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000415}
416
417
Martin v. Löwis823725e2008-03-24 13:39:54 +0000418#ifdef HAVE_SETITIMER
419static PyObject *
420signal_setitimer(PyObject *self, PyObject *args)
421{
422 double first;
423 double interval = 0;
424 int which;
425 struct itimerval new, old;
426
427 if(!PyArg_ParseTuple(args, "id|d:setitimer", &which, &first, &interval))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000428 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000429
430 timeval_from_double(first, &new.it_value);
431 timeval_from_double(interval, &new.it_interval);
432 /* Let OS check "which" value */
433 if (setitimer(which, &new, &old) != 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000434 PyErr_SetFromErrno(ItimerError);
435 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000436 }
437
438 return itimer_retval(&old);
439}
440
441PyDoc_STRVAR(setitimer_doc,
442"setitimer(which, seconds[, interval])\n\
443\n\
444Sets given itimer (one of ITIMER_REAL, ITIMER_VIRTUAL\n\
445or ITIMER_PROF) to fire after value seconds and after\n\
446that every interval seconds.\n\
447The itimer can be cleared by setting seconds to zero.\n\
448\n\
449Returns old values as a tuple: (delay, interval).");
450#endif
451
452
453#ifdef HAVE_GETITIMER
454static PyObject *
455signal_getitimer(PyObject *self, PyObject *args)
456{
457 int which;
458 struct itimerval old;
459
460 if (!PyArg_ParseTuple(args, "i:getitimer", &which))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000461 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000462
463 if (getitimer(which, &old) != 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000464 PyErr_SetFromErrno(ItimerError);
465 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000466 }
467
468 return itimer_retval(&old);
469}
470
471PyDoc_STRVAR(getitimer_doc,
472"getitimer(which)\n\
473\n\
474Returns current value of given itimer.");
475#endif
476
477
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000478/* List of functions defined in the module */
Barry Warsaw92971171997-01-03 00:14:25 +0000479static PyMethodDef signal_methods[] = {
Guido van Rossum1171ee61997-08-22 20:42:00 +0000480#ifdef HAVE_ALARM
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000481 {"alarm", signal_alarm, METH_VARARGS, alarm_doc},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000482#endif
Martin v. Löwis823725e2008-03-24 13:39:54 +0000483#ifdef HAVE_SETITIMER
484 {"setitimer", signal_setitimer, METH_VARARGS, setitimer_doc},
485#endif
486#ifdef HAVE_GETITIMER
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000487 {"getitimer", signal_getitimer, METH_VARARGS, getitimer_doc},
Martin v. Löwis823725e2008-03-24 13:39:54 +0000488#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000489 {"signal", signal_signal, METH_VARARGS, signal_doc},
490 {"getsignal", signal_getsignal, METH_VARARGS, getsignal_doc},
491 {"set_wakeup_fd", signal_set_wakeup_fd, METH_VARARGS, set_wakeup_fd_doc},
Christian Heimes8640e742008-02-23 16:23:06 +0000492#ifdef HAVE_SIGINTERRUPT
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000493 {"siginterrupt", signal_siginterrupt, METH_VARARGS, siginterrupt_doc},
Christian Heimes8640e742008-02-23 16:23:06 +0000494#endif
Guido van Rossum1171ee61997-08-22 20:42:00 +0000495#ifdef HAVE_PAUSE
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000496 {"pause", (PyCFunction)signal_pause,
497 METH_NOARGS,pause_doc},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000498#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000499 {"default_int_handler", signal_default_int_handler,
500 METH_VARARGS, default_int_handler_doc},
501 {NULL, NULL} /* sentinel */
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000502};
503
Barry Warsaw92971171997-01-03 00:14:25 +0000504
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000505PyDoc_STRVAR(module_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000506"This module provides mechanisms to use signal handlers in Python.\n\
507\n\
508Functions:\n\
509\n\
510alarm() -- cause SIGALRM after a specified time [Unix only]\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +0000511setitimer() -- cause a signal (described below) after a specified\n\
512 float time and the timer may restart then [Unix only]\n\
513getitimer() -- get current value of timer [Unix only]\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000514signal() -- set the action for a given signal\n\
515getsignal() -- get the signal action for a given signal\n\
516pause() -- wait until a signal arrives [Unix only]\n\
517default_int_handler() -- default SIGINT handler\n\
518\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +0000519signal constants:\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000520SIG_DFL -- used to refer to the system default handler\n\
521SIG_IGN -- used to ignore the signal\n\
522NSIG -- number of defined signals\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000523SIGINT, SIGTERM, etc. -- signal numbers\n\
524\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +0000525itimer constants:\n\
526ITIMER_REAL -- decrements in real time, and delivers SIGALRM upon\n\
527 expiration\n\
528ITIMER_VIRTUAL -- decrements only when the process is executing,\n\
529 and delivers SIGVTALRM upon expiration\n\
530ITIMER_PROF -- decrements both when the process is executing and\n\
531 when the system is executing on behalf of the process.\n\
532 Coupled with ITIMER_VIRTUAL, this timer is usually\n\
533 used to profile the time spent by the application\n\
534 in user and kernel space. SIGPROF is delivered upon\n\
535 expiration.\n\
536\n\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000537*** IMPORTANT NOTICE ***\n\
538A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000539the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000540
Martin v. Löwis1a214512008-06-11 05:26:20 +0000541static struct PyModuleDef signalmodule = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000542 PyModuleDef_HEAD_INIT,
543 "signal",
544 module_doc,
545 -1,
546 signal_methods,
547 NULL,
548 NULL,
549 NULL,
550 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000551};
552
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000553PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000554PyInit_signal(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000555{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000556 PyObject *m, *d, *x;
557 int i;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000558
559#ifdef WITH_THREAD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000560 main_thread = PyThread_get_thread_ident();
561 main_pid = getpid();
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000562#endif
563
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000564 /* Create the module and add the functions */
565 m = PyModule_Create(&signalmodule);
566 if (m == NULL)
567 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000568
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000569 /* Add some symbolic constants to the module */
570 d = PyModule_GetDict(m);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000571
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000572 x = DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL);
573 if (!x || PyDict_SetItemString(d, "SIG_DFL", x) < 0)
574 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000575
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000576 x = IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN);
577 if (!x || PyDict_SetItemString(d, "SIG_IGN", x) < 0)
578 goto finally;
Barry Warsaw92971171997-01-03 00:14:25 +0000579
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000580 x = PyLong_FromLong((long)NSIG);
581 if (!x || PyDict_SetItemString(d, "NSIG", x) < 0)
582 goto finally;
583 Py_DECREF(x);
Barry Warsaw92971171997-01-03 00:14:25 +0000584
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000585 x = IntHandler = PyDict_GetItemString(d, "default_int_handler");
586 if (!x)
587 goto finally;
588 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +0000589
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000590 Handlers[0].tripped = 0;
591 for (i = 1; i < NSIG; i++) {
592 void (*t)(int);
593 t = PyOS_getsig(i);
594 Handlers[i].tripped = 0;
595 if (t == SIG_DFL)
596 Handlers[i].func = DefaultHandler;
597 else if (t == SIG_IGN)
598 Handlers[i].func = IgnoreHandler;
599 else
600 Handlers[i].func = Py_None; /* None of our business */
601 Py_INCREF(Handlers[i].func);
602 }
603 if (Handlers[SIGINT].func == DefaultHandler) {
604 /* Install default int handler */
605 Py_INCREF(IntHandler);
606 Py_DECREF(Handlers[SIGINT].func);
607 Handlers[SIGINT].func = IntHandler;
608 old_siginthandler = PyOS_setsig(SIGINT, signal_handler);
609 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000610
611#ifdef SIGHUP
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000612 x = PyLong_FromLong(SIGHUP);
613 PyDict_SetItemString(d, "SIGHUP", x);
614 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000615#endif
616#ifdef SIGINT
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000617 x = PyLong_FromLong(SIGINT);
618 PyDict_SetItemString(d, "SIGINT", x);
619 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000620#endif
Tim Peters1ce3cf72001-10-01 17:58:40 +0000621#ifdef SIGBREAK
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000622 x = PyLong_FromLong(SIGBREAK);
623 PyDict_SetItemString(d, "SIGBREAK", x);
624 Py_XDECREF(x);
Tim Peters1ce3cf72001-10-01 17:58:40 +0000625#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000626#ifdef SIGQUIT
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000627 x = PyLong_FromLong(SIGQUIT);
628 PyDict_SetItemString(d, "SIGQUIT", x);
629 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000630#endif
631#ifdef SIGILL
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000632 x = PyLong_FromLong(SIGILL);
633 PyDict_SetItemString(d, "SIGILL", x);
634 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000635#endif
636#ifdef SIGTRAP
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000637 x = PyLong_FromLong(SIGTRAP);
638 PyDict_SetItemString(d, "SIGTRAP", x);
639 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000640#endif
641#ifdef SIGIOT
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000642 x = PyLong_FromLong(SIGIOT);
643 PyDict_SetItemString(d, "SIGIOT", x);
644 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000645#endif
646#ifdef SIGABRT
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000647 x = PyLong_FromLong(SIGABRT);
648 PyDict_SetItemString(d, "SIGABRT", x);
649 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000650#endif
651#ifdef SIGEMT
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000652 x = PyLong_FromLong(SIGEMT);
653 PyDict_SetItemString(d, "SIGEMT", x);
654 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000655#endif
656#ifdef SIGFPE
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000657 x = PyLong_FromLong(SIGFPE);
658 PyDict_SetItemString(d, "SIGFPE", x);
659 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000660#endif
661#ifdef SIGKILL
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000662 x = PyLong_FromLong(SIGKILL);
663 PyDict_SetItemString(d, "SIGKILL", x);
664 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000665#endif
666#ifdef SIGBUS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000667 x = PyLong_FromLong(SIGBUS);
668 PyDict_SetItemString(d, "SIGBUS", x);
669 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000670#endif
671#ifdef SIGSEGV
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000672 x = PyLong_FromLong(SIGSEGV);
673 PyDict_SetItemString(d, "SIGSEGV", x);
674 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000675#endif
676#ifdef SIGSYS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000677 x = PyLong_FromLong(SIGSYS);
678 PyDict_SetItemString(d, "SIGSYS", x);
679 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000680#endif
681#ifdef SIGPIPE
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000682 x = PyLong_FromLong(SIGPIPE);
683 PyDict_SetItemString(d, "SIGPIPE", x);
684 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000685#endif
686#ifdef SIGALRM
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000687 x = PyLong_FromLong(SIGALRM);
688 PyDict_SetItemString(d, "SIGALRM", x);
689 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000690#endif
691#ifdef SIGTERM
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000692 x = PyLong_FromLong(SIGTERM);
693 PyDict_SetItemString(d, "SIGTERM", x);
694 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000695#endif
696#ifdef SIGUSR1
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000697 x = PyLong_FromLong(SIGUSR1);
698 PyDict_SetItemString(d, "SIGUSR1", x);
699 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000700#endif
701#ifdef SIGUSR2
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000702 x = PyLong_FromLong(SIGUSR2);
703 PyDict_SetItemString(d, "SIGUSR2", x);
704 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000705#endif
706#ifdef SIGCLD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000707 x = PyLong_FromLong(SIGCLD);
708 PyDict_SetItemString(d, "SIGCLD", x);
709 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000710#endif
711#ifdef SIGCHLD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000712 x = PyLong_FromLong(SIGCHLD);
713 PyDict_SetItemString(d, "SIGCHLD", x);
714 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000715#endif
716#ifdef SIGPWR
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000717 x = PyLong_FromLong(SIGPWR);
718 PyDict_SetItemString(d, "SIGPWR", x);
719 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000720#endif
721#ifdef SIGIO
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000722 x = PyLong_FromLong(SIGIO);
723 PyDict_SetItemString(d, "SIGIO", x);
724 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000725#endif
726#ifdef SIGURG
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000727 x = PyLong_FromLong(SIGURG);
728 PyDict_SetItemString(d, "SIGURG", x);
729 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000730#endif
731#ifdef SIGWINCH
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000732 x = PyLong_FromLong(SIGWINCH);
733 PyDict_SetItemString(d, "SIGWINCH", x);
734 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000735#endif
736#ifdef SIGPOLL
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000737 x = PyLong_FromLong(SIGPOLL);
738 PyDict_SetItemString(d, "SIGPOLL", x);
739 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000740#endif
741#ifdef SIGSTOP
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000742 x = PyLong_FromLong(SIGSTOP);
743 PyDict_SetItemString(d, "SIGSTOP", x);
744 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000745#endif
746#ifdef SIGTSTP
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000747 x = PyLong_FromLong(SIGTSTP);
748 PyDict_SetItemString(d, "SIGTSTP", x);
749 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000750#endif
751#ifdef SIGCONT
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000752 x = PyLong_FromLong(SIGCONT);
753 PyDict_SetItemString(d, "SIGCONT", x);
754 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000755#endif
756#ifdef SIGTTIN
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000757 x = PyLong_FromLong(SIGTTIN);
758 PyDict_SetItemString(d, "SIGTTIN", x);
759 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000760#endif
761#ifdef SIGTTOU
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000762 x = PyLong_FromLong(SIGTTOU);
763 PyDict_SetItemString(d, "SIGTTOU", x);
764 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000765#endif
766#ifdef SIGVTALRM
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000767 x = PyLong_FromLong(SIGVTALRM);
768 PyDict_SetItemString(d, "SIGVTALRM", x);
769 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000770#endif
771#ifdef SIGPROF
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000772 x = PyLong_FromLong(SIGPROF);
773 PyDict_SetItemString(d, "SIGPROF", x);
774 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000775#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000776#ifdef SIGXCPU
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000777 x = PyLong_FromLong(SIGXCPU);
778 PyDict_SetItemString(d, "SIGXCPU", x);
779 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000780#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000781#ifdef SIGXFSZ
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000782 x = PyLong_FromLong(SIGXFSZ);
783 PyDict_SetItemString(d, "SIGXFSZ", x);
784 Py_XDECREF(x);
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000785#endif
Anthony Baxterf37f37d2003-07-31 10:35:29 +0000786#ifdef SIGRTMIN
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000787 x = PyLong_FromLong(SIGRTMIN);
788 PyDict_SetItemString(d, "SIGRTMIN", x);
789 Py_XDECREF(x);
Anthony Baxterf37f37d2003-07-31 10:35:29 +0000790#endif
791#ifdef SIGRTMAX
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000792 x = PyLong_FromLong(SIGRTMAX);
793 PyDict_SetItemString(d, "SIGRTMAX", x);
794 Py_XDECREF(x);
Anthony Baxterf37f37d2003-07-31 10:35:29 +0000795#endif
Martin v. Löwis175af252002-01-12 11:43:25 +0000796#ifdef SIGINFO
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000797 x = PyLong_FromLong(SIGINFO);
798 PyDict_SetItemString(d, "SIGINFO", x);
799 Py_XDECREF(x);
Martin v. Löwis175af252002-01-12 11:43:25 +0000800#endif
Martin v. Löwis823725e2008-03-24 13:39:54 +0000801
802#ifdef ITIMER_REAL
803 x = PyLong_FromLong(ITIMER_REAL);
804 PyDict_SetItemString(d, "ITIMER_REAL", x);
805 Py_DECREF(x);
806#endif
807#ifdef ITIMER_VIRTUAL
808 x = PyLong_FromLong(ITIMER_VIRTUAL);
809 PyDict_SetItemString(d, "ITIMER_VIRTUAL", x);
810 Py_DECREF(x);
811#endif
812#ifdef ITIMER_PROF
813 x = PyLong_FromLong(ITIMER_PROF);
814 PyDict_SetItemString(d, "ITIMER_PROF", x);
815 Py_DECREF(x);
816#endif
817
818#if defined (HAVE_SETITIMER) || defined (HAVE_GETITIMER)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000819 ItimerError = PyErr_NewException("signal.ItimerError",
820 PyExc_IOError, NULL);
Neal Norwitz2f99b242008-08-24 05:48:10 +0000821 if (ItimerError != NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000822 PyDict_SetItemString(d, "ItimerError", ItimerError);
Martin v. Löwis823725e2008-03-24 13:39:54 +0000823#endif
824
Martin v. Löwis1a214512008-06-11 05:26:20 +0000825 if (PyErr_Occurred()) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000826 Py_DECREF(m);
827 m = NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000828 }
Barry Warsaw92971171997-01-03 00:14:25 +0000829
Barry Warsaw92971171997-01-03 00:14:25 +0000830 finally:
Martin v. Löwis1a214512008-06-11 05:26:20 +0000831 return m;
Guido van Rossum08c16611997-08-02 03:01:42 +0000832}
833
834static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000835finisignal(void)
Guido van Rossum08c16611997-08-02 03:01:42 +0000836{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000837 int i;
838 PyObject *func;
Guido van Rossum08c16611997-08-02 03:01:42 +0000839
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000840 PyOS_setsig(SIGINT, old_siginthandler);
841 old_siginthandler = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +0000842
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000843 for (i = 1; i < NSIG; i++) {
844 func = Handlers[i].func;
845 Handlers[i].tripped = 0;
846 Handlers[i].func = NULL;
847 if (i != SIGINT && func != NULL && func != Py_None &&
848 func != DefaultHandler && func != IgnoreHandler)
849 PyOS_setsig(i, SIG_DFL);
850 Py_XDECREF(func);
851 }
Guido van Rossum08c16611997-08-02 03:01:42 +0000852
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000853 Py_XDECREF(IntHandler);
854 IntHandler = NULL;
855 Py_XDECREF(DefaultHandler);
856 DefaultHandler = NULL;
857 Py_XDECREF(IgnoreHandler);
858 IgnoreHandler = NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000859}
860
Barry Warsaw92971171997-01-03 00:14:25 +0000861
Barry Warsaw92971171997-01-03 00:14:25 +0000862/* Declared in pyerrors.h */
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000863int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000864PyErr_CheckSignals(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000865{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000866 int i;
867 PyObject *f;
Barry Warsaw92971171997-01-03 00:14:25 +0000868
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000869 if (!is_tripped)
870 return 0;
Christian Heimesb76922a2007-12-11 01:06:40 +0000871
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000872#ifdef WITH_THREAD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000873 if (PyThread_get_thread_ident() != main_thread)
874 return 0;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000875#endif
Christian Heimesb76922a2007-12-11 01:06:40 +0000876
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000877 /*
878 * The is_tripped variable is meant to speed up the calls to
879 * PyErr_CheckSignals (both directly or via pending calls) when no
880 * signal has arrived. This variable is set to 1 when a signal arrives
881 * and it is set to 0 here, when we know some signals arrived. This way
882 * we can run the registered handlers with no signals blocked.
883 *
884 * NOTE: with this approach we can have a situation where is_tripped is
885 * 1 but we have no more signals to handle (Handlers[i].tripped
886 * is 0 for every signal i). This won't do us any harm (except
887 * we're gonna spent some cycles for nothing). This happens when
888 * we receive a signal i after we zero is_tripped and before we
889 * check Handlers[i].tripped.
890 */
891 is_tripped = 0;
Christian Heimesb76922a2007-12-11 01:06:40 +0000892
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000893 if (!(f = (PyObject *)PyEval_GetFrame()))
894 f = Py_None;
Christian Heimesb76922a2007-12-11 01:06:40 +0000895
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000896 for (i = 1; i < NSIG; i++) {
897 if (Handlers[i].tripped) {
898 PyObject *result = NULL;
899 PyObject *arglist = Py_BuildValue("(iO)", i, f);
900 Handlers[i].tripped = 0;
Barry Warsaw92971171997-01-03 00:14:25 +0000901
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000902 if (arglist) {
903 result = PyEval_CallObject(Handlers[i].func,
904 arglist);
905 Py_DECREF(arglist);
906 }
907 if (!result)
908 return -1;
Barry Warsaw92971171997-01-03 00:14:25 +0000909
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000910 Py_DECREF(result);
911 }
912 }
Christian Heimesb76922a2007-12-11 01:06:40 +0000913
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000914 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000915}
916
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000917
Barry Warsaw92971171997-01-03 00:14:25 +0000918/* Replacements for intrcheck.c functionality
919 * Declared in pyerrors.h
920 */
921void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000922PyErr_SetInterrupt(void)
Barry Warsaw92971171997-01-03 00:14:25 +0000923{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000924 is_tripped = 1;
925 Handlers[SIGINT].tripped = 1;
926 Py_AddPendingCall((int (*)(void *))PyErr_CheckSignals, NULL);
Barry Warsaw92971171997-01-03 00:14:25 +0000927}
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000928
929void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000930PyOS_InitInterrupts(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000931{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000932 PyObject *m = PyInit_signal();
933 if (m) {
934 _PyImport_FixupExtension(m, "signal", "signal");
935 Py_DECREF(m);
936 }
Guido van Rossum08c16611997-08-02 03:01:42 +0000937}
938
939void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000940PyOS_FiniInterrupts(void)
Guido van Rossum08c16611997-08-02 03:01:42 +0000941{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000942 finisignal();
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000943}
944
945int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000946PyOS_InterruptOccurred(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000947{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000948 if (Handlers[SIGINT].tripped) {
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000949#ifdef WITH_THREAD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000950 if (PyThread_get_thread_ident() != main_thread)
951 return 0;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000952#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000953 Handlers[SIGINT].tripped = 0;
954 return 1;
955 }
956 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000957}
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000958
959void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000960PyOS_AfterFork(void)
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000961{
962#ifdef WITH_THREAD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000963 PyEval_ReInitThreads();
964 main_thread = PyThread_get_thread_ident();
965 main_pid = getpid();
966 _PyImport_ReInitLock();
967 PyThread_ReInitTLS();
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000968#endif
969}