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