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 */
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 _DBUS_DEFINE_GLOBAL_LOCK (bus);
73 if (bus_data_slot < 0)
75 bus_data_slot = dbus_connection_allocate_data_slot ();
77 if (bus_data_slot < 0)
83 _dbus_assert (bus_data_slot_refcount == 0);
86 bus_data_slot_refcount += 1;
94 data_slot_unref (void)
98 _dbus_assert (bus_data_slot_refcount > 0);
99 _dbus_assert (bus_data_slot >= 0);
101 bus_data_slot_refcount -= 1;
103 if (bus_data_slot_refcount == 0)
105 dbus_connection_free_data_slot (bus_data_slot);
113 bus_data_free (void *data)
117 dbus_free (bd->base_service);
124 ensure_bus_data (DBusConnection *connection)
128 if (!data_slot_ref ())
131 bd = dbus_connection_get_data (connection, bus_data_slot);
134 bd = dbus_new0 (BusData, 1);
141 if (!dbus_connection_set_data (connection, bus_data_slot, bd,
149 /* Data slot refcount now held by the BusData */
159 /** @} */ /* end of implementation details docs */
162 * @addtogroup DBusBus
167 * Registers a connection with the bus. This must be the first
168 * thing an application does when connecting to the message bus.
169 * If registration succeeds, the base service name will be set,
170 * and can be obtained using dbus_bus_get_base_service().
172 * @todo if we get an error reply, it has to be converted into
173 * DBusError and returned
175 * @param connection the connection
176 * @param error place to store errors
177 * @returns #TRUE on success
180 dbus_bus_register (DBusConnection *connection,
183 DBusMessage *message, *reply;
187 bd = ensure_bus_data (connection);
190 _DBUS_SET_OOM (error);
194 if (bd->base_service != NULL)
196 _dbus_warn ("Attempt to register the same DBusConnection with the message bus, but it is already registered\n");
197 /* This isn't an error, it's a programming bug. We'll be nice
198 * and not _dbus_assert_not_reached()
203 message = dbus_message_new (DBUS_SERVICE_DBUS,
208 _DBUS_SET_OOM (error);
212 reply = dbus_connection_send_with_reply_and_block (connection, message, -1, error);
214 dbus_message_unref (message);
218 _DBUS_ASSERT_ERROR_IS_SET (error);
222 if (!dbus_message_get_args (reply, error,
223 DBUS_TYPE_STRING, &name,
226 _DBUS_ASSERT_ERROR_IS_SET (error);
230 bd->base_service = name;
237 * Sets the base service name of the connection.
238 * Can only be used if you registered with the
239 * bus manually (i.e. if you did not call
240 * dbus_bus_register()). Can only be called
241 * once per connection.
243 * @param connection the connection
244 * @param base_service the base service name
245 * @returns #FALSE if not enough memory
248 dbus_bus_set_base_service (DBusConnection *connection,
249 const char *base_service)
253 bd = ensure_bus_data (connection);
257 _dbus_assert (bd->base_service == NULL);
258 _dbus_assert (base_service != NULL);
260 bd->base_service = _dbus_strdup (base_service);
261 return bd->base_service != NULL;
265 * Gets the base service name of the connection.
266 * Only possible after the connection has been registered
267 * with the message bus.
269 * @param connection the connection
270 * @returns the base service name
273 dbus_bus_get_base_service (DBusConnection *connection)
277 bd = ensure_bus_data (connection);
281 return bd->base_service;
285 * Asks the bus to try to acquire a certain service.
287 * @todo these docs are not complete, need to document the
288 * return value and flags
290 * @todo if we get an error reply, it has to be converted into
291 * DBusError and returned
293 * @param connection the connection
294 * @param service_name the service name
296 * @param error location to store the error
297 * @returns a result code, -1 if error is set
300 dbus_bus_acquire_service (DBusConnection *connection,
301 const char *service_name,
305 DBusMessage *message, *reply;
308 message = dbus_message_new (DBUS_SERVICE_DBUS,
309 DBUS_MESSAGE_ACQUIRE_SERVICE);
313 _DBUS_SET_OOM (error);
317 if (!dbus_message_append_args (message,
318 DBUS_TYPE_STRING, service_name,
319 DBUS_TYPE_UINT32, flags,
322 dbus_message_unref (message);
323 _DBUS_SET_OOM (error);
327 reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
330 dbus_message_unref (message);
334 _DBUS_ASSERT_ERROR_IS_SET (error);
338 if (!dbus_message_get_args (reply, error,
339 DBUS_TYPE_UINT32, &service_result,
342 _DBUS_ASSERT_ERROR_IS_SET (error);
346 return service_result;
350 * Checks whether a certain service exists.
352 * @todo the SERVICE_EXISTS message should use BOOLEAN not UINT32
354 * @param connection the connection
355 * @param service_name the service name
356 * @param error location to store any errors
357 * @returns #TRUE if the service exists, #FALSE if not or on error
360 dbus_bus_service_exists (DBusConnection *connection,
361 const char *service_name,
364 DBusMessage *message, *reply;
367 message = dbus_message_new (DBUS_SERVICE_DBUS,
368 DBUS_MESSAGE_SERVICE_EXISTS);
371 _DBUS_SET_OOM (error);
375 if (!dbus_message_append_args (message,
376 DBUS_TYPE_STRING, service_name,
379 dbus_message_unref (message);
380 _DBUS_SET_OOM (error);
384 reply = dbus_connection_send_with_reply_and_block (connection, message, -1, error);
385 dbus_message_unref (message);
389 _DBUS_ASSERT_ERROR_IS_SET (error);
393 if (!dbus_message_get_args (reply, error,
394 DBUS_TYPE_UINT32, &exists,
397 _DBUS_ASSERT_ERROR_IS_SET (error);
401 return (exists != FALSE);