xref: /csrg-svn/usr.sbin/rwhod/rwhod.c (revision 22549)
1*22549Sdist /*
2*22549Sdist  * Copyright (c) 1983 Regents of the University of California.
3*22549Sdist  * All rights reserved.  The Berkeley software License Agreement
4*22549Sdist  * specifies the terms and conditions for redistribution.
5*22549Sdist  */
6*22549Sdist 
76460Swnj #ifndef lint
8*22549Sdist char copyright[] =
9*22549Sdist "@(#) Copyright (c) 1983 Regents of the University of California.\n\
10*22549Sdist  All rights reserved.\n";
11*22549Sdist #endif not lint
126460Swnj 
13*22549Sdist #ifndef lint
14*22549Sdist static char sccsid[] = "@(#)rwhod.c	5.1 (Berkeley) 06/06/85";
15*22549Sdist #endif not lint
16*22549Sdist 
179215Ssam #include <sys/types.h>
189215Ssam #include <sys/socket.h>
199215Ssam #include <sys/stat.h>
209215Ssam #include <sys/ioctl.h>
2113187Ssam #include <sys/file.h>
229215Ssam 
2312236Ssam #include <net/if.h>
249215Ssam #include <netinet/in.h>
259215Ssam 
269215Ssam #include <nlist.h>
276460Swnj #include <stdio.h>
286460Swnj #include <signal.h>
296460Swnj #include <errno.h>
306460Swnj #include <utmp.h>
318486Ssam #include <ctype.h>
328486Ssam #include <netdb.h>
3316673Sralph #include <syslog.h>
3413567Ssam #include "rwhod.h"
356460Swnj 
3615541Sralph /*
3715541Sralph  * Alarm interval. Don't forget to change the down time check in ruptime
3815541Sralph  * if this is changed.
3915541Sralph  */
4016673Sralph #define AL_INTERVAL (3 * 60)
4115541Sralph 
428486Ssam struct	sockaddr_in sin = { AF_INET };
436460Swnj 
446460Swnj extern	errno;
456460Swnj 
4612236Ssam char	myname[32];
476460Swnj 
486460Swnj struct	nlist nl[] = {
496460Swnj #define	NL_AVENRUN	0
506460Swnj 	{ "_avenrun" },
519215Ssam #define	NL_BOOTTIME	1
529215Ssam 	{ "_boottime" },
536460Swnj 	0
546460Swnj };
556460Swnj 
5612236Ssam /*
5712236Ssam  * We communicate with each neighbor in
5812236Ssam  * a list constructed at the time we're
5912236Ssam  * started up.  Neighbors are currently
6012236Ssam  * directly connected via a hardware interface.
6112236Ssam  */
6212236Ssam struct	neighbor {
6312236Ssam 	struct	neighbor *n_next;
6412236Ssam 	char	*n_name;		/* interface name */
6512236Ssam 	char	*n_addr;		/* who to send to */
6612236Ssam 	int	n_addrlen;		/* size of address */
6712236Ssam 	int	n_flags;		/* should forward?, interface flags */
6812236Ssam };
6912236Ssam 
7012236Ssam struct	neighbor *neighbors;
716460Swnj struct	whod mywd;
7212236Ssam struct	servent *sp;
736460Swnj int	s, utmpf, kmemf = -1;
746460Swnj 
7512236Ssam #define	WHDRSIZE	(sizeof (mywd) - sizeof (mywd.wd_we))
7612236Ssam #define	RWHODIR		"/usr/spool/rwho"
7711834Ssam 
786460Swnj int	onalrm();
7912236Ssam char	*strcpy(), *sprintf(), *malloc();
806460Swnj long	lseek();
816460Swnj int	getkmem();
8212236Ssam struct	in_addr inet_makeaddr();
836460Swnj 
846460Swnj main()
856460Swnj {
866460Swnj 	struct sockaddr_in from;
876460Swnj 	char path[64];
8817155Ssam 	int addr, on = 1;
8912236Ssam 	struct hostent *hp;
906460Swnj 
9116673Sralph 	if (getuid()) {
9216673Sralph 		fprintf(stderr, "rwhod: not super user\n");
9316673Sralph 		exit(1);
9416673Sralph 	}
958486Ssam 	sp = getservbyname("who", "udp");
968486Ssam 	if (sp == 0) {
978486Ssam 		fprintf(stderr, "rwhod: udp/who: unknown service\n");
988486Ssam 		exit(1);
998486Ssam 	}
1006460Swnj #ifndef DEBUG
1016460Swnj 	if (fork())
1026460Swnj 		exit(0);
1036460Swnj 	{ int s;
1046460Swnj 	  for (s = 0; s < 10; s++)
1056460Swnj 		(void) close(s);
1066460Swnj 	  (void) open("/", 0);
1076460Swnj 	  (void) dup2(0, 1);
1086460Swnj 	  (void) dup2(0, 2);
1096460Swnj 	  s = open("/dev/tty", 2);
1106460Swnj 	  if (s >= 0) {
1116460Swnj 		ioctl(s, TIOCNOTTY, 0);
1126460Swnj 		(void) close(s);
1136460Swnj 	  }
1146460Swnj 	}
1156460Swnj #endif
1166460Swnj 	(void) chdir("/dev");
1176460Swnj 	(void) signal(SIGHUP, getkmem);
11816673Sralph 	openlog("rwhod", LOG_PID, 0);
11912236Ssam 	/*
12012236Ssam 	 * Establish host name as returned by system.
12112236Ssam 	 */
12212236Ssam 	if (gethostname(myname, sizeof (myname) - 1) < 0) {
12316673Sralph 		syslog(LOG_ERR, "gethostname: %m");
1246460Swnj 		exit(1);
1256460Swnj 	}
12612236Ssam 	strncpy(mywd.wd_hostname, myname, sizeof (myname) - 1);
12713187Ssam 	utmpf = open("/etc/utmp", O_RDONLY);
1286460Swnj 	if (utmpf < 0) {
1296460Swnj 		(void) close(creat("/etc/utmp", 0644));
13013187Ssam 		utmpf = open("/etc/utmp", O_RDONLY);
1316460Swnj 	}
1326460Swnj 	if (utmpf < 0) {
13316673Sralph 		syslog(LOG_ERR, "/etc/utmp: %m");
1346460Swnj 		exit(1);
1356460Swnj 	}
1366460Swnj 	getkmem();
13713187Ssam 	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
13816673Sralph 		syslog(LOG_ERR, "socket: %m");
1399215Ssam 		exit(1);
1406460Swnj 	}
14117155Ssam 	if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0) {
14217055Skarels 		syslog(LOG_ERR, "setsockopt SO_BROADCAST: %m");
14317055Skarels 		exit(1);
14417055Skarels 	}
14512236Ssam 	hp = gethostbyname(myname);
14612236Ssam 	if (hp == NULL) {
14716673Sralph 		syslog(LOG_ERR, "%s: don't know my own name", myname);
14812236Ssam 		exit(1);
14912236Ssam 	}
15012236Ssam 	sin.sin_family = hp->h_addrtype;
15112236Ssam 	sin.sin_port = sp->s_port;
15213187Ssam 	if (bind(s, &sin, sizeof (sin)) < 0) {
15316673Sralph 		syslog(LOG_ERR, "bind: %m");
1549215Ssam 		exit(1);
1559215Ssam 	}
15612236Ssam 	if (!configure(s))
15712236Ssam 		exit(1);
15813027Ssam 	signal(SIGALRM, onalrm);
1596460Swnj 	onalrm();
1606460Swnj 	for (;;) {
1616460Swnj 		struct whod wd;
16212236Ssam 		int cc, whod, len = sizeof (from);
1636460Swnj 
16412236Ssam 		cc = recvfrom(s, (char *)&wd, sizeof (struct whod), 0,
16512236Ssam 			&from, &len);
1666460Swnj 		if (cc <= 0) {
1676460Swnj 			if (cc < 0 && errno != EINTR)
16816673Sralph 				syslog(LOG_WARNING, "recv: %m");
1696460Swnj 			continue;
1706460Swnj 		}
1718486Ssam 		if (from.sin_port != sp->s_port) {
17216673Sralph 			syslog(LOG_WARNING, "%d: bad from port",
1738486Ssam 				ntohs(from.sin_port));
1746460Swnj 			continue;
1756460Swnj 		}
1768486Ssam #ifdef notdef
1778486Ssam 		if (gethostbyname(wd.wd_hostname) == 0) {
17816673Sralph 			syslog(LOG_WARNING, "%s: unknown host",
1798486Ssam 				wd.wd_hostname);
1806460Swnj 			continue;
1816460Swnj 		}
1828486Ssam #endif
18312347Ssam 		if (wd.wd_vers != WHODVERSION)
18412347Ssam 			continue;
18512236Ssam 		if (wd.wd_type != WHODTYPE_STATUS)
18612236Ssam 			continue;
1878486Ssam 		if (!verify(wd.wd_hostname)) {
18816673Sralph 			syslog(LOG_WARNING, "malformed host name from %x",
1898486Ssam 				from.sin_addr);
1908486Ssam 			continue;
1918486Ssam 		}
1929914Ssam 		(void) sprintf(path, "%s/whod.%s", RWHODIR, wd.wd_hostname);
1936460Swnj 		whod = creat(path, 0666);
1946460Swnj 		if (whod < 0) {
19516673Sralph 			syslog(LOG_WARNING, "%s: %m", path);
1966460Swnj 			continue;
1976460Swnj 		}
19811834Ssam #if vax || pdp11
19911834Ssam 		{
20013187Ssam 			int i, n = (cc - WHDRSIZE)/sizeof(struct whoent);
20111834Ssam 			struct whoent *we;
20211834Ssam 
20311834Ssam 			/* undo header byte swapping before writing to file */
20411834Ssam 			wd.wd_sendtime = ntohl(wd.wd_sendtime);
20511834Ssam 			for (i = 0; i < 3; i++)
20611834Ssam 				wd.wd_loadav[i] = ntohl(wd.wd_loadav[i]);
20711834Ssam 			wd.wd_boottime = ntohl(wd.wd_boottime);
20811834Ssam 			we = wd.wd_we;
20911834Ssam 			for (i = 0; i < n; i++) {
21011834Ssam 				we->we_idle = ntohl(we->we_idle);
21112872Ssam 				we->we_utmp.out_time =
21212872Ssam 				    ntohl(we->we_utmp.out_time);
21311834Ssam 				we++;
21411834Ssam 			}
21511834Ssam 		}
21611834Ssam #endif
2176460Swnj 		(void) time(&wd.wd_recvtime);
2186460Swnj 		(void) write(whod, (char *)&wd, cc);
2196460Swnj 		(void) close(whod);
2206460Swnj 	}
2216460Swnj }
2226460Swnj 
2238486Ssam /*
2248486Ssam  * Check out host name for unprintables
2258486Ssam  * and other funnies before allowing a file
2268486Ssam  * to be created.  Sorry, but blanks aren't allowed.
2278486Ssam  */
2288486Ssam verify(name)
2298486Ssam 	register char *name;
2308486Ssam {
2318486Ssam 	register int size = 0;
2328486Ssam 
2338486Ssam 	while (*name) {
23415214Sleres 		if (!isascii(*name) || !(isalnum(*name) || ispunct(*name)))
2358486Ssam 			return (0);
2368486Ssam 		name++, size++;
2378486Ssam 	}
2388486Ssam 	return (size > 0);
2398486Ssam }
2408486Ssam 
2416460Swnj int	utmptime;
2426460Swnj int	utmpent;
24322546Sbloom int	utmpsize = 0;
24422546Sbloom struct	utmp *utmp;
2456460Swnj int	alarmcount;
2466460Swnj 
2476460Swnj onalrm()
2486460Swnj {
2496460Swnj 	register int i;
2506460Swnj 	struct stat stb;
2516577Ssam 	register struct whoent *we = mywd.wd_we, *wlast;
2526460Swnj 	int cc;
2536460Swnj 	double avenrun[3];
2546460Swnj 	time_t now = time(0);
25512236Ssam 	register struct neighbor *np;
2566460Swnj 
2576460Swnj 	if (alarmcount % 10 == 0)
2586460Swnj 		getkmem();
2596460Swnj 	alarmcount++;
2606460Swnj 	(void) fstat(utmpf, &stb);
26122546Sbloom 	if ((stb.st_mtime != utmptime) || (stb.st_size > utmpsize)) {
26217303Stef 		utmptime = stb.st_mtime;
26322546Sbloom 		if (stb.st_size > utmpsize) {
26422546Sbloom 			utmpsize = stb.st_size + 10 * sizeof(struct utmp);
26522546Sbloom 			if (utmp)
26622546Sbloom 				utmp = (struct utmp *)realloc(utmp, utmpsize);
26722546Sbloom 			else
26822546Sbloom 				utmp = (struct utmp *)malloc(utmpsize);
26922546Sbloom 			if (! utmp) {
27022546Sbloom 				fprintf(stderr, "rwhod: malloc failed\n");
27122546Sbloom 				utmpsize = 0;
27222546Sbloom 				goto done;
27322546Sbloom 			}
27422546Sbloom 		}
27513187Ssam 		(void) lseek(utmpf, (long)0, L_SET);
27622546Sbloom 		cc = read(utmpf, (char *)utmp, stb.st_size);
2776460Swnj 		if (cc < 0) {
2786460Swnj 			perror("/etc/utmp");
27913469Ssam 			goto done;
2806460Swnj 		}
28112236Ssam 		wlast = &mywd.wd_we[1024 / sizeof (struct whoent) - 1];
2826460Swnj 		utmpent = cc / sizeof (struct utmp);
2836460Swnj 		for (i = 0; i < utmpent; i++)
2846460Swnj 			if (utmp[i].ut_name[0]) {
28512807Ssam 				bcopy(utmp[i].ut_line, we->we_utmp.out_line,
28612807Ssam 				   sizeof (utmp[i].ut_line));
28712807Ssam 				bcopy(utmp[i].ut_name, we->we_utmp.out_name,
28812807Ssam 				   sizeof (utmp[i].ut_name));
28912807Ssam 				we->we_utmp.out_time = htonl(utmp[i].ut_time);
2906577Ssam 				if (we >= wlast)
2916577Ssam 					break;
2926460Swnj 				we++;
2936460Swnj 			}
2946460Swnj 		utmpent = we - mywd.wd_we;
2956460Swnj 	}
2966460Swnj 	we = mywd.wd_we;
2976460Swnj 	for (i = 0; i < utmpent; i++) {
29812807Ssam 		if (stat(we->we_utmp.out_line, &stb) >= 0)
29912807Ssam 			we->we_idle = htonl(now - stb.st_atime);
3006460Swnj 		we++;
3016460Swnj 	}
30213187Ssam 	(void) lseek(kmemf, (long)nl[NL_AVENRUN].n_value, L_SET);
3036460Swnj 	(void) read(kmemf, (char *)avenrun, sizeof (avenrun));
3046460Swnj 	for (i = 0; i < 3; i++)
30512873Ssam 		mywd.wd_loadav[i] = htonl((u_long)(avenrun[i] * 100));
3066460Swnj 	cc = (char *)we - (char *)&mywd;
30712807Ssam 	mywd.wd_sendtime = htonl(time(0));
30812236Ssam 	mywd.wd_vers = WHODVERSION;
30912236Ssam 	mywd.wd_type = WHODTYPE_STATUS;
31012236Ssam 	for (np = neighbors; np != NULL; np = np->n_next)
31112236Ssam 		(void) sendto(s, (char *)&mywd, cc, 0,
31212236Ssam 			np->n_addr, np->n_addrlen);
31313469Ssam done:
31415541Sralph 	(void) alarm(AL_INTERVAL);
3156460Swnj }
3166460Swnj 
3176460Swnj getkmem()
3186460Swnj {
3196460Swnj 	struct nlist *nlp;
32016663Ssam 	static ino_t vmunixino;
32116663Ssam 	static time_t vmunixctime;
32216663Ssam 	struct stat sb;
3236460Swnj 
32416663Ssam 	if (stat("/vmunix", &sb) < 0) {
32516663Ssam 		if (vmunixctime)
32616663Ssam 			return;
32716663Ssam 	} else {
32816663Ssam 		if (sb.st_ctime == vmunixctime && sb.st_ino == vmunixino)
32916663Ssam 			return;
33016663Ssam 		vmunixctime = sb.st_ctime;
33116663Ssam 		vmunixino= sb.st_ino;
33216663Ssam 	}
3336460Swnj 	if (kmemf >= 0)
3346460Swnj 		(void) close(kmemf);
3356460Swnj loop:
33616673Sralph 	if (nlist("/vmunix", nl)) {
33716673Sralph 		syslog(LOG_WARNING, "/vmunix namelist botch");
3386460Swnj 		sleep(300);
3396460Swnj 		goto loop;
3406460Swnj 	}
34113187Ssam 	kmemf = open("/dev/kmem", O_RDONLY);
3426460Swnj 	if (kmemf < 0) {
34316673Sralph 		syslog(LOG_ERR, "/dev/kmem: %m");
34416673Sralph 		exit(1);
3456460Swnj 	}
34613187Ssam 	(void) lseek(kmemf, (long)nl[NL_BOOTTIME].n_value, L_SET);
34713187Ssam 	(void) read(kmemf, (char *)&mywd.wd_boottime,
34813187Ssam 	    sizeof (mywd.wd_boottime));
34912807Ssam 	mywd.wd_boottime = htonl(mywd.wd_boottime);
3506460Swnj }
35112236Ssam 
35212236Ssam /*
35312236Ssam  * Figure out device configuration and select
35412236Ssam  * networks which deserve status information.
35512236Ssam  */
35612236Ssam configure(s)
35712236Ssam 	int s;
35812236Ssam {
35912236Ssam 	char buf[BUFSIZ];
36012236Ssam 	struct ifconf ifc;
36112236Ssam 	struct ifreq ifreq, *ifr;
36212236Ssam 	struct sockaddr_in *sin;
36312236Ssam 	register struct neighbor *np;
36413187Ssam 	int n;
36512236Ssam 
36612236Ssam 	ifc.ifc_len = sizeof (buf);
36712236Ssam 	ifc.ifc_buf = buf;
36812236Ssam 	if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0) {
36916673Sralph 		syslog(LOG_ERR, "ioctl (get interface configuration)");
37012236Ssam 		return (0);
37112236Ssam 	}
37212236Ssam 	ifr = ifc.ifc_req;
37312236Ssam 	for (n = ifc.ifc_len / sizeof (struct ifreq); n > 0; n--, ifr++) {
37412236Ssam 		for (np = neighbors; np != NULL; np = np->n_next)
37512236Ssam 			if (np->n_name &&
37612236Ssam 			    strcmp(ifr->ifr_name, np->n_name) == 0)
37712236Ssam 				break;
37812236Ssam 		if (np != NULL)
37912236Ssam 			continue;
38012236Ssam 		ifreq = *ifr;
38112236Ssam 		np = (struct neighbor *)malloc(sizeof (*np));
38212236Ssam 		if (np == NULL)
38312236Ssam 			continue;
38412236Ssam 		np->n_name = malloc(strlen(ifr->ifr_name) + 1);
38512236Ssam 		if (np->n_name == NULL) {
38612236Ssam 			free((char *)np);
38712236Ssam 			continue;
38812236Ssam 		}
38912236Ssam 		strcpy(np->n_name, ifr->ifr_name);
39012236Ssam 		np->n_addrlen = sizeof (ifr->ifr_addr);
39112236Ssam 		np->n_addr = malloc(np->n_addrlen);
39212236Ssam 		if (np->n_addr == NULL) {
39312236Ssam 			free(np->n_name);
39412236Ssam 			free((char *)np);
39512236Ssam 			continue;
39612236Ssam 		}
39712236Ssam 		bcopy((char *)&ifr->ifr_addr, np->n_addr, np->n_addrlen);
39812236Ssam 		if (ioctl(s, SIOCGIFFLAGS, (char *)&ifreq) < 0) {
39916673Sralph 			syslog(LOG_ERR, "ioctl (get interface flags)");
40012236Ssam 			free((char *)np);
40112236Ssam 			continue;
40212236Ssam 		}
40321852Skarels 		if ((ifreq.ifr_flags & IFF_UP) == 0 ||
40421852Skarels 		    (ifreq.ifr_flags & (IFF_BROADCAST|IFF_POINTOPOINT)) == 0) {
40512236Ssam 			free((char *)np);
40612236Ssam 			continue;
40712236Ssam 		}
40812236Ssam 		np->n_flags = ifreq.ifr_flags;
40912236Ssam 		if (np->n_flags & IFF_POINTOPOINT) {
41012236Ssam 			if (ioctl(s, SIOCGIFDSTADDR, (char *)&ifreq) < 0) {
41116673Sralph 				syslog(LOG_ERR, "ioctl (get dstaddr)");
41212236Ssam 				free((char *)np);
41312236Ssam 				continue;
41412236Ssam 			}
41512236Ssam 			/* we assume addresses are all the same size */
41612236Ssam 			bcopy((char *)&ifreq.ifr_dstaddr,
41712236Ssam 			  np->n_addr, np->n_addrlen);
41812236Ssam 		}
41912236Ssam 		if (np->n_flags & IFF_BROADCAST) {
42021852Skarels 			if (ioctl(s, SIOCGIFBRDADDR, (char *)&ifreq) < 0) {
42121852Skarels 				syslog(LOG_ERR, "ioctl (get broadaddr)");
42221852Skarels 				free((char *)np);
42321852Skarels 				continue;
42421852Skarels 			}
42512236Ssam 			/* we assume addresses are all the same size */
42621852Skarels 			bcopy((char *)&ifreq.ifr_broadaddr,
42721852Skarels 			  np->n_addr, np->n_addrlen);
42812236Ssam 		}
42912236Ssam 		/* gag, wish we could get rid of Internet dependencies */
43012236Ssam 		sin = (struct sockaddr_in *)np->n_addr;
43112236Ssam 		sin->sin_port = sp->s_port;
43212236Ssam 		np->n_next = neighbors;
43312236Ssam 		neighbors = np;
43412236Ssam 	}
43512236Ssam 	return (1);
43612236Ssam }
43712807Ssam 
43816468Skarels /*
43916468Skarels  * Return the possible subnetwork number from an internet address.
44016468Skarels  * If the address is of the form of a subnet address (most significant
44116468Skarels  * bit of the host part is set), believe the subnet exists.
44216468Skarels  * Otherwise, return the network number.  Any subnet number is only valid
44316468Skarels  * if this is a ``local'' net.
44416468Skarels  */
44516468Skarels inet_subnetof(in)
44616468Skarels 	struct in_addr in;
44716468Skarels {
44816468Skarels 	register u_long i = ntohl(in.s_addr);
44916468Skarels 
45016468Skarels 	if (IN_CLASSA(i)) {
45116468Skarels 		if (IN_SUBNETA(i))
45216468Skarels 			return ((i & IN_CLASSA_SUBNET) >> IN_CLASSA_SUBNSHIFT);
45316468Skarels 		else
45416468Skarels 			return ((i & IN_CLASSA_NET) >> IN_CLASSA_NSHIFT);
45516468Skarels 	} else if (IN_CLASSB(i)) {
45616468Skarels 		if (IN_SUBNETB(i))
45716468Skarels 			return ((i & IN_CLASSB_SUBNET) >> IN_CLASSB_SUBNSHIFT);
45816468Skarels 		else
45916468Skarels 			return ((i & IN_CLASSB_NET) >> IN_CLASSB_NSHIFT);
46016468Skarels 	} else
46116468Skarels 		return ((i & IN_CLASSC_NET) >> IN_CLASSC_NSHIFT);
46216468Skarels }
46316468Skarels 
46412807Ssam #ifdef DEBUG
46512807Ssam sendto(s, buf, cc, flags, to, tolen)
46612807Ssam 	int s;
46712807Ssam 	char *buf;
46812807Ssam 	int cc, flags;
46912807Ssam 	char *to;
47012807Ssam 	int tolen;
47112807Ssam {
47212807Ssam 	register struct whod *w = (struct whod *)buf;
47312807Ssam 	register struct whoent *we;
47412807Ssam 	struct sockaddr_in *sin = (struct sockaddr_in *)to;
47512807Ssam 	char *interval();
47612807Ssam 
47712807Ssam 	printf("sendto %x.%d\n", ntohl(sin->sin_addr), ntohs(sin->sin_port));
47812807Ssam 	printf("hostname %s %s\n", w->wd_hostname,
47913187Ssam 	   interval(ntohl(w->wd_sendtime) - ntohl(w->wd_boottime), "  up"));
48012807Ssam 	printf("load %4.2f, %4.2f, %4.2f\n",
48113187Ssam 	    ntohl(w->wd_loadav[0]) / 100.0, ntohl(w->wd_loadav[1]) / 100.0,
48213187Ssam 	    ntohl(w->wd_loadav[2]) / 100.0);
48312807Ssam 	cc -= WHDRSIZE;
48412807Ssam 	for (we = w->wd_we, cc /= sizeof (struct whoent); cc > 0; cc--, we++) {
48513187Ssam 		time_t t = ntohl(we->we_utmp.out_time);
48612807Ssam 		printf("%-8.8s %s:%s %.12s",
48713187Ssam 			we->we_utmp.out_name,
48813187Ssam 			w->wd_hostname, we->we_utmp.out_line,
48913187Ssam 			ctime(&t)+4);
49013187Ssam 		we->we_idle = ntohl(we->we_idle) / 60;
49112807Ssam 		if (we->we_idle) {
49212807Ssam 			if (we->we_idle >= 100*60)
49312807Ssam 				we->we_idle = 100*60 - 1;
49412807Ssam 			if (we->we_idle >= 60)
49512807Ssam 				printf(" %2d", we->we_idle / 60);
49612807Ssam 			else
49712807Ssam 				printf("   ");
49812807Ssam 			printf(":%02d", we->we_idle % 60);
49912807Ssam 		}
50012807Ssam 		printf("\n");
50112807Ssam 	}
50212807Ssam }
50312807Ssam 
50412807Ssam char *
50512807Ssam interval(time, updown)
50612807Ssam 	int time;
50712807Ssam 	char *updown;
50812807Ssam {
50912807Ssam 	static char resbuf[32];
51012807Ssam 	int days, hours, minutes;
51112807Ssam 
51212807Ssam 	if (time < 0 || time > 3*30*24*60*60) {
51312807Ssam 		(void) sprintf(resbuf, "   %s ??:??", updown);
51412807Ssam 		return (resbuf);
51512807Ssam 	}
51612807Ssam 	minutes = (time + 59) / 60;		/* round to minutes */
51712807Ssam 	hours = minutes / 60; minutes %= 60;
51812807Ssam 	days = hours / 24; hours %= 24;
51912807Ssam 	if (days)
52012807Ssam 		(void) sprintf(resbuf, "%s %2d+%02d:%02d",
52112807Ssam 		    updown, days, hours, minutes);
52212807Ssam 	else
52312807Ssam 		(void) sprintf(resbuf, "%s    %2d:%02d",
52412807Ssam 		    updown, hours, minutes);
52512807Ssam 	return (resbuf);
52612807Ssam }
52712807Ssam #endif
528