blob: eb4c25a08b24674a2ea61ca7e01a394e41ce9bb6 [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)
31# define NSIG _NSIG /* For BSD/SysV */
32# elif defined(_SIGMAX)
33# define NSIG (_SIGMAX + 1) /* For QNX */
34# elif defined(SIGMAX)
35# define NSIG (SIGMAX + 1) /* For djgpp */
36# else
37# define NSIG 64 /* Use a reasonable default value */
38# 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 {
79 int tripped;
80 PyObject *func;
81} 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
110static inline double
111double_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)
123 return NULL;
124
125 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_value)))) {
126 Py_DECREF(r);
127 return NULL;
128 }
129
130 PyTuple_SET_ITEM(r, 0, v);
131
132 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_interval)))) {
133 Py_DECREF(r);
134 return NULL;
135 }
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{
Guido van Rossume4485b01994-09-07 14:32:49 +0000146 PyErr_SetNone(PyExc_KeyboardInterrupt);
Barry Warsaw92971171997-01-03 00:14:25 +0000147 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{
160 return PyErr_CheckSignals();
161}
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
168 if (PyThread_get_thread_ident() != main_thread) {
169 pth_raise(*(pth_t *) main_thread, sig_num);
170 return;
171 }
172#endif
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000173 /* See NOTES section above */
174 if (getpid() == main_pid) {
175#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000176 Handlers[sig_num].tripped = 1;
Christian Heimesb76922a2007-12-11 01:06:40 +0000177 /* Set is_tripped after setting .tripped, as it gets
178 cleared in PyErr_CheckSignals() before .tripped. */
179 is_tripped = 1;
Thomas Wouters0796b002000-07-22 23:49:30 +0000180 Py_AddPendingCall(checksignals_witharg, NULL);
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000181 if (wakeup_fd != -1)
182 write(wakeup_fd, "\0", 1);
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000183#ifdef WITH_THREAD
184 }
185#endif
Guido van Rossum602099a1994-09-14 13:32:22 +0000186#ifdef SIGCHLD
187 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... */
Tim Peters4f1b2082000-07-23 21:18:09 +0000192 return;
Guido van Rossum602099a1994-09-14 13:32:22 +0000193 }
194#endif
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000195 PyOS_setsig(sig_num, signal_handler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000196}
Guido van Rossume4485b01994-09-07 14:32:49 +0000197
Guido van Rossum06d511d1995-03-10 15:13:48 +0000198
Guido van Rossum1171ee61997-08-22 20:42:00 +0000199#ifdef HAVE_ALARM
Guido van Rossume4485b01994-09-07 14:32:49 +0000200static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000201signal_alarm(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000202{
203 int t;
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000204 if (!PyArg_ParseTuple(args, "i:alarm", &t))
Barry Warsaw92971171997-01-03 00:14:25 +0000205 return NULL;
Guido van Rossume4485b01994-09-07 14:32:49 +0000206 /* alarm() returns the number of seconds remaining */
Christian Heimes217cfd12007-12-02 14:31:20 +0000207 return PyLong_FromLong((long)alarm(t));
Guido van Rossumaa0f4c71994-08-23 13:49:37 +0000208}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000209
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000210PyDoc_STRVAR(alarm_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000211"alarm(seconds)\n\
212\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000213Arrange for SIGALRM to arrive after the given number of seconds.");
Guido van Rossum06d511d1995-03-10 15:13:48 +0000214#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000215
Guido van Rossum1171ee61997-08-22 20:42:00 +0000216#ifdef HAVE_PAUSE
Guido van Rossuma597dde1995-01-10 20:56:29 +0000217static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000218signal_pause(PyObject *self)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000219{
Guido van Rossuma597dde1995-01-10 20:56:29 +0000220 Py_BEGIN_ALLOW_THREADS
Barry Warsaw92971171997-01-03 00:14:25 +0000221 (void)pause();
Guido van Rossuma597dde1995-01-10 20:56:29 +0000222 Py_END_ALLOW_THREADS
Barry Warsaw92971171997-01-03 00:14:25 +0000223 /* make sure that any exceptions that got raised are propagated
224 * back into Python
225 */
226 if (PyErr_CheckSignals())
227 return NULL;
228
Guido van Rossume4485b01994-09-07 14:32:49 +0000229 Py_INCREF(Py_None);
230 return Py_None;
231}
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000232PyDoc_STRVAR(pause_doc,
Barry Warsaw1ee36ff1998-07-21 22:41:18 +0000233"pause()\n\
234\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000235Wait until a signal arrives.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000236
Guido van Rossum06d511d1995-03-10 15:13:48 +0000237#endif
Guido van Rossume4485b01994-09-07 14:32:49 +0000238
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000239
Guido van Rossume4485b01994-09-07 14:32:49 +0000240static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000241signal_signal(PyObject *self, PyObject *args)
Guido van Rossume4485b01994-09-07 14:32:49 +0000242{
243 PyObject *obj;
244 int sig_num;
245 PyObject *old_handler;
Tim Peters4f1b2082000-07-23 21:18:09 +0000246 void (*func)(int);
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000247 if (!PyArg_ParseTuple(args, "iO:signal", &sig_num, &obj))
Barry Warsaw92971171997-01-03 00:14:25 +0000248 return NULL;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000249#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000250 if (PyThread_get_thread_ident() != main_thread) {
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000251 PyErr_SetString(PyExc_ValueError,
252 "signal only works in main thread");
Barry Warsaw92971171997-01-03 00:14:25 +0000253 return NULL;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000254 }
255#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000256 if (sig_num < 1 || sig_num >= NSIG) {
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000257 PyErr_SetString(PyExc_ValueError,
258 "signal number out of range");
Barry Warsaw92971171997-01-03 00:14:25 +0000259 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000260 }
Barry Warsaw92971171997-01-03 00:14:25 +0000261 if (obj == IgnoreHandler)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000262 func = SIG_IGN;
Barry Warsaw92971171997-01-03 00:14:25 +0000263 else if (obj == DefaultHandler)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000264 func = SIG_DFL;
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000265 else if (!PyCallable_Check(obj)) {
Guido van Rossume4485b01994-09-07 14:32:49 +0000266 PyErr_SetString(PyExc_TypeError,
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000267"signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object");
Barry Warsaw92971171997-01-03 00:14:25 +0000268 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000269 }
270 else
Barry Warsaw92971171997-01-03 00:14:25 +0000271 func = signal_handler;
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000272 if (PyOS_setsig(sig_num, func) == SIG_ERR) {
Guido van Rossume4485b01994-09-07 14:32:49 +0000273 PyErr_SetFromErrno(PyExc_RuntimeError);
Barry Warsaw92971171997-01-03 00:14:25 +0000274 return NULL;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000275 }
Barry Warsaw92971171997-01-03 00:14:25 +0000276 old_handler = Handlers[sig_num].func;
277 Handlers[sig_num].tripped = 0;
Guido van Rossume4485b01994-09-07 14:32:49 +0000278 Py_INCREF(obj);
Barry Warsaw92971171997-01-03 00:14:25 +0000279 Handlers[sig_num].func = obj;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000280 return old_handler;
281}
282
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000283PyDoc_STRVAR(signal_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000284"signal(sig, action) -> action\n\
285\n\
286Set the action for the given signal. The action can be SIG_DFL,\n\
287SIG_IGN, or a callable Python object. The previous action is\n\
288returned. See getsignal() for possible return values.\n\
289\n\
290*** IMPORTANT NOTICE ***\n\
291A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000292the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000293
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000294
Guido van Rossume4485b01994-09-07 14:32:49 +0000295static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000296signal_getsignal(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000297{
298 int sig_num;
Guido van Rossume4485b01994-09-07 14:32:49 +0000299 PyObject *old_handler;
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000300 if (!PyArg_ParseTuple(args, "i:getsignal", &sig_num))
Barry Warsaw92971171997-01-03 00:14:25 +0000301 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000302 if (sig_num < 1 || sig_num >= NSIG) {
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000303 PyErr_SetString(PyExc_ValueError,
304 "signal number out of range");
Barry Warsaw92971171997-01-03 00:14:25 +0000305 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000306 }
Barry Warsaw92971171997-01-03 00:14:25 +0000307 old_handler = Handlers[sig_num].func;
Guido van Rossume4485b01994-09-07 14:32:49 +0000308 Py_INCREF(old_handler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000309 return old_handler;
310}
311
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000312PyDoc_STRVAR(getsignal_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000313"getsignal(sig) -> action\n\
314\n\
315Return the current action for the given signal. The return value can be:\n\
316SIG_IGN -- if the signal is being ignored\n\
317SIG_DFL -- if the default action for the signal is in effect\n\
318None -- if an unknown handler is in effect\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000319anything else -- the callable Python object used as a handler");
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000320
Christian Heimes8640e742008-02-23 16:23:06 +0000321#ifdef HAVE_SIGINTERRUPT
322PyDoc_STRVAR(siginterrupt_doc,
323"siginterrupt(sig, flag) -> None\n\
324change system call restart behaviour: if flag is False, system calls\n\
325will be restarted when interrupted by signal sig, else system calls\n\
326will be interrupted.");
327
328static PyObject *
329signal_siginterrupt(PyObject *self, PyObject *args)
330{
331 int sig_num;
332 int flag;
333
334 if (!PyArg_ParseTuple(args, "ii:siginterrupt", &sig_num, &flag))
335 return NULL;
336 if (sig_num < 1 || sig_num >= NSIG) {
337 PyErr_SetString(PyExc_ValueError,
338 "signal number out of range");
339 return NULL;
340 }
341 if (siginterrupt(sig_num, flag)<0) {
342 PyErr_SetFromErrno(PyExc_RuntimeError);
343 return NULL;
344 }
345
346 Py_INCREF(Py_None);
347 return Py_None;
348}
349
350#endif
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000351
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000352static PyObject *
353signal_set_wakeup_fd(PyObject *self, PyObject *args)
354{
355 struct stat buf;
356 int fd, old_fd;
357 if (!PyArg_ParseTuple(args, "i:set_wakeup_fd", &fd))
358 return NULL;
359#ifdef WITH_THREAD
360 if (PyThread_get_thread_ident() != main_thread) {
361 PyErr_SetString(PyExc_ValueError,
362 "set_wakeup_fd only works in main thread");
363 return NULL;
364 }
365#endif
366 if (fd != -1 && fstat(fd, &buf) != 0) {
367 PyErr_SetString(PyExc_ValueError, "invalid fd");
368 return NULL;
369 }
370 old_fd = wakeup_fd;
371 wakeup_fd = fd;
372 return PyLong_FromLong(old_fd);
373}
374
375PyDoc_STRVAR(set_wakeup_fd_doc,
376"set_wakeup_fd(fd) -> fd\n\
377\n\
378Sets the fd to be written to (with '\\0') when a signal\n\
379comes in. A library can use this to wakeup select or poll.\n\
380The previous fd is returned.\n\
381\n\
382The fd must be non-blocking.");
383
384/* C API for the same, without all the error checking */
385int
386PySignal_SetWakeupFd(int fd)
387{
388 int old_fd = wakeup_fd;
389 if (fd < 0)
390 fd = -1;
391 wakeup_fd = fd;
392 return old_fd;
393}
394
395
Martin v. Löwis823725e2008-03-24 13:39:54 +0000396#ifdef HAVE_SETITIMER
397static PyObject *
398signal_setitimer(PyObject *self, PyObject *args)
399{
400 double first;
401 double interval = 0;
402 int which;
403 struct itimerval new, old;
404
405 if(!PyArg_ParseTuple(args, "id|d:setitimer", &which, &first, &interval))
406 return NULL;
407
408 timeval_from_double(first, &new.it_value);
409 timeval_from_double(interval, &new.it_interval);
410 /* Let OS check "which" value */
411 if (setitimer(which, &new, &old) != 0) {
412 PyErr_SetFromErrno(ItimerError);
413 return NULL;
414 }
415
416 return itimer_retval(&old);
417}
418
419PyDoc_STRVAR(setitimer_doc,
420"setitimer(which, seconds[, interval])\n\
421\n\
422Sets given itimer (one of ITIMER_REAL, ITIMER_VIRTUAL\n\
423or ITIMER_PROF) to fire after value seconds and after\n\
424that every interval seconds.\n\
425The itimer can be cleared by setting seconds to zero.\n\
426\n\
427Returns old values as a tuple: (delay, interval).");
428#endif
429
430
431#ifdef HAVE_GETITIMER
432static PyObject *
433signal_getitimer(PyObject *self, PyObject *args)
434{
435 int which;
436 struct itimerval old;
437
438 if (!PyArg_ParseTuple(args, "i:getitimer", &which))
439 return NULL;
440
441 if (getitimer(which, &old) != 0) {
442 PyErr_SetFromErrno(ItimerError);
443 return NULL;
444 }
445
446 return itimer_retval(&old);
447}
448
449PyDoc_STRVAR(getitimer_doc,
450"getitimer(which)\n\
451\n\
452Returns current value of given itimer.");
453#endif
454
455
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000456/* List of functions defined in the module */
Barry Warsaw92971171997-01-03 00:14:25 +0000457static PyMethodDef signal_methods[] = {
Guido van Rossum1171ee61997-08-22 20:42:00 +0000458#ifdef HAVE_ALARM
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000459 {"alarm", signal_alarm, METH_VARARGS, alarm_doc},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000460#endif
Martin v. Löwis823725e2008-03-24 13:39:54 +0000461#ifdef HAVE_SETITIMER
462 {"setitimer", signal_setitimer, METH_VARARGS, setitimer_doc},
463#endif
464#ifdef HAVE_GETITIMER
465 {"getitimer", signal_getitimer, METH_VARARGS, getitimer_doc},
466#endif
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000467 {"signal", signal_signal, METH_VARARGS, signal_doc},
468 {"getsignal", signal_getsignal, METH_VARARGS, getsignal_doc},
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000469 {"set_wakeup_fd", signal_set_wakeup_fd, METH_VARARGS, set_wakeup_fd_doc},
Christian Heimes8640e742008-02-23 16:23:06 +0000470#ifdef HAVE_SIGINTERRUPT
471 {"siginterrupt", signal_siginterrupt, METH_VARARGS, siginterrupt_doc},
472#endif
Guido van Rossum1171ee61997-08-22 20:42:00 +0000473#ifdef HAVE_PAUSE
Neil Schemenauer78662cf2002-03-28 21:04:14 +0000474 {"pause", (PyCFunction)signal_pause,
475 METH_NOARGS,pause_doc},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000476#endif
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000477 {"default_int_handler", signal_default_int_handler,
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000478 METH_VARARGS, default_int_handler_doc},
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000479 {NULL, NULL} /* sentinel */
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000480};
481
Barry Warsaw92971171997-01-03 00:14:25 +0000482
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000483PyDoc_STRVAR(module_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000484"This module provides mechanisms to use signal handlers in Python.\n\
485\n\
486Functions:\n\
487\n\
488alarm() -- cause SIGALRM after a specified time [Unix only]\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +0000489setitimer() -- cause a signal (described below) after a specified\n\
490 float time and the timer may restart then [Unix only]\n\
491getitimer() -- get current value of timer [Unix only]\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000492signal() -- set the action for a given signal\n\
493getsignal() -- get the signal action for a given signal\n\
494pause() -- wait until a signal arrives [Unix only]\n\
495default_int_handler() -- default SIGINT handler\n\
496\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +0000497signal constants:\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000498SIG_DFL -- used to refer to the system default handler\n\
499SIG_IGN -- used to ignore the signal\n\
500NSIG -- number of defined signals\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000501SIGINT, SIGTERM, etc. -- signal numbers\n\
502\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +0000503itimer constants:\n\
504ITIMER_REAL -- decrements in real time, and delivers SIGALRM upon\n\
505 expiration\n\
506ITIMER_VIRTUAL -- decrements only when the process is executing,\n\
507 and delivers SIGVTALRM upon expiration\n\
508ITIMER_PROF -- decrements both when the process is executing and\n\
509 when the system is executing on behalf of the process.\n\
510 Coupled with ITIMER_VIRTUAL, this timer is usually\n\
511 used to profile the time spent by the application\n\
512 in user and kernel space. SIGPROF is delivered upon\n\
513 expiration.\n\
514\n\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000515*** IMPORTANT NOTICE ***\n\
516A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000517the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000518
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000519PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000520initsignal(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000521{
Guido van Rossume4485b01994-09-07 14:32:49 +0000522 PyObject *m, *d, *x;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000523 int i;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000524
525#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000526 main_thread = PyThread_get_thread_ident();
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000527 main_pid = getpid();
528#endif
529
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000530 /* Create the module and add the functions */
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000531 m = Py_InitModule3("signal", signal_methods, module_doc);
Neal Norwitz1ac754f2006-01-19 06:09:39 +0000532 if (m == NULL)
533 return;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000534
535 /* Add some symbolic constants to the module */
Guido van Rossume4485b01994-09-07 14:32:49 +0000536 d = PyModule_GetDict(m);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000537
Guido van Rossum276fa432000-06-30 23:04:18 +0000538 x = DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL);
Barry Warsaw92971171997-01-03 00:14:25 +0000539 if (!x || PyDict_SetItemString(d, "SIG_DFL", x) < 0)
540 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000541
Guido van Rossum276fa432000-06-30 23:04:18 +0000542 x = IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN);
Barry Warsaw92971171997-01-03 00:14:25 +0000543 if (!x || PyDict_SetItemString(d, "SIG_IGN", x) < 0)
544 goto finally;
545
Christian Heimes217cfd12007-12-02 14:31:20 +0000546 x = PyLong_FromLong((long)NSIG);
Barry Warsaw92971171997-01-03 00:14:25 +0000547 if (!x || PyDict_SetItemString(d, "NSIG", x) < 0)
548 goto finally;
Guido van Rossum08c16611997-08-02 03:01:42 +0000549 Py_DECREF(x);
Barry Warsaw92971171997-01-03 00:14:25 +0000550
551 x = IntHandler = PyDict_GetItemString(d, "default_int_handler");
552 if (!x)
553 goto finally;
Guido van Rossum08c16611997-08-02 03:01:42 +0000554 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +0000555
556 Handlers[0].tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000557 for (i = 1; i < NSIG; i++) {
Tim Peters4f1b2082000-07-23 21:18:09 +0000558 void (*t)(int);
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000559 t = PyOS_getsig(i);
Barry Warsaw92971171997-01-03 00:14:25 +0000560 Handlers[i].tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000561 if (t == SIG_DFL)
Barry Warsaw92971171997-01-03 00:14:25 +0000562 Handlers[i].func = DefaultHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000563 else if (t == SIG_IGN)
Barry Warsaw92971171997-01-03 00:14:25 +0000564 Handlers[i].func = IgnoreHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000565 else
Barry Warsaw92971171997-01-03 00:14:25 +0000566 Handlers[i].func = Py_None; /* None of our business */
567 Py_INCREF(Handlers[i].func);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000568 }
Barry Warsaw92971171997-01-03 00:14:25 +0000569 if (Handlers[SIGINT].func == DefaultHandler) {
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000570 /* Install default int handler */
Guido van Rossum08c16611997-08-02 03:01:42 +0000571 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +0000572 Py_DECREF(Handlers[SIGINT].func);
573 Handlers[SIGINT].func = IntHandler;
Neal Norwitz41785152002-06-13 21:42:51 +0000574 old_siginthandler = PyOS_setsig(SIGINT, signal_handler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000575 }
576
577#ifdef SIGHUP
Christian Heimes217cfd12007-12-02 14:31:20 +0000578 x = PyLong_FromLong(SIGHUP);
Guido van Rossume4485b01994-09-07 14:32:49 +0000579 PyDict_SetItemString(d, "SIGHUP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000580 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000581#endif
582#ifdef SIGINT
Christian Heimes217cfd12007-12-02 14:31:20 +0000583 x = PyLong_FromLong(SIGINT);
Guido van Rossume4485b01994-09-07 14:32:49 +0000584 PyDict_SetItemString(d, "SIGINT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000585 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000586#endif
Tim Peters1ce3cf72001-10-01 17:58:40 +0000587#ifdef SIGBREAK
Christian Heimes217cfd12007-12-02 14:31:20 +0000588 x = PyLong_FromLong(SIGBREAK);
Tim Peters1ce3cf72001-10-01 17:58:40 +0000589 PyDict_SetItemString(d, "SIGBREAK", x);
590 Py_XDECREF(x);
591#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000592#ifdef SIGQUIT
Christian Heimes217cfd12007-12-02 14:31:20 +0000593 x = PyLong_FromLong(SIGQUIT);
Guido van Rossume4485b01994-09-07 14:32:49 +0000594 PyDict_SetItemString(d, "SIGQUIT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000595 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000596#endif
597#ifdef SIGILL
Christian Heimes217cfd12007-12-02 14:31:20 +0000598 x = PyLong_FromLong(SIGILL);
Guido van Rossume4485b01994-09-07 14:32:49 +0000599 PyDict_SetItemString(d, "SIGILL", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000600 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000601#endif
602#ifdef SIGTRAP
Christian Heimes217cfd12007-12-02 14:31:20 +0000603 x = PyLong_FromLong(SIGTRAP);
Guido van Rossume4485b01994-09-07 14:32:49 +0000604 PyDict_SetItemString(d, "SIGTRAP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000605 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000606#endif
607#ifdef SIGIOT
Christian Heimes217cfd12007-12-02 14:31:20 +0000608 x = PyLong_FromLong(SIGIOT);
Guido van Rossume4485b01994-09-07 14:32:49 +0000609 PyDict_SetItemString(d, "SIGIOT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000610 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000611#endif
612#ifdef SIGABRT
Christian Heimes217cfd12007-12-02 14:31:20 +0000613 x = PyLong_FromLong(SIGABRT);
Guido van Rossume4485b01994-09-07 14:32:49 +0000614 PyDict_SetItemString(d, "SIGABRT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000615 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000616#endif
617#ifdef SIGEMT
Christian Heimes217cfd12007-12-02 14:31:20 +0000618 x = PyLong_FromLong(SIGEMT);
Guido van Rossume4485b01994-09-07 14:32:49 +0000619 PyDict_SetItemString(d, "SIGEMT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000620 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000621#endif
622#ifdef SIGFPE
Christian Heimes217cfd12007-12-02 14:31:20 +0000623 x = PyLong_FromLong(SIGFPE);
Guido van Rossume4485b01994-09-07 14:32:49 +0000624 PyDict_SetItemString(d, "SIGFPE", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000625 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000626#endif
627#ifdef SIGKILL
Christian Heimes217cfd12007-12-02 14:31:20 +0000628 x = PyLong_FromLong(SIGKILL);
Guido van Rossume4485b01994-09-07 14:32:49 +0000629 PyDict_SetItemString(d, "SIGKILL", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000630 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000631#endif
632#ifdef SIGBUS
Christian Heimes217cfd12007-12-02 14:31:20 +0000633 x = PyLong_FromLong(SIGBUS);
Guido van Rossume4485b01994-09-07 14:32:49 +0000634 PyDict_SetItemString(d, "SIGBUS", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000635 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000636#endif
637#ifdef SIGSEGV
Christian Heimes217cfd12007-12-02 14:31:20 +0000638 x = PyLong_FromLong(SIGSEGV);
Guido van Rossume4485b01994-09-07 14:32:49 +0000639 PyDict_SetItemString(d, "SIGSEGV", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000640 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000641#endif
642#ifdef SIGSYS
Christian Heimes217cfd12007-12-02 14:31:20 +0000643 x = PyLong_FromLong(SIGSYS);
Guido van Rossume4485b01994-09-07 14:32:49 +0000644 PyDict_SetItemString(d, "SIGSYS", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000645 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000646#endif
647#ifdef SIGPIPE
Christian Heimes217cfd12007-12-02 14:31:20 +0000648 x = PyLong_FromLong(SIGPIPE);
Guido van Rossume4485b01994-09-07 14:32:49 +0000649 PyDict_SetItemString(d, "SIGPIPE", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000650 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000651#endif
652#ifdef SIGALRM
Christian Heimes217cfd12007-12-02 14:31:20 +0000653 x = PyLong_FromLong(SIGALRM);
Guido van Rossume4485b01994-09-07 14:32:49 +0000654 PyDict_SetItemString(d, "SIGALRM", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000655 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000656#endif
657#ifdef SIGTERM
Christian Heimes217cfd12007-12-02 14:31:20 +0000658 x = PyLong_FromLong(SIGTERM);
Guido van Rossume4485b01994-09-07 14:32:49 +0000659 PyDict_SetItemString(d, "SIGTERM", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000660 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000661#endif
662#ifdef SIGUSR1
Christian Heimes217cfd12007-12-02 14:31:20 +0000663 x = PyLong_FromLong(SIGUSR1);
Guido van Rossume4485b01994-09-07 14:32:49 +0000664 PyDict_SetItemString(d, "SIGUSR1", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000665 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000666#endif
667#ifdef SIGUSR2
Christian Heimes217cfd12007-12-02 14:31:20 +0000668 x = PyLong_FromLong(SIGUSR2);
Guido van Rossume4485b01994-09-07 14:32:49 +0000669 PyDict_SetItemString(d, "SIGUSR2", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000670 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000671#endif
672#ifdef SIGCLD
Christian Heimes217cfd12007-12-02 14:31:20 +0000673 x = PyLong_FromLong(SIGCLD);
Guido van Rossume4485b01994-09-07 14:32:49 +0000674 PyDict_SetItemString(d, "SIGCLD", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000675 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000676#endif
677#ifdef SIGCHLD
Christian Heimes217cfd12007-12-02 14:31:20 +0000678 x = PyLong_FromLong(SIGCHLD);
Guido van Rossume4485b01994-09-07 14:32:49 +0000679 PyDict_SetItemString(d, "SIGCHLD", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000680 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000681#endif
682#ifdef SIGPWR
Christian Heimes217cfd12007-12-02 14:31:20 +0000683 x = PyLong_FromLong(SIGPWR);
Guido van Rossume4485b01994-09-07 14:32:49 +0000684 PyDict_SetItemString(d, "SIGPWR", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000685 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000686#endif
687#ifdef SIGIO
Christian Heimes217cfd12007-12-02 14:31:20 +0000688 x = PyLong_FromLong(SIGIO);
Guido van Rossume4485b01994-09-07 14:32:49 +0000689 PyDict_SetItemString(d, "SIGIO", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000690 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000691#endif
692#ifdef SIGURG
Christian Heimes217cfd12007-12-02 14:31:20 +0000693 x = PyLong_FromLong(SIGURG);
Guido van Rossume4485b01994-09-07 14:32:49 +0000694 PyDict_SetItemString(d, "SIGURG", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000695 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000696#endif
697#ifdef SIGWINCH
Christian Heimes217cfd12007-12-02 14:31:20 +0000698 x = PyLong_FromLong(SIGWINCH);
Guido van Rossume4485b01994-09-07 14:32:49 +0000699 PyDict_SetItemString(d, "SIGWINCH", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000700 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000701#endif
702#ifdef SIGPOLL
Christian Heimes217cfd12007-12-02 14:31:20 +0000703 x = PyLong_FromLong(SIGPOLL);
Guido van Rossume4485b01994-09-07 14:32:49 +0000704 PyDict_SetItemString(d, "SIGPOLL", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000705 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000706#endif
707#ifdef SIGSTOP
Christian Heimes217cfd12007-12-02 14:31:20 +0000708 x = PyLong_FromLong(SIGSTOP);
Guido van Rossume4485b01994-09-07 14:32:49 +0000709 PyDict_SetItemString(d, "SIGSTOP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000710 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000711#endif
712#ifdef SIGTSTP
Christian Heimes217cfd12007-12-02 14:31:20 +0000713 x = PyLong_FromLong(SIGTSTP);
Guido van Rossume4485b01994-09-07 14:32:49 +0000714 PyDict_SetItemString(d, "SIGTSTP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000715 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000716#endif
717#ifdef SIGCONT
Christian Heimes217cfd12007-12-02 14:31:20 +0000718 x = PyLong_FromLong(SIGCONT);
Guido van Rossume4485b01994-09-07 14:32:49 +0000719 PyDict_SetItemString(d, "SIGCONT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000720 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000721#endif
722#ifdef SIGTTIN
Christian Heimes217cfd12007-12-02 14:31:20 +0000723 x = PyLong_FromLong(SIGTTIN);
Guido van Rossume4485b01994-09-07 14:32:49 +0000724 PyDict_SetItemString(d, "SIGTTIN", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000725 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000726#endif
727#ifdef SIGTTOU
Christian Heimes217cfd12007-12-02 14:31:20 +0000728 x = PyLong_FromLong(SIGTTOU);
Guido van Rossume4485b01994-09-07 14:32:49 +0000729 PyDict_SetItemString(d, "SIGTTOU", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000730 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000731#endif
732#ifdef SIGVTALRM
Christian Heimes217cfd12007-12-02 14:31:20 +0000733 x = PyLong_FromLong(SIGVTALRM);
Guido van Rossume4485b01994-09-07 14:32:49 +0000734 PyDict_SetItemString(d, "SIGVTALRM", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000735 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000736#endif
737#ifdef SIGPROF
Christian Heimes217cfd12007-12-02 14:31:20 +0000738 x = PyLong_FromLong(SIGPROF);
Guido van Rossume4485b01994-09-07 14:32:49 +0000739 PyDict_SetItemString(d, "SIGPROF", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000740 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000741#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000742#ifdef SIGXCPU
Christian Heimes217cfd12007-12-02 14:31:20 +0000743 x = PyLong_FromLong(SIGXCPU);
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000744 PyDict_SetItemString(d, "SIGXCPU", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000745 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000746#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000747#ifdef SIGXFSZ
Christian Heimes217cfd12007-12-02 14:31:20 +0000748 x = PyLong_FromLong(SIGXFSZ);
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000749 PyDict_SetItemString(d, "SIGXFSZ", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000750 Py_XDECREF(x);
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000751#endif
Anthony Baxterf37f37d2003-07-31 10:35:29 +0000752#ifdef SIGRTMIN
Christian Heimes217cfd12007-12-02 14:31:20 +0000753 x = PyLong_FromLong(SIGRTMIN);
Anthony Baxterf37f37d2003-07-31 10:35:29 +0000754 PyDict_SetItemString(d, "SIGRTMIN", x);
755 Py_XDECREF(x);
756#endif
757#ifdef SIGRTMAX
Christian Heimes217cfd12007-12-02 14:31:20 +0000758 x = PyLong_FromLong(SIGRTMAX);
Anthony Baxterf37f37d2003-07-31 10:35:29 +0000759 PyDict_SetItemString(d, "SIGRTMAX", x);
760 Py_XDECREF(x);
761#endif
Martin v. Löwis175af252002-01-12 11:43:25 +0000762#ifdef SIGINFO
Christian Heimes217cfd12007-12-02 14:31:20 +0000763 x = PyLong_FromLong(SIGINFO);
Martin v. Löwis175af252002-01-12 11:43:25 +0000764 PyDict_SetItemString(d, "SIGINFO", x);
765 Py_XDECREF(x);
766#endif
Martin v. Löwis823725e2008-03-24 13:39:54 +0000767
768#ifdef ITIMER_REAL
769 x = PyLong_FromLong(ITIMER_REAL);
770 PyDict_SetItemString(d, "ITIMER_REAL", x);
771 Py_DECREF(x);
772#endif
773#ifdef ITIMER_VIRTUAL
774 x = PyLong_FromLong(ITIMER_VIRTUAL);
775 PyDict_SetItemString(d, "ITIMER_VIRTUAL", x);
776 Py_DECREF(x);
777#endif
778#ifdef ITIMER_PROF
779 x = PyLong_FromLong(ITIMER_PROF);
780 PyDict_SetItemString(d, "ITIMER_PROF", x);
781 Py_DECREF(x);
782#endif
783
784#if defined (HAVE_SETITIMER) || defined (HAVE_GETITIMER)
785 ItimerError = PyErr_NewException("signal.ItimerError",
786 PyExc_IOError, NULL);
787 PyDict_SetItemString(d, "ItimerError", ItimerError);
788#endif
789
Barry Warsaw92971171997-01-03 00:14:25 +0000790 if (!PyErr_Occurred())
791 return;
792
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000793 /* Check for errors */
Barry Warsaw92971171997-01-03 00:14:25 +0000794 finally:
Guido van Rossum08c16611997-08-02 03:01:42 +0000795 return;
796}
797
798static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000799finisignal(void)
Guido van Rossum08c16611997-08-02 03:01:42 +0000800{
801 int i;
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000802 PyObject *func;
Guido van Rossum08c16611997-08-02 03:01:42 +0000803
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000804 PyOS_setsig(SIGINT, old_siginthandler);
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000805 old_siginthandler = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +0000806
807 for (i = 1; i < NSIG; i++) {
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000808 func = Handlers[i].func;
Guido van Rossum08c16611997-08-02 03:01:42 +0000809 Handlers[i].tripped = 0;
Guido van Rossum08c16611997-08-02 03:01:42 +0000810 Handlers[i].func = NULL;
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000811 if (i != SIGINT && func != NULL && func != Py_None &&
812 func != DefaultHandler && func != IgnoreHandler)
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000813 PyOS_setsig(i, SIG_DFL);
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000814 Py_XDECREF(func);
Guido van Rossum08c16611997-08-02 03:01:42 +0000815 }
816
817 Py_XDECREF(IntHandler);
818 IntHandler = NULL;
819 Py_XDECREF(DefaultHandler);
820 DefaultHandler = NULL;
821 Py_XDECREF(IgnoreHandler);
822 IgnoreHandler = NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000823}
824
Barry Warsaw92971171997-01-03 00:14:25 +0000825
Barry Warsaw92971171997-01-03 00:14:25 +0000826/* Declared in pyerrors.h */
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000827int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000828PyErr_CheckSignals(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000829{
830 int i;
Guido van Rossume4485b01994-09-07 14:32:49 +0000831 PyObject *f;
Barry Warsaw92971171997-01-03 00:14:25 +0000832
833 if (!is_tripped)
Guido van Rossum6299d1e1994-05-31 12:51:13 +0000834 return 0;
Christian Heimesb76922a2007-12-11 01:06:40 +0000835
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000836#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000837 if (PyThread_get_thread_ident() != main_thread)
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000838 return 0;
839#endif
Christian Heimesb76922a2007-12-11 01:06:40 +0000840
841 /*
842 * The is_stripped variable is meant to speed up the calls to
843 * PyErr_CheckSignals (both directly or via pending calls) when no
844 * signal has arrived. This variable is set to 1 when a signal arrives
845 * and it is set to 0 here, when we know some signals arrived. This way
846 * we can run the registered handlers with no signals blocked.
847 *
848 * NOTE: with this approach we can have a situation where is_tripped is
849 * 1 but we have no more signals to handle (Handlers[i].tripped
850 * is 0 for every signal i). This won't do us any harm (except
851 * we're gonna spent some cycles for nothing). This happens when
852 * we receive a signal i after we zero is_tripped and before we
853 * check Handlers[i].tripped.
854 */
855 is_tripped = 0;
856
Guido van Rossum6297a7a2003-02-19 15:53:17 +0000857 if (!(f = (PyObject *)PyEval_GetFrame()))
Guido van Rossume4485b01994-09-07 14:32:49 +0000858 f = Py_None;
Christian Heimesb76922a2007-12-11 01:06:40 +0000859
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000860 for (i = 1; i < NSIG; i++) {
Barry Warsaw92971171997-01-03 00:14:25 +0000861 if (Handlers[i].tripped) {
862 PyObject *result = NULL;
863 PyObject *arglist = Py_BuildValue("(iO)", i, f);
864 Handlers[i].tripped = 0;
865
866 if (arglist) {
867 result = PyEval_CallObject(Handlers[i].func,
868 arglist);
Guido van Rossume4485b01994-09-07 14:32:49 +0000869 Py_DECREF(arglist);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000870 }
Barry Warsaw92971171997-01-03 00:14:25 +0000871 if (!result)
Guido van Rossumfcdd0e41997-01-21 06:13:09 +0000872 return -1;
Barry Warsaw92971171997-01-03 00:14:25 +0000873
874 Py_DECREF(result);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000875 }
876 }
Christian Heimesb76922a2007-12-11 01:06:40 +0000877
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000878 return 0;
879}
880
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000881
Barry Warsaw92971171997-01-03 00:14:25 +0000882/* Replacements for intrcheck.c functionality
883 * Declared in pyerrors.h
884 */
885void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000886PyErr_SetInterrupt(void)
Barry Warsaw92971171997-01-03 00:14:25 +0000887{
Christian Heimesb76922a2007-12-11 01:06:40 +0000888 is_tripped = 1;
Barry Warsaw92971171997-01-03 00:14:25 +0000889 Handlers[SIGINT].tripped = 1;
Thomas Wouters334fb892000-07-25 12:56:38 +0000890 Py_AddPendingCall((int (*)(void *))PyErr_CheckSignals, NULL);
Barry Warsaw92971171997-01-03 00:14:25 +0000891}
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000892
893void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000894PyOS_InitInterrupts(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000895{
896 initsignal();
Guido van Rossum08c16611997-08-02 03:01:42 +0000897 _PyImport_FixupExtension("signal", "signal");
898}
899
900void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000901PyOS_FiniInterrupts(void)
Guido van Rossum08c16611997-08-02 03:01:42 +0000902{
903 finisignal();
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000904}
905
906int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000907PyOS_InterruptOccurred(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000908{
Barry Warsaw92971171997-01-03 00:14:25 +0000909 if (Handlers[SIGINT].tripped) {
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000910#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000911 if (PyThread_get_thread_ident() != main_thread)
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000912 return 0;
913#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000914 Handlers[SIGINT].tripped = 0;
Guido van Rossum6299d1e1994-05-31 12:51:13 +0000915 return 1;
916 }
917 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000918}
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000919
920void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000921PyOS_AfterFork(void)
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000922{
923#ifdef WITH_THREAD
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000924 PyEval_ReInitThreads();
Guido van Rossum65d5b571998-12-21 19:32:43 +0000925 main_thread = PyThread_get_thread_ident();
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000926 main_pid = getpid();
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +0000927 _PyImport_ReInitLock();
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000928#endif
929}