summaryrefslogtreecommitdiff
path: root/usr.bin
diff options
context:
space:
mode:
authorcheloha <cheloha@cvs.openbsd.org>2017-12-18 05:51:54 +0000
committercheloha <cheloha@cvs.openbsd.org>2017-12-18 05:51:54 +0000
commit8e394bb0e5f25c84775150ebb5389670cae488c7 (patch)
treee2716e9835a7872f76ef5fcfa82893972f1624c4 /usr.bin
parent8fd06186df470fde0080330a0b2f5829bf0c6a1d (diff)
Add the CLOCK_BOOTTIME clockid for use with clock_gettime(2)
and put it to use in userspace in lieu of the kern.boottime sysctl. Its absolute value is the time that has elapsed since the system booted, i.e., the system uptime. Use in top(1), w(1), and snmpd(8) eliminates a race with settimeofday(2), adjtime(2), etc. inherent to deriving the system uptime via the kern.boottime sysctl. Product of a great deal of discussion/revision with jca@, tb@, and guenther@. ok tb@ jca@ guenther@ dlg@ mlarkin@ tom@
Diffstat (limited to 'usr.bin')
-rw-r--r--usr.bin/top/display.c18
-rw-r--r--usr.bin/w/w.c14
2 files changed, 9 insertions, 23 deletions
diff --git a/usr.bin/top/display.c b/usr.bin/top/display.c
index 30b20b06489..2b2e678b63f 100644
--- a/usr.bin/top/display.c
+++ b/usr.bin/top/display.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: display.c,v 1.52 2017/03/15 04:24:14 deraadt Exp $ */
+/* $OpenBSD: display.c,v 1.53 2017/12/18 05:51:53 cheloha Exp $ */
/*
* Top users/processes display for Unix
@@ -57,7 +57,6 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
-#include <sys/sysctl.h>
#include "screen.h" /* interface to screen package */
#include "layout.h" /* defines for screen position layout */
@@ -209,22 +208,15 @@ display_init(struct statics * statics)
static void
format_uptime(char *buf, size_t buflen)
{
- time_t now, uptime;
+ time_t uptime;
int days, hrs, mins;
- int mib[2];
- size_t size;
- struct timeval boottime;
+ struct timespec boottime;
- now = time(NULL);
/*
* Print how long system has been up.
- * (Found by getting "boottime" from the kernel)
*/
- mib[0] = CTL_KERN;
- mib[1] = KERN_BOOTTIME;
- size = sizeof(boottime);
- if (sysctl(mib, 2, &boottime, &size, NULL, 0) != -1) {
- uptime = now - boottime.tv_sec;
+ if (clock_gettime(CLOCK_BOOTTIME, &boottime) != -1) {
+ uptime = boottime.tv_sec;
uptime += 30;
days = uptime / (3600 * 24);
uptime %= (3600 * 24);
diff --git a/usr.bin/w/w.c b/usr.bin/w/w.c
index e85784a49b6..5642197f418 100644
--- a/usr.bin/w/w.c
+++ b/usr.bin/w/w.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: w.c,v 1.64 2017/12/14 18:03:03 jasper Exp $ */
+/* $OpenBSD: w.c,v 1.65 2017/12/18 05:51:53 cheloha Exp $ */
/*-
* Copyright (c) 1980, 1991, 1993, 1994
@@ -66,7 +66,6 @@
#include "extern.h"
-struct timeval boottime;
struct utmp utmp;
struct winsize ws;
kvm_t *kd;
@@ -426,10 +425,9 @@ static void
pr_header(time_t *nowp, int nusers)
{
double avenrun[3];
+ struct timespec boottime;
time_t uptime;
int days, hrs, i, mins;
- int mib[2];
- size_t size;
char buf[256];
/*
@@ -441,13 +439,9 @@ pr_header(time_t *nowp, int nusers)
/*
* Print how long system has been up.
- * (Found by getting "boottime" from the kernel)
*/
- mib[0] = CTL_KERN;
- mib[1] = KERN_BOOTTIME;
- size = sizeof(boottime);
- if (sysctl(mib, 2, &boottime, &size, NULL, 0) != -1) {
- uptime = now - boottime.tv_sec;
+ if (clock_gettime(CLOCK_BOOTTIME, &boottime) != -1) {
+ uptime = boottime.tv_sec;
if (uptime > 59) {
uptime += 30;
days = uptime / SECSPERDAY;