2003-03-12 Havoc Pennington <hp@pobox.com>
[platform/upstream/dbus.git] / bus / services.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* services.c  Service management
3  *
4  * Copyright (C) 2003  Red Hat, Inc.
5  * Copyright (C) 2003  CodeFactory AB
6  *
7  * Licensed under the Academic Free License version 1.2
8  * 
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  * 
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  *
23  */
24 #include <dbus/dbus-hash.h>
25 #include <dbus/dbus-list.h>
26 #include <dbus/dbus-mempool.h>
27
28 #include "driver.h"
29 #include "services.h"
30 #include "connection.h"
31 #include "utils.h"
32
33 struct BusService
34 {
35   BusRegistry *registry;
36   char *name;
37   DBusList *owners;
38   
39   unsigned int prohibit_replacement : 1;
40 };
41
42 struct BusRegistry
43 {
44   int refcount;
45   
46   DBusHashTable *service_hash;
47   DBusMemPool   *service_pool;
48 };
49
50 BusRegistry*
51 bus_registry_new (void)
52 {
53   BusRegistry *registry;
54
55   registry = dbus_new0 (BusRegistry, 1);
56   if (registry == NULL)
57     return NULL;
58
59   registry->refcount = 1;
60
61   registry->service_hash = _dbus_hash_table_new (DBUS_HASH_STRING,
62                                                  NULL, NULL);
63   if (registry->service_hash == NULL)
64     goto failed;
65   
66   registry->service_pool = _dbus_mem_pool_new (sizeof (BusService),
67                                                TRUE);
68   if (registry->service_pool == NULL)
69     goto failed;
70
71   return registry;
72
73  failed:
74   bus_registry_unref (registry);
75   return NULL;
76 }
77
78 void
79 bus_registry_ref (BusRegistry *registry)
80 {
81   _dbus_assert (registry->refcount > 0);
82   registry->refcount += 1;
83 }
84
85 void
86 bus_registry_unref  (BusRegistry *registry)
87 {
88   _dbus_assert (registry->refcount > 0);
89   registry->refcount -= 1;
90
91   if (registry->refcount == 0)
92     {
93       if (registry->service_hash)
94         _dbus_hash_table_unref (registry->service_hash);
95       if (registry->service_pool)
96         _dbus_mem_pool_free (registry->service_pool);
97
98       dbus_free (registry);
99     }
100 }
101
102 BusService*
103 bus_registry_lookup (BusRegistry      *registry,
104                      const DBusString *service_name)
105 {
106   const char *c_name;
107   BusService *service;
108   
109   _dbus_string_get_const_data (service_name, &c_name);
110
111   service = _dbus_hash_table_lookup_string (registry->service_hash,
112                                             c_name);
113
114   return service;
115 }
116
117 BusService*
118 bus_registry_ensure (BusRegistry               *registry,
119                      const DBusString          *service_name,
120                      DBusConnection            *owner_if_created,
121                      BusTransaction            *transaction,
122                      DBusError                 *error)
123 {
124   const char *c_name;
125   BusService *service;
126
127   _dbus_assert (owner_if_created != NULL);
128   _dbus_assert (transaction != NULL);
129   
130   _dbus_string_get_const_data (service_name, &c_name);
131
132   service = _dbus_hash_table_lookup_string (registry->service_hash,
133                                             c_name);
134   if (service != NULL)
135     return service;
136   
137   service = _dbus_mem_pool_alloc (registry->service_pool);
138   if (service == NULL)
139     {
140       BUS_SET_OOM (error);
141       return NULL;
142     }
143
144   service->registry = registry;  
145
146   service->name = _dbus_strdup (c_name);
147   if (service->name == NULL)
148     {
149       _dbus_mem_pool_dealloc (registry->service_pool, service);
150       BUS_SET_OOM (error);
151       return NULL;
152     }
153
154   if (!bus_driver_send_service_created (service->name, transaction, error))
155     {
156       dbus_free (service->name);
157       _dbus_mem_pool_dealloc (registry->service_pool, service);
158       return NULL;
159     }
160
161   if (!bus_service_add_owner (service, owner_if_created,
162                               transaction, error))
163     {
164       dbus_free (service->name);
165       _dbus_mem_pool_dealloc (registry->service_pool, service);
166       return NULL;
167     }
168   
169   if (!_dbus_hash_table_insert_string (registry->service_hash,
170                                        service->name,
171                                        service))
172     {
173       _dbus_list_clear (&service->owners);
174       dbus_free (service->name);
175       _dbus_mem_pool_dealloc (registry->service_pool, service);
176       BUS_SET_OOM (error);
177       return NULL;
178     }
179   
180   return service;
181 }
182
183 void
184 bus_registry_foreach (BusRegistry               *registry,
185                       BusServiceForeachFunction  function,
186                       void                      *data)
187 {
188   DBusHashIter iter;
189   
190   _dbus_hash_iter_init (registry->service_hash, &iter);
191   while (_dbus_hash_iter_next (&iter))
192     {
193       BusService *service = _dbus_hash_iter_get_value (&iter);
194
195       (* function) (service, data);
196     }
197 }
198
199 dbus_bool_t
200 bus_registry_list_services (BusRegistry *registry,
201                             char      ***listp,
202                             int         *array_len)
203 {
204   int i, j, len;
205   char **retval;
206   DBusHashIter iter;
207    
208   len = _dbus_hash_table_get_n_entries (registry->service_hash);
209   retval = dbus_new (char *, len + 1);
210
211   if (retval == NULL)
212     return FALSE;
213
214   _dbus_hash_iter_init (registry->service_hash, &iter);
215   i = 0;
216   while (_dbus_hash_iter_next (&iter))
217     {
218       BusService *service = _dbus_hash_iter_get_value (&iter);
219
220       retval[i] = _dbus_strdup (service->name);
221       if (retval[i] == NULL)
222         goto error;
223
224       i++;
225     }
226
227   retval[i] = NULL;
228   
229   if (array_len)
230     *array_len = len;
231   
232   *listp = retval;
233   return TRUE;
234   
235  error:
236   for (j = 0; j < i; j++)
237     dbus_free (retval[i]);
238   dbus_free (retval);
239
240   return FALSE;
241 }
242
243 dbus_bool_t
244 bus_service_add_owner (BusService     *service,
245                        DBusConnection *owner,
246                        BusTransaction *transaction,
247                        DBusError      *error)
248 {
249  /* Send service acquired message first, OOM will result
250   * in cancelling the transaction
251   */
252   if (service->owners == NULL)
253     {
254       if (!bus_driver_send_service_acquired (owner, service->name, transaction, error))
255         return FALSE;
256     }
257   
258   if (!_dbus_list_append (&service->owners,
259                           owner))
260     {
261       BUS_SET_OOM (error);
262       return FALSE;
263     }
264
265   if (!bus_connection_add_owned_service (owner, service))
266     {
267       _dbus_list_remove_last (&service->owners, owner);
268       BUS_SET_OOM (error);
269       return FALSE;
270     }
271   
272   return TRUE;
273 }
274
275 dbus_bool_t
276 bus_service_remove_owner (BusService     *service,
277                           DBusConnection *owner,
278                           BusTransaction *transaction,
279                           DBusError      *error)
280 {
281   /* We send out notifications before we do any work we
282    * might have to undo if the notification-sending failed
283    */
284   
285   /* Send service lost message */
286   if (bus_service_get_primary_owner (service) == owner)
287     {
288       if (!bus_driver_send_service_lost (owner, service->name,
289                                          transaction, error))
290         return FALSE;
291     }
292
293   if (_dbus_list_length_is_one (&service->owners))
294     {
295       /* We are the only owner - send service deleted */
296       if (!bus_driver_send_service_deleted (service->name,
297                                             transaction, error))
298         return FALSE;
299     }
300   else
301     {
302       DBusList *link;
303       link = _dbus_list_get_first (&service->owners);
304       link = _dbus_list_get_next_link (&service->owners, link);
305
306       if (link != NULL)
307         {
308           /* This will be our new owner */
309           if (!bus_driver_send_service_acquired (link->data,
310                                                  service->name,
311                                                  transaction,
312                                                  error))
313             return FALSE;
314         }
315     }
316   
317   _dbus_list_remove_last (&service->owners, owner);
318   bus_connection_remove_owned_service (owner, service);
319
320   if (service->owners == NULL)
321     {
322       /* Delete service (already sent message that it was deleted above) */
323       _dbus_hash_table_remove_string (service->registry->service_hash, service->name);
324       
325       dbus_free (service->name);
326       _dbus_mem_pool_dealloc (service->registry->service_pool, service);
327     }
328
329   return TRUE;
330 }
331
332 DBusConnection*
333 bus_service_get_primary_owner (BusService *service)
334 {
335   return _dbus_list_get_first (&service->owners);
336 }
337
338 const char*
339 bus_service_get_name (BusService *service)
340 {
341   return service->name;
342 }
343
344 void
345 bus_service_set_prohibit_replacement (BusService  *service,
346                                       dbus_bool_t  prohibit_replacement)
347 {
348   service->prohibit_replacement = prohibit_replacement != FALSE;
349 }
350
351 dbus_bool_t
352 bus_service_get_prohibit_replacement (BusService *service)
353 {
354   return service->prohibit_replacement;
355 }
356
357 dbus_bool_t
358 bus_service_has_owner (BusService     *service,
359                        DBusConnection *owner)
360 {
361   DBusList *link;
362
363   link = _dbus_list_get_first_link (&service->owners);
364   
365   while (link != NULL)
366     {
367       if (link->data == owner)
368         return TRUE;
369       
370       link = _dbus_list_get_next_link (&service->owners, link);
371     }
372
373   return FALSE;
374 }