]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
virsystemd: introduce virSystemdGetMachineUnitByPID
authorPavel Hrdina <phrdina@redhat.com>
Tue, 27 Oct 2020 13:15:03 +0000 (14:15 +0100)
committerPavel Hrdina <phrdina@redhat.com>
Wed, 10 Feb 2021 12:37:11 +0000 (13:37 +0100)
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
src/libvirt_private.syms
src/util/virsystemd.c
src/util/virsystemd.h
tests/virsystemdtest.c

index b2d2282ce0923efb1dfc9520d6813c68dc9aafb9..837986845f35e87879b9fc8149da9d64b32c925f 100644 (file)
@@ -3302,6 +3302,7 @@ virSystemdCanSuspend;
 virSystemdCreateMachine;
 virSystemdGetActivation;
 virSystemdGetMachineNameByPID;
+virSystemdGetMachineUnitByPID;
 virSystemdHasLogind;
 virSystemdHasLogindResetCachedValue;
 virSystemdHasMachined;
index d2fcf0bf0cc2dee0c0140443ebae8a911e099892..c109324e962cc85a42afee1f186435f088b17fb8 100644 (file)
@@ -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
index 9ce16b7de1315cd2ae58cb157533a6423cdd0a46..cd329c49f9e1902e59df864d8076935b81ee0a9d 100644 (file)
@@ -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);
index bd0ca51140cb39a1423400b69da47ae7cb43eae0..b48cdb950ca9675fe6c0573dbba342c238cc309a 100644 (file)
@@ -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 { \