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