xref: /csrg-svn/usr.sbin/rwhod/rwhod.c (revision 37971)
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*37971Sbostic static char sccsid[] = "@(#)rwhod.c	5.13 (Berkeley) 05/11/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>
46*37971Sbostic #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[] = {
596460Swnj #define	NL_AVENRUN	0
606460Swnj 	{ "_avenrun" },
619215Ssam #define	NL_BOOTTIME	1
629215Ssam 	{ "_boottime" },
636460Swnj 	0
646460Swnj };
656460Swnj 
6612236Ssam /*
6712236Ssam  * We communicate with each neighbor in
6812236Ssam  * a list constructed at the time we're
6912236Ssam  * started up.  Neighbors are currently
7012236Ssam  * directly connected via a hardware interface.
7112236Ssam  */
7212236Ssam struct	neighbor {
7312236Ssam 	struct	neighbor *n_next;
7412236Ssam 	char	*n_name;		/* interface name */
7512236Ssam 	char	*n_addr;		/* who to send to */
7612236Ssam 	int	n_addrlen;		/* size of address */
7712236Ssam 	int	n_flags;		/* should forward?, interface flags */
7812236Ssam };
7912236Ssam 
8012236Ssam struct	neighbor *neighbors;
816460Swnj struct	whod mywd;
8212236Ssam struct	servent *sp;
836460Swnj int	s, utmpf, kmemf = -1;
846460Swnj 
8512236Ssam #define	WHDRSIZE	(sizeof (mywd) - sizeof (mywd.wd_we))
8611834Ssam 
8737295Sbostic extern int errno;
886460Swnj int	onalrm();
8932449Sbostic char	*strcpy(), *malloc();
906460Swnj long	lseek();
916460Swnj int	getkmem();
9212236Ssam struct	in_addr inet_makeaddr();
936460Swnj 
946460Swnj main()
956460Swnj {
966460Swnj 	struct sockaddr_in from;
9726016Skarels 	struct stat st;
986460Swnj 	char path[64];
9926484Smckusick 	int on = 1;
10037295Sbostic 	char *cp, *index(), *strerror();
1016460Swnj 
10216673Sralph 	if (getuid()) {
10316673Sralph 		fprintf(stderr, "rwhod: not super user\n");
10416673Sralph 		exit(1);
10516673Sralph 	}
1068486Ssam 	sp = getservbyname("who", "udp");
1078486Ssam 	if (sp == 0) {
1088486Ssam 		fprintf(stderr, "rwhod: udp/who: unknown service\n");
1098486Ssam 		exit(1);
1108486Ssam 	}
1116460Swnj #ifndef DEBUG
1126460Swnj 	if (fork())
1136460Swnj 		exit(0);
1146460Swnj 	{ int s;
1156460Swnj 	  for (s = 0; s < 10; s++)
1166460Swnj 		(void) close(s);
1176460Swnj 	  (void) open("/", 0);
1186460Swnj 	  (void) dup2(0, 1);
1196460Swnj 	  (void) dup2(0, 2);
120*37971Sbostic 	  s = open(_PATH_TTY, 2);
1216460Swnj 	  if (s >= 0) {
1226460Swnj 		ioctl(s, TIOCNOTTY, 0);
1236460Swnj 		(void) close(s);
1246460Swnj 	  }
1256460Swnj 	}
1266460Swnj #endif
12737295Sbostic 	if (chdir(_PATH_RWHODIR) < 0) {
12837295Sbostic 		(void)fprintf(stderr, "rwhod: %s: %s\n",
12937295Sbostic 		    _PATH_RWHODIR, strerror(errno));
13026011Smckusick 		exit(1);
13126011Smckusick 	}
1326460Swnj 	(void) signal(SIGHUP, getkmem);
13324853Seric 	openlog("rwhod", LOG_PID, LOG_DAEMON);
13412236Ssam 	/*
13512236Ssam 	 * Establish host name as returned by system.
13612236Ssam 	 */
13712236Ssam 	if (gethostname(myname, sizeof (myname) - 1) < 0) {
13816673Sralph 		syslog(LOG_ERR, "gethostname: %m");
1396460Swnj 		exit(1);
1406460Swnj 	}
14124745Sbloom 	if ((cp = index(myname, '.')) != NULL)
14224745Sbloom 		*cp = '\0';
14312236Ssam 	strncpy(mywd.wd_hostname, myname, sizeof (myname) - 1);
14437295Sbostic 	utmpf = open(_PATH_UTMP, O_RDONLY|O_CREAT, 0644);
1456460Swnj 	if (utmpf < 0) {
14637295Sbostic 		syslog(LOG_ERR, "%s: %m", _PATH_UTMP);
1476460Swnj 		exit(1);
1486460Swnj 	}
1496460Swnj 	getkmem();
15013187Ssam 	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
15116673Sralph 		syslog(LOG_ERR, "socket: %m");
1529215Ssam 		exit(1);
1536460Swnj 	}
15417155Ssam 	if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0) {
15517055Skarels 		syslog(LOG_ERR, "setsockopt SO_BROADCAST: %m");
15617055Skarels 		exit(1);
15717055Skarels 	}
15812236Ssam 	sin.sin_port = sp->s_port;
15913187Ssam 	if (bind(s, &sin, sizeof (sin)) < 0) {
16016673Sralph 		syslog(LOG_ERR, "bind: %m");
1619215Ssam 		exit(1);
1629215Ssam 	}
16312236Ssam 	if (!configure(s))
16412236Ssam 		exit(1);
16513027Ssam 	signal(SIGALRM, onalrm);
1666460Swnj 	onalrm();
1676460Swnj 	for (;;) {
1686460Swnj 		struct whod wd;
16912236Ssam 		int cc, whod, len = sizeof (from);
1706460Swnj 
17112236Ssam 		cc = recvfrom(s, (char *)&wd, sizeof (struct whod), 0,
17212236Ssam 			&from, &len);
1736460Swnj 		if (cc <= 0) {
1746460Swnj 			if (cc < 0 && errno != EINTR)
17516673Sralph 				syslog(LOG_WARNING, "recv: %m");
1766460Swnj 			continue;
1776460Swnj 		}
1788486Ssam 		if (from.sin_port != sp->s_port) {
17916673Sralph 			syslog(LOG_WARNING, "%d: bad from port",
1808486Ssam 				ntohs(from.sin_port));
1816460Swnj 			continue;
1826460Swnj 		}
1838486Ssam #ifdef notdef
1848486Ssam 		if (gethostbyname(wd.wd_hostname) == 0) {
18516673Sralph 			syslog(LOG_WARNING, "%s: unknown host",
1868486Ssam 				wd.wd_hostname);
1876460Swnj 			continue;
1886460Swnj 		}
1898486Ssam #endif
19012347Ssam 		if (wd.wd_vers != WHODVERSION)
19112347Ssam 			continue;
19212236Ssam 		if (wd.wd_type != WHODTYPE_STATUS)
19312236Ssam 			continue;
1948486Ssam 		if (!verify(wd.wd_hostname)) {
19516673Sralph 			syslog(LOG_WARNING, "malformed host name from %x",
1968486Ssam 				from.sin_addr);
1978486Ssam 			continue;
1988486Ssam 		}
19926011Smckusick 		(void) sprintf(path, "whod.%s", wd.wd_hostname);
20026016Skarels 		/*
20126016Skarels 		 * Rather than truncating and growing the file each time,
20226016Skarels 		 * use ftruncate if size is less than previous size.
20326016Skarels 		 */
20426016Skarels 		whod = open(path, O_WRONLY | O_CREAT, 0644);
2056460Swnj 		if (whod < 0) {
20616673Sralph 			syslog(LOG_WARNING, "%s: %m", path);
2076460Swnj 			continue;
2086460Swnj 		}
20911834Ssam #if vax || pdp11
21011834Ssam 		{
21113187Ssam 			int i, n = (cc - WHDRSIZE)/sizeof(struct whoent);
21211834Ssam 			struct whoent *we;
21311834Ssam 
21411834Ssam 			/* undo header byte swapping before writing to file */
21511834Ssam 			wd.wd_sendtime = ntohl(wd.wd_sendtime);
21611834Ssam 			for (i = 0; i < 3; i++)
21711834Ssam 				wd.wd_loadav[i] = ntohl(wd.wd_loadav[i]);
21811834Ssam 			wd.wd_boottime = ntohl(wd.wd_boottime);
21911834Ssam 			we = wd.wd_we;
22011834Ssam 			for (i = 0; i < n; i++) {
22111834Ssam 				we->we_idle = ntohl(we->we_idle);
22212872Ssam 				we->we_utmp.out_time =
22312872Ssam 				    ntohl(we->we_utmp.out_time);
22411834Ssam 				we++;
22511834Ssam 			}
22611834Ssam 		}
22711834Ssam #endif
2286460Swnj 		(void) time(&wd.wd_recvtime);
2296460Swnj 		(void) write(whod, (char *)&wd, cc);
23026016Skarels 		if (fstat(whod, &st) < 0 || st.st_size > cc)
23126016Skarels 			ftruncate(whod, cc);
2326460Swnj 		(void) close(whod);
2336460Swnj 	}
2346460Swnj }
2356460Swnj 
2368486Ssam /*
2378486Ssam  * Check out host name for unprintables
2388486Ssam  * and other funnies before allowing a file
2398486Ssam  * to be created.  Sorry, but blanks aren't allowed.
2408486Ssam  */
2418486Ssam verify(name)
2428486Ssam 	register char *name;
2438486Ssam {
2448486Ssam 	register int size = 0;
2458486Ssam 
2468486Ssam 	while (*name) {
24715214Sleres 		if (!isascii(*name) || !(isalnum(*name) || ispunct(*name)))
2488486Ssam 			return (0);
2498486Ssam 		name++, size++;
2508486Ssam 	}
2518486Ssam 	return (size > 0);
2528486Ssam }
2538486Ssam 
2546460Swnj int	utmptime;
2556460Swnj int	utmpent;
25622546Sbloom int	utmpsize = 0;
25722546Sbloom struct	utmp *utmp;
2586460Swnj int	alarmcount;
2596460Swnj 
2606460Swnj onalrm()
2616460Swnj {
26237295Sbostic 	register struct neighbor *np;
26337295Sbostic 	register struct whoent *we = mywd.wd_we, *wlast;
2646460Swnj 	register int i;
2656460Swnj 	struct stat stb;
2666460Swnj 	int cc;
2676460Swnj 	double avenrun[3];
26837295Sbostic 	time_t now = time((time_t *)NULL);
26937295Sbostic 	char *strerror();
2706460Swnj 
2716460Swnj 	if (alarmcount % 10 == 0)
2726460Swnj 		getkmem();
2736460Swnj 	alarmcount++;
2746460Swnj 	(void) fstat(utmpf, &stb);
27522546Sbloom 	if ((stb.st_mtime != utmptime) || (stb.st_size > utmpsize)) {
27617303Stef 		utmptime = stb.st_mtime;
27722546Sbloom 		if (stb.st_size > utmpsize) {
27822546Sbloom 			utmpsize = stb.st_size + 10 * sizeof(struct utmp);
27922546Sbloom 			if (utmp)
28022546Sbloom 				utmp = (struct utmp *)realloc(utmp, utmpsize);
28122546Sbloom 			else
28222546Sbloom 				utmp = (struct utmp *)malloc(utmpsize);
28322546Sbloom 			if (! utmp) {
28422546Sbloom 				fprintf(stderr, "rwhod: malloc failed\n");
28522546Sbloom 				utmpsize = 0;
28622546Sbloom 				goto done;
28722546Sbloom 			}
28822546Sbloom 		}
28913187Ssam 		(void) lseek(utmpf, (long)0, L_SET);
29022546Sbloom 		cc = read(utmpf, (char *)utmp, stb.st_size);
2916460Swnj 		if (cc < 0) {
29237295Sbostic 			fprintf(stderr, "rwhod: %s: %s\n",
29337295Sbostic 			    _PATH_UTMP, strerror(errno));
29413469Ssam 			goto done;
2956460Swnj 		}
29612236Ssam 		wlast = &mywd.wd_we[1024 / sizeof (struct whoent) - 1];
2976460Swnj 		utmpent = cc / sizeof (struct utmp);
2986460Swnj 		for (i = 0; i < utmpent; i++)
2996460Swnj 			if (utmp[i].ut_name[0]) {
30012807Ssam 				bcopy(utmp[i].ut_line, we->we_utmp.out_line,
30112807Ssam 				   sizeof (utmp[i].ut_line));
30212807Ssam 				bcopy(utmp[i].ut_name, we->we_utmp.out_name,
30312807Ssam 				   sizeof (utmp[i].ut_name));
30412807Ssam 				we->we_utmp.out_time = htonl(utmp[i].ut_time);
3056577Ssam 				if (we >= wlast)
3066577Ssam 					break;
3076460Swnj 				we++;
3086460Swnj 			}
3096460Swnj 		utmpent = we - mywd.wd_we;
3106460Swnj 	}
31126484Smckusick 
31226484Smckusick 	/*
31326484Smckusick 	 * The test on utmpent looks silly---after all, if no one is
31426484Smckusick 	 * logged on, why worry about efficiency?---but is useful on
31526484Smckusick 	 * (e.g.) compute servers.
31626484Smckusick 	 */
317*37971Sbostic 	if (utmpent && chdir(_PATH_DEV)) {
318*37971Sbostic 		syslog(LOG_ERR, "chdir(%s): %m", _PATH_DEV);
31926484Smckusick 		exit(1);
32026484Smckusick 	}
3216460Swnj 	we = mywd.wd_we;
3226460Swnj 	for (i = 0; i < utmpent; i++) {
32326484Smckusick 		if (stat(we->we_utmp.out_line, &stb) >= 0)
32412807Ssam 			we->we_idle = htonl(now - stb.st_atime);
3256460Swnj 		we++;
3266460Swnj 	}
32713187Ssam 	(void) lseek(kmemf, (long)nl[NL_AVENRUN].n_value, L_SET);
3286460Swnj 	(void) read(kmemf, (char *)avenrun, sizeof (avenrun));
3296460Swnj 	for (i = 0; i < 3; i++)
33012873Ssam 		mywd.wd_loadav[i] = htonl((u_long)(avenrun[i] * 100));
3316460Swnj 	cc = (char *)we - (char *)&mywd;
33212807Ssam 	mywd.wd_sendtime = htonl(time(0));
33312236Ssam 	mywd.wd_vers = WHODVERSION;
33412236Ssam 	mywd.wd_type = WHODTYPE_STATUS;
33512236Ssam 	for (np = neighbors; np != NULL; np = np->n_next)
33612236Ssam 		(void) sendto(s, (char *)&mywd, cc, 0,
33712236Ssam 			np->n_addr, np->n_addrlen);
33837295Sbostic 	if (utmpent && chdir(_PATH_RWHODIR)) {
33937295Sbostic 		syslog(LOG_ERR, "chdir(%s): %m", _PATH_RWHODIR);
34026484Smckusick 		exit(1);
34126484Smckusick 	}
34213469Ssam done:
34315541Sralph 	(void) alarm(AL_INTERVAL);
3446460Swnj }
3456460Swnj 
3466460Swnj getkmem()
3476460Swnj {
34816663Ssam 	static ino_t vmunixino;
34916663Ssam 	static time_t vmunixctime;
35016663Ssam 	struct stat sb;
3516460Swnj 
35237295Sbostic 	if (stat(_PATH_UNIX, &sb) < 0) {
35316663Ssam 		if (vmunixctime)
35416663Ssam 			return;
35516663Ssam 	} else {
35616663Ssam 		if (sb.st_ctime == vmunixctime && sb.st_ino == vmunixino)
35716663Ssam 			return;
35816663Ssam 		vmunixctime = sb.st_ctime;
35916663Ssam 		vmunixino= sb.st_ino;
36016663Ssam 	}
3616460Swnj 	if (kmemf >= 0)
3626460Swnj 		(void) close(kmemf);
3636460Swnj loop:
36437295Sbostic 	if (nlist(_PATH_UNIX, nl)) {
36537295Sbostic 		syslog(LOG_WARNING, "%s: namelist botch", _PATH_UNIX);
3666460Swnj 		sleep(300);
3676460Swnj 		goto loop;
3686460Swnj 	}
36937295Sbostic 	kmemf = open(_PATH_KMEM, O_RDONLY, 0);
3706460Swnj 	if (kmemf < 0) {
37137295Sbostic 		syslog(LOG_ERR, "%s: %m", _PATH_KMEM);
37216673Sralph 		exit(1);
3736460Swnj 	}
37413187Ssam 	(void) lseek(kmemf, (long)nl[NL_BOOTTIME].n_value, L_SET);
37513187Ssam 	(void) read(kmemf, (char *)&mywd.wd_boottime,
37613187Ssam 	    sizeof (mywd.wd_boottime));
37712807Ssam 	mywd.wd_boottime = htonl(mywd.wd_boottime);
3786460Swnj }
37912236Ssam 
38012236Ssam /*
38112236Ssam  * Figure out device configuration and select
38212236Ssam  * networks which deserve status information.
38312236Ssam  */
38412236Ssam configure(s)
38512236Ssam 	int s;
38612236Ssam {
38712236Ssam 	char buf[BUFSIZ];
38812236Ssam 	struct ifconf ifc;
38912236Ssam 	struct ifreq ifreq, *ifr;
39012236Ssam 	struct sockaddr_in *sin;
39112236Ssam 	register struct neighbor *np;
39213187Ssam 	int n;
39312236Ssam 
39412236Ssam 	ifc.ifc_len = sizeof (buf);
39512236Ssam 	ifc.ifc_buf = buf;
39612236Ssam 	if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0) {
39716673Sralph 		syslog(LOG_ERR, "ioctl (get interface configuration)");
39812236Ssam 		return (0);
39912236Ssam 	}
40012236Ssam 	ifr = ifc.ifc_req;
40112236Ssam 	for (n = ifc.ifc_len / sizeof (struct ifreq); n > 0; n--, ifr++) {
40212236Ssam 		for (np = neighbors; np != NULL; np = np->n_next)
40312236Ssam 			if (np->n_name &&
40412236Ssam 			    strcmp(ifr->ifr_name, np->n_name) == 0)
40512236Ssam 				break;
40612236Ssam 		if (np != NULL)
40712236Ssam 			continue;
40812236Ssam 		ifreq = *ifr;
40912236Ssam 		np = (struct neighbor *)malloc(sizeof (*np));
41012236Ssam 		if (np == NULL)
41112236Ssam 			continue;
41212236Ssam 		np->n_name = malloc(strlen(ifr->ifr_name) + 1);
41312236Ssam 		if (np->n_name == NULL) {
41412236Ssam 			free((char *)np);
41512236Ssam 			continue;
41612236Ssam 		}
41712236Ssam 		strcpy(np->n_name, ifr->ifr_name);
41812236Ssam 		np->n_addrlen = sizeof (ifr->ifr_addr);
41912236Ssam 		np->n_addr = malloc(np->n_addrlen);
42012236Ssam 		if (np->n_addr == NULL) {
42112236Ssam 			free(np->n_name);
42212236Ssam 			free((char *)np);
42312236Ssam 			continue;
42412236Ssam 		}
42512236Ssam 		bcopy((char *)&ifr->ifr_addr, np->n_addr, np->n_addrlen);
42612236Ssam 		if (ioctl(s, SIOCGIFFLAGS, (char *)&ifreq) < 0) {
42716673Sralph 			syslog(LOG_ERR, "ioctl (get interface flags)");
42812236Ssam 			free((char *)np);
42912236Ssam 			continue;
43012236Ssam 		}
43121852Skarels 		if ((ifreq.ifr_flags & IFF_UP) == 0 ||
43221852Skarels 		    (ifreq.ifr_flags & (IFF_BROADCAST|IFF_POINTOPOINT)) == 0) {
43312236Ssam 			free((char *)np);
43412236Ssam 			continue;
43512236Ssam 		}
43612236Ssam 		np->n_flags = ifreq.ifr_flags;
43712236Ssam 		if (np->n_flags & IFF_POINTOPOINT) {
43812236Ssam 			if (ioctl(s, SIOCGIFDSTADDR, (char *)&ifreq) < 0) {
43916673Sralph 				syslog(LOG_ERR, "ioctl (get dstaddr)");
44012236Ssam 				free((char *)np);
44112236Ssam 				continue;
44212236Ssam 			}
44312236Ssam 			/* we assume addresses are all the same size */
44412236Ssam 			bcopy((char *)&ifreq.ifr_dstaddr,
44512236Ssam 			  np->n_addr, np->n_addrlen);
44612236Ssam 		}
44712236Ssam 		if (np->n_flags & IFF_BROADCAST) {
44821852Skarels 			if (ioctl(s, SIOCGIFBRDADDR, (char *)&ifreq) < 0) {
44921852Skarels 				syslog(LOG_ERR, "ioctl (get broadaddr)");
45021852Skarels 				free((char *)np);
45121852Skarels 				continue;
45221852Skarels 			}
45312236Ssam 			/* we assume addresses are all the same size */
45421852Skarels 			bcopy((char *)&ifreq.ifr_broadaddr,
45521852Skarels 			  np->n_addr, np->n_addrlen);
45612236Ssam 		}
45712236Ssam 		/* gag, wish we could get rid of Internet dependencies */
45812236Ssam 		sin = (struct sockaddr_in *)np->n_addr;
45912236Ssam 		sin->sin_port = sp->s_port;
46012236Ssam 		np->n_next = neighbors;
46112236Ssam 		neighbors = np;
46212236Ssam 	}
46312236Ssam 	return (1);
46412236Ssam }
46512807Ssam 
46612807Ssam #ifdef DEBUG
46712807Ssam sendto(s, buf, cc, flags, to, tolen)
46812807Ssam 	int s;
46912807Ssam 	char *buf;
47012807Ssam 	int cc, flags;
47112807Ssam 	char *to;
47212807Ssam 	int tolen;
47312807Ssam {
47412807Ssam 	register struct whod *w = (struct whod *)buf;
47512807Ssam 	register struct whoent *we;
47612807Ssam 	struct sockaddr_in *sin = (struct sockaddr_in *)to;
47712807Ssam 	char *interval();
47812807Ssam 
47912807Ssam 	printf("sendto %x.%d\n", ntohl(sin->sin_addr), ntohs(sin->sin_port));
48012807Ssam 	printf("hostname %s %s\n", w->wd_hostname,
48113187Ssam 	   interval(ntohl(w->wd_sendtime) - ntohl(w->wd_boottime), "  up"));
48212807Ssam 	printf("load %4.2f, %4.2f, %4.2f\n",
48313187Ssam 	    ntohl(w->wd_loadav[0]) / 100.0, ntohl(w->wd_loadav[1]) / 100.0,
48413187Ssam 	    ntohl(w->wd_loadav[2]) / 100.0);
48512807Ssam 	cc -= WHDRSIZE;
48612807Ssam 	for (we = w->wd_we, cc /= sizeof (struct whoent); cc > 0; cc--, we++) {
48713187Ssam 		time_t t = ntohl(we->we_utmp.out_time);
48812807Ssam 		printf("%-8.8s %s:%s %.12s",
48913187Ssam 			we->we_utmp.out_name,
49013187Ssam 			w->wd_hostname, we->we_utmp.out_line,
49113187Ssam 			ctime(&t)+4);
49213187Ssam 		we->we_idle = ntohl(we->we_idle) / 60;
49312807Ssam 		if (we->we_idle) {
49412807Ssam 			if (we->we_idle >= 100*60)
49512807Ssam 				we->we_idle = 100*60 - 1;
49612807Ssam 			if (we->we_idle >= 60)
49712807Ssam 				printf(" %2d", we->we_idle / 60);
49812807Ssam 			else
49912807Ssam 				printf("   ");
50012807Ssam 			printf(":%02d", we->we_idle % 60);
50112807Ssam 		}
50212807Ssam 		printf("\n");
50312807Ssam 	}
50412807Ssam }
50512807Ssam 
50612807Ssam char *
50712807Ssam interval(time, updown)
50812807Ssam 	int time;
50912807Ssam 	char *updown;
51012807Ssam {
51112807Ssam 	static char resbuf[32];
51212807Ssam 	int days, hours, minutes;
51312807Ssam 
51412807Ssam 	if (time < 0 || time > 3*30*24*60*60) {
51512807Ssam 		(void) sprintf(resbuf, "   %s ??:??", updown);
51612807Ssam 		return (resbuf);
51712807Ssam 	}
51812807Ssam 	minutes = (time + 59) / 60;		/* round to minutes */
51912807Ssam 	hours = minutes / 60; minutes %= 60;
52012807Ssam 	days = hours / 24; hours %= 24;
52112807Ssam 	if (days)
52212807Ssam 		(void) sprintf(resbuf, "%s %2d+%02d:%02d",
52312807Ssam 		    updown, days, hours, minutes);
52412807Ssam 	else
52512807Ssam 		(void) sprintf(resbuf, "%s    %2d:%02d",
52612807Ssam 		    updown, hours, minutes);
52712807Ssam 	return (resbuf);
52812807Ssam }
52912807Ssam #endif
530