dbus: set the socket as invalid in _dbus_close_socket()

This can simplify error handling in many situation where a socket is
returned, such as in the following commits.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
This commit is contained in:
Marc-André Lureau 2022-02-08 14:57:05 +04:00 committed by Simon McVittie
parent 7d20a3c604
commit 581344c17d
2 changed files with 15 additions and 4 deletions

View file

@ -297,8 +297,12 @@ _dbus_open_unix_socket (int *fd,
}
/**
* Closes a socket. Should not be used on non-socket
* file descriptors or handles.
* Closes a socket and invalidates it. Should not be used on non-socket file
* descriptors or handles.
*
* If an error is detected, this function returns #FALSE and sets the error, but
* the socket is still closed and invalidated. Callers can use the error in a
* diagnostic message, but should not retry closing the socket.
*
* @param fd the socket
* @param error return location for an error
@ -308,10 +312,15 @@ dbus_bool_t
_dbus_close_socket (DBusSocket *fd,
DBusError *error)
{
dbus_bool_t rv;
_dbus_assert (fd != NULL);
_DBUS_ASSERT_ERROR_IS_CLEAR (error);
return _dbus_close (fd->fd, error);
rv = _dbus_close (fd->fd, error);
_dbus_socket_invalidate (fd);
return rv;
}
/**

View file

@ -483,7 +483,7 @@ _dbus_write_socket (DBusSocket fd,
/**
* Closes a file descriptor.
* Closes a socket and invalidates it.
*
* @param fd the file descriptor
* @param error error object
@ -507,10 +507,12 @@ _dbus_close_socket (DBusSocket *fd,
dbus_set_error (error, _dbus_error_from_errno (errno),
"Could not close socket: socket=%Iu, , %s",
fd->sock, _dbus_strerror_from_errno ());
_dbus_socket_invalidate (fd);
return FALSE;
}
_dbus_verbose ("socket=%Iu, \n", fd->sock);
_dbus_socket_invalidate (fd);
return TRUE;
}