Consistently include <config.h> in all C source files and never in header files.
[platform/upstream/dbus.git] / dbus / dbus-sysdeps-win.c
index 7939008..bba8915 100644 (file)
@@ -26,6 +26,8 @@
  *
  */
 
+#include <config.h>
+
 #define STRSAFE_NO_DEPRECATE
 
 #ifndef DBUS_WINCE
@@ -83,7 +85,7 @@ extern BOOL WINAPI ConvertSidToStringSidA (PSID Sid, LPSTR *StringSid);
 
 typedef int socklen_t;
 
-static char*
+char*
 _dbus_win_error_string (int error_number)
 {
   char *msg;
@@ -102,69 +104,13 @@ _dbus_win_error_string (int error_number)
   return msg;
 }
 
-static void
+void
 _dbus_win_free_error_string (char *string)
 {
   LocalFree (string);
 }
 
 /**
- * write data to a pipe.
- *
- * @param pipe the pipe instance
- * @param buffer the buffer to write data from
- * @param start the first byte in the buffer to write
- * @param len the number of bytes to try to write
- * @param error error return
- * @returns the number of bytes written or -1 on error
- */
-int
-_dbus_pipe_write (DBusPipe         *pipe,
-                  const DBusString *buffer,
-                  int               start,
-                  int               len,
-                  DBusError        *error)
-{
-  int written;
-  const char *buffer_c = _dbus_string_get_const_data (buffer);
-
-  written = _write (pipe->fd_or_handle, buffer_c + start, len);
-  if (written < 0)
-    {
-      dbus_set_error (error, DBUS_ERROR_FAILED,
-                      "Writing to pipe: %s\n",
-                      strerror (errno));
-    }
-  return written;
-}
-
-/**
- * close a pipe.
- *
- * @param pipe the pipe instance
- * @param error return location for an error
- * @returns #FALSE if error is set
- */
-int
-_dbus_pipe_close  (DBusPipe         *pipe,
-                   DBusError        *error)
-{
-  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
-
-  if (_close (pipe->fd_or_handle) < 0)
-    {
-      dbus_set_error (error, _dbus_error_from_errno (errno),
-                      "Could not close pipe %d: %s", pipe->fd_or_handle, strerror (errno));
-      return -1;
-    }
-  else
-    {
-      _dbus_pipe_invalidate (pipe);
-      return 0;
-    }
-}
-
-/**
  * Socket interface
  *
  */
@@ -174,8 +120,9 @@ _dbus_pipe_close  (DBusPipe         *pipe,
  * the data it reads to the DBusString buffer. It appends
  * up to the given count, and returns the same value
  * and same errno as read(). The only exception is that
- * _dbus_read() handles EINTR for you. _dbus_read() can
- * return ENOMEM, even though regular UNIX read doesn't.
+ * _dbus_read_socket() handles EINTR for you. 
+ * _dbus_read_socket() can return ENOMEM, even though 
+ * regular UNIX read doesn't.
  *
  * @param fd the file descriptor to read from
  * @param buffer the buffer to append data to
@@ -331,31 +278,12 @@ _dbus_close_socket (int        fd,
 void
 _dbus_fd_set_close_on_exec (int handle)
 {
-#ifdef ENABLE_DBUSSOCKET
-  DBusSocket *s;
-  if (handle < 0)
-    return;
-
-  _dbus_lock_sockets();
-
-  _dbus_handle_to_socket_unlocked (handle, &s);
-  s->close_on_exec = TRUE;
-
-  _dbus_unlock_sockets();
-#else
-  /* TODO unic code.
-  int val;
-  
-  val = fcntl (fd, F_GETFD, 0);
-  
-  if (val < 0)
-    return;
-
-  val |= FD_CLOEXEC;
-  
-  fcntl (fd, F_SETFD, val);
-  */
-#endif
+  if ( !SetHandleInformation( (HANDLE) handle,
+                        HANDLE_FLAG_INHERIT | HANDLE_FLAG_PROTECT_FROM_CLOSE,
+                        0 /*disable both flags*/ ) )
+    {
+      _dbus_win_warn_win_error ("Disabling socket handle inheritance failed:", GetLastError());
+    }
 }
 
 /**
@@ -742,15 +670,6 @@ out1:
 /** @} end of sysdeps-win */
 
 
-/** Gets our UID
- * @returns process UID
- */
-dbus_uid_t
-_dbus_getuid (void)
-{
-       return DBUS_UID_UNSET;
-}
-
 /**
  * The only reason this is separate from _dbus_getpid() is to allow it
  * on Windows for logging but not for other purposes.
@@ -767,10 +686,10 @@ _dbus_pid_for_log (void)
  * @param points to sid buffer, need to be freed with LocalFree()
  * @returns process sid
  */
-dbus_bool_t
+static dbus_bool_t
 _dbus_getsid(char **sid)
 {
-  HANDLE process_token = NULL;
+  HANDLE process_token = INVALID_HANDLE_VALUE;
   TOKEN_USER *token_user = NULL;
   DWORD n;
   PSID psid;
@@ -804,38 +723,13 @@ _dbus_getsid(char **sid)
   retval = TRUE;
 
 failed:
-  if (process_token != NULL)
+  if (process_token != INVALID_HANDLE_VALUE)
     CloseHandle (process_token);
 
   _dbus_verbose("_dbus_getsid() returns %d\n",retval);
   return retval;
 }
 
-
-#ifdef DBUS_BUILD_TESTS
-/** Gets our GID
- * @returns process GID
- */
-dbus_gid_t
-_dbus_getgid (void)
-{
-       return DBUS_GID_UNSET;
-}
-
-#if 0
-dbus_bool_t
-_dbus_domain_test (const char *test_data_dir)
-{
-  if (!_dbus_test_oom_handling ("spawn_nonexistent",
-                                check_spawn_nonexistent,
-                                NULL))
-    return FALSE;
-}
-
-#endif
-
-#endif //DBUS_BUILD_TESTS
-
 /************************************************************************
  
  pipes
@@ -1104,7 +998,7 @@ _dbus_poll (DBusPollFD *fds,
   if (DBUS_SOCKET_API_RETURNS_ERROR (ready))
     {
       DBUS_SOCKET_SET_ERRNO ();
-      if (errno != EWOULDBLOCK)
+      if (errno != WSAEWOULDBLOCK)
         _dbus_verbose ("WSAWaitForMultipleEvents: failed: %s\n", strerror (errno));
       ret = -1;
     }
@@ -1242,7 +1136,7 @@ _dbus_poll (DBusPollFD *fds,
   if (DBUS_SOCKET_API_RETURNS_ERROR (ready))
     {
       DBUS_SOCKET_SET_ERRNO ();
-      if (errno != EWOULDBLOCK)
+      if (errno != WSAEWOULDBLOCK)
         _dbus_verbose ("select: failed: %s\n", _dbus_strerror (errno));
     }
   else if (ready == 0)
@@ -1486,10 +1380,6 @@ _dbus_connect_tcp_socket_with_nonce (const char     *host,
   return fd;
 }
 
-
-void
-_dbus_daemon_init(const char *host, dbus_uint32_t port);
-
 /**
  * Creates a socket and binds it to the given path, then listens on
  * the socket. The socket is set to be nonblocking.  In case of port=0
@@ -1667,7 +1557,6 @@ _dbus_listen_tcp_socket (const char     *host,
     }
 
   sscanf(_dbus_string_get_const_data(retport), "%d", &port_num);
-  _dbus_daemon_init(host, port_num);
 
   for (i = 0 ; i < nlisten_fd ; i++)
     {
@@ -1886,7 +1775,7 @@ _dbus_concat_dir_and_file (DBusString       *dir,
                             _dbus_string_get_length (dir));
 }
 
-/*---------------- DBusCredentials ----------------------------------
+/*---------------- DBusCredentials ----------------------------------*/
 
 /**
  * Adds the credentials corresponding to the given username.
@@ -2007,21 +1896,23 @@ _dbus_get_current_time (long *tv_sec,
                         long *tv_usec)
 {
   FILETIME ft;
-  dbus_uint64_t *time64 = (dbus_uint64_t *) &ft;
+  dbus_uint64_t time64;
 
   GetSystemTimeAsFileTime (&ft);
 
+  memcpy (&time64, &ft, sizeof (time64));
+
   /* Convert from 100s of nanoseconds since 1601-01-01
   * to Unix epoch. Yes, this is Y2038 unsafe.
   */
-  *time64 -= DBUS_INT64_CONSTANT (116444736000000000);
-  *time64 /= 10;
+  time64 -= DBUS_INT64_CONSTANT (116444736000000000);
+  time64 /= 10;
 
   if (tv_sec)
-    *tv_sec = *time64 / 1000000;
+    *tv_sec = time64 / 1000000;
 
   if (tv_usec)
-    *tv_usec = *time64 % 1000000;
+    *tv_usec = time64 % 1000000;
 }
 
 
@@ -2033,354 +1924,6 @@ _dbus_disable_sigpipe (void)
 {
 }
 
-
-/* _dbus_read() is static on Windows, only used below in this file.
- */
-static int
-_dbus_read (int               fd,
-            DBusString       *buffer,
-            int               count)
-{
-  int bytes_read;
-  int start;
-  char *data;
-
-  _dbus_assert (count >= 0);
-
-  start = _dbus_string_get_length (buffer);
-
-  if (!_dbus_string_lengthen (buffer, count))
-    {
-      errno = ENOMEM;
-      return -1;
-    }
-
-  data = _dbus_string_get_data_len (buffer, start, count);
-
- again:
-
-  bytes_read = _read (fd, data, count);
-
-  if (bytes_read < 0)
-    {
-      if (errno == EINTR)
-        goto again;
-      else
-        {
-          /* put length back (note that this doesn't actually realloc anything) */
-          _dbus_string_set_length (buffer, start);
-          return -1;
-        }
-    }
-  else
-    {
-      /* put length back (doesn't actually realloc) */
-      _dbus_string_set_length (buffer, start + bytes_read);
-
-#if 0
-      if (bytes_read > 0)
-        _dbus_verbose_bytes_of_string (buffer, start, bytes_read);
-#endif
-
-      return bytes_read;
-    }
-}
-
-/**
- * Appends the contents of the given file to the string,
- * returning error code. At the moment, won't open a file
- * more than a megabyte in size.
- *
- * @param str the string to append to
- * @param filename filename to load
- * @param error place to set an error
- * @returns #FALSE if error was set
- */
-dbus_bool_t
-_dbus_file_get_contents (DBusString       *str,
-                         const DBusString *filename,
-                         DBusError        *error)
-{
-  int fd;
-  struct _stati64 sb;
-  int orig_len;
-  int total;
-  const char *filename_c;
-
-  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
-
-  filename_c = _dbus_string_get_const_data (filename);
-
-  fd = _open (filename_c, O_RDONLY | O_BINARY);
-  if (fd < 0)
-    {
-      dbus_set_error (error, _dbus_error_from_errno (errno),
-                      "Failed to open \"%s\": %s",
-                      filename_c,
-                      strerror (errno));
-      return FALSE;
-    }
-
-  _dbus_verbose ("file %s fd %d opened\n", filename_c, fd);
-
-  if (_fstati64 (fd, &sb) < 0)
-    {
-      dbus_set_error (error, _dbus_error_from_errno (errno),
-                      "Failed to stat \"%s\": %s",
-                      filename_c,
-                      strerror (errno));
-
-      _dbus_verbose ("fstat() failed: %s",
-                     strerror (errno));
-
-      _close (fd);
-
-      return FALSE;
-    }
-
-  if (sb.st_size > _DBUS_ONE_MEGABYTE)
-    {
-      dbus_set_error (error, DBUS_ERROR_FAILED,
-                      "File size %lu of \"%s\" is too large.",
-                      (unsigned long) sb.st_size, filename_c);
-      _close (fd);
-      return FALSE;
-    }
-
-  total = 0;
-  orig_len = _dbus_string_get_length (str);
-  if (sb.st_size > 0 && S_ISREG (sb.st_mode))
-    {
-      int bytes_read;
-
-      while (total < (int) sb.st_size)
-        {
-          bytes_read = _dbus_read (fd, str, sb.st_size - total);
-          if (bytes_read <= 0)
-            {
-              dbus_set_error (error, _dbus_error_from_errno (errno),
-                              "Error reading \"%s\": %s",
-                              filename_c,
-                              strerror (errno));
-
-              _dbus_verbose ("read() failed: %s",
-                             strerror (errno));
-
-              _close (fd);
-              _dbus_string_set_length (str, orig_len);
-              return FALSE;
-            }
-          else
-            total += bytes_read;
-        }
-
-      _close (fd);
-      return TRUE;
-    }
-  else if (sb.st_size != 0)
-    {
-      _dbus_verbose ("Can only open regular files at the moment.\n");
-      dbus_set_error (error, DBUS_ERROR_FAILED,
-                      "\"%s\" is not a regular file",
-                      filename_c);
-      _close (fd);
-      return FALSE;
-    }
-  else
-    {
-      _close (fd);
-      return TRUE;
-    }
-}
-
-/**
- * Writes a string out to a file. If the file exists,
- * it will be atomically overwritten by the new data.
- *
- * @param str the string to write out
- * @param filename the file to save string to
- * @param error error to be filled in on failure
- * @returns #FALSE on failure
- */
-dbus_bool_t
-_dbus_string_save_to_file (const DBusString *str,
-                           const DBusString *filename,
-                           DBusError        *error)
-{
-  int fd;
-  int bytes_to_write;
-  const char *filename_c;
-  DBusString tmp_filename;
-  const char *tmp_filename_c;
-  int total;
-  const char *str_c;
-  dbus_bool_t need_unlink;
-  dbus_bool_t retval;
-
-  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
-
-  fd = -1;
-  retval = FALSE;
-  need_unlink = FALSE;
-
-  if (!_dbus_string_init (&tmp_filename))
-    {
-      dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
-      return FALSE;
-    }
-
-  if (!_dbus_string_copy (filename, 0, &tmp_filename, 0))
-    {
-      dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
-      _dbus_string_free (&tmp_filename);
-      return FALSE;
-    }
-
-  if (!_dbus_string_append (&tmp_filename, "."))
-    {
-      dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
-      _dbus_string_free (&tmp_filename);
-      return FALSE;
-    }
-
-#define N_TMP_FILENAME_RANDOM_BYTES 8
-  if (!_dbus_generate_random_ascii (&tmp_filename, N_TMP_FILENAME_RANDOM_BYTES))
-    {
-      dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
-      _dbus_string_free (&tmp_filename);
-      return FALSE;
-    }
-
-  filename_c = _dbus_string_get_const_data (filename);
-  tmp_filename_c = _dbus_string_get_const_data (&tmp_filename);
-
-  fd = _open (tmp_filename_c, O_WRONLY | O_BINARY | O_EXCL | O_CREAT,
-              0600);
-  if (fd < 0)
-    {
-      dbus_set_error (error, _dbus_error_from_errno (errno),
-                      "Could not create %s: %s", tmp_filename_c,
-                      strerror (errno));
-      goto out;
-    }
-
-  _dbus_verbose ("tmp file %s fd %d opened\n", tmp_filename_c, fd);
-
-  need_unlink = TRUE;
-
-  total = 0;
-  bytes_to_write = _dbus_string_get_length (str);
-  str_c = _dbus_string_get_const_data (str);
-
-  while (total < bytes_to_write)
-    {
-      int bytes_written;
-
-      bytes_written = _write (fd, str_c + total, bytes_to_write - total);
-
-      if (bytes_written <= 0)
-        {
-          dbus_set_error (error, _dbus_error_from_errno (errno),
-                          "Could not write to %s: %s", tmp_filename_c,
-                          strerror (errno));
-          goto out;
-        }
-
-      total += bytes_written;
-    }
-
-  if (_close (fd) < 0)
-    {
-      dbus_set_error (error, _dbus_error_from_errno (errno),
-                      "Could not close file %s: %s",
-                      tmp_filename_c, strerror (errno));
-
-      goto out;
-    }
-
-  fd = -1;
-
-  /* Unlike rename(), MoveFileEx() can replace existing files */
-  if (MoveFileExA (tmp_filename_c, filename_c, MOVEFILE_REPLACE_EXISTING) < 0)
-    {
-      char *emsg = _dbus_win_error_string (GetLastError ());
-      dbus_set_error (error, DBUS_ERROR_FAILED,
-                      "Could not rename %s to %s: %s",
-                      tmp_filename_c, filename_c,
-                      emsg);
-      _dbus_win_free_error_string (emsg);
-
-      goto out;
-    }
-
-  need_unlink = FALSE;
-
-  retval = TRUE;
-
- out:
-  /* close first, then unlink */
-
-  if (fd >= 0)
-    _close (fd);
-
-  if (need_unlink && _unlink (tmp_filename_c) < 0)
-    _dbus_verbose ("failed to unlink temp file %s: %s\n",
-                   tmp_filename_c, strerror (errno));
-
-  _dbus_string_free (&tmp_filename);
-
-  if (!retval)
-    _DBUS_ASSERT_ERROR_IS_SET (error);
-
-  return retval;
-}
-
-
-/** Creates the given file, failing if the file already exists.
- *
- * @param filename the filename
- * @param error error location
- * @returns #TRUE if we created the file and it didn't exist
- */
-dbus_bool_t
-_dbus_create_file_exclusively (const DBusString *filename,
-                               DBusError        *error)
-{
-  int fd;
-  const char *filename_c;
-
-  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
-
-  filename_c = _dbus_string_get_const_data (filename);
-
-  fd = _open (filename_c, O_WRONLY | O_BINARY | O_EXCL | O_CREAT,
-              0600);
-  if (fd < 0)
-    {
-      dbus_set_error (error,
-                      DBUS_ERROR_FAILED,
-                      "Could not create file %s: %s\n",
-                      filename_c,
-                      strerror (errno));
-      return FALSE;
-    }
-
-  _dbus_verbose ("exclusive file %s fd %d opened\n", filename_c, fd);
-
-  if (_close (fd) < 0)
-    {
-      dbus_set_error (error,
-                      DBUS_ERROR_FAILED,
-                      "Could not close file %s: %s\n",
-                      filename_c,
-                      strerror (errno));
-      return FALSE;
-    }
-
-  return TRUE;
-}
-
-
 /**
  * Creates a directory; succeeds if the directory
  * is created or already existed.
@@ -2465,8 +2008,21 @@ _dbus_get_tmpdir(void)
 
   if (tmpdir == NULL)
     {
+      char *last_slash;
+
       if (!GetTempPath (sizeof (buf), buf))
-        strcpy (buf, "\\");
+        {
+          _dbus_warn ("GetTempPath failed\n");
+          _dbus_abort ();
+        }
+
+      /* Drop terminating backslash or slash */
+      last_slash = _mbsrchr (buf, '\\');
+      if (last_slash > buf && last_slash[1] == '\0')
+        last_slash[0] = '\0';
+      last_slash = _mbsrchr (buf, '/');
+      if (last_slash > buf && last_slash[1] == '\0')
+        last_slash[0] = '\0';
 
       tmpdir = buf;
     }
@@ -2894,7 +2450,7 @@ void _dbus_global_unlock (HANDLE mutex)
 // for proper cleanup in dbus-daemon
 static HANDLE hDBusDaemonMutex = NULL;
 static HANDLE hDBusSharedMem = NULL;
-// sync _dbus_daemon_init, _dbus_daemon_uninit and _dbus_daemon_already_runs
+// sync _dbus_daemon_publish_session_bus_address, _dbus_daemon_unpublish_session_bus_address and _dbus_daemon_already_runs
 static const char *cUniqueDBusInitMutex = "UniqueDBusInitMutex";
 // sync _dbus_get_autolaunch_address
 static const char *cDBusAutolaunchMutex = "DBusAutolaunchMutex";
@@ -2907,63 +2463,49 @@ static const char *cDBusDaemonAddressInfo = "DBusDaemonAddressInfoDebug";
 static const char *cDBusDaemonAddressInfo = "DBusDaemonAddressInfo";
 #endif
 
+
 void
-_dbus_daemon_init(const char *host, dbus_uint32_t port)
+_dbus_daemon_publish_session_bus_address (const char* address)
 {
   HANDLE lock;
-  char *adr = NULL;
-  char szUserName[64];
-  DWORD dwUserNameSize = sizeof(szUserName);
-  char szDBusDaemonMutex[128];
-  char szDBusDaemonAddressInfo[128];
-  char szAddress[128];
+  char *shared_addr = NULL;
   DWORD ret;
 
-  _dbus_assert(host);
-  _dbus_assert(port);
-
-  _snprintf(szAddress, sizeof(szAddress) - 1, "tcp:host=%s,port=%d", host, port);
-  ret = GetUserName(szUserName, &dwUserNameSize);
-  _dbus_assert(ret != 0);
-  _snprintf(szDBusDaemonMutex, sizeof(szDBusDaemonMutex) - 1, "%s:%s",
-            cDBusDaemonMutex, szUserName);
-  _snprintf(szDBusDaemonAddressInfo, sizeof(szDBusDaemonAddressInfo) - 1, "%s:%s",
-            cDBusDaemonAddressInfo, szUserName);
-
+  _dbus_assert (address);
   // before _dbus_global_lock to keep correct lock/release order
-  hDBusDaemonMutex = CreateMutex( NULL, FALSE, szDBusDaemonMutex );
+  hDBusDaemonMutex = CreateMutex( NULL, FALSE, cDBusDaemonMutex );
   ret = WaitForSingleObject( hDBusDaemonMutex, 1000 );
   if ( ret != WAIT_OBJECT_0 ) {
-    _dbus_warn("Could not lock mutex %s (return code %d). daemon already running?\n", szDBusDaemonMutex, ret );
-    _dbus_assert( !"Could not lock mutex, daemon already running?" );
+    _dbus_warn("Could not lock mutex %s (return code %d). daemon already running? Bus address not published.\n", cDBusDaemonMutex, ret );
+    return;
   }
 
-  // sync _dbus_daemon_init, _dbus_daemon_uninit and _dbus_daemon_already_runs
+  // sync _dbus_daemon_publish_session_bus_address, _dbus_daemon_unpublish_session_bus_address and _dbus_daemon_already_runs
   lock = _dbus_global_lock( cUniqueDBusInitMutex );
 
   // create shm
   hDBusSharedMem = CreateFileMapping( INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE,
-                                      0, strlen( szAddress ) + 1, szDBusDaemonAddressInfo );
+                                      0, strlen( address ) + 1, cDBusDaemonAddressInfo );
   _dbus_assert( hDBusSharedMem );
 
-  adr = MapViewOfFile( hDBusSharedMem, FILE_MAP_WRITE, 0, 0, 0 );
+  shared_addr = MapViewOfFile( hDBusSharedMem, FILE_MAP_WRITE, 0, 0, 0 );
 
-  _dbus_assert( adr );
+  _dbus_assert (shared_addr);
 
-  strcpy( adr, szAddress);
+  strcpy( shared_addr, address);
 
   // cleanup
-  UnmapViewOfFile( adr );
+  UnmapViewOfFile( shared_addr );
 
   _dbus_global_unlock( lock );
 }
 
 void
-_dbus_daemon_release()
+_dbus_daemon_unpublish_session_bus_address (void)
 {
   HANDLE lock;
 
-  // sync _dbus_daemon_init, _dbus_daemon_uninit and _dbus_daemon_already_runs
+  // sync _dbus_daemon_publish_session_bus_address, _dbus_daemon_unpublish_session_bus_address and _dbus_daemon_already_runs
   lock = _dbus_global_lock( cUniqueDBusInitMutex );
 
   CloseHandle( hDBusSharedMem );
@@ -2980,24 +2522,16 @@ _dbus_daemon_release()
 }
 
 static dbus_bool_t
-_dbus_get_autolaunch_shm(DBusString *adress)
+_dbus_get_autolaunch_shm (DBusString *address)
 {
   HANDLE sharedMem;
-  char *adr;
-  char szUserName[64];
-  DWORD dwUserNameSize = sizeof(szUserName);
-  char szDBusDaemonAddressInfo[128];
+  char *shared_addr;
   int i;
 
-  if( !GetUserName(szUserName, &dwUserNameSize) )
-      return FALSE;
-  _snprintf(szDBusDaemonAddressInfo, sizeof(szDBusDaemonAddressInfo) - 1, "%s:%s",
-            cDBusDaemonAddressInfo, szUserName);
-
   // read shm
   for(i=0;i<20;++i) {
       // we know that dbus-daemon is available, so we wait until shm is available
-      sharedMem = OpenFileMapping( FILE_MAP_READ, FALSE, szDBusDaemonAddressInfo );
+      sharedMem = OpenFileMapping( FILE_MAP_READ, FALSE, cDBusDaemonAddressInfo );
       if( sharedMem == 0 )
           Sleep( 100 );
       if ( sharedMem != 0)
@@ -3007,17 +2541,17 @@ _dbus_get_autolaunch_shm(DBusString *adress)
   if( sharedMem == 0 )
       return FALSE;
 
-  adr = MapViewOfFile( sharedMem, FILE_MAP_READ, 0, 0, 0 );
+  shared_addr = MapViewOfFile( sharedMem, FILE_MAP_READ, 0, 0, 0 );
 
-  if( adr == 0 )
+  if( !shared_addr )
       return FALSE;
 
-  _dbus_string_init( adress );
+  _dbus_string_init( address );
 
-  _dbus_string_append( adress, adr ); 
+  _dbus_string_append( address, shared_addr );
 
   // cleanup
-  UnmapViewOfFile( adr );
+  UnmapViewOfFile( shared_addr );
 
   CloseHandle( sharedMem );
 
@@ -3025,25 +2559,17 @@ _dbus_get_autolaunch_shm(DBusString *adress)
 }
 
 static dbus_bool_t
-_dbus_daemon_already_runs (DBusString *adress)
+_dbus_daemon_already_runs (DBusString *address)
 {
   HANDLE lock;
   HANDLE daemon;
   dbus_bool_t bRet = TRUE;
-  char szUserName[64];
-  DWORD dwUserNameSize = sizeof(szUserName);
-  char szDBusDaemonMutex[128];
 
-  // sync _dbus_daemon_init, _dbus_daemon_uninit and _dbus_daemon_already_runs
+  // sync _dbus_daemon_publish_session_bus_address, _dbus_daemon_unpublish_session_bus_address and _dbus_daemon_already_runs
   lock = _dbus_global_lock( cUniqueDBusInitMutex );
 
-  if( !GetUserName(szUserName, &dwUserNameSize) )
-      return FALSE;
-  _snprintf(szDBusDaemonMutex, sizeof(szDBusDaemonMutex) - 1, "%s:%s",
-            cDBusDaemonMutex, szUserName);
-
   // do checks
-  daemon = CreateMutex( NULL, FALSE, szDBusDaemonMutex );
+  daemon = CreateMutex( NULL, FALSE, cDBusDaemonMutex );
   if(WaitForSingleObject( daemon, 10 ) != WAIT_TIMEOUT)
     {
       ReleaseMutex (daemon);
@@ -3054,7 +2580,7 @@ _dbus_daemon_already_runs (DBusString *adress)
     }
 
   // read shm
-  bRet = _dbus_get_autolaunch_shm( adress );
+  bRet = _dbus_get_autolaunch_shm( address );
 
   // cleanup
   CloseHandle ( daemon );
@@ -3075,11 +2601,7 @@ _dbus_get_autolaunch_address (DBusString *address,
   LPSTR lpFile;
   char dbus_exe_path[MAX_PATH];
   char dbus_args[MAX_PATH * 2];
-#ifdef _DEBUG
-  const char * daemon_name = "dbus-daemond.exe";
-#else
-  const char * daemon_name = "dbus-daemon.exe";
-#endif
+  const char * daemon_name = DBUS_DAEMON_NAME ".exe";
 
   mutex = _dbus_global_lock ( cDBusAutolaunchMutex );
 
@@ -3111,7 +2633,8 @@ _dbus_get_autolaunch_address (DBusString *address,
 //  printf("create process \"%s\" %s\n", dbus_exe_path, dbus_args);
   if(CreateProcessA(dbus_exe_path, dbus_args, NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL, &si, &pi))
     {
-
+      CloseHandle (pi.hThread);
+      CloseHandle (pi.hProcess);
       retval = _dbus_get_autolaunch_shm( address );
     }
   
@@ -3280,7 +2803,7 @@ _dbus_flush_caches (void)
 dbus_bool_t
 _dbus_get_is_errno_eagain_or_ewouldblock (void)
 {
-  return errno == EAGAIN || errno == EWOULDBLOCK;
+  return errno == WSAEWOULDBLOCK;
 }
 
 /**
@@ -3290,7 +2813,7 @@ _dbus_get_is_errno_eagain_or_ewouldblock (void)
  * @param len length of buffer
  * @returns #FALSE on failure
  */
-dbus_bool_t 
+static dbus_bool_t
 _dbus_get_install_root(char *prefix, int len)
 {
     //To find the prefix, we cut the filename and also \bin\ if present
@@ -3299,7 +2822,7 @@ _dbus_get_install_root(char *prefix, int len)
     DWORD pathLength;
     char *lastSlash;
     SetLastError( 0 );
-    pathLength = GetModuleFileName(NULL, prefix, len);
+    pathLength = GetModuleFileName(_dbus_win_get_dll_hmodule(), prefix, len);
     if ( pathLength == 0 || GetLastError() != 0 ) {
         *prefix = '\0';
         return FALSE;
@@ -3318,8 +2841,13 @@ _dbus_get_install_root(char *prefix, int len)
     //folder's name happens to end with the *bytes*
     //"\\bin"... (I.e. the second byte of some Han character and then
     //the Latin "bin", but that is not likely I think...
-    if (lastSlash - prefix > 3 && strncmp(lastSlash - 4, "\\bin", 4) == 0)
+    if (lastSlash - prefix >= 4 && strnicmp(lastSlash - 4, "\\bin", 4) == 0)
         lastSlash[-3] = 0;
+    else if (lastSlash - prefix >= 10 && strnicmp(lastSlash - 10, "\\bin\\debug", 10) == 0)
+        lastSlash[-9] = 0;
+    else if (lastSlash - prefix >= 12 && strnicmp(lastSlash - 12, "\\bin\\release", 12) == 0)
+        lastSlash[-11] = 0;
+
     return TRUE;
 }
 
@@ -3446,6 +2974,7 @@ _dbus_append_keyring_directory_for_credentials (DBusString      *directory,
   DBusString dotdir;
   dbus_uid_t uid;
   const char *homepath;
+  const char *homedrive;
 
   _dbus_assert (credentials != NULL);
   _dbus_assert (!_dbus_credentials_are_anonymous (credentials));
@@ -3453,6 +2982,12 @@ _dbus_append_keyring_directory_for_credentials (DBusString      *directory,
   if (!_dbus_string_init (&homedir))
     return FALSE;
 
+  homedrive = _dbus_getenv("HOMEDRIVE");
+  if (homedrive != NULL && *homedrive != '\0')
+    {
+      _dbus_string_append(&homedir,homedrive);
+    }
+
   homepath = _dbus_getenv("HOMEPATH");
   if (homepath != NULL && *homepath != '\0')
     {