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