xref: /csrg-svn/usr.sbin/rwhod/rwhod.c (revision 57744)
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*57744Sbostic static char sccsid[] = "@(#)rwhod.c	5.23 (Berkeley) 01/30/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>
2352574Ssklower #include <sys/kinfo.h>
249215Ssam 
2512236Ssam #include <net/if.h>
2652574Ssklower #include <net/if_dl.h>
2752574Ssklower #include <net/route.h>
289215Ssam #include <netinet/in.h>
29*57744Sbostic #include <protocols/rwhod.h>
309215Ssam 
31*57744Sbostic #include <ctype.h>
326460Swnj #include <errno.h>
33*57744Sbostic #include <fcntl.h>
348486Ssam #include <netdb.h>
35*57744Sbostic #include <nlist.h>
36*57744Sbostic #include <paths.h>
37*57744Sbostic #include <stdio.h>
38*57744Sbostic #include <stdlib.h>
39*57744Sbostic #include <string.h>
4016673Sralph #include <syslog.h>
4157739Ssklower #include <unistd.h>
42*57744Sbostic #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 /*
61*57744Sbostic  * We communicate with each neighbor in a list constructed at the time we're
62*57744Sbostic  * started up.  Neighbors are currently directly connected via a hardware
63*57744Sbostic  * 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 
80*57744Sbostic int	 configure __P((int));
81*57744Sbostic void	 getkmem __P((int));
82*57744Sbostic void	 onalrm __P((int));
83*57744Sbostic void	 quit __P((char *));
84*57744Sbostic void	 rt_xaddrs __P((caddr_t, caddr_t, struct rt_addrinfo *));
85*57744Sbostic int	 verify __P((char *));
86*57744Sbostic #ifdef DEBUG
87*57744Sbostic char	*interval __P((int, char *));
88*57744Sbostic void	 sendto __P((int, char *, int, int, char *, int));
89*57744Sbostic #endif
906460Swnj 
91*57744Sbostic int
92*57744Sbostic main(argc, argv)
93*57744Sbostic 	int argc;
94*57744Sbostic 	char argv[];
956460Swnj {
966460Swnj 	struct sockaddr_in from;
9726016Skarels 	struct stat st;
986460Swnj 	char path[64];
9926484Smckusick 	int on = 1;
100*57744Sbostic 	char *cp;
1016460Swnj 
10216673Sralph 	if (getuid()) {
10316673Sralph 		fprintf(stderr, "rwhod: not super user\n");
10416673Sralph 		exit(1);
10516673Sralph 	}
1068486Ssam 	sp = getservbyname("who", "udp");
107*57744Sbostic 	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 	}
136*57744Sbostic 	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);
154*57744Sbostic 	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  */
222*57744Sbostic 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
243*57744Sbostic onalrm(signo)
244*57744Sbostic 	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;
250*57744Sbostic 	double avenrun[3];
251*57744Sbostic 	time_t now;
2526460Swnj 	int cc;
2536460Swnj 
254*57744Sbostic 	now = time(NULL);
2556460Swnj 	if (alarmcount % 10 == 0)
256*57744Sbostic 		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
330*57744Sbostic getkmem(signo)
331*57744Sbostic 	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)
367*57744Sbostic 	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  */
398*57744Sbostic 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;
40652574Ssklower 	int needed, rlen = 0, flags = 0, len;
40752574Ssklower 	char *buf, *lim, *next;
40852574Ssklower 	struct rt_addrinfo info;
40912236Ssam 
41052574Ssklower 	if ((needed = getkerninfo(KINFO_RT_IFLIST, 0, 0, 0)) < 0)
41152574Ssklower 		quit("route-getkerninfo-estimate");
41252574Ssklower 	if ((buf = malloc(needed)) == NULL)
41352574Ssklower 		quit("malloc");
41452574Ssklower 	if ((rlen = getkerninfo(KINFO_RT_IFLIST, buf, &needed, 0)) < 0)
41552574Ssklower 		quit("actual retrieval of interface table");
41652574Ssklower 	lim = buf + rlen;
41752574Ssklower 
41852574Ssklower 	for (next = buf; next < lim; next += ifm->ifm_msglen) {
41952574Ssklower 		ifm = (struct if_msghdr *)next;
42052574Ssklower 		if (ifm->ifm_type == RTM_IFINFO) {
42152574Ssklower 			sdl = (struct sockaddr_dl *)(ifm + 1);
42252574Ssklower 			flags = ifm->ifm_flags;
42352574Ssklower 			continue;
42452574Ssklower 		}
42552574Ssklower 		if ((flags & IFF_UP) == 0 ||
42652574Ssklower 		    (flags & (IFF_BROADCAST|IFF_POINTOPOINT)) == 0)
42752574Ssklower 			continue;
42852574Ssklower 		if (ifm->ifm_type != RTM_NEWADDR)
42952574Ssklower 			quit("out of sync parsing KINFO_RT_IFLIST");
43052574Ssklower 		ifam = (struct ifa_msghdr *)ifm;
43152574Ssklower 		info.rti_addrs = ifam->ifam_addrs;
43252574Ssklower 		rt_xaddrs((char *)(ifam + 1), ifam->ifam_msglen + (char *)ifam,
43352574Ssklower 			&info);
43452574Ssklower 		/* gag, wish we could get rid of Internet dependencies */
43552574Ssklower #define dstaddr	info.rti_info[RTAX_BRD]
43652574Ssklower #define IPADDR_SA(x) ((struct sockaddr_in *)(x))->sin_addr.s_addr
43752574Ssklower #define PORT_SA(x) ((struct sockaddr_in *)(x))->sin_port
43852574Ssklower 		if (dstaddr == 0 || dstaddr->sa_family != AF_INET)
43952574Ssklower 			continue;
44052574Ssklower 		PORT_SA(dstaddr) = sp->s_port;
44112236Ssam 		for (np = neighbors; np != NULL; np = np->n_next)
44252574Ssklower 			if (bcmp(sdl->sdl_data, np->n_name, sdl->sdl_nlen) == 0
44352574Ssklower 			    && IPADDR_SA(np->n_addr) == IPADDR_SA(dstaddr))
44412236Ssam 				break;
44512236Ssam 		if (np != NULL)
44612236Ssam 			continue;
44752574Ssklower 		len = sizeof(*np) + dstaddr->sa_len + sdl->sdl_nlen + 1;
44852574Ssklower 		np = (struct neighbor *)malloc(len);
44912236Ssam 		if (np == NULL)
45052574Ssklower 			quit("malloc of neighbor structure");
45152574Ssklower 		bzero((char *)np, len);
45252574Ssklower 		np->n_flags = flags;
45352574Ssklower 		np->n_addr = (struct sockaddr *)(np + 1);
45452574Ssklower 		np->n_addrlen = dstaddr->sa_len;
45552574Ssklower 		np->n_name = np->n_addrlen + (char *)np->n_addr;
45612236Ssam 		np->n_next = neighbors;
45712236Ssam 		neighbors = np;
45852574Ssklower 		bcopy((char *)dstaddr, (char *)np->n_addr, np->n_addrlen);
45952574Ssklower 		bcopy(sdl->sdl_data, np->n_name, sdl->sdl_nlen);
46012236Ssam 	}
46152574Ssklower 	free(buf);
46212236Ssam 	return (1);
46312236Ssam }
46412807Ssam 
46512807Ssam #ifdef DEBUG
466*57744Sbostic void
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 
47812807Ssam 	printf("sendto %x.%d\n", ntohl(sin->sin_addr), ntohs(sin->sin_port));
47912807Ssam 	printf("hostname %s %s\n", w->wd_hostname,
48013187Ssam 	   interval(ntohl(w->wd_sendtime) - ntohl(w->wd_boottime), "  up"));
48112807Ssam 	printf("load %4.2f, %4.2f, %4.2f\n",
48213187Ssam 	    ntohl(w->wd_loadav[0]) / 100.0, ntohl(w->wd_loadav[1]) / 100.0,
48313187Ssam 	    ntohl(w->wd_loadav[2]) / 100.0);
48412807Ssam 	cc -= WHDRSIZE;
48512807Ssam 	for (we = w->wd_we, cc /= sizeof (struct whoent); cc > 0; cc--, we++) {
48613187Ssam 		time_t t = ntohl(we->we_utmp.out_time);
48712807Ssam 		printf("%-8.8s %s:%s %.12s",
48813187Ssam 			we->we_utmp.out_name,
48913187Ssam 			w->wd_hostname, we->we_utmp.out_line,
49013187Ssam 			ctime(&t)+4);
49113187Ssam 		we->we_idle = ntohl(we->we_idle) / 60;
49212807Ssam 		if (we->we_idle) {
49312807Ssam 			if (we->we_idle >= 100*60)
49412807Ssam 				we->we_idle = 100*60 - 1;
49512807Ssam 			if (we->we_idle >= 60)
49612807Ssam 				printf(" %2d", we->we_idle / 60);
49712807Ssam 			else
49812807Ssam 				printf("   ");
49912807Ssam 			printf(":%02d", we->we_idle % 60);
50012807Ssam 		}
50112807Ssam 		printf("\n");
50212807Ssam 	}
50312807Ssam }
50412807Ssam 
50512807Ssam char *
50612807Ssam interval(time, updown)
50712807Ssam 	int time;
50812807Ssam 	char *updown;
50912807Ssam {
51012807Ssam 	static char resbuf[32];
51112807Ssam 	int days, hours, minutes;
51212807Ssam 
51312807Ssam 	if (time < 0 || time > 3*30*24*60*60) {
51412807Ssam 		(void) sprintf(resbuf, "   %s ??:??", updown);
51512807Ssam 		return (resbuf);
51612807Ssam 	}
51712807Ssam 	minutes = (time + 59) / 60;		/* round to minutes */
51812807Ssam 	hours = minutes / 60; minutes %= 60;
51912807Ssam 	days = hours / 24; hours %= 24;
52012807Ssam 	if (days)
52112807Ssam 		(void) sprintf(resbuf, "%s %2d+%02d:%02d",
52212807Ssam 		    updown, days, hours, minutes);
52312807Ssam 	else
52412807Ssam 		(void) sprintf(resbuf, "%s    %2d:%02d",
52512807Ssam 		    updown, hours, minutes);
52612807Ssam 	return (resbuf);
52712807Ssam }
52812807Ssam #endif
529