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