bug-coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

uptime on GNU


From: Alfred M. Szmidt
Subject: uptime on GNU
Date: Wed, 29 Dec 2004 20:28:18 +0100

The following makes uptime work on GNU, we don't use the same means as
GNU/Linux or *BSD to get the boot time.

It was tested by Michael Banck.

Happy new year.

ChangeLog
2004-12-29  Alfred M. Szmidt  <address@hidden>

        * src/uptime.c [HAVE_PS_CONTEXT_CREATE && HAVE_PS_H]: Include
        <ps.h>.
        (fetch_boot_time) [HAVE_PS_CONTEXT_CREATE]: New function.
        (print_uptime) [HAVE_PS_CONTEXT_CREATE]: Use it to get the uptime
        on GNU.
        * src/Makefile.am (uptime_LDADD): Added `$(LIB_PS)'

m4/ChangeLog
2004-12-29  Alfred M. Szmidt  <address@hidden>

        * lib-check.m4 (gl_LIB_PS_CHECK): New function.
        (gl_LIB_CHECK): Use it.

diff -ur coreutils.orig/m4/lib-check.m4 coreutils/m4/lib-check.m4
--- coreutils.orig/m4/lib-check.m4      2004-04-13 17:28:45.000000000 +0200
+++ coreutils/m4/lib-check.m4   2004-12-29 18:52:20.000000000 +0100
@@ -2,6 +2,18 @@
 
 dnl Misc lib-related macros for fileutils, sh-utils, textutils.
 
+AC_DEFUN([gl_LIB_PS_CHECK],
+[
+  ac_saved_libs="$LIBS"
+  AC_SEARCH_LIBS(ps_context_create, [ps],
+                 [test "$ac_cv_search_ps_context_create" = "none required" ||
+                  LIB_PS=$ac_cv_search_ps_context_create])
+  AC_SUBST(LIB_PS)
+  AC_CHECK_FUNCS(ps_context_create)
+  AC_CHECK_HEADERS(ps.h)
+  LIBS="$ac_saved_libs"
+])
+
 AC_DEFUN([gl_LIB_CHECK],
 [
 
@@ -11,6 +23,8 @@
   # m88k running dgux 5.4 needs this
   AC_CHECK_LIB(ldgc, main)
 
+  gl_LIB_PS_CHECK
+
   # Some programs need to link with -lm.  printf does if it uses
   # lib/strtod.c which uses pow.  And seq uses the math functions,
   # floor, modf, rint.  And factor uses sqrt.  And sleep uses fesetround.
diff -ur coreutils.orig/src/Makefile.am coreutils/src/Makefile.am
--- coreutils.orig/src/Makefile.am      2004-12-15 00:53:48.000000000 +0100
+++ coreutils/src/Makefile.am   2004-12-29 13:28:25.000000000 +0100
@@ -74,7 +74,7 @@
 sleep_LDADD = $(nanosec_libs)
 tail_LDADD = $(nanosec_libs)
 
-uptime_LDADD = $(LDADD) $(GETLOADAVG_LIBS)
+uptime_LDADD = $(LDADD) $(GETLOADAVG_LIBS) $(LIB_PS)
 
 su_LDADD = $(LDADD) $(LIB_CRYPT)
 
diff -ur coreutils.orig/src/uptime.c coreutils/src/uptime.c
--- coreutils.orig/src/uptime.c 2004-11-30 22:40:29.000000000 +0100
+++ coreutils/src/uptime.c      2004-12-29 18:05:47.000000000 +0100
@@ -28,6 +28,10 @@
 # include <sys/sysctl.h>
 #endif
 
+#if HAVE_PS_CONTEXT_CREATE && HAVE_PS_H
+# include <ps.h>
+#endif
+
 #include "c-strtod.h"
 #include "error.h"
 #include "long-options.h"
@@ -44,6 +48,41 @@
 /* The name this program was run with. */
 char *program_name;
 
+#if HAVE_PS_CONTEXT_CREATE
+/* Find the absolute timestamp of when the system was booted.  The GNU
+   system defines "system boot time" as the task creation time of PID
+   1 (init).  */
+static error_t
+fetch_boot_time (struct timeval *when)
+{
+  error_t err = 0;
+  struct ps_context *context;
+  struct proc_stat *ps;
+  
+  err = ps_context_create (getproc (), &context);
+  if (err)
+    return err;
+  
+  err = ps_context_find_proc_stat (context, 1, &ps);
+  if (err)
+    return err;
+  
+  err = proc_stat_set_flags (ps, PSTAT_TASK_BASIC);
+  if (!err && !(ps->flags & PSTAT_TASK_BASIC))
+    err = EGRATUITOUS;
+  else
+    {
+      time_value_t *const tv = &proc_stat_task_basic_info (ps)->creation_time;
+      when->tv_sec = tv->seconds;
+      when->tv_usec = tv->microseconds;
+    }
+  
+  ps_context_free (context);
+  
+  return err;
+}
+#endif
+
 static void
 print_uptime (size_t n, const STRUCT_UTMP *this)
 {
@@ -91,6 +130,16 @@
   }
 #endif
 
+#if HAVE_PS_CONTEXT_CREATE
+  {
+    struct timeval result;
+    if (fetch_boot_time (&result))
+      boot_time = 0;
+    else
+      boot_time = result.tv_sec;
+  }
+#endif
+
   /* Loop through all the utmp entries we just read and count up the valid
      ones, also in the process possibly gleaning boottime. */
   while (n--)




reply via email to

[Prev in Thread] Current Thread [Next in Thread]