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