[libvirt] [PATCH v3 5/6] util: Introduce virFileWaitForAccess

Erik Skultety posted 6 patches 7 years, 8 months ago
There is a newer version of this series
[libvirt] [PATCH v3 5/6] util: Introduce virFileWaitForAccess
Posted by Erik Skultety 7 years, 8 months ago
Since we have a number of places where we workaround timing issues with
devices, attributes (files in general) not being available at the time
of processing them by calling usleep in a loop for a fixed number of
tries, we could as well have a utility function that would do that.
Therefore we won't have to duplicate this ugly workaround even more.

Signed-off-by: Erik Skultety <eskultet@redhat.com>
---
 src/libvirt_private.syms |  1 +
 src/util/virfile.c       | 29 +++++++++++++++++++++++++++++
 src/util/virfile.h       |  2 ++
 3 files changed, 32 insertions(+)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 2149b11b7..b9fb54a79 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1724,6 +1724,7 @@ virFileStripSuffix;
 virFileTouch;
 virFileUnlock;
 virFileUpdatePerm;
+virFileWaitForAccess;
 virFileWrapperFdClose;
 virFileWrapperFdFree;
 virFileWrapperFdNew;
diff --git a/src/util/virfile.c b/src/util/virfile.c
index 2f28e83f4..15b46bcdd 100644
--- a/src/util/virfile.c
+++ b/src/util/virfile.c
@@ -4166,3 +4166,32 @@ virFileReadValueString(char **value, const char *format, ...)
     VIR_FREE(str);
     return ret;
 }
+
+
+/**
+ * virFileWaitForAccess:
+ * @path: absolute path to a sysfs attribute (can be a symlink)
+ * @ms: how long to wait (in milliseconds)
+ * @tries: how many times should we try to wait for @path to become accessible
+ *
+ * Checks the existence of @path. In case the file defined by @path
+ * doesn't exist, we wait for it to appear in @ms milliseconds (for up to
+ * @tries attempts).
+ *
+ * Returns 0 on success, -1 on error, setting errno appropriately.
+ */
+int
+virFileWaitForAccess(const char *path, size_t ms, size_t tries)
+{
+    errno = 0;
+
+    /* wait for @path to be accessible in @ms milliseconds, up to @tries */
+    while (tries-- > 0 && !virFileExists(path)) {
+        if (tries == 0 || errno != ENOENT)
+            return -1;
+
+        usleep(ms * 1000);
+    }
+
+    return 0;
+}
diff --git a/src/util/virfile.h b/src/util/virfile.h
index 57ceb8072..42630ebb5 100644
--- a/src/util/virfile.h
+++ b/src/util/virfile.h
@@ -347,6 +347,8 @@ int virFileReadValueScaledInt(unsigned long long *value, const char *format, ...
 int virFileReadValueString(char **value, const char *format, ...)
  ATTRIBUTE_FMT_PRINTF(2, 3);
 
+int virFileWaitForAccess(const char *path, size_t ms, size_t tries);
+
 
 int virFileInData(int fd,
                   int *inData,
-- 
2.13.3

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH v3 5/6] util: Introduce virFileWaitForAccess
Posted by John Ferlan 7 years, 8 months ago

On 08/24/2017 07:23 AM, Erik Skultety wrote:
> Since we have a number of places where we workaround timing issues with
> devices, attributes (files in general) not being available at the time
> of processing them by calling usleep in a loop for a fixed number of
> tries, we could as well have a utility function that would do that.
> Therefore we won't have to duplicate this ugly workaround even more.
> 
> Signed-off-by: Erik Skultety <eskultet@redhat.com>
> ---
>  src/libvirt_private.syms |  1 +
>  src/util/virfile.c       | 29 +++++++++++++++++++++++++++++
>  src/util/virfile.h       |  2 ++
>  3 files changed, 32 insertions(+)
> 
> diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
> index 2149b11b7..b9fb54a79 100644
> --- a/src/libvirt_private.syms
> +++ b/src/libvirt_private.syms
> @@ -1724,6 +1724,7 @@ virFileStripSuffix;
>  virFileTouch;
>  virFileUnlock;
>  virFileUpdatePerm;
> +virFileWaitForAccess;
>  virFileWrapperFdClose;
>  virFileWrapperFdFree;
>  virFileWrapperFdNew;
> diff --git a/src/util/virfile.c b/src/util/virfile.c
> index 2f28e83f4..15b46bcdd 100644
> --- a/src/util/virfile.c
> +++ b/src/util/virfile.c
> @@ -4166,3 +4166,32 @@ virFileReadValueString(char **value, const char *format, ...)
>      VIR_FREE(str);
>      return ret;
>  }
> +
> +
> +/**
> + * virFileWaitForAccess:
> + * @path: absolute path to a sysfs attribute (can be a symlink)
> + * @ms: how long to wait (in milliseconds)
> + * @tries: how many times should we try to wait for @path to become accessible
> + *
> + * Checks the existence of @path. In case the file defined by @path
> + * doesn't exist, we wait for it to appear in @ms milliseconds (for up to
> + * @tries attempts).
> + *
> + * Returns 0 on success, -1 on error, setting errno appropriately.
> + */
> +int
> +virFileWaitForAccess(const char *path, size_t ms, size_t tries)

Multiple lines per argument

NIT: WaitForExists()

John

> +{
> +    errno = 0;
> +
> +    /* wait for @path to be accessible in @ms milliseconds, up to @tries */
> +    while (tries-- > 0 && !virFileExists(path)) {
> +        if (tries == 0 || errno != ENOENT)
> +            return -1;
> +
> +        usleep(ms * 1000);
> +    }
> +
> +    return 0;
> +}
> diff --git a/src/util/virfile.h b/src/util/virfile.h
> index 57ceb8072..42630ebb5 100644
> --- a/src/util/virfile.h
> +++ b/src/util/virfile.h
> @@ -347,6 +347,8 @@ int virFileReadValueScaledInt(unsigned long long *value, const char *format, ...
>  int virFileReadValueString(char **value, const char *format, ...)
>   ATTRIBUTE_FMT_PRINTF(2, 3);
>  
> +int virFileWaitForAccess(const char *path, size_t ms, size_t tries);
> +
>  
>  int virFileInData(int fd,
>                    int *inData,
> 

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