blob: f75ec43118613423a574c5b84171a760c94d908f [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
Guido van Rossumc1cc8ab1997-02-14 16:35:36 +000038#ifdef _SIGMAX
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000039#define NSIG (_SIGMAX + 1) /* For QNX */
Guido van Rossumc1cc8ab1997-02-14 16:35:36 +000040#else
41#define NSIG (SIGMAX + 1) /* for djgpp */
42#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000043#endif
44
45
Barry Warsaw92971171997-01-03 00:14:25 +000046
Guido van Rossumbb4ba121994-06-23 11:25:45 +000047/*
48 NOTES ON THE INTERACTION BETWEEN SIGNALS AND THREADS
49
50 When threads are supported, we want the following semantics:
51
52 - only the main thread can set a signal handler
53 - any thread can get a signal handler
54 - signals are only delivered to the main thread
55
56 I.e. we don't support "synchronous signals" like SIGFPE (catching
57 this doesn't make much sense in Python anyway) nor do we support
58 signals as a means of inter-thread communication, since not all
59 thread implementations support that (at least our thread library
60 doesn't).
61
62 We still have the problem that in some implementations signals
63 generated by the keyboard (e.g. SIGINT) are delivered to all
64 threads (e.g. SGI), while in others (e.g. Solaris) such signals are
65 delivered to one random thread (an intermediate possibility would
Guido van Rossuma3c04b01995-01-12 11:29:01 +000066 be to deliver it to the main thread -- POSIX?). For now, we have
Guido van Rossumbb4ba121994-06-23 11:25:45 +000067 a working implementation that works in all three cases -- the
68 handler ignores signals if getpid() isn't the same as in the main
69 thread. XXX This is a hack.
70
71*/
72
73#ifdef WITH_THREAD
Guido van Rossum295b8e51997-06-06 21:16:41 +000074#include <sys/types.h> /* For pid_t */
Guido van Rossum49b56061998-10-01 20:42:43 +000075#include "pythread.h"
Guido van Rossumbb4ba121994-06-23 11:25:45 +000076static long main_thread;
77static pid_t main_pid;
78#endif
79
Barry Warsaw92971171997-01-03 00:14:25 +000080static struct {
81 int tripped;
82 PyObject *func;
83} Handlers[NSIG];
Guido van Rossum398d9fe1994-05-11 08:59:13 +000084
Barry Warsaw92971171997-01-03 00:14:25 +000085static int is_tripped = 0; /* Speed up sigcheck() when none tripped */
Guido van Rossum398d9fe1994-05-11 08:59:13 +000086
Barry Warsaw92971171997-01-03 00:14:25 +000087static PyObject *DefaultHandler;
88static PyObject *IgnoreHandler;
89static PyObject *IntHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +000090
Guido van Rossum08c16611997-08-02 03:01:42 +000091static RETSIGTYPE (*old_siginthandler)() = SIG_DFL;
92
Barry Warsaw92971171997-01-03 00:14:25 +000093
94
Guido van Rossume4485b01994-09-07 14:32:49 +000095static PyObject *
Barry Warsaw92971171997-01-03 00:14:25 +000096signal_default_int_handler(self, arg)
Guido van Rossume4485b01994-09-07 14:32:49 +000097 PyObject *self;
98 PyObject *arg;
Guido van Rossum398d9fe1994-05-11 08:59:13 +000099{
Guido van Rossume4485b01994-09-07 14:32:49 +0000100 PyErr_SetNone(PyExc_KeyboardInterrupt);
Barry Warsaw92971171997-01-03 00:14:25 +0000101 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000102}
103
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000104static char default_int_handler_doc[] =
105"default_int_handler(...)\n\
106\n\
107The default handler for SIGINT instated by Python.\n\
108It raises KeyboardInterrupt.";
109
Barry Warsaw92971171997-01-03 00:14:25 +0000110
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000111static RETSIGTYPE
Barry Warsaw92971171997-01-03 00:14:25 +0000112signal_handler(sig_num)
Guido van Rossume4485b01994-09-07 14:32:49 +0000113 int sig_num;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000114{
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000115#ifdef WITH_THREAD
116 /* See NOTES section above */
117 if (getpid() == main_pid) {
118#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000119 is_tripped++;
120 Handlers[sig_num].tripped = 1;
Guido van Rossum644a12b1997-04-09 19:24:53 +0000121 Py_AddPendingCall(
122 (int (*) Py_PROTO((ANY *)))PyErr_CheckSignals, NULL);
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000123#ifdef WITH_THREAD
124 }
125#endif
Guido van Rossum602099a1994-09-14 13:32:22 +0000126#ifdef SIGCHLD
127 if (sig_num == SIGCHLD) {
128 /* To avoid infinite recursion, this signal remains
129 reset until explicit re-instated.
130 Don't clear the 'func' field as it is our pointer
131 to the Python handler... */
132 return;
133 }
134#endif
Guido van Rossum1b236761998-09-21 14:46:00 +0000135#ifdef HAVE_SIGINTERRUPT
136 siginterrupt(sig_num, 1);
137#endif
Guido van Rossuma5e54d01998-05-01 18:58:59 +0000138 (void)signal(sig_num, &signal_handler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000139}
Guido van Rossume4485b01994-09-07 14:32:49 +0000140
Guido van Rossum06d511d1995-03-10 15:13:48 +0000141
Barry Warsaw92971171997-01-03 00:14:25 +0000142
Guido van Rossum1171ee61997-08-22 20:42:00 +0000143#ifdef HAVE_ALARM
Guido van Rossume4485b01994-09-07 14:32:49 +0000144static PyObject *
Barry Warsaw92971171997-01-03 00:14:25 +0000145signal_alarm(self, args)
Guido van Rossume4485b01994-09-07 14:32:49 +0000146 PyObject *self; /* Not used */
147 PyObject *args;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000148{
149 int t;
Guido van Rossume4485b01994-09-07 14:32:49 +0000150 if (!PyArg_Parse(args, "i", &t))
Barry Warsaw92971171997-01-03 00:14:25 +0000151 return NULL;
Guido van Rossume4485b01994-09-07 14:32:49 +0000152 /* alarm() returns the number of seconds remaining */
153 return PyInt_FromLong(alarm(t));
Guido van Rossumaa0f4c71994-08-23 13:49:37 +0000154}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000155
156static char alarm_doc[] =
157"alarm(seconds)\n\
158\n\
Guido van Rossum911ec181998-06-28 17:00:19 +0000159Arrange for SIGALRM to arrive after the given number of seconds.";
Guido van Rossum06d511d1995-03-10 15:13:48 +0000160#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000161
Guido van Rossum1171ee61997-08-22 20:42:00 +0000162#ifdef HAVE_PAUSE
Guido van Rossuma597dde1995-01-10 20:56:29 +0000163static PyObject *
Barry Warsaw92971171997-01-03 00:14:25 +0000164signal_pause(self, args)
Guido van Rossume4485b01994-09-07 14:32:49 +0000165 PyObject *self; /* Not used */
166 PyObject *args;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000167{
Guido van Rossume4485b01994-09-07 14:32:49 +0000168 if (!PyArg_NoArgs(args))
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000169 return NULL;
Barry Warsaw92971171997-01-03 00:14:25 +0000170
Guido van Rossuma597dde1995-01-10 20:56:29 +0000171 Py_BEGIN_ALLOW_THREADS
Barry Warsaw92971171997-01-03 00:14:25 +0000172 (void)pause();
Guido van Rossuma597dde1995-01-10 20:56:29 +0000173 Py_END_ALLOW_THREADS
Barry Warsaw92971171997-01-03 00:14:25 +0000174 /* make sure that any exceptions that got raised are propagated
175 * back into Python
176 */
177 if (PyErr_CheckSignals())
178 return NULL;
179
Guido van Rossume4485b01994-09-07 14:32:49 +0000180 Py_INCREF(Py_None);
181 return Py_None;
182}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000183static char pause_doc[] =
Barry Warsaw1ee36ff1998-07-21 22:41:18 +0000184"pause()\n\
185\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000186Wait until a signal arrives.";
187
Guido van Rossum06d511d1995-03-10 15:13:48 +0000188#endif
Guido van Rossume4485b01994-09-07 14:32:49 +0000189
Barry Warsaw92971171997-01-03 00:14:25 +0000190
Guido van Rossume4485b01994-09-07 14:32:49 +0000191static PyObject *
Barry Warsaw92971171997-01-03 00:14:25 +0000192signal_signal(self, args)
Guido van Rossume4485b01994-09-07 14:32:49 +0000193 PyObject *self; /* Not used */
194 PyObject *args;
195{
196 PyObject *obj;
197 int sig_num;
198 PyObject *old_handler;
199 RETSIGTYPE (*func)();
200 if (!PyArg_Parse(args, "(iO)", &sig_num, &obj))
Barry Warsaw92971171997-01-03 00:14:25 +0000201 return NULL;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000202#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000203 if (PyThread_get_thread_ident() != main_thread) {
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000204 PyErr_SetString(PyExc_ValueError,
205 "signal only works in main thread");
Barry Warsaw92971171997-01-03 00:14:25 +0000206 return NULL;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000207 }
208#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000209 if (sig_num < 1 || sig_num >= NSIG) {
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000210 PyErr_SetString(PyExc_ValueError,
211 "signal number out of range");
Barry Warsaw92971171997-01-03 00:14:25 +0000212 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000213 }
Barry Warsaw92971171997-01-03 00:14:25 +0000214 if (obj == IgnoreHandler)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000215 func = SIG_IGN;
Barry Warsaw92971171997-01-03 00:14:25 +0000216 else if (obj == DefaultHandler)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000217 func = SIG_DFL;
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000218 else if (!PyCallable_Check(obj)) {
Guido van Rossume4485b01994-09-07 14:32:49 +0000219 PyErr_SetString(PyExc_TypeError,
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000220"signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object");
Barry Warsaw92971171997-01-03 00:14:25 +0000221 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000222 }
223 else
Barry Warsaw92971171997-01-03 00:14:25 +0000224 func = signal_handler;
Guido van Rossum1b236761998-09-21 14:46:00 +0000225#ifdef HAVE_SIGINTERRUPT
226 siginterrupt(sig_num, 1);
227#endif
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000228 if (signal(sig_num, func) == SIG_ERR) {
Guido van Rossume4485b01994-09-07 14:32:49 +0000229 PyErr_SetFromErrno(PyExc_RuntimeError);
Barry Warsaw92971171997-01-03 00:14:25 +0000230 return NULL;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000231 }
Barry Warsaw92971171997-01-03 00:14:25 +0000232 old_handler = Handlers[sig_num].func;
233 Handlers[sig_num].tripped = 0;
Guido van Rossume4485b01994-09-07 14:32:49 +0000234 Py_INCREF(obj);
Barry Warsaw92971171997-01-03 00:14:25 +0000235 Handlers[sig_num].func = obj;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000236 return old_handler;
237}
238
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000239static char signal_doc[] =
240"signal(sig, action) -> action\n\
241\n\
242Set the action for the given signal. The action can be SIG_DFL,\n\
243SIG_IGN, or a callable Python object. The previous action is\n\
244returned. See getsignal() for possible return values.\n\
245\n\
246*** IMPORTANT NOTICE ***\n\
247A signal handler function is called with two arguments:\n\
248the first is the signal number, the second is the interrupted stack frame.";
249
Barry Warsaw92971171997-01-03 00:14:25 +0000250
Guido van Rossume4485b01994-09-07 14:32:49 +0000251static PyObject *
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000252signal_getsignal(self, args)
Guido van Rossume4485b01994-09-07 14:32:49 +0000253 PyObject *self; /* Not used */
254 PyObject *args;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000255{
256 int sig_num;
Guido van Rossume4485b01994-09-07 14:32:49 +0000257 PyObject *old_handler;
258 if (!PyArg_Parse(args, "i", &sig_num))
Barry Warsaw92971171997-01-03 00:14:25 +0000259 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000260 if (sig_num < 1 || sig_num >= NSIG) {
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000261 PyErr_SetString(PyExc_ValueError,
262 "signal number out of range");
Barry Warsaw92971171997-01-03 00:14:25 +0000263 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000264 }
Barry Warsaw92971171997-01-03 00:14:25 +0000265 old_handler = Handlers[sig_num].func;
Guido van Rossume4485b01994-09-07 14:32:49 +0000266 Py_INCREF(old_handler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000267 return old_handler;
268}
269
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000270static char getsignal_doc[] =
271"getsignal(sig) -> action\n\
272\n\
273Return the current action for the given signal. The return value can be:\n\
274SIG_IGN -- if the signal is being ignored\n\
275SIG_DFL -- if the default action for the signal is in effect\n\
276None -- if an unknown handler is in effect\n\
277anything else -- the callable Python object used as a handler\n\
278";
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000279
Barry Warsaw92971171997-01-03 00:14:25 +0000280
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000281/* List of functions defined in the module */
Barry Warsaw92971171997-01-03 00:14:25 +0000282static PyMethodDef signal_methods[] = {
Guido van Rossum1171ee61997-08-22 20:42:00 +0000283#ifdef HAVE_ALARM
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000284 {"alarm", signal_alarm, 0, alarm_doc},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000285#endif
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000286 {"signal", signal_signal, 0, signal_doc},
287 {"getsignal", signal_getsignal, 0, getsignal_doc},
Guido van Rossum1171ee61997-08-22 20:42:00 +0000288#ifdef HAVE_PAUSE
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000289 {"pause", signal_pause, 0, pause_doc},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000290#endif
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000291 {"default_int_handler", signal_default_int_handler, 0,
292 default_int_handler_doc},
293 {NULL, NULL} /* sentinel */
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000294};
295
Barry Warsaw92971171997-01-03 00:14:25 +0000296
297
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000298static char module_doc[] =
299"This module provides mechanisms to use signal handlers in Python.\n\
300\n\
301Functions:\n\
302\n\
303alarm() -- cause SIGALRM after a specified time [Unix only]\n\
304signal() -- set the action for a given signal\n\
305getsignal() -- get the signal action for a given signal\n\
306pause() -- wait until a signal arrives [Unix only]\n\
307default_int_handler() -- default SIGINT handler\n\
308\n\
309Constants:\n\
310\n\
311SIG_DFL -- used to refer to the system default handler\n\
312SIG_IGN -- used to ignore the signal\n\
313NSIG -- number of defined signals\n\
314\n\
315SIGINT, SIGTERM, etc. -- signal numbers\n\
316\n\
317*** IMPORTANT NOTICE ***\n\
318A signal handler function is called with two arguments:\n\
319the first is the signal number, the second is the interrupted stack frame.";
320
Guido van Rossum3886bb61998-12-04 18:50:17 +0000321DL_EXPORT(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000322initsignal()
323{
Guido van Rossume4485b01994-09-07 14:32:49 +0000324 PyObject *m, *d, *x;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000325 int i;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000326
327#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000328 main_thread = PyThread_get_thread_ident();
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000329 main_pid = getpid();
330#endif
331
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000332 /* Create the module and add the functions */
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000333 m = Py_InitModule3("signal", signal_methods, module_doc);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000334
335 /* Add some symbolic constants to the module */
Guido van Rossume4485b01994-09-07 14:32:49 +0000336 d = PyModule_GetDict(m);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000337
Guido van Rossum276fa432000-06-30 23:04:18 +0000338 x = DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL);
Barry Warsaw92971171997-01-03 00:14:25 +0000339 if (!x || PyDict_SetItemString(d, "SIG_DFL", x) < 0)
340 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000341
Guido van Rossum276fa432000-06-30 23:04:18 +0000342 x = IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN);
Barry Warsaw92971171997-01-03 00:14:25 +0000343 if (!x || PyDict_SetItemString(d, "SIG_IGN", x) < 0)
344 goto finally;
345
346 x = PyInt_FromLong((long)NSIG);
347 if (!x || PyDict_SetItemString(d, "NSIG", x) < 0)
348 goto finally;
Guido van Rossum08c16611997-08-02 03:01:42 +0000349 Py_DECREF(x);
Barry Warsaw92971171997-01-03 00:14:25 +0000350
351 x = IntHandler = PyDict_GetItemString(d, "default_int_handler");
352 if (!x)
353 goto finally;
Guido van Rossum08c16611997-08-02 03:01:42 +0000354 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +0000355
356 Handlers[0].tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000357 for (i = 1; i < NSIG; i++) {
Guido van Rossum6299d1e1994-05-31 12:51:13 +0000358 RETSIGTYPE (*t)();
Guido van Rossumfb0df941996-05-23 22:55:35 +0000359#ifdef HAVE_SIGACTION
360 struct sigaction act;
361 sigaction(i, 0, &act);
Guido van Rossum1835c4f1996-05-29 14:15:19 +0000362 t = act.sa_handler;
Guido van Rossumfb0df941996-05-23 22:55:35 +0000363#else
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000364 t = signal(i, SIG_IGN);
365 signal(i, t);
Guido van Rossumfb0df941996-05-23 22:55:35 +0000366#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000367 Handlers[i].tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000368 if (t == SIG_DFL)
Barry Warsaw92971171997-01-03 00:14:25 +0000369 Handlers[i].func = DefaultHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000370 else if (t == SIG_IGN)
Barry Warsaw92971171997-01-03 00:14:25 +0000371 Handlers[i].func = IgnoreHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000372 else
Barry Warsaw92971171997-01-03 00:14:25 +0000373 Handlers[i].func = Py_None; /* None of our business */
374 Py_INCREF(Handlers[i].func);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000375 }
Barry Warsaw92971171997-01-03 00:14:25 +0000376 if (Handlers[SIGINT].func == DefaultHandler) {
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000377 /* Install default int handler */
Guido van Rossum08c16611997-08-02 03:01:42 +0000378 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +0000379 Py_DECREF(Handlers[SIGINT].func);
380 Handlers[SIGINT].func = IntHandler;
Guido van Rossum08c16611997-08-02 03:01:42 +0000381 old_siginthandler = signal(SIGINT, &signal_handler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000382 }
383
384#ifdef SIGHUP
Guido van Rossume4485b01994-09-07 14:32:49 +0000385 x = PyInt_FromLong(SIGHUP);
386 PyDict_SetItemString(d, "SIGHUP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000387 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000388#endif
389#ifdef SIGINT
Guido van Rossume4485b01994-09-07 14:32:49 +0000390 x = PyInt_FromLong(SIGINT);
391 PyDict_SetItemString(d, "SIGINT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000392 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000393#endif
394#ifdef SIGQUIT
Guido van Rossume4485b01994-09-07 14:32:49 +0000395 x = PyInt_FromLong(SIGQUIT);
396 PyDict_SetItemString(d, "SIGQUIT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000397 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000398#endif
399#ifdef SIGILL
Guido van Rossume4485b01994-09-07 14:32:49 +0000400 x = PyInt_FromLong(SIGILL);
401 PyDict_SetItemString(d, "SIGILL", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000402 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000403#endif
404#ifdef SIGTRAP
Guido van Rossume4485b01994-09-07 14:32:49 +0000405 x = PyInt_FromLong(SIGTRAP);
406 PyDict_SetItemString(d, "SIGTRAP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000407 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000408#endif
409#ifdef SIGIOT
Guido van Rossume4485b01994-09-07 14:32:49 +0000410 x = PyInt_FromLong(SIGIOT);
411 PyDict_SetItemString(d, "SIGIOT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000412 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000413#endif
414#ifdef SIGABRT
Guido van Rossume4485b01994-09-07 14:32:49 +0000415 x = PyInt_FromLong(SIGABRT);
416 PyDict_SetItemString(d, "SIGABRT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000417 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000418#endif
419#ifdef SIGEMT
Guido van Rossume4485b01994-09-07 14:32:49 +0000420 x = PyInt_FromLong(SIGEMT);
421 PyDict_SetItemString(d, "SIGEMT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000422 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000423#endif
424#ifdef SIGFPE
Guido van Rossume4485b01994-09-07 14:32:49 +0000425 x = PyInt_FromLong(SIGFPE);
426 PyDict_SetItemString(d, "SIGFPE", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000427 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000428#endif
429#ifdef SIGKILL
Guido van Rossume4485b01994-09-07 14:32:49 +0000430 x = PyInt_FromLong(SIGKILL);
431 PyDict_SetItemString(d, "SIGKILL", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000432 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000433#endif
434#ifdef SIGBUS
Guido van Rossume4485b01994-09-07 14:32:49 +0000435 x = PyInt_FromLong(SIGBUS);
436 PyDict_SetItemString(d, "SIGBUS", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000437 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000438#endif
439#ifdef SIGSEGV
Guido van Rossume4485b01994-09-07 14:32:49 +0000440 x = PyInt_FromLong(SIGSEGV);
441 PyDict_SetItemString(d, "SIGSEGV", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000442 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000443#endif
444#ifdef SIGSYS
Guido van Rossume4485b01994-09-07 14:32:49 +0000445 x = PyInt_FromLong(SIGSYS);
446 PyDict_SetItemString(d, "SIGSYS", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000447 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000448#endif
449#ifdef SIGPIPE
Guido van Rossume4485b01994-09-07 14:32:49 +0000450 x = PyInt_FromLong(SIGPIPE);
451 PyDict_SetItemString(d, "SIGPIPE", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000452 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000453#endif
454#ifdef SIGALRM
Guido van Rossume4485b01994-09-07 14:32:49 +0000455 x = PyInt_FromLong(SIGALRM);
456 PyDict_SetItemString(d, "SIGALRM", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000457 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000458#endif
459#ifdef SIGTERM
Guido van Rossume4485b01994-09-07 14:32:49 +0000460 x = PyInt_FromLong(SIGTERM);
461 PyDict_SetItemString(d, "SIGTERM", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000462 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000463#endif
464#ifdef SIGUSR1
Guido van Rossume4485b01994-09-07 14:32:49 +0000465 x = PyInt_FromLong(SIGUSR1);
466 PyDict_SetItemString(d, "SIGUSR1", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000467 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000468#endif
469#ifdef SIGUSR2
Guido van Rossume4485b01994-09-07 14:32:49 +0000470 x = PyInt_FromLong(SIGUSR2);
471 PyDict_SetItemString(d, "SIGUSR2", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000472 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000473#endif
474#ifdef SIGCLD
Guido van Rossume4485b01994-09-07 14:32:49 +0000475 x = PyInt_FromLong(SIGCLD);
476 PyDict_SetItemString(d, "SIGCLD", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000477 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000478#endif
479#ifdef SIGCHLD
Guido van Rossume4485b01994-09-07 14:32:49 +0000480 x = PyInt_FromLong(SIGCHLD);
481 PyDict_SetItemString(d, "SIGCHLD", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000482 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000483#endif
484#ifdef SIGPWR
Guido van Rossume4485b01994-09-07 14:32:49 +0000485 x = PyInt_FromLong(SIGPWR);
486 PyDict_SetItemString(d, "SIGPWR", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000487 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000488#endif
489#ifdef SIGIO
Guido van Rossume4485b01994-09-07 14:32:49 +0000490 x = PyInt_FromLong(SIGIO);
491 PyDict_SetItemString(d, "SIGIO", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000492 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000493#endif
494#ifdef SIGURG
Guido van Rossume4485b01994-09-07 14:32:49 +0000495 x = PyInt_FromLong(SIGURG);
496 PyDict_SetItemString(d, "SIGURG", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000497 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000498#endif
499#ifdef SIGWINCH
Guido van Rossume4485b01994-09-07 14:32:49 +0000500 x = PyInt_FromLong(SIGWINCH);
501 PyDict_SetItemString(d, "SIGWINCH", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000502 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000503#endif
504#ifdef SIGPOLL
Guido van Rossume4485b01994-09-07 14:32:49 +0000505 x = PyInt_FromLong(SIGPOLL);
506 PyDict_SetItemString(d, "SIGPOLL", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000507 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000508#endif
509#ifdef SIGSTOP
Guido van Rossume4485b01994-09-07 14:32:49 +0000510 x = PyInt_FromLong(SIGSTOP);
511 PyDict_SetItemString(d, "SIGSTOP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000512 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000513#endif
514#ifdef SIGTSTP
Guido van Rossume4485b01994-09-07 14:32:49 +0000515 x = PyInt_FromLong(SIGTSTP);
516 PyDict_SetItemString(d, "SIGTSTP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000517 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000518#endif
519#ifdef SIGCONT
Guido van Rossume4485b01994-09-07 14:32:49 +0000520 x = PyInt_FromLong(SIGCONT);
521 PyDict_SetItemString(d, "SIGCONT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000522 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000523#endif
524#ifdef SIGTTIN
Guido van Rossume4485b01994-09-07 14:32:49 +0000525 x = PyInt_FromLong(SIGTTIN);
526 PyDict_SetItemString(d, "SIGTTIN", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000527 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000528#endif
529#ifdef SIGTTOU
Guido van Rossume4485b01994-09-07 14:32:49 +0000530 x = PyInt_FromLong(SIGTTOU);
531 PyDict_SetItemString(d, "SIGTTOU", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000532 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000533#endif
534#ifdef SIGVTALRM
Guido van Rossume4485b01994-09-07 14:32:49 +0000535 x = PyInt_FromLong(SIGVTALRM);
536 PyDict_SetItemString(d, "SIGVTALRM", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000537 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000538#endif
539#ifdef SIGPROF
Guido van Rossume4485b01994-09-07 14:32:49 +0000540 x = PyInt_FromLong(SIGPROF);
541 PyDict_SetItemString(d, "SIGPROF", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000542 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000543#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000544#ifdef SIGXCPU
545 x = PyInt_FromLong(SIGXCPU);
546 PyDict_SetItemString(d, "SIGXCPU", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000547 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000548#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000549#ifdef SIGXFSZ
550 x = PyInt_FromLong(SIGXFSZ);
551 PyDict_SetItemString(d, "SIGXFSZ", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000552 Py_XDECREF(x);
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000553#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000554 if (!PyErr_Occurred())
555 return;
556
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000557 /* Check for errors */
Barry Warsaw92971171997-01-03 00:14:25 +0000558 finally:
Guido van Rossum08c16611997-08-02 03:01:42 +0000559 return;
560}
561
562static void
563finisignal()
564{
565 int i;
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000566 PyObject *func;
Guido van Rossum08c16611997-08-02 03:01:42 +0000567
568 signal(SIGINT, old_siginthandler);
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000569 old_siginthandler = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +0000570
571 for (i = 1; i < NSIG; i++) {
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000572 func = Handlers[i].func;
Guido van Rossum08c16611997-08-02 03:01:42 +0000573 Handlers[i].tripped = 0;
Guido van Rossum08c16611997-08-02 03:01:42 +0000574 Handlers[i].func = NULL;
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000575 if (i != SIGINT && func != NULL && func != Py_None &&
576 func != DefaultHandler && func != IgnoreHandler)
577 signal(i, SIG_DFL);
578 Py_XDECREF(func);
Guido van Rossum08c16611997-08-02 03:01:42 +0000579 }
580
581 Py_XDECREF(IntHandler);
582 IntHandler = NULL;
583 Py_XDECREF(DefaultHandler);
584 DefaultHandler = NULL;
585 Py_XDECREF(IgnoreHandler);
586 IgnoreHandler = NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000587}
588
Barry Warsaw92971171997-01-03 00:14:25 +0000589
590
591/* Declared in pyerrors.h */
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000592int
Guido van Rossumec25b911995-01-22 00:46:57 +0000593PyErr_CheckSignals()
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000594{
595 int i;
Guido van Rossume4485b01994-09-07 14:32:49 +0000596 PyObject *f;
Barry Warsaw92971171997-01-03 00:14:25 +0000597
598 if (!is_tripped)
Guido van Rossum6299d1e1994-05-31 12:51:13 +0000599 return 0;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000600#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000601 if (PyThread_get_thread_ident() != main_thread)
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000602 return 0;
603#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000604 if (!(f = PyEval_GetFrame()))
Guido van Rossume4485b01994-09-07 14:32:49 +0000605 f = Py_None;
Barry Warsaw92971171997-01-03 00:14:25 +0000606
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000607 for (i = 1; i < NSIG; i++) {
Barry Warsaw92971171997-01-03 00:14:25 +0000608 if (Handlers[i].tripped) {
609 PyObject *result = NULL;
610 PyObject *arglist = Py_BuildValue("(iO)", i, f);
611 Handlers[i].tripped = 0;
612
613 if (arglist) {
614 result = PyEval_CallObject(Handlers[i].func,
615 arglist);
Guido van Rossume4485b01994-09-07 14:32:49 +0000616 Py_DECREF(arglist);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000617 }
Barry Warsaw92971171997-01-03 00:14:25 +0000618 if (!result)
Guido van Rossumfcdd0e41997-01-21 06:13:09 +0000619 return -1;
Barry Warsaw92971171997-01-03 00:14:25 +0000620
621 Py_DECREF(result);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000622 }
623 }
Barry Warsaw92971171997-01-03 00:14:25 +0000624 is_tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000625 return 0;
626}
627
Barry Warsaw92971171997-01-03 00:14:25 +0000628
629/* Replacements for intrcheck.c functionality
630 * Declared in pyerrors.h
631 */
632void
633PyErr_SetInterrupt()
634{
635 is_tripped++;
636 Handlers[SIGINT].tripped = 1;
Guido van Rossumfcdd0e41997-01-21 06:13:09 +0000637 Py_AddPendingCall((int (*) Py_PROTO((ANY *)))PyErr_CheckSignals, NULL);
Barry Warsaw92971171997-01-03 00:14:25 +0000638}
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000639
640void
Barry Warsaw92971171997-01-03 00:14:25 +0000641PyOS_InitInterrupts()
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000642{
643 initsignal();
Guido van Rossum08c16611997-08-02 03:01:42 +0000644 _PyImport_FixupExtension("signal", "signal");
645}
646
647void
648PyOS_FiniInterrupts()
649{
650 finisignal();
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000651}
652
653int
Barry Warsaw92971171997-01-03 00:14:25 +0000654PyOS_InterruptOccurred()
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000655{
Barry Warsaw92971171997-01-03 00:14:25 +0000656 if (Handlers[SIGINT].tripped) {
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000657#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000658 if (PyThread_get_thread_ident() != main_thread)
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000659 return 0;
660#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000661 Handlers[SIGINT].tripped = 0;
Guido van Rossum6299d1e1994-05-31 12:51:13 +0000662 return 1;
663 }
664 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000665}
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000666
667void
668PyOS_AfterFork()
669{
670#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000671 main_thread = PyThread_get_thread_ident();
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000672 main_pid = getpid();
673#endif
674}