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 /** Function to be called in protected_change_watch() with refcount held */
198 typedef dbus_bool_t (* DBusWatchAddFunction) (DBusWatchList *list,
200 /** Function to be called in protected_change_watch() with refcount held */
201 typedef void (* DBusWatchRemoveFunction) (DBusWatchList *list,
203 /** Function to be called in protected_change_watch() with refcount held */
204 typedef void (* DBusWatchToggleFunction) (DBusWatchList *list,
206 dbus_bool_t enabled);
209 protected_change_watch (DBusServer *server,
211 DBusWatchAddFunction add_function,
212 DBusWatchRemoveFunction remove_function,
213 DBusWatchToggleFunction toggle_function,
216 DBusWatchList *watches;
219 HAVE_LOCK_CHECK (server);
221 /* This isn't really safe or reasonable; a better pattern is the "do
222 * everything, then drop lock and call out" one; but it has to be
223 * propagated up through all callers
226 watches = server->watches;
229 server->watches = NULL;
230 _dbus_server_ref_unlocked (server);
231 SERVER_UNLOCK (server);
234 retval = (* add_function) (watches, watch);
235 else if (remove_function)
238 (* remove_function) (watches, watch);
243 (* toggle_function) (watches, watch, enabled);
246 SERVER_LOCK (server);
247 server->watches = watches;
248 _dbus_server_unref_unlocked (server);
257 * Adds a watch for this server, chaining out to application-provided
260 * @param server the server.
261 * @param watch the watch to add.
264 _dbus_server_add_watch (DBusServer *server,
267 HAVE_LOCK_CHECK (server);
268 return protected_change_watch (server, watch,
269 _dbus_watch_list_add_watch,
274 * Removes a watch previously added with _dbus_server_remove_watch().
276 * @param server the server.
277 * @param watch the watch to remove.
280 _dbus_server_remove_watch (DBusServer *server,
283 HAVE_LOCK_CHECK (server);
284 protected_change_watch (server, watch,
286 _dbus_watch_list_remove_watch,
291 * Toggles a watch and notifies app via server's
292 * DBusWatchToggledFunction if available. It's an error to call this
293 * function on a watch that was not previously added.
295 * @param server the server.
296 * @param watch the watch to toggle.
297 * @param enabled whether to enable or disable
300 _dbus_server_toggle_watch (DBusServer *server,
304 _dbus_assert (watch != NULL);
306 HAVE_LOCK_CHECK (server);
307 protected_change_watch (server, watch,
309 _dbus_watch_list_toggle_watch,
313 /** Function to be called in protected_change_timeout() with refcount held */
314 typedef dbus_bool_t (* DBusTimeoutAddFunction) (DBusTimeoutList *list,
315 DBusTimeout *timeout);
316 /** Function to be called in protected_change_timeout() with refcount held */
317 typedef void (* DBusTimeoutRemoveFunction) (DBusTimeoutList *list,
318 DBusTimeout *timeout);
319 /** Function to be called in protected_change_timeout() with refcount held */
320 typedef void (* DBusTimeoutToggleFunction) (DBusTimeoutList *list,
321 DBusTimeout *timeout,
322 dbus_bool_t enabled);
326 protected_change_timeout (DBusServer *server,
327 DBusTimeout *timeout,
328 DBusTimeoutAddFunction add_function,
329 DBusTimeoutRemoveFunction remove_function,
330 DBusTimeoutToggleFunction toggle_function,
333 DBusTimeoutList *timeouts;
336 HAVE_LOCK_CHECK (server);
338 /* This isn't really safe or reasonable; a better pattern is the "do everything, then
339 * drop lock and call out" one; but it has to be propagated up through all callers
342 timeouts = server->timeouts;
345 server->timeouts = NULL;
346 _dbus_server_ref_unlocked (server);
347 SERVER_UNLOCK (server);
350 retval = (* add_function) (timeouts, timeout);
351 else if (remove_function)
354 (* remove_function) (timeouts, timeout);
359 (* toggle_function) (timeouts, timeout, enabled);
362 SERVER_LOCK (server);
363 server->timeouts = timeouts;
364 _dbus_server_unref_unlocked (server);
373 * Adds a timeout for this server, chaining out to
374 * application-provided timeout handlers. The timeout should be
375 * repeatedly handled with dbus_timeout_handle() at its given interval
376 * until it is removed.
378 * @param server the server.
379 * @param timeout the timeout to add.
382 _dbus_server_add_timeout (DBusServer *server,
383 DBusTimeout *timeout)
385 return protected_change_timeout (server, timeout,
386 _dbus_timeout_list_add_timeout,
391 * Removes a timeout previously added with _dbus_server_add_timeout().
393 * @param server the server.
394 * @param timeout the timeout to remove.
397 _dbus_server_remove_timeout (DBusServer *server,
398 DBusTimeout *timeout)
400 protected_change_timeout (server, timeout,
402 _dbus_timeout_list_remove_timeout,
407 * Toggles a timeout and notifies app via server's
408 * DBusTimeoutToggledFunction if available. It's an error to call this
409 * function on a timeout that was not previously added.
411 * @param server the server.
412 * @param timeout the timeout to toggle.
413 * @param enabled whether to enable or disable
416 _dbus_server_toggle_timeout (DBusServer *server,
417 DBusTimeout *timeout,
420 protected_change_timeout (server, timeout,
422 _dbus_timeout_list_toggle_timeout,
428 * Like dbus_server_ref() but does not acquire the lock (must already be held)
430 * @param server the server.
433 _dbus_server_ref_unlocked (DBusServer *server)
435 _dbus_assert (server != NULL);
436 _dbus_assert (server->refcount.value > 0);
438 HAVE_LOCK_CHECK (server);
440 #ifdef DBUS_HAVE_ATOMIC_INT
441 _dbus_atomic_inc (&server->refcount);
443 _dbus_assert (server->refcount.value > 0);
445 server->refcount.value += 1;
450 * Like dbus_server_unref() but does not acquire the lock (must already be held)
452 * @param server the server.
455 _dbus_server_unref_unlocked (DBusServer *server)
457 dbus_bool_t last_unref;
459 /* Keep this in sync with dbus_server_unref */
461 _dbus_assert (server != NULL);
462 _dbus_assert (server->refcount.value > 0);
464 HAVE_LOCK_CHECK (server);
466 #ifdef DBUS_HAVE_ATOMIC_INT
467 last_unref = (_dbus_atomic_dec (&server->refcount) == 1);
469 _dbus_assert (server->refcount.value > 0);
471 server->refcount.value -= 1;
472 last_unref = (server->refcount.value == 0);
477 _dbus_assert (server->disconnected);
479 SERVER_UNLOCK (server);
481 _dbus_assert (server->vtable->finalize != NULL);
483 (* server->vtable->finalize) (server);
490 * @addtogroup DBusServer
497 * @typedef DBusServer
499 * An opaque object representing a server that listens for
500 * connections from other applications. Each time a connection
501 * is made, a new DBusConnection is created and made available
502 * via an application-provided DBusNewConnectionFunction.
503 * The DBusNewConnectionFunction is provided with
504 * dbus_server_set_new_connection_function().
508 static const struct {
509 DBusServerListenResult (* func) (DBusAddressEntry *entry,
510 DBusServer **server_p,
513 { _dbus_server_listen_socket },
514 { _dbus_server_listen_platform_specific }
515 #ifdef DBUS_BUILD_TESTS
516 , { _dbus_server_listen_debug_pipe }
521 * Listens for new connections on the given address.
522 * If there are multiple address entries in the address,
523 * tries each one and listens on the first one that
526 * Returns #NULL and sets error if listening fails for any reason.
527 * Otherwise returns a new #DBusServer.
528 * dbus_server_set_new_connection_function() and
529 * dbus_server_set_watch_functions() should be called
530 * immediately to render the server fully functional.
532 * @param address the address of this server.
533 * @param error location to store rationale for failure.
534 * @returns a new DBusServer, or #NULL on failure.
538 dbus_server_listen (const char *address,
542 DBusAddressEntry **entries;
544 DBusError first_connect_error;
545 dbus_bool_t handled_once;
547 _dbus_return_val_if_fail (address != NULL, NULL);
548 _dbus_return_val_if_error_is_set (error, NULL);
550 if (!dbus_parse_address (address, &entries, &len, error))
554 dbus_error_init (&first_connect_error);
555 handled_once = FALSE;
557 for (i = 0; i < len; i++)
561 for (j = 0; j < (int) _DBUS_N_ELEMENTS (listen_funcs); ++j)
563 DBusServerListenResult result;
566 dbus_error_init (&tmp_error);
567 result = (* listen_funcs[j].func) (entries[i],
571 if (result == DBUS_SERVER_LISTEN_OK)
573 _dbus_assert (server != NULL);
574 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
578 else if (result == DBUS_SERVER_LISTEN_BAD_ADDRESS)
580 _dbus_assert (server == NULL);
581 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
582 dbus_move_error (&tmp_error, error);
586 else if (result == DBUS_SERVER_LISTEN_NOT_HANDLED)
588 _dbus_assert (server == NULL);
589 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
591 /* keep trying addresses */
593 else if (result == DBUS_SERVER_LISTEN_DID_NOT_CONNECT)
595 _dbus_assert (server == NULL);
596 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
597 if (!dbus_error_is_set (&first_connect_error))
598 dbus_move_error (&tmp_error, &first_connect_error);
600 dbus_error_free (&tmp_error);
604 /* keep trying addresses */
608 _dbus_assert (server == NULL);
609 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
616 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
618 dbus_set_error (error,
619 DBUS_ERROR_BAD_ADDRESS,
620 "Unknown address type '%s'",
621 dbus_address_entry_get_method (entries[0]));
623 dbus_set_error (error,
624 DBUS_ERROR_BAD_ADDRESS,
625 "Empty address '%s'",
629 dbus_address_entries_free (entries);
633 _dbus_assert (error == NULL || dbus_error_is_set (&first_connect_error) ||
634 dbus_error_is_set (error));
636 if (error && dbus_error_is_set (error))
638 /* already set the error */
642 /* didn't set the error but either error should be
643 * NULL or first_connect_error should be set.
645 _dbus_assert (error == NULL || dbus_error_is_set (&first_connect_error));
646 dbus_move_error (&first_connect_error, error);
649 _DBUS_ASSERT_ERROR_IS_CLEAR (&first_connect_error); /* be sure we freed it */
650 _DBUS_ASSERT_ERROR_IS_SET (error);
656 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
662 * Increments the reference count of a DBusServer.
664 * @param server the server.
665 * @returns the server
668 dbus_server_ref (DBusServer *server)
670 _dbus_return_val_if_fail (server != NULL, NULL);
671 _dbus_return_val_if_fail (server->refcount.value > 0, NULL);
673 #ifdef DBUS_HAVE_ATOMIC_INT
674 _dbus_atomic_inc (&server->refcount);
676 SERVER_LOCK (server);
677 _dbus_assert (server->refcount.value > 0);
679 server->refcount.value += 1;
680 SERVER_UNLOCK (server);
687 * Decrements the reference count of a DBusServer. Finalizes the
688 * server if the reference count reaches zero.
690 * The server must be disconnected before the refcount reaches zero.
692 * @param server the server.
695 dbus_server_unref (DBusServer *server)
697 dbus_bool_t last_unref;
699 /* keep this in sync with unref_unlocked */
701 _dbus_return_if_fail (server != NULL);
702 _dbus_return_if_fail (server->refcount.value > 0);
704 #ifdef DBUS_HAVE_ATOMIC_INT
705 last_unref = (_dbus_atomic_dec (&server->refcount) == 1);
707 SERVER_LOCK (server);
709 _dbus_assert (server->refcount.value > 0);
711 server->refcount.value -= 1;
712 last_unref = (server->refcount.value == 0);
714 SERVER_UNLOCK (server);
720 _dbus_assert (server->disconnected);
722 _dbus_assert (server->vtable->finalize != NULL);
724 (* server->vtable->finalize) (server);
729 * Releases the server's address and stops listening for
730 * new clients. If called more than once, only the first
731 * call has an effect. Does not modify the server's
734 * @param server the server.
737 dbus_server_disconnect (DBusServer *server)
739 _dbus_return_if_fail (server != NULL);
740 _dbus_return_if_fail (server->refcount.value > 0);
742 SERVER_LOCK (server);
743 _dbus_server_ref_unlocked (server);
745 _dbus_assert (server->vtable->disconnect != NULL);
747 if (!server->disconnected)
749 /* this has to be first so recursive calls to disconnect don't happen */
750 server->disconnected = TRUE;
752 (* server->vtable->disconnect) (server);
755 SERVER_UNLOCK (server);
756 dbus_server_unref (server);
760 * Returns #TRUE if the server is still listening for new connections.
762 * @param server the server.
765 dbus_server_get_is_connected (DBusServer *server)
769 _dbus_return_val_if_fail (server != NULL, FALSE);
771 SERVER_LOCK (server);
772 retval = !server->disconnected;
773 SERVER_UNLOCK (server);
779 * Returns the address of the server, as a newly-allocated
780 * string which must be freed by the caller.
782 * @param server the server
783 * @returns the address or #NULL if no memory
786 dbus_server_get_address (DBusServer *server)
790 _dbus_return_val_if_fail (server != NULL, NULL);
792 SERVER_LOCK (server);
793 retval = _dbus_strdup (server->address);
794 SERVER_UNLOCK (server);
800 * Sets a function to be used for handling new connections. The given
801 * function is passed each new connection as the connection is
802 * created. If the new connection function increments the connection's
803 * reference count, the connection will stay alive. Otherwise, the
804 * connection will be unreferenced and closed. The new connection
805 * function may also close the connection itself, which is considered
806 * good form if the connection is not wanted.
808 * The connection here is private in the sense of
809 * dbus_connection_open_private(), so if the new connection function
810 * keeps a reference it must arrange for the connection to be closed.
811 * i.e. libdbus does not own this connection once the new connection
812 * function takes a reference.
814 * @param server the server.
815 * @param function a function to handle new connections.
816 * @param data data to pass to the new connection handler.
817 * @param free_data_function function to free the data.
820 dbus_server_set_new_connection_function (DBusServer *server,
821 DBusNewConnectionFunction function,
823 DBusFreeFunction free_data_function)
825 DBusFreeFunction old_free_function;
828 _dbus_return_if_fail (server != NULL);
830 SERVER_LOCK (server);
831 old_free_function = server->new_connection_free_data_function;
832 old_data = server->new_connection_data;
834 server->new_connection_function = function;
835 server->new_connection_data = data;
836 server->new_connection_free_data_function = free_data_function;
837 SERVER_UNLOCK (server);
839 if (old_free_function != NULL)
840 (* old_free_function) (old_data);
844 * Sets the watch functions for the connection. These functions are
845 * responsible for making the application's main loop aware of file
846 * descriptors that need to be monitored for events.
848 * This function behaves exactly like dbus_connection_set_watch_functions();
849 * see the documentation for that routine.
851 * @param server the server.
852 * @param add_function function to begin monitoring a new descriptor.
853 * @param remove_function function to stop monitoring a descriptor.
854 * @param toggled_function function to notify when the watch is enabled/disabled
855 * @param data data to pass to add_function and remove_function.
856 * @param free_data_function function to be called to free the data.
857 * @returns #FALSE on failure (no memory)
860 dbus_server_set_watch_functions (DBusServer *server,
861 DBusAddWatchFunction add_function,
862 DBusRemoveWatchFunction remove_function,
863 DBusWatchToggledFunction toggled_function,
865 DBusFreeFunction free_data_function)
868 DBusWatchList *watches;
870 _dbus_return_val_if_fail (server != NULL, FALSE);
872 SERVER_LOCK (server);
873 watches = server->watches;
874 server->watches = NULL;
877 SERVER_UNLOCK (server);
878 result = _dbus_watch_list_set_functions (watches,
884 SERVER_LOCK (server);
888 _dbus_warn_check_failed ("Re-entrant call to %s\n", _DBUS_FUNCTION_NAME);
891 server->watches = watches;
892 SERVER_UNLOCK (server);
898 * Sets the timeout functions for the connection. These functions are
899 * responsible for making the application's main loop aware of timeouts.
901 * This function behaves exactly like dbus_connection_set_timeout_functions();
902 * see the documentation for that routine.
904 * @param server the server.
905 * @param add_function function to add a timeout.
906 * @param remove_function function to remove a timeout.
907 * @param toggled_function function to notify when the timeout is enabled/disabled
908 * @param data data to pass to add_function and remove_function.
909 * @param free_data_function function to be called to free the data.
910 * @returns #FALSE on failure (no memory)
913 dbus_server_set_timeout_functions (DBusServer *server,
914 DBusAddTimeoutFunction add_function,
915 DBusRemoveTimeoutFunction remove_function,
916 DBusTimeoutToggledFunction toggled_function,
918 DBusFreeFunction free_data_function)
921 DBusTimeoutList *timeouts;
923 _dbus_return_val_if_fail (server != NULL, FALSE);
925 SERVER_LOCK (server);
926 timeouts = server->timeouts;
927 server->timeouts = NULL;
930 SERVER_UNLOCK (server);
931 result = _dbus_timeout_list_set_functions (timeouts,
937 SERVER_LOCK (server);
941 _dbus_warn_check_failed ("Re-entrant call to %s\n", _DBUS_FUNCTION_NAME);
944 server->timeouts = timeouts;
945 SERVER_UNLOCK (server);
951 * Sets the authentication mechanisms that this server offers
952 * to clients, as a list of SASL mechanisms. This function
953 * only affects connections created *after* it is called.
954 * Pass #NULL instead of an array to use all available mechanisms.
956 * @param server the server
957 * @param mechanisms #NULL-terminated array of mechanisms
958 * @returns #FALSE if no memory
961 dbus_server_set_auth_mechanisms (DBusServer *server,
962 const char **mechanisms)
966 _dbus_return_val_if_fail (server != NULL, FALSE);
968 SERVER_LOCK (server);
970 if (mechanisms != NULL)
972 copy = _dbus_dup_string_array (mechanisms);
979 dbus_free_string_array (server->auth_mechanisms);
980 server->auth_mechanisms = copy;
982 SERVER_UNLOCK (server);
988 static DBusDataSlotAllocator slot_allocator;
989 _DBUS_DEFINE_GLOBAL_LOCK (server_slots);
992 * Allocates an integer ID to be used for storing application-specific
993 * data on any DBusServer. The allocated ID may then be used
994 * with dbus_server_set_data() and dbus_server_get_data().
995 * The slot must be initialized with -1. If a nonnegative
996 * slot is passed in, the refcount is incremented on that
997 * slot, rather than creating a new slot.
999 * The allocated slot is global, i.e. all DBusServer objects will have
1000 * a slot with the given integer ID reserved.
1002 * @param slot_p address of global variable storing the slot ID
1003 * @returns #FALSE on no memory
1006 dbus_server_allocate_data_slot (dbus_int32_t *slot_p)
1008 return _dbus_data_slot_allocator_alloc (&slot_allocator,
1009 (DBusMutex **)&_DBUS_LOCK_NAME (server_slots),
1014 * Deallocates a global ID for server data slots.
1015 * dbus_server_get_data() and dbus_server_set_data()
1016 * may no longer be used with this slot.
1017 * Existing data stored on existing DBusServer objects
1018 * will be freed when the server is finalized,
1019 * but may not be retrieved (and may only be replaced
1020 * if someone else reallocates the slot).
1022 * @param slot_p address of the slot to deallocate
1025 dbus_server_free_data_slot (dbus_int32_t *slot_p)
1027 _dbus_return_if_fail (*slot_p >= 0);
1029 _dbus_data_slot_allocator_free (&slot_allocator, slot_p);
1033 * Stores a pointer on a DBusServer, along
1034 * with an optional function to be used for freeing
1035 * the data when the data is set again, or when
1036 * the server is finalized. The slot number
1037 * must have been allocated with dbus_server_allocate_data_slot().
1039 * @param server the server
1040 * @param slot the slot number
1041 * @param data the data to store
1042 * @param free_data_func finalizer function for the data
1043 * @returns #TRUE if there was enough memory to store the data
1046 dbus_server_set_data (DBusServer *server,
1049 DBusFreeFunction free_data_func)
1051 DBusFreeFunction old_free_func;
1055 _dbus_return_val_if_fail (server != NULL, FALSE);
1057 SERVER_LOCK (server);
1059 retval = _dbus_data_slot_list_set (&slot_allocator,
1061 slot, data, free_data_func,
1062 &old_free_func, &old_data);
1065 SERVER_UNLOCK (server);
1069 /* Do the actual free outside the server lock */
1071 (* old_free_func) (old_data);
1078 * Retrieves data previously set with dbus_server_set_data().
1079 * The slot must still be allocated (must not have been freed).
1081 * @param server the server
1082 * @param slot the slot to get data from
1083 * @returns the data, or #NULL if not found
1086 dbus_server_get_data (DBusServer *server,
1091 _dbus_return_val_if_fail (server != NULL, NULL);
1093 SERVER_LOCK (server);
1095 res = _dbus_data_slot_list_get (&slot_allocator,
1099 SERVER_UNLOCK (server);
1106 #ifdef DBUS_BUILD_TESTS
1107 #include "dbus-test.h"
1110 _dbus_server_test (void)
1112 const char *valid_addresses[] = {
1114 "unix:path=./boogie",
1115 "tcp:host=localhost,port=1234",
1116 "tcp:host=localhost,port=1234;tcp:port=5678",
1117 "tcp:port=1234;unix:path=./boogie",
1123 for (i = 0; i < _DBUS_N_ELEMENTS (valid_addresses); i++)
1127 /* FIXME um, how are the two tests here different? */
1129 dbus_error_init (&error);
1130 server = dbus_server_listen (valid_addresses[i], &error);
1133 _dbus_warn ("server listen error: %s: %s\n", error.name, error.message);
1134 dbus_error_free (&error);
1135 _dbus_assert_not_reached ("Failed to listen for valid address.");
1138 dbus_server_disconnect (server);
1139 dbus_server_unref (server);
1141 /* Try disconnecting before unreffing */
1142 server = dbus_server_listen (valid_addresses[i], &error);
1145 _dbus_warn ("server listen error: %s: %s\n", error.name, error.message);
1146 dbus_error_free (&error);
1147 _dbus_assert_not_reached ("Failed to listen for valid address.");
1150 dbus_server_disconnect (server);
1151 dbus_server_unref (server);
1157 #endif /* DBUS_BUILD_TESTS */