Split _dbus_fd_set_close_on_exec into Unix and Windows versions
authorSimon McVittie <simon.mcvittie@collabora.co.uk>
Thu, 11 Sep 2014 11:04:04 +0000 (12:04 +0100)
committerRalf Habacker <ralf.habacker@freenet.de>
Mon, 15 Sep 2014 11:31:22 +0000 (13:31 +0200)
On Unix, the thing that can be made close-on-exec is a file descriptor,
which is an int.

On Windows, the thing that can be made close-on-exec is a HANDLE,
which is pointer-sized (but not necessarily a pointer!). In practice,
on Windows we only called _dbus_fd_set_close_on_exec() on socket
pseudo-file-descriptors (SOCKET, which is an unsigned int);
every SOCKET can validly be cast to HANDLE, but not every HANDLE
is a SOCKET.

Before this commit we used an intptr_t as a sort of fake
union { int; HANDLE; }, which just obscures what's going on.

In practice, everything that called _dbus_fd_set_close_on_exec()
is really platform-specific anyway, so let's just have two separate
functions and call this solved.

Bug: https://bugs.freedesktop.org/show_bug.cgi?id=39610

dbus/dbus-server-launchd.c
dbus/dbus-sysdeps-unix.c
dbus/dbus-sysdeps-unix.h
dbus/dbus-sysdeps-win.c
dbus/dbus-sysdeps.h

index 9832875..d9d5908 100644 (file)
@@ -42,6 +42,7 @@
 
 #include "dbus-misc.h"
 #include "dbus-server-socket.h"
+#include "dbus-sysdeps-unix.h"
 
 /* put other private launchd functions here */
 
index acff89e..a5090d4 100644 (file)
@@ -2919,7 +2919,7 @@ _dbus_disable_sigpipe (void)
  * @param fd the file descriptor
  */
 void
-_dbus_fd_set_close_on_exec (intptr_t fd)
+_dbus_fd_set_close_on_exec (int fd)
 {
   int val;
 
index a265b33..df9902d 100644 (file)
@@ -142,6 +142,8 @@ dbus_bool_t _dbus_append_address_from_socket (int         fd,
                                               DBusString *address,
                                               DBusError  *error);
 
+void _dbus_fd_set_close_on_exec (int fd);
+
 /** @} */
 
 DBUS_END_DECLS
index 1167e96..341db8a 100644 (file)
@@ -518,10 +518,10 @@ _dbus_close_socket (int        fd,
  * on exec. Should be called for all file
  * descriptors in D-Bus code.
  *
- * @param handle the Windows HANDLE
+ * @param handle the Windows HANDLE (a SOCKET is also OK)
  */
-void
-_dbus_fd_set_close_on_exec (intptr_t handle)
+static void
+_dbus_win_handle_set_close_on_exec (HANDLE handle)
 {
   if ( !SetHandleInformation( (HANDLE) handle,
                         HANDLE_FLAG_INHERIT | HANDLE_FLAG_PROTECT_FROM_CLOSE,
@@ -1605,7 +1605,8 @@ _dbus_connect_tcp_socket_with_nonce (const char     *host,
         }
     }
 
-  _dbus_fd_set_close_on_exec (fd);
+  /* Every SOCKET is also a HANDLE. */
+  _dbus_win_handle_set_close_on_exec ((HANDLE) fd);
 
   if (!_dbus_set_fd_nonblocking (fd, error))
     {
@@ -1803,7 +1804,7 @@ _dbus_listen_tcp_socket (const char     *host,
 
   for (i = 0 ; i < nlisten_fd ; i++)
     {
-      _dbus_fd_set_close_on_exec (listen_fd[i]);
+      _dbus_win_handle_set_close_on_exec ((HANDLE) listen_fd[i]);
       if (!_dbus_set_fd_nonblocking (listen_fd[i], error))
         {
           goto failed;
index 21033eb..7d44f93 100644 (file)
@@ -359,8 +359,6 @@ void         _dbus_directory_close         (DBusDirIter      *iter);
 dbus_bool_t  _dbus_check_dir_is_private_to_user    (DBusString *dir,
                                                     DBusError *error);
 
-void _dbus_fd_set_close_on_exec (intptr_t fd);
-
 const char* _dbus_get_tmpdir      (void);
 
 /**