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