[libvirt] [PATCH] virhostuptime: Add linux stub for musl

Michal Privoznik posted 1 patch 4 years, 5 months ago
Test syntax-check passed
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/libvirt tags/patchew/6b0838d1dfee8dc161d7fcfc411b0aded5faca0d.1571065041.git.mprivozn@redhat.com
src/util/virhostuptime.c | 64 ++++++++++++++++++++++++++++++++++++++--
1 file changed, 61 insertions(+), 3 deletions(-)
[libvirt] [PATCH] virhostuptime: Add linux stub for musl
Posted by Michal Privoznik 4 years, 5 months ago
When we want to know the boot timestamp of the host, we can call
virHostGetBootTime(). Under the hood, it uses getutxid() which is
defined by POSIX and properly check for in configure. However,
musl took a path where it declares the function but instead of
providing any useful implementation it returns NULL meaning "no
record found". If that's the case, use our second best option -
/proc/uptime and a bit of maths.

https://bugzilla.redhat.com/show_bug.cgi?id=1760885

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
---
 src/util/virhostuptime.c | 64 ++++++++++++++++++++++++++++++++++++++--
 1 file changed, 61 insertions(+), 3 deletions(-)

diff --git a/src/util/virhostuptime.c b/src/util/virhostuptime.c
index 62b781acd5..3189074d3d 100644
--- a/src/util/virhostuptime.c
+++ b/src/util/virhostuptime.c
@@ -25,16 +25,68 @@
 #endif
 
 #include "virhostuptime.h"
+#include "viralloc.h"
+#include "virfile.h"
+#include "virlog.h"
+#include "virstring.h"
+#include "virtime.h"
 #include "virthread.h"
 
+#define VIR_FROM_THIS VIR_FROM_NONE
+
+VIR_LOG_INIT("util.virhostuptime");
+
 static unsigned long long bootTime;
 static int bootTimeErrno;
 static virOnceControl virHostGetBootTimeOnce = VIR_ONCE_CONTROL_INITIALIZER;
 
-#ifdef HAVE_GETUTXID
+#if defined(__linux__)
+# define UPTIME_FILE  "/proc/uptime"
+static int
+virHostGetBootTimeProcfs(unsigned long long *btime)
+{
+    unsigned long long now;
+    double up;
+    g_autofree char *buf = NULL;
+    char *tmp;
+
+    if (virTimeMillisNow(&now) < 0)
+        return -errno;
+
+    /* 1KiB limit is more than enough. */
+    if (virFileReadAll(UPTIME_FILE, 1024, &buf) < 0)
+        return -errno;
+
+    /* buf contains two doubles now:
+     *   $uptime $idle_time
+     * We're interested only in the first one */
+    if (!(tmp = strchr(buf, ' '))) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("uptime file has unexpected format '%s'"),
+                       buf);
+        return -EINVAL;
+    }
+
+    *tmp = '\0';
+
+    if (virStrToDouble(buf, NULL, &up) < 0) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("Unable to parse sched info value '%s'"),
+                       buf);
+        return -EINVAL;
+    }
+
+    *btime = now / 1000 - up + 0.5;
+
+    return 0;
+}
+#endif /* defined(__linux__) */
+
+#if defined(HAVE_GETUTXID) || defined(__linux__)
 static void
 virHostGetBootTimeOnceInit(void)
 {
+# ifdef HAVE_GETUTXID
     struct utmpx id = {.ut_type = BOOT_TIME};
     struct utmpx *res = NULL;
 
@@ -45,16 +97,22 @@ virHostGetBootTimeOnceInit(void)
     }
 
     endutxent();
+# endif /* HAVE_GETUTXID */
+
+    if (bootTimeErrno == ENODATA ||
+        (bootTime == 0 && bootTimeErrno == 0)) {
+        bootTimeErrno = -virHostGetBootTimeProcfs(&bootTime);
+    }
 }
 
-#else /* !HAVE_GETUTXID */
+#else /* !defined(HAVE_GETUTXID) && !defined(__linux__) */
 
 static void
 virHostGetBootTimeOnceInit(void)
 {
     bootTimeErrno = ENOSYS;
 }
-#endif /* HAVE_GETUTXID */
+#endif /* !defined(HAVE_GETUTXID) && !defined(__linux__) */
 
 /**
  * virHostGetBootTime:
-- 
2.21.0

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH] virhostuptime: Add linux stub for musl
Posted by Cole Robinson 4 years, 4 months ago
On 10/14/19 10:57 AM, Michal Privoznik wrote:
> When we want to know the boot timestamp of the host, we can call
> virHostGetBootTime(). Under the hood, it uses getutxid() which is
> defined by POSIX and properly check for in configure. However,
> musl took a path where it declares the function but instead of
> providing any useful implementation it returns NULL meaning "no
> record found". If that's the case, use our second best option -
> /proc/uptime and a bit of maths.
> 
> https://bugzilla.redhat.com/show_bug.cgi?id=1760885
> 
> Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
> ---
>  src/util/virhostuptime.c | 64 ++++++++++++++++++++++++++++++++++++++--
>  1 file changed, 61 insertions(+), 3 deletions(-)
> 
> diff --git a/src/util/virhostuptime.c b/src/util/virhostuptime.c
> index 62b781acd5..3189074d3d 100644
> --- a/src/util/virhostuptime.c
> +++ b/src/util/virhostuptime.c
> @@ -25,16 +25,68 @@
>  #endif
>  
>  #include "virhostuptime.h"
> +#include "viralloc.h"
> +#include "virfile.h"
> +#include "virlog.h"
> +#include "virstring.h"
> +#include "virtime.h"
>  #include "virthread.h"
>  
> +#define VIR_FROM_THIS VIR_FROM_NONE
> +
> +VIR_LOG_INIT("util.virhostuptime");
> +
>  static unsigned long long bootTime;
>  static int bootTimeErrno;
>  static virOnceControl virHostGetBootTimeOnce = VIR_ONCE_CONTROL_INITIALIZER;
>  
> -#ifdef HAVE_GETUTXID
> +#if defined(__linux__)
> +# define UPTIME_FILE  "/proc/uptime"
> +static int
> +virHostGetBootTimeProcfs(unsigned long long *btime)
> +{
> +    unsigned long long now;
> +    double up;
> +    g_autofree char *buf = NULL;
> +    char *tmp;
> +
> +    if (virTimeMillisNow(&now) < 0)
> +        return -errno;
> +
> +    /* 1KiB limit is more than enough. */
> +    if (virFileReadAll(UPTIME_FILE, 1024, &buf) < 0)
> +        return -errno;
> +
> +    /* buf contains two doubles now:
> +     *   $uptime $idle_time
> +     * We're interested only in the first one */
> +    if (!(tmp = strchr(buf, ' '))) {
> +        virReportError(VIR_ERR_INTERNAL_ERROR,
> +                       _("uptime file has unexpected format '%s'"),
> +                       buf);
> +        return -EINVAL;
> +    }
> +
> +    *tmp = '\0';
> +
> +    if (virStrToDouble(buf, NULL, &up) < 0) {
> +        virReportError(VIR_ERR_INTERNAL_ERROR,
> +                       _("Unable to parse sched info value '%s'"),
> +                       buf);
> +        return -EINVAL;
> +    }
> +

error message here is a copy+paste mistake. syntax-check says you need
to extend POTFILES.in too. With those fixed:

Reviewed-by: Cole Robinson <crobinso@redhat.com>

- Cole

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