blob: 2eb7f297ebe39fcb2d12798cbc7c50307ae23c1a [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
Antoine Pitrou6dd381e2011-11-21 21:26:56 +0100112#ifdef MS_WINDOWS
113static HANDLE sigint_event = NULL;
114#endif
115
Martin v. Löwis823725e2008-03-24 13:39:54 +0000116#ifdef HAVE_GETITIMER
117static PyObject *ItimerError;
118
119/* auxiliary functions for setitimer/getitimer */
120static void
121timeval_from_double(double d, struct timeval *tv)
122{
123 tv->tv_sec = floor(d);
124 tv->tv_usec = fmod(d, 1.0) * 1000000.0;
125}
126
Christian Heimes1a8501c2008-10-02 19:56:01 +0000127Py_LOCAL_INLINE(double)
Martin v. Löwis823725e2008-03-24 13:39:54 +0000128double_from_timeval(struct timeval *tv)
129{
130 return tv->tv_sec + (double)(tv->tv_usec / 1000000.0);
131}
132
133static PyObject *
134itimer_retval(struct itimerval *iv)
135{
136 PyObject *r, *v;
137
138 r = PyTuple_New(2);
139 if (r == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000140 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000141
142 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_value)))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000143 Py_DECREF(r);
144 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000145 }
146
147 PyTuple_SET_ITEM(r, 0, v);
148
149 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_interval)))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000150 Py_DECREF(r);
151 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000152 }
153
154 PyTuple_SET_ITEM(r, 1, v);
155
156 return r;
157}
158#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000159
Guido van Rossume4485b01994-09-07 14:32:49 +0000160static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000161signal_default_int_handler(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000162{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000163 PyErr_SetNone(PyExc_KeyboardInterrupt);
164 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000165}
166
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000167PyDoc_STRVAR(default_int_handler_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000168"default_int_handler(...)\n\
169\n\
Michael W. Hudson24ec2112004-06-17 15:55:53 +0000170The default handler for SIGINT installed by Python.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000171It raises KeyboardInterrupt.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000172
Thomas Wouters0796b002000-07-22 23:49:30 +0000173
174static int
175checksignals_witharg(void * unused)
176{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000177 return PyErr_CheckSignals();
Thomas Wouters0796b002000-07-22 23:49:30 +0000178}
179
Tim Peters4f1b2082000-07-23 21:18:09 +0000180static void
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200181trip_signal(int sig_num)
182{
Victor Stinnerd49b1f12011-05-08 02:03:15 +0200183 unsigned char byte;
Victor Stinnerc13ef662011-05-25 02:35:58 +0200184
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200185 Handlers[sig_num].tripped = 1;
Victor Stinnerc13ef662011-05-25 02:35:58 +0200186 if (wakeup_fd != -1) {
187 byte = (unsigned char)sig_num;
188 write(wakeup_fd, &byte, 1);
189 }
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200190 if (is_tripped)
191 return;
192 /* Set is_tripped after setting .tripped, as it gets
193 cleared in PyErr_CheckSignals() before .tripped. */
194 is_tripped = 1;
195 Py_AddPendingCall(checksignals_witharg, NULL);
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200196}
197
198static void
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000199signal_handler(int sig_num)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000200{
Antoine Pitrou39a65912010-11-05 19:47:27 +0000201 int save_errno = errno;
202
203#if defined(WITH_THREAD) && defined(WITH_PTH)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000204 if (PyThread_get_thread_ident() != main_thread) {
205 pth_raise(*(pth_t *) main_thread, sig_num);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000206 }
Antoine Pitrou39a65912010-11-05 19:47:27 +0000207 else
Guido van Rossum9e8181b2000-09-19 00:46:46 +0000208#endif
Antoine Pitrou39a65912010-11-05 19:47:27 +0000209 {
210#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000211 /* See NOTES section above */
Antoine Pitrou39a65912010-11-05 19:47:27 +0000212 if (getpid() == main_pid)
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000213#endif
Antoine Pitrou39a65912010-11-05 19:47:27 +0000214 {
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200215 trip_signal(sig_num);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000216 }
Antoine Pitrou39a65912010-11-05 19:47:27 +0000217
Jean-Paul Calderone6f137ca2010-05-09 03:18:57 +0000218#ifndef HAVE_SIGACTION
Antoine Pitrou39a65912010-11-05 19:47:27 +0000219#ifdef SIGCHLD
220 /* To avoid infinite recursion, this signal remains
221 reset until explicit re-instated.
222 Don't clear the 'func' field as it is our pointer
223 to the Python handler... */
224 if (sig_num != SIGCHLD)
225#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000226 /* If the handler was not set up with sigaction, reinstall it. See
227 * Python/pythonrun.c for the implementation of PyOS_setsig which
228 * makes this true. See also issue8354. */
229 PyOS_setsig(sig_num, signal_handler);
Jean-Paul Calderone6f137ca2010-05-09 03:18:57 +0000230#endif
Antoine Pitrou39a65912010-11-05 19:47:27 +0000231 }
232
233 /* Issue #10311: asynchronously executing signal handlers should not
234 mutate errno under the feet of unsuspecting C code. */
235 errno = save_errno;
Antoine Pitrou6dd381e2011-11-21 21:26:56 +0100236
237#ifdef MS_WINDOWS
238 if (sig_num == SIGINT)
239 SetEvent(sigint_event);
240#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000241}
Guido van Rossume4485b01994-09-07 14:32:49 +0000242
Guido van Rossum06d511d1995-03-10 15:13:48 +0000243
Guido van Rossum1171ee61997-08-22 20:42:00 +0000244#ifdef HAVE_ALARM
Guido van Rossume4485b01994-09-07 14:32:49 +0000245static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000246signal_alarm(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000247{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000248 int t;
249 if (!PyArg_ParseTuple(args, "i:alarm", &t))
250 return NULL;
251 /* alarm() returns the number of seconds remaining */
252 return PyLong_FromLong((long)alarm(t));
Guido van Rossumaa0f4c71994-08-23 13:49:37 +0000253}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000254
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000255PyDoc_STRVAR(alarm_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000256"alarm(seconds)\n\
257\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000258Arrange for SIGALRM to arrive after the given number of seconds.");
Guido van Rossum06d511d1995-03-10 15:13:48 +0000259#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000260
Guido van Rossum1171ee61997-08-22 20:42:00 +0000261#ifdef HAVE_PAUSE
Guido van Rossuma597dde1995-01-10 20:56:29 +0000262static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000263signal_pause(PyObject *self)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000264{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000265 Py_BEGIN_ALLOW_THREADS
266 (void)pause();
267 Py_END_ALLOW_THREADS
268 /* make sure that any exceptions that got raised are propagated
269 * back into Python
270 */
271 if (PyErr_CheckSignals())
272 return NULL;
Barry Warsaw92971171997-01-03 00:14:25 +0000273
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000274 Py_INCREF(Py_None);
275 return Py_None;
Guido van Rossume4485b01994-09-07 14:32:49 +0000276}
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000277PyDoc_STRVAR(pause_doc,
Barry Warsaw1ee36ff1998-07-21 22:41:18 +0000278"pause()\n\
279\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000280Wait until a signal arrives.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000281
Guido van Rossum06d511d1995-03-10 15:13:48 +0000282#endif
Guido van Rossume4485b01994-09-07 14:32:49 +0000283
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000284
Guido van Rossume4485b01994-09-07 14:32:49 +0000285static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000286signal_signal(PyObject *self, PyObject *args)
Guido van Rossume4485b01994-09-07 14:32:49 +0000287{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000288 PyObject *obj;
289 int sig_num;
290 PyObject *old_handler;
291 void (*func)(int);
292 if (!PyArg_ParseTuple(args, "iO:signal", &sig_num, &obj))
293 return NULL;
Brian Curtinef9efbd2010-08-06 19:27:32 +0000294#ifdef MS_WINDOWS
295 /* Validate that sig_num is one of the allowable signals */
Brian Curtinc734b312010-09-06 16:04:10 +0000296 switch (sig_num) {
297 case SIGABRT: break;
Brian Curtin9e88b5a2010-10-01 14:49:24 +0000298#ifdef SIGBREAK
299 /* Issue #10003: SIGBREAK is not documented as permitted, but works
300 and corresponds to CTRL_BREAK_EVENT. */
301 case SIGBREAK: break;
302#endif
Brian Curtinc734b312010-09-06 16:04:10 +0000303 case SIGFPE: break;
304 case SIGILL: break;
305 case SIGINT: break;
306 case SIGSEGV: break;
307 case SIGTERM: break;
308 default:
309 PyErr_SetString(PyExc_ValueError, "invalid signal value");
310 return NULL;
Brian Curtinef9efbd2010-08-06 19:27:32 +0000311 }
312#endif
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000313#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000314 if (PyThread_get_thread_ident() != main_thread) {
315 PyErr_SetString(PyExc_ValueError,
316 "signal only works in main thread");
317 return NULL;
318 }
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000319#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000320 if (sig_num < 1 || sig_num >= NSIG) {
321 PyErr_SetString(PyExc_ValueError,
322 "signal number out of range");
323 return NULL;
324 }
325 if (obj == IgnoreHandler)
326 func = SIG_IGN;
327 else if (obj == DefaultHandler)
328 func = SIG_DFL;
329 else if (!PyCallable_Check(obj)) {
330 PyErr_SetString(PyExc_TypeError,
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000331"signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000332 return NULL;
333 }
334 else
335 func = signal_handler;
336 if (PyOS_setsig(sig_num, func) == SIG_ERR) {
Victor Stinner388196e2011-05-10 17:13:00 +0200337 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000338 return NULL;
339 }
340 old_handler = Handlers[sig_num].func;
341 Handlers[sig_num].tripped = 0;
342 Py_INCREF(obj);
343 Handlers[sig_num].func = obj;
344 return old_handler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000345}
346
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000347PyDoc_STRVAR(signal_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000348"signal(sig, action) -> action\n\
349\n\
350Set the action for the given signal. The action can be SIG_DFL,\n\
351SIG_IGN, or a callable Python object. The previous action is\n\
352returned. See getsignal() for possible return values.\n\
353\n\
354*** IMPORTANT NOTICE ***\n\
355A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000356the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000357
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000358
Guido van Rossume4485b01994-09-07 14:32:49 +0000359static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000360signal_getsignal(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000361{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000362 int sig_num;
363 PyObject *old_handler;
364 if (!PyArg_ParseTuple(args, "i:getsignal", &sig_num))
365 return NULL;
366 if (sig_num < 1 || sig_num >= NSIG) {
367 PyErr_SetString(PyExc_ValueError,
368 "signal number out of range");
369 return NULL;
370 }
371 old_handler = Handlers[sig_num].func;
372 Py_INCREF(old_handler);
373 return old_handler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000374}
375
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000376PyDoc_STRVAR(getsignal_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000377"getsignal(sig) -> action\n\
378\n\
379Return the current action for the given signal. The return value can be:\n\
380SIG_IGN -- if the signal is being ignored\n\
381SIG_DFL -- if the default action for the signal is in effect\n\
382None -- if an unknown handler is in effect\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000383anything else -- the callable Python object used as a handler");
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000384
Christian Heimes8640e742008-02-23 16:23:06 +0000385#ifdef HAVE_SIGINTERRUPT
386PyDoc_STRVAR(siginterrupt_doc,
387"siginterrupt(sig, flag) -> None\n\
388change system call restart behaviour: if flag is False, system calls\n\
389will be restarted when interrupted by signal sig, else system calls\n\
390will be interrupted.");
391
392static PyObject *
393signal_siginterrupt(PyObject *self, PyObject *args)
394{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000395 int sig_num;
396 int flag;
Christian Heimes8640e742008-02-23 16:23:06 +0000397
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000398 if (!PyArg_ParseTuple(args, "ii:siginterrupt", &sig_num, &flag))
399 return NULL;
400 if (sig_num < 1 || sig_num >= NSIG) {
401 PyErr_SetString(PyExc_ValueError,
402 "signal number out of range");
403 return NULL;
404 }
405 if (siginterrupt(sig_num, flag)<0) {
Victor Stinner388196e2011-05-10 17:13:00 +0200406 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000407 return NULL;
408 }
Christian Heimes8640e742008-02-23 16:23:06 +0000409
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000410 Py_INCREF(Py_None);
411 return Py_None;
Christian Heimes8640e742008-02-23 16:23:06 +0000412}
413
414#endif
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000415
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000416static PyObject *
417signal_set_wakeup_fd(PyObject *self, PyObject *args)
418{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000419 struct stat buf;
420 int fd, old_fd;
421 if (!PyArg_ParseTuple(args, "i:set_wakeup_fd", &fd))
422 return NULL;
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000423#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000424 if (PyThread_get_thread_ident() != main_thread) {
425 PyErr_SetString(PyExc_ValueError,
426 "set_wakeup_fd only works in main thread");
427 return NULL;
428 }
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000429#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000430 if (fd != -1 && fstat(fd, &buf) != 0) {
431 PyErr_SetString(PyExc_ValueError, "invalid fd");
432 return NULL;
433 }
434 old_fd = wakeup_fd;
435 wakeup_fd = fd;
436 return PyLong_FromLong(old_fd);
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000437}
438
439PyDoc_STRVAR(set_wakeup_fd_doc,
440"set_wakeup_fd(fd) -> fd\n\
441\n\
442Sets the fd to be written to (with '\\0') when a signal\n\
443comes in. A library can use this to wakeup select or poll.\n\
444The previous fd is returned.\n\
445\n\
446The fd must be non-blocking.");
447
448/* C API for the same, without all the error checking */
449int
450PySignal_SetWakeupFd(int fd)
451{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000452 int old_fd = wakeup_fd;
453 if (fd < 0)
454 fd = -1;
455 wakeup_fd = fd;
456 return old_fd;
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000457}
458
459
Martin v. Löwis823725e2008-03-24 13:39:54 +0000460#ifdef HAVE_SETITIMER
461static PyObject *
462signal_setitimer(PyObject *self, PyObject *args)
463{
464 double first;
465 double interval = 0;
466 int which;
467 struct itimerval new, old;
468
469 if(!PyArg_ParseTuple(args, "id|d:setitimer", &which, &first, &interval))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000470 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000471
472 timeval_from_double(first, &new.it_value);
473 timeval_from_double(interval, &new.it_interval);
474 /* Let OS check "which" value */
475 if (setitimer(which, &new, &old) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000476 PyErr_SetFromErrno(ItimerError);
477 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000478 }
479
480 return itimer_retval(&old);
481}
482
483PyDoc_STRVAR(setitimer_doc,
484"setitimer(which, seconds[, interval])\n\
485\n\
486Sets given itimer (one of ITIMER_REAL, ITIMER_VIRTUAL\n\
487or ITIMER_PROF) to fire after value seconds and after\n\
488that every interval seconds.\n\
489The itimer can be cleared by setting seconds to zero.\n\
490\n\
491Returns old values as a tuple: (delay, interval).");
492#endif
493
494
495#ifdef HAVE_GETITIMER
496static PyObject *
497signal_getitimer(PyObject *self, PyObject *args)
498{
499 int which;
500 struct itimerval old;
501
502 if (!PyArg_ParseTuple(args, "i:getitimer", &which))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000504
505 if (getitimer(which, &old) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000506 PyErr_SetFromErrno(ItimerError);
507 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000508 }
509
510 return itimer_retval(&old);
511}
512
513PyDoc_STRVAR(getitimer_doc,
514"getitimer(which)\n\
515\n\
516Returns current value of given itimer.");
517#endif
518
Ross Lagerwallbc808222011-06-25 12:13:40 +0200519#if defined(PYPTHREAD_SIGMASK) || defined(HAVE_SIGWAIT) || \
520 defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
Victor Stinnera9293352011-04-30 15:21:58 +0200521/* Convert an iterable to a sigset.
522 Return 0 on success, return -1 and raise an exception on error. */
523
524static int
525iterable_to_sigset(PyObject *iterable, sigset_t *mask)
526{
527 int result = -1;
528 PyObject *iterator, *item;
529 long signum;
530 int err;
531
532 sigemptyset(mask);
533
534 iterator = PyObject_GetIter(iterable);
535 if (iterator == NULL)
536 goto error;
537
538 while (1)
539 {
540 item = PyIter_Next(iterator);
541 if (item == NULL) {
542 if (PyErr_Occurred())
543 goto error;
544 else
545 break;
546 }
547
548 signum = PyLong_AsLong(item);
549 Py_DECREF(item);
550 if (signum == -1 && PyErr_Occurred())
551 goto error;
552 if (0 < signum && signum < NSIG)
553 err = sigaddset(mask, (int)signum);
554 else
555 err = 1;
556 if (err) {
557 PyErr_Format(PyExc_ValueError,
558 "signal number %ld out of range", signum);
559 goto error;
560 }
561 }
562 result = 0;
563
564error:
565 Py_XDECREF(iterator);
566 return result;
567}
Victor Stinnerb3e72192011-05-08 01:46:11 +0200568#endif
Victor Stinnera9293352011-04-30 15:21:58 +0200569
Victor Stinnerb3e72192011-05-08 01:46:11 +0200570#if defined(PYPTHREAD_SIGMASK) || defined(HAVE_SIGPENDING)
Victor Stinner35b300c2011-05-04 13:20:35 +0200571static PyObject*
572sigset_to_set(sigset_t mask)
573{
574 PyObject *signum, *result;
575 int sig;
576
577 result = PySet_New(0);
578 if (result == NULL)
579 return NULL;
580
581 for (sig = 1; sig < NSIG; sig++) {
582 if (sigismember(&mask, sig) != 1)
583 continue;
584
585 /* Handle the case where it is a member by adding the signal to
586 the result list. Ignore the other cases because they mean the
587 signal isn't a member of the mask or the signal was invalid,
588 and an invalid signal must have been our fault in constructing
589 the loop boundaries. */
590 signum = PyLong_FromLong(sig);
591 if (signum == NULL) {
592 Py_DECREF(result);
593 return NULL;
594 }
595 if (PySet_Add(result, signum) == -1) {
596 Py_DECREF(signum);
597 Py_DECREF(result);
598 return NULL;
599 }
600 Py_DECREF(signum);
601 }
602 return result;
603}
Victor Stinnerb3e72192011-05-08 01:46:11 +0200604#endif
Victor Stinner35b300c2011-05-04 13:20:35 +0200605
Victor Stinnerb3e72192011-05-08 01:46:11 +0200606#ifdef PYPTHREAD_SIGMASK
Victor Stinnera9293352011-04-30 15:21:58 +0200607static PyObject *
608signal_pthread_sigmask(PyObject *self, PyObject *args)
609{
Victor Stinner35b300c2011-05-04 13:20:35 +0200610 int how;
611 PyObject *signals;
Victor Stinnera9293352011-04-30 15:21:58 +0200612 sigset_t mask, previous;
613 int err;
614
615 if (!PyArg_ParseTuple(args, "iO:pthread_sigmask", &how, &signals))
616 return NULL;
617
618 if (iterable_to_sigset(signals, &mask))
619 return NULL;
620
621 err = pthread_sigmask(how, &mask, &previous);
622 if (err != 0) {
623 errno = err;
Victor Stinnerb3e72192011-05-08 01:46:11 +0200624 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnera9293352011-04-30 15:21:58 +0200625 return NULL;
626 }
627
Victor Stinnerd0e516d2011-05-03 14:57:12 +0200628 /* if signals was unblocked, signal handlers have been called */
629 if (PyErr_CheckSignals())
630 return NULL;
631
Victor Stinner35b300c2011-05-04 13:20:35 +0200632 return sigset_to_set(previous);
Victor Stinnera9293352011-04-30 15:21:58 +0200633}
634
635PyDoc_STRVAR(signal_pthread_sigmask_doc,
636"pthread_sigmask(how, mask) -> old mask\n\
637\n\
638Fetch and/or change the signal mask of the calling thread.");
639#endif /* #ifdef PYPTHREAD_SIGMASK */
640
Martin v. Löwis823725e2008-03-24 13:39:54 +0000641
Victor Stinnerb3e72192011-05-08 01:46:11 +0200642#ifdef HAVE_SIGPENDING
643static PyObject *
644signal_sigpending(PyObject *self)
645{
646 int err;
647 sigset_t mask;
648 err = sigpending(&mask);
649 if (err)
650 return PyErr_SetFromErrno(PyExc_OSError);
651 return sigset_to_set(mask);
652}
653
654PyDoc_STRVAR(signal_sigpending_doc,
655"sigpending() -> list\n\
656\n\
657Examine pending signals.");
658#endif /* #ifdef HAVE_SIGPENDING */
659
660
661#ifdef HAVE_SIGWAIT
662static PyObject *
663signal_sigwait(PyObject *self, PyObject *args)
664{
665 PyObject *signals;
666 sigset_t set;
667 int err, signum;
668
669 if (!PyArg_ParseTuple(args, "O:sigwait", &signals))
670 return NULL;
671
672 if (iterable_to_sigset(signals, &set))
673 return NULL;
674
Victor Stinner10c30d62011-06-10 01:39:53 +0200675 Py_BEGIN_ALLOW_THREADS
Victor Stinnerb3e72192011-05-08 01:46:11 +0200676 err = sigwait(&set, &signum);
Victor Stinner10c30d62011-06-10 01:39:53 +0200677 Py_END_ALLOW_THREADS
Victor Stinnerb3e72192011-05-08 01:46:11 +0200678 if (err) {
679 errno = err;
680 return PyErr_SetFromErrno(PyExc_OSError);
681 }
682
683 return PyLong_FromLong(signum);
684}
685
686PyDoc_STRVAR(signal_sigwait_doc,
687"sigwait(sigset) -> signum\n\
688\n\
689Wait a signal.");
690#endif /* #ifdef HAVE_SIGPENDING */
691
Ross Lagerwallbc808222011-06-25 12:13:40 +0200692#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
693static int initialized;
694static PyStructSequence_Field struct_siginfo_fields[] = {
695 {"si_signo", "signal number"},
696 {"si_code", "signal code"},
697 {"si_errno", "errno associated with this signal"},
698 {"si_pid", "sending process ID"},
699 {"si_uid", "real user ID of sending process"},
700 {"si_status", "exit value or signal"},
701 {"si_band", "band event for SIGPOLL"},
702 {0}
703};
704
705PyDoc_STRVAR(struct_siginfo__doc__,
706"struct_siginfo: Result from sigwaitinfo or sigtimedwait.\n\n\
707This object may be accessed either as a tuple of\n\
708(si_signo, si_code, si_errno, si_pid, si_uid, si_status, si_band),\n\
709or via the attributes si_signo, si_code, and so on.");
710
711static PyStructSequence_Desc struct_siginfo_desc = {
712 "signal.struct_siginfo", /* name */
713 struct_siginfo__doc__, /* doc */
714 struct_siginfo_fields, /* fields */
715 7 /* n_in_sequence */
716};
717
718static PyTypeObject SiginfoType;
719
720static PyObject *
721fill_siginfo(siginfo_t *si)
722{
723 PyObject *result = PyStructSequence_New(&SiginfoType);
724 if (!result)
725 return NULL;
726
727 PyStructSequence_SET_ITEM(result, 0, PyLong_FromLong((long)(si->si_signo)));
728 PyStructSequence_SET_ITEM(result, 1, PyLong_FromLong((long)(si->si_code)));
729 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si->si_errno)));
730 PyStructSequence_SET_ITEM(result, 3, PyLong_FromPid(si->si_pid));
731 PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si->si_uid)));
732 PyStructSequence_SET_ITEM(result, 5,
733 PyLong_FromLong((long)(si->si_status)));
734 PyStructSequence_SET_ITEM(result, 6, PyLong_FromLong(si->si_band));
735 if (PyErr_Occurred()) {
736 Py_DECREF(result);
737 return NULL;
738 }
739
740 return result;
741}
742#endif
743
744#ifdef HAVE_SIGWAITINFO
745static PyObject *
746signal_sigwaitinfo(PyObject *self, PyObject *args)
747{
748 PyObject *signals;
749 sigset_t set;
750 siginfo_t si;
751 int err;
752
753 if (!PyArg_ParseTuple(args, "O:sigwaitinfo", &signals))
754 return NULL;
755
756 if (iterable_to_sigset(signals, &set))
757 return NULL;
758
759 Py_BEGIN_ALLOW_THREADS
760 err = sigwaitinfo(&set, &si);
761 Py_END_ALLOW_THREADS
762 if (err == -1)
763 return PyErr_SetFromErrno(PyExc_OSError);
764
765 return fill_siginfo(&si);
766}
767
768PyDoc_STRVAR(signal_sigwaitinfo_doc,
769"sigwaitinfo(sigset) -> struct_siginfo\n\
770\n\
771Wait synchronously for a signal until one of the signals in *sigset* is\n\
772delivered.\n\
773Returns a struct_siginfo containing information about the signal.");
774#endif /* #ifdef HAVE_SIGWAITINFO */
775
776#ifdef HAVE_SIGTIMEDWAIT
777static PyObject *
778signal_sigtimedwait(PyObject *self, PyObject *args)
779{
780 PyObject *signals, *timeout;
781 struct timespec buf;
782 sigset_t set;
783 siginfo_t si;
784 int err;
785
Victor Stinner643cd682012-03-02 22:54:03 +0100786 if (!PyArg_ParseTuple(args, "OO:sigtimedwait",
787 &signals, &timeout))
Ross Lagerwallbc808222011-06-25 12:13:40 +0200788 return NULL;
789
Victor Stinner643cd682012-03-02 22:54:03 +0100790 if (_PyTime_ObjectToTimespec(timeout, &buf.tv_sec, &buf.tv_nsec) == -1)
Ross Lagerwallbc808222011-06-25 12:13:40 +0200791 return NULL;
792
793 if (buf.tv_sec < 0 || buf.tv_nsec < 0) {
794 PyErr_SetString(PyExc_ValueError, "timeout must be non-negative");
795 return NULL;
796 }
797
798 if (iterable_to_sigset(signals, &set))
799 return NULL;
800
801 Py_BEGIN_ALLOW_THREADS
802 err = sigtimedwait(&set, &si, &buf);
803 Py_END_ALLOW_THREADS
804 if (err == -1) {
805 if (errno == EAGAIN)
806 Py_RETURN_NONE;
807 else
808 return PyErr_SetFromErrno(PyExc_OSError);
809 }
810
811 return fill_siginfo(&si);
812}
813
814PyDoc_STRVAR(signal_sigtimedwait_doc,
815"sigtimedwait(sigset, (timeout_sec, timeout_nsec)) -> struct_siginfo\n\
816\n\
817Like sigwaitinfo(), but with a timeout specified as a tuple of (seconds,\n\
818nanoseconds).");
819#endif /* #ifdef HAVE_SIGTIMEDWAIT */
820
Victor Stinnerb3e72192011-05-08 01:46:11 +0200821
822#if defined(HAVE_PTHREAD_KILL) && defined(WITH_THREAD)
823static PyObject *
824signal_pthread_kill(PyObject *self, PyObject *args)
825{
826 long tid;
827 int signum;
828 int err;
829
830 if (!PyArg_ParseTuple(args, "li:pthread_kill", &tid, &signum))
831 return NULL;
832
Victor Stinner86e104a2011-05-09 14:45:38 +0200833 err = pthread_kill((pthread_t)tid, signum);
Victor Stinnerb3e72192011-05-08 01:46:11 +0200834 if (err != 0) {
835 errno = err;
836 PyErr_SetFromErrno(PyExc_OSError);
837 return NULL;
838 }
839
840 /* the signal may have been send to the current thread */
841 if (PyErr_CheckSignals())
842 return NULL;
843
844 Py_RETURN_NONE;
845}
846
847PyDoc_STRVAR(signal_pthread_kill_doc,
848"pthread_kill(thread_id, signum)\n\
849\n\
850Send a signal to a thread.");
851#endif /* #if defined(HAVE_PTHREAD_KILL) && defined(WITH_THREAD) */
852
853
854
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000855/* List of functions defined in the module */
Barry Warsaw92971171997-01-03 00:14:25 +0000856static PyMethodDef signal_methods[] = {
Guido van Rossum1171ee61997-08-22 20:42:00 +0000857#ifdef HAVE_ALARM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000858 {"alarm", signal_alarm, METH_VARARGS, alarm_doc},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000859#endif
Martin v. Löwis823725e2008-03-24 13:39:54 +0000860#ifdef HAVE_SETITIMER
861 {"setitimer", signal_setitimer, METH_VARARGS, setitimer_doc},
862#endif
863#ifdef HAVE_GETITIMER
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000864 {"getitimer", signal_getitimer, METH_VARARGS, getitimer_doc},
Martin v. Löwis823725e2008-03-24 13:39:54 +0000865#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000866 {"signal", signal_signal, METH_VARARGS, signal_doc},
867 {"getsignal", signal_getsignal, METH_VARARGS, getsignal_doc},
868 {"set_wakeup_fd", signal_set_wakeup_fd, METH_VARARGS, set_wakeup_fd_doc},
Christian Heimes8640e742008-02-23 16:23:06 +0000869#ifdef HAVE_SIGINTERRUPT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000870 {"siginterrupt", signal_siginterrupt, METH_VARARGS, siginterrupt_doc},
Christian Heimes8640e742008-02-23 16:23:06 +0000871#endif
Guido van Rossum1171ee61997-08-22 20:42:00 +0000872#ifdef HAVE_PAUSE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000873 {"pause", (PyCFunction)signal_pause,
Victor Stinnera9293352011-04-30 15:21:58 +0200874 METH_NOARGS, pause_doc},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000875#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000876 {"default_int_handler", signal_default_int_handler,
877 METH_VARARGS, default_int_handler_doc},
Victor Stinnerb3e72192011-05-08 01:46:11 +0200878#if defined(HAVE_PTHREAD_KILL) && defined(WITH_THREAD)
879 {"pthread_kill", (PyCFunction)signal_pthread_kill,
880 METH_VARARGS, signal_pthread_kill_doc},
881#endif
Victor Stinnera9293352011-04-30 15:21:58 +0200882#ifdef PYPTHREAD_SIGMASK
883 {"pthread_sigmask", (PyCFunction)signal_pthread_sigmask,
884 METH_VARARGS, signal_pthread_sigmask_doc},
885#endif
Victor Stinnerb3e72192011-05-08 01:46:11 +0200886#ifdef HAVE_SIGPENDING
887 {"sigpending", (PyCFunction)signal_sigpending,
888 METH_NOARGS, signal_sigpending_doc},
889#endif
890#ifdef HAVE_SIGWAIT
891 {"sigwait", (PyCFunction)signal_sigwait,
892 METH_VARARGS, signal_sigwait_doc},
893#endif
Ross Lagerwallbc808222011-06-25 12:13:40 +0200894#ifdef HAVE_SIGWAITINFO
895 {"sigwaitinfo", (PyCFunction)signal_sigwaitinfo,
896 METH_VARARGS, signal_sigwaitinfo_doc},
897#endif
898#ifdef HAVE_SIGTIMEDWAIT
899 {"sigtimedwait", (PyCFunction)signal_sigtimedwait,
900 METH_VARARGS, signal_sigtimedwait_doc},
901#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000902 {NULL, NULL} /* sentinel */
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000903};
904
Barry Warsaw92971171997-01-03 00:14:25 +0000905
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000906PyDoc_STRVAR(module_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000907"This module provides mechanisms to use signal handlers in Python.\n\
908\n\
909Functions:\n\
910\n\
911alarm() -- cause SIGALRM after a specified time [Unix only]\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +0000912setitimer() -- cause a signal (described below) after a specified\n\
913 float time and the timer may restart then [Unix only]\n\
914getitimer() -- get current value of timer [Unix only]\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000915signal() -- set the action for a given signal\n\
916getsignal() -- get the signal action for a given signal\n\
917pause() -- wait until a signal arrives [Unix only]\n\
918default_int_handler() -- default SIGINT handler\n\
919\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +0000920signal constants:\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000921SIG_DFL -- used to refer to the system default handler\n\
922SIG_IGN -- used to ignore the signal\n\
923NSIG -- number of defined signals\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000924SIGINT, SIGTERM, etc. -- signal numbers\n\
925\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +0000926itimer constants:\n\
927ITIMER_REAL -- decrements in real time, and delivers SIGALRM upon\n\
928 expiration\n\
929ITIMER_VIRTUAL -- decrements only when the process is executing,\n\
930 and delivers SIGVTALRM upon expiration\n\
931ITIMER_PROF -- decrements both when the process is executing and\n\
932 when the system is executing on behalf of the process.\n\
933 Coupled with ITIMER_VIRTUAL, this timer is usually\n\
934 used to profile the time spent by the application\n\
935 in user and kernel space. SIGPROF is delivered upon\n\
936 expiration.\n\
937\n\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000938*** IMPORTANT NOTICE ***\n\
939A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000940the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000941
Martin v. Löwis1a214512008-06-11 05:26:20 +0000942static struct PyModuleDef signalmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000943 PyModuleDef_HEAD_INIT,
944 "signal",
945 module_doc,
946 -1,
947 signal_methods,
948 NULL,
949 NULL,
950 NULL,
951 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000952};
953
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000954PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000955PyInit_signal(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000956{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000957 PyObject *m, *d, *x;
958 int i;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000959
960#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000961 main_thread = PyThread_get_thread_ident();
962 main_pid = getpid();
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000963#endif
964
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000965 /* Create the module and add the functions */
966 m = PyModule_Create(&signalmodule);
967 if (m == NULL)
968 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000969
Ross Lagerwallbc808222011-06-25 12:13:40 +0200970#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
971 if (!initialized)
972 PyStructSequence_InitType(&SiginfoType, &struct_siginfo_desc);
973
974 Py_INCREF((PyObject*) &SiginfoType);
975 PyModule_AddObject(m, "struct_siginfo", (PyObject*) &SiginfoType);
976 initialized = 1;
977#endif
978
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000979 /* Add some symbolic constants to the module */
980 d = PyModule_GetDict(m);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000981
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000982 x = DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL);
983 if (!x || PyDict_SetItemString(d, "SIG_DFL", x) < 0)
984 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000985
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000986 x = IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN);
987 if (!x || PyDict_SetItemString(d, "SIG_IGN", x) < 0)
988 goto finally;
Barry Warsaw92971171997-01-03 00:14:25 +0000989
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000990 x = PyLong_FromLong((long)NSIG);
991 if (!x || PyDict_SetItemString(d, "NSIG", x) < 0)
992 goto finally;
993 Py_DECREF(x);
Barry Warsaw92971171997-01-03 00:14:25 +0000994
Victor Stinnera9293352011-04-30 15:21:58 +0200995#ifdef SIG_BLOCK
Victor Stinner72c53b52011-05-02 16:15:43 +0200996 if (PyModule_AddIntMacro(m, SIG_BLOCK))
997 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +0200998#endif
Victor Stinnera9293352011-04-30 15:21:58 +0200999#ifdef SIG_UNBLOCK
Victor Stinner72c53b52011-05-02 16:15:43 +02001000 if (PyModule_AddIntMacro(m, SIG_UNBLOCK))
1001 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001002#endif
Victor Stinnera9293352011-04-30 15:21:58 +02001003#ifdef SIG_SETMASK
Victor Stinner72c53b52011-05-02 16:15:43 +02001004 if (PyModule_AddIntMacro(m, SIG_SETMASK))
1005 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +02001006#endif
1007
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001008 x = IntHandler = PyDict_GetItemString(d, "default_int_handler");
1009 if (!x)
1010 goto finally;
1011 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +00001012
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001013 Handlers[0].tripped = 0;
1014 for (i = 1; i < NSIG; i++) {
1015 void (*t)(int);
1016 t = PyOS_getsig(i);
1017 Handlers[i].tripped = 0;
1018 if (t == SIG_DFL)
1019 Handlers[i].func = DefaultHandler;
1020 else if (t == SIG_IGN)
1021 Handlers[i].func = IgnoreHandler;
1022 else
1023 Handlers[i].func = Py_None; /* None of our business */
1024 Py_INCREF(Handlers[i].func);
1025 }
1026 if (Handlers[SIGINT].func == DefaultHandler) {
1027 /* Install default int handler */
1028 Py_INCREF(IntHandler);
1029 Py_DECREF(Handlers[SIGINT].func);
1030 Handlers[SIGINT].func = IntHandler;
1031 old_siginthandler = PyOS_setsig(SIGINT, signal_handler);
1032 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001033
1034#ifdef SIGHUP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001035 x = PyLong_FromLong(SIGHUP);
1036 PyDict_SetItemString(d, "SIGHUP", x);
1037 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001038#endif
1039#ifdef SIGINT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001040 x = PyLong_FromLong(SIGINT);
1041 PyDict_SetItemString(d, "SIGINT", x);
1042 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001043#endif
Tim Peters1ce3cf72001-10-01 17:58:40 +00001044#ifdef SIGBREAK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001045 x = PyLong_FromLong(SIGBREAK);
1046 PyDict_SetItemString(d, "SIGBREAK", x);
1047 Py_XDECREF(x);
Tim Peters1ce3cf72001-10-01 17:58:40 +00001048#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001049#ifdef SIGQUIT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001050 x = PyLong_FromLong(SIGQUIT);
1051 PyDict_SetItemString(d, "SIGQUIT", x);
1052 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001053#endif
1054#ifdef SIGILL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001055 x = PyLong_FromLong(SIGILL);
1056 PyDict_SetItemString(d, "SIGILL", x);
1057 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001058#endif
1059#ifdef SIGTRAP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001060 x = PyLong_FromLong(SIGTRAP);
1061 PyDict_SetItemString(d, "SIGTRAP", x);
1062 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001063#endif
1064#ifdef SIGIOT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001065 x = PyLong_FromLong(SIGIOT);
1066 PyDict_SetItemString(d, "SIGIOT", x);
1067 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001068#endif
1069#ifdef SIGABRT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001070 x = PyLong_FromLong(SIGABRT);
1071 PyDict_SetItemString(d, "SIGABRT", x);
1072 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001073#endif
1074#ifdef SIGEMT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001075 x = PyLong_FromLong(SIGEMT);
1076 PyDict_SetItemString(d, "SIGEMT", x);
1077 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001078#endif
1079#ifdef SIGFPE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001080 x = PyLong_FromLong(SIGFPE);
1081 PyDict_SetItemString(d, "SIGFPE", x);
1082 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001083#endif
1084#ifdef SIGKILL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001085 x = PyLong_FromLong(SIGKILL);
1086 PyDict_SetItemString(d, "SIGKILL", x);
1087 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001088#endif
1089#ifdef SIGBUS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001090 x = PyLong_FromLong(SIGBUS);
1091 PyDict_SetItemString(d, "SIGBUS", x);
1092 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001093#endif
1094#ifdef SIGSEGV
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001095 x = PyLong_FromLong(SIGSEGV);
1096 PyDict_SetItemString(d, "SIGSEGV", x);
1097 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001098#endif
1099#ifdef SIGSYS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001100 x = PyLong_FromLong(SIGSYS);
1101 PyDict_SetItemString(d, "SIGSYS", x);
1102 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001103#endif
1104#ifdef SIGPIPE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001105 x = PyLong_FromLong(SIGPIPE);
1106 PyDict_SetItemString(d, "SIGPIPE", x);
1107 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001108#endif
1109#ifdef SIGALRM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001110 x = PyLong_FromLong(SIGALRM);
1111 PyDict_SetItemString(d, "SIGALRM", x);
1112 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001113#endif
1114#ifdef SIGTERM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001115 x = PyLong_FromLong(SIGTERM);
1116 PyDict_SetItemString(d, "SIGTERM", x);
1117 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001118#endif
1119#ifdef SIGUSR1
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001120 x = PyLong_FromLong(SIGUSR1);
1121 PyDict_SetItemString(d, "SIGUSR1", x);
1122 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001123#endif
1124#ifdef SIGUSR2
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001125 x = PyLong_FromLong(SIGUSR2);
1126 PyDict_SetItemString(d, "SIGUSR2", x);
1127 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001128#endif
1129#ifdef SIGCLD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001130 x = PyLong_FromLong(SIGCLD);
1131 PyDict_SetItemString(d, "SIGCLD", x);
1132 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001133#endif
1134#ifdef SIGCHLD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001135 x = PyLong_FromLong(SIGCHLD);
1136 PyDict_SetItemString(d, "SIGCHLD", x);
1137 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001138#endif
1139#ifdef SIGPWR
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001140 x = PyLong_FromLong(SIGPWR);
1141 PyDict_SetItemString(d, "SIGPWR", x);
1142 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001143#endif
1144#ifdef SIGIO
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001145 x = PyLong_FromLong(SIGIO);
1146 PyDict_SetItemString(d, "SIGIO", x);
1147 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001148#endif
1149#ifdef SIGURG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001150 x = PyLong_FromLong(SIGURG);
1151 PyDict_SetItemString(d, "SIGURG", x);
1152 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001153#endif
1154#ifdef SIGWINCH
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001155 x = PyLong_FromLong(SIGWINCH);
1156 PyDict_SetItemString(d, "SIGWINCH", x);
1157 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001158#endif
1159#ifdef SIGPOLL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001160 x = PyLong_FromLong(SIGPOLL);
1161 PyDict_SetItemString(d, "SIGPOLL", x);
1162 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001163#endif
1164#ifdef SIGSTOP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001165 x = PyLong_FromLong(SIGSTOP);
1166 PyDict_SetItemString(d, "SIGSTOP", x);
1167 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001168#endif
1169#ifdef SIGTSTP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001170 x = PyLong_FromLong(SIGTSTP);
1171 PyDict_SetItemString(d, "SIGTSTP", x);
1172 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001173#endif
1174#ifdef SIGCONT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001175 x = PyLong_FromLong(SIGCONT);
1176 PyDict_SetItemString(d, "SIGCONT", x);
1177 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001178#endif
1179#ifdef SIGTTIN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001180 x = PyLong_FromLong(SIGTTIN);
1181 PyDict_SetItemString(d, "SIGTTIN", x);
1182 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001183#endif
1184#ifdef SIGTTOU
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001185 x = PyLong_FromLong(SIGTTOU);
1186 PyDict_SetItemString(d, "SIGTTOU", x);
1187 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001188#endif
1189#ifdef SIGVTALRM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001190 x = PyLong_FromLong(SIGVTALRM);
1191 PyDict_SetItemString(d, "SIGVTALRM", x);
1192 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001193#endif
1194#ifdef SIGPROF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001195 x = PyLong_FromLong(SIGPROF);
1196 PyDict_SetItemString(d, "SIGPROF", x);
1197 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001198#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001199#ifdef SIGXCPU
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001200 x = PyLong_FromLong(SIGXCPU);
1201 PyDict_SetItemString(d, "SIGXCPU", x);
1202 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001203#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001204#ifdef SIGXFSZ
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001205 x = PyLong_FromLong(SIGXFSZ);
1206 PyDict_SetItemString(d, "SIGXFSZ", x);
1207 Py_XDECREF(x);
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001208#endif
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001209#ifdef SIGRTMIN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001210 x = PyLong_FromLong(SIGRTMIN);
1211 PyDict_SetItemString(d, "SIGRTMIN", x);
1212 Py_XDECREF(x);
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001213#endif
1214#ifdef SIGRTMAX
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001215 x = PyLong_FromLong(SIGRTMAX);
1216 PyDict_SetItemString(d, "SIGRTMAX", x);
1217 Py_XDECREF(x);
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001218#endif
Martin v. Löwis175af252002-01-12 11:43:25 +00001219#ifdef SIGINFO
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001220 x = PyLong_FromLong(SIGINFO);
1221 PyDict_SetItemString(d, "SIGINFO", x);
1222 Py_XDECREF(x);
Martin v. Löwis175af252002-01-12 11:43:25 +00001223#endif
Martin v. Löwis823725e2008-03-24 13:39:54 +00001224
1225#ifdef ITIMER_REAL
1226 x = PyLong_FromLong(ITIMER_REAL);
1227 PyDict_SetItemString(d, "ITIMER_REAL", x);
1228 Py_DECREF(x);
1229#endif
1230#ifdef ITIMER_VIRTUAL
1231 x = PyLong_FromLong(ITIMER_VIRTUAL);
1232 PyDict_SetItemString(d, "ITIMER_VIRTUAL", x);
1233 Py_DECREF(x);
1234#endif
1235#ifdef ITIMER_PROF
1236 x = PyLong_FromLong(ITIMER_PROF);
1237 PyDict_SetItemString(d, "ITIMER_PROF", x);
1238 Py_DECREF(x);
1239#endif
1240
1241#if defined (HAVE_SETITIMER) || defined (HAVE_GETITIMER)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001242 ItimerError = PyErr_NewException("signal.ItimerError",
1243 PyExc_IOError, NULL);
Neal Norwitz2f99b242008-08-24 05:48:10 +00001244 if (ItimerError != NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001245 PyDict_SetItemString(d, "ItimerError", ItimerError);
Martin v. Löwis823725e2008-03-24 13:39:54 +00001246#endif
1247
Brian Curtineb24d742010-04-12 17:16:38 +00001248#ifdef CTRL_C_EVENT
1249 x = PyLong_FromLong(CTRL_C_EVENT);
1250 PyDict_SetItemString(d, "CTRL_C_EVENT", x);
1251 Py_DECREF(x);
1252#endif
1253
1254#ifdef CTRL_BREAK_EVENT
1255 x = PyLong_FromLong(CTRL_BREAK_EVENT);
1256 PyDict_SetItemString(d, "CTRL_BREAK_EVENT", x);
1257 Py_DECREF(x);
1258#endif
1259
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001260#ifdef MS_WINDOWS
1261 /* Create manual-reset event, initially unset */
1262 sigint_event = CreateEvent(NULL, TRUE, FALSE, FALSE);
1263#endif
1264
Martin v. Löwis1a214512008-06-11 05:26:20 +00001265 if (PyErr_Occurred()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001266 Py_DECREF(m);
1267 m = NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001268 }
Barry Warsaw92971171997-01-03 00:14:25 +00001269
Barry Warsaw92971171997-01-03 00:14:25 +00001270 finally:
Martin v. Löwis1a214512008-06-11 05:26:20 +00001271 return m;
Guido van Rossum08c16611997-08-02 03:01:42 +00001272}
1273
1274static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001275finisignal(void)
Guido van Rossum08c16611997-08-02 03:01:42 +00001276{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001277 int i;
1278 PyObject *func;
Guido van Rossum08c16611997-08-02 03:01:42 +00001279
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001280 PyOS_setsig(SIGINT, old_siginthandler);
1281 old_siginthandler = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +00001282
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001283 for (i = 1; i < NSIG; i++) {
1284 func = Handlers[i].func;
1285 Handlers[i].tripped = 0;
1286 Handlers[i].func = NULL;
1287 if (i != SIGINT && func != NULL && func != Py_None &&
1288 func != DefaultHandler && func != IgnoreHandler)
1289 PyOS_setsig(i, SIG_DFL);
1290 Py_XDECREF(func);
1291 }
Guido van Rossum08c16611997-08-02 03:01:42 +00001292
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001293 Py_XDECREF(IntHandler);
1294 IntHandler = NULL;
1295 Py_XDECREF(DefaultHandler);
1296 DefaultHandler = NULL;
1297 Py_XDECREF(IgnoreHandler);
1298 IgnoreHandler = NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001299}
1300
Barry Warsaw92971171997-01-03 00:14:25 +00001301
Barry Warsaw92971171997-01-03 00:14:25 +00001302/* Declared in pyerrors.h */
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001303int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001304PyErr_CheckSignals(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001305{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001306 int i;
1307 PyObject *f;
Barry Warsaw92971171997-01-03 00:14:25 +00001308
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001309 if (!is_tripped)
1310 return 0;
Christian Heimesb76922a2007-12-11 01:06:40 +00001311
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001312#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001313 if (PyThread_get_thread_ident() != main_thread)
1314 return 0;
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001315#endif
Christian Heimesb76922a2007-12-11 01:06:40 +00001316
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001317 /*
1318 * The is_tripped variable is meant to speed up the calls to
1319 * PyErr_CheckSignals (both directly or via pending calls) when no
1320 * signal has arrived. This variable is set to 1 when a signal arrives
1321 * and it is set to 0 here, when we know some signals arrived. This way
1322 * we can run the registered handlers with no signals blocked.
1323 *
1324 * NOTE: with this approach we can have a situation where is_tripped is
1325 * 1 but we have no more signals to handle (Handlers[i].tripped
1326 * is 0 for every signal i). This won't do us any harm (except
1327 * we're gonna spent some cycles for nothing). This happens when
1328 * we receive a signal i after we zero is_tripped and before we
1329 * check Handlers[i].tripped.
1330 */
1331 is_tripped = 0;
Christian Heimesb76922a2007-12-11 01:06:40 +00001332
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001333 if (!(f = (PyObject *)PyEval_GetFrame()))
1334 f = Py_None;
Christian Heimesb76922a2007-12-11 01:06:40 +00001335
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001336 for (i = 1; i < NSIG; i++) {
1337 if (Handlers[i].tripped) {
1338 PyObject *result = NULL;
1339 PyObject *arglist = Py_BuildValue("(iO)", i, f);
1340 Handlers[i].tripped = 0;
Barry Warsaw92971171997-01-03 00:14:25 +00001341
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001342 if (arglist) {
1343 result = PyEval_CallObject(Handlers[i].func,
1344 arglist);
1345 Py_DECREF(arglist);
1346 }
1347 if (!result)
1348 return -1;
Barry Warsaw92971171997-01-03 00:14:25 +00001349
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001350 Py_DECREF(result);
1351 }
1352 }
Christian Heimesb76922a2007-12-11 01:06:40 +00001353
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001354 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001355}
1356
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +00001357
Barry Warsaw92971171997-01-03 00:14:25 +00001358/* Replacements for intrcheck.c functionality
1359 * Declared in pyerrors.h
1360 */
1361void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001362PyErr_SetInterrupt(void)
Barry Warsaw92971171997-01-03 00:14:25 +00001363{
Victor Stinner6c9b35b2011-04-18 16:25:56 +02001364 trip_signal(SIGINT);
Barry Warsaw92971171997-01-03 00:14:25 +00001365}
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001366
1367void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001368PyOS_InitInterrupts(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001369{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001370 PyObject *m = PyInit_signal();
1371 if (m) {
Victor Stinner49d3f252010-10-17 01:24:53 +00001372 _PyImport_FixupBuiltin(m, "signal");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001373 Py_DECREF(m);
1374 }
Guido van Rossum08c16611997-08-02 03:01:42 +00001375}
1376
1377void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001378PyOS_FiniInterrupts(void)
Guido van Rossum08c16611997-08-02 03:01:42 +00001379{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001380 finisignal();
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001381}
1382
1383int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001384PyOS_InterruptOccurred(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001385{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001386 if (Handlers[SIGINT].tripped) {
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001387#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001388 if (PyThread_get_thread_ident() != main_thread)
1389 return 0;
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001390#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001391 Handlers[SIGINT].tripped = 0;
1392 return 1;
1393 }
1394 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001395}
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001396
1397void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001398PyOS_AfterFork(void)
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001399{
1400#ifdef WITH_THREAD
Charles-François Natali6d0d24e2012-02-02 20:31:42 +01001401 /* PyThread_ReInitTLS() must be called early, to make sure that the TLS API
1402 * can be called safely. */
1403 PyThread_ReInitTLS();
Antoine Pitrou0c759fe2011-04-27 19:28:05 +02001404 _PyGILState_Reinit();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001405 PyEval_ReInitThreads();
1406 main_thread = PyThread_get_thread_ident();
1407 main_pid = getpid();
1408 _PyImport_ReInitLock();
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001409#endif
1410}
Antoine Pitrou6dd381e2011-11-21 21:26:56 +01001411
1412int
1413_PyOS_IsMainThread(void)
1414{
1415#ifdef WITH_THREAD
1416 return PyThread_get_thread_ident() == main_thread;
1417#else
1418 return 1;
1419#endif
1420}
1421
1422#ifdef MS_WINDOWS
1423void *_PyOS_SigintEvent(void)
1424{
1425 /* Returns a manual-reset event which gets tripped whenever
1426 SIGINT is received.
1427
1428 Python.h does not include windows.h so we do cannot use HANDLE
1429 as the return type of this function. We use void* instead. */
1430 return sigint_event;
1431}
1432#endif