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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



The function is used to retrieve the PID of the process holding an
exclusive lock on the file.

Signed-off-by: Vasiliy Ulyanov <vulyanov@xxxxxxx>
---
 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





[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux