[libvirt] [PATCH v2 1/2] util: Introduce virhostuptime

Michal Privoznik mprivozn at redhat.com
Thu Aug 22 11:15:32 UTC 2019


This module contains function to get host boot time.

Signed-off-by: Michal Privoznik <mprivozn at redhat.com>
---
 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;
 
 
+# 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 = \
 	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
+ * <http://www.gnu.org/licenses/>.
+ */
+
+#include <config.h>
+
+#ifdef HAVE_GETUTXID
+# include <utmpx.h>
+#endif
+
+#include "virhostuptime.h"
+#include "virthread.h"
+
+static unsigned long long bootTime;
+static int bootTimeErrno;
+static virOnceControl virHostGetBootTimeOnce = VIR_ONCE_CONTROL_INITIALIZER;
+
+#ifdef HAVE_GETUTXID
+static void
+virHostGetBootTimeOnceInit(void)
+{
+    struct utmpx id = {.ut_type = BOOT_TIME};
+    struct utmpx *res = NULL;
+
+    if (!(res = getutxid(&id))) {
+        bootTimeErrno = errno;
+    } else {
+        bootTime = res->ut_tv.tv_sec;
+    }
+
+    endutxent();
+}
+
+#else /* !HAVE_GETUTXID */
+
+static void
+virHostGetBootTimeOnceInit(void)
+{
+    bootTimeErrno = 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 = bootTimeErrno;
+        return -1;
+    }
+
+    *when = 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
+ * <http://www.gnu.org/licenses/>.
+ */
+
+#pragma once
+
+#include "internal.h"
+
+int
+virHostGetBootTime(unsigned long long *when)
+    ATTRIBUTE_NOINLINE;
-- 
2.21.0




More information about the libvir-list mailing list