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 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 9 | #ifdef MS_WINDOWS |
Guido van Rossum | 644a12b | 1997-04-09 19:24:53 +0000 | [diff] [blame] | 10 | #include <process.h> |
| 11 | #endif |
| 12 | |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 13 | #include <signal.h> |
| 14 | |
Guido van Rossum | 02de897 | 2007-12-19 19:41:06 +0000 | [diff] [blame] | 15 | #include <sys/stat.h> |
Martin v. Löwis | b74d084 | 2008-03-24 13:54:23 +0000 | [diff] [blame] | 16 | #ifdef HAVE_SYS_TIME_H |
Martin v. Löwis | aef18b1 | 2008-03-24 13:31:16 +0000 | [diff] [blame] | 17 | #include <sys/time.h> |
Martin v. Löwis | b74d084 | 2008-03-24 13:54:23 +0000 | [diff] [blame] | 18 | #endif |
Guido van Rossum | 02de897 | 2007-12-19 19:41:06 +0000 | [diff] [blame] | 19 | |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 20 | #ifndef SIG_ERR |
Guido van Rossum | d2cd7ad | 2000-09-16 16:35:28 +0000 | [diff] [blame] | 21 | #define SIG_ERR ((PyOS_sighandler_t)(-1)) |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 22 | #endif |
| 23 | |
Andrew MacIntyre | 7bf6833 | 2002-03-03 02:59:16 +0000 | [diff] [blame] | 24 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 25 | #define NSIG 12 |
| 26 | #include <process.h> |
| 27 | #endif |
| 28 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 29 | #ifndef NSIG |
Marc-André Lemburg | 8bcfb8a | 2000-07-04 14:17:33 +0000 | [diff] [blame] | 30 | # if defined(_NSIG) |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 31 | # define NSIG _NSIG /* For BSD/SysV */ |
Marc-André Lemburg | 8bcfb8a | 2000-07-04 14:17:33 +0000 | [diff] [blame] | 32 | # elif defined(_SIGMAX) |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 33 | # define NSIG (_SIGMAX + 1) /* For QNX */ |
Marc-André Lemburg | 8bcfb8a | 2000-07-04 14:17:33 +0000 | [diff] [blame] | 34 | # elif defined(SIGMAX) |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 35 | # define NSIG (SIGMAX + 1) /* For djgpp */ |
Marc-André Lemburg | 8bcfb8a | 2000-07-04 14:17:33 +0000 | [diff] [blame] | 36 | # else |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 37 | # define NSIG 64 /* Use a reasonable default value */ |
Marc-André Lemburg | 8bcfb8a | 2000-07-04 14:17:33 +0000 | [diff] [blame] | 38 | # endif |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 39 | #endif |
| 40 | |
| 41 | |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 42 | /* |
| 43 | NOTES ON THE INTERACTION BETWEEN SIGNALS AND THREADS |
| 44 | |
| 45 | When threads are supported, we want the following semantics: |
| 46 | |
| 47 | - only the main thread can set a signal handler |
| 48 | - any thread can get a signal handler |
| 49 | - signals are only delivered to the main thread |
| 50 | |
| 51 | I.e. we don't support "synchronous signals" like SIGFPE (catching |
| 52 | this doesn't make much sense in Python anyway) nor do we support |
| 53 | signals as a means of inter-thread communication, since not all |
| 54 | thread implementations support that (at least our thread library |
| 55 | doesn't). |
| 56 | |
| 57 | We still have the problem that in some implementations signals |
| 58 | generated by the keyboard (e.g. SIGINT) are delivered to all |
| 59 | threads (e.g. SGI), while in others (e.g. Solaris) such signals are |
| 60 | delivered to one random thread (an intermediate possibility would |
Guido van Rossum | a3c04b0 | 1995-01-12 11:29:01 +0000 | [diff] [blame] | 61 | 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] | 62 | a working implementation that works in all three cases -- the |
| 63 | handler ignores signals if getpid() isn't the same as in the main |
| 64 | thread. XXX This is a hack. |
| 65 | |
Guido van Rossum | 9e8181b | 2000-09-19 00:46:46 +0000 | [diff] [blame] | 66 | GNU pth is a user-space threading library, and as such, all threads |
| 67 | run within the same process. In this case, if the currently running |
| 68 | 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] | 69 | */ |
| 70 | |
| 71 | #ifdef WITH_THREAD |
Guido van Rossum | 295b8e5 | 1997-06-06 21:16:41 +0000 | [diff] [blame] | 72 | #include <sys/types.h> /* For pid_t */ |
Guido van Rossum | 49b5606 | 1998-10-01 20:42:43 +0000 | [diff] [blame] | 73 | #include "pythread.h" |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 74 | static long main_thread; |
| 75 | static pid_t main_pid; |
| 76 | #endif |
| 77 | |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 78 | static struct { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 79 | int tripped; |
| 80 | PyObject *func; |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 81 | } Handlers[NSIG]; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 82 | |
Guido van Rossum | 02de897 | 2007-12-19 19:41:06 +0000 | [diff] [blame] | 83 | static sig_atomic_t wakeup_fd = -1; |
| 84 | |
Guido van Rossum | 137c49c | 2007-12-10 23:00:12 +0000 | [diff] [blame] | 85 | /* Speed up sigcheck() when none tripped */ |
| 86 | static volatile sig_atomic_t is_tripped = 0; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 87 | |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 88 | static PyObject *DefaultHandler; |
| 89 | static PyObject *IgnoreHandler; |
| 90 | static PyObject *IntHandler; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 91 | |
Martin v. Löwis | f58de1b | 2001-03-06 12:13:56 +0000 | [diff] [blame] | 92 | /* On Solaris 8, gcc will produce a warning that the function |
| 93 | declaration is not a prototype. This is caused by the definition of |
| 94 | SIG_DFL as (void (*)())0; the correct declaration would have been |
| 95 | (void (*)(int))0. */ |
| 96 | |
Guido van Rossum | d2cd7ad | 2000-09-16 16:35:28 +0000 | [diff] [blame] | 97 | static PyOS_sighandler_t old_siginthandler = SIG_DFL; |
Guido van Rossum | 08c1661 | 1997-08-02 03:01:42 +0000 | [diff] [blame] | 98 | |
Martin v. Löwis | aef18b1 | 2008-03-24 13:31:16 +0000 | [diff] [blame] | 99 | #ifdef HAVE_GETITIMER |
| 100 | static PyObject *ItimerError; |
| 101 | |
| 102 | /* auxiliary functions for setitimer/getitimer */ |
| 103 | static void |
| 104 | timeval_from_double(double d, struct timeval *tv) |
| 105 | { |
| 106 | tv->tv_sec = floor(d); |
| 107 | tv->tv_usec = fmod(d, 1.0) * 1000000.0; |
| 108 | } |
| 109 | |
Christian Heimes | 4d8a6f4 | 2008-10-02 19:49:47 +0000 | [diff] [blame] | 110 | Py_LOCAL_INLINE(double) |
Martin v. Löwis | aef18b1 | 2008-03-24 13:31:16 +0000 | [diff] [blame] | 111 | double_from_timeval(struct timeval *tv) |
| 112 | { |
| 113 | return tv->tv_sec + (double)(tv->tv_usec / 1000000.0); |
| 114 | } |
| 115 | |
| 116 | static PyObject * |
| 117 | itimer_retval(struct itimerval *iv) |
| 118 | { |
| 119 | PyObject *r, *v; |
| 120 | |
| 121 | r = PyTuple_New(2); |
| 122 | if (r == NULL) |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 123 | return NULL; |
Martin v. Löwis | aef18b1 | 2008-03-24 13:31:16 +0000 | [diff] [blame] | 124 | |
| 125 | if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_value)))) { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 126 | Py_DECREF(r); |
| 127 | return NULL; |
Martin v. Löwis | aef18b1 | 2008-03-24 13:31:16 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | PyTuple_SET_ITEM(r, 0, v); |
| 131 | |
| 132 | if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_interval)))) { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 133 | Py_DECREF(r); |
| 134 | return NULL; |
Martin v. Löwis | aef18b1 | 2008-03-24 13:31:16 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | PyTuple_SET_ITEM(r, 1, v); |
| 138 | |
| 139 | return r; |
| 140 | } |
| 141 | #endif |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 142 | |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 143 | static PyObject * |
Peter Schneider-Kamp | e89b156 | 2000-07-10 12:04:18 +0000 | [diff] [blame] | 144 | signal_default_int_handler(PyObject *self, PyObject *args) |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 145 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 146 | PyErr_SetNone(PyExc_KeyboardInterrupt); |
| 147 | return NULL; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 150 | PyDoc_STRVAR(default_int_handler_doc, |
Guido van Rossum | 1d8fb2d | 1998-06-28 16:54:49 +0000 | [diff] [blame] | 151 | "default_int_handler(...)\n\ |
| 152 | \n\ |
Michael W. Hudson | 24ec211 | 2004-06-17 15:55:53 +0000 | [diff] [blame] | 153 | The default handler for SIGINT installed by Python.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 154 | It raises KeyboardInterrupt."); |
Guido van Rossum | 1d8fb2d | 1998-06-28 16:54:49 +0000 | [diff] [blame] | 155 | |
Thomas Wouters | 0796b00 | 2000-07-22 23:49:30 +0000 | [diff] [blame] | 156 | |
| 157 | static int |
| 158 | checksignals_witharg(void * unused) |
| 159 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 160 | return PyErr_CheckSignals(); |
Thomas Wouters | 0796b00 | 2000-07-22 23:49:30 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Tim Peters | 4f1b208 | 2000-07-23 21:18:09 +0000 | [diff] [blame] | 163 | static void |
Peter Schneider-Kamp | e89b156 | 2000-07-10 12:04:18 +0000 | [diff] [blame] | 164 | signal_handler(int sig_num) |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 165 | { |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 166 | #ifdef WITH_THREAD |
Guido van Rossum | 9e8181b | 2000-09-19 00:46:46 +0000 | [diff] [blame] | 167 | #ifdef WITH_PTH |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 168 | if (PyThread_get_thread_ident() != main_thread) { |
| 169 | pth_raise(*(pth_t *) main_thread, sig_num); |
| 170 | return; |
| 171 | } |
Guido van Rossum | 9e8181b | 2000-09-19 00:46:46 +0000 | [diff] [blame] | 172 | #endif |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 173 | /* See NOTES section above */ |
| 174 | if (getpid() == main_pid) { |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 175 | #endif |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 176 | Handlers[sig_num].tripped = 1; |
| 177 | /* Set is_tripped after setting .tripped, as it gets |
| 178 | cleared in PyErr_CheckSignals() before .tripped. */ |
| 179 | is_tripped = 1; |
| 180 | Py_AddPendingCall(checksignals_witharg, NULL); |
| 181 | if (wakeup_fd != -1) |
| 182 | write(wakeup_fd, "\0", 1); |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 183 | #ifdef WITH_THREAD |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 184 | } |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 185 | #endif |
Guido van Rossum | 602099a | 1994-09-14 13:32:22 +0000 | [diff] [blame] | 186 | #ifdef SIGCHLD |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 187 | if (sig_num == SIGCHLD) { |
| 188 | /* To avoid infinite recursion, this signal remains |
| 189 | reset until explicit re-instated. |
| 190 | Don't clear the 'func' field as it is our pointer |
| 191 | to the Python handler... */ |
| 192 | return; |
| 193 | } |
Guido van Rossum | 602099a | 1994-09-14 13:32:22 +0000 | [diff] [blame] | 194 | #endif |
Jean-Paul Calderone | 0819b09 | 2010-05-08 21:11:28 +0000 | [diff] [blame] | 195 | #ifndef HAVE_SIGACTION |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 196 | /* If the handler was not set up with sigaction, reinstall it. See |
| 197 | * Python/pythonrun.c for the implementation of PyOS_setsig which |
| 198 | * makes this true. See also issue8354. */ |
| 199 | PyOS_setsig(sig_num, signal_handler); |
Jean-Paul Calderone | 0819b09 | 2010-05-08 21:11:28 +0000 | [diff] [blame] | 200 | #endif |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 201 | } |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 202 | |
Guido van Rossum | 06d511d | 1995-03-10 15:13:48 +0000 | [diff] [blame] | 203 | |
Guido van Rossum | 1171ee6 | 1997-08-22 20:42:00 +0000 | [diff] [blame] | 204 | #ifdef HAVE_ALARM |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 205 | static PyObject * |
Peter Schneider-Kamp | e89b156 | 2000-07-10 12:04:18 +0000 | [diff] [blame] | 206 | signal_alarm(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 207 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 208 | int t; |
| 209 | if (!PyArg_ParseTuple(args, "i:alarm", &t)) |
| 210 | return NULL; |
| 211 | /* alarm() returns the number of seconds remaining */ |
| 212 | return PyInt_FromLong((long)alarm(t)); |
Guido van Rossum | aa0f4c7 | 1994-08-23 13:49:37 +0000 | [diff] [blame] | 213 | } |
Guido van Rossum | 1d8fb2d | 1998-06-28 16:54:49 +0000 | [diff] [blame] | 214 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 215 | PyDoc_STRVAR(alarm_doc, |
Guido van Rossum | 1d8fb2d | 1998-06-28 16:54:49 +0000 | [diff] [blame] | 216 | "alarm(seconds)\n\ |
| 217 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 218 | Arrange for SIGALRM to arrive after the given number of seconds."); |
Guido van Rossum | 06d511d | 1995-03-10 15:13:48 +0000 | [diff] [blame] | 219 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 220 | |
Guido van Rossum | 1171ee6 | 1997-08-22 20:42:00 +0000 | [diff] [blame] | 221 | #ifdef HAVE_PAUSE |
Guido van Rossum | a597dde | 1995-01-10 20:56:29 +0000 | [diff] [blame] | 222 | static PyObject * |
Neal Norwitz | 3a6f978 | 2002-03-25 20:46:46 +0000 | [diff] [blame] | 223 | signal_pause(PyObject *self) |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 224 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 225 | Py_BEGIN_ALLOW_THREADS |
| 226 | (void)pause(); |
| 227 | Py_END_ALLOW_THREADS |
| 228 | /* make sure that any exceptions that got raised are propagated |
| 229 | * back into Python |
| 230 | */ |
| 231 | if (PyErr_CheckSignals()) |
| 232 | return NULL; |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 233 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 234 | Py_INCREF(Py_None); |
| 235 | return Py_None; |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 236 | } |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 237 | PyDoc_STRVAR(pause_doc, |
Barry Warsaw | 1ee36ff | 1998-07-21 22:41:18 +0000 | [diff] [blame] | 238 | "pause()\n\ |
| 239 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 240 | Wait until a signal arrives."); |
Guido van Rossum | 1d8fb2d | 1998-06-28 16:54:49 +0000 | [diff] [blame] | 241 | |
Guido van Rossum | 06d511d | 1995-03-10 15:13:48 +0000 | [diff] [blame] | 242 | #endif |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 243 | |
Guido van Rossum | d2cd7ad | 2000-09-16 16:35:28 +0000 | [diff] [blame] | 244 | |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 245 | static PyObject * |
Peter Schneider-Kamp | e89b156 | 2000-07-10 12:04:18 +0000 | [diff] [blame] | 246 | signal_signal(PyObject *self, PyObject *args) |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 247 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 248 | PyObject *obj; |
| 249 | int sig_num; |
| 250 | PyObject *old_handler; |
| 251 | void (*func)(int); |
| 252 | if (!PyArg_ParseTuple(args, "iO:signal", &sig_num, &obj)) |
| 253 | return NULL; |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 254 | #ifdef WITH_THREAD |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 255 | if (PyThread_get_thread_ident() != main_thread) { |
| 256 | PyErr_SetString(PyExc_ValueError, |
| 257 | "signal only works in main thread"); |
| 258 | return NULL; |
| 259 | } |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 260 | #endif |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 261 | if (sig_num < 1 || sig_num >= NSIG) { |
| 262 | PyErr_SetString(PyExc_ValueError, |
| 263 | "signal number out of range"); |
| 264 | return NULL; |
| 265 | } |
| 266 | if (obj == IgnoreHandler) |
| 267 | func = SIG_IGN; |
| 268 | else if (obj == DefaultHandler) |
| 269 | func = SIG_DFL; |
| 270 | else if (!PyCallable_Check(obj)) { |
| 271 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 272 | "signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object"); |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 273 | return NULL; |
| 274 | } |
| 275 | else |
| 276 | func = signal_handler; |
| 277 | if (PyOS_setsig(sig_num, func) == SIG_ERR) { |
| 278 | PyErr_SetFromErrno(PyExc_RuntimeError); |
| 279 | return NULL; |
| 280 | } |
| 281 | old_handler = Handlers[sig_num].func; |
| 282 | Handlers[sig_num].tripped = 0; |
| 283 | Py_INCREF(obj); |
| 284 | Handlers[sig_num].func = obj; |
| 285 | return old_handler; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 286 | } |
| 287 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 288 | PyDoc_STRVAR(signal_doc, |
Guido van Rossum | 1d8fb2d | 1998-06-28 16:54:49 +0000 | [diff] [blame] | 289 | "signal(sig, action) -> action\n\ |
| 290 | \n\ |
| 291 | Set the action for the given signal. The action can be SIG_DFL,\n\ |
| 292 | SIG_IGN, or a callable Python object. The previous action is\n\ |
| 293 | returned. See getsignal() for possible return values.\n\ |
| 294 | \n\ |
| 295 | *** IMPORTANT NOTICE ***\n\ |
| 296 | A signal handler function is called with two arguments:\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 297 | the first is the signal number, the second is the interrupted stack frame."); |
Guido van Rossum | 1d8fb2d | 1998-06-28 16:54:49 +0000 | [diff] [blame] | 298 | |
Guido van Rossum | d2cd7ad | 2000-09-16 16:35:28 +0000 | [diff] [blame] | 299 | |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 300 | static PyObject * |
Peter Schneider-Kamp | e89b156 | 2000-07-10 12:04:18 +0000 | [diff] [blame] | 301 | signal_getsignal(PyObject *self, PyObject *args) |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 302 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 303 | int sig_num; |
| 304 | PyObject *old_handler; |
| 305 | if (!PyArg_ParseTuple(args, "i:getsignal", &sig_num)) |
| 306 | return NULL; |
| 307 | if (sig_num < 1 || sig_num >= NSIG) { |
| 308 | PyErr_SetString(PyExc_ValueError, |
| 309 | "signal number out of range"); |
| 310 | return NULL; |
| 311 | } |
| 312 | old_handler = Handlers[sig_num].func; |
| 313 | Py_INCREF(old_handler); |
| 314 | return old_handler; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 315 | } |
| 316 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 317 | PyDoc_STRVAR(getsignal_doc, |
Guido van Rossum | 1d8fb2d | 1998-06-28 16:54:49 +0000 | [diff] [blame] | 318 | "getsignal(sig) -> action\n\ |
| 319 | \n\ |
| 320 | Return the current action for the given signal. The return value can be:\n\ |
| 321 | SIG_IGN -- if the signal is being ignored\n\ |
| 322 | SIG_DFL -- if the default action for the signal is in effect\n\ |
| 323 | None -- if an unknown handler is in effect\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 324 | anything else -- the callable Python object used as a handler"); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 325 | |
Facundo Batista | 7e251e8 | 2008-02-23 15:07:35 +0000 | [diff] [blame] | 326 | #ifdef HAVE_SIGINTERRUPT |
| 327 | PyDoc_STRVAR(siginterrupt_doc, |
| 328 | "siginterrupt(sig, flag) -> None\n\ |
| 329 | change system call restart behaviour: if flag is False, system calls\n\ |
| 330 | will be restarted when interrupted by signal sig, else system calls\n\ |
| 331 | will be interrupted."); |
| 332 | |
| 333 | static PyObject * |
| 334 | signal_siginterrupt(PyObject *self, PyObject *args) |
| 335 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 336 | int sig_num; |
| 337 | int flag; |
Facundo Batista | 7e251e8 | 2008-02-23 15:07:35 +0000 | [diff] [blame] | 338 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 339 | if (!PyArg_ParseTuple(args, "ii:siginterrupt", &sig_num, &flag)) |
| 340 | return NULL; |
| 341 | if (sig_num < 1 || sig_num >= NSIG) { |
| 342 | PyErr_SetString(PyExc_ValueError, |
| 343 | "signal number out of range"); |
| 344 | return NULL; |
| 345 | } |
| 346 | if (siginterrupt(sig_num, flag)<0) { |
| 347 | PyErr_SetFromErrno(PyExc_RuntimeError); |
| 348 | return NULL; |
| 349 | } |
Facundo Batista | 7e251e8 | 2008-02-23 15:07:35 +0000 | [diff] [blame] | 350 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 351 | Py_INCREF(Py_None); |
| 352 | return Py_None; |
Facundo Batista | 7e251e8 | 2008-02-23 15:07:35 +0000 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | #endif |
Guido van Rossum | d2cd7ad | 2000-09-16 16:35:28 +0000 | [diff] [blame] | 356 | |
Guido van Rossum | 02de897 | 2007-12-19 19:41:06 +0000 | [diff] [blame] | 357 | static PyObject * |
| 358 | signal_set_wakeup_fd(PyObject *self, PyObject *args) |
| 359 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 360 | struct stat buf; |
| 361 | int fd, old_fd; |
| 362 | if (!PyArg_ParseTuple(args, "i:set_wakeup_fd", &fd)) |
| 363 | return NULL; |
Guido van Rossum | 02de897 | 2007-12-19 19:41:06 +0000 | [diff] [blame] | 364 | #ifdef WITH_THREAD |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 365 | if (PyThread_get_thread_ident() != main_thread) { |
| 366 | PyErr_SetString(PyExc_ValueError, |
| 367 | "set_wakeup_fd only works in main thread"); |
| 368 | return NULL; |
| 369 | } |
Guido van Rossum | 02de897 | 2007-12-19 19:41:06 +0000 | [diff] [blame] | 370 | #endif |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 371 | if (fd != -1 && fstat(fd, &buf) != 0) { |
| 372 | PyErr_SetString(PyExc_ValueError, "invalid fd"); |
| 373 | return NULL; |
| 374 | } |
| 375 | old_fd = wakeup_fd; |
| 376 | wakeup_fd = fd; |
| 377 | return PyLong_FromLong(old_fd); |
Guido van Rossum | 02de897 | 2007-12-19 19:41:06 +0000 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | PyDoc_STRVAR(set_wakeup_fd_doc, |
| 381 | "set_wakeup_fd(fd) -> fd\n\ |
| 382 | \n\ |
| 383 | Sets the fd to be written to (with '\\0') when a signal\n\ |
| 384 | comes in. A library can use this to wakeup select or poll.\n\ |
| 385 | The previous fd is returned.\n\ |
| 386 | \n\ |
| 387 | The fd must be non-blocking."); |
| 388 | |
| 389 | /* C API for the same, without all the error checking */ |
| 390 | int |
| 391 | PySignal_SetWakeupFd(int fd) |
| 392 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 393 | int old_fd = wakeup_fd; |
| 394 | if (fd < 0) |
| 395 | fd = -1; |
| 396 | wakeup_fd = fd; |
| 397 | return old_fd; |
Guido van Rossum | 02de897 | 2007-12-19 19:41:06 +0000 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | |
Martin v. Löwis | aef18b1 | 2008-03-24 13:31:16 +0000 | [diff] [blame] | 401 | #ifdef HAVE_SETITIMER |
| 402 | static PyObject * |
| 403 | signal_setitimer(PyObject *self, PyObject *args) |
| 404 | { |
| 405 | double first; |
| 406 | double interval = 0; |
| 407 | int which; |
| 408 | struct itimerval new, old; |
| 409 | |
| 410 | if(!PyArg_ParseTuple(args, "id|d:setitimer", &which, &first, &interval)) |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 411 | return NULL; |
Martin v. Löwis | aef18b1 | 2008-03-24 13:31:16 +0000 | [diff] [blame] | 412 | |
| 413 | timeval_from_double(first, &new.it_value); |
| 414 | timeval_from_double(interval, &new.it_interval); |
| 415 | /* Let OS check "which" value */ |
| 416 | if (setitimer(which, &new, &old) != 0) { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 417 | PyErr_SetFromErrno(ItimerError); |
| 418 | return NULL; |
Martin v. Löwis | aef18b1 | 2008-03-24 13:31:16 +0000 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | return itimer_retval(&old); |
| 422 | } |
| 423 | |
| 424 | PyDoc_STRVAR(setitimer_doc, |
| 425 | "setitimer(which, seconds[, interval])\n\ |
| 426 | \n\ |
| 427 | Sets given itimer (one of ITIMER_REAL, ITIMER_VIRTUAL\n\ |
| 428 | or ITIMER_PROF) to fire after value seconds and after\n\ |
| 429 | that every interval seconds.\n\ |
| 430 | The itimer can be cleared by setting seconds to zero.\n\ |
| 431 | \n\ |
| 432 | Returns old values as a tuple: (delay, interval)."); |
| 433 | #endif |
| 434 | |
| 435 | |
| 436 | #ifdef HAVE_GETITIMER |
| 437 | static PyObject * |
| 438 | signal_getitimer(PyObject *self, PyObject *args) |
| 439 | { |
| 440 | int which; |
| 441 | struct itimerval old; |
| 442 | |
| 443 | if (!PyArg_ParseTuple(args, "i:getitimer", &which)) |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 444 | return NULL; |
Martin v. Löwis | aef18b1 | 2008-03-24 13:31:16 +0000 | [diff] [blame] | 445 | |
| 446 | if (getitimer(which, &old) != 0) { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 447 | PyErr_SetFromErrno(ItimerError); |
| 448 | return NULL; |
Martin v. Löwis | aef18b1 | 2008-03-24 13:31:16 +0000 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | return itimer_retval(&old); |
| 452 | } |
| 453 | |
| 454 | PyDoc_STRVAR(getitimer_doc, |
| 455 | "getitimer(which)\n\ |
| 456 | \n\ |
| 457 | Returns current value of given itimer."); |
| 458 | #endif |
| 459 | |
| 460 | |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 461 | /* List of functions defined in the module */ |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 462 | static PyMethodDef signal_methods[] = { |
Guido van Rossum | 1171ee6 | 1997-08-22 20:42:00 +0000 | [diff] [blame] | 463 | #ifdef HAVE_ALARM |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 464 | {"alarm", signal_alarm, METH_VARARGS, alarm_doc}, |
Guido van Rossum | 06d511d | 1995-03-10 15:13:48 +0000 | [diff] [blame] | 465 | #endif |
Martin v. Löwis | aef18b1 | 2008-03-24 13:31:16 +0000 | [diff] [blame] | 466 | #ifdef HAVE_SETITIMER |
| 467 | {"setitimer", signal_setitimer, METH_VARARGS, setitimer_doc}, |
| 468 | #endif |
| 469 | #ifdef HAVE_GETITIMER |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 470 | {"getitimer", signal_getitimer, METH_VARARGS, getitimer_doc}, |
Martin v. Löwis | aef18b1 | 2008-03-24 13:31:16 +0000 | [diff] [blame] | 471 | #endif |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 472 | {"signal", signal_signal, METH_VARARGS, signal_doc}, |
| 473 | {"getsignal", signal_getsignal, METH_VARARGS, getsignal_doc}, |
| 474 | {"set_wakeup_fd", signal_set_wakeup_fd, METH_VARARGS, set_wakeup_fd_doc}, |
Facundo Batista | 7e251e8 | 2008-02-23 15:07:35 +0000 | [diff] [blame] | 475 | #ifdef HAVE_SIGINTERRUPT |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 476 | {"siginterrupt", signal_siginterrupt, METH_VARARGS, siginterrupt_doc}, |
Facundo Batista | 7e251e8 | 2008-02-23 15:07:35 +0000 | [diff] [blame] | 477 | #endif |
Guido van Rossum | 1171ee6 | 1997-08-22 20:42:00 +0000 | [diff] [blame] | 478 | #ifdef HAVE_PAUSE |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 479 | {"pause", (PyCFunction)signal_pause, |
| 480 | METH_NOARGS,pause_doc}, |
Guido van Rossum | 06d511d | 1995-03-10 15:13:48 +0000 | [diff] [blame] | 481 | #endif |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 482 | {"default_int_handler", signal_default_int_handler, |
| 483 | METH_VARARGS, default_int_handler_doc}, |
| 484 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 485 | }; |
| 486 | |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 487 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 488 | PyDoc_STRVAR(module_doc, |
Guido van Rossum | 1d8fb2d | 1998-06-28 16:54:49 +0000 | [diff] [blame] | 489 | "This module provides mechanisms to use signal handlers in Python.\n\ |
| 490 | \n\ |
| 491 | Functions:\n\ |
| 492 | \n\ |
| 493 | alarm() -- cause SIGALRM after a specified time [Unix only]\n\ |
Martin v. Löwis | aef18b1 | 2008-03-24 13:31:16 +0000 | [diff] [blame] | 494 | setitimer() -- cause a signal (described below) after a specified\n\ |
| 495 | float time and the timer may restart then [Unix only]\n\ |
| 496 | getitimer() -- get current value of timer [Unix only]\n\ |
Guido van Rossum | 1d8fb2d | 1998-06-28 16:54:49 +0000 | [diff] [blame] | 497 | signal() -- set the action for a given signal\n\ |
| 498 | getsignal() -- get the signal action for a given signal\n\ |
| 499 | pause() -- wait until a signal arrives [Unix only]\n\ |
| 500 | default_int_handler() -- default SIGINT handler\n\ |
| 501 | \n\ |
Martin v. Löwis | aef18b1 | 2008-03-24 13:31:16 +0000 | [diff] [blame] | 502 | signal constants:\n\ |
Guido van Rossum | 1d8fb2d | 1998-06-28 16:54:49 +0000 | [diff] [blame] | 503 | SIG_DFL -- used to refer to the system default handler\n\ |
| 504 | SIG_IGN -- used to ignore the signal\n\ |
| 505 | NSIG -- number of defined signals\n\ |
Guido van Rossum | 1d8fb2d | 1998-06-28 16:54:49 +0000 | [diff] [blame] | 506 | SIGINT, SIGTERM, etc. -- signal numbers\n\ |
| 507 | \n\ |
Martin v. Löwis | aef18b1 | 2008-03-24 13:31:16 +0000 | [diff] [blame] | 508 | itimer constants:\n\ |
| 509 | ITIMER_REAL -- decrements in real time, and delivers SIGALRM upon\n\ |
| 510 | expiration\n\ |
| 511 | ITIMER_VIRTUAL -- decrements only when the process is executing,\n\ |
| 512 | and delivers SIGVTALRM upon expiration\n\ |
| 513 | ITIMER_PROF -- decrements both when the process is executing and\n\ |
| 514 | when the system is executing on behalf of the process.\n\ |
| 515 | Coupled with ITIMER_VIRTUAL, this timer is usually\n\ |
| 516 | used to profile the time spent by the application\n\ |
| 517 | in user and kernel space. SIGPROF is delivered upon\n\ |
| 518 | expiration.\n\ |
| 519 | \n\n\ |
Guido van Rossum | 1d8fb2d | 1998-06-28 16:54:49 +0000 | [diff] [blame] | 520 | *** IMPORTANT NOTICE ***\n\ |
| 521 | A signal handler function is called with two arguments:\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 522 | the first is the signal number, the second is the interrupted stack frame."); |
Guido van Rossum | 1d8fb2d | 1998-06-28 16:54:49 +0000 | [diff] [blame] | 523 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 524 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 525 | initsignal(void) |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 526 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 527 | PyObject *m, *d, *x; |
| 528 | int i; |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 529 | |
| 530 | #ifdef WITH_THREAD |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 531 | main_thread = PyThread_get_thread_ident(); |
| 532 | main_pid = getpid(); |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 533 | #endif |
| 534 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 535 | /* Create the module and add the functions */ |
| 536 | m = Py_InitModule3("signal", signal_methods, module_doc); |
| 537 | if (m == NULL) |
| 538 | return; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 539 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 540 | /* Add some symbolic constants to the module */ |
| 541 | d = PyModule_GetDict(m); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 542 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 543 | x = DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL); |
| 544 | if (!x || PyDict_SetItemString(d, "SIG_DFL", x) < 0) |
| 545 | goto finally; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 546 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 547 | x = IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN); |
| 548 | if (!x || PyDict_SetItemString(d, "SIG_IGN", x) < 0) |
| 549 | goto finally; |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 550 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 551 | x = PyInt_FromLong((long)NSIG); |
| 552 | if (!x || PyDict_SetItemString(d, "NSIG", x) < 0) |
| 553 | goto finally; |
| 554 | Py_DECREF(x); |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 555 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 556 | x = IntHandler = PyDict_GetItemString(d, "default_int_handler"); |
| 557 | if (!x) |
| 558 | goto finally; |
| 559 | Py_INCREF(IntHandler); |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 560 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 561 | Handlers[0].tripped = 0; |
| 562 | for (i = 1; i < NSIG; i++) { |
| 563 | void (*t)(int); |
| 564 | t = PyOS_getsig(i); |
| 565 | Handlers[i].tripped = 0; |
| 566 | if (t == SIG_DFL) |
| 567 | Handlers[i].func = DefaultHandler; |
| 568 | else if (t == SIG_IGN) |
| 569 | Handlers[i].func = IgnoreHandler; |
| 570 | else |
| 571 | Handlers[i].func = Py_None; /* None of our business */ |
| 572 | Py_INCREF(Handlers[i].func); |
| 573 | } |
| 574 | if (Handlers[SIGINT].func == DefaultHandler) { |
| 575 | /* Install default int handler */ |
| 576 | Py_INCREF(IntHandler); |
| 577 | Py_DECREF(Handlers[SIGINT].func); |
| 578 | Handlers[SIGINT].func = IntHandler; |
| 579 | old_siginthandler = PyOS_setsig(SIGINT, signal_handler); |
| 580 | } |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 581 | |
| 582 | #ifdef SIGHUP |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 583 | x = PyInt_FromLong(SIGHUP); |
| 584 | PyDict_SetItemString(d, "SIGHUP", x); |
| 585 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 586 | #endif |
| 587 | #ifdef SIGINT |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 588 | x = PyInt_FromLong(SIGINT); |
| 589 | PyDict_SetItemString(d, "SIGINT", x); |
| 590 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 591 | #endif |
Tim Peters | 1ce3cf7 | 2001-10-01 17:58:40 +0000 | [diff] [blame] | 592 | #ifdef SIGBREAK |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 593 | x = PyInt_FromLong(SIGBREAK); |
| 594 | PyDict_SetItemString(d, "SIGBREAK", x); |
| 595 | Py_XDECREF(x); |
Tim Peters | 1ce3cf7 | 2001-10-01 17:58:40 +0000 | [diff] [blame] | 596 | #endif |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 597 | #ifdef SIGQUIT |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 598 | x = PyInt_FromLong(SIGQUIT); |
| 599 | PyDict_SetItemString(d, "SIGQUIT", x); |
| 600 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 601 | #endif |
| 602 | #ifdef SIGILL |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 603 | x = PyInt_FromLong(SIGILL); |
| 604 | PyDict_SetItemString(d, "SIGILL", x); |
| 605 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 606 | #endif |
| 607 | #ifdef SIGTRAP |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 608 | x = PyInt_FromLong(SIGTRAP); |
| 609 | PyDict_SetItemString(d, "SIGTRAP", x); |
| 610 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 611 | #endif |
| 612 | #ifdef SIGIOT |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 613 | x = PyInt_FromLong(SIGIOT); |
| 614 | PyDict_SetItemString(d, "SIGIOT", x); |
| 615 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 616 | #endif |
| 617 | #ifdef SIGABRT |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 618 | x = PyInt_FromLong(SIGABRT); |
| 619 | PyDict_SetItemString(d, "SIGABRT", x); |
| 620 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 621 | #endif |
| 622 | #ifdef SIGEMT |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 623 | x = PyInt_FromLong(SIGEMT); |
| 624 | PyDict_SetItemString(d, "SIGEMT", x); |
| 625 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 626 | #endif |
| 627 | #ifdef SIGFPE |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 628 | x = PyInt_FromLong(SIGFPE); |
| 629 | PyDict_SetItemString(d, "SIGFPE", x); |
| 630 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 631 | #endif |
| 632 | #ifdef SIGKILL |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 633 | x = PyInt_FromLong(SIGKILL); |
| 634 | PyDict_SetItemString(d, "SIGKILL", x); |
| 635 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 636 | #endif |
| 637 | #ifdef SIGBUS |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 638 | x = PyInt_FromLong(SIGBUS); |
| 639 | PyDict_SetItemString(d, "SIGBUS", x); |
| 640 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 641 | #endif |
| 642 | #ifdef SIGSEGV |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 643 | x = PyInt_FromLong(SIGSEGV); |
| 644 | PyDict_SetItemString(d, "SIGSEGV", x); |
| 645 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 646 | #endif |
| 647 | #ifdef SIGSYS |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 648 | x = PyInt_FromLong(SIGSYS); |
| 649 | PyDict_SetItemString(d, "SIGSYS", x); |
| 650 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 651 | #endif |
| 652 | #ifdef SIGPIPE |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 653 | x = PyInt_FromLong(SIGPIPE); |
| 654 | PyDict_SetItemString(d, "SIGPIPE", x); |
| 655 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 656 | #endif |
| 657 | #ifdef SIGALRM |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 658 | x = PyInt_FromLong(SIGALRM); |
| 659 | PyDict_SetItemString(d, "SIGALRM", x); |
| 660 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 661 | #endif |
| 662 | #ifdef SIGTERM |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 663 | x = PyInt_FromLong(SIGTERM); |
| 664 | PyDict_SetItemString(d, "SIGTERM", x); |
| 665 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 666 | #endif |
| 667 | #ifdef SIGUSR1 |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 668 | x = PyInt_FromLong(SIGUSR1); |
| 669 | PyDict_SetItemString(d, "SIGUSR1", x); |
| 670 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 671 | #endif |
| 672 | #ifdef SIGUSR2 |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 673 | x = PyInt_FromLong(SIGUSR2); |
| 674 | PyDict_SetItemString(d, "SIGUSR2", x); |
| 675 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 676 | #endif |
| 677 | #ifdef SIGCLD |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 678 | x = PyInt_FromLong(SIGCLD); |
| 679 | PyDict_SetItemString(d, "SIGCLD", x); |
| 680 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 681 | #endif |
| 682 | #ifdef SIGCHLD |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 683 | x = PyInt_FromLong(SIGCHLD); |
| 684 | PyDict_SetItemString(d, "SIGCHLD", x); |
| 685 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 686 | #endif |
| 687 | #ifdef SIGPWR |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 688 | x = PyInt_FromLong(SIGPWR); |
| 689 | PyDict_SetItemString(d, "SIGPWR", x); |
| 690 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 691 | #endif |
| 692 | #ifdef SIGIO |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 693 | x = PyInt_FromLong(SIGIO); |
| 694 | PyDict_SetItemString(d, "SIGIO", x); |
| 695 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 696 | #endif |
| 697 | #ifdef SIGURG |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 698 | x = PyInt_FromLong(SIGURG); |
| 699 | PyDict_SetItemString(d, "SIGURG", x); |
| 700 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 701 | #endif |
| 702 | #ifdef SIGWINCH |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 703 | x = PyInt_FromLong(SIGWINCH); |
| 704 | PyDict_SetItemString(d, "SIGWINCH", x); |
| 705 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 706 | #endif |
| 707 | #ifdef SIGPOLL |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 708 | x = PyInt_FromLong(SIGPOLL); |
| 709 | PyDict_SetItemString(d, "SIGPOLL", x); |
| 710 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 711 | #endif |
| 712 | #ifdef SIGSTOP |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 713 | x = PyInt_FromLong(SIGSTOP); |
| 714 | PyDict_SetItemString(d, "SIGSTOP", x); |
| 715 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 716 | #endif |
| 717 | #ifdef SIGTSTP |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 718 | x = PyInt_FromLong(SIGTSTP); |
| 719 | PyDict_SetItemString(d, "SIGTSTP", x); |
| 720 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 721 | #endif |
| 722 | #ifdef SIGCONT |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 723 | x = PyInt_FromLong(SIGCONT); |
| 724 | PyDict_SetItemString(d, "SIGCONT", x); |
| 725 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 726 | #endif |
| 727 | #ifdef SIGTTIN |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 728 | x = PyInt_FromLong(SIGTTIN); |
| 729 | PyDict_SetItemString(d, "SIGTTIN", x); |
| 730 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 731 | #endif |
| 732 | #ifdef SIGTTOU |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 733 | x = PyInt_FromLong(SIGTTOU); |
| 734 | PyDict_SetItemString(d, "SIGTTOU", x); |
| 735 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 736 | #endif |
| 737 | #ifdef SIGVTALRM |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 738 | x = PyInt_FromLong(SIGVTALRM); |
| 739 | PyDict_SetItemString(d, "SIGVTALRM", x); |
| 740 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 741 | #endif |
| 742 | #ifdef SIGPROF |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 743 | x = PyInt_FromLong(SIGPROF); |
| 744 | PyDict_SetItemString(d, "SIGPROF", x); |
| 745 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 746 | #endif |
Barry Warsaw | 14ed5fb | 1996-12-16 20:24:22 +0000 | [diff] [blame] | 747 | #ifdef SIGXCPU |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 748 | x = PyInt_FromLong(SIGXCPU); |
| 749 | PyDict_SetItemString(d, "SIGXCPU", x); |
| 750 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 751 | #endif |
Barry Warsaw | 14ed5fb | 1996-12-16 20:24:22 +0000 | [diff] [blame] | 752 | #ifdef SIGXFSZ |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 753 | x = PyInt_FromLong(SIGXFSZ); |
| 754 | PyDict_SetItemString(d, "SIGXFSZ", x); |
| 755 | Py_XDECREF(x); |
Barry Warsaw | 14ed5fb | 1996-12-16 20:24:22 +0000 | [diff] [blame] | 756 | #endif |
Anthony Baxter | f37f37d | 2003-07-31 10:35:29 +0000 | [diff] [blame] | 757 | #ifdef SIGRTMIN |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 758 | x = PyInt_FromLong(SIGRTMIN); |
| 759 | PyDict_SetItemString(d, "SIGRTMIN", x); |
| 760 | Py_XDECREF(x); |
Anthony Baxter | f37f37d | 2003-07-31 10:35:29 +0000 | [diff] [blame] | 761 | #endif |
| 762 | #ifdef SIGRTMAX |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 763 | x = PyInt_FromLong(SIGRTMAX); |
| 764 | PyDict_SetItemString(d, "SIGRTMAX", x); |
| 765 | Py_XDECREF(x); |
Anthony Baxter | f37f37d | 2003-07-31 10:35:29 +0000 | [diff] [blame] | 766 | #endif |
Martin v. Löwis | 175af25 | 2002-01-12 11:43:25 +0000 | [diff] [blame] | 767 | #ifdef SIGINFO |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 768 | x = PyInt_FromLong(SIGINFO); |
| 769 | PyDict_SetItemString(d, "SIGINFO", x); |
| 770 | Py_XDECREF(x); |
Martin v. Löwis | 175af25 | 2002-01-12 11:43:25 +0000 | [diff] [blame] | 771 | #endif |
Martin v. Löwis | aef18b1 | 2008-03-24 13:31:16 +0000 | [diff] [blame] | 772 | |
| 773 | #ifdef ITIMER_REAL |
| 774 | x = PyLong_FromLong(ITIMER_REAL); |
| 775 | PyDict_SetItemString(d, "ITIMER_REAL", x); |
| 776 | Py_DECREF(x); |
| 777 | #endif |
| 778 | #ifdef ITIMER_VIRTUAL |
| 779 | x = PyLong_FromLong(ITIMER_VIRTUAL); |
| 780 | PyDict_SetItemString(d, "ITIMER_VIRTUAL", x); |
| 781 | Py_DECREF(x); |
| 782 | #endif |
| 783 | #ifdef ITIMER_PROF |
| 784 | x = PyLong_FromLong(ITIMER_PROF); |
| 785 | PyDict_SetItemString(d, "ITIMER_PROF", x); |
| 786 | Py_DECREF(x); |
| 787 | #endif |
| 788 | |
| 789 | #if defined (HAVE_SETITIMER) || defined (HAVE_GETITIMER) |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 790 | ItimerError = PyErr_NewException("signal.ItimerError", |
| 791 | PyExc_IOError, NULL); |
Neal Norwitz | 18aa388 | 2008-08-24 05:04:52 +0000 | [diff] [blame] | 792 | if (ItimerError != NULL) |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 793 | PyDict_SetItemString(d, "ItimerError", ItimerError); |
Martin v. Löwis | aef18b1 | 2008-03-24 13:31:16 +0000 | [diff] [blame] | 794 | #endif |
| 795 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 796 | if (!PyErr_Occurred()) |
Guido van Rossum | 08c1661 | 1997-08-02 03:01:42 +0000 | [diff] [blame] | 797 | return; |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 798 | |
| 799 | /* Check for errors */ |
| 800 | finally: |
| 801 | return; |
Guido van Rossum | 08c1661 | 1997-08-02 03:01:42 +0000 | [diff] [blame] | 802 | } |
| 803 | |
| 804 | static void |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 805 | finisignal(void) |
Guido van Rossum | 08c1661 | 1997-08-02 03:01:42 +0000 | [diff] [blame] | 806 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 807 | int i; |
| 808 | PyObject *func; |
Guido van Rossum | 08c1661 | 1997-08-02 03:01:42 +0000 | [diff] [blame] | 809 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 810 | PyOS_setsig(SIGINT, old_siginthandler); |
| 811 | old_siginthandler = SIG_DFL; |
Guido van Rossum | 08c1661 | 1997-08-02 03:01:42 +0000 | [diff] [blame] | 812 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 813 | for (i = 1; i < NSIG; i++) { |
| 814 | func = Handlers[i].func; |
| 815 | Handlers[i].tripped = 0; |
| 816 | Handlers[i].func = NULL; |
| 817 | if (i != SIGINT && func != NULL && func != Py_None && |
| 818 | func != DefaultHandler && func != IgnoreHandler) |
| 819 | PyOS_setsig(i, SIG_DFL); |
| 820 | Py_XDECREF(func); |
| 821 | } |
Guido van Rossum | 08c1661 | 1997-08-02 03:01:42 +0000 | [diff] [blame] | 822 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 823 | Py_XDECREF(IntHandler); |
| 824 | IntHandler = NULL; |
| 825 | Py_XDECREF(DefaultHandler); |
| 826 | DefaultHandler = NULL; |
| 827 | Py_XDECREF(IgnoreHandler); |
| 828 | IgnoreHandler = NULL; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 829 | } |
| 830 | |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 831 | |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 832 | /* Declared in pyerrors.h */ |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 833 | int |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 834 | PyErr_CheckSignals(void) |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 835 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 836 | int i; |
| 837 | PyObject *f; |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 838 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 839 | if (!is_tripped) |
| 840 | return 0; |
Guido van Rossum | 137c49c | 2007-12-10 23:00:12 +0000 | [diff] [blame] | 841 | |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 842 | #ifdef WITH_THREAD |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 843 | if (PyThread_get_thread_ident() != main_thread) |
| 844 | return 0; |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 845 | #endif |
Guido van Rossum | 137c49c | 2007-12-10 23:00:12 +0000 | [diff] [blame] | 846 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 847 | /* |
| 848 | * The is_stripped variable is meant to speed up the calls to |
| 849 | * PyErr_CheckSignals (both directly or via pending calls) when no |
| 850 | * signal has arrived. This variable is set to 1 when a signal arrives |
| 851 | * and it is set to 0 here, when we know some signals arrived. This way |
| 852 | * we can run the registered handlers with no signals blocked. |
| 853 | * |
| 854 | * NOTE: with this approach we can have a situation where is_tripped is |
| 855 | * 1 but we have no more signals to handle (Handlers[i].tripped |
| 856 | * is 0 for every signal i). This won't do us any harm (except |
| 857 | * we're gonna spent some cycles for nothing). This happens when |
| 858 | * we receive a signal i after we zero is_tripped and before we |
| 859 | * check Handlers[i].tripped. |
| 860 | */ |
| 861 | is_tripped = 0; |
Guido van Rossum | 137c49c | 2007-12-10 23:00:12 +0000 | [diff] [blame] | 862 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 863 | if (!(f = (PyObject *)PyEval_GetFrame())) |
| 864 | f = Py_None; |
Guido van Rossum | 137c49c | 2007-12-10 23:00:12 +0000 | [diff] [blame] | 865 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 866 | for (i = 1; i < NSIG; i++) { |
| 867 | if (Handlers[i].tripped) { |
| 868 | PyObject *result = NULL; |
| 869 | PyObject *arglist = Py_BuildValue("(iO)", i, f); |
| 870 | Handlers[i].tripped = 0; |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 871 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 872 | if (arglist) { |
| 873 | result = PyEval_CallObject(Handlers[i].func, |
| 874 | arglist); |
| 875 | Py_DECREF(arglist); |
| 876 | } |
| 877 | if (!result) |
| 878 | return -1; |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 879 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 880 | Py_DECREF(result); |
| 881 | } |
| 882 | } |
Guido van Rossum | 137c49c | 2007-12-10 23:00:12 +0000 | [diff] [blame] | 883 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 884 | return 0; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 885 | } |
| 886 | |
Guido van Rossum | d2cd7ad | 2000-09-16 16:35:28 +0000 | [diff] [blame] | 887 | |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 888 | /* Replacements for intrcheck.c functionality |
| 889 | * Declared in pyerrors.h |
| 890 | */ |
| 891 | void |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 892 | PyErr_SetInterrupt(void) |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 893 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 894 | is_tripped = 1; |
| 895 | Handlers[SIGINT].tripped = 1; |
| 896 | Py_AddPendingCall((int (*)(void *))PyErr_CheckSignals, NULL); |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 897 | } |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 898 | |
| 899 | void |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 900 | PyOS_InitInterrupts(void) |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 901 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 902 | initsignal(); |
| 903 | _PyImport_FixupExtension("signal", "signal"); |
Guido van Rossum | 08c1661 | 1997-08-02 03:01:42 +0000 | [diff] [blame] | 904 | } |
| 905 | |
| 906 | void |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 907 | PyOS_FiniInterrupts(void) |
Guido van Rossum | 08c1661 | 1997-08-02 03:01:42 +0000 | [diff] [blame] | 908 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 909 | finisignal(); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 910 | } |
| 911 | |
| 912 | int |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 913 | PyOS_InterruptOccurred(void) |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 914 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 915 | if (Handlers[SIGINT].tripped) { |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 916 | #ifdef WITH_THREAD |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 917 | if (PyThread_get_thread_ident() != main_thread) |
| 918 | return 0; |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 919 | #endif |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 920 | Handlers[SIGINT].tripped = 0; |
| 921 | return 1; |
| 922 | } |
| 923 | return 0; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 924 | } |
Guido van Rossum | 359bcaa | 1997-11-14 22:24:28 +0000 | [diff] [blame] | 925 | |
| 926 | void |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 927 | PyOS_AfterFork(void) |
Guido van Rossum | 359bcaa | 1997-11-14 22:24:28 +0000 | [diff] [blame] | 928 | { |
| 929 | #ifdef WITH_THREAD |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 930 | PyEval_ReInitThreads(); |
| 931 | main_thread = PyThread_get_thread_ident(); |
| 932 | main_pid = getpid(); |
| 933 | _PyImport_ReInitLock(); |
| 934 | PyThread_ReInitTLS(); |
Guido van Rossum | 359bcaa | 1997-11-14 22:24:28 +0000 | [diff] [blame] | 935 | #endif |
| 936 | } |