From: Bruno Haible Date: Tue, 15 Aug 2023 19:01:13 +0000 (-0700) Subject: uptime: Include VM sleep time in the "up" duration X-Git-Tag: v9.4~25 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d8cfa5db49641f39b63aba2037c6a9e6bd95c0e0;p=thirdparty%2Fcoreutils.git uptime: Include VM sleep time in the "up" duration * src/uptime.c: Don't include c-strtod.h. (print_uptime): Don't read /proc/uptime, because the value it provides does not change when a date adjustment occurs. * bootstrap.conf (gnulib_modules): Remove 'uptime'. --- diff --git a/bootstrap.conf b/bootstrap.conf index 6ce27d5dd2..d758a9ff63 100644 --- a/bootstrap.conf +++ b/bootstrap.conf @@ -285,7 +285,6 @@ gnulib_modules=" unlocked-io unsetenv update-copyright - uptime useless-if-before-free userspec utimecmp diff --git a/src/uptime.c b/src/uptime.c index 22a3bdb199..813d29430a 100644 --- a/src/uptime.c +++ b/src/uptime.c @@ -30,7 +30,6 @@ # include #endif -#include "c-strtod.h" #include "long-options.h" #include "quote.h" #include "readutmp.h" @@ -50,33 +49,13 @@ print_uptime (idx_t n, struct gl_utmp const *this) idx_t entries = 0; time_t boot_time = 0; time_t time_now; - time_t uptime = 0; + time_t uptime; intmax_t updays; int uphours; int upmins; struct tm *tmn; double avg[3]; int loads; -#ifdef HAVE_PROC_UPTIME - FILE *fp; - - fp = fopen ("/proc/uptime", "r"); - if (fp != nullptr) - { - char buf[BUFSIZ]; - char *b = fgets (buf, BUFSIZ, fp); - if (b == buf) - { - char *end_ptr; - double upsecs = c_strtod (buf, &end_ptr); - if (buf != end_ptr) - uptime = (0 <= upsecs && upsecs < TYPE_MAXIMUM (time_t) - ? upsecs : -1); - } - - fclose (fp); - } -#endif /* HAVE_PROC_UPTIME */ #if HAVE_SYSCTL && ! defined __GLIBC__ \ && defined CTL_KERN && defined KERN_BOOTTIME @@ -109,16 +88,13 @@ print_uptime (idx_t n, struct gl_utmp const *this) boot_time = this->ut_ts.tv_sec; ++this; } + /* The gnulib module 'readutmp' is supposed to provide a BOOT_TIME entry + on all platforms. */ + if (boot_time == 0) + error (EXIT_FAILURE, errno, _("couldn't get boot time")); time_now = time (nullptr); -#if defined HAVE_PROC_UPTIME - if (uptime == 0) -#endif - { - if (boot_time == 0) - error (EXIT_FAILURE, errno, _("couldn't get boot time")); - uptime = time_now - boot_time; - } + uptime = time_now - boot_time; updays = uptime / 86400; uphours = uptime % 86400 / 3600; upmins = uptime % 86400 % 3600 / 60;