summary refs log tree commit diff
path: root/src/core/nm-dbus-manager.c
diff options
context:
space:
mode:
authorMichael Biebl <biebl@debian.org>2023-08-09 21:55:35 +0200
committerMichael Biebl <biebl@debian.org>2023-08-09 21:55:35 +0200
commit05e4a733f2141995181a551854d5df929f084adf (patch)
tree83bb937740a6667525ba0df046748ecaa829c269 /src/core/nm-dbus-manager.c
parent14b0f3a9dc9ea90d60a3b057350fd4d637dc021a (diff)
New upstream version 1.44.0 upstream/1.44.0
Diffstat (limited to 'src/core/nm-dbus-manager.c')
-rw-r--r--src/core/nm-dbus-manager.c81
1 files changed, 49 insertions, 32 deletions
diff --git a/src/core/nm-dbus-manager.c b/src/core/nm-dbus-manager.c
index af7de8c4..0bde5971 100644
--- a/src/core/nm-dbus-manager.c
+++ b/src/core/nm-dbus-manager.c
@@ -1410,48 +1410,23 @@ 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");
+    if (priv->objmgr_registration_id == 0) {
+        /* Do nothing. We're presumably in the configure-and-quit mode. */
         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 +1440,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 +1454,55 @@ 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");
 
     return TRUE;
 }