From nobody Sat Feb 7 18:51:55 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 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=1570704888; cv=none; d=zoho.com; s=zohoarc; b=oQ6dWPUpDXLd2fLWFLqhd/9NSKCTTQaDWgkXdFHuo0YcBSvVZ/icJmAZtWiwhImz/gOGS2xOTYT6yAXiYiAOwqTQv7Aex7yPJVEHGL3yua7CD2haOoeUgeMArM6sTUCZ5PNwRf0c/uEn6t1YJo8aWKxC+dfH4HGETqBSpblNguI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1570704888; h=Content-Type:Content-Transfer-Encoding:Cc: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=kRhKNjUH8Qszaw6tWgjJvO8/bKa6g068l06EKvD6BcI=; b=mNrnMSRZjJfPnqVEqh7xFXmA4J3DqVxWlErw0nZFmGkuSpgvfRtfRI/2zYX5Oe0//x6Q/Wj/xqeknOqa8sEOA3HKFPXLCA5VLFsTLxi9kXK2ru40ku44nALoJXmo8cwM4QEEwIM6tCwEzHTGaOLNAK0lpKmMwm1vCiy00KzYyR0= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1570704888560612.3660685827534; Thu, 10 Oct 2019 03:54:48 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id F341218CB914; Thu, 10 Oct 2019 10:54:46 +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 CF6E161B61; Thu, 10 Oct 2019 10:54:46 +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 97CBB180BA98; Thu, 10 Oct 2019 10:54:46 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id x9AAsTJT010436 for ; Thu, 10 Oct 2019 06:54:29 -0400 Received: by smtp.corp.redhat.com (Postfix) id AA3335C22C; Thu, 10 Oct 2019 10:54:29 +0000 (UTC) Received: from dhcp-16-231.lcy.redhat.com (unknown [10.42.16.231]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8ECDD5C22F; Thu, 10 Oct 2019 10:54:26 +0000 (UTC) From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: libvir-list@redhat.com Date: Thu, 10 Oct 2019 11:54:04 +0100 Message-Id: <20191010105413.4091-11-berrange@redhat.com> In-Reply-To: <20191010105413.4091-1-berrange@redhat.com> References: <20191010105413.4091-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-loop: libvir-list@redhat.com Cc: =?UTF-8?q?J=C3=A1n=20Tomko?= Subject: [libvirt] [PATCH v3 10/19] util: convert virIdentity implementation and test suite to g_autoptr 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: , Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.6.2 (mx1.redhat.com [10.5.110.63]); Thu, 10 Oct 2019 10:54:47 +0000 (UTC) To simplify the later conversion from virObject to GObject, introduce the use of g_autoptr to the virIdentity implementnation and test suite. Reviewed-by: J=C3=A1n Tomko Signed-off-by: Daniel P. Berrang=C3=A9 --- src/util/viridentity.c | 36 ++++++++++++++------------------- tests/viridentitytest.c | 44 +++++++++++++++++------------------------ 2 files changed, 33 insertions(+), 47 deletions(-) diff --git a/src/util/viridentity.c b/src/util/viridentity.c index 22e2644c19..6636077161 100644 --- a/src/util/viridentity.c +++ b/src/util/viridentity.c @@ -105,7 +105,7 @@ virIdentityPtr virIdentityGetCurrent(void) */ int virIdentitySetCurrent(virIdentityPtr ident) { - virIdentityPtr old; + g_autoptr(virIdentity) old =3D NULL; =20 if (virIdentityInitialize() < 0) return -1; @@ -120,8 +120,6 @@ int virIdentitySetCurrent(virIdentityPtr ident) return -1; } =20 - virObjectUnref(old); - return 0; } =20 @@ -136,60 +134,56 @@ int virIdentitySetCurrent(virIdentityPtr ident) */ virIdentityPtr virIdentityGetSystem(void) { - VIR_AUTOFREE(char *) username =3D NULL; - VIR_AUTOFREE(char *) groupname =3D NULL; + g_autofree char *username =3D NULL; + g_autofree char *groupname =3D NULL; unsigned long long startTime; - virIdentityPtr ret =3D NULL; + g_autoptr(virIdentity) ret =3D NULL; #if WITH_SELINUX security_context_t con; #endif =20 if (!(ret =3D virIdentityNew())) - goto error; + return NULL; =20 if (virIdentitySetProcessID(ret, getpid()) < 0) - goto error; + return NULL; =20 if (virProcessGetStartTime(getpid(), &startTime) < 0) - goto error; + return NULL; if (startTime !=3D 0 && virIdentitySetProcessTime(ret, startTime) < 0) - goto error; + return NULL; =20 if (!(username =3D virGetUserName(geteuid()))) return ret; if (virIdentitySetUserName(ret, username) < 0) - goto error; + return NULL; if (virIdentitySetUNIXUserID(ret, getuid()) < 0) - goto error; + return NULL; =20 if (!(groupname =3D virGetGroupName(getegid()))) return ret; if (virIdentitySetGroupName(ret, groupname) < 0) - goto error; + return NULL; if (virIdentitySetUNIXGroupID(ret, getgid()) < 0) - goto error; + return NULL; =20 #if WITH_SELINUX if (is_selinux_enabled() > 0) { if (getcon(&con) < 0) { virReportSystemError(errno, "%s", _("Unable to lookup SELinux process conte= xt")); - return ret; + return NULL; } if (virIdentitySetSELinuxContext(ret, con) < 0) { freecon(con); - goto error; + return NULL; } freecon(con); } #endif =20 - return ret; - - error: - virObjectUnref(ret); - return NULL; + return g_steal_pointer(&ret); } =20 =20 diff --git a/tests/viridentitytest.c b/tests/viridentitytest.c index 1eadd6173a..db041a98a8 100644 --- a/tests/viridentitytest.c +++ b/tests/viridentitytest.c @@ -38,58 +38,53 @@ VIR_LOG_INIT("tests.identitytest"); =20 static int testIdentityAttrs(const void *data ATTRIBUTE_UNUSED) { - int ret =3D -1; - virIdentityPtr ident; + g_autoptr(virIdentity) ident =3D NULL; const char *val; int rc; =20 if (!(ident =3D virIdentityNew())) - goto cleanup; + return -1; =20 if (virIdentitySetUserName(ident, "fred") < 0) - goto cleanup; + return -1; =20 if ((rc =3D virIdentityGetUserName(ident, &val)) < 0) - goto cleanup; + return -1; =20 if (STRNEQ_NULLABLE(val, "fred") || rc !=3D 1) { VIR_DEBUG("Expected 'fred' got '%s'", NULLSTR(val)); - goto cleanup; + return -1; } =20 if ((rc =3D virIdentityGetGroupName(ident, &val)) < 0) - goto cleanup; + return -1; =20 if (val !=3D NULL || rc !=3D 0) { VIR_DEBUG("Unexpected groupname attribute"); - goto cleanup; + return -1; } =20 if (virIdentitySetUserName(ident, "joe") >=3D 0) { VIR_DEBUG("Unexpectedly overwrote attribute"); - goto cleanup; + return -1; } =20 if ((rc =3D virIdentityGetUserName(ident, &val)) < 0) - goto cleanup; + return -1; =20 if (STRNEQ_NULLABLE(val, "fred") || rc !=3D 1) { VIR_DEBUG("Expected 'fred' got '%s'", NULLSTR(val)); - goto cleanup; + return -1; } =20 - ret =3D 0; - cleanup: - virObjectUnref(ident); - return ret; + return 0; } =20 =20 static int testIdentityGetSystem(const void *data) { const char *context =3D data; - int ret =3D -1; - virIdentityPtr ident =3D NULL; + g_autoptr(virIdentity) ident =3D NULL; const char *val; int rc; =20 @@ -97,35 +92,32 @@ static int testIdentityGetSystem(const void *data) if (context) { VIR_DEBUG("libvirt not compiled with SELinux, skipping this test"); ret =3D EXIT_AM_SKIP; - goto cleanup; + return -1; } #endif =20 if (!(ident =3D virIdentityGetSystem())) { VIR_DEBUG("Unable to get system identity"); - goto cleanup; + return -1; } =20 if ((rc =3D virIdentityGetSELinuxContext(ident, &val)) < 0) - goto cleanup; + return -1; =20 if (context =3D=3D NULL) { if (val !=3D NULL || rc !=3D 0) { VIR_DEBUG("Unexpected SELinux context %s", NULLSTR(val)); - goto cleanup; + return -1; } } else { if (STRNEQ_NULLABLE(val, context) || rc !=3D 1) { VIR_DEBUG("Want SELinux context '%s' got '%s'", context, val); - goto cleanup; + return -1; } } =20 - ret =3D 0; - cleanup: - virObjectUnref(ident); - return ret; + return 0; } =20 static int testSetFakeSELinuxContext(const void *data ATTRIBUTE_UNUSED) --=20 2.21.0 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list