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