blob: 850221ea326c733bc94037f8f2b2a13465a65319 [file] [log] [blame]
Guido van Rossum398d9fe1994-05-11 08:59:13 +00001
Guido van Rossum3bbc62e1995-01-02 19:30:30 +00002/* Signal module -- many thanks to Lance Ellinghaus */
Guido van Rossum398d9fe1994-05-11 08:59:13 +00003
Guido van Rossum644a12b1997-04-09 19:24:53 +00004/* XXX Signals should be recorded per thread, now we have thread state. */
5
Guido van Rossum602099a1994-09-14 13:32:22 +00006#include "Python.h"
Guido van Rossum398d9fe1994-05-11 08:59:13 +00007#include "intrcheck.h"
8
Guido van Rossum644a12b1997-04-09 19:24:53 +00009#ifdef MS_WIN32
10#include <process.h>
11#endif
12
Guido van Rossuma376cc51996-12-05 23:43:35 +000013#ifdef HAVE_UNISTD_H
14#include <unistd.h>
15#endif
16
Guido van Rossum398d9fe1994-05-11 08:59:13 +000017#include <signal.h>
18
Guido van Rossumbb4ba121994-06-23 11:25:45 +000019#ifndef SIG_ERR
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +000020#define SIG_ERR ((PyOS_sighandler_t)(-1))
Guido van Rossumbb4ba121994-06-23 11:25:45 +000021#endif
22
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000023#if defined(PYOS_OS2)
24#define NSIG 12
25#include <process.h>
26#endif
27
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000028#ifndef NSIG
Marc-André Lemburg8bcfb8a2000-07-04 14:17:33 +000029# if defined(_NSIG)
30# define NSIG _NSIG /* For BSD/SysV */
31# elif defined(_SIGMAX)
32# define NSIG (_SIGMAX + 1) /* For QNX */
33# elif defined(SIGMAX)
34# define NSIG (SIGMAX + 1) /* For djgpp */
35# else
36# define NSIG 64 /* Use a reasonable default value */
37# endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000038#endif
39
40
Guido van Rossumbb4ba121994-06-23 11:25:45 +000041/*
42 NOTES ON THE INTERACTION BETWEEN SIGNALS AND THREADS
43
44 When threads are supported, we want the following semantics:
45
46 - only the main thread can set a signal handler
47 - any thread can get a signal handler
48 - signals are only delivered to the main thread
49
50 I.e. we don't support "synchronous signals" like SIGFPE (catching
51 this doesn't make much sense in Python anyway) nor do we support
52 signals as a means of inter-thread communication, since not all
53 thread implementations support that (at least our thread library
54 doesn't).
55
56 We still have the problem that in some implementations signals
57 generated by the keyboard (e.g. SIGINT) are delivered to all
58 threads (e.g. SGI), while in others (e.g. Solaris) such signals are
59 delivered to one random thread (an intermediate possibility would
Guido van Rossuma3c04b01995-01-12 11:29:01 +000060 be to deliver it to the main thread -- POSIX?). For now, we have
Guido van Rossumbb4ba121994-06-23 11:25:45 +000061 a working implementation that works in all three cases -- the
62 handler ignores signals if getpid() isn't the same as in the main
63 thread. XXX This is a hack.
64
Guido van Rossum9e8181b2000-09-19 00:46:46 +000065 GNU pth is a user-space threading library, and as such, all threads
66 run within the same process. In this case, if the currently running
67 thread is not the main_thread, send the signal to the main_thread.
Guido van Rossumbb4ba121994-06-23 11:25:45 +000068*/
69
70#ifdef WITH_THREAD
Guido van Rossum295b8e51997-06-06 21:16:41 +000071#include <sys/types.h> /* For pid_t */
Guido van Rossum49b56061998-10-01 20:42:43 +000072#include "pythread.h"
Guido van Rossumbb4ba121994-06-23 11:25:45 +000073static long main_thread;
74static pid_t main_pid;
75#endif
76
Barry Warsaw92971171997-01-03 00:14:25 +000077static struct {
78 int tripped;
79 PyObject *func;
80} Handlers[NSIG];
Guido van Rossum398d9fe1994-05-11 08:59:13 +000081
Barry Warsaw92971171997-01-03 00:14:25 +000082static int is_tripped = 0; /* Speed up sigcheck() when none tripped */
Guido van Rossum398d9fe1994-05-11 08:59:13 +000083
Barry Warsaw92971171997-01-03 00:14:25 +000084static PyObject *DefaultHandler;
85static PyObject *IgnoreHandler;
86static PyObject *IntHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +000087
Martin v. Löwisf58de1b2001-03-06 12:13:56 +000088/* On Solaris 8, gcc will produce a warning that the function
89 declaration is not a prototype. This is caused by the definition of
90 SIG_DFL as (void (*)())0; the correct declaration would have been
91 (void (*)(int))0. */
92
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +000093static PyOS_sighandler_t old_siginthandler = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +000094
Barry Warsaw92971171997-01-03 00:14:25 +000095
Guido van Rossume4485b01994-09-07 14:32:49 +000096static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +000097signal_default_int_handler(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +000098{
Guido van Rossume4485b01994-09-07 14:32:49 +000099 PyErr_SetNone(PyExc_KeyboardInterrupt);
Barry Warsaw92971171997-01-03 00:14:25 +0000100 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000101}
102
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000103static char default_int_handler_doc[] =
104"default_int_handler(...)\n\
105\n\
106The default handler for SIGINT instated by Python.\n\
107It raises KeyboardInterrupt.";
108
Thomas Wouters0796b002000-07-22 23:49:30 +0000109
110static int
111checksignals_witharg(void * unused)
112{
113 return PyErr_CheckSignals();
114}
115
Tim Peters4f1b2082000-07-23 21:18:09 +0000116static void
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000117signal_handler(int sig_num)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000118{
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000119#ifdef WITH_THREAD
Guido van Rossum9e8181b2000-09-19 00:46:46 +0000120#ifdef WITH_PTH
121 if (PyThread_get_thread_ident() != main_thread) {
122 pth_raise(*(pth_t *) main_thread, sig_num);
123 return;
124 }
125#endif
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000126 /* See NOTES section above */
127 if (getpid() == main_pid) {
128#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000129 is_tripped++;
130 Handlers[sig_num].tripped = 1;
Thomas Wouters0796b002000-07-22 23:49:30 +0000131 Py_AddPendingCall(checksignals_witharg, NULL);
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000132#ifdef WITH_THREAD
133 }
134#endif
Guido van Rossum602099a1994-09-14 13:32:22 +0000135#ifdef SIGCHLD
136 if (sig_num == SIGCHLD) {
137 /* To avoid infinite recursion, this signal remains
138 reset until explicit re-instated.
139 Don't clear the 'func' field as it is our pointer
140 to the Python handler... */
Tim Peters4f1b2082000-07-23 21:18:09 +0000141 return;
Guido van Rossum602099a1994-09-14 13:32:22 +0000142 }
143#endif
Guido van Rossum1b236761998-09-21 14:46:00 +0000144#ifdef HAVE_SIGINTERRUPT
145 siginterrupt(sig_num, 1);
146#endif
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000147 PyOS_setsig(sig_num, signal_handler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000148}
Guido van Rossume4485b01994-09-07 14:32:49 +0000149
Guido van Rossum06d511d1995-03-10 15:13:48 +0000150
Guido van Rossum1171ee61997-08-22 20:42:00 +0000151#ifdef HAVE_ALARM
Guido van Rossume4485b01994-09-07 14:32:49 +0000152static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000153signal_alarm(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000154{
155 int t;
Guido van Rossume4485b01994-09-07 14:32:49 +0000156 if (!PyArg_Parse(args, "i", &t))
Barry Warsaw92971171997-01-03 00:14:25 +0000157 return NULL;
Guido van Rossume4485b01994-09-07 14:32:49 +0000158 /* alarm() returns the number of seconds remaining */
Fred Drakedff3a372001-07-19 21:29:49 +0000159 return PyInt_FromLong((long)alarm(t));
Guido van Rossumaa0f4c71994-08-23 13:49:37 +0000160}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000161
162static char alarm_doc[] =
163"alarm(seconds)\n\
164\n\
Guido van Rossum911ec181998-06-28 17:00:19 +0000165Arrange for SIGALRM to arrive after the given number of seconds.";
Guido van Rossum06d511d1995-03-10 15:13:48 +0000166#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000167
Guido van Rossum1171ee61997-08-22 20:42:00 +0000168#ifdef HAVE_PAUSE
Guido van Rossuma597dde1995-01-10 20:56:29 +0000169static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000170signal_pause(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000171{
Guido van Rossume4485b01994-09-07 14:32:49 +0000172 if (!PyArg_NoArgs(args))
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000173 return NULL;
Barry Warsaw92971171997-01-03 00:14:25 +0000174
Guido van Rossuma597dde1995-01-10 20:56:29 +0000175 Py_BEGIN_ALLOW_THREADS
Barry Warsaw92971171997-01-03 00:14:25 +0000176 (void)pause();
Guido van Rossuma597dde1995-01-10 20:56:29 +0000177 Py_END_ALLOW_THREADS
Barry Warsaw92971171997-01-03 00:14:25 +0000178 /* make sure that any exceptions that got raised are propagated
179 * back into Python
180 */
181 if (PyErr_CheckSignals())
182 return NULL;
183
Guido van Rossume4485b01994-09-07 14:32:49 +0000184 Py_INCREF(Py_None);
185 return Py_None;
186}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000187static char pause_doc[] =
Barry Warsaw1ee36ff1998-07-21 22:41:18 +0000188"pause()\n\
189\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000190Wait until a signal arrives.";
191
Guido van Rossum06d511d1995-03-10 15:13:48 +0000192#endif
Guido van Rossume4485b01994-09-07 14:32:49 +0000193
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000194
Guido van Rossume4485b01994-09-07 14:32:49 +0000195static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000196signal_signal(PyObject *self, PyObject *args)
Guido van Rossume4485b01994-09-07 14:32:49 +0000197{
198 PyObject *obj;
199 int sig_num;
200 PyObject *old_handler;
Tim Peters4f1b2082000-07-23 21:18:09 +0000201 void (*func)(int);
Guido van Rossume4485b01994-09-07 14:32:49 +0000202 if (!PyArg_Parse(args, "(iO)", &sig_num, &obj))
Barry Warsaw92971171997-01-03 00:14:25 +0000203 return NULL;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000204#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000205 if (PyThread_get_thread_ident() != main_thread) {
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000206 PyErr_SetString(PyExc_ValueError,
207 "signal only works in main thread");
Barry Warsaw92971171997-01-03 00:14:25 +0000208 return NULL;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000209 }
210#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000211 if (sig_num < 1 || sig_num >= NSIG) {
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000212 PyErr_SetString(PyExc_ValueError,
213 "signal number out of range");
Barry Warsaw92971171997-01-03 00:14:25 +0000214 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000215 }
Barry Warsaw92971171997-01-03 00:14:25 +0000216 if (obj == IgnoreHandler)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000217 func = SIG_IGN;
Barry Warsaw92971171997-01-03 00:14:25 +0000218 else if (obj == DefaultHandler)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000219 func = SIG_DFL;
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000220 else if (!PyCallable_Check(obj)) {
Guido van Rossume4485b01994-09-07 14:32:49 +0000221 PyErr_SetString(PyExc_TypeError,
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000222"signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object");
Barry Warsaw92971171997-01-03 00:14:25 +0000223 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000224 }
225 else
Barry Warsaw92971171997-01-03 00:14:25 +0000226 func = signal_handler;
Guido van Rossum1b236761998-09-21 14:46:00 +0000227#ifdef HAVE_SIGINTERRUPT
228 siginterrupt(sig_num, 1);
229#endif
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000230 if (PyOS_setsig(sig_num, func) == SIG_ERR) {
Guido van Rossume4485b01994-09-07 14:32:49 +0000231 PyErr_SetFromErrno(PyExc_RuntimeError);
Barry Warsaw92971171997-01-03 00:14:25 +0000232 return NULL;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000233 }
Barry Warsaw92971171997-01-03 00:14:25 +0000234 old_handler = Handlers[sig_num].func;
235 Handlers[sig_num].tripped = 0;
Guido van Rossume4485b01994-09-07 14:32:49 +0000236 Py_INCREF(obj);
Barry Warsaw92971171997-01-03 00:14:25 +0000237 Handlers[sig_num].func = obj;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000238 return old_handler;
239}
240
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000241static char signal_doc[] =
242"signal(sig, action) -> action\n\
243\n\
244Set the action for the given signal. The action can be SIG_DFL,\n\
245SIG_IGN, or a callable Python object. The previous action is\n\
246returned. See getsignal() for possible return values.\n\
247\n\
248*** IMPORTANT NOTICE ***\n\
249A signal handler function is called with two arguments:\n\
250the first is the signal number, the second is the interrupted stack frame.";
251
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000252
Guido van Rossume4485b01994-09-07 14:32:49 +0000253static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000254signal_getsignal(PyObject *self, 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
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +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
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000284 {"alarm", signal_alarm, METH_OLDARGS, alarm_doc},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000285#endif
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000286 {"signal", signal_signal, METH_OLDARGS, signal_doc},
287 {"getsignal", signal_getsignal, METH_OLDARGS, getsignal_doc},
Guido van Rossum1171ee61997-08-22 20:42:00 +0000288#ifdef HAVE_PAUSE
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000289 {"pause", signal_pause, METH_OLDARGS, pause_doc},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000290#endif
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000291 {"default_int_handler", signal_default_int_handler,
292 METH_OLDARGS, default_int_handler_doc},
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000293 {NULL, NULL} /* sentinel */
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000294};
295
Barry Warsaw92971171997-01-03 00:14:25 +0000296
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 Rossumd2cd7ad2000-09-16 16:35:28 +0000358 t = PyOS_getsig(i);
Barry Warsaw92971171997-01-03 00:14:25 +0000359 Handlers[i].tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000360 if (t == SIG_DFL)
Barry Warsaw92971171997-01-03 00:14:25 +0000361 Handlers[i].func = DefaultHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000362 else if (t == SIG_IGN)
Barry Warsaw92971171997-01-03 00:14:25 +0000363 Handlers[i].func = IgnoreHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000364 else
Barry Warsaw92971171997-01-03 00:14:25 +0000365 Handlers[i].func = Py_None; /* None of our business */
366 Py_INCREF(Handlers[i].func);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000367 }
Barry Warsaw92971171997-01-03 00:14:25 +0000368 if (Handlers[SIGINT].func == DefaultHandler) {
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000369 /* Install default int handler */
Guido van Rossum08c16611997-08-02 03:01:42 +0000370 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +0000371 Py_DECREF(Handlers[SIGINT].func);
372 Handlers[SIGINT].func = IntHandler;
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000373 old_siginthandler = PyOS_setsig(SIGINT, &signal_handler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000374 }
375
376#ifdef SIGHUP
Guido van Rossume4485b01994-09-07 14:32:49 +0000377 x = PyInt_FromLong(SIGHUP);
378 PyDict_SetItemString(d, "SIGHUP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000379 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000380#endif
381#ifdef SIGINT
Guido van Rossume4485b01994-09-07 14:32:49 +0000382 x = PyInt_FromLong(SIGINT);
383 PyDict_SetItemString(d, "SIGINT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000384 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000385#endif
Tim Peters1ce3cf72001-10-01 17:58:40 +0000386#ifdef SIGBREAK
387 x = PyInt_FromLong(SIGBREAK);
388 PyDict_SetItemString(d, "SIGBREAK", x);
389 Py_XDECREF(x);
390#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000391#ifdef SIGQUIT
Guido van Rossume4485b01994-09-07 14:32:49 +0000392 x = PyInt_FromLong(SIGQUIT);
393 PyDict_SetItemString(d, "SIGQUIT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000394 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000395#endif
396#ifdef SIGILL
Guido van Rossume4485b01994-09-07 14:32:49 +0000397 x = PyInt_FromLong(SIGILL);
398 PyDict_SetItemString(d, "SIGILL", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000399 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000400#endif
401#ifdef SIGTRAP
Guido van Rossume4485b01994-09-07 14:32:49 +0000402 x = PyInt_FromLong(SIGTRAP);
403 PyDict_SetItemString(d, "SIGTRAP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000404 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000405#endif
406#ifdef SIGIOT
Guido van Rossume4485b01994-09-07 14:32:49 +0000407 x = PyInt_FromLong(SIGIOT);
408 PyDict_SetItemString(d, "SIGIOT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000409 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000410#endif
411#ifdef SIGABRT
Guido van Rossume4485b01994-09-07 14:32:49 +0000412 x = PyInt_FromLong(SIGABRT);
413 PyDict_SetItemString(d, "SIGABRT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000414 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000415#endif
416#ifdef SIGEMT
Guido van Rossume4485b01994-09-07 14:32:49 +0000417 x = PyInt_FromLong(SIGEMT);
418 PyDict_SetItemString(d, "SIGEMT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000419 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000420#endif
421#ifdef SIGFPE
Guido van Rossume4485b01994-09-07 14:32:49 +0000422 x = PyInt_FromLong(SIGFPE);
423 PyDict_SetItemString(d, "SIGFPE", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000424 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000425#endif
426#ifdef SIGKILL
Guido van Rossume4485b01994-09-07 14:32:49 +0000427 x = PyInt_FromLong(SIGKILL);
428 PyDict_SetItemString(d, "SIGKILL", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000429 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000430#endif
431#ifdef SIGBUS
Guido van Rossume4485b01994-09-07 14:32:49 +0000432 x = PyInt_FromLong(SIGBUS);
433 PyDict_SetItemString(d, "SIGBUS", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000434 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000435#endif
436#ifdef SIGSEGV
Guido van Rossume4485b01994-09-07 14:32:49 +0000437 x = PyInt_FromLong(SIGSEGV);
438 PyDict_SetItemString(d, "SIGSEGV", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000439 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000440#endif
441#ifdef SIGSYS
Guido van Rossume4485b01994-09-07 14:32:49 +0000442 x = PyInt_FromLong(SIGSYS);
443 PyDict_SetItemString(d, "SIGSYS", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000444 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000445#endif
446#ifdef SIGPIPE
Guido van Rossume4485b01994-09-07 14:32:49 +0000447 x = PyInt_FromLong(SIGPIPE);
448 PyDict_SetItemString(d, "SIGPIPE", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000449 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000450#endif
451#ifdef SIGALRM
Guido van Rossume4485b01994-09-07 14:32:49 +0000452 x = PyInt_FromLong(SIGALRM);
453 PyDict_SetItemString(d, "SIGALRM", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000454 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000455#endif
456#ifdef SIGTERM
Guido van Rossume4485b01994-09-07 14:32:49 +0000457 x = PyInt_FromLong(SIGTERM);
458 PyDict_SetItemString(d, "SIGTERM", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000459 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000460#endif
461#ifdef SIGUSR1
Guido van Rossume4485b01994-09-07 14:32:49 +0000462 x = PyInt_FromLong(SIGUSR1);
463 PyDict_SetItemString(d, "SIGUSR1", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000464 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000465#endif
466#ifdef SIGUSR2
Guido van Rossume4485b01994-09-07 14:32:49 +0000467 x = PyInt_FromLong(SIGUSR2);
468 PyDict_SetItemString(d, "SIGUSR2", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000469 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000470#endif
471#ifdef SIGCLD
Guido van Rossume4485b01994-09-07 14:32:49 +0000472 x = PyInt_FromLong(SIGCLD);
473 PyDict_SetItemString(d, "SIGCLD", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000474 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000475#endif
476#ifdef SIGCHLD
Guido van Rossume4485b01994-09-07 14:32:49 +0000477 x = PyInt_FromLong(SIGCHLD);
478 PyDict_SetItemString(d, "SIGCHLD", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000479 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000480#endif
481#ifdef SIGPWR
Guido van Rossume4485b01994-09-07 14:32:49 +0000482 x = PyInt_FromLong(SIGPWR);
483 PyDict_SetItemString(d, "SIGPWR", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000484 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000485#endif
486#ifdef SIGIO
Guido van Rossume4485b01994-09-07 14:32:49 +0000487 x = PyInt_FromLong(SIGIO);
488 PyDict_SetItemString(d, "SIGIO", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000489 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000490#endif
491#ifdef SIGURG
Guido van Rossume4485b01994-09-07 14:32:49 +0000492 x = PyInt_FromLong(SIGURG);
493 PyDict_SetItemString(d, "SIGURG", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000494 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000495#endif
496#ifdef SIGWINCH
Guido van Rossume4485b01994-09-07 14:32:49 +0000497 x = PyInt_FromLong(SIGWINCH);
498 PyDict_SetItemString(d, "SIGWINCH", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000499 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000500#endif
501#ifdef SIGPOLL
Guido van Rossume4485b01994-09-07 14:32:49 +0000502 x = PyInt_FromLong(SIGPOLL);
503 PyDict_SetItemString(d, "SIGPOLL", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000504 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000505#endif
506#ifdef SIGSTOP
Guido van Rossume4485b01994-09-07 14:32:49 +0000507 x = PyInt_FromLong(SIGSTOP);
508 PyDict_SetItemString(d, "SIGSTOP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000509 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000510#endif
511#ifdef SIGTSTP
Guido van Rossume4485b01994-09-07 14:32:49 +0000512 x = PyInt_FromLong(SIGTSTP);
513 PyDict_SetItemString(d, "SIGTSTP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000514 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000515#endif
516#ifdef SIGCONT
Guido van Rossume4485b01994-09-07 14:32:49 +0000517 x = PyInt_FromLong(SIGCONT);
518 PyDict_SetItemString(d, "SIGCONT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000519 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000520#endif
521#ifdef SIGTTIN
Guido van Rossume4485b01994-09-07 14:32:49 +0000522 x = PyInt_FromLong(SIGTTIN);
523 PyDict_SetItemString(d, "SIGTTIN", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000524 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000525#endif
526#ifdef SIGTTOU
Guido van Rossume4485b01994-09-07 14:32:49 +0000527 x = PyInt_FromLong(SIGTTOU);
528 PyDict_SetItemString(d, "SIGTTOU", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000529 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000530#endif
531#ifdef SIGVTALRM
Guido van Rossume4485b01994-09-07 14:32:49 +0000532 x = PyInt_FromLong(SIGVTALRM);
533 PyDict_SetItemString(d, "SIGVTALRM", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000534 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000535#endif
536#ifdef SIGPROF
Guido van Rossume4485b01994-09-07 14:32:49 +0000537 x = PyInt_FromLong(SIGPROF);
538 PyDict_SetItemString(d, "SIGPROF", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000539 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000540#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000541#ifdef SIGXCPU
542 x = PyInt_FromLong(SIGXCPU);
543 PyDict_SetItemString(d, "SIGXCPU", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000544 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000545#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000546#ifdef SIGXFSZ
547 x = PyInt_FromLong(SIGXFSZ);
548 PyDict_SetItemString(d, "SIGXFSZ", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000549 Py_XDECREF(x);
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000550#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000551 if (!PyErr_Occurred())
552 return;
553
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000554 /* Check for errors */
Barry Warsaw92971171997-01-03 00:14:25 +0000555 finally:
Guido van Rossum08c16611997-08-02 03:01:42 +0000556 return;
557}
558
559static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000560finisignal(void)
Guido van Rossum08c16611997-08-02 03:01:42 +0000561{
562 int i;
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000563 PyObject *func;
Guido van Rossum08c16611997-08-02 03:01:42 +0000564
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000565 PyOS_setsig(SIGINT, old_siginthandler);
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000566 old_siginthandler = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +0000567
568 for (i = 1; i < NSIG; i++) {
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000569 func = Handlers[i].func;
Guido van Rossum08c16611997-08-02 03:01:42 +0000570 Handlers[i].tripped = 0;
Guido van Rossum08c16611997-08-02 03:01:42 +0000571 Handlers[i].func = NULL;
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000572 if (i != SIGINT && func != NULL && func != Py_None &&
573 func != DefaultHandler && func != IgnoreHandler)
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000574 PyOS_setsig(i, SIG_DFL);
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000575 Py_XDECREF(func);
Guido van Rossum08c16611997-08-02 03:01:42 +0000576 }
577
578 Py_XDECREF(IntHandler);
579 IntHandler = NULL;
580 Py_XDECREF(DefaultHandler);
581 DefaultHandler = NULL;
582 Py_XDECREF(IgnoreHandler);
583 IgnoreHandler = NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000584}
585
Barry Warsaw92971171997-01-03 00:14:25 +0000586
Barry Warsaw92971171997-01-03 00:14:25 +0000587/* Declared in pyerrors.h */
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000588int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000589PyErr_CheckSignals(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000590{
591 int i;
Guido van Rossume4485b01994-09-07 14:32:49 +0000592 PyObject *f;
Barry Warsaw92971171997-01-03 00:14:25 +0000593
594 if (!is_tripped)
Guido van Rossum6299d1e1994-05-31 12:51:13 +0000595 return 0;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000596#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000597 if (PyThread_get_thread_ident() != main_thread)
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000598 return 0;
599#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000600 if (!(f = PyEval_GetFrame()))
Guido van Rossume4485b01994-09-07 14:32:49 +0000601 f = Py_None;
Barry Warsaw92971171997-01-03 00:14:25 +0000602
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000603 for (i = 1; i < NSIG; i++) {
Barry Warsaw92971171997-01-03 00:14:25 +0000604 if (Handlers[i].tripped) {
605 PyObject *result = NULL;
606 PyObject *arglist = Py_BuildValue("(iO)", i, f);
607 Handlers[i].tripped = 0;
608
609 if (arglist) {
610 result = PyEval_CallObject(Handlers[i].func,
611 arglist);
Guido van Rossume4485b01994-09-07 14:32:49 +0000612 Py_DECREF(arglist);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000613 }
Barry Warsaw92971171997-01-03 00:14:25 +0000614 if (!result)
Guido van Rossumfcdd0e41997-01-21 06:13:09 +0000615 return -1;
Barry Warsaw92971171997-01-03 00:14:25 +0000616
617 Py_DECREF(result);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000618 }
619 }
Barry Warsaw92971171997-01-03 00:14:25 +0000620 is_tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000621 return 0;
622}
623
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000624
Barry Warsaw92971171997-01-03 00:14:25 +0000625/* Replacements for intrcheck.c functionality
626 * Declared in pyerrors.h
627 */
628void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000629PyErr_SetInterrupt(void)
Barry Warsaw92971171997-01-03 00:14:25 +0000630{
631 is_tripped++;
632 Handlers[SIGINT].tripped = 1;
Thomas Wouters334fb892000-07-25 12:56:38 +0000633 Py_AddPendingCall((int (*)(void *))PyErr_CheckSignals, NULL);
Barry Warsaw92971171997-01-03 00:14:25 +0000634}
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000635
636void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000637PyOS_InitInterrupts(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000638{
639 initsignal();
Guido van Rossum08c16611997-08-02 03:01:42 +0000640 _PyImport_FixupExtension("signal", "signal");
641}
642
643void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000644PyOS_FiniInterrupts(void)
Guido van Rossum08c16611997-08-02 03:01:42 +0000645{
646 finisignal();
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000647}
648
649int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000650PyOS_InterruptOccurred(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000651{
Barry Warsaw92971171997-01-03 00:14:25 +0000652 if (Handlers[SIGINT].tripped) {
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000653#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000654 if (PyThread_get_thread_ident() != main_thread)
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000655 return 0;
656#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000657 Handlers[SIGINT].tripped = 0;
Guido van Rossum6299d1e1994-05-31 12:51:13 +0000658 return 1;
659 }
660 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000661}
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000662
663void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000664PyOS_AfterFork(void)
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000665{
666#ifdef WITH_THREAD
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000667 PyEval_ReInitThreads();
Guido van Rossum65d5b571998-12-21 19:32:43 +0000668 main_thread = PyThread_get_thread_ident();
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000669 main_pid = getpid();
670#endif
671}