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