From nobody Sat Feb 7 08:23:14 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; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=fail(p=none dis=none) header.from=suse.de 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 1642077956714363.6605352817818; Thu, 13 Jan 2022 04:45:56 -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-660-n4gebiqhPxy1swL9RhWqHw-1; Thu, 13 Jan 2022 07:45:52 -0500 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id D234F93926; Thu, 13 Jan 2022 12:45:47 +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 B2F347CD6C; Thu, 13 Jan 2022 12:45:47 +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 809E11809CB8; Thu, 13 Jan 2022 12:45:47 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 20DCgx28009467 for ; Thu, 13 Jan 2022 07:43:00 -0500 Received: by smtp.corp.redhat.com (Postfix) id DCCB81120AC3; Thu, 13 Jan 2022 12:42:59 +0000 (UTC) Received: from mimecast-mx02.redhat.com (mimecast08.extmail.prod.ext.rdu2.redhat.com [10.11.55.24]) by smtp.corp.redhat.com (Postfix) with ESMTPS id D895E1120ABD for ; Thu, 13 Jan 2022 12:42:57 +0000 (UTC) Received: from us-smtp-1.mimecast.com (us-smtp-delivery-1.mimecast.com [205.139.110.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id DF4CC3800689 for ; Thu, 13 Jan 2022 12:42:56 +0000 (UTC) Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-292-kjyexceON6OXCBU3cE43ig-1; Thu, 13 Jan 2022 07:42:55 -0500 Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id E25E41F3BA; Thu, 13 Jan 2022 12:42:53 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id B927713BF7; Thu, 13 Jan 2022 12:42:53 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id cNBrK00e4GG0SQAAMHmgww (envelope-from ); Thu, 13 Jan 2022 12:42:53 +0000 X-MC-Unique: n4gebiqhPxy1swL9RhWqHw-1 X-MC-Unique: kjyexceON6OXCBU3cE43ig-1 From: Vasiliy Ulyanov To: libvir-list@redhat.com Subject: [PATCH v2 1/4] virfile: Add virFileGetLockOwner function Date: Thu, 13 Jan 2022 13:42:35 +0100 Message-Id: <20220113124238.2279-2-vulyanov@suse.de> In-Reply-To: <20220113124238.2279-1-vulyanov@suse.de> References: <20220113124238.2279-1-vulyanov@suse.de> MIME-Version: 1.0 X-Mimecast-Impersonation-Protect: Policy=CLT - Impersonation Protection Definition; Similar Internal Domain=false; Similar Monitored External Domain=false; Custom External Domain=false; Mimecast External Domain=false; Newly Observed Domain=false; Internal User Name=false; Custom Display Name List=false; Reply-to Address Mismatch=false; Targeted Threat Dictionary=false; Mimecast Threat Dictionary=false; Custom Threat Dictionary=false X-Scanned-By: MIMEDefang 2.78 on 10.11.54.3 X-MIME-Autoconverted: from quoted-printable to 8bit by lists01.pubmisc.prod.ext.phx2.redhat.com id 20DCgx28009467 X-loop: libvir-list@redhat.com Cc: Vasiliy Ulyanov 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.12 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-ZM-MESSAGEID: 1642077959051100001 Content-Type: text/plain; charset="utf-8" The function is used to retrieve the PID of the process holding an exclusive lock on the file. Signed-off-by: Vasiliy Ulyanov --- src/libvirt_private.syms | 1 + src/util/virfile.c | 45 ++++++++++++++++++++++++++++++++++++++++ src/util/virfile.h | 2 ++ 3 files changed, 48 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 5b76e66e61..214f375a91 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2224,6 +2224,7 @@ virFileFreeACLs; virFileGetACLs; virFileGetDefaultHugepage; virFileGetHugepageSize; +virFileGetLockOwner; virFileGetMountReverseSubtree; virFileGetMountSubtree; virFileGetXAttr; diff --git a/src/util/virfile.c b/src/util/virfile.c index d6faf7e3d2..b9149fb0d7 100644 --- a/src/util/virfile.c +++ b/src/util/virfile.c @@ -460,6 +460,43 @@ int virFileUnlock(int fd, off_t start, off_t len) } =20 =20 +/** + * virFileGetLockOwner: + * @fd: file descriptor to get the lock from + * @start: byte offset for the lock + * @len: length of the lock (0 to specify entire remaining file from @star= t) + * @pid: variable to return the PID of the process owning the lock + * + * Attempt to retrieve the PID of the process holding an exclusive lock + * on the file @fd. + * + * Returns 0 on success, or -errno on error. If the file is not locked @pid + * will be set ot -1. + */ +int virFileGetLockOwner(int fd, + off_t start, + off_t len, + pid_t *pid) +{ + struct flock fl =3D { + .l_type =3D F_WRLCK, + .l_whence =3D SEEK_SET, + .l_start =3D start, + .l_len =3D len, + }; + + *pid =3D -1; + + if (fcntl(fd, F_GETLK, &fl) < 0) + return -errno; + + if (fl.l_type !=3D F_UNLCK) + *pid =3D fl.l_pid; + + return 0; +} + + #else /* WIN32 */ =20 =20 @@ -480,6 +517,14 @@ int virFileUnlock(int fd G_GNUC_UNUSED, return -ENOSYS; } =20 +int virFileGetLockOwner(int fd G_GNUC_UNUSED, + off_t start G_GNUC_UNUSED, + off_t len G_GNUC_UNUSED, + pid_t *pid G_GNUC_UNUSED) +{ + return -ENOSYS; +} + =20 #endif /* WIN32 */ =20 diff --git a/src/util/virfile.h b/src/util/virfile.h index 967c9a9b4f..0f4aa6e441 100644 --- a/src/util/virfile.h +++ b/src/util/virfile.h @@ -122,6 +122,8 @@ int virFileLock(int fd, bool shared, off_t start, off_t= len, bool waitForLock) G_GNUC_NO_INLINE; int virFileUnlock(int fd, off_t start, off_t len) G_GNUC_NO_INLINE; +int virFileGetLockOwner(int fd, off_t start, off_t len, pid_t *pid) + G_GNUC_NO_INLINE; =20 typedef int (*virFileRewriteFunc)(int fd, const void *opaque); int virFileRewrite(const char *path, --=20 2.34.1