gsocket: Use accept4 () for race-free setting of the close-on-exec flag

The code was already setting the close-on-exec flag for the new socket,
just in a racy way.
This commit is contained in:
Maciej S. Szmigiero 2023-02-19 14:19:37 +01:00
parent 1c305c4fb0
commit 3dc77fef24
2 changed files with 26 additions and 1 deletions

View File

@ -2858,6 +2858,9 @@ g_socket_accept (GSocket *socket,
GCancellable *cancellable,
GError **error)
{
#ifdef HAVE_ACCEPT4
gboolean try_accept4 = TRUE;
#endif
GSocket *new_socket;
gint ret;
@ -2871,7 +2874,28 @@ g_socket_accept (GSocket *socket,
while (TRUE)
{
if ((ret = accept (socket->priv->fd, NULL, 0)) < 0)
gboolean try_accept = TRUE;
#ifdef HAVE_ACCEPT4
if (try_accept4)
{
ret = accept4 (socket->priv->fd, NULL, 0, SOCK_CLOEXEC);
if (ret < 0 && errno == ENOSYS)
{
try_accept4 = FALSE;
}
else
{
try_accept = FALSE;
}
}
g_assert (try_accept4 || try_accept);
#endif
if (try_accept)
ret = accept (socket->priv->fd, NULL, 0);
if (ret < 0)
{
int errsv = get_socket_errno ();

View File

@ -606,6 +606,7 @@ if host_system == 'windows'
endif
functions = [
'accept4',
'close_range',
'endmntent',
'endservent',