Date: Wed, 5 May 2021 21:13:45 GMT From: Warner Losh <imp@FreeBSD.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org Subject: git: f009aedae4d0 - main - bsnmpd: Return the correct uptime. Message-ID: <202105052113.145LDjbh055386@gitrepo.freebsd.org>
next in thread | raw e-mail | index | archive | help
The branch main has been updated by imp: URL: https://cgit.FreeBSD.org/src/commit/?id=f009aedae4d054819848ab3f7ef1b846c4d35d3c commit f009aedae4d054819848ab3f7ef1b846c4d35d3c Author: Warner Losh <imp@FreeBSD.org> AuthorDate: 2021-05-05 21:11:56 +0000 Commit: Warner Losh <imp@FreeBSD.org> CommitDate: 2021-05-05 21:12:38 +0000 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 --- .../bsnmpd/modules/snmp_hostres/hostres_scalars.c | 37 ++++------------------ 1 file 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); }
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202105052113.145LDjbh055386>