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