From nobody Mon Feb 9 14:33:12 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 63.128.21.124 as permitted sender) client-ip=63.128.21.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 63.128.21.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1612877204; cv=none; d=zohomail.com; s=zohoarc; b=Tn5V16G2rv7xNj4F70B9G52EJd55TZa6YVpxhos7tHGpS5I42Cv0sKAr94sVC7eb3e6egBhm2kFmUhlHNRMIYYhFDJhJQ0q9SkvI4s1/BWq3e1j4GoIP9NxWmUsmZawyZm3GK01bOt+B/knjC9578Hr8LBROVUJPqoomABe2zro= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1612877204; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=HOiTXP5kbNibnyalijZsCfNG3fQG+viJ6OIXU15B/Ck=; b=BzV78m4LqKITjpLO3hUynqG5BwFQxNOclhGM+66eOu7i9RAUJefeZckz9n9uzbknm3D3xY1EGvekIxCLsRdxfnlfuTQqyYrCxOC1OvBof1OY14Vnqzc2PXW2EguWe7QILx9T8u2kuhDIlzhVQDRYTNA8Bb0yKGJLPkT4wdNUhEY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 63.128.21.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [63.128.21.124]) by mx.zohomail.com with SMTPS id 161287720370185.27349275977224; Tue, 9 Feb 2021 05:26:43 -0800 (PST) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-562-V-pLUSThMuK6fjhUq9OOtg-1; Tue, 09 Feb 2021 08:26:41 -0500 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 77E8B80196E; Tue, 9 Feb 2021 13:26:35 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id C5C1D60CC4; Tue, 9 Feb 2021 13:26:34 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 999FB1809C8F; Tue, 9 Feb 2021 13:26:33 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 119DQMFk020409 for ; Tue, 9 Feb 2021 08:26:22 -0500 Received: by smtp.corp.redhat.com (Postfix) id D9F745D9CD; Tue, 9 Feb 2021 13:26:22 +0000 (UTC) Received: from antique-laptop.redhat.com (unknown [10.40.208.58]) by smtp.corp.redhat.com (Postfix) with ESMTP id 3C1435D9C0 for ; Tue, 9 Feb 2021 13:26:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1612877202; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=HOiTXP5kbNibnyalijZsCfNG3fQG+viJ6OIXU15B/Ck=; b=ioJmkaTOJPxOhSUDtAEvpvQt5ljXCZTt+3sD6n8TklR6+SL4WWc3TsfmPZjBsO0GTG14kx /15bBMiH5PJK6Y45C/tCsjJiIv/Mm0LqsNZ01xQP/GfEwZl94DuyWzI8PJx+2YFiBPXqM7 4qLZWdvboM+zRvBvHELz6n5p8gG0yBU= X-MC-Unique: V-pLUSThMuK6fjhUq9OOtg-1 From: Pavel Hrdina To: libvir-list@redhat.com Subject: [libvirt PATCH 3/9] virsystemd: introduce virSystemdGetMachineUnitByPID Date: Tue, 9 Feb 2021 14:26:08 +0100 Message-Id: <07d5f5519b3b85ed34e9752aea3d664d06c7ed99.1612875677.git.phrdina@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: libvir-list@redhat.com X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" Signed-off-by: Pavel Hrdina --- src/libvirt_private.syms | 1 + src/util/virsystemd.c | 51 ++++++++++++++++++++++++++++++++++++++++ src/util/virsystemd.h | 2 ++ tests/virsystemdtest.c | 39 ++++++++++++++++++++++++++---- 4 files changed, 88 insertions(+), 5 deletions(-) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index b2d2282ce0..837986845f 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -3302,6 +3302,7 @@ virSystemdCanSuspend; virSystemdCreateMachine; virSystemdGetActivation; virSystemdGetMachineNameByPID; +virSystemdGetMachineUnitByPID; virSystemdHasLogind; virSystemdHasLogindResetCachedValue; virSystemdHasMachined; diff --git a/src/util/virsystemd.c b/src/util/virsystemd.c index d2fcf0bf0c..c109324e96 100644 --- a/src/util/virsystemd.c +++ b/src/util/virsystemd.c @@ -276,6 +276,57 @@ virSystemdGetMachineNameByPID(pid_t pid) } =20 =20 +/** + * 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 =3D NULL; + g_autoptr(GVariant) reply =3D NULL; + g_autoptr(GVariant) gvar =3D NULL; + g_autofree char *object =3D NULL; + char *unit =3D NULL; + + if (virSystemdHasMachined() < 0) + return NULL; + + if (!(conn =3D virGDBusGetSystemBus())) + return NULL; + + object =3D virSystemdGetMachineByPID(conn, pid); + if (!object) + return NULL; + + message =3D 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 diff --git a/src/util/virsystemd.h b/src/util/virsystemd.h index 9ce16b7de1..cd329c49f9 100644 --- a/src/util/virsystemd.h +++ b/src/util/virsystemd.h @@ -69,6 +69,8 @@ int virSystemdCanHybridSleep(bool *result); =20 char *virSystemdGetMachineNameByPID(pid_t pid); =20 +char *virSystemdGetMachineUnitByPID(pid_t pid); + int virSystemdGetActivation(virSystemdActivationMap *map, size_t nmap, virSystemdActivationPtr *act); diff --git a/tests/virsystemdtest.c b/tests/virsystemdtest.c index bd0ca51140..b48cdb950c 100644 --- a/tests/virsystemdtest.c +++ b/tests/virsystemdtest.c @@ -53,11 +53,10 @@ VIR_MOCK_WRAP_RET_ARGS(g_dbus_connection_call_sync, GError **, error) { GVariant *reply =3D NULL; + g_autoptr(GVariant) params =3D parameters; =20 - if (parameters) { - g_variant_ref_sink(parameters); - g_variant_unref(parameters); - } + if (params) + g_variant_ref_sink(params); =20 VIR_MOCK_REAL_INIT(g_dbus_connection_call_sync); =20 @@ -71,7 +70,19 @@ VIR_MOCK_WRAP_RET_ARGS(g_dbus_connection_call_sync, reply =3D g_variant_new("(o)", "/org/freedesktop/machine1/machine/q= emu_2ddemo"); } else if (STREQ(method_name, "Get")) { - reply =3D 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 =3D g_variant_new("(v)", g_variant_new_string("q= emu-demo")); + } else if (STREQ(prop, "Unit")) { + reply =3D g_variant_new("(v)", + g_variant_new_string("machine-qe= mu-demo.scope")); + } else { + *error =3D g_dbus_error_new_for_dbus_error( + "org.freedesktop.systemd.badthing", + "Unknown machine property"); + } } else { reply =3D g_variant_new("()"); } @@ -330,6 +341,23 @@ testGetMachineName(const void *opaque G_GNUC_UNUSED) } =20 =20 +static int +testGetMachineUnit(const void *opaque G_GNUC_UNUSED) +{ + g_autofree char *tmp =3D 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); =20 # define TEST_SCOPE(_name, unitname, _legacy) \ do { \ --=20 2.29.2