1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-server.c DBusServer object
4 * Copyright (C) 2002, 2003, 2004, 2005 Red Hat Inc.
6 * Licensed under the Academic Free License version 2.1
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "dbus-server.h"
25 #include "dbus-server-unix.h"
26 #include "dbus-server-socket.h"
27 #include "dbus-string.h"
28 #ifdef DBUS_BUILD_TESTS
29 #include "dbus-server-debug-pipe.h"
31 #include "dbus-address.h"
32 #include "dbus-protocol.h"
35 * @defgroup DBusServer DBusServer
37 * @brief Server that listens for new connections.
39 * Types and functions related to DBusServer.
40 * A DBusServer represents a server that other applications
41 * can connect to. Each connection from another application
42 * is represented by a DBusConnection.
44 * @todo Thread safety hasn't been looked at for #DBusServer
45 * @todo Need notification to apps of disconnection, may matter for some transports
49 * @defgroup DBusServerInternals DBusServer implementation details
50 * @ingroup DBusInternals
51 * @brief Implementation details of DBusServer
56 /* this is a little fragile since it assumes the address doesn't
57 * already have a guid, but it shouldn't
60 copy_address_with_guid_appended (const DBusString *address,
61 const DBusString *guid_hex)
66 if (!_dbus_string_init (&with_guid))
69 if (!_dbus_string_copy (address, 0, &with_guid,
70 _dbus_string_get_length (&with_guid)) ||
71 !_dbus_string_append (&with_guid, ",guid=") ||
72 !_dbus_string_copy (guid_hex, 0,
73 &with_guid, _dbus_string_get_length (&with_guid)))
75 _dbus_string_free (&with_guid);
80 _dbus_string_steal_data (&with_guid, &retval);
82 _dbus_string_free (&with_guid);
84 return retval; /* may be NULL if steal_data failed */
88 * Initializes the members of the DBusServer base class.
89 * Chained up to by subclass constructors.
91 * @param server the server.
92 * @param vtable the vtable for the subclass.
93 * @param address the server's address
94 * @returns #TRUE on success.
97 _dbus_server_init_base (DBusServer *server,
98 const DBusServerVTable *vtable,
99 const DBusString *address)
101 server->vtable = vtable;
102 server->refcount.value = 1;
104 server->address = NULL;
105 server->watches = NULL;
106 server->timeouts = NULL;
108 if (!_dbus_string_init (&server->guid_hex))
111 _dbus_generate_uuid (&server->guid);
113 if (!_dbus_uuid_encode (&server->guid, &server->guid_hex))
116 server->address = copy_address_with_guid_appended (address,
118 if (server->address == NULL)
121 _dbus_mutex_new_at_location (&server->mutex);
122 if (server->mutex == NULL)
125 server->watches = _dbus_watch_list_new ();
126 if (server->watches == NULL)
129 server->timeouts = _dbus_timeout_list_new ();
130 if (server->timeouts == NULL)
133 _dbus_data_slot_list_init (&server->slot_list);
135 _dbus_verbose ("Initialized server on address %s\n", server->address);
140 _dbus_mutex_free_at_location (&server->mutex);
141 server->mutex = NULL;
144 _dbus_watch_list_free (server->watches);
145 server->watches = NULL;
147 if (server->timeouts)
149 _dbus_timeout_list_free (server->timeouts);
150 server->timeouts = NULL;
154 dbus_free (server->address);
155 server->address = NULL;
157 _dbus_string_free (&server->guid_hex);
163 * Finalizes the members of the DBusServer base class.
164 * Chained up to by subclass finalizers.
166 * @param server the server.
169 _dbus_server_finalize_base (DBusServer *server)
171 /* We don't have the lock, but nobody should be accessing
172 * concurrently since they don't have a ref
174 #ifndef DBUS_DISABLE_CHECKS
175 _dbus_assert (!server->have_server_lock);
177 _dbus_assert (server->disconnected);
179 /* calls out to application code... */
180 _dbus_data_slot_list_free (&server->slot_list);
182 dbus_server_set_new_connection_function (server, NULL, NULL, NULL);
184 _dbus_watch_list_free (server->watches);
185 _dbus_timeout_list_free (server->timeouts);
187 _dbus_mutex_free_at_location (&server->mutex);
189 dbus_free (server->address);
191 dbus_free_string_array (server->auth_mechanisms);
193 _dbus_string_free (&server->guid_hex);
197 typedef dbus_bool_t (* DBusWatchAddFunction) (DBusWatchList *list,
199 typedef void (* DBusWatchRemoveFunction) (DBusWatchList *list,
201 typedef void (* DBusWatchToggleFunction) (DBusWatchList *list,
203 dbus_bool_t enabled);
206 protected_change_watch (DBusServer *server,
208 DBusWatchAddFunction add_function,
209 DBusWatchRemoveFunction remove_function,
210 DBusWatchToggleFunction toggle_function,
213 DBusWatchList *watches;
216 HAVE_LOCK_CHECK (server);
218 /* This isn't really safe or reasonable; a better pattern is the "do
219 * everything, then drop lock and call out" one; but it has to be
220 * propagated up through all callers
223 watches = server->watches;
226 server->watches = NULL;
227 _dbus_server_ref_unlocked (server);
228 SERVER_UNLOCK (server);
231 retval = (* add_function) (watches, watch);
232 else if (remove_function)
235 (* remove_function) (watches, watch);
240 (* toggle_function) (watches, watch, enabled);
243 SERVER_LOCK (server);
244 server->watches = watches;
245 _dbus_server_unref_unlocked (server);
254 * Adds a watch for this server, chaining out to application-provided
257 * @param server the server.
258 * @param watch the watch to add.
261 _dbus_server_add_watch (DBusServer *server,
264 HAVE_LOCK_CHECK (server);
265 return protected_change_watch (server, watch,
266 _dbus_watch_list_add_watch,
271 * Removes a watch previously added with _dbus_server_remove_watch().
273 * @param server the server.
274 * @param watch the watch to remove.
277 _dbus_server_remove_watch (DBusServer *server,
280 HAVE_LOCK_CHECK (server);
281 protected_change_watch (server, watch,
283 _dbus_watch_list_remove_watch,
288 * Toggles a watch and notifies app via server's
289 * DBusWatchToggledFunction if available. It's an error to call this
290 * function on a watch that was not previously added.
292 * @param server the server.
293 * @param watch the watch to toggle.
294 * @param enabled whether to enable or disable
297 _dbus_server_toggle_watch (DBusServer *server,
301 _dbus_assert (watch != NULL);
303 HAVE_LOCK_CHECK (server);
304 protected_change_watch (server, watch,
306 _dbus_watch_list_toggle_watch,
311 typedef dbus_bool_t (* DBusTimeoutAddFunction) (DBusTimeoutList *list,
312 DBusTimeout *timeout);
313 typedef void (* DBusTimeoutRemoveFunction) (DBusTimeoutList *list,
314 DBusTimeout *timeout);
315 typedef void (* DBusTimeoutToggleFunction) (DBusTimeoutList *list,
316 DBusTimeout *timeout,
317 dbus_bool_t enabled);
321 protected_change_timeout (DBusServer *server,
322 DBusTimeout *timeout,
323 DBusTimeoutAddFunction add_function,
324 DBusTimeoutRemoveFunction remove_function,
325 DBusTimeoutToggleFunction toggle_function,
328 DBusTimeoutList *timeouts;
331 HAVE_LOCK_CHECK (server);
333 /* This isn't really safe or reasonable; a better pattern is the "do everything, then
334 * drop lock and call out" one; but it has to be propagated up through all callers
337 timeouts = server->timeouts;
340 server->timeouts = NULL;
341 _dbus_server_ref_unlocked (server);
342 SERVER_UNLOCK (server);
345 retval = (* add_function) (timeouts, timeout);
346 else if (remove_function)
349 (* remove_function) (timeouts, timeout);
354 (* toggle_function) (timeouts, timeout, enabled);
357 SERVER_LOCK (server);
358 server->timeouts = timeouts;
359 _dbus_server_unref_unlocked (server);
368 * Adds a timeout for this server, chaining out to
369 * application-provided timeout handlers. The timeout should be
370 * repeatedly handled with dbus_timeout_handle() at its given interval
371 * until it is removed.
373 * @param server the server.
374 * @param timeout the timeout to add.
377 _dbus_server_add_timeout (DBusServer *server,
378 DBusTimeout *timeout)
380 return protected_change_timeout (server, timeout,
381 _dbus_timeout_list_add_timeout,
386 * Removes a timeout previously added with _dbus_server_add_timeout().
388 * @param server the server.
389 * @param timeout the timeout to remove.
392 _dbus_server_remove_timeout (DBusServer *server,
393 DBusTimeout *timeout)
395 protected_change_timeout (server, timeout,
397 _dbus_timeout_list_remove_timeout,
402 * Toggles a timeout and notifies app via server's
403 * DBusTimeoutToggledFunction if available. It's an error to call this
404 * function on a timeout that was not previously added.
406 * @param server the server.
407 * @param timeout the timeout to toggle.
408 * @param enabled whether to enable or disable
411 _dbus_server_toggle_timeout (DBusServer *server,
412 DBusTimeout *timeout,
415 protected_change_timeout (server, timeout,
417 _dbus_timeout_list_toggle_timeout,
424 * @addtogroup DBusServer
431 * @typedef DBusServer
433 * An opaque object representing a server that listens for
434 * connections from other applications. Each time a connection
435 * is made, a new DBusConnection is created and made available
436 * via an application-provided DBusNewConnectionFunction.
437 * The DBusNewConnectionFunction is provided with
438 * dbus_server_set_new_connection_function().
442 static const struct {
443 DBusServerListenResult (* func) (DBusAddressEntry *entry,
444 DBusServer **server_p,
447 { _dbus_server_listen_socket },
448 { _dbus_server_listen_platform_specific }
449 #ifdef DBUS_BUILD_TESTS
450 , { _dbus_server_listen_debug_pipe }
455 * Listens for new connections on the given address.
456 * If there are multiple address entries in the address,
457 * tries each one and listens on the first one that
460 * Returns #NULL and sets error if listening fails for any reason.
461 * Otherwise returns a new #DBusServer.
462 * dbus_server_set_new_connection_function() and
463 * dbus_server_set_watch_functions() should be called
464 * immediately to render the server fully functional.
466 * @param address the address of this server.
467 * @param error location to store rationale for failure.
468 * @returns a new DBusServer, or #NULL on failure.
472 dbus_server_listen (const char *address,
476 DBusAddressEntry **entries;
478 DBusError first_connect_error;
479 dbus_bool_t handled_once;
481 _dbus_return_val_if_fail (address != NULL, NULL);
482 _dbus_return_val_if_error_is_set (error, NULL);
484 if (!dbus_parse_address (address, &entries, &len, error))
488 dbus_error_init (&first_connect_error);
489 handled_once = FALSE;
491 for (i = 0; i < len; i++)
495 for (j = 0; j < (int) _DBUS_N_ELEMENTS (listen_funcs); ++j)
497 DBusServerListenResult result;
500 dbus_error_init (&tmp_error);
501 result = (* listen_funcs[j].func) (entries[i],
505 if (result == DBUS_SERVER_LISTEN_OK)
507 _dbus_assert (server != NULL);
508 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
512 else if (result == DBUS_SERVER_LISTEN_BAD_ADDRESS)
514 _dbus_assert (server == NULL);
515 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
516 dbus_move_error (&tmp_error, error);
520 else if (result == DBUS_SERVER_LISTEN_NOT_HANDLED)
522 _dbus_assert (server == NULL);
523 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
525 /* keep trying addresses */
527 else if (result == DBUS_SERVER_LISTEN_DID_NOT_CONNECT)
529 _dbus_assert (server == NULL);
530 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
531 if (!dbus_error_is_set (&first_connect_error))
532 dbus_move_error (&tmp_error, &first_connect_error);
534 dbus_error_free (&tmp_error);
538 /* keep trying addresses */
542 _dbus_assert (server == NULL);
543 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
550 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
552 dbus_set_error (error,
553 DBUS_ERROR_BAD_ADDRESS,
554 "Unknown address type '%s'",
555 dbus_address_entry_get_method (entries[0]));
557 dbus_set_error (error,
558 DBUS_ERROR_BAD_ADDRESS,
559 "Empty address '%s'",
563 dbus_address_entries_free (entries);
567 _dbus_assert (error == NULL || dbus_error_is_set (&first_connect_error) ||
568 dbus_error_is_set (error));
570 if (error && dbus_error_is_set (error))
572 /* already set the error */
576 /* didn't set the error but either error should be
577 * NULL or first_connect_error should be set.
579 _dbus_assert (error == NULL || dbus_error_is_set (&first_connect_error));
580 dbus_move_error (&first_connect_error, error);
583 _DBUS_ASSERT_ERROR_IS_CLEAR (&first_connect_error); /* be sure we freed it */
584 _DBUS_ASSERT_ERROR_IS_SET (error);
590 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
596 * Increments the reference count of a DBusServer.
598 * @param server the server.
599 * @returns the server
602 dbus_server_ref (DBusServer *server)
604 _dbus_return_val_if_fail (server != NULL, NULL);
605 _dbus_return_val_if_fail (server->refcount.value > 0, NULL);
607 #ifdef DBUS_HAVE_ATOMIC_INT
608 _dbus_atomic_inc (&server->refcount);
610 SERVER_LOCK (server);
611 _dbus_assert (server->refcount.value > 0);
613 server->refcount.value += 1;
614 SERVER_UNLOCK (server);
621 * Decrements the reference count of a DBusServer. Finalizes the
622 * server if the reference count reaches zero.
624 * The server must be disconnected before the refcount reaches zero.
626 * @param server the server.
629 dbus_server_unref (DBusServer *server)
631 dbus_bool_t last_unref;
633 _dbus_return_if_fail (server != NULL);
634 _dbus_return_if_fail (server->refcount.value > 0);
636 #ifdef DBUS_HAVE_ATOMIC_INT
637 last_unref = (_dbus_atomic_dec (&server->refcount) == 1);
639 SERVER_LOCK (server);
641 _dbus_assert (server->refcount.value > 0);
643 server->refcount.value -= 1;
644 last_unref = (server->refcount.value == 0);
646 SERVER_UNLOCK (server);
652 _dbus_assert (server->disconnected);
654 _dbus_assert (server->vtable->finalize != NULL);
656 (* server->vtable->finalize) (server);
661 * Like dbus_server_ref() but does not acquire the lock (must already be held)
663 * @param server the server.
666 _dbus_server_ref_unlocked (DBusServer *server)
668 _dbus_assert (server != NULL);
669 _dbus_assert (server->refcount.value > 0);
671 HAVE_LOCK_CHECK (server);
673 #ifdef DBUS_HAVE_ATOMIC_INT
674 _dbus_atomic_inc (&server->refcount);
676 _dbus_assert (server->refcount.value > 0);
678 server->refcount.value += 1;
683 * Like dbus_server_unref() but does not acquire the lock (must already be held)
685 * @param server the server.
688 _dbus_server_unref_unlocked (DBusServer *server)
690 dbus_bool_t last_unref;
692 _dbus_assert (server != NULL);
693 _dbus_assert (server->refcount.value > 0);
695 HAVE_LOCK_CHECK (server);
697 #ifdef DBUS_HAVE_ATOMIC_INT
698 last_unref = (_dbus_atomic_dec (&server->refcount) == 1);
700 _dbus_assert (server->refcount.value > 0);
702 server->refcount.value -= 1;
703 last_unref = (server->refcount.value == 0);
708 _dbus_assert (server->disconnected);
710 SERVER_UNLOCK (server);
712 _dbus_assert (server->vtable->finalize != NULL);
714 (* server->vtable->finalize) (server);
719 * Releases the server's address and stops listening for
720 * new clients. If called more than once, only the first
721 * call has an effect. Does not modify the server's
724 * @param server the server.
727 dbus_server_disconnect (DBusServer *server)
729 _dbus_return_if_fail (server != NULL);
730 _dbus_return_if_fail (server->refcount.value > 0);
732 SERVER_LOCK (server);
733 _dbus_server_ref_unlocked (server);
735 _dbus_assert (server->vtable->disconnect != NULL);
737 if (!server->disconnected)
739 /* this has to be first so recursive calls to disconnect don't happen */
740 server->disconnected = TRUE;
742 (* server->vtable->disconnect) (server);
745 SERVER_UNLOCK (server);
746 dbus_server_unref (server);
750 * Returns #TRUE if the server is still listening for new connections.
752 * @param server the server.
755 dbus_server_get_is_connected (DBusServer *server)
759 _dbus_return_val_if_fail (server != NULL, FALSE);
761 SERVER_LOCK (server);
762 retval = !server->disconnected;
763 SERVER_UNLOCK (server);
769 * Returns the address of the server, as a newly-allocated
770 * string which must be freed by the caller.
772 * @param server the server
773 * @returns the address or #NULL if no memory
776 dbus_server_get_address (DBusServer *server)
780 _dbus_return_val_if_fail (server != NULL, NULL);
782 SERVER_LOCK (server);
783 retval = _dbus_strdup (server->address);
784 SERVER_UNLOCK (server);
790 * Sets a function to be used for handling new connections. The given
791 * function is passed each new connection as the connection is
792 * created. If the new connection function increments the connection's
793 * reference count, the connection will stay alive. Otherwise, the
794 * connection will be unreferenced and closed. The new connection
795 * function may also close the connection itself, which is considered
796 * good form if the connection is not wanted.
798 * The connection here is private in the sense of
799 * dbus_connection_open_private(), so if the new connection function
800 * keeps a reference it must arrange for the connection to be closed.
801 * i.e. libdbus does not own this connection once the new connection
802 * function takes a reference.
804 * @param server the server.
805 * @param function a function to handle new connections.
806 * @param data data to pass to the new connection handler.
807 * @param free_data_function function to free the data.
810 dbus_server_set_new_connection_function (DBusServer *server,
811 DBusNewConnectionFunction function,
813 DBusFreeFunction free_data_function)
815 DBusFreeFunction old_free_function;
818 _dbus_return_if_fail (server != NULL);
820 SERVER_LOCK (server);
821 old_free_function = server->new_connection_free_data_function;
822 old_data = server->new_connection_data;
824 server->new_connection_function = function;
825 server->new_connection_data = data;
826 server->new_connection_free_data_function = free_data_function;
827 SERVER_UNLOCK (server);
829 if (old_free_function != NULL)
830 (* old_free_function) (old_data);
834 * Sets the watch functions for the connection. These functions are
835 * responsible for making the application's main loop aware of file
836 * descriptors that need to be monitored for events.
838 * This function behaves exactly like dbus_connection_set_watch_functions();
839 * see the documentation for that routine.
841 * @param server the server.
842 * @param add_function function to begin monitoring a new descriptor.
843 * @param remove_function function to stop monitoring a descriptor.
844 * @param toggled_function function to notify when the watch is enabled/disabled
845 * @param data data to pass to add_function and remove_function.
846 * @param free_data_function function to be called to free the data.
847 * @returns #FALSE on failure (no memory)
850 dbus_server_set_watch_functions (DBusServer *server,
851 DBusAddWatchFunction add_function,
852 DBusRemoveWatchFunction remove_function,
853 DBusWatchToggledFunction toggled_function,
855 DBusFreeFunction free_data_function)
858 DBusWatchList *watches;
860 _dbus_return_val_if_fail (server != NULL, FALSE);
862 SERVER_LOCK (server);
863 watches = server->watches;
864 server->watches = NULL;
867 SERVER_UNLOCK (server);
868 result = _dbus_watch_list_set_functions (watches,
874 SERVER_LOCK (server);
878 _dbus_warn ("Re-entrant call to %s\n", _DBUS_FUNCTION_NAME);
881 server->watches = watches;
882 SERVER_UNLOCK (server);
888 * Sets the timeout functions for the connection. These functions are
889 * responsible for making the application's main loop aware of timeouts.
891 * This function behaves exactly like dbus_connection_set_timeout_functions();
892 * see the documentation for that routine.
894 * @param server the server.
895 * @param add_function function to add a timeout.
896 * @param remove_function function to remove a timeout.
897 * @param toggled_function function to notify when the timeout is enabled/disabled
898 * @param data data to pass to add_function and remove_function.
899 * @param free_data_function function to be called to free the data.
900 * @returns #FALSE on failure (no memory)
903 dbus_server_set_timeout_functions (DBusServer *server,
904 DBusAddTimeoutFunction add_function,
905 DBusRemoveTimeoutFunction remove_function,
906 DBusTimeoutToggledFunction toggled_function,
908 DBusFreeFunction free_data_function)
911 DBusTimeoutList *timeouts;
913 _dbus_return_val_if_fail (server != NULL, FALSE);
915 SERVER_LOCK (server);
916 timeouts = server->timeouts;
917 server->timeouts = NULL;
920 SERVER_UNLOCK (server);
921 result = _dbus_timeout_list_set_functions (timeouts,
927 SERVER_LOCK (server);
931 _dbus_warn ("Re-entrant call to %s\n", _DBUS_FUNCTION_NAME);
934 server->timeouts = timeouts;
935 SERVER_UNLOCK (server);
941 * Sets the authentication mechanisms that this server offers
942 * to clients, as a list of SASL mechanisms. This function
943 * only affects connections created *after* it is called.
944 * Pass #NULL instead of an array to use all available mechanisms.
946 * @param server the server
947 * @param mechanisms #NULL-terminated array of mechanisms
948 * @returns #FALSE if no memory
951 dbus_server_set_auth_mechanisms (DBusServer *server,
952 const char **mechanisms)
956 _dbus_return_val_if_fail (server != NULL, FALSE);
958 SERVER_LOCK (server);
960 if (mechanisms != NULL)
962 copy = _dbus_dup_string_array (mechanisms);
969 dbus_free_string_array (server->auth_mechanisms);
970 server->auth_mechanisms = copy;
972 SERVER_UNLOCK (server);
978 static DBusDataSlotAllocator slot_allocator;
979 _DBUS_DEFINE_GLOBAL_LOCK (server_slots);
982 * Allocates an integer ID to be used for storing application-specific
983 * data on any DBusServer. The allocated ID may then be used
984 * with dbus_server_set_data() and dbus_server_get_data().
985 * The slot must be initialized with -1. If a nonnegative
986 * slot is passed in, the refcount is incremented on that
987 * slot, rather than creating a new slot.
989 * The allocated slot is global, i.e. all DBusServer objects will have
990 * a slot with the given integer ID reserved.
992 * @param slot_p address of global variable storing the slot ID
993 * @returns #FALSE on no memory
996 dbus_server_allocate_data_slot (dbus_int32_t *slot_p)
998 return _dbus_data_slot_allocator_alloc (&slot_allocator,
999 (DBusMutex **)&_DBUS_LOCK_NAME (server_slots),
1004 * Deallocates a global ID for server data slots.
1005 * dbus_server_get_data() and dbus_server_set_data()
1006 * may no longer be used with this slot.
1007 * Existing data stored on existing DBusServer objects
1008 * will be freed when the server is finalized,
1009 * but may not be retrieved (and may only be replaced
1010 * if someone else reallocates the slot).
1012 * @param slot_p address of the slot to deallocate
1015 dbus_server_free_data_slot (dbus_int32_t *slot_p)
1017 _dbus_return_if_fail (*slot_p >= 0);
1019 _dbus_data_slot_allocator_free (&slot_allocator, slot_p);
1023 * Stores a pointer on a DBusServer, along
1024 * with an optional function to be used for freeing
1025 * the data when the data is set again, or when
1026 * the server is finalized. The slot number
1027 * must have been allocated with dbus_server_allocate_data_slot().
1029 * @param server the server
1030 * @param slot the slot number
1031 * @param data the data to store
1032 * @param free_data_func finalizer function for the data
1033 * @returns #TRUE if there was enough memory to store the data
1036 dbus_server_set_data (DBusServer *server,
1039 DBusFreeFunction free_data_func)
1041 DBusFreeFunction old_free_func;
1045 _dbus_return_val_if_fail (server != NULL, FALSE);
1047 SERVER_LOCK (server);
1049 retval = _dbus_data_slot_list_set (&slot_allocator,
1051 slot, data, free_data_func,
1052 &old_free_func, &old_data);
1055 SERVER_UNLOCK (server);
1059 /* Do the actual free outside the server lock */
1061 (* old_free_func) (old_data);
1068 * Retrieves data previously set with dbus_server_set_data().
1069 * The slot must still be allocated (must not have been freed).
1071 * @param server the server
1072 * @param slot the slot to get data from
1073 * @returns the data, or #NULL if not found
1076 dbus_server_get_data (DBusServer *server,
1081 _dbus_return_val_if_fail (server != NULL, NULL);
1083 SERVER_LOCK (server);
1085 res = _dbus_data_slot_list_get (&slot_allocator,
1089 SERVER_UNLOCK (server);
1096 #ifdef DBUS_BUILD_TESTS
1097 #include "dbus-test.h"
1100 _dbus_server_test (void)
1102 const char *valid_addresses[] = {
1104 "unix:path=./boogie",
1105 "tcp:host=localhost,port=1234",
1106 "tcp:host=localhost,port=1234;tcp:port=5678",
1107 "tcp:port=1234;unix:path=./boogie",
1113 for (i = 0; i < _DBUS_N_ELEMENTS (valid_addresses); i++)
1117 /* FIXME um, how are the two tests here different? */
1119 dbus_error_init (&error);
1120 server = dbus_server_listen (valid_addresses[i], &error);
1123 _dbus_warn ("server listen error: %s: %s\n", error.name, error.message);
1124 dbus_error_free (&error);
1125 _dbus_assert_not_reached ("Failed to listen for valid address.");
1128 dbus_server_disconnect (server);
1129 dbus_server_unref (server);
1131 /* Try disconnecting before unreffing */
1132 server = dbus_server_listen (valid_addresses[i], &error);
1135 _dbus_warn ("server listen error: %s: %s\n", error.name, error.message);
1136 dbus_error_free (&error);
1137 _dbus_assert_not_reached ("Failed to listen for valid address.");
1140 dbus_server_disconnect (server);
1141 dbus_server_unref (server);
1147 #endif /* DBUS_BUILD_TESTS */