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 | #include "intrcheck.h" |
| 8 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 9 | #ifdef MS_WINDOWS |
Brian Curtin | e5aa886 | 2010-04-02 23:26:06 +0000 | [diff] [blame] | 10 | #include <Windows.h> |
Andrew M. Kuchling | 4b81bc7 | 2010-02-22 23:12:00 +0000 | [diff] [blame] | 11 | #ifdef HAVE_PROCESS_H |
Guido van Rossum | 644a12b | 1997-04-09 19:24:53 +0000 | [diff] [blame] | 12 | #include <process.h> |
| 13 | #endif |
Andrew M. Kuchling | 4b81bc7 | 2010-02-22 23:12:00 +0000 | [diff] [blame] | 14 | #endif |
Guido van Rossum | 644a12b | 1997-04-09 19:24:53 +0000 | [diff] [blame] | 15 | |
Andrew M. Kuchling | 4b81bc7 | 2010-02-22 23:12:00 +0000 | [diff] [blame] | 16 | #ifdef HAVE_SIGNAL_H |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 17 | #include <signal.h> |
Andrew M. Kuchling | 4b81bc7 | 2010-02-22 23:12:00 +0000 | [diff] [blame] | 18 | #endif |
| 19 | #ifdef HAVE_SYS_STAT_H |
Guido van Rossum | 02de897 | 2007-12-19 19:41:06 +0000 | [diff] [blame] | 20 | #include <sys/stat.h> |
Andrew M. Kuchling | 4b81bc7 | 2010-02-22 23:12:00 +0000 | [diff] [blame] | 21 | #endif |
Martin v. Löwis | b74d084 | 2008-03-24 13:54:23 +0000 | [diff] [blame] | 22 | #ifdef HAVE_SYS_TIME_H |
Martin v. Löwis | aef18b1 | 2008-03-24 13:31:16 +0000 | [diff] [blame] | 23 | #include <sys/time.h> |
Martin v. Löwis | b74d084 | 2008-03-24 13:54:23 +0000 | [diff] [blame] | 24 | #endif |
Guido van Rossum | 02de897 | 2007-12-19 19:41:06 +0000 | [diff] [blame] | 25 | |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 26 | #ifndef SIG_ERR |
Guido van Rossum | d2cd7ad | 2000-09-16 16:35:28 +0000 | [diff] [blame] | 27 | #define SIG_ERR ((PyOS_sighandler_t)(-1)) |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 28 | #endif |
| 29 | |
Andrew MacIntyre | 7bf6833 | 2002-03-03 02:59:16 +0000 | [diff] [blame] | 30 | #if defined(PYOS_OS2) && !defined(PYCC_GCC) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 31 | #define NSIG 12 |
| 32 | #include <process.h> |
| 33 | #endif |
| 34 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 35 | #ifndef NSIG |
Marc-André Lemburg | 8bcfb8a | 2000-07-04 14:17:33 +0000 | [diff] [blame] | 36 | # if defined(_NSIG) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 37 | # define NSIG _NSIG /* For BSD/SysV */ |
Marc-André Lemburg | 8bcfb8a | 2000-07-04 14:17:33 +0000 | [diff] [blame] | 38 | # elif defined(_SIGMAX) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 39 | # define NSIG (_SIGMAX + 1) /* For QNX */ |
Marc-André Lemburg | 8bcfb8a | 2000-07-04 14:17:33 +0000 | [diff] [blame] | 40 | # elif defined(SIGMAX) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 41 | # define NSIG (SIGMAX + 1) /* For djgpp */ |
Marc-André Lemburg | 8bcfb8a | 2000-07-04 14:17:33 +0000 | [diff] [blame] | 42 | # else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 43 | # define NSIG 64 /* Use a reasonable default value */ |
Marc-André Lemburg | 8bcfb8a | 2000-07-04 14:17:33 +0000 | [diff] [blame] | 44 | # endif |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 45 | #endif |
| 46 | |
| 47 | |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 48 | /* |
| 49 | NOTES ON THE INTERACTION BETWEEN SIGNALS AND THREADS |
| 50 | |
| 51 | When threads are supported, we want the following semantics: |
| 52 | |
| 53 | - only the main thread can set a signal handler |
| 54 | - any thread can get a signal handler |
| 55 | - signals are only delivered to the main thread |
| 56 | |
| 57 | I.e. we don't support "synchronous signals" like SIGFPE (catching |
| 58 | this doesn't make much sense in Python anyway) nor do we support |
| 59 | signals as a means of inter-thread communication, since not all |
| 60 | thread implementations support that (at least our thread library |
| 61 | doesn't). |
| 62 | |
| 63 | We still have the problem that in some implementations signals |
| 64 | generated by the keyboard (e.g. SIGINT) are delivered to all |
| 65 | threads (e.g. SGI), while in others (e.g. Solaris) such signals are |
| 66 | delivered to one random thread (an intermediate possibility would |
Guido van Rossum | a3c04b0 | 1995-01-12 11:29:01 +0000 | [diff] [blame] | 67 | 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] | 68 | a working implementation that works in all three cases -- the |
| 69 | handler ignores signals if getpid() isn't the same as in the main |
| 70 | thread. XXX This is a hack. |
| 71 | |
Guido van Rossum | 9e8181b | 2000-09-19 00:46:46 +0000 | [diff] [blame] | 72 | GNU pth is a user-space threading library, and as such, all threads |
| 73 | run within the same process. In this case, if the currently running |
| 74 | 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] | 75 | */ |
| 76 | |
| 77 | #ifdef WITH_THREAD |
Guido van Rossum | 295b8e5 | 1997-06-06 21:16:41 +0000 | [diff] [blame] | 78 | #include <sys/types.h> /* For pid_t */ |
Guido van Rossum | 49b5606 | 1998-10-01 20:42:43 +0000 | [diff] [blame] | 79 | #include "pythread.h" |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 80 | static long main_thread; |
| 81 | static pid_t main_pid; |
| 82 | #endif |
| 83 | |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 84 | static struct { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 85 | int tripped; |
| 86 | PyObject *func; |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 87 | } Handlers[NSIG]; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 88 | |
Guido van Rossum | 02de897 | 2007-12-19 19:41:06 +0000 | [diff] [blame] | 89 | static sig_atomic_t wakeup_fd = -1; |
| 90 | |
Guido van Rossum | 137c49c | 2007-12-10 23:00:12 +0000 | [diff] [blame] | 91 | /* Speed up sigcheck() when none tripped */ |
| 92 | static volatile sig_atomic_t is_tripped = 0; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 93 | |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 94 | static PyObject *DefaultHandler; |
| 95 | static PyObject *IgnoreHandler; |
| 96 | static PyObject *IntHandler; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 97 | |
Martin v. Löwis | f58de1b | 2001-03-06 12:13:56 +0000 | [diff] [blame] | 98 | /* On Solaris 8, gcc will produce a warning that the function |
| 99 | declaration is not a prototype. This is caused by the definition of |
| 100 | SIG_DFL as (void (*)())0; the correct declaration would have been |
| 101 | (void (*)(int))0. */ |
| 102 | |
Guido van Rossum | d2cd7ad | 2000-09-16 16:35:28 +0000 | [diff] [blame] | 103 | static PyOS_sighandler_t old_siginthandler = SIG_DFL; |
Guido van Rossum | 08c1661 | 1997-08-02 03:01:42 +0000 | [diff] [blame] | 104 | |
Martin v. Löwis | aef18b1 | 2008-03-24 13:31:16 +0000 | [diff] [blame] | 105 | #ifdef HAVE_GETITIMER |
| 106 | static PyObject *ItimerError; |
| 107 | |
| 108 | /* auxiliary functions for setitimer/getitimer */ |
| 109 | static void |
| 110 | timeval_from_double(double d, struct timeval *tv) |
| 111 | { |
| 112 | tv->tv_sec = floor(d); |
| 113 | tv->tv_usec = fmod(d, 1.0) * 1000000.0; |
Antoine Pitrou | a45a99b | 2017-06-30 10:54:54 +0200 | [diff] [blame^] | 114 | /* Don't disable the timer if the computation above rounds down to zero. */ |
| 115 | if (d > 0.0 && tv->tv_sec == 0 && tv->tv_usec == 0) { |
| 116 | tv->tv_usec = 1; |
| 117 | } |
Martin v. Löwis | aef18b1 | 2008-03-24 13:31:16 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Christian Heimes | 32a66a0 | 2008-10-02 19:47:50 +0000 | [diff] [blame] | 120 | Py_LOCAL_INLINE(double) |
Martin v. Löwis | aef18b1 | 2008-03-24 13:31:16 +0000 | [diff] [blame] | 121 | double_from_timeval(struct timeval *tv) |
| 122 | { |
| 123 | return tv->tv_sec + (double)(tv->tv_usec / 1000000.0); |
| 124 | } |
| 125 | |
| 126 | static PyObject * |
| 127 | itimer_retval(struct itimerval *iv) |
| 128 | { |
| 129 | PyObject *r, *v; |
| 130 | |
| 131 | r = PyTuple_New(2); |
| 132 | if (r == NULL) |
Martin Panter | ca56dd4 | 2016-09-17 07:54:55 +0000 | [diff] [blame] | 133 | return NULL; |
Martin v. Löwis | aef18b1 | 2008-03-24 13:31:16 +0000 | [diff] [blame] | 134 | |
| 135 | if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_value)))) { |
Martin Panter | ca56dd4 | 2016-09-17 07:54:55 +0000 | [diff] [blame] | 136 | Py_DECREF(r); |
| 137 | return NULL; |
Martin v. Löwis | aef18b1 | 2008-03-24 13:31:16 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | PyTuple_SET_ITEM(r, 0, v); |
| 141 | |
| 142 | if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_interval)))) { |
Martin Panter | ca56dd4 | 2016-09-17 07:54:55 +0000 | [diff] [blame] | 143 | Py_DECREF(r); |
| 144 | return NULL; |
Martin v. Löwis | aef18b1 | 2008-03-24 13:31:16 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | PyTuple_SET_ITEM(r, 1, v); |
| 148 | |
| 149 | return r; |
| 150 | } |
| 151 | #endif |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 152 | |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 153 | static PyObject * |
Peter Schneider-Kamp | e89b156 | 2000-07-10 12:04:18 +0000 | [diff] [blame] | 154 | signal_default_int_handler(PyObject *self, PyObject *args) |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 155 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 156 | PyErr_SetNone(PyExc_KeyboardInterrupt); |
| 157 | return NULL; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 160 | PyDoc_STRVAR(default_int_handler_doc, |
Guido van Rossum | 1d8fb2d | 1998-06-28 16:54:49 +0000 | [diff] [blame] | 161 | "default_int_handler(...)\n\ |
| 162 | \n\ |
Michael W. Hudson | 24ec211 | 2004-06-17 15:55:53 +0000 | [diff] [blame] | 163 | The default handler for SIGINT installed by Python.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 164 | It raises KeyboardInterrupt."); |
Guido van Rossum | 1d8fb2d | 1998-06-28 16:54:49 +0000 | [diff] [blame] | 165 | |
Thomas Wouters | 0796b00 | 2000-07-22 23:49:30 +0000 | [diff] [blame] | 166 | |
| 167 | static int |
| 168 | checksignals_witharg(void * unused) |
| 169 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 170 | return PyErr_CheckSignals(); |
Thomas Wouters | 0796b00 | 2000-07-22 23:49:30 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Tim Peters | 4f1b208 | 2000-07-23 21:18:09 +0000 | [diff] [blame] | 173 | static void |
Victor Stinner | 33feeab | 2011-04-18 16:33:28 +0200 | [diff] [blame] | 174 | trip_signal(int sig_num) |
| 175 | { |
| 176 | Handlers[sig_num].tripped = 1; |
| 177 | if (is_tripped) |
| 178 | return; |
| 179 | /* Set is_tripped after setting .tripped, as it gets |
| 180 | cleared in PyErr_CheckSignals() before .tripped. */ |
| 181 | is_tripped = 1; |
| 182 | Py_AddPendingCall(checksignals_witharg, NULL); |
| 183 | if (wakeup_fd != -1) |
| 184 | write(wakeup_fd, "\0", 1); |
| 185 | } |
| 186 | |
| 187 | static void |
Peter Schneider-Kamp | e89b156 | 2000-07-10 12:04:18 +0000 | [diff] [blame] | 188 | signal_handler(int sig_num) |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 189 | { |
Antoine Pitrou | 6b1167c | 2010-11-05 19:55:02 +0000 | [diff] [blame] | 190 | int save_errno = errno; |
| 191 | |
| 192 | #if defined(WITH_THREAD) && defined(WITH_PTH) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 193 | if (PyThread_get_thread_ident() != main_thread) { |
| 194 | pth_raise(*(pth_t *) main_thread, sig_num); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 195 | } |
Antoine Pitrou | 6b1167c | 2010-11-05 19:55:02 +0000 | [diff] [blame] | 196 | else |
Guido van Rossum | 9e8181b | 2000-09-19 00:46:46 +0000 | [diff] [blame] | 197 | #endif |
Antoine Pitrou | 6b1167c | 2010-11-05 19:55:02 +0000 | [diff] [blame] | 198 | { |
| 199 | #ifdef WITH_THREAD |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 200 | /* See NOTES section above */ |
Antoine Pitrou | 6b1167c | 2010-11-05 19:55:02 +0000 | [diff] [blame] | 201 | if (getpid() == main_pid) |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 202 | #endif |
Antoine Pitrou | 6b1167c | 2010-11-05 19:55:02 +0000 | [diff] [blame] | 203 | { |
Victor Stinner | 33feeab | 2011-04-18 16:33:28 +0200 | [diff] [blame] | 204 | trip_signal(sig_num); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 205 | } |
Antoine Pitrou | 6b1167c | 2010-11-05 19:55:02 +0000 | [diff] [blame] | 206 | |
Jean-Paul Calderone | e54ddf1 | 2010-05-08 20:06:02 +0000 | [diff] [blame] | 207 | #ifndef HAVE_SIGACTION |
Antoine Pitrou | 6b1167c | 2010-11-05 19:55:02 +0000 | [diff] [blame] | 208 | #ifdef SIGCHLD |
| 209 | /* To avoid infinite recursion, this signal remains |
| 210 | reset until explicit re-instated. |
| 211 | Don't clear the 'func' field as it is our pointer |
| 212 | to the Python handler... */ |
| 213 | if (sig_num != SIGCHLD) |
| 214 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 215 | /* If the handler was not set up with sigaction, reinstall it. See |
| 216 | * Python/pythonrun.c for the implementation of PyOS_setsig which |
| 217 | * makes this true. See also issue8354. */ |
| 218 | PyOS_setsig(sig_num, signal_handler); |
Jean-Paul Calderone | e54ddf1 | 2010-05-08 20:06:02 +0000 | [diff] [blame] | 219 | #endif |
Antoine Pitrou | 6b1167c | 2010-11-05 19:55:02 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | /* Issue #10311: asynchronously executing signal handlers should not |
| 223 | mutate errno under the feet of unsuspecting C code. */ |
| 224 | errno = save_errno; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 225 | } |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 226 | |
Guido van Rossum | 06d511d | 1995-03-10 15:13:48 +0000 | [diff] [blame] | 227 | |
Guido van Rossum | 1171ee6 | 1997-08-22 20:42:00 +0000 | [diff] [blame] | 228 | #ifdef HAVE_ALARM |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 229 | static PyObject * |
Peter Schneider-Kamp | e89b156 | 2000-07-10 12:04:18 +0000 | [diff] [blame] | 230 | signal_alarm(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 231 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 232 | int t; |
| 233 | if (!PyArg_ParseTuple(args, "i:alarm", &t)) |
| 234 | return NULL; |
| 235 | /* alarm() returns the number of seconds remaining */ |
| 236 | return PyInt_FromLong((long)alarm(t)); |
Guido van Rossum | aa0f4c7 | 1994-08-23 13:49:37 +0000 | [diff] [blame] | 237 | } |
Guido van Rossum | 1d8fb2d | 1998-06-28 16:54:49 +0000 | [diff] [blame] | 238 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 239 | PyDoc_STRVAR(alarm_doc, |
Guido van Rossum | 1d8fb2d | 1998-06-28 16:54:49 +0000 | [diff] [blame] | 240 | "alarm(seconds)\n\ |
| 241 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 242 | Arrange for SIGALRM to arrive after the given number of seconds."); |
Guido van Rossum | 06d511d | 1995-03-10 15:13:48 +0000 | [diff] [blame] | 243 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 244 | |
Guido van Rossum | 1171ee6 | 1997-08-22 20:42:00 +0000 | [diff] [blame] | 245 | #ifdef HAVE_PAUSE |
Guido van Rossum | a597dde | 1995-01-10 20:56:29 +0000 | [diff] [blame] | 246 | static PyObject * |
Neal Norwitz | 3a6f978 | 2002-03-25 20:46:46 +0000 | [diff] [blame] | 247 | signal_pause(PyObject *self) |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 248 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 249 | Py_BEGIN_ALLOW_THREADS |
| 250 | (void)pause(); |
| 251 | Py_END_ALLOW_THREADS |
| 252 | /* make sure that any exceptions that got raised are propagated |
| 253 | * back into Python |
| 254 | */ |
| 255 | if (PyErr_CheckSignals()) |
| 256 | return NULL; |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 257 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 258 | Py_INCREF(Py_None); |
| 259 | return Py_None; |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 260 | } |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 261 | PyDoc_STRVAR(pause_doc, |
Barry Warsaw | 1ee36ff | 1998-07-21 22:41:18 +0000 | [diff] [blame] | 262 | "pause()\n\ |
| 263 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 264 | Wait until a signal arrives."); |
Guido van Rossum | 1d8fb2d | 1998-06-28 16:54:49 +0000 | [diff] [blame] | 265 | |
Guido van Rossum | 06d511d | 1995-03-10 15:13:48 +0000 | [diff] [blame] | 266 | #endif |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 267 | |
Guido van Rossum | d2cd7ad | 2000-09-16 16:35:28 +0000 | [diff] [blame] | 268 | |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 269 | static PyObject * |
Peter Schneider-Kamp | e89b156 | 2000-07-10 12:04:18 +0000 | [diff] [blame] | 270 | signal_signal(PyObject *self, PyObject *args) |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 271 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 272 | PyObject *obj; |
| 273 | int sig_num; |
| 274 | PyObject *old_handler; |
| 275 | void (*func)(int); |
| 276 | if (!PyArg_ParseTuple(args, "iO:signal", &sig_num, &obj)) |
| 277 | return NULL; |
Brian Curtin | 24af0e9 | 2010-08-06 19:41:01 +0000 | [diff] [blame] | 278 | #ifdef MS_WINDOWS |
| 279 | /* Validate that sig_num is one of the allowable signals */ |
Brian Curtin | 8426333 | 2010-09-06 16:17:50 +0000 | [diff] [blame] | 280 | switch (sig_num) { |
| 281 | case SIGABRT: break; |
Brian Curtin | 1390dd7 | 2010-10-01 16:44:03 +0000 | [diff] [blame] | 282 | #ifdef SIGBREAK |
| 283 | /* Issue #10003: SIGBREAK is not documented as permitted, but works |
| 284 | and corresponds to CTRL_BREAK_EVENT. */ |
| 285 | case SIGBREAK: break; |
| 286 | #endif |
Brian Curtin | 8426333 | 2010-09-06 16:17:50 +0000 | [diff] [blame] | 287 | case SIGFPE: break; |
| 288 | case SIGILL: break; |
| 289 | case SIGINT: break; |
| 290 | case SIGSEGV: break; |
| 291 | case SIGTERM: break; |
| 292 | default: |
| 293 | PyErr_SetString(PyExc_ValueError, "invalid signal value"); |
| 294 | return NULL; |
Brian Curtin | 24af0e9 | 2010-08-06 19:41:01 +0000 | [diff] [blame] | 295 | } |
| 296 | #endif |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 297 | #ifdef WITH_THREAD |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 298 | if (PyThread_get_thread_ident() != main_thread) { |
| 299 | PyErr_SetString(PyExc_ValueError, |
| 300 | "signal only works in main thread"); |
| 301 | return NULL; |
| 302 | } |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 303 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 304 | if (sig_num < 1 || sig_num >= NSIG) { |
| 305 | PyErr_SetString(PyExc_ValueError, |
| 306 | "signal number out of range"); |
| 307 | return NULL; |
| 308 | } |
| 309 | if (obj == IgnoreHandler) |
| 310 | func = SIG_IGN; |
| 311 | else if (obj == DefaultHandler) |
| 312 | func = SIG_DFL; |
| 313 | else if (!PyCallable_Check(obj)) { |
| 314 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 315 | "signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object"); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 316 | return NULL; |
| 317 | } |
| 318 | else |
| 319 | func = signal_handler; |
| 320 | if (PyOS_setsig(sig_num, func) == SIG_ERR) { |
| 321 | PyErr_SetFromErrno(PyExc_RuntimeError); |
| 322 | return NULL; |
| 323 | } |
| 324 | old_handler = Handlers[sig_num].func; |
| 325 | Handlers[sig_num].tripped = 0; |
| 326 | Py_INCREF(obj); |
| 327 | Handlers[sig_num].func = obj; |
Antoine Pitrou | d8931c3 | 2013-05-04 23:16:59 +0200 | [diff] [blame] | 328 | if (old_handler != NULL) |
| 329 | return old_handler; |
| 330 | else |
| 331 | Py_RETURN_NONE; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 332 | } |
| 333 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 334 | PyDoc_STRVAR(signal_doc, |
Guido van Rossum | 1d8fb2d | 1998-06-28 16:54:49 +0000 | [diff] [blame] | 335 | "signal(sig, action) -> action\n\ |
| 336 | \n\ |
| 337 | Set the action for the given signal. The action can be SIG_DFL,\n\ |
| 338 | SIG_IGN, or a callable Python object. The previous action is\n\ |
| 339 | returned. See getsignal() for possible return values.\n\ |
| 340 | \n\ |
| 341 | *** IMPORTANT NOTICE ***\n\ |
| 342 | A signal handler function is called with two arguments:\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 343 | 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] | 344 | |
Guido van Rossum | d2cd7ad | 2000-09-16 16:35:28 +0000 | [diff] [blame] | 345 | |
Guido van Rossum | e4485b0 | 1994-09-07 14:32:49 +0000 | [diff] [blame] | 346 | static PyObject * |
Peter Schneider-Kamp | e89b156 | 2000-07-10 12:04:18 +0000 | [diff] [blame] | 347 | signal_getsignal(PyObject *self, PyObject *args) |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 348 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 349 | int sig_num; |
| 350 | PyObject *old_handler; |
| 351 | if (!PyArg_ParseTuple(args, "i:getsignal", &sig_num)) |
| 352 | return NULL; |
| 353 | if (sig_num < 1 || sig_num >= NSIG) { |
| 354 | PyErr_SetString(PyExc_ValueError, |
| 355 | "signal number out of range"); |
| 356 | return NULL; |
| 357 | } |
| 358 | old_handler = Handlers[sig_num].func; |
Antoine Pitrou | d8931c3 | 2013-05-04 23:16:59 +0200 | [diff] [blame] | 359 | if (old_handler != NULL) { |
| 360 | Py_INCREF(old_handler); |
| 361 | return old_handler; |
| 362 | } |
| 363 | else { |
| 364 | Py_RETURN_NONE; |
| 365 | } |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 366 | } |
| 367 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 368 | PyDoc_STRVAR(getsignal_doc, |
Guido van Rossum | 1d8fb2d | 1998-06-28 16:54:49 +0000 | [diff] [blame] | 369 | "getsignal(sig) -> action\n\ |
| 370 | \n\ |
| 371 | Return the current action for the given signal. The return value can be:\n\ |
| 372 | SIG_IGN -- if the signal is being ignored\n\ |
| 373 | SIG_DFL -- if the default action for the signal is in effect\n\ |
| 374 | None -- if an unknown handler is in effect\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 375 | anything else -- the callable Python object used as a handler"); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 376 | |
Facundo Batista | 7e251e8 | 2008-02-23 15:07:35 +0000 | [diff] [blame] | 377 | #ifdef HAVE_SIGINTERRUPT |
| 378 | PyDoc_STRVAR(siginterrupt_doc, |
| 379 | "siginterrupt(sig, flag) -> None\n\ |
| 380 | change system call restart behaviour: if flag is False, system calls\n\ |
| 381 | will be restarted when interrupted by signal sig, else system calls\n\ |
| 382 | will be interrupted."); |
| 383 | |
| 384 | static PyObject * |
| 385 | signal_siginterrupt(PyObject *self, PyObject *args) |
| 386 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 387 | int sig_num; |
| 388 | int flag; |
Facundo Batista | 7e251e8 | 2008-02-23 15:07:35 +0000 | [diff] [blame] | 389 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 390 | if (!PyArg_ParseTuple(args, "ii:siginterrupt", &sig_num, &flag)) |
| 391 | return NULL; |
| 392 | if (sig_num < 1 || sig_num >= NSIG) { |
| 393 | PyErr_SetString(PyExc_ValueError, |
| 394 | "signal number out of range"); |
| 395 | return NULL; |
| 396 | } |
| 397 | if (siginterrupt(sig_num, flag)<0) { |
| 398 | PyErr_SetFromErrno(PyExc_RuntimeError); |
| 399 | return NULL; |
| 400 | } |
Facundo Batista | 7e251e8 | 2008-02-23 15:07:35 +0000 | [diff] [blame] | 401 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 402 | Py_INCREF(Py_None); |
| 403 | return Py_None; |
Facundo Batista | 7e251e8 | 2008-02-23 15:07:35 +0000 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | #endif |
Guido van Rossum | d2cd7ad | 2000-09-16 16:35:28 +0000 | [diff] [blame] | 407 | |
Guido van Rossum | 02de897 | 2007-12-19 19:41:06 +0000 | [diff] [blame] | 408 | static PyObject * |
| 409 | signal_set_wakeup_fd(PyObject *self, PyObject *args) |
| 410 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 411 | struct stat buf; |
| 412 | int fd, old_fd; |
| 413 | if (!PyArg_ParseTuple(args, "i:set_wakeup_fd", &fd)) |
| 414 | return NULL; |
Guido van Rossum | 02de897 | 2007-12-19 19:41:06 +0000 | [diff] [blame] | 415 | #ifdef WITH_THREAD |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 416 | if (PyThread_get_thread_ident() != main_thread) { |
| 417 | PyErr_SetString(PyExc_ValueError, |
| 418 | "set_wakeup_fd only works in main thread"); |
| 419 | return NULL; |
| 420 | } |
Guido van Rossum | 02de897 | 2007-12-19 19:41:06 +0000 | [diff] [blame] | 421 | #endif |
Benjamin Peterson | 08e153a | 2013-01-18 00:10:24 -0500 | [diff] [blame] | 422 | if (fd != -1 && (!_PyVerify_fd(fd) || fstat(fd, &buf) != 0)) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 423 | PyErr_SetString(PyExc_ValueError, "invalid fd"); |
| 424 | return NULL; |
| 425 | } |
| 426 | old_fd = wakeup_fd; |
| 427 | wakeup_fd = fd; |
| 428 | return PyLong_FromLong(old_fd); |
Guido van Rossum | 02de897 | 2007-12-19 19:41:06 +0000 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | PyDoc_STRVAR(set_wakeup_fd_doc, |
| 432 | "set_wakeup_fd(fd) -> fd\n\ |
| 433 | \n\ |
| 434 | Sets the fd to be written to (with '\\0') when a signal\n\ |
| 435 | comes in. A library can use this to wakeup select or poll.\n\ |
| 436 | The previous fd is returned.\n\ |
| 437 | \n\ |
| 438 | The fd must be non-blocking."); |
| 439 | |
| 440 | /* C API for the same, without all the error checking */ |
| 441 | int |
| 442 | PySignal_SetWakeupFd(int fd) |
| 443 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 444 | int old_fd = wakeup_fd; |
| 445 | if (fd < 0) |
| 446 | fd = -1; |
| 447 | wakeup_fd = fd; |
| 448 | return old_fd; |
Guido van Rossum | 02de897 | 2007-12-19 19:41:06 +0000 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | |
Martin v. Löwis | aef18b1 | 2008-03-24 13:31:16 +0000 | [diff] [blame] | 452 | #ifdef HAVE_SETITIMER |
| 453 | static PyObject * |
| 454 | signal_setitimer(PyObject *self, PyObject *args) |
| 455 | { |
| 456 | double first; |
| 457 | double interval = 0; |
| 458 | int which; |
| 459 | struct itimerval new, old; |
| 460 | |
| 461 | if(!PyArg_ParseTuple(args, "id|d:setitimer", &which, &first, &interval)) |
Martin Panter | ca56dd4 | 2016-09-17 07:54:55 +0000 | [diff] [blame] | 462 | return NULL; |
Martin v. Löwis | aef18b1 | 2008-03-24 13:31:16 +0000 | [diff] [blame] | 463 | |
| 464 | timeval_from_double(first, &new.it_value); |
| 465 | timeval_from_double(interval, &new.it_interval); |
| 466 | /* Let OS check "which" value */ |
| 467 | if (setitimer(which, &new, &old) != 0) { |
Martin Panter | ca56dd4 | 2016-09-17 07:54:55 +0000 | [diff] [blame] | 468 | PyErr_SetFromErrno(ItimerError); |
| 469 | return NULL; |
Martin v. Löwis | aef18b1 | 2008-03-24 13:31:16 +0000 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | return itimer_retval(&old); |
| 473 | } |
| 474 | |
| 475 | PyDoc_STRVAR(setitimer_doc, |
| 476 | "setitimer(which, seconds[, interval])\n\ |
| 477 | \n\ |
| 478 | Sets given itimer (one of ITIMER_REAL, ITIMER_VIRTUAL\n\ |
| 479 | or ITIMER_PROF) to fire after value seconds and after\n\ |
| 480 | that every interval seconds.\n\ |
| 481 | The itimer can be cleared by setting seconds to zero.\n\ |
| 482 | \n\ |
| 483 | Returns old values as a tuple: (delay, interval)."); |
| 484 | #endif |
| 485 | |
| 486 | |
| 487 | #ifdef HAVE_GETITIMER |
| 488 | static PyObject * |
| 489 | signal_getitimer(PyObject *self, PyObject *args) |
| 490 | { |
| 491 | int which; |
| 492 | struct itimerval old; |
| 493 | |
| 494 | if (!PyArg_ParseTuple(args, "i:getitimer", &which)) |
Martin Panter | ca56dd4 | 2016-09-17 07:54:55 +0000 | [diff] [blame] | 495 | return NULL; |
Martin v. Löwis | aef18b1 | 2008-03-24 13:31:16 +0000 | [diff] [blame] | 496 | |
| 497 | if (getitimer(which, &old) != 0) { |
Martin Panter | ca56dd4 | 2016-09-17 07:54:55 +0000 | [diff] [blame] | 498 | PyErr_SetFromErrno(ItimerError); |
| 499 | return NULL; |
Martin v. Löwis | aef18b1 | 2008-03-24 13:31:16 +0000 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | return itimer_retval(&old); |
| 503 | } |
| 504 | |
| 505 | PyDoc_STRVAR(getitimer_doc, |
| 506 | "getitimer(which)\n\ |
| 507 | \n\ |
| 508 | Returns current value of given itimer."); |
| 509 | #endif |
| 510 | |
| 511 | |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 512 | /* List of functions defined in the module */ |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 513 | static PyMethodDef signal_methods[] = { |
Guido van Rossum | 1171ee6 | 1997-08-22 20:42:00 +0000 | [diff] [blame] | 514 | #ifdef HAVE_ALARM |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 515 | {"alarm", signal_alarm, METH_VARARGS, alarm_doc}, |
Guido van Rossum | 06d511d | 1995-03-10 15:13:48 +0000 | [diff] [blame] | 516 | #endif |
Martin v. Löwis | aef18b1 | 2008-03-24 13:31:16 +0000 | [diff] [blame] | 517 | #ifdef HAVE_SETITIMER |
| 518 | {"setitimer", signal_setitimer, METH_VARARGS, setitimer_doc}, |
| 519 | #endif |
| 520 | #ifdef HAVE_GETITIMER |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 521 | {"getitimer", signal_getitimer, METH_VARARGS, getitimer_doc}, |
Martin v. Löwis | aef18b1 | 2008-03-24 13:31:16 +0000 | [diff] [blame] | 522 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 523 | {"signal", signal_signal, METH_VARARGS, signal_doc}, |
| 524 | {"getsignal", signal_getsignal, METH_VARARGS, getsignal_doc}, |
| 525 | {"set_wakeup_fd", signal_set_wakeup_fd, METH_VARARGS, set_wakeup_fd_doc}, |
Facundo Batista | 7e251e8 | 2008-02-23 15:07:35 +0000 | [diff] [blame] | 526 | #ifdef HAVE_SIGINTERRUPT |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 527 | {"siginterrupt", signal_siginterrupt, METH_VARARGS, siginterrupt_doc}, |
Facundo Batista | 7e251e8 | 2008-02-23 15:07:35 +0000 | [diff] [blame] | 528 | #endif |
Guido van Rossum | 1171ee6 | 1997-08-22 20:42:00 +0000 | [diff] [blame] | 529 | #ifdef HAVE_PAUSE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 530 | {"pause", (PyCFunction)signal_pause, |
| 531 | METH_NOARGS,pause_doc}, |
Guido van Rossum | 06d511d | 1995-03-10 15:13:48 +0000 | [diff] [blame] | 532 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 533 | {"default_int_handler", signal_default_int_handler, |
| 534 | METH_VARARGS, default_int_handler_doc}, |
| 535 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 536 | }; |
| 537 | |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 538 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 539 | PyDoc_STRVAR(module_doc, |
Guido van Rossum | 1d8fb2d | 1998-06-28 16:54:49 +0000 | [diff] [blame] | 540 | "This module provides mechanisms to use signal handlers in Python.\n\ |
| 541 | \n\ |
| 542 | Functions:\n\ |
| 543 | \n\ |
| 544 | alarm() -- cause SIGALRM after a specified time [Unix only]\n\ |
Martin v. Löwis | aef18b1 | 2008-03-24 13:31:16 +0000 | [diff] [blame] | 545 | setitimer() -- cause a signal (described below) after a specified\n\ |
| 546 | float time and the timer may restart then [Unix only]\n\ |
| 547 | getitimer() -- get current value of timer [Unix only]\n\ |
Guido van Rossum | 1d8fb2d | 1998-06-28 16:54:49 +0000 | [diff] [blame] | 548 | signal() -- set the action for a given signal\n\ |
| 549 | getsignal() -- get the signal action for a given signal\n\ |
| 550 | pause() -- wait until a signal arrives [Unix only]\n\ |
| 551 | default_int_handler() -- default SIGINT handler\n\ |
| 552 | \n\ |
Martin v. Löwis | aef18b1 | 2008-03-24 13:31:16 +0000 | [diff] [blame] | 553 | signal constants:\n\ |
Guido van Rossum | 1d8fb2d | 1998-06-28 16:54:49 +0000 | [diff] [blame] | 554 | SIG_DFL -- used to refer to the system default handler\n\ |
| 555 | SIG_IGN -- used to ignore the signal\n\ |
| 556 | NSIG -- number of defined signals\n\ |
Guido van Rossum | 1d8fb2d | 1998-06-28 16:54:49 +0000 | [diff] [blame] | 557 | SIGINT, SIGTERM, etc. -- signal numbers\n\ |
| 558 | \n\ |
Martin v. Löwis | aef18b1 | 2008-03-24 13:31:16 +0000 | [diff] [blame] | 559 | itimer constants:\n\ |
| 560 | ITIMER_REAL -- decrements in real time, and delivers SIGALRM upon\n\ |
| 561 | expiration\n\ |
| 562 | ITIMER_VIRTUAL -- decrements only when the process is executing,\n\ |
| 563 | and delivers SIGVTALRM upon expiration\n\ |
| 564 | ITIMER_PROF -- decrements both when the process is executing and\n\ |
| 565 | when the system is executing on behalf of the process.\n\ |
| 566 | Coupled with ITIMER_VIRTUAL, this timer is usually\n\ |
| 567 | used to profile the time spent by the application\n\ |
| 568 | in user and kernel space. SIGPROF is delivered upon\n\ |
| 569 | expiration.\n\ |
| 570 | \n\n\ |
Guido van Rossum | 1d8fb2d | 1998-06-28 16:54:49 +0000 | [diff] [blame] | 571 | *** IMPORTANT NOTICE ***\n\ |
| 572 | A signal handler function is called with two arguments:\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 573 | 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] | 574 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 575 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 576 | initsignal(void) |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 577 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 578 | PyObject *m, *d, *x; |
| 579 | int i; |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 580 | |
| 581 | #ifdef WITH_THREAD |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 582 | main_thread = PyThread_get_thread_ident(); |
| 583 | main_pid = getpid(); |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 584 | #endif |
| 585 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 586 | /* Create the module and add the functions */ |
| 587 | m = Py_InitModule3("signal", signal_methods, module_doc); |
| 588 | if (m == NULL) |
| 589 | return; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 590 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 591 | /* Add some symbolic constants to the module */ |
| 592 | d = PyModule_GetDict(m); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 593 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 594 | x = DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL); |
| 595 | if (!x || PyDict_SetItemString(d, "SIG_DFL", x) < 0) |
| 596 | goto finally; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 597 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 598 | x = IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN); |
| 599 | if (!x || PyDict_SetItemString(d, "SIG_IGN", x) < 0) |
| 600 | goto finally; |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 601 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 602 | x = PyInt_FromLong((long)NSIG); |
| 603 | if (!x || PyDict_SetItemString(d, "NSIG", x) < 0) |
| 604 | goto finally; |
| 605 | Py_DECREF(x); |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 606 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 607 | x = IntHandler = PyDict_GetItemString(d, "default_int_handler"); |
| 608 | if (!x) |
| 609 | goto finally; |
| 610 | Py_INCREF(IntHandler); |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 611 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 612 | Handlers[0].tripped = 0; |
| 613 | for (i = 1; i < NSIG; i++) { |
| 614 | void (*t)(int); |
| 615 | t = PyOS_getsig(i); |
| 616 | Handlers[i].tripped = 0; |
| 617 | if (t == SIG_DFL) |
| 618 | Handlers[i].func = DefaultHandler; |
| 619 | else if (t == SIG_IGN) |
| 620 | Handlers[i].func = IgnoreHandler; |
| 621 | else |
| 622 | Handlers[i].func = Py_None; /* None of our business */ |
| 623 | Py_INCREF(Handlers[i].func); |
| 624 | } |
| 625 | if (Handlers[SIGINT].func == DefaultHandler) { |
| 626 | /* Install default int handler */ |
| 627 | Py_INCREF(IntHandler); |
Serhiy Storchaka | 763a61c | 2016-04-10 18:05:12 +0300 | [diff] [blame] | 628 | Py_SETREF(Handlers[SIGINT].func, IntHandler); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 629 | old_siginthandler = PyOS_setsig(SIGINT, signal_handler); |
| 630 | } |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 631 | |
| 632 | #ifdef SIGHUP |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 633 | x = PyInt_FromLong(SIGHUP); |
| 634 | PyDict_SetItemString(d, "SIGHUP", x); |
| 635 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 636 | #endif |
| 637 | #ifdef SIGINT |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 638 | x = PyInt_FromLong(SIGINT); |
| 639 | PyDict_SetItemString(d, "SIGINT", x); |
| 640 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 641 | #endif |
Tim Peters | 1ce3cf7 | 2001-10-01 17:58:40 +0000 | [diff] [blame] | 642 | #ifdef SIGBREAK |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 643 | x = PyInt_FromLong(SIGBREAK); |
| 644 | PyDict_SetItemString(d, "SIGBREAK", x); |
| 645 | Py_XDECREF(x); |
Tim Peters | 1ce3cf7 | 2001-10-01 17:58:40 +0000 | [diff] [blame] | 646 | #endif |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 647 | #ifdef SIGQUIT |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 648 | x = PyInt_FromLong(SIGQUIT); |
| 649 | PyDict_SetItemString(d, "SIGQUIT", x); |
| 650 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 651 | #endif |
| 652 | #ifdef SIGILL |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 653 | x = PyInt_FromLong(SIGILL); |
| 654 | PyDict_SetItemString(d, "SIGILL", x); |
| 655 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 656 | #endif |
| 657 | #ifdef SIGTRAP |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 658 | x = PyInt_FromLong(SIGTRAP); |
| 659 | PyDict_SetItemString(d, "SIGTRAP", x); |
| 660 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 661 | #endif |
| 662 | #ifdef SIGIOT |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 663 | x = PyInt_FromLong(SIGIOT); |
| 664 | PyDict_SetItemString(d, "SIGIOT", x); |
| 665 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 666 | #endif |
| 667 | #ifdef SIGABRT |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 668 | x = PyInt_FromLong(SIGABRT); |
| 669 | PyDict_SetItemString(d, "SIGABRT", x); |
| 670 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 671 | #endif |
| 672 | #ifdef SIGEMT |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 673 | x = PyInt_FromLong(SIGEMT); |
| 674 | PyDict_SetItemString(d, "SIGEMT", x); |
| 675 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 676 | #endif |
| 677 | #ifdef SIGFPE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 678 | x = PyInt_FromLong(SIGFPE); |
| 679 | PyDict_SetItemString(d, "SIGFPE", x); |
| 680 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 681 | #endif |
| 682 | #ifdef SIGKILL |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 683 | x = PyInt_FromLong(SIGKILL); |
| 684 | PyDict_SetItemString(d, "SIGKILL", x); |
| 685 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 686 | #endif |
| 687 | #ifdef SIGBUS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 688 | x = PyInt_FromLong(SIGBUS); |
| 689 | PyDict_SetItemString(d, "SIGBUS", x); |
| 690 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 691 | #endif |
| 692 | #ifdef SIGSEGV |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 693 | x = PyInt_FromLong(SIGSEGV); |
| 694 | PyDict_SetItemString(d, "SIGSEGV", x); |
| 695 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 696 | #endif |
| 697 | #ifdef SIGSYS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 698 | x = PyInt_FromLong(SIGSYS); |
| 699 | PyDict_SetItemString(d, "SIGSYS", x); |
| 700 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 701 | #endif |
| 702 | #ifdef SIGPIPE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 703 | x = PyInt_FromLong(SIGPIPE); |
| 704 | PyDict_SetItemString(d, "SIGPIPE", x); |
| 705 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 706 | #endif |
| 707 | #ifdef SIGALRM |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 708 | x = PyInt_FromLong(SIGALRM); |
| 709 | PyDict_SetItemString(d, "SIGALRM", x); |
| 710 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 711 | #endif |
| 712 | #ifdef SIGTERM |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 713 | x = PyInt_FromLong(SIGTERM); |
| 714 | PyDict_SetItemString(d, "SIGTERM", x); |
| 715 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 716 | #endif |
| 717 | #ifdef SIGUSR1 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 718 | x = PyInt_FromLong(SIGUSR1); |
| 719 | PyDict_SetItemString(d, "SIGUSR1", x); |
| 720 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 721 | #endif |
| 722 | #ifdef SIGUSR2 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 723 | x = PyInt_FromLong(SIGUSR2); |
| 724 | PyDict_SetItemString(d, "SIGUSR2", x); |
| 725 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 726 | #endif |
| 727 | #ifdef SIGCLD |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 728 | x = PyInt_FromLong(SIGCLD); |
| 729 | PyDict_SetItemString(d, "SIGCLD", x); |
| 730 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 731 | #endif |
| 732 | #ifdef SIGCHLD |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 733 | x = PyInt_FromLong(SIGCHLD); |
| 734 | PyDict_SetItemString(d, "SIGCHLD", x); |
| 735 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 736 | #endif |
| 737 | #ifdef SIGPWR |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 738 | x = PyInt_FromLong(SIGPWR); |
| 739 | PyDict_SetItemString(d, "SIGPWR", x); |
| 740 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 741 | #endif |
| 742 | #ifdef SIGIO |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 743 | x = PyInt_FromLong(SIGIO); |
| 744 | PyDict_SetItemString(d, "SIGIO", x); |
| 745 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 746 | #endif |
| 747 | #ifdef SIGURG |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 748 | x = PyInt_FromLong(SIGURG); |
| 749 | PyDict_SetItemString(d, "SIGURG", x); |
| 750 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 751 | #endif |
| 752 | #ifdef SIGWINCH |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 753 | x = PyInt_FromLong(SIGWINCH); |
| 754 | PyDict_SetItemString(d, "SIGWINCH", x); |
| 755 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 756 | #endif |
| 757 | #ifdef SIGPOLL |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 758 | x = PyInt_FromLong(SIGPOLL); |
| 759 | PyDict_SetItemString(d, "SIGPOLL", x); |
| 760 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 761 | #endif |
| 762 | #ifdef SIGSTOP |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 763 | x = PyInt_FromLong(SIGSTOP); |
| 764 | PyDict_SetItemString(d, "SIGSTOP", x); |
| 765 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 766 | #endif |
| 767 | #ifdef SIGTSTP |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 768 | x = PyInt_FromLong(SIGTSTP); |
| 769 | PyDict_SetItemString(d, "SIGTSTP", x); |
| 770 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 771 | #endif |
| 772 | #ifdef SIGCONT |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 773 | x = PyInt_FromLong(SIGCONT); |
| 774 | PyDict_SetItemString(d, "SIGCONT", x); |
| 775 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 776 | #endif |
| 777 | #ifdef SIGTTIN |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 778 | x = PyInt_FromLong(SIGTTIN); |
| 779 | PyDict_SetItemString(d, "SIGTTIN", x); |
| 780 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 781 | #endif |
| 782 | #ifdef SIGTTOU |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 783 | x = PyInt_FromLong(SIGTTOU); |
| 784 | PyDict_SetItemString(d, "SIGTTOU", x); |
| 785 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 786 | #endif |
| 787 | #ifdef SIGVTALRM |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 788 | x = PyInt_FromLong(SIGVTALRM); |
| 789 | PyDict_SetItemString(d, "SIGVTALRM", x); |
| 790 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 791 | #endif |
| 792 | #ifdef SIGPROF |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 793 | x = PyInt_FromLong(SIGPROF); |
| 794 | PyDict_SetItemString(d, "SIGPROF", x); |
| 795 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 796 | #endif |
Barry Warsaw | 14ed5fb | 1996-12-16 20:24:22 +0000 | [diff] [blame] | 797 | #ifdef SIGXCPU |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 798 | x = PyInt_FromLong(SIGXCPU); |
| 799 | PyDict_SetItemString(d, "SIGXCPU", x); |
| 800 | Py_XDECREF(x); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 801 | #endif |
Barry Warsaw | 14ed5fb | 1996-12-16 20:24:22 +0000 | [diff] [blame] | 802 | #ifdef SIGXFSZ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 803 | x = PyInt_FromLong(SIGXFSZ); |
| 804 | PyDict_SetItemString(d, "SIGXFSZ", x); |
| 805 | Py_XDECREF(x); |
Barry Warsaw | 14ed5fb | 1996-12-16 20:24:22 +0000 | [diff] [blame] | 806 | #endif |
Anthony Baxter | f37f37d | 2003-07-31 10:35:29 +0000 | [diff] [blame] | 807 | #ifdef SIGRTMIN |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 808 | x = PyInt_FromLong(SIGRTMIN); |
| 809 | PyDict_SetItemString(d, "SIGRTMIN", x); |
| 810 | Py_XDECREF(x); |
Anthony Baxter | f37f37d | 2003-07-31 10:35:29 +0000 | [diff] [blame] | 811 | #endif |
| 812 | #ifdef SIGRTMAX |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 813 | x = PyInt_FromLong(SIGRTMAX); |
| 814 | PyDict_SetItemString(d, "SIGRTMAX", x); |
| 815 | Py_XDECREF(x); |
Anthony Baxter | f37f37d | 2003-07-31 10:35:29 +0000 | [diff] [blame] | 816 | #endif |
Martin v. Löwis | 175af25 | 2002-01-12 11:43:25 +0000 | [diff] [blame] | 817 | #ifdef SIGINFO |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 818 | x = PyInt_FromLong(SIGINFO); |
| 819 | PyDict_SetItemString(d, "SIGINFO", x); |
| 820 | Py_XDECREF(x); |
Martin v. Löwis | 175af25 | 2002-01-12 11:43:25 +0000 | [diff] [blame] | 821 | #endif |
Martin v. Löwis | aef18b1 | 2008-03-24 13:31:16 +0000 | [diff] [blame] | 822 | |
| 823 | #ifdef ITIMER_REAL |
| 824 | x = PyLong_FromLong(ITIMER_REAL); |
| 825 | PyDict_SetItemString(d, "ITIMER_REAL", x); |
| 826 | Py_DECREF(x); |
| 827 | #endif |
| 828 | #ifdef ITIMER_VIRTUAL |
| 829 | x = PyLong_FromLong(ITIMER_VIRTUAL); |
| 830 | PyDict_SetItemString(d, "ITIMER_VIRTUAL", x); |
| 831 | Py_DECREF(x); |
| 832 | #endif |
| 833 | #ifdef ITIMER_PROF |
| 834 | x = PyLong_FromLong(ITIMER_PROF); |
| 835 | PyDict_SetItemString(d, "ITIMER_PROF", x); |
| 836 | Py_DECREF(x); |
| 837 | #endif |
| 838 | |
| 839 | #if defined (HAVE_SETITIMER) || defined (HAVE_GETITIMER) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 840 | ItimerError = PyErr_NewException("signal.ItimerError", |
Martin Panter | ca56dd4 | 2016-09-17 07:54:55 +0000 | [diff] [blame] | 841 | PyExc_IOError, NULL); |
Neal Norwitz | 18aa388 | 2008-08-24 05:04:52 +0000 | [diff] [blame] | 842 | if (ItimerError != NULL) |
Martin Panter | ca56dd4 | 2016-09-17 07:54:55 +0000 | [diff] [blame] | 843 | PyDict_SetItemString(d, "ItimerError", ItimerError); |
Martin v. Löwis | aef18b1 | 2008-03-24 13:31:16 +0000 | [diff] [blame] | 844 | #endif |
| 845 | |
Brian Curtin | e5aa886 | 2010-04-02 23:26:06 +0000 | [diff] [blame] | 846 | #ifdef CTRL_C_EVENT |
| 847 | x = PyInt_FromLong(CTRL_C_EVENT); |
| 848 | PyDict_SetItemString(d, "CTRL_C_EVENT", x); |
| 849 | Py_DECREF(x); |
| 850 | #endif |
| 851 | |
| 852 | #ifdef CTRL_BREAK_EVENT |
| 853 | x = PyInt_FromLong(CTRL_BREAK_EVENT); |
| 854 | PyDict_SetItemString(d, "CTRL_BREAK_EVENT", x); |
| 855 | Py_DECREF(x); |
| 856 | #endif |
| 857 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 858 | if (!PyErr_Occurred()) |
Guido van Rossum | 08c1661 | 1997-08-02 03:01:42 +0000 | [diff] [blame] | 859 | return; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 860 | |
| 861 | /* Check for errors */ |
| 862 | finally: |
| 863 | return; |
Guido van Rossum | 08c1661 | 1997-08-02 03:01:42 +0000 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | static void |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 867 | finisignal(void) |
Guido van Rossum | 08c1661 | 1997-08-02 03:01:42 +0000 | [diff] [blame] | 868 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 869 | int i; |
| 870 | PyObject *func; |
Guido van Rossum | 08c1661 | 1997-08-02 03:01:42 +0000 | [diff] [blame] | 871 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 872 | PyOS_setsig(SIGINT, old_siginthandler); |
| 873 | old_siginthandler = SIG_DFL; |
Guido van Rossum | 08c1661 | 1997-08-02 03:01:42 +0000 | [diff] [blame] | 874 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 875 | for (i = 1; i < NSIG; i++) { |
| 876 | func = Handlers[i].func; |
| 877 | Handlers[i].tripped = 0; |
| 878 | Handlers[i].func = NULL; |
| 879 | if (i != SIGINT && func != NULL && func != Py_None && |
| 880 | func != DefaultHandler && func != IgnoreHandler) |
| 881 | PyOS_setsig(i, SIG_DFL); |
| 882 | Py_XDECREF(func); |
| 883 | } |
Guido van Rossum | 08c1661 | 1997-08-02 03:01:42 +0000 | [diff] [blame] | 884 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 885 | Py_XDECREF(IntHandler); |
| 886 | IntHandler = NULL; |
| 887 | Py_XDECREF(DefaultHandler); |
| 888 | DefaultHandler = NULL; |
| 889 | Py_XDECREF(IgnoreHandler); |
| 890 | IgnoreHandler = NULL; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 891 | } |
| 892 | |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 893 | |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 894 | /* Declared in pyerrors.h */ |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 895 | int |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 896 | PyErr_CheckSignals(void) |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 897 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 898 | int i; |
| 899 | PyObject *f; |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 900 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 901 | if (!is_tripped) |
| 902 | return 0; |
Guido van Rossum | 137c49c | 2007-12-10 23:00:12 +0000 | [diff] [blame] | 903 | |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 904 | #ifdef WITH_THREAD |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 905 | if (PyThread_get_thread_ident() != main_thread) |
| 906 | return 0; |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 907 | #endif |
Guido van Rossum | 137c49c | 2007-12-10 23:00:12 +0000 | [diff] [blame] | 908 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 909 | /* |
| 910 | * The is_tripped variable is meant to speed up the calls to |
| 911 | * PyErr_CheckSignals (both directly or via pending calls) when no |
| 912 | * signal has arrived. This variable is set to 1 when a signal arrives |
| 913 | * and it is set to 0 here, when we know some signals arrived. This way |
| 914 | * we can run the registered handlers with no signals blocked. |
| 915 | * |
| 916 | * NOTE: with this approach we can have a situation where is_tripped is |
| 917 | * 1 but we have no more signals to handle (Handlers[i].tripped |
| 918 | * is 0 for every signal i). This won't do us any harm (except |
| 919 | * we're gonna spent some cycles for nothing). This happens when |
| 920 | * we receive a signal i after we zero is_tripped and before we |
| 921 | * check Handlers[i].tripped. |
| 922 | */ |
| 923 | is_tripped = 0; |
Guido van Rossum | 137c49c | 2007-12-10 23:00:12 +0000 | [diff] [blame] | 924 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 925 | if (!(f = (PyObject *)PyEval_GetFrame())) |
| 926 | f = Py_None; |
Guido van Rossum | 137c49c | 2007-12-10 23:00:12 +0000 | [diff] [blame] | 927 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 928 | for (i = 1; i < NSIG; i++) { |
| 929 | if (Handlers[i].tripped) { |
| 930 | PyObject *result = NULL; |
| 931 | PyObject *arglist = Py_BuildValue("(iO)", i, f); |
| 932 | Handlers[i].tripped = 0; |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 933 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 934 | if (arglist) { |
| 935 | result = PyEval_CallObject(Handlers[i].func, |
| 936 | arglist); |
| 937 | Py_DECREF(arglist); |
| 938 | } |
| 939 | if (!result) |
| 940 | return -1; |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 941 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 942 | Py_DECREF(result); |
| 943 | } |
| 944 | } |
Guido van Rossum | 137c49c | 2007-12-10 23:00:12 +0000 | [diff] [blame] | 945 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 946 | return 0; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 947 | } |
| 948 | |
Guido van Rossum | d2cd7ad | 2000-09-16 16:35:28 +0000 | [diff] [blame] | 949 | |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 950 | /* Replacements for intrcheck.c functionality |
| 951 | * Declared in pyerrors.h |
| 952 | */ |
| 953 | void |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 954 | PyErr_SetInterrupt(void) |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 955 | { |
Victor Stinner | 33feeab | 2011-04-18 16:33:28 +0200 | [diff] [blame] | 956 | trip_signal(SIGINT); |
Barry Warsaw | 9297117 | 1997-01-03 00:14:25 +0000 | [diff] [blame] | 957 | } |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 958 | |
| 959 | void |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 960 | PyOS_InitInterrupts(void) |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 961 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 962 | initsignal(); |
| 963 | _PyImport_FixupExtension("signal", "signal"); |
Guido van Rossum | 08c1661 | 1997-08-02 03:01:42 +0000 | [diff] [blame] | 964 | } |
| 965 | |
| 966 | void |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 967 | PyOS_FiniInterrupts(void) |
Guido van Rossum | 08c1661 | 1997-08-02 03:01:42 +0000 | [diff] [blame] | 968 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 969 | finisignal(); |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 970 | } |
| 971 | |
| 972 | int |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 973 | PyOS_InterruptOccurred(void) |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 974 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 975 | if (Handlers[SIGINT].tripped) { |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 976 | #ifdef WITH_THREAD |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 977 | if (PyThread_get_thread_ident() != main_thread) |
| 978 | return 0; |
Guido van Rossum | bb4ba12 | 1994-06-23 11:25:45 +0000 | [diff] [blame] | 979 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 980 | Handlers[SIGINT].tripped = 0; |
| 981 | return 1; |
| 982 | } |
| 983 | return 0; |
Guido van Rossum | 398d9fe | 1994-05-11 08:59:13 +0000 | [diff] [blame] | 984 | } |
Guido van Rossum | 359bcaa | 1997-11-14 22:24:28 +0000 | [diff] [blame] | 985 | |
Gregory P. Smith | c1ce93a | 2012-11-10 20:38:17 -0800 | [diff] [blame] | 986 | static void |
| 987 | _clear_pending_signals(void) |
| 988 | { |
| 989 | int i; |
| 990 | if (!is_tripped) |
| 991 | return; |
| 992 | is_tripped = 0; |
| 993 | for (i = 1; i < NSIG; ++i) { |
| 994 | Handlers[i].tripped = 0; |
| 995 | } |
| 996 | } |
| 997 | |
Guido van Rossum | 359bcaa | 1997-11-14 22:24:28 +0000 | [diff] [blame] | 998 | void |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 999 | PyOS_AfterFork(void) |
Guido van Rossum | 359bcaa | 1997-11-14 22:24:28 +0000 | [diff] [blame] | 1000 | { |
Gregory P. Smith | c1ce93a | 2012-11-10 20:38:17 -0800 | [diff] [blame] | 1001 | /* Clear the signal flags after forking so that they aren't handled |
| 1002 | * in both processes if they came in just before the fork() but before |
| 1003 | * the interpreter had an opportunity to call the handlers. issue9535. */ |
| 1004 | _clear_pending_signals(); |
Guido van Rossum | 359bcaa | 1997-11-14 22:24:28 +0000 | [diff] [blame] | 1005 | #ifdef WITH_THREAD |
Charles-François Natali | e0e88b0 | 2012-02-02 19:57:19 +0100 | [diff] [blame] | 1006 | /* PyThread_ReInitTLS() must be called early, to make sure that the TLS API |
| 1007 | * can be called safely. */ |
| 1008 | PyThread_ReInitTLS(); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1009 | PyEval_ReInitThreads(); |
| 1010 | main_thread = PyThread_get_thread_ident(); |
| 1011 | main_pid = getpid(); |
| 1012 | _PyImport_ReInitLock(); |
Guido van Rossum | 359bcaa | 1997-11-14 22:24:28 +0000 | [diff] [blame] | 1013 | #endif |
| 1014 | } |