blob: 9b7f7bbbfc92b53138ed05d7671df6ea1a58dda0 [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öwis823725e2008-03-24 13:39:54 +000016#include <sys/time.h>
Christian Heimes5fb7c2a2007-12-24 08:52:31 +000017
Guido van Rossumbb4ba121994-06-23 11:25:45 +000018#ifndef SIG_ERR
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +000019#define SIG_ERR ((PyOS_sighandler_t)(-1))
Guido van Rossumbb4ba121994-06-23 11:25:45 +000020#endif
21
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000022#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000023#define NSIG 12
24#include <process.h>
25#endif
26
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000027#ifndef NSIG
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000028# if defined(_NSIG)
29# define NSIG _NSIG /* For BSD/SysV */
30# elif defined(_SIGMAX)
31# define NSIG (_SIGMAX + 1) /* For QNX */
32# elif defined(SIGMAX)
33# define NSIG (SIGMAX + 1) /* For djgpp */
34# else
35# define NSIG 64 /* Use a reasonable default value */
36# endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000037#endif
38
39
Guido van Rossumbb4ba121994-06-23 11:25:45 +000040/*
41 NOTES ON THE INTERACTION BETWEEN SIGNALS AND THREADS
42
43 When threads are supported, we want the following semantics:
44
45 - only the main thread can set a signal handler
46 - any thread can get a signal handler
47 - signals are only delivered to the main thread
48
49 I.e. we don't support "synchronous signals" like SIGFPE (catching
50 this doesn't make much sense in Python anyway) nor do we support
51 signals as a means of inter-thread communication, since not all
52 thread implementations support that (at least our thread library
53 doesn't).
54
55 We still have the problem that in some implementations signals
56 generated by the keyboard (e.g. SIGINT) are delivered to all
57 threads (e.g. SGI), while in others (e.g. Solaris) such signals are
58 delivered to one random thread (an intermediate possibility would
Guido van Rossuma3c04b01995-01-12 11:29:01 +000059 be to deliver it to the main thread -- POSIX?). For now, we have
Guido van Rossumbb4ba121994-06-23 11:25:45 +000060 a working implementation that works in all three cases -- the
61 handler ignores signals if getpid() isn't the same as in the main
62 thread. XXX This is a hack.
63
Guido van Rossum9e8181b2000-09-19 00:46:46 +000064 GNU pth is a user-space threading library, and as such, all threads
65 run within the same process. In this case, if the currently running
66 thread is not the main_thread, send the signal to the main_thread.
Guido van Rossumbb4ba121994-06-23 11:25:45 +000067*/
68
69#ifdef WITH_THREAD
Guido van Rossum295b8e51997-06-06 21:16:41 +000070#include <sys/types.h> /* For pid_t */
Guido van Rossum49b56061998-10-01 20:42:43 +000071#include "pythread.h"
Guido van Rossumbb4ba121994-06-23 11:25:45 +000072static long main_thread;
73static pid_t main_pid;
74#endif
75
Barry Warsaw92971171997-01-03 00:14:25 +000076static struct {
77 int tripped;
78 PyObject *func;
79} Handlers[NSIG];
Guido van Rossum398d9fe1994-05-11 08:59:13 +000080
Christian Heimes5fb7c2a2007-12-24 08:52:31 +000081static sig_atomic_t wakeup_fd = -1;
82
Christian Heimesb76922a2007-12-11 01:06:40 +000083/* Speed up sigcheck() when none tripped */
84static volatile sig_atomic_t is_tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +000085
Barry Warsaw92971171997-01-03 00:14:25 +000086static PyObject *DefaultHandler;
87static PyObject *IgnoreHandler;
88static PyObject *IntHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +000089
Martin v. Löwisf58de1b2001-03-06 12:13:56 +000090/* On Solaris 8, gcc will produce a warning that the function
91 declaration is not a prototype. This is caused by the definition of
92 SIG_DFL as (void (*)())0; the correct declaration would have been
93 (void (*)(int))0. */
94
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +000095static PyOS_sighandler_t old_siginthandler = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +000096
Martin v. Löwis823725e2008-03-24 13:39:54 +000097#ifdef HAVE_GETITIMER
98static PyObject *ItimerError;
99
100/* auxiliary functions for setitimer/getitimer */
101static void
102timeval_from_double(double d, struct timeval *tv)
103{
104 tv->tv_sec = floor(d);
105 tv->tv_usec = fmod(d, 1.0) * 1000000.0;
106}
107
108static inline double
109double_from_timeval(struct timeval *tv)
110{
111 return tv->tv_sec + (double)(tv->tv_usec / 1000000.0);
112}
113
114static PyObject *
115itimer_retval(struct itimerval *iv)
116{
117 PyObject *r, *v;
118
119 r = PyTuple_New(2);
120 if (r == NULL)
121 return NULL;
122
123 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_value)))) {
124 Py_DECREF(r);
125 return NULL;
126 }
127
128 PyTuple_SET_ITEM(r, 0, v);
129
130 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_interval)))) {
131 Py_DECREF(r);
132 return NULL;
133 }
134
135 PyTuple_SET_ITEM(r, 1, v);
136
137 return r;
138}
139#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000140
Guido van Rossume4485b01994-09-07 14:32:49 +0000141static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000142signal_default_int_handler(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000143{
Guido van Rossume4485b01994-09-07 14:32:49 +0000144 PyErr_SetNone(PyExc_KeyboardInterrupt);
Barry Warsaw92971171997-01-03 00:14:25 +0000145 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000146}
147
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000148PyDoc_STRVAR(default_int_handler_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000149"default_int_handler(...)\n\
150\n\
Michael W. Hudson24ec2112004-06-17 15:55:53 +0000151The default handler for SIGINT installed by Python.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000152It raises KeyboardInterrupt.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000153
Thomas Wouters0796b002000-07-22 23:49:30 +0000154
155static int
156checksignals_witharg(void * unused)
157{
158 return PyErr_CheckSignals();
159}
160
Tim Peters4f1b2082000-07-23 21:18:09 +0000161static void
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000162signal_handler(int sig_num)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000163{
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000164#ifdef WITH_THREAD
Guido van Rossum9e8181b2000-09-19 00:46:46 +0000165#ifdef WITH_PTH
166 if (PyThread_get_thread_ident() != main_thread) {
167 pth_raise(*(pth_t *) main_thread, sig_num);
168 return;
169 }
170#endif
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000171 /* See NOTES section above */
172 if (getpid() == main_pid) {
173#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000174 Handlers[sig_num].tripped = 1;
Christian Heimesb76922a2007-12-11 01:06:40 +0000175 /* Set is_tripped after setting .tripped, as it gets
176 cleared in PyErr_CheckSignals() before .tripped. */
177 is_tripped = 1;
Thomas Wouters0796b002000-07-22 23:49:30 +0000178 Py_AddPendingCall(checksignals_witharg, NULL);
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000179 if (wakeup_fd != -1)
180 write(wakeup_fd, "\0", 1);
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000181#ifdef WITH_THREAD
182 }
183#endif
Guido van Rossum602099a1994-09-14 13:32:22 +0000184#ifdef SIGCHLD
185 if (sig_num == SIGCHLD) {
186 /* To avoid infinite recursion, this signal remains
187 reset until explicit re-instated.
188 Don't clear the 'func' field as it is our pointer
189 to the Python handler... */
Tim Peters4f1b2082000-07-23 21:18:09 +0000190 return;
Guido van Rossum602099a1994-09-14 13:32:22 +0000191 }
192#endif
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000193 PyOS_setsig(sig_num, signal_handler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000194}
Guido van Rossume4485b01994-09-07 14:32:49 +0000195
Guido van Rossum06d511d1995-03-10 15:13:48 +0000196
Guido van Rossum1171ee61997-08-22 20:42:00 +0000197#ifdef HAVE_ALARM
Guido van Rossume4485b01994-09-07 14:32:49 +0000198static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000199signal_alarm(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000200{
201 int t;
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000202 if (!PyArg_ParseTuple(args, "i:alarm", &t))
Barry Warsaw92971171997-01-03 00:14:25 +0000203 return NULL;
Guido van Rossume4485b01994-09-07 14:32:49 +0000204 /* alarm() returns the number of seconds remaining */
Christian Heimes217cfd12007-12-02 14:31:20 +0000205 return PyLong_FromLong((long)alarm(t));
Guido van Rossumaa0f4c71994-08-23 13:49:37 +0000206}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000207
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000208PyDoc_STRVAR(alarm_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000209"alarm(seconds)\n\
210\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000211Arrange for SIGALRM to arrive after the given number of seconds.");
Guido van Rossum06d511d1995-03-10 15:13:48 +0000212#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000213
Guido van Rossum1171ee61997-08-22 20:42:00 +0000214#ifdef HAVE_PAUSE
Guido van Rossuma597dde1995-01-10 20:56:29 +0000215static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000216signal_pause(PyObject *self)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000217{
Guido van Rossuma597dde1995-01-10 20:56:29 +0000218 Py_BEGIN_ALLOW_THREADS
Barry Warsaw92971171997-01-03 00:14:25 +0000219 (void)pause();
Guido van Rossuma597dde1995-01-10 20:56:29 +0000220 Py_END_ALLOW_THREADS
Barry Warsaw92971171997-01-03 00:14:25 +0000221 /* make sure that any exceptions that got raised are propagated
222 * back into Python
223 */
224 if (PyErr_CheckSignals())
225 return NULL;
226
Guido van Rossume4485b01994-09-07 14:32:49 +0000227 Py_INCREF(Py_None);
228 return Py_None;
229}
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000230PyDoc_STRVAR(pause_doc,
Barry Warsaw1ee36ff1998-07-21 22:41:18 +0000231"pause()\n\
232\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000233Wait until a signal arrives.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000234
Guido van Rossum06d511d1995-03-10 15:13:48 +0000235#endif
Guido van Rossume4485b01994-09-07 14:32:49 +0000236
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000237
Guido van Rossume4485b01994-09-07 14:32:49 +0000238static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000239signal_signal(PyObject *self, PyObject *args)
Guido van Rossume4485b01994-09-07 14:32:49 +0000240{
241 PyObject *obj;
242 int sig_num;
243 PyObject *old_handler;
Tim Peters4f1b2082000-07-23 21:18:09 +0000244 void (*func)(int);
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000245 if (!PyArg_ParseTuple(args, "iO:signal", &sig_num, &obj))
Barry Warsaw92971171997-01-03 00:14:25 +0000246 return NULL;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000247#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000248 if (PyThread_get_thread_ident() != main_thread) {
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000249 PyErr_SetString(PyExc_ValueError,
250 "signal only works in main thread");
Barry Warsaw92971171997-01-03 00:14:25 +0000251 return NULL;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000252 }
253#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000254 if (sig_num < 1 || sig_num >= NSIG) {
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000255 PyErr_SetString(PyExc_ValueError,
256 "signal number out of range");
Barry Warsaw92971171997-01-03 00:14:25 +0000257 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000258 }
Barry Warsaw92971171997-01-03 00:14:25 +0000259 if (obj == IgnoreHandler)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000260 func = SIG_IGN;
Barry Warsaw92971171997-01-03 00:14:25 +0000261 else if (obj == DefaultHandler)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000262 func = SIG_DFL;
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000263 else if (!PyCallable_Check(obj)) {
Guido van Rossume4485b01994-09-07 14:32:49 +0000264 PyErr_SetString(PyExc_TypeError,
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000265"signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object");
Barry Warsaw92971171997-01-03 00:14:25 +0000266 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000267 }
268 else
Barry Warsaw92971171997-01-03 00:14:25 +0000269 func = signal_handler;
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000270 if (PyOS_setsig(sig_num, func) == SIG_ERR) {
Guido van Rossume4485b01994-09-07 14:32:49 +0000271 PyErr_SetFromErrno(PyExc_RuntimeError);
Barry Warsaw92971171997-01-03 00:14:25 +0000272 return NULL;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000273 }
Barry Warsaw92971171997-01-03 00:14:25 +0000274 old_handler = Handlers[sig_num].func;
275 Handlers[sig_num].tripped = 0;
Guido van Rossume4485b01994-09-07 14:32:49 +0000276 Py_INCREF(obj);
Barry Warsaw92971171997-01-03 00:14:25 +0000277 Handlers[sig_num].func = obj;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000278 return old_handler;
279}
280
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000281PyDoc_STRVAR(signal_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000282"signal(sig, action) -> action\n\
283\n\
284Set the action for the given signal. The action can be SIG_DFL,\n\
285SIG_IGN, or a callable Python object. The previous action is\n\
286returned. See getsignal() for possible return values.\n\
287\n\
288*** IMPORTANT NOTICE ***\n\
289A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000290the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000291
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000292
Guido van Rossume4485b01994-09-07 14:32:49 +0000293static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000294signal_getsignal(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000295{
296 int sig_num;
Guido van Rossume4485b01994-09-07 14:32:49 +0000297 PyObject *old_handler;
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000298 if (!PyArg_ParseTuple(args, "i:getsignal", &sig_num))
Barry Warsaw92971171997-01-03 00:14:25 +0000299 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000300 if (sig_num < 1 || sig_num >= NSIG) {
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000301 PyErr_SetString(PyExc_ValueError,
302 "signal number out of range");
Barry Warsaw92971171997-01-03 00:14:25 +0000303 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000304 }
Barry Warsaw92971171997-01-03 00:14:25 +0000305 old_handler = Handlers[sig_num].func;
Guido van Rossume4485b01994-09-07 14:32:49 +0000306 Py_INCREF(old_handler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000307 return old_handler;
308}
309
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000310PyDoc_STRVAR(getsignal_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000311"getsignal(sig) -> action\n\
312\n\
313Return the current action for the given signal. The return value can be:\n\
314SIG_IGN -- if the signal is being ignored\n\
315SIG_DFL -- if the default action for the signal is in effect\n\
316None -- if an unknown handler is in effect\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000317anything else -- the callable Python object used as a handler");
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000318
Christian Heimes8640e742008-02-23 16:23:06 +0000319#ifdef HAVE_SIGINTERRUPT
320PyDoc_STRVAR(siginterrupt_doc,
321"siginterrupt(sig, flag) -> None\n\
322change system call restart behaviour: if flag is False, system calls\n\
323will be restarted when interrupted by signal sig, else system calls\n\
324will be interrupted.");
325
326static PyObject *
327signal_siginterrupt(PyObject *self, PyObject *args)
328{
329 int sig_num;
330 int flag;
331
332 if (!PyArg_ParseTuple(args, "ii:siginterrupt", &sig_num, &flag))
333 return NULL;
334 if (sig_num < 1 || sig_num >= NSIG) {
335 PyErr_SetString(PyExc_ValueError,
336 "signal number out of range");
337 return NULL;
338 }
339 if (siginterrupt(sig_num, flag)<0) {
340 PyErr_SetFromErrno(PyExc_RuntimeError);
341 return NULL;
342 }
343
344 Py_INCREF(Py_None);
345 return Py_None;
346}
347
348#endif
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000349
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000350static PyObject *
351signal_set_wakeup_fd(PyObject *self, PyObject *args)
352{
353 struct stat buf;
354 int fd, old_fd;
355 if (!PyArg_ParseTuple(args, "i:set_wakeup_fd", &fd))
356 return NULL;
357#ifdef WITH_THREAD
358 if (PyThread_get_thread_ident() != main_thread) {
359 PyErr_SetString(PyExc_ValueError,
360 "set_wakeup_fd only works in main thread");
361 return NULL;
362 }
363#endif
364 if (fd != -1 && fstat(fd, &buf) != 0) {
365 PyErr_SetString(PyExc_ValueError, "invalid fd");
366 return NULL;
367 }
368 old_fd = wakeup_fd;
369 wakeup_fd = fd;
370 return PyLong_FromLong(old_fd);
371}
372
373PyDoc_STRVAR(set_wakeup_fd_doc,
374"set_wakeup_fd(fd) -> fd\n\
375\n\
376Sets the fd to be written to (with '\\0') when a signal\n\
377comes in. A library can use this to wakeup select or poll.\n\
378The previous fd is returned.\n\
379\n\
380The fd must be non-blocking.");
381
382/* C API for the same, without all the error checking */
383int
384PySignal_SetWakeupFd(int fd)
385{
386 int old_fd = wakeup_fd;
387 if (fd < 0)
388 fd = -1;
389 wakeup_fd = fd;
390 return old_fd;
391}
392
393
Martin v. Löwis823725e2008-03-24 13:39:54 +0000394#ifdef HAVE_SETITIMER
395static PyObject *
396signal_setitimer(PyObject *self, PyObject *args)
397{
398 double first;
399 double interval = 0;
400 int which;
401 struct itimerval new, old;
402
403 if(!PyArg_ParseTuple(args, "id|d:setitimer", &which, &first, &interval))
404 return NULL;
405
406 timeval_from_double(first, &new.it_value);
407 timeval_from_double(interval, &new.it_interval);
408 /* Let OS check "which" value */
409 if (setitimer(which, &new, &old) != 0) {
410 PyErr_SetFromErrno(ItimerError);
411 return NULL;
412 }
413
414 return itimer_retval(&old);
415}
416
417PyDoc_STRVAR(setitimer_doc,
418"setitimer(which, seconds[, interval])\n\
419\n\
420Sets given itimer (one of ITIMER_REAL, ITIMER_VIRTUAL\n\
421or ITIMER_PROF) to fire after value seconds and after\n\
422that every interval seconds.\n\
423The itimer can be cleared by setting seconds to zero.\n\
424\n\
425Returns old values as a tuple: (delay, interval).");
426#endif
427
428
429#ifdef HAVE_GETITIMER
430static PyObject *
431signal_getitimer(PyObject *self, PyObject *args)
432{
433 int which;
434 struct itimerval old;
435
436 if (!PyArg_ParseTuple(args, "i:getitimer", &which))
437 return NULL;
438
439 if (getitimer(which, &old) != 0) {
440 PyErr_SetFromErrno(ItimerError);
441 return NULL;
442 }
443
444 return itimer_retval(&old);
445}
446
447PyDoc_STRVAR(getitimer_doc,
448"getitimer(which)\n\
449\n\
450Returns current value of given itimer.");
451#endif
452
453
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000454/* List of functions defined in the module */
Barry Warsaw92971171997-01-03 00:14:25 +0000455static PyMethodDef signal_methods[] = {
Guido van Rossum1171ee61997-08-22 20:42:00 +0000456#ifdef HAVE_ALARM
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000457 {"alarm", signal_alarm, METH_VARARGS, alarm_doc},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000458#endif
Martin v. Löwis823725e2008-03-24 13:39:54 +0000459#ifdef HAVE_SETITIMER
460 {"setitimer", signal_setitimer, METH_VARARGS, setitimer_doc},
461#endif
462#ifdef HAVE_GETITIMER
463 {"getitimer", signal_getitimer, METH_VARARGS, getitimer_doc},
464#endif
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000465 {"signal", signal_signal, METH_VARARGS, signal_doc},
466 {"getsignal", signal_getsignal, METH_VARARGS, getsignal_doc},
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000467 {"set_wakeup_fd", signal_set_wakeup_fd, METH_VARARGS, set_wakeup_fd_doc},
Christian Heimes8640e742008-02-23 16:23:06 +0000468#ifdef HAVE_SIGINTERRUPT
469 {"siginterrupt", signal_siginterrupt, METH_VARARGS, siginterrupt_doc},
470#endif
Guido van Rossum1171ee61997-08-22 20:42:00 +0000471#ifdef HAVE_PAUSE
Neil Schemenauer78662cf2002-03-28 21:04:14 +0000472 {"pause", (PyCFunction)signal_pause,
473 METH_NOARGS,pause_doc},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000474#endif
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000475 {"default_int_handler", signal_default_int_handler,
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000476 METH_VARARGS, default_int_handler_doc},
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000477 {NULL, NULL} /* sentinel */
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000478};
479
Barry Warsaw92971171997-01-03 00:14:25 +0000480
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000481PyDoc_STRVAR(module_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000482"This module provides mechanisms to use signal handlers in Python.\n\
483\n\
484Functions:\n\
485\n\
486alarm() -- cause SIGALRM after a specified time [Unix only]\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +0000487setitimer() -- cause a signal (described below) after a specified\n\
488 float time and the timer may restart then [Unix only]\n\
489getitimer() -- get current value of timer [Unix only]\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000490signal() -- set the action for a given signal\n\
491getsignal() -- get the signal action for a given signal\n\
492pause() -- wait until a signal arrives [Unix only]\n\
493default_int_handler() -- default SIGINT handler\n\
494\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +0000495signal constants:\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000496SIG_DFL -- used to refer to the system default handler\n\
497SIG_IGN -- used to ignore the signal\n\
498NSIG -- number of defined signals\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000499SIGINT, SIGTERM, etc. -- signal numbers\n\
500\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +0000501itimer constants:\n\
502ITIMER_REAL -- decrements in real time, and delivers SIGALRM upon\n\
503 expiration\n\
504ITIMER_VIRTUAL -- decrements only when the process is executing,\n\
505 and delivers SIGVTALRM upon expiration\n\
506ITIMER_PROF -- decrements both when the process is executing and\n\
507 when the system is executing on behalf of the process.\n\
508 Coupled with ITIMER_VIRTUAL, this timer is usually\n\
509 used to profile the time spent by the application\n\
510 in user and kernel space. SIGPROF is delivered upon\n\
511 expiration.\n\
512\n\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000513*** IMPORTANT NOTICE ***\n\
514A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000515the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000516
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000517PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000518initsignal(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000519{
Guido van Rossume4485b01994-09-07 14:32:49 +0000520 PyObject *m, *d, *x;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000521 int i;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000522
523#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000524 main_thread = PyThread_get_thread_ident();
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000525 main_pid = getpid();
526#endif
527
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000528 /* Create the module and add the functions */
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000529 m = Py_InitModule3("signal", signal_methods, module_doc);
Neal Norwitz1ac754f2006-01-19 06:09:39 +0000530 if (m == NULL)
531 return;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000532
533 /* Add some symbolic constants to the module */
Guido van Rossume4485b01994-09-07 14:32:49 +0000534 d = PyModule_GetDict(m);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000535
Guido van Rossum276fa432000-06-30 23:04:18 +0000536 x = DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL);
Barry Warsaw92971171997-01-03 00:14:25 +0000537 if (!x || PyDict_SetItemString(d, "SIG_DFL", x) < 0)
538 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000539
Guido van Rossum276fa432000-06-30 23:04:18 +0000540 x = IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN);
Barry Warsaw92971171997-01-03 00:14:25 +0000541 if (!x || PyDict_SetItemString(d, "SIG_IGN", x) < 0)
542 goto finally;
543
Christian Heimes217cfd12007-12-02 14:31:20 +0000544 x = PyLong_FromLong((long)NSIG);
Barry Warsaw92971171997-01-03 00:14:25 +0000545 if (!x || PyDict_SetItemString(d, "NSIG", x) < 0)
546 goto finally;
Guido van Rossum08c16611997-08-02 03:01:42 +0000547 Py_DECREF(x);
Barry Warsaw92971171997-01-03 00:14:25 +0000548
549 x = IntHandler = PyDict_GetItemString(d, "default_int_handler");
550 if (!x)
551 goto finally;
Guido van Rossum08c16611997-08-02 03:01:42 +0000552 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +0000553
554 Handlers[0].tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000555 for (i = 1; i < NSIG; i++) {
Tim Peters4f1b2082000-07-23 21:18:09 +0000556 void (*t)(int);
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000557 t = PyOS_getsig(i);
Barry Warsaw92971171997-01-03 00:14:25 +0000558 Handlers[i].tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000559 if (t == SIG_DFL)
Barry Warsaw92971171997-01-03 00:14:25 +0000560 Handlers[i].func = DefaultHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000561 else if (t == SIG_IGN)
Barry Warsaw92971171997-01-03 00:14:25 +0000562 Handlers[i].func = IgnoreHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000563 else
Barry Warsaw92971171997-01-03 00:14:25 +0000564 Handlers[i].func = Py_None; /* None of our business */
565 Py_INCREF(Handlers[i].func);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000566 }
Barry Warsaw92971171997-01-03 00:14:25 +0000567 if (Handlers[SIGINT].func == DefaultHandler) {
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000568 /* Install default int handler */
Guido van Rossum08c16611997-08-02 03:01:42 +0000569 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +0000570 Py_DECREF(Handlers[SIGINT].func);
571 Handlers[SIGINT].func = IntHandler;
Neal Norwitz41785152002-06-13 21:42:51 +0000572 old_siginthandler = PyOS_setsig(SIGINT, signal_handler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000573 }
574
575#ifdef SIGHUP
Christian Heimes217cfd12007-12-02 14:31:20 +0000576 x = PyLong_FromLong(SIGHUP);
Guido van Rossume4485b01994-09-07 14:32:49 +0000577 PyDict_SetItemString(d, "SIGHUP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000578 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000579#endif
580#ifdef SIGINT
Christian Heimes217cfd12007-12-02 14:31:20 +0000581 x = PyLong_FromLong(SIGINT);
Guido van Rossume4485b01994-09-07 14:32:49 +0000582 PyDict_SetItemString(d, "SIGINT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000583 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000584#endif
Tim Peters1ce3cf72001-10-01 17:58:40 +0000585#ifdef SIGBREAK
Christian Heimes217cfd12007-12-02 14:31:20 +0000586 x = PyLong_FromLong(SIGBREAK);
Tim Peters1ce3cf72001-10-01 17:58:40 +0000587 PyDict_SetItemString(d, "SIGBREAK", x);
588 Py_XDECREF(x);
589#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000590#ifdef SIGQUIT
Christian Heimes217cfd12007-12-02 14:31:20 +0000591 x = PyLong_FromLong(SIGQUIT);
Guido van Rossume4485b01994-09-07 14:32:49 +0000592 PyDict_SetItemString(d, "SIGQUIT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000593 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000594#endif
595#ifdef SIGILL
Christian Heimes217cfd12007-12-02 14:31:20 +0000596 x = PyLong_FromLong(SIGILL);
Guido van Rossume4485b01994-09-07 14:32:49 +0000597 PyDict_SetItemString(d, "SIGILL", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000598 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000599#endif
600#ifdef SIGTRAP
Christian Heimes217cfd12007-12-02 14:31:20 +0000601 x = PyLong_FromLong(SIGTRAP);
Guido van Rossume4485b01994-09-07 14:32:49 +0000602 PyDict_SetItemString(d, "SIGTRAP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000603 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000604#endif
605#ifdef SIGIOT
Christian Heimes217cfd12007-12-02 14:31:20 +0000606 x = PyLong_FromLong(SIGIOT);
Guido van Rossume4485b01994-09-07 14:32:49 +0000607 PyDict_SetItemString(d, "SIGIOT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000608 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000609#endif
610#ifdef SIGABRT
Christian Heimes217cfd12007-12-02 14:31:20 +0000611 x = PyLong_FromLong(SIGABRT);
Guido van Rossume4485b01994-09-07 14:32:49 +0000612 PyDict_SetItemString(d, "SIGABRT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000613 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000614#endif
615#ifdef SIGEMT
Christian Heimes217cfd12007-12-02 14:31:20 +0000616 x = PyLong_FromLong(SIGEMT);
Guido van Rossume4485b01994-09-07 14:32:49 +0000617 PyDict_SetItemString(d, "SIGEMT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000618 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000619#endif
620#ifdef SIGFPE
Christian Heimes217cfd12007-12-02 14:31:20 +0000621 x = PyLong_FromLong(SIGFPE);
Guido van Rossume4485b01994-09-07 14:32:49 +0000622 PyDict_SetItemString(d, "SIGFPE", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000623 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000624#endif
625#ifdef SIGKILL
Christian Heimes217cfd12007-12-02 14:31:20 +0000626 x = PyLong_FromLong(SIGKILL);
Guido van Rossume4485b01994-09-07 14:32:49 +0000627 PyDict_SetItemString(d, "SIGKILL", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000628 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000629#endif
630#ifdef SIGBUS
Christian Heimes217cfd12007-12-02 14:31:20 +0000631 x = PyLong_FromLong(SIGBUS);
Guido van Rossume4485b01994-09-07 14:32:49 +0000632 PyDict_SetItemString(d, "SIGBUS", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000633 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000634#endif
635#ifdef SIGSEGV
Christian Heimes217cfd12007-12-02 14:31:20 +0000636 x = PyLong_FromLong(SIGSEGV);
Guido van Rossume4485b01994-09-07 14:32:49 +0000637 PyDict_SetItemString(d, "SIGSEGV", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000638 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000639#endif
640#ifdef SIGSYS
Christian Heimes217cfd12007-12-02 14:31:20 +0000641 x = PyLong_FromLong(SIGSYS);
Guido van Rossume4485b01994-09-07 14:32:49 +0000642 PyDict_SetItemString(d, "SIGSYS", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000643 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000644#endif
645#ifdef SIGPIPE
Christian Heimes217cfd12007-12-02 14:31:20 +0000646 x = PyLong_FromLong(SIGPIPE);
Guido van Rossume4485b01994-09-07 14:32:49 +0000647 PyDict_SetItemString(d, "SIGPIPE", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000648 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000649#endif
650#ifdef SIGALRM
Christian Heimes217cfd12007-12-02 14:31:20 +0000651 x = PyLong_FromLong(SIGALRM);
Guido van Rossume4485b01994-09-07 14:32:49 +0000652 PyDict_SetItemString(d, "SIGALRM", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000653 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000654#endif
655#ifdef SIGTERM
Christian Heimes217cfd12007-12-02 14:31:20 +0000656 x = PyLong_FromLong(SIGTERM);
Guido van Rossume4485b01994-09-07 14:32:49 +0000657 PyDict_SetItemString(d, "SIGTERM", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000658 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000659#endif
660#ifdef SIGUSR1
Christian Heimes217cfd12007-12-02 14:31:20 +0000661 x = PyLong_FromLong(SIGUSR1);
Guido van Rossume4485b01994-09-07 14:32:49 +0000662 PyDict_SetItemString(d, "SIGUSR1", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000663 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000664#endif
665#ifdef SIGUSR2
Christian Heimes217cfd12007-12-02 14:31:20 +0000666 x = PyLong_FromLong(SIGUSR2);
Guido van Rossume4485b01994-09-07 14:32:49 +0000667 PyDict_SetItemString(d, "SIGUSR2", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000668 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000669#endif
670#ifdef SIGCLD
Christian Heimes217cfd12007-12-02 14:31:20 +0000671 x = PyLong_FromLong(SIGCLD);
Guido van Rossume4485b01994-09-07 14:32:49 +0000672 PyDict_SetItemString(d, "SIGCLD", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000673 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000674#endif
675#ifdef SIGCHLD
Christian Heimes217cfd12007-12-02 14:31:20 +0000676 x = PyLong_FromLong(SIGCHLD);
Guido van Rossume4485b01994-09-07 14:32:49 +0000677 PyDict_SetItemString(d, "SIGCHLD", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000678 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000679#endif
680#ifdef SIGPWR
Christian Heimes217cfd12007-12-02 14:31:20 +0000681 x = PyLong_FromLong(SIGPWR);
Guido van Rossume4485b01994-09-07 14:32:49 +0000682 PyDict_SetItemString(d, "SIGPWR", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000683 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000684#endif
685#ifdef SIGIO
Christian Heimes217cfd12007-12-02 14:31:20 +0000686 x = PyLong_FromLong(SIGIO);
Guido van Rossume4485b01994-09-07 14:32:49 +0000687 PyDict_SetItemString(d, "SIGIO", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000688 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000689#endif
690#ifdef SIGURG
Christian Heimes217cfd12007-12-02 14:31:20 +0000691 x = PyLong_FromLong(SIGURG);
Guido van Rossume4485b01994-09-07 14:32:49 +0000692 PyDict_SetItemString(d, "SIGURG", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000693 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000694#endif
695#ifdef SIGWINCH
Christian Heimes217cfd12007-12-02 14:31:20 +0000696 x = PyLong_FromLong(SIGWINCH);
Guido van Rossume4485b01994-09-07 14:32:49 +0000697 PyDict_SetItemString(d, "SIGWINCH", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000698 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000699#endif
700#ifdef SIGPOLL
Christian Heimes217cfd12007-12-02 14:31:20 +0000701 x = PyLong_FromLong(SIGPOLL);
Guido van Rossume4485b01994-09-07 14:32:49 +0000702 PyDict_SetItemString(d, "SIGPOLL", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000703 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000704#endif
705#ifdef SIGSTOP
Christian Heimes217cfd12007-12-02 14:31:20 +0000706 x = PyLong_FromLong(SIGSTOP);
Guido van Rossume4485b01994-09-07 14:32:49 +0000707 PyDict_SetItemString(d, "SIGSTOP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000708 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000709#endif
710#ifdef SIGTSTP
Christian Heimes217cfd12007-12-02 14:31:20 +0000711 x = PyLong_FromLong(SIGTSTP);
Guido van Rossume4485b01994-09-07 14:32:49 +0000712 PyDict_SetItemString(d, "SIGTSTP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000713 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000714#endif
715#ifdef SIGCONT
Christian Heimes217cfd12007-12-02 14:31:20 +0000716 x = PyLong_FromLong(SIGCONT);
Guido van Rossume4485b01994-09-07 14:32:49 +0000717 PyDict_SetItemString(d, "SIGCONT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000718 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000719#endif
720#ifdef SIGTTIN
Christian Heimes217cfd12007-12-02 14:31:20 +0000721 x = PyLong_FromLong(SIGTTIN);
Guido van Rossume4485b01994-09-07 14:32:49 +0000722 PyDict_SetItemString(d, "SIGTTIN", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000723 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000724#endif
725#ifdef SIGTTOU
Christian Heimes217cfd12007-12-02 14:31:20 +0000726 x = PyLong_FromLong(SIGTTOU);
Guido van Rossume4485b01994-09-07 14:32:49 +0000727 PyDict_SetItemString(d, "SIGTTOU", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000728 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000729#endif
730#ifdef SIGVTALRM
Christian Heimes217cfd12007-12-02 14:31:20 +0000731 x = PyLong_FromLong(SIGVTALRM);
Guido van Rossume4485b01994-09-07 14:32:49 +0000732 PyDict_SetItemString(d, "SIGVTALRM", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000733 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000734#endif
735#ifdef SIGPROF
Christian Heimes217cfd12007-12-02 14:31:20 +0000736 x = PyLong_FromLong(SIGPROF);
Guido van Rossume4485b01994-09-07 14:32:49 +0000737 PyDict_SetItemString(d, "SIGPROF", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000738 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000739#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000740#ifdef SIGXCPU
Christian Heimes217cfd12007-12-02 14:31:20 +0000741 x = PyLong_FromLong(SIGXCPU);
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000742 PyDict_SetItemString(d, "SIGXCPU", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000743 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000744#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000745#ifdef SIGXFSZ
Christian Heimes217cfd12007-12-02 14:31:20 +0000746 x = PyLong_FromLong(SIGXFSZ);
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000747 PyDict_SetItemString(d, "SIGXFSZ", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000748 Py_XDECREF(x);
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000749#endif
Anthony Baxterf37f37d2003-07-31 10:35:29 +0000750#ifdef SIGRTMIN
Christian Heimes217cfd12007-12-02 14:31:20 +0000751 x = PyLong_FromLong(SIGRTMIN);
Anthony Baxterf37f37d2003-07-31 10:35:29 +0000752 PyDict_SetItemString(d, "SIGRTMIN", x);
753 Py_XDECREF(x);
754#endif
755#ifdef SIGRTMAX
Christian Heimes217cfd12007-12-02 14:31:20 +0000756 x = PyLong_FromLong(SIGRTMAX);
Anthony Baxterf37f37d2003-07-31 10:35:29 +0000757 PyDict_SetItemString(d, "SIGRTMAX", x);
758 Py_XDECREF(x);
759#endif
Martin v. Löwis175af252002-01-12 11:43:25 +0000760#ifdef SIGINFO
Christian Heimes217cfd12007-12-02 14:31:20 +0000761 x = PyLong_FromLong(SIGINFO);
Martin v. Löwis175af252002-01-12 11:43:25 +0000762 PyDict_SetItemString(d, "SIGINFO", x);
763 Py_XDECREF(x);
764#endif
Martin v. Löwis823725e2008-03-24 13:39:54 +0000765
766#ifdef ITIMER_REAL
767 x = PyLong_FromLong(ITIMER_REAL);
768 PyDict_SetItemString(d, "ITIMER_REAL", x);
769 Py_DECREF(x);
770#endif
771#ifdef ITIMER_VIRTUAL
772 x = PyLong_FromLong(ITIMER_VIRTUAL);
773 PyDict_SetItemString(d, "ITIMER_VIRTUAL", x);
774 Py_DECREF(x);
775#endif
776#ifdef ITIMER_PROF
777 x = PyLong_FromLong(ITIMER_PROF);
778 PyDict_SetItemString(d, "ITIMER_PROF", x);
779 Py_DECREF(x);
780#endif
781
782#if defined (HAVE_SETITIMER) || defined (HAVE_GETITIMER)
783 ItimerError = PyErr_NewException("signal.ItimerError",
784 PyExc_IOError, NULL);
785 PyDict_SetItemString(d, "ItimerError", ItimerError);
786#endif
787
Barry Warsaw92971171997-01-03 00:14:25 +0000788 if (!PyErr_Occurred())
789 return;
790
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000791 /* Check for errors */
Barry Warsaw92971171997-01-03 00:14:25 +0000792 finally:
Guido van Rossum08c16611997-08-02 03:01:42 +0000793 return;
794}
795
796static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000797finisignal(void)
Guido van Rossum08c16611997-08-02 03:01:42 +0000798{
799 int i;
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000800 PyObject *func;
Guido van Rossum08c16611997-08-02 03:01:42 +0000801
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000802 PyOS_setsig(SIGINT, old_siginthandler);
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000803 old_siginthandler = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +0000804
805 for (i = 1; i < NSIG; i++) {
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000806 func = Handlers[i].func;
Guido van Rossum08c16611997-08-02 03:01:42 +0000807 Handlers[i].tripped = 0;
Guido van Rossum08c16611997-08-02 03:01:42 +0000808 Handlers[i].func = NULL;
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000809 if (i != SIGINT && func != NULL && func != Py_None &&
810 func != DefaultHandler && func != IgnoreHandler)
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000811 PyOS_setsig(i, SIG_DFL);
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000812 Py_XDECREF(func);
Guido van Rossum08c16611997-08-02 03:01:42 +0000813 }
814
815 Py_XDECREF(IntHandler);
816 IntHandler = NULL;
817 Py_XDECREF(DefaultHandler);
818 DefaultHandler = NULL;
819 Py_XDECREF(IgnoreHandler);
820 IgnoreHandler = NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000821}
822
Barry Warsaw92971171997-01-03 00:14:25 +0000823
Barry Warsaw92971171997-01-03 00:14:25 +0000824/* Declared in pyerrors.h */
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000825int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000826PyErr_CheckSignals(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000827{
828 int i;
Guido van Rossume4485b01994-09-07 14:32:49 +0000829 PyObject *f;
Barry Warsaw92971171997-01-03 00:14:25 +0000830
831 if (!is_tripped)
Guido van Rossum6299d1e1994-05-31 12:51:13 +0000832 return 0;
Christian Heimesb76922a2007-12-11 01:06:40 +0000833
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000834#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000835 if (PyThread_get_thread_ident() != main_thread)
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000836 return 0;
837#endif
Christian Heimesb76922a2007-12-11 01:06:40 +0000838
839 /*
840 * The is_stripped variable is meant to speed up the calls to
841 * PyErr_CheckSignals (both directly or via pending calls) when no
842 * signal has arrived. This variable is set to 1 when a signal arrives
843 * and it is set to 0 here, when we know some signals arrived. This way
844 * we can run the registered handlers with no signals blocked.
845 *
846 * NOTE: with this approach we can have a situation where is_tripped is
847 * 1 but we have no more signals to handle (Handlers[i].tripped
848 * is 0 for every signal i). This won't do us any harm (except
849 * we're gonna spent some cycles for nothing). This happens when
850 * we receive a signal i after we zero is_tripped and before we
851 * check Handlers[i].tripped.
852 */
853 is_tripped = 0;
854
Guido van Rossum6297a7a2003-02-19 15:53:17 +0000855 if (!(f = (PyObject *)PyEval_GetFrame()))
Guido van Rossume4485b01994-09-07 14:32:49 +0000856 f = Py_None;
Christian Heimesb76922a2007-12-11 01:06:40 +0000857
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000858 for (i = 1; i < NSIG; i++) {
Barry Warsaw92971171997-01-03 00:14:25 +0000859 if (Handlers[i].tripped) {
860 PyObject *result = NULL;
861 PyObject *arglist = Py_BuildValue("(iO)", i, f);
862 Handlers[i].tripped = 0;
863
864 if (arglist) {
865 result = PyEval_CallObject(Handlers[i].func,
866 arglist);
Guido van Rossume4485b01994-09-07 14:32:49 +0000867 Py_DECREF(arglist);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000868 }
Barry Warsaw92971171997-01-03 00:14:25 +0000869 if (!result)
Guido van Rossumfcdd0e41997-01-21 06:13:09 +0000870 return -1;
Barry Warsaw92971171997-01-03 00:14:25 +0000871
872 Py_DECREF(result);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000873 }
874 }
Christian Heimesb76922a2007-12-11 01:06:40 +0000875
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000876 return 0;
877}
878
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000879
Barry Warsaw92971171997-01-03 00:14:25 +0000880/* Replacements for intrcheck.c functionality
881 * Declared in pyerrors.h
882 */
883void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000884PyErr_SetInterrupt(void)
Barry Warsaw92971171997-01-03 00:14:25 +0000885{
Christian Heimesb76922a2007-12-11 01:06:40 +0000886 is_tripped = 1;
Barry Warsaw92971171997-01-03 00:14:25 +0000887 Handlers[SIGINT].tripped = 1;
Thomas Wouters334fb892000-07-25 12:56:38 +0000888 Py_AddPendingCall((int (*)(void *))PyErr_CheckSignals, NULL);
Barry Warsaw92971171997-01-03 00:14:25 +0000889}
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000890
891void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000892PyOS_InitInterrupts(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000893{
894 initsignal();
Guido van Rossum08c16611997-08-02 03:01:42 +0000895 _PyImport_FixupExtension("signal", "signal");
896}
897
898void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000899PyOS_FiniInterrupts(void)
Guido van Rossum08c16611997-08-02 03:01:42 +0000900{
901 finisignal();
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000902}
903
904int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000905PyOS_InterruptOccurred(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000906{
Barry Warsaw92971171997-01-03 00:14:25 +0000907 if (Handlers[SIGINT].tripped) {
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000908#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000909 if (PyThread_get_thread_ident() != main_thread)
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000910 return 0;
911#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000912 Handlers[SIGINT].tripped = 0;
Guido van Rossum6299d1e1994-05-31 12:51:13 +0000913 return 1;
914 }
915 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000916}
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000917
918void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000919PyOS_AfterFork(void)
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000920{
921#ifdef WITH_THREAD
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000922 PyEval_ReInitThreads();
Guido van Rossum65d5b571998-12-21 19:32:43 +0000923 main_thread = PyThread_get_thread_ident();
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000924 main_pid = getpid();
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +0000925 _PyImport_ReInitLock();
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000926#endif
927}