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