Skip to content

Commit

Permalink
[Inet] Fix setting for CLOEXEC flag at socket creation (#33164)
Browse files Browse the repository at this point in the history
* [Inet] Fix setting for SOCK_CLOEXEC

The linux kernel allows to set the flag SOCK_CLOEXEC through the
parameter type[1].

The implementation sets the flag through the parameter protocol and
causes the call to socket()[2] to return -1 with EINVAL.

This fixes the behaviour by OR-ing the flag SOCK_CLOEXEC to type instead
of protocol.

[1]: https://github.com/torvalds/linux/blob/v6.8/net/socket.c#L1655
[2]: https://man7.org/linux/man-pages/man2/socket.2.html

* [Inet] Fix setting for O_CLOEXEC

The macro O_CLOEXEC is a filedescriptor flag.

The implemention sets the flag O_CLOEXEC as the parameter command given
to function fcntl[1] and causes the call to fcntl to return -1 with
EINVAL.

The flag O_CLOEXEC intends to be set using the command F_SETFD.

This fixes the behaviour by adding the missing command paramete F_SETFD
before the optional argument O_CLOEXEC.

[1]: https://man7.org/linux/man-pages/man2/fcntl.2.html
  • Loading branch information
gportay authored Apr 25, 2024
1 parent be15c9b commit b68f1b1
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/inet/InetInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -500,12 +500,12 @@ int GetIOCTLSocket()
{
int s;
#ifdef SOCK_CLOEXEC
s = socket(AF_INET, SOCK_STREAM, SOCK_CLOEXEC);
s = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0);
if (s < 0)
#endif
{
s = socket(AF_INET, SOCK_STREAM, 0);
fcntl(s, O_CLOEXEC);
fcntl(s, F_SETFD, O_CLOEXEC);
}

if (!__sync_bool_compare_and_swap(&sIOCTLSocket, -1, s))
Expand Down

0 comments on commit b68f1b1

Please sign in to comment.