diff options
Diffstat (limited to 'bus')
-rw-r--r-- | bus/Makefile.am | 6 | ||||
-rw-r--r-- | bus/connection.c | 186 | ||||
-rw-r--r-- | bus/connection.h | 41 | ||||
-rw-r--r-- | bus/loop.c | 6 | ||||
-rw-r--r-- | bus/main.c | 72 | ||||
-rw-r--r-- | bus/services.c | 154 | ||||
-rw-r--r-- | bus/services.h | 53 |
7 files changed, 448 insertions, 70 deletions
diff --git a/bus/Makefile.am b/bus/Makefile.am index f51d8950..b7f4504a 100644 --- a/bus/Makefile.am +++ b/bus/Makefile.am @@ -7,9 +7,13 @@ EFENCE= bin_PROGRAMS=dbus-daemon-1 dbus_daemon_1_SOURCES= \ + connection.c \ + connection.h \ loop.c \ loop.h \ - main.c + main.c \ + services.c \ + services.h dbus_daemon_1_LDADD= \ $(EFENCE) \ diff --git a/bus/connection.c b/bus/connection.c new file mode 100644 index 00000000..5b7c5391 --- /dev/null +++ b/bus/connection.c @@ -0,0 +1,186 @@ +/* -*- mode: C; c-file-style: "gnu" -*- */ +/* connection.c Client connections + * + * Copyright (C) 2003 Red Hat, Inc. + * + * Licensed under the Academic Free License version 1.2 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ +#include "connection.h" +#include "loop.h" +#include "services.h" +#include <dbus/dbus-list.h> + +static int connection_data_slot; +static DBusList *connections = NULL; + +typedef struct +{ + DBusList *services_owned; + +} BusConnectionData; + +#define BUS_CONNECTION_DATA(connection) (dbus_connection_get_data ((connection), connection_data_slot)) + +static void +connection_error_handler (DBusConnection *connection, + DBusResultCode error_code, + void *data) +{ + BusConnectionData *d; + BusService *service; + + _dbus_warn ("Error on connection: %s\n", + dbus_result_to_string (error_code)); + + d = BUS_CONNECTION_DATA (connection); + _dbus_assert (d != NULL); + + /* we don't want to be called again since we're dropping the connection */ + dbus_connection_set_error_function (connection, NULL, NULL, NULL); + + /* Drop any service ownership */ + while ((service = _dbus_list_get_last (&d->services_owned))) + bus_service_remove_owner (service, connection); + + /* no more watching */ + dbus_connection_set_watch_functions (connection, + NULL, NULL, + connection, + NULL); + + dbus_connection_set_data (connection, + connection_data_slot, + NULL, NULL); + + _dbus_list_remove (&connections, connection); + dbus_connection_unref (connection); +} + +static void +connection_watch_callback (DBusWatch *watch, + unsigned int condition, + void *data) +{ + DBusConnection *connection = data; + + dbus_connection_handle_watch (connection, watch, condition); +} + +static void +add_connection_watch (DBusWatch *watch, + DBusConnection *connection) +{ + bus_loop_add_watch (watch, connection_watch_callback, connection, + NULL); +} + +static void +remove_connection_watch (DBusWatch *watch, + DBusConnection *connection) +{ + bus_loop_remove_watch (watch, connection_watch_callback, connection); +} + +static void +free_connection_data (void *data) +{ + BusConnectionData *d = data; + + /* services_owned should be NULL since we should be disconnected */ + _dbus_assert (d->services_owned == NULL); + + dbus_free (d); +} + +dbus_bool_t +bus_connection_init (void) +{ + connection_data_slot = dbus_connection_allocate_data_slot (); + + if (connection_data_slot < 0) + return FALSE; + + return TRUE; +} + +dbus_bool_t +bus_connection_setup (DBusConnection *connection) +{ + BusConnectionData *d; + + d = dbus_new (BusConnectionData, 1); + if (d == NULL) + return FALSE; + + if (!dbus_connection_set_data (connection, + connection_data_slot, + d, free_connection_data)) + { + dbus_free (d); + return FALSE; + } + + if (!_dbus_list_append (&connections, connection)) + { + /* this will free our data when connection gets finalized */ + dbus_connection_disconnect (connection); + return FALSE; + } + + dbus_connection_ref (connection); + + dbus_connection_set_watch_functions (connection, + (DBusAddWatchFunction) add_connection_watch, + (DBusRemoveWatchFunction) remove_connection_watch, + connection, + NULL); + + dbus_connection_set_error_function (connection, + connection_error_handler, + NULL, NULL); + + return TRUE; +} + +dbus_bool_t +bus_connection_add_owned_service (DBusConnection *connection, + BusService *service) +{ + BusConnectionData *d; + + d = BUS_CONNECTION_DATA (connection); + _dbus_assert (d != NULL); + + if (!_dbus_list_append (&d->services_owned, + service)) + return FALSE; + + return TRUE; +} + +void +bus_connection_remove_owned_service (DBusConnection *connection, + BusService *service) +{ + BusConnectionData *d; + + d = BUS_CONNECTION_DATA (connection); + _dbus_assert (d != NULL); + + _dbus_list_remove_last (&d->services_owned, service); +} diff --git a/bus/connection.h b/bus/connection.h new file mode 100644 index 00000000..e6f9d10d --- /dev/null +++ b/bus/connection.h @@ -0,0 +1,41 @@ +/* -*- mode: C; c-file-style: "gnu" -*- */ +/* connection.h Client connections + * + * Copyright (C) 2003 Red Hat, Inc. + * + * Licensed under the Academic Free License version 1.2 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +#ifndef BUS_CONNECTION_H +#define BUS_CONNECTION_H + +#include <dbus/dbus.h> +#include "services.h" + +dbus_bool_t bus_connection_init (void); + +dbus_bool_t bus_connection_setup (DBusConnection *connection); + + +/* called by services.c */ +dbus_bool_t bus_connection_add_owned_service (DBusConnection *connection, + BusService *service); +void bus_connection_remove_owned_service (DBusConnection *connection, + BusService *service); + +#endif /* BUS_CONNECTION_H */ @@ -173,7 +173,11 @@ bus_loop_run (void) initial_serial = watch_list_serial; i = 0; while (i < n_fds) - { + { + /* FIXME I think this "restart if we change the watches" + * approach could result in starving watches + * toward the end of the list. + */ if (initial_serial != watch_list_serial) goto next_iteration; @@ -21,35 +21,8 @@ * */ #include "loop.h" +#include "connection.h" #include <dbus/dbus-list.h> -#include <dbus/dbus.h> - -static DBusList *connections = NULL; - -static void -connection_error_handler (DBusConnection *connection, - DBusResultCode error_code, - void *data) -{ - _dbus_warn ("Error on connection: %s\n", - dbus_result_to_string (error_code)); - - /* we don't want to be called again since we're dropping the connection */ - dbus_connection_set_error_function (connection, NULL, NULL, NULL); - - _dbus_list_remove (&connections, connection); - dbus_connection_unref (connection); -} - -static void -connection_watch_callback (DBusWatch *watch, - unsigned int condition, - void *data) -{ - DBusConnection *connection = data; - - dbus_connection_handle_watch (connection, watch, condition); -} static void server_watch_callback (DBusWatch *watch, @@ -62,21 +35,6 @@ server_watch_callback (DBusWatch *watch, } static void -add_connection_watch (DBusWatch *watch, - DBusConnection *connection) -{ - bus_loop_add_watch (watch, connection_watch_callback, connection, - NULL); -} - -static void -remove_connection_watch (DBusWatch *watch, - DBusConnection *connection) -{ - bus_loop_remove_watch (watch, connection_watch_callback, connection); -} - -static void add_server_watch (DBusWatch *watch, DBusServer *server) { @@ -91,30 +49,6 @@ remove_server_watch (DBusWatch *watch, bus_loop_remove_watch (watch, server_watch_callback, server); } -static dbus_bool_t -setup_connection (DBusConnection *connection) -{ - if (!_dbus_list_append (&connections, connection)) - { - dbus_connection_disconnect (connection); - return FALSE; - } - - dbus_connection_ref (connection); - - dbus_connection_set_watch_functions (connection, - (DBusAddWatchFunction) add_connection_watch, - (DBusRemoveWatchFunction) remove_connection_watch, - connection, - NULL); - - dbus_connection_set_error_function (connection, - connection_error_handler, - NULL, NULL); - - return TRUE; -} - static void setup_server (DBusServer *server) { @@ -130,7 +64,7 @@ new_connection_callback (DBusServer *server, DBusConnection *new_connection, void *data) { - if (!setup_connection (new_connection)) + if (!bus_connection_setup (new_connection)) ; /* we won't have ref'd the connection so it will die */ } @@ -156,6 +90,8 @@ main (int argc, char **argv) setup_server (server); + bus_connection_init (); + dbus_server_set_new_connection_function (server, new_connection_callback, NULL, NULL); diff --git a/bus/services.c b/bus/services.c new file mode 100644 index 00000000..f8124b05 --- /dev/null +++ b/bus/services.c @@ -0,0 +1,154 @@ +/* -*- mode: C; c-file-style: "gnu" -*- */ +/* services.c Service management + * + * Copyright (C) 2003 Red Hat, Inc. + * + * Licensed under the Academic Free License version 1.2 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ +#include "services.h" +#include "connection.h" +#include <dbus/dbus-hash.h> +#include <dbus/dbus-list.h> +#include <dbus/dbus-mempool.h> + +struct BusService +{ + char *name; + DBusList *owners; +}; + +static DBusHashTable *service_hash = NULL; +static DBusMemPool *service_pool = NULL; + +BusService* +bus_service_lookup (const DBusString *service_name, + dbus_bool_t create_if_not_found) +{ + const char *c_name; + BusService *service; + + if (service_hash == NULL) + { + service_hash = _dbus_hash_table_new (DBUS_HASH_STRING, + NULL, NULL); + service_pool = _dbus_mem_pool_new (sizeof (BusService), + TRUE); + + if (service_hash == NULL || service_pool == NULL) + { + if (service_hash) + { + _dbus_hash_table_unref (service_hash); + service_hash = NULL; + } + if (service_pool) + { + _dbus_mem_pool_free (service_pool); + service_pool = NULL; + } + return NULL; + } + } + + _dbus_string_get_const_data (service_name, &c_name); + + service = _dbus_hash_table_lookup_string (service_hash, + c_name); + if (service != NULL) + return service; + + if (!create_if_not_found) + return NULL; + + service = _dbus_mem_pool_alloc (service_pool); + if (service == NULL) + return NULL; + + service->name = _dbus_strdup (c_name); + if (service->name == NULL) + { + _dbus_mem_pool_dealloc (service_pool, service); + return NULL; + } + + if (!_dbus_hash_table_insert_string (service_hash, + service->name, + service)) + { + dbus_free (service->name); + _dbus_mem_pool_dealloc (service_pool, service); + return NULL; + } + + return service; +} + +dbus_bool_t +bus_service_add_owner (BusService *service, + DBusConnection *owner) +{ + if (!_dbus_list_append (&service->owners, + owner)) + return FALSE; + + if (!bus_connection_add_owned_service (owner, service)) + { + _dbus_list_remove_last (&service->owners, owner); + return FALSE; + } + + return TRUE; +} + +void +bus_service_remove_owner (BusService *service, + DBusConnection *owner) +{ + _dbus_list_remove_last (&service->owners, owner); + bus_connection_remove_owned_service (owner, service); +} + +DBusConnection* +bus_service_get_primary_owner (BusService *service) +{ + return _dbus_list_get_first (&service->owners); +} + +const char* +bus_service_get_name (BusService *service) +{ + return service->name; +} + +void +bus_service_foreach (BusServiceForeachFunction function, + void *data) +{ + DBusHashIter iter; + + if (service_hash == NULL) + return; + + _dbus_hash_iter_init (service_hash, &iter); + while (_dbus_hash_iter_next (&iter)) + { + BusService *service = _dbus_hash_iter_get_value (&iter); + + (* function) (service, data); + } +} diff --git a/bus/services.h b/bus/services.h new file mode 100644 index 00000000..56ed3d1b --- /dev/null +++ b/bus/services.h @@ -0,0 +1,53 @@ +/* -*- mode: C; c-file-style: "gnu" -*- */ +/* services.h Service management + * + * Copyright (C) 2003 Red Hat, Inc. + * + * Licensed under the Academic Free License version 1.2 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +#ifndef BUS_SERVICES_H +#define BUS_SERVICES_H + +#include <dbus/dbus.h> +#include <dbus/dbus-string.h> + +/* Each service can have multiple owners; one owner is the "real + * owner" and the others are queued up. For example, if I have + * multiple text editors open, one might own the TextEditor service; + * if I close that one, the next in line will become the owner of it. + */ + +typedef struct BusService BusService; + +typedef void (* BusServiceForeachFunction) (BusService *service, + void *data); + +BusService* bus_service_lookup (const DBusString *service_name, + dbus_bool_t create_if_not_found); +dbus_bool_t bus_service_add_owner (BusService *service, + DBusConnection *owner); +void bus_service_remove_owner (BusService *service, + DBusConnection *owner); +DBusConnection* bus_service_get_primary_owner (BusService *service); +const char* bus_service_get_name (BusService *service); + +void bus_service_foreach (BusServiceForeachFunction function, + void *data); + +#endif /* BUS_SERVICES_H */ |