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