summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2023-03-06 14:48:32 +0100
committerThomas Haller <thaller@redhat.com>2023-03-23 13:06:57 +0100
commit4699a4c3cd17e6bc832d854386d47edc47bff331 (patch)
treeb54e73683ef9344c2fdc42a19fc0beb4d5a14418
parent2805ddcbcc23c4e97abad2028b670b5626704ace (diff)
downloadNetworkManager-4699a4c3cd17e6bc832d854386d47edc47bff331.tar.gz
core/dbus: split RequestName D-Bus call out of initialization for NMDBusManager
-rw-r--r--src/core/main.c13
-rw-r--r--src/core/nm-dbus-manager.c85
-rw-r--r--src/core/nm-dbus-manager.h4
3 files changed, 60 insertions, 42 deletions
diff --git a/src/core/main.c b/src/core/main.c
index 2eb230d9ca..4c436a631b 100644
--- a/src/core/main.c
+++ b/src/core/main.c
@@ -266,13 +266,14 @@ _dbus_manager_init(NMConfig *config)
c_a_q_type = nm_config_get_configure_and_quit(config);
- if (c_a_q_type == NM_CONFIG_CONFIGURE_AND_QUIT_DISABLED)
- return nm_dbus_manager_acquire_bus(busmgr, TRUE);
+ if (c_a_q_type == NM_CONFIG_CONFIGURE_AND_QUIT_INITRD) {
+ /* in initrd we don't have D-Bus at all. Don't even try to get the G_BUS_TYPE_SYSTEM
+ * connection. And of course don't claim the D-Bus name. */
+ return TRUE;
+ }
- nm_assert(c_a_q_type == NM_CONFIG_CONFIGURE_AND_QUIT_INITRD);
- /* in initrd we don't have D-Bus at all. Don't even try to get the G_BUS_TYPE_SYSTEM
- * connection. And of course don't claim the D-Bus name. */
- return TRUE;
+ nm_assert(c_a_q_type == NM_CONFIG_CONFIGURE_AND_QUIT_DISABLED);
+ return nm_dbus_manager_setup(busmgr);
}
/*
diff --git a/src/core/nm-dbus-manager.c b/src/core/nm-dbus-manager.c
index af7de8c458..5ecd9fe680 100644
--- a/src/core/nm-dbus-manager.c
+++ b/src/core/nm-dbus-manager.c
@@ -1410,48 +1410,18 @@ nm_dbus_manager_start(NMDBusManager *self,
}
gboolean
-nm_dbus_manager_acquire_bus(NMDBusManager *self, gboolean request_name)
+nm_dbus_manager_request_name_sync(NMDBusManager *self)
{
NMDBusManagerPrivate *priv;
gs_free_error GError *error = NULL;
gs_unref_variant GVariant *ret = NULL;
guint32 result;
- guint registration_id;
g_return_val_if_fail(NM_IS_DBUS_MANAGER(self), FALSE);
priv = NM_DBUS_MANAGER_GET_PRIVATE(self);
- /* Create the D-Bus connection and registering the name synchronously.
- * That is necessary because we need to exit right away if we can't
- * acquire the name despite connecting to the bus successfully.
- * It means that something is gravely broken -- such as another NetworkManager
- * instance running. */
- priv->main_dbus_connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
- if (!priv->main_dbus_connection) {
- _LOGE("cannot connect to D-Bus: %s", error->message);
- return FALSE;
- }
-
- g_dbus_connection_set_exit_on_close(priv->main_dbus_connection, FALSE);
-
- if (!request_name) {
- _LOGD("D-Bus connection created");
- return TRUE;
- }
-
- registration_id = g_dbus_connection_register_object(
- priv->main_dbus_connection,
- OBJECT_MANAGER_SERVER_BASE_PATH,
- NM_UNCONST_PTR(GDBusInterfaceInfo, &interface_info_objmgr),
- &dbus_vtable_objmgr,
- self,
- NULL,
- &error);
- if (!registration_id) {
- _LOGE("failure to register object manager: %s", error->message);
- return FALSE;
- }
+ g_return_val_if_fail(G_IS_DBUS_CONNECTION(priv->main_dbus_connection), FALSE);
ret = g_dbus_connection_call_sync(
priv->main_dbus_connection,
@@ -1465,12 +1435,12 @@ nm_dbus_manager_acquire_bus(NMDBusManager *self, gboolean request_name)
-1,
NULL,
&error);
+
if (!ret) {
_LOGE("fatal failure to acquire D-Bus service \"%s"
": %s",
NM_DBUS_SERVICE,
error->message);
- g_dbus_connection_unregister_object(priv->main_dbus_connection, registration_id);
return FALSE;
}
@@ -1479,13 +1449,58 @@ nm_dbus_manager_acquire_bus(NMDBusManager *self, gboolean request_name)
_LOGE("fatal failure to acquire D-Bus service \"%s\" (%u). Service already taken",
NM_DBUS_SERVICE,
(guint) result);
- g_dbus_connection_unregister_object(priv->main_dbus_connection, registration_id);
+ return FALSE;
+ }
+
+ _LOGI("acquired D-Bus service \"%s\"", NM_DBUS_SERVICE);
+ return TRUE;
+}
+
+gboolean
+nm_dbus_manager_setup(NMDBusManager *self)
+{
+ NMDBusManagerPrivate *priv;
+ gs_free_error GError *error = NULL;
+ guint registration_id;
+
+ g_return_val_if_fail(NM_IS_DBUS_MANAGER(self), FALSE);
+
+ priv = NM_DBUS_MANAGER_GET_PRIVATE(self);
+
+ g_return_val_if_fail(!priv->main_dbus_connection, FALSE);
+
+ /* Create the D-Bus connection and registering the name synchronously.
+ * That is necessary because we need to exit right away if we can't
+ * acquire the name despite connecting to the bus successfully.
+ * It means that something is gravely broken -- such as another NetworkManager
+ * instance running. */
+ priv->main_dbus_connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
+ if (!priv->main_dbus_connection) {
+ _LOGE("cannot connect to D-Bus: %s", error->message);
+ return FALSE;
+ }
+
+ g_dbus_connection_set_exit_on_close(priv->main_dbus_connection, FALSE);
+
+ registration_id = g_dbus_connection_register_object(
+ priv->main_dbus_connection,
+ OBJECT_MANAGER_SERVER_BASE_PATH,
+ NM_UNCONST_PTR(GDBusInterfaceInfo, &interface_info_objmgr),
+ &dbus_vtable_objmgr,
+ self,
+ NULL,
+ &error);
+ if (!registration_id) {
+ _LOGE("failure to register object manager: %s", error->message);
return FALSE;
}
priv->objmgr_registration_id = registration_id;
- _LOGI("acquired D-Bus service \"%s\"", NM_DBUS_SERVICE);
+ _LOGD("D-Bus connection created and ObjectManager object registered");
+
+ if (!nm_dbus_manager_request_name_sync(self))
+ return FALSE;
return TRUE;
}
diff --git a/src/core/nm-dbus-manager.h b/src/core/nm-dbus-manager.h
index b68161db88..078dbdd203 100644
--- a/src/core/nm-dbus-manager.h
+++ b/src/core/nm-dbus-manager.h
@@ -37,7 +37,9 @@ typedef void (*NMDBusManagerSetPropertyHandler)(NMDBusObject
GVariant *value,
gpointer user_data);
-gboolean nm_dbus_manager_acquire_bus(NMDBusManager *self, gboolean request_name);
+gboolean nm_dbus_manager_setup(NMDBusManager *self);
+
+gboolean nm_dbus_manager_request_name_sync(NMDBusManager *self);
GDBusConnection *nm_dbus_manager_get_dbus_connection(NMDBusManager *self);