27#include "dbus-server.h"
28#include "dbus-server-socket.h"
29#include "dbus-string.h"
30#ifdef DBUS_ENABLE_EMBEDDED_TESTS
31#include "dbus-server-debug-pipe.h"
33#include "dbus-address.h"
34#include "dbus-protocol.h"
57#ifndef _dbus_server_trace_ref
64 static int enabled = -1;
66 _dbus_trace_ref (
"DBusServer", server, old_refcount, new_refcount, why,
67 "DBUS_SERVER_TRACE", &enabled);
75copy_address_with_guid_appended (
const DBusString *address,
120#ifdef DBUS_DISABLE_ASSERT
137 _DBUS_SET_OOM (error);
147 server->
address = copy_address_with_guid_appended (address,
166 _dbus_verbose (
"Initialized server on address %s\n", server->
address);
171 _DBUS_SET_OOM (error);
207#ifndef DBUS_DISABLE_CHECKS
239 dbus_bool_t enabled);
252 HAVE_LOCK_CHECK (server);
264 SERVER_UNLOCK (server);
267 retval = (* add_function) (watches, watch);
268 else if (remove_function)
271 (* remove_function) (watches, watch);
276 (* toggle_function) (watches, watch, enabled);
279 SERVER_LOCK (server);
300 HAVE_LOCK_CHECK (server);
301 return protected_change_watch (server, watch,
316 HAVE_LOCK_CHECK (server);
317 protected_change_watch (server, watch,
346 dbus_bool_t enabled);
360 HAVE_LOCK_CHECK (server);
371 SERVER_UNLOCK (server);
374 retval = (* add_function) (timeouts, timeout);
375 else if (remove_function)
378 (* remove_function) (timeouts, timeout);
383 (* toggle_function) (timeouts, timeout, enabled);
386 SERVER_LOCK (server);
409 return protected_change_timeout (server, timeout,
424 protected_change_timeout (server, timeout,
444 protected_change_timeout (server, timeout,
459 dbus_int32_t old_refcount;
462 HAVE_LOCK_CHECK (server);
466 _dbus_server_trace_ref (server, old_refcount, old_refcount + 1,
478 dbus_int32_t old_refcount;
484 HAVE_LOCK_CHECK (server);
489 _dbus_server_trace_ref (server, old_refcount, old_refcount - 1,
492 if (old_refcount == 1)
496 SERVER_UNLOCK (server);
533#ifdef DBUS_ENABLE_EMBEDDED_TESTS
534 , { _dbus_server_listen_debug_pipe }
566 dbus_bool_t handled_once;
568 _dbus_return_val_if_fail (address !=
NULL,
NULL);
569 _dbus_return_val_if_error_is_set (error,
NULL);
575 handled_once =
FALSE;
577 for (i = 0; i < len; i++)
583 DBusServerListenResult result;
586 result = (* listen_funcs[j].func) (entries[i],
590 if (result == DBUS_SERVER_LISTEN_OK)
593 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
597 else if (result == DBUS_SERVER_LISTEN_ADDRESS_ALREADY_USED)
600 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
603 "Address '%s' already used",
608 else if (result == DBUS_SERVER_LISTEN_BAD_ADDRESS)
611 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
616 else if (result == DBUS_SERVER_LISTEN_NOT_HANDLED)
619 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
623 else if (result == DBUS_SERVER_LISTEN_DID_NOT_CONNECT)
626 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
643 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
650 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
654 "Unknown address type '%s'",
659 "Empty address '%s'",
683 _DBUS_ASSERT_ERROR_IS_CLEAR (&first_connect_error);
684 _DBUS_ASSERT_ERROR_IS_SET (error);
691 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
705 dbus_int32_t old_refcount;
707 _dbus_return_val_if_fail (server !=
NULL,
NULL);
711#ifndef DBUS_DISABLE_CHECKS
712 if (_DBUS_UNLIKELY (old_refcount <= 0))
715 _dbus_warn_return_if_fail (_DBUS_FUNCTION_NAME,
"old_refcount > 0",
721 _dbus_server_trace_ref (server, old_refcount, old_refcount + 1,
"ref");
737 dbus_int32_t old_refcount;
741 _dbus_return_if_fail (server !=
NULL);
745#ifndef DBUS_DISABLE_CHECKS
746 if (_DBUS_UNLIKELY (old_refcount <= 0))
756 _dbus_warn_return_if_fail (_DBUS_FUNCTION_NAME,
"old_refcount > 0",
762 _dbus_server_trace_ref (server, old_refcount, old_refcount - 1,
"unref");
764 if (old_refcount == 1)
776_dbus_server_disconnect_unlocked (
DBusServer *server)
800 _dbus_return_if_fail (server !=
NULL);
803 SERVER_LOCK (server);
805 _dbus_server_disconnect_unlocked (server);
807 SERVER_UNLOCK (server);
821 _dbus_return_val_if_fail (server !=
NULL,
FALSE);
823 SERVER_LOCK (server);
825 SERVER_UNLOCK (server);
842 _dbus_return_val_if_fail (server !=
NULL,
NULL);
844 SERVER_LOCK (server);
846 SERVER_UNLOCK (server);
878 _dbus_return_val_if_fail (server !=
NULL,
NULL);
880 SERVER_LOCK (server);
883 SERVER_UNLOCK (server);
917 _dbus_return_if_fail (server !=
NULL);
919 SERVER_LOCK (server);
926 SERVER_UNLOCK (server);
928 if (old_free_function !=
NULL)
929 (* old_free_function) (old_data);
959 _dbus_return_val_if_fail (server !=
NULL,
FALSE);
961 SERVER_LOCK (server);
966 SERVER_UNLOCK (server);
973 SERVER_LOCK (server);
981 SERVER_UNLOCK (server);
1012 _dbus_return_val_if_fail (server !=
NULL,
FALSE);
1014 SERVER_LOCK (server);
1019 SERVER_UNLOCK (server);
1025 free_data_function);
1026 SERVER_LOCK (server);
1034 SERVER_UNLOCK (server);
1054 const char **mechanisms)
1058 _dbus_return_val_if_fail (server !=
NULL,
FALSE);
1060 SERVER_LOCK (server);
1062 if (mechanisms !=
NULL)
1067 SERVER_UNLOCK (server);
1077 SERVER_UNLOCK (server);
1120 _dbus_return_if_fail (*slot_p >= 0);
1148 _dbus_return_val_if_fail (server !=
NULL,
FALSE);
1150 SERVER_LOCK (server);
1154 slot, data, free_data_func,
1155 &old_free_func, &old_data);
1158 SERVER_UNLOCK (server);
1164 (* old_free_func) (old_data);
1184 _dbus_return_val_if_fail (server !=
NULL,
NULL);
1186 SERVER_LOCK (server);
1192 SERVER_UNLOCK (server);
void dbus_address_entries_free(DBusAddressEntry **entries)
Frees a NULL-terminated array of address entries.
dbus_bool_t dbus_parse_address(const char *address, DBusAddressEntry ***entry_result, int *array_len, DBusError *error)
Parses an address string of the form:
const char * dbus_address_entry_get_method(DBusAddressEntry *entry)
Returns the method string of an address entry.
void(* DBusWatchToggledFunction)(DBusWatch *watch, void *data)
Called when dbus_watch_get_enabled() may return a different value than it did before.
dbus_bool_t(* DBusAddWatchFunction)(DBusWatch *watch, void *data)
Called when libdbus needs a new watch to be monitored by the main loop.
void(* DBusTimeoutToggledFunction)(DBusTimeout *timeout, void *data)
Called when dbus_timeout_get_enabled() may return a different value than it did before.
dbus_bool_t(* DBusAddTimeoutFunction)(DBusTimeout *timeout, void *data)
Called when libdbus needs a new timeout to be monitored by the main loop.
void(* DBusRemoveWatchFunction)(DBusWatch *watch, void *data)
Called when libdbus no longer needs a watch to be monitored by the main loop.
void(* DBusRemoveTimeoutFunction)(DBusTimeout *timeout, void *data)
Called when libdbus no longer needs a timeout to be monitored by the main loop.
void _dbus_data_slot_allocator_free(DBusDataSlotAllocator *allocator, dbus_int32_t *slot_id_p)
Deallocates an ID previously allocated with _dbus_data_slot_allocator_alloc().
void _dbus_data_slot_list_init(DBusDataSlotList *list)
Initializes a slot list.
void _dbus_data_slot_list_free(DBusDataSlotList *list)
Frees the data slot list and all data slots contained in it, calling application-provided free functi...
void * _dbus_data_slot_list_get(DBusDataSlotAllocator *allocator, DBusDataSlotList *list, int slot)
Retrieves data previously set with _dbus_data_slot_list_set_data().
dbus_bool_t _dbus_data_slot_list_set(DBusDataSlotAllocator *allocator, DBusDataSlotList *list, int slot, void *data, DBusFreeFunction free_data_func, DBusFreeFunction *old_free_func, void **old_data)
Stores a pointer in the data slot list, along with an optional function to be used for freeing the da...
dbus_bool_t _dbus_data_slot_allocator_alloc(DBusDataSlotAllocator *allocator, dbus_int32_t *slot_id_p)
Allocates an integer ID to be used for storing data in a DBusDataSlotList.
#define DBUS_ERROR_INIT
Expands to a suitable initializer for a DBusError on the stack.
void dbus_move_error(DBusError *src, DBusError *dest)
Moves an error src into dest, freeing src and overwriting dest.
void dbus_set_error(DBusError *error, const char *name, const char *format,...)
Assigns an error name and message to a DBusError.
void dbus_error_free(DBusError *error)
Frees an error that's been set (or just initialized), then reinitializes the error as in dbus_error_i...
dbus_bool_t dbus_error_is_set(const DBusError *error)
Checks whether an error occurred (the error is set).
#define _dbus_assert_not_reached(explanation)
Aborts with an error message if called.
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
dbus_bool_t _dbus_generate_uuid(DBusGUID *uuid, DBusError *error)
Generates a new UUID.
void _dbus_warn_check_failed(const char *format,...)
Prints a "critical" warning to stderr when an assertion fails; differs from _dbus_warn primarily in t...
char * _dbus_strdup(const char *str)
Duplicates a string.
#define _DBUS_N_ELEMENTS(array)
Computes the number of elements in a fixed-size array using sizeof().
#define _DBUS_LOCK_NAME(name)
Expands to name of a global lock variable.
dbus_bool_t _dbus_uuid_encode(const DBusGUID *uuid, DBusString *encoded)
Hex-encode a UUID.
char ** _dbus_dup_string_array(const char **array)
Duplicates a string array.
#define NULL
A null pointer, defined appropriately for C or C++.
#define TRUE
Expands to "1".
#define FALSE
Expands to "0".
void(* DBusFreeFunction)(void *memory)
The type of a function which frees a block of memory.
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
void dbus_free_string_array(char **str_array)
Frees a NULL-terminated array of strings.
#define DBUS_ERROR_ADDRESS_IN_USE
Can't bind a socket since its address is in use (i.e.
#define DBUS_ERROR_BAD_ADDRESS
A D-Bus bus address was malformed.
dbus_bool_t _dbus_server_add_watch(DBusServer *server, DBusWatch *watch)
Adds a watch for this server, chaining out to application-provided watch handlers.
void(* DBusWatchRemoveFunction)(DBusWatchList *list, DBusWatch *watch)
Function to be called in protected_change_watch() with refcount held.
void _dbus_server_remove_watch(DBusServer *server, DBusWatch *watch)
Removes a watch previously added with _dbus_server_remove_watch().
void _dbus_server_remove_timeout(DBusServer *server, DBusTimeout *timeout)
Removes a timeout previously added with _dbus_server_add_timeout().
void _dbus_server_unref_unlocked(DBusServer *server)
Like dbus_server_unref() but does not acquire the lock (must already be held)
void _dbus_server_toggle_timeout(DBusServer *server, DBusTimeout *timeout, dbus_bool_t enabled)
Toggles a timeout and notifies app via server's DBusTimeoutToggledFunction if available.
void(* DBusTimeoutToggleFunction)(DBusTimeoutList *list, DBusTimeout *timeout, dbus_bool_t enabled)
Function to be called in protected_change_timeout() with refcount held.
void(* DBusTimeoutRemoveFunction)(DBusTimeoutList *list, DBusTimeout *timeout)
Function to be called in protected_change_timeout() with refcount held.
dbus_bool_t _dbus_server_add_timeout(DBusServer *server, DBusTimeout *timeout)
Adds a timeout for this server, chaining out to application-provided timeout handlers.
dbus_bool_t _dbus_server_init_base(DBusServer *server, const DBusServerVTable *vtable, const DBusString *address, DBusError *error)
Initializes the members of the DBusServer base class.
void _dbus_server_finalize_base(DBusServer *server)
Finalizes the members of the DBusServer base class.
void _dbus_server_ref_unlocked(DBusServer *server)
Like dbus_server_ref() but does not acquire the lock (must already be held)
dbus_bool_t(* DBusTimeoutAddFunction)(DBusTimeoutList *list, DBusTimeout *timeout)
Function to be called in protected_change_timeout() with refcount held.
void(* DBusWatchToggleFunction)(DBusWatchList *list, DBusWatch *watch, dbus_bool_t enabled)
Function to be called in protected_change_watch() with refcount held.
void _dbus_server_toggle_all_watches(DBusServer *server, dbus_bool_t enabled)
Toggles all watch and notifies app via server's DBusWatchToggledFunction if available.
dbus_bool_t(* DBusWatchAddFunction)(DBusWatchList *list, DBusWatch *watch)
Function to be called in protected_change_watch() with refcount held.
DBusServerListenResult _dbus_server_listen_socket(DBusAddressEntry *entry, DBusServer **server_p, DBusError *error)
Tries to interpret the address entry for various socket-related addresses (well, currently only tcp a...
DBusServerListenResult _dbus_server_listen_unix_socket(DBusAddressEntry *entry, DBusServer **server_p, DBusError *error)
Tries to interpret the address entry for UNIX socket addresses.
DBusServerListenResult _dbus_server_listen_platform_specific(DBusAddressEntry *entry, DBusServer **server_p, DBusError *error)
Tries to interpret the address entry in a platform-specific way, creating a platform-specific server ...
dbus_bool_t dbus_server_allocate_data_slot(dbus_int32_t *slot_p)
Allocates an integer ID to be used for storing application-specific data on any DBusServer.
void dbus_server_disconnect(DBusServer *server)
Releases the server's address and stops listening for new clients.
dbus_bool_t dbus_server_set_auth_mechanisms(DBusServer *server, const char **mechanisms)
Sets the authentication mechanisms that this server offers to clients, as a NULL-terminated array of ...
char * dbus_server_get_id(DBusServer *server)
Returns the unique ID of the server, as a newly-allocated string which must be freed by the caller.
DBusServer * dbus_server_listen(const char *address, DBusError *error)
Listens for new connections on the given address.
char * dbus_server_get_address(DBusServer *server)
Returns the address of the server, as a newly-allocated string which must be freed by the caller.
dbus_bool_t dbus_server_get_is_connected(DBusServer *server)
Returns TRUE if the server is still listening for new connections.
void dbus_server_unref(DBusServer *server)
Decrements the reference count of a DBusServer.
void dbus_server_set_new_connection_function(DBusServer *server, DBusNewConnectionFunction function, void *data, DBusFreeFunction free_data_function)
Sets a function to be used for handling new connections.
dbus_bool_t dbus_server_set_watch_functions(DBusServer *server, DBusAddWatchFunction add_function, DBusRemoveWatchFunction remove_function, DBusWatchToggledFunction toggled_function, void *data, DBusFreeFunction free_data_function)
Sets the watch functions for the server.
dbus_bool_t dbus_server_set_data(DBusServer *server, int slot, void *data, DBusFreeFunction free_data_func)
Stores a pointer on a DBusServer, along with an optional function to be used for freeing the data whe...
DBusServer * dbus_server_ref(DBusServer *server)
Increments the reference count of a DBusServer.
void * dbus_server_get_data(DBusServer *server, int slot)
Retrieves data previously set with dbus_server_set_data().
void dbus_server_free_data_slot(dbus_int32_t *slot_p)
Deallocates a global ID for server data slots.
dbus_bool_t dbus_server_set_timeout_functions(DBusServer *server, DBusAddTimeoutFunction add_function, DBusRemoveTimeoutFunction remove_function, DBusTimeoutToggledFunction toggled_function, void *data, DBusFreeFunction free_data_function)
Sets the timeout functions for the server.
void(* DBusNewConnectionFunction)(DBusServer *server, DBusConnection *new_connection, void *data)
Called when a new connection to the server is available.
dbus_bool_t _dbus_string_append(DBusString *str, const char *buffer)
Appends a nul-terminated C-style string to a DBusString.
dbus_bool_t _dbus_string_init(DBusString *str)
Initializes a string.
dbus_bool_t _dbus_string_copy(const DBusString *source, int start, DBusString *dest, int insert_at)
Like _dbus_string_move(), but does not delete the section of the source string that's copied to the d...
dbus_bool_t _dbus_string_steal_data(DBusString *str, char **data_return)
Like _dbus_string_get_data(), but removes the gotten data from the original string.
void _dbus_string_free(DBusString *str)
Frees a string created by _dbus_string_init(), and fills it with the same contents as _DBUS_STRING_IN...
dbus_bool_t _dbus_string_copy_data(const DBusString *str, char **data_return)
Copies the data from the string into a char*.
int _dbus_string_get_length(const DBusString *str)
Gets the length of a string (not including nul termination).
dbus_int32_t _dbus_atomic_dec(DBusAtomic *atomic)
Atomically decrement an integer.
dbus_int32_t _dbus_atomic_inc(DBusAtomic *atomic)
Atomically increments an integer.
void _dbus_rmutex_new_at_location(DBusRMutex **location_p)
Creates a new mutex or creates a no-op mutex if threads are not initialized.
void _dbus_rmutex_free_at_location(DBusRMutex **location_p)
Frees a DBusRMutex; does nothing if passed a NULL pointer.
dbus_bool_t _dbus_timeout_list_add_timeout(DBusTimeoutList *timeout_list, DBusTimeout *timeout)
Adds a new timeout to the timeout list, invoking the application DBusAddTimeoutFunction if appropriat...
void _dbus_timeout_list_free(DBusTimeoutList *timeout_list)
Frees a DBusTimeoutList.
void _dbus_timeout_list_toggle_timeout(DBusTimeoutList *timeout_list, DBusTimeout *timeout, dbus_bool_t enabled)
Sets a timeout to the given enabled state, invoking the application's DBusTimeoutToggledFunction if a...
DBusTimeoutList * _dbus_timeout_list_new(void)
Creates a new timeout list.
dbus_bool_t _dbus_timeout_list_set_functions(DBusTimeoutList *timeout_list, DBusAddTimeoutFunction add_function, DBusRemoveTimeoutFunction remove_function, DBusTimeoutToggledFunction toggled_function, void *data, DBusFreeFunction free_data_function)
Sets the timeout functions.
void _dbus_timeout_list_remove_timeout(DBusTimeoutList *timeout_list, DBusTimeout *timeout)
Removes a timeout from the timeout list, invoking the application's DBusRemoveTimeoutFunction if appr...
dbus_bool_t _dbus_watch_list_add_watch(DBusWatchList *watch_list, DBusWatch *watch)
Adds a new watch to the watch list, invoking the application DBusAddWatchFunction if appropriate.
DBusWatchList * _dbus_watch_list_new(void)
Creates a new watch list.
void _dbus_watch_list_free(DBusWatchList *watch_list)
Frees a DBusWatchList.
dbus_bool_t _dbus_watch_list_set_functions(DBusWatchList *watch_list, DBusAddWatchFunction add_function, DBusRemoveWatchFunction remove_function, DBusWatchToggledFunction toggled_function, void *data, DBusFreeFunction free_data_function)
Sets the watch functions.
void _dbus_watch_list_toggle_all_watches(DBusWatchList *watch_list, dbus_bool_t enabled)
Sets all watches to the given enabled state, invoking the application's DBusWatchToggledFunction if a...
void _dbus_watch_list_remove_watch(DBusWatchList *watch_list, DBusWatch *watch)
Removes a watch from the watch list, invoking the application's DBusRemoveWatchFunction if appropriat...
Internals of DBusAddressEntry.
An allocator that tracks a set of slot IDs.
Object representing an exception.
Virtual table to be implemented by all server "subclasses".
void(* disconnect)(DBusServer *server)
Disconnect this server.
void(* finalize)(DBusServer *server)
The finalize method must free the server.
Internals of DBusServer object.
dbus_bool_t published_address
flag which indicates that server has published its bus address.
DBusDataSlotList slot_list
Data stored by allocated integer ID.
char * address
Address this server is listening on.
DBusFreeFunction new_connection_free_data_function
Callback to invoke to free new_connection_data when server is finalized or data is replaced.
DBusAtomic refcount
Reference count.
DBusWatchList * watches
Our watches.
DBusGUID guid
Globally unique ID of server.
DBusString guid_hex
Hex-encoded version of GUID.
unsigned int disconnected
TRUE if we are disconnected.
DBusRMutex * mutex
Lock on the server object.
DBusNewConnectionFunction new_connection_function
Callback to invoke when a new connection is created.
const DBusServerVTable * vtable
Virtual methods for this instance.
void * new_connection_data
Data for new connection callback.
unsigned int have_server_lock
Does someone have the server mutex locked.
char ** auth_mechanisms
Array of allowed authentication mechanisms.
DBusTimeoutList * timeouts
Our timeouts.
DBusTimeoutList implementation details.
Internals of DBusTimeout.
DBusWatchList implementation details.
Implementation of DBusWatch.