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_BUILD_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,
117 #ifdef DBUS_DISABLE_ASSERT
140 server->
address = copy_address_with_guid_appended (address,
159 _dbus_verbose (
"Initialized server on address %s\n", server->
address);
198 #ifndef DBUS_DISABLE_CHECKS
243 HAVE_LOCK_CHECK (server);
255 SERVER_UNLOCK (server);
258 retval = (* add_function) (watches, watch);
259 else if (remove_function)
262 (* remove_function) (watches, watch);
267 (* toggle_function) (watches, watch, enabled);
270 SERVER_LOCK (server);
291 HAVE_LOCK_CHECK (server);
292 return protected_change_watch (server, watch,
307 HAVE_LOCK_CHECK (server);
308 protected_change_watch (server, watch,
330 HAVE_LOCK_CHECK (server);
331 protected_change_watch (server, watch,
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,
462 HAVE_LOCK_CHECK (server);
466 _dbus_server_trace_ref (server, old_refcount, old_refcount + 1,
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);
525 static const struct {
532 #ifdef DBUS_BUILD_TESTS
533 , { _dbus_server_listen_debug_pipe }
567 _dbus_return_val_if_fail (address !=
NULL,
NULL);
568 _dbus_return_val_if_error_is_set (error,
NULL);
574 handled_once =
FALSE;
576 for (i = 0; i < len; i++)
582 DBusServerListenResult result;
585 result = (* listen_funcs[j].func) (entries[i],
589 if (result == DBUS_SERVER_LISTEN_OK)
592 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
596 else if (result == DBUS_SERVER_LISTEN_ADDRESS_ALREADY_USED)
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);
637 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
644 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
648 "Unknown address type '%s'",
653 "Empty address '%s'",
677 _DBUS_ASSERT_ERROR_IS_CLEAR (&first_connect_error);
678 _DBUS_ASSERT_ERROR_IS_SET (error);
684 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
700 _dbus_return_val_if_fail (server !=
NULL,
NULL);
705 #ifndef DBUS_DISABLE_CHECKS
706 if (_DBUS_UNLIKELY (old_refcount <= 0))
711 _DBUS_FUNCTION_NAME,
"old_refcount > 0",
717 _dbus_server_trace_ref (server, old_refcount, old_refcount + 1,
"ref");
737 _dbus_return_if_fail (server !=
NULL);
742 #ifndef DBUS_DISABLE_CHECKS
743 if (_DBUS_UNLIKELY (old_refcount <= 0))
748 _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);
780 #ifdef DBUS_DISABLE_CHECKS
786 _dbus_return_if_fail (old_refcount > 0);
790 SERVER_LOCK (server);
802 SERVER_UNLOCK (server);
816 _dbus_return_val_if_fail (server !=
NULL,
FALSE);
818 SERVER_LOCK (server);
820 SERVER_UNLOCK (server);
837 _dbus_return_val_if_fail (server !=
NULL,
NULL);
839 SERVER_LOCK (server);
841 SERVER_UNLOCK (server);
873 _dbus_return_val_if_fail (server !=
NULL,
NULL);
875 SERVER_LOCK (server);
878 SERVER_UNLOCK (server);
912 _dbus_return_if_fail (server !=
NULL);
914 SERVER_LOCK (server);
921 SERVER_UNLOCK (server);
923 if (old_free_function !=
NULL)
924 (* old_free_function) (old_data);
954 _dbus_return_val_if_fail (server !=
NULL,
FALSE);
956 SERVER_LOCK (server);
961 SERVER_UNLOCK (server);
968 SERVER_LOCK (server);
976 SERVER_UNLOCK (server);
1007 _dbus_return_val_if_fail (server !=
NULL,
FALSE);
1009 SERVER_LOCK (server);
1014 SERVER_UNLOCK (server);
1020 free_data_function);
1021 SERVER_LOCK (server);
1029 SERVER_UNLOCK (server);
1049 const char **mechanisms)
1053 _dbus_return_val_if_fail (server !=
NULL,
FALSE);
1055 SERVER_LOCK (server);
1057 if (mechanisms !=
NULL)
1069 SERVER_UNLOCK (server);
1114 _dbus_return_if_fail (*slot_p >= 0);
1142 _dbus_return_val_if_fail (server !=
NULL,
FALSE);
1144 SERVER_LOCK (server);
1148 slot, data, free_data_func,
1149 &old_free_func, &old_data);
1152 SERVER_UNLOCK (server);
1158 (* old_free_func) (old_data);
1178 _dbus_return_val_if_fail (server !=
NULL,
NULL);
1180 SERVER_LOCK (server);
1186 SERVER_UNLOCK (server);
1193 #ifdef DBUS_BUILD_TESTS
1194 #include "dbus-test.h"
1198 _dbus_server_test (
void)
1200 const char *valid_addresses[] = {
1202 "tcp:host=localhost,port=1234",
1203 "tcp:host=localhost,port=1234;tcp:port=5678",
1205 "unix:path=./boogie",
1206 "tcp:port=1234;unix:path=./boogie",
1232 if (strstr (address,
id) ==
NULL)
1234 _dbus_warn (
"server id '%s' is not in the server address '%s'\n",
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.
void(* DBusTimeoutToggledFunction)(DBusTimeout *timeout, void *data)
Called when dbus_timeout_get_enabled() may return a different value than it did before.
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(* DBusRemoveWatchFunction)(DBusWatch *watch, void *data)
Called when libdbus no longer needs a watch to be monitored by the main loop.
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
DBusWatchList * _dbus_watch_list_new(void)
Creates a new watch list.
unsigned int disconnected
TRUE if we are disconnected.
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...
dbus_bool_t(* DBusWatchAddFunction)(DBusWatchList *list, DBusWatch *watch)
Function to be called in protected_change_watch() with refcount held.
void _dbus_timeout_list_free(DBusTimeoutList *timeout_list)
Frees a DBusTimeoutList.
Internals of DBusServer object.
const char * _dbus_return_if_fail_warning_format
String used in _dbus_return_if_fail macro.
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.
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...
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_watch_list_toggle_watch(DBusWatchList *watch_list, DBusWatch *watch, dbus_bool_t enabled)
Sets a watch to the given enabled state, invoking the application's DBusWatchToggledFunction if appro...
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_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.
dbus_bool_t _dbus_string_init(DBusString *str)
Initializes a string.
void(* DBusWatchRemoveFunction)(DBusWatchList *list, DBusWatch *watch)
Function to be called in protected_change_watch() with refcount held.
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_data_slot_allocator_alloc(DBusDataSlotAllocator *allocator, DBusRMutex **mutex_loc, dbus_int32_t *slot_id_p)
Allocates an integer ID to be used for storing data in a DBusDataSlotList.
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...
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.
const DBusServerVTable * vtable
Virtual methods for this instance.
void(* disconnect)(DBusServer *server)
Disconnect this server.
dbus_int32_t _dbus_atomic_dec(DBusAtomic *atomic)
Atomically decrement an integer.
dbus_bool_t(* DBusAddWatchFunction)(DBusWatch *watch, void *data)
Called when libdbus needs a new watch to be monitored by the main loop.
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.
void _dbus_warn(const char *format,...)
Prints a warning message to stderr.
dbus_bool_t(* DBusAddTimeoutFunction)(DBusTimeout *timeout, void *data)
Called when libdbus needs a new timeout to be monitored by the main loop.
DBusTimeoutList * timeouts
Our timeouts.
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.
int _dbus_string_get_length(const DBusString *str)
Gets the length of a string (not including nul termination).
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_toggle_watch(DBusServer *server, DBusWatch *watch, dbus_bool_t enabled)
Toggles a watch and notifies app via server's DBusWatchToggledFunction if available.
void(* finalize)(DBusServer *server)
The finalize method must free the server.
dbus_bool_t _dbus_server_init_base(DBusServer *server, const DBusServerVTable *vtable, const DBusString *address)
Initializes the members of the DBusServer base class.
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.
dbus_int32_t _dbus_atomic_inc(DBusAtomic *atomic)
Atomically increments an integer.
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...
void(* DBusWatchToggleFunction)(DBusWatchList *list, DBusWatch *watch, dbus_bool_t enabled)
Function to be called in protected_change_watch() with refcount held.
#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().
#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 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
void(* DBusTimeoutRemoveFunction)(DBusTimeoutList *list, DBusTimeout *timeout)
Function to be called in protected_change_timeout() with refcount held.
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.
#define _DBUS_DEFINE_GLOBAL_LOCK(name)
Defines a global lock variable with the given name.
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_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_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_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 or removes it from the uninitialized mutex list; does nothing if passed a NULL poi...
unsigned int have_server_lock
Does someone have the server mutex locked.
DBusString guid_hex
Hex-encoded version of GUID.
#define FALSE
Expands to "0".
void _dbus_generate_uuid(DBusGUID *uuid)
Generates a new UUID.
dbus_bool_t dbus_parse_address(const char *address, DBusAddressEntry ***entry, int *array_len, DBusError *error)
Parses an address string of the form:
#define _DBUS_LOCK_NAME(name)
Expands to name of a global lock variable.
void(* DBusWatchToggledFunction)(DBusWatch *watch, void *data)
Called when dbus_watch_get_enabled() may return a different value than it did before.
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.
void _dbus_watch_list_free(DBusWatchList *watch_list)
Frees a DBusWatchList.
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().
void(* DBusNewConnectionFunction)(DBusServer *server, DBusConnection *new_connection, void *data)
Called when a new connection to the server is available.
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_error_is_set(const DBusError *error)
Checks whether an error occurred (the error is set).
DBusTimeoutList implementation details.
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_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_data_slot_list_free(DBusDataSlotList *list)
Frees the data slot list and all data slots contained in it, calling application-provided free functi...
dbus_bool_t(* DBusTimeoutAddFunction)(DBusTimeoutList *list, DBusTimeout *timeout)
Function to be called in protected_change_timeout() with refcount held.