xref: /illumos-gate/usr/src/cmd/w/w.c (revision 45de8795)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
576e222fdSSumanth Naropanth  * Common Development and Distribution License (the "License").
676e222fdSSumanth Naropanth  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
220a1278f2SGary Mills  * Copyright (c) 2013 Gary Mills
230a1278f2SGary Mills  *
2476e222fdSSumanth Naropanth  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
257c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
266a79a301SJason King  *
276a79a301SJason King  * Copyright 2020 Joyent, Inc.
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
31352d7aedSToomas Soome /*	  All Rights Reserved	*/
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate /*
347c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
357c478bd9Sstevel@tonic-gate  * The Regents of the University of California
367c478bd9Sstevel@tonic-gate  * All Rights Reserved
377c478bd9Sstevel@tonic-gate  *
387c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
397c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
407c478bd9Sstevel@tonic-gate  * contributors.
417c478bd9Sstevel@tonic-gate  */
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate /*
447c478bd9Sstevel@tonic-gate  * This is the new w command which takes advantage of
457c478bd9Sstevel@tonic-gate  * the /proc interface to gain access to the information
467c478bd9Sstevel@tonic-gate  * of all the processes currently on the system.
477c478bd9Sstevel@tonic-gate  *
487c478bd9Sstevel@tonic-gate  * This program also implements 'uptime'.
497c478bd9Sstevel@tonic-gate  *
507c478bd9Sstevel@tonic-gate  * Maintenance note:
517c478bd9Sstevel@tonic-gate  *
527c478bd9Sstevel@tonic-gate  * Much of this code is replicated in whodo.c.  If you're
537c478bd9Sstevel@tonic-gate  * fixing bugs here, then you should probably fix 'em there too.
547c478bd9Sstevel@tonic-gate  */
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate #include <stdio.h>
577c478bd9Sstevel@tonic-gate #include <string.h>
587c478bd9Sstevel@tonic-gate #include <stdarg.h>
597c478bd9Sstevel@tonic-gate #include <stdlib.h>
607c478bd9Sstevel@tonic-gate #include <ctype.h>
617c478bd9Sstevel@tonic-gate #include <fcntl.h>
627c478bd9Sstevel@tonic-gate #include <time.h>
636a79a301SJason King #include <err.h>
647c478bd9Sstevel@tonic-gate #include <errno.h>
657c478bd9Sstevel@tonic-gate #include <sys/types.h>
667c478bd9Sstevel@tonic-gate #include <utmpx.h>
677c478bd9Sstevel@tonic-gate #include <sys/stat.h>
687c478bd9Sstevel@tonic-gate #include <dirent.h>
697c478bd9Sstevel@tonic-gate #include <procfs.h>		/* /proc header file */
707c478bd9Sstevel@tonic-gate #include <locale.h>
717c478bd9Sstevel@tonic-gate #include <unistd.h>
727c478bd9Sstevel@tonic-gate #include <sys/loadavg.h>
737c478bd9Sstevel@tonic-gate #include <limits.h>
747c478bd9Sstevel@tonic-gate #include <priv_utils.h>
756a79a301SJason King #include <sys/sysmacros.h>
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate /*
780a1278f2SGary Mills  * Use the full lengths from utmpx for user and line.
797c478bd9Sstevel@tonic-gate  */
807c478bd9Sstevel@tonic-gate static struct utmpx dummy;
817c478bd9Sstevel@tonic-gate #define	NMAX		(sizeof (dummy.ut_user))
827c478bd9Sstevel@tonic-gate #define	LMAX		(sizeof (dummy.ut_line))
830a1278f2SGary Mills 
840a1278f2SGary Mills /* Print minimum field widths. */
850a1278f2SGary Mills #define	LOGIN_WIDTH	8
86f0540631SGary Mills #define	LINE_WIDTH	8
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate #define	DIV60(t)	((t+30)/60)	/* x/60 rounded */
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate #ifdef ERR
917c478bd9Sstevel@tonic-gate #undef ERR
927c478bd9Sstevel@tonic-gate #endif
937c478bd9Sstevel@tonic-gate #define	ERR		(-1)
947c478bd9Sstevel@tonic-gate 
95352d7aedSToomas Soome #define	HSIZE		256		/* size of process hash table	*/
967c478bd9Sstevel@tonic-gate #define	PROCDIR		"/proc"
977c478bd9Sstevel@tonic-gate #define	INITPROCESS	(pid_t)1	/* init process pid */
987c478bd9Sstevel@tonic-gate #define	NONE		'n'		/* no state */
997c478bd9Sstevel@tonic-gate #define	RUNNING		'r'		/* runnable process */
1007c478bd9Sstevel@tonic-gate #define	ZOMBIE		'z'		/* zombie process */
1017c478bd9Sstevel@tonic-gate #define	VISITED		'v'		/* marked node as visited */
1027c478bd9Sstevel@tonic-gate #define	PRINTF(a)	if (printf a < 0) { \
1037c478bd9Sstevel@tonic-gate 		perror((gettext("%s: printf failed"), prog)); \
1047c478bd9Sstevel@tonic-gate 		exit(1); }
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate struct uproc {
1077c478bd9Sstevel@tonic-gate 	pid_t	p_upid;			/* process id */
1087c478bd9Sstevel@tonic-gate 	char	p_state;		/* numeric value of process state */
1097c478bd9Sstevel@tonic-gate 	dev_t   p_ttyd;			/* controlling tty of process */
1107c478bd9Sstevel@tonic-gate 	time_t  p_time;			/* seconds of user & system time */
1117c478bd9Sstevel@tonic-gate 	time_t	p_ctime;		/* seconds of child user & sys time */
1127c478bd9Sstevel@tonic-gate 	int	p_igintr;		/* 1 = ignores SIGQUIT and SIGINT */
1137c478bd9Sstevel@tonic-gate 	char    p_comm[PRARGSZ+1];	/* command */
1147c478bd9Sstevel@tonic-gate 	char    p_args[PRARGSZ+1];	/* command line arguments */
1157c478bd9Sstevel@tonic-gate 	struct uproc	*p_child,	/* first child pointer */
1167c478bd9Sstevel@tonic-gate 			*p_sibling,	/* sibling pointer */
1177c478bd9Sstevel@tonic-gate 			*p_pgrpl,	/* pgrp link */
1187c478bd9Sstevel@tonic-gate 			*p_link;	/* hash table chain pointer */
1197c478bd9Sstevel@tonic-gate };
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate /*
122352d7aedSToomas Soome  *	define hash table for struct uproc
1237c478bd9Sstevel@tonic-gate  *	Hash function uses process id
1247c478bd9Sstevel@tonic-gate  *	and the size of the hash table(HSIZE)
1257c478bd9Sstevel@tonic-gate  *	to determine process index into the table.
1267c478bd9Sstevel@tonic-gate  */
1277c478bd9Sstevel@tonic-gate static struct uproc	pr_htbl[HSIZE];
1287c478bd9Sstevel@tonic-gate 
129352d7aedSToomas Soome static struct	uproc	*findhash(pid_t);
130352d7aedSToomas Soome static time_t	findidle(char *);
1317c478bd9Sstevel@tonic-gate static void	clnarglist(char *);
1327c478bd9Sstevel@tonic-gate static void	showtotals(struct uproc *);
1337c478bd9Sstevel@tonic-gate static void	calctotals(struct uproc *);
134f0540631SGary Mills static void	prttime(time_t, int);
1357c478bd9Sstevel@tonic-gate static void	prtat(time_t *time);
1367c478bd9Sstevel@tonic-gate 
1376a79a301SJason King static int	priv_proc_open(const char *, int);
1386a79a301SJason King static int	priv_proc_openat(int, const char *, int);
1396a79a301SJason King static boolean_t do_proc_read(int, void *, size_t);
1406a79a301SJason King 
1417c478bd9Sstevel@tonic-gate static char	*prog;		/* pointer to invocation name */
1427c478bd9Sstevel@tonic-gate static int	header = 1;	/* true if -h flag: don't print heading */
1437c478bd9Sstevel@tonic-gate static int	lflag = 1;	/* set if -l flag; 0 for -s flag: short form */
1447c478bd9Sstevel@tonic-gate static char	*sel_user;	/* login of particular user selected */
145352d7aedSToomas Soome static char	firstchar;	/* first char of name of prog invoked as */
1467c478bd9Sstevel@tonic-gate static int	login;		/* true if invoked as login shell */
1477c478bd9Sstevel@tonic-gate static time_t	now;		/* current time of day */
1487c478bd9Sstevel@tonic-gate static time_t	uptime;		/* time of last reboot & elapsed time since */
1497c478bd9Sstevel@tonic-gate static int	nusers;		/* number of users logged in now */
1507c478bd9Sstevel@tonic-gate static time_t	idle;		/* number of minutes user is idle */
1517c478bd9Sstevel@tonic-gate static time_t	jobtime;	/* total cpu time visible */
1527c478bd9Sstevel@tonic-gate static char	doing[520];	/* process attached to terminal */
1537c478bd9Sstevel@tonic-gate static time_t	proctime;	/* cpu time of process in doing */
1547c478bd9Sstevel@tonic-gate static pid_t	curpid, empty;
1557c478bd9Sstevel@tonic-gate static int	add_times;	/* boolean: add the cpu times or not */
1567c478bd9Sstevel@tonic-gate 
1576a79a301SJason King /*
1586a79a301SJason King  * Basic privs we never need and can drop. This is likely not exhaustive,
1596a79a301SJason King  * but should significantly reduce any potential attack surfaces.
1606a79a301SJason King  */
1616a79a301SJason King static const char *drop_privs[] = {
1626a79a301SJason King 	PRIV_FILE_WRITE,
1636a79a301SJason King 	PRIV_NET_ACCESS,
1646a79a301SJason King 	PRIV_PROC_EXEC,
1656a79a301SJason King 	PRIV_PROC_FORK,
1666a79a301SJason King 	PRIV_FILE_LINK_ANY
1676a79a301SJason King };
1686a79a301SJason King 
1697c478bd9Sstevel@tonic-gate #if SIGQUIT > SIGINT
1707c478bd9Sstevel@tonic-gate #define	ACTSIZE	SIGQUIT
1717c478bd9Sstevel@tonic-gate #else
1727c478bd9Sstevel@tonic-gate #define	ACTSIZE	SIGINT
1737c478bd9Sstevel@tonic-gate #endif
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate int
1767c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
1777c478bd9Sstevel@tonic-gate {
1787c478bd9Sstevel@tonic-gate 	struct utmpx	*ut;
1797c478bd9Sstevel@tonic-gate 	struct utmpx	*utmpbegin;
1807c478bd9Sstevel@tonic-gate 	struct utmpx	*utmpend;
1817c478bd9Sstevel@tonic-gate 	struct utmpx	*utp;
1827c478bd9Sstevel@tonic-gate 	struct uproc	*up, *parent, *pgrp;
1837c478bd9Sstevel@tonic-gate 	struct psinfo	info;
1847c478bd9Sstevel@tonic-gate 	struct sigaction actinfo[ACTSIZE];
1857c478bd9Sstevel@tonic-gate 	struct pstatus	statinfo;
1867c478bd9Sstevel@tonic-gate 	struct stat	sbuf;
187352d7aedSToomas Soome 	DIR		*dirp;
1887c478bd9Sstevel@tonic-gate 	struct	dirent	*dp;
1896a79a301SJason King 	char		pname[PATH_MAX];
1907c478bd9Sstevel@tonic-gate 	int		procfd;
1916a79a301SJason King 	int		dirfd;
1927c478bd9Sstevel@tonic-gate 	char		*cp;
1937c478bd9Sstevel@tonic-gate 	int		i;
1947c478bd9Sstevel@tonic-gate 	int		days, hrs, mins;
1957c478bd9Sstevel@tonic-gate 	int		entries;
1967c478bd9Sstevel@tonic-gate 	double		loadavg[3];
1976a79a301SJason King 	priv_set_t	*pset;
1986a79a301SJason King 
1996a79a301SJason King 	if (__init_suid_priv(PU_CLEARLIMITSET, PRIV_PROC_OWNER, NULL) != 0) {
2006a79a301SJason King 		err(EXIT_FAILURE, "failed to enable privilege bracketing");
2016a79a301SJason King 	}
2026a79a301SJason King 
2036a79a301SJason King 	/*
2046a79a301SJason King 	 * After setting up privilege bracketing, we can further reduce the
2056a79a301SJason King 	 * privileges in use. The effective set is set to the basic set minus
2066a79a301SJason King 	 * the privs in drop_privs. The permitted set is the effective set
2076a79a301SJason King 	 * plus PRIV_PROC_OWNER (i.e. the privilege being bracketed).
2086a79a301SJason King 	 */
2096a79a301SJason King 	pset = priv_allocset();
2106a79a301SJason King 	if (pset == NULL)
2116a79a301SJason King 		err(EXIT_FAILURE, "priv_allocset failed");
2126a79a301SJason King 
2136a79a301SJason King 	priv_basicset(pset);
2146a79a301SJason King 	for (i = 0; i < ARRAY_SIZE(drop_privs); i++) {
2156a79a301SJason King 		if (priv_delset(pset, drop_privs[i]) != 0) {
2166a79a301SJason King 			err(EXIT_FAILURE,
2176a79a301SJason King 			    "failed to remove %s privilege from privilege set",
2186a79a301SJason King 			    drop_privs[i]);
2196a79a301SJason King 		}
2206a79a301SJason King 	}
2216a79a301SJason King 
2226a79a301SJason King 	if (setppriv(PRIV_SET, PRIV_EFFECTIVE, pset) < 0)
2236a79a301SJason King 		err(EXIT_FAILURE, "failed setting effective privilege set");
2246a79a301SJason King 
2256a79a301SJason King 	if (priv_addset(pset, PRIV_PROC_OWNER) != 0) {
2266a79a301SJason King 		err(EXIT_FAILURE,
2276a79a301SJason King 		    "failed to add PRIV_PROC_OWNER privilege to privilege set");
2286a79a301SJason King 	}
2296a79a301SJason King 
2306a79a301SJason King 	if (setppriv(PRIV_SET, PRIV_PERMITTED, pset) < 0)
2316a79a301SJason King 		err(EXIT_FAILURE, "failed to set permitted privilege set");
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 	/*
2346a79a301SJason King 	 * Unfortunately, when run as root, privilege bracketing is a no-op,
2356a79a301SJason King 	 * so we have to add PRIV_PROC_OWNER into our effective set for things
2366a79a301SJason King 	 * to work.
2377c478bd9Sstevel@tonic-gate 	 */
2386a79a301SJason King 	if (getuid() == 0 && setppriv(PRIV_SET, PRIV_EFFECTIVE, pset) < 0) {
2396a79a301SJason King 		err(EXIT_FAILURE, "failed to set effective privilege set");
2406a79a301SJason King 	}
2416a79a301SJason King 
2426a79a301SJason King 	priv_freeset(pset);
2436a79a301SJason King 	pset = NULL;
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
2467c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
2477c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
2487c478bd9Sstevel@tonic-gate #endif
2497c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 	login = (argv[0][0] == '-');
2527c478bd9Sstevel@tonic-gate 	cp = strrchr(argv[0], '/');
2537c478bd9Sstevel@tonic-gate 	firstchar = login ? argv[0][1] : (cp == 0) ? argv[0][0] : cp[1];
2547c478bd9Sstevel@tonic-gate 	prog = argv[0];
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 	while (argc > 1) {
2577c478bd9Sstevel@tonic-gate 		if (argv[1][0] == '-') {
2587c478bd9Sstevel@tonic-gate 			for (i = 1; argv[1][i]; i++) {
2597c478bd9Sstevel@tonic-gate 				switch (argv[1][i]) {
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 				case 'h':
2627c478bd9Sstevel@tonic-gate 					header = 0;
2637c478bd9Sstevel@tonic-gate 					break;
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 				case 'l':
2667c478bd9Sstevel@tonic-gate 					lflag++;
2677c478bd9Sstevel@tonic-gate 					break;
2687c478bd9Sstevel@tonic-gate 				case 's':
2697c478bd9Sstevel@tonic-gate 					lflag = 0;
2707c478bd9Sstevel@tonic-gate 					break;
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 				case 'u':
2737c478bd9Sstevel@tonic-gate 				case 'w':
2747c478bd9Sstevel@tonic-gate 					firstchar = argv[1][i];
2757c478bd9Sstevel@tonic-gate 					break;
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 				default:
2787c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
2797c478bd9Sstevel@tonic-gate 					    "%s: bad flag %s\n"),
2807c478bd9Sstevel@tonic-gate 					    prog, argv[1]);
2817c478bd9Sstevel@tonic-gate 					exit(1);
2827c478bd9Sstevel@tonic-gate 				}
2837c478bd9Sstevel@tonic-gate 			}
2847c478bd9Sstevel@tonic-gate 		} else {
2857c478bd9Sstevel@tonic-gate 			if (!isalnum(argv[1][0]) || argc > 2) {
2867c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
2877c478bd9Sstevel@tonic-gate 				    "usage: %s [ -hlsuw ] [ user ]\n"), prog);
2887c478bd9Sstevel@tonic-gate 				exit(1);
2897c478bd9Sstevel@tonic-gate 			} else
2907c478bd9Sstevel@tonic-gate 				sel_user = argv[1];
2917c478bd9Sstevel@tonic-gate 		}
2927c478bd9Sstevel@tonic-gate 		argc--; argv++;
2937c478bd9Sstevel@tonic-gate 	}
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 	/*
2961eabc4beSSachidananda Urs 	 * read the UTMPX_FILE (contains information about each logged in user)
2977c478bd9Sstevel@tonic-gate 	 */
2986a79a301SJason King 	if (stat(UTMPX_FILE, &sbuf) < 0)
2996a79a301SJason King 		err(EXIT_FAILURE, gettext("stat error of %s"), UTMPX_FILE);
3006a79a301SJason King 
3017c478bd9Sstevel@tonic-gate 	entries = sbuf.st_size / sizeof (struct futmpx);
3026a79a301SJason King 	if ((ut = calloc(entries, sizeof (struct utmpx))) == NULL)
3036a79a301SJason King 		err(EXIT_FAILURE, gettext("calloc error of %s"), UTMPX_FILE);
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 	(void) utmpxname(UTMPX_FILE);
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 	utmpbegin = ut;
3086a79a301SJason King 	utmpend = utmpbegin + entries;
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 	setutxent();
31176e222fdSSumanth Naropanth 	while ((ut < utmpend) && ((utp = getutxent()) != NULL))
3127c478bd9Sstevel@tonic-gate 		(void) memcpy(ut++, utp, sizeof (*ut));
3137c478bd9Sstevel@tonic-gate 	endutxent();
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 	(void) time(&now);	/* get current time */
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 	if (header) {	/* print a header */
3187c478bd9Sstevel@tonic-gate 		prtat(&now);
3197c478bd9Sstevel@tonic-gate 		for (ut = utmpbegin; ut < utmpend; ut++) {
3207c478bd9Sstevel@tonic-gate 			if (ut->ut_type == USER_PROCESS) {
3211eabc4beSSachidananda Urs 				if (!nonuserx(*ut))
3227c478bd9Sstevel@tonic-gate 					nusers++;
3237c478bd9Sstevel@tonic-gate 			} else if (ut->ut_type == BOOT_TIME) {
3247c478bd9Sstevel@tonic-gate 				uptime = now - ut->ut_xtime;
3257c478bd9Sstevel@tonic-gate 				uptime += 30;
3267c478bd9Sstevel@tonic-gate 				days = uptime / (60*60*24);
3277c478bd9Sstevel@tonic-gate 				uptime %= (60*60*24);
3287c478bd9Sstevel@tonic-gate 				hrs = uptime / (60*60);
3297c478bd9Sstevel@tonic-gate 				uptime %= (60*60);
3307c478bd9Sstevel@tonic-gate 				mins = uptime / 60;
3317c478bd9Sstevel@tonic-gate 
332f0540631SGary Mills 				PRINTF((gettext("up")));
3337c478bd9Sstevel@tonic-gate 				if (days > 0)
3347c478bd9Sstevel@tonic-gate 					PRINTF((gettext(
3357c478bd9Sstevel@tonic-gate 					    " %d day(s),"), days));
3367c478bd9Sstevel@tonic-gate 				if (hrs > 0 && mins > 0) {
3377c478bd9Sstevel@tonic-gate 					PRINTF((" %2d:%02d,", hrs, mins));
3387c478bd9Sstevel@tonic-gate 				} else {
3397c478bd9Sstevel@tonic-gate 					if (hrs > 0)
3407c478bd9Sstevel@tonic-gate 						PRINTF((gettext(
3417c478bd9Sstevel@tonic-gate 						    " %d hr(s),"), hrs));
3427c478bd9Sstevel@tonic-gate 					if (mins > 0)
3437c478bd9Sstevel@tonic-gate 						PRINTF((gettext(
3447c478bd9Sstevel@tonic-gate 						    " %d min(s),"), mins));
3457c478bd9Sstevel@tonic-gate 				}
3467c478bd9Sstevel@tonic-gate 			}
3477c478bd9Sstevel@tonic-gate 		}
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 		ut = utmpbegin;	/* rewind utmp data */
3507c478bd9Sstevel@tonic-gate 		PRINTF((((nusers == 1) ?
3517c478bd9Sstevel@tonic-gate 		    gettext("  %d user") : gettext("  %d users")), nusers));
3527c478bd9Sstevel@tonic-gate 		/*
3537c478bd9Sstevel@tonic-gate 		 * Print 1, 5, and 15 minute load averages.
3547c478bd9Sstevel@tonic-gate 		 */
3557c478bd9Sstevel@tonic-gate 		(void) getloadavg(loadavg, 3);
3567c478bd9Sstevel@tonic-gate 		PRINTF((gettext(",  load average: %.2f, %.2f, %.2f\n"),
3577c478bd9Sstevel@tonic-gate 		    loadavg[LOADAVG_1MIN], loadavg[LOADAVG_5MIN],
3587c478bd9Sstevel@tonic-gate 		    loadavg[LOADAVG_15MIN]));
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 		if (firstchar == 'u')	/* uptime command */
3617c478bd9Sstevel@tonic-gate 			exit(0);
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 		if (lflag) {
364f0540631SGary Mills 			PRINTF((dcgettext(NULL, "User     tty      "
365f0540631SGary Mills 			    "login@         idle    JCPU    PCPU what\n",
366f0540631SGary Mills 			    LC_TIME)));
3677c478bd9Sstevel@tonic-gate 		} else {
3687c478bd9Sstevel@tonic-gate 			PRINTF((dcgettext(NULL,
369f0540631SGary Mills 			    "User     tty         idle what\n",
370f0540631SGary Mills 			    LC_TIME)));
3717c478bd9Sstevel@tonic-gate 		}
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 		if (fflush(stdout) == EOF) {
3746a79a301SJason King 			err(EXIT_FAILURE, "fflush failed");
3757c478bd9Sstevel@tonic-gate 		}
3767c478bd9Sstevel@tonic-gate 	}
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 	/*
3797c478bd9Sstevel@tonic-gate 	 * loop through /proc, reading info about each process
3807c478bd9Sstevel@tonic-gate 	 * and build the parent/child tree
3817c478bd9Sstevel@tonic-gate 	 */
3826a79a301SJason King 	if ((dirp = opendir(PROCDIR)) == NULL)
3836a79a301SJason King 		err(EXIT_FAILURE, gettext("could not open %s"), PROCDIR);
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 	while ((dp = readdir(dirp)) != NULL) {
3867c478bd9Sstevel@tonic-gate 		if (dp->d_name[0] == '.')
3877c478bd9Sstevel@tonic-gate 			continue;
3886a79a301SJason King 
3896a79a301SJason King 		if (snprintf(pname, sizeof (pname), "%s/%s", PROCDIR,
3906a79a301SJason King 		    dp->d_name) > sizeof (pname))
3917c478bd9Sstevel@tonic-gate 			continue;
3926a79a301SJason King 
3936a79a301SJason King 		dirfd = priv_proc_open(pname, O_RDONLY | O_DIRECTORY);
3946a79a301SJason King 
395*45de8795SJason King 		if (dirfd < 0)
3966a79a301SJason King 			continue;
3976a79a301SJason King 
3986a79a301SJason King 		procfd = priv_proc_openat(dirfd, "psinfo", O_RDONLY);
3996a79a301SJason King 		if (procfd < 0) {
4006a79a301SJason King 			(void) close(dirfd);
4016a79a301SJason King 			continue;
4026a79a301SJason King 		}
4036a79a301SJason King 
4046a79a301SJason King 		if (!do_proc_read(procfd, &info, sizeof (info))) {
4056a79a301SJason King 			warn(gettext("read() failed on %s"), pname);
4066a79a301SJason King 			(void) close(dirfd);
4077c478bd9Sstevel@tonic-gate 			continue;
4087c478bd9Sstevel@tonic-gate 		}
4097c478bd9Sstevel@tonic-gate 		(void) close(procfd);
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate 		up = findhash(info.pr_pid);
4127c478bd9Sstevel@tonic-gate 		up->p_ttyd = info.pr_ttydev;
4136a79a301SJason King 		up->p_state = (info.pr_nlwp == 0 ? ZOMBIE : RUNNING);
4147c478bd9Sstevel@tonic-gate 		up->p_time = 0;
4157c478bd9Sstevel@tonic-gate 		up->p_ctime = 0;
4167c478bd9Sstevel@tonic-gate 		up->p_igintr = 0;
4176a79a301SJason King 		(void) strlcpy(up->p_comm, info.pr_fname,
4186a79a301SJason King 		    sizeof (up->p_comm));
4197c478bd9Sstevel@tonic-gate 		up->p_args[0] = 0;
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate 		if (up->p_state != NONE && up->p_state != ZOMBIE) {
4226a79a301SJason King 			procfd = priv_proc_openat(dirfd, "status", O_RDONLY);
4236a79a301SJason King 			if (procfd < 0) {
4246a79a301SJason King 				(void) close(dirfd);
4257c478bd9Sstevel@tonic-gate 				continue;
4266a79a301SJason King 			}
4276a79a301SJason King 
4286a79a301SJason King 			if (!do_proc_read(procfd, &statinfo,
4296a79a301SJason King 			    sizeof (statinfo))) {
4306a79a301SJason King 				warn(gettext("read() failed on %s/status"),
4316a79a301SJason King 				    pname);
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 				(void) close(procfd);
4346a79a301SJason King 				(void) close(dirfd);
4357c478bd9Sstevel@tonic-gate 				continue;
4367c478bd9Sstevel@tonic-gate 			}
4377c478bd9Sstevel@tonic-gate 			(void) close(procfd);
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate 			up->p_time = statinfo.pr_utime.tv_sec +
4407c478bd9Sstevel@tonic-gate 			    statinfo.pr_stime.tv_sec;	/* seconds */
4417c478bd9Sstevel@tonic-gate 			up->p_ctime = statinfo.pr_cutime.tv_sec +
4427c478bd9Sstevel@tonic-gate 			    statinfo.pr_cstime.tv_sec;
4437c478bd9Sstevel@tonic-gate 
4446a79a301SJason King 			procfd = priv_proc_openat(dirfd, "sigact", O_RDONLY);
4456a79a301SJason King 			if (procfd < 0) {
4466a79a301SJason King 				(void) close(dirfd);
4477c478bd9Sstevel@tonic-gate 				continue;
4486a79a301SJason King 			}
4496a79a301SJason King 
4506a79a301SJason King 			if (!do_proc_read(procfd, actinfo, sizeof (actinfo))) {
4516a79a301SJason King 				warn(gettext("read() failed on %s/sigact"),
4526a79a301SJason King 				    pname);
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate 				(void) close(procfd);
4556a79a301SJason King 				(void) close(dirfd);
4567c478bd9Sstevel@tonic-gate 				continue;
4577c478bd9Sstevel@tonic-gate 			}
4587c478bd9Sstevel@tonic-gate 			(void) close(procfd);
4596a79a301SJason King 			(void) close(dirfd);
4607c478bd9Sstevel@tonic-gate 			up->p_igintr =
46176e222fdSSumanth Naropanth 			    actinfo[SIGINT-1].sa_handler == SIG_IGN &&
46276e222fdSSumanth Naropanth 			    actinfo[SIGQUIT-1].sa_handler == SIG_IGN;
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 			/*
4657c478bd9Sstevel@tonic-gate 			 * Process args.
4667c478bd9Sstevel@tonic-gate 			 */
4676a79a301SJason King 			up->p_args[0] = '\0';
4687c478bd9Sstevel@tonic-gate 			clnarglist(info.pr_psargs);
4696a79a301SJason King 			(void) strlcpy(up->p_args, info.pr_psargs,
4706a79a301SJason King 			    sizeof (up->p_args));
4717c478bd9Sstevel@tonic-gate 			if (up->p_args[0] == 0 ||
4727c478bd9Sstevel@tonic-gate 			    up->p_args[0] == '-' && up->p_args[1] <= ' ' ||
4737c478bd9Sstevel@tonic-gate 			    up->p_args[0] == '?') {
4746a79a301SJason King 				(void) strlcat(up->p_args, " (",
4756a79a301SJason King 				    sizeof (up->p_args));
4766a79a301SJason King 				(void) strlcat(up->p_args, up->p_comm,
4776a79a301SJason King 				    sizeof (up->p_args));
4786a79a301SJason King 				(void) strlcat(up->p_args, ")",
4796a79a301SJason King 				    sizeof (up->p_args));
4807c478bd9Sstevel@tonic-gate 			}
4817c478bd9Sstevel@tonic-gate 		}
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 		/*
4847c478bd9Sstevel@tonic-gate 		 * link pgrp together in case parents go away
4857c478bd9Sstevel@tonic-gate 		 * Pgrp chain is a single linked list originating
4867c478bd9Sstevel@tonic-gate 		 * from the pgrp leader to its group member.
4877c478bd9Sstevel@tonic-gate 		 */
4887c478bd9Sstevel@tonic-gate 		if (info.pr_pgid != info.pr_pid) {	/* not pgrp leader */
4897c478bd9Sstevel@tonic-gate 			pgrp = findhash(info.pr_pgid);
4907c478bd9Sstevel@tonic-gate 			up->p_pgrpl = pgrp->p_pgrpl;
4917c478bd9Sstevel@tonic-gate 			pgrp->p_pgrpl = up;
4927c478bd9Sstevel@tonic-gate 		}
4937c478bd9Sstevel@tonic-gate 		parent = findhash(info.pr_ppid);
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate 		/* if this is the new member, link it in */
4967c478bd9Sstevel@tonic-gate 		if (parent->p_upid != INITPROCESS) {
4977c478bd9Sstevel@tonic-gate 			if (parent->p_child) {
4987c478bd9Sstevel@tonic-gate 				up->p_sibling = parent->p_child;
4997c478bd9Sstevel@tonic-gate 				up->p_child = 0;
5007c478bd9Sstevel@tonic-gate 			}
5017c478bd9Sstevel@tonic-gate 			parent->p_child = up;
5027c478bd9Sstevel@tonic-gate 		}
5037c478bd9Sstevel@tonic-gate 	}
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 	/* revert to non-privileged user after opening */
5066a79a301SJason King 	__priv_relinquish();
5076a79a301SJason King 	if (getuid() == 0) {
5086a79a301SJason King 		/*
5096a79a301SJason King 		 * Since the privilege bracketing functions are effectively
5106a79a301SJason King 		 * no-ops when running as root, we must explicitly
5116a79a301SJason King 		 * relinquish PRIV_PROC_OWNER ourselves.
5126a79a301SJason King 		 */
5136a79a301SJason King 		pset = priv_allocset();
5146a79a301SJason King 		if (pset == NULL) {
5156a79a301SJason King 			err(EXIT_FAILURE,
5166a79a301SJason King 			    gettext("failed to allocate privilege set"));
5176a79a301SJason King 		}
5186a79a301SJason King 
5196a79a301SJason King 		priv_emptyset(pset);
5206a79a301SJason King 
5216a79a301SJason King 		if (priv_addset(pset, PRIV_PROC_OWNER) != 0) {
5226a79a301SJason King 			err(EXIT_FAILURE, gettext("failed to add "
5236a79a301SJason King 			    "PRIV_PROC_OWNER to privilege set"));
5246a79a301SJason King 		}
5256a79a301SJason King 
5266a79a301SJason King 		if (setppriv(PRIV_OFF, PRIV_PERMITTED, pset) != 0) {
5276a79a301SJason King 			err(EXIT_FAILURE,
5286a79a301SJason King 			    gettext("failed to set permitted privilege set"));
5296a79a301SJason King 		}
5306a79a301SJason King 
5316a79a301SJason King 		priv_freeset(pset);
5326a79a301SJason King 		pset = NULL;
5336a79a301SJason King 	}
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 	(void) closedir(dirp);
5367c478bd9Sstevel@tonic-gate 	(void) time(&now);	/* get current time */
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 	/*
5397c478bd9Sstevel@tonic-gate 	 * loop through utmpx file, printing process info
5407c478bd9Sstevel@tonic-gate 	 * about each logged in user
5417c478bd9Sstevel@tonic-gate 	 */
5427c478bd9Sstevel@tonic-gate 	for (ut = utmpbegin; ut < utmpend; ut++) {
5437c478bd9Sstevel@tonic-gate 		if (ut->ut_type != USER_PROCESS)
5447c478bd9Sstevel@tonic-gate 			continue;
5457c478bd9Sstevel@tonic-gate 		if (sel_user && strncmp(ut->ut_name, sel_user, NMAX) != 0)
5467c478bd9Sstevel@tonic-gate 			continue;	/* we're looking for somebody else */
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate 		/* print login name of the user */
5490a1278f2SGary Mills 		PRINTF(("%-*.*s ", LOGIN_WIDTH, NMAX, ut->ut_name));
5507c478bd9Sstevel@tonic-gate 
5517c478bd9Sstevel@tonic-gate 		/* print tty user is on */
5527c478bd9Sstevel@tonic-gate 		if (lflag) {
553f0540631SGary Mills 			PRINTF(("%-*.*s ", LINE_WIDTH, LMAX, ut->ut_line));
5547c478bd9Sstevel@tonic-gate 		} else {
5557c478bd9Sstevel@tonic-gate 			if (ut->ut_line[0] == 'p' && ut->ut_line[1] == 't' &&
5567c478bd9Sstevel@tonic-gate 			    ut->ut_line[2] == 's' && ut->ut_line[3] == '/') {
557f0540631SGary Mills 				PRINTF(("%-*.*s ", LINE_WIDTH, LMAX,
558f0540631SGary Mills 				    &ut->ut_line[4]));
5597c478bd9Sstevel@tonic-gate 			} else {
560f0540631SGary Mills 				PRINTF(("%-*.*s ", LINE_WIDTH, LMAX,
5610a1278f2SGary Mills 				    ut->ut_line));
5627c478bd9Sstevel@tonic-gate 			}
5637c478bd9Sstevel@tonic-gate 		}
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate 		/* print when the user logged in */
5667c478bd9Sstevel@tonic-gate 		if (lflag) {
5677c478bd9Sstevel@tonic-gate 			time_t tim = ut->ut_xtime;
5687c478bd9Sstevel@tonic-gate 			prtat(&tim);
5697c478bd9Sstevel@tonic-gate 		}
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate 		/* print idle time */
5727c478bd9Sstevel@tonic-gate 		idle = findidle(ut->ut_line);
573f0540631SGary Mills 		prttime(idle, 8);
5747c478bd9Sstevel@tonic-gate 		showtotals(findhash(ut->ut_pid));
5757c478bd9Sstevel@tonic-gate 	}
5766a79a301SJason King 	if (fclose(stdout) == EOF)
5776a79a301SJason King 		err(EXIT_FAILURE, gettext("fclose failed"));
5786a79a301SJason King 
5797c478bd9Sstevel@tonic-gate 	return (0);
5807c478bd9Sstevel@tonic-gate }
5817c478bd9Sstevel@tonic-gate 
5827c478bd9Sstevel@tonic-gate /*
5837c478bd9Sstevel@tonic-gate  *  Prints the CPU time for all processes & children,
5847c478bd9Sstevel@tonic-gate  *  and the cpu time for interesting process,
5857c478bd9Sstevel@tonic-gate  *  and what the user is doing.
5867c478bd9Sstevel@tonic-gate  */
5877c478bd9Sstevel@tonic-gate static void
5887c478bd9Sstevel@tonic-gate showtotals(struct uproc *up)
5897c478bd9Sstevel@tonic-gate {
5907c478bd9Sstevel@tonic-gate 	jobtime = 0;
5917c478bd9Sstevel@tonic-gate 	proctime = 0;
5927c478bd9Sstevel@tonic-gate 	empty = 1;
5937c478bd9Sstevel@tonic-gate 	curpid = -1;
5947c478bd9Sstevel@tonic-gate 	add_times = 1;
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate 	calctotals(up);
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate 	if (lflag) {
5997c478bd9Sstevel@tonic-gate 		/* print CPU time for all processes & children */
6007c478bd9Sstevel@tonic-gate 		/* and need to convert clock ticks to seconds first */
601f0540631SGary Mills 		prttime((time_t)jobtime, 8);
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate 		/* print cpu time for interesting process */
6047c478bd9Sstevel@tonic-gate 		/* and need to convert clock ticks to seconds first */
605f0540631SGary Mills 		prttime((time_t)proctime, 8);
6067c478bd9Sstevel@tonic-gate 	}
6077c478bd9Sstevel@tonic-gate 	/* what user is doing, current process */
608f0540631SGary Mills 	PRINTF(("%-.32s\n", doing));
6097c478bd9Sstevel@tonic-gate }
6107c478bd9Sstevel@tonic-gate 
6117c478bd9Sstevel@tonic-gate /*
6127c478bd9Sstevel@tonic-gate  *  This recursive routine descends the process
6137c478bd9Sstevel@tonic-gate  *  tree starting from the given process pointer(up).
6147c478bd9Sstevel@tonic-gate  *  It used depth-first search strategy and also marked
6157c478bd9Sstevel@tonic-gate  *  each node as visited as it traversed down the tree.
6167c478bd9Sstevel@tonic-gate  *  It calculates the process time for all processes &
6177c478bd9Sstevel@tonic-gate  *  children.  It also finds the interesting process
6187c478bd9Sstevel@tonic-gate  *  and determines its cpu time and command.
6197c478bd9Sstevel@tonic-gate  */
6207c478bd9Sstevel@tonic-gate static void
6217c478bd9Sstevel@tonic-gate calctotals(struct uproc *up)
6227c478bd9Sstevel@tonic-gate {
6237c478bd9Sstevel@tonic-gate 	struct uproc   *zp;
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate 	/*
6267c478bd9Sstevel@tonic-gate 	 * Once a node has been visited, stop adding cpu times
6277c478bd9Sstevel@tonic-gate 	 * for its children so they don't get totalled twice.
6287c478bd9Sstevel@tonic-gate 	 * Still look for the interesting job for this utmp
6297c478bd9Sstevel@tonic-gate 	 * entry, however.
6307c478bd9Sstevel@tonic-gate 	 */
6317c478bd9Sstevel@tonic-gate 	if (up->p_state == VISITED)
6327c478bd9Sstevel@tonic-gate 		add_times = 0;
6337c478bd9Sstevel@tonic-gate 	up->p_state = VISITED;
6347c478bd9Sstevel@tonic-gate 	if (up->p_state == NONE || up->p_state == ZOMBIE)
6357c478bd9Sstevel@tonic-gate 		return;
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate 	if (empty && !up->p_igintr) {
6387c478bd9Sstevel@tonic-gate 		empty = 0;
6397c478bd9Sstevel@tonic-gate 		curpid = -1;
6407c478bd9Sstevel@tonic-gate 	}
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate 	if (up->p_upid > curpid && (!up->p_igintr || empty)) {
6437c478bd9Sstevel@tonic-gate 		curpid = up->p_upid;
6447c478bd9Sstevel@tonic-gate 		if (lflag)
6456a79a301SJason King 			(void) strlcpy(doing, up->p_args, sizeof (doing));
6467c478bd9Sstevel@tonic-gate 		else
6476a79a301SJason King 			(void) strlcpy(doing, up->p_comm, sizeof (doing));
6487c478bd9Sstevel@tonic-gate 	}
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate 	if (add_times == 1) {
6517c478bd9Sstevel@tonic-gate 		jobtime += up->p_time + up->p_ctime;
6527c478bd9Sstevel@tonic-gate 		proctime += up->p_time;
6537c478bd9Sstevel@tonic-gate 	}
6547c478bd9Sstevel@tonic-gate 
6557c478bd9Sstevel@tonic-gate 	/* descend for its children */
6567c478bd9Sstevel@tonic-gate 	if (up->p_child) {
6577c478bd9Sstevel@tonic-gate 		calctotals(up->p_child);
6587c478bd9Sstevel@tonic-gate 		for (zp = up->p_child->p_sibling; zp; zp = zp->p_sibling)
6597c478bd9Sstevel@tonic-gate 			calctotals(zp);
6607c478bd9Sstevel@tonic-gate 	}
6617c478bd9Sstevel@tonic-gate }
6627c478bd9Sstevel@tonic-gate 
6637c478bd9Sstevel@tonic-gate /*
6647c478bd9Sstevel@tonic-gate  *   Findhash  finds the appropriate entry in the process
6657c478bd9Sstevel@tonic-gate  *   hash table (pr_htbl) for the given pid in case that
6667c478bd9Sstevel@tonic-gate  *   pid exists on the hash chain. It returns back a pointer
6677c478bd9Sstevel@tonic-gate  *   to that uproc structure. If this is a new pid, it allocates
6687c478bd9Sstevel@tonic-gate  *   a new node, initializes it, links it into the chain (after
6697c478bd9Sstevel@tonic-gate  *   head) and returns a structure pointer.
6707c478bd9Sstevel@tonic-gate  */
6717c478bd9Sstevel@tonic-gate static struct uproc *
6727c478bd9Sstevel@tonic-gate findhash(pid_t pid)
6737c478bd9Sstevel@tonic-gate {
6747c478bd9Sstevel@tonic-gate 	struct uproc *up, *tp;
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate 	tp = up = &pr_htbl[pid % HSIZE];
6777c478bd9Sstevel@tonic-gate 	if (up->p_upid == 0) {			/* empty slot */
6787c478bd9Sstevel@tonic-gate 		up->p_upid = pid;
6797c478bd9Sstevel@tonic-gate 		up->p_state = NONE;
6807c478bd9Sstevel@tonic-gate 		up->p_child = up->p_sibling = up->p_pgrpl = up->p_link = 0;
6817c478bd9Sstevel@tonic-gate 		return (up);
6827c478bd9Sstevel@tonic-gate 	}
6837c478bd9Sstevel@tonic-gate 	if (up->p_upid == pid) {		/* found in hash table */
6847c478bd9Sstevel@tonic-gate 		return (up);
6857c478bd9Sstevel@tonic-gate 	}
6867c478bd9Sstevel@tonic-gate 	for (tp = up->p_link; tp; tp = tp->p_link) {	/* follow chain */
6877c478bd9Sstevel@tonic-gate 		if (tp->p_upid == pid)
6887c478bd9Sstevel@tonic-gate 			return (tp);
6897c478bd9Sstevel@tonic-gate 	}
6907c478bd9Sstevel@tonic-gate 	tp = malloc(sizeof (*tp));		/* add new node */
6916a79a301SJason King 	if (tp == NULL)
6926a79a301SJason King 		err(EXIT_FAILURE, gettext("out of memory!"));
6936a79a301SJason King 
6947c478bd9Sstevel@tonic-gate 	(void) memset(tp, 0, sizeof (*tp));
6957c478bd9Sstevel@tonic-gate 	tp->p_upid = pid;
6967c478bd9Sstevel@tonic-gate 	tp->p_state = NONE;
6977c478bd9Sstevel@tonic-gate 	tp->p_child = tp->p_sibling = tp->p_pgrpl = 0;
6987c478bd9Sstevel@tonic-gate 	tp->p_link = up->p_link;		/* insert after head */
6997c478bd9Sstevel@tonic-gate 	up->p_link = tp;
7007c478bd9Sstevel@tonic-gate 	return (tp);
7017c478bd9Sstevel@tonic-gate }
7027c478bd9Sstevel@tonic-gate 
7037c478bd9Sstevel@tonic-gate #define	HR	(60 * 60)
7047c478bd9Sstevel@tonic-gate #define	DAY	(24 * HR)
7057c478bd9Sstevel@tonic-gate #define	MON	(30 * DAY)
7067c478bd9Sstevel@tonic-gate 
7077c478bd9Sstevel@tonic-gate /*
708f0540631SGary Mills  * Prttime prints an elapsed time in hours, minutes, or seconds,
709f0540631SGary Mills  * right-justified with the rightmost column always blank.
710f0540631SGary Mills  * The second argument is the minimum field width.
7117c478bd9Sstevel@tonic-gate  */
7127c478bd9Sstevel@tonic-gate static void
713f0540631SGary Mills prttime(time_t tim, int width)
7147c478bd9Sstevel@tonic-gate {
715f0540631SGary Mills 	char value[36];
716f0540631SGary Mills 
717f0540631SGary Mills 	if (tim >= 36 * 60) {
718f0540631SGary Mills 		(void) snprintf(value, sizeof (value), "%d:%02d:%02d",
719f0540631SGary Mills 		    (int)tim / HR, (int)(tim % HR) / 60, (int)tim % 60);
720f0540631SGary Mills 	} else if (tim >= 60) {
721f0540631SGary Mills 		(void) snprintf(value, sizeof (value), "%d:%02d",
722f0540631SGary Mills 		    (int)tim / 60, (int)tim % 60);
7237c478bd9Sstevel@tonic-gate 	} else if (tim > 0) {
724f0540631SGary Mills 		(void) snprintf(value, sizeof (value), "%d", (int)tim);
7257c478bd9Sstevel@tonic-gate 	} else {
7266a79a301SJason King 		(void) strlcpy(value, "0", sizeof (value));
7277c478bd9Sstevel@tonic-gate 	}
728f0540631SGary Mills 	width = (width > 2) ? width - 1 : 1;
729f0540631SGary Mills 	PRINTF(("%*s ", width, value));
7307c478bd9Sstevel@tonic-gate }
7317c478bd9Sstevel@tonic-gate 
7327c478bd9Sstevel@tonic-gate /*
733f0540631SGary Mills  * Prints the ISO date or time given a pointer to a time of day,
734f0540631SGary Mills  * left-justfied in a 12-character expanding field with the
735f0540631SGary Mills  * rightmost column always blank.
736f0540631SGary Mills  * Includes a dcgettext() override in case a message catalog is needed.
7377c478bd9Sstevel@tonic-gate  */
7387c478bd9Sstevel@tonic-gate static void
7397c478bd9Sstevel@tonic-gate prtat(time_t *time)
7407c478bd9Sstevel@tonic-gate {
7417c478bd9Sstevel@tonic-gate 	struct tm	*p;
7427c478bd9Sstevel@tonic-gate 
7437c478bd9Sstevel@tonic-gate 	p = localtime(time);
7447c478bd9Sstevel@tonic-gate 	if (now - *time <= 18 * HR) {
7457c478bd9Sstevel@tonic-gate 		char timestr[50];
746f0540631SGary Mills 
7477c478bd9Sstevel@tonic-gate 		(void) strftime(timestr, sizeof (timestr),
748f0540631SGary Mills 		    dcgettext(NULL, "%T", LC_TIME), p);
749f0540631SGary Mills 		PRINTF(("%-11s ", timestr));
7507c478bd9Sstevel@tonic-gate 	} else if (now - *time <= 7 * DAY) {
7517c478bd9Sstevel@tonic-gate 		char weekdaytime[20];
7527c478bd9Sstevel@tonic-gate 
7537c478bd9Sstevel@tonic-gate 		(void) strftime(weekdaytime, sizeof (weekdaytime),
754f0540631SGary Mills 		    dcgettext(NULL, "%a %H:%M", LC_TIME), p);
755f0540631SGary Mills 		PRINTF(("%-11s ", weekdaytime));
7567c478bd9Sstevel@tonic-gate 	} else {
7577c478bd9Sstevel@tonic-gate 		char monthtime[20];
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate 		(void) strftime(monthtime, sizeof (monthtime),
760f0540631SGary Mills 		    dcgettext(NULL, "%F", LC_TIME), p);
761f0540631SGary Mills 		PRINTF(("%-11s ", monthtime));
7627c478bd9Sstevel@tonic-gate 	}
7637c478bd9Sstevel@tonic-gate }
7647c478bd9Sstevel@tonic-gate 
7657c478bd9Sstevel@tonic-gate /*
7667c478bd9Sstevel@tonic-gate  * find & return number of minutes current tty has been idle
7677c478bd9Sstevel@tonic-gate  */
7687c478bd9Sstevel@tonic-gate static time_t
7697c478bd9Sstevel@tonic-gate findidle(char *devname)
7707c478bd9Sstevel@tonic-gate {
7717c478bd9Sstevel@tonic-gate 	struct stat stbuf;
7727c478bd9Sstevel@tonic-gate 	time_t lastaction, diff;
7737c478bd9Sstevel@tonic-gate 	char ttyname[64];
7747c478bd9Sstevel@tonic-gate 
7756a79a301SJason King 	(void) strlcpy(ttyname, "/dev/", sizeof (ttyname));
7766a79a301SJason King 	(void) strlcat(ttyname, devname, sizeof (ttyname));
7777c478bd9Sstevel@tonic-gate 	if (stat(ttyname, &stbuf) != -1) {
7787c478bd9Sstevel@tonic-gate 		lastaction = stbuf.st_atime;
7797c478bd9Sstevel@tonic-gate 		diff = now - lastaction;
7807c478bd9Sstevel@tonic-gate 		diff = DIV60(diff);
7817c478bd9Sstevel@tonic-gate 		if (diff < 0)
7827c478bd9Sstevel@tonic-gate 			diff = 0;
7837c478bd9Sstevel@tonic-gate 	} else
7847c478bd9Sstevel@tonic-gate 		diff = 0;
7857c478bd9Sstevel@tonic-gate 	return (diff);
7867c478bd9Sstevel@tonic-gate }
7877c478bd9Sstevel@tonic-gate 
7887c478bd9Sstevel@tonic-gate /*
7897c478bd9Sstevel@tonic-gate  * given a pointer to the argument string get rid of unsavory characters.
7907c478bd9Sstevel@tonic-gate  */
7917c478bd9Sstevel@tonic-gate static void
7927c478bd9Sstevel@tonic-gate clnarglist(char *arglist)
7937c478bd9Sstevel@tonic-gate {
7947c478bd9Sstevel@tonic-gate 	char	*c;
795352d7aedSToomas Soome 	int	err = 0;
7967c478bd9Sstevel@tonic-gate 
7977c478bd9Sstevel@tonic-gate 	/* get rid of unsavory characters */
798352d7aedSToomas Soome 	for (c = arglist; *c != '\0'; c++) {
7997c478bd9Sstevel@tonic-gate 		if ((*c < ' ') || (*c > 0176)) {
8007c478bd9Sstevel@tonic-gate 			if (err++ > 5) {
801352d7aedSToomas Soome 				*arglist = '\0';
8027c478bd9Sstevel@tonic-gate 				break;
8037c478bd9Sstevel@tonic-gate 			}
8047c478bd9Sstevel@tonic-gate 			*c = '?';
8057c478bd9Sstevel@tonic-gate 		}
8067c478bd9Sstevel@tonic-gate 	}
8077c478bd9Sstevel@tonic-gate }
8086a79a301SJason King 
8096a79a301SJason King static int
8106a79a301SJason King priv_proc_open(const char *path, int oflag)
8116a79a301SJason King {
8126a79a301SJason King 	int fd, errsave = 0;
8136a79a301SJason King 
8146a79a301SJason King 	if (__priv_bracket(PRIV_ON) != 0)
8156a79a301SJason King 		err(EXIT_FAILURE, gettext("privilege bracketing failed"));
8166a79a301SJason King 
8176a79a301SJason King 	do {
8186a79a301SJason King 		fd = open(path, oflag);
8196a79a301SJason King 		if (fd < 0)
8206a79a301SJason King 			errsave = errno;
8216a79a301SJason King 	} while (fd < 0 && errno == EAGAIN);
8226a79a301SJason King 
8236a79a301SJason King 	if (__priv_bracket(PRIV_OFF) != 0)
8246a79a301SJason King 		err(EXIT_FAILURE, gettext("privilege bracketing failed"));
8256a79a301SJason King 
8266a79a301SJason King 	if (fd < 0)
8276a79a301SJason King 		errno = errsave;
8286a79a301SJason King 
8296a79a301SJason King 	return (fd);
8306a79a301SJason King }
8316a79a301SJason King 
8326a79a301SJason King static int
8336a79a301SJason King priv_proc_openat(int dfd, const char *path, int mode)
8346a79a301SJason King {
8356a79a301SJason King 	int fd, errsave = 0;
8366a79a301SJason King 
8376a79a301SJason King 	if (__priv_bracket(PRIV_ON) != 0)
8386a79a301SJason King 		err(EXIT_FAILURE, gettext("privilege bracketing failed"));
8396a79a301SJason King 
8406a79a301SJason King 	do {
8416a79a301SJason King 		fd = openat(dfd, path, mode);
8426a79a301SJason King 		if (fd < 0)
8436a79a301SJason King 			errsave = errno;
8446a79a301SJason King 	} while (fd < 0 && errno == EAGAIN);
8456a79a301SJason King 
8466a79a301SJason King 	if (__priv_bracket(PRIV_OFF) != 0)
8476a79a301SJason King 		err(EXIT_FAILURE, gettext("privilege bracketing failed"));
8486a79a301SJason King 
8496a79a301SJason King 	if (fd < 0)
8506a79a301SJason King 		errno = errsave;
8516a79a301SJason King 
8526a79a301SJason King 	return (fd);
8536a79a301SJason King }
8546a79a301SJason King 
8556a79a301SJason King static boolean_t
8566a79a301SJason King do_proc_read(int fd, void *buf, size_t bufsize)
8576a79a301SJason King {
8586a79a301SJason King 	ssize_t n;
8596a79a301SJason King 
8606a79a301SJason King 	do {
8616a79a301SJason King 		n = pread(fd, buf, bufsize, 0);
8626a79a301SJason King 		if (n == bufsize)
8636a79a301SJason King 			return (B_TRUE);
8646a79a301SJason King 		/*
8656a79a301SJason King 		 * Retry on a partial read or EAGAIN, otherwise fail
8666a79a301SJason King 		 */
8676a79a301SJason King 	} while (n >= 0 || errno == EAGAIN);
8686a79a301SJason King 
8696a79a301SJason King 	return (B_FALSE);
8706a79a301SJason King }
871