d71922
From 108e6134a8f61d2c809b7d0e423fba4a5f796e00 Mon Sep 17 00:00:00 2001
d71922
Message-Id: <108e6134a8f61d2c809b7d0e423fba4a5f796e00@dist-git>
d71922
From: Martin Kletzander <mkletzan@redhat.com>
d71922
Date: Tue, 16 Feb 2016 11:55:08 +0100
d71922
Subject: [PATCH] systemd: Add virSystemdGetMachineNameByPID
d71922
d71922
https://bugzilla.redhat.com/show_bug.cgi?id=1308494
d71922
d71922
Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
d71922
(cherry picked from commit 92757d4d2d108cf612916928e733739d40a31942)
d71922
Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
d71922
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
d71922
---
d71922
 src/libvirt_private.syms |  1 +
d71922
 src/util/virsystemd.c    | 56 ++++++++++++++++++++++++++++++++++++++++++++++++
d71922
 src/util/virsystemd.h    |  2 ++
d71922
 tests/virsystemdtest.c   | 46 +++++++++++++++++++++++++++++++++++++++
d71922
 4 files changed, 105 insertions(+)
d71922
d71922
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
d71922
index 16ae24f..4ad9266 100644
d71922
--- a/src/libvirt_private.syms
d71922
+++ b/src/libvirt_private.syms
d71922
@@ -2228,6 +2228,7 @@ virSystemdCanHibernate;
d71922
 virSystemdCanHybridSleep;
d71922
 virSystemdCanSuspend;
d71922
 virSystemdCreateMachine;
d71922
+virSystemdGetMachineNameByPID;
d71922
 virSystemdMakeMachineName;
d71922
 virSystemdMakeScopeName;
d71922
 virSystemdMakeSliceName;
d71922
diff --git a/src/util/virsystemd.c b/src/util/virsystemd.c
d71922
index 0c8f026..daaa37c 100644
d71922
--- a/src/util/virsystemd.c
d71922
+++ b/src/util/virsystemd.c
d71922
@@ -113,6 +113,7 @@ char *virSystemdMakeSliceName(const char *partition)
d71922
     return virBufferContentAndReset(&buf;;
d71922
 }
d71922
 
d71922
+
d71922
 char *virSystemdMakeMachineName(const char *name,
d71922
                                 const char *drivername,
d71922
                                 bool privileged)
d71922
@@ -139,6 +140,61 @@ char *virSystemdMakeMachineName(const char *name,
d71922
     return machinename;
d71922
 }
d71922
 
d71922
+
d71922
+char *
d71922
+virSystemdGetMachineNameByPID(pid_t pid)
d71922
+{
d71922
+    DBusConnection *conn;
d71922
+    DBusMessage *reply;
d71922
+    char *name = NULL, *object = NULL;
d71922
+
d71922
+    if (virDBusIsServiceEnabled("org.freedesktop.machine1") < 0)
d71922
+        goto cleanup;
d71922
+
d71922
+    if (virDBusIsServiceRegistered("org.freedesktop.systemd1") < 0)
d71922
+        goto cleanup;
d71922
+
d71922
+    if (!(conn = virDBusGetSystemBus()))
d71922
+        goto cleanup;
d71922
+
d71922
+    if (virDBusCallMethod(conn, &reply, NULL,
d71922
+                          "org.freedesktop.machine1",
d71922
+                          "/org/freedesktop/machine1",
d71922
+                          "org.freedesktop.machine1.Manager",
d71922
+                          "GetMachineByPID",
d71922
+                          "u", pid) < 0)
d71922
+        goto cleanup;
d71922
+
d71922
+    if (virDBusMessageRead(reply, "o", &object) < 0)
d71922
+        goto cleanup;
d71922
+
d71922
+    VIR_DEBUG("Domain with pid %llu has object path '%s'",
d71922
+              (unsigned long long)pid, object);
d71922
+
d71922
+    if (virDBusCallMethod(conn, &reply, NULL,
d71922
+                          "org.freedesktop.machine1",
d71922
+                          object,
d71922
+                          "org.freedesktop.DBus.Properties",
d71922
+                          "Get",
d71922
+                          "ss",
d71922
+                          "org.freedesktop.machine1.Machine",
d71922
+                          "Name") < 0)
d71922
+        goto cleanup;
d71922
+
d71922
+    if (virDBusMessageRead(reply, "v", "s", &name) < 0)
d71922
+        goto cleanup;
d71922
+
d71922
+    VIR_DEBUG("Domain with pid %llu has machine name '%s'",
d71922
+              (unsigned long long)pid, name);
d71922
+
d71922
+ cleanup:
d71922
+    VIR_FREE(object);
d71922
+    dbus_message_unref(reply);
d71922
+
d71922
+    return name;
d71922
+}
d71922
+
d71922
+
d71922
 /**
d71922
  * virSystemdCreateMachine:
d71922
  * @name: driver unique name of the machine
d71922
diff --git a/src/util/virsystemd.h b/src/util/virsystemd.h
d71922
index 8af2169..a13a4c0 100644
d71922
--- a/src/util/virsystemd.h
d71922
+++ b/src/util/virsystemd.h
d71922
@@ -55,4 +55,6 @@ int virSystemdCanHibernate(bool *result);
d71922
 
d71922
 int virSystemdCanHybridSleep(bool *result);
d71922
 
d71922
+char *virSystemdGetMachineNameByPID(pid_t pid);
d71922
+
d71922
 #endif /* __VIR_SYSTEMD_H__ */
d71922
diff --git a/tests/virsystemdtest.c b/tests/virsystemdtest.c
d71922
index 06fec54..3a3cd99 100644
d71922
--- a/tests/virsystemdtest.c
d71922
+++ b/tests/virsystemdtest.c
d71922
@@ -54,6 +54,31 @@ VIR_MOCK_WRAP_RET_ARGS(dbus_connection_send_with_reply_and_block,
d71922
                                  "Something went wrong creating the machine");
d71922
         } else {
d71922
             reply = dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_RETURN);
d71922
+
d71922
+            if (STREQ(member, "GetMachineByPID")) {
d71922
+                const char *object_path = "/org/freedesktop/machine1/machine/qemu_2ddemo";
d71922
+                DBusMessageIter iter;
d71922
+
d71922
+                dbus_message_iter_init_append(reply, &iter);
d71922
+                if (!dbus_message_iter_append_basic(&iter,
d71922
+                                                    DBUS_TYPE_OBJECT_PATH,
d71922
+                                                    &object_path))
d71922
+                    goto error;
d71922
+            } else if (STREQ(member, "Get")) {
d71922
+                const char *name = "qemu-demo";
d71922
+                DBusMessageIter iter;
d71922
+                DBusMessageIter sub;
d71922
+
d71922
+                dbus_message_iter_init_append(reply, &iter);
d71922
+                dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT,
d71922
+                                                 "s", &sub);
d71922
+
d71922
+                if (!dbus_message_iter_append_basic(&sub,
d71922
+                                                    DBUS_TYPE_STRING,
d71922
+                                                    &name))
d71922
+                    goto error;
d71922
+                dbus_message_iter_close_container(&iter, &sub);
d71922
+            }
d71922
         }
d71922
     } else if (STREQ(service, "org.freedesktop.login1")) {
d71922
         char *supported = getenv("RESULT_SUPPORT");
d71922
@@ -338,6 +363,25 @@ static int testCreateNetwork(const void *opaque ATTRIBUTE_UNUSED)
d71922
 }
d71922
 
d71922
 
d71922
+static int
d71922
+testGetMachineName(const void *opaque ATTRIBUTE_UNUSED)
d71922
+{
d71922
+    char *tmp = virSystemdGetMachineNameByPID(1234);
d71922
+    int ret = -1;
d71922
+
d71922
+    if (!tmp) {
d71922
+        fprintf(stderr, "%s", "Failed to create LXC machine\n");
d71922
+        return ret;
d71922
+    }
d71922
+
d71922
+    if (STREQ(tmp, "qemu-demo"))
d71922
+        ret = 0;
d71922
+
d71922
+    VIR_FREE(tmp);
d71922
+    return ret;
d71922
+}
d71922
+
d71922
+
d71922
 struct testNameData {
d71922
     const char *name;
d71922
     const char *expected;
d71922
@@ -491,6 +535,8 @@ mymain(void)
d71922
         ret = -1;
d71922
     if (virtTestRun("Test create with network ", testCreateNetwork, NULL) < 0)
d71922
         ret = -1;
d71922
+    if (virtTestRun("Test getting machine name ", testGetMachineName, NULL) < 0)
d71922
+        ret = -1;
d71922
 
d71922
 # define TEST_SCOPE(name, unitname)                                     \
d71922
     do {                                                                \
d71922
-- 
d71922
2.7.2
d71922