[libvirt PATCH 3/9] virsystemd: introduce virSystemdGetMachineUnitByPID

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Signed-off-by: Pavel Hrdina <phrdina@xxxxxxxxxx>
---
 src/libvirt_private.syms |  1 +
 src/util/virsystemd.c    | 51 ++++++++++++++++++++++++++++++++++++++++
 src/util/virsystemd.h    |  2 ++
 tests/virsystemdtest.c   | 39 ++++++++++++++++++++++++++----
 4 files changed, 88 insertions(+), 5 deletions(-)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index b2d2282ce0..837986845f 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -3302,6 +3302,7 @@ virSystemdCanSuspend;
 virSystemdCreateMachine;
 virSystemdGetActivation;
 virSystemdGetMachineNameByPID;
+virSystemdGetMachineUnitByPID;
 virSystemdHasLogind;
 virSystemdHasLogindResetCachedValue;
 virSystemdHasMachined;
diff --git a/src/util/virsystemd.c b/src/util/virsystemd.c
index d2fcf0bf0c..c109324e96 100644
--- a/src/util/virsystemd.c
+++ b/src/util/virsystemd.c
@@ -276,6 +276,57 @@ virSystemdGetMachineNameByPID(pid_t pid)
 }
 
 
+/**
+ * virSystemdGetMachineUnitByPID:
+ * @pid: pid of running VM
+ *
+ * Returns systemd Unit name of a running VM registered with machined.
+ * On error returns NULL.
+ */
+char *
+virSystemdGetMachineUnitByPID(pid_t pid)
+{
+    GDBusConnection *conn;
+    g_autoptr(GVariant) message = NULL;
+    g_autoptr(GVariant) reply = NULL;
+    g_autoptr(GVariant) gvar = NULL;
+    g_autofree char *object = NULL;
+    char *unit = NULL;
+
+    if (virSystemdHasMachined() < 0)
+        return NULL;
+
+    if (!(conn = virGDBusGetSystemBus()))
+        return NULL;
+
+    object = virSystemdGetMachineByPID(conn, pid);
+    if (!object)
+        return NULL;
+
+    message = g_variant_new("(ss)",
+                            "org.freedesktop.machine1.Machine", "Unit");
+
+    if (virGDBusCallMethod(conn,
+                           &reply,
+                           G_VARIANT_TYPE("(v)"),
+                           NULL,
+                           "org.freedesktop.machine1",
+                           object,
+                           "org.freedesktop.DBus.Properties",
+                           "Get",
+                           message) < 0)
+        return NULL;
+
+    g_variant_get(reply, "(v)", &gvar);
+    g_variant_get(gvar, "s", &unit);
+
+    VIR_DEBUG("Domain with pid %lld has unit name '%s'",
+              (long long) pid, unit);
+
+    return unit;
+}
+
+
 /**
  * virSystemdCreateMachine:
  * @name: driver unique name of the machine
diff --git a/src/util/virsystemd.h b/src/util/virsystemd.h
index 9ce16b7de1..cd329c49f9 100644
--- a/src/util/virsystemd.h
+++ b/src/util/virsystemd.h
@@ -69,6 +69,8 @@ int virSystemdCanHybridSleep(bool *result);
 
 char *virSystemdGetMachineNameByPID(pid_t pid);
 
+char *virSystemdGetMachineUnitByPID(pid_t pid);
+
 int virSystemdGetActivation(virSystemdActivationMap *map,
                             size_t nmap,
                             virSystemdActivationPtr *act);
diff --git a/tests/virsystemdtest.c b/tests/virsystemdtest.c
index bd0ca51140..b48cdb950c 100644
--- a/tests/virsystemdtest.c
+++ b/tests/virsystemdtest.c
@@ -53,11 +53,10 @@ VIR_MOCK_WRAP_RET_ARGS(g_dbus_connection_call_sync,
                        GError **, error)
 {
     GVariant *reply = NULL;
+    g_autoptr(GVariant) params = parameters;
 
-    if (parameters) {
-        g_variant_ref_sink(parameters);
-        g_variant_unref(parameters);
-    }
+    if (params)
+        g_variant_ref_sink(params);
 
     VIR_MOCK_REAL_INIT(g_dbus_connection_call_sync);
 
@@ -71,7 +70,19 @@ VIR_MOCK_WRAP_RET_ARGS(g_dbus_connection_call_sync,
                 reply = g_variant_new("(o)",
                                       "/org/freedesktop/machine1/machine/qemu_2ddemo");
             } else if (STREQ(method_name, "Get")) {
-                reply = g_variant_new("(v)", g_variant_new_string("qemu-demo"));
+                const char *prop;
+                g_variant_get(params, "(@s&s)", NULL, &prop);
+
+                if (STREQ(prop, "Name")) {
+                    reply = g_variant_new("(v)", g_variant_new_string("qemu-demo"));
+                } else if (STREQ(prop, "Unit")) {
+                    reply = g_variant_new("(v)",
+                                          g_variant_new_string("machine-qemu-demo.scope"));
+                } else {
+                    *error = g_dbus_error_new_for_dbus_error(
+                            "org.freedesktop.systemd.badthing",
+                            "Unknown machine property");
+                }
             } else {
                 reply = g_variant_new("()");
             }
@@ -330,6 +341,23 @@ testGetMachineName(const void *opaque G_GNUC_UNUSED)
 }
 
 
+static int
+testGetMachineUnit(const void *opaque G_GNUC_UNUSED)
+{
+    g_autofree char *tmp = virSystemdGetMachineUnitByPID(1234);
+
+    if (!tmp) {
+        fprintf(stderr, "%s", "Failed to create get machine unit\n");
+        return -1;
+    }
+
+    if (STREQ(tmp, "machine-qemu-demo.scope"))
+        return 0;
+
+    return -1;
+}
+
+
 struct testNameData {
     const char *name;
     const char *expected;
@@ -656,6 +684,7 @@ mymain(void)
     DO_TEST("Test create bad systemd ", testCreateBadSystemd);
     DO_TEST("Test create with network ", testCreateNetwork);
     DO_TEST("Test getting machine name ", testGetMachineName);
+    DO_TEST("Test getting machine unit ", testGetMachineUnit);
 
 # define TEST_SCOPE(_name, unitname, _legacy) \
     do { \
-- 
2.29.2




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux