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