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