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"
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 * @returns #TRUE on success.
62 _dbus_server_init_base (DBusServer *server,
63 const DBusServerVTable *vtable)
65 server->vtable = vtable;
68 server->watches = _dbus_watch_list_new ();
69 if (server->watches == NULL)
72 server->timeouts = _dbus_timeout_list_new ();
73 if (server->timeouts == NULL)
75 _dbus_watch_list_free (server->watches);
76 server->watches = NULL;
80 server->connection_counter = _dbus_counter_new ();
81 if (server->connection_counter == NULL)
83 _dbus_watch_list_free (server->watches);
84 server->watches = NULL;
85 _dbus_timeout_list_free (server->timeouts);
86 server->timeouts = NULL;
91 server->max_connections = 256; /* same as an X server, seems like a nice default */
93 _dbus_data_slot_list_init (&server->slot_list);
99 * Finalizes the members of the DBusServer base class.
100 * Chained up to by subclass finalizers.
102 * @param server the server.
105 _dbus_server_finalize_base (DBusServer *server)
107 /* calls out to application code... */
108 _dbus_data_slot_list_free (&server->slot_list);
110 dbus_server_set_new_connection_function (server, NULL, NULL, NULL);
112 if (!server->disconnected)
113 dbus_server_disconnect (server);
115 _dbus_watch_list_free (server->watches);
116 _dbus_timeout_list_free (server->timeouts);
117 _dbus_counter_unref (server->connection_counter);
121 * Adds a watch for this server, chaining out to application-provided
124 * @param server the server.
125 * @param watch the watch to add.
128 _dbus_server_add_watch (DBusServer *server,
131 return _dbus_watch_list_add_watch (server->watches, watch);
135 * Removes a watch previously added with _dbus_server_remove_watch().
137 * @param server the server.
138 * @param watch the watch to remove.
141 _dbus_server_remove_watch (DBusServer *server,
144 _dbus_watch_list_remove_watch (server->watches, watch);
148 * Adds a timeout for this server, chaining out to application-provided
149 * timeout handlers. The timeout will fire only one time.
151 * @param server the server.
152 * @param timeout the timeout to add.
155 _dbus_server_add_timeout (DBusServer *server,
156 DBusTimeout *timeout)
158 return _dbus_timeout_list_add_timeout (server->timeouts, timeout);
162 * Removes a timeout previously added with _dbus_server_add_timeout().
164 * @param server the server.
165 * @param timeout the timeout to remove.
168 _dbus_server_remove_timeout (DBusServer *server,
169 DBusTimeout *timeout)
171 _dbus_timeout_list_remove_timeout (server->timeouts, timeout);
177 * @addtogroup DBusServer
184 * @typedef DBusServer
186 * An opaque object representing a server that listens for
187 * connections from other applications. Each time a connection
188 * is made, a new DBusConnection is created and made available
189 * via an application-provided DBusNewConnectionFunction.
190 * The DBusNewConnectionFunction is provided with
191 * dbus_server_set_new_connection_function().
196 * Listens for new connections on the given address.
197 * Returns #NULL if listening fails for any reason.
198 * Otherwise returns a new #DBusServer.
199 * dbus_server_set_new_connection_function() and
200 * dbus_server_set_watch_functions() should be called
201 * immediately to render the server fully functional.
203 * @todo error messages on bad address could really be better.
204 * DBusResultCode is a bit limiting here.
206 * @param address the address of this server.
207 * @param result location to store rationale for failure.
208 * @returns a new DBusServer, or #NULL on failure.
212 dbus_server_listen (const char *address,
213 DBusResultCode *result)
216 DBusAddressEntry **entries;
219 if (!dbus_parse_address (address, &entries, &len, result))
224 for (i = 0; i < len; i++)
226 const char *method = dbus_address_entry_get_method (entries[i]);
228 if (strcmp (method, "unix") == 0)
230 const char *path = dbus_address_entry_get_value (entries[i], "path");
235 server = _dbus_server_new_for_domain_socket (path, result);
240 else if (strcmp (method, "tcp") == 0)
242 const char *host = dbus_address_entry_get_value (entries[i], "host");
243 const char *port = dbus_address_entry_get_value (entries[i], "port");
251 _dbus_string_init_const (&str, port);
252 sresult = _dbus_string_parse_int (&str, 0, &lport, NULL);
253 _dbus_string_free (&str);
255 if (sresult == FALSE || lport <= 0 || lport > 65535)
258 server = _dbus_server_new_for_tcp_socket (host, lport, result);
263 #ifdef DBUS_BUILD_TESTS
264 else if (strcmp (method, "debug") == 0)
266 const char *name = dbus_address_entry_get_value (entries[i], "name");
271 server = _dbus_server_debug_new (name, result);
281 dbus_address_entries_free (entries);
285 dbus_address_entries_free (entries);
286 dbus_set_result (result, DBUS_RESULT_BAD_ADDRESS);
292 * Increments the reference count of a DBusServer.
294 * @param server the server.
297 dbus_server_ref (DBusServer *server)
299 server->refcount += 1;
303 * Decrements the reference count of a DBusServer. Finalizes the
304 * server if the reference count reaches zero. The server connection
305 * will be closed as with dbus_server_disconnect() when the server is
308 * @param server the server.
311 dbus_server_unref (DBusServer *server)
313 _dbus_assert (server != NULL);
314 _dbus_assert (server->refcount > 0);
316 server->refcount -= 1;
317 if (server->refcount == 0)
319 _dbus_assert (server->vtable->finalize != NULL);
321 (* server->vtable->finalize) (server);
326 * Releases the server's address and stops listening for
327 * new clients. If called more than once, only the first
328 * call has an effect. Does not modify the server's
331 * @param server the server.
334 dbus_server_disconnect (DBusServer *server)
336 _dbus_assert (server->vtable->disconnect != NULL);
338 if (server->disconnected)
341 (* server->vtable->disconnect) (server);
342 server->disconnected = TRUE;
346 * Returns #TRUE if the server is still listening for new connections.
348 * @param server the server.
351 dbus_server_get_is_connected (DBusServer *server)
353 return !server->disconnected;
357 * Sets a function to be used for handling new connections. The given
358 * function is passed each new connection as the connection is
359 * created. If the new connection function increments the connection's
360 * reference count, the connection will stay alive. Otherwise, the
361 * connection will be unreferenced and closed.
363 * @param server the server.
364 * @param function a function to handle new connections.
365 * @param data data to pass to the new connection handler.
366 * @param free_data_function function to free the data.
369 dbus_server_set_new_connection_function (DBusServer *server,
370 DBusNewConnectionFunction function,
372 DBusFreeFunction free_data_function)
374 if (server->new_connection_free_data_function != NULL)
375 (* server->new_connection_free_data_function) (server->new_connection_data);
377 server->new_connection_function = function;
378 server->new_connection_data = data;
379 server->new_connection_free_data_function = free_data_function;
383 * Sets the watch functions for the connection. These functions are
384 * responsible for making the application's main loop aware of file
385 * descriptors that need to be monitored for events.
387 * This function behaves exactly like dbus_connection_set_watch_functions();
388 * see the documentation for that routine.
390 * @param server the server.
391 * @param add_function function to begin monitoring a new descriptor.
392 * @param remove_function function to stop monitoring a descriptor.
393 * @param data data to pass to add_function and remove_function.
394 * @param free_data_function function to be called to free the data.
395 * @returns #FALSE on failure (no memory)
398 dbus_server_set_watch_functions (DBusServer *server,
399 DBusAddWatchFunction add_function,
400 DBusRemoveWatchFunction remove_function,
402 DBusFreeFunction free_data_function)
404 return _dbus_watch_list_set_functions (server->watches,
412 * Sets the timeout functions for the connection. These functions are
413 * responsible for making the application's main loop aware of timeouts.
415 * This function behaves exactly like dbus_connection_set_timeout_functions();
416 * see the documentation for that routine.
418 * @param server the server.
419 * @param add_function function to add a timeout.
420 * @param remove_function function to remove a timeout.
421 * @param data data to pass to add_function and remove_function.
422 * @param free_data_function function to be called to free the data.
423 * @returns #FALSE on failure (no memory)
426 dbus_server_set_timeout_functions (DBusServer *server,
427 DBusAddTimeoutFunction add_function,
428 DBusRemoveTimeoutFunction remove_function,
430 DBusFreeFunction free_data_function)
432 return _dbus_timeout_list_set_functions (server->timeouts,
433 add_function, remove_function,
434 data, free_data_function);
438 * Called to notify the server when a previously-added watch
439 * is ready for reading or writing, or has an exception such
442 * @param server the server.
443 * @param watch the watch.
444 * @param condition the current condition of the file descriptors being watched.
447 dbus_server_handle_watch (DBusServer *server,
449 unsigned int condition)
451 _dbus_assert (server->vtable->handle_watch != NULL);
453 _dbus_watch_sanitize_condition (watch, &condition);
455 (* server->vtable->handle_watch) (server, watch, condition);
459 * Sets the maximum number of connections that can be open at one
460 * time for this server. If the maximum is reached, and another
461 * client tries to connect, then the oldest unauthenticated client
462 * will be dropped. If no unauthenticated client exists, then
463 * the new connection will be refused.
465 * If the maximum is set to a number lower than the current
466 * number of connections, no current connections are
469 * @todo honoring max_connections has not been implemented
470 * yet. The only real work involved is keeping a list
471 * of live connections on the DBusServer so the oldest
472 * unauthenticated client can be located when required.
474 * @todo for a systemwide daemon, we need a max number of connections
475 * per user, since any user can authenticate a bunch of connections
478 * @todo a single process might listen on multiple mechanisms
479 * (multiple DBusServer) and might want the max connections
480 * value to span all those servers. Should consider
481 * changing the API accordingly, though I'm inclined to
482 * punt this to the app that wants to do it instead of
483 * putting it in the library.
485 * @param server the server
486 * @param max_connections maximum number of connections allowed
489 dbus_server_set_max_connections (DBusServer *server,
492 server->max_connections = max_connections;
496 * Gets the maximum number of connections that can be active
497 * at a time for this server.
499 * @param server the server
500 * @returns maximum number of connections at once
503 dbus_server_get_max_connections (DBusServer *server)
505 return server->max_connections;
509 * Gets the number of #DBusConnection to this server that
510 * have not yet been finalized. i.e. all #DBusConnection that
511 * were passed to #DBusNewConnectionFunction and have not yet been
512 * finalized will count in this total.
514 * @param server the server
515 * @returns the number of connections
518 dbus_server_get_n_connections (DBusServer *server)
520 return _dbus_counter_get_value (server->connection_counter);
524 static DBusDataSlotAllocator slot_allocator;
527 * Initialize the mutex used for #DBusConnection data
533 _dbus_server_slots_init_lock (void)
535 if (!_dbus_data_slot_allocator_init (&slot_allocator))
538 return slot_allocator.lock;
542 * Allocates an integer ID to be used for storing application-specific
543 * data on any DBusServer. The allocated ID may then be used
544 * with dbus_server_set_data() and dbus_server_get_data().
545 * If allocation fails, -1 is returned. Again, the allocated
546 * slot is global, i.e. all DBusServer objects will
547 * have a slot with the given integer ID reserved.
549 * @returns -1 on failure, otherwise the data slot ID
552 dbus_server_allocate_data_slot (void)
554 return _dbus_data_slot_allocator_alloc (&slot_allocator);
558 * Deallocates a global ID for server data slots.
559 * dbus_server_get_data() and dbus_server_set_data()
560 * may no longer be used with this slot.
561 * Existing data stored on existing DBusServer objects
562 * will be freed when the server is finalized,
563 * but may not be retrieved (and may only be replaced
564 * if someone else reallocates the slot).
566 * @param slot the slot to deallocate
569 dbus_server_free_data_slot (int slot)
571 _dbus_data_slot_allocator_free (&slot_allocator, slot);
575 * Stores a pointer on a DBusServer, along
576 * with an optional function to be used for freeing
577 * the data when the data is set again, or when
578 * the server is finalized. The slot number
579 * must have been allocated with dbus_server_allocate_data_slot().
581 * @param server the server
582 * @param slot the slot number
583 * @param data the data to store
584 * @param free_data_func finalizer function for the data
585 * @returns #TRUE if there was enough memory to store the data
588 dbus_server_set_data (DBusServer *server,
591 DBusFreeFunction free_data_func)
593 DBusFreeFunction old_free_func;
598 dbus_mutex_lock (server->mutex);
601 retval = _dbus_data_slot_list_set (&slot_allocator,
603 slot, data, free_data_func,
604 &old_free_func, &old_data);
607 dbus_mutex_unlock (server->mutex);
612 /* Do the actual free outside the server lock */
614 (* old_free_func) (old_data);
621 * Retrieves data previously set with dbus_server_set_data().
622 * The slot must still be allocated (must not have been freed).
624 * @param server the server
625 * @param slot the slot to get data from
626 * @returns the data, or #NULL if not found
629 dbus_server_get_data (DBusServer *server,
635 dbus_mutex_lock (server->mutex);
638 res = _dbus_data_slot_list_get (&slot_allocator,
643 dbus_mutex_unlock (server->mutex);