blob: 8b60e41e80793b06d6467a0622f22a25cc5591cf [file] [log] [blame]
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002/* Signal module -- many thanks to Lance Ellinghaus */
Guido van Rossum398d9fe1994-05-11 08:59:13 +00003
Guido van Rossum644a12b1997-04-09 19:24:53 +00004/* XXX Signals should be recorded per thread, now we have thread state. */
5
Guido van Rossum602099a1994-09-14 13:32:22 +00006#include "Python.h"
Guido van Rossum398d9fe1994-05-11 08:59:13 +00007#include "intrcheck.h"
8
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00009#ifdef MS_WINDOWS
Brian Curtineb24d742010-04-12 17:16:38 +000010#include <Windows.h>
Benjamin Peterson2614cda2010-03-21 22:36:19 +000011#ifdef HAVE_PROCESS_H
Guido van Rossum644a12b1997-04-09 19:24:53 +000012#include <process.h>
13#endif
Benjamin Peterson2614cda2010-03-21 22:36:19 +000014#endif
Guido van Rossum644a12b1997-04-09 19:24:53 +000015
Benjamin Peterson2614cda2010-03-21 22:36:19 +000016#ifdef HAVE_SIGNAL_H
Guido van Rossum398d9fe1994-05-11 08:59:13 +000017#include <signal.h>
Benjamin Peterson2614cda2010-03-21 22:36:19 +000018#endif
19#ifdef HAVE_SYS_STAT_H
Christian Heimes5fb7c2a2007-12-24 08:52:31 +000020#include <sys/stat.h>
Benjamin Peterson2614cda2010-03-21 22:36:19 +000021#endif
Martin v. Löwis3bf3cc02008-03-24 14:05:07 +000022#ifdef HAVE_SYS_TIME_H
Martin v. Löwis823725e2008-03-24 13:39:54 +000023#include <sys/time.h>
Martin v. Löwis3bf3cc02008-03-24 14:05:07 +000024#endif
Christian Heimes5fb7c2a2007-12-24 08:52:31 +000025
Guido van Rossumbb4ba121994-06-23 11:25:45 +000026#ifndef SIG_ERR
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +000027#define SIG_ERR ((PyOS_sighandler_t)(-1))
Guido van Rossumbb4ba121994-06-23 11:25:45 +000028#endif
29
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000030#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000031#define NSIG 12
32#include <process.h>
33#endif
34
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000035#ifndef NSIG
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000036# if defined(_NSIG)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000037# define NSIG _NSIG /* For BSD/SysV */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000038# elif defined(_SIGMAX)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000039# define NSIG (_SIGMAX + 1) /* For QNX */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000040# elif defined(SIGMAX)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000041# define NSIG (SIGMAX + 1) /* For djgpp */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000042# else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000043# define NSIG 64 /* Use a reasonable default value */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000044# endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000045#endif
46
47
Guido van Rossumbb4ba121994-06-23 11:25:45 +000048/*
49 NOTES ON THE INTERACTION BETWEEN SIGNALS AND THREADS
50
51 When threads are supported, we want the following semantics:
52
53 - only the main thread can set a signal handler
54 - any thread can get a signal handler
55 - signals are only delivered to the main thread
56
57 I.e. we don't support "synchronous signals" like SIGFPE (catching
58 this doesn't make much sense in Python anyway) nor do we support
59 signals as a means of inter-thread communication, since not all
60 thread implementations support that (at least our thread library
61 doesn't).
62
63 We still have the problem that in some implementations signals
64 generated by the keyboard (e.g. SIGINT) are delivered to all
65 threads (e.g. SGI), while in others (e.g. Solaris) such signals are
66 delivered to one random thread (an intermediate possibility would
Guido van Rossuma3c04b01995-01-12 11:29:01 +000067 be to deliver it to the main thread -- POSIX?). For now, we have
Guido van Rossumbb4ba121994-06-23 11:25:45 +000068 a working implementation that works in all three cases -- the
69 handler ignores signals if getpid() isn't the same as in the main
70 thread. XXX This is a hack.
71
Guido van Rossum9e8181b2000-09-19 00:46:46 +000072 GNU pth is a user-space threading library, and as such, all threads
73 run within the same process. In this case, if the currently running
74 thread is not the main_thread, send the signal to the main_thread.
Guido van Rossumbb4ba121994-06-23 11:25:45 +000075*/
76
77#ifdef WITH_THREAD
Guido van Rossum295b8e51997-06-06 21:16:41 +000078#include <sys/types.h> /* For pid_t */
Guido van Rossum49b56061998-10-01 20:42:43 +000079#include "pythread.h"
Guido van Rossumbb4ba121994-06-23 11:25:45 +000080static long main_thread;
81static pid_t main_pid;
82#endif
83
Barry Warsaw92971171997-01-03 00:14:25 +000084static struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000085 int tripped;
86 PyObject *func;
Barry Warsaw92971171997-01-03 00:14:25 +000087} Handlers[NSIG];
Guido van Rossum398d9fe1994-05-11 08:59:13 +000088
Christian Heimes5fb7c2a2007-12-24 08:52:31 +000089static sig_atomic_t wakeup_fd = -1;
90
Christian Heimesb76922a2007-12-11 01:06:40 +000091/* Speed up sigcheck() when none tripped */
92static volatile sig_atomic_t is_tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +000093
Barry Warsaw92971171997-01-03 00:14:25 +000094static PyObject *DefaultHandler;
95static PyObject *IgnoreHandler;
96static PyObject *IntHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +000097
Martin v. Löwisf58de1b2001-03-06 12:13:56 +000098/* On Solaris 8, gcc will produce a warning that the function
99 declaration is not a prototype. This is caused by the definition of
100 SIG_DFL as (void (*)())0; the correct declaration would have been
101 (void (*)(int))0. */
102
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000103static PyOS_sighandler_t old_siginthandler = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +0000104
Martin v. Löwis823725e2008-03-24 13:39:54 +0000105#ifdef HAVE_GETITIMER
106static PyObject *ItimerError;
107
108/* auxiliary functions for setitimer/getitimer */
109static void
110timeval_from_double(double d, struct timeval *tv)
111{
112 tv->tv_sec = floor(d);
113 tv->tv_usec = fmod(d, 1.0) * 1000000.0;
114}
115
Christian Heimes1a8501c2008-10-02 19:56:01 +0000116Py_LOCAL_INLINE(double)
Martin v. Löwis823725e2008-03-24 13:39:54 +0000117double_from_timeval(struct timeval *tv)
118{
119 return tv->tv_sec + (double)(tv->tv_usec / 1000000.0);
120}
121
122static PyObject *
123itimer_retval(struct itimerval *iv)
124{
125 PyObject *r, *v;
126
127 r = PyTuple_New(2);
128 if (r == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000129 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000130
131 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_value)))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000132 Py_DECREF(r);
133 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000134 }
135
136 PyTuple_SET_ITEM(r, 0, v);
137
138 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_interval)))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000139 Py_DECREF(r);
140 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000141 }
142
143 PyTuple_SET_ITEM(r, 1, v);
144
145 return r;
146}
147#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000148
Guido van Rossume4485b01994-09-07 14:32:49 +0000149static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000150signal_default_int_handler(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000151{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000152 PyErr_SetNone(PyExc_KeyboardInterrupt);
153 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000154}
155
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000156PyDoc_STRVAR(default_int_handler_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000157"default_int_handler(...)\n\
158\n\
Michael W. Hudson24ec2112004-06-17 15:55:53 +0000159The default handler for SIGINT installed by Python.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000160It raises KeyboardInterrupt.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000161
Thomas Wouters0796b002000-07-22 23:49:30 +0000162
163static int
164checksignals_witharg(void * unused)
165{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000166 return PyErr_CheckSignals();
Thomas Wouters0796b002000-07-22 23:49:30 +0000167}
168
Tim Peters4f1b2082000-07-23 21:18:09 +0000169static void
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000170signal_handler(int sig_num)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000171{
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000172#ifdef WITH_THREAD
Guido van Rossum9e8181b2000-09-19 00:46:46 +0000173#ifdef WITH_PTH
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000174 if (PyThread_get_thread_ident() != main_thread) {
175 pth_raise(*(pth_t *) main_thread, sig_num);
176 return;
177 }
Guido van Rossum9e8181b2000-09-19 00:46:46 +0000178#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000179 /* See NOTES section above */
180 if (getpid() == main_pid) {
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000181#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000182 Handlers[sig_num].tripped = 1;
183 /* Set is_tripped after setting .tripped, as it gets
184 cleared in PyErr_CheckSignals() before .tripped. */
185 is_tripped = 1;
186 Py_AddPendingCall(checksignals_witharg, NULL);
187 if (wakeup_fd != -1)
188 write(wakeup_fd, "\0", 1);
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000189#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000190 }
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000191#endif
Guido van Rossum602099a1994-09-14 13:32:22 +0000192#ifdef SIGCHLD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000193 if (sig_num == SIGCHLD) {
194 /* To avoid infinite recursion, this signal remains
195 reset until explicit re-instated.
196 Don't clear the 'func' field as it is our pointer
197 to the Python handler... */
198 return;
199 }
Guido van Rossum602099a1994-09-14 13:32:22 +0000200#endif
Jean-Paul Calderone6f137ca2010-05-09 03:18:57 +0000201#ifndef HAVE_SIGACTION
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000202 /* If the handler was not set up with sigaction, reinstall it. See
203 * Python/pythonrun.c for the implementation of PyOS_setsig which
204 * makes this true. See also issue8354. */
205 PyOS_setsig(sig_num, signal_handler);
Jean-Paul Calderone6f137ca2010-05-09 03:18:57 +0000206#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000207}
Guido van Rossume4485b01994-09-07 14:32:49 +0000208
Guido van Rossum06d511d1995-03-10 15:13:48 +0000209
Guido van Rossum1171ee61997-08-22 20:42:00 +0000210#ifdef HAVE_ALARM
Guido van Rossume4485b01994-09-07 14:32:49 +0000211static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000212signal_alarm(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000213{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000214 int t;
215 if (!PyArg_ParseTuple(args, "i:alarm", &t))
216 return NULL;
217 /* alarm() returns the number of seconds remaining */
218 return PyLong_FromLong((long)alarm(t));
Guido van Rossumaa0f4c71994-08-23 13:49:37 +0000219}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000220
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000221PyDoc_STRVAR(alarm_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000222"alarm(seconds)\n\
223\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000224Arrange for SIGALRM to arrive after the given number of seconds.");
Guido van Rossum06d511d1995-03-10 15:13:48 +0000225#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000226
Guido van Rossum1171ee61997-08-22 20:42:00 +0000227#ifdef HAVE_PAUSE
Guido van Rossuma597dde1995-01-10 20:56:29 +0000228static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000229signal_pause(PyObject *self)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000230{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000231 Py_BEGIN_ALLOW_THREADS
232 (void)pause();
233 Py_END_ALLOW_THREADS
234 /* make sure that any exceptions that got raised are propagated
235 * back into Python
236 */
237 if (PyErr_CheckSignals())
238 return NULL;
Barry Warsaw92971171997-01-03 00:14:25 +0000239
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000240 Py_INCREF(Py_None);
241 return Py_None;
Guido van Rossume4485b01994-09-07 14:32:49 +0000242}
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000243PyDoc_STRVAR(pause_doc,
Barry Warsaw1ee36ff1998-07-21 22:41:18 +0000244"pause()\n\
245\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000246Wait until a signal arrives.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000247
Guido van Rossum06d511d1995-03-10 15:13:48 +0000248#endif
Guido van Rossume4485b01994-09-07 14:32:49 +0000249
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000250
Guido van Rossume4485b01994-09-07 14:32:49 +0000251static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000252signal_signal(PyObject *self, PyObject *args)
Guido van Rossume4485b01994-09-07 14:32:49 +0000253{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000254 PyObject *obj;
255 int sig_num;
256 PyObject *old_handler;
257 void (*func)(int);
258 if (!PyArg_ParseTuple(args, "iO:signal", &sig_num, &obj))
259 return NULL;
Brian Curtinef9efbd2010-08-06 19:27:32 +0000260#ifdef MS_WINDOWS
261 /* Validate that sig_num is one of the allowable signals */
Brian Curtinc734b312010-09-06 16:04:10 +0000262 switch (sig_num) {
263 case SIGABRT: break;
264 case SIGFPE: break;
265 case SIGILL: break;
266 case SIGINT: break;
267 case SIGSEGV: break;
268 case SIGTERM: break;
269 default:
270 PyErr_SetString(PyExc_ValueError, "invalid signal value");
271 return NULL;
Brian Curtinef9efbd2010-08-06 19:27:32 +0000272 }
273#endif
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000274#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000275 if (PyThread_get_thread_ident() != main_thread) {
276 PyErr_SetString(PyExc_ValueError,
277 "signal only works in main thread");
278 return NULL;
279 }
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000280#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000281 if (sig_num < 1 || sig_num >= NSIG) {
282 PyErr_SetString(PyExc_ValueError,
283 "signal number out of range");
284 return NULL;
285 }
286 if (obj == IgnoreHandler)
287 func = SIG_IGN;
288 else if (obj == DefaultHandler)
289 func = SIG_DFL;
290 else if (!PyCallable_Check(obj)) {
291 PyErr_SetString(PyExc_TypeError,
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000292"signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000293 return NULL;
294 }
295 else
296 func = signal_handler;
297 if (PyOS_setsig(sig_num, func) == SIG_ERR) {
298 PyErr_SetFromErrno(PyExc_RuntimeError);
299 return NULL;
300 }
301 old_handler = Handlers[sig_num].func;
302 Handlers[sig_num].tripped = 0;
303 Py_INCREF(obj);
304 Handlers[sig_num].func = obj;
305 return old_handler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000306}
307
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000308PyDoc_STRVAR(signal_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000309"signal(sig, action) -> action\n\
310\n\
311Set the action for the given signal. The action can be SIG_DFL,\n\
312SIG_IGN, or a callable Python object. The previous action is\n\
313returned. See getsignal() for possible return values.\n\
314\n\
315*** IMPORTANT NOTICE ***\n\
316A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000317the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000318
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000319
Guido van Rossume4485b01994-09-07 14:32:49 +0000320static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000321signal_getsignal(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000322{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000323 int sig_num;
324 PyObject *old_handler;
325 if (!PyArg_ParseTuple(args, "i:getsignal", &sig_num))
326 return NULL;
327 if (sig_num < 1 || sig_num >= NSIG) {
328 PyErr_SetString(PyExc_ValueError,
329 "signal number out of range");
330 return NULL;
331 }
332 old_handler = Handlers[sig_num].func;
333 Py_INCREF(old_handler);
334 return old_handler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000335}
336
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000337PyDoc_STRVAR(getsignal_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000338"getsignal(sig) -> action\n\
339\n\
340Return the current action for the given signal. The return value can be:\n\
341SIG_IGN -- if the signal is being ignored\n\
342SIG_DFL -- if the default action for the signal is in effect\n\
343None -- if an unknown handler is in effect\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000344anything else -- the callable Python object used as a handler");
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000345
Christian Heimes8640e742008-02-23 16:23:06 +0000346#ifdef HAVE_SIGINTERRUPT
347PyDoc_STRVAR(siginterrupt_doc,
348"siginterrupt(sig, flag) -> None\n\
349change system call restart behaviour: if flag is False, system calls\n\
350will be restarted when interrupted by signal sig, else system calls\n\
351will be interrupted.");
352
353static PyObject *
354signal_siginterrupt(PyObject *self, PyObject *args)
355{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000356 int sig_num;
357 int flag;
Christian Heimes8640e742008-02-23 16:23:06 +0000358
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000359 if (!PyArg_ParseTuple(args, "ii:siginterrupt", &sig_num, &flag))
360 return NULL;
361 if (sig_num < 1 || sig_num >= NSIG) {
362 PyErr_SetString(PyExc_ValueError,
363 "signal number out of range");
364 return NULL;
365 }
366 if (siginterrupt(sig_num, flag)<0) {
367 PyErr_SetFromErrno(PyExc_RuntimeError);
368 return NULL;
369 }
Christian Heimes8640e742008-02-23 16:23:06 +0000370
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000371 Py_INCREF(Py_None);
372 return Py_None;
Christian Heimes8640e742008-02-23 16:23:06 +0000373}
374
375#endif
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000376
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000377static PyObject *
378signal_set_wakeup_fd(PyObject *self, PyObject *args)
379{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000380 struct stat buf;
381 int fd, old_fd;
382 if (!PyArg_ParseTuple(args, "i:set_wakeup_fd", &fd))
383 return NULL;
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000384#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000385 if (PyThread_get_thread_ident() != main_thread) {
386 PyErr_SetString(PyExc_ValueError,
387 "set_wakeup_fd only works in main thread");
388 return NULL;
389 }
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000390#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000391 if (fd != -1 && fstat(fd, &buf) != 0) {
392 PyErr_SetString(PyExc_ValueError, "invalid fd");
393 return NULL;
394 }
395 old_fd = wakeup_fd;
396 wakeup_fd = fd;
397 return PyLong_FromLong(old_fd);
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000398}
399
400PyDoc_STRVAR(set_wakeup_fd_doc,
401"set_wakeup_fd(fd) -> fd\n\
402\n\
403Sets the fd to be written to (with '\\0') when a signal\n\
404comes in. A library can use this to wakeup select or poll.\n\
405The previous fd is returned.\n\
406\n\
407The fd must be non-blocking.");
408
409/* C API for the same, without all the error checking */
410int
411PySignal_SetWakeupFd(int fd)
412{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000413 int old_fd = wakeup_fd;
414 if (fd < 0)
415 fd = -1;
416 wakeup_fd = fd;
417 return old_fd;
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000418}
419
420
Martin v. Löwis823725e2008-03-24 13:39:54 +0000421#ifdef HAVE_SETITIMER
422static PyObject *
423signal_setitimer(PyObject *self, PyObject *args)
424{
425 double first;
426 double interval = 0;
427 int which;
428 struct itimerval new, old;
429
430 if(!PyArg_ParseTuple(args, "id|d:setitimer", &which, &first, &interval))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000431 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000432
433 timeval_from_double(first, &new.it_value);
434 timeval_from_double(interval, &new.it_interval);
435 /* Let OS check "which" value */
436 if (setitimer(which, &new, &old) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000437 PyErr_SetFromErrno(ItimerError);
438 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000439 }
440
441 return itimer_retval(&old);
442}
443
444PyDoc_STRVAR(setitimer_doc,
445"setitimer(which, seconds[, interval])\n\
446\n\
447Sets given itimer (one of ITIMER_REAL, ITIMER_VIRTUAL\n\
448or ITIMER_PROF) to fire after value seconds and after\n\
449that every interval seconds.\n\
450The itimer can be cleared by setting seconds to zero.\n\
451\n\
452Returns old values as a tuple: (delay, interval).");
453#endif
454
455
456#ifdef HAVE_GETITIMER
457static PyObject *
458signal_getitimer(PyObject *self, PyObject *args)
459{
460 int which;
461 struct itimerval old;
462
463 if (!PyArg_ParseTuple(args, "i:getitimer", &which))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000464 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000465
466 if (getitimer(which, &old) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000467 PyErr_SetFromErrno(ItimerError);
468 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000469 }
470
471 return itimer_retval(&old);
472}
473
474PyDoc_STRVAR(getitimer_doc,
475"getitimer(which)\n\
476\n\
477Returns current value of given itimer.");
478#endif
479
480
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000481/* List of functions defined in the module */
Barry Warsaw92971171997-01-03 00:14:25 +0000482static PyMethodDef signal_methods[] = {
Guido van Rossum1171ee61997-08-22 20:42:00 +0000483#ifdef HAVE_ALARM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000484 {"alarm", signal_alarm, METH_VARARGS, alarm_doc},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000485#endif
Martin v. Löwis823725e2008-03-24 13:39:54 +0000486#ifdef HAVE_SETITIMER
487 {"setitimer", signal_setitimer, METH_VARARGS, setitimer_doc},
488#endif
489#ifdef HAVE_GETITIMER
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000490 {"getitimer", signal_getitimer, METH_VARARGS, getitimer_doc},
Martin v. Löwis823725e2008-03-24 13:39:54 +0000491#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000492 {"signal", signal_signal, METH_VARARGS, signal_doc},
493 {"getsignal", signal_getsignal, METH_VARARGS, getsignal_doc},
494 {"set_wakeup_fd", signal_set_wakeup_fd, METH_VARARGS, set_wakeup_fd_doc},
Christian Heimes8640e742008-02-23 16:23:06 +0000495#ifdef HAVE_SIGINTERRUPT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000496 {"siginterrupt", signal_siginterrupt, METH_VARARGS, siginterrupt_doc},
Christian Heimes8640e742008-02-23 16:23:06 +0000497#endif
Guido van Rossum1171ee61997-08-22 20:42:00 +0000498#ifdef HAVE_PAUSE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 {"pause", (PyCFunction)signal_pause,
500 METH_NOARGS,pause_doc},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000501#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000502 {"default_int_handler", signal_default_int_handler,
503 METH_VARARGS, default_int_handler_doc},
504 {NULL, NULL} /* sentinel */
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000505};
506
Barry Warsaw92971171997-01-03 00:14:25 +0000507
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000508PyDoc_STRVAR(module_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000509"This module provides mechanisms to use signal handlers in Python.\n\
510\n\
511Functions:\n\
512\n\
513alarm() -- cause SIGALRM after a specified time [Unix only]\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +0000514setitimer() -- cause a signal (described below) after a specified\n\
515 float time and the timer may restart then [Unix only]\n\
516getitimer() -- get current value of timer [Unix only]\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000517signal() -- set the action for a given signal\n\
518getsignal() -- get the signal action for a given signal\n\
519pause() -- wait until a signal arrives [Unix only]\n\
520default_int_handler() -- default SIGINT handler\n\
521\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +0000522signal constants:\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000523SIG_DFL -- used to refer to the system default handler\n\
524SIG_IGN -- used to ignore the signal\n\
525NSIG -- number of defined signals\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000526SIGINT, SIGTERM, etc. -- signal numbers\n\
527\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +0000528itimer constants:\n\
529ITIMER_REAL -- decrements in real time, and delivers SIGALRM upon\n\
530 expiration\n\
531ITIMER_VIRTUAL -- decrements only when the process is executing,\n\
532 and delivers SIGVTALRM upon expiration\n\
533ITIMER_PROF -- decrements both when the process is executing and\n\
534 when the system is executing on behalf of the process.\n\
535 Coupled with ITIMER_VIRTUAL, this timer is usually\n\
536 used to profile the time spent by the application\n\
537 in user and kernel space. SIGPROF is delivered upon\n\
538 expiration.\n\
539\n\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000540*** IMPORTANT NOTICE ***\n\
541A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000542the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000543
Martin v. Löwis1a214512008-06-11 05:26:20 +0000544static struct PyModuleDef signalmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000545 PyModuleDef_HEAD_INIT,
546 "signal",
547 module_doc,
548 -1,
549 signal_methods,
550 NULL,
551 NULL,
552 NULL,
553 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000554};
555
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000556PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000557PyInit_signal(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000558{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000559 PyObject *m, *d, *x;
560 int i;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000561
562#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000563 main_thread = PyThread_get_thread_ident();
564 main_pid = getpid();
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000565#endif
566
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000567 /* Create the module and add the functions */
568 m = PyModule_Create(&signalmodule);
569 if (m == NULL)
570 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000571
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000572 /* Add some symbolic constants to the module */
573 d = PyModule_GetDict(m);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000574
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000575 x = DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL);
576 if (!x || PyDict_SetItemString(d, "SIG_DFL", x) < 0)
577 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000578
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000579 x = IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN);
580 if (!x || PyDict_SetItemString(d, "SIG_IGN", x) < 0)
581 goto finally;
Barry Warsaw92971171997-01-03 00:14:25 +0000582
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000583 x = PyLong_FromLong((long)NSIG);
584 if (!x || PyDict_SetItemString(d, "NSIG", x) < 0)
585 goto finally;
586 Py_DECREF(x);
Barry Warsaw92971171997-01-03 00:14:25 +0000587
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000588 x = IntHandler = PyDict_GetItemString(d, "default_int_handler");
589 if (!x)
590 goto finally;
591 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +0000592
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000593 Handlers[0].tripped = 0;
594 for (i = 1; i < NSIG; i++) {
595 void (*t)(int);
596 t = PyOS_getsig(i);
597 Handlers[i].tripped = 0;
598 if (t == SIG_DFL)
599 Handlers[i].func = DefaultHandler;
600 else if (t == SIG_IGN)
601 Handlers[i].func = IgnoreHandler;
602 else
603 Handlers[i].func = Py_None; /* None of our business */
604 Py_INCREF(Handlers[i].func);
605 }
606 if (Handlers[SIGINT].func == DefaultHandler) {
607 /* Install default int handler */
608 Py_INCREF(IntHandler);
609 Py_DECREF(Handlers[SIGINT].func);
610 Handlers[SIGINT].func = IntHandler;
611 old_siginthandler = PyOS_setsig(SIGINT, signal_handler);
612 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000613
614#ifdef SIGHUP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000615 x = PyLong_FromLong(SIGHUP);
616 PyDict_SetItemString(d, "SIGHUP", x);
617 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000618#endif
619#ifdef SIGINT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000620 x = PyLong_FromLong(SIGINT);
621 PyDict_SetItemString(d, "SIGINT", x);
622 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000623#endif
Tim Peters1ce3cf72001-10-01 17:58:40 +0000624#ifdef SIGBREAK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000625 x = PyLong_FromLong(SIGBREAK);
626 PyDict_SetItemString(d, "SIGBREAK", x);
627 Py_XDECREF(x);
Tim Peters1ce3cf72001-10-01 17:58:40 +0000628#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000629#ifdef SIGQUIT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000630 x = PyLong_FromLong(SIGQUIT);
631 PyDict_SetItemString(d, "SIGQUIT", x);
632 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000633#endif
634#ifdef SIGILL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000635 x = PyLong_FromLong(SIGILL);
636 PyDict_SetItemString(d, "SIGILL", x);
637 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000638#endif
639#ifdef SIGTRAP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000640 x = PyLong_FromLong(SIGTRAP);
641 PyDict_SetItemString(d, "SIGTRAP", x);
642 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000643#endif
644#ifdef SIGIOT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000645 x = PyLong_FromLong(SIGIOT);
646 PyDict_SetItemString(d, "SIGIOT", x);
647 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000648#endif
649#ifdef SIGABRT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000650 x = PyLong_FromLong(SIGABRT);
651 PyDict_SetItemString(d, "SIGABRT", x);
652 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000653#endif
654#ifdef SIGEMT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000655 x = PyLong_FromLong(SIGEMT);
656 PyDict_SetItemString(d, "SIGEMT", x);
657 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000658#endif
659#ifdef SIGFPE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000660 x = PyLong_FromLong(SIGFPE);
661 PyDict_SetItemString(d, "SIGFPE", x);
662 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000663#endif
664#ifdef SIGKILL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000665 x = PyLong_FromLong(SIGKILL);
666 PyDict_SetItemString(d, "SIGKILL", x);
667 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000668#endif
669#ifdef SIGBUS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000670 x = PyLong_FromLong(SIGBUS);
671 PyDict_SetItemString(d, "SIGBUS", x);
672 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000673#endif
674#ifdef SIGSEGV
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000675 x = PyLong_FromLong(SIGSEGV);
676 PyDict_SetItemString(d, "SIGSEGV", x);
677 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000678#endif
679#ifdef SIGSYS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000680 x = PyLong_FromLong(SIGSYS);
681 PyDict_SetItemString(d, "SIGSYS", x);
682 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000683#endif
684#ifdef SIGPIPE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000685 x = PyLong_FromLong(SIGPIPE);
686 PyDict_SetItemString(d, "SIGPIPE", x);
687 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000688#endif
689#ifdef SIGALRM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000690 x = PyLong_FromLong(SIGALRM);
691 PyDict_SetItemString(d, "SIGALRM", x);
692 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000693#endif
694#ifdef SIGTERM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000695 x = PyLong_FromLong(SIGTERM);
696 PyDict_SetItemString(d, "SIGTERM", x);
697 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000698#endif
699#ifdef SIGUSR1
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000700 x = PyLong_FromLong(SIGUSR1);
701 PyDict_SetItemString(d, "SIGUSR1", x);
702 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000703#endif
704#ifdef SIGUSR2
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000705 x = PyLong_FromLong(SIGUSR2);
706 PyDict_SetItemString(d, "SIGUSR2", x);
707 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000708#endif
709#ifdef SIGCLD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000710 x = PyLong_FromLong(SIGCLD);
711 PyDict_SetItemString(d, "SIGCLD", x);
712 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000713#endif
714#ifdef SIGCHLD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000715 x = PyLong_FromLong(SIGCHLD);
716 PyDict_SetItemString(d, "SIGCHLD", x);
717 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000718#endif
719#ifdef SIGPWR
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000720 x = PyLong_FromLong(SIGPWR);
721 PyDict_SetItemString(d, "SIGPWR", x);
722 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000723#endif
724#ifdef SIGIO
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000725 x = PyLong_FromLong(SIGIO);
726 PyDict_SetItemString(d, "SIGIO", x);
727 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000728#endif
729#ifdef SIGURG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000730 x = PyLong_FromLong(SIGURG);
731 PyDict_SetItemString(d, "SIGURG", x);
732 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000733#endif
734#ifdef SIGWINCH
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000735 x = PyLong_FromLong(SIGWINCH);
736 PyDict_SetItemString(d, "SIGWINCH", x);
737 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000738#endif
739#ifdef SIGPOLL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000740 x = PyLong_FromLong(SIGPOLL);
741 PyDict_SetItemString(d, "SIGPOLL", x);
742 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000743#endif
744#ifdef SIGSTOP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000745 x = PyLong_FromLong(SIGSTOP);
746 PyDict_SetItemString(d, "SIGSTOP", x);
747 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000748#endif
749#ifdef SIGTSTP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000750 x = PyLong_FromLong(SIGTSTP);
751 PyDict_SetItemString(d, "SIGTSTP", x);
752 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000753#endif
754#ifdef SIGCONT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000755 x = PyLong_FromLong(SIGCONT);
756 PyDict_SetItemString(d, "SIGCONT", x);
757 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000758#endif
759#ifdef SIGTTIN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000760 x = PyLong_FromLong(SIGTTIN);
761 PyDict_SetItemString(d, "SIGTTIN", x);
762 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000763#endif
764#ifdef SIGTTOU
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000765 x = PyLong_FromLong(SIGTTOU);
766 PyDict_SetItemString(d, "SIGTTOU", x);
767 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000768#endif
769#ifdef SIGVTALRM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000770 x = PyLong_FromLong(SIGVTALRM);
771 PyDict_SetItemString(d, "SIGVTALRM", x);
772 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000773#endif
774#ifdef SIGPROF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000775 x = PyLong_FromLong(SIGPROF);
776 PyDict_SetItemString(d, "SIGPROF", x);
777 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000778#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000779#ifdef SIGXCPU
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000780 x = PyLong_FromLong(SIGXCPU);
781 PyDict_SetItemString(d, "SIGXCPU", x);
782 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000783#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000784#ifdef SIGXFSZ
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000785 x = PyLong_FromLong(SIGXFSZ);
786 PyDict_SetItemString(d, "SIGXFSZ", x);
787 Py_XDECREF(x);
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000788#endif
Anthony Baxterf37f37d2003-07-31 10:35:29 +0000789#ifdef SIGRTMIN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000790 x = PyLong_FromLong(SIGRTMIN);
791 PyDict_SetItemString(d, "SIGRTMIN", x);
792 Py_XDECREF(x);
Anthony Baxterf37f37d2003-07-31 10:35:29 +0000793#endif
794#ifdef SIGRTMAX
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000795 x = PyLong_FromLong(SIGRTMAX);
796 PyDict_SetItemString(d, "SIGRTMAX", x);
797 Py_XDECREF(x);
Anthony Baxterf37f37d2003-07-31 10:35:29 +0000798#endif
Martin v. Löwis175af252002-01-12 11:43:25 +0000799#ifdef SIGINFO
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000800 x = PyLong_FromLong(SIGINFO);
801 PyDict_SetItemString(d, "SIGINFO", x);
802 Py_XDECREF(x);
Martin v. Löwis175af252002-01-12 11:43:25 +0000803#endif
Martin v. Löwis823725e2008-03-24 13:39:54 +0000804
805#ifdef ITIMER_REAL
806 x = PyLong_FromLong(ITIMER_REAL);
807 PyDict_SetItemString(d, "ITIMER_REAL", x);
808 Py_DECREF(x);
809#endif
810#ifdef ITIMER_VIRTUAL
811 x = PyLong_FromLong(ITIMER_VIRTUAL);
812 PyDict_SetItemString(d, "ITIMER_VIRTUAL", x);
813 Py_DECREF(x);
814#endif
815#ifdef ITIMER_PROF
816 x = PyLong_FromLong(ITIMER_PROF);
817 PyDict_SetItemString(d, "ITIMER_PROF", x);
818 Py_DECREF(x);
819#endif
820
821#if defined (HAVE_SETITIMER) || defined (HAVE_GETITIMER)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000822 ItimerError = PyErr_NewException("signal.ItimerError",
823 PyExc_IOError, NULL);
Neal Norwitz2f99b242008-08-24 05:48:10 +0000824 if (ItimerError != NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000825 PyDict_SetItemString(d, "ItimerError", ItimerError);
Martin v. Löwis823725e2008-03-24 13:39:54 +0000826#endif
827
Brian Curtineb24d742010-04-12 17:16:38 +0000828#ifdef CTRL_C_EVENT
829 x = PyLong_FromLong(CTRL_C_EVENT);
830 PyDict_SetItemString(d, "CTRL_C_EVENT", x);
831 Py_DECREF(x);
832#endif
833
834#ifdef CTRL_BREAK_EVENT
835 x = PyLong_FromLong(CTRL_BREAK_EVENT);
836 PyDict_SetItemString(d, "CTRL_BREAK_EVENT", x);
837 Py_DECREF(x);
838#endif
839
Martin v. Löwis1a214512008-06-11 05:26:20 +0000840 if (PyErr_Occurred()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000841 Py_DECREF(m);
842 m = NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000843 }
Barry Warsaw92971171997-01-03 00:14:25 +0000844
Barry Warsaw92971171997-01-03 00:14:25 +0000845 finally:
Martin v. Löwis1a214512008-06-11 05:26:20 +0000846 return m;
Guido van Rossum08c16611997-08-02 03:01:42 +0000847}
848
849static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000850finisignal(void)
Guido van Rossum08c16611997-08-02 03:01:42 +0000851{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000852 int i;
853 PyObject *func;
Guido van Rossum08c16611997-08-02 03:01:42 +0000854
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000855 PyOS_setsig(SIGINT, old_siginthandler);
856 old_siginthandler = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +0000857
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000858 for (i = 1; i < NSIG; i++) {
859 func = Handlers[i].func;
860 Handlers[i].tripped = 0;
861 Handlers[i].func = NULL;
862 if (i != SIGINT && func != NULL && func != Py_None &&
863 func != DefaultHandler && func != IgnoreHandler)
864 PyOS_setsig(i, SIG_DFL);
865 Py_XDECREF(func);
866 }
Guido van Rossum08c16611997-08-02 03:01:42 +0000867
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000868 Py_XDECREF(IntHandler);
869 IntHandler = NULL;
870 Py_XDECREF(DefaultHandler);
871 DefaultHandler = NULL;
872 Py_XDECREF(IgnoreHandler);
873 IgnoreHandler = NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000874}
875
Barry Warsaw92971171997-01-03 00:14:25 +0000876
Barry Warsaw92971171997-01-03 00:14:25 +0000877/* Declared in pyerrors.h */
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000878int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000879PyErr_CheckSignals(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000880{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000881 int i;
882 PyObject *f;
Barry Warsaw92971171997-01-03 00:14:25 +0000883
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000884 if (!is_tripped)
885 return 0;
Christian Heimesb76922a2007-12-11 01:06:40 +0000886
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000887#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000888 if (PyThread_get_thread_ident() != main_thread)
889 return 0;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000890#endif
Christian Heimesb76922a2007-12-11 01:06:40 +0000891
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000892 /*
893 * The is_tripped variable is meant to speed up the calls to
894 * PyErr_CheckSignals (both directly or via pending calls) when no
895 * signal has arrived. This variable is set to 1 when a signal arrives
896 * and it is set to 0 here, when we know some signals arrived. This way
897 * we can run the registered handlers with no signals blocked.
898 *
899 * NOTE: with this approach we can have a situation where is_tripped is
900 * 1 but we have no more signals to handle (Handlers[i].tripped
901 * is 0 for every signal i). This won't do us any harm (except
902 * we're gonna spent some cycles for nothing). This happens when
903 * we receive a signal i after we zero is_tripped and before we
904 * check Handlers[i].tripped.
905 */
906 is_tripped = 0;
Christian Heimesb76922a2007-12-11 01:06:40 +0000907
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000908 if (!(f = (PyObject *)PyEval_GetFrame()))
909 f = Py_None;
Christian Heimesb76922a2007-12-11 01:06:40 +0000910
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000911 for (i = 1; i < NSIG; i++) {
912 if (Handlers[i].tripped) {
913 PyObject *result = NULL;
914 PyObject *arglist = Py_BuildValue("(iO)", i, f);
915 Handlers[i].tripped = 0;
Barry Warsaw92971171997-01-03 00:14:25 +0000916
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000917 if (arglist) {
918 result = PyEval_CallObject(Handlers[i].func,
919 arglist);
920 Py_DECREF(arglist);
921 }
922 if (!result)
923 return -1;
Barry Warsaw92971171997-01-03 00:14:25 +0000924
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000925 Py_DECREF(result);
926 }
927 }
Christian Heimesb76922a2007-12-11 01:06:40 +0000928
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000929 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000930}
931
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000932
Barry Warsaw92971171997-01-03 00:14:25 +0000933/* Replacements for intrcheck.c functionality
934 * Declared in pyerrors.h
935 */
936void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000937PyErr_SetInterrupt(void)
Barry Warsaw92971171997-01-03 00:14:25 +0000938{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000939 is_tripped = 1;
940 Handlers[SIGINT].tripped = 1;
941 Py_AddPendingCall((int (*)(void *))PyErr_CheckSignals, NULL);
Barry Warsaw92971171997-01-03 00:14:25 +0000942}
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000943
944void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000945PyOS_InitInterrupts(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000946{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000947 PyObject *m = PyInit_signal();
948 if (m) {
949 _PyImport_FixupExtension(m, "signal", "signal");
950 Py_DECREF(m);
951 }
Guido van Rossum08c16611997-08-02 03:01:42 +0000952}
953
954void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000955PyOS_FiniInterrupts(void)
Guido van Rossum08c16611997-08-02 03:01:42 +0000956{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000957 finisignal();
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000958}
959
960int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000961PyOS_InterruptOccurred(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000962{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000963 if (Handlers[SIGINT].tripped) {
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000964#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000965 if (PyThread_get_thread_ident() != main_thread)
966 return 0;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000967#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000968 Handlers[SIGINT].tripped = 0;
969 return 1;
970 }
971 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000972}
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000973
974void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000975PyOS_AfterFork(void)
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000976{
977#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000978 PyEval_ReInitThreads();
979 main_thread = PyThread_get_thread_ident();
980 main_pid = getpid();
981 _PyImport_ReInitLock();
982 PyThread_ReInitTLS();
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000983#endif
984}