uptime(1): also report uptime

It might be more useful this way.  *cough*

Change-Id: I318169fef8bf7737dc46eebf5c5332ce42a9076a
This commit is contained in:
David van Moolenbroek 2014-08-28 11:40:55 +00:00
parent cd34841de5
commit 7e94554b62
3 changed files with 32 additions and 2 deletions

View file

@ -129,7 +129,7 @@ minix_getproc(void * __unused dummy, int op, int arg, int elemsize, int *cnt)
for (i = 0; i < npids; i++) { for (i = 0; i < npids; i++) {
pid = pids[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. */ /* Processes may legitimately disappear between calls. */
if ((fp = fopen(path, "r")) == NULL) 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; int fd, argc;
/* Get the command line of the process from procfs. */ /* 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) if ((fd = open(path, O_RDONLY)) < 0)
return NULL; 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; 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;
}

View file

@ -44,4 +44,6 @@ int minix_proc_compare(const struct minix_proc *p1,
#undef proc_compare_wrapper #undef proc_compare_wrapper
#define proc_compare_wrapper minix_proc_compare #define proc_compare_wrapper minix_proc_compare
int minix_getuptime(time_t *timep);
#endif /* !_W_MINIX_PROC_H */ #endif /* !_W_MINIX_PROC_H */

View file

@ -475,8 +475,12 @@ pr_header(time_t *nowp, int nusers)
double avenrun[3]; double avenrun[3];
time_t uptime; time_t uptime;
int days, hrs, mins; int days, hrs, mins;
#ifndef __minix
int mib[2]; int mib[2];
size_t size, i; size_t size, i;
#else
size_t i;
#endif /* __minix */
char buf[256]; char buf[256];
/* /*
@ -493,12 +497,16 @@ pr_header(time_t *nowp, int nusers)
* Print how long system has been up. * Print how long system has been up.
* (Found by looking getting "boottime" from the kernel) * (Found by looking getting "boottime" from the kernel)
*/ */
#ifndef __minix
mib[0] = CTL_KERN; mib[0] = CTL_KERN;
mib[1] = KERN_BOOTTIME; mib[1] = KERN_BOOTTIME;
size = sizeof(boottime); size = sizeof(boottime);
if (sysctl(mib, 2, &boottime, &size, NULL, 0) != -1 && if (sysctl(mib, 2, &boottime, &size, NULL, 0) != -1 &&
boottime.tv_sec != 0) { boottime.tv_sec != 0) {
uptime = now - boottime.tv_sec; uptime = now - boottime.tv_sec;
#else
if (minix_getuptime(&uptime) != -1) {
#endif /* __minix */
uptime += 30; uptime += 30;
if (uptime > SECSPERMIN) { if (uptime > SECSPERMIN) {
days = uptime / SECSPERDAY; days = uptime / SECSPERDAY;