Commit d9e4725f authored by Frank Osterfeld's avatar Frank Osterfeld Committed by Ralf Habacker
Browse files

Windows fixes

Replace dbus_daemon_init() by dbus_publish_session_bus_address(),
publishing the full address. Omit username from mutexes (not necessary
as mutex names are local to session). Don't exit if publishing the
address failed (allow multiple session bus instances per
session). Based on 00ee92ae314 by Tor Lillqvist.

Cherry-picked from commit 23945513e9a4da61d286ebfbce3897aa061ddbfe in
the dbus4win repository by tml@iki.fi. Remove claim of that commit not
being merged from README.dbus4win.
parent 459c19b9
......@@ -62,10 +62,6 @@ b0d14fed92d9f539cd3478e72dc2f3ea75f3941a
configure.in: only check for wspiapi.h on Windows
nah, no harm checking for it on UNIX too
23945513e9a4da61d286ebfbce3897aa061ddbfe
replace dbus_daemon_init by dbus_publish_session_bus_address, publishing the...
doesn't apply, too complex, split into separate chunks
1724fc10e516e2d570537d522d8608abba90be5e
prototypes cleanup
related to cd2cdb3457f72cf058d5b19796ee691ceca6b52c which was skipped above
......@@ -254,6 +254,7 @@ main (int argc, char **argv)
int i;
dbus_bool_t print_address;
dbus_bool_t print_pid;
dbus_bool_t is_session_bus;
int force_fork;
if (!_dbus_string_init (&config_file))
......@@ -267,6 +268,7 @@ main (int argc, char **argv)
print_address = FALSE;
print_pid = FALSE;
is_session_bus = FALSE;
force_fork = FORK_FOLLOW_CONFIG_FILE;
prev_arg = NULL;
......@@ -296,6 +298,7 @@ main (int argc, char **argv)
}
else if (strcmp (arg, "--session") == 0)
{
is_session_bus = TRUE;
check_two_config_files (&config_file, "session");
if (!_dbus_append_session_config_file (&config_file))
......@@ -452,6 +455,9 @@ main (int argc, char **argv)
exit (1);
}
if (is_session_bus)
_dbus_daemon_publish_session_bus_address (bus_context_get_address (context));
/* bus_context_new() closes the print_addr_pipe and
* print_pid_pipe
*/
......@@ -473,5 +479,8 @@ main (int argc, char **argv)
bus_context_unref (context);
bus_selinux_shutdown ();
if (is_session_bus)
_dbus_daemon_unpublish_session_bus_address ();
return 0;
}
......@@ -3896,6 +3896,19 @@ _dbus_append_keyring_directory_for_credentials (DBusString *directory,
return FALSE;
}
//PENDING(kdab) docs
void
_dbus_daemon_publish_session_bus_address (const char* addr)
{
}
//PENDING(kdab) docs
void
_dbus_daemon_unpublish_session_bus_address (void)
{
}
/**
* See if errno is EAGAIN or EWOULDBLOCK (this has to be done differently
......
......@@ -1452,10 +1452,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
......@@ -1633,7 +1629,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++)
{
......@@ -2873,7 +2868,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";
......@@ -2886,65 +2881,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 );
}
#if 0
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 );
......@@ -2960,27 +2939,17 @@ _dbus_daemon_release()
_dbus_global_unlock( lock );
}
#endif
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)
......@@ -2990,17 +2959,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 );
......@@ -3008,25 +2977,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);
......@@ -3037,7 +2998,7 @@ _dbus_daemon_already_runs (DBusString *adress)
}
// read shm
bRet = _dbus_get_autolaunch_shm( adress );
bRet = _dbus_get_autolaunch_shm( address );
// cleanup
CloseHandle ( daemon );
......
......@@ -221,6 +221,10 @@ dbus_bool_t _dbus_windows_user_is_process_owner (const char *windows_sid)
dbus_bool_t _dbus_append_keyring_directory_for_credentials (DBusString *directory,
DBusCredentials *credentials);
void _dbus_daemon_publish_session_bus_address (const char* address);
void _dbus_daemon_unpublish_session_bus_address (void);
dbus_bool_t _dbus_socket_can_pass_unix_fd(int fd);
/** Opaque type representing an atomically-modifiable integer
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment