From owner-dev-commits-src-main@freebsd.org Wed May 5 21:13:46 2021 Return-Path: Delivered-To: dev-commits-src-main@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 33704623E91; Wed, 5 May 2021 21:13:46 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Fb8Zy0zhpz4lLH; Wed, 5 May 2021 21:13:46 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 0B370429D; Wed, 5 May 2021 21:13:46 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 145LDjLs055387; Wed, 5 May 2021 21:13:45 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 145LDjbh055386; Wed, 5 May 2021 21:13:45 GMT (envelope-from git) Date: Wed, 5 May 2021 21:13:45 GMT Message-Id: <202105052113.145LDjbh055386@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Warner Losh Subject: git: f009aedae4d0 - main - bsnmpd: Return the correct uptime. MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: imp X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: f009aedae4d054819848ab3f7ef1b846c4d35d3c Auto-Submitted: auto-generated X-BeenThere: dev-commits-src-main@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Commit messages for the main branch of the src repository List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 05 May 2021 21:13:46 -0000 The branch main has been updated by imp: URL: https://cgit.FreeBSD.org/src/commit/?id=f009aedae4d054819848ab3f7ef1b846c4d35d3c commit f009aedae4d054819848ab3f7ef1b846c4d35d3c Author: Warner Losh AuthorDate: 2021-05-05 21:11:56 +0000 Commit: Warner Losh 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); }