blob: 1c11fdd250310917e4bf906046a1e628cca5ebb2 [file] [log] [blame]
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001/***********************************************************
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00002Copyright (c) 2000, BeOpen.com.
3Copyright (c) 1995-2000, Corporation for National Research Initiatives.
4Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
5All rights reserved.
Guido van Rossum398d9fe1994-05-11 08:59:13 +00006
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00007See the file "Misc/COPYRIGHT" for information on usage and
8redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
Guido van Rossum398d9fe1994-05-11 08:59:13 +00009******************************************************************/
10
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000011/* Signal module -- many thanks to Lance Ellinghaus */
Guido van Rossum398d9fe1994-05-11 08:59:13 +000012
Guido van Rossum644a12b1997-04-09 19:24:53 +000013/* XXX Signals should be recorded per thread, now we have thread state. */
14
Guido van Rossum602099a1994-09-14 13:32:22 +000015#include "Python.h"
Guido van Rossum398d9fe1994-05-11 08:59:13 +000016#include "intrcheck.h"
17
Guido van Rossum644a12b1997-04-09 19:24:53 +000018#ifdef MS_WIN32
19#include <process.h>
20#endif
21
Guido van Rossuma376cc51996-12-05 23:43:35 +000022#ifdef HAVE_UNISTD_H
23#include <unistd.h>
24#endif
25
Guido van Rossum398d9fe1994-05-11 08:59:13 +000026#include <signal.h>
27
Guido van Rossumbb4ba121994-06-23 11:25:45 +000028#ifndef SIG_ERR
29#define SIG_ERR ((RETSIGTYPE (*)())-1)
30#endif
31
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000032#if defined(PYOS_OS2)
33#define NSIG 12
34#include <process.h>
35#endif
36
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000037#ifndef NSIG
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000038# if defined(_NSIG)
39# define NSIG _NSIG /* For BSD/SysV */
40# elif defined(_SIGMAX)
41# define NSIG (_SIGMAX + 1) /* For QNX */
42# elif defined(SIGMAX)
43# define NSIG (SIGMAX + 1) /* For djgpp */
44# else
45# define NSIG 64 /* Use a reasonable default value */
46# endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000047#endif
48
49
Barry Warsaw92971171997-01-03 00:14:25 +000050
Guido van Rossumbb4ba121994-06-23 11:25:45 +000051/*
52 NOTES ON THE INTERACTION BETWEEN SIGNALS AND THREADS
53
54 When threads are supported, we want the following semantics:
55
56 - only the main thread can set a signal handler
57 - any thread can get a signal handler
58 - signals are only delivered to the main thread
59
60 I.e. we don't support "synchronous signals" like SIGFPE (catching
61 this doesn't make much sense in Python anyway) nor do we support
62 signals as a means of inter-thread communication, since not all
63 thread implementations support that (at least our thread library
64 doesn't).
65
66 We still have the problem that in some implementations signals
67 generated by the keyboard (e.g. SIGINT) are delivered to all
68 threads (e.g. SGI), while in others (e.g. Solaris) such signals are
69 delivered to one random thread (an intermediate possibility would
Guido van Rossuma3c04b01995-01-12 11:29:01 +000070 be to deliver it to the main thread -- POSIX?). For now, we have
Guido van Rossumbb4ba121994-06-23 11:25:45 +000071 a working implementation that works in all three cases -- the
72 handler ignores signals if getpid() isn't the same as in the main
73 thread. XXX This is a hack.
74
75*/
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
Barry Warsaw92971171997-01-03 00:14:25 +000089static int is_tripped = 0; /* Speed up sigcheck() when none tripped */
Guido van Rossum398d9fe1994-05-11 08:59:13 +000090
Barry Warsaw92971171997-01-03 00:14:25 +000091static PyObject *DefaultHandler;
92static PyObject *IgnoreHandler;
93static PyObject *IntHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +000094
Guido van Rossum08c16611997-08-02 03:01:42 +000095static RETSIGTYPE (*old_siginthandler)() = SIG_DFL;
96
Barry Warsaw92971171997-01-03 00:14:25 +000097
98
Guido van Rossume4485b01994-09-07 14:32:49 +000099static PyObject *
Barry Warsaw92971171997-01-03 00:14:25 +0000100signal_default_int_handler(self, arg)
Guido van Rossume4485b01994-09-07 14:32:49 +0000101 PyObject *self;
102 PyObject *arg;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000103{
Guido van Rossume4485b01994-09-07 14:32:49 +0000104 PyErr_SetNone(PyExc_KeyboardInterrupt);
Barry Warsaw92971171997-01-03 00:14:25 +0000105 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000106}
107
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000108static char default_int_handler_doc[] =
109"default_int_handler(...)\n\
110\n\
111The default handler for SIGINT instated by Python.\n\
112It raises KeyboardInterrupt.";
113
Barry Warsaw92971171997-01-03 00:14:25 +0000114
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000115static RETSIGTYPE
Barry Warsaw92971171997-01-03 00:14:25 +0000116signal_handler(sig_num)
Guido van Rossume4485b01994-09-07 14:32:49 +0000117 int sig_num;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000118{
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000119#ifdef WITH_THREAD
120 /* See NOTES section above */
121 if (getpid() == main_pid) {
122#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000123 is_tripped++;
124 Handlers[sig_num].tripped = 1;
Guido van Rossum644a12b1997-04-09 19:24:53 +0000125 Py_AddPendingCall(
126 (int (*) Py_PROTO((ANY *)))PyErr_CheckSignals, NULL);
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000127#ifdef WITH_THREAD
128 }
129#endif
Guido van Rossum602099a1994-09-14 13:32:22 +0000130#ifdef SIGCHLD
131 if (sig_num == SIGCHLD) {
132 /* To avoid infinite recursion, this signal remains
133 reset until explicit re-instated.
134 Don't clear the 'func' field as it is our pointer
135 to the Python handler... */
136 return;
137 }
138#endif
Guido van Rossum1b236761998-09-21 14:46:00 +0000139#ifdef HAVE_SIGINTERRUPT
140 siginterrupt(sig_num, 1);
141#endif
Guido van Rossuma5e54d01998-05-01 18:58:59 +0000142 (void)signal(sig_num, &signal_handler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000143}
Guido van Rossume4485b01994-09-07 14:32:49 +0000144
Guido van Rossum06d511d1995-03-10 15:13:48 +0000145
Barry Warsaw92971171997-01-03 00:14:25 +0000146
Guido van Rossum1171ee61997-08-22 20:42:00 +0000147#ifdef HAVE_ALARM
Guido van Rossume4485b01994-09-07 14:32:49 +0000148static PyObject *
Barry Warsaw92971171997-01-03 00:14:25 +0000149signal_alarm(self, args)
Guido van Rossume4485b01994-09-07 14:32:49 +0000150 PyObject *self; /* Not used */
151 PyObject *args;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000152{
153 int t;
Guido van Rossume4485b01994-09-07 14:32:49 +0000154 if (!PyArg_Parse(args, "i", &t))
Barry Warsaw92971171997-01-03 00:14:25 +0000155 return NULL;
Guido van Rossume4485b01994-09-07 14:32:49 +0000156 /* alarm() returns the number of seconds remaining */
157 return PyInt_FromLong(alarm(t));
Guido van Rossumaa0f4c71994-08-23 13:49:37 +0000158}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000159
160static char alarm_doc[] =
161"alarm(seconds)\n\
162\n\
Guido van Rossum911ec181998-06-28 17:00:19 +0000163Arrange for SIGALRM to arrive after the given number of seconds.";
Guido van Rossum06d511d1995-03-10 15:13:48 +0000164#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000165
Guido van Rossum1171ee61997-08-22 20:42:00 +0000166#ifdef HAVE_PAUSE
Guido van Rossuma597dde1995-01-10 20:56:29 +0000167static PyObject *
Barry Warsaw92971171997-01-03 00:14:25 +0000168signal_pause(self, args)
Guido van Rossume4485b01994-09-07 14:32:49 +0000169 PyObject *self; /* Not used */
170 PyObject *args;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000171{
Guido van Rossume4485b01994-09-07 14:32:49 +0000172 if (!PyArg_NoArgs(args))
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000173 return NULL;
Barry Warsaw92971171997-01-03 00:14:25 +0000174
Guido van Rossuma597dde1995-01-10 20:56:29 +0000175 Py_BEGIN_ALLOW_THREADS
Barry Warsaw92971171997-01-03 00:14:25 +0000176 (void)pause();
Guido van Rossuma597dde1995-01-10 20:56:29 +0000177 Py_END_ALLOW_THREADS
Barry Warsaw92971171997-01-03 00:14:25 +0000178 /* make sure that any exceptions that got raised are propagated
179 * back into Python
180 */
181 if (PyErr_CheckSignals())
182 return NULL;
183
Guido van Rossume4485b01994-09-07 14:32:49 +0000184 Py_INCREF(Py_None);
185 return Py_None;
186}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000187static char pause_doc[] =
Barry Warsaw1ee36ff1998-07-21 22:41:18 +0000188"pause()\n\
189\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000190Wait until a signal arrives.";
191
Guido van Rossum06d511d1995-03-10 15:13:48 +0000192#endif
Guido van Rossume4485b01994-09-07 14:32:49 +0000193
Barry Warsaw92971171997-01-03 00:14:25 +0000194
Guido van Rossume4485b01994-09-07 14:32:49 +0000195static PyObject *
Barry Warsaw92971171997-01-03 00:14:25 +0000196signal_signal(self, args)
Guido van Rossume4485b01994-09-07 14:32:49 +0000197 PyObject *self; /* Not used */
198 PyObject *args;
199{
200 PyObject *obj;
201 int sig_num;
202 PyObject *old_handler;
203 RETSIGTYPE (*func)();
204 if (!PyArg_Parse(args, "(iO)", &sig_num, &obj))
Barry Warsaw92971171997-01-03 00:14:25 +0000205 return NULL;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000206#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000207 if (PyThread_get_thread_ident() != main_thread) {
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000208 PyErr_SetString(PyExc_ValueError,
209 "signal only works in main thread");
Barry Warsaw92971171997-01-03 00:14:25 +0000210 return NULL;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000211 }
212#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000213 if (sig_num < 1 || sig_num >= NSIG) {
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000214 PyErr_SetString(PyExc_ValueError,
215 "signal number out of range");
Barry Warsaw92971171997-01-03 00:14:25 +0000216 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000217 }
Barry Warsaw92971171997-01-03 00:14:25 +0000218 if (obj == IgnoreHandler)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000219 func = SIG_IGN;
Barry Warsaw92971171997-01-03 00:14:25 +0000220 else if (obj == DefaultHandler)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000221 func = SIG_DFL;
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000222 else if (!PyCallable_Check(obj)) {
Guido van Rossume4485b01994-09-07 14:32:49 +0000223 PyErr_SetString(PyExc_TypeError,
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000224"signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object");
Barry Warsaw92971171997-01-03 00:14:25 +0000225 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000226 }
227 else
Barry Warsaw92971171997-01-03 00:14:25 +0000228 func = signal_handler;
Guido van Rossum1b236761998-09-21 14:46:00 +0000229#ifdef HAVE_SIGINTERRUPT
230 siginterrupt(sig_num, 1);
231#endif
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000232 if (signal(sig_num, func) == SIG_ERR) {
Guido van Rossume4485b01994-09-07 14:32:49 +0000233 PyErr_SetFromErrno(PyExc_RuntimeError);
Barry Warsaw92971171997-01-03 00:14:25 +0000234 return NULL;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000235 }
Barry Warsaw92971171997-01-03 00:14:25 +0000236 old_handler = Handlers[sig_num].func;
237 Handlers[sig_num].tripped = 0;
Guido van Rossume4485b01994-09-07 14:32:49 +0000238 Py_INCREF(obj);
Barry Warsaw92971171997-01-03 00:14:25 +0000239 Handlers[sig_num].func = obj;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000240 return old_handler;
241}
242
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000243static char signal_doc[] =
244"signal(sig, action) -> action\n\
245\n\
246Set the action for the given signal. The action can be SIG_DFL,\n\
247SIG_IGN, or a callable Python object. The previous action is\n\
248returned. See getsignal() for possible return values.\n\
249\n\
250*** IMPORTANT NOTICE ***\n\
251A signal handler function is called with two arguments:\n\
252the first is the signal number, the second is the interrupted stack frame.";
253
Barry Warsaw92971171997-01-03 00:14:25 +0000254
Guido van Rossume4485b01994-09-07 14:32:49 +0000255static PyObject *
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000256signal_getsignal(self, args)
Guido van Rossume4485b01994-09-07 14:32:49 +0000257 PyObject *self; /* Not used */
258 PyObject *args;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000259{
260 int sig_num;
Guido van Rossume4485b01994-09-07 14:32:49 +0000261 PyObject *old_handler;
262 if (!PyArg_Parse(args, "i", &sig_num))
Barry Warsaw92971171997-01-03 00:14:25 +0000263 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000264 if (sig_num < 1 || sig_num >= NSIG) {
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000265 PyErr_SetString(PyExc_ValueError,
266 "signal number out of range");
Barry Warsaw92971171997-01-03 00:14:25 +0000267 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000268 }
Barry Warsaw92971171997-01-03 00:14:25 +0000269 old_handler = Handlers[sig_num].func;
Guido van Rossume4485b01994-09-07 14:32:49 +0000270 Py_INCREF(old_handler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000271 return old_handler;
272}
273
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000274static char getsignal_doc[] =
275"getsignal(sig) -> action\n\
276\n\
277Return the current action for the given signal. The return value can be:\n\
278SIG_IGN -- if the signal is being ignored\n\
279SIG_DFL -- if the default action for the signal is in effect\n\
280None -- if an unknown handler is in effect\n\
281anything else -- the callable Python object used as a handler\n\
282";
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000283
Barry Warsaw92971171997-01-03 00:14:25 +0000284
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000285/* List of functions defined in the module */
Barry Warsaw92971171997-01-03 00:14:25 +0000286static PyMethodDef signal_methods[] = {
Guido van Rossum1171ee61997-08-22 20:42:00 +0000287#ifdef HAVE_ALARM
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000288 {"alarm", signal_alarm, 0, alarm_doc},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000289#endif
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000290 {"signal", signal_signal, 0, signal_doc},
291 {"getsignal", signal_getsignal, 0, getsignal_doc},
Guido van Rossum1171ee61997-08-22 20:42:00 +0000292#ifdef HAVE_PAUSE
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000293 {"pause", signal_pause, 0, pause_doc},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000294#endif
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000295 {"default_int_handler", signal_default_int_handler, 0,
296 default_int_handler_doc},
297 {NULL, NULL} /* sentinel */
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000298};
299
Barry Warsaw92971171997-01-03 00:14:25 +0000300
301
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000302static char module_doc[] =
303"This module provides mechanisms to use signal handlers in Python.\n\
304\n\
305Functions:\n\
306\n\
307alarm() -- cause SIGALRM after a specified time [Unix only]\n\
308signal() -- set the action for a given signal\n\
309getsignal() -- get the signal action for a given signal\n\
310pause() -- wait until a signal arrives [Unix only]\n\
311default_int_handler() -- default SIGINT handler\n\
312\n\
313Constants:\n\
314\n\
315SIG_DFL -- used to refer to the system default handler\n\
316SIG_IGN -- used to ignore the signal\n\
317NSIG -- number of defined signals\n\
318\n\
319SIGINT, SIGTERM, etc. -- signal numbers\n\
320\n\
321*** IMPORTANT NOTICE ***\n\
322A signal handler function is called with two arguments:\n\
323the first is the signal number, the second is the interrupted stack frame.";
324
Guido van Rossum3886bb61998-12-04 18:50:17 +0000325DL_EXPORT(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000326initsignal()
327{
Guido van Rossume4485b01994-09-07 14:32:49 +0000328 PyObject *m, *d, *x;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000329 int i;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000330
331#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000332 main_thread = PyThread_get_thread_ident();
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000333 main_pid = getpid();
334#endif
335
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000336 /* Create the module and add the functions */
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000337 m = Py_InitModule3("signal", signal_methods, module_doc);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000338
339 /* Add some symbolic constants to the module */
Guido van Rossume4485b01994-09-07 14:32:49 +0000340 d = PyModule_GetDict(m);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000341
Guido van Rossum276fa432000-06-30 23:04:18 +0000342 x = DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL);
Barry Warsaw92971171997-01-03 00:14:25 +0000343 if (!x || PyDict_SetItemString(d, "SIG_DFL", x) < 0)
344 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000345
Guido van Rossum276fa432000-06-30 23:04:18 +0000346 x = IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN);
Barry Warsaw92971171997-01-03 00:14:25 +0000347 if (!x || PyDict_SetItemString(d, "SIG_IGN", x) < 0)
348 goto finally;
349
350 x = PyInt_FromLong((long)NSIG);
351 if (!x || PyDict_SetItemString(d, "NSIG", x) < 0)
352 goto finally;
Guido van Rossum08c16611997-08-02 03:01:42 +0000353 Py_DECREF(x);
Barry Warsaw92971171997-01-03 00:14:25 +0000354
355 x = IntHandler = PyDict_GetItemString(d, "default_int_handler");
356 if (!x)
357 goto finally;
Guido van Rossum08c16611997-08-02 03:01:42 +0000358 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +0000359
360 Handlers[0].tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000361 for (i = 1; i < NSIG; i++) {
Guido van Rossum6299d1e1994-05-31 12:51:13 +0000362 RETSIGTYPE (*t)();
Guido van Rossumfb0df941996-05-23 22:55:35 +0000363#ifdef HAVE_SIGACTION
364 struct sigaction act;
365 sigaction(i, 0, &act);
Guido van Rossum1835c4f1996-05-29 14:15:19 +0000366 t = act.sa_handler;
Guido van Rossumfb0df941996-05-23 22:55:35 +0000367#else
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000368 t = signal(i, SIG_IGN);
369 signal(i, t);
Guido van Rossumfb0df941996-05-23 22:55:35 +0000370#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000371 Handlers[i].tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000372 if (t == SIG_DFL)
Barry Warsaw92971171997-01-03 00:14:25 +0000373 Handlers[i].func = DefaultHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000374 else if (t == SIG_IGN)
Barry Warsaw92971171997-01-03 00:14:25 +0000375 Handlers[i].func = IgnoreHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000376 else
Barry Warsaw92971171997-01-03 00:14:25 +0000377 Handlers[i].func = Py_None; /* None of our business */
378 Py_INCREF(Handlers[i].func);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000379 }
Barry Warsaw92971171997-01-03 00:14:25 +0000380 if (Handlers[SIGINT].func == DefaultHandler) {
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000381 /* Install default int handler */
Guido van Rossum08c16611997-08-02 03:01:42 +0000382 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +0000383 Py_DECREF(Handlers[SIGINT].func);
384 Handlers[SIGINT].func = IntHandler;
Guido van Rossum08c16611997-08-02 03:01:42 +0000385 old_siginthandler = signal(SIGINT, &signal_handler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000386 }
387
388#ifdef SIGHUP
Guido van Rossume4485b01994-09-07 14:32:49 +0000389 x = PyInt_FromLong(SIGHUP);
390 PyDict_SetItemString(d, "SIGHUP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000391 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000392#endif
393#ifdef SIGINT
Guido van Rossume4485b01994-09-07 14:32:49 +0000394 x = PyInt_FromLong(SIGINT);
395 PyDict_SetItemString(d, "SIGINT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000396 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000397#endif
398#ifdef SIGQUIT
Guido van Rossume4485b01994-09-07 14:32:49 +0000399 x = PyInt_FromLong(SIGQUIT);
400 PyDict_SetItemString(d, "SIGQUIT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000401 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000402#endif
403#ifdef SIGILL
Guido van Rossume4485b01994-09-07 14:32:49 +0000404 x = PyInt_FromLong(SIGILL);
405 PyDict_SetItemString(d, "SIGILL", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000406 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000407#endif
408#ifdef SIGTRAP
Guido van Rossume4485b01994-09-07 14:32:49 +0000409 x = PyInt_FromLong(SIGTRAP);
410 PyDict_SetItemString(d, "SIGTRAP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000411 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000412#endif
413#ifdef SIGIOT
Guido van Rossume4485b01994-09-07 14:32:49 +0000414 x = PyInt_FromLong(SIGIOT);
415 PyDict_SetItemString(d, "SIGIOT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000416 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000417#endif
418#ifdef SIGABRT
Guido van Rossume4485b01994-09-07 14:32:49 +0000419 x = PyInt_FromLong(SIGABRT);
420 PyDict_SetItemString(d, "SIGABRT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000421 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000422#endif
423#ifdef SIGEMT
Guido van Rossume4485b01994-09-07 14:32:49 +0000424 x = PyInt_FromLong(SIGEMT);
425 PyDict_SetItemString(d, "SIGEMT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000426 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000427#endif
428#ifdef SIGFPE
Guido van Rossume4485b01994-09-07 14:32:49 +0000429 x = PyInt_FromLong(SIGFPE);
430 PyDict_SetItemString(d, "SIGFPE", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000431 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000432#endif
433#ifdef SIGKILL
Guido van Rossume4485b01994-09-07 14:32:49 +0000434 x = PyInt_FromLong(SIGKILL);
435 PyDict_SetItemString(d, "SIGKILL", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000436 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000437#endif
438#ifdef SIGBUS
Guido van Rossume4485b01994-09-07 14:32:49 +0000439 x = PyInt_FromLong(SIGBUS);
440 PyDict_SetItemString(d, "SIGBUS", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000441 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000442#endif
443#ifdef SIGSEGV
Guido van Rossume4485b01994-09-07 14:32:49 +0000444 x = PyInt_FromLong(SIGSEGV);
445 PyDict_SetItemString(d, "SIGSEGV", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000446 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000447#endif
448#ifdef SIGSYS
Guido van Rossume4485b01994-09-07 14:32:49 +0000449 x = PyInt_FromLong(SIGSYS);
450 PyDict_SetItemString(d, "SIGSYS", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000451 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000452#endif
453#ifdef SIGPIPE
Guido van Rossume4485b01994-09-07 14:32:49 +0000454 x = PyInt_FromLong(SIGPIPE);
455 PyDict_SetItemString(d, "SIGPIPE", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000456 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000457#endif
458#ifdef SIGALRM
Guido van Rossume4485b01994-09-07 14:32:49 +0000459 x = PyInt_FromLong(SIGALRM);
460 PyDict_SetItemString(d, "SIGALRM", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000461 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000462#endif
463#ifdef SIGTERM
Guido van Rossume4485b01994-09-07 14:32:49 +0000464 x = PyInt_FromLong(SIGTERM);
465 PyDict_SetItemString(d, "SIGTERM", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000466 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000467#endif
468#ifdef SIGUSR1
Guido van Rossume4485b01994-09-07 14:32:49 +0000469 x = PyInt_FromLong(SIGUSR1);
470 PyDict_SetItemString(d, "SIGUSR1", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000471 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000472#endif
473#ifdef SIGUSR2
Guido van Rossume4485b01994-09-07 14:32:49 +0000474 x = PyInt_FromLong(SIGUSR2);
475 PyDict_SetItemString(d, "SIGUSR2", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000476 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000477#endif
478#ifdef SIGCLD
Guido van Rossume4485b01994-09-07 14:32:49 +0000479 x = PyInt_FromLong(SIGCLD);
480 PyDict_SetItemString(d, "SIGCLD", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000481 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000482#endif
483#ifdef SIGCHLD
Guido van Rossume4485b01994-09-07 14:32:49 +0000484 x = PyInt_FromLong(SIGCHLD);
485 PyDict_SetItemString(d, "SIGCHLD", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000486 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000487#endif
488#ifdef SIGPWR
Guido van Rossume4485b01994-09-07 14:32:49 +0000489 x = PyInt_FromLong(SIGPWR);
490 PyDict_SetItemString(d, "SIGPWR", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000491 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000492#endif
493#ifdef SIGIO
Guido van Rossume4485b01994-09-07 14:32:49 +0000494 x = PyInt_FromLong(SIGIO);
495 PyDict_SetItemString(d, "SIGIO", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000496 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000497#endif
498#ifdef SIGURG
Guido van Rossume4485b01994-09-07 14:32:49 +0000499 x = PyInt_FromLong(SIGURG);
500 PyDict_SetItemString(d, "SIGURG", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000501 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000502#endif
503#ifdef SIGWINCH
Guido van Rossume4485b01994-09-07 14:32:49 +0000504 x = PyInt_FromLong(SIGWINCH);
505 PyDict_SetItemString(d, "SIGWINCH", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000506 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000507#endif
508#ifdef SIGPOLL
Guido van Rossume4485b01994-09-07 14:32:49 +0000509 x = PyInt_FromLong(SIGPOLL);
510 PyDict_SetItemString(d, "SIGPOLL", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000511 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000512#endif
513#ifdef SIGSTOP
Guido van Rossume4485b01994-09-07 14:32:49 +0000514 x = PyInt_FromLong(SIGSTOP);
515 PyDict_SetItemString(d, "SIGSTOP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000516 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000517#endif
518#ifdef SIGTSTP
Guido van Rossume4485b01994-09-07 14:32:49 +0000519 x = PyInt_FromLong(SIGTSTP);
520 PyDict_SetItemString(d, "SIGTSTP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000521 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000522#endif
523#ifdef SIGCONT
Guido van Rossume4485b01994-09-07 14:32:49 +0000524 x = PyInt_FromLong(SIGCONT);
525 PyDict_SetItemString(d, "SIGCONT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000526 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000527#endif
528#ifdef SIGTTIN
Guido van Rossume4485b01994-09-07 14:32:49 +0000529 x = PyInt_FromLong(SIGTTIN);
530 PyDict_SetItemString(d, "SIGTTIN", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000531 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000532#endif
533#ifdef SIGTTOU
Guido van Rossume4485b01994-09-07 14:32:49 +0000534 x = PyInt_FromLong(SIGTTOU);
535 PyDict_SetItemString(d, "SIGTTOU", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000536 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000537#endif
538#ifdef SIGVTALRM
Guido van Rossume4485b01994-09-07 14:32:49 +0000539 x = PyInt_FromLong(SIGVTALRM);
540 PyDict_SetItemString(d, "SIGVTALRM", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000541 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000542#endif
543#ifdef SIGPROF
Guido van Rossume4485b01994-09-07 14:32:49 +0000544 x = PyInt_FromLong(SIGPROF);
545 PyDict_SetItemString(d, "SIGPROF", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000546 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000547#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000548#ifdef SIGXCPU
549 x = PyInt_FromLong(SIGXCPU);
550 PyDict_SetItemString(d, "SIGXCPU", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000551 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000552#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000553#ifdef SIGXFSZ
554 x = PyInt_FromLong(SIGXFSZ);
555 PyDict_SetItemString(d, "SIGXFSZ", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000556 Py_XDECREF(x);
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000557#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000558 if (!PyErr_Occurred())
559 return;
560
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000561 /* Check for errors */
Barry Warsaw92971171997-01-03 00:14:25 +0000562 finally:
Guido van Rossum08c16611997-08-02 03:01:42 +0000563 return;
564}
565
566static void
567finisignal()
568{
569 int i;
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000570 PyObject *func;
Guido van Rossum08c16611997-08-02 03:01:42 +0000571
572 signal(SIGINT, old_siginthandler);
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000573 old_siginthandler = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +0000574
575 for (i = 1; i < NSIG; i++) {
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000576 func = Handlers[i].func;
Guido van Rossum08c16611997-08-02 03:01:42 +0000577 Handlers[i].tripped = 0;
Guido van Rossum08c16611997-08-02 03:01:42 +0000578 Handlers[i].func = NULL;
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000579 if (i != SIGINT && func != NULL && func != Py_None &&
580 func != DefaultHandler && func != IgnoreHandler)
581 signal(i, SIG_DFL);
582 Py_XDECREF(func);
Guido van Rossum08c16611997-08-02 03:01:42 +0000583 }
584
585 Py_XDECREF(IntHandler);
586 IntHandler = NULL;
587 Py_XDECREF(DefaultHandler);
588 DefaultHandler = NULL;
589 Py_XDECREF(IgnoreHandler);
590 IgnoreHandler = NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000591}
592
Barry Warsaw92971171997-01-03 00:14:25 +0000593
594
595/* Declared in pyerrors.h */
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000596int
Guido van Rossumec25b911995-01-22 00:46:57 +0000597PyErr_CheckSignals()
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000598{
599 int i;
Guido van Rossume4485b01994-09-07 14:32:49 +0000600 PyObject *f;
Barry Warsaw92971171997-01-03 00:14:25 +0000601
602 if (!is_tripped)
Guido van Rossum6299d1e1994-05-31 12:51:13 +0000603 return 0;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000604#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000605 if (PyThread_get_thread_ident() != main_thread)
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000606 return 0;
607#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000608 if (!(f = PyEval_GetFrame()))
Guido van Rossume4485b01994-09-07 14:32:49 +0000609 f = Py_None;
Barry Warsaw92971171997-01-03 00:14:25 +0000610
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000611 for (i = 1; i < NSIG; i++) {
Barry Warsaw92971171997-01-03 00:14:25 +0000612 if (Handlers[i].tripped) {
613 PyObject *result = NULL;
614 PyObject *arglist = Py_BuildValue("(iO)", i, f);
615 Handlers[i].tripped = 0;
616
617 if (arglist) {
618 result = PyEval_CallObject(Handlers[i].func,
619 arglist);
Guido van Rossume4485b01994-09-07 14:32:49 +0000620 Py_DECREF(arglist);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000621 }
Barry Warsaw92971171997-01-03 00:14:25 +0000622 if (!result)
Guido van Rossumfcdd0e41997-01-21 06:13:09 +0000623 return -1;
Barry Warsaw92971171997-01-03 00:14:25 +0000624
625 Py_DECREF(result);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000626 }
627 }
Barry Warsaw92971171997-01-03 00:14:25 +0000628 is_tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000629 return 0;
630}
631
Barry Warsaw92971171997-01-03 00:14:25 +0000632
633/* Replacements for intrcheck.c functionality
634 * Declared in pyerrors.h
635 */
636void
637PyErr_SetInterrupt()
638{
639 is_tripped++;
640 Handlers[SIGINT].tripped = 1;
Guido van Rossumfcdd0e41997-01-21 06:13:09 +0000641 Py_AddPendingCall((int (*) Py_PROTO((ANY *)))PyErr_CheckSignals, NULL);
Barry Warsaw92971171997-01-03 00:14:25 +0000642}
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000643
644void
Barry Warsaw92971171997-01-03 00:14:25 +0000645PyOS_InitInterrupts()
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000646{
647 initsignal();
Guido van Rossum08c16611997-08-02 03:01:42 +0000648 _PyImport_FixupExtension("signal", "signal");
649}
650
651void
652PyOS_FiniInterrupts()
653{
654 finisignal();
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000655}
656
657int
Barry Warsaw92971171997-01-03 00:14:25 +0000658PyOS_InterruptOccurred()
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000659{
Barry Warsaw92971171997-01-03 00:14:25 +0000660 if (Handlers[SIGINT].tripped) {
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000661#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000662 if (PyThread_get_thread_ident() != main_thread)
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000663 return 0;
664#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000665 Handlers[SIGINT].tripped = 0;
Guido van Rossum6299d1e1994-05-31 12:51:13 +0000666 return 1;
667 }
668 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000669}
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000670
671void
672PyOS_AfterFork()
673{
674#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000675 main_thread = PyThread_get_thread_ident();
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000676 main_pid = getpid();
677#endif
678}