From 7e94554b6208295bfbfce701dbb2131f17daa0a5 Mon Sep 17 00:00:00 2001 From: David van Moolenbroek Date: Thu, 28 Aug 2014 11:40:55 +0000 Subject: [PATCH] uptime(1): also report uptime It might be more useful this way. *cough* Change-Id: I318169fef8bf7737dc46eebf5c5332ce42a9076a --- minix/usr.bin/w/minix_proc.c | 24 ++++++++++++++++++++++-- minix/usr.bin/w/minix_proc.h | 2 ++ usr.bin/w/w.c | 8 ++++++++ 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/minix/usr.bin/w/minix_proc.c b/minix/usr.bin/w/minix_proc.c index a5d112fdd..665196c9e 100644 --- a/minix/usr.bin/w/minix_proc.c +++ b/minix/usr.bin/w/minix_proc.c @@ -129,7 +129,7 @@ minix_getproc(void * __unused dummy, int op, int arg, int elemsize, int *cnt) for (i = 0; i < npids; i++) { pid = pids[i]; - snprintf(path, sizeof(path), _PATH_PROC "/%u/psinfo", pid); + snprintf(path, sizeof(path), _PATH_PROC "%u/psinfo", pid); /* Processes may legitimately disappear between calls. */ if ((fp = fopen(path, "r")) == NULL) @@ -165,7 +165,7 @@ minix_getargv(void * __unused dummy, const struct minix_proc * p, int nchr) int fd, argc; /* Get the command line of the process from procfs. */ - snprintf(path, sizeof(path), _PATH_PROC "/%u/cmdline", p->p_pid); + snprintf(path, sizeof(path), _PATH_PROC "%u/cmdline", p->p_pid); if ((fd = open(path, O_RDONLY)) < 0) return NULL; @@ -251,3 +251,23 @@ minix_proc_compare(const struct minix_proc * p1, const struct minix_proc * p2) */ return p1->p_pid < p2->p_pid; } + +/* + * Obtain the system uptime in seconds. Return 0 on success, with the uptime + * stored in the given time_t field. Return -1 on failure. + */ +int +minix_getuptime(time_t *timep) +{ + FILE *fp; + int r; + + if ((fp = fopen(_PATH_PROC "uptime", "r")) == NULL) + return -1; + + r = fscanf(fp, "%llu", timep); + + fclose(fp); + + return (r == 1) ? 0 : -1; +} diff --git a/minix/usr.bin/w/minix_proc.h b/minix/usr.bin/w/minix_proc.h index a216dbb45..a38bedbe9 100644 --- a/minix/usr.bin/w/minix_proc.h +++ b/minix/usr.bin/w/minix_proc.h @@ -44,4 +44,6 @@ int minix_proc_compare(const struct minix_proc *p1, #undef proc_compare_wrapper #define proc_compare_wrapper minix_proc_compare +int minix_getuptime(time_t *timep); + #endif /* !_W_MINIX_PROC_H */ diff --git a/usr.bin/w/w.c b/usr.bin/w/w.c index 1516ef357..4a80312e3 100644 --- a/usr.bin/w/w.c +++ b/usr.bin/w/w.c @@ -475,8 +475,12 @@ pr_header(time_t *nowp, int nusers) double avenrun[3]; time_t uptime; int days, hrs, mins; +#ifndef __minix int mib[2]; size_t size, i; +#else + size_t i; +#endif /* __minix */ char buf[256]; /* @@ -493,12 +497,16 @@ pr_header(time_t *nowp, int nusers) * Print how long system has been up. * (Found by looking getting "boottime" from the kernel) */ +#ifndef __minix mib[0] = CTL_KERN; mib[1] = KERN_BOOTTIME; size = sizeof(boottime); if (sysctl(mib, 2, &boottime, &size, NULL, 0) != -1 && boottime.tv_sec != 0) { uptime = now - boottime.tv_sec; +#else + if (minix_getuptime(&uptime) != -1) { +#endif /* __minix */ uptime += 30; if (uptime > SECSPERMIN) { days = uptime / SECSPERDAY;