xref: /csrg-svn/usr.sbin/rwhod/rwhod.c (revision 35392)
122549Sdist /*
2*35392Sbostic  * Copyright (c) 1983 The Regents of the University of California.
3*35392Sbostic  * All rights reserved.
4*35392Sbostic  *
5*35392Sbostic  * Redistribution and use in source and binary forms are permitted
6*35392Sbostic  * provided that the above copyright notice and this paragraph are
7*35392Sbostic  * duplicated in all such forms and that any documentation,
8*35392Sbostic  * advertising materials, and other materials related to such
9*35392Sbostic  * distribution and use acknowledge that the software was developed
10*35392Sbostic  * by the University of California, Berkeley.  The name of the
11*35392Sbostic  * University may not be used to endorse or promote products derived
12*35392Sbostic  * from this software without specific prior written permission.
13*35392Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14*35392Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15*35392Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1622549Sdist  */
1722549Sdist 
186460Swnj #ifndef lint
1922549Sdist char copyright[] =
20*35392Sbostic "@(#) Copyright (c) 1983 The Regents of the University of California.\n\
2122549Sdist  All rights reserved.\n";
22*35392Sbostic #endif /* not lint */
236460Swnj 
2422549Sdist #ifndef lint
25*35392Sbostic static char sccsid[] = "@(#)rwhod.c	5.11 (Berkeley) 08/25/88";
26*35392Sbostic #endif /* not lint */
2722549Sdist 
289215Ssam #include <sys/types.h>
299215Ssam #include <sys/socket.h>
309215Ssam #include <sys/stat.h>
319215Ssam #include <sys/ioctl.h>
3213187Ssam #include <sys/file.h>
339215Ssam 
3412236Ssam #include <net/if.h>
359215Ssam #include <netinet/in.h>
369215Ssam 
379215Ssam #include <nlist.h>
386460Swnj #include <stdio.h>
396460Swnj #include <signal.h>
406460Swnj #include <errno.h>
416460Swnj #include <utmp.h>
428486Ssam #include <ctype.h>
438486Ssam #include <netdb.h>
4416673Sralph #include <syslog.h>
4523535Smckusick #include <protocols/rwhod.h>
466460Swnj 
4715541Sralph /*
4815541Sralph  * Alarm interval. Don't forget to change the down time check in ruptime
4915541Sralph  * if this is changed.
5015541Sralph  */
5116673Sralph #define AL_INTERVAL (3 * 60)
5215541Sralph 
538486Ssam struct	sockaddr_in sin = { AF_INET };
546460Swnj 
556460Swnj extern	errno;
566460Swnj 
5712236Ssam char	myname[32];
586460Swnj 
596460Swnj struct	nlist nl[] = {
606460Swnj #define	NL_AVENRUN	0
616460Swnj 	{ "_avenrun" },
629215Ssam #define	NL_BOOTTIME	1
639215Ssam 	{ "_boottime" },
646460Swnj 	0
656460Swnj };
666460Swnj 
6712236Ssam /*
6812236Ssam  * We communicate with each neighbor in
6912236Ssam  * a list constructed at the time we're
7012236Ssam  * started up.  Neighbors are currently
7112236Ssam  * directly connected via a hardware interface.
7212236Ssam  */
7312236Ssam struct	neighbor {
7412236Ssam 	struct	neighbor *n_next;
7512236Ssam 	char	*n_name;		/* interface name */
7612236Ssam 	char	*n_addr;		/* who to send to */
7712236Ssam 	int	n_addrlen;		/* size of address */
7812236Ssam 	int	n_flags;		/* should forward?, interface flags */
7912236Ssam };
8012236Ssam 
8112236Ssam struct	neighbor *neighbors;
826460Swnj struct	whod mywd;
8312236Ssam struct	servent *sp;
846460Swnj int	s, utmpf, kmemf = -1;
856460Swnj 
8612236Ssam #define	WHDRSIZE	(sizeof (mywd) - sizeof (mywd.wd_we))
8712236Ssam #define	RWHODIR		"/usr/spool/rwho"
8811834Ssam 
896460Swnj int	onalrm();
9032449Sbostic char	*strcpy(), *malloc();
916460Swnj long	lseek();
926460Swnj int	getkmem();
9312236Ssam struct	in_addr inet_makeaddr();
946460Swnj 
956460Swnj main()
966460Swnj {
976460Swnj 	struct sockaddr_in from;
9826016Skarels 	struct stat st;
996460Swnj 	char path[64];
10026484Smckusick 	int on = 1;
10124745Sbloom 	char *cp;
10224745Sbloom 	extern char *index();
1036460Swnj 
10416673Sralph 	if (getuid()) {
10516673Sralph 		fprintf(stderr, "rwhod: not super user\n");
10616673Sralph 		exit(1);
10716673Sralph 	}
1088486Ssam 	sp = getservbyname("who", "udp");
1098486Ssam 	if (sp == 0) {
1108486Ssam 		fprintf(stderr, "rwhod: udp/who: unknown service\n");
1118486Ssam 		exit(1);
1128486Ssam 	}
1136460Swnj #ifndef DEBUG
1146460Swnj 	if (fork())
1156460Swnj 		exit(0);
1166460Swnj 	{ int s;
1176460Swnj 	  for (s = 0; s < 10; s++)
1186460Swnj 		(void) close(s);
1196460Swnj 	  (void) open("/", 0);
1206460Swnj 	  (void) dup2(0, 1);
1216460Swnj 	  (void) dup2(0, 2);
1226460Swnj 	  s = open("/dev/tty", 2);
1236460Swnj 	  if (s >= 0) {
1246460Swnj 		ioctl(s, TIOCNOTTY, 0);
1256460Swnj 		(void) close(s);
1266460Swnj 	  }
1276460Swnj 	}
1286460Swnj #endif
12926011Smckusick 	if (chdir(RWHODIR) < 0) {
13026011Smckusick 		perror(RWHODIR);
13126011Smckusick 		exit(1);
13226011Smckusick 	}
1336460Swnj 	(void) signal(SIGHUP, getkmem);
13424853Seric 	openlog("rwhod", LOG_PID, LOG_DAEMON);
13512236Ssam 	/*
13612236Ssam 	 * Establish host name as returned by system.
13712236Ssam 	 */
13812236Ssam 	if (gethostname(myname, sizeof (myname) - 1) < 0) {
13916673Sralph 		syslog(LOG_ERR, "gethostname: %m");
1406460Swnj 		exit(1);
1416460Swnj 	}
14224745Sbloom 	if ((cp = index(myname, '.')) != NULL)
14324745Sbloom 		*cp = '\0';
14412236Ssam 	strncpy(mywd.wd_hostname, myname, sizeof (myname) - 1);
14513187Ssam 	utmpf = open("/etc/utmp", O_RDONLY);
1466460Swnj 	if (utmpf < 0) {
1476460Swnj 		(void) close(creat("/etc/utmp", 0644));
14813187Ssam 		utmpf = open("/etc/utmp", O_RDONLY);
1496460Swnj 	}
1506460Swnj 	if (utmpf < 0) {
15116673Sralph 		syslog(LOG_ERR, "/etc/utmp: %m");
1526460Swnj 		exit(1);
1536460Swnj 	}
1546460Swnj 	getkmem();
15513187Ssam 	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
15616673Sralph 		syslog(LOG_ERR, "socket: %m");
1579215Ssam 		exit(1);
1586460Swnj 	}
15917155Ssam 	if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0) {
16017055Skarels 		syslog(LOG_ERR, "setsockopt SO_BROADCAST: %m");
16117055Skarels 		exit(1);
16217055Skarels 	}
16312236Ssam 	sin.sin_port = sp->s_port;
16413187Ssam 	if (bind(s, &sin, sizeof (sin)) < 0) {
16516673Sralph 		syslog(LOG_ERR, "bind: %m");
1669215Ssam 		exit(1);
1679215Ssam 	}
16812236Ssam 	if (!configure(s))
16912236Ssam 		exit(1);
17013027Ssam 	signal(SIGALRM, onalrm);
1716460Swnj 	onalrm();
1726460Swnj 	for (;;) {
1736460Swnj 		struct whod wd;
17412236Ssam 		int cc, whod, len = sizeof (from);
1756460Swnj 
17612236Ssam 		cc = recvfrom(s, (char *)&wd, sizeof (struct whod), 0,
17712236Ssam 			&from, &len);
1786460Swnj 		if (cc <= 0) {
1796460Swnj 			if (cc < 0 && errno != EINTR)
18016673Sralph 				syslog(LOG_WARNING, "recv: %m");
1816460Swnj 			continue;
1826460Swnj 		}
1838486Ssam 		if (from.sin_port != sp->s_port) {
18416673Sralph 			syslog(LOG_WARNING, "%d: bad from port",
1858486Ssam 				ntohs(from.sin_port));
1866460Swnj 			continue;
1876460Swnj 		}
1888486Ssam #ifdef notdef
1898486Ssam 		if (gethostbyname(wd.wd_hostname) == 0) {
19016673Sralph 			syslog(LOG_WARNING, "%s: unknown host",
1918486Ssam 				wd.wd_hostname);
1926460Swnj 			continue;
1936460Swnj 		}
1948486Ssam #endif
19512347Ssam 		if (wd.wd_vers != WHODVERSION)
19612347Ssam 			continue;
19712236Ssam 		if (wd.wd_type != WHODTYPE_STATUS)
19812236Ssam 			continue;
1998486Ssam 		if (!verify(wd.wd_hostname)) {
20016673Sralph 			syslog(LOG_WARNING, "malformed host name from %x",
2018486Ssam 				from.sin_addr);
2028486Ssam 			continue;
2038486Ssam 		}
20426011Smckusick 		(void) sprintf(path, "whod.%s", wd.wd_hostname);
20526016Skarels 		/*
20626016Skarels 		 * Rather than truncating and growing the file each time,
20726016Skarels 		 * use ftruncate if size is less than previous size.
20826016Skarels 		 */
20926016Skarels 		whod = open(path, O_WRONLY | O_CREAT, 0644);
2106460Swnj 		if (whod < 0) {
21116673Sralph 			syslog(LOG_WARNING, "%s: %m", path);
2126460Swnj 			continue;
2136460Swnj 		}
21411834Ssam #if vax || pdp11
21511834Ssam 		{
21613187Ssam 			int i, n = (cc - WHDRSIZE)/sizeof(struct whoent);
21711834Ssam 			struct whoent *we;
21811834Ssam 
21911834Ssam 			/* undo header byte swapping before writing to file */
22011834Ssam 			wd.wd_sendtime = ntohl(wd.wd_sendtime);
22111834Ssam 			for (i = 0; i < 3; i++)
22211834Ssam 				wd.wd_loadav[i] = ntohl(wd.wd_loadav[i]);
22311834Ssam 			wd.wd_boottime = ntohl(wd.wd_boottime);
22411834Ssam 			we = wd.wd_we;
22511834Ssam 			for (i = 0; i < n; i++) {
22611834Ssam 				we->we_idle = ntohl(we->we_idle);
22712872Ssam 				we->we_utmp.out_time =
22812872Ssam 				    ntohl(we->we_utmp.out_time);
22911834Ssam 				we++;
23011834Ssam 			}
23111834Ssam 		}
23211834Ssam #endif
2336460Swnj 		(void) time(&wd.wd_recvtime);
2346460Swnj 		(void) write(whod, (char *)&wd, cc);
23526016Skarels 		if (fstat(whod, &st) < 0 || st.st_size > cc)
23626016Skarels 			ftruncate(whod, cc);
2376460Swnj 		(void) close(whod);
2386460Swnj 	}
2396460Swnj }
2406460Swnj 
2418486Ssam /*
2428486Ssam  * Check out host name for unprintables
2438486Ssam  * and other funnies before allowing a file
2448486Ssam  * to be created.  Sorry, but blanks aren't allowed.
2458486Ssam  */
2468486Ssam verify(name)
2478486Ssam 	register char *name;
2488486Ssam {
2498486Ssam 	register int size = 0;
2508486Ssam 
2518486Ssam 	while (*name) {
25215214Sleres 		if (!isascii(*name) || !(isalnum(*name) || ispunct(*name)))
2538486Ssam 			return (0);
2548486Ssam 		name++, size++;
2558486Ssam 	}
2568486Ssam 	return (size > 0);
2578486Ssam }
2588486Ssam 
2596460Swnj int	utmptime;
2606460Swnj int	utmpent;
26122546Sbloom int	utmpsize = 0;
26222546Sbloom struct	utmp *utmp;
2636460Swnj int	alarmcount;
2646460Swnj 
2656460Swnj onalrm()
2666460Swnj {
2676460Swnj 	register int i;
2686460Swnj 	struct stat stb;
2696577Ssam 	register struct whoent *we = mywd.wd_we, *wlast;
2706460Swnj 	int cc;
2716460Swnj 	double avenrun[3];
2726460Swnj 	time_t now = time(0);
27312236Ssam 	register struct neighbor *np;
2746460Swnj 
2756460Swnj 	if (alarmcount % 10 == 0)
2766460Swnj 		getkmem();
2776460Swnj 	alarmcount++;
2786460Swnj 	(void) fstat(utmpf, &stb);
27922546Sbloom 	if ((stb.st_mtime != utmptime) || (stb.st_size > utmpsize)) {
28017303Stef 		utmptime = stb.st_mtime;
28122546Sbloom 		if (stb.st_size > utmpsize) {
28222546Sbloom 			utmpsize = stb.st_size + 10 * sizeof(struct utmp);
28322546Sbloom 			if (utmp)
28422546Sbloom 				utmp = (struct utmp *)realloc(utmp, utmpsize);
28522546Sbloom 			else
28622546Sbloom 				utmp = (struct utmp *)malloc(utmpsize);
28722546Sbloom 			if (! utmp) {
28822546Sbloom 				fprintf(stderr, "rwhod: malloc failed\n");
28922546Sbloom 				utmpsize = 0;
29022546Sbloom 				goto done;
29122546Sbloom 			}
29222546Sbloom 		}
29313187Ssam 		(void) lseek(utmpf, (long)0, L_SET);
29422546Sbloom 		cc = read(utmpf, (char *)utmp, stb.st_size);
2956460Swnj 		if (cc < 0) {
2966460Swnj 			perror("/etc/utmp");
29713469Ssam 			goto done;
2986460Swnj 		}
29912236Ssam 		wlast = &mywd.wd_we[1024 / sizeof (struct whoent) - 1];
3006460Swnj 		utmpent = cc / sizeof (struct utmp);
3016460Swnj 		for (i = 0; i < utmpent; i++)
3026460Swnj 			if (utmp[i].ut_name[0]) {
30312807Ssam 				bcopy(utmp[i].ut_line, we->we_utmp.out_line,
30412807Ssam 				   sizeof (utmp[i].ut_line));
30512807Ssam 				bcopy(utmp[i].ut_name, we->we_utmp.out_name,
30612807Ssam 				   sizeof (utmp[i].ut_name));
30712807Ssam 				we->we_utmp.out_time = htonl(utmp[i].ut_time);
3086577Ssam 				if (we >= wlast)
3096577Ssam 					break;
3106460Swnj 				we++;
3116460Swnj 			}
3126460Swnj 		utmpent = we - mywd.wd_we;
3136460Swnj 	}
31426484Smckusick 
31526484Smckusick 	/*
31626484Smckusick 	 * The test on utmpent looks silly---after all, if no one is
31726484Smckusick 	 * logged on, why worry about efficiency?---but is useful on
31826484Smckusick 	 * (e.g.) compute servers.
31926484Smckusick 	 */
32026484Smckusick 	if (utmpent && chdir("/dev")) {
32126484Smckusick 		syslog(LOG_ERR, "chdir(/dev): %m");
32226484Smckusick 		exit(1);
32326484Smckusick 	}
3246460Swnj 	we = mywd.wd_we;
3256460Swnj 	for (i = 0; i < utmpent; i++) {
32626484Smckusick 		if (stat(we->we_utmp.out_line, &stb) >= 0)
32712807Ssam 			we->we_idle = htonl(now - stb.st_atime);
3286460Swnj 		we++;
3296460Swnj 	}
33013187Ssam 	(void) lseek(kmemf, (long)nl[NL_AVENRUN].n_value, L_SET);
3316460Swnj 	(void) read(kmemf, (char *)avenrun, sizeof (avenrun));
3326460Swnj 	for (i = 0; i < 3; i++)
33312873Ssam 		mywd.wd_loadav[i] = htonl((u_long)(avenrun[i] * 100));
3346460Swnj 	cc = (char *)we - (char *)&mywd;
33512807Ssam 	mywd.wd_sendtime = htonl(time(0));
33612236Ssam 	mywd.wd_vers = WHODVERSION;
33712236Ssam 	mywd.wd_type = WHODTYPE_STATUS;
33812236Ssam 	for (np = neighbors; np != NULL; np = np->n_next)
33912236Ssam 		(void) sendto(s, (char *)&mywd, cc, 0,
34012236Ssam 			np->n_addr, np->n_addrlen);
34126484Smckusick 	if (utmpent && chdir(RWHODIR)) {
34226484Smckusick 		syslog(LOG_ERR, "chdir(%s): %m", RWHODIR);
34326484Smckusick 		exit(1);
34426484Smckusick 	}
34513469Ssam done:
34615541Sralph 	(void) alarm(AL_INTERVAL);
3476460Swnj }
3486460Swnj 
3496460Swnj getkmem()
3506460Swnj {
35116663Ssam 	static ino_t vmunixino;
35216663Ssam 	static time_t vmunixctime;
35316663Ssam 	struct stat sb;
3546460Swnj 
35516663Ssam 	if (stat("/vmunix", &sb) < 0) {
35616663Ssam 		if (vmunixctime)
35716663Ssam 			return;
35816663Ssam 	} else {
35916663Ssam 		if (sb.st_ctime == vmunixctime && sb.st_ino == vmunixino)
36016663Ssam 			return;
36116663Ssam 		vmunixctime = sb.st_ctime;
36216663Ssam 		vmunixino= sb.st_ino;
36316663Ssam 	}
3646460Swnj 	if (kmemf >= 0)
3656460Swnj 		(void) close(kmemf);
3666460Swnj loop:
36716673Sralph 	if (nlist("/vmunix", nl)) {
36816673Sralph 		syslog(LOG_WARNING, "/vmunix namelist botch");
3696460Swnj 		sleep(300);
3706460Swnj 		goto loop;
3716460Swnj 	}
37213187Ssam 	kmemf = open("/dev/kmem", O_RDONLY);
3736460Swnj 	if (kmemf < 0) {
37416673Sralph 		syslog(LOG_ERR, "/dev/kmem: %m");
37516673Sralph 		exit(1);
3766460Swnj 	}
37713187Ssam 	(void) lseek(kmemf, (long)nl[NL_BOOTTIME].n_value, L_SET);
37813187Ssam 	(void) read(kmemf, (char *)&mywd.wd_boottime,
37913187Ssam 	    sizeof (mywd.wd_boottime));
38012807Ssam 	mywd.wd_boottime = htonl(mywd.wd_boottime);
3816460Swnj }
38212236Ssam 
38312236Ssam /*
38412236Ssam  * Figure out device configuration and select
38512236Ssam  * networks which deserve status information.
38612236Ssam  */
38712236Ssam configure(s)
38812236Ssam 	int s;
38912236Ssam {
39012236Ssam 	char buf[BUFSIZ];
39112236Ssam 	struct ifconf ifc;
39212236Ssam 	struct ifreq ifreq, *ifr;
39312236Ssam 	struct sockaddr_in *sin;
39412236Ssam 	register struct neighbor *np;
39513187Ssam 	int n;
39612236Ssam 
39712236Ssam 	ifc.ifc_len = sizeof (buf);
39812236Ssam 	ifc.ifc_buf = buf;
39912236Ssam 	if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0) {
40016673Sralph 		syslog(LOG_ERR, "ioctl (get interface configuration)");
40112236Ssam 		return (0);
40212236Ssam 	}
40312236Ssam 	ifr = ifc.ifc_req;
40412236Ssam 	for (n = ifc.ifc_len / sizeof (struct ifreq); n > 0; n--, ifr++) {
40512236Ssam 		for (np = neighbors; np != NULL; np = np->n_next)
40612236Ssam 			if (np->n_name &&
40712236Ssam 			    strcmp(ifr->ifr_name, np->n_name) == 0)
40812236Ssam 				break;
40912236Ssam 		if (np != NULL)
41012236Ssam 			continue;
41112236Ssam 		ifreq = *ifr;
41212236Ssam 		np = (struct neighbor *)malloc(sizeof (*np));
41312236Ssam 		if (np == NULL)
41412236Ssam 			continue;
41512236Ssam 		np->n_name = malloc(strlen(ifr->ifr_name) + 1);
41612236Ssam 		if (np->n_name == NULL) {
41712236Ssam 			free((char *)np);
41812236Ssam 			continue;
41912236Ssam 		}
42012236Ssam 		strcpy(np->n_name, ifr->ifr_name);
42112236Ssam 		np->n_addrlen = sizeof (ifr->ifr_addr);
42212236Ssam 		np->n_addr = malloc(np->n_addrlen);
42312236Ssam 		if (np->n_addr == NULL) {
42412236Ssam 			free(np->n_name);
42512236Ssam 			free((char *)np);
42612236Ssam 			continue;
42712236Ssam 		}
42812236Ssam 		bcopy((char *)&ifr->ifr_addr, np->n_addr, np->n_addrlen);
42912236Ssam 		if (ioctl(s, SIOCGIFFLAGS, (char *)&ifreq) < 0) {
43016673Sralph 			syslog(LOG_ERR, "ioctl (get interface flags)");
43112236Ssam 			free((char *)np);
43212236Ssam 			continue;
43312236Ssam 		}
43421852Skarels 		if ((ifreq.ifr_flags & IFF_UP) == 0 ||
43521852Skarels 		    (ifreq.ifr_flags & (IFF_BROADCAST|IFF_POINTOPOINT)) == 0) {
43612236Ssam 			free((char *)np);
43712236Ssam 			continue;
43812236Ssam 		}
43912236Ssam 		np->n_flags = ifreq.ifr_flags;
44012236Ssam 		if (np->n_flags & IFF_POINTOPOINT) {
44112236Ssam 			if (ioctl(s, SIOCGIFDSTADDR, (char *)&ifreq) < 0) {
44216673Sralph 				syslog(LOG_ERR, "ioctl (get dstaddr)");
44312236Ssam 				free((char *)np);
44412236Ssam 				continue;
44512236Ssam 			}
44612236Ssam 			/* we assume addresses are all the same size */
44712236Ssam 			bcopy((char *)&ifreq.ifr_dstaddr,
44812236Ssam 			  np->n_addr, np->n_addrlen);
44912236Ssam 		}
45012236Ssam 		if (np->n_flags & IFF_BROADCAST) {
45121852Skarels 			if (ioctl(s, SIOCGIFBRDADDR, (char *)&ifreq) < 0) {
45221852Skarels 				syslog(LOG_ERR, "ioctl (get broadaddr)");
45321852Skarels 				free((char *)np);
45421852Skarels 				continue;
45521852Skarels 			}
45612236Ssam 			/* we assume addresses are all the same size */
45721852Skarels 			bcopy((char *)&ifreq.ifr_broadaddr,
45821852Skarels 			  np->n_addr, np->n_addrlen);
45912236Ssam 		}
46012236Ssam 		/* gag, wish we could get rid of Internet dependencies */
46112236Ssam 		sin = (struct sockaddr_in *)np->n_addr;
46212236Ssam 		sin->sin_port = sp->s_port;
46312236Ssam 		np->n_next = neighbors;
46412236Ssam 		neighbors = np;
46512236Ssam 	}
46612236Ssam 	return (1);
46712236Ssam }
46812807Ssam 
46912807Ssam #ifdef DEBUG
47012807Ssam sendto(s, buf, cc, flags, to, tolen)
47112807Ssam 	int s;
47212807Ssam 	char *buf;
47312807Ssam 	int cc, flags;
47412807Ssam 	char *to;
47512807Ssam 	int tolen;
47612807Ssam {
47712807Ssam 	register struct whod *w = (struct whod *)buf;
47812807Ssam 	register struct whoent *we;
47912807Ssam 	struct sockaddr_in *sin = (struct sockaddr_in *)to;
48012807Ssam 	char *interval();
48112807Ssam 
48212807Ssam 	printf("sendto %x.%d\n", ntohl(sin->sin_addr), ntohs(sin->sin_port));
48312807Ssam 	printf("hostname %s %s\n", w->wd_hostname,
48413187Ssam 	   interval(ntohl(w->wd_sendtime) - ntohl(w->wd_boottime), "  up"));
48512807Ssam 	printf("load %4.2f, %4.2f, %4.2f\n",
48613187Ssam 	    ntohl(w->wd_loadav[0]) / 100.0, ntohl(w->wd_loadav[1]) / 100.0,
48713187Ssam 	    ntohl(w->wd_loadav[2]) / 100.0);
48812807Ssam 	cc -= WHDRSIZE;
48912807Ssam 	for (we = w->wd_we, cc /= sizeof (struct whoent); cc > 0; cc--, we++) {
49013187Ssam 		time_t t = ntohl(we->we_utmp.out_time);
49112807Ssam 		printf("%-8.8s %s:%s %.12s",
49213187Ssam 			we->we_utmp.out_name,
49313187Ssam 			w->wd_hostname, we->we_utmp.out_line,
49413187Ssam 			ctime(&t)+4);
49513187Ssam 		we->we_idle = ntohl(we->we_idle) / 60;
49612807Ssam 		if (we->we_idle) {
49712807Ssam 			if (we->we_idle >= 100*60)
49812807Ssam 				we->we_idle = 100*60 - 1;
49912807Ssam 			if (we->we_idle >= 60)
50012807Ssam 				printf(" %2d", we->we_idle / 60);
50112807Ssam 			else
50212807Ssam 				printf("   ");
50312807Ssam 			printf(":%02d", we->we_idle % 60);
50412807Ssam 		}
50512807Ssam 		printf("\n");
50612807Ssam 	}
50712807Ssam }
50812807Ssam 
50912807Ssam char *
51012807Ssam interval(time, updown)
51112807Ssam 	int time;
51212807Ssam 	char *updown;
51312807Ssam {
51412807Ssam 	static char resbuf[32];
51512807Ssam 	int days, hours, minutes;
51612807Ssam 
51712807Ssam 	if (time < 0 || time > 3*30*24*60*60) {
51812807Ssam 		(void) sprintf(resbuf, "   %s ??:??", updown);
51912807Ssam 		return (resbuf);
52012807Ssam 	}
52112807Ssam 	minutes = (time + 59) / 60;		/* round to minutes */
52212807Ssam 	hours = minutes / 60; minutes %= 60;
52312807Ssam 	days = hours / 24; hours %= 24;
52412807Ssam 	if (days)
52512807Ssam 		(void) sprintf(resbuf, "%s %2d+%02d:%02d",
52612807Ssam 		    updown, days, hours, minutes);
52712807Ssam 	else
52812807Ssam 		(void) sprintf(resbuf, "%s    %2d:%02d",
52912807Ssam 		    updown, hours, minutes);
53012807Ssam 	return (resbuf);
53112807Ssam }
53212807Ssam #endif
533