/* -*- mode: C; c-file-style: "gnu" -*- */
/* dbus-server.c DBusServer object
*
- * Copyright (C) 2002, 2003 Red Hat Inc.
+ * Copyright (C) 2002, 2003, 2004, 2005 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
#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.h"
#include "dbus-server-debug-pipe.h"
#endif
#include "dbus-address.h"
+#include "dbus-protocol.h"
/**
* @defgroup DBusServer DBusServer
* @{
*/
+/* 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.
*
* @param server the server.
* @param vtable the vtable for the subclass.
+ * @param address the server's address
* @returns #TRUE on success.
*/
dbus_bool_t
_dbus_server_init_base (DBusServer *server,
- const DBusServerVTable *vtable)
+ const DBusServerVTable *vtable,
+ const DBusString *address)
{
server->vtable = vtable;
- server->refcount = 1;
+ server->refcount.value = 1;
+
+ 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;
+
+ server->address = copy_address_with_guid_appended (address,
+ &server->guid_hex);
+ if (server->address == NULL)
+ goto failed;
+
+ _dbus_mutex_new_at_location (&server->mutex);
+ if (server->mutex == NULL)
+ goto failed;
+
server->watches = _dbus_watch_list_new ();
if (server->watches == NULL)
- return FALSE;
+ goto failed;
server->timeouts = _dbus_timeout_list_new ();
if (server->timeouts == NULL)
+ goto failed;
+
+ _dbus_data_slot_list_init (&server->slot_list);
+
+ _dbus_verbose ("Initialized server on address %s\n", server->address);
+
+ return TRUE;
+
+ failed:
+ _dbus_mutex_free_at_location (&server->mutex);
+ server->mutex = NULL;
+ if (server->watches)
{
_dbus_watch_list_free (server->watches);
server->watches = NULL;
- return FALSE;
}
-
- server->connection_counter = _dbus_counter_new ();
- if (server->connection_counter == NULL)
+ if (server->timeouts)
{
- _dbus_watch_list_free (server->watches);
- server->watches = NULL;
_dbus_timeout_list_free (server->timeouts);
server->timeouts = NULL;
-
- return FALSE;
}
-
- server->max_connections = 256; /* same as an X server, seems like a nice default */
-
- _dbus_data_slot_list_init (&server->slot_list);
+ if (server->address)
+ {
+ dbus_free (server->address);
+ server->address = NULL;
+ }
+ _dbus_string_free (&server->guid_hex);
- return TRUE;
+ 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_counter_unref (server->connection_counter);
+
+ _dbus_mutex_free_at_location (&server->mutex);
+
+ dbus_free (server->address);
+
+ dbus_free_string_array (server->auth_mechanisms);
+
+ _dbus_string_free (&server->guid_hex);
+}
+
+
+typedef dbus_bool_t (* DBusWatchAddFunction) (DBusWatchList *list,
+ DBusWatch *watch);
+typedef void (* DBusWatchRemoveFunction) (DBusWatchList *list,
+ DBusWatch *watch);
+typedef void (* DBusWatchToggleFunction) (DBusWatchList *list,
+ DBusWatch *watch,
+ dbus_bool_t enabled);
+
+static dbus_bool_t
+protected_change_watch (DBusServer *server,
+ DBusWatch *watch,
+ DBusWatchAddFunction add_function,
+ DBusWatchRemoveFunction remove_function,
+ DBusWatchToggleFunction toggle_function,
+ dbus_bool_t enabled)
+{
+ DBusWatchList *watches;
+ dbus_bool_t retval;
+
+ 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
+ */
+
+ watches = server->watches;
+ if (watches)
+ {
+ server->watches = NULL;
+ _dbus_server_ref_unlocked (server);
+ SERVER_UNLOCK (server);
+
+ if (add_function)
+ retval = (* add_function) (watches, watch);
+ else if (remove_function)
+ {
+ retval = TRUE;
+ (* remove_function) (watches, watch);
+ }
+ else
+ {
+ retval = TRUE;
+ (* toggle_function) (watches, watch, enabled);
+ }
+
+ SERVER_LOCK (server);
+ server->watches = watches;
+ _dbus_server_unref_unlocked (server);
+
+ return retval;
+ }
+ else
+ return FALSE;
}
/**
_dbus_server_add_watch (DBusServer *server,
DBusWatch *watch)
{
- return _dbus_watch_list_add_watch (server->watches, 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)
{
- _dbus_watch_list_remove_watch (server->watches, watch);
+ HAVE_LOCK_CHECK (server);
+ protected_change_watch (server, watch,
+ NULL,
+ _dbus_watch_list_remove_watch,
+ NULL, FALSE);
}
/**
DBusWatch *watch,
dbus_bool_t enabled)
{
- if (server->watches) /* null during finalize */
- _dbus_watch_list_toggle_watch (server->watches,
- watch, enabled);
+ _dbus_assert (watch != NULL);
+
+ HAVE_LOCK_CHECK (server);
+ protected_change_watch (server, watch,
+ NULL, NULL,
+ _dbus_watch_list_toggle_watch,
+ enabled);
+}
+
+
+typedef dbus_bool_t (* DBusTimeoutAddFunction) (DBusTimeoutList *list,
+ DBusTimeout *timeout);
+typedef void (* DBusTimeoutRemoveFunction) (DBusTimeoutList *list,
+ DBusTimeout *timeout);
+typedef void (* DBusTimeoutToggleFunction) (DBusTimeoutList *list,
+ DBusTimeout *timeout,
+ dbus_bool_t enabled);
+
+
+static dbus_bool_t
+protected_change_timeout (DBusServer *server,
+ DBusTimeout *timeout,
+ DBusTimeoutAddFunction add_function,
+ DBusTimeoutRemoveFunction remove_function,
+ DBusTimeoutToggleFunction toggle_function,
+ dbus_bool_t enabled)
+{
+ DBusTimeoutList *timeouts;
+ dbus_bool_t retval;
+
+ 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
+ */
+
+ timeouts = server->timeouts;
+ if (timeouts)
+ {
+ server->timeouts = NULL;
+ _dbus_server_ref_unlocked (server);
+ SERVER_UNLOCK (server);
+
+ if (add_function)
+ retval = (* add_function) (timeouts, timeout);
+ else if (remove_function)
+ {
+ retval = TRUE;
+ (* remove_function) (timeouts, timeout);
+ }
+ else
+ {
+ retval = TRUE;
+ (* toggle_function) (timeouts, timeout, enabled);
+ }
+
+ SERVER_LOCK (server);
+ server->timeouts = timeouts;
+ _dbus_server_unref_unlocked (server);
+
+ return retval;
+ }
+ else
+ return FALSE;
}
/**
_dbus_server_add_timeout (DBusServer *server,
DBusTimeout *timeout)
{
- return _dbus_timeout_list_add_timeout (server->timeouts, timeout);
+ return protected_change_timeout (server, timeout,
+ _dbus_timeout_list_add_timeout,
+ NULL, NULL, FALSE);
}
/**
_dbus_server_remove_timeout (DBusServer *server,
DBusTimeout *timeout)
{
- _dbus_timeout_list_remove_timeout (server->timeouts, timeout);
+ protected_change_timeout (server, timeout,
+ NULL,
+ _dbus_timeout_list_remove_timeout,
+ NULL, FALSE);
}
/**
DBusTimeout *timeout,
dbus_bool_t enabled)
{
- if (server->timeouts) /* null during finalize */
- _dbus_timeout_list_toggle_timeout (server->timeouts,
- timeout, enabled);
+ protected_change_timeout (server, timeout,
+ NULL, NULL,
+ _dbus_timeout_list_toggle_timeout,
+ 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 result location to store rationale for failure.
+ * @param error location to store rationale for failure.
* @returns a new DBusServer, or #NULL on failure.
*
*/
DBusServer*
dbus_server_listen (const char *address,
- DBusResultCode *result)
+ DBusError *error)
{
DBusServer *server;
DBusAddressEntry **entries;
int len, i;
+ 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);
- if (!dbus_parse_address (address, &entries, &len, result))
+ if (!dbus_parse_address (address, &entries, &len, error))
return NULL;
server = 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");
-
- if (path == NULL)
- goto bad_address;
-
- server = _dbus_server_new_for_domain_socket (path, result);
-
- if (server)
- break;
- }
- 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)
- goto bad_address;
-
- _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)
- goto bad_address;
-
- server = _dbus_server_new_for_tcp_socket (host, lport, result);
-
- if (server)
- break;
- }
-#ifdef DBUS_BUILD_TESTS
- else if (strcmp (method, "debug") == 0)
- {
- const char *name = dbus_address_entry_get_value (entries[i], "name");
-
- if (name == NULL)
- goto bad_address;
+ int j;
- server = _dbus_server_debug_new (name, result);
-
- if (server)
- break;
- }
- else if (strcmp (method, "debug-pipe") == 0)
- {
- const char *name = dbus_address_entry_get_value (entries[i], "name");
-
- if (name == NULL)
- goto bad_address;
+ 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)
+ {
+ _dbus_assert (server != NULL);
+ _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
+ handled_once = TRUE;
+ goto out;
+ }
+ else if (result == DBUS_SERVER_LISTEN_BAD_ADDRESS)
+ {
+ _dbus_assert (server == NULL);
+ _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
+ dbus_move_error (&tmp_error, error);
+ handled_once = TRUE;
+ goto out;
+ }
+ else if (result == DBUS_SERVER_LISTEN_NOT_HANDLED)
+ {
+ _dbus_assert (server == NULL);
+ _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
+
+ /* keep trying addresses */
+ }
+ else if (result == DBUS_SERVER_LISTEN_DID_NOT_CONNECT)
+ {
+ _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
+ dbus_error_free (&tmp_error);
+
+ handled_once = TRUE;
+
+ /* keep trying addresses */
+ }
+ }
+
+ _dbus_assert (server == NULL);
+ _DBUS_ASSERT_ERROR_IS_CLEAR (error);
+ }
- server = _dbus_server_debug_pipe_new (name, result);
+ out:
- if (server)
- break;
- }
-#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
- goto bad_address;
+ dbus_set_error (error,
+ DBUS_ERROR_BAD_ADDRESS,
+ "Empty address '%s'",
+ address);
}
dbus_address_entries_free (entries);
- return server;
- bad_address:
- dbus_address_entries_free (entries);
- dbus_set_result (result, DBUS_RESULT_BAD_ADDRESS);
-
- return NULL;
+ 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;
+ }
+ else
+ {
+ _DBUS_ASSERT_ERROR_IS_CLEAR (error);
+ return server;
+ }
}
/**
* Increments the reference count of a DBusServer.
*
* @param server the server.
+ * @returns the server
*/
-void
+DBusServer *
dbus_server_ref (DBusServer *server)
{
- server->refcount += 1;
+ _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);
+#else
+ SERVER_LOCK (server);
+ _dbus_assert (server->refcount.value > 0);
+
+ server->refcount.value += 1;
+ SERVER_UNLOCK (server);
+#endif
+
+ return server;
}
/**
* 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.
*/
void
dbus_server_unref (DBusServer *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);
+#else
+ SERVER_LOCK (server);
+
+ _dbus_assert (server->refcount.value > 0);
+
+ server->refcount.value -= 1;
+ last_unref = (server->refcount.value == 0);
+
+ SERVER_UNLOCK (server);
+#endif
+
+ if (last_unref)
+ {
+ /* lock not held! */
+ _dbus_assert (server->disconnected);
+
+ _dbus_assert (server->vtable->finalize != NULL);
+
+ (* server->vtable->finalize) (server);
+ }
+}
+
+/**
+ * Like dbus_server_ref() but does not acquire the lock (must already be held)
+ *
+ * @param server the server.
+ */
+void
+_dbus_server_ref_unlocked (DBusServer *server)
+{
_dbus_assert (server != NULL);
- _dbus_assert (server->refcount > 0);
+ _dbus_assert (server->refcount.value > 0);
+
+ HAVE_LOCK_CHECK (server);
+
+#ifdef DBUS_HAVE_ATOMIC_INT
+ _dbus_atomic_inc (&server->refcount);
+#else
+ _dbus_assert (server->refcount.value > 0);
+
+ server->refcount.value += 1;
+#endif
+}
- server->refcount -= 1;
- if (server->refcount == 0)
+/**
+ * Like dbus_server_unref() but does not acquire the lock (must already be held)
+ *
+ * @param server the server.
+ */
+void
+_dbus_server_unref_unlocked (DBusServer *server)
+{
+ dbus_bool_t last_unref;
+
+ _dbus_assert (server != NULL);
+ _dbus_assert (server->refcount.value > 0);
+
+ HAVE_LOCK_CHECK (server);
+
+#ifdef DBUS_HAVE_ATOMIC_INT
+ last_unref = (_dbus_atomic_dec (&server->refcount) == 1);
+#else
+ _dbus_assert (server->refcount.value > 0);
+
+ server->refcount.value -= 1;
+ last_unref = (server->refcount.value == 0);
+#endif
+
+ if (last_unref)
{
+ _dbus_assert (server->disconnected);
+
+ SERVER_UNLOCK (server);
+
_dbus_assert (server->vtable->finalize != NULL);
(* server->vtable->finalize) (server);
void
dbus_server_disconnect (DBusServer *server)
{
- _dbus_assert (server->vtable->disconnect != NULL);
+ _dbus_return_if_fail (server != NULL);
+ _dbus_return_if_fail (server->refcount.value > 0);
- if (server->disconnected)
- return;
+ SERVER_LOCK (server);
+ _dbus_server_ref_unlocked (server);
- (* server->vtable->disconnect) (server);
- server->disconnected = TRUE;
+ _dbus_assert (server->vtable->disconnect != NULL);
+
+ if (!server->disconnected)
+ {
+ /* this has to be first so recursive calls to disconnect don't happen */
+ server->disconnected = TRUE;
+
+ (* server->vtable->disconnect) (server);
+ }
+
+ SERVER_UNLOCK (server);
+ dbus_server_unref (server);
}
/**
dbus_bool_t
dbus_server_get_is_connected (DBusServer *server)
{
- return !server->disconnected;
+ dbus_bool_t retval;
+
+ _dbus_return_val_if_fail (server != NULL, FALSE);
+
+ SERVER_LOCK (server);
+ retval = !server->disconnected;
+ SERVER_UNLOCK (server);
+
+ return retval;
+}
+
+/**
+ * Returns the address of the server, as a newly-allocated
+ * string which must be freed by the caller.
+ *
+ * @param server the server
+ * @returns the address or #NULL if no memory
+ */
+char*
+dbus_server_get_address (DBusServer *server)
+{
+ char *retval;
+
+ _dbus_return_val_if_fail (server != NULL, NULL);
+
+ SERVER_LOCK (server);
+ retval = _dbus_strdup (server->address);
+ SERVER_UNLOCK (server);
+
+ return retval;
}
/**
void *data,
DBusFreeFunction free_data_function)
{
- if (server->new_connection_free_data_function != NULL)
- (* server->new_connection_free_data_function) (server->new_connection_data);
+ DBusFreeFunction old_free_function;
+ void *old_data;
+
+ _dbus_return_if_fail (server != NULL);
+
+ SERVER_LOCK (server);
+ old_free_function = server->new_connection_free_data_function;
+ old_data = server->new_connection_data;
server->new_connection_function = function;
server->new_connection_data = data;
server->new_connection_free_data_function = free_data_function;
+ SERVER_UNLOCK (server);
+
+ if (old_free_function != NULL)
+ (* old_free_function) (old_data);
}
/**
void *data,
DBusFreeFunction free_data_function)
{
- return _dbus_watch_list_set_functions (server->watches,
- add_function,
- remove_function,
- toggled_function,
- data,
- free_data_function);
+ dbus_bool_t result;
+ DBusWatchList *watches;
+
+ _dbus_return_val_if_fail (server != NULL, FALSE);
+
+ SERVER_LOCK (server);
+ watches = server->watches;
+ server->watches = NULL;
+ if (watches)
+ {
+ SERVER_UNLOCK (server);
+ result = _dbus_watch_list_set_functions (watches,
+ add_function,
+ remove_function,
+ toggled_function,
+ data,
+ free_data_function);
+ SERVER_LOCK (server);
+ }
+ else
+ {
+ _dbus_warn ("Re-entrant call to %s\n", _DBUS_FUNCTION_NAME);
+ result = FALSE;
+ }
+ server->watches = watches;
+ SERVER_UNLOCK (server);
+
+ return result;
}
/**
void *data,
DBusFreeFunction free_data_function)
{
- return _dbus_timeout_list_set_functions (server->timeouts,
- add_function, remove_function,
- toggled_function,
- data, free_data_function);
+ dbus_bool_t result;
+ DBusTimeoutList *timeouts;
+
+ _dbus_return_val_if_fail (server != NULL, FALSE);
+
+ SERVER_LOCK (server);
+ timeouts = server->timeouts;
+ server->timeouts = NULL;
+ if (timeouts)
+ {
+ SERVER_UNLOCK (server);
+ result = _dbus_timeout_list_set_functions (timeouts,
+ add_function,
+ remove_function,
+ toggled_function,
+ data,
+ free_data_function);
+ SERVER_LOCK (server);
+ }
+ else
+ {
+ _dbus_warn ("Re-entrant call to %s\n", _DBUS_FUNCTION_NAME);
+ result = FALSE;
+ }
+ server->timeouts = timeouts;
+ SERVER_UNLOCK (server);
+
+ return result;
}
/**
- * Called to notify the server when a previously-added watch
- * is ready for reading or writing, or has an exception such
- * as a hangup.
- *
- * If this function returns #FALSE, then the file descriptor may still
- * be ready for reading or writing, but more memory is needed in order
- * to do the reading or writing. If you ignore the #FALSE return, your
- * application may spin in a busy loop on the file descriptor until
- * memory becomes available, but nothing more catastrophic should
- * happen.
+ * Sets the authentication mechanisms that this server offers
+ * to clients, as a list of SASL mechanisms. This function
+ * only affects connections created *after* it is called.
+ * Pass #NULL instead of an array to use all available mechanisms.
*
- * @param server the server.
- * @param watch the watch.
- * @param condition the current condition of the file descriptors being watched.
+ * @param server the server
+ * @param mechanisms #NULL-terminated array of mechanisms
+ * @returns #FALSE if no memory
*/
dbus_bool_t
-dbus_server_handle_watch (DBusServer *server,
- DBusWatch *watch,
- unsigned int condition)
+dbus_server_set_auth_mechanisms (DBusServer *server,
+ const char **mechanisms)
{
- _dbus_assert (server->vtable->handle_watch != NULL);
+ char **copy;
- _dbus_watch_sanitize_condition (watch, &condition);
-
- return (* server->vtable->handle_watch) (server, watch, condition);
-}
+ _dbus_return_val_if_fail (server != NULL, FALSE);
-/**
- * Sets the maximum number of connections that can be open at one
- * time for this server. If the maximum is reached, and another
- * client tries to connect, then the oldest unauthenticated client
- * will be dropped. If no unauthenticated client exists, then
- * the new connection will be refused.
- *
- * If the maximum is set to a number lower than the current
- * number of connections, no current connections are
- * disconnected.
- *
- * @todo honoring max_connections has not been implemented
- * yet. The only real work involved is keeping a list
- * of live connections on the DBusServer so the oldest
- * unauthenticated client can be located when required.
- *
- * @todo for a systemwide daemon, we need a max number of connections
- * per user, since any user can authenticate a bunch of connections
- * and create a DOS.
- *
- * @todo a single process might listen on multiple mechanisms
- * (multiple DBusServer) and might want the max connections
- * value to span all those servers. Should consider
- * changing the API accordingly, though I'm inclined to
- * punt this to the app that wants to do it instead of
- * putting it in the library.
- *
- * @param server the server
- * @param max_connections maximum number of connections allowed
- */
-void
-dbus_server_set_max_connections (DBusServer *server,
- int max_connections)
-{
- server->max_connections = max_connections;
-}
+ SERVER_LOCK (server);
+
+ if (mechanisms != NULL)
+ {
+ copy = _dbus_dup_string_array (mechanisms);
+ if (copy == NULL)
+ return FALSE;
+ }
+ else
+ copy = NULL;
-/**
- * Gets the maximum number of connections that can be active
- * at a time for this server.
- *
- * @param server the server
- * @returns maximum number of connections at once
- */
-int
-dbus_server_get_max_connections (DBusServer *server)
-{
- return server->max_connections;
-}
+ dbus_free_string_array (server->auth_mechanisms);
+ server->auth_mechanisms = copy;
-/**
- * Gets the number of #DBusConnection to this server that
- * have not yet been finalized. i.e. all #DBusConnection that
- * were passed to #DBusNewConnectionFunction and have not yet been
- * finalized will count in this total.
- *
- * @param server the server
- * @returns the number of connections
- */
-int
-dbus_server_get_n_connections (DBusServer *server)
-{
- return _dbus_counter_get_value (server->connection_counter);
+ SERVER_UNLOCK (server);
+
+ return TRUE;
}
static DBusDataSlotAllocator slot_allocator;
-
-/**
- * Initialize the mutex used for #DBusConnection data
- * slot reservations.
- *
- * @returns the mutex
- */
-DBusMutex *
-_dbus_server_slots_init_lock (void)
-{
- if (!_dbus_data_slot_allocator_init (&slot_allocator))
- return NULL;
- else
- return slot_allocator.lock;
-}
+_DBUS_DEFINE_GLOBAL_LOCK (server_slots);
/**
* Allocates an integer ID to be used for storing application-specific
* data on any DBusServer. The allocated ID may then be used
* with dbus_server_set_data() and dbus_server_get_data().
- * If allocation fails, -1 is returned. Again, the allocated
- * slot is global, i.e. all DBusServer objects will
- * have a slot with the given integer ID reserved.
+ * The slot must be initialized with -1. If a nonnegative
+ * slot is passed in, the refcount is incremented on that
+ * slot, rather than creating a new slot.
+ *
+ * The allocated slot is global, i.e. all DBusServer objects will have
+ * a slot with the given integer ID reserved.
*
- * @returns -1 on failure, otherwise the data slot ID
+ * @param slot_p address of global variable storing the slot ID
+ * @returns #FALSE on no memory
*/
-int
-dbus_server_allocate_data_slot (void)
+dbus_bool_t
+dbus_server_allocate_data_slot (dbus_int32_t *slot_p)
{
- return _dbus_data_slot_allocator_alloc (&slot_allocator);
+ return _dbus_data_slot_allocator_alloc (&slot_allocator,
+ (DBusMutex **)&_DBUS_LOCK_NAME (server_slots),
+ slot_p);
}
/**
* but may not be retrieved (and may only be replaced
* if someone else reallocates the slot).
*
- * @param slot the slot to deallocate
+ * @param slot_p address of the slot to deallocate
*/
void
-dbus_server_free_data_slot (int slot)
+dbus_server_free_data_slot (dbus_int32_t *slot_p)
{
- _dbus_data_slot_allocator_free (&slot_allocator, slot);
+ _dbus_return_if_fail (*slot_p >= 0);
+
+ _dbus_data_slot_allocator_free (&slot_allocator, slot_p);
}
/**
* @returns #TRUE if there was enough memory to store the data
*/
dbus_bool_t
-dbus_server_set_data (DBusServer *server,
+dbus_server_set_data (DBusServer *server,
int slot,
void *data,
DBusFreeFunction free_data_func)
void *old_data;
dbus_bool_t retval;
-#if 0
- dbus_mutex_lock (server->mutex);
-#endif
+ _dbus_return_val_if_fail (server != NULL, FALSE);
+
+ SERVER_LOCK (server);
retval = _dbus_data_slot_list_set (&slot_allocator,
&server->slot_list,
slot, data, free_data_func,
&old_free_func, &old_data);
-#if 0
- dbus_mutex_unlock (server->mutex);
-#endif
+
+ SERVER_UNLOCK (server);
if (retval)
{
*/
void*
dbus_server_get_data (DBusServer *server,
- int slot)
+ int slot)
{
void *res;
+
+ _dbus_return_val_if_fail (server != NULL, NULL);
-#if 0
- dbus_mutex_lock (server->mutex);
-#endif
+ SERVER_LOCK (server);
res = _dbus_data_slot_list_get (&slot_allocator,
&server->slot_list,
slot);
-#if 0
- dbus_mutex_unlock (server->mutex);
-#endif
+ SERVER_UNLOCK (server);
return res;
}
/** @} */
+#ifdef DBUS_BUILD_TESTS
+#include "dbus-test.h"
+
+dbus_bool_t
+_dbus_server_test (void)
+{
+ const char *valid_addresses[] = {
+ "tcp:port=1234",
+ "unix:path=./boogie",
+ "tcp:host=localhost,port=1234",
+ "tcp:host=localhost,port=1234;tcp:port=5678",
+ "tcp:port=1234;unix:path=./boogie",
+ };
+
+ DBusServer *server;
+ int i;
+
+ for (i = 0; i < _DBUS_N_ELEMENTS (valid_addresses); i++)
+ {
+ 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_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], &error);
+ if (server == NULL)
+ {
+ _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);
+ }
+
+ return TRUE;
+}
+
+#endif /* DBUS_BUILD_TESTS */