blob: 251e67ca115bb1a89d0b5eba3b52edd54b340f6e [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);
Brian Curtin3f004b12010-08-06 19:34:52 +0000252#ifdef MS_WINDOWS
253 int cur_sig, num_valid_sigs = 6;
254 static int valid_sigs[] = {SIGABRT, SIGFPE, SIGILL, SIGINT,
255 SIGSEGV, SIGTERM};
256 BOOL valid_sig = FALSE;
257#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000258 if (!PyArg_ParseTuple(args, "iO:signal", &sig_num, &obj))
259 return NULL;
Brian Curtin3f004b12010-08-06 19:34:52 +0000260#ifdef MS_WINDOWS
261 /* Validate that sig_num is one of the allowable signals */
262 for (cur_sig = 0; cur_sig < num_valid_sigs; cur_sig++)
263 valid_sig |= (sig_num == valid_sigs[cur_sig]);
264 if (!valid_sig) {
265 PyErr_SetString(PyExc_ValueError, "signal number out of range");
266 return NULL;
267 }
268#endif
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000269#ifdef WITH_THREAD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000270 if (PyThread_get_thread_ident() != main_thread) {
271 PyErr_SetString(PyExc_ValueError,
272 "signal only works in main thread");
273 return NULL;
274 }
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000275#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000276 if (sig_num < 1 || sig_num >= NSIG) {
277 PyErr_SetString(PyExc_ValueError,
278 "signal number out of range");
279 return NULL;
280 }
281 if (obj == IgnoreHandler)
282 func = SIG_IGN;
283 else if (obj == DefaultHandler)
284 func = SIG_DFL;
285 else if (!PyCallable_Check(obj)) {
286 PyErr_SetString(PyExc_TypeError,
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000287"signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000288 return NULL;
289 }
290 else
291 func = signal_handler;
292 if (PyOS_setsig(sig_num, func) == SIG_ERR) {
293 PyErr_SetFromErrno(PyExc_RuntimeError);
294 return NULL;
295 }
296 old_handler = Handlers[sig_num].func;
297 Handlers[sig_num].tripped = 0;
298 Py_INCREF(obj);
299 Handlers[sig_num].func = obj;
300 return old_handler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000301}
302
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000303PyDoc_STRVAR(signal_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000304"signal(sig, action) -> action\n\
305\n\
306Set the action for the given signal. The action can be SIG_DFL,\n\
307SIG_IGN, or a callable Python object. The previous action is\n\
308returned. See getsignal() for possible return values.\n\
309\n\
310*** IMPORTANT NOTICE ***\n\
311A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000312the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000313
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000314
Guido van Rossume4485b01994-09-07 14:32:49 +0000315static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000316signal_getsignal(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000317{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000318 int sig_num;
319 PyObject *old_handler;
320 if (!PyArg_ParseTuple(args, "i:getsignal", &sig_num))
321 return NULL;
322 if (sig_num < 1 || sig_num >= NSIG) {
323 PyErr_SetString(PyExc_ValueError,
324 "signal number out of range");
325 return NULL;
326 }
327 old_handler = Handlers[sig_num].func;
328 Py_INCREF(old_handler);
329 return old_handler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000330}
331
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000332PyDoc_STRVAR(getsignal_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000333"getsignal(sig) -> action\n\
334\n\
335Return the current action for the given signal. The return value can be:\n\
336SIG_IGN -- if the signal is being ignored\n\
337SIG_DFL -- if the default action for the signal is in effect\n\
338None -- if an unknown handler is in effect\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000339anything else -- the callable Python object used as a handler");
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000340
Christian Heimes8640e742008-02-23 16:23:06 +0000341#ifdef HAVE_SIGINTERRUPT
342PyDoc_STRVAR(siginterrupt_doc,
343"siginterrupt(sig, flag) -> None\n\
344change system call restart behaviour: if flag is False, system calls\n\
345will be restarted when interrupted by signal sig, else system calls\n\
346will be interrupted.");
347
348static PyObject *
349signal_siginterrupt(PyObject *self, PyObject *args)
350{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000351 int sig_num;
352 int flag;
Christian Heimes8640e742008-02-23 16:23:06 +0000353
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000354 if (!PyArg_ParseTuple(args, "ii:siginterrupt", &sig_num, &flag))
355 return NULL;
356 if (sig_num < 1 || sig_num >= NSIG) {
357 PyErr_SetString(PyExc_ValueError,
358 "signal number out of range");
359 return NULL;
360 }
361 if (siginterrupt(sig_num, flag)<0) {
362 PyErr_SetFromErrno(PyExc_RuntimeError);
363 return NULL;
364 }
Christian Heimes8640e742008-02-23 16:23:06 +0000365
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000366 Py_INCREF(Py_None);
367 return Py_None;
Christian Heimes8640e742008-02-23 16:23:06 +0000368}
369
370#endif
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000371
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000372static PyObject *
373signal_set_wakeup_fd(PyObject *self, PyObject *args)
374{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000375 struct stat buf;
376 int fd, old_fd;
377 if (!PyArg_ParseTuple(args, "i:set_wakeup_fd", &fd))
378 return NULL;
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000379#ifdef WITH_THREAD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000380 if (PyThread_get_thread_ident() != main_thread) {
381 PyErr_SetString(PyExc_ValueError,
382 "set_wakeup_fd only works in main thread");
383 return NULL;
384 }
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000385#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000386 if (fd != -1 && fstat(fd, &buf) != 0) {
387 PyErr_SetString(PyExc_ValueError, "invalid fd");
388 return NULL;
389 }
390 old_fd = wakeup_fd;
391 wakeup_fd = fd;
392 return PyLong_FromLong(old_fd);
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000393}
394
395PyDoc_STRVAR(set_wakeup_fd_doc,
396"set_wakeup_fd(fd) -> fd\n\
397\n\
398Sets the fd to be written to (with '\\0') when a signal\n\
399comes in. A library can use this to wakeup select or poll.\n\
400The previous fd is returned.\n\
401\n\
402The fd must be non-blocking.");
403
404/* C API for the same, without all the error checking */
405int
406PySignal_SetWakeupFd(int fd)
407{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000408 int old_fd = wakeup_fd;
409 if (fd < 0)
410 fd = -1;
411 wakeup_fd = fd;
412 return old_fd;
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000413}
414
415
Martin v. Löwis823725e2008-03-24 13:39:54 +0000416#ifdef HAVE_SETITIMER
417static PyObject *
418signal_setitimer(PyObject *self, PyObject *args)
419{
420 double first;
421 double interval = 0;
422 int which;
423 struct itimerval new, old;
424
425 if(!PyArg_ParseTuple(args, "id|d:setitimer", &which, &first, &interval))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000426 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000427
428 timeval_from_double(first, &new.it_value);
429 timeval_from_double(interval, &new.it_interval);
430 /* Let OS check "which" value */
431 if (setitimer(which, &new, &old) != 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000432 PyErr_SetFromErrno(ItimerError);
433 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000434 }
435
436 return itimer_retval(&old);
437}
438
439PyDoc_STRVAR(setitimer_doc,
440"setitimer(which, seconds[, interval])\n\
441\n\
442Sets given itimer (one of ITIMER_REAL, ITIMER_VIRTUAL\n\
443or ITIMER_PROF) to fire after value seconds and after\n\
444that every interval seconds.\n\
445The itimer can be cleared by setting seconds to zero.\n\
446\n\
447Returns old values as a tuple: (delay, interval).");
448#endif
449
450
451#ifdef HAVE_GETITIMER
452static PyObject *
453signal_getitimer(PyObject *self, PyObject *args)
454{
455 int which;
456 struct itimerval old;
457
458 if (!PyArg_ParseTuple(args, "i:getitimer", &which))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000459 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000460
461 if (getitimer(which, &old) != 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000462 PyErr_SetFromErrno(ItimerError);
463 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000464 }
465
466 return itimer_retval(&old);
467}
468
469PyDoc_STRVAR(getitimer_doc,
470"getitimer(which)\n\
471\n\
472Returns current value of given itimer.");
473#endif
474
475
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000476/* List of functions defined in the module */
Barry Warsaw92971171997-01-03 00:14:25 +0000477static PyMethodDef signal_methods[] = {
Guido van Rossum1171ee61997-08-22 20:42:00 +0000478#ifdef HAVE_ALARM
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000479 {"alarm", signal_alarm, METH_VARARGS, alarm_doc},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000480#endif
Martin v. Löwis823725e2008-03-24 13:39:54 +0000481#ifdef HAVE_SETITIMER
482 {"setitimer", signal_setitimer, METH_VARARGS, setitimer_doc},
483#endif
484#ifdef HAVE_GETITIMER
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000485 {"getitimer", signal_getitimer, METH_VARARGS, getitimer_doc},
Martin v. Löwis823725e2008-03-24 13:39:54 +0000486#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000487 {"signal", signal_signal, METH_VARARGS, signal_doc},
488 {"getsignal", signal_getsignal, METH_VARARGS, getsignal_doc},
489 {"set_wakeup_fd", signal_set_wakeup_fd, METH_VARARGS, set_wakeup_fd_doc},
Christian Heimes8640e742008-02-23 16:23:06 +0000490#ifdef HAVE_SIGINTERRUPT
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000491 {"siginterrupt", signal_siginterrupt, METH_VARARGS, siginterrupt_doc},
Christian Heimes8640e742008-02-23 16:23:06 +0000492#endif
Guido van Rossum1171ee61997-08-22 20:42:00 +0000493#ifdef HAVE_PAUSE
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000494 {"pause", (PyCFunction)signal_pause,
495 METH_NOARGS,pause_doc},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000496#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000497 {"default_int_handler", signal_default_int_handler,
498 METH_VARARGS, default_int_handler_doc},
499 {NULL, NULL} /* sentinel */
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000500};
501
Barry Warsaw92971171997-01-03 00:14:25 +0000502
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000503PyDoc_STRVAR(module_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000504"This module provides mechanisms to use signal handlers in Python.\n\
505\n\
506Functions:\n\
507\n\
508alarm() -- cause SIGALRM after a specified time [Unix only]\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +0000509setitimer() -- cause a signal (described below) after a specified\n\
510 float time and the timer may restart then [Unix only]\n\
511getitimer() -- get current value of timer [Unix only]\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000512signal() -- set the action for a given signal\n\
513getsignal() -- get the signal action for a given signal\n\
514pause() -- wait until a signal arrives [Unix only]\n\
515default_int_handler() -- default SIGINT handler\n\
516\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +0000517signal constants:\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000518SIG_DFL -- used to refer to the system default handler\n\
519SIG_IGN -- used to ignore the signal\n\
520NSIG -- number of defined signals\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000521SIGINT, SIGTERM, etc. -- signal numbers\n\
522\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +0000523itimer constants:\n\
524ITIMER_REAL -- decrements in real time, and delivers SIGALRM upon\n\
525 expiration\n\
526ITIMER_VIRTUAL -- decrements only when the process is executing,\n\
527 and delivers SIGVTALRM upon expiration\n\
528ITIMER_PROF -- decrements both when the process is executing and\n\
529 when the system is executing on behalf of the process.\n\
530 Coupled with ITIMER_VIRTUAL, this timer is usually\n\
531 used to profile the time spent by the application\n\
532 in user and kernel space. SIGPROF is delivered upon\n\
533 expiration.\n\
534\n\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000535*** IMPORTANT NOTICE ***\n\
536A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000537the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000538
Martin v. Löwis1a214512008-06-11 05:26:20 +0000539static struct PyModuleDef signalmodule = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000540 PyModuleDef_HEAD_INIT,
541 "signal",
542 module_doc,
543 -1,
544 signal_methods,
545 NULL,
546 NULL,
547 NULL,
548 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000549};
550
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000551PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000552PyInit_signal(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000553{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000554 PyObject *m, *d, *x;
555 int i;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000556
557#ifdef WITH_THREAD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000558 main_thread = PyThread_get_thread_ident();
559 main_pid = getpid();
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000560#endif
561
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000562 /* Create the module and add the functions */
563 m = PyModule_Create(&signalmodule);
564 if (m == NULL)
565 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000566
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000567 /* Add some symbolic constants to the module */
568 d = PyModule_GetDict(m);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000569
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000570 x = DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL);
571 if (!x || PyDict_SetItemString(d, "SIG_DFL", x) < 0)
572 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000573
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000574 x = IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN);
575 if (!x || PyDict_SetItemString(d, "SIG_IGN", x) < 0)
576 goto finally;
Barry Warsaw92971171997-01-03 00:14:25 +0000577
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000578 x = PyLong_FromLong((long)NSIG);
579 if (!x || PyDict_SetItemString(d, "NSIG", x) < 0)
580 goto finally;
581 Py_DECREF(x);
Barry Warsaw92971171997-01-03 00:14:25 +0000582
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000583 x = IntHandler = PyDict_GetItemString(d, "default_int_handler");
584 if (!x)
585 goto finally;
586 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +0000587
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000588 Handlers[0].tripped = 0;
589 for (i = 1; i < NSIG; i++) {
590 void (*t)(int);
591 t = PyOS_getsig(i);
592 Handlers[i].tripped = 0;
593 if (t == SIG_DFL)
594 Handlers[i].func = DefaultHandler;
595 else if (t == SIG_IGN)
596 Handlers[i].func = IgnoreHandler;
597 else
598 Handlers[i].func = Py_None; /* None of our business */
599 Py_INCREF(Handlers[i].func);
600 }
601 if (Handlers[SIGINT].func == DefaultHandler) {
602 /* Install default int handler */
603 Py_INCREF(IntHandler);
604 Py_DECREF(Handlers[SIGINT].func);
605 Handlers[SIGINT].func = IntHandler;
606 old_siginthandler = PyOS_setsig(SIGINT, signal_handler);
607 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000608
609#ifdef SIGHUP
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000610 x = PyLong_FromLong(SIGHUP);
611 PyDict_SetItemString(d, "SIGHUP", x);
612 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000613#endif
614#ifdef SIGINT
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000615 x = PyLong_FromLong(SIGINT);
616 PyDict_SetItemString(d, "SIGINT", x);
617 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000618#endif
Tim Peters1ce3cf72001-10-01 17:58:40 +0000619#ifdef SIGBREAK
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000620 x = PyLong_FromLong(SIGBREAK);
621 PyDict_SetItemString(d, "SIGBREAK", x);
622 Py_XDECREF(x);
Tim Peters1ce3cf72001-10-01 17:58:40 +0000623#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000624#ifdef SIGQUIT
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000625 x = PyLong_FromLong(SIGQUIT);
626 PyDict_SetItemString(d, "SIGQUIT", x);
627 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000628#endif
629#ifdef SIGILL
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000630 x = PyLong_FromLong(SIGILL);
631 PyDict_SetItemString(d, "SIGILL", x);
632 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000633#endif
634#ifdef SIGTRAP
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000635 x = PyLong_FromLong(SIGTRAP);
636 PyDict_SetItemString(d, "SIGTRAP", x);
637 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000638#endif
639#ifdef SIGIOT
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000640 x = PyLong_FromLong(SIGIOT);
641 PyDict_SetItemString(d, "SIGIOT", x);
642 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000643#endif
644#ifdef SIGABRT
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000645 x = PyLong_FromLong(SIGABRT);
646 PyDict_SetItemString(d, "SIGABRT", x);
647 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000648#endif
649#ifdef SIGEMT
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000650 x = PyLong_FromLong(SIGEMT);
651 PyDict_SetItemString(d, "SIGEMT", x);
652 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000653#endif
654#ifdef SIGFPE
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000655 x = PyLong_FromLong(SIGFPE);
656 PyDict_SetItemString(d, "SIGFPE", x);
657 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000658#endif
659#ifdef SIGKILL
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000660 x = PyLong_FromLong(SIGKILL);
661 PyDict_SetItemString(d, "SIGKILL", x);
662 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000663#endif
664#ifdef SIGBUS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000665 x = PyLong_FromLong(SIGBUS);
666 PyDict_SetItemString(d, "SIGBUS", x);
667 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000668#endif
669#ifdef SIGSEGV
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000670 x = PyLong_FromLong(SIGSEGV);
671 PyDict_SetItemString(d, "SIGSEGV", x);
672 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000673#endif
674#ifdef SIGSYS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000675 x = PyLong_FromLong(SIGSYS);
676 PyDict_SetItemString(d, "SIGSYS", x);
677 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000678#endif
679#ifdef SIGPIPE
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000680 x = PyLong_FromLong(SIGPIPE);
681 PyDict_SetItemString(d, "SIGPIPE", x);
682 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000683#endif
684#ifdef SIGALRM
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000685 x = PyLong_FromLong(SIGALRM);
686 PyDict_SetItemString(d, "SIGALRM", x);
687 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000688#endif
689#ifdef SIGTERM
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000690 x = PyLong_FromLong(SIGTERM);
691 PyDict_SetItemString(d, "SIGTERM", x);
692 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000693#endif
694#ifdef SIGUSR1
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000695 x = PyLong_FromLong(SIGUSR1);
696 PyDict_SetItemString(d, "SIGUSR1", x);
697 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000698#endif
699#ifdef SIGUSR2
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000700 x = PyLong_FromLong(SIGUSR2);
701 PyDict_SetItemString(d, "SIGUSR2", x);
702 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000703#endif
704#ifdef SIGCLD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000705 x = PyLong_FromLong(SIGCLD);
706 PyDict_SetItemString(d, "SIGCLD", x);
707 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000708#endif
709#ifdef SIGCHLD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000710 x = PyLong_FromLong(SIGCHLD);
711 PyDict_SetItemString(d, "SIGCHLD", x);
712 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000713#endif
714#ifdef SIGPWR
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000715 x = PyLong_FromLong(SIGPWR);
716 PyDict_SetItemString(d, "SIGPWR", x);
717 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000718#endif
719#ifdef SIGIO
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000720 x = PyLong_FromLong(SIGIO);
721 PyDict_SetItemString(d, "SIGIO", x);
722 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000723#endif
724#ifdef SIGURG
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000725 x = PyLong_FromLong(SIGURG);
726 PyDict_SetItemString(d, "SIGURG", x);
727 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000728#endif
729#ifdef SIGWINCH
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000730 x = PyLong_FromLong(SIGWINCH);
731 PyDict_SetItemString(d, "SIGWINCH", x);
732 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000733#endif
734#ifdef SIGPOLL
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000735 x = PyLong_FromLong(SIGPOLL);
736 PyDict_SetItemString(d, "SIGPOLL", x);
737 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000738#endif
739#ifdef SIGSTOP
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000740 x = PyLong_FromLong(SIGSTOP);
741 PyDict_SetItemString(d, "SIGSTOP", x);
742 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000743#endif
744#ifdef SIGTSTP
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000745 x = PyLong_FromLong(SIGTSTP);
746 PyDict_SetItemString(d, "SIGTSTP", x);
747 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000748#endif
749#ifdef SIGCONT
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000750 x = PyLong_FromLong(SIGCONT);
751 PyDict_SetItemString(d, "SIGCONT", x);
752 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000753#endif
754#ifdef SIGTTIN
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000755 x = PyLong_FromLong(SIGTTIN);
756 PyDict_SetItemString(d, "SIGTTIN", x);
757 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000758#endif
759#ifdef SIGTTOU
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000760 x = PyLong_FromLong(SIGTTOU);
761 PyDict_SetItemString(d, "SIGTTOU", x);
762 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000763#endif
764#ifdef SIGVTALRM
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000765 x = PyLong_FromLong(SIGVTALRM);
766 PyDict_SetItemString(d, "SIGVTALRM", x);
767 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000768#endif
769#ifdef SIGPROF
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000770 x = PyLong_FromLong(SIGPROF);
771 PyDict_SetItemString(d, "SIGPROF", x);
772 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000773#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000774#ifdef SIGXCPU
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000775 x = PyLong_FromLong(SIGXCPU);
776 PyDict_SetItemString(d, "SIGXCPU", x);
777 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000778#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000779#ifdef SIGXFSZ
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000780 x = PyLong_FromLong(SIGXFSZ);
781 PyDict_SetItemString(d, "SIGXFSZ", x);
782 Py_XDECREF(x);
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000783#endif
Anthony Baxterf37f37d2003-07-31 10:35:29 +0000784#ifdef SIGRTMIN
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000785 x = PyLong_FromLong(SIGRTMIN);
786 PyDict_SetItemString(d, "SIGRTMIN", x);
787 Py_XDECREF(x);
Anthony Baxterf37f37d2003-07-31 10:35:29 +0000788#endif
789#ifdef SIGRTMAX
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000790 x = PyLong_FromLong(SIGRTMAX);
791 PyDict_SetItemString(d, "SIGRTMAX", x);
792 Py_XDECREF(x);
Anthony Baxterf37f37d2003-07-31 10:35:29 +0000793#endif
Martin v. Löwis175af252002-01-12 11:43:25 +0000794#ifdef SIGINFO
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000795 x = PyLong_FromLong(SIGINFO);
796 PyDict_SetItemString(d, "SIGINFO", x);
797 Py_XDECREF(x);
Martin v. Löwis175af252002-01-12 11:43:25 +0000798#endif
Martin v. Löwis823725e2008-03-24 13:39:54 +0000799
800#ifdef ITIMER_REAL
801 x = PyLong_FromLong(ITIMER_REAL);
802 PyDict_SetItemString(d, "ITIMER_REAL", x);
803 Py_DECREF(x);
804#endif
805#ifdef ITIMER_VIRTUAL
806 x = PyLong_FromLong(ITIMER_VIRTUAL);
807 PyDict_SetItemString(d, "ITIMER_VIRTUAL", x);
808 Py_DECREF(x);
809#endif
810#ifdef ITIMER_PROF
811 x = PyLong_FromLong(ITIMER_PROF);
812 PyDict_SetItemString(d, "ITIMER_PROF", x);
813 Py_DECREF(x);
814#endif
815
816#if defined (HAVE_SETITIMER) || defined (HAVE_GETITIMER)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000817 ItimerError = PyErr_NewException("signal.ItimerError",
818 PyExc_IOError, NULL);
Neal Norwitz2f99b242008-08-24 05:48:10 +0000819 if (ItimerError != NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000820 PyDict_SetItemString(d, "ItimerError", ItimerError);
Martin v. Löwis823725e2008-03-24 13:39:54 +0000821#endif
822
Martin v. Löwis1a214512008-06-11 05:26:20 +0000823 if (PyErr_Occurred()) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000824 Py_DECREF(m);
825 m = NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000826 }
Barry Warsaw92971171997-01-03 00:14:25 +0000827
Barry Warsaw92971171997-01-03 00:14:25 +0000828 finally:
Martin v. Löwis1a214512008-06-11 05:26:20 +0000829 return m;
Guido van Rossum08c16611997-08-02 03:01:42 +0000830}
831
832static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000833finisignal(void)
Guido van Rossum08c16611997-08-02 03:01:42 +0000834{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000835 int i;
836 PyObject *func;
Guido van Rossum08c16611997-08-02 03:01:42 +0000837
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000838 PyOS_setsig(SIGINT, old_siginthandler);
839 old_siginthandler = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +0000840
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000841 for (i = 1; i < NSIG; i++) {
842 func = Handlers[i].func;
843 Handlers[i].tripped = 0;
844 Handlers[i].func = NULL;
845 if (i != SIGINT && func != NULL && func != Py_None &&
846 func != DefaultHandler && func != IgnoreHandler)
847 PyOS_setsig(i, SIG_DFL);
848 Py_XDECREF(func);
849 }
Guido van Rossum08c16611997-08-02 03:01:42 +0000850
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000851 Py_XDECREF(IntHandler);
852 IntHandler = NULL;
853 Py_XDECREF(DefaultHandler);
854 DefaultHandler = NULL;
855 Py_XDECREF(IgnoreHandler);
856 IgnoreHandler = NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000857}
858
Barry Warsaw92971171997-01-03 00:14:25 +0000859
Barry Warsaw92971171997-01-03 00:14:25 +0000860/* Declared in pyerrors.h */
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000861int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000862PyErr_CheckSignals(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000863{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000864 int i;
865 PyObject *f;
Barry Warsaw92971171997-01-03 00:14:25 +0000866
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000867 if (!is_tripped)
868 return 0;
Christian Heimesb76922a2007-12-11 01:06:40 +0000869
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000870#ifdef WITH_THREAD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000871 if (PyThread_get_thread_ident() != main_thread)
872 return 0;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000873#endif
Christian Heimesb76922a2007-12-11 01:06:40 +0000874
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000875 /*
876 * The is_tripped variable is meant to speed up the calls to
877 * PyErr_CheckSignals (both directly or via pending calls) when no
878 * signal has arrived. This variable is set to 1 when a signal arrives
879 * and it is set to 0 here, when we know some signals arrived. This way
880 * we can run the registered handlers with no signals blocked.
881 *
882 * NOTE: with this approach we can have a situation where is_tripped is
883 * 1 but we have no more signals to handle (Handlers[i].tripped
884 * is 0 for every signal i). This won't do us any harm (except
885 * we're gonna spent some cycles for nothing). This happens when
886 * we receive a signal i after we zero is_tripped and before we
887 * check Handlers[i].tripped.
888 */
889 is_tripped = 0;
Christian Heimesb76922a2007-12-11 01:06:40 +0000890
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000891 if (!(f = (PyObject *)PyEval_GetFrame()))
892 f = Py_None;
Christian Heimesb76922a2007-12-11 01:06:40 +0000893
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000894 for (i = 1; i < NSIG; i++) {
895 if (Handlers[i].tripped) {
896 PyObject *result = NULL;
897 PyObject *arglist = Py_BuildValue("(iO)", i, f);
898 Handlers[i].tripped = 0;
Barry Warsaw92971171997-01-03 00:14:25 +0000899
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000900 if (arglist) {
901 result = PyEval_CallObject(Handlers[i].func,
902 arglist);
903 Py_DECREF(arglist);
904 }
905 if (!result)
906 return -1;
Barry Warsaw92971171997-01-03 00:14:25 +0000907
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000908 Py_DECREF(result);
909 }
910 }
Christian Heimesb76922a2007-12-11 01:06:40 +0000911
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000912 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000913}
914
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000915
Barry Warsaw92971171997-01-03 00:14:25 +0000916/* Replacements for intrcheck.c functionality
917 * Declared in pyerrors.h
918 */
919void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000920PyErr_SetInterrupt(void)
Barry Warsaw92971171997-01-03 00:14:25 +0000921{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000922 is_tripped = 1;
923 Handlers[SIGINT].tripped = 1;
924 Py_AddPendingCall((int (*)(void *))PyErr_CheckSignals, NULL);
Barry Warsaw92971171997-01-03 00:14:25 +0000925}
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000926
927void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000928PyOS_InitInterrupts(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000929{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000930 PyObject *m = PyInit_signal();
931 if (m) {
932 _PyImport_FixupExtension(m, "signal", "signal");
933 Py_DECREF(m);
934 }
Guido van Rossum08c16611997-08-02 03:01:42 +0000935}
936
937void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000938PyOS_FiniInterrupts(void)
Guido van Rossum08c16611997-08-02 03:01:42 +0000939{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000940 finisignal();
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000941}
942
943int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000944PyOS_InterruptOccurred(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000945{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000946 if (Handlers[SIGINT].tripped) {
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000947#ifdef WITH_THREAD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000948 if (PyThread_get_thread_ident() != main_thread)
949 return 0;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000950#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000951 Handlers[SIGINT].tripped = 0;
952 return 1;
953 }
954 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000955}
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000956
957void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000958PyOS_AfterFork(void)
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000959{
960#ifdef WITH_THREAD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000961 PyEval_ReInitThreads();
962 main_thread = PyThread_get_thread_ident();
963 main_pid = getpid();
964 _PyImport_ReInitLock();
965 PyThread_ReInitTLS();
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000966#endif
967}