[PATCH v2 1/4] virfile: Add virFileGetLockOwner function

Vasiliy Ulyanov posted 4 patches 4 years ago
There is a newer version of this series
[PATCH v2 1/4] virfile: Add virFileGetLockOwner function
Posted by Vasiliy Ulyanov 4 years ago
The function is used to retrieve the PID of the process holding an
exclusive lock on the file.

Signed-off-by: Vasiliy Ulyanov <vulyanov@suse.de>
---
 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)
 }
 
 
+/**
+ * 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 @start)
+ * @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 = {
+        .l_type = F_WRLCK,
+        .l_whence = SEEK_SET,
+        .l_start = start,
+        .l_len = len,
+    };
+
+    *pid = -1;
+
+    if (fcntl(fd, F_GETLK, &fl) < 0)
+        return -errno;
+
+    if (fl.l_type != F_UNLCK)
+        *pid = fl.l_pid;
+
+    return 0;
+}
+
+
 #else /* WIN32 */
 
 
@@ -480,6 +517,14 @@ int virFileUnlock(int fd G_GNUC_UNUSED,
     return -ENOSYS;
 }
 
+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;
+}
+
 
 #endif /* WIN32 */
 
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;
 
 typedef int (*virFileRewriteFunc)(int fd, const void *opaque);
 int virFileRewrite(const char *path,
-- 
2.34.1


Re: [PATCH v2 1/4] virfile: Add virFileGetLockOwner function
Posted by Michal Prívozník 4 years ago
On 1/13/22 13:42, Vasiliy Ulyanov wrote:
> The function is used to retrieve the PID of the process holding an
> exclusive lock on the file.
> 
> Signed-off-by: Vasiliy Ulyanov <vulyanov@suse.de>
> ---
>  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)
>  }
>  
>  
> +/**
> + * 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 @start)
> + * @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.

Here I'd mention that the lock has to be acquired via virFileLock(),
because there are plenty of file locks (at least on Linux) that are not
compatible with each other.

But I don't think we will need this function after all.

Michal