aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWarner Losh <imp@FreeBSD.org>2021-05-05 21:11:56 +0000
committerWarner Losh <imp@FreeBSD.org>2021-05-05 21:12:38 +0000
commitf009aedae4d054819848ab3f7ef1b846c4d35d3c (patch)
tree32b868d0d4f796c267590987748ba2ae54141c33
parent6c34dde83ee61fc0ba095dcfdac2f381f6bae007 (diff)
downloadsrc-f009aedae4d054819848ab3f7ef1b846c4d35d3c.tar.gz
src-f009aedae4d054819848ab3f7ef1b846c4d35d3c.zip
bsnmpd: Return the correct uptime.
Do not assume that the kernel boot time is invariant. It is not. FreeBSD uses the formula: wall_time = boot_time + uptime where uptime is monotinically increasing and boot_time is adjusted to get the proper time of day. FreeBSD offers a way to retrieve the uptime directly, so use that instead of trying to compute it by subtracting boot_time from wall_time. Sponsored by: Netflix Reviewed by: cy@ Differential Revision: https://reviews.freebsd.org/D30114
-rw-r--r--usr.sbin/bsnmpd/modules/snmp_hostres/hostres_scalars.c37
1 files changed, 7 insertions, 30 deletions
diff --git a/usr.sbin/bsnmpd/modules/snmp_hostres/hostres_scalars.c b/usr.sbin/bsnmpd/modules/snmp_hostres/hostres_scalars.c
index 6634ae271a45..872b8a5da329 100644
--- a/usr.sbin/bsnmpd/modules/snmp_hostres/hostres_scalars.c
+++ b/usr.sbin/bsnmpd/modules/snmp_hostres/hostres_scalars.c
@@ -47,9 +47,6 @@
#include "hostres_oid.h"
#include "hostres_tree.h"
-/* boot timestamp in centi-seconds */
-static uint64_t kernel_boot;
-
/* physical memory size in Kb */
static uint64_t phys_mem_size;
@@ -76,39 +73,19 @@ fini_scalars(void)
static int
OS_getSystemUptime(uint32_t *ut)
{
- struct timeval right_now;
- uint64_t now;
-
- if (kernel_boot == 0) {
- /* first time, do the sysctl */
- struct timeval kernel_boot_timestamp;
- int mib[2] = { CTL_KERN, KERN_BOOTTIME };
- size_t len = sizeof(kernel_boot_timestamp);
-
- if (sysctl(mib, nitems(mib), &kernel_boot_timestamp,
- &len, NULL, 0) == -1) {
- syslog(LOG_ERR, "sysctl KERN_BOOTTIME failed: %m");
- return (SNMP_ERR_GENERR);
- }
+ uint64_t uptime;
+ struct timespec ts;
- HRDBG("boot timestamp from kernel: {%lld, %ld}",
- (long long)kernel_boot_timestamp.tv_sec,
- (long)kernel_boot_timestamp.tv_usec);
-
- kernel_boot = ((uint64_t)kernel_boot_timestamp.tv_sec * 100) +
- (kernel_boot_timestamp.tv_usec / 10000);
- }
-
- if (gettimeofday(&right_now, NULL) < 0) {
- syslog(LOG_ERR, "gettimeofday failed: %m");
+ if (clock_gettime(CLOCK_UPTIME, &ts)) {
+ syslog(LOG_ERR, "clock_gettime failed: %m");
return (SNMP_ERR_GENERR);
}
- now = ((uint64_t)right_now.tv_sec * 100) + (right_now.tv_usec / 10000);
- if (now - kernel_boot > UINT32_MAX)
+ uptime = ts.tv_sec * 100 + ts.tv_nsec / 1000000;
+ if (uptime > UINT32_MAX)
*ut = UINT32_MAX;
else
- *ut = now - kernel_boot;
+ *ut = (uint32_t)uptime;
return (SNMP_ERR_NOERROR);
}