From nobody Mon Dec 15 23:14:16 2025 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 Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1528379680088671.6114346198449; Thu, 7 Jun 2018 06:54:40 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id AA2C2804E5; Thu, 7 Jun 2018 13:54:37 +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 26D02106223E; Thu, 7 Jun 2018 13:54:37 +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 9DA6F4CA82; Thu, 7 Jun 2018 13:54:36 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w57DsY2V000416 for ; Thu, 7 Jun 2018 09:54:34 -0400 Received: by smtp.corp.redhat.com (Postfix) id 0F2F52035723; Thu, 7 Jun 2018 13:54:34 +0000 (UTC) Received: from caroline (unknown [10.43.2.67]) by smtp.corp.redhat.com (Postfix) with ESMTPS id DFF1D2035721 for ; Thu, 7 Jun 2018 13:54:33 +0000 (UTC) Received: from caroline.brq.redhat.com (caroline.usersys.redhat.com [127.0.0.1]) by caroline (Postfix) with ESMTP id E44D412006B for ; Thu, 7 Jun 2018 15:54:32 +0200 (CEST) From: Martin Kletzander To: libvir-list@redhat.com Date: Thu, 7 Jun 2018 15:54:24 +0200 Message-Id: <83059507f479ec316a2006b8cfb460d1f4628a84.1528377367.git.mkletzan@redhat.com> In-Reply-To: References: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.78 on 10.11.54.6 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 03/10] util: Introduce virFileFlock 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: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Thu, 07 Jun 2018 13:54:39 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" We already have virFileLock(), but we are now using flock() in the code as well (due to requirements for mutual exclusion between libvirt and other programs using flock() as well), so let's have a function for that as well = so we don't need to have stubs for unsupported platforms in other files. Signed-off-by: Martin Kletzander Reviewed-by: J=EF=BF=BDn Tomko --- src/libvirt_private.syms | 1 + src/util/virfile.c | 40 ++++++++++++++++++++++++++++++++++++++++ src/util/virfile.h | 2 ++ 3 files changed, 43 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index b4ab1f36290c..3a9680b3cd2b 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1783,6 +1783,7 @@ virFileFindHugeTLBFS; virFileFindMountPoint; virFileFindResource; virFileFindResourceFull; +virFileFlock; virFileFreeACLs; virFileGetACLs; virFileGetHugepageSize; diff --git a/src/util/virfile.c b/src/util/virfile.c index 12b41a64e030..9296ccbe2a4f 100644 --- a/src/util/virfile.c +++ b/src/util/virfile.c @@ -51,6 +51,7 @@ #if HAVE_SYS_ACL_H # include #endif +#include =20 #ifdef __linux__ # if HAVE_LINUX_MAGIC_H @@ -362,6 +363,7 @@ virFileWrapperFdFree(virFileWrapperFdPtr wfd) =20 =20 #ifndef WIN32 + /** * virFileLock: * @fd: file descriptor to acquire the lock on @@ -430,7 +432,32 @@ int virFileUnlock(int fd, off_t start, off_t len) =20 return 0; } + + +/** + * 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` + * + * 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`. + * + * Returns 0 on success, -1 otherwise (with errno set) + */ +int virFileFlock(int fd, bool lock, bool shared) +{ + if (lock) + return flock(fd, shared ? LOCK_SH : LOCK_EX); + + return flock(fd, LOCK_UN); +} + #else + int virFileLock(int fd ATTRIBUTE_UNUSED, bool shared ATTRIBUTE_UNUSED, off_t start ATTRIBUTE_UNUSED, @@ -439,14 +466,27 @@ int virFileLock(int fd ATTRIBUTE_UNUSED, { return -ENOSYS; } + + int virFileUnlock(int fd ATTRIBUTE_UNUSED, off_t start ATTRIBUTE_UNUSED, off_t len ATTRIBUTE_UNUSED) { return -ENOSYS; } + + +int virFileFlock(int fd ATTRIBUTE_UNUSED, + bool lock ATTRIBUTE_UNUSED, + bool shared ATTRIBUTE_UNUSED) +{ + errno =3D ENOSYS; + return -1; +} + #endif =20 + int virFileRewrite(const char *path, mode_t mode, diff --git a/src/util/virfile.h b/src/util/virfile.h index 59c14b97a6ac..6f1e802fde78 100644 --- a/src/util/virfile.h +++ b/src/util/virfile.h @@ -104,6 +104,8 @@ void virFileWrapperFdFree(virFileWrapperFdPtr dfd); int virFileLock(int fd, bool shared, off_t start, off_t len, bool waitForL= ock); int virFileUnlock(int fd, off_t start, off_t len); =20 +int virFileFlock(int fd, bool lock, bool shared); + typedef int (*virFileRewriteFunc)(int fd, const void *opaque); int virFileRewrite(const char *path, mode_t mode, --=20 2.17.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list