1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-server.c DBusServer object
4 * Copyright (C) 2002 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.
43 * @defgroup DBusServerInternals DBusServer implementation details
44 * @ingroup DBusInternals
45 * @brief Implementation details of DBusServer
51 * Initializes the members of the DBusServer base class.
52 * Chained up to by subclass constructors.
54 * @param server the server.
55 * @param vtable the vtable for the subclass.
56 * @returns #TRUE on success.
59 _dbus_server_init_base (DBusServer *server,
60 const DBusServerVTable *vtable)
62 server->vtable = vtable;
65 server->watches = _dbus_watch_list_new ();
66 if (server->watches == NULL)
69 server->timeouts = _dbus_timeout_list_new ();
70 if (server->timeouts == NULL)
72 _dbus_watch_list_free (server->watches);
73 server->watches = NULL;
77 server->connection_counter = _dbus_counter_new ();
78 if (server->connection_counter == NULL)
80 _dbus_watch_list_free (server->watches);
81 server->watches = NULL;
82 _dbus_timeout_list_free (server->timeouts);
83 server->timeouts = NULL;
88 server->max_connections = 256; /* same as an X server, seems like a nice default */
94 * Finalizes the members of the DBusServer base class.
95 * Chained up to by subclass finalizers.
97 * @param server the server.
100 _dbus_server_finalize_base (DBusServer *server)
102 dbus_server_set_new_connection_function (server, NULL, NULL, NULL);
104 if (!server->disconnected)
105 dbus_server_disconnect (server);
107 _dbus_watch_list_free (server->watches);
108 _dbus_timeout_list_free (server->timeouts);
109 _dbus_counter_unref (server->connection_counter);
113 * Adds a watch for this server, chaining out to application-provided
116 * @param server the server.
117 * @param watch the watch to add.
120 _dbus_server_add_watch (DBusServer *server,
123 return _dbus_watch_list_add_watch (server->watches, watch);
127 * Removes a watch previously added with _dbus_server_remove_watch().
129 * @param server the server.
130 * @param watch the watch to remove.
133 _dbus_server_remove_watch (DBusServer *server,
136 _dbus_watch_list_remove_watch (server->watches, watch);
140 * Adds a timeout for this server, chaining out to application-provided
143 * @param server the server.
144 * @param timeout the timeout to add.
147 _dbus_server_add_timeout (DBusServer *server,
148 DBusTimeout *timeout)
150 return _dbus_timeout_list_add_timeout (server->timeouts, timeout);
154 * Removes a timeout previously added with _dbus_server_add_timeout().
156 * @param server the server.
157 * @param timeout the timeout to remove.
160 _dbus_server_remove_timeout (DBusServer *server,
161 DBusTimeout *timeout)
163 _dbus_timeout_list_remove_timeout (server->timeouts, timeout);
169 * @addtogroup DBusServer
176 * @typedef DBusServer
178 * An opaque object representing a server that listens for
179 * connections from other applications. Each time a connection
180 * is made, a new DBusConnection is created and made available
181 * via an application-provided DBusNewConnectionFunction.
182 * The DBusNewConnectionFunction is provided with
183 * dbus_server_set_new_connection_function().
188 * Listens for new connections on the given address.
189 * Returns #NULL if listening fails for any reason.
190 * Otherwise returns a new #DBusServer.
191 * dbus_server_set_new_connection_function() and
192 * dbus_server_set_watch_functions() should be called
193 * immediately to render the server fully functional.
195 * @todo error messages on bad address could really be better.
196 * DBusResultCode is a bit limiting here.
198 * @param address the address of this server.
199 * @param result location to store rationale for failure.
200 * @returns a new DBusServer, or #NULL on failure.
204 dbus_server_listen (const char *address,
205 DBusResultCode *result)
208 DBusAddressEntry **entries;
211 if (!dbus_parse_address (address, &entries, &len, result))
216 for (i = 0; i < len; i++)
218 const char *method = dbus_address_entry_get_method (entries[i]);
220 if (strcmp (method, "unix") == 0)
222 const char *path = dbus_address_entry_get_value (entries[i], "path");
227 server = _dbus_server_new_for_domain_socket (path, result);
232 else if (strcmp (method, "tcp") == 0)
234 const char *host = dbus_address_entry_get_value (entries[i], "host");
235 const char *port = dbus_address_entry_get_value (entries[i], "port");
243 _dbus_string_init_const (&str, port);
244 sresult = _dbus_string_parse_int (&str, 0, &lport, NULL);
245 _dbus_string_free (&str);
247 if (sresult == FALSE || lport <= 0 || lport > 65535)
250 server = _dbus_server_new_for_tcp_socket (host, lport, result);
255 #ifdef DBUS_BUILD_TESTS
256 else if (strcmp (method, "debug") == 0)
258 const char *name = dbus_address_entry_get_value (entries[i], "name");
263 server = _dbus_server_debug_new (name, result);
273 dbus_address_entries_free (entries);
277 dbus_address_entries_free (entries);
278 dbus_set_result (result, DBUS_RESULT_BAD_ADDRESS);
284 * Increments the reference count of a DBusServer.
286 * @param server the server.
289 dbus_server_ref (DBusServer *server)
291 server->refcount += 1;
295 * Decrements the reference count of a DBusServer. Finalizes the
296 * server if the reference count reaches zero. The server connection
297 * will be closed as with dbus_server_disconnect() when the server is
300 * @param server the server.
303 dbus_server_unref (DBusServer *server)
305 _dbus_assert (server != NULL);
306 _dbus_assert (server->refcount > 0);
308 server->refcount -= 1;
309 if (server->refcount == 0)
311 _dbus_assert (server->vtable->finalize != NULL);
313 (* server->vtable->finalize) (server);
318 * Releases the server's address and stops listening for
319 * new clients. If called more than once, only the first
320 * call has an effect. Does not modify the server's
323 * @param server the server.
326 dbus_server_disconnect (DBusServer *server)
328 _dbus_assert (server->vtable->disconnect != NULL);
330 if (server->disconnected)
333 (* server->vtable->disconnect) (server);
334 server->disconnected = TRUE;
338 * Returns #TRUE if the server is still listening for new connections.
340 * @param server the server.
343 dbus_server_get_is_connected (DBusServer *server)
345 return !server->disconnected;
349 * Sets a function to be used for handling new connections. The given
350 * function is passed each new connection as the connection is
351 * created. If the new connection function increments the connection's
352 * reference count, the connection will stay alive. Otherwise, the
353 * connection will be unreferenced and closed.
355 * @param server the server.
356 * @param function a function to handle new connections.
357 * @param data data to pass to the new connection handler.
358 * @param free_data_function function to free the data.
361 dbus_server_set_new_connection_function (DBusServer *server,
362 DBusNewConnectionFunction function,
364 DBusFreeFunction free_data_function)
366 if (server->new_connection_free_data_function != NULL)
367 (* server->new_connection_free_data_function) (server->new_connection_data);
369 server->new_connection_function = function;
370 server->new_connection_data = data;
371 server->new_connection_free_data_function = free_data_function;
375 * Sets the watch functions for the connection. These functions are
376 * responsible for making the application's main loop aware of file
377 * descriptors that need to be monitored for events.
379 * This function behaves exactly like dbus_connection_set_watch_functions();
380 * see the documentation for that routine.
382 * @param server the server.
383 * @param add_function function to begin monitoring a new descriptor.
384 * @param remove_function function to stop monitoring a descriptor.
385 * @param data data to pass to add_function and remove_function.
386 * @param free_data_function function to be called to free the data.
389 dbus_server_set_watch_functions (DBusServer *server,
390 DBusAddWatchFunction add_function,
391 DBusRemoveWatchFunction remove_function,
393 DBusFreeFunction free_data_function)
395 _dbus_watch_list_set_functions (server->watches,
403 * Sets the timeout functions for the connection. These functions are
404 * responsible for making the application's main loop aware of timeouts.
406 * This function behaves exactly like dbus_connection_set_timeout_functions();
407 * see the documentation for that routine.
409 * @param server the server.
410 * @param add_function function to add a timeout.
411 * @param remove_function function to remove a timeout.
412 * @param data data to pass to add_function and remove_function.
413 * @param free_data_function function to be called to free the data.
416 dbus_server_set_timeout_functions (DBusServer *server,
417 DBusAddTimeoutFunction add_function,
418 DBusRemoveTimeoutFunction remove_function,
420 DBusFreeFunction free_data_function)
422 _dbus_timeout_list_set_functions (server->timeouts,
423 add_function, remove_function,
424 data, free_data_function);
428 * Called to notify the server when a previously-added watch
429 * is ready for reading or writing, or has an exception such
432 * @param server the server.
433 * @param watch the watch.
434 * @param condition the current condition of the file descriptors being watched.
437 dbus_server_handle_watch (DBusServer *server,
439 unsigned int condition)
441 _dbus_assert (server->vtable->handle_watch != NULL);
443 _dbus_watch_sanitize_condition (watch, &condition);
445 (* server->vtable->handle_watch) (server, watch, condition);
449 * Sets the maximum number of connections that can be open at one
450 * time for this server. If the maximum is reached, and another
451 * client tries to connect, then the oldest unauthenticated client
452 * will be dropped. If no unauthenticated client exists, then
453 * the new connection will be refused.
455 * If the maximum is set to a number lower than the current
456 * number of connections, no current connections are
459 * @todo honoring max_connections has not been implemented
460 * yet. The only real work involved is keeping a list
461 * of live connections on the DBusServer so the oldest
462 * unauthenticated client can be located when required.
464 * @todo for a systemwide daemon, we need a max number of connections
465 * per user, since any user can authenticate a bunch of connections
468 * @todo a single process might listen on multiple mechanisms
469 * (multiple DBusServer) and might want the max connections
470 * value to span all those servers. Should consider
471 * changing the API accordingly, though I'm inclined to
472 * punt this to the app that wants to do it instead of
473 * putting it in the library.
475 * @param server the server
476 * @param max_connections maximum number of connections allowed
479 dbus_server_set_max_connections (DBusServer *server,
482 server->max_connections = max_connections;
486 * Gets the maximum number of connections that can be active
487 * at a time for this server.
489 * @param server the server
490 * @returns maximum number of connections at once
493 dbus_server_get_max_connections (DBusServer *server)
495 return server->max_connections;
499 * Gets the number of #DBusConnection to this server that
500 * have not yet been finalized. i.e. all #DBusConnection that
501 * were passed to #DBusNewConnectionFunction and have not yet been
502 * finalized will count in this total.
504 * @param server the server
505 * @returns the number of connections
508 dbus_server_get_n_connections (DBusServer *server)
510 return _dbus_counter_get_value (server->connection_counter);