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 | fb0df94 | 1996-05-23 22:55:35 +0000 | [diff] [blame] | 265 | #ifdef HAVE_SIGACTION |
| 266 | struct sigaction act; |
| 267 | sigaction(i, 0, &act); |
| 268 | t = act.sa_flags; |
| 269 | #else |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 270 | t = signal(i, SIG_IGN); |
| 271 | signal(i, t); |
Guido van Rossum | fb0df94 | 1996-05-23 22:55:35 +0000 | [diff] [blame] | 272 | #endif |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 273 | PySignal_SignalHandlerArray[i].tripped = 0; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 274 | if (t == SIG_DFL) |
Guido van Rossum | f4b012a | 1995-03-16 15:43:29 +0000 | [diff] [blame] | 275 | PySignal_SignalHandlerArray[i].func = |
| 276 | PySignal_SignalDefaultHandler; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 277 | else if (t == SIG_IGN) |
Guido van Rossum | f4b012a | 1995-03-16 15:43:29 +0000 | [diff] [blame] | 278 | PySignal_SignalHandlerArray[i].func = |
| 279 | PySignal_SignalIgnoreHandler; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 280 | else |
Guido van Rossum | f4b012a | 1995-03-16 15:43:29 +0000 | [diff] [blame] | 281 | PySignal_SignalHandlerArray[i].func = |
| 282 | Py_None; /* None of our business */ |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 283 | Py_INCREF(PySignal_SignalHandlerArray[i].func); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 284 | } |
Guido van Rossum | f4b012a | 1995-03-16 15:43:29 +0000 | [diff] [blame] | 285 | if (PySignal_SignalHandlerArray[SIGINT].func == |
| 286 | PySignal_SignalDefaultHandler) { |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 287 | /* Install default int handler */ |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 288 | Py_DECREF(PySignal_SignalHandlerArray[SIGINT].func); |
Guido van Rossum | f4b012a | 1995-03-16 15:43:29 +0000 | [diff] [blame] | 289 | PySignal_SignalHandlerArray[SIGINT].func = |
| 290 | PySignal_DefaultIntHandler; |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 291 | Py_INCREF(PySignal_DefaultIntHandler); |
| 292 | signal(SIGINT, &PySignal_Handler); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | #ifdef SIGHUP |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 296 | x = PyInt_FromLong(SIGHUP); |
| 297 | PyDict_SetItemString(d, "SIGHUP", x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 298 | #endif |
| 299 | #ifdef SIGINT |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 300 | x = PyInt_FromLong(SIGINT); |
| 301 | PyDict_SetItemString(d, "SIGINT", x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 302 | #endif |
| 303 | #ifdef SIGQUIT |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 304 | x = PyInt_FromLong(SIGQUIT); |
| 305 | PyDict_SetItemString(d, "SIGQUIT", x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 306 | #endif |
| 307 | #ifdef SIGILL |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 308 | x = PyInt_FromLong(SIGILL); |
| 309 | PyDict_SetItemString(d, "SIGILL", x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 310 | #endif |
| 311 | #ifdef SIGTRAP |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 312 | x = PyInt_FromLong(SIGTRAP); |
| 313 | PyDict_SetItemString(d, "SIGTRAP", x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 314 | #endif |
| 315 | #ifdef SIGIOT |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 316 | x = PyInt_FromLong(SIGIOT); |
| 317 | PyDict_SetItemString(d, "SIGIOT", x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 318 | #endif |
| 319 | #ifdef SIGABRT |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 320 | x = PyInt_FromLong(SIGABRT); |
| 321 | PyDict_SetItemString(d, "SIGABRT", x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 322 | #endif |
| 323 | #ifdef SIGEMT |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 324 | x = PyInt_FromLong(SIGEMT); |
| 325 | PyDict_SetItemString(d, "SIGEMT", x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 326 | #endif |
| 327 | #ifdef SIGFPE |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 328 | x = PyInt_FromLong(SIGFPE); |
| 329 | PyDict_SetItemString(d, "SIGFPE", x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 330 | #endif |
| 331 | #ifdef SIGKILL |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 332 | x = PyInt_FromLong(SIGKILL); |
| 333 | PyDict_SetItemString(d, "SIGKILL", x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 334 | #endif |
| 335 | #ifdef SIGBUS |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 336 | x = PyInt_FromLong(SIGBUS); |
| 337 | PyDict_SetItemString(d, "SIGBUS", x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 338 | #endif |
| 339 | #ifdef SIGSEGV |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 340 | x = PyInt_FromLong(SIGSEGV); |
| 341 | PyDict_SetItemString(d, "SIGSEGV", x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 342 | #endif |
| 343 | #ifdef SIGSYS |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 344 | x = PyInt_FromLong(SIGSYS); |
| 345 | PyDict_SetItemString(d, "SIGSYS", x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 346 | #endif |
| 347 | #ifdef SIGPIPE |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 348 | x = PyInt_FromLong(SIGPIPE); |
| 349 | PyDict_SetItemString(d, "SIGPIPE", x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 350 | #endif |
| 351 | #ifdef SIGALRM |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 352 | x = PyInt_FromLong(SIGALRM); |
| 353 | PyDict_SetItemString(d, "SIGALRM", x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 354 | #endif |
| 355 | #ifdef SIGTERM |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 356 | x = PyInt_FromLong(SIGTERM); |
| 357 | PyDict_SetItemString(d, "SIGTERM", x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 358 | #endif |
| 359 | #ifdef SIGUSR1 |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 360 | x = PyInt_FromLong(SIGUSR1); |
| 361 | PyDict_SetItemString(d, "SIGUSR1", x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 362 | #endif |
| 363 | #ifdef SIGUSR2 |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 364 | x = PyInt_FromLong(SIGUSR2); |
| 365 | PyDict_SetItemString(d, "SIGUSR2", x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 366 | #endif |
| 367 | #ifdef SIGCLD |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 368 | x = PyInt_FromLong(SIGCLD); |
| 369 | PyDict_SetItemString(d, "SIGCLD", x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 370 | #endif |
| 371 | #ifdef SIGCHLD |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 372 | x = PyInt_FromLong(SIGCHLD); |
| 373 | PyDict_SetItemString(d, "SIGCHLD", x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 374 | #endif |
| 375 | #ifdef SIGPWR |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 376 | x = PyInt_FromLong(SIGPWR); |
| 377 | PyDict_SetItemString(d, "SIGPWR", x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 378 | #endif |
| 379 | #ifdef SIGIO |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 380 | x = PyInt_FromLong(SIGIO); |
| 381 | PyDict_SetItemString(d, "SIGIO", x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 382 | #endif |
| 383 | #ifdef SIGURG |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 384 | x = PyInt_FromLong(SIGURG); |
| 385 | PyDict_SetItemString(d, "SIGURG", x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 386 | #endif |
| 387 | #ifdef SIGWINCH |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 388 | x = PyInt_FromLong(SIGWINCH); |
| 389 | PyDict_SetItemString(d, "SIGWINCH", x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 390 | #endif |
| 391 | #ifdef SIGPOLL |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 392 | x = PyInt_FromLong(SIGPOLL); |
| 393 | PyDict_SetItemString(d, "SIGPOLL", x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 394 | #endif |
| 395 | #ifdef SIGSTOP |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 396 | x = PyInt_FromLong(SIGSTOP); |
| 397 | PyDict_SetItemString(d, "SIGSTOP", x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 398 | #endif |
| 399 | #ifdef SIGTSTP |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 400 | x = PyInt_FromLong(SIGTSTP); |
| 401 | PyDict_SetItemString(d, "SIGTSTP", x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 402 | #endif |
| 403 | #ifdef SIGCONT |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 404 | x = PyInt_FromLong(SIGCONT); |
| 405 | PyDict_SetItemString(d, "SIGCONT", x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 406 | #endif |
| 407 | #ifdef SIGTTIN |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 408 | x = PyInt_FromLong(SIGTTIN); |
| 409 | PyDict_SetItemString(d, "SIGTTIN", x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 410 | #endif |
| 411 | #ifdef SIGTTOU |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 412 | x = PyInt_FromLong(SIGTTOU); |
| 413 | PyDict_SetItemString(d, "SIGTTOU", x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 414 | #endif |
| 415 | #ifdef SIGVTALRM |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 416 | x = PyInt_FromLong(SIGVTALRM); |
| 417 | PyDict_SetItemString(d, "SIGVTALRM", x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 418 | #endif |
| 419 | #ifdef SIGPROF |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 420 | x = PyInt_FromLong(SIGPROF); |
| 421 | PyDict_SetItemString(d, "SIGPROF", x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 422 | #endif |
| 423 | #ifdef SIGCPU |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 424 | x = PyInt_FromLong(SIGCPU); |
| 425 | PyDict_SetItemString(d, "SIGCPU", x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 426 | #endif |
| 427 | #ifdef SIGFSZ |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 428 | x = PyInt_FromLong(SIGFSZ); |
| 429 | PyDict_SetItemString(d, "SIGFSZ", x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 430 | #endif |
| 431 | /* Check for errors */ |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 432 | if (PyErr_Occurred()) |
| 433 | Py_FatalError("can't initialize module signal"); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | int |
Guido van Rossum | ec25b91 | 1995-01-22 00:46:57 +0000 | [diff] [blame] | 437 | PyErr_CheckSignals() |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 438 | { |
| 439 | int i; |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 440 | PyObject *f; |
| 441 | if (!PySignal_IsTripped) |
Guido van Rossum | 6299d1e | 1994-05-31 12:51:13 +0000 | [diff] [blame] | 442 | return 0; |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 443 | #ifdef WITH_THREAD |
| 444 | if (get_thread_ident() != main_thread) |
| 445 | return 0; |
| 446 | #endif |
Guido van Rossum | a597dde | 1995-01-10 20:56:29 +0000 | [diff] [blame] | 447 | f = PyEval_GetFrame(); |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 448 | if (f == (PyObject *)NULL) |
| 449 | f = Py_None; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 450 | for (i = 1; i < NSIG; i++) { |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 451 | if (PySignal_SignalHandlerArray[i].tripped) { |
| 452 | PyObject *arglist, *result; |
| 453 | PySignal_SignalHandlerArray[i].tripped = 0; |
| 454 | arglist = Py_BuildValue("(iO)", i, f); |
| 455 | if (arglist == (PyObject *)NULL) |
| 456 | result = (PyObject *)NULL; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 457 | else { |
Guido van Rossum | f4b012a | 1995-03-16 15:43:29 +0000 | [diff] [blame] | 458 | result = PyEval_CallObject( |
| 459 | PySignal_SignalHandlerArray[i].func, arglist); |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 460 | Py_DECREF(arglist); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 461 | } |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 462 | if (result == (PyObject *)NULL) { |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 463 | return 1; |
| 464 | } else { |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 465 | Py_DECREF(result); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 466 | } |
| 467 | } |
| 468 | } |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 469 | PySignal_IsTripped = 0; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 470 | return 0; |
| 471 | } |
| 472 | |
| 473 | /* Replacement for intrcheck.c functionality */ |
| 474 | |
| 475 | void |
Guido van Rossum | a597dde | 1995-01-10 20:56:29 +0000 | [diff] [blame] | 476 | PyOS_InitInterrupts () |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 477 | { |
| 478 | initsignal(); |
| 479 | } |
| 480 | |
| 481 | int |
Guido van Rossum | a597dde | 1995-01-10 20:56:29 +0000 | [diff] [blame] | 482 | PyOS_InterruptOccurred () |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 483 | { |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 484 | if (PySignal_SignalHandlerArray[SIGINT].tripped) { |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 485 | #ifdef WITH_THREAD |
| 486 | if (get_thread_ident() != main_thread) |
| 487 | return 0; |
| 488 | #endif |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 489 | PySignal_SignalHandlerArray[SIGINT].tripped = 0; |
Guido van Rossum | 6299d1e | 1994-05-31 12:51:13 +0000 | [diff] [blame] | 490 | return 1; |
| 491 | } |
| 492 | return 0; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 493 | } |