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 _dbus_mutex_new_at_location (&server->mutex);
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);
163 _dbus_mutex_free_at_location (&server->mutex);
164 server->mutex = NULL;
167 _dbus_watch_list_free (server->watches);
168 server->watches = NULL;
170 if (server->timeouts)
172 _dbus_timeout_list_free (server->timeouts);
173 server->timeouts = NULL;
177 dbus_free (server->address);
178 server->address = NULL;
180 _dbus_string_free (&server->guid_hex);
186 * Finalizes the members of the DBusServer base class.
187 * Chained up to by subclass finalizers.
189 * @param server the server.
192 _dbus_server_finalize_base (DBusServer *server)
194 /* We don't have the lock, but nobody should be accessing
195 * concurrently since they don't have a ref
197 #ifndef DBUS_DISABLE_CHECKS
198 _dbus_assert (!server->have_server_lock);
200 _dbus_assert (server->disconnected);
202 /* calls out to application code... */
203 _dbus_data_slot_list_free (&server->slot_list);
205 dbus_server_set_new_connection_function (server, NULL, NULL, NULL);
207 _dbus_watch_list_free (server->watches);
208 _dbus_timeout_list_free (server->timeouts);
210 _dbus_mutex_free_at_location (&server->mutex);
212 dbus_free (server->address);
214 dbus_free_string_array (server->auth_mechanisms);
216 _dbus_string_free (&server->guid_hex);
220 typedef dbus_bool_t (* DBusWatchAddFunction) (DBusWatchList *list,
222 typedef void (* DBusWatchRemoveFunction) (DBusWatchList *list,
224 typedef void (* DBusWatchToggleFunction) (DBusWatchList *list,
226 dbus_bool_t enabled);
229 protected_change_watch (DBusServer *server,
231 DBusWatchAddFunction add_function,
232 DBusWatchRemoveFunction remove_function,
233 DBusWatchToggleFunction toggle_function,
236 DBusWatchList *watches;
239 HAVE_LOCK_CHECK (server);
241 /* This isn't really safe or reasonable; a better pattern is the "do
242 * everything, then drop lock and call out" one; but it has to be
243 * propagated up through all callers
246 watches = server->watches;
249 server->watches = NULL;
250 _dbus_server_ref_unlocked (server);
251 SERVER_UNLOCK (server);
254 retval = (* add_function) (watches, watch);
255 else if (remove_function)
258 (* remove_function) (watches, watch);
263 (* toggle_function) (watches, watch, enabled);
266 SERVER_LOCK (server);
267 server->watches = watches;
268 _dbus_server_unref_unlocked (server);
277 * Adds a watch for this server, chaining out to application-provided
280 * @param server the server.
281 * @param watch the watch to add.
284 _dbus_server_add_watch (DBusServer *server,
287 HAVE_LOCK_CHECK (server);
288 return protected_change_watch (server, watch,
289 _dbus_watch_list_add_watch,
294 * Removes a watch previously added with _dbus_server_remove_watch().
296 * @param server the server.
297 * @param watch the watch to remove.
300 _dbus_server_remove_watch (DBusServer *server,
303 HAVE_LOCK_CHECK (server);
304 protected_change_watch (server, watch,
306 _dbus_watch_list_remove_watch,
311 * Toggles a watch and notifies app via server's
312 * DBusWatchToggledFunction if available. It's an error to call this
313 * function on a watch that was not previously added.
315 * @param server the server.
316 * @param watch the watch to toggle.
317 * @param enabled whether to enable or disable
320 _dbus_server_toggle_watch (DBusServer *server,
324 _dbus_assert (watch != NULL);
326 HAVE_LOCK_CHECK (server);
327 protected_change_watch (server, watch,
329 _dbus_watch_list_toggle_watch,
334 typedef dbus_bool_t (* DBusTimeoutAddFunction) (DBusTimeoutList *list,
335 DBusTimeout *timeout);
336 typedef void (* DBusTimeoutRemoveFunction) (DBusTimeoutList *list,
337 DBusTimeout *timeout);
338 typedef void (* DBusTimeoutToggleFunction) (DBusTimeoutList *list,
339 DBusTimeout *timeout,
340 dbus_bool_t enabled);
344 protected_change_timeout (DBusServer *server,
345 DBusTimeout *timeout,
346 DBusTimeoutAddFunction add_function,
347 DBusTimeoutRemoveFunction remove_function,
348 DBusTimeoutToggleFunction toggle_function,
351 DBusTimeoutList *timeouts;
354 HAVE_LOCK_CHECK (server);
356 /* This isn't really safe or reasonable; a better pattern is the "do everything, then
357 * drop lock and call out" one; but it has to be propagated up through all callers
360 timeouts = server->timeouts;
363 server->timeouts = NULL;
364 _dbus_server_ref_unlocked (server);
365 SERVER_UNLOCK (server);
368 retval = (* add_function) (timeouts, timeout);
369 else if (remove_function)
372 (* remove_function) (timeouts, timeout);
377 (* toggle_function) (timeouts, timeout, enabled);
380 SERVER_LOCK (server);
381 server->timeouts = timeouts;
382 _dbus_server_unref_unlocked (server);
391 * Adds a timeout for this server, chaining out to
392 * application-provided timeout handlers. The timeout should be
393 * repeatedly handled with dbus_timeout_handle() at its given interval
394 * until it is removed.
396 * @param server the server.
397 * @param timeout the timeout to add.
400 _dbus_server_add_timeout (DBusServer *server,
401 DBusTimeout *timeout)
403 return protected_change_timeout (server, timeout,
404 _dbus_timeout_list_add_timeout,
409 * Removes a timeout previously added with _dbus_server_add_timeout().
411 * @param server the server.
412 * @param timeout the timeout to remove.
415 _dbus_server_remove_timeout (DBusServer *server,
416 DBusTimeout *timeout)
418 protected_change_timeout (server, timeout,
420 _dbus_timeout_list_remove_timeout,
425 * Toggles a timeout and notifies app via server's
426 * DBusTimeoutToggledFunction if available. It's an error to call this
427 * function on a timeout that was not previously added.
429 * @param server the server.
430 * @param timeout the timeout to toggle.
431 * @param enabled whether to enable or disable
434 _dbus_server_toggle_timeout (DBusServer *server,
435 DBusTimeout *timeout,
438 protected_change_timeout (server, timeout,
440 _dbus_timeout_list_toggle_timeout,
448 * @addtogroup DBusServer
455 * @typedef DBusServer
457 * An opaque object representing a server that listens for
458 * connections from other applications. Each time a connection
459 * is made, a new DBusConnection is created and made available
460 * via an application-provided DBusNewConnectionFunction.
461 * The DBusNewConnectionFunction is provided with
462 * dbus_server_set_new_connection_function().
467 * Listens for new connections on the given address.
468 * Returns #NULL if listening fails for any reason.
469 * Otherwise returns a new #DBusServer.
470 * dbus_server_set_new_connection_function() and
471 * dbus_server_set_watch_functions() should be called
472 * immediately to render the server fully functional.
474 * @todo error messages on bad address could really be better.
475 * DBusResultCode is a bit limiting here.
477 * @param address the address of this server.
478 * @param error location to store rationale for failure.
479 * @returns a new DBusServer, or #NULL on failure.
483 dbus_server_listen (const char *address,
487 DBusAddressEntry **entries;
489 const char *address_problem_type;
490 const char *address_problem_field;
491 const char *address_problem_other;
493 _dbus_return_val_if_fail (address != NULL, NULL);
494 _dbus_return_val_if_error_is_set (error, NULL);
496 if (!dbus_parse_address (address, &entries, &len, error))
500 address_problem_type = NULL;
501 address_problem_field = NULL;
502 address_problem_other = NULL;
504 for (i = 0; i < len; i++)
508 method = dbus_address_entry_get_method (entries[i]);
510 if (strcmp (method, "unix") == 0)
512 const char *path = dbus_address_entry_get_value (entries[i], "path");
513 const char *tmpdir = dbus_address_entry_get_value (entries[i], "tmpdir");
514 const char *abstract = dbus_address_entry_get_value (entries[i], "abstract");
516 if (path == NULL && tmpdir == NULL && abstract == NULL)
518 address_problem_type = "unix";
519 address_problem_field = "path or tmpdir or abstract";
523 if ((path && tmpdir) ||
524 (path && abstract) ||
525 (tmpdir && abstract))
527 address_problem_other = "cannot specify two of \"path\" and \"tmpdir\" and \"abstract\" at the same time";
533 DBusString full_path;
536 if (!_dbus_string_init (&full_path))
538 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
542 if (!_dbus_string_init (&filename))
544 _dbus_string_free (&full_path);
545 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
549 if (!_dbus_string_append (&filename,
551 !_dbus_generate_random_ascii (&filename, 10) ||
552 !_dbus_string_append (&full_path, tmpdir) ||
553 !_dbus_concat_dir_and_file (&full_path, &filename))
555 _dbus_string_free (&full_path);
556 _dbus_string_free (&filename);
557 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
561 /* Always use abstract namespace if possible with tmpdir */
564 _dbus_server_new_for_domain_socket (_dbus_string_get_const_data (&full_path),
565 #ifdef HAVE_ABSTRACT_SOCKETS
572 _dbus_string_free (&full_path);
573 _dbus_string_free (&filename);
578 server = _dbus_server_new_for_domain_socket (path, FALSE, error);
580 server = _dbus_server_new_for_domain_socket (abstract, TRUE, error);
583 else if (strcmp (method, "tcp") == 0)
585 const char *host = dbus_address_entry_get_value (entries[i], "host");
586 const char *port = dbus_address_entry_get_value (entries[i], "port");
593 address_problem_type = "tcp";
594 address_problem_field = "port";
598 _dbus_string_init_const (&str, port);
599 sresult = _dbus_string_parse_int (&str, 0, &lport, NULL);
600 _dbus_string_free (&str);
602 if (sresult == FALSE || lport <= 0 || lport > 65535)
604 address_problem_other = "Port is not an integer between 0 and 65535";
608 server = _dbus_server_new_for_tcp_socket (host, lport, error);
613 #ifdef DBUS_BUILD_TESTS
614 else if (strcmp (method, "debug-pipe") == 0)
616 const char *name = dbus_address_entry_get_value (entries[i], "name");
620 address_problem_type = "debug-pipe";
621 address_problem_field = "name";
625 server = _dbus_server_debug_pipe_new (name, error);
630 address_problem_other = "Unknown address type (examples of valid types are \"unix\" and \"tcp\")";
640 dbus_address_entries_free (entries);
644 dbus_address_entries_free (entries);
645 if (address_problem_type != NULL)
646 dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS,
647 "Server address of type %s was missing argument %s",
648 address_problem_type, address_problem_field);
650 dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS,
651 "Could not parse server address: %s",
652 address_problem_other);
658 * Increments the reference count of a DBusServer.
660 * @param server the server.
661 * @returns the server
664 dbus_server_ref (DBusServer *server)
666 _dbus_return_val_if_fail (server != NULL, NULL);
667 _dbus_return_val_if_fail (server->refcount.value > 0, NULL);
669 #ifdef DBUS_HAVE_ATOMIC_INT
670 _dbus_atomic_inc (&server->refcount);
672 SERVER_LOCK (server);
673 _dbus_assert (server->refcount.value > 0);
675 server->refcount.value += 1;
676 SERVER_UNLOCK (server);
683 * Decrements the reference count of a DBusServer. Finalizes the
684 * server if the reference count reaches zero.
686 * The server must be disconnected before the refcount reaches zero.
688 * @param server the server.
691 dbus_server_unref (DBusServer *server)
693 dbus_bool_t last_unref;
695 _dbus_return_if_fail (server != NULL);
696 _dbus_return_if_fail (server->refcount.value > 0);
698 #ifdef DBUS_HAVE_ATOMIC_INT
699 last_unref = (_dbus_atomic_dec (&server->refcount) == 1);
701 SERVER_LOCK (server);
703 _dbus_assert (server->refcount.value > 0);
705 server->refcount.value -= 1;
706 last_unref = (server->refcount.value == 0);
708 SERVER_UNLOCK (server);
714 _dbus_assert (server->disconnected);
716 _dbus_assert (server->vtable->finalize != NULL);
718 (* server->vtable->finalize) (server);
723 * Like dbus_server_ref() but does not acquire the lock (must already be held)
725 * @param server the server.
728 _dbus_server_ref_unlocked (DBusServer *server)
730 _dbus_assert (server != NULL);
731 _dbus_assert (server->refcount.value > 0);
733 HAVE_LOCK_CHECK (server);
735 #ifdef DBUS_HAVE_ATOMIC_INT
736 _dbus_atomic_inc (&server->refcount);
738 _dbus_assert (server->refcount.value > 0);
740 server->refcount.value += 1;
745 * Like dbus_server_unref() but does not acquire the lock (must already be held)
747 * @param server the server.
750 _dbus_server_unref_unlocked (DBusServer *server)
752 dbus_bool_t last_unref;
754 _dbus_assert (server != NULL);
755 _dbus_assert (server->refcount.value > 0);
757 HAVE_LOCK_CHECK (server);
759 #ifdef DBUS_HAVE_ATOMIC_INT
760 last_unref = (_dbus_atomic_dec (&server->refcount) == 1);
762 _dbus_assert (server->refcount.value > 0);
764 server->refcount.value -= 1;
765 last_unref = (server->refcount.value == 0);
770 _dbus_assert (server->disconnected);
772 SERVER_UNLOCK (server);
774 _dbus_assert (server->vtable->finalize != NULL);
776 (* server->vtable->finalize) (server);
781 * Releases the server's address and stops listening for
782 * new clients. If called more than once, only the first
783 * call has an effect. Does not modify the server's
786 * @param server the server.
789 dbus_server_disconnect (DBusServer *server)
791 _dbus_return_if_fail (server != NULL);
792 _dbus_return_if_fail (server->refcount.value > 0);
794 SERVER_LOCK (server);
795 _dbus_server_ref_unlocked (server);
797 _dbus_assert (server->vtable->disconnect != NULL);
799 if (!server->disconnected)
801 /* this has to be first so recursive calls to disconnect don't happen */
802 server->disconnected = TRUE;
804 (* server->vtable->disconnect) (server);
807 SERVER_UNLOCK (server);
808 dbus_server_unref (server);
812 * Returns #TRUE if the server is still listening for new connections.
814 * @param server the server.
817 dbus_server_get_is_connected (DBusServer *server)
821 _dbus_return_val_if_fail (server != NULL, FALSE);
823 SERVER_LOCK (server);
824 retval = !server->disconnected;
825 SERVER_UNLOCK (server);
831 * Returns the address of the server, as a newly-allocated
832 * string which must be freed by the caller.
834 * @param server the server
835 * @returns the address or #NULL if no memory
838 dbus_server_get_address (DBusServer *server)
842 _dbus_return_val_if_fail (server != NULL, NULL);
844 SERVER_LOCK (server);
845 retval = _dbus_strdup (server->address);
846 SERVER_UNLOCK (server);
852 * Sets a function to be used for handling new connections. The given
853 * function is passed each new connection as the connection is
854 * created. If the new connection function increments the connection's
855 * reference count, the connection will stay alive. Otherwise, the
856 * connection will be unreferenced and closed.
858 * @param server the server.
859 * @param function a function to handle new connections.
860 * @param data data to pass to the new connection handler.
861 * @param free_data_function function to free the data.
864 dbus_server_set_new_connection_function (DBusServer *server,
865 DBusNewConnectionFunction function,
867 DBusFreeFunction free_data_function)
869 DBusFreeFunction old_free_function;
872 _dbus_return_if_fail (server != NULL);
874 SERVER_LOCK (server);
875 old_free_function = server->new_connection_free_data_function;
876 old_data = server->new_connection_data;
878 server->new_connection_function = function;
879 server->new_connection_data = data;
880 server->new_connection_free_data_function = free_data_function;
881 SERVER_UNLOCK (server);
883 if (old_free_function != NULL)
884 (* old_free_function) (old_data);
888 * Sets the watch functions for the connection. These functions are
889 * responsible for making the application's main loop aware of file
890 * descriptors that need to be monitored for events.
892 * This function behaves exactly like dbus_connection_set_watch_functions();
893 * see the documentation for that routine.
895 * @param server the server.
896 * @param add_function function to begin monitoring a new descriptor.
897 * @param remove_function function to stop monitoring a descriptor.
898 * @param toggled_function function to notify when the watch is enabled/disabled
899 * @param data data to pass to add_function and remove_function.
900 * @param free_data_function function to be called to free the data.
901 * @returns #FALSE on failure (no memory)
904 dbus_server_set_watch_functions (DBusServer *server,
905 DBusAddWatchFunction add_function,
906 DBusRemoveWatchFunction remove_function,
907 DBusWatchToggledFunction toggled_function,
909 DBusFreeFunction free_data_function)
912 DBusWatchList *watches;
914 _dbus_return_val_if_fail (server != NULL, FALSE);
916 SERVER_LOCK (server);
917 watches = server->watches;
918 server->watches = NULL;
921 SERVER_UNLOCK (server);
922 result = _dbus_watch_list_set_functions (watches,
928 SERVER_LOCK (server);
932 _dbus_warn ("Re-entrant call to %s\n", _DBUS_FUNCTION_NAME);
935 server->watches = watches;
936 SERVER_UNLOCK (server);
942 * Sets the timeout functions for the connection. These functions are
943 * responsible for making the application's main loop aware of timeouts.
945 * This function behaves exactly like dbus_connection_set_timeout_functions();
946 * see the documentation for that routine.
948 * @param server the server.
949 * @param add_function function to add a timeout.
950 * @param remove_function function to remove a timeout.
951 * @param toggled_function function to notify when the timeout is enabled/disabled
952 * @param data data to pass to add_function and remove_function.
953 * @param free_data_function function to be called to free the data.
954 * @returns #FALSE on failure (no memory)
957 dbus_server_set_timeout_functions (DBusServer *server,
958 DBusAddTimeoutFunction add_function,
959 DBusRemoveTimeoutFunction remove_function,
960 DBusTimeoutToggledFunction toggled_function,
962 DBusFreeFunction free_data_function)
965 DBusTimeoutList *timeouts;
967 _dbus_return_val_if_fail (server != NULL, FALSE);
969 SERVER_LOCK (server);
970 timeouts = server->timeouts;
971 server->timeouts = NULL;
974 SERVER_UNLOCK (server);
975 result = _dbus_timeout_list_set_functions (timeouts,
981 SERVER_LOCK (server);
985 _dbus_warn ("Re-entrant call to %s\n", _DBUS_FUNCTION_NAME);
988 server->timeouts = timeouts;
989 SERVER_UNLOCK (server);
995 * Sets the authentication mechanisms that this server offers
996 * to clients, as a list of SASL mechanisms. This function
997 * only affects connections created *after* it is called.
998 * Pass #NULL instead of an array to use all available mechanisms.
1000 * @param server the server
1001 * @param mechanisms #NULL-terminated array of mechanisms
1002 * @returns #FALSE if no memory
1005 dbus_server_set_auth_mechanisms (DBusServer *server,
1006 const char **mechanisms)
1010 _dbus_return_val_if_fail (server != NULL, FALSE);
1012 SERVER_LOCK (server);
1014 if (mechanisms != NULL)
1016 copy = _dbus_dup_string_array (mechanisms);
1023 dbus_free_string_array (server->auth_mechanisms);
1024 server->auth_mechanisms = copy;
1026 SERVER_UNLOCK (server);
1032 static DBusDataSlotAllocator slot_allocator;
1033 _DBUS_DEFINE_GLOBAL_LOCK (server_slots);
1036 * Allocates an integer ID to be used for storing application-specific
1037 * data on any DBusServer. The allocated ID may then be used
1038 * with dbus_server_set_data() and dbus_server_get_data().
1039 * The slot must be initialized with -1. If a nonnegative
1040 * slot is passed in, the refcount is incremented on that
1041 * slot, rather than creating a new slot.
1043 * The allocated slot is global, i.e. all DBusServer objects will have
1044 * a slot with the given integer ID reserved.
1046 * @param slot_p address of global variable storing the slot ID
1047 * @returns #FALSE on no memory
1050 dbus_server_allocate_data_slot (dbus_int32_t *slot_p)
1052 return _dbus_data_slot_allocator_alloc (&slot_allocator,
1053 (DBusMutex **)&_DBUS_LOCK_NAME (server_slots),
1058 * Deallocates a global ID for server data slots.
1059 * dbus_server_get_data() and dbus_server_set_data()
1060 * may no longer be used with this slot.
1061 * Existing data stored on existing DBusServer objects
1062 * will be freed when the server is finalized,
1063 * but may not be retrieved (and may only be replaced
1064 * if someone else reallocates the slot).
1066 * @param slot_p address of the slot to deallocate
1069 dbus_server_free_data_slot (dbus_int32_t *slot_p)
1071 _dbus_return_if_fail (*slot_p >= 0);
1073 _dbus_data_slot_allocator_free (&slot_allocator, slot_p);
1077 * Stores a pointer on a DBusServer, along
1078 * with an optional function to be used for freeing
1079 * the data when the data is set again, or when
1080 * the server is finalized. The slot number
1081 * must have been allocated with dbus_server_allocate_data_slot().
1083 * @param server the server
1084 * @param slot the slot number
1085 * @param data the data to store
1086 * @param free_data_func finalizer function for the data
1087 * @returns #TRUE if there was enough memory to store the data
1090 dbus_server_set_data (DBusServer *server,
1093 DBusFreeFunction free_data_func)
1095 DBusFreeFunction old_free_func;
1099 _dbus_return_val_if_fail (server != NULL, FALSE);
1101 SERVER_LOCK (server);
1103 retval = _dbus_data_slot_list_set (&slot_allocator,
1105 slot, data, free_data_func,
1106 &old_free_func, &old_data);
1109 SERVER_UNLOCK (server);
1113 /* Do the actual free outside the server lock */
1115 (* old_free_func) (old_data);
1122 * Retrieves data previously set with dbus_server_set_data().
1123 * The slot must still be allocated (must not have been freed).
1125 * @param server the server
1126 * @param slot the slot to get data from
1127 * @returns the data, or #NULL if not found
1130 dbus_server_get_data (DBusServer *server,
1135 _dbus_return_val_if_fail (server != NULL, NULL);
1137 SERVER_LOCK (server);
1139 res = _dbus_data_slot_list_get (&slot_allocator,
1143 SERVER_UNLOCK (server);
1150 #ifdef DBUS_BUILD_TESTS
1151 #include "dbus-test.h"
1154 _dbus_server_test (void)
1156 const char *valid_addresses[] = {
1158 "unix:path=./boogie",
1159 "tcp:host=localhost,port=1234",
1160 "tcp:host=localhost,port=1234;tcp:port=5678",
1161 "tcp:port=1234;unix:path=./boogie",
1167 for (i = 0; i < _DBUS_N_ELEMENTS (valid_addresses); i++)
1169 server = dbus_server_listen (valid_addresses[i], NULL);
1171 _dbus_assert_not_reached ("Failed to listen for valid address.");
1173 dbus_server_disconnect (server);
1174 dbus_server_unref (server);
1176 /* Try disconnecting before unreffing */
1177 server = dbus_server_listen (valid_addresses[i], NULL);
1179 _dbus_assert_not_reached ("Failed to listen for valid address.");
1181 dbus_server_disconnect (server);
1182 dbus_server_unref (server);
1188 #endif /* DBUS_BUILD_TESTS */