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