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