From nobody Tue May 7 01:09:05 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 205.139.110.61 as permitted sender) client-ip=205.139.110.61; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-1.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 205.139.110.61 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=1594387638; cv=none; d=zohomail.com; s=zohoarc; b=I6tEWrZjw4U4ka8ccd8+wcfK9uO7dbRbS7MWrTUHZeaOGNumL67lXXGJVNQxYucIDhIeMHuSNUG52iXh6zPsZNWyrPso/ksTEad+XbZu2dgjnyKvqI6RLAamLdXmUbgFL8Lmse2uLNuQZlYXG3kx4Fdj2Y48CleL77BzuK0x+mA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1594387638; h=Content-Type:Content-Transfer-Encoding:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To; bh=prFww4u79GKt3qdqn/rqaoI4CqMtMXPvvW7qrZtIgpM=; b=FC5/iuROLHCUsYhzJsChMvpB0FTvfuHsnM4aeB2JKJho1z+K/Z1BYoyisFxWm3O28tcI7t17t2OFqnalcA0lKRJ+o+UMD9uH9vrdGdA/zalS1EZm8KdxEOMiWqiWMqf9izZrRoo6l9mxvjW4D92NXlF8eiOIwTe6OAX6XkRSMkY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 205.139.110.61 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-1.mimecast.com (us-smtp-2.mimecast.com [205.139.110.61]) by mx.zohomail.com with SMTPS id 1594387638391962.3942821371039; Fri, 10 Jul 2020 06:27:18 -0700 (PDT) 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-388-MaEjnszeO_6U50BFqJe_cA-1; Fri, 10 Jul 2020 09:27:15 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 6A07F800D5C; Fri, 10 Jul 2020 13:27:09 +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 9A4907EFA8; Fri, 10 Jul 2020 13:27:07 +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 3D3621809557; Fri, 10 Jul 2020 13:27:05 +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 06ADL6t7030278 for ; Fri, 10 Jul 2020 09:21:06 -0400 Received: by smtp.corp.redhat.com (Postfix) id D76DE5C1D6; Fri, 10 Jul 2020 13:21:06 +0000 (UTC) Received: from carol.localdomain (unknown [10.40.195.78]) by smtp.corp.redhat.com (Postfix) with ESMTPS id A98945C1D3 for ; Fri, 10 Jul 2020 13:21:03 +0000 (UTC) Received: from carol.redhat.com (carol.k8r.cz [127.0.0.1]) by carol.localdomain (Postfix) with ESMTP id 67597C201D8 for ; Fri, 10 Jul 2020 15:21:02 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1594387637; 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:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=prFww4u79GKt3qdqn/rqaoI4CqMtMXPvvW7qrZtIgpM=; b=MPSflJHNvmsEe0Q9NBgTY/hNpwGNbZ6ksICmSJw8IHsvLxI2viCMPLIXmA3X+VAc0bChL0 fdpAckSz5XQ/gTwmtO5O5ftu4a5ojmnjHvjtRmljI29IMpIoqeTn0fHkIUVFp/AfcqnWAr EFD3AYseKTHg/qRTIdjnHtkf4IM+CbM= X-MC-Unique: MaEjnszeO_6U50BFqJe_cA-1 From: Martin Kletzander To: libvir-list@redhat.com Subject: [PATCH] util: Rework virFileFlock() to be unambiguous Date: Fri, 10 Jul 2020 15:20:52 +0200 Message-Id: MIME-Version: 1.0 X-Clacks-Overhead: GNU Terry Pratchett X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 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.15 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" The boolean parameters for lock/unlock and read/write (shared/exclusive) ca= used a lot of confusion when reading the callers. The new approach is explicit = and unambiguous. While at it, also change the only caller so that it acquires an exclusive l= ock as it should've been all the time. This was caused because the function was ambiguous since it was created in commit 5a0a5f7fb5f5, and due to that it w= as misused in commit 657ddeff2313 and since then the lock being taken was shar= ed rather than exclusive. Signed-off-by: Martin Kletzander --- src/util/virfile.c | 27 ++++++++++++++++++--------- src/util/virfile.h | 9 ++++++++- src/util/virresctrl.c | 4 ++-- 3 files changed, 28 insertions(+), 12 deletions(-) diff --git a/src/util/virfile.c b/src/util/virfile.c index 213acdbcaa2b..554011eecb25 100644 --- a/src/util/virfile.c +++ b/src/util/virfile.c @@ -465,23 +465,33 @@ int virFileUnlock(int fd, off_t start, off_t len) /** * virFileFlock: * @fd: file descriptor to call flock on - * @lock: true for lock, false for unlock - * @shared: true if shared, false for exclusive, ignored if `@lock =3D=3D = false` + * @op: operation to perform * * This is just a simple wrapper around flock(2) that errors out on unsupp= orted * platforms. * * The lock will be released when @fd is closed or this function is called= with - * `@lock =3D=3D false`. + * `@op =3D=3D VIR_FILE_FLOCK_UNLOCK`. * * Returns 0 on success, -1 otherwise (with errno set) */ -int virFileFlock(int fd, bool lock, bool shared) +int virFileFlock(int fd, virFileFlockOperation op) { - if (lock) - return flock(fd, shared ? LOCK_SH : LOCK_EX); + int flock_op =3D -1; =20 - return flock(fd, LOCK_UN); + switch (op) { + case VIR_FILE_FLOCK_SHARED: + flock_op =3D LOCK_SH; + break; + case VIR_FILE_FLOCK_EXCLUSIVE: + flock_op =3D LOCK_EX; + break; + case VIR_FILE_FLOCK_UNLOCK: + flock_op =3D LOCK_UN; + break; + } + + return flock(fd, flock_op); } =20 #else /* WIN32 */ @@ -505,8 +515,7 @@ int virFileUnlock(int fd G_GNUC_UNUSED, =20 =20 int virFileFlock(int fd G_GNUC_UNUSED, - bool lock G_GNUC_UNUSED, - bool shared G_GNUC_UNUSED) + virFileFlockOperation op G_GNUC_UNUSED) { errno =3D ENOSYS; return -1; diff --git a/src/util/virfile.h b/src/util/virfile.h index 7a92364a5c9f..04428727fd3b 100644 --- a/src/util/virfile.h +++ b/src/util/virfile.h @@ -118,7 +118,14 @@ int virFileLock(int fd, bool shared, off_t start, off_= t len, bool waitForLock) int virFileUnlock(int fd, off_t start, off_t len) G_GNUC_NO_INLINE; =20 -int virFileFlock(int fd, bool lock, bool shared); + +typedef enum { + VIR_FILE_FLOCK_EXCLUSIVE, + VIR_FILE_FLOCK_SHARED, + VIR_FILE_FLOCK_UNLOCK, +} virFileFlockOperation; + +int virFileFlock(int fd, virFileFlockOperation op); =20 typedef int (*virFileRewriteFunc)(int fd, const void *opaque); int virFileRewrite(const char *path, diff --git a/src/util/virresctrl.c b/src/util/virresctrl.c index 3563fc560db5..784a8d43bb2f 100644 --- a/src/util/virresctrl.c +++ b/src/util/virresctrl.c @@ -463,7 +463,7 @@ virResctrlLockWrite(void) return -1; } =20 - if (virFileFlock(fd, true, true) < 0) { + if (virFileFlock(fd, VIR_FILE_FLOCK_EXCLUSIVE) < 0) { virReportSystemError(errno, "%s", _("Cannot lock resctrl")); VIR_FORCE_CLOSE(fd); return -1; @@ -485,7 +485,7 @@ virResctrlUnlock(int fd) virReportSystemError(errno, "%s", _("Cannot close resctrl")); =20 /* Trying to save the already broken */ - if (virFileFlock(fd, false, false) < 0) + if (virFileFlock(fd, VIR_FILE_FLOCK_UNLOCK) < 0) virReportSystemError(errno, "%s", _("Cannot unlock resctrl")); =20 return -1; --=20 2.27.0