blob: 6d27ab32912ff9f351ebd990da5b875da62eff0f [file] [log] [blame]
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002/* Signal module -- many thanks to Lance Ellinghaus */
Guido van Rossum398d9fe1994-05-11 08:59:13 +00003
Guido van Rossum644a12b1997-04-09 19:24:53 +00004/* XXX Signals should be recorded per thread, now we have thread state. */
5
Guido van Rossum602099a1994-09-14 13:32:22 +00006#include "Python.h"
Guido van Rossum398d9fe1994-05-11 08:59:13 +00007
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00008#ifdef MS_WINDOWS
Brian Curtineb24d742010-04-12 17:16:38 +00009#include <Windows.h>
Benjamin Peterson2614cda2010-03-21 22:36:19 +000010#ifdef HAVE_PROCESS_H
Guido van Rossum644a12b1997-04-09 19:24:53 +000011#include <process.h>
12#endif
Benjamin Peterson2614cda2010-03-21 22:36:19 +000013#endif
Guido van Rossum644a12b1997-04-09 19:24:53 +000014
Benjamin Peterson2614cda2010-03-21 22:36:19 +000015#ifdef HAVE_SIGNAL_H
Guido van Rossum398d9fe1994-05-11 08:59:13 +000016#include <signal.h>
Benjamin Peterson2614cda2010-03-21 22:36:19 +000017#endif
18#ifdef HAVE_SYS_STAT_H
Christian Heimes5fb7c2a2007-12-24 08:52:31 +000019#include <sys/stat.h>
Benjamin Peterson2614cda2010-03-21 22:36:19 +000020#endif
Martin v. Löwis3bf3cc02008-03-24 14:05:07 +000021#ifdef HAVE_SYS_TIME_H
Martin v. Löwis823725e2008-03-24 13:39:54 +000022#include <sys/time.h>
Martin v. Löwis3bf3cc02008-03-24 14:05:07 +000023#endif
Christian Heimes5fb7c2a2007-12-24 08:52:31 +000024
Victor Stinnera9293352011-04-30 15:21:58 +020025#if defined(HAVE_PTHREAD_SIGMASK) && !defined(HAVE_BROKEN_PTHREAD_SIGMASK)
26# define PYPTHREAD_SIGMASK
27#endif
28
29#if defined(PYPTHREAD_SIGMASK) && defined(HAVE_PTHREAD_H)
30# include <pthread.h>
31#endif
32
Guido van Rossumbb4ba121994-06-23 11:25:45 +000033#ifndef SIG_ERR
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +000034#define SIG_ERR ((PyOS_sighandler_t)(-1))
Guido van Rossumbb4ba121994-06-23 11:25:45 +000035#endif
36
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000037#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000038#define NSIG 12
39#include <process.h>
40#endif
41
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000042#ifndef NSIG
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000043# if defined(_NSIG)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000044# define NSIG _NSIG /* For BSD/SysV */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000045# elif defined(_SIGMAX)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000046# define NSIG (_SIGMAX + 1) /* For QNX */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000047# elif defined(SIGMAX)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000048# define NSIG (SIGMAX + 1) /* For djgpp */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000049# else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000050# define NSIG 64 /* Use a reasonable default value */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000051# endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000052#endif
53
54
Guido van Rossumbb4ba121994-06-23 11:25:45 +000055/*
56 NOTES ON THE INTERACTION BETWEEN SIGNALS AND THREADS
57
58 When threads are supported, we want the following semantics:
59
60 - only the main thread can set a signal handler
61 - any thread can get a signal handler
62 - signals are only delivered to the main thread
63
64 I.e. we don't support "synchronous signals" like SIGFPE (catching
65 this doesn't make much sense in Python anyway) nor do we support
66 signals as a means of inter-thread communication, since not all
67 thread implementations support that (at least our thread library
68 doesn't).
69
70 We still have the problem that in some implementations signals
71 generated by the keyboard (e.g. SIGINT) are delivered to all
72 threads (e.g. SGI), while in others (e.g. Solaris) such signals are
73 delivered to one random thread (an intermediate possibility would
Guido van Rossuma3c04b01995-01-12 11:29:01 +000074 be to deliver it to the main thread -- POSIX?). For now, we have
Guido van Rossumbb4ba121994-06-23 11:25:45 +000075 a working implementation that works in all three cases -- the
76 handler ignores signals if getpid() isn't the same as in the main
77 thread. XXX This is a hack.
78
Guido van Rossum9e8181b2000-09-19 00:46:46 +000079 GNU pth is a user-space threading library, and as such, all threads
80 run within the same process. In this case, if the currently running
81 thread is not the main_thread, send the signal to the main_thread.
Guido van Rossumbb4ba121994-06-23 11:25:45 +000082*/
83
84#ifdef WITH_THREAD
Guido van Rossum295b8e51997-06-06 21:16:41 +000085#include <sys/types.h> /* For pid_t */
Guido van Rossum49b56061998-10-01 20:42:43 +000086#include "pythread.h"
Guido van Rossumbb4ba121994-06-23 11:25:45 +000087static long main_thread;
88static pid_t main_pid;
89#endif
90
Victor Stinner2ec6b172011-05-15 10:21:59 +020091static volatile struct {
92 sig_atomic_t tripped;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000093 PyObject *func;
Barry Warsaw92971171997-01-03 00:14:25 +000094} Handlers[NSIG];
Guido van Rossum398d9fe1994-05-11 08:59:13 +000095
Victor Stinner2ec6b172011-05-15 10:21:59 +020096static volatile sig_atomic_t wakeup_fd = -1;
Christian Heimes5fb7c2a2007-12-24 08:52:31 +000097
Christian Heimesb76922a2007-12-11 01:06:40 +000098/* Speed up sigcheck() when none tripped */
99static volatile sig_atomic_t is_tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000100
Barry Warsaw92971171997-01-03 00:14:25 +0000101static PyObject *DefaultHandler;
102static PyObject *IgnoreHandler;
103static PyObject *IntHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000104
Martin v. Löwisf58de1b2001-03-06 12:13:56 +0000105/* On Solaris 8, gcc will produce a warning that the function
106 declaration is not a prototype. This is caused by the definition of
107 SIG_DFL as (void (*)())0; the correct declaration would have been
108 (void (*)(int))0. */
109
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000110static PyOS_sighandler_t old_siginthandler = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +0000111
Martin v. Löwis823725e2008-03-24 13:39:54 +0000112#ifdef HAVE_GETITIMER
113static PyObject *ItimerError;
114
115/* auxiliary functions for setitimer/getitimer */
116static void
117timeval_from_double(double d, struct timeval *tv)
118{
119 tv->tv_sec = floor(d);
120 tv->tv_usec = fmod(d, 1.0) * 1000000.0;
121}
122
Christian Heimes1a8501c2008-10-02 19:56:01 +0000123Py_LOCAL_INLINE(double)
Martin v. Löwis823725e2008-03-24 13:39:54 +0000124double_from_timeval(struct timeval *tv)
125{
126 return tv->tv_sec + (double)(tv->tv_usec / 1000000.0);
127}
128
129static PyObject *
130itimer_retval(struct itimerval *iv)
131{
132 PyObject *r, *v;
133
134 r = PyTuple_New(2);
135 if (r == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000136 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000137
138 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_value)))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000139 Py_DECREF(r);
140 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000141 }
142
143 PyTuple_SET_ITEM(r, 0, v);
144
145 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_interval)))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000146 Py_DECREF(r);
147 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000148 }
149
150 PyTuple_SET_ITEM(r, 1, v);
151
152 return r;
153}
154#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000155
Guido van Rossume4485b01994-09-07 14:32:49 +0000156static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000157signal_default_int_handler(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000158{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000159 PyErr_SetNone(PyExc_KeyboardInterrupt);
160 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000161}
162
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000163PyDoc_STRVAR(default_int_handler_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000164"default_int_handler(...)\n\
165\n\
Michael W. Hudson24ec2112004-06-17 15:55:53 +0000166The default handler for SIGINT installed by Python.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000167It raises KeyboardInterrupt.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000168
Thomas Wouters0796b002000-07-22 23:49:30 +0000169
170static int
171checksignals_witharg(void * unused)
172{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000173 return PyErr_CheckSignals();
Thomas Wouters0796b002000-07-22 23:49:30 +0000174}
175
Tim Peters4f1b2082000-07-23 21:18:09 +0000176static void
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200177trip_signal(int sig_num)
178{
Victor Stinnerd49b1f12011-05-08 02:03:15 +0200179 unsigned char byte;
Victor Stinnerc13ef662011-05-25 02:35:58 +0200180
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200181 Handlers[sig_num].tripped = 1;
Victor Stinnerc13ef662011-05-25 02:35:58 +0200182 if (wakeup_fd != -1) {
183 byte = (unsigned char)sig_num;
184 write(wakeup_fd, &byte, 1);
185 }
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200186 if (is_tripped)
187 return;
188 /* Set is_tripped after setting .tripped, as it gets
189 cleared in PyErr_CheckSignals() before .tripped. */
190 is_tripped = 1;
191 Py_AddPendingCall(checksignals_witharg, NULL);
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200192}
193
194static void
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000195signal_handler(int sig_num)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000196{
Antoine Pitrou39a65912010-11-05 19:47:27 +0000197 int save_errno = errno;
198
199#if defined(WITH_THREAD) && defined(WITH_PTH)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000200 if (PyThread_get_thread_ident() != main_thread) {
201 pth_raise(*(pth_t *) main_thread, sig_num);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000202 }
Antoine Pitrou39a65912010-11-05 19:47:27 +0000203 else
Guido van Rossum9e8181b2000-09-19 00:46:46 +0000204#endif
Antoine Pitrou39a65912010-11-05 19:47:27 +0000205 {
206#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000207 /* See NOTES section above */
Antoine Pitrou39a65912010-11-05 19:47:27 +0000208 if (getpid() == main_pid)
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000209#endif
Antoine Pitrou39a65912010-11-05 19:47:27 +0000210 {
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200211 trip_signal(sig_num);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000212 }
Antoine Pitrou39a65912010-11-05 19:47:27 +0000213
Jean-Paul Calderone6f137ca2010-05-09 03:18:57 +0000214#ifndef HAVE_SIGACTION
Antoine Pitrou39a65912010-11-05 19:47:27 +0000215#ifdef SIGCHLD
216 /* To avoid infinite recursion, this signal remains
217 reset until explicit re-instated.
218 Don't clear the 'func' field as it is our pointer
219 to the Python handler... */
220 if (sig_num != SIGCHLD)
221#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000222 /* If the handler was not set up with sigaction, reinstall it. See
223 * Python/pythonrun.c for the implementation of PyOS_setsig which
224 * makes this true. See also issue8354. */
225 PyOS_setsig(sig_num, signal_handler);
Jean-Paul Calderone6f137ca2010-05-09 03:18:57 +0000226#endif
Antoine Pitrou39a65912010-11-05 19:47:27 +0000227 }
228
229 /* Issue #10311: asynchronously executing signal handlers should not
230 mutate errno under the feet of unsuspecting C code. */
231 errno = save_errno;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000232}
Guido van Rossume4485b01994-09-07 14:32:49 +0000233
Guido van Rossum06d511d1995-03-10 15:13:48 +0000234
Guido van Rossum1171ee61997-08-22 20:42:00 +0000235#ifdef HAVE_ALARM
Guido van Rossume4485b01994-09-07 14:32:49 +0000236static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000237signal_alarm(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000238{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000239 int t;
240 if (!PyArg_ParseTuple(args, "i:alarm", &t))
241 return NULL;
242 /* alarm() returns the number of seconds remaining */
243 return PyLong_FromLong((long)alarm(t));
Guido van Rossumaa0f4c71994-08-23 13:49:37 +0000244}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000245
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000246PyDoc_STRVAR(alarm_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000247"alarm(seconds)\n\
248\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000249Arrange for SIGALRM to arrive after the given number of seconds.");
Guido van Rossum06d511d1995-03-10 15:13:48 +0000250#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000251
Guido van Rossum1171ee61997-08-22 20:42:00 +0000252#ifdef HAVE_PAUSE
Guido van Rossuma597dde1995-01-10 20:56:29 +0000253static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000254signal_pause(PyObject *self)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000255{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000256 Py_BEGIN_ALLOW_THREADS
257 (void)pause();
258 Py_END_ALLOW_THREADS
259 /* make sure that any exceptions that got raised are propagated
260 * back into Python
261 */
262 if (PyErr_CheckSignals())
263 return NULL;
Barry Warsaw92971171997-01-03 00:14:25 +0000264
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000265 Py_INCREF(Py_None);
266 return Py_None;
Guido van Rossume4485b01994-09-07 14:32:49 +0000267}
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000268PyDoc_STRVAR(pause_doc,
Barry Warsaw1ee36ff1998-07-21 22:41:18 +0000269"pause()\n\
270\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000271Wait until a signal arrives.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000272
Guido van Rossum06d511d1995-03-10 15:13:48 +0000273#endif
Guido van Rossume4485b01994-09-07 14:32:49 +0000274
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000275
Guido van Rossume4485b01994-09-07 14:32:49 +0000276static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000277signal_signal(PyObject *self, PyObject *args)
Guido van Rossume4485b01994-09-07 14:32:49 +0000278{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000279 PyObject *obj;
280 int sig_num;
281 PyObject *old_handler;
282 void (*func)(int);
283 if (!PyArg_ParseTuple(args, "iO:signal", &sig_num, &obj))
284 return NULL;
Brian Curtinef9efbd2010-08-06 19:27:32 +0000285#ifdef MS_WINDOWS
286 /* Validate that sig_num is one of the allowable signals */
Brian Curtinc734b312010-09-06 16:04:10 +0000287 switch (sig_num) {
288 case SIGABRT: break;
Brian Curtin9e88b5a2010-10-01 14:49:24 +0000289#ifdef SIGBREAK
290 /* Issue #10003: SIGBREAK is not documented as permitted, but works
291 and corresponds to CTRL_BREAK_EVENT. */
292 case SIGBREAK: break;
293#endif
Brian Curtinc734b312010-09-06 16:04:10 +0000294 case SIGFPE: break;
295 case SIGILL: break;
296 case SIGINT: break;
297 case SIGSEGV: break;
298 case SIGTERM: break;
299 default:
300 PyErr_SetString(PyExc_ValueError, "invalid signal value");
301 return NULL;
Brian Curtinef9efbd2010-08-06 19:27:32 +0000302 }
303#endif
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000304#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000305 if (PyThread_get_thread_ident() != main_thread) {
306 PyErr_SetString(PyExc_ValueError,
307 "signal only works in main thread");
308 return NULL;
309 }
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000310#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000311 if (sig_num < 1 || sig_num >= NSIG) {
312 PyErr_SetString(PyExc_ValueError,
313 "signal number out of range");
314 return NULL;
315 }
316 if (obj == IgnoreHandler)
317 func = SIG_IGN;
318 else if (obj == DefaultHandler)
319 func = SIG_DFL;
320 else if (!PyCallable_Check(obj)) {
321 PyErr_SetString(PyExc_TypeError,
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000322"signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000323 return NULL;
324 }
325 else
326 func = signal_handler;
327 if (PyOS_setsig(sig_num, func) == SIG_ERR) {
Victor Stinner388196e2011-05-10 17:13:00 +0200328 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000329 return NULL;
330 }
331 old_handler = Handlers[sig_num].func;
332 Handlers[sig_num].tripped = 0;
333 Py_INCREF(obj);
334 Handlers[sig_num].func = obj;
335 return old_handler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000336}
337
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000338PyDoc_STRVAR(signal_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000339"signal(sig, action) -> action\n\
340\n\
341Set the action for the given signal. The action can be SIG_DFL,\n\
342SIG_IGN, or a callable Python object. The previous action is\n\
343returned. See getsignal() for possible return values.\n\
344\n\
345*** IMPORTANT NOTICE ***\n\
346A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000347the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000348
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000349
Guido van Rossume4485b01994-09-07 14:32:49 +0000350static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000351signal_getsignal(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000352{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000353 int sig_num;
354 PyObject *old_handler;
355 if (!PyArg_ParseTuple(args, "i:getsignal", &sig_num))
356 return NULL;
357 if (sig_num < 1 || sig_num >= NSIG) {
358 PyErr_SetString(PyExc_ValueError,
359 "signal number out of range");
360 return NULL;
361 }
362 old_handler = Handlers[sig_num].func;
363 Py_INCREF(old_handler);
364 return old_handler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000365}
366
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000367PyDoc_STRVAR(getsignal_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000368"getsignal(sig) -> action\n\
369\n\
370Return the current action for the given signal. The return value can be:\n\
371SIG_IGN -- if the signal is being ignored\n\
372SIG_DFL -- if the default action for the signal is in effect\n\
373None -- if an unknown handler is in effect\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000374anything else -- the callable Python object used as a handler");
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000375
Christian Heimes8640e742008-02-23 16:23:06 +0000376#ifdef HAVE_SIGINTERRUPT
377PyDoc_STRVAR(siginterrupt_doc,
378"siginterrupt(sig, flag) -> None\n\
379change system call restart behaviour: if flag is False, system calls\n\
380will be restarted when interrupted by signal sig, else system calls\n\
381will be interrupted.");
382
383static PyObject *
384signal_siginterrupt(PyObject *self, PyObject *args)
385{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000386 int sig_num;
387 int flag;
Christian Heimes8640e742008-02-23 16:23:06 +0000388
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000389 if (!PyArg_ParseTuple(args, "ii:siginterrupt", &sig_num, &flag))
390 return NULL;
391 if (sig_num < 1 || sig_num >= NSIG) {
392 PyErr_SetString(PyExc_ValueError,
393 "signal number out of range");
394 return NULL;
395 }
396 if (siginterrupt(sig_num, flag)<0) {
Victor Stinner388196e2011-05-10 17:13:00 +0200397 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000398 return NULL;
399 }
Christian Heimes8640e742008-02-23 16:23:06 +0000400
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000401 Py_INCREF(Py_None);
402 return Py_None;
Christian Heimes8640e742008-02-23 16:23:06 +0000403}
404
405#endif
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000406
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000407static PyObject *
408signal_set_wakeup_fd(PyObject *self, PyObject *args)
409{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000410 struct stat buf;
411 int fd, old_fd;
412 if (!PyArg_ParseTuple(args, "i:set_wakeup_fd", &fd))
413 return NULL;
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000414#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000415 if (PyThread_get_thread_ident() != main_thread) {
416 PyErr_SetString(PyExc_ValueError,
417 "set_wakeup_fd only works in main thread");
418 return NULL;
419 }
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000420#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000421 if (fd != -1 && fstat(fd, &buf) != 0) {
422 PyErr_SetString(PyExc_ValueError, "invalid fd");
423 return NULL;
424 }
425 old_fd = wakeup_fd;
426 wakeup_fd = fd;
427 return PyLong_FromLong(old_fd);
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000428}
429
430PyDoc_STRVAR(set_wakeup_fd_doc,
431"set_wakeup_fd(fd) -> fd\n\
432\n\
433Sets the fd to be written to (with '\\0') when a signal\n\
434comes in. A library can use this to wakeup select or poll.\n\
435The previous fd is returned.\n\
436\n\
437The fd must be non-blocking.");
438
439/* C API for the same, without all the error checking */
440int
441PySignal_SetWakeupFd(int fd)
442{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000443 int old_fd = wakeup_fd;
444 if (fd < 0)
445 fd = -1;
446 wakeup_fd = fd;
447 return old_fd;
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000448}
449
450
Martin v. Löwis823725e2008-03-24 13:39:54 +0000451#ifdef HAVE_SETITIMER
452static PyObject *
453signal_setitimer(PyObject *self, PyObject *args)
454{
455 double first;
456 double interval = 0;
457 int which;
458 struct itimerval new, old;
459
460 if(!PyArg_ParseTuple(args, "id|d:setitimer", &which, &first, &interval))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000461 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000462
463 timeval_from_double(first, &new.it_value);
464 timeval_from_double(interval, &new.it_interval);
465 /* Let OS check "which" value */
466 if (setitimer(which, &new, &old) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000467 PyErr_SetFromErrno(ItimerError);
468 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000469 }
470
471 return itimer_retval(&old);
472}
473
474PyDoc_STRVAR(setitimer_doc,
475"setitimer(which, seconds[, interval])\n\
476\n\
477Sets given itimer (one of ITIMER_REAL, ITIMER_VIRTUAL\n\
478or ITIMER_PROF) to fire after value seconds and after\n\
479that every interval seconds.\n\
480The itimer can be cleared by setting seconds to zero.\n\
481\n\
482Returns old values as a tuple: (delay, interval).");
483#endif
484
485
486#ifdef HAVE_GETITIMER
487static PyObject *
488signal_getitimer(PyObject *self, PyObject *args)
489{
490 int which;
491 struct itimerval old;
492
493 if (!PyArg_ParseTuple(args, "i:getitimer", &which))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000494 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000495
496 if (getitimer(which, &old) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 PyErr_SetFromErrno(ItimerError);
498 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000499 }
500
501 return itimer_retval(&old);
502}
503
504PyDoc_STRVAR(getitimer_doc,
505"getitimer(which)\n\
506\n\
507Returns current value of given itimer.");
508#endif
509
Victor Stinnerb3e72192011-05-08 01:46:11 +0200510#if defined(PYPTHREAD_SIGMASK) || defined(HAVE_SIGWAIT)
Victor Stinnera9293352011-04-30 15:21:58 +0200511/* Convert an iterable to a sigset.
512 Return 0 on success, return -1 and raise an exception on error. */
513
514static int
515iterable_to_sigset(PyObject *iterable, sigset_t *mask)
516{
517 int result = -1;
518 PyObject *iterator, *item;
519 long signum;
520 int err;
521
522 sigemptyset(mask);
523
524 iterator = PyObject_GetIter(iterable);
525 if (iterator == NULL)
526 goto error;
527
528 while (1)
529 {
530 item = PyIter_Next(iterator);
531 if (item == NULL) {
532 if (PyErr_Occurred())
533 goto error;
534 else
535 break;
536 }
537
538 signum = PyLong_AsLong(item);
539 Py_DECREF(item);
540 if (signum == -1 && PyErr_Occurred())
541 goto error;
542 if (0 < signum && signum < NSIG)
543 err = sigaddset(mask, (int)signum);
544 else
545 err = 1;
546 if (err) {
547 PyErr_Format(PyExc_ValueError,
548 "signal number %ld out of range", signum);
549 goto error;
550 }
551 }
552 result = 0;
553
554error:
555 Py_XDECREF(iterator);
556 return result;
557}
Victor Stinnerb3e72192011-05-08 01:46:11 +0200558#endif
Victor Stinnera9293352011-04-30 15:21:58 +0200559
Victor Stinnerb3e72192011-05-08 01:46:11 +0200560#if defined(PYPTHREAD_SIGMASK) || defined(HAVE_SIGPENDING)
Victor Stinner35b300c2011-05-04 13:20:35 +0200561static PyObject*
562sigset_to_set(sigset_t mask)
563{
564 PyObject *signum, *result;
565 int sig;
566
567 result = PySet_New(0);
568 if (result == NULL)
569 return NULL;
570
571 for (sig = 1; sig < NSIG; sig++) {
572 if (sigismember(&mask, sig) != 1)
573 continue;
574
575 /* Handle the case where it is a member by adding the signal to
576 the result list. Ignore the other cases because they mean the
577 signal isn't a member of the mask or the signal was invalid,
578 and an invalid signal must have been our fault in constructing
579 the loop boundaries. */
580 signum = PyLong_FromLong(sig);
581 if (signum == NULL) {
582 Py_DECREF(result);
583 return NULL;
584 }
585 if (PySet_Add(result, signum) == -1) {
586 Py_DECREF(signum);
587 Py_DECREF(result);
588 return NULL;
589 }
590 Py_DECREF(signum);
591 }
592 return result;
593}
Victor Stinnerb3e72192011-05-08 01:46:11 +0200594#endif
Victor Stinner35b300c2011-05-04 13:20:35 +0200595
Victor Stinnerb3e72192011-05-08 01:46:11 +0200596#ifdef PYPTHREAD_SIGMASK
Victor Stinnera9293352011-04-30 15:21:58 +0200597static PyObject *
598signal_pthread_sigmask(PyObject *self, PyObject *args)
599{
Victor Stinner35b300c2011-05-04 13:20:35 +0200600 int how;
601 PyObject *signals;
Victor Stinnera9293352011-04-30 15:21:58 +0200602 sigset_t mask, previous;
603 int err;
604
605 if (!PyArg_ParseTuple(args, "iO:pthread_sigmask", &how, &signals))
606 return NULL;
607
608 if (iterable_to_sigset(signals, &mask))
609 return NULL;
610
611 err = pthread_sigmask(how, &mask, &previous);
612 if (err != 0) {
613 errno = err;
Victor Stinnerb3e72192011-05-08 01:46:11 +0200614 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnera9293352011-04-30 15:21:58 +0200615 return NULL;
616 }
617
Victor Stinnerd0e516d2011-05-03 14:57:12 +0200618 /* if signals was unblocked, signal handlers have been called */
619 if (PyErr_CheckSignals())
620 return NULL;
621
Victor Stinner35b300c2011-05-04 13:20:35 +0200622 return sigset_to_set(previous);
Victor Stinnera9293352011-04-30 15:21:58 +0200623}
624
625PyDoc_STRVAR(signal_pthread_sigmask_doc,
626"pthread_sigmask(how, mask) -> old mask\n\
627\n\
628Fetch and/or change the signal mask of the calling thread.");
629#endif /* #ifdef PYPTHREAD_SIGMASK */
630
Martin v. Löwis823725e2008-03-24 13:39:54 +0000631
Victor Stinnerb3e72192011-05-08 01:46:11 +0200632#ifdef HAVE_SIGPENDING
633static PyObject *
634signal_sigpending(PyObject *self)
635{
636 int err;
637 sigset_t mask;
638 err = sigpending(&mask);
639 if (err)
640 return PyErr_SetFromErrno(PyExc_OSError);
641 return sigset_to_set(mask);
642}
643
644PyDoc_STRVAR(signal_sigpending_doc,
645"sigpending() -> list\n\
646\n\
647Examine pending signals.");
648#endif /* #ifdef HAVE_SIGPENDING */
649
650
651#ifdef HAVE_SIGWAIT
652static PyObject *
653signal_sigwait(PyObject *self, PyObject *args)
654{
655 PyObject *signals;
656 sigset_t set;
657 int err, signum;
658
659 if (!PyArg_ParseTuple(args, "O:sigwait", &signals))
660 return NULL;
661
662 if (iterable_to_sigset(signals, &set))
663 return NULL;
664
665 err = sigwait(&set, &signum);
666 if (err) {
667 errno = err;
668 return PyErr_SetFromErrno(PyExc_OSError);
669 }
670
671 return PyLong_FromLong(signum);
672}
673
674PyDoc_STRVAR(signal_sigwait_doc,
675"sigwait(sigset) -> signum\n\
676\n\
677Wait a signal.");
678#endif /* #ifdef HAVE_SIGPENDING */
679
680
681#if defined(HAVE_PTHREAD_KILL) && defined(WITH_THREAD)
682static PyObject *
683signal_pthread_kill(PyObject *self, PyObject *args)
684{
685 long tid;
686 int signum;
687 int err;
688
689 if (!PyArg_ParseTuple(args, "li:pthread_kill", &tid, &signum))
690 return NULL;
691
Victor Stinner86e104a2011-05-09 14:45:38 +0200692 err = pthread_kill((pthread_t)tid, signum);
Victor Stinnerb3e72192011-05-08 01:46:11 +0200693 if (err != 0) {
694 errno = err;
695 PyErr_SetFromErrno(PyExc_OSError);
696 return NULL;
697 }
698
699 /* the signal may have been send to the current thread */
700 if (PyErr_CheckSignals())
701 return NULL;
702
703 Py_RETURN_NONE;
704}
705
706PyDoc_STRVAR(signal_pthread_kill_doc,
707"pthread_kill(thread_id, signum)\n\
708\n\
709Send a signal to a thread.");
710#endif /* #if defined(HAVE_PTHREAD_KILL) && defined(WITH_THREAD) */
711
712
713
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000714/* List of functions defined in the module */
Barry Warsaw92971171997-01-03 00:14:25 +0000715static PyMethodDef signal_methods[] = {
Guido van Rossum1171ee61997-08-22 20:42:00 +0000716#ifdef HAVE_ALARM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000717 {"alarm", signal_alarm, METH_VARARGS, alarm_doc},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000718#endif
Martin v. Löwis823725e2008-03-24 13:39:54 +0000719#ifdef HAVE_SETITIMER
720 {"setitimer", signal_setitimer, METH_VARARGS, setitimer_doc},
721#endif
722#ifdef HAVE_GETITIMER
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000723 {"getitimer", signal_getitimer, METH_VARARGS, getitimer_doc},
Martin v. Löwis823725e2008-03-24 13:39:54 +0000724#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000725 {"signal", signal_signal, METH_VARARGS, signal_doc},
726 {"getsignal", signal_getsignal, METH_VARARGS, getsignal_doc},
727 {"set_wakeup_fd", signal_set_wakeup_fd, METH_VARARGS, set_wakeup_fd_doc},
Christian Heimes8640e742008-02-23 16:23:06 +0000728#ifdef HAVE_SIGINTERRUPT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000729 {"siginterrupt", signal_siginterrupt, METH_VARARGS, siginterrupt_doc},
Christian Heimes8640e742008-02-23 16:23:06 +0000730#endif
Guido van Rossum1171ee61997-08-22 20:42:00 +0000731#ifdef HAVE_PAUSE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000732 {"pause", (PyCFunction)signal_pause,
Victor Stinnera9293352011-04-30 15:21:58 +0200733 METH_NOARGS, pause_doc},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000734#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000735 {"default_int_handler", signal_default_int_handler,
736 METH_VARARGS, default_int_handler_doc},
Victor Stinnerb3e72192011-05-08 01:46:11 +0200737#if defined(HAVE_PTHREAD_KILL) && defined(WITH_THREAD)
738 {"pthread_kill", (PyCFunction)signal_pthread_kill,
739 METH_VARARGS, signal_pthread_kill_doc},
740#endif
Victor Stinnera9293352011-04-30 15:21:58 +0200741#ifdef PYPTHREAD_SIGMASK
742 {"pthread_sigmask", (PyCFunction)signal_pthread_sigmask,
743 METH_VARARGS, signal_pthread_sigmask_doc},
744#endif
Victor Stinnerb3e72192011-05-08 01:46:11 +0200745#ifdef HAVE_SIGPENDING
746 {"sigpending", (PyCFunction)signal_sigpending,
747 METH_NOARGS, signal_sigpending_doc},
748#endif
749#ifdef HAVE_SIGWAIT
750 {"sigwait", (PyCFunction)signal_sigwait,
751 METH_VARARGS, signal_sigwait_doc},
752#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000753 {NULL, NULL} /* sentinel */
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000754};
755
Barry Warsaw92971171997-01-03 00:14:25 +0000756
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000757PyDoc_STRVAR(module_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000758"This module provides mechanisms to use signal handlers in Python.\n\
759\n\
760Functions:\n\
761\n\
762alarm() -- cause SIGALRM after a specified time [Unix only]\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +0000763setitimer() -- cause a signal (described below) after a specified\n\
764 float time and the timer may restart then [Unix only]\n\
765getitimer() -- get current value of timer [Unix only]\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000766signal() -- set the action for a given signal\n\
767getsignal() -- get the signal action for a given signal\n\
768pause() -- wait until a signal arrives [Unix only]\n\
769default_int_handler() -- default SIGINT handler\n\
770\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +0000771signal constants:\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000772SIG_DFL -- used to refer to the system default handler\n\
773SIG_IGN -- used to ignore the signal\n\
774NSIG -- number of defined signals\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000775SIGINT, SIGTERM, etc. -- signal numbers\n\
776\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +0000777itimer constants:\n\
778ITIMER_REAL -- decrements in real time, and delivers SIGALRM upon\n\
779 expiration\n\
780ITIMER_VIRTUAL -- decrements only when the process is executing,\n\
781 and delivers SIGVTALRM upon expiration\n\
782ITIMER_PROF -- decrements both when the process is executing and\n\
783 when the system is executing on behalf of the process.\n\
784 Coupled with ITIMER_VIRTUAL, this timer is usually\n\
785 used to profile the time spent by the application\n\
786 in user and kernel space. SIGPROF is delivered upon\n\
787 expiration.\n\
788\n\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000789*** IMPORTANT NOTICE ***\n\
790A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000791the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000792
Martin v. Löwis1a214512008-06-11 05:26:20 +0000793static struct PyModuleDef signalmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000794 PyModuleDef_HEAD_INIT,
795 "signal",
796 module_doc,
797 -1,
798 signal_methods,
799 NULL,
800 NULL,
801 NULL,
802 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000803};
804
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000805PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000806PyInit_signal(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000807{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000808 PyObject *m, *d, *x;
809 int i;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000810
811#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000812 main_thread = PyThread_get_thread_ident();
813 main_pid = getpid();
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000814#endif
815
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000816 /* Create the module and add the functions */
817 m = PyModule_Create(&signalmodule);
818 if (m == NULL)
819 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000820
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000821 /* Add some symbolic constants to the module */
822 d = PyModule_GetDict(m);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000823
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000824 x = DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL);
825 if (!x || PyDict_SetItemString(d, "SIG_DFL", x) < 0)
826 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000827
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000828 x = IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN);
829 if (!x || PyDict_SetItemString(d, "SIG_IGN", x) < 0)
830 goto finally;
Barry Warsaw92971171997-01-03 00:14:25 +0000831
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000832 x = PyLong_FromLong((long)NSIG);
833 if (!x || PyDict_SetItemString(d, "NSIG", x) < 0)
834 goto finally;
835 Py_DECREF(x);
Barry Warsaw92971171997-01-03 00:14:25 +0000836
Victor Stinnera9293352011-04-30 15:21:58 +0200837#ifdef SIG_BLOCK
Victor Stinner72c53b52011-05-02 16:15:43 +0200838 if (PyModule_AddIntMacro(m, SIG_BLOCK))
839 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +0200840#endif
Victor Stinnera9293352011-04-30 15:21:58 +0200841#ifdef SIG_UNBLOCK
Victor Stinner72c53b52011-05-02 16:15:43 +0200842 if (PyModule_AddIntMacro(m, SIG_UNBLOCK))
843 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +0200844#endif
Victor Stinnera9293352011-04-30 15:21:58 +0200845#ifdef SIG_SETMASK
Victor Stinner72c53b52011-05-02 16:15:43 +0200846 if (PyModule_AddIntMacro(m, SIG_SETMASK))
847 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +0200848#endif
849
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000850 x = IntHandler = PyDict_GetItemString(d, "default_int_handler");
851 if (!x)
852 goto finally;
853 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +0000854
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000855 Handlers[0].tripped = 0;
856 for (i = 1; i < NSIG; i++) {
857 void (*t)(int);
858 t = PyOS_getsig(i);
859 Handlers[i].tripped = 0;
860 if (t == SIG_DFL)
861 Handlers[i].func = DefaultHandler;
862 else if (t == SIG_IGN)
863 Handlers[i].func = IgnoreHandler;
864 else
865 Handlers[i].func = Py_None; /* None of our business */
866 Py_INCREF(Handlers[i].func);
867 }
868 if (Handlers[SIGINT].func == DefaultHandler) {
869 /* Install default int handler */
870 Py_INCREF(IntHandler);
871 Py_DECREF(Handlers[SIGINT].func);
872 Handlers[SIGINT].func = IntHandler;
873 old_siginthandler = PyOS_setsig(SIGINT, signal_handler);
874 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000875
876#ifdef SIGHUP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000877 x = PyLong_FromLong(SIGHUP);
878 PyDict_SetItemString(d, "SIGHUP", x);
879 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000880#endif
881#ifdef SIGINT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000882 x = PyLong_FromLong(SIGINT);
883 PyDict_SetItemString(d, "SIGINT", x);
884 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000885#endif
Tim Peters1ce3cf72001-10-01 17:58:40 +0000886#ifdef SIGBREAK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000887 x = PyLong_FromLong(SIGBREAK);
888 PyDict_SetItemString(d, "SIGBREAK", x);
889 Py_XDECREF(x);
Tim Peters1ce3cf72001-10-01 17:58:40 +0000890#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000891#ifdef SIGQUIT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000892 x = PyLong_FromLong(SIGQUIT);
893 PyDict_SetItemString(d, "SIGQUIT", x);
894 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000895#endif
896#ifdef SIGILL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000897 x = PyLong_FromLong(SIGILL);
898 PyDict_SetItemString(d, "SIGILL", x);
899 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000900#endif
901#ifdef SIGTRAP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000902 x = PyLong_FromLong(SIGTRAP);
903 PyDict_SetItemString(d, "SIGTRAP", x);
904 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000905#endif
906#ifdef SIGIOT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000907 x = PyLong_FromLong(SIGIOT);
908 PyDict_SetItemString(d, "SIGIOT", x);
909 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000910#endif
911#ifdef SIGABRT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000912 x = PyLong_FromLong(SIGABRT);
913 PyDict_SetItemString(d, "SIGABRT", x);
914 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000915#endif
916#ifdef SIGEMT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000917 x = PyLong_FromLong(SIGEMT);
918 PyDict_SetItemString(d, "SIGEMT", x);
919 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000920#endif
921#ifdef SIGFPE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000922 x = PyLong_FromLong(SIGFPE);
923 PyDict_SetItemString(d, "SIGFPE", x);
924 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000925#endif
926#ifdef SIGKILL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000927 x = PyLong_FromLong(SIGKILL);
928 PyDict_SetItemString(d, "SIGKILL", x);
929 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000930#endif
931#ifdef SIGBUS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000932 x = PyLong_FromLong(SIGBUS);
933 PyDict_SetItemString(d, "SIGBUS", x);
934 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000935#endif
936#ifdef SIGSEGV
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000937 x = PyLong_FromLong(SIGSEGV);
938 PyDict_SetItemString(d, "SIGSEGV", x);
939 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000940#endif
941#ifdef SIGSYS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000942 x = PyLong_FromLong(SIGSYS);
943 PyDict_SetItemString(d, "SIGSYS", x);
944 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000945#endif
946#ifdef SIGPIPE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000947 x = PyLong_FromLong(SIGPIPE);
948 PyDict_SetItemString(d, "SIGPIPE", x);
949 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000950#endif
951#ifdef SIGALRM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000952 x = PyLong_FromLong(SIGALRM);
953 PyDict_SetItemString(d, "SIGALRM", x);
954 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000955#endif
956#ifdef SIGTERM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000957 x = PyLong_FromLong(SIGTERM);
958 PyDict_SetItemString(d, "SIGTERM", x);
959 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000960#endif
961#ifdef SIGUSR1
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000962 x = PyLong_FromLong(SIGUSR1);
963 PyDict_SetItemString(d, "SIGUSR1", x);
964 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000965#endif
966#ifdef SIGUSR2
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000967 x = PyLong_FromLong(SIGUSR2);
968 PyDict_SetItemString(d, "SIGUSR2", x);
969 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000970#endif
971#ifdef SIGCLD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000972 x = PyLong_FromLong(SIGCLD);
973 PyDict_SetItemString(d, "SIGCLD", x);
974 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000975#endif
976#ifdef SIGCHLD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000977 x = PyLong_FromLong(SIGCHLD);
978 PyDict_SetItemString(d, "SIGCHLD", x);
979 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000980#endif
981#ifdef SIGPWR
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000982 x = PyLong_FromLong(SIGPWR);
983 PyDict_SetItemString(d, "SIGPWR", x);
984 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000985#endif
986#ifdef SIGIO
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000987 x = PyLong_FromLong(SIGIO);
988 PyDict_SetItemString(d, "SIGIO", x);
989 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000990#endif
991#ifdef SIGURG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000992 x = PyLong_FromLong(SIGURG);
993 PyDict_SetItemString(d, "SIGURG", x);
994 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000995#endif
996#ifdef SIGWINCH
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000997 x = PyLong_FromLong(SIGWINCH);
998 PyDict_SetItemString(d, "SIGWINCH", x);
999 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001000#endif
1001#ifdef SIGPOLL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001002 x = PyLong_FromLong(SIGPOLL);
1003 PyDict_SetItemString(d, "SIGPOLL", x);
1004 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001005#endif
1006#ifdef SIGSTOP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001007 x = PyLong_FromLong(SIGSTOP);
1008 PyDict_SetItemString(d, "SIGSTOP", x);
1009 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001010#endif
1011#ifdef SIGTSTP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001012 x = PyLong_FromLong(SIGTSTP);
1013 PyDict_SetItemString(d, "SIGTSTP", x);
1014 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001015#endif
1016#ifdef SIGCONT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001017 x = PyLong_FromLong(SIGCONT);
1018 PyDict_SetItemString(d, "SIGCONT", x);
1019 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001020#endif
1021#ifdef SIGTTIN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001022 x = PyLong_FromLong(SIGTTIN);
1023 PyDict_SetItemString(d, "SIGTTIN", x);
1024 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001025#endif
1026#ifdef SIGTTOU
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001027 x = PyLong_FromLong(SIGTTOU);
1028 PyDict_SetItemString(d, "SIGTTOU", x);
1029 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001030#endif
1031#ifdef SIGVTALRM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001032 x = PyLong_FromLong(SIGVTALRM);
1033 PyDict_SetItemString(d, "SIGVTALRM", x);
1034 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001035#endif
1036#ifdef SIGPROF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001037 x = PyLong_FromLong(SIGPROF);
1038 PyDict_SetItemString(d, "SIGPROF", x);
1039 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001040#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001041#ifdef SIGXCPU
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001042 x = PyLong_FromLong(SIGXCPU);
1043 PyDict_SetItemString(d, "SIGXCPU", x);
1044 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001045#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001046#ifdef SIGXFSZ
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001047 x = PyLong_FromLong(SIGXFSZ);
1048 PyDict_SetItemString(d, "SIGXFSZ", x);
1049 Py_XDECREF(x);
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001050#endif
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001051#ifdef SIGRTMIN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001052 x = PyLong_FromLong(SIGRTMIN);
1053 PyDict_SetItemString(d, "SIGRTMIN", x);
1054 Py_XDECREF(x);
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001055#endif
1056#ifdef SIGRTMAX
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001057 x = PyLong_FromLong(SIGRTMAX);
1058 PyDict_SetItemString(d, "SIGRTMAX", x);
1059 Py_XDECREF(x);
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001060#endif
Martin v. Löwis175af252002-01-12 11:43:25 +00001061#ifdef SIGINFO
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001062 x = PyLong_FromLong(SIGINFO);
1063 PyDict_SetItemString(d, "SIGINFO", x);
1064 Py_XDECREF(x);
Martin v. Löwis175af252002-01-12 11:43:25 +00001065#endif
Martin v. Löwis823725e2008-03-24 13:39:54 +00001066
1067#ifdef ITIMER_REAL
1068 x = PyLong_FromLong(ITIMER_REAL);
1069 PyDict_SetItemString(d, "ITIMER_REAL", x);
1070 Py_DECREF(x);
1071#endif
1072#ifdef ITIMER_VIRTUAL
1073 x = PyLong_FromLong(ITIMER_VIRTUAL);
1074 PyDict_SetItemString(d, "ITIMER_VIRTUAL", x);
1075 Py_DECREF(x);
1076#endif
1077#ifdef ITIMER_PROF
1078 x = PyLong_FromLong(ITIMER_PROF);
1079 PyDict_SetItemString(d, "ITIMER_PROF", x);
1080 Py_DECREF(x);
1081#endif
1082
1083#if defined (HAVE_SETITIMER) || defined (HAVE_GETITIMER)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001084 ItimerError = PyErr_NewException("signal.ItimerError",
1085 PyExc_IOError, NULL);
Neal Norwitz2f99b242008-08-24 05:48:10 +00001086 if (ItimerError != NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001087 PyDict_SetItemString(d, "ItimerError", ItimerError);
Martin v. Löwis823725e2008-03-24 13:39:54 +00001088#endif
1089
Brian Curtineb24d742010-04-12 17:16:38 +00001090#ifdef CTRL_C_EVENT
1091 x = PyLong_FromLong(CTRL_C_EVENT);
1092 PyDict_SetItemString(d, "CTRL_C_EVENT", x);
1093 Py_DECREF(x);
1094#endif
1095
1096#ifdef CTRL_BREAK_EVENT
1097 x = PyLong_FromLong(CTRL_BREAK_EVENT);
1098 PyDict_SetItemString(d, "CTRL_BREAK_EVENT", x);
1099 Py_DECREF(x);
1100#endif
1101
Martin v. Löwis1a214512008-06-11 05:26:20 +00001102 if (PyErr_Occurred()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001103 Py_DECREF(m);
1104 m = NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001105 }
Barry Warsaw92971171997-01-03 00:14:25 +00001106
Barry Warsaw92971171997-01-03 00:14:25 +00001107 finally:
Martin v. Löwis1a214512008-06-11 05:26:20 +00001108 return m;
Guido van Rossum08c16611997-08-02 03:01:42 +00001109}
1110
1111static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001112finisignal(void)
Guido van Rossum08c16611997-08-02 03:01:42 +00001113{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001114 int i;
1115 PyObject *func;
Guido van Rossum08c16611997-08-02 03:01:42 +00001116
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001117 PyOS_setsig(SIGINT, old_siginthandler);
1118 old_siginthandler = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +00001119
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001120 for (i = 1; i < NSIG; i++) {
1121 func = Handlers[i].func;
1122 Handlers[i].tripped = 0;
1123 Handlers[i].func = NULL;
1124 if (i != SIGINT && func != NULL && func != Py_None &&
1125 func != DefaultHandler && func != IgnoreHandler)
1126 PyOS_setsig(i, SIG_DFL);
1127 Py_XDECREF(func);
1128 }
Guido van Rossum08c16611997-08-02 03:01:42 +00001129
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001130 Py_XDECREF(IntHandler);
1131 IntHandler = NULL;
1132 Py_XDECREF(DefaultHandler);
1133 DefaultHandler = NULL;
1134 Py_XDECREF(IgnoreHandler);
1135 IgnoreHandler = NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001136}
1137
Barry Warsaw92971171997-01-03 00:14:25 +00001138
Barry Warsaw92971171997-01-03 00:14:25 +00001139/* Declared in pyerrors.h */
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001140int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001141PyErr_CheckSignals(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001142{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001143 int i;
1144 PyObject *f;
Barry Warsaw92971171997-01-03 00:14:25 +00001145
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001146 if (!is_tripped)
1147 return 0;
Christian Heimesb76922a2007-12-11 01:06:40 +00001148
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001149#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001150 if (PyThread_get_thread_ident() != main_thread)
1151 return 0;
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001152#endif
Christian Heimesb76922a2007-12-11 01:06:40 +00001153
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001154 /*
1155 * The is_tripped variable is meant to speed up the calls to
1156 * PyErr_CheckSignals (both directly or via pending calls) when no
1157 * signal has arrived. This variable is set to 1 when a signal arrives
1158 * and it is set to 0 here, when we know some signals arrived. This way
1159 * we can run the registered handlers with no signals blocked.
1160 *
1161 * NOTE: with this approach we can have a situation where is_tripped is
1162 * 1 but we have no more signals to handle (Handlers[i].tripped
1163 * is 0 for every signal i). This won't do us any harm (except
1164 * we're gonna spent some cycles for nothing). This happens when
1165 * we receive a signal i after we zero is_tripped and before we
1166 * check Handlers[i].tripped.
1167 */
1168 is_tripped = 0;
Christian Heimesb76922a2007-12-11 01:06:40 +00001169
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001170 if (!(f = (PyObject *)PyEval_GetFrame()))
1171 f = Py_None;
Christian Heimesb76922a2007-12-11 01:06:40 +00001172
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001173 for (i = 1; i < NSIG; i++) {
1174 if (Handlers[i].tripped) {
1175 PyObject *result = NULL;
1176 PyObject *arglist = Py_BuildValue("(iO)", i, f);
1177 Handlers[i].tripped = 0;
Barry Warsaw92971171997-01-03 00:14:25 +00001178
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001179 if (arglist) {
1180 result = PyEval_CallObject(Handlers[i].func,
1181 arglist);
1182 Py_DECREF(arglist);
1183 }
1184 if (!result)
1185 return -1;
Barry Warsaw92971171997-01-03 00:14:25 +00001186
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001187 Py_DECREF(result);
1188 }
1189 }
Christian Heimesb76922a2007-12-11 01:06:40 +00001190
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001191 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001192}
1193
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +00001194
Barry Warsaw92971171997-01-03 00:14:25 +00001195/* Replacements for intrcheck.c functionality
1196 * Declared in pyerrors.h
1197 */
1198void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001199PyErr_SetInterrupt(void)
Barry Warsaw92971171997-01-03 00:14:25 +00001200{
Victor Stinner6c9b35b2011-04-18 16:25:56 +02001201 trip_signal(SIGINT);
Barry Warsaw92971171997-01-03 00:14:25 +00001202}
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001203
1204void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001205PyOS_InitInterrupts(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001206{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001207 PyObject *m = PyInit_signal();
1208 if (m) {
Victor Stinner49d3f252010-10-17 01:24:53 +00001209 _PyImport_FixupBuiltin(m, "signal");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001210 Py_DECREF(m);
1211 }
Guido van Rossum08c16611997-08-02 03:01:42 +00001212}
1213
1214void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001215PyOS_FiniInterrupts(void)
Guido van Rossum08c16611997-08-02 03:01:42 +00001216{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001217 finisignal();
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001218}
1219
1220int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001221PyOS_InterruptOccurred(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001222{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001223 if (Handlers[SIGINT].tripped) {
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001224#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001225 if (PyThread_get_thread_ident() != main_thread)
1226 return 0;
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001227#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001228 Handlers[SIGINT].tripped = 0;
1229 return 1;
1230 }
1231 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001232}
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001233
1234void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001235PyOS_AfterFork(void)
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001236{
1237#ifdef WITH_THREAD
Antoine Pitrou0c759fe2011-04-27 19:28:05 +02001238 _PyGILState_Reinit();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001239 PyEval_ReInitThreads();
1240 main_thread = PyThread_get_thread_ident();
1241 main_pid = getpid();
1242 _PyImport_ReInitLock();
1243 PyThread_ReInitTLS();
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001244#endif
1245}