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
57 init_guid (DBusGUID *guid)
63 _dbus_get_current_time (&now, NULL);
65 guid->as_uint32s[0] = now;
67 ts_size = sizeof (guid->as_uint32s[0]);
68 p = ((char*)guid->as_bytes) + ts_size;
70 _dbus_generate_random_bytes_buffer (p,
71 sizeof (guid->as_bytes) - ts_size);
74 /* this is a little fragile since it assumes the address doesn't
75 * already have a guid, but it shouldn't
78 copy_address_with_guid_appended (const DBusString *address,
79 const DBusString *guid_hex)
84 if (!_dbus_string_init (&with_guid))
87 if (!_dbus_string_copy (address, 0, &with_guid,
88 _dbus_string_get_length (&with_guid)) ||
89 !_dbus_string_append (&with_guid, ",guid=") ||
90 !_dbus_string_copy (guid_hex, 0,
91 &with_guid, _dbus_string_get_length (&with_guid)))
93 _dbus_string_free (&with_guid);
98 _dbus_string_steal_data (&with_guid, &retval);
100 _dbus_string_free (&with_guid);
102 return retval; /* may be NULL if steal_data failed */
106 * Initializes the members of the DBusServer base class.
107 * Chained up to by subclass constructors.
109 * @param server the server.
110 * @param vtable the vtable for the subclass.
111 * @param address the server's address
112 * @returns #TRUE on success.
115 _dbus_server_init_base (DBusServer *server,
116 const DBusServerVTable *vtable,
117 const DBusString *address)
121 server->vtable = vtable;
122 server->refcount.value = 1;
124 server->address = NULL;
125 server->watches = NULL;
126 server->timeouts = NULL;
128 if (!_dbus_string_init (&server->guid_hex))
131 init_guid (&server->guid);
133 _dbus_string_init_const_len (&guid_raw, (signed char*) server->guid.as_bytes,
134 sizeof (server->guid.as_bytes));
135 if (!_dbus_string_hex_encode (&guid_raw, 0,
137 _dbus_string_get_length (&server->guid_hex)))
140 server->address = copy_address_with_guid_appended (address,
142 if (server->address == NULL)
145 _dbus_mutex_new_at_location (&server->mutex);
146 if (server->mutex == NULL)
149 server->watches = _dbus_watch_list_new ();
150 if (server->watches == NULL)
153 server->timeouts = _dbus_timeout_list_new ();
154 if (server->timeouts == NULL)
157 _dbus_data_slot_list_init (&server->slot_list);
159 _dbus_verbose ("Initialized server on address %s\n", server->address);
164 _dbus_mutex_free_at_location (&server->mutex);
165 server->mutex = NULL;
168 _dbus_watch_list_free (server->watches);
169 server->watches = NULL;
171 if (server->timeouts)
173 _dbus_timeout_list_free (server->timeouts);
174 server->timeouts = NULL;
178 dbus_free (server->address);
179 server->address = NULL;
181 _dbus_string_free (&server->guid_hex);
187 * Finalizes the members of the DBusServer base class.
188 * Chained up to by subclass finalizers.
190 * @param server the server.
193 _dbus_server_finalize_base (DBusServer *server)
195 /* We don't have the lock, but nobody should be accessing
196 * concurrently since they don't have a ref
198 #ifndef DBUS_DISABLE_CHECKS
199 _dbus_assert (!server->have_server_lock);
201 _dbus_assert (server->disconnected);
203 /* calls out to application code... */
204 _dbus_data_slot_list_free (&server->slot_list);
206 dbus_server_set_new_connection_function (server, NULL, NULL, NULL);
208 _dbus_watch_list_free (server->watches);
209 _dbus_timeout_list_free (server->timeouts);
211 _dbus_mutex_free_at_location (&server->mutex);
213 dbus_free (server->address);
215 dbus_free_string_array (server->auth_mechanisms);
217 _dbus_string_free (&server->guid_hex);
221 typedef dbus_bool_t (* DBusWatchAddFunction) (DBusWatchList *list,
223 typedef void (* DBusWatchRemoveFunction) (DBusWatchList *list,
225 typedef void (* DBusWatchToggleFunction) (DBusWatchList *list,
227 dbus_bool_t enabled);
230 protected_change_watch (DBusServer *server,
232 DBusWatchAddFunction add_function,
233 DBusWatchRemoveFunction remove_function,
234 DBusWatchToggleFunction toggle_function,
237 DBusWatchList *watches;
240 HAVE_LOCK_CHECK (server);
242 /* This isn't really safe or reasonable; a better pattern is the "do
243 * everything, then drop lock and call out" one; but it has to be
244 * propagated up through all callers
247 watches = server->watches;
250 server->watches = NULL;
251 _dbus_server_ref_unlocked (server);
252 SERVER_UNLOCK (server);
255 retval = (* add_function) (watches, watch);
256 else if (remove_function)
259 (* remove_function) (watches, watch);
264 (* toggle_function) (watches, watch, enabled);
267 SERVER_LOCK (server);
268 server->watches = watches;
269 _dbus_server_unref_unlocked (server);
278 * Adds a watch for this server, chaining out to application-provided
281 * @param server the server.
282 * @param watch the watch to add.
285 _dbus_server_add_watch (DBusServer *server,
288 HAVE_LOCK_CHECK (server);
289 return protected_change_watch (server, watch,
290 _dbus_watch_list_add_watch,
295 * Removes a watch previously added with _dbus_server_remove_watch().
297 * @param server the server.
298 * @param watch the watch to remove.
301 _dbus_server_remove_watch (DBusServer *server,
304 HAVE_LOCK_CHECK (server);
305 protected_change_watch (server, watch,
307 _dbus_watch_list_remove_watch,
312 * Toggles a watch and notifies app via server's
313 * DBusWatchToggledFunction if available. It's an error to call this
314 * function on a watch that was not previously added.
316 * @param server the server.
317 * @param watch the watch to toggle.
318 * @param enabled whether to enable or disable
321 _dbus_server_toggle_watch (DBusServer *server,
325 _dbus_assert (watch != NULL);
327 HAVE_LOCK_CHECK (server);
328 protected_change_watch (server, watch,
330 _dbus_watch_list_toggle_watch,
335 typedef dbus_bool_t (* DBusTimeoutAddFunction) (DBusTimeoutList *list,
336 DBusTimeout *timeout);
337 typedef void (* DBusTimeoutRemoveFunction) (DBusTimeoutList *list,
338 DBusTimeout *timeout);
339 typedef void (* DBusTimeoutToggleFunction) (DBusTimeoutList *list,
340 DBusTimeout *timeout,
341 dbus_bool_t enabled);
345 protected_change_timeout (DBusServer *server,
346 DBusTimeout *timeout,
347 DBusTimeoutAddFunction add_function,
348 DBusTimeoutRemoveFunction remove_function,
349 DBusTimeoutToggleFunction toggle_function,
352 DBusTimeoutList *timeouts;
355 HAVE_LOCK_CHECK (server);
357 /* This isn't really safe or reasonable; a better pattern is the "do everything, then
358 * drop lock and call out" one; but it has to be propagated up through all callers
361 timeouts = server->timeouts;
364 server->timeouts = NULL;
365 _dbus_server_ref_unlocked (server);
366 SERVER_UNLOCK (server);
369 retval = (* add_function) (timeouts, timeout);
370 else if (remove_function)
373 (* remove_function) (timeouts, timeout);
378 (* toggle_function) (timeouts, timeout, enabled);
381 SERVER_LOCK (server);
382 server->timeouts = timeouts;
383 _dbus_server_unref_unlocked (server);
392 * Adds a timeout for this server, chaining out to
393 * application-provided timeout handlers. The timeout should be
394 * repeatedly handled with dbus_timeout_handle() at its given interval
395 * until it is removed.
397 * @param server the server.
398 * @param timeout the timeout to add.
401 _dbus_server_add_timeout (DBusServer *server,
402 DBusTimeout *timeout)
404 return protected_change_timeout (server, timeout,
405 _dbus_timeout_list_add_timeout,
410 * Removes a timeout previously added with _dbus_server_add_timeout().
412 * @param server the server.
413 * @param timeout the timeout to remove.
416 _dbus_server_remove_timeout (DBusServer *server,
417 DBusTimeout *timeout)
419 protected_change_timeout (server, timeout,
421 _dbus_timeout_list_remove_timeout,
426 * Toggles a timeout and notifies app via server's
427 * DBusTimeoutToggledFunction if available. It's an error to call this
428 * function on a timeout that was not previously added.
430 * @param server the server.
431 * @param timeout the timeout to toggle.
432 * @param enabled whether to enable or disable
435 _dbus_server_toggle_timeout (DBusServer *server,
436 DBusTimeout *timeout,
439 protected_change_timeout (server, timeout,
441 _dbus_timeout_list_toggle_timeout,
446 _dbus_server_set_bad_address (DBusError *error,
447 const char *address_problem_type,
448 const char *address_problem_field,
449 const char *address_problem_other)
451 if (address_problem_type != NULL)
452 dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS,
453 "Server address of type %s was missing argument %s",
454 address_problem_type, address_problem_field);
456 dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS,
457 "Could not parse server address: %s",
458 address_problem_other);
464 * @addtogroup DBusServer
471 * @typedef DBusServer
473 * An opaque object representing a server that listens for
474 * connections from other applications. Each time a connection
475 * is made, a new DBusConnection is created and made available
476 * via an application-provided DBusNewConnectionFunction.
477 * The DBusNewConnectionFunction is provided with
478 * dbus_server_set_new_connection_function().
482 static const struct {
483 DBusServerListenResult (* func) (DBusAddressEntry *entry,
484 DBusServer **server_p,
487 { _dbus_server_listen_socket },
488 { _dbus_server_listen_platform_specific }
489 #ifdef DBUS_BUILD_TESTS
490 , { _dbus_server_listen_debug_pipe }
495 * Listens for new connections on the given address.
496 * Returns #NULL if listening fails for any reason.
497 * Otherwise returns a new #DBusServer.
498 * dbus_server_set_new_connection_function() and
499 * dbus_server_set_watch_functions() should be called
500 * immediately to render the server fully functional.
502 * @param address the address of this server.
503 * @param error location to store rationale for failure.
504 * @returns a new DBusServer, or #NULL on failure.
508 dbus_server_listen (const char *address,
512 DBusAddressEntry **entries;
514 DBusError first_connect_error;
515 dbus_bool_t handled_once;
517 _dbus_return_val_if_fail (address != NULL, NULL);
518 _dbus_return_val_if_error_is_set (error, NULL);
520 if (!dbus_parse_address (address, &entries, &len, error))
524 dbus_error_init (&first_connect_error);
525 handled_once = FALSE;
527 for (i = 0; i < len; i++)
531 for (j = 0; j < (int) _DBUS_N_ELEMENTS (listen_funcs); ++j)
533 DBusServerListenResult result;
536 dbus_error_init (&tmp_error);
537 result = (* listen_funcs[j].func) (entries[i],
541 if (result == DBUS_SERVER_LISTEN_OK)
543 _dbus_assert (server != NULL);
544 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
548 else if (result == DBUS_SERVER_LISTEN_BAD_ADDRESS)
550 _dbus_assert (server == NULL);
551 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
552 dbus_move_error (&tmp_error, error);
556 else if (result == DBUS_SERVER_LISTEN_NOT_HANDLED)
558 _dbus_assert (server == NULL);
559 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
561 /* keep trying addresses */
563 else if (result == DBUS_SERVER_LISTEN_DID_NOT_CONNECT)
565 _dbus_assert (server == NULL);
566 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
567 if (!dbus_error_is_set (&first_connect_error))
568 dbus_move_error (&tmp_error, &first_connect_error);
570 dbus_error_free (&tmp_error);
574 /* keep trying addresses */
578 _dbus_assert (server == NULL);
579 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
586 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
588 dbus_set_error (error,
589 DBUS_ERROR_BAD_ADDRESS,
590 "Unknown address type '%s'",
591 dbus_address_entry_get_method (entries[0]));
593 dbus_set_error (error,
594 DBUS_ERROR_BAD_ADDRESS,
595 "Empty address '%s'",
599 dbus_address_entries_free (entries);
603 _dbus_assert (error == NULL || dbus_error_is_set (&first_connect_error) ||
604 dbus_error_is_set (error));
606 if (error && dbus_error_is_set (error))
608 /* already set the error */
612 /* didn't set the error but either error should be
613 * NULL or first_connect_error should be set.
615 _dbus_assert (error == NULL || dbus_error_is_set (&first_connect_error));
616 dbus_move_error (&first_connect_error, error);
619 _DBUS_ASSERT_ERROR_IS_CLEAR (&first_connect_error); /* be sure we freed it */
620 _DBUS_ASSERT_ERROR_IS_SET (error);
626 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
632 * Increments the reference count of a DBusServer.
634 * @param server the server.
635 * @returns the server
638 dbus_server_ref (DBusServer *server)
640 _dbus_return_val_if_fail (server != NULL, NULL);
641 _dbus_return_val_if_fail (server->refcount.value > 0, NULL);
643 #ifdef DBUS_HAVE_ATOMIC_INT
644 _dbus_atomic_inc (&server->refcount);
646 SERVER_LOCK (server);
647 _dbus_assert (server->refcount.value > 0);
649 server->refcount.value += 1;
650 SERVER_UNLOCK (server);
657 * Decrements the reference count of a DBusServer. Finalizes the
658 * server if the reference count reaches zero.
660 * The server must be disconnected before the refcount reaches zero.
662 * @param server the server.
665 dbus_server_unref (DBusServer *server)
667 dbus_bool_t last_unref;
669 _dbus_return_if_fail (server != NULL);
670 _dbus_return_if_fail (server->refcount.value > 0);
672 #ifdef DBUS_HAVE_ATOMIC_INT
673 last_unref = (_dbus_atomic_dec (&server->refcount) == 1);
675 SERVER_LOCK (server);
677 _dbus_assert (server->refcount.value > 0);
679 server->refcount.value -= 1;
680 last_unref = (server->refcount.value == 0);
682 SERVER_UNLOCK (server);
688 _dbus_assert (server->disconnected);
690 _dbus_assert (server->vtable->finalize != NULL);
692 (* server->vtable->finalize) (server);
697 * Like dbus_server_ref() but does not acquire the lock (must already be held)
699 * @param server the server.
702 _dbus_server_ref_unlocked (DBusServer *server)
704 _dbus_assert (server != NULL);
705 _dbus_assert (server->refcount.value > 0);
707 HAVE_LOCK_CHECK (server);
709 #ifdef DBUS_HAVE_ATOMIC_INT
710 _dbus_atomic_inc (&server->refcount);
712 _dbus_assert (server->refcount.value > 0);
714 server->refcount.value += 1;
719 * Like dbus_server_unref() but does not acquire the lock (must already be held)
721 * @param server the server.
724 _dbus_server_unref_unlocked (DBusServer *server)
726 dbus_bool_t last_unref;
728 _dbus_assert (server != NULL);
729 _dbus_assert (server->refcount.value > 0);
731 HAVE_LOCK_CHECK (server);
733 #ifdef DBUS_HAVE_ATOMIC_INT
734 last_unref = (_dbus_atomic_dec (&server->refcount) == 1);
736 _dbus_assert (server->refcount.value > 0);
738 server->refcount.value -= 1;
739 last_unref = (server->refcount.value == 0);
744 _dbus_assert (server->disconnected);
746 SERVER_UNLOCK (server);
748 _dbus_assert (server->vtable->finalize != NULL);
750 (* server->vtable->finalize) (server);
755 * Releases the server's address and stops listening for
756 * new clients. If called more than once, only the first
757 * call has an effect. Does not modify the server's
760 * @param server the server.
763 dbus_server_disconnect (DBusServer *server)
765 _dbus_return_if_fail (server != NULL);
766 _dbus_return_if_fail (server->refcount.value > 0);
768 SERVER_LOCK (server);
769 _dbus_server_ref_unlocked (server);
771 _dbus_assert (server->vtable->disconnect != NULL);
773 if (!server->disconnected)
775 /* this has to be first so recursive calls to disconnect don't happen */
776 server->disconnected = TRUE;
778 (* server->vtable->disconnect) (server);
781 SERVER_UNLOCK (server);
782 dbus_server_unref (server);
786 * Returns #TRUE if the server is still listening for new connections.
788 * @param server the server.
791 dbus_server_get_is_connected (DBusServer *server)
795 _dbus_return_val_if_fail (server != NULL, FALSE);
797 SERVER_LOCK (server);
798 retval = !server->disconnected;
799 SERVER_UNLOCK (server);
805 * Returns the address of the server, as a newly-allocated
806 * string which must be freed by the caller.
808 * @param server the server
809 * @returns the address or #NULL if no memory
812 dbus_server_get_address (DBusServer *server)
816 _dbus_return_val_if_fail (server != NULL, NULL);
818 SERVER_LOCK (server);
819 retval = _dbus_strdup (server->address);
820 SERVER_UNLOCK (server);
826 * Sets a function to be used for handling new connections. The given
827 * function is passed each new connection as the connection is
828 * created. If the new connection function increments the connection's
829 * reference count, the connection will stay alive. Otherwise, the
830 * connection will be unreferenced and closed.
832 * @param server the server.
833 * @param function a function to handle new connections.
834 * @param data data to pass to the new connection handler.
835 * @param free_data_function function to free the data.
838 dbus_server_set_new_connection_function (DBusServer *server,
839 DBusNewConnectionFunction function,
841 DBusFreeFunction free_data_function)
843 DBusFreeFunction old_free_function;
846 _dbus_return_if_fail (server != NULL);
848 SERVER_LOCK (server);
849 old_free_function = server->new_connection_free_data_function;
850 old_data = server->new_connection_data;
852 server->new_connection_function = function;
853 server->new_connection_data = data;
854 server->new_connection_free_data_function = free_data_function;
855 SERVER_UNLOCK (server);
857 if (old_free_function != NULL)
858 (* old_free_function) (old_data);
862 * Sets the watch functions for the connection. These functions are
863 * responsible for making the application's main loop aware of file
864 * descriptors that need to be monitored for events.
866 * This function behaves exactly like dbus_connection_set_watch_functions();
867 * see the documentation for that routine.
869 * @param server the server.
870 * @param add_function function to begin monitoring a new descriptor.
871 * @param remove_function function to stop monitoring a descriptor.
872 * @param toggled_function function to notify when the watch is enabled/disabled
873 * @param data data to pass to add_function and remove_function.
874 * @param free_data_function function to be called to free the data.
875 * @returns #FALSE on failure (no memory)
878 dbus_server_set_watch_functions (DBusServer *server,
879 DBusAddWatchFunction add_function,
880 DBusRemoveWatchFunction remove_function,
881 DBusWatchToggledFunction toggled_function,
883 DBusFreeFunction free_data_function)
886 DBusWatchList *watches;
888 _dbus_return_val_if_fail (server != NULL, FALSE);
890 SERVER_LOCK (server);
891 watches = server->watches;
892 server->watches = NULL;
895 SERVER_UNLOCK (server);
896 result = _dbus_watch_list_set_functions (watches,
902 SERVER_LOCK (server);
906 _dbus_warn ("Re-entrant call to %s\n", _DBUS_FUNCTION_NAME);
909 server->watches = watches;
910 SERVER_UNLOCK (server);
916 * Sets the timeout functions for the connection. These functions are
917 * responsible for making the application's main loop aware of timeouts.
919 * This function behaves exactly like dbus_connection_set_timeout_functions();
920 * see the documentation for that routine.
922 * @param server the server.
923 * @param add_function function to add a timeout.
924 * @param remove_function function to remove a timeout.
925 * @param toggled_function function to notify when the timeout is enabled/disabled
926 * @param data data to pass to add_function and remove_function.
927 * @param free_data_function function to be called to free the data.
928 * @returns #FALSE on failure (no memory)
931 dbus_server_set_timeout_functions (DBusServer *server,
932 DBusAddTimeoutFunction add_function,
933 DBusRemoveTimeoutFunction remove_function,
934 DBusTimeoutToggledFunction toggled_function,
936 DBusFreeFunction free_data_function)
939 DBusTimeoutList *timeouts;
941 _dbus_return_val_if_fail (server != NULL, FALSE);
943 SERVER_LOCK (server);
944 timeouts = server->timeouts;
945 server->timeouts = NULL;
948 SERVER_UNLOCK (server);
949 result = _dbus_timeout_list_set_functions (timeouts,
955 SERVER_LOCK (server);
959 _dbus_warn ("Re-entrant call to %s\n", _DBUS_FUNCTION_NAME);
962 server->timeouts = timeouts;
963 SERVER_UNLOCK (server);
969 * Sets the authentication mechanisms that this server offers
970 * to clients, as a list of SASL mechanisms. This function
971 * only affects connections created *after* it is called.
972 * Pass #NULL instead of an array to use all available mechanisms.
974 * @param server the server
975 * @param mechanisms #NULL-terminated array of mechanisms
976 * @returns #FALSE if no memory
979 dbus_server_set_auth_mechanisms (DBusServer *server,
980 const char **mechanisms)
984 _dbus_return_val_if_fail (server != NULL, FALSE);
986 SERVER_LOCK (server);
988 if (mechanisms != NULL)
990 copy = _dbus_dup_string_array (mechanisms);
997 dbus_free_string_array (server->auth_mechanisms);
998 server->auth_mechanisms = copy;
1000 SERVER_UNLOCK (server);
1006 static DBusDataSlotAllocator slot_allocator;
1007 _DBUS_DEFINE_GLOBAL_LOCK (server_slots);
1010 * Allocates an integer ID to be used for storing application-specific
1011 * data on any DBusServer. The allocated ID may then be used
1012 * with dbus_server_set_data() and dbus_server_get_data().
1013 * The slot must be initialized with -1. If a nonnegative
1014 * slot is passed in, the refcount is incremented on that
1015 * slot, rather than creating a new slot.
1017 * The allocated slot is global, i.e. all DBusServer objects will have
1018 * a slot with the given integer ID reserved.
1020 * @param slot_p address of global variable storing the slot ID
1021 * @returns #FALSE on no memory
1024 dbus_server_allocate_data_slot (dbus_int32_t *slot_p)
1026 return _dbus_data_slot_allocator_alloc (&slot_allocator,
1027 (DBusMutex **)&_DBUS_LOCK_NAME (server_slots),
1032 * Deallocates a global ID for server data slots.
1033 * dbus_server_get_data() and dbus_server_set_data()
1034 * may no longer be used with this slot.
1035 * Existing data stored on existing DBusServer objects
1036 * will be freed when the server is finalized,
1037 * but may not be retrieved (and may only be replaced
1038 * if someone else reallocates the slot).
1040 * @param slot_p address of the slot to deallocate
1043 dbus_server_free_data_slot (dbus_int32_t *slot_p)
1045 _dbus_return_if_fail (*slot_p >= 0);
1047 _dbus_data_slot_allocator_free (&slot_allocator, slot_p);
1051 * Stores a pointer on a DBusServer, along
1052 * with an optional function to be used for freeing
1053 * the data when the data is set again, or when
1054 * the server is finalized. The slot number
1055 * must have been allocated with dbus_server_allocate_data_slot().
1057 * @param server the server
1058 * @param slot the slot number
1059 * @param data the data to store
1060 * @param free_data_func finalizer function for the data
1061 * @returns #TRUE if there was enough memory to store the data
1064 dbus_server_set_data (DBusServer *server,
1067 DBusFreeFunction free_data_func)
1069 DBusFreeFunction old_free_func;
1073 _dbus_return_val_if_fail (server != NULL, FALSE);
1075 SERVER_LOCK (server);
1077 retval = _dbus_data_slot_list_set (&slot_allocator,
1079 slot, data, free_data_func,
1080 &old_free_func, &old_data);
1083 SERVER_UNLOCK (server);
1087 /* Do the actual free outside the server lock */
1089 (* old_free_func) (old_data);
1096 * Retrieves data previously set with dbus_server_set_data().
1097 * The slot must still be allocated (must not have been freed).
1099 * @param server the server
1100 * @param slot the slot to get data from
1101 * @returns the data, or #NULL if not found
1104 dbus_server_get_data (DBusServer *server,
1109 _dbus_return_val_if_fail (server != NULL, NULL);
1111 SERVER_LOCK (server);
1113 res = _dbus_data_slot_list_get (&slot_allocator,
1117 SERVER_UNLOCK (server);
1124 #ifdef DBUS_BUILD_TESTS
1125 #include "dbus-test.h"
1128 _dbus_server_test (void)
1130 const char *valid_addresses[] = {
1132 "unix:path=./boogie",
1133 "tcp:host=localhost,port=1234",
1134 "tcp:host=localhost,port=1234;tcp:port=5678",
1135 "tcp:port=1234;unix:path=./boogie",
1141 for (i = 0; i < _DBUS_N_ELEMENTS (valid_addresses); i++)
1145 /* FIXME um, how are the two tests here different? */
1147 dbus_error_init (&error);
1148 server = dbus_server_listen (valid_addresses[i], &error);
1151 _dbus_warn ("server listen error: %s: %s\n", error.name, error.message);
1152 dbus_error_free (&error);
1153 _dbus_assert_not_reached ("Failed to listen for valid address.");
1156 dbus_server_disconnect (server);
1157 dbus_server_unref (server);
1159 /* Try disconnecting before unreffing */
1160 server = dbus_server_listen (valid_addresses[i], &error);
1163 _dbus_warn ("server listen error: %s: %s\n", error.name, error.message);
1164 dbus_error_free (&error);
1165 _dbus_assert_not_reached ("Failed to listen for valid address.");
1168 dbus_server_disconnect (server);
1169 dbus_server_unref (server);
1175 #endif /* DBUS_BUILD_TESTS */