xref: /csrg-svn/usr.sbin/rwhod/rwhod.c (revision 18118)
16460Swnj #ifndef lint
2*18118Skarels static char sccsid[] = "@(#)rwhod.c	4.28 (Berkeley) 85/02/25";
36460Swnj #endif
46460Swnj 
59215Ssam #include <sys/types.h>
69215Ssam #include <sys/socket.h>
79215Ssam #include <sys/stat.h>
89215Ssam #include <sys/ioctl.h>
913187Ssam #include <sys/file.h>
109215Ssam 
1112236Ssam #include <net/if.h>
129215Ssam #include <netinet/in.h>
139215Ssam 
149215Ssam #include <nlist.h>
156460Swnj #include <stdio.h>
166460Swnj #include <signal.h>
176460Swnj #include <errno.h>
186460Swnj #include <utmp.h>
198486Ssam #include <ctype.h>
208486Ssam #include <netdb.h>
2116673Sralph #include <syslog.h>
2213567Ssam #include "rwhod.h"
236460Swnj 
2415541Sralph /*
2515541Sralph  * Alarm interval. Don't forget to change the down time check in ruptime
2615541Sralph  * if this is changed.
2715541Sralph  */
2816673Sralph #define AL_INTERVAL (3 * 60)
29*18118Skarels #define	MAXTTYS	256			/* Max # of utmp entries examined */
3015541Sralph 
318486Ssam struct	sockaddr_in sin = { AF_INET };
326460Swnj 
336460Swnj extern	errno;
346460Swnj 
3512236Ssam char	myname[32];
366460Swnj 
376460Swnj struct	nlist nl[] = {
386460Swnj #define	NL_AVENRUN	0
396460Swnj 	{ "_avenrun" },
409215Ssam #define	NL_BOOTTIME	1
419215Ssam 	{ "_boottime" },
426460Swnj 	0
436460Swnj };
446460Swnj 
4512236Ssam /*
4612236Ssam  * We communicate with each neighbor in
4712236Ssam  * a list constructed at the time we're
4812236Ssam  * started up.  Neighbors are currently
4912236Ssam  * directly connected via a hardware interface.
5012236Ssam  */
5112236Ssam struct	neighbor {
5212236Ssam 	struct	neighbor *n_next;
5312236Ssam 	char	*n_name;		/* interface name */
5412236Ssam 	char	*n_addr;		/* who to send to */
5512236Ssam 	int	n_addrlen;		/* size of address */
5612236Ssam 	int	n_flags;		/* should forward?, interface flags */
5712236Ssam };
5812236Ssam 
5912236Ssam struct	neighbor *neighbors;
606460Swnj struct	whod mywd;
6112236Ssam struct	servent *sp;
626460Swnj int	s, utmpf, kmemf = -1;
636460Swnj 
6412236Ssam #define	WHDRSIZE	(sizeof (mywd) - sizeof (mywd.wd_we))
6512236Ssam #define	RWHODIR		"/usr/spool/rwho"
6611834Ssam 
676460Swnj int	onalrm();
6812236Ssam char	*strcpy(), *sprintf(), *malloc();
696460Swnj long	lseek();
706460Swnj int	getkmem();
7112236Ssam struct	in_addr inet_makeaddr();
726460Swnj 
736460Swnj main()
746460Swnj {
756460Swnj 	struct sockaddr_in from;
766460Swnj 	char path[64];
7717155Ssam 	int addr, on = 1;
7812236Ssam 	struct hostent *hp;
796460Swnj 
8016673Sralph 	if (getuid()) {
8116673Sralph 		fprintf(stderr, "rwhod: not super user\n");
8216673Sralph 		exit(1);
8316673Sralph 	}
848486Ssam 	sp = getservbyname("who", "udp");
858486Ssam 	if (sp == 0) {
868486Ssam 		fprintf(stderr, "rwhod: udp/who: unknown service\n");
878486Ssam 		exit(1);
888486Ssam 	}
896460Swnj #ifndef DEBUG
906460Swnj 	if (fork())
916460Swnj 		exit(0);
926460Swnj 	{ int s;
936460Swnj 	  for (s = 0; s < 10; s++)
946460Swnj 		(void) close(s);
956460Swnj 	  (void) open("/", 0);
966460Swnj 	  (void) dup2(0, 1);
976460Swnj 	  (void) dup2(0, 2);
986460Swnj 	  s = open("/dev/tty", 2);
996460Swnj 	  if (s >= 0) {
1006460Swnj 		ioctl(s, TIOCNOTTY, 0);
1016460Swnj 		(void) close(s);
1026460Swnj 	  }
1036460Swnj 	}
1046460Swnj #endif
1056460Swnj 	(void) chdir("/dev");
1066460Swnj 	(void) signal(SIGHUP, getkmem);
10716673Sralph 	openlog("rwhod", LOG_PID, 0);
10812236Ssam 	/*
10912236Ssam 	 * Establish host name as returned by system.
11012236Ssam 	 */
11112236Ssam 	if (gethostname(myname, sizeof (myname) - 1) < 0) {
11216673Sralph 		syslog(LOG_ERR, "gethostname: %m");
1136460Swnj 		exit(1);
1146460Swnj 	}
11512236Ssam 	strncpy(mywd.wd_hostname, myname, sizeof (myname) - 1);
11613187Ssam 	utmpf = open("/etc/utmp", O_RDONLY);
1176460Swnj 	if (utmpf < 0) {
1186460Swnj 		(void) close(creat("/etc/utmp", 0644));
11913187Ssam 		utmpf = open("/etc/utmp", O_RDONLY);
1206460Swnj 	}
1216460Swnj 	if (utmpf < 0) {
12216673Sralph 		syslog(LOG_ERR, "/etc/utmp: %m");
1236460Swnj 		exit(1);
1246460Swnj 	}
1256460Swnj 	getkmem();
12613187Ssam 	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
12716673Sralph 		syslog(LOG_ERR, "socket: %m");
1289215Ssam 		exit(1);
1296460Swnj 	}
13017155Ssam 	if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0) {
13117055Skarels 		syslog(LOG_ERR, "setsockopt SO_BROADCAST: %m");
13217055Skarels 		exit(1);
13317055Skarels 	}
13412236Ssam 	hp = gethostbyname(myname);
13512236Ssam 	if (hp == NULL) {
13616673Sralph 		syslog(LOG_ERR, "%s: don't know my own name", myname);
13712236Ssam 		exit(1);
13812236Ssam 	}
13912236Ssam 	sin.sin_family = hp->h_addrtype;
14012236Ssam 	sin.sin_port = sp->s_port;
14113187Ssam 	if (bind(s, &sin, sizeof (sin)) < 0) {
14216673Sralph 		syslog(LOG_ERR, "bind: %m");
1439215Ssam 		exit(1);
1449215Ssam 	}
14512236Ssam 	if (!configure(s))
14612236Ssam 		exit(1);
14713027Ssam 	signal(SIGALRM, onalrm);
1486460Swnj 	onalrm();
1496460Swnj 	for (;;) {
1506460Swnj 		struct whod wd;
15112236Ssam 		int cc, whod, len = sizeof (from);
1526460Swnj 
15312236Ssam 		cc = recvfrom(s, (char *)&wd, sizeof (struct whod), 0,
15412236Ssam 			&from, &len);
1556460Swnj 		if (cc <= 0) {
1566460Swnj 			if (cc < 0 && errno != EINTR)
15716673Sralph 				syslog(LOG_WARNING, "recv: %m");
1586460Swnj 			continue;
1596460Swnj 		}
1608486Ssam 		if (from.sin_port != sp->s_port) {
16116673Sralph 			syslog(LOG_WARNING, "%d: bad from port",
1628486Ssam 				ntohs(from.sin_port));
1636460Swnj 			continue;
1646460Swnj 		}
1658486Ssam #ifdef notdef
1668486Ssam 		if (gethostbyname(wd.wd_hostname) == 0) {
16716673Sralph 			syslog(LOG_WARNING, "%s: unknown host",
1688486Ssam 				wd.wd_hostname);
1696460Swnj 			continue;
1706460Swnj 		}
1718486Ssam #endif
17212347Ssam 		if (wd.wd_vers != WHODVERSION)
17312347Ssam 			continue;
17412236Ssam 		if (wd.wd_type != WHODTYPE_STATUS)
17512236Ssam 			continue;
1768486Ssam 		if (!verify(wd.wd_hostname)) {
17716673Sralph 			syslog(LOG_WARNING, "malformed host name from %x",
1788486Ssam 				from.sin_addr);
1798486Ssam 			continue;
1808486Ssam 		}
1819914Ssam 		(void) sprintf(path, "%s/whod.%s", RWHODIR, wd.wd_hostname);
1826460Swnj 		whod = creat(path, 0666);
1836460Swnj 		if (whod < 0) {
18416673Sralph 			syslog(LOG_WARNING, "%s: %m", path);
1856460Swnj 			continue;
1866460Swnj 		}
18711834Ssam #if vax || pdp11
18811834Ssam 		{
18913187Ssam 			int i, n = (cc - WHDRSIZE)/sizeof(struct whoent);
19011834Ssam 			struct whoent *we;
19111834Ssam 
19211834Ssam 			/* undo header byte swapping before writing to file */
19311834Ssam 			wd.wd_sendtime = ntohl(wd.wd_sendtime);
19411834Ssam 			for (i = 0; i < 3; i++)
19511834Ssam 				wd.wd_loadav[i] = ntohl(wd.wd_loadav[i]);
19611834Ssam 			wd.wd_boottime = ntohl(wd.wd_boottime);
19711834Ssam 			we = wd.wd_we;
19811834Ssam 			for (i = 0; i < n; i++) {
19911834Ssam 				we->we_idle = ntohl(we->we_idle);
20012872Ssam 				we->we_utmp.out_time =
20112872Ssam 				    ntohl(we->we_utmp.out_time);
20211834Ssam 				we++;
20311834Ssam 			}
20411834Ssam 		}
20511834Ssam #endif
2066460Swnj 		(void) time(&wd.wd_recvtime);
2076460Swnj 		(void) write(whod, (char *)&wd, cc);
2086460Swnj 		(void) close(whod);
2096460Swnj 	}
2106460Swnj }
2116460Swnj 
2128486Ssam /*
2138486Ssam  * Check out host name for unprintables
2148486Ssam  * and other funnies before allowing a file
2158486Ssam  * to be created.  Sorry, but blanks aren't allowed.
2168486Ssam  */
2178486Ssam verify(name)
2188486Ssam 	register char *name;
2198486Ssam {
2208486Ssam 	register int size = 0;
2218486Ssam 
2228486Ssam 	while (*name) {
22315214Sleres 		if (!isascii(*name) || !(isalnum(*name) || ispunct(*name)))
2248486Ssam 			return (0);
2258486Ssam 		name++, size++;
2268486Ssam 	}
2278486Ssam 	return (size > 0);
2288486Ssam }
2298486Ssam 
2306460Swnj int	utmptime;
2316460Swnj int	utmpent;
232*18118Skarels struct	utmp utmp[MAXTTYS];
2336460Swnj int	alarmcount;
2346460Swnj 
2356460Swnj onalrm()
2366460Swnj {
2376460Swnj 	register int i;
2386460Swnj 	struct stat stb;
2396577Ssam 	register struct whoent *we = mywd.wd_we, *wlast;
2406460Swnj 	int cc;
2416460Swnj 	double avenrun[3];
2426460Swnj 	time_t now = time(0);
24312236Ssam 	register struct neighbor *np;
2446460Swnj 
2456460Swnj 	if (alarmcount % 10 == 0)
2466460Swnj 		getkmem();
2476460Swnj 	alarmcount++;
2486460Swnj 	(void) fstat(utmpf, &stb);
2496460Swnj 	if (stb.st_mtime != utmptime) {
25017303Stef 		utmptime = stb.st_mtime;
25113187Ssam 		(void) lseek(utmpf, (long)0, L_SET);
2526460Swnj 		cc = read(utmpf, (char *)utmp, sizeof (utmp));
2536460Swnj 		if (cc < 0) {
2546460Swnj 			perror("/etc/utmp");
25513469Ssam 			goto done;
2566460Swnj 		}
25712236Ssam 		wlast = &mywd.wd_we[1024 / sizeof (struct whoent) - 1];
2586460Swnj 		utmpent = cc / sizeof (struct utmp);
2596460Swnj 		for (i = 0; i < utmpent; i++)
2606460Swnj 			if (utmp[i].ut_name[0]) {
26112807Ssam 				bcopy(utmp[i].ut_line, we->we_utmp.out_line,
26212807Ssam 				   sizeof (utmp[i].ut_line));
26312807Ssam 				bcopy(utmp[i].ut_name, we->we_utmp.out_name,
26412807Ssam 				   sizeof (utmp[i].ut_name));
26512807Ssam 				we->we_utmp.out_time = htonl(utmp[i].ut_time);
2666577Ssam 				if (we >= wlast)
2676577Ssam 					break;
2686460Swnj 				we++;
2696460Swnj 			}
2706460Swnj 		utmpent = we - mywd.wd_we;
2716460Swnj 	}
2726460Swnj 	we = mywd.wd_we;
2736460Swnj 	for (i = 0; i < utmpent; i++) {
27412807Ssam 		if (stat(we->we_utmp.out_line, &stb) >= 0)
27512807Ssam 			we->we_idle = htonl(now - stb.st_atime);
2766460Swnj 		we++;
2776460Swnj 	}
27813187Ssam 	(void) lseek(kmemf, (long)nl[NL_AVENRUN].n_value, L_SET);
2796460Swnj 	(void) read(kmemf, (char *)avenrun, sizeof (avenrun));
2806460Swnj 	for (i = 0; i < 3; i++)
28112873Ssam 		mywd.wd_loadav[i] = htonl((u_long)(avenrun[i] * 100));
2826460Swnj 	cc = (char *)we - (char *)&mywd;
28312807Ssam 	mywd.wd_sendtime = htonl(time(0));
28412236Ssam 	mywd.wd_vers = WHODVERSION;
28512236Ssam 	mywd.wd_type = WHODTYPE_STATUS;
28612236Ssam 	for (np = neighbors; np != NULL; np = np->n_next)
28712236Ssam 		(void) sendto(s, (char *)&mywd, cc, 0,
28812236Ssam 			np->n_addr, np->n_addrlen);
28913469Ssam done:
29015541Sralph 	(void) alarm(AL_INTERVAL);
2916460Swnj }
2926460Swnj 
2936460Swnj getkmem()
2946460Swnj {
2956460Swnj 	struct nlist *nlp;
29616663Ssam 	static ino_t vmunixino;
29716663Ssam 	static time_t vmunixctime;
29816663Ssam 	struct stat sb;
2996460Swnj 
30016663Ssam 	if (stat("/vmunix", &sb) < 0) {
30116663Ssam 		if (vmunixctime)
30216663Ssam 			return;
30316663Ssam 	} else {
30416663Ssam 		if (sb.st_ctime == vmunixctime && sb.st_ino == vmunixino)
30516663Ssam 			return;
30616663Ssam 		vmunixctime = sb.st_ctime;
30716663Ssam 		vmunixino= sb.st_ino;
30816663Ssam 	}
3096460Swnj 	if (kmemf >= 0)
3106460Swnj 		(void) close(kmemf);
3116460Swnj loop:
31216673Sralph 	if (nlist("/vmunix", nl)) {
31316673Sralph 		syslog(LOG_WARNING, "/vmunix namelist botch");
3146460Swnj 		sleep(300);
3156460Swnj 		goto loop;
3166460Swnj 	}
31713187Ssam 	kmemf = open("/dev/kmem", O_RDONLY);
3186460Swnj 	if (kmemf < 0) {
31916673Sralph 		syslog(LOG_ERR, "/dev/kmem: %m");
32016673Sralph 		exit(1);
3216460Swnj 	}
32213187Ssam 	(void) lseek(kmemf, (long)nl[NL_BOOTTIME].n_value, L_SET);
32313187Ssam 	(void) read(kmemf, (char *)&mywd.wd_boottime,
32413187Ssam 	    sizeof (mywd.wd_boottime));
32512807Ssam 	mywd.wd_boottime = htonl(mywd.wd_boottime);
3266460Swnj }
32712236Ssam 
32812236Ssam /*
32912236Ssam  * Figure out device configuration and select
33012236Ssam  * networks which deserve status information.
33112236Ssam  */
33212236Ssam configure(s)
33312236Ssam 	int s;
33412236Ssam {
33512236Ssam 	char buf[BUFSIZ];
33612236Ssam 	struct ifconf ifc;
33712236Ssam 	struct ifreq ifreq, *ifr;
33812236Ssam 	struct sockaddr_in *sin;
33912236Ssam 	register struct neighbor *np;
34013187Ssam 	int n;
34112236Ssam 
34212236Ssam 	ifc.ifc_len = sizeof (buf);
34312236Ssam 	ifc.ifc_buf = buf;
34412236Ssam 	if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0) {
34516673Sralph 		syslog(LOG_ERR, "ioctl (get interface configuration)");
34612236Ssam 		return (0);
34712236Ssam 	}
34812236Ssam 	ifr = ifc.ifc_req;
34912236Ssam 	for (n = ifc.ifc_len / sizeof (struct ifreq); n > 0; n--, ifr++) {
35012236Ssam 		for (np = neighbors; np != NULL; np = np->n_next)
35112236Ssam 			if (np->n_name &&
35212236Ssam 			    strcmp(ifr->ifr_name, np->n_name) == 0)
35312236Ssam 				break;
35412236Ssam 		if (np != NULL)
35512236Ssam 			continue;
35612236Ssam 		ifreq = *ifr;
35712236Ssam 		np = (struct neighbor *)malloc(sizeof (*np));
35812236Ssam 		if (np == NULL)
35912236Ssam 			continue;
36012236Ssam 		np->n_name = malloc(strlen(ifr->ifr_name) + 1);
36112236Ssam 		if (np->n_name == NULL) {
36212236Ssam 			free((char *)np);
36312236Ssam 			continue;
36412236Ssam 		}
36512236Ssam 		strcpy(np->n_name, ifr->ifr_name);
36612236Ssam 		np->n_addrlen = sizeof (ifr->ifr_addr);
36712236Ssam 		np->n_addr = malloc(np->n_addrlen);
36812236Ssam 		if (np->n_addr == NULL) {
36912236Ssam 			free(np->n_name);
37012236Ssam 			free((char *)np);
37112236Ssam 			continue;
37212236Ssam 		}
37312236Ssam 		bcopy((char *)&ifr->ifr_addr, np->n_addr, np->n_addrlen);
37412236Ssam 		if (ioctl(s, SIOCGIFFLAGS, (char *)&ifreq) < 0) {
37516673Sralph 			syslog(LOG_ERR, "ioctl (get interface flags)");
37612236Ssam 			free((char *)np);
37712236Ssam 			continue;
37812236Ssam 		}
37912236Ssam 		if ((ifreq.ifr_flags & (IFF_BROADCAST|IFF_POINTOPOINT)) == 0) {
38012236Ssam 			free((char *)np);
38112236Ssam 			continue;
38212236Ssam 		}
38312236Ssam 		np->n_flags = ifreq.ifr_flags;
38412236Ssam 		if (np->n_flags & IFF_POINTOPOINT) {
38512236Ssam 			if (ioctl(s, SIOCGIFDSTADDR, (char *)&ifreq) < 0) {
38616673Sralph 				syslog(LOG_ERR, "ioctl (get dstaddr)");
38712236Ssam 				free((char *)np);
38812236Ssam 				continue;
38912236Ssam 			}
39012236Ssam 			/* we assume addresses are all the same size */
39112236Ssam 			bcopy((char *)&ifreq.ifr_dstaddr,
39212236Ssam 			  np->n_addr, np->n_addrlen);
39312236Ssam 		}
39412236Ssam 		if (np->n_flags & IFF_BROADCAST) {
39512236Ssam 			/* we assume addresses are all the same size */
39612236Ssam 			sin = (struct sockaddr_in *)np->n_addr;
39716468Skarels 			sin->sin_addr = inet_makeaddr((np->n_flags & IFF_LOCAL)?
39816468Skarels 				inet_subnetof(sin->sin_addr) :
39916468Skarels 				inet_netof(sin->sin_addr),
40016468Skarels 				INADDR_ANY);
40112236Ssam 		}
40212236Ssam 		/* gag, wish we could get rid of Internet dependencies */
40312236Ssam 		sin = (struct sockaddr_in *)np->n_addr;
40412236Ssam 		sin->sin_port = sp->s_port;
40512236Ssam 		np->n_next = neighbors;
40612236Ssam 		neighbors = np;
40712236Ssam 	}
40812236Ssam 	return (1);
40912236Ssam }
41012807Ssam 
41116468Skarels /*
41216468Skarels  * Return the possible subnetwork number from an internet address.
41316468Skarels  * If the address is of the form of a subnet address (most significant
41416468Skarels  * bit of the host part is set), believe the subnet exists.
41516468Skarels  * Otherwise, return the network number.  Any subnet number is only valid
41616468Skarels  * if this is a ``local'' net.
41716468Skarels  */
41816468Skarels inet_subnetof(in)
41916468Skarels 	struct in_addr in;
42016468Skarels {
42116468Skarels 	register u_long i = ntohl(in.s_addr);
42216468Skarels 
42316468Skarels 	if (IN_CLASSA(i)) {
42416468Skarels 		if (IN_SUBNETA(i))
42516468Skarels 			return ((i & IN_CLASSA_SUBNET) >> IN_CLASSA_SUBNSHIFT);
42616468Skarels 		else
42716468Skarels 			return ((i & IN_CLASSA_NET) >> IN_CLASSA_NSHIFT);
42816468Skarels 	} else if (IN_CLASSB(i)) {
42916468Skarels 		if (IN_SUBNETB(i))
43016468Skarels 			return ((i & IN_CLASSB_SUBNET) >> IN_CLASSB_SUBNSHIFT);
43116468Skarels 		else
43216468Skarels 			return ((i & IN_CLASSB_NET) >> IN_CLASSB_NSHIFT);
43316468Skarels 	} else
43416468Skarels 		return ((i & IN_CLASSC_NET) >> IN_CLASSC_NSHIFT);
43516468Skarels }
43616468Skarels 
43712807Ssam #ifdef DEBUG
43812807Ssam sendto(s, buf, cc, flags, to, tolen)
43912807Ssam 	int s;
44012807Ssam 	char *buf;
44112807Ssam 	int cc, flags;
44212807Ssam 	char *to;
44312807Ssam 	int tolen;
44412807Ssam {
44512807Ssam 	register struct whod *w = (struct whod *)buf;
44612807Ssam 	register struct whoent *we;
44712807Ssam 	struct sockaddr_in *sin = (struct sockaddr_in *)to;
44812807Ssam 	char *interval();
44912807Ssam 
45012807Ssam 	printf("sendto %x.%d\n", ntohl(sin->sin_addr), ntohs(sin->sin_port));
45112807Ssam 	printf("hostname %s %s\n", w->wd_hostname,
45213187Ssam 	   interval(ntohl(w->wd_sendtime) - ntohl(w->wd_boottime), "  up"));
45312807Ssam 	printf("load %4.2f, %4.2f, %4.2f\n",
45413187Ssam 	    ntohl(w->wd_loadav[0]) / 100.0, ntohl(w->wd_loadav[1]) / 100.0,
45513187Ssam 	    ntohl(w->wd_loadav[2]) / 100.0);
45612807Ssam 	cc -= WHDRSIZE;
45712807Ssam 	for (we = w->wd_we, cc /= sizeof (struct whoent); cc > 0; cc--, we++) {
45813187Ssam 		time_t t = ntohl(we->we_utmp.out_time);
45912807Ssam 		printf("%-8.8s %s:%s %.12s",
46013187Ssam 			we->we_utmp.out_name,
46113187Ssam 			w->wd_hostname, we->we_utmp.out_line,
46213187Ssam 			ctime(&t)+4);
46313187Ssam 		we->we_idle = ntohl(we->we_idle) / 60;
46412807Ssam 		if (we->we_idle) {
46512807Ssam 			if (we->we_idle >= 100*60)
46612807Ssam 				we->we_idle = 100*60 - 1;
46712807Ssam 			if (we->we_idle >= 60)
46812807Ssam 				printf(" %2d", we->we_idle / 60);
46912807Ssam 			else
47012807Ssam 				printf("   ");
47112807Ssam 			printf(":%02d", we->we_idle % 60);
47212807Ssam 		}
47312807Ssam 		printf("\n");
47412807Ssam 	}
47512807Ssam }
47612807Ssam 
47712807Ssam char *
47812807Ssam interval(time, updown)
47912807Ssam 	int time;
48012807Ssam 	char *updown;
48112807Ssam {
48212807Ssam 	static char resbuf[32];
48312807Ssam 	int days, hours, minutes;
48412807Ssam 
48512807Ssam 	if (time < 0 || time > 3*30*24*60*60) {
48612807Ssam 		(void) sprintf(resbuf, "   %s ??:??", updown);
48712807Ssam 		return (resbuf);
48812807Ssam 	}
48912807Ssam 	minutes = (time + 59) / 60;		/* round to minutes */
49012807Ssam 	hours = minutes / 60; minutes %= 60;
49112807Ssam 	days = hours / 24; hours %= 24;
49212807Ssam 	if (days)
49312807Ssam 		(void) sprintf(resbuf, "%s %2d+%02d:%02d",
49412807Ssam 		    updown, days, hours, minutes);
49512807Ssam 	else
49612807Ssam 		(void) sprintf(resbuf, "%s    %2d:%02d",
49712807Ssam 		    updown, hours, minutes);
49812807Ssam 	return (resbuf);
49912807Ssam }
50012807Ssam #endif
501