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_ENABLE_EMBEDDED_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 #ifndef _dbus_server_trace_ref
58 _dbus_server_trace_ref (DBusServer *server,
63 static int enabled = -1;
65 _dbus_trace_ref ("DBusServer", server, old_refcount, new_refcount, why,
66 "DBUS_SERVER_TRACE", &enabled);
70 /* this is a little fragile since it assumes the address doesn't
71 * already have a guid, but it shouldn't
74 copy_address_with_guid_appended (const DBusString *address,
75 const DBusString *guid_hex)
80 if (!_dbus_string_init (&with_guid))
83 if (!_dbus_string_copy (address, 0, &with_guid,
84 _dbus_string_get_length (&with_guid)) ||
85 !_dbus_string_append (&with_guid, ",guid=") ||
86 !_dbus_string_copy (guid_hex, 0,
87 &with_guid, _dbus_string_get_length (&with_guid)))
89 _dbus_string_free (&with_guid);
94 _dbus_string_steal_data (&with_guid, &retval);
96 _dbus_string_free (&with_guid);
98 return retval; /* may be NULL if steal_data failed */
102 * Initializes the members of the DBusServer base class.
103 * Chained up to by subclass constructors.
105 * @param server the server.
106 * @param vtable the vtable for the subclass.
107 * @param address the server's address
108 * @returns #TRUE on success.
111 _dbus_server_init_base (DBusServer *server,
112 const DBusServerVTable *vtable,
113 const DBusString *address)
115 server->vtable = vtable;
117 #ifdef DBUS_DISABLE_ASSERT
118 _dbus_atomic_inc (&server->refcount);
121 dbus_int32_t old_refcount = _dbus_atomic_inc (&server->refcount);
123 _dbus_assert (old_refcount == 0);
127 server->address = NULL;
128 server->watches = NULL;
129 server->timeouts = NULL;
130 server->published_address = FALSE;
132 if (!_dbus_string_init (&server->guid_hex))
135 _dbus_generate_uuid (&server->guid);
137 if (!_dbus_uuid_encode (&server->guid, &server->guid_hex))
140 server->address = copy_address_with_guid_appended (address,
142 if (server->address == NULL)
145 _dbus_rmutex_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_rmutex_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 dbus_server_init_mini (char* address)
191 server = dbus_new0(struct DBusServer, 1);
195 memset(server, 0, sizeof(struct DBusServer));
196 _dbus_rmutex_new_at_location (&server->mutex);
197 if (server->mutex == NULL)
199 server->address = address;
210 * Finalizes the members of the DBusServer base class.
211 * Chained up to by subclass finalizers.
213 * @param server the server.
216 _dbus_server_finalize_base (DBusServer *server)
218 /* We don't have the lock, but nobody should be accessing
219 * concurrently since they don't have a ref
221 #ifndef DBUS_DISABLE_CHECKS
222 _dbus_assert (!server->have_server_lock);
224 _dbus_assert (server->disconnected);
226 /* calls out to application code... */
227 _dbus_data_slot_list_free (&server->slot_list);
229 dbus_server_set_new_connection_function (server, NULL, NULL, NULL);
231 _dbus_watch_list_free (server->watches);
232 _dbus_timeout_list_free (server->timeouts);
234 _dbus_rmutex_free_at_location (&server->mutex);
236 dbus_free (server->address);
238 dbus_free_string_array (server->auth_mechanisms);
240 _dbus_string_free (&server->guid_hex);
244 /** Function to be called in protected_change_watch() with refcount held */
245 typedef dbus_bool_t (* DBusWatchAddFunction) (DBusWatchList *list,
247 /** Function to be called in protected_change_watch() with refcount held */
248 typedef void (* DBusWatchRemoveFunction) (DBusWatchList *list,
250 /** Function to be called in protected_change_watch() with refcount held */
251 typedef void (* DBusWatchToggleFunction) (DBusWatchList *list,
253 dbus_bool_t enabled);
256 protected_change_watch (DBusServer *server,
258 DBusWatchAddFunction add_function,
259 DBusWatchRemoveFunction remove_function,
260 DBusWatchToggleFunction toggle_function,
263 DBusWatchList *watches;
266 HAVE_LOCK_CHECK (server);
268 /* This isn't really safe or reasonable; a better pattern is the "do
269 * everything, then drop lock and call out" one; but it has to be
270 * propagated up through all callers
273 watches = server->watches;
276 server->watches = NULL;
277 _dbus_server_ref_unlocked (server);
278 SERVER_UNLOCK (server);
281 retval = (* add_function) (watches, watch);
282 else if (remove_function)
285 (* remove_function) (watches, watch);
290 (* toggle_function) (watches, watch, enabled);
293 SERVER_LOCK (server);
294 server->watches = watches;
295 _dbus_server_unref_unlocked (server);
304 * Adds a watch for this server, chaining out to application-provided
307 * @param server the server.
308 * @param watch the watch to add.
311 _dbus_server_add_watch (DBusServer *server,
314 HAVE_LOCK_CHECK (server);
315 return protected_change_watch (server, watch,
316 _dbus_watch_list_add_watch,
321 * Removes a watch previously added with _dbus_server_remove_watch().
323 * @param server the server.
324 * @param watch the watch to remove.
327 _dbus_server_remove_watch (DBusServer *server,
330 HAVE_LOCK_CHECK (server);
331 protected_change_watch (server, watch,
333 _dbus_watch_list_remove_watch,
338 * Toggles a watch and notifies app via server's
339 * DBusWatchToggledFunction if available. It's an error to call this
340 * function on a watch that was not previously added.
342 * @param server the server.
343 * @param watch the watch to toggle.
344 * @param enabled whether to enable or disable
347 _dbus_server_toggle_watch (DBusServer *server,
351 _dbus_assert (watch != NULL);
353 HAVE_LOCK_CHECK (server);
354 protected_change_watch (server, watch,
356 _dbus_watch_list_toggle_watch,
360 /** Function to be called in protected_change_timeout() with refcount held */
361 typedef dbus_bool_t (* DBusTimeoutAddFunction) (DBusTimeoutList *list,
362 DBusTimeout *timeout);
363 /** Function to be called in protected_change_timeout() with refcount held */
364 typedef void (* DBusTimeoutRemoveFunction) (DBusTimeoutList *list,
365 DBusTimeout *timeout);
366 /** Function to be called in protected_change_timeout() with refcount held */
367 typedef void (* DBusTimeoutToggleFunction) (DBusTimeoutList *list,
368 DBusTimeout *timeout,
369 dbus_bool_t enabled);
373 protected_change_timeout (DBusServer *server,
374 DBusTimeout *timeout,
375 DBusTimeoutAddFunction add_function,
376 DBusTimeoutRemoveFunction remove_function,
377 DBusTimeoutToggleFunction toggle_function,
380 DBusTimeoutList *timeouts;
383 HAVE_LOCK_CHECK (server);
385 /* This isn't really safe or reasonable; a better pattern is the "do everything, then
386 * drop lock and call out" one; but it has to be propagated up through all callers
389 timeouts = server->timeouts;
392 server->timeouts = NULL;
393 _dbus_server_ref_unlocked (server);
394 SERVER_UNLOCK (server);
397 retval = (* add_function) (timeouts, timeout);
398 else if (remove_function)
401 (* remove_function) (timeouts, timeout);
406 (* toggle_function) (timeouts, timeout, enabled);
409 SERVER_LOCK (server);
410 server->timeouts = timeouts;
411 _dbus_server_unref_unlocked (server);
420 * Adds a timeout for this server, chaining out to
421 * application-provided timeout handlers. The timeout should be
422 * repeatedly handled with dbus_timeout_handle() at its given interval
423 * until it is removed.
425 * @param server the server.
426 * @param timeout the timeout to add.
429 _dbus_server_add_timeout (DBusServer *server,
430 DBusTimeout *timeout)
432 return protected_change_timeout (server, timeout,
433 _dbus_timeout_list_add_timeout,
438 * Removes a timeout previously added with _dbus_server_add_timeout().
440 * @param server the server.
441 * @param timeout the timeout to remove.
444 _dbus_server_remove_timeout (DBusServer *server,
445 DBusTimeout *timeout)
447 protected_change_timeout (server, timeout,
449 _dbus_timeout_list_remove_timeout,
454 * Toggles a timeout and notifies app via server's
455 * DBusTimeoutToggledFunction if available. It's an error to call this
456 * function on a timeout that was not previously added.
458 * @param server the server.
459 * @param timeout the timeout to toggle.
460 * @param enabled whether to enable or disable
463 _dbus_server_toggle_timeout (DBusServer *server,
464 DBusTimeout *timeout,
467 protected_change_timeout (server, timeout,
469 _dbus_timeout_list_toggle_timeout,
475 * Like dbus_server_ref() but does not acquire the lock (must already be held)
477 * @param server the server.
480 _dbus_server_ref_unlocked (DBusServer *server)
482 dbus_int32_t old_refcount;
484 _dbus_assert (server != NULL);
485 HAVE_LOCK_CHECK (server);
487 old_refcount = _dbus_atomic_inc (&server->refcount);
488 _dbus_assert (old_refcount > 0);
489 _dbus_server_trace_ref (server, old_refcount, old_refcount + 1,
494 * Like dbus_server_unref() but does not acquire the lock (must already be held)
496 * @param server the server.
499 _dbus_server_unref_unlocked (DBusServer *server)
501 dbus_int32_t old_refcount;
503 /* Keep this in sync with dbus_server_unref */
505 _dbus_assert (server != NULL);
507 HAVE_LOCK_CHECK (server);
509 old_refcount = _dbus_atomic_dec (&server->refcount);
510 _dbus_assert (old_refcount > 0);
512 _dbus_server_trace_ref (server, old_refcount, old_refcount - 1,
515 if (old_refcount == 1)
517 _dbus_assert (server->disconnected);
519 SERVER_UNLOCK (server);
521 _dbus_assert (server->vtable->finalize != NULL);
523 (* server->vtable->finalize) (server);
530 * @addtogroup DBusServer
537 * @typedef DBusServer
539 * An opaque object representing a server that listens for
540 * connections from other applications. Each time a connection
541 * is made, a new DBusConnection is created and made available
542 * via an application-provided DBusNewConnectionFunction.
543 * The DBusNewConnectionFunction is provided with
544 * dbus_server_set_new_connection_function().
548 static const struct {
549 DBusServerListenResult (* func) (DBusAddressEntry *entry,
550 DBusServer **server_p,
553 { _dbus_server_listen_socket }
554 , { _dbus_server_listen_platform_specific }
555 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
556 , { _dbus_server_listen_debug_pipe }
561 * Listens for new connections on the given address. If there are
562 * multiple semicolon-separated address entries in the address, tries
563 * each one and listens on the first one that works.
565 * Returns #NULL and sets error if listening fails for any reason.
566 * Otherwise returns a new #DBusServer.
567 * dbus_server_set_new_connection_function(),
568 * dbus_server_set_watch_functions(), and
569 * dbus_server_set_timeout_functions() should be called immediately to
570 * render the server fully functional.
572 * To free the server, applications must call first
573 * dbus_server_disconnect() and then dbus_server_unref().
575 * @param address the address of this server.
576 * @param error location to store reason for failure.
577 * @returns a new #DBusServer, or #NULL on failure.
581 dbus_server_listen (const char *address,
585 DBusAddressEntry **entries;
587 DBusError first_connect_error = DBUS_ERROR_INIT;
588 dbus_bool_t handled_once;
590 _dbus_return_val_if_fail (address != NULL, NULL);
591 _dbus_return_val_if_error_is_set (error, NULL);
593 if (!dbus_parse_address (address, &entries, &len, error))
597 handled_once = FALSE;
599 for (i = 0; i < len; i++)
603 for (j = 0; j < (int) _DBUS_N_ELEMENTS (listen_funcs); ++j)
605 DBusServerListenResult result;
606 DBusError tmp_error = DBUS_ERROR_INIT;
608 result = (* listen_funcs[j].func) (entries[i],
612 if (result == DBUS_SERVER_LISTEN_OK)
614 _dbus_assert (server != NULL);
615 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
619 else if (result == DBUS_SERVER_LISTEN_ADDRESS_ALREADY_USED)
621 _dbus_assert (server == NULL);
622 dbus_set_error (error,
623 DBUS_ERROR_ADDRESS_IN_USE,
624 "Address '%s' already used",
625 dbus_address_entry_get_method (entries[0]));
629 else if (result == DBUS_SERVER_LISTEN_BAD_ADDRESS)
631 _dbus_assert (server == NULL);
632 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
633 dbus_move_error (&tmp_error, error);
637 else if (result == DBUS_SERVER_LISTEN_NOT_HANDLED)
639 _dbus_assert (server == NULL);
640 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
642 /* keep trying addresses */
644 else if (result == DBUS_SERVER_LISTEN_DID_NOT_CONNECT)
646 _dbus_assert (server == NULL);
647 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
648 if (!dbus_error_is_set (&first_connect_error))
649 dbus_move_error (&tmp_error, &first_connect_error);
651 dbus_error_free (&tmp_error);
655 /* keep trying addresses */
659 _dbus_assert (server == NULL);
660 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
667 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
669 dbus_set_error (error,
670 DBUS_ERROR_BAD_ADDRESS,
671 "Unknown address type '%s'",
672 dbus_address_entry_get_method (entries[0]));
674 dbus_set_error (error,
675 DBUS_ERROR_BAD_ADDRESS,
676 "Empty address '%s'",
680 dbus_address_entries_free (entries);
684 _dbus_assert (error == NULL || dbus_error_is_set (&first_connect_error) ||
685 dbus_error_is_set (error));
687 if (error && dbus_error_is_set (error))
689 /* already set the error */
693 /* didn't set the error but either error should be
694 * NULL or first_connect_error should be set.
696 _dbus_assert (error == NULL || dbus_error_is_set (&first_connect_error));
697 dbus_move_error (&first_connect_error, error);
700 _DBUS_ASSERT_ERROR_IS_CLEAR (&first_connect_error); /* be sure we freed it */
701 _DBUS_ASSERT_ERROR_IS_SET (error);
707 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
713 * Increments the reference count of a DBusServer.
715 * @param server the server.
716 * @returns the server
719 dbus_server_ref (DBusServer *server)
721 dbus_int32_t old_refcount;
723 _dbus_return_val_if_fail (server != NULL, NULL);
725 old_refcount = _dbus_atomic_inc (&server->refcount);
727 #ifndef DBUS_DISABLE_CHECKS
728 if (_DBUS_UNLIKELY (old_refcount <= 0))
730 _dbus_atomic_dec (&server->refcount);
731 _dbus_warn_check_failed (_dbus_return_if_fail_warning_format,
732 _DBUS_FUNCTION_NAME, "old_refcount > 0",
738 _dbus_server_trace_ref (server, old_refcount, old_refcount + 1, "ref");
744 * Decrements the reference count of a DBusServer. Finalizes the
745 * server if the reference count reaches zero.
747 * The server must be disconnected before the refcount reaches zero.
749 * @param server the server.
752 dbus_server_unref (DBusServer *server)
754 dbus_int32_t old_refcount;
756 /* keep this in sync with unref_unlocked */
758 _dbus_return_if_fail (server != NULL);
760 old_refcount = _dbus_atomic_dec (&server->refcount);
762 #ifndef DBUS_DISABLE_CHECKS
763 if (_DBUS_UNLIKELY (old_refcount <= 0))
765 /* undo side-effect first
766 * please do not try to simplify the code here by using
767 * _dbus_atomic_get(), why we don't use it is
768 * because it issues another atomic operation even though
769 * DBUS_DISABLE_CHECKS defined.
770 * Bug: https://bugs.freedesktop.org/show_bug.cgi?id=68303
772 _dbus_atomic_inc (&server->refcount);
773 _dbus_warn_check_failed (_dbus_return_if_fail_warning_format,
774 _DBUS_FUNCTION_NAME, "old_refcount > 0",
780 _dbus_server_trace_ref (server, old_refcount, old_refcount - 1, "unref");
782 if (old_refcount == 1)
785 _dbus_assert (server->disconnected);
787 _dbus_assert (server->vtable->finalize != NULL);
789 (* server->vtable->finalize) (server);
794 * Releases the server's address and stops listening for
795 * new clients. If called more than once, only the first
796 * call has an effect. Does not modify the server's
799 * @param server the server.
802 dbus_server_disconnect (DBusServer *server)
804 _dbus_return_if_fail (server != NULL);
806 dbus_server_ref (server);
807 SERVER_LOCK (server);
809 _dbus_assert (server->vtable->disconnect != NULL);
811 if (!server->disconnected)
813 /* this has to be first so recursive calls to disconnect don't happen */
814 server->disconnected = TRUE;
816 (* server->vtable->disconnect) (server);
819 SERVER_UNLOCK (server);
820 dbus_server_unref (server);
824 * Returns #TRUE if the server is still listening for new connections.
826 * @param server the server.
829 dbus_server_get_is_connected (DBusServer *server)
833 _dbus_return_val_if_fail (server != NULL, FALSE);
835 SERVER_LOCK (server);
836 retval = !server->disconnected;
837 SERVER_UNLOCK (server);
843 * Returns the address of the server, as a newly-allocated
844 * string which must be freed by the caller.
846 * @param server the server
847 * @returns the address or #NULL if no memory
850 dbus_server_get_address (DBusServer *server)
854 _dbus_return_val_if_fail (server != NULL, NULL);
856 SERVER_LOCK (server);
857 retval = _dbus_strdup (server->address);
858 SERVER_UNLOCK (server);
864 * Returns the unique ID of the server, as a newly-allocated
865 * string which must be freed by the caller. This ID is
866 * normally used by clients to tell when two #DBusConnection
867 * would be equivalent (because the server address passed
868 * to dbus_connection_open() will have the same guid in the
869 * two cases). dbus_connection_open() can re-use an existing
870 * connection with the same ID instead of opening a new
873 * This is an ID unique to each #DBusServer. Remember that
874 * a #DBusServer represents only one mode of connecting,
875 * so e.g. a bus daemon can listen on multiple addresses
876 * which will mean it has multiple #DBusServer each with
879 * The ID is not a UUID in the sense of RFC4122; the details
880 * are explained in the D-Bus specification.
882 * @param server the server
883 * @returns the id of the server or #NULL if no memory
886 dbus_server_get_id (DBusServer *server)
890 _dbus_return_val_if_fail (server != NULL, NULL);
892 SERVER_LOCK (server);
894 _dbus_string_copy_data (&server->guid_hex, &retval);
895 SERVER_UNLOCK (server);
901 * Sets a function to be used for handling new connections. The given
902 * function is passed each new connection as the connection is
903 * created. If the new connection function increments the connection's
904 * reference count, the connection will stay alive. Otherwise, the
905 * connection will be unreferenced and closed. The new connection
906 * function may also close the connection itself, which is considered
907 * good form if the connection is not wanted.
909 * The connection here is private in the sense of
910 * dbus_connection_open_private(), so if the new connection function
911 * keeps a reference it must arrange for the connection to be closed.
912 * i.e. libdbus does not own this connection once the new connection
913 * function takes a reference.
915 * @param server the server.
916 * @param function a function to handle new connections.
917 * @param data data to pass to the new connection handler.
918 * @param free_data_function function to free the data.
921 dbus_server_set_new_connection_function (DBusServer *server,
922 DBusNewConnectionFunction function,
924 DBusFreeFunction free_data_function)
926 DBusFreeFunction old_free_function;
929 _dbus_return_if_fail (server != NULL);
931 SERVER_LOCK (server);
932 old_free_function = server->new_connection_free_data_function;
933 old_data = server->new_connection_data;
935 server->new_connection_function = function;
936 server->new_connection_data = data;
937 server->new_connection_free_data_function = free_data_function;
938 SERVER_UNLOCK (server);
940 if (old_free_function != NULL)
941 (* old_free_function) (old_data);
945 * Sets the watch functions for the server. These functions are
946 * responsible for making the application's main loop aware of file
947 * descriptors that need to be monitored for events.
949 * This function behaves exactly like dbus_connection_set_watch_functions();
950 * see the documentation for that routine.
952 * @param server the server.
953 * @param add_function function to begin monitoring a new descriptor.
954 * @param remove_function function to stop monitoring a descriptor.
955 * @param toggled_function function to notify when the watch is enabled/disabled
956 * @param data data to pass to add_function and remove_function.
957 * @param free_data_function function to be called to free the data.
958 * @returns #FALSE on failure (no memory)
961 dbus_server_set_watch_functions (DBusServer *server,
962 DBusAddWatchFunction add_function,
963 DBusRemoveWatchFunction remove_function,
964 DBusWatchToggledFunction toggled_function,
966 DBusFreeFunction free_data_function)
969 DBusWatchList *watches;
971 _dbus_return_val_if_fail (server != NULL, FALSE);
973 SERVER_LOCK (server);
974 watches = server->watches;
975 server->watches = NULL;
978 SERVER_UNLOCK (server);
979 result = _dbus_watch_list_set_functions (watches,
985 SERVER_LOCK (server);
989 _dbus_warn_check_failed ("Re-entrant call to %s\n", _DBUS_FUNCTION_NAME);
992 server->watches = watches;
993 SERVER_UNLOCK (server);
999 * Sets the timeout functions for the server. These functions are
1000 * responsible for making the application's main loop aware of timeouts.
1002 * This function behaves exactly like dbus_connection_set_timeout_functions();
1003 * see the documentation for that routine.
1005 * @param server the server.
1006 * @param add_function function to add a timeout.
1007 * @param remove_function function to remove a timeout.
1008 * @param toggled_function function to notify when the timeout is enabled/disabled
1009 * @param data data to pass to add_function and remove_function.
1010 * @param free_data_function function to be called to free the data.
1011 * @returns #FALSE on failure (no memory)
1014 dbus_server_set_timeout_functions (DBusServer *server,
1015 DBusAddTimeoutFunction add_function,
1016 DBusRemoveTimeoutFunction remove_function,
1017 DBusTimeoutToggledFunction toggled_function,
1019 DBusFreeFunction free_data_function)
1022 DBusTimeoutList *timeouts;
1024 _dbus_return_val_if_fail (server != NULL, FALSE);
1026 SERVER_LOCK (server);
1027 timeouts = server->timeouts;
1028 server->timeouts = NULL;
1031 SERVER_UNLOCK (server);
1032 result = _dbus_timeout_list_set_functions (timeouts,
1037 free_data_function);
1038 SERVER_LOCK (server);
1042 _dbus_warn_check_failed ("Re-entrant call to %s\n", _DBUS_FUNCTION_NAME);
1045 server->timeouts = timeouts;
1046 SERVER_UNLOCK (server);
1052 * Sets the authentication mechanisms that this server offers to
1053 * clients, as a #NULL-terminated array of mechanism names. This
1054 * function only affects connections created <em>after</em> it is
1055 * called. Pass #NULL instead of an array to use all available
1056 * mechanisms (this is the default behavior).
1058 * The D-Bus specification describes some of the supported mechanisms.
1060 * @param server the server
1061 * @param mechanisms #NULL-terminated array of mechanisms
1062 * @returns #FALSE if no memory
1065 dbus_server_set_auth_mechanisms (DBusServer *server,
1066 const char **mechanisms)
1070 _dbus_return_val_if_fail (server != NULL, FALSE);
1072 SERVER_LOCK (server);
1074 if (mechanisms != NULL)
1076 copy = _dbus_dup_string_array (mechanisms);
1083 dbus_free_string_array (server->auth_mechanisms);
1084 server->auth_mechanisms = copy;
1086 SERVER_UNLOCK (server);
1091 static DBusDataSlotAllocator slot_allocator =
1092 _DBUS_DATA_SLOT_ALLOCATOR_INIT (_DBUS_LOCK_NAME (server_slots));
1095 * Allocates an integer ID to be used for storing application-specific
1096 * data on any DBusServer. The allocated ID may then be used
1097 * with dbus_server_set_data() and dbus_server_get_data().
1098 * The slot must be initialized with -1. If a nonnegative
1099 * slot is passed in, the refcount is incremented on that
1100 * slot, rather than creating a new slot.
1102 * The allocated slot is global, i.e. all DBusServer objects will have
1103 * a slot with the given integer ID reserved.
1105 * @param slot_p address of global variable storing the slot ID
1106 * @returns #FALSE on no memory
1109 dbus_server_allocate_data_slot (dbus_int32_t *slot_p)
1111 return _dbus_data_slot_allocator_alloc (&slot_allocator,
1116 * Deallocates a global ID for server data slots.
1117 * dbus_server_get_data() and dbus_server_set_data()
1118 * may no longer be used with this slot.
1119 * Existing data stored on existing DBusServer objects
1120 * will be freed when the server is finalized,
1121 * but may not be retrieved (and may only be replaced
1122 * if someone else reallocates the slot).
1124 * @param slot_p address of the slot to deallocate
1127 dbus_server_free_data_slot (dbus_int32_t *slot_p)
1129 _dbus_return_if_fail (*slot_p >= 0);
1131 _dbus_data_slot_allocator_free (&slot_allocator, slot_p);
1135 * Stores a pointer on a DBusServer, along
1136 * with an optional function to be used for freeing
1137 * the data when the data is set again, or when
1138 * the server is finalized. The slot number
1139 * must have been allocated with dbus_server_allocate_data_slot().
1141 * @param server the server
1142 * @param slot the slot number
1143 * @param data the data to store
1144 * @param free_data_func finalizer function for the data
1145 * @returns #TRUE if there was enough memory to store the data
1148 dbus_server_set_data (DBusServer *server,
1151 DBusFreeFunction free_data_func)
1153 DBusFreeFunction old_free_func;
1157 _dbus_return_val_if_fail (server != NULL, FALSE);
1159 SERVER_LOCK (server);
1161 retval = _dbus_data_slot_list_set (&slot_allocator,
1163 slot, data, free_data_func,
1164 &old_free_func, &old_data);
1167 SERVER_UNLOCK (server);
1171 /* Do the actual free outside the server lock */
1173 (* old_free_func) (old_data);
1180 * Retrieves data previously set with dbus_server_set_data().
1181 * The slot must still be allocated (must not have been freed).
1183 * @param server the server
1184 * @param slot the slot to get data from
1185 * @returns the data, or #NULL if not found
1188 dbus_server_get_data (DBusServer *server,
1193 _dbus_return_val_if_fail (server != NULL, NULL);
1195 SERVER_LOCK (server);
1197 res = _dbus_data_slot_list_get (&slot_allocator,
1201 SERVER_UNLOCK (server);
1208 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
1209 #include "dbus-test.h"
1213 _dbus_server_test (void)
1215 const char *valid_addresses[] = {
1217 "tcp:host=localhost,port=1234",
1218 "tcp:host=localhost,port=1234;tcp:port=5678",
1220 "unix:path=./boogie",
1221 "tcp:port=1234;unix:path=./boogie",
1228 for (i = 0; i < _DBUS_N_ELEMENTS (valid_addresses); i++)
1230 DBusError error = DBUS_ERROR_INIT;
1234 server = dbus_server_listen (valid_addresses[i], &error);
1237 _dbus_warn ("server listen error: %s: %s\n", error.name, error.message);
1238 dbus_error_free (&error);
1239 _dbus_assert_not_reached ("Failed to listen for valid address.");
1242 id = dbus_server_get_id (server);
1243 _dbus_assert (id != NULL);
1244 address = dbus_server_get_address (server);
1245 _dbus_assert (address != NULL);
1247 if (strstr (address, id) == NULL)
1249 _dbus_warn ("server id '%s' is not in the server address '%s'\n",
1251 _dbus_assert_not_reached ("bad server id or address");
1255 dbus_free (address);
1257 dbus_server_disconnect (server);
1258 dbus_server_unref (server);
1264 #endif /* DBUS_ENABLE_EMBEDDED_TESTS */