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