[daemon-fix] Fixed sending daemon match rules for kdbus broadcasts
[platform/upstream/dbus.git] / dbus / dbus-transport-unix.c
index 869aa33..9a9fea5 100644 (file)
@@ -1,9 +1,9 @@
-/* -*- mode: C; c-file-style: "gnu" -*- */
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
 /* dbus-transport-unix.c UNIX socket subclasses of DBusTransport
  *
- * Copyright (C) 2002  Red Hat Inc.
+ * Copyright (C) 2002, 2003, 2004  Red Hat Inc.
  *
- * Licensed under the Academic Free License version 1.2
+ * Licensed under the Academic Free License version 2.1
  * 
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * 
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  *
  */
 
+#include <config.h>
+
+#include <stdio.h>
+
 #include "dbus-internals.h"
 #include "dbus-connection-internal.h"
 #include "dbus-transport-unix.h"
+#include "dbus-transport-socket.h"
 #include "dbus-transport-protected.h"
 #include "dbus-watch.h"
-#include <sys/types.h>
-#include <unistd.h>
-#include <sys/socket.h>
-#include <sys/un.h>
-#include <errno.h>
-#include <fcntl.h>
-#ifdef HAVE_WRITEV
-#include <sys/uio.h>
-#endif
+#include "dbus-sysdeps-unix.h"
+#include "dbus-test.h"
 
 /**
  * @defgroup DBusTransportUnix DBusTransport implementations for UNIX
  */
 
 /**
- * Opaque object representing a Unix file descriptor transport.
- */
-typedef struct DBusTransportUnix DBusTransportUnix;
-
-/**
- * Implementation details of DBusTransportUnix. All members are private.
+ * Creates a new transport for the given Unix domain socket
+ * path. This creates a client-side of a transport.
+ *
+ * @todo once we add a way to escape paths in a dbus
+ * address, this function needs to do escaping.
+ *
+ * @param path the path to the domain socket.
+ * @param abstract #TRUE to use abstract socket namespace
+ * @param error address where an error can be returned.
+ * @returns a new transport, or #NULL on failure.
  */
-struct DBusTransportUnix
-{
-  DBusTransport base;                   /**< Parent instance */
-  int fd;                               /**< File descriptor. */
-  DBusWatch *watch;                     /**< Watch for readability. */
-  DBusWatch *write_watch;               /**< Watch for writability. */
-
-  int max_bytes_read_per_iteration;     /**< To avoid blocking too long. */
-  int max_bytes_written_per_iteration;  /**< To avoid blocking too long. */
-
-  int message_bytes_written;            /**< Number of bytes of current
-                                         *   outgoing message that have
-                                         *   been written.
-                                         */
-};
-
-static void
-unix_finalize (DBusTransport *transport)
+DBusTransport*
+_dbus_transport_new_for_domain_socket (const char     *path,
+                                       dbus_bool_t     abstract,
+                                       DBusError      *error)
 {
-  DBusTransportUnix *unix_transport = (DBusTransportUnix*) transport;
+  int fd;
+  DBusTransport *transport;
+  DBusString address;
   
-  _dbus_transport_finalize_base (transport);
+  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
 
-  if (unix_transport->watch)
+  if (!_dbus_string_init (&address))
     {
-      _dbus_watch_invalidate (unix_transport->watch);
-      _dbus_watch_unref (unix_transport->watch);
+      dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
+      return NULL;
     }
-  
-  dbus_free (transport);
-}
 
-static void
-do_io_error (DBusTransport *transport)
-{
-  _dbus_transport_disconnect (transport);
-  _dbus_connection_transport_error (transport->connection,
-                                    DBUS_RESULT_DISCONNECTED);
-}
+  fd = -1;
 
-static void
-do_writing (DBusTransport *transport)
-{
-  int total;
-  DBusTransportUnix *unix_transport = (DBusTransportUnix*) transport;
-  
-  total = 0;
+  if ((abstract &&
+       !_dbus_string_append (&address, "unix:abstract=")) ||
+      (!abstract &&
+       !_dbus_string_append (&address, "unix:path=")) ||
+      !_dbus_string_append (&address, path))
+    {
+      dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
+      goto failed_0;
+    }
   
- again:
-
-  while (_dbus_connection_have_messages_to_send (transport->connection))
+  fd = _dbus_connect_unix_socket (path, abstract, error);
+  if (fd < 0)
     {
-      int bytes_written;
-      DBusMessage *message;
-      const unsigned char *header;
-      const unsigned char *body;
-      int header_len, body_len;
-      
-      if (total > unix_transport->max_bytes_written_per_iteration)
-        {
-          _dbus_verbose ("%d bytes exceeds %d bytes written per iteration, returning\n",
-                         total, unix_transport->max_bytes_written_per_iteration);
-          goto out;
-        }
-      
-      message = _dbus_connection_get_message_to_send (transport->connection);
-      _dbus_assert (message != NULL);
-      _dbus_message_lock (message);
-
-      _dbus_message_get_network_data (message,
-                                      &header, &header_len,
-                                      &body, &body_len);
-
-      if (unix_transport->message_bytes_written < header_len)
-        {
-#ifdef HAVE_WRITEV
-          struct iovec vectors[2];
+      _DBUS_ASSERT_ERROR_IS_SET (error);
+      goto failed_0;
+    }
 
-          vectors[0].iov_base = header + unix_transport->message_bytes_written;
-          vectors[0].iov_len = header_len - unix_transport->message_bytes_written;
-          vectors[1].iov_base = body;
-          vectors[1].iov_len = body_len;
-          
-          bytes_written = writev (unix_transport->fd,
-                                  vectors, _DBUS_N_ELEMENTS (vectors));
-#else
-          bytes_written = write (unix_transport->fd,
-                                 header + unix_transport->message_bytes_written, 
-                                 header_len - unix_transport->message_bytes_written);
-#endif
-        }
-      else
-        {
-          bytes_written = write (unix_transport->fd,
-                                 body +
-                                 (unix_transport->message_bytes_written - header_len), 
-                                 body_len -
-                                 (unix_transport->message_bytes_written - body_len));
-        }
+  _dbus_verbose ("Successfully connected to unix socket %s\n",
+                 path);
 
-      if (bytes_written < 0)
-        {
-          if (errno == EINTR)
-            goto again;
-          else if (errno == EAGAIN ||
-                   errno == EWOULDBLOCK)
-            goto out;
-          else
-            {
-              _dbus_verbose ("Error writing to message bus: %s\n",
-                             _dbus_strerror (errno));
-              do_io_error (transport);
-              goto out;
-            }
-        }
-      else
-        {          
-          _dbus_verbose (" wrote %d bytes\n", bytes_written);
-          
-          total += bytes_written;
-          unix_transport->message_bytes_written += bytes_written;
-
-          _dbus_assert (unix_transport->message_bytes_written <=
-                        (header_len + body_len));
-          
-          if (unix_transport->message_bytes_written == (header_len + body_len))
-            {
-              _dbus_connection_message_sent (transport->connection,
-                                             message);
-              unix_transport->message_bytes_written = 0;
-            }
-        }
+  transport = _dbus_transport_new_for_socket (fd, NULL, &address);
+  if (transport == NULL)
+    {
+      dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
+      goto failed_1;
     }
+  
+  _dbus_string_free (&address);
+  
+  return transport;
 
- out:
-  return; /* I think some C compilers require a statement after a label */
+ failed_1:
+  _dbus_close_socket (fd, NULL);
+ failed_0:
+  _dbus_string_free (&address);
+  return NULL;
 }
 
-static void
-do_reading (DBusTransport *transport)
+/**
+ * Creates a new transport for the given binary and arguments. This
+ * creates a client-side of a transport. The process will be forked
+ * off and executed with stdin/stdout connected to a local AF_UNIX
+ * socket.
+ *
+ * @param path the path to the domain socket.
+ * @param argv Parameters list
+ * @param error address where an error can be returned.
+ * @returns a new transport, or #NULL on failure.
+ */
+static DBusTransport*
+_dbus_transport_new_for_exec (const char     *path,
+                              char *const     argv[],
+                              DBusError      *error)
 {
-  DBusTransportUnix *unix_transport = (DBusTransportUnix*) transport;
-  unsigned char *buffer;
-  int buffer_len;
-  int bytes_read;
-  int total;
-  
-  total = 0;
-  
- again:
+  int fd;
+  DBusTransport *transport;
+  DBusString address;
+  unsigned i;
+  char *escaped;
+
+  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
 
-  if (total > unix_transport->max_bytes_read_per_iteration)
+  if (!_dbus_string_init (&address))
     {
-      _dbus_verbose ("%d bytes exceeds %d bytes read per iteration, returning\n",
-                     total, unix_transport->max_bytes_read_per_iteration);
-      goto out;
+      dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
+      return NULL;
     }
 
-  if (!_dbus_message_loader_get_buffer (transport->loader,
-                                        &buffer, &buffer_len))
-    goto out; /* no memory for a buffer */
-  
-  bytes_read = read (unix_transport->fd,
-                     buffer, buffer_len);
+  fd = -1;
 
-  _dbus_message_loader_return_buffer (transport->loader,
-                                      buffer,
-                                      bytes_read < 0 ? 0 : bytes_read);
-  
-  if (bytes_read < 0)
-    {      
-      if (errno == EINTR)
-        goto again;
-      else if (errno == EAGAIN ||
-               errno == EWOULDBLOCK)
-        goto out;
-      else
-        {
-          _dbus_verbose ("Error reading from message bus: %s\n",
-                         _dbus_strerror (errno));
-          do_io_error (transport);
-          goto out;
-        }
+  escaped = dbus_address_escape_value (path);
+  if (!escaped)
+    {
+      dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
+      goto failed;
     }
-  else if (bytes_read == 0)
+
+  if (!_dbus_string_append (&address, "unixexec:path=") ||
+      !_dbus_string_append (&address, escaped))
     {
-      _dbus_verbose ("Disconnected from message bus\n");
-      do_io_error (transport);
-      goto out;
+      dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
+      dbus_free (escaped);
+      goto failed;
     }
-  else
+
+  dbus_free (escaped);
+
+  if (argv)
     {
-      DBusMessage *message;
-      
-      _dbus_verbose (" read %d bytes\n", bytes_read);
-      
-      total += bytes_read;      
-
-      /* Queue any messages */
-      while ((message = _dbus_message_loader_pop_message (transport->loader)))
+      for (i = 0; argv[i]; i++)
         {
-          _dbus_verbose ("queueing received message %p\n", message);
-          
-          _dbus_connection_queue_received_message (transport->connection,
-                                                   message);
-          dbus_message_unref (message);
-        }
-      
-      /* Try reading more data until we get EAGAIN and return, or
-       * exceed max bytes per iteration.  If in blocking mode of
-       * course we'll block instead of returning.
-       */
-      goto again;
-    }
+          dbus_bool_t success;
 
- out:
-  return; /* I think some C compilers require a statement after a label */
-}
+          escaped = dbus_address_escape_value (argv[i]);
+          if (!escaped)
+            {
+              dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
+              goto failed;
+            }
 
-static void
-unix_handle_watch (DBusTransport *transport,
-                   DBusWatch     *watch,
-                   unsigned int   flags)
-{
-  DBusTransportUnix *unix_transport = (DBusTransportUnix*) transport;
+          success = _dbus_string_append_printf (&address, ",argv%u=%s", i, escaped);
+          dbus_free (escaped);
 
-  _dbus_assert (watch == unix_transport->watch ||
-                watch == unix_transport->write_watch);
+          if (!success)
+            {
+              dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
+              goto failed;
+            }
+        }
+    }
 
-  if (flags & (DBUS_WATCH_HANGUP | DBUS_WATCH_ERROR))
+  fd = _dbus_connect_exec (path, argv, error);
+  if (fd < 0)
     {
-      _dbus_transport_disconnect (transport);
-      _dbus_connection_transport_error (transport->connection,
-                                        DBUS_RESULT_DISCONNECTED);
-      return;
+      _DBUS_ASSERT_ERROR_IS_SET (error);
+      goto failed;
     }
-  
-  if (watch == unix_transport->watch &&
-      (flags & DBUS_WATCH_READABLE))
-    do_reading (transport);
-  else if (watch == unix_transport->write_watch &&
-           (flags & DBUS_WATCH_WRITABLE))
-    do_writing (transport); 
-}
 
-static void
-unix_disconnect (DBusTransport *transport)
-{
-  DBusTransportUnix *unix_transport = (DBusTransportUnix*) transport;
+  _dbus_verbose ("Successfully connected to process %s\n",
+                 path);
 
-  if (unix_transport->watch)
+  transport = _dbus_transport_new_for_socket (fd, NULL, &address);
+  if (transport == NULL)
     {
-      _dbus_connection_remove_watch (transport->connection,
-                                     unix_transport->watch);
-      _dbus_watch_invalidate (unix_transport->watch);
-      _dbus_watch_unref (unix_transport->watch);
-      unix_transport->watch = NULL;
+      dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
+      goto failed;
     }
-  
-  close (unix_transport->fd);
-  unix_transport->fd = -1;
-}
 
-static void
-unix_connection_set (DBusTransport *transport)
-{
-  DBusTransportUnix *unix_transport = (DBusTransportUnix*) transport;
-  DBusWatch *watch;
+  _dbus_string_free (&address);
 
-  _dbus_assert (unix_transport->watch == NULL);
-  
-  watch = _dbus_watch_new (unix_transport->fd,
-                           DBUS_WATCH_READABLE);
-  
-  if (watch == NULL)
-    {
-      _dbus_transport_disconnect (transport);
-      return;
-    }
-  
-  if (!_dbus_connection_add_watch (transport->connection,
-                                   watch))
-    {
-      _dbus_transport_disconnect (transport);
-      return;
-    }
+  return transport;
+
+ failed:
+  if (fd >= 0)
+    _dbus_close_socket (fd, NULL);
 
-  unix_transport->watch = watch;
+  _dbus_string_free (&address);
+  return NULL;
 }
 
-static void
-unix_messages_pending (DBusTransport *transport,
-                       int            messages_pending)
+/**
+ * Opens platform specific transport types.
+ * 
+ * @param entry the address entry to try opening
+ * @param transport_p return location for the opened transport
+ * @param error error to be set
+ * @returns result of the attempt
+ */
+DBusTransportOpenResult
+_dbus_transport_open_platform_specific (DBusAddressEntry  *entry,
+                                        DBusTransport    **transport_p,
+                                        DBusError         *error)
 {
-  DBusTransportUnix *unix_transport = (DBusTransportUnix*) transport;
+  const char *method;
+  
+  method = dbus_address_entry_get_method (entry);
+  _dbus_assert (method != NULL);
 
-  if (messages_pending > 0 &&
-      unix_transport->write_watch == NULL)
+  if (strcmp (method, "unix") == 0)
     {
-      unix_transport->write_watch =
-        _dbus_watch_new (unix_transport->fd,
-                         DBUS_WATCH_WRITABLE);
+      const char *path = dbus_address_entry_get_value (entry, "path");
+      const char *tmpdir = dbus_address_entry_get_value (entry, "tmpdir");
+      const char *abstract = dbus_address_entry_get_value (entry, "abstract");
+          
+      if (tmpdir != NULL)
+        {
+          _dbus_set_bad_address (error, NULL, NULL,
+                                 "cannot use the \"tmpdir\" option for an address to connect to, only in an address to listen on");
+          return DBUS_TRANSPORT_OPEN_BAD_ADDRESS;
+        }
+          
+      if (path == NULL && abstract == NULL)
+        {
+          _dbus_set_bad_address (error, "unix",
+                                 "path or abstract",
+                                 NULL);
+          return DBUS_TRANSPORT_OPEN_BAD_ADDRESS;
+        }
 
-      /* we can maybe add it some other time, just silently bomb */
-      if (unix_transport->write_watch == NULL)
-        return;
+      if (path != NULL && abstract != NULL)
+        {
+          _dbus_set_bad_address (error, NULL, NULL,
+                                 "can't specify both \"path\" and \"abstract\" options in an address");
+          return DBUS_TRANSPORT_OPEN_BAD_ADDRESS;
+        }
 
-      if (!_dbus_connection_add_watch (transport->connection,
-                                       unix_transport->write_watch))
+      if (path)
+        *transport_p = _dbus_transport_new_for_domain_socket (path, FALSE,
+                                                           error);
+      else
+        *transport_p = _dbus_transport_new_for_domain_socket (abstract, TRUE,
+                                                           error);
+      if (*transport_p == NULL)
         {
-          _dbus_watch_invalidate (unix_transport->write_watch);
-          _dbus_watch_unref (unix_transport->write_watch);
-          unix_transport->write_watch = NULL;
+          _DBUS_ASSERT_ERROR_IS_SET (error);
+          return DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT;
+        }
+      else
+        {
+          _DBUS_ASSERT_ERROR_IS_CLEAR (error);
+          return DBUS_TRANSPORT_OPEN_OK;
         }
     }
-  else if (messages_pending == 0 &&
-           unix_transport->write_watch != NULL)
+  else if (strcmp (method, "unixexec") == 0)
     {
-      _dbus_connection_remove_watch (transport->connection,
-                                     unix_transport->write_watch);
-      _dbus_watch_invalidate (unix_transport->write_watch);
-      _dbus_watch_unref (unix_transport->write_watch);
-      unix_transport->write_watch = NULL;
-    }
-}
+      const char *path;
+      unsigned i;
+      char **argv;
 
-static  void
-unix_do_iteration (DBusTransport *transport,
-                   unsigned int   flags,
-                   int            timeout_milliseconds)
-{
-  DBusTransportUnix *unix_transport = (DBusTransportUnix*) transport;
-  fd_set read_set;
-  fd_set write_set;
-  dbus_bool_t do_select;
-  
-  do_select = FALSE;
-  
-  FD_ZERO (&read_set);
-  if (flags & DBUS_ITERATION_DO_READING)
-    {
-      FD_SET (unix_transport->fd, &read_set);
-      do_select = TRUE;
-    }
-  
-  FD_ZERO (&write_set);
-  if (flags & DBUS_ITERATION_DO_WRITING)
-    {
-      FD_SET (unix_transport->fd, &write_set);
-      do_select = TRUE;
-    }
+      path = dbus_address_entry_get_value (entry, "path");
+      if (path == NULL)
+        {
+          _dbus_set_bad_address (error, NULL, NULL,
+                                 "No process path specified");
+          return DBUS_TRANSPORT_OPEN_BAD_ADDRESS;
+        }
 
-  if (do_select)
-    {
-      fd_set err_set;
-      struct timeval timeout;
-      dbus_bool_t use_timeout;
-
-    again:
-      
-      FD_ZERO (&err_set);
-      FD_SET (unix_transport->fd, &err_set);
-  
-      if (flags & DBUS_ITERATION_BLOCK)
+      /* First count argv arguments */
+      for (i = 1; ; i++)
         {
-          if (timeout_milliseconds >= 0)
+          char t[4+20+1]; /* "argv" plus space for a formatted base 10 64bit integer, plus NUL */
+
+          snprintf (t, sizeof(t), "argv%u", i);
+
+          if (!dbus_address_entry_get_value (entry, t))
+            break;
+        }
+
+      /* Allocate string array */
+      argv = dbus_new0 (char*, i+1);
+      if (!argv)
+        {
+          dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
+          return DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT;
+        }
+
+      /* Fill in string array */
+      for (i = 0; ; i++)
+        {
+          char t[4+20+1];
+          const char *p;
+
+          snprintf (t, sizeof(t), "argv%u", i);
+
+          p = dbus_address_entry_get_value (entry, t);
+          if (!p)
             {
-              timeout.tv_sec = timeout_milliseconds / 1000;
-              timeout.tv_usec = (timeout_milliseconds % 1000) * 1000;
-              
-              /* Always use timeout if one is passed in. */
-              use_timeout = TRUE;
+              if (i == 0)
+                /* If argv0 isn't specified, fill in the path instead */
+                p = path;
+              else
+                break;
             }
-          else
+
+          argv[i] = _dbus_strdup (p);
+          if (!argv[i])
             {
-              use_timeout = FALSE; /* NULL timeout to block forever */
+              dbus_free_string_array (argv);
+              dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
+              return DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT;
             }
         }
+
+      *transport_p = _dbus_transport_new_for_exec (path, argv, error);
+      dbus_free_string_array (argv);
+
+      if (*transport_p == NULL)
+        {
+          _DBUS_ASSERT_ERROR_IS_SET (error);
+          return DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT;
+        }
       else
         {
-          /* 0 timeout to not block */
-          timeout.tv_sec = 0;
-          timeout.tv_usec = 0;
-          use_timeout = TRUE;
+          _DBUS_ASSERT_ERROR_IS_CLEAR (error);
+          return DBUS_TRANSPORT_OPEN_OK;
         }
-      
-      if (select (unix_transport->fd + 1, &read_set, &write_set, &err_set,
-                  use_timeout ? &timeout : NULL) >= 0)
+    }
+#ifdef DBUS_ENABLE_LAUNCHD
+  else if (strcmp (method, "launchd") == 0)
+    {
+      DBusError tmp_error = DBUS_ERROR_INIT;
+      const char *launchd_env_var = dbus_address_entry_get_value (entry, "env");
+      const char *launchd_socket;
+      DBusString socket_path;
+      dbus_bool_t valid_socket;
+
+      if (!_dbus_string_init (&socket_path))
         {
-          if (FD_ISSET (unix_transport->fd, &err_set))
-            do_io_error (transport);
-          else
-            {
-              if (FD_ISSET (unix_transport->fd, &read_set))
-                do_reading (transport);
-              if (FD_ISSET (unix_transport->fd, &write_set))
-                do_writing (transport);
-            }
+          _DBUS_SET_OOM (error);
+          return FALSE;
         }
-      else if (errno == EINTR)
-        goto again;
-      else
+
+      if (launchd_env_var == NULL)
         {
-          _dbus_verbose ("Error from select(): %s\n",
-                         _dbus_strerror (errno));
+          _dbus_set_bad_address (error, "launchd", "env", NULL);
+          return DBUS_TRANSPORT_OPEN_BAD_ADDRESS;
         }
-    }
-}
 
-static DBusTransportVTable unix_vtable = {
-  unix_finalize,
-  unix_handle_watch,
-  unix_disconnect,
-  unix_connection_set,
-  unix_messages_pending,
-  unix_do_iteration
-};
+      valid_socket = _dbus_lookup_launchd_socket (&socket_path, launchd_env_var, error);
 
-/**
- * Creates a new transport for the given file descriptor.  The file
- * descriptor must be nonblocking (use _dbus_set_fd_nonblocking() to
- * make it so). This function is shared by various transports that
- * boil down to a full duplex file descriptor.
- *
- * @param fd the file descriptor.
- * @returns the new transport, or #NULL if no memory.
- */
-DBusTransport*
-_dbus_transport_new_for_fd (int fd)
-{
-  DBusTransportUnix *unix_transport;
-  
-  unix_transport = dbus_new0 (DBusTransportUnix, 1);
-  if (unix_transport == NULL)
-    return NULL;
+      if (dbus_error_is_set(error))
+        {
+          _dbus_string_free(&socket_path);
+          return DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT;
+        }
+
+      if (!valid_socket)
+        {
+          dbus_set_error(&tmp_error, DBUS_ERROR_BAD_ADDRESS,
+                         "launchd's env var %s does not exist", launchd_env_var);
+          dbus_error_free(error);
+          dbus_move_error(&tmp_error, error);
+          return DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT;
+        }
+
+      launchd_socket = _dbus_string_get_const_data(&socket_path);
+      *transport_p = _dbus_transport_new_for_domain_socket (launchd_socket, FALSE, error);
 
-  if (!_dbus_transport_init_base (&unix_transport->base,
-                                  &unix_vtable))
+      if (*transport_p == NULL)
+        {
+          _DBUS_ASSERT_ERROR_IS_SET (error);
+          return DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT;
+        }
+      else
+        {
+          _DBUS_ASSERT_ERROR_IS_CLEAR (error);
+          return DBUS_TRANSPORT_OPEN_OK;
+        }
+    }
+#endif
+  else
     {
-      dbus_free (unix_transport);
-      return NULL;
+      _DBUS_ASSERT_ERROR_IS_CLEAR (error);
+      return DBUS_TRANSPORT_OPEN_NOT_HANDLED;
     }
-  
-  unix_transport->fd = fd;
-  unix_transport->message_bytes_written = 0;
-  
-  /* These values should probably be tunable or something. */     
-  unix_transport->max_bytes_read_per_iteration = 2048;
-  unix_transport->max_bytes_written_per_iteration = 2048;
-  
-  return (DBusTransport*) unix_transport;
 }
 
-/**
- * Creates a new transport for the given Unix domain socket
- * path.
- *
- * @param path the path to the domain socket.
- * @param result location to store reason for failure.
- * @returns a new transport, or #NULL on failure.
- */
-DBusTransport*
-_dbus_transport_new_for_domain_socket (const char     *path,
-                                       DBusResultCode *result)
+/** @} */
+
+#ifdef DBUS_ENABLE_EMBEDDED_TESTS
+
+dbus_bool_t
+_dbus_transport_unix_test (void)
 {
-  int fd;
-  DBusTransport *transport;
-  struct sockaddr_un addr;
-  
-  transport = NULL;
-  
-  fd = socket (AF_LOCAL, SOCK_STREAM, 0);
+  DBusConnection *c;
+  DBusError error;
+  dbus_bool_t ret;
+  const char *address;
 
-  if (fd < 0)
-    {
-      dbus_set_result (result,
-                       _dbus_result_from_errno (errno));
-      
-      _dbus_verbose ("Failed to create socket: %s\n",
-                     _dbus_strerror (errno)); 
-      
-      goto out;
-    }
+  dbus_error_init (&error);
 
-  _DBUS_ZERO (addr);
-  addr.sun_family = AF_LOCAL;
-  strncpy (addr.sun_path, path, _DBUS_MAX_SUN_PATH_LENGTH);
-  addr.sun_path[_DBUS_MAX_SUN_PATH_LENGTH] = '\0';
-  
-  if (connect (fd, (struct sockaddr*) &addr, sizeof (addr)) < 0)
-    {      
-      dbus_set_result (result,
-                       _dbus_result_from_errno (errno));
-
-      _dbus_verbose ("Failed to connect to socket %s: %s\n",
-                     path, _dbus_strerror (errno));
-
-      close (fd);
-      fd = -1;
-      
-      goto out;
-    }
+  c = dbus_connection_open ("unixexec:argv0=false,argv1=foobar,path=/bin/false", &error);
+  _dbus_assert (c != NULL);
+  _dbus_assert (!dbus_error_is_set (&error));
 
-  if (!_dbus_set_fd_nonblocking (fd, result))
-    {
-      close (fd);
-      fd = -1;
+  address = _dbus_connection_get_address (c);
+  _dbus_assert (address != NULL);
 
-      goto out;
-    }
-  
-  transport = _dbus_transport_new_for_fd (fd);
-  if (transport == NULL)
-    {
-      dbus_set_result (result, DBUS_RESULT_NO_MEMORY);
-      close (fd);
-      fd = -1;
-      goto out;
-    }
-  
- out:  
-  return transport;
-}
+  /* Let's see if the address got parsed, reordered and formatted correctly */
+  ret = strcmp (address, "unixexec:path=/bin/false,argv0=false,argv1=foobar") == 0;
 
+  dbus_connection_unref (c);
 
-/** @} */
+  return ret;
+}
 
+#endif