blob: a729604a316bf4cb27376d880e54b12e32518c56 [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
Guido van Rossum644a12b1997-04-09 19:24:53 +000010#include <process.h>
11#endif
12
Guido van Rossum398d9fe1994-05-11 08:59:13 +000013#include <signal.h>
14
Guido van Rossumbb4ba121994-06-23 11:25:45 +000015#ifndef SIG_ERR
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +000016#define SIG_ERR ((PyOS_sighandler_t)(-1))
Guido van Rossumbb4ba121994-06-23 11:25:45 +000017#endif
18
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000019#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000020#define NSIG 12
21#include <process.h>
22#endif
23
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000024#ifndef NSIG
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000025# if defined(_NSIG)
26# define NSIG _NSIG /* For BSD/SysV */
27# elif defined(_SIGMAX)
28# define NSIG (_SIGMAX + 1) /* For QNX */
29# elif defined(SIGMAX)
30# define NSIG (SIGMAX + 1) /* For djgpp */
31# else
32# define NSIG 64 /* Use a reasonable default value */
33# endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000034#endif
35
36
Guido van Rossumbb4ba121994-06-23 11:25:45 +000037/*
38 NOTES ON THE INTERACTION BETWEEN SIGNALS AND THREADS
39
40 When threads are supported, we want the following semantics:
41
42 - only the main thread can set a signal handler
43 - any thread can get a signal handler
44 - signals are only delivered to the main thread
45
46 I.e. we don't support "synchronous signals" like SIGFPE (catching
47 this doesn't make much sense in Python anyway) nor do we support
48 signals as a means of inter-thread communication, since not all
49 thread implementations support that (at least our thread library
50 doesn't).
51
52 We still have the problem that in some implementations signals
53 generated by the keyboard (e.g. SIGINT) are delivered to all
54 threads (e.g. SGI), while in others (e.g. Solaris) such signals are
55 delivered to one random thread (an intermediate possibility would
Guido van Rossuma3c04b01995-01-12 11:29:01 +000056 be to deliver it to the main thread -- POSIX?). For now, we have
Guido van Rossumbb4ba121994-06-23 11:25:45 +000057 a working implementation that works in all three cases -- the
58 handler ignores signals if getpid() isn't the same as in the main
59 thread. XXX This is a hack.
60
Guido van Rossum9e8181b2000-09-19 00:46:46 +000061 GNU pth is a user-space threading library, and as such, all threads
62 run within the same process. In this case, if the currently running
63 thread is not the main_thread, send the signal to the main_thread.
Guido van Rossumbb4ba121994-06-23 11:25:45 +000064*/
65
66#ifdef WITH_THREAD
Guido van Rossum295b8e51997-06-06 21:16:41 +000067#include <sys/types.h> /* For pid_t */
Guido van Rossum49b56061998-10-01 20:42:43 +000068#include "pythread.h"
Guido van Rossumbb4ba121994-06-23 11:25:45 +000069static long main_thread;
70static pid_t main_pid;
71#endif
72
Barry Warsaw92971171997-01-03 00:14:25 +000073static struct {
74 int tripped;
75 PyObject *func;
76} Handlers[NSIG];
Guido van Rossum398d9fe1994-05-11 08:59:13 +000077
Barry Warsaw92971171997-01-03 00:14:25 +000078static int is_tripped = 0; /* Speed up sigcheck() when none tripped */
Guido van Rossum398d9fe1994-05-11 08:59:13 +000079
Barry Warsaw92971171997-01-03 00:14:25 +000080static PyObject *DefaultHandler;
81static PyObject *IgnoreHandler;
82static PyObject *IntHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +000083
Martin v. Löwisf58de1b2001-03-06 12:13:56 +000084/* On Solaris 8, gcc will produce a warning that the function
85 declaration is not a prototype. This is caused by the definition of
86 SIG_DFL as (void (*)())0; the correct declaration would have been
87 (void (*)(int))0. */
88
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +000089static PyOS_sighandler_t old_siginthandler = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +000090
Barry Warsaw92971171997-01-03 00:14:25 +000091
Guido van Rossume4485b01994-09-07 14:32:49 +000092static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +000093signal_default_int_handler(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +000094{
Guido van Rossume4485b01994-09-07 14:32:49 +000095 PyErr_SetNone(PyExc_KeyboardInterrupt);
Barry Warsaw92971171997-01-03 00:14:25 +000096 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +000097}
98
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000099PyDoc_STRVAR(default_int_handler_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000100"default_int_handler(...)\n\
101\n\
Michael W. Hudson24ec2112004-06-17 15:55:53 +0000102The default handler for SIGINT installed by Python.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000103It raises KeyboardInterrupt.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000104
Thomas Wouters0796b002000-07-22 23:49:30 +0000105
106static int
107checksignals_witharg(void * unused)
108{
109 return PyErr_CheckSignals();
110}
111
Tim Peters4f1b2082000-07-23 21:18:09 +0000112static void
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000113signal_handler(int sig_num)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000114{
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000115#ifdef WITH_THREAD
Guido van Rossum9e8181b2000-09-19 00:46:46 +0000116#ifdef WITH_PTH
117 if (PyThread_get_thread_ident() != main_thread) {
118 pth_raise(*(pth_t *) main_thread, sig_num);
119 return;
120 }
121#endif
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000122 /* See NOTES section above */
123 if (getpid() == main_pid) {
124#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000125 is_tripped++;
126 Handlers[sig_num].tripped = 1;
Thomas Wouters0796b002000-07-22 23:49:30 +0000127 Py_AddPendingCall(checksignals_witharg, NULL);
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000128#ifdef WITH_THREAD
129 }
130#endif
Guido van Rossum602099a1994-09-14 13:32:22 +0000131#ifdef SIGCHLD
132 if (sig_num == SIGCHLD) {
133 /* To avoid infinite recursion, this signal remains
134 reset until explicit re-instated.
135 Don't clear the 'func' field as it is our pointer
136 to the Python handler... */
Tim Peters4f1b2082000-07-23 21:18:09 +0000137 return;
Guido van Rossum602099a1994-09-14 13:32:22 +0000138 }
139#endif
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000140 PyOS_setsig(sig_num, signal_handler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000141}
Guido van Rossume4485b01994-09-07 14:32:49 +0000142
Guido van Rossum06d511d1995-03-10 15:13:48 +0000143
Guido van Rossum1171ee61997-08-22 20:42:00 +0000144#ifdef HAVE_ALARM
Guido van Rossume4485b01994-09-07 14:32:49 +0000145static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000146signal_alarm(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000147{
148 int t;
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000149 if (!PyArg_ParseTuple(args, "i:alarm", &t))
Barry Warsaw92971171997-01-03 00:14:25 +0000150 return NULL;
Guido van Rossume4485b01994-09-07 14:32:49 +0000151 /* alarm() returns the number of seconds remaining */
Fred Drakedff3a372001-07-19 21:29:49 +0000152 return PyInt_FromLong((long)alarm(t));
Guido van Rossumaa0f4c71994-08-23 13:49:37 +0000153}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000154
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000155PyDoc_STRVAR(alarm_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000156"alarm(seconds)\n\
157\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000158Arrange for SIGALRM to arrive after the given number of seconds.");
Guido van Rossum06d511d1995-03-10 15:13:48 +0000159#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000160
Guido van Rossum1171ee61997-08-22 20:42:00 +0000161#ifdef HAVE_PAUSE
Guido van Rossuma597dde1995-01-10 20:56:29 +0000162static PyObject *
Neal Norwitz3a6f9782002-03-25 20:46:46 +0000163signal_pause(PyObject *self)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000164{
Guido van Rossuma597dde1995-01-10 20:56:29 +0000165 Py_BEGIN_ALLOW_THREADS
Barry Warsaw92971171997-01-03 00:14:25 +0000166 (void)pause();
Guido van Rossuma597dde1995-01-10 20:56:29 +0000167 Py_END_ALLOW_THREADS
Barry Warsaw92971171997-01-03 00:14:25 +0000168 /* make sure that any exceptions that got raised are propagated
169 * back into Python
170 */
171 if (PyErr_CheckSignals())
172 return NULL;
173
Guido van Rossume4485b01994-09-07 14:32:49 +0000174 Py_INCREF(Py_None);
175 return Py_None;
176}
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000177PyDoc_STRVAR(pause_doc,
Barry Warsaw1ee36ff1998-07-21 22:41:18 +0000178"pause()\n\
179\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000180Wait until a signal arrives.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000181
Guido van Rossum06d511d1995-03-10 15:13:48 +0000182#endif
Guido van Rossume4485b01994-09-07 14:32:49 +0000183
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000184
Guido van Rossume4485b01994-09-07 14:32:49 +0000185static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000186signal_signal(PyObject *self, PyObject *args)
Guido van Rossume4485b01994-09-07 14:32:49 +0000187{
188 PyObject *obj;
189 int sig_num;
190 PyObject *old_handler;
Tim Peters4f1b2082000-07-23 21:18:09 +0000191 void (*func)(int);
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000192 if (!PyArg_ParseTuple(args, "iO:signal", &sig_num, &obj))
Barry Warsaw92971171997-01-03 00:14:25 +0000193 return NULL;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000194#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000195 if (PyThread_get_thread_ident() != main_thread) {
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000196 PyErr_SetString(PyExc_ValueError,
197 "signal only works in main thread");
Barry Warsaw92971171997-01-03 00:14:25 +0000198 return NULL;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000199 }
200#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000201 if (sig_num < 1 || sig_num >= NSIG) {
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000202 PyErr_SetString(PyExc_ValueError,
203 "signal number out of range");
Barry Warsaw92971171997-01-03 00:14:25 +0000204 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000205 }
Barry Warsaw92971171997-01-03 00:14:25 +0000206 if (obj == IgnoreHandler)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000207 func = SIG_IGN;
Barry Warsaw92971171997-01-03 00:14:25 +0000208 else if (obj == DefaultHandler)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000209 func = SIG_DFL;
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000210 else if (!PyCallable_Check(obj)) {
Guido van Rossume4485b01994-09-07 14:32:49 +0000211 PyErr_SetString(PyExc_TypeError,
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000212"signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object");
Barry Warsaw92971171997-01-03 00:14:25 +0000213 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000214 }
215 else
Barry Warsaw92971171997-01-03 00:14:25 +0000216 func = signal_handler;
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000217 if (PyOS_setsig(sig_num, func) == SIG_ERR) {
Guido van Rossume4485b01994-09-07 14:32:49 +0000218 PyErr_SetFromErrno(PyExc_RuntimeError);
Barry Warsaw92971171997-01-03 00:14:25 +0000219 return NULL;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000220 }
Barry Warsaw92971171997-01-03 00:14:25 +0000221 old_handler = Handlers[sig_num].func;
222 Handlers[sig_num].tripped = 0;
Guido van Rossume4485b01994-09-07 14:32:49 +0000223 Py_INCREF(obj);
Barry Warsaw92971171997-01-03 00:14:25 +0000224 Handlers[sig_num].func = obj;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000225 return old_handler;
226}
227
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000228PyDoc_STRVAR(signal_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000229"signal(sig, action) -> action\n\
230\n\
231Set the action for the given signal. The action can be SIG_DFL,\n\
232SIG_IGN, or a callable Python object. The previous action is\n\
233returned. See getsignal() for possible return values.\n\
234\n\
235*** IMPORTANT NOTICE ***\n\
236A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000237the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000238
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000239
Guido van Rossume4485b01994-09-07 14:32:49 +0000240static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000241signal_getsignal(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000242{
243 int sig_num;
Guido van Rossume4485b01994-09-07 14:32:49 +0000244 PyObject *old_handler;
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000245 if (!PyArg_ParseTuple(args, "i:getsignal", &sig_num))
Barry Warsaw92971171997-01-03 00:14:25 +0000246 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000247 if (sig_num < 1 || sig_num >= NSIG) {
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000248 PyErr_SetString(PyExc_ValueError,
249 "signal number out of range");
Barry Warsaw92971171997-01-03 00:14:25 +0000250 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000251 }
Barry Warsaw92971171997-01-03 00:14:25 +0000252 old_handler = Handlers[sig_num].func;
Guido van Rossume4485b01994-09-07 14:32:49 +0000253 Py_INCREF(old_handler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000254 return old_handler;
255}
256
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000257PyDoc_STRVAR(getsignal_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000258"getsignal(sig) -> action\n\
259\n\
260Return the current action for the given signal. The return value can be:\n\
261SIG_IGN -- if the signal is being ignored\n\
262SIG_DFL -- if the default action for the signal is in effect\n\
263None -- if an unknown handler is in effect\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000264anything else -- the callable Python object used as a handler");
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000265
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000266
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000267/* List of functions defined in the module */
Barry Warsaw92971171997-01-03 00:14:25 +0000268static PyMethodDef signal_methods[] = {
Guido van Rossum1171ee61997-08-22 20:42:00 +0000269#ifdef HAVE_ALARM
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000270 {"alarm", signal_alarm, METH_VARARGS, alarm_doc},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000271#endif
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000272 {"signal", signal_signal, METH_VARARGS, signal_doc},
273 {"getsignal", signal_getsignal, METH_VARARGS, getsignal_doc},
Guido van Rossum1171ee61997-08-22 20:42:00 +0000274#ifdef HAVE_PAUSE
Neil Schemenauer78662cf2002-03-28 21:04:14 +0000275 {"pause", (PyCFunction)signal_pause,
276 METH_NOARGS,pause_doc},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000277#endif
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000278 {"default_int_handler", signal_default_int_handler,
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000279 METH_VARARGS, default_int_handler_doc},
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000280 {NULL, NULL} /* sentinel */
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000281};
282
Barry Warsaw92971171997-01-03 00:14:25 +0000283
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000284PyDoc_STRVAR(module_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000285"This module provides mechanisms to use signal handlers in Python.\n\
286\n\
287Functions:\n\
288\n\
289alarm() -- cause SIGALRM after a specified time [Unix only]\n\
290signal() -- set the action for a given signal\n\
291getsignal() -- get the signal action for a given signal\n\
292pause() -- wait until a signal arrives [Unix only]\n\
293default_int_handler() -- default SIGINT handler\n\
294\n\
295Constants:\n\
296\n\
297SIG_DFL -- used to refer to the system default handler\n\
298SIG_IGN -- used to ignore the signal\n\
299NSIG -- number of defined signals\n\
300\n\
301SIGINT, SIGTERM, etc. -- signal numbers\n\
302\n\
303*** IMPORTANT NOTICE ***\n\
304A signal handler function is called with two arguments:\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000305the first is the signal number, the second is the interrupted stack frame.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000306
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000307PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000308initsignal(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000309{
Guido van Rossume4485b01994-09-07 14:32:49 +0000310 PyObject *m, *d, *x;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000311 int i;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000312
313#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000314 main_thread = PyThread_get_thread_ident();
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000315 main_pid = getpid();
316#endif
317
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000318 /* Create the module and add the functions */
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000319 m = Py_InitModule3("signal", signal_methods, module_doc);
Neal Norwitz1ac754f2006-01-19 06:09:39 +0000320 if (m == NULL)
321 return;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000322
323 /* Add some symbolic constants to the module */
Guido van Rossume4485b01994-09-07 14:32:49 +0000324 d = PyModule_GetDict(m);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000325
Guido van Rossum276fa432000-06-30 23:04:18 +0000326 x = DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL);
Barry Warsaw92971171997-01-03 00:14:25 +0000327 if (!x || PyDict_SetItemString(d, "SIG_DFL", x) < 0)
328 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000329
Guido van Rossum276fa432000-06-30 23:04:18 +0000330 x = IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN);
Barry Warsaw92971171997-01-03 00:14:25 +0000331 if (!x || PyDict_SetItemString(d, "SIG_IGN", x) < 0)
332 goto finally;
333
334 x = PyInt_FromLong((long)NSIG);
335 if (!x || PyDict_SetItemString(d, "NSIG", x) < 0)
336 goto finally;
Guido van Rossum08c16611997-08-02 03:01:42 +0000337 Py_DECREF(x);
Barry Warsaw92971171997-01-03 00:14:25 +0000338
339 x = IntHandler = PyDict_GetItemString(d, "default_int_handler");
340 if (!x)
341 goto finally;
Guido van Rossum08c16611997-08-02 03:01:42 +0000342 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +0000343
344 Handlers[0].tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000345 for (i = 1; i < NSIG; i++) {
Tim Peters4f1b2082000-07-23 21:18:09 +0000346 void (*t)(int);
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000347 t = PyOS_getsig(i);
Barry Warsaw92971171997-01-03 00:14:25 +0000348 Handlers[i].tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000349 if (t == SIG_DFL)
Barry Warsaw92971171997-01-03 00:14:25 +0000350 Handlers[i].func = DefaultHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000351 else if (t == SIG_IGN)
Barry Warsaw92971171997-01-03 00:14:25 +0000352 Handlers[i].func = IgnoreHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000353 else
Barry Warsaw92971171997-01-03 00:14:25 +0000354 Handlers[i].func = Py_None; /* None of our business */
355 Py_INCREF(Handlers[i].func);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000356 }
Barry Warsaw92971171997-01-03 00:14:25 +0000357 if (Handlers[SIGINT].func == DefaultHandler) {
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000358 /* Install default int handler */
Guido van Rossum08c16611997-08-02 03:01:42 +0000359 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +0000360 Py_DECREF(Handlers[SIGINT].func);
361 Handlers[SIGINT].func = IntHandler;
Neal Norwitz41785152002-06-13 21:42:51 +0000362 old_siginthandler = PyOS_setsig(SIGINT, signal_handler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000363 }
364
365#ifdef SIGHUP
Guido van Rossume4485b01994-09-07 14:32:49 +0000366 x = PyInt_FromLong(SIGHUP);
367 PyDict_SetItemString(d, "SIGHUP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000368 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000369#endif
370#ifdef SIGINT
Guido van Rossume4485b01994-09-07 14:32:49 +0000371 x = PyInt_FromLong(SIGINT);
372 PyDict_SetItemString(d, "SIGINT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000373 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000374#endif
Tim Peters1ce3cf72001-10-01 17:58:40 +0000375#ifdef SIGBREAK
376 x = PyInt_FromLong(SIGBREAK);
377 PyDict_SetItemString(d, "SIGBREAK", x);
378 Py_XDECREF(x);
379#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000380#ifdef SIGQUIT
Guido van Rossume4485b01994-09-07 14:32:49 +0000381 x = PyInt_FromLong(SIGQUIT);
382 PyDict_SetItemString(d, "SIGQUIT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000383 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000384#endif
385#ifdef SIGILL
Guido van Rossume4485b01994-09-07 14:32:49 +0000386 x = PyInt_FromLong(SIGILL);
387 PyDict_SetItemString(d, "SIGILL", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000388 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000389#endif
390#ifdef SIGTRAP
Guido van Rossume4485b01994-09-07 14:32:49 +0000391 x = PyInt_FromLong(SIGTRAP);
392 PyDict_SetItemString(d, "SIGTRAP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000393 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000394#endif
395#ifdef SIGIOT
Guido van Rossume4485b01994-09-07 14:32:49 +0000396 x = PyInt_FromLong(SIGIOT);
397 PyDict_SetItemString(d, "SIGIOT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000398 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000399#endif
400#ifdef SIGABRT
Guido van Rossume4485b01994-09-07 14:32:49 +0000401 x = PyInt_FromLong(SIGABRT);
402 PyDict_SetItemString(d, "SIGABRT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000403 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000404#endif
405#ifdef SIGEMT
Guido van Rossume4485b01994-09-07 14:32:49 +0000406 x = PyInt_FromLong(SIGEMT);
407 PyDict_SetItemString(d, "SIGEMT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000408 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000409#endif
410#ifdef SIGFPE
Guido van Rossume4485b01994-09-07 14:32:49 +0000411 x = PyInt_FromLong(SIGFPE);
412 PyDict_SetItemString(d, "SIGFPE", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000413 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000414#endif
415#ifdef SIGKILL
Guido van Rossume4485b01994-09-07 14:32:49 +0000416 x = PyInt_FromLong(SIGKILL);
417 PyDict_SetItemString(d, "SIGKILL", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000418 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000419#endif
420#ifdef SIGBUS
Guido van Rossume4485b01994-09-07 14:32:49 +0000421 x = PyInt_FromLong(SIGBUS);
422 PyDict_SetItemString(d, "SIGBUS", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000423 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000424#endif
425#ifdef SIGSEGV
Guido van Rossume4485b01994-09-07 14:32:49 +0000426 x = PyInt_FromLong(SIGSEGV);
427 PyDict_SetItemString(d, "SIGSEGV", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000428 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000429#endif
430#ifdef SIGSYS
Guido van Rossume4485b01994-09-07 14:32:49 +0000431 x = PyInt_FromLong(SIGSYS);
432 PyDict_SetItemString(d, "SIGSYS", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000433 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000434#endif
435#ifdef SIGPIPE
Guido van Rossume4485b01994-09-07 14:32:49 +0000436 x = PyInt_FromLong(SIGPIPE);
437 PyDict_SetItemString(d, "SIGPIPE", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000438 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000439#endif
440#ifdef SIGALRM
Guido van Rossume4485b01994-09-07 14:32:49 +0000441 x = PyInt_FromLong(SIGALRM);
442 PyDict_SetItemString(d, "SIGALRM", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000443 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000444#endif
445#ifdef SIGTERM
Guido van Rossume4485b01994-09-07 14:32:49 +0000446 x = PyInt_FromLong(SIGTERM);
447 PyDict_SetItemString(d, "SIGTERM", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000448 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000449#endif
450#ifdef SIGUSR1
Guido van Rossume4485b01994-09-07 14:32:49 +0000451 x = PyInt_FromLong(SIGUSR1);
452 PyDict_SetItemString(d, "SIGUSR1", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000453 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000454#endif
455#ifdef SIGUSR2
Guido van Rossume4485b01994-09-07 14:32:49 +0000456 x = PyInt_FromLong(SIGUSR2);
457 PyDict_SetItemString(d, "SIGUSR2", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000458 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000459#endif
460#ifdef SIGCLD
Guido van Rossume4485b01994-09-07 14:32:49 +0000461 x = PyInt_FromLong(SIGCLD);
462 PyDict_SetItemString(d, "SIGCLD", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000463 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000464#endif
465#ifdef SIGCHLD
Guido van Rossume4485b01994-09-07 14:32:49 +0000466 x = PyInt_FromLong(SIGCHLD);
467 PyDict_SetItemString(d, "SIGCHLD", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000468 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000469#endif
470#ifdef SIGPWR
Guido van Rossume4485b01994-09-07 14:32:49 +0000471 x = PyInt_FromLong(SIGPWR);
472 PyDict_SetItemString(d, "SIGPWR", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000473 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000474#endif
475#ifdef SIGIO
Guido van Rossume4485b01994-09-07 14:32:49 +0000476 x = PyInt_FromLong(SIGIO);
477 PyDict_SetItemString(d, "SIGIO", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000478 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000479#endif
480#ifdef SIGURG
Guido van Rossume4485b01994-09-07 14:32:49 +0000481 x = PyInt_FromLong(SIGURG);
482 PyDict_SetItemString(d, "SIGURG", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000483 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000484#endif
485#ifdef SIGWINCH
Guido van Rossume4485b01994-09-07 14:32:49 +0000486 x = PyInt_FromLong(SIGWINCH);
487 PyDict_SetItemString(d, "SIGWINCH", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000488 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000489#endif
490#ifdef SIGPOLL
Guido van Rossume4485b01994-09-07 14:32:49 +0000491 x = PyInt_FromLong(SIGPOLL);
492 PyDict_SetItemString(d, "SIGPOLL", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000493 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000494#endif
495#ifdef SIGSTOP
Guido van Rossume4485b01994-09-07 14:32:49 +0000496 x = PyInt_FromLong(SIGSTOP);
497 PyDict_SetItemString(d, "SIGSTOP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000498 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000499#endif
500#ifdef SIGTSTP
Guido van Rossume4485b01994-09-07 14:32:49 +0000501 x = PyInt_FromLong(SIGTSTP);
502 PyDict_SetItemString(d, "SIGTSTP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000503 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000504#endif
505#ifdef SIGCONT
Guido van Rossume4485b01994-09-07 14:32:49 +0000506 x = PyInt_FromLong(SIGCONT);
507 PyDict_SetItemString(d, "SIGCONT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000508 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000509#endif
510#ifdef SIGTTIN
Guido van Rossume4485b01994-09-07 14:32:49 +0000511 x = PyInt_FromLong(SIGTTIN);
512 PyDict_SetItemString(d, "SIGTTIN", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000513 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000514#endif
515#ifdef SIGTTOU
Guido van Rossume4485b01994-09-07 14:32:49 +0000516 x = PyInt_FromLong(SIGTTOU);
517 PyDict_SetItemString(d, "SIGTTOU", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000518 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000519#endif
520#ifdef SIGVTALRM
Guido van Rossume4485b01994-09-07 14:32:49 +0000521 x = PyInt_FromLong(SIGVTALRM);
522 PyDict_SetItemString(d, "SIGVTALRM", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000523 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000524#endif
525#ifdef SIGPROF
Guido van Rossume4485b01994-09-07 14:32:49 +0000526 x = PyInt_FromLong(SIGPROF);
527 PyDict_SetItemString(d, "SIGPROF", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000528 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000529#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000530#ifdef SIGXCPU
531 x = PyInt_FromLong(SIGXCPU);
532 PyDict_SetItemString(d, "SIGXCPU", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000533 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000534#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000535#ifdef SIGXFSZ
536 x = PyInt_FromLong(SIGXFSZ);
537 PyDict_SetItemString(d, "SIGXFSZ", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000538 Py_XDECREF(x);
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000539#endif
Anthony Baxterf37f37d2003-07-31 10:35:29 +0000540#ifdef SIGRTMIN
541 x = PyInt_FromLong(SIGRTMIN);
542 PyDict_SetItemString(d, "SIGRTMIN", x);
543 Py_XDECREF(x);
544#endif
545#ifdef SIGRTMAX
546 x = PyInt_FromLong(SIGRTMAX);
547 PyDict_SetItemString(d, "SIGRTMAX", x);
548 Py_XDECREF(x);
549#endif
Martin v. Löwis175af252002-01-12 11:43:25 +0000550#ifdef SIGINFO
551 x = PyInt_FromLong(SIGINFO);
552 PyDict_SetItemString(d, "SIGINFO", x);
553 Py_XDECREF(x);
554#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000555 if (!PyErr_Occurred())
556 return;
557
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000558 /* Check for errors */
Barry Warsaw92971171997-01-03 00:14:25 +0000559 finally:
Guido van Rossum08c16611997-08-02 03:01:42 +0000560 return;
561}
562
563static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000564finisignal(void)
Guido van Rossum08c16611997-08-02 03:01:42 +0000565{
566 int i;
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000567 PyObject *func;
Guido van Rossum08c16611997-08-02 03:01:42 +0000568
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000569 PyOS_setsig(SIGINT, old_siginthandler);
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000570 old_siginthandler = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +0000571
572 for (i = 1; i < NSIG; i++) {
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000573 func = Handlers[i].func;
Guido van Rossum08c16611997-08-02 03:01:42 +0000574 Handlers[i].tripped = 0;
Guido van Rossum08c16611997-08-02 03:01:42 +0000575 Handlers[i].func = NULL;
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000576 if (i != SIGINT && func != NULL && func != Py_None &&
577 func != DefaultHandler && func != IgnoreHandler)
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000578 PyOS_setsig(i, SIG_DFL);
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000579 Py_XDECREF(func);
Guido van Rossum08c16611997-08-02 03:01:42 +0000580 }
581
582 Py_XDECREF(IntHandler);
583 IntHandler = NULL;
584 Py_XDECREF(DefaultHandler);
585 DefaultHandler = NULL;
586 Py_XDECREF(IgnoreHandler);
587 IgnoreHandler = NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000588}
589
Barry Warsaw92971171997-01-03 00:14:25 +0000590
Barry Warsaw92971171997-01-03 00:14:25 +0000591/* Declared in pyerrors.h */
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000592int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000593PyErr_CheckSignals(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000594{
595 int i;
Guido van Rossume4485b01994-09-07 14:32:49 +0000596 PyObject *f;
Barry Warsaw92971171997-01-03 00:14:25 +0000597
598 if (!is_tripped)
Guido van Rossum6299d1e1994-05-31 12:51:13 +0000599 return 0;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000600#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000601 if (PyThread_get_thread_ident() != main_thread)
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000602 return 0;
603#endif
Guido van Rossum6297a7a2003-02-19 15:53:17 +0000604 if (!(f = (PyObject *)PyEval_GetFrame()))
Guido van Rossume4485b01994-09-07 14:32:49 +0000605 f = Py_None;
Barry Warsaw92971171997-01-03 00:14:25 +0000606
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000607 for (i = 1; i < NSIG; i++) {
Barry Warsaw92971171997-01-03 00:14:25 +0000608 if (Handlers[i].tripped) {
609 PyObject *result = NULL;
610 PyObject *arglist = Py_BuildValue("(iO)", i, f);
611 Handlers[i].tripped = 0;
612
613 if (arglist) {
614 result = PyEval_CallObject(Handlers[i].func,
615 arglist);
Guido van Rossume4485b01994-09-07 14:32:49 +0000616 Py_DECREF(arglist);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000617 }
Barry Warsaw92971171997-01-03 00:14:25 +0000618 if (!result)
Guido van Rossumfcdd0e41997-01-21 06:13:09 +0000619 return -1;
Barry Warsaw92971171997-01-03 00:14:25 +0000620
621 Py_DECREF(result);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000622 }
623 }
Barry Warsaw92971171997-01-03 00:14:25 +0000624 is_tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000625 return 0;
626}
627
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000628
Barry Warsaw92971171997-01-03 00:14:25 +0000629/* Replacements for intrcheck.c functionality
630 * Declared in pyerrors.h
631 */
632void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000633PyErr_SetInterrupt(void)
Barry Warsaw92971171997-01-03 00:14:25 +0000634{
635 is_tripped++;
636 Handlers[SIGINT].tripped = 1;
Thomas Wouters334fb892000-07-25 12:56:38 +0000637 Py_AddPendingCall((int (*)(void *))PyErr_CheckSignals, NULL);
Barry Warsaw92971171997-01-03 00:14:25 +0000638}
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000639
640void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000641PyOS_InitInterrupts(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000642{
643 initsignal();
Guido van Rossum08c16611997-08-02 03:01:42 +0000644 _PyImport_FixupExtension("signal", "signal");
645}
646
647void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000648PyOS_FiniInterrupts(void)
Guido van Rossum08c16611997-08-02 03:01:42 +0000649{
650 finisignal();
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000651}
652
653int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000654PyOS_InterruptOccurred(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000655{
Barry Warsaw92971171997-01-03 00:14:25 +0000656 if (Handlers[SIGINT].tripped) {
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000657#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000658 if (PyThread_get_thread_ident() != main_thread)
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000659 return 0;
660#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000661 Handlers[SIGINT].tripped = 0;
Guido van Rossum6299d1e1994-05-31 12:51:13 +0000662 return 1;
663 }
664 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000665}
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000666
667void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000668PyOS_AfterFork(void)
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000669{
670#ifdef WITH_THREAD
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000671 PyEval_ReInitThreads();
Guido van Rossum65d5b571998-12-21 19:32:43 +0000672 main_thread = PyThread_get_thread_ident();
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000673 main_pid = getpid();
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +0000674 _PyImport_ReInitLock();
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000675#endif
676}