+/*
+ * Opens a sd-bus connection and returns it in 'ret'.
+ * The sd-bus connexion is intended to be for user if 'isuser'
+ * is not null. The adress is the default address when 'address'
+ * is NULL or, otherwise, the given address.
+ * It might be necessary to pass the address as an argument because
+ * library systemd uses secure_getenv to retrieves the default
+ * addresses and secure_getenv might return NULL in some cases.
+ */
+static int open_bus(sd_bus **ret, int isuser, const char *address)
+{
+ sd_bus *b;
+ int rc;
+
+ if (address == NULL)
+ return (isuser ? sd_bus_open_user : sd_bus_open_system)(ret);
+
+ rc = sd_bus_new(&b);
+ if (rc < 0)
+ return rc;
+
+ rc = sd_bus_set_address(b, address);
+ if (rc < 0)
+ goto fail;
+
+ sd_bus_set_bus_client(b, 1);
+
+ rc = sd_bus_start(b);
+ if (rc < 0)
+ goto fail;
+
+ *ret = b;
+ return 0;
+
+fail:
+ sd_bus_unref(b);
+ return rc;
+}
+