xref: /illumos-gate/usr/src/cmd/ps/ps.c (revision ab618543)
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
551223f18Sjonb  * Common Development and Distribution License (the "License").
651223f18Sjonb  * 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  */
2151223f18Sjonb 
227c478bd9Sstevel@tonic-gate /*
230a1278f2SGary Mills  * Copyright (c) 2013 Gary Mills
240a1278f2SGary Mills  *
25f5c9e9f9SCasper H.S. Dik  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
267c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
277c478bd9Sstevel@tonic-gate  */
287c478bd9Sstevel@tonic-gate 
29eaaa4d5fSBryan Cantrill /*
30*ab618543SJohn Levon  * Copyright (c) 2018, Joyent, Inc.
31eaaa4d5fSBryan Cantrill  */
32eaaa4d5fSBryan Cantrill 
337c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
34*ab618543SJohn Levon /*	  All Rights Reserved	*/
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate /*
377c478bd9Sstevel@tonic-gate  * ps -- print things about processes.
387c478bd9Sstevel@tonic-gate  */
397c478bd9Sstevel@tonic-gate #include <stdio.h>
407c478bd9Sstevel@tonic-gate #include <ctype.h>
417c478bd9Sstevel@tonic-gate #include <string.h>
427c478bd9Sstevel@tonic-gate #include <errno.h>
437c478bd9Sstevel@tonic-gate #include <fcntl.h>
447c478bd9Sstevel@tonic-gate #include <pwd.h>
457c478bd9Sstevel@tonic-gate #include <grp.h>
467c478bd9Sstevel@tonic-gate #include <sys/types.h>
477c478bd9Sstevel@tonic-gate #include <sys/stat.h>
487c478bd9Sstevel@tonic-gate #include <sys/mkdev.h>
497c478bd9Sstevel@tonic-gate #include <unistd.h>
507c478bd9Sstevel@tonic-gate #include <stdlib.h>
517c478bd9Sstevel@tonic-gate #include <limits.h>
527c478bd9Sstevel@tonic-gate #include <dirent.h>
537c478bd9Sstevel@tonic-gate #include <sys/signal.h>
547c478bd9Sstevel@tonic-gate #include <sys/fault.h>
557c478bd9Sstevel@tonic-gate #include <sys/syscall.h>
567c478bd9Sstevel@tonic-gate #include <sys/time.h>
577c478bd9Sstevel@tonic-gate #include <procfs.h>
587c478bd9Sstevel@tonic-gate #include <locale.h>
597c478bd9Sstevel@tonic-gate #include <wctype.h>
607c478bd9Sstevel@tonic-gate #include <wchar.h>
617c478bd9Sstevel@tonic-gate #include <libw.h>
627c478bd9Sstevel@tonic-gate #include <stdarg.h>
637c478bd9Sstevel@tonic-gate #include <sys/proc.h>
647c478bd9Sstevel@tonic-gate #include <sys/pset.h>
657c478bd9Sstevel@tonic-gate #include <project.h>
667c478bd9Sstevel@tonic-gate #include <zone.h>
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate #define	min(a, b)	((a) > (b) ? (b) : (a))
697c478bd9Sstevel@tonic-gate #define	max(a, b)	((a) < (b) ? (b) : (a))
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate #define	NTTYS	20	/* initial size of table for -t option  */
72c6402783Sakolb #define	SIZ	30	/* initial size of tables for -p, -s, -g, -h and -z */
7351223f18Sjonb 
7451223f18Sjonb /*
7551223f18Sjonb  * Size of buffer holding args for t, p, s, g, u, U, G, z options.
7651223f18Sjonb  * Set to ZONENAME_MAX, the minimum value needed to allow any
7751223f18Sjonb  * zone to be specified.
7851223f18Sjonb  */
7951223f18Sjonb #define	ARGSIZ ZONENAME_MAX
807c478bd9Sstevel@tonic-gate 
810a1278f2SGary Mills /* Max chars in a user/group name or printed u/g id */
820a1278f2SGary Mills #define	MAXUGNAME (LOGNAME_MAX+2)
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate /* Structure for storing user or group info */
857c478bd9Sstevel@tonic-gate struct ugdata {
867c478bd9Sstevel@tonic-gate 	id_t	id;			/* numeric user-id or group-id */
877c478bd9Sstevel@tonic-gate 	char	name[MAXUGNAME+1];	/* user/group name, null terminated */
887c478bd9Sstevel@tonic-gate };
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate struct ughead {
917c478bd9Sstevel@tonic-gate 	size_t	size;		/* number of ugdata structs allocated */
927c478bd9Sstevel@tonic-gate 	size_t	nent;		/* number of active entries */
937c478bd9Sstevel@tonic-gate 	struct ugdata *ent;	/* pointer to array of actual entries */
947c478bd9Sstevel@tonic-gate };
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate enum fname {	/* enumeration of field names */
977c478bd9Sstevel@tonic-gate 	F_USER,		/* effective user of the process */
987c478bd9Sstevel@tonic-gate 	F_RUSER,	/* real user of the process */
997c478bd9Sstevel@tonic-gate 	F_GROUP,	/* effective group of the process */
1007c478bd9Sstevel@tonic-gate 	F_RGROUP,	/* real group of the process */
1017c478bd9Sstevel@tonic-gate 	F_UID,		/* numeric effective uid of the process */
1027c478bd9Sstevel@tonic-gate 	F_RUID,		/* numeric real uid of the process */
1037c478bd9Sstevel@tonic-gate 	F_GID,		/* numeric effective gid of the process */
1047c478bd9Sstevel@tonic-gate 	F_RGID,		/* numeric real gid of the process */
1057c478bd9Sstevel@tonic-gate 	F_PID,		/* process id */
1067c478bd9Sstevel@tonic-gate 	F_PPID,		/* parent process id */
1077c478bd9Sstevel@tonic-gate 	F_PGID,		/* process group id */
1087c478bd9Sstevel@tonic-gate 	F_SID,		/* session id */
1097c478bd9Sstevel@tonic-gate 	F_PSR,		/* bound processor */
1107c478bd9Sstevel@tonic-gate 	F_LWP,		/* lwp-id */
111*ab618543SJohn Levon 	F_LWPNAME,	/* lwp name */
1127c478bd9Sstevel@tonic-gate 	F_NLWP,		/* number of lwps */
1137c478bd9Sstevel@tonic-gate 	F_OPRI,		/* old priority (obsolete) */
1147c478bd9Sstevel@tonic-gate 	F_PRI,		/* new priority */
1157c478bd9Sstevel@tonic-gate 	F_F,		/* process flags */
1167c478bd9Sstevel@tonic-gate 	F_S,		/* letter indicating the state */
1177c478bd9Sstevel@tonic-gate 	F_C,		/* processor utilization (obsolete) */
1187c478bd9Sstevel@tonic-gate 	F_PCPU,		/* percent of recently used cpu time */
1197c478bd9Sstevel@tonic-gate 	F_PMEM,		/* percent of physical memory used (rss) */
1207c478bd9Sstevel@tonic-gate 	F_OSZ,		/* virtual size of the process in pages */
1217c478bd9Sstevel@tonic-gate 	F_VSZ,		/* virtual size of the process in kilobytes */
1227c478bd9Sstevel@tonic-gate 	F_RSS,		/* resident set size of the process in kilobytes */
1237c478bd9Sstevel@tonic-gate 	F_NICE,		/* "nice" value of the process */
1247c478bd9Sstevel@tonic-gate 	F_CLASS,	/* scheduler class */
1257c478bd9Sstevel@tonic-gate 	F_STIME,	/* start time of the process, hh:mm:ss or Month Day */
1267c478bd9Sstevel@tonic-gate 	F_ETIME,	/* elapsed time of the process, [[dd-]hh:]mm:ss */
1277c478bd9Sstevel@tonic-gate 	F_TIME,		/* cpu time of the process, [[dd-]hh:]mm:ss */
1287c478bd9Sstevel@tonic-gate 	F_TTY,		/* name of the controlling terminal */
1297c478bd9Sstevel@tonic-gate 	F_ADDR,		/* address of the process (obsolete) */
1307c478bd9Sstevel@tonic-gate 	F_WCHAN,	/* wait channel (sleep condition variable) */
1317c478bd9Sstevel@tonic-gate 	F_FNAME,	/* file name of command */
1327c478bd9Sstevel@tonic-gate 	F_COMM,		/* name of command (argv[0] value) */
1337c478bd9Sstevel@tonic-gate 	F_ARGS,		/* name of command plus all its arguments */
1347c478bd9Sstevel@tonic-gate 	F_TASKID,	/* task id */
1357c478bd9Sstevel@tonic-gate 	F_PROJID,	/* project id */
1367c478bd9Sstevel@tonic-gate 	F_PROJECT,	/* project name of the process */
1377c478bd9Sstevel@tonic-gate 	F_PSET,		/* bound processor set */
1387c478bd9Sstevel@tonic-gate 	F_ZONE,		/* zone name */
1397c478bd9Sstevel@tonic-gate 	F_ZONEID,	/* zone id */
140c6402783Sakolb 	F_CTID,		/* process contract id */
141eaaa4d5fSBryan Cantrill 	F_LGRP,		/* process home lgroup */
142eaaa4d5fSBryan Cantrill 	F_DMODEL	/* process data model */
1437c478bd9Sstevel@tonic-gate };
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate struct field {
1467c478bd9Sstevel@tonic-gate 	struct field	*next;		/* linked list */
1477c478bd9Sstevel@tonic-gate 	int		fname;		/* field index */
1487c478bd9Sstevel@tonic-gate 	const char	*header;	/* header to use */
1497c478bd9Sstevel@tonic-gate 	int		width;		/* width of field */
1507c478bd9Sstevel@tonic-gate };
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate static	struct field *fields = NULL;	/* fields selected via -o */
1537c478bd9Sstevel@tonic-gate static	struct field *last_field = NULL;
1547c478bd9Sstevel@tonic-gate static	int do_header = 0;
1557c478bd9Sstevel@tonic-gate static	struct timeval now;
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate /* array of defined fields, in fname order */
1587c478bd9Sstevel@tonic-gate struct def_field {
1597c478bd9Sstevel@tonic-gate 	const char *fname;
1607c478bd9Sstevel@tonic-gate 	const char *header;
1617c478bd9Sstevel@tonic-gate 	int width;
1627c478bd9Sstevel@tonic-gate 	int minwidth;
1637c478bd9Sstevel@tonic-gate };
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate static struct def_field fname[] = {
1667c478bd9Sstevel@tonic-gate 	/* fname	header		width	minwidth */
1677c478bd9Sstevel@tonic-gate 	{ "user",	"USER",		8,	8	},
1687c478bd9Sstevel@tonic-gate 	{ "ruser",	"RUSER",	8,	8	},
1697c478bd9Sstevel@tonic-gate 	{ "group",	"GROUP",	8,	8	},
1707c478bd9Sstevel@tonic-gate 	{ "rgroup",	"RGROUP",	8,	8	},
1717c478bd9Sstevel@tonic-gate 	{ "uid",	"UID",		5,	5	},
1727c478bd9Sstevel@tonic-gate 	{ "ruid",	"RUID",		5,	5	},
1737c478bd9Sstevel@tonic-gate 	{ "gid",	"GID",		5,	5	},
1747c478bd9Sstevel@tonic-gate 	{ "rgid",	"RGID",		5,	5	},
1757c478bd9Sstevel@tonic-gate 	{ "pid",	"PID",		5,	5	},
1767c478bd9Sstevel@tonic-gate 	{ "ppid",	"PPID",		5,	5	},
1777c478bd9Sstevel@tonic-gate 	{ "pgid",	"PGID",		5,	5	},
1787c478bd9Sstevel@tonic-gate 	{ "sid",	"SID",		5,	5	},
1797c478bd9Sstevel@tonic-gate 	{ "psr",	"PSR",		3,	2	},
1807c478bd9Sstevel@tonic-gate 	{ "lwp",	"LWP",		6,	2	},
181*ab618543SJohn Levon 	{ "lwpname",	"LWPNAME",	32,	8	},
1827c478bd9Sstevel@tonic-gate 	{ "nlwp",	"NLWP",		4,	2	},
1837c478bd9Sstevel@tonic-gate 	{ "opri",	"PRI",		3,	2	},
1847c478bd9Sstevel@tonic-gate 	{ "pri",	"PRI",		3,	2	},
1857c478bd9Sstevel@tonic-gate 	{ "f",		"F",		2,	2	},
1867c478bd9Sstevel@tonic-gate 	{ "s",		"S",		1,	1	},
1877c478bd9Sstevel@tonic-gate 	{ "c",		"C",		2,	2	},
1887c478bd9Sstevel@tonic-gate 	{ "pcpu",	"%CPU",		4,	4	},
1897c478bd9Sstevel@tonic-gate 	{ "pmem",	"%MEM",		4,	4	},
1907c478bd9Sstevel@tonic-gate 	{ "osz",	"SZ",		4,	4	},
1917c478bd9Sstevel@tonic-gate 	{ "vsz",	"VSZ",		4,	4	},
1927c478bd9Sstevel@tonic-gate 	{ "rss",	"RSS",		4,	4	},
1937c478bd9Sstevel@tonic-gate 	{ "nice",	"NI",		2,	2	},
1947c478bd9Sstevel@tonic-gate 	{ "class",	"CLS",		4,	2	},
1957c478bd9Sstevel@tonic-gate 	{ "stime",	"STIME",	8,	8	},
1967c478bd9Sstevel@tonic-gate 	{ "etime",	"ELAPSED",	11,	7	},
1977c478bd9Sstevel@tonic-gate 	{ "time",	"TIME",		11,	5	},
1987c478bd9Sstevel@tonic-gate 	{ "tty",	"TT",		7,	7	},
1997c478bd9Sstevel@tonic-gate #ifdef _LP64
2007c478bd9Sstevel@tonic-gate 	{ "addr",	"ADDR",		16,	8	},
2017c478bd9Sstevel@tonic-gate 	{ "wchan",	"WCHAN",	16,	8	},
2027c478bd9Sstevel@tonic-gate #else
2037c478bd9Sstevel@tonic-gate 	{ "addr",	"ADDR",		8,	8	},
2047c478bd9Sstevel@tonic-gate 	{ "wchan",	"WCHAN",	8,	8	},
2057c478bd9Sstevel@tonic-gate #endif
2067c478bd9Sstevel@tonic-gate 	{ "fname",	"COMMAND",	8,	8	},
2077c478bd9Sstevel@tonic-gate 	{ "comm",	"COMMAND",	80,	8	},
2087c478bd9Sstevel@tonic-gate 	{ "args",	"COMMAND",	80,	80	},
2097c478bd9Sstevel@tonic-gate 	{ "taskid",	"TASKID",	5,	5	},
2107c478bd9Sstevel@tonic-gate 	{ "projid",	"PROJID",	5,	5	},
2117c478bd9Sstevel@tonic-gate 	{ "project",	"PROJECT",	8,	8	},
2127c478bd9Sstevel@tonic-gate 	{ "pset",	"PSET",		3,	3	},
2137c478bd9Sstevel@tonic-gate 	{ "zone",	"ZONE",		8,	8	},
2147c478bd9Sstevel@tonic-gate 	{ "zoneid",	"ZONEID",	5,	5	},
2157c478bd9Sstevel@tonic-gate 	{ "ctid",	"CTID",		5,	5	},
216*ab618543SJohn Levon 	{ "lgrp",	"LGRP",		4,	2	},
217*ab618543SJohn Levon 	{ "dmodel",	"DMODEL",	6,	6	},
2187c478bd9Sstevel@tonic-gate };
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate #define	NFIELDS	(sizeof (fname) / sizeof (fname[0]))
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate static	int	retcode = 1;
2237c478bd9Sstevel@tonic-gate static	int	lflg;
2247c478bd9Sstevel@tonic-gate static	int	Aflg;
2257c478bd9Sstevel@tonic-gate static	int	uflg;
2267c478bd9Sstevel@tonic-gate static	int	Uflg;
2277c478bd9Sstevel@tonic-gate static	int	Gflg;
2287c478bd9Sstevel@tonic-gate static	int	aflg;
2297c478bd9Sstevel@tonic-gate static	int	dflg;
2307c478bd9Sstevel@tonic-gate static	int	Lflg;
2317c478bd9Sstevel@tonic-gate static	int	Pflg;
2320a1278f2SGary Mills static	int	Wflg;
2337c478bd9Sstevel@tonic-gate static	int	yflg;
2347c478bd9Sstevel@tonic-gate static	int	pflg;
2357c478bd9Sstevel@tonic-gate static	int	fflg;
2367c478bd9Sstevel@tonic-gate static	int	cflg;
2377c478bd9Sstevel@tonic-gate static	int	jflg;
2387c478bd9Sstevel@tonic-gate static	int	gflg;
2397c478bd9Sstevel@tonic-gate static	int	sflg;
2407c478bd9Sstevel@tonic-gate static	int	tflg;
2417c478bd9Sstevel@tonic-gate static	int	zflg;
2427c478bd9Sstevel@tonic-gate static	int	Zflg;
243c6402783Sakolb static	int	hflg;
244c6402783Sakolb static	int	Hflg;
245f48205beScasper static	uid_t	tuid = (uid_t)-1;
2467c478bd9Sstevel@tonic-gate static	int	errflg;
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate static	int	ndev;		/* number of devices */
2497c478bd9Sstevel@tonic-gate static	int	maxdev;		/* number of devl structures allocated */
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate #define	DNINCR	100
2527c478bd9Sstevel@tonic-gate #define	DNSIZE	14
2537c478bd9Sstevel@tonic-gate static struct devl {		/* device list   */
2547c478bd9Sstevel@tonic-gate 	char	dname[DNSIZE];	/* device name   */
2557c478bd9Sstevel@tonic-gate 	dev_t	ddev;		/* device number */
2567c478bd9Sstevel@tonic-gate } *devl;
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate static	struct tty {
2597c478bd9Sstevel@tonic-gate 	char *tname;
2607c478bd9Sstevel@tonic-gate 	dev_t tdev;
2617c478bd9Sstevel@tonic-gate } *tty = NULL;			/* for t option */
2627c478bd9Sstevel@tonic-gate static	size_t	ttysz = 0;
2637c478bd9Sstevel@tonic-gate static	int	ntty = 0;
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate static	pid_t	*pid = NULL;	/* for p option */
2667c478bd9Sstevel@tonic-gate static	size_t	pidsz = 0;
2677c478bd9Sstevel@tonic-gate static	size_t	npid = 0;
2687c478bd9Sstevel@tonic-gate 
269c6402783Sakolb static	int	*lgrps = NULL;	/* list of lgroup IDs for for h option */
270c6402783Sakolb static	size_t	lgrps_size = 0;	/* size of the lgrps list */
271c6402783Sakolb static	size_t	nlgrps = 0;	/* number elements in the list */
272c6402783Sakolb 
273c6402783Sakolb /* Maximum possible lgroup ID value */
274c6402783Sakolb #define	MAX_LGRP_ID 256
275c6402783Sakolb 
2767c478bd9Sstevel@tonic-gate static	pid_t	*grpid = NULL;	/* for g option */
2777c478bd9Sstevel@tonic-gate static	size_t	grpidsz = 0;
2787c478bd9Sstevel@tonic-gate static	int	ngrpid = 0;
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate static	pid_t	*sessid = NULL;	/* for s option */
2817c478bd9Sstevel@tonic-gate static	size_t	sessidsz = 0;
2827c478bd9Sstevel@tonic-gate static	int	nsessid = 0;
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate static	zoneid_t *zoneid = NULL; /* for z option */
2857c478bd9Sstevel@tonic-gate static	size_t	zoneidsz = 0;
2867c478bd9Sstevel@tonic-gate static	int	nzoneid = 0;
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate static	int	kbytes_per_page;
2897c478bd9Sstevel@tonic-gate static	int	pidwidth;
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate static	char	*procdir = "/proc";	/* standard /proc directory */
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate static struct ughead	euid_tbl;	/* table to store selected euid's */
2947c478bd9Sstevel@tonic-gate static struct ughead	ruid_tbl;	/* table to store selected real uid's */
2957c478bd9Sstevel@tonic-gate static struct ughead	egid_tbl;	/* table to store selected egid's */
2967c478bd9Sstevel@tonic-gate static struct ughead	rgid_tbl;	/* table to store selected real gid's */
2977c478bd9Sstevel@tonic-gate static prheader_t *lpsinfobuf;		/* buffer to contain lpsinfo */
2987c478bd9Sstevel@tonic-gate static size_t	lpbufsize;
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate /*
3017c478bd9Sstevel@tonic-gate  * This constant defines the sentinal number of process IDs below which we
3027c478bd9Sstevel@tonic-gate  * only examine individual entries in /proc rather than scanning through
3037c478bd9Sstevel@tonic-gate  * /proc. This optimization is a huge win in the common case.
3047c478bd9Sstevel@tonic-gate  */
3057c478bd9Sstevel@tonic-gate #define	PTHRESHOLD	40
3067c478bd9Sstevel@tonic-gate 
307f5c9e9f9SCasper H.S. Dik #define	UCB_OPTS	"-aceglnrtuvwxSU"
308f5c9e9f9SCasper H.S. Dik 
3097c478bd9Sstevel@tonic-gate static	void	usage(void);
3107c478bd9Sstevel@tonic-gate static	char	*getarg(char **);
3117c478bd9Sstevel@tonic-gate static	char	*parse_format(char *);
3127c478bd9Sstevel@tonic-gate static	char	*gettty(psinfo_t *);
3137c478bd9Sstevel@tonic-gate static	int	prfind(int, psinfo_t *, char **);
3147c478bd9Sstevel@tonic-gate static	void	prcom(psinfo_t *, char *);
3157c478bd9Sstevel@tonic-gate static	void	prtpct(ushort_t, int);
3167c478bd9Sstevel@tonic-gate static	void	print_time(time_t, int);
3177c478bd9Sstevel@tonic-gate static	void	print_field(psinfo_t *, struct field *, const char *);
3187c478bd9Sstevel@tonic-gate static	void	print_zombie_field(psinfo_t *, struct field *, const char *);
3197c478bd9Sstevel@tonic-gate static	void	pr_fields(psinfo_t *, const char *,
3207c478bd9Sstevel@tonic-gate 		void (*print_fld)(psinfo_t *, struct field *, const char *));
3217c478bd9Sstevel@tonic-gate static	int	search(pid_t *, int, pid_t);
3227c478bd9Sstevel@tonic-gate static	void	add_ugentry(struct ughead *, char *);
3237c478bd9Sstevel@tonic-gate static	int	uconv(struct ughead *);
3247c478bd9Sstevel@tonic-gate static	int	gconv(struct ughead *);
325f48205beScasper static	int	ugfind(id_t, struct ughead *);
3267c478bd9Sstevel@tonic-gate static	void	prtime(timestruc_t, int, int);
3277c478bd9Sstevel@tonic-gate static	void	przom(psinfo_t *);
3287c478bd9Sstevel@tonic-gate static	int	namencnt(char *, int, int);
3297c478bd9Sstevel@tonic-gate static	char	*err_string(int);
3307c478bd9Sstevel@tonic-gate static	int	print_proc(char *pname);
3317c478bd9Sstevel@tonic-gate static	time_t	delta_secs(const timestruc_t *);
3327c478bd9Sstevel@tonic-gate static	int	str2id(const char *, pid_t *, long, long);
333f48205beScasper static	int	str2uid(const char *,  uid_t *, unsigned long, unsigned long);
3347c478bd9Sstevel@tonic-gate static	void	*Realloc(void *, size_t);
3357c478bd9Sstevel@tonic-gate static	int	pidcmp(const void *p1, const void *p2);
3367c478bd9Sstevel@tonic-gate 
337f5c9e9f9SCasper H.S. Dik extern	int	ucbmain(int, char **);
338f5c9e9f9SCasper H.S. Dik static	int	stdmain(int, char **);
339f5c9e9f9SCasper H.S. Dik 
3407c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv)3417c478bd9Sstevel@tonic-gate main(int argc, char **argv)
342f5c9e9f9SCasper H.S. Dik {
343f5c9e9f9SCasper H.S. Dik 	const char *me;
344f5c9e9f9SCasper H.S. Dik 
345f5c9e9f9SCasper H.S. Dik 	/*
346f5c9e9f9SCasper H.S. Dik 	 * The original two ps'es are linked in a single binary;
347f5c9e9f9SCasper H.S. Dik 	 * their main()s are renamed to stdmain for /usr/bin/ps and
348f5c9e9f9SCasper H.S. Dik 	 * ucbmain for /usr/ucb/ps.
349f5c9e9f9SCasper H.S. Dik 	 * We try to figure out which instance of ps the user wants to run.
350f5c9e9f9SCasper H.S. Dik 	 * Traditionally, the UCB variant doesn't require the flag argument
351f5c9e9f9SCasper H.S. Dik 	 * start with a "-".  If the first argument doesn't start with a
352f5c9e9f9SCasper H.S. Dik 	 * "-", we call "ucbmain".
353f5c9e9f9SCasper H.S. Dik 	 * If there's a first argument and it starts with a "-", we check
354f5c9e9f9SCasper H.S. Dik 	 * whether any of the options isn't acceptable to "ucbmain"; in that
355f5c9e9f9SCasper H.S. Dik 	 * case we run "stdmain".
356f5c9e9f9SCasper H.S. Dik 	 * If we can't tell from the options which main to call, we check
357f5c9e9f9SCasper H.S. Dik 	 * the binary we are running.  We default to "stdmain" but
358f5c9e9f9SCasper H.S. Dik 	 * any mention in the executable name of "ucb" causes us to call
359f5c9e9f9SCasper H.S. Dik 	 * ucbmain.
360f5c9e9f9SCasper H.S. Dik 	 */
361f5c9e9f9SCasper H.S. Dik 	if (argv[1] != NULL) {
362f5c9e9f9SCasper H.S. Dik 		if (argv[1][0] != '-')
363f5c9e9f9SCasper H.S. Dik 			return (ucbmain(argc, argv));
364f5c9e9f9SCasper H.S. Dik 		else if (argv[1][strspn(argv[1], UCB_OPTS)] != '\0')
365f5c9e9f9SCasper H.S. Dik 			return (stdmain(argc, argv));
366f5c9e9f9SCasper H.S. Dik 	}
367f5c9e9f9SCasper H.S. Dik 
368f5c9e9f9SCasper H.S. Dik 	me = getexecname();
369f5c9e9f9SCasper H.S. Dik 
370f5c9e9f9SCasper H.S. Dik 	if (me != NULL && strstr(me, "ucb") != NULL)
371f5c9e9f9SCasper H.S. Dik 		return (ucbmain(argc, argv));
372f5c9e9f9SCasper H.S. Dik 	else
373f5c9e9f9SCasper H.S. Dik 		return (stdmain(argc, argv));
374f5c9e9f9SCasper H.S. Dik }
375f5c9e9f9SCasper H.S. Dik 
376f5c9e9f9SCasper H.S. Dik static int
stdmain(int argc,char ** argv)377f5c9e9f9SCasper H.S. Dik stdmain(int argc, char **argv)
3787c478bd9Sstevel@tonic-gate {
3797c478bd9Sstevel@tonic-gate 	char	*p;
3807c478bd9Sstevel@tonic-gate 	char	*p1;
3817c478bd9Sstevel@tonic-gate 	char	*parg;
3827c478bd9Sstevel@tonic-gate 	int	c;
3837c478bd9Sstevel@tonic-gate 	int	i;
3847c478bd9Sstevel@tonic-gate 	int	pgerrflg = 0;	/* err flg: non-numeric arg w/p & g options */
38516f94f58Ssayama 	size_t	size, len;
3867c478bd9Sstevel@tonic-gate 	DIR	*dirp;
3877c478bd9Sstevel@tonic-gate 	struct dirent *dentp;
3887c478bd9Sstevel@tonic-gate 	pid_t	maxpid;
3897c478bd9Sstevel@tonic-gate 	pid_t	id;
3907c478bd9Sstevel@tonic-gate 	int	ret;
39116f94f58Ssayama 	char	loc_stime_str[32];
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
3947c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
3957c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"	/* Use this only if it weren't */
3967c478bd9Sstevel@tonic-gate #endif
3977c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate 	(void) memset(&euid_tbl, 0, sizeof (euid_tbl));
4007c478bd9Sstevel@tonic-gate 	(void) memset(&ruid_tbl, 0, sizeof (ruid_tbl));
4017c478bd9Sstevel@tonic-gate 	(void) memset(&egid_tbl, 0, sizeof (egid_tbl));
4027c478bd9Sstevel@tonic-gate 	(void) memset(&rgid_tbl, 0, sizeof (rgid_tbl));
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 	kbytes_per_page = sysconf(_SC_PAGESIZE) / 1024;
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 	(void) gettimeofday(&now, NULL);
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 	/*
4097c478bd9Sstevel@tonic-gate 	 * calculate width of pid fields based on configured MAXPID
4107c478bd9Sstevel@tonic-gate 	 * (must be at least 5 to retain output format compatibility)
4117c478bd9Sstevel@tonic-gate 	 */
4127c478bd9Sstevel@tonic-gate 	id = maxpid = (pid_t)sysconf(_SC_MAXPID);
4137c478bd9Sstevel@tonic-gate 	pidwidth = 1;
4147c478bd9Sstevel@tonic-gate 	while ((id /= 10) > 0)
4157c478bd9Sstevel@tonic-gate 		++pidwidth;
4167c478bd9Sstevel@tonic-gate 	pidwidth = pidwidth < 5 ? 5 : pidwidth;
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 	fname[F_PID].width = fname[F_PPID].width = pidwidth;
4197c478bd9Sstevel@tonic-gate 	fname[F_PGID].width = fname[F_SID].width = pidwidth;
4207c478bd9Sstevel@tonic-gate 
42116f94f58Ssayama 	/*
42216f94f58Ssayama 	 * TRANSLATION_NOTE
42316f94f58Ssayama 	 * Specify the printf format with width and precision for
42416f94f58Ssayama 	 * the STIME field.
42516f94f58Ssayama 	 */
42616f94f58Ssayama 	len = snprintf(loc_stime_str, sizeof (loc_stime_str),
42716f94f58Ssayama 	    dcgettext(NULL, "%8.8s", LC_TIME), "STIME");
42816f94f58Ssayama 	if (len >= sizeof (loc_stime_str))
42916f94f58Ssayama 		len = sizeof (loc_stime_str) - 1;
43016f94f58Ssayama 
43116f94f58Ssayama 	fname[F_STIME].width = fname[F_STIME].minwidth = len;
43216f94f58Ssayama 
4330a1278f2SGary Mills 	while ((c = getopt(argc, argv, "jlfceAadLPWyZHh:t:p:g:u:U:G:n:s:o:z:"))
434c6402783Sakolb 	    != EOF)
4357c478bd9Sstevel@tonic-gate 		switch (c) {
436c6402783Sakolb 		case 'H':		/* Show home lgroups */
437c6402783Sakolb 			Hflg++;
438c6402783Sakolb 			break;
439c6402783Sakolb 		case 'h':
440c6402783Sakolb 			/*
441c6402783Sakolb 			 * Show processes/threads with given home lgroups
442c6402783Sakolb 			 */
443c6402783Sakolb 			hflg++;
444c6402783Sakolb 			p1 = optarg;
445c6402783Sakolb 			do {
446c6402783Sakolb 				int id;
447c6402783Sakolb 
448c6402783Sakolb 				/*
449c6402783Sakolb 				 * Get all IDs in the list, verify for
450c6402783Sakolb 				 * correctness and place in lgrps array.
451c6402783Sakolb 				 */
452c6402783Sakolb 				parg = getarg(&p1);
453c6402783Sakolb 				/* Convert string to integer */
454c6402783Sakolb 				ret = str2id(parg, (pid_t *)&id, 0,
455d21c64a5SDavid Edmondson 				    MAX_LGRP_ID);
456c6402783Sakolb 				/* Complain if ID didn't parse correctly */
457c6402783Sakolb 				if (ret != 0) {
458c6402783Sakolb 					pgerrflg++;
459c6402783Sakolb 					(void) fprintf(stderr,
460c6402783Sakolb 					    gettext("ps: %s "), parg);
461c6402783Sakolb 					if (ret == EINVAL)
462c6402783Sakolb 						(void) fprintf(stderr,
463c6402783Sakolb 						    gettext("is an invalid "
464c6402783Sakolb 						    "non-numeric argument"));
465c6402783Sakolb 					else
466c6402783Sakolb 						(void) fprintf(stderr,
467c6402783Sakolb 						    gettext("exceeds valid "
468c6402783Sakolb 						    "range"));
469c6402783Sakolb 					(void) fprintf(stderr,
470c6402783Sakolb 					    gettext(" for -h option\n"));
471c6402783Sakolb 					continue;
472c6402783Sakolb 				}
473c6402783Sakolb 
474c6402783Sakolb 				/* Extend lgrps array if needed */
475c6402783Sakolb 				if (nlgrps == lgrps_size) {
476c6402783Sakolb 					/* Double the size of the lgrps array */
477c6402783Sakolb 					if (lgrps_size == 0)
478c6402783Sakolb 						lgrps_size = SIZ;
479c6402783Sakolb 					lgrps_size *= 2;
480c6402783Sakolb 					lgrps = Realloc(lgrps,
481c6402783Sakolb 					    lgrps_size * sizeof (int));
482c6402783Sakolb 				}
483c6402783Sakolb 				/* place the id in the lgrps table */
484c6402783Sakolb 				lgrps[nlgrps++] = id;
485c6402783Sakolb 			} while (*p1);
486c6402783Sakolb 			break;
4877c478bd9Sstevel@tonic-gate 		case 'l':		/* long listing */
4887c478bd9Sstevel@tonic-gate 			lflg++;
4897c478bd9Sstevel@tonic-gate 			break;
4907c478bd9Sstevel@tonic-gate 		case 'f':		/* full listing */
4917c478bd9Sstevel@tonic-gate 			fflg++;
4927c478bd9Sstevel@tonic-gate 			break;
4937c478bd9Sstevel@tonic-gate 		case 'j':
4947c478bd9Sstevel@tonic-gate 			jflg++;
4957c478bd9Sstevel@tonic-gate 			break;
4967c478bd9Sstevel@tonic-gate 		case 'c':
4977c478bd9Sstevel@tonic-gate 			/*
4987c478bd9Sstevel@tonic-gate 			 * Format output to reflect scheduler changes:
4997c478bd9Sstevel@tonic-gate 			 * high numbers for high priorities and don't
5007c478bd9Sstevel@tonic-gate 			 * print nice or p_cpu values.  'c' option only
5017c478bd9Sstevel@tonic-gate 			 * effective when used with 'l' or 'f' options.
5027c478bd9Sstevel@tonic-gate 			 */
5037c478bd9Sstevel@tonic-gate 			cflg++;
5047c478bd9Sstevel@tonic-gate 			break;
5057c478bd9Sstevel@tonic-gate 		case 'A':		/* list every process */
5067c478bd9Sstevel@tonic-gate 		case 'e':		/* (obsolete) list every process */
5077c478bd9Sstevel@tonic-gate 			Aflg++;
5087c478bd9Sstevel@tonic-gate 			tflg = Gflg = Uflg = uflg = pflg = gflg = sflg = 0;
509c6402783Sakolb 			zflg = hflg = 0;
5107c478bd9Sstevel@tonic-gate 			break;
5117c478bd9Sstevel@tonic-gate 		case 'a':
5127c478bd9Sstevel@tonic-gate 			/*
5137c478bd9Sstevel@tonic-gate 			 * Same as 'e' except no session group leaders
5147c478bd9Sstevel@tonic-gate 			 * and no non-terminal processes.
5157c478bd9Sstevel@tonic-gate 			 */
5167c478bd9Sstevel@tonic-gate 			aflg++;
5177c478bd9Sstevel@tonic-gate 			break;
5187c478bd9Sstevel@tonic-gate 		case 'd':	/* same as e except no session leaders */
5197c478bd9Sstevel@tonic-gate 			dflg++;
5207c478bd9Sstevel@tonic-gate 			break;
5217c478bd9Sstevel@tonic-gate 		case 'L':	/* show lwps */
5227c478bd9Sstevel@tonic-gate 			Lflg++;
5237c478bd9Sstevel@tonic-gate 			break;
5247c478bd9Sstevel@tonic-gate 		case 'P':	/* show bound processor */
5257c478bd9Sstevel@tonic-gate 			Pflg++;
5267c478bd9Sstevel@tonic-gate 			break;
5270a1278f2SGary Mills 		case 'W':	/* truncate long names */
5280a1278f2SGary Mills 			Wflg++;
5290a1278f2SGary Mills 			break;
5307c478bd9Sstevel@tonic-gate 		case 'y':	/* omit F & ADDR, report RSS & SZ in Kby */
5317c478bd9Sstevel@tonic-gate 			yflg++;
5327c478bd9Sstevel@tonic-gate 			break;
5337c478bd9Sstevel@tonic-gate 		case 'n':	/* no longer needed; retain as no-op */
5347c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
5357c478bd9Sstevel@tonic-gate 			    gettext("ps: warning: -n option ignored\n"));
5367c478bd9Sstevel@tonic-gate 			break;
5377c478bd9Sstevel@tonic-gate 		case 't':		/* terminals */
5387c478bd9Sstevel@tonic-gate #define	TSZ	30
5397c478bd9Sstevel@tonic-gate 			tflg++;
5407c478bd9Sstevel@tonic-gate 			p1 = optarg;
5417c478bd9Sstevel@tonic-gate 			do {
5427c478bd9Sstevel@tonic-gate 				char nambuf[TSZ+6];	/* for "/dev/" + '\0' */
5437c478bd9Sstevel@tonic-gate 				struct stat64 s;
5447c478bd9Sstevel@tonic-gate 				parg = getarg(&p1);
5457c478bd9Sstevel@tonic-gate 				p = Realloc(NULL, TSZ+1);	/* for '\0' */
5467c478bd9Sstevel@tonic-gate 				/* zero the buffer before using it */
5477c478bd9Sstevel@tonic-gate 				p[0] = '\0';
5487c478bd9Sstevel@tonic-gate 				size = TSZ;
5497c478bd9Sstevel@tonic-gate 				if (isdigit(*parg)) {
5507c478bd9Sstevel@tonic-gate 					(void) strcpy(p, "tty");
5517c478bd9Sstevel@tonic-gate 					size -= 3;
5527c478bd9Sstevel@tonic-gate 				}
5537c478bd9Sstevel@tonic-gate 				(void) strncat(p, parg, size);
5547c478bd9Sstevel@tonic-gate 				if (ntty == ttysz) {
5557c478bd9Sstevel@tonic-gate 					if ((ttysz *= 2) == 0)
5567c478bd9Sstevel@tonic-gate 						ttysz = NTTYS;
5577c478bd9Sstevel@tonic-gate 					tty = Realloc(tty,
5587c478bd9Sstevel@tonic-gate 					    (ttysz + 1) * sizeof (struct tty));
5597c478bd9Sstevel@tonic-gate 				}
5607c478bd9Sstevel@tonic-gate 				tty[ntty].tdev = PRNODEV;
5617c478bd9Sstevel@tonic-gate 				(void) strcpy(nambuf, "/dev/");
5627c478bd9Sstevel@tonic-gate 				(void) strcat(nambuf, p);
5637c478bd9Sstevel@tonic-gate 				if (stat64(nambuf, &s) == 0)
5647c478bd9Sstevel@tonic-gate 					tty[ntty].tdev = s.st_rdev;
5657c478bd9Sstevel@tonic-gate 				tty[ntty++].tname = p;
5667c478bd9Sstevel@tonic-gate 			} while (*p1);
5677c478bd9Sstevel@tonic-gate 			break;
5687c478bd9Sstevel@tonic-gate 		case 'p':		/* proc ids */
5697c478bd9Sstevel@tonic-gate 			pflg++;
5707c478bd9Sstevel@tonic-gate 			p1 = optarg;
5717c478bd9Sstevel@tonic-gate 			do {
5727c478bd9Sstevel@tonic-gate 				pid_t id;
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate 				parg = getarg(&p1);
5757c478bd9Sstevel@tonic-gate 				if ((ret = str2id(parg, &id, 0, maxpid)) != 0) {
5767c478bd9Sstevel@tonic-gate 					pgerrflg++;
5777c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
5787c478bd9Sstevel@tonic-gate 					    gettext("ps: %s "), parg);
5797c478bd9Sstevel@tonic-gate 					if (ret == EINVAL)
5807c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr,
5817c478bd9Sstevel@tonic-gate 						    gettext("is an invalid "
5827c478bd9Sstevel@tonic-gate 						    "non-numeric argument"));
5837c478bd9Sstevel@tonic-gate 					else
5847c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr,
5857c478bd9Sstevel@tonic-gate 						    gettext("exceeds valid "
5867c478bd9Sstevel@tonic-gate 						    "range"));
5877c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
5887c478bd9Sstevel@tonic-gate 					    gettext(" for -p option\n"));
5897c478bd9Sstevel@tonic-gate 					continue;
5907c478bd9Sstevel@tonic-gate 				}
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate 				if (npid == pidsz) {
5937c478bd9Sstevel@tonic-gate 					if ((pidsz *= 2) == 0)
5947c478bd9Sstevel@tonic-gate 						pidsz = SIZ;
5957c478bd9Sstevel@tonic-gate 					pid = Realloc(pid,
5967c478bd9Sstevel@tonic-gate 					    pidsz * sizeof (pid_t));
5977c478bd9Sstevel@tonic-gate 				}
5987c478bd9Sstevel@tonic-gate 				pid[npid++] = id;
5997c478bd9Sstevel@tonic-gate 			} while (*p1);
6007c478bd9Sstevel@tonic-gate 			break;
6017c478bd9Sstevel@tonic-gate 		case 's':		/* session */
6027c478bd9Sstevel@tonic-gate 			sflg++;
6037c478bd9Sstevel@tonic-gate 			p1 = optarg;
6047c478bd9Sstevel@tonic-gate 			do {
6057c478bd9Sstevel@tonic-gate 				pid_t id;
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate 				parg = getarg(&p1);
6087c478bd9Sstevel@tonic-gate 				if ((ret = str2id(parg, &id, 0, maxpid)) != 0) {
6097c478bd9Sstevel@tonic-gate 					pgerrflg++;
6107c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
6117c478bd9Sstevel@tonic-gate 					    gettext("ps: %s "), parg);
6127c478bd9Sstevel@tonic-gate 					if (ret == EINVAL)
6137c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr,
6147c478bd9Sstevel@tonic-gate 						    gettext("is an invalid "
6157c478bd9Sstevel@tonic-gate 						    "non-numeric argument"));
6167c478bd9Sstevel@tonic-gate 					else
6177c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr,
6187c478bd9Sstevel@tonic-gate 						    gettext("exceeds valid "
6197c478bd9Sstevel@tonic-gate 						    "range"));
6207c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
6217c478bd9Sstevel@tonic-gate 					    gettext(" for -s option\n"));
6227c478bd9Sstevel@tonic-gate 					continue;
6237c478bd9Sstevel@tonic-gate 				}
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate 				if (nsessid == sessidsz) {
6267c478bd9Sstevel@tonic-gate 					if ((sessidsz *= 2) == 0)
6277c478bd9Sstevel@tonic-gate 						sessidsz = SIZ;
6287c478bd9Sstevel@tonic-gate 					sessid = Realloc(sessid,
6297c478bd9Sstevel@tonic-gate 					    sessidsz * sizeof (pid_t));
6307c478bd9Sstevel@tonic-gate 				}
6317c478bd9Sstevel@tonic-gate 				sessid[nsessid++] = id;
6327c478bd9Sstevel@tonic-gate 			} while (*p1);
6337c478bd9Sstevel@tonic-gate 			break;
6347c478bd9Sstevel@tonic-gate 		case 'g':		/* proc group */
6357c478bd9Sstevel@tonic-gate 			gflg++;
6367c478bd9Sstevel@tonic-gate 			p1 = optarg;
6377c478bd9Sstevel@tonic-gate 			do {
6387c478bd9Sstevel@tonic-gate 				pid_t id;
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate 				parg = getarg(&p1);
6417c478bd9Sstevel@tonic-gate 				if ((ret = str2id(parg, &id, 0, maxpid)) != 0) {
6427c478bd9Sstevel@tonic-gate 					pgerrflg++;
6437c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
6447c478bd9Sstevel@tonic-gate 					    gettext("ps: %s "), parg);
6457c478bd9Sstevel@tonic-gate 					if (ret == EINVAL)
6467c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr,
6477c478bd9Sstevel@tonic-gate 						    gettext("is an invalid "
6487c478bd9Sstevel@tonic-gate 						    "non-numeric argument"));
6497c478bd9Sstevel@tonic-gate 					else
6507c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr,
6517c478bd9Sstevel@tonic-gate 						    gettext("exceeds valid "
6527c478bd9Sstevel@tonic-gate 						    "range"));
6537c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
6547c478bd9Sstevel@tonic-gate 					    gettext(" for -g option\n"));
6557c478bd9Sstevel@tonic-gate 					continue;
6567c478bd9Sstevel@tonic-gate 				}
6577c478bd9Sstevel@tonic-gate 
6587c478bd9Sstevel@tonic-gate 				if (ngrpid == grpidsz) {
6597c478bd9Sstevel@tonic-gate 					if ((grpidsz *= 2) == 0)
6607c478bd9Sstevel@tonic-gate 						grpidsz = SIZ;
6617c478bd9Sstevel@tonic-gate 					grpid = Realloc(grpid,
6627c478bd9Sstevel@tonic-gate 					    grpidsz * sizeof (pid_t));
6637c478bd9Sstevel@tonic-gate 				}
6647c478bd9Sstevel@tonic-gate 				grpid[ngrpid++] = id;
6657c478bd9Sstevel@tonic-gate 			} while (*p1);
6667c478bd9Sstevel@tonic-gate 			break;
6677c478bd9Sstevel@tonic-gate 		case 'u':		/* effective user name or number */
6687c478bd9Sstevel@tonic-gate 			uflg++;
6697c478bd9Sstevel@tonic-gate 			p1 = optarg;
6707c478bd9Sstevel@tonic-gate 			do {
6717c478bd9Sstevel@tonic-gate 				parg = getarg(&p1);
6727c478bd9Sstevel@tonic-gate 				add_ugentry(&euid_tbl, parg);
6737c478bd9Sstevel@tonic-gate 			} while (*p1);
6747c478bd9Sstevel@tonic-gate 			break;
6757c478bd9Sstevel@tonic-gate 		case 'U':		/* real user name or number */
6767c478bd9Sstevel@tonic-gate 			Uflg++;
6777c478bd9Sstevel@tonic-gate 			p1 = optarg;
6787c478bd9Sstevel@tonic-gate 			do {
6797c478bd9Sstevel@tonic-gate 				parg = getarg(&p1);
6807c478bd9Sstevel@tonic-gate 				add_ugentry(&ruid_tbl, parg);
6817c478bd9Sstevel@tonic-gate 			} while (*p1);
6827c478bd9Sstevel@tonic-gate 			break;
6837c478bd9Sstevel@tonic-gate 		case 'G':		/* real group name or number */
6847c478bd9Sstevel@tonic-gate 			Gflg++;
6857c478bd9Sstevel@tonic-gate 			p1 = optarg;
6867c478bd9Sstevel@tonic-gate 			do {
6877c478bd9Sstevel@tonic-gate 				parg = getarg(&p1);
6887c478bd9Sstevel@tonic-gate 				add_ugentry(&rgid_tbl, parg);
6897c478bd9Sstevel@tonic-gate 			} while (*p1);
6907c478bd9Sstevel@tonic-gate 			break;
6917c478bd9Sstevel@tonic-gate 		case 'o':		/* output format */
6927c478bd9Sstevel@tonic-gate 			p = optarg;
6937c478bd9Sstevel@tonic-gate 			while ((p = parse_format(p)) != NULL)
6947c478bd9Sstevel@tonic-gate 				;
6957c478bd9Sstevel@tonic-gate 			break;
6967c478bd9Sstevel@tonic-gate 		case 'z':		/* zone name or number */
6977c478bd9Sstevel@tonic-gate 			zflg++;
6987c478bd9Sstevel@tonic-gate 			p1 = optarg;
6997c478bd9Sstevel@tonic-gate 			do {
7007c478bd9Sstevel@tonic-gate 				zoneid_t id;
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate 				parg = getarg(&p1);
7037c478bd9Sstevel@tonic-gate 				if (zone_get_id(parg, &id) != 0) {
7047c478bd9Sstevel@tonic-gate 					pgerrflg++;
7057c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
7067c478bd9Sstevel@tonic-gate 					    gettext("ps: unknown zone %s\n"),
7077c478bd9Sstevel@tonic-gate 					    parg);
7087c478bd9Sstevel@tonic-gate 					continue;
7097c478bd9Sstevel@tonic-gate 				}
7107c478bd9Sstevel@tonic-gate 
7117c478bd9Sstevel@tonic-gate 				if (nzoneid == zoneidsz) {
7127c478bd9Sstevel@tonic-gate 					if ((zoneidsz *= 2) == 0)
7137c478bd9Sstevel@tonic-gate 						zoneidsz = SIZ;
7147c478bd9Sstevel@tonic-gate 					zoneid = Realloc(zoneid,
7157c478bd9Sstevel@tonic-gate 					    zoneidsz * sizeof (zoneid_t));
7167c478bd9Sstevel@tonic-gate 				}
7177c478bd9Sstevel@tonic-gate 				zoneid[nzoneid++] = id;
7187c478bd9Sstevel@tonic-gate 			} while (*p1);
7197c478bd9Sstevel@tonic-gate 			break;
7207c478bd9Sstevel@tonic-gate 		case 'Z':		/* show zone name */
7217c478bd9Sstevel@tonic-gate 			Zflg++;
7227c478bd9Sstevel@tonic-gate 			break;
7237c478bd9Sstevel@tonic-gate 		default:			/* error on ? */
7247c478bd9Sstevel@tonic-gate 			errflg++;
7257c478bd9Sstevel@tonic-gate 			break;
7267c478bd9Sstevel@tonic-gate 		}
7277c478bd9Sstevel@tonic-gate 
7287c478bd9Sstevel@tonic-gate 	if (errflg || optind < argc || pgerrflg)
7297c478bd9Sstevel@tonic-gate 		usage();
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate 	if (tflg)
7327c478bd9Sstevel@tonic-gate 		tty[ntty].tname = NULL;
7337c478bd9Sstevel@tonic-gate 	/*
7347c478bd9Sstevel@tonic-gate 	 * If an appropriate option has not been specified, use the
7357c478bd9Sstevel@tonic-gate 	 * current terminal and effective uid as the default.
7367c478bd9Sstevel@tonic-gate 	 */
737c6402783Sakolb 	if (!(aflg|Aflg|dflg|Gflg|hflg|Uflg|uflg|tflg|pflg|gflg|sflg|zflg)) {
7387c478bd9Sstevel@tonic-gate 		psinfo_t info;
7397c478bd9Sstevel@tonic-gate 		int procfd;
7407c478bd9Sstevel@tonic-gate 		char *name;
7417c478bd9Sstevel@tonic-gate 		char pname[100];
7427c478bd9Sstevel@tonic-gate 
7437c478bd9Sstevel@tonic-gate 		/* get our own controlling tty name using /proc */
7447c478bd9Sstevel@tonic-gate 		(void) snprintf(pname, sizeof (pname),
7457c478bd9Sstevel@tonic-gate 		    "%s/self/psinfo", procdir);
7467c478bd9Sstevel@tonic-gate 		if ((procfd = open(pname, O_RDONLY)) < 0 ||
7477c478bd9Sstevel@tonic-gate 		    read(procfd, (char *)&info, sizeof (info)) < 0 ||
7487c478bd9Sstevel@tonic-gate 		    info.pr_ttydev == PRNODEV) {
7497c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
7507c478bd9Sstevel@tonic-gate 			    gettext("ps: no controlling terminal\n"));
7517c478bd9Sstevel@tonic-gate 			exit(1);
7527c478bd9Sstevel@tonic-gate 		}
7537c478bd9Sstevel@tonic-gate 		(void) close(procfd);
7547c478bd9Sstevel@tonic-gate 
7557c478bd9Sstevel@tonic-gate 		i = 0;
7567c478bd9Sstevel@tonic-gate 		name = gettty(&info);
7577c478bd9Sstevel@tonic-gate 		if (*name == '?') {
7587c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
7597c478bd9Sstevel@tonic-gate 			    gettext("ps: can't find controlling terminal\n"));
7607c478bd9Sstevel@tonic-gate 			exit(1);
7617c478bd9Sstevel@tonic-gate 		}
7627c478bd9Sstevel@tonic-gate 		if (ntty == ttysz) {
7637c478bd9Sstevel@tonic-gate 			if ((ttysz *= 2) == 0)
7647c478bd9Sstevel@tonic-gate 				ttysz = NTTYS;
7657c478bd9Sstevel@tonic-gate 			tty = Realloc(tty, (ttysz + 1) * sizeof (struct tty));
7667c478bd9Sstevel@tonic-gate 		}
7677c478bd9Sstevel@tonic-gate 		tty[ntty].tdev = info.pr_ttydev;
7687c478bd9Sstevel@tonic-gate 		tty[ntty++].tname = name;
7697c478bd9Sstevel@tonic-gate 		tty[ntty].tname = NULL;
7707c478bd9Sstevel@tonic-gate 		tflg++;
7717c478bd9Sstevel@tonic-gate 		tuid = getuid();
7727c478bd9Sstevel@tonic-gate 	}
7737c478bd9Sstevel@tonic-gate 	if (Aflg) {
7747c478bd9Sstevel@tonic-gate 		Gflg = Uflg = uflg = pflg = sflg = gflg = aflg = dflg = 0;
775c6402783Sakolb 		zflg = hflg = 0;
7767c478bd9Sstevel@tonic-gate 	}
7777c478bd9Sstevel@tonic-gate 	if (Aflg | aflg | dflg)
7787c478bd9Sstevel@tonic-gate 		tflg = 0;
7797c478bd9Sstevel@tonic-gate 
7807c478bd9Sstevel@tonic-gate 	i = 0;		/* prepare to exit on name lookup errors */
7817c478bd9Sstevel@tonic-gate 	i += uconv(&euid_tbl);
7827c478bd9Sstevel@tonic-gate 	i += uconv(&ruid_tbl);
7837c478bd9Sstevel@tonic-gate 	i += gconv(&egid_tbl);
7847c478bd9Sstevel@tonic-gate 	i += gconv(&rgid_tbl);
7857c478bd9Sstevel@tonic-gate 	if (i)
7867c478bd9Sstevel@tonic-gate 		exit(1);
7877c478bd9Sstevel@tonic-gate 
7887c478bd9Sstevel@tonic-gate 	/* allocate a buffer for lwpsinfo structures */
7897c478bd9Sstevel@tonic-gate 	lpbufsize = 4096;
7907c478bd9Sstevel@tonic-gate 	if (Lflg && (lpsinfobuf = malloc(lpbufsize)) == NULL) {
7917c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
7927c478bd9Sstevel@tonic-gate 		    gettext("ps: no memory\n"));
7937c478bd9Sstevel@tonic-gate 		exit(1);
7947c478bd9Sstevel@tonic-gate 	}
7957c478bd9Sstevel@tonic-gate 
7967c478bd9Sstevel@tonic-gate 	if (fields) {	/* print user-specified header */
7977c478bd9Sstevel@tonic-gate 		if (do_header) {
7987c478bd9Sstevel@tonic-gate 			struct field *f;
7997c478bd9Sstevel@tonic-gate 
8007c478bd9Sstevel@tonic-gate 			for (f = fields; f != NULL; f = f->next) {
8017c478bd9Sstevel@tonic-gate 				if (f != fields)
8027c478bd9Sstevel@tonic-gate 					(void) printf(" ");
8037c478bd9Sstevel@tonic-gate 				switch (f->fname) {
8047c478bd9Sstevel@tonic-gate 				case F_TTY:
8057c478bd9Sstevel@tonic-gate 					(void) printf("%-*s",
8067c478bd9Sstevel@tonic-gate 					    f->width, f->header);
8077c478bd9Sstevel@tonic-gate 					break;
808*ab618543SJohn Levon 				case F_LWPNAME:
8097c478bd9Sstevel@tonic-gate 				case F_FNAME:
8107c478bd9Sstevel@tonic-gate 				case F_COMM:
8117c478bd9Sstevel@tonic-gate 				case F_ARGS:
8127c478bd9Sstevel@tonic-gate 					/*
8137c478bd9Sstevel@tonic-gate 					 * Print these headers full width
8147c478bd9Sstevel@tonic-gate 					 * unless they appear at the end.
8157c478bd9Sstevel@tonic-gate 					 */
8167c478bd9Sstevel@tonic-gate 					if (f->next != NULL) {
8177c478bd9Sstevel@tonic-gate 						(void) printf("%-*s",
8187c478bd9Sstevel@tonic-gate 						    f->width, f->header);
8197c478bd9Sstevel@tonic-gate 					} else {
8207c478bd9Sstevel@tonic-gate 						(void) printf("%s",
8217c478bd9Sstevel@tonic-gate 						    f->header);
8227c478bd9Sstevel@tonic-gate 					}
8237c478bd9Sstevel@tonic-gate 					break;
8247c478bd9Sstevel@tonic-gate 				default:
8257c478bd9Sstevel@tonic-gate 					(void) printf("%*s",
8267c478bd9Sstevel@tonic-gate 					    f->width, f->header);
8277c478bd9Sstevel@tonic-gate 					break;
8287c478bd9Sstevel@tonic-gate 				}
8297c478bd9Sstevel@tonic-gate 			}
8307c478bd9Sstevel@tonic-gate 			(void) printf("\n");
8317c478bd9Sstevel@tonic-gate 		}
8327c478bd9Sstevel@tonic-gate 	} else {	/* print standard header */
833f5c9e9f9SCasper H.S. Dik 		/*
834f5c9e9f9SCasper H.S. Dik 		 * All fields before 'PID' are printed with a trailing space
835f5c9e9f9SCasper H.S. Dik 		 * as a separator and that is how we print the headers too.
836f5c9e9f9SCasper H.S. Dik 		 */
8377c478bd9Sstevel@tonic-gate 		if (lflg) {
8387c478bd9Sstevel@tonic-gate 			if (yflg)
839f5c9e9f9SCasper H.S. Dik 				(void) printf("S ");
8407c478bd9Sstevel@tonic-gate 			else
841f5c9e9f9SCasper H.S. Dik 				(void) printf(" F S ");
8427c478bd9Sstevel@tonic-gate 		}
8437c478bd9Sstevel@tonic-gate 		if (Zflg)
844f5c9e9f9SCasper H.S. Dik 			(void) printf("    ZONE ");
8457c478bd9Sstevel@tonic-gate 		if (fflg) {
846f5c9e9f9SCasper H.S. Dik 			(void) printf("     UID ");
8477c478bd9Sstevel@tonic-gate 		} else if (lflg)
848f5c9e9f9SCasper H.S. Dik 			(void) printf("   UID ");
8497c478bd9Sstevel@tonic-gate 
850f5c9e9f9SCasper H.S. Dik 		(void) printf("%*s", pidwidth,  "PID");
8517c478bd9Sstevel@tonic-gate 		if (lflg || fflg)
8527c478bd9Sstevel@tonic-gate 			(void) printf(" %*s", pidwidth, "PPID");
8537c478bd9Sstevel@tonic-gate 		if (jflg)
8547c478bd9Sstevel@tonic-gate 			(void) printf(" %*s %*s", pidwidth, "PGID",
8557c478bd9Sstevel@tonic-gate 			    pidwidth, "SID");
8567c478bd9Sstevel@tonic-gate 		if (Lflg)
8577c478bd9Sstevel@tonic-gate 			(void) printf("   LWP");
8587c478bd9Sstevel@tonic-gate 		if (Pflg)
8597c478bd9Sstevel@tonic-gate 			(void) printf(" PSR");
8607c478bd9Sstevel@tonic-gate 		if (Lflg && fflg)
8617c478bd9Sstevel@tonic-gate 			(void) printf("  NLWP");
8627c478bd9Sstevel@tonic-gate 		if (cflg)
8637c478bd9Sstevel@tonic-gate 			(void) printf("  CLS PRI");
8647c478bd9Sstevel@tonic-gate 		else if (lflg || fflg) {
8657c478bd9Sstevel@tonic-gate 			(void) printf("   C");
8667c478bd9Sstevel@tonic-gate 			if (lflg)
8677c478bd9Sstevel@tonic-gate 				(void) printf(" PRI NI");
8687c478bd9Sstevel@tonic-gate 		}
8697c478bd9Sstevel@tonic-gate 		if (lflg) {
8707c478bd9Sstevel@tonic-gate 			if (yflg)
8717c478bd9Sstevel@tonic-gate 				(void) printf("   RSS     SZ    WCHAN");
8727c478bd9Sstevel@tonic-gate 			else
8737c478bd9Sstevel@tonic-gate 				(void) printf("     ADDR     SZ    WCHAN");
8747c478bd9Sstevel@tonic-gate 		}
8757c478bd9Sstevel@tonic-gate 		if (fflg)
87616f94f58Ssayama 			(void) printf(" %s", loc_stime_str);
877c6402783Sakolb 		if (Hflg)
878c6402783Sakolb 			(void) printf(" LGRP");
8797c478bd9Sstevel@tonic-gate 		if (Lflg)
8807c478bd9Sstevel@tonic-gate 			(void) printf(" TTY        LTIME CMD\n");
8817c478bd9Sstevel@tonic-gate 		else
8827c478bd9Sstevel@tonic-gate 			(void) printf(" TTY         TIME CMD\n");
8837c478bd9Sstevel@tonic-gate 	}
8847c478bd9Sstevel@tonic-gate 
8857c478bd9Sstevel@tonic-gate 
886c6402783Sakolb 	if (pflg && !(aflg|Aflg|dflg|Gflg|Uflg|uflg|hflg|tflg|gflg|sflg|zflg) &&
8877c478bd9Sstevel@tonic-gate 	    npid <= PTHRESHOLD) {
8887c478bd9Sstevel@tonic-gate 		/*
8897c478bd9Sstevel@tonic-gate 		 * If we are looking at specific processes go straight
8907c478bd9Sstevel@tonic-gate 		 * to their /proc entries and don't scan /proc.
8917c478bd9Sstevel@tonic-gate 		 */
8927c478bd9Sstevel@tonic-gate 		int i;
8937c478bd9Sstevel@tonic-gate 
8947c478bd9Sstevel@tonic-gate 		(void) qsort(pid, npid, sizeof (pid_t), pidcmp);
8957c478bd9Sstevel@tonic-gate 		for (i = 0; i < npid; i++) {
8967c478bd9Sstevel@tonic-gate 			char pname[12];
8977c478bd9Sstevel@tonic-gate 
8987c478bd9Sstevel@tonic-gate 			if (i >= 1 && pid[i] == pid[i - 1])
8997c478bd9Sstevel@tonic-gate 				continue;
9007c478bd9Sstevel@tonic-gate 			(void) sprintf(pname, "%d", (int)pid[i]);
9017c478bd9Sstevel@tonic-gate 			if (print_proc(pname) == 0)
9027c478bd9Sstevel@tonic-gate 				retcode = 0;
9037c478bd9Sstevel@tonic-gate 		}
9047c478bd9Sstevel@tonic-gate 	} else {
9057c478bd9Sstevel@tonic-gate 		/*
9067c478bd9Sstevel@tonic-gate 		 * Determine which processes to print info about by searching
9077c478bd9Sstevel@tonic-gate 		 * the /proc directory and looking at each process.
9087c478bd9Sstevel@tonic-gate 		 */
9097c478bd9Sstevel@tonic-gate 		if ((dirp = opendir(procdir)) == NULL) {
9107c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
9117c478bd9Sstevel@tonic-gate 			    gettext("ps: cannot open PROC directory %s\n"),
9127c478bd9Sstevel@tonic-gate 			    procdir);
9137c478bd9Sstevel@tonic-gate 			exit(1);
9147c478bd9Sstevel@tonic-gate 		}
9157c478bd9Sstevel@tonic-gate 
9167c478bd9Sstevel@tonic-gate 		/* for each active process --- */
917e503abb7SIgor Kozhukhov 		while ((dentp = readdir(dirp)) != NULL) {
9187c478bd9Sstevel@tonic-gate 			if (dentp->d_name[0] == '.')    /* skip . and .. */
9197c478bd9Sstevel@tonic-gate 				continue;
9207c478bd9Sstevel@tonic-gate 			if (print_proc(dentp->d_name) == 0)
9217c478bd9Sstevel@tonic-gate 				retcode = 0;
9227c478bd9Sstevel@tonic-gate 		}
9237c478bd9Sstevel@tonic-gate 
9247c478bd9Sstevel@tonic-gate 		(void) closedir(dirp);
9257c478bd9Sstevel@tonic-gate 	}
9267c478bd9Sstevel@tonic-gate 	return (retcode);
9277c478bd9Sstevel@tonic-gate }
9287c478bd9Sstevel@tonic-gate 
9297c478bd9Sstevel@tonic-gate 
9307c478bd9Sstevel@tonic-gate int
print_proc(char * pid_name)9317c478bd9Sstevel@tonic-gate print_proc(char *pid_name)
9327c478bd9Sstevel@tonic-gate {
9337c478bd9Sstevel@tonic-gate 	char	pname[PATH_MAX];
9347c478bd9Sstevel@tonic-gate 	int	pdlen;
9357c478bd9Sstevel@tonic-gate 	int	found;
9367c478bd9Sstevel@tonic-gate 	int	procfd; /* filedescriptor for /proc/nnnnn/psinfo */
9377c478bd9Sstevel@tonic-gate 	char	*tp;    /* ptr to ttyname,  if any */
9387c478bd9Sstevel@tonic-gate 	psinfo_t info;  /* process information from /proc */
9397c478bd9Sstevel@tonic-gate 	lwpsinfo_t *lwpsinfo;   /* array of lwpsinfo structs */
9407c478bd9Sstevel@tonic-gate 
9417c478bd9Sstevel@tonic-gate 	pdlen = snprintf(pname, sizeof (pname), "%s/%s/", procdir, pid_name);
9427c478bd9Sstevel@tonic-gate 	if (pdlen >= sizeof (pname) - 10)
9437c478bd9Sstevel@tonic-gate 		return (1);
9447c478bd9Sstevel@tonic-gate retry:
9457c478bd9Sstevel@tonic-gate 	(void) strcpy(&pname[pdlen], "psinfo");
9467c478bd9Sstevel@tonic-gate 	if ((procfd = open(pname, O_RDONLY)) == -1) {
9477c478bd9Sstevel@tonic-gate 		/* Process may have exited meanwhile. */
9487c478bd9Sstevel@tonic-gate 		return (1);
9497c478bd9Sstevel@tonic-gate 	}
9507c478bd9Sstevel@tonic-gate 	/*
9517c478bd9Sstevel@tonic-gate 	 * Get the info structure for the process and close quickly.
9527c478bd9Sstevel@tonic-gate 	 */
9537c478bd9Sstevel@tonic-gate 	if (read(procfd, (char *)&info, sizeof (info)) < 0) {
9547c478bd9Sstevel@tonic-gate 		int	saverr = errno;
9557c478bd9Sstevel@tonic-gate 
9567c478bd9Sstevel@tonic-gate 		(void) close(procfd);
9577c478bd9Sstevel@tonic-gate 		if (saverr == EAGAIN)
9587c478bd9Sstevel@tonic-gate 			goto retry;
9597c478bd9Sstevel@tonic-gate 		if (saverr != ENOENT)
9607c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
9617c478bd9Sstevel@tonic-gate 			    gettext("ps: read() on %s: %s\n"),
9627c478bd9Sstevel@tonic-gate 			    pname, err_string(saverr));
9637c478bd9Sstevel@tonic-gate 		return (1);
9647c478bd9Sstevel@tonic-gate 	}
9657c478bd9Sstevel@tonic-gate 	(void) close(procfd);
9667c478bd9Sstevel@tonic-gate 
9677c478bd9Sstevel@tonic-gate 	found = 0;
9687c478bd9Sstevel@tonic-gate 	if (info.pr_lwp.pr_state == 0)	/* can't happen? */
9697c478bd9Sstevel@tonic-gate 		return (1);
9707c478bd9Sstevel@tonic-gate 
9717c478bd9Sstevel@tonic-gate 	/*
9727c478bd9Sstevel@tonic-gate 	 * Omit session group leaders for 'a' and 'd' options.
9737c478bd9Sstevel@tonic-gate 	 */
9747c478bd9Sstevel@tonic-gate 	if ((info.pr_pid == info.pr_sid) && (dflg || aflg))
9757c478bd9Sstevel@tonic-gate 		return (1);
9767c478bd9Sstevel@tonic-gate 	if (Aflg || dflg)
9777c478bd9Sstevel@tonic-gate 		found++;
9787c478bd9Sstevel@tonic-gate 	else if (pflg && search(pid, npid, info.pr_pid))
9797c478bd9Sstevel@tonic-gate 		found++;	/* ppid in p option arg list */
980f48205beScasper 	else if (uflg && ugfind((id_t)info.pr_euid, &euid_tbl))
9817c478bd9Sstevel@tonic-gate 		found++;	/* puid in u option arg list */
982f48205beScasper 	else if (Uflg && ugfind((id_t)info.pr_uid, &ruid_tbl))
9837c478bd9Sstevel@tonic-gate 		found++;	/* puid in U option arg list */
9847c478bd9Sstevel@tonic-gate #ifdef NOT_YET
985f48205beScasper 	else if (gflg && ugfind((id_t)info.pr_egid, &egid_tbl))
9867c478bd9Sstevel@tonic-gate 		found++;	/* pgid in g option arg list */
9877c478bd9Sstevel@tonic-gate #endif	/* NOT_YET */
988f48205beScasper 	else if (Gflg && ugfind((id_t)info.pr_gid, &rgid_tbl))
9897c478bd9Sstevel@tonic-gate 		found++;	/* pgid in G option arg list */
9907c478bd9Sstevel@tonic-gate 	else if (gflg && search(grpid, ngrpid, info.pr_pgid))
9917c478bd9Sstevel@tonic-gate 		found++;	/* grpid in g option arg list */
9927c478bd9Sstevel@tonic-gate 	else if (sflg && search(sessid, nsessid, info.pr_sid))
9937c478bd9Sstevel@tonic-gate 		found++;	/* sessid in s option arg list */
9947c478bd9Sstevel@tonic-gate 	else if (zflg && search(zoneid, nzoneid, info.pr_zoneid))
9957c478bd9Sstevel@tonic-gate 		found++;	/* zoneid in z option arg list */
996c6402783Sakolb 	else if (hflg && search((pid_t *)lgrps, nlgrps, info.pr_lwp.pr_lgrp))
997c6402783Sakolb 		found++;	/* home lgroup in h option arg list */
9987c478bd9Sstevel@tonic-gate 	if (!found && !tflg && !aflg)
9997c478bd9Sstevel@tonic-gate 		return (1);
10007c478bd9Sstevel@tonic-gate 	if (!prfind(found, &info, &tp))
10017c478bd9Sstevel@tonic-gate 		return (1);
10027c478bd9Sstevel@tonic-gate 	if (Lflg && (info.pr_nlwp + info.pr_nzomb) > 1) {
10037c478bd9Sstevel@tonic-gate 		ssize_t prsz;
10047c478bd9Sstevel@tonic-gate 
10057c478bd9Sstevel@tonic-gate 		(void) strcpy(&pname[pdlen], "lpsinfo");
10067c478bd9Sstevel@tonic-gate 		if ((procfd = open(pname, O_RDONLY)) == -1)
10077c478bd9Sstevel@tonic-gate 			return (1);
10087c478bd9Sstevel@tonic-gate 		/*
10097c478bd9Sstevel@tonic-gate 		 * Get the info structures for the lwps.
10107c478bd9Sstevel@tonic-gate 		 */
10117c478bd9Sstevel@tonic-gate 		prsz = read(procfd, lpsinfobuf, lpbufsize);
10127c478bd9Sstevel@tonic-gate 		if (prsz == -1) {
10137c478bd9Sstevel@tonic-gate 			int	saverr = errno;
10147c478bd9Sstevel@tonic-gate 
10157c478bd9Sstevel@tonic-gate 			(void) close(procfd);
10167c478bd9Sstevel@tonic-gate 			if (saverr == EAGAIN)
10177c478bd9Sstevel@tonic-gate 				goto retry;
10187c478bd9Sstevel@tonic-gate 			if (saverr != ENOENT)
10197c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
10207c478bd9Sstevel@tonic-gate 				    gettext("ps: read() on %s: %s\n"),
10217c478bd9Sstevel@tonic-gate 				    pname, err_string(saverr));
10227c478bd9Sstevel@tonic-gate 			return (1);
10237c478bd9Sstevel@tonic-gate 		}
10247c478bd9Sstevel@tonic-gate 		(void) close(procfd);
10257c478bd9Sstevel@tonic-gate 		if (prsz == lpbufsize) {
10267c478bd9Sstevel@tonic-gate 			/*
10277c478bd9Sstevel@tonic-gate 			 * buffer overflow. Realloc new buffer.
10287c478bd9Sstevel@tonic-gate 			 * Error handling is done in Realloc().
10297c478bd9Sstevel@tonic-gate 			 */
10307c478bd9Sstevel@tonic-gate 			lpbufsize *= 2;
10317c478bd9Sstevel@tonic-gate 			lpsinfobuf = Realloc(lpsinfobuf, lpbufsize);
10327c478bd9Sstevel@tonic-gate 			goto retry;
10337c478bd9