blob: 26bb94050e75b7dfc8ed3dea194208c9a6ef55d4 [file] [log] [blame]
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001/***********************************************************
Guido van Rossum524b5881995-01-04 19:10:35 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossum398d9fe1994-05-11 08:59:13 +00004
5 All Rights Reserved
6
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossum398d9fe1994-05-11 08:59:13 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossum398d9fe1994-05-11 08:59:13 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossum398d9fe1994-05-11 08:59:13 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossum398d9fe1994-05-11 08:59:13 +000029
30******************************************************************/
31
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000032/* Signal module -- many thanks to Lance Ellinghaus */
Guido van Rossum398d9fe1994-05-11 08:59:13 +000033
Guido van Rossum644a12b1997-04-09 19:24:53 +000034/* XXX Signals should be recorded per thread, now we have thread state. */
35
Guido van Rossum602099a1994-09-14 13:32:22 +000036#include "Python.h"
Guido van Rossum398d9fe1994-05-11 08:59:13 +000037#include "intrcheck.h"
38
Guido van Rossum644a12b1997-04-09 19:24:53 +000039#ifdef MS_WIN32
40#include <process.h>
41#endif
42
Guido van Rossuma376cc51996-12-05 23:43:35 +000043#ifdef HAVE_UNISTD_H
44#include <unistd.h>
45#endif
46
Guido van Rossum398d9fe1994-05-11 08:59:13 +000047#include <signal.h>
48
Guido van Rossumbb4ba121994-06-23 11:25:45 +000049#ifndef SIG_ERR
50#define SIG_ERR ((RETSIGTYPE (*)())-1)
51#endif
52
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000053#if defined(PYOS_OS2)
54#define NSIG 12
55#include <process.h>
56#endif
57
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000058#ifndef NSIG
Guido van Rossumc1cc8ab1997-02-14 16:35:36 +000059#ifdef _SIGMAX
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000060#define NSIG (_SIGMAX + 1) /* For QNX */
Guido van Rossumc1cc8ab1997-02-14 16:35:36 +000061#else
62#define NSIG (SIGMAX + 1) /* for djgpp */
63#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000064#endif
65
66
Barry Warsaw92971171997-01-03 00:14:25 +000067
Guido van Rossumbb4ba121994-06-23 11:25:45 +000068/*
69 NOTES ON THE INTERACTION BETWEEN SIGNALS AND THREADS
70
71 When threads are supported, we want the following semantics:
72
73 - only the main thread can set a signal handler
74 - any thread can get a signal handler
75 - signals are only delivered to the main thread
76
77 I.e. we don't support "synchronous signals" like SIGFPE (catching
78 this doesn't make much sense in Python anyway) nor do we support
79 signals as a means of inter-thread communication, since not all
80 thread implementations support that (at least our thread library
81 doesn't).
82
83 We still have the problem that in some implementations signals
84 generated by the keyboard (e.g. SIGINT) are delivered to all
85 threads (e.g. SGI), while in others (e.g. Solaris) such signals are
86 delivered to one random thread (an intermediate possibility would
Guido van Rossuma3c04b01995-01-12 11:29:01 +000087 be to deliver it to the main thread -- POSIX?). For now, we have
Guido van Rossumbb4ba121994-06-23 11:25:45 +000088 a working implementation that works in all three cases -- the
89 handler ignores signals if getpid() isn't the same as in the main
90 thread. XXX This is a hack.
91
92*/
93
94#ifdef WITH_THREAD
Guido van Rossum295b8e51997-06-06 21:16:41 +000095#include <sys/types.h> /* For pid_t */
Guido van Rossum49b56061998-10-01 20:42:43 +000096#include "pythread.h"
Guido van Rossumbb4ba121994-06-23 11:25:45 +000097static long main_thread;
98static pid_t main_pid;
99#endif
100
Barry Warsaw92971171997-01-03 00:14:25 +0000101static struct {
102 int tripped;
103 PyObject *func;
104} Handlers[NSIG];
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000105
Barry Warsaw92971171997-01-03 00:14:25 +0000106static int is_tripped = 0; /* Speed up sigcheck() when none tripped */
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000107
Barry Warsaw92971171997-01-03 00:14:25 +0000108static PyObject *DefaultHandler;
109static PyObject *IgnoreHandler;
110static PyObject *IntHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000111
Guido van Rossum08c16611997-08-02 03:01:42 +0000112static RETSIGTYPE (*old_siginthandler)() = SIG_DFL;
113
Barry Warsaw92971171997-01-03 00:14:25 +0000114
115
Guido van Rossume4485b01994-09-07 14:32:49 +0000116static PyObject *
Barry Warsaw92971171997-01-03 00:14:25 +0000117signal_default_int_handler(self, arg)
Guido van Rossume4485b01994-09-07 14:32:49 +0000118 PyObject *self;
119 PyObject *arg;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000120{
Guido van Rossume4485b01994-09-07 14:32:49 +0000121 PyErr_SetNone(PyExc_KeyboardInterrupt);
Barry Warsaw92971171997-01-03 00:14:25 +0000122 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000123}
124
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000125static char default_int_handler_doc[] =
126"default_int_handler(...)\n\
127\n\
128The default handler for SIGINT instated by Python.\n\
129It raises KeyboardInterrupt.";
130
Barry Warsaw92971171997-01-03 00:14:25 +0000131
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000132static RETSIGTYPE
Barry Warsaw92971171997-01-03 00:14:25 +0000133signal_handler(sig_num)
Guido van Rossume4485b01994-09-07 14:32:49 +0000134 int sig_num;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000135{
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000136#ifdef WITH_THREAD
137 /* See NOTES section above */
138 if (getpid() == main_pid) {
139#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000140 is_tripped++;
141 Handlers[sig_num].tripped = 1;
Guido van Rossum644a12b1997-04-09 19:24:53 +0000142 Py_AddPendingCall(
143 (int (*) Py_PROTO((ANY *)))PyErr_CheckSignals, NULL);
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000144#ifdef WITH_THREAD
145 }
146#endif
Guido van Rossum602099a1994-09-14 13:32:22 +0000147#ifdef SIGCHLD
148 if (sig_num == SIGCHLD) {
149 /* To avoid infinite recursion, this signal remains
150 reset until explicit re-instated.
151 Don't clear the 'func' field as it is our pointer
152 to the Python handler... */
153 return;
154 }
155#endif
Guido van Rossum1b236761998-09-21 14:46:00 +0000156#ifdef HAVE_SIGINTERRUPT
157 siginterrupt(sig_num, 1);
158#endif
Guido van Rossuma5e54d01998-05-01 18:58:59 +0000159 (void)signal(sig_num, &signal_handler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000160}
Guido van Rossume4485b01994-09-07 14:32:49 +0000161
Guido van Rossum06d511d1995-03-10 15:13:48 +0000162
Barry Warsaw92971171997-01-03 00:14:25 +0000163
Guido van Rossum1171ee61997-08-22 20:42:00 +0000164#ifdef HAVE_ALARM
Guido van Rossume4485b01994-09-07 14:32:49 +0000165static PyObject *
Barry Warsaw92971171997-01-03 00:14:25 +0000166signal_alarm(self, args)
Guido van Rossume4485b01994-09-07 14:32:49 +0000167 PyObject *self; /* Not used */
168 PyObject *args;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000169{
170 int t;
Guido van Rossume4485b01994-09-07 14:32:49 +0000171 if (!PyArg_Parse(args, "i", &t))
Barry Warsaw92971171997-01-03 00:14:25 +0000172 return NULL;
Guido van Rossume4485b01994-09-07 14:32:49 +0000173 /* alarm() returns the number of seconds remaining */
174 return PyInt_FromLong(alarm(t));
Guido van Rossumaa0f4c71994-08-23 13:49:37 +0000175}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000176
177static char alarm_doc[] =
178"alarm(seconds)\n\
179\n\
Guido van Rossum911ec181998-06-28 17:00:19 +0000180Arrange for SIGALRM to arrive after the given number of seconds.";
Guido van Rossum06d511d1995-03-10 15:13:48 +0000181#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000182
Guido van Rossum1171ee61997-08-22 20:42:00 +0000183#ifdef HAVE_PAUSE
Guido van Rossuma597dde1995-01-10 20:56:29 +0000184static PyObject *
Barry Warsaw92971171997-01-03 00:14:25 +0000185signal_pause(self, args)
Guido van Rossume4485b01994-09-07 14:32:49 +0000186 PyObject *self; /* Not used */
187 PyObject *args;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000188{
Guido van Rossume4485b01994-09-07 14:32:49 +0000189 if (!PyArg_NoArgs(args))
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000190 return NULL;
Barry Warsaw92971171997-01-03 00:14:25 +0000191
Guido van Rossuma597dde1995-01-10 20:56:29 +0000192 Py_BEGIN_ALLOW_THREADS
Barry Warsaw92971171997-01-03 00:14:25 +0000193 (void)pause();
Guido van Rossuma597dde1995-01-10 20:56:29 +0000194 Py_END_ALLOW_THREADS
Barry Warsaw92971171997-01-03 00:14:25 +0000195 /* make sure that any exceptions that got raised are propagated
196 * back into Python
197 */
198 if (PyErr_CheckSignals())
199 return NULL;
200
Guido van Rossume4485b01994-09-07 14:32:49 +0000201 Py_INCREF(Py_None);
202 return Py_None;
203}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000204static char pause_doc[] =
Barry Warsaw1ee36ff1998-07-21 22:41:18 +0000205"pause()\n\
206\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000207Wait until a signal arrives.";
208
Guido van Rossum06d511d1995-03-10 15:13:48 +0000209#endif
Guido van Rossume4485b01994-09-07 14:32:49 +0000210
Barry Warsaw92971171997-01-03 00:14:25 +0000211
Guido van Rossume4485b01994-09-07 14:32:49 +0000212static PyObject *
Barry Warsaw92971171997-01-03 00:14:25 +0000213signal_signal(self, args)
Guido van Rossume4485b01994-09-07 14:32:49 +0000214 PyObject *self; /* Not used */
215 PyObject *args;
216{
217 PyObject *obj;
218 int sig_num;
219 PyObject *old_handler;
220 RETSIGTYPE (*func)();
221 if (!PyArg_Parse(args, "(iO)", &sig_num, &obj))
Barry Warsaw92971171997-01-03 00:14:25 +0000222 return NULL;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000223#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000224 if (PyThread_get_thread_ident() != main_thread) {
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000225 PyErr_SetString(PyExc_ValueError,
226 "signal only works in main thread");
Barry Warsaw92971171997-01-03 00:14:25 +0000227 return NULL;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000228 }
229#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000230 if (sig_num < 1 || sig_num >= NSIG) {
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000231 PyErr_SetString(PyExc_ValueError,
232 "signal number out of range");
Barry Warsaw92971171997-01-03 00:14:25 +0000233 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000234 }
Barry Warsaw92971171997-01-03 00:14:25 +0000235 if (obj == IgnoreHandler)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000236 func = SIG_IGN;
Barry Warsaw92971171997-01-03 00:14:25 +0000237 else if (obj == DefaultHandler)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000238 func = SIG_DFL;
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000239 else if (!PyCallable_Check(obj)) {
Guido van Rossume4485b01994-09-07 14:32:49 +0000240 PyErr_SetString(PyExc_TypeError,
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000241"signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object");
Barry Warsaw92971171997-01-03 00:14:25 +0000242 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000243 }
244 else
Barry Warsaw92971171997-01-03 00:14:25 +0000245 func = signal_handler;
Guido van Rossum1b236761998-09-21 14:46:00 +0000246#ifdef HAVE_SIGINTERRUPT
247 siginterrupt(sig_num, 1);
248#endif
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000249 if (signal(sig_num, func) == SIG_ERR) {
Guido van Rossume4485b01994-09-07 14:32:49 +0000250 PyErr_SetFromErrno(PyExc_RuntimeError);
Barry Warsaw92971171997-01-03 00:14:25 +0000251 return NULL;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000252 }
Barry Warsaw92971171997-01-03 00:14:25 +0000253 old_handler = Handlers[sig_num].func;
254 Handlers[sig_num].tripped = 0;
Guido van Rossume4485b01994-09-07 14:32:49 +0000255 Py_INCREF(obj);
Barry Warsaw92971171997-01-03 00:14:25 +0000256 Handlers[sig_num].func = obj;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000257 return old_handler;
258}
259
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000260static char signal_doc[] =
261"signal(sig, action) -> action\n\
262\n\
263Set the action for the given signal. The action can be SIG_DFL,\n\
264SIG_IGN, or a callable Python object. The previous action is\n\
265returned. See getsignal() for possible return values.\n\
266\n\
267*** IMPORTANT NOTICE ***\n\
268A signal handler function is called with two arguments:\n\
269the first is the signal number, the second is the interrupted stack frame.";
270
Barry Warsaw92971171997-01-03 00:14:25 +0000271
Guido van Rossume4485b01994-09-07 14:32:49 +0000272static PyObject *
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000273signal_getsignal(self, args)
Guido van Rossume4485b01994-09-07 14:32:49 +0000274 PyObject *self; /* Not used */
275 PyObject *args;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000276{
277 int sig_num;
Guido van Rossume4485b01994-09-07 14:32:49 +0000278 PyObject *old_handler;
279 if (!PyArg_Parse(args, "i", &sig_num))
Barry Warsaw92971171997-01-03 00:14:25 +0000280 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000281 if (sig_num < 1 || sig_num >= NSIG) {
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000282 PyErr_SetString(PyExc_ValueError,
283 "signal number out of range");
Barry Warsaw92971171997-01-03 00:14:25 +0000284 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000285 }
Barry Warsaw92971171997-01-03 00:14:25 +0000286 old_handler = Handlers[sig_num].func;
Guido van Rossume4485b01994-09-07 14:32:49 +0000287 Py_INCREF(old_handler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000288 return old_handler;
289}
290
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000291static char getsignal_doc[] =
292"getsignal(sig) -> action\n\
293\n\
294Return the current action for the given signal. The return value can be:\n\
295SIG_IGN -- if the signal is being ignored\n\
296SIG_DFL -- if the default action for the signal is in effect\n\
297None -- if an unknown handler is in effect\n\
298anything else -- the callable Python object used as a handler\n\
299";
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000300
Barry Warsaw92971171997-01-03 00:14:25 +0000301
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000302/* List of functions defined in the module */
Barry Warsaw92971171997-01-03 00:14:25 +0000303static PyMethodDef signal_methods[] = {
Guido van Rossum1171ee61997-08-22 20:42:00 +0000304#ifdef HAVE_ALARM
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000305 {"alarm", signal_alarm, 0, alarm_doc},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000306#endif
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000307 {"signal", signal_signal, 0, signal_doc},
308 {"getsignal", signal_getsignal, 0, getsignal_doc},
Guido van Rossum1171ee61997-08-22 20:42:00 +0000309#ifdef HAVE_PAUSE
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000310 {"pause", signal_pause, 0, pause_doc},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000311#endif
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000312 {"default_int_handler", signal_default_int_handler, 0,
313 default_int_handler_doc},
314 {NULL, NULL} /* sentinel */
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000315};
316
Barry Warsaw92971171997-01-03 00:14:25 +0000317
318
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000319static char module_doc[] =
320"This module provides mechanisms to use signal handlers in Python.\n\
321\n\
322Functions:\n\
323\n\
324alarm() -- cause SIGALRM after a specified time [Unix only]\n\
325signal() -- set the action for a given signal\n\
326getsignal() -- get the signal action for a given signal\n\
327pause() -- wait until a signal arrives [Unix only]\n\
328default_int_handler() -- default SIGINT handler\n\
329\n\
330Constants:\n\
331\n\
332SIG_DFL -- used to refer to the system default handler\n\
333SIG_IGN -- used to ignore the signal\n\
334NSIG -- number of defined signals\n\
335\n\
336SIGINT, SIGTERM, etc. -- signal numbers\n\
337\n\
338*** IMPORTANT NOTICE ***\n\
339A signal handler function is called with two arguments:\n\
340the first is the signal number, the second is the interrupted stack frame.";
341
Guido van Rossum3886bb61998-12-04 18:50:17 +0000342DL_EXPORT(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000343initsignal()
344{
Guido van Rossume4485b01994-09-07 14:32:49 +0000345 PyObject *m, *d, *x;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000346 int i;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000347
348#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000349 main_thread = PyThread_get_thread_ident();
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000350 main_pid = getpid();
351#endif
352
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000353 /* Create the module and add the functions */
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000354 m = Py_InitModule3("signal", signal_methods, module_doc);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000355
356 /* Add some symbolic constants to the module */
Guido van Rossume4485b01994-09-07 14:32:49 +0000357 d = PyModule_GetDict(m);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000358
Barry Warsaw92971171997-01-03 00:14:25 +0000359 x = DefaultHandler = PyInt_FromLong((long)SIG_DFL);
360 if (!x || PyDict_SetItemString(d, "SIG_DFL", x) < 0)
361 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000362
Barry Warsaw92971171997-01-03 00:14:25 +0000363 x = IgnoreHandler = PyInt_FromLong((long)SIG_IGN);
364 if (!x || PyDict_SetItemString(d, "SIG_IGN", x) < 0)
365 goto finally;
366
367 x = PyInt_FromLong((long)NSIG);
368 if (!x || PyDict_SetItemString(d, "NSIG", x) < 0)
369 goto finally;
Guido van Rossum08c16611997-08-02 03:01:42 +0000370 Py_DECREF(x);
Barry Warsaw92971171997-01-03 00:14:25 +0000371
372 x = IntHandler = PyDict_GetItemString(d, "default_int_handler");
373 if (!x)
374 goto finally;
Guido van Rossum08c16611997-08-02 03:01:42 +0000375 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +0000376
377 Handlers[0].tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000378 for (i = 1; i < NSIG; i++) {
Guido van Rossum6299d1e1994-05-31 12:51:13 +0000379 RETSIGTYPE (*t)();
Guido van Rossumfb0df941996-05-23 22:55:35 +0000380#ifdef HAVE_SIGACTION
381 struct sigaction act;
382 sigaction(i, 0, &act);
Guido van Rossum1835c4f1996-05-29 14:15:19 +0000383 t = act.sa_handler;
Guido van Rossumfb0df941996-05-23 22:55:35 +0000384#else
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000385 t = signal(i, SIG_IGN);
386 signal(i, t);
Guido van Rossumfb0df941996-05-23 22:55:35 +0000387#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000388 Handlers[i].tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000389 if (t == SIG_DFL)
Barry Warsaw92971171997-01-03 00:14:25 +0000390 Handlers[i].func = DefaultHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000391 else if (t == SIG_IGN)
Barry Warsaw92971171997-01-03 00:14:25 +0000392 Handlers[i].func = IgnoreHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000393 else
Barry Warsaw92971171997-01-03 00:14:25 +0000394 Handlers[i].func = Py_None; /* None of our business */
395 Py_INCREF(Handlers[i].func);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000396 }
Barry Warsaw92971171997-01-03 00:14:25 +0000397 if (Handlers[SIGINT].func == DefaultHandler) {
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000398 /* Install default int handler */
Guido van Rossum08c16611997-08-02 03:01:42 +0000399 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +0000400 Py_DECREF(Handlers[SIGINT].func);
401 Handlers[SIGINT].func = IntHandler;
Guido van Rossum08c16611997-08-02 03:01:42 +0000402 old_siginthandler = signal(SIGINT, &signal_handler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000403 }
404
405#ifdef SIGHUP
Guido van Rossume4485b01994-09-07 14:32:49 +0000406 x = PyInt_FromLong(SIGHUP);
407 PyDict_SetItemString(d, "SIGHUP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000408 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000409#endif
410#ifdef SIGINT
Guido van Rossume4485b01994-09-07 14:32:49 +0000411 x = PyInt_FromLong(SIGINT);
412 PyDict_SetItemString(d, "SIGINT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000413 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000414#endif
415#ifdef SIGQUIT
Guido van Rossume4485b01994-09-07 14:32:49 +0000416 x = PyInt_FromLong(SIGQUIT);
417 PyDict_SetItemString(d, "SIGQUIT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000418 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000419#endif
420#ifdef SIGILL
Guido van Rossume4485b01994-09-07 14:32:49 +0000421 x = PyInt_FromLong(SIGILL);
422 PyDict_SetItemString(d, "SIGILL", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000423 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000424#endif
425#ifdef SIGTRAP
Guido van Rossume4485b01994-09-07 14:32:49 +0000426 x = PyInt_FromLong(SIGTRAP);
427 PyDict_SetItemString(d, "SIGTRAP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000428 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000429#endif
430#ifdef SIGIOT
Guido van Rossume4485b01994-09-07 14:32:49 +0000431 x = PyInt_FromLong(SIGIOT);
432 PyDict_SetItemString(d, "SIGIOT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000433 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000434#endif
435#ifdef SIGABRT
Guido van Rossume4485b01994-09-07 14:32:49 +0000436 x = PyInt_FromLong(SIGABRT);
437 PyDict_SetItemString(d, "SIGABRT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000438 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000439#endif
440#ifdef SIGEMT
Guido van Rossume4485b01994-09-07 14:32:49 +0000441 x = PyInt_FromLong(SIGEMT);
442 PyDict_SetItemString(d, "SIGEMT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000443 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000444#endif
445#ifdef SIGFPE
Guido van Rossume4485b01994-09-07 14:32:49 +0000446 x = PyInt_FromLong(SIGFPE);
447 PyDict_SetItemString(d, "SIGFPE", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000448 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000449#endif
450#ifdef SIGKILL
Guido van Rossume4485b01994-09-07 14:32:49 +0000451 x = PyInt_FromLong(SIGKILL);
452 PyDict_SetItemString(d, "SIGKILL", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000453 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000454#endif
455#ifdef SIGBUS
Guido van Rossume4485b01994-09-07 14:32:49 +0000456 x = PyInt_FromLong(SIGBUS);
457 PyDict_SetItemString(d, "SIGBUS", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000458 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000459#endif
460#ifdef SIGSEGV
Guido van Rossume4485b01994-09-07 14:32:49 +0000461 x = PyInt_FromLong(SIGSEGV);
462 PyDict_SetItemString(d, "SIGSEGV", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000463 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000464#endif
465#ifdef SIGSYS
Guido van Rossume4485b01994-09-07 14:32:49 +0000466 x = PyInt_FromLong(SIGSYS);
467 PyDict_SetItemString(d, "SIGSYS", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000468 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000469#endif
470#ifdef SIGPIPE
Guido van Rossume4485b01994-09-07 14:32:49 +0000471 x = PyInt_FromLong(SIGPIPE);
472 PyDict_SetItemString(d, "SIGPIPE", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000473 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000474#endif
475#ifdef SIGALRM
Guido van Rossume4485b01994-09-07 14:32:49 +0000476 x = PyInt_FromLong(SIGALRM);
477 PyDict_SetItemString(d, "SIGALRM", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000478 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000479#endif
480#ifdef SIGTERM
Guido van Rossume4485b01994-09-07 14:32:49 +0000481 x = PyInt_FromLong(SIGTERM);
482 PyDict_SetItemString(d, "SIGTERM", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000483 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000484#endif
485#ifdef SIGUSR1
Guido van Rossume4485b01994-09-07 14:32:49 +0000486 x = PyInt_FromLong(SIGUSR1);
487 PyDict_SetItemString(d, "SIGUSR1", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000488 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000489#endif
490#ifdef SIGUSR2
Guido van Rossume4485b01994-09-07 14:32:49 +0000491 x = PyInt_FromLong(SIGUSR2);
492 PyDict_SetItemString(d, "SIGUSR2", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000493 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000494#endif
495#ifdef SIGCLD
Guido van Rossume4485b01994-09-07 14:32:49 +0000496 x = PyInt_FromLong(SIGCLD);
497 PyDict_SetItemString(d, "SIGCLD", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000498 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000499#endif
500#ifdef SIGCHLD
Guido van Rossume4485b01994-09-07 14:32:49 +0000501 x = PyInt_FromLong(SIGCHLD);
502 PyDict_SetItemString(d, "SIGCHLD", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000503 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000504#endif
505#ifdef SIGPWR
Guido van Rossume4485b01994-09-07 14:32:49 +0000506 x = PyInt_FromLong(SIGPWR);
507 PyDict_SetItemString(d, "SIGPWR", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000508 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000509#endif
510#ifdef SIGIO
Guido van Rossume4485b01994-09-07 14:32:49 +0000511 x = PyInt_FromLong(SIGIO);
512 PyDict_SetItemString(d, "SIGIO", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000513 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000514#endif
515#ifdef SIGURG
Guido van Rossume4485b01994-09-07 14:32:49 +0000516 x = PyInt_FromLong(SIGURG);
517 PyDict_SetItemString(d, "SIGURG", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000518 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000519#endif
520#ifdef SIGWINCH
Guido van Rossume4485b01994-09-07 14:32:49 +0000521 x = PyInt_FromLong(SIGWINCH);
522 PyDict_SetItemString(d, "SIGWINCH", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000523 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000524#endif
525#ifdef SIGPOLL
Guido van Rossume4485b01994-09-07 14:32:49 +0000526 x = PyInt_FromLong(SIGPOLL);
527 PyDict_SetItemString(d, "SIGPOLL", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000528 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000529#endif
530#ifdef SIGSTOP
Guido van Rossume4485b01994-09-07 14:32:49 +0000531 x = PyInt_FromLong(SIGSTOP);
532 PyDict_SetItemString(d, "SIGSTOP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000533 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000534#endif
535#ifdef SIGTSTP
Guido van Rossume4485b01994-09-07 14:32:49 +0000536 x = PyInt_FromLong(SIGTSTP);
537 PyDict_SetItemString(d, "SIGTSTP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000538 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000539#endif
540#ifdef SIGCONT
Guido van Rossume4485b01994-09-07 14:32:49 +0000541 x = PyInt_FromLong(SIGCONT);
542 PyDict_SetItemString(d, "SIGCONT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000543 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000544#endif
545#ifdef SIGTTIN
Guido van Rossume4485b01994-09-07 14:32:49 +0000546 x = PyInt_FromLong(SIGTTIN);
547 PyDict_SetItemString(d, "SIGTTIN", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000548 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000549#endif
550#ifdef SIGTTOU
Guido van Rossume4485b01994-09-07 14:32:49 +0000551 x = PyInt_FromLong(SIGTTOU);
552 PyDict_SetItemString(d, "SIGTTOU", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000553 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000554#endif
555#ifdef SIGVTALRM
Guido van Rossume4485b01994-09-07 14:32:49 +0000556 x = PyInt_FromLong(SIGVTALRM);
557 PyDict_SetItemString(d, "SIGVTALRM", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000558 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000559#endif
560#ifdef SIGPROF
Guido van Rossume4485b01994-09-07 14:32:49 +0000561 x = PyInt_FromLong(SIGPROF);
562 PyDict_SetItemString(d, "SIGPROF", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000563 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000564#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000565#ifdef SIGXCPU
566 x = PyInt_FromLong(SIGXCPU);
567 PyDict_SetItemString(d, "SIGXCPU", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000568 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000569#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000570#ifdef SIGXFSZ
571 x = PyInt_FromLong(SIGXFSZ);
572 PyDict_SetItemString(d, "SIGXFSZ", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000573 Py_XDECREF(x);
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000574#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000575 if (!PyErr_Occurred())
576 return;
577
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000578 /* Check for errors */
Barry Warsaw92971171997-01-03 00:14:25 +0000579 finally:
Guido van Rossum08c16611997-08-02 03:01:42 +0000580 return;
581}
582
583static void
584finisignal()
585{
586 int i;
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000587 PyObject *func;
Guido van Rossum08c16611997-08-02 03:01:42 +0000588
589 signal(SIGINT, old_siginthandler);
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000590 old_siginthandler = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +0000591
592 for (i = 1; i < NSIG; i++) {
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000593 func = Handlers[i].func;
Guido van Rossum08c16611997-08-02 03:01:42 +0000594 Handlers[i].tripped = 0;
Guido van Rossum08c16611997-08-02 03:01:42 +0000595 Handlers[i].func = NULL;
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000596 if (i != SIGINT && func != NULL && func != Py_None &&
597 func != DefaultHandler && func != IgnoreHandler)
598 signal(i, SIG_DFL);
599 Py_XDECREF(func);
Guido van Rossum08c16611997-08-02 03:01:42 +0000600 }
601
602 Py_XDECREF(IntHandler);
603 IntHandler = NULL;
604 Py_XDECREF(DefaultHandler);
605 DefaultHandler = NULL;
606 Py_XDECREF(IgnoreHandler);
607 IgnoreHandler = NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000608}
609
Barry Warsaw92971171997-01-03 00:14:25 +0000610
611
612/* Declared in pyerrors.h */
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000613int
Guido van Rossumec25b911995-01-22 00:46:57 +0000614PyErr_CheckSignals()
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000615{
616 int i;
Guido van Rossume4485b01994-09-07 14:32:49 +0000617 PyObject *f;
Barry Warsaw92971171997-01-03 00:14:25 +0000618
619 if (!is_tripped)
Guido van Rossum6299d1e1994-05-31 12:51:13 +0000620 return 0;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000621#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000622 if (PyThread_get_thread_ident() != main_thread)
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000623 return 0;
624#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000625 if (!(f = PyEval_GetFrame()))
Guido van Rossume4485b01994-09-07 14:32:49 +0000626 f = Py_None;
Barry Warsaw92971171997-01-03 00:14:25 +0000627
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000628 for (i = 1; i < NSIG; i++) {
Barry Warsaw92971171997-01-03 00:14:25 +0000629 if (Handlers[i].tripped) {
630 PyObject *result = NULL;
631 PyObject *arglist = Py_BuildValue("(iO)", i, f);
632 Handlers[i].tripped = 0;
633
634 if (arglist) {
635 result = PyEval_CallObject(Handlers[i].func,
636 arglist);
Guido van Rossume4485b01994-09-07 14:32:49 +0000637 Py_DECREF(arglist);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000638 }
Barry Warsaw92971171997-01-03 00:14:25 +0000639 if (!result)
Guido van Rossumfcdd0e41997-01-21 06:13:09 +0000640 return -1;
Barry Warsaw92971171997-01-03 00:14:25 +0000641
642 Py_DECREF(result);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000643 }
644 }
Barry Warsaw92971171997-01-03 00:14:25 +0000645 is_tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000646 return 0;
647}
648
Barry Warsaw92971171997-01-03 00:14:25 +0000649
650/* Replacements for intrcheck.c functionality
651 * Declared in pyerrors.h
652 */
653void
654PyErr_SetInterrupt()
655{
656 is_tripped++;
657 Handlers[SIGINT].tripped = 1;
Guido van Rossumfcdd0e41997-01-21 06:13:09 +0000658 Py_AddPendingCall((int (*) Py_PROTO((ANY *)))PyErr_CheckSignals, NULL);
Barry Warsaw92971171997-01-03 00:14:25 +0000659}
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000660
661void
Barry Warsaw92971171997-01-03 00:14:25 +0000662PyOS_InitInterrupts()
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000663{
664 initsignal();
Guido van Rossum08c16611997-08-02 03:01:42 +0000665 _PyImport_FixupExtension("signal", "signal");
666}
667
668void
669PyOS_FiniInterrupts()
670{
671 finisignal();
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000672}
673
674int
Barry Warsaw92971171997-01-03 00:14:25 +0000675PyOS_InterruptOccurred()
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000676{
Barry Warsaw92971171997-01-03 00:14:25 +0000677 if (Handlers[SIGINT].tripped) {
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000678#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000679 if (PyThread_get_thread_ident() != main_thread)
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000680 return 0;
681#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000682 Handlers[SIGINT].tripped = 0;
Guido van Rossum6299d1e1994-05-31 12:51:13 +0000683 return 1;
684 }
685 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000686}
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000687
688void
689PyOS_AfterFork()
690{
691#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000692 main_thread = PyThread_get_thread_ident();
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000693 main_pid = getpid();
694#endif
695}