[PATCH 2/3] systemd: Add virSystemdGetMachineNameByPID

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

 



Signed-off-by: Martin Kletzander <mkletzan@xxxxxxxxxx>
---
 src/libvirt_private.syms |  1 +
 src/util/virsystemd.c    | 53 ++++++++++++++++++++++++++++++++++++++++++++++++
 src/util/virsystemd.h    |  2 ++
 tests/virsystemdtest.c   | 44 ++++++++++++++++++++++++++++++++++++++++
 4 files changed, 100 insertions(+)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 3d0ec9d786b6..e2d552e0193d 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -2273,6 +2273,7 @@ virSystemdCanHibernate;
 virSystemdCanHybridSleep;
 virSystemdCanSuspend;
 virSystemdCreateMachine;
+virSystemdGetMachineNameByPID;
 virSystemdMakeMachineName;
 virSystemdMakeScopeName;
 virSystemdMakeSliceName;
diff --git a/src/util/virsystemd.c b/src/util/virsystemd.c
index abd883c73844..3349d95a4597 100644
--- a/src/util/virsystemd.c
+++ b/src/util/virsystemd.c
@@ -139,6 +139,59 @@ char *virSystemdMakeMachineName(const char *name,
     return machinename;
 }

+char *
+virSystemdGetMachineNameByPID(pid_t pid)
+{
+    DBusConnection *conn;
+    DBusMessage *reply;
+    char *name = NULL, *object = NULL;
+
+    if (virDBusIsServiceEnabled("org.freedesktop.machine1") < 0)
+        goto cleanup;
+
+    if (virDBusIsServiceRegistered("org.freedesktop.systemd1") < 0)
+        goto cleanup;
+
+    if (!(conn = virDBusGetSystemBus()))
+        goto cleanup;
+
+    if (virDBusCallMethod(conn, &reply, NULL,
+                          "org.freedesktop.machine1",
+                          "/org/freedesktop/machine1",
+                          "org.freedesktop.machine1.Manager",
+                          "GetMachineByPID",
+                          "u", pid) < 0)
+        goto cleanup;
+
+    if (virDBusMessageRead(reply, "o", &object) < 0)
+        goto cleanup;
+
+    VIR_DEBUG("Domain with pid %llu has object path '%s'",
+              (unsigned long long)pid, object);
+
+    if (virDBusCallMethod(conn, &reply, NULL,
+                          "org.freedesktop.machine1",
+                          object,
+                          "org.freedesktop.DBus.Properties",
+                          "Get",
+                          "ss",
+                          "org.freedesktop.machine1.Machine",
+                          "Name") < 0)
+        goto cleanup;
+
+    if (virDBusMessageRead(reply, "v", "s", &name) < 0)
+        goto cleanup;
+
+    VIR_DEBUG("Domain with pid %llu has machine name '%s'",
+              (unsigned long long)pid, name);
+
+ cleanup:
+    VIR_FREE(object);
+    dbus_message_unref(reply);
+
+    return name;
+}
+
 /**
  * virSystemdCreateMachine:
  * @name: driver unique name of the machine
diff --git a/src/util/virsystemd.h b/src/util/virsystemd.h
index 8af216910188..a13a4c0c48be 100644
--- a/src/util/virsystemd.h
+++ b/src/util/virsystemd.h
@@ -55,4 +55,6 @@ int virSystemdCanHibernate(bool *result);

 int virSystemdCanHybridSleep(bool *result);

+char *virSystemdGetMachineNameByPID(pid_t pid);
+
 #endif /* __VIR_SYSTEMD_H__ */
diff --git a/tests/virsystemdtest.c b/tests/virsystemdtest.c
index 06fec5495bc2..1746a02b131d 100644
--- a/tests/virsystemdtest.c
+++ b/tests/virsystemdtest.c
@@ -54,6 +54,31 @@ VIR_MOCK_WRAP_RET_ARGS(dbus_connection_send_with_reply_and_block,
                                  "Something went wrong creating the machine");
         } else {
             reply = dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_RETURN);
+
+            if (STREQ(member, "GetMachineByPID")) {
+                const char *object_path = "/org/freedesktop/machine1/machine/qemu_2ddemo";
+                DBusMessageIter iter;
+
+                dbus_message_iter_init_append(reply, &iter);
+                if (!dbus_message_iter_append_basic(&iter,
+                                                    DBUS_TYPE_OBJECT_PATH,
+                                                    &object_path))
+                    goto error;
+            } else if (STREQ(member, "Get")) {
+                const char *name = "qemu-demo";
+                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))
+                    goto error;
+                dbus_message_iter_close_container(&iter, &sub);
+            }
         }
     } else if (STREQ(service, "org.freedesktop.login1")) {
         char *supported = getenv("RESULT_SUPPORT");
@@ -337,6 +362,23 @@ static int testCreateNetwork(const void *opaque ATTRIBUTE_UNUSED)
     return 0;
 }

+static int testGetMachineName(const void *opaque ATTRIBUTE_UNUSED)
+{
+    char *tmp = virSystemdGetMachineNameByPID(1234);
+    int ret = -1;
+
+    if (!tmp) {
+        fprintf(stderr, "%s", "Failed to create LXC machine\n");
+        return ret;
+    }
+
+    if (STREQ(tmp, "qemu-demo"))
+        ret = 0;
+
+    VIR_FREE(tmp);
+    return ret;
+}
+

 struct testNameData {
     const char *name;
@@ -491,6 +533,8 @@ mymain(void)
         ret = -1;
     if (virtTestRun("Test create with network ", testCreateNetwork, NULL) < 0)
         ret = -1;
+    if (virtTestRun("Test getting machine name ", testGetMachineName, NULL) < 0)
+        ret = -1;

 # define TEST_SCOPE(name, unitname)                                     \
     do {                                                                \
-- 
2.7.0

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list



[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]