blob: 14297709c51d3afcea795171c3c0c636df251c78 [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 Curtinb42a21d2010-08-06 19:52:50 +000010#include <Windows.h>
11#ifdef HAVE_PROCESS_H
Guido van Rossum644a12b1997-04-09 19:24:53 +000012#include <process.h>
13#endif
Brian Curtinb42a21d2010-08-06 19:52:50 +000014#endif
Guido van Rossum644a12b1997-04-09 19:24:53 +000015
Guido van Rossum398d9fe1994-05-11 08:59:13 +000016#include <signal.h>
17
Christian Heimes5fb7c2a2007-12-24 08:52:31 +000018#include <sys/stat.h>
Martin v. Löwis3bf3cc02008-03-24 14:05:07 +000019#ifdef HAVE_SYS_TIME_H
Martin v. Löwis823725e2008-03-24 13:39:54 +000020#include <sys/time.h>
Martin v. Löwis3bf3cc02008-03-24 14:05:07 +000021#endif
Christian Heimes5fb7c2a2007-12-24 08:52:31 +000022
Guido van Rossumbb4ba121994-06-23 11:25:45 +000023#ifndef SIG_ERR
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +000024#define SIG_ERR ((PyOS_sighandler_t)(-1))
Guido van Rossumbb4ba121994-06-23 11:25:45 +000025#endif
26
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000027#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000028#define NSIG 12
29#include <process.h>
30#endif
31
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000032#ifndef NSIG
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000033# if defined(_NSIG)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000034# define NSIG _NSIG /* For BSD/SysV */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000035# elif defined(_SIGMAX)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000036# define NSIG (_SIGMAX + 1) /* For QNX */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000037# elif defined(SIGMAX)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000038# define NSIG (SIGMAX + 1) /* For djgpp */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000039# else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000040# define NSIG 64 /* Use a reasonable default value */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000041# endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000042#endif
43
44
Guido van Rossumbb4ba121994-06-23 11:25:45 +000045/*
46 NOTES ON THE INTERACTION BETWEEN SIGNALS AND THREADS
47
48 When threads are supported, we want the following semantics:
49
50 - only the main thread can set a signal handler
51 - any thread can get a signal handler
52 - signals are only delivered to the main thread
53
54 I.e. we don't support "synchronous signals" like SIGFPE (catching
55 this doesn't make much sense in Python anyway) nor do we support
56 signals as a means of inter-thread communication, since not all
57 thread implementations support that (at least our thread library
58 doesn't).
59
60 We still have the problem that in some implementations signals
61 generated by the keyboard (e.g. SIGINT) are delivered to all
62 threads (e.g. SGI), while in others (e.g. Solaris) such signals are
63 delivered to one random thread (an intermediate possibility would
Guido van Rossuma3c04b01995-01-12 11:29:01 +000064 be to deliver it to the main thread -- POSIX?). For now, we have
Guido van Rossumbb4ba121994-06-23 11:25:45 +000065 a working implementation that works in all three cases -- the
66 handler ignores signals if getpid() isn't the same as in the main
67 thread. XXX This is a hack.
68
Guido van Rossum9e8181b2000-09-19 00:46:46 +000069 GNU pth is a user-space threading library, and as such, all threads
70 run within the same process. In this case, if the currently running
71 thread is not the main_thread, send the signal to the main_thread.
Guido van Rossumbb4ba121994-06-23 11:25:45 +000072*/
73
74#ifdef WITH_THREAD
Guido van Rossum295b8e51997-06-06 21:16:41 +000075#include <sys/types.h> /* For pid_t */
Guido van Rossum49b56061998-10-01 20:42:43 +000076#include "pythread.h"
Guido van Rossumbb4ba121994-06-23 11:25:45 +000077static long main_thread;
78static pid_t main_pid;
79#endif
80
Barry Warsaw92971171997-01-03 00:14:25 +000081static struct {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000082 int tripped;
83 PyObject *func;
Barry Warsaw92971171997-01-03 00:14:25 +000084} Handlers[NSIG];
Guido van Rossum398d9fe1994-05-11 08:59:13 +000085
Christian Heimes5fb7c2a2007-12-24 08:52:31 +000086static sig_atomic_t wakeup_fd = -1;
87
Christian Heimesb76922a2007-12-11 01:06:40 +000088/* Speed up sigcheck() when none tripped */
89static volatile sig_atomic_t is_tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +000090
Barry Warsaw92971171997-01-03 00:14:25 +000091static PyObject *DefaultHandler;
92static PyObject *IgnoreHandler;
93static PyObject *IntHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +000094
Martin v. Löwisf58de1b2001-03-06 12:13:56 +000095/* On Solaris 8, gcc will produce a warning that the function
96 declaration is not a prototype. This is caused by the definition of
97 SIG_DFL as (void (*)())0; the correct declaration would have been
98 (void (*)(int))0. */
99
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000100static PyOS_sighandler_t old_siginthandler = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +0000101
Martin v. Löwis823725e2008-03-24 13:39:54 +0000102#ifdef HAVE_GETITIMER
103static PyObject *ItimerError;
104
105/* auxiliary functions for setitimer/getitimer */
106static void
107timeval_from_double(double d, struct timeval *tv)
108{
109 tv->tv_sec = floor(d);
110 tv->tv_usec = fmod(d, 1.0) * 1000000.0;
111}
112
Christian Heimes1a8501c2008-10-02 19:56:01 +0000113Py_LOCAL_INLINE(double)
Martin v. Löwis823725e2008-03-24 13:39:54 +0000114double_from_timeval(struct timeval *tv)
115{
116 return tv->tv_sec + (double)(tv->tv_usec / 1000000.0);
117}
118
119static PyObject *
120itimer_retval(struct itimerval *iv)
121{
122 PyObject *r, *v;
123
124 r = PyTuple_New(2);
125 if (r == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000126 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000127
128 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_value)))) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000129 Py_DECREF(r);
130 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000131 }
132
133 PyTuple_SET_ITEM(r, 0, v);
134
135 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_interval)))) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000136 Py_DECREF(r);
137 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000138 }
139
140 PyTuple_SET_ITEM(r, 1, v);
141
142 return r;
143}
144#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000145
Guido van Rossume4485b01994-09-07 14:32:49 +0000146static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000147signal_default_int_handler(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000148{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000149 PyErr_SetNone(PyExc_KeyboardInterrupt);
150 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000151}
152
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000153PyDoc_STRVAR(default_int_handler_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000154"default_int_handler(...)\n\
155\n\
Michael W. Hudson24ec2112004-06-17 15:55:53 +0000156The default handler for SIGINT installed by Python.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000157It raises KeyboardInterrupt.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000158
Thomas Wouters0796b002000-07-22 23:49:30 +0000159
160static int
161checksignals_witharg(void * unused)
162{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000163 return PyErr_CheckSignals();
Thomas Wouters0796b002000-07-22 23:49:30 +0000164}
165
Tim Peters4f1b2082000-07-23 21:18:09 +0000166static void
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200167trip_signal(int sig_num)
168{
169 Handlers[sig_num].tripped = 1;
170 if (is_tripped)
171 return;
172 /* Set is_tripped after setting .tripped, as it gets
173 cleared in PyErr_CheckSignals() before .tripped. */
174 is_tripped = 1;
175 Py_AddPendingCall(checksignals_witharg, NULL);
176 if (wakeup_fd != -1)
177 write(wakeup_fd, "\0", 1);
178}
179
180static void
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000181signal_handler(int sig_num)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000182{
Antoine Pitrou8d46e422010-11-05 19:54:58 +0000183 int save_errno = errno;
184
185#if defined(WITH_THREAD) && defined(WITH_PTH)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000186 if (PyThread_get_thread_ident() != main_thread) {
187 pth_raise(*(pth_t *) main_thread, sig_num);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000188 }
Antoine Pitrou8d46e422010-11-05 19:54:58 +0000189 else
Guido van Rossum9e8181b2000-09-19 00:46:46 +0000190#endif
Antoine Pitrou8d46e422010-11-05 19:54:58 +0000191 {
192#ifdef WITH_THREAD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000193 /* See NOTES section above */
Antoine Pitrou8d46e422010-11-05 19:54:58 +0000194 if (getpid() == main_pid)
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000195#endif
Antoine Pitrou8d46e422010-11-05 19:54:58 +0000196 {
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200197 trip_signal(sig_num);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000198 }
Antoine Pitrou8d46e422010-11-05 19:54:58 +0000199
Jean-Paul Calderone9c39bc72010-05-09 03:25:16 +0000200#ifndef HAVE_SIGACTION
Antoine Pitrou8d46e422010-11-05 19:54:58 +0000201#ifdef SIGCHLD
202 /* To avoid infinite recursion, this signal remains
203 reset until explicit re-instated.
204 Don't clear the 'func' field as it is our pointer
205 to the Python handler... */
206 if (sig_num != SIGCHLD)
207#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000208 /* If the handler was not set up with sigaction, reinstall it. See
209 * Python/pythonrun.c for the implementation of PyOS_setsig which
210 * makes this true. See also issue8354. */
211 PyOS_setsig(sig_num, signal_handler);
Jean-Paul Calderone9c39bc72010-05-09 03:25:16 +0000212#endif
Antoine Pitrou8d46e422010-11-05 19:54:58 +0000213 }
214
215 /* Issue #10311: asynchronously executing signal handlers should not
216 mutate errno under the feet of unsuspecting C code. */
217 errno = save_errno;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000218}
Guido van Rossume4485b01994-09-07 14:32:49 +0000219
Guido van Rossum06d511d1995-03-10 15:13:48 +0000220
Guido van Rossum1171ee61997-08-22 20:42:00 +0000221#ifdef HAVE_ALARM
Guido van Rossume4485b01994-09-07 14:32:49 +0000222static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000223signal_alarm(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000224{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000225 int t;
226 if (!PyArg_ParseTuple(args, "i:alarm", &t))
227 return NULL;
228 /* alarm() returns the number of seconds remaining */
229 return PyLong_FromLong((long)alarm(t));
Guido van Rossumaa0f4c71994-08-23 13:49:37 +0000230}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000231
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000232PyDoc_STRVAR(alarm_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000233"alarm(seconds)\n\
234\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000235Arrange for SIGALRM to arrive after the given number of seconds.");
Guido van Rossum06d511d1995-03-10 15:13:48 +0000236#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000237
Guido van Rossum1171ee61997-08-22 20:42:00 +0000238#ifdef HAVE_PAUSE
Guido van Rossuma597dde1995-01-10 20:56:29 +0000239static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000240signal_pause(PyObject *self)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000241{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000242 Py_BEGIN_ALLOW_THREADS
243 (void)pause();
244 Py_END_ALLOW_THREADS
245 /* make sure that any exceptions that got raised are propagated
246 * back into Python
247 */
248 if (PyErr_CheckSignals())
249 return NULL;
Barry Warsaw92971171997-01-03 00:14:25 +0000250
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000251 Py_INCREF(Py_None);
252 return Py_None;
Guido van Rossume4485b01994-09-07 14:32:49 +0000253}
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000254PyDoc_STRVAR(pause_doc,
Barry Warsaw1ee36ff1998-07-21 22:41:18 +0000255"pause()\n\
256\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000257Wait until a signal arrives.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000258
Guido van Rossum06d511d1995-03-10 15:13:48 +0000259#endif
Guido van Rossume4485b01994-09-07 14:32:49 +0000260
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000261
Guido van Rossume4485b01994-09-07 14:32:49 +0000262static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000263signal_signal(PyObject *self, PyObject *args)
Guido van Rossume4485b01994-09-07 14:32:49 +0000264{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000265 PyObject *obj;
266 int sig_num;
267 PyObject *old_handler;
268 void (*func)(int);
269 if (!PyArg_ParseTuple(args, "iO:signal", &sig_num, &obj))
270 return NULL;
Brian Curtin3f004b12010-08-06 19:34:52 +0000271#ifdef MS_WINDOWS
272 /* Validate that sig_num is one of the allowable signals */
Brian Curtin912443c2010-09-06 16:10:04 +0000273 switch (sig_num) {
274 case SIGABRT: break;
Brian Curtineccd4d92010-10-01 15:09:53 +0000275#ifdef SIGBREAK
276 /* Issue #10003: SIGBREAK is not documented as permitted, but works
277 and corresponds to CTRL_BREAK_EVENT. */
278 case SIGBREAK: break;
279#endif
Brian Curtin912443c2010-09-06 16:10:04 +0000280 case SIGFPE: break;
281 case SIGILL: break;
282 case SIGINT: break;
283 case SIGSEGV: break;
284 case SIGTERM: break;
285 default:
286 PyErr_SetString(PyExc_ValueError, "invalid signal value");
287 return NULL;
Brian Curtin3f004b12010-08-06 19:34:52 +0000288 }
289#endif
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000290#ifdef WITH_THREAD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000291 if (PyThread_get_thread_ident() != main_thread) {
292 PyErr_SetString(PyExc_ValueError,
293 "signal only works in main thread");
294 return NULL;
295 }
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000296#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000297 if (sig_num < 1 || sig_num >= NSIG) {
298 PyErr_SetString(PyExc_ValueError,
299 "signal number out of range");
300 return NULL;
301 }
302 if (obj == IgnoreHandler)
303 func = SIG_IGN;
304 else if (obj == DefaultHandler)
305 func = SIG_DFL;
306 else if (!PyCallable_Check(obj)) {
307 PyErr_SetString(PyExc_TypeError,
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000308"signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object");
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000309 return NULL;
310 }
311 else
312 func = signal_handler;
313 if (PyOS_setsig(sig_num, func) == SIG_ERR) {
314 PyErr_SetFromErrno(PyExc_RuntimeError);
315 return NULL;
316 }
317 old_handler = Handlers[sig_num].func;
318 Handlers[sig_num].tripped = 0;
319 Py_INCREF(obj);
320 Handlers[sig_num].func = obj;
321 return old_handler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000322}
323
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000324PyDoc_STRVAR(signal_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000325"signal(sig, action) -> action\n\
326\n\
327Set the action for the given signal. The action can be SIG_DFL,\n\
328SIG_IGN, or a callable Python object. The previous action is\n\
329returned. See getsignal() for possible return values.\n\
330\n\
331*** IMPORTANT NOTICE ***\n\
332A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000333the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000334
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000335
Guido van Rossume4485b01994-09-07 14:32:49 +0000336static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000337signal_getsignal(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000338{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000339 int sig_num;
340 PyObject *old_handler;
341 if (!PyArg_ParseTuple(args, "i:getsignal", &sig_num))
342 return NULL;
343 if (sig_num < 1 || sig_num >= NSIG) {
344 PyErr_SetString(PyExc_ValueError,
345 "signal number out of range");
346 return NULL;
347 }
348 old_handler = Handlers[sig_num].func;
349 Py_INCREF(old_handler);
350 return old_handler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000351}
352
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000353PyDoc_STRVAR(getsignal_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000354"getsignal(sig) -> action\n\
355\n\
356Return the current action for the given signal. The return value can be:\n\
357SIG_IGN -- if the signal is being ignored\n\
358SIG_DFL -- if the default action for the signal is in effect\n\
359None -- if an unknown handler is in effect\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000360anything else -- the callable Python object used as a handler");
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000361
Christian Heimes8640e742008-02-23 16:23:06 +0000362#ifdef HAVE_SIGINTERRUPT
363PyDoc_STRVAR(siginterrupt_doc,
364"siginterrupt(sig, flag) -> None\n\
365change system call restart behaviour: if flag is False, system calls\n\
366will be restarted when interrupted by signal sig, else system calls\n\
367will be interrupted.");
368
369static PyObject *
370signal_siginterrupt(PyObject *self, PyObject *args)
371{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000372 int sig_num;
373 int flag;
Christian Heimes8640e742008-02-23 16:23:06 +0000374
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000375 if (!PyArg_ParseTuple(args, "ii:siginterrupt", &sig_num, &flag))
376 return NULL;
377 if (sig_num < 1 || sig_num >= NSIG) {
378 PyErr_SetString(PyExc_ValueError,
379 "signal number out of range");
380 return NULL;
381 }
382 if (siginterrupt(sig_num, flag)<0) {
383 PyErr_SetFromErrno(PyExc_RuntimeError);
384 return NULL;
385 }
Christian Heimes8640e742008-02-23 16:23:06 +0000386
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000387 Py_INCREF(Py_None);
388 return Py_None;
Christian Heimes8640e742008-02-23 16:23:06 +0000389}
390
391#endif
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000392
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000393static PyObject *
394signal_set_wakeup_fd(PyObject *self, PyObject *args)
395{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000396 struct stat buf;
397 int fd, old_fd;
398 if (!PyArg_ParseTuple(args, "i:set_wakeup_fd", &fd))
399 return NULL;
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000400#ifdef WITH_THREAD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000401 if (PyThread_get_thread_ident() != main_thread) {
402 PyErr_SetString(PyExc_ValueError,
403 "set_wakeup_fd only works in main thread");
404 return NULL;
405 }
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000406#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000407 if (fd != -1 && fstat(fd, &buf) != 0) {
408 PyErr_SetString(PyExc_ValueError, "invalid fd");
409 return NULL;
410 }
411 old_fd = wakeup_fd;
412 wakeup_fd = fd;
413 return PyLong_FromLong(old_fd);
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000414}
415
416PyDoc_STRVAR(set_wakeup_fd_doc,
417"set_wakeup_fd(fd) -> fd\n\
418\n\
419Sets the fd to be written to (with '\\0') when a signal\n\
420comes in. A library can use this to wakeup select or poll.\n\
421The previous fd is returned.\n\
422\n\
423The fd must be non-blocking.");
424
425/* C API for the same, without all the error checking */
426int
427PySignal_SetWakeupFd(int fd)
428{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000429 int old_fd = wakeup_fd;
430 if (fd < 0)
431 fd = -1;
432 wakeup_fd = fd;
433 return old_fd;
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000434}
435
436
Martin v. Löwis823725e2008-03-24 13:39:54 +0000437#ifdef HAVE_SETITIMER
438static PyObject *
439signal_setitimer(PyObject *self, PyObject *args)
440{
441 double first;
442 double interval = 0;
443 int which;
444 struct itimerval new, old;
445
446 if(!PyArg_ParseTuple(args, "id|d:setitimer", &which, &first, &interval))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000447 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000448
449 timeval_from_double(first, &new.it_value);
450 timeval_from_double(interval, &new.it_interval);
451 /* Let OS check "which" value */
452 if (setitimer(which, &new, &old) != 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000453 PyErr_SetFromErrno(ItimerError);
454 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000455 }
456
457 return itimer_retval(&old);
458}
459
460PyDoc_STRVAR(setitimer_doc,
461"setitimer(which, seconds[, interval])\n\
462\n\
463Sets given itimer (one of ITIMER_REAL, ITIMER_VIRTUAL\n\
464or ITIMER_PROF) to fire after value seconds and after\n\
465that every interval seconds.\n\
466The itimer can be cleared by setting seconds to zero.\n\
467\n\
468Returns old values as a tuple: (delay, interval).");
469#endif
470
471
472#ifdef HAVE_GETITIMER
473static PyObject *
474signal_getitimer(PyObject *self, PyObject *args)
475{
476 int which;
477 struct itimerval old;
478
479 if (!PyArg_ParseTuple(args, "i:getitimer", &which))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000480 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000481
482 if (getitimer(which, &old) != 0) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000483 PyErr_SetFromErrno(ItimerError);
484 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000485 }
486
487 return itimer_retval(&old);
488}
489
490PyDoc_STRVAR(getitimer_doc,
491"getitimer(which)\n\
492\n\
493Returns current value of given itimer.");
494#endif
495
496
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000497/* List of functions defined in the module */
Barry Warsaw92971171997-01-03 00:14:25 +0000498static PyMethodDef signal_methods[] = {
Guido van Rossum1171ee61997-08-22 20:42:00 +0000499#ifdef HAVE_ALARM
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000500 {"alarm", signal_alarm, METH_VARARGS, alarm_doc},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000501#endif
Martin v. Löwis823725e2008-03-24 13:39:54 +0000502#ifdef HAVE_SETITIMER
503 {"setitimer", signal_setitimer, METH_VARARGS, setitimer_doc},
504#endif
505#ifdef HAVE_GETITIMER
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000506 {"getitimer", signal_getitimer, METH_VARARGS, getitimer_doc},
Martin v. Löwis823725e2008-03-24 13:39:54 +0000507#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000508 {"signal", signal_signal, METH_VARARGS, signal_doc},
509 {"getsignal", signal_getsignal, METH_VARARGS, getsignal_doc},
510 {"set_wakeup_fd", signal_set_wakeup_fd, METH_VARARGS, set_wakeup_fd_doc},
Christian Heimes8640e742008-02-23 16:23:06 +0000511#ifdef HAVE_SIGINTERRUPT
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000512 {"siginterrupt", signal_siginterrupt, METH_VARARGS, siginterrupt_doc},
Christian Heimes8640e742008-02-23 16:23:06 +0000513#endif
Guido van Rossum1171ee61997-08-22 20:42:00 +0000514#ifdef HAVE_PAUSE
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000515 {"pause", (PyCFunction)signal_pause,
516 METH_NOARGS,pause_doc},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000517#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000518 {"default_int_handler", signal_default_int_handler,
519 METH_VARARGS, default_int_handler_doc},
520 {NULL, NULL} /* sentinel */
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000521};
522
Barry Warsaw92971171997-01-03 00:14:25 +0000523
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000524PyDoc_STRVAR(module_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000525"This module provides mechanisms to use signal handlers in Python.\n\
526\n\
527Functions:\n\
528\n\
529alarm() -- cause SIGALRM after a specified time [Unix only]\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +0000530setitimer() -- cause a signal (described below) after a specified\n\
531 float time and the timer may restart then [Unix only]\n\
532getitimer() -- get current value of timer [Unix only]\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000533signal() -- set the action for a given signal\n\
534getsignal() -- get the signal action for a given signal\n\
535pause() -- wait until a signal arrives [Unix only]\n\
536default_int_handler() -- default SIGINT handler\n\
537\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +0000538signal constants:\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000539SIG_DFL -- used to refer to the system default handler\n\
540SIG_IGN -- used to ignore the signal\n\
541NSIG -- number of defined signals\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000542SIGINT, SIGTERM, etc. -- signal numbers\n\
543\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +0000544itimer constants:\n\
545ITIMER_REAL -- decrements in real time, and delivers SIGALRM upon\n\
546 expiration\n\
547ITIMER_VIRTUAL -- decrements only when the process is executing,\n\
548 and delivers SIGVTALRM upon expiration\n\
549ITIMER_PROF -- decrements both when the process is executing and\n\
550 when the system is executing on behalf of the process.\n\
551 Coupled with ITIMER_VIRTUAL, this timer is usually\n\
552 used to profile the time spent by the application\n\
553 in user and kernel space. SIGPROF is delivered upon\n\
554 expiration.\n\
555\n\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000556*** IMPORTANT NOTICE ***\n\
557A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000558the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000559
Martin v. Löwis1a214512008-06-11 05:26:20 +0000560static struct PyModuleDef signalmodule = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000561 PyModuleDef_HEAD_INIT,
562 "signal",
563 module_doc,
564 -1,
565 signal_methods,
566 NULL,
567 NULL,
568 NULL,
569 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000570};
571
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000572PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000573PyInit_signal(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000574{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000575 PyObject *m, *d, *x;
576 int i;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000577
578#ifdef WITH_THREAD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000579 main_thread = PyThread_get_thread_ident();
580 main_pid = getpid();
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000581#endif
582
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000583 /* Create the module and add the functions */
584 m = PyModule_Create(&signalmodule);
585 if (m == NULL)
586 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000587
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000588 /* Add some symbolic constants to the module */
589 d = PyModule_GetDict(m);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000590
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000591 x = DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL);
592 if (!x || PyDict_SetItemString(d, "SIG_DFL", x) < 0)
593 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000594
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000595 x = IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN);
596 if (!x || PyDict_SetItemString(d, "SIG_IGN", x) < 0)
597 goto finally;
Barry Warsaw92971171997-01-03 00:14:25 +0000598
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000599 x = PyLong_FromLong((long)NSIG);
600 if (!x || PyDict_SetItemString(d, "NSIG", x) < 0)
601 goto finally;
602 Py_DECREF(x);
Barry Warsaw92971171997-01-03 00:14:25 +0000603
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000604 x = IntHandler = PyDict_GetItemString(d, "default_int_handler");
605 if (!x)
606 goto finally;
607 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +0000608
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000609 Handlers[0].tripped = 0;
610 for (i = 1; i < NSIG; i++) {
611 void (*t)(int);
612 t = PyOS_getsig(i);
613 Handlers[i].tripped = 0;
614 if (t == SIG_DFL)
615 Handlers[i].func = DefaultHandler;
616 else if (t == SIG_IGN)
617 Handlers[i].func = IgnoreHandler;
618 else
619 Handlers[i].func = Py_None; /* None of our business */
620 Py_INCREF(Handlers[i].func);
621 }
622 if (Handlers[SIGINT].func == DefaultHandler) {
623 /* Install default int handler */
624 Py_INCREF(IntHandler);
625 Py_DECREF(Handlers[SIGINT].func);
626 Handlers[SIGINT].func = IntHandler;
627 old_siginthandler = PyOS_setsig(SIGINT, signal_handler);
628 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000629
630#ifdef SIGHUP
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000631 x = PyLong_FromLong(SIGHUP);
632 PyDict_SetItemString(d, "SIGHUP", x);
633 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000634#endif
635#ifdef SIGINT
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000636 x = PyLong_FromLong(SIGINT);
637 PyDict_SetItemString(d, "SIGINT", x);
638 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000639#endif
Tim Peters1ce3cf72001-10-01 17:58:40 +0000640#ifdef SIGBREAK
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000641 x = PyLong_FromLong(SIGBREAK);
642 PyDict_SetItemString(d, "SIGBREAK", x);
643 Py_XDECREF(x);
Tim Peters1ce3cf72001-10-01 17:58:40 +0000644#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000645#ifdef SIGQUIT
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000646 x = PyLong_FromLong(SIGQUIT);
647 PyDict_SetItemString(d, "SIGQUIT", x);
648 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000649#endif
650#ifdef SIGILL
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000651 x = PyLong_FromLong(SIGILL);
652 PyDict_SetItemString(d, "SIGILL", x);
653 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000654#endif
655#ifdef SIGTRAP
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000656 x = PyLong_FromLong(SIGTRAP);
657 PyDict_SetItemString(d, "SIGTRAP", x);
658 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000659#endif
660#ifdef SIGIOT
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000661 x = PyLong_FromLong(SIGIOT);
662 PyDict_SetItemString(d, "SIGIOT", x);
663 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000664#endif
665#ifdef SIGABRT
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000666 x = PyLong_FromLong(SIGABRT);
667 PyDict_SetItemString(d, "SIGABRT", x);
668 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000669#endif
670#ifdef SIGEMT
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000671 x = PyLong_FromLong(SIGEMT);
672 PyDict_SetItemString(d, "SIGEMT", x);
673 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000674#endif
675#ifdef SIGFPE
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000676 x = PyLong_FromLong(SIGFPE);
677 PyDict_SetItemString(d, "SIGFPE", x);
678 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000679#endif
680#ifdef SIGKILL
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000681 x = PyLong_FromLong(SIGKILL);
682 PyDict_SetItemString(d, "SIGKILL", x);
683 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000684#endif
685#ifdef SIGBUS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000686 x = PyLong_FromLong(SIGBUS);
687 PyDict_SetItemString(d, "SIGBUS", x);
688 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000689#endif
690#ifdef SIGSEGV
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000691 x = PyLong_FromLong(SIGSEGV);
692 PyDict_SetItemString(d, "SIGSEGV", x);
693 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000694#endif
695#ifdef SIGSYS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000696 x = PyLong_FromLong(SIGSYS);
697 PyDict_SetItemString(d, "SIGSYS", x);
698 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000699#endif
700#ifdef SIGPIPE
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000701 x = PyLong_FromLong(SIGPIPE);
702 PyDict_SetItemString(d, "SIGPIPE", x);
703 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000704#endif
705#ifdef SIGALRM
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000706 x = PyLong_FromLong(SIGALRM);
707 PyDict_SetItemString(d, "SIGALRM", x);
708 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000709#endif
710#ifdef SIGTERM
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000711 x = PyLong_FromLong(SIGTERM);
712 PyDict_SetItemString(d, "SIGTERM", x);
713 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000714#endif
715#ifdef SIGUSR1
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000716 x = PyLong_FromLong(SIGUSR1);
717 PyDict_SetItemString(d, "SIGUSR1", x);
718 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000719#endif
720#ifdef SIGUSR2
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000721 x = PyLong_FromLong(SIGUSR2);
722 PyDict_SetItemString(d, "SIGUSR2", x);
723 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000724#endif
725#ifdef SIGCLD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000726 x = PyLong_FromLong(SIGCLD);
727 PyDict_SetItemString(d, "SIGCLD", x);
728 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000729#endif
730#ifdef SIGCHLD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000731 x = PyLong_FromLong(SIGCHLD);
732 PyDict_SetItemString(d, "SIGCHLD", x);
733 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000734#endif
735#ifdef SIGPWR
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000736 x = PyLong_FromLong(SIGPWR);
737 PyDict_SetItemString(d, "SIGPWR", x);
738 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000739#endif
740#ifdef SIGIO
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000741 x = PyLong_FromLong(SIGIO);
742 PyDict_SetItemString(d, "SIGIO", x);
743 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000744#endif
745#ifdef SIGURG
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000746 x = PyLong_FromLong(SIGURG);
747 PyDict_SetItemString(d, "SIGURG", x);
748 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000749#endif
750#ifdef SIGWINCH
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000751 x = PyLong_FromLong(SIGWINCH);
752 PyDict_SetItemString(d, "SIGWINCH", x);
753 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000754#endif
755#ifdef SIGPOLL
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000756 x = PyLong_FromLong(SIGPOLL);
757 PyDict_SetItemString(d, "SIGPOLL", x);
758 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000759#endif
760#ifdef SIGSTOP
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000761 x = PyLong_FromLong(SIGSTOP);
762 PyDict_SetItemString(d, "SIGSTOP", x);
763 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000764#endif
765#ifdef SIGTSTP
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000766 x = PyLong_FromLong(SIGTSTP);
767 PyDict_SetItemString(d, "SIGTSTP", x);
768 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000769#endif
770#ifdef SIGCONT
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000771 x = PyLong_FromLong(SIGCONT);
772 PyDict_SetItemString(d, "SIGCONT", x);
773 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000774#endif
775#ifdef SIGTTIN
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000776 x = PyLong_FromLong(SIGTTIN);
777 PyDict_SetItemString(d, "SIGTTIN", x);
778 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000779#endif
780#ifdef SIGTTOU
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000781 x = PyLong_FromLong(SIGTTOU);
782 PyDict_SetItemString(d, "SIGTTOU", x);
783 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000784#endif
785#ifdef SIGVTALRM
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000786 x = PyLong_FromLong(SIGVTALRM);
787 PyDict_SetItemString(d, "SIGVTALRM", x);
788 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000789#endif
790#ifdef SIGPROF
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000791 x = PyLong_FromLong(SIGPROF);
792 PyDict_SetItemString(d, "SIGPROF", x);
793 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000794#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000795#ifdef SIGXCPU
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000796 x = PyLong_FromLong(SIGXCPU);
797 PyDict_SetItemString(d, "SIGXCPU", x);
798 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000799#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000800#ifdef SIGXFSZ
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000801 x = PyLong_FromLong(SIGXFSZ);
802 PyDict_SetItemString(d, "SIGXFSZ", x);
803 Py_XDECREF(x);
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000804#endif
Anthony Baxterf37f37d2003-07-31 10:35:29 +0000805#ifdef SIGRTMIN
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000806 x = PyLong_FromLong(SIGRTMIN);
807 PyDict_SetItemString(d, "SIGRTMIN", x);
808 Py_XDECREF(x);
Anthony Baxterf37f37d2003-07-31 10:35:29 +0000809#endif
810#ifdef SIGRTMAX
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000811 x = PyLong_FromLong(SIGRTMAX);
812 PyDict_SetItemString(d, "SIGRTMAX", x);
813 Py_XDECREF(x);
Anthony Baxterf37f37d2003-07-31 10:35:29 +0000814#endif
Martin v. Löwis175af252002-01-12 11:43:25 +0000815#ifdef SIGINFO
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000816 x = PyLong_FromLong(SIGINFO);
817 PyDict_SetItemString(d, "SIGINFO", x);
818 Py_XDECREF(x);
Martin v. Löwis175af252002-01-12 11:43:25 +0000819#endif
Martin v. Löwis823725e2008-03-24 13:39:54 +0000820
821#ifdef ITIMER_REAL
822 x = PyLong_FromLong(ITIMER_REAL);
823 PyDict_SetItemString(d, "ITIMER_REAL", x);
824 Py_DECREF(x);
825#endif
826#ifdef ITIMER_VIRTUAL
827 x = PyLong_FromLong(ITIMER_VIRTUAL);
828 PyDict_SetItemString(d, "ITIMER_VIRTUAL", x);
829 Py_DECREF(x);
830#endif
831#ifdef ITIMER_PROF
832 x = PyLong_FromLong(ITIMER_PROF);
833 PyDict_SetItemString(d, "ITIMER_PROF", x);
834 Py_DECREF(x);
835#endif
836
837#if defined (HAVE_SETITIMER) || defined (HAVE_GETITIMER)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000838 ItimerError = PyErr_NewException("signal.ItimerError",
839 PyExc_IOError, NULL);
Neal Norwitz2f99b242008-08-24 05:48:10 +0000840 if (ItimerError != NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000841 PyDict_SetItemString(d, "ItimerError", ItimerError);
Martin v. Löwis823725e2008-03-24 13:39:54 +0000842#endif
843
Martin v. Löwis1a214512008-06-11 05:26:20 +0000844 if (PyErr_Occurred()) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000845 Py_DECREF(m);
846 m = NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000847 }
Barry Warsaw92971171997-01-03 00:14:25 +0000848
Barry Warsaw92971171997-01-03 00:14:25 +0000849 finally:
Martin v. Löwis1a214512008-06-11 05:26:20 +0000850 return m;
Guido van Rossum08c16611997-08-02 03:01:42 +0000851}
852
853static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000854finisignal(void)
Guido van Rossum08c16611997-08-02 03:01:42 +0000855{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000856 int i;
857 PyObject *func;
Guido van Rossum08c16611997-08-02 03:01:42 +0000858
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000859 PyOS_setsig(SIGINT, old_siginthandler);
860 old_siginthandler = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +0000861
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000862 for (i = 1; i < NSIG; i++) {
863 func = Handlers[i].func;
864 Handlers[i].tripped = 0;
865 Handlers[i].func = NULL;
866 if (i != SIGINT && func != NULL && func != Py_None &&
867 func != DefaultHandler && func != IgnoreHandler)
868 PyOS_setsig(i, SIG_DFL);
869 Py_XDECREF(func);
870 }
Guido van Rossum08c16611997-08-02 03:01:42 +0000871
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000872 Py_XDECREF(IntHandler);
873 IntHandler = NULL;
874 Py_XDECREF(DefaultHandler);
875 DefaultHandler = NULL;
876 Py_XDECREF(IgnoreHandler);
877 IgnoreHandler = NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000878}
879
Barry Warsaw92971171997-01-03 00:14:25 +0000880
Barry Warsaw92971171997-01-03 00:14:25 +0000881/* Declared in pyerrors.h */
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000882int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000883PyErr_CheckSignals(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000884{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000885 int i;
886 PyObject *f;
Barry Warsaw92971171997-01-03 00:14:25 +0000887
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000888 if (!is_tripped)
889 return 0;
Christian Heimesb76922a2007-12-11 01:06:40 +0000890
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000891#ifdef WITH_THREAD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000892 if (PyThread_get_thread_ident() != main_thread)
893 return 0;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000894#endif
Christian Heimesb76922a2007-12-11 01:06:40 +0000895
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000896 /*
897 * The is_tripped variable is meant to speed up the calls to
898 * PyErr_CheckSignals (both directly or via pending calls) when no
899 * signal has arrived. This variable is set to 1 when a signal arrives
900 * and it is set to 0 here, when we know some signals arrived. This way
901 * we can run the registered handlers with no signals blocked.
902 *
903 * NOTE: with this approach we can have a situation where is_tripped is
904 * 1 but we have no more signals to handle (Handlers[i].tripped
905 * is 0 for every signal i). This won't do us any harm (except
906 * we're gonna spent some cycles for nothing). This happens when
907 * we receive a signal i after we zero is_tripped and before we
908 * check Handlers[i].tripped.
909 */
910 is_tripped = 0;
Christian Heimesb76922a2007-12-11 01:06:40 +0000911
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000912 if (!(f = (PyObject *)PyEval_GetFrame()))
913 f = Py_None;
Christian Heimesb76922a2007-12-11 01:06:40 +0000914
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000915 for (i = 1; i < NSIG; i++) {
916 if (Handlers[i].tripped) {
917 PyObject *result = NULL;
918 PyObject *arglist = Py_BuildValue("(iO)", i, f);
919 Handlers[i].tripped = 0;
Barry Warsaw92971171997-01-03 00:14:25 +0000920
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000921 if (arglist) {
922 result = PyEval_CallObject(Handlers[i].func,
923 arglist);
924 Py_DECREF(arglist);
925 }
926 if (!result)
927 return -1;
Barry Warsaw92971171997-01-03 00:14:25 +0000928
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000929 Py_DECREF(result);
930 }
931 }
Christian Heimesb76922a2007-12-11 01:06:40 +0000932
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000933 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000934}
935
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000936
Barry Warsaw92971171997-01-03 00:14:25 +0000937/* Replacements for intrcheck.c functionality
938 * Declared in pyerrors.h
939 */
940void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000941PyErr_SetInterrupt(void)
Barry Warsaw92971171997-01-03 00:14:25 +0000942{
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200943 trip_signal(SIGINT);
Barry Warsaw92971171997-01-03 00:14:25 +0000944}
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000945
946void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000947PyOS_InitInterrupts(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000948{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000949 PyObject *m = PyInit_signal();
950 if (m) {
951 _PyImport_FixupExtension(m, "signal", "signal");
952 Py_DECREF(m);
953 }
Guido van Rossum08c16611997-08-02 03:01:42 +0000954}
955
956void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000957PyOS_FiniInterrupts(void)
Guido van Rossum08c16611997-08-02 03:01:42 +0000958{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000959 finisignal();
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000960}
961
962int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000963PyOS_InterruptOccurred(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000964{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000965 if (Handlers[SIGINT].tripped) {
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000966#ifdef WITH_THREAD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000967 if (PyThread_get_thread_ident() != main_thread)
968 return 0;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000969#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000970 Handlers[SIGINT].tripped = 0;
971 return 1;
972 }
973 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000974}
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000975
976void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000977PyOS_AfterFork(void)
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000978{
979#ifdef WITH_THREAD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000980 PyEval_ReInitThreads();
981 main_thread = PyThread_get_thread_ident();
982 main_pid = getpid();
983 _PyImport_ReInitLock();
984 PyThread_ReInitTLS();
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000985#endif
986}