}
+/**
+ * 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
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);
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("()");
}
}
+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;
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 { \