10000 gh-103092: Isolate socket by erlend-aasland · Pull Request #103094 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-103092: Isolate socket #103094

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 24 commits into from
Apr 9, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
4a7c6d9
Establish global state
erlend-aasland Mar 11, 2023
220a0aa
Add sock_type to state
erlend-aasland Mar 12, 2023
94ca00c
Add exceptions to state
erlend-aasland Mar 12, 2023
fc9cdce
Add accept4_works to state
erlend-aasland Mar 12, 2023
29b4d1c
Add sock_cloexec_works to state
erlend-aasland Mar 12, 2023
93a2b62
Add defaulttimeout to state
erlend-aasland Mar 12, 2023
4164f96
Refactor: replace state query with param in set_herror()
erlend-aasland Mar 12, 2023
dc740d1
Refactor: replace state query with param in set_gaierror()
erlend-aasland Mar 12, 2023
abf00c3
Refactor: replace state query with param in new_sockobject()
erlend-aasland Mar 12, 2023
9c1c9c6
Refactor: replace state query with param in init_sockobject()
erlend-aasland Mar 12, 2023
82c00c4
Refactor: replace state query with param in gethost_common()
erlend-aasland Mar 12, 2023
72e69e3
Refactor: replace state query with param in sock_get_api()
erlend-aasland Mar 12, 2023
274c412
Refactor: replace state query with param in setipaddr()
erlend-aasland Mar 12, 2023
d839af0
Add state pointer to PySocketSockObject
erlend-aasland Mar 12, 2023
13d14da
Use sock pointer from sock type context where applicable
erlend-aasland Mar 12, 2023
b4edf64
Multi-phase init
erlend-aasland Mar 12, 2023
c51a696
Remove unneeded forward decl
erlend-aasland Mar 30, 2023
d8e448f
Add test
erlend-aasland Mar 30, 2023
8000 4332571
fixup dealloc
erlend-aasland Mar 30, 2023
6a00c54
Add NEWS
erlend-aasland Apr 3, 2023
e5027c2
Style: remove unneeded parens
erlend-aasland Apr 4, 2023
98d1566
Pull in main
erlend-aasland Apr 4, 2023
e5827e7
Sync with main
erlend-aasland Apr 8, 2023
2019a93
Simplify ADD_EXC macro further
erlend-aasland Apr 8, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add sock_cloexec_works to state
  • Loading branch information
erlend-aasland committed Mar 28, 2023
commit 29b4d1c20a50a79c6680cd6e01a6fcf55580aaa6
40 changes: 24 additions & 16 deletions Modules/socketmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,12 @@ typedef struct {
int accept4_works;
#endif
#endif

#ifdef SOCK_CLOEXEC
/* socket() and socketpair() fail with EINVAL on Linux kernel older
* than 2.6.27 if SOCK_CLOEXEC flag is set in the socket type. */
int sock_cloexec_works;
#endif
} socket_state;

static socket_state global_state;
Expand Down Expand Up @@ -5301,12 +5307,6 @@ sock_new(PyTypeObject *type, PyObject *args, PyObject *kwds)

/* Initialize a new socket object. */

#ifdef SOCK_CLOEXEC
/* socket() and socketpair() fail with EINVAL on Linux kernel older
* than 2.6.27 if SOCK_CLOEXEC flag is set in the socket type. */
static int sock_cloexec_works = -1;
#endif

/*ARGSUSED*/

#ifndef HAVE_SOCKET
Expand Down Expand Up @@ -5337,7 +5337,8 @@ sock_initobj_impl(PySocketSockObject *self, int family, int type, int proto,

#ifndef MS_WINDOWS
#ifdef SOCK_CLOEXEC
int *atomic_flag_works = &sock_cloexec_works;
socket_state *state = GLOBAL_STATE();
int *atomic_flag_works = &(state->sock_cloexec_works);
#else
int *atomic_flag_works = NULL;
#endif
Expand Down Expand Up @@ -5492,15 +5493,16 @@ sock_initobj_impl(PySocketSockObject *self, int family, int type, int proto,
/* UNIX */
Py_BEGIN_ALLOW_THREADS
#ifdef SOCK_CLOEXEC
if (sock_cloexec_works != 0) {
socket_state *state = GLOBAL_STATE();
if (state->sock_cloexec_works != 0) {
fd = socket(family, type | SOCK_CLOEXEC, proto);
if (sock_cloexec_works == -1) {
if (state->sock_cloexec_works == -1) {
if (fd >= 0) {
sock_cloexec_works = 1;
state->sock_cloexec_works = 1;
}
else if (errno == EINVAL) {
/* Linux older than 2.6.27 does not support SOCK_CLOEXEC */
sock_cloexec_works = 0;
state->sock_cloexec_works = 0;
fd = socket(family, type, proto);
}
}
Expand Down Expand Up @@ -6217,7 +6219,8 @@ socket_socketpair(PyObject *self, PyObject *args)
int family, type = SOCK_STREAM, proto = 0;
PyObject *res = NULL;
#ifdef SOCK_CLOEXEC
int *atomic_flag_works = &sock_cloexec_works;
socket_state *state = GLOBAL_STATE();
int *atomic_flag_works = &(state->sock_cloexec_works);
#else
int *atomic_flag_works = NULL;
#endif
Expand All @@ -6235,15 +6238,16 @@ socket_socketpair(PyObject *self, PyObject *args)
/* Create a pair of socket fds */
Py_BEGIN_ALLOW_THREADS
#ifdef SOCK_CLOEXEC
if (sock_cloexec_works != 0) {
socket_state *state = GLOBAL_STATE();
if (state->sock_cloexec_works != 0) {
ret = socketpair(family, type | SOCK_CLOEXEC, proto, sv);
if (sock_cloexec_works == -1) {
if (state->sock_cloexec_works == -1) {
if (ret >= 0) {
sock_cloexec_works = 1;
state->sock_cloexec_works = 1;
}
else if (errno == EINVAL) {
/* Linux older than 2.6.27 does not support SOCK_CLOEXEC */
sock_cloexec_works = 0;
state->sock_cloexec_works = 0;
ret = socketpair(family, type, proto, sv);
}
}
Expand Down Expand Up @@ -7346,6 +7350,10 @@ PyInit__socket(void)
#endif
#endif

#ifdef SOCK_CLOEXEC
state->sock_cloexec_works = -1;
#endif

// Create exceptions and types, and add them to the module state.
state->socket_herror = PyErr_NewException("socket.herror",
PyExc_OSError, NULL);
Expand Down
1 change: 0 additions & 1 deletion Tools/c-analyzer/cpython/globals-to-fix.tsv
7579
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,6 @@ Modules/cjkcodecs/cjkcodecs.h - mapping_list -
Modules/readline.c - libedit_append_replace_history_offset -
Modules/readline.c - using_libedit_emulation -
Modules/readline.c - libedit_history_start -
Modules/socketmodule.c - sock_cloexec_works -

##-----------------------
## state
Expand Down
0