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