From nobody Sun Apr 28 17:19:53 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1571065478; cv=none; d=zoho.com; s=zohoarc; b=SJgSuHcSUoeVHyR3cTNRrLsi8KnBnT8BnVEweLUvq+KLDJeZcKN3PKVuXop8ppE3/VGiSNTpfzKu/5Gx/nE9rhvcfOTbvoCg9NgTQA954fa065t9xWNFKp6fvL+k2w871ioyAg/iUSSLlUCdKMnhtevFutXvxuMpypigb7YfsV8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1571065478; h=Content-Type:Content-Transfer-Encoding:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To; bh=L5bAl5s2/7XfpR6j0H978pZDNAwAvMvPSJvgMJzZX78=; b=Qr4dd+XzDuI1RoPBRZz+4BvRH8afEG68DwLk3DHOkm4szxwj4t0j5Wuwm0rHkJ6FqGlwFfyN2rMRCUMUaR7beJNY86SW/UaPTvTbVxzLeGJtygZbC2I2g7kYhGjMeupSZ3dvUKO0hp4AA5/mary/VkCs8Zz+Rr6AAnacT5xqJ4s= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1571065478964651.0123833562329; Mon, 14 Oct 2019 08:04:38 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 8B934C0546F1; Mon, 14 Oct 2019 15:04:36 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 2715C600CD; Mon, 14 Oct 2019 15:04:32 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 2E6D44E589; Mon, 14 Oct 2019 15:04:27 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id x9EEvbAC022981 for ; Mon, 14 Oct 2019 10:57:37 -0400 Received: by smtp.corp.redhat.com (Postfix) id CE1F260C05; Mon, 14 Oct 2019 14:57:37 +0000 (UTC) Received: from moe.brq.redhat.com (unknown [10.43.2.30]) by smtp.corp.redhat.com (Postfix) with ESMTP id 5603560C5D for ; Mon, 14 Oct 2019 14:57:34 +0000 (UTC) From: Michal Privoznik To: libvir-list@redhat.com Date: Mon, 14 Oct 2019 16:57:21 +0200 Message-Id: <6b0838d1dfee8dc161d7fcfc411b0aded5faca0d.1571065041.git.mprivozn@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH] virhostuptime: Add linux stub for musl X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.32]); Mon, 14 Oct 2019 15:04:37 +0000 (UTC) Content-Type: text/plain; charset="utf-8" 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=3D1760885 Signed-off-by: Michal Privoznik Reviewed-by: Cole Robinson --- 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 =20 #include "virhostuptime.h" +#include "viralloc.h" +#include "virfile.h" +#include "virlog.h" +#include "virstring.h" +#include "virtime.h" #include "virthread.h" =20 +#define VIR_FROM_THIS VIR_FROM_NONE + +VIR_LOG_INIT("util.virhostuptime"); + static unsigned long long bootTime; static int bootTimeErrno; static virOnceControl virHostGetBootTimeOnce =3D VIR_ONCE_CONTROL_INITIALI= ZER; =20 -#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 =3D 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 =3D strchr(buf, ' '))) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("uptime file has unexpected format '%s'"), + buf); + return -EINVAL; + } + + *tmp =3D '\0'; + + if (virStrToDouble(buf, NULL, &up) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Unable to parse sched info value '%s'"), + buf); + return -EINVAL; + } + + *btime =3D 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 =3D {.ut_type =3D BOOT_TIME}; struct utmpx *res =3D NULL; =20 @@ -45,16 +97,22 @@ virHostGetBootTimeOnceInit(void) } =20 endutxent(); +# endif /* HAVE_GETUTXID */ + + if (bootTimeErrno =3D=3D ENODATA || + (bootTime =3D=3D 0 && bootTimeErrno =3D=3D 0)) { + bootTimeErrno =3D -virHostGetBootTimeProcfs(&bootTime); + } } =20 -#else /* !HAVE_GETUTXID */ +#else /* !defined(HAVE_GETUTXID) && !defined(__linux__) */ =20 static void virHostGetBootTimeOnceInit(void) { bootTimeErrno =3D ENOSYS; } -#endif /* HAVE_GETUTXID */ +#endif /* !defined(HAVE_GETUTXID) && !defined(__linux__) */ =20 /** * virHostGetBootTime: --=20 2.21.0 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list