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