xref: /onnv-gate/usr/src/cmd/cmd-inet/usr.sbin/in.rwhod.c (revision 473:9c6ea6da29de)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
23*473Sbw  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
320Sstevel@tonic-gate  * under license from the Regents of the University of California.
330Sstevel@tonic-gate  */
340Sstevel@tonic-gate 
350Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
360Sstevel@tonic-gate 
370Sstevel@tonic-gate #include <sys/types.h>
380Sstevel@tonic-gate #include <sys/param.h>
390Sstevel@tonic-gate #include <sys/socket.h>
400Sstevel@tonic-gate #include <sys/sockio.h>
410Sstevel@tonic-gate #include <sys/stat.h>
420Sstevel@tonic-gate #include <sys/ioctl.h>
430Sstevel@tonic-gate #include <sys/file.h>
440Sstevel@tonic-gate #include <sys/loadavg.h>
450Sstevel@tonic-gate 
460Sstevel@tonic-gate #include <net/if.h>
470Sstevel@tonic-gate #include <netinet/in.h>
480Sstevel@tonic-gate 
490Sstevel@tonic-gate #include <stdio.h>
500Sstevel@tonic-gate #include <signal.h>
510Sstevel@tonic-gate #include <errno.h>
520Sstevel@tonic-gate #include <utmpx.h>
530Sstevel@tonic-gate #include <ctype.h>
540Sstevel@tonic-gate #include <netdb.h>
550Sstevel@tonic-gate #include <syslog.h>
560Sstevel@tonic-gate #include <fcntl.h>
570Sstevel@tonic-gate #include <sys/isa_defs.h>	/* for ENDIAN defines */
580Sstevel@tonic-gate #include <arpa/inet.h>
590Sstevel@tonic-gate #include <protocols/rwhod.h>
600Sstevel@tonic-gate 
610Sstevel@tonic-gate #include <strings.h>
620Sstevel@tonic-gate #include <stdlib.h>
630Sstevel@tonic-gate #include <unistd.h>
640Sstevel@tonic-gate 
650Sstevel@tonic-gate /*
660Sstevel@tonic-gate  * This version of Berkeley's rwhod has been modified to use IP multicast
670Sstevel@tonic-gate  * datagrams, under control of a new command-line option:
680Sstevel@tonic-gate  *
690Sstevel@tonic-gate  *	rwhod -m	causes rwhod to use IP multicast (instead of
700Sstevel@tonic-gate  *			broadcast or unicast) on all interfaces that have
710Sstevel@tonic-gate  *			the IFF_MULTICAST flag set in their "ifnet" structs
720Sstevel@tonic-gate  *			(excluding the loopback interface).  The multicast
730Sstevel@tonic-gate  *			reports are sent with a time-to-live of 1, to prevent
740Sstevel@tonic-gate  *			forwarding beyond the directly-connected subnet(s).
750Sstevel@tonic-gate  *
760Sstevel@tonic-gate  *	rwhod -m <ttl>	causes rwhod to send IP multicast datagrams with a
770Sstevel@tonic-gate  *			time-to-live of <ttl>, via a SINGLE interface rather
780Sstevel@tonic-gate  *			than all interfaces.  <ttl> must be between 0 and
790Sstevel@tonic-gate  *			MAX_MULTICAST_SCOPE, defined below.  Note that "-m 1"
800Sstevel@tonic-gate  *			is different than "-m", in that "-m 1" specifies
810Sstevel@tonic-gate  *			transmission on one interface only.
820Sstevel@tonic-gate  *
830Sstevel@tonic-gate  * When "-m" is used without a <ttl> argument, the program accepts multicast
840Sstevel@tonic-gate  * rwhod reports from all multicast-capable interfaces.  If a <ttl> argument
850Sstevel@tonic-gate  * is given, it accepts multicast reports from only one interface, the one
860Sstevel@tonic-gate  * on which reports are sent (which may be controlled via the host's routing
870Sstevel@tonic-gate  * table).  Regardless of the "-m" option, the program accepts broadcast or
880Sstevel@tonic-gate  * unicast reports from all interfaces.  Thus, this program will hear the
890Sstevel@tonic-gate  * reports of old, non-multicasting rwhods, but, if multicasting is used,
900Sstevel@tonic-gate  * those old rwhods won't hear the reports generated by this program.
910Sstevel@tonic-gate  *
920Sstevel@tonic-gate  *                  -- Steve Deering, Stanford University, February 1989
930Sstevel@tonic-gate  */
940Sstevel@tonic-gate 
950Sstevel@tonic-gate #define	NO_MULTICAST		0	  /* multicast modes */
960Sstevel@tonic-gate #define	PER_INTERFACE_MULTICAST	1
970Sstevel@tonic-gate #define	SCOPED_MULTICAST	2
980Sstevel@tonic-gate 
990Sstevel@tonic-gate #define	MAX_MULTICAST_SCOPE	32	  /* "site-wide", by convention */
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate #define	INADDR_WHOD_GROUP	(ulong_t)0xe0000103	/* 224.0.1.3 */
1020Sstevel@tonic-gate 					/* (belongs in protocols/rwhod.h) */
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate static int			multicast_mode  = NO_MULTICAST;
1050Sstevel@tonic-gate static int			multicast_scope;
1060Sstevel@tonic-gate static struct sockaddr_in	multicast_addr  = { AF_INET };
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate /*
1100Sstevel@tonic-gate  * Alarm interval. Don't forget to change the down time check in ruptime
1110Sstevel@tonic-gate  * if this is changed.
1120Sstevel@tonic-gate  */
1130Sstevel@tonic-gate #define	AL_INTERVAL (3 * 60)
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate static struct	sockaddr_in sin = { AF_INET };
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate static char	myname[MAXHOSTNAMELEN];
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate /*
1200Sstevel@tonic-gate  * We communicate with each neighbor in
1210Sstevel@tonic-gate  * a list constructed at the time we're
1220Sstevel@tonic-gate  * started up.  Neighbors are currently
1230Sstevel@tonic-gate  * directly connected via a hardware interface.
1240Sstevel@tonic-gate  */
1250Sstevel@tonic-gate struct	neighbor {
1260Sstevel@tonic-gate 	struct	neighbor *n_next;
1270Sstevel@tonic-gate 	char	*n_name;		/* interface name */
1280Sstevel@tonic-gate 	char	*n_addr;		/* who to send to */
1290Sstevel@tonic-gate 	int	n_addrlen;		/* size of address */
1300Sstevel@tonic-gate 	ulong_t	n_subnet;		/* AF_INET subnet */
1310Sstevel@tonic-gate 	uint_t	n_flags;		/* should forward?, interface flags */
1320Sstevel@tonic-gate };
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate static struct	neighbor *neighbors;
1350Sstevel@tonic-gate static struct	whod mywd;
1360Sstevel@tonic-gate static struct	servent *sp;
1370Sstevel@tonic-gate static int	s;
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate #define	WHDRSIZE	(sizeof (mywd) - sizeof (mywd.wd_we))
1400Sstevel@tonic-gate #define	RWHODIR		"/var/spool/rwho"
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate static void		onalrm(void);
1430Sstevel@tonic-gate static void		getkmem(void);
1440Sstevel@tonic-gate static boolean_t	configure(int);
1450Sstevel@tonic-gate static int		verify(const struct whod *);
1460Sstevel@tonic-gate 
147*473Sbw int
main(int argc,char * argv[])1480Sstevel@tonic-gate main(int argc, char *argv[])
1490Sstevel@tonic-gate {
1500Sstevel@tonic-gate 	struct sockaddr_in from;
1510Sstevel@tonic-gate 	struct stat st;
1520Sstevel@tonic-gate 	char path[64];
1530Sstevel@tonic-gate 	struct hostent *hp;
1540Sstevel@tonic-gate 	int on = 1;
1550Sstevel@tonic-gate 	char *cp;
1560Sstevel@tonic-gate 	struct stat sb;
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 	if (getuid()) {
1590Sstevel@tonic-gate 		(void) fprintf(stderr, "in.rwhod: not super user\n");
1600Sstevel@tonic-gate 		exit(1);
1610Sstevel@tonic-gate 	}
1620Sstevel@tonic-gate 	sp = getservbyname("who", "udp");
1630Sstevel@tonic-gate 	if (sp == NULL) {
1640Sstevel@tonic-gate 		(void) fprintf(stderr, "in.rwhod: udp/who: unknown service\n");
1650Sstevel@tonic-gate 		exit(1);
1660Sstevel@tonic-gate 	}
1670Sstevel@tonic-gate 	argv++;
1680Sstevel@tonic-gate 	argc--;
1690Sstevel@tonic-gate 	while (argc > 0 && *argv[0] == '-') {
1700Sstevel@tonic-gate 		if (strcmp(*argv, "-m") == 0) {
1710Sstevel@tonic-gate 			if (argc > 1 && isdigit(*(argv + 1)[0])) {
1720Sstevel@tonic-gate 				argv++;
1730Sstevel@tonic-gate 				argc--;
1740Sstevel@tonic-gate 				multicast_mode  = SCOPED_MULTICAST;
1750Sstevel@tonic-gate 				multicast_scope = atoi(*argv);
1760Sstevel@tonic-gate 				if (multicast_scope > MAX_MULTICAST_SCOPE) {
1770Sstevel@tonic-gate 					(void) fprintf(stderr,
1780Sstevel@tonic-gate 					    "in.rwhod: "
1790Sstevel@tonic-gate 					    "ttl must not exceed %u\n",
1800Sstevel@tonic-gate 					    MAX_MULTICAST_SCOPE);
1810Sstevel@tonic-gate 					exit(1);
1820Sstevel@tonic-gate 				}
1830Sstevel@tonic-gate 			} else {
1840Sstevel@tonic-gate 				multicast_mode = PER_INTERFACE_MULTICAST;
1850Sstevel@tonic-gate 			}
1860Sstevel@tonic-gate 		} else {
1870Sstevel@tonic-gate 			goto usage;
1880Sstevel@tonic-gate 		}
1890Sstevel@tonic-gate 		argv++;
1900Sstevel@tonic-gate 		argc--;
1910Sstevel@tonic-gate 	}
1920Sstevel@tonic-gate 	if (argc > 0)
1930Sstevel@tonic-gate 		goto usage;
1940Sstevel@tonic-gate 	if (chdir(RWHODIR) < 0) {
1950Sstevel@tonic-gate 		perror(RWHODIR);
1960Sstevel@tonic-gate 		exit(1);
1970Sstevel@tonic-gate 	}
1980Sstevel@tonic-gate #ifndef DEBUG
1990Sstevel@tonic-gate 	if (fork())
2000Sstevel@tonic-gate 		exit(0);
2010Sstevel@tonic-gate 	/* CSTYLED */
2020Sstevel@tonic-gate 	{
2030Sstevel@tonic-gate 		(void) close(0);
2040Sstevel@tonic-gate 		(void) close(1);
2050Sstevel@tonic-gate 		(void) close(2);
2060Sstevel@tonic-gate 		(void) open("/", 0);
2070Sstevel@tonic-gate 		(void) dup2(0, 1);
2080Sstevel@tonic-gate 		(void) dup2(0, 2);
2090Sstevel@tonic-gate 		(void) setsid();
2100Sstevel@tonic-gate 	}
2110Sstevel@tonic-gate #endif
2120Sstevel@tonic-gate 	(void) sigset(SIGHUP, (void (*)())getkmem);
2130Sstevel@tonic-gate 	openlog("in.rwhod", LOG_PID, LOG_DAEMON);
2140Sstevel@tonic-gate 	/*
2150Sstevel@tonic-gate 	 * Establish host name as returned by system.
2160Sstevel@tonic-gate 	 */
2170Sstevel@tonic-gate 	if (gethostname(myname, sizeof (myname) - 1) < 0) {
2180Sstevel@tonic-gate 		syslog(LOG_ERR, "main: gethostname: %m");
2190Sstevel@tonic-gate 		exit(1);
2200Sstevel@tonic-gate 	}
2210Sstevel@tonic-gate 	if ((cp = index(myname, '.')) != NULL)
2220Sstevel@tonic-gate 		*cp = '\0';
2230Sstevel@tonic-gate 	(void) strlcpy(mywd.wd_hostname, myname, sizeof (mywd.wd_hostname));
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate 	if (stat(UTMPX_FILE, &sb) < 0) {
2260Sstevel@tonic-gate 		syslog(LOG_ERR, "main: stat: %s: %m", UTMPX_FILE);
2270Sstevel@tonic-gate 		exit(1);
2280Sstevel@tonic-gate 	}
2290Sstevel@tonic-gate 	getkmem();
2300Sstevel@tonic-gate 	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
2310Sstevel@tonic-gate 		syslog(LOG_ERR, "main: socket: %m");
2320Sstevel@tonic-gate 		exit(1);
2330Sstevel@tonic-gate 	}
2340Sstevel@tonic-gate 	if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0) {
2350Sstevel@tonic-gate 		syslog(LOG_ERR, "main: setsockopt SO_BROADCAST: %m");
2360Sstevel@tonic-gate 		exit(1);
2370Sstevel@tonic-gate 	}
2380Sstevel@tonic-gate 	hp = gethostbyname(myname);
2390Sstevel@tonic-gate 	if (hp == NULL) {
2400Sstevel@tonic-gate 		syslog(LOG_ERR, "main: %s: don't know my own name\n", myname);
2410Sstevel@tonic-gate 		exit(1);
2420Sstevel@tonic-gate 	}
2430Sstevel@tonic-gate 	sin.sin_family = hp->h_addrtype;
2440Sstevel@tonic-gate 	sin.sin_port = sp->s_port;
2450Sstevel@tonic-gate 	if (bind(s, (struct sockaddr *)&sin, sizeof (sin)) < 0) {
2460Sstevel@tonic-gate 		syslog(LOG_ERR, "main: bind: %m");
2470Sstevel@tonic-gate 		exit(1);
2480Sstevel@tonic-gate 	}
2490Sstevel@tonic-gate 	if (!configure(s))
2500Sstevel@tonic-gate 		exit(1);
2510Sstevel@tonic-gate 	(void) sigset(SIGALRM, (void (*)())onalrm);
2520Sstevel@tonic-gate 	onalrm();
2530Sstevel@tonic-gate 	for (;;) {
2540Sstevel@tonic-gate 		struct whod wd;
2550Sstevel@tonic-gate 		int cc, whod;
2560Sstevel@tonic-gate 		socklen_t len = sizeof (from);
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate 		cc = recvfrom(s, &wd, sizeof (struct whod), 0,
2590Sstevel@tonic-gate 		    (struct sockaddr *)&from, &len);
2600Sstevel@tonic-gate 		if (cc <= 0) {
2610Sstevel@tonic-gate 			if (cc < 0 && errno != EINTR)
2620Sstevel@tonic-gate 				syslog(LOG_WARNING, "main: recvfrom: %m");
2630Sstevel@tonic-gate 			continue;
2640Sstevel@tonic-gate 		}
2650Sstevel@tonic-gate 		if (from.sin_port != sp->s_port) {
2660Sstevel@tonic-gate 			syslog(LOG_WARNING, "main: %d: bad from port",
2670Sstevel@tonic-gate 			    ntohs(from.sin_port));
2680Sstevel@tonic-gate 			continue;
2690Sstevel@tonic-gate 		}
2700Sstevel@tonic-gate #ifdef notdef
2710Sstevel@tonic-gate 		if (gethostbyname(wd.wd_hostname) == 0) {
2720Sstevel@tonic-gate 			syslog(LOG_WARNING, "main: %s: unknown host",
2730Sstevel@tonic-gate 			    wd.wd_hostname);
2740Sstevel@tonic-gate 			continue;
2750Sstevel@tonic-gate 		}
2760Sstevel@tonic-gate #endif
2770Sstevel@tonic-gate 		if (wd.wd_vers != WHODVERSION)
2780Sstevel@tonic-gate 			continue;
2790Sstevel@tonic-gate 		if (wd.wd_type != WHODTYPE_STATUS)
2800Sstevel@tonic-gate 			continue;
2810Sstevel@tonic-gate 		if (!verify(&wd)) {
2820Sstevel@tonic-gate 			syslog(LOG_WARNING, "main: malformed host name from %x",
2830Sstevel@tonic-gate 			    from.sin_addr.s_addr);
2840Sstevel@tonic-gate 			continue;
2850Sstevel@tonic-gate 		}
2860Sstevel@tonic-gate 		(void) sprintf(path, "whod.%s", wd.wd_hostname);
2870Sstevel@tonic-gate 		/*
2880Sstevel@tonic-gate 		 * Rather than truncating and growing the file each time,
2890Sstevel@tonic-gate 		 * use ftruncate if size is less than previous size.
2900Sstevel@tonic-gate 		 */
2910Sstevel@tonic-gate 		whod = open(path, O_WRONLY | O_CREAT, 0644);
2920Sstevel@tonic-gate 		if (whod < 0) {
2930Sstevel@tonic-gate 			syslog(LOG_WARNING, "main: open: %s: %m", path);
2940Sstevel@tonic-gate 			continue;
2950Sstevel@tonic-gate 		}
2960Sstevel@tonic-gate #if defined(_LITTLE_ENDIAN)
2970Sstevel@tonic-gate 		/* CSTYLED */
2980Sstevel@tonic-gate 		{
2990Sstevel@tonic-gate 			int i, n = (cc - WHDRSIZE)/sizeof (struct whoent);
3000Sstevel@tonic-gate 			struct whoent *we;
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate 			/* undo header byte swapping before writing to file */
3030Sstevel@tonic-gate 			wd.wd_sendtime = ntohl(wd.wd_sendtime);
3040Sstevel@tonic-gate 			for (i = 0; i < 3; i++)
3050Sstevel@tonic-gate 				wd.wd_loadav[i] = ntohl(wd.wd_loadav[i]);
3060Sstevel@tonic-gate 			wd.wd_boottime = ntohl(wd.wd_boottime);
3070Sstevel@tonic-gate 			we = wd.wd_we;
3080Sstevel@tonic-gate 			for (i = 0; i < n; i++) {
3090Sstevel@tonic-gate 				we->we_idle = ntohl(we->we_idle);
3100Sstevel@tonic-gate 				we->we_utmp.out_time =
3110Sstevel@tonic-gate 				    ntohl(we->we_utmp.out_time);
3120Sstevel@tonic-gate 				we++;
3130Sstevel@tonic-gate 			}
3140Sstevel@tonic-gate 		}
3150Sstevel@tonic-gate #endif
3160Sstevel@tonic-gate 		(void) time((time_t *)&wd.wd_recvtime);
3170Sstevel@tonic-gate 		(void) write(whod, &wd, cc);
3180Sstevel@tonic-gate 		if (fstat(whod, &st) < 0 || st.st_size > cc)
3190Sstevel@tonic-gate 			(void) ftruncate(whod, cc);
3200Sstevel@tonic-gate 		(void) close(whod);
3210Sstevel@tonic-gate 	}
3220Sstevel@tonic-gate 	/* NOTREACHED */
3230Sstevel@tonic-gate usage:
3240Sstevel@tonic-gate 	(void) fprintf(stderr, "usage: in.rwhod [ -m [ ttl ] ]\n");
325*473Sbw 	return (1);
3260Sstevel@tonic-gate }
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate /*
3290Sstevel@tonic-gate  * Check out host name for unprintables
3300Sstevel@tonic-gate  * and other funnies before allowing a file
3310Sstevel@tonic-gate  * to be created.  Sorry, but blanks aren't allowed.
3320Sstevel@tonic-gate  */
3330Sstevel@tonic-gate static int
verify(const struct whod * wd)3340Sstevel@tonic-gate verify(const struct whod *wd)
3350Sstevel@tonic-gate {
3360Sstevel@tonic-gate 	int size = 0;
3370Sstevel@tonic-gate 	const char *name = wd->wd_hostname;
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate 	/*
3400Sstevel@tonic-gate 	 * We shouldn't assume the name is NUL terminated, so bound the
3410Sstevel@tonic-gate 	 * checks at the size of the whod structures wd_hostname field.
3420Sstevel@tonic-gate 	 */
3430Sstevel@tonic-gate 	while ((size < sizeof (wd->wd_hostname)) &&
3440Sstevel@tonic-gate 	    (*name != '\0')) {
3450Sstevel@tonic-gate 		if (*name == '/' || !isascii(*name) ||
3460Sstevel@tonic-gate 		    !(isalnum(*name) || ispunct(*name)))
3470Sstevel@tonic-gate 			return (0);
3480Sstevel@tonic-gate 		name++, size++;
3490Sstevel@tonic-gate 	}
3500Sstevel@tonic-gate 	/*
3510Sstevel@tonic-gate 	 * Fail the verification if NULL name or it wasn't NUL terminated.
3520Sstevel@tonic-gate 	 */
3530Sstevel@tonic-gate 	return ((size > 0) && (size < sizeof (wd->wd_hostname)));
3540Sstevel@tonic-gate }
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate static int	utmpxtime;
3570Sstevel@tonic-gate static int	utmpxent;
3580Sstevel@tonic-gate static int	alarmcount;
3590Sstevel@tonic-gate struct	utmpx *utmpx;
3600Sstevel@tonic-gate 
3610Sstevel@tonic-gate static void
onalrm(void)3620Sstevel@tonic-gate onalrm(void)
3630Sstevel@tonic-gate {
3640Sstevel@tonic-gate 	int i;
3650Sstevel@tonic-gate 	struct stat stb;
3660Sstevel@tonic-gate 	int	utmpxsize = 0;
3670Sstevel@tonic-gate 	int	entries;
3680Sstevel@tonic-gate 	struct	utmpx *utp;
3690Sstevel@tonic-gate 	struct	utmpx *utmpxbegin;
3700Sstevel@tonic-gate 	struct whoent *we = mywd.wd_we, *wlast;
3710Sstevel@tonic-gate 	int cc, cnt;
3720Sstevel@tonic-gate 	double avenrun[3];
3730Sstevel@tonic-gate 
3740Sstevel@tonic-gate 	time_t now = time(0);
3750Sstevel@tonic-gate 	struct neighbor *np;
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate 	if (alarmcount % 10 == 0)
3780Sstevel@tonic-gate 		getkmem();
3790Sstevel@tonic-gate 	alarmcount++;
3800Sstevel@tonic-gate 	(void) stat(UTMPX_FILE, &stb);
3810Sstevel@tonic-gate 	entries = stb.st_size / sizeof (struct futmpx);
3820Sstevel@tonic-gate 	if ((stb.st_mtime != utmpxtime) || (entries > utmpxent)) {
3830Sstevel@tonic-gate 		utmpxtime = stb.st_mtime;
3840Sstevel@tonic-gate 		if (entries > utmpxent) {
3850Sstevel@tonic-gate 			utmpxent = entries;
3860Sstevel@tonic-gate 			utmpxsize = utmpxent * sizeof (struct utmpx);
3870Sstevel@tonic-gate 			utmpx = realloc(utmpx, utmpxsize);
3880Sstevel@tonic-gate 			if (utmpx == NULL) {
3890Sstevel@tonic-gate 				syslog(LOG_ERR, "onalrm: realloc: %m");
3900Sstevel@tonic-gate 				utmpxsize = 0;
3910Sstevel@tonic-gate 				goto done;
3920Sstevel@tonic-gate 			}
3930Sstevel@tonic-gate 		}
3940Sstevel@tonic-gate 		utmpxbegin = utmpx;
3950Sstevel@tonic-gate 		setutxent();
3960Sstevel@tonic-gate 		cnt = 0;
3970Sstevel@tonic-gate 		while (cnt++ < utmpxent && (utp = getutxent()) != NULL)
3980Sstevel@tonic-gate 			(void) memcpy(utmpxbegin++, utp, sizeof (struct utmpx));
3990Sstevel@tonic-gate 		endutxent();
4000Sstevel@tonic-gate 		wlast = &mywd.wd_we[1024 / sizeof (struct whoent) - 1];
4010Sstevel@tonic-gate 		for (i = 0; i < utmpxent; i++) {
4020Sstevel@tonic-gate 			if (utmpx[i].ut_name[0] &&
4030Sstevel@tonic-gate 			    utmpx[i].ut_type == USER_PROCESS) {
4040Sstevel@tonic-gate 				/*
4050Sstevel@tonic-gate 				 * XXX - utmpx name and line lengths should
4060Sstevel@tonic-gate 				 * be here
4070Sstevel@tonic-gate 				 */
4080Sstevel@tonic-gate 				bcopy(utmpx[i].ut_line, we->we_utmp.out_line,
4090Sstevel@tonic-gate 				    sizeof (we->we_utmp.out_line));
4100Sstevel@tonic-gate 				bcopy(utmpx[i].ut_name, we->we_utmp.out_name,
4110Sstevel@tonic-gate 				    sizeof (we->we_utmp.out_name));
4120Sstevel@tonic-gate 				we->we_utmp.out_time =
4130Sstevel@tonic-gate 				    htonl(utmpx[i].ut_xtime);
4140Sstevel@tonic-gate 				if (we >= wlast)
4150Sstevel@tonic-gate 					break;
4160Sstevel@tonic-gate 				we++;
4170Sstevel@tonic-gate 			}
4180Sstevel@tonic-gate 		}
4190Sstevel@tonic-gate 		utmpxent = we - mywd.wd_we;
4200Sstevel@tonic-gate 	}
4210Sstevel@tonic-gate 
4220Sstevel@tonic-gate 	/*
4230Sstevel@tonic-gate 	 * The test on utmpxent looks silly---after all, if no one is
4240Sstevel@tonic-gate 	 * logged on, why worry about efficiency?---but is useful on
4250Sstevel@tonic-gate 	 * (e.g.) compute servers.
4260Sstevel@tonic-gate 	 */
4270Sstevel@tonic-gate 	if (utmpxent > 0 && chdir("/dev") == -1) {
4280Sstevel@tonic-gate 		syslog(LOG_ERR, "onalrm: chdir /dev: %m");
4290Sstevel@tonic-gate 		exit(1);
4300Sstevel@tonic-gate 	}
4310Sstevel@tonic-gate 	we = mywd.wd_we;
4320Sstevel@tonic-gate 	for (i = 0; i < utmpxent; i++) {
4330Sstevel@tonic-gate 		if (stat(we->we_utmp.out_line, &stb) >= 0)
4340Sstevel@tonic-gate 			we->we_idle = htonl(now - stb.st_atime);
4350Sstevel@tonic-gate 		we++;
4360Sstevel@tonic-gate 	}
4370Sstevel@tonic-gate 	if (getloadavg(avenrun, 3) == -1) {
4380Sstevel@tonic-gate 		syslog(LOG_ERR, "onalrm: getloadavg: %m");
4390Sstevel@tonic-gate 		exit(1);
4400Sstevel@tonic-gate 	}
4410Sstevel@tonic-gate 
4420Sstevel@tonic-gate 	for (i = 0; i < 3; i++)
4430Sstevel@tonic-gate 		mywd.wd_loadav[i] = htonl((ulong_t)(avenrun[i] * 100));
4440Sstevel@tonic-gate 	cc = (char *)we - (char *)&mywd;
4450Sstevel@tonic-gate 	mywd.wd_sendtime = htonl(time(0));
4460Sstevel@tonic-gate 	mywd.wd_vers = WHODVERSION;
4470Sstevel@tonic-gate 	mywd.wd_type = WHODTYPE_STATUS;
4480Sstevel@tonic-gate 	if (multicast_mode == SCOPED_MULTICAST) {
4490Sstevel@tonic-gate 		(void) sendto(s, &mywd, cc, 0,
4500Sstevel@tonic-gate 		    (struct sockaddr *)&multicast_addr,
4510Sstevel@tonic-gate 		    sizeof (multicast_addr));
4520Sstevel@tonic-gate 	} else for (np = neighbors; np != NULL; np = np->n_next) {
4530Sstevel@tonic-gate 		if (multicast_mode == PER_INTERFACE_MULTICAST &&
4540Sstevel@tonic-gate 		    np->n_flags & IFF_MULTICAST) {
4550Sstevel@tonic-gate 			/*
4560Sstevel@tonic-gate 			 * Select the outgoing interface for the multicast.
4570Sstevel@tonic-gate 			 */
4580Sstevel@tonic-gate 			if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF,
4590Sstevel@tonic-gate 			    &(((struct sockaddr_in *)np->n_addr)->sin_addr),
4600Sstevel@tonic-gate 			    sizeof (struct in_addr)) < 0) {
4610Sstevel@tonic-gate 				syslog(LOG_ERR,
4620Sstevel@tonic-gate 				    "onalrm: setsockopt IP_MULTICAST_IF: %m");
4630Sstevel@tonic-gate 				exit(1);
4640Sstevel@tonic-gate 			}
4650Sstevel@tonic-gate 			(void) sendto(s, &mywd, cc, 0,
4660Sstevel@tonic-gate 			    (struct sockaddr *)&multicast_addr,
4670Sstevel@tonic-gate 			    sizeof (multicast_addr));
4680Sstevel@tonic-gate 		} else {
4690Sstevel@tonic-gate 			(void) sendto(s, &mywd, cc, 0,
4700Sstevel@tonic-gate 			    (struct sockaddr *)np->n_addr, np->n_addrlen);
4710Sstevel@tonic-gate 		}
4720Sstevel@tonic-gate 	}
4730Sstevel@tonic-gate 	if (utmpxent > 0 && chdir(RWHODIR) == -1) {
4740Sstevel@tonic-gate 		syslog(LOG_ERR, "onalrm: chdir %s: %m", RWHODIR);
4750Sstevel@tonic-gate 		exit(1);
4760Sstevel@tonic-gate 	}
4770Sstevel@tonic-gate done:
4780Sstevel@tonic-gate 	(void) alarm(AL_INTERVAL);
4790Sstevel@tonic-gate }
4800Sstevel@tonic-gate 
4810Sstevel@tonic-gate static void
getkmem(void)4820Sstevel@tonic-gate getkmem(void)
4830Sstevel@tonic-gate {
4840Sstevel@tonic-gate 	struct utmpx *utmpx, utmpx_id;
4850Sstevel@tonic-gate 
4860Sstevel@tonic-gate 	utmpx_id.ut_type = BOOT_TIME;
4870Sstevel@tonic-gate 	if ((utmpx = getutxid(&utmpx_id)) != NULL)
4880Sstevel@tonic-gate 		mywd.wd_boottime = utmpx->ut_xtime;
4890Sstevel@tonic-gate 	endutxent();
4900Sstevel@tonic-gate 	mywd.wd_boottime = htonl(mywd.wd_boottime);
4910Sstevel@tonic-gate }
4920Sstevel@tonic-gate 
4930Sstevel@tonic-gate /*
4940Sstevel@tonic-gate  * Figure out device configuration and select
4950Sstevel@tonic-gate  * networks which deserve status information.
4960Sstevel@tonic-gate  */
4970Sstevel@tonic-gate static boolean_t
configure(int s)4980Sstevel@tonic-gate configure(int s)
4990Sstevel@tonic-gate {
5000Sstevel@tonic-gate 	char *buf;
5010Sstevel@tonic-gate 	struct ifconf ifc;
5020Sstevel@tonic-gate 	struct ifreq ifreq, *ifr;
5030Sstevel@tonic-gate 	struct sockaddr_in *sin;
5040Sstevel@tonic-gate 	struct neighbor *np;
5050Sstevel@tonic-gate 	struct neighbor *np2;
5060Sstevel@tonic-gate 	int n;
5070Sstevel@tonic-gate 	int numifs;
5080Sstevel@tonic-gate 	unsigned bufsize;
5090Sstevel@tonic-gate 
5100Sstevel@tonic-gate 	if (multicast_mode == SCOPED_MULTICAST) {
5110Sstevel@tonic-gate 		struct ip_mreq mreq;
5120Sstevel@tonic-gate 		unsigned char ttl;
5130Sstevel@tonic-gate 
5140Sstevel@tonic-gate 		mreq.imr_multiaddr.s_addr = htonl(INADDR_WHOD_GROUP);
5150Sstevel@tonic-gate 		mreq.imr_interface.s_addr = htonl(INADDR_ANY);
5160Sstevel@tonic-gate 		if (setsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq,
5170Sstevel@tonic-gate 		    sizeof (mreq)) < 0) {
5180Sstevel@tonic-gate 			syslog(LOG_ERR,
5190Sstevel@tonic-gate 			    "configure: setsockopt IP_ADD_MEMBERSHIP: %m");
5200Sstevel@tonic-gate 			return (B_FALSE);
5210Sstevel@tonic-gate 		}
5220Sstevel@tonic-gate 		ttl = multicast_scope;
5230Sstevel@tonic-gate 		if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, &ttl,
5240Sstevel@tonic-gate 		    sizeof (ttl)) < 0) {
5250Sstevel@tonic-gate 			syslog(LOG_ERR,
5260Sstevel@tonic-gate 			    "configure: setsockopt IP_MULTICAST_TTL: %m");
5270Sstevel@tonic-gate 			return (B_FALSE);
5280Sstevel@tonic-gate 		}
5290Sstevel@tonic-gate 		multicast_addr.sin_addr.s_addr = htonl(INADDR_WHOD_GROUP);
5300Sstevel@tonic-gate 		multicast_addr.sin_port = sp->s_port;
5310Sstevel@tonic-gate 		return (B_TRUE);
5320Sstevel@tonic-gate 	}
5330Sstevel@tonic-gate 
5340Sstevel@tonic-gate 	if (ioctl(s, SIOCGIFNUM, (char *)&numifs) < 0) {
5350Sstevel@tonic-gate 		syslog(LOG_ERR, "configure: ioctl SIOCGIFNUM: %m");
5360Sstevel@tonic-gate 		return (B_FALSE);
5370Sstevel@tonic-gate 	}
5380Sstevel@tonic-gate 	bufsize = numifs * sizeof (struct ifreq);
5390Sstevel@tonic-gate 	buf = malloc(bufsize);
5400Sstevel@tonic-gate 	if (buf == NULL) {
5410Sstevel@tonic-gate 		syslog(LOG_ERR, "configure: malloc: %m");
5420Sstevel@tonic-gate 		return (B_FALSE);
5430Sstevel@tonic-gate 	}
5440Sstevel@tonic-gate 	ifc.ifc_len = bufsize;
5450Sstevel@tonic-gate 	ifc.ifc_buf = buf;
5460Sstevel@tonic-gate 	if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0) {
5470Sstevel@tonic-gate 		syslog(LOG_ERR,
5480Sstevel@tonic-gate 		    "configure: ioctl (get interface configuration): %m");
5490Sstevel@tonic-gate 		(void) free(buf);
5500Sstevel@tonic-gate 		return (B_FALSE);
5510Sstevel@tonic-gate 	}
5520Sstevel@tonic-gate 	ifr = ifc.ifc_req;
5530Sstevel@tonic-gate 	for (n = ifc.ifc_len / sizeof (struct ifreq); n > 0; n--, ifr++) {
5540Sstevel@tonic-gate 		/* Skip all logical interfaces */
5550Sstevel@tonic-gate 		if (index(ifr->ifr_name, ':') != NULL)
5560Sstevel@tonic-gate 			continue;
5570Sstevel@tonic-gate 
5580Sstevel@tonic-gate 		for (np = neighbors; np != NULL; np = np->n_next) {
5590Sstevel@tonic-gate 			if (np->n_name &&
5600Sstevel@tonic-gate 			    strcmp(ifr->ifr_name, np->n_name) == 0)
5610Sstevel@tonic-gate 				break;
5620Sstevel@tonic-gate 		}
5630Sstevel@tonic-gate 		if (np != NULL)
5640Sstevel@tonic-gate 			continue;
5650Sstevel@tonic-gate 		ifreq = *ifr;
5660Sstevel@tonic-gate 		np = (struct neighbor *)malloc(sizeof (*np));
5670Sstevel@tonic-gate 		if (np == NULL)
5680Sstevel@tonic-gate 			continue;
5690Sstevel@tonic-gate 		np->n_name = malloc(strlen(ifr->ifr_name) + 1);
5700Sstevel@tonic-gate 		if (np->n_name == NULL) {
5710Sstevel@tonic-gate 			free(np);
5720Sstevel@tonic-gate 			continue;
5730Sstevel@tonic-gate 		}
5740Sstevel@tonic-gate 		(void) strcpy(np->n_name, ifr->ifr_name);
5750Sstevel@tonic-gate 		np->n_addrlen = sizeof (ifr->ifr_addr);
5760Sstevel@tonic-gate 		np->n_addr = malloc(np->n_addrlen);
5770Sstevel@tonic-gate 		if (np->n_addr == NULL) {
5780Sstevel@tonic-gate 			free(np->n_name);
5790Sstevel@tonic-gate 			free(np);
5800Sstevel@tonic-gate 			continue;
5810Sstevel@tonic-gate 		}
5820Sstevel@tonic-gate 		bcopy(&ifr->ifr_addr, np->n_addr, np->n_addrlen);
5830Sstevel@tonic-gate 		if (ioctl(s, SIOCGIFFLAGS, (char *)&ifreq) < 0) {
5840Sstevel@tonic-gate 			syslog(LOG_ERR,
5850Sstevel@tonic-gate 			    "configure: ioctl (get interface flags): %m");
5860Sstevel@tonic-gate 			free(np->n_addr);
5870Sstevel@tonic-gate 			free(np->n_name);
5880Sstevel@tonic-gate 			free(np);
5890Sstevel@tonic-gate 			continue;
5900Sstevel@tonic-gate 		}
5910Sstevel@tonic-gate 		np->n_flags = ifreq.ifr_flags;
5920Sstevel@tonic-gate 		if (((struct sockaddr_in *)np->n_addr)->sin_family == AF_INET &&
5930Sstevel@tonic-gate 		    ioctl(s, SIOCGIFNETMASK, (char *)&ifreq) >= 0) {
5940Sstevel@tonic-gate 			sin = (struct sockaddr_in *)np->n_addr;
5950Sstevel@tonic-gate 
5960Sstevel@tonic-gate 			np->n_subnet = sin->sin_addr.s_addr &
5970Sstevel@tonic-gate 			    ((struct sockaddr_in *)&ifreq.ifr_addr)->
5980Sstevel@tonic-gate 			    sin_addr.s_addr;
5990Sstevel@tonic-gate 		}
6000Sstevel@tonic-gate 		if (multicast_mode == PER_INTERFACE_MULTICAST &&
6010Sstevel@tonic-gate 		    (np->n_flags & IFF_UP) &&
6020Sstevel@tonic-gate 		    (np->n_flags & IFF_MULTICAST) &&
6030Sstevel@tonic-gate 		    !(np->n_flags & IFF_LOOPBACK)) {
6040Sstevel@tonic-gate 			struct ip_mreq mreq;
6050Sstevel@tonic-gate 
6060Sstevel@tonic-gate 			/*
6070Sstevel@tonic-gate 			 * Skip interfaces that have matching subnets i.e.
6080Sstevel@tonic-gate 			 * (addr & netmask) are identical.
6090Sstevel@tonic-gate 			 * Such interfaces are connected to the same
6100Sstevel@tonic-gate 			 * physical wire.
6110Sstevel@tonic-gate 			 */
6120Sstevel@tonic-gate 			for (np2 = neighbors; np2 != NULL; np2 = np2->n_next) {
6130Sstevel@tonic-gate 
6140Sstevel@tonic-gate 				if (!(np->n_flags & IFF_POINTOPOINT) &&
6150Sstevel@tonic-gate 				    !(np2->n_flags & IFF_POINTOPOINT) &&
6160Sstevel@tonic-gate 				    (np->n_subnet == np2->n_subnet)) {
6170Sstevel@tonic-gate 					free(np->n_addr);
6180Sstevel@tonic-gate 					free(np->n_name);
6190Sstevel@tonic-gate 					free(np);
6200Sstevel@tonic-gate 					break;
6210Sstevel@tonic-gate 				}
6220Sstevel@tonic-gate 			}
6230Sstevel@tonic-gate 			if (np2 != NULL)
6240Sstevel@tonic-gate 				continue;
6250Sstevel@tonic-gate 
6260Sstevel@tonic-gate 			mreq.imr_multiaddr.s_addr = htonl(INADDR_WHOD_GROUP);
6270Sstevel@tonic-gate 			mreq.imr_interface.s_addr =
6280Sstevel@tonic-gate 			    ((struct sockaddr_in *)np->n_addr)->sin_addr.s_addr;
6290Sstevel@tonic-gate 			if (setsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq,
6300Sstevel@tonic-gate 			    sizeof (mreq)) < 0) {
6310Sstevel@tonic-gate 				syslog(LOG_ERR,
6320Sstevel@tonic-gate 				    "configure: "
6330Sstevel@tonic-gate 				    "setsockopt IP_ADD_MEMBERSHIP: %m");
6340Sstevel@tonic-gate 				free(np->n_addr);
6350Sstevel@tonic-gate 				free(np->n_name);
6360Sstevel@tonic-gate 				free(np);
6370Sstevel@tonic-gate 				continue;
6380Sstevel@tonic-gate 			}
6390Sstevel@tonic-gate 			multicast_addr.sin_addr.s_addr =
6400Sstevel@tonic-gate 			    htonl(INADDR_WHOD_GROUP);
6410Sstevel@tonic-gate 			multicast_addr.sin_port = sp->s_port;
6420Sstevel@tonic-gate 			np->n_next = neighbors;
6430Sstevel@tonic-gate 			neighbors = np;
6440Sstevel@tonic-gate 			continue;
6450Sstevel@tonic-gate 		}
6460Sstevel@tonic-gate 		if ((np->n_flags & IFF_UP) == 0 ||
6470Sstevel@tonic-gate 		    (np->n_flags & (IFF_BROADCAST|IFF_POINTOPOINT)) == 0) {
6480Sstevel@tonic-gate 			free(np->n_addr);
6490Sstevel@tonic-gate 			free(np->n_name);
6500Sstevel@tonic-gate 			free(np);
6510Sstevel@tonic-gate 			continue;
6520Sstevel@tonic-gate 		}
6530Sstevel@tonic-gate 		if (np->n_flags & IFF_POINTOPOINT) {
6540Sstevel@tonic-gate 			if (ioctl(s, SIOCGIFDSTADDR, (char *)&ifreq) < 0) {
6550Sstevel@tonic-gate 				syslog(LOG_ERR,
6560Sstevel@tonic-gate 				    "configure: ioctl (get dstaddr): %m");
6570Sstevel@tonic-gate 				free(np->n_addr);
6580Sstevel@tonic-gate 				free(np->n_name);
6590Sstevel@tonic-gate 				free(np);
6600Sstevel@tonic-gate 				continue;
6610Sstevel@tonic-gate 			}
6620Sstevel@tonic-gate 			/* we assume addresses are all the same size */
6630Sstevel@tonic-gate 			bcopy(&ifreq.ifr_dstaddr, np->n_addr, np->n_addrlen);
6640Sstevel@tonic-gate 		}
6650Sstevel@tonic-gate 		if (np->n_flags & IFF_BROADCAST) {
6660Sstevel@tonic-gate 			if (ioctl(s, SIOCGIFBRDADDR, (char *)&ifreq) < 0) {
6670Sstevel@tonic-gate 				syslog(LOG_ERR,
6680Sstevel@tonic-gate 				    "configure: ioctl (get broadaddr): %m");
6690Sstevel@tonic-gate 				free(np->n_addr);
6700Sstevel@tonic-gate 				free(np->n_name);
6710Sstevel@tonic-gate 				free(np);
6720Sstevel@tonic-gate 				continue;
6730Sstevel@tonic-gate 			}
6740Sstevel@tonic-gate 			/* we assume addresses are all the same size */
6750Sstevel@tonic-gate 			bcopy(&ifreq.ifr_broadaddr, np->n_addr, np->n_addrlen);
6760Sstevel@tonic-gate 		}
6770Sstevel@tonic-gate 		/* gag, wish we could get rid of Internet dependencies */
6780Sstevel@tonic-gate 		sin = (struct sockaddr_in *)np->n_addr;
6790Sstevel@tonic-gate 		sin->sin_port = sp->s_port;
6800Sstevel@tonic-gate 
6810Sstevel@tonic-gate 		/*
6820Sstevel@tonic-gate 		 * Avoid adding duplicate broadcast and pt-pt destinations
6830Sstevel@tonic-gate 		 * to the list.
6840Sstevel@tonic-gate 		 */
6850Sstevel@tonic-gate 		for (np2 = neighbors; np2 != NULL; np2 = np2->n_next) {
6860Sstevel@tonic-gate 			struct sockaddr_in *sin2;
6870Sstevel@tonic-gate 
6880Sstevel@tonic-gate 			sin2 = (struct sockaddr_in *)np2->n_addr;
6890Sstevel@tonic-gate 			if (sin2->sin_addr.s_addr == sin->sin_addr.s_addr) {
6900Sstevel@tonic-gate 				free(np->n_addr);
6910Sstevel@tonic-gate 				free(np->n_name);
6920Sstevel@tonic-gate 				free(np);
6930Sstevel@tonic-gate 				break;
6940Sstevel@tonic-gate 			}
6950Sstevel@tonic-gate 		}
6960Sstevel@tonic-gate 		if (np2 != NULL)
6970Sstevel@tonic-gate 			continue;
6980Sstevel@tonic-gate 
6990Sstevel@tonic-gate 		np->n_next = neighbors;
7000Sstevel@tonic-gate 		neighbors = np;
7010Sstevel@tonic-gate 	}
7020Sstevel@tonic-gate 	(void) free(buf);
7030Sstevel@tonic-gate 	return (B_TRUE);
7040Sstevel@tonic-gate }
7050Sstevel@tonic-gate 
7060Sstevel@tonic-gate #ifdef DEBUG
7070Sstevel@tonic-gate static char *interval(uint_t, char *);
7080Sstevel@tonic-gate 
7090Sstevel@tonic-gate /* ARGSUSED */
7100Sstevel@tonic-gate static ssize_t
sendto(int s,const void * buf,size_t cc,int flags,const struct sockaddr * to,socklen_t tolen)7110Sstevel@tonic-gate sendto(int s, const void *buf, size_t cc, int flags, const struct sockaddr *to,
7120Sstevel@tonic-gate     socklen_t tolen)
7130Sstevel@tonic-gate {
7140Sstevel@tonic-gate 	struct whod *w = (struct whod *)buf;
7150Sstevel@tonic-gate 	struct whoent *we;
7160Sstevel@tonic-gate 	struct sockaddr_in *sin = (struct sockaddr_in *)to;
7170Sstevel@tonic-gate 	int nsz;
7180Sstevel@tonic-gate 
7190Sstevel@tonic-gate 	(void) printf("sendto %x.%d\n", ntohl(sin->sin_addr.s_addr),
7200Sstevel@tonic-gate 	    ntohs(sin->sin_port));
7210Sstevel@tonic-gate 	(void) printf("hostname %s %s\n", w->wd_hostname,
7220Sstevel@tonic-gate 	    interval(ntohl(w->wd_sendtime) - ntohl(w->wd_boottime), "  up"));
7230Sstevel@tonic-gate 	(void) printf("load %4.2f, %4.2f, %4.2f\n",
7240Sstevel@tonic-gate 	    ntohl(w->wd_loadav[0]) / 100.0, ntohl(w->wd_loadav[1]) / 100.0,
7250Sstevel@tonic-gate 	    ntohl(w->wd_loadav[2]) / 100.0);
7260Sstevel@tonic-gate 	cc -= WHDRSIZE;
7270Sstevel@tonic-gate 	for (we = w->wd_we, cc /= sizeof (struct whoent); cc > 0; cc--, we++) {
7280Sstevel@tonic-gate 		time_t t = ntohl(we->we_utmp.out_time);
7290Sstevel@tonic-gate 
7300Sstevel@tonic-gate 		nsz = sizeof (we->we_utmp.out_name);
7310Sstevel@tonic-gate 		(void) printf("%-*.*s %s:%s %.12s",
7320Sstevel@tonic-gate 		    nsz,
7330Sstevel@tonic-gate 		    nsz,
7340Sstevel@tonic-gate 		    we->we_utmp.out_name,
7350Sstevel@tonic-gate 		    w->wd_hostname,
7360Sstevel@tonic-gate 		    we->we_utmp.out_line,
7370Sstevel@tonic-gate 		    ctime(&t)+4);
7380Sstevel@tonic-gate 		we->we_idle = ntohl(we->we_idle) / 60;
7390Sstevel@tonic-gate 		if (we->we_idle) {
7400Sstevel@tonic-gate 			if (we->we_idle >= 100*60)
7410Sstevel@tonic-gate 				we->we_idle = 100*60 - 1;
7420Sstevel@tonic-gate 			if (we->we_idle >= 60)
7430Sstevel@tonic-gate 				(void) printf(" %2d", we->we_idle / 60);
7440Sstevel@tonic-gate 			else
7450Sstevel@tonic-gate 				(void) printf("   ");
7460Sstevel@tonic-gate 			(void) printf(":%02d", we->we_idle % 60);
7470Sstevel@tonic-gate 		}
7480Sstevel@tonic-gate 		(void) printf("\n");
7490Sstevel@tonic-gate 	}
7500Sstevel@tonic-gate 	return (0);
7510Sstevel@tonic-gate }
7520Sstevel@tonic-gate 
7530Sstevel@tonic-gate static char *
interval(uint_t time,char * updown)7540Sstevel@tonic-gate interval(uint_t time, char *updown)
7550Sstevel@tonic-gate {
7560Sstevel@tonic-gate 	static char resbuf[32];
7570Sstevel@tonic-gate 	int days, hours, minutes;
7580Sstevel@tonic-gate 
7590Sstevel@tonic-gate 	if (time > 3*30*24*60*60) {
7600Sstevel@tonic-gate 		(void) sprintf(resbuf, "   %s ??:??", updown);
7610Sstevel@tonic-gate 		return (resbuf);
7620Sstevel@tonic-gate 	}
7630Sstevel@tonic-gate 	minutes = (time + 59) / 60;		/* round to minutes */
7640Sstevel@tonic-gate 	hours = minutes / 60;
7650Sstevel@tonic-gate 	minutes %= 60;
7660Sstevel@tonic-gate 	days = hours / 24;
7670Sstevel@tonic-gate 	hours %= 24;
7680Sstevel@tonic-gate 	if (days > 0) {
7690Sstevel@tonic-gate 		(void) sprintf(resbuf, "%s %2d+%02d:%02d",
7700Sstevel@tonic-gate 		    updown, days, hours, minutes);
7710Sstevel@tonic-gate 	} else {
7720Sstevel@tonic-gate 		(void) sprintf(resbuf, "%s    %2d:%02d",
7730Sstevel@tonic-gate 		    updown, hours, minutes);
7740Sstevel@tonic-gate 	}
7750Sstevel@tonic-gate 	return (resbuf);
7760Sstevel@tonic-gate }
7770Sstevel@tonic-gate #endif
778