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