blob: 9cae454194a3d4ddca15cace75890e7a1ce4dbfc [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
Guido van Rossumbb4ba121994-06-23 11:25:45 +000025#ifndef SIG_ERR
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +000026#define SIG_ERR ((PyOS_sighandler_t)(-1))
Guido van Rossumbb4ba121994-06-23 11:25:45 +000027#endif
28
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000029#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000030#define NSIG 12
31#include <process.h>
32#endif
33
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000034#ifndef NSIG
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000035# if defined(_NSIG)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000036# define NSIG _NSIG /* For BSD/SysV */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000037# elif defined(_SIGMAX)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000038# define NSIG (_SIGMAX + 1) /* For QNX */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000039# elif defined(SIGMAX)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000040# define NSIG (SIGMAX + 1) /* For djgpp */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000041# else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000042# define NSIG 64 /* Use a reasonable default value */
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000043# endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000044#endif
45
46
Guido van Rossumbb4ba121994-06-23 11:25:45 +000047/*
48 NOTES ON THE INTERACTION BETWEEN SIGNALS AND THREADS
49
50 When threads are supported, we want the following semantics:
51
52 - only the main thread can set a signal handler
53 - any thread can get a signal handler
54 - signals are only delivered to the main thread
55
56 I.e. we don't support "synchronous signals" like SIGFPE (catching
57 this doesn't make much sense in Python anyway) nor do we support
58 signals as a means of inter-thread communication, since not all
59 thread implementations support that (at least our thread library
60 doesn't).
61
62 We still have the problem that in some implementations signals
63 generated by the keyboard (e.g. SIGINT) are delivered to all
64 threads (e.g. SGI), while in others (e.g. Solaris) such signals are
65 delivered to one random thread (an intermediate possibility would
Guido van Rossuma3c04b01995-01-12 11:29:01 +000066 be to deliver it to the main thread -- POSIX?). For now, we have
Guido van Rossumbb4ba121994-06-23 11:25:45 +000067 a working implementation that works in all three cases -- the
68 handler ignores signals if getpid() isn't the same as in the main
69 thread. XXX This is a hack.
70
Guido van Rossum9e8181b2000-09-19 00:46:46 +000071 GNU pth is a user-space threading library, and as such, all threads
72 run within the same process. In this case, if the currently running
73 thread is not the main_thread, send the signal to the main_thread.
Guido van Rossumbb4ba121994-06-23 11:25:45 +000074*/
75
76#ifdef WITH_THREAD
Guido van Rossum295b8e51997-06-06 21:16:41 +000077#include <sys/types.h> /* For pid_t */
Guido van Rossum49b56061998-10-01 20:42:43 +000078#include "pythread.h"
Guido van Rossumbb4ba121994-06-23 11:25:45 +000079static long main_thread;
80static pid_t main_pid;
81#endif
82
Victor Stinner2ec6b172011-05-15 10:21:59 +020083static volatile struct {
84 sig_atomic_t tripped;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000085 PyObject *func;
Barry Warsaw92971171997-01-03 00:14:25 +000086} Handlers[NSIG];
Guido van Rossum398d9fe1994-05-11 08:59:13 +000087
Victor Stinner2ec6b172011-05-15 10:21:59 +020088static volatile sig_atomic_t wakeup_fd = -1;
Christian Heimes5fb7c2a2007-12-24 08:52:31 +000089
Christian Heimesb76922a2007-12-11 01:06:40 +000090/* Speed up sigcheck() when none tripped */
91static volatile sig_atomic_t is_tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +000092
Barry Warsaw92971171997-01-03 00:14:25 +000093static PyObject *DefaultHandler;
94static PyObject *IgnoreHandler;
95static PyObject *IntHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +000096
Martin v. Löwisf58de1b2001-03-06 12:13:56 +000097/* On Solaris 8, gcc will produce a warning that the function
98 declaration is not a prototype. This is caused by the definition of
99 SIG_DFL as (void (*)())0; the correct declaration would have been
100 (void (*)(int))0. */
101
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000102static PyOS_sighandler_t old_siginthandler = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +0000103
Martin v. Löwis823725e2008-03-24 13:39:54 +0000104#ifdef HAVE_GETITIMER
105static PyObject *ItimerError;
106
107/* auxiliary functions for setitimer/getitimer */
108static void
109timeval_from_double(double d, struct timeval *tv)
110{
111 tv->tv_sec = floor(d);
112 tv->tv_usec = fmod(d, 1.0) * 1000000.0;
113}
114
Christian Heimes1a8501c2008-10-02 19:56:01 +0000115Py_LOCAL_INLINE(double)
Martin v. Löwis823725e2008-03-24 13:39:54 +0000116double_from_timeval(struct timeval *tv)
117{
118 return tv->tv_sec + (double)(tv->tv_usec / 1000000.0);
119}
120
121static PyObject *
122itimer_retval(struct itimerval *iv)
123{
124 PyObject *r, *v;
125
126 r = PyTuple_New(2);
127 if (r == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000128 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000129
130 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_value)))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000131 Py_DECREF(r);
132 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000133 }
134
135 PyTuple_SET_ITEM(r, 0, v);
136
137 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_interval)))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000138 Py_DECREF(r);
139 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000140 }
141
142 PyTuple_SET_ITEM(r, 1, v);
143
144 return r;
145}
146#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000147
Guido van Rossume4485b01994-09-07 14:32:49 +0000148static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000149signal_default_int_handler(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000150{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000151 PyErr_SetNone(PyExc_KeyboardInterrupt);
152 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000153}
154
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000155PyDoc_STRVAR(default_int_handler_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000156"default_int_handler(...)\n\
157\n\
Michael W. Hudson24ec2112004-06-17 15:55:53 +0000158The default handler for SIGINT installed by Python.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000159It raises KeyboardInterrupt.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000160
Thomas Wouters0796b002000-07-22 23:49:30 +0000161
162static int
163checksignals_witharg(void * unused)
164{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000165 return PyErr_CheckSignals();
Thomas Wouters0796b002000-07-22 23:49:30 +0000166}
167
Tim Peters4f1b2082000-07-23 21:18:09 +0000168static void
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200169trip_signal(int sig_num)
170{
171 Handlers[sig_num].tripped = 1;
172 if (is_tripped)
173 return;
174 /* Set is_tripped after setting .tripped, as it gets
175 cleared in PyErr_CheckSignals() before .tripped. */
176 is_tripped = 1;
177 Py_AddPendingCall(checksignals_witharg, NULL);
178 if (wakeup_fd != -1)
179 write(wakeup_fd, "\0", 1);
180}
181
182static void
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000183signal_handler(int sig_num)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000184{
Antoine Pitrou39a65912010-11-05 19:47:27 +0000185 int save_errno = errno;
186
187#if defined(WITH_THREAD) && defined(WITH_PTH)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000188 if (PyThread_get_thread_ident() != main_thread) {
189 pth_raise(*(pth_t *) main_thread, sig_num);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000190 }
Antoine Pitrou39a65912010-11-05 19:47:27 +0000191 else
Guido van Rossum9e8181b2000-09-19 00:46:46 +0000192#endif
Antoine Pitrou39a65912010-11-05 19:47:27 +0000193 {
194#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000195 /* See NOTES section above */
Antoine Pitrou39a65912010-11-05 19:47:27 +0000196 if (getpid() == main_pid)
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000197#endif
Antoine Pitrou39a65912010-11-05 19:47:27 +0000198 {
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200199 trip_signal(sig_num);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000200 }
Antoine Pitrou39a65912010-11-05 19:47:27 +0000201
Jean-Paul Calderone6f137ca2010-05-09 03:18:57 +0000202#ifndef HAVE_SIGACTION
Antoine Pitrou39a65912010-11-05 19:47:27 +0000203#ifdef SIGCHLD
204 /* To avoid infinite recursion, this signal remains
205 reset until explicit re-instated.
206 Don't clear the 'func' field as it is our pointer
207 to the Python handler... */
208 if (sig_num != SIGCHLD)
209#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000210 /* If the handler was not set up with sigaction, reinstall it. See
211 * Python/pythonrun.c for the implementation of PyOS_setsig which
212 * makes this true. See also issue8354. */
213 PyOS_setsig(sig_num, signal_handler);
Jean-Paul Calderone6f137ca2010-05-09 03:18:57 +0000214#endif
Antoine Pitrou39a65912010-11-05 19:47:27 +0000215 }
216
217 /* Issue #10311: asynchronously executing signal handlers should not
218 mutate errno under the feet of unsuspecting C code. */
219 errno = save_errno;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000220}
Guido van Rossume4485b01994-09-07 14:32:49 +0000221
Guido van Rossum06d511d1995-03-10 15:13:48 +0000222
Guido van Rossum1171ee61997-08-22 20:42:00 +0000223#ifdef HAVE_ALARM
Guido van Rossume4485b01994-09-07 14:32:49 +0000224static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000225signal_alarm(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000226{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000227 int t;
228 if (!PyArg_ParseTuple(args, "i:alarm", &t))
229 return NULL;
230 /* alarm() returns the number of seconds remaining */
231 return PyLong_FromLong((long)alarm(t));
Guido van Rossumaa0f4c71994-08-23 13:49:37 +0000232}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000233
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000234PyDoc_STRVAR(alarm_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000235"alarm(seconds)\n\
236\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000237Arrange for SIGALRM to arrive after the given number of seconds.");
Guido van Rossum06d511d1995-03-10 15:13:48 +0000238#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000239
Guido van Rossum1171ee61997-08-22 20:42:00 +0000240#ifdef HAVE_PAUSE
Guido van Rossuma597dde1995-01-10 20:56:29 +0000241static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000242signal_pause(PyObject *self)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000243{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000244 Py_BEGIN_ALLOW_THREADS
245 (void)pause();
246 Py_END_ALLOW_THREADS
247 /* make sure that any exceptions that got raised are propagated
248 * back into Python
249 */
250 if (PyErr_CheckSignals())
251 return NULL;
Barry Warsaw92971171997-01-03 00:14:25 +0000252
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000253 Py_INCREF(Py_None);
254 return Py_None;
Guido van Rossume4485b01994-09-07 14:32:49 +0000255}
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000256PyDoc_STRVAR(pause_doc,
Barry Warsaw1ee36ff1998-07-21 22:41:18 +0000257"pause()\n\
258\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000259Wait until a signal arrives.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000260
Guido van Rossum06d511d1995-03-10 15:13:48 +0000261#endif
Guido van Rossume4485b01994-09-07 14:32:49 +0000262
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000263
Guido van Rossume4485b01994-09-07 14:32:49 +0000264static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000265signal_signal(PyObject *self, PyObject *args)
Guido van Rossume4485b01994-09-07 14:32:49 +0000266{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000267 PyObject *obj;
268 int sig_num;
269 PyObject *old_handler;
270 void (*func)(int);
271 if (!PyArg_ParseTuple(args, "iO:signal", &sig_num, &obj))
272 return NULL;
Brian Curtinef9efbd2010-08-06 19:27:32 +0000273#ifdef MS_WINDOWS
274 /* Validate that sig_num is one of the allowable signals */
Brian Curtinc734b312010-09-06 16:04:10 +0000275 switch (sig_num) {
276 case SIGABRT: break;
Brian Curtin9e88b5a2010-10-01 14:49:24 +0000277#ifdef SIGBREAK
278 /* Issue #10003: SIGBREAK is not documented as permitted, but works
279 and corresponds to CTRL_BREAK_EVENT. */
280 case SIGBREAK: break;
281#endif
Brian Curtinc734b312010-09-06 16:04:10 +0000282 case SIGFPE: break;
283 case SIGILL: break;
284 case SIGINT: break;
285 case SIGSEGV: break;
286 case SIGTERM: break;
287 default:
288 PyErr_SetString(PyExc_ValueError, "invalid signal value");
289 return NULL;
Brian Curtinef9efbd2010-08-06 19:27:32 +0000290 }
291#endif
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000292#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000293 if (PyThread_get_thread_ident() != main_thread) {
294 PyErr_SetString(PyExc_ValueError,
295 "signal only works in main thread");
296 return NULL;
297 }
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000298#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000299 if (sig_num < 1 || sig_num >= NSIG) {
300 PyErr_SetString(PyExc_ValueError,
301 "signal number out of range");
302 return NULL;
303 }
304 if (obj == IgnoreHandler)
305 func = SIG_IGN;
306 else if (obj == DefaultHandler)
307 func = SIG_DFL;
308 else if (!PyCallable_Check(obj)) {
309 PyErr_SetString(PyExc_TypeError,
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000310"signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000311 return NULL;
312 }
313 else
314 func = signal_handler;
315 if (PyOS_setsig(sig_num, func) == SIG_ERR) {
316 PyErr_SetFromErrno(PyExc_RuntimeError);
317 return NULL;
318 }
319 old_handler = Handlers[sig_num].func;
320 Handlers[sig_num].tripped = 0;
321 Py_INCREF(obj);
322 Handlers[sig_num].func = obj;
323 return old_handler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000324}
325
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000326PyDoc_STRVAR(signal_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000327"signal(sig, action) -> action\n\
328\n\
329Set the action for the given signal. The action can be SIG_DFL,\n\
330SIG_IGN, or a callable Python object. The previous action is\n\
331returned. See getsignal() for possible return values.\n\
332\n\
333*** IMPORTANT NOTICE ***\n\
334A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000335the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000336
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000337
Guido van Rossume4485b01994-09-07 14:32:49 +0000338static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000339signal_getsignal(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000340{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000341 int sig_num;
342 PyObject *old_handler;
343 if (!PyArg_ParseTuple(args, "i:getsignal", &sig_num))
344 return NULL;
345 if (sig_num < 1 || sig_num >= NSIG) {
346 PyErr_SetString(PyExc_ValueError,
347 "signal number out of range");
348 return NULL;
349 }
350 old_handler = Handlers[sig_num].func;
351 Py_INCREF(old_handler);
352 return old_handler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000353}
354
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000355PyDoc_STRVAR(getsignal_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000356"getsignal(sig) -> action\n\
357\n\
358Return the current action for the given signal. The return value can be:\n\
359SIG_IGN -- if the signal is being ignored\n\
360SIG_DFL -- if the default action for the signal is in effect\n\
361None -- if an unknown handler is in effect\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000362anything else -- the callable Python object used as a handler");
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000363
Christian Heimes8640e742008-02-23 16:23:06 +0000364#ifdef HAVE_SIGINTERRUPT
365PyDoc_STRVAR(siginterrupt_doc,
366"siginterrupt(sig, flag) -> None\n\
367change system call restart behaviour: if flag is False, system calls\n\
368will be restarted when interrupted by signal sig, else system calls\n\
369will be interrupted.");
370
371static PyObject *
372signal_siginterrupt(PyObject *self, PyObject *args)
373{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000374 int sig_num;
375 int flag;
Christian Heimes8640e742008-02-23 16:23:06 +0000376
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000377 if (!PyArg_ParseTuple(args, "ii:siginterrupt", &sig_num, &flag))
378 return NULL;
379 if (sig_num < 1 || sig_num >= NSIG) {
380 PyErr_SetString(PyExc_ValueError,
381 "signal number out of range");
382 return NULL;
383 }
384 if (siginterrupt(sig_num, flag)<0) {
385 PyErr_SetFromErrno(PyExc_RuntimeError);
386 return NULL;
387 }
Christian Heimes8640e742008-02-23 16:23:06 +0000388
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000389 Py_INCREF(Py_None);
390 return Py_None;
Christian Heimes8640e742008-02-23 16:23:06 +0000391}
392
393#endif
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000394
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000395static PyObject *
396signal_set_wakeup_fd(PyObject *self, PyObject *args)
397{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000398 struct stat buf;
399 int fd, old_fd;
400 if (!PyArg_ParseTuple(args, "i:set_wakeup_fd", &fd))
401 return NULL;
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000402#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000403 if (PyThread_get_thread_ident() != main_thread) {
404 PyErr_SetString(PyExc_ValueError,
405 "set_wakeup_fd only works in main thread");
406 return NULL;
407 }
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000408#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000409 if (fd != -1 && fstat(fd, &buf) != 0) {
410 PyErr_SetString(PyExc_ValueError, "invalid fd");
411 return NULL;
412 }
413 old_fd = wakeup_fd;
414 wakeup_fd = fd;
415 return PyLong_FromLong(old_fd);
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000416}
417
418PyDoc_STRVAR(set_wakeup_fd_doc,
419"set_wakeup_fd(fd) -> fd\n\
420\n\
421Sets the fd to be written to (with '\\0') when a signal\n\
422comes in. A library can use this to wakeup select or poll.\n\
423The previous fd is returned.\n\
424\n\
425The fd must be non-blocking.");
426
427/* C API for the same, without all the error checking */
428int
429PySignal_SetWakeupFd(int fd)
430{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000431 int old_fd = wakeup_fd;
432 if (fd < 0)
433 fd = -1;
434 wakeup_fd = fd;
435 return old_fd;
Christian Heimes5fb7c2a2007-12-24 08:52:31 +0000436}
437
438
Martin v. Löwis823725e2008-03-24 13:39:54 +0000439#ifdef HAVE_SETITIMER
440static PyObject *
441signal_setitimer(PyObject *self, PyObject *args)
442{
443 double first;
444 double interval = 0;
445 int which;
446 struct itimerval new, old;
447
448 if(!PyArg_ParseTuple(args, "id|d:setitimer", &which, &first, &interval))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000449 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000450
451 timeval_from_double(first, &new.it_value);
452 timeval_from_double(interval, &new.it_interval);
453 /* Let OS check "which" value */
454 if (setitimer(which, &new, &old) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000455 PyErr_SetFromErrno(ItimerError);
456 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000457 }
458
459 return itimer_retval(&old);
460}
461
462PyDoc_STRVAR(setitimer_doc,
463"setitimer(which, seconds[, interval])\n\
464\n\
465Sets given itimer (one of ITIMER_REAL, ITIMER_VIRTUAL\n\
466or ITIMER_PROF) to fire after value seconds and after\n\
467that every interval seconds.\n\
468The itimer can be cleared by setting seconds to zero.\n\
469\n\
470Returns old values as a tuple: (delay, interval).");
471#endif
472
473
474#ifdef HAVE_GETITIMER
475static PyObject *
476signal_getitimer(PyObject *self, PyObject *args)
477{
478 int which;
479 struct itimerval old;
480
481 if (!PyArg_ParseTuple(args, "i:getitimer", &which))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000482 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000483
484 if (getitimer(which, &old) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000485 PyErr_SetFromErrno(ItimerError);
486 return NULL;
Martin v. Löwis823725e2008-03-24 13:39:54 +0000487 }
488
489 return itimer_retval(&old);
490}
491
492PyDoc_STRVAR(getitimer_doc,
493"getitimer(which)\n\
494\n\
495Returns current value of given itimer.");
496#endif
497
498
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000499/* List of functions defined in the module */
Barry Warsaw92971171997-01-03 00:14:25 +0000500static PyMethodDef signal_methods[] = {
Guido van Rossum1171ee61997-08-22 20:42:00 +0000501#ifdef HAVE_ALARM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000502 {"alarm", signal_alarm, METH_VARARGS, alarm_doc},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000503#endif
Martin v. Löwis823725e2008-03-24 13:39:54 +0000504#ifdef HAVE_SETITIMER
505 {"setitimer", signal_setitimer, METH_VARARGS, setitimer_doc},
506#endif
507#ifdef HAVE_GETITIMER
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000508 {"getitimer", signal_getitimer, METH_VARARGS, getitimer_doc},
Martin v. Löwis823725e2008-03-24 13:39:54 +0000509#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000510 {"signal", signal_signal, METH_VARARGS, signal_doc},
511 {"getsignal", signal_getsignal, METH_VARARGS, getsignal_doc},
512 {"set_wakeup_fd", signal_set_wakeup_fd, METH_VARARGS, set_wakeup_fd_doc},
Christian Heimes8640e742008-02-23 16:23:06 +0000513#ifdef HAVE_SIGINTERRUPT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000514 {"siginterrupt", signal_siginterrupt, METH_VARARGS, siginterrupt_doc},
Christian Heimes8640e742008-02-23 16:23:06 +0000515#endif
Guido van Rossum1171ee61997-08-22 20:42:00 +0000516#ifdef HAVE_PAUSE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000517 {"pause", (PyCFunction)signal_pause,
518 METH_NOARGS,pause_doc},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000519#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000520 {"default_int_handler", signal_default_int_handler,
521 METH_VARARGS, default_int_handler_doc},
522 {NULL, NULL} /* sentinel */
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000523};
524
Barry Warsaw92971171997-01-03 00:14:25 +0000525
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000526PyDoc_STRVAR(module_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000527"This module provides mechanisms to use signal handlers in Python.\n\
528\n\
529Functions:\n\
530\n\
531alarm() -- cause SIGALRM after a specified time [Unix only]\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +0000532setitimer() -- cause a signal (described below) after a specified\n\
533 float time and the timer may restart then [Unix only]\n\
534getitimer() -- get current value of timer [Unix only]\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000535signal() -- set the action for a given signal\n\
536getsignal() -- get the signal action for a given signal\n\
537pause() -- wait until a signal arrives [Unix only]\n\
538default_int_handler() -- default SIGINT handler\n\
539\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +0000540signal constants:\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000541SIG_DFL -- used to refer to the system default handler\n\
542SIG_IGN -- used to ignore the signal\n\
543NSIG -- number of defined signals\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000544SIGINT, SIGTERM, etc. -- signal numbers\n\
545\n\
Martin v. Löwis823725e2008-03-24 13:39:54 +0000546itimer constants:\n\
547ITIMER_REAL -- decrements in real time, and delivers SIGALRM upon\n\
548 expiration\n\
549ITIMER_VIRTUAL -- decrements only when the process is executing,\n\
550 and delivers SIGVTALRM upon expiration\n\
551ITIMER_PROF -- decrements both when the process is executing and\n\
552 when the system is executing on behalf of the process.\n\
553 Coupled with ITIMER_VIRTUAL, this timer is usually\n\
554 used to profile the time spent by the application\n\
555 in user and kernel space. SIGPROF is delivered upon\n\
556 expiration.\n\
557\n\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000558*** IMPORTANT NOTICE ***\n\
559A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000560the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000561
Martin v. Löwis1a214512008-06-11 05:26:20 +0000562static struct PyModuleDef signalmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000563 PyModuleDef_HEAD_INIT,
564 "signal",
565 module_doc,
566 -1,
567 signal_methods,
568 NULL,
569 NULL,
570 NULL,
571 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000572};
573
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000574PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000575PyInit_signal(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000576{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000577 PyObject *m, *d, *x;
578 int i;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000579
580#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000581 main_thread = PyThread_get_thread_ident();
582 main_pid = getpid();
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000583#endif
584
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000585 /* Create the module and add the functions */
586 m = PyModule_Create(&signalmodule);
587 if (m == NULL)
588 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000589
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000590 /* Add some symbolic constants to the module */
591 d = PyModule_GetDict(m);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000592
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000593 x = DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL);
594 if (!x || PyDict_SetItemString(d, "SIG_DFL", x) < 0)
595 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000596
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000597 x = IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN);
598 if (!x || PyDict_SetItemString(d, "SIG_IGN", x) < 0)
599 goto finally;
Barry Warsaw92971171997-01-03 00:14:25 +0000600
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000601 x = PyLong_FromLong((long)NSIG);
602 if (!x || PyDict_SetItemString(d, "NSIG", x) < 0)
603 goto finally;
604 Py_DECREF(x);
Barry Warsaw92971171997-01-03 00:14:25 +0000605
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000606 x = IntHandler = PyDict_GetItemString(d, "default_int_handler");
607 if (!x)
608 goto finally;
609 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +0000610
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000611 Handlers[0].tripped = 0;
612 for (i = 1; i < NSIG; i++) {
613 void (*t)(int);
614 t = PyOS_getsig(i);
615 Handlers[i].tripped = 0;
616 if (t == SIG_DFL)
617 Handlers[i].func = DefaultHandler;
618 else if (t == SIG_IGN)
619 Handlers[i].func = IgnoreHandler;
620 else
621 Handlers[i].func = Py_None; /* None of our business */
622 Py_INCREF(Handlers[i].func);
623 }
624 if (Handlers[SIGINT].func == DefaultHandler) {
625 /* Install default int handler */
626 Py_INCREF(IntHandler);
627 Py_DECREF(Handlers[SIGINT].func);
628 Handlers[SIGINT].func = IntHandler;
629 old_siginthandler = PyOS_setsig(SIGINT, signal_handler);
630 }
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000631
632#ifdef SIGHUP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000633 x = PyLong_FromLong(SIGHUP);
634 PyDict_SetItemString(d, "SIGHUP", x);
635 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000636#endif
637#ifdef SIGINT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000638 x = PyLong_FromLong(SIGINT);
639 PyDict_SetItemString(d, "SIGINT", x);
640 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000641#endif
Tim Peters1ce3cf72001-10-01 17:58:40 +0000642#ifdef SIGBREAK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000643 x = PyLong_FromLong(SIGBREAK);
644 PyDict_SetItemString(d, "SIGBREAK", x);
645 Py_XDECREF(x);
Tim Peters1ce3cf72001-10-01 17:58:40 +0000646#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000647#ifdef SIGQUIT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000648 x = PyLong_FromLong(SIGQUIT);
649 PyDict_SetItemString(d, "SIGQUIT", x);
650 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000651#endif
652#ifdef SIGILL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000653 x = PyLong_FromLong(SIGILL);
654 PyDict_SetItemString(d, "SIGILL", x);
655 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000656#endif
657#ifdef SIGTRAP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000658 x = PyLong_FromLong(SIGTRAP);
659 PyDict_SetItemString(d, "SIGTRAP", x);
660 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000661#endif
662#ifdef SIGIOT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000663 x = PyLong_FromLong(SIGIOT);
664 PyDict_SetItemString(d, "SIGIOT", x);
665 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000666#endif
667#ifdef SIGABRT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000668 x = PyLong_FromLong(SIGABRT);
669 PyDict_SetItemString(d, "SIGABRT", x);
670 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000671#endif
672#ifdef SIGEMT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000673 x = PyLong_FromLong(SIGEMT);
674 PyDict_SetItemString(d, "SIGEMT", x);
675 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000676#endif
677#ifdef SIGFPE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000678 x = PyLong_FromLong(SIGFPE);
679 PyDict_SetItemString(d, "SIGFPE", x);
680 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000681#endif
682#ifdef SIGKILL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000683 x = PyLong_FromLong(SIGKILL);
684 PyDict_SetItemString(d, "SIGKILL", x);
685 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000686#endif
687#ifdef SIGBUS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000688 x = PyLong_FromLong(SIGBUS);
689 PyDict_SetItemString(d, "SIGBUS", x);
690 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000691#endif
692#ifdef SIGSEGV
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000693 x = PyLong_FromLong(SIGSEGV);
694 PyDict_SetItemString(d, "SIGSEGV", x);
695 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000696#endif
697#ifdef SIGSYS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000698 x = PyLong_FromLong(SIGSYS);
699 PyDict_SetItemString(d, "SIGSYS", x);
700 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000701#endif
702#ifdef SIGPIPE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000703 x = PyLong_FromLong(SIGPIPE);
704 PyDict_SetItemString(d, "SIGPIPE", x);
705 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000706#endif
707#ifdef SIGALRM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000708 x = PyLong_FromLong(SIGALRM);
709 PyDict_SetItemString(d, "SIGALRM", x);
710 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000711#endif
712#ifdef SIGTERM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000713 x = PyLong_FromLong(SIGTERM);
714 PyDict_SetItemString(d, "SIGTERM", x);
715 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000716#endif
717#ifdef SIGUSR1
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000718 x = PyLong_FromLong(SIGUSR1);
719 PyDict_SetItemString(d, "SIGUSR1", x);
720 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000721#endif
722#ifdef SIGUSR2
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000723 x = PyLong_FromLong(SIGUSR2);
724 PyDict_SetItemString(d, "SIGUSR2", x);
725 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000726#endif
727#ifdef SIGCLD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000728 x = PyLong_FromLong(SIGCLD);
729 PyDict_SetItemString(d, "SIGCLD", x);
730 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000731#endif
732#ifdef SIGCHLD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000733 x = PyLong_FromLong(SIGCHLD);
734 PyDict_SetItemString(d, "SIGCHLD", x);
735 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000736#endif
737#ifdef SIGPWR
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000738 x = PyLong_FromLong(SIGPWR);
739 PyDict_SetItemString(d, "SIGPWR", x);
740 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000741#endif
742#ifdef SIGIO
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000743 x = PyLong_FromLong(SIGIO);
744 PyDict_SetItemString(d, "SIGIO", x);
745 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000746#endif
747#ifdef SIGURG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000748 x = PyLong_FromLong(SIGURG);
749 PyDict_SetItemString(d, "SIGURG", x);
750 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000751#endif
752#ifdef SIGWINCH
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000753 x = PyLong_FromLong(SIGWINCH);
754 PyDict_SetItemString(d, "SIGWINCH", x);
755 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000756#endif
757#ifdef SIGPOLL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000758 x = PyLong_FromLong(SIGPOLL);
759 PyDict_SetItemString(d, "SIGPOLL", x);
760 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000761#endif
762#ifdef SIGSTOP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000763 x = PyLong_FromLong(SIGSTOP);
764 PyDict_SetItemString(d, "SIGSTOP", x);
765 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000766#endif
767#ifdef SIGTSTP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000768 x = PyLong_FromLong(SIGTSTP);
769 PyDict_SetItemString(d, "SIGTSTP", x);
770 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000771#endif
772#ifdef SIGCONT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000773 x = PyLong_FromLong(SIGCONT);
774 PyDict_SetItemString(d, "SIGCONT", x);
775 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000776#endif
777#ifdef SIGTTIN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000778 x = PyLong_FromLong(SIGTTIN);
779 PyDict_SetItemString(d, "SIGTTIN", x);
780 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000781#endif
782#ifdef SIGTTOU
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000783 x = PyLong_FromLong(SIGTTOU);
784 PyDict_SetItemString(d, "SIGTTOU", x);
785 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000786#endif
787#ifdef SIGVTALRM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000788 x = PyLong_FromLong(SIGVTALRM);
789 PyDict_SetItemString(d, "SIGVTALRM", x);
790 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000791#endif
792#ifdef SIGPROF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000793 x = PyLong_FromLong(SIGPROF);
794 PyDict_SetItemString(d, "SIGPROF", x);
795 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000796#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000797#ifdef SIGXCPU
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000798 x = PyLong_FromLong(SIGXCPU);
799 PyDict_SetItemString(d, "SIGXCPU", x);
800 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000801#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000802#ifdef SIGXFSZ
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000803 x = PyLong_FromLong(SIGXFSZ);
804 PyDict_SetItemString(d, "SIGXFSZ", x);
805 Py_XDECREF(x);
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000806#endif
Anthony Baxterf37f37d2003-07-31 10:35:29 +0000807#ifdef SIGRTMIN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000808 x = PyLong_FromLong(SIGRTMIN);
809 PyDict_SetItemString(d, "SIGRTMIN", x);
810 Py_XDECREF(x);
Anthony Baxterf37f37d2003-07-31 10:35:29 +0000811#endif
812#ifdef SIGRTMAX
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000813 x = PyLong_FromLong(SIGRTMAX);
814 PyDict_SetItemString(d, "SIGRTMAX", x);
815 Py_XDECREF(x);
Anthony Baxterf37f37d2003-07-31 10:35:29 +0000816#endif
Martin v. Löwis175af252002-01-12 11:43:25 +0000817#ifdef SIGINFO
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000818 x = PyLong_FromLong(SIGINFO);
819 PyDict_SetItemString(d, "SIGINFO", x);
820 Py_XDECREF(x);
Martin v. Löwis175af252002-01-12 11:43:25 +0000821#endif
Martin v. Löwis823725e2008-03-24 13:39:54 +0000822
823#ifdef ITIMER_REAL
824 x = PyLong_FromLong(ITIMER_REAL);
825 PyDict_SetItemString(d, "ITIMER_REAL", x);
826 Py_DECREF(x);
827#endif
828#ifdef ITIMER_VIRTUAL
829 x = PyLong_FromLong(ITIMER_VIRTUAL);
830 PyDict_SetItemString(d, "ITIMER_VIRTUAL", x);
831 Py_DECREF(x);
832#endif
833#ifdef ITIMER_PROF
834 x = PyLong_FromLong(ITIMER_PROF);
835 PyDict_SetItemString(d, "ITIMER_PROF", x);
836 Py_DECREF(x);
837#endif
838
839#if defined (HAVE_SETITIMER) || defined (HAVE_GETITIMER)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000840 ItimerError = PyErr_NewException("signal.ItimerError",
841 PyExc_IOError, NULL);
Neal Norwitz2f99b242008-08-24 05:48:10 +0000842 if (ItimerError != NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000843 PyDict_SetItemString(d, "ItimerError", ItimerError);
Martin v. Löwis823725e2008-03-24 13:39:54 +0000844#endif
845
Brian Curtineb24d742010-04-12 17:16:38 +0000846#ifdef CTRL_C_EVENT
847 x = PyLong_FromLong(CTRL_C_EVENT);
848 PyDict_SetItemString(d, "CTRL_C_EVENT", x);
849 Py_DECREF(x);
850#endif
851
852#ifdef CTRL_BREAK_EVENT
853 x = PyLong_FromLong(CTRL_BREAK_EVENT);
854 PyDict_SetItemString(d, "CTRL_BREAK_EVENT", x);
855 Py_DECREF(x);
856#endif
857
Martin v. Löwis1a214512008-06-11 05:26:20 +0000858 if (PyErr_Occurred()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000859 Py_DECREF(m);
860 m = NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000861 }
Barry Warsaw92971171997-01-03 00:14:25 +0000862
Barry Warsaw92971171997-01-03 00:14:25 +0000863 finally:
Martin v. Löwis1a214512008-06-11 05:26:20 +0000864 return m;
Guido van Rossum08c16611997-08-02 03:01:42 +0000865}
866
867static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000868finisignal(void)
Guido van Rossum08c16611997-08-02 03:01:42 +0000869{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000870 int i;
871 PyObject *func;
Guido van Rossum08c16611997-08-02 03:01:42 +0000872
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000873 PyOS_setsig(SIGINT, old_siginthandler);
874 old_siginthandler = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +0000875
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000876 for (i = 1; i < NSIG; i++) {
877 func = Handlers[i].func;
878 Handlers[i].tripped = 0;
879 Handlers[i].func = NULL;
880 if (i != SIGINT && func != NULL && func != Py_None &&
881 func != DefaultHandler && func != IgnoreHandler)
882 PyOS_setsig(i, SIG_DFL);
883 Py_XDECREF(func);
884 }
Guido van Rossum08c16611997-08-02 03:01:42 +0000885
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000886 Py_XDECREF(IntHandler);
887 IntHandler = NULL;
888 Py_XDECREF(DefaultHandler);
889 DefaultHandler = NULL;
890 Py_XDECREF(IgnoreHandler);
891 IgnoreHandler = NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000892}
893
Barry Warsaw92971171997-01-03 00:14:25 +0000894
Barry Warsaw92971171997-01-03 00:14:25 +0000895/* Declared in pyerrors.h */
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000896int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000897PyErr_CheckSignals(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000898{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000899 int i;
900 PyObject *f;
Barry Warsaw92971171997-01-03 00:14:25 +0000901
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000902 if (!is_tripped)
903 return 0;
Christian Heimesb76922a2007-12-11 01:06:40 +0000904
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000905#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000906 if (PyThread_get_thread_ident() != main_thread)
907 return 0;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000908#endif
Christian Heimesb76922a2007-12-11 01:06:40 +0000909
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000910 /*
911 * The is_tripped variable is meant to speed up the calls to
912 * PyErr_CheckSignals (both directly or via pending calls) when no
913 * signal has arrived. This variable is set to 1 when a signal arrives
914 * and it is set to 0 here, when we know some signals arrived. This way
915 * we can run the registered handlers with no signals blocked.
916 *
917 * NOTE: with this approach we can have a situation where is_tripped is
918 * 1 but we have no more signals to handle (Handlers[i].tripped
919 * is 0 for every signal i). This won't do us any harm (except
920 * we're gonna spent some cycles for nothing). This happens when
921 * we receive a signal i after we zero is_tripped and before we
922 * check Handlers[i].tripped.
923 */
924 is_tripped = 0;
Christian Heimesb76922a2007-12-11 01:06:40 +0000925
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000926 if (!(f = (PyObject *)PyEval_GetFrame()))
927 f = Py_None;
Christian Heimesb76922a2007-12-11 01:06:40 +0000928
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000929 for (i = 1; i < NSIG; i++) {
930 if (Handlers[i].tripped) {
931 PyObject *result = NULL;
932 PyObject *arglist = Py_BuildValue("(iO)", i, f);
933 Handlers[i].tripped = 0;
Barry Warsaw92971171997-01-03 00:14:25 +0000934
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000935 if (arglist) {
936 result = PyEval_CallObject(Handlers[i].func,
937 arglist);
938 Py_DECREF(arglist);
939 }
940 if (!result)
941 return -1;
Barry Warsaw92971171997-01-03 00:14:25 +0000942
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000943 Py_DECREF(result);
944 }
945 }
Christian Heimesb76922a2007-12-11 01:06:40 +0000946
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000947 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000948}
949
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000950
Barry Warsaw92971171997-01-03 00:14:25 +0000951/* Replacements for intrcheck.c functionality
952 * Declared in pyerrors.h
953 */
954void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000955PyErr_SetInterrupt(void)
Barry Warsaw92971171997-01-03 00:14:25 +0000956{
Victor Stinner6c9b35b2011-04-18 16:25:56 +0200957 trip_signal(SIGINT);
Barry Warsaw92971171997-01-03 00:14:25 +0000958}
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000959
960void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000961PyOS_InitInterrupts(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000962{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000963 PyObject *m = PyInit_signal();
964 if (m) {
Victor Stinner49d3f252010-10-17 01:24:53 +0000965 _PyImport_FixupBuiltin(m, "signal");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000966 Py_DECREF(m);
967 }
Guido van Rossum08c16611997-08-02 03:01:42 +0000968}
969
970void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000971PyOS_FiniInterrupts(void)
Guido van Rossum08c16611997-08-02 03:01:42 +0000972{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000973 finisignal();
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000974}
975
976int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000977PyOS_InterruptOccurred(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000978{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000979 if (Handlers[SIGINT].tripped) {
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000980#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000981 if (PyThread_get_thread_ident() != main_thread)
982 return 0;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000983#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000984 Handlers[SIGINT].tripped = 0;
985 return 1;
986 }
987 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000988}
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000989
Gregory P. Smith9463e3a2012-11-10 20:33:07 -0800990static void
991_clear_pending_signals(void)
992{
993 int i;
994 if (!is_tripped)
995 return;
996 is_tripped = 0;
997 for (i = 1; i < NSIG; ++i) {
998 Handlers[i].tripped = 0;
999 }
1000}
1001
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001002void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001003PyOS_AfterFork(void)
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001004{
Gregory P. Smith9463e3a2012-11-10 20:33:07 -08001005 /* Clear the signal flags after forking so that they aren't handled
1006 * in both processes if they came in just before the fork() but before
1007 * the interpreter had an opportunity to call the handlers. issue9535. */
1008 _clear_pending_signals();
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001009#ifdef WITH_THREAD
Charles-François Natali6d0d24e2012-02-02 20:31:42 +01001010 /* PyThread_ReInitTLS() must be called early, to make sure that the TLS API
1011 * can be called safely. */
1012 PyThread_ReInitTLS();
Antoine Pitrou0c759fe2011-04-27 19:28:05 +02001013 _PyGILState_Reinit();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001014 PyEval_ReInitThreads();
1015 main_thread = PyThread_get_thread_ident();
1016 main_pid = getpid();
1017 _PyImport_ReInitLock();
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001018#endif
1019}