From nobody Sun Feb 8 19:03:04 2026 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=1566472546; cv=none; d=zoho.com; s=zohoarc; b=Ddm7v2pr1azeBnl2R/mUNNtvHWek9qbhAoKtZAlzpAgxm4FlJyyy/Q8eHPWSbh/gHM4st9FgrFV2F8Ko1FDHUrdHp591Li/jYAEORy5RjOh1nY0CvToLFdWL42oQrReRnm24vy+NFEUNoC/pOsV2U30/UksNsHK4EGAbNB0gH8Q= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1566472546; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=4az1j0Ou4gnJO3rKSBOeqNkbRDfhBe8jD8hwQLgHcmA=; b=bAd5Tdoxw8tj6BYB6aVVL4eInZaZQqaibg6+Dnjv2VbKAxF014SesLLxqx4lic9aWoNQt2nbUItSJCq31+e03amzAoWEevmQV+AVhHa3xG2ETD4O1GgJ1FNgAnTaQQiyOnPNZdwBikmll2dxARzgkuQZ/mtXcZ2vzyL7miadu9Y= 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 1566472546141733.260449214951; Thu, 22 Aug 2019 04:15:46 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id F2706C08EC25; Thu, 22 Aug 2019 11:15:43 +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 985E15DE5C; Thu, 22 Aug 2019 11:15:42 +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 97DB12551E; Thu, 22 Aug 2019 11:15:39 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id x7MBFcI2018672 for ; Thu, 22 Aug 2019 07:15:38 -0400 Received: by smtp.corp.redhat.com (Postfix) id 4A35B60603; Thu, 22 Aug 2019 11:15:38 +0000 (UTC) Received: from moe.brq.redhat.com (unknown [10.43.2.30]) by smtp.corp.redhat.com (Postfix) with ESMTP id C756960605 for ; Thu, 22 Aug 2019 11:15:37 +0000 (UTC) From: Michal Privoznik To: libvir-list@redhat.com Date: Thu, 22 Aug 2019 13:15:32 +0200 Message-Id: <5e6dc2c30e287e8fdae90f1c5ced453cc0a3d943.1566472451.git.mprivozn@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH v2 1/2] util: Introduce virhostuptime 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.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Thu, 22 Aug 2019 11:15:45 +0000 (UTC) Content-Type: text/plain; charset="utf-8" This module contains function to get host boot time. Signed-off-by: Michal Privoznik Reviewed-by: Jiri Denemark --- configure.ac | 1 + src/libvirt_private.syms | 4 ++ src/util/Makefile.inc.am | 2 + src/util/virhostuptime.c | 81 ++++++++++++++++++++++++++++++++++++++++ src/util/virhostuptime.h | 27 ++++++++++++++ 5 files changed, 115 insertions(+) create mode 100644 src/util/virhostuptime.c create mode 100644 src/util/virhostuptime.h diff --git a/configure.ac b/configure.ac index f41c6d5d86..6744ace578 100644 --- a/configure.ac +++ b/configure.ac @@ -337,6 +337,7 @@ AC_CHECK_FUNCS_ONCE([\ getpwuid_r \ getrlimit \ getuid \ + getutxid \ if_indextoname \ mmap \ newlocale \ diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 9db4ac7933..c230a852e7 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2135,6 +2135,10 @@ virHostMemGetStats; virHostMemSetParameters; =20 =20 +# util/virhostuptime.h +virHostGetBootTime; + + # util/viridentity.h virIdentityGetAttr; virIdentityGetCurrent; diff --git a/src/util/Makefile.inc.am b/src/util/Makefile.inc.am index a47f333a98..46866cf213 100644 --- a/src/util/Makefile.inc.am +++ b/src/util/Makefile.inc.am @@ -91,6 +91,8 @@ UTIL_SOURCES =3D \ util/virhostdev.h \ util/virhostmem.c \ util/virhostmem.h \ + util/virhostuptime.c \ + util/virhostuptime.h \ util/viridentity.c \ util/viridentity.h \ util/virinitctl.c \ diff --git a/src/util/virhostuptime.c b/src/util/virhostuptime.c new file mode 100644 index 0000000000..62b781acd5 --- /dev/null +++ b/src/util/virhostuptime.c @@ -0,0 +1,81 @@ +/* + * virhostuptime.c: helper APIs for host uptime + * + * Copyright (C) 2019 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * . + */ + +#include + +#ifdef HAVE_GETUTXID +# include +#endif + +#include "virhostuptime.h" +#include "virthread.h" + +static unsigned long long bootTime; +static int bootTimeErrno; +static virOnceControl virHostGetBootTimeOnce =3D VIR_ONCE_CONTROL_INITIALI= ZER; + +#ifdef HAVE_GETUTXID +static void +virHostGetBootTimeOnceInit(void) +{ + struct utmpx id =3D {.ut_type =3D BOOT_TIME}; + struct utmpx *res =3D NULL; + + if (!(res =3D getutxid(&id))) { + bootTimeErrno =3D errno; + } else { + bootTime =3D res->ut_tv.tv_sec; + } + + endutxent(); +} + +#else /* !HAVE_GETUTXID */ + +static void +virHostGetBootTimeOnceInit(void) +{ + bootTimeErrno =3D ENOSYS; +} +#endif /* HAVE_GETUTXID */ + +/** + * virHostGetBootTime: + * @when: UNIX timestamp of boot time + * + * Get a UNIX timestamp of host boot time and store it at @when. + * + * Return: 0 on success, + * -1 otherwise. + */ +int +virHostGetBootTime(unsigned long long *when) +{ + if (virOnce(&virHostGetBootTimeOnce, virHostGetBootTimeOnceInit) < 0) + return -1; + + if (bootTimeErrno) { + errno =3D bootTimeErrno; + return -1; + } + + *when =3D bootTime; + return 0; +} diff --git a/src/util/virhostuptime.h b/src/util/virhostuptime.h new file mode 100644 index 0000000000..03c1517a64 --- /dev/null +++ b/src/util/virhostuptime.h @@ -0,0 +1,27 @@ +/* + * virhostuptime.h: helper APIs for host uptime + * + * Copyright (C) 2019 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * . + */ + +#pragma once + +#include "internal.h" + +int +virHostGetBootTime(unsigned long long *when) + ATTRIBUTE_NOINLINE; --=20 2.21.0 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list