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