blob: 1347c6d291e9ec54f6dfc8d35cdb7d931a795a5d [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
Guido van Rossum644a12b1997-04-09 19:24:53 +00009#ifdef MS_WIN32
10#include <process.h>
11#endif
12
Guido van Rossuma376cc51996-12-05 23:43:35 +000013#ifdef HAVE_UNISTD_H
14#include <unistd.h>
15#endif
16
Guido van Rossum398d9fe1994-05-11 08:59:13 +000017#include <signal.h>
18
Guido van Rossumbb4ba121994-06-23 11:25:45 +000019#ifndef SIG_ERR
Tim Peters4f1b2082000-07-23 21:18:09 +000020#define SIG_ERR ((void (*)(int))-1)
Guido van Rossumbb4ba121994-06-23 11:25:45 +000021#endif
22
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000023#if defined(PYOS_OS2)
24#define NSIG 12
25#include <process.h>
26#endif
27
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000028#ifndef NSIG
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000029# if defined(_NSIG)
30# define NSIG _NSIG /* For BSD/SysV */
31# elif defined(_SIGMAX)
32# define NSIG (_SIGMAX + 1) /* For QNX */
33# elif defined(SIGMAX)
34# define NSIG (SIGMAX + 1) /* For djgpp */
35# else
36# define NSIG 64 /* Use a reasonable default value */
37# endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000038#endif
39
40
Barry Warsaw92971171997-01-03 00:14:25 +000041
Guido van Rossumbb4ba121994-06-23 11:25:45 +000042/*
43 NOTES ON THE INTERACTION BETWEEN SIGNALS AND THREADS
44
45 When threads are supported, we want the following semantics:
46
47 - only the main thread can set a signal handler
48 - any thread can get a signal handler
49 - signals are only delivered to the main thread
50
51 I.e. we don't support "synchronous signals" like SIGFPE (catching
52 this doesn't make much sense in Python anyway) nor do we support
53 signals as a means of inter-thread communication, since not all
54 thread implementations support that (at least our thread library
55 doesn't).
56
57 We still have the problem that in some implementations signals
58 generated by the keyboard (e.g. SIGINT) are delivered to all
59 threads (e.g. SGI), while in others (e.g. Solaris) such signals are
60 delivered to one random thread (an intermediate possibility would
Guido van Rossuma3c04b01995-01-12 11:29:01 +000061 be to deliver it to the main thread -- POSIX?). For now, we have
Guido van Rossumbb4ba121994-06-23 11:25:45 +000062 a working implementation that works in all three cases -- the
63 handler ignores signals if getpid() isn't the same as in the main
64 thread. XXX This is a hack.
65
66*/
67
68#ifdef WITH_THREAD
Guido van Rossum295b8e51997-06-06 21:16:41 +000069#include <sys/types.h> /* For pid_t */
Guido van Rossum49b56061998-10-01 20:42:43 +000070#include "pythread.h"
Guido van Rossumbb4ba121994-06-23 11:25:45 +000071static long main_thread;
72static pid_t main_pid;
73#endif
74
Barry Warsaw92971171997-01-03 00:14:25 +000075static struct {
76 int tripped;
77 PyObject *func;
78} Handlers[NSIG];
Guido van Rossum398d9fe1994-05-11 08:59:13 +000079
Barry Warsaw92971171997-01-03 00:14:25 +000080static int is_tripped = 0; /* Speed up sigcheck() when none tripped */
Guido van Rossum398d9fe1994-05-11 08:59:13 +000081
Barry Warsaw92971171997-01-03 00:14:25 +000082static PyObject *DefaultHandler;
83static PyObject *IgnoreHandler;
84static PyObject *IntHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +000085
Tim Peters4f1b2082000-07-23 21:18:09 +000086static void (*old_siginthandler)(int) = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +000087
Barry Warsaw92971171997-01-03 00:14:25 +000088
89
Guido van Rossume4485b01994-09-07 14:32:49 +000090static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +000091signal_default_int_handler(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +000092{
Guido van Rossume4485b01994-09-07 14:32:49 +000093 PyErr_SetNone(PyExc_KeyboardInterrupt);
Barry Warsaw92971171997-01-03 00:14:25 +000094 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +000095}
96
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +000097static char default_int_handler_doc[] =
98"default_int_handler(...)\n\
99\n\
100The default handler for SIGINT instated by Python.\n\
101It raises KeyboardInterrupt.";
102
Barry Warsaw92971171997-01-03 00:14:25 +0000103
Thomas Wouters0796b002000-07-22 23:49:30 +0000104
105static int
106checksignals_witharg(void * unused)
107{
108 return PyErr_CheckSignals();
109}
110
Tim Peters4f1b2082000-07-23 21:18:09 +0000111static void
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000112signal_handler(int sig_num)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000113{
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000114#ifdef WITH_THREAD
115 /* See NOTES section above */
116 if (getpid() == main_pid) {
117#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000118 is_tripped++;
119 Handlers[sig_num].tripped = 1;
Thomas Wouters0796b002000-07-22 23:49:30 +0000120 Py_AddPendingCall(checksignals_witharg, NULL);
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000121#ifdef WITH_THREAD
122 }
123#endif
Guido van Rossum602099a1994-09-14 13:32:22 +0000124#ifdef SIGCHLD
125 if (sig_num == SIGCHLD) {
126 /* To avoid infinite recursion, this signal remains
127 reset until explicit re-instated.
128 Don't clear the 'func' field as it is our pointer
129 to the Python handler... */
Tim Peters4f1b2082000-07-23 21:18:09 +0000130 return;
Guido van Rossum602099a1994-09-14 13:32:22 +0000131 }
132#endif
Guido van Rossum1b236761998-09-21 14:46:00 +0000133#ifdef HAVE_SIGINTERRUPT
134 siginterrupt(sig_num, 1);
135#endif
Thomas Wouters0796b002000-07-22 23:49:30 +0000136 signal(sig_num, signal_handler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000137}
Guido van Rossume4485b01994-09-07 14:32:49 +0000138
Guido van Rossum06d511d1995-03-10 15:13:48 +0000139
Barry Warsaw92971171997-01-03 00:14:25 +0000140
Guido van Rossum1171ee61997-08-22 20:42:00 +0000141#ifdef HAVE_ALARM
Guido van Rossume4485b01994-09-07 14:32:49 +0000142static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000143signal_alarm(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000144{
145 int t;
Guido van Rossume4485b01994-09-07 14:32:49 +0000146 if (!PyArg_Parse(args, "i", &t))
Barry Warsaw92971171997-01-03 00:14:25 +0000147 return NULL;
Guido van Rossume4485b01994-09-07 14:32:49 +0000148 /* alarm() returns the number of seconds remaining */
149 return PyInt_FromLong(alarm(t));
Guido van Rossumaa0f4c71994-08-23 13:49:37 +0000150}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000151
152static char alarm_doc[] =
153"alarm(seconds)\n\
154\n\
Guido van Rossum911ec181998-06-28 17:00:19 +0000155Arrange for SIGALRM to arrive after the given number of seconds.";
Guido van Rossum06d511d1995-03-10 15:13:48 +0000156#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000157
Guido van Rossum1171ee61997-08-22 20:42:00 +0000158#ifdef HAVE_PAUSE
Guido van Rossuma597dde1995-01-10 20:56:29 +0000159static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000160signal_pause(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000161{
Guido van Rossume4485b01994-09-07 14:32:49 +0000162 if (!PyArg_NoArgs(args))
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000163 return NULL;
Barry Warsaw92971171997-01-03 00:14:25 +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}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000177static char pause_doc[] =
Barry Warsaw1ee36ff1998-07-21 22:41:18 +0000178"pause()\n\
179\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000180Wait until a signal arrives.";
181
Guido van Rossum06d511d1995-03-10 15:13:48 +0000182#endif
Guido van Rossume4485b01994-09-07 14:32:49 +0000183
Barry Warsaw92971171997-01-03 00:14:25 +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);
Guido van Rossume4485b01994-09-07 14:32:49 +0000192 if (!PyArg_Parse(args, "(iO)", &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 Rossum1b236761998-09-21 14:46:00 +0000217#ifdef HAVE_SIGINTERRUPT
218 siginterrupt(sig_num, 1);
219#endif
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000220 if (signal(sig_num, func) == SIG_ERR) {
Guido van Rossume4485b01994-09-07 14:32:49 +0000221 PyErr_SetFromErrno(PyExc_RuntimeError);
Barry Warsaw92971171997-01-03 00:14:25 +0000222 return NULL;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000223 }
Barry Warsaw92971171997-01-03 00:14:25 +0000224 old_handler = Handlers[sig_num].func;
225 Handlers[sig_num].tripped = 0;
Guido van Rossume4485b01994-09-07 14:32:49 +0000226 Py_INCREF(obj);
Barry Warsaw92971171997-01-03 00:14:25 +0000227 Handlers[sig_num].func = obj;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000228 return old_handler;
229}
230
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000231static char signal_doc[] =
232"signal(sig, action) -> action\n\
233\n\
234Set the action for the given signal. The action can be SIG_DFL,\n\
235SIG_IGN, or a callable Python object. The previous action is\n\
236returned. See getsignal() for possible return values.\n\
237\n\
238*** IMPORTANT NOTICE ***\n\
239A signal handler function is called with two arguments:\n\
240the first is the signal number, the second is the interrupted stack frame.";
241
Barry Warsaw92971171997-01-03 00:14:25 +0000242
Guido van Rossume4485b01994-09-07 14:32:49 +0000243static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000244signal_getsignal(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000245{
246 int sig_num;
Guido van Rossume4485b01994-09-07 14:32:49 +0000247 PyObject *old_handler;
248 if (!PyArg_Parse(args, "i", &sig_num))
Barry Warsaw92971171997-01-03 00:14:25 +0000249 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000250 if (sig_num < 1 || sig_num >= NSIG) {
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000251 PyErr_SetString(PyExc_ValueError,
252 "signal number out of range");
Barry Warsaw92971171997-01-03 00:14:25 +0000253 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000254 }
Barry Warsaw92971171997-01-03 00:14:25 +0000255 old_handler = Handlers[sig_num].func;
Guido van Rossume4485b01994-09-07 14:32:49 +0000256 Py_INCREF(old_handler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000257 return old_handler;
258}
259
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000260static char getsignal_doc[] =
261"getsignal(sig) -> action\n\
262\n\
263Return the current action for the given signal. The return value can be:\n\
264SIG_IGN -- if the signal is being ignored\n\
265SIG_DFL -- if the default action for the signal is in effect\n\
266None -- if an unknown handler is in effect\n\
267anything else -- the callable Python object used as a handler\n\
268";
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000269
Barry Warsaw92971171997-01-03 00:14:25 +0000270
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000271/* List of functions defined in the module */
Barry Warsaw92971171997-01-03 00:14:25 +0000272static PyMethodDef signal_methods[] = {
Guido van Rossum1171ee61997-08-22 20:42:00 +0000273#ifdef HAVE_ALARM
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000274 {"alarm", signal_alarm, METH_OLDARGS, alarm_doc},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000275#endif
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000276 {"signal", signal_signal, METH_OLDARGS, signal_doc},
277 {"getsignal", signal_getsignal, METH_OLDARGS, getsignal_doc},
Guido van Rossum1171ee61997-08-22 20:42:00 +0000278#ifdef HAVE_PAUSE
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000279 {"pause", signal_pause, METH_OLDARGS, pause_doc},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000280#endif
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000281 {"default_int_handler", signal_default_int_handler,
282 METH_OLDARGS, default_int_handler_doc},
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000283 {NULL, NULL} /* sentinel */
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000284};
285
Barry Warsaw92971171997-01-03 00:14:25 +0000286
287
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000288static char module_doc[] =
289"This module provides mechanisms to use signal handlers in Python.\n\
290\n\
291Functions:\n\
292\n\
293alarm() -- cause SIGALRM after a specified time [Unix only]\n\
294signal() -- set the action for a given signal\n\
295getsignal() -- get the signal action for a given signal\n\
296pause() -- wait until a signal arrives [Unix only]\n\
297default_int_handler() -- default SIGINT handler\n\
298\n\
299Constants:\n\
300\n\
301SIG_DFL -- used to refer to the system default handler\n\
302SIG_IGN -- used to ignore the signal\n\
303NSIG -- number of defined signals\n\
304\n\
305SIGINT, SIGTERM, etc. -- signal numbers\n\
306\n\
307*** IMPORTANT NOTICE ***\n\
308A signal handler function is called with two arguments:\n\
309the first is the signal number, the second is the interrupted stack frame.";
310
Guido van Rossum3886bb61998-12-04 18:50:17 +0000311DL_EXPORT(void)
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000312initsignal(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000313{
Guido van Rossume4485b01994-09-07 14:32:49 +0000314 PyObject *m, *d, *x;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000315 int i;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000316
317#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000318 main_thread = PyThread_get_thread_ident();
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000319 main_pid = getpid();
320#endif
321
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000322 /* Create the module and add the functions */
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000323 m = Py_InitModule3("signal", signal_methods, module_doc);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000324
325 /* Add some symbolic constants to the module */
Guido van Rossume4485b01994-09-07 14:32:49 +0000326 d = PyModule_GetDict(m);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000327
Guido van Rossum276fa432000-06-30 23:04:18 +0000328 x = DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL);
Barry Warsaw92971171997-01-03 00:14:25 +0000329 if (!x || PyDict_SetItemString(d, "SIG_DFL", x) < 0)
330 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000331
Guido van Rossum276fa432000-06-30 23:04:18 +0000332 x = IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN);
Barry Warsaw92971171997-01-03 00:14:25 +0000333 if (!x || PyDict_SetItemString(d, "SIG_IGN", x) < 0)
334 goto finally;
335
336 x = PyInt_FromLong((long)NSIG);
337 if (!x || PyDict_SetItemString(d, "NSIG", x) < 0)
338 goto finally;
Guido van Rossum08c16611997-08-02 03:01:42 +0000339 Py_DECREF(x);
Barry Warsaw92971171997-01-03 00:14:25 +0000340
341 x = IntHandler = PyDict_GetItemString(d, "default_int_handler");
342 if (!x)
343 goto finally;
Guido van Rossum08c16611997-08-02 03:01:42 +0000344 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +0000345
346 Handlers[0].tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000347 for (i = 1; i < NSIG; i++) {
Tim Peters4f1b2082000-07-23 21:18:09 +0000348 void (*t)(int);
Guido van Rossumfb0df941996-05-23 22:55:35 +0000349#ifdef HAVE_SIGACTION
350 struct sigaction act;
351 sigaction(i, 0, &act);
Guido van Rossum1835c4f1996-05-29 14:15:19 +0000352 t = act.sa_handler;
Guido van Rossumfb0df941996-05-23 22:55:35 +0000353#else
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000354 t = signal(i, SIG_IGN);
355 signal(i, t);
Guido van Rossumfb0df941996-05-23 22:55:35 +0000356#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000357 Handlers[i].tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000358 if (t == SIG_DFL)
Barry Warsaw92971171997-01-03 00:14:25 +0000359 Handlers[i].func = DefaultHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000360 else if (t == SIG_IGN)
Barry Warsaw92971171997-01-03 00:14:25 +0000361 Handlers[i].func = IgnoreHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000362 else
Barry Warsaw92971171997-01-03 00:14:25 +0000363 Handlers[i].func = Py_None; /* None of our business */
364 Py_INCREF(Handlers[i].func);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000365 }
Barry Warsaw92971171997-01-03 00:14:25 +0000366 if (Handlers[SIGINT].func == DefaultHandler) {
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000367 /* Install default int handler */
Guido van Rossum08c16611997-08-02 03:01:42 +0000368 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +0000369 Py_DECREF(Handlers[SIGINT].func);
370 Handlers[SIGINT].func = IntHandler;
Guido van Rossum08c16611997-08-02 03:01:42 +0000371 old_siginthandler = signal(SIGINT, &signal_handler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000372 }
373
374#ifdef SIGHUP
Guido van Rossume4485b01994-09-07 14:32:49 +0000375 x = PyInt_FromLong(SIGHUP);
376 PyDict_SetItemString(d, "SIGHUP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000377 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000378#endif
379#ifdef SIGINT
Guido van Rossume4485b01994-09-07 14:32:49 +0000380 x = PyInt_FromLong(SIGINT);
381 PyDict_SetItemString(d, "SIGINT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000382 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000383#endif
384#ifdef SIGQUIT
Guido van Rossume4485b01994-09-07 14:32:49 +0000385 x = PyInt_FromLong(SIGQUIT);
386 PyDict_SetItemString(d, "SIGQUIT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000387 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000388#endif
389#ifdef SIGILL
Guido van Rossume4485b01994-09-07 14:32:49 +0000390 x = PyInt_FromLong(SIGILL);
391 PyDict_SetItemString(d, "SIGILL", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000392 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000393#endif
394#ifdef SIGTRAP
Guido van Rossume4485b01994-09-07 14:32:49 +0000395 x = PyInt_FromLong(SIGTRAP);
396 PyDict_SetItemString(d, "SIGTRAP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000397 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000398#endif
399#ifdef SIGIOT
Guido van Rossume4485b01994-09-07 14:32:49 +0000400 x = PyInt_FromLong(SIGIOT);
401 PyDict_SetItemString(d, "SIGIOT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000402 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000403#endif
404#ifdef SIGABRT
Guido van Rossume4485b01994-09-07 14:32:49 +0000405 x = PyInt_FromLong(SIGABRT);
406 PyDict_SetItemString(d, "SIGABRT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000407 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000408#endif
409#ifdef SIGEMT
Guido van Rossume4485b01994-09-07 14:32:49 +0000410 x = PyInt_FromLong(SIGEMT);
411 PyDict_SetItemString(d, "SIGEMT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000412 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000413#endif
414#ifdef SIGFPE
Guido van Rossume4485b01994-09-07 14:32:49 +0000415 x = PyInt_FromLong(SIGFPE);
416 PyDict_SetItemString(d, "SIGFPE", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000417 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000418#endif
419#ifdef SIGKILL
Guido van Rossume4485b01994-09-07 14:32:49 +0000420 x = PyInt_FromLong(SIGKILL);
421 PyDict_SetItemString(d, "SIGKILL", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000422 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000423#endif
424#ifdef SIGBUS
Guido van Rossume4485b01994-09-07 14:32:49 +0000425 x = PyInt_FromLong(SIGBUS);
426 PyDict_SetItemString(d, "SIGBUS", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000427 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000428#endif
429#ifdef SIGSEGV
Guido van Rossume4485b01994-09-07 14:32:49 +0000430 x = PyInt_FromLong(SIGSEGV);
431 PyDict_SetItemString(d, "SIGSEGV", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000432 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000433#endif
434#ifdef SIGSYS
Guido van Rossume4485b01994-09-07 14:32:49 +0000435 x = PyInt_FromLong(SIGSYS);
436 PyDict_SetItemString(d, "SIGSYS", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000437 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000438#endif
439#ifdef SIGPIPE
Guido van Rossume4485b01994-09-07 14:32:49 +0000440 x = PyInt_FromLong(SIGPIPE);
441 PyDict_SetItemString(d, "SIGPIPE", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000442 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000443#endif
444#ifdef SIGALRM
Guido van Rossume4485b01994-09-07 14:32:49 +0000445 x = PyInt_FromLong(SIGALRM);
446 PyDict_SetItemString(d, "SIGALRM", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000447 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000448#endif
449#ifdef SIGTERM
Guido van Rossume4485b01994-09-07 14:32:49 +0000450 x = PyInt_FromLong(SIGTERM);
451 PyDict_SetItemString(d, "SIGTERM", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000452 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000453#endif
454#ifdef SIGUSR1
Guido van Rossume4485b01994-09-07 14:32:49 +0000455 x = PyInt_FromLong(SIGUSR1);
456 PyDict_SetItemString(d, "SIGUSR1", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000457 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000458#endif
459#ifdef SIGUSR2
Guido van Rossume4485b01994-09-07 14:32:49 +0000460 x = PyInt_FromLong(SIGUSR2);
461 PyDict_SetItemString(d, "SIGUSR2", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000462 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000463#endif
464#ifdef SIGCLD
Guido van Rossume4485b01994-09-07 14:32:49 +0000465 x = PyInt_FromLong(SIGCLD);
466 PyDict_SetItemString(d, "SIGCLD", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000467 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000468#endif
469#ifdef SIGCHLD
Guido van Rossume4485b01994-09-07 14:32:49 +0000470 x = PyInt_FromLong(SIGCHLD);
471 PyDict_SetItemString(d, "SIGCHLD", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000472 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000473#endif
474#ifdef SIGPWR
Guido van Rossume4485b01994-09-07 14:32:49 +0000475 x = PyInt_FromLong(SIGPWR);
476 PyDict_SetItemString(d, "SIGPWR", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000477 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000478#endif
479#ifdef SIGIO
Guido van Rossume4485b01994-09-07 14:32:49 +0000480 x = PyInt_FromLong(SIGIO);
481 PyDict_SetItemString(d, "SIGIO", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000482 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000483#endif
484#ifdef SIGURG
Guido van Rossume4485b01994-09-07 14:32:49 +0000485 x = PyInt_FromLong(SIGURG);
486 PyDict_SetItemString(d, "SIGURG", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000487 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000488#endif
489#ifdef SIGWINCH
Guido van Rossume4485b01994-09-07 14:32:49 +0000490 x = PyInt_FromLong(SIGWINCH);
491 PyDict_SetItemString(d, "SIGWINCH", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000492 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000493#endif
494#ifdef SIGPOLL
Guido van Rossume4485b01994-09-07 14:32:49 +0000495 x = PyInt_FromLong(SIGPOLL);
496 PyDict_SetItemString(d, "SIGPOLL", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000497 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000498#endif
499#ifdef SIGSTOP
Guido van Rossume4485b01994-09-07 14:32:49 +0000500 x = PyInt_FromLong(SIGSTOP);
501 PyDict_SetItemString(d, "SIGSTOP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000502 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000503#endif
504#ifdef SIGTSTP
Guido van Rossume4485b01994-09-07 14:32:49 +0000505 x = PyInt_FromLong(SIGTSTP);
506 PyDict_SetItemString(d, "SIGTSTP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000507 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000508#endif
509#ifdef SIGCONT
Guido van Rossume4485b01994-09-07 14:32:49 +0000510 x = PyInt_FromLong(SIGCONT);
511 PyDict_SetItemString(d, "SIGCONT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000512 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000513#endif
514#ifdef SIGTTIN
Guido van Rossume4485b01994-09-07 14:32:49 +0000515 x = PyInt_FromLong(SIGTTIN);
516 PyDict_SetItemString(d, "SIGTTIN", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000517 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000518#endif
519#ifdef SIGTTOU
Guido van Rossume4485b01994-09-07 14:32:49 +0000520 x = PyInt_FromLong(SIGTTOU);
521 PyDict_SetItemString(d, "SIGTTOU", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000522 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000523#endif
524#ifdef SIGVTALRM
Guido van Rossume4485b01994-09-07 14:32:49 +0000525 x = PyInt_FromLong(SIGVTALRM);
526 PyDict_SetItemString(d, "SIGVTALRM", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000527 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000528#endif
529#ifdef SIGPROF
Guido van Rossume4485b01994-09-07 14:32:49 +0000530 x = PyInt_FromLong(SIGPROF);
531 PyDict_SetItemString(d, "SIGPROF", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000532 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000533#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000534#ifdef SIGXCPU
535 x = PyInt_FromLong(SIGXCPU);
536 PyDict_SetItemString(d, "SIGXCPU", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000537 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000538#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000539#ifdef SIGXFSZ
540 x = PyInt_FromLong(SIGXFSZ);
541 PyDict_SetItemString(d, "SIGXFSZ", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000542 Py_XDECREF(x);
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000543#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000544 if (!PyErr_Occurred())
545 return;
546
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000547 /* Check for errors */
Barry Warsaw92971171997-01-03 00:14:25 +0000548 finally:
Guido van Rossum08c16611997-08-02 03:01:42 +0000549 return;
550}
551
552static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000553finisignal(void)
Guido van Rossum08c16611997-08-02 03:01:42 +0000554{
555 int i;
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000556 PyObject *func;
Guido van Rossum08c16611997-08-02 03:01:42 +0000557
558 signal(SIGINT, old_siginthandler);
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000559 old_siginthandler = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +0000560
561 for (i = 1; i < NSIG; i++) {
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000562 func = Handlers[i].func;
Guido van Rossum08c16611997-08-02 03:01:42 +0000563 Handlers[i].tripped = 0;
Guido van Rossum08c16611997-08-02 03:01:42 +0000564 Handlers[i].func = NULL;
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000565 if (i != SIGINT && func != NULL && func != Py_None &&
566 func != DefaultHandler && func != IgnoreHandler)
567 signal(i, SIG_DFL);
568 Py_XDECREF(func);
Guido van Rossum08c16611997-08-02 03:01:42 +0000569 }
570
571 Py_XDECREF(IntHandler);
572 IntHandler = NULL;
573 Py_XDECREF(DefaultHandler);
574 DefaultHandler = NULL;
575 Py_XDECREF(IgnoreHandler);
576 IgnoreHandler = NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000577}
578
Barry Warsaw92971171997-01-03 00:14:25 +0000579
580
581/* Declared in pyerrors.h */
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000582int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000583PyErr_CheckSignals(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000584{
585 int i;
Guido van Rossume4485b01994-09-07 14:32:49 +0000586 PyObject *f;
Barry Warsaw92971171997-01-03 00:14:25 +0000587
588 if (!is_tripped)
Guido van Rossum6299d1e1994-05-31 12:51:13 +0000589 return 0;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000590#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000591 if (PyThread_get_thread_ident() != main_thread)
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000592 return 0;
593#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000594 if (!(f = PyEval_GetFrame()))
Guido van Rossume4485b01994-09-07 14:32:49 +0000595 f = Py_None;
Barry Warsaw92971171997-01-03 00:14:25 +0000596
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000597 for (i = 1; i < NSIG; i++) {
Barry Warsaw92971171997-01-03 00:14:25 +0000598 if (Handlers[i].tripped) {
599 PyObject *result = NULL;
600 PyObject *arglist = Py_BuildValue("(iO)", i, f);
601 Handlers[i].tripped = 0;
602
603 if (arglist) {
604 result = PyEval_CallObject(Handlers[i].func,
605 arglist);
Guido van Rossume4485b01994-09-07 14:32:49 +0000606 Py_DECREF(arglist);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000607 }
Barry Warsaw92971171997-01-03 00:14:25 +0000608 if (!result)
Guido van Rossumfcdd0e41997-01-21 06:13:09 +0000609 return -1;
Barry Warsaw92971171997-01-03 00:14:25 +0000610
611 Py_DECREF(result);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000612 }
613 }
Barry Warsaw92971171997-01-03 00:14:25 +0000614 is_tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000615 return 0;
616}
617
Barry Warsaw92971171997-01-03 00:14:25 +0000618
619/* Replacements for intrcheck.c functionality
620 * Declared in pyerrors.h
621 */
622void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000623PyErr_SetInterrupt(void)
Barry Warsaw92971171997-01-03 00:14:25 +0000624{
625 is_tripped++;
626 Handlers[SIGINT].tripped = 1;
Thomas Wouters334fb892000-07-25 12:56:38 +0000627 Py_AddPendingCall((int (*)(void *))PyErr_CheckSignals, NULL);
Barry Warsaw92971171997-01-03 00:14:25 +0000628}
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000629
630void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000631PyOS_InitInterrupts(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000632{
633 initsignal();
Guido van Rossum08c16611997-08-02 03:01:42 +0000634 _PyImport_FixupExtension("signal", "signal");
635}
636
637void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000638PyOS_FiniInterrupts(void)
Guido van Rossum08c16611997-08-02 03:01:42 +0000639{
640 finisignal();
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000641}
642
643int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000644PyOS_InterruptOccurred(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000645{
Barry Warsaw92971171997-01-03 00:14:25 +0000646 if (Handlers[SIGINT].tripped) {
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000647#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000648 if (PyThread_get_thread_ident() != main_thread)
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000649 return 0;
650#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000651 Handlers[SIGINT].tripped = 0;
Guido van Rossum6299d1e1994-05-31 12:51:13 +0000652 return 1;
653 }
654 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000655}
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000656
657void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000658PyOS_AfterFork(void)
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000659{
660#ifdef WITH_THREAD
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000661 PyEval_ReInitThreads();
Guido van Rossum65d5b571998-12-21 19:32:43 +0000662 main_thread = PyThread_get_thread_ident();
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000663 main_pid = getpid();
664#endif
665}