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