1994-05-27 12:33:43 +00:00
|
|
|
/*
|
2017-11-20 19:49:47 +00:00
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
*
|
1994-05-27 12:33:43 +00:00
|
|
|
* Copyright (c) 1983, 1993, 1994
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2017-02-28 23:42:47 +00:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1994-05-27 12:33:43 +00:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef lint
|
2000-03-26 14:50:56 +00:00
|
|
|
static const char copyright[] =
|
1994-05-27 12:33:43 +00:00
|
|
|
"@(#) Copyright (c) 1983, 1993, 1994\n\
|
|
|
|
The Regents of the University of California. All rights reserved.\n";
|
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#ifndef lint
|
2002-04-28 10:56:09 +00:00
|
|
|
static const char sccsid[] = "@(#)ruptime.c 8.2 (Berkeley) 4/5/94";
|
1994-05-27 12:33:43 +00:00
|
|
|
#endif /* not lint */
|
|
|
|
|
2002-04-28 10:56:09 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
|
|
|
|
#include <protocols/rwhod.h>
|
|
|
|
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <err.h>
|
2009-12-13 03:14:06 +00:00
|
|
|
#include <errno.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2011-11-06 08:16:41 +00:00
|
|
|
static struct hs {
|
2011-10-16 07:36:27 +00:00
|
|
|
struct whod hs_wd;
|
1994-05-27 12:33:43 +00:00
|
|
|
int hs_nusers;
|
|
|
|
} *hs;
|
2011-10-16 07:36:27 +00:00
|
|
|
#define LEFTEARTH(h) (now - (h) > 4*24*60*60)
|
|
|
|
#define ISDOWN(h) (now - (h)->hs_wd.wd_recvtime > 11 * 60)
|
|
|
|
#define WHDRSIZE __offsetof(struct whod, wd_we)
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2011-11-06 08:16:41 +00:00
|
|
|
static size_t nhosts;
|
|
|
|
static time_t now;
|
|
|
|
static int rflg = 1;
|
|
|
|
static DIR *dirp;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2011-11-06 08:16:41 +00:00
|
|
|
static int hscmp(const void *, const void *);
|
|
|
|
static char *interval(time_t, const char *);
|
2017-03-04 04:06:33 +00:00
|
|
|
static int iwidth(int);
|
2011-11-06 08:16:41 +00:00
|
|
|
static int lcmp(const void *, const void *);
|
|
|
|
static void ruptime(const char *, int, int (*)(const void *, const void *));
|
|
|
|
static int tcmp(const void *, const void *);
|
|
|
|
static int ucmp(const void *, const void *);
|
|
|
|
static void usage(void);
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
int
|
2002-04-28 10:56:09 +00:00
|
|
|
main(int argc, char *argv[])
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2002-03-22 01:33:25 +00:00
|
|
|
int (*cmp)(const void *, const void *);
|
2003-03-01 23:09:26 +00:00
|
|
|
int aflg, ch;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
aflg = 0;
|
|
|
|
cmp = hscmp;
|
1997-03-29 04:34:07 +00:00
|
|
|
while ((ch = getopt(argc, argv, "alrut")) != -1)
|
1994-05-27 12:33:43 +00:00
|
|
|
switch (ch) {
|
|
|
|
case 'a':
|
|
|
|
aflg = 1;
|
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
cmp = lcmp;
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
rflg = -1;
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
cmp = tcmp;
|
|
|
|
break;
|
|
|
|
case 'u':
|
|
|
|
cmp = ucmp;
|
|
|
|
break;
|
1995-05-30 06:41:30 +00:00
|
|
|
default:
|
1994-05-27 12:33:43 +00:00
|
|
|
usage();
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
if (chdir(_PATH_RWHODIR) || (dirp = opendir(".")) == NULL)
|
|
|
|
err(1, "%s", _PATH_RWHODIR);
|
|
|
|
|
2003-03-01 23:09:26 +00:00
|
|
|
ruptime(*argv, aflg, cmp);
|
|
|
|
while (*argv++ != NULL) {
|
|
|
|
if (*argv == NULL)
|
|
|
|
break;
|
|
|
|
ruptime(*argv, aflg, cmp);
|
|
|
|
}
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2011-11-06 08:16:41 +00:00
|
|
|
static char *
|
2003-03-01 23:09:26 +00:00
|
|
|
interval(time_t tval, const char *updown)
|
|
|
|
{
|
|
|
|
static char resbuf[32];
|
|
|
|
int days, hours, minutes;
|
|
|
|
|
|
|
|
if (tval < 0) {
|
2011-10-16 07:36:27 +00:00
|
|
|
(void)snprintf(resbuf, sizeof(resbuf), "%s ??:??", updown);
|
2003-03-01 23:09:26 +00:00
|
|
|
return (resbuf);
|
|
|
|
}
|
2011-10-16 07:36:27 +00:00
|
|
|
/* Round to minutes. */
|
2003-03-01 23:09:26 +00:00
|
|
|
minutes = (tval + (60 - 1)) / 60;
|
|
|
|
hours = minutes / 60;
|
|
|
|
minutes %= 60;
|
|
|
|
days = hours / 24;
|
|
|
|
hours %= 24;
|
|
|
|
if (days)
|
|
|
|
(void)snprintf(resbuf, sizeof(resbuf),
|
2011-10-16 07:36:27 +00:00
|
|
|
"%s %4d+%02d:%02d", updown, days, hours, minutes);
|
2003-03-01 23:09:26 +00:00
|
|
|
else
|
|
|
|
(void)snprintf(resbuf, sizeof(resbuf),
|
2011-10-16 07:36:27 +00:00
|
|
|
"%s %2d:%02d", updown, hours, minutes);
|
2003-03-01 23:09:26 +00:00
|
|
|
return (resbuf);
|
|
|
|
}
|
|
|
|
|
2017-03-04 04:06:33 +00:00
|
|
|
/* Width to print a small nonnegative integer. */
|
|
|
|
static int
|
|
|
|
iwidth(int w)
|
|
|
|
{
|
|
|
|
if (w < 10)
|
|
|
|
return (1);
|
|
|
|
if (w < 100)
|
|
|
|
return (2);
|
|
|
|
if (w < 1000)
|
|
|
|
return (3);
|
|
|
|
if (w < 10000)
|
|
|
|
return (4);
|
|
|
|
return (5);
|
|
|
|
}
|
|
|
|
|
2003-03-01 23:09:26 +00:00
|
|
|
#define HS(a) ((const struct hs *)(a))
|
|
|
|
|
|
|
|
/* Alphabetical comparison. */
|
2011-11-06 08:16:41 +00:00
|
|
|
static int
|
2003-03-01 23:09:26 +00:00
|
|
|
hscmp(const void *a1, const void *a2)
|
|
|
|
{
|
|
|
|
return (rflg *
|
2011-10-16 07:36:27 +00:00
|
|
|
strcmp(HS(a1)->hs_wd.wd_hostname, HS(a2)->hs_wd.wd_hostname));
|
2003-03-01 23:09:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Load average comparison. */
|
2011-11-06 08:16:41 +00:00
|
|
|
static int
|
2003-03-01 23:09:26 +00:00
|
|
|
lcmp(const void *a1, const void *a2)
|
|
|
|
{
|
|
|
|
if (ISDOWN(HS(a1)))
|
|
|
|
if (ISDOWN(HS(a2)))
|
|
|
|
return (tcmp(a1, a2));
|
|
|
|
else
|
|
|
|
return (rflg);
|
|
|
|
else if (ISDOWN(HS(a2)))
|
|
|
|
return (-rflg);
|
|
|
|
else
|
|
|
|
return (rflg *
|
2011-10-16 07:36:27 +00:00
|
|
|
(HS(a2)->hs_wd.wd_loadav[0] - HS(a1)->hs_wd.wd_loadav[0]));
|
2003-03-01 23:09:26 +00:00
|
|
|
}
|
|
|
|
|
2011-11-06 08:16:41 +00:00
|
|
|
static void
|
2003-03-01 23:09:26 +00:00
|
|
|
ruptime(const char *host, int aflg, int (*cmp)(const void *, const void *))
|
|
|
|
{
|
|
|
|
struct hs *hsp;
|
|
|
|
struct whod *wd;
|
|
|
|
struct whoent *we;
|
|
|
|
struct dirent *dp;
|
2017-03-04 04:06:33 +00:00
|
|
|
int fd, hostnamewidth, i, loadavwidth[3], userswidth, w;
|
2003-03-01 23:09:26 +00:00
|
|
|
size_t hspace;
|
2011-10-16 07:36:27 +00:00
|
|
|
ssize_t cc;
|
2003-03-01 23:09:26 +00:00
|
|
|
|
|
|
|
rewinddir(dirp);
|
|
|
|
hsp = NULL;
|
2017-03-04 04:06:33 +00:00
|
|
|
hostnamewidth = 0;
|
|
|
|
loadavwidth[0] = 4;
|
|
|
|
loadavwidth[1] = 4;
|
|
|
|
loadavwidth[2] = 4;
|
|
|
|
userswidth = 1;
|
2008-03-17 18:31:43 +00:00
|
|
|
(void)time(&now);
|
1994-05-27 12:33:43 +00:00
|
|
|
for (nhosts = hspace = 0; (dp = readdir(dirp)) != NULL;) {
|
2003-03-01 23:09:26 +00:00
|
|
|
if (dp->d_ino == 0 || strncmp(dp->d_name, "whod.", 5) != 0)
|
1994-05-27 12:33:43 +00:00
|
|
|
continue;
|
|
|
|
if ((fd = open(dp->d_name, O_RDONLY, 0)) < 0) {
|
|
|
|
warn("%s", dp->d_name);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nhosts == hspace) {
|
|
|
|
if ((hs =
|
|
|
|
realloc(hs, (hspace += 40) * sizeof(*hs))) == NULL)
|
|
|
|
err(1, NULL);
|
|
|
|
hsp = hs + nhosts;
|
|
|
|
}
|
|
|
|
|
2011-10-16 07:36:27 +00:00
|
|
|
wd = &hsp->hs_wd;
|
|
|
|
cc = read(fd, wd, sizeof(*wd));
|
|
|
|
(void)close(fd);
|
|
|
|
if (cc < (ssize_t)WHDRSIZE)
|
|
|
|
continue;
|
|
|
|
|
2017-03-04 04:06:33 +00:00
|
|
|
if (host != NULL && strcasecmp(wd->wd_hostname, host) != 0)
|
|
|
|
continue;
|
2011-10-16 07:36:27 +00:00
|
|
|
if (LEFTEARTH(wd->wd_recvtime))
|
|
|
|
continue;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2017-03-04 04:06:33 +00:00
|
|
|
if (hostnamewidth < (int)strlen(wd->wd_hostname))
|
|
|
|
hostnamewidth = (int)strlen(wd->wd_hostname);
|
2020-04-04 12:07:36 +00:00
|
|
|
|
|
|
|
if (!ISDOWN(hsp)) {
|
|
|
|
for (i = 0; i < 3; i++) {
|
|
|
|
w = iwidth(wd->wd_loadav[i] / 100) + 3;
|
|
|
|
if (loadavwidth[i] < w)
|
|
|
|
loadavwidth[i] = w;
|
|
|
|
}
|
|
|
|
for (hsp->hs_nusers = 0, we = &wd->wd_we[0];
|
|
|
|
(char *)(we + 1) <= (char *)wd + cc; we++)
|
|
|
|
if (aflg || we->we_idle < 3600)
|
|
|
|
++hsp->hs_nusers;
|
|
|
|
if (userswidth < iwidth(hsp->hs_nusers))
|
|
|
|
userswidth = iwidth(hsp->hs_nusers);
|
2017-03-04 04:06:33 +00:00
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
++hsp;
|
|
|
|
++nhosts;
|
|
|
|
}
|
2003-03-01 23:09:26 +00:00
|
|
|
if (nhosts == 0) {
|
|
|
|
if (host == NULL)
|
|
|
|
errx(1, "no hosts in %s", _PATH_RWHODIR);
|
|
|
|
else
|
|
|
|
warnx("host %s not in %s", host, _PATH_RWHODIR);
|
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
qsort(hs, nhosts, sizeof(hs[0]), cmp);
|
2017-03-04 04:06:33 +00:00
|
|
|
w = userswidth + loadavwidth[0] + loadavwidth[1] + loadavwidth[2];
|
|
|
|
if (hostnamewidth + w > 41)
|
|
|
|
hostnamewidth = 41 - w; /* limit to 79 cols */
|
2002-04-28 10:56:09 +00:00
|
|
|
for (i = 0; i < (int)nhosts; i++) {
|
1994-05-27 12:33:43 +00:00
|
|
|
hsp = &hs[i];
|
2011-10-16 07:36:27 +00:00
|
|
|
wd = &hsp->hs_wd;
|
1994-05-27 12:33:43 +00:00
|
|
|
if (ISDOWN(hsp)) {
|
2019-01-12 12:57:32 +00:00
|
|
|
(void)printf("%-*.*s %s\n",
|
2017-03-04 04:06:33 +00:00
|
|
|
hostnamewidth, hostnamewidth, wd->wd_hostname,
|
2011-10-16 07:36:27 +00:00
|
|
|
interval(now - hsp->hs_wd.wd_recvtime, "down"));
|
1994-05-27 12:33:43 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
(void)printf(
|
2017-03-04 04:06:33 +00:00
|
|
|
"%-*.*s %s, %*d user%s load %*.2f, %*.2f, %*.2f\n",
|
|
|
|
hostnamewidth, hostnamewidth, wd->wd_hostname,
|
2011-10-16 07:36:27 +00:00
|
|
|
interval((time_t)wd->wd_sendtime -
|
|
|
|
(time_t)wd->wd_boottime, " up"),
|
2017-03-04 04:06:33 +00:00
|
|
|
userswidth, hsp->hs_nusers,
|
1994-05-27 12:33:43 +00:00
|
|
|
hsp->hs_nusers == 1 ? ", " : "s,",
|
2017-03-04 04:06:33 +00:00
|
|
|
loadavwidth[0], wd->wd_loadav[0] / 100.0,
|
|
|
|
loadavwidth[1], wd->wd_loadav[1] / 100.0,
|
|
|
|
loadavwidth[2], wd->wd_loadav[2] / 100.0);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
2003-03-01 23:09:26 +00:00
|
|
|
free(hs);
|
|
|
|
hs = NULL;
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Number of users comparison. */
|
2011-11-06 08:16:41 +00:00
|
|
|
static int
|
2002-04-28 10:56:09 +00:00
|
|
|
ucmp(const void *a1, const void *a2)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
if (ISDOWN(HS(a1)))
|
|
|
|
if (ISDOWN(HS(a2)))
|
|
|
|
return (tcmp(a1, a2));
|
|
|
|
else
|
|
|
|
return (rflg);
|
|
|
|
else if (ISDOWN(HS(a2)))
|
|
|
|
return (-rflg);
|
|
|
|
else
|
|
|
|
return (rflg * (HS(a2)->hs_nusers - HS(a1)->hs_nusers));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Uptime comparison. */
|
2011-11-06 08:16:41 +00:00
|
|
|
static int
|
2002-04-28 10:56:09 +00:00
|
|
|
tcmp(const void *a1, const void *a2)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
return (rflg * (
|
2011-10-16 07:36:27 +00:00
|
|
|
(ISDOWN(HS(a2)) ? HS(a2)->hs_wd.wd_recvtime - now
|
|
|
|
: HS(a2)->hs_wd.wd_sendtime - HS(a2)->hs_wd.wd_boottime)
|
1994-05-27 12:33:43 +00:00
|
|
|
-
|
2011-10-16 07:36:27 +00:00
|
|
|
(ISDOWN(HS(a1)) ? HS(a1)->hs_wd.wd_recvtime - now
|
|
|
|
: HS(a1)->hs_wd.wd_sendtime - HS(a1)->hs_wd.wd_boottime)
|
1994-05-27 12:33:43 +00:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2011-11-06 08:16:41 +00:00
|
|
|
static void
|
2002-04-28 10:56:09 +00:00
|
|
|
usage(void)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2005-05-21 09:55:10 +00:00
|
|
|
(void)fprintf(stderr, "usage: ruptime [-alrtu] [host ...]\n");
|
1994-05-27 12:33:43 +00:00
|
|
|
exit(1);
|
|
|
|
}
|