2003-03-31 Havoc Pennington <hp@redhat.com>
[platform/upstream/dbus.git] / dbus / dbus-bus.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-bus.c  Convenience functions for communicating with the bus.
3  *
4  * Copyright (C) 2003  CodeFactory AB
5  * Copyright (C) 2003  Red Hat, Inc.
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
25 #include "dbus-bus.h"
26 #include "dbus-protocol.h"
27 #include "dbus-internals.h"
28
29 /**
30  * @defgroup DBusBus Message bus APIs
31  * @ingroup DBus
32  * @brief Functions for communicating with the message bus
33  *
34  */
35
36
37 /**
38  * @defgroup DBusBusInternals Message bus APIs internals
39  * @ingroup DBusInternals
40  * @brief Internals of functions for communicating with the message bus
41  *
42  * @{
43  */
44
45 /**
46  * Block of message-bus-related data we attach to each
47  * #DBusConnection used with these convenience functions.
48  *
49  */
50 typedef struct
51 {
52   char *base_service; /**< Base service name of this connection */
53
54   DBusConnection **connection; /**< Pointer to bus_connections entry */
55 } BusData;
56
57 /** The slot we have reserved to store BusData
58  */
59 static int bus_data_slot = -1;
60 /** Number of connections using the slot
61  */
62 static int bus_data_slot_refcount = 0;
63
64 /**
65  * Lock for bus_data_slot and bus_data_slot_refcount
66  */
67 _DBUS_DEFINE_GLOBAL_LOCK (bus);
68
69
70 static dbus_bool_t
71 data_slot_ref (void)
72 {
73   _DBUS_LOCK (bus);
74
75   if (bus_data_slot < 0)
76     {
77       bus_data_slot = dbus_connection_allocate_data_slot ();
78       
79       if (bus_data_slot < 0)
80         {
81           _DBUS_UNLOCK (bus);
82           return FALSE;
83         }
84
85       _dbus_assert (bus_data_slot_refcount == 0);
86     }
87
88   bus_data_slot_refcount += 1;
89
90   _DBUS_UNLOCK (bus);
91
92   return TRUE;
93 }
94
95 static void
96 data_slot_unref (void)
97 {
98   _DBUS_LOCK (bus);
99
100   _dbus_assert (bus_data_slot_refcount > 0);
101   _dbus_assert (bus_data_slot >= 0);
102
103   bus_data_slot_refcount -= 1;
104
105   if (bus_data_slot_refcount == 0)
106     {
107       dbus_connection_free_data_slot (bus_data_slot);
108       bus_data_slot = -1;
109     }
110
111   _DBUS_UNLOCK (bus);
112 }
113
114 static void
115 bus_data_free (void *data)
116 {
117   BusData *bd = data;
118
119   if (bd->connection)
120     {
121       _DBUS_LOCK (bus);
122       *bd->connection = NULL;
123       _DBUS_UNLOCK (bus);
124     }
125   
126   dbus_free (bd->base_service);
127   dbus_free (bd);
128
129   data_slot_unref ();
130 }
131
132 static BusData*
133 ensure_bus_data (DBusConnection *connection)
134 {
135   BusData *bd;
136
137   if (!data_slot_ref ())
138     return NULL;
139
140   bd = dbus_connection_get_data (connection, bus_data_slot);
141   if (bd == NULL)
142     {      
143       bd = dbus_new0 (BusData, 1);
144       if (bd == NULL)
145         {
146           data_slot_unref ();
147           return NULL;
148         }
149       
150       if (!dbus_connection_set_data (connection, bus_data_slot, bd,
151                                      bus_data_free))
152         {
153           dbus_free (bd);
154           data_slot_unref ();
155           return NULL;
156         }
157
158       /* Data slot refcount now held by the BusData */
159     }
160   else
161     {
162       data_slot_unref ();
163     }
164
165   return bd;
166 }
167
168 /** @} */ /* end of implementation details docs */
169
170 /**
171  * @addtogroup DBusBus
172  * @{
173  */
174
175 /** Number of bus types */
176 #define BUS_TYPES 2
177
178 static DBusConnection *bus_connections[BUS_TYPES];
179
180 /**
181  * Connects to a bus daemon and registers the client with it.
182  * If a connection to the bus already exists, then that connection is returned.
183  *
184  * @todo alex thinks we should nullify the connection when we get a disconnect-message.
185  *
186  * @param type bus type
187  * @param error address where an error can be returned.
188  * @returns a DBusConnection
189  */
190 DBusConnection *
191 dbus_bus_get (DBusBusType  type,
192               DBusError   *error)
193 {
194   const char *name, *value;
195   DBusConnection *connection;
196   BusData *bd;
197
198   if (type <= 0 || type >= BUS_TYPES)
199     {
200       _dbus_assert_not_reached ("Invalid bus type specified.");
201
202       return NULL;
203     }
204
205   _DBUS_LOCK (bus);
206   
207   if (bus_connections[type] != NULL)
208     {
209       connection = bus_connections[type];
210       dbus_connection_ref (connection);
211       
212       _DBUS_UNLOCK (bus);
213       return connection;
214     }
215
216   switch (type)
217     {
218     case DBUS_BUS_SESSION:
219       name = "DBUS_SESSION_BUS_ADDRESS";
220       break;
221     case DBUS_BUS_SYSTEM:
222       name = "DBUS_SYSTEM_BUS_ADDRESS";
223       break;
224     }
225
226   value = _dbus_getenv (name);
227
228   if (type == DBUS_BUS_SYSTEM &&
229       (value == NULL || *value == '\0'))
230     {
231       /* Use default system bus address if none set */
232       value = "unix:path=" DBUS_SYSTEM_BUS_PATH;
233     }
234   
235   if (value == NULL || *value == '\0')
236     {
237       dbus_set_error (error, DBUS_ERROR_FAILED,
238                       "Environment variable %s not set, address of message bus unknown",
239                       name);
240       _DBUS_UNLOCK (bus);
241       
242       return NULL;
243     }
244
245   connection = dbus_connection_open (value, error);
246   
247   if (!connection)
248     {
249       _DBUS_UNLOCK (bus);
250       return NULL;
251     }
252   
253   if (!dbus_bus_register (connection, error))
254     {
255       dbus_connection_disconnect (connection);
256       dbus_connection_unref (connection);
257
258       _DBUS_UNLOCK (bus);
259       return NULL;
260     }
261
262   bus_connections[type] = connection;
263   bd = ensure_bus_data (connection);
264   _dbus_assert (bd != NULL);
265
266   bd->connection = &bus_connections[type];
267
268   _DBUS_UNLOCK (bus);  
269   return connection;
270 }
271
272
273 /**
274  * Registers a connection with the bus. This must be the first
275  * thing an application does when connecting to the message bus.
276  * If registration succeeds, the base service name will be set,
277  * and can be obtained using dbus_bus_get_base_service().
278  *
279  * @todo if we get an error reply, it has to be converted into
280  * DBusError and returned
281  * 
282  * @param connection the connection
283  * @param error place to store errors
284  * @returns #TRUE on success
285  */
286 dbus_bool_t
287 dbus_bus_register (DBusConnection *connection,
288                    DBusError      *error)
289 {
290   DBusMessage *message, *reply;
291   char *name;
292   BusData *bd;
293
294   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
295   
296   bd = ensure_bus_data (connection);
297   if (bd == NULL)
298     {
299       _DBUS_SET_OOM (error);
300       return FALSE;
301     }
302
303   if (bd->base_service != NULL)
304     {
305       _dbus_warn ("Attempt to register the same DBusConnection with the message bus, but it is already registered\n");
306       /* This isn't an error, it's a programming bug. We'll be nice
307        * and not _dbus_assert_not_reached()
308        */
309       return TRUE;
310     }
311   
312   message = dbus_message_new (DBUS_SERVICE_DBUS,
313                               DBUS_MESSAGE_HELLO);
314
315   if (!message)
316     {
317       _DBUS_SET_OOM (error);
318       return FALSE;
319     }
320   
321   reply = dbus_connection_send_with_reply_and_block (connection, message, -1, error);
322
323   dbus_message_unref (message);
324   
325   if (reply == NULL)
326     {
327       _DBUS_ASSERT_ERROR_IS_SET (error);
328       return FALSE;
329     }
330
331   if (!dbus_message_get_args (reply, error,
332                               DBUS_TYPE_STRING, &name,
333                               0))
334     {
335       _DBUS_ASSERT_ERROR_IS_SET (error);
336       return FALSE;
337     }
338
339   bd->base_service = name;
340   
341   return TRUE;
342 }
343
344
345 /**
346  * Sets the base service name of the connection.
347  * Can only be used if you registered with the
348  * bus manually (i.e. if you did not call
349  * dbus_bus_register()). Can only be called
350  * once per connection.
351  *
352  * @param connection the connection
353  * @param base_service the base service name
354  * @returns #FALSE if not enough memory
355  */
356 dbus_bool_t
357 dbus_bus_set_base_service (DBusConnection *connection,
358                            const char     *base_service)
359 {
360   BusData *bd;
361
362   bd = ensure_bus_data (connection);
363   if (bd == NULL)
364     return FALSE;
365
366   _dbus_assert (bd->base_service == NULL);
367   _dbus_assert (base_service != NULL);
368   
369   bd->base_service = _dbus_strdup (base_service);
370   return bd->base_service != NULL;
371 }
372
373 /**
374  * Gets the base service name of the connection.
375  * Only possible after the connection has been registered
376  * with the message bus.
377  *
378  * @param connection the connection
379  * @returns the base service name
380  */
381 const char*
382 dbus_bus_get_base_service (DBusConnection *connection)
383 {
384   BusData *bd;
385
386   bd = ensure_bus_data (connection);
387   if (bd == NULL)
388     return NULL;
389   
390   return bd->base_service;
391 }
392
393 /**
394  * Asks the bus to try to acquire a certain service.
395  *
396  * @todo these docs are not complete, need to document the
397  * return value and flags
398  * 
399  * @todo if we get an error reply, it has to be converted into
400  * DBusError and returned
401  *
402  * @param connection the connection
403  * @param service_name the service name
404  * @param flags flags
405  * @param error location to store the error
406  * @returns a result code, -1 if error is set
407  */ 
408 int
409 dbus_bus_acquire_service (DBusConnection *connection,
410                           const char     *service_name,
411                           unsigned int    flags,
412                           DBusError      *error)
413 {
414   DBusMessage *message, *reply;
415   int service_result;
416   
417   message = dbus_message_new (DBUS_SERVICE_DBUS,
418                               DBUS_MESSAGE_ACQUIRE_SERVICE);
419
420   if (message == NULL)
421     {
422       _DBUS_SET_OOM (error);
423       return -1;
424     }
425  
426   if (!dbus_message_append_args (message,
427                                  DBUS_TYPE_STRING, service_name,
428                                  DBUS_TYPE_UINT32, flags,
429                                  0))
430     {
431       dbus_message_unref (message);
432       _DBUS_SET_OOM (error);
433       return -1;
434     }
435   
436   reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
437                                                      error);
438   
439   dbus_message_unref (message);
440   
441   if (reply == NULL)
442     {
443       _DBUS_ASSERT_ERROR_IS_SET (error);
444       return -1;
445     }
446
447   if (!dbus_message_get_args (reply, error,
448                               DBUS_TYPE_UINT32, &service_result,
449                               0))
450     {
451       _DBUS_ASSERT_ERROR_IS_SET (error);
452       return -1;
453     }
454
455   return service_result;
456 }
457
458 /**
459  * Checks whether a certain service exists.
460  *
461  * @todo the SERVICE_EXISTS message should use BOOLEAN not UINT32
462  *
463  * @param connection the connection
464  * @param service_name the service name
465  * @param error location to store any errors
466  * @returns #TRUE if the service exists, #FALSE if not or on error
467  */
468 dbus_bool_t
469 dbus_bus_service_exists (DBusConnection *connection,
470                          const char     *service_name,
471                          DBusError      *error)
472 {
473   DBusMessage *message, *reply;
474   unsigned int exists;
475
476   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
477   
478   message = dbus_message_new (DBUS_SERVICE_DBUS,
479                               DBUS_MESSAGE_SERVICE_EXISTS);
480   if (message == NULL)
481     {
482       _DBUS_SET_OOM (error);
483       return FALSE;
484     }
485   
486   if (!dbus_message_append_args (message,
487                                  DBUS_TYPE_STRING, service_name,
488                                  0))
489     {
490       dbus_message_unref (message);
491       _DBUS_SET_OOM (error);
492       return FALSE;
493     }
494   
495   reply = dbus_connection_send_with_reply_and_block (connection, message, -1, error);
496   dbus_message_unref (message);
497
498   if (reply == NULL)
499     {
500       _DBUS_ASSERT_ERROR_IS_SET (error);
501       return FALSE;
502     }
503
504   if (!dbus_message_get_args (reply, error,
505                               DBUS_TYPE_UINT32, &exists,
506                               0))
507     {
508       _DBUS_ASSERT_ERROR_IS_SET (error);
509       return FALSE;
510     }
511   
512   return (exists != FALSE);
513 }
514
515 /** @} */