blob: 45a7dfad3bc5751488f681ab7d803b1f2e63a855 [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
Victor Stinner2ec6b172011-05-15 10:21:59 +020091static volatile struct {
92 sig_atomic_t tripped;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000093 PyObject *func;
Barry Warsaw92971171997-01-03 00:14:25 +000094} Handlers[NSIG];
Guido van Rossum398d9fe1994-05-11 08:59:13 +000095
Victor Stinner2ec6b172011-05-15 10:21:59 +020096static volatile sig_atomic_t wakeup_fd = -1;
Christian Heimes5fb7c2a2007-12-24 08:52:31 +000097
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{
Victor Stinnerd49b1f12011-05-08 02:03:15 +0200179 unsigned char byte;
Victor Stinnerc13ef662011-05-25 02:35:58 +0200180
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200181 Handlers[sig_num].tripped = 1;
Victor Stinnerc13ef662011-05-25 02:35:58 +0200182 if (wakeup_fd != -1) {
183 byte = (unsigned char)sig_num;
184 write(wakeup_fd, &byte, 1);
185 }
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200186 if (is_tripped)
187 return;
188 /* Set is_tripped after setting .tripped, as it gets
189 cleared in PyErr_CheckSignals() before .tripped. */
190 is_tripped = 1;
191 Py_AddPendingCall(checksignals_witharg, NULL);
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200192}
193
194static void
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000195signal_handler(int sig_num)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000196{
Antoine Pitrou39a65912010-11-05 19:47:27 +0000197 int save_errno = errno;
198
199#if defined(WITH_THREAD) && defined(WITH_PTH)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000200 if (PyThread_get_thread_ident() != main_thread) {
201 pth_raise(*(pth_t *) main_thread, sig_num);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000202 }
Antoine Pitrou39a65912010-11-05 19:47:27 +0000203 else
Guido van Rossum9e8181b2000-09-19 00:46:46 +0000204#endif
Antoine Pitrou39a65912010-11-05 19:47:27 +0000205 {
206#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000207 /* See NOTES section above */
Antoine Pitrou39a65912010-11-05 19:47:27 +0000208 if (getpid() == main_pid)
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000209#endif
Antoine Pitrou39a65912010-11-05 19:47:27 +0000210 {
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200211 trip_signal(sig_num);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000212 }
Antoine Pitrou39a65912010-11-05 19:47:27 +0000213
Jean-Paul Calderone6f137ca2010-05-09 03:18:57 +0000214#ifndef HAVE_SIGACTION
Antoine Pitrou39a65912010-11-05 19:47:27 +0000215#ifdef SIGCHLD
216 /* To avoid infinite recursion, this signal remains
217 reset until explicit re-instated.
218 Don't clear the 'func' field as it is our pointer
219 to the Python handler... */
220 if (sig_num != SIGCHLD)
221#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000222 /* If the handler was not set up with sigaction, reinstall it. See
223 * Python/pythonrun.c for the implementation of PyOS_setsig which
224 * makes this true. See also issue8354. */
225 PyOS_setsig(sig_num, signal_handler);
Jean-Paul Calderone6f137ca2010-05-09 03:18:57 +0000226#endif
Antoine Pitrou39a65912010-11-05 19:47:27 +0000227 }
228
229 /* Issue #10311: asynchronously executing signal handlers should not
230 mutate errno under the feet of unsuspecting C code. */
231 errno = save_errno;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000232}
Guido van Rossume4485b01994-09-07 14:32:49 +0000233
Guido van Rossum06d511d1995-03-10 15:13:48 +0000234
Guido van Rossum1171ee61997-08-22 20:42:00 +0000235#ifdef HAVE_ALARM
Guido van Rossume4485b01994-09-07 14:32:49 +0000236static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000237signal_alarm(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000238{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000239 int t;
240 if (!PyArg_ParseTuple(args, "i:alarm", &t))
241 return NULL;
242 /* alarm() returns the number of seconds remaining */
243 return PyLong_FromLong((long)alarm(t));
Guido van Rossumaa0f4c71994-08-23 13:49:37 +0000244}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000245
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000246PyDoc_STRVAR(alarm_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000247"alarm(seconds)\n\
248\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000249Arrange for SIGALRM to arrive after the given number of seconds.");
Guido van Rossum06d511d1995-03-10 15:13:48 +0000250#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000251
Guido van Rossum1171ee61997-08-22 20:42:00 +0000252#ifdef HAVE_PAUSE
Guido van Rossuma597dde1995-01-10 20:56:29 +0000253static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000254signal_pause(PyObject *self)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000255{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000256 Py_BEGIN_ALLOW_THREADS
257 (void)pause();
258 Py_END_ALLOW_THREADS
259 /* make sure that any exceptions that got raised are propagated
260 * back into Python
261 */
262 if (PyErr_CheckSignals())
263 return NULL;
Barry Warsaw92971171997-01-03 00:14:25 +0000264
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000265 Py_INCREF(Py_None);
266 return Py_None;
Guido van Rossume4485b01994-09-07 14:32:49 +0000267}
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000268PyDoc_STRVAR(pause_doc,
Barry Warsaw1ee36ff1998-07-21 22:41:18 +0000269"pause()\n\
270\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000271Wait until a signal arrives.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000272
Guido van Rossum06d511d1995-03-10 15:13:48 +0000273#endif
Guido van Rossume4485b01994-09-07 14:32:49 +0000274
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000275
Guido van Rossume4485b01994-09-07 14:32:49 +0000276static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000277signal_signal(PyObject *self, PyObject *args)
Guido van Rossume4485b01994-09-07 14:32:49 +0000278{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000279 PyObject *obj;
280 int sig_num;
281 PyObject *old_handler;
282 void (*func)(int);
283 if (!PyArg_ParseTuple(args, "iO:signal", &sig_num, &obj))
284 return NULL;
Brian Curtinef9efbd2010-08-06 19:27:32 +0000285#ifdef MS_WINDOWS
286 /* Validate that sig_num is one of the allowable signals */
Brian Curtinc734b312010-09-06 16:04:10 +0000287 switch (sig_num) {
288 case SIGABRT: break;
Brian Curtin9e88b5a2010-10-01 14:49:24 +0000289#ifdef SIGBREAK
290 /* Issue #10003: SIGBREAK is not documented as permitted, but works
291 and corresponds to CTRL_BREAK_EVENT. */
292 case SIGBREAK: break;
293#endif
Brian Curtinc734b312010-09-06 16:04:10 +0000294 case SIGFPE: break;
295 case SIGILL: break;
296 case SIGINT: break;
297 case SIGSEGV: break;
298 case SIGTERM: break;
299 default:
300 PyErr_SetString(PyExc_ValueError, "invalid signal value");
301 return NULL;
Brian Curtinef9efbd2010-08-06 19:27:32 +0000302 }
303#endif
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000304#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000305 if (PyThread_get_thread_ident() != main_thread) {
306 PyErr_SetString(PyExc_ValueError,
307 "signal only works in main thread");
308 return NULL;
309 }
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000310#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000311 if (sig_num < 1 || sig_num >= NSIG) {
312 PyErr_SetString(PyExc_ValueError,
313 "signal number out of range");
314 return NULL;
315 }
316 if (obj == IgnoreHandler)
317 func = SIG_IGN;
318 else if (obj == DefaultHandler)
319 func = SIG_DFL;
320 else if (!PyCallable_Check(obj)) {
321 PyErr_SetString(PyExc_TypeError,
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000322"signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000323 return NULL;
324 }
325 else
326 func = signal_handler;
327 if (PyOS_setsig(sig_num, func) == SIG_ERR) {
Victor Stinner388196e2011-05-10 17:13:00 +0200328 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000329 return NULL;
330 }
331 old_handler = Handlers[sig_num].func;
332 Handlers[sig_num].tripped = 0;
333 Py_INCREF(obj);
334 Handlers[sig_num].func = obj;
335 return old_handler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000336}
337
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000338PyDoc_STRVAR(signal_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000339"signal(sig, action) -> action\n\
340\n\
341Set the action for the given signal. The action can be SIG_DFL,\n\
342SIG_IGN, or a callable Python object. The previous action is\n\
343returned. See getsignal() for possible return values.\n\
344\n\
345*** IMPORTANT NOTICE ***\n\
346A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000347the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000348
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000349
Guido van Rossume4485b01994-09-07 14:32:49 +0000350static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000351signal_getsignal(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000352{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000353 int sig_num;
354 PyObject *old_handler;
355 if (!PyArg_ParseTuple(args, "i:getsignal", &sig_num))
356 return NULL;
357 if (sig_num < 1 || sig_num >= NSIG) {
358 PyErr_SetString(PyExc_ValueError,
359 "signal number out of range");
360 return NULL;
361 }
362 old_handler = Handlers[sig_num].func;
363 Py_INCREF(old_handler);
364 return old_handler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000365}
366
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000367PyDoc_STRVAR(getsignal_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000368"getsignal(sig) -> action\n\
369\n\
370Return the current action for the given signal. The return value can be:\n\
371SIG_IGN -- if the signal is being ignored\n\
372SIG_DFL -- if the default action for the signal is in effect\n\
373None -- if an unknown handler is in effect\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000374anything else -- the callable Python object used as a handler");
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000375
Christian Heimes8640e742008-02-23 16:23:06 +0000376#ifdef HAVE_SIGINTERRUPT
377PyDoc_STRVAR(siginterrupt_doc,
378"siginterrupt(sig, flag) -> None\n\
379change system call restart behaviour: if flag is False, system calls\n\
380will be restarted when interrupted by signal sig, else system calls\n\
381will be interrupted.");
382
383static PyObject *
384signal_siginterrupt(PyObject *self, PyObject *args)
385{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000386 int sig_num;
387 int flag;
Christian Heimes8640e742008-02-23 16:23:06 +0000388
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000389 if (!PyArg_ParseTuple(args, "ii:siginterrupt", &sig_num, &flag))
390 return NULL;
391 if (sig_num < 1 || sig_num >= NSIG) {
392 PyErr_SetString(PyExc_ValueError,
393 "signal number out of range");
394 return NULL;
395 }
396 if (siginterrupt(sig_num, flag)<0) {
Victor Stinner388196e2011-05-10 17:13:00 +0200397 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000398 return NULL;
399 }
Christian Heimes8640e742008-02-23 16:23:06 +0000400
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000401 Py_INCREF(Py_None);
402 return Py_None;
Christian Heimes8640e742008-02-23 16:23:06 +0000403}
404
405#endif
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000406
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000407static PyObject *
408signal_set_wakeup_fd(PyObject *self, PyObject *args)
409{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000410 struct stat buf;
411 int fd, old_fd;
412 if (!PyArg_ParseTuple(args, "i:set_wakeup_fd", &fd))
413 return NULL;
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000414#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000415 if (PyThread_get_thread_ident() != main_thread) {
416 PyErr_SetString(PyExc_ValueError,
417 "set_wakeup_fd only works in main thread");
418 return NULL;
419 }
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000420#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000421 if (fd != -1 && fstat(fd, &buf) != 0) {
422 PyErr_SetString(PyExc_ValueError, "invalid fd");
423 return NULL;
424 }
425 old_fd = wakeup_fd;
426 wakeup_fd = fd;
427 return PyLong_FromLong(old_fd);
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000428}
429
430PyDoc_STRVAR(set_wakeup_fd_doc,
431"set_wakeup_fd(fd) -> fd\n\
432\n\
433Sets the fd to be written to (with '\\0') when a signal\n\
434comes in. A library can use this to wakeup select or poll.\n\
435The previous fd is returned.\n\
436\n\
437The fd must be non-blocking.");
438
439/* C API for the same, without all the error checking */
440int
441PySignal_SetWakeupFd(int fd)
442{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000443 int old_fd = wakeup_fd;
444 if (fd < 0)
445 fd = -1;
446 wakeup_fd = fd;
447 return old_fd;
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000448}
449
450
Martin v. Löwis823725e2008-03-24 13:39:54 +0000451#ifdef HAVE_SETITIMER
452static PyObject *
453signal_setitimer(PyObject *self, PyObject *args)
454{
455 double first;
456 double interval = 0;
457 int which;
458 struct itimerval new, old;
459
460 if(!PyArg_ParseTuple(args, "id|d:setitimer", &which, &first, &interval))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000461 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000462
463 timeval_from_double(first, &new.it_value);
464 timeval_from_double(interval, &new.it_interval);
465 /* Let OS check "which" value */
466 if (setitimer(which, &new, &old) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000467 PyErr_SetFromErrno(ItimerError);
468 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000469 }
470
471 return itimer_retval(&old);
472}
473
474PyDoc_STRVAR(setitimer_doc,
475"setitimer(which, seconds[, interval])\n\
476\n\
477Sets given itimer (one of ITIMER_REAL, ITIMER_VIRTUAL\n\
478or ITIMER_PROF) to fire after value seconds and after\n\
479that every interval seconds.\n\
480The itimer can be cleared by setting seconds to zero.\n\
481\n\
482Returns old values as a tuple: (delay, interval).");
483#endif
484
485
486#ifdef HAVE_GETITIMER
487static PyObject *
488signal_getitimer(PyObject *self, PyObject *args)
489{
490 int which;
491 struct itimerval old;
492
493 if (!PyArg_ParseTuple(args, "i:getitimer", &which))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000494 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000495
496 if (getitimer(which, &old) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 PyErr_SetFromErrno(ItimerError);
498 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000499 }
500
501 return itimer_retval(&old);
502}
503
504PyDoc_STRVAR(getitimer_doc,
505"getitimer(which)\n\
506\n\
507Returns current value of given itimer.");
508#endif
509
Ross Lagerwallbc808222011-06-25 12:13:40 +0200510#if defined(PYPTHREAD_SIGMASK) || defined(HAVE_SIGWAIT) || \
511 defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
Victor Stinnera9293352011-04-30 15:21:58 +0200512/* Convert an iterable to a sigset.
513 Return 0 on success, return -1 and raise an exception on error. */
514
515static int
516iterable_to_sigset(PyObject *iterable, sigset_t *mask)
517{
518 int result = -1;
519 PyObject *iterator, *item;
520 long signum;
521 int err;
522
523 sigemptyset(mask);
524
525 iterator = PyObject_GetIter(iterable);
526 if (iterator == NULL)
527 goto error;
528
529 while (1)
530 {
531 item = PyIter_Next(iterator);
532 if (item == NULL) {
533 if (PyErr_Occurred())
534 goto error;
535 else
536 break;
537 }
538
539 signum = PyLong_AsLong(item);
540 Py_DECREF(item);
541 if (signum == -1 && PyErr_Occurred())
542 goto error;
543 if (0 < signum && signum < NSIG)
544 err = sigaddset(mask, (int)signum);
545 else
546 err = 1;
547 if (err) {
548 PyErr_Format(PyExc_ValueError,
549 "signal number %ld out of range", signum);
550 goto error;
551 }
552 }
553 result = 0;
554
555error:
556 Py_XDECREF(iterator);
557 return result;
558}
Victor Stinnerb3e72192011-05-08 01:46:11 +0200559#endif
Victor Stinnera9293352011-04-30 15:21:58 +0200560
Victor Stinnerb3e72192011-05-08 01:46:11 +0200561#if defined(PYPTHREAD_SIGMASK) || defined(HAVE_SIGPENDING)
Victor Stinner35b300c2011-05-04 13:20:35 +0200562static PyObject*
563sigset_to_set(sigset_t mask)
564{
565 PyObject *signum, *result;
566 int sig;
567
568 result = PySet_New(0);
569 if (result == NULL)
570 return NULL;
571
572 for (sig = 1; sig < NSIG; sig++) {
573 if (sigismember(&mask, sig) != 1)
574 continue;
575
576 /* Handle the case where it is a member by adding the signal to
577 the result list. Ignore the other cases because they mean the
578 signal isn't a member of the mask or the signal was invalid,
579 and an invalid signal must have been our fault in constructing
580 the loop boundaries. */
581 signum = PyLong_FromLong(sig);
582 if (signum == NULL) {
583 Py_DECREF(result);
584 return NULL;
585 }
586 if (PySet_Add(result, signum) == -1) {
587 Py_DECREF(signum);
588 Py_DECREF(result);
589 return NULL;
590 }
591 Py_DECREF(signum);
592 }
593 return result;
594}
Victor Stinnerb3e72192011-05-08 01:46:11 +0200595#endif
Victor Stinner35b300c2011-05-04 13:20:35 +0200596
Victor Stinnerb3e72192011-05-08 01:46:11 +0200597#ifdef PYPTHREAD_SIGMASK
Victor Stinnera9293352011-04-30 15:21:58 +0200598static PyObject *
599signal_pthread_sigmask(PyObject *self, PyObject *args)
600{
Victor Stinner35b300c2011-05-04 13:20:35 +0200601 int how;
602 PyObject *signals;
Victor Stinnera9293352011-04-30 15:21:58 +0200603 sigset_t mask, previous;
604 int err;
605
606 if (!PyArg_ParseTuple(args, "iO:pthread_sigmask", &how, &signals))
607 return NULL;
608
609 if (iterable_to_sigset(signals, &mask))
610 return NULL;
611
612 err = pthread_sigmask(how, &mask, &previous);
613 if (err != 0) {
614 errno = err;
Victor Stinnerb3e72192011-05-08 01:46:11 +0200615 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnera9293352011-04-30 15:21:58 +0200616 return NULL;
617 }
618
Victor Stinnerd0e516d2011-05-03 14:57:12 +0200619 /* if signals was unblocked, signal handlers have been called */
620 if (PyErr_CheckSignals())
621 return NULL;
622
Victor Stinner35b300c2011-05-04 13:20:35 +0200623 return sigset_to_set(previous);
Victor Stinnera9293352011-04-30 15:21:58 +0200624}
625
626PyDoc_STRVAR(signal_pthread_sigmask_doc,
627"pthread_sigmask(how, mask) -> old mask\n\
628\n\
629Fetch and/or change the signal mask of the calling thread.");
630#endif /* #ifdef PYPTHREAD_SIGMASK */
631
Martin v. Löwis823725e2008-03-24 13:39:54 +0000632
Victor Stinnerb3e72192011-05-08 01:46:11 +0200633#ifdef HAVE_SIGPENDING
634static PyObject *
635signal_sigpending(PyObject *self)
636{
637 int err;
638 sigset_t mask;
639 err = sigpending(&mask);
640 if (err)
641 return PyErr_SetFromErrno(PyExc_OSError);
642 return sigset_to_set(mask);
643}
644
645PyDoc_STRVAR(signal_sigpending_doc,
646"sigpending() -> list\n\
647\n\
648Examine pending signals.");
649#endif /* #ifdef HAVE_SIGPENDING */
650
651
652#ifdef HAVE_SIGWAIT
653static PyObject *
654signal_sigwait(PyObject *self, PyObject *args)
655{
656 PyObject *signals;
657 sigset_t set;
658 int err, signum;
659
660 if (!PyArg_ParseTuple(args, "O:sigwait", &signals))
661 return NULL;
662
663 if (iterable_to_sigset(signals, &set))
664 return NULL;
665
Victor Stinner10c30d62011-06-10 01:39:53 +0200666 Py_BEGIN_ALLOW_THREADS
Victor Stinnerb3e72192011-05-08 01:46:11 +0200667 err = sigwait(&set, &signum);
Victor Stinner10c30d62011-06-10 01:39:53 +0200668 Py_END_ALLOW_THREADS
Victor Stinnerb3e72192011-05-08 01:46:11 +0200669 if (err) {
670 errno = err;
671 return PyErr_SetFromErrno(PyExc_OSError);
672 }
673
674 return PyLong_FromLong(signum);
675}
676
677PyDoc_STRVAR(signal_sigwait_doc,
678"sigwait(sigset) -> signum\n\
679\n\
680Wait a signal.");
681#endif /* #ifdef HAVE_SIGPENDING */
682
Ross Lagerwallbc808222011-06-25 12:13:40 +0200683#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
684static int initialized;
685static PyStructSequence_Field struct_siginfo_fields[] = {
686 {"si_signo", "signal number"},
687 {"si_code", "signal code"},
688 {"si_errno", "errno associated with this signal"},
689 {"si_pid", "sending process ID"},
690 {"si_uid", "real user ID of sending process"},
691 {"si_status", "exit value or signal"},
692 {"si_band", "band event for SIGPOLL"},
693 {0}
694};
695
696PyDoc_STRVAR(struct_siginfo__doc__,
697"struct_siginfo: Result from sigwaitinfo or sigtimedwait.\n\n\
698This object may be accessed either as a tuple of\n\
699(si_signo, si_code, si_errno, si_pid, si_uid, si_status, si_band),\n\
700or via the attributes si_signo, si_code, and so on.");
701
702static PyStructSequence_Desc struct_siginfo_desc = {
703 "signal.struct_siginfo", /* name */
704 struct_siginfo__doc__, /* doc */
705 struct_siginfo_fields, /* fields */
706 7 /* n_in_sequence */
707};
708
709static PyTypeObject SiginfoType;
710
711static PyObject *
712fill_siginfo(siginfo_t *si)
713{
714 PyObject *result = PyStructSequence_New(&SiginfoType);
715 if (!result)
716 return NULL;
717
718 PyStructSequence_SET_ITEM(result, 0, PyLong_FromLong((long)(si->si_signo)));
719 PyStructSequence_SET_ITEM(result, 1, PyLong_FromLong((long)(si->si_code)));
720 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si->si_errno)));
721 PyStructSequence_SET_ITEM(result, 3, PyLong_FromPid(si->si_pid));
722 PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si->si_uid)));
723 PyStructSequence_SET_ITEM(result, 5,
724 PyLong_FromLong((long)(si->si_status)));
725 PyStructSequence_SET_ITEM(result, 6, PyLong_FromLong(si->si_band));
726 if (PyErr_Occurred()) {
727 Py_DECREF(result);
728 return NULL;
729 }
730
731 return result;
732}
733#endif
734
735#ifdef HAVE_SIGWAITINFO
736static PyObject *
737signal_sigwaitinfo(PyObject *self, PyObject *args)
738{
739 PyObject *signals;
740 sigset_t set;
741 siginfo_t si;
742 int err;
743
744 if (!PyArg_ParseTuple(args, "O:sigwaitinfo", &signals))
745 return NULL;
746
747 if (iterable_to_sigset(signals, &set))
748 return NULL;
749
750 Py_BEGIN_ALLOW_THREADS
751 err = sigwaitinfo(&set, &si);
752 Py_END_ALLOW_THREADS
753 if (err == -1)
754 return PyErr_SetFromErrno(PyExc_OSError);
755
756 return fill_siginfo(&si);
757}
758
759PyDoc_STRVAR(signal_sigwaitinfo_doc,
760"sigwaitinfo(sigset) -> struct_siginfo\n\
761\n\
762Wait synchronously for a signal until one of the signals in *sigset* is\n\
763delivered.\n\
764Returns a struct_siginfo containing information about the signal.");
765#endif /* #ifdef HAVE_SIGWAITINFO */
766
767#ifdef HAVE_SIGTIMEDWAIT
768static PyObject *
769signal_sigtimedwait(PyObject *self, PyObject *args)
770{
771 PyObject *signals, *timeout;
772 struct timespec buf;
773 sigset_t set;
774 siginfo_t si;
775 int err;
776
777 if (!PyArg_ParseTuple(args, "OO:sigtimedwait", &signals, &timeout))
778 return NULL;
779
780 if (!PyTuple_Check(timeout) || PyTuple_Size(timeout) != 2) {
781 PyErr_SetString(PyExc_TypeError,
782 "sigtimedwait() arg 2 must be a tuple "
783 "(timeout_sec, timeout_nsec)");
784 return NULL;
785 } else if (!PyArg_ParseTuple(timeout, "ll:sigtimedwait",
786 &(buf.tv_sec), &(buf.tv_nsec)))
787 return NULL;
788
789 if (buf.tv_sec < 0 || buf.tv_nsec < 0) {
790 PyErr_SetString(PyExc_ValueError, "timeout must be non-negative");
791 return NULL;
792 }
793
794 if (iterable_to_sigset(signals, &set))
795 return NULL;
796
797 Py_BEGIN_ALLOW_THREADS
798 err = sigtimedwait(&set, &si, &buf);
799 Py_END_ALLOW_THREADS
800 if (err == -1) {
801 if (errno == EAGAIN)
802 Py_RETURN_NONE;
803 else
804 return PyErr_SetFromErrno(PyExc_OSError);
805 }
806
807 return fill_siginfo(&si);
808}
809
810PyDoc_STRVAR(signal_sigtimedwait_doc,
811"sigtimedwait(sigset, (timeout_sec, timeout_nsec)) -> struct_siginfo\n\
812\n\
813Like sigwaitinfo(), but with a timeout specified as a tuple of (seconds,\n\
814nanoseconds).");
815#endif /* #ifdef HAVE_SIGTIMEDWAIT */
816
Victor Stinnerb3e72192011-05-08 01:46:11 +0200817
818#if defined(HAVE_PTHREAD_KILL) && defined(WITH_THREAD)
819static PyObject *
820signal_pthread_kill(PyObject *self, PyObject *args)
821{
822 long tid;
823 int signum;
824 int err;
825
826 if (!PyArg_ParseTuple(args, "li:pthread_kill", &tid, &signum))
827 return NULL;
828
Victor Stinner86e104a2011-05-09 14:45:38 +0200829 err = pthread_kill((pthread_t)tid, signum);
Victor Stinnerb3e72192011-05-08 01:46:11 +0200830 if (err != 0) {
831 errno = err;
832 PyErr_SetFromErrno(PyExc_OSError);
833 return NULL;
834 }
835
836 /* the signal may have been send to the current thread */
837 if (PyErr_CheckSignals())
838 return NULL;
839
840 Py_RETURN_NONE;
841}
842
843PyDoc_STRVAR(signal_pthread_kill_doc,
844"pthread_kill(thread_id, signum)\n\
845\n\
846Send a signal to a thread.");
847#endif /* #if defined(HAVE_PTHREAD_KILL) && defined(WITH_THREAD) */
848
849
850
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000851/* List of functions defined in the module */
Barry Warsaw92971171997-01-03 00:14:25 +0000852static PyMethodDef signal_methods[] = {
Guido van Rossum1171ee61997-08-22 20:42:00 +0000853#ifdef HAVE_ALARM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000854 {"alarm", signal_alarm, METH_VARARGS, alarm_doc},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000855#endif
Martin v. Löwis823725e2008-03-24 13:39:54 +0000856#ifdef HAVE_SETITIMER
857 {"setitimer", signal_setitimer, METH_VARARGS, setitimer_doc},
858#endif
859#ifdef HAVE_GETITIMER
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000860 {"getitimer", signal_getitimer, METH_VARARGS, getitimer_doc},
Martin v. Löwis823725e2008-03-24 13:39:54 +0000861#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000862 {"signal", signal_signal, METH_VARARGS, signal_doc},
863 {"getsignal", signal_getsignal, METH_VARARGS, getsignal_doc},
864 {"set_wakeup_fd", signal_set_wakeup_fd, METH_VARARGS, set_wakeup_fd_doc},
Christian Heimes8640e742008-02-23 16:23:06 +0000865#ifdef HAVE_SIGINTERRUPT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000866 {"siginterrupt", signal_siginterrupt, METH_VARARGS, siginterrupt_doc},
Christian Heimes8640e742008-02-23 16:23:06 +0000867#endif
Guido van Rossum1171ee61997-08-22 20:42:00 +0000868#ifdef HAVE_PAUSE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000869 {"pause", (PyCFunction)signal_pause,
Victor Stinnera9293352011-04-30 15:21:58 +0200870 METH_NOARGS, pause_doc},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000871#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000872 {"default_int_handler", signal_default_int_handler,
873 METH_VARARGS, default_int_handler_doc},
Victor Stinnerb3e72192011-05-08 01:46:11 +0200874#if defined(HAVE_PTHREAD_KILL) && defined(WITH_THREAD)
875 {"pthread_kill", (PyCFunction)signal_pthread_kill,
876 METH_VARARGS, signal_pthread_kill_doc},
877#endif
Victor Stinnera9293352011-04-30 15:21:58 +0200878#ifdef PYPTHREAD_SIGMASK
879 {"pthread_sigmask", (PyCFunction)signal_pthread_sigmask,
880 METH_VARARGS, signal_pthread_sigmask_doc},
881#endif
Victor Stinnerb3e72192011-05-08 01:46:11 +0200882#ifdef HAVE_SIGPENDING
883 {"sigpending", (PyCFunction)signal_sigpending,
884 METH_NOARGS, signal_sigpending_doc},
885#endif
886#ifdef HAVE_SIGWAIT
887 {"sigwait", (PyCFunction)signal_sigwait,
888 METH_VARARGS, signal_sigwait_doc},
889#endif
Ross Lagerwallbc808222011-06-25 12:13:40 +0200890#ifdef HAVE_SIGWAITINFO
891 {"sigwaitinfo", (PyCFunction)signal_sigwaitinfo,
892 METH_VARARGS, signal_sigwaitinfo_doc},
893#endif
894#ifdef HAVE_SIGTIMEDWAIT
895 {"sigtimedwait", (PyCFunction)signal_sigtimedwait,
896 METH_VARARGS, signal_sigtimedwait_doc},
897#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000898 {NULL, NULL} /* sentinel */
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000899};
900
Barry Warsaw92971171997-01-03 00:14:25 +0000901
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000902PyDoc_STRVAR(module_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000903"This module provides mechanisms to use signal handlers in Python.\n\
904\n\
905Functions:\n\
906\n\
907alarm() -- cause SIGALRM after a specified time [Unix only]\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +0000908setitimer() -- cause a signal (described below) after a specified\n\
909 float time and the timer may restart then [Unix only]\n\
910getitimer() -- get current value of timer [Unix only]\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000911signal() -- set the action for a given signal\n\
912getsignal() -- get the signal action for a given signal\n\
913pause() -- wait until a signal arrives [Unix only]\n\
914default_int_handler() -- default SIGINT handler\n\
915\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +0000916signal constants:\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000917SIG_DFL -- used to refer to the system default handler\n\
918SIG_IGN -- used to ignore the signal\n\
919NSIG -- number of defined signals\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000920SIGINT, SIGTERM, etc. -- signal numbers\n\
921\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +0000922itimer constants:\n\
923ITIMER_REAL -- decrements in real time, and delivers SIGALRM upon\n\
924 expiration\n\
925ITIMER_VIRTUAL -- decrements only when the process is executing,\n\
926 and delivers SIGVTALRM upon expiration\n\
927ITIMER_PROF -- decrements both when the process is executing and\n\
928 when the system is executing on behalf of the process.\n\
929 Coupled with ITIMER_VIRTUAL, this timer is usually\n\
930 used to profile the time spent by the application\n\
931 in user and kernel space. SIGPROF is delivered upon\n\
932 expiration.\n\
933\n\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000934*** IMPORTANT NOTICE ***\n\
935A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000936the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000937
Martin v. Löwis1a214512008-06-11 05:26:20 +0000938static struct PyModuleDef signalmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000939 PyModuleDef_HEAD_INIT,
940 "signal",
941 module_doc,
942 -1,
943 signal_methods,
944 NULL,
945 NULL,
946 NULL,
947 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000948};
949
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000950PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000951PyInit_signal(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000952{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000953 PyObject *m, *d, *x;
954 int i;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000955
956#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000957 main_thread = PyThread_get_thread_ident();
958 main_pid = getpid();
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000959#endif
960
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000961 /* Create the module and add the functions */
962 m = PyModule_Create(&signalmodule);
963 if (m == NULL)
964 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000965
Ross Lagerwallbc808222011-06-25 12:13:40 +0200966#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
967 if (!initialized)
968 PyStructSequence_InitType(&SiginfoType, &struct_siginfo_desc);
969
970 Py_INCREF((PyObject*) &SiginfoType);
971 PyModule_AddObject(m, "struct_siginfo", (PyObject*) &SiginfoType);
972 initialized = 1;
973#endif
974
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000975 /* Add some symbolic constants to the module */
976 d = PyModule_GetDict(m);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000977
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000978 x = DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL);
979 if (!x || PyDict_SetItemString(d, "SIG_DFL", x) < 0)
980 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000981
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000982 x = IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN);
983 if (!x || PyDict_SetItemString(d, "SIG_IGN", x) < 0)
984 goto finally;
Barry Warsaw92971171997-01-03 00:14:25 +0000985
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000986 x = PyLong_FromLong((long)NSIG);
987 if (!x || PyDict_SetItemString(d, "NSIG", x) < 0)
988 goto finally;
989 Py_DECREF(x);
Barry Warsaw92971171997-01-03 00:14:25 +0000990
Victor Stinnera9293352011-04-30 15:21:58 +0200991#ifdef SIG_BLOCK
Victor Stinner72c53b52011-05-02 16:15:43 +0200992 if (PyModule_AddIntMacro(m, SIG_BLOCK))
993 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +0200994#endif
Victor Stinnera9293352011-04-30 15:21:58 +0200995#ifdef SIG_UNBLOCK
Victor Stinner72c53b52011-05-02 16:15:43 +0200996 if (PyModule_AddIntMacro(m, SIG_UNBLOCK))
997 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +0200998#endif
Victor Stinnera9293352011-04-30 15:21:58 +0200999#ifdef SIG_SETMASK
Victor Stinner72c53b52011-05-02 16:15:43 +02001000 if (PyModule_AddIntMacro(m, SIG_SETMASK))
1001 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001002#endif
1003
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001004 x = IntHandler = PyDict_GetItemString(d, "default_int_handler");
1005 if (!x)
1006 goto finally;
1007 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +00001008
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001009 Handlers[0].tripped = 0;
1010 for (i = 1; i < NSIG; i++) {
1011 void (*t)(int);
1012 t = PyOS_getsig(i);
1013 Handlers[i].tripped = 0;
1014 if (t == SIG_DFL)
1015 Handlers[i].func = DefaultHandler;
1016 else if (t == SIG_IGN)
1017 Handlers[i].func = IgnoreHandler;
1018 else
1019 Handlers[i].func = Py_None; /* None of our business */
1020 Py_INCREF(Handlers[i].func);
1021 }
1022 if (Handlers[SIGINT].func == DefaultHandler) {
1023 /* Install default int handler */
1024 Py_INCREF(IntHandler);
1025 Py_DECREF(Handlers[SIGINT].func);
1026 Handlers[SIGINT].func = IntHandler;
1027 old_siginthandler = PyOS_setsig(SIGINT, signal_handler);
1028 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001029
1030#ifdef SIGHUP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001031 x = PyLong_FromLong(SIGHUP);
1032 PyDict_SetItemString(d, "SIGHUP", x);
1033 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001034#endif
1035#ifdef SIGINT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001036 x = PyLong_FromLong(SIGINT);
1037 PyDict_SetItemString(d, "SIGINT", x);
1038 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001039#endif
Tim Peters1ce3cf72001-10-01 17:58:40 +00001040#ifdef SIGBREAK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001041 x = PyLong_FromLong(SIGBREAK);
1042 PyDict_SetItemString(d, "SIGBREAK", x);
1043 Py_XDECREF(x);
Tim Peters1ce3cf72001-10-01 17:58:40 +00001044#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001045#ifdef SIGQUIT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001046 x = PyLong_FromLong(SIGQUIT);
1047 PyDict_SetItemString(d, "SIGQUIT", x);
1048 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001049#endif
1050#ifdef SIGILL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001051 x = PyLong_FromLong(SIGILL);
1052 PyDict_SetItemString(d, "SIGILL", x);
1053 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001054#endif
1055#ifdef SIGTRAP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001056 x = PyLong_FromLong(SIGTRAP);
1057 PyDict_SetItemString(d, "SIGTRAP", x);
1058 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001059#endif
1060#ifdef SIGIOT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001061 x = PyLong_FromLong(SIGIOT);
1062 PyDict_SetItemString(d, "SIGIOT", x);
1063 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001064#endif
1065#ifdef SIGABRT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001066 x = PyLong_FromLong(SIGABRT);
1067 PyDict_SetItemString(d, "SIGABRT", x);
1068 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001069#endif
1070#ifdef SIGEMT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001071 x = PyLong_FromLong(SIGEMT);
1072 PyDict_SetItemString(d, "SIGEMT", x);
1073 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001074#endif
1075#ifdef SIGFPE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001076 x = PyLong_FromLong(SIGFPE);
1077 PyDict_SetItemString(d, "SIGFPE", x);
1078 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001079#endif
1080#ifdef SIGKILL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001081 x = PyLong_FromLong(SIGKILL);
1082 PyDict_SetItemString(d, "SIGKILL", x);
1083 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001084#endif
1085#ifdef SIGBUS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001086 x = PyLong_FromLong(SIGBUS);
1087 PyDict_SetItemString(d, "SIGBUS", x);
1088 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001089#endif
1090#ifdef SIGSEGV
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001091 x = PyLong_FromLong(SIGSEGV);
1092 PyDict_SetItemString(d, "SIGSEGV", x);
1093 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001094#endif
1095#ifdef SIGSYS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001096 x = PyLong_FromLong(SIGSYS);
1097 PyDict_SetItemString(d, "SIGSYS", x);
1098 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001099#endif
1100#ifdef SIGPIPE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001101 x = PyLong_FromLong(SIGPIPE);
1102 PyDict_SetItemString(d, "SIGPIPE", x);
1103 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001104#endif
1105#ifdef SIGALRM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001106 x = PyLong_FromLong(SIGALRM);
1107 PyDict_SetItemString(d, "SIGALRM", x);
1108 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001109#endif
1110#ifdef SIGTERM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001111 x = PyLong_FromLong(SIGTERM);
1112 PyDict_SetItemString(d, "SIGTERM", x);
1113 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001114#endif
1115#ifdef SIGUSR1
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001116 x = PyLong_FromLong(SIGUSR1);
1117 PyDict_SetItemString(d, "SIGUSR1", x);
1118 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001119#endif
1120#ifdef SIGUSR2
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001121 x = PyLong_FromLong(SIGUSR2);
1122 PyDict_SetItemString(d, "SIGUSR2", x);
1123 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001124#endif
1125#ifdef SIGCLD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001126 x = PyLong_FromLong(SIGCLD);
1127 PyDict_SetItemString(d, "SIGCLD", x);
1128 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001129#endif
1130#ifdef SIGCHLD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001131 x = PyLong_FromLong(SIGCHLD);
1132 PyDict_SetItemString(d, "SIGCHLD", x);
1133 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001134#endif
1135#ifdef SIGPWR
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001136 x = PyLong_FromLong(SIGPWR);
1137 PyDict_SetItemString(d, "SIGPWR", x);
1138 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001139#endif
1140#ifdef SIGIO
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001141 x = PyLong_FromLong(SIGIO);
1142 PyDict_SetItemString(d, "SIGIO", x);
1143 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001144#endif
1145#ifdef SIGURG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001146 x = PyLong_FromLong(SIGURG);
1147 PyDict_SetItemString(d, "SIGURG", x);
1148 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001149#endif
1150#ifdef SIGWINCH
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001151 x = PyLong_FromLong(SIGWINCH);
1152 PyDict_SetItemString(d, "SIGWINCH", x);
1153 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001154#endif
1155#ifdef SIGPOLL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001156 x = PyLong_FromLong(SIGPOLL);
1157 PyDict_SetItemString(d, "SIGPOLL", x);
1158 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001159#endif
1160#ifdef SIGSTOP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001161 x = PyLong_FromLong(SIGSTOP);
1162 PyDict_SetItemString(d, "SIGSTOP", x);
1163 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001164#endif
1165#ifdef SIGTSTP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001166 x = PyLong_FromLong(SIGTSTP);
1167 PyDict_SetItemString(d, "SIGTSTP", x);
1168 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001169#endif
1170#ifdef SIGCONT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001171 x = PyLong_FromLong(SIGCONT);
1172 PyDict_SetItemString(d, "SIGCONT", x);
1173 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001174#endif
1175#ifdef SIGTTIN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001176 x = PyLong_FromLong(SIGTTIN);
1177 PyDict_SetItemString(d, "SIGTTIN", x);
1178 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001179#endif
1180#ifdef SIGTTOU
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001181 x = PyLong_FromLong(SIGTTOU);
1182 PyDict_SetItemString(d, "SIGTTOU", x);
1183 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001184#endif
1185#ifdef SIGVTALRM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001186 x = PyLong_FromLong(SIGVTALRM);
1187 PyDict_SetItemString(d, "SIGVTALRM", x);
1188 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001189#endif
1190#ifdef SIGPROF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001191 x = PyLong_FromLong(SIGPROF);
1192 PyDict_SetItemString(d, "SIGPROF", x);
1193 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001194#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001195#ifdef SIGXCPU
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001196 x = PyLong_FromLong(SIGXCPU);
1197 PyDict_SetItemString(d, "SIGXCPU", x);
1198 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001199#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001200#ifdef SIGXFSZ
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001201 x = PyLong_FromLong(SIGXFSZ);
1202 PyDict_SetItemString(d, "SIGXFSZ", x);
1203 Py_XDECREF(x);
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001204#endif
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001205#ifdef SIGRTMIN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001206 x = PyLong_FromLong(SIGRTMIN);
1207 PyDict_SetItemString(d, "SIGRTMIN", x);
1208 Py_XDECREF(x);
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001209#endif
1210#ifdef SIGRTMAX
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001211 x = PyLong_FromLong(SIGRTMAX);
1212 PyDict_SetItemString(d, "SIGRTMAX", x);
1213 Py_XDECREF(x);
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001214#endif
Martin v. Löwis175af252002-01-12 11:43:25 +00001215#ifdef SIGINFO
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001216 x = PyLong_FromLong(SIGINFO);
1217 PyDict_SetItemString(d, "SIGINFO", x);
1218 Py_XDECREF(x);
Martin v. Löwis175af252002-01-12 11:43:25 +00001219#endif
Martin v. Löwis823725e2008-03-24 13:39:54 +00001220
1221#ifdef ITIMER_REAL
1222 x = PyLong_FromLong(ITIMER_REAL);
1223 PyDict_SetItemString(d, "ITIMER_REAL", x);
1224 Py_DECREF(x);
1225#endif
1226#ifdef ITIMER_VIRTUAL
1227 x = PyLong_FromLong(ITIMER_VIRTUAL);
1228 PyDict_SetItemString(d, "ITIMER_VIRTUAL", x);
1229 Py_DECREF(x);
1230#endif
1231#ifdef ITIMER_PROF
1232 x = PyLong_FromLong(ITIMER_PROF);
1233 PyDict_SetItemString(d, "ITIMER_PROF", x);
1234 Py_DECREF(x);
1235#endif
1236
1237#if defined (HAVE_SETITIMER) || defined (HAVE_GETITIMER)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001238 ItimerError = PyErr_NewException("signal.ItimerError",
1239 PyExc_IOError, NULL);
Neal Norwitz2f99b242008-08-24 05:48:10 +00001240 if (ItimerError != NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001241 PyDict_SetItemString(d, "ItimerError", ItimerError);
Martin v. Löwis823725e2008-03-24 13:39:54 +00001242#endif
1243
Brian Curtineb24d742010-04-12 17:16:38 +00001244#ifdef CTRL_C_EVENT
1245 x = PyLong_FromLong(CTRL_C_EVENT);
1246 PyDict_SetItemString(d, "CTRL_C_EVENT", x);
1247 Py_DECREF(x);
1248#endif
1249
1250#ifdef CTRL_BREAK_EVENT
1251 x = PyLong_FromLong(CTRL_BREAK_EVENT);
1252 PyDict_SetItemString(d, "CTRL_BREAK_EVENT", x);
1253 Py_DECREF(x);
1254#endif
1255
Martin v. Löwis1a214512008-06-11 05:26:20 +00001256 if (PyErr_Occurred()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001257 Py_DECREF(m);
1258 m = NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001259 }
Barry Warsaw92971171997-01-03 00:14:25 +00001260
Barry Warsaw92971171997-01-03 00:14:25 +00001261 finally:
Martin v. Löwis1a214512008-06-11 05:26:20 +00001262 return m;
Guido van Rossum08c16611997-08-02 03:01:42 +00001263}
1264
1265static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001266finisignal(void)
Guido van Rossum08c16611997-08-02 03:01:42 +00001267{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001268 int i;
1269 PyObject *func;
Guido van Rossum08c16611997-08-02 03:01:42 +00001270
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001271 PyOS_setsig(SIGINT, old_siginthandler);
1272 old_siginthandler = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +00001273
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001274 for (i = 1; i < NSIG; i++) {
1275 func = Handlers[i].func;
1276 Handlers[i].tripped = 0;
1277 Handlers[i].func = NULL;
1278 if (i != SIGINT && func != NULL && func != Py_None &&
1279 func != DefaultHandler && func != IgnoreHandler)
1280 PyOS_setsig(i, SIG_DFL);
1281 Py_XDECREF(func);
1282 }
Guido van Rossum08c16611997-08-02 03:01:42 +00001283
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001284 Py_XDECREF(IntHandler);
1285 IntHandler = NULL;
1286 Py_XDECREF(DefaultHandler);
1287 DefaultHandler = NULL;
1288 Py_XDECREF(IgnoreHandler);
1289 IgnoreHandler = NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001290}
1291
Barry Warsaw92971171997-01-03 00:14:25 +00001292
Barry Warsaw92971171997-01-03 00:14:25 +00001293/* Declared in pyerrors.h */
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001294int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001295PyErr_CheckSignals(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001296{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001297 int i;
1298 PyObject *f;
Barry Warsaw92971171997-01-03 00:14:25 +00001299
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001300 if (!is_tripped)
1301 return 0;
Christian Heimesb76922a2007-12-11 01:06:40 +00001302
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001303#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001304 if (PyThread_get_thread_ident() != main_thread)
1305 return 0;
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001306#endif
Christian Heimesb76922a2007-12-11 01:06:40 +00001307
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001308 /*
1309 * The is_tripped variable is meant to speed up the calls to
1310 * PyErr_CheckSignals (both directly or via pending calls) when no
1311 * signal has arrived. This variable is set to 1 when a signal arrives
1312 * and it is set to 0 here, when we know some signals arrived. This way
1313 * we can run the registered handlers with no signals blocked.
1314 *
1315 * NOTE: with this approach we can have a situation where is_tripped is
1316 * 1 but we have no more signals to handle (Handlers[i].tripped
1317 * is 0 for every signal i). This won't do us any harm (except
1318 * we're gonna spent some cycles for nothing). This happens when
1319 * we receive a signal i after we zero is_tripped and before we
1320 * check Handlers[i].tripped.
1321 */
1322 is_tripped = 0;
Christian Heimesb76922a2007-12-11 01:06:40 +00001323
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001324 if (!(f = (PyObject *)PyEval_GetFrame()))
1325 f = Py_None;
Christian Heimesb76922a2007-12-11 01:06:40 +00001326
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001327 for (i = 1; i < NSIG; i++) {
1328 if (Handlers[i].tripped) {
1329 PyObject *result = NULL;
1330 PyObject *arglist = Py_BuildValue("(iO)", i, f);
1331 Handlers[i].tripped = 0;
Barry Warsaw92971171997-01-03 00:14:25 +00001332
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001333 if (arglist) {
1334 result = PyEval_CallObject(Handlers[i].func,
1335 arglist);
1336 Py_DECREF(arglist);
1337 }
1338 if (!result)
1339 return -1;
Barry Warsaw92971171997-01-03 00:14:25 +00001340
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001341 Py_DECREF(result);
1342 }
1343 }
Christian Heimesb76922a2007-12-11 01:06:40 +00001344
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001345 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001346}
1347
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +00001348
Barry Warsaw92971171997-01-03 00:14:25 +00001349/* Replacements for intrcheck.c functionality
1350 * Declared in pyerrors.h
1351 */
1352void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001353PyErr_SetInterrupt(void)
Barry Warsaw92971171997-01-03 00:14:25 +00001354{
Victor Stinner6c9b35b2011-04-18 16:25:56 +02001355 trip_signal(SIGINT);
Barry Warsaw92971171997-01-03 00:14:25 +00001356}
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001357
1358void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001359PyOS_InitInterrupts(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001360{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001361 PyObject *m = PyInit_signal();
1362 if (m) {
Victor Stinner49d3f252010-10-17 01:24:53 +00001363 _PyImport_FixupBuiltin(m, "signal");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001364 Py_DECREF(m);
1365 }
Guido van Rossum08c16611997-08-02 03:01:42 +00001366}
1367
1368void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001369PyOS_FiniInterrupts(void)
Guido van Rossum08c16611997-08-02 03:01:42 +00001370{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001371 finisignal();
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001372}
1373
1374int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001375PyOS_InterruptOccurred(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001376{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001377 if (Handlers[SIGINT].tripped) {
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001378#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001379 if (PyThread_get_thread_ident() != main_thread)
1380 return 0;
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001381#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001382 Handlers[SIGINT].tripped = 0;
1383 return 1;
1384 }
1385 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001386}
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001387
1388void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001389PyOS_AfterFork(void)
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001390{
1391#ifdef WITH_THREAD
Antoine Pitrou0c759fe2011-04-27 19:28:05 +02001392 _PyGILState_Reinit();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001393 PyEval_ReInitThreads();
1394 main_thread = PyThread_get_thread_ident();
1395 main_pid = getpid();
1396 _PyImport_ReInitLock();
1397 PyThread_ReInitTLS();
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001398#endif
1399}