blob: ff65f04d9516ecf56dbd5c1619408ae791460c84 [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 Stinner6c9b35b2011-04-18 16:25:56 +0200180 Handlers[sig_num].tripped = 1;
181 if (is_tripped)
182 return;
183 /* Set is_tripped after setting .tripped, as it gets
184 cleared in PyErr_CheckSignals() before .tripped. */
185 is_tripped = 1;
186 Py_AddPendingCall(checksignals_witharg, NULL);
Victor Stinnerd49b1f12011-05-08 02:03:15 +0200187 if (wakeup_fd != -1) {
188 byte = (unsigned char)sig_num;
189 write(wakeup_fd, &byte, 1);
190 }
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200191}
192
193static void
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000194signal_handler(int sig_num)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000195{
Antoine Pitrou39a65912010-11-05 19:47:27 +0000196 int save_errno = errno;
197
198#if defined(WITH_THREAD) && defined(WITH_PTH)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000199 if (PyThread_get_thread_ident() != main_thread) {
200 pth_raise(*(pth_t *) main_thread, sig_num);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000201 }
Antoine Pitrou39a65912010-11-05 19:47:27 +0000202 else
Guido van Rossum9e8181b2000-09-19 00:46:46 +0000203#endif
Antoine Pitrou39a65912010-11-05 19:47:27 +0000204 {
205#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000206 /* See NOTES section above */
Antoine Pitrou39a65912010-11-05 19:47:27 +0000207 if (getpid() == main_pid)
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000208#endif
Antoine Pitrou39a65912010-11-05 19:47:27 +0000209 {
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200210 trip_signal(sig_num);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000211 }
Antoine Pitrou39a65912010-11-05 19:47:27 +0000212
Jean-Paul Calderone6f137ca2010-05-09 03:18:57 +0000213#ifndef HAVE_SIGACTION
Antoine Pitrou39a65912010-11-05 19:47:27 +0000214#ifdef SIGCHLD
215 /* To avoid infinite recursion, this signal remains
216 reset until explicit re-instated.
217 Don't clear the 'func' field as it is our pointer
218 to the Python handler... */
219 if (sig_num != SIGCHLD)
220#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000221 /* If the handler was not set up with sigaction, reinstall it. See
222 * Python/pythonrun.c for the implementation of PyOS_setsig which
223 * makes this true. See also issue8354. */
224 PyOS_setsig(sig_num, signal_handler);
Jean-Paul Calderone6f137ca2010-05-09 03:18:57 +0000225#endif
Antoine Pitrou39a65912010-11-05 19:47:27 +0000226 }
227
228 /* Issue #10311: asynchronously executing signal handlers should not
229 mutate errno under the feet of unsuspecting C code. */
230 errno = save_errno;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000231}
Guido van Rossume4485b01994-09-07 14:32:49 +0000232
Guido van Rossum06d511d1995-03-10 15:13:48 +0000233
Guido van Rossum1171ee61997-08-22 20:42:00 +0000234#ifdef HAVE_ALARM
Guido van Rossume4485b01994-09-07 14:32:49 +0000235static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000236signal_alarm(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000237{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000238 int t;
239 if (!PyArg_ParseTuple(args, "i:alarm", &t))
240 return NULL;
241 /* alarm() returns the number of seconds remaining */
242 return PyLong_FromLong((long)alarm(t));
Guido van Rossumaa0f4c71994-08-23 13:49:37 +0000243}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000244
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000245PyDoc_STRVAR(alarm_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000246"alarm(seconds)\n\
247\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000248Arrange for SIGALRM to arrive after the given number of seconds.");
Guido van Rossum06d511d1995-03-10 15:13:48 +0000249#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000250
Guido van Rossum1171ee61997-08-22 20:42:00 +0000251#ifdef HAVE_PAUSE
Guido van Rossuma597dde1995-01-10 20:56:29 +0000252static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000253signal_pause(PyObject *self)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000254{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000255 Py_BEGIN_ALLOW_THREADS
256 (void)pause();
257 Py_END_ALLOW_THREADS
258 /* make sure that any exceptions that got raised are propagated
259 * back into Python
260 */
261 if (PyErr_CheckSignals())
262 return NULL;
Barry Warsaw92971171997-01-03 00:14:25 +0000263
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000264 Py_INCREF(Py_None);
265 return Py_None;
Guido van Rossume4485b01994-09-07 14:32:49 +0000266}
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000267PyDoc_STRVAR(pause_doc,
Barry Warsaw1ee36ff1998-07-21 22:41:18 +0000268"pause()\n\
269\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000270Wait until a signal arrives.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000271
Guido van Rossum06d511d1995-03-10 15:13:48 +0000272#endif
Guido van Rossume4485b01994-09-07 14:32:49 +0000273
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000274
Guido van Rossume4485b01994-09-07 14:32:49 +0000275static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000276signal_signal(PyObject *self, PyObject *args)
Guido van Rossume4485b01994-09-07 14:32:49 +0000277{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000278 PyObject *obj;
279 int sig_num;
280 PyObject *old_handler;
281 void (*func)(int);
282 if (!PyArg_ParseTuple(args, "iO:signal", &sig_num, &obj))
283 return NULL;
Brian Curtinef9efbd2010-08-06 19:27:32 +0000284#ifdef MS_WINDOWS
285 /* Validate that sig_num is one of the allowable signals */
Brian Curtinc734b312010-09-06 16:04:10 +0000286 switch (sig_num) {
287 case SIGABRT: break;
Brian Curtin9e88b5a2010-10-01 14:49:24 +0000288#ifdef SIGBREAK
289 /* Issue #10003: SIGBREAK is not documented as permitted, but works
290 and corresponds to CTRL_BREAK_EVENT. */
291 case SIGBREAK: break;
292#endif
Brian Curtinc734b312010-09-06 16:04:10 +0000293 case SIGFPE: break;
294 case SIGILL: break;
295 case SIGINT: break;
296 case SIGSEGV: break;
297 case SIGTERM: break;
298 default:
299 PyErr_SetString(PyExc_ValueError, "invalid signal value");
300 return NULL;
Brian Curtinef9efbd2010-08-06 19:27:32 +0000301 }
302#endif
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000303#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000304 if (PyThread_get_thread_ident() != main_thread) {
305 PyErr_SetString(PyExc_ValueError,
306 "signal only works in main thread");
307 return NULL;
308 }
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000309#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000310 if (sig_num < 1 || sig_num >= NSIG) {
311 PyErr_SetString(PyExc_ValueError,
312 "signal number out of range");
313 return NULL;
314 }
315 if (obj == IgnoreHandler)
316 func = SIG_IGN;
317 else if (obj == DefaultHandler)
318 func = SIG_DFL;
319 else if (!PyCallable_Check(obj)) {
320 PyErr_SetString(PyExc_TypeError,
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000321"signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000322 return NULL;
323 }
324 else
325 func = signal_handler;
326 if (PyOS_setsig(sig_num, func) == SIG_ERR) {
Victor Stinner388196e2011-05-10 17:13:00 +0200327 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000328 return NULL;
329 }
330 old_handler = Handlers[sig_num].func;
331 Handlers[sig_num].tripped = 0;
332 Py_INCREF(obj);
333 Handlers[sig_num].func = obj;
334 return old_handler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000335}
336
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000337PyDoc_STRVAR(signal_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000338"signal(sig, action) -> action\n\
339\n\
340Set the action for the given signal. The action can be SIG_DFL,\n\
341SIG_IGN, or a callable Python object. The previous action is\n\
342returned. See getsignal() for possible return values.\n\
343\n\
344*** IMPORTANT NOTICE ***\n\
345A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000346the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000347
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000348
Guido van Rossume4485b01994-09-07 14:32:49 +0000349static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000350signal_getsignal(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000351{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000352 int sig_num;
353 PyObject *old_handler;
354 if (!PyArg_ParseTuple(args, "i:getsignal", &sig_num))
355 return NULL;
356 if (sig_num < 1 || sig_num >= NSIG) {
357 PyErr_SetString(PyExc_ValueError,
358 "signal number out of range");
359 return NULL;
360 }
361 old_handler = Handlers[sig_num].func;
362 Py_INCREF(old_handler);
363 return old_handler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000364}
365
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000366PyDoc_STRVAR(getsignal_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000367"getsignal(sig) -> action\n\
368\n\
369Return the current action for the given signal. The return value can be:\n\
370SIG_IGN -- if the signal is being ignored\n\
371SIG_DFL -- if the default action for the signal is in effect\n\
372None -- if an unknown handler is in effect\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000373anything else -- the callable Python object used as a handler");
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000374
Christian Heimes8640e742008-02-23 16:23:06 +0000375#ifdef HAVE_SIGINTERRUPT
376PyDoc_STRVAR(siginterrupt_doc,
377"siginterrupt(sig, flag) -> None\n\
378change system call restart behaviour: if flag is False, system calls\n\
379will be restarted when interrupted by signal sig, else system calls\n\
380will be interrupted.");
381
382static PyObject *
383signal_siginterrupt(PyObject *self, PyObject *args)
384{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000385 int sig_num;
386 int flag;
Christian Heimes8640e742008-02-23 16:23:06 +0000387
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000388 if (!PyArg_ParseTuple(args, "ii:siginterrupt", &sig_num, &flag))
389 return NULL;
390 if (sig_num < 1 || sig_num >= NSIG) {
391 PyErr_SetString(PyExc_ValueError,
392 "signal number out of range");
393 return NULL;
394 }
395 if (siginterrupt(sig_num, flag)<0) {
Victor Stinner388196e2011-05-10 17:13:00 +0200396 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000397 return NULL;
398 }
Christian Heimes8640e742008-02-23 16:23:06 +0000399
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000400 Py_INCREF(Py_None);
401 return Py_None;
Christian Heimes8640e742008-02-23 16:23:06 +0000402}
403
404#endif
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000405
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000406static PyObject *
407signal_set_wakeup_fd(PyObject *self, PyObject *args)
408{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000409 struct stat buf;
410 int fd, old_fd;
411 if (!PyArg_ParseTuple(args, "i:set_wakeup_fd", &fd))
412 return NULL;
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000413#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000414 if (PyThread_get_thread_ident() != main_thread) {
415 PyErr_SetString(PyExc_ValueError,
416 "set_wakeup_fd only works in main thread");
417 return NULL;
418 }
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000419#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000420 if (fd != -1 && fstat(fd, &buf) != 0) {
421 PyErr_SetString(PyExc_ValueError, "invalid fd");
422 return NULL;
423 }
424 old_fd = wakeup_fd;
425 wakeup_fd = fd;
426 return PyLong_FromLong(old_fd);
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000427}
428
429PyDoc_STRVAR(set_wakeup_fd_doc,
430"set_wakeup_fd(fd) -> fd\n\
431\n\
432Sets the fd to be written to (with '\\0') when a signal\n\
433comes in. A library can use this to wakeup select or poll.\n\
434The previous fd is returned.\n\
435\n\
436The fd must be non-blocking.");
437
438/* C API for the same, without all the error checking */
439int
440PySignal_SetWakeupFd(int fd)
441{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000442 int old_fd = wakeup_fd;
443 if (fd < 0)
444 fd = -1;
445 wakeup_fd = fd;
446 return old_fd;
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000447}
448
449
Martin v. Löwis823725e2008-03-24 13:39:54 +0000450#ifdef HAVE_SETITIMER
451static PyObject *
452signal_setitimer(PyObject *self, PyObject *args)
453{
454 double first;
455 double interval = 0;
456 int which;
457 struct itimerval new, old;
458
459 if(!PyArg_ParseTuple(args, "id|d:setitimer", &which, &first, &interval))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000460 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000461
462 timeval_from_double(first, &new.it_value);
463 timeval_from_double(interval, &new.it_interval);
464 /* Let OS check "which" value */
465 if (setitimer(which, &new, &old) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000466 PyErr_SetFromErrno(ItimerError);
467 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000468 }
469
470 return itimer_retval(&old);
471}
472
473PyDoc_STRVAR(setitimer_doc,
474"setitimer(which, seconds[, interval])\n\
475\n\
476Sets given itimer (one of ITIMER_REAL, ITIMER_VIRTUAL\n\
477or ITIMER_PROF) to fire after value seconds and after\n\
478that every interval seconds.\n\
479The itimer can be cleared by setting seconds to zero.\n\
480\n\
481Returns old values as a tuple: (delay, interval).");
482#endif
483
484
485#ifdef HAVE_GETITIMER
486static PyObject *
487signal_getitimer(PyObject *self, PyObject *args)
488{
489 int which;
490 struct itimerval old;
491
492 if (!PyArg_ParseTuple(args, "i:getitimer", &which))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000493 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000494
495 if (getitimer(which, &old) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000496 PyErr_SetFromErrno(ItimerError);
497 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000498 }
499
500 return itimer_retval(&old);
501}
502
503PyDoc_STRVAR(getitimer_doc,
504"getitimer(which)\n\
505\n\
506Returns current value of given itimer.");
507#endif
508
Victor Stinnerb3e72192011-05-08 01:46:11 +0200509#if defined(PYPTHREAD_SIGMASK) || defined(HAVE_SIGWAIT)
Victor Stinnera9293352011-04-30 15:21:58 +0200510/* Convert an iterable to a sigset.
511 Return 0 on success, return -1 and raise an exception on error. */
512
513static int
514iterable_to_sigset(PyObject *iterable, sigset_t *mask)
515{
516 int result = -1;
517 PyObject *iterator, *item;
518 long signum;
519 int err;
520
521 sigemptyset(mask);
522
523 iterator = PyObject_GetIter(iterable);
524 if (iterator == NULL)
525 goto error;
526
527 while (1)
528 {
529 item = PyIter_Next(iterator);
530 if (item == NULL) {
531 if (PyErr_Occurred())
532 goto error;
533 else
534 break;
535 }
536
537 signum = PyLong_AsLong(item);
538 Py_DECREF(item);
539 if (signum == -1 && PyErr_Occurred())
540 goto error;
541 if (0 < signum && signum < NSIG)
542 err = sigaddset(mask, (int)signum);
543 else
544 err = 1;
545 if (err) {
546 PyErr_Format(PyExc_ValueError,
547 "signal number %ld out of range", signum);
548 goto error;
549 }
550 }
551 result = 0;
552
553error:
554 Py_XDECREF(iterator);
555 return result;
556}
Victor Stinnerb3e72192011-05-08 01:46:11 +0200557#endif
Victor Stinnera9293352011-04-30 15:21:58 +0200558
Victor Stinnerb3e72192011-05-08 01:46:11 +0200559#if defined(PYPTHREAD_SIGMASK) || defined(HAVE_SIGPENDING)
Victor Stinner35b300c2011-05-04 13:20:35 +0200560static PyObject*
561sigset_to_set(sigset_t mask)
562{
563 PyObject *signum, *result;
564 int sig;
565
566 result = PySet_New(0);
567 if (result == NULL)
568 return NULL;
569
570 for (sig = 1; sig < NSIG; sig++) {
571 if (sigismember(&mask, sig) != 1)
572 continue;
573
574 /* Handle the case where it is a member by adding the signal to
575 the result list. Ignore the other cases because they mean the
576 signal isn't a member of the mask or the signal was invalid,
577 and an invalid signal must have been our fault in constructing
578 the loop boundaries. */
579 signum = PyLong_FromLong(sig);
580 if (signum == NULL) {
581 Py_DECREF(result);
582 return NULL;
583 }
584 if (PySet_Add(result, signum) == -1) {
585 Py_DECREF(signum);
586 Py_DECREF(result);
587 return NULL;
588 }
589 Py_DECREF(signum);
590 }
591 return result;
592}
Victor Stinnerb3e72192011-05-08 01:46:11 +0200593#endif
Victor Stinner35b300c2011-05-04 13:20:35 +0200594
Victor Stinnerb3e72192011-05-08 01:46:11 +0200595#ifdef PYPTHREAD_SIGMASK
Victor Stinnera9293352011-04-30 15:21:58 +0200596static PyObject *
597signal_pthread_sigmask(PyObject *self, PyObject *args)
598{
Victor Stinner35b300c2011-05-04 13:20:35 +0200599 int how;
600 PyObject *signals;
Victor Stinnera9293352011-04-30 15:21:58 +0200601 sigset_t mask, previous;
602 int err;
603
604 if (!PyArg_ParseTuple(args, "iO:pthread_sigmask", &how, &signals))
605 return NULL;
606
607 if (iterable_to_sigset(signals, &mask))
608 return NULL;
609
610 err = pthread_sigmask(how, &mask, &previous);
611 if (err != 0) {
612 errno = err;
Victor Stinnerb3e72192011-05-08 01:46:11 +0200613 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnera9293352011-04-30 15:21:58 +0200614 return NULL;
615 }
616
Victor Stinnerd0e516d2011-05-03 14:57:12 +0200617 /* if signals was unblocked, signal handlers have been called */
618 if (PyErr_CheckSignals())
619 return NULL;
620
Victor Stinner35b300c2011-05-04 13:20:35 +0200621 return sigset_to_set(previous);
Victor Stinnera9293352011-04-30 15:21:58 +0200622}
623
624PyDoc_STRVAR(signal_pthread_sigmask_doc,
625"pthread_sigmask(how, mask) -> old mask\n\
626\n\
627Fetch and/or change the signal mask of the calling thread.");
628#endif /* #ifdef PYPTHREAD_SIGMASK */
629
Martin v. Löwis823725e2008-03-24 13:39:54 +0000630
Victor Stinnerb3e72192011-05-08 01:46:11 +0200631#ifdef HAVE_SIGPENDING
632static PyObject *
633signal_sigpending(PyObject *self)
634{
635 int err;
636 sigset_t mask;
637 err = sigpending(&mask);
638 if (err)
639 return PyErr_SetFromErrno(PyExc_OSError);
640 return sigset_to_set(mask);
641}
642
643PyDoc_STRVAR(signal_sigpending_doc,
644"sigpending() -> list\n\
645\n\
646Examine pending signals.");
647#endif /* #ifdef HAVE_SIGPENDING */
648
649
650#ifdef HAVE_SIGWAIT
651static PyObject *
652signal_sigwait(PyObject *self, PyObject *args)
653{
654 PyObject *signals;
655 sigset_t set;
656 int err, signum;
657
658 if (!PyArg_ParseTuple(args, "O:sigwait", &signals))
659 return NULL;
660
661 if (iterable_to_sigset(signals, &set))
662 return NULL;
663
664 err = sigwait(&set, &signum);
665 if (err) {
666 errno = err;
667 return PyErr_SetFromErrno(PyExc_OSError);
668 }
669
670 return PyLong_FromLong(signum);
671}
672
673PyDoc_STRVAR(signal_sigwait_doc,
674"sigwait(sigset) -> signum\n\
675\n\
676Wait a signal.");
677#endif /* #ifdef HAVE_SIGPENDING */
678
679
680#if defined(HAVE_PTHREAD_KILL) && defined(WITH_THREAD)
681static PyObject *
682signal_pthread_kill(PyObject *self, PyObject *args)
683{
684 long tid;
685 int signum;
686 int err;
687
688 if (!PyArg_ParseTuple(args, "li:pthread_kill", &tid, &signum))
689 return NULL;
690
Victor Stinner86e104a2011-05-09 14:45:38 +0200691 err = pthread_kill((pthread_t)tid, signum);
Victor Stinnerb3e72192011-05-08 01:46:11 +0200692 if (err != 0) {
693 errno = err;
694 PyErr_SetFromErrno(PyExc_OSError);
695 return NULL;
696 }
697
698 /* the signal may have been send to the current thread */
699 if (PyErr_CheckSignals())
700 return NULL;
701
702 Py_RETURN_NONE;
703}
704
705PyDoc_STRVAR(signal_pthread_kill_doc,
706"pthread_kill(thread_id, signum)\n\
707\n\
708Send a signal to a thread.");
709#endif /* #if defined(HAVE_PTHREAD_KILL) && defined(WITH_THREAD) */
710
711
712
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000713/* List of functions defined in the module */
Barry Warsaw92971171997-01-03 00:14:25 +0000714static PyMethodDef signal_methods[] = {
Guido van Rossum1171ee61997-08-22 20:42:00 +0000715#ifdef HAVE_ALARM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000716 {"alarm", signal_alarm, METH_VARARGS, alarm_doc},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000717#endif
Martin v. Löwis823725e2008-03-24 13:39:54 +0000718#ifdef HAVE_SETITIMER
719 {"setitimer", signal_setitimer, METH_VARARGS, setitimer_doc},
720#endif
721#ifdef HAVE_GETITIMER
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000722 {"getitimer", signal_getitimer, METH_VARARGS, getitimer_doc},
Martin v. Löwis823725e2008-03-24 13:39:54 +0000723#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000724 {"signal", signal_signal, METH_VARARGS, signal_doc},
725 {"getsignal", signal_getsignal, METH_VARARGS, getsignal_doc},
726 {"set_wakeup_fd", signal_set_wakeup_fd, METH_VARARGS, set_wakeup_fd_doc},
Christian Heimes8640e742008-02-23 16:23:06 +0000727#ifdef HAVE_SIGINTERRUPT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000728 {"siginterrupt", signal_siginterrupt, METH_VARARGS, siginterrupt_doc},
Christian Heimes8640e742008-02-23 16:23:06 +0000729#endif
Guido van Rossum1171ee61997-08-22 20:42:00 +0000730#ifdef HAVE_PAUSE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000731 {"pause", (PyCFunction)signal_pause,
Victor Stinnera9293352011-04-30 15:21:58 +0200732 METH_NOARGS, pause_doc},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000733#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000734 {"default_int_handler", signal_default_int_handler,
735 METH_VARARGS, default_int_handler_doc},
Victor Stinnerb3e72192011-05-08 01:46:11 +0200736#if defined(HAVE_PTHREAD_KILL) && defined(WITH_THREAD)
737 {"pthread_kill", (PyCFunction)signal_pthread_kill,
738 METH_VARARGS, signal_pthread_kill_doc},
739#endif
Victor Stinnera9293352011-04-30 15:21:58 +0200740#ifdef PYPTHREAD_SIGMASK
741 {"pthread_sigmask", (PyCFunction)signal_pthread_sigmask,
742 METH_VARARGS, signal_pthread_sigmask_doc},
743#endif
Victor Stinnerb3e72192011-05-08 01:46:11 +0200744#ifdef HAVE_SIGPENDING
745 {"sigpending", (PyCFunction)signal_sigpending,
746 METH_NOARGS, signal_sigpending_doc},
747#endif
748#ifdef HAVE_SIGWAIT
749 {"sigwait", (PyCFunction)signal_sigwait,
750 METH_VARARGS, signal_sigwait_doc},
751#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000752 {NULL, NULL} /* sentinel */
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000753};
754
Barry Warsaw92971171997-01-03 00:14:25 +0000755
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000756PyDoc_STRVAR(module_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000757"This module provides mechanisms to use signal handlers in Python.\n\
758\n\
759Functions:\n\
760\n\
761alarm() -- cause SIGALRM after a specified time [Unix only]\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +0000762setitimer() -- cause a signal (described below) after a specified\n\
763 float time and the timer may restart then [Unix only]\n\
764getitimer() -- get current value of timer [Unix only]\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000765signal() -- set the action for a given signal\n\
766getsignal() -- get the signal action for a given signal\n\
767pause() -- wait until a signal arrives [Unix only]\n\
768default_int_handler() -- default SIGINT handler\n\
769\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +0000770signal constants:\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000771SIG_DFL -- used to refer to the system default handler\n\
772SIG_IGN -- used to ignore the signal\n\
773NSIG -- number of defined signals\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000774SIGINT, SIGTERM, etc. -- signal numbers\n\
775\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +0000776itimer constants:\n\
777ITIMER_REAL -- decrements in real time, and delivers SIGALRM upon\n\
778 expiration\n\
779ITIMER_VIRTUAL -- decrements only when the process is executing,\n\
780 and delivers SIGVTALRM upon expiration\n\
781ITIMER_PROF -- decrements both when the process is executing and\n\
782 when the system is executing on behalf of the process.\n\
783 Coupled with ITIMER_VIRTUAL, this timer is usually\n\
784 used to profile the time spent by the application\n\
785 in user and kernel space. SIGPROF is delivered upon\n\
786 expiration.\n\
787\n\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000788*** IMPORTANT NOTICE ***\n\
789A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000790the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000791
Martin v. Löwis1a214512008-06-11 05:26:20 +0000792static struct PyModuleDef signalmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000793 PyModuleDef_HEAD_INIT,
794 "signal",
795 module_doc,
796 -1,
797 signal_methods,
798 NULL,
799 NULL,
800 NULL,
801 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000802};
803
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000804PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000805PyInit_signal(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000806{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000807 PyObject *m, *d, *x;
808 int i;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000809
810#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000811 main_thread = PyThread_get_thread_ident();
812 main_pid = getpid();
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000813#endif
814
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000815 /* Create the module and add the functions */
816 m = PyModule_Create(&signalmodule);
817 if (m == NULL)
818 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000819
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000820 /* Add some symbolic constants to the module */
821 d = PyModule_GetDict(m);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000822
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000823 x = DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL);
824 if (!x || PyDict_SetItemString(d, "SIG_DFL", x) < 0)
825 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000826
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000827 x = IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN);
828 if (!x || PyDict_SetItemString(d, "SIG_IGN", x) < 0)
829 goto finally;
Barry Warsaw92971171997-01-03 00:14:25 +0000830
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000831 x = PyLong_FromLong((long)NSIG);
832 if (!x || PyDict_SetItemString(d, "NSIG", x) < 0)
833 goto finally;
834 Py_DECREF(x);
Barry Warsaw92971171997-01-03 00:14:25 +0000835
Victor Stinnera9293352011-04-30 15:21:58 +0200836#ifdef SIG_BLOCK
Victor Stinner72c53b52011-05-02 16:15:43 +0200837 if (PyModule_AddIntMacro(m, SIG_BLOCK))
838 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +0200839#endif
Victor Stinnera9293352011-04-30 15:21:58 +0200840#ifdef SIG_UNBLOCK
Victor Stinner72c53b52011-05-02 16:15:43 +0200841 if (PyModule_AddIntMacro(m, SIG_UNBLOCK))
842 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +0200843#endif
Victor Stinnera9293352011-04-30 15:21:58 +0200844#ifdef SIG_SETMASK
Victor Stinner72c53b52011-05-02 16:15:43 +0200845 if (PyModule_AddIntMacro(m, SIG_SETMASK))
846 goto finally;
Victor Stinnera9293352011-04-30 15:21:58 +0200847#endif
848
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000849 x = IntHandler = PyDict_GetItemString(d, "default_int_handler");
850 if (!x)
851 goto finally;
852 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +0000853
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000854 Handlers[0].tripped = 0;
855 for (i = 1; i < NSIG; i++) {
856 void (*t)(int);
857 t = PyOS_getsig(i);
858 Handlers[i].tripped = 0;
859 if (t == SIG_DFL)
860 Handlers[i].func = DefaultHandler;
861 else if (t == SIG_IGN)
862 Handlers[i].func = IgnoreHandler;
863 else
864 Handlers[i].func = Py_None; /* None of our business */
865 Py_INCREF(Handlers[i].func);
866 }
867 if (Handlers[SIGINT].func == DefaultHandler) {
868 /* Install default int handler */
869 Py_INCREF(IntHandler);
870 Py_DECREF(Handlers[SIGINT].func);
871 Handlers[SIGINT].func = IntHandler;
872 old_siginthandler = PyOS_setsig(SIGINT, signal_handler);
873 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000874
875#ifdef SIGHUP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000876 x = PyLong_FromLong(SIGHUP);
877 PyDict_SetItemString(d, "SIGHUP", x);
878 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000879#endif
880#ifdef SIGINT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000881 x = PyLong_FromLong(SIGINT);
882 PyDict_SetItemString(d, "SIGINT", x);
883 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000884#endif
Tim Peters1ce3cf72001-10-01 17:58:40 +0000885#ifdef SIGBREAK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000886 x = PyLong_FromLong(SIGBREAK);
887 PyDict_SetItemString(d, "SIGBREAK", x);
888 Py_XDECREF(x);
Tim Peters1ce3cf72001-10-01 17:58:40 +0000889#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000890#ifdef SIGQUIT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000891 x = PyLong_FromLong(SIGQUIT);
892 PyDict_SetItemString(d, "SIGQUIT", x);
893 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000894#endif
895#ifdef SIGILL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000896 x = PyLong_FromLong(SIGILL);
897 PyDict_SetItemString(d, "SIGILL", x);
898 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000899#endif
900#ifdef SIGTRAP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000901 x = PyLong_FromLong(SIGTRAP);
902 PyDict_SetItemString(d, "SIGTRAP", x);
903 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000904#endif
905#ifdef SIGIOT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000906 x = PyLong_FromLong(SIGIOT);
907 PyDict_SetItemString(d, "SIGIOT", x);
908 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000909#endif
910#ifdef SIGABRT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000911 x = PyLong_FromLong(SIGABRT);
912 PyDict_SetItemString(d, "SIGABRT", x);
913 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000914#endif
915#ifdef SIGEMT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000916 x = PyLong_FromLong(SIGEMT);
917 PyDict_SetItemString(d, "SIGEMT", x);
918 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000919#endif
920#ifdef SIGFPE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000921 x = PyLong_FromLong(SIGFPE);
922 PyDict_SetItemString(d, "SIGFPE", x);
923 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000924#endif
925#ifdef SIGKILL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000926 x = PyLong_FromLong(SIGKILL);
927 PyDict_SetItemString(d, "SIGKILL", x);
928 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000929#endif
930#ifdef SIGBUS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000931 x = PyLong_FromLong(SIGBUS);
932 PyDict_SetItemString(d, "SIGBUS", x);
933 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000934#endif
935#ifdef SIGSEGV
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000936 x = PyLong_FromLong(SIGSEGV);
937 PyDict_SetItemString(d, "SIGSEGV", x);
938 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000939#endif
940#ifdef SIGSYS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000941 x = PyLong_FromLong(SIGSYS);
942 PyDict_SetItemString(d, "SIGSYS", x);
943 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000944#endif
945#ifdef SIGPIPE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000946 x = PyLong_FromLong(SIGPIPE);
947 PyDict_SetItemString(d, "SIGPIPE", x);
948 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000949#endif
950#ifdef SIGALRM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000951 x = PyLong_FromLong(SIGALRM);
952 PyDict_SetItemString(d, "SIGALRM", x);
953 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000954#endif
955#ifdef SIGTERM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000956 x = PyLong_FromLong(SIGTERM);
957 PyDict_SetItemString(d, "SIGTERM", x);
958 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000959#endif
960#ifdef SIGUSR1
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000961 x = PyLong_FromLong(SIGUSR1);
962 PyDict_SetItemString(d, "SIGUSR1", x);
963 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000964#endif
965#ifdef SIGUSR2
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000966 x = PyLong_FromLong(SIGUSR2);
967 PyDict_SetItemString(d, "SIGUSR2", x);
968 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000969#endif
970#ifdef SIGCLD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000971 x = PyLong_FromLong(SIGCLD);
972 PyDict_SetItemString(d, "SIGCLD", x);
973 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000974#endif
975#ifdef SIGCHLD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000976 x = PyLong_FromLong(SIGCHLD);
977 PyDict_SetItemString(d, "SIGCHLD", x);
978 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000979#endif
980#ifdef SIGPWR
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000981 x = PyLong_FromLong(SIGPWR);
982 PyDict_SetItemString(d, "SIGPWR", x);
983 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000984#endif
985#ifdef SIGIO
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000986 x = PyLong_FromLong(SIGIO);
987 PyDict_SetItemString(d, "SIGIO", x);
988 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000989#endif
990#ifdef SIGURG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000991 x = PyLong_FromLong(SIGURG);
992 PyDict_SetItemString(d, "SIGURG", x);
993 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000994#endif
995#ifdef SIGWINCH
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000996 x = PyLong_FromLong(SIGWINCH);
997 PyDict_SetItemString(d, "SIGWINCH", x);
998 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000999#endif
1000#ifdef SIGPOLL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001001 x = PyLong_FromLong(SIGPOLL);
1002 PyDict_SetItemString(d, "SIGPOLL", x);
1003 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001004#endif
1005#ifdef SIGSTOP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001006 x = PyLong_FromLong(SIGSTOP);
1007 PyDict_SetItemString(d, "SIGSTOP", x);
1008 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001009#endif
1010#ifdef SIGTSTP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001011 x = PyLong_FromLong(SIGTSTP);
1012 PyDict_SetItemString(d, "SIGTSTP", x);
1013 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001014#endif
1015#ifdef SIGCONT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001016 x = PyLong_FromLong(SIGCONT);
1017 PyDict_SetItemString(d, "SIGCONT", x);
1018 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001019#endif
1020#ifdef SIGTTIN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001021 x = PyLong_FromLong(SIGTTIN);
1022 PyDict_SetItemString(d, "SIGTTIN", x);
1023 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001024#endif
1025#ifdef SIGTTOU
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001026 x = PyLong_FromLong(SIGTTOU);
1027 PyDict_SetItemString(d, "SIGTTOU", x);
1028 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001029#endif
1030#ifdef SIGVTALRM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001031 x = PyLong_FromLong(SIGVTALRM);
1032 PyDict_SetItemString(d, "SIGVTALRM", x);
1033 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001034#endif
1035#ifdef SIGPROF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001036 x = PyLong_FromLong(SIGPROF);
1037 PyDict_SetItemString(d, "SIGPROF", x);
1038 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001039#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001040#ifdef SIGXCPU
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001041 x = PyLong_FromLong(SIGXCPU);
1042 PyDict_SetItemString(d, "SIGXCPU", x);
1043 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001044#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001045#ifdef SIGXFSZ
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001046 x = PyLong_FromLong(SIGXFSZ);
1047 PyDict_SetItemString(d, "SIGXFSZ", x);
1048 Py_XDECREF(x);
Barry Warsaw14ed5fb1996-12-16 20:24:22 +00001049#endif
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001050#ifdef SIGRTMIN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001051 x = PyLong_FromLong(SIGRTMIN);
1052 PyDict_SetItemString(d, "SIGRTMIN", x);
1053 Py_XDECREF(x);
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001054#endif
1055#ifdef SIGRTMAX
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001056 x = PyLong_FromLong(SIGRTMAX);
1057 PyDict_SetItemString(d, "SIGRTMAX", x);
1058 Py_XDECREF(x);
Anthony Baxterf37f37d2003-07-31 10:35:29 +00001059#endif
Martin v. Löwis175af252002-01-12 11:43:25 +00001060#ifdef SIGINFO
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001061 x = PyLong_FromLong(SIGINFO);
1062 PyDict_SetItemString(d, "SIGINFO", x);
1063 Py_XDECREF(x);
Martin v. Löwis175af252002-01-12 11:43:25 +00001064#endif
Martin v. Löwis823725e2008-03-24 13:39:54 +00001065
1066#ifdef ITIMER_REAL
1067 x = PyLong_FromLong(ITIMER_REAL);
1068 PyDict_SetItemString(d, "ITIMER_REAL", x);
1069 Py_DECREF(x);
1070#endif
1071#ifdef ITIMER_VIRTUAL
1072 x = PyLong_FromLong(ITIMER_VIRTUAL);
1073 PyDict_SetItemString(d, "ITIMER_VIRTUAL", x);
1074 Py_DECREF(x);
1075#endif
1076#ifdef ITIMER_PROF
1077 x = PyLong_FromLong(ITIMER_PROF);
1078 PyDict_SetItemString(d, "ITIMER_PROF", x);
1079 Py_DECREF(x);
1080#endif
1081
1082#if defined (HAVE_SETITIMER) || defined (HAVE_GETITIMER)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001083 ItimerError = PyErr_NewException("signal.ItimerError",
1084 PyExc_IOError, NULL);
Neal Norwitz2f99b242008-08-24 05:48:10 +00001085 if (ItimerError != NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001086 PyDict_SetItemString(d, "ItimerError", ItimerError);
Martin v. Löwis823725e2008-03-24 13:39:54 +00001087#endif
1088
Brian Curtineb24d742010-04-12 17:16:38 +00001089#ifdef CTRL_C_EVENT
1090 x = PyLong_FromLong(CTRL_C_EVENT);
1091 PyDict_SetItemString(d, "CTRL_C_EVENT", x);
1092 Py_DECREF(x);
1093#endif
1094
1095#ifdef CTRL_BREAK_EVENT
1096 x = PyLong_FromLong(CTRL_BREAK_EVENT);
1097 PyDict_SetItemString(d, "CTRL_BREAK_EVENT", x);
1098 Py_DECREF(x);
1099#endif
1100
Martin v. Löwis1a214512008-06-11 05:26:20 +00001101 if (PyErr_Occurred()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001102 Py_DECREF(m);
1103 m = NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001104 }
Barry Warsaw92971171997-01-03 00:14:25 +00001105
Barry Warsaw92971171997-01-03 00:14:25 +00001106 finally:
Martin v. Löwis1a214512008-06-11 05:26:20 +00001107 return m;
Guido van Rossum08c16611997-08-02 03:01:42 +00001108}
1109
1110static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001111finisignal(void)
Guido van Rossum08c16611997-08-02 03:01:42 +00001112{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001113 int i;
1114 PyObject *func;
Guido van Rossum08c16611997-08-02 03:01:42 +00001115
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001116 PyOS_setsig(SIGINT, old_siginthandler);
1117 old_siginthandler = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +00001118
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001119 for (i = 1; i < NSIG; i++) {
1120 func = Handlers[i].func;
1121 Handlers[i].tripped = 0;
1122 Handlers[i].func = NULL;
1123 if (i != SIGINT && func != NULL && func != Py_None &&
1124 func != DefaultHandler && func != IgnoreHandler)
1125 PyOS_setsig(i, SIG_DFL);
1126 Py_XDECREF(func);
1127 }
Guido van Rossum08c16611997-08-02 03:01:42 +00001128
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001129 Py_XDECREF(IntHandler);
1130 IntHandler = NULL;
1131 Py_XDECREF(DefaultHandler);
1132 DefaultHandler = NULL;
1133 Py_XDECREF(IgnoreHandler);
1134 IgnoreHandler = NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001135}
1136
Barry Warsaw92971171997-01-03 00:14:25 +00001137
Barry Warsaw92971171997-01-03 00:14:25 +00001138/* Declared in pyerrors.h */
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001139int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001140PyErr_CheckSignals(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001141{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001142 int i;
1143 PyObject *f;
Barry Warsaw92971171997-01-03 00:14:25 +00001144
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001145 if (!is_tripped)
1146 return 0;
Christian Heimesb76922a2007-12-11 01:06:40 +00001147
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001148#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001149 if (PyThread_get_thread_ident() != main_thread)
1150 return 0;
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001151#endif
Christian Heimesb76922a2007-12-11 01:06:40 +00001152
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001153 /*
1154 * The is_tripped variable is meant to speed up the calls to
1155 * PyErr_CheckSignals (both directly or via pending calls) when no
1156 * signal has arrived. This variable is set to 1 when a signal arrives
1157 * and it is set to 0 here, when we know some signals arrived. This way
1158 * we can run the registered handlers with no signals blocked.
1159 *
1160 * NOTE: with this approach we can have a situation where is_tripped is
1161 * 1 but we have no more signals to handle (Handlers[i].tripped
1162 * is 0 for every signal i). This won't do us any harm (except
1163 * we're gonna spent some cycles for nothing). This happens when
1164 * we receive a signal i after we zero is_tripped and before we
1165 * check Handlers[i].tripped.
1166 */
1167 is_tripped = 0;
Christian Heimesb76922a2007-12-11 01:06:40 +00001168
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001169 if (!(f = (PyObject *)PyEval_GetFrame()))
1170 f = Py_None;
Christian Heimesb76922a2007-12-11 01:06:40 +00001171
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001172 for (i = 1; i < NSIG; i++) {
1173 if (Handlers[i].tripped) {
1174 PyObject *result = NULL;
1175 PyObject *arglist = Py_BuildValue("(iO)", i, f);
1176 Handlers[i].tripped = 0;
Barry Warsaw92971171997-01-03 00:14:25 +00001177
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001178 if (arglist) {
1179 result = PyEval_CallObject(Handlers[i].func,
1180 arglist);
1181 Py_DECREF(arglist);
1182 }
1183 if (!result)
1184 return -1;
Barry Warsaw92971171997-01-03 00:14:25 +00001185
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001186 Py_DECREF(result);
1187 }
1188 }
Christian Heimesb76922a2007-12-11 01:06:40 +00001189
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001190 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001191}
1192
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +00001193
Barry Warsaw92971171997-01-03 00:14:25 +00001194/* Replacements for intrcheck.c functionality
1195 * Declared in pyerrors.h
1196 */
1197void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001198PyErr_SetInterrupt(void)
Barry Warsaw92971171997-01-03 00:14:25 +00001199{
Victor Stinner6c9b35b2011-04-18 16:25:56 +02001200 trip_signal(SIGINT);
Barry Warsaw92971171997-01-03 00:14:25 +00001201}
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001202
1203void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001204PyOS_InitInterrupts(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001205{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001206 PyObject *m = PyInit_signal();
1207 if (m) {
Victor Stinner49d3f252010-10-17 01:24:53 +00001208 _PyImport_FixupBuiltin(m, "signal");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001209 Py_DECREF(m);
1210 }
Guido van Rossum08c16611997-08-02 03:01:42 +00001211}
1212
1213void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001214PyOS_FiniInterrupts(void)
Guido van Rossum08c16611997-08-02 03:01:42 +00001215{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001216 finisignal();
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001217}
1218
1219int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001220PyOS_InterruptOccurred(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001221{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001222 if (Handlers[SIGINT].tripped) {
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001223#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001224 if (PyThread_get_thread_ident() != main_thread)
1225 return 0;
Guido van Rossumbb4ba121994-06-23 11:25:45 +00001226#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001227 Handlers[SIGINT].tripped = 0;
1228 return 1;
1229 }
1230 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001231}
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001232
1233void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001234PyOS_AfterFork(void)
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001235{
1236#ifdef WITH_THREAD
Antoine Pitrou0c759fe2011-04-27 19:28:05 +02001237 _PyGILState_Reinit();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001238 PyEval_ReInitThreads();
1239 main_thread = PyThread_get_thread_ident();
1240 main_pid = getpid();
1241 _PyImport_ReInitLock();
1242 PyThread_ReInitTLS();
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001243#endif
1244}