From nobody Sun Feb 8 19:55:24 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 205.139.110.120 as permitted sender) client-ip=205.139.110.120; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-1.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 205.139.110.120 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=1594410351; cv=none; d=zohomail.com; s=zohoarc; b=Wx97K+Sd3APsKfKbKmf4JfkXJeW4orjUk7g7LZ4pC8j8tVgaFl8fNrSkdLBkFI133Yb3dZ5FBWerh+Oacq3tftcFApc5jV1fA/V1WldqT4NIwZqV7Vjpm7A2cAptnjXpeUdWhne9sNTrKUvVX4+IOzYpw3HX8JstNeYGQbf8KHg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1594410351; 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=7GAlOwgS+U3pZbKK1rRRKsBBcTaRNq20ejCethcrptw=; b=Fl5h9oLAFTD2++GfRkBiwB0ypT92nEFyxLjPZ226UE/jMDvXZFFcmAIBTD/KetMGPpJKP05Ro80xyr3lZ4O67A4Ek5X+o0fEpS9Y9YlgNGIlEKMuHUl9JRn8S25j9Xgq6OcH/oaYWBUHYoeOg58I3wlhwmkmRmdV0qqgYLqZ85I= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 205.139.110.120 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-1.mimecast.com (us-smtp-delivery-1.mimecast.com [205.139.110.120]) by mx.zohomail.com with SMTPS id 1594410351719696.6500040494138; Fri, 10 Jul 2020 12:45:51 -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-434-QrwO_baVNcW3_maRZa4sqg-1; Fri, 10 Jul 2020 15:45:48 -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 18C82800EB6; Fri, 10 Jul 2020 19:45:43 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id A63537EF8F; Fri, 10 Jul 2020 19:45:42 +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 D67028436D; Fri, 10 Jul 2020 19:45:38 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 06AJjbrG009072 for ; Fri, 10 Jul 2020 15:45:37 -0400 Received: by smtp.corp.redhat.com (Postfix) id 9834C5F7D8; Fri, 10 Jul 2020 19:45:37 +0000 (UTC) Received: from carol.localdomain (unknown [10.40.195.78]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 9BC3A74F56; Fri, 10 Jul 2020 19:45:34 +0000 (UTC) Received: from carol.redhat.com (carol.k8r.cz [127.0.0.1]) by carol.localdomain (Postfix) with ESMTP id 30D22C201D9; Fri, 10 Jul 2020 21:45:33 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1594410350; 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=7GAlOwgS+U3pZbKK1rRRKsBBcTaRNq20ejCethcrptw=; b=H6Kky3m4YXnvSxZgSWHZgKdfxHUn1/zgHUGwYc3U50OWdswUIqe2ZYmD1YHov5RkWOsGdf LlCo/SlAgWMRZ6P0z3CyeN7cAPafLtM14u4sKClon3+OGr6YisGxwSKyenXtRJh/R6EckX WXHb/J7zzo9kSIJ6Flhszk8YEAlOISI= X-MC-Unique: QrwO_baVNcW3_maRZa4sqg-1 From: Martin Kletzander To: libvir-list@redhat.com Subject: [PATCH v2 1/2] util: Rework virFileFlock() to be unambiguous Date: Fri, 10 Jul 2020 21:45:25 +0200 Message-Id: <1f414c6bf5ca8e09bef0e4911590366f717e91c1.1594410214.git.mkletzan@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Clacks-Overhead: GNU Terry Pratchett X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-loop: libvir-list@redhat.com Cc: Andrea Bolognani 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. Signed-off-by: Martin Kletzander Reviewed-by: Andrea Bolognani --- 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..b685b8bb6f7b 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_SHARED) < 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