]> git.ipfire.org Git - thirdparty/dbus.git/commitdiff
dbus: set the socket as invalid in _dbus_close_socket()
authorMarc-André Lureau <marcandre.lureau@redhat.com>
Tue, 8 Feb 2022 10:57:05 +0000 (14:57 +0400)
committerSimon McVittie <smcv@collabora.com>
Fri, 15 Jul 2022 15:26:18 +0000 (16:26 +0100)
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>
dbus/dbus-sysdeps-unix.c
dbus/dbus-sysdeps-win.c

index 152a3862fb9e4dbd71aefe143ed2295286f840da..c77adc09795f6f6cb5b0869b29ac98410ffce9f5 100644 (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;
 }
 
 /**
index 445662148dc910a636c2f7c00c22159335adab15..d15b6991e4a418a13d24f23dda8cda5015957ef4 100644 (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;
 }