blob: 7ebad93efe64fca1e678fb8240a8fcf5bc59a325 [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 Rossumbb4ba121994-06-23 11:25:45 +000096#include "thread.h"
97static 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 Rossuma5e54d01998-05-01 18:58:59 +0000156 (void)signal(sig_num, &signal_handler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000157}
Guido van Rossume4485b01994-09-07 14:32:49 +0000158
Guido van Rossum06d511d1995-03-10 15:13:48 +0000159
Barry Warsaw92971171997-01-03 00:14:25 +0000160
Guido van Rossum1171ee61997-08-22 20:42:00 +0000161#ifdef HAVE_ALARM
Guido van Rossume4485b01994-09-07 14:32:49 +0000162static PyObject *
Barry Warsaw92971171997-01-03 00:14:25 +0000163signal_alarm(self, args)
Guido van Rossume4485b01994-09-07 14:32:49 +0000164 PyObject *self; /* Not used */
165 PyObject *args;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000166{
167 int t;
Guido van Rossume4485b01994-09-07 14:32:49 +0000168 if (!PyArg_Parse(args, "i", &t))
Barry Warsaw92971171997-01-03 00:14:25 +0000169 return NULL;
Guido van Rossume4485b01994-09-07 14:32:49 +0000170 /* alarm() returns the number of seconds remaining */
171 return PyInt_FromLong(alarm(t));
Guido van Rossumaa0f4c71994-08-23 13:49:37 +0000172}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000173
174static char alarm_doc[] =
175"alarm(seconds)\n\
176\n\
Guido van Rossum911ec181998-06-28 17:00:19 +0000177Arrange for SIGALRM to arrive after the given number of seconds.";
Guido van Rossum06d511d1995-03-10 15:13:48 +0000178#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000179
Guido van Rossum1171ee61997-08-22 20:42:00 +0000180#ifdef HAVE_PAUSE
Guido van Rossuma597dde1995-01-10 20:56:29 +0000181static PyObject *
Barry Warsaw92971171997-01-03 00:14:25 +0000182signal_pause(self, args)
Guido van Rossume4485b01994-09-07 14:32:49 +0000183 PyObject *self; /* Not used */
184 PyObject *args;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000185{
Guido van Rossume4485b01994-09-07 14:32:49 +0000186 if (!PyArg_NoArgs(args))
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000187 return NULL;
Barry Warsaw92971171997-01-03 00:14:25 +0000188
Guido van Rossuma597dde1995-01-10 20:56:29 +0000189 Py_BEGIN_ALLOW_THREADS
Barry Warsaw92971171997-01-03 00:14:25 +0000190 (void)pause();
Guido van Rossuma597dde1995-01-10 20:56:29 +0000191 Py_END_ALLOW_THREADS
Barry Warsaw92971171997-01-03 00:14:25 +0000192 /* make sure that any exceptions that got raised are propagated
193 * back into Python
194 */
195 if (PyErr_CheckSignals())
196 return NULL;
197
Guido van Rossume4485b01994-09-07 14:32:49 +0000198 Py_INCREF(Py_None);
199 return Py_None;
200}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000201static char pause_doc[] =
Barry Warsaw1ee36ff1998-07-21 22:41:18 +0000202"pause()\n\
203\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000204Wait until a signal arrives.";
205
Guido van Rossum06d511d1995-03-10 15:13:48 +0000206#endif
Guido van Rossume4485b01994-09-07 14:32:49 +0000207
Barry Warsaw92971171997-01-03 00:14:25 +0000208
Guido van Rossume4485b01994-09-07 14:32:49 +0000209static PyObject *
Barry Warsaw92971171997-01-03 00:14:25 +0000210signal_signal(self, args)
Guido van Rossume4485b01994-09-07 14:32:49 +0000211 PyObject *self; /* Not used */
212 PyObject *args;
213{
214 PyObject *obj;
215 int sig_num;
216 PyObject *old_handler;
217 RETSIGTYPE (*func)();
218 if (!PyArg_Parse(args, "(iO)", &sig_num, &obj))
Barry Warsaw92971171997-01-03 00:14:25 +0000219 return NULL;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000220#ifdef WITH_THREAD
221 if (get_thread_ident() != main_thread) {
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000222 PyErr_SetString(PyExc_ValueError,
223 "signal only works in main thread");
Barry Warsaw92971171997-01-03 00:14:25 +0000224 return NULL;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000225 }
226#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000227 if (sig_num < 1 || sig_num >= NSIG) {
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000228 PyErr_SetString(PyExc_ValueError,
229 "signal number out of range");
Barry Warsaw92971171997-01-03 00:14:25 +0000230 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000231 }
Barry Warsaw92971171997-01-03 00:14:25 +0000232 if (obj == IgnoreHandler)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000233 func = SIG_IGN;
Barry Warsaw92971171997-01-03 00:14:25 +0000234 else if (obj == DefaultHandler)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000235 func = SIG_DFL;
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000236 else if (!PyCallable_Check(obj)) {
Guido van Rossume4485b01994-09-07 14:32:49 +0000237 PyErr_SetString(PyExc_TypeError,
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000238"signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object");
Barry Warsaw92971171997-01-03 00:14:25 +0000239 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000240 }
241 else
Barry Warsaw92971171997-01-03 00:14:25 +0000242 func = signal_handler;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000243 if (signal(sig_num, func) == SIG_ERR) {
Guido van Rossume4485b01994-09-07 14:32:49 +0000244 PyErr_SetFromErrno(PyExc_RuntimeError);
Barry Warsaw92971171997-01-03 00:14:25 +0000245 return NULL;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000246 }
Barry Warsaw92971171997-01-03 00:14:25 +0000247 old_handler = Handlers[sig_num].func;
248 Handlers[sig_num].tripped = 0;
Guido van Rossume4485b01994-09-07 14:32:49 +0000249 Py_INCREF(obj);
Barry Warsaw92971171997-01-03 00:14:25 +0000250 Handlers[sig_num].func = obj;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000251 return old_handler;
252}
253
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000254static char signal_doc[] =
255"signal(sig, action) -> action\n\
256\n\
257Set the action for the given signal. The action can be SIG_DFL,\n\
258SIG_IGN, or a callable Python object. The previous action is\n\
259returned. See getsignal() for possible return values.\n\
260\n\
261*** IMPORTANT NOTICE ***\n\
262A signal handler function is called with two arguments:\n\
263the first is the signal number, the second is the interrupted stack frame.";
264
Barry Warsaw92971171997-01-03 00:14:25 +0000265
Guido van Rossume4485b01994-09-07 14:32:49 +0000266static PyObject *
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000267signal_getsignal(self, args)
Guido van Rossume4485b01994-09-07 14:32:49 +0000268 PyObject *self; /* Not used */
269 PyObject *args;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000270{
271 int sig_num;
Guido van Rossume4485b01994-09-07 14:32:49 +0000272 PyObject *old_handler;
273 if (!PyArg_Parse(args, "i", &sig_num))
Barry Warsaw92971171997-01-03 00:14:25 +0000274 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000275 if (sig_num < 1 || sig_num >= NSIG) {
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000276 PyErr_SetString(PyExc_ValueError,
277 "signal number out of range");
Barry Warsaw92971171997-01-03 00:14:25 +0000278 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000279 }
Barry Warsaw92971171997-01-03 00:14:25 +0000280 old_handler = Handlers[sig_num].func;
Guido van Rossume4485b01994-09-07 14:32:49 +0000281 Py_INCREF(old_handler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000282 return old_handler;
283}
284
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000285static char getsignal_doc[] =
286"getsignal(sig) -> action\n\
287\n\
288Return the current action for the given signal. The return value can be:\n\
289SIG_IGN -- if the signal is being ignored\n\
290SIG_DFL -- if the default action for the signal is in effect\n\
291None -- if an unknown handler is in effect\n\
292anything else -- the callable Python object used as a handler\n\
293";
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000294
Barry Warsaw92971171997-01-03 00:14:25 +0000295
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000296/* List of functions defined in the module */
Barry Warsaw92971171997-01-03 00:14:25 +0000297static PyMethodDef signal_methods[] = {
Guido van Rossum1171ee61997-08-22 20:42:00 +0000298#ifdef HAVE_ALARM
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000299 {"alarm", signal_alarm, 0, alarm_doc},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000300#endif
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000301 {"signal", signal_signal, 0, signal_doc},
302 {"getsignal", signal_getsignal, 0, getsignal_doc},
Guido van Rossum1171ee61997-08-22 20:42:00 +0000303#ifdef HAVE_PAUSE
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000304 {"pause", signal_pause, 0, pause_doc},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000305#endif
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000306 {"default_int_handler", signal_default_int_handler, 0,
307 default_int_handler_doc},
308 {NULL, NULL} /* sentinel */
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000309};
310
Barry Warsaw92971171997-01-03 00:14:25 +0000311
312
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000313static char module_doc[] =
314"This module provides mechanisms to use signal handlers in Python.\n\
315\n\
316Functions:\n\
317\n\
318alarm() -- cause SIGALRM after a specified time [Unix only]\n\
319signal() -- set the action for a given signal\n\
320getsignal() -- get the signal action for a given signal\n\
321pause() -- wait until a signal arrives [Unix only]\n\
322default_int_handler() -- default SIGINT handler\n\
323\n\
324Constants:\n\
325\n\
326SIG_DFL -- used to refer to the system default handler\n\
327SIG_IGN -- used to ignore the signal\n\
328NSIG -- number of defined signals\n\
329\n\
330SIGINT, SIGTERM, etc. -- signal numbers\n\
331\n\
332*** IMPORTANT NOTICE ***\n\
333A signal handler function is called with two arguments:\n\
334the first is the signal number, the second is the interrupted stack frame.";
335
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000336void
337initsignal()
338{
Guido van Rossume4485b01994-09-07 14:32:49 +0000339 PyObject *m, *d, *x;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000340 int i;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000341
342#ifdef WITH_THREAD
343 main_thread = get_thread_ident();
344 main_pid = getpid();
345#endif
346
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000347 /* Create the module and add the functions */
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000348 m = Py_InitModule3("signal", signal_methods, module_doc);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000349
350 /* Add some symbolic constants to the module */
Guido van Rossume4485b01994-09-07 14:32:49 +0000351 d = PyModule_GetDict(m);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000352
Barry Warsaw92971171997-01-03 00:14:25 +0000353 x = DefaultHandler = PyInt_FromLong((long)SIG_DFL);
354 if (!x || PyDict_SetItemString(d, "SIG_DFL", x) < 0)
355 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000356
Barry Warsaw92971171997-01-03 00:14:25 +0000357 x = IgnoreHandler = PyInt_FromLong((long)SIG_IGN);
358 if (!x || PyDict_SetItemString(d, "SIG_IGN", x) < 0)
359 goto finally;
360
361 x = PyInt_FromLong((long)NSIG);
362 if (!x || PyDict_SetItemString(d, "NSIG", x) < 0)
363 goto finally;
Guido van Rossum08c16611997-08-02 03:01:42 +0000364 Py_DECREF(x);
Barry Warsaw92971171997-01-03 00:14:25 +0000365
366 x = IntHandler = PyDict_GetItemString(d, "default_int_handler");
367 if (!x)
368 goto finally;
Guido van Rossum08c16611997-08-02 03:01:42 +0000369 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +0000370
371 Handlers[0].tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000372 for (i = 1; i < NSIG; i++) {
Guido van Rossum6299d1e1994-05-31 12:51:13 +0000373 RETSIGTYPE (*t)();
Guido van Rossumfb0df941996-05-23 22:55:35 +0000374#ifdef HAVE_SIGACTION
375 struct sigaction act;
376 sigaction(i, 0, &act);
Guido van Rossum1835c4f1996-05-29 14:15:19 +0000377 t = act.sa_handler;
Guido van Rossumfb0df941996-05-23 22:55:35 +0000378#else
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000379 t = signal(i, SIG_IGN);
380 signal(i, t);
Guido van Rossumfb0df941996-05-23 22:55:35 +0000381#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000382 Handlers[i].tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000383 if (t == SIG_DFL)
Barry Warsaw92971171997-01-03 00:14:25 +0000384 Handlers[i].func = DefaultHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000385 else if (t == SIG_IGN)
Barry Warsaw92971171997-01-03 00:14:25 +0000386 Handlers[i].func = IgnoreHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000387 else
Barry Warsaw92971171997-01-03 00:14:25 +0000388 Handlers[i].func = Py_None; /* None of our business */
389 Py_INCREF(Handlers[i].func);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000390 }
Barry Warsaw92971171997-01-03 00:14:25 +0000391 if (Handlers[SIGINT].func == DefaultHandler) {
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000392 /* Install default int handler */
Guido van Rossum08c16611997-08-02 03:01:42 +0000393 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +0000394 Py_DECREF(Handlers[SIGINT].func);
395 Handlers[SIGINT].func = IntHandler;
Guido van Rossum08c16611997-08-02 03:01:42 +0000396 old_siginthandler = signal(SIGINT, &signal_handler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000397 }
398
399#ifdef SIGHUP
Guido van Rossume4485b01994-09-07 14:32:49 +0000400 x = PyInt_FromLong(SIGHUP);
401 PyDict_SetItemString(d, "SIGHUP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000402 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000403#endif
404#ifdef SIGINT
Guido van Rossume4485b01994-09-07 14:32:49 +0000405 x = PyInt_FromLong(SIGINT);
406 PyDict_SetItemString(d, "SIGINT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000407 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000408#endif
409#ifdef SIGQUIT
Guido van Rossume4485b01994-09-07 14:32:49 +0000410 x = PyInt_FromLong(SIGQUIT);
411 PyDict_SetItemString(d, "SIGQUIT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000412 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000413#endif
414#ifdef SIGILL
Guido van Rossume4485b01994-09-07 14:32:49 +0000415 x = PyInt_FromLong(SIGILL);
416 PyDict_SetItemString(d, "SIGILL", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000417 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000418#endif
419#ifdef SIGTRAP
Guido van Rossume4485b01994-09-07 14:32:49 +0000420 x = PyInt_FromLong(SIGTRAP);
421 PyDict_SetItemString(d, "SIGTRAP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000422 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000423#endif
424#ifdef SIGIOT
Guido van Rossume4485b01994-09-07 14:32:49 +0000425 x = PyInt_FromLong(SIGIOT);
426 PyDict_SetItemString(d, "SIGIOT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000427 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000428#endif
429#ifdef SIGABRT
Guido van Rossume4485b01994-09-07 14:32:49 +0000430 x = PyInt_FromLong(SIGABRT);
431 PyDict_SetItemString(d, "SIGABRT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000432 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000433#endif
434#ifdef SIGEMT
Guido van Rossume4485b01994-09-07 14:32:49 +0000435 x = PyInt_FromLong(SIGEMT);
436 PyDict_SetItemString(d, "SIGEMT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000437 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000438#endif
439#ifdef SIGFPE
Guido van Rossume4485b01994-09-07 14:32:49 +0000440 x = PyInt_FromLong(SIGFPE);
441 PyDict_SetItemString(d, "SIGFPE", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000442 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000443#endif
444#ifdef SIGKILL
Guido van Rossume4485b01994-09-07 14:32:49 +0000445 x = PyInt_FromLong(SIGKILL);
446 PyDict_SetItemString(d, "SIGKILL", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000447 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000448#endif
449#ifdef SIGBUS
Guido van Rossume4485b01994-09-07 14:32:49 +0000450 x = PyInt_FromLong(SIGBUS);
451 PyDict_SetItemString(d, "SIGBUS", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000452 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000453#endif
454#ifdef SIGSEGV
Guido van Rossume4485b01994-09-07 14:32:49 +0000455 x = PyInt_FromLong(SIGSEGV);
456 PyDict_SetItemString(d, "SIGSEGV", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000457 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000458#endif
459#ifdef SIGSYS
Guido van Rossume4485b01994-09-07 14:32:49 +0000460 x = PyInt_FromLong(SIGSYS);
461 PyDict_SetItemString(d, "SIGSYS", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000462 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000463#endif
464#ifdef SIGPIPE
Guido van Rossume4485b01994-09-07 14:32:49 +0000465 x = PyInt_FromLong(SIGPIPE);
466 PyDict_SetItemString(d, "SIGPIPE", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000467 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000468#endif
469#ifdef SIGALRM
Guido van Rossume4485b01994-09-07 14:32:49 +0000470 x = PyInt_FromLong(SIGALRM);
471 PyDict_SetItemString(d, "SIGALRM", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000472 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000473#endif
474#ifdef SIGTERM
Guido van Rossume4485b01994-09-07 14:32:49 +0000475 x = PyInt_FromLong(SIGTERM);
476 PyDict_SetItemString(d, "SIGTERM", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000477 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000478#endif
479#ifdef SIGUSR1
Guido van Rossume4485b01994-09-07 14:32:49 +0000480 x = PyInt_FromLong(SIGUSR1);
481 PyDict_SetItemString(d, "SIGUSR1", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000482 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000483#endif
484#ifdef SIGUSR2
Guido van Rossume4485b01994-09-07 14:32:49 +0000485 x = PyInt_FromLong(SIGUSR2);
486 PyDict_SetItemString(d, "SIGUSR2", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000487 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000488#endif
489#ifdef SIGCLD
Guido van Rossume4485b01994-09-07 14:32:49 +0000490 x = PyInt_FromLong(SIGCLD);
491 PyDict_SetItemString(d, "SIGCLD", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000492 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000493#endif
494#ifdef SIGCHLD
Guido van Rossume4485b01994-09-07 14:32:49 +0000495 x = PyInt_FromLong(SIGCHLD);
496 PyDict_SetItemString(d, "SIGCHLD", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000497 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000498#endif
499#ifdef SIGPWR
Guido van Rossume4485b01994-09-07 14:32:49 +0000500 x = PyInt_FromLong(SIGPWR);
501 PyDict_SetItemString(d, "SIGPWR", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000502 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000503#endif
504#ifdef SIGIO
Guido van Rossume4485b01994-09-07 14:32:49 +0000505 x = PyInt_FromLong(SIGIO);
506 PyDict_SetItemString(d, "SIGIO", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000507 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000508#endif
509#ifdef SIGURG
Guido van Rossume4485b01994-09-07 14:32:49 +0000510 x = PyInt_FromLong(SIGURG);
511 PyDict_SetItemString(d, "SIGURG", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000512 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000513#endif
514#ifdef SIGWINCH
Guido van Rossume4485b01994-09-07 14:32:49 +0000515 x = PyInt_FromLong(SIGWINCH);
516 PyDict_SetItemString(d, "SIGWINCH", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000517 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000518#endif
519#ifdef SIGPOLL
Guido van Rossume4485b01994-09-07 14:32:49 +0000520 x = PyInt_FromLong(SIGPOLL);
521 PyDict_SetItemString(d, "SIGPOLL", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000522 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000523#endif
524#ifdef SIGSTOP
Guido van Rossume4485b01994-09-07 14:32:49 +0000525 x = PyInt_FromLong(SIGSTOP);
526 PyDict_SetItemString(d, "SIGSTOP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000527 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000528#endif
529#ifdef SIGTSTP
Guido van Rossume4485b01994-09-07 14:32:49 +0000530 x = PyInt_FromLong(SIGTSTP);
531 PyDict_SetItemString(d, "SIGTSTP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000532 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000533#endif
534#ifdef SIGCONT
Guido van Rossume4485b01994-09-07 14:32:49 +0000535 x = PyInt_FromLong(SIGCONT);
536 PyDict_SetItemString(d, "SIGCONT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000537 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000538#endif
539#ifdef SIGTTIN
Guido van Rossume4485b01994-09-07 14:32:49 +0000540 x = PyInt_FromLong(SIGTTIN);
541 PyDict_SetItemString(d, "SIGTTIN", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000542 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000543#endif
544#ifdef SIGTTOU
Guido van Rossume4485b01994-09-07 14:32:49 +0000545 x = PyInt_FromLong(SIGTTOU);
546 PyDict_SetItemString(d, "SIGTTOU", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000547 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000548#endif
549#ifdef SIGVTALRM
Guido van Rossume4485b01994-09-07 14:32:49 +0000550 x = PyInt_FromLong(SIGVTALRM);
551 PyDict_SetItemString(d, "SIGVTALRM", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000552 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000553#endif
554#ifdef SIGPROF
Guido van Rossume4485b01994-09-07 14:32:49 +0000555 x = PyInt_FromLong(SIGPROF);
556 PyDict_SetItemString(d, "SIGPROF", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000557 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000558#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000559#ifdef SIGXCPU
560 x = PyInt_FromLong(SIGXCPU);
561 PyDict_SetItemString(d, "SIGXCPU", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000562 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000563#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000564#ifdef SIGXFSZ
565 x = PyInt_FromLong(SIGXFSZ);
566 PyDict_SetItemString(d, "SIGXFSZ", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000567 Py_XDECREF(x);
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000568#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000569 if (!PyErr_Occurred())
570 return;
571
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000572 /* Check for errors */
Barry Warsaw92971171997-01-03 00:14:25 +0000573 finally:
Guido van Rossum08c16611997-08-02 03:01:42 +0000574 return;
575}
576
577static void
578finisignal()
579{
580 int i;
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000581 PyObject *func;
Guido van Rossum08c16611997-08-02 03:01:42 +0000582
583 signal(SIGINT, old_siginthandler);
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000584 old_siginthandler = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +0000585
586 for (i = 1; i < NSIG; i++) {
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000587 func = Handlers[i].func;
Guido van Rossum08c16611997-08-02 03:01:42 +0000588 Handlers[i].tripped = 0;
Guido van Rossum08c16611997-08-02 03:01:42 +0000589 Handlers[i].func = NULL;
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000590 if (i != SIGINT && func != NULL && func != Py_None &&
591 func != DefaultHandler && func != IgnoreHandler)
592 signal(i, SIG_DFL);
593 Py_XDECREF(func);
Guido van Rossum08c16611997-08-02 03:01:42 +0000594 }
595
596 Py_XDECREF(IntHandler);
597 IntHandler = NULL;
598 Py_XDECREF(DefaultHandler);
599 DefaultHandler = NULL;
600 Py_XDECREF(IgnoreHandler);
601 IgnoreHandler = NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000602}
603
Barry Warsaw92971171997-01-03 00:14:25 +0000604
605
606/* Declared in pyerrors.h */
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000607int
Guido van Rossumec25b911995-01-22 00:46:57 +0000608PyErr_CheckSignals()
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000609{
610 int i;
Guido van Rossume4485b01994-09-07 14:32:49 +0000611 PyObject *f;
Barry Warsaw92971171997-01-03 00:14:25 +0000612
613 if (!is_tripped)
Guido van Rossum6299d1e1994-05-31 12:51:13 +0000614 return 0;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000615#ifdef WITH_THREAD
616 if (get_thread_ident() != main_thread)
617 return 0;
618#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000619 if (!(f = PyEval_GetFrame()))
Guido van Rossume4485b01994-09-07 14:32:49 +0000620 f = Py_None;
Barry Warsaw92971171997-01-03 00:14:25 +0000621
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000622 for (i = 1; i < NSIG; i++) {
Barry Warsaw92971171997-01-03 00:14:25 +0000623 if (Handlers[i].tripped) {
624 PyObject *result = NULL;
625 PyObject *arglist = Py_BuildValue("(iO)", i, f);
626 Handlers[i].tripped = 0;
627
628 if (arglist) {
629 result = PyEval_CallObject(Handlers[i].func,
630 arglist);
Guido van Rossume4485b01994-09-07 14:32:49 +0000631 Py_DECREF(arglist);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000632 }
Barry Warsaw92971171997-01-03 00:14:25 +0000633 if (!result)
Guido van Rossumfcdd0e41997-01-21 06:13:09 +0000634 return -1;
Barry Warsaw92971171997-01-03 00:14:25 +0000635
636 Py_DECREF(result);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000637 }
638 }
Barry Warsaw92971171997-01-03 00:14:25 +0000639 is_tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000640 return 0;
641}
642
Barry Warsaw92971171997-01-03 00:14:25 +0000643
644/* Replacements for intrcheck.c functionality
645 * Declared in pyerrors.h
646 */
647void
648PyErr_SetInterrupt()
649{
650 is_tripped++;
651 Handlers[SIGINT].tripped = 1;
Guido van Rossumfcdd0e41997-01-21 06:13:09 +0000652 Py_AddPendingCall((int (*) Py_PROTO((ANY *)))PyErr_CheckSignals, NULL);
Barry Warsaw92971171997-01-03 00:14:25 +0000653}
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000654
655void
Barry Warsaw92971171997-01-03 00:14:25 +0000656PyOS_InitInterrupts()
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000657{
658 initsignal();
Guido van Rossum08c16611997-08-02 03:01:42 +0000659 _PyImport_FixupExtension("signal", "signal");
660}
661
662void
663PyOS_FiniInterrupts()
664{
665 finisignal();
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000666}
667
668int
Barry Warsaw92971171997-01-03 00:14:25 +0000669PyOS_InterruptOccurred()
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000670{
Barry Warsaw92971171997-01-03 00:14:25 +0000671 if (Handlers[SIGINT].tripped) {
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000672#ifdef WITH_THREAD
673 if (get_thread_ident() != main_thread)
674 return 0;
675#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000676 Handlers[SIGINT].tripped = 0;
Guido van Rossum6299d1e1994-05-31 12:51:13 +0000677 return 1;
678 }
679 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000680}
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000681
682void
683PyOS_AfterFork()
684{
685#ifdef WITH_THREAD
686 main_thread = get_thread_ident();
687 main_pid = getpid();
688#endif
689}