397dc2
From a3a5c16f04d044502eecedbef6043bce79043df9 Mon Sep 17 00:00:00 2001
397dc2
Message-Id: <a3a5c16f04d044502eecedbef6043bce79043df9@dist-git>
397dc2
From: Pavel Hrdina <phrdina@redhat.com>
397dc2
Date: Fri, 19 Feb 2021 13:33:53 +0100
397dc2
Subject: [PATCH] virsystemd: introduce virSystemdGetMachineByPID
397dc2
MIME-Version: 1.0
397dc2
Content-Type: text/plain; charset=UTF-8
397dc2
Content-Transfer-Encoding: 8bit
397dc2
397dc2
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
397dc2
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
397dc2
(cherry picked from commit 385704d5a4e1c02c21fb5779fa5067cf0d8ab56c)
397dc2
397dc2
Conflicts:
397dc2
    src/util/virsystemd.c
397dc2
        - missing upstream glib dbus rewrite
397dc2
397dc2
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1798463
397dc2
397dc2
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
397dc2
Message-Id: <7de7eae45f139e79c45731263924ae078f3e33c5.1613737828.git.phrdina@redhat.com>
397dc2
Reviewed-by: Ján Tomko <jtomko@redhat.com>
397dc2
---
397dc2
 src/util/virsystemd.c | 46 +++++++++++++++++++++++++++++++++----------
397dc2
 1 file changed, 36 insertions(+), 10 deletions(-)
397dc2
397dc2
diff --git a/src/util/virsystemd.c b/src/util/virsystemd.c
397dc2
index ca708cd1bd..394eb13f38 100644
397dc2
--- a/src/util/virsystemd.c
397dc2
+++ b/src/util/virsystemd.c
397dc2
@@ -200,19 +200,24 @@ virSystemdHasLogind(void)
397dc2
 }
397dc2
 
397dc2
 
397dc2
-char *
397dc2
-virSystemdGetMachineNameByPID(pid_t pid)
397dc2
+/**
397dc2
+ * virSystemdGetMachineByPID:
397dc2
+ * @conn: dbus connection
397dc2
+ * @pid: pid of running VM
397dc2
+ *
397dc2
+ * Returns dbus object path to VM registered with machined.
397dc2
+ * On error returns NULL.
397dc2
+ */
397dc2
+static char *
397dc2
+virSystemdGetMachineByPID(DBusConnection *conn,
397dc2
+                          pid_t pid)
397dc2
 {
397dc2
-    DBusConnection *conn;
397dc2
     DBusMessage *reply = NULL;
397dc2
-    char *name = NULL, *object = NULL;
397dc2
+    char *object = NULL;
397dc2
 
397dc2
     if (virSystemdHasMachined() < 0)
397dc2
         goto cleanup;
397dc2
 
397dc2
-    if (!(conn = virDBusGetSystemBus()))
397dc2
-        goto cleanup;
397dc2
-
397dc2
     if (virDBusCallMethod(conn, &reply, NULL,
397dc2
                           "org.freedesktop.machine1",
397dc2
                           "/org/freedesktop/machine1",
397dc2
@@ -224,12 +229,33 @@ virSystemdGetMachineNameByPID(pid_t pid)
397dc2
     if (virDBusMessageDecode(reply, "o", &object) < 0)
397dc2
         goto cleanup;
397dc2
 
397dc2
-    virDBusMessageUnref(reply);
397dc2
-    reply = NULL;
397dc2
-
397dc2
     VIR_DEBUG("Domain with pid %lld has object path '%s'",
397dc2
               (long long) pid, object);
397dc2
 
397dc2
+ cleanup:
397dc2
+    virDBusMessageUnref(reply);
397dc2
+
397dc2
+    return object;
397dc2
+}
397dc2
+
397dc2
+
397dc2
+char *
397dc2
+virSystemdGetMachineNameByPID(pid_t pid)
397dc2
+{
397dc2
+    DBusConnection *conn;
397dc2
+    DBusMessage *reply = NULL;
397dc2
+    char *name = NULL, *object = NULL;
397dc2
+
397dc2
+    if (virSystemdHasMachined() < 0)
397dc2
+        goto cleanup;
397dc2
+
397dc2
+    if (!(conn = virDBusGetSystemBus()))
397dc2
+        goto cleanup;
397dc2
+
397dc2
+    object = virSystemdGetMachineByPID(conn, pid);
397dc2
+    if (!object)
397dc2
+        goto cleanup;
397dc2
+
397dc2
     if (virDBusCallMethod(conn, &reply, NULL,
397dc2
                           "org.freedesktop.machine1",
397dc2
                           object,
397dc2
-- 
397dc2
2.30.0
397dc2