mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-02-01 17:35:17 +00:00
virsystemd: introduce virSystemdGetMachineUnitByPID
Signed-off-by: Pavel Hrdina <phrdina@redhat.com> Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
This commit is contained in:
parent
385704d5a4
commit
d3fb774b1e
@ -3302,6 +3302,7 @@ virSystemdCanSuspend;
|
||||
virSystemdCreateMachine;
|
||||
virSystemdGetActivation;
|
||||
virSystemdGetMachineNameByPID;
|
||||
virSystemdGetMachineUnitByPID;
|
||||
virSystemdHasLogind;
|
||||
virSystemdHasLogindResetCachedValue;
|
||||
virSystemdHasMachined;
|
||||
|
@ -276,6 +276,57 @@ virSystemdGetMachineNameByPID(pid_t pid)
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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
|
||||
|
@ -69,6 +69,8 @@ int virSystemdCanHybridSleep(bool *result);
|
||||
|
||||
char *virSystemdGetMachineNameByPID(pid_t pid);
|
||||
|
||||
char *virSystemdGetMachineUnitByPID(pid_t pid);
|
||||
|
||||
int virSystemdGetActivation(virSystemdActivationMap *map,
|
||||
size_t nmap,
|
||||
virSystemdActivationPtr *act);
|
||||
|
@ -53,11 +53,10 @@ VIR_MOCK_WRAP_RET_ARGS(g_dbus_connection_call_sync,
|
||||
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);
|
||||
|
||||
@ -71,7 +70,19 @@ VIR_MOCK_WRAP_RET_ARGS(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("()");
|
||||
}
|
||||
@ -330,6 +341,23 @@ testGetMachineName(const void *opaque G_GNUC_UNUSED)
|
||||
}
|
||||
|
||||
|
||||
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;
|
||||
@ -656,6 +684,7 @@ mymain(void)
|
||||
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 { \
|
||||
|
Loading…
x
Reference in New Issue
Block a user