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 * Adds a timeout for this server, chaining out to
150 * application-provided timeout handlers. The timeout should be
151 * repeatedly handled with dbus_timeout_handle() at its given interval
152 * until it is removed.
154 * @param server the server.
155 * @param timeout the timeout to add.
158 _dbus_server_add_timeout (DBusServer *server,
159 DBusTimeout *timeout)
161 return _dbus_timeout_list_add_timeout (server->timeouts, timeout);
165 * Removes a timeout previously added with _dbus_server_add_timeout().
167 * @param server the server.
168 * @param timeout the timeout to remove.
171 _dbus_server_remove_timeout (DBusServer *server,
172 DBusTimeout *timeout)
174 _dbus_timeout_list_remove_timeout (server->timeouts, timeout);
180 * @addtogroup DBusServer
187 * @typedef DBusServer
189 * An opaque object representing a server that listens for
190 * connections from other applications. Each time a connection
191 * is made, a new DBusConnection is created and made available
192 * via an application-provided DBusNewConnectionFunction.
193 * The DBusNewConnectionFunction is provided with
194 * dbus_server_set_new_connection_function().
199 * Listens for new connections on the given address.
200 * Returns #NULL if listening fails for any reason.
201 * Otherwise returns a new #DBusServer.
202 * dbus_server_set_new_connection_function() and
203 * dbus_server_set_watch_functions() should be called
204 * immediately to render the server fully functional.
206 * @todo error messages on bad address could really be better.
207 * DBusResultCode is a bit limiting here.
209 * @param address the address of this server.
210 * @param result location to store rationale for failure.
211 * @returns a new DBusServer, or #NULL on failure.
215 dbus_server_listen (const char *address,
216 DBusResultCode *result)
219 DBusAddressEntry **entries;
222 if (!dbus_parse_address (address, &entries, &len, result))
227 for (i = 0; i < len; i++)
229 const char *method = dbus_address_entry_get_method (entries[i]);
231 if (strcmp (method, "unix") == 0)
233 const char *path = dbus_address_entry_get_value (entries[i], "path");
238 server = _dbus_server_new_for_domain_socket (path, result);
243 else if (strcmp (method, "tcp") == 0)
245 const char *host = dbus_address_entry_get_value (entries[i], "host");
246 const char *port = dbus_address_entry_get_value (entries[i], "port");
254 _dbus_string_init_const (&str, port);
255 sresult = _dbus_string_parse_int (&str, 0, &lport, NULL);
256 _dbus_string_free (&str);
258 if (sresult == FALSE || lport <= 0 || lport > 65535)
261 server = _dbus_server_new_for_tcp_socket (host, lport, result);
266 #ifdef DBUS_BUILD_TESTS
267 else if (strcmp (method, "debug") == 0)
269 const char *name = dbus_address_entry_get_value (entries[i], "name");
274 server = _dbus_server_debug_new (name, result);
279 else if (strcmp (method, "debug-pipe") == 0)
281 const char *name = dbus_address_entry_get_value (entries[i], "name");
286 server = _dbus_server_debug_pipe_new (name, result);
296 dbus_address_entries_free (entries);
300 dbus_address_entries_free (entries);
301 dbus_set_result (result, DBUS_RESULT_BAD_ADDRESS);
307 * Increments the reference count of a DBusServer.
309 * @param server the server.
312 dbus_server_ref (DBusServer *server)
314 server->refcount += 1;
318 * Decrements the reference count of a DBusServer. Finalizes the
319 * server if the reference count reaches zero. The server connection
320 * will be closed as with dbus_server_disconnect() when the server is
323 * @param server the server.
326 dbus_server_unref (DBusServer *server)
328 _dbus_assert (server != NULL);
329 _dbus_assert (server->refcount > 0);
331 server->refcount -= 1;
332 if (server->refcount == 0)
334 _dbus_assert (server->vtable->finalize != NULL);
336 (* server->vtable->finalize) (server);
341 * Releases the server's address and stops listening for
342 * new clients. If called more than once, only the first
343 * call has an effect. Does not modify the server's
346 * @param server the server.
349 dbus_server_disconnect (DBusServer *server)
351 _dbus_assert (server->vtable->disconnect != NULL);
353 if (server->disconnected)
356 (* server->vtable->disconnect) (server);
357 server->disconnected = TRUE;
361 * Returns #TRUE if the server is still listening for new connections.
363 * @param server the server.
366 dbus_server_get_is_connected (DBusServer *server)
368 return !server->disconnected;
372 * Sets a function to be used for handling new connections. The given
373 * function is passed each new connection as the connection is
374 * created. If the new connection function increments the connection's
375 * reference count, the connection will stay alive. Otherwise, the
376 * connection will be unreferenced and closed.
378 * @param server the server.
379 * @param function a function to handle new connections.
380 * @param data data to pass to the new connection handler.
381 * @param free_data_function function to free the data.
384 dbus_server_set_new_connection_function (DBusServer *server,
385 DBusNewConnectionFunction function,
387 DBusFreeFunction free_data_function)
389 if (server->new_connection_free_data_function != NULL)
390 (* server->new_connection_free_data_function) (server->new_connection_data);
392 server->new_connection_function = function;
393 server->new_connection_data = data;
394 server->new_connection_free_data_function = free_data_function;
398 * Sets the watch functions for the connection. These functions are
399 * responsible for making the application's main loop aware of file
400 * descriptors that need to be monitored for events.
402 * This function behaves exactly like dbus_connection_set_watch_functions();
403 * see the documentation for that routine.
405 * @param server the server.
406 * @param add_function function to begin monitoring a new descriptor.
407 * @param remove_function function to stop monitoring a descriptor.
408 * @param data data to pass to add_function and remove_function.
409 * @param free_data_function function to be called to free the data.
410 * @returns #FALSE on failure (no memory)
413 dbus_server_set_watch_functions (DBusServer *server,
414 DBusAddWatchFunction add_function,
415 DBusRemoveWatchFunction remove_function,
417 DBusFreeFunction free_data_function)
419 return _dbus_watch_list_set_functions (server->watches,
427 * Sets the timeout functions for the connection. These functions are
428 * responsible for making the application's main loop aware of timeouts.
430 * This function behaves exactly like dbus_connection_set_timeout_functions();
431 * see the documentation for that routine.
433 * @param server the server.
434 * @param add_function function to add a timeout.
435 * @param remove_function function to remove a timeout.
436 * @param data data to pass to add_function and remove_function.
437 * @param free_data_function function to be called to free the data.
438 * @returns #FALSE on failure (no memory)
441 dbus_server_set_timeout_functions (DBusServer *server,
442 DBusAddTimeoutFunction add_function,
443 DBusRemoveTimeoutFunction remove_function,
445 DBusFreeFunction free_data_function)
447 return _dbus_timeout_list_set_functions (server->timeouts,
448 add_function, remove_function,
449 data, free_data_function);
453 * Called to notify the server when a previously-added watch
454 * is ready for reading or writing, or has an exception such
457 * @param server the server.
458 * @param watch the watch.
459 * @param condition the current condition of the file descriptors being watched.
462 dbus_server_handle_watch (DBusServer *server,
464 unsigned int condition)
466 _dbus_assert (server->vtable->handle_watch != NULL);
468 _dbus_watch_sanitize_condition (watch, &condition);
470 (* server->vtable->handle_watch) (server, watch, condition);
474 * Sets the maximum number of connections that can be open at one
475 * time for this server. If the maximum is reached, and another
476 * client tries to connect, then the oldest unauthenticated client
477 * will be dropped. If no unauthenticated client exists, then
478 * the new connection will be refused.
480 * If the maximum is set to a number lower than the current
481 * number of connections, no current connections are
484 * @todo honoring max_connections has not been implemented
485 * yet. The only real work involved is keeping a list
486 * of live connections on the DBusServer so the oldest
487 * unauthenticated client can be located when required.
489 * @todo for a systemwide daemon, we need a max number of connections
490 * per user, since any user can authenticate a bunch of connections
493 * @todo a single process might listen on multiple mechanisms
494 * (multiple DBusServer) and might want the max connections
495 * value to span all those servers. Should consider
496 * changing the API accordingly, though I'm inclined to
497 * punt this to the app that wants to do it instead of
498 * putting it in the library.
500 * @param server the server
501 * @param max_connections maximum number of connections allowed
504 dbus_server_set_max_connections (DBusServer *server,
507 server->max_connections = max_connections;
511 * Gets the maximum number of connections that can be active
512 * at a time for this server.
514 * @param server the server
515 * @returns maximum number of connections at once
518 dbus_server_get_max_connections (DBusServer *server)
520 return server->max_connections;
524 * Gets the number of #DBusConnection to this server that
525 * have not yet been finalized. i.e. all #DBusConnection that
526 * were passed to #DBusNewConnectionFunction and have not yet been
527 * finalized will count in this total.
529 * @param server the server
530 * @returns the number of connections
533 dbus_server_get_n_connections (DBusServer *server)
535 return _dbus_counter_get_value (server->connection_counter);
539 static DBusDataSlotAllocator slot_allocator;
542 * Initialize the mutex used for #DBusConnection data
548 _dbus_server_slots_init_lock (void)
550 if (!_dbus_data_slot_allocator_init (&slot_allocator))
553 return slot_allocator.lock;
557 * Allocates an integer ID to be used for storing application-specific
558 * data on any DBusServer. The allocated ID may then be used
559 * with dbus_server_set_data() and dbus_server_get_data().
560 * If allocation fails, -1 is returned. Again, the allocated
561 * slot is global, i.e. all DBusServer objects will
562 * have a slot with the given integer ID reserved.
564 * @returns -1 on failure, otherwise the data slot ID
567 dbus_server_allocate_data_slot (void)
569 return _dbus_data_slot_allocator_alloc (&slot_allocator);
573 * Deallocates a global ID for server data slots.
574 * dbus_server_get_data() and dbus_server_set_data()
575 * may no longer be used with this slot.
576 * Existing data stored on existing DBusServer objects
577 * will be freed when the server is finalized,
578 * but may not be retrieved (and may only be replaced
579 * if someone else reallocates the slot).
581 * @param slot the slot to deallocate
584 dbus_server_free_data_slot (int slot)
586 _dbus_data_slot_allocator_free (&slot_allocator, slot);
590 * Stores a pointer on a DBusServer, along
591 * with an optional function to be used for freeing
592 * the data when the data is set again, or when
593 * the server is finalized. The slot number
594 * must have been allocated with dbus_server_allocate_data_slot().
596 * @param server the server
597 * @param slot the slot number
598 * @param data the data to store
599 * @param free_data_func finalizer function for the data
600 * @returns #TRUE if there was enough memory to store the data
603 dbus_server_set_data (DBusServer *server,
606 DBusFreeFunction free_data_func)
608 DBusFreeFunction old_free_func;
613 dbus_mutex_lock (server->mutex);
616 retval = _dbus_data_slot_list_set (&slot_allocator,
618 slot, data, free_data_func,
619 &old_free_func, &old_data);
622 dbus_mutex_unlock (server->mutex);
627 /* Do the actual free outside the server lock */
629 (* old_free_func) (old_data);
636 * Retrieves data previously set with dbus_server_set_data().
637 * The slot must still be allocated (must not have been freed).
639 * @param server the server
640 * @param slot the slot to get data from
641 * @returns the data, or #NULL if not found
644 dbus_server_get_data (DBusServer *server,
650 dbus_mutex_lock (server->mutex);
653 res = _dbus_data_slot_list_get (&slot_allocator,
658 dbus_mutex_unlock (server->mutex);