1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-server.c DBusServer object
4 * Copyright (C) 2002, 2003 Red Hat Inc.
6 * Licensed under the Academic Free License version 1.2
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 #ifdef DBUS_BUILD_TESTS
27 #include "dbus-server-debug-pipe.h"
29 #include "dbus-address.h"
32 * @defgroup DBusServer DBusServer
34 * @brief Server that listens for new connections.
36 * Types and functions related to DBusServer.
37 * A DBusServer represents a server that other applications
38 * can connect to. Each connection from another application
39 * is represented by a DBusConnection.
41 * @todo Thread safety hasn't been looked at for #DBusServer
42 * @todo Need notification to apps of disconnection, may matter for some transports
46 * @defgroup DBusServerInternals DBusServer implementation details
47 * @ingroup DBusInternals
48 * @brief Implementation details of DBusServer
54 * Initializes the members of the DBusServer base class.
55 * Chained up to by subclass constructors.
57 * @param server the server.
58 * @param vtable the vtable for the subclass.
59 * @param address the server's address
60 * @returns #TRUE on success.
63 _dbus_server_init_base (DBusServer *server,
64 const DBusServerVTable *vtable,
65 const DBusString *address)
67 server->vtable = vtable;
70 server->address = NULL;
71 server->watches = NULL;
72 server->timeouts = NULL;
74 if (!_dbus_string_copy_data (address, &server->address))
77 server->watches = _dbus_watch_list_new ();
78 if (server->watches == NULL)
81 server->timeouts = _dbus_timeout_list_new ();
82 if (server->timeouts == NULL)
85 _dbus_data_slot_list_init (&server->slot_list);
87 _dbus_verbose ("Initialized server on address %s\n", server->address);
94 _dbus_watch_list_free (server->watches);
95 server->watches = NULL;
99 _dbus_timeout_list_free (server->timeouts);
100 server->timeouts = NULL;
104 dbus_free (server->address);
105 server->address = NULL;
112 * Finalizes the members of the DBusServer base class.
113 * Chained up to by subclass finalizers.
115 * @param server the server.
118 _dbus_server_finalize_base (DBusServer *server)
120 /* calls out to application code... */
121 _dbus_data_slot_list_free (&server->slot_list);
123 dbus_server_set_new_connection_function (server, NULL, NULL, NULL);
125 if (!server->disconnected)
126 dbus_server_disconnect (server);
128 _dbus_watch_list_free (server->watches);
129 _dbus_timeout_list_free (server->timeouts);
131 dbus_free (server->address);
133 dbus_free_string_array (server->auth_mechanisms);
137 * Adds a watch for this server, chaining out to application-provided
140 * @param server the server.
141 * @param watch the watch to add.
144 _dbus_server_add_watch (DBusServer *server,
147 return _dbus_watch_list_add_watch (server->watches, watch);
151 * Removes a watch previously added with _dbus_server_remove_watch().
153 * @param server the server.
154 * @param watch the watch to remove.
157 _dbus_server_remove_watch (DBusServer *server,
160 _dbus_watch_list_remove_watch (server->watches, watch);
164 * Toggles a watch and notifies app via server's
165 * DBusWatchToggledFunction if available. It's an error to call this
166 * function on a watch that was not previously added.
168 * @param server the server.
169 * @param watch the watch to toggle.
170 * @param enabled whether to enable or disable
173 _dbus_server_toggle_watch (DBusServer *server,
177 if (server->watches) /* null during finalize */
178 _dbus_watch_list_toggle_watch (server->watches,
183 * Adds a timeout for this server, chaining out to
184 * application-provided timeout handlers. The timeout should be
185 * repeatedly handled with dbus_timeout_handle() at its given interval
186 * until it is removed.
188 * @param server the server.
189 * @param timeout the timeout to add.
192 _dbus_server_add_timeout (DBusServer *server,
193 DBusTimeout *timeout)
195 return _dbus_timeout_list_add_timeout (server->timeouts, timeout);
199 * Removes a timeout previously added with _dbus_server_add_timeout().
201 * @param server the server.
202 * @param timeout the timeout to remove.
205 _dbus_server_remove_timeout (DBusServer *server,
206 DBusTimeout *timeout)
208 _dbus_timeout_list_remove_timeout (server->timeouts, timeout);
212 * Toggles a timeout and notifies app via server's
213 * DBusTimeoutToggledFunction if available. It's an error to call this
214 * function on a timeout that was not previously added.
216 * @param server the server.
217 * @param timeout the timeout to toggle.
218 * @param enabled whether to enable or disable
221 _dbus_server_toggle_timeout (DBusServer *server,
222 DBusTimeout *timeout,
225 if (server->timeouts) /* null during finalize */
226 _dbus_timeout_list_toggle_timeout (server->timeouts,
234 * @addtogroup DBusServer
241 * @typedef DBusServer
243 * An opaque object representing a server that listens for
244 * connections from other applications. Each time a connection
245 * is made, a new DBusConnection is created and made available
246 * via an application-provided DBusNewConnectionFunction.
247 * The DBusNewConnectionFunction is provided with
248 * dbus_server_set_new_connection_function().
253 * Listens for new connections on the given address.
254 * Returns #NULL if listening fails for any reason.
255 * Otherwise returns a new #DBusServer.
256 * dbus_server_set_new_connection_function() and
257 * dbus_server_set_watch_functions() should be called
258 * immediately to render the server fully functional.
260 * @todo error messages on bad address could really be better.
261 * DBusResultCode is a bit limiting here.
263 * @param address the address of this server.
264 * @param error location to store rationale for failure.
265 * @returns a new DBusServer, or #NULL on failure.
269 dbus_server_listen (const char *address,
273 DBusAddressEntry **entries;
275 const char *address_problem_type;
276 const char *address_problem_field;
277 const char *address_problem_other;
279 _dbus_return_val_if_fail (address != NULL, NULL);
280 _dbus_return_val_if_error_is_set (error, NULL);
282 if (!dbus_parse_address (address, &entries, &len, error))
286 address_problem_type = NULL;
287 address_problem_field = NULL;
288 address_problem_other = NULL;
290 for (i = 0; i < len; i++)
292 const char *method = dbus_address_entry_get_method (entries[i]);
294 if (strcmp (method, "unix") == 0)
296 const char *path = dbus_address_entry_get_value (entries[i], "path");
297 const char *tmpdir = dbus_address_entry_get_value (entries[i], "tmpdir");
298 const char *abstract = dbus_address_entry_get_value (entries[i], "abstract");
300 if (path == NULL && tmpdir == NULL && abstract == NULL)
302 address_problem_type = "unix";
303 address_problem_field = "path or tmpdir or abstract";
307 if ((path && tmpdir) ||
308 (path && abstract) ||
309 (tmpdir && abstract))
311 address_problem_other = "cannot specify two of \"path\" and \"tmpdir\" and \"abstract\" at the same time";
317 DBusString full_path;
320 if (!_dbus_string_init (&full_path))
322 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
326 if (!_dbus_string_init (&filename))
328 _dbus_string_free (&full_path);
329 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
333 if (!_dbus_string_append (&filename,
335 !_dbus_generate_random_ascii (&filename, 10) ||
336 !_dbus_string_append (&full_path, tmpdir) ||
337 !_dbus_concat_dir_and_file (&full_path, &filename))
339 _dbus_string_free (&full_path);
340 _dbus_string_free (&filename);
341 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
345 /* FIXME - we will unconditionally unlink() the path if
346 * we don't support abstract namespace. unlink() does
347 * not follow symlinks, but would like independent
348 * confirmation this is safe enough. See also
349 * _dbus_listen_unix_socket() and comments therein.
352 /* Always use abstract namespace if possible with tmpdir */
355 _dbus_server_new_for_domain_socket (_dbus_string_get_const_data (&full_path),
356 #ifdef HAVE_ABSTRACT_SOCKETS
363 _dbus_string_free (&full_path);
364 _dbus_string_free (&filename);
369 server = _dbus_server_new_for_domain_socket (path, FALSE, error);
371 server = _dbus_server_new_for_domain_socket (abstract, TRUE, error);
374 else if (strcmp (method, "tcp") == 0)
376 const char *host = dbus_address_entry_get_value (entries[i], "host");
377 const char *port = dbus_address_entry_get_value (entries[i], "port");
384 address_problem_type = "tcp";
385 address_problem_field = "port";
389 _dbus_string_init_const (&str, port);
390 sresult = _dbus_string_parse_int (&str, 0, &lport, NULL);
391 _dbus_string_free (&str);
393 if (sresult == FALSE || lport <= 0 || lport > 65535)
395 address_problem_other = "Port is not an integer between 0 and 65535";
399 server = _dbus_server_new_for_tcp_socket (host, lport, error);
404 #ifdef DBUS_BUILD_TESTS
405 else if (strcmp (method, "debug-pipe") == 0)
407 const char *name = dbus_address_entry_get_value (entries[i], "name");
411 address_problem_type = "debug-pipe";
412 address_problem_field = "name";
416 server = _dbus_server_debug_pipe_new (name, error);
421 address_problem_other = "Unknown address type (examples of valid types are \"unix\" and \"tcp\")";
431 dbus_address_entries_free (entries);
435 dbus_address_entries_free (entries);
436 if (address_problem_type != NULL)
437 dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS,
438 "Server address of type %s was missing argument %s",
439 address_problem_type, address_problem_field);
441 dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS,
442 "Could not parse server address: %s",
443 address_problem_other);
449 * Increments the reference count of a DBusServer.
451 * @param server the server.
454 dbus_server_ref (DBusServer *server)
456 _dbus_return_if_fail (server != NULL);
458 server->refcount += 1;
462 * Decrements the reference count of a DBusServer. Finalizes the
463 * server if the reference count reaches zero. The server connection
464 * will be closed as with dbus_server_disconnect() when the server is
467 * @param server the server.
470 dbus_server_unref (DBusServer *server)
472 _dbus_return_if_fail (server != NULL);
474 _dbus_assert (server->refcount > 0);
476 server->refcount -= 1;
477 if (server->refcount == 0)
479 _dbus_assert (server->vtable->finalize != NULL);
481 (* server->vtable->finalize) (server);
486 * Releases the server's address and stops listening for
487 * new clients. If called more than once, only the first
488 * call has an effect. Does not modify the server's
491 * @param server the server.
494 dbus_server_disconnect (DBusServer *server)
496 _dbus_return_if_fail (server != NULL);
498 _dbus_assert (server->vtable->disconnect != NULL);
500 if (server->disconnected)
503 (* server->vtable->disconnect) (server);
504 server->disconnected = TRUE;
508 * Returns #TRUE if the server is still listening for new connections.
510 * @param server the server.
513 dbus_server_get_is_connected (DBusServer *server)
515 _dbus_return_val_if_fail (server != NULL, FALSE);
517 return !server->disconnected;
521 * Returns the address of the server, as a newly-allocated
522 * string which must be freed by the caller.
524 * @param server the server
525 * @returns the address or #NULL if no memory
528 dbus_server_get_address (DBusServer *server)
530 _dbus_return_val_if_fail (server != NULL, NULL);
532 return _dbus_strdup (server->address);
536 * Sets a function to be used for handling new connections. The given
537 * function is passed each new connection as the connection is
538 * created. If the new connection function increments the connection's
539 * reference count, the connection will stay alive. Otherwise, the
540 * connection will be unreferenced and closed.
542 * @param server the server.
543 * @param function a function to handle new connections.
544 * @param data data to pass to the new connection handler.
545 * @param free_data_function function to free the data.
548 dbus_server_set_new_connection_function (DBusServer *server,
549 DBusNewConnectionFunction function,
551 DBusFreeFunction free_data_function)
553 _dbus_return_if_fail (server != NULL);
555 if (server->new_connection_free_data_function != NULL)
556 (* server->new_connection_free_data_function) (server->new_connection_data);
558 server->new_connection_function = function;
559 server->new_connection_data = data;
560 server->new_connection_free_data_function = free_data_function;
564 * Sets the watch functions for the connection. These functions are
565 * responsible for making the application's main loop aware of file
566 * descriptors that need to be monitored for events.
568 * This function behaves exactly like dbus_connection_set_watch_functions();
569 * see the documentation for that routine.
571 * @param server the server.
572 * @param add_function function to begin monitoring a new descriptor.
573 * @param remove_function function to stop monitoring a descriptor.
574 * @param toggled_function function to notify when the watch is enabled/disabled
575 * @param data data to pass to add_function and remove_function.
576 * @param free_data_function function to be called to free the data.
577 * @returns #FALSE on failure (no memory)
580 dbus_server_set_watch_functions (DBusServer *server,
581 DBusAddWatchFunction add_function,
582 DBusRemoveWatchFunction remove_function,
583 DBusWatchToggledFunction toggled_function,
585 DBusFreeFunction free_data_function)
587 _dbus_return_val_if_fail (server != NULL, FALSE);
589 return _dbus_watch_list_set_functions (server->watches,
598 * Sets the timeout functions for the connection. These functions are
599 * responsible for making the application's main loop aware of timeouts.
601 * This function behaves exactly like dbus_connection_set_timeout_functions();
602 * see the documentation for that routine.
604 * @param server the server.
605 * @param add_function function to add a timeout.
606 * @param remove_function function to remove a timeout.
607 * @param toggled_function function to notify when the timeout is enabled/disabled
608 * @param data data to pass to add_function and remove_function.
609 * @param free_data_function function to be called to free the data.
610 * @returns #FALSE on failure (no memory)
613 dbus_server_set_timeout_functions (DBusServer *server,
614 DBusAddTimeoutFunction add_function,
615 DBusRemoveTimeoutFunction remove_function,
616 DBusTimeoutToggledFunction toggled_function,
618 DBusFreeFunction free_data_function)
620 _dbus_return_val_if_fail (server != NULL, FALSE);
622 return _dbus_timeout_list_set_functions (server->timeouts,
623 add_function, remove_function,
625 data, free_data_function);
629 * Sets the authentication mechanisms that this server offers
630 * to clients, as a list of SASL mechanisms. This function
631 * only affects connections created *after* it is called.
632 * Pass #NULL instead of an array to use all available mechanisms.
634 * @param server the server
635 * @param mechanisms #NULL-terminated array of mechanisms
636 * @returns #FALSE if no memory
639 dbus_server_set_auth_mechanisms (DBusServer *server,
640 const char **mechanisms)
644 _dbus_return_val_if_fail (server != NULL, FALSE);
646 if (mechanisms != NULL)
648 copy = _dbus_dup_string_array (mechanisms);
655 dbus_free_string_array (server->auth_mechanisms);
656 server->auth_mechanisms = copy;
662 static DBusDataSlotAllocator slot_allocator;
663 _DBUS_DEFINE_GLOBAL_LOCK (server_slots);
666 * Allocates an integer ID to be used for storing application-specific
667 * data on any DBusServer. The allocated ID may then be used
668 * with dbus_server_set_data() and dbus_server_get_data().
669 * If allocation fails, -1 is returned. Again, the allocated
670 * slot is global, i.e. all DBusServer objects will
671 * have a slot with the given integer ID reserved.
673 * @returns -1 on failure, otherwise the data slot ID
676 dbus_server_allocate_data_slot (void)
678 return _dbus_data_slot_allocator_alloc (&slot_allocator,
679 _DBUS_LOCK_NAME (server_slots));
683 * Deallocates a global ID for server data slots.
684 * dbus_server_get_data() and dbus_server_set_data()
685 * may no longer be used with this slot.
686 * Existing data stored on existing DBusServer objects
687 * will be freed when the server is finalized,
688 * but may not be retrieved (and may only be replaced
689 * if someone else reallocates the slot).
691 * @param slot the slot to deallocate
694 dbus_server_free_data_slot (int slot)
696 _dbus_return_if_fail (slot >= 0);
698 _dbus_data_slot_allocator_free (&slot_allocator, slot);
702 * Stores a pointer on a DBusServer, along
703 * with an optional function to be used for freeing
704 * the data when the data is set again, or when
705 * the server is finalized. The slot number
706 * must have been allocated with dbus_server_allocate_data_slot().
708 * @param server the server
709 * @param slot the slot number
710 * @param data the data to store
711 * @param free_data_func finalizer function for the data
712 * @returns #TRUE if there was enough memory to store the data
715 dbus_server_set_data (DBusServer *server,
718 DBusFreeFunction free_data_func)
720 DBusFreeFunction old_free_func;
724 _dbus_return_val_if_fail (server != NULL, FALSE);
727 dbus_mutex_lock (server->mutex);
730 retval = _dbus_data_slot_list_set (&slot_allocator,
732 slot, data, free_data_func,
733 &old_free_func, &old_data);
736 dbus_mutex_unlock (server->mutex);
741 /* Do the actual free outside the server lock */
743 (* old_free_func) (old_data);
750 * Retrieves data previously set with dbus_server_set_data().
751 * The slot must still be allocated (must not have been freed).
753 * @param server the server
754 * @param slot the slot to get data from
755 * @returns the data, or #NULL if not found
758 dbus_server_get_data (DBusServer *server,
763 _dbus_return_val_if_fail (server != NULL, NULL);
766 dbus_mutex_lock (server->mutex);
769 res = _dbus_data_slot_list_get (&slot_allocator,
774 dbus_mutex_unlock (server->mutex);