xref: /freebsd/usr.bin/w/w.c (revision d11cba9c89ab1edb3211f88914527bfe2924aad2)
19b50d902SRodney W. Grimes /*-
29b50d902SRodney W. Grimes  * Copyright (c) 1980, 1991, 1993, 1994
39b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
49b50d902SRodney W. Grimes  *
59b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
69b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
79b50d902SRodney W. Grimes  * are met:
89b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
99b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
109b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
119b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
129b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
139b50d902SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
149b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
159b50d902SRodney W. Grimes  *    without specific prior written permission.
169b50d902SRodney W. Grimes  *
179b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
189b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
199b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
209b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
219b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
229b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
239b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
249b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
259b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
269b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
279b50d902SRodney W. Grimes  * SUCH DAMAGE.
289b50d902SRodney W. Grimes  */
299b50d902SRodney W. Grimes 
308b56c58bSMark Murray #include <sys/cdefs.h>
318b56c58bSMark Murray 
328b56c58bSMark Murray __FBSDID("$FreeBSD$");
338b56c58bSMark Murray 
349b50d902SRodney W. Grimes #ifndef lint
3590389da9SPhilippe Charnier static const char copyright[] =
369b50d902SRodney W. Grimes "@(#) Copyright (c) 1980, 1991, 1993, 1994\n\
379b50d902SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
388b56c58bSMark Murray #endif
399b50d902SRodney W. Grimes 
409b50d902SRodney W. Grimes #ifndef lint
418b56c58bSMark Murray static const char sccsid[] = "@(#)w.c	8.4 (Berkeley) 4/16/94";
4290389da9SPhilippe Charnier #endif
439b50d902SRodney W. Grimes 
449b50d902SRodney W. Grimes /*
459b50d902SRodney W. Grimes  * w - print system status (who and what)
469b50d902SRodney W. Grimes  *
479b50d902SRodney W. Grimes  * This program is similar to the systat command on Tenex/Tops 10/20
489b50d902SRodney W. Grimes  *
499b50d902SRodney W. Grimes  */
509b50d902SRodney W. Grimes #include <sys/param.h>
519b50d902SRodney W. Grimes #include <sys/time.h>
529b50d902SRodney W. Grimes #include <sys/stat.h>
539b50d902SRodney W. Grimes #include <sys/sysctl.h>
549b50d902SRodney W. Grimes #include <sys/proc.h>
559b50d902SRodney W. Grimes #include <sys/user.h>
569b50d902SRodney W. Grimes #include <sys/ioctl.h>
579b50d902SRodney W. Grimes #include <sys/socket.h>
589b50d902SRodney W. Grimes #include <sys/tty.h>
599b50d902SRodney W. Grimes 
609b50d902SRodney W. Grimes #include <machine/cpu.h>
619b50d902SRodney W. Grimes #include <netinet/in.h>
629b50d902SRodney W. Grimes #include <arpa/inet.h>
638b56c58bSMark Murray #include <arpa/nameser.h>
649b50d902SRodney W. Grimes 
65821df508SXin LI #include <ctype.h>
669b50d902SRodney W. Grimes #include <err.h>
679b50d902SRodney W. Grimes #include <errno.h>
689b50d902SRodney W. Grimes #include <fcntl.h>
699b50d902SRodney W. Grimes #include <kvm.h>
7000034645SAndrey A. Chernov #include <langinfo.h>
715b718312SBrian Somers #include <libutil.h>
72af943f55SThomas Moestl #include <limits.h>
7300034645SAndrey A. Chernov #include <locale.h>
749b50d902SRodney W. Grimes #include <netdb.h>
759b50d902SRodney W. Grimes #include <nlist.h>
769b50d902SRodney W. Grimes #include <paths.h>
778b56c58bSMark Murray #include <resolv.h>
789b50d902SRodney W. Grimes #include <stdio.h>
799b50d902SRodney W. Grimes #include <stdlib.h>
809b50d902SRodney W. Grimes #include <string.h>
81821df508SXin LI #include <timeconv.h>
829b50d902SRodney W. Grimes #include <unistd.h>
831131779fSEd Schouten #include <utmpx.h>
84821df508SXin LI #include <vis.h>
859b50d902SRodney W. Grimes 
869b50d902SRodney W. Grimes #include "extern.h"
879b50d902SRodney W. Grimes 
88*d11cba9cSEd Schouten static struct utmpx *utmp;
89*d11cba9cSEd Schouten static struct winsize ws;
90*d11cba9cSEd Schouten static kvm_t   *kd;
91*d11cba9cSEd Schouten static time_t	now;		/* the current time of day */
92*d11cba9cSEd Schouten static int	ttywidth;	/* width of tty */
93*d11cba9cSEd Schouten static int	argwidth;	/* width of tty */
94*d11cba9cSEd Schouten static int	header = 1;	/* true if -h flag: don't print heading */
95*d11cba9cSEd Schouten static int	nflag;		/* true if -n flag: don't convert addrs */
96*d11cba9cSEd Schouten static int	dflag;		/* true if -d flag: output debug info */
97*d11cba9cSEd Schouten static int	sortidle;	/* sort by idle time */
9800034645SAndrey A. Chernov int		use_ampm;	/* use AM/PM time */
99*d11cba9cSEd Schouten static int	use_comma;      /* use comma as floats separator */
100*d11cba9cSEd Schouten static char   **sel_users;	/* login array of particular users selected */
1019b50d902SRodney W. Grimes 
1029b50d902SRodney W. Grimes /*
1039b50d902SRodney W. Grimes  * One of these per active utmp entry.
1049b50d902SRodney W. Grimes  */
105*d11cba9cSEd Schouten static struct entry {
1069b50d902SRodney W. Grimes 	struct	entry *next;
107dc65bcacSEd Schouten 	struct	utmpx utmp;
1089b50d902SRodney W. Grimes 	dev_t	tdev;			/* dev_t of terminal */
1099b50d902SRodney W. Grimes 	time_t	idle;			/* idle time of terminal in seconds */
1109b50d902SRodney W. Grimes 	struct	kinfo_proc *kp;		/* `most interesting' proc */
1119b50d902SRodney W. Grimes 	char	*args;			/* arg list of interesting process */
1121bb32253SAndrey A. Chernov 	struct	kinfo_proc *dkp;	/* debug option proc list */
1139b50d902SRodney W. Grimes } *ep, *ehead = NULL, **nextp = &ehead;
1149b50d902SRodney W. Grimes 
11502f56376SDavid Malone #define	debugproc(p) *(&((struct kinfo_proc *)p)->ki_udata)
116ac3cd520SBrian Feldman 
117dc65bcacSEd Schouten #define	W_DISPUSERSIZE	10
118dc65bcacSEd Schouten #define	W_DISPLINESIZE	8
119dc65bcacSEd Schouten #define	W_DISPHOSTSIZE	24
1205b718312SBrian Somers 
1213f330d7dSWarner Losh static void		 pr_header(time_t *, int);
122dc65bcacSEd Schouten static struct stat	*ttystat(char *);
1233f330d7dSWarner Losh static void		 usage(int);
1243f330d7dSWarner Losh static int		 this_is_uptime(const char *s);
1259b50d902SRodney W. Grimes 
1263f330d7dSWarner Losh char *fmt_argv(char **, char *, int);	/* ../../bin/ps/fmt.c */
1279b50d902SRodney W. Grimes 
1289b50d902SRodney W. Grimes int
129e8e649ccSJuli Mallett main(int argc, char *argv[])
1309b50d902SRodney W. Grimes {
1319b50d902SRodney W. Grimes 	struct kinfo_proc *kp;
1321bb32253SAndrey A. Chernov 	struct kinfo_proc *dkp;
1339b50d902SRodney W. Grimes 	struct stat *stp;
1343bebe991SBrian Somers 	time_t touched;
13534903a55SHajimu UMEMOTO 	int ch, i, nentries, nusers, wcmd, longidle, longattime, dropgid;
1368b56c58bSMark Murray 	const char *memf, *nlistf, *p;
1378b56c58bSMark Murray 	char *x_suffix;
138af943f55SThomas Moestl 	char buf[MAXHOSTNAMELEN], errbuf[_POSIX2_LINE_MAX];
1395b718312SBrian Somers 	char fn[MAXHOSTNAMELEN];
1405b718312SBrian Somers 	char *dot;
1419b50d902SRodney W. Grimes 
142d1b2ad1aSAndrey A. Chernov 	(void)setlocale(LC_ALL, "");
14300034645SAndrey A. Chernov 	use_ampm = (*nl_langinfo(T_FMT_AMPM) != '\0');
1442742fc8eSAndrey A. Chernov 	use_comma = (*nl_langinfo(RADIXCHAR) != ',');
145baf72ec1SAndrey A. Chernov 
1469b50d902SRodney W. Grimes 	/* Are we w(1) or uptime(1)? */
147a4f33f43SMatthew Dillon 	if (this_is_uptime(argv[0]) == 0) {
1489b50d902SRodney W. Grimes 		wcmd = 0;
1499b50d902SRodney W. Grimes 		p = "";
1509b50d902SRodney W. Grimes 	} else {
1519b50d902SRodney W. Grimes 		wcmd = 1;
1521bb32253SAndrey A. Chernov 		p = "dhiflM:N:nsuw";
1539b50d902SRodney W. Grimes 	}
1549b50d902SRodney W. Grimes 
1556ff4ec18SPeter Wemm 	dropgid = 0;
156c08dcaf1SRebecca Cran 	memf = _PATH_DEVNULL;
157c08dcaf1SRebecca Cran 	nlistf = NULL;
1581c8af878SWarner Losh 	while ((ch = getopt(argc, argv, p)) != -1)
1599b50d902SRodney W. Grimes 		switch (ch) {
1601bb32253SAndrey A. Chernov 		case 'd':
1611bb32253SAndrey A. Chernov 			dflag = 1;
1621bb32253SAndrey A. Chernov 			break;
1639b50d902SRodney W. Grimes 		case 'h':
1649b50d902SRodney W. Grimes 			header = 0;
1659b50d902SRodney W. Grimes 			break;
1669b50d902SRodney W. Grimes 		case 'i':
1679b50d902SRodney W. Grimes 			sortidle = 1;
1689b50d902SRodney W. Grimes 			break;
1699b50d902SRodney W. Grimes 		case 'M':
1709b50d902SRodney W. Grimes 			header = 0;
1719b50d902SRodney W. Grimes 			memf = optarg;
1726ff4ec18SPeter Wemm 			dropgid = 1;
1739b50d902SRodney W. Grimes 			break;
1749b50d902SRodney W. Grimes 		case 'N':
1759b50d902SRodney W. Grimes 			nlistf = optarg;
1766ff4ec18SPeter Wemm 			dropgid = 1;
1779b50d902SRodney W. Grimes 			break;
1789b50d902SRodney W. Grimes 		case 'n':
1799b50d902SRodney W. Grimes 			nflag = 1;
1809b50d902SRodney W. Grimes 			break;
1819b50d902SRodney W. Grimes 		case 'f': case 'l': case 's': case 'u': case 'w':
1829b50d902SRodney W. Grimes 			warnx("[-flsuw] no longer supported");
1839b50d902SRodney W. Grimes 			/* FALLTHROUGH */
1849b50d902SRodney W. Grimes 		case '?':
1859b50d902SRodney W. Grimes 		default:
1869b50d902SRodney W. Grimes 			usage(wcmd);
1879b50d902SRodney W. Grimes 		}
1889b50d902SRodney W. Grimes 	argc -= optind;
1899b50d902SRodney W. Grimes 	argv += optind;
1909b50d902SRodney W. Grimes 
1916367cd09SPeter Wemm 	if (!(_res.options & RES_INIT))
1926367cd09SPeter Wemm 		res_init();
1936367cd09SPeter Wemm 	_res.retrans = 2;	/* resolver timeout to 2 seconds per try */
1946367cd09SPeter Wemm 	_res.retry = 1;		/* only try once.. */
1956367cd09SPeter Wemm 
19666e5b18fSPaul Traina 	/*
19766e5b18fSPaul Traina 	 * Discard setgid privileges if not the running kernel so that bad
19866e5b18fSPaul Traina 	 * guys can't print interesting stuff from kernel memory.
19966e5b18fSPaul Traina 	 */
2006ff4ec18SPeter Wemm 	if (dropgid)
20166e5b18fSPaul Traina 		setgid(getgid());
20266e5b18fSPaul Traina 
2039b50d902SRodney W. Grimes 	if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf)) == NULL)
2049b50d902SRodney W. Grimes 		errx(1, "%s", errbuf);
2059b50d902SRodney W. Grimes 
2069b50d902SRodney W. Grimes 	(void)time(&now);
2079b50d902SRodney W. Grimes 
2089b50d902SRodney W. Grimes 	if (*argv)
209ac3cd520SBrian Feldman 		sel_users = argv;
2109b50d902SRodney W. Grimes 
211dc65bcacSEd Schouten 	setutxent();
212dc65bcacSEd Schouten 	for (nusers = 0; (utmp = getutxent()) != NULL;) {
213dc65bcacSEd Schouten 		if (utmp->ut_type != USER_PROCESS)
2149b50d902SRodney W. Grimes 			continue;
215dc65bcacSEd Schouten 		if (!(stp = ttystat(utmp->ut_line)))
2160cadb9caSBrian Somers 			continue;	/* corrupted record */
2179b50d902SRodney W. Grimes 		++nusers;
218ac3cd520SBrian Feldman 		if (wcmd == 0)
2199b50d902SRodney W. Grimes 			continue;
220ac3cd520SBrian Feldman 		if (sel_users) {
221ac3cd520SBrian Feldman 			int usermatch;
222ac3cd520SBrian Feldman 			char **user;
223ac3cd520SBrian Feldman 
224ac3cd520SBrian Feldman 			usermatch = 0;
225ac3cd520SBrian Feldman 			for (user = sel_users; !usermatch && *user; user++)
226dc65bcacSEd Schouten 				if (!strcmp(utmp->ut_user, *user))
227ac3cd520SBrian Feldman 					usermatch = 1;
228ac3cd520SBrian Feldman 			if (!usermatch)
229ac3cd520SBrian Feldman 				continue;
230ac3cd520SBrian Feldman 		}
2319b50d902SRodney W. Grimes 		if ((ep = calloc(1, sizeof(struct entry))) == NULL)
23290389da9SPhilippe Charnier 			errx(1, "calloc");
2339b50d902SRodney W. Grimes 		*nextp = ep;
234ac3cd520SBrian Feldman 		nextp = &ep->next;
235dc65bcacSEd Schouten 		memmove(&ep->utmp, utmp, sizeof *utmp);
2369b50d902SRodney W. Grimes 		ep->tdev = stp->st_rdev;
2379b50d902SRodney W. Grimes 		/*
2389b50d902SRodney W. Grimes 		 * If this is the console device, attempt to ascertain
2399b50d902SRodney W. Grimes 		 * the true console device dev_t.
2409b50d902SRodney W. Grimes 		 */
2419b50d902SRodney W. Grimes 		if (ep->tdev == 0) {
2429b50d902SRodney W. Grimes 			size_t size;
2439b50d902SRodney W. Grimes 
2449b50d902SRodney W. Grimes 			size = sizeof(dev_t);
245bec1fa86SPoul-Henning Kamp 			(void)sysctlbyname("machdep.consdev", &ep->tdev, &size, NULL, 0);
2469b50d902SRodney W. Grimes 		}
2473bebe991SBrian Somers 		touched = stp->st_atime;
248dc65bcacSEd Schouten 		if (touched < ep->utmp.ut_tv.tv_sec) {
2493bebe991SBrian Somers 			/* tty untouched since before login */
250dc65bcacSEd Schouten 			touched = ep->utmp.ut_tv.tv_sec;
2513bebe991SBrian Somers 		}
2523bebe991SBrian Somers 		if ((ep->idle = now - touched) < 0)
2539b50d902SRodney W. Grimes 			ep->idle = 0;
2549b50d902SRodney W. Grimes 	}
255dc65bcacSEd Schouten 	endutxent();
2569b50d902SRodney W. Grimes 
2579b50d902SRodney W. Grimes 	if (header || wcmd == 0) {
2589b50d902SRodney W. Grimes 		pr_header(&now, nusers);
259c59c7f97SPoul-Henning Kamp 		if (wcmd == 0) {
260c59c7f97SPoul-Henning Kamp 			(void)kvm_close(kd);
2619b50d902SRodney W. Grimes 			exit(0);
262c59c7f97SPoul-Henning Kamp 		}
2639b50d902SRodney W. Grimes 
264a88d7a82SJoseph Koshy #define HEADER_USER		"USER"
265a88d7a82SJoseph Koshy #define HEADER_TTY		"TTY"
266a88d7a82SJoseph Koshy #define HEADER_FROM		"FROM"
267a88d7a82SJoseph Koshy #define HEADER_LOGIN_IDLE	"LOGIN@  IDLE "
268a88d7a82SJoseph Koshy #define HEADER_WHAT		"WHAT\n"
269dc65bcacSEd Schouten #define WUSED  (W_DISPUSERSIZE + W_DISPLINESIZE + W_DISPHOSTSIZE + \
270a88d7a82SJoseph Koshy 		sizeof(HEADER_LOGIN_IDLE) + 3)	/* header width incl. spaces */
271a88d7a82SJoseph Koshy 		(void)printf("%-*.*s %-*.*s %-*.*s  %s",
272dc65bcacSEd Schouten 				W_DISPUSERSIZE, W_DISPUSERSIZE, HEADER_USER,
273dc65bcacSEd Schouten 				W_DISPLINESIZE, W_DISPLINESIZE, HEADER_TTY,
2745b718312SBrian Somers 				W_DISPHOSTSIZE, W_DISPHOSTSIZE, HEADER_FROM,
275a88d7a82SJoseph Koshy 				HEADER_LOGIN_IDLE HEADER_WHAT);
2760812a2b4SPeter Wemm 	}
2779b50d902SRodney W. Grimes 
2789b50d902SRodney W. Grimes 	if ((kp = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nentries)) == NULL)
2799b50d902SRodney W. Grimes 		err(1, "%s", kvm_geterr(kd));
2809b50d902SRodney W. Grimes 	for (i = 0; i < nentries; i++, kp++) {
2811131779fSEd Schouten 		if (kp->ki_stat == SIDL || kp->ki_stat == SZOMB ||
2821131779fSEd Schouten 		    kp->ki_tdev == NODEV)
2839b50d902SRodney W. Grimes 			continue;
2849b50d902SRodney W. Grimes 		for (ep = ehead; ep != NULL; ep = ep->next) {
2851f7d2501SKirk McKusick 			if (ep->tdev == kp->ki_tdev) {
2869b50d902SRodney W. Grimes 				/*
2871bb32253SAndrey A. Chernov 				 * proc is associated with this terminal
2881bb32253SAndrey A. Chernov 				 */
2891f7d2501SKirk McKusick 				if (ep->kp == NULL && kp->ki_pgid == kp->ki_tpgid) {
2901bb32253SAndrey A. Chernov 					/*
2911bb32253SAndrey A. Chernov 					 * Proc is 'most interesting'
2929b50d902SRodney W. Grimes 					 */
2931f7d2501SKirk McKusick 					if (proc_compare(ep->kp, kp))
2949b50d902SRodney W. Grimes 						ep->kp = kp;
2951bb32253SAndrey A. Chernov 				}
2961bb32253SAndrey A. Chernov 				/*
2971bb32253SAndrey A. Chernov 				 * Proc debug option info; add to debug
2981f7d2501SKirk McKusick 				 * list using kinfo_proc ki_spare[0]
2991bb32253SAndrey A. Chernov 				 * as next pointer; ptr to ptr avoids the
3001bb32253SAndrey A. Chernov 				 * ptr = long assumption.
3011bb32253SAndrey A. Chernov 				 */
3021bb32253SAndrey A. Chernov 				dkp = ep->dkp;
3031bb32253SAndrey A. Chernov 				ep->dkp = kp;
304ac3cd520SBrian Feldman 				debugproc(kp) = dkp;
3059b50d902SRodney W. Grimes 			}
3069b50d902SRodney W. Grimes 		}
3079b50d902SRodney W. Grimes 	}
3089b50d902SRodney W. Grimes 	if ((ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1 &&
3099b50d902SRodney W. Grimes 	     ioctl(STDERR_FILENO, TIOCGWINSZ, &ws) == -1 &&
3109b50d902SRodney W. Grimes 	     ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == -1) || ws.ws_col == 0)
3119b50d902SRodney W. Grimes 	       ttywidth = 79;
3129b50d902SRodney W. Grimes         else
3139b50d902SRodney W. Grimes 	       ttywidth = ws.ws_col - 1;
3149b50d902SRodney W. Grimes 	argwidth = ttywidth - WUSED;
3159b50d902SRodney W. Grimes 	if (argwidth < 4)
3169b50d902SRodney W. Grimes 		argwidth = 8;
3179b50d902SRodney W. Grimes 	for (ep = ehead; ep != NULL; ep = ep->next) {
3189b50d902SRodney W. Grimes 		if (ep->kp == NULL) {
3198b56c58bSMark Murray 			ep->args = strdup("-");
3209b50d902SRodney W. Grimes 			continue;
3219b50d902SRodney W. Grimes 		}
3229b50d902SRodney W. Grimes 		ep->args = fmt_argv(kvm_getargv(kd, ep->kp, argwidth),
3231f7d2501SKirk McKusick 		    ep->kp->ki_comm, MAXCOMLEN);
3249b50d902SRodney W. Grimes 		if (ep->args == NULL)
3259b50d902SRodney W. Grimes 			err(1, NULL);
3269b50d902SRodney W. Grimes 	}
3279b50d902SRodney W. Grimes 	/* sort by idle time */
3289b50d902SRodney W. Grimes 	if (sortidle && ehead != NULL) {
329ac3cd520SBrian Feldman 		struct entry *from, *save;
3309b50d902SRodney W. Grimes 
331ac3cd520SBrian Feldman 		from = ehead;
3329b50d902SRodney W. Grimes 		ehead = NULL;
3339b50d902SRodney W. Grimes 		while (from != NULL) {
3349b50d902SRodney W. Grimes 			for (nextp = &ehead;
3359b50d902SRodney W. Grimes 			    (*nextp) && from->idle >= (*nextp)->idle;
3369b50d902SRodney W. Grimes 			    nextp = &(*nextp)->next)
3379b50d902SRodney W. Grimes 				continue;
3389b50d902SRodney W. Grimes 			save = from;
3399b50d902SRodney W. Grimes 			from = from->next;
3409b50d902SRodney W. Grimes 			save->next = *nextp;
3419b50d902SRodney W. Grimes 			*nextp = save;
3429b50d902SRodney W. Grimes 		}
3439b50d902SRodney W. Grimes 	}
3449b50d902SRodney W. Grimes 
3459b50d902SRodney W. Grimes 	for (ep = ehead; ep != NULL; ep = ep->next) {
346b94f6865SHajimu UMEMOTO 		struct addrinfo hints, *res;
3475b718312SBrian Somers 		struct sockaddr_storage ss;
3485b718312SBrian Somers 		struct sockaddr *sa = (struct sockaddr *)&ss;
3498b56c58bSMark Murray 		struct sockaddr_in *lsin = (struct sockaddr_in *)&ss;
3508b56c58bSMark Murray 		struct sockaddr_in6 *lsin6 = (struct sockaddr_in6 *)&ss;
351c76b41b7SPeter Wemm 		time_t t;
3525b718312SBrian Somers 		int isaddr;
3539587fac0SAndrey A. Chernov 
354dc65bcacSEd Schouten 		p = *ep->utmp.ut_host ? ep->utmp.ut_host : "-";
3555b718312SBrian Somers 		if ((x_suffix = strrchr(p, ':')) != NULL) {
3565b718312SBrian Somers 			if ((dot = strchr(x_suffix, '.')) != NULL &&
3575b718312SBrian Somers 			    strchr(dot+1, '.') == NULL)
3585b718312SBrian Somers 				*x_suffix++ = '\0';
3595b718312SBrian Somers 			else
3605b718312SBrian Somers 				x_suffix = NULL;
3619b50d902SRodney W. Grimes 		}
362b94f6865SHajimu UMEMOTO 
3635b718312SBrian Somers 		isaddr = 0;
3645b718312SBrian Somers 		memset(&ss, '\0', sizeof(ss));
3658b56c58bSMark Murray 		if (inet_pton(AF_INET6, p, &lsin6->sin6_addr) == 1) {
3668b56c58bSMark Murray 			lsin6->sin6_len = sizeof(*lsin6);
3678b56c58bSMark Murray 			lsin6->sin6_family = AF_INET6;
3685b718312SBrian Somers 			isaddr = 1;
3698b56c58bSMark Murray 		} else if (inet_pton(AF_INET, p, &lsin->sin_addr) == 1) {
3708b56c58bSMark Murray 			lsin->sin_len = sizeof(*lsin);
3718b56c58bSMark Murray 			lsin->sin_family = AF_INET;
3725b718312SBrian Somers 			isaddr = 1;
3739b50d902SRodney W. Grimes 		}
374b94f6865SHajimu UMEMOTO 		if (!nflag) {
375b94f6865SHajimu UMEMOTO 			/* Attempt to change an IP address into a name */
3765b718312SBrian Somers 			if (isaddr && realhostname_sa(fn, sizeof(fn), sa,
3775b718312SBrian Somers 			    sa->sa_len) == HOSTNAME_FOUND)
3785b718312SBrian Somers 				p = fn;
379b94f6865SHajimu UMEMOTO 		} else if (!isaddr) {
380b94f6865SHajimu UMEMOTO 			/*
381b94f6865SHajimu UMEMOTO 			 * If a host has only one A/AAAA RR, change a
382b94f6865SHajimu UMEMOTO 			 * name into an IP address
383b94f6865SHajimu UMEMOTO 			 */
384b94f6865SHajimu UMEMOTO 			memset(&hints, 0, sizeof(hints));
385b94f6865SHajimu UMEMOTO 			hints.ai_flags = AI_PASSIVE;
386b94f6865SHajimu UMEMOTO 			hints.ai_family = AF_UNSPEC;
387b94f6865SHajimu UMEMOTO 			hints.ai_socktype = SOCK_STREAM;
388b94f6865SHajimu UMEMOTO 			if (getaddrinfo(p, NULL, &hints, &res) == 0) {
389b94f6865SHajimu UMEMOTO 				if (res->ai_next == NULL &&
390b94f6865SHajimu UMEMOTO 				    getnameinfo(res->ai_addr, res->ai_addrlen,
391b94f6865SHajimu UMEMOTO 					fn, sizeof(fn), NULL, 0,
392b94f6865SHajimu UMEMOTO 					NI_NUMERICHOST) == 0)
393b94f6865SHajimu UMEMOTO 					p = fn;
394b94f6865SHajimu UMEMOTO 				freeaddrinfo(res);
395c54481c1SAndrey A. Chernov 			}
396b94f6865SHajimu UMEMOTO 		}
397b94f6865SHajimu UMEMOTO 
3985b718312SBrian Somers 		if (x_suffix) {
3995b718312SBrian Somers 			(void)snprintf(buf, sizeof(buf), "%s:%s", p, x_suffix);
4009b50d902SRodney W. Grimes 			p = buf;
4019b50d902SRodney W. Grimes 		}
4021bb32253SAndrey A. Chernov 		if (dflag) {
403ac3cd520SBrian Feldman 			for (dkp = ep->dkp; dkp != NULL; dkp = debugproc(dkp)) {
4048b56c58bSMark Murray 				const char *ptr;
405ac3cd520SBrian Feldman 
4060cadb9caSBrian Somers 				ptr = fmt_argv(kvm_getargv(kd, dkp, argwidth),
4071f7d2501SKirk McKusick 				    dkp->ki_comm, MAXCOMLEN);
4080cadb9caSBrian Somers 				if (ptr == NULL)
4090cadb9caSBrian Somers 					ptr = "-";
410ac3cd520SBrian Feldman 				(void)printf("\t\t%-9d %s\n",
4111f7d2501SKirk McKusick 				    dkp->ki_pid, ptr);
4121bb32253SAndrey A. Chernov 			}
4131bb32253SAndrey A. Chernov 		}
4143d65e4d1SPoul-Henning Kamp 		(void)printf("%-*.*s %-*.*s %-*.*s ",
415dc65bcacSEd Schouten 		    W_DISPUSERSIZE, W_DISPUSERSIZE, ep->utmp.ut_user,
416dc65bcacSEd Schouten 		    W_DISPLINESIZE, W_DISPLINESIZE,
4171131779fSEd Schouten 		    *ep->utmp.ut_line ?
4181131779fSEd Schouten 		    (strncmp(ep->utmp.ut_line, "tty", 3) &&
419255318a8SAndrey A. Chernov 		    strncmp(ep->utmp.ut_line, "cua", 3) ?
4201131779fSEd Schouten 		    ep->utmp.ut_line : ep->utmp.ut_line + 3) : "-",
4215b718312SBrian Somers 		    W_DISPHOSTSIZE, W_DISPHOSTSIZE, *p ? p : "-");
422dc65bcacSEd Schouten 		t = ep->utmp.ut_tv.tv_sec;
42334903a55SHajimu UMEMOTO 		longattime = pr_attime(&t, &now);
42470498d5aSDaniel O'Callaghan 		longidle = pr_idle(ep->idle);
42534903a55SHajimu UMEMOTO 		(void)printf("%.*s\n", argwidth - longidle - longattime,
42634903a55SHajimu UMEMOTO 		    ep->args);
4279b50d902SRodney W. Grimes 	}
428c59c7f97SPoul-Henning Kamp 	(void)kvm_close(kd);
4299b50d902SRodney W. Grimes 	exit(0);
4309b50d902SRodney W. Grimes }
4319b50d902SRodney W. Grimes 
4329b50d902SRodney W. Grimes static void
433e8e649ccSJuli Mallett pr_header(time_t *nowp, int nusers)
4349b50d902SRodney W. Grimes {
4359b50d902SRodney W. Grimes 	double avenrun[3];
436716ced0bSDavid E. O'Brien 	time_t uptime;
437a21cbcb8SAndre Oppermann 	struct timespec tp;
4389d8c91b7SAndrey A. Chernov 	int days, hrs, i, mins, secs;
4399b50d902SRodney W. Grimes 	char buf[256];
4409b50d902SRodney W. Grimes 
4419b50d902SRodney W. Grimes 	/*
4429b50d902SRodney W. Grimes 	 * Print time of day.
4439b50d902SRodney W. Grimes 	 */
44469fe77ceSPhilippe Charnier 	if (strftime(buf, sizeof(buf),
44569fe77ceSPhilippe Charnier 	    use_ampm ? "%l:%M%p" : "%k:%M", localtime(nowp)) != 0)
4469b50d902SRodney W. Grimes 		(void)printf("%s ", buf);
4479b50d902SRodney W. Grimes 	/*
4489b50d902SRodney W. Grimes 	 * Print how long system has been up.
4499b50d902SRodney W. Grimes 	 */
450a21cbcb8SAndre Oppermann 	if (clock_gettime(CLOCK_MONOTONIC, &tp) != -1) {
451a21cbcb8SAndre Oppermann 		uptime = tp.tv_sec;
452716ced0bSDavid E. O'Brien 		if (uptime > 60)
453716ced0bSDavid E. O'Brien 			uptime += 30;
454716ced0bSDavid E. O'Brien 		days = uptime / 86400;
455716ced0bSDavid E. O'Brien 		uptime %= 86400;
456716ced0bSDavid E. O'Brien 		hrs = uptime / 3600;
457716ced0bSDavid E. O'Brien 		uptime %= 3600;
458716ced0bSDavid E. O'Brien 		mins = uptime / 60;
459716ced0bSDavid E. O'Brien 		secs = uptime % 60;
4609b50d902SRodney W. Grimes 		(void)printf(" up");
4619b50d902SRodney W. Grimes 		if (days > 0)
4629b50d902SRodney W. Grimes 			(void)printf(" %d day%s,", days, days > 1 ? "s" : "");
4639b50d902SRodney W. Grimes 		if (hrs > 0 && mins > 0)
4649b50d902SRodney W. Grimes 			(void)printf(" %2d:%02d,", hrs, mins);
4659d8c91b7SAndrey A. Chernov 		else if (hrs > 0)
466ac3cd520SBrian Feldman 			(void)printf(" %d hr%s,", hrs, hrs > 1 ? "s" : "");
4679d8c91b7SAndrey A. Chernov 		else if (mins > 0)
468ac3cd520SBrian Feldman 			(void)printf(" %d min%s,", mins, mins > 1 ? "s" : "");
4699d8c91b7SAndrey A. Chernov 		else
470ac3cd520SBrian Feldman 			(void)printf(" %d sec%s,", secs, secs > 1 ? "s" : "");
4719b50d902SRodney W. Grimes 	}
4729b50d902SRodney W. Grimes 
4739b50d902SRodney W. Grimes 	/* Print number of users logged in to system */
4741b0c06d9SScott Mace 	(void)printf(" %d user%s", nusers, nusers == 1 ? "" : "s");
4759b50d902SRodney W. Grimes 
4769b50d902SRodney W. Grimes 	/*
4779b50d902SRodney W. Grimes 	 * Print 1, 5, and 15 minute load averages.
4789b50d902SRodney W. Grimes 	 */
4799b50d902SRodney W. Grimes 	if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) == -1)
4809b50d902SRodney W. Grimes 		(void)printf(", no load average information available\n");
4819b50d902SRodney W. Grimes 	else {
4829b50d902SRodney W. Grimes 		(void)printf(", load averages:");
4838b56c58bSMark Murray 		for (i = 0; i < (int)(sizeof(avenrun) / sizeof(avenrun[0])); i++) {
4842742fc8eSAndrey A. Chernov 			if (use_comma && i > 0)
4852742fc8eSAndrey A. Chernov 				(void)printf(",");
486e25fd27cSAndrey A. Chernov 			(void)printf(" %.2f", avenrun[i]);
4872742fc8eSAndrey A. Chernov 		}
4889b50d902SRodney W. Grimes 		(void)printf("\n");
4899b50d902SRodney W. Grimes 	}
4909b50d902SRodney W. Grimes }
4919b50d902SRodney W. Grimes 
4929b50d902SRodney W. Grimes static struct stat *
493dc65bcacSEd Schouten ttystat(char *line)
4949b50d902SRodney W. Grimes {
4959b50d902SRodney W. Grimes 	static struct stat sb;
4969b50d902SRodney W. Grimes 	char ttybuf[MAXPATHLEN];
4979b50d902SRodney W. Grimes 
498dc65bcacSEd Schouten 	(void)snprintf(ttybuf, sizeof(ttybuf), "%s%s", _PATH_DEV, line);
49921632754SEd Schouten 	if (stat(ttybuf, &sb) == 0 && S_ISCHR(sb.st_mode)) {
5009b50d902SRodney W. Grimes 		return (&sb);
501d0d0355eSSean Chittenden 	} else
502d0d0355eSSean Chittenden 		return (NULL);
5039b50d902SRodney W. Grimes }
5049b50d902SRodney W. Grimes 
5059b50d902SRodney W. Grimes static void
506e8e649ccSJuli Mallett usage(int wcmd)
5079b50d902SRodney W. Grimes {
5089b50d902SRodney W. Grimes 	if (wcmd)
5099b50d902SRodney W. Grimes 		(void)fprintf(stderr,
510ac3cd520SBrian Feldman 		    "usage: w [-dhin] [-M core] [-N system] [user ...]\n");
5119b50d902SRodney W. Grimes 	else
512ac3cd520SBrian Feldman 		(void)fprintf(stderr, "usage: uptime\n");
5139b50d902SRodney W. Grimes 	exit(1);
5149b50d902SRodney W. Grimes }
515a4f33f43SMatthew Dillon 
516a4f33f43SMatthew Dillon static int
517e8e649ccSJuli Mallett this_is_uptime(const char *s)
518a4f33f43SMatthew Dillon {
519a4f33f43SMatthew Dillon 	const char *u;
520a4f33f43SMatthew Dillon 
521a4f33f43SMatthew Dillon 	if ((u = strrchr(s, '/')) != NULL)
522a4f33f43SMatthew Dillon 		++u;
523a4f33f43SMatthew Dillon 	else
524a4f33f43SMatthew Dillon 		u = s;
525a4f33f43SMatthew Dillon 	if (strcmp(u, "uptime") == 0)
526a4f33f43SMatthew Dillon 		return (0);
527a4f33f43SMatthew Dillon 	return (-1);
528a4f33f43SMatthew Dillon }
529