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.h"
28 #include "dbus-server-debug-pipe.h"
30 #include "dbus-address.h"
33 * @defgroup DBusServer DBusServer
35 * @brief Server that listens for new connections.
37 * Types and functions related to DBusServer.
38 * A DBusServer represents a server that other applications
39 * can connect to. Each connection from another application
40 * is represented by a DBusConnection.
42 * @todo Thread safety hasn't been looked at for #DBusServer
43 * @todo Need notification to apps of disconnection, may matter for some transports
47 * @defgroup DBusServerInternals DBusServer implementation details
48 * @ingroup DBusInternals
49 * @brief Implementation details of DBusServer
55 * Initializes the members of the DBusServer base class.
56 * Chained up to by subclass constructors.
58 * @param server the server.
59 * @param vtable the vtable for the subclass.
60 * @returns #TRUE on success.
63 _dbus_server_init_base (DBusServer *server,
64 const DBusServerVTable *vtable)
66 server->vtable = vtable;
69 server->watches = _dbus_watch_list_new ();
70 if (server->watches == NULL)
73 server->timeouts = _dbus_timeout_list_new ();
74 if (server->timeouts == NULL)
76 _dbus_watch_list_free (server->watches);
77 server->watches = NULL;
81 server->connection_counter = _dbus_counter_new ();
82 if (server->connection_counter == NULL)
84 _dbus_watch_list_free (server->watches);
85 server->watches = NULL;
86 _dbus_timeout_list_free (server->timeouts);
87 server->timeouts = NULL;
92 server->max_connections = 256; /* same as an X server, seems like a nice default */
94 _dbus_data_slot_list_init (&server->slot_list);
100 * Finalizes the members of the DBusServer base class.
101 * Chained up to by subclass finalizers.
103 * @param server the server.
106 _dbus_server_finalize_base (DBusServer *server)
108 /* calls out to application code... */
109 _dbus_data_slot_list_free (&server->slot_list);
111 dbus_server_set_new_connection_function (server, NULL, NULL, NULL);
113 if (!server->disconnected)
114 dbus_server_disconnect (server);
116 _dbus_watch_list_free (server->watches);
117 _dbus_timeout_list_free (server->timeouts);
118 _dbus_counter_unref (server->connection_counter);
122 * Adds a watch for this server, chaining out to application-provided
125 * @param server the server.
126 * @param watch the watch to add.
129 _dbus_server_add_watch (DBusServer *server,
132 return _dbus_watch_list_add_watch (server->watches, watch);
136 * Removes a watch previously added with _dbus_server_remove_watch().
138 * @param server the server.
139 * @param watch the watch to remove.
142 _dbus_server_remove_watch (DBusServer *server,
145 _dbus_watch_list_remove_watch (server->watches, watch);
149 * Toggles a watch and notifies app via server's
150 * DBusWatchToggledFunction if available. It's an error to call this
151 * function on a watch that was not previously added.
153 * @param server the server.
154 * @param watch the watch to toggle.
155 * @param enabled whether to enable or disable
158 _dbus_server_toggle_watch (DBusServer *server,
162 if (server->watches) /* null during finalize */
163 _dbus_watch_list_toggle_watch (server->watches,
168 * Adds a timeout for this server, chaining out to
169 * application-provided timeout handlers. The timeout should be
170 * repeatedly handled with dbus_timeout_handle() at its given interval
171 * until it is removed.
173 * @param server the server.
174 * @param timeout the timeout to add.
177 _dbus_server_add_timeout (DBusServer *server,
178 DBusTimeout *timeout)
180 return _dbus_timeout_list_add_timeout (server->timeouts, timeout);
184 * Removes a timeout previously added with _dbus_server_add_timeout().
186 * @param server the server.
187 * @param timeout the timeout to remove.
190 _dbus_server_remove_timeout (DBusServer *server,
191 DBusTimeout *timeout)
193 _dbus_timeout_list_remove_timeout (server->timeouts, timeout);
197 * Toggles a timeout and notifies app via server's
198 * DBusTimeoutToggledFunction if available. It's an error to call this
199 * function on a timeout that was not previously added.
201 * @param server the server.
202 * @param timeout the timeout to toggle.
203 * @param enabled whether to enable or disable
206 _dbus_server_toggle_timeout (DBusServer *server,
207 DBusTimeout *timeout,
210 if (server->timeouts) /* null during finalize */
211 _dbus_timeout_list_toggle_timeout (server->timeouts,
219 * @addtogroup DBusServer
226 * @typedef DBusServer
228 * An opaque object representing a server that listens for
229 * connections from other applications. Each time a connection
230 * is made, a new DBusConnection is created and made available
231 * via an application-provided DBusNewConnectionFunction.
232 * The DBusNewConnectionFunction is provided with
233 * dbus_server_set_new_connection_function().
238 * Listens for new connections on the given address.
239 * Returns #NULL if listening fails for any reason.
240 * Otherwise returns a new #DBusServer.
241 * dbus_server_set_new_connection_function() and
242 * dbus_server_set_watch_functions() should be called
243 * immediately to render the server fully functional.
245 * @todo error messages on bad address could really be better.
246 * DBusResultCode is a bit limiting here.
248 * @param address the address of this server.
249 * @param error location to store rationale for failure.
250 * @returns a new DBusServer, or #NULL on failure.
254 dbus_server_listen (const char *address,
258 DBusAddressEntry **entries;
260 const char *address_problem_type;
261 const char *address_problem_field;
262 const char *address_problem_other;
264 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
266 if (!dbus_parse_address (address, &entries, &len, error))
270 address_problem_type = NULL;
271 address_problem_field = NULL;
272 address_problem_other = NULL;
274 for (i = 0; i < len; i++)
276 const char *method = dbus_address_entry_get_method (entries[i]);
278 if (strcmp (method, "unix") == 0)
280 const char *path = dbus_address_entry_get_value (entries[i], "path");
284 address_problem_type = "unix";
285 address_problem_field = "path";
289 server = _dbus_server_new_for_domain_socket (path, error);
294 else if (strcmp (method, "tcp") == 0)
296 const char *host = dbus_address_entry_get_value (entries[i], "host");
297 const char *port = dbus_address_entry_get_value (entries[i], "port");
304 address_problem_type = "tcp";
305 address_problem_field = "port";
309 _dbus_string_init_const (&str, port);
310 sresult = _dbus_string_parse_int (&str, 0, &lport, NULL);
311 _dbus_string_free (&str);
313 if (sresult == FALSE || lport <= 0 || lport > 65535)
315 address_problem_other = "Port is not an integer between 0 and 65535";
319 server = _dbus_server_new_for_tcp_socket (host, lport, error);
324 #ifdef DBUS_BUILD_TESTS
325 else if (strcmp (method, "debug") == 0)
327 const char *name = dbus_address_entry_get_value (entries[i], "name");
331 address_problem_type = "debug";
332 address_problem_field = "name";
336 server = _dbus_server_debug_new (name, error);
341 else if (strcmp (method, "debug-pipe") == 0)
343 const char *name = dbus_address_entry_get_value (entries[i], "name");
347 address_problem_type = "debug-pipe";
348 address_problem_field = "name";
352 server = _dbus_server_debug_pipe_new (name, error);
360 address_problem_other = "Unknown address type (examples of valid types are \"unix\" and \"tcp\")";
365 dbus_address_entries_free (entries);
369 dbus_address_entries_free (entries);
370 if (address_problem_type != NULL)
371 dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS,
372 "Server address of type %s was missing argument %s",
373 address_problem_type, address_problem_field);
375 dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS,
376 "Could not parse server address: %s",
377 address_problem_other);
383 * Increments the reference count of a DBusServer.
385 * @param server the server.
388 dbus_server_ref (DBusServer *server)
390 server->refcount += 1;
394 * Decrements the reference count of a DBusServer. Finalizes the
395 * server if the reference count reaches zero. The server connection
396 * will be closed as with dbus_server_disconnect() when the server is
399 * @param server the server.
402 dbus_server_unref (DBusServer *server)
404 _dbus_assert (server != NULL);
405 _dbus_assert (server->refcount > 0);
407 server->refcount -= 1;
408 if (server->refcount == 0)
410 _dbus_assert (server->vtable->finalize != NULL);
412 (* server->vtable->finalize) (server);
417 * Releases the server's address and stops listening for
418 * new clients. If called more than once, only the first
419 * call has an effect. Does not modify the server's
422 * @param server the server.
425 dbus_server_disconnect (DBusServer *server)
427 _dbus_assert (server->vtable->disconnect != NULL);
429 if (server->disconnected)
432 (* server->vtable->disconnect) (server);
433 server->disconnected = TRUE;
437 * Returns #TRUE if the server is still listening for new connections.
439 * @param server the server.
442 dbus_server_get_is_connected (DBusServer *server)
444 return !server->disconnected;
448 * Sets a function to be used for handling new connections. The given
449 * function is passed each new connection as the connection is
450 * created. If the new connection function increments the connection's
451 * reference count, the connection will stay alive. Otherwise, the
452 * connection will be unreferenced and closed.
454 * @param server the server.
455 * @param function a function to handle new connections.
456 * @param data data to pass to the new connection handler.
457 * @param free_data_function function to free the data.
460 dbus_server_set_new_connection_function (DBusServer *server,
461 DBusNewConnectionFunction function,
463 DBusFreeFunction free_data_function)
465 if (server->new_connection_free_data_function != NULL)
466 (* server->new_connection_free_data_function) (server->new_connection_data);
468 server->new_connection_function = function;
469 server->new_connection_data = data;
470 server->new_connection_free_data_function = free_data_function;
474 * Sets the watch functions for the connection. These functions are
475 * responsible for making the application's main loop aware of file
476 * descriptors that need to be monitored for events.
478 * This function behaves exactly like dbus_connection_set_watch_functions();
479 * see the documentation for that routine.
481 * @param server the server.
482 * @param add_function function to begin monitoring a new descriptor.
483 * @param remove_function function to stop monitoring a descriptor.
484 * @param toggled_function function to notify when the watch is enabled/disabled
485 * @param data data to pass to add_function and remove_function.
486 * @param free_data_function function to be called to free the data.
487 * @returns #FALSE on failure (no memory)
490 dbus_server_set_watch_functions (DBusServer *server,
491 DBusAddWatchFunction add_function,
492 DBusRemoveWatchFunction remove_function,
493 DBusWatchToggledFunction toggled_function,
495 DBusFreeFunction free_data_function)
497 return _dbus_watch_list_set_functions (server->watches,
506 * Sets the timeout functions for the connection. These functions are
507 * responsible for making the application's main loop aware of timeouts.
509 * This function behaves exactly like dbus_connection_set_timeout_functions();
510 * see the documentation for that routine.
512 * @param server the server.
513 * @param add_function function to add a timeout.
514 * @param remove_function function to remove a timeout.
515 * @param toggled_function function to notify when the timeout is enabled/disabled
516 * @param data data to pass to add_function and remove_function.
517 * @param free_data_function function to be called to free the data.
518 * @returns #FALSE on failure (no memory)
521 dbus_server_set_timeout_functions (DBusServer *server,
522 DBusAddTimeoutFunction add_function,
523 DBusRemoveTimeoutFunction remove_function,
524 DBusTimeoutToggledFunction toggled_function,
526 DBusFreeFunction free_data_function)
528 return _dbus_timeout_list_set_functions (server->timeouts,
529 add_function, remove_function,
531 data, free_data_function);
535 * Called to notify the server when a previously-added watch
536 * is ready for reading or writing, or has an exception such
539 * If this function returns #FALSE, then the file descriptor may still
540 * be ready for reading or writing, but more memory is needed in order
541 * to do the reading or writing. If you ignore the #FALSE return, your
542 * application may spin in a busy loop on the file descriptor until
543 * memory becomes available, but nothing more catastrophic should
546 * @param server the server.
547 * @param watch the watch.
548 * @param condition the current condition of the file descriptors being watched.
551 dbus_server_handle_watch (DBusServer *server,
553 unsigned int condition)
555 _dbus_assert (server->vtable->handle_watch != NULL);
557 _dbus_watch_sanitize_condition (watch, &condition);
559 return (* server->vtable->handle_watch) (server, watch, condition);
563 * Sets the maximum number of connections that can be open at one
564 * time for this server. If the maximum is reached, and another
565 * client tries to connect, then the oldest unauthenticated client
566 * will be dropped. If no unauthenticated client exists, then
567 * the new connection will be refused.
569 * If the maximum is set to a number lower than the current
570 * number of connections, no current connections are
573 * @todo honoring max_connections has not been implemented
574 * yet. The only real work involved is keeping a list
575 * of live connections on the DBusServer so the oldest
576 * unauthenticated client can be located when required.
578 * @todo for a systemwide daemon, we need a max number of connections
579 * per user, since any user can authenticate a bunch of connections
582 * @todo a single process might listen on multiple mechanisms
583 * (multiple DBusServer) and might want the max connections
584 * value to span all those servers. Should consider
585 * changing the API accordingly, though I'm inclined to
586 * punt this to the app that wants to do it instead of
587 * putting it in the library.
589 * @param server the server
590 * @param max_connections maximum number of connections allowed
593 dbus_server_set_max_connections (DBusServer *server,
596 server->max_connections = max_connections;
600 * Gets the maximum number of connections that can be active
601 * at a time for this server.
603 * @param server the server
604 * @returns maximum number of connections at once
607 dbus_server_get_max_connections (DBusServer *server)
609 return server->max_connections;
613 * Gets the number of #DBusConnection to this server that
614 * have not yet been finalized. i.e. all #DBusConnection that
615 * were passed to #DBusNewConnectionFunction and have not yet been
616 * finalized will count in this total.
618 * @param server the server
619 * @returns the number of connections
622 dbus_server_get_n_connections (DBusServer *server)
624 return _dbus_counter_get_value (server->connection_counter);
628 static DBusDataSlotAllocator slot_allocator;
629 _DBUS_DEFINE_GLOBAL_LOCK (server_slots);
632 * Allocates an integer ID to be used for storing application-specific
633 * data on any DBusServer. The allocated ID may then be used
634 * with dbus_server_set_data() and dbus_server_get_data().
635 * If allocation fails, -1 is returned. Again, the allocated
636 * slot is global, i.e. all DBusServer objects will
637 * have a slot with the given integer ID reserved.
639 * @returns -1 on failure, otherwise the data slot ID
642 dbus_server_allocate_data_slot (void)
644 return _dbus_data_slot_allocator_alloc (&slot_allocator,
645 _DBUS_LOCK_NAME (server_slots));
649 * Deallocates a global ID for server data slots.
650 * dbus_server_get_data() and dbus_server_set_data()
651 * may no longer be used with this slot.
652 * Existing data stored on existing DBusServer objects
653 * will be freed when the server is finalized,
654 * but may not be retrieved (and may only be replaced
655 * if someone else reallocates the slot).
657 * @param slot the slot to deallocate
660 dbus_server_free_data_slot (int slot)
662 _dbus_data_slot_allocator_free (&slot_allocator, slot);
666 * Stores a pointer on a DBusServer, along
667 * with an optional function to be used for freeing
668 * the data when the data is set again, or when
669 * the server is finalized. The slot number
670 * must have been allocated with dbus_server_allocate_data_slot().
672 * @param server the server
673 * @param slot the slot number
674 * @param data the data to store
675 * @param free_data_func finalizer function for the data
676 * @returns #TRUE if there was enough memory to store the data
679 dbus_server_set_data (DBusServer *server,
682 DBusFreeFunction free_data_func)
684 DBusFreeFunction old_free_func;
689 dbus_mutex_lock (server->mutex);
692 retval = _dbus_data_slot_list_set (&slot_allocator,
694 slot, data, free_data_func,
695 &old_free_func, &old_data);
698 dbus_mutex_unlock (server->mutex);
703 /* Do the actual free outside the server lock */
705 (* old_free_func) (old_data);
712 * Retrieves data previously set with dbus_server_set_data().
713 * The slot must still be allocated (must not have been freed).
715 * @param server the server
716 * @param slot the slot to get data from
717 * @returns the data, or #NULL if not found
720 dbus_server_get_data (DBusServer *server,
726 dbus_mutex_lock (server->mutex);
729 res = _dbus_data_slot_list_get (&slot_allocator,
734 dbus_mutex_unlock (server->mutex);