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++) {
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;
}

View file

@ -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 */

View file

@ -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;