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