dbus-bus.c
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
/* dbus-bus.c Convenience functions for communicating with the bus.
*
* Copyright (C) 2003 CodeFactory AB
* Copyright (C) 2003 Red Hat, Inc.
*
* SPDX-License-Identifier: AFL-2.1 OR GPL-2.0-or-later
*
* Licensed under the Academic Free License version 2.1
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include <config.h>
#include "dbus-bus.h"
#include "dbus-protocol.h"
#include "dbus-internals.h"
#include "dbus-message.h"
#include "dbus-marshal-validate.h"
#include "dbus-misc.h"
#include "dbus-threads-internal.h"
#include "dbus-connection-internal.h"
#include "dbus-string.h"
typedef struct
{
DBusConnection *connection;
char *unique_name;
unsigned int is_well_known : 1;
} BusData;
static dbus_int32_t bus_data_slot = -1;
#define N_BUS_TYPES 3
/* Protected by _DBUS_LOCK_bus, except during shutdown, which can't safely
* be done in a threaded application anyway. */
static DBusConnection *bus_connections[N_BUS_TYPES];
static char *bus_connection_addresses[N_BUS_TYPES] = { NULL, NULL, NULL };
static DBusBusType activation_bus_type = DBUS_BUS_STARTER;
static dbus_bool_t initialized = FALSE;
static void
addresses_shutdown_func (void *data)
{
int i;
i = 0;
while (i < N_BUS_TYPES)
{
if (bus_connections[i] != NULL)
_dbus_warn_check_failed ("dbus_shutdown() called but connections were still live. This probably means the application did not drop all its references to bus connections.");
dbus_free (bus_connection_addresses[i]);
bus_connection_addresses[i] = NULL;
++i;
}
activation_bus_type = DBUS_BUS_STARTER;
initialized = FALSE;
}
static dbus_bool_t
get_from_env (char **connection_p,
const char *env_var)
{
const char *s;
_dbus_assert (*connection_p == NULL);
s = _dbus_getenv (env_var);
if (s == NULL || *s == '\0')
return TRUE; /* successfully didn't use the env var */
else
{
*connection_p = _dbus_strdup (s);
return *connection_p != NULL;
}
}
static dbus_bool_t
init_session_address (void)
{
dbus_bool_t retval;
retval = FALSE;
/* First, look in the environment. This is the normal case on
* freedesktop.org/Unix systems. */
get_from_env (&bus_connection_addresses[DBUS_BUS_SESSION],
"DBUS_SESSION_BUS_ADDRESS");
if (bus_connection_addresses[DBUS_BUS_SESSION] == NULL)
{
dbus_bool_t supported;
DBusString addr;
DBusError error = DBUS_ERROR_INIT;
if (!_dbus_string_init (&addr))
return FALSE;
supported = FALSE;
/* So it's not in the environment - let's try a platform-specific method.
* On MacOS, this involves asking launchd. On Windows (not specified yet)
* we might do a COM lookup.
* Ignore errors - if we failed, fall back to autolaunch. */
retval = _dbus_lookup_session_address (&supported, &addr, &error);
if (supported && retval)
{
retval =_dbus_string_steal_data (&addr, &bus_connection_addresses[DBUS_BUS_SESSION]);
}
else if (supported && !retval)
{
if (dbus_error_is_set(&error))
_dbus_warn ("Dynamic session lookup supported but failed: %s", error.message);
else
_dbus_warn ("Dynamic session lookup supported but failed silently");
}
_dbus_string_free (&addr);
}
else
retval = TRUE;
if (!retval)
return FALSE;
/* We have a hard-coded (but compile-time-configurable) fallback address for
* the session bus. */
if (bus_connection_addresses[DBUS_BUS_SESSION] == NULL)
bus_connection_addresses[DBUS_BUS_SESSION] =
_dbus_strdup (DBUS_SESSION_BUS_CONNECT_ADDRESS);
if (bus_connection_addresses[DBUS_BUS_SESSION] == NULL)
return FALSE;
return TRUE;
}
static dbus_bool_t
init_connections_unlocked (void)
{
if (!initialized)
{
const char *s;
int i;
i = 0;
while (i < N_BUS_TYPES)
{
bus_connections[i] = NULL;
++i;
}
/* Don't init these twice, we may run this code twice if
* init_connections_unlocked() fails midway through.
* In practice, each block below should contain only one
* "return FALSE" or running through twice may not
* work right.
*/
if (bus_connection_addresses[DBUS_BUS_SYSTEM] == NULL)
{
_dbus_verbose ("Filling in system bus address...\n");
if (!get_from_env (&bus_connection_addresses[DBUS_BUS_SYSTEM],
"DBUS_SYSTEM_BUS_ADDRESS"))
return FALSE;
}
if (bus_connection_addresses[DBUS_BUS_SYSTEM] == NULL)
{
/* Use default system bus address if none set in environment */
bus_connection_addresses[DBUS_BUS_SYSTEM] =
_dbus_strdup (DBUS_SYSTEM_BUS_DEFAULT_ADDRESS);
if (bus_connection_addresses[DBUS_BUS_SYSTEM] == NULL)
return FALSE;
_dbus_verbose (" used default system bus \"%s\"\n",
bus_connection_addresses[DBUS_BUS_SYSTEM]);
}
else
_dbus_verbose (" used env var system bus \"%s\"\n",
bus_connection_addresses[DBUS_BUS_SYSTEM]);
if (bus_connection_addresses[DBUS_BUS_SESSION] == NULL)
{
_dbus_verbose ("Filling in session bus address...\n");
if (!init_session_address ())
return FALSE;
_dbus_verbose (" \"%s\"\n", bus_connection_addresses[DBUS_BUS_SESSION] ?
bus_connection_addresses[DBUS_BUS_SESSION] : "none set");
}
if (bus_connection_addresses[DBUS_BUS_STARTER] == NULL)
{
_dbus_verbose ("Filling in activation bus address...\n");
if (!get_from_env (&bus_connection_addresses[DBUS_BUS_STARTER],
"DBUS_STARTER_ADDRESS"))
return FALSE;
_dbus_verbose (" \"%s\"\n", bus_connection_addresses[DBUS_BUS_STARTER] ?
bus_connection_addresses[DBUS_BUS_STARTER] : "none set");
}
if (bus_connection_addresses[DBUS_BUS_STARTER] != NULL)
{
s = _dbus_getenv ("DBUS_STARTER_BUS_TYPE");
if (s != NULL)
{
_dbus_verbose ("Bus activation type was set to \"%s\"\n", s);
if (strcmp (s, "system") == 0)
activation_bus_type = DBUS_BUS_SYSTEM;
else if (strcmp (s, "session") == 0)
activation_bus_type = DBUS_BUS_SESSION;
}
}
else
{
/* Default to the session bus instead if available */
if (bus_connection_addresses[DBUS_BUS_SESSION] != NULL)
{
bus_connection_addresses[DBUS_BUS_STARTER] =
_dbus_strdup (bus_connection_addresses[DBUS_BUS_SESSION]);
if (bus_connection_addresses[DBUS_BUS_STARTER] == NULL)
return FALSE;
}
}
/* If we return FALSE we have to be sure that restarting
* the above code will work right
*/
if (!_dbus_register_shutdown_func (addresses_shutdown_func,
NULL))
return FALSE;
initialized = TRUE;
}
return initialized;
}
static void
bus_data_free (void *data)
{
BusData *bd = data;
if (bd->is_well_known)
{
int i;
if (!_DBUS_LOCK (bus))
_dbus_assert_not_reached ("global locks should have been initialized "
"when we attached bus data");
/* We may be stored in more than one slot */
/* This should now be impossible - these slots are supposed to
* be cleared on disconnect, so should not need to be cleared on
* finalize
*/
i = 0;
while (i < N_BUS_TYPES)
{
if (bus_connections[i] == bd->connection)
bus_connections[i] = NULL;
++i;
}
_DBUS_UNLOCK (bus);
}
dbus_free (bd->unique_name);
dbus_free (bd);
dbus_connection_free_data_slot (&bus_data_slot);
}
static BusData*
ensure_bus_data (DBusConnection *connection)
{
BusData *bd;
if (!dbus_connection_allocate_data_slot (&bus_data_slot))
return NULL;
bd = dbus_connection_get_data (connection, bus_data_slot);
if (bd == NULL)
{
bd = dbus_new0 (BusData, 1);
if (bd == NULL)
{
dbus_connection_free_data_slot (&bus_data_slot);
return NULL;
}
bd->connection = connection;
if (!dbus_connection_set_data (connection, bus_data_slot, bd,
bus_data_free))
{
dbus_free (bd);
dbus_connection_free_data_slot (&bus_data_slot);
return NULL;
}
/* Data slot refcount now held by the BusData */
}
else
{
dbus_connection_free_data_slot (&bus_data_slot);
}
return bd;
}
void
_dbus_bus_notify_shared_connection_disconnected_unlocked (DBusConnection *connection)
{
int i;
if (!_DBUS_LOCK (bus))
{
/* If it was in bus_connections, we would have initialized global locks
* when we added it. So, it can't be. */
return;
}
/* We are expecting to have the connection saved in only one of these
* slots, but someone could in a pathological case set system and session
* bus to the same bus or something. Or set one of them to the starter
* bus without setting the starter bus type in the env variable.
* So we don't break the loop as soon as we find a match.
*/
for (i = 0; i < N_BUS_TYPES; ++i)
{
if (bus_connections[i] == connection)
{
bus_connections[i] = NULL;
}
}
_DBUS_UNLOCK (bus);
}
static DBusConnection *
internal_bus_get (DBusBusType type,
dbus_bool_t private,
DBusError *error)
{
const char *address;
DBusConnection *connection;
BusData *bd;
DBusBusType address_type;
_dbus_return_val_if_fail (type >= 0 && type < N_BUS_TYPES, NULL);
_dbus_return_val_if_error_is_set (error, NULL);
connection = NULL;
if (!_DBUS_LOCK (bus))
{
_DBUS_SET_OOM (error);
/* do not "goto out", that would try to unlock */
return NULL;
}
if (!init_connections_unlocked ())
{
_DBUS_SET_OOM (error);
goto out;
}
/* We want to use the activation address even if the
* activating bus is the session or system bus,
* per the spec.
*/
address_type = type;
/* Use the real type of the activation bus for getting its
* connection, but only if the real type's address is available. (If
* the activating bus isn't a well-known bus then
* activation_bus_type == DBUS_BUS_STARTER)
*/
if (type == DBUS_BUS_STARTER &&
bus_connection_addresses[activation_bus_type] != NULL)
type = activation_bus_type;
if (!private && bus_connections[type] != NULL)
{
connection = bus_connections[type];
dbus_connection_ref (connection);
goto out;
}
address = bus_connection_addresses[address_type];
if (address == NULL)
{
dbus_set_error (error, DBUS_ERROR_FAILED,
"Unable to determine the address of the message bus (try 'man dbus-launch' and 'man dbus-daemon' for help)");
goto out;
}
if (private)
connection = dbus_connection_open_private (address, error);
else
connection = dbus_connection_open (address, error);
if (!connection)
{
goto out;
}
if (!dbus_bus_register (connection, error))
{
_dbus_connection_close_possibly_shared (connection);
dbus_connection_unref (connection);
connection = NULL;
goto out;
}
if (!private)
{
/* store a weak ref to the connection (dbus-connection.c is
* supposed to have a strong ref that it drops on disconnect,
* since this is a shared connection)
*/
bus_connections[type] = connection;
}
/* By default we're bound to the lifecycle of
* the message bus.
*/
dbus_connection_set_exit_on_disconnect (connection,
TRUE);
if (!_DBUS_LOCK (bus_datas))
_dbus_assert_not_reached ("global locks were initialized already");
bd = ensure_bus_data (connection);
_dbus_assert (bd != NULL); /* it should have been created on
register, so OOM not possible */
bd->is_well_known = TRUE;
_DBUS_UNLOCK (bus_datas);
out:
/* Return a reference to the caller, or NULL with error set. */
if (connection == NULL)
_DBUS_ASSERT_ERROR_IS_SET (error);
_DBUS_UNLOCK (bus);
return connection;
}
/* end of implementation details docs */
DBusConnection *
dbus_bus_get (DBusBusType type,
DBusError *error)
{
return internal_bus_get (type, FALSE, error);
}
DBusConnection *
dbus_bus_get_private (DBusBusType type,
DBusError *error)
{
return internal_bus_get (type, TRUE, error);
}
dbus_bool_t
dbus_bus_register (DBusConnection *connection,
DBusError *error)
{
DBusMessage *message, *reply;
char *name;
BusData *bd;
dbus_bool_t retval;
_dbus_return_val_if_fail (connection != NULL, FALSE);
_dbus_return_val_if_error_is_set (error, FALSE);
retval = FALSE;
message = NULL;
reply = NULL;
if (!_DBUS_LOCK (bus_datas))
{
_DBUS_SET_OOM (error);
/* do not "goto out", that would try to unlock */
return FALSE;
}
bd = ensure_bus_data (connection);
if (bd == NULL)
{
_DBUS_SET_OOM (error);
goto out;
}
if (bd->unique_name != NULL)
{
_dbus_verbose ("Ignoring attempt to register the same DBusConnection %s with the message bus a second time.\n",
bd->unique_name);
/* Success! */
retval = TRUE;
goto out;
}
message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
DBUS_PATH_DBUS,
DBUS_INTERFACE_DBUS,
"Hello");
if (!message)
{
_DBUS_SET_OOM (error);
goto out;
}
reply = dbus_connection_send_with_reply_and_block (connection, message, -1, error);
if (reply == NULL)
goto out;
else if (dbus_set_error_from_message (error, reply))
goto out;
else if (!dbus_message_get_args (reply, error,
DBUS_TYPE_STRING, &name,
DBUS_TYPE_INVALID))
goto out;
bd->unique_name = _dbus_strdup (name);
if (bd->unique_name == NULL)
{
_DBUS_SET_OOM (error);
goto out;
}
retval = TRUE;
out:
_DBUS_UNLOCK (bus_datas);
if (message)
dbus_message_unref (message);
if (reply)
dbus_message_unref (reply);
if (!retval)
_DBUS_ASSERT_ERROR_IS_SET (error);
return retval;
}
dbus_bool_t
dbus_bus_set_unique_name (DBusConnection *connection,
const char *unique_name)
{
BusData *bd;
dbus_bool_t success = FALSE;
_dbus_return_val_if_fail (connection != NULL, FALSE);
_dbus_return_val_if_fail (unique_name != NULL, FALSE);
if (!_DBUS_LOCK (bus_datas))
{
/* do not "goto out", that would try to unlock */
return FALSE;
}
bd = ensure_bus_data (connection);
if (bd == NULL)
goto out;
_dbus_assert (bd->unique_name == NULL);
bd->unique_name = _dbus_strdup (unique_name);
success = bd->unique_name != NULL;
out:
_DBUS_UNLOCK (bus_datas);
return success;
}
const char*
dbus_bus_get_unique_name (DBusConnection *connection)
{
BusData *bd;
const char *unique_name = NULL;
_dbus_return_val_if_fail (connection != NULL, NULL);
if (!_DBUS_LOCK (bus_datas))
{
/* We'd have initialized locks when we gave it its unique name, if it
* had one. Don't "goto out", that would try to unlock. */
return NULL;
}
bd = ensure_bus_data (connection);
if (bd == NULL)
goto out;
unique_name = bd->unique_name;
out:
_DBUS_UNLOCK (bus_datas);
return unique_name;
}
unsigned long
dbus_bus_get_unix_user (DBusConnection *connection,
const char *name,
DBusError *error)
{
DBusMessage *message, *reply;
dbus_uint32_t uid;
_dbus_return_val_if_fail (connection != NULL, DBUS_UID_UNSET);
_dbus_return_val_if_fail (name != NULL, DBUS_UID_UNSET);
_dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), DBUS_UID_UNSET);
_dbus_return_val_if_error_is_set (error, DBUS_UID_UNSET);
message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
DBUS_PATH_DBUS,
DBUS_INTERFACE_DBUS,
"GetConnectionUnixUser");
if (message == NULL)
{
_DBUS_SET_OOM (error);
return DBUS_UID_UNSET;
}
if (!dbus_message_append_args (message,
DBUS_TYPE_STRING, &name,
DBUS_TYPE_INVALID))
{
dbus_message_unref (message);
_DBUS_SET_OOM (error);
return DBUS_UID_UNSET;
}
reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
error);
dbus_message_unref (message);
if (reply == NULL)
{
_DBUS_ASSERT_ERROR_IS_SET (error);
return DBUS_UID_UNSET;
}
if (dbus_set_error_from_message (error, reply))
{
_DBUS_ASSERT_ERROR_IS_SET (error);
dbus_message_unref (reply);
return DBUS_UID_UNSET;
}
if (!dbus_message_get_args (reply, error,
DBUS_TYPE_UINT32, &uid,
DBUS_TYPE_INVALID))
{
_DBUS_ASSERT_ERROR_IS_SET (error);
dbus_message_unref (reply);
return DBUS_UID_UNSET;
}
dbus_message_unref (reply);
return (unsigned long) uid;
}
char*
dbus_bus_get_id (DBusConnection *connection,
DBusError *error)
{
DBusMessage *message, *reply;
char *id;
const char *v_STRING;
_dbus_return_val_if_fail (connection != NULL, NULL);
_dbus_return_val_if_error_is_set (error, NULL);
message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
DBUS_PATH_DBUS,
DBUS_INTERFACE_DBUS,
"GetId");
if (message == NULL)
{
_DBUS_SET_OOM (error);
return NULL;
}
reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
error);
dbus_message_unref (message);
if (reply == NULL)
{
_DBUS_ASSERT_ERROR_IS_SET (error);
return NULL;
}
if (dbus_set_error_from_message (error, reply))
{
_DBUS_ASSERT_ERROR_IS_SET (error);
dbus_message_unref (reply);
return NULL;
}
v_STRING = NULL;
if (!dbus_message_get_args (reply, error,
DBUS_TYPE_STRING, &v_STRING,
DBUS_TYPE_INVALID))
{
_DBUS_ASSERT_ERROR_IS_SET (error);
dbus_message_unref (reply);
return NULL;
}
id = _dbus_strdup (v_STRING); /* may be NULL */
dbus_message_unref (reply);
if (id == NULL)
_DBUS_SET_OOM (error);
/* FIXME it might be nice to cache the ID locally */
return id;
}
int
dbus_bus_request_name (DBusConnection *connection,
const char *name,
unsigned int flags,
DBusError *error)
{
DBusMessage *message, *reply;
dbus_uint32_t result;
_dbus_return_val_if_fail (connection != NULL, 0);
_dbus_return_val_if_fail (name != NULL, 0);
_dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), 0);
_dbus_return_val_if_error_is_set (error, 0);
message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
DBUS_PATH_DBUS,
DBUS_INTERFACE_DBUS,
"RequestName");
if (message == NULL)
{
_DBUS_SET_OOM (error);
return -1;
}
if (!dbus_message_append_args (message,
DBUS_TYPE_STRING, &name,
DBUS_TYPE_UINT32, &flags,
DBUS_TYPE_INVALID))
{
dbus_message_unref (message);
_DBUS_SET_OOM (error);
return -1;
}
reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
error);
dbus_message_unref (message);
if (reply == NULL)
{
_DBUS_ASSERT_ERROR_IS_SET (error);
return -1;
}
if (dbus_set_error_from_message (error, reply))
{
_DBUS_ASSERT_ERROR_IS_SET (error);
dbus_message_unref (reply);
return -1;
}
if (!dbus_message_get_args (reply, error,
DBUS_TYPE_UINT32, &result,
DBUS_TYPE_INVALID))
{
_DBUS_ASSERT_ERROR_IS_SET (error);
dbus_message_unref (reply);
return -1;
}
dbus_message_unref (reply);
return result;
}
int
dbus_bus_release_name (DBusConnection *connection,
const char *name,
DBusError *error)
{
DBusMessage *message, *reply;
dbus_uint32_t result;
_dbus_return_val_if_fail (connection != NULL, 0);
_dbus_return_val_if_fail (name != NULL, 0);
_dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), 0);
_dbus_return_val_if_error_is_set (error, 0);
message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
DBUS_PATH_DBUS,
DBUS_INTERFACE_DBUS,
"ReleaseName");
if (message == NULL)
{
_DBUS_SET_OOM (error);
return -1;
}
if (!dbus_message_append_args (message,
DBUS_TYPE_STRING, &name,
DBUS_TYPE_INVALID))
{
dbus_message_unref (message);
_DBUS_SET_OOM (error);
return -1;
}
reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
error);
dbus_message_unref (message);
if (reply == NULL)
{
_DBUS_ASSERT_ERROR_IS_SET (error);
return -1;
}
if (dbus_set_error_from_message (error, reply))
{
_DBUS_ASSERT_ERROR_IS_SET (error);
dbus_message_unref (reply);
return -1;
}
if (!dbus_message_get_args (reply, error,
DBUS_TYPE_UINT32, &result,
DBUS_TYPE_INVALID))
{
_DBUS_ASSERT_ERROR_IS_SET (error);
dbus_message_unref (reply);
return -1;
}
dbus_message_unref (reply);
return result;
}
dbus_bool_t
dbus_bus_name_has_owner (DBusConnection *connection,
const char *name,
DBusError *error)
{
DBusMessage *message, *reply;
dbus_bool_t exists;
_dbus_return_val_if_fail (connection != NULL, FALSE);
_dbus_return_val_if_fail (name != NULL, FALSE);
_dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), FALSE);
_dbus_return_val_if_error_is_set (error, FALSE);
message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
DBUS_PATH_DBUS,
DBUS_INTERFACE_DBUS,
"NameHasOwner");
if (message == NULL)
{
_DBUS_SET_OOM (error);
return FALSE;
}
if (!dbus_message_append_args (message,
DBUS_TYPE_STRING, &name,
DBUS_TYPE_INVALID))
{
dbus_message_unref (message);
_DBUS_SET_OOM (error);
return FALSE;
}
reply = dbus_connection_send_with_reply_and_block (connection, message, -1, error);
dbus_message_unref (message);
if (reply == NULL)
{
_DBUS_ASSERT_ERROR_IS_SET (error);
return FALSE;
}
if (!dbus_message_get_args (reply, error,
DBUS_TYPE_BOOLEAN, &exists,
DBUS_TYPE_INVALID))
{
_DBUS_ASSERT_ERROR_IS_SET (error);
dbus_message_unref (reply);
return FALSE;
}
dbus_message_unref (reply);
return exists;
}
dbus_bool_t
dbus_bus_start_service_by_name (DBusConnection *connection,
const char *name,
dbus_uint32_t flags,
dbus_uint32_t *result,
DBusError *error)
{
DBusMessage *msg;
DBusMessage *reply;
_dbus_return_val_if_fail (connection != NULL, FALSE);
_dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), FALSE);
msg = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
DBUS_PATH_DBUS,
DBUS_INTERFACE_DBUS,
"StartServiceByName");
if (!dbus_message_append_args (msg, DBUS_TYPE_STRING, &name,
DBUS_TYPE_UINT32, &flags, DBUS_TYPE_INVALID))
{
dbus_message_unref (msg);
_DBUS_SET_OOM (error);
return FALSE;
}
reply = dbus_connection_send_with_reply_and_block (connection, msg,
-1, error);
dbus_message_unref (msg);
if (reply == NULL)
{
_DBUS_ASSERT_ERROR_IS_SET (error);
return FALSE;
}
if (dbus_set_error_from_message (error, reply))
{
_DBUS_ASSERT_ERROR_IS_SET (error);
dbus_message_unref (reply);
return FALSE;
}
if (result != NULL &&
!dbus_message_get_args (reply, error, DBUS_TYPE_UINT32,
result, DBUS_TYPE_INVALID))
{
_DBUS_ASSERT_ERROR_IS_SET (error);
dbus_message_unref (reply);
return FALSE;
}
dbus_message_unref (reply);
return TRUE;
}
static void
send_no_return_values (DBusConnection *connection,
DBusMessage *msg,
DBusError *error)
{
if (error)
{
/* Block to check success codepath */
DBusMessage *reply;
reply = dbus_connection_send_with_reply_and_block (connection, msg,
-1, error);
if (reply == NULL)
_DBUS_ASSERT_ERROR_IS_SET (error);
else
dbus_message_unref (reply);
}
else
{
/* Silently-fail nonblocking codepath */
dbus_message_set_no_reply (msg, TRUE);
dbus_connection_send (connection, msg, NULL);
}
}
void
dbus_bus_add_match (DBusConnection *connection,
const char *rule,
DBusError *error)
{
DBusMessage *msg;
_dbus_return_if_fail (rule != NULL);
msg = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
DBUS_PATH_DBUS,
DBUS_INTERFACE_DBUS,
"AddMatch");
if (msg == NULL)
{
_DBUS_SET_OOM (error);
return;
}
if (!dbus_message_append_args (msg, DBUS_TYPE_STRING, &rule,
DBUS_TYPE_INVALID))
{
dbus_message_unref (msg);
_DBUS_SET_OOM (error);
return;
}
send_no_return_values (connection, msg, error);
dbus_message_unref (msg);
}
void
dbus_bus_remove_match (DBusConnection *connection,
const char *rule,
DBusError *error)
{
DBusMessage *msg;
_dbus_return_if_fail (rule != NULL);
msg = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
DBUS_PATH_DBUS,
DBUS_INTERFACE_DBUS,
"RemoveMatch");
if (!dbus_message_append_args (msg, DBUS_TYPE_STRING, &rule,
DBUS_TYPE_INVALID))
{
dbus_message_unref (msg);
_DBUS_SET_OOM (error);
return;
}
send_no_return_values (connection, msg, error);
dbus_message_unref (msg);
}
_dbus_bus_notify_shared_connection_disconnected_unlocked
void _dbus_bus_notify_shared_connection_disconnected_unlocked(DBusConnection *connection)
Internal function that checks to see if this is a shared connection owned by the bus and if it is unr...
Definition dbus-bus.c:390
N_BUS_TYPES
#define N_BUS_TYPES
Number of bus types.
Definition dbus-bus.c:93
dbus_bus_set_unique_name
dbus_bool_t dbus_bus_set_unique_name(DBusConnection *connection, const char *unique_name)
Sets the unique name of the connection, as assigned by the message bus.
Definition dbus-bus.c:769
dbus_bus_register
dbus_bool_t dbus_bus_register(DBusConnection *connection, DBusError *error)
Registers a connection with the bus.
Definition dbus-bus.c:649
dbus_bus_get_id
char * dbus_bus_get_id(DBusConnection *connection, DBusError *error)
Asks the bus to return its globally unique ID, as described in the D-Bus specification.
Definition dbus-bus.c:951
dbus_bus_get_unix_user
unsigned long dbus_bus_get_unix_user(DBusConnection *connection, const char *name, DBusError *error)
Asks the bus to return the UID the named connection authenticated as, if any.
Definition dbus-bus.c:868
dbus_bus_add_match
void dbus_bus_add_match(DBusConnection *connection, const char *rule, DBusError *error)
Adds a match rule to match messages going through the message bus.
Definition dbus-bus.c:1529
dbus_bus_name_has_owner
dbus_bool_t dbus_bus_name_has_owner(DBusConnection *connection, const char *name, DBusError *error)
Asks the bus whether a certain name has an owner.
Definition dbus-bus.c:1283
dbus_bus_remove_match
void dbus_bus_remove_match(DBusConnection *connection, const char *rule, DBusError *error)
Removes a previously-added match rule "by value" (the most recently-added identical rule gets removed...
Definition dbus-bus.c:1579
dbus_bus_get
DBusConnection * dbus_bus_get(DBusBusType type, DBusError *error)
Connects to a bus daemon and registers the client with it.
Definition dbus-bus.c:561
dbus_bus_start_service_by_name
dbus_bool_t dbus_bus_start_service_by_name(DBusConnection *connection, const char *name, dbus_uint32_t flags, dbus_uint32_t *result, DBusError *error)
Starts a service that will request ownership of the given name.
Definition dbus-bus.c:1359
dbus_bus_request_name
int dbus_bus_request_name(DBusConnection *connection, const char *name, unsigned int flags, DBusError *error)
Asks the bus to assign the given name to this connection by invoking the RequestName method on the bu...
Definition dbus-bus.c:1115
dbus_bus_get_unique_name
const char * dbus_bus_get_unique_name(DBusConnection *connection)
Gets the unique name of the connection as assigned by the message bus.
Definition dbus-bus.c:818
dbus_bus_get_private
DBusConnection * dbus_bus_get_private(DBusBusType type, DBusError *error)
Connects to a bus daemon and registers the client with it as with dbus_bus_register().
Definition dbus-bus.c:593
dbus_bus_release_name
int dbus_bus_release_name(DBusConnection *connection, const char *name, DBusError *error)
Asks the bus to unassign the given name from this connection by invoking the ReleaseName method on th...
Definition dbus-bus.c:1201
_dbus_connection_close_possibly_shared
void _dbus_connection_close_possibly_shared(DBusConnection *connection)
Closes a shared OR private connection, while dbus_connection_close() can only be used on private conn...
Definition dbus-connection.c:1962
dbus_connection_set_exit_on_disconnect
void dbus_connection_set_exit_on_disconnect(DBusConnection *connection, dbus_bool_t exit_on_disconnect)
Set whether _exit() should be called when the connection receives a disconnect signal.
Definition dbus-connection.c:3160
dbus_connection_get_data
void * dbus_connection_get_data(DBusConnection *connection, dbus_int32_t slot)
Retrieves data previously set with dbus_connection_set_data().
Definition dbus-connection.c:6144
dbus_connection_open_private
DBusConnection * dbus_connection_open_private(const char *address, DBusError *error)
Opens a new, dedicated connection to a remote address.
Definition dbus-connection.c:2678
dbus_connection_unref
void dbus_connection_unref(DBusConnection *connection)
Decrements the reference count of a DBusConnection, and finalizes it if the count reaches zero.
Definition dbus-connection.c:2832
dbus_connection_allocate_data_slot
dbus_bool_t dbus_connection_allocate_data_slot(dbus_int32_t *slot_p)
Allocates an integer ID to be used for storing application-specific data on any DBusConnection.
Definition dbus-connection.c:6047
dbus_connection_free_data_slot
void dbus_connection_free_data_slot(dbus_int32_t *slot_p)
Deallocates a global ID for connection data slots.
Definition dbus-connection.c:6065
dbus_connection_set_data
dbus_bool_t dbus_connection_set_data(DBusConnection *connection, dbus_int32_t slot, void *data, DBusFreeFunction free_data_func)
Stores a pointer on a DBusConnection, along with an optional function to be used for freeing the data...
Definition dbus-connection.c:6095
dbus_connection_send_with_reply_and_block
DBusMessage * dbus_connection_send_with_reply_and_block(DBusConnection *connection, DBusMessage *message, int timeout_milliseconds, DBusError *error)
Sends a message and blocks a certain time period while waiting for a reply.
Definition dbus-connection.c:3551
dbus_connection_open
DBusConnection * dbus_connection_open(const char *address, DBusError *error)
Gets a connection to a remote address.
Definition dbus-connection.c:2635
dbus_connection_send
dbus_bool_t dbus_connection_send(DBusConnection *connection, DBusMessage *message, dbus_uint32_t *serial)
Adds a message to the outgoing message queue.
Definition dbus-connection.c:3317
dbus_connection_ref
DBusConnection * dbus_connection_ref(DBusConnection *connection)
Increments the reference count of a DBusConnection.
Definition dbus-connection.c:2700
DBUS_ERROR_INIT
#define DBUS_ERROR_INIT
Expands to a suitable initializer for a DBusError on the stack.
Definition dbus-errors.h:64
dbus_set_error
void dbus_set_error(DBusError *error, const char *name, const char *format,...)
Assigns an error name and message to a DBusError.
Definition dbus-errors.c:356
dbus_error_is_set
dbus_bool_t dbus_error_is_set(const DBusError *error)
Checks whether an error occurred (the error is set).
Definition dbus-errors.c:331
_dbus_assert_not_reached
#define _dbus_assert_not_reached(explanation)
Aborts with an error message if called.
Definition dbus-internals.h:164
_dbus_assert
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
Definition dbus-internals.h:153
_DBUS_UNLOCK
#define _DBUS_UNLOCK(name)
Unlocks a global lock.
Definition dbus-internals.h:438
_DBUS_LOCK
#define _DBUS_LOCK(name)
Locks a global lock, initializing it first if necessary.
Definition dbus-internals.h:437
_dbus_warn_check_failed
void _dbus_warn_check_failed(const char *format,...)
Prints a "critical" warning to stderr when an assertion fails; differs from _dbus_warn primarily in t...
Definition dbus-internals.c:310
_dbus_strdup
char * _dbus_strdup(const char *str)
Duplicates a string.
Definition dbus-internals.c:621
_dbus_warn
void _dbus_warn(const char *format,...)
Prints a warning message to stderr.
Definition dbus-internals.c:278
NULL
#define NULL
A null pointer, defined appropriately for C or C++.
Definition dbus-macros.h:51
TRUE
#define TRUE
Expands to "1".
Definition dbus-macros.h:41
FALSE
#define FALSE
Expands to "0".
Definition dbus-macros.h:44
_dbus_register_shutdown_func
DBUS_PRIVATE_EXPORT dbus_bool_t _dbus_register_shutdown_func(DBusShutdownFunction function, void *data)
Register a cleanup function to be called exactly once the next time dbus_shutdown() is called.
Definition dbus-memory.c:819
dbus_free
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
Definition dbus-memory.c:710
dbus_new0
#define dbus_new0(type, count)
Safe macro for using dbus_malloc0().
Definition dbus-memory.h:60
dbus_message_set_no_reply
void dbus_message_set_no_reply(DBusMessage *message, dbus_bool_t no_reply)
Sets a flag indicating that the message does not want a reply; if this flag is set,...
Definition dbus-message.c:3247
dbus_message_append_args
dbus_bool_t dbus_message_append_args(DBusMessage *message, int first_arg_type,...)
Appends fields to a message given a variable argument list.
Definition dbus-message.c:1843
dbus_message_new_method_call
DBusMessage * dbus_message_new_method_call(const char *destination, const char *path, const char *iface, const char *method)
Constructs a new message to invoke a method on a remote object.
Definition dbus-message.c:1378
dbus_message_unref
void dbus_message_unref(DBusMessage *message)
Decrements the reference count of a DBusMessage, freeing the message if the count reaches 0.
Definition dbus-message.c:1735
dbus_set_error_from_message
dbus_bool_t dbus_set_error_from_message(DBusError *error, DBusMessage *message)
Sets a DBusError based on the contents of the given message.
Definition dbus-message.c:4059
dbus_message_get_args
dbus_bool_t dbus_message_get_args(DBusMessage *message, DBusError *error, int first_arg_type,...)
Gets arguments from a message given a variable argument list.
Definition dbus-message.c:2032
DBUS_TYPE_BOOLEAN
#define DBUS_TYPE_BOOLEAN
Type code marking a boolean.
Definition dbus-protocol.h:72
DBUS_TYPE_STRING
#define DBUS_TYPE_STRING
Type code marking a UTF-8 encoded, nul-terminated Unicode string.
Definition dbus-protocol.h:104
DBUS_TYPE_INVALID
#define DBUS_TYPE_INVALID
Type code that is never equal to a legitimate type code.
Definition dbus-protocol.h:62
DBUS_ERROR_FAILED
#define DBUS_ERROR_FAILED
A generic error; "something went wrong" - see the error message for more.
Definition dbus-protocol.h:361
DBUS_TYPE_UINT32
#define DBUS_TYPE_UINT32
Type code marking a 32-bit unsigned integer.
Definition dbus-protocol.h:88
DBUS_PATH_DBUS
#define DBUS_PATH_DBUS
The object path used to talk to the bus itself.
Definition dbus-shared.h:82
DBusBusType
DBusBusType
Well-known bus types.
Definition dbus-shared.h:59
DBUS_SERVICE_DBUS
#define DBUS_SERVICE_DBUS
The bus name used to talk to the bus itself.
Definition dbus-shared.h:78
DBUS_INTERFACE_DBUS
#define DBUS_INTERFACE_DBUS
The interface exported by the object with DBUS_SERVICE_DBUS and DBUS_PATH_DBUS.
Definition dbus-shared.h:90
DBUS_BUS_SESSION
@ DBUS_BUS_SESSION
The login session bus.
Definition dbus-shared.h:60
DBUS_BUS_STARTER
@ DBUS_BUS_STARTER
The bus that started us, if any.
Definition dbus-shared.h:62
DBUS_BUS_SYSTEM
@ DBUS_BUS_SYSTEM
The systemwide bus.
Definition dbus-shared.h:61
_dbus_string_init
dbus_bool_t _dbus_string_init(DBusString *str)
Initializes a string.
Definition dbus-string.c:182
_dbus_string_steal_data
dbus_bool_t _dbus_string_steal_data(DBusString *str, char **data_return)
Like _dbus_string_get_data(), but removes the gotten data from the original string.
Definition dbus-string.c:686
_dbus_string_free
void _dbus_string_free(DBusString *str)
Frees a string created by _dbus_string_init(), and fills it with the same contents as _DBUS_STRING_IN...
Definition dbus-string.c:278
DBUS_UID_UNSET
#define DBUS_UID_UNSET
an invalid UID used to represent an uninitialized dbus_uid_t field
Definition dbus-sysdeps.h:148
_dbus_getenv
const char * _dbus_getenv(const char *varname)
Wrapper for getenv().
Definition dbus-sysdeps.c:197
_dbus_lookup_session_address
dbus_bool_t _dbus_lookup_session_address(dbus_bool_t *supported, DBusString *address, DBusError *error)
Determines the address of the session bus by querying a platform-specific method.
Definition dbus-sysdeps-unix.c:4670
dbus_uint32_t
unsigned int dbus_uint32_t
A 32-bit unsigned integer on all platforms.
Definition dbus-arch-deps.h:45
dbus_int32_t
int dbus_int32_t
A 32-bit signed integer on all platforms.
Definition dbus-arch-deps.h:44
dbus_bool_t
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Definition dbus-types.h:37
BusData
Block of message-bus-related data we attach to each DBusConnection used with these convenience functi...
Definition dbus-bus.c:80
BusData::is_well_known
unsigned int is_well_known
Is one of the well-known connections in our global array.
Definition dbus-bus.c:84
BusData::connection
DBusConnection * connection
Connection we're associated with.
Definition dbus-bus.c:81
BusData::unique_name
char * unique_name
Unique name of this connection.
Definition dbus-bus.c:82
DBusConnection
Implementation details of DBusConnection.
Definition dbus-connection.c:259
DBusError
Object representing an exception.
Definition dbus-errors.h:51
DBusError::message
const char * message
public error message field
Definition dbus-errors.h:53
DBusMessage
Internals of DBusMessage.
Definition dbus-message-private.h:102
DBusString
Definition dbus-string.h:47