blob: bb7e4b0dcb070a77668dd0836bc7645da1832c92 [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#include "intrcheck.h"
8
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00009#ifdef MS_WINDOWS
Brian Curtine5aa8862010-04-02 23:26:06 +000010#include <Windows.h>
Andrew M. Kuchling4b81bc72010-02-22 23:12:00 +000011#ifdef HAVE_PROCESS_H
Guido van Rossum644a12b1997-04-09 19:24:53 +000012#include <process.h>
13#endif
Andrew M. Kuchling4b81bc72010-02-22 23:12:00 +000014#endif
Guido van Rossum644a12b1997-04-09 19:24:53 +000015
Andrew M. Kuchling4b81bc72010-02-22 23:12:00 +000016#ifdef HAVE_SIGNAL_H
Guido van Rossum398d9fe1994-05-11 08:59:13 +000017#include <signal.h>
Andrew M. Kuchling4b81bc72010-02-22 23:12:00 +000018#endif
19#ifdef HAVE_SYS_STAT_H
Guido van Rossum02de8972007-12-19 19:41:06 +000020#include <sys/stat.h>
Andrew M. Kuchling4b81bc72010-02-22 23:12:00 +000021#endif
Martin v. Löwisb74d0842008-03-24 13:54:23 +000022#ifdef HAVE_SYS_TIME_H
Martin v. Löwisaef18b12008-03-24 13:31:16 +000023#include <sys/time.h>
Martin v. Löwisb74d0842008-03-24 13:54:23 +000024#endif
Guido van Rossum02de8972007-12-19 19:41:06 +000025
Guido van Rossumbb4ba121994-06-23 11:25:45 +000026#ifndef SIG_ERR
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +000027#define SIG_ERR ((PyOS_sighandler_t)(-1))
Guido van Rossumbb4ba121994-06-23 11:25:45 +000028#endif
29
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000030#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000031#define NSIG 12
32#include <process.h>
33#endif
34
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000035#ifndef NSIG
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000036# if defined(_NSIG)
37# define NSIG _NSIG /* For BSD/SysV */
38# elif defined(_SIGMAX)
39# define NSIG (_SIGMAX + 1) /* For QNX */
40# elif defined(SIGMAX)
41# define NSIG (SIGMAX + 1) /* For djgpp */
42# else
43# define NSIG 64 /* Use a reasonable default value */
44# endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000045#endif
46
47
Guido van Rossumbb4ba121994-06-23 11:25:45 +000048/*
49 NOTES ON THE INTERACTION BETWEEN SIGNALS AND THREADS
50
51 When threads are supported, we want the following semantics:
52
53 - only the main thread can set a signal handler
54 - any thread can get a signal handler
55 - signals are only delivered to the main thread
56
57 I.e. we don't support "synchronous signals" like SIGFPE (catching
58 this doesn't make much sense in Python anyway) nor do we support
59 signals as a means of inter-thread communication, since not all
60 thread implementations support that (at least our thread library
61 doesn't).
62
63 We still have the problem that in some implementations signals
64 generated by the keyboard (e.g. SIGINT) are delivered to all
65 threads (e.g. SGI), while in others (e.g. Solaris) such signals are
66 delivered to one random thread (an intermediate possibility would
Guido van Rossuma3c04b01995-01-12 11:29:01 +000067 be to deliver it to the main thread -- POSIX?). For now, we have
Guido van Rossumbb4ba121994-06-23 11:25:45 +000068 a working implementation that works in all three cases -- the
69 handler ignores signals if getpid() isn't the same as in the main
70 thread. XXX This is a hack.
71
Guido van Rossum9e8181b2000-09-19 00:46:46 +000072 GNU pth is a user-space threading library, and as such, all threads
73 run within the same process. In this case, if the currently running
74 thread is not the main_thread, send the signal to the main_thread.
Guido van Rossumbb4ba121994-06-23 11:25:45 +000075*/
76
77#ifdef WITH_THREAD
Guido van Rossum295b8e51997-06-06 21:16:41 +000078#include <sys/types.h> /* For pid_t */
Guido van Rossum49b56061998-10-01 20:42:43 +000079#include "pythread.h"
Guido van Rossumbb4ba121994-06-23 11:25:45 +000080static long main_thread;
81static pid_t main_pid;
82#endif
83
Barry Warsaw92971171997-01-03 00:14:25 +000084static struct {
85 int tripped;
86 PyObject *func;
87} Handlers[NSIG];
Guido van Rossum398d9fe1994-05-11 08:59:13 +000088
Guido van Rossum02de8972007-12-19 19:41:06 +000089static sig_atomic_t wakeup_fd = -1;
90
Guido van Rossum137c49c2007-12-10 23:00:12 +000091/* Speed up sigcheck() when none tripped */
92static volatile sig_atomic_t is_tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +000093
Barry Warsaw92971171997-01-03 00:14:25 +000094static PyObject *DefaultHandler;
95static PyObject *IgnoreHandler;
96static PyObject *IntHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +000097
Martin v. Löwisf58de1b2001-03-06 12:13:56 +000098/* On Solaris 8, gcc will produce a warning that the function
99 declaration is not a prototype. This is caused by the definition of
100 SIG_DFL as (void (*)())0; the correct declaration would have been
101 (void (*)(int))0. */
102
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000103static PyOS_sighandler_t old_siginthandler = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +0000104
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000105#ifdef HAVE_GETITIMER
106static PyObject *ItimerError;
107
108/* auxiliary functions for setitimer/getitimer */
109static void
110timeval_from_double(double d, struct timeval *tv)
111{
112 tv->tv_sec = floor(d);
113 tv->tv_usec = fmod(d, 1.0) * 1000000.0;
114}
115
Christian Heimes32a66a02008-10-02 19:47:50 +0000116Py_LOCAL_INLINE(double)
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000117double_from_timeval(struct timeval *tv)
118{
119 return tv->tv_sec + (double)(tv->tv_usec / 1000000.0);
120}
121
122static PyObject *
123itimer_retval(struct itimerval *iv)
124{
125 PyObject *r, *v;
126
127 r = PyTuple_New(2);
128 if (r == NULL)
129 return NULL;
130
131 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_value)))) {
132 Py_DECREF(r);
133 return NULL;
134 }
135
136 PyTuple_SET_ITEM(r, 0, v);
137
138 if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_interval)))) {
139 Py_DECREF(r);
140 return NULL;
141 }
142
143 PyTuple_SET_ITEM(r, 1, v);
144
145 return r;
146}
147#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000148
Guido van Rossume4485b01994-09-07 14:32:49 +0000149static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000150signal_default_int_handler(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000151{
Guido van Rossume4485b01994-09-07 14:32:49 +0000152 PyErr_SetNone(PyExc_KeyboardInterrupt);
Barry Warsaw92971171997-01-03 00:14:25 +0000153 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000154}
155
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000156PyDoc_STRVAR(default_int_handler_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000157"default_int_handler(...)\n\
158\n\
Michael W. Hudson24ec2112004-06-17 15:55:53 +0000159The default handler for SIGINT installed by Python.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000160It raises KeyboardInterrupt.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000161
Thomas Wouters0796b002000-07-22 23:49:30 +0000162
163static int
164checksignals_witharg(void * unused)
165{
166 return PyErr_CheckSignals();
167}
168
Tim Peters4f1b2082000-07-23 21:18:09 +0000169static void
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000170signal_handler(int sig_num)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000171{
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000172#ifdef WITH_THREAD
Guido van Rossum9e8181b2000-09-19 00:46:46 +0000173#ifdef WITH_PTH
174 if (PyThread_get_thread_ident() != main_thread) {
175 pth_raise(*(pth_t *) main_thread, sig_num);
176 return;
177 }
178#endif
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000179 /* See NOTES section above */
180 if (getpid() == main_pid) {
181#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000182 Handlers[sig_num].tripped = 1;
Guido van Rossum137c49c2007-12-10 23:00:12 +0000183 /* Set is_tripped after setting .tripped, as it gets
184 cleared in PyErr_CheckSignals() before .tripped. */
185 is_tripped = 1;
Thomas Wouters0796b002000-07-22 23:49:30 +0000186 Py_AddPendingCall(checksignals_witharg, NULL);
Guido van Rossum02de8972007-12-19 19:41:06 +0000187 if (wakeup_fd != -1)
188 write(wakeup_fd, "\0", 1);
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000189#ifdef WITH_THREAD
190 }
191#endif
Guido van Rossum602099a1994-09-14 13:32:22 +0000192#ifdef SIGCHLD
193 if (sig_num == SIGCHLD) {
194 /* To avoid infinite recursion, this signal remains
195 reset until explicit re-instated.
196 Don't clear the 'func' field as it is our pointer
197 to the Python handler... */
Tim Peters4f1b2082000-07-23 21:18:09 +0000198 return;
Guido van Rossum602099a1994-09-14 13:32:22 +0000199 }
200#endif
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000201 PyOS_setsig(sig_num, signal_handler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000202}
Guido van Rossume4485b01994-09-07 14:32:49 +0000203
Guido van Rossum06d511d1995-03-10 15:13:48 +0000204
Guido van Rossum1171ee61997-08-22 20:42:00 +0000205#ifdef HAVE_ALARM
Guido van Rossume4485b01994-09-07 14:32:49 +0000206static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000207signal_alarm(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000208{
209 int t;
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000210 if (!PyArg_ParseTuple(args, "i:alarm", &t))
Barry Warsaw92971171997-01-03 00:14:25 +0000211 return NULL;
Guido van Rossume4485b01994-09-07 14:32:49 +0000212 /* alarm() returns the number of seconds remaining */
Fred Drakedff3a372001-07-19 21:29:49 +0000213 return PyInt_FromLong((long)alarm(t));
Guido van Rossumaa0f4c71994-08-23 13:49:37 +0000214}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000215
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000216PyDoc_STRVAR(alarm_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000217"alarm(seconds)\n\
218\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000219Arrange for SIGALRM to arrive after the given number of seconds.");
Guido van Rossum06d511d1995-03-10 15:13:48 +0000220#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000221
Guido van Rossum1171ee61997-08-22 20:42:00 +0000222#ifdef HAVE_PAUSE
Guido van Rossuma597dde1995-01-10 20:56:29 +0000223static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000224signal_pause(PyObject *self)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000225{
Guido van Rossuma597dde1995-01-10 20:56:29 +0000226 Py_BEGIN_ALLOW_THREADS
Barry Warsaw92971171997-01-03 00:14:25 +0000227 (void)pause();
Guido van Rossuma597dde1995-01-10 20:56:29 +0000228 Py_END_ALLOW_THREADS
Barry Warsaw92971171997-01-03 00:14:25 +0000229 /* make sure that any exceptions that got raised are propagated
230 * back into Python
231 */
232 if (PyErr_CheckSignals())
233 return NULL;
234
Guido van Rossume4485b01994-09-07 14:32:49 +0000235 Py_INCREF(Py_None);
236 return Py_None;
237}
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000238PyDoc_STRVAR(pause_doc,
Barry Warsaw1ee36ff1998-07-21 22:41:18 +0000239"pause()\n\
240\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000241Wait until a signal arrives.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000242
Guido van Rossum06d511d1995-03-10 15:13:48 +0000243#endif
Guido van Rossume4485b01994-09-07 14:32:49 +0000244
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000245
Guido van Rossume4485b01994-09-07 14:32:49 +0000246static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000247signal_signal(PyObject *self, PyObject *args)
Guido van Rossume4485b01994-09-07 14:32:49 +0000248{
249 PyObject *obj;
250 int sig_num;
251 PyObject *old_handler;
Tim Peters4f1b2082000-07-23 21:18:09 +0000252 void (*func)(int);
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000253 if (!PyArg_ParseTuple(args, "iO:signal", &sig_num, &obj))
Barry Warsaw92971171997-01-03 00:14:25 +0000254 return NULL;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000255#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000256 if (PyThread_get_thread_ident() != main_thread) {
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000257 PyErr_SetString(PyExc_ValueError,
258 "signal only works in main thread");
Barry Warsaw92971171997-01-03 00:14:25 +0000259 return NULL;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000260 }
261#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000262 if (sig_num < 1 || sig_num >= NSIG) {
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000263 PyErr_SetString(PyExc_ValueError,
264 "signal number out of range");
Barry Warsaw92971171997-01-03 00:14:25 +0000265 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000266 }
Barry Warsaw92971171997-01-03 00:14:25 +0000267 if (obj == IgnoreHandler)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000268 func = SIG_IGN;
Barry Warsaw92971171997-01-03 00:14:25 +0000269 else if (obj == DefaultHandler)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000270 func = SIG_DFL;
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000271 else if (!PyCallable_Check(obj)) {
Guido van Rossume4485b01994-09-07 14:32:49 +0000272 PyErr_SetString(PyExc_TypeError,
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000273"signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object");
Barry Warsaw92971171997-01-03 00:14:25 +0000274 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000275 }
276 else
Barry Warsaw92971171997-01-03 00:14:25 +0000277 func = signal_handler;
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000278 if (PyOS_setsig(sig_num, func) == SIG_ERR) {
Guido van Rossume4485b01994-09-07 14:32:49 +0000279 PyErr_SetFromErrno(PyExc_RuntimeError);
Barry Warsaw92971171997-01-03 00:14:25 +0000280 return NULL;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000281 }
Barry Warsaw92971171997-01-03 00:14:25 +0000282 old_handler = Handlers[sig_num].func;
283 Handlers[sig_num].tripped = 0;
Guido van Rossume4485b01994-09-07 14:32:49 +0000284 Py_INCREF(obj);
Barry Warsaw92971171997-01-03 00:14:25 +0000285 Handlers[sig_num].func = obj;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000286 return old_handler;
287}
288
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000289PyDoc_STRVAR(signal_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000290"signal(sig, action) -> action\n\
291\n\
292Set the action for the given signal. The action can be SIG_DFL,\n\
293SIG_IGN, or a callable Python object. The previous action is\n\
294returned. See getsignal() for possible return values.\n\
295\n\
296*** IMPORTANT NOTICE ***\n\
297A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000298the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000299
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000300
Guido van Rossume4485b01994-09-07 14:32:49 +0000301static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000302signal_getsignal(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000303{
304 int sig_num;
Guido van Rossume4485b01994-09-07 14:32:49 +0000305 PyObject *old_handler;
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000306 if (!PyArg_ParseTuple(args, "i:getsignal", &sig_num))
Barry Warsaw92971171997-01-03 00:14:25 +0000307 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000308 if (sig_num < 1 || sig_num >= NSIG) {
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000309 PyErr_SetString(PyExc_ValueError,
310 "signal number out of range");
Barry Warsaw92971171997-01-03 00:14:25 +0000311 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000312 }
Barry Warsaw92971171997-01-03 00:14:25 +0000313 old_handler = Handlers[sig_num].func;
Guido van Rossume4485b01994-09-07 14:32:49 +0000314 Py_INCREF(old_handler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000315 return old_handler;
316}
317
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000318PyDoc_STRVAR(getsignal_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000319"getsignal(sig) -> action\n\
320\n\
321Return the current action for the given signal. The return value can be:\n\
322SIG_IGN -- if the signal is being ignored\n\
323SIG_DFL -- if the default action for the signal is in effect\n\
324None -- if an unknown handler is in effect\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000325anything else -- the callable Python object used as a handler");
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000326
Facundo Batista7e251e82008-02-23 15:07:35 +0000327#ifdef HAVE_SIGINTERRUPT
328PyDoc_STRVAR(siginterrupt_doc,
329"siginterrupt(sig, flag) -> None\n\
330change system call restart behaviour: if flag is False, system calls\n\
331will be restarted when interrupted by signal sig, else system calls\n\
332will be interrupted.");
333
334static PyObject *
335signal_siginterrupt(PyObject *self, PyObject *args)
336{
337 int sig_num;
338 int flag;
339
340 if (!PyArg_ParseTuple(args, "ii:siginterrupt", &sig_num, &flag))
341 return NULL;
342 if (sig_num < 1 || sig_num >= NSIG) {
343 PyErr_SetString(PyExc_ValueError,
344 "signal number out of range");
345 return NULL;
346 }
347 if (siginterrupt(sig_num, flag)<0) {
348 PyErr_SetFromErrno(PyExc_RuntimeError);
349 return NULL;
350 }
351
352 Py_INCREF(Py_None);
353 return Py_None;
354}
355
356#endif
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000357
Guido van Rossum02de8972007-12-19 19:41:06 +0000358static PyObject *
359signal_set_wakeup_fd(PyObject *self, PyObject *args)
360{
361 struct stat buf;
362 int fd, old_fd;
363 if (!PyArg_ParseTuple(args, "i:set_wakeup_fd", &fd))
364 return NULL;
365#ifdef WITH_THREAD
366 if (PyThread_get_thread_ident() != main_thread) {
367 PyErr_SetString(PyExc_ValueError,
368 "set_wakeup_fd only works in main thread");
369 return NULL;
370 }
371#endif
372 if (fd != -1 && fstat(fd, &buf) != 0) {
373 PyErr_SetString(PyExc_ValueError, "invalid fd");
374 return NULL;
375 }
376 old_fd = wakeup_fd;
377 wakeup_fd = fd;
378 return PyLong_FromLong(old_fd);
379}
380
381PyDoc_STRVAR(set_wakeup_fd_doc,
382"set_wakeup_fd(fd) -> fd\n\
383\n\
384Sets the fd to be written to (with '\\0') when a signal\n\
385comes in. A library can use this to wakeup select or poll.\n\
386The previous fd is returned.\n\
387\n\
388The fd must be non-blocking.");
389
390/* C API for the same, without all the error checking */
391int
392PySignal_SetWakeupFd(int fd)
393{
394 int old_fd = wakeup_fd;
395 if (fd < 0)
396 fd = -1;
397 wakeup_fd = fd;
398 return old_fd;
399}
400
401
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000402#ifdef HAVE_SETITIMER
403static PyObject *
404signal_setitimer(PyObject *self, PyObject *args)
405{
406 double first;
407 double interval = 0;
408 int which;
409 struct itimerval new, old;
410
411 if(!PyArg_ParseTuple(args, "id|d:setitimer", &which, &first, &interval))
412 return NULL;
413
414 timeval_from_double(first, &new.it_value);
415 timeval_from_double(interval, &new.it_interval);
416 /* Let OS check "which" value */
417 if (setitimer(which, &new, &old) != 0) {
418 PyErr_SetFromErrno(ItimerError);
419 return NULL;
420 }
421
422 return itimer_retval(&old);
423}
424
425PyDoc_STRVAR(setitimer_doc,
426"setitimer(which, seconds[, interval])\n\
427\n\
428Sets given itimer (one of ITIMER_REAL, ITIMER_VIRTUAL\n\
429or ITIMER_PROF) to fire after value seconds and after\n\
430that every interval seconds.\n\
431The itimer can be cleared by setting seconds to zero.\n\
432\n\
433Returns old values as a tuple: (delay, interval).");
434#endif
435
436
437#ifdef HAVE_GETITIMER
438static PyObject *
439signal_getitimer(PyObject *self, PyObject *args)
440{
441 int which;
442 struct itimerval old;
443
444 if (!PyArg_ParseTuple(args, "i:getitimer", &which))
445 return NULL;
446
447 if (getitimer(which, &old) != 0) {
448 PyErr_SetFromErrno(ItimerError);
449 return NULL;
450 }
451
452 return itimer_retval(&old);
453}
454
455PyDoc_STRVAR(getitimer_doc,
456"getitimer(which)\n\
457\n\
458Returns current value of given itimer.");
459#endif
460
461
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000462/* List of functions defined in the module */
Barry Warsaw92971171997-01-03 00:14:25 +0000463static PyMethodDef signal_methods[] = {
Guido van Rossum1171ee61997-08-22 20:42:00 +0000464#ifdef HAVE_ALARM
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000465 {"alarm", signal_alarm, METH_VARARGS, alarm_doc},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000466#endif
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000467#ifdef HAVE_SETITIMER
468 {"setitimer", signal_setitimer, METH_VARARGS, setitimer_doc},
469#endif
470#ifdef HAVE_GETITIMER
471 {"getitimer", signal_getitimer, METH_VARARGS, getitimer_doc},
472#endif
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000473 {"signal", signal_signal, METH_VARARGS, signal_doc},
474 {"getsignal", signal_getsignal, METH_VARARGS, getsignal_doc},
Guido van Rossum02de8972007-12-19 19:41:06 +0000475 {"set_wakeup_fd", signal_set_wakeup_fd, METH_VARARGS, set_wakeup_fd_doc},
Facundo Batista7e251e82008-02-23 15:07:35 +0000476#ifdef HAVE_SIGINTERRUPT
477 {"siginterrupt", signal_siginterrupt, METH_VARARGS, siginterrupt_doc},
478#endif
Guido van Rossum1171ee61997-08-22 20:42:00 +0000479#ifdef HAVE_PAUSE
Neil Schemenauer78662cf2002-03-28 21:04:14 +0000480 {"pause", (PyCFunction)signal_pause,
481 METH_NOARGS,pause_doc},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000482#endif
Guido van Rossum02de8972007-12-19 19:41:06 +0000483 {"default_int_handler", signal_default_int_handler,
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000484 METH_VARARGS, default_int_handler_doc},
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000485 {NULL, NULL} /* sentinel */
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000486};
487
Barry Warsaw92971171997-01-03 00:14:25 +0000488
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000489PyDoc_STRVAR(module_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000490"This module provides mechanisms to use signal handlers in Python.\n\
491\n\
492Functions:\n\
493\n\
494alarm() -- cause SIGALRM after a specified time [Unix only]\n\
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000495setitimer() -- cause a signal (described below) after a specified\n\
496 float time and the timer may restart then [Unix only]\n\
497getitimer() -- get current value of timer [Unix only]\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000498signal() -- set the action for a given signal\n\
499getsignal() -- get the signal action for a given signal\n\
500pause() -- wait until a signal arrives [Unix only]\n\
501default_int_handler() -- default SIGINT handler\n\
502\n\
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000503signal constants:\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000504SIG_DFL -- used to refer to the system default handler\n\
505SIG_IGN -- used to ignore the signal\n\
506NSIG -- number of defined signals\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000507SIGINT, SIGTERM, etc. -- signal numbers\n\
508\n\
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000509itimer constants:\n\
510ITIMER_REAL -- decrements in real time, and delivers SIGALRM upon\n\
511 expiration\n\
512ITIMER_VIRTUAL -- decrements only when the process is executing,\n\
513 and delivers SIGVTALRM upon expiration\n\
514ITIMER_PROF -- decrements both when the process is executing and\n\
515 when the system is executing on behalf of the process.\n\
516 Coupled with ITIMER_VIRTUAL, this timer is usually\n\
517 used to profile the time spent by the application\n\
518 in user and kernel space. SIGPROF is delivered upon\n\
519 expiration.\n\
520\n\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000521*** IMPORTANT NOTICE ***\n\
522A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000523the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000524
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000525PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000526initsignal(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000527{
Guido van Rossume4485b01994-09-07 14:32:49 +0000528 PyObject *m, *d, *x;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000529 int i;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000530
531#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000532 main_thread = PyThread_get_thread_ident();
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000533 main_pid = getpid();
534#endif
535
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000536 /* Create the module and add the functions */
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000537 m = Py_InitModule3("signal", signal_methods, module_doc);
Neal Norwitz1ac754f2006-01-19 06:09:39 +0000538 if (m == NULL)
539 return;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000540
541 /* Add some symbolic constants to the module */
Guido van Rossume4485b01994-09-07 14:32:49 +0000542 d = PyModule_GetDict(m);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000543
Guido van Rossum276fa432000-06-30 23:04:18 +0000544 x = DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL);
Barry Warsaw92971171997-01-03 00:14:25 +0000545 if (!x || PyDict_SetItemString(d, "SIG_DFL", x) < 0)
546 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000547
Guido van Rossum276fa432000-06-30 23:04:18 +0000548 x = IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN);
Barry Warsaw92971171997-01-03 00:14:25 +0000549 if (!x || PyDict_SetItemString(d, "SIG_IGN", x) < 0)
550 goto finally;
551
552 x = PyInt_FromLong((long)NSIG);
553 if (!x || PyDict_SetItemString(d, "NSIG", x) < 0)
554 goto finally;
Guido van Rossum08c16611997-08-02 03:01:42 +0000555 Py_DECREF(x);
Barry Warsaw92971171997-01-03 00:14:25 +0000556
557 x = IntHandler = PyDict_GetItemString(d, "default_int_handler");
558 if (!x)
559 goto finally;
Guido van Rossum08c16611997-08-02 03:01:42 +0000560 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +0000561
562 Handlers[0].tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000563 for (i = 1; i < NSIG; i++) {
Tim Peters4f1b2082000-07-23 21:18:09 +0000564 void (*t)(int);
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000565 t = PyOS_getsig(i);
Barry Warsaw92971171997-01-03 00:14:25 +0000566 Handlers[i].tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000567 if (t == SIG_DFL)
Barry Warsaw92971171997-01-03 00:14:25 +0000568 Handlers[i].func = DefaultHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000569 else if (t == SIG_IGN)
Barry Warsaw92971171997-01-03 00:14:25 +0000570 Handlers[i].func = IgnoreHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000571 else
Barry Warsaw92971171997-01-03 00:14:25 +0000572 Handlers[i].func = Py_None; /* None of our business */
573 Py_INCREF(Handlers[i].func);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000574 }
Barry Warsaw92971171997-01-03 00:14:25 +0000575 if (Handlers[SIGINT].func == DefaultHandler) {
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000576 /* Install default int handler */
Guido van Rossum08c16611997-08-02 03:01:42 +0000577 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +0000578 Py_DECREF(Handlers[SIGINT].func);
579 Handlers[SIGINT].func = IntHandler;
Neal Norwitz41785152002-06-13 21:42:51 +0000580 old_siginthandler = PyOS_setsig(SIGINT, signal_handler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000581 }
582
583#ifdef SIGHUP
Guido van Rossume4485b01994-09-07 14:32:49 +0000584 x = PyInt_FromLong(SIGHUP);
585 PyDict_SetItemString(d, "SIGHUP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000586 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000587#endif
588#ifdef SIGINT
Guido van Rossume4485b01994-09-07 14:32:49 +0000589 x = PyInt_FromLong(SIGINT);
590 PyDict_SetItemString(d, "SIGINT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000591 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000592#endif
Tim Peters1ce3cf72001-10-01 17:58:40 +0000593#ifdef SIGBREAK
594 x = PyInt_FromLong(SIGBREAK);
595 PyDict_SetItemString(d, "SIGBREAK", x);
596 Py_XDECREF(x);
597#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000598#ifdef SIGQUIT
Guido van Rossume4485b01994-09-07 14:32:49 +0000599 x = PyInt_FromLong(SIGQUIT);
600 PyDict_SetItemString(d, "SIGQUIT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000601 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000602#endif
603#ifdef SIGILL
Guido van Rossume4485b01994-09-07 14:32:49 +0000604 x = PyInt_FromLong(SIGILL);
605 PyDict_SetItemString(d, "SIGILL", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000606 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000607#endif
608#ifdef SIGTRAP
Guido van Rossume4485b01994-09-07 14:32:49 +0000609 x = PyInt_FromLong(SIGTRAP);
610 PyDict_SetItemString(d, "SIGTRAP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000611 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000612#endif
613#ifdef SIGIOT
Guido van Rossume4485b01994-09-07 14:32:49 +0000614 x = PyInt_FromLong(SIGIOT);
615 PyDict_SetItemString(d, "SIGIOT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000616 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000617#endif
618#ifdef SIGABRT
Guido van Rossume4485b01994-09-07 14:32:49 +0000619 x = PyInt_FromLong(SIGABRT);
620 PyDict_SetItemString(d, "SIGABRT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000621 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000622#endif
623#ifdef SIGEMT
Guido van Rossume4485b01994-09-07 14:32:49 +0000624 x = PyInt_FromLong(SIGEMT);
625 PyDict_SetItemString(d, "SIGEMT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000626 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000627#endif
628#ifdef SIGFPE
Guido van Rossume4485b01994-09-07 14:32:49 +0000629 x = PyInt_FromLong(SIGFPE);
630 PyDict_SetItemString(d, "SIGFPE", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000631 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000632#endif
633#ifdef SIGKILL
Guido van Rossume4485b01994-09-07 14:32:49 +0000634 x = PyInt_FromLong(SIGKILL);
635 PyDict_SetItemString(d, "SIGKILL", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000636 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000637#endif
638#ifdef SIGBUS
Guido van Rossume4485b01994-09-07 14:32:49 +0000639 x = PyInt_FromLong(SIGBUS);
640 PyDict_SetItemString(d, "SIGBUS", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000641 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000642#endif
643#ifdef SIGSEGV
Guido van Rossume4485b01994-09-07 14:32:49 +0000644 x = PyInt_FromLong(SIGSEGV);
645 PyDict_SetItemString(d, "SIGSEGV", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000646 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000647#endif
648#ifdef SIGSYS
Guido van Rossume4485b01994-09-07 14:32:49 +0000649 x = PyInt_FromLong(SIGSYS);
650 PyDict_SetItemString(d, "SIGSYS", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000651 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000652#endif
653#ifdef SIGPIPE
Guido van Rossume4485b01994-09-07 14:32:49 +0000654 x = PyInt_FromLong(SIGPIPE);
655 PyDict_SetItemString(d, "SIGPIPE", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000656 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000657#endif
658#ifdef SIGALRM
Guido van Rossume4485b01994-09-07 14:32:49 +0000659 x = PyInt_FromLong(SIGALRM);
660 PyDict_SetItemString(d, "SIGALRM", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000661 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000662#endif
663#ifdef SIGTERM
Guido van Rossume4485b01994-09-07 14:32:49 +0000664 x = PyInt_FromLong(SIGTERM);
665 PyDict_SetItemString(d, "SIGTERM", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000666 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000667#endif
668#ifdef SIGUSR1
Guido van Rossume4485b01994-09-07 14:32:49 +0000669 x = PyInt_FromLong(SIGUSR1);
670 PyDict_SetItemString(d, "SIGUSR1", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000671 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000672#endif
673#ifdef SIGUSR2
Guido van Rossume4485b01994-09-07 14:32:49 +0000674 x = PyInt_FromLong(SIGUSR2);
675 PyDict_SetItemString(d, "SIGUSR2", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000676 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000677#endif
678#ifdef SIGCLD
Guido van Rossume4485b01994-09-07 14:32:49 +0000679 x = PyInt_FromLong(SIGCLD);
680 PyDict_SetItemString(d, "SIGCLD", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000681 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000682#endif
683#ifdef SIGCHLD
Guido van Rossume4485b01994-09-07 14:32:49 +0000684 x = PyInt_FromLong(SIGCHLD);
685 PyDict_SetItemString(d, "SIGCHLD", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000686 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000687#endif
688#ifdef SIGPWR
Guido van Rossume4485b01994-09-07 14:32:49 +0000689 x = PyInt_FromLong(SIGPWR);
690 PyDict_SetItemString(d, "SIGPWR", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000691 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000692#endif
693#ifdef SIGIO
Guido van Rossume4485b01994-09-07 14:32:49 +0000694 x = PyInt_FromLong(SIGIO);
695 PyDict_SetItemString(d, "SIGIO", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000696 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000697#endif
698#ifdef SIGURG
Guido van Rossume4485b01994-09-07 14:32:49 +0000699 x = PyInt_FromLong(SIGURG);
700 PyDict_SetItemString(d, "SIGURG", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000701 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000702#endif
703#ifdef SIGWINCH
Guido van Rossume4485b01994-09-07 14:32:49 +0000704 x = PyInt_FromLong(SIGWINCH);
705 PyDict_SetItemString(d, "SIGWINCH", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000706 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000707#endif
708#ifdef SIGPOLL
Guido van Rossume4485b01994-09-07 14:32:49 +0000709 x = PyInt_FromLong(SIGPOLL);
710 PyDict_SetItemString(d, "SIGPOLL", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000711 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000712#endif
713#ifdef SIGSTOP
Guido van Rossume4485b01994-09-07 14:32:49 +0000714 x = PyInt_FromLong(SIGSTOP);
715 PyDict_SetItemString(d, "SIGSTOP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000716 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000717#endif
718#ifdef SIGTSTP
Guido van Rossume4485b01994-09-07 14:32:49 +0000719 x = PyInt_FromLong(SIGTSTP);
720 PyDict_SetItemString(d, "SIGTSTP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000721 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000722#endif
723#ifdef SIGCONT
Guido van Rossume4485b01994-09-07 14:32:49 +0000724 x = PyInt_FromLong(SIGCONT);
725 PyDict_SetItemString(d, "SIGCONT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000726 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000727#endif
728#ifdef SIGTTIN
Guido van Rossume4485b01994-09-07 14:32:49 +0000729 x = PyInt_FromLong(SIGTTIN);
730 PyDict_SetItemString(d, "SIGTTIN", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000731 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000732#endif
733#ifdef SIGTTOU
Guido van Rossume4485b01994-09-07 14:32:49 +0000734 x = PyInt_FromLong(SIGTTOU);
735 PyDict_SetItemString(d, "SIGTTOU", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000736 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000737#endif
738#ifdef SIGVTALRM
Guido van Rossume4485b01994-09-07 14:32:49 +0000739 x = PyInt_FromLong(SIGVTALRM);
740 PyDict_SetItemString(d, "SIGVTALRM", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000741 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000742#endif
743#ifdef SIGPROF
Guido van Rossume4485b01994-09-07 14:32:49 +0000744 x = PyInt_FromLong(SIGPROF);
745 PyDict_SetItemString(d, "SIGPROF", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000746 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000747#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000748#ifdef SIGXCPU
749 x = PyInt_FromLong(SIGXCPU);
750 PyDict_SetItemString(d, "SIGXCPU", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000751 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000752#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000753#ifdef SIGXFSZ
754 x = PyInt_FromLong(SIGXFSZ);
755 PyDict_SetItemString(d, "SIGXFSZ", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000756 Py_XDECREF(x);
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000757#endif
Anthony Baxterf37f37d2003-07-31 10:35:29 +0000758#ifdef SIGRTMIN
759 x = PyInt_FromLong(SIGRTMIN);
760 PyDict_SetItemString(d, "SIGRTMIN", x);
761 Py_XDECREF(x);
762#endif
763#ifdef SIGRTMAX
764 x = PyInt_FromLong(SIGRTMAX);
765 PyDict_SetItemString(d, "SIGRTMAX", x);
766 Py_XDECREF(x);
767#endif
Martin v. Löwis175af252002-01-12 11:43:25 +0000768#ifdef SIGINFO
769 x = PyInt_FromLong(SIGINFO);
770 PyDict_SetItemString(d, "SIGINFO", x);
771 Py_XDECREF(x);
772#endif
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000773
774#ifdef ITIMER_REAL
775 x = PyLong_FromLong(ITIMER_REAL);
776 PyDict_SetItemString(d, "ITIMER_REAL", x);
777 Py_DECREF(x);
778#endif
779#ifdef ITIMER_VIRTUAL
780 x = PyLong_FromLong(ITIMER_VIRTUAL);
781 PyDict_SetItemString(d, "ITIMER_VIRTUAL", x);
782 Py_DECREF(x);
783#endif
784#ifdef ITIMER_PROF
785 x = PyLong_FromLong(ITIMER_PROF);
786 PyDict_SetItemString(d, "ITIMER_PROF", x);
787 Py_DECREF(x);
788#endif
789
790#if defined (HAVE_SETITIMER) || defined (HAVE_GETITIMER)
791 ItimerError = PyErr_NewException("signal.ItimerError",
792 PyExc_IOError, NULL);
Neal Norwitz18aa3882008-08-24 05:04:52 +0000793 if (ItimerError != NULL)
794 PyDict_SetItemString(d, "ItimerError", ItimerError);
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000795#endif
796
Brian Curtine5aa8862010-04-02 23:26:06 +0000797#ifdef CTRL_C_EVENT
798 x = PyInt_FromLong(CTRL_C_EVENT);
799 PyDict_SetItemString(d, "CTRL_C_EVENT", x);
800 Py_DECREF(x);
801#endif
802
803#ifdef CTRL_BREAK_EVENT
804 x = PyInt_FromLong(CTRL_BREAK_EVENT);
805 PyDict_SetItemString(d, "CTRL_BREAK_EVENT", x);
806 Py_DECREF(x);
807#endif
808
Barry Warsaw92971171997-01-03 00:14:25 +0000809 if (!PyErr_Occurred())
810 return;
811
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000812 /* Check for errors */
Barry Warsaw92971171997-01-03 00:14:25 +0000813 finally:
Guido van Rossum08c16611997-08-02 03:01:42 +0000814 return;
815}
816
817static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000818finisignal(void)
Guido van Rossum08c16611997-08-02 03:01:42 +0000819{
820 int i;
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000821 PyObject *func;
Guido van Rossum08c16611997-08-02 03:01:42 +0000822
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000823 PyOS_setsig(SIGINT, old_siginthandler);
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000824 old_siginthandler = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +0000825
826 for (i = 1; i < NSIG; i++) {
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000827 func = Handlers[i].func;
Guido van Rossum08c16611997-08-02 03:01:42 +0000828 Handlers[i].tripped = 0;
Guido van Rossum08c16611997-08-02 03:01:42 +0000829 Handlers[i].func = NULL;
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000830 if (i != SIGINT && func != NULL && func != Py_None &&
831 func != DefaultHandler && func != IgnoreHandler)
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000832 PyOS_setsig(i, SIG_DFL);
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000833 Py_XDECREF(func);
Guido van Rossum08c16611997-08-02 03:01:42 +0000834 }
835
836 Py_XDECREF(IntHandler);
837 IntHandler = NULL;
838 Py_XDECREF(DefaultHandler);
839 DefaultHandler = NULL;
840 Py_XDECREF(IgnoreHandler);
841 IgnoreHandler = NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000842}
843
Barry Warsaw92971171997-01-03 00:14:25 +0000844
Barry Warsaw92971171997-01-03 00:14:25 +0000845/* Declared in pyerrors.h */
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000846int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000847PyErr_CheckSignals(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000848{
849 int i;
Guido van Rossume4485b01994-09-07 14:32:49 +0000850 PyObject *f;
Barry Warsaw92971171997-01-03 00:14:25 +0000851
852 if (!is_tripped)
Guido van Rossum6299d1e1994-05-31 12:51:13 +0000853 return 0;
Guido van Rossum137c49c2007-12-10 23:00:12 +0000854
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000855#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000856 if (PyThread_get_thread_ident() != main_thread)
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000857 return 0;
858#endif
Guido van Rossum137c49c2007-12-10 23:00:12 +0000859
860 /*
Gregory P. Smithf4666422009-08-12 17:02:37 +0000861 * The is_tripped variable is meant to speed up the calls to
Guido van Rossum137c49c2007-12-10 23:00:12 +0000862 * PyErr_CheckSignals (both directly or via pending calls) when no
863 * signal has arrived. This variable is set to 1 when a signal arrives
864 * and it is set to 0 here, when we know some signals arrived. This way
865 * we can run the registered handlers with no signals blocked.
866 *
867 * NOTE: with this approach we can have a situation where is_tripped is
868 * 1 but we have no more signals to handle (Handlers[i].tripped
869 * is 0 for every signal i). This won't do us any harm (except
870 * we're gonna spent some cycles for nothing). This happens when
871 * we receive a signal i after we zero is_tripped and before we
872 * check Handlers[i].tripped.
873 */
874 is_tripped = 0;
875
Guido van Rossum6297a7a2003-02-19 15:53:17 +0000876 if (!(f = (PyObject *)PyEval_GetFrame()))
Guido van Rossume4485b01994-09-07 14:32:49 +0000877 f = Py_None;
Guido van Rossum137c49c2007-12-10 23:00:12 +0000878
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000879 for (i = 1; i < NSIG; i++) {
Barry Warsaw92971171997-01-03 00:14:25 +0000880 if (Handlers[i].tripped) {
881 PyObject *result = NULL;
882 PyObject *arglist = Py_BuildValue("(iO)", i, f);
883 Handlers[i].tripped = 0;
884
885 if (arglist) {
886 result = PyEval_CallObject(Handlers[i].func,
887 arglist);
Guido van Rossume4485b01994-09-07 14:32:49 +0000888 Py_DECREF(arglist);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000889 }
Barry Warsaw92971171997-01-03 00:14:25 +0000890 if (!result)
Guido van Rossumfcdd0e41997-01-21 06:13:09 +0000891 return -1;
Barry Warsaw92971171997-01-03 00:14:25 +0000892
893 Py_DECREF(result);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000894 }
895 }
Guido van Rossum137c49c2007-12-10 23:00:12 +0000896
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000897 return 0;
898}
899
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000900
Barry Warsaw92971171997-01-03 00:14:25 +0000901/* Replacements for intrcheck.c functionality
902 * Declared in pyerrors.h
903 */
904void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000905PyErr_SetInterrupt(void)
Barry Warsaw92971171997-01-03 00:14:25 +0000906{
Guido van Rossum137c49c2007-12-10 23:00:12 +0000907 is_tripped = 1;
Barry Warsaw92971171997-01-03 00:14:25 +0000908 Handlers[SIGINT].tripped = 1;
Thomas Wouters334fb892000-07-25 12:56:38 +0000909 Py_AddPendingCall((int (*)(void *))PyErr_CheckSignals, NULL);
Barry Warsaw92971171997-01-03 00:14:25 +0000910}
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000911
912void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000913PyOS_InitInterrupts(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000914{
915 initsignal();
Guido van Rossum08c16611997-08-02 03:01:42 +0000916 _PyImport_FixupExtension("signal", "signal");
917}
918
919void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000920PyOS_FiniInterrupts(void)
Guido van Rossum08c16611997-08-02 03:01:42 +0000921{
922 finisignal();
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000923}
924
925int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000926PyOS_InterruptOccurred(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000927{
Barry Warsaw92971171997-01-03 00:14:25 +0000928 if (Handlers[SIGINT].tripped) {
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000929#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000930 if (PyThread_get_thread_ident() != main_thread)
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000931 return 0;
932#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000933 Handlers[SIGINT].tripped = 0;
Guido van Rossum6299d1e1994-05-31 12:51:13 +0000934 return 1;
935 }
936 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000937}
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000938
939void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000940PyOS_AfterFork(void)
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000941{
942#ifdef WITH_THREAD
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000943 PyEval_ReInitThreads();
Guido van Rossum65d5b571998-12-21 19:32:43 +0000944 main_thread = PyThread_get_thread_ident();
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000945 main_pid = getpid();
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +0000946 _PyImport_ReInitLock();
Benjamin Peterson114f7e52008-06-13 00:09:47 +0000947 PyThread_ReInitTLS();
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000948#endif
949}