|
|
7548c0 |
From 9ec1193393c48198fd05b795bcce0d607b45d4ee Mon Sep 17 00:00:00 2001
|
|
|
7548c0 |
Message-Id: <9ec1193393c48198fd05b795bcce0d607b45d4ee@dist-git>
|
|
|
7548c0 |
From: Pavel Hrdina <phrdina@redhat.com>
|
|
|
7548c0 |
Date: Fri, 19 Feb 2021 13:33:54 +0100
|
|
|
7548c0 |
Subject: [PATCH] virsystemd: introduce virSystemdGetMachineUnitByPID
|
|
|
7548c0 |
MIME-Version: 1.0
|
|
|
7548c0 |
Content-Type: text/plain; charset=UTF-8
|
|
|
7548c0 |
Content-Transfer-Encoding: 8bit
|
|
|
7548c0 |
|
|
|
7548c0 |
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
|
|
|
7548c0 |
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
|
|
|
7548c0 |
(cherry picked from commit d3fb774b1ed548c0338b3338a87094dafea32aa2)
|
|
|
7548c0 |
|
|
|
7548c0 |
Conflicts:
|
|
|
7548c0 |
src/util/virsystemd.c
|
|
|
7548c0 |
tests/virsystemdtest.c
|
|
|
7548c0 |
- missing upstream glib dbus rewrite
|
|
|
7548c0 |
|
|
|
7548c0 |
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1798463
|
|
|
7548c0 |
|
|
|
7548c0 |
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
|
|
|
7548c0 |
Message-Id: <28be8d962cde455d215fe9ee09fbdcc4145e931f.1613737828.git.phrdina@redhat.com>
|
|
|
7548c0 |
Reviewed-by: Ján Tomko <jtomko@redhat.com>
|
|
|
7548c0 |
---
|
|
|
7548c0 |
src/libvirt_private.syms | 1 +
|
|
|
7548c0 |
src/util/virsystemd.c | 48 ++++++++++++++++++++++++++++++++
|
|
|
7548c0 |
src/util/virsystemd.h | 2 ++
|
|
|
7548c0 |
tests/virsystemdtest.c | 59 ++++++++++++++++++++++++++++++++++------
|
|
|
7548c0 |
4 files changed, 101 insertions(+), 9 deletions(-)
|
|
|
7548c0 |
|
|
|
7548c0 |
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
|
|
|
7548c0 |
index a869d1f7a4..af6f32fb1e 100644
|
|
|
7548c0 |
--- a/src/libvirt_private.syms
|
|
|
7548c0 |
+++ b/src/libvirt_private.syms
|
|
|
7548c0 |
@@ -3241,6 +3241,7 @@ virSystemdCanSuspend;
|
|
|
7548c0 |
virSystemdCreateMachine;
|
|
|
7548c0 |
virSystemdGetActivation;
|
|
|
7548c0 |
virSystemdGetMachineNameByPID;
|
|
|
7548c0 |
+virSystemdGetMachineUnitByPID;
|
|
|
7548c0 |
virSystemdHasLogind;
|
|
|
7548c0 |
virSystemdHasLogindResetCachedValue;
|
|
|
7548c0 |
virSystemdHasMachined;
|
|
|
7548c0 |
diff --git a/src/util/virsystemd.c b/src/util/virsystemd.c
|
|
|
7548c0 |
index 394eb13f38..0b8e21ae46 100644
|
|
|
7548c0 |
--- a/src/util/virsystemd.c
|
|
|
7548c0 |
+++ b/src/util/virsystemd.c
|
|
|
7548c0 |
@@ -280,6 +280,54 @@ virSystemdGetMachineNameByPID(pid_t pid)
|
|
|
7548c0 |
}
|
|
|
7548c0 |
|
|
|
7548c0 |
|
|
|
7548c0 |
+/**
|
|
|
7548c0 |
+ * virSystemdGetMachineUnitByPID:
|
|
|
7548c0 |
+ * @pid: pid of running VM
|
|
|
7548c0 |
+ *
|
|
|
7548c0 |
+ * Returns systemd Unit name of a running VM registered with machined.
|
|
|
7548c0 |
+ * On error returns NULL.
|
|
|
7548c0 |
+ */
|
|
|
7548c0 |
+char *
|
|
|
7548c0 |
+virSystemdGetMachineUnitByPID(pid_t pid)
|
|
|
7548c0 |
+{
|
|
|
7548c0 |
+ DBusConnection *conn;
|
|
|
7548c0 |
+ DBusMessage *reply = NULL;
|
|
|
7548c0 |
+ char *unit = NULL, *object = NULL;
|
|
|
7548c0 |
+
|
|
|
7548c0 |
+ if (virSystemdHasMachined() < 0)
|
|
|
7548c0 |
+ goto cleanup;
|
|
|
7548c0 |
+
|
|
|
7548c0 |
+ if (!(conn = virDBusGetSystemBus()))
|
|
|
7548c0 |
+ goto cleanup;
|
|
|
7548c0 |
+
|
|
|
7548c0 |
+ object = virSystemdGetMachineByPID(conn, pid);
|
|
|
7548c0 |
+ if (!object)
|
|
|
7548c0 |
+ goto cleanup;
|
|
|
7548c0 |
+
|
|
|
7548c0 |
+ if (virDBusCallMethod(conn, &reply, NULL,
|
|
|
7548c0 |
+ "org.freedesktop.machine1",
|
|
|
7548c0 |
+ object,
|
|
|
7548c0 |
+ "org.freedesktop.DBus.Properties",
|
|
|
7548c0 |
+ "Get",
|
|
|
7548c0 |
+ "ss",
|
|
|
7548c0 |
+ "org.freedesktop.machine1.Machine",
|
|
|
7548c0 |
+ "Unit") < 0)
|
|
|
7548c0 |
+ goto cleanup;
|
|
|
7548c0 |
+
|
|
|
7548c0 |
+ if (virDBusMessageDecode(reply, "v", "s", &unit) < 0)
|
|
|
7548c0 |
+ goto cleanup;
|
|
|
7548c0 |
+
|
|
|
7548c0 |
+ VIR_DEBUG("Domain with pid %lld has unit name '%s'",
|
|
|
7548c0 |
+ (long long) pid, unit);
|
|
|
7548c0 |
+
|
|
|
7548c0 |
+ cleanup:
|
|
|
7548c0 |
+ VIR_FREE(object);
|
|
|
7548c0 |
+ virDBusMessageUnref(reply);
|
|
|
7548c0 |
+
|
|
|
7548c0 |
+ return unit;
|
|
|
7548c0 |
+}
|
|
|
7548c0 |
+
|
|
|
7548c0 |
+
|
|
|
7548c0 |
/**
|
|
|
7548c0 |
* virSystemdCreateMachine:
|
|
|
7548c0 |
* @name: driver unique name of the machine
|
|
|
7548c0 |
diff --git a/src/util/virsystemd.h b/src/util/virsystemd.h
|
|
|
7548c0 |
index 9ce16b7de1..cd329c49f9 100644
|
|
|
7548c0 |
--- a/src/util/virsystemd.h
|
|
|
7548c0 |
+++ b/src/util/virsystemd.h
|
|
|
7548c0 |
@@ -69,6 +69,8 @@ int virSystemdCanHybridSleep(bool *result);
|
|
|
7548c0 |
|
|
|
7548c0 |
char *virSystemdGetMachineNameByPID(pid_t pid);
|
|
|
7548c0 |
|
|
|
7548c0 |
+char *virSystemdGetMachineUnitByPID(pid_t pid);
|
|
|
7548c0 |
+
|
|
|
7548c0 |
int virSystemdGetActivation(virSystemdActivationMap *map,
|
|
|
7548c0 |
size_t nmap,
|
|
|
7548c0 |
virSystemdActivationPtr *act);
|
|
|
7548c0 |
diff --git a/tests/virsystemdtest.c b/tests/virsystemdtest.c
|
|
|
7548c0 |
index eb510b40e4..475bf8debc 100644
|
|
|
7548c0 |
--- a/tests/virsystemdtest.c
|
|
|
7548c0 |
+++ b/tests/virsystemdtest.c
|
|
|
7548c0 |
@@ -69,19 +69,42 @@ VIR_MOCK_WRAP_RET_ARGS(dbus_connection_send_with_reply_and_block,
|
|
|
7548c0 |
&object_path))
|
|
|
7548c0 |
goto error;
|
|
|
7548c0 |
} else if (STREQ(member, "Get")) {
|
|
|
7548c0 |
- const char *name = "qemu-demo";
|
|
|
7548c0 |
+ const char *name = NULL;
|
|
|
7548c0 |
+ char *iface = NULL;
|
|
|
7548c0 |
+ char *prop = NULL;
|
|
|
7548c0 |
DBusMessageIter iter;
|
|
|
7548c0 |
DBusMessageIter sub;
|
|
|
7548c0 |
|
|
|
7548c0 |
- dbus_message_iter_init_append(reply, &iter);
|
|
|
7548c0 |
- dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT,
|
|
|
7548c0 |
- "s", &sub);
|
|
|
7548c0 |
-
|
|
|
7548c0 |
- if (!dbus_message_iter_append_basic(&sub,
|
|
|
7548c0 |
- DBUS_TYPE_STRING,
|
|
|
7548c0 |
- &name))
|
|
|
7548c0 |
+ if (virDBusMessageDecode(message, "ss", &iface, &prop) < 0)
|
|
|
7548c0 |
goto error;
|
|
|
7548c0 |
- dbus_message_iter_close_container(&iter, &sub);
|
|
|
7548c0 |
+
|
|
|
7548c0 |
+ VIR_FREE(iface);
|
|
|
7548c0 |
+
|
|
|
7548c0 |
+ if (STREQ(prop, "Name")) {
|
|
|
7548c0 |
+ name = "qemu-demo";
|
|
|
7548c0 |
+ } else if (STREQ(prop, "Unit")) {
|
|
|
7548c0 |
+ name = "machine-qemu-demo.scope";
|
|
|
7548c0 |
+ } else {
|
|
|
7548c0 |
+ dbus_set_error_const(error,
|
|
|
7548c0 |
+ "org.freedesktop.systemd.badthing",
|
|
|
7548c0 |
+ "Unknown machine property");
|
|
|
7548c0 |
+ }
|
|
|
7548c0 |
+
|
|
|
7548c0 |
+ VIR_FREE(prop);
|
|
|
7548c0 |
+
|
|
|
7548c0 |
+ if (name) {
|
|
|
7548c0 |
+ dbus_message_iter_init_append(reply, &iter);
|
|
|
7548c0 |
+
|
|
|
7548c0 |
+ dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT,
|
|
|
7548c0 |
+ "s", &sub);
|
|
|
7548c0 |
+
|
|
|
7548c0 |
+ if (!dbus_message_iter_append_basic(&sub,
|
|
|
7548c0 |
+ DBUS_TYPE_STRING,
|
|
|
7548c0 |
+ &name))
|
|
|
7548c0 |
+ goto error;
|
|
|
7548c0 |
+
|
|
|
7548c0 |
+ dbus_message_iter_close_container(&iter, &sub);
|
|
|
7548c0 |
+ }
|
|
|
7548c0 |
}
|
|
|
7548c0 |
}
|
|
|
7548c0 |
} else if (STREQ(service, "org.freedesktop.login1")) {
|
|
|
7548c0 |
@@ -376,6 +399,23 @@ testGetMachineName(const void *opaque G_GNUC_UNUSED)
|
|
|
7548c0 |
}
|
|
|
7548c0 |
|
|
|
7548c0 |
|
|
|
7548c0 |
+static int
|
|
|
7548c0 |
+testGetMachineUnit(const void *opaque G_GNUC_UNUSED)
|
|
|
7548c0 |
+{
|
|
|
7548c0 |
+ g_autofree char *tmp = virSystemdGetMachineUnitByPID(1234);
|
|
|
7548c0 |
+
|
|
|
7548c0 |
+ if (!tmp) {
|
|
|
7548c0 |
+ fprintf(stderr, "%s", "Failed to create get machine unit\n");
|
|
|
7548c0 |
+ return -1;
|
|
|
7548c0 |
+ }
|
|
|
7548c0 |
+
|
|
|
7548c0 |
+ if (STREQ(tmp, "machine-qemu-demo.scope"))
|
|
|
7548c0 |
+ return 0;
|
|
|
7548c0 |
+
|
|
|
7548c0 |
+ return -1;
|
|
|
7548c0 |
+}
|
|
|
7548c0 |
+
|
|
|
7548c0 |
+
|
|
|
7548c0 |
struct testNameData {
|
|
|
7548c0 |
const char *name;
|
|
|
7548c0 |
const char *expected;
|
|
|
7548c0 |
@@ -698,6 +738,7 @@ mymain(void)
|
|
|
7548c0 |
DO_TEST("Test create bad systemd ", testCreateBadSystemd);
|
|
|
7548c0 |
DO_TEST("Test create with network ", testCreateNetwork);
|
|
|
7548c0 |
DO_TEST("Test getting machine name ", testGetMachineName);
|
|
|
7548c0 |
+ DO_TEST("Test getting machine unit ", testGetMachineUnit);
|
|
|
7548c0 |
|
|
|
7548c0 |
# define TEST_SCOPE(_name, unitname, _legacy) \
|
|
|
7548c0 |
do { \
|
|
|
7548c0 |
--
|
|
|
7548c0 |
2.30.0
|
|
|
7548c0 |
|