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