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