[libvirt] [PATCH 03/10] util: Introduce virFileFlock

Martin Kletzander posted 10 patches 7 years, 6 months ago
[libvirt] [PATCH 03/10] util: Introduce virFileFlock
Posted by Martin Kletzander 7 years, 6 months ago
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 <mkletzan@redhat.com>
---
 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 <sys/acl.h>
 #endif
+#include <sys/file.h>
 
 #ifdef __linux__
 # if HAVE_LINUX_MAGIC_H
@@ -362,6 +363,7 @@ virFileWrapperFdFree(virFileWrapperFdPtr wfd)
 
 
 #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)
 
     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 == 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
+
 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 = ENOSYS;
+    return -1;
+}
+
 #endif
 
+
 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 waitForLock);
 int virFileUnlock(int fd, off_t start, off_t len);
 
+int virFileFlock(int fd, bool lock, bool shared);
+
 typedef int (*virFileRewriteFunc)(int fd, const void *opaque);
 int virFileRewrite(const char *path,
                    mode_t mode,
-- 
2.17.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH 03/10] util: Introduce virFileFlock
Posted by Daniel P. Berrangé 7 years, 6 months ago
On Thu, Jun 07, 2018 at 03:54:24PM +0200, Martin Kletzander wrote:
> 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.

Sigh, what program is using the flock interface ?

Regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH 03/10] util: Introduce virFileFlock
Posted by Martin Kletzander 7 years, 6 months ago
On Thu, Jun 07, 2018 at 02:58:12PM +0100, Daniel P. Berrangé wrote:
>On Thu, Jun 07, 2018 at 03:54:24PM +0200, Martin Kletzander wrote:
>> 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.
>
>Sigh, what program is using the flock interface ?
>

It is required (in the docs [1]) for the /sys/fs/resctrl so that all possible
programs that will access that directory are mutually excluded.  The only way
this will work is when all of them behave according to the documentation.

[1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/x86/intel_rdt_ui.txt

>Regards,
>Daniel
>-- 
>|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
>|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
>|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH 03/10] util: Introduce virFileFlock
Posted by Ján Tomko 7 years, 6 months ago
On Thu, Jun 07, 2018 at 03:54:24PM +0200, Martin Kletzander wrote:
>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 <mkletzan@redhat.com>
>---
> src/libvirt_private.syms |  1 +
> src/util/virfile.c       | 40 ++++++++++++++++++++++++++++++++++++++++
> src/util/virfile.h       |  2 ++
> 3 files changed, 43 insertions(+)
>

Reviewed-by: Ján Tomko <jtomko@redhat.com>

Jano
--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list