|
13 | 13 | #include "pycore_moduleobject.h" // _PyModule_GetState() |
14 | 14 | #include "pycore_pyerrors.h" // _PyErr_SetString() |
15 | 15 | #include "pycore_pystate.h" // _PyThreadState_GET() |
16 | | -#include "pycore_signal.h" // Py_NSIG |
| 16 | +#include "pycore_signal.h" |
17 | 17 |
|
18 | 18 | #ifndef MS_WINDOWS |
19 | 19 | # include "posixmodule.h" |
|
23 | 23 | #endif |
24 | 24 |
|
25 | 25 | #ifdef MS_WINDOWS |
26 | | -# include <windows.h> |
27 | 26 | # ifdef HAVE_PROCESS_H |
28 | 27 | # include <process.h> |
29 | 28 | # endif |
30 | 29 | #endif |
31 | 30 |
|
| 31 | +#include "pycore_signal.h" // Py_NSIG |
| 32 | + |
32 | 33 | #ifdef HAVE_SIGNAL_H |
33 | 34 | # include <signal.h> |
34 | 35 | #endif |
@@ -100,47 +101,13 @@ class sigset_t_converter(CConverter): |
100 | 101 | may not be the thread that received the signal. |
101 | 102 | */ |
102 | 103 |
|
103 | | -static volatile struct { |
104 | | - _Py_atomic_int tripped; |
105 | | - /* func is atomic to ensure that PyErr_SetInterrupt is async-signal-safe |
106 | | - * (even though it would probably be otherwise, anyway). |
107 | | - */ |
108 | | - _Py_atomic_address func; |
109 | | -} Handlers[Py_NSIG]; |
110 | | - |
111 | | -#ifdef MS_WINDOWS |
112 | | -#define INVALID_FD ((SOCKET_T)-1) |
113 | | - |
114 | | -static volatile struct { |
115 | | - SOCKET_T fd; |
116 | | - int warn_on_full_buffer; |
117 | | - int use_send; |
118 | | -} wakeup = {.fd = INVALID_FD, .warn_on_full_buffer = 1, .use_send = 0}; |
119 | | -#else |
120 | | -#define INVALID_FD (-1) |
121 | | -static volatile struct { |
122 | | -#ifdef __VXWORKS__ |
123 | | - int fd; |
124 | | -#else |
125 | | - sig_atomic_t fd; |
126 | | -#endif |
127 | | - int warn_on_full_buffer; |
128 | | -} wakeup = {.fd = INVALID_FD, .warn_on_full_buffer = 1}; |
129 | | -#endif |
130 | | - |
131 | | -/* Speed up sigcheck() when none tripped */ |
132 | | -static _Py_atomic_int is_tripped; |
133 | | - |
134 | | -typedef struct { |
135 | | - PyObject *default_handler; |
136 | | - PyObject *ignore_handler; |
137 | | -#ifdef MS_WINDOWS |
138 | | - HANDLE sigint_event; |
139 | | -#endif |
140 | | -} signal_state_t; |
| 104 | +#define Handlers _PyRuntime.signals.handlers |
| 105 | +#define wakeup _PyRuntime.signals.wakeup |
| 106 | +#define is_tripped _PyRuntime.signals.is_tripped |
141 | 107 |
|
142 | 108 | // State shared by all Python interpreters |
143 | | -static signal_state_t signal_global_state = {0}; |
| 109 | +typedef struct _signals_runtime_state signal_state_t; |
| 110 | +#define signal_global_state _PyRuntime.signals |
144 | 111 |
|
145 | 112 | #if defined(HAVE_GETITIMER) || defined(HAVE_SETITIMER) |
146 | 113 | # define PYHAVE_ITIMER_ERROR |
@@ -331,13 +298,7 @@ trip_signal(int sig_num) |
331 | 298 | See bpo-30038 for more details. |
332 | 299 | */ |
333 | 300 |
|
334 | | - int fd; |
335 | | -#ifdef MS_WINDOWS |
336 | | - fd = Py_SAFE_DOWNCAST(wakeup.fd, SOCKET_T, int); |
337 | | -#else |
338 | | - fd = wakeup.fd; |
339 | | -#endif |
340 | | - |
| 301 | + int fd = wakeup.fd; |
341 | 302 | if (fd != INVALID_FD) { |
342 | 303 | unsigned char byte = (unsigned char)sig_num; |
343 | 304 | #ifdef MS_WINDOWS |
@@ -407,7 +368,7 @@ signal_handler(int sig_num) |
407 | 368 | #ifdef MS_WINDOWS |
408 | 369 | if (sig_num == SIGINT) { |
409 | 370 | signal_state_t *state = &signal_global_state; |
410 | | - SetEvent(state->sigint_event); |
| 371 | + SetEvent((HANDLE)state->sigint_event); |
411 | 372 | } |
412 | 373 | #endif |
413 | 374 | } |
@@ -822,7 +783,7 @@ signal_set_wakeup_fd(PyObject *self, PyObject *args, PyObject *kwds) |
822 | 783 | } |
823 | 784 |
|
824 | 785 | old_sockfd = wakeup.fd; |
825 | | - wakeup.fd = sockfd; |
| 786 | + wakeup.fd = Py_SAFE_DOWNCAST(sockfd, SOCKET_T, int); |
826 | 787 | wakeup.warn_on_full_buffer = warn_on_full_buffer; |
827 | 788 | wakeup.use_send = is_socket; |
828 | 789 |
|
@@ -873,11 +834,7 @@ PySignal_SetWakeupFd(int fd) |
873 | 834 | fd = -1; |
874 | 835 | } |
875 | 836 |
|
876 | | -#ifdef MS_WINDOWS |
877 | | - int old_fd = Py_SAFE_DOWNCAST(wakeup.fd, SOCKET_T, int); |
878 | | -#else |
879 | 837 | int old_fd = wakeup.fd; |
880 | | -#endif |
881 | 838 | wakeup.fd = fd; |
882 | 839 | wakeup.warn_on_full_buffer = 1; |
883 | 840 | return old_fd; |
@@ -1654,6 +1611,8 @@ signal_module_exec(PyObject *m) |
1654 | 1611 | signal_state_t *state = &signal_global_state; |
1655 | 1612 | _signal_module_state *modstate = get_signal_state(m); |
1656 | 1613 |
|
| 1614 | + // XXX For proper isolation, these values must be guaranteed |
| 1615 | + // to be effectively const (e.g. immortal). |
1657 | 1616 | modstate->default_handler = state->default_handler; // borrowed ref |
1658 | 1617 | modstate->ignore_handler = state->ignore_handler; // borrowed ref |
1659 | 1618 |
|
@@ -1783,7 +1742,7 @@ _PySignal_Fini(void) |
1783 | 1742 |
|
1784 | 1743 | #ifdef MS_WINDOWS |
1785 | 1744 | if (state->sigint_event != NULL) { |
1786 | | - CloseHandle(state->sigint_event); |
| 1745 | + CloseHandle((HANDLE)state->sigint_event); |
1787 | 1746 | state->sigint_event = NULL; |
1788 | 1747 | } |
1789 | 1748 | #endif |
@@ -2009,7 +1968,7 @@ _PySignal_Init(int install_signal_handlers) |
2009 | 1968 |
|
2010 | 1969 | #ifdef MS_WINDOWS |
2011 | 1970 | /* Create manual-reset event, initially unset */ |
2012 | | - state->sigint_event = CreateEvent(NULL, TRUE, FALSE, FALSE); |
| 1971 | + state->sigint_event = (void *)CreateEvent(NULL, TRUE, FALSE, FALSE); |
2013 | 1972 | if (state->sigint_event == NULL) { |
2014 | 1973 | PyErr_SetFromWindowsErr(0); |
2015 | 1974 | return -1; |
|
0 commit comments