blob: c8626adf7e56f6db2ff212964dc740ec0b3606b4 [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
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00008#ifdef MS_WINDOWS
Brian Curtineb24d742010-04-12 17:16:38 +00009#include <Windows.h>
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
Victor Stinnera9293352011-04-30 15:21:58 +020025#if defined(HAVE_PTHREAD_SIGMASK) && !defined(HAVE_BROKEN_PTHREAD_SIGMASK)
26# define PYPTHREAD_SIGMASK
27#endif
28
29#if defined(PYPTHREAD_SIGMASK) && defined(HAVE_PTHREAD_H)
30# include <pthread.h>
31#endif
32
Guido van Rossumbb4ba121994-06-23 11:25:45 +000033#ifndef SIG_ERR
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +000034#define SIG_ERR ((PyOS_sighandler_t)(-1))
Guido van Rossumbb4ba121994-06-23 11:25:45 +000035#endif
36
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000037#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000038#define NSIG 12
39#include <process.h>
40#endif
41
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000042#ifndef NSIG
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000043# if defined(_NSIG)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000044# define NSIG _NSIG /* For BSD/SysV */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000045# elif defined(_SIGMAX)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000046# define NSIG (_SIGMAX + 1) /* For QNX */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000047# elif defined(SIGMAX)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000048# define NSIG (SIGMAX + 1) /* For djgpp */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000049# else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000050# define NSIG 64 /* Use a reasonable default value */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000051# endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000052#endif
53
54
Guido van Rossumbb4ba121994-06-23 11:25:45 +000055/*
56 NOTES ON THE INTERACTION BETWEEN SIGNALS AND THREADS
57
58 When threads are supported, we want the following semantics:
59
60 - only the main thread can set a signal handler
61 - any thread can get a signal handler
62 - signals are only delivered to the main thread
63
64 I.e. we don't support "synchronous signals" like SIGFPE (catching
65 this doesn't make much sense in Python anyway) nor do we support
66 signals as a means of inter-thread communication, since not all
67 thread implementations support that (at least our thread library
68 doesn't).
69
70 We still have the problem that in some implementations signals
71 generated by the keyboard (e.g. SIGINT) are delivered to all
72 threads (e.g. SGI), while in others (e.g. Solaris) such signals are
73 delivered to one random thread (an intermediate possibility would
Guido van Rossuma3c04b01995-01-12 11:29:01 +000074 be to deliver it to the main thread -- POSIX?). For now, we have
Guido van Rossumbb4ba121994-06-23 11:25:45 +000075 a working implementation that works in all three cases -- the
76 handler ignores signals if getpid() isn't the same as in the main
77 thread. XXX This is a hack.
78
Guido van Rossum9e8181b2000-09-19 00:46:46 +000079 GNU pth is a user-space threading library, and as such, all threads
80 run within the same process. In this case, if the currently running
81 thread is not the main_thread, send the signal to the main_thread.
Guido van Rossumbb4ba121994-06-23 11:25:45 +000082*/
83
84#ifdef WITH_THREAD
Guido van Rossum295b8e51997-06-06 21:16:41 +000085#include <sys/types.h> /* For pid_t */
Guido van Rossum49b56061998-10-01 20:42:43 +000086#include "pythread.h"
Guido van Rossumbb4ba121994-06-23 11:25:45 +000087static long main_thread;
88static pid_t main_pid;
89#endif
90
Barry Warsaw92971171997-01-03 00:14:25 +000091static struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000092 int tripped;
93 PyObject *func;
Barry Warsaw92971171997-01-03 00:14:25 +000094} Handlers[NSIG];
Guido van Rossum398d9fe1994-05-11 08:59:13 +000095
Christian Heimes5fb7c2a2007-12-24 08:52:31 +000096static sig_atomic_t wakeup_fd = -1;
97
Christian Heimesb76922a2007-12-11 01:06:40 +000098/* Speed up sigcheck() when none tripped */
99static volatile sig_atomic_t is_tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000100
Barry Warsaw92971171997-01-03 00:14:25 +0000101static PyObject *DefaultHandler;
102static PyObject *IgnoreHandler;
103static PyObject *IntHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000104
Martin v. Löwisf58de1b2001-03-06 12:13:56 +0000105/* On Solaris 8, gcc will produce a warning that the function
106 declaration is not a prototype. This is caused by the definition of
107 SIG_DFL as (void (*)())0; the correct declaration would have been
108 (void (*)(int))0. */
109
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000110static PyOS_sighandler_t old_siginthandler = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +0000111
Martin v. Löwis823725e2008-03-24 13:39:54 +0000112#ifdef HAVE_GETITIMER
113static PyObject *ItimerError;
114
115/* auxiliary functions for setitimer/getitimer */
116static void
117timeval_from_double(double d, struct timeval *tv)
118{
119 tv->tv_sec = floor(d);
120 tv->tv_usec = fmod(d, 1.0) * 1000000.0;
121}
122
Christian Heimes1a8501c2008-10-02 19:56:01 +0000123Py_LOCAL_INLINE(double)
Martin v. Löwis823725e2008-03-24 13:39:54 +0000124double_from_timeval(struct timeval *tv)
125{
126 return tv->tv_sec + (double)(tv->tv_usec / 1000000.0);
127}
128
129static PyObject *
130itimer_retval(struct itimerval *iv)
131{
132 PyObject *r, *v;
133
134 r = PyTuple_New(2);
135 if (r == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000136 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000137
138 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_value)))) {
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, 0, v);
144
145 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_interval)))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000146 Py_DECREF(r);
147 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000148 }
149
150 PyTuple_SET_ITEM(r, 1, v);
151
152 return r;
153}
154#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000155
Guido van Rossume4485b01994-09-07 14:32:49 +0000156static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000157signal_default_int_handler(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000158{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000159 PyErr_SetNone(PyExc_KeyboardInterrupt);
160 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000161}
162
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000163PyDoc_STRVAR(default_int_handler_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000164"default_int_handler(...)\n\
165\n\
Michael W. Hudson24ec2112004-06-17 15:55:53 +0000166The default handler for SIGINT installed by Python.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000167It raises KeyboardInterrupt.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000168
Thomas Wouters0796b002000-07-22 23:49:30 +0000169
170static int
171checksignals_witharg(void * unused)
172{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000173 return PyErr_CheckSignals();
Thomas Wouters0796b002000-07-22 23:49:30 +0000174}
175
Tim Peters4f1b2082000-07-23 21:18:09 +0000176static void
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200177trip_signal(int sig_num)
178{
179 Handlers[sig_num].tripped = 1;
180 if (is_tripped)
181 return;
182 /* Set is_tripped after setting .tripped, as it gets
183 cleared in PyErr_CheckSignals() before .tripped. */
184 is_tripped = 1;
185 Py_AddPendingCall(checksignals_witharg, NULL);
186 if (wakeup_fd != -1)
187 write(wakeup_fd, "\0", 1);
188}
189
190static void
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000191signal_handler(int sig_num)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000192{
Antoine Pitrou39a65912010-11-05 19:47:27 +0000193 int save_errno = errno;
194
195#if defined(WITH_THREAD) && defined(WITH_PTH)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000196 if (PyThread_get_thread_ident() != main_thread) {
197 pth_raise(*(pth_t *) main_thread, sig_num);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000198 }
Antoine Pitrou39a65912010-11-05 19:47:27 +0000199 else
Guido van Rossum9e8181b2000-09-19 00:46:46 +0000200#endif
Antoine Pitrou39a65912010-11-05 19:47:27 +0000201 {
202#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000203 /* See NOTES section above */
Antoine Pitrou39a65912010-11-05 19:47:27 +0000204 if (getpid() == main_pid)
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000205#endif
Antoine Pitrou39a65912010-11-05 19:47:27 +0000206 {
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200207 trip_signal(sig_num);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000208 }
Antoine Pitrou39a65912010-11-05 19:47:27 +0000209
Jean-Paul Calderone6f137ca2010-05-09 03:18:57 +0000210#ifndef HAVE_SIGACTION
Antoine Pitrou39a65912010-11-05 19:47:27 +0000211#ifdef SIGCHLD
212 /* To avoid infinite recursion, this signal remains
213 reset until explicit re-instated.
214 Don't clear the 'func' field as it is our pointer
215 to the Python handler... */
216 if (sig_num != SIGCHLD)
217#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000218 /* If the handler was not set up with sigaction, reinstall it. See
219 * Python/pythonrun.c for the implementation of PyOS_setsig which
220 * makes this true. See also issue8354. */
221 PyOS_setsig(sig_num, signal_handler);
Jean-Paul Calderone6f137ca2010-05-09 03:18:57 +0000222#endif
Antoine Pitrou39a65912010-11-05 19:47:27 +0000223 }
224
225 /* Issue #10311: asynchronously executing signal handlers should not
226 mutate errno under the feet of unsuspecting C code. */
227 errno = save_errno;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000228}
Guido van Rossume4485b01994-09-07 14:32:49 +0000229
Guido van Rossum06d511d1995-03-10 15:13:48 +0000230
Guido van Rossum1171ee61997-08-22 20:42:00 +0000231#ifdef HAVE_ALARM
Guido van Rossume4485b01994-09-07 14:32:49 +0000232static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000233signal_alarm(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000234{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000235 int t;
236 if (!PyArg_ParseTuple(args, "i:alarm", &t))
237 return NULL;
238 /* alarm() returns the number of seconds remaining */
239 return PyLong_FromLong((long)alarm(t));
Guido van Rossumaa0f4c71994-08-23 13:49:37 +0000240}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000241
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000242PyDoc_STRVAR(alarm_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000243"alarm(seconds)\n\
244\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000245Arrange for SIGALRM to arrive after the given number of seconds.");
Guido van Rossum06d511d1995-03-10 15:13:48 +0000246#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000247
Guido van Rossum1171ee61997-08-22 20:42:00 +0000248#ifdef HAVE_PAUSE
Guido van Rossuma597dde1995-01-10 20:56:29 +0000249static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000250signal_pause(PyObject *self)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000251{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000252 Py_BEGIN_ALLOW_THREADS
253 (void)pause();
254 Py_END_ALLOW_THREADS
255 /* make sure that any exceptions that got raised are propagated
256 * back into Python
257 */
258 if (PyErr_CheckSignals())
259 return NULL;
Barry Warsaw92971171997-01-03 00:14:25 +0000260
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000261 Py_INCREF(Py_None);
262 return Py_None;
Guido van Rossume4485b01994-09-07 14:32:49 +0000263}
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000264PyDoc_STRVAR(pause_doc,
Barry Warsaw1ee36ff1998-07-21 22:41:18 +0000265"pause()\n\
266\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000267Wait until a signal arrives.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000268
Guido van Rossum06d511d1995-03-10 15:13:48 +0000269#endif
Guido van Rossume4485b01994-09-07 14:32:49 +0000270
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000271
Guido van Rossume4485b01994-09-07 14:32:49 +0000272static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000273signal_signal(PyObject *self, PyObject *args)
Guido van Rossume4485b01994-09-07 14:32:49 +0000274{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000275 PyObject *obj;
276 int sig_num;
277 PyObject *old_handler;
278 void (*func)(int);
279 if (!PyArg_ParseTuple(args, "iO:signal", &sig_num, &obj))
280 return NULL;
Brian Curtinef9efbd2010-08-06 19:27:32 +0000281#ifdef MS_WINDOWS
282 /* Validate that sig_num is one of the allowable signals */
Brian Curtinc734b312010-09-06 16:04:10 +0000283 switch (sig_num) {
284 case SIGABRT: break;
Brian Curtin9e88b5a2010-10-01 14:49:24 +0000285#ifdef SIGBREAK
286 /* Issue #10003: SIGBREAK is not documented as permitted, but works
287 and corresponds to CTRL_BREAK_EVENT. */
288 case SIGBREAK: break;
289#endif
Brian Curtinc734b312010-09-06 16:04:10 +0000290 case SIGFPE: break;
291 case SIGILL: break;
292 case SIGINT: break;
293 case SIGSEGV: break;
294 case SIGTERM: break;
295 default:
296 PyErr_SetString(PyExc_ValueError, "invalid signal value");
297 return NULL;
Brian Curtinef9efbd2010-08-06 19:27:32 +0000298 }
299#endif
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000300#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000301 if (PyThread_get_thread_ident() != main_thread) {
302 PyErr_SetString(PyExc_ValueError,
303 "signal only works in main thread");
304 return NULL;
305 }
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000306#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000307 if (sig_num < 1 || sig_num >= NSIG) {
308 PyErr_SetString(PyExc_ValueError,
309 "signal number out of range");
310 return NULL;
311 }
312 if (obj == IgnoreHandler)
313 func = SIG_IGN;
314 else if (obj == DefaultHandler)
315 func = SIG_DFL;
316 else if (!PyCallable_Check(obj)) {
317 PyErr_SetString(PyExc_TypeError,
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000318"signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000319 return NULL;
320 }
321 else
322 func = signal_handler;
323 if (PyOS_setsig(sig_num, func) == SIG_ERR) {
324 PyErr_SetFromErrno(PyExc_RuntimeError);
325 return NULL;
326 }
327 old_handler = Handlers[sig_num].func;
328 Handlers[sig_num].tripped = 0;
329 Py_INCREF(obj);
330 Handlers[sig_num].func = obj;
331 return old_handler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000332}
333
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000334PyDoc_STRVAR(signal_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000335"signal(sig, action) -> action\n\
336\n\
337Set the action for the given signal. The action can be SIG_DFL,\n\
338SIG_IGN, or a callable Python object. The previous action is\n\
339returned. See getsignal() for possible return values.\n\
340\n\
341*** IMPORTANT NOTICE ***\n\
342A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000343the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000344
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000345
Guido van Rossume4485b01994-09-07 14:32:49 +0000346static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000347signal_getsignal(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000348{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000349 int sig_num;
350 PyObject *old_handler;
351 if (!PyArg_ParseTuple(args, "i:getsignal", &sig_num))
352 return NULL;
353 if (sig_num < 1 || sig_num >= NSIG) {
354 PyErr_SetString(PyExc_ValueError,
355 "signal number out of range");
356 return NULL;
357 }
358 old_handler = Handlers[sig_num].func;
359 Py_INCREF(old_handler);
360 return old_handler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000361}
362
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000363PyDoc_STRVAR(getsignal_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000364"getsignal(sig) -> action\n\
365\n\
366Return the current action for the given signal. The return value can be:\n\
367SIG_IGN -- if the signal is being ignored\n\
368SIG_DFL -- if the default action for the signal is in effect\n\
369None -- if an unknown handler is in effect\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000370anything else -- the callable Python object used as a handler");
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000371
Christian Heimes8640e742008-02-23 16:23:06 +0000372#ifdef HAVE_SIGINTERRUPT
373PyDoc_STRVAR(siginterrupt_doc,
374"siginterrupt(sig, flag) -> None\n\
375change system call restart behaviour: if flag is False, system calls\n\
376will be restarted when interrupted by signal sig, else system calls\n\
377will be interrupted.");
378
379static PyObject *
380signal_siginterrupt(PyObject *self, PyObject *args)
381{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000382 int sig_num;
383 int flag;
Christian Heimes8640e742008-02-23 16:23:06 +0000384
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000385 if (!PyArg_ParseTuple(args, "ii:siginterrupt", &sig_num, &flag))
386 return NULL;
387 if (sig_num < 1 || sig_num >= NSIG) {
388 PyErr_SetString(PyExc_ValueError,
389 "signal number out of range");
390 return NULL;
391 }
392 if (siginterrupt(sig_num, flag)<0) {
393 PyErr_SetFromErrno(PyExc_RuntimeError);
394 return NULL;
395 }
Christian Heimes8640e742008-02-23 16:23:06 +0000396
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000397 Py_INCREF(Py_None);
398 return Py_None;
Christian Heimes8640e742008-02-23 16:23:06 +0000399}
400
401#endif
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000402
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000403static PyObject *
404signal_set_wakeup_fd(PyObject *self, PyObject *args)
405{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000406 struct stat buf;
407 int fd, old_fd;
408 if (!PyArg_ParseTuple(args, "i:set_wakeup_fd", &fd))
409 return NULL;
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000410#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000411 if (PyThread_get_thread_ident() != main_thread) {
412 PyErr_SetString(PyExc_ValueError,
413 "set_wakeup_fd only works in main thread");
414 return NULL;
415 }
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000416#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000417 if (fd != -1 && fstat(fd, &buf) != 0) {
418 PyErr_SetString(PyExc_ValueError, "invalid fd");
419 return NULL;
420 }
421 old_fd = wakeup_fd;
422 wakeup_fd = fd;
423 return PyLong_FromLong(old_fd);
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000424}
425
426PyDoc_STRVAR(set_wakeup_fd_doc,
427"set_wakeup_fd(fd) -> fd\n\
428\n\
429Sets the fd to be written to (with '\\0') when a signal\n\
430comes in. A library can use this to wakeup select or poll.\n\
431The previous fd is returned.\n\
432\n\
433The fd must be non-blocking.");
434
435/* C API for the same, without all the error checking */
436int
437PySignal_SetWakeupFd(int fd)
438{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000439 int old_fd = wakeup_fd;
440 if (fd < 0)
441 fd = -1;
442 wakeup_fd = fd;
443 return old_fd;
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000444}
445
446
Martin v. Löwis823725e2008-03-24 13:39:54 +0000447#ifdef HAVE_SETITIMER
448static PyObject *
449signal_setitimer(PyObject *self, PyObject *args)
450{
451 double first;
452 double interval = 0;
453 int which;
454 struct itimerval new, old;
455
456 if(!PyArg_ParseTuple(args, "id|d:setitimer", &which, &first, &interval))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000457 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000458
459 timeval_from_double(first, &new.it_value);
460 timeval_from_double(interval, &new.it_interval);
461 /* Let OS check "which" value */
462 if (setitimer(which, &new, &old) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000463 PyErr_SetFromErrno(ItimerError);
464 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000465 }
466
467 return itimer_retval(&old);
468}
469
470PyDoc_STRVAR(setitimer_doc,
471"setitimer(which, seconds[, interval])\n\
472\n\
473Sets given itimer (one of ITIMER_REAL, ITIMER_VIRTUAL\n\
474or ITIMER_PROF) to fire after value seconds and after\n\
475that every interval seconds.\n\
476The itimer can be cleared by setting seconds to zero.\n\
477\n\
478Returns old values as a tuple: (delay, interval).");
479#endif
480
481
482#ifdef HAVE_GETITIMER
483static PyObject *
484signal_getitimer(PyObject *self, PyObject *args)
485{
486 int which;
487 struct itimerval old;
488
489 if (!PyArg_ParseTuple(args, "i:getitimer", &which))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000490 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000491
492 if (getitimer(which, &old) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000493 PyErr_SetFromErrno(ItimerError);
494 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000495 }
496
497 return itimer_retval(&old);
498}
499
500PyDoc_STRVAR(getitimer_doc,
501"getitimer(which)\n\
502\n\
503Returns current value of given itimer.");
504#endif
505
Victor Stinnera9293352011-04-30 15:21:58 +0200506#ifdef PYPTHREAD_SIGMASK
507/* Convert an iterable to a sigset.
508 Return 0 on success, return -1 and raise an exception on error. */
509
510static int
511iterable_to_sigset(PyObject *iterable, sigset_t *mask)
512{
513 int result = -1;
514 PyObject *iterator, *item;
515 long signum;
516 int err;
517
518 sigemptyset(mask);
519
520 iterator = PyObject_GetIter(iterable);
521 if (iterator == NULL)
522 goto error;
523
524 while (1)
525 {
526 item = PyIter_Next(iterator);
527 if (item == NULL) {
528 if (PyErr_Occurred())
529 goto error;
530 else
531 break;
532 }
533
534 signum = PyLong_AsLong(item);
535 Py_DECREF(item);
536 if (signum == -1 && PyErr_Occurred())
537 goto error;
538 if (0 < signum && signum < NSIG)
539 err = sigaddset(mask, (int)signum);
540 else
541 err = 1;
542 if (err) {
543 PyErr_Format(PyExc_ValueError,
544 "signal number %ld out of range", signum);
545 goto error;
546 }
547 }
548 result = 0;
549
550error:
551 Py_XDECREF(iterator);
552 return result;
553}
554
Victor Stinner35b300c2011-05-04 13:20:35 +0200555static PyObject*
556sigset_to_set(sigset_t mask)
557{
558 PyObject *signum, *result;
559 int sig;
560
561 result = PySet_New(0);
562 if (result == NULL)
563 return NULL;
564
565 for (sig = 1; sig < NSIG; sig++) {
566 if (sigismember(&mask, sig) != 1)
567 continue;
568
569 /* Handle the case where it is a member by adding the signal to
570 the result list. Ignore the other cases because they mean the
571 signal isn't a member of the mask or the signal was invalid,
572 and an invalid signal must have been our fault in constructing
573 the loop boundaries. */
574 signum = PyLong_FromLong(sig);
575 if (signum == NULL) {
576 Py_DECREF(result);
577 return NULL;
578 }
579 if (PySet_Add(result, signum) == -1) {
580 Py_DECREF(signum);
581 Py_DECREF(result);
582 return NULL;
583 }
584 Py_DECREF(signum);
585 }
586 return result;
587}
588
Victor Stinnera9293352011-04-30 15:21:58 +0200589static PyObject *
590signal_pthread_sigmask(PyObject *self, PyObject *args)
591{
Victor Stinner35b300c2011-05-04 13:20:35 +0200592 int how;
593 PyObject *signals;
Victor Stinnera9293352011-04-30 15:21:58 +0200594 sigset_t mask, previous;
595 int err;
596
597 if (!PyArg_ParseTuple(args, "iO:pthread_sigmask", &how, &signals))
598 return NULL;
599
600 if (iterable_to_sigset(signals, &mask))
601 return NULL;
602
603 err = pthread_sigmask(how, &mask, &previous);
604 if (err != 0) {
605 errno = err;
606 PyErr_SetFromErrno(PyExc_RuntimeError);
607 return NULL;
608 }
609
Victor Stinnerd0e516d2011-05-03 14:57:12 +0200610 /* if signals was unblocked, signal handlers have been called */
611 if (PyErr_CheckSignals())
612 return NULL;
613
Victor Stinner35b300c2011-05-04 13:20:35 +0200614 return sigset_to_set(previous);
Victor Stinnera9293352011-04-30 15:21:58 +0200615}
616
617PyDoc_STRVAR(signal_pthread_sigmask_doc,
618"pthread_sigmask(how, mask) -> old mask\n\
619\n\
620Fetch and/or change the signal mask of the calling thread.");
621#endif /* #ifdef PYPTHREAD_SIGMASK */
622
Martin v. Löwis823725e2008-03-24 13:39:54 +0000623
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000624/* List of functions defined in the module */
Barry Warsaw92971171997-01-03 00:14:25 +0000625static PyMethodDef signal_methods[] = {
Guido van Rossum1171ee61997-08-22 20:42:00 +0000626#ifdef HAVE_ALARM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000627 {"alarm", signal_alarm, METH_VARARGS, alarm_doc},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000628#endif
Martin v. Löwis823725e2008-03-24 13:39:54 +0000629#ifdef HAVE_SETITIMER
630 {"setitimer", signal_setitimer, METH_VARARGS, setitimer_doc},
631#endif
632#ifdef HAVE_GETITIMER
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000633 {"getitimer", signal_getitimer, METH_VARARGS, getitimer_doc},
Martin v. Löwis823725e2008-03-24 13:39:54 +0000634#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000635 {"signal", signal_signal, METH_VARARGS, signal_doc},
636 {"getsignal", signal_getsignal, METH_VARARGS, getsignal_doc},
637 {"set_wakeup_fd", signal_set_wakeup_fd, METH_VARARGS, set_wakeup_fd_doc},
Christian Heimes8640e742008-02-23 16:23:06 +0000638#ifdef HAVE_SIGINTERRUPT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000639 {"siginterrupt", signal_siginterrupt, METH_VARARGS, siginterrupt_doc},
Christian Heimes8640e742008-02-23 16:23:06 +0000640#endif
Guido van Rossum1171ee61997-08-22 20:42:00 +0000641#ifdef HAVE_PAUSE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000642 {"pause", (PyCFunction)signal_pause,
Victor Stinnera9293352011-04-30 15:21:58 +0200643 METH_NOARGS, pause_doc},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000644#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000645 {"default_int_handler", signal_default_int_handler,
646 METH_VARARGS, default_int_handler_doc},
Victor Stinnera9293352011-04-30 15:21:58 +0200647#ifdef PYPTHREAD_SIGMASK
648 {"pthread_sigmask", (PyCFunction)signal_pthread_sigmask,
649 METH_VARARGS, signal_pthread_sigmask_doc},
650#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000651 {NULL, NULL} /* sentinel */
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000652};
653
Barry Warsaw92971171997-01-03 00:14:25 +0000654
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000655PyDoc_STRVAR(module_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000656"This module provides mechanisms to use signal handlers in Python.\n\
657\n\
658Functions:\n\
659\n\
660alarm() -- cause SIGALRM after a specified time [Unix only]\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +0000661setitimer() -- cause a signal (described below) after a specified\n\
662 float time and the timer may restart then [Unix only]\n\
663getitimer() -- get current value of timer [Unix only]\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000664signal() -- set the action for a given signal\n\
665getsignal() -- get the signal action for a given signal\n\
666pause() -- wait until a signal arrives [Unix only]\n\
667default_int_handler() -- default SIGINT handler\n\
668\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +0000669signal constants:\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000670SIG_DFL -- used to refer to the system default handler\n\
671SIG_IGN -- used to ignore the signal\n\
672NSIG -- number of defined signals\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000673SIGINT, SIGTERM, etc. -- signal numbers\n\
674\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +0000675itimer constants:\n\
676ITIMER_REAL -- decrements in real time, and delivers SIGALRM upon\n\
677 expiration\n\
678ITIMER_VIRTUAL -- decrements only when the process is executing,\n\
679 and delivers SIGVTALRM upon expiration\n\
680ITIMER_PROF -- decrements both when the process is executing and\n\
681 when the system is executing on behalf of the process.\n\
682 Coupled with ITIMER_VIRTUAL, this timer is usually\n\
683 used to profile the time spent by the application\n\
684 in user and kernel space. SIGPROF is delivered upon\n\
685 expiration.\n\
686\n\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000687*** IMPORTANT NOTICE ***\n\
688A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000689the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000690
Martin v. Löwis1a214512008-06-11 05:26:20 +0000691static struct PyModuleDef signalmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000692 PyModuleDef_HEAD_INIT,
693 "signal",
694 module_doc,
695 -1,
696 signal_methods,
697 NULL,
698 NULL,
699 NULL,
700 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000701};
702
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000703PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000704PyInit_signal(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000705{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000706 PyObject *m, *d, *x;
707 int i;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000708
709#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000710 main_thread = PyThread_get_thread_ident();
711 main_pid = getpid();
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000712#endif
713
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000714 /* Create the module and add the functions */
715 m = PyModule_Create(&signalmodule);
716 if (m == NULL)
717 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000718
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000719 /* Add some symbolic constants to the module */
720 d = PyModule_GetDict(m);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000721
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000722 x = DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL);
723 if (!x || PyDict_SetItemString(d, "SIG_DFL", x) < 0)
724 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000725
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000726 x = IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN);
727 if (!x || PyDict_SetItemString(d, "SIG_IGN", x) < 0)
728 goto finally;
Barry Warsaw92971171997-01-03 00:14:25 +0000729
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000730 x = PyLong_FromLong((long)NSIG);
731 if (!x || PyDict_SetItemString(d, "NSIG", x) < 0)
732 goto finally;
733 Py_DECREF(x);
Barry Warsaw92971171997-01-03 00:14:25 +0000734
Victor Stinnera9293352011-04-30 15:21:58 +0200735#ifdef SIG_BLOCK
Victor Stinner72c53b52011-05-02 16:15:43 +0200736 if (PyModule_AddIntMacro(m, SIG_BLOCK))
737 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +0200738#endif
Victor Stinnera9293352011-04-30 15:21:58 +0200739#ifdef SIG_UNBLOCK
Victor Stinner72c53b52011-05-02 16:15:43 +0200740 if (PyModule_AddIntMacro(m, SIG_UNBLOCK))
741 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +0200742#endif
Victor Stinnera9293352011-04-30 15:21:58 +0200743#ifdef SIG_SETMASK
Victor Stinner72c53b52011-05-02 16:15:43 +0200744 if (PyModule_AddIntMacro(m, SIG_SETMASK))
745 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +0200746#endif
747
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000748 x = IntHandler = PyDict_GetItemString(d, "default_int_handler");
749 if (!x)
750 goto finally;
751 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +0000752
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000753 Handlers[0].tripped = 0;
754 for (i = 1; i < NSIG; i++) {
755 void (*t)(int);
756 t = PyOS_getsig(i);
757 Handlers[i].tripped = 0;
758 if (t == SIG_DFL)
759 Handlers[i].func = DefaultHandler;
760 else if (t == SIG_IGN)
761 Handlers[i].func = IgnoreHandler;
762 else
763 Handlers[i].func = Py_None; /* None of our business */
764 Py_INCREF(Handlers[i].func);
765 }
766 if (Handlers[SIGINT].func == DefaultHandler) {
767 /* Install default int handler */
768 Py_INCREF(IntHandler);
769 Py_DECREF(Handlers[SIGINT].func);
770 Handlers[SIGINT].func = IntHandler;
771 old_siginthandler = PyOS_setsig(SIGINT, signal_handler);
772 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000773
774#ifdef SIGHUP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000775 x = PyLong_FromLong(SIGHUP);
776 PyDict_SetItemString(d, "SIGHUP", x);
777 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000778#endif
779#ifdef SIGINT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000780 x = PyLong_FromLong(SIGINT);
781 PyDict_SetItemString(d, "SIGINT", x);
782 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000783#endif
Tim Peters1ce3cf72001-10-01 17:58:40 +0000784#ifdef SIGBREAK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000785 x = PyLong_FromLong(SIGBREAK);
786 PyDict_SetItemString(d, "SIGBREAK", x);
787 Py_XDECREF(x);
Tim Peters1ce3cf72001-10-01 17:58:40 +0000788#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000789#ifdef SIGQUIT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000790 x = PyLong_FromLong(SIGQUIT);
791 PyDict_SetItemString(d, "SIGQUIT", x);
792 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000793#endif
794#ifdef SIGILL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000795 x = PyLong_FromLong(SIGILL);
796 PyDict_SetItemString(d, "SIGILL", x);
797 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000798#endif
799#ifdef SIGTRAP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000800 x = PyLong_FromLong(SIGTRAP);
801 PyDict_SetItemString(d, "SIGTRAP", x);
802 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000803#endif
804#ifdef SIGIOT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000805 x = PyLong_FromLong(SIGIOT);
806 PyDict_SetItemString(d, "SIGIOT", x);
807 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000808#endif
809#ifdef SIGABRT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000810 x = PyLong_FromLong(SIGABRT);
811 PyDict_SetItemString(d, "SIGABRT", x);
812 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000813#endif
814#ifdef SIGEMT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000815 x = PyLong_FromLong(SIGEMT);
816 PyDict_SetItemString(d, "SIGEMT", x);
817 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000818#endif
819#ifdef SIGFPE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000820 x = PyLong_FromLong(SIGFPE);
821 PyDict_SetItemString(d, "SIGFPE", x);
822 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000823#endif
824#ifdef SIGKILL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000825 x = PyLong_FromLong(SIGKILL);
826 PyDict_SetItemString(d, "SIGKILL", x);
827 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000828#endif
829#ifdef SIGBUS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000830 x = PyLong_FromLong(SIGBUS);
831 PyDict_SetItemString(d, "SIGBUS", x);
832 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000833#endif
834#ifdef SIGSEGV
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000835 x = PyLong_FromLong(SIGSEGV);
836 PyDict_SetItemString(d, "SIGSEGV", x);
837 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000838#endif
839#ifdef SIGSYS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000840 x = PyLong_FromLong(SIGSYS);
841 PyDict_SetItemString(d, "SIGSYS", x);
842 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000843#endif
844#ifdef SIGPIPE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000845 x = PyLong_FromLong(SIGPIPE);
846 PyDict_SetItemString(d, "SIGPIPE", x);
847 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000848#endif
849#ifdef SIGALRM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000850 x = PyLong_FromLong(SIGALRM);
851 PyDict_SetItemString(d, "SIGALRM", x);
852 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000853#endif
854#ifdef SIGTERM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000855 x = PyLong_FromLong(SIGTERM);
856 PyDict_SetItemString(d, "SIGTERM", x);
857 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000858#endif
859#ifdef SIGUSR1
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000860 x = PyLong_FromLong(SIGUSR1);
861 PyDict_SetItemString(d, "SIGUSR1", x);
862 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000863#endif
864#ifdef SIGUSR2
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000865 x = PyLong_FromLong(SIGUSR2);
866 PyDict_SetItemString(d, "SIGUSR2", x);
867 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000868#endif
869#ifdef SIGCLD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000870 x = PyLong_FromLong(SIGCLD);
871 PyDict_SetItemString(d, "SIGCLD", x);
872 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000873#endif
874#ifdef SIGCHLD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000875 x = PyLong_FromLong(SIGCHLD);
876 PyDict_SetItemString(d, "SIGCHLD", x);
877 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000878#endif
879#ifdef SIGPWR
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000880 x = PyLong_FromLong(SIGPWR);
881 PyDict_SetItemString(d, "SIGPWR", x);
882 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000883#endif
884#ifdef SIGIO
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000885 x = PyLong_FromLong(SIGIO);
886 PyDict_SetItemString(d, "SIGIO", x);
887 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000888#endif
889#ifdef SIGURG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000890 x = PyLong_FromLong(SIGURG);
891 PyDict_SetItemString(d, "SIGURG", x);
892 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000893#endif
894#ifdef SIGWINCH
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000895 x = PyLong_FromLong(SIGWINCH);
896 PyDict_SetItemString(d, "SIGWINCH", x);
897 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000898#endif
899#ifdef SIGPOLL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000900 x = PyLong_FromLong(SIGPOLL);
901 PyDict_SetItemString(d, "SIGPOLL", x);
902 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000903#endif
904#ifdef SIGSTOP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000905 x = PyLong_FromLong(SIGSTOP);
906 PyDict_SetItemString(d, "SIGSTOP", x);
907 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000908#endif
909#ifdef SIGTSTP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000910 x = PyLong_FromLong(SIGTSTP);
911 PyDict_SetItemString(d, "SIGTSTP", x);
912 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000913#endif
914#ifdef SIGCONT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000915 x = PyLong_FromLong(SIGCONT);
916 PyDict_SetItemString(d, "SIGCONT", x);
917 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000918#endif
919#ifdef SIGTTIN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000920 x = PyLong_FromLong(SIGTTIN);
921 PyDict_SetItemString(d, "SIGTTIN", x);
922 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000923#endif
924#ifdef SIGTTOU
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000925 x = PyLong_FromLong(SIGTTOU);
926 PyDict_SetItemString(d, "SIGTTOU", x);
927 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000928#endif
929#ifdef SIGVTALRM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000930 x = PyLong_FromLong(SIGVTALRM);
931 PyDict_SetItemString(d, "SIGVTALRM", x);
932 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000933#endif
934#ifdef SIGPROF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000935 x = PyLong_FromLong(SIGPROF);
936 PyDict_SetItemString(d, "SIGPROF", x);
937 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000938#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000939#ifdef SIGXCPU
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000940 x = PyLong_FromLong(SIGXCPU);
941 PyDict_SetItemString(d, "SIGXCPU", x);
942 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000943#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000944#ifdef SIGXFSZ
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000945 x = PyLong_FromLong(SIGXFSZ);
946 PyDict_SetItemString(d, "SIGXFSZ", x);
947 Py_XDECREF(x);
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000948#endif
Anthony Baxterf37f37d2003-07-31 10:35:29 +0000949#ifdef SIGRTMIN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000950 x = PyLong_FromLong(SIGRTMIN);
951 PyDict_SetItemString(d, "SIGRTMIN", x);
952 Py_XDECREF(x);
Anthony Baxterf37f37d2003-07-31 10:35:29 +0000953#endif
954#ifdef SIGRTMAX
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000955 x = PyLong_FromLong(SIGRTMAX);
956 PyDict_SetItemString(d, "SIGRTMAX", x);
957 Py_XDECREF(x);
Anthony Baxterf37f37d2003-07-31 10:35:29 +0000958#endif
Martin v. Löwis175af252002-01-12 11:43:25 +0000959#ifdef SIGINFO
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000960 x = PyLong_FromLong(SIGINFO);
961 PyDict_SetItemString(d, "SIGINFO", x);
962 Py_XDECREF(x);
Martin v. Löwis175af252002-01-12 11:43:25 +0000963#endif
Martin v. Löwis823725e2008-03-24 13:39:54 +0000964
965#ifdef ITIMER_REAL
966 x = PyLong_FromLong(ITIMER_REAL);
967 PyDict_SetItemString(d, "ITIMER_REAL", x);
968 Py_DECREF(x);
969#endif
970#ifdef ITIMER_VIRTUAL
971 x = PyLong_FromLong(ITIMER_VIRTUAL);
972 PyDict_SetItemString(d, "ITIMER_VIRTUAL", x);
973 Py_DECREF(x);
974#endif
975#ifdef ITIMER_PROF
976 x = PyLong_FromLong(ITIMER_PROF);
977 PyDict_SetItemString(d, "ITIMER_PROF", x);
978 Py_DECREF(x);
979#endif
980
981#if defined (HAVE_SETITIMER) || defined (HAVE_GETITIMER)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000982 ItimerError = PyErr_NewException("signal.ItimerError",
983 PyExc_IOError, NULL);
Neal Norwitz2f99b242008-08-24 05:48:10 +0000984 if (ItimerError != NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000985 PyDict_SetItemString(d, "ItimerError", ItimerError);
Martin v. Löwis823725e2008-03-24 13:39:54 +0000986#endif
987
Brian Curtineb24d742010-04-12 17:16:38 +0000988#ifdef CTRL_C_EVENT
989 x = PyLong_FromLong(CTRL_C_EVENT);
990 PyDict_SetItemString(d, "CTRL_C_EVENT", x);
991 Py_DECREF(x);
992#endif
993
994#ifdef CTRL_BREAK_EVENT
995 x = PyLong_FromLong(CTRL_BREAK_EVENT);
996 PyDict_SetItemString(d, "CTRL_BREAK_EVENT", x);
997 Py_DECREF(x);
998#endif
999
Martin v. Löwis1a214512008-06-11 05:26:20 +00001000 if (PyErr_Occurred()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001001 Py_DECREF(m);
1002 m = NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001003 }
Barry Warsaw92971171997-01-03 00:14:25 +00001004
Barry Warsaw92971171997-01-03 00:14:25 +00001005 finally:
Martin v. Löwis1a214512008-06-11 05:26:20 +00001006 return m;
Guido van Rossum08c16611997-08-02 03:01:42 +00001007}
1008
1009static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001010finisignal(void)
Guido van Rossum08c16611997-08-02 03:01:42 +00001011{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001012 int i;
1013 PyObject *func;
Guido van Rossum08c16611997-08-02 03:01:42 +00001014
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001015 PyOS_setsig(SIGINT, old_siginthandler);
1016 old_siginthandler = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +00001017
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001018 for (i = 1; i < NSIG; i++) {
1019 func = Handlers[i].func;
1020 Handlers[i].tripped = 0;
1021 Handlers[i].func = NULL;
1022 if (i != SIGINT && func != NULL && func != Py_None &&
1023 func != DefaultHandler && func != IgnoreHandler)
1024 PyOS_setsig(i, SIG_DFL);
1025 Py_XDECREF(func);
1026 }
Guido van Rossum08c16611997-08-02 03:01:42 +00001027
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001028 Py_XDECREF(IntHandler);
1029 IntHandler = NULL;
1030 Py_XDECREF(DefaultHandler);
1031 DefaultHandler = NULL;
1032 Py_XDECREF(IgnoreHandler);
1033 IgnoreHandler = NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001034}
1035
Barry Warsaw92971171997-01-03 00:14:25 +00001036
Barry Warsaw92971171997-01-03 00:14:25 +00001037/* Declared in pyerrors.h */
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001038int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001039PyErr_CheckSignals(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001040{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001041 int i;
1042 PyObject *f;
Barry Warsaw92971171997-01-03 00:14:25 +00001043
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001044 if (!is_tripped)
1045 return 0;
Christian Heimesb76922a2007-12-11 01:06:40 +00001046
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001047#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001048 if (PyThread_get_thread_ident() != main_thread)
1049 return 0;
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001050#endif
Christian Heimesb76922a2007-12-11 01:06:40 +00001051
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001052 /*
1053 * The is_tripped variable is meant to speed up the calls to
1054 * PyErr_CheckSignals (both directly or via pending calls) when no
1055 * signal has arrived. This variable is set to 1 when a signal arrives
1056 * and it is set to 0 here, when we know some signals arrived. This way
1057 * we can run the registered handlers with no signals blocked.
1058 *
1059 * NOTE: with this approach we can have a situation where is_tripped is
1060 * 1 but we have no more signals to handle (Handlers[i].tripped
1061 * is 0 for every signal i). This won't do us any harm (except
1062 * we're gonna spent some cycles for nothing). This happens when
1063 * we receive a signal i after we zero is_tripped and before we
1064 * check Handlers[i].tripped.
1065 */
1066 is_tripped = 0;
Christian Heimesb76922a2007-12-11 01:06:40 +00001067
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001068 if (!(f = (PyObject *)PyEval_GetFrame()))
1069 f = Py_None;
Christian Heimesb76922a2007-12-11 01:06:40 +00001070
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001071 for (i = 1; i < NSIG; i++) {
1072 if (Handlers[i].tripped) {
1073 PyObject *result = NULL;
1074 PyObject *arglist = Py_BuildValue("(iO)", i, f);
1075 Handlers[i].tripped = 0;
Barry Warsaw92971171997-01-03 00:14:25 +00001076
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001077 if (arglist) {
1078 result = PyEval_CallObject(Handlers[i].func,
1079 arglist);
1080 Py_DECREF(arglist);
1081 }
1082 if (!result)
1083 return -1;
Barry Warsaw92971171997-01-03 00:14:25 +00001084
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001085 Py_DECREF(result);
1086 }
1087 }
Christian Heimesb76922a2007-12-11 01:06:40 +00001088
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001089 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001090}
1091
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +00001092
Barry Warsaw92971171997-01-03 00:14:25 +00001093/* Replacements for intrcheck.c functionality
1094 * Declared in pyerrors.h
1095 */
1096void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001097PyErr_SetInterrupt(void)
Barry Warsaw92971171997-01-03 00:14:25 +00001098{
Victor Stinner6c9b35b2011-04-18 16:25:56 +02001099 trip_signal(SIGINT);
Barry Warsaw92971171997-01-03 00:14:25 +00001100}
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001101
1102void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001103PyOS_InitInterrupts(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001104{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001105 PyObject *m = PyInit_signal();
1106 if (m) {
Victor Stinner49d3f252010-10-17 01:24:53 +00001107 _PyImport_FixupBuiltin(m, "signal");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001108 Py_DECREF(m);
1109 }
Guido van Rossum08c16611997-08-02 03:01:42 +00001110}
1111
1112void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001113PyOS_FiniInterrupts(void)
Guido van Rossum08c16611997-08-02 03:01:42 +00001114{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001115 finisignal();
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001116}
1117
1118int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001119PyOS_InterruptOccurred(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001120{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001121 if (Handlers[SIGINT].tripped) {
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001122#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001123 if (PyThread_get_thread_ident() != main_thread)
1124 return 0;
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001125#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001126 Handlers[SIGINT].tripped = 0;
1127 return 1;
1128 }
1129 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001130}
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001131
1132void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001133PyOS_AfterFork(void)
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001134{
1135#ifdef WITH_THREAD
Antoine Pitrou0c759fe2011-04-27 19:28:05 +02001136 _PyGILState_Reinit();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001137 PyEval_ReInitThreads();
1138 main_thread = PyThread_get_thread_ident();
1139 main_pid = getpid();
1140 _PyImport_ReInitLock();
1141 PyThread_ReInitTLS();
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001142#endif
1143}