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"
25 #ifdef DBUS_BUILD_TESTS
26 #include "dbus-server-debug.h"
30 * @defgroup DBusServer DBusServer
32 * @brief Server that listens for new connections.
34 * Types and functions related to DBusServer.
35 * A DBusServer represents a server that other applications
36 * can connect to. Each connection from another application
37 * is represented by a DBusConnection.
41 * @defgroup DBusServerInternals DBusServer implementation details
42 * @ingroup DBusInternals
43 * @brief Implementation details of DBusServer
49 * Initializes the members of the DBusServer base class.
50 * Chained up to by subclass constructors.
52 * @param server the server.
53 * @param vtable the vtable for the subclass.
54 * @returns #TRUE on success.
57 _dbus_server_init_base (DBusServer *server,
58 const DBusServerVTable *vtable)
60 server->vtable = vtable;
63 server->watches = _dbus_watch_list_new ();
64 if (server->watches == NULL)
67 server->timeouts = _dbus_timeout_list_new ();
68 if (server->timeouts == NULL)
70 _dbus_watch_list_free (server->watches);
71 server->watches = NULL;
75 server->connection_counter = _dbus_counter_new ();
76 if (server->connection_counter == NULL)
78 _dbus_watch_list_free (server->watches);
79 server->watches = NULL;
80 _dbus_timeout_list_free (server->timeouts);
81 server->timeouts = NULL;
86 server->max_connections = 256; /* same as an X server, seems like a nice default */
92 * Finalizes the members of the DBusServer base class.
93 * Chained up to by subclass finalizers.
95 * @param server the server.
98 _dbus_server_finalize_base (DBusServer *server)
100 dbus_server_set_new_connection_function (server, NULL, NULL, NULL);
102 if (!server->disconnected)
103 dbus_server_disconnect (server);
105 _dbus_watch_list_free (server->watches);
106 _dbus_timeout_list_free (server->timeouts);
107 _dbus_counter_unref (server->connection_counter);
111 * Adds a watch for this server, chaining out to application-provided
114 * @param server the server.
115 * @param watch the watch to add.
118 _dbus_server_add_watch (DBusServer *server,
121 return _dbus_watch_list_add_watch (server->watches, watch);
125 * Removes a watch previously added with _dbus_server_remove_watch().
127 * @param server the server.
128 * @param watch the watch to remove.
131 _dbus_server_remove_watch (DBusServer *server,
134 _dbus_watch_list_remove_watch (server->watches, watch);
138 _dbus_server_add_timeout (DBusServer *server,
139 DBusTimeout *timeout)
141 return _dbus_timeout_list_add_timeout (server->timeouts, timeout);
145 _dbus_server_remove_timeout (DBusServer *server,
146 DBusTimeout *timeout)
148 _dbus_timeout_list_remove_timeout (server->timeouts, timeout);
154 * @addtogroup DBusServer
161 * @typedef DBusServer
163 * An opaque object representing a server that listens for
164 * connections from other applications. Each time a connection
165 * is made, a new DBusConnection is created and made available
166 * via an application-provided DBusNewConnectionFunction.
167 * The DBusNewConnectionFunction is provided with
168 * dbus_server_set_new_connection_function().
173 * Listens for new connections on the given address.
174 * Returns #NULL if listening fails for any reason.
175 * Otherwise returns a new #DBusServer.
176 * dbus_server_set_new_connection_function() and
177 * dbus_server_set_watch_functions() should be called
178 * immediately to render the server fully functional.
180 * @param address the address of this server.
181 * @param result location to store rationale for failure.
182 * @returns a new DBusServer, or #NULL on failure.
186 dbus_server_listen (const char *address,
187 DBusResultCode *result)
192 /* For now just pretend the address is a unix domain socket path */
193 server = _dbus_server_new_for_domain_socket (address, result);
195 server = _dbus_server_debug_new (address, result);
202 * Increments the reference count of a DBusServer.
204 * @param server the server.
207 dbus_server_ref (DBusServer *server)
209 server->refcount += 1;
213 * Decrements the reference count of a DBusServer. Finalizes the
214 * server if the reference count reaches zero. The server connection
215 * will be closed as with dbus_server_disconnect() when the server is
218 * @param server the server.
221 dbus_server_unref (DBusServer *server)
223 _dbus_assert (server != NULL);
224 _dbus_assert (server->refcount > 0);
226 server->refcount -= 1;
227 if (server->refcount == 0)
229 _dbus_assert (server->vtable->finalize != NULL);
231 (* server->vtable->finalize) (server);
236 * Releases the server's address and stops listening for
237 * new clients. If called more than once, only the first
238 * call has an effect. Does not modify the server's
241 * @param server the server.
244 dbus_server_disconnect (DBusServer *server)
246 _dbus_assert (server->vtable->disconnect != NULL);
248 if (server->disconnected)
251 (* server->vtable->disconnect) (server);
252 server->disconnected = TRUE;
256 * Returns #TRUE if the server is still listening for new connections.
258 * @param server the server.
261 dbus_server_get_is_connected (DBusServer *server)
263 return !server->disconnected;
267 * Sets a function to be used for handling new connections. The given
268 * function is passed each new connection as the connection is
269 * created. If the new connection function increments the connection's
270 * reference count, the connection will stay alive. Otherwise, the
271 * connection will be unreferenced and closed.
273 * @param server the server.
274 * @param function a function to handle new connections.
275 * @param data data to pass to the new connection handler.
276 * @param free_data_function function to free the data.
279 dbus_server_set_new_connection_function (DBusServer *server,
280 DBusNewConnectionFunction function,
282 DBusFreeFunction free_data_function)
284 if (server->new_connection_free_data_function != NULL)
285 (* server->new_connection_free_data_function) (server->new_connection_data);
287 server->new_connection_function = function;
288 server->new_connection_data = data;
289 server->new_connection_free_data_function = free_data_function;
293 * Sets the watch functions for the connection. These functions are
294 * responsible for making the application's main loop aware of file
295 * descriptors that need to be monitored for events.
297 * This function behaves exactly like dbus_connection_set_watch_functions();
298 * see the documentation for that routine.
300 * @param server the server.
301 * @param add_function function to begin monitoring a new descriptor.
302 * @param remove_function function to stop monitoring a descriptor.
303 * @param data data to pass to add_function and remove_function.
304 * @param free_data_function function to be called to free the data.
307 dbus_server_set_watch_functions (DBusServer *server,
308 DBusAddWatchFunction add_function,
309 DBusRemoveWatchFunction remove_function,
311 DBusFreeFunction free_data_function)
313 _dbus_watch_list_set_functions (server->watches,
321 * Sets the timeout functions for the connection. These functions are
322 * responsible for making the application's main loop aware of timeouts.
324 * This function behaves exactly like dbus_connection_set_timeout_functions();
325 * see the documentation for that routine.
327 * @param server the server.
328 * @param add_function function to add a timeout.
329 * @param remove_function function to remove a timeout.
330 * @param data data to pass to add_function and remove_function.
331 * @param free_data_function function to be called to free the data.
334 dbus_server_set_timeout_functions (DBusServer *server,
335 DBusAddTimeoutFunction add_function,
336 DBusRemoveTimeoutFunction remove_function,
338 DBusFreeFunction free_data_function)
340 _dbus_timeout_list_set_functions (server->timeouts,
341 add_function, remove_function,
342 data, free_data_function);
346 * Called to notify the server when a previously-added watch
347 * is ready for reading or writing, or has an exception such
350 * @param server the server.
351 * @param watch the watch.
352 * @param condition the current condition of the file descriptors being watched.
355 dbus_server_handle_watch (DBusServer *server,
357 unsigned int condition)
359 _dbus_assert (server->vtable->handle_watch != NULL);
361 _dbus_watch_sanitize_condition (watch, &condition);
363 (* server->vtable->handle_watch) (server, watch, condition);
367 * Sets the maximum number of connections that can be open at one
368 * time for this server. If the maximum is reached, and another
369 * client tries to connect, then the oldest unauthenticated client
370 * will be dropped. If no unauthenticated client exists, then
371 * the new connection will be refused.
373 * If the maximum is set to a number lower than the current
374 * number of connections, no current connections are
377 * @todo honoring max_connections has not been implemented
378 * yet. The only real work involved is keeping a list
379 * of live connections on the DBusServer so the oldest
380 * unauthenticated client can be located when required.
382 * @todo for a systemwide daemon, we need a max number of connections
383 * per user, since any user can authenticate a bunch of connections
386 * @todo a single process might listen on multiple mechanisms
387 * (multiple DBusServer) and might want the max connections
388 * value to span all those servers. Should consider
389 * changing the API accordingly, though I'm inclined to
390 * punt this to the app that wants to do it instead of
391 * putting it in the library.
393 * @param server the server
394 * @param max_connections maximum number of connections allowed
397 dbus_server_set_max_connections (DBusServer *server,
400 server->max_connections = max_connections;
404 * Gets the maximum number of connections that can be active
405 * at a time for this server.
407 * @param server the server
408 * @returns maximum number of connections at once
411 dbus_server_get_max_connections (DBusServer *server)
413 return server->max_connections;
417 * Gets the number of #DBusConnection to this server that
418 * have not yet been finalized. i.e. all #DBusConnection that
419 * were passed to #DBusNewConnectionFunction and have not yet been
420 * finalized will count in this total.
422 * @param server the server
423 * @returns the number of connections
426 dbus_server_get_n_connections (DBusServer *server)
428 return _dbus_counter_get_value (server->connection_counter);