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.
56 /** The slot we have reserved to store BusData
58 static int bus_data_slot = -1;
59 /** Number of connections using the slot
61 static int bus_data_slot_refcount = 0;
64 * Lock for bus_data_slot and bus_data_slot_refcount
66 static DBusMutex *slot_lock;
69 * Initialize the mutex used for bus_data_slot
74 _dbus_bus_init_lock (void)
76 slot_lock = dbus_mutex_new ();
83 dbus_mutex_lock (slot_lock);
85 if (bus_data_slot < 0)
86 bus_data_slot = dbus_connection_allocate_data_slot ();
88 if (bus_data_slot < 0)
90 dbus_mutex_unlock (slot_lock);
94 bus_data_slot_refcount += 1;
96 dbus_mutex_unlock (slot_lock);
102 data_slot_unref (void)
104 dbus_mutex_lock (slot_lock);
106 _dbus_assert (bus_data_slot >= 0);
107 _dbus_assert (bus_data_slot_refcount > 0);
109 bus_data_slot_refcount -= 1;
111 if (bus_data_slot_refcount == 0)
113 dbus_connection_free_data_slot (bus_data_slot);
117 dbus_mutex_unlock (slot_lock);
121 bus_data_free (void *data)
125 dbus_free (bd->base_service);
132 ensure_bus_data (DBusConnection *connection)
136 if (!data_slot_ref ())
139 bd = dbus_connection_get_data (connection, bus_data_slot);
142 bd = dbus_new0 (BusData, 1);
149 dbus_connection_set_data (connection, bus_data_slot, bd,
152 /* Data slot refcount now held by the BusData */
162 /** @} */ /* end of implementation details docs */
165 * @addtogroup DBusBus
170 * Registers a connection with the bus. This must be the first
171 * thing an application does when connecting to the message bus.
172 * If registration succeeds, the base service name will be set,
173 * and can be obtained using dbus_bus_get_base_service().
175 * @todo if we get an error reply, it has to be converted into
176 * DBusError and returned
178 * @param connection the connection
179 * @param error place to store errors
180 * @returns #TRUE on success
183 dbus_bus_register (DBusConnection *connection,
186 DBusMessage *message, *reply;
190 bd = ensure_bus_data (connection);
193 _DBUS_SET_OOM (error);
197 if (bd->base_service != NULL)
199 _dbus_warn ("Attempt to register the same DBusConnection with the message bus, but it is already registered\n");
200 /* This isn't an error, it's a programming bug. We'll be nice
201 * and not _dbus_assert_not_reached()
206 message = dbus_message_new (DBUS_SERVICE_DBUS,
211 _DBUS_SET_OOM (error);
215 reply = dbus_connection_send_with_reply_and_block (connection, message, -1, error);
217 dbus_message_unref (message);
221 _DBUS_ASSERT_ERROR_IS_SET (error);
225 if (!dbus_message_get_args (reply, error,
226 DBUS_TYPE_STRING, &name,
229 _DBUS_ASSERT_ERROR_IS_SET (error);
233 bd->base_service = name;
240 * Sets the base service name of the connection.
241 * Can only be used if you registered with the
242 * bus manually (i.e. if you did not call
243 * dbus_bus_register()). Can only be called
244 * once per connection.
246 * @param connection the connection
247 * @param the base service name
248 * @returns #FALSE if not enough memory
251 dbus_bus_set_base_service (DBusConnection *connection,
252 const char *base_service)
256 bd = ensure_bus_data (connection);
260 _dbus_assert (bd->base_service == NULL);
261 _dbus_assert (base_service != NULL);
263 bd->base_service = _dbus_strdup (base_service);
264 return bd->base_service != NULL;
268 * Gets the base service name of the connection.
269 * Only possible after the connection has been registered
270 * with the message bus.
272 * @param connection the connection
273 * @returns the base service name
276 dbus_bus_get_base_service (DBusConnection *connection)
280 bd = ensure_bus_data (connection);
284 return bd->base_service;
288 * Asks the bus to try to acquire a certain service.
290 * @todo these docs are not complete, need to document the
291 * return value and flags
293 * @todo if we get an error reply, it has to be converted into
294 * DBusError and returned
296 * @param connection the connection
297 * @param service_name the service name
299 * @param error location to store the error
300 * @returns a result code, -1 if error is set
303 dbus_bus_acquire_service (DBusConnection *connection,
304 const char *service_name,
308 DBusMessage *message, *reply;
311 message = dbus_message_new (DBUS_SERVICE_DBUS,
312 DBUS_MESSAGE_ACQUIRE_SERVICE);
316 _DBUS_SET_OOM (error);
320 if (!dbus_message_append_args (message,
321 DBUS_TYPE_STRING, service_name,
322 DBUS_TYPE_UINT32, flags,
325 dbus_message_unref (message);
326 _DBUS_SET_OOM (error);
330 reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
333 dbus_message_unref (message);
337 _DBUS_ASSERT_ERROR_IS_SET (error);
341 if (!dbus_message_get_args (reply, error,
342 DBUS_TYPE_UINT32, &service_result,
345 _DBUS_ASSERT_ERROR_IS_SET (error);
349 return service_result;
353 * Checks whether a certain service exists.
355 * @todo the SERVICE_EXISTS message should use BOOLEAN not UINT32
357 * @param connection the connection
358 * @param service_name the service name
359 * @param error location to store any errors
360 * @returns #TRUE if the service exists, #FALSE if not or on error
363 dbus_bus_service_exists (DBusConnection *connection,
364 const char *service_name,
367 DBusMessage *message, *reply;
370 message = dbus_message_new (DBUS_SERVICE_DBUS,
371 DBUS_MESSAGE_SERVICE_EXISTS);
374 _DBUS_SET_OOM (error);
378 if (!dbus_message_append_args (message,
379 DBUS_TYPE_STRING, service_name,
382 dbus_message_unref (message);
383 _DBUS_SET_OOM (error);
387 reply = dbus_connection_send_with_reply_and_block (connection, message, -1, error);
388 dbus_message_unref (message);
392 _DBUS_ASSERT_ERROR_IS_SET (error);
396 if (!dbus_message_get_args (reply, error,
397 DBUS_TYPE_UINT32, &exists,
400 _DBUS_ASSERT_ERROR_IS_SET (error);
404 return (exists != FALSE);