xref: /csrg-svn/usr.sbin/rwhod/rwhod.c (revision 58922)
122549Sdist /*
235392Sbostic  * Copyright (c) 1983 The Regents of the University of California.
335392Sbostic  * All rights reserved.
435392Sbostic  *
542819Sbostic  * %sccs.include.redist.c%
622549Sdist  */
722549Sdist 
86460Swnj #ifndef lint
922549Sdist char copyright[] =
1035392Sbostic "@(#) Copyright (c) 1983 The Regents of the University of California.\n\
1122549Sdist  All rights reserved.\n";
1235392Sbostic #endif /* not lint */
136460Swnj 
1422549Sdist #ifndef lint
15*58922Smckusick static char sccsid[] = "@(#)rwhod.c	5.24 (Berkeley) 04/01/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>
23*58922Smckusick #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 <nlist.h>
3657744Sbostic #include <paths.h>
3757744Sbostic #include <stdio.h>
3857744Sbostic #include <stdlib.h>
3957744Sbostic #include <string.h>
4016673Sralph #include <syslog.h>
4157739Ssklower #include <unistd.h>
4257744Sbostic #include <utmp.h>
436460Swnj 
4415541Sralph /*
4515541Sralph  * Alarm interval. Don't forget to change the down time check in ruptime
4615541Sralph  * if this is changed.
4715541Sralph  */
4816673Sralph #define AL_INTERVAL (3 * 60)
4915541Sralph 
5042265Skarels struct	sockaddr_in sin;
516460Swnj 
5242265Skarels char	myname[MAXHOSTNAMELEN];
536460Swnj 
546460Swnj struct	nlist nl[] = {
5538182Smckusick #define	NL_BOOTTIME	0
569215Ssam 	{ "_boottime" },
576460Swnj 	0
586460Swnj };
596460Swnj 
6012236Ssam /*
6157744Sbostic  * We communicate with each neighbor in a list constructed at the time we're
6257744Sbostic  * started up.  Neighbors are currently directly connected via a hardware
6357744Sbostic  * interface.
6412236Ssam  */
6512236Ssam struct	neighbor {
6612236Ssam 	struct	neighbor *n_next;
6712236Ssam 	char	*n_name;		/* interface name */
6852574Ssklower 	struct	sockaddr *n_addr;		/* who to send to */
6912236Ssam 	int	n_addrlen;		/* size of address */
7012236Ssam 	int	n_flags;		/* should forward?, interface flags */
7112236Ssam };
7212236Ssam 
7312236Ssam struct	neighbor *neighbors;
746460Swnj struct	whod mywd;
7512236Ssam struct	servent *sp;
766460Swnj int	s, utmpf, kmemf = -1;
776460Swnj 
7812236Ssam #define	WHDRSIZE	(sizeof (mywd) - sizeof (mywd.wd_we))
7911834Ssam 
8057744Sbostic int	 configure __P((int));
8157744Sbostic void	 getkmem __P((int));
8257744Sbostic void	 onalrm __P((int));
8357744Sbostic void	 quit __P((char *));
8457744Sbostic void	 rt_xaddrs __P((caddr_t, caddr_t, struct rt_addrinfo *));
8557744Sbostic int	 verify __P((char *));
8657744Sbostic #ifdef DEBUG
8757744Sbostic char	*interval __P((int, char *));
8857744Sbostic void	 sendto __P((int, char *, int, int, char *, int));
8957744Sbostic #endif
906460Swnj 
9157744Sbostic int
9257744Sbostic main(argc, argv)
9357744Sbostic 	int argc;
9457744Sbostic 	char argv[];
956460Swnj {
966460Swnj 	struct sockaddr_in from;
9726016Skarels 	struct stat st;
986460Swnj 	char path[64];
9926484Smckusick 	int on = 1;
10057744Sbostic 	char *cp;
1016460Swnj 
10216673Sralph 	if (getuid()) {
10316673Sralph 		fprintf(stderr, "rwhod: not super user\n");
10416673Sralph 		exit(1);
10516673Sralph 	}
1068486Ssam 	sp = getservbyname("who", "udp");
10757744Sbostic 	if (sp == NULL) {
1088486Ssam 		fprintf(stderr, "rwhod: udp/who: unknown service\n");
1098486Ssam 		exit(1);
1108486Ssam 	}
1116460Swnj #ifndef DEBUG
11244707Skarels 	daemon(1, 0);
1136460Swnj #endif
11437295Sbostic 	if (chdir(_PATH_RWHODIR) < 0) {
11537295Sbostic 		(void)fprintf(stderr, "rwhod: %s: %s\n",
11637295Sbostic 		    _PATH_RWHODIR, strerror(errno));
11726011Smckusick 		exit(1);
11826011Smckusick 	}
1196460Swnj 	(void) signal(SIGHUP, getkmem);
12024853Seric 	openlog("rwhod", LOG_PID, LOG_DAEMON);
12112236Ssam 	/*
12212236Ssam 	 * Establish host name as returned by system.
12312236Ssam 	 */
12412236Ssam 	if (gethostname(myname, sizeof (myname) - 1) < 0) {
12516673Sralph 		syslog(LOG_ERR, "gethostname: %m");
1266460Swnj 		exit(1);
1276460Swnj 	}
12824745Sbloom 	if ((cp = index(myname, '.')) != NULL)
12924745Sbloom 		*cp = '\0';
13012236Ssam 	strncpy(mywd.wd_hostname, myname, sizeof (myname) - 1);
13137295Sbostic 	utmpf = open(_PATH_UTMP, O_RDONLY|O_CREAT, 0644);
1326460Swnj 	if (utmpf < 0) {
13337295Sbostic 		syslog(LOG_ERR, "%s: %m", _PATH_UTMP);
1346460Swnj 		exit(1);
1356460Swnj 	}
13657744Sbostic 	getkmem(0);
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 	}
14542265Skarels 	sin.sin_family = AF_INET;
14612236Ssam 	sin.sin_port = sp->s_port;
14746927Sbostic 	if (bind(s, (struct sockaddr *)&sin, sizeof (sin)) < 0) {
14816673Sralph 		syslog(LOG_ERR, "bind: %m");
1499215Ssam 		exit(1);
1509215Ssam 	}
15112236Ssam 	if (!configure(s))
15212236Ssam 		exit(1);
15313027Ssam 	signal(SIGALRM, onalrm);
15457744Sbostic 	onalrm(0);
1556460Swnj 	for (;;) {
1566460Swnj 		struct whod wd;
15712236Ssam 		int cc, whod, len = sizeof (from);
1586460Swnj 
15912236Ssam 		cc = recvfrom(s, (char *)&wd, sizeof (struct whod), 0,
16046927Sbostic 			(struct sockaddr *)&from, &len);
1616460Swnj 		if (cc <= 0) {
1626460Swnj 			if (cc < 0 && errno != EINTR)
16316673Sralph 				syslog(LOG_WARNING, "recv: %m");
1646460Swnj 			continue;
1656460Swnj 		}
1668486Ssam 		if (from.sin_port != sp->s_port) {
16716673Sralph 			syslog(LOG_WARNING, "%d: bad from port",
1688486Ssam 				ntohs(from.sin_port));
1696460Swnj 			continue;
1706460Swnj 		}
17112347Ssam 		if (wd.wd_vers != WHODVERSION)
17212347Ssam 			continue;
17312236Ssam 		if (wd.wd_type != WHODTYPE_STATUS)
17412236Ssam 			continue;
1758486Ssam 		if (!verify(wd.wd_hostname)) {
17616673Sralph 			syslog(LOG_WARNING, "malformed host name from %x",
1778486Ssam 				from.sin_addr);
1788486Ssam 			continue;
1798486Ssam 		}
18026011Smckusick 		(void) sprintf(path, "whod.%s", wd.wd_hostname);
18126016Skarels 		/*
18226016Skarels 		 * Rather than truncating and growing the file each time,
18326016Skarels 		 * use ftruncate if size is less than previous size.
18426016Skarels 		 */
18526016Skarels 		whod = open(path, O_WRONLY | O_CREAT, 0644);
1866460Swnj 		if (whod < 0) {
18716673Sralph 			syslog(LOG_WARNING, "%s: %m", path);
1886460Swnj 			continue;
1896460Swnj 		}
19042265Skarels #if ENDIAN != BIG_ENDIAN
19111834Ssam 		{
19213187Ssam 			int i, n = (cc - WHDRSIZE)/sizeof(struct whoent);
19311834Ssam 			struct whoent *we;
19411834Ssam 
19511834Ssam 			/* undo header byte swapping before writing to file */
19611834Ssam 			wd.wd_sendtime = ntohl(wd.wd_sendtime);
19711834Ssam 			for (i = 0; i < 3; i++)
19811834Ssam 				wd.wd_loadav[i] = ntohl(wd.wd_loadav[i]);
19911834Ssam 			wd.wd_boottime = ntohl(wd.wd_boottime);
20011834Ssam 			we = wd.wd_we;
20111834Ssam 			for (i = 0; i < n; i++) {
20211834Ssam 				we->we_idle = ntohl(we->we_idle);
20312872Ssam 				we->we_utmp.out_time =
20412872Ssam 				    ntohl(we->we_utmp.out_time);
20511834Ssam 				we++;
20611834Ssam 			}
20711834Ssam 		}
20811834Ssam #endif
20946927Sbostic 		(void) time((time_t *)&wd.wd_recvtime);
2106460Swnj 		(void) write(whod, (char *)&wd, cc);
21126016Skarels 		if (fstat(whod, &st) < 0 || st.st_size > cc)
21226016Skarels 			ftruncate(whod, cc);
2136460Swnj 		(void) close(whod);
2146460Swnj 	}
2156460Swnj }
2166460Swnj 
2178486Ssam /*
2188486Ssam  * Check out host name for unprintables
2198486Ssam  * and other funnies before allowing a file
2208486Ssam  * to be created.  Sorry, but blanks aren't allowed.
2218486Ssam  */
22257744Sbostic int
2238486Ssam verify(name)
2248486Ssam 	register char *name;
2258486Ssam {
2268486Ssam 	register int size = 0;
2278486Ssam 
2288486Ssam 	while (*name) {
22915214Sleres 		if (!isascii(*name) || !(isalnum(*name) || ispunct(*name)))
2308486Ssam 			return (0);
2318486Ssam 		name++, size++;
2328486Ssam 	}
2338486Ssam 	return (size > 0);
2348486Ssam }
2358486Ssam 
2366460Swnj int	utmptime;
2376460Swnj int	utmpent;
23822546Sbloom int	utmpsize = 0;
23922546Sbloom struct	utmp *utmp;
2406460Swnj int	alarmcount;
2416460Swnj 
24246927Sbostic void
24357744Sbostic onalrm(signo)
24457744Sbostic 	int signo;
2456460Swnj {
24637295Sbostic 	register struct neighbor *np;
24737295Sbostic 	register struct whoent *we = mywd.wd_we, *wlast;
2486460Swnj 	register int i;
2496460Swnj 	struct stat stb;
25057744Sbostic 	double avenrun[3];
25157744Sbostic 	time_t now;
2526460Swnj 	int cc;
2536460Swnj 
25457744Sbostic 	now = time(NULL);
2556460Swnj 	if (alarmcount % 10 == 0)
25657744Sbostic 		getkmem(0);
2576460Swnj 	alarmcount++;
2586460Swnj 	(void) fstat(utmpf, &stb);
25922546Sbloom 	if ((stb.st_mtime != utmptime) || (stb.st_size > utmpsize)) {
26017303Stef 		utmptime = stb.st_mtime;
26122546Sbloom 		if (stb.st_size > utmpsize) {
26222546Sbloom 			utmpsize = stb.st_size + 10 * sizeof(struct utmp);
26322546Sbloom 			if (utmp)
26422546Sbloom 				utmp = (struct utmp *)realloc(utmp, utmpsize);
26522546Sbloom 			else
26622546Sbloom 				utmp = (struct utmp *)malloc(utmpsize);
26722546Sbloom 			if (! utmp) {
26822546Sbloom 				fprintf(stderr, "rwhod: malloc failed\n");
26922546Sbloom 				utmpsize = 0;
27022546Sbloom 				goto done;
27122546Sbloom 			}
27222546Sbloom 		}
27357739Ssklower 		(void) lseek(utmpf, (off_t)0, L_SET);
27422546Sbloom 		cc = read(utmpf, (char *)utmp, stb.st_size);
2756460Swnj 		if (cc < 0) {
27637295Sbostic 			fprintf(stderr, "rwhod: %s: %s\n",
27737295Sbostic 			    _PATH_UTMP, strerror(errno));
27813469Ssam 			goto done;
2796460Swnj 		}
28012236Ssam 		wlast = &mywd.wd_we[1024 / sizeof (struct whoent) - 1];
2816460Swnj 		utmpent = cc / sizeof (struct utmp);
2826460Swnj 		for (i = 0; i < utmpent; i++)
2836460Swnj 			if (utmp[i].ut_name[0]) {
28412807Ssam 				bcopy(utmp[i].ut_line, we->we_utmp.out_line,
28512807Ssam 				   sizeof (utmp[i].ut_line));
28612807Ssam 				bcopy(utmp[i].ut_name, we->we_utmp.out_name,
28712807Ssam 				   sizeof (utmp[i].ut_name));
28812807Ssam 				we->we_utmp.out_time = htonl(utmp[i].ut_time);
2896577Ssam 				if (we >= wlast)
2906577Ssam 					break;
2916460Swnj 				we++;
2926460Swnj 			}
2936460Swnj 		utmpent = we - mywd.wd_we;
2946460Swnj 	}
29526484Smckusick 
29626484Smckusick 	/*
29726484Smckusick 	 * The test on utmpent looks silly---after all, if no one is
29826484Smckusick 	 * logged on, why worry about efficiency?---but is useful on
29926484Smckusick 	 * (e.g.) compute servers.
30026484Smckusick 	 */
30137971Sbostic 	if (utmpent && chdir(_PATH_DEV)) {
30237971Sbostic 		syslog(LOG_ERR, "chdir(%s): %m", _PATH_DEV);
30326484Smckusick 		exit(1);
30426484Smckusick 	}
3056460Swnj 	we = mywd.wd_we;
3066460Swnj 	for (i = 0; i < utmpent; i++) {
30726484Smckusick 		if (stat(we->we_utmp.out_line, &stb) >= 0)
30812807Ssam 			we->we_idle = htonl(now - stb.st_atime);
3096460Swnj 		we++;
3106460Swnj 	}
31138182Smckusick 	(void) getloadavg(avenrun, sizeof(avenrun)/sizeof(avenrun[0]));
3126460Swnj 	for (i = 0; i < 3; i++)
31312873Ssam 		mywd.wd_loadav[i] = htonl((u_long)(avenrun[i] * 100));
3146460Swnj 	cc = (char *)we - (char *)&mywd;
31512807Ssam 	mywd.wd_sendtime = htonl(time(0));
31612236Ssam 	mywd.wd_vers = WHODVERSION;
31712236Ssam 	mywd.wd_type = WHODTYPE_STATUS;
31812236Ssam 	for (np = neighbors; np != NULL; np = np->n_next)
31912236Ssam 		(void) sendto(s, (char *)&mywd, cc, 0,
32052574Ssklower 				np->n_addr, np->n_addrlen);
32137295Sbostic 	if (utmpent && chdir(_PATH_RWHODIR)) {
32237295Sbostic 		syslog(LOG_ERR, "chdir(%s): %m", _PATH_RWHODIR);
32326484Smckusick 		exit(1);
32426484Smckusick 	}
32513469Ssam done:
32615541Sralph 	(void) alarm(AL_INTERVAL);
3276460Swnj }
3286460Swnj 
32946927Sbostic void
33057744Sbostic getkmem(signo)
33157744Sbostic 	int signo;
3326460Swnj {
33316663Ssam 	static ino_t vmunixino;
33416663Ssam 	static time_t vmunixctime;
33516663Ssam 	struct stat sb;
3366460Swnj 
33737295Sbostic 	if (stat(_PATH_UNIX, &sb) < 0) {
33816663Ssam 		if (vmunixctime)
33916663Ssam 			return;
34016663Ssam 	} else {
34116663Ssam 		if (sb.st_ctime == vmunixctime && sb.st_ino == vmunixino)
34216663Ssam 			return;
34316663Ssam 		vmunixctime = sb.st_ctime;
34416663Ssam 		vmunixino= sb.st_ino;
34516663Ssam 	}
3466460Swnj 	if (kmemf >= 0)
3476460Swnj 		(void) close(kmemf);
3486460Swnj loop:
34937295Sbostic 	if (nlist(_PATH_UNIX, nl)) {
35037295Sbostic 		syslog(LOG_WARNING, "%s: namelist botch", _PATH_UNIX);
3516460Swnj 		sleep(300);
3526460Swnj 		goto loop;
3536460Swnj 	}
35437295Sbostic 	kmemf = open(_PATH_KMEM, O_RDONLY, 0);
3556460Swnj 	if (kmemf < 0) {
35637295Sbostic 		syslog(LOG_ERR, "%s: %m", _PATH_KMEM);
35716673Sralph 		exit(1);
3586460Swnj 	}
35957739Ssklower 	(void) lseek(kmemf, (off_t)nl[NL_BOOTTIME].n_value, L_SET);
36013187Ssam 	(void) read(kmemf, (char *)&mywd.wd_boottime,
36113187Ssam 	    sizeof (mywd.wd_boottime));
36212807Ssam 	mywd.wd_boottime = htonl(mywd.wd_boottime);
3636460Swnj }
36412236Ssam 
36552574Ssklower void
36652574Ssklower quit(msg)
36757744Sbostic 	char *msg;
36852574Ssklower {
36952574Ssklower 	syslog(LOG_ERR, msg);
37052574Ssklower 	exit(1);
37152574Ssklower }
37252574Ssklower 
37352574Ssklower #define ROUNDUP(a) \
37452574Ssklower 	((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
37552574Ssklower #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
37652574Ssklower 
37752574Ssklower void
37852574Ssklower rt_xaddrs(cp, cplim, rtinfo)
37952574Ssklower 	register caddr_t cp, cplim;
38052574Ssklower 	register struct rt_addrinfo *rtinfo;
38152574Ssklower {
38252574Ssklower 	register struct sockaddr *sa;
38352574Ssklower 	register int i;
38452574Ssklower 
38552574Ssklower 	bzero(rtinfo->rti_info, sizeof(rtinfo->rti_info));
38652574Ssklower 	for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
38752574Ssklower 		if ((rtinfo->rti_addrs & (1 << i)) == 0)
38852574Ssklower 			continue;
38952574Ssklower 		rtinfo->rti_info[i] = sa = (struct sockaddr *)cp;
39052574Ssklower 		ADVANCE(cp, sa);
39152574Ssklower 	}
39252574Ssklower }
39352574Ssklower 
39412236Ssam /*
39512236Ssam  * Figure out device configuration and select
39612236Ssam  * networks which deserve status information.
39712236Ssam  */
39857744Sbostic int
39912236Ssam configure(s)
40012236Ssam 	int s;
40112236Ssam {
40252574Ssklower 	register struct neighbor *np;
40352574Ssklower 	register struct if_msghdr *ifm;
40452574Ssklower 	register struct ifa_msghdr *ifam;
40552574Ssklower 	struct sockaddr_dl *sdl;
406*58922Smckusick 	size_t needed;
407*58922Smckusick 	int mib[6], flags = 0, len;
40852574Ssklower 	char *buf, *lim, *next;
40952574Ssklower 	struct rt_addrinfo info;
41012236Ssam 
411*58922Smckusick 	mib[0] = CTL_NET;
412*58922Smckusick 	mib[1] = PF_ROUTE;
413*58922Smckusick 	mib[2] = 0;
414*58922Smckusick 	mib[3] = AF_INET;
415*58922Smckusick 	mib[4] = NET_RT_IFLIST;
416*58922Smckusick 	mib[5] = 0;
417*58922Smckusick 	if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
418*58922Smckusick 		quit("route-sysctl-estimate");
41952574Ssklower 	if ((buf = malloc(needed)) == NULL)
42052574Ssklower 		quit("malloc");
421*58922Smckusick 	if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
42252574Ssklower 		quit("actual retrieval of interface table");
423*58922Smckusick 	lim = buf + needed;
42452574Ssklower 
42552574Ssklower 	for (next = buf; next < lim; next += ifm->ifm_msglen) {
42652574Ssklower 		ifm = (struct if_msghdr *)next;
42752574Ssklower 		if (ifm->ifm_type == RTM_IFINFO) {
42852574Ssklower 			sdl = (struct sockaddr_dl *)(ifm + 1);
42952574Ssklower 			flags = ifm->ifm_flags;
43052574Ssklower 			continue;
43152574Ssklower 		}
43252574Ssklower 		if ((flags & IFF_UP) == 0 ||
43352574Ssklower 		    (flags & (IFF_BROADCAST|IFF_POINTOPOINT)) == 0)
43452574Ssklower 			continue;
43552574Ssklower 		if (ifm->ifm_type != RTM_NEWADDR)
436*58922Smckusick 			quit("out of sync parsing NET_RT_IFLIST");
43752574Ssklower 		ifam = (struct ifa_msghdr *)ifm;
43852574Ssklower 		info.rti_addrs = ifam->ifam_addrs;
43952574Ssklower 		rt_xaddrs((char *)(ifam + 1), ifam->ifam_msglen + (char *)ifam,
44052574Ssklower 			&info);
44152574Ssklower 		/* gag, wish we could get rid of Internet dependencies */
44252574Ssklower #define dstaddr	info.rti_info[RTAX_BRD]
44352574Ssklower #define IPADDR_SA(x) ((struct sockaddr_in *)(x))->sin_addr.s_addr
44452574Ssklower #define PORT_SA(x) ((struct sockaddr_in *)(x))->sin_port
44552574Ssklower 		if (dstaddr == 0 || dstaddr->sa_family != AF_INET)
44652574Ssklower 			continue;
44752574Ssklower 		PORT_SA(dstaddr) = sp->s_port;
44812236Ssam 		for (np = neighbors; np != NULL; np = np->n_next)
44952574Ssklower 			if (bcmp(sdl->sdl_data, np->n_name, sdl->sdl_nlen) == 0
45052574Ssklower 			    && IPADDR_SA(np->n_addr) == IPADDR_SA(dstaddr))
45112236Ssam 				break;
45212236Ssam 		if (np != NULL)
45312236Ssam 			continue;
45452574Ssklower 		len = sizeof(*np) + dstaddr->sa_len + sdl->sdl_nlen + 1;
45552574Ssklower 		np = (struct neighbor *)malloc(len);
45612236Ssam 		if (np == NULL)
45752574Ssklower 			quit("malloc of neighbor structure");
45852574Ssklower 		bzero((char *)np, len);
45952574Ssklower 		np->n_flags = flags;
46052574Ssklower 		np->n_addr = (struct sockaddr *)(np + 1);
46152574Ssklower 		np->n_addrlen = dstaddr->sa_len;
46252574Ssklower 		np->n_name = np->n_addrlen + (char *)np->n_addr;
46312236Ssam 		np->n_next = neighbors;
46412236Ssam 		neighbors = np;
46552574Ssklower 		bcopy((char *)dstaddr, (char *)np->n_addr, np->n_addrlen);
46652574Ssklower 		bcopy(sdl->sdl_data, np->n_name, sdl->sdl_nlen);
46712236Ssam 	}
46852574Ssklower 	free(buf);
46912236Ssam 	return (1);
47012236Ssam }
47112807Ssam 
47212807Ssam #ifdef DEBUG
47357744Sbostic void
47412807Ssam sendto(s, buf, cc, flags, to, tolen)
47512807Ssam 	int s;
47612807Ssam 	char *buf;
47712807Ssam 	int cc, flags;
47812807Ssam 	char *to;
47912807Ssam 	int tolen;
48012807Ssam {
48112807Ssam 	register struct whod *w = (struct whod *)buf;
48212807Ssam 	register struct whoent *we;
48312807Ssam 	struct sockaddr_in *sin = (struct sockaddr_in *)to;
48412807Ssam 
48512807Ssam 	printf("sendto %x.%d\n", ntohl(sin->sin_addr), ntohs(sin->sin_port));
48612807Ssam 	printf("hostname %s %s\n", w->wd_hostname,
48713187Ssam 	   interval(ntohl(w->wd_sendtime) - ntohl(w->wd_boottime), "  up"));
48812807Ssam 	printf("load %4.2f, %4.2f, %4.2f\n",
48913187Ssam 	    ntohl(w->wd_loadav[0]) / 100.0, ntohl(w->wd_loadav[1]) / 100.0,
49013187Ssam 	    ntohl(w->wd_loadav[2]) / 100.0);
49112807Ssam 	cc -= WHDRSIZE;
49212807Ssam 	for (we = w->wd_we, cc /= sizeof (struct whoent); cc > 0; cc--, we++) {
49313187Ssam 		time_t t = ntohl(we->we_utmp.out_time);
49412807Ssam 		printf("%-8.8s %s:%s %.12s",
49513187Ssam 			we->we_utmp.out_name,
49613187Ssam 			w->wd_hostname, we->we_utmp.out_line,
49713187Ssam 			ctime(&t)+4);
49813187Ssam 		we->we_idle = ntohl(we->we_idle) / 60;
49912807Ssam 		if (we->we_idle) {
50012807Ssam 			if (we->we_idle >= 100*60)
50112807Ssam 				we->we_idle = 100*60 - 1;
50212807Ssam 			if (we->we_idle >= 60)
50312807Ssam 				printf(" %2d", we->we_idle / 60);
50412807Ssam 			else
50512807Ssam 				printf("   ");
50612807Ssam 			printf(":%02d", we->we_idle % 60);
50712807Ssam 		}
50812807Ssam 		printf("\n");
50912807Ssam 	}
51012807Ssam }
51112807Ssam 
51212807Ssam char *
51312807Ssam interval(time, updown)
51412807Ssam 	int time;
51512807Ssam 	char *updown;
51612807Ssam {
51712807Ssam 	static char resbuf[32];
51812807Ssam 	int days, hours, minutes;
51912807Ssam 
52012807Ssam 	if (time < 0 || time > 3*30*24*60*60) {
52112807Ssam 		(void) sprintf(resbuf, "   %s ??:??", updown);
52212807Ssam 		return (resbuf);
52312807Ssam 	}
52412807Ssam 	minutes = (time + 59) / 60;		/* round to minutes */
52512807Ssam 	hours = minutes / 60; minutes %= 60;
52612807Ssam 	days = hours / 24; hours %= 24;
52712807Ssam 	if (days)
52812807Ssam 		(void) sprintf(resbuf, "%s %2d+%02d:%02d",
52912807Ssam 		    updown, days, hours, minutes);
53012807Ssam 	else
53112807Ssam 		(void) sprintf(resbuf, "%s    %2d:%02d",
53212807Ssam 		    updown, hours, minutes);
53312807Ssam 	return (resbuf);
53412807Ssam }
53512807Ssam #endif
536