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