Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame] | 2 | Copyright (c) 2000, BeOpen.com. |
| 3 | Copyright (c) 1995-2000, Corporation for National Research Initiatives. |
| 4 | Copyright (c) 1990-1995, Stichting Mathematisch Centrum. |
| 5 | All rights reserved. |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame] | 7 | See the file "Misc/COPYRIGHT" for information on usage and |
| 8 | redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 9 | ******************************************************************/ |
| 10 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 11 | /* Signal module -- many thanks to Lance Ellinghaus */ |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 12 | |
Guido van Rossum | 644a12b | 1997-04-09 19:24:53 +0000 | [diff] [blame] | 13 | /* XXX Signals should be recorded per thread, now we have thread state. */ |
| 14 | |
Guido van Rossum | 602099a | 1994-09-14 13:32:22 +0000 | [diff] [blame] | 15 | #include "Python.h" |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 16 | #include "intrcheck.h" |
| 17 | |
Guido van Rossum | 644a12b | 1997-04-09 19:24:53 +0000 | [diff] [blame] | 18 | #ifdef MS_WIN32 |
| 19 | #include <process.h> |
| 20 | #endif |
| 21 | |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 22 | #ifdef HAVE_UNISTD_H |
| 23 | #include <unistd.h> |
| 24 | #endif |
| 25 | |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 26 | #include <signal.h> |
| 27 | |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 28 | #ifndef SIG_ERR |
| 29 | #define SIG_ERR ((RETSIGTYPE (*)())-1) |
| 30 | #endif |
| 31 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 32 | #if defined(PYOS_OS2) |
| 33 | #define NSIG 12 |
| 34 | #include <process.h> |
| 35 | #endif |
| 36 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 37 | #ifndef NSIG |
Marc-André Lemburg | 8bcfb8a | 2000-07-04 14:17:33 +0000 | [diff] [blame] | 38 | # 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 Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 47 | #endif |
| 48 | |
| 49 | |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 50 | |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 51 | /* |
| 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 Rossum | a3c04b0 | 1995-01-12 11:29:01 +0000 | [diff] [blame] | 70 | be to deliver it to the main thread -- POSIX?). For now, we have |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 71 | 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 Rossum | 295b8e5 | 1997-06-06 21:16:41 +0000 | [diff] [blame] | 78 | #include <sys/types.h> /* For pid_t */ |
Guido van Rossum | 49b5606 | 1998-10-01 20:42:43 +0000 | [diff] [blame] | 79 | #include "pythread.h" |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 80 | static long main_thread; |
| 81 | static pid_t main_pid; |
| 82 | #endif |
| 83 | |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 84 | static struct { |
| 85 | int tripped; |
| 86 | PyObject *func; |
| 87 | } Handlers[NSIG]; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 88 | |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 89 | static int is_tripped = 0; /* Speed up sigcheck() when none tripped */ |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 90 | |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 91 | static PyObject *DefaultHandler; |
| 92 | static PyObject *IgnoreHandler; |
| 93 | static PyObject *IntHandler; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 94 | |
Guido van Rossum | 08c1661 | 1997-08-02 03:01:42 +0000 | [diff] [blame] | 95 | static RETSIGTYPE (*old_siginthandler)() = SIG_DFL; |
| 96 | |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 97 | |
| 98 | |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 99 | static PyObject * |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 100 | signal_default_int_handler(self, arg) |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 101 | PyObject *self; |
| 102 | PyObject *arg; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 103 | { |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 104 | PyErr_SetNone(PyExc_KeyboardInterrupt); |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 105 | return NULL; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Guido van Rossum | 1d8fb2d | 1998-06-28 16:54:49 +0000 | [diff] [blame] | 108 | static char default_int_handler_doc[] = |
| 109 | "default_int_handler(...)\n\ |
| 110 | \n\ |
| 111 | The default handler for SIGINT instated by Python.\n\ |
| 112 | It raises KeyboardInterrupt."; |
| 113 | |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 114 | |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 115 | static RETSIGTYPE |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 116 | signal_handler(sig_num) |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 117 | int sig_num; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 118 | { |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 119 | #ifdef WITH_THREAD |
| 120 | /* See NOTES section above */ |
| 121 | if (getpid() == main_pid) { |
| 122 | #endif |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 123 | is_tripped++; |
| 124 | Handlers[sig_num].tripped = 1; |
Guido van Rossum | 644a12b | 1997-04-09 19:24:53 +0000 | [diff] [blame] | 125 | Py_AddPendingCall( |
| 126 | (int (*) Py_PROTO((ANY *)))PyErr_CheckSignals, NULL); |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 127 | #ifdef WITH_THREAD |
| 128 | } |
| 129 | #endif |
Guido van Rossum | 602099a | 1994-09-14 13:32:22 +0000 | [diff] [blame] | 130 | #ifdef SIGCHLD |
| 131 | if (sig_num == SIGCHLD) { |
| 132 | /* To avoid infinite recursion, this signal remains |
| 133 | reset until explicit re-instated. |
| 134 | Don't clear the 'func' field as it is our pointer |
| 135 | to the Python handler... */ |
| 136 | return; |
| 137 | } |
| 138 | #endif |
Guido van Rossum | 1b23676 | 1998-09-21 14:46:00 +0000 | [diff] [blame] | 139 | #ifdef HAVE_SIGINTERRUPT |
| 140 | siginterrupt(sig_num, 1); |
| 141 | #endif |
Guido van Rossum | a5e54d0 | 1998-05-01 18:58:59 +0000 | [diff] [blame] | 142 | (void)signal(sig_num, &signal_handler); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 143 | } |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 144 | |
Guido van Rossum | 06d511d | 1995-03-10 15:13:48 +0000 | [diff] [blame] | 145 | |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 146 | |
Guido van Rossum | 1171ee6 | 1997-08-22 20:42:00 +0000 | [diff] [blame] | 147 | #ifdef HAVE_ALARM |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 148 | static PyObject * |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 149 | signal_alarm(self, args) |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 150 | PyObject *self; /* Not used */ |
| 151 | PyObject *args; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 152 | { |
| 153 | int t; |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 154 | if (!PyArg_Parse(args, "i", &t)) |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 155 | return NULL; |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 156 | /* alarm() returns the number of seconds remaining */ |
| 157 | return PyInt_FromLong(alarm(t)); |
Guido van Rossum | aa0f4c7 | 1994-08-23 13:49:37 +0000 | [diff] [blame] | 158 | } |
Guido van Rossum | 1d8fb2d | 1998-06-28 16:54:49 +0000 | [diff] [blame] | 159 | |
| 160 | static char alarm_doc[] = |
| 161 | "alarm(seconds)\n\ |
| 162 | \n\ |
Guido van Rossum | 911ec18 | 1998-06-28 17:00:19 +0000 | [diff] [blame] | 163 | Arrange for SIGALRM to arrive after the given number of seconds."; |
Guido van Rossum | 06d511d | 1995-03-10 15:13:48 +0000 | [diff] [blame] | 164 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 165 | |
Guido van Rossum | 1171ee6 | 1997-08-22 20:42:00 +0000 | [diff] [blame] | 166 | #ifdef HAVE_PAUSE |
Guido van Rossum | a597dde | 1995-01-10 20:56:29 +0000 | [diff] [blame] | 167 | static PyObject * |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 168 | signal_pause(self, args) |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 169 | PyObject *self; /* Not used */ |
| 170 | PyObject *args; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 171 | { |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 172 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 173 | return NULL; |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 174 | |
Guido van Rossum | a597dde | 1995-01-10 20:56:29 +0000 | [diff] [blame] | 175 | Py_BEGIN_ALLOW_THREADS |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 176 | (void)pause(); |
Guido van Rossum | a597dde | 1995-01-10 20:56:29 +0000 | [diff] [blame] | 177 | Py_END_ALLOW_THREADS |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 178 | /* 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 Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 184 | Py_INCREF(Py_None); |
| 185 | return Py_None; |
| 186 | } |
Guido van Rossum | 1d8fb2d | 1998-06-28 16:54:49 +0000 | [diff] [blame] | 187 | static char pause_doc[] = |
Barry Warsaw | 1ee36ff | 1998-07-21 22:41:18 +0000 | [diff] [blame] | 188 | "pause()\n\ |
| 189 | \n\ |
Guido van Rossum | 1d8fb2d | 1998-06-28 16:54:49 +0000 | [diff] [blame] | 190 | Wait until a signal arrives."; |
| 191 | |
Guido van Rossum | 06d511d | 1995-03-10 15:13:48 +0000 | [diff] [blame] | 192 | #endif |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 193 | |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 194 | |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 195 | static PyObject * |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 196 | signal_signal(self, args) |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 197 | PyObject *self; /* Not used */ |
| 198 | PyObject *args; |
| 199 | { |
| 200 | PyObject *obj; |
| 201 | int sig_num; |
| 202 | PyObject *old_handler; |
| 203 | RETSIGTYPE (*func)(); |
| 204 | if (!PyArg_Parse(args, "(iO)", &sig_num, &obj)) |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 205 | return NULL; |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 206 | #ifdef WITH_THREAD |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 207 | if (PyThread_get_thread_ident() != main_thread) { |
Guido van Rossum | f4b012a | 1995-03-16 15:43:29 +0000 | [diff] [blame] | 208 | PyErr_SetString(PyExc_ValueError, |
| 209 | "signal only works in main thread"); |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 210 | return NULL; |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 211 | } |
| 212 | #endif |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 213 | if (sig_num < 1 || sig_num >= NSIG) { |
Guido van Rossum | f4b012a | 1995-03-16 15:43:29 +0000 | [diff] [blame] | 214 | PyErr_SetString(PyExc_ValueError, |
| 215 | "signal number out of range"); |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 216 | return NULL; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 217 | } |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 218 | if (obj == IgnoreHandler) |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 219 | func = SIG_IGN; |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 220 | else if (obj == DefaultHandler) |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 221 | func = SIG_DFL; |
Guido van Rossum | f4b012a | 1995-03-16 15:43:29 +0000 | [diff] [blame] | 222 | else if (!PyCallable_Check(obj)) { |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 223 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 224 | "signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object"); |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 225 | return NULL; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 226 | } |
| 227 | else |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 228 | func = signal_handler; |
Guido van Rossum | 1b23676 | 1998-09-21 14:46:00 +0000 | [diff] [blame] | 229 | #ifdef HAVE_SIGINTERRUPT |
| 230 | siginterrupt(sig_num, 1); |
| 231 | #endif |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 232 | if (signal(sig_num, func) == SIG_ERR) { |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 233 | PyErr_SetFromErrno(PyExc_RuntimeError); |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 234 | return NULL; |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 235 | } |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 236 | old_handler = Handlers[sig_num].func; |
| 237 | Handlers[sig_num].tripped = 0; |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 238 | Py_INCREF(obj); |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 239 | Handlers[sig_num].func = obj; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 240 | return old_handler; |
| 241 | } |
| 242 | |
Guido van Rossum | 1d8fb2d | 1998-06-28 16:54:49 +0000 | [diff] [blame] | 243 | static char signal_doc[] = |
| 244 | "signal(sig, action) -> action\n\ |
| 245 | \n\ |
| 246 | Set the action for the given signal. The action can be SIG_DFL,\n\ |
| 247 | SIG_IGN, or a callable Python object. The previous action is\n\ |
| 248 | returned. See getsignal() for possible return values.\n\ |
| 249 | \n\ |
| 250 | *** IMPORTANT NOTICE ***\n\ |
| 251 | A signal handler function is called with two arguments:\n\ |
| 252 | the first is the signal number, the second is the interrupted stack frame."; |
| 253 | |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 254 | |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 255 | static PyObject * |
Guido van Rossum | 1d8fb2d | 1998-06-28 16:54:49 +0000 | [diff] [blame] | 256 | signal_getsignal(self, args) |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 257 | PyObject *self; /* Not used */ |
| 258 | PyObject *args; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 259 | { |
| 260 | int sig_num; |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 261 | PyObject *old_handler; |
| 262 | if (!PyArg_Parse(args, "i", &sig_num)) |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 263 | return NULL; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 264 | if (sig_num < 1 || sig_num >= NSIG) { |
Guido van Rossum | f4b012a | 1995-03-16 15:43:29 +0000 | [diff] [blame] | 265 | PyErr_SetString(PyExc_ValueError, |
| 266 | "signal number out of range"); |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 267 | return NULL; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 268 | } |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 269 | old_handler = Handlers[sig_num].func; |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 270 | Py_INCREF(old_handler); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 271 | return old_handler; |
| 272 | } |
| 273 | |
Guido van Rossum | 1d8fb2d | 1998-06-28 16:54:49 +0000 | [diff] [blame] | 274 | static char getsignal_doc[] = |
| 275 | "getsignal(sig) -> action\n\ |
| 276 | \n\ |
| 277 | Return the current action for the given signal. The return value can be:\n\ |
| 278 | SIG_IGN -- if the signal is being ignored\n\ |
| 279 | SIG_DFL -- if the default action for the signal is in effect\n\ |
| 280 | None -- if an unknown handler is in effect\n\ |
| 281 | anything else -- the callable Python object used as a handler\n\ |
| 282 | "; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 283 | |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 284 | |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 285 | /* List of functions defined in the module */ |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 286 | static PyMethodDef signal_methods[] = { |
Guido van Rossum | 1171ee6 | 1997-08-22 20:42:00 +0000 | [diff] [blame] | 287 | #ifdef HAVE_ALARM |
Guido van Rossum | 1d8fb2d | 1998-06-28 16:54:49 +0000 | [diff] [blame] | 288 | {"alarm", signal_alarm, 0, alarm_doc}, |
Guido van Rossum | 06d511d | 1995-03-10 15:13:48 +0000 | [diff] [blame] | 289 | #endif |
Guido van Rossum | 1d8fb2d | 1998-06-28 16:54:49 +0000 | [diff] [blame] | 290 | {"signal", signal_signal, 0, signal_doc}, |
| 291 | {"getsignal", signal_getsignal, 0, getsignal_doc}, |
Guido van Rossum | 1171ee6 | 1997-08-22 20:42:00 +0000 | [diff] [blame] | 292 | #ifdef HAVE_PAUSE |
Guido van Rossum | 1d8fb2d | 1998-06-28 16:54:49 +0000 | [diff] [blame] | 293 | {"pause", signal_pause, 0, pause_doc}, |
Guido van Rossum | 06d511d | 1995-03-10 15:13:48 +0000 | [diff] [blame] | 294 | #endif |
Guido van Rossum | 1d8fb2d | 1998-06-28 16:54:49 +0000 | [diff] [blame] | 295 | {"default_int_handler", signal_default_int_handler, 0, |
| 296 | default_int_handler_doc}, |
| 297 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 298 | }; |
| 299 | |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 300 | |
| 301 | |
Guido van Rossum | 1d8fb2d | 1998-06-28 16:54:49 +0000 | [diff] [blame] | 302 | static char module_doc[] = |
| 303 | "This module provides mechanisms to use signal handlers in Python.\n\ |
| 304 | \n\ |
| 305 | Functions:\n\ |
| 306 | \n\ |
| 307 | alarm() -- cause SIGALRM after a specified time [Unix only]\n\ |
| 308 | signal() -- set the action for a given signal\n\ |
| 309 | getsignal() -- get the signal action for a given signal\n\ |
| 310 | pause() -- wait until a signal arrives [Unix only]\n\ |
| 311 | default_int_handler() -- default SIGINT handler\n\ |
| 312 | \n\ |
| 313 | Constants:\n\ |
| 314 | \n\ |
| 315 | SIG_DFL -- used to refer to the system default handler\n\ |
| 316 | SIG_IGN -- used to ignore the signal\n\ |
| 317 | NSIG -- number of defined signals\n\ |
| 318 | \n\ |
| 319 | SIGINT, SIGTERM, etc. -- signal numbers\n\ |
| 320 | \n\ |
| 321 | *** IMPORTANT NOTICE ***\n\ |
| 322 | A signal handler function is called with two arguments:\n\ |
| 323 | the first is the signal number, the second is the interrupted stack frame."; |
| 324 | |
Guido van Rossum | 3886bb6 | 1998-12-04 18:50:17 +0000 | [diff] [blame] | 325 | DL_EXPORT(void) |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 326 | initsignal() |
| 327 | { |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 328 | PyObject *m, *d, *x; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 329 | int i; |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 330 | |
| 331 | #ifdef WITH_THREAD |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 332 | main_thread = PyThread_get_thread_ident(); |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 333 | main_pid = getpid(); |
| 334 | #endif |
| 335 | |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 336 | /* Create the module and add the functions */ |
Guido van Rossum | 1d8fb2d | 1998-06-28 16:54:49 +0000 | [diff] [blame] | 337 | m = Py_InitModule3("signal", signal_methods, module_doc); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 338 | |
| 339 | /* Add some symbolic constants to the module */ |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 340 | d = PyModule_GetDict(m); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 341 | |
Guido van Rossum | 276fa43 | 2000-06-30 23:04:18 +0000 | [diff] [blame] | 342 | x = DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL); |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 343 | if (!x || PyDict_SetItemString(d, "SIG_DFL", x) < 0) |
| 344 | goto finally; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 345 | |
Guido van Rossum | 276fa43 | 2000-06-30 23:04:18 +0000 | [diff] [blame] | 346 | x = IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN); |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 347 | if (!x || PyDict_SetItemString(d, "SIG_IGN", x) < 0) |
| 348 | goto finally; |
| 349 | |
| 350 | x = PyInt_FromLong((long)NSIG); |
| 351 | if (!x || PyDict_SetItemString(d, "NSIG", x) < 0) |
| 352 | goto finally; |
Guido van Rossum | 08c1661 | 1997-08-02 03:01:42 +0000 | [diff] [blame] | 353 | Py_DECREF(x); |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 354 | |
| 355 | x = IntHandler = PyDict_GetItemString(d, "default_int_handler"); |
| 356 | if (!x) |
| 357 | goto finally; |
Guido van Rossum | 08c1661 | 1997-08-02 03:01:42 +0000 | [diff] [blame] | 358 | Py_INCREF(IntHandler); |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 359 | |
| 360 | Handlers[0].tripped = 0; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 361 | for (i = 1; i < NSIG; i++) { |
Guido van Rossum | 6299d1e | 1994-05-31 12:51:13 +0000 | [diff] [blame] | 362 | RETSIGTYPE (*t)(); |
Guido van Rossum | fb0df94 | 1996-05-23 22:55:35 +0000 | [diff] [blame] | 363 | #ifdef HAVE_SIGACTION |
| 364 | struct sigaction act; |
| 365 | sigaction(i, 0, &act); |
Guido van Rossum | 1835c4f | 1996-05-29 14:15:19 +0000 | [diff] [blame] | 366 | t = act.sa_handler; |
Guido van Rossum | fb0df94 | 1996-05-23 22:55:35 +0000 | [diff] [blame] | 367 | #else |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 368 | t = signal(i, SIG_IGN); |
| 369 | signal(i, t); |
Guido van Rossum | fb0df94 | 1996-05-23 22:55:35 +0000 | [diff] [blame] | 370 | #endif |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 371 | Handlers[i].tripped = 0; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 372 | if (t == SIG_DFL) |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 373 | Handlers[i].func = DefaultHandler; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 374 | else if (t == SIG_IGN) |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 375 | Handlers[i].func = IgnoreHandler; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 376 | else |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 377 | Handlers[i].func = Py_None; /* None of our business */ |
| 378 | Py_INCREF(Handlers[i].func); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 379 | } |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 380 | if (Handlers[SIGINT].func == DefaultHandler) { |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 381 | /* Install default int handler */ |
Guido van Rossum | 08c1661 | 1997-08-02 03:01:42 +0000 | [diff] [blame] | 382 | Py_INCREF(IntHandler); |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 383 | Py_DECREF(Handlers[SIGINT].func); |
| 384 | Handlers[SIGINT].func = IntHandler; |
Guido van Rossum | 08c1661 | 1997-08-02 03:01:42 +0000 | [diff] [blame] | 385 | old_siginthandler = signal(SIGINT, &signal_handler); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | #ifdef SIGHUP |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 389 | x = PyInt_FromLong(SIGHUP); |
| 390 | PyDict_SetItemString(d, "SIGHUP", x); |
Barry Warsaw | 73a75eb | 1997-01-09 23:50:28 +0000 | [diff] [blame] | 391 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 392 | #endif |
| 393 | #ifdef SIGINT |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 394 | x = PyInt_FromLong(SIGINT); |
| 395 | PyDict_SetItemString(d, "SIGINT", x); |
Barry Warsaw | 73a75eb | 1997-01-09 23:50:28 +0000 | [diff] [blame] | 396 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 397 | #endif |
| 398 | #ifdef SIGQUIT |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 399 | x = PyInt_FromLong(SIGQUIT); |
| 400 | PyDict_SetItemString(d, "SIGQUIT", x); |
Barry Warsaw | 73a75eb | 1997-01-09 23:50:28 +0000 | [diff] [blame] | 401 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 402 | #endif |
| 403 | #ifdef SIGILL |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 404 | x = PyInt_FromLong(SIGILL); |
| 405 | PyDict_SetItemString(d, "SIGILL", x); |
Barry Warsaw | 73a75eb | 1997-01-09 23:50:28 +0000 | [diff] [blame] | 406 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 407 | #endif |
| 408 | #ifdef SIGTRAP |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 409 | x = PyInt_FromLong(SIGTRAP); |
| 410 | PyDict_SetItemString(d, "SIGTRAP", x); |
Barry Warsaw | 73a75eb | 1997-01-09 23:50:28 +0000 | [diff] [blame] | 411 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 412 | #endif |
| 413 | #ifdef SIGIOT |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 414 | x = PyInt_FromLong(SIGIOT); |
| 415 | PyDict_SetItemString(d, "SIGIOT", x); |
Barry Warsaw | 73a75eb | 1997-01-09 23:50:28 +0000 | [diff] [blame] | 416 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 417 | #endif |
| 418 | #ifdef SIGABRT |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 419 | x = PyInt_FromLong(SIGABRT); |
| 420 | PyDict_SetItemString(d, "SIGABRT", x); |
Barry Warsaw | 73a75eb | 1997-01-09 23:50:28 +0000 | [diff] [blame] | 421 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 422 | #endif |
| 423 | #ifdef SIGEMT |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 424 | x = PyInt_FromLong(SIGEMT); |
| 425 | PyDict_SetItemString(d, "SIGEMT", x); |
Barry Warsaw | 73a75eb | 1997-01-09 23:50:28 +0000 | [diff] [blame] | 426 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 427 | #endif |
| 428 | #ifdef SIGFPE |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 429 | x = PyInt_FromLong(SIGFPE); |
| 430 | PyDict_SetItemString(d, "SIGFPE", x); |
Barry Warsaw | 73a75eb | 1997-01-09 23:50:28 +0000 | [diff] [blame] | 431 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 432 | #endif |
| 433 | #ifdef SIGKILL |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 434 | x = PyInt_FromLong(SIGKILL); |
| 435 | PyDict_SetItemString(d, "SIGKILL", x); |
Barry Warsaw | 73a75eb | 1997-01-09 23:50:28 +0000 | [diff] [blame] | 436 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 437 | #endif |
| 438 | #ifdef SIGBUS |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 439 | x = PyInt_FromLong(SIGBUS); |
| 440 | PyDict_SetItemString(d, "SIGBUS", x); |
Barry Warsaw | 73a75eb | 1997-01-09 23:50:28 +0000 | [diff] [blame] | 441 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 442 | #endif |
| 443 | #ifdef SIGSEGV |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 444 | x = PyInt_FromLong(SIGSEGV); |
| 445 | PyDict_SetItemString(d, "SIGSEGV", x); |
Barry Warsaw | 73a75eb | 1997-01-09 23:50:28 +0000 | [diff] [blame] | 446 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 447 | #endif |
| 448 | #ifdef SIGSYS |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 449 | x = PyInt_FromLong(SIGSYS); |
| 450 | PyDict_SetItemString(d, "SIGSYS", x); |
Barry Warsaw | 73a75eb | 1997-01-09 23:50:28 +0000 | [diff] [blame] | 451 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 452 | #endif |
| 453 | #ifdef SIGPIPE |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 454 | x = PyInt_FromLong(SIGPIPE); |
| 455 | PyDict_SetItemString(d, "SIGPIPE", x); |
Barry Warsaw | 73a75eb | 1997-01-09 23:50:28 +0000 | [diff] [blame] | 456 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 457 | #endif |
| 458 | #ifdef SIGALRM |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 459 | x = PyInt_FromLong(SIGALRM); |
| 460 | PyDict_SetItemString(d, "SIGALRM", x); |
Barry Warsaw | 73a75eb | 1997-01-09 23:50:28 +0000 | [diff] [blame] | 461 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 462 | #endif |
| 463 | #ifdef SIGTERM |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 464 | x = PyInt_FromLong(SIGTERM); |
| 465 | PyDict_SetItemString(d, "SIGTERM", x); |
Barry Warsaw | 73a75eb | 1997-01-09 23:50:28 +0000 | [diff] [blame] | 466 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 467 | #endif |
| 468 | #ifdef SIGUSR1 |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 469 | x = PyInt_FromLong(SIGUSR1); |
| 470 | PyDict_SetItemString(d, "SIGUSR1", x); |
Barry Warsaw | 73a75eb | 1997-01-09 23:50:28 +0000 | [diff] [blame] | 471 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 472 | #endif |
| 473 | #ifdef SIGUSR2 |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 474 | x = PyInt_FromLong(SIGUSR2); |
| 475 | PyDict_SetItemString(d, "SIGUSR2", x); |
Barry Warsaw | 73a75eb | 1997-01-09 23:50:28 +0000 | [diff] [blame] | 476 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 477 | #endif |
| 478 | #ifdef SIGCLD |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 479 | x = PyInt_FromLong(SIGCLD); |
| 480 | PyDict_SetItemString(d, "SIGCLD", x); |
Barry Warsaw | 73a75eb | 1997-01-09 23:50:28 +0000 | [diff] [blame] | 481 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 482 | #endif |
| 483 | #ifdef SIGCHLD |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 484 | x = PyInt_FromLong(SIGCHLD); |
| 485 | PyDict_SetItemString(d, "SIGCHLD", x); |
Barry Warsaw | 73a75eb | 1997-01-09 23:50:28 +0000 | [diff] [blame] | 486 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 487 | #endif |
| 488 | #ifdef SIGPWR |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 489 | x = PyInt_FromLong(SIGPWR); |
| 490 | PyDict_SetItemString(d, "SIGPWR", x); |
Barry Warsaw | 73a75eb | 1997-01-09 23:50:28 +0000 | [diff] [blame] | 491 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 492 | #endif |
| 493 | #ifdef SIGIO |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 494 | x = PyInt_FromLong(SIGIO); |
| 495 | PyDict_SetItemString(d, "SIGIO", x); |
Barry Warsaw | 73a75eb | 1997-01-09 23:50:28 +0000 | [diff] [blame] | 496 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 497 | #endif |
| 498 | #ifdef SIGURG |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 499 | x = PyInt_FromLong(SIGURG); |
| 500 | PyDict_SetItemString(d, "SIGURG", x); |
Barry Warsaw | 73a75eb | 1997-01-09 23:50:28 +0000 | [diff] [blame] | 501 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 502 | #endif |
| 503 | #ifdef SIGWINCH |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 504 | x = PyInt_FromLong(SIGWINCH); |
| 505 | PyDict_SetItemString(d, "SIGWINCH", x); |
Barry Warsaw | 73a75eb | 1997-01-09 23:50:28 +0000 | [diff] [blame] | 506 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 507 | #endif |
| 508 | #ifdef SIGPOLL |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 509 | x = PyInt_FromLong(SIGPOLL); |
| 510 | PyDict_SetItemString(d, "SIGPOLL", x); |
Barry Warsaw | 73a75eb | 1997-01-09 23:50:28 +0000 | [diff] [blame] | 511 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 512 | #endif |
| 513 | #ifdef SIGSTOP |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 514 | x = PyInt_FromLong(SIGSTOP); |
| 515 | PyDict_SetItemString(d, "SIGSTOP", x); |
Barry Warsaw | 73a75eb | 1997-01-09 23:50:28 +0000 | [diff] [blame] | 516 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 517 | #endif |
| 518 | #ifdef SIGTSTP |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 519 | x = PyInt_FromLong(SIGTSTP); |
| 520 | PyDict_SetItemString(d, "SIGTSTP", x); |
Barry Warsaw | 73a75eb | 1997-01-09 23:50:28 +0000 | [diff] [blame] | 521 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 522 | #endif |
| 523 | #ifdef SIGCONT |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 524 | x = PyInt_FromLong(SIGCONT); |
| 525 | PyDict_SetItemString(d, "SIGCONT", x); |
Barry Warsaw | 73a75eb | 1997-01-09 23:50:28 +0000 | [diff] [blame] | 526 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 527 | #endif |
| 528 | #ifdef SIGTTIN |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 529 | x = PyInt_FromLong(SIGTTIN); |
| 530 | PyDict_SetItemString(d, "SIGTTIN", x); |
Barry Warsaw | 73a75eb | 1997-01-09 23:50:28 +0000 | [diff] [blame] | 531 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 532 | #endif |
| 533 | #ifdef SIGTTOU |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 534 | x = PyInt_FromLong(SIGTTOU); |
| 535 | PyDict_SetItemString(d, "SIGTTOU", x); |
Barry Warsaw | 73a75eb | 1997-01-09 23:50:28 +0000 | [diff] [blame] | 536 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 537 | #endif |
| 538 | #ifdef SIGVTALRM |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 539 | x = PyInt_FromLong(SIGVTALRM); |
| 540 | PyDict_SetItemString(d, "SIGVTALRM", x); |
Barry Warsaw | 73a75eb | 1997-01-09 23:50:28 +0000 | [diff] [blame] | 541 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 542 | #endif |
| 543 | #ifdef SIGPROF |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 544 | x = PyInt_FromLong(SIGPROF); |
| 545 | PyDict_SetItemString(d, "SIGPROF", x); |
Barry Warsaw | 73a75eb | 1997-01-09 23:50:28 +0000 | [diff] [blame] | 546 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 547 | #endif |
Barry Warsaw | 14ed5fb | 1996-12-16 20:24:22 +0000 | [diff] [blame] | 548 | #ifdef SIGXCPU |
| 549 | x = PyInt_FromLong(SIGXCPU); |
| 550 | PyDict_SetItemString(d, "SIGXCPU", x); |
Barry Warsaw | 73a75eb | 1997-01-09 23:50:28 +0000 | [diff] [blame] | 551 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 552 | #endif |
Barry Warsaw | 14ed5fb | 1996-12-16 20:24:22 +0000 | [diff] [blame] | 553 | #ifdef SIGXFSZ |
| 554 | x = PyInt_FromLong(SIGXFSZ); |
| 555 | PyDict_SetItemString(d, "SIGXFSZ", x); |
Barry Warsaw | 73a75eb | 1997-01-09 23:50:28 +0000 | [diff] [blame] | 556 | Py_XDECREF(x); |
Barry Warsaw | 14ed5fb | 1996-12-16 20:24:22 +0000 | [diff] [blame] | 557 | #endif |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 558 | if (!PyErr_Occurred()) |
| 559 | return; |
| 560 | |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 561 | /* Check for errors */ |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 562 | finally: |
Guido van Rossum | 08c1661 | 1997-08-02 03:01:42 +0000 | [diff] [blame] | 563 | return; |
| 564 | } |
| 565 | |
| 566 | static void |
| 567 | finisignal() |
| 568 | { |
| 569 | int i; |
Guido van Rossum | 7ff20ac | 1997-11-03 21:53:55 +0000 | [diff] [blame] | 570 | PyObject *func; |
Guido van Rossum | 08c1661 | 1997-08-02 03:01:42 +0000 | [diff] [blame] | 571 | |
| 572 | signal(SIGINT, old_siginthandler); |
Guido van Rossum | 7ff20ac | 1997-11-03 21:53:55 +0000 | [diff] [blame] | 573 | old_siginthandler = SIG_DFL; |
Guido van Rossum | 08c1661 | 1997-08-02 03:01:42 +0000 | [diff] [blame] | 574 | |
| 575 | for (i = 1; i < NSIG; i++) { |
Guido van Rossum | 7ff20ac | 1997-11-03 21:53:55 +0000 | [diff] [blame] | 576 | func = Handlers[i].func; |
Guido van Rossum | 08c1661 | 1997-08-02 03:01:42 +0000 | [diff] [blame] | 577 | Handlers[i].tripped = 0; |
Guido van Rossum | 08c1661 | 1997-08-02 03:01:42 +0000 | [diff] [blame] | 578 | Handlers[i].func = NULL; |
Guido van Rossum | 7ff20ac | 1997-11-03 21:53:55 +0000 | [diff] [blame] | 579 | if (i != SIGINT && func != NULL && func != Py_None && |
| 580 | func != DefaultHandler && func != IgnoreHandler) |
| 581 | signal(i, SIG_DFL); |
| 582 | Py_XDECREF(func); |
Guido van Rossum | 08c1661 | 1997-08-02 03:01:42 +0000 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | Py_XDECREF(IntHandler); |
| 586 | IntHandler = NULL; |
| 587 | Py_XDECREF(DefaultHandler); |
| 588 | DefaultHandler = NULL; |
| 589 | Py_XDECREF(IgnoreHandler); |
| 590 | IgnoreHandler = NULL; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 591 | } |
| 592 | |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 593 | |
| 594 | |
| 595 | /* Declared in pyerrors.h */ |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 596 | int |
Guido van Rossum | ec25b91 | 1995-01-22 00:46:57 +0000 | [diff] [blame] | 597 | PyErr_CheckSignals() |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 598 | { |
| 599 | int i; |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 600 | PyObject *f; |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 601 | |
| 602 | if (!is_tripped) |
Guido van Rossum | 6299d1e | 1994-05-31 12:51:13 +0000 | [diff] [blame] | 603 | return 0; |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 604 | #ifdef WITH_THREAD |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 605 | if (PyThread_get_thread_ident() != main_thread) |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 606 | return 0; |
| 607 | #endif |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 608 | if (!(f = PyEval_GetFrame())) |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 609 | f = Py_None; |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 610 | |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 611 | for (i = 1; i < NSIG; i++) { |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 612 | if (Handlers[i].tripped) { |
| 613 | PyObject *result = NULL; |
| 614 | PyObject *arglist = Py_BuildValue("(iO)", i, f); |
| 615 | Handlers[i].tripped = 0; |
| 616 | |
| 617 | if (arglist) { |
| 618 | result = PyEval_CallObject(Handlers[i].func, |
| 619 | arglist); |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 620 | Py_DECREF(arglist); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 621 | } |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 622 | if (!result) |
Guido van Rossum | fcdd0e4 | 1997-01-21 06:13:09 +0000 | [diff] [blame] | 623 | return -1; |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 624 | |
| 625 | Py_DECREF(result); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 626 | } |
| 627 | } |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 628 | is_tripped = 0; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 629 | return 0; |
| 630 | } |
| 631 | |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 632 | |
| 633 | /* Replacements for intrcheck.c functionality |
| 634 | * Declared in pyerrors.h |
| 635 | */ |
| 636 | void |
| 637 | PyErr_SetInterrupt() |
| 638 | { |
| 639 | is_tripped++; |
| 640 | Handlers[SIGINT].tripped = 1; |
Guido van Rossum | fcdd0e4 | 1997-01-21 06:13:09 +0000 | [diff] [blame] | 641 | Py_AddPendingCall((int (*) Py_PROTO((ANY *)))PyErr_CheckSignals, NULL); |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 642 | } |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 643 | |
| 644 | void |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 645 | PyOS_InitInterrupts() |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 646 | { |
| 647 | initsignal(); |
Guido van Rossum | 08c1661 | 1997-08-02 03:01:42 +0000 | [diff] [blame] | 648 | _PyImport_FixupExtension("signal", "signal"); |
| 649 | } |
| 650 | |
| 651 | void |
| 652 | PyOS_FiniInterrupts() |
| 653 | { |
| 654 | finisignal(); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | int |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 658 | PyOS_InterruptOccurred() |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 659 | { |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 660 | if (Handlers[SIGINT].tripped) { |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 661 | #ifdef WITH_THREAD |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 662 | if (PyThread_get_thread_ident() != main_thread) |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 663 | return 0; |
| 664 | #endif |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 665 | Handlers[SIGINT].tripped = 0; |
Guido van Rossum | 6299d1e | 1994-05-31 12:51:13 +0000 | [diff] [blame] | 666 | return 1; |
| 667 | } |
| 668 | return 0; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 669 | } |
Guido van Rossum | 359bcaa | 1997-11-14 22:24:28 +0000 | [diff] [blame] | 670 | |
| 671 | void |
| 672 | PyOS_AfterFork() |
| 673 | { |
| 674 | #ifdef WITH_THREAD |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 675 | main_thread = PyThread_get_thread_ident(); |
Guido van Rossum | 359bcaa | 1997-11-14 22:24:28 +0000 | [diff] [blame] | 676 | main_pid = getpid(); |
| 677 | #endif |
| 678 | } |