1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #include "dbus-server.h"
26 #include "dbus-server-unix.h"
27 #include "dbus-server-socket.h"
28 #include "dbus-string.h"
29 #ifdef DBUS_BUILD_TESTS
30 #include "dbus-server-debug-pipe.h"
32 #include "dbus-address.h"
33 #include "dbus-protocol.h"
36 * @defgroup DBusServer DBusServer
38 * @brief Server that listens for new connections.
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 tested much 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;
107 server->published_address = FALSE;
109 if (!_dbus_string_init (&server->guid_hex))
112 _dbus_generate_uuid (&server->guid);
114 if (!_dbus_uuid_encode (&server->guid, &server->guid_hex))
117 server->address = copy_address_with_guid_appended (address,
119 if (server->address == NULL)
122 _dbus_mutex_new_at_location (&server->mutex);
123 if (server->mutex == NULL)
126 server->watches = _dbus_watch_list_new ();
127 if (server->watches == NULL)
130 server->timeouts = _dbus_timeout_list_new ();
131 if (server->timeouts == NULL)
134 _dbus_data_slot_list_init (&server->slot_list);
136 _dbus_verbose ("Initialized server on address %s\n", server->address);
141 _dbus_mutex_free_at_location (&server->mutex);
142 server->mutex = NULL;
145 _dbus_watch_list_free (server->watches);
146 server->watches = NULL;
148 if (server->timeouts)
150 _dbus_timeout_list_free (server->timeouts);
151 server->timeouts = NULL;
155 dbus_free (server->address);
156 server->address = NULL;
158 _dbus_string_free (&server->guid_hex);
164 * Finalizes the members of the DBusServer base class.
165 * Chained up to by subclass finalizers.
167 * @param server the server.
170 _dbus_server_finalize_base (DBusServer *server)
172 /* We don't have the lock, but nobody should be accessing
173 * concurrently since they don't have a ref
175 #ifndef DBUS_DISABLE_CHECKS
176 _dbus_assert (!server->have_server_lock);
178 _dbus_assert (server->disconnected);
180 /* calls out to application code... */
181 _dbus_data_slot_list_free (&server->slot_list);
183 dbus_server_set_new_connection_function (server, NULL, NULL, NULL);
185 _dbus_watch_list_free (server->watches);
186 _dbus_timeout_list_free (server->timeouts);
188 _dbus_mutex_free_at_location (&server->mutex);
190 dbus_free (server->address);
192 dbus_free_string_array (server->auth_mechanisms);
194 _dbus_string_free (&server->guid_hex);
198 /** Function to be called in protected_change_watch() with refcount held */
199 typedef dbus_bool_t (* DBusWatchAddFunction) (DBusWatchList *list,
201 /** Function to be called in protected_change_watch() with refcount held */
202 typedef void (* DBusWatchRemoveFunction) (DBusWatchList *list,
204 /** Function to be called in protected_change_watch() with refcount held */
205 typedef void (* DBusWatchToggleFunction) (DBusWatchList *list,
207 dbus_bool_t enabled);
210 protected_change_watch (DBusServer *server,
212 DBusWatchAddFunction add_function,
213 DBusWatchRemoveFunction remove_function,
214 DBusWatchToggleFunction toggle_function,
217 DBusWatchList *watches;
220 HAVE_LOCK_CHECK (server);
222 /* This isn't really safe or reasonable; a better pattern is the "do
223 * everything, then drop lock and call out" one; but it has to be
224 * propagated up through all callers
227 watches = server->watches;
230 server->watches = NULL;
231 _dbus_server_ref_unlocked (server);
232 SERVER_UNLOCK (server);
235 retval = (* add_function) (watches, watch);
236 else if (remove_function)
239 (* remove_function) (watches, watch);
244 (* toggle_function) (watches, watch, enabled);
247 SERVER_LOCK (server);
248 server->watches = watches;
249 _dbus_server_unref_unlocked (server);
258 * Adds a watch for this server, chaining out to application-provided
261 * @param server the server.
262 * @param watch the watch to add.
265 _dbus_server_add_watch (DBusServer *server,
268 HAVE_LOCK_CHECK (server);
269 return protected_change_watch (server, watch,
270 _dbus_watch_list_add_watch,
275 * Removes a watch previously added with _dbus_server_remove_watch().
277 * @param server the server.
278 * @param watch the watch to remove.
281 _dbus_server_remove_watch (DBusServer *server,
284 HAVE_LOCK_CHECK (server);
285 protected_change_watch (server, watch,
287 _dbus_watch_list_remove_watch,
292 * Toggles a watch and notifies app via server's
293 * DBusWatchToggledFunction if available. It's an error to call this
294 * function on a watch that was not previously added.
296 * @param server the server.
297 * @param watch the watch to toggle.
298 * @param enabled whether to enable or disable
301 _dbus_server_toggle_watch (DBusServer *server,
305 _dbus_assert (watch != NULL);
307 HAVE_LOCK_CHECK (server);
308 protected_change_watch (server, watch,
310 _dbus_watch_list_toggle_watch,
314 /** Function to be called in protected_change_timeout() with refcount held */
315 typedef dbus_bool_t (* DBusTimeoutAddFunction) (DBusTimeoutList *list,
316 DBusTimeout *timeout);
317 /** Function to be called in protected_change_timeout() with refcount held */
318 typedef void (* DBusTimeoutRemoveFunction) (DBusTimeoutList *list,
319 DBusTimeout *timeout);
320 /** Function to be called in protected_change_timeout() with refcount held */
321 typedef void (* DBusTimeoutToggleFunction) (DBusTimeoutList *list,
322 DBusTimeout *timeout,
323 dbus_bool_t enabled);
327 protected_change_timeout (DBusServer *server,
328 DBusTimeout *timeout,
329 DBusTimeoutAddFunction add_function,
330 DBusTimeoutRemoveFunction remove_function,
331 DBusTimeoutToggleFunction toggle_function,
334 DBusTimeoutList *timeouts;
337 HAVE_LOCK_CHECK (server);
339 /* This isn't really safe or reasonable; a better pattern is the "do everything, then
340 * drop lock and call out" one; but it has to be propagated up through all callers
343 timeouts = server->timeouts;
346 server->timeouts = NULL;
347 _dbus_server_ref_unlocked (server);
348 SERVER_UNLOCK (server);
351 retval = (* add_function) (timeouts, timeout);
352 else if (remove_function)
355 (* remove_function) (timeouts, timeout);
360 (* toggle_function) (timeouts, timeout, enabled);
363 SERVER_LOCK (server);
364 server->timeouts = timeouts;
365 _dbus_server_unref_unlocked (server);
374 * Adds a timeout for this server, chaining out to
375 * application-provided timeout handlers. The timeout should be
376 * repeatedly handled with dbus_timeout_handle() at its given interval
377 * until it is removed.
379 * @param server the server.
380 * @param timeout the timeout to add.
383 _dbus_server_add_timeout (DBusServer *server,
384 DBusTimeout *timeout)
386 return protected_change_timeout (server, timeout,
387 _dbus_timeout_list_add_timeout,
392 * Removes a timeout previously added with _dbus_server_add_timeout().
394 * @param server the server.
395 * @param timeout the timeout to remove.
398 _dbus_server_remove_timeout (DBusServer *server,
399 DBusTimeout *timeout)
401 protected_change_timeout (server, timeout,
403 _dbus_timeout_list_remove_timeout,
408 * Toggles a timeout and notifies app via server's
409 * DBusTimeoutToggledFunction if available. It's an error to call this
410 * function on a timeout that was not previously added.
412 * @param server the server.
413 * @param timeout the timeout to toggle.
414 * @param enabled whether to enable or disable
417 _dbus_server_toggle_timeout (DBusServer *server,
418 DBusTimeout *timeout,
421 protected_change_timeout (server, timeout,
423 _dbus_timeout_list_toggle_timeout,
429 * Like dbus_server_ref() but does not acquire the lock (must already be held)
431 * @param server the server.
434 _dbus_server_ref_unlocked (DBusServer *server)
436 _dbus_assert (server != NULL);
437 _dbus_assert (server->refcount.value > 0);
439 HAVE_LOCK_CHECK (server);
441 #ifdef DBUS_HAVE_ATOMIC_INT
442 _dbus_atomic_inc (&server->refcount);
444 _dbus_assert (server->refcount.value > 0);
446 server->refcount.value += 1;
451 * Like dbus_server_unref() but does not acquire the lock (must already be held)
453 * @param server the server.
456 _dbus_server_unref_unlocked (DBusServer *server)
458 dbus_bool_t last_unref;
460 /* Keep this in sync with dbus_server_unref */
462 _dbus_assert (server != NULL);
463 _dbus_assert (server->refcount.value > 0);
465 HAVE_LOCK_CHECK (server);
467 #ifdef DBUS_HAVE_ATOMIC_INT
468 last_unref = (_dbus_atomic_dec (&server->refcount) == 1);
470 _dbus_assert (server->refcount.value > 0);
472 server->refcount.value -= 1;
473 last_unref = (server->refcount.value == 0);
478 _dbus_assert (server->disconnected);
480 SERVER_UNLOCK (server);
482 _dbus_assert (server->vtable->finalize != NULL);
484 (* server->vtable->finalize) (server);
491 * @addtogroup DBusServer
498 * @typedef DBusServer
500 * An opaque object representing a server that listens for
501 * connections from other applications. Each time a connection
502 * is made, a new DBusConnection is created and made available
503 * via an application-provided DBusNewConnectionFunction.
504 * The DBusNewConnectionFunction is provided with
505 * dbus_server_set_new_connection_function().
509 static const struct {
510 DBusServerListenResult (* func) (DBusAddressEntry *entry,
511 DBusServer **server_p,
514 { _dbus_server_listen_socket }
515 , { _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 = DBUS_ERROR_INIT;
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 handled_once = FALSE;
560 for (i = 0; i < len; i++)
564 for (j = 0; j < (int) _DBUS_N_ELEMENTS (listen_funcs); ++j)
566 DBusServerListenResult result;
567 DBusError tmp_error = DBUS_ERROR_INIT;
569 result = (* listen_funcs[j].func) (entries[i],
573 if (result == DBUS_SERVER_LISTEN_OK)
575 _dbus_assert (server != NULL);
576 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
580 else if (result == DBUS_SERVER_LISTEN_ADDRESS_ALREADY_USED)
582 _dbus_assert (server == NULL);
583 dbus_set_error (error,
584 DBUS_ERROR_ADDRESS_IN_USE,
585 "Address '%s' already used",
586 dbus_address_entry_get_method (entries[0]));
590 else if (result == DBUS_SERVER_LISTEN_BAD_ADDRESS)
592 _dbus_assert (server == NULL);
593 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
594 dbus_move_error (&tmp_error, error);
598 else if (result == DBUS_SERVER_LISTEN_NOT_HANDLED)
600 _dbus_assert (server == NULL);
601 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
603 /* keep trying addresses */
605 else if (result == DBUS_SERVER_LISTEN_DID_NOT_CONNECT)
607 _dbus_assert (server == NULL);
608 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
609 if (!dbus_error_is_set (&first_connect_error))
610 dbus_move_error (&tmp_error, &first_connect_error);
612 dbus_error_free (&tmp_error);
616 /* keep trying addresses */
620 _dbus_assert (server == NULL);
621 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
628 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
630 dbus_set_error (error,
631 DBUS_ERROR_BAD_ADDRESS,
632 "Unknown address type '%s'",
633 dbus_address_entry_get_method (entries[0]));
635 dbus_set_error (error,
636 DBUS_ERROR_BAD_ADDRESS,
637 "Empty address '%s'",
641 dbus_address_entries_free (entries);
645 _dbus_assert (error == NULL || dbus_error_is_set (&first_connect_error) ||
646 dbus_error_is_set (error));
648 if (error && dbus_error_is_set (error))
650 /* already set the error */
654 /* didn't set the error but either error should be
655 * NULL or first_connect_error should be set.
657 _dbus_assert (error == NULL || dbus_error_is_set (&first_connect_error));
658 dbus_move_error (&first_connect_error, error);
661 _DBUS_ASSERT_ERROR_IS_CLEAR (&first_connect_error); /* be sure we freed it */
662 _DBUS_ASSERT_ERROR_IS_SET (error);
668 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
674 * Increments the reference count of a DBusServer.
676 * @param server the server.
677 * @returns the server
680 dbus_server_ref (DBusServer *server)
682 _dbus_return_val_if_fail (server != NULL, NULL);
683 _dbus_return_val_if_fail (server->refcount.value > 0, NULL);
685 #ifdef DBUS_HAVE_ATOMIC_INT
686 _dbus_atomic_inc (&server->refcount);
688 SERVER_LOCK (server);
689 _dbus_assert (server->refcount.value > 0);
691 server->refcount.value += 1;
692 SERVER_UNLOCK (server);
699 * Decrements the reference count of a DBusServer. Finalizes the
700 * server if the reference count reaches zero.
702 * The server must be disconnected before the refcount reaches zero.
704 * @param server the server.
707 dbus_server_unref (DBusServer *server)
709 dbus_bool_t last_unref;
711 /* keep this in sync with unref_unlocked */
713 _dbus_return_if_fail (server != NULL);
714 _dbus_return_if_fail (server->refcount.value > 0);
716 #ifdef DBUS_HAVE_ATOMIC_INT
717 last_unref = (_dbus_atomic_dec (&server->refcount) == 1);
719 SERVER_LOCK (server);
721 _dbus_assert (server->refcount.value > 0);
723 server->refcount.value -= 1;
724 last_unref = (server->refcount.value == 0);
726 SERVER_UNLOCK (server);
732 _dbus_assert (server->disconnected);
734 _dbus_assert (server->vtable->finalize != NULL);
736 (* server->vtable->finalize) (server);
741 * Releases the server's address and stops listening for
742 * new clients. If called more than once, only the first
743 * call has an effect. Does not modify the server's
746 * @param server the server.
749 dbus_server_disconnect (DBusServer *server)
751 _dbus_return_if_fail (server != NULL);
752 _dbus_return_if_fail (server->refcount.value > 0);
754 SERVER_LOCK (server);
755 _dbus_server_ref_unlocked (server);
757 _dbus_assert (server->vtable->disconnect != NULL);
759 if (!server->disconnected)
761 /* this has to be first so recursive calls to disconnect don't happen */
762 server->disconnected = TRUE;
764 (* server->vtable->disconnect) (server);
767 SERVER_UNLOCK (server);
768 dbus_server_unref (server);
772 * Returns #TRUE if the server is still listening for new connections.
774 * @param server the server.
777 dbus_server_get_is_connected (DBusServer *server)
781 _dbus_return_val_if_fail (server != NULL, FALSE);
783 SERVER_LOCK (server);
784 retval = !server->disconnected;
785 SERVER_UNLOCK (server);
791 * Returns the address of the server, as a newly-allocated
792 * string which must be freed by the caller.
794 * @param server the server
795 * @returns the address or #NULL if no memory
798 dbus_server_get_address (DBusServer *server)
802 _dbus_return_val_if_fail (server != NULL, NULL);
804 SERVER_LOCK (server);
805 retval = _dbus_strdup (server->address);
806 SERVER_UNLOCK (server);
812 * Returns the unique ID of the server, as a newly-allocated
813 * string which must be freed by the caller. This ID is
814 * normally used by clients to tell when two #DBusConnection
815 * would be equivalent (because the server address passed
816 * to dbus_connection_open() will have the same guid in the
817 * two cases). dbus_connection_open() can re-use an existing
818 * connection with the same ID instead of opening a new
821 * This is an ID unique to each #DBusServer. Remember that
822 * a #DBusServer represents only one mode of connecting,
823 * so e.g. a bus daemon can listen on multiple addresses
824 * which will mean it has multiple #DBusServer each with
827 * The ID is not a UUID in the sense of RFC4122; the details
828 * are explained in the D-Bus specification.
830 * @param server the server
831 * @returns the id of the server or #NULL if no memory
834 dbus_server_get_id (DBusServer *server)
838 _dbus_return_val_if_fail (server != NULL, NULL);
840 SERVER_LOCK (server);
842 _dbus_string_copy_data (&server->guid_hex, &retval);
843 SERVER_UNLOCK (server);
849 * Sets a function to be used for handling new connections. The given
850 * function is passed each new connection as the connection is
851 * created. If the new connection function increments the connection's
852 * reference count, the connection will stay alive. Otherwise, the
853 * connection will be unreferenced and closed. The new connection
854 * function may also close the connection itself, which is considered
855 * good form if the connection is not wanted.
857 * The connection here is private in the sense of
858 * dbus_connection_open_private(), so if the new connection function
859 * keeps a reference it must arrange for the connection to be closed.
860 * i.e. libdbus does not own this connection once the new connection
861 * function takes a reference.
863 * @param server the server.
864 * @param function a function to handle new connections.
865 * @param data data to pass to the new connection handler.
866 * @param free_data_function function to free the data.
869 dbus_server_set_new_connection_function (DBusServer *server,
870 DBusNewConnectionFunction function,
872 DBusFreeFunction free_data_function)
874 DBusFreeFunction old_free_function;
877 _dbus_return_if_fail (server != NULL);
879 SERVER_LOCK (server);
880 old_free_function = server->new_connection_free_data_function;
881 old_data = server->new_connection_data;
883 server->new_connection_function = function;
884 server->new_connection_data = data;
885 server->new_connection_free_data_function = free_data_function;
886 SERVER_UNLOCK (server);
888 if (old_free_function != NULL)
889 (* old_free_function) (old_data);
893 * Sets the watch functions for the server. These functions are
894 * responsible for making the application's main loop aware of file
895 * descriptors that need to be monitored for events.
897 * This function behaves exactly like dbus_connection_set_watch_functions();
898 * see the documentation for that routine.
900 * @param server the server.
901 * @param add_function function to begin monitoring a new descriptor.
902 * @param remove_function function to stop monitoring a descriptor.
903 * @param toggled_function function to notify when the watch is enabled/disabled
904 * @param data data to pass to add_function and remove_function.
905 * @param free_data_function function to be called to free the data.
906 * @returns #FALSE on failure (no memory)
909 dbus_server_set_watch_functions (DBusServer *server,
910 DBusAddWatchFunction add_function,
911 DBusRemoveWatchFunction remove_function,
912 DBusWatchToggledFunction toggled_function,
914 DBusFreeFunction free_data_function)
917 DBusWatchList *watches;
919 _dbus_return_val_if_fail (server != NULL, FALSE);
921 SERVER_LOCK (server);
922 watches = server->watches;
923 server->watches = NULL;
926 SERVER_UNLOCK (server);
927 result = _dbus_watch_list_set_functions (watches,
933 SERVER_LOCK (server);
937 _dbus_warn_check_failed ("Re-entrant call to %s\n", _DBUS_FUNCTION_NAME);
940 server->watches = watches;
941 SERVER_UNLOCK (server);
947 * Sets the timeout functions for the server. These functions are
948 * responsible for making the application's main loop aware of timeouts.
950 * This function behaves exactly like dbus_connection_set_timeout_functions();
951 * see the documentation for that routine.
953 * @param server the server.
954 * @param add_function function to add a timeout.
955 * @param remove_function function to remove a timeout.
956 * @param toggled_function function to notify when the timeout is enabled/disabled
957 * @param data data to pass to add_function and remove_function.
958 * @param free_data_function function to be called to free the data.
959 * @returns #FALSE on failure (no memory)
962 dbus_server_set_timeout_functions (DBusServer *server,
963 DBusAddTimeoutFunction add_function,
964 DBusRemoveTimeoutFunction remove_function,
965 DBusTimeoutToggledFunction toggled_function,
967 DBusFreeFunction free_data_function)
970 DBusTimeoutList *timeouts;
972 _dbus_return_val_if_fail (server != NULL, FALSE);
974 SERVER_LOCK (server);
975 timeouts = server->timeouts;
976 server->timeouts = NULL;
979 SERVER_UNLOCK (server);
980 result = _dbus_timeout_list_set_functions (timeouts,
986 SERVER_LOCK (server);
990 _dbus_warn_check_failed ("Re-entrant call to %s\n", _DBUS_FUNCTION_NAME);
993 server->timeouts = timeouts;
994 SERVER_UNLOCK (server);
1000 * Sets the authentication mechanisms that this server offers to
1001 * clients, as a #NULL-terminated array of mechanism names. This
1002 * function only affects connections created <em>after</em> it is
1003 * called. Pass #NULL instead of an array to use all available
1004 * mechanisms (this is the default behavior).
1006 * The D-Bus specification describes some of the supported mechanisms.
1008 * @param server the server
1009 * @param mechanisms #NULL-terminated array of mechanisms
1010 * @returns #FALSE if no memory
1013 dbus_server_set_auth_mechanisms (DBusServer *server,
1014 const char **mechanisms)
1018 _dbus_return_val_if_fail (server != NULL, FALSE);
1020 SERVER_LOCK (server);
1022 if (mechanisms != NULL)
1024 copy = _dbus_dup_string_array (mechanisms);
1031 dbus_free_string_array (server->auth_mechanisms);
1032 server->auth_mechanisms = copy;
1034 SERVER_UNLOCK (server);
1040 static DBusDataSlotAllocator slot_allocator;
1041 _DBUS_DEFINE_GLOBAL_LOCK (server_slots);
1044 * Allocates an integer ID to be used for storing application-specific
1045 * data on any DBusServer. The allocated ID may then be used
1046 * with dbus_server_set_data() and dbus_server_get_data().
1047 * The slot must be initialized with -1. If a nonnegative
1048 * slot is passed in, the refcount is incremented on that
1049 * slot, rather than creating a new slot.
1051 * The allocated slot is global, i.e. all DBusServer objects will have
1052 * a slot with the given integer ID reserved.
1054 * @param slot_p address of global variable storing the slot ID
1055 * @returns #FALSE on no memory
1058 dbus_server_allocate_data_slot (dbus_int32_t *slot_p)
1060 return _dbus_data_slot_allocator_alloc (&slot_allocator,
1061 (DBusMutex **)&_DBUS_LOCK_NAME (server_slots),
1066 * Deallocates a global ID for server data slots.
1067 * dbus_server_get_data() and dbus_server_set_data()
1068 * may no longer be used with this slot.
1069 * Existing data stored on existing DBusServer objects
1070 * will be freed when the server is finalized,
1071 * but may not be retrieved (and may only be replaced
1072 * if someone else reallocates the slot).
1074 * @param slot_p address of the slot to deallocate
1077 dbus_server_free_data_slot (dbus_int32_t *slot_p)
1079 _dbus_return_if_fail (*slot_p >= 0);
1081 _dbus_data_slot_allocator_free (&slot_allocator, slot_p);
1085 * Stores a pointer on a DBusServer, along
1086 * with an optional function to be used for freeing
1087 * the data when the data is set again, or when
1088 * the server is finalized. The slot number
1089 * must have been allocated with dbus_server_allocate_data_slot().
1091 * @param server the server
1092 * @param slot the slot number
1093 * @param data the data to store
1094 * @param free_data_func finalizer function for the data
1095 * @returns #TRUE if there was enough memory to store the data
1098 dbus_server_set_data (DBusServer *server,
1101 DBusFreeFunction free_data_func)
1103 DBusFreeFunction old_free_func;
1107 _dbus_return_val_if_fail (server != NULL, FALSE);
1109 SERVER_LOCK (server);
1111 retval = _dbus_data_slot_list_set (&slot_allocator,
1113 slot, data, free_data_func,
1114 &old_free_func, &old_data);
1117 SERVER_UNLOCK (server);
1121 /* Do the actual free outside the server lock */
1123 (* old_free_func) (old_data);
1130 * Retrieves data previously set with dbus_server_set_data().
1131 * The slot must still be allocated (must not have been freed).
1133 * @param server the server
1134 * @param slot the slot to get data from
1135 * @returns the data, or #NULL if not found
1138 dbus_server_get_data (DBusServer *server,
1143 _dbus_return_val_if_fail (server != NULL, NULL);
1145 SERVER_LOCK (server);
1147 res = _dbus_data_slot_list_get (&slot_allocator,
1151 SERVER_UNLOCK (server);
1158 #ifdef DBUS_BUILD_TESTS
1159 #include "dbus-test.h"
1163 _dbus_server_test (void)
1165 const char *valid_addresses[] = {
1167 "tcp:host=localhost,port=1234",
1168 "tcp:host=localhost,port=1234;tcp:port=5678",
1170 "unix:path=./boogie",
1171 "tcp:port=1234;unix:path=./boogie",
1178 for (i = 0; i < _DBUS_N_ELEMENTS (valid_addresses); i++)
1180 DBusError error = DBUS_ERROR_INIT;
1184 server = dbus_server_listen (valid_addresses[i], &error);
1187 _dbus_warn ("server listen error: %s: %s\n", error.name, error.message);
1188 dbus_error_free (&error);
1189 _dbus_assert_not_reached ("Failed to listen for valid address.");
1192 id = dbus_server_get_id (server);
1193 _dbus_assert (id != NULL);
1194 address = dbus_server_get_address (server);
1195 _dbus_assert (address != NULL);
1197 if (strstr (address, id) == NULL)
1199 _dbus_warn ("server id '%s' is not in the server address '%s'\n",
1201 _dbus_assert_not_reached ("bad server id or address");
1205 dbus_free (address);
1207 dbus_server_disconnect (server);
1208 dbus_server_unref (server);
1214 #endif /* DBUS_BUILD_TESTS */