From 07a32fc57e52c6788a886a4d60789ae51394d25b Mon Sep 17 00:00:00 2001 From: millert Date: Fri, 2 Jul 2021 15:34:16 +0000 Subject: [PATCH] Use nanosleep() and setitimer() instead of usleep() and ualarm(). Both usleep() and ualarm() are obsolete and were removed from POSIX. OK deraadt@ --- usr.bin/systat/engine.c | 26 +++++++++++++++++++++----- usr.bin/systat/engine.h | 4 ++-- usr.bin/systat/main.c | 11 ++++------- 3 files changed, 27 insertions(+), 14 deletions(-) diff --git a/usr.bin/systat/engine.c b/usr.bin/systat/engine.c index 068174a74cc..84d3146534d 100644 --- a/usr.bin/systat/engine.c +++ b/usr.bin/systat/engine.c @@ -1,4 +1,4 @@ -/* $OpenBSD: engine.c,v 1.28 2021/06/02 08:32:22 martijn Exp $ */ +/* $OpenBSD: engine.c,v 1.29 2021/07/02 15:34:16 millert Exp $ */ /* * Copyright (c) 2001, 2007 Can Erkin Acar * @@ -27,6 +27,7 @@ #include #include #include +#include #include /* XXX These are defined in term.h and conflict with our variable names */ @@ -50,7 +51,9 @@ struct view_ent { TAILQ_ENTRY(view_ent) entries; }; -useconds_t udelay = 5000000; +static struct timespec ts_delay = { 5, 0 }; +static struct itimerval it_delay = { { 0, 0 }, { 5, 0 } }; + int dispstart = 0; int humanreadable = 0; int interactive = 1; @@ -1355,7 +1358,7 @@ engine_loop(int countmax) read_view(); need_sort = 1; gotsig_alarm = 0; - ualarm(udelay, 0); + setitimer(ITIMER_REAL, &it_delay, NULL); } if (need_sort) { @@ -1366,7 +1369,7 @@ engine_loop(int countmax) /* XXX if sort took too long */ if (gotsig_alarm) { gotsig_alarm = 0; - ualarm(udelay, 0); + setitimer(ITIMER_REAL, &it_delay, NULL); } } @@ -1408,7 +1411,7 @@ engine_loop(int countmax) if (interactive && need_update == 0) keyboard(); else if (interactive == 0) - usleep(udelay); + nanosleep(&ts_delay, NULL); } if (rawmode == 0) @@ -1457,3 +1460,16 @@ check_termcap(void) return(0); } + +void +refresh_delay(double delay) +{ + double secs, frac; + + frac = modf(delay, &secs); + ts_delay.tv_sec = secs; + ts_delay.tv_nsec = frac * 1000000000.0; + if (!timespecisset(&ts_delay)) + ts_delay.tv_nsec = 1000000000; + TIMESPEC_TO_TIMEVAL(&it_delay.it_value, &ts_delay); +} diff --git a/usr.bin/systat/engine.h b/usr.bin/systat/engine.h index c8239b5bfbd..0d0b970f4ef 100644 --- a/usr.bin/systat/engine.h +++ b/usr.bin/systat/engine.h @@ -1,4 +1,4 @@ -/* $OpenBSD: engine.h,v 1.13 2021/06/02 08:32:22 martijn Exp $ */ +/* $OpenBSD: engine.h,v 1.14 2021/07/02 15:34:16 millert Exp $ */ /* * Copyright (c) 2001, 2007 Can Erkin Acar * @@ -145,6 +145,7 @@ void show_order(void); void setup_term(int maxpr); int check_termcap(void); +void refresh_delay(double delay); void engine_initialize(void); void engine_loop(int countmax); @@ -156,7 +157,6 @@ const char *message_set(const char *msg); void foreach_view(void (*callback)(field_view *)); extern int sortdir; -extern useconds_t udelay; extern int dispstart; extern int humanreadable; extern int interactive; diff --git a/usr.bin/systat/main.c b/usr.bin/systat/main.c index 1a67a23e3b8..48b42ea0031 100644 --- a/usr.bin/systat/main.c +++ b/usr.bin/systat/main.c @@ -1,4 +1,4 @@ -/* $OpenBSD: main.c,v 1.74 2021/06/02 08:32:22 martijn Exp $ */ +/* $OpenBSD: main.c,v 1.75 2021/07/02 15:34:16 millert Exp $ */ /* * Copyright (c) 2001, 2007 Can Erkin Acar * Copyright (c) 2001 Daniel Hartmeier @@ -332,7 +332,7 @@ cmd_delay(const char *buf) if (errstr != NULL) error("s: \"%s\": delay value is %s", buf, errstr); else { - udelay = (useconds_t)(del * 1000000); + refresh_delay(del); gotsig_alarm = 1; naptime = del; } @@ -557,11 +557,8 @@ main(int argc, char *argv[]) errx(1, "\"%s\": delay value is %s", argv[1], errstr); } - udelay = (useconds_t)(delay * 1000000.0); - if (udelay < 1) - udelay = 1; - - naptime = (double)udelay / 1000000.0; + refresh_delay(delay); + naptime = delay; gethostname(hostname, sizeof (hostname)); gethz(); -- 2.20.1