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);
74 copy_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,
461 HAVE_LOCK_CHECK (server);
465 _dbus_server_trace_ref (server, old_refcount, old_refcount + 1,
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);
524 static const struct {
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)
600 "Address '%s' already used",
605 else if (result == DBUS_SERVER_LISTEN_BAD_ADDRESS)
608 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
613 else if (result == DBUS_SERVER_LISTEN_NOT_HANDLED)
616 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
620 else if (result == DBUS_SERVER_LISTEN_DID_NOT_CONNECT)
623 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
636 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
643 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
647 "Unknown address type '%s'",
652 "Empty address '%s'",
676 _DBUS_ASSERT_ERROR_IS_CLEAR (&first_connect_error);
677 _DBUS_ASSERT_ERROR_IS_SET (error);
683 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
699 _dbus_return_val_if_fail (server !=
NULL,
NULL);
703 #ifndef DBUS_DISABLE_CHECKS 704 if (_DBUS_UNLIKELY (old_refcount <= 0))
707 _dbus_warn_return_if_fail (_DBUS_FUNCTION_NAME,
"old_refcount > 0",
713 _dbus_server_trace_ref (server, old_refcount, old_refcount + 1,
"ref");
733 _dbus_return_if_fail (server !=
NULL);
737 #ifndef DBUS_DISABLE_CHECKS 738 if (_DBUS_UNLIKELY (old_refcount <= 0))
748 _dbus_warn_return_if_fail (_DBUS_FUNCTION_NAME,
"old_refcount > 0",
754 _dbus_server_trace_ref (server, old_refcount, old_refcount - 1,
"unref");
756 if (old_refcount == 1)
778 _dbus_return_if_fail (server !=
NULL);
781 SERVER_LOCK (server);
793 SERVER_UNLOCK (server);
807 _dbus_return_val_if_fail (server !=
NULL,
FALSE);
809 SERVER_LOCK (server);
811 SERVER_UNLOCK (server);
828 _dbus_return_val_if_fail (server !=
NULL,
NULL);
830 SERVER_LOCK (server);
832 SERVER_UNLOCK (server);
864 _dbus_return_val_if_fail (server !=
NULL,
NULL);
866 SERVER_LOCK (server);
869 SERVER_UNLOCK (server);
903 _dbus_return_if_fail (server !=
NULL);
905 SERVER_LOCK (server);
912 SERVER_UNLOCK (server);
914 if (old_free_function !=
NULL)
915 (* old_free_function) (old_data);
945 _dbus_return_val_if_fail (server !=
NULL,
FALSE);
947 SERVER_LOCK (server);
952 SERVER_UNLOCK (server);
959 SERVER_LOCK (server);
967 SERVER_UNLOCK (server);
998 _dbus_return_val_if_fail (server !=
NULL,
FALSE);
1000 SERVER_LOCK (server);
1005 SERVER_UNLOCK (server);
1011 free_data_function);
1012 SERVER_LOCK (server);
1020 SERVER_UNLOCK (server);
1040 const char **mechanisms)
1044 _dbus_return_val_if_fail (server !=
NULL,
FALSE);
1046 SERVER_LOCK (server);
1048 if (mechanisms !=
NULL)
1053 SERVER_UNLOCK (server);
1063 SERVER_UNLOCK (server);
1106 _dbus_return_if_fail (*slot_p >= 0);
1134 _dbus_return_val_if_fail (server !=
NULL,
FALSE);
1136 SERVER_LOCK (server);
1140 slot, data, free_data_func,
1141 &old_free_func, &old_data);
1144 SERVER_UNLOCK (server);
1150 (* old_free_func) (old_data);
1170 _dbus_return_val_if_fail (server !=
NULL,
NULL);
1172 SERVER_LOCK (server);
1178 SERVER_UNLOCK (server);
1185 #ifdef DBUS_ENABLE_EMBEDDED_TESTS 1186 #include "dbus-test.h" 1190 _dbus_server_test (
void)
1192 const char *valid_addresses[] = {
1194 "tcp:host=localhost,port=1234",
1195 "tcp:host=localhost,port=1234;tcp:port=5678",
1197 "unix:path=./boogie",
1198 "tcp:port=1234;unix:path=./boogie",
1224 if (strstr (address,
id) ==
NULL)
1226 _dbus_warn (
"server id '%s' is not in the server address '%s'",
dbus_bool_t _dbus_string_append(DBusString *str, const char *buffer)
Appends a nul-terminated C-style string to a DBusString.
Internals of DBusTimeout.
void dbus_server_free_data_slot(dbus_int32_t *slot_p)
Deallocates a global ID for server data slots.
dbus_bool_t _dbus_uuid_encode(const DBusGUID *uuid, DBusString *encoded)
Hex-encode a UUID.
dbus_bool_t _dbus_server_add_watch(DBusServer *server, DBusWatch *watch)
Adds a watch for this server, chaining out to application-provided watch handlers.
const char * message
public error message field
Implementation of DBusWatch.
#define NULL
A null pointer, defined appropriately for C or C++.
void(* DBusFreeFunction)(void *memory)
The type of a function which frees a block of memory.
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_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
void(* disconnect)(DBusServer *server)
Disconnect this server.
unsigned int disconnected
TRUE if we are disconnected.
void(* DBusWatchToggledFunction)(DBusWatch *watch, void *data)
Called when dbus_watch_get_enabled() may return a different value than it did before.
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...
void _dbus_timeout_list_free(DBusTimeoutList *timeout_list)
Frees a DBusTimeoutList.
dbus_bool_t _dbus_generate_uuid(DBusGUID *uuid, DBusError *error)
Generates a new UUID.
Internals of DBusServer object.
DBusTimeoutList * _dbus_timeout_list_new(void)
Creates a new timeout list.
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
#define DBUS_ERROR_INIT
Expands to a suitable initializer for a DBusError on the stack.
const char * dbus_address_entry_get_method(DBusAddressEntry *entry)
Returns the method string of an address entry.
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...
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...
void _dbus_watch_list_free(DBusWatchList *watch_list)
Frees a DBusWatchList.
void dbus_address_entries_free(DBusAddressEntry **entries)
Frees a NULL-terminated array of address entries.
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(* DBusTimeoutToggleFunction)(DBusTimeoutList *list, DBusTimeout *timeout, dbus_bool_t enabled)
Function to be called in protected_change_timeout() with refcount held.
DBusWatchList * _dbus_watch_list_new(void)
Creates a new watch list.
dbus_bool_t _dbus_string_init(DBusString *str)
Initializes a string.
void _dbus_server_ref_unlocked(DBusServer *server)
Like dbus_server_ref() but does not acquire the lock (must already be held)
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...
void(* DBusNewConnectionFunction)(DBusServer *server, DBusConnection *new_connection, void *data)
Called when a new connection to the server is available.
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 ...
DBusNewConnectionFunction new_connection_function
Callback to invoke when a new connection is created.
char ** auth_mechanisms
Array of allowed authentication mechanisms.
void(* DBusRemoveTimeoutFunction)(DBusTimeout *timeout, void *data)
Called when libdbus no longer needs a timeout to be monitored by the main loop.
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.
const DBusServerVTable * vtable
Virtual methods for this instance.
void(* DBusWatchToggleFunction)(DBusWatchList *list, DBusWatch *watch, dbus_bool_t enabled)
Function to be called in protected_change_watch() with refcount held.
void(* finalize)(DBusServer *server)
The finalize method must free the server.
DBusRMutex * mutex
Lock on the server object.
DBusServer * dbus_server_listen(const char *address, DBusError *error)
Listens for new connections on the given address.
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_server_toggle_timeout(DBusServer *server, DBusTimeout *timeout, dbus_bool_t enabled)
Toggles a timeout and notifies app via server's DBusTimeoutToggledFunction if available.
dbus_bool_t _dbus_string_copy_data(const DBusString *str, char **data_return)
Copies the data from the string into a char*.
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
void _dbus_server_unref_unlocked(DBusServer *server)
Like dbus_server_unref() but does not acquire the lock (must already be held)
DBusAtomic refcount
Reference count.
dbus_bool_t(* DBusAddTimeoutFunction)(DBusTimeout *timeout, void *data)
Called when libdbus needs a new timeout to be monitored by the main loop.
void _dbus_warn(const char *format,...)
Prints a warning message to stderr.
dbus_bool_t(* DBusTimeoutAddFunction)(DBusTimeoutList *list, DBusTimeout *timeout)
Function to be called in protected_change_timeout() with refcount held.
DBusTimeoutList * timeouts
Our timeouts.
dbus_int32_t _dbus_atomic_inc(DBusAtomic *atomic)
Atomically increments an integer.
DBusServer * dbus_server_ref(DBusServer *server)
Increments the reference count of a DBusServer.
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...
Internals of DBusAddressEntry.
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_server_remove_watch(DBusServer *server, DBusWatch *watch)
Removes a watch previously added with _dbus_server_remove_watch().
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.
Object representing an exception.
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.
#define DBUS_ERROR_BAD_ADDRESS
A D-Bus bus address was malformed.
void dbus_set_error(DBusError *error, const char *name, const char *format,...)
Assigns an error name and message to a DBusError.
void * dbus_server_get_data(DBusServer *server, int slot)
Retrieves data previously set with dbus_server_set_data().
void dbus_server_disconnect(DBusServer *server)
Releases the server's address and stops listening for new clients.
void _dbus_server_remove_timeout(DBusServer *server, DBusTimeout *timeout)
Removes a timeout previously added with _dbus_server_add_timeout().
void _dbus_server_finalize_base(DBusServer *server)
Finalizes the members of the DBusServer base class.
#define _DBUS_N_ELEMENTS(array)
Computes the number of elements in a fixed-size array using sizeof().
Virtual table to be implemented by all server "subclasses".
DBusWatchList * watches
Our watches.
void dbus_server_unref(DBusServer *server)
Decrements the reference count of a DBusServer.
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...
#define DBUS_ERROR_ADDRESS_IN_USE
Can't bind a socket since its address is in use (i.e.
void _dbus_string_free(DBusString *str)
Frees a string created by _dbus_string_init().
char ** _dbus_dup_string_array(const char **array)
Duplicates a string array.
DBusDataSlotList slot_list
Data stored by allocated integer ID.
void * _dbus_data_slot_list_get(DBusDataSlotAllocator *allocator, DBusDataSlotList *list, int slot)
Retrieves data previously set with _dbus_data_slot_list_set_data().
void(* DBusTimeoutToggledFunction)(DBusTimeout *timeout, void *data)
Called when dbus_timeout_get_enabled() may return a different value than it did before.
#define TRUE
Expands to "1".
void _dbus_data_slot_list_init(DBusDataSlotList *list)
Initializes a slot list.
#define _dbus_assert_not_reached(explanation)
Aborts with an error message if called.
char * address
Address this server is listening on.
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...
dbus_bool_t published_address
flag which indicates that server has published its bus address.
void dbus_move_error(DBusError *src, DBusError *dest)
Moves an error src into dest, freeing src and overwriting dest.
const char * name
public error name field
dbus_bool_t(* DBusAddWatchFunction)(DBusWatch *watch, void *data)
Called when libdbus needs a new watch 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.
DBusFreeFunction new_connection_free_data_function
Callback to invoke to free new_connection_data when server is finalized or data is replaced...
DBusWatchList implementation details.
void * new_connection_data
Data for new connection callback.
An allocator that tracks a set of slot IDs.
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 ...
dbus_bool_t dbus_server_get_is_connected(DBusServer *server)
Returns TRUE if the server is still listening for new connections.
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...
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...
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...
void(* DBusTimeoutRemoveFunction)(DBusTimeoutList *list, DBusTimeout *timeout)
Function to be called in protected_change_timeout() with refcount held.
dbus_int32_t _dbus_atomic_dec(DBusAtomic *atomic)
Atomically decrement an integer.
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_free_string_array(char **str_array)
Frees a NULL-terminated array of strings.
void _dbus_rmutex_free_at_location(DBusRMutex **location_p)
Frees a DBusRMutex; does nothing if passed a NULL pointer.
unsigned int have_server_lock
Does someone have the server mutex locked.
DBusString guid_hex
Hex-encoded version of GUID.
dbus_bool_t(* DBusWatchAddFunction)(DBusWatchList *list, DBusWatch *watch)
Function to be called in protected_change_watch() with refcount held.
#define FALSE
Expands to "0".
void _dbus_server_toggle_all_watches(DBusServer *server, dbus_bool_t enabled)
Toggles all watch and notifies app via server's DBusWatchToggledFunction if available.
void(* DBusWatchRemoveFunction)(DBusWatchList *list, DBusWatch *watch)
Function to be called in protected_change_watch() with refcount held.
#define _DBUS_LOCK_NAME(name)
Expands to name of a global lock variable.
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_string_steal_data(DBusString *str, char **data_return)
Like _dbus_string_get_data(), but removes the gotten data from the original string.
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_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.
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_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.
int dbus_int32_t
A 32-bit signed integer on all platforms.
char * _dbus_strdup(const char *str)
Duplicates a string.
DBusGUID guid
Globally unique ID of server.
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().
dbus_bool_t dbus_error_is_set(const DBusError *error)
Checks whether an error occurred (the error is set).
DBusTimeoutList implementation details.
dbus_bool_t dbus_parse_address(const char *address, DBusAddressEntry ***entry_result, int *array_len, DBusError *error)
Parses an address string of the form:
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...
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...