xref: /csrg-svn/usr.sbin/rwhod/rwhod.c (revision 61873)
122549Sdist /*
2*61873Sbostic  * Copyright (c) 1983, 1993
3*61873Sbostic  *	The Regents of the University of California.  All rights reserved.
435392Sbostic  *
542819Sbostic  * %sccs.include.redist.c%
622549Sdist  */
722549Sdist 
86460Swnj #ifndef lint
9*61873Sbostic static char copyright[] =
10*61873Sbostic "@(#) Copyright (c) 1983, 1993\n\
11*61873Sbostic 	The Regents of the University of California.  All rights reserved.\n";
1235392Sbostic #endif /* not lint */
136460Swnj 
1422549Sdist #ifndef lint
15*61873Sbostic static char sccsid[] = "@(#)rwhod.c	8.1 (Berkeley) 06/06/93";
1635392Sbostic #endif /* not lint */
1722549Sdist 
1842265Skarels #include <sys/param.h>
199215Ssam #include <sys/socket.h>
209215Ssam #include <sys/stat.h>
2137295Sbostic #include <sys/signal.h>
229215Ssam #include <sys/ioctl.h>
2358922Smckusick #include <sys/sysctl.h>
249215Ssam 
2512236Ssam #include <net/if.h>
2652574Ssklower #include <net/if_dl.h>
2752574Ssklower #include <net/route.h>
289215Ssam #include <netinet/in.h>
2957744Sbostic #include <protocols/rwhod.h>
309215Ssam 
3157744Sbostic #include <ctype.h>
326460Swnj #include <errno.h>
3357744Sbostic #include <fcntl.h>
348486Ssam #include <netdb.h>
3557744Sbostic #include <paths.h>
3657744Sbostic #include <stdio.h>
3757744Sbostic #include <stdlib.h>
3857744Sbostic #include <string.h>
3916673Sralph #include <syslog.h>
4057739Ssklower #include <unistd.h>
4157744Sbostic #include <utmp.h>
426460Swnj 
4315541Sralph /*
4415541Sralph  * Alarm interval. Don't forget to change the down time check in ruptime
4515541Sralph  * if this is changed.
4615541Sralph  */
4716673Sralph #define AL_INTERVAL (3 * 60)
4815541Sralph 
4942265Skarels char	myname[MAXHOSTNAMELEN];
506460Swnj 
5112236Ssam /*
5257744Sbostic  * We communicate with each neighbor in a list constructed at the time we're
5357744Sbostic  * started up.  Neighbors are currently directly connected via a hardware
5457744Sbostic  * interface.
5512236Ssam  */
5612236Ssam struct	neighbor {
5712236Ssam 	struct	neighbor *n_next;
5812236Ssam 	char	*n_name;		/* interface name */
5952574Ssklower 	struct	sockaddr *n_addr;		/* who to send to */
6012236Ssam 	int	n_addrlen;		/* size of address */
6112236Ssam 	int	n_flags;		/* should forward?, interface flags */
6212236Ssam };
6312236Ssam 
6412236Ssam struct	neighbor *neighbors;
656460Swnj struct	whod mywd;
6612236Ssam struct	servent *sp;
6760204Smckusick int	s, utmpf;
686460Swnj 
6960119Storek #define	WHDRSIZE	(sizeof(mywd) - sizeof(mywd.wd_we))
7011834Ssam 
7157744Sbostic int	 configure __P((int));
7260204Smckusick void	 getboottime __P((int));
7357744Sbostic void	 onalrm __P((int));
7457744Sbostic void	 quit __P((char *));
7557744Sbostic void	 rt_xaddrs __P((caddr_t, caddr_t, struct rt_addrinfo *));
7657744Sbostic int	 verify __P((char *));
7757744Sbostic #ifdef DEBUG
7857744Sbostic char	*interval __P((int, char *));
7960119Storek void	 Sendto __P((int, char *, int, int, char *, int));
8060119Storek #define	 sendto Sendto
8157744Sbostic #endif
826460Swnj 
8357744Sbostic int
main(argc,argv)8457744Sbostic main(argc, argv)
8557744Sbostic 	int argc;
8657744Sbostic 	char argv[];
876460Swnj {
886460Swnj 	struct sockaddr_in from;
8926016Skarels 	struct stat st;
906460Swnj 	char path[64];
9126484Smckusick 	int on = 1;
9257744Sbostic 	char *cp;
9360119Storek 	struct sockaddr_in sin;
946460Swnj 
9516673Sralph 	if (getuid()) {
9616673Sralph 		fprintf(stderr, "rwhod: not super user\n");
9716673Sralph 		exit(1);
9816673Sralph 	}
998486Ssam 	sp = getservbyname("who", "udp");
10057744Sbostic 	if (sp == NULL) {
1018486Ssam 		fprintf(stderr, "rwhod: udp/who: unknown service\n");
1028486Ssam 		exit(1);
1038486Ssam 	}
1046460Swnj #ifndef DEBUG
10544707Skarels 	daemon(1, 0);
1066460Swnj #endif
10737295Sbostic 	if (chdir(_PATH_RWHODIR) < 0) {
10837295Sbostic 		(void)fprintf(stderr, "rwhod: %s: %s\n",
10937295Sbostic 		    _PATH_RWHODIR, strerror(errno));
11026011Smckusick 		exit(1);
11126011Smckusick 	}
11260204Smckusick 	(void) signal(SIGHUP, getboottime);
11324853Seric 	openlog("rwhod", LOG_PID, LOG_DAEMON);
11412236Ssam 	/*
11512236Ssam 	 * Establish host name as returned by system.
11612236Ssam 	 */
11760119Storek 	if (gethostname(myname, sizeof(myname) - 1) < 0) {
11816673Sralph 		syslog(LOG_ERR, "gethostname: %m");
1196460Swnj 		exit(1);
1206460Swnj 	}
12124745Sbloom 	if ((cp = index(myname, '.')) != NULL)
12224745Sbloom 		*cp = '\0';
12360119Storek 	strncpy(mywd.wd_hostname, myname, sizeof(myname) - 1);
12437295Sbostic 	utmpf = open(_PATH_UTMP, O_RDONLY|O_CREAT, 0644);
1256460Swnj 	if (utmpf < 0) {
12637295Sbostic 		syslog(LOG_ERR, "%s: %m", _PATH_UTMP);
1276460Swnj 		exit(1);
1286460Swnj 	}
12960204Smckusick 	getboottime(0);
13013187Ssam 	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
13116673Sralph 		syslog(LOG_ERR, "socket: %m");
1329215Ssam 		exit(1);
1336460Swnj 	}
13460119Storek 	if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) < 0) {
13517055Skarels 		syslog(LOG_ERR, "setsockopt SO_BROADCAST: %m");
13617055Skarels 		exit(1);
13717055Skarels 	}
13860119Storek 	memset(&sin, 0, sizeof(sin));
13942265Skarels 	sin.sin_family = AF_INET;
14012236Ssam 	sin.sin_port = sp->s_port;
14160119Storek 	if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
14216673Sralph 		syslog(LOG_ERR, "bind: %m");
1439215Ssam 		exit(1);
1449215Ssam 	}
14512236Ssam 	if (!configure(s))
14612236Ssam 		exit(1);
14713027Ssam 	signal(SIGALRM, onalrm);
14857744Sbostic 	onalrm(0);
1496460Swnj 	for (;;) {
1506460Swnj 		struct whod wd;
15160119Storek 		int cc, whod, len = sizeof(from);
1526460Swnj 
15360119Storek 		cc = recvfrom(s, (char *)&wd, sizeof(struct whod), 0,
15446927Sbostic 			(struct sockaddr *)&from, &len);
1556460Swnj 		if (cc <= 0) {
1566460Swnj 			if (cc < 0 && errno != EINTR)
15716673Sralph 				syslog(LOG_WARNING, "recv: %m");
1586460Swnj 			continue;
1596460Swnj 		}
1608486Ssam 		if (from.sin_port != sp->s_port) {
16116673Sralph 			syslog(LOG_WARNING, "%d: bad from port",
1628486Ssam 				ntohs(from.sin_port));
1636460Swnj 			continue;
1646460Swnj 		}
16512347Ssam 		if (wd.wd_vers != WHODVERSION)
16612347Ssam 			continue;
16712236Ssam 		if (wd.wd_type != WHODTYPE_STATUS)
16812236Ssam 			continue;
1698486Ssam 		if (!verify(wd.wd_hostname)) {
17016673Sralph 			syslog(LOG_WARNING, "malformed host name from %x",
1718486Ssam 				from.sin_addr);
1728486Ssam 			continue;
1738486Ssam 		}
17426011Smckusick 		(void) sprintf(path, "whod.%s", wd.wd_hostname);
17526016Skarels 		/*
17626016Skarels 		 * Rather than truncating and growing the file each time,
17726016Skarels 		 * use ftruncate if size is less than previous size.
17826016Skarels 		 */
17926016Skarels 		whod = open(path, O_WRONLY | O_CREAT, 0644);
1806460Swnj 		if (whod < 0) {
18116673Sralph 			syslog(LOG_WARNING, "%s: %m", path);
1826460Swnj 			continue;
1836460Swnj 		}
18442265Skarels #if ENDIAN != BIG_ENDIAN
18511834Ssam 		{
18613187Ssam 			int i, n = (cc - WHDRSIZE)/sizeof(struct whoent);
18711834Ssam 			struct whoent *we;
18811834Ssam 
18911834Ssam 			/* undo header byte swapping before writing to file */
19011834Ssam 			wd.wd_sendtime = ntohl(wd.wd_sendtime);
19111834Ssam 			for (i = 0; i < 3; i++)
19211834Ssam 				wd.wd_loadav[i] = ntohl(wd.wd_loadav[i]);
19311834Ssam 			wd.wd_boottime = ntohl(wd.wd_boottime);
19411834Ssam 			we = wd.wd_we;
19511834Ssam 			for (i = 0; i < n; i++) {
19611834Ssam 				we->we_idle = ntohl(we->we_idle);
19712872Ssam 				we->we_utmp.out_time =
19812872Ssam 				    ntohl(we->we_utmp.out_time);
19911834Ssam 				we++;
20011834Ssam 			}
20111834Ssam 		}
20211834Ssam #endif
20346927Sbostic 		(void) time((time_t *)&wd.wd_recvtime);
2046460Swnj 		(void) write(whod, (char *)&wd, cc);
20526016Skarels 		if (fstat(whod, &st) < 0 || st.st_size > cc)
20626016Skarels 			ftruncate(whod, cc);
2076460Swnj 		(void) close(whod);
2086460Swnj 	}
2096460Swnj }
2106460Swnj 
2118486Ssam /*
2128486Ssam  * Check out host name for unprintables
2138486Ssam  * and other funnies before allowing a file
2148486Ssam  * to be created.  Sorry, but blanks aren't allowed.
2158486Ssam  */
21657744Sbostic int
verify(name)2178486Ssam verify(name)
2188486Ssam 	register char *name;
2198486Ssam {
2208486Ssam 	register int size = 0;
2218486Ssam 
2228486Ssam 	while (*name) {
22315214Sleres 		if (!isascii(*name) || !(isalnum(*name) || ispunct(*name)))
2248486Ssam 			return (0);
2258486Ssam 		name++, size++;
2268486Ssam 	}
2278486Ssam 	return (size > 0);
2288486Ssam }
2298486Ssam 
2306460Swnj int	utmptime;
2316460Swnj int	utmpent;
23222546Sbloom int	utmpsize = 0;
23322546Sbloom struct	utmp *utmp;
2346460Swnj int	alarmcount;
2356460Swnj 
23646927Sbostic void
onalrm(signo)23757744Sbostic onalrm(signo)
23857744Sbostic 	int signo;
2396460Swnj {
24037295Sbostic 	register struct neighbor *np;
24137295Sbostic 	register struct whoent *we = mywd.wd_we, *wlast;
2426460Swnj 	register int i;
2436460Swnj 	struct stat stb;
24457744Sbostic 	double avenrun[3];
24557744Sbostic 	time_t now;
2466460Swnj 	int cc;
2476460Swnj 
24857744Sbostic 	now = time(NULL);
2496460Swnj 	if (alarmcount % 10 == 0)
25060204Smckusick 		getboottime(0);
2516460Swnj 	alarmcount++;
2526460Swnj 	(void) fstat(utmpf, &stb);
25322546Sbloom 	if ((stb.st_mtime != utmptime) || (stb.st_size > utmpsize)) {
25417303Stef 		utmptime = stb.st_mtime;
25522546Sbloom 		if (stb.st_size > utmpsize) {
25622546Sbloom 			utmpsize = stb.st_size + 10 * sizeof(struct utmp);
25722546Sbloom 			if (utmp)
25822546Sbloom 				utmp = (struct utmp *)realloc(utmp, utmpsize);
25922546Sbloom 			else
26022546Sbloom 				utmp = (struct utmp *)malloc(utmpsize);
26122546Sbloom 			if (! utmp) {
26222546Sbloom 				fprintf(stderr, "rwhod: malloc failed\n");
26322546Sbloom 				utmpsize = 0;
26422546Sbloom 				goto done;
26522546Sbloom 			}
26622546Sbloom 		}
26757739Ssklower 		(void) lseek(utmpf, (off_t)0, L_SET);
26822546Sbloom 		cc = read(utmpf, (char *)utmp, stb.st_size);
2696460Swnj 		if (cc < 0) {
27037295Sbostic 			fprintf(stderr, "rwhod: %s: %s\n",
27137295Sbostic 			    _PATH_UTMP, strerror(errno));
27213469Ssam 			goto done;
2736460Swnj 		}
27460119Storek 		wlast = &mywd.wd_we[1024 / sizeof(struct whoent) - 1];
27560119Storek 		utmpent = cc / sizeof(struct utmp);
2766460Swnj 		for (i = 0; i < utmpent; i++)
2776460Swnj 			if (utmp[i].ut_name[0]) {
27860119Storek 				memcpy(we->we_utmp.out_line, utmp[i].ut_line,
27960119Storek 				   sizeof(utmp[i].ut_line));
28060119Storek 				memcpy(we->we_utmp.out_name, utmp[i].ut_name,
28160119Storek 				   sizeof(utmp[i].ut_name));
28212807Ssam 				we->we_utmp.out_time = htonl(utmp[i].ut_time);
2836577Ssam 				if (we >= wlast)
2846577Ssam 					break;
2856460Swnj 				we++;
2866460Swnj 			}
2876460Swnj 		utmpent = we - mywd.wd_we;
2886460Swnj 	}
28926484Smckusick 
29026484Smckusick 	/*
29126484Smckusick 	 * The test on utmpent looks silly---after all, if no one is
29226484Smckusick 	 * logged on, why worry about efficiency?---but is useful on
29326484Smckusick 	 * (e.g.) compute servers.
29426484Smckusick 	 */
29537971Sbostic 	if (utmpent && chdir(_PATH_DEV)) {
29637971Sbostic 		syslog(LOG_ERR, "chdir(%s): %m", _PATH_DEV);
29726484Smckusick 		exit(1);
29826484Smckusick 	}
2996460Swnj 	we = mywd.wd_we;
3006460Swnj 	for (i = 0; i < utmpent; i++) {
30126484Smckusick 		if (stat(we->we_utmp.out_line, &stb) >= 0)
30212807Ssam 			we->we_idle = htonl(now - stb.st_atime);
3036460Swnj 		we++;
3046460Swnj 	}
30560119Storek 	(void)getloadavg(avenrun, sizeof(avenrun)/sizeof(avenrun[0]));
3066460Swnj 	for (i = 0; i < 3; i++)
30712873Ssam 		mywd.wd_loadav[i] = htonl((u_long)(avenrun[i] * 100));
3086460Swnj 	cc = (char *)we - (char *)&mywd;
30912807Ssam 	mywd.wd_sendtime = htonl(time(0));
31012236Ssam 	mywd.wd_vers = WHODVERSION;
31112236Ssam 	mywd.wd_type = WHODTYPE_STATUS;
31212236Ssam 	for (np = neighbors; np != NULL; np = np->n_next)
31360119Storek 		(void)sendto(s, (char *)&mywd, cc, 0,
31452574Ssklower 				np->n_addr, np->n_addrlen);
31537295Sbostic 	if (utmpent && chdir(_PATH_RWHODIR)) {
31637295Sbostic 		syslog(LOG_ERR, "chdir(%s): %m", _PATH_RWHODIR);
31726484Smckusick 		exit(1);
31826484Smckusick 	}
31913469Ssam done:
32015541Sralph 	(void) alarm(AL_INTERVAL);
3216460Swnj }
3226460Swnj 
32346927Sbostic void
getboottime(signo)32460204Smckusick getboottime(signo)
32557744Sbostic 	int signo;
3266460Swnj {
32760464Storek 	int mib[2];
32860464Storek 	size_t size;
32960204Smckusick 	struct timeval tm;
3306460Swnj 
33160204Smckusick 	mib[0] = CTL_KERN;
33260204Smckusick 	mib[1] = KERN_BOOTTIME;
33360204Smckusick 	size = sizeof(tm);
33460204Smckusick 	if (sysctl(mib, 2, &tm, &size, NULL, 0) == -1) {
33560204Smckusick 		syslog(LOG_ERR, "cannot get boottime: %m");
33616673Sralph 		exit(1);
3376460Swnj 	}
33860204Smckusick 	mywd.wd_boottime = htonl(tm.tv_sec);
3396460Swnj }
34012236Ssam 
34152574Ssklower void
quit(msg)34252574Ssklower quit(msg)
34357744Sbostic 	char *msg;
34452574Ssklower {
34552574Ssklower 	syslog(LOG_ERR, msg);
34652574Ssklower 	exit(1);
34752574Ssklower }
34852574Ssklower 
34952574Ssklower #define ROUNDUP(a) \
35052574Ssklower 	((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
35152574Ssklower #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
35252574Ssklower 
35352574Ssklower void
rt_xaddrs(cp,cplim,rtinfo)35452574Ssklower rt_xaddrs(cp, cplim, rtinfo)
35552574Ssklower 	register caddr_t cp, cplim;
35652574Ssklower 	register struct rt_addrinfo *rtinfo;
35752574Ssklower {
35852574Ssklower 	register struct sockaddr *sa;
35952574Ssklower 	register int i;
36052574Ssklower 
36160119Storek 	memset(rtinfo->rti_info, 0, sizeof(rtinfo->rti_info));
36252574Ssklower 	for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
36352574Ssklower 		if ((rtinfo->rti_addrs & (1 << i)) == 0)
36452574Ssklower 			continue;
36552574Ssklower 		rtinfo->rti_info[i] = sa = (struct sockaddr *)cp;
36652574Ssklower 		ADVANCE(cp, sa);
36752574Ssklower 	}
36852574Ssklower }
36952574Ssklower 
37012236Ssam /*
37112236Ssam  * Figure out device configuration and select
37212236Ssam  * networks which deserve status information.
37312236Ssam  */
37457744Sbostic int
configure(s)37512236Ssam configure(s)
37612236Ssam 	int s;
37712236Ssam {
37852574Ssklower 	register struct neighbor *np;
37952574Ssklower 	register struct if_msghdr *ifm;
38052574Ssklower 	register struct ifa_msghdr *ifam;
38152574Ssklower 	struct sockaddr_dl *sdl;
38258922Smckusick 	size_t needed;
38358922Smckusick 	int mib[6], flags = 0, len;
38452574Ssklower 	char *buf, *lim, *next;
38552574Ssklower 	struct rt_addrinfo info;
38612236Ssam 
38758922Smckusick 	mib[0] = CTL_NET;
38858922Smckusick 	mib[1] = PF_ROUTE;
38958922Smckusick 	mib[2] = 0;
39058922Smckusick 	mib[3] = AF_INET;
39158922Smckusick 	mib[4] = NET_RT_IFLIST;
39258922Smckusick 	mib[5] = 0;
39358922Smckusick 	if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
39458922Smckusick 		quit("route-sysctl-estimate");
39552574Ssklower 	if ((buf = malloc(needed)) == NULL)
39652574Ssklower 		quit("malloc");
39758922Smckusick 	if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
39852574Ssklower 		quit("actual retrieval of interface table");
39958922Smckusick 	lim = buf + needed;
40052574Ssklower 
40160119Storek 	sdl = NULL;		/* XXX just to keep gcc -Wall happy */
40252574Ssklower 	for (next = buf; next < lim; next += ifm->ifm_msglen) {
40352574Ssklower 		ifm = (struct if_msghdr *)next;
40452574Ssklower 		if (ifm->ifm_type == RTM_IFINFO) {
40552574Ssklower 			sdl = (struct sockaddr_dl *)(ifm + 1);
40652574Ssklower 			flags = ifm->ifm_flags;
40752574Ssklower 			continue;
40852574Ssklower 		}
40952574Ssklower 		if ((flags & IFF_UP) == 0 ||
41052574Ssklower 		    (flags & (IFF_BROADCAST|IFF_POINTOPOINT)) == 0)
41152574Ssklower 			continue;
41252574Ssklower 		if (ifm->ifm_type != RTM_NEWADDR)
41358922Smckusick 			quit("out of sync parsing NET_RT_IFLIST");
41452574Ssklower 		ifam = (struct ifa_msghdr *)ifm;
41552574Ssklower 		info.rti_addrs = ifam->ifam_addrs;
41652574Ssklower 		rt_xaddrs((char *)(ifam + 1), ifam->ifam_msglen + (char *)ifam,
41752574Ssklower 			&info);
41852574Ssklower 		/* gag, wish we could get rid of Internet dependencies */
41952574Ssklower #define dstaddr	info.rti_info[RTAX_BRD]
42052574Ssklower #define IPADDR_SA(x) ((struct sockaddr_in *)(x))->sin_addr.s_addr
42152574Ssklower #define PORT_SA(x) ((struct sockaddr_in *)(x))->sin_port
42252574Ssklower 		if (dstaddr == 0 || dstaddr->sa_family != AF_INET)
42352574Ssklower 			continue;
42452574Ssklower 		PORT_SA(dstaddr) = sp->s_port;
42512236Ssam 		for (np = neighbors; np != NULL; np = np->n_next)
42660119Storek 			if (memcmp(sdl->sdl_data, np->n_name,
42760119Storek 				   sdl->sdl_nlen) == 0 &&
42860119Storek 			    IPADDR_SA(np->n_addr) == IPADDR_SA(dstaddr))
42912236Ssam 				break;
43012236Ssam 		if (np != NULL)
43112236Ssam 			continue;
43252574Ssklower 		len = sizeof(*np) + dstaddr->sa_len + sdl->sdl_nlen + 1;
43352574Ssklower 		np = (struct neighbor *)malloc(len);
43412236Ssam 		if (np == NULL)
43552574Ssklower 			quit("malloc of neighbor structure");
43660119Storek 		memset(np, 0, len);
43752574Ssklower 		np->n_flags = flags;
43852574Ssklower 		np->n_addr = (struct sockaddr *)(np + 1);
43952574Ssklower 		np->n_addrlen = dstaddr->sa_len;
44052574Ssklower 		np->n_name = np->n_addrlen + (char *)np->n_addr;
44112236Ssam 		np->n_next = neighbors;
44212236Ssam 		neighbors = np;
44360119Storek 		memcpy((char *)np->n_addr, (char *)dstaddr, np->n_addrlen);
44460119Storek 		memcpy(np->n_name, sdl->sdl_data, sdl->sdl_nlen);
44512236Ssam 	}
44652574Ssklower 	free(buf);
44712236Ssam 	return (1);
44812236Ssam }
44912807Ssam 
45012807Ssam #ifdef DEBUG
45157744Sbostic void
Sendto(s,buf,cc,flags,to,tolen)45260119Storek Sendto(s, buf, cc, flags, to, tolen)
45312807Ssam 	int s;
45412807Ssam 	char *buf;
45512807Ssam 	int cc, flags;
45612807Ssam 	char *to;
45712807Ssam 	int tolen;
45812807Ssam {
45912807Ssam 	register struct whod *w = (struct whod *)buf;
46012807Ssam 	register struct whoent *we;
46112807Ssam 	struct sockaddr_in *sin = (struct sockaddr_in *)to;
46212807Ssam 
46312807Ssam 	printf("sendto %x.%d\n", ntohl(sin->sin_addr), ntohs(sin->sin_port));
46412807Ssam 	printf("hostname %s %s\n", w->wd_hostname,
46513187Ssam 	   interval(ntohl(w->wd_sendtime) - ntohl(w->wd_boottime), "  up"));
46612807Ssam 	printf("load %4.2f, %4.2f, %4.2f\n",
46713187Ssam 	    ntohl(w->wd_loadav[0]) / 100.0, ntohl(w->wd_loadav[1]) / 100.0,
46813187Ssam 	    ntohl(w->wd_loadav[2]) / 100.0);
46912807Ssam 	cc -= WHDRSIZE;
47060119Storek 	for (we = w->wd_we, cc /= sizeof(struct whoent); cc > 0; cc--, we++) {
47113187Ssam 		time_t t = ntohl(we->we_utmp.out_time);
47212807Ssam 		printf("%-8.8s %s:%s %.12s",
47313187Ssam 			we->we_utmp.out_name,
47413187Ssam 			w->wd_hostname, we->we_utmp.out_line,
47513187Ssam 			ctime(&t)+4);
47613187Ssam 		we->we_idle = ntohl(we->we_idle) / 60;
47712807Ssam 		if (we->we_idle) {
47812807Ssam 			if (we->we_idle >= 100*60)
47912807Ssam 				we->we_idle = 100*60 - 1;
48012807Ssam 			if (we->we_idle >= 60)
48112807Ssam 				printf(" %2d", we->we_idle / 60);
48212807Ssam 			else
48312807Ssam 				printf("   ");
48412807Ssam 			printf(":%02d", we->we_idle % 60);
48512807Ssam 		}
48612807Ssam 		printf("\n");
48712807Ssam 	}
48812807Ssam }
48912807Ssam 
49012807Ssam char *
interval(time,updown)49112807Ssam interval(time, updown)
49212807Ssam 	int time;
49312807Ssam 	char *updown;
49412807Ssam {
49512807Ssam 	static char resbuf[32];
49612807Ssam 	int days, hours, minutes;
49712807Ssam 
49812807Ssam 	if (time < 0 || time > 3*30*24*60*60) {
49912807Ssam 		(void) sprintf(resbuf, "   %s ??:??", updown);
50012807Ssam 		return (resbuf);
50112807Ssam 	}
50212807Ssam 	minutes = (time + 59) / 60;		/* round to minutes */
50312807Ssam 	hours = minutes / 60; minutes %= 60;
50412807Ssam 	days = hours / 24; hours %= 24;
50512807Ssam 	if (days)
50612807Ssam 		(void) sprintf(resbuf, "%s %2d+%02d:%02d",
50712807Ssam 		    updown, days, hours, minutes);
50812807Ssam 	else
50912807Ssam 		(void) sprintf(resbuf, "%s    %2d:%02d",
51012807Ssam 		    updown, hours, minutes);
51112807Ssam 	return (resbuf);
51212807Ssam }
51312807Ssam #endif
514