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