From nobody Mon Feb 9 15:11:19 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) client-ip=170.10.133.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 170.10.133.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=1644575580; cv=none; d=zohomail.com; s=zohoarc; b=OXZgdd4Ms3zZuDt+LU0s46SUUwV9kv/Hj9Jcm7EG4mdsrzlBpqmmfxPE0IWd6TKpZCD/qiCqM+ul7aLt6ucCBChfGYpNCpXFn2fudCBmgKClk2jDTh6ZWLCEzODNwRyqmcUgu4HkWQYMUxyvM687vGLQGUw+6Ie5EI4P6ubWMRs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1644575580; 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=FciPPUgZoX04r+UKTM82Y3tqJQffG7M/ZA1XvuAQeew=; b=j6vBH/AHuYls9q/PdmkJqcncVThnGq0aSBWt+2c/hGDs7GrMcchHMhif5Hx3Skcm4u/kD+elRfNQfT8ENeO76n/HuPMSR5Tt//sActg1aeJHCNtJmsczKj/8eHHaTk/lDS7CH3OuLJlKznO3HEWjEAAux4b3ML7yR/GSSlqsI+0= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by mx.zohomail.com with SMTPS id 1644575580173351.49110213619736; Fri, 11 Feb 2022 02:33:00 -0800 (PST) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-178-OJV4mXi0O4aqJ0bVx10_YA-1; Fri, 11 Feb 2022 05:32:55 -0500 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id F166294B203; Fri, 11 Feb 2022 10:31:14 +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 B79EA7C141; Fri, 11 Feb 2022 10:31:14 +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 74AD11806D1C; Fri, 11 Feb 2022 10:31:14 +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 21BAUws1031777 for ; Fri, 11 Feb 2022 05:30:58 -0500 Received: by smtp.corp.redhat.com (Postfix) id A65E17C0D6; Fri, 11 Feb 2022 10:30:58 +0000 (UTC) Received: from work.speedport.ip (unknown [10.39.193.135]) by smtp.corp.redhat.com (Postfix) with ESMTP id EA0017BB4A; Fri, 11 Feb 2022 10:30:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1644575579; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc: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=FciPPUgZoX04r+UKTM82Y3tqJQffG7M/ZA1XvuAQeew=; b=PA2MyvKn/XUiN2GRBjISd1J4jvf6o8cbDv0BVSk9xA9gBrievTKrqsX/FIYk6AaPwg6ESl trmlT/XB6dFa6QsYP1Y4rnrkRCVd5QmhH/mW1dgO+yf3yeyrFsgtvyy94Y7rDJc6oBObFy /cuOgCO2mWukr/bPXcMiiSynTC9BNvU= X-MC-Unique: OJV4mXi0O4aqJ0bVx10_YA-1 From: Tim Wiederhake To: libvir-list@redhat.com Subject: [libvirt PATCH 09/10] qemusecuritymock: Use automatic mutex management Date: Fri, 11 Feb 2022 11:30:45 +0100 Message-Id: <20220211103046.34651-10-twiederh@redhat.com> In-Reply-To: <20220211103046.34651-1-twiederh@redhat.com> References: <20220211103046.34651-1-twiederh@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-loop: libvir-list@redhat.com Cc: Tim Wiederhake 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.16 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) X-ZM-MESSAGEID: 1644575581921100003 Content-Type: text/plain; charset="utf-8" Signed-off-by: Tim Wiederhake --- tests/qemusecuritymock.c | 88 +++++++++++++--------------------------- 1 file changed, 29 insertions(+), 59 deletions(-) diff --git a/tests/qemusecuritymock.c b/tests/qemusecuritymock.c index 778b0561ac..03c818d8a3 100644 --- a/tests/qemusecuritymock.c +++ b/tests/qemusecuritymock.c @@ -137,27 +137,21 @@ virFileGetXAttrQuiet(const char *path, const char *name, char **value) { - int ret =3D -1; - g_autofree char *key =3D NULL; + g_autofree char *key =3D get_key(path, name); char *val; + VIR_LOCK_GUARD lock =3D virLockGuardLock(&m); =20 - key =3D get_key(path, name); - - virMutexLock(&m); init_syms(); init_hash(); =20 if (!(val =3D virHashLookup(xattr_paths, key))) { errno =3D ENODATA; - goto cleanup; + return -1; } =20 *value =3D g_strdup(val); =20 - ret =3D 0; - cleanup: - virMutexUnlock(&m); - return ret; + return 0; } =20 =20 @@ -193,25 +187,18 @@ int virFileSetXAttr(const char *path, const char *name, const char *value) { - int ret =3D -1; - g_autofree char *key =3D NULL; - g_autofree char *val =3D NULL; - - key =3D get_key(path, name); - val =3D g_strdup(value); + g_autofree char *key =3D get_key(path, name); + g_autofree char *val =3D g_strdup(value); + VIR_LOCK_GUARD lock =3D virLockGuardLock(&m); =20 - virMutexLock(&m); init_syms(); init_hash(); =20 if (virHashUpdateEntry(xattr_paths, key, val) < 0) - goto cleanup; + return -1; val =3D NULL; =20 - ret =3D 0; - cleanup: - virMutexUnlock(&m); - return ret; + return 0; } =20 =20 @@ -219,18 +206,15 @@ int virFileRemoveXAttr(const char *path, const char *name) { int ret =3D -1; - g_autofree char *key =3D NULL; - - key =3D get_key(path, name); + g_autofree char *key =3D get_key(path, name); + VIR_LOCK_GUARD lock =3D virLockGuardLock(&m); =20 - virMutexLock(&m); init_syms(); init_hash(); =20 if ((ret =3D virHashRemoveEntry(xattr_paths, key)) < 0) errno =3D ENODATA; =20 - virMutexUnlock(&m); return ret; } =20 @@ -270,8 +254,8 @@ mock_chown(const char *path, uid_t uid, gid_t gid) { - uint32_t *val =3D NULL; - int ret =3D -1; + g_autofree uint32_t *val =3D NULL; + VIR_LOCK_GUARD lock =3D virLockGuardLock(&m); =20 if (gid >> 16 || uid >> 16) { fprintf(stderr, "Attempt to set too high UID or GID: %llu %llu", @@ -283,18 +267,13 @@ mock_chown(const char *path, =20 *val =3D (gid << 16) + uid; =20 - virMutexLock(&m); init_hash(); =20 if (virHashUpdateEntry(chown_paths, path, val) < 0) - goto cleanup; - val =3D NULL; + return -1; =20 - ret =3D 0; - cleanup: - virMutexUnlock(&m); - VIR_FREE(val); - return ret; + val =3D NULL; + return 0; } =20 =20 @@ -460,13 +439,12 @@ printXATTR(void *payload, */ int checkPaths(GHashTable *paths) { - int ret =3D -1; checkOwnerData data =3D { .paths =3D paths, .chown_fail =3D false, .se= linux_fail =3D false }; bool xattr_fail =3D false; GHashTableIter htitr; void *key; + VIR_LOCK_GUARD lock =3D virLockGuardLock(&m); =20 - virMutexLock(&m); init_hash(); =20 g_hash_table_iter_init(&htitr, paths); @@ -474,38 +452,35 @@ int checkPaths(GHashTable *paths) while (g_hash_table_iter_next(&htitr, &key, NULL)) { if (!virHashLookup(chown_paths, key)) { fprintf(stderr, "Unexpected path restored: %s\n", (const char = *) key); - goto cleanup; + return -1; } } =20 if (virHashForEach(selinux_paths, checkSELinux, &data) < 0) - goto cleanup; + return -1; =20 if (virHashForEach(chown_paths, checkOwner, &data) < 0) - goto cleanup; + return -1; =20 if (virHashForEach(xattr_paths, printXATTR, &xattr_fail) < 0) - goto cleanup; + return -1; =20 if (data.chown_fail || data.selinux_fail || xattr_fail) - goto cleanup; + return -1; =20 - ret =3D 0; - cleanup: - virMutexUnlock(&m); - return ret; + return 0; } =20 =20 void freePaths(void) { - virMutexLock(&m); + VIR_LOCK_GUARD lock =3D virLockGuardLock(&m); + init_hash(); =20 g_clear_pointer(&selinux_paths, g_hash_table_unref); g_clear_pointer(&chown_paths, g_hash_table_unref); g_clear_pointer(&xattr_paths, g_hash_table_unref); - virMutexUnlock(&m); } =20 =20 @@ -578,19 +553,15 @@ mock_setfilecon_raw(const char *path, const char *context) { g_autofree char *val =3D g_strdup(context); - int ret =3D -1; + VIR_LOCK_GUARD lock =3D virLockGuardLock(&m); =20 - virMutexLock(&m); init_hash(); =20 if (virHashUpdateEntry(selinux_paths, path, val) < 0) - goto cleanup; + return -1; val =3D NULL; =20 - ret =3D 0; - cleanup: - virMutexUnlock(&m); - return ret; + return 0; } =20 =20 @@ -599,8 +570,8 @@ mock_getfilecon_raw(const char *path, char **context) { const char *val; + VIR_LOCK_GUARD lock =3D virLockGuardLock(&m); =20 - virMutexLock(&m); init_hash(); =20 val =3D virHashLookup(selinux_paths, path); @@ -608,7 +579,6 @@ mock_getfilecon_raw(const char *path, val =3D DEFAULT_SELINUX_LABEL; =20 *context =3D g_strdup(val); - virMutexUnlock(&m); return 0; } =20 --=20 2.31.1