blob: 13806c76147cd18d4c0a5ff702e7f759d9462040 [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
Benjamin Peterson2614cda2010-03-21 22:36:19 +000010#ifdef HAVE_PROCESS_H
Guido van Rossum644a12b1997-04-09 19:24:53 +000011#include <process.h>
12#endif
Benjamin Peterson2614cda2010-03-21 22:36:19 +000013#endif
Guido van Rossum644a12b1997-04-09 19:24:53 +000014
Benjamin Peterson2614cda2010-03-21 22:36:19 +000015#ifdef HAVE_SIGNAL_H
Guido van Rossum398d9fe1994-05-11 08:59:13 +000016#include <signal.h>
Benjamin Peterson2614cda2010-03-21 22:36:19 +000017#endif
18#ifdef HAVE_SYS_STAT_H
Christian Heimes5fb7c2a2007-12-24 08:52:31 +000019#include <sys/stat.h>
Benjamin Peterson2614cda2010-03-21 22:36:19 +000020#endif
Martin v. Löwis3bf3cc02008-03-24 14:05:07 +000021#ifdef HAVE_SYS_TIME_H
Martin v. Löwis823725e2008-03-24 13:39:54 +000022#include <sys/time.h>
Martin v. Löwis3bf3cc02008-03-24 14:05:07 +000023#endif
Christian Heimes5fb7c2a2007-12-24 08:52:31 +000024
Guido van Rossumbb4ba121994-06-23 11:25:45 +000025#ifndef SIG_ERR
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +000026#define SIG_ERR ((PyOS_sighandler_t)(-1))
Guido van Rossumbb4ba121994-06-23 11:25:45 +000027#endif
28
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000029#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000030#define NSIG 12
31#include <process.h>
32#endif
33
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000034#ifndef NSIG
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000035# if defined(_NSIG)
36# define NSIG _NSIG /* For BSD/SysV */
37# elif defined(_SIGMAX)
38# define NSIG (_SIGMAX + 1) /* For QNX */
39# elif defined(SIGMAX)
40# define NSIG (SIGMAX + 1) /* For djgpp */
41# else
42# define NSIG 64 /* Use a reasonable default value */
43# endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000044#endif
45
46
Guido van Rossumbb4ba121994-06-23 11:25:45 +000047/*
48 NOTES ON THE INTERACTION BETWEEN SIGNALS AND THREADS
49
50 When threads are supported, we want the following semantics:
51
52 - only the main thread can set a signal handler
53 - any thread can get a signal handler
54 - signals are only delivered to the main thread
55
56 I.e. we don't support "synchronous signals" like SIGFPE (catching
57 this doesn't make much sense in Python anyway) nor do we support
58 signals as a means of inter-thread communication, since not all
59 thread implementations support that (at least our thread library
60 doesn't).
61
62 We still have the problem that in some implementations signals
63 generated by the keyboard (e.g. SIGINT) are delivered to all
64 threads (e.g. SGI), while in others (e.g. Solaris) such signals are
65 delivered to one random thread (an intermediate possibility would
Guido van Rossuma3c04b01995-01-12 11:29:01 +000066 be to deliver it to the main thread -- POSIX?). For now, we have
Guido van Rossumbb4ba121994-06-23 11:25:45 +000067 a working implementation that works in all three cases -- the
68 handler ignores signals if getpid() isn't the same as in the main
69 thread. XXX This is a hack.
70
Guido van Rossum9e8181b2000-09-19 00:46:46 +000071 GNU pth is a user-space threading library, and as such, all threads
72 run within the same process. In this case, if the currently running
73 thread is not the main_thread, send the signal to the main_thread.
Guido van Rossumbb4ba121994-06-23 11:25:45 +000074*/
75
76#ifdef WITH_THREAD
Guido van Rossum295b8e51997-06-06 21:16:41 +000077#include <sys/types.h> /* For pid_t */
Guido van Rossum49b56061998-10-01 20:42:43 +000078#include "pythread.h"
Guido van Rossumbb4ba121994-06-23 11:25:45 +000079static long main_thread;
80static pid_t main_pid;
81#endif
82
Barry Warsaw92971171997-01-03 00:14:25 +000083static struct {
84 int tripped;
85 PyObject *func;
86} Handlers[NSIG];
Guido van Rossum398d9fe1994-05-11 08:59:13 +000087
Christian Heimes5fb7c2a2007-12-24 08:52:31 +000088static sig_atomic_t wakeup_fd = -1;
89
Christian Heimesb76922a2007-12-11 01:06:40 +000090/* Speed up sigcheck() when none tripped */
91static volatile sig_atomic_t is_tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +000092
Barry Warsaw92971171997-01-03 00:14:25 +000093static PyObject *DefaultHandler;
94static PyObject *IgnoreHandler;
95static PyObject *IntHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +000096
Martin v. Löwisf58de1b2001-03-06 12:13:56 +000097/* On Solaris 8, gcc will produce a warning that the function
98 declaration is not a prototype. This is caused by the definition of
99 SIG_DFL as (void (*)())0; the correct declaration would have been
100 (void (*)(int))0. */
101
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000102static PyOS_sighandler_t old_siginthandler = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +0000103
Martin v. Löwis823725e2008-03-24 13:39:54 +0000104#ifdef HAVE_GETITIMER
105static PyObject *ItimerError;
106
107/* auxiliary functions for setitimer/getitimer */
108static void
109timeval_from_double(double d, struct timeval *tv)
110{
111 tv->tv_sec = floor(d);
112 tv->tv_usec = fmod(d, 1.0) * 1000000.0;
113}
114
Christian Heimes1a8501c2008-10-02 19:56:01 +0000115Py_LOCAL_INLINE(double)
Martin v. Löwis823725e2008-03-24 13:39:54 +0000116double_from_timeval(struct timeval *tv)
117{
118 return tv->tv_sec + (double)(tv->tv_usec / 1000000.0);
119}
120
121static PyObject *
122itimer_retval(struct itimerval *iv)
123{
124 PyObject *r, *v;
125
126 r = PyTuple_New(2);
127 if (r == NULL)
128 return NULL;
129
130 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_value)))) {
131 Py_DECREF(r);
132 return NULL;
133 }
134
135 PyTuple_SET_ITEM(r, 0, v);
136
137 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_interval)))) {
138 Py_DECREF(r);
139 return NULL;
140 }
141
142 PyTuple_SET_ITEM(r, 1, v);
143
144 return r;
145}
146#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000147
Guido van Rossume4485b01994-09-07 14:32:49 +0000148static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000149signal_default_int_handler(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000150{
Guido van Rossume4485b01994-09-07 14:32:49 +0000151 PyErr_SetNone(PyExc_KeyboardInterrupt);
Barry Warsaw92971171997-01-03 00:14:25 +0000152 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000153}
154
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000155PyDoc_STRVAR(default_int_handler_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000156"default_int_handler(...)\n\
157\n\
Michael W. Hudson24ec2112004-06-17 15:55:53 +0000158The default handler for SIGINT installed by Python.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000159It raises KeyboardInterrupt.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000160
Thomas Wouters0796b002000-07-22 23:49:30 +0000161
162static int
163checksignals_witharg(void * unused)
164{
165 return PyErr_CheckSignals();
166}
167
Tim Peters4f1b2082000-07-23 21:18:09 +0000168static void
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000169signal_handler(int sig_num)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000170{
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000171#ifdef WITH_THREAD
Guido van Rossum9e8181b2000-09-19 00:46:46 +0000172#ifdef WITH_PTH
173 if (PyThread_get_thread_ident() != main_thread) {
174 pth_raise(*(pth_t *) main_thread, sig_num);
175 return;
176 }
177#endif
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000178 /* See NOTES section above */
179 if (getpid() == main_pid) {
180#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000181 Handlers[sig_num].tripped = 1;
Christian Heimesb76922a2007-12-11 01:06:40 +0000182 /* Set is_tripped after setting .tripped, as it gets
183 cleared in PyErr_CheckSignals() before .tripped. */
184 is_tripped = 1;
Thomas Wouters0796b002000-07-22 23:49:30 +0000185 Py_AddPendingCall(checksignals_witharg, NULL);
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000186 if (wakeup_fd != -1)
187 write(wakeup_fd, "\0", 1);
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000188#ifdef WITH_THREAD
189 }
190#endif
Guido van Rossum602099a1994-09-14 13:32:22 +0000191#ifdef SIGCHLD
192 if (sig_num == SIGCHLD) {
193 /* To avoid infinite recursion, this signal remains
194 reset until explicit re-instated.
195 Don't clear the 'func' field as it is our pointer
196 to the Python handler... */
Tim Peters4f1b2082000-07-23 21:18:09 +0000197 return;
Guido van Rossum602099a1994-09-14 13:32:22 +0000198 }
199#endif
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000200 PyOS_setsig(sig_num, signal_handler);
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{
208 int t;
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000209 if (!PyArg_ParseTuple(args, "i:alarm", &t))
Barry Warsaw92971171997-01-03 00:14:25 +0000210 return NULL;
Guido van Rossume4485b01994-09-07 14:32:49 +0000211 /* alarm() returns the number of seconds remaining */
Christian Heimes217cfd12007-12-02 14:31:20 +0000212 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{
Guido van Rossuma597dde1995-01-10 20:56:29 +0000225 Py_BEGIN_ALLOW_THREADS
Barry Warsaw92971171997-01-03 00:14:25 +0000226 (void)pause();
Guido van Rossuma597dde1995-01-10 20:56:29 +0000227 Py_END_ALLOW_THREADS
Barry Warsaw92971171997-01-03 00:14:25 +0000228 /* make sure that any exceptions that got raised are propagated
229 * back into Python
230 */
231 if (PyErr_CheckSignals())
232 return NULL;
233
Guido van Rossume4485b01994-09-07 14:32:49 +0000234 Py_INCREF(Py_None);
235 return Py_None;
236}
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{
248 PyObject *obj;
249 int sig_num;
250 PyObject *old_handler;
Tim Peters4f1b2082000-07-23 21:18:09 +0000251 void (*func)(int);
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000252 if (!PyArg_ParseTuple(args, "iO:signal", &sig_num, &obj))
Barry Warsaw92971171997-01-03 00:14:25 +0000253 return NULL;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000254#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000255 if (PyThread_get_thread_ident() != main_thread) {
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000256 PyErr_SetString(PyExc_ValueError,
257 "signal only works in main thread");
Barry Warsaw92971171997-01-03 00:14:25 +0000258 return NULL;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000259 }
260#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000261 if (sig_num < 1 || sig_num >= NSIG) {
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000262 PyErr_SetString(PyExc_ValueError,
263 "signal number out of range");
Barry Warsaw92971171997-01-03 00:14:25 +0000264 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000265 }
Barry Warsaw92971171997-01-03 00:14:25 +0000266 if (obj == IgnoreHandler)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000267 func = SIG_IGN;
Barry Warsaw92971171997-01-03 00:14:25 +0000268 else if (obj == DefaultHandler)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000269 func = SIG_DFL;
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000270 else if (!PyCallable_Check(obj)) {
Guido van Rossume4485b01994-09-07 14:32:49 +0000271 PyErr_SetString(PyExc_TypeError,
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000272"signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object");
Barry Warsaw92971171997-01-03 00:14:25 +0000273 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000274 }
275 else
Barry Warsaw92971171997-01-03 00:14:25 +0000276 func = signal_handler;
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000277 if (PyOS_setsig(sig_num, func) == SIG_ERR) {
Guido van Rossume4485b01994-09-07 14:32:49 +0000278 PyErr_SetFromErrno(PyExc_RuntimeError);
Barry Warsaw92971171997-01-03 00:14:25 +0000279 return NULL;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000280 }
Barry Warsaw92971171997-01-03 00:14:25 +0000281 old_handler = Handlers[sig_num].func;
282 Handlers[sig_num].tripped = 0;
Guido van Rossume4485b01994-09-07 14:32:49 +0000283 Py_INCREF(obj);
Barry Warsaw92971171997-01-03 00:14:25 +0000284 Handlers[sig_num].func = obj;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000285 return old_handler;
286}
287
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000288PyDoc_STRVAR(signal_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000289"signal(sig, action) -> action\n\
290\n\
291Set the action for the given signal. The action can be SIG_DFL,\n\
292SIG_IGN, or a callable Python object. The previous action is\n\
293returned. See getsignal() for possible return values.\n\
294\n\
295*** IMPORTANT NOTICE ***\n\
296A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000297the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000298
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000299
Guido van Rossume4485b01994-09-07 14:32:49 +0000300static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000301signal_getsignal(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000302{
303 int sig_num;
Guido van Rossume4485b01994-09-07 14:32:49 +0000304 PyObject *old_handler;
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000305 if (!PyArg_ParseTuple(args, "i:getsignal", &sig_num))
Barry Warsaw92971171997-01-03 00:14:25 +0000306 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000307 if (sig_num < 1 || sig_num >= NSIG) {
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000308 PyErr_SetString(PyExc_ValueError,
309 "signal number out of range");
Barry Warsaw92971171997-01-03 00:14:25 +0000310 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000311 }
Barry Warsaw92971171997-01-03 00:14:25 +0000312 old_handler = Handlers[sig_num].func;
Guido van Rossume4485b01994-09-07 14:32:49 +0000313 Py_INCREF(old_handler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000314 return old_handler;
315}
316
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000317PyDoc_STRVAR(getsignal_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000318"getsignal(sig) -> action\n\
319\n\
320Return the current action for the given signal. The return value can be:\n\
321SIG_IGN -- if the signal is being ignored\n\
322SIG_DFL -- if the default action for the signal is in effect\n\
323None -- if an unknown handler is in effect\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000324anything else -- the callable Python object used as a handler");
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000325
Christian Heimes8640e742008-02-23 16:23:06 +0000326#ifdef HAVE_SIGINTERRUPT
327PyDoc_STRVAR(siginterrupt_doc,
328"siginterrupt(sig, flag) -> None\n\
329change system call restart behaviour: if flag is False, system calls\n\
330will be restarted when interrupted by signal sig, else system calls\n\
331will be interrupted.");
332
333static PyObject *
334signal_siginterrupt(PyObject *self, PyObject *args)
335{
336 int sig_num;
337 int flag;
338
339 if (!PyArg_ParseTuple(args, "ii:siginterrupt", &sig_num, &flag))
340 return NULL;
341 if (sig_num < 1 || sig_num >= NSIG) {
342 PyErr_SetString(PyExc_ValueError,
343 "signal number out of range");
344 return NULL;
345 }
346 if (siginterrupt(sig_num, flag)<0) {
347 PyErr_SetFromErrno(PyExc_RuntimeError);
348 return NULL;
349 }
350
351 Py_INCREF(Py_None);
352 return Py_None;
353}
354
355#endif
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000356
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000357static PyObject *
358signal_set_wakeup_fd(PyObject *self, PyObject *args)
359{
360 struct stat buf;
361 int fd, old_fd;
362 if (!PyArg_ParseTuple(args, "i:set_wakeup_fd", &fd))
363 return NULL;
364#ifdef WITH_THREAD
365 if (PyThread_get_thread_ident() != main_thread) {
366 PyErr_SetString(PyExc_ValueError,
367 "set_wakeup_fd only works in main thread");
368 return NULL;
369 }
370#endif
371 if (fd != -1 && fstat(fd, &buf) != 0) {
372 PyErr_SetString(PyExc_ValueError, "invalid fd");
373 return NULL;
374 }
375 old_fd = wakeup_fd;
376 wakeup_fd = fd;
377 return PyLong_FromLong(old_fd);
378}
379
380PyDoc_STRVAR(set_wakeup_fd_doc,
381"set_wakeup_fd(fd) -> fd\n\
382\n\
383Sets the fd to be written to (with '\\0') when a signal\n\
384comes in. A library can use this to wakeup select or poll.\n\
385The previous fd is returned.\n\
386\n\
387The fd must be non-blocking.");
388
389/* C API for the same, without all the error checking */
390int
391PySignal_SetWakeupFd(int fd)
392{
393 int old_fd = wakeup_fd;
394 if (fd < 0)
395 fd = -1;
396 wakeup_fd = fd;
397 return old_fd;
398}
399
400
Martin v. Löwis823725e2008-03-24 13:39:54 +0000401#ifdef HAVE_SETITIMER
402static PyObject *
403signal_setitimer(PyObject *self, PyObject *args)
404{
405 double first;
406 double interval = 0;
407 int which;
408 struct itimerval new, old;
409
410 if(!PyArg_ParseTuple(args, "id|d:setitimer", &which, &first, &interval))
411 return NULL;
412
413 timeval_from_double(first, &new.it_value);
414 timeval_from_double(interval, &new.it_interval);
415 /* Let OS check "which" value */
416 if (setitimer(which, &new, &old) != 0) {
417 PyErr_SetFromErrno(ItimerError);
418 return NULL;
419 }
420
421 return itimer_retval(&old);
422}
423
424PyDoc_STRVAR(setitimer_doc,
425"setitimer(which, seconds[, interval])\n\
426\n\
427Sets given itimer (one of ITIMER_REAL, ITIMER_VIRTUAL\n\
428or ITIMER_PROF) to fire after value seconds and after\n\
429that every interval seconds.\n\
430The itimer can be cleared by setting seconds to zero.\n\
431\n\
432Returns old values as a tuple: (delay, interval).");
433#endif
434
435
436#ifdef HAVE_GETITIMER
437static PyObject *
438signal_getitimer(PyObject *self, PyObject *args)
439{
440 int which;
441 struct itimerval old;
442
443 if (!PyArg_ParseTuple(args, "i:getitimer", &which))
444 return NULL;
445
446 if (getitimer(which, &old) != 0) {
447 PyErr_SetFromErrno(ItimerError);
448 return NULL;
449 }
450
451 return itimer_retval(&old);
452}
453
454PyDoc_STRVAR(getitimer_doc,
455"getitimer(which)\n\
456\n\
457Returns current value of given itimer.");
458#endif
459
460
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000461/* List of functions defined in the module */
Barry Warsaw92971171997-01-03 00:14:25 +0000462static PyMethodDef signal_methods[] = {
Guido van Rossum1171ee61997-08-22 20:42:00 +0000463#ifdef HAVE_ALARM
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000464 {"alarm", signal_alarm, METH_VARARGS, alarm_doc},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000465#endif
Martin v. Löwis823725e2008-03-24 13:39:54 +0000466#ifdef HAVE_SETITIMER
467 {"setitimer", signal_setitimer, METH_VARARGS, setitimer_doc},
468#endif
469#ifdef HAVE_GETITIMER
470 {"getitimer", signal_getitimer, METH_VARARGS, getitimer_doc},
471#endif
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000472 {"signal", signal_signal, METH_VARARGS, signal_doc},
473 {"getsignal", signal_getsignal, METH_VARARGS, getsignal_doc},
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000474 {"set_wakeup_fd", signal_set_wakeup_fd, METH_VARARGS, set_wakeup_fd_doc},
Christian Heimes8640e742008-02-23 16:23:06 +0000475#ifdef HAVE_SIGINTERRUPT
476 {"siginterrupt", signal_siginterrupt, METH_VARARGS, siginterrupt_doc},
477#endif
Guido van Rossum1171ee61997-08-22 20:42:00 +0000478#ifdef HAVE_PAUSE
Neil Schemenauer78662cf2002-03-28 21:04:14 +0000479 {"pause", (PyCFunction)signal_pause,
480 METH_NOARGS,pause_doc},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000481#endif
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000482 {"default_int_handler", signal_default_int_handler,
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000483 METH_VARARGS, default_int_handler_doc},
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000484 {NULL, NULL} /* sentinel */
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000485};
486
Barry Warsaw92971171997-01-03 00:14:25 +0000487
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000488PyDoc_STRVAR(module_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000489"This module provides mechanisms to use signal handlers in Python.\n\
490\n\
491Functions:\n\
492\n\
493alarm() -- cause SIGALRM after a specified time [Unix only]\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +0000494setitimer() -- cause a signal (described below) after a specified\n\
495 float time and the timer may restart then [Unix only]\n\
496getitimer() -- get current value of timer [Unix only]\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000497signal() -- set the action for a given signal\n\
498getsignal() -- get the signal action for a given signal\n\
499pause() -- wait until a signal arrives [Unix only]\n\
500default_int_handler() -- default SIGINT handler\n\
501\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +0000502signal constants:\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000503SIG_DFL -- used to refer to the system default handler\n\
504SIG_IGN -- used to ignore the signal\n\
505NSIG -- number of defined signals\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000506SIGINT, SIGTERM, etc. -- signal numbers\n\
507\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +0000508itimer constants:\n\
509ITIMER_REAL -- decrements in real time, and delivers SIGALRM upon\n\
510 expiration\n\
511ITIMER_VIRTUAL -- decrements only when the process is executing,\n\
512 and delivers SIGVTALRM upon expiration\n\
513ITIMER_PROF -- decrements both when the process is executing and\n\
514 when the system is executing on behalf of the process.\n\
515 Coupled with ITIMER_VIRTUAL, this timer is usually\n\
516 used to profile the time spent by the application\n\
517 in user and kernel space. SIGPROF is delivered upon\n\
518 expiration.\n\
519\n\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000520*** IMPORTANT NOTICE ***\n\
521A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000522the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000523
Martin v. Löwis1a214512008-06-11 05:26:20 +0000524static struct PyModuleDef signalmodule = {
525 PyModuleDef_HEAD_INIT,
526 "signal",
527 module_doc,
528 -1,
529 signal_methods,
530 NULL,
531 NULL,
532 NULL,
533 NULL
534};
535
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000536PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000537PyInit_signal(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000538{
Guido van Rossume4485b01994-09-07 14:32:49 +0000539 PyObject *m, *d, *x;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000540 int i;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000541
542#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000543 main_thread = PyThread_get_thread_ident();
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000544 main_pid = getpid();
545#endif
546
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000547 /* Create the module and add the functions */
Martin v. Löwis1a214512008-06-11 05:26:20 +0000548 m = PyModule_Create(&signalmodule);
Neal Norwitz1ac754f2006-01-19 06:09:39 +0000549 if (m == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000550 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000551
552 /* Add some symbolic constants to the module */
Guido van Rossume4485b01994-09-07 14:32:49 +0000553 d = PyModule_GetDict(m);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000554
Guido van Rossum276fa432000-06-30 23:04:18 +0000555 x = DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL);
Barry Warsaw92971171997-01-03 00:14:25 +0000556 if (!x || PyDict_SetItemString(d, "SIG_DFL", x) < 0)
557 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000558
Guido van Rossum276fa432000-06-30 23:04:18 +0000559 x = IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN);
Barry Warsaw92971171997-01-03 00:14:25 +0000560 if (!x || PyDict_SetItemString(d, "SIG_IGN", x) < 0)
561 goto finally;
562
Christian Heimes217cfd12007-12-02 14:31:20 +0000563 x = PyLong_FromLong((long)NSIG);
Barry Warsaw92971171997-01-03 00:14:25 +0000564 if (!x || PyDict_SetItemString(d, "NSIG", x) < 0)
565 goto finally;
Guido van Rossum08c16611997-08-02 03:01:42 +0000566 Py_DECREF(x);
Barry Warsaw92971171997-01-03 00:14:25 +0000567
568 x = IntHandler = PyDict_GetItemString(d, "default_int_handler");
569 if (!x)
570 goto finally;
Guido van Rossum08c16611997-08-02 03:01:42 +0000571 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +0000572
573 Handlers[0].tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000574 for (i = 1; i < NSIG; i++) {
Tim Peters4f1b2082000-07-23 21:18:09 +0000575 void (*t)(int);
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000576 t = PyOS_getsig(i);
Barry Warsaw92971171997-01-03 00:14:25 +0000577 Handlers[i].tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000578 if (t == SIG_DFL)
Barry Warsaw92971171997-01-03 00:14:25 +0000579 Handlers[i].func = DefaultHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000580 else if (t == SIG_IGN)
Barry Warsaw92971171997-01-03 00:14:25 +0000581 Handlers[i].func = IgnoreHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000582 else
Barry Warsaw92971171997-01-03 00:14:25 +0000583 Handlers[i].func = Py_None; /* None of our business */
584 Py_INCREF(Handlers[i].func);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000585 }
Barry Warsaw92971171997-01-03 00:14:25 +0000586 if (Handlers[SIGINT].func == DefaultHandler) {
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000587 /* Install default int handler */
Guido van Rossum08c16611997-08-02 03:01:42 +0000588 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +0000589 Py_DECREF(Handlers[SIGINT].func);
590 Handlers[SIGINT].func = IntHandler;
Neal Norwitz41785152002-06-13 21:42:51 +0000591 old_siginthandler = PyOS_setsig(SIGINT, signal_handler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000592 }
593
594#ifdef SIGHUP
Christian Heimes217cfd12007-12-02 14:31:20 +0000595 x = PyLong_FromLong(SIGHUP);
Guido van Rossume4485b01994-09-07 14:32:49 +0000596 PyDict_SetItemString(d, "SIGHUP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000597 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000598#endif
599#ifdef SIGINT
Christian Heimes217cfd12007-12-02 14:31:20 +0000600 x = PyLong_FromLong(SIGINT);
Guido van Rossume4485b01994-09-07 14:32:49 +0000601 PyDict_SetItemString(d, "SIGINT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000602 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000603#endif
Tim Peters1ce3cf72001-10-01 17:58:40 +0000604#ifdef SIGBREAK
Christian Heimes217cfd12007-12-02 14:31:20 +0000605 x = PyLong_FromLong(SIGBREAK);
Tim Peters1ce3cf72001-10-01 17:58:40 +0000606 PyDict_SetItemString(d, "SIGBREAK", x);
607 Py_XDECREF(x);
608#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000609#ifdef SIGQUIT
Christian Heimes217cfd12007-12-02 14:31:20 +0000610 x = PyLong_FromLong(SIGQUIT);
Guido van Rossume4485b01994-09-07 14:32:49 +0000611 PyDict_SetItemString(d, "SIGQUIT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000612 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000613#endif
614#ifdef SIGILL
Christian Heimes217cfd12007-12-02 14:31:20 +0000615 x = PyLong_FromLong(SIGILL);
Guido van Rossume4485b01994-09-07 14:32:49 +0000616 PyDict_SetItemString(d, "SIGILL", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000617 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000618#endif
619#ifdef SIGTRAP
Christian Heimes217cfd12007-12-02 14:31:20 +0000620 x = PyLong_FromLong(SIGTRAP);
Guido van Rossume4485b01994-09-07 14:32:49 +0000621 PyDict_SetItemString(d, "SIGTRAP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000622 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000623#endif
624#ifdef SIGIOT
Christian Heimes217cfd12007-12-02 14:31:20 +0000625 x = PyLong_FromLong(SIGIOT);
Guido van Rossume4485b01994-09-07 14:32:49 +0000626 PyDict_SetItemString(d, "SIGIOT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000627 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000628#endif
629#ifdef SIGABRT
Christian Heimes217cfd12007-12-02 14:31:20 +0000630 x = PyLong_FromLong(SIGABRT);
Guido van Rossume4485b01994-09-07 14:32:49 +0000631 PyDict_SetItemString(d, "SIGABRT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000632 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000633#endif
634#ifdef SIGEMT
Christian Heimes217cfd12007-12-02 14:31:20 +0000635 x = PyLong_FromLong(SIGEMT);
Guido van Rossume4485b01994-09-07 14:32:49 +0000636 PyDict_SetItemString(d, "SIGEMT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000637 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000638#endif
639#ifdef SIGFPE
Christian Heimes217cfd12007-12-02 14:31:20 +0000640 x = PyLong_FromLong(SIGFPE);
Guido van Rossume4485b01994-09-07 14:32:49 +0000641 PyDict_SetItemString(d, "SIGFPE", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000642 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000643#endif
644#ifdef SIGKILL
Christian Heimes217cfd12007-12-02 14:31:20 +0000645 x = PyLong_FromLong(SIGKILL);
Guido van Rossume4485b01994-09-07 14:32:49 +0000646 PyDict_SetItemString(d, "SIGKILL", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000647 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000648#endif
649#ifdef SIGBUS
Christian Heimes217cfd12007-12-02 14:31:20 +0000650 x = PyLong_FromLong(SIGBUS);
Guido van Rossume4485b01994-09-07 14:32:49 +0000651 PyDict_SetItemString(d, "SIGBUS", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000652 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000653#endif
654#ifdef SIGSEGV
Christian Heimes217cfd12007-12-02 14:31:20 +0000655 x = PyLong_FromLong(SIGSEGV);
Guido van Rossume4485b01994-09-07 14:32:49 +0000656 PyDict_SetItemString(d, "SIGSEGV", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000657 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000658#endif
659#ifdef SIGSYS
Christian Heimes217cfd12007-12-02 14:31:20 +0000660 x = PyLong_FromLong(SIGSYS);
Guido van Rossume4485b01994-09-07 14:32:49 +0000661 PyDict_SetItemString(d, "SIGSYS", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000662 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000663#endif
664#ifdef SIGPIPE
Christian Heimes217cfd12007-12-02 14:31:20 +0000665 x = PyLong_FromLong(SIGPIPE);
Guido van Rossume4485b01994-09-07 14:32:49 +0000666 PyDict_SetItemString(d, "SIGPIPE", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000667 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000668#endif
669#ifdef SIGALRM
Christian Heimes217cfd12007-12-02 14:31:20 +0000670 x = PyLong_FromLong(SIGALRM);
Guido van Rossume4485b01994-09-07 14:32:49 +0000671 PyDict_SetItemString(d, "SIGALRM", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000672 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000673#endif
674#ifdef SIGTERM
Christian Heimes217cfd12007-12-02 14:31:20 +0000675 x = PyLong_FromLong(SIGTERM);
Guido van Rossume4485b01994-09-07 14:32:49 +0000676 PyDict_SetItemString(d, "SIGTERM", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000677 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000678#endif
679#ifdef SIGUSR1
Christian Heimes217cfd12007-12-02 14:31:20 +0000680 x = PyLong_FromLong(SIGUSR1);
Guido van Rossume4485b01994-09-07 14:32:49 +0000681 PyDict_SetItemString(d, "SIGUSR1", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000682 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000683#endif
684#ifdef SIGUSR2
Christian Heimes217cfd12007-12-02 14:31:20 +0000685 x = PyLong_FromLong(SIGUSR2);
Guido van Rossume4485b01994-09-07 14:32:49 +0000686 PyDict_SetItemString(d, "SIGUSR2", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000687 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000688#endif
689#ifdef SIGCLD
Christian Heimes217cfd12007-12-02 14:31:20 +0000690 x = PyLong_FromLong(SIGCLD);
Guido van Rossume4485b01994-09-07 14:32:49 +0000691 PyDict_SetItemString(d, "SIGCLD", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000692 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000693#endif
694#ifdef SIGCHLD
Christian Heimes217cfd12007-12-02 14:31:20 +0000695 x = PyLong_FromLong(SIGCHLD);
Guido van Rossume4485b01994-09-07 14:32:49 +0000696 PyDict_SetItemString(d, "SIGCHLD", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000697 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000698#endif
699#ifdef SIGPWR
Christian Heimes217cfd12007-12-02 14:31:20 +0000700 x = PyLong_FromLong(SIGPWR);
Guido van Rossume4485b01994-09-07 14:32:49 +0000701 PyDict_SetItemString(d, "SIGPWR", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000702 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000703#endif
704#ifdef SIGIO
Christian Heimes217cfd12007-12-02 14:31:20 +0000705 x = PyLong_FromLong(SIGIO);
Guido van Rossume4485b01994-09-07 14:32:49 +0000706 PyDict_SetItemString(d, "SIGIO", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000707 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000708#endif
709#ifdef SIGURG
Christian Heimes217cfd12007-12-02 14:31:20 +0000710 x = PyLong_FromLong(SIGURG);
Guido van Rossume4485b01994-09-07 14:32:49 +0000711 PyDict_SetItemString(d, "SIGURG", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000712 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000713#endif
714#ifdef SIGWINCH
Christian Heimes217cfd12007-12-02 14:31:20 +0000715 x = PyLong_FromLong(SIGWINCH);
Guido van Rossume4485b01994-09-07 14:32:49 +0000716 PyDict_SetItemString(d, "SIGWINCH", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000717 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000718#endif
719#ifdef SIGPOLL
Christian Heimes217cfd12007-12-02 14:31:20 +0000720 x = PyLong_FromLong(SIGPOLL);
Guido van Rossume4485b01994-09-07 14:32:49 +0000721 PyDict_SetItemString(d, "SIGPOLL", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000722 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000723#endif
724#ifdef SIGSTOP
Christian Heimes217cfd12007-12-02 14:31:20 +0000725 x = PyLong_FromLong(SIGSTOP);
Guido van Rossume4485b01994-09-07 14:32:49 +0000726 PyDict_SetItemString(d, "SIGSTOP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000727 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000728#endif
729#ifdef SIGTSTP
Christian Heimes217cfd12007-12-02 14:31:20 +0000730 x = PyLong_FromLong(SIGTSTP);
Guido van Rossume4485b01994-09-07 14:32:49 +0000731 PyDict_SetItemString(d, "SIGTSTP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000732 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000733#endif
734#ifdef SIGCONT
Christian Heimes217cfd12007-12-02 14:31:20 +0000735 x = PyLong_FromLong(SIGCONT);
Guido van Rossume4485b01994-09-07 14:32:49 +0000736 PyDict_SetItemString(d, "SIGCONT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000737 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000738#endif
739#ifdef SIGTTIN
Christian Heimes217cfd12007-12-02 14:31:20 +0000740 x = PyLong_FromLong(SIGTTIN);
Guido van Rossume4485b01994-09-07 14:32:49 +0000741 PyDict_SetItemString(d, "SIGTTIN", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000742 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000743#endif
744#ifdef SIGTTOU
Christian Heimes217cfd12007-12-02 14:31:20 +0000745 x = PyLong_FromLong(SIGTTOU);
Guido van Rossume4485b01994-09-07 14:32:49 +0000746 PyDict_SetItemString(d, "SIGTTOU", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000747 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000748#endif
749#ifdef SIGVTALRM
Christian Heimes217cfd12007-12-02 14:31:20 +0000750 x = PyLong_FromLong(SIGVTALRM);
Guido van Rossume4485b01994-09-07 14:32:49 +0000751 PyDict_SetItemString(d, "SIGVTALRM", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000752 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000753#endif
754#ifdef SIGPROF
Christian Heimes217cfd12007-12-02 14:31:20 +0000755 x = PyLong_FromLong(SIGPROF);
Guido van Rossume4485b01994-09-07 14:32:49 +0000756 PyDict_SetItemString(d, "SIGPROF", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000757 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000758#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000759#ifdef SIGXCPU
Christian Heimes217cfd12007-12-02 14:31:20 +0000760 x = PyLong_FromLong(SIGXCPU);
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000761 PyDict_SetItemString(d, "SIGXCPU", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000762 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000763#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000764#ifdef SIGXFSZ
Christian Heimes217cfd12007-12-02 14:31:20 +0000765 x = PyLong_FromLong(SIGXFSZ);
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000766 PyDict_SetItemString(d, "SIGXFSZ", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000767 Py_XDECREF(x);
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000768#endif
Anthony Baxterf37f37d2003-07-31 10:35:29 +0000769#ifdef SIGRTMIN
Christian Heimes217cfd12007-12-02 14:31:20 +0000770 x = PyLong_FromLong(SIGRTMIN);
Anthony Baxterf37f37d2003-07-31 10:35:29 +0000771 PyDict_SetItemString(d, "SIGRTMIN", x);
772 Py_XDECREF(x);
773#endif
774#ifdef SIGRTMAX
Christian Heimes217cfd12007-12-02 14:31:20 +0000775 x = PyLong_FromLong(SIGRTMAX);
Anthony Baxterf37f37d2003-07-31 10:35:29 +0000776 PyDict_SetItemString(d, "SIGRTMAX", x);
777 Py_XDECREF(x);
778#endif
Martin v. Löwis175af252002-01-12 11:43:25 +0000779#ifdef SIGINFO
Christian Heimes217cfd12007-12-02 14:31:20 +0000780 x = PyLong_FromLong(SIGINFO);
Martin v. Löwis175af252002-01-12 11:43:25 +0000781 PyDict_SetItemString(d, "SIGINFO", x);
782 Py_XDECREF(x);
783#endif
Martin v. Löwis823725e2008-03-24 13:39:54 +0000784
785#ifdef ITIMER_REAL
786 x = PyLong_FromLong(ITIMER_REAL);
787 PyDict_SetItemString(d, "ITIMER_REAL", x);
788 Py_DECREF(x);
789#endif
790#ifdef ITIMER_VIRTUAL
791 x = PyLong_FromLong(ITIMER_VIRTUAL);
792 PyDict_SetItemString(d, "ITIMER_VIRTUAL", x);
793 Py_DECREF(x);
794#endif
795#ifdef ITIMER_PROF
796 x = PyLong_FromLong(ITIMER_PROF);
797 PyDict_SetItemString(d, "ITIMER_PROF", x);
798 Py_DECREF(x);
799#endif
800
801#if defined (HAVE_SETITIMER) || defined (HAVE_GETITIMER)
802 ItimerError = PyErr_NewException("signal.ItimerError",
803 PyExc_IOError, NULL);
Neal Norwitz2f99b242008-08-24 05:48:10 +0000804 if (ItimerError != NULL)
805 PyDict_SetItemString(d, "ItimerError", ItimerError);
Martin v. Löwis823725e2008-03-24 13:39:54 +0000806#endif
807
Martin v. Löwis1a214512008-06-11 05:26:20 +0000808 if (PyErr_Occurred()) {
809 Py_DECREF(m);
810 m = NULL;
811 }
Barry Warsaw92971171997-01-03 00:14:25 +0000812
Barry Warsaw92971171997-01-03 00:14:25 +0000813 finally:
Martin v. Löwis1a214512008-06-11 05:26:20 +0000814 return m;
Guido van Rossum08c16611997-08-02 03:01:42 +0000815}
816
817static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000818finisignal(void)
Guido van Rossum08c16611997-08-02 03:01:42 +0000819{
820 int i;
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000821 PyObject *func;
Guido van Rossum08c16611997-08-02 03:01:42 +0000822
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000823 PyOS_setsig(SIGINT, old_siginthandler);
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000824 old_siginthandler = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +0000825
826 for (i = 1; i < NSIG; i++) {
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000827 func = Handlers[i].func;
Guido van Rossum08c16611997-08-02 03:01:42 +0000828 Handlers[i].tripped = 0;
Guido van Rossum08c16611997-08-02 03:01:42 +0000829 Handlers[i].func = NULL;
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000830 if (i != SIGINT && func != NULL && func != Py_None &&
831 func != DefaultHandler && func != IgnoreHandler)
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000832 PyOS_setsig(i, SIG_DFL);
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000833 Py_XDECREF(func);
Guido van Rossum08c16611997-08-02 03:01:42 +0000834 }
835
836 Py_XDECREF(IntHandler);
837 IntHandler = NULL;
838 Py_XDECREF(DefaultHandler);
839 DefaultHandler = NULL;
840 Py_XDECREF(IgnoreHandler);
841 IgnoreHandler = NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000842}
843
Barry Warsaw92971171997-01-03 00:14:25 +0000844
Barry Warsaw92971171997-01-03 00:14:25 +0000845/* Declared in pyerrors.h */
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000846int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000847PyErr_CheckSignals(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000848{
849 int i;
Guido van Rossume4485b01994-09-07 14:32:49 +0000850 PyObject *f;
Barry Warsaw92971171997-01-03 00:14:25 +0000851
852 if (!is_tripped)
Guido van Rossum6299d1e1994-05-31 12:51:13 +0000853 return 0;
Christian Heimesb76922a2007-12-11 01:06:40 +0000854
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000855#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000856 if (PyThread_get_thread_ident() != main_thread)
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000857 return 0;
858#endif
Christian Heimesb76922a2007-12-11 01:06:40 +0000859
860 /*
Benjamin Peterson8719ad52009-09-11 22:24:02 +0000861 * The is_tripped variable is meant to speed up the calls to
Christian Heimesb76922a2007-12-11 01:06:40 +0000862 * PyErr_CheckSignals (both directly or via pending calls) when no
863 * signal has arrived. This variable is set to 1 when a signal arrives
864 * and it is set to 0 here, when we know some signals arrived. This way
865 * we can run the registered handlers with no signals blocked.
866 *
867 * NOTE: with this approach we can have a situation where is_tripped is
868 * 1 but we have no more signals to handle (Handlers[i].tripped
869 * is 0 for every signal i). This won't do us any harm (except
870 * we're gonna spent some cycles for nothing). This happens when
871 * we receive a signal i after we zero is_tripped and before we
872 * check Handlers[i].tripped.
873 */
874 is_tripped = 0;
875
Guido van Rossum6297a7a2003-02-19 15:53:17 +0000876 if (!(f = (PyObject *)PyEval_GetFrame()))
Guido van Rossume4485b01994-09-07 14:32:49 +0000877 f = Py_None;
Christian Heimesb76922a2007-12-11 01:06:40 +0000878
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000879 for (i = 1; i < NSIG; i++) {
Barry Warsaw92971171997-01-03 00:14:25 +0000880 if (Handlers[i].tripped) {
881 PyObject *result = NULL;
882 PyObject *arglist = Py_BuildValue("(iO)", i, f);
883 Handlers[i].tripped = 0;
884
885 if (arglist) {
886 result = PyEval_CallObject(Handlers[i].func,
887 arglist);
Guido van Rossume4485b01994-09-07 14:32:49 +0000888 Py_DECREF(arglist);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000889 }
Barry Warsaw92971171997-01-03 00:14:25 +0000890 if (!result)
Guido van Rossumfcdd0e41997-01-21 06:13:09 +0000891 return -1;
Barry Warsaw92971171997-01-03 00:14:25 +0000892
893 Py_DECREF(result);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000894 }
895 }
Christian Heimesb76922a2007-12-11 01:06:40 +0000896
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000897 return 0;
898}
899
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000900
Barry Warsaw92971171997-01-03 00:14:25 +0000901/* Replacements for intrcheck.c functionality
902 * Declared in pyerrors.h
903 */
904void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000905PyErr_SetInterrupt(void)
Barry Warsaw92971171997-01-03 00:14:25 +0000906{
Christian Heimesb76922a2007-12-11 01:06:40 +0000907 is_tripped = 1;
Barry Warsaw92971171997-01-03 00:14:25 +0000908 Handlers[SIGINT].tripped = 1;
Thomas Wouters334fb892000-07-25 12:56:38 +0000909 Py_AddPendingCall((int (*)(void *))PyErr_CheckSignals, NULL);
Barry Warsaw92971171997-01-03 00:14:25 +0000910}
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000911
912void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000913PyOS_InitInterrupts(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000914{
Martin v. Löwis1a214512008-06-11 05:26:20 +0000915 PyObject *m = PyInit_signal();
916 if (m) {
917 _PyImport_FixupExtension(m, "signal", "signal");
918 Py_DECREF(m);
919 }
Guido van Rossum08c16611997-08-02 03:01:42 +0000920}
921
922void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000923PyOS_FiniInterrupts(void)
Guido van Rossum08c16611997-08-02 03:01:42 +0000924{
925 finisignal();
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000926}
927
928int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000929PyOS_InterruptOccurred(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000930{
Barry Warsaw92971171997-01-03 00:14:25 +0000931 if (Handlers[SIGINT].tripped) {
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000932#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000933 if (PyThread_get_thread_ident() != main_thread)
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000934 return 0;
935#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000936 Handlers[SIGINT].tripped = 0;
Guido van Rossum6299d1e1994-05-31 12:51:13 +0000937 return 1;
938 }
939 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000940}
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000941
942void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000943PyOS_AfterFork(void)
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000944{
945#ifdef WITH_THREAD
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000946 PyEval_ReInitThreads();
Guido van Rossum65d5b571998-12-21 19:32:43 +0000947 main_thread = PyThread_get_thread_ident();
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000948 main_pid = getpid();
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +0000949 _PyImport_ReInitLock();
Benjamin Petersone68df0f2008-06-13 00:26:50 +0000950 PyThread_ReInitTLS();
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000951#endif
952}