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