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