25#include "dbus-server.h"
26#include "dbus-server-unix.h"
27#include "dbus-server-socket.h"
28#include "dbus-string.h"
29#ifdef DBUS_ENABLE_EMBEDDED_TESTS
30#include "dbus-server-debug-pipe.h"
32#include "dbus-address.h"
33#include "dbus-protocol.h"
56#ifndef _dbus_server_trace_ref
63 static int enabled = -1;
65 _dbus_trace_ref (
"DBusServer", server, old_refcount, new_refcount, why,
66 "DBUS_SERVER_TRACE", &enabled);
74copy_address_with_guid_appended (
const DBusString *address,
84 _dbus_string_get_length (&with_guid)) ||
87 &with_guid, _dbus_string_get_length (&with_guid)))
119#ifdef DBUS_DISABLE_ASSERT
136 _DBUS_SET_OOM (error);
146 server->
address = copy_address_with_guid_appended (address,
165 _dbus_verbose (
"Initialized server on address %s\n", server->
address);
170 _DBUS_SET_OOM (error);
206#ifndef DBUS_DISABLE_CHECKS
251 HAVE_LOCK_CHECK (server);
263 SERVER_UNLOCK (server);
266 retval = (* add_function) (watches, watch);
267 else if (remove_function)
270 (* remove_function) (watches, watch);
275 (* toggle_function) (watches, watch, enabled);
278 SERVER_LOCK (server);
299 HAVE_LOCK_CHECK (server);
300 return protected_change_watch (server, watch,
315 HAVE_LOCK_CHECK (server);
316 protected_change_watch (server, watch,
359 HAVE_LOCK_CHECK (server);
370 SERVER_UNLOCK (server);
373 retval = (* add_function) (timeouts, timeout);
374 else if (remove_function)
377 (* remove_function) (timeouts, timeout);
382 (* toggle_function) (timeouts, timeout, enabled);
385 SERVER_LOCK (server);
408 return protected_change_timeout (server, timeout,
423 protected_change_timeout (server, timeout,
443 protected_change_timeout (server, timeout,
458 dbus_int32_t old_refcount;
461 HAVE_LOCK_CHECK (server);
465 _dbus_server_trace_ref (server, old_refcount, old_refcount + 1,
477 dbus_int32_t old_refcount;
483 HAVE_LOCK_CHECK (server);
488 _dbus_server_trace_ref (server, old_refcount, old_refcount - 1,
491 if (old_refcount == 1)
495 SERVER_UNLOCK (server);
531#ifdef DBUS_ENABLE_EMBEDDED_TESTS
532 , { _dbus_server_listen_debug_pipe }
566 _dbus_return_val_if_fail (address !=
NULL,
NULL);
567 _dbus_return_val_if_error_is_set (error,
NULL);
573 handled_once =
FALSE;
575 for (i = 0; i < len; i++)
581 DBusServerListenResult result;
584 result = (* listen_funcs[j].func) (entries[i],
588 if (result == DBUS_SERVER_LISTEN_OK)
591 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
595 else if (result == DBUS_SERVER_LISTEN_ADDRESS_ALREADY_USED)
598 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
601 "Address '%s' already used",
606 else if (result == DBUS_SERVER_LISTEN_BAD_ADDRESS)
609 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
614 else if (result == DBUS_SERVER_LISTEN_NOT_HANDLED)
617 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
621 else if (result == DBUS_SERVER_LISTEN_DID_NOT_CONNECT)
624 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
641 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
648 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
652 "Unknown address type '%s'",
657 "Empty address '%s'",
681 _DBUS_ASSERT_ERROR_IS_CLEAR (&first_connect_error);
682 _DBUS_ASSERT_ERROR_IS_SET (error);
689 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
703 dbus_int32_t old_refcount;
705 _dbus_return_val_if_fail (server !=
NULL,
NULL);
709#ifndef DBUS_DISABLE_CHECKS
710 if (_DBUS_UNLIKELY (old_refcount <= 0))
713 _dbus_warn_return_if_fail (_DBUS_FUNCTION_NAME,
"old_refcount > 0",
719 _dbus_server_trace_ref (server, old_refcount, old_refcount + 1,
"ref");
735 dbus_int32_t old_refcount;
739 _dbus_return_if_fail (server !=
NULL);
743#ifndef DBUS_DISABLE_CHECKS
744 if (_DBUS_UNLIKELY (old_refcount <= 0))
754 _dbus_warn_return_if_fail (_DBUS_FUNCTION_NAME,
"old_refcount > 0",
760 _dbus_server_trace_ref (server, old_refcount, old_refcount - 1,
"unref");
762 if (old_refcount == 1)
774_dbus_server_disconnect_unlocked (
DBusServer *server)
798 _dbus_return_if_fail (server !=
NULL);
801 SERVER_LOCK (server);
803 _dbus_server_disconnect_unlocked (server);
805 SERVER_UNLOCK (server);
819 _dbus_return_val_if_fail (server !=
NULL,
FALSE);
821 SERVER_LOCK (server);
823 SERVER_UNLOCK (server);
840 _dbus_return_val_if_fail (server !=
NULL,
NULL);
842 SERVER_LOCK (server);
844 SERVER_UNLOCK (server);
876 _dbus_return_val_if_fail (server !=
NULL,
NULL);
878 SERVER_LOCK (server);
881 SERVER_UNLOCK (server);
915 _dbus_return_if_fail (server !=
NULL);
917 SERVER_LOCK (server);
924 SERVER_UNLOCK (server);
926 if (old_free_function !=
NULL)
927 (* old_free_function) (old_data);
957 _dbus_return_val_if_fail (server !=
NULL,
FALSE);
959 SERVER_LOCK (server);
964 SERVER_UNLOCK (server);
971 SERVER_LOCK (server);
979 SERVER_UNLOCK (server);
1010 _dbus_return_val_if_fail (server !=
NULL,
FALSE);
1012 SERVER_LOCK (server);
1017 SERVER_UNLOCK (server);
1023 free_data_function);
1024 SERVER_LOCK (server);
1032 SERVER_UNLOCK (server);
1052 const char **mechanisms)
1056 _dbus_return_val_if_fail (server !=
NULL,
FALSE);
1058 SERVER_LOCK (server);
1060 if (mechanisms !=
NULL)
1065 SERVER_UNLOCK (server);
1075 SERVER_UNLOCK (server);
1118 _dbus_return_if_fail (*slot_p >= 0);
1146 _dbus_return_val_if_fail (server !=
NULL,
FALSE);
1148 SERVER_LOCK (server);
1152 slot, data, free_data_func,
1153 &old_free_func, &old_data);
1156 SERVER_UNLOCK (server);
1162 (* old_free_func) (old_data);
1182 _dbus_return_val_if_fail (server !=
NULL,
NULL);
1184 SERVER_LOCK (server);
1190 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_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_I...
dbus_bool_t _dbus_string_copy_data(const DBusString *str, char **data_return)
Copies the data from the string into a char*.
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_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
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.