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
149 * application-provided timeout handlers. The timeout should be
150 * repeatedly handled with dbus_timeout_handle() at its given interval
151 * until it is removed.
153 * @param server the server.
154 * @param timeout the timeout to add.
157 _dbus_server_add_timeout (DBusServer *server,
158 DBusTimeout *timeout)
160 return _dbus_timeout_list_add_timeout (server->timeouts, timeout);
164 * Removes a timeout previously added with _dbus_server_add_timeout().
166 * @param server the server.
167 * @param timeout the timeout to remove.
170 _dbus_server_remove_timeout (DBusServer *server,
171 DBusTimeout *timeout)
173 _dbus_timeout_list_remove_timeout (server->timeouts, timeout);
179 * @addtogroup DBusServer
186 * @typedef DBusServer
188 * An opaque object representing a server that listens for
189 * connections from other applications. Each time a connection
190 * is made, a new DBusConnection is created and made available
191 * via an application-provided DBusNewConnectionFunction.
192 * The DBusNewConnectionFunction is provided with
193 * dbus_server_set_new_connection_function().
198 * Listens for new connections on the given address.
199 * Returns #NULL if listening fails for any reason.
200 * Otherwise returns a new #DBusServer.
201 * dbus_server_set_new_connection_function() and
202 * dbus_server_set_watch_functions() should be called
203 * immediately to render the server fully functional.
205 * @todo error messages on bad address could really be better.
206 * DBusResultCode is a bit limiting here.
208 * @param address the address of this server.
209 * @param result location to store rationale for failure.
210 * @returns a new DBusServer, or #NULL on failure.
214 dbus_server_listen (const char *address,
215 DBusResultCode *result)
218 DBusAddressEntry **entries;
221 if (!dbus_parse_address (address, &entries, &len, result))
226 for (i = 0; i < len; i++)
228 const char *method = dbus_address_entry_get_method (entries[i]);
230 if (strcmp (method, "unix") == 0)
232 const char *path = dbus_address_entry_get_value (entries[i], "path");
237 server = _dbus_server_new_for_domain_socket (path, result);
242 else if (strcmp (method, "tcp") == 0)
244 const char *host = dbus_address_entry_get_value (entries[i], "host");
245 const char *port = dbus_address_entry_get_value (entries[i], "port");
253 _dbus_string_init_const (&str, port);
254 sresult = _dbus_string_parse_int (&str, 0, &lport, NULL);
255 _dbus_string_free (&str);
257 if (sresult == FALSE || lport <= 0 || lport > 65535)
260 server = _dbus_server_new_for_tcp_socket (host, lport, result);
265 #ifdef DBUS_BUILD_TESTS
266 else if (strcmp (method, "debug") == 0)
268 const char *name = dbus_address_entry_get_value (entries[i], "name");
273 server = _dbus_server_debug_new (name, result);
283 dbus_address_entries_free (entries);
287 dbus_address_entries_free (entries);
288 dbus_set_result (result, DBUS_RESULT_BAD_ADDRESS);
294 * Increments the reference count of a DBusServer.
296 * @param server the server.
299 dbus_server_ref (DBusServer *server)
301 server->refcount += 1;
305 * Decrements the reference count of a DBusServer. Finalizes the
306 * server if the reference count reaches zero. The server connection
307 * will be closed as with dbus_server_disconnect() when the server is
310 * @param server the server.
313 dbus_server_unref (DBusServer *server)
315 _dbus_assert (server != NULL);
316 _dbus_assert (server->refcount > 0);
318 server->refcount -= 1;
319 if (server->refcount == 0)
321 _dbus_assert (server->vtable->finalize != NULL);
323 (* server->vtable->finalize) (server);
328 * Releases the server's address and stops listening for
329 * new clients. If called more than once, only the first
330 * call has an effect. Does not modify the server's
333 * @param server the server.
336 dbus_server_disconnect (DBusServer *server)
338 _dbus_assert (server->vtable->disconnect != NULL);
340 if (server->disconnected)
343 (* server->vtable->disconnect) (server);
344 server->disconnected = TRUE;
348 * Returns #TRUE if the server is still listening for new connections.
350 * @param server the server.
353 dbus_server_get_is_connected (DBusServer *server)
355 return !server->disconnected;
359 * Sets a function to be used for handling new connections. The given
360 * function is passed each new connection as the connection is
361 * created. If the new connection function increments the connection's
362 * reference count, the connection will stay alive. Otherwise, the
363 * connection will be unreferenced and closed.
365 * @param server the server.
366 * @param function a function to handle new connections.
367 * @param data data to pass to the new connection handler.
368 * @param free_data_function function to free the data.
371 dbus_server_set_new_connection_function (DBusServer *server,
372 DBusNewConnectionFunction function,
374 DBusFreeFunction free_data_function)
376 if (server->new_connection_free_data_function != NULL)
377 (* server->new_connection_free_data_function) (server->new_connection_data);
379 server->new_connection_function = function;
380 server->new_connection_data = data;
381 server->new_connection_free_data_function = free_data_function;
385 * Sets the watch functions for the connection. These functions are
386 * responsible for making the application's main loop aware of file
387 * descriptors that need to be monitored for events.
389 * This function behaves exactly like dbus_connection_set_watch_functions();
390 * see the documentation for that routine.
392 * @param server the server.
393 * @param add_function function to begin monitoring a new descriptor.
394 * @param remove_function function to stop monitoring a descriptor.
395 * @param data data to pass to add_function and remove_function.
396 * @param free_data_function function to be called to free the data.
397 * @returns #FALSE on failure (no memory)
400 dbus_server_set_watch_functions (DBusServer *server,
401 DBusAddWatchFunction add_function,
402 DBusRemoveWatchFunction remove_function,
404 DBusFreeFunction free_data_function)
406 return _dbus_watch_list_set_functions (server->watches,
414 * Sets the timeout functions for the connection. These functions are
415 * responsible for making the application's main loop aware of timeouts.
417 * This function behaves exactly like dbus_connection_set_timeout_functions();
418 * see the documentation for that routine.
420 * @param server the server.
421 * @param add_function function to add a timeout.
422 * @param remove_function function to remove a timeout.
423 * @param data data to pass to add_function and remove_function.
424 * @param free_data_function function to be called to free the data.
425 * @returns #FALSE on failure (no memory)
428 dbus_server_set_timeout_functions (DBusServer *server,
429 DBusAddTimeoutFunction add_function,
430 DBusRemoveTimeoutFunction remove_function,
432 DBusFreeFunction free_data_function)
434 return _dbus_timeout_list_set_functions (server->timeouts,
435 add_function, remove_function,
436 data, free_data_function);
440 * Called to notify the server when a previously-added watch
441 * is ready for reading or writing, or has an exception such
444 * @param server the server.
445 * @param watch the watch.
446 * @param condition the current condition of the file descriptors being watched.
449 dbus_server_handle_watch (DBusServer *server,
451 unsigned int condition)
453 _dbus_assert (server->vtable->handle_watch != NULL);
455 _dbus_watch_sanitize_condition (watch, &condition);
457 (* server->vtable->handle_watch) (server, watch, condition);
461 * Sets the maximum number of connections that can be open at one
462 * time for this server. If the maximum is reached, and another
463 * client tries to connect, then the oldest unauthenticated client
464 * will be dropped. If no unauthenticated client exists, then
465 * the new connection will be refused.
467 * If the maximum is set to a number lower than the current
468 * number of connections, no current connections are
471 * @todo honoring max_connections has not been implemented
472 * yet. The only real work involved is keeping a list
473 * of live connections on the DBusServer so the oldest
474 * unauthenticated client can be located when required.
476 * @todo for a systemwide daemon, we need a max number of connections
477 * per user, since any user can authenticate a bunch of connections
480 * @todo a single process might listen on multiple mechanisms
481 * (multiple DBusServer) and might want the max connections
482 * value to span all those servers. Should consider
483 * changing the API accordingly, though I'm inclined to
484 * punt this to the app that wants to do it instead of
485 * putting it in the library.
487 * @param server the server
488 * @param max_connections maximum number of connections allowed
491 dbus_server_set_max_connections (DBusServer *server,
494 server->max_connections = max_connections;
498 * Gets the maximum number of connections that can be active
499 * at a time for this server.
501 * @param server the server
502 * @returns maximum number of connections at once
505 dbus_server_get_max_connections (DBusServer *server)
507 return server->max_connections;
511 * Gets the number of #DBusConnection to this server that
512 * have not yet been finalized. i.e. all #DBusConnection that
513 * were passed to #DBusNewConnectionFunction and have not yet been
514 * finalized will count in this total.
516 * @param server the server
517 * @returns the number of connections
520 dbus_server_get_n_connections (DBusServer *server)
522 return _dbus_counter_get_value (server->connection_counter);
526 static DBusDataSlotAllocator slot_allocator;
529 * Initialize the mutex used for #DBusConnection data
535 _dbus_server_slots_init_lock (void)
537 if (!_dbus_data_slot_allocator_init (&slot_allocator))
540 return slot_allocator.lock;
544 * Allocates an integer ID to be used for storing application-specific
545 * data on any DBusServer. The allocated ID may then be used
546 * with dbus_server_set_data() and dbus_server_get_data().
547 * If allocation fails, -1 is returned. Again, the allocated
548 * slot is global, i.e. all DBusServer objects will
549 * have a slot with the given integer ID reserved.
551 * @returns -1 on failure, otherwise the data slot ID
554 dbus_server_allocate_data_slot (void)
556 return _dbus_data_slot_allocator_alloc (&slot_allocator);
560 * Deallocates a global ID for server data slots.
561 * dbus_server_get_data() and dbus_server_set_data()
562 * may no longer be used with this slot.
563 * Existing data stored on existing DBusServer objects
564 * will be freed when the server is finalized,
565 * but may not be retrieved (and may only be replaced
566 * if someone else reallocates the slot).
568 * @param slot the slot to deallocate
571 dbus_server_free_data_slot (int slot)
573 _dbus_data_slot_allocator_free (&slot_allocator, slot);
577 * Stores a pointer on a DBusServer, along
578 * with an optional function to be used for freeing
579 * the data when the data is set again, or when
580 * the server is finalized. The slot number
581 * must have been allocated with dbus_server_allocate_data_slot().
583 * @param server the server
584 * @param slot the slot number
585 * @param data the data to store
586 * @param free_data_func finalizer function for the data
587 * @returns #TRUE if there was enough memory to store the data
590 dbus_server_set_data (DBusServer *server,
593 DBusFreeFunction free_data_func)
595 DBusFreeFunction old_free_func;
600 dbus_mutex_lock (server->mutex);
603 retval = _dbus_data_slot_list_set (&slot_allocator,
605 slot, data, free_data_func,
606 &old_free_func, &old_data);
609 dbus_mutex_unlock (server->mutex);
614 /* Do the actual free outside the server lock */
616 (* old_free_func) (old_data);
623 * Retrieves data previously set with dbus_server_set_data().
624 * The slot must still be allocated (must not have been freed).
626 * @param server the server
627 * @param slot the slot to get data from
628 * @returns the data, or #NULL if not found
631 dbus_server_get_data (DBusServer *server,
637 dbus_mutex_lock (server->mutex);
640 res = _dbus_data_slot_list_get (&slot_allocator,
645 dbus_mutex_unlock (server->mutex);