[PATCH v3 2/3] util: Get rid of virFileFlock()

Martin Kletzander mkletzan at redhat.com
Wed Jul 29 11:43:13 UTC 2020


It was created to get rid of conditional compilation in the resctrl code and
make it usable anywhere else.  However this is not something that is going to be
used in other places because it is not portable and resctrl is just very
specific in this regard.  And there is no reason why there could not be a
preprocessor conditional in the resctrl code.  Also the interface of
virFileFlock() was very ambiguous which lead to some issues.

Signed-off-by: Martin Kletzander <mkletzan at redhat.com>
---
 src/libvirt_private.syms |  1 -
 src/util/virfile.c       | 31 +------------------------------
 src/util/virfile.h       |  2 --
 src/util/virresctrl.c    | 17 +++++++++++++++--
 4 files changed, 16 insertions(+), 35 deletions(-)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index eea31a736def..fbbe07becbcf 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -2074,7 +2074,6 @@ virFileFindHugeTLBFS;
 virFileFindMountPoint;
 virFileFindResource;
 virFileFindResourceFull;
-virFileFlock;
 virFileFreeACLs;
 virFileGetACLs;
 virFileGetDefaultHugepage;
diff --git a/src/util/virfile.c b/src/util/virfile.c
index af150421e726..b1ca3adcff04 100644
--- a/src/util/virfile.c
+++ b/src/util/virfile.c
@@ -463,30 +463,9 @@ 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 == false`
- *
- * This is just a simple wrapper around flock(2) that errors out on unsupported
- * platforms.
- *
- * The lock will be released when @fd is closed or this function is called with
- * `@lock == 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 /* WIN32 */
 
+
 int virFileLock(int fd G_GNUC_UNUSED,
                 bool shared G_GNUC_UNUSED,
                 off_t start G_GNUC_UNUSED,
@@ -505,14 +484,6 @@ int virFileUnlock(int fd G_GNUC_UNUSED,
 }
 
 
-int virFileFlock(int fd G_GNUC_UNUSED,
-                 bool lock G_GNUC_UNUSED,
-                 bool shared G_GNUC_UNUSED)
-{
-    errno = ENOSYS;
-    return -1;
-}
-
 #endif /* WIN32 */
 
 
diff --git a/src/util/virfile.h b/src/util/virfile.h
index cb0e586a7d11..2eec89598f6b 100644
--- a/src/util/virfile.h
+++ b/src/util/virfile.h
@@ -118,8 +118,6 @@ 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;
 
-int virFileFlock(int fd, bool lock, bool shared);
-
 typedef int (*virFileRewriteFunc)(int fd, const void *opaque);
 int virFileRewrite(const char *path,
                    mode_t mode,
diff --git a/src/util/virresctrl.c b/src/util/virresctrl.c
index 9b78a6cb159b..2129630a190f 100644
--- a/src/util/virresctrl.c
+++ b/src/util/virresctrl.c
@@ -456,6 +456,8 @@ VIR_ONCE_GLOBAL_INIT(virResctrl);
 static int
 virResctrlLockWrite(void)
 {
+#ifndef WIN32
+
     int fd = open(SYSFS_RESCTRL_PATH, O_RDONLY | O_CLOEXEC);
 
     if (fd < 0) {
@@ -463,13 +465,20 @@ virResctrlLockWrite(void)
         return -1;
     }
 
-    if (virFileFlock(fd, true, false) < 0) {
+    if (flock(fd, LOCK_EX) < 0) {
         virReportSystemError(errno, "%s", _("Cannot lock resctrl"));
         VIR_FORCE_CLOSE(fd);
         return -1;
     }
 
     return fd;
+
+#else /* WIN32 */
+
+    virReportSystemError(ENOSYS, "%s", _("Cannot lock resctrl"));
+    return -1;
+
+#endif
 }
 
 
@@ -484,10 +493,14 @@ virResctrlUnlock(int fd)
     if (VIR_CLOSE(fd) < 0) {
         virReportSystemError(errno, "%s", _("Cannot close resctrl"));
 
+#ifndef WIN32
+
         /* Trying to save the already broken */
-        if (virFileFlock(fd, false, false) < 0)
+        if (flock(fd, LOCK_UN) < 0)
             virReportSystemError(errno, "%s", _("Cannot unlock resctrl"));
 
+#endif
+
         return -1;
     }
 
-- 
2.28.0




More information about the libvir-list mailing list