xref: /titanic_54/usr/src/cmd/prstat/prstat.c (revision c64027834c5ffc60c557c2b12555e0cd4d30320c)
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
5*c6402783Sakolb  * Common Development and Distribution License (the "License").
6*c6402783Sakolb  * 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  */
21*c6402783Sakolb 
227c478bd9Sstevel@tonic-gate /*
23*c6402783Sakolb  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <sys/types.h>
307c478bd9Sstevel@tonic-gate #include <sys/resource.h>
317c478bd9Sstevel@tonic-gate #include <sys/loadavg.h>
327c478bd9Sstevel@tonic-gate #include <sys/time.h>
337c478bd9Sstevel@tonic-gate #include <sys/pset.h>
347c478bd9Sstevel@tonic-gate #include <zone.h>
357c478bd9Sstevel@tonic-gate #include <libzonecfg.h>
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #include <stdio.h>
387c478bd9Sstevel@tonic-gate #include <stdlib.h>
397c478bd9Sstevel@tonic-gate #include <unistd.h>
407c478bd9Sstevel@tonic-gate #include <dirent.h>
417c478bd9Sstevel@tonic-gate #include <string.h>
427c478bd9Sstevel@tonic-gate #include <errno.h>
437c478bd9Sstevel@tonic-gate #include <poll.h>
447c478bd9Sstevel@tonic-gate #include <ctype.h>
457c478bd9Sstevel@tonic-gate #include <fcntl.h>
467c478bd9Sstevel@tonic-gate #include <limits.h>
477c478bd9Sstevel@tonic-gate #include <signal.h>
487c478bd9Sstevel@tonic-gate #include <time.h>
497c478bd9Sstevel@tonic-gate #include <project.h>
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate #include <libintl.h>
527c478bd9Sstevel@tonic-gate #include <locale.h>
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate #include "prstat.h"
557c478bd9Sstevel@tonic-gate #include "prutil.h"
567c478bd9Sstevel@tonic-gate #include "prtable.h"
577c478bd9Sstevel@tonic-gate #include "prsort.h"
587c478bd9Sstevel@tonic-gate #include "prfile.h"
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate /*
617c478bd9Sstevel@tonic-gate  * x86 <sys/regs.h> ERR conflicts with <curses.h> ERR.  For the purposes
627c478bd9Sstevel@tonic-gate  * of this file, we care about the curses.h ERR so include that last.
637c478bd9Sstevel@tonic-gate  */
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate #if	defined(ERR)
667c478bd9Sstevel@tonic-gate #undef	ERR
677c478bd9Sstevel@tonic-gate #endif
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate #ifndef	TEXT_DOMAIN			/* should be defined by cc -D */
707c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"	/* use this only if it wasn't */
717c478bd9Sstevel@tonic-gate #endif
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate #include <curses.h>
747c478bd9Sstevel@tonic-gate #include <term.h>
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate #define	PSINFO_HEADER_PROC \
777c478bd9Sstevel@tonic-gate "   PID USERNAME  SIZE   RSS STATE  PRI NICE      TIME  CPU PROCESS/NLWP       "
78*c6402783Sakolb #define	PSINFO_HEADER_PROC_LGRP \
79*c6402783Sakolb "   PID USERNAME  SIZE   RSS STATE  PRI NICE      TIME  CPU LGRP PROCESS/NLWP  "
807c478bd9Sstevel@tonic-gate #define	PSINFO_HEADER_LWP \
817c478bd9Sstevel@tonic-gate "   PID USERNAME  SIZE   RSS STATE  PRI NICE      TIME  CPU PROCESS/LWPID      "
82*c6402783Sakolb #define	PSINFO_HEADER_LWP_LGRP \
83*c6402783Sakolb "   PID USERNAME  SIZE   RSS STATE  PRI NICE      TIME  CPU LGRP PROCESS/LWPID "
847c478bd9Sstevel@tonic-gate #define	USAGE_HEADER_PROC \
857c478bd9Sstevel@tonic-gate "   PID USERNAME USR SYS TRP TFL DFL LCK SLP LAT VCX ICX SCL SIG PROCESS/NLWP  "
867c478bd9Sstevel@tonic-gate #define	USAGE_HEADER_LWP \
877c478bd9Sstevel@tonic-gate "   PID USERNAME USR SYS TRP TFL DFL LCK SLP LAT VCX ICX SCL SIG PROCESS/LWPID "
887c478bd9Sstevel@tonic-gate #define	USER_HEADER_PROC \
897c478bd9Sstevel@tonic-gate " NPROC USERNAME  SIZE   RSS MEMORY      TIME  CPU                             "
907c478bd9Sstevel@tonic-gate #define	USER_HEADER_LWP \
917c478bd9Sstevel@tonic-gate "  NLWP USERNAME  SIZE   RSS MEMORY      TIME  CPU                             "
927c478bd9Sstevel@tonic-gate #define	TASK_HEADER_PROC \
937c478bd9Sstevel@tonic-gate "TASKID    NPROC  SIZE   RSS MEMORY      TIME  CPU PROJECT                     "
947c478bd9Sstevel@tonic-gate #define	TASK_HEADER_LWP \
957c478bd9Sstevel@tonic-gate "TASKID     NLWP  SIZE   RSS MEMORY      TIME  CPU PROJECT                     "
967c478bd9Sstevel@tonic-gate #define	PROJECT_HEADER_PROC \
977c478bd9Sstevel@tonic-gate "PROJID    NPROC  SIZE   RSS MEMORY      TIME  CPU PROJECT                     "
987c478bd9Sstevel@tonic-gate #define	PROJECT_HEADER_LWP \
997c478bd9Sstevel@tonic-gate "PROJID     NLWP  SIZE   RSS MEMORY      TIME  CPU PROJECT                     "
1007c478bd9Sstevel@tonic-gate #define	ZONE_HEADER_PROC \
1017c478bd9Sstevel@tonic-gate "ZONEID    NPROC  SIZE   RSS MEMORY      TIME  CPU ZONE                        "
1027c478bd9Sstevel@tonic-gate #define	ZONE_HEADER_LWP \
1037c478bd9Sstevel@tonic-gate "ZONEID     NLWP  SIZE   RSS MEMORY      TIME  CPU ZONE                        "
1047c478bd9Sstevel@tonic-gate #define	PSINFO_LINE \
1057c478bd9Sstevel@tonic-gate "%6d %-8s %5s %5s %-6s %3s  %3s %9s %3.3s%% %-.16s/%d"
106*c6402783Sakolb #define	PSINFO_LINE_LGRP \
107*c6402783Sakolb "%6d %-8s %5s %5s %-6s %3s  %3s %9s %3.3s%% %4d %-.16s/%d"
1087c478bd9Sstevel@tonic-gate #define	USAGE_LINE \
1097c478bd9Sstevel@tonic-gate "%6d %-8s %3.3s %3.3s %3.3s %3.3s %3.3s %3.3s %3.3s %3.3s %3.3s %3.3s %3.3s "\
1107c478bd9Sstevel@tonic-gate "%3.3s %-.12s/%d"
1117c478bd9Sstevel@tonic-gate #define	USER_LINE \
1127c478bd9Sstevel@tonic-gate "%6d %-8s %5.5s %5.5s   %3.3s%% %9s %3.3s%%"
1137c478bd9Sstevel@tonic-gate #define	TASK_LINE \
1147c478bd9Sstevel@tonic-gate "%6d %8d %5s %5s   %3.3s%% %9s %3.3s%% %28s"
1157c478bd9Sstevel@tonic-gate #define	PROJECT_LINE \
1167c478bd9Sstevel@tonic-gate "%6d %8d %5s %5s   %3.3s%% %9s %3.3s%% %28s"
1177c478bd9Sstevel@tonic-gate #define	ZONE_LINE \
1187c478bd9Sstevel@tonic-gate "%6d %8d %5s %5s   %3.3s%% %9s %3.3s%% %28s"
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate #define	TOTAL_LINE \
1217c478bd9Sstevel@tonic-gate "Total: %d processes, %d lwps, load averages: %3.2f, %3.2f, %3.2f"
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate /* global variables */
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate static char	*t_ulon;			/* termcap: start underline */
1267c478bd9Sstevel@tonic-gate static char	*t_uloff;			/* termcap: end underline */
1277c478bd9Sstevel@tonic-gate static char	*t_up;				/* termcap: cursor 1 line up */
1287c478bd9Sstevel@tonic-gate static char	*t_eol;				/* termcap: clear end of line */
1297c478bd9Sstevel@tonic-gate static char	*t_smcup;			/* termcap: cursor mvcap on */
1307c478bd9Sstevel@tonic-gate static char	*t_rmcup;			/* termcap: cursor mvcap off */
1317c478bd9Sstevel@tonic-gate static char	*t_home;			/* termcap: move cursor home */
1327c478bd9Sstevel@tonic-gate static char	*movecur = NULL;		/* termcap: move up string */
1337c478bd9Sstevel@tonic-gate static char	*empty_string = "\0";		/* termcap: empty string */
1347c478bd9Sstevel@tonic-gate static uint_t	print_movecur = FALSE;		/* print movecur or not */
1357c478bd9Sstevel@tonic-gate static int	is_curses_on = FALSE;		/* current curses state */
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate static table_t	pid_tbl = {0, 0, NULL};		/* selected processes */
1387c478bd9Sstevel@tonic-gate static table_t	cpu_tbl = {0, 0, NULL};		/* selected processors */
1397c478bd9Sstevel@tonic-gate static table_t  set_tbl = {0, 0, NULL};		/* selected processor sets */
1407c478bd9Sstevel@tonic-gate static table_t	prj_tbl = {0, 0, NULL};		/* selected projects */
1417c478bd9Sstevel@tonic-gate static table_t	tsk_tbl = {0, 0, NULL};		/* selected tasks */
142*c6402783Sakolb static table_t	lgr_tbl = {0, 0, NULL};		/* selected lgroups */
1437c478bd9Sstevel@tonic-gate static zonetbl_t zone_tbl = {0, 0, NULL};	/* selected zones */
1447c478bd9Sstevel@tonic-gate static nametbl_t euid_tbl = {0, 0, NULL}; 	/* selected effective users */
1457c478bd9Sstevel@tonic-gate static nametbl_t ruid_tbl = {0, 0, NULL}; 	/* selected real users */
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate static uint_t	total_procs;			/* total number of procs */
1487c478bd9Sstevel@tonic-gate static uint_t	total_lwps;			/* total number of lwps */
1497c478bd9Sstevel@tonic-gate static float	total_cpu;			/* total cpu usage */
1507c478bd9Sstevel@tonic-gate static float	total_mem;			/* total memory usage */
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate static list_t	lwps;				/* list of lwps/processes */
1537c478bd9Sstevel@tonic-gate static list_t	users;				/* list of users */
1547c478bd9Sstevel@tonic-gate static list_t	tasks;				/* list of tasks */
1557c478bd9Sstevel@tonic-gate static list_t	projects;			/* list of projects */
1567c478bd9Sstevel@tonic-gate static list_t	zones;				/* list of zones */
157*c6402783Sakolb static list_t	lgroups;			/* list of lgroups */
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate static volatile uint_t sigwinch = 0;
1607c478bd9Sstevel@tonic-gate static volatile uint_t sigtstp = 0;
1617c478bd9Sstevel@tonic-gate static volatile uint_t sigterm = 0;
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate /* default settings */
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate static optdesc_t opts = {
1667c478bd9Sstevel@tonic-gate 	5,			/* interval between updates, seconds */
1677c478bd9Sstevel@tonic-gate 	15,			/* number of lines in top part */
1687c478bd9Sstevel@tonic-gate 	5,			/* number of lines in bottom part */
1697c478bd9Sstevel@tonic-gate 	-1,			/* number of iterations; infinitely */
1707c478bd9Sstevel@tonic-gate 	OPT_PSINFO | OPT_FULLSCREEN | OPT_USEHOME | OPT_TERMCAP,
1717c478bd9Sstevel@tonic-gate 	-1			/* sort in decreasing order */
1727c478bd9Sstevel@tonic-gate };
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate static void
1757c478bd9Sstevel@tonic-gate psetloadavg(long psetid, void *ptr)
1767c478bd9Sstevel@tonic-gate {
1777c478bd9Sstevel@tonic-gate 	double psetloadavg[3];
1787c478bd9Sstevel@tonic-gate 	double *loadavg = ptr;
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	if (pset_getloadavg((psetid_t)psetid, psetloadavg, 3) != -1) {
1817c478bd9Sstevel@tonic-gate 		*loadavg++ += psetloadavg[0];
1827c478bd9Sstevel@tonic-gate 		*loadavg++ += psetloadavg[1];
1837c478bd9Sstevel@tonic-gate 		*loadavg += psetloadavg[2];
1847c478bd9Sstevel@tonic-gate 	}
1857c478bd9Sstevel@tonic-gate }
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate /*
1887c478bd9Sstevel@tonic-gate  * A routine to display the contents of the list on the screen
1897c478bd9Sstevel@tonic-gate  */
1907c478bd9Sstevel@tonic-gate static void
1917c478bd9Sstevel@tonic-gate list_print(list_t *list)
1927c478bd9Sstevel@tonic-gate {
1937c478bd9Sstevel@tonic-gate 	lwp_info_t *lwp;
1947c478bd9Sstevel@tonic-gate 	id_info_t *id;
1957c478bd9Sstevel@tonic-gate 	char usr[4], sys[4], trp[4], tfl[4];
1967c478bd9Sstevel@tonic-gate 	char dfl[4], lck[4], slp[4], lat[4];
1977c478bd9Sstevel@tonic-gate 	char vcx[4], icx[4], scl[4], sig[4];
1987c478bd9Sstevel@tonic-gate 	char psize[6], prssize[6], pmem[6], pcpu[6], ptime[12];
1997c478bd9Sstevel@tonic-gate 	char pstate[7], pnice[4], ppri[4];
2007c478bd9Sstevel@tonic-gate 	char pname[LOGNAME_MAX+1];
2017c478bd9Sstevel@tonic-gate 	char projname[PROJNAME_MAX+1];
2027c478bd9Sstevel@tonic-gate 	char zonename[ZONENAME_MAX+1];
2037c478bd9Sstevel@tonic-gate 	float cpu, mem;
2047c478bd9Sstevel@tonic-gate 	double loadavg[3] = {0, 0, 0};
2057c478bd9Sstevel@tonic-gate 	int i, lwpid;
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 	if (foreach_element(&set_tbl, &loadavg, psetloadavg) == 0) {
2087c478bd9Sstevel@tonic-gate 		/*
2097c478bd9Sstevel@tonic-gate 		 * If processor sets aren't specified, we display system-wide
2107c478bd9Sstevel@tonic-gate 		 * load averages.
2117c478bd9Sstevel@tonic-gate 		 */
2127c478bd9Sstevel@tonic-gate 		(void) getloadavg(loadavg, 3);
2137c478bd9Sstevel@tonic-gate 	}
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	if (opts.o_outpmode & OPT_TTY)
2167c478bd9Sstevel@tonic-gate 		(void) putchar('\r');
2177c478bd9Sstevel@tonic-gate 	(void) putp(t_ulon);
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	switch (list->l_type) {
2207c478bd9Sstevel@tonic-gate 	case LT_PROJECTS:
2217c478bd9Sstevel@tonic-gate 		if (opts.o_outpmode & OPT_LWPS)
2227c478bd9Sstevel@tonic-gate 			(void) printf(PROJECT_HEADER_LWP);
2237c478bd9Sstevel@tonic-gate 		else
2247c478bd9Sstevel@tonic-gate 			(void) printf(PROJECT_HEADER_PROC);
2257c478bd9Sstevel@tonic-gate 		break;
2267c478bd9Sstevel@tonic-gate 	case LT_TASKS:
2277c478bd9Sstevel@tonic-gate 		if (opts.o_outpmode & OPT_LWPS)
2287c478bd9Sstevel@tonic-gate 			(void) printf(TASK_HEADER_LWP);
2297c478bd9Sstevel@tonic-gate 		else
2307c478bd9Sstevel@tonic-gate 			(void) printf(TASK_HEADER_PROC);
2317c478bd9Sstevel@tonic-gate 		break;
2327c478bd9Sstevel@tonic-gate 	case LT_ZONES:
2337c478bd9Sstevel@tonic-gate 		if (opts.o_outpmode & OPT_LWPS)
2347c478bd9Sstevel@tonic-gate 			(void) printf(ZONE_HEADER_LWP);
2357c478bd9Sstevel@tonic-gate 		else
2367c478bd9Sstevel@tonic-gate 			(void) printf(ZONE_HEADER_PROC);
2377c478bd9Sstevel@tonic-gate 		break;
2387c478bd9Sstevel@tonic-gate 	case LT_USERS:
2397c478bd9Sstevel@tonic-gate 		if (opts.o_outpmode & OPT_LWPS)
2407c478bd9Sstevel@tonic-gate 			(void) printf(USER_HEADER_LWP);
2417c478bd9Sstevel@tonic-gate 		else
2427c478bd9Sstevel@tonic-gate 			(void) printf(USER_HEADER_PROC);
2437c478bd9Sstevel@tonic-gate 		break;
2447c478bd9Sstevel@tonic-gate 	case LT_LWPS:
2457c478bd9Sstevel@tonic-gate 		if (opts.o_outpmode & OPT_LWPS) {
246*c6402783Sakolb 			if (opts.o_outpmode & OPT_PSINFO) {
247*c6402783Sakolb 				if (opts.o_outpmode & OPT_LGRP)
248*c6402783Sakolb 					(void) printf(PSINFO_HEADER_LWP_LGRP);
249*c6402783Sakolb 				else
2507c478bd9Sstevel@tonic-gate 					(void) printf(PSINFO_HEADER_LWP);
251*c6402783Sakolb 			}
2527c478bd9Sstevel@tonic-gate 			if (opts.o_outpmode & OPT_MSACCT)
2537c478bd9Sstevel@tonic-gate 				(void) printf(USAGE_HEADER_LWP);
2547c478bd9Sstevel@tonic-gate 		} else {
255*c6402783Sakolb 			if (opts.o_outpmode & OPT_PSINFO) {
256*c6402783Sakolb 				if (opts.o_outpmode & OPT_LGRP)
257*c6402783Sakolb 					(void) printf(PSINFO_HEADER_PROC_LGRP);
258*c6402783Sakolb 				else
2597c478bd9Sstevel@tonic-gate 					(void) printf(PSINFO_HEADER_PROC);
260*c6402783Sakolb 			}
2617c478bd9Sstevel@tonic-gate 			if (opts.o_outpmode & OPT_MSACCT)
2627c478bd9Sstevel@tonic-gate 				(void) printf(USAGE_HEADER_PROC);
2637c478bd9Sstevel@tonic-gate 		}
2647c478bd9Sstevel@tonic-gate 		break;
2657c478bd9Sstevel@tonic-gate 	}
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 	(void) putp(t_uloff);
2687c478bd9Sstevel@tonic-gate 	(void) putp(t_eol);
2697c478bd9Sstevel@tonic-gate 	(void) putchar('\n');
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 	for (i = 0; i < list->l_used; i++) {
2727c478bd9Sstevel@tonic-gate 		switch (list->l_type) {
2737c478bd9Sstevel@tonic-gate 		case LT_PROJECTS:
2747c478bd9Sstevel@tonic-gate 		case LT_TASKS:
2757c478bd9Sstevel@tonic-gate 		case LT_USERS:
2767c478bd9Sstevel@tonic-gate 		case LT_ZONES:
2777c478bd9Sstevel@tonic-gate 			id = list->l_ptrs[i];
2787c478bd9Sstevel@tonic-gate 			/*
2797c478bd9Sstevel@tonic-gate 			 * CPU usage and memory usage normalization
2807c478bd9Sstevel@tonic-gate 			 */
2817c478bd9Sstevel@tonic-gate 			if (total_cpu >= 100)
2827c478bd9Sstevel@tonic-gate 				cpu = (100 * id->id_pctcpu) / total_cpu;
2837c478bd9Sstevel@tonic-gate 			else
2847c478bd9Sstevel@tonic-gate 				cpu = id->id_pctcpu;
2857c478bd9Sstevel@tonic-gate 			if (total_mem >= 100)
2867c478bd9Sstevel@tonic-gate 				mem = (100 * id->id_pctmem) / total_mem;
2877c478bd9Sstevel@tonic-gate 			else
2887c478bd9Sstevel@tonic-gate 				mem = id->id_pctmem;
2897c478bd9Sstevel@tonic-gate 			if (list->l_type == LT_USERS)
2907c478bd9Sstevel@tonic-gate 				pwd_getname(id->id_uid, pname, LOGNAME_MAX + 1);
2917c478bd9Sstevel@tonic-gate 			else if (list->l_type == LT_ZONES)
2927c478bd9Sstevel@tonic-gate 				getzonename(id->id_zoneid, zonename,
2937c478bd9Sstevel@tonic-gate 				    ZONENAME_MAX);
2947c478bd9Sstevel@tonic-gate 			else
2957c478bd9Sstevel@tonic-gate 				getprojname(id->id_projid, projname,
2967c478bd9Sstevel@tonic-gate 				    PROJNAME_MAX);
2977c478bd9Sstevel@tonic-gate 			Format_size(psize, id->id_size, 6);
2987c478bd9Sstevel@tonic-gate 			Format_size(prssize, id->id_rssize, 6);
2997c478bd9Sstevel@tonic-gate 			Format_pct(pmem, mem, 4);
3007c478bd9Sstevel@tonic-gate 			Format_pct(pcpu, cpu, 4);
3017c478bd9Sstevel@tonic-gate 			Format_time(ptime, id->id_time, 10);
3027c478bd9Sstevel@tonic-gate 			if (opts.o_outpmode & OPT_TTY)
3037c478bd9Sstevel@tonic-gate 				(void) putchar('\r');
3047c478bd9Sstevel@tonic-gate 			if (list->l_type == LT_PROJECTS)
3057c478bd9Sstevel@tonic-gate 				(void) printf(PROJECT_LINE, (int)id->id_projid,
3067c478bd9Sstevel@tonic-gate 				    id->id_nproc, psize, prssize, pmem, ptime,
3077c478bd9Sstevel@tonic-gate 				    pcpu, projname);
3087c478bd9Sstevel@tonic-gate 			else if (list->l_type == LT_TASKS)
3097c478bd9Sstevel@tonic-gate 				(void) printf(TASK_LINE, (int)id->id_taskid,
3107c478bd9Sstevel@tonic-gate 				    id->id_nproc, psize, prssize, pmem, ptime,
3117c478bd9Sstevel@tonic-gate 				    pcpu, projname);
3127c478bd9Sstevel@tonic-gate 			else if (list->l_type == LT_ZONES)
3137c478bd9Sstevel@tonic-gate 				(void) printf(ZONE_LINE, (int)id->id_zoneid,
3147c478bd9Sstevel@tonic-gate 				    id->id_nproc, psize, prssize, pmem, ptime,
3157c478bd9Sstevel@tonic-gate 				    pcpu, zonename);
3167c478bd9Sstevel@tonic-gate 			else
3177c478bd9Sstevel@tonic-gate 				(void) printf(USER_LINE, id->id_nproc, pname,
3187c478bd9Sstevel@tonic-gate 				    psize, prssize, pmem, ptime, pcpu);
3197c478bd9Sstevel@tonic-gate 			(void) putp(t_eol);
3207c478bd9Sstevel@tonic-gate 			(void) putchar('\n');
3217c478bd9Sstevel@tonic-gate 			break;
3227c478bd9Sstevel@tonic-gate 		case LT_LWPS:
3237c478bd9Sstevel@tonic-gate 			lwp = list->l_ptrs[i];
3247c478bd9Sstevel@tonic-gate 			if (opts.o_outpmode & OPT_LWPS)
3257c478bd9Sstevel@tonic-gate 				lwpid = lwp->li_info.pr_lwp.pr_lwpid;
3267c478bd9Sstevel@tonic-gate 			else
3277c478bd9Sstevel@tonic-gate 				lwpid = lwp->li_info.pr_nlwp +
3287c478bd9Sstevel@tonic-gate 				    lwp->li_info.pr_nzomb;
3297c478bd9Sstevel@tonic-gate 			pwd_getname(lwp->li_info.pr_uid, pname,
3307c478bd9Sstevel@tonic-gate 			    LOGNAME_MAX + 1);
3317c478bd9Sstevel@tonic-gate 			if (opts.o_outpmode & OPT_PSINFO) {
3327c478bd9Sstevel@tonic-gate 				Format_size(psize, lwp->li_info.pr_size, 6);
3337c478bd9Sstevel@tonic-gate 				Format_size(prssize, lwp->li_info.pr_rssize, 6);
3347c478bd9Sstevel@tonic-gate 				Format_state(pstate,
3357c478bd9Sstevel@tonic-gate 				    lwp->li_info.pr_lwp.pr_sname,
3367c478bd9Sstevel@tonic-gate 				    lwp->li_info.pr_lwp.pr_onpro, 7);
3377c478bd9Sstevel@tonic-gate 				if (strcmp(lwp->li_info.pr_lwp.pr_clname,
3387c478bd9Sstevel@tonic-gate 				    "RT") == 0 ||
3397c478bd9Sstevel@tonic-gate 				    strcmp(lwp->li_info.pr_lwp.pr_clname,
3407c478bd9Sstevel@tonic-gate 				    "SYS") == 0 ||
3417c478bd9Sstevel@tonic-gate 				    lwp->li_info.pr_lwp.pr_sname == 'Z')
3427c478bd9Sstevel@tonic-gate 					(void) strcpy(pnice, "  -");
3437c478bd9Sstevel@tonic-gate 				else
3447c478bd9Sstevel@tonic-gate 					Format_num(pnice,
3457c478bd9Sstevel@tonic-gate 					    lwp->li_info.pr_lwp.pr_nice - NZERO,
3467c478bd9Sstevel@tonic-gate 					    4);
3477c478bd9Sstevel@tonic-gate 				Format_num(ppri, lwp->li_info.pr_lwp.pr_pri, 4);
3487c478bd9Sstevel@tonic-gate 				Format_pct(pcpu,
3497c478bd9Sstevel@tonic-gate 				    FRC2PCT(lwp->li_info.pr_lwp.pr_pctcpu), 4);
3507c478bd9Sstevel@tonic-gate 				if (opts.o_outpmode & OPT_LWPS)
3517c478bd9Sstevel@tonic-gate 					Format_time(ptime,
3527c478bd9Sstevel@tonic-gate 					    lwp->li_info.pr_lwp.pr_time.tv_sec,
3537c478bd9Sstevel@tonic-gate 					    10);
3547c478bd9Sstevel@tonic-gate 				else
3557c478bd9Sstevel@tonic-gate 					Format_time(ptime,
3567c478bd9Sstevel@tonic-gate 					    lwp->li_info.pr_time.tv_sec, 10);
3577c478bd9Sstevel@tonic-gate 				if (opts.o_outpmode & OPT_TTY)
3587c478bd9Sstevel@tonic-gate 					(void) putchar('\r');
3597c478bd9Sstevel@tonic-gate 				stripfname(lwp->li_info.pr_fname);
360*c6402783Sakolb 				if (opts.o_outpmode & OPT_LGRP) {
361*c6402783Sakolb 					(void) printf(PSINFO_LINE_LGRP,
362*c6402783Sakolb 					    (int)lwp->li_info.pr_pid, pname,
363*c6402783Sakolb 					    psize, prssize, pstate, ppri, pnice,
364*c6402783Sakolb 					    ptime, pcpu,
365*c6402783Sakolb 					    (int)lwp->li_info.pr_lwp.pr_lgrp,
366*c6402783Sakolb 					    lwp->li_info.pr_fname, lwpid);
367*c6402783Sakolb 				} else {
3687c478bd9Sstevel@tonic-gate 					(void) printf(PSINFO_LINE,
3697c478bd9Sstevel@tonic-gate 					    (int)lwp->li_info.pr_pid, pname,
3707c478bd9Sstevel@tonic-gate 					    psize, prssize, pstate, ppri, pnice,
371*c6402783Sakolb 					    ptime, pcpu,
372*c6402783Sakolb 					    lwp->li_info.pr_fname, lwpid);
373*c6402783Sakolb 				}
3747c478bd9Sstevel@tonic-gate 				(void) putp(t_eol);
3757c478bd9Sstevel@tonic-gate 				(void) putchar('\n');
3767c478bd9Sstevel@tonic-gate 			}
3777c478bd9Sstevel@tonic-gate 			if (opts.o_outpmode & OPT_MSACCT) {
3787c478bd9Sstevel@tonic-gate 				Format_pct(usr, lwp->li_usr, 4);
3797c478bd9Sstevel@tonic-gate 				Format_pct(sys, lwp->li_sys, 4);
3807c478bd9Sstevel@tonic-gate 				Format_pct(slp, lwp->li_slp, 4);
3817c478bd9Sstevel@tonic-gate 				Format_num(vcx, lwp->li_vcx, 4);
3827c478bd9Sstevel@tonic-gate 				Format_num(icx, lwp->li_icx, 4);
3837c478bd9Sstevel@tonic-gate 				Format_num(scl, lwp->li_scl, 4);
3847c478bd9Sstevel@tonic-gate 				Format_num(sig, lwp->li_sig, 4);
3857c478bd9Sstevel@tonic-gate 				Format_pct(trp, lwp->li_trp, 4);
3867c478bd9Sstevel@tonic-gate 				Format_pct(tfl, lwp->li_tfl, 4);
3877c478bd9Sstevel@tonic-gate 				Format_pct(dfl, lwp->li_dfl, 4);
3887c478bd9Sstevel@tonic-gate 				Format_pct(lck, lwp->li_lck, 4);
3897c478bd9Sstevel@tonic-gate 				Format_pct(lat, lwp->li_lat, 4);
3907c478bd9Sstevel@tonic-gate 				if (opts.o_outpmode & OPT_TTY)
3917c478bd9Sstevel@tonic-gate 					(void) putchar('\r');
3927c478bd9Sstevel@tonic-gate 				stripfname(lwp->li_info.pr_fname);
3937c478bd9Sstevel@tonic-gate 				(void) printf(USAGE_LINE,
3947c478bd9Sstevel@tonic-gate 				    (int)lwp->li_info.pr_pid, pname,
3957c478bd9Sstevel@tonic-gate 				    usr, sys, trp, tfl, dfl, lck,
3967c478bd9Sstevel@tonic-gate 				    slp, lat, vcx, icx, scl, sig,
3977c478bd9Sstevel@tonic-gate 				    lwp->li_info.pr_fname, lwpid);
3987c478bd9Sstevel@tonic-gate 				(void) putp(t_eol);
3997c478bd9Sstevel@tonic-gate 				(void) putchar('\n');
4007c478bd9Sstevel@tonic-gate 			}
4017c478bd9Sstevel@tonic-gate 			break;
4027c478bd9Sstevel@tonic-gate 		}
4037c478bd9Sstevel@tonic-gate 	}
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 	if (opts.o_outpmode & OPT_TTY)
4067c478bd9Sstevel@tonic-gate 		(void) putchar('\r');
4077c478bd9Sstevel@tonic-gate 	if (opts.o_outpmode & OPT_TERMCAP) {
4087c478bd9Sstevel@tonic-gate 		switch (list->l_type) {
4097c478bd9Sstevel@tonic-gate 		case LT_PROJECTS:
4107c478bd9Sstevel@tonic-gate 		case LT_USERS:
4117c478bd9Sstevel@tonic-gate 		case LT_TASKS:
4127c478bd9Sstevel@tonic-gate 		case LT_ZONES:
4137c478bd9Sstevel@tonic-gate 			while (i++ < opts.o_nbottom) {
4147c478bd9Sstevel@tonic-gate 				(void) putp(t_eol);
4157c478bd9Sstevel@tonic-gate 				(void) putchar('\n');
4167c478bd9Sstevel@tonic-gate 			}
4177c478bd9Sstevel@tonic-gate 			break;
4187c478bd9Sstevel@tonic-gate 		case LT_LWPS:
4197c478bd9Sstevel@tonic-gate 			while (i++ < opts.o_ntop) {
4207c478bd9Sstevel@tonic-gate 				(void) putp(t_eol);
4217c478bd9Sstevel@tonic-gate 				(void) putchar('\n');
4227c478bd9Sstevel@tonic-gate 			}
4237c478bd9Sstevel@tonic-gate 		}
4247c478bd9Sstevel@tonic-gate 	}
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate 	if (opts.o_outpmode & OPT_TTY)
4277c478bd9Sstevel@tonic-gate 		(void) putchar('\r');
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 	if ((opts.o_outpmode & OPT_SPLIT) && list->l_type == LT_LWPS)
4307c478bd9Sstevel@tonic-gate 		return;
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate 	(void) printf(TOTAL_LINE, total_procs, total_lwps,
4337c478bd9Sstevel@tonic-gate 	    loadavg[LOADAVG_1MIN], loadavg[LOADAVG_5MIN],
4347c478bd9Sstevel@tonic-gate 	    loadavg[LOADAVG_15MIN]);
4357c478bd9Sstevel@tonic-gate 	(void) putp(t_eol);
4367c478bd9Sstevel@tonic-gate 	(void) putchar('\n');
4377c478bd9Sstevel@tonic-gate 	if (opts.o_outpmode & OPT_TTY)
4387c478bd9Sstevel@tonic-gate 		(void) putchar('\r');
4397c478bd9Sstevel@tonic-gate 	(void) putp(t_eol);
4407c478bd9Sstevel@tonic-gate 	(void) fflush(stdout);
4417c478bd9Sstevel@tonic-gate }
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate static lwp_info_t *
4447c478bd9Sstevel@tonic-gate list_add_lwp(list_t *list, pid_t pid, id_t lwpid)
4457c478bd9Sstevel@tonic-gate {
4467c478bd9Sstevel@tonic-gate 	lwp_info_t *lwp;
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 	if (list->l_head == NULL) {
4497c478bd9Sstevel@tonic-gate 		list->l_head = list->l_tail = lwp = Zalloc(sizeof (lwp_info_t));
4507c478bd9Sstevel@tonic-gate 	} else {
4517c478bd9Sstevel@tonic-gate 		lwp = Zalloc(sizeof (lwp_info_t));
4527c478bd9Sstevel@tonic-gate 		lwp->li_prev = list->l_tail;
4537c478bd9Sstevel@tonic-gate 		((lwp_info_t *)list->l_tail)->li_next = lwp;
4547c478bd9Sstevel@tonic-gate 		list->l_tail = lwp;
4557c478bd9Sstevel@tonic-gate 	}
4567c478bd9Sstevel@tonic-gate 	lwp->li_info.pr_pid = pid;
4577c478bd9Sstevel@tonic-gate 	lwp->li_info.pr_lwp.pr_lwpid = lwpid;
4587c478bd9Sstevel@tonic-gate 	lwpid_add(lwp, pid, lwpid);
4597c478bd9Sstevel@tonic-gate 	list->l_count++;
4607c478bd9Sstevel@tonic-gate 	return (lwp);
4617c478bd9Sstevel@tonic-gate }
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate static void
4647c478bd9Sstevel@tonic-gate list_remove_lwp(list_t *list, lwp_info_t *lwp)
4657c478bd9Sstevel@tonic-gate {
4667c478bd9Sstevel@tonic-gate 	if (lwp->li_prev)
4677c478bd9Sstevel@tonic-gate 		lwp->li_prev->li_next = lwp->li_next;
4687c478bd9Sstevel@tonic-gate 	else
4697c478bd9Sstevel@tonic-gate 		list->l_head = lwp->li_next;	/* removing the head */
4707c478bd9Sstevel@tonic-gate 	if (lwp->li_next)
4717c478bd9Sstevel@tonic-gate 		lwp->li_next->li_prev = lwp->li_prev;
4727c478bd9Sstevel@tonic-gate 	else
4737c478bd9Sstevel@tonic-gate 		list->l_tail = lwp->li_prev;	/* removing the tail */
4747c478bd9Sstevel@tonic-gate 	lwpid_del(lwp->li_info.pr_pid, lwp->li_info.pr_lwp.pr_lwpid);
4757c478bd9Sstevel@tonic-gate 	if (lwpid_pidcheck(lwp->li_info.pr_pid) == 0)
4767c478bd9Sstevel@tonic-gate 		fds_rm(lwp->li_info.pr_pid);
4777c478bd9Sstevel@tonic-gate 	list->l_count--;
4787c478bd9Sstevel@tonic-gate 	free(lwp);
4797c478bd9Sstevel@tonic-gate }
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate static void
4827c478bd9Sstevel@tonic-gate list_clear(list_t *list)
4837c478bd9Sstevel@tonic-gate {
4847c478bd9Sstevel@tonic-gate 	if (list->l_type == LT_LWPS) {
4857c478bd9Sstevel@tonic-gate 		lwp_info_t	*lwp = list->l_tail;
4867c478bd9Sstevel@tonic-gate 		lwp_info_t	*lwp_tmp;
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate 		fd_closeall();
4897c478bd9Sstevel@tonic-gate 		while (lwp) {
4907c478bd9Sstevel@tonic-gate 			lwp_tmp = lwp;
4917c478bd9Sstevel@tonic-gate 			lwp = lwp->li_prev;
4927c478bd9Sstevel@tonic-gate 			list_remove_lwp(&lwps, lwp_tmp);
4937c478bd9Sstevel@tonic-gate 		}
4947c478bd9Sstevel@tonic-gate 	} else {
4957c478bd9Sstevel@tonic-gate 		id_info_t *id = list->l_head;
4967c478bd9Sstevel@tonic-gate 		id_info_t *nextid;
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate 		while (id) {
4997c478bd9Sstevel@tonic-gate 			nextid = id->id_next;
5007c478bd9Sstevel@tonic-gate 			free(id);
5017c478bd9Sstevel@tonic-gate 			id = nextid;
5027c478bd9Sstevel@tonic-gate 		}
5037c478bd9Sstevel@tonic-gate 		list->l_count = 0;
5047c478bd9Sstevel@tonic-gate 		list->l_head = list->l_tail = NULL;
5057c478bd9Sstevel@tonic-gate 	}
5067c478bd9Sstevel@tonic-gate }
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate static void
5097c478bd9Sstevel@tonic-gate list_update(list_t *list, lwp_info_t *lwp)
5107c478bd9Sstevel@tonic-gate {
5117c478bd9Sstevel@tonic-gate 	id_info_t *id;
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate 	if (list->l_head == NULL) {			/* first element */
5147c478bd9Sstevel@tonic-gate 		list->l_head = list->l_tail = id = Zalloc(sizeof (id_info_t));
5157c478bd9Sstevel@tonic-gate 		goto update;
5167c478bd9Sstevel@tonic-gate 	}
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate 	for (id = list->l_head; id; id = id->id_next) {
5197c478bd9Sstevel@tonic-gate 		if ((list->l_type == LT_USERS) &&
5207c478bd9Sstevel@tonic-gate 		    (id->id_uid != lwp->li_info.pr_uid))
5217c478bd9Sstevel@tonic-gate 			continue;
5227c478bd9Sstevel@tonic-gate 		if ((list->l_type == LT_TASKS) &&
5237c478bd9Sstevel@tonic-gate 		    (id->id_taskid != lwp->li_info.pr_taskid))
5247c478bd9Sstevel@tonic-gate 			continue;
5257c478bd9Sstevel@tonic-gate 		if ((list->l_type == LT_PROJECTS) &&
5267c478bd9Sstevel@tonic-gate 		    (id->id_projid != lwp->li_info.pr_projid))
5277c478bd9Sstevel@tonic-gate 			continue;
5287c478bd9Sstevel@tonic-gate 		if ((list->l_type == LT_ZONES) &&
5297c478bd9Sstevel@tonic-gate 		    (id->id_zoneid != lwp->li_info.pr_zoneid))
5307c478bd9Sstevel@tonic-gate 			continue;
531*c6402783Sakolb 		if ((list->l_type == LT_LGRPS) &&
532*c6402783Sakolb 		    (id->id_lgroup != lwp->li_info.pr_lwp.pr_lgrp))
533*c6402783Sakolb 			continue;
5347c478bd9Sstevel@tonic-gate 		id->id_nproc++;
5357c478bd9Sstevel@tonic-gate 		id->id_taskid	= lwp->li_info.pr_taskid;
5367c478bd9Sstevel@tonic-gate 		id->id_projid	= lwp->li_info.pr_projid;
5377c478bd9Sstevel@tonic-gate 		id->id_zoneid	= lwp->li_info.pr_zoneid;
538*c6402783Sakolb 		id->id_lgroup	= lwp->li_info.pr_lwp.pr_lgrp;
539*c6402783Sakolb 
5407c478bd9Sstevel@tonic-gate 		if (lwp->li_flags & LWP_REPRESENT) {
5417c478bd9Sstevel@tonic-gate 			id->id_size	+= lwp->li_info.pr_size;
5427c478bd9Sstevel@tonic-gate 			id->id_rssize	+= lwp->li_info.pr_rssize;
5437c478bd9Sstevel@tonic-gate 		}
5447c478bd9Sstevel@tonic-gate 		id->id_pctcpu	+= FRC2PCT(lwp->li_info.pr_lwp.pr_pctcpu);
5457c478bd9Sstevel@tonic-gate 		if (opts.o_outpmode & OPT_LWPS)
5467c478bd9Sstevel@tonic-gate 			id->id_time += TIME2SEC(lwp->li_info.pr_lwp.pr_time);
5477c478bd9Sstevel@tonic-gate 		else
5487c478bd9Sstevel@tonic-gate 			id->id_time += TIME2SEC(lwp->li_info.pr_time);
5497c478bd9Sstevel@tonic-gate 		id->id_pctmem	+= FRC2PCT(lwp->li_info.pr_pctmem);
5507c478bd9Sstevel@tonic-gate 		id->id_key	+= lwp->li_key;
5517c478bd9Sstevel@tonic-gate 		total_cpu	+= FRC2PCT(lwp->li_info.pr_lwp.pr_pctcpu);
5527c478bd9Sstevel@tonic-gate 		total_mem	+= FRC2PCT(lwp->li_info.pr_pctmem);
5537c478bd9Sstevel@tonic-gate 		return;
5547c478bd9Sstevel@tonic-gate 	}
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate 	id = list->l_tail;
5577c478bd9Sstevel@tonic-gate 	id->id_next = Zalloc(sizeof (id_info_t));
5587c478bd9Sstevel@tonic-gate 	id->id_next->id_prev = list->l_tail;
5597c478bd9Sstevel@tonic-gate 	id->id_next->id_next = NULL;
5607c478bd9Sstevel@tonic-gate 	list->l_tail = id->id_next;
5617c478bd9Sstevel@tonic-gate 	id = list->l_tail;
5627c478bd9Sstevel@tonic-gate update:
5637c478bd9Sstevel@tonic-gate 	id->id_uid	= lwp->li_info.pr_uid;
5647c478bd9Sstevel@tonic-gate 	id->id_projid	= lwp->li_info.pr_projid;
5657c478bd9Sstevel@tonic-gate 	id->id_taskid	= lwp->li_info.pr_taskid;
5667c478bd9Sstevel@tonic-gate 	id->id_zoneid	= lwp->li_info.pr_zoneid;
567*c6402783Sakolb 	id->id_lgroup	= lwp->li_info.pr_lwp.pr_lgrp;
5687c478bd9Sstevel@tonic-gate 	id->id_nproc++;
5697c478bd9Sstevel@tonic-gate 	if (lwp->li_flags & LWP_REPRESENT) {
5707c478bd9Sstevel@tonic-gate 		id->id_size	= lwp->li_info.pr_size;
5717c478bd9Sstevel@tonic-gate 		id->id_rssize	= lwp->li_info.pr_rssize;
5727c478bd9Sstevel@tonic-gate 	}
5737c478bd9Sstevel@tonic-gate 	id->id_pctcpu	= FRC2PCT(lwp->li_info.pr_lwp.pr_pctcpu);
5747c478bd9Sstevel@tonic-gate 	if (opts.o_outpmode & OPT_LWPS)
5757c478bd9Sstevel@tonic-gate 		id->id_time = TIME2SEC(lwp->li_info.pr_lwp.pr_time);
5767c478bd9Sstevel@tonic-gate 	else
5777c478bd9Sstevel@tonic-gate 		id->id_time = TIME2SEC(lwp->li_info.pr_time);
5787c478bd9Sstevel@tonic-gate 	id->id_pctmem	= FRC2PCT(lwp->li_info.pr_pctmem);
5797c478bd9Sstevel@tonic-gate 	id->id_key	= lwp->li_key;
5807c478bd9Sstevel@tonic-gate 	total_cpu	+= id->id_pctcpu;
5817c478bd9Sstevel@tonic-gate 	total_mem	+= id->id_pctmem;
5827c478bd9Sstevel@tonic-gate 	list->l_count++;
5837c478bd9Sstevel@tonic-gate }
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate static void
5867c478bd9Sstevel@tonic-gate lwp_update(lwp_info_t *lwp, pid_t pid, id_t lwpid, struct prusage *usage)
5877c478bd9Sstevel@tonic-gate {
5887c478bd9Sstevel@tonic-gate 	float period;
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate 	if (!lwpid_is_active(pid, lwpid)) {
5917c478bd9Sstevel@tonic-gate 		/*
5927c478bd9Sstevel@tonic-gate 		 * If we are reading cpu times for the first time then
5937c478bd9Sstevel@tonic-gate 		 * calculate average cpu times based on whole process
5947c478bd9Sstevel@tonic-gate 		 * execution time.
5957c478bd9Sstevel@tonic-gate 		 */
5967c478bd9Sstevel@tonic-gate 		(void) memcpy(&lwp->li_usage, usage, sizeof (prusage_t));
5977c478bd9Sstevel@tonic-gate 		period = TIME2NSEC(usage->pr_rtime);
5987c478bd9Sstevel@tonic-gate 		period = period/(float)100;
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate 		if (period == 0) { /* zombie */
6017c478bd9Sstevel@tonic-gate 			period = 1;
6027c478bd9Sstevel@tonic-gate 			lwp->li_usr = 0;
6037c478bd9Sstevel@tonic-gate 			lwp->li_sys = 0;
6047c478bd9Sstevel@tonic-gate 			lwp->li_slp = 0;
6057c478bd9Sstevel@tonic-gate 		} else {
6067c478bd9Sstevel@tonic-gate 			lwp->li_usr = TIME2NSEC(usage->pr_utime)/period;
6077c478bd9Sstevel@tonic-gate 			lwp->li_sys = TIME2NSEC(usage->pr_stime)/period;
6087c478bd9Sstevel@tonic-gate 			lwp->li_slp = TIME2NSEC(usage->pr_slptime)/period;
6097c478bd9Sstevel@tonic-gate 		}
6107c478bd9Sstevel@tonic-gate 		lwp->li_trp = TIME2NSEC(usage->pr_ttime)/period;
6117c478bd9Sstevel@tonic-gate 		lwp->li_tfl = TIME2NSEC(usage->pr_tftime)/period;
6127c478bd9Sstevel@tonic-gate 		lwp->li_dfl = TIME2NSEC(usage->pr_dftime)/period;
6137c478bd9Sstevel@tonic-gate 		lwp->li_lck = TIME2NSEC(usage->pr_ltime)/period;
6147c478bd9Sstevel@tonic-gate 		lwp->li_lat = TIME2NSEC(usage->pr_wtime)/period;
6157c478bd9Sstevel@tonic-gate 		period = (period / NANOSEC)*(float)100; /* now in seconds */
6167c478bd9Sstevel@tonic-gate 		lwp->li_vcx = (ulong_t)
6177c478bd9Sstevel@tonic-gate 		    (opts.o_interval * (usage->pr_vctx/period));
6187c478bd9Sstevel@tonic-gate 		lwp->li_icx = (ulong_t)
6197c478bd9Sstevel@tonic-gate 		    (opts.o_interval * (usage->pr_ictx/period));
6207c478bd9Sstevel@tonic-gate 		lwp->li_scl = (ulong_t)
6217c478bd9Sstevel@tonic-gate 		    (opts.o_interval * (usage->pr_sysc/period));
6227c478bd9Sstevel@tonic-gate 		lwp->li_sig = (ulong_t)
6237c478bd9Sstevel@tonic-gate 		    (opts.o_interval * (usage->pr_sigs/period));
6247c478bd9Sstevel@tonic-gate 		(void) lwpid_set_active(pid, lwpid);
6257c478bd9Sstevel@tonic-gate 	} else {
6267c478bd9Sstevel@tonic-gate 		/*
6277c478bd9Sstevel@tonic-gate 		 * If this is not a first time we are reading a process's
6287c478bd9Sstevel@tonic-gate 		 * CPU times then recalculate CPU times based on fresh data
6297c478bd9Sstevel@tonic-gate 		 * obtained from procfs and previous CPU time usage values.
6307c478bd9Sstevel@tonic-gate 		 */
6317c478bd9Sstevel@tonic-gate 		period = TIME2NSEC(usage->pr_rtime)-
6327c478bd9Sstevel@tonic-gate 		    TIME2NSEC(lwp->li_usage.pr_rtime);
6337c478bd9Sstevel@tonic-gate 		period = period/(float)100;
6347c478bd9Sstevel@tonic-gate 
6357c478bd9Sstevel@tonic-gate 		if (period == 0) { /* zombie */
6367c478bd9Sstevel@tonic-gate 			period = 1;
6377c478bd9Sstevel@tonic-gate 			lwp->li_usr = 0;
6387c478bd9Sstevel@tonic-gate 			lwp->li_sys = 0;
6397c478bd9Sstevel@tonic-gate 			lwp->li_slp = 0;
6407c478bd9Sstevel@tonic-gate 		} else {
6417c478bd9Sstevel@tonic-gate 			lwp->li_usr = (TIME2NSEC(usage->pr_utime)-
6427c478bd9Sstevel@tonic-gate 			    TIME2NSEC(lwp->li_usage.pr_utime))/period;
6437c478bd9Sstevel@tonic-gate 			lwp->li_sys = (TIME2NSEC(usage->pr_stime) -
6447c478bd9Sstevel@tonic-gate 			    TIME2NSEC(lwp->li_usage.pr_stime))/period;
6457c478bd9Sstevel@tonic-gate 			lwp->li_slp = (TIME2NSEC(usage->pr_slptime) -
6467c478bd9Sstevel@tonic-gate 			    TIME2NSEC(lwp->li_usage.pr_slptime))/period;
6477c478bd9Sstevel@tonic-gate 		}
6487c478bd9Sstevel@tonic-gate 		lwp->li_trp = (TIME2NSEC(usage->pr_ttime) -
6497c478bd9Sstevel@tonic-gate 		    TIME2NSEC(lwp->li_usage.pr_ttime))/period;
6507c478bd9Sstevel@tonic-gate 		lwp->li_tfl = (TIME2NSEC(usage->pr_tftime) -
6517c478bd9Sstevel@tonic-gate 		    TIME2NSEC(lwp->li_usage.pr_tftime))/period;
6527c478bd9Sstevel@tonic-gate 		lwp->li_dfl = (TIME2NSEC(usage->pr_dftime) -
6537c478bd9Sstevel@tonic-gate 		    TIME2NSEC(lwp->li_usage.pr_dftime))/period;
6547c478bd9Sstevel@tonic-gate 		lwp->li_lck = (TIME2NSEC(usage->pr_ltime) -
6557c478bd9Sstevel@tonic-gate 		    TIME2NSEC(lwp->li_usage.pr_ltime))/period;
6567c478bd9Sstevel@tonic-gate 		lwp->li_lat = (TIME2NSEC(usage->pr_wtime) -
6577c478bd9Sstevel@tonic-gate 		    TIME2NSEC(lwp->li_usage.pr_wtime))/period;
6587c478bd9Sstevel@tonic-gate 		lwp->li_vcx = usage->pr_vctx - lwp->li_usage.pr_vctx;
6597c478bd9Sstevel@tonic-gate 		lwp->li_icx = usage->pr_ictx - lwp->li_usage.pr_ictx;
6607c478bd9Sstevel@tonic-gate 		lwp->li_scl = usage->pr_sysc - lwp->li_usage.pr_sysc;
6617c478bd9Sstevel@tonic-gate 		lwp->li_sig = usage->pr_sigs - lwp->li_usage.pr_sigs;
6627c478bd9Sstevel@tonic-gate 		(void) memcpy(&lwp->li_usage, usage, sizeof (prusage_t));
6637c478bd9Sstevel@tonic-gate 	}
6647c478bd9Sstevel@tonic-gate }
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate static int
6677c478bd9Sstevel@tonic-gate read_procfile(fd_t **fd, char *pidstr, char *file, void *buf, size_t bufsize)
6687c478bd9Sstevel@tonic-gate {
6697c478bd9Sstevel@tonic-gate 	char procfile[MAX_PROCFS_PATH];
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate 	(void) snprintf(procfile, MAX_PROCFS_PATH,
6727c478bd9Sstevel@tonic-gate 	    "/proc/%s/%s", pidstr, file);
6737c478bd9Sstevel@tonic-gate 	if ((*fd = fd_open(procfile, O_RDONLY, *fd)) == NULL)
6747c478bd9Sstevel@tonic-gate 		return (1);
6757c478bd9Sstevel@tonic-gate 	if (pread(fd_getfd(*fd), buf, bufsize, 0) != bufsize) {
6767c478bd9Sstevel@tonic-gate 		fd_close(*fd);
6777c478bd9Sstevel@tonic-gate 		return (1);
6787c478bd9Sstevel@tonic-gate 	}
6797c478bd9Sstevel@tonic-gate 	return (0);
6807c478bd9Sstevel@tonic-gate }
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate static void
6837c478bd9Sstevel@tonic-gate add_proc(psinfo_t *psinfo)
6847c478bd9Sstevel@tonic-gate {
6857c478bd9Sstevel@tonic-gate 	lwp_info_t *lwp;
6867c478bd9Sstevel@tonic-gate 	id_t lwpid;
6877c478bd9Sstevel@tonic-gate 	pid_t pid = psinfo->pr_pid;
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate 	lwpid = psinfo->pr_lwp.pr_lwpid;
6907c478bd9Sstevel@tonic-gate 	if ((lwp = lwpid_get(pid, lwpid)) == NULL)
6917c478bd9Sstevel@tonic-gate 		lwp = list_add_lwp(&lwps, pid, lwpid);
6927c478bd9Sstevel@tonic-gate 	lwp->li_flags |= LWP_ALIVE | LWP_REPRESENT;
6937c478bd9Sstevel@tonic-gate 	(void) memcpy(&lwp->li_info, psinfo, sizeof (psinfo_t));
6947c478bd9Sstevel@tonic-gate 	lwp->li_info.pr_lwp.pr_pctcpu = lwp->li_info.pr_pctcpu;
6957c478bd9Sstevel@tonic-gate }
6967c478bd9Sstevel@tonic-gate 
6977c478bd9Sstevel@tonic-gate static void
6987c478bd9Sstevel@tonic-gate add_lwp(psinfo_t *psinfo, lwpsinfo_t *lwpsinfo, int flags)
6997c478bd9Sstevel@tonic-gate {
7007c478bd9Sstevel@tonic-gate 	lwp_info_t *lwp;
7017c478bd9Sstevel@tonic-gate 	pid_t pid = psinfo->pr_pid;
7027c478bd9Sstevel@tonic-gate 	id_t lwpid = lwpsinfo->pr_lwpid;
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate 	if ((lwp = lwpid_get(pid, lwpid)) == NULL)
7057c478bd9Sstevel@tonic-gate 		lwp = list_add_lwp(&lwps, pid, lwpid);
7067c478bd9Sstevel@tonic-gate 	lwp->li_flags &= ~LWP_REPRESENT;
7077c478bd9Sstevel@tonic-gate 	lwp->li_flags |= LWP_ALIVE;
7087c478bd9Sstevel@tonic-gate 	lwp->li_flags |= flags;
7097c478bd9Sstevel@tonic-gate 	(void) memcpy(&lwp->li_info, psinfo,
7107c478bd9Sstevel@tonic-gate 	    sizeof (psinfo_t) - sizeof (lwpsinfo_t));
7117c478bd9Sstevel@tonic-gate 	(void) memcpy(&lwp->li_info.pr_lwp, lwpsinfo, sizeof (lwpsinfo_t));
7127c478bd9Sstevel@tonic-gate }
7137c478bd9Sstevel@tonic-gate 
7147c478bd9Sstevel@tonic-gate static void
7157c478bd9Sstevel@tonic-gate prstat_scandir(DIR *procdir)
7167c478bd9Sstevel@tonic-gate {
7177c478bd9Sstevel@tonic-gate 	char *pidstr;
7187c478bd9Sstevel@tonic-gate 	pid_t pid;
7197c478bd9Sstevel@tonic-gate 	id_t lwpid;
7207c478bd9Sstevel@tonic-gate 	size_t entsz;
7217c478bd9Sstevel@tonic-gate 	long nlwps, nent, i;
7227c478bd9Sstevel@tonic-gate 	char *buf, *ptr;
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate 	fds_t *fds;
7257c478bd9Sstevel@tonic-gate 	lwp_info_t *lwp;
7267c478bd9Sstevel@tonic-gate 	dirent_t *direntp;
7277c478bd9Sstevel@tonic-gate 
7287c478bd9Sstevel@tonic-gate 	prheader_t	header;
7297c478bd9Sstevel@tonic-gate 	psinfo_t	psinfo;
7307c478bd9Sstevel@tonic-gate 	prusage_t	usage;
7317c478bd9Sstevel@tonic-gate 	lwpsinfo_t	*lwpsinfo;
7327c478bd9Sstevel@tonic-gate 	prusage_t	*lwpusage;
7337c478bd9Sstevel@tonic-gate 
7347c478bd9Sstevel@tonic-gate 	total_procs = 0;
7357c478bd9Sstevel@tonic-gate 	total_lwps = 0;
7367c478bd9Sstevel@tonic-gate 	total_cpu = 0;
7377c478bd9Sstevel@tonic-gate 	total_mem = 0;
7387c478bd9Sstevel@tonic-gate 
7397c478bd9Sstevel@tonic-gate 	convert_zone(&zone_tbl);
7407c478bd9Sstevel@tonic-gate 	for (rewinddir(procdir); (direntp = readdir(procdir)); ) {
7417c478bd9Sstevel@tonic-gate 		pidstr = direntp->d_name;
7427c478bd9Sstevel@tonic-gate 		if (pidstr[0] == '.')	/* skip "." and ".."  */
7437c478bd9Sstevel@tonic-gate 			continue;
7447c478bd9Sstevel@tonic-gate 		pid = atoi(pidstr);
7457c478bd9Sstevel@tonic-gate 		if (pid == 0 || pid == 2 || pid == 3)
7467c478bd9Sstevel@tonic-gate 			continue;	/* skip sched, pageout and fsflush */
7477c478bd9Sstevel@tonic-gate 		if (has_element(&pid_tbl, pid) == 0)
7487c478bd9Sstevel@tonic-gate 			continue;	/* check if we really want this pid */
7497c478bd9Sstevel@tonic-gate 		fds = fds_get(pid);	/* get ptr to file descriptors */
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate 		if (read_procfile(&fds->fds_psinfo, pidstr,
7527c478bd9Sstevel@tonic-gate 		    "psinfo", &psinfo, sizeof (psinfo_t)) != 0)
7537c478bd9Sstevel@tonic-gate 			continue;
7547c478bd9Sstevel@tonic-gate 		if (!has_uid(&ruid_tbl, psinfo.pr_uid) ||
7557c478bd9Sstevel@tonic-gate 		    !has_uid(&euid_tbl, psinfo.pr_euid) ||
7567c478bd9Sstevel@tonic-gate 		    !has_element(&prj_tbl, psinfo.pr_projid) ||
7577c478bd9Sstevel@tonic-gate 		    !has_element(&tsk_tbl, psinfo.pr_taskid) ||
7587c478bd9Sstevel@tonic-gate 		    !has_zone(&zone_tbl, psinfo.pr_zoneid)) {
7597c478bd9Sstevel@tonic-gate 			fd_close(fds->fds_psinfo);
7607c478bd9Sstevel@tonic-gate 			continue;
7617c478bd9Sstevel@tonic-gate 		}
7627c478bd9Sstevel@tonic-gate 		nlwps = psinfo.pr_nlwp + psinfo.pr_nzomb;
7637c478bd9Sstevel@tonic-gate 
7647c478bd9Sstevel@tonic-gate 		if (nlwps > 1 && (opts.o_outpmode & (OPT_LWPS | OPT_PSETS))) {
7657c478bd9Sstevel@tonic-gate 			int rep_lwp = 0;
7667c478bd9Sstevel@tonic-gate 
7677c478bd9Sstevel@tonic-gate 			if (read_procfile(&fds->fds_lpsinfo, pidstr, "lpsinfo",
7687c478bd9Sstevel@tonic-gate 			    &header, sizeof (prheader_t)) != 0) {
7697c478bd9Sstevel@tonic-gate 				fd_close(fds->fds_psinfo);
7707c478bd9Sstevel@tonic-gate 				continue;
7717c478bd9Sstevel@tonic-gate 			}
7727c478bd9Sstevel@tonic-gate 
7737c478bd9Sstevel@tonic-gate 			nent = header.pr_nent;
7747c478bd9Sstevel@tonic-gate 			entsz = header.pr_entsize * nent;
7757c478bd9Sstevel@tonic-gate 			ptr = buf = Malloc(entsz);
7767c478bd9Sstevel@tonic-gate 			if (pread(fd_getfd(fds->fds_lpsinfo), buf,
7777c478bd9Sstevel@tonic-gate 			    entsz, sizeof (struct prheader)) != entsz) {
7787c478bd9Sstevel@tonic-gate 				fd_close(fds->fds_lpsinfo);
7797c478bd9Sstevel@tonic-gate 				fd_close(fds->fds_psinfo);
7807c478bd9Sstevel@tonic-gate 				free(buf);
7817c478bd9Sstevel@tonic-gate 				continue;
7827c478bd9Sstevel@tonic-gate 			}
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate 			nlwps = 0;
7857c478bd9Sstevel@tonic-gate 			for (i = 0; i < nent; i++, ptr += header.pr_entsize) {
7867c478bd9Sstevel@tonic-gate 				/*LINTED ALIGNMENT*/
7877c478bd9Sstevel@tonic-gate 				lwpsinfo = (lwpsinfo_t *)ptr;
7887c478bd9Sstevel@tonic-gate 				if (!has_element(&cpu_tbl,
7897c478bd9Sstevel@tonic-gate 				    lwpsinfo->pr_onpro) ||
7907c478bd9Sstevel@tonic-gate 				    !has_element(&set_tbl,
791*c6402783Sakolb 				    lwpsinfo->pr_bindpset) ||
792*c6402783Sakolb 				    !has_element(&lgr_tbl,
793*c6402783Sakolb 					lwpsinfo->pr_lgrp))
7947c478bd9Sstevel@tonic-gate 					continue;
7957c478bd9Sstevel@tonic-gate 				nlwps++;
7967c478bd9Sstevel@tonic-gate 				if ((opts.o_outpmode & (OPT_PSETS | OPT_LWPS))
7977c478bd9Sstevel@tonic-gate 				    == OPT_PSETS) {
7987c478bd9Sstevel@tonic-gate 					/*
7997c478bd9Sstevel@tonic-gate 					 * If one of process's LWPs is bound
8007c478bd9Sstevel@tonic-gate 					 * to a given processor set, report the
8017c478bd9Sstevel@tonic-gate 					 * whole process.  We may be doing this
8027c478bd9Sstevel@tonic-gate 					 * a few times but we'll get an accurate
8037c478bd9Sstevel@tonic-gate 					 * lwp count in return.
8047c478bd9Sstevel@tonic-gate 					 */
8057c478bd9Sstevel@tonic-gate 					add_proc(&psinfo);
8067c478bd9Sstevel@tonic-gate 				} else {
8077c478bd9Sstevel@tonic-gate 					if (rep_lwp == 0) {
8087c478bd9Sstevel@tonic-gate 						rep_lwp = 1;
8097c478bd9Sstevel@tonic-gate 						add_lwp(&psinfo, lwpsinfo,
8107c478bd9Sstevel@tonic-gate 						    LWP_REPRESENT);
8117c478bd9Sstevel@tonic-gate 					} else {
8127c478bd9Sstevel@tonic-gate 						add_lwp(&psinfo, lwpsinfo, 0);
8137c478bd9Sstevel@tonic-gate 					}
8147c478bd9Sstevel@tonic-gate 				}
8157c478bd9Sstevel@tonic-gate 			}
8167c478bd9Sstevel@tonic-gate 			free(buf);
8177c478bd9Sstevel@tonic-gate 			if (nlwps == 0) {
8187c478bd9Sstevel@tonic-gate 				fd_close(fds->fds_lpsinfo);
8197c478bd9Sstevel@tonic-gate 				fd_close(fds->fds_psinfo);
8207c478bd9Sstevel@tonic-gate 				continue;
8217c478bd9Sstevel@tonic-gate 			}
8227c478bd9Sstevel@tonic-gate 		} else {
8237c478bd9Sstevel@tonic-gate 			if (!has_element(&cpu_tbl, psinfo.pr_lwp.pr_onpro) ||
824*c6402783Sakolb 			    !has_element(&set_tbl, psinfo.pr_lwp.pr_bindpset) ||
825*c6402783Sakolb 			    !has_element(&lgr_tbl, psinfo.pr_lwp.pr_lgrp)) {
8267c478bd9Sstevel@tonic-gate 				fd_close(fds->fds_psinfo);
8277c478bd9Sstevel@tonic-gate 				continue;
8287c478bd9Sstevel@tonic-gate 			}
8297c478bd9Sstevel@tonic-gate 			add_proc(&psinfo);
8307c478bd9Sstevel@tonic-gate 		}
8317c478bd9Sstevel@tonic-gate 		if (!(opts.o_outpmode & OPT_MSACCT)) {
8327c478bd9Sstevel@tonic-gate 			total_procs++;
8337c478bd9Sstevel@tonic-gate 			total_lwps += nlwps;
8347c478bd9Sstevel@tonic-gate 			continue;
8357c478bd9Sstevel@tonic-gate 		}
8367c478bd9Sstevel@tonic-gate 		/*
8377c478bd9Sstevel@tonic-gate 		 * Get more information about processes from /proc/pid/usage.
8387c478bd9Sstevel@tonic-gate 		 * If process has more than one lwp, then we may have to
8397c478bd9Sstevel@tonic-gate 		 * also look at the /proc/pid/lusage file.
8407c478bd9Sstevel@tonic-gate 		 */
8417c478bd9Sstevel@tonic-gate 		if ((opts.o_outpmode & OPT_LWPS) && (nlwps > 1)) {
8427c478bd9Sstevel@tonic-gate 			if (read_procfile(&fds->fds_lusage, pidstr, "lusage",
8437c478bd9Sstevel@tonic-gate 			    &header, sizeof (prheader_t)) != 0) {
8447c478bd9Sstevel@tonic-gate 				fd_close(fds->fds_lpsinfo);
8457c478bd9Sstevel@tonic-gate 				fd_close(fds->fds_psinfo);
8467c478bd9Sstevel@tonic-gate 				continue;
8477c478bd9Sstevel@tonic-gate 			}
8487c478bd9Sstevel@tonic-gate 			nent = header.pr_nent;
8497c478bd9Sstevel@tonic-gate 			entsz = header.pr_entsize * nent;
8507c478bd9Sstevel@tonic-gate 			buf = Malloc(entsz);
8517c478bd9Sstevel@tonic-gate 			if (pread(fd_getfd(fds->fds_lusage), buf,
8527c478bd9Sstevel@tonic-gate 			    entsz, sizeof (struct prheader)) != entsz) {
8537c478bd9Sstevel@tonic-gate 				fd_close(fds->fds_lusage);
8547c478bd9Sstevel@tonic-gate 				fd_close(fds->fds_lpsinfo);
8557c478bd9Sstevel@tonic-gate 				fd_close(fds->fds_psinfo);
8567c478bd9Sstevel@tonic-gate 				free(buf);
8577c478bd9Sstevel@tonic-gate 				continue;
8587c478bd9Sstevel@tonic-gate 			}
8597c478bd9Sstevel@tonic-gate 			for (i = 1, ptr = buf + header.pr_entsize; i < nent;
8607c478bd9Sstevel@tonic-gate 			    i++, ptr += header.pr_entsize) {
8617c478bd9Sstevel@tonic-gate 				/*LINTED ALIGNMENT*/
8627c478bd9Sstevel@tonic-gate 				lwpusage = (prusage_t *)ptr;
8637c478bd9Sstevel@tonic-gate 				lwpid = lwpusage->pr_lwpid;
8647c478bd9Sstevel@tonic-gate 				/*
8657c478bd9Sstevel@tonic-gate 				 * New LWPs created after we read lpsinfo
8667c478bd9Sstevel@tonic-gate 				 * will be ignored.  Don't want to do
8677c478bd9Sstevel@tonic-gate 				 * everything all over again.
8687c478bd9Sstevel@tonic-gate 				 */
8697c478bd9Sstevel@tonic-gate 				if ((lwp = lwpid_get(pid, lwpid)) == NULL)
8707c478bd9Sstevel@tonic-gate 					continue;
8717c478bd9Sstevel@tonic-gate 				lwp_update(lwp, pid, lwpid, lwpusage);
8727c478bd9Sstevel@tonic-gate 			}
8737c478bd9Sstevel@tonic-gate 			free(buf);
8747c478bd9Sstevel@tonic-gate 		} else {
8757c478bd9Sstevel@tonic-gate 			if (read_procfile(&fds->fds_usage, pidstr, "usage",
8767c478bd9Sstevel@tonic-gate 			    &usage, sizeof (prusage_t)) != 0) {
8777c478bd9Sstevel@tonic-gate 				fd_close(fds->fds_lpsinfo);
8787c478bd9Sstevel@tonic-gate 				fd_close(fds->fds_psinfo);
8797c478bd9Sstevel@tonic-gate 				continue;
8807c478bd9Sstevel@tonic-gate 			}
8817c478bd9Sstevel@tonic-gate 			lwpid = psinfo.pr_lwp.pr_lwpid;
8827c478bd9Sstevel@tonic-gate 			if ((lwp = lwpid_get(pid, lwpid)) == NULL)
8837c478bd9Sstevel@tonic-gate 				continue;
8847c478bd9Sstevel@tonic-gate 			lwp_update(lwp, pid, lwpid, &usage);
8857c478bd9Sstevel@tonic-gate 		}
8867c478bd9Sstevel@tonic-gate 		total_procs++;
8877c478bd9Sstevel@tonic-gate 		total_lwps += nlwps;
8887c478bd9Sstevel@tonic-gate 	}
8897c478bd9Sstevel@tonic-gate 	fd_update();
8907c478bd9Sstevel@tonic-gate }
8917c478bd9Sstevel@tonic-gate 
8927c478bd9Sstevel@tonic-gate /*
8937c478bd9Sstevel@tonic-gate  * This procedure removes all dead lwps from the linked list of all lwps.
8947c478bd9Sstevel@tonic-gate  * It also creates linked list of ids if necessary.
8957c478bd9Sstevel@tonic-gate  */
8967c478bd9Sstevel@tonic-gate static void
8977c478bd9Sstevel@tonic-gate list_refresh(list_t *list)
8987c478bd9Sstevel@tonic-gate {
8997c478bd9Sstevel@tonic-gate 	lwp_info_t *lwp, *lwp_next;
9007c478bd9Sstevel@tonic-gate 
9017c478bd9Sstevel@tonic-gate 	if (!(list->l_type & LT_LWPS))
9027c478bd9Sstevel@tonic-gate 		return;
9037c478bd9Sstevel@tonic-gate 
9047c478bd9Sstevel@tonic-gate 	for (lwp = list->l_head; lwp != NULL; ) {
9057c478bd9Sstevel@tonic-gate 		if (lwp->li_flags & LWP_ALIVE) {
9067c478bd9Sstevel@tonic-gate 			/*
9077c478bd9Sstevel@tonic-gate 			 * Process all live LWPs.
9087c478bd9Sstevel@tonic-gate 			 * When we're done, mark them as dead.
9097c478bd9Sstevel@tonic-gate 			 * They will be marked "alive" on the next
9107c478bd9Sstevel@tonic-gate 			 * /proc scan if they still exist.
9117c478bd9Sstevel@tonic-gate 			 */
9127c478bd9Sstevel@tonic-gate 			lwp->li_key = list_getkeyval(list, lwp);
9137c478bd9Sstevel@tonic-gate 			if (opts.o_outpmode & OPT_USERS)
9147c478bd9Sstevel@tonic-gate 				list_update(&users, lwp);
9157c478bd9Sstevel@tonic-gate 			if (opts.o_outpmode & OPT_TASKS)
9167c478bd9Sstevel@tonic-gate 				list_update(&tasks, lwp);
9177c478bd9Sstevel@tonic-gate 			if (opts.o_outpmode & OPT_PROJECTS)
9187c478bd9Sstevel@tonic-gate 				list_update(&projects, lwp);
9197c478bd9Sstevel@tonic-gate 			if (opts.o_outpmode & OPT_ZONES)
9207c478bd9Sstevel@tonic-gate 				list_update(&zones, lwp);
921*c6402783Sakolb 			if (opts.o_outpmode & OPT_LGRP)
922*c6402783Sakolb 				list_update(&lgroups, lwp);
9237c478bd9Sstevel@tonic-gate 			lwp->li_flags &= ~LWP_ALIVE;
9247c478bd9Sstevel@tonic-gate 			lwp = lwp->li_next;
9257c478bd9Sstevel@tonic-gate 
9267c478bd9Sstevel@tonic-gate 		} else {
9277c478bd9Sstevel@tonic-gate 			lwp_next = lwp->li_next;
9287c478bd9Sstevel@tonic-gate 			list_remove_lwp(&lwps, lwp);
9297c478bd9Sstevel@tonic-gate 			lwp = lwp_next;
9307c478bd9Sstevel@tonic-gate 		}
9317c478bd9Sstevel@tonic-gate 	}
9327c478bd9Sstevel@tonic-gate }
9337c478bd9Sstevel@tonic-gate 
9347c478bd9Sstevel@tonic-gate static void
9357c478bd9Sstevel@tonic-gate curses_on()
9367c478bd9Sstevel@tonic-gate {
9377c478bd9Sstevel@tonic-gate 	if ((opts.o_outpmode & OPT_TERMCAP) && (is_curses_on == FALSE)) {
9387c478bd9Sstevel@tonic-gate 		(void) initscr();
9397c478bd9Sstevel@tonic-gate 		(void) nonl();
9407c478bd9Sstevel@tonic-gate 		(void) putp(t_smcup);
9417c478bd9Sstevel@tonic-gate 		is_curses_on = TRUE;
9427c478bd9Sstevel@tonic-gate 	}
9437c478bd9Sstevel@tonic-gate }
9447c478bd9Sstevel@tonic-gate 
9457c478bd9Sstevel@tonic-gate static void
9467c478bd9Sstevel@tonic-gate curses_off()
9477c478bd9Sstevel@tonic-gate {
9487c478bd9Sstevel@tonic-gate 	if ((is_curses_on == TRUE) && (opts.o_outpmode & OPT_TERMCAP)) {
9497c478bd9Sstevel@tonic-gate 		(void) putp(t_rmcup);
9507c478bd9Sstevel@tonic-gate 		(void) endwin();
9517c478bd9Sstevel@tonic-gate 		is_curses_on = FALSE;
9527c478bd9Sstevel@tonic-gate 	}
9537c478bd9Sstevel@tonic-gate 	(void) fflush(stdout);
9547c478bd9Sstevel@tonic-gate }
9557c478bd9Sstevel@tonic-gate 
9567c478bd9Sstevel@tonic-gate static int
9577c478bd9Sstevel@tonic-gate nlines()
9587c478bd9Sstevel@tonic-gate {
9597c478bd9Sstevel@tonic-gate 	struct winsize ws;
9607c478bd9Sstevel@tonic-gate 	char *envp;
9617c478bd9Sstevel@tonic-gate 	int n;
9627c478bd9Sstevel@tonic-gate 	if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) != -1) {
9637c478bd9Sstevel@tonic-gate 		if (ws.ws_row > 0)
9647c478bd9Sstevel@tonic-gate 			return (ws.ws_row);
9657c478bd9Sstevel@tonic-gate 	}
9667c478bd9Sstevel@tonic-gate 	if (envp = getenv("LINES")) {
9677c478bd9Sstevel@tonic-gate 		if ((n = Atoi(envp)) > 0) {
9687c478bd9Sstevel@tonic-gate 			opts.o_outpmode &= ~OPT_USEHOME;
9697c478bd9Sstevel@tonic-gate 			return (n);
9707c478bd9Sstevel@tonic-gate 		}
9717c478bd9Sstevel@tonic-gate 	}
9727c478bd9Sstevel@tonic-gate 	return (-1);
9737c478bd9Sstevel@tonic-gate }
9747c478bd9Sstevel@tonic-gate 
9757c478bd9Sstevel@tonic-gate static void
9767c478bd9Sstevel@tonic-gate setmovecur()
9777c478bd9Sstevel@tonic-gate {
9787c478bd9Sstevel@tonic-gate 	int i, n;
9797c478bd9Sstevel@tonic-gate 	if ((opts.o_outpmode & OPT_FULLSCREEN) &&
9807c478bd9Sstevel@tonic-gate 	    (opts.o_outpmode & OPT_USEHOME)) {
9817c478bd9Sstevel@tonic-gate 		movecur = t_home;
9827c478bd9Sstevel@tonic-gate 		return;
9837c478bd9Sstevel@tonic-gate 	}
9847c478bd9Sstevel@tonic-gate 	if (opts.o_outpmode & OPT_SPLIT) {
9857c478bd9Sstevel@tonic-gate 		n = opts.o_ntop + opts.o_nbottom + 2;
9867c478bd9Sstevel@tonic-gate 	} else {
9877c478bd9Sstevel@tonic-gate 		if (opts.o_outpmode & OPT_USERS)
9887c478bd9Sstevel@tonic-gate 			n = opts.o_nbottom + 1;
9897c478bd9Sstevel@tonic-gate 		else
9907c478bd9Sstevel@tonic-gate 			n = opts.o_ntop + 1;
9917c478bd9Sstevel@tonic-gate 	}
9927c478bd9Sstevel@tonic-gate 	if (movecur != NULL && movecur != empty_string && movecur != t_home)
9937c478bd9Sstevel@tonic-gate 		free(movecur);
9947c478bd9Sstevel@tonic-gate 	movecur = Zalloc(strlen(t_up) * (n + 5));
9957c478bd9Sstevel@tonic-gate 	for (i = 0; i <= n; i++)
9967c478bd9Sstevel@tonic-gate 		(void) strcat(movecur, t_up);
9977c478bd9Sstevel@tonic-gate }
9987c478bd9Sstevel@tonic-gate 
9997c478bd9Sstevel@tonic-gate static int
10007c478bd9Sstevel@tonic-gate setsize()
10017c478bd9Sstevel@tonic-gate {
10027c478bd9Sstevel@tonic-gate 	static int oldn = 0;
10037c478bd9Sstevel@tonic-gate 	int n;
10047c478bd9Sstevel@tonic-gate 
10057c478bd9Sstevel@tonic-gate 	if (opts.o_outpmode & OPT_FULLSCREEN) {
10067c478bd9Sstevel@tonic-gate 		n = nlines();
10077c478bd9Sstevel@tonic-gate 		if (n == oldn)
10087c478bd9Sstevel@tonic-gate 			return (0);
10097c478bd9Sstevel@tonic-gate 		oldn = n;
10107c478bd9Sstevel@tonic-gate 		if (n == -1) {
10117c478bd9Sstevel@tonic-gate 			opts.o_outpmode &= ~OPT_USEHOME;
10127c478bd9Sstevel@tonic-gate 			setmovecur();		/* set default window size */
10137c478bd9Sstevel@tonic-gate 			return (1);
10147c478bd9Sstevel@tonic-gate 		}
10157c478bd9Sstevel@tonic-gate 		n = n - 3;	/* minus header, total and cursor lines */
10167c478bd9Sstevel@tonic-gate 		if (n < 1)
10177c478bd9Sstevel@tonic-gate 			Die(gettext("window is too small (try -n)\n"));
10187c478bd9Sstevel@tonic-gate 		if (opts.o_outpmode & OPT_SPLIT) {
10197c478bd9Sstevel@tonic-gate 			if (n < 8) {
10207c478bd9Sstevel@tonic-gate 				Die(gettext("window is too small (try -n)\n"));
10217c478bd9Sstevel@tonic-gate 			} else {
10227c478bd9Sstevel@tonic-gate 				opts.o_ntop = (n / 4) * 3;
10237c478bd9Sstevel@tonic-gate 				opts.o_nbottom = n - 1 - opts.o_ntop;
10247c478bd9Sstevel@tonic-gate 			}
10257c478bd9Sstevel@tonic-gate 		} else {
10267c478bd9Sstevel@tonic-gate 			if (opts.o_outpmode & OPT_USERS)
10277c478bd9Sstevel@tonic-gate 				opts.o_nbottom = n;
10287c478bd9Sstevel@tonic-gate 			else
10297c478bd9Sstevel@tonic-gate 				opts.o_ntop = n;
10307c478bd9Sstevel@tonic-gate 		}
10317c478bd9Sstevel@tonic-gate 	}
10327c478bd9Sstevel@tonic-gate 	setmovecur();
10337c478bd9Sstevel@tonic-gate 	return (1);
10347c478bd9Sstevel@tonic-gate }
10357c478bd9Sstevel@tonic-gate 
10367c478bd9Sstevel@tonic-gate static void
10377c478bd9Sstevel@tonic-gate ldtermcap()
10387c478bd9Sstevel@tonic-gate {
10397c478bd9Sstevel@tonic-gate 	int err;
10407c478bd9Sstevel@tonic-gate 	if (setupterm(NULL, STDIN_FILENO, &err) == ERR) {
10417c478bd9Sstevel@tonic-gate 		switch (err) {
10427c478bd9Sstevel@tonic-gate 		case 0:
10437c478bd9Sstevel@tonic-gate 			Warn(gettext("failed to load terminal info, "
10447c478bd9Sstevel@tonic-gate 			    "defaulting to -c option\n"));
10457c478bd9Sstevel@tonic-gate 			break;
10467c478bd9Sstevel@tonic-gate 		case -1:
10477c478bd9Sstevel@tonic-gate 			Warn(gettext("terminfo database not found, "
10487c478bd9Sstevel@tonic-gate 			    "defaulting to -c option\n"));
10497c478bd9Sstevel@tonic-gate 			break;
10507c478bd9Sstevel@tonic-gate 		default:
10517c478bd9Sstevel@tonic-gate 			Warn(gettext("failed to initialize terminal, "
10527c478bd9Sstevel@tonic-gate 			    "defaulting to -c option\n"));
10537c478bd9Sstevel@tonic-gate 		}
10547c478bd9Sstevel@tonic-gate 		opts.o_outpmode &= ~OPT_TERMCAP;
10557c478bd9Sstevel@tonic-gate 		t_up = t_eol = t_smcup = t_rmcup = movecur = empty_string;
10567c478bd9Sstevel@tonic-gate 		t_ulon = t_uloff = empty_string;
10577c478bd9Sstevel@tonic-gate 		return;
10587c478bd9Sstevel@tonic-gate 	}
10597c478bd9Sstevel@tonic-gate 	t_ulon	= tigetstr("smul");
10607c478bd9Sstevel@tonic-gate 	t_uloff	= tigetstr("rmul");
10617c478bd9Sstevel@tonic-gate 	t_up	= tigetstr("cuu1");
10627c478bd9Sstevel@tonic-gate 	t_eol	= tigetstr("el");
10637c478bd9Sstevel@tonic-gate 	t_smcup	= tigetstr("smcup");
10647c478bd9Sstevel@tonic-gate 	t_rmcup = tigetstr("rmcup");
10657c478bd9Sstevel@tonic-gate 	t_home  = tigetstr("home");
10667c478bd9Sstevel@tonic-gate 	if ((t_up == (char *)-1) || (t_eol == (char *)-1) ||
10677c478bd9Sstevel@tonic-gate 	    (t_smcup == (char *)-1) || (t_rmcup == (char *)-1)) {
10687c478bd9Sstevel@tonic-gate 		opts.o_outpmode &= ~OPT_TERMCAP;
10697c478bd9Sstevel@tonic-gate 		t_up = t_eol = t_smcup = t_rmcup = movecur = empty_string;
10707c478bd9Sstevel@tonic-gate 		return;
10717c478bd9Sstevel@tonic-gate 	}
10727c478bd9Sstevel@tonic-gate 	if (t_up == NULL || t_eol == NULL) {
10737c478bd9Sstevel@tonic-gate 		opts.o_outpmode &= ~OPT_TERMCAP;
10747c478bd9Sstevel@tonic-gate 		t_eol = t_up = movecur = empty_string;
10757c478bd9Sstevel@tonic-gate 		return;
10767c478bd9Sstevel@tonic-gate 	}
10777c478bd9Sstevel@tonic-gate 	if (t_ulon == (char *)-1 || t_uloff == (char *)-1 ||
10787c478bd9Sstevel@tonic-gate 	    t_ulon == NULL || t_uloff == NULL) {
10797c478bd9Sstevel@tonic-gate 		t_ulon = t_uloff = empty_string;  /* can live without it */
10807c478bd9Sstevel@tonic-gate 	}
10817c478bd9Sstevel@tonic-gate 	if (t_smcup == NULL || t_rmcup == NULL)
10827c478bd9Sstevel@tonic-gate 		t_smcup = t_rmcup = empty_string;
10837c478bd9Sstevel@tonic-gate 	if (t_home == (char *)-1 || t_home == NULL) {
10847c478bd9Sstevel@tonic-gate 		opts.o_outpmode &= ~OPT_USEHOME;
10857c478bd9Sstevel@tonic-gate 		t_home = empty_string;
10867c478bd9Sstevel@tonic-gate 	}
10877c478bd9Sstevel@tonic-gate }
10887c478bd9Sstevel@tonic-gate 
10897c478bd9Sstevel@tonic-gate static void
10907c478bd9Sstevel@tonic-gate sig_handler(int sig)
10917c478bd9Sstevel@tonic-gate {
10927c478bd9Sstevel@tonic-gate 	switch (sig) {
10937c478bd9Sstevel@tonic-gate 	case SIGTSTP:	sigtstp = 1;
10947c478bd9Sstevel@tonic-gate 			break;
10957c478bd9Sstevel@tonic-gate 	case SIGWINCH:	sigwinch = 1;
10967c478bd9Sstevel@tonic-gate 			break;
10977c478bd9Sstevel@tonic-gate 	case SIGINT:
10987c478bd9Sstevel@tonic-gate 	case SIGTERM:	sigterm = 1;
10997c478bd9Sstevel@tonic-gate 			break;
11007c478bd9Sstevel@tonic-gate 	}
11017c478bd9Sstevel@tonic-gate }
11027c478bd9Sstevel@tonic-gate 
11037c478bd9Sstevel@tonic-gate static void
11047c478bd9Sstevel@tonic-gate set_signals()
11057c478bd9Sstevel@tonic-gate {
11067c478bd9Sstevel@tonic-gate 	(void) signal(SIGTSTP, sig_handler);
11077c478bd9Sstevel@tonic-gate 	(void) signal(SIGINT, sig_handler);
11087c478bd9Sstevel@tonic-gate 	(void) signal(SIGTERM, sig_handler);
11097c478bd9Sstevel@tonic-gate 	if (opts.o_outpmode & OPT_FULLSCREEN)
11107c478bd9Sstevel@tonic-gate 		(void) signal(SIGWINCH, sig_handler);
11117c478bd9Sstevel@tonic-gate }
11127c478bd9Sstevel@tonic-gate 
11137c478bd9Sstevel@tonic-gate static void
1114a851ab35Svb160487 fill_table(table_t *table, char *arg, char option)
11157c478bd9Sstevel@tonic-gate {
11167c478bd9Sstevel@tonic-gate 	char *p = strtok(arg, ", ");
11177c478bd9Sstevel@tonic-gate 
1118a851ab35Svb160487 	if (p == NULL)
1119a851ab35Svb160487 		Die(gettext("invalid argument for -%c\n"), option);
1120a851ab35Svb160487 
1121a851ab35Svb160487 	add_element(table, (long)Atoi(p));
1122a851ab35Svb160487 	while (p = strtok(NULL, ", "))
1123a851ab35Svb160487 		add_element(table, (long)Atoi(p));
11247c478bd9Sstevel@tonic-gate }
11257c478bd9Sstevel@tonic-gate 
11267c478bd9Sstevel@tonic-gate static void
11277c478bd9Sstevel@tonic-gate fill_prj_table(char *arg)
11287c478bd9Sstevel@tonic-gate {
11297c478bd9Sstevel@tonic-gate 	projid_t projid;
11307c478bd9Sstevel@tonic-gate 	char *p = strtok(arg, ", ");
11317c478bd9Sstevel@tonic-gate 
1132a851ab35Svb160487 	if (p == NULL)
1133a851ab35Svb160487 		Die(gettext("invalid argument for -j\n"));
1134a851ab35Svb160487 
11357c478bd9Sstevel@tonic-gate 	if ((projid = getprojidbyname(p)) == -1)
11367c478bd9Sstevel@tonic-gate 		projid = Atoi(p);
11377c478bd9Sstevel@tonic-gate 	add_element(&prj_tbl, (long)projid);
11387c478bd9Sstevel@tonic-gate 
11397c478bd9Sstevel@tonic-gate 	while (p = strtok(NULL, ", ")) {
11407c478bd9Sstevel@tonic-gate 		if ((projid = getprojidbyname(p)) == -1)
11417c478bd9Sstevel@tonic-gate 			projid = Atoi(p);
11427c478bd9Sstevel@tonic-gate 		add_element(&prj_tbl, (long)projid);
11437c478bd9Sstevel@tonic-gate 	}
11447c478bd9Sstevel@tonic-gate }
11457c478bd9Sstevel@tonic-gate 
11467c478bd9Sstevel@tonic-gate static void
11477c478bd9Sstevel@tonic-gate fill_set_table(char *arg)
11487c478bd9Sstevel@tonic-gate {
11497c478bd9Sstevel@tonic-gate 	char *p = strtok(arg, ", ");
11507c478bd9Sstevel@tonic-gate 	psetid_t id;
11517c478bd9Sstevel@tonic-gate 
1152a851ab35Svb160487 	if (p == NULL)
1153a851ab35Svb160487 		Die(gettext("invalid argument for -C\n"));
1154a851ab35Svb160487 
11557c478bd9Sstevel@tonic-gate 	if ((id = Atoi(p)) == 0)
11567c478bd9Sstevel@tonic-gate 		id = PS_NONE;
11577c478bd9Sstevel@tonic-gate 	add_element(&set_tbl, id);
11587c478bd9Sstevel@tonic-gate 	while (p = strtok(NULL, ", ")) {
11597c478bd9Sstevel@tonic-gate 		if ((id = Atoi(p)) == 0)
11607c478bd9Sstevel@tonic-gate 			id = PS_NONE;
11617c478bd9Sstevel@tonic-gate 		if (!has_element(&set_tbl, id))
11627c478bd9Sstevel@tonic-gate 			add_element(&set_tbl, id);
11637c478bd9Sstevel@tonic-gate 	}
11647c478bd9Sstevel@tonic-gate }
11657c478bd9Sstevel@tonic-gate 
11667c478bd9Sstevel@tonic-gate static void
11677c478bd9Sstevel@tonic-gate Exit()
11687c478bd9Sstevel@tonic-gate {
11697c478bd9Sstevel@tonic-gate 	curses_off();
11707c478bd9Sstevel@tonic-gate 	list_clear(&lwps);
11717c478bd9Sstevel@tonic-gate 	list_clear(&users);
11727c478bd9Sstevel@tonic-gate 	list_clear(&tasks);
11737c478bd9Sstevel@tonic-gate 	list_clear(&projects);
11747c478bd9Sstevel@tonic-gate 	list_clear(&zones);
11757c478bd9Sstevel@tonic-gate 	fd_exit();
11767c478bd9Sstevel@tonic-gate }
11777c478bd9Sstevel@tonic-gate 
11787c478bd9Sstevel@tonic-gate int
11797c478bd9Sstevel@tonic-gate main(int argc, char **argv)
11807c478bd9Sstevel@tonic-gate {
11817c478bd9Sstevel@tonic-gate 	DIR *procdir;
11827c478bd9Sstevel@tonic-gate 	char *p;
11837c478bd9Sstevel@tonic-gate 	char *sortk = "cpu";	/* default sort key */
11847c478bd9Sstevel@tonic-gate 	int opt;
11857c478bd9Sstevel@tonic-gate 	int timeout;
11867c478bd9Sstevel@tonic-gate 	struct pollfd pollset;
11877c478bd9Sstevel@tonic-gate 	char key;
11887c478bd9Sstevel@tonic-gate 
11897c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
11907c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
11917c478bd9Sstevel@tonic-gate 	Progname(argv[0]);
11927c478bd9Sstevel@tonic-gate 	lwpid_init();
11937c478bd9Sstevel@tonic-gate 	fd_init(Setrlimit());
11947c478bd9Sstevel@tonic-gate 
1195*c6402783Sakolb 	while ((opt = getopt(argc, argv, "vcHmaRLtu:U:n:p:C:P:h:s:S:j:k:TJz:Z"))
11967c478bd9Sstevel@tonic-gate 	    != (int)EOF) {
11977c478bd9Sstevel@tonic-gate 		switch (opt) {
11987c478bd9Sstevel@tonic-gate 		case 'R':
11997c478bd9Sstevel@tonic-gate 			opts.o_outpmode |= OPT_REALTIME;
12007c478bd9Sstevel@tonic-gate 			break;
12017c478bd9Sstevel@tonic-gate 		case 'c':
12027c478bd9Sstevel@tonic-gate 			opts.o_outpmode &= ~OPT_TERMCAP;
12037c478bd9Sstevel@tonic-gate 			opts.o_outpmode &= ~OPT_FULLSCREEN;
12047c478bd9Sstevel@tonic-gate 			break;
1205*c6402783Sakolb 		case 'h':
1206*c6402783Sakolb 			fill_table(&lgr_tbl, optarg, 'h');
1207*c6402783Sakolb 			break;
1208*c6402783Sakolb 		case 'H':
1209*c6402783Sakolb 			opts.o_outpmode |= OPT_LGRP;
1210*c6402783Sakolb 			break;
12117c478bd9Sstevel@tonic-gate 		case 'm':
12127c478bd9Sstevel@tonic-gate 		case 'v':
12137c478bd9Sstevel@tonic-gate 			opts.o_outpmode &= ~OPT_PSINFO;
12147c478bd9Sstevel@tonic-gate 			opts.o_outpmode |=  OPT_MSACCT;
12157c478bd9Sstevel@tonic-gate 			break;
12167c478bd9Sstevel@tonic-gate 		case 't':
12177c478bd9Sstevel@tonic-gate 			opts.o_outpmode &= ~OPT_PSINFO;
12187c478bd9Sstevel@tonic-gate 			opts.o_outpmode |= OPT_USERS;
12197c478bd9Sstevel@tonic-gate 			break;
12207c478bd9Sstevel@tonic-gate 		case 'a':
12217c478bd9Sstevel@tonic-gate 			opts.o_outpmode |= OPT_SPLIT | OPT_USERS;
12227c478bd9Sstevel@tonic-gate 			break;
12237c478bd9Sstevel@tonic-gate 		case 'T':
12247c478bd9Sstevel@tonic-gate 			opts.o_outpmode |= OPT_SPLIT | OPT_TASKS;
12257c478bd9Sstevel@tonic-gate 			break;
12267c478bd9Sstevel@tonic-gate 		case 'J':
12277c478bd9Sstevel@tonic-gate 			opts.o_outpmode |= OPT_SPLIT | OPT_PROJECTS;
12287c478bd9Sstevel@tonic-gate 			break;
12297c478bd9Sstevel@tonic-gate 		case 'n':
1230a851ab35Svb160487 			if ((p = strtok(optarg, ",")) == NULL)
1231a851ab35Svb160487 				Die(gettext("invalid argument for -n\n"));
12327c478bd9Sstevel@tonic-gate 			opts.o_ntop = Atoi(p);
12337c478bd9Sstevel@tonic-gate 			if (p = strtok(NULL, ","))
12347c478bd9Sstevel@tonic-gate 				opts.o_nbottom = Atoi(p);
12357c478bd9Sstevel@tonic-gate 			opts.o_outpmode &= ~OPT_FULLSCREEN;
12367c478bd9Sstevel@tonic-gate 			break;
12377c478bd9Sstevel@tonic-gate 		case 's':
12387c478bd9Sstevel@tonic-gate 			opts.o_sortorder = -1;
12397c478bd9Sstevel@tonic-gate 			sortk = optarg;
12407c478bd9Sstevel@tonic-gate 			break;
12417c478bd9Sstevel@tonic-gate 		case 'S':
12427c478bd9Sstevel@tonic-gate 			opts.o_sortorder = 1;
12437c478bd9Sstevel@tonic-gate 			sortk = optarg;
12447c478bd9Sstevel@tonic-gate 			break;
12457c478bd9Sstevel@tonic-gate 		case 'u':
1246a851ab35Svb160487 			if ((p = strtok(optarg, ", ")) == NULL)
1247a851ab35Svb160487 				Die(gettext("invalid argument for -u\n"));
12487c478bd9Sstevel@tonic-gate 			add_uid(&euid_tbl, p);
12497c478bd9Sstevel@tonic-gate 			while (p = strtok(NULL, ", "))
12507c478bd9Sstevel@tonic-gate 				add_uid(&euid_tbl, p);
12517c478bd9Sstevel@tonic-gate 			break;
12527c478bd9Sstevel@tonic-gate 		case 'U':
1253a851ab35Svb160487 			if ((p = strtok(optarg, ", ")) == NULL)
1254a851ab35Svb160487 				Die(gettext("invalid argument for -U\n"));
12557c478bd9Sstevel@tonic-gate 			add_uid(&ruid_tbl, p);
12567c478bd9Sstevel@tonic-gate 			while (p = strtok(NULL, ", "))
12577c478bd9Sstevel@tonic-gate 				add_uid(&ruid_tbl, p);
12587c478bd9Sstevel@tonic-gate 			break;
12597c478bd9Sstevel@tonic-gate 		case 'p':
1260a851ab35Svb160487 			fill_table(&pid_tbl, optarg, 'p');
12617c478bd9Sstevel@tonic-gate 			break;
12627c478bd9Sstevel@tonic-gate 		case 'C':
12637c478bd9Sstevel@tonic-gate 			fill_set_table(optarg);
12647c478bd9Sstevel@tonic-gate 			opts.o_outpmode |= OPT_PSETS;
12657c478bd9Sstevel@tonic-gate 			break;
12667c478bd9Sstevel@tonic-gate 		case 'P':
1267a851ab35Svb160487 			fill_table(&cpu_tbl, optarg, 'P');
12687c478bd9Sstevel@tonic-gate 			break;
12697c478bd9Sstevel@tonic-gate 		case 'k':
1270a851ab35Svb160487 			fill_table(&tsk_tbl, optarg, 'k');
12717c478bd9Sstevel@tonic-gate 			break;
12727c478bd9Sstevel@tonic-gate 		case 'j':
12737c478bd9Sstevel@tonic-gate 			fill_prj_table(optarg);
12747c478bd9Sstevel@tonic-gate 			break;
12757c478bd9Sstevel@tonic-gate 		case 'L':
12767c478bd9Sstevel@tonic-gate 			opts.o_outpmode |= OPT_LWPS;
12777c478bd9Sstevel@tonic-gate 			break;
12787c478bd9Sstevel@tonic-gate 		case 'z':
1279a851ab35Svb160487 			if ((p = strtok(optarg, ", ")) == NULL)
1280a851ab35Svb160487 				Die(gettext("invalid argument for -z\n"));
12817c478bd9Sstevel@tonic-gate 			add_zone(&zone_tbl, p);
12827c478bd9Sstevel@tonic-gate 			while (p = strtok(NULL, ", "))
12837c478bd9Sstevel@tonic-gate 				add_zone(&zone_tbl, p);
12847c478bd9Sstevel@tonic-gate 			break;
12857c478bd9Sstevel@tonic-gate 		case 'Z':
12867c478bd9Sstevel@tonic-gate 			opts.o_outpmode |= OPT_SPLIT | OPT_ZONES;
12877c478bd9Sstevel@tonic-gate 			break;
12887c478bd9Sstevel@tonic-gate 		default:
12897c478bd9Sstevel@tonic-gate 			Usage();
12907c478bd9Sstevel@tonic-gate 		}
12917c478bd9Sstevel@tonic-gate 	}
12927c478bd9Sstevel@tonic-gate 
12937c478bd9Sstevel@tonic-gate 	(void) atexit(Exit);
12947c478bd9Sstevel@tonic-gate 	if ((opts.o_outpmode & OPT_USERS) &&
12957c478bd9Sstevel@tonic-gate 	    !(opts.o_outpmode & OPT_SPLIT))
12967c478bd9Sstevel@tonic-gate 		opts.o_nbottom = opts.o_ntop;
12977c478bd9Sstevel@tonic-gate 	if (opts.o_ntop == 0 || opts.o_nbottom == 0)
12987c478bd9Sstevel@tonic-gate 		Die(gettext("invalid argument for -n\n"));
12997c478bd9Sstevel@tonic-gate 	if (!(opts.o_outpmode & OPT_SPLIT) && (opts.o_outpmode & OPT_USERS) &&
13007c478bd9Sstevel@tonic-gate 	    ((opts.o_outpmode & (OPT_PSINFO | OPT_MSACCT))))
13017c478bd9Sstevel@tonic-gate 		Die(gettext("-t option cannot be used with -v or -m\n"));
13027c478bd9Sstevel@tonic-gate 
13037c478bd9Sstevel@tonic-gate 	if ((opts.o_outpmode & OPT_SPLIT) && (opts.o_outpmode && OPT_USERS) &&
13047c478bd9Sstevel@tonic-gate 	    !((opts.o_outpmode & (OPT_PSINFO | OPT_MSACCT))))
13057c478bd9Sstevel@tonic-gate 		Die(gettext("-t option cannot be used with "
13067c478bd9Sstevel@tonic-gate 		    "-a, -J, -T or -Z\n"));
13077c478bd9Sstevel@tonic-gate 
13087c478bd9Sstevel@tonic-gate 	if ((opts.o_outpmode & OPT_USERS) &&
1309*c6402783Sakolb 	    (opts.o_outpmode &
1310*c6402783Sakolb 		(OPT_TASKS | OPT_PROJECTS | OPT_ZONES)))
13117c478bd9Sstevel@tonic-gate 		Die(gettext("-a option cannot be used with "
13127c478bd9Sstevel@tonic-gate 		    "-t, -J, -T or -Z\n"));
13137c478bd9Sstevel@tonic-gate 
13147c478bd9Sstevel@tonic-gate 	if (((opts.o_outpmode & OPT_TASKS) &&
13157c478bd9Sstevel@tonic-gate 	    (opts.o_outpmode & (OPT_PROJECTS|OPT_ZONES))) ||
13167c478bd9Sstevel@tonic-gate 	    ((opts.o_outpmode & OPT_PROJECTS) &&
13177c478bd9Sstevel@tonic-gate 		(opts.o_outpmode & (OPT_TASKS|OPT_ZONES)))) {
1318*c6402783Sakolb 		Die(gettext(
1319*c6402783Sakolb 		    "-J, -T and -Z options are mutually exclusive\n"));
1320*c6402783Sakolb 	}
1321*c6402783Sakolb 
1322*c6402783Sakolb 	/*
1323*c6402783Sakolb 	 * There is not enough space to combine microstate information and
1324*c6402783Sakolb 	 * lgroup information and still fit in 80-column output.
1325*c6402783Sakolb 	 */
1326*c6402783Sakolb 	if ((opts.o_outpmode & OPT_LGRP) && (opts.o_outpmode & OPT_MSACCT)) {
1327*c6402783Sakolb 		Die(gettext("-H and -m options are mutually exclusive\n"));
13287c478bd9Sstevel@tonic-gate 	}
13297c478bd9Sstevel@tonic-gate 
13307c478bd9Sstevel@tonic-gate 	if (argc > optind)
13317c478bd9Sstevel@tonic-gate 		opts.o_interval = Atoi(argv[optind++]);
13327c478bd9Sstevel@tonic-gate 	if (argc > optind)
13337c478bd9Sstevel@tonic-gate 		opts.o_count = Atoi(argv[optind++]);
13347c478bd9Sstevel@tonic-gate 	if (opts.o_count == 0)
13357c478bd9Sstevel@tonic-gate 		Die(gettext("invalid counter value\n"));
13367c478bd9Sstevel@tonic-gate 	if (argc > optind)
13377c478bd9Sstevel@tonic-gate 		Usage();
13387c478bd9Sstevel@tonic-gate 	if (opts.o_outpmode & OPT_REALTIME)
13397c478bd9Sstevel@tonic-gate 		Priocntl("RT");
13407c478bd9Sstevel@tonic-gate 	if (isatty(STDOUT_FILENO) == 1 && isatty(STDIN_FILENO))
13417c478bd9Sstevel@tonic-gate 		opts.o_outpmode |= OPT_TTY;	/* interactive */
13427c478bd9Sstevel@tonic-gate 	if (!(opts.o_outpmode & OPT_TTY)) {
13437c478bd9Sstevel@tonic-gate 		opts.o_outpmode &= ~OPT_TERMCAP; /* no termcap for pipes */
13447c478bd9Sstevel@tonic-gate 		opts.o_outpmode &= ~OPT_FULLSCREEN;
13457c478bd9Sstevel@tonic-gate 	}
13467c478bd9Sstevel@tonic-gate 	if (opts.o_outpmode & OPT_TERMCAP)
13477c478bd9Sstevel@tonic-gate 		ldtermcap();		/* can turn OPT_TERMCAP off */
13487c478bd9Sstevel@tonic-gate 	if (opts.o_outpmode & OPT_TERMCAP)
13497c478bd9Sstevel@tonic-gate 		(void) setsize();
13507c478bd9Sstevel@tonic-gate 	list_alloc(&lwps, opts.o_ntop);
13517c478bd9Sstevel@tonic-gate 	list_alloc(&users, opts.o_nbottom);
13527c478bd9Sstevel@tonic-gate 	list_alloc(&tasks, opts.o_nbottom);
13537c478bd9Sstevel@tonic-gate 	list_alloc(&projects, opts.o_nbottom);
13547c478bd9Sstevel@tonic-gate 	list_alloc(&zones, opts.o_nbottom);
1355*c6402783Sakolb 	list_alloc(&lgroups, opts.o_nbottom);
13567c478bd9Sstevel@tonic-gate 	list_setkeyfunc(sortk, &opts, &lwps, LT_LWPS);
13577c478bd9Sstevel@tonic-gate 	list_setkeyfunc(NULL, &opts, &users, LT_USERS);
13587c478bd9Sstevel@tonic-gate 	list_setkeyfunc(NULL, &opts, &tasks, LT_TASKS);
13597c478bd9Sstevel@tonic-gate 	list_setkeyfunc(NULL, &opts, &projects, LT_PROJECTS);
13607c478bd9Sstevel@tonic-gate 	list_setkeyfunc(NULL, &opts, &zones, LT_ZONES);
1361*c6402783Sakolb 	list_setkeyfunc(NULL, &opts, &lgroups, LT_LGRPS);
13627c478bd9Sstevel@tonic-gate 	if (opts.o_outpmode & OPT_TERMCAP)
13637c478bd9Sstevel@tonic-gate 		curses_on();
13647c478bd9Sstevel@tonic-gate 	if ((procdir = opendir("/proc")) == NULL)
13657c478bd9Sstevel@tonic-gate 		Die(gettext("cannot open /proc directory\n"));
13667c478bd9Sstevel@tonic-gate 	if (opts.o_outpmode & OPT_TTY) {
13677c478bd9Sstevel@tonic-gate 		(void) printf(gettext("Please wait...\r"));
13687c478bd9Sstevel@tonic-gate 		(void) fflush(stdout);
13697c478bd9Sstevel@tonic-gate 	}
13707c478bd9Sstevel@tonic-gate 	set_signals();
13717c478bd9Sstevel@tonic-gate 	pollset.fd = STDIN_FILENO;
13727c478bd9Sstevel@tonic-gate 	pollset.events = POLLIN;
13737c478bd9Sstevel@tonic-gate 	timeout = opts.o_interval * MILLISEC;
13747c478bd9Sstevel@tonic-gate 
13757c478bd9Sstevel@tonic-gate 	/*
13767c478bd9Sstevel@tonic-gate 	 * main program loop
13777c478bd9Sstevel@tonic-gate 	 */
13787c478bd9Sstevel@tonic-gate 	do {
13797c478bd9Sstevel@tonic-gate 		if (sigterm == 1)
13807c478bd9Sstevel@tonic-gate 			break;
13817c478bd9Sstevel@tonic-gate 		if (sigtstp == 1) {
13827c478bd9Sstevel@tonic-gate 			curses_off();
13837c478bd9Sstevel@tonic-gate 			(void) signal(SIGTSTP, SIG_DFL);
13847c478bd9Sstevel@tonic-gate 			(void) kill(0, SIGTSTP);
13857c478bd9Sstevel@tonic-gate 			/*
13867c478bd9Sstevel@tonic-gate 			 * prstat stops here until it receives SIGCONT signal.
13877c478bd9Sstevel@tonic-gate 			 */
13887c478bd9Sstevel@tonic-gate 			sigtstp = 0;
13897c478bd9Sstevel@tonic-gate 			(void) signal(SIGTSTP, sig_handler);
13907c478bd9Sstevel@tonic-gate 			curses_on();
13917c478bd9Sstevel@tonic-gate 			print_movecur = FALSE;
13927c478bd9Sstevel@tonic-gate 			if (opts.o_outpmode & OPT_FULLSCREEN)
13937c478bd9Sstevel@tonic-gate 				sigwinch = 1;
13947c478bd9Sstevel@tonic-gate 		}
13957c478bd9Sstevel@tonic-gate 		if (sigwinch == 1) {
13967c478bd9Sstevel@tonic-gate 			if (setsize() == 1) {
13977c478bd9Sstevel@tonic-gate 				list_free(&lwps);
13987c478bd9Sstevel@tonic-gate 				list_free(&users);
13997c478bd9Sstevel@tonic-gate 				list_free(&tasks);
14007c478bd9Sstevel@tonic-gate 				list_free(&projects);
14017c478bd9Sstevel@tonic-gate 				list_free(&zones);
14027c478bd9Sstevel@tonic-gate 				list_alloc(&lwps, opts.o_ntop);
14037c478bd9Sstevel@tonic-gate 				list_alloc(&users, opts.o_nbottom);
14047c478bd9Sstevel@tonic-gate 				list_alloc(&tasks, opts.o_nbottom);
14057c478bd9Sstevel@tonic-gate 				list_alloc(&projects, opts.o_nbottom);
14067c478bd9Sstevel@tonic-gate 				list_alloc(&zones, opts.o_nbottom);
14077c478bd9Sstevel@tonic-gate 			}
14087c478bd9Sstevel@tonic-gate 			sigwinch = 0;
14097c478bd9Sstevel@tonic-gate 			(void) signal(SIGWINCH, sig_handler);
14107c478bd9Sstevel@tonic-gate 		}
14117c478bd9Sstevel@tonic-gate 		prstat_scandir(procdir);
14127c478bd9Sstevel@tonic-gate 		list_refresh(&lwps);
14137c478bd9Sstevel@tonic-gate 		if (print_movecur)
14147c478bd9Sstevel@tonic-gate 			(void) putp(movecur);
14157c478bd9Sstevel@tonic-gate 		print_movecur = TRUE;
14167c478bd9Sstevel@tonic-gate 		if ((opts.o_outpmode & OPT_PSINFO) ||
14177c478bd9Sstevel@tonic-gate 		    (opts.o_outpmode & OPT_MSACCT)) {
14187c478bd9Sstevel@tonic-gate 			list_sort(&lwps);
14197c478bd9Sstevel@tonic-gate 			list_print(&lwps);
14207c478bd9Sstevel@tonic-gate 		}
14217c478bd9Sstevel@tonic-gate 		if (opts.o_outpmode & OPT_USERS) {
14227c478bd9Sstevel@tonic-gate 			list_sort(&users);
14237c478bd9Sstevel@tonic-gate 			list_print(&users);
14247c478bd9Sstevel@tonic-gate 			list_clear(&users);
14257c478bd9Sstevel@tonic-gate 		}
14267c478bd9Sstevel@tonic-gate 		if (opts.o_outpmode & OPT_TASKS) {
14277c478bd9Sstevel@tonic-gate 			list_sort(&tasks);
14287c478bd9Sstevel@tonic-gate 			list_print(&tasks);
14297c478bd9Sstevel@tonic-gate 			list_clear(&tasks);
14307c478bd9Sstevel@tonic-gate 		}
14317c478bd9Sstevel@tonic-gate 		if (opts.o_outpmode & OPT_PROJECTS) {
14327c478bd9Sstevel@tonic-gate 			list_sort(&projects);
14337c478bd9Sstevel@tonic-gate 			list_print(&projects);
14347c478bd9Sstevel@tonic-gate 			list_clear(&projects);
14357c478bd9Sstevel@tonic-gate 		}
14367c478bd9Sstevel@tonic-gate 		if (opts.o_outpmode & OPT_ZONES) {
14377c478bd9Sstevel@tonic-gate 			list_sort(&zones);
14387c478bd9Sstevel@tonic-gate 			list_print(&zones);
14397c478bd9Sstevel@tonic-gate 			list_clear(&zones);
14407c478bd9Sstevel@tonic-gate 		}
14417c478bd9Sstevel@tonic-gate 		if (opts.o_count == 1)
14427c478bd9Sstevel@tonic-gate 			break;
14437c478bd9Sstevel@tonic-gate 		/*
14447c478bd9Sstevel@tonic-gate 		 * If poll() returns -1 and sets errno to EINTR here because
14457c478bd9Sstevel@tonic-gate 		 * the process received a signal, it is Ok to abort this
14467c478bd9Sstevel@tonic-gate 		 * timeout and loop around because we check the signals at the
14477c478bd9Sstevel@tonic-gate 		 * top of the loop.
14487c478bd9Sstevel@tonic-gate 		 */
14497c478bd9Sstevel@tonic-gate 		if (opts.o_outpmode & OPT_TTY) {
14507c478bd9Sstevel@tonic-gate 			if (poll(&pollset, (nfds_t)1, timeout) > 0) {
14517c478bd9Sstevel@tonic-gate 				if (read(STDIN_FILENO, &key, 1) == 1) {
14527c478bd9Sstevel@tonic-gate 					if (tolower(key) == 'q')
14537c478bd9Sstevel@tonic-gate 						break;
14547c478bd9Sstevel@tonic-gate 				}
14557c478bd9Sstevel@tonic-gate 			}
14567c478bd9Sstevel@tonic-gate 		} else {
14577c478bd9Sstevel@tonic-gate 			(void) sleep(opts.o_interval);
14587c478bd9Sstevel@tonic-gate 		}
14597c478bd9Sstevel@tonic-gate 	} while (opts.o_count == (-1) || --opts.o_count);
14607c478bd9Sstevel@tonic-gate 
14617c478bd9Sstevel@tonic-gate 	if (opts.o_outpmode & OPT_TTY)
14627c478bd9Sstevel@tonic-gate 		(void) putchar('\r');
14637c478bd9Sstevel@tonic-gate 	return (0);
14647c478bd9Sstevel@tonic-gate }
1465