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 * A DBusServer represents a server that other applications
40 * can connect to. Each connection from another application
41 * is represented by a #DBusConnection.
43 * @todo Thread safety hasn't been tested much for #DBusServer
44 * @todo Need notification to apps of disconnection, may matter for some transports
48 * @defgroup DBusServerInternals DBusServer implementation details
49 * @ingroup DBusInternals
50 * @brief Implementation details of DBusServer
55 /* this is a little fragile since it assumes the address doesn't
56 * already have a guid, but it shouldn't
59 copy_address_with_guid_appended (const DBusString *address,
60 const DBusString *guid_hex)
65 if (!_dbus_string_init (&with_guid))
68 if (!_dbus_string_copy (address, 0, &with_guid,
69 _dbus_string_get_length (&with_guid)) ||
70 !_dbus_string_append (&with_guid, ",guid=") ||
71 !_dbus_string_copy (guid_hex, 0,
72 &with_guid, _dbus_string_get_length (&with_guid)))
74 _dbus_string_free (&with_guid);
79 _dbus_string_steal_data (&with_guid, &retval);
81 _dbus_string_free (&with_guid);
83 return retval; /* may be NULL if steal_data failed */
87 * Initializes the members of the DBusServer base class.
88 * Chained up to by subclass constructors.
90 * @param server the server.
91 * @param vtable the vtable for the subclass.
92 * @param address the server's address
93 * @returns #TRUE on success.
96 _dbus_server_init_base (DBusServer *server,
97 const DBusServerVTable *vtable,
98 const DBusString *address)
100 server->vtable = vtable;
101 server->refcount.value = 1;
103 server->address = NULL;
104 server->watches = NULL;
105 server->timeouts = NULL;
107 if (!_dbus_string_init (&server->guid_hex))
110 _dbus_generate_uuid (&server->guid);
112 if (!_dbus_uuid_encode (&server->guid, &server->guid_hex))
115 server->address = copy_address_with_guid_appended (address,
117 if (server->address == NULL)
120 _dbus_mutex_new_at_location (&server->mutex);
121 if (server->mutex == NULL)
124 server->watches = _dbus_watch_list_new ();
125 if (server->watches == NULL)
128 server->timeouts = _dbus_timeout_list_new ();
129 if (server->timeouts == NULL)
132 _dbus_data_slot_list_init (&server->slot_list);
134 _dbus_verbose ("Initialized server on address %s\n", server->address);
139 _dbus_mutex_free_at_location (&server->mutex);
140 server->mutex = NULL;
143 _dbus_watch_list_free (server->watches);
144 server->watches = NULL;
146 if (server->timeouts)
148 _dbus_timeout_list_free (server->timeouts);
149 server->timeouts = NULL;
153 dbus_free (server->address);
154 server->address = NULL;
156 _dbus_string_free (&server->guid_hex);
162 * Finalizes the members of the DBusServer base class.
163 * Chained up to by subclass finalizers.
165 * @param server the server.
168 _dbus_server_finalize_base (DBusServer *server)
170 /* We don't have the lock, but nobody should be accessing
171 * concurrently since they don't have a ref
173 #ifndef DBUS_DISABLE_CHECKS
174 _dbus_assert (!server->have_server_lock);
176 _dbus_assert (server->disconnected);
178 /* calls out to application code... */
179 _dbus_data_slot_list_free (&server->slot_list);
181 dbus_server_set_new_connection_function (server, NULL, NULL, NULL);
183 _dbus_watch_list_free (server->watches);
184 _dbus_timeout_list_free (server->timeouts);
186 _dbus_mutex_free_at_location (&server->mutex);
188 dbus_free (server->address);
190 dbus_free_string_array (server->auth_mechanisms);
192 _dbus_string_free (&server->guid_hex);
196 /** Function to be called in protected_change_watch() with refcount held */
197 typedef dbus_bool_t (* DBusWatchAddFunction) (DBusWatchList *list,
199 /** Function to be called in protected_change_watch() with refcount held */
200 typedef void (* DBusWatchRemoveFunction) (DBusWatchList *list,
202 /** Function to be called in protected_change_watch() with refcount held */
203 typedef void (* DBusWatchToggleFunction) (DBusWatchList *list,
205 dbus_bool_t enabled);
208 protected_change_watch (DBusServer *server,
210 DBusWatchAddFunction add_function,
211 DBusWatchRemoveFunction remove_function,
212 DBusWatchToggleFunction toggle_function,
215 DBusWatchList *watches;
218 HAVE_LOCK_CHECK (server);
220 /* This isn't really safe or reasonable; a better pattern is the "do
221 * everything, then drop lock and call out" one; but it has to be
222 * propagated up through all callers
225 watches = server->watches;
228 server->watches = NULL;
229 _dbus_server_ref_unlocked (server);
230 SERVER_UNLOCK (server);
233 retval = (* add_function) (watches, watch);
234 else if (remove_function)
237 (* remove_function) (watches, watch);
242 (* toggle_function) (watches, watch, enabled);
245 SERVER_LOCK (server);
246 server->watches = watches;
247 _dbus_server_unref_unlocked (server);
256 * Adds a watch for this server, chaining out to application-provided
259 * @param server the server.
260 * @param watch the watch to add.
263 _dbus_server_add_watch (DBusServer *server,
266 HAVE_LOCK_CHECK (server);
267 return protected_change_watch (server, watch,
268 _dbus_watch_list_add_watch,
273 * Removes a watch previously added with _dbus_server_remove_watch().
275 * @param server the server.
276 * @param watch the watch to remove.
279 _dbus_server_remove_watch (DBusServer *server,
282 HAVE_LOCK_CHECK (server);
283 protected_change_watch (server, watch,
285 _dbus_watch_list_remove_watch,
290 * Toggles a watch and notifies app via server's
291 * DBusWatchToggledFunction if available. It's an error to call this
292 * function on a watch that was not previously added.
294 * @param server the server.
295 * @param watch the watch to toggle.
296 * @param enabled whether to enable or disable
299 _dbus_server_toggle_watch (DBusServer *server,
303 _dbus_assert (watch != NULL);
305 HAVE_LOCK_CHECK (server);
306 protected_change_watch (server, watch,
308 _dbus_watch_list_toggle_watch,
312 /** Function to be called in protected_change_timeout() with refcount held */
313 typedef dbus_bool_t (* DBusTimeoutAddFunction) (DBusTimeoutList *list,
314 DBusTimeout *timeout);
315 /** Function to be called in protected_change_timeout() with refcount held */
316 typedef void (* DBusTimeoutRemoveFunction) (DBusTimeoutList *list,
317 DBusTimeout *timeout);
318 /** Function to be called in protected_change_timeout() with refcount held */
319 typedef void (* DBusTimeoutToggleFunction) (DBusTimeoutList *list,
320 DBusTimeout *timeout,
321 dbus_bool_t enabled);
325 protected_change_timeout (DBusServer *server,
326 DBusTimeout *timeout,
327 DBusTimeoutAddFunction add_function,
328 DBusTimeoutRemoveFunction remove_function,
329 DBusTimeoutToggleFunction toggle_function,
332 DBusTimeoutList *timeouts;
335 HAVE_LOCK_CHECK (server);
337 /* This isn't really safe or reasonable; a better pattern is the "do everything, then
338 * drop lock and call out" one; but it has to be propagated up through all callers
341 timeouts = server->timeouts;
344 server->timeouts = NULL;
345 _dbus_server_ref_unlocked (server);
346 SERVER_UNLOCK (server);
349 retval = (* add_function) (timeouts, timeout);
350 else if (remove_function)
353 (* remove_function) (timeouts, timeout);
358 (* toggle_function) (timeouts, timeout, enabled);
361 SERVER_LOCK (server);
362 server->timeouts = timeouts;
363 _dbus_server_unref_unlocked (server);
372 * Adds a timeout for this server, chaining out to
373 * application-provided timeout handlers. The timeout should be
374 * repeatedly handled with dbus_timeout_handle() at its given interval
375 * until it is removed.
377 * @param server the server.
378 * @param timeout the timeout to add.
381 _dbus_server_add_timeout (DBusServer *server,
382 DBusTimeout *timeout)
384 return protected_change_timeout (server, timeout,
385 _dbus_timeout_list_add_timeout,
390 * Removes a timeout previously added with _dbus_server_add_timeout().
392 * @param server the server.
393 * @param timeout the timeout to remove.
396 _dbus_server_remove_timeout (DBusServer *server,
397 DBusTimeout *timeout)
399 protected_change_timeout (server, timeout,
401 _dbus_timeout_list_remove_timeout,
406 * Toggles a timeout and notifies app via server's
407 * DBusTimeoutToggledFunction if available. It's an error to call this
408 * function on a timeout that was not previously added.
410 * @param server the server.
411 * @param timeout the timeout to toggle.
412 * @param enabled whether to enable or disable
415 _dbus_server_toggle_timeout (DBusServer *server,
416 DBusTimeout *timeout,
419 protected_change_timeout (server, timeout,
421 _dbus_timeout_list_toggle_timeout,
427 * Like dbus_server_ref() but does not acquire the lock (must already be held)
429 * @param server the server.
432 _dbus_server_ref_unlocked (DBusServer *server)
434 _dbus_assert (server != NULL);
435 _dbus_assert (server->refcount.value > 0);
437 HAVE_LOCK_CHECK (server);
439 #ifdef DBUS_HAVE_ATOMIC_INT
440 _dbus_atomic_inc (&server->refcount);
442 _dbus_assert (server->refcount.value > 0);
444 server->refcount.value += 1;
449 * Like dbus_server_unref() but does not acquire the lock (must already be held)
451 * @param server the server.
454 _dbus_server_unref_unlocked (DBusServer *server)
456 dbus_bool_t last_unref;
458 /* Keep this in sync with dbus_server_unref */
460 _dbus_assert (server != NULL);
461 _dbus_assert (server->refcount.value > 0);
463 HAVE_LOCK_CHECK (server);
465 #ifdef DBUS_HAVE_ATOMIC_INT
466 last_unref = (_dbus_atomic_dec (&server->refcount) == 1);
468 _dbus_assert (server->refcount.value > 0);
470 server->refcount.value -= 1;
471 last_unref = (server->refcount.value == 0);
476 _dbus_assert (server->disconnected);
478 SERVER_UNLOCK (server);
480 _dbus_assert (server->vtable->finalize != NULL);
482 (* server->vtable->finalize) (server);
489 * @addtogroup DBusServer
496 * @typedef DBusServer
498 * An opaque object representing a server that listens for
499 * connections from other applications. Each time a connection
500 * is made, a new DBusConnection is created and made available
501 * via an application-provided DBusNewConnectionFunction.
502 * The DBusNewConnectionFunction is provided with
503 * dbus_server_set_new_connection_function().
507 static const struct {
508 DBusServerListenResult (* func) (DBusAddressEntry *entry,
509 DBusServer **server_p,
512 { _dbus_server_listen_socket }
514 , { _dbus_server_listen_platform_specific }
516 #ifdef DBUS_BUILD_TESTS
517 , { _dbus_server_listen_debug_pipe }
522 * Listens for new connections on the given address. If there are
523 * multiple semicolon-separated address entries in the address, tries
524 * each one and listens on the first one that works.
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(),
529 * dbus_server_set_watch_functions(), and
530 * dbus_server_set_timeout_functions() should be called immediately to
531 * render the server fully functional.
533 * To free the server, applications must call first
534 * dbus_server_disconnect() and then dbus_server_unref().
536 * @param address the address of this server.
537 * @param error location to store reason for failure.
538 * @returns a new #DBusServer, or #NULL on failure.
542 dbus_server_listen (const char *address,
546 DBusAddressEntry **entries;
548 DBusError first_connect_error;
549 dbus_bool_t handled_once;
551 _dbus_return_val_if_fail (address != NULL, NULL);
552 _dbus_return_val_if_error_is_set (error, NULL);
554 if (!dbus_parse_address (address, &entries, &len, error))
558 dbus_error_init (&first_connect_error);
559 handled_once = FALSE;
561 for (i = 0; i < len; i++)
565 for (j = 0; j < (int) _DBUS_N_ELEMENTS (listen_funcs); ++j)
567 DBusServerListenResult result;
570 dbus_error_init (&tmp_error);
571 result = (* listen_funcs[j].func) (entries[i],
575 if (result == DBUS_SERVER_LISTEN_OK)
577 _dbus_assert (server != NULL);
578 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
582 else if (result == DBUS_SERVER_LISTEN_BAD_ADDRESS)
584 _dbus_assert (server == NULL);
585 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
586 dbus_move_error (&tmp_error, error);
590 else if (result == DBUS_SERVER_LISTEN_NOT_HANDLED)
592 _dbus_assert (server == NULL);
593 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
595 /* keep trying addresses */
597 else if (result == DBUS_SERVER_LISTEN_DID_NOT_CONNECT)
599 _dbus_assert (server == NULL);
600 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
601 if (!dbus_error_is_set (&first_connect_error))
602 dbus_move_error (&tmp_error, &first_connect_error);
604 dbus_error_free (&tmp_error);
608 /* keep trying addresses */
612 _dbus_assert (server == NULL);
613 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
620 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
622 dbus_set_error (error,
623 DBUS_ERROR_BAD_ADDRESS,
624 "Unknown address type '%s'",
625 dbus_address_entry_get_method (entries[0]));
627 dbus_set_error (error,
628 DBUS_ERROR_BAD_ADDRESS,
629 "Empty address '%s'",
633 dbus_address_entries_free (entries);
637 _dbus_assert (error == NULL || dbus_error_is_set (&first_connect_error) ||
638 dbus_error_is_set (error));
640 if (error && dbus_error_is_set (error))
642 /* already set the error */
646 /* didn't set the error but either error should be
647 * NULL or first_connect_error should be set.
649 _dbus_assert (error == NULL || dbus_error_is_set (&first_connect_error));
650 dbus_move_error (&first_connect_error, error);
653 _DBUS_ASSERT_ERROR_IS_CLEAR (&first_connect_error); /* be sure we freed it */
654 _DBUS_ASSERT_ERROR_IS_SET (error);
660 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
666 * Increments the reference count of a DBusServer.
668 * @param server the server.
669 * @returns the server
672 dbus_server_ref (DBusServer *server)
674 _dbus_return_val_if_fail (server != NULL, NULL);
675 _dbus_return_val_if_fail (server->refcount.value > 0, NULL);
677 #ifdef DBUS_HAVE_ATOMIC_INT
678 _dbus_atomic_inc (&server->refcount);
680 SERVER_LOCK (server);
681 _dbus_assert (server->refcount.value > 0);
683 server->refcount.value += 1;
684 SERVER_UNLOCK (server);
691 * Decrements the reference count of a DBusServer. Finalizes the
692 * server if the reference count reaches zero.
694 * The server must be disconnected before the refcount reaches zero.
696 * @param server the server.
699 dbus_server_unref (DBusServer *server)
701 dbus_bool_t last_unref;
703 /* keep this in sync with unref_unlocked */
705 _dbus_return_if_fail (server != NULL);
706 _dbus_return_if_fail (server->refcount.value > 0);
708 #ifdef DBUS_HAVE_ATOMIC_INT
709 last_unref = (_dbus_atomic_dec (&server->refcount) == 1);
711 SERVER_LOCK (server);
713 _dbus_assert (server->refcount.value > 0);
715 server->refcount.value -= 1;
716 last_unref = (server->refcount.value == 0);
718 SERVER_UNLOCK (server);
724 _dbus_assert (server->disconnected);
726 _dbus_assert (server->vtable->finalize != NULL);
728 (* server->vtable->finalize) (server);
733 * Releases the server's address and stops listening for
734 * new clients. If called more than once, only the first
735 * call has an effect. Does not modify the server's
738 * @param server the server.
741 dbus_server_disconnect (DBusServer *server)
743 _dbus_return_if_fail (server != NULL);
744 _dbus_return_if_fail (server->refcount.value > 0);
746 SERVER_LOCK (server);
747 _dbus_server_ref_unlocked (server);
749 _dbus_assert (server->vtable->disconnect != NULL);
751 if (!server->disconnected)
753 /* this has to be first so recursive calls to disconnect don't happen */
754 server->disconnected = TRUE;
756 (* server->vtable->disconnect) (server);
759 SERVER_UNLOCK (server);
760 dbus_server_unref (server);
764 * Returns #TRUE if the server is still listening for new connections.
766 * @param server the server.
769 dbus_server_get_is_connected (DBusServer *server)
773 _dbus_return_val_if_fail (server != NULL, FALSE);
775 SERVER_LOCK (server);
776 retval = !server->disconnected;
777 SERVER_UNLOCK (server);
783 * Returns the address of the server, as a newly-allocated
784 * string which must be freed by the caller.
786 * @param server the server
787 * @returns the address or #NULL if no memory
790 dbus_server_get_address (DBusServer *server)
794 _dbus_return_val_if_fail (server != NULL, NULL);
796 SERVER_LOCK (server);
797 retval = _dbus_strdup (server->address);
798 SERVER_UNLOCK (server);
804 * Sets a function to be used for handling new connections. The given
805 * function is passed each new connection as the connection is
806 * created. If the new connection function increments the connection's
807 * reference count, the connection will stay alive. Otherwise, the
808 * connection will be unreferenced and closed. The new connection
809 * function may also close the connection itself, which is considered
810 * good form if the connection is not wanted.
812 * The connection here is private in the sense of
813 * dbus_connection_open_private(), so if the new connection function
814 * keeps a reference it must arrange for the connection to be closed.
815 * i.e. libdbus does not own this connection once the new connection
816 * function takes a reference.
818 * @param server the server.
819 * @param function a function to handle new connections.
820 * @param data data to pass to the new connection handler.
821 * @param free_data_function function to free the data.
824 dbus_server_set_new_connection_function (DBusServer *server,
825 DBusNewConnectionFunction function,
827 DBusFreeFunction free_data_function)
829 DBusFreeFunction old_free_function;
832 _dbus_return_if_fail (server != NULL);
834 SERVER_LOCK (server);
835 old_free_function = server->new_connection_free_data_function;
836 old_data = server->new_connection_data;
838 server->new_connection_function = function;
839 server->new_connection_data = data;
840 server->new_connection_free_data_function = free_data_function;
841 SERVER_UNLOCK (server);
843 if (old_free_function != NULL)
844 (* old_free_function) (old_data);
848 * Sets the watch functions for the server. These functions are
849 * responsible for making the application's main loop aware of file
850 * descriptors that need to be monitored for events.
852 * This function behaves exactly like dbus_connection_set_watch_functions();
853 * see the documentation for that routine.
855 * @param server the server.
856 * @param add_function function to begin monitoring a new descriptor.
857 * @param remove_function function to stop monitoring a descriptor.
858 * @param toggled_function function to notify when the watch is enabled/disabled
859 * @param data data to pass to add_function and remove_function.
860 * @param free_data_function function to be called to free the data.
861 * @returns #FALSE on failure (no memory)
864 dbus_server_set_watch_functions (DBusServer *server,
865 DBusAddWatchFunction add_function,
866 DBusRemoveWatchFunction remove_function,
867 DBusWatchToggledFunction toggled_function,
869 DBusFreeFunction free_data_function)
872 DBusWatchList *watches;
874 _dbus_return_val_if_fail (server != NULL, FALSE);
876 SERVER_LOCK (server);
877 watches = server->watches;
878 server->watches = NULL;
881 SERVER_UNLOCK (server);
882 result = _dbus_watch_list_set_functions (watches,
888 SERVER_LOCK (server);
892 _dbus_warn_check_failed ("Re-entrant call to %s\n", _DBUS_FUNCTION_NAME);
895 server->watches = watches;
896 SERVER_UNLOCK (server);
902 * Sets the timeout functions for the server. These functions are
903 * responsible for making the application's main loop aware of timeouts.
905 * This function behaves exactly like dbus_connection_set_timeout_functions();
906 * see the documentation for that routine.
908 * @param server the server.
909 * @param add_function function to add a timeout.
910 * @param remove_function function to remove a timeout.
911 * @param toggled_function function to notify when the timeout is enabled/disabled
912 * @param data data to pass to add_function and remove_function.
913 * @param free_data_function function to be called to free the data.
914 * @returns #FALSE on failure (no memory)
917 dbus_server_set_timeout_functions (DBusServer *server,
918 DBusAddTimeoutFunction add_function,
919 DBusRemoveTimeoutFunction remove_function,
920 DBusTimeoutToggledFunction toggled_function,
922 DBusFreeFunction free_data_function)
925 DBusTimeoutList *timeouts;
927 _dbus_return_val_if_fail (server != NULL, FALSE);
929 SERVER_LOCK (server);
930 timeouts = server->timeouts;
931 server->timeouts = NULL;
934 SERVER_UNLOCK (server);
935 result = _dbus_timeout_list_set_functions (timeouts,
941 SERVER_LOCK (server);
945 _dbus_warn_check_failed ("Re-entrant call to %s\n", _DBUS_FUNCTION_NAME);
948 server->timeouts = timeouts;
949 SERVER_UNLOCK (server);
955 * Sets the authentication mechanisms that this server offers to
956 * clients, as a #NULL-terminated array of mechanism names. This
957 * function only affects connections created <em>after</em> it is
958 * called. Pass #NULL instead of an array to use all available
959 * mechanisms (this is the default behavior).
961 * The D-Bus specification describes some of the supported mechanisms.
963 * @param server the server
964 * @param mechanisms #NULL-terminated array of mechanisms
965 * @returns #FALSE if no memory
968 dbus_server_set_auth_mechanisms (DBusServer *server,
969 const char **mechanisms)
973 _dbus_return_val_if_fail (server != NULL, FALSE);
975 SERVER_LOCK (server);
977 if (mechanisms != NULL)
979 copy = _dbus_dup_string_array (mechanisms);
986 dbus_free_string_array (server->auth_mechanisms);
987 server->auth_mechanisms = copy;
989 SERVER_UNLOCK (server);
995 static DBusDataSlotAllocator slot_allocator;
996 _DBUS_DEFINE_GLOBAL_LOCK (server_slots);
999 * Allocates an integer ID to be used for storing application-specific
1000 * data on any DBusServer. The allocated ID may then be used
1001 * with dbus_server_set_data() and dbus_server_get_data().
1002 * The slot must be initialized with -1. If a nonnegative
1003 * slot is passed in, the refcount is incremented on that
1004 * slot, rather than creating a new slot.
1006 * The allocated slot is global, i.e. all DBusServer objects will have
1007 * a slot with the given integer ID reserved.
1009 * @param slot_p address of global variable storing the slot ID
1010 * @returns #FALSE on no memory
1013 dbus_server_allocate_data_slot (dbus_int32_t *slot_p)
1015 return _dbus_data_slot_allocator_alloc (&slot_allocator,
1016 (DBusMutex **)&_DBUS_LOCK_NAME (server_slots),
1021 * Deallocates a global ID for server data slots.
1022 * dbus_server_get_data() and dbus_server_set_data()
1023 * may no longer be used with this slot.
1024 * Existing data stored on existing DBusServer objects
1025 * will be freed when the server is finalized,
1026 * but may not be retrieved (and may only be replaced
1027 * if someone else reallocates the slot).
1029 * @param slot_p address of the slot to deallocate
1032 dbus_server_free_data_slot (dbus_int32_t *slot_p)
1034 _dbus_return_if_fail (*slot_p >= 0);
1036 _dbus_data_slot_allocator_free (&slot_allocator, slot_p);
1040 * Stores a pointer on a DBusServer, along
1041 * with an optional function to be used for freeing
1042 * the data when the data is set again, or when
1043 * the server is finalized. The slot number
1044 * must have been allocated with dbus_server_allocate_data_slot().
1046 * @param server the server
1047 * @param slot the slot number
1048 * @param data the data to store
1049 * @param free_data_func finalizer function for the data
1050 * @returns #TRUE if there was enough memory to store the data
1053 dbus_server_set_data (DBusServer *server,
1056 DBusFreeFunction free_data_func)
1058 DBusFreeFunction old_free_func;
1062 _dbus_return_val_if_fail (server != NULL, FALSE);
1064 SERVER_LOCK (server);
1066 retval = _dbus_data_slot_list_set (&slot_allocator,
1068 slot, data, free_data_func,
1069 &old_free_func, &old_data);
1072 SERVER_UNLOCK (server);
1076 /* Do the actual free outside the server lock */
1078 (* old_free_func) (old_data);
1085 * Retrieves data previously set with dbus_server_set_data().
1086 * The slot must still be allocated (must not have been freed).
1088 * @param server the server
1089 * @param slot the slot to get data from
1090 * @returns the data, or #NULL if not found
1093 dbus_server_get_data (DBusServer *server,
1098 _dbus_return_val_if_fail (server != NULL, NULL);
1100 SERVER_LOCK (server);
1102 res = _dbus_data_slot_list_get (&slot_allocator,
1106 SERVER_UNLOCK (server);
1113 #ifdef DBUS_BUILD_TESTS
1114 #include "dbus-test.h"
1117 _dbus_server_test (void)
1119 const char *valid_addresses[] = {
1121 "tcp:host=localhost,port=1234",
1122 "tcp:host=localhost,port=1234;tcp:port=5678",
1124 "unix:path=./boogie",
1125 "tcp:port=1234;unix:path=./boogie",
1132 for (i = 0; i < _DBUS_N_ELEMENTS (valid_addresses); i++)
1136 /* FIXME um, how are the two tests here different? */
1138 dbus_error_init (&error);
1139 server = dbus_server_listen (valid_addresses[i], &error);
1142 _dbus_warn ("server listen error: %s: %s\n", error.name, error.message);
1143 dbus_error_free (&error);
1144 _dbus_assert_not_reached ("Failed to listen for valid address.");
1147 dbus_server_disconnect (server);
1148 dbus_server_unref (server);
1150 /* Try disconnecting before unreffing */
1151 server = dbus_server_listen (valid_addresses[i], &error);
1154 _dbus_warn ("server listen error: %s: %s\n", error.name, error.message);
1155 dbus_error_free (&error);
1156 _dbus_assert_not_reached ("Failed to listen for valid address.");
1159 dbus_server_disconnect (server);
1160 dbus_server_unref (server);
1166 #endif /* DBUS_BUILD_TESTS */