#include "dbus-server.h"
#include "dbus-server-unix.h"
+#include "dbus-server-socket.h"
#include "dbus-string.h"
#ifdef DBUS_BUILD_TESTS
#include "dbus-server-debug-pipe.h"
* @{
*/
+/* this is a little fragile since it assumes the address doesn't
+ * already have a guid, but it shouldn't
+ */
+static char*
+copy_address_with_guid_appended (const DBusString *address,
+ const DBusString *guid_hex)
+{
+ DBusString with_guid;
+ char *retval;
+
+ if (!_dbus_string_init (&with_guid))
+ return NULL;
+
+ if (!_dbus_string_copy (address, 0, &with_guid,
+ _dbus_string_get_length (&with_guid)) ||
+ !_dbus_string_append (&with_guid, ",guid=") ||
+ !_dbus_string_copy (guid_hex, 0,
+ &with_guid, _dbus_string_get_length (&with_guid)))
+ {
+ _dbus_string_free (&with_guid);
+ return NULL;
+ }
+
+ retval = NULL;
+ _dbus_string_steal_data (&with_guid, &retval);
+
+ _dbus_string_free (&with_guid);
+
+ return retval; /* may be NULL if steal_data failed */
+}
+
/**
* Initializes the members of the DBusServer base class.
* Chained up to by subclass constructors.
server->address = NULL;
server->watches = NULL;
server->timeouts = NULL;
+
+ if (!_dbus_string_init (&server->guid_hex))
+ return FALSE;
+
+ _dbus_generate_uuid (&server->guid);
+
+ if (!_dbus_uuid_encode (&server->guid, &server->guid_hex))
+ goto failed;
- if (!_dbus_string_copy_data (address, &server->address))
+ server->address = copy_address_with_guid_appended (address,
+ &server->guid_hex);
+ if (server->address == NULL)
goto failed;
-
- server->mutex = dbus_mutex_new ();
+
+ _dbus_mutex_new_at_location (&server->mutex);
if (server->mutex == NULL)
goto failed;
return TRUE;
failed:
- if (server->mutex)
- {
- dbus_mutex_free (server->mutex);
- server->mutex = NULL;
- }
+ _dbus_mutex_free_at_location (&server->mutex);
+ server->mutex = NULL;
if (server->watches)
{
_dbus_watch_list_free (server->watches);
dbus_free (server->address);
server->address = NULL;
}
+ _dbus_string_free (&server->guid_hex);
return FALSE;
}
*/
void
_dbus_server_finalize_base (DBusServer *server)
-{
+{
+ /* We don't have the lock, but nobody should be accessing
+ * concurrently since they don't have a ref
+ */
+#ifndef DBUS_DISABLE_CHECKS
+ _dbus_assert (!server->have_server_lock);
+#endif
+ _dbus_assert (server->disconnected);
+
/* calls out to application code... */
_dbus_data_slot_list_free (&server->slot_list);
dbus_server_set_new_connection_function (server, NULL, NULL, NULL);
- if (!server->disconnected)
- dbus_server_disconnect (server);
-
_dbus_watch_list_free (server->watches);
_dbus_timeout_list_free (server->timeouts);
- dbus_mutex_free (server->mutex);
+ _dbus_mutex_free_at_location (&server->mutex);
dbus_free (server->address);
dbus_free_string_array (server->auth_mechanisms);
+
+ _dbus_string_free (&server->guid_hex);
}
HAVE_LOCK_CHECK (server);
- /* This isn't really safe or reasonable; a better pattern is the "do everything, then
- * drop lock and call out" one; but it has to be propagated up through all callers
+ /* This isn't really safe or reasonable; a better pattern is the "do
+ * everything, then drop lock and call out" one; but it has to be
+ * propagated up through all callers
*/
watches = server->watches;
_dbus_server_add_watch (DBusServer *server,
DBusWatch *watch)
{
+ HAVE_LOCK_CHECK (server);
return protected_change_watch (server, watch,
_dbus_watch_list_add_watch,
NULL, NULL, FALSE);
_dbus_server_remove_watch (DBusServer *server,
DBusWatch *watch)
{
+ HAVE_LOCK_CHECK (server);
protected_change_watch (server, watch,
NULL,
_dbus_watch_list_remove_watch,
{
_dbus_assert (watch != NULL);
+ HAVE_LOCK_CHECK (server);
protected_change_watch (server, watch,
NULL, NULL,
_dbus_watch_list_toggle_watch,
enabled);
}
-
/** @} */
/**
*
*/
+static const struct {
+ DBusServerListenResult (* func) (DBusAddressEntry *entry,
+ DBusServer **server_p,
+ DBusError *error);
+} listen_funcs[] = {
+ { _dbus_server_listen_socket },
+ { _dbus_server_listen_platform_specific }
+#ifdef DBUS_BUILD_TESTS
+ , { _dbus_server_listen_debug_pipe }
+#endif
+};
+
/**
* Listens for new connections on the given address.
- * Returns #NULL if listening fails for any reason.
+ * If there are multiple address entries in the address,
+ * tries each one and listens on the first one that
+ * works.
+ *
+ * Returns #NULL and sets error if listening fails for any reason.
* Otherwise returns a new #DBusServer.
* dbus_server_set_new_connection_function() and
* dbus_server_set_watch_functions() should be called
* immediately to render the server fully functional.
- *
- * @todo error messages on bad address could really be better.
- * DBusResultCode is a bit limiting here.
- *
+ *
* @param address the address of this server.
* @param error location to store rationale for failure.
* @returns a new DBusServer, or #NULL on failure.
DBusServer *server;
DBusAddressEntry **entries;
int len, i;
- const char *address_problem_type;
- const char *address_problem_field;
- const char *address_problem_other;
-
+ DBusError first_connect_error;
+ dbus_bool_t handled_once;
+
_dbus_return_val_if_fail (address != NULL, NULL);
_dbus_return_val_if_error_is_set (error, NULL);
return NULL;
server = NULL;
- address_problem_type = NULL;
- address_problem_field = NULL;
- address_problem_other = NULL;
+ dbus_error_init (&first_connect_error);
+ handled_once = FALSE;
for (i = 0; i < len; i++)
{
- const char *method = dbus_address_entry_get_method (entries[i]);
-
- if (strcmp (method, "unix") == 0)
- {
- const char *path = dbus_address_entry_get_value (entries[i], "path");
- const char *tmpdir = dbus_address_entry_get_value (entries[i], "tmpdir");
- const char *abstract = dbus_address_entry_get_value (entries[i], "abstract");
-
- if (path == NULL && tmpdir == NULL && abstract == NULL)
+ int j;
+
+ for (j = 0; j < (int) _DBUS_N_ELEMENTS (listen_funcs); ++j)
+ {
+ DBusServerListenResult result;
+ DBusError tmp_error;
+
+ dbus_error_init (&tmp_error);
+ result = (* listen_funcs[j].func) (entries[i],
+ &server,
+ &tmp_error);
+
+ if (result == DBUS_SERVER_LISTEN_OK)
{
- address_problem_type = "unix";
- address_problem_field = "path or tmpdir or abstract";
- goto bad_address;
+ _dbus_assert (server != NULL);
+ _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
+ handled_once = TRUE;
+ goto out;
}
-
- if ((path && tmpdir) ||
- (path && abstract) ||
- (tmpdir && abstract))
+ else if (result == DBUS_SERVER_LISTEN_BAD_ADDRESS)
{
- address_problem_other = "cannot specify two of \"path\" and \"tmpdir\" and \"abstract\" at the same time";
- goto bad_address;
+ _dbus_assert (server == NULL);
+ _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
+ dbus_move_error (&tmp_error, error);
+ handled_once = TRUE;
+ goto out;
}
-
- if (tmpdir != NULL)
+ else if (result == DBUS_SERVER_LISTEN_NOT_HANDLED)
{
- DBusString full_path;
- DBusString filename;
-
- if (!_dbus_string_init (&full_path))
- {
- dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
- goto out;
- }
-
- if (!_dbus_string_init (&filename))
- {
- _dbus_string_free (&full_path);
- dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
- goto out;
- }
-
- if (!_dbus_string_append (&filename,
- "dbus-") ||
- !_dbus_generate_random_ascii (&filename, 10) ||
- !_dbus_string_append (&full_path, tmpdir) ||
- !_dbus_concat_dir_and_file (&full_path, &filename))
- {
- _dbus_string_free (&full_path);
- _dbus_string_free (&filename);
- dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
- goto out;
- }
-
- /* FIXME - we will unconditionally unlink() the path if
- * we don't support abstract namespace. unlink() does
- * not follow symlinks, but would like independent
- * confirmation this is safe enough. See also
- * _dbus_listen_unix_socket() and comments therein.
- */
-
- /* Always use abstract namespace if possible with tmpdir */
-
- server =
- _dbus_server_new_for_domain_socket (_dbus_string_get_const_data (&full_path),
-#ifdef HAVE_ABSTRACT_SOCKETS
- TRUE,
-#else
- FALSE,
-#endif
- error);
+ _dbus_assert (server == NULL);
+ _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
- _dbus_string_free (&full_path);
- _dbus_string_free (&filename);
+ /* keep trying addresses */
}
- else
+ else if (result == DBUS_SERVER_LISTEN_DID_NOT_CONNECT)
{
- if (path)
- server = _dbus_server_new_for_domain_socket (path, FALSE, error);
+ _dbus_assert (server == NULL);
+ _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
+ if (!dbus_error_is_set (&first_connect_error))
+ dbus_move_error (&tmp_error, &first_connect_error);
else
- server = _dbus_server_new_for_domain_socket (abstract, TRUE, error);
- }
- }
- else if (strcmp (method, "tcp") == 0)
- {
- const char *host = dbus_address_entry_get_value (entries[i], "host");
- const char *port = dbus_address_entry_get_value (entries[i], "port");
- DBusString str;
- long lport;
- dbus_bool_t sresult;
-
- if (port == NULL)
- {
- address_problem_type = "tcp";
- address_problem_field = "port";
- goto bad_address;
- }
+ dbus_error_free (&tmp_error);
- _dbus_string_init_const (&str, port);
- sresult = _dbus_string_parse_int (&str, 0, &lport, NULL);
- _dbus_string_free (&str);
-
- if (sresult == FALSE || lport <= 0 || lport > 65535)
- {
- address_problem_other = "Port is not an integer between 0 and 65535";
- goto bad_address;
+ handled_once = TRUE;
+
+ /* keep trying addresses */
}
-
- server = _dbus_server_new_for_tcp_socket (host, lport, error);
+ }
- if (server)
- break;
- }
-#ifdef DBUS_BUILD_TESTS
- else if (strcmp (method, "debug-pipe") == 0)
- {
- const char *name = dbus_address_entry_get_value (entries[i], "name");
+ _dbus_assert (server == NULL);
+ _DBUS_ASSERT_ERROR_IS_CLEAR (error);
+ }
- if (name == NULL)
- {
- address_problem_type = "debug-pipe";
- address_problem_field = "name";
- goto bad_address;
- }
+ out:
- server = _dbus_server_debug_pipe_new (name, error);
- }
-#endif
+ if (!handled_once)
+ {
+ _DBUS_ASSERT_ERROR_IS_CLEAR (error);
+ if (len > 0)
+ dbus_set_error (error,
+ DBUS_ERROR_BAD_ADDRESS,
+ "Unknown address type '%s'",
+ dbus_address_entry_get_method (entries[0]));
else
- {
- address_problem_other = "Unknown address type (examples of valid types are \"unix\" and \"tcp\")";
- goto bad_address;
- }
-
- if (server)
- break;
+ dbus_set_error (error,
+ DBUS_ERROR_BAD_ADDRESS,
+ "Empty address '%s'",
+ address);
}
-
- out:
dbus_address_entries_free (entries);
- return server;
- bad_address:
- dbus_address_entries_free (entries);
- if (address_problem_type != NULL)
- dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS,
- "Server address of type %s was missing argument %s",
- address_problem_type, address_problem_field);
- else
- dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS,
- "Could not parse server address: %s",
- address_problem_other);
+ if (server == NULL)
+ {
+ _dbus_assert (error == NULL || dbus_error_is_set (&first_connect_error) ||
+ dbus_error_is_set (error));
+
+ if (error && dbus_error_is_set (error))
+ {
+ /* already set the error */
+ }
+ else
+ {
+ /* didn't set the error but either error should be
+ * NULL or first_connect_error should be set.
+ */
+ _dbus_assert (error == NULL || dbus_error_is_set (&first_connect_error));
+ dbus_move_error (&first_connect_error, error);
+ }
+
+ _DBUS_ASSERT_ERROR_IS_CLEAR (&first_connect_error); /* be sure we freed it */
+ _DBUS_ASSERT_ERROR_IS_SET (error);
- return NULL;
+ return NULL;
+ }
+ else
+ {
+ _DBUS_ASSERT_ERROR_IS_CLEAR (error);
+ return server;
+ }
}
/**
dbus_server_ref (DBusServer *server)
{
_dbus_return_val_if_fail (server != NULL, NULL);
+ _dbus_return_val_if_fail (server->refcount.value > 0, NULL);
#ifdef DBUS_HAVE_ATOMIC_INT
_dbus_atomic_inc (&server->refcount);
/**
* Decrements the reference count of a DBusServer. Finalizes the
- * server if the reference count reaches zero. The server connection
- * will be closed as with dbus_server_disconnect() when the server is
- * finalized.
+ * server if the reference count reaches zero.
+ *
+ * The server must be disconnected before the refcount reaches zero.
*
* @param server the server.
*/
dbus_bool_t last_unref;
_dbus_return_if_fail (server != NULL);
+ _dbus_return_if_fail (server->refcount.value > 0);
#ifdef DBUS_HAVE_ATOMIC_INT
last_unref = (_dbus_atomic_dec (&server->refcount) == 1);
if (last_unref)
{
+ /* lock not held! */
+ _dbus_assert (server->disconnected);
+
_dbus_assert (server->vtable->finalize != NULL);
(* server->vtable->finalize) (server);
void
_dbus_server_ref_unlocked (DBusServer *server)
{
+ _dbus_assert (server != NULL);
+ _dbus_assert (server->refcount.value > 0);
+
HAVE_LOCK_CHECK (server);
#ifdef DBUS_HAVE_ATOMIC_INT
dbus_bool_t last_unref;
_dbus_assert (server != NULL);
+ _dbus_assert (server->refcount.value > 0);
HAVE_LOCK_CHECK (server);
if (last_unref)
{
+ _dbus_assert (server->disconnected);
+
+ SERVER_UNLOCK (server);
+
_dbus_assert (server->vtable->finalize != NULL);
(* server->vtable->finalize) (server);
dbus_server_disconnect (DBusServer *server)
{
_dbus_return_if_fail (server != NULL);
+ _dbus_return_if_fail (server->refcount.value > 0);
SERVER_LOCK (server);
+ _dbus_server_ref_unlocked (server);
_dbus_assert (server->vtable->disconnect != NULL);
- if (server->disconnected)
+ if (!server->disconnected)
{
- SERVER_UNLOCK (server);
- return;
+ /* this has to be first so recursive calls to disconnect don't happen */
+ server->disconnected = TRUE;
+
+ (* server->vtable->disconnect) (server);
}
-
- (* server->vtable->disconnect) (server);
- server->disconnected = TRUE;
SERVER_UNLOCK (server);
+ dbus_server_unref (server);
}
/**
dbus_server_allocate_data_slot (dbus_int32_t *slot_p)
{
return _dbus_data_slot_allocator_alloc (&slot_allocator,
- _DBUS_LOCK_NAME (server_slots),
+ (DBusMutex **)&_DBUS_LOCK_NAME (server_slots),
slot_p);
}
for (i = 0; i < _DBUS_N_ELEMENTS (valid_addresses); i++)
{
- server = dbus_server_listen (valid_addresses[i], NULL);
+ DBusError error;
+
+ /* FIXME um, how are the two tests here different? */
+
+ dbus_error_init (&error);
+ server = dbus_server_listen (valid_addresses[i], &error);
if (server == NULL)
- _dbus_assert_not_reached ("Failed to listen for valid address.");
+ {
+ _dbus_warn ("server listen error: %s: %s\n", error.name, error.message);
+ dbus_error_free (&error);
+ _dbus_assert_not_reached ("Failed to listen for valid address.");
+ }
+ dbus_server_disconnect (server);
dbus_server_unref (server);
/* Try disconnecting before unreffing */
- server = dbus_server_listen (valid_addresses[i], NULL);
+ server = dbus_server_listen (valid_addresses[i], &error);
if (server == NULL)
- _dbus_assert_not_reached ("Failed to listen for valid address.");
+ {
+ _dbus_warn ("server listen error: %s: %s\n", error.name, error.message);
+ dbus_error_free (&error);
+ _dbus_assert_not_reached ("Failed to listen for valid address.");
+ }
dbus_server_disconnect (server);
-
dbus_server_unref (server);
}