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
23 #include "dbus-server.h"
24 #include "dbus-server-unix.h"
27 * @defgroup DBusServer DBusServer
29 * @brief Server that listens for new connections.
31 * Types and functions related to DBusServer.
32 * A DBusServer represents a server that other applications
33 * can connect to. Each connection from another application
34 * is represented by a DBusConnection.
38 * @defgroup DBusServerInternals DBusServer implementation details
39 * @ingroup DBusInternals
40 * @brief Implementation details of DBusServer
46 * Initializes the members of the DBusServer base class.
47 * Chained up to by subclass constructors.
49 * @param server the server.
50 * @param vtable the vtable for the subclass.
51 * @returns #TRUE on success.
54 _dbus_server_init_base (DBusServer *server,
55 const DBusServerVTable *vtable)
57 server->vtable = vtable;
60 server->watches = _dbus_watch_list_new ();
61 if (server->watches == NULL)
64 server->connection_counter = _dbus_counter_new ();
65 if (server->connection_counter == NULL)
67 _dbus_watch_list_free (server->watches);
68 server->watches = NULL;
72 server->max_connections = 256; /* same as an X server, seems like a nice default */
78 * Finalizes the members of the DBusServer base class.
79 * Chained up to by subclass finalizers.
81 * @param server the server.
84 _dbus_server_finalize_base (DBusServer *server)
86 dbus_server_set_new_connection_function (server, NULL, NULL, NULL);
88 if (!server->disconnected)
89 dbus_server_disconnect (server);
91 _dbus_watch_list_free (server->watches);
92 _dbus_counter_unref (server->connection_counter);
96 * Adds a watch for this server, chaining out to application-provided
99 * @param server the server.
100 * @param watch the watch to add.
103 _dbus_server_add_watch (DBusServer *server,
106 return _dbus_watch_list_add_watch (server->watches, watch);
110 * Removes a watch previously added with _dbus_server_remove_watch().
112 * @param server the server.
113 * @param watch the watch to remove.
116 _dbus_server_remove_watch (DBusServer *server,
119 _dbus_watch_list_remove_watch (server->watches, watch);
126 * @addtogroup DBusServer
133 * @typedef DBusServer
135 * An opaque object representing a server that listens for
136 * connections from other applications. Each time a connection
137 * is made, a new DBusConnection is created and made available
138 * via an application-provided DBusNewConnectionFunction.
139 * The DBusNewConnectionFunction is provided with
140 * dbus_server_set_new_connection_function().
145 * Listens for new connections on the given address.
146 * Returns #NULL if listening fails for any reason.
147 * Otherwise returns a new #DBusServer.
148 * dbus_server_set_new_connection_function() and
149 * dbus_server_set_watch_functions() should be called
150 * immediately to render the server fully functional.
152 * @param address the address of this server.
153 * @param result location to store rationale for failure.
154 * @returns a new DBusServer, or #NULL on failure.
158 dbus_server_listen (const char *address,
159 DBusResultCode *result)
163 /* For now just pretend the address is a unix domain socket path */
164 server = _dbus_server_new_for_domain_socket (address, result);
170 * Increments the reference count of a DBusServer.
172 * @param server the server.
175 dbus_server_ref (DBusServer *server)
177 server->refcount += 1;
181 * Decrements the reference count of a DBusServer. Finalizes the
182 * server if the reference count reaches zero. The server connection
183 * will be closed as with dbus_server_disconnect() when the server is
186 * @param server the server.
189 dbus_server_unref (DBusServer *server)
191 _dbus_assert (server != NULL);
192 _dbus_assert (server->refcount > 0);
194 server->refcount -= 1;
195 if (server->refcount == 0)
197 _dbus_assert (server->vtable->finalize != NULL);
199 (* server->vtable->finalize) (server);
204 * Releases the server's address and stops listening for
205 * new clients. If called more than once, only the first
206 * call has an effect. Does not modify the server's
209 * @param server the server.
212 dbus_server_disconnect (DBusServer *server)
214 _dbus_assert (server->vtable->disconnect != NULL);
216 if (server->disconnected)
219 (* server->vtable->disconnect) (server);
220 server->disconnected = TRUE;
224 * Returns #TRUE if the server is still listening for new connections.
226 * @param server the server.
229 dbus_server_get_is_connected (DBusServer *server)
231 return !server->disconnected;
235 * Sets a function to be used for handling new connections. The given
236 * function is passed each new connection as the connection is
237 * created. If the new connection function increments the connection's
238 * reference count, the connection will stay alive. Otherwise, the
239 * connection will be unreferenced and closed.
241 * @param server the server.
242 * @param function a function to handle new connections.
243 * @param data data to pass to the new connection handler.
244 * @param free_data_function function to free the data.
247 dbus_server_set_new_connection_function (DBusServer *server,
248 DBusNewConnectionFunction function,
250 DBusFreeFunction free_data_function)
252 if (server->new_connection_free_data_function != NULL)
253 (* server->new_connection_free_data_function) (server->new_connection_data);
255 server->new_connection_function = function;
256 server->new_connection_data = data;
257 server->new_connection_free_data_function = free_data_function;
261 * Sets the watch functions for the connection. These functions are
262 * responsible for making the application's main loop aware of file
263 * descriptors that need to be monitored for events.
265 * This function behaves exactly like dbus_connection_set_watch_functions();
266 * see the documentation for that routine.
268 * @param server the server.
269 * @param add_function function to begin monitoring a new descriptor.
270 * @param remove_function function to stop monitoring a descriptor.
271 * @param data data to pass to add_function and remove_function.
272 * @param free_data_function function to be called to free the data.
275 dbus_server_set_watch_functions (DBusServer *server,
276 DBusAddWatchFunction add_function,
277 DBusRemoveWatchFunction remove_function,
279 DBusFreeFunction free_data_function)
281 _dbus_watch_list_set_functions (server->watches,
289 * Called to notify the server when a previously-added watch
290 * is ready for reading or writing, or has an exception such
293 * @param server the server.
294 * @param watch the watch.
295 * @param condition the current condition of the file descriptors being watched.
298 dbus_server_handle_watch (DBusServer *server,
300 unsigned int condition)
302 _dbus_assert (server->vtable->handle_watch != NULL);
304 _dbus_watch_sanitize_condition (watch, &condition);
306 (* server->vtable->handle_watch) (server, watch, condition);
310 * Sets the maximum number of connections that can be open at one
311 * time for this server. If the maximum is reached, and another
312 * client tries to connect, then the oldest unauthenticated client
313 * will be dropped. If no unauthenticated client exists, then
314 * the new connection will be refused.
316 * If the maximum is set to a number lower than the current
317 * number of connections, no current connections are
320 * @todo honoring max_connections has not been implemented
321 * yet. The only real work involved is keeping a list
322 * of live connections on the DBusServer so the oldest
323 * unauthenticated client can be located when required.
325 * @todo for a systemwide daemon, we need a max number of connections
326 * per user, since any user can authenticate a bunch of connections
329 * @todo a single process might listen on multiple mechanisms
330 * (multiple DBusServer) and might want the max connections
331 * value to span all those servers. Should consider
332 * changing the API accordingly, though I'm inclined to
333 * punt this to the app that wants to do it instead of
334 * putting it in the library.
336 * @param server the server
337 * @param max_connections maximum number of connections allowed
340 dbus_server_set_max_connections (DBusServer *server,
343 server->max_connections = max_connections;
347 * Gets the maximum number of connections that can be active
348 * at a time for this server.
350 * @param server the server
351 * @returns maximum number of connections at once
354 dbus_server_get_max_connections (DBusServer *server)
356 return server->max_connections;
360 * Gets the number of #DBusConnection to this server that
361 * have not yet been finalized. i.e. all #DBusConnection that
362 * were passed to #DBusNewConnectionFunction and have not yet been
363 * finalized will count in this total.
365 * @param server the server
366 * @param returns the number of connections
369 dbus_server_get_n_connections (DBusServer *server)
371 return _dbus_counter_get_value (server->connection_counter);