blob: 9c5a18f0a1aa29cba56d26e76b367f13407a114c [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
65*/
66
67#ifdef WITH_THREAD
Guido van Rossum295b8e51997-06-06 21:16:41 +000068#include <sys/types.h> /* For pid_t */
Guido van Rossum49b56061998-10-01 20:42:43 +000069#include "pythread.h"
Guido van Rossumbb4ba121994-06-23 11:25:45 +000070static long main_thread;
71static pid_t main_pid;
72#endif
73
Barry Warsaw92971171997-01-03 00:14:25 +000074static struct {
75 int tripped;
76 PyObject *func;
77} Handlers[NSIG];
Guido van Rossum398d9fe1994-05-11 08:59:13 +000078
Barry Warsaw92971171997-01-03 00:14:25 +000079static int is_tripped = 0; /* Speed up sigcheck() when none tripped */
Guido van Rossum398d9fe1994-05-11 08:59:13 +000080
Barry Warsaw92971171997-01-03 00:14:25 +000081static PyObject *DefaultHandler;
82static PyObject *IgnoreHandler;
83static PyObject *IntHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +000084
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +000085static PyOS_sighandler_t old_siginthandler = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +000086
Barry Warsaw92971171997-01-03 00:14:25 +000087
Guido van Rossume4485b01994-09-07 14:32:49 +000088static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +000089signal_default_int_handler(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +000090{
Guido van Rossume4485b01994-09-07 14:32:49 +000091 PyErr_SetNone(PyExc_KeyboardInterrupt);
Barry Warsaw92971171997-01-03 00:14:25 +000092 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +000093}
94
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +000095static char default_int_handler_doc[] =
96"default_int_handler(...)\n\
97\n\
98The default handler for SIGINT instated by Python.\n\
99It raises KeyboardInterrupt.";
100
Thomas Wouters0796b002000-07-22 23:49:30 +0000101
102static int
103checksignals_witharg(void * unused)
104{
105 return PyErr_CheckSignals();
106}
107
Tim Peters4f1b2082000-07-23 21:18:09 +0000108static void
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000109signal_handler(int sig_num)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000110{
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000111#ifdef WITH_THREAD
112 /* See NOTES section above */
113 if (getpid() == main_pid) {
114#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000115 is_tripped++;
116 Handlers[sig_num].tripped = 1;
Thomas Wouters0796b002000-07-22 23:49:30 +0000117 Py_AddPendingCall(checksignals_witharg, NULL);
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000118#ifdef WITH_THREAD
119 }
120#endif
Guido van Rossum602099a1994-09-14 13:32:22 +0000121#ifdef SIGCHLD
122 if (sig_num == SIGCHLD) {
123 /* To avoid infinite recursion, this signal remains
124 reset until explicit re-instated.
125 Don't clear the 'func' field as it is our pointer
126 to the Python handler... */
Tim Peters4f1b2082000-07-23 21:18:09 +0000127 return;
Guido van Rossum602099a1994-09-14 13:32:22 +0000128 }
129#endif
Guido van Rossum1b236761998-09-21 14:46:00 +0000130#ifdef HAVE_SIGINTERRUPT
131 siginterrupt(sig_num, 1);
132#endif
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000133 PyOS_setsig(sig_num, signal_handler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000134}
Guido van Rossume4485b01994-09-07 14:32:49 +0000135
Guido van Rossum06d511d1995-03-10 15:13:48 +0000136
Guido van Rossum1171ee61997-08-22 20:42:00 +0000137#ifdef HAVE_ALARM
Guido van Rossume4485b01994-09-07 14:32:49 +0000138static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000139signal_alarm(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000140{
141 int t;
Guido van Rossume4485b01994-09-07 14:32:49 +0000142 if (!PyArg_Parse(args, "i", &t))
Barry Warsaw92971171997-01-03 00:14:25 +0000143 return NULL;
Guido van Rossume4485b01994-09-07 14:32:49 +0000144 /* alarm() returns the number of seconds remaining */
145 return PyInt_FromLong(alarm(t));
Guido van Rossumaa0f4c71994-08-23 13:49:37 +0000146}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000147
148static char alarm_doc[] =
149"alarm(seconds)\n\
150\n\
Guido van Rossum911ec181998-06-28 17:00:19 +0000151Arrange for SIGALRM to arrive after the given number of seconds.";
Guido van Rossum06d511d1995-03-10 15:13:48 +0000152#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000153
Guido van Rossum1171ee61997-08-22 20:42:00 +0000154#ifdef HAVE_PAUSE
Guido van Rossuma597dde1995-01-10 20:56:29 +0000155static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000156signal_pause(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000157{
Guido van Rossume4485b01994-09-07 14:32:49 +0000158 if (!PyArg_NoArgs(args))
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000159 return NULL;
Barry Warsaw92971171997-01-03 00:14:25 +0000160
Guido van Rossuma597dde1995-01-10 20:56:29 +0000161 Py_BEGIN_ALLOW_THREADS
Barry Warsaw92971171997-01-03 00:14:25 +0000162 (void)pause();
Guido van Rossuma597dde1995-01-10 20:56:29 +0000163 Py_END_ALLOW_THREADS
Barry Warsaw92971171997-01-03 00:14:25 +0000164 /* make sure that any exceptions that got raised are propagated
165 * back into Python
166 */
167 if (PyErr_CheckSignals())
168 return NULL;
169
Guido van Rossume4485b01994-09-07 14:32:49 +0000170 Py_INCREF(Py_None);
171 return Py_None;
172}
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000173static char pause_doc[] =
Barry Warsaw1ee36ff1998-07-21 22:41:18 +0000174"pause()\n\
175\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000176Wait until a signal arrives.";
177
Guido van Rossum06d511d1995-03-10 15:13:48 +0000178#endif
Guido van Rossume4485b01994-09-07 14:32:49 +0000179
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000180
Guido van Rossume4485b01994-09-07 14:32:49 +0000181static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000182signal_signal(PyObject *self, PyObject *args)
Guido van Rossume4485b01994-09-07 14:32:49 +0000183{
184 PyObject *obj;
185 int sig_num;
186 PyObject *old_handler;
Tim Peters4f1b2082000-07-23 21:18:09 +0000187 void (*func)(int);
Guido van Rossume4485b01994-09-07 14:32:49 +0000188 if (!PyArg_Parse(args, "(iO)", &sig_num, &obj))
Barry Warsaw92971171997-01-03 00:14:25 +0000189 return NULL;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000190#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000191 if (PyThread_get_thread_ident() != main_thread) {
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000192 PyErr_SetString(PyExc_ValueError,
193 "signal only works in main thread");
Barry Warsaw92971171997-01-03 00:14:25 +0000194 return NULL;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000195 }
196#endif
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000197 if (sig_num < 1 || sig_num >= NSIG) {
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000198 PyErr_SetString(PyExc_ValueError,
199 "signal number out of range");
Barry Warsaw92971171997-01-03 00:14:25 +0000200 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000201 }
Barry Warsaw92971171997-01-03 00:14:25 +0000202 if (obj == IgnoreHandler)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000203 func = SIG_IGN;
Barry Warsaw92971171997-01-03 00:14:25 +0000204 else if (obj == DefaultHandler)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000205 func = SIG_DFL;
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000206 else if (!PyCallable_Check(obj)) {
Guido van Rossume4485b01994-09-07 14:32:49 +0000207 PyErr_SetString(PyExc_TypeError,
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000208"signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object");
Barry Warsaw92971171997-01-03 00:14:25 +0000209 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000210 }
211 else
Barry Warsaw92971171997-01-03 00:14:25 +0000212 func = signal_handler;
Guido van Rossum1b236761998-09-21 14:46:00 +0000213#ifdef HAVE_SIGINTERRUPT
214 siginterrupt(sig_num, 1);
215#endif
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000216 if (PyOS_setsig(sig_num, func) == SIG_ERR) {
Guido van Rossume4485b01994-09-07 14:32:49 +0000217 PyErr_SetFromErrno(PyExc_RuntimeError);
Barry Warsaw92971171997-01-03 00:14:25 +0000218 return NULL;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000219 }
Barry Warsaw92971171997-01-03 00:14:25 +0000220 old_handler = Handlers[sig_num].func;
221 Handlers[sig_num].tripped = 0;
Guido van Rossume4485b01994-09-07 14:32:49 +0000222 Py_INCREF(obj);
Barry Warsaw92971171997-01-03 00:14:25 +0000223 Handlers[sig_num].func = obj;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000224 return old_handler;
225}
226
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000227static char signal_doc[] =
228"signal(sig, action) -> action\n\
229\n\
230Set the action for the given signal. The action can be SIG_DFL,\n\
231SIG_IGN, or a callable Python object. The previous action is\n\
232returned. See getsignal() for possible return values.\n\
233\n\
234*** IMPORTANT NOTICE ***\n\
235A signal handler function is called with two arguments:\n\
236the first is the signal number, the second is the interrupted stack frame.";
237
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000238
Guido van Rossume4485b01994-09-07 14:32:49 +0000239static PyObject *
Peter Schneider-Kampe89b1562000-07-10 12:04:18 +0000240signal_getsignal(PyObject *self, PyObject *args)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000241{
242 int sig_num;
Guido van Rossume4485b01994-09-07 14:32:49 +0000243 PyObject *old_handler;
244 if (!PyArg_Parse(args, "i", &sig_num))
Barry Warsaw92971171997-01-03 00:14:25 +0000245 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000246 if (sig_num < 1 || sig_num >= NSIG) {
Guido van Rossumf4b012a1995-03-16 15:43:29 +0000247 PyErr_SetString(PyExc_ValueError,
248 "signal number out of range");
Barry Warsaw92971171997-01-03 00:14:25 +0000249 return NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000250 }
Barry Warsaw92971171997-01-03 00:14:25 +0000251 old_handler = Handlers[sig_num].func;
Guido van Rossume4485b01994-09-07 14:32:49 +0000252 Py_INCREF(old_handler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000253 return old_handler;
254}
255
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000256static char getsignal_doc[] =
257"getsignal(sig) -> action\n\
258\n\
259Return the current action for the given signal. The return value can be:\n\
260SIG_IGN -- if the signal is being ignored\n\
261SIG_DFL -- if the default action for the signal is in effect\n\
262None -- if an unknown handler is in effect\n\
263anything else -- the callable Python object used as a handler\n\
264";
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000265
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000266
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000267/* List of functions defined in the module */
Barry Warsaw92971171997-01-03 00:14:25 +0000268static PyMethodDef signal_methods[] = {
Guido van Rossum1171ee61997-08-22 20:42:00 +0000269#ifdef HAVE_ALARM
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000270 {"alarm", signal_alarm, METH_OLDARGS, alarm_doc},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000271#endif
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000272 {"signal", signal_signal, METH_OLDARGS, signal_doc},
273 {"getsignal", signal_getsignal, METH_OLDARGS, getsignal_doc},
Guido van Rossum1171ee61997-08-22 20:42:00 +0000274#ifdef HAVE_PAUSE
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000275 {"pause", signal_pause, METH_OLDARGS, pause_doc},
Guido van Rossum06d511d1995-03-10 15:13:48 +0000276#endif
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000277 {"default_int_handler", signal_default_int_handler,
278 METH_OLDARGS, default_int_handler_doc},
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000279 {NULL, NULL} /* sentinel */
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000280};
281
Barry Warsaw92971171997-01-03 00:14:25 +0000282
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000283static char module_doc[] =
284"This module provides mechanisms to use signal handlers in Python.\n\
285\n\
286Functions:\n\
287\n\
288alarm() -- cause SIGALRM after a specified time [Unix only]\n\
289signal() -- set the action for a given signal\n\
290getsignal() -- get the signal action for a given signal\n\
291pause() -- wait until a signal arrives [Unix only]\n\
292default_int_handler() -- default SIGINT handler\n\
293\n\
294Constants:\n\
295\n\
296SIG_DFL -- used to refer to the system default handler\n\
297SIG_IGN -- used to ignore the signal\n\
298NSIG -- number of defined signals\n\
299\n\
300SIGINT, SIGTERM, etc. -- signal numbers\n\
301\n\
302*** IMPORTANT NOTICE ***\n\
303A signal handler function is called with two arguments:\n\
304the first is the signal number, the second is the interrupted stack frame.";
305
Guido van Rossum3886bb61998-12-04 18:50:17 +0000306DL_EXPORT(void)
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000307initsignal(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000308{
Guido van Rossume4485b01994-09-07 14:32:49 +0000309 PyObject *m, *d, *x;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000310 int i;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000311
312#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000313 main_thread = PyThread_get_thread_ident();
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000314 main_pid = getpid();
315#endif
316
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000317 /* Create the module and add the functions */
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000318 m = Py_InitModule3("signal", signal_methods, module_doc);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000319
320 /* Add some symbolic constants to the module */
Guido van Rossume4485b01994-09-07 14:32:49 +0000321 d = PyModule_GetDict(m);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000322
Guido van Rossum276fa432000-06-30 23:04:18 +0000323 x = DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL);
Barry Warsaw92971171997-01-03 00:14:25 +0000324 if (!x || PyDict_SetItemString(d, "SIG_DFL", x) < 0)
325 goto finally;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000326
Guido van Rossum276fa432000-06-30 23:04:18 +0000327 x = IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN);
Barry Warsaw92971171997-01-03 00:14:25 +0000328 if (!x || PyDict_SetItemString(d, "SIG_IGN", x) < 0)
329 goto finally;
330
331 x = PyInt_FromLong((long)NSIG);
332 if (!x || PyDict_SetItemString(d, "NSIG", x) < 0)
333 goto finally;
Guido van Rossum08c16611997-08-02 03:01:42 +0000334 Py_DECREF(x);
Barry Warsaw92971171997-01-03 00:14:25 +0000335
336 x = IntHandler = PyDict_GetItemString(d, "default_int_handler");
337 if (!x)
338 goto finally;
Guido van Rossum08c16611997-08-02 03:01:42 +0000339 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +0000340
341 Handlers[0].tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000342 for (i = 1; i < NSIG; i++) {
Tim Peters4f1b2082000-07-23 21:18:09 +0000343 void (*t)(int);
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000344 t = PyOS_getsig(i);
Barry Warsaw92971171997-01-03 00:14:25 +0000345 Handlers[i].tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000346 if (t == SIG_DFL)
Barry Warsaw92971171997-01-03 00:14:25 +0000347 Handlers[i].func = DefaultHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000348 else if (t == SIG_IGN)
Barry Warsaw92971171997-01-03 00:14:25 +0000349 Handlers[i].func = IgnoreHandler;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000350 else
Barry Warsaw92971171997-01-03 00:14:25 +0000351 Handlers[i].func = Py_None; /* None of our business */
352 Py_INCREF(Handlers[i].func);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000353 }
Barry Warsaw92971171997-01-03 00:14:25 +0000354 if (Handlers[SIGINT].func == DefaultHandler) {
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000355 /* Install default int handler */
Guido van Rossum08c16611997-08-02 03:01:42 +0000356 Py_INCREF(IntHandler);
Barry Warsaw92971171997-01-03 00:14:25 +0000357 Py_DECREF(Handlers[SIGINT].func);
358 Handlers[SIGINT].func = IntHandler;
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000359 old_siginthandler = PyOS_setsig(SIGINT, &signal_handler);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000360 }
361
362#ifdef SIGHUP
Guido van Rossume4485b01994-09-07 14:32:49 +0000363 x = PyInt_FromLong(SIGHUP);
364 PyDict_SetItemString(d, "SIGHUP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000365 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000366#endif
367#ifdef SIGINT
Guido van Rossume4485b01994-09-07 14:32:49 +0000368 x = PyInt_FromLong(SIGINT);
369 PyDict_SetItemString(d, "SIGINT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000370 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000371#endif
372#ifdef SIGQUIT
Guido van Rossume4485b01994-09-07 14:32:49 +0000373 x = PyInt_FromLong(SIGQUIT);
374 PyDict_SetItemString(d, "SIGQUIT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000375 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000376#endif
377#ifdef SIGILL
Guido van Rossume4485b01994-09-07 14:32:49 +0000378 x = PyInt_FromLong(SIGILL);
379 PyDict_SetItemString(d, "SIGILL", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000380 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000381#endif
382#ifdef SIGTRAP
Guido van Rossume4485b01994-09-07 14:32:49 +0000383 x = PyInt_FromLong(SIGTRAP);
384 PyDict_SetItemString(d, "SIGTRAP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000385 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000386#endif
387#ifdef SIGIOT
Guido van Rossume4485b01994-09-07 14:32:49 +0000388 x = PyInt_FromLong(SIGIOT);
389 PyDict_SetItemString(d, "SIGIOT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000390 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000391#endif
392#ifdef SIGABRT
Guido van Rossume4485b01994-09-07 14:32:49 +0000393 x = PyInt_FromLong(SIGABRT);
394 PyDict_SetItemString(d, "SIGABRT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000395 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000396#endif
397#ifdef SIGEMT
Guido van Rossume4485b01994-09-07 14:32:49 +0000398 x = PyInt_FromLong(SIGEMT);
399 PyDict_SetItemString(d, "SIGEMT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000400 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000401#endif
402#ifdef SIGFPE
Guido van Rossume4485b01994-09-07 14:32:49 +0000403 x = PyInt_FromLong(SIGFPE);
404 PyDict_SetItemString(d, "SIGFPE", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000405 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000406#endif
407#ifdef SIGKILL
Guido van Rossume4485b01994-09-07 14:32:49 +0000408 x = PyInt_FromLong(SIGKILL);
409 PyDict_SetItemString(d, "SIGKILL", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000410 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000411#endif
412#ifdef SIGBUS
Guido van Rossume4485b01994-09-07 14:32:49 +0000413 x = PyInt_FromLong(SIGBUS);
414 PyDict_SetItemString(d, "SIGBUS", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000415 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000416#endif
417#ifdef SIGSEGV
Guido van Rossume4485b01994-09-07 14:32:49 +0000418 x = PyInt_FromLong(SIGSEGV);
419 PyDict_SetItemString(d, "SIGSEGV", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000420 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000421#endif
422#ifdef SIGSYS
Guido van Rossume4485b01994-09-07 14:32:49 +0000423 x = PyInt_FromLong(SIGSYS);
424 PyDict_SetItemString(d, "SIGSYS", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000425 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000426#endif
427#ifdef SIGPIPE
Guido van Rossume4485b01994-09-07 14:32:49 +0000428 x = PyInt_FromLong(SIGPIPE);
429 PyDict_SetItemString(d, "SIGPIPE", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000430 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000431#endif
432#ifdef SIGALRM
Guido van Rossume4485b01994-09-07 14:32:49 +0000433 x = PyInt_FromLong(SIGALRM);
434 PyDict_SetItemString(d, "SIGALRM", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000435 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000436#endif
437#ifdef SIGTERM
Guido van Rossume4485b01994-09-07 14:32:49 +0000438 x = PyInt_FromLong(SIGTERM);
439 PyDict_SetItemString(d, "SIGTERM", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000440 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000441#endif
442#ifdef SIGUSR1
Guido van Rossume4485b01994-09-07 14:32:49 +0000443 x = PyInt_FromLong(SIGUSR1);
444 PyDict_SetItemString(d, "SIGUSR1", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000445 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000446#endif
447#ifdef SIGUSR2
Guido van Rossume4485b01994-09-07 14:32:49 +0000448 x = PyInt_FromLong(SIGUSR2);
449 PyDict_SetItemString(d, "SIGUSR2", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000450 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000451#endif
452#ifdef SIGCLD
Guido van Rossume4485b01994-09-07 14:32:49 +0000453 x = PyInt_FromLong(SIGCLD);
454 PyDict_SetItemString(d, "SIGCLD", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000455 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000456#endif
457#ifdef SIGCHLD
Guido van Rossume4485b01994-09-07 14:32:49 +0000458 x = PyInt_FromLong(SIGCHLD);
459 PyDict_SetItemString(d, "SIGCHLD", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000460 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000461#endif
462#ifdef SIGPWR
Guido van Rossume4485b01994-09-07 14:32:49 +0000463 x = PyInt_FromLong(SIGPWR);
464 PyDict_SetItemString(d, "SIGPWR", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000465 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000466#endif
467#ifdef SIGIO
Guido van Rossume4485b01994-09-07 14:32:49 +0000468 x = PyInt_FromLong(SIGIO);
469 PyDict_SetItemString(d, "SIGIO", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000470 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000471#endif
472#ifdef SIGURG
Guido van Rossume4485b01994-09-07 14:32:49 +0000473 x = PyInt_FromLong(SIGURG);
474 PyDict_SetItemString(d, "SIGURG", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000475 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000476#endif
477#ifdef SIGWINCH
Guido van Rossume4485b01994-09-07 14:32:49 +0000478 x = PyInt_FromLong(SIGWINCH);
479 PyDict_SetItemString(d, "SIGWINCH", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000480 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000481#endif
482#ifdef SIGPOLL
Guido van Rossume4485b01994-09-07 14:32:49 +0000483 x = PyInt_FromLong(SIGPOLL);
484 PyDict_SetItemString(d, "SIGPOLL", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000485 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000486#endif
487#ifdef SIGSTOP
Guido van Rossume4485b01994-09-07 14:32:49 +0000488 x = PyInt_FromLong(SIGSTOP);
489 PyDict_SetItemString(d, "SIGSTOP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000490 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000491#endif
492#ifdef SIGTSTP
Guido van Rossume4485b01994-09-07 14:32:49 +0000493 x = PyInt_FromLong(SIGTSTP);
494 PyDict_SetItemString(d, "SIGTSTP", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000495 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000496#endif
497#ifdef SIGCONT
Guido van Rossume4485b01994-09-07 14:32:49 +0000498 x = PyInt_FromLong(SIGCONT);
499 PyDict_SetItemString(d, "SIGCONT", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000500 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000501#endif
502#ifdef SIGTTIN
Guido van Rossume4485b01994-09-07 14:32:49 +0000503 x = PyInt_FromLong(SIGTTIN);
504 PyDict_SetItemString(d, "SIGTTIN", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000505 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000506#endif
507#ifdef SIGTTOU
Guido van Rossume4485b01994-09-07 14:32:49 +0000508 x = PyInt_FromLong(SIGTTOU);
509 PyDict_SetItemString(d, "SIGTTOU", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000510 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000511#endif
512#ifdef SIGVTALRM
Guido van Rossume4485b01994-09-07 14:32:49 +0000513 x = PyInt_FromLong(SIGVTALRM);
514 PyDict_SetItemString(d, "SIGVTALRM", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000515 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000516#endif
517#ifdef SIGPROF
Guido van Rossume4485b01994-09-07 14:32:49 +0000518 x = PyInt_FromLong(SIGPROF);
519 PyDict_SetItemString(d, "SIGPROF", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000520 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000521#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000522#ifdef SIGXCPU
523 x = PyInt_FromLong(SIGXCPU);
524 PyDict_SetItemString(d, "SIGXCPU", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000525 Py_XDECREF(x);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000526#endif
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000527#ifdef SIGXFSZ
528 x = PyInt_FromLong(SIGXFSZ);
529 PyDict_SetItemString(d, "SIGXFSZ", x);
Barry Warsaw73a75eb1997-01-09 23:50:28 +0000530 Py_XDECREF(x);
Barry Warsaw14ed5fb1996-12-16 20:24:22 +0000531#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000532 if (!PyErr_Occurred())
533 return;
534
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000535 /* Check for errors */
Barry Warsaw92971171997-01-03 00:14:25 +0000536 finally:
Guido van Rossum08c16611997-08-02 03:01:42 +0000537 return;
538}
539
540static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000541finisignal(void)
Guido van Rossum08c16611997-08-02 03:01:42 +0000542{
543 int i;
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000544 PyObject *func;
Guido van Rossum08c16611997-08-02 03:01:42 +0000545
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000546 PyOS_setsig(SIGINT, old_siginthandler);
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000547 old_siginthandler = SIG_DFL;
Guido van Rossum08c16611997-08-02 03:01:42 +0000548
549 for (i = 1; i < NSIG; i++) {
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000550 func = Handlers[i].func;
Guido van Rossum08c16611997-08-02 03:01:42 +0000551 Handlers[i].tripped = 0;
Guido van Rossum08c16611997-08-02 03:01:42 +0000552 Handlers[i].func = NULL;
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000553 if (i != SIGINT && func != NULL && func != Py_None &&
554 func != DefaultHandler && func != IgnoreHandler)
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000555 PyOS_setsig(i, SIG_DFL);
Guido van Rossum7ff20ac1997-11-03 21:53:55 +0000556 Py_XDECREF(func);
Guido van Rossum08c16611997-08-02 03:01:42 +0000557 }
558
559 Py_XDECREF(IntHandler);
560 IntHandler = NULL;
561 Py_XDECREF(DefaultHandler);
562 DefaultHandler = NULL;
563 Py_XDECREF(IgnoreHandler);
564 IgnoreHandler = NULL;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000565}
566
Barry Warsaw92971171997-01-03 00:14:25 +0000567
Barry Warsaw92971171997-01-03 00:14:25 +0000568/* Declared in pyerrors.h */
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000569int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000570PyErr_CheckSignals(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000571{
572 int i;
Guido van Rossume4485b01994-09-07 14:32:49 +0000573 PyObject *f;
Barry Warsaw92971171997-01-03 00:14:25 +0000574
575 if (!is_tripped)
Guido van Rossum6299d1e1994-05-31 12:51:13 +0000576 return 0;
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000577#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000578 if (PyThread_get_thread_ident() != main_thread)
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000579 return 0;
580#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000581 if (!(f = PyEval_GetFrame()))
Guido van Rossume4485b01994-09-07 14:32:49 +0000582 f = Py_None;
Barry Warsaw92971171997-01-03 00:14:25 +0000583
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000584 for (i = 1; i < NSIG; i++) {
Barry Warsaw92971171997-01-03 00:14:25 +0000585 if (Handlers[i].tripped) {
586 PyObject *result = NULL;
587 PyObject *arglist = Py_BuildValue("(iO)", i, f);
588 Handlers[i].tripped = 0;
589
590 if (arglist) {
591 result = PyEval_CallObject(Handlers[i].func,
592 arglist);
Guido van Rossume4485b01994-09-07 14:32:49 +0000593 Py_DECREF(arglist);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000594 }
Barry Warsaw92971171997-01-03 00:14:25 +0000595 if (!result)
Guido van Rossumfcdd0e41997-01-21 06:13:09 +0000596 return -1;
Barry Warsaw92971171997-01-03 00:14:25 +0000597
598 Py_DECREF(result);
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000599 }
600 }
Barry Warsaw92971171997-01-03 00:14:25 +0000601 is_tripped = 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000602 return 0;
603}
604
Guido van Rossumd2cd7ad2000-09-16 16:35:28 +0000605
Barry Warsaw92971171997-01-03 00:14:25 +0000606/* Replacements for intrcheck.c functionality
607 * Declared in pyerrors.h
608 */
609void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000610PyErr_SetInterrupt(void)
Barry Warsaw92971171997-01-03 00:14:25 +0000611{
612 is_tripped++;
613 Handlers[SIGINT].tripped = 1;
Thomas Wouters334fb892000-07-25 12:56:38 +0000614 Py_AddPendingCall((int (*)(void *))PyErr_CheckSignals, NULL);
Barry Warsaw92971171997-01-03 00:14:25 +0000615}
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000616
617void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000618PyOS_InitInterrupts(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000619{
620 initsignal();
Guido van Rossum08c16611997-08-02 03:01:42 +0000621 _PyImport_FixupExtension("signal", "signal");
622}
623
624void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000625PyOS_FiniInterrupts(void)
Guido van Rossum08c16611997-08-02 03:01:42 +0000626{
627 finisignal();
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000628}
629
630int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000631PyOS_InterruptOccurred(void)
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000632{
Barry Warsaw92971171997-01-03 00:14:25 +0000633 if (Handlers[SIGINT].tripped) {
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000634#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000635 if (PyThread_get_thread_ident() != main_thread)
Guido van Rossumbb4ba121994-06-23 11:25:45 +0000636 return 0;
637#endif
Barry Warsaw92971171997-01-03 00:14:25 +0000638 Handlers[SIGINT].tripped = 0;
Guido van Rossum6299d1e1994-05-31 12:51:13 +0000639 return 1;
640 }
641 return 0;
Guido van Rossum398d9fe1994-05-11 08:59:13 +0000642}
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000643
644void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000645PyOS_AfterFork(void)
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000646{
647#ifdef WITH_THREAD
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000648 PyEval_ReInitThreads();
Guido van Rossum65d5b571998-12-21 19:32:43 +0000649 main_thread = PyThread_get_thread_ident();
Guido van Rossum359bcaa1997-11-14 22:24:28 +0000650 main_pid = getpid();
651#endif
652}