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