1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-bus.c Convenience functions for communicating with the bus.
4 * Copyright (C) 2003 CodeFactory AB
5 * Copyright (C) 2003 Red Hat, Inc.
7 * Licensed under the Academic Free License version 1.2
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.
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.
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
26 #include "dbus-protocol.h"
27 #include "dbus-internals.h"
30 * @defgroup DBusBus Message bus APIs
32 * @brief Functions for communicating with the message bus
38 * @defgroup DBusBusInternals Message bus APIs internals
39 * @ingroup DBusInternals
40 * @brief Internals of functions for communicating with the message bus
46 * Block of message-bus-related data we attach to each
47 * #DBusConnection used with these convenience functions.
52 char *base_service; /**< Base service name of this connection */
54 DBusConnection **connection; /**< Pointer to bus_connections entry */
57 /** The slot we have reserved to store BusData
59 static int bus_data_slot = -1;
60 /** Number of connections using the slot
62 static int bus_data_slot_refcount = 0;
65 * Lock for bus_data_slot and bus_data_slot_refcount
67 _DBUS_DEFINE_GLOBAL_LOCK (bus);
75 if (bus_data_slot < 0)
77 bus_data_slot = dbus_connection_allocate_data_slot ();
79 if (bus_data_slot < 0)
85 _dbus_assert (bus_data_slot_refcount == 0);
88 bus_data_slot_refcount += 1;
96 data_slot_unref (void)
100 _dbus_assert (bus_data_slot_refcount > 0);
101 _dbus_assert (bus_data_slot >= 0);
103 bus_data_slot_refcount -= 1;
105 if (bus_data_slot_refcount == 0)
107 dbus_connection_free_data_slot (bus_data_slot);
115 bus_data_free (void *data)
120 *bd->connection = NULL;
122 dbus_free (bd->base_service);
129 ensure_bus_data (DBusConnection *connection)
133 if (!data_slot_ref ())
136 bd = dbus_connection_get_data (connection, bus_data_slot);
139 bd = dbus_new0 (BusData, 1);
146 if (!dbus_connection_set_data (connection, bus_data_slot, bd,
154 /* Data slot refcount now held by the BusData */
164 /** @} */ /* end of implementation details docs */
167 * @addtogroup DBusBus
171 /** Number of bus types */
174 static DBusConnection *bus_connections[BUS_TYPES];
177 * Connects to a bus daemon and registers the client with it.
178 * If a connection to the bus already exists, then that connection is returned.
180 * @todo alex thinks we should nullify the connection when we get a disconnect-message.
182 * @param type bus type
183 * @param error address where an error can be returned.
184 * @returns a DBusConnection
187 dbus_bus_get (DBusBusType type,
190 const char *name, *value;
191 DBusConnection *connection;
194 if (type <= 0 || type >= BUS_TYPES)
196 _dbus_assert_not_reached ("Invalid bus type specified.");
203 if (bus_connections[type] != NULL)
205 connection = bus_connections[type];
206 dbus_connection_ref (connection);
214 case DBUS_BUS_SESSION:
215 name = "DBUS_SESSION_BUS_ADDRESS";
217 case DBUS_BUS_SYSTEM:
218 name = "DBUS_SYSTEM_BUS_ADDRESS";
222 value = _dbus_getenv (name);
226 dbus_set_error (error, DBUS_ERROR_FAILED,
227 "Could not get bus daemon address.");
233 connection = dbus_connection_open (value, error);
241 if (!dbus_bus_register (connection, error))
243 dbus_connection_disconnect (connection);
244 dbus_connection_unref (connection);
250 bus_connections[type] = connection;
251 bd = ensure_bus_data (connection);
252 _dbus_assert (bd != NULL);
254 bd->connection = &bus_connections[type];
262 * Registers a connection with the bus. This must be the first
263 * thing an application does when connecting to the message bus.
264 * If registration succeeds, the base service name will be set,
265 * and can be obtained using dbus_bus_get_base_service().
267 * @todo if we get an error reply, it has to be converted into
268 * DBusError and returned
270 * @param connection the connection
271 * @param error place to store errors
272 * @returns #TRUE on success
275 dbus_bus_register (DBusConnection *connection,
278 DBusMessage *message, *reply;
282 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
284 bd = ensure_bus_data (connection);
287 _DBUS_SET_OOM (error);
291 if (bd->base_service != NULL)
293 _dbus_warn ("Attempt to register the same DBusConnection with the message bus, but it is already registered\n");
294 /* This isn't an error, it's a programming bug. We'll be nice
295 * and not _dbus_assert_not_reached()
300 message = dbus_message_new (DBUS_SERVICE_DBUS,
305 _DBUS_SET_OOM (error);
309 reply = dbus_connection_send_with_reply_and_block (connection, message, -1, error);
311 dbus_message_unref (message);
315 _DBUS_ASSERT_ERROR_IS_SET (error);
319 if (!dbus_message_get_args (reply, error,
320 DBUS_TYPE_STRING, &name,
323 _DBUS_ASSERT_ERROR_IS_SET (error);
327 bd->base_service = name;
334 * Sets the base service name of the connection.
335 * Can only be used if you registered with the
336 * bus manually (i.e. if you did not call
337 * dbus_bus_register()). Can only be called
338 * once per connection.
340 * @param connection the connection
341 * @param base_service the base service name
342 * @returns #FALSE if not enough memory
345 dbus_bus_set_base_service (DBusConnection *connection,
346 const char *base_service)
350 bd = ensure_bus_data (connection);
354 _dbus_assert (bd->base_service == NULL);
355 _dbus_assert (base_service != NULL);
357 bd->base_service = _dbus_strdup (base_service);
358 return bd->base_service != NULL;
362 * Gets the base service name of the connection.
363 * Only possible after the connection has been registered
364 * with the message bus.
366 * @param connection the connection
367 * @returns the base service name
370 dbus_bus_get_base_service (DBusConnection *connection)
374 bd = ensure_bus_data (connection);
378 return bd->base_service;
382 * Asks the bus to try to acquire a certain service.
384 * @todo these docs are not complete, need to document the
385 * return value and flags
387 * @todo if we get an error reply, it has to be converted into
388 * DBusError and returned
390 * @param connection the connection
391 * @param service_name the service name
393 * @param error location to store the error
394 * @returns a result code, -1 if error is set
397 dbus_bus_acquire_service (DBusConnection *connection,
398 const char *service_name,
402 DBusMessage *message, *reply;
405 message = dbus_message_new (DBUS_SERVICE_DBUS,
406 DBUS_MESSAGE_ACQUIRE_SERVICE);
410 _DBUS_SET_OOM (error);
414 if (!dbus_message_append_args (message,
415 DBUS_TYPE_STRING, service_name,
416 DBUS_TYPE_UINT32, flags,
419 dbus_message_unref (message);
420 _DBUS_SET_OOM (error);
424 reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
427 dbus_message_unref (message);
431 _DBUS_ASSERT_ERROR_IS_SET (error);
435 if (!dbus_message_get_args (reply, error,
436 DBUS_TYPE_UINT32, &service_result,
439 _DBUS_ASSERT_ERROR_IS_SET (error);
443 return service_result;
447 * Checks whether a certain service exists.
449 * @todo the SERVICE_EXISTS message should use BOOLEAN not UINT32
451 * @param connection the connection
452 * @param service_name the service name
453 * @param error location to store any errors
454 * @returns #TRUE if the service exists, #FALSE if not or on error
457 dbus_bus_service_exists (DBusConnection *connection,
458 const char *service_name,
461 DBusMessage *message, *reply;
464 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
466 message = dbus_message_new (DBUS_SERVICE_DBUS,
467 DBUS_MESSAGE_SERVICE_EXISTS);
470 _DBUS_SET_OOM (error);
474 if (!dbus_message_append_args (message,
475 DBUS_TYPE_STRING, service_name,
478 dbus_message_unref (message);
479 _DBUS_SET_OOM (error);
483 reply = dbus_connection_send_with_reply_and_block (connection, message, -1, error);
484 dbus_message_unref (message);
488 _DBUS_ASSERT_ERROR_IS_SET (error);
492 if (!dbus_message_get_args (reply, error,
493 DBUS_TYPE_UINT32, &exists,
496 _DBUS_ASSERT_ERROR_IS_SET (error);
500 return (exists != FALSE);