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"
31 * @defgroup DBusBus Message bus APIs
33 * @brief Functions for communicating with the message bus
39 * @defgroup DBusBusInternals Message bus APIs internals
40 * @ingroup DBusInternals
41 * @brief Internals of functions for communicating with the message bus
47 * Block of message-bus-related data we attach to each
48 * #DBusConnection used with these convenience functions.
53 DBusConnection *connection; /**< Connection we're associated with */
54 char *base_service; /**< Base service name of this connection */
56 unsigned int is_well_known : 1; /**< Is one of the well-known connections in our global array */
59 /** The slot we have reserved to store BusData
61 static int bus_data_slot = -1;
62 /** Number of connections using the slot
64 static int bus_data_slot_refcount = 0;
66 /** Number of bus types */
69 static DBusConnection *bus_connections[N_BUS_TYPES];
70 static char *bus_connection_addresses[N_BUS_TYPES] = { NULL, NULL, NULL };
72 static DBusBusType activation_bus_type = DBUS_BUS_ACTIVATION;
74 static dbus_bool_t initialized = FALSE;
77 * Lock for globals in this file
79 _DBUS_DEFINE_GLOBAL_LOCK (bus);
82 addresses_shutdown_func (void *data)
87 while (i < N_BUS_TYPES)
89 if (bus_connections[i] != NULL)
90 _dbus_warn ("dbus_shutdown() called but connections were still live!");
92 dbus_free (bus_connection_addresses[i]);
93 bus_connection_addresses[i] = NULL;
97 activation_bus_type = DBUS_BUS_ACTIVATION;
101 get_from_env (char **connection_p,
106 _dbus_assert (*connection_p == NULL);
108 s = _dbus_getenv (env_var);
109 if (s == NULL || *s == '\0')
110 return TRUE; /* successfully didn't use the env var */
113 *connection_p = _dbus_strdup (s);
114 return *connection_p != NULL;
119 init_connections_unlocked (void)
127 while (i < N_BUS_TYPES)
129 bus_connections[i] = NULL;
133 /* Don't init these twice, we may run this code twice if
134 * init_connections_unlocked() fails midway through.
137 if (bus_connection_addresses[DBUS_BUS_SYSTEM] == NULL)
139 if (!get_from_env (&bus_connection_addresses[DBUS_BUS_SYSTEM],
140 "DBUS_SYSTEM_BUS_ADDRESS"))
143 if (bus_connection_addresses[DBUS_BUS_SYSTEM] == NULL)
145 /* Use default system bus address if none set in environment */
146 bus_connection_addresses[DBUS_BUS_SYSTEM] =
147 _dbus_strdup ("unix:path=" DBUS_SYSTEM_BUS_PATH);
148 if (bus_connection_addresses[DBUS_BUS_SYSTEM] == NULL)
153 if (bus_connection_addresses[DBUS_BUS_SESSION] == NULL)
155 if (!get_from_env (&bus_connection_addresses[DBUS_BUS_SESSION],
156 "DBUS_SESSION_BUS_ADDRESS"))
160 if (bus_connection_addresses[DBUS_BUS_ACTIVATION] == NULL)
162 if (!get_from_env (&bus_connection_addresses[DBUS_BUS_ACTIVATION],
163 "DBUS_ACTIVATION_ADDRESS"))
167 s = _dbus_getenv ("DBUS_ACTIVATION_BUS_TYPE");
171 if (strcmp (s, "system") == 0)
172 activation_bus_type = DBUS_BUS_SYSTEM;
173 else if (strcmp (s, "session") == 0)
174 activation_bus_type = DBUS_BUS_SESSION;
177 /* If we return FALSE we have to be sure that restarting
178 * the above code will work right
181 if (!_dbus_setenv ("DBUS_ACTIVATION_ADDRESS", NULL))
184 if (!_dbus_setenv ("DBUS_ACTIVATION_BUS_TYPE", NULL))
187 if (!_dbus_register_shutdown_func (addresses_shutdown_func,
202 if (bus_data_slot < 0)
204 bus_data_slot = dbus_connection_allocate_data_slot ();
206 if (bus_data_slot < 0)
212 _dbus_assert (bus_data_slot_refcount == 0);
215 bus_data_slot_refcount += 1;
223 data_slot_unref (void)
227 _dbus_assert (bus_data_slot_refcount > 0);
228 _dbus_assert (bus_data_slot >= 0);
230 bus_data_slot_refcount -= 1;
232 if (bus_data_slot_refcount == 0)
234 dbus_connection_free_data_slot (bus_data_slot);
242 bus_data_free (void *data)
246 if (bd->is_well_known)
250 /* We may be stored in more than one slot */
252 while (i < N_BUS_TYPES)
254 if (bus_connections[i] == bd->connection)
255 bus_connections[i] = NULL;
262 dbus_free (bd->base_service);
269 ensure_bus_data (DBusConnection *connection)
273 if (!data_slot_ref ())
276 bd = dbus_connection_get_data (connection, bus_data_slot);
279 bd = dbus_new0 (BusData, 1);
286 bd->connection = connection;
288 if (!dbus_connection_set_data (connection, bus_data_slot, bd,
296 /* Data slot refcount now held by the BusData */
306 /** @} */ /* end of implementation details docs */
309 * @addtogroup DBusBus
314 * Connects to a bus daemon and registers the client with it.
315 * If a connection to the bus already exists, then that connection is returned.
317 * @todo alex thinks we should nullify the connection when we get a disconnect-message.
319 * @param type bus type
320 * @param error address where an error can be returned.
321 * @returns a DBusConnection
324 dbus_bus_get (DBusBusType type,
328 DBusConnection *connection;
330 DBusBusType address_type;
332 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
334 if (type < 0 || type >= N_BUS_TYPES)
336 _dbus_assert_not_reached ("Invalid bus type specified.");
343 if (!init_connections_unlocked ())
346 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
350 /* We want to use the activation address even if the
351 * activating bus is the session or system bus,
356 /* Use the real type of the activation bus for getting its
357 * connection. (If the activating bus isn't a well-known
358 * bus then activation_bus_type == DBUS_BUS_ACTIVATION)
360 if (type == DBUS_BUS_ACTIVATION)
361 type = activation_bus_type;
363 if (bus_connections[type] != NULL)
365 connection = bus_connections[type];
366 dbus_connection_ref (connection);
372 address = bus_connection_addresses[address_type];
375 dbus_set_error (error, DBUS_ERROR_FAILED,
376 "Unable to determine the address of the message bus");
381 connection = dbus_connection_open (address, error);
385 _DBUS_ASSERT_ERROR_IS_SET (error);
390 if (!dbus_bus_register (connection, error))
392 _DBUS_ASSERT_ERROR_IS_SET (error);
393 dbus_connection_disconnect (connection);
394 dbus_connection_unref (connection);
400 bus_connections[type] = connection;
401 bd = ensure_bus_data (connection);
402 _dbus_assert (bd != NULL);
404 bd->is_well_known = TRUE;
412 * Registers a connection with the bus. This must be the first
413 * thing an application does when connecting to the message bus.
414 * If registration succeeds, the base service name will be set,
415 * and can be obtained using dbus_bus_get_base_service().
417 * @todo if we get an error reply, it has to be converted into
418 * DBusError and returned
420 * @param connection the connection
421 * @param error place to store errors
422 * @returns #TRUE on success
425 dbus_bus_register (DBusConnection *connection,
428 DBusMessage *message, *reply;
432 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
434 bd = ensure_bus_data (connection);
437 _DBUS_SET_OOM (error);
441 if (bd->base_service != NULL)
443 _dbus_warn ("Attempt to register the same DBusConnection with the message bus, but it is already registered\n");
444 /* This isn't an error, it's a programming bug. We'll be nice
445 * and not _dbus_assert_not_reached()
450 message = dbus_message_new (DBUS_SERVICE_DBUS,
455 _DBUS_SET_OOM (error);
459 reply = dbus_connection_send_with_reply_and_block (connection, message, -1, error);
461 dbus_message_unref (message);
465 _DBUS_ASSERT_ERROR_IS_SET (error);
469 if (!dbus_message_get_args (reply, error,
470 DBUS_TYPE_STRING, &name,
473 _DBUS_ASSERT_ERROR_IS_SET (error);
477 bd->base_service = name;
484 * Sets the base service name of the connection.
485 * Can only be used if you registered with the
486 * bus manually (i.e. if you did not call
487 * dbus_bus_register()). Can only be called
488 * once per connection.
490 * @param connection the connection
491 * @param base_service the base service name
492 * @returns #FALSE if not enough memory
495 dbus_bus_set_base_service (DBusConnection *connection,
496 const char *base_service)
500 bd = ensure_bus_data (connection);
504 _dbus_assert (bd->base_service == NULL);
505 _dbus_assert (base_service != NULL);
507 bd->base_service = _dbus_strdup (base_service);
508 return bd->base_service != NULL;
512 * Gets the base service name of the connection.
513 * Only possible after the connection has been registered
514 * with the message bus.
516 * @param connection the connection
517 * @returns the base service name
520 dbus_bus_get_base_service (DBusConnection *connection)
524 bd = ensure_bus_data (connection);
528 return bd->base_service;
532 * Asks the bus to try to acquire a certain service.
534 * @todo these docs are not complete, need to document the
535 * return value and flags
537 * @todo if we get an error reply, it has to be converted into
538 * DBusError and returned
540 * @param connection the connection
541 * @param service_name the service name
543 * @param error location to store the error
544 * @returns a result code, -1 if error is set
547 dbus_bus_acquire_service (DBusConnection *connection,
548 const char *service_name,
552 DBusMessage *message, *reply;
553 dbus_uint32_t service_result;
555 message = dbus_message_new (DBUS_SERVICE_DBUS,
556 DBUS_MESSAGE_ACQUIRE_SERVICE);
560 _DBUS_SET_OOM (error);
564 if (!dbus_message_append_args (message,
565 DBUS_TYPE_STRING, service_name,
566 DBUS_TYPE_UINT32, flags,
569 dbus_message_unref (message);
570 _DBUS_SET_OOM (error);
574 reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
577 dbus_message_unref (message);
581 _DBUS_ASSERT_ERROR_IS_SET (error);
585 if (dbus_set_error_from_message (error, reply))
587 _DBUS_ASSERT_ERROR_IS_SET (error);
588 dbus_message_unref (reply);
592 if (!dbus_message_get_args (reply, error,
593 DBUS_TYPE_UINT32, &service_result,
596 _DBUS_ASSERT_ERROR_IS_SET (error);
597 dbus_message_unref (reply);
601 dbus_message_unref (reply);
603 return service_result;
607 * Checks whether a certain service exists.
609 * @todo the SERVICE_EXISTS message should use BOOLEAN not UINT32
611 * @param connection the connection
612 * @param service_name the service name
613 * @param error location to store any errors
614 * @returns #TRUE if the service exists, #FALSE if not or on error
617 dbus_bus_service_exists (DBusConnection *connection,
618 const char *service_name,
621 DBusMessage *message, *reply;
624 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
626 message = dbus_message_new (DBUS_SERVICE_DBUS,
627 DBUS_MESSAGE_SERVICE_EXISTS);
630 _DBUS_SET_OOM (error);
634 if (!dbus_message_append_args (message,
635 DBUS_TYPE_STRING, service_name,
638 dbus_message_unref (message);
639 _DBUS_SET_OOM (error);
643 reply = dbus_connection_send_with_reply_and_block (connection, message, -1, error);
644 dbus_message_unref (message);
648 _DBUS_ASSERT_ERROR_IS_SET (error);
652 if (!dbus_message_get_args (reply, error,
653 DBUS_TYPE_UINT32, &exists,
656 _DBUS_ASSERT_ERROR_IS_SET (error);
660 return (exists != FALSE);