xref: /illumos-gate/usr/src/cmd/whodo/whodo.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 */
317c478bd9Sstevel@tonic-gate /*	  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 whodo 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  * Maintenance note:
497c478bd9Sstevel@tonic-gate  *
507c478bd9Sstevel@tonic-gate  * Much of this code is replicated in w.c.  If you're
517c478bd9Sstevel@tonic-gate  * fixing bugs here, then you should probably fix 'em there too.
527c478bd9Sstevel@tonic-gate  */
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate #include <stdio.h>
557c478bd9Sstevel@tonic-gate #include <string.h>
567c478bd9Sstevel@tonic-gate #include <stdlib.h>
577c478bd9Sstevel@tonic-gate #include <ctype.h>
587c478bd9Sstevel@tonic-gate #include <fcntl.h>
597c478bd9Sstevel@tonic-gate #include <time.h>
606a79a301SJason King #include <err.h>
617c478bd9Sstevel@tonic-gate #include <errno.h>
627c478bd9Sstevel@tonic-gate #include <sys/types.h>
637c478bd9Sstevel@tonic-gate #include <utmpx.h>
646a79a301SJason King #include <sys/sysmacros.h>
657c478bd9Sstevel@tonic-gate #include <sys/utsname.h>
667c478bd9Sstevel@tonic-gate #include <sys/stat.h>
677c478bd9Sstevel@tonic-gate #include <sys/mkdev.h>
687c478bd9Sstevel@tonic-gate #include <dirent.h>
697c478bd9Sstevel@tonic-gate #include <procfs.h>		/* /proc header file */
707c478bd9Sstevel@tonic-gate #include <sys/wait.h>
717c478bd9Sstevel@tonic-gate #include <locale.h>
727c478bd9Sstevel@tonic-gate #include <unistd.h>
737c478bd9Sstevel@tonic-gate #include <limits.h>
747c478bd9Sstevel@tonic-gate #include <priv_utils.h>
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate /*
770a1278f2SGary Mills  * Use the full lengths from utmpx for user and line.
787c478bd9Sstevel@tonic-gate  */
790a1278f2SGary Mills #define	NMAX	(sizeof (((struct utmpx *)0)->ut_user))
800a1278f2SGary Mills #define	LMAX	(sizeof (((struct utmpx *)0)->ut_line))
810a1278f2SGary Mills 
820a1278f2SGary Mills /* Print minimum field widths. */
830a1278f2SGary Mills #define	LOGIN_WIDTH	8
84f0540631SGary Mills #define	LINE_WIDTH	8
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate #define	DIV60(t)	((t+30)/60)    /* x/60 rounded */
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate #ifdef ERR
897c478bd9Sstevel@tonic-gate #undef ERR
907c478bd9Sstevel@tonic-gate #endif
917c478bd9Sstevel@tonic-gate #define	ERR		(-1)
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate #define	DEVNAMELEN	14
947c478bd9Sstevel@tonic-gate #define	HSIZE		256		/* size of process hash table */
957c478bd9Sstevel@tonic-gate #define	PROCDIR		"/proc"
967c478bd9Sstevel@tonic-gate #define	INITPROCESS	(pid_t)1	/* init process pid */
977c478bd9Sstevel@tonic-gate #define	NONE		'n'		/* no state */
987c478bd9Sstevel@tonic-gate #define	RUNNING		'r'		/* runnable process */
997c478bd9Sstevel@tonic-gate #define	ZOMBIE		'z'		/* zombie process */
1007c478bd9Sstevel@tonic-gate #define	VISITED		'v'		/* marked node as visited */
1017c478bd9Sstevel@tonic-gate 
1026a79a301SJason King static uint_t	ndevs;			/* number of configured devices */
1036a79a301SJason King static uint_t	maxdev;			/* slots for configured devices */
1047c478bd9Sstevel@tonic-gate #define	DNINCR	100
1057c478bd9Sstevel@tonic-gate static struct devl {			/* device list   */
1067c478bd9Sstevel@tonic-gate 	char	dname[DEVNAMELEN];	/* device name   */
1077c478bd9Sstevel@tonic-gate 	dev_t	ddev;			/* device number */
1087c478bd9Sstevel@tonic-gate } *devl;
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate struct uproc {
1117c478bd9Sstevel@tonic-gate 	pid_t	p_upid;			/* user process id */
1127c478bd9Sstevel@tonic-gate 	char	p_state;		/* numeric value of process state */
1137c478bd9Sstevel@tonic-gate 	dev_t	p_ttyd;			/* controlling tty of process */
1147c478bd9Sstevel@tonic-gate 	time_t	p_time;			/* ticks of user & system time */
1157c478bd9Sstevel@tonic-gate 	time_t	p_ctime;		/* ticks of child user & system time */
1167c478bd9Sstevel@tonic-gate 	int	p_igintr;		/* 1=ignores SIGQUIT and SIGINT */
1177c478bd9Sstevel@tonic-gate 	char	p_comm[PRARGSZ+1];	/* command */
1187c478bd9Sstevel@tonic-gate 	char	p_args[PRARGSZ+1];	/* command line arguments */
1197c478bd9Sstevel@tonic-gate 	struct uproc	*p_child,	/* first child pointer */
1207c478bd9Sstevel@tonic-gate 			*p_sibling,	/* sibling pointer */
1217c478bd9Sstevel@tonic-gate 			*p_pgrplink,	/* pgrp link */
1227c478bd9Sstevel@tonic-gate 			*p_link;	/* hash table chain pointer */
1237c478bd9Sstevel@tonic-gate };
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate /*
1267c478bd9Sstevel@tonic-gate  *	define	hash table for struct uproc
1277c478bd9Sstevel@tonic-gate  *	Hash function uses process id
1287c478bd9Sstevel@tonic-gate  *	and the size of the hash table(HSIZE)
1297c478bd9Sstevel@tonic-gate  *	to determine process index into the table.
1307c478bd9Sstevel@tonic-gate  */
1317c478bd9Sstevel@tonic-gate static struct uproc	pr_htbl[HSIZE];
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate static struct	uproc	*findhash(pid_t);
1347c478bd9Sstevel@tonic-gate static time_t	findidle(char *);
1357c478bd9Sstevel@tonic-gate static void	clnarglist(char *);
1367c478bd9Sstevel@tonic-gate static void	showproc(struct uproc *);
1377c478bd9Sstevel@tonic-gate static void	showtotals(struct uproc *);
1387c478bd9Sstevel@tonic-gate static void	calctotals(struct uproc *);
1397c478bd9Sstevel@tonic-gate static char	*getty(dev_t);
140f0540631SGary Mills static void	prttime(time_t, int);
1417c478bd9Sstevel@tonic-gate static void	prtat(time_t *);
1427c478bd9Sstevel@tonic-gate 
1436a79a301SJason King static int	priv_proc_open(const char *, int);
1446a79a301SJason King static int	priv_proc_openat(int, const char *, int);
1456a79a301SJason King static boolean_t do_proc_read(int, void *, size_t);
1466a79a301SJason King 
1477c478bd9Sstevel@tonic-gate static char	*prog;
1487c478bd9Sstevel@tonic-gate static int	header = 1;	/* true if -h flag: don't print heading */
1497c478bd9Sstevel@tonic-gate static int	lflag = 0;	/* true if -l flag: w command format */
15061aaa916SToomas Soome static char	*sel_user;	/* login of particular user selected */
1517c478bd9Sstevel@tonic-gate static time_t	now;		/* current time of day */
1527c478bd9Sstevel@tonic-gate static time_t	uptime;		/* time of last reboot & elapsed time since */
1537c478bd9Sstevel@tonic-gate static int	nusers;		/* number of users logged in now */
1547c478bd9Sstevel@tonic-gate static time_t	idle;		/* number of minutes user is idle */
1557c478bd9Sstevel@tonic-gate static time_t	jobtime;	/* total cpu time visible */
1567c478bd9Sstevel@tonic-gate static char	doing[520];	/* process attached to terminal */
1577c478bd9Sstevel@tonic-gate static time_t	proctime;	/* cpu time of process in doing */
1587c478bd9Sstevel@tonic-gate static int	empty;
1597c478bd9Sstevel@tonic-gate static pid_t	curpid;
1607c478bd9Sstevel@tonic-gate 
1616a79a301SJason King /*
1626a79a301SJason King  * Basic privs we never need and can drop. This is likely not exhaustive,
1636a79a301SJason King  * but should significantly reduce any potential attack surfaces.
1646a79a301SJason King  */
1656a79a301SJason King static const char *drop_privs[] = {
1666a79a301SJason King 	PRIV_FILE_WRITE,
1676a79a301SJason King 	PRIV_NET_ACCESS,
1686a79a301SJason King 	PRIV_PROC_EXEC,
1696a79a301SJason King 	PRIV_PROC_FORK,
1706a79a301SJason King 	PRIV_FILE_LINK_ANY
1716a79a301SJason King };
1726a79a301SJason King 
1737c478bd9Sstevel@tonic-gate #if SIGQUIT > SIGINT
1747c478bd9Sstevel@tonic-gate #define	ACTSIZE	SIGQUIT
1757c478bd9Sstevel@tonic-gate #else
1767c478bd9Sstevel@tonic-gate #define	ACTSIZE	SIGINT
1777c478bd9Sstevel@tonic-gate #endif
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate int
main(int argc,char * argv[])1807c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
1817c478bd9Sstevel@tonic-gate {
1827c478bd9Sstevel@tonic-gate 	struct utmpx	*ut;
1837c478bd9Sstevel@tonic-gate 	struct utmpx	*utmpbegin;
1847c478bd9Sstevel@tonic-gate 	struct utmpx	*utmpend;
18561aaa916SToomas Soome 	struct utmpx	*utp;
1867c478bd9Sstevel@tonic-gate 	struct tm		*tm;
1877c478bd9Sstevel@tonic-gate 	struct uproc	*up, *parent, *pgrp;
1887c478bd9Sstevel@tonic-gate 	struct psinfo	info;
1897c478bd9Sstevel@tonic-gate 	struct sigaction actinfo[ACTSIZE];
1907c478bd9Sstevel@tonic-gate 	struct pstatus	statinfo;
1917c478bd9Sstevel@tonic-gate 	struct stat	sbuf;
1927c478bd9Sstevel@tonic-gate 	struct utsname	uts;
1937c478bd9Sstevel@tonic-gate 	DIR		*dirp;
1947c478bd9Sstevel@tonic-gate 	struct	dirent	*dp;
1956a79a301SJason King 	char		pname[PATH_MAX];
1966a79a301SJason King 	int		procfd, dirfd;
1977c478bd9Sstevel@tonic-gate 	int		i;
1987c478bd9Sstevel@tonic-gate 	int		days, hrs, mins;
1997c478bd9Sstevel@tonic-gate 	int		entries;
2006a79a301SJason King 	priv_set_t	*pset;
2016a79a301SJason King 
2026a79a301SJason King 	if (__init_suid_priv(PU_CLEARLIMITSET, PRIV_PROC_OWNER, NULL) != 0) {
2036a79a301SJason King 		err(EXIT_FAILURE, "failed to enable privilege bracketing");
2046a79a301SJason King 	}
2056a79a301SJason King 
2066a79a301SJason King 	/*
2076a79a301SJason King 	 * After setting up privilege bracketing, we can further reduce the
2086a79a301SJason King 	 * privileges in use. The effective set is set to the basic set minus
2096a79a301SJason King 	 * the privs in drop_privs. The permitted set is the effective set
2106a79a301SJason King 	 * plus PRIV_PROC_OWNER (i.e. the privilege being bracketed).
2116a79a301SJason King 	 */
2126a79a301SJason King 	pset = priv_allocset();
2136a79a301SJason King 	if (pset == NULL)
2146a79a301SJason King 		err(EXIT_FAILURE, "priv_allocset failed");
2156a79a301SJason King 
2166a79a301SJason King 	priv_basicset(pset);
2176a79a301SJason King 	for (i = 0; i < ARRAY_SIZE(drop_privs); i++) {
2186a79a301SJason King 		if (priv_delset(pset, drop_privs[i]) != 0) {
2196a79a301SJason King 			err(EXIT_FAILURE,
2206a79a301SJason King 			    "failed to remove %s privilege from privilege set",
2216a79a301SJason King 			    drop_privs[i]);
2226a79a301SJason King 		}
2236a79a301SJason King 	}
2246a79a301SJason King 
2256a79a301SJason King 	if (setppriv(PRIV_SET, PRIV_EFFECTIVE, pset) < 0)
2266a79a301SJason King 		err(EXIT_FAILURE, "failed setting effective privilege set");
2276a79a301SJason King 
2286a79a301SJason King 	if (priv_addset(pset, PRIV_PROC_OWNER) != 0) {
2296a79a301SJason King 		err(EXIT_FAILURE,
2306a79a301SJason King 		    "failed to add PRIV_PROC_OWNER privilege to privilege set");
2316a79a301SJason King 	}
2326a79a301SJason King 
2336a79a301SJason King 	if (setppriv(PRIV_SET, PRIV_PERMITTED, pset) < 0)
2346a79a301SJason King 		err(EXIT_FAILURE, "failed to set permitted privilege set");
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 	/*
2376a79a301SJason King 	 * Unfortunately, when run as root, privilege bracketing is a no-op,
2386a79a301SJason King 	 * so we have to add PRIV_PROC_OWNER into our effective set for things
2396a79a301SJason King 	 * to work.
2407c478bd9Sstevel@tonic-gate 	 */
2416a79a301SJason King 	if (getuid() == 0 && setppriv(PRIV_SET, PRIV_EFFECTIVE, pset) < 0) {
2426a79a301SJason King 		err(EXIT_FAILURE, "failed to set effective privilege set");
2436a79a301SJason King 	}
2446a79a301SJason King 
2456a79a301SJason King 	priv_freeset(pset);
2466a79a301SJason King 	pset = NULL;
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
2497c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
2507c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
2517c478bd9Sstevel@tonic-gate #endif
2527c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
2537c478bd9Sstevel@tonic-gate 
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 
2697c478bd9Sstevel@tonic-gate 				default:
2707c478bd9Sstevel@tonic-gate 					(void) printf(gettext(
2717c478bd9Sstevel@tonic-gate 					    "usage: %s [ -hl ] [ user ]\n"),
2727c478bd9Sstevel@tonic-gate 					    prog);
2737c478bd9Sstevel@tonic-gate 					exit(1);
2747c478bd9Sstevel@tonic-gate 				}
2757c478bd9Sstevel@tonic-gate 			}
2767c478bd9Sstevel@tonic-gate 		} else {
2777c478bd9Sstevel@tonic-gate 			if (!isalnum(argv[1][0]) || argc > 2) {
2787c478bd9Sstevel@tonic-gate 				(void) printf(gettext(
2797c478bd9Sstevel@tonic-gate 				    "usage: %s [ -hl ] [ user ]\n"), prog);
2807c478bd9Sstevel@tonic-gate 				exit(1);
2817c478bd9Sstevel@tonic-gate 			} else
2827c478bd9Sstevel@tonic-gate 				sel_user = argv[1];
2837c478bd9Sstevel@tonic-gate 		}
2847c478bd9Sstevel@tonic-gate 		argc--; argv++;
2857c478bd9Sstevel@tonic-gate 	}
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 	/*
2887c478bd9Sstevel@tonic-gate 	 * read the UTMPX_FILE (contains information about
2897c478bd9Sstevel@tonic-gate 	 * each logged in user)
2907c478bd9Sstevel@tonic-gate 	 */
2916a79a301SJason King 	if (stat(UTMPX_FILE, &sbuf) < 0)
2926a79a301SJason King 		err(EXIT_FAILURE, gettext("stat error of %s"), UTMPX_FILE);
2936a79a301SJason King 
2947c478bd9Sstevel@tonic-gate 	entries = sbuf.st_size / sizeof (struct futmpx);
2957c478bd9Sstevel@tonic-gate 
2966a79a301SJason King 	if ((ut = calloc(entries, sizeof (struct utmpx))) == NULL)
2976a79a301SJason King 		err(EXIT_FAILURE, gettext("calloc error of %s"), UTMPX_FILE);
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 	(void) utmpxname(UTMPX_FILE);
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 	utmpbegin = ut;
3026a79a301SJason King 	utmpend = utmpbegin + entries;
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 	setutxent();
30576e222fdSSumanth Naropanth 	while ((ut < utmpend) && ((utp = getutxent()) != NULL))
3067c478bd9Sstevel@tonic-gate 		(void) memcpy(ut++, utp, sizeof (*ut));
3077c478bd9Sstevel@tonic-gate 	endutxent();
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 	(void) time(&now);	/* get current time */
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 	if (header) {	/* print a header */
3127c478bd9Sstevel@tonic-gate 		if (lflag) {	/* w command format header */
3137c478bd9Sstevel@tonic-gate 			prtat(&now);
3147c478bd9Sstevel@tonic-gate 			for (ut = utmpbegin; ut < utmpend; ut++) {
3157c478bd9Sstevel@tonic-gate 				if (ut->ut_type == USER_PROCESS) {
3167c478bd9Sstevel@tonic-gate 					nusers++;
3177c478bd9Sstevel@tonic-gate 				} else if (ut->ut_type == BOOT_TIME) {
3187c478bd9Sstevel@tonic-gate 					uptime = now - ut->ut_xtime;
3197c478bd9Sstevel@tonic-gate 					uptime += 30;
3207c478bd9Sstevel@tonic-gate 					days = uptime / (60*60*24);
3217c478bd9Sstevel@tonic-gate 					uptime %= (60*60*24);
3227c478bd9Sstevel@tonic-gate 					hrs = uptime / (60*60);
3237c478bd9Sstevel@tonic-gate 					uptime %= (60*60);
3247c478bd9Sstevel@tonic-gate 					mins = uptime / 60;
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 					(void) printf(dcgettext(NULL,
327f0540631SGary Mills 					    "up %d day(s), %d hr(s), "
3287c478bd9Sstevel@tonic-gate 					    "%d min(s)", LC_TIME),
3297c478bd9Sstevel@tonic-gate 					    days, hrs, mins);
3307c478bd9Sstevel@tonic-gate 				}
3317c478bd9Sstevel@tonic-gate 			}
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate 			ut = utmpbegin; /* rewind utmp data */
3347c478bd9Sstevel@tonic-gate 			(void) printf(dcgettext(NULL,
3357c478bd9Sstevel@tonic-gate 			    "  %d user(s)\n", LC_TIME), nusers);
336f0540631SGary Mills 			(void) printf(dcgettext(NULL, "User     tty      "
337f0540631SGary Mills 			    "login@         idle    JCPU    PCPU what\n",
338f0540631SGary Mills 			    LC_TIME));
3397c478bd9Sstevel@tonic-gate 		} else {	/* standard whodo header */
3407c478bd9Sstevel@tonic-gate 			char date_buf[100];
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 			/*
3437c478bd9Sstevel@tonic-gate 			 * print current time and date
3447c478bd9Sstevel@tonic-gate 			 */
3457c478bd9Sstevel@tonic-gate 			(void) strftime(date_buf, sizeof (date_buf),
346f0540631SGary Mills 			    "%c", localtime(&now));
3477c478bd9Sstevel@tonic-gate 			(void) printf("%s\n", date_buf);
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 			/*
3507c478bd9Sstevel@tonic-gate 			 * print system name
3517c478bd9Sstevel@tonic-gate 			 */
3527c478bd9Sstevel@tonic-gate 			(void) uname(&uts);
3537c478bd9Sstevel@tonic-gate 			(void) printf("%s\n", uts.nodename);
3547c478bd9Sstevel@tonic-gate 		}
3557c478bd9Sstevel@tonic-gate 	}
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 	/*
3587c478bd9Sstevel@tonic-gate 	 * loop through /proc, reading info about each process
3597c478bd9Sstevel@tonic-gate 	 * and build the parent/child tree
3607c478bd9Sstevel@tonic-gate 	 */
3616a79a301SJason King 	if ((dirp = opendir(PROCDIR)) == NULL)
3626a79a301SJason King 		err(EXIT_FAILURE, gettext("could not open %s"), PROCDIR);
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 	while ((dp = readdir(dirp)) != NULL) {
3657c478bd9Sstevel@tonic-gate 		if (dp->d_name[0] == '.')
3667c478bd9Sstevel@tonic-gate 			continue;
3676a79a301SJason King 
3686a79a301SJason King 		if (snprintf(pname, sizeof (pname), "%s/%s", PROCDIR,
3696a79a301SJason King 		    dp->d_name) > sizeof (pname))
3707c478bd9Sstevel@tonic-gate 			continue;
3716a79a301SJason King 
3726a79a301SJason King 		dirfd = priv_proc_open(pname, O_RDONLY | O_DIRECTORY);
3736a79a301SJason King 
374*45de8795SJason King 		if (dirfd < 0)
3756a79a301SJason King 			continue;
3766a79a301SJason King 
3776a79a301SJason King 		procfd = priv_proc_openat(dirfd, "psinfo", O_RDONLY);
3786a79a301SJason King 		if (procfd < 0) {
3796a79a301SJason King 			(void) close(dirfd);
3806a79a301SJason King 			continue;
3816a79a301SJason King 		}
3826a79a301SJason King 
3836a79a301SJason King 		if (!do_proc_read(procfd, &info, sizeof (info))) {
3846a79a301SJason King 			warn(gettext("read() failed on %s"), pname);
3856a79a301SJason King 			(void) close(dirfd);
3867c478bd9Sstevel@tonic-gate 			continue;
3877c478bd9Sstevel@tonic-gate 		}
3887c478bd9Sstevel@tonic-gate 		(void) close(procfd);
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 		up = findhash(info.pr_pid);
3917c478bd9Sstevel@tonic-gate 		up->p_ttyd = info.pr_ttydev;
3927c478bd9Sstevel@tonic-gate 		up->p_state = (info.pr_nlwp == 0? ZOMBIE : RUNNING);
3937c478bd9Sstevel@tonic-gate 		up->p_time = 0;
3947c478bd9Sstevel@tonic-gate 		up->p_ctime = 0;
3957c478bd9Sstevel@tonic-gate 		up->p_igintr = 0;
3966a79a301SJason King 		(void) strlcpy(up->p_comm, info.pr_fname,
3976a79a301SJason King 		    sizeof (up->p_comm));
3987c478bd9Sstevel@tonic-gate 		up->p_args[0] = 0;
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 		if (up->p_state != NONE && up->p_state != ZOMBIE) {
4016a79a301SJason King 			procfd = priv_proc_openat(dirfd, "status", O_RDONLY);
4026a79a301SJason King 			if (procfd < 0) {
4036a79a301SJason King 				(void) close(dirfd);
4047c478bd9Sstevel@tonic-gate 				continue;
4056a79a301SJason King 			}
4066a79a301SJason King 
4076a79a301SJason King 			if (!do_proc_read(procfd, &statinfo,
4086a79a301SJason King 			    sizeof (statinfo))) {
4096a79a301SJason King 				warn(gettext("read() failed on %s/status"),
4106a79a301SJason King 				    pname);
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 				(void) close(procfd);
4136a79a301SJason King 				(void) close(dirfd);
4147c478bd9Sstevel@tonic-gate 				continue;
4157c478bd9Sstevel@tonic-gate 			}
4167c478bd9Sstevel@tonic-gate 			(void) close(procfd);
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 			up->p_time = statinfo.pr_utime.tv_sec +
4197c478bd9Sstevel@tonic-gate 			    statinfo.pr_stime.tv_sec;
4207c478bd9Sstevel@tonic-gate 			up->p_ctime = statinfo.pr_cutime.tv_sec +
4217c478bd9Sstevel@tonic-gate 			    statinfo.pr_cstime.tv_sec;
4227c478bd9Sstevel@tonic-gate 
4236a79a301SJason King 			procfd = priv_proc_openat(dirfd, "sigact", O_RDONLY);
4246a79a301SJason King 			if (procfd < 0) {
4256a79a301SJason King 				(void) close(dirfd);
4266a79a301SJason King 				continue;
4276a79a301SJason King 			}
4287c478bd9Sstevel@tonic-gate 
4296a79a301SJason King 			if (!do_proc_read(procfd, actinfo, sizeof (actinfo))) {
4306a79a301SJason King 				warn(gettext("read() failed on %s/sigact"),
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);
4386a79a301SJason King 			(void) close(dirfd);
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate 			up->p_igintr =
44176e222fdSSumanth Naropanth 			    actinfo[SIGINT-1].sa_handler == SIG_IGN &&
44276e222fdSSumanth Naropanth 			    actinfo[SIGQUIT-1].sa_handler == SIG_IGN;
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate 			up->p_args[0] = 0;
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 			/*
4477c478bd9Sstevel@tonic-gate 			 * Process args if there's a chance we'll print it.
4487c478bd9Sstevel@tonic-gate 			 */
4497c478bd9Sstevel@tonic-gate 			if (lflag) { /* w command needs args */
4507c478bd9Sstevel@tonic-gate 				clnarglist(info.pr_psargs);
4516a79a301SJason King 				(void) strlcpy(up->p_args, info.pr_psargs,
4526a79a301SJason King 				    sizeof (up->p_args));
4537c478bd9Sstevel@tonic-gate 				if (up->p_args[0] == 0 ||
4547c478bd9Sstevel@tonic-gate 				    up->p_args[0] == '-' &&
4557c478bd9Sstevel@tonic-gate 				    up->p_args[1] <= ' ' ||
4567c478bd9Sstevel@tonic-gate 				    up->p_args[0] == '?') {
4576a79a301SJason King 					(void) strlcat(up->p_args, " (",
4586a79a301SJason King 					    sizeof (up->p_args));
4596a79a301SJason King 					(void) strlcat(up->p_args, up->p_comm,
4606a79a301SJason King 					    sizeof (up->p_args));
4616a79a301SJason King 					(void) strlcat(up->p_args, ")",
4626a79a301SJason King 					    sizeof (up->p_args));
4637c478bd9Sstevel@tonic-gate 				}
4647c478bd9Sstevel@tonic-gate 			}
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 		}
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate 		/*
4697c478bd9Sstevel@tonic-gate 		 * link pgrp together in case parents go away
4707c478bd9Sstevel@tonic-gate 		 * Pgrp chain is a single linked list originating
4717c478bd9Sstevel@tonic-gate 		 * from the pgrp leader to its group member.
4727c478bd9Sstevel@tonic-gate 		 */
4737c478bd9Sstevel@tonic-gate 		if (info.pr_pgid != info.pr_pid) {	/* not pgrp leader */
4747c478bd9Sstevel@tonic-gate 			pgrp = findhash(info.pr_pgid);
4757c478bd9Sstevel@tonic-gate 			up->p_pgrplink = pgrp->p_pgrplink;
4767c478bd9Sstevel@tonic-gate 			pgrp->p_pgrplink = up;
4777c478bd9Sstevel@tonic-gate 		}
4787c478bd9Sstevel@tonic-gate 		parent = findhash(info.pr_ppid);
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate 		/* if this is the new member, link it in */
4817c478bd9Sstevel@tonic-gate 		if (parent->p_upid != INITPROCESS) {
4827c478bd9Sstevel@tonic-gate 			if (parent->p_child) {
4837c478bd9Sstevel@tonic-gate 				up->p_sibling = parent->p_child;
4847c478bd9Sstevel@tonic-gate 				up->p_child = 0;
4857c478bd9Sstevel@tonic-gate 			}
4867c478bd9Sstevel@tonic-gate 			parent->p_child = up;
4877c478bd9Sstevel@tonic-gate 		}
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate 	}
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate 	/* revert to non-privileged user */
4926a79a301SJason King 	__priv_relinquish();
4936a79a301SJason King 	if (getuid() == 0) {
4946a79a301SJason King 		/*
4956a79a301SJason King 		 * Since the privilege bracketing functions are effectively
4966a79a301SJason King 		 * no-ops when running as root, we must explicitly
4976a79a301SJason King 		 * relinquish PRIV_PROC_OWNER ourselves.
4986a79a301SJason King 		 */
4996a79a301SJason King 		pset = priv_allocset();
5006a79a301SJason King 		if (pset == NULL) {
5016a79a301SJason King 			err(EXIT_FAILURE,
5026a79a301SJason King 			    gettext("failed to allocate privilege set"));
5036a79a301SJason King 		}
5046a79a301SJason King 
5056a79a301SJason King 		priv_emptyset(pset);
5066a79a301SJason King 
5076a79a301SJason King 		if (priv_addset(pset, PRIV_PROC_OWNER) != 0) {
5086a79a301SJason King 			err(EXIT_FAILURE, gettext("failed to add "
5096a79a301SJason King 			    "PRIV_PROC_OWNER to privilege set"));
5106a79a301SJason King 		}
5116a79a301SJason King 
5126a79a301SJason King 		if (setppriv(PRIV_OFF, PRIV_PERMITTED, pset) != 0) {
5136a79a301SJason King 			err(EXIT_FAILURE,
5146a79a301SJason King 			    gettext("failed to set permitted privilege set"));
5156a79a301SJason King 		}
5166a79a301SJason King 
5176a79a301SJason King 		priv_freeset(pset);
5186a79a301SJason King 		pset = NULL;
5196a79a301SJason King 	}
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 	(void) closedir(dirp);
5227c478bd9Sstevel@tonic-gate 	(void) time(&now);	/* get current time */
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate 	/*
5257c478bd9Sstevel@tonic-gate 	 * loop through utmpx file, printing process info
5267c478bd9Sstevel@tonic-gate 	 * about each logged in user
5277c478bd9Sstevel@tonic-gate 	 */
5287c478bd9Sstevel@tonic-gate 	for (ut = utmpbegin; ut < utmpend; ut++) {
5297c478bd9Sstevel@tonic-gate 		time_t tim;
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate 		if (ut->ut_type != USER_PROCESS)
5327c478bd9Sstevel@tonic-gate 			continue;
5337c478bd9Sstevel@tonic-gate 		if (sel_user && strncmp(ut->ut_name, sel_user, NMAX) != 0)
5347c478bd9Sstevel@tonic-gate 			continue;	/* we're looking for somebody else */
5357c478bd9Sstevel@tonic-gate 		if (lflag) {	/* -l flag format (w command) */
5367c478bd9Sstevel@tonic-gate 			/* print login name of the user */
5370a1278f2SGary Mills 			(void) printf("%-*.*s ", LOGIN_WIDTH, (int)NMAX,
5380a1278f2SGary Mills 			    ut->ut_name);
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate 			/* print tty user is on */
541f0540631SGary Mills 			(void) printf("%-*.*s ", LINE_WIDTH, (int)LMAX,
5420a1278f2SGary Mills 			    ut->ut_line);
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate 			/* print when the user logged in */
5457c478bd9Sstevel@tonic-gate 			tim = ut->ut_xtime;
5467c478bd9Sstevel@tonic-gate 			(void) prtat(&tim);
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate 			/* print idle time */
5497c478bd9Sstevel@tonic-gate 			idle = findidle(ut->ut_line);
550f0540631SGary Mills 			prttime(idle, 8);
5517c478bd9Sstevel@tonic-gate 			showtotals(findhash((pid_t)ut->ut_pid));
5527c478bd9Sstevel@tonic-gate 		} else {	/* standard whodo format */
5537c478bd9Sstevel@tonic-gate 			tim = ut->ut_xtime;
5547c478bd9Sstevel@tonic-gate 			tm = localtime(&tim);
5557c478bd9Sstevel@tonic-gate 			(void) printf("\n%-*.*s %-*.*s %2.1d:%2.2d\n",
5560a1278f2SGary Mills 			    LINE_WIDTH, (int)LMAX, ut->ut_line,
5570a1278f2SGary Mills 			    LOGIN_WIDTH, (int)NMAX, ut->ut_name, tm->tm_hour,
5580a1278f2SGary Mills 			    tm->tm_min);
5597c478bd9Sstevel@tonic-gate 			showproc(findhash((pid_t)ut->ut_pid));
5607c478bd9Sstevel@tonic-gate 		}
5617c478bd9Sstevel@tonic-gate 	}
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate 	return (0);
5647c478bd9Sstevel@tonic-gate }
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate /*
5677c478bd9Sstevel@tonic-gate  * Used for standard whodo format.
5687c478bd9Sstevel@tonic-gate  * This is the recursive routine descending the process
5697c478bd9Sstevel@tonic-gate  * tree starting from the given process pointer(up).
5707c478bd9Sstevel@tonic-gate  * It used depth-first search strategy and also marked
5717c478bd9Sstevel@tonic-gate  * each node as printed as it traversed down the tree.
5727c478bd9Sstevel@tonic-gate  */
5737c478bd9Sstevel@tonic-gate static void
showproc(struct uproc * up)5747c478bd9Sstevel@tonic-gate showproc(struct uproc *up)
5757c478bd9Sstevel@tonic-gate {
5767c478bd9Sstevel@tonic-gate 	struct	uproc	*zp;
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate 	if (up->p_state == VISITED) /* we already been here */
5797c478bd9Sstevel@tonic-gate 		return;
5807c478bd9Sstevel@tonic-gate 	/* print the data for this process */
5817c478bd9Sstevel@tonic-gate 	if (up->p_state == ZOMBIE)
5827c478bd9Sstevel@tonic-gate 		(void) printf("    %-*.*s %5d %4.1ld:%2.2ld %s\n",
5830a1278f2SGary Mills 		    LINE_WIDTH, (int)LMAX, "  ?", (int)up->p_upid, 0L, 0L,
5840a1278f2SGary Mills 		    "<defunct>");
5857c478bd9Sstevel@tonic-gate 	else if (up->p_state != NONE) {
5867c478bd9Sstevel@tonic-gate 		(void) printf("    %-*.*s %5d %4.1ld:%2.2ld %s\n",
5870a1278f2SGary Mills 		    LINE_WIDTH, (int)LMAX, getty(up->p_ttyd), (int)up->p_upid,
5887c478bd9Sstevel@tonic-gate 		    up->p_time / 60L, up->p_time % 60L,
5897c478bd9Sstevel@tonic-gate 		    up->p_comm);
5907c478bd9Sstevel@tonic-gate 	}
5917c478bd9Sstevel@tonic-gate 	up->p_state = VISITED;
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate 	/* descend for its children */
5947c478bd9Sstevel@tonic-gate 	if (up->p_child) {
5957c478bd9Sstevel@tonic-gate 		showproc(up->p_child);
5967c478bd9Sstevel@tonic-gate 		for (zp = up->p_child->p_sibling; zp; zp = zp->p_sibling) {
5977c478bd9Sstevel@tonic-gate 			showproc(zp);
5987c478bd9Sstevel@tonic-gate 		}
5997c478bd9Sstevel@tonic-gate 	}
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate 	/* print the pgrp relation */
6027c478bd9Sstevel@tonic-gate 	if (up->p_pgrplink)
6037c478bd9Sstevel@tonic-gate 		showproc(up->p_pgrplink);
6047c478bd9Sstevel@tonic-gate }
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate /*
6087c478bd9Sstevel@tonic-gate  * Used for -l flag (w command) format.
6097c478bd9Sstevel@tonic-gate  * Prints the CPU time for all processes & children,
6107c478bd9Sstevel@tonic-gate  * and the cpu time for interesting process,
6117c478bd9Sstevel@tonic-gate  * and what the user is doing.
6127c478bd9Sstevel@tonic-gate  */
6137c478bd9Sstevel@tonic-gate static void
showtotals(struct uproc * up)6147c478bd9Sstevel@tonic-gate showtotals(struct uproc *up)
6157c478bd9Sstevel@tonic-gate {
6167c478bd9Sstevel@tonic-gate 	jobtime = 0;
6177c478bd9Sstevel@tonic-gate 	proctime = 0;
6187c478bd9Sstevel@tonic-gate 	empty = 1;
6197c478bd9Sstevel@tonic-gate 	curpid = -1;
6206a79a301SJason King 
6216a79a301SJason King 	/* default act: normally never prints */
6226a79a301SJason King 	(void) strlcpy(doing, "-", sizeof (doing));
6237c478bd9Sstevel@tonic-gate 	calctotals(up);
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate 	/* print CPU time for all processes & children */
6267c478bd9Sstevel@tonic-gate 	/* and need to convert clock ticks to seconds first */
627f0540631SGary Mills 	prttime((time_t)jobtime, 8);
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate 	/* print cpu time for interesting process */
6307c478bd9Sstevel@tonic-gate 	/* and need to convert clock ticks to seconds first */
631f0540631SGary Mills 	prttime((time_t)proctime, 8);
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate 	/* what user is doing, current process */
634f0540631SGary Mills 	(void) printf("%-.32s\n", doing);
6357c478bd9Sstevel@tonic-gate }
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate /*
6387c478bd9Sstevel@tonic-gate  *  Used for -l flag (w command) format.
6397c478bd9Sstevel@tonic-gate  *  This recursive routine descends the process
6407c478bd9Sstevel@tonic-gate  *  tree starting from the given process pointer(up).
6417c478bd9Sstevel@tonic-gate  *  It used depth-first search strategy and also marked
6427c478bd9Sstevel@tonic-gate  *  each node as visited as it traversed down the tree.
6437c478bd9Sstevel@tonic-gate  *  It calculates the process time for all processes &
6447c478bd9Sstevel@tonic-gate  *  children.  It also finds the "interesting" process
6457c478bd9Sstevel@tonic-gate  *  and determines its cpu time and command.
6467c478bd9Sstevel@tonic-gate  */
6477c478bd9Sstevel@tonic-gate static void
calctotals(struct uproc * up)6487c478bd9Sstevel@tonic-gate calctotals(struct uproc *up)
6497c478bd9Sstevel@tonic-gate {
6507c478bd9Sstevel@tonic-gate 	struct uproc	*zp;
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate 	if (up->p_state == VISITED)
6537c478bd9Sstevel@tonic-gate 		return;
6547c478bd9Sstevel@tonic-gate 	up->p_state = VISITED;
6557c478bd9Sstevel@tonic-gate 	if (up->p_state == NONE || up->p_state == ZOMBIE)
6567c478bd9Sstevel@tonic-gate 		return;
6577c478bd9Sstevel@tonic-gate 	jobtime += up->p_time + up->p_ctime;
6587c478bd9Sstevel@tonic-gate 	proctime += up->p_time;
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 	if (empty && !up->p_igintr) {
6617c478bd9Sstevel@tonic-gate 		empty = 0;
6627c478bd9Sstevel@tonic-gate 		curpid = -1;
6637c478bd9Sstevel@tonic-gate 	}
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate 	if (up->p_upid > curpid && (!up->p_igintr || empty)) {
6667c478bd9Sstevel@tonic-gate 		curpid = up->p_upid;
6676a79a301SJason King 		(void) strlcpy(doing, up->p_args, sizeof (doing));
6687c478bd9Sstevel@tonic-gate 	}
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate 	/* descend for its children */
6717c478bd9Sstevel@tonic-gate 	if (up->p_child) {
6727c478bd9Sstevel@tonic-gate 		calctotals(up->p_child);
6737c478bd9Sstevel@tonic-gate 		for (zp = up->p_child->p_sibling; zp; zp = zp->p_sibling)
6747c478bd9Sstevel@tonic-gate 			calctotals(zp);
6757c478bd9Sstevel@tonic-gate 	}
6767c478bd9Sstevel@tonic-gate }
6777c478bd9Sstevel@tonic-gate 
6787c478bd9Sstevel@tonic-gate static char *
devadd(const char * name,dev_t ddev)6796a79a301SJason King devadd(const char *name, dev_t ddev)
6807c478bd9Sstevel@tonic-gate {
6817c478bd9Sstevel@tonic-gate 	struct devl *dp;
6827c478bd9Sstevel@tonic-gate 	int leng, start, i;
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate 	if (ndevs == maxdev) {
6856a79a301SJason King 		uint_t newdev;
6866a79a301SJason King 
6876a79a301SJason King 		newdev = maxdev + DNINCR;
6886a79a301SJason King 		if (newdev < DNINCR)
6896a79a301SJason King 			errx(EXIT_FAILURE, gettext("devadd overflow"));
6906a79a301SJason King 
6916a79a301SJason King 		dp = recallocarray(devl, maxdev, newdev, sizeof (struct devl));
6926a79a301SJason King 		if (dp == NULL)
6936a79a301SJason King 			err(EXIT_FAILURE, gettext("out of memory!"));
6946a79a301SJason King 		maxdev = newdev;
6957c478bd9Sstevel@tonic-gate 		devl = dp;
6967c478bd9Sstevel@tonic-gate 	}
6977c478bd9Sstevel@tonic-gate 	dp = &devl[ndevs++];
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate 	dp->ddev = ddev;
7007c478bd9Sstevel@tonic-gate 	if (name == NULL) {
7016a79a301SJason King 		(void) strlcpy(dp->dname, "  ?  ", sizeof (dp->dname));
7027c478bd9Sstevel@tonic-gate 		return (dp->dname);
7037c478bd9Sstevel@tonic-gate 	}
7047c478bd9Sstevel@tonic-gate 
7057c478bd9Sstevel@tonic-gate 	leng = strlen(name);
7067c478bd9Sstevel@tonic-gate 	if (leng < DEVNAMELEN + 4) {
7077c478bd9Sstevel@tonic-gate 		/* strip off "/dev/" */
7086a79a301SJason King 		(void) strlcpy(dp->dname, &name[5], sizeof (dp->dname));
7097c478bd9Sstevel@tonic-gate 	} else {
7107c478bd9Sstevel@tonic-gate 		/* strip enough off the front to fit */
7117c478bd9Sstevel@tonic-gate 		start = leng - DEVNAMELEN - 1;
7127c478bd9Sstevel@tonic-gate 
7137c478bd9Sstevel@tonic-gate 		for (i = start; i < leng && name[i] != '/'; i++)
7147c478bd9Sstevel@tonic-gate 				;
7157c478bd9Sstevel@tonic-gate 		if (i == leng)
7166a79a301SJason King 			(void) strlcpy(dp->dname, &name[start], DEVNAMELEN);
7177c478bd9Sstevel@tonic-gate 		else
7186a79a301SJason King 			(void) strlcpy(dp->dname, &name[i+1], DEVNAMELEN);
7197c478bd9Sstevel@tonic-gate 	}
7207c478bd9Sstevel@tonic-gate 	return (dp->dname);
7217c478bd9Sstevel@tonic-gate }
7227c478bd9Sstevel@tonic-gate 
7237c478bd9Sstevel@tonic-gate static char *
devlookup(dev_t ddev)7247c478bd9Sstevel@tonic-gate devlookup(dev_t ddev)
7257c478bd9Sstevel@tonic-gate {
7267c478bd9Sstevel@tonic-gate 	struct devl *dp;
7277c478bd9Sstevel@tonic-gate 	int i;
7287c478bd9Sstevel@tonic-gate 
7297c478bd9Sstevel@tonic-gate 	for (dp = devl, i = 0; i < ndevs; dp++, i++) {
7307c478bd9Sstevel@tonic-gate 		if (dp->ddev == ddev)
7317c478bd9Sstevel@tonic-gate 			return (dp->dname);
7327c478bd9Sstevel@tonic-gate 	}
7337c478bd9Sstevel@tonic-gate 	return (NULL);
7347c478bd9Sstevel@tonic-gate }
7357c478bd9Sstevel@tonic-gate 
7367c478bd9Sstevel@tonic-gate /*
7377c478bd9Sstevel@tonic-gate  * This routine gives back a corresponding device name
7387c478bd9Sstevel@tonic-gate  * from the device number given.
7397c478bd9Sstevel@tonic-gate  */
7407c478bd9Sstevel@tonic-gate static char *
getty(dev_t dev)7417c478bd9Sstevel@tonic-gate getty(dev_t dev)
7427c478bd9Sstevel@tonic-gate {
7437c478bd9Sstevel@tonic-gate 	extern char *_ttyname_dev(dev_t, char *, size_t);
7447c478bd9Sstevel@tonic-gate 	char devname[TTYNAME_MAX];
7457c478bd9Sstevel@tonic-gate 	char *retval;
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate 	if (dev == PRNODEV)
7487c478bd9Sstevel@tonic-gate 		return ("  ?  ");
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate 	if ((retval = devlookup(dev)) != NULL)
7517c478bd9Sstevel@tonic-gate 		return (retval);
7527c478bd9Sstevel@tonic-gate 
7537c478bd9Sstevel@tonic-gate 	retval = _ttyname_dev(dev, devname, sizeof (devname));
7547c478bd9Sstevel@tonic-gate 	return (devadd(retval, dev));
7557c478bd9Sstevel@tonic-gate }
7567c478bd9Sstevel@tonic-gate 
7577c478bd9Sstevel@tonic-gate /*
7587c478bd9Sstevel@tonic-gate  * Findhash  finds the appropriate entry in the process
7597c478bd9Sstevel@tonic-gate  * hash table (pr_htbl) for the given pid in case that
7607c478bd9Sstevel@tonic-gate  * pid exists on the hash chain. It returns back a pointer
7617c478bd9Sstevel@tonic-gate  * to that uproc structure. If this is a new pid, it allocates
7627c478bd9Sstevel@tonic-gate  * a new node, initializes it, links it into the chain (after
7637c478bd9Sstevel@tonic-gate  * head) and returns a structure pointer.
7647c478bd9Sstevel@tonic-gate  */
7657c478bd9Sstevel@tonic-gate static struct uproc *
findhash(pid_t pid)7667c478bd9Sstevel@tonic-gate findhash(pid_t pid)
7677c478bd9Sstevel@tonic-gate {
7687c478bd9Sstevel@tonic-gate 	struct uproc *up, *tp;
7697c478bd9Sstevel@tonic-gate 
7707c478bd9Sstevel@tonic-gate 	tp = up = &pr_htbl[(int)pid % HSIZE];
7717c478bd9Sstevel@tonic-gate 	if (up->p_upid == 0) {			/* empty slot */
7727c478bd9Sstevel@tonic-gate 		up->p_upid = pid;
7737c478bd9Sstevel@tonic-gate 		up->p_state = NONE;
7747c478bd9Sstevel@tonic-gate 		up->p_child = up->p_sibling = up->p_pgrplink = up->p_link = 0;
7757c478bd9Sstevel@tonic-gate 		return (up);
7767c478bd9Sstevel@tonic-gate 	}
7777c478bd9Sstevel@tonic-gate 	if (up->p_upid == pid) {		/* found in hash table */
7787c478bd9Sstevel@tonic-gate 		return (up);
7797c478bd9Sstevel@tonic-gate 	}
7807c478bd9Sstevel@tonic-gate 	for (tp = up->p_link; tp; tp = tp->p_link) {	/* follow chain */
7817c478bd9Sstevel@tonic-gate 		if (tp->p_upid == pid) {
7827c478bd9Sstevel@tonic-gate 			return (tp);
7837c478bd9Sstevel@tonic-gate 		}
7847c478bd9Sstevel@tonic-gate 	}
7857c478bd9Sstevel@tonic-gate 	tp = malloc(sizeof (*tp));		/* add new node */
7866a79a301SJason King 	if (tp == NULL)
7876a79a301SJason King 		err(EXIT_FAILURE, gettext("out of memory!"));
7886a79a301SJason King 
7897c478bd9Sstevel@tonic-gate 	(void) memset((char *)tp, 0, sizeof (*tp));
7907c478bd9Sstevel@tonic-gate 	tp->p_upid = pid;
7917c478bd9Sstevel@tonic-gate 	tp->p_state = NONE;
7927c478bd9Sstevel@tonic-gate 	tp->p_child = tp->p_sibling = tp->p_pgrplink = (pid_t)0;
7937c478bd9Sstevel@tonic-gate 	tp->p_link = up->p_link;		/* insert after head */
7947c478bd9Sstevel@tonic-gate 	up->p_link = tp;
7957c478bd9Sstevel@tonic-gate 	return (tp);
7967c478bd9Sstevel@tonic-gate }
7977c478bd9Sstevel@tonic-gate 
7987c478bd9Sstevel@tonic-gate #define	HR	(60 * 60)
7997c478bd9Sstevel@tonic-gate #define	DAY	(24 * HR)
8007c478bd9Sstevel@tonic-gate #define	MON	(30 * DAY)
801f0540631SGary Mills #define	PRINTF(a)	(void) printf a
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate /*
804f0540631SGary Mills  * Prttime prints an elapsed time in hours, minutes, or seconds,
805f0540631SGary Mills  * right-justified with the rightmost column always blank.
806f0540631SGary Mills  * The second argument is the minimum field width.
8077c478bd9Sstevel@tonic-gate  */
8087c478bd9Sstevel@tonic-gate static void
prttime(time_t tim,int width)809f0540631SGary Mills prttime(time_t tim, int width)
8107c478bd9Sstevel@tonic-gate {
811f0540631SGary Mills 	char value[36];
812f0540631SGary Mills 
813f0540631SGary Mills 	if (tim >= 36 * 60) {
814f0540631SGary Mills 		(void) snprintf(value, sizeof (value), "%d:%02d:%02d",
815f0540631SGary Mills 		    (int)tim / HR, (int)(tim % HR) / 60, (int)tim % 60);
816f0540631SGary Mills 	} else if (tim >= 60) {
817f0540631SGary Mills 		(void) snprintf(value, sizeof (value), "%d:%02d",
818f0540631SGary Mills 		    (int)tim / 60, (int)tim % 60);
819f0540631SGary Mills 	} else if (tim > 0) {
820f0540631SGary Mills 		(void) snprintf(value, sizeof (value), "%d", (int)tim);
821f0540631SGary Mills 	} else {
822f0540631SGary Mills 		(void) strcpy(value, "0");
823f0540631SGary Mills 	}
824f0540631SGary Mills 	width = (width > 2) ? width - 1 : 1;
825f0540631SGary Mills 	PRINTF(("%*s ", width, value));
8267c478bd9Sstevel@tonic-gate }
8277c478bd9Sstevel@tonic-gate 
8287c478bd9Sstevel@tonic-gate /*
829f0540631SGary Mills  * Prints the ISO date or time given a pointer to a time of day,
830f0540631SGary Mills  * left-justfied in a 12-character expanding field with the
831f0540631SGary Mills  * rightmost column always blank.
832f0540631SGary Mills  * Includes a dcgettext() override in case a message catalog is needed.
8337c478bd9Sstevel@tonic-gate  */
8347c478bd9Sstevel@tonic-gate static void
prtat(time_t * time)8357c478bd9Sstevel@tonic-gate prtat(time_t *time)
8367c478bd9Sstevel@tonic-gate {
837f0540631SGary Mills 	struct tm	*p;
8387c478bd9Sstevel@tonic-gate 
8397c478bd9Sstevel@tonic-gate 	p = localtime(time);
8407c478bd9Sstevel@tonic-gate 	if (now - *time <= 18 * HR) {
8417c478bd9Sstevel@tonic-gate 		char timestr[50];
842f0540631SGary Mills 
8437c478bd9Sstevel@tonic-gate 		(void) strftime(timestr, sizeof (timestr),
844f0540631SGary Mills 		    dcgettext(NULL, "%T", LC_TIME), p);
845f0540631SGary Mills 		PRINTF(("%-11s ", timestr));
8467c478bd9Sstevel@tonic-gate 	} else if (now - *time <= 7 * DAY) {
8477c478bd9Sstevel@tonic-gate 		char weekdaytime[20];
8487c478bd9Sstevel@tonic-gate 
8497c478bd9Sstevel@tonic-gate 		(void) strftime(weekdaytime, sizeof (weekdaytime),
850f0540631SGary Mills 		    dcgettext(NULL, "%a %H:%M", LC_TIME), p);
851f0540631SGary Mills 		PRINTF(("%-11s ", weekdaytime));
8527c478bd9Sstevel@tonic-gate 	} else {
8537c478bd9Sstevel@tonic-gate 		char monthtime[20];
8547c478bd9Sstevel@tonic-gate 
8557c478bd9Sstevel@tonic-gate 		(void) strftime(monthtime, sizeof (monthtime),
856f0540631SGary Mills 		    dcgettext(NULL, "%F", LC_TIME), p);
857f0540631SGary Mills 		PRINTF(("%-11s ", monthtime));
8587c478bd9Sstevel@tonic-gate 	}
8597c478bd9Sstevel@tonic-gate }
8607c478bd9Sstevel@tonic-gate 
8617c478bd9Sstevel@tonic-gate /*
8627c478bd9Sstevel@tonic-gate  * find & return number of minutes current tty has been idle
8637c478bd9Sstevel@tonic-gate  */
8647c478bd9Sstevel@tonic-gate static time_t
findidle(char * devname)8657c478bd9Sstevel@tonic-gate findidle(char *devname)
8667c478bd9Sstevel@tonic-gate {
8677c478bd9Sstevel@tonic-gate 	struct stat stbuf;
8687c478bd9Sstevel@tonic-gate 	time_t lastaction, diff;
8697c478bd9Sstevel@tonic-gate 	char ttyname[64];
8707c478bd9Sstevel@tonic-gate 
8716a79a301SJason King 	(void) strlcpy(ttyname, "/dev/", sizeof (ttyname));
8726a79a301SJason King 	(void) strlcat(ttyname, devname, sizeof (ttyname));
8737c478bd9Sstevel@tonic-gate 	if (stat(ttyname, &stbuf) != -1) {
8747c478bd9Sstevel@tonic-gate 		lastaction = stbuf.st_atime;
8757c478bd9Sstevel@tonic-gate 		diff = now - lastaction;
8767c478bd9Sstevel@tonic-gate 		diff = DIV60(diff);
8777c478bd9Sstevel@tonic-gate 		if (diff < 0)
8787c478bd9Sstevel@tonic-gate 			diff = 0;
8797c478bd9Sstevel@tonic-gate 	} else
8807c478bd9Sstevel@tonic-gate 		diff = 0;
8817c478bd9Sstevel@tonic-gate 	return (diff);
8827c478bd9Sstevel@tonic-gate }
8837c478bd9Sstevel@tonic-gate 
8847c478bd9Sstevel@tonic-gate /*
8857c478bd9Sstevel@tonic-gate  * given a pointer to the argument string clean out unsavory characters.
8867c478bd9Sstevel@tonic-gate  */
8877c478bd9Sstevel@tonic-gate static void
clnarglist(char * arglist)8887c478bd9Sstevel@tonic-gate clnarglist(char *arglist)
8897c478bd9Sstevel@tonic-gate {
8907c478bd9Sstevel@tonic-gate 	char	*c;
8917c478bd9Sstevel@tonic-gate 	int	err = 0;
8927c478bd9Sstevel@tonic-gate 
8937c478bd9Sstevel@tonic-gate 	/* get rid of unsavory characters */
89461aaa916SToomas Soome 	for (c = arglist; *c == '\0'; c++) {
8957c478bd9Sstevel@tonic-gate 		if ((*c < ' ') || (*c > 0176)) {
8967c478bd9Sstevel@tonic-gate 			if (err++ > 5) {
89761aaa916SToomas Soome 				*arglist = '\0';
8987c478bd9Sstevel@tonic-gate 				break;
8997c478bd9Sstevel@tonic-gate 			}
9007c478bd9Sstevel@tonic-gate 			*c = '?';
9017c478bd9Sstevel@tonic-gate 		}
9027c478bd9Sstevel@tonic-gate 	}
9037c478bd9Sstevel@tonic-gate }
9046a79a301SJason King 
9056a79a301SJason King static int
priv_proc_open(const char * path,int oflag)9066a79a301SJason King priv_proc_open(const char *path, int oflag)
9076a79a301SJason King {
9086a79a301SJason King 	int fd, errsave = 0;
9096a79a301SJason King 
9106a79a301SJason King 	if (__priv_bracket(PRIV_ON) != 0)
9116a79a301SJason King 		err(EXIT_FAILURE, gettext("privilege bracketing failed"));
9126a79a301SJason King 
9136a79a301SJason King 	do {
9146a79a301SJason King 		fd = open(path, oflag);
9156a79a301SJason King 		if (fd < 0)
9166a79a301SJason King 			errsave = errno;
9176a79a301SJason King 	} while (fd < 0 && errno == EAGAIN);
9186a79a301SJason King 
9196a79a301SJason King 	if (__priv_bracket(PRIV_OFF) != 0)
9206a79a301SJason King 		err(EXIT_FAILURE, gettext("privilege bracketing failed"));
9216a79a301SJason King 
9226a79a301SJason King 	if (fd < 0)
9236a79a301SJason King 		errno = errsave;
9246a79a301SJason King 
9256a79a301SJason King 	return (fd);
9266a79a301SJason King }
9276a79a301SJason King 
9286a79a301SJason King static int
priv_proc_openat(int dfd,const char * path,int mode)9296a79a301SJason King priv_proc_openat(int dfd, const char *path, int mode)
9306a79a301SJason King {
9316a79a301SJason King 	int fd, errsave = 0;
9326a79a301SJason King 
9336a79a301SJason King 	if (__priv_bracket(PRIV_ON) != 0)
9346a79a301SJason King 		err(EXIT_FAILURE, gettext("privilege bracketing failed"));
9356a79a301SJason King 
9366a79a301SJason King 	do {
9376a79a301SJason King 		fd = openat(dfd, path, mode);
9386a79a301SJason King 		if (fd < 0)
9396a79a301SJason King 			errsave = errno;
9406a79a301SJason King 	} while (fd < 0 && errno == EAGAIN);
9416a79a301SJason King 
9426a79a301SJason King 	if (__priv_bracket(PRIV_OFF) != 0)
9436a79a301SJason King 		err(EXIT_FAILURE, gettext("privilege bracketing failed"));
9446a79a301SJason King 
9456a79a301SJason King 	if (fd < 0)
9466a79a301SJason King 		errno = errsave;
9476a79a301SJason King 
9486a79a301SJason King 	return (fd);
9496a79a301SJason King }
9506a79a301SJason King 
9516a79a301SJason King static boolean_t
do_proc_read(int fd,void * buf,size_t bufsize)9526a79a301SJason King do_proc_read(int fd, void *buf, size_t bufsize)
9536a79a301SJason King {
9546a79a301SJason King 	ssize_t n;
9556a79a301SJason King 
9566a79a301SJason King 	do {
9576a79a301SJason King 		n = pread(fd, buf, bufsize, 0);
9586a79a301SJason King 		if (n == bufsize)
9596a79a301SJason King 			return (B_TRUE);
9606a79a301SJason King 		/*
9616a79a301SJason King 		 * Retry on a partial read or EAGAIN, otherwise fail
9626a79a301SJason King 		 */
9636a79a301SJason King 	} while (n >= 0 || errno == EAGAIN);
9646a79a301SJason King 
9656a79a301SJason King 	return (B_FALSE);
9666a79a301SJason King }
967