From 9ec1193393c48198fd05b795bcce0d607b45d4ee Mon Sep 17 00:00:00 2001
Message-Id: <9ec1193393c48198fd05b795bcce0d607b45d4ee@dist-git>
From: Pavel Hrdina <phrdina@redhat.com>
Date: Fri, 19 Feb 2021 13:33:54 +0100
Subject: [PATCH] virsystemd: introduce virSystemdGetMachineUnitByPID
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
(cherry picked from commit d3fb774b1ed548c0338b3338a87094dafea32aa2)
Conflicts:
src/util/virsystemd.c
tests/virsystemdtest.c
- missing upstream glib dbus rewrite
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1798463
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
Message-Id: <28be8d962cde455d215fe9ee09fbdcc4145e931f.1613737828.git.phrdina@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
---
src/libvirt_private.syms | 1 +
src/util/virsystemd.c | 48 ++++++++++++++++++++++++++++++++
src/util/virsystemd.h | 2 ++
tests/virsystemdtest.c | 59 ++++++++++++++++++++++++++++++++++------
4 files changed, 101 insertions(+), 9 deletions(-)
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index a869d1f7a4..af6f32fb1e 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -3241,6 +3241,7 @@ virSystemdCanSuspend;
virSystemdCreateMachine;
virSystemdGetActivation;
virSystemdGetMachineNameByPID;
+virSystemdGetMachineUnitByPID;
virSystemdHasLogind;
virSystemdHasLogindResetCachedValue;
virSystemdHasMachined;
diff --git a/src/util/virsystemd.c b/src/util/virsystemd.c
index 394eb13f38..0b8e21ae46 100644
--- a/src/util/virsystemd.c
+++ b/src/util/virsystemd.c
@@ -280,6 +280,54 @@ 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)
+{
+ DBusConnection *conn;
+ DBusMessage *reply = NULL;
+ char *unit = NULL, *object = NULL;
+
+ if (virSystemdHasMachined() < 0)
+ goto cleanup;
+
+ if (!(conn = virDBusGetSystemBus()))
+ goto cleanup;
+
+ object = virSystemdGetMachineByPID(conn, pid);
+ if (!object)
+ goto cleanup;
+
+ if (virDBusCallMethod(conn, &reply, NULL,
+ "org.freedesktop.machine1",
+ object,
+ "org.freedesktop.DBus.Properties",
+ "Get",
+ "ss",
+ "org.freedesktop.machine1.Machine",
+ "Unit") < 0)
+ goto cleanup;
+
+ if (virDBusMessageDecode(reply, "v", "s", &unit) < 0)
+ goto cleanup;
+
+ VIR_DEBUG("Domain with pid %lld has unit name '%s'",
+ (long long) pid, unit);
+
+ cleanup:
+ VIR_FREE(object);
+ virDBusMessageUnref(reply);
+
+ 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 eb510b40e4..475bf8debc 100644
--- a/tests/virsystemdtest.c
+++ b/tests/virsystemdtest.c
@@ -69,19 +69,42 @@ VIR_MOCK_WRAP_RET_ARGS(dbus_connection_send_with_reply_and_block,
&object_path))
goto error;
} else if (STREQ(member, "Get")) {
- const char *name = "qemu-demo";
+ const char *name = NULL;
+ char *iface = NULL;
+ char *prop = NULL;
DBusMessageIter iter;
DBusMessageIter sub;
- dbus_message_iter_init_append(reply, &iter);
- dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT,
- "s", &sub);
-
- if (!dbus_message_iter_append_basic(&sub,
- DBUS_TYPE_STRING,
- &name))
+ if (virDBusMessageDecode(message, "ss", &iface, &prop) < 0)
goto error;
- dbus_message_iter_close_container(&iter, &sub);
+
+ VIR_FREE(iface);
+
+ if (STREQ(prop, "Name")) {
+ name = "qemu-demo";
+ } else if (STREQ(prop, "Unit")) {
+ name = "machine-qemu-demo.scope";
+ } else {
+ dbus_set_error_const(error,
+ "org.freedesktop.systemd.badthing",
+ "Unknown machine property");
+ }
+
+ VIR_FREE(prop);
+
+ if (name) {
+ dbus_message_iter_init_append(reply, &iter);
+
+ dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT,
+ "s", &sub);
+
+ if (!dbus_message_iter_append_basic(&sub,
+ DBUS_TYPE_STRING,
+ &name))
+ goto error;
+
+ dbus_message_iter_close_container(&iter, &sub);
+ }
}
}
} else if (STREQ(service, "org.freedesktop.login1")) {
@@ -376,6 +399,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;
@@ -698,6 +738,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.30.0