xref: /csrg-svn/usr.sbin/rwhod/rwhod.c (revision 38182)
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*38182Smckusick static char sccsid[] = "@(#)rwhod.c	5.14 (Berkeley) 05/29/89";
2635392Sbostic #endif /* not lint */
2722549Sdist 
289215Ssam #include <sys/types.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 
548486Ssam struct	sockaddr_in sin = { AF_INET };
556460Swnj 
5612236Ssam char	myname[32];
576460Swnj 
586460Swnj struct	nlist nl[] = {
59*38182Smckusick #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 	}
15612236Ssam 	sin.sin_port = sp->s_port;
15713187Ssam 	if (bind(s, &sin, sizeof (sin)) < 0) {
15816673Sralph 		syslog(LOG_ERR, "bind: %m");
1599215Ssam 		exit(1);
1609215Ssam 	}
16112236Ssam 	if (!configure(s))
16212236Ssam 		exit(1);
16313027Ssam 	signal(SIGALRM, onalrm);
1646460Swnj 	onalrm();
1656460Swnj 	for (;;) {
1666460Swnj 		struct whod wd;
16712236Ssam 		int cc, whod, len = sizeof (from);
1686460Swnj 
16912236Ssam 		cc = recvfrom(s, (char *)&wd, sizeof (struct whod), 0,
17012236Ssam 			&from, &len);
1716460Swnj 		if (cc <= 0) {
1726460Swnj 			if (cc < 0 && errno != EINTR)
17316673Sralph 				syslog(LOG_WARNING, "recv: %m");
1746460Swnj 			continue;
1756460Swnj 		}
1768486Ssam 		if (from.sin_port != sp->s_port) {
17716673Sralph 			syslog(LOG_WARNING, "%d: bad from port",
1788486Ssam 				ntohs(from.sin_port));
1796460Swnj 			continue;
1806460Swnj 		}
1818486Ssam #ifdef notdef
1828486Ssam 		if (gethostbyname(wd.wd_hostname) == 0) {
18316673Sralph 			syslog(LOG_WARNING, "%s: unknown host",
1848486Ssam 				wd.wd_hostname);
1856460Swnj 			continue;
1866460Swnj 		}
1878486Ssam #endif
18812347Ssam 		if (wd.wd_vers != WHODVERSION)
18912347Ssam 			continue;
19012236Ssam 		if (wd.wd_type != WHODTYPE_STATUS)
19112236Ssam 			continue;
1928486Ssam 		if (!verify(wd.wd_hostname)) {
19316673Sralph 			syslog(LOG_WARNING, "malformed host name from %x",
1948486Ssam 				from.sin_addr);
1958486Ssam 			continue;
1968486Ssam 		}
19726011Smckusick 		(void) sprintf(path, "whod.%s", wd.wd_hostname);
19826016Skarels 		/*
19926016Skarels 		 * Rather than truncating and growing the file each time,
20026016Skarels 		 * use ftruncate if size is less than previous size.
20126016Skarels 		 */
20226016Skarels 		whod = open(path, O_WRONLY | O_CREAT, 0644);
2036460Swnj 		if (whod < 0) {
20416673Sralph 			syslog(LOG_WARNING, "%s: %m", path);
2056460Swnj 			continue;
2066460Swnj 		}
20711834Ssam #if vax || pdp11
20811834Ssam 		{
20913187Ssam 			int i, n = (cc - WHDRSIZE)/sizeof(struct whoent);
21011834Ssam 			struct whoent *we;
21111834Ssam 
21211834Ssam 			/* undo header byte swapping before writing to file */
21311834Ssam 			wd.wd_sendtime = ntohl(wd.wd_sendtime);
21411834Ssam 			for (i = 0; i < 3; i++)
21511834Ssam 				wd.wd_loadav[i] = ntohl(wd.wd_loadav[i]);
21611834Ssam 			wd.wd_boottime = ntohl(wd.wd_boottime);
21711834Ssam 			we = wd.wd_we;
21811834Ssam 			for (i = 0; i < n; i++) {
21911834Ssam 				we->we_idle = ntohl(we->we_idle);
22012872Ssam 				we->we_utmp.out_time =
22112872Ssam 				    ntohl(we->we_utmp.out_time);
22211834Ssam 				we++;
22311834Ssam 			}
22411834Ssam 		}
22511834Ssam #endif
2266460Swnj 		(void) time(&wd.wd_recvtime);
2276460Swnj 		(void) write(whod, (char *)&wd, cc);
22826016Skarels 		if (fstat(whod, &st) < 0 || st.st_size > cc)
22926016Skarels 			ftruncate(whod, cc);
2306460Swnj 		(void) close(whod);
2316460Swnj 	}
2326460Swnj }
2336460Swnj 
2348486Ssam /*
2358486Ssam  * Check out host name for unprintables
2368486Ssam  * and other funnies before allowing a file
2378486Ssam  * to be created.  Sorry, but blanks aren't allowed.
2388486Ssam  */
2398486Ssam verify(name)
2408486Ssam 	register char *name;
2418486Ssam {
2428486Ssam 	register int size = 0;
2438486Ssam 
2448486Ssam 	while (*name) {
24515214Sleres 		if (!isascii(*name) || !(isalnum(*name) || ispunct(*name)))
2468486Ssam 			return (0);
2478486Ssam 		name++, size++;
2488486Ssam 	}
2498486Ssam 	return (size > 0);
2508486Ssam }
2518486Ssam 
2526460Swnj int	utmptime;
2536460Swnj int	utmpent;
25422546Sbloom int	utmpsize = 0;
25522546Sbloom struct	utmp *utmp;
2566460Swnj int	alarmcount;
2576460Swnj 
2586460Swnj onalrm()
2596460Swnj {
26037295Sbostic 	register struct neighbor *np;
26137295Sbostic 	register struct whoent *we = mywd.wd_we, *wlast;
2626460Swnj 	register int i;
2636460Swnj 	struct stat stb;
2646460Swnj 	int cc;
2656460Swnj 	double avenrun[3];
26637295Sbostic 	time_t now = time((time_t *)NULL);
26737295Sbostic 	char *strerror();
2686460Swnj 
2696460Swnj 	if (alarmcount % 10 == 0)
2706460Swnj 		getkmem();
2716460Swnj 	alarmcount++;
2726460Swnj 	(void) fstat(utmpf, &stb);
27322546Sbloom 	if ((stb.st_mtime != utmptime) || (stb.st_size > utmpsize)) {
27417303Stef 		utmptime = stb.st_mtime;
27522546Sbloom 		if (stb.st_size > utmpsize) {
27622546Sbloom 			utmpsize = stb.st_size + 10 * sizeof(struct utmp);
27722546Sbloom 			if (utmp)
27822546Sbloom 				utmp = (struct utmp *)realloc(utmp, utmpsize);
27922546Sbloom 			else
28022546Sbloom 				utmp = (struct utmp *)malloc(utmpsize);
28122546Sbloom 			if (! utmp) {
28222546Sbloom 				fprintf(stderr, "rwhod: malloc failed\n");
28322546Sbloom 				utmpsize = 0;
28422546Sbloom 				goto done;
28522546Sbloom 			}
28622546Sbloom 		}
28713187Ssam 		(void) lseek(utmpf, (long)0, L_SET);
28822546Sbloom 		cc = read(utmpf, (char *)utmp, stb.st_size);
2896460Swnj 		if (cc < 0) {
29037295Sbostic 			fprintf(stderr, "rwhod: %s: %s\n",
29137295Sbostic 			    _PATH_UTMP, strerror(errno));
29213469Ssam 			goto done;
2936460Swnj 		}
29412236Ssam 		wlast = &mywd.wd_we[1024 / sizeof (struct whoent) - 1];
2956460Swnj 		utmpent = cc / sizeof (struct utmp);
2966460Swnj 		for (i = 0; i < utmpent; i++)
2976460Swnj 			if (utmp[i].ut_name[0]) {
29812807Ssam 				bcopy(utmp[i].ut_line, we->we_utmp.out_line,
29912807Ssam 				   sizeof (utmp[i].ut_line));
30012807Ssam 				bcopy(utmp[i].ut_name, we->we_utmp.out_name,
30112807Ssam 				   sizeof (utmp[i].ut_name));
30212807Ssam 				we->we_utmp.out_time = htonl(utmp[i].ut_time);
3036577Ssam 				if (we >= wlast)
3046577Ssam 					break;
3056460Swnj 				we++;
3066460Swnj 			}
3076460Swnj 		utmpent = we - mywd.wd_we;
3086460Swnj 	}
30926484Smckusick 
31026484Smckusick 	/*
31126484Smckusick 	 * The test on utmpent looks silly---after all, if no one is
31226484Smckusick 	 * logged on, why worry about efficiency?---but is useful on
31326484Smckusick 	 * (e.g.) compute servers.
31426484Smckusick 	 */
31537971Sbostic 	if (utmpent && chdir(_PATH_DEV)) {
31637971Sbostic 		syslog(LOG_ERR, "chdir(%s): %m", _PATH_DEV);
31726484Smckusick 		exit(1);
31826484Smckusick 	}
3196460Swnj 	we = mywd.wd_we;
3206460Swnj 	for (i = 0; i < utmpent; i++) {
32126484Smckusick 		if (stat(we->we_utmp.out_line, &stb) >= 0)
32212807Ssam 			we->we_idle = htonl(now - stb.st_atime);
3236460Swnj 		we++;
3246460Swnj 	}
325*38182Smckusick 	(void) getloadavg(avenrun, sizeof(avenrun)/sizeof(avenrun[0]));
3266460Swnj 	for (i = 0; i < 3; i++)
32712873Ssam 		mywd.wd_loadav[i] = htonl((u_long)(avenrun[i] * 100));
3286460Swnj 	cc = (char *)we - (char *)&mywd;
32912807Ssam 	mywd.wd_sendtime = htonl(time(0));
33012236Ssam 	mywd.wd_vers = WHODVERSION;
33112236Ssam 	mywd.wd_type = WHODTYPE_STATUS;
33212236Ssam 	for (np = neighbors; np != NULL; np = np->n_next)
33312236Ssam 		(void) sendto(s, (char *)&mywd, cc, 0,
33412236Ssam 			np->n_addr, np->n_addrlen);
33537295Sbostic 	if (utmpent && chdir(_PATH_RWHODIR)) {
33637295Sbostic 		syslog(LOG_ERR, "chdir(%s): %m", _PATH_RWHODIR);
33726484Smckusick 		exit(1);
33826484Smckusick 	}
33913469Ssam done:
34015541Sralph 	(void) alarm(AL_INTERVAL);
3416460Swnj }
3426460Swnj 
3436460Swnj getkmem()
3446460Swnj {
34516663Ssam 	static ino_t vmunixino;
34616663Ssam 	static time_t vmunixctime;
34716663Ssam 	struct stat sb;
3486460Swnj 
34937295Sbostic 	if (stat(_PATH_UNIX, &sb) < 0) {
35016663Ssam 		if (vmunixctime)
35116663Ssam 			return;
35216663Ssam 	} else {
35316663Ssam 		if (sb.st_ctime == vmunixctime && sb.st_ino == vmunixino)
35416663Ssam 			return;
35516663Ssam 		vmunixctime = sb.st_ctime;
35616663Ssam 		vmunixino= sb.st_ino;
35716663Ssam 	}
3586460Swnj 	if (kmemf >= 0)
3596460Swnj 		(void) close(kmemf);
3606460Swnj loop:
36137295Sbostic 	if (nlist(_PATH_UNIX, nl)) {
36237295Sbostic 		syslog(LOG_WARNING, "%s: namelist botch", _PATH_UNIX);
3636460Swnj 		sleep(300);
3646460Swnj 		goto loop;
3656460Swnj 	}
36637295Sbostic 	kmemf = open(_PATH_KMEM, O_RDONLY, 0);
3676460Swnj 	if (kmemf < 0) {
36837295Sbostic 		syslog(LOG_ERR, "%s: %m", _PATH_KMEM);
36916673Sralph 		exit(1);
3706460Swnj 	}
37113187Ssam 	(void) lseek(kmemf, (long)nl[NL_BOOTTIME].n_value, L_SET);
37213187Ssam 	(void) read(kmemf, (char *)&mywd.wd_boottime,
37313187Ssam 	    sizeof (mywd.wd_boottime));
37412807Ssam 	mywd.wd_boottime = htonl(mywd.wd_boottime);
3756460Swnj }
37612236Ssam 
37712236Ssam /*
37812236Ssam  * Figure out device configuration and select
37912236Ssam  * networks which deserve status information.
38012236Ssam  */
38112236Ssam configure(s)
38212236Ssam 	int s;
38312236Ssam {
38412236Ssam 	char buf[BUFSIZ];
38512236Ssam 	struct ifconf ifc;
38612236Ssam 	struct ifreq ifreq, *ifr;
38712236Ssam 	struct sockaddr_in *sin;
38812236Ssam 	register struct neighbor *np;
38913187Ssam 	int n;
39012236Ssam 
39112236Ssam 	ifc.ifc_len = sizeof (buf);
39212236Ssam 	ifc.ifc_buf = buf;
39312236Ssam 	if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0) {
39416673Sralph 		syslog(LOG_ERR, "ioctl (get interface configuration)");
39512236Ssam 		return (0);
39612236Ssam 	}
39712236Ssam 	ifr = ifc.ifc_req;
39812236Ssam 	for (n = ifc.ifc_len / sizeof (struct ifreq); n > 0; n--, ifr++) {
39912236Ssam 		for (np = neighbors; np != NULL; np = np->n_next)
40012236Ssam 			if (np->n_name &&
40112236Ssam 			    strcmp(ifr->ifr_name, np->n_name) == 0)
40212236Ssam 				break;
40312236Ssam 		if (np != NULL)
40412236Ssam 			continue;
40512236Ssam 		ifreq = *ifr;
40612236Ssam 		np = (struct neighbor *)malloc(sizeof (*np));
40712236Ssam 		if (np == NULL)
40812236Ssam 			continue;
40912236Ssam 		np->n_name = malloc(strlen(ifr->ifr_name) + 1);
41012236Ssam 		if (np->n_name == NULL) {
41112236Ssam 			free((char *)np);
41212236Ssam 			continue;
41312236Ssam 		}
41412236Ssam 		strcpy(np->n_name, ifr->ifr_name);
41512236Ssam 		np->n_addrlen = sizeof (ifr->ifr_addr);
41612236Ssam 		np->n_addr = malloc(np->n_addrlen);
41712236Ssam 		if (np->n_addr == NULL) {
41812236Ssam 			free(np->n_name);
41912236Ssam 			free((char *)np);
42012236Ssam 			continue;
42112236Ssam 		}
42212236Ssam 		bcopy((char *)&ifr->ifr_addr, np->n_addr, np->n_addrlen);
42312236Ssam 		if (ioctl(s, SIOCGIFFLAGS, (char *)&ifreq) < 0) {
42416673Sralph 			syslog(LOG_ERR, "ioctl (get interface flags)");
42512236Ssam 			free((char *)np);
42612236Ssam 			continue;
42712236Ssam 		}
42821852Skarels 		if ((ifreq.ifr_flags & IFF_UP) == 0 ||
42921852Skarels 		    (ifreq.ifr_flags & (IFF_BROADCAST|IFF_POINTOPOINT)) == 0) {
43012236Ssam 			free((char *)np);
43112236Ssam 			continue;
43212236Ssam 		}
43312236Ssam 		np->n_flags = ifreq.ifr_flags;
43412236Ssam 		if (np->n_flags & IFF_POINTOPOINT) {
43512236Ssam 			if (ioctl(s, SIOCGIFDSTADDR, (char *)&ifreq) < 0) {
43616673Sralph 				syslog(LOG_ERR, "ioctl (get dstaddr)");
43712236Ssam 				free((char *)np);
43812236Ssam 				continue;
43912236Ssam 			}
44012236Ssam 			/* we assume addresses are all the same size */
44112236Ssam 			bcopy((char *)&ifreq.ifr_dstaddr,
44212236Ssam 			  np->n_addr, np->n_addrlen);
44312236Ssam 		}
44412236Ssam 		if (np->n_flags & IFF_BROADCAST) {
44521852Skarels 			if (ioctl(s, SIOCGIFBRDADDR, (char *)&ifreq) < 0) {
44621852Skarels 				syslog(LOG_ERR, "ioctl (get broadaddr)");
44721852Skarels 				free((char *)np);
44821852Skarels 				continue;
44921852Skarels 			}
45012236Ssam 			/* we assume addresses are all the same size */
45121852Skarels 			bcopy((char *)&ifreq.ifr_broadaddr,
45221852Skarels 			  np->n_addr, np->n_addrlen);
45312236Ssam 		}
45412236Ssam 		/* gag, wish we could get rid of Internet dependencies */
45512236Ssam 		sin = (struct sockaddr_in *)np->n_addr;
45612236Ssam 		sin->sin_port = sp->s_port;
45712236Ssam 		np->n_next = neighbors;
45812236Ssam 		neighbors = np;
45912236Ssam 	}
46012236Ssam 	return (1);
46112236Ssam }
46212807Ssam 
46312807Ssam #ifdef DEBUG
46412807Ssam sendto(s, buf, cc, flags, to, tolen)
46512807Ssam 	int s;
46612807Ssam 	char *buf;
46712807Ssam 	int cc, flags;
46812807Ssam 	char *to;
46912807Ssam 	int tolen;
47012807Ssam {
47112807Ssam 	register struct whod *w = (struct whod *)buf;
47212807Ssam 	register struct whoent *we;
47312807Ssam 	struct sockaddr_in *sin = (struct sockaddr_in *)to;
47412807Ssam 	char *interval();
47512807Ssam 
47612807Ssam 	printf("sendto %x.%d\n", ntohl(sin->sin_addr), ntohs(sin->sin_port));
47712807Ssam 	printf("hostname %s %s\n", w->wd_hostname,
47813187Ssam 	   interval(ntohl(w->wd_sendtime) - ntohl(w->wd_boottime), "  up"));
47912807Ssam 	printf("load %4.2f, %4.2f, %4.2f\n",
48013187Ssam 	    ntohl(w->wd_loadav[0]) / 100.0, ntohl(w->wd_loadav[1]) / 100.0,
48113187Ssam 	    ntohl(w->wd_loadav[2]) / 100.0);
48212807Ssam 	cc -= WHDRSIZE;
48312807Ssam 	for (we = w->wd_we, cc /= sizeof (struct whoent); cc > 0; cc--, we++) {
48413187Ssam 		time_t t = ntohl(we->we_utmp.out_time);
48512807Ssam 		printf("%-8.8s %s:%s %.12s",
48613187Ssam 			we->we_utmp.out_name,
48713187Ssam 			w->wd_hostname, we->we_utmp.out_line,
48813187Ssam 			ctime(&t)+4);
48913187Ssam 		we->we_idle = ntohl(we->we_idle) / 60;
49012807Ssam 		if (we->we_idle) {
49112807Ssam 			if (we->we_idle >= 100*60)
49212807Ssam 				we->we_idle = 100*60 - 1;
49312807Ssam 			if (we->we_idle >= 60)
49412807Ssam 				printf(" %2d", we->we_idle / 60);
49512807Ssam 			else
49612807Ssam 				printf("   ");
49712807Ssam 			printf(":%02d", we->we_idle % 60);
49812807Ssam 		}
49912807Ssam 		printf("\n");
50012807Ssam 	}
50112807Ssam }
50212807Ssam 
50312807Ssam char *
50412807Ssam interval(time, updown)
50512807Ssam 	int time;
50612807Ssam 	char *updown;
50712807Ssam {
50812807Ssam 	static char resbuf[32];
50912807Ssam 	int days, hours, minutes;
51012807Ssam 
51112807Ssam 	if (time < 0 || time > 3*30*24*60*60) {
51212807Ssam 		(void) sprintf(resbuf, "   %s ??:??", updown);
51312807Ssam 		return (resbuf);
51412807Ssam 	}
51512807Ssam 	minutes = (time + 59) / 60;		/* round to minutes */
51612807Ssam 	hours = minutes / 60; minutes %= 60;
51712807Ssam 	days = hours / 24; hours %= 24;
51812807Ssam 	if (days)
51912807Ssam 		(void) sprintf(resbuf, "%s %2d+%02d:%02d",
52012807Ssam 		    updown, days, hours, minutes);
52112807Ssam 	else
52212807Ssam 		(void) sprintf(resbuf, "%s    %2d:%02d",
52312807Ssam 		    updown, hours, minutes);
52412807Ssam 	return (resbuf);
52512807Ssam }
52612807Ssam #endif
527