Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 524b588 | 1995-01-04 19:10:35 +0000 | [diff] [blame] | 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 4 | |
| 5 | All Rights Reserved |
| 6 | |
| 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
| 9 | provided that the above copyright notice appear in all copies and that |
| 10 | both that copyright notice and this permission notice appear in |
| 11 | supporting documentation, and that the names of Stichting Mathematisch |
| 12 | Centrum or CWI not be used in advertising or publicity pertaining to |
| 13 | distribution of the software without specific, written prior permission. |
| 14 | |
| 15 | STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 16 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 17 | FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE |
| 18 | FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 19 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 20 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 21 | OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 22 | |
| 23 | ******************************************************************/ |
| 24 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 25 | /* Signal module -- many thanks to Lance Ellinghaus */ |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 26 | |
Guido van Rossum | 602099a | 1994-09-14 13:32:22 +0000 | [diff] [blame] | 27 | #include "Python.h" |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 28 | #include "intrcheck.h" |
| 29 | |
| 30 | #include <signal.h> |
| 31 | |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 32 | #ifndef SIG_ERR |
| 33 | #define SIG_ERR ((RETSIGTYPE (*)())-1) |
| 34 | #endif |
| 35 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 36 | #ifndef NSIG |
| 37 | #define NSIG (_SIGMAX + 1) /* For QNX */ |
| 38 | #endif |
| 39 | |
| 40 | |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 41 | /* |
| 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 Rossum | a3c04b0 | 1995-01-12 11:29:01 +0000 | [diff] [blame] | 60 | 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] | 61 | 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 |
| 68 | #include "thread.h" |
| 69 | static long main_thread; |
| 70 | static pid_t main_pid; |
| 71 | #endif |
| 72 | |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 73 | struct PySignal_SignalArrayStruct { |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 74 | int tripped; |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 75 | PyObject *func; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 76 | }; |
| 77 | |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 78 | static struct PySignal_SignalArrayStruct PySignal_SignalHandlerArray[NSIG]; |
| 79 | static int PySignal_IsTripped = 0; /* Speed up sigcheck() when none tripped */ |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 80 | |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 81 | static PyObject *PySignal_SignalDefaultHandler; |
| 82 | static PyObject *PySignal_SignalIgnoreHandler; |
| 83 | static PyObject *PySignal_DefaultIntHandler; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 84 | |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 85 | static PyObject * |
| 86 | PySignal_CDefaultIntHandler(self, arg) |
| 87 | PyObject *self; |
| 88 | PyObject *arg; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 89 | { |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 90 | PyErr_SetNone(PyExc_KeyboardInterrupt); |
| 91 | return (PyObject *)NULL; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Guido van Rossum | 06d511d | 1995-03-10 15:13:48 +0000 | [diff] [blame] | 94 | void |
| 95 | PyErr_SetInterrupt() |
| 96 | { |
| 97 | PySignal_IsTripped++; |
| 98 | PySignal_SignalHandlerArray[SIGINT].tripped = 1; |
| 99 | } |
| 100 | |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 101 | static RETSIGTYPE |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 102 | PySignal_Handler(sig_num) |
| 103 | int sig_num; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 104 | { |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 105 | #ifdef WITH_THREAD |
| 106 | /* See NOTES section above */ |
| 107 | if (getpid() == main_pid) { |
| 108 | #endif |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 109 | PySignal_IsTripped++; |
| 110 | PySignal_SignalHandlerArray[sig_num].tripped = 1; |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 111 | #ifdef WITH_THREAD |
| 112 | } |
| 113 | #endif |
Guido van Rossum | 602099a | 1994-09-14 13:32:22 +0000 | [diff] [blame] | 114 | #ifdef SIGCHLD |
| 115 | if (sig_num == SIGCHLD) { |
| 116 | /* To avoid infinite recursion, this signal remains |
| 117 | reset until explicit re-instated. |
| 118 | Don't clear the 'func' field as it is our pointer |
| 119 | to the Python handler... */ |
| 120 | return; |
| 121 | } |
| 122 | #endif |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 123 | (void *)signal(sig_num, &PySignal_Handler); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 124 | } |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 125 | |
Guido van Rossum | 06d511d | 1995-03-10 15:13:48 +0000 | [diff] [blame] | 126 | |
| 127 | #ifndef DONT_HAVE_SIG_ALARM |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 128 | static PyObject * |
| 129 | PySignal_Alarm(self, args) |
| 130 | PyObject *self; /* Not used */ |
| 131 | PyObject *args; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 132 | { |
| 133 | int t; |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 134 | if (!PyArg_Parse(args, "i", &t)) |
| 135 | return (PyObject *)NULL; |
| 136 | /* alarm() returns the number of seconds remaining */ |
| 137 | return PyInt_FromLong(alarm(t)); |
Guido van Rossum | aa0f4c7 | 1994-08-23 13:49:37 +0000 | [diff] [blame] | 138 | } |
Guido van Rossum | 06d511d | 1995-03-10 15:13:48 +0000 | [diff] [blame] | 139 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 140 | |
Guido van Rossum | 06d511d | 1995-03-10 15:13:48 +0000 | [diff] [blame] | 141 | #ifndef DONT_HAVE_SIG_PAUSE |
Guido van Rossum | a597dde | 1995-01-10 20:56:29 +0000 | [diff] [blame] | 142 | static PyObject * |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 143 | PySignal_Pause(self, args) |
| 144 | PyObject *self; /* Not used */ |
| 145 | PyObject *args; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 146 | { |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 147 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 148 | return NULL; |
Guido van Rossum | a597dde | 1995-01-10 20:56:29 +0000 | [diff] [blame] | 149 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 150 | pause(); |
Guido van Rossum | a597dde | 1995-01-10 20:56:29 +0000 | [diff] [blame] | 151 | Py_END_ALLOW_THREADS |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 152 | Py_INCREF(Py_None); |
| 153 | return Py_None; |
| 154 | } |
Guido van Rossum | 06d511d | 1995-03-10 15:13:48 +0000 | [diff] [blame] | 155 | #endif |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 156 | |
| 157 | static PyObject * |
| 158 | PySignal_Signal(self, args) |
| 159 | PyObject *self; /* Not used */ |
| 160 | PyObject *args; |
| 161 | { |
| 162 | PyObject *obj; |
| 163 | int sig_num; |
| 164 | PyObject *old_handler; |
| 165 | RETSIGTYPE (*func)(); |
| 166 | if (!PyArg_Parse(args, "(iO)", &sig_num, &obj)) |
| 167 | return (PyObject *)NULL; |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 168 | #ifdef WITH_THREAD |
| 169 | if (get_thread_ident() != main_thread) { |
Guido van Rossum | f4b012a | 1995-03-16 15:43:29 +0000 | [diff] [blame] | 170 | PyErr_SetString(PyExc_ValueError, |
| 171 | "signal only works in main thread"); |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 172 | return (PyObject *)NULL; |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 173 | } |
| 174 | #endif |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 175 | if (sig_num < 1 || sig_num >= NSIG) { |
Guido van Rossum | f4b012a | 1995-03-16 15:43:29 +0000 | [diff] [blame] | 176 | PyErr_SetString(PyExc_ValueError, |
| 177 | "signal number out of range"); |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 178 | return (PyObject *)NULL; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 179 | } |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 180 | if (obj == PySignal_SignalIgnoreHandler) |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 181 | func = SIG_IGN; |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 182 | else if (obj == PySignal_SignalDefaultHandler) |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 183 | func = SIG_DFL; |
Guido van Rossum | f4b012a | 1995-03-16 15:43:29 +0000 | [diff] [blame] | 184 | else if (!PyCallable_Check(obj)) { |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 185 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 186 | "signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object"); |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 187 | return (PyObject *)NULL; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 188 | } |
| 189 | else |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 190 | func = PySignal_Handler; |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 191 | if (signal(sig_num, func) == SIG_ERR) { |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 192 | PyErr_SetFromErrno(PyExc_RuntimeError); |
| 193 | return (PyObject *)NULL; |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 194 | } |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 195 | old_handler = PySignal_SignalHandlerArray[sig_num].func; |
| 196 | PySignal_SignalHandlerArray[sig_num].tripped = 0; |
| 197 | Py_INCREF(obj); |
| 198 | PySignal_SignalHandlerArray[sig_num].func = obj; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 199 | return old_handler; |
| 200 | } |
| 201 | |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 202 | static PyObject * |
| 203 | PySignal_GetSignal(self, args) |
| 204 | PyObject *self; /* Not used */ |
| 205 | PyObject *args; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 206 | { |
| 207 | int sig_num; |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 208 | PyObject *old_handler; |
| 209 | if (!PyArg_Parse(args, "i", &sig_num)) |
| 210 | return (PyObject *)NULL; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 211 | if (sig_num < 1 || sig_num >= NSIG) { |
Guido van Rossum | f4b012a | 1995-03-16 15:43:29 +0000 | [diff] [blame] | 212 | PyErr_SetString(PyExc_ValueError, |
| 213 | "signal number out of range"); |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 214 | return (PyObject *)NULL; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 215 | } |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 216 | old_handler = PySignal_SignalHandlerArray[sig_num].func; |
| 217 | Py_INCREF(old_handler); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 218 | return old_handler; |
| 219 | } |
| 220 | |
| 221 | |
| 222 | /* List of functions defined in the module */ |
| 223 | |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 224 | static PyMethodDef PySignal_methods[] = { |
Guido van Rossum | 06d511d | 1995-03-10 15:13:48 +0000 | [diff] [blame] | 225 | #ifndef DONT_HAVE_SIG_ALARM |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 226 | {"alarm", PySignal_Alarm}, |
Guido van Rossum | 06d511d | 1995-03-10 15:13:48 +0000 | [diff] [blame] | 227 | #endif |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 228 | {"signal", PySignal_Signal}, |
| 229 | {"getsignal", PySignal_GetSignal}, |
Guido van Rossum | 06d511d | 1995-03-10 15:13:48 +0000 | [diff] [blame] | 230 | #ifndef DONT_HAVE_SIG_PAUSE |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 231 | {"pause", PySignal_Pause}, |
Guido van Rossum | 06d511d | 1995-03-10 15:13:48 +0000 | [diff] [blame] | 232 | #endif |
Guido van Rossum | 9558825 | 1995-01-07 11:50:04 +0000 | [diff] [blame] | 233 | {"default_int_handler", PySignal_CDefaultIntHandler}, |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 234 | {NULL, NULL} /* sentinel */ |
| 235 | }; |
| 236 | |
| 237 | void |
| 238 | initsignal() |
| 239 | { |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 240 | PyObject *m, *d, *x; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 241 | int i; |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 242 | |
| 243 | #ifdef WITH_THREAD |
| 244 | main_thread = get_thread_ident(); |
| 245 | main_pid = getpid(); |
| 246 | #endif |
| 247 | |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 248 | /* Create the module and add the functions */ |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 249 | m = Py_InitModule("signal", PySignal_methods); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 250 | |
| 251 | /* Add some symbolic constants to the module */ |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 252 | d = PyModule_GetDict(m); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 253 | |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 254 | PySignal_SignalDefaultHandler = PyInt_FromLong((long)SIG_DFL); |
| 255 | PyDict_SetItemString(d, "SIG_DFL", PySignal_SignalDefaultHandler); |
| 256 | PySignal_SignalIgnoreHandler = PyInt_FromLong((long)SIG_IGN); |
| 257 | PyDict_SetItemString(d, "SIG_IGN", PySignal_SignalIgnoreHandler); |
| 258 | PyDict_SetItemString(d, "NSIG", PyInt_FromLong((long)NSIG)); |
Guido van Rossum | 9558825 | 1995-01-07 11:50:04 +0000 | [diff] [blame] | 259 | PySignal_DefaultIntHandler = |
| 260 | PyDict_GetItemString(d, "default_int_handler"); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 261 | |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 262 | PySignal_SignalHandlerArray[0].tripped = 0; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 263 | for (i = 1; i < NSIG; i++) { |
Guido van Rossum | 6299d1e | 1994-05-31 12:51:13 +0000 | [diff] [blame] | 264 | RETSIGTYPE (*t)(); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 265 | t = signal(i, SIG_IGN); |
| 266 | signal(i, t); |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 267 | PySignal_SignalHandlerArray[i].tripped = 0; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 268 | if (t == SIG_DFL) |
Guido van Rossum | f4b012a | 1995-03-16 15:43:29 +0000 | [diff] [blame] | 269 | PySignal_SignalHandlerArray[i].func = |
| 270 | PySignal_SignalDefaultHandler; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 271 | else if (t == SIG_IGN) |
Guido van Rossum | f4b012a | 1995-03-16 15:43:29 +0000 | [diff] [blame] | 272 | PySignal_SignalHandlerArray[i].func = |
| 273 | PySignal_SignalIgnoreHandler; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 274 | else |
Guido van Rossum | f4b012a | 1995-03-16 15:43:29 +0000 | [diff] [blame] | 275 | PySignal_SignalHandlerArray[i].func = |
| 276 | Py_None; /* None of our business */ |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 277 | Py_INCREF(PySignal_SignalHandlerArray[i].func); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 278 | } |
Guido van Rossum | f4b012a | 1995-03-16 15:43:29 +0000 | [diff] [blame] | 279 | if (PySignal_SignalHandlerArray[SIGINT].func == |
| 280 | PySignal_SignalDefaultHandler) { |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 281 | /* Install default int handler */ |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 282 | Py_DECREF(PySignal_SignalHandlerArray[SIGINT].func); |
Guido van Rossum | f4b012a | 1995-03-16 15:43:29 +0000 | [diff] [blame] | 283 | PySignal_SignalHandlerArray[SIGINT].func = |
| 284 | PySignal_DefaultIntHandler; |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 285 | Py_INCREF(PySignal_DefaultIntHandler); |
| 286 | signal(SIGINT, &PySignal_Handler); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | #ifdef SIGHUP |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 290 | x = PyInt_FromLong(SIGHUP); |
| 291 | PyDict_SetItemString(d, "SIGHUP", x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 292 | #endif |
| 293 | #ifdef SIGINT |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 294 | x = PyInt_FromLong(SIGINT); |
| 295 | PyDict_SetItemString(d, "SIGINT", x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 296 | #endif |
| 297 | #ifdef SIGQUIT |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 298 | x = PyInt_FromLong(SIGQUIT); |
| 299 | PyDict_SetItemString(d, "SIGQUIT", x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 300 | #endif |
| 301 | #ifdef SIGILL |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 302 | x = PyInt_FromLong(SIGILL); |
| 303 | PyDict_SetItemString(d, "SIGILL", x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 304 | #endif |
| 305 | #ifdef SIGTRAP |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 306 | x = PyInt_FromLong(SIGTRAP); |
| 307 | PyDict_SetItemString(d, "SIGTRAP", x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 308 | #endif |
| 309 | #ifdef SIGIOT |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 310 | x = PyInt_FromLong(SIGIOT); |
| 311 | PyDict_SetItemString(d, "SIGIOT", x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 312 | #endif |
| 313 | #ifdef SIGABRT |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 314 | x = PyInt_FromLong(SIGABRT); |
| 315 | PyDict_SetItemString(d, "SIGABRT", x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 316 | #endif |
| 317 | #ifdef SIGEMT |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 318 | x = PyInt_FromLong(SIGEMT); |
| 319 | PyDict_SetItemString(d, "SIGEMT", x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 320 | #endif |
| 321 | #ifdef SIGFPE |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 322 | x = PyInt_FromLong(SIGFPE); |
| 323 | PyDict_SetItemString(d, "SIGFPE", x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 324 | #endif |
| 325 | #ifdef SIGKILL |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 326 | x = PyInt_FromLong(SIGKILL); |
| 327 | PyDict_SetItemString(d, "SIGKILL", x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 328 | #endif |
| 329 | #ifdef SIGBUS |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 330 | x = PyInt_FromLong(SIGBUS); |
| 331 | PyDict_SetItemString(d, "SIGBUS", x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 332 | #endif |
| 333 | #ifdef SIGSEGV |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 334 | x = PyInt_FromLong(SIGSEGV); |
| 335 | PyDict_SetItemString(d, "SIGSEGV", x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 336 | #endif |
| 337 | #ifdef SIGSYS |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 338 | x = PyInt_FromLong(SIGSYS); |
| 339 | PyDict_SetItemString(d, "SIGSYS", x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 340 | #endif |
| 341 | #ifdef SIGPIPE |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 342 | x = PyInt_FromLong(SIGPIPE); |
| 343 | PyDict_SetItemString(d, "SIGPIPE", x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 344 | #endif |
| 345 | #ifdef SIGALRM |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 346 | x = PyInt_FromLong(SIGALRM); |
| 347 | PyDict_SetItemString(d, "SIGALRM", x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 348 | #endif |
| 349 | #ifdef SIGTERM |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 350 | x = PyInt_FromLong(SIGTERM); |
| 351 | PyDict_SetItemString(d, "SIGTERM", x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 352 | #endif |
| 353 | #ifdef SIGUSR1 |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 354 | x = PyInt_FromLong(SIGUSR1); |
| 355 | PyDict_SetItemString(d, "SIGUSR1", x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 356 | #endif |
| 357 | #ifdef SIGUSR2 |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 358 | x = PyInt_FromLong(SIGUSR2); |
| 359 | PyDict_SetItemString(d, "SIGUSR2", x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 360 | #endif |
| 361 | #ifdef SIGCLD |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 362 | x = PyInt_FromLong(SIGCLD); |
| 363 | PyDict_SetItemString(d, "SIGCLD", x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 364 | #endif |
| 365 | #ifdef SIGCHLD |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 366 | x = PyInt_FromLong(SIGCHLD); |
| 367 | PyDict_SetItemString(d, "SIGCHLD", x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 368 | #endif |
| 369 | #ifdef SIGPWR |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 370 | x = PyInt_FromLong(SIGPWR); |
| 371 | PyDict_SetItemString(d, "SIGPWR", x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 372 | #endif |
| 373 | #ifdef SIGIO |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 374 | x = PyInt_FromLong(SIGIO); |
| 375 | PyDict_SetItemString(d, "SIGIO", x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 376 | #endif |
| 377 | #ifdef SIGURG |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 378 | x = PyInt_FromLong(SIGURG); |
| 379 | PyDict_SetItemString(d, "SIGURG", x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 380 | #endif |
| 381 | #ifdef SIGWINCH |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 382 | x = PyInt_FromLong(SIGWINCH); |
| 383 | PyDict_SetItemString(d, "SIGWINCH", x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 384 | #endif |
| 385 | #ifdef SIGPOLL |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 386 | x = PyInt_FromLong(SIGPOLL); |
| 387 | PyDict_SetItemString(d, "SIGPOLL", x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 388 | #endif |
| 389 | #ifdef SIGSTOP |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 390 | x = PyInt_FromLong(SIGSTOP); |
| 391 | PyDict_SetItemString(d, "SIGSTOP", x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 392 | #endif |
| 393 | #ifdef SIGTSTP |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 394 | x = PyInt_FromLong(SIGTSTP); |
| 395 | PyDict_SetItemString(d, "SIGTSTP", x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 396 | #endif |
| 397 | #ifdef SIGCONT |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 398 | x = PyInt_FromLong(SIGCONT); |
| 399 | PyDict_SetItemString(d, "SIGCONT", x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 400 | #endif |
| 401 | #ifdef SIGTTIN |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 402 | x = PyInt_FromLong(SIGTTIN); |
| 403 | PyDict_SetItemString(d, "SIGTTIN", x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 404 | #endif |
| 405 | #ifdef SIGTTOU |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 406 | x = PyInt_FromLong(SIGTTOU); |
| 407 | PyDict_SetItemString(d, "SIGTTOU", x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 408 | #endif |
| 409 | #ifdef SIGVTALRM |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 410 | x = PyInt_FromLong(SIGVTALRM); |
| 411 | PyDict_SetItemString(d, "SIGVTALRM", x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 412 | #endif |
| 413 | #ifdef SIGPROF |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 414 | x = PyInt_FromLong(SIGPROF); |
| 415 | PyDict_SetItemString(d, "SIGPROF", x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 416 | #endif |
| 417 | #ifdef SIGCPU |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 418 | x = PyInt_FromLong(SIGCPU); |
| 419 | PyDict_SetItemString(d, "SIGCPU", x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 420 | #endif |
| 421 | #ifdef SIGFSZ |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 422 | x = PyInt_FromLong(SIGFSZ); |
| 423 | PyDict_SetItemString(d, "SIGFSZ", x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 424 | #endif |
| 425 | /* Check for errors */ |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 426 | if (PyErr_Occurred()) |
| 427 | Py_FatalError("can't initialize module signal"); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | int |
Guido van Rossum | ec25b91 | 1995-01-22 00:46:57 +0000 | [diff] [blame] | 431 | PyErr_CheckSignals() |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 432 | { |
| 433 | int i; |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 434 | PyObject *f; |
| 435 | if (!PySignal_IsTripped) |
Guido van Rossum | 6299d1e | 1994-05-31 12:51:13 +0000 | [diff] [blame] | 436 | return 0; |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 437 | #ifdef WITH_THREAD |
| 438 | if (get_thread_ident() != main_thread) |
| 439 | return 0; |
| 440 | #endif |
Guido van Rossum | a597dde | 1995-01-10 20:56:29 +0000 | [diff] [blame] | 441 | f = PyEval_GetFrame(); |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 442 | if (f == (PyObject *)NULL) |
| 443 | f = Py_None; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 444 | for (i = 1; i < NSIG; i++) { |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 445 | if (PySignal_SignalHandlerArray[i].tripped) { |
| 446 | PyObject *arglist, *result; |
| 447 | PySignal_SignalHandlerArray[i].tripped = 0; |
| 448 | arglist = Py_BuildValue("(iO)", i, f); |
| 449 | if (arglist == (PyObject *)NULL) |
| 450 | result = (PyObject *)NULL; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 451 | else { |
Guido van Rossum | f4b012a | 1995-03-16 15:43:29 +0000 | [diff] [blame] | 452 | result = PyEval_CallObject( |
| 453 | PySignal_SignalHandlerArray[i].func, arglist); |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 454 | Py_DECREF(arglist); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 455 | } |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 456 | if (result == (PyObject *)NULL) { |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 457 | return 1; |
| 458 | } else { |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 459 | Py_DECREF(result); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 460 | } |
| 461 | } |
| 462 | } |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 463 | PySignal_IsTripped = 0; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 464 | return 0; |
| 465 | } |
| 466 | |
| 467 | /* Replacement for intrcheck.c functionality */ |
| 468 | |
| 469 | void |
Guido van Rossum | a597dde | 1995-01-10 20:56:29 +0000 | [diff] [blame] | 470 | PyOS_InitInterrupts () |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 471 | { |
| 472 | initsignal(); |
| 473 | } |
| 474 | |
| 475 | int |
Guido van Rossum | a597dde | 1995-01-10 20:56:29 +0000 | [diff] [blame] | 476 | PyOS_InterruptOccurred () |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 477 | { |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 478 | if (PySignal_SignalHandlerArray[SIGINT].tripped) { |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 479 | #ifdef WITH_THREAD |
| 480 | if (get_thread_ident() != main_thread) |
| 481 | return 0; |
| 482 | #endif |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 483 | PySignal_SignalHandlerArray[SIGINT].tripped = 0; |
Guido van Rossum | 6299d1e | 1994-05-31 12:51:13 +0000 | [diff] [blame] | 484 | return 1; |
| 485 | } |
| 486 | return 0; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 487 | } |