blob: 8b6c1c23886e94fc5b9f551b52f136da7e661994 [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"
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007#ifndef MS_WINDOWS
8#include "posixmodule.h"
9#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +000010
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000011#ifdef MS_WINDOWS
Antoine Pitrou7faf7052013-03-31 22:48:04 +020012#include <windows.h>
Benjamin Peterson2614cda2010-03-21 22:36:19 +000013#ifdef HAVE_PROCESS_H
Guido van Rossum644a12b1997-04-09 19:24:53 +000014#include <process.h>
15#endif
Benjamin Peterson2614cda2010-03-21 22:36:19 +000016#endif
Guido van Rossum644a12b1997-04-09 19:24:53 +000017
Benjamin Peterson2614cda2010-03-21 22:36:19 +000018#ifdef HAVE_SIGNAL_H
Guido van Rossum398d9fe1994-05-11 08:59:13 +000019#include <signal.h>
Benjamin Peterson2614cda2010-03-21 22:36:19 +000020#endif
21#ifdef HAVE_SYS_STAT_H
Christian Heimes5fb7c2a2007-12-24 08:52:31 +000022#include <sys/stat.h>
Benjamin Peterson2614cda2010-03-21 22:36:19 +000023#endif
Martin v. Löwis3bf3cc02008-03-24 14:05:07 +000024#ifdef HAVE_SYS_TIME_H
Martin v. Löwis823725e2008-03-24 13:39:54 +000025#include <sys/time.h>
Martin v. Löwis3bf3cc02008-03-24 14:05:07 +000026#endif
Christian Heimes5fb7c2a2007-12-24 08:52:31 +000027
Victor Stinnera9293352011-04-30 15:21:58 +020028#if defined(HAVE_PTHREAD_SIGMASK) && !defined(HAVE_BROKEN_PTHREAD_SIGMASK)
29# define PYPTHREAD_SIGMASK
30#endif
31
32#if defined(PYPTHREAD_SIGMASK) && defined(HAVE_PTHREAD_H)
33# include <pthread.h>
34#endif
35
Guido van Rossumbb4ba121994-06-23 11:25:45 +000036#ifndef SIG_ERR
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +000037#define SIG_ERR ((PyOS_sighandler_t)(-1))
Guido van Rossumbb4ba121994-06-23 11:25:45 +000038#endif
39
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000040#ifndef NSIG
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000041# if defined(_NSIG)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000042# define NSIG _NSIG /* For BSD/SysV */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000043# elif defined(_SIGMAX)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000044# define NSIG (_SIGMAX + 1) /* For QNX */
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 djgpp */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000047# else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000048# define NSIG 64 /* Use a reasonable default value */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000049# endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000050#endif
51
52
Guido van Rossumbb4ba121994-06-23 11:25:45 +000053/*
54 NOTES ON THE INTERACTION BETWEEN SIGNALS AND THREADS
55
56 When threads are supported, we want the following semantics:
57
58 - only the main thread can set a signal handler
59 - any thread can get a signal handler
60 - signals are only delivered to the main thread
61
62 I.e. we don't support "synchronous signals" like SIGFPE (catching
63 this doesn't make much sense in Python anyway) nor do we support
64 signals as a means of inter-thread communication, since not all
65 thread implementations support that (at least our thread library
66 doesn't).
67
68 We still have the problem that in some implementations signals
69 generated by the keyboard (e.g. SIGINT) are delivered to all
70 threads (e.g. SGI), while in others (e.g. Solaris) such signals are
71 delivered to one random thread (an intermediate possibility would
Guido van Rossuma3c04b01995-01-12 11:29:01 +000072 be to deliver it to the main thread -- POSIX?). For now, we have
Guido van Rossumbb4ba121994-06-23 11:25:45 +000073 a working implementation that works in all three cases -- the
74 handler ignores signals if getpid() isn't the same as in the main
75 thread. XXX This is a hack.
76
Guido van Rossum9e8181b2000-09-19 00:46:46 +000077 GNU pth is a user-space threading library, and as such, all threads
78 run within the same process. In this case, if the currently running
79 thread is not the main_thread, send the signal to the main_thread.
Guido van Rossumbb4ba121994-06-23 11:25:45 +000080*/
81
82#ifdef WITH_THREAD
Guido van Rossum295b8e51997-06-06 21:16:41 +000083#include <sys/types.h> /* For pid_t */
Guido van Rossum49b56061998-10-01 20:42:43 +000084#include "pythread.h"
Guido van Rossumbb4ba121994-06-23 11:25:45 +000085static long main_thread;
86static pid_t main_pid;
87#endif
88
Victor Stinner2ec6b172011-05-15 10:21:59 +020089static volatile struct {
90 sig_atomic_t tripped;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000091 PyObject *func;
Barry Warsaw92971171997-01-03 00:14:25 +000092} Handlers[NSIG];
Guido van Rossum398d9fe1994-05-11 08:59:13 +000093
Victor Stinner2ec6b172011-05-15 10:21:59 +020094static volatile sig_atomic_t wakeup_fd = -1;
Christian Heimes5fb7c2a2007-12-24 08:52:31 +000095
Christian Heimesb76922a2007-12-11 01:06:40 +000096/* Speed up sigcheck() when none tripped */
97static volatile sig_atomic_t is_tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +000098
Barry Warsaw92971171997-01-03 00:14:25 +000099static PyObject *DefaultHandler;
100static PyObject *IgnoreHandler;
101static PyObject *IntHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000102
Martin v. Löwisf58de1b2001-03-06 12:13:56 +0000103/* On Solaris 8, gcc will produce a warning that the function
104 declaration is not a prototype. This is caused by the definition of
105 SIG_DFL as (void (*)())0; the correct declaration would have been
106 (void (*)(int))0. */
107
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000108static PyOS_sighandler_t old_siginthandler = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +0000109
Antoine Pitrou6dd381e2011-11-21 21:26:56 +0100110#ifdef MS_WINDOWS
111static HANDLE sigint_event = NULL;
112#endif
113
Martin v. Löwis823725e2008-03-24 13:39:54 +0000114#ifdef HAVE_GETITIMER
115static PyObject *ItimerError;
116
117/* auxiliary functions for setitimer/getitimer */
118static void
119timeval_from_double(double d, struct timeval *tv)
120{
121 tv->tv_sec = floor(d);
122 tv->tv_usec = fmod(d, 1.0) * 1000000.0;
123}
124
Christian Heimes1a8501c2008-10-02 19:56:01 +0000125Py_LOCAL_INLINE(double)
Martin v. Löwis823725e2008-03-24 13:39:54 +0000126double_from_timeval(struct timeval *tv)
127{
128 return tv->tv_sec + (double)(tv->tv_usec / 1000000.0);
129}
130
131static PyObject *
132itimer_retval(struct itimerval *iv)
133{
134 PyObject *r, *v;
135
136 r = PyTuple_New(2);
137 if (r == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000138 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000139
140 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_value)))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000141 Py_DECREF(r);
142 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000143 }
144
145 PyTuple_SET_ITEM(r, 0, v);
146
147 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_interval)))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000148 Py_DECREF(r);
149 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000150 }
151
152 PyTuple_SET_ITEM(r, 1, v);
153
154 return r;
155}
156#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000157
Guido van Rossume4485b01994-09-07 14:32:49 +0000158static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000159signal_default_int_handler(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000160{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000161 PyErr_SetNone(PyExc_KeyboardInterrupt);
162 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000163}
164
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000165PyDoc_STRVAR(default_int_handler_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000166"default_int_handler(...)\n\
167\n\
Michael W. Hudson24ec2112004-06-17 15:55:53 +0000168The default handler for SIGINT installed by Python.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000169It raises KeyboardInterrupt.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000170
Thomas Wouters0796b002000-07-22 23:49:30 +0000171
172static int
173checksignals_witharg(void * unused)
174{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000175 return PyErr_CheckSignals();
Thomas Wouters0796b002000-07-22 23:49:30 +0000176}
177
Tim Peters4f1b2082000-07-23 21:18:09 +0000178static void
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200179trip_signal(int sig_num)
180{
Victor Stinnerd49b1f12011-05-08 02:03:15 +0200181 unsigned char byte;
Victor Stinnerc13ef662011-05-25 02:35:58 +0200182
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200183 Handlers[sig_num].tripped = 1;
Victor Stinnerc13ef662011-05-25 02:35:58 +0200184 if (wakeup_fd != -1) {
185 byte = (unsigned char)sig_num;
186 write(wakeup_fd, &byte, 1);
187 }
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200188 if (is_tripped)
189 return;
190 /* Set is_tripped after setting .tripped, as it gets
191 cleared in PyErr_CheckSignals() before .tripped. */
192 is_tripped = 1;
193 Py_AddPendingCall(checksignals_witharg, NULL);
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200194}
195
196static void
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000197signal_handler(int sig_num)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000198{
Antoine Pitrou39a65912010-11-05 19:47:27 +0000199 int save_errno = errno;
200
201#if defined(WITH_THREAD) && defined(WITH_PTH)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000202 if (PyThread_get_thread_ident() != main_thread) {
203 pth_raise(*(pth_t *) main_thread, sig_num);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000204 }
Antoine Pitrou39a65912010-11-05 19:47:27 +0000205 else
Guido van Rossum9e8181b2000-09-19 00:46:46 +0000206#endif
Antoine Pitrou39a65912010-11-05 19:47:27 +0000207 {
208#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000209 /* See NOTES section above */
Antoine Pitrou39a65912010-11-05 19:47:27 +0000210 if (getpid() == main_pid)
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000211#endif
Antoine Pitrou39a65912010-11-05 19:47:27 +0000212 {
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200213 trip_signal(sig_num);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000214 }
Antoine Pitrou39a65912010-11-05 19:47:27 +0000215
Jean-Paul Calderone6f137ca2010-05-09 03:18:57 +0000216#ifndef HAVE_SIGACTION
Antoine Pitrou39a65912010-11-05 19:47:27 +0000217#ifdef SIGCHLD
218 /* To avoid infinite recursion, this signal remains
219 reset until explicit re-instated.
220 Don't clear the 'func' field as it is our pointer
221 to the Python handler... */
222 if (sig_num != SIGCHLD)
223#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000224 /* If the handler was not set up with sigaction, reinstall it. See
225 * Python/pythonrun.c for the implementation of PyOS_setsig which
226 * makes this true. See also issue8354. */
227 PyOS_setsig(sig_num, signal_handler);
Jean-Paul Calderone6f137ca2010-05-09 03:18:57 +0000228#endif
Antoine Pitrou39a65912010-11-05 19:47:27 +0000229 }
230
231 /* Issue #10311: asynchronously executing signal handlers should not
232 mutate errno under the feet of unsuspecting C code. */
233 errno = save_errno;
Antoine Pitrou6dd381e2011-11-21 21:26:56 +0100234
235#ifdef MS_WINDOWS
236 if (sig_num == SIGINT)
237 SetEvent(sigint_event);
238#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000239}
Guido van Rossume4485b01994-09-07 14:32:49 +0000240
Guido van Rossum06d511d1995-03-10 15:13:48 +0000241
Guido van Rossum1171ee61997-08-22 20:42:00 +0000242#ifdef HAVE_ALARM
Guido van Rossume4485b01994-09-07 14:32:49 +0000243static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000244signal_alarm(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000245{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000246 int t;
247 if (!PyArg_ParseTuple(args, "i:alarm", &t))
248 return NULL;
249 /* alarm() returns the number of seconds remaining */
250 return PyLong_FromLong((long)alarm(t));
Guido van Rossumaa0f4c71994-08-23 13:49:37 +0000251}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000252
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000253PyDoc_STRVAR(alarm_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000254"alarm(seconds)\n\
255\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000256Arrange for SIGALRM to arrive after the given number of seconds.");
Guido van Rossum06d511d1995-03-10 15:13:48 +0000257#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000258
Guido van Rossum1171ee61997-08-22 20:42:00 +0000259#ifdef HAVE_PAUSE
Guido van Rossuma597dde1995-01-10 20:56:29 +0000260static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000261signal_pause(PyObject *self)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000262{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000263 Py_BEGIN_ALLOW_THREADS
264 (void)pause();
265 Py_END_ALLOW_THREADS
266 /* make sure that any exceptions that got raised are propagated
267 * back into Python
268 */
269 if (PyErr_CheckSignals())
270 return NULL;
Barry Warsaw92971171997-01-03 00:14:25 +0000271
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000272 Py_INCREF(Py_None);
273 return Py_None;
Guido van Rossume4485b01994-09-07 14:32:49 +0000274}
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000275PyDoc_STRVAR(pause_doc,
Barry Warsaw1ee36ff1998-07-21 22:41:18 +0000276"pause()\n\
277\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000278Wait until a signal arrives.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000279
Guido van Rossum06d511d1995-03-10 15:13:48 +0000280#endif
Guido van Rossume4485b01994-09-07 14:32:49 +0000281
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000282
Guido van Rossume4485b01994-09-07 14:32:49 +0000283static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000284signal_signal(PyObject *self, PyObject *args)
Guido van Rossume4485b01994-09-07 14:32:49 +0000285{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000286 PyObject *obj;
287 int sig_num;
288 PyObject *old_handler;
289 void (*func)(int);
290 if (!PyArg_ParseTuple(args, "iO:signal", &sig_num, &obj))
291 return NULL;
Brian Curtinef9efbd2010-08-06 19:27:32 +0000292#ifdef MS_WINDOWS
293 /* Validate that sig_num is one of the allowable signals */
Brian Curtinc734b312010-09-06 16:04:10 +0000294 switch (sig_num) {
295 case SIGABRT: break;
Brian Curtin9e88b5a2010-10-01 14:49:24 +0000296#ifdef SIGBREAK
297 /* Issue #10003: SIGBREAK is not documented as permitted, but works
298 and corresponds to CTRL_BREAK_EVENT. */
299 case SIGBREAK: break;
300#endif
Brian Curtinc734b312010-09-06 16:04:10 +0000301 case SIGFPE: break;
302 case SIGILL: break;
303 case SIGINT: break;
304 case SIGSEGV: break;
305 case SIGTERM: break;
306 default:
307 PyErr_SetString(PyExc_ValueError, "invalid signal value");
308 return NULL;
Brian Curtinef9efbd2010-08-06 19:27:32 +0000309 }
310#endif
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000311#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000312 if (PyThread_get_thread_ident() != main_thread) {
313 PyErr_SetString(PyExc_ValueError,
314 "signal only works in main thread");
315 return NULL;
316 }
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000317#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000318 if (sig_num < 1 || sig_num >= NSIG) {
319 PyErr_SetString(PyExc_ValueError,
320 "signal number out of range");
321 return NULL;
322 }
323 if (obj == IgnoreHandler)
324 func = SIG_IGN;
325 else if (obj == DefaultHandler)
326 func = SIG_DFL;
327 else if (!PyCallable_Check(obj)) {
328 PyErr_SetString(PyExc_TypeError,
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000329"signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000330 return NULL;
331 }
332 else
333 func = signal_handler;
334 if (PyOS_setsig(sig_num, func) == SIG_ERR) {
Victor Stinner388196e2011-05-10 17:13:00 +0200335 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000336 return NULL;
337 }
338 old_handler = Handlers[sig_num].func;
339 Handlers[sig_num].tripped = 0;
340 Py_INCREF(obj);
341 Handlers[sig_num].func = obj;
342 return old_handler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000343}
344
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000345PyDoc_STRVAR(signal_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000346"signal(sig, action) -> action\n\
347\n\
348Set the action for the given signal. The action can be SIG_DFL,\n\
349SIG_IGN, or a callable Python object. The previous action is\n\
350returned. See getsignal() for possible return values.\n\
351\n\
352*** IMPORTANT NOTICE ***\n\
353A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000354the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000355
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000356
Guido van Rossume4485b01994-09-07 14:32:49 +0000357static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000358signal_getsignal(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000359{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000360 int sig_num;
361 PyObject *old_handler;
362 if (!PyArg_ParseTuple(args, "i:getsignal", &sig_num))
363 return NULL;
364 if (sig_num < 1 || sig_num >= NSIG) {
365 PyErr_SetString(PyExc_ValueError,
366 "signal number out of range");
367 return NULL;
368 }
369 old_handler = Handlers[sig_num].func;
370 Py_INCREF(old_handler);
371 return old_handler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000372}
373
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000374PyDoc_STRVAR(getsignal_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000375"getsignal(sig) -> action\n\
376\n\
377Return the current action for the given signal. The return value can be:\n\
378SIG_IGN -- if the signal is being ignored\n\
379SIG_DFL -- if the default action for the signal is in effect\n\
380None -- if an unknown handler is in effect\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000381anything else -- the callable Python object used as a handler");
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000382
Christian Heimes8640e742008-02-23 16:23:06 +0000383#ifdef HAVE_SIGINTERRUPT
384PyDoc_STRVAR(siginterrupt_doc,
385"siginterrupt(sig, flag) -> None\n\
386change system call restart behaviour: if flag is False, system calls\n\
387will be restarted when interrupted by signal sig, else system calls\n\
388will be interrupted.");
389
390static PyObject *
391signal_siginterrupt(PyObject *self, PyObject *args)
392{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000393 int sig_num;
394 int flag;
Christian Heimes8640e742008-02-23 16:23:06 +0000395
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000396 if (!PyArg_ParseTuple(args, "ii:siginterrupt", &sig_num, &flag))
397 return NULL;
398 if (sig_num < 1 || sig_num >= NSIG) {
399 PyErr_SetString(PyExc_ValueError,
400 "signal number out of range");
401 return NULL;
402 }
403 if (siginterrupt(sig_num, flag)<0) {
Victor Stinner388196e2011-05-10 17:13:00 +0200404 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000405 return NULL;
406 }
Christian Heimes8640e742008-02-23 16:23:06 +0000407
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000408 Py_INCREF(Py_None);
409 return Py_None;
Christian Heimes8640e742008-02-23 16:23:06 +0000410}
411
412#endif
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000413
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000414static PyObject *
415signal_set_wakeup_fd(PyObject *self, PyObject *args)
416{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000417 struct stat buf;
418 int fd, old_fd;
419 if (!PyArg_ParseTuple(args, "i:set_wakeup_fd", &fd))
420 return NULL;
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000421#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000422 if (PyThread_get_thread_ident() != main_thread) {
423 PyErr_SetString(PyExc_ValueError,
424 "set_wakeup_fd only works in main thread");
425 return NULL;
426 }
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000427#endif
Benjamin Petersonc68a4a02013-01-18 00:10:24 -0500428 if (fd != -1 && (!_PyVerify_fd(fd) || fstat(fd, &buf) != 0)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000429 PyErr_SetString(PyExc_ValueError, "invalid fd");
430 return NULL;
431 }
432 old_fd = wakeup_fd;
433 wakeup_fd = fd;
434 return PyLong_FromLong(old_fd);
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000435}
436
437PyDoc_STRVAR(set_wakeup_fd_doc,
438"set_wakeup_fd(fd) -> fd\n\
439\n\
440Sets the fd to be written to (with '\\0') when a signal\n\
441comes in. A library can use this to wakeup select or poll.\n\
442The previous fd is returned.\n\
443\n\
444The fd must be non-blocking.");
445
446/* C API for the same, without all the error checking */
447int
448PySignal_SetWakeupFd(int fd)
449{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000450 int old_fd = wakeup_fd;
451 if (fd < 0)
452 fd = -1;
453 wakeup_fd = fd;
454 return old_fd;
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000455}
456
457
Martin v. Löwis823725e2008-03-24 13:39:54 +0000458#ifdef HAVE_SETITIMER
459static PyObject *
460signal_setitimer(PyObject *self, PyObject *args)
461{
462 double first;
463 double interval = 0;
464 int which;
465 struct itimerval new, old;
466
467 if(!PyArg_ParseTuple(args, "id|d:setitimer", &which, &first, &interval))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000468 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000469
470 timeval_from_double(first, &new.it_value);
471 timeval_from_double(interval, &new.it_interval);
472 /* Let OS check "which" value */
473 if (setitimer(which, &new, &old) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000474 PyErr_SetFromErrno(ItimerError);
475 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000476 }
477
478 return itimer_retval(&old);
479}
480
481PyDoc_STRVAR(setitimer_doc,
482"setitimer(which, seconds[, interval])\n\
483\n\
484Sets given itimer (one of ITIMER_REAL, ITIMER_VIRTUAL\n\
485or ITIMER_PROF) to fire after value seconds and after\n\
486that every interval seconds.\n\
487The itimer can be cleared by setting seconds to zero.\n\
488\n\
489Returns old values as a tuple: (delay, interval).");
490#endif
491
492
493#ifdef HAVE_GETITIMER
494static PyObject *
495signal_getitimer(PyObject *self, PyObject *args)
496{
497 int which;
498 struct itimerval old;
499
500 if (!PyArg_ParseTuple(args, "i:getitimer", &which))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000501 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000502
503 if (getitimer(which, &old) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000504 PyErr_SetFromErrno(ItimerError);
505 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000506 }
507
508 return itimer_retval(&old);
509}
510
511PyDoc_STRVAR(getitimer_doc,
512"getitimer(which)\n\
513\n\
514Returns current value of given itimer.");
515#endif
516
Ross Lagerwallbc808222011-06-25 12:13:40 +0200517#if defined(PYPTHREAD_SIGMASK) || defined(HAVE_SIGWAIT) || \
518 defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
Victor Stinnera9293352011-04-30 15:21:58 +0200519/* Convert an iterable to a sigset.
520 Return 0 on success, return -1 and raise an exception on error. */
521
522static int
523iterable_to_sigset(PyObject *iterable, sigset_t *mask)
524{
525 int result = -1;
526 PyObject *iterator, *item;
527 long signum;
528 int err;
529
530 sigemptyset(mask);
531
532 iterator = PyObject_GetIter(iterable);
533 if (iterator == NULL)
534 goto error;
535
536 while (1)
537 {
538 item = PyIter_Next(iterator);
539 if (item == NULL) {
540 if (PyErr_Occurred())
541 goto error;
542 else
543 break;
544 }
545
546 signum = PyLong_AsLong(item);
547 Py_DECREF(item);
548 if (signum == -1 && PyErr_Occurred())
549 goto error;
550 if (0 < signum && signum < NSIG)
551 err = sigaddset(mask, (int)signum);
552 else
553 err = 1;
554 if (err) {
555 PyErr_Format(PyExc_ValueError,
556 "signal number %ld out of range", signum);
557 goto error;
558 }
559 }
560 result = 0;
561
562error:
563 Py_XDECREF(iterator);
564 return result;
565}
Victor Stinnerb3e72192011-05-08 01:46:11 +0200566#endif
Victor Stinnera9293352011-04-30 15:21:58 +0200567
Victor Stinnerb3e72192011-05-08 01:46:11 +0200568#if defined(PYPTHREAD_SIGMASK) || defined(HAVE_SIGPENDING)
Victor Stinner35b300c2011-05-04 13:20:35 +0200569static PyObject*
570sigset_to_set(sigset_t mask)
571{
572 PyObject *signum, *result;
573 int sig;
574
575 result = PySet_New(0);
576 if (result == NULL)
577 return NULL;
578
579 for (sig = 1; sig < NSIG; sig++) {
580 if (sigismember(&mask, sig) != 1)
581 continue;
582
583 /* Handle the case where it is a member by adding the signal to
584 the result list. Ignore the other cases because they mean the
585 signal isn't a member of the mask or the signal was invalid,
586 and an invalid signal must have been our fault in constructing
587 the loop boundaries. */
588 signum = PyLong_FromLong(sig);
589 if (signum == NULL) {
590 Py_DECREF(result);
591 return NULL;
592 }
593 if (PySet_Add(result, signum) == -1) {
594 Py_DECREF(signum);
595 Py_DECREF(result);
596 return NULL;
597 }
598 Py_DECREF(signum);
599 }
600 return result;
601}
Victor Stinnerb3e72192011-05-08 01:46:11 +0200602#endif
Victor Stinner35b300c2011-05-04 13:20:35 +0200603
Victor Stinnerb3e72192011-05-08 01:46:11 +0200604#ifdef PYPTHREAD_SIGMASK
Victor Stinnera9293352011-04-30 15:21:58 +0200605static PyObject *
606signal_pthread_sigmask(PyObject *self, PyObject *args)
607{
Victor Stinner35b300c2011-05-04 13:20:35 +0200608 int how;
609 PyObject *signals;
Victor Stinnera9293352011-04-30 15:21:58 +0200610 sigset_t mask, previous;
611 int err;
612
613 if (!PyArg_ParseTuple(args, "iO:pthread_sigmask", &how, &signals))
614 return NULL;
615
616 if (iterable_to_sigset(signals, &mask))
617 return NULL;
618
619 err = pthread_sigmask(how, &mask, &previous);
620 if (err != 0) {
621 errno = err;
Victor Stinnerb3e72192011-05-08 01:46:11 +0200622 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnera9293352011-04-30 15:21:58 +0200623 return NULL;
624 }
625
Victor Stinnerd0e516d2011-05-03 14:57:12 +0200626 /* if signals was unblocked, signal handlers have been called */
627 if (PyErr_CheckSignals())
628 return NULL;
629
Victor Stinner35b300c2011-05-04 13:20:35 +0200630 return sigset_to_set(previous);
Victor Stinnera9293352011-04-30 15:21:58 +0200631}
632
633PyDoc_STRVAR(signal_pthread_sigmask_doc,
634"pthread_sigmask(how, mask) -> old mask\n\
635\n\
636Fetch and/or change the signal mask of the calling thread.");
637#endif /* #ifdef PYPTHREAD_SIGMASK */
638
Martin v. Löwis823725e2008-03-24 13:39:54 +0000639
Victor Stinnerb3e72192011-05-08 01:46:11 +0200640#ifdef HAVE_SIGPENDING
641static PyObject *
642signal_sigpending(PyObject *self)
643{
644 int err;
645 sigset_t mask;
646 err = sigpending(&mask);
647 if (err)
648 return PyErr_SetFromErrno(PyExc_OSError);
649 return sigset_to_set(mask);
650}
651
652PyDoc_STRVAR(signal_sigpending_doc,
653"sigpending() -> list\n\
654\n\
655Examine pending signals.");
656#endif /* #ifdef HAVE_SIGPENDING */
657
658
659#ifdef HAVE_SIGWAIT
660static PyObject *
661signal_sigwait(PyObject *self, PyObject *args)
662{
663 PyObject *signals;
664 sigset_t set;
665 int err, signum;
666
667 if (!PyArg_ParseTuple(args, "O:sigwait", &signals))
668 return NULL;
669
670 if (iterable_to_sigset(signals, &set))
671 return NULL;
672
Victor Stinner10c30d62011-06-10 01:39:53 +0200673 Py_BEGIN_ALLOW_THREADS
Victor Stinnerb3e72192011-05-08 01:46:11 +0200674 err = sigwait(&set, &signum);
Victor Stinner10c30d62011-06-10 01:39:53 +0200675 Py_END_ALLOW_THREADS
Victor Stinnerb3e72192011-05-08 01:46:11 +0200676 if (err) {
677 errno = err;
678 return PyErr_SetFromErrno(PyExc_OSError);
679 }
680
681 return PyLong_FromLong(signum);
682}
683
684PyDoc_STRVAR(signal_sigwait_doc,
685"sigwait(sigset) -> signum\n\
686\n\
687Wait a signal.");
688#endif /* #ifdef HAVE_SIGPENDING */
689
Ross Lagerwallbc808222011-06-25 12:13:40 +0200690#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
691static int initialized;
692static PyStructSequence_Field struct_siginfo_fields[] = {
693 {"si_signo", "signal number"},
694 {"si_code", "signal code"},
695 {"si_errno", "errno associated with this signal"},
696 {"si_pid", "sending process ID"},
697 {"si_uid", "real user ID of sending process"},
698 {"si_status", "exit value or signal"},
699 {"si_band", "band event for SIGPOLL"},
700 {0}
701};
702
703PyDoc_STRVAR(struct_siginfo__doc__,
704"struct_siginfo: Result from sigwaitinfo or sigtimedwait.\n\n\
705This object may be accessed either as a tuple of\n\
706(si_signo, si_code, si_errno, si_pid, si_uid, si_status, si_band),\n\
707or via the attributes si_signo, si_code, and so on.");
708
709static PyStructSequence_Desc struct_siginfo_desc = {
710 "signal.struct_siginfo", /* name */
711 struct_siginfo__doc__, /* doc */
712 struct_siginfo_fields, /* fields */
713 7 /* n_in_sequence */
714};
715
716static PyTypeObject SiginfoType;
717
718static PyObject *
719fill_siginfo(siginfo_t *si)
720{
721 PyObject *result = PyStructSequence_New(&SiginfoType);
722 if (!result)
723 return NULL;
724
725 PyStructSequence_SET_ITEM(result, 0, PyLong_FromLong((long)(si->si_signo)));
726 PyStructSequence_SET_ITEM(result, 1, PyLong_FromLong((long)(si->si_code)));
727 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si->si_errno)));
728 PyStructSequence_SET_ITEM(result, 3, PyLong_FromPid(si->si_pid));
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200729 PyStructSequence_SET_ITEM(result, 4, _PyLong_FromUid(si->si_uid));
Ross Lagerwallbc808222011-06-25 12:13:40 +0200730 PyStructSequence_SET_ITEM(result, 5,
731 PyLong_FromLong((long)(si->si_status)));
732 PyStructSequence_SET_ITEM(result, 6, PyLong_FromLong(si->si_band));
733 if (PyErr_Occurred()) {
734 Py_DECREF(result);
735 return NULL;
736 }
737
738 return result;
739}
740#endif
741
742#ifdef HAVE_SIGWAITINFO
743static PyObject *
744signal_sigwaitinfo(PyObject *self, PyObject *args)
745{
746 PyObject *signals;
747 sigset_t set;
748 siginfo_t si;
749 int err;
750
751 if (!PyArg_ParseTuple(args, "O:sigwaitinfo", &signals))
752 return NULL;
753
754 if (iterable_to_sigset(signals, &set))
755 return NULL;
756
757 Py_BEGIN_ALLOW_THREADS
758 err = sigwaitinfo(&set, &si);
759 Py_END_ALLOW_THREADS
760 if (err == -1)
761 return PyErr_SetFromErrno(PyExc_OSError);
762
763 return fill_siginfo(&si);
764}
765
766PyDoc_STRVAR(signal_sigwaitinfo_doc,
767"sigwaitinfo(sigset) -> struct_siginfo\n\
768\n\
769Wait synchronously for a signal until one of the signals in *sigset* is\n\
770delivered.\n\
771Returns a struct_siginfo containing information about the signal.");
772#endif /* #ifdef HAVE_SIGWAITINFO */
773
774#ifdef HAVE_SIGTIMEDWAIT
775static PyObject *
776signal_sigtimedwait(PyObject *self, PyObject *args)
777{
778 PyObject *signals, *timeout;
779 struct timespec buf;
780 sigset_t set;
781 siginfo_t si;
782 int err;
783
Victor Stinner643cd682012-03-02 22:54:03 +0100784 if (!PyArg_ParseTuple(args, "OO:sigtimedwait",
785 &signals, &timeout))
Ross Lagerwallbc808222011-06-25 12:13:40 +0200786 return NULL;
787
Victor Stinner643cd682012-03-02 22:54:03 +0100788 if (_PyTime_ObjectToTimespec(timeout, &buf.tv_sec, &buf.tv_nsec) == -1)
Ross Lagerwallbc808222011-06-25 12:13:40 +0200789 return NULL;
790
791 if (buf.tv_sec < 0 || buf.tv_nsec < 0) {
792 PyErr_SetString(PyExc_ValueError, "timeout must be non-negative");
793 return NULL;
794 }
795
796 if (iterable_to_sigset(signals, &set))
797 return NULL;
798
799 Py_BEGIN_ALLOW_THREADS
800 err = sigtimedwait(&set, &si, &buf);
801 Py_END_ALLOW_THREADS
802 if (err == -1) {
803 if (errno == EAGAIN)
804 Py_RETURN_NONE;
805 else
806 return PyErr_SetFromErrno(PyExc_OSError);
807 }
808
809 return fill_siginfo(&si);
810}
811
812PyDoc_STRVAR(signal_sigtimedwait_doc,
813"sigtimedwait(sigset, (timeout_sec, timeout_nsec)) -> struct_siginfo\n\
814\n\
815Like sigwaitinfo(), but with a timeout specified as a tuple of (seconds,\n\
816nanoseconds).");
817#endif /* #ifdef HAVE_SIGTIMEDWAIT */
818
Victor Stinnerb3e72192011-05-08 01:46:11 +0200819
820#if defined(HAVE_PTHREAD_KILL) && defined(WITH_THREAD)
821static PyObject *
822signal_pthread_kill(PyObject *self, PyObject *args)
823{
824 long tid;
825 int signum;
826 int err;
827
828 if (!PyArg_ParseTuple(args, "li:pthread_kill", &tid, &signum))
829 return NULL;
830
Victor Stinner86e104a2011-05-09 14:45:38 +0200831 err = pthread_kill((pthread_t)tid, signum);
Victor Stinnerb3e72192011-05-08 01:46:11 +0200832 if (err != 0) {
833 errno = err;
834 PyErr_SetFromErrno(PyExc_OSError);
835 return NULL;
836 }
837
838 /* the signal may have been send to the current thread */
839 if (PyErr_CheckSignals())
840 return NULL;
841
842 Py_RETURN_NONE;
843}
844
845PyDoc_STRVAR(signal_pthread_kill_doc,
846"pthread_kill(thread_id, signum)\n\
847\n\
848Send a signal to a thread.");
849#endif /* #if defined(HAVE_PTHREAD_KILL) && defined(WITH_THREAD) */
850
851
852
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000853/* List of functions defined in the module */
Barry Warsaw92971171997-01-03 00:14:25 +0000854static PyMethodDef signal_methods[] = {
Guido van Rossum1171ee61997-08-22 20:42:00 +0000855#ifdef HAVE_ALARM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000856 {"alarm", signal_alarm, METH_VARARGS, alarm_doc},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000857#endif
Martin v. Löwis823725e2008-03-24 13:39:54 +0000858#ifdef HAVE_SETITIMER
859 {"setitimer", signal_setitimer, METH_VARARGS, setitimer_doc},
860#endif
861#ifdef HAVE_GETITIMER
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000862 {"getitimer", signal_getitimer, METH_VARARGS, getitimer_doc},
Martin v. Löwis823725e2008-03-24 13:39:54 +0000863#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000864 {"signal", signal_signal, METH_VARARGS, signal_doc},
865 {"getsignal", signal_getsignal, METH_VARARGS, getsignal_doc},
866 {"set_wakeup_fd", signal_set_wakeup_fd, METH_VARARGS, set_wakeup_fd_doc},
Christian Heimes8640e742008-02-23 16:23:06 +0000867#ifdef HAVE_SIGINTERRUPT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000868 {"siginterrupt", signal_siginterrupt, METH_VARARGS, siginterrupt_doc},
Christian Heimes8640e742008-02-23 16:23:06 +0000869#endif
Guido van Rossum1171ee61997-08-22 20:42:00 +0000870#ifdef HAVE_PAUSE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000871 {"pause", (PyCFunction)signal_pause,
Victor Stinnera9293352011-04-30 15:21:58 +0200872 METH_NOARGS, pause_doc},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000873#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000874 {"default_int_handler", signal_default_int_handler,
875 METH_VARARGS, default_int_handler_doc},
Victor Stinnerb3e72192011-05-08 01:46:11 +0200876#if defined(HAVE_PTHREAD_KILL) && defined(WITH_THREAD)
877 {"pthread_kill", (PyCFunction)signal_pthread_kill,
878 METH_VARARGS, signal_pthread_kill_doc},
879#endif
Victor Stinnera9293352011-04-30 15:21:58 +0200880#ifdef PYPTHREAD_SIGMASK
881 {"pthread_sigmask", (PyCFunction)signal_pthread_sigmask,
882 METH_VARARGS, signal_pthread_sigmask_doc},
883#endif
Victor Stinnerb3e72192011-05-08 01:46:11 +0200884#ifdef HAVE_SIGPENDING
885 {"sigpending", (PyCFunction)signal_sigpending,
886 METH_NOARGS, signal_sigpending_doc},
887#endif
888#ifdef HAVE_SIGWAIT
889 {"sigwait", (PyCFunction)signal_sigwait,
890 METH_VARARGS, signal_sigwait_doc},
891#endif
Ross Lagerwallbc808222011-06-25 12:13:40 +0200892#ifdef HAVE_SIGWAITINFO
893 {"sigwaitinfo", (PyCFunction)signal_sigwaitinfo,
894 METH_VARARGS, signal_sigwaitinfo_doc},
895#endif
896#ifdef HAVE_SIGTIMEDWAIT
897 {"sigtimedwait", (PyCFunction)signal_sigtimedwait,
898 METH_VARARGS, signal_sigtimedwait_doc},
899#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000900 {NULL, NULL} /* sentinel */
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000901};
902
Barry Warsaw92971171997-01-03 00:14:25 +0000903
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000904PyDoc_STRVAR(module_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000905"This module provides mechanisms to use signal handlers in Python.\n\
906\n\
907Functions:\n\
908\n\
909alarm() -- cause SIGALRM after a specified time [Unix only]\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +0000910setitimer() -- cause a signal (described below) after a specified\n\
911 float time and the timer may restart then [Unix only]\n\
912getitimer() -- get current value of timer [Unix only]\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000913signal() -- set the action for a given signal\n\
914getsignal() -- get the signal action for a given signal\n\
915pause() -- wait until a signal arrives [Unix only]\n\
916default_int_handler() -- default SIGINT handler\n\
917\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +0000918signal constants:\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000919SIG_DFL -- used to refer to the system default handler\n\
920SIG_IGN -- used to ignore the signal\n\
921NSIG -- number of defined signals\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000922SIGINT, SIGTERM, etc. -- signal numbers\n\
923\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +0000924itimer constants:\n\
925ITIMER_REAL -- decrements in real time, and delivers SIGALRM upon\n\
926 expiration\n\
927ITIMER_VIRTUAL -- decrements only when the process is executing,\n\
928 and delivers SIGVTALRM upon expiration\n\
929ITIMER_PROF -- decrements both when the process is executing and\n\
930 when the system is executing on behalf of the process.\n\
931 Coupled with ITIMER_VIRTUAL, this timer is usually\n\
932 used to profile the time spent by the application\n\
933 in user and kernel space. SIGPROF is delivered upon\n\
934 expiration.\n\
935\n\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000936*** IMPORTANT NOTICE ***\n\
937A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000938the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000939
Martin v. Löwis1a214512008-06-11 05:26:20 +0000940static struct PyModuleDef signalmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000941 PyModuleDef_HEAD_INIT,
942 "signal",
943 module_doc,
944 -1,
945 signal_methods,
946 NULL,
947 NULL,
948 NULL,
949 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000950};
951
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000952PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000953PyInit_signal(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000954{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000955 PyObject *m, *d, *x;
956 int i;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000957
958#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000959 main_thread = PyThread_get_thread_ident();
960 main_pid = getpid();
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000961#endif
962
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000963 /* Create the module and add the functions */
964 m = PyModule_Create(&signalmodule);
965 if (m == NULL)
966 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000967
Ross Lagerwallbc808222011-06-25 12:13:40 +0200968#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
969 if (!initialized)
970 PyStructSequence_InitType(&SiginfoType, &struct_siginfo_desc);
971
972 Py_INCREF((PyObject*) &SiginfoType);
973 PyModule_AddObject(m, "struct_siginfo", (PyObject*) &SiginfoType);
974 initialized = 1;
975#endif
976
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000977 /* Add some symbolic constants to the module */
978 d = PyModule_GetDict(m);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000979
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000980 x = DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL);
981 if (!x || PyDict_SetItemString(d, "SIG_DFL", x) < 0)
982 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000983
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000984 x = IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN);
985 if (!x || PyDict_SetItemString(d, "SIG_IGN", x) < 0)
986 goto finally;
Barry Warsaw92971171997-01-03 00:14:25 +0000987
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000988 x = PyLong_FromLong((long)NSIG);
989 if (!x || PyDict_SetItemString(d, "NSIG", x) < 0)
990 goto finally;
991 Py_DECREF(x);
Barry Warsaw92971171997-01-03 00:14:25 +0000992
Victor Stinnera9293352011-04-30 15:21:58 +0200993#ifdef SIG_BLOCK
Victor Stinner72c53b52011-05-02 16:15:43 +0200994 if (PyModule_AddIntMacro(m, SIG_BLOCK))
995 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +0200996#endif
Victor Stinnera9293352011-04-30 15:21:58 +0200997#ifdef SIG_UNBLOCK
Victor Stinner72c53b52011-05-02 16:15:43 +0200998 if (PyModule_AddIntMacro(m, SIG_UNBLOCK))
999 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001000#endif
Victor Stinnera9293352011-04-30 15:21:58 +02001001#ifdef SIG_SETMASK
Victor Stinner72c53b52011-05-02 16:15:43 +02001002 if (PyModule_AddIntMacro(m, SIG_SETMASK))
1003 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001004#endif
1005
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001006 x = IntHandler = PyDict_GetItemString(d, "default_int_handler");
1007 if (!x)
1008 goto finally;
1009 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +00001010
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001011 Handlers[0].tripped = 0;
1012 for (i = 1; i < NSIG; i++) {
1013 void (*t)(int);
1014 t = PyOS_getsig(i);
1015 Handlers[i].tripped = 0;
1016 if (t == SIG_DFL)
1017 Handlers[i].func = DefaultHandler;
1018 else if (t == SIG_IGN)
1019 Handlers[i].func = IgnoreHandler;
1020 else
1021 Handlers[i].func = Py_None; /* None of our business */
1022 Py_INCREF(Handlers[i].func);
1023 }
1024 if (Handlers[SIGINT].func == DefaultHandler) {
1025 /* Install default int handler */
1026 Py_INCREF(IntHandler);
1027 Py_DECREF(Handlers[SIGINT].func);
1028 Handlers[SIGINT].func = IntHandler;
1029 old_siginthandler = PyOS_setsig(SIGINT, signal_handler);
1030 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001031
1032#ifdef SIGHUP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001033 x = PyLong_FromLong(SIGHUP);
1034 PyDict_SetItemString(d, "SIGHUP", x);
1035 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001036#endif
1037#ifdef SIGINT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001038 x = PyLong_FromLong(SIGINT);
1039 PyDict_SetItemString(d, "SIGINT", x);
1040 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001041#endif
Tim Peters1ce3cf72001-10-01 17:58:40 +00001042#ifdef SIGBREAK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001043 x = PyLong_FromLong(SIGBREAK);
1044 PyDict_SetItemString(d, "SIGBREAK", x);
1045 Py_XDECREF(x);
Tim Peters1ce3cf72001-10-01 17:58:40 +00001046#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001047#ifdef SIGQUIT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001048 x = PyLong_FromLong(SIGQUIT);
1049 PyDict_SetItemString(d, "SIGQUIT", x);
1050 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001051#endif
1052#ifdef SIGILL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001053 x = PyLong_FromLong(SIGILL);
1054 PyDict_SetItemString(d, "SIGILL", x);
1055 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001056#endif
1057#ifdef SIGTRAP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001058 x = PyLong_FromLong(SIGTRAP);
1059 PyDict_SetItemString(d, "SIGTRAP", x);
1060 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001061#endif
1062#ifdef SIGIOT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001063 x = PyLong_FromLong(SIGIOT);
1064 PyDict_SetItemString(d, "SIGIOT", x);
1065 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001066#endif
1067#ifdef SIGABRT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001068 x = PyLong_FromLong(SIGABRT);
1069 PyDict_SetItemString(d, "SIGABRT", x);
1070 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001071#endif
1072#ifdef SIGEMT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001073 x = PyLong_FromLong(SIGEMT);
1074 PyDict_SetItemString(d, "SIGEMT", x);
1075 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001076#endif
1077#ifdef SIGFPE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001078 x = PyLong_FromLong(SIGFPE);
1079 PyDict_SetItemString(d, "SIGFPE", x);
1080 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001081#endif
1082#ifdef SIGKILL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001083 x = PyLong_FromLong(SIGKILL);
1084 PyDict_SetItemString(d, "SIGKILL", x);
1085 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001086#endif
1087#ifdef SIGBUS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001088 x = PyLong_FromLong(SIGBUS);
1089 PyDict_SetItemString(d, "SIGBUS", x);
1090 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001091#endif
1092#ifdef SIGSEGV
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001093 x = PyLong_FromLong(SIGSEGV);
1094 PyDict_SetItemString(d, "SIGSEGV", x);
1095 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001096#endif
1097#ifdef SIGSYS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001098 x = PyLong_FromLong(SIGSYS);
1099 PyDict_SetItemString(d, "SIGSYS", x);
1100 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001101#endif
1102#ifdef SIGPIPE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001103 x = PyLong_FromLong(SIGPIPE);
1104 PyDict_SetItemString(d, "SIGPIPE", x);
1105 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001106#endif
1107#ifdef SIGALRM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001108 x = PyLong_FromLong(SIGALRM);
1109 PyDict_SetItemString(d, "SIGALRM", x);
1110 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001111#endif
1112#ifdef SIGTERM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001113 x = PyLong_FromLong(SIGTERM);
1114 PyDict_SetItemString(d, "SIGTERM", x);
1115 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001116#endif
1117#ifdef SIGUSR1
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001118 x = PyLong_FromLong(SIGUSR1);
1119 PyDict_SetItemString(d, "SIGUSR1", x);
1120 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001121#endif
1122#ifdef SIGUSR2
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001123 x = PyLong_FromLong(SIGUSR2);
1124 PyDict_SetItemString(d, "SIGUSR2", x);
1125 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001126#endif
1127#ifdef SIGCLD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001128 x = PyLong_FromLong(SIGCLD);
1129 PyDict_SetItemString(d, "SIGCLD", x);
1130 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001131#endif
1132#ifdef SIGCHLD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001133 x = PyLong_FromLong(SIGCHLD);
1134 PyDict_SetItemString(d, "SIGCHLD", x);
1135 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001136#endif
1137#ifdef SIGPWR
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001138 x = PyLong_FromLong(SIGPWR);
1139 PyDict_SetItemString(d, "SIGPWR", x);
1140 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001141#endif
1142#ifdef SIGIO
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001143 x = PyLong_FromLong(SIGIO);
1144 PyDict_SetItemString(d, "SIGIO", x);
1145 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001146#endif
1147#ifdef SIGURG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001148 x = PyLong_FromLong(SIGURG);
1149 PyDict_SetItemString(d, "SIGURG", x);
1150 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001151#endif
1152#ifdef SIGWINCH
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001153 x = PyLong_FromLong(SIGWINCH);
1154 PyDict_SetItemString(d, "SIGWINCH", x);
1155 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001156#endif
1157#ifdef SIGPOLL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001158 x = PyLong_FromLong(SIGPOLL);
1159 PyDict_SetItemString(d, "SIGPOLL", x);
1160 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001161#endif
1162#ifdef SIGSTOP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001163 x = PyLong_FromLong(SIGSTOP);
1164 PyDict_SetItemString(d, "SIGSTOP", x);
1165 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001166#endif
1167#ifdef SIGTSTP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001168 x = PyLong_FromLong(SIGTSTP);
1169 PyDict_SetItemString(d, "SIGTSTP", x);
1170 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001171#endif
1172#ifdef SIGCONT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001173 x = PyLong_FromLong(SIGCONT);
1174 PyDict_SetItemString(d, "SIGCONT", x);
1175 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001176#endif
1177#ifdef SIGTTIN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001178 x = PyLong_FromLong(SIGTTIN);
1179 PyDict_SetItemString(d, "SIGTTIN", x);
1180 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001181#endif
1182#ifdef SIGTTOU
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001183 x = PyLong_FromLong(SIGTTOU);
1184 PyDict_SetItemString(d, "SIGTTOU", x);
1185 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001186#endif
1187#ifdef SIGVTALRM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001188 x = PyLong_FromLong(SIGVTALRM);
1189 PyDict_SetItemString(d, "SIGVTALRM", x);
1190 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001191#endif
1192#ifdef SIGPROF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001193 x = PyLong_FromLong(SIGPROF);
1194 PyDict_SetItemString(d, "SIGPROF", x);
1195 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001196#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001197#ifdef SIGXCPU
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001198 x = PyLong_FromLong(SIGXCPU);
1199 PyDict_SetItemString(d, "SIGXCPU", x);
1200 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001201#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001202#ifdef SIGXFSZ
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001203 x = PyLong_FromLong(SIGXFSZ);
1204 PyDict_SetItemString(d, "SIGXFSZ", x);
1205 Py_XDECREF(x);
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001206#endif
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001207#ifdef SIGRTMIN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001208 x = PyLong_FromLong(SIGRTMIN);
1209 PyDict_SetItemString(d, "SIGRTMIN", x);
1210 Py_XDECREF(x);
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001211#endif
1212#ifdef SIGRTMAX
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001213 x = PyLong_FromLong(SIGRTMAX);
1214 PyDict_SetItemString(d, "SIGRTMAX", x);
1215 Py_XDECREF(x);
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001216#endif
Martin v. Löwis175af252002-01-12 11:43:25 +00001217#ifdef SIGINFO
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001218 x = PyLong_FromLong(SIGINFO);
1219 PyDict_SetItemString(d, "SIGINFO", x);
1220 Py_XDECREF(x);
Martin v. Löwis175af252002-01-12 11:43:25 +00001221#endif
Martin v. Löwis823725e2008-03-24 13:39:54 +00001222
1223#ifdef ITIMER_REAL
1224 x = PyLong_FromLong(ITIMER_REAL);
1225 PyDict_SetItemString(d, "ITIMER_REAL", x);
1226 Py_DECREF(x);
1227#endif
1228#ifdef ITIMER_VIRTUAL
1229 x = PyLong_FromLong(ITIMER_VIRTUAL);
1230 PyDict_SetItemString(d, "ITIMER_VIRTUAL", x);
1231 Py_DECREF(x);
1232#endif
1233#ifdef ITIMER_PROF
1234 x = PyLong_FromLong(ITIMER_PROF);
1235 PyDict_SetItemString(d, "ITIMER_PROF", x);
1236 Py_DECREF(x);
1237#endif
1238
1239#if defined (HAVE_SETITIMER) || defined (HAVE_GETITIMER)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001240 ItimerError = PyErr_NewException("signal.ItimerError",
1241 PyExc_IOError, NULL);
Neal Norwitz2f99b242008-08-24 05:48:10 +00001242 if (ItimerError != NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001243 PyDict_SetItemString(d, "ItimerError", ItimerError);
Martin v. Löwis823725e2008-03-24 13:39:54 +00001244#endif
1245
Brian Curtineb24d742010-04-12 17:16:38 +00001246#ifdef CTRL_C_EVENT
1247 x = PyLong_FromLong(CTRL_C_EVENT);
1248 PyDict_SetItemString(d, "CTRL_C_EVENT", x);
1249 Py_DECREF(x);
1250#endif
1251
1252#ifdef CTRL_BREAK_EVENT
1253 x = PyLong_FromLong(CTRL_BREAK_EVENT);
1254 PyDict_SetItemString(d, "CTRL_BREAK_EVENT", x);
1255 Py_DECREF(x);
1256#endif
1257
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001258#ifdef MS_WINDOWS
1259 /* Create manual-reset event, initially unset */
1260 sigint_event = CreateEvent(NULL, TRUE, FALSE, FALSE);
1261#endif
1262
Martin v. Löwis1a214512008-06-11 05:26:20 +00001263 if (PyErr_Occurred()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001264 Py_DECREF(m);
1265 m = NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001266 }
Barry Warsaw92971171997-01-03 00:14:25 +00001267
Barry Warsaw92971171997-01-03 00:14:25 +00001268 finally:
Martin v. Löwis1a214512008-06-11 05:26:20 +00001269 return m;
Guido van Rossum08c16611997-08-02 03:01:42 +00001270}
1271
1272static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001273finisignal(void)
Guido van Rossum08c16611997-08-02 03:01:42 +00001274{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001275 int i;
1276 PyObject *func;
Guido van Rossum08c16611997-08-02 03:01:42 +00001277
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001278 PyOS_setsig(SIGINT, old_siginthandler);
1279 old_siginthandler = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +00001280
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001281 for (i = 1; i < NSIG; i++) {
1282 func = Handlers[i].func;
1283 Handlers[i].tripped = 0;
1284 Handlers[i].func = NULL;
1285 if (i != SIGINT && func != NULL && func != Py_None &&
1286 func != DefaultHandler && func != IgnoreHandler)
1287 PyOS_setsig(i, SIG_DFL);
1288 Py_XDECREF(func);
1289 }
Guido van Rossum08c16611997-08-02 03:01:42 +00001290
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001291 Py_XDECREF(IntHandler);
1292 IntHandler = NULL;
1293 Py_XDECREF(DefaultHandler);
1294 DefaultHandler = NULL;
1295 Py_XDECREF(IgnoreHandler);
1296 IgnoreHandler = NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001297}
1298
Barry Warsaw92971171997-01-03 00:14:25 +00001299
Barry Warsaw92971171997-01-03 00:14:25 +00001300/* Declared in pyerrors.h */
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001301int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001302PyErr_CheckSignals(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001303{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001304 int i;
1305 PyObject *f;
Barry Warsaw92971171997-01-03 00:14:25 +00001306
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001307 if (!is_tripped)
1308 return 0;
Christian Heimesb76922a2007-12-11 01:06:40 +00001309
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001310#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001311 if (PyThread_get_thread_ident() != main_thread)
1312 return 0;
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001313#endif
Christian Heimesb76922a2007-12-11 01:06:40 +00001314
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001315 /*
1316 * The is_tripped variable is meant to speed up the calls to
1317 * PyErr_CheckSignals (both directly or via pending calls) when no
1318 * signal has arrived. This variable is set to 1 when a signal arrives
1319 * and it is set to 0 here, when we know some signals arrived. This way
1320 * we can run the registered handlers with no signals blocked.
1321 *
1322 * NOTE: with this approach we can have a situation where is_tripped is
1323 * 1 but we have no more signals to handle (Handlers[i].tripped
1324 * is 0 for every signal i). This won't do us any harm (except
1325 * we're gonna spent some cycles for nothing). This happens when
1326 * we receive a signal i after we zero is_tripped and before we
1327 * check Handlers[i].tripped.
1328 */
1329 is_tripped = 0;
Christian Heimesb76922a2007-12-11 01:06:40 +00001330
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001331 if (!(f = (PyObject *)PyEval_GetFrame()))
1332 f = Py_None;
Christian Heimesb76922a2007-12-11 01:06:40 +00001333
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001334 for (i = 1; i < NSIG; i++) {
1335 if (Handlers[i].tripped) {
1336 PyObject *result = NULL;
1337 PyObject *arglist = Py_BuildValue("(iO)", i, f);
1338 Handlers[i].tripped = 0;
Barry Warsaw92971171997-01-03 00:14:25 +00001339
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001340 if (arglist) {
1341 result = PyEval_CallObject(Handlers[i].func,
1342 arglist);
1343 Py_DECREF(arglist);
1344 }
1345 if (!result)
1346 return -1;
Barry Warsaw92971171997-01-03 00:14:25 +00001347
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001348 Py_DECREF(result);
1349 }
1350 }
Christian Heimesb76922a2007-12-11 01:06:40 +00001351
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001352 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001353}
1354
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +00001355
Barry Warsaw92971171997-01-03 00:14:25 +00001356/* Replacements for intrcheck.c functionality
1357 * Declared in pyerrors.h
1358 */
1359void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001360PyErr_SetInterrupt(void)
Barry Warsaw92971171997-01-03 00:14:25 +00001361{
Victor Stinner6c9b35b2011-04-18 16:25:56 +02001362 trip_signal(SIGINT);
Barry Warsaw92971171997-01-03 00:14:25 +00001363}
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001364
1365void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001366PyOS_InitInterrupts(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001367{
Brett Cannon0ecd30b2013-02-01 14:04:12 -05001368 PyObject *m = PyImport_ImportModule("signal");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001369 if (m) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001370 Py_DECREF(m);
1371 }
Guido van Rossum08c16611997-08-02 03:01:42 +00001372}
1373
1374void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001375PyOS_FiniInterrupts(void)
Guido van Rossum08c16611997-08-02 03:01:42 +00001376{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001377 finisignal();
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001378}
1379
1380int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001381PyOS_InterruptOccurred(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001382{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001383 if (Handlers[SIGINT].tripped) {
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001384#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001385 if (PyThread_get_thread_ident() != main_thread)
1386 return 0;
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001387#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001388 Handlers[SIGINT].tripped = 0;
1389 return 1;
1390 }
1391 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001392}
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001393
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001394static void
1395_clear_pending_signals(void)
1396{
1397 int i;
1398 if (!is_tripped)
1399 return;
1400 is_tripped = 0;
1401 for (i = 1; i < NSIG; ++i) {
1402 Handlers[i].tripped = 0;
1403 }
1404}
1405
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001406void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001407PyOS_AfterFork(void)
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001408{
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001409 /* Clear the signal flags after forking so that they aren't handled
1410 * in both processes if they came in just before the fork() but before
1411 * the interpreter had an opportunity to call the handlers. issue9535. */
1412 _clear_pending_signals();
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001413#ifdef WITH_THREAD
Charles-François Natali6d0d24e2012-02-02 20:31:42 +01001414 /* PyThread_ReInitTLS() must be called early, to make sure that the TLS API
1415 * can be called safely. */
1416 PyThread_ReInitTLS();
Antoine Pitrou0c759fe2011-04-27 19:28:05 +02001417 _PyGILState_Reinit();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001418 PyEval_ReInitThreads();
1419 main_thread = PyThread_get_thread_ident();
1420 main_pid = getpid();
1421 _PyImport_ReInitLock();
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001422#endif
1423}
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001424
1425int
1426_PyOS_IsMainThread(void)
1427{
1428#ifdef WITH_THREAD
1429 return PyThread_get_thread_ident() == main_thread;
1430#else
1431 return 1;
1432#endif
1433}
1434
1435#ifdef MS_WINDOWS
1436void *_PyOS_SigintEvent(void)
1437{
1438 /* Returns a manual-reset event which gets tripped whenever
1439 SIGINT is received.
1440
1441 Python.h does not include windows.h so we do cannot use HANDLE
1442 as the return type of this function. We use void* instead. */
1443 return sigint_event;
1444}
1445#endif