xref: /csrg-svn/usr.sbin/rwhod/rwhod.c (revision 42265)
122549Sdist /*
235392Sbostic  * Copyright (c) 1983 The Regents of the University of California.
335392Sbostic  * All rights reserved.
435392Sbostic  *
535392Sbostic  * Redistribution and use in source and binary forms are permitted
635392Sbostic  * provided that the above copyright notice and this paragraph are
735392Sbostic  * duplicated in all such forms and that any documentation,
835392Sbostic  * advertising materials, and other materials related to such
935392Sbostic  * distribution and use acknowledge that the software was developed
1035392Sbostic  * by the University of California, Berkeley.  The name of the
1135392Sbostic  * University may not be used to endorse or promote products derived
1235392Sbostic  * from this software without specific prior written permission.
1335392Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1435392Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1535392Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1622549Sdist  */
1722549Sdist 
186460Swnj #ifndef lint
1922549Sdist char copyright[] =
2035392Sbostic "@(#) Copyright (c) 1983 The Regents of the University of California.\n\
2122549Sdist  All rights reserved.\n";
2235392Sbostic #endif /* not lint */
236460Swnj 
2422549Sdist #ifndef lint
25*42265Skarels static char sccsid[] = "@(#)rwhod.c	5.15 (Berkeley) 05/21/90";
2635392Sbostic #endif /* not lint */
2722549Sdist 
28*42265Skarels #include <sys/param.h>
299215Ssam #include <sys/socket.h>
309215Ssam #include <sys/stat.h>
3137295Sbostic #include <sys/signal.h>
329215Ssam #include <sys/ioctl.h>
3313187Ssam #include <sys/file.h>
349215Ssam 
3512236Ssam #include <net/if.h>
369215Ssam #include <netinet/in.h>
379215Ssam 
389215Ssam #include <nlist.h>
396460Swnj #include <errno.h>
406460Swnj #include <utmp.h>
418486Ssam #include <ctype.h>
428486Ssam #include <netdb.h>
4316673Sralph #include <syslog.h>
4423535Smckusick #include <protocols/rwhod.h>
4537295Sbostic #include <stdio.h>
4637971Sbostic #include <paths.h>
476460Swnj 
4815541Sralph /*
4915541Sralph  * Alarm interval. Don't forget to change the down time check in ruptime
5015541Sralph  * if this is changed.
5115541Sralph  */
5216673Sralph #define AL_INTERVAL (3 * 60)
5315541Sralph 
54*42265Skarels struct	sockaddr_in sin;
556460Swnj 
56*42265Skarels char	myname[MAXHOSTNAMELEN];
576460Swnj 
586460Swnj struct	nlist nl[] = {
5938182Smckusick #define	NL_BOOTTIME	0
609215Ssam 	{ "_boottime" },
616460Swnj 	0
626460Swnj };
636460Swnj 
6412236Ssam /*
6512236Ssam  * We communicate with each neighbor in
6612236Ssam  * a list constructed at the time we're
6712236Ssam  * started up.  Neighbors are currently
6812236Ssam  * directly connected via a hardware interface.
6912236Ssam  */
7012236Ssam struct	neighbor {
7112236Ssam 	struct	neighbor *n_next;
7212236Ssam 	char	*n_name;		/* interface name */
7312236Ssam 	char	*n_addr;		/* who to send to */
7412236Ssam 	int	n_addrlen;		/* size of address */
7512236Ssam 	int	n_flags;		/* should forward?, interface flags */
7612236Ssam };
7712236Ssam 
7812236Ssam struct	neighbor *neighbors;
796460Swnj struct	whod mywd;
8012236Ssam struct	servent *sp;
816460Swnj int	s, utmpf, kmemf = -1;
826460Swnj 
8312236Ssam #define	WHDRSIZE	(sizeof (mywd) - sizeof (mywd.wd_we))
8411834Ssam 
8537295Sbostic extern int errno;
866460Swnj int	onalrm();
8732449Sbostic char	*strcpy(), *malloc();
886460Swnj long	lseek();
896460Swnj int	getkmem();
9012236Ssam struct	in_addr inet_makeaddr();
916460Swnj 
926460Swnj main()
936460Swnj {
946460Swnj 	struct sockaddr_in from;
9526016Skarels 	struct stat st;
966460Swnj 	char path[64];
9726484Smckusick 	int on = 1;
9837295Sbostic 	char *cp, *index(), *strerror();
996460Swnj 
10016673Sralph 	if (getuid()) {
10116673Sralph 		fprintf(stderr, "rwhod: not super user\n");
10216673Sralph 		exit(1);
10316673Sralph 	}
1048486Ssam 	sp = getservbyname("who", "udp");
1058486Ssam 	if (sp == 0) {
1068486Ssam 		fprintf(stderr, "rwhod: udp/who: unknown service\n");
1078486Ssam 		exit(1);
1088486Ssam 	}
1096460Swnj #ifndef DEBUG
1106460Swnj 	if (fork())
1116460Swnj 		exit(0);
1126460Swnj 	{ int s;
1136460Swnj 	  for (s = 0; s < 10; s++)
1146460Swnj 		(void) close(s);
1156460Swnj 	  (void) open("/", 0);
1166460Swnj 	  (void) dup2(0, 1);
1176460Swnj 	  (void) dup2(0, 2);
11837971Sbostic 	  s = open(_PATH_TTY, 2);
1196460Swnj 	  if (s >= 0) {
1206460Swnj 		ioctl(s, TIOCNOTTY, 0);
1216460Swnj 		(void) close(s);
1226460Swnj 	  }
1236460Swnj 	}
1246460Swnj #endif
12537295Sbostic 	if (chdir(_PATH_RWHODIR) < 0) {
12637295Sbostic 		(void)fprintf(stderr, "rwhod: %s: %s\n",
12737295Sbostic 		    _PATH_RWHODIR, strerror(errno));
12826011Smckusick 		exit(1);
12926011Smckusick 	}
1306460Swnj 	(void) signal(SIGHUP, getkmem);
13124853Seric 	openlog("rwhod", LOG_PID, LOG_DAEMON);
13212236Ssam 	/*
13312236Ssam 	 * Establish host name as returned by system.
13412236Ssam 	 */
13512236Ssam 	if (gethostname(myname, sizeof (myname) - 1) < 0) {
13616673Sralph 		syslog(LOG_ERR, "gethostname: %m");
1376460Swnj 		exit(1);
1386460Swnj 	}
13924745Sbloom 	if ((cp = index(myname, '.')) != NULL)
14024745Sbloom 		*cp = '\0';
14112236Ssam 	strncpy(mywd.wd_hostname, myname, sizeof (myname) - 1);
14237295Sbostic 	utmpf = open(_PATH_UTMP, O_RDONLY|O_CREAT, 0644);
1436460Swnj 	if (utmpf < 0) {
14437295Sbostic 		syslog(LOG_ERR, "%s: %m", _PATH_UTMP);
1456460Swnj 		exit(1);
1466460Swnj 	}
1476460Swnj 	getkmem();
14813187Ssam 	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
14916673Sralph 		syslog(LOG_ERR, "socket: %m");
1509215Ssam 		exit(1);
1516460Swnj 	}
15217155Ssam 	if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0) {
15317055Skarels 		syslog(LOG_ERR, "setsockopt SO_BROADCAST: %m");
15417055Skarels 		exit(1);
15517055Skarels 	}
156*42265Skarels 	sin.sin_family = AF_INET;
15712236Ssam 	sin.sin_port = sp->s_port;
15813187Ssam 	if (bind(s, &sin, sizeof (sin)) < 0) {
15916673Sralph 		syslog(LOG_ERR, "bind: %m");
1609215Ssam 		exit(1);
1619215Ssam 	}
16212236Ssam 	if (!configure(s))
16312236Ssam 		exit(1);
16413027Ssam 	signal(SIGALRM, onalrm);
1656460Swnj 	onalrm();
1666460Swnj 	for (;;) {
1676460Swnj 		struct whod wd;
16812236Ssam 		int cc, whod, len = sizeof (from);
1696460Swnj 
17012236Ssam 		cc = recvfrom(s, (char *)&wd, sizeof (struct whod), 0,
17112236Ssam 			&from, &len);
1726460Swnj 		if (cc <= 0) {
1736460Swnj 			if (cc < 0 && errno != EINTR)
17416673Sralph 				syslog(LOG_WARNING, "recv: %m");
1756460Swnj 			continue;
1766460Swnj 		}
1778486Ssam 		if (from.sin_port != sp->s_port) {
17816673Sralph 			syslog(LOG_WARNING, "%d: bad from port",
1798486Ssam 				ntohs(from.sin_port));
1806460Swnj 			continue;
1816460Swnj 		}
18212347Ssam 		if (wd.wd_vers != WHODVERSION)
18312347Ssam 			continue;
18412236Ssam 		if (wd.wd_type != WHODTYPE_STATUS)
18512236Ssam 			continue;
1868486Ssam 		if (!verify(wd.wd_hostname)) {
18716673Sralph 			syslog(LOG_WARNING, "malformed host name from %x",
1888486Ssam 				from.sin_addr);
1898486Ssam 			continue;
1908486Ssam 		}
19126011Smckusick 		(void) sprintf(path, "whod.%s", wd.wd_hostname);
19226016Skarels 		/*
19326016Skarels 		 * Rather than truncating and growing the file each time,
19426016Skarels 		 * use ftruncate if size is less than previous size.
19526016Skarels 		 */
19626016Skarels 		whod = open(path, O_WRONLY | O_CREAT, 0644);
1976460Swnj 		if (whod < 0) {
19816673Sralph 			syslog(LOG_WARNING, "%s: %m", path);
1996460Swnj 			continue;
2006460Swnj 		}
201*42265Skarels #if ENDIAN != BIG_ENDIAN
20211834Ssam 		{
20313187Ssam 			int i, n = (cc - WHDRSIZE)/sizeof(struct whoent);
20411834Ssam 			struct whoent *we;
20511834Ssam 
20611834Ssam 			/* undo header byte swapping before writing to file */
20711834Ssam 			wd.wd_sendtime = ntohl(wd.wd_sendtime);
20811834Ssam 			for (i = 0; i < 3; i++)
20911834Ssam 				wd.wd_loadav[i] = ntohl(wd.wd_loadav[i]);
21011834Ssam 			wd.wd_boottime = ntohl(wd.wd_boottime);
21111834Ssam 			we = wd.wd_we;
21211834Ssam 			for (i = 0; i < n; i++) {
21311834Ssam 				we->we_idle = ntohl(we->we_idle);
21412872Ssam 				we->we_utmp.out_time =
21512872Ssam 				    ntohl(we->we_utmp.out_time);
21611834Ssam 				we++;
21711834Ssam 			}
21811834Ssam 		}
21911834Ssam #endif
2206460Swnj 		(void) time(&wd.wd_recvtime);
2216460Swnj 		(void) write(whod, (char *)&wd, cc);
22226016Skarels 		if (fstat(whod, &st) < 0 || st.st_size > cc)
22326016Skarels 			ftruncate(whod, cc);
2246460Swnj 		(void) close(whod);
2256460Swnj 	}
2266460Swnj }
2276460Swnj 
2288486Ssam /*
2298486Ssam  * Check out host name for unprintables
2308486Ssam  * and other funnies before allowing a file
2318486Ssam  * to be created.  Sorry, but blanks aren't allowed.
2328486Ssam  */
2338486Ssam verify(name)
2348486Ssam 	register char *name;
2358486Ssam {
2368486Ssam 	register int size = 0;
2378486Ssam 
2388486Ssam 	while (*name) {
23915214Sleres 		if (!isascii(*name) || !(isalnum(*name) || ispunct(*name)))
2408486Ssam 			return (0);
2418486Ssam 		name++, size++;
2428486Ssam 	}
2438486Ssam 	return (size > 0);
2448486Ssam }
2458486Ssam 
2466460Swnj int	utmptime;
2476460Swnj int	utmpent;
24822546Sbloom int	utmpsize = 0;
24922546Sbloom struct	utmp *utmp;
2506460Swnj int	alarmcount;
2516460Swnj 
2526460Swnj onalrm()
2536460Swnj {
25437295Sbostic 	register struct neighbor *np;
25537295Sbostic 	register struct whoent *we = mywd.wd_we, *wlast;
2566460Swnj 	register int i;
2576460Swnj 	struct stat stb;
2586460Swnj 	int cc;
2596460Swnj 	double avenrun[3];
26037295Sbostic 	time_t now = time((time_t *)NULL);
26137295Sbostic 	char *strerror();
2626460Swnj 
2636460Swnj 	if (alarmcount % 10 == 0)
2646460Swnj 		getkmem();
2656460Swnj 	alarmcount++;
2666460Swnj 	(void) fstat(utmpf, &stb);
26722546Sbloom 	if ((stb.st_mtime != utmptime) || (stb.st_size > utmpsize)) {
26817303Stef 		utmptime = stb.st_mtime;
26922546Sbloom 		if (stb.st_size > utmpsize) {
27022546Sbloom 			utmpsize = stb.st_size + 10 * sizeof(struct utmp);
27122546Sbloom 			if (utmp)
27222546Sbloom 				utmp = (struct utmp *)realloc(utmp, utmpsize);
27322546Sbloom 			else
27422546Sbloom 				utmp = (struct utmp *)malloc(utmpsize);
27522546Sbloom 			if (! utmp) {
27622546Sbloom 				fprintf(stderr, "rwhod: malloc failed\n");
27722546Sbloom 				utmpsize = 0;
27822546Sbloom 				goto done;
27922546Sbloom 			}
28022546Sbloom 		}
28113187Ssam 		(void) lseek(utmpf, (long)0, L_SET);
28222546Sbloom 		cc = read(utmpf, (char *)utmp, stb.st_size);
2836460Swnj 		if (cc < 0) {
28437295Sbostic 			fprintf(stderr, "rwhod: %s: %s\n",
28537295Sbostic 			    _PATH_UTMP, strerror(errno));
28613469Ssam 			goto done;
2876460Swnj 		}
28812236Ssam 		wlast = &mywd.wd_we[1024 / sizeof (struct whoent) - 1];
2896460Swnj 		utmpent = cc / sizeof (struct utmp);
2906460Swnj 		for (i = 0; i < utmpent; i++)
2916460Swnj 			if (utmp[i].ut_name[0]) {
29212807Ssam 				bcopy(utmp[i].ut_line, we->we_utmp.out_line,
29312807Ssam 				   sizeof (utmp[i].ut_line));
29412807Ssam 				bcopy(utmp[i].ut_name, we->we_utmp.out_name,
29512807Ssam 				   sizeof (utmp[i].ut_name));
29612807Ssam 				we->we_utmp.out_time = htonl(utmp[i].ut_time);
2976577Ssam 				if (we >= wlast)
2986577Ssam 					break;
2996460Swnj 				we++;
3006460Swnj 			}
3016460Swnj 		utmpent = we - mywd.wd_we;
3026460Swnj 	}
30326484Smckusick 
30426484Smckusick 	/*
30526484Smckusick 	 * The test on utmpent looks silly---after all, if no one is
30626484Smckusick 	 * logged on, why worry about efficiency?---but is useful on
30726484Smckusick 	 * (e.g.) compute servers.
30826484Smckusick 	 */
30937971Sbostic 	if (utmpent && chdir(_PATH_DEV)) {
31037971Sbostic 		syslog(LOG_ERR, "chdir(%s): %m", _PATH_DEV);
31126484Smckusick 		exit(1);
31226484Smckusick 	}
3136460Swnj 	we = mywd.wd_we;
3146460Swnj 	for (i = 0; i < utmpent; i++) {
31526484Smckusick 		if (stat(we->we_utmp.out_line, &stb) >= 0)
31612807Ssam 			we->we_idle = htonl(now - stb.st_atime);
3176460Swnj 		we++;
3186460Swnj 	}
31938182Smckusick 	(void) getloadavg(avenrun, sizeof(avenrun)/sizeof(avenrun[0]));
3206460Swnj 	for (i = 0; i < 3; i++)
32112873Ssam 		mywd.wd_loadav[i] = htonl((u_long)(avenrun[i] * 100));
3226460Swnj 	cc = (char *)we - (char *)&mywd;
32312807Ssam 	mywd.wd_sendtime = htonl(time(0));
32412236Ssam 	mywd.wd_vers = WHODVERSION;
32512236Ssam 	mywd.wd_type = WHODTYPE_STATUS;
32612236Ssam 	for (np = neighbors; np != NULL; np = np->n_next)
32712236Ssam 		(void) sendto(s, (char *)&mywd, cc, 0,
32812236Ssam 			np->n_addr, np->n_addrlen);
32937295Sbostic 	if (utmpent && chdir(_PATH_RWHODIR)) {
33037295Sbostic 		syslog(LOG_ERR, "chdir(%s): %m", _PATH_RWHODIR);
33126484Smckusick 		exit(1);
33226484Smckusick 	}
33313469Ssam done:
33415541Sralph 	(void) alarm(AL_INTERVAL);
3356460Swnj }
3366460Swnj 
3376460Swnj getkmem()
3386460Swnj {
33916663Ssam 	static ino_t vmunixino;
34016663Ssam 	static time_t vmunixctime;
34116663Ssam 	struct stat sb;
3426460Swnj 
34337295Sbostic 	if (stat(_PATH_UNIX, &sb) < 0) {
34416663Ssam 		if (vmunixctime)
34516663Ssam 			return;
34616663Ssam 	} else {
34716663Ssam 		if (sb.st_ctime == vmunixctime && sb.st_ino == vmunixino)
34816663Ssam 			return;
34916663Ssam 		vmunixctime = sb.st_ctime;
35016663Ssam 		vmunixino= sb.st_ino;
35116663Ssam 	}
3526460Swnj 	if (kmemf >= 0)
3536460Swnj 		(void) close(kmemf);
3546460Swnj loop:
35537295Sbostic 	if (nlist(_PATH_UNIX, nl)) {
35637295Sbostic 		syslog(LOG_WARNING, "%s: namelist botch", _PATH_UNIX);
3576460Swnj 		sleep(300);
3586460Swnj 		goto loop;
3596460Swnj 	}
36037295Sbostic 	kmemf = open(_PATH_KMEM, O_RDONLY, 0);
3616460Swnj 	if (kmemf < 0) {
36237295Sbostic 		syslog(LOG_ERR, "%s: %m", _PATH_KMEM);
36316673Sralph 		exit(1);
3646460Swnj 	}
36513187Ssam 	(void) lseek(kmemf, (long)nl[NL_BOOTTIME].n_value, L_SET);
36613187Ssam 	(void) read(kmemf, (char *)&mywd.wd_boottime,
36713187Ssam 	    sizeof (mywd.wd_boottime));
36812807Ssam 	mywd.wd_boottime = htonl(mywd.wd_boottime);
3696460Swnj }
37012236Ssam 
37112236Ssam /*
37212236Ssam  * Figure out device configuration and select
37312236Ssam  * networks which deserve status information.
37412236Ssam  */
37512236Ssam configure(s)
37612236Ssam 	int s;
37712236Ssam {
37812236Ssam 	char buf[BUFSIZ];
37912236Ssam 	struct ifconf ifc;
38012236Ssam 	struct ifreq ifreq, *ifr;
38112236Ssam 	struct sockaddr_in *sin;
38212236Ssam 	register struct neighbor *np;
38313187Ssam 	int n;
38412236Ssam 
38512236Ssam 	ifc.ifc_len = sizeof (buf);
38612236Ssam 	ifc.ifc_buf = buf;
38712236Ssam 	if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0) {
38816673Sralph 		syslog(LOG_ERR, "ioctl (get interface configuration)");
38912236Ssam 		return (0);
39012236Ssam 	}
39112236Ssam 	ifr = ifc.ifc_req;
39212236Ssam 	for (n = ifc.ifc_len / sizeof (struct ifreq); n > 0; n--, ifr++) {
39312236Ssam 		for (np = neighbors; np != NULL; np = np->n_next)
39412236Ssam 			if (np->n_name &&
39512236Ssam 			    strcmp(ifr->ifr_name, np->n_name) == 0)
39612236Ssam 				break;
39712236Ssam 		if (np != NULL)
39812236Ssam 			continue;
39912236Ssam 		ifreq = *ifr;
40012236Ssam 		np = (struct neighbor *)malloc(sizeof (*np));
40112236Ssam 		if (np == NULL)
40212236Ssam 			continue;
40312236Ssam 		np->n_name = malloc(strlen(ifr->ifr_name) + 1);
40412236Ssam 		if (np->n_name == NULL) {
40512236Ssam 			free((char *)np);
40612236Ssam 			continue;
40712236Ssam 		}
40812236Ssam 		strcpy(np->n_name, ifr->ifr_name);
40912236Ssam 		np->n_addrlen = sizeof (ifr->ifr_addr);
41012236Ssam 		np->n_addr = malloc(np->n_addrlen);
41112236Ssam 		if (np->n_addr == NULL) {
41212236Ssam 			free(np->n_name);
41312236Ssam 			free((char *)np);
41412236Ssam 			continue;
41512236Ssam 		}
41612236Ssam 		bcopy((char *)&ifr->ifr_addr, np->n_addr, np->n_addrlen);
41712236Ssam 		if (ioctl(s, SIOCGIFFLAGS, (char *)&ifreq) < 0) {
41816673Sralph 			syslog(LOG_ERR, "ioctl (get interface flags)");
41912236Ssam 			free((char *)np);
42012236Ssam 			continue;
42112236Ssam 		}
42221852Skarels 		if ((ifreq.ifr_flags & IFF_UP) == 0 ||
42321852Skarels 		    (ifreq.ifr_flags & (IFF_BROADCAST|IFF_POINTOPOINT)) == 0) {
42412236Ssam 			free((char *)np);
42512236Ssam 			continue;
42612236Ssam 		}
42712236Ssam 		np->n_flags = ifreq.ifr_flags;
42812236Ssam 		if (np->n_flags & IFF_POINTOPOINT) {
42912236Ssam 			if (ioctl(s, SIOCGIFDSTADDR, (char *)&ifreq) < 0) {
43016673Sralph 				syslog(LOG_ERR, "ioctl (get dstaddr)");
43112236Ssam 				free((char *)np);
43212236Ssam 				continue;
43312236Ssam 			}
43412236Ssam 			/* we assume addresses are all the same size */
43512236Ssam 			bcopy((char *)&ifreq.ifr_dstaddr,
43612236Ssam 			  np->n_addr, np->n_addrlen);
43712236Ssam 		}
43812236Ssam 		if (np->n_flags & IFF_BROADCAST) {
43921852Skarels 			if (ioctl(s, SIOCGIFBRDADDR, (char *)&ifreq) < 0) {
44021852Skarels 				syslog(LOG_ERR, "ioctl (get broadaddr)");
44121852Skarels 				free((char *)np);
44221852Skarels 				continue;
44321852Skarels 			}
44412236Ssam 			/* we assume addresses are all the same size */
44521852Skarels 			bcopy((char *)&ifreq.ifr_broadaddr,
44621852Skarels 			  np->n_addr, np->n_addrlen);
44712236Ssam 		}
44812236Ssam 		/* gag, wish we could get rid of Internet dependencies */
44912236Ssam 		sin = (struct sockaddr_in *)np->n_addr;
45012236Ssam 		sin->sin_port = sp->s_port;
45112236Ssam 		np->n_next = neighbors;
45212236Ssam 		neighbors = np;
45312236Ssam 	}
45412236Ssam 	return (1);
45512236Ssam }
45612807Ssam 
45712807Ssam #ifdef DEBUG
45812807Ssam sendto(s, buf, cc, flags, to, tolen)
45912807Ssam 	int s;
46012807Ssam 	char *buf;
46112807Ssam 	int cc, flags;
46212807Ssam 	char *to;
46312807Ssam 	int tolen;
46412807Ssam {
46512807Ssam 	register struct whod *w = (struct whod *)buf;
46612807Ssam 	register struct whoent *we;
46712807Ssam 	struct sockaddr_in *sin = (struct sockaddr_in *)to;
46812807Ssam 	char *interval();
46912807Ssam 
47012807Ssam 	printf("sendto %x.%d\n", ntohl(sin->sin_addr), ntohs(sin->sin_port));
47112807Ssam 	printf("hostname %s %s\n", w->wd_hostname,
47213187Ssam 	   interval(ntohl(w->wd_sendtime) - ntohl(w->wd_boottime), "  up"));
47312807Ssam 	printf("load %4.2f, %4.2f, %4.2f\n",
47413187Ssam 	    ntohl(w->wd_loadav[0]) / 100.0, ntohl(w->wd_loadav[1]) / 100.0,
47513187Ssam 	    ntohl(w->wd_loadav[2]) / 100.0);
47612807Ssam 	cc -= WHDRSIZE;
47712807Ssam 	for (we = w->wd_we, cc /= sizeof (struct whoent); cc > 0; cc--, we++) {
47813187Ssam 		time_t t = ntohl(we->we_utmp.out_time);
47912807Ssam 		printf("%-8.8s %s:%s %.12s",
48013187Ssam 			we->we_utmp.out_name,
48113187Ssam 			w->wd_hostname, we->we_utmp.out_line,
48213187Ssam 			ctime(&t)+4);
48313187Ssam 		we->we_idle = ntohl(we->we_idle) / 60;
48412807Ssam 		if (we->we_idle) {
48512807Ssam 			if (we->we_idle >= 100*60)
48612807Ssam 				we->we_idle = 100*60 - 1;
48712807Ssam 			if (we->we_idle >= 60)
48812807Ssam 				printf(" %2d", we->we_idle / 60);
48912807Ssam 			else
49012807Ssam 				printf("   ");
49112807Ssam 			printf(":%02d", we->we_idle % 60);
49212807Ssam 		}
49312807Ssam 		printf("\n");
49412807Ssam 	}
49512807Ssam }
49612807Ssam 
49712807Ssam char *
49812807Ssam interval(time, updown)
49912807Ssam 	int time;
50012807Ssam 	char *updown;
50112807Ssam {
50212807Ssam 	static char resbuf[32];
50312807Ssam 	int days, hours, minutes;
50412807Ssam 
50512807Ssam 	if (time < 0 || time > 3*30*24*60*60) {
50612807Ssam 		(void) sprintf(resbuf, "   %s ??:??", updown);
50712807Ssam 		return (resbuf);
50812807Ssam 	}
50912807Ssam 	minutes = (time + 59) / 60;		/* round to minutes */
51012807Ssam 	hours = minutes / 60; minutes %= 60;
51112807Ssam 	days = hours / 24; hours %= 24;
51212807Ssam 	if (days)
51312807Ssam 		(void) sprintf(resbuf, "%s %2d+%02d:%02d",
51412807Ssam 		    updown, days, hours, minutes);
51512807Ssam 	else
51612807Ssam 		(void) sprintf(resbuf, "%s    %2d:%02d",
51712807Ssam 		    updown, hours, minutes);
51812807Ssam 	return (resbuf);
51912807Ssam }
52012807Ssam #endif
521