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-string.h"
27 #ifdef DBUS_BUILD_TESTS
28 #include "dbus-server-debug-pipe.h"
30 #include "dbus-address.h"
31 #include "dbus-protocol.h"
34 * @defgroup DBusServer DBusServer
36 * @brief Server that listens for new connections.
38 * Types and functions related to DBusServer.
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 looked at 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
56 init_guid (DBusGUID *guid)
62 _dbus_get_current_time (&now, NULL);
64 guid->as_uint32s[0] = now;
66 ts_size = sizeof (guid->as_uint32s[0]);
67 p = ((char*)guid->as_bytes) + ts_size;
69 _dbus_generate_random_bytes_buffer (p,
70 sizeof (guid->as_bytes) - ts_size);
73 /* this is a little fragile since it assumes the address doesn't
74 * already have a guid, but it shouldn't
77 copy_address_with_guid_appended (const DBusString *address,
78 const DBusString *guid_hex)
83 if (!_dbus_string_init (&with_guid))
86 if (!_dbus_string_copy (address, 0, &with_guid,
87 _dbus_string_get_length (&with_guid)) ||
88 !_dbus_string_append (&with_guid, ",guid=") ||
89 !_dbus_string_copy (guid_hex, 0,
90 &with_guid, _dbus_string_get_length (&with_guid)))
92 _dbus_string_free (&with_guid);
97 _dbus_string_steal_data (&with_guid, &retval);
99 _dbus_string_free (&with_guid);
101 return retval; /* may be NULL if steal_data failed */
105 * Initializes the members of the DBusServer base class.
106 * Chained up to by subclass constructors.
108 * @param server the server.
109 * @param vtable the vtable for the subclass.
110 * @param address the server's address
111 * @returns #TRUE on success.
114 _dbus_server_init_base (DBusServer *server,
115 const DBusServerVTable *vtable,
116 const DBusString *address)
120 server->vtable = vtable;
121 server->refcount.value = 1;
123 server->address = NULL;
124 server->watches = NULL;
125 server->timeouts = NULL;
127 if (!_dbus_string_init (&server->guid_hex))
130 init_guid (&server->guid);
132 _dbus_string_init_const_len (&guid_raw, (signed char*) server->guid.as_bytes,
133 sizeof (server->guid.as_bytes));
134 if (!_dbus_string_hex_encode (&guid_raw, 0,
136 _dbus_string_get_length (&server->guid_hex)))
139 server->address = copy_address_with_guid_appended (address,
141 if (server->address == NULL)
144 server->mutex = _dbus_mutex_new ();
145 if (server->mutex == NULL)
148 server->watches = _dbus_watch_list_new ();
149 if (server->watches == NULL)
152 server->timeouts = _dbus_timeout_list_new ();
153 if (server->timeouts == NULL)
156 _dbus_data_slot_list_init (&server->slot_list);
158 _dbus_verbose ("Initialized server on address %s\n", server->address);
165 _dbus_mutex_free (server->mutex);
166 server->mutex = NULL;
170 _dbus_watch_list_free (server->watches);
171 server->watches = NULL;
173 if (server->timeouts)
175 _dbus_timeout_list_free (server->timeouts);
176 server->timeouts = NULL;
180 dbus_free (server->address);
181 server->address = NULL;
183 _dbus_string_free (&server->guid_hex);
189 * Finalizes the members of the DBusServer base class.
190 * Chained up to by subclass finalizers.
192 * @param server the server.
195 _dbus_server_finalize_base (DBusServer *server)
197 /* We don't have the lock, but nobody should be accessing
198 * concurrently since they don't have a ref
200 #ifndef DBUS_DISABLE_CHECKS
201 _dbus_assert (!server->have_server_lock);
203 _dbus_assert (server->disconnected);
205 /* calls out to application code... */
206 _dbus_data_slot_list_free (&server->slot_list);
208 dbus_server_set_new_connection_function (server, NULL, NULL, NULL);
210 _dbus_watch_list_free (server->watches);
211 _dbus_timeout_list_free (server->timeouts);
213 _dbus_mutex_free (server->mutex);
215 dbus_free (server->address);
217 dbus_free_string_array (server->auth_mechanisms);
219 _dbus_string_free (&server->guid_hex);
223 typedef dbus_bool_t (* DBusWatchAddFunction) (DBusWatchList *list,
225 typedef void (* DBusWatchRemoveFunction) (DBusWatchList *list,
227 typedef void (* DBusWatchToggleFunction) (DBusWatchList *list,
229 dbus_bool_t enabled);
232 protected_change_watch (DBusServer *server,
234 DBusWatchAddFunction add_function,
235 DBusWatchRemoveFunction remove_function,
236 DBusWatchToggleFunction toggle_function,
239 DBusWatchList *watches;
242 HAVE_LOCK_CHECK (server);
244 /* This isn't really safe or reasonable; a better pattern is the "do
245 * everything, then drop lock and call out" one; but it has to be
246 * propagated up through all callers
249 watches = server->watches;
252 server->watches = NULL;
253 _dbus_server_ref_unlocked (server);
254 SERVER_UNLOCK (server);
257 retval = (* add_function) (watches, watch);
258 else if (remove_function)
261 (* remove_function) (watches, watch);
266 (* toggle_function) (watches, watch, enabled);
269 SERVER_LOCK (server);
270 server->watches = watches;
271 _dbus_server_unref_unlocked (server);
280 * Adds a watch for this server, chaining out to application-provided
283 * @param server the server.
284 * @param watch the watch to add.
287 _dbus_server_add_watch (DBusServer *server,
290 HAVE_LOCK_CHECK (server);
291 return protected_change_watch (server, watch,
292 _dbus_watch_list_add_watch,
297 * Removes a watch previously added with _dbus_server_remove_watch().
299 * @param server the server.
300 * @param watch the watch to remove.
303 _dbus_server_remove_watch (DBusServer *server,
306 HAVE_LOCK_CHECK (server);
307 protected_change_watch (server, watch,
309 _dbus_watch_list_remove_watch,
314 * Toggles a watch and notifies app via server's
315 * DBusWatchToggledFunction if available. It's an error to call this
316 * function on a watch that was not previously added.
318 * @param server the server.
319 * @param watch the watch to toggle.
320 * @param enabled whether to enable or disable
323 _dbus_server_toggle_watch (DBusServer *server,
327 _dbus_assert (watch != NULL);
329 HAVE_LOCK_CHECK (server);
330 protected_change_watch (server, watch,
332 _dbus_watch_list_toggle_watch,
337 typedef dbus_bool_t (* DBusTimeoutAddFunction) (DBusTimeoutList *list,
338 DBusTimeout *timeout);
339 typedef void (* DBusTimeoutRemoveFunction) (DBusTimeoutList *list,
340 DBusTimeout *timeout);
341 typedef void (* DBusTimeoutToggleFunction) (DBusTimeoutList *list,
342 DBusTimeout *timeout,
343 dbus_bool_t enabled);
347 protected_change_timeout (DBusServer *server,
348 DBusTimeout *timeout,
349 DBusTimeoutAddFunction add_function,
350 DBusTimeoutRemoveFunction remove_function,
351 DBusTimeoutToggleFunction toggle_function,
354 DBusTimeoutList *timeouts;
357 HAVE_LOCK_CHECK (server);
359 /* This isn't really safe or reasonable; a better pattern is the "do everything, then
360 * drop lock and call out" one; but it has to be propagated up through all callers
363 timeouts = server->timeouts;
366 server->timeouts = NULL;
367 _dbus_server_ref_unlocked (server);
368 SERVER_UNLOCK (server);
371 retval = (* add_function) (timeouts, timeout);
372 else if (remove_function)
375 (* remove_function) (timeouts, timeout);
380 (* toggle_function) (timeouts, timeout, enabled);
383 SERVER_LOCK (server);
384 server->timeouts = timeouts;
385 _dbus_server_unref_unlocked (server);
394 * Adds a timeout for this server, chaining out to
395 * application-provided timeout handlers. The timeout should be
396 * repeatedly handled with dbus_timeout_handle() at its given interval
397 * until it is removed.
399 * @param server the server.
400 * @param timeout the timeout to add.
403 _dbus_server_add_timeout (DBusServer *server,
404 DBusTimeout *timeout)
406 return protected_change_timeout (server, timeout,
407 _dbus_timeout_list_add_timeout,
412 * Removes a timeout previously added with _dbus_server_add_timeout().
414 * @param server the server.
415 * @param timeout the timeout to remove.
418 _dbus_server_remove_timeout (DBusServer *server,
419 DBusTimeout *timeout)
421 protected_change_timeout (server, timeout,
423 _dbus_timeout_list_remove_timeout,
428 * Toggles a timeout and notifies app via server's
429 * DBusTimeoutToggledFunction if available. It's an error to call this
430 * function on a timeout that was not previously added.
432 * @param server the server.
433 * @param timeout the timeout to toggle.
434 * @param enabled whether to enable or disable
437 _dbus_server_toggle_timeout (DBusServer *server,
438 DBusTimeout *timeout,
441 protected_change_timeout (server, timeout,
443 _dbus_timeout_list_toggle_timeout,
451 * @addtogroup DBusServer
458 * @typedef DBusServer
460 * An opaque object representing a server that listens for
461 * connections from other applications. Each time a connection
462 * is made, a new DBusConnection is created and made available
463 * via an application-provided DBusNewConnectionFunction.
464 * The DBusNewConnectionFunction is provided with
465 * dbus_server_set_new_connection_function().
470 * Listens for new connections on the given address.
471 * Returns #NULL if listening fails for any reason.
472 * Otherwise returns a new #DBusServer.
473 * dbus_server_set_new_connection_function() and
474 * dbus_server_set_watch_functions() should be called
475 * immediately to render the server fully functional.
477 * @todo error messages on bad address could really be better.
478 * DBusResultCode is a bit limiting here.
480 * @param address the address of this server.
481 * @param error location to store rationale for failure.
482 * @returns a new DBusServer, or #NULL on failure.
486 dbus_server_listen (const char *address,
490 DBusAddressEntry **entries;
492 const char *address_problem_type;
493 const char *address_problem_field;
494 const char *address_problem_other;
496 _dbus_return_val_if_fail (address != NULL, NULL);
497 _dbus_return_val_if_error_is_set (error, NULL);
499 if (!dbus_parse_address (address, &entries, &len, error))
503 address_problem_type = NULL;
504 address_problem_field = NULL;
505 address_problem_other = NULL;
507 for (i = 0; i < len; i++)
511 method = dbus_address_entry_get_method (entries[i]);
513 if (strcmp (method, "unix") == 0)
515 const char *path = dbus_address_entry_get_value (entries[i], "path");
516 const char *tmpdir = dbus_address_entry_get_value (entries[i], "tmpdir");
517 const char *abstract = dbus_address_entry_get_value (entries[i], "abstract");
519 if (path == NULL && tmpdir == NULL && abstract == NULL)
521 address_problem_type = "unix";
522 address_problem_field = "path or tmpdir or abstract";
526 if ((path && tmpdir) ||
527 (path && abstract) ||
528 (tmpdir && abstract))
530 address_problem_other = "cannot specify two of \"path\" and \"tmpdir\" and \"abstract\" at the same time";
536 DBusString full_path;
539 if (!_dbus_string_init (&full_path))
541 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
545 if (!_dbus_string_init (&filename))
547 _dbus_string_free (&full_path);
548 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
552 if (!_dbus_string_append (&filename,
554 !_dbus_generate_random_ascii (&filename, 10) ||
555 !_dbus_string_append (&full_path, tmpdir) ||
556 !_dbus_concat_dir_and_file (&full_path, &filename))
558 _dbus_string_free (&full_path);
559 _dbus_string_free (&filename);
560 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
564 /* FIXME - we will unconditionally unlink() the path if
565 * we don't support abstract namespace. unlink() does
566 * not follow symlinks, but would like independent
567 * confirmation this is safe enough. See also
568 * _dbus_listen_unix_socket() and comments therein.
571 /* Always use abstract namespace if possible with tmpdir */
574 _dbus_server_new_for_domain_socket (_dbus_string_get_const_data (&full_path),
575 #ifdef HAVE_ABSTRACT_SOCKETS
582 _dbus_string_free (&full_path);
583 _dbus_string_free (&filename);
588 server = _dbus_server_new_for_domain_socket (path, FALSE, error);
590 server = _dbus_server_new_for_domain_socket (abstract, TRUE, error);
593 else if (strcmp (method, "tcp") == 0)
595 const char *host = dbus_address_entry_get_value (entries[i], "host");
596 const char *port = dbus_address_entry_get_value (entries[i], "port");
603 address_problem_type = "tcp";
604 address_problem_field = "port";
608 _dbus_string_init_const (&str, port);
609 sresult = _dbus_string_parse_int (&str, 0, &lport, NULL);
610 _dbus_string_free (&str);
612 if (sresult == FALSE || lport <= 0 || lport > 65535)
614 address_problem_other = "Port is not an integer between 0 and 65535";
618 server = _dbus_server_new_for_tcp_socket (host, lport, error);
623 #ifdef DBUS_BUILD_TESTS
624 else if (strcmp (method, "debug-pipe") == 0)
626 const char *name = dbus_address_entry_get_value (entries[i], "name");
630 address_problem_type = "debug-pipe";
631 address_problem_field = "name";
635 server = _dbus_server_debug_pipe_new (name, error);
640 address_problem_other = "Unknown address type (examples of valid types are \"unix\" and \"tcp\")";
650 dbus_address_entries_free (entries);
654 dbus_address_entries_free (entries);
655 if (address_problem_type != NULL)
656 dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS,
657 "Server address of type %s was missing argument %s",
658 address_problem_type, address_problem_field);
660 dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS,
661 "Could not parse server address: %s",
662 address_problem_other);
668 * Increments the reference count of a DBusServer.
670 * @param server the server.
671 * @returns the server
674 dbus_server_ref (DBusServer *server)
676 _dbus_return_val_if_fail (server != NULL, NULL);
677 _dbus_return_val_if_fail (server->refcount.value > 0, NULL);
679 #ifdef DBUS_HAVE_ATOMIC_INT
680 _dbus_atomic_inc (&server->refcount);
682 SERVER_LOCK (server);
683 _dbus_assert (server->refcount.value > 0);
685 server->refcount.value += 1;
686 SERVER_UNLOCK (server);
693 * Decrements the reference count of a DBusServer. Finalizes the
694 * server if the reference count reaches zero.
696 * The server must be disconnected before the refcount reaches zero.
698 * @param server the server.
701 dbus_server_unref (DBusServer *server)
703 dbus_bool_t last_unref;
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 * Like dbus_server_ref() but does not acquire the lock (must already be held)
735 * @param server the server.
738 _dbus_server_ref_unlocked (DBusServer *server)
740 _dbus_assert (server != NULL);
741 _dbus_assert (server->refcount.value > 0);
743 HAVE_LOCK_CHECK (server);
745 #ifdef DBUS_HAVE_ATOMIC_INT
746 _dbus_atomic_inc (&server->refcount);
748 _dbus_assert (server->refcount.value > 0);
750 server->refcount.value += 1;
755 * Like dbus_server_unref() but does not acquire the lock (must already be held)
757 * @param server the server.
760 _dbus_server_unref_unlocked (DBusServer *server)
762 dbus_bool_t last_unref;
764 _dbus_assert (server != NULL);
765 _dbus_assert (server->refcount.value > 0);
767 HAVE_LOCK_CHECK (server);
769 #ifdef DBUS_HAVE_ATOMIC_INT
770 last_unref = (_dbus_atomic_dec (&server->refcount) == 1);
772 _dbus_assert (server->refcount.value > 0);
774 server->refcount.value -= 1;
775 last_unref = (server->refcount.value == 0);
780 _dbus_assert (server->disconnected);
782 SERVER_UNLOCK (server);
784 _dbus_assert (server->vtable->finalize != NULL);
786 (* server->vtable->finalize) (server);
791 * Releases the server's address and stops listening for
792 * new clients. If called more than once, only the first
793 * call has an effect. Does not modify the server's
796 * @param server the server.
799 dbus_server_disconnect (DBusServer *server)
801 _dbus_return_if_fail (server != NULL);
802 _dbus_return_if_fail (server->refcount.value > 0);
804 SERVER_LOCK (server);
805 _dbus_server_ref_unlocked (server);
807 _dbus_assert (server->vtable->disconnect != NULL);
809 if (!server->disconnected)
811 /* this has to be first so recursive calls to disconnect don't happen */
812 server->disconnected = TRUE;
814 (* server->vtable->disconnect) (server);
817 SERVER_UNLOCK (server);
818 dbus_server_unref (server);
822 * Returns #TRUE if the server is still listening for new connections.
824 * @param server the server.
827 dbus_server_get_is_connected (DBusServer *server)
831 _dbus_return_val_if_fail (server != NULL, FALSE);
833 SERVER_LOCK (server);
834 retval = !server->disconnected;
835 SERVER_UNLOCK (server);
841 * Returns the address of the server, as a newly-allocated
842 * string which must be freed by the caller.
844 * @param server the server
845 * @returns the address or #NULL if no memory
848 dbus_server_get_address (DBusServer *server)
852 _dbus_return_val_if_fail (server != NULL, NULL);
854 SERVER_LOCK (server);
855 retval = _dbus_strdup (server->address);
856 SERVER_UNLOCK (server);
862 * Sets a function to be used for handling new connections. The given
863 * function is passed each new connection as the connection is
864 * created. If the new connection function increments the connection's
865 * reference count, the connection will stay alive. Otherwise, the
866 * connection will be unreferenced and closed.
868 * @param server the server.
869 * @param function a function to handle new connections.
870 * @param data data to pass to the new connection handler.
871 * @param free_data_function function to free the data.
874 dbus_server_set_new_connection_function (DBusServer *server,
875 DBusNewConnectionFunction function,
877 DBusFreeFunction free_data_function)
879 DBusFreeFunction old_free_function;
882 _dbus_return_if_fail (server != NULL);
884 SERVER_LOCK (server);
885 old_free_function = server->new_connection_free_data_function;
886 old_data = server->new_connection_data;
888 server->new_connection_function = function;
889 server->new_connection_data = data;
890 server->new_connection_free_data_function = free_data_function;
891 SERVER_UNLOCK (server);
893 if (old_free_function != NULL)
894 (* old_free_function) (old_data);
898 * Sets the watch functions for the connection. These functions are
899 * responsible for making the application's main loop aware of file
900 * descriptors that need to be monitored for events.
902 * This function behaves exactly like dbus_connection_set_watch_functions();
903 * see the documentation for that routine.
905 * @param server the server.
906 * @param add_function function to begin monitoring a new descriptor.
907 * @param remove_function function to stop monitoring a descriptor.
908 * @param toggled_function function to notify when the watch is enabled/disabled
909 * @param data data to pass to add_function and remove_function.
910 * @param free_data_function function to be called to free the data.
911 * @returns #FALSE on failure (no memory)
914 dbus_server_set_watch_functions (DBusServer *server,
915 DBusAddWatchFunction add_function,
916 DBusRemoveWatchFunction remove_function,
917 DBusWatchToggledFunction toggled_function,
919 DBusFreeFunction free_data_function)
922 DBusWatchList *watches;
924 _dbus_return_val_if_fail (server != NULL, FALSE);
926 SERVER_LOCK (server);
927 watches = server->watches;
928 server->watches = NULL;
931 SERVER_UNLOCK (server);
932 result = _dbus_watch_list_set_functions (watches,
938 SERVER_LOCK (server);
942 _dbus_warn ("Re-entrant call to %s\n", _DBUS_FUNCTION_NAME);
945 server->watches = watches;
946 SERVER_UNLOCK (server);
952 * Sets the timeout functions for the connection. These functions are
953 * responsible for making the application's main loop aware of timeouts.
955 * This function behaves exactly like dbus_connection_set_timeout_functions();
956 * see the documentation for that routine.
958 * @param server the server.
959 * @param add_function function to add a timeout.
960 * @param remove_function function to remove a timeout.
961 * @param toggled_function function to notify when the timeout is enabled/disabled
962 * @param data data to pass to add_function and remove_function.
963 * @param free_data_function function to be called to free the data.
964 * @returns #FALSE on failure (no memory)
967 dbus_server_set_timeout_functions (DBusServer *server,
968 DBusAddTimeoutFunction add_function,
969 DBusRemoveTimeoutFunction remove_function,
970 DBusTimeoutToggledFunction toggled_function,
972 DBusFreeFunction free_data_function)
975 DBusTimeoutList *timeouts;
977 _dbus_return_val_if_fail (server != NULL, FALSE);
979 SERVER_LOCK (server);
980 timeouts = server->timeouts;
981 server->timeouts = NULL;
984 SERVER_UNLOCK (server);
985 result = _dbus_timeout_list_set_functions (timeouts,
991 SERVER_LOCK (server);
995 _dbus_warn ("Re-entrant call to %s\n", _DBUS_FUNCTION_NAME);
998 server->timeouts = timeouts;
999 SERVER_UNLOCK (server);
1005 * Sets the authentication mechanisms that this server offers
1006 * to clients, as a list of SASL mechanisms. This function
1007 * only affects connections created *after* it is called.
1008 * Pass #NULL instead of an array to use all available mechanisms.
1010 * @param server the server
1011 * @param mechanisms #NULL-terminated array of mechanisms
1012 * @returns #FALSE if no memory
1015 dbus_server_set_auth_mechanisms (DBusServer *server,
1016 const char **mechanisms)
1020 _dbus_return_val_if_fail (server != NULL, FALSE);
1022 SERVER_LOCK (server);
1024 if (mechanisms != NULL)
1026 copy = _dbus_dup_string_array (mechanisms);
1033 dbus_free_string_array (server->auth_mechanisms);
1034 server->auth_mechanisms = copy;
1036 SERVER_UNLOCK (server);
1042 static DBusDataSlotAllocator slot_allocator;
1043 _DBUS_DEFINE_GLOBAL_LOCK (server_slots);
1046 * Allocates an integer ID to be used for storing application-specific
1047 * data on any DBusServer. The allocated ID may then be used
1048 * with dbus_server_set_data() and dbus_server_get_data().
1049 * The slot must be initialized with -1. If a nonnegative
1050 * slot is passed in, the refcount is incremented on that
1051 * slot, rather than creating a new slot.
1053 * The allocated slot is global, i.e. all DBusServer objects will have
1054 * a slot with the given integer ID reserved.
1056 * @param slot_p address of global variable storing the slot ID
1057 * @returns #FALSE on no memory
1060 dbus_server_allocate_data_slot (dbus_int32_t *slot_p)
1062 return _dbus_data_slot_allocator_alloc (&slot_allocator,
1063 _DBUS_LOCK_NAME (server_slots),
1068 * Deallocates a global ID for server data slots.
1069 * dbus_server_get_data() and dbus_server_set_data()
1070 * may no longer be used with this slot.
1071 * Existing data stored on existing DBusServer objects
1072 * will be freed when the server is finalized,
1073 * but may not be retrieved (and may only be replaced
1074 * if someone else reallocates the slot).
1076 * @param slot_p address of the slot to deallocate
1079 dbus_server_free_data_slot (dbus_int32_t *slot_p)
1081 _dbus_return_if_fail (*slot_p >= 0);
1083 _dbus_data_slot_allocator_free (&slot_allocator, slot_p);
1087 * Stores a pointer on a DBusServer, along
1088 * with an optional function to be used for freeing
1089 * the data when the data is set again, or when
1090 * the server is finalized. The slot number
1091 * must have been allocated with dbus_server_allocate_data_slot().
1093 * @param server the server
1094 * @param slot the slot number
1095 * @param data the data to store
1096 * @param free_data_func finalizer function for the data
1097 * @returns #TRUE if there was enough memory to store the data
1100 dbus_server_set_data (DBusServer *server,
1103 DBusFreeFunction free_data_func)
1105 DBusFreeFunction old_free_func;
1109 _dbus_return_val_if_fail (server != NULL, FALSE);
1111 SERVER_LOCK (server);
1113 retval = _dbus_data_slot_list_set (&slot_allocator,
1115 slot, data, free_data_func,
1116 &old_free_func, &old_data);
1119 SERVER_UNLOCK (server);
1123 /* Do the actual free outside the server lock */
1125 (* old_free_func) (old_data);
1132 * Retrieves data previously set with dbus_server_set_data().
1133 * The slot must still be allocated (must not have been freed).
1135 * @param server the server
1136 * @param slot the slot to get data from
1137 * @returns the data, or #NULL if not found
1140 dbus_server_get_data (DBusServer *server,
1145 _dbus_return_val_if_fail (server != NULL, NULL);
1147 SERVER_LOCK (server);
1149 res = _dbus_data_slot_list_get (&slot_allocator,
1153 SERVER_UNLOCK (server);
1160 #ifdef DBUS_BUILD_TESTS
1161 #include "dbus-test.h"
1164 _dbus_server_test (void)
1166 const char *valid_addresses[] = {
1168 "unix:path=./boogie",
1169 "tcp:host=localhost,port=1234",
1170 "tcp:host=localhost,port=1234;tcp:port=5678",
1171 "tcp:port=1234;unix:path=./boogie",
1177 for (i = 0; i < _DBUS_N_ELEMENTS (valid_addresses); i++)
1179 server = dbus_server_listen (valid_addresses[i], NULL);
1181 _dbus_assert_not_reached ("Failed to listen for valid address.");
1183 dbus_server_disconnect (server);
1184 dbus_server_unref (server);
1186 /* Try disconnecting before unreffing */
1187 server = dbus_server_listen (valid_addresses[i], NULL);
1189 _dbus_assert_not_reached ("Failed to listen for valid address.");
1191 dbus_server_disconnect (server);
1192 dbus_server_unref (server);
1198 #endif /* DBUS_BUILD_TESTS */