mirror of
https://github.com/libvirt/libvirt.git
synced 2025-01-08 07:03:19 -06:00
systemd: Add virSystemdGetMachineNameByPID
Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
This commit is contained in:
parent
9ba2646291
commit
92757d4d2d
@ -2274,6 +2274,7 @@ virSystemdCanHibernate;
|
||||
virSystemdCanHybridSleep;
|
||||
virSystemdCanSuspend;
|
||||
virSystemdCreateMachine;
|
||||
virSystemdGetMachineNameByPID;
|
||||
virSystemdMakeMachineName;
|
||||
virSystemdMakeScopeName;
|
||||
virSystemdMakeSliceName;
|
||||
|
@ -113,6 +113,7 @@ char *virSystemdMakeSliceName(const char *partition)
|
||||
return virBufferContentAndReset(&buf);
|
||||
}
|
||||
|
||||
|
||||
char *virSystemdMakeMachineName(const char *name,
|
||||
const char *drivername,
|
||||
bool privileged)
|
||||
@ -139,6 +140,61 @@ 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
|
||||
|
@ -55,4 +55,6 @@ int virSystemdCanHibernate(bool *result);
|
||||
|
||||
int virSystemdCanHybridSleep(bool *result);
|
||||
|
||||
char *virSystemdGetMachineNameByPID(pid_t pid);
|
||||
|
||||
#endif /* __VIR_SYSTEMD_H__ */
|
||||
|
@ -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");
|
||||
@ -338,6 +363,25 @@ static int testCreateNetwork(const void *opaque ATTRIBUTE_UNUSED)
|
||||
}
|
||||
|
||||
|
||||
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;
|
||||
const char *expected;
|
||||
@ -491,6 +535,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 { \
|
||||
|
Loading…
Reference in New Issue
Block a user