On OpenBSD
, uptime
and w
are actually the same program:
$ ls -lt /usr/bin/uptime /usr/bin/w
-r-xr-xr-x 2 root bin 18136 May 30 12:53 /usr/bin/uptime
-r-xr-xr-x 2 root bin 18136 May 30 12:53 /usr/bin/w
and the source code is usr.bin/w/w.c.
Compare the outputs of uptime
and w
:
$ uptime
10:59AM up 7 days, 1:51, 1 user, load averages: 0.00, 0.00, 0.00
$ w
10:59AM up 7 days, 1:51, 1 user, load averages: 0.00, 0.00, 0.00
USER TTY FROM LOGIN@ IDLE WHAT
root p0 10.217.242.57 9:10AM 0 w
You can see the uptime
just displays the first line of w
, and w
also shows the login users’ information.
w
uses clock_gettime to get system up time:
if (clock_gettime(CLOCK_BOOTTIME, &boottime) != -1) {
......
}
and getloadavg to retrieve system load average int the past 1
, 5
, and 15
minutes:
int
getloadavg(double loadavg[], int nelem)
{
......
mib[0] = CTL_VM;
mib[1] = VM_LOADAVG;
size = sizeof(loadinfo);
if (sysctl(mib, 2, &loadinfo, &size, NULL, 0) < 0)
return (-1);
......
}
The current user login information is kept in /var/run/utmp
, and it is composed of utmp struct:
struct utmp {
char ut_line[UT_LINESIZE];
char ut_name[UT_NAMESIZE];
char ut_host[UT_HOSTSIZE];
time_t ut_time;
};
utmp.ut_line
is the login terminal (remove “tty
” prefix); utmp.ut_name
is the login user name; utmp.ut_host
is the login address and the utmp.ut_time
is the login time. These are the first 4
columns of every line:
USER TTY FROM LOGIN@ IDLE WHAT
root p0 10.217.242.57 9:10AM 0 w
The IDLE
column displays how long has passed since you last operates on terminal:
if ((ep->idle = now - stp->st_atime) < 0)
ep->idle = 0;
and WHAT
shows the current process.