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