blob: 368955e0c596e2d5b33cc9d461737d0ffd98b448 [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
Tim Peters4f1b2082000-07-23 21:18:09 +000029#define SIG_ERR ((void (*)(int))-1)
Guido van Rossumbb4ba121994-06-23 11:25:45 +000030#endif
31
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000032#if defined(PYOS_OS2)
33#define NSIG 12
34#include <process.h>
35#endif
36
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000037#ifndef NSIG
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000038# if defined(_NSIG)
39# define NSIG _NSIG /* For BSD/SysV */
40# elif defined(_SIGMAX)
41# define NSIG (_SIGMAX + 1) /* For QNX */
42# elif defined(SIGMAX)
43# define NSIG (SIGMAX + 1) /* For djgpp */
44# else
45# define NSIG 64 /* Use a reasonable default value */
46# endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000047#endif
48
49
Barry Warsaw92971171997-01-03 00:14:25 +000050
Guido van Rossumbb4ba121994-06-23 11:25:45 +000051/*
52 NOTES ON THE INTERACTION BETWEEN SIGNALS AND THREADS
53
54 When threads are supported, we want the following semantics:
55
56 - only the main thread can set a signal handler
57 - any thread can get a signal handler
58 - signals are only delivered to the main thread
59
60 I.e. we don't support "synchronous signals" like SIGFPE (catching
61 this doesn't make much sense in Python anyway) nor do we support
62 signals as a means of inter-thread communication, since not all
63 thread implementations support that (at least our thread library
64 doesn't).
65
66 We still have the problem that in some implementations signals
67 generated by the keyboard (e.g. SIGINT) are delivered to all
68 threads (e.g. SGI), while in others (e.g. Solaris) such signals are
69 delivered to one random thread (an intermediate possibility would
Guido van Rossuma3c04b01995-01-12 11:29:01 +000070 be to deliver it to the main thread -- POSIX?). For now, we have
Guido van Rossumbb4ba121994-06-23 11:25:45 +000071 a working implementation that works in all three cases -- the
72 handler ignores signals if getpid() isn't the same as in the main
73 thread. XXX This is a hack.
74
75*/
76
77#ifdef WITH_THREAD
Guido van Rossum295b8e51997-06-06 21:16:41 +000078#include <sys/types.h> /* For pid_t */
Guido van Rossum49b56061998-10-01 20:42:43 +000079#include "pythread.h"
Guido van Rossumbb4ba121994-06-23 11:25:45 +000080static long main_thread;
81static pid_t main_pid;
82#endif
83
Barry Warsaw92971171997-01-03 00:14:25 +000084static struct {
85 int tripped;
86 PyObject *func;
87} Handlers[NSIG];
Guido van Rossum398d9fe1994-05-11 08:59:13 +000088
Barry Warsaw92971171997-01-03 00:14:25 +000089static int is_tripped = 0; /* Speed up sigcheck() when none tripped */
Guido van Rossum398d9fe1994-05-11 08:59:13 +000090
Barry Warsaw92971171997-01-03 00:14:25 +000091static PyObject *DefaultHandler;
92static PyObject *IgnoreHandler;
93static PyObject *IntHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +000094
Tim Peters4f1b2082000-07-23 21:18:09 +000095static void (*old_siginthandler)(int) = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +000096
Barry Warsaw92971171997-01-03 00:14:25 +000097
98
Guido van Rossume4485b01994-09-07 14:32:49 +000099static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000100signal_default_int_handler(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000101{
Guido van Rossume4485b01994-09-07 14:32:49 +0000102 PyErr_SetNone(PyExc_KeyboardInterrupt);
Barry Warsaw92971171997-01-03 00:14:25 +0000103 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000104}
105
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000106static char default_int_handler_doc[] =
107"default_int_handler(...)\n\
108\n\
109The default handler for SIGINT instated by Python.\n\
110It raises KeyboardInterrupt.";
111
Barry Warsaw92971171997-01-03 00:14:25 +0000112
Thomas Wouters0796b002000-07-22 23:49:30 +0000113
114static int
115checksignals_witharg(void * unused)
116{
117 return PyErr_CheckSignals();
118}
119
Tim Peters4f1b2082000-07-23 21:18:09 +0000120static void
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000121signal_handler(int sig_num)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000122{
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000123#ifdef WITH_THREAD
124 /* See NOTES section above */
125 if (getpid() == main_pid) {
126#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000127 is_tripped++;
128 Handlers[sig_num].tripped = 1;
Thomas Wouters0796b002000-07-22 23:49:30 +0000129 Py_AddPendingCall(checksignals_witharg, NULL);
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000130#ifdef WITH_THREAD
131 }
132#endif
Guido van Rossum602099a1994-09-14 13:32:22 +0000133#ifdef SIGCHLD
134 if (sig_num == SIGCHLD) {
135 /* To avoid infinite recursion, this signal remains
136 reset until explicit re-instated.
137 Don't clear the 'func' field as it is our pointer
138 to the Python handler... */
Tim Peters4f1b2082000-07-23 21:18:09 +0000139 return;
Guido van Rossum602099a1994-09-14 13:32:22 +0000140 }
141#endif
Guido van Rossum1b236761998-09-21 14:46:00 +0000142#ifdef HAVE_SIGINTERRUPT
143 siginterrupt(sig_num, 1);
144#endif
Thomas Wouters0796b002000-07-22 23:49:30 +0000145 signal(sig_num, signal_handler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000146}
Guido van Rossume4485b01994-09-07 14:32:49 +0000147
Guido van Rossum06d511d1995-03-10 15:13:48 +0000148
Barry Warsaw92971171997-01-03 00:14:25 +0000149
Guido van Rossum1171ee61997-08-22 20:42:00 +0000150#ifdef HAVE_ALARM
Guido van Rossume4485b01994-09-07 14:32:49 +0000151static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000152signal_alarm(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000153{
154 int t;
Guido van Rossume4485b01994-09-07 14:32:49 +0000155 if (!PyArg_Parse(args, "i", &t))
Barry Warsaw92971171997-01-03 00:14:25 +0000156 return NULL;
Guido van Rossume4485b01994-09-07 14:32:49 +0000157 /* alarm() returns the number of seconds remaining */
158 return PyInt_FromLong(alarm(t));
Guido van Rossumaa0f4c71994-08-23 13:49:37 +0000159}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000160
161static char alarm_doc[] =
162"alarm(seconds)\n\
163\n\
Guido van Rossum911ec181998-06-28 17:00:19 +0000164Arrange for SIGALRM to arrive after the given number of seconds.";
Guido van Rossum06d511d1995-03-10 15:13:48 +0000165#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000166
Guido van Rossum1171ee61997-08-22 20:42:00 +0000167#ifdef HAVE_PAUSE
Guido van Rossuma597dde1995-01-10 20:56:29 +0000168static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000169signal_pause(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000170{
Guido van Rossume4485b01994-09-07 14:32:49 +0000171 if (!PyArg_NoArgs(args))
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000172 return NULL;
Barry Warsaw92971171997-01-03 00:14:25 +0000173
Guido van Rossuma597dde1995-01-10 20:56:29 +0000174 Py_BEGIN_ALLOW_THREADS
Barry Warsaw92971171997-01-03 00:14:25 +0000175 (void)pause();
Guido van Rossuma597dde1995-01-10 20:56:29 +0000176 Py_END_ALLOW_THREADS
Barry Warsaw92971171997-01-03 00:14:25 +0000177 /* make sure that any exceptions that got raised are propagated
178 * back into Python
179 */
180 if (PyErr_CheckSignals())
181 return NULL;
182
Guido van Rossume4485b01994-09-07 14:32:49 +0000183 Py_INCREF(Py_None);
184 return Py_None;
185}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000186static char pause_doc[] =
Barry Warsaw1ee36ff1998-07-21 22:41:18 +0000187"pause()\n\
188\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000189Wait until a signal arrives.";
190
Guido van Rossum06d511d1995-03-10 15:13:48 +0000191#endif
Guido van Rossume4485b01994-09-07 14:32:49 +0000192
Barry Warsaw92971171997-01-03 00:14:25 +0000193
Guido van Rossume4485b01994-09-07 14:32:49 +0000194static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000195signal_signal(PyObject *self, PyObject *args)
Guido van Rossume4485b01994-09-07 14:32:49 +0000196{
197 PyObject *obj;
198 int sig_num;
199 PyObject *old_handler;
Tim Peters4f1b2082000-07-23 21:18:09 +0000200 void (*func)(int);
Guido van Rossume4485b01994-09-07 14:32:49 +0000201 if (!PyArg_Parse(args, "(iO)", &sig_num, &obj))
Barry Warsaw92971171997-01-03 00:14:25 +0000202 return NULL;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000203#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000204 if (PyThread_get_thread_ident() != main_thread) {
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000205 PyErr_SetString(PyExc_ValueError,
206 "signal only works in main thread");
Barry Warsaw92971171997-01-03 00:14:25 +0000207 return NULL;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000208 }
209#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000210 if (sig_num < 1 || sig_num >= NSIG) {
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000211 PyErr_SetString(PyExc_ValueError,
212 "signal number out of range");
Barry Warsaw92971171997-01-03 00:14:25 +0000213 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000214 }
Barry Warsaw92971171997-01-03 00:14:25 +0000215 if (obj == IgnoreHandler)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000216 func = SIG_IGN;
Barry Warsaw92971171997-01-03 00:14:25 +0000217 else if (obj == DefaultHandler)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000218 func = SIG_DFL;
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000219 else if (!PyCallable_Check(obj)) {
Guido van Rossume4485b01994-09-07 14:32:49 +0000220 PyErr_SetString(PyExc_TypeError,
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000221"signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object");
Barry Warsaw92971171997-01-03 00:14:25 +0000222 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000223 }
224 else
Barry Warsaw92971171997-01-03 00:14:25 +0000225 func = signal_handler;
Guido van Rossum1b236761998-09-21 14:46:00 +0000226#ifdef HAVE_SIGINTERRUPT
227 siginterrupt(sig_num, 1);
228#endif
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000229 if (signal(sig_num, func) == SIG_ERR) {
Guido van Rossume4485b01994-09-07 14:32:49 +0000230 PyErr_SetFromErrno(PyExc_RuntimeError);
Barry Warsaw92971171997-01-03 00:14:25 +0000231 return NULL;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000232 }
Barry Warsaw92971171997-01-03 00:14:25 +0000233 old_handler = Handlers[sig_num].func;
234 Handlers[sig_num].tripped = 0;
Guido van Rossume4485b01994-09-07 14:32:49 +0000235 Py_INCREF(obj);
Barry Warsaw92971171997-01-03 00:14:25 +0000236 Handlers[sig_num].func = obj;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000237 return old_handler;
238}
239
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000240static char signal_doc[] =
241"signal(sig, action) -> action\n\
242\n\
243Set the action for the given signal. The action can be SIG_DFL,\n\
244SIG_IGN, or a callable Python object. The previous action is\n\
245returned. See getsignal() for possible return values.\n\
246\n\
247*** IMPORTANT NOTICE ***\n\
248A signal handler function is called with two arguments:\n\
249the first is the signal number, the second is the interrupted stack frame.";
250
Barry Warsaw92971171997-01-03 00:14:25 +0000251
Guido van Rossume4485b01994-09-07 14:32:49 +0000252static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000253signal_getsignal(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000254{
255 int sig_num;
Guido van Rossume4485b01994-09-07 14:32:49 +0000256 PyObject *old_handler;
257 if (!PyArg_Parse(args, "i", &sig_num))
Barry Warsaw92971171997-01-03 00:14:25 +0000258 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000259 if (sig_num < 1 || sig_num >= NSIG) {
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000260 PyErr_SetString(PyExc_ValueError,
261 "signal number out of range");
Barry Warsaw92971171997-01-03 00:14:25 +0000262 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000263 }
Barry Warsaw92971171997-01-03 00:14:25 +0000264 old_handler = Handlers[sig_num].func;
Guido van Rossume4485b01994-09-07 14:32:49 +0000265 Py_INCREF(old_handler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000266 return old_handler;
267}
268
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000269static char getsignal_doc[] =
270"getsignal(sig) -> action\n\
271\n\
272Return the current action for the given signal. The return value can be:\n\
273SIG_IGN -- if the signal is being ignored\n\
274SIG_DFL -- if the default action for the signal is in effect\n\
275None -- if an unknown handler is in effect\n\
276anything else -- the callable Python object used as a handler\n\
277";
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000278
Barry Warsaw92971171997-01-03 00:14:25 +0000279
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000280/* List of functions defined in the module */
Barry Warsaw92971171997-01-03 00:14:25 +0000281static PyMethodDef signal_methods[] = {
Guido van Rossum1171ee61997-08-22 20:42:00 +0000282#ifdef HAVE_ALARM
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000283 {"alarm", signal_alarm, METH_OLDARGS, alarm_doc},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000284#endif
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000285 {"signal", signal_signal, METH_OLDARGS, signal_doc},
286 {"getsignal", signal_getsignal, METH_OLDARGS, getsignal_doc},
Guido van Rossum1171ee61997-08-22 20:42:00 +0000287#ifdef HAVE_PAUSE
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000288 {"pause", signal_pause, METH_OLDARGS, pause_doc},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000289#endif
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000290 {"default_int_handler", signal_default_int_handler,
291 METH_OLDARGS, default_int_handler_doc},
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000292 {NULL, NULL} /* sentinel */
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000293};
294
Barry Warsaw92971171997-01-03 00:14:25 +0000295
296
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000297static char module_doc[] =
298"This module provides mechanisms to use signal handlers in Python.\n\
299\n\
300Functions:\n\
301\n\
302alarm() -- cause SIGALRM after a specified time [Unix only]\n\
303signal() -- set the action for a given signal\n\
304getsignal() -- get the signal action for a given signal\n\
305pause() -- wait until a signal arrives [Unix only]\n\
306default_int_handler() -- default SIGINT handler\n\
307\n\
308Constants:\n\
309\n\
310SIG_DFL -- used to refer to the system default handler\n\
311SIG_IGN -- used to ignore the signal\n\
312NSIG -- number of defined signals\n\
313\n\
314SIGINT, SIGTERM, etc. -- signal numbers\n\
315\n\
316*** IMPORTANT NOTICE ***\n\
317A signal handler function is called with two arguments:\n\
318the first is the signal number, the second is the interrupted stack frame.";
319
Guido van Rossum3886bb61998-12-04 18:50:17 +0000320DL_EXPORT(void)
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000321initsignal(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000322{
Guido van Rossume4485b01994-09-07 14:32:49 +0000323 PyObject *m, *d, *x;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000324 int i;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000325
326#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000327 main_thread = PyThread_get_thread_ident();
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000328 main_pid = getpid();
329#endif
330
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000331 /* Create the module and add the functions */
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000332 m = Py_InitModule3("signal", signal_methods, module_doc);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000333
334 /* Add some symbolic constants to the module */
Guido van Rossume4485b01994-09-07 14:32:49 +0000335 d = PyModule_GetDict(m);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000336
Guido van Rossum276fa432000-06-30 23:04:18 +0000337 x = DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL);
Barry Warsaw92971171997-01-03 00:14:25 +0000338 if (!x || PyDict_SetItemString(d, "SIG_DFL", x) < 0)
339 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000340
Guido van Rossum276fa432000-06-30 23:04:18 +0000341 x = IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN);
Barry Warsaw92971171997-01-03 00:14:25 +0000342 if (!x || PyDict_SetItemString(d, "SIG_IGN", x) < 0)
343 goto finally;
344
345 x = PyInt_FromLong((long)NSIG);
346 if (!x || PyDict_SetItemString(d, "NSIG", x) < 0)
347 goto finally;
Guido van Rossum08c16611997-08-02 03:01:42 +0000348 Py_DECREF(x);
Barry Warsaw92971171997-01-03 00:14:25 +0000349
350 x = IntHandler = PyDict_GetItemString(d, "default_int_handler");
351 if (!x)
352 goto finally;
Guido van Rossum08c16611997-08-02 03:01:42 +0000353 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +0000354
355 Handlers[0].tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000356 for (i = 1; i < NSIG; i++) {
Tim Peters4f1b2082000-07-23 21:18:09 +0000357 void (*t)(int);
Guido van Rossumfb0df941996-05-23 22:55:35 +0000358#ifdef HAVE_SIGACTION
359 struct sigaction act;
360 sigaction(i, 0, &act);
Guido van Rossum1835c4f1996-05-29 14:15:19 +0000361 t = act.sa_handler;
Guido van Rossumfb0df941996-05-23 22:55:35 +0000362#else
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000363 t = signal(i, SIG_IGN);
364 signal(i, t);
Guido van Rossumfb0df941996-05-23 22:55:35 +0000365#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000366 Handlers[i].tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000367 if (t == SIG_DFL)
Barry Warsaw92971171997-01-03 00:14:25 +0000368 Handlers[i].func = DefaultHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000369 else if (t == SIG_IGN)
Barry Warsaw92971171997-01-03 00:14:25 +0000370 Handlers[i].func = IgnoreHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000371 else
Barry Warsaw92971171997-01-03 00:14:25 +0000372 Handlers[i].func = Py_None; /* None of our business */
373 Py_INCREF(Handlers[i].func);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000374 }
Barry Warsaw92971171997-01-03 00:14:25 +0000375 if (Handlers[SIGINT].func == DefaultHandler) {
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000376 /* Install default int handler */
Guido van Rossum08c16611997-08-02 03:01:42 +0000377 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +0000378 Py_DECREF(Handlers[SIGINT].func);
379 Handlers[SIGINT].func = IntHandler;
Guido van Rossum08c16611997-08-02 03:01:42 +0000380 old_siginthandler = signal(SIGINT, &signal_handler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000381 }
382
383#ifdef SIGHUP
Guido van Rossume4485b01994-09-07 14:32:49 +0000384 x = PyInt_FromLong(SIGHUP);
385 PyDict_SetItemString(d, "SIGHUP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000386 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000387#endif
388#ifdef SIGINT
Guido van Rossume4485b01994-09-07 14:32:49 +0000389 x = PyInt_FromLong(SIGINT);
390 PyDict_SetItemString(d, "SIGINT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000391 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000392#endif
393#ifdef SIGQUIT
Guido van Rossume4485b01994-09-07 14:32:49 +0000394 x = PyInt_FromLong(SIGQUIT);
395 PyDict_SetItemString(d, "SIGQUIT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000396 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000397#endif
398#ifdef SIGILL
Guido van Rossume4485b01994-09-07 14:32:49 +0000399 x = PyInt_FromLong(SIGILL);
400 PyDict_SetItemString(d, "SIGILL", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000401 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000402#endif
403#ifdef SIGTRAP
Guido van Rossume4485b01994-09-07 14:32:49 +0000404 x = PyInt_FromLong(SIGTRAP);
405 PyDict_SetItemString(d, "SIGTRAP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000406 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000407#endif
408#ifdef SIGIOT
Guido van Rossume4485b01994-09-07 14:32:49 +0000409 x = PyInt_FromLong(SIGIOT);
410 PyDict_SetItemString(d, "SIGIOT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000411 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000412#endif
413#ifdef SIGABRT
Guido van Rossume4485b01994-09-07 14:32:49 +0000414 x = PyInt_FromLong(SIGABRT);
415 PyDict_SetItemString(d, "SIGABRT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000416 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000417#endif
418#ifdef SIGEMT
Guido van Rossume4485b01994-09-07 14:32:49 +0000419 x = PyInt_FromLong(SIGEMT);
420 PyDict_SetItemString(d, "SIGEMT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000421 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000422#endif
423#ifdef SIGFPE
Guido van Rossume4485b01994-09-07 14:32:49 +0000424 x = PyInt_FromLong(SIGFPE);
425 PyDict_SetItemString(d, "SIGFPE", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000426 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000427#endif
428#ifdef SIGKILL
Guido van Rossume4485b01994-09-07 14:32:49 +0000429 x = PyInt_FromLong(SIGKILL);
430 PyDict_SetItemString(d, "SIGKILL", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000431 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000432#endif
433#ifdef SIGBUS
Guido van Rossume4485b01994-09-07 14:32:49 +0000434 x = PyInt_FromLong(SIGBUS);
435 PyDict_SetItemString(d, "SIGBUS", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000436 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000437#endif
438#ifdef SIGSEGV
Guido van Rossume4485b01994-09-07 14:32:49 +0000439 x = PyInt_FromLong(SIGSEGV);
440 PyDict_SetItemString(d, "SIGSEGV", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000441 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000442#endif
443#ifdef SIGSYS
Guido van Rossume4485b01994-09-07 14:32:49 +0000444 x = PyInt_FromLong(SIGSYS);
445 PyDict_SetItemString(d, "SIGSYS", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000446 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000447#endif
448#ifdef SIGPIPE
Guido van Rossume4485b01994-09-07 14:32:49 +0000449 x = PyInt_FromLong(SIGPIPE);
450 PyDict_SetItemString(d, "SIGPIPE", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000451 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000452#endif
453#ifdef SIGALRM
Guido van Rossume4485b01994-09-07 14:32:49 +0000454 x = PyInt_FromLong(SIGALRM);
455 PyDict_SetItemString(d, "SIGALRM", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000456 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000457#endif
458#ifdef SIGTERM
Guido van Rossume4485b01994-09-07 14:32:49 +0000459 x = PyInt_FromLong(SIGTERM);
460 PyDict_SetItemString(d, "SIGTERM", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000461 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000462#endif
463#ifdef SIGUSR1
Guido van Rossume4485b01994-09-07 14:32:49 +0000464 x = PyInt_FromLong(SIGUSR1);
465 PyDict_SetItemString(d, "SIGUSR1", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000466 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000467#endif
468#ifdef SIGUSR2
Guido van Rossume4485b01994-09-07 14:32:49 +0000469 x = PyInt_FromLong(SIGUSR2);
470 PyDict_SetItemString(d, "SIGUSR2", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000471 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000472#endif
473#ifdef SIGCLD
Guido van Rossume4485b01994-09-07 14:32:49 +0000474 x = PyInt_FromLong(SIGCLD);
475 PyDict_SetItemString(d, "SIGCLD", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000476 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000477#endif
478#ifdef SIGCHLD
Guido van Rossume4485b01994-09-07 14:32:49 +0000479 x = PyInt_FromLong(SIGCHLD);
480 PyDict_SetItemString(d, "SIGCHLD", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000481 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000482#endif
483#ifdef SIGPWR
Guido van Rossume4485b01994-09-07 14:32:49 +0000484 x = PyInt_FromLong(SIGPWR);
485 PyDict_SetItemString(d, "SIGPWR", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000486 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000487#endif
488#ifdef SIGIO
Guido van Rossume4485b01994-09-07 14:32:49 +0000489 x = PyInt_FromLong(SIGIO);
490 PyDict_SetItemString(d, "SIGIO", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000491 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000492#endif
493#ifdef SIGURG
Guido van Rossume4485b01994-09-07 14:32:49 +0000494 x = PyInt_FromLong(SIGURG);
495 PyDict_SetItemString(d, "SIGURG", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000496 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000497#endif
498#ifdef SIGWINCH
Guido van Rossume4485b01994-09-07 14:32:49 +0000499 x = PyInt_FromLong(SIGWINCH);
500 PyDict_SetItemString(d, "SIGWINCH", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000501 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000502#endif
503#ifdef SIGPOLL
Guido van Rossume4485b01994-09-07 14:32:49 +0000504 x = PyInt_FromLong(SIGPOLL);
505 PyDict_SetItemString(d, "SIGPOLL", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000506 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000507#endif
508#ifdef SIGSTOP
Guido van Rossume4485b01994-09-07 14:32:49 +0000509 x = PyInt_FromLong(SIGSTOP);
510 PyDict_SetItemString(d, "SIGSTOP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000511 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000512#endif
513#ifdef SIGTSTP
Guido van Rossume4485b01994-09-07 14:32:49 +0000514 x = PyInt_FromLong(SIGTSTP);
515 PyDict_SetItemString(d, "SIGTSTP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000516 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000517#endif
518#ifdef SIGCONT
Guido van Rossume4485b01994-09-07 14:32:49 +0000519 x = PyInt_FromLong(SIGCONT);
520 PyDict_SetItemString(d, "SIGCONT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000521 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000522#endif
523#ifdef SIGTTIN
Guido van Rossume4485b01994-09-07 14:32:49 +0000524 x = PyInt_FromLong(SIGTTIN);
525 PyDict_SetItemString(d, "SIGTTIN", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000526 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000527#endif
528#ifdef SIGTTOU
Guido van Rossume4485b01994-09-07 14:32:49 +0000529 x = PyInt_FromLong(SIGTTOU);
530 PyDict_SetItemString(d, "SIGTTOU", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000531 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000532#endif
533#ifdef SIGVTALRM
Guido van Rossume4485b01994-09-07 14:32:49 +0000534 x = PyInt_FromLong(SIGVTALRM);
535 PyDict_SetItemString(d, "SIGVTALRM", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000536 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000537#endif
538#ifdef SIGPROF
Guido van Rossume4485b01994-09-07 14:32:49 +0000539 x = PyInt_FromLong(SIGPROF);
540 PyDict_SetItemString(d, "SIGPROF", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000541 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000542#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000543#ifdef SIGXCPU
544 x = PyInt_FromLong(SIGXCPU);
545 PyDict_SetItemString(d, "SIGXCPU", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000546 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000547#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000548#ifdef SIGXFSZ
549 x = PyInt_FromLong(SIGXFSZ);
550 PyDict_SetItemString(d, "SIGXFSZ", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000551 Py_XDECREF(x);
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000552#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000553 if (!PyErr_Occurred())
554 return;
555
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000556 /* Check for errors */
Barry Warsaw92971171997-01-03 00:14:25 +0000557 finally:
Guido van Rossum08c16611997-08-02 03:01:42 +0000558 return;
559}
560
561static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000562finisignal(void)
Guido van Rossum08c16611997-08-02 03:01:42 +0000563{
564 int i;
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000565 PyObject *func;
Guido van Rossum08c16611997-08-02 03:01:42 +0000566
567 signal(SIGINT, old_siginthandler);
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000568 old_siginthandler = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +0000569
570 for (i = 1; i < NSIG; i++) {
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000571 func = Handlers[i].func;
Guido van Rossum08c16611997-08-02 03:01:42 +0000572 Handlers[i].tripped = 0;
Guido van Rossum08c16611997-08-02 03:01:42 +0000573 Handlers[i].func = NULL;
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000574 if (i != SIGINT && func != NULL && func != Py_None &&
575 func != DefaultHandler && func != IgnoreHandler)
576 signal(i, SIG_DFL);
577 Py_XDECREF(func);
Guido van Rossum08c16611997-08-02 03:01:42 +0000578 }
579
580 Py_XDECREF(IntHandler);
581 IntHandler = NULL;
582 Py_XDECREF(DefaultHandler);
583 DefaultHandler = NULL;
584 Py_XDECREF(IgnoreHandler);
585 IgnoreHandler = NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000586}
587
Barry Warsaw92971171997-01-03 00:14:25 +0000588
589
590/* Declared in pyerrors.h */
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000591int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000592PyErr_CheckSignals(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000593{
594 int i;
Guido van Rossume4485b01994-09-07 14:32:49 +0000595 PyObject *f;
Barry Warsaw92971171997-01-03 00:14:25 +0000596
597 if (!is_tripped)
Guido van Rossum6299d1e1994-05-31 12:51:13 +0000598 return 0;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000599#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000600 if (PyThread_get_thread_ident() != main_thread)
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000601 return 0;
602#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000603 if (!(f = PyEval_GetFrame()))
Guido van Rossume4485b01994-09-07 14:32:49 +0000604 f = Py_None;
Barry Warsaw92971171997-01-03 00:14:25 +0000605
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000606 for (i = 1; i < NSIG; i++) {
Barry Warsaw92971171997-01-03 00:14:25 +0000607 if (Handlers[i].tripped) {
608 PyObject *result = NULL;
609 PyObject *arglist = Py_BuildValue("(iO)", i, f);
610 Handlers[i].tripped = 0;
611
612 if (arglist) {
613 result = PyEval_CallObject(Handlers[i].func,
614 arglist);
Guido van Rossume4485b01994-09-07 14:32:49 +0000615 Py_DECREF(arglist);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000616 }
Barry Warsaw92971171997-01-03 00:14:25 +0000617 if (!result)
Guido van Rossumfcdd0e41997-01-21 06:13:09 +0000618 return -1;
Barry Warsaw92971171997-01-03 00:14:25 +0000619
620 Py_DECREF(result);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000621 }
622 }
Barry Warsaw92971171997-01-03 00:14:25 +0000623 is_tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000624 return 0;
625}
626
Barry Warsaw92971171997-01-03 00:14:25 +0000627
628/* Replacements for intrcheck.c functionality
629 * Declared in pyerrors.h
630 */
631void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000632PyErr_SetInterrupt(void)
Barry Warsaw92971171997-01-03 00:14:25 +0000633{
634 is_tripped++;
635 Handlers[SIGINT].tripped = 1;
Thomas Wouters334fb892000-07-25 12:56:38 +0000636 Py_AddPendingCall((int (*)(void *))PyErr_CheckSignals, NULL);
Barry Warsaw92971171997-01-03 00:14:25 +0000637}
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000638
639void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000640PyOS_InitInterrupts(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000641{
642 initsignal();
Guido van Rossum08c16611997-08-02 03:01:42 +0000643 _PyImport_FixupExtension("signal", "signal");
644}
645
646void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000647PyOS_FiniInterrupts(void)
Guido van Rossum08c16611997-08-02 03:01:42 +0000648{
649 finisignal();
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000650}
651
652int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000653PyOS_InterruptOccurred(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000654{
Barry Warsaw92971171997-01-03 00:14:25 +0000655 if (Handlers[SIGINT].tripped) {
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000656#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000657 if (PyThread_get_thread_ident() != main_thread)
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000658 return 0;
659#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000660 Handlers[SIGINT].tripped = 0;
Guido van Rossum6299d1e1994-05-31 12:51:13 +0000661 return 1;
662 }
663 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000664}
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000665
666void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000667PyOS_AfterFork(void)
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000668{
669#ifdef WITH_THREAD
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000670 PyEval_ReInitThreads();
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}