1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* services.c Service management
4 * Copyright (C) 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 "connection.h"
25 #include <dbus/dbus-hash.h>
26 #include <dbus/dbus-list.h>
27 #include <dbus/dbus-mempool.h>
35 static DBusHashTable *service_hash = NULL;
36 static DBusMemPool *service_pool = NULL;
39 bus_service_lookup (const DBusString *service_name,
40 dbus_bool_t create_if_not_found)
45 if (service_hash == NULL)
47 service_hash = _dbus_hash_table_new (DBUS_HASH_STRING,
49 service_pool = _dbus_mem_pool_new (sizeof (BusService),
52 if (service_hash == NULL || service_pool == NULL)
56 _dbus_hash_table_unref (service_hash);
61 _dbus_mem_pool_free (service_pool);
68 _dbus_string_get_const_data (service_name, &c_name);
70 service = _dbus_hash_table_lookup_string (service_hash,
75 if (!create_if_not_found)
78 service = _dbus_mem_pool_alloc (service_pool);
82 service->name = _dbus_strdup (c_name);
83 if (service->name == NULL)
85 _dbus_mem_pool_dealloc (service_pool, service);
89 if (!_dbus_hash_table_insert_string (service_hash,
93 dbus_free (service->name);
94 _dbus_mem_pool_dealloc (service_pool, service);
102 bus_service_free (BusService *service)
104 _dbus_assert (service->owners == NULL);
106 _dbus_hash_table_remove_string (service_hash, service->name);
108 dbus_free (service->name);
109 _dbus_mem_pool_dealloc (service_pool, service);
113 bus_service_add_owner (BusService *service,
114 DBusConnection *owner)
116 if (!_dbus_list_append (&service->owners,
120 if (!bus_connection_add_owned_service (owner, service))
122 _dbus_list_remove_last (&service->owners, owner);
130 bus_service_remove_owner (BusService *service,
131 DBusConnection *owner)
133 _dbus_list_remove_last (&service->owners, owner);
134 bus_connection_remove_owned_service (owner, service);
138 bus_service_get_primary_owner (BusService *service)
140 return _dbus_list_get_first (&service->owners);
144 bus_service_get_name (BusService *service)
146 return service->name;
150 bus_service_foreach (BusServiceForeachFunction function,
155 if (service_hash == NULL)
158 _dbus_hash_iter_init (service_hash, &iter);
159 while (_dbus_hash_iter_next (&iter))
161 BusService *service = _dbus_hash_iter_get_value (&iter);
163 (* function) (service, data);
168 bus_services_list (int *array_len)
174 len = _dbus_hash_table_get_n_entries (service_hash);
175 retval = dbus_new (char *, len);
180 _dbus_hash_iter_init (service_hash, &iter);
182 while (_dbus_hash_iter_next (&iter))
184 BusService *service = _dbus_hash_iter_get_value (&iter);
186 retval[i] = _dbus_strdup (service->name);
187 if (retval[i] == NULL)
199 for (j = 0; j < i; j++)
200 dbus_free (retval[i]);