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