blob: 757fe787af6891d53c19274b9f774adfe8d10fa7 [file] [log] [blame]
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001/***********************************************************
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00002Copyright (c) 2000, BeOpen.com.
3Copyright (c) 1995-2000, Corporation for National Research Initiatives.
4Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
5All rights reserved.
Guido van Rossum398d9fe1994-05-11 08:59:13 +00006
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00007See the file "Misc/COPYRIGHT" for information on usage and
8redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
Guido van Rossum398d9fe1994-05-11 08:59:13 +00009******************************************************************/
10
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000011/* Signal module -- many thanks to Lance Ellinghaus */
Guido van Rossum398d9fe1994-05-11 08:59:13 +000012
Guido van Rossum644a12b1997-04-09 19:24:53 +000013/* XXX Signals should be recorded per thread, now we have thread state. */
14
Guido van Rossum602099a1994-09-14 13:32:22 +000015#include "Python.h"
Guido van Rossum398d9fe1994-05-11 08:59:13 +000016#include "intrcheck.h"
17
Guido van Rossum644a12b1997-04-09 19:24:53 +000018#ifdef MS_WIN32
19#include <process.h>
20#endif
21
Guido van Rossuma376cc51996-12-05 23:43:35 +000022#ifdef HAVE_UNISTD_H
23#include <unistd.h>
24#endif
25
Guido van Rossum398d9fe1994-05-11 08:59:13 +000026#include <signal.h>
27
Guido van Rossumbb4ba121994-06-23 11:25:45 +000028#ifndef SIG_ERR
29#define SIG_ERR ((RETSIGTYPE (*)())-1)
30#endif
31
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000032#if defined(PYOS_OS2)
33#define NSIG 12
34#include <process.h>
35#endif
36
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000037#ifndef NSIG
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000038# if defined(_NSIG)
39# define NSIG _NSIG /* For BSD/SysV */
40# elif defined(_SIGMAX)
41# define NSIG (_SIGMAX + 1) /* For QNX */
42# elif defined(SIGMAX)
43# define NSIG (SIGMAX + 1) /* For djgpp */
44# else
45# define NSIG 64 /* Use a reasonable default value */
46# endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000047#endif
48
49
Barry Warsaw92971171997-01-03 00:14:25 +000050
Guido van Rossumbb4ba121994-06-23 11:25:45 +000051/*
52 NOTES ON THE INTERACTION BETWEEN SIGNALS AND THREADS
53
54 When threads are supported, we want the following semantics:
55
56 - only the main thread can set a signal handler
57 - any thread can get a signal handler
58 - signals are only delivered to the main thread
59
60 I.e. we don't support "synchronous signals" like SIGFPE (catching
61 this doesn't make much sense in Python anyway) nor do we support
62 signals as a means of inter-thread communication, since not all
63 thread implementations support that (at least our thread library
64 doesn't).
65
66 We still have the problem that in some implementations signals
67 generated by the keyboard (e.g. SIGINT) are delivered to all
68 threads (e.g. SGI), while in others (e.g. Solaris) such signals are
69 delivered to one random thread (an intermediate possibility would
Guido van Rossuma3c04b01995-01-12 11:29:01 +000070 be to deliver it to the main thread -- POSIX?). For now, we have
Guido van Rossumbb4ba121994-06-23 11:25:45 +000071 a working implementation that works in all three cases -- the
72 handler ignores signals if getpid() isn't the same as in the main
73 thread. XXX This is a hack.
74
75*/
76
77#ifdef WITH_THREAD
Guido van Rossum295b8e51997-06-06 21:16:41 +000078#include <sys/types.h> /* For pid_t */
Guido van Rossum49b56061998-10-01 20:42:43 +000079#include "pythread.h"
Guido van Rossumbb4ba121994-06-23 11:25:45 +000080static long main_thread;
81static pid_t main_pid;
82#endif
83
Barry Warsaw92971171997-01-03 00:14:25 +000084static struct {
85 int tripped;
86 PyObject *func;
87} Handlers[NSIG];
Guido van Rossum398d9fe1994-05-11 08:59:13 +000088
Barry Warsaw92971171997-01-03 00:14:25 +000089static int is_tripped = 0; /* Speed up sigcheck() when none tripped */
Guido van Rossum398d9fe1994-05-11 08:59:13 +000090
Barry Warsaw92971171997-01-03 00:14:25 +000091static PyObject *DefaultHandler;
92static PyObject *IgnoreHandler;
93static PyObject *IntHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +000094
Guido van Rossum08c16611997-08-02 03:01:42 +000095static RETSIGTYPE (*old_siginthandler)() = SIG_DFL;
96
Barry Warsaw92971171997-01-03 00:14:25 +000097
98
Guido van Rossume4485b01994-09-07 14:32:49 +000099static PyObject *
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
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000113static RETSIGTYPE
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000114signal_handler(int sig_num)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000115{
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000116#ifdef WITH_THREAD
117 /* See NOTES section above */
118 if (getpid() == main_pid) {
119#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000120 is_tripped++;
121 Handlers[sig_num].tripped = 1;
Guido van Rossum644a12b1997-04-09 19:24:53 +0000122 Py_AddPendingCall(
Tim Petersdbd9ba62000-07-09 03:09:57 +0000123 (int (*)(ANY *))PyErr_CheckSignals, NULL);
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000124#ifdef WITH_THREAD
125 }
126#endif
Guido van Rossum602099a1994-09-14 13:32:22 +0000127#ifdef SIGCHLD
128 if (sig_num == SIGCHLD) {
129 /* To avoid infinite recursion, this signal remains
130 reset until explicit re-instated.
131 Don't clear the 'func' field as it is our pointer
132 to the Python handler... */
133 return;
134 }
135#endif
Guido van Rossum1b236761998-09-21 14:46:00 +0000136#ifdef HAVE_SIGINTERRUPT
137 siginterrupt(sig_num, 1);
138#endif
Guido van Rossuma5e54d01998-05-01 18:58:59 +0000139 (void)signal(sig_num, &signal_handler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000140}
Guido van Rossume4485b01994-09-07 14:32:49 +0000141
Guido van Rossum06d511d1995-03-10 15:13:48 +0000142
Barry Warsaw92971171997-01-03 00:14:25 +0000143
Guido van Rossum1171ee61997-08-22 20:42:00 +0000144#ifdef HAVE_ALARM
Guido van Rossume4485b01994-09-07 14:32:49 +0000145static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000146signal_alarm(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000147{
148 int t;
Guido van Rossume4485b01994-09-07 14:32:49 +0000149 if (!PyArg_Parse(args, "i", &t))
Barry Warsaw92971171997-01-03 00:14:25 +0000150 return NULL;
Guido van Rossume4485b01994-09-07 14:32:49 +0000151 /* alarm() returns the number of seconds remaining */
152 return PyInt_FromLong(alarm(t));
Guido van Rossumaa0f4c71994-08-23 13:49:37 +0000153}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000154
155static char alarm_doc[] =
156"alarm(seconds)\n\
157\n\
Guido van Rossum911ec181998-06-28 17:00:19 +0000158Arrange for SIGALRM to arrive after the given number of seconds.";
Guido van Rossum06d511d1995-03-10 15:13:48 +0000159#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000160
Guido van Rossum1171ee61997-08-22 20:42:00 +0000161#ifdef HAVE_PAUSE
Guido van Rossuma597dde1995-01-10 20:56:29 +0000162static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000163signal_pause(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000164{
Guido van Rossume4485b01994-09-07 14:32:49 +0000165 if (!PyArg_NoArgs(args))
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000166 return NULL;
Barry Warsaw92971171997-01-03 00:14:25 +0000167
Guido van Rossuma597dde1995-01-10 20:56:29 +0000168 Py_BEGIN_ALLOW_THREADS
Barry Warsaw92971171997-01-03 00:14:25 +0000169 (void)pause();
Guido van Rossuma597dde1995-01-10 20:56:29 +0000170 Py_END_ALLOW_THREADS
Barry Warsaw92971171997-01-03 00:14:25 +0000171 /* make sure that any exceptions that got raised are propagated
172 * back into Python
173 */
174 if (PyErr_CheckSignals())
175 return NULL;
176
Guido van Rossume4485b01994-09-07 14:32:49 +0000177 Py_INCREF(Py_None);
178 return Py_None;
179}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000180static char pause_doc[] =
Barry Warsaw1ee36ff1998-07-21 22:41:18 +0000181"pause()\n\
182\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000183Wait until a signal arrives.";
184
Guido van Rossum06d511d1995-03-10 15:13:48 +0000185#endif
Guido van Rossume4485b01994-09-07 14:32:49 +0000186
Barry Warsaw92971171997-01-03 00:14:25 +0000187
Guido van Rossume4485b01994-09-07 14:32:49 +0000188static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000189signal_signal(PyObject *self, PyObject *args)
Guido van Rossume4485b01994-09-07 14:32:49 +0000190{
191 PyObject *obj;
192 int sig_num;
193 PyObject *old_handler;
194 RETSIGTYPE (*func)();
195 if (!PyArg_Parse(args, "(iO)", &sig_num, &obj))
Barry Warsaw92971171997-01-03 00:14:25 +0000196 return NULL;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000197#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000198 if (PyThread_get_thread_ident() != main_thread) {
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000199 PyErr_SetString(PyExc_ValueError,
200 "signal only works in main thread");
Barry Warsaw92971171997-01-03 00:14:25 +0000201 return NULL;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000202 }
203#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000204 if (sig_num < 1 || sig_num >= NSIG) {
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000205 PyErr_SetString(PyExc_ValueError,
206 "signal number out of range");
Barry Warsaw92971171997-01-03 00:14:25 +0000207 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000208 }
Barry Warsaw92971171997-01-03 00:14:25 +0000209 if (obj == IgnoreHandler)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000210 func = SIG_IGN;
Barry Warsaw92971171997-01-03 00:14:25 +0000211 else if (obj == DefaultHandler)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000212 func = SIG_DFL;
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000213 else if (!PyCallable_Check(obj)) {
Guido van Rossume4485b01994-09-07 14:32:49 +0000214 PyErr_SetString(PyExc_TypeError,
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000215"signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object");
Barry Warsaw92971171997-01-03 00:14:25 +0000216 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000217 }
218 else
Barry Warsaw92971171997-01-03 00:14:25 +0000219 func = signal_handler;
Guido van Rossum1b236761998-09-21 14:46:00 +0000220#ifdef HAVE_SIGINTERRUPT
221 siginterrupt(sig_num, 1);
222#endif
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000223 if (signal(sig_num, func) == SIG_ERR) {
Guido van Rossume4485b01994-09-07 14:32:49 +0000224 PyErr_SetFromErrno(PyExc_RuntimeError);
Barry Warsaw92971171997-01-03 00:14:25 +0000225 return NULL;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000226 }
Barry Warsaw92971171997-01-03 00:14:25 +0000227 old_handler = Handlers[sig_num].func;
228 Handlers[sig_num].tripped = 0;
Guido van Rossume4485b01994-09-07 14:32:49 +0000229 Py_INCREF(obj);
Barry Warsaw92971171997-01-03 00:14:25 +0000230 Handlers[sig_num].func = obj;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000231 return old_handler;
232}
233
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000234static char signal_doc[] =
235"signal(sig, action) -> action\n\
236\n\
237Set the action for the given signal. The action can be SIG_DFL,\n\
238SIG_IGN, or a callable Python object. The previous action is\n\
239returned. See getsignal() for possible return values.\n\
240\n\
241*** IMPORTANT NOTICE ***\n\
242A signal handler function is called with two arguments:\n\
243the first is the signal number, the second is the interrupted stack frame.";
244
Barry Warsaw92971171997-01-03 00:14:25 +0000245
Guido van Rossume4485b01994-09-07 14:32:49 +0000246static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000247signal_getsignal(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000248{
249 int sig_num;
Guido van Rossume4485b01994-09-07 14:32:49 +0000250 PyObject *old_handler;
251 if (!PyArg_Parse(args, "i", &sig_num))
Barry Warsaw92971171997-01-03 00:14:25 +0000252 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000253 if (sig_num < 1 || sig_num >= NSIG) {
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000254 PyErr_SetString(PyExc_ValueError,
255 "signal number out of range");
Barry Warsaw92971171997-01-03 00:14:25 +0000256 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000257 }
Barry Warsaw92971171997-01-03 00:14:25 +0000258 old_handler = Handlers[sig_num].func;
Guido van Rossume4485b01994-09-07 14:32:49 +0000259 Py_INCREF(old_handler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000260 return old_handler;
261}
262
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000263static char getsignal_doc[] =
264"getsignal(sig) -> action\n\
265\n\
266Return the current action for the given signal. The return value can be:\n\
267SIG_IGN -- if the signal is being ignored\n\
268SIG_DFL -- if the default action for the signal is in effect\n\
269None -- if an unknown handler is in effect\n\
270anything else -- the callable Python object used as a handler\n\
271";
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000272
Barry Warsaw92971171997-01-03 00:14:25 +0000273
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000274/* List of functions defined in the module */
Barry Warsaw92971171997-01-03 00:14:25 +0000275static PyMethodDef signal_methods[] = {
Guido van Rossum1171ee61997-08-22 20:42:00 +0000276#ifdef HAVE_ALARM
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000277 {"alarm", signal_alarm, 0, alarm_doc},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000278#endif
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000279 {"signal", signal_signal, 0, signal_doc},
280 {"getsignal", signal_getsignal, 0, getsignal_doc},
Guido van Rossum1171ee61997-08-22 20:42:00 +0000281#ifdef HAVE_PAUSE
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000282 {"pause", signal_pause, 0, pause_doc},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000283#endif
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000284 {"default_int_handler", signal_default_int_handler, 0,
285 default_int_handler_doc},
286 {NULL, NULL} /* sentinel */
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000287};
288
Barry Warsaw92971171997-01-03 00:14:25 +0000289
290
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000291static char module_doc[] =
292"This module provides mechanisms to use signal handlers in Python.\n\
293\n\
294Functions:\n\
295\n\
296alarm() -- cause SIGALRM after a specified time [Unix only]\n\
297signal() -- set the action for a given signal\n\
298getsignal() -- get the signal action for a given signal\n\
299pause() -- wait until a signal arrives [Unix only]\n\
300default_int_handler() -- default SIGINT handler\n\
301\n\
302Constants:\n\
303\n\
304SIG_DFL -- used to refer to the system default handler\n\
305SIG_IGN -- used to ignore the signal\n\
306NSIG -- number of defined signals\n\
307\n\
308SIGINT, SIGTERM, etc. -- signal numbers\n\
309\n\
310*** IMPORTANT NOTICE ***\n\
311A signal handler function is called with two arguments:\n\
312the first is the signal number, the second is the interrupted stack frame.";
313
Guido van Rossum3886bb61998-12-04 18:50:17 +0000314DL_EXPORT(void)
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000315initsignal(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000316{
Guido van Rossume4485b01994-09-07 14:32:49 +0000317 PyObject *m, *d, *x;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000318 int i;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000319
320#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000321 main_thread = PyThread_get_thread_ident();
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000322 main_pid = getpid();
323#endif
324
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000325 /* Create the module and add the functions */
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000326 m = Py_InitModule3("signal", signal_methods, module_doc);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000327
328 /* Add some symbolic constants to the module */
Guido van Rossume4485b01994-09-07 14:32:49 +0000329 d = PyModule_GetDict(m);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000330
Guido van Rossum276fa432000-06-30 23:04:18 +0000331 x = DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL);
Barry Warsaw92971171997-01-03 00:14:25 +0000332 if (!x || PyDict_SetItemString(d, "SIG_DFL", x) < 0)
333 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000334
Guido van Rossum276fa432000-06-30 23:04:18 +0000335 x = IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN);
Barry Warsaw92971171997-01-03 00:14:25 +0000336 if (!x || PyDict_SetItemString(d, "SIG_IGN", x) < 0)
337 goto finally;
338
339 x = PyInt_FromLong((long)NSIG);
340 if (!x || PyDict_SetItemString(d, "NSIG", x) < 0)
341 goto finally;
Guido van Rossum08c16611997-08-02 03:01:42 +0000342 Py_DECREF(x);
Barry Warsaw92971171997-01-03 00:14:25 +0000343
344 x = IntHandler = PyDict_GetItemString(d, "default_int_handler");
345 if (!x)
346 goto finally;
Guido van Rossum08c16611997-08-02 03:01:42 +0000347 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +0000348
349 Handlers[0].tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000350 for (i = 1; i < NSIG; i++) {
Guido van Rossum6299d1e1994-05-31 12:51:13 +0000351 RETSIGTYPE (*t)();
Guido van Rossumfb0df941996-05-23 22:55:35 +0000352#ifdef HAVE_SIGACTION
353 struct sigaction act;
354 sigaction(i, 0, &act);
Guido van Rossum1835c4f1996-05-29 14:15:19 +0000355 t = act.sa_handler;
Guido van Rossumfb0df941996-05-23 22:55:35 +0000356#else
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000357 t = signal(i, SIG_IGN);
358 signal(i, t);
Guido van Rossumfb0df941996-05-23 22:55:35 +0000359#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000360 Handlers[i].tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000361 if (t == SIG_DFL)
Barry Warsaw92971171997-01-03 00:14:25 +0000362 Handlers[i].func = DefaultHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000363 else if (t == SIG_IGN)
Barry Warsaw92971171997-01-03 00:14:25 +0000364 Handlers[i].func = IgnoreHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000365 else
Barry Warsaw92971171997-01-03 00:14:25 +0000366 Handlers[i].func = Py_None; /* None of our business */
367 Py_INCREF(Handlers[i].func);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000368 }
Barry Warsaw92971171997-01-03 00:14:25 +0000369 if (Handlers[SIGINT].func == DefaultHandler) {
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000370 /* Install default int handler */
Guido van Rossum08c16611997-08-02 03:01:42 +0000371 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +0000372 Py_DECREF(Handlers[SIGINT].func);
373 Handlers[SIGINT].func = IntHandler;
Guido van Rossum08c16611997-08-02 03:01:42 +0000374 old_siginthandler = signal(SIGINT, &signal_handler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000375 }
376
377#ifdef SIGHUP
Guido van Rossume4485b01994-09-07 14:32:49 +0000378 x = PyInt_FromLong(SIGHUP);
379 PyDict_SetItemString(d, "SIGHUP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000380 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000381#endif
382#ifdef SIGINT
Guido van Rossume4485b01994-09-07 14:32:49 +0000383 x = PyInt_FromLong(SIGINT);
384 PyDict_SetItemString(d, "SIGINT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000385 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000386#endif
387#ifdef SIGQUIT
Guido van Rossume4485b01994-09-07 14:32:49 +0000388 x = PyInt_FromLong(SIGQUIT);
389 PyDict_SetItemString(d, "SIGQUIT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000390 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000391#endif
392#ifdef SIGILL
Guido van Rossume4485b01994-09-07 14:32:49 +0000393 x = PyInt_FromLong(SIGILL);
394 PyDict_SetItemString(d, "SIGILL", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000395 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000396#endif
397#ifdef SIGTRAP
Guido van Rossume4485b01994-09-07 14:32:49 +0000398 x = PyInt_FromLong(SIGTRAP);
399 PyDict_SetItemString(d, "SIGTRAP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000400 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000401#endif
402#ifdef SIGIOT
Guido van Rossume4485b01994-09-07 14:32:49 +0000403 x = PyInt_FromLong(SIGIOT);
404 PyDict_SetItemString(d, "SIGIOT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000405 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000406#endif
407#ifdef SIGABRT
Guido van Rossume4485b01994-09-07 14:32:49 +0000408 x = PyInt_FromLong(SIGABRT);
409 PyDict_SetItemString(d, "SIGABRT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000410 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000411#endif
412#ifdef SIGEMT
Guido van Rossume4485b01994-09-07 14:32:49 +0000413 x = PyInt_FromLong(SIGEMT);
414 PyDict_SetItemString(d, "SIGEMT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000415 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000416#endif
417#ifdef SIGFPE
Guido van Rossume4485b01994-09-07 14:32:49 +0000418 x = PyInt_FromLong(SIGFPE);
419 PyDict_SetItemString(d, "SIGFPE", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000420 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000421#endif
422#ifdef SIGKILL
Guido van Rossume4485b01994-09-07 14:32:49 +0000423 x = PyInt_FromLong(SIGKILL);
424 PyDict_SetItemString(d, "SIGKILL", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000425 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000426#endif
427#ifdef SIGBUS
Guido van Rossume4485b01994-09-07 14:32:49 +0000428 x = PyInt_FromLong(SIGBUS);
429 PyDict_SetItemString(d, "SIGBUS", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000430 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000431#endif
432#ifdef SIGSEGV
Guido van Rossume4485b01994-09-07 14:32:49 +0000433 x = PyInt_FromLong(SIGSEGV);
434 PyDict_SetItemString(d, "SIGSEGV", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000435 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000436#endif
437#ifdef SIGSYS
Guido van Rossume4485b01994-09-07 14:32:49 +0000438 x = PyInt_FromLong(SIGSYS);
439 PyDict_SetItemString(d, "SIGSYS", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000440 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000441#endif
442#ifdef SIGPIPE
Guido van Rossume4485b01994-09-07 14:32:49 +0000443 x = PyInt_FromLong(SIGPIPE);
444 PyDict_SetItemString(d, "SIGPIPE", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000445 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000446#endif
447#ifdef SIGALRM
Guido van Rossume4485b01994-09-07 14:32:49 +0000448 x = PyInt_FromLong(SIGALRM);
449 PyDict_SetItemString(d, "SIGALRM", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000450 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000451#endif
452#ifdef SIGTERM
Guido van Rossume4485b01994-09-07 14:32:49 +0000453 x = PyInt_FromLong(SIGTERM);
454 PyDict_SetItemString(d, "SIGTERM", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000455 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000456#endif
457#ifdef SIGUSR1
Guido van Rossume4485b01994-09-07 14:32:49 +0000458 x = PyInt_FromLong(SIGUSR1);
459 PyDict_SetItemString(d, "SIGUSR1", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000460 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000461#endif
462#ifdef SIGUSR2
Guido van Rossume4485b01994-09-07 14:32:49 +0000463 x = PyInt_FromLong(SIGUSR2);
464 PyDict_SetItemString(d, "SIGUSR2", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000465 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000466#endif
467#ifdef SIGCLD
Guido van Rossume4485b01994-09-07 14:32:49 +0000468 x = PyInt_FromLong(SIGCLD);
469 PyDict_SetItemString(d, "SIGCLD", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000470 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000471#endif
472#ifdef SIGCHLD
Guido van Rossume4485b01994-09-07 14:32:49 +0000473 x = PyInt_FromLong(SIGCHLD);
474 PyDict_SetItemString(d, "SIGCHLD", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000475 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000476#endif
477#ifdef SIGPWR
Guido van Rossume4485b01994-09-07 14:32:49 +0000478 x = PyInt_FromLong(SIGPWR);
479 PyDict_SetItemString(d, "SIGPWR", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000480 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000481#endif
482#ifdef SIGIO
Guido van Rossume4485b01994-09-07 14:32:49 +0000483 x = PyInt_FromLong(SIGIO);
484 PyDict_SetItemString(d, "SIGIO", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000485 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000486#endif
487#ifdef SIGURG
Guido van Rossume4485b01994-09-07 14:32:49 +0000488 x = PyInt_FromLong(SIGURG);
489 PyDict_SetItemString(d, "SIGURG", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000490 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000491#endif
492#ifdef SIGWINCH
Guido van Rossume4485b01994-09-07 14:32:49 +0000493 x = PyInt_FromLong(SIGWINCH);
494 PyDict_SetItemString(d, "SIGWINCH", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000495 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000496#endif
497#ifdef SIGPOLL
Guido van Rossume4485b01994-09-07 14:32:49 +0000498 x = PyInt_FromLong(SIGPOLL);
499 PyDict_SetItemString(d, "SIGPOLL", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000500 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000501#endif
502#ifdef SIGSTOP
Guido van Rossume4485b01994-09-07 14:32:49 +0000503 x = PyInt_FromLong(SIGSTOP);
504 PyDict_SetItemString(d, "SIGSTOP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000505 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000506#endif
507#ifdef SIGTSTP
Guido van Rossume4485b01994-09-07 14:32:49 +0000508 x = PyInt_FromLong(SIGTSTP);
509 PyDict_SetItemString(d, "SIGTSTP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000510 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000511#endif
512#ifdef SIGCONT
Guido van Rossume4485b01994-09-07 14:32:49 +0000513 x = PyInt_FromLong(SIGCONT);
514 PyDict_SetItemString(d, "SIGCONT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000515 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000516#endif
517#ifdef SIGTTIN
Guido van Rossume4485b01994-09-07 14:32:49 +0000518 x = PyInt_FromLong(SIGTTIN);
519 PyDict_SetItemString(d, "SIGTTIN", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000520 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000521#endif
522#ifdef SIGTTOU
Guido van Rossume4485b01994-09-07 14:32:49 +0000523 x = PyInt_FromLong(SIGTTOU);
524 PyDict_SetItemString(d, "SIGTTOU", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000525 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000526#endif
527#ifdef SIGVTALRM
Guido van Rossume4485b01994-09-07 14:32:49 +0000528 x = PyInt_FromLong(SIGVTALRM);
529 PyDict_SetItemString(d, "SIGVTALRM", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000530 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000531#endif
532#ifdef SIGPROF
Guido van Rossume4485b01994-09-07 14:32:49 +0000533 x = PyInt_FromLong(SIGPROF);
534 PyDict_SetItemString(d, "SIGPROF", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000535 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000536#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000537#ifdef SIGXCPU
538 x = PyInt_FromLong(SIGXCPU);
539 PyDict_SetItemString(d, "SIGXCPU", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000540 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000541#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000542#ifdef SIGXFSZ
543 x = PyInt_FromLong(SIGXFSZ);
544 PyDict_SetItemString(d, "SIGXFSZ", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000545 Py_XDECREF(x);
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000546#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000547 if (!PyErr_Occurred())
548 return;
549
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000550 /* Check for errors */
Barry Warsaw92971171997-01-03 00:14:25 +0000551 finally:
Guido van Rossum08c16611997-08-02 03:01:42 +0000552 return;
553}
554
555static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000556finisignal(void)
Guido van Rossum08c16611997-08-02 03:01:42 +0000557{
558 int i;
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000559 PyObject *func;
Guido van Rossum08c16611997-08-02 03:01:42 +0000560
561 signal(SIGINT, old_siginthandler);
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000562 old_siginthandler = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +0000563
564 for (i = 1; i < NSIG; i++) {
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000565 func = Handlers[i].func;
Guido van Rossum08c16611997-08-02 03:01:42 +0000566 Handlers[i].tripped = 0;
Guido van Rossum08c16611997-08-02 03:01:42 +0000567 Handlers[i].func = NULL;
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000568 if (i != SIGINT && func != NULL && func != Py_None &&
569 func != DefaultHandler && func != IgnoreHandler)
570 signal(i, SIG_DFL);
571 Py_XDECREF(func);
Guido van Rossum08c16611997-08-02 03:01:42 +0000572 }
573
574 Py_XDECREF(IntHandler);
575 IntHandler = NULL;
576 Py_XDECREF(DefaultHandler);
577 DefaultHandler = NULL;
578 Py_XDECREF(IgnoreHandler);
579 IgnoreHandler = NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000580}
581
Barry Warsaw92971171997-01-03 00:14:25 +0000582
583
584/* Declared in pyerrors.h */
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000585int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000586PyErr_CheckSignals(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000587{
588 int i;
Guido van Rossume4485b01994-09-07 14:32:49 +0000589 PyObject *f;
Barry Warsaw92971171997-01-03 00:14:25 +0000590
591 if (!is_tripped)
Guido van Rossum6299d1e1994-05-31 12:51:13 +0000592 return 0;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000593#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000594 if (PyThread_get_thread_ident() != main_thread)
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000595 return 0;
596#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000597 if (!(f = PyEval_GetFrame()))
Guido van Rossume4485b01994-09-07 14:32:49 +0000598 f = Py_None;
Barry Warsaw92971171997-01-03 00:14:25 +0000599
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000600 for (i = 1; i < NSIG; i++) {
Barry Warsaw92971171997-01-03 00:14:25 +0000601 if (Handlers[i].tripped) {
602 PyObject *result = NULL;
603 PyObject *arglist = Py_BuildValue("(iO)", i, f);
604 Handlers[i].tripped = 0;
605
606 if (arglist) {
607 result = PyEval_CallObject(Handlers[i].func,
608 arglist);
Guido van Rossume4485b01994-09-07 14:32:49 +0000609 Py_DECREF(arglist);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000610 }
Barry Warsaw92971171997-01-03 00:14:25 +0000611 if (!result)
Guido van Rossumfcdd0e41997-01-21 06:13:09 +0000612 return -1;
Barry Warsaw92971171997-01-03 00:14:25 +0000613
614 Py_DECREF(result);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000615 }
616 }
Barry Warsaw92971171997-01-03 00:14:25 +0000617 is_tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000618 return 0;
619}
620
Barry Warsaw92971171997-01-03 00:14:25 +0000621
622/* Replacements for intrcheck.c functionality
623 * Declared in pyerrors.h
624 */
625void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000626PyErr_SetInterrupt(void)
Barry Warsaw92971171997-01-03 00:14:25 +0000627{
628 is_tripped++;
629 Handlers[SIGINT].tripped = 1;
Tim Petersdbd9ba62000-07-09 03:09:57 +0000630 Py_AddPendingCall((int (*)(ANY *))PyErr_CheckSignals, NULL);
Barry Warsaw92971171997-01-03 00:14:25 +0000631}
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000632
633void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000634PyOS_InitInterrupts(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000635{
636 initsignal();
Guido van Rossum08c16611997-08-02 03:01:42 +0000637 _PyImport_FixupExtension("signal", "signal");
638}
639
640void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000641PyOS_FiniInterrupts(void)
Guido van Rossum08c16611997-08-02 03:01:42 +0000642{
643 finisignal();
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000644}
645
646int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000647PyOS_InterruptOccurred(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000648{
Barry Warsaw92971171997-01-03 00:14:25 +0000649 if (Handlers[SIGINT].tripped) {
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000650#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000651 if (PyThread_get_thread_ident() != main_thread)
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000652 return 0;
653#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000654 Handlers[SIGINT].tripped = 0;
Guido van Rossum6299d1e1994-05-31 12:51:13 +0000655 return 1;
656 }
657 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000658}
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000659
660void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000661PyOS_AfterFork(void)
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000662{
663#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000664 main_thread = PyThread_get_thread_ident();
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000665 main_pid = getpid();
666#endif
667}