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
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.
397 dbus_server_set_watch_functions (DBusServer *server,
398 DBusAddWatchFunction add_function,
399 DBusRemoveWatchFunction remove_function,
401 DBusFreeFunction free_data_function)
403 _dbus_watch_list_set_functions (server->watches,
411 * Sets the timeout functions for the connection. These functions are
412 * responsible for making the application's main loop aware of timeouts.
414 * This function behaves exactly like dbus_connection_set_timeout_functions();
415 * see the documentation for that routine.
417 * @param server the server.
418 * @param add_function function to add a timeout.
419 * @param remove_function function to remove a timeout.
420 * @param data data to pass to add_function and remove_function.
421 * @param free_data_function function to be called to free the data.
424 dbus_server_set_timeout_functions (DBusServer *server,
425 DBusAddTimeoutFunction add_function,
426 DBusRemoveTimeoutFunction remove_function,
428 DBusFreeFunction free_data_function)
430 _dbus_timeout_list_set_functions (server->timeouts,
431 add_function, remove_function,
432 data, free_data_function);
436 * Called to notify the server when a previously-added watch
437 * is ready for reading or writing, or has an exception such
440 * @param server the server.
441 * @param watch the watch.
442 * @param condition the current condition of the file descriptors being watched.
445 dbus_server_handle_watch (DBusServer *server,
447 unsigned int condition)
449 _dbus_assert (server->vtable->handle_watch != NULL);
451 _dbus_watch_sanitize_condition (watch, &condition);
453 (* server->vtable->handle_watch) (server, watch, condition);
457 * Sets the maximum number of connections that can be open at one
458 * time for this server. If the maximum is reached, and another
459 * client tries to connect, then the oldest unauthenticated client
460 * will be dropped. If no unauthenticated client exists, then
461 * the new connection will be refused.
463 * If the maximum is set to a number lower than the current
464 * number of connections, no current connections are
467 * @todo honoring max_connections has not been implemented
468 * yet. The only real work involved is keeping a list
469 * of live connections on the DBusServer so the oldest
470 * unauthenticated client can be located when required.
472 * @todo for a systemwide daemon, we need a max number of connections
473 * per user, since any user can authenticate a bunch of connections
476 * @todo a single process might listen on multiple mechanisms
477 * (multiple DBusServer) and might want the max connections
478 * value to span all those servers. Should consider
479 * changing the API accordingly, though I'm inclined to
480 * punt this to the app that wants to do it instead of
481 * putting it in the library.
483 * @param server the server
484 * @param max_connections maximum number of connections allowed
487 dbus_server_set_max_connections (DBusServer *server,
490 server->max_connections = max_connections;
494 * Gets the maximum number of connections that can be active
495 * at a time for this server.
497 * @param server the server
498 * @returns maximum number of connections at once
501 dbus_server_get_max_connections (DBusServer *server)
503 return server->max_connections;
507 * Gets the number of #DBusConnection to this server that
508 * have not yet been finalized. i.e. all #DBusConnection that
509 * were passed to #DBusNewConnectionFunction and have not yet been
510 * finalized will count in this total.
512 * @param server the server
513 * @returns the number of connections
516 dbus_server_get_n_connections (DBusServer *server)
518 return _dbus_counter_get_value (server->connection_counter);
522 static DBusDataSlotAllocator slot_allocator;
525 * Initialize the mutex used for #DBusConnection data
531 _dbus_server_slots_init_lock (void)
533 if (!_dbus_data_slot_allocator_init (&slot_allocator))
536 return slot_allocator.lock;
540 * Allocates an integer ID to be used for storing application-specific
541 * data on any DBusServer. The allocated ID may then be used
542 * with dbus_server_set_data() and dbus_server_get_data().
543 * If allocation fails, -1 is returned. Again, the allocated
544 * slot is global, i.e. all DBusServer objects will
545 * have a slot with the given integer ID reserved.
547 * @returns -1 on failure, otherwise the data slot ID
550 dbus_server_allocate_data_slot (void)
552 return _dbus_data_slot_allocator_alloc (&slot_allocator);
556 * Deallocates a global ID for server data slots.
557 * dbus_server_get_data() and dbus_server_set_data()
558 * may no longer be used with this slot.
559 * Existing data stored on existing DBusServer objects
560 * will be freed when the server is finalized,
561 * but may not be retrieved (and may only be replaced
562 * if someone else reallocates the slot).
564 * @param slot the slot to deallocate
567 dbus_server_free_data_slot (int slot)
569 _dbus_data_slot_allocator_free (&slot_allocator, slot);
573 * Stores a pointer on a DBusServer, along
574 * with an optional function to be used for freeing
575 * the data when the data is set again, or when
576 * the server is finalized. The slot number
577 * must have been allocated with dbus_server_allocate_data_slot().
579 * @param server the server
580 * @param slot the slot number
581 * @param data the data to store
582 * @param free_data_func finalizer function for the data
583 * @returns #TRUE if there was enough memory to store the data
586 dbus_server_set_data (DBusServer *server,
589 DBusFreeFunction free_data_func)
591 DBusFreeFunction old_free_func;
596 dbus_mutex_lock (server->mutex);
599 retval = _dbus_data_slot_list_set (&slot_allocator,
601 slot, data, free_data_func,
602 &old_free_func, &old_data);
605 dbus_mutex_unlock (server->mutex);
610 /* Do the actual free outside the server lock */
612 (* old_free_func) (old_data);
619 * Retrieves data previously set with dbus_server_set_data().
620 * The slot must still be allocated (must not have been freed).
622 * @param server the server
623 * @param slot the slot to get data from
624 * @returns the data, or #NULL if not found
627 dbus_server_get_data (DBusServer *server,
633 dbus_mutex_lock (server->mutex);
636 res = _dbus_data_slot_list_get (&slot_allocator,
641 dbus_mutex_unlock (server->mutex);