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 result location to store rationale for failure.
250 * @returns a new DBusServer, or #NULL on failure.
254 dbus_server_listen (const char *address,
255 DBusResultCode *result)
258 DBusAddressEntry **entries;
261 if (!dbus_parse_address (address, &entries, &len, result))
266 for (i = 0; i < len; i++)
268 const char *method = dbus_address_entry_get_method (entries[i]);
270 if (strcmp (method, "unix") == 0)
272 const char *path = dbus_address_entry_get_value (entries[i], "path");
277 server = _dbus_server_new_for_domain_socket (path, result);
282 else if (strcmp (method, "tcp") == 0)
284 const char *host = dbus_address_entry_get_value (entries[i], "host");
285 const char *port = dbus_address_entry_get_value (entries[i], "port");
293 _dbus_string_init_const (&str, port);
294 sresult = _dbus_string_parse_int (&str, 0, &lport, NULL);
295 _dbus_string_free (&str);
297 if (sresult == FALSE || lport <= 0 || lport > 65535)
300 server = _dbus_server_new_for_tcp_socket (host, lport, result);
305 #ifdef DBUS_BUILD_TESTS
306 else if (strcmp (method, "debug") == 0)
308 const char *name = dbus_address_entry_get_value (entries[i], "name");
313 server = _dbus_server_debug_new (name, result);
318 else if (strcmp (method, "debug-pipe") == 0)
320 const char *name = dbus_address_entry_get_value (entries[i], "name");
325 server = _dbus_server_debug_pipe_new (name, result);
335 dbus_address_entries_free (entries);
339 dbus_address_entries_free (entries);
340 dbus_set_result (result, DBUS_RESULT_BAD_ADDRESS);
346 * Increments the reference count of a DBusServer.
348 * @param server the server.
351 dbus_server_ref (DBusServer *server)
353 server->refcount += 1;
357 * Decrements the reference count of a DBusServer. Finalizes the
358 * server if the reference count reaches zero. The server connection
359 * will be closed as with dbus_server_disconnect() when the server is
362 * @param server the server.
365 dbus_server_unref (DBusServer *server)
367 _dbus_assert (server != NULL);
368 _dbus_assert (server->refcount > 0);
370 server->refcount -= 1;
371 if (server->refcount == 0)
373 _dbus_assert (server->vtable->finalize != NULL);
375 (* server->vtable->finalize) (server);
380 * Releases the server's address and stops listening for
381 * new clients. If called more than once, only the first
382 * call has an effect. Does not modify the server's
385 * @param server the server.
388 dbus_server_disconnect (DBusServer *server)
390 _dbus_assert (server->vtable->disconnect != NULL);
392 if (server->disconnected)
395 (* server->vtable->disconnect) (server);
396 server->disconnected = TRUE;
400 * Returns #TRUE if the server is still listening for new connections.
402 * @param server the server.
405 dbus_server_get_is_connected (DBusServer *server)
407 return !server->disconnected;
411 * Sets a function to be used for handling new connections. The given
412 * function is passed each new connection as the connection is
413 * created. If the new connection function increments the connection's
414 * reference count, the connection will stay alive. Otherwise, the
415 * connection will be unreferenced and closed.
417 * @param server the server.
418 * @param function a function to handle new connections.
419 * @param data data to pass to the new connection handler.
420 * @param free_data_function function to free the data.
423 dbus_server_set_new_connection_function (DBusServer *server,
424 DBusNewConnectionFunction function,
426 DBusFreeFunction free_data_function)
428 if (server->new_connection_free_data_function != NULL)
429 (* server->new_connection_free_data_function) (server->new_connection_data);
431 server->new_connection_function = function;
432 server->new_connection_data = data;
433 server->new_connection_free_data_function = free_data_function;
437 * Sets the watch functions for the connection. These functions are
438 * responsible for making the application's main loop aware of file
439 * descriptors that need to be monitored for events.
441 * This function behaves exactly like dbus_connection_set_watch_functions();
442 * see the documentation for that routine.
444 * @param server the server.
445 * @param add_function function to begin monitoring a new descriptor.
446 * @param remove_function function to stop monitoring a descriptor.
447 * @param toggled_function function to notify when the watch is enabled/disabled
448 * @param data data to pass to add_function and remove_function.
449 * @param free_data_function function to be called to free the data.
450 * @returns #FALSE on failure (no memory)
453 dbus_server_set_watch_functions (DBusServer *server,
454 DBusAddWatchFunction add_function,
455 DBusRemoveWatchFunction remove_function,
456 DBusWatchToggledFunction toggled_function,
458 DBusFreeFunction free_data_function)
460 return _dbus_watch_list_set_functions (server->watches,
469 * Sets the timeout functions for the connection. These functions are
470 * responsible for making the application's main loop aware of timeouts.
472 * This function behaves exactly like dbus_connection_set_timeout_functions();
473 * see the documentation for that routine.
475 * @param server the server.
476 * @param add_function function to add a timeout.
477 * @param remove_function function to remove a timeout.
478 * @param toggled_function function to notify when the timeout is enabled/disabled
479 * @param data data to pass to add_function and remove_function.
480 * @param free_data_function function to be called to free the data.
481 * @returns #FALSE on failure (no memory)
484 dbus_server_set_timeout_functions (DBusServer *server,
485 DBusAddTimeoutFunction add_function,
486 DBusRemoveTimeoutFunction remove_function,
487 DBusTimeoutToggledFunction toggled_function,
489 DBusFreeFunction free_data_function)
491 return _dbus_timeout_list_set_functions (server->timeouts,
492 add_function, remove_function,
494 data, free_data_function);
498 * Called to notify the server when a previously-added watch
499 * is ready for reading or writing, or has an exception such
502 * @param server the server.
503 * @param watch the watch.
504 * @param condition the current condition of the file descriptors being watched.
507 dbus_server_handle_watch (DBusServer *server,
509 unsigned int condition)
511 _dbus_assert (server->vtable->handle_watch != NULL);
513 _dbus_watch_sanitize_condition (watch, &condition);
515 (* server->vtable->handle_watch) (server, watch, condition);
519 * Sets the maximum number of connections that can be open at one
520 * time for this server. If the maximum is reached, and another
521 * client tries to connect, then the oldest unauthenticated client
522 * will be dropped. If no unauthenticated client exists, then
523 * the new connection will be refused.
525 * If the maximum is set to a number lower than the current
526 * number of connections, no current connections are
529 * @todo honoring max_connections has not been implemented
530 * yet. The only real work involved is keeping a list
531 * of live connections on the DBusServer so the oldest
532 * unauthenticated client can be located when required.
534 * @todo for a systemwide daemon, we need a max number of connections
535 * per user, since any user can authenticate a bunch of connections
538 * @todo a single process might listen on multiple mechanisms
539 * (multiple DBusServer) and might want the max connections
540 * value to span all those servers. Should consider
541 * changing the API accordingly, though I'm inclined to
542 * punt this to the app that wants to do it instead of
543 * putting it in the library.
545 * @param server the server
546 * @param max_connections maximum number of connections allowed
549 dbus_server_set_max_connections (DBusServer *server,
552 server->max_connections = max_connections;
556 * Gets the maximum number of connections that can be active
557 * at a time for this server.
559 * @param server the server
560 * @returns maximum number of connections at once
563 dbus_server_get_max_connections (DBusServer *server)
565 return server->max_connections;
569 * Gets the number of #DBusConnection to this server that
570 * have not yet been finalized. i.e. all #DBusConnection that
571 * were passed to #DBusNewConnectionFunction and have not yet been
572 * finalized will count in this total.
574 * @param server the server
575 * @returns the number of connections
578 dbus_server_get_n_connections (DBusServer *server)
580 return _dbus_counter_get_value (server->connection_counter);
584 static DBusDataSlotAllocator slot_allocator;
587 * Initialize the mutex used for #DBusConnection data
593 _dbus_server_slots_init_lock (void)
595 if (!_dbus_data_slot_allocator_init (&slot_allocator))
598 return slot_allocator.lock;
602 * Allocates an integer ID to be used for storing application-specific
603 * data on any DBusServer. The allocated ID may then be used
604 * with dbus_server_set_data() and dbus_server_get_data().
605 * If allocation fails, -1 is returned. Again, the allocated
606 * slot is global, i.e. all DBusServer objects will
607 * have a slot with the given integer ID reserved.
609 * @returns -1 on failure, otherwise the data slot ID
612 dbus_server_allocate_data_slot (void)
614 return _dbus_data_slot_allocator_alloc (&slot_allocator);
618 * Deallocates a global ID for server data slots.
619 * dbus_server_get_data() and dbus_server_set_data()
620 * may no longer be used with this slot.
621 * Existing data stored on existing DBusServer objects
622 * will be freed when the server is finalized,
623 * but may not be retrieved (and may only be replaced
624 * if someone else reallocates the slot).
626 * @param slot the slot to deallocate
629 dbus_server_free_data_slot (int slot)
631 _dbus_data_slot_allocator_free (&slot_allocator, slot);
635 * Stores a pointer on a DBusServer, along
636 * with an optional function to be used for freeing
637 * the data when the data is set again, or when
638 * the server is finalized. The slot number
639 * must have been allocated with dbus_server_allocate_data_slot().
641 * @param server the server
642 * @param slot the slot number
643 * @param data the data to store
644 * @param free_data_func finalizer function for the data
645 * @returns #TRUE if there was enough memory to store the data
648 dbus_server_set_data (DBusServer *server,
651 DBusFreeFunction free_data_func)
653 DBusFreeFunction old_free_func;
658 dbus_mutex_lock (server->mutex);
661 retval = _dbus_data_slot_list_set (&slot_allocator,
663 slot, data, free_data_func,
664 &old_free_func, &old_data);
667 dbus_mutex_unlock (server->mutex);
672 /* Do the actual free outside the server lock */
674 (* old_free_func) (old_data);
681 * Retrieves data previously set with dbus_server_set_data().
682 * The slot must still be allocated (must not have been freed).
684 * @param server the server
685 * @param slot the slot to get data from
686 * @returns the data, or #NULL if not found
689 dbus_server_get_data (DBusServer *server,
695 dbus_mutex_lock (server->mutex);
698 res = _dbus_data_slot_list_get (&slot_allocator,
703 dbus_mutex_unlock (server->mutex);