From 73b17726f35b9ab561537536ddbda957fc078350 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Mon, 16 Apr 2018 19:35:59 +0100 Subject: [PATCH 08/39] containers: Factor out connection_get_instance and make it NULL-safe When I introduce per-container message filtering, it'll be useful to be able to get the instance for a connection without worrying about whether that connection is NULL (representing the dbus-daemon itself, or an activatable service that has not yet been activated). Also make it robust against Containers having not been initialized, for completeness. Signed-off-by: Simon McVittie --- bus/containers.c | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/bus/containers.c b/bus/containers.c index 038068e1..ece12eab 100644 --- a/bus/containers.c +++ b/bus/containers.c @@ -1119,6 +1119,23 @@ failed: return FALSE; } +/* + * This accepts a NULL connection so that it can be used when checking + * whether to allow sending or receiving a message, which might involve + * the dbus-daemon itself as a message sender or recipient. + */ +static BusContainerInstance * +connection_get_instance (DBusConnection *connection) +{ + if (connection == NULL) + return NULL; + + if (contained_data_slot == -1) + return NULL; + + return dbus_connection_get_data (connection, contained_data_slot); +} + dbus_bool_t bus_containers_handle_get_connection_instance (DBusConnection *caller, BusTransaction *transaction, @@ -1154,7 +1171,7 @@ bus_containers_handle_get_connection_instance (DBusConnection *caller, goto failed; } - instance = dbus_connection_get_data (subject, contained_data_slot); + instance = connection_get_instance (subject); if (instance == NULL) { @@ -1413,7 +1430,7 @@ bus_containers_remove_connection (BusContainers *self, } } - instance = dbus_connection_get_data (connection, contained_data_slot); + instance = connection_get_instance (connection); if (instance != NULL) { @@ -1438,7 +1455,7 @@ bus_containers_connection_is_contained (DBusConnection *connection, #ifdef DBUS_ENABLE_CONTAINERS BusContainerInstance *instance; - instance = dbus_connection_get_data (connection, contained_data_slot); + instance = connection_get_instance (connection); if (instance != NULL) { -- 2.17.0