xref: /titanic_41/usr/src/cmd/ptools/ptree/ptree.c (revision 0630b7d7c67e829f6a241c545de26de16348de94)
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
5f48205beScasper  * Common Development and Distribution License (the "License").
6f48205beScasper  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22f48205beScasper  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * ptree -- print family tree of processes
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include <assert.h>
337c478bd9Sstevel@tonic-gate #include <stdio.h>
347c478bd9Sstevel@tonic-gate #include <string.h>
357c478bd9Sstevel@tonic-gate #include <errno.h>
367c478bd9Sstevel@tonic-gate #include <fcntl.h>
377c478bd9Sstevel@tonic-gate #include <sys/types.h>
387c478bd9Sstevel@tonic-gate #include <sys/termios.h>
397c478bd9Sstevel@tonic-gate #include <unistd.h>
407c478bd9Sstevel@tonic-gate #include <stdlib.h>
417c478bd9Sstevel@tonic-gate #include <dirent.h>
427c478bd9Sstevel@tonic-gate #include <pwd.h>
437c478bd9Sstevel@tonic-gate #include <libproc.h>
447c478bd9Sstevel@tonic-gate #include <libzonecfg.h>
457c478bd9Sstevel@tonic-gate #include <limits.h>
467c478bd9Sstevel@tonic-gate #include <libcontract.h>
477c478bd9Sstevel@tonic-gate #include <sys/contract.h>
487c478bd9Sstevel@tonic-gate #include <sys/ctfs.h>
497c478bd9Sstevel@tonic-gate #include <libcontract_priv.h>
507c478bd9Sstevel@tonic-gate #include <sys/stat.h>
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate #define	FAKEDPID0(p)	(p->pid == 0 && p->psargs[0] == '\0')
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate typedef struct ps {
557c478bd9Sstevel@tonic-gate 	int	done;
567c478bd9Sstevel@tonic-gate 	uid_t	uid;
577c478bd9Sstevel@tonic-gate 	uid_t	gid;
587c478bd9Sstevel@tonic-gate 	pid_t	pid;		/* pid == -1 indicates this is a contract */
597c478bd9Sstevel@tonic-gate 	pid_t	ppid;
607c478bd9Sstevel@tonic-gate 	pid_t	pgrp;
617c478bd9Sstevel@tonic-gate 	pid_t	sid;
627c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
637c478bd9Sstevel@tonic-gate 	ctid_t	ctid;
647c478bd9Sstevel@tonic-gate 	timestruc_t start;
657c478bd9Sstevel@tonic-gate 	char	psargs[PRARGSZ];
667c478bd9Sstevel@tonic-gate 	struct ps *pp;		/* parent */
677c478bd9Sstevel@tonic-gate 	struct ps *sp;		/* sibling */
687c478bd9Sstevel@tonic-gate 	struct ps *cp;		/* child */
697c478bd9Sstevel@tonic-gate } ps_t;
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate static	ps_t	**ps;		/* array of ps_t's */
727c478bd9Sstevel@tonic-gate static	unsigned psize;		/* size of array */
737c478bd9Sstevel@tonic-gate static	int	nps;		/* number of ps_t's */
747c478bd9Sstevel@tonic-gate static	ps_t	**ctps;		/* array of contract ps_t's */
757c478bd9Sstevel@tonic-gate static	unsigned ctsize;	/* size of contract array */
767c478bd9Sstevel@tonic-gate static	int	nctps;		/* number of contract ps_t's */
777c478bd9Sstevel@tonic-gate static	ps_t	*proc0;		/* process 0 */
787c478bd9Sstevel@tonic-gate static	ps_t	*proc1;		/* process 1 */
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate static	char	*command;
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate static	int	aflag = 0;
837c478bd9Sstevel@tonic-gate static	int	cflag = 0;
847c478bd9Sstevel@tonic-gate static	int	zflag = 0;
857c478bd9Sstevel@tonic-gate static	zoneid_t zoneid;
867c478bd9Sstevel@tonic-gate static	int	columns = 80;
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate static void markprocs(ps_t *p);
897c478bd9Sstevel@tonic-gate static int printone(ps_t *p, int level);
907c478bd9Sstevel@tonic-gate static void insertchild(ps_t *, ps_t *);
917c478bd9Sstevel@tonic-gate static void prsort(ps_t *p);
927c478bd9Sstevel@tonic-gate static void printsubtree(ps_t *p, int level);
937c478bd9Sstevel@tonic-gate static zoneid_t getzone(char *arg);
947c478bd9Sstevel@tonic-gate static ps_t *fakepid0(void);
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv)977c478bd9Sstevel@tonic-gate main(int argc, char **argv)
987c478bd9Sstevel@tonic-gate {
997c478bd9Sstevel@tonic-gate 	psinfo_t info;	/* process information structure from /proc */
1007c478bd9Sstevel@tonic-gate 	int opt;
1017c478bd9Sstevel@tonic-gate 	int errflg = 0;
1027c478bd9Sstevel@tonic-gate 	struct winsize winsize;
1037c478bd9Sstevel@tonic-gate 	char *s;
1047c478bd9Sstevel@tonic-gate 	int n;
1057c478bd9Sstevel@tonic-gate 	int retc = 0;
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 	DIR *dirp;
1087c478bd9Sstevel@tonic-gate 	struct dirent *dentp;
1097c478bd9Sstevel@tonic-gate 	char	pname[100];
1107c478bd9Sstevel@tonic-gate 	int	pdlen;
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 	ps_t *p;
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 	if ((command = strrchr(argv[0], '/')) == NULL)
1157c478bd9Sstevel@tonic-gate 		command = argv[0];
1167c478bd9Sstevel@tonic-gate 	else
1177c478bd9Sstevel@tonic-gate 		command++;
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 	/* options */
1207c478bd9Sstevel@tonic-gate 	while ((opt = getopt(argc, argv, "acz:")) != EOF) {
1217c478bd9Sstevel@tonic-gate 		switch (opt) {
1227c478bd9Sstevel@tonic-gate 		case 'a':		/* include children of process 0 */
1237c478bd9Sstevel@tonic-gate 			aflag = 1;
1247c478bd9Sstevel@tonic-gate 			break;
1257c478bd9Sstevel@tonic-gate 		case 'c':		/* display contract ownership */
1267c478bd9Sstevel@tonic-gate 			aflag = cflag = 1;
1277c478bd9Sstevel@tonic-gate 			break;
1287c478bd9Sstevel@tonic-gate 		case 'z':		/* only processes in given zone */
1297c478bd9Sstevel@tonic-gate 			zflag = 1;
1307c478bd9Sstevel@tonic-gate 			zoneid = getzone(optarg);
1317c478bd9Sstevel@tonic-gate 			break;
1327c478bd9Sstevel@tonic-gate 		default:
1337c478bd9Sstevel@tonic-gate 			errflg = 1;
1347c478bd9Sstevel@tonic-gate 			break;
1357c478bd9Sstevel@tonic-gate 		}
1367c478bd9Sstevel@tonic-gate 	}
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 	argc -= optind;
1397c478bd9Sstevel@tonic-gate 	argv += optind;
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 	if (errflg) {
1427c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
1437c478bd9Sstevel@tonic-gate 		    "usage:\t%s [-ac] [-z zone] [ {pid|user} ... ]\n",
1447c478bd9Sstevel@tonic-gate 		    command);
1457c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
1467c478bd9Sstevel@tonic-gate 		    "  (show process trees)\n");
1477c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
1487c478bd9Sstevel@tonic-gate 		    "  list can include process-ids and user names\n");
1497c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
1507c478bd9Sstevel@tonic-gate 		    "  -a : include children of process 0\n");
1517c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
1527c478bd9Sstevel@tonic-gate 		    "  -c : show contract ownership\n");
1537c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
1547c478bd9Sstevel@tonic-gate 		    "  -z : print only processes in given zone\n");
1557c478bd9Sstevel@tonic-gate 		return (2);
1567c478bd9Sstevel@tonic-gate 	}
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	/*
1597c478bd9Sstevel@tonic-gate 	 * Kind of a hack to determine the width of the output...
1607c478bd9Sstevel@tonic-gate 	 */
1617c478bd9Sstevel@tonic-gate 	if ((s = getenv("COLUMNS")) != NULL && (n = atoi(s)) > 0)
1627c478bd9Sstevel@tonic-gate 		columns = n;
1637c478bd9Sstevel@tonic-gate 	else if (isatty(fileno(stdout)) &&
1647c478bd9Sstevel@tonic-gate 	    ioctl(fileno(stdout), TIOCGWINSZ, &winsize) == 0 &&
1657c478bd9Sstevel@tonic-gate 	    winsize.ws_col != 0)
1667c478bd9Sstevel@tonic-gate 		columns = winsize.ws_col;
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	nps = 0;
1697c478bd9Sstevel@tonic-gate 	psize = 0;
1707c478bd9Sstevel@tonic-gate 	ps = NULL;
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 	/*
1737c478bd9Sstevel@tonic-gate 	 * Search the /proc directory for all processes.
1747c478bd9Sstevel@tonic-gate 	 */
1757c478bd9Sstevel@tonic-gate 	if ((dirp = opendir("/proc")) == NULL) {
1767c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: cannot open /proc directory\n",
1777c478bd9Sstevel@tonic-gate 		    command);
1787c478bd9Sstevel@tonic-gate 		return (1);
1797c478bd9Sstevel@tonic-gate 	}
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	(void) strcpy(pname, "/proc");
1827c478bd9Sstevel@tonic-gate 	pdlen = strlen(pname);
1837c478bd9Sstevel@tonic-gate 	pname[pdlen++] = '/';
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	/* for each active process --- */
1867c478bd9Sstevel@tonic-gate 	while (dentp = readdir(dirp)) {
1877c478bd9Sstevel@tonic-gate 		int	procfd;	/* filedescriptor for /proc/nnnnn/psinfo */
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 		if (dentp->d_name[0] == '.')		/* skip . and .. */
1907c478bd9Sstevel@tonic-gate 			continue;
1917c478bd9Sstevel@tonic-gate 		(void) strcpy(pname + pdlen, dentp->d_name);
1927c478bd9Sstevel@tonic-gate 		(void) strcpy(pname + strlen(pname), "/psinfo");
1937c478bd9Sstevel@tonic-gate retry:
1947c478bd9Sstevel@tonic-gate 		if ((procfd = open(pname, O_RDONLY)) == -1)
1957c478bd9Sstevel@tonic-gate 			continue;
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 		/*
1987c478bd9Sstevel@tonic-gate 		 * Get the info structure for the process and close quickly.
1997c478bd9Sstevel@tonic-gate 		 */
2007c478bd9Sstevel@tonic-gate 		if (read(procfd, &info, sizeof (info)) != sizeof (info)) {
2017c478bd9Sstevel@tonic-gate 			int	saverr = errno;
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 			(void) close(procfd);
2047c478bd9Sstevel@tonic-gate 			if (saverr == EAGAIN)
2057c478bd9Sstevel@tonic-gate 				goto retry;
2067c478bd9Sstevel@tonic-gate 			if (saverr != ENOENT)
2077c478bd9Sstevel@tonic-gate 				perror(pname);
2087c478bd9Sstevel@tonic-gate 			continue;
2097c478bd9Sstevel@tonic-gate 		}
2107c478bd9Sstevel@tonic-gate 		(void) close(procfd);
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 		/*
2137c478bd9Sstevel@tonic-gate 		 * We make sure there's always a free slot in the table
2147c478bd9Sstevel@tonic-gate 		 * in case we need to add a fake p0.
2157c478bd9Sstevel@tonic-gate 		 */
2167c478bd9Sstevel@tonic-gate 		if (nps + 1 >= psize) {
2177c478bd9Sstevel@tonic-gate 			if ((psize *= 2) == 0)
2187c478bd9Sstevel@tonic-gate 				psize = 20;
2197c478bd9Sstevel@tonic-gate 			if ((ps = realloc(ps, psize*sizeof (ps_t *))) == NULL) {
2207c478bd9Sstevel@tonic-gate 				perror("realloc()");
2217c478bd9Sstevel@tonic-gate 				return (1);
2227c478bd9Sstevel@tonic-gate 			}
2237c478bd9Sstevel@tonic-gate 		}
2247c478bd9Sstevel@tonic-gate 		if ((p = malloc(sizeof (ps_t))) == NULL) {
2257c478bd9Sstevel@tonic-gate 			perror("malloc()");
2267c478bd9Sstevel@tonic-gate 			return (1);
2277c478bd9Sstevel@tonic-gate 		}
2287c478bd9Sstevel@tonic-gate 		ps[nps++] = p;
2297c478bd9Sstevel@tonic-gate 		p->done = 0;
2307c478bd9Sstevel@tonic-gate 		p->uid = info.pr_uid;
2317c478bd9Sstevel@tonic-gate 		p->gid = info.pr_gid;
2327c478bd9Sstevel@tonic-gate 		p->pid = info.pr_pid;
2337c478bd9Sstevel@tonic-gate 		p->ppid = info.pr_ppid;
2347c478bd9Sstevel@tonic-gate 		p->pgrp = info.pr_pgid;
2357c478bd9Sstevel@tonic-gate 		p->sid = info.pr_sid;
2367c478bd9Sstevel@tonic-gate 		p->zoneid = info.pr_zoneid;
2377c478bd9Sstevel@tonic-gate 		p->ctid = info.pr_contract;
2387c478bd9Sstevel@tonic-gate 		p->start = info.pr_start;
2397c478bd9Sstevel@tonic-gate 		proc_unctrl_psinfo(&info);
2407c478bd9Sstevel@tonic-gate 		if (info.pr_nlwp == 0)
2417c478bd9Sstevel@tonic-gate 			(void) strcpy(p->psargs, "<defunct>");
2427c478bd9Sstevel@tonic-gate 		else if (info.pr_psargs[0] == '\0')
2437c478bd9Sstevel@tonic-gate 			(void) strncpy(p->psargs, info.pr_fname,
2447c478bd9Sstevel@tonic-gate 			    sizeof (p->psargs));
2457c478bd9Sstevel@tonic-gate 		else
2467c478bd9Sstevel@tonic-gate 			(void) strncpy(p->psargs, info.pr_psargs,
2477c478bd9Sstevel@tonic-gate 			    sizeof (p->psargs));
2487c478bd9Sstevel@tonic-gate 		p->psargs[sizeof (p->psargs)-1] = '\0';
2497c478bd9Sstevel@tonic-gate 		p->pp = NULL;
2507c478bd9Sstevel@tonic-gate 		p->sp = NULL;
2517c478bd9Sstevel@tonic-gate 		p->cp = NULL;
2527c478bd9Sstevel@tonic-gate 		if (p->pid == p->ppid)
2537c478bd9Sstevel@tonic-gate 			proc0 = p;
2547c478bd9Sstevel@tonic-gate 		if (p->pid == 1)
2557c478bd9Sstevel@tonic-gate 			proc1 = p;
2567c478bd9Sstevel@tonic-gate 	}
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 	(void) closedir(dirp);
2597c478bd9Sstevel@tonic-gate 	if (proc0 == NULL)
2607c478bd9Sstevel@tonic-gate 		proc0 = fakepid0();
2617c478bd9Sstevel@tonic-gate 	if (proc1 == NULL)
2627c478bd9Sstevel@tonic-gate 		proc1 = proc0;
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 	for (n = 0; n < nps; n++) {
2657c478bd9Sstevel@tonic-gate 		p = ps[n];
2667c478bd9Sstevel@tonic-gate 		if (p->pp == NULL)
2677c478bd9Sstevel@tonic-gate 			prsort(p);
2687c478bd9Sstevel@tonic-gate 	}
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 	if (cflag)
2717c478bd9Sstevel@tonic-gate 		/* Parent all orphan contracts to process 0. */
2727c478bd9Sstevel@tonic-gate 		for (n = 0; n < nctps; n++) {
2737c478bd9Sstevel@tonic-gate 			p = ctps[n];
2747c478bd9Sstevel@tonic-gate 			if (p->pp == NULL)
2757c478bd9Sstevel@tonic-gate 				insertchild(proc0, p);
2767c478bd9Sstevel@tonic-gate 		}
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 	if (argc == 0) {
2797c478bd9Sstevel@tonic-gate 		for (p = aflag ? proc0->cp : proc1->cp; p != NULL; p = p->sp) {
2807c478bd9Sstevel@tonic-gate 			markprocs(p);
2817c478bd9Sstevel@tonic-gate 			printsubtree(p, 0);
2827c478bd9Sstevel@tonic-gate 		}
2837c478bd9Sstevel@tonic-gate 		return (0);
2847c478bd9Sstevel@tonic-gate 	}
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 	/*
2877c478bd9Sstevel@tonic-gate 	 * Initially, assume we're not going to find any processes.  If we do
2887c478bd9Sstevel@tonic-gate 	 * mark any, then set this to 0 to indicate no error.
2897c478bd9Sstevel@tonic-gate 	 */
2907c478bd9Sstevel@tonic-gate 	errflg = 1;
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 	while (argc-- > 0) {
2937c478bd9Sstevel@tonic-gate 		char *arg;
2947c478bd9Sstevel@tonic-gate 		char *next;
2957c478bd9Sstevel@tonic-gate 		pid_t pid;
2967c478bd9Sstevel@tonic-gate 		uid_t uid;
2977c478bd9Sstevel@tonic-gate 		int n;
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 		/* in case some silly person said 'ptree /proc/[0-9]*' */
3007c478bd9Sstevel@tonic-gate 		arg = strrchr(*argv, '/');
3017c478bd9Sstevel@tonic-gate 		if (arg++ == NULL)
3027c478bd9Sstevel@tonic-gate 			arg = *argv;
3037c478bd9Sstevel@tonic-gate 		argv++;
304f48205beScasper 		uid = (uid_t)-1;
3057c478bd9Sstevel@tonic-gate 		errno = 0;
3067c478bd9Sstevel@tonic-gate 		pid = strtoul(arg, &next, 10);
3077c478bd9Sstevel@tonic-gate 		if (errno != 0 || *next != '\0') {
3087c478bd9Sstevel@tonic-gate 			struct passwd *pw = getpwnam(arg);
3097c478bd9Sstevel@tonic-gate 			if (pw == NULL) {
3107c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
3117c478bd9Sstevel@tonic-gate 				    "%s: invalid username: %s\n",
3127c478bd9Sstevel@tonic-gate 				    command, arg);
3137c478bd9Sstevel@tonic-gate 				retc = 1;
3147c478bd9Sstevel@tonic-gate 				continue;
3157c478bd9Sstevel@tonic-gate 			}
3167c478bd9Sstevel@tonic-gate 			uid = pw->pw_uid;
3177c478bd9Sstevel@tonic-gate 			pid = -1;
3187c478bd9Sstevel@tonic-gate 		}
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 		for (n = 0; n < nps; n++) {
3217c478bd9Sstevel@tonic-gate 			ps_t *p = ps[n];
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 			/*
3247c478bd9Sstevel@tonic-gate 			 * A match on pid causes the subtree starting at pid
3257c478bd9Sstevel@tonic-gate 			 * to be printed, regardless of the -a flag.
3267c478bd9Sstevel@tonic-gate 			 * For uid matches, we never include pid 0 and only
3277c478bd9Sstevel@tonic-gate 			 * include the children of pid 0 if -a was specified.
3287c478bd9Sstevel@tonic-gate 			 */
3297c478bd9Sstevel@tonic-gate 			if (p->pid == pid || (p->uid == uid && p->pid != 0 &&
3307c478bd9Sstevel@tonic-gate 			    (p->ppid != 0 || aflag))) {
3317c478bd9Sstevel@tonic-gate 				errflg = 0;
3327c478bd9Sstevel@tonic-gate 				markprocs(p);
3337c478bd9Sstevel@tonic-gate 				if (p->pid != 0)
3347c478bd9Sstevel@tonic-gate 					for (p = p->pp; p != NULL &&
3357c478bd9Sstevel@tonic-gate 					    p->done != 1 && p->pid != 0;
3367c478bd9Sstevel@tonic-gate 					    p = p->pp)
3377c478bd9Sstevel@tonic-gate 						if ((p->ppid != 0 || aflag) &&
3387c478bd9Sstevel@tonic-gate 						    (!zflag ||
3397c478bd9Sstevel@tonic-gate 						    p->zoneid == zoneid))
3407c478bd9Sstevel@tonic-gate 							p->done = 1;
341f48205beScasper 				if (uid == (uid_t)-1)
3427c478bd9Sstevel@tonic-gate 					break;
3437c478bd9Sstevel@tonic-gate 			}
3447c478bd9Sstevel@tonic-gate 		}
3457c478bd9Sstevel@tonic-gate 	}
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 	printsubtree(proc0, 0);
3487c478bd9Sstevel@tonic-gate 	/*
3497c478bd9Sstevel@tonic-gate 	 * retc = 1 if an invalid username was supplied.
3507c478bd9Sstevel@tonic-gate 	 * errflg = 1 if no matching processes were found.
3517c478bd9Sstevel@tonic-gate 	 */
3527c478bd9Sstevel@tonic-gate 	return (retc || errflg);
3537c478bd9Sstevel@tonic-gate }
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate #define	PIDWIDTH	5
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate static int
printone(ps_t * p,int level)3587c478bd9Sstevel@tonic-gate printone(ps_t *p, int level)
3597c478bd9Sstevel@tonic-gate {
3607c478bd9Sstevel@tonic-gate 	int n, indent;
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 	if (p->done && !FAKEDPID0(p)) {
3637c478bd9Sstevel@tonic-gate 		indent = level * 2;
3647c478bd9Sstevel@tonic-gate 		if ((n = columns - PIDWIDTH - indent - 2) < 0)
3657c478bd9Sstevel@tonic-gate 			n = 0;
3667c478bd9Sstevel@tonic-gate 		if (p->pid >= 0) {
3677c478bd9Sstevel@tonic-gate 			(void) printf("%*.*s%-*d %.*s\n", indent, indent, " ",
3687c478bd9Sstevel@tonic-gate 			    PIDWIDTH, (int)p->pid, n, p->psargs);
3697c478bd9Sstevel@tonic-gate 		} else {
3707c478bd9Sstevel@tonic-gate 			assert(cflag != 0);
3717c478bd9Sstevel@tonic-gate 			(void) printf("%*.*s[process contract %d]\n",
3727c478bd9Sstevel@tonic-gate 			    indent, indent, " ", (int)p->ctid);
3737c478bd9Sstevel@tonic-gate 		}
3747c478bd9Sstevel@tonic-gate 		return (1);
3757c478bd9Sstevel@tonic-gate 	}
3767c478bd9Sstevel@tonic-gate 	return (0);
3777c478bd9Sstevel@tonic-gate }
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate static void
insertchild(ps_t * pp,ps_t * cp)3807c478bd9Sstevel@tonic-gate insertchild(ps_t *pp, ps_t *cp)
3817c478bd9Sstevel@tonic-gate {
3827c478bd9Sstevel@tonic-gate 	/* insert as child process of p */
3837c478bd9Sstevel@tonic-gate 	ps_t **here;
3847c478bd9Sstevel@tonic-gate 	ps_t *sp;
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate 	/* sort by start time */
3877c478bd9Sstevel@tonic-gate 	for (here = &pp->cp, sp = pp->cp;
3887c478bd9Sstevel@tonic-gate 	    sp != NULL;
3897c478bd9Sstevel@tonic-gate 	    here = &sp->sp, sp = sp->sp) {
3907c478bd9Sstevel@tonic-gate 		if (cp->start.tv_sec < sp->start.tv_sec)
3917c478bd9Sstevel@tonic-gate 			break;
3927c478bd9Sstevel@tonic-gate 		if (cp->start.tv_sec == sp->start.tv_sec &&
3937c478bd9Sstevel@tonic-gate 		    cp->start.tv_nsec < sp->start.tv_nsec)
3947c478bd9Sstevel@tonic-gate 			break;
3957c478bd9Sstevel@tonic-gate 	}
3967c478bd9Sstevel@tonic-gate 	cp->pp = pp;
3977c478bd9Sstevel@tonic-gate 	cp->sp = sp;
3987c478bd9Sstevel@tonic-gate 	*here = cp;
3997c478bd9Sstevel@tonic-gate }
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate static void
ctsort(ctid_t ctid,ps_t * p)4027c478bd9Sstevel@tonic-gate ctsort(ctid_t ctid, ps_t *p)
4037c478bd9Sstevel@tonic-gate {
4047c478bd9Sstevel@tonic-gate 	ps_t *pp;
4057c478bd9Sstevel@tonic-gate 	int fd, n;
4067c478bd9Sstevel@tonic-gate 	ct_stathdl_t hdl;
4077c478bd9Sstevel@tonic-gate 	struct stat64 st;
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate 	for (n = 0; n < nctps; n++)
4107c478bd9Sstevel@tonic-gate 		if (ctps[n]->ctid == ctid) {
4117c478bd9Sstevel@tonic-gate 			insertchild(ctps[n], p);
4127c478bd9Sstevel@tonic-gate 			return;
4137c478bd9Sstevel@tonic-gate 		}
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 	if ((fd = contract_open(ctid, "process", "status", O_RDONLY)) == -1)
4167c478bd9Sstevel@tonic-gate 		return;
4177c478bd9Sstevel@tonic-gate 	if (fstat64(fd, &st) == -1 || ct_status_read(fd, CTD_COMMON, &hdl)) {
4187c478bd9Sstevel@tonic-gate 		(void) close(fd);
4197c478bd9Sstevel@tonic-gate 		return;
4207c478bd9Sstevel@tonic-gate 	}
4217c478bd9Sstevel@tonic-gate 	(void) close(fd);
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate 	if (nctps >= ctsize) {
4247c478bd9Sstevel@tonic-gate 		if ((ctsize *= 2) == 0)
4257c478bd9Sstevel@tonic-gate 			ctsize = 20;
4267c478bd9Sstevel@tonic-gate 		if ((ctps = realloc(ctps, ctsize * sizeof (ps_t *))) == NULL) {
4277c478bd9Sstevel@tonic-gate 			perror("realloc()");
4287c478bd9Sstevel@tonic-gate 			exit(1);
4297c478bd9Sstevel@tonic-gate 		}
4307c478bd9Sstevel@tonic-gate 	}
4317c478bd9Sstevel@tonic-gate 	pp = calloc(sizeof (ps_t), 1);
4327c478bd9Sstevel@tonic-gate 	if (pp == NULL) {
4337c478bd9Sstevel@tonic-gate 		perror("calloc()");
4347c478bd9Sstevel@tonic-gate 		exit(1);
4357c478bd9Sstevel@tonic-gate 	}
4367c478bd9Sstevel@tonic-gate 	ctps[nctps++] = pp;
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate 	pp->pid = -1;
4397c478bd9Sstevel@tonic-gate 	pp->ctid = ctid;
4407c478bd9Sstevel@tonic-gate 	pp->start.tv_sec = st.st_ctime;
4417c478bd9Sstevel@tonic-gate 	insertchild(pp, p);
4427c478bd9Sstevel@tonic-gate 
443*6b31a7daSacruz 	pp->zoneid = ct_status_get_zoneid(hdl);
444*6b31a7daSacruz 	/*
445*6b31a7daSacruz 	 * In a zlogin <zonename>, the contract belongs to the
446*6b31a7daSacruz 	 * global zone and the shell opened belongs to <zonename>.
447*6b31a7daSacruz 	 * If the -c and -z zonename flags are used together, then
448*6b31a7daSacruz 	 * we need to adjust the zoneid in the contract's ps_t as
449*6b31a7daSacruz 	 * follows:
450*6b31a7daSacruz 	 *
451*6b31a7daSacruz 	 * ptree -c -z <zonename> --> zoneid == p->zoneid
452*6b31a7daSacruz 	 * ptree -c -z global	  --> zoneid == pp->zoneid
453*6b31a7daSacruz 	 *
454*6b31a7daSacruz 	 * The approach assumes that no tool can create processes in
455*6b31a7daSacruz 	 * different zones under the same contract. If this is
456*6b31a7daSacruz 	 * possible, ptree will need to refactor how it builds
457*6b31a7daSacruz 	 * its internal tree of ps_t's
458*6b31a7daSacruz 	 */
459*6b31a7daSacruz 	if (zflag && p->zoneid != pp->zoneid &&
460*6b31a7daSacruz 	    (zoneid == p->zoneid || zoneid == pp->zoneid))
461*6b31a7daSacruz 		pp->zoneid = p->zoneid;
4627c478bd9Sstevel@tonic-gate 	if (ct_status_get_state(hdl) == CTS_OWNED) {
4637c478bd9Sstevel@tonic-gate 		pp->ppid = ct_status_get_holder(hdl);
4647c478bd9Sstevel@tonic-gate 		prsort(pp);
4657c478bd9Sstevel@tonic-gate 	} else if (ct_status_get_state(hdl) == CTS_INHERITED) {
4667c478bd9Sstevel@tonic-gate 		ctsort(ct_status_get_holder(hdl), pp);
4677c478bd9Sstevel@tonic-gate 	}
4687c478bd9Sstevel@tonic-gate 	ct_status_free(hdl);
4697c478bd9Sstevel@tonic-gate }
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate static void
prsort(ps_t * p)4727c478bd9Sstevel@tonic-gate prsort(ps_t *p)
4737c478bd9Sstevel@tonic-gate {
4747c478bd9Sstevel@tonic-gate 	int n;
4757c478bd9Sstevel@tonic-gate 	ps_t *pp;
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate 	/* If this node already has a parent, it's sorted */
4787c478bd9Sstevel@tonic-gate 	if (p->pp != NULL)
4797c478bd9Sstevel@tonic-gate 		return;
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 	for (n = 0; n < nps; n++) {
4827c478bd9Sstevel@tonic-gate 		pp = ps[n];
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 		if (pp != NULL && p != pp && p->ppid == pp->pid) {
485*6b31a7daSacruz 			if (cflag && p->pid >= 0 &&
486*6b31a7daSacruz 			    p->ctid != -1 && p->ctid != pp->ctid) {
4877c478bd9Sstevel@tonic-gate 				ctsort(p->ctid, p);
4887c478bd9Sstevel@tonic-gate 			} else {
4897c478bd9Sstevel@tonic-gate 				insertchild(pp, p);
4907c478bd9Sstevel@tonic-gate 				prsort(pp);
4917c478bd9Sstevel@tonic-gate 			}
4927c478bd9Sstevel@tonic-gate 			return;
4937c478bd9Sstevel@tonic-gate 		}
4947c478bd9Sstevel@tonic-gate 	}
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate 	/* File parentless processes under their contracts */
4977c478bd9Sstevel@tonic-gate 	if (cflag && p->pid >= 0)
4987c478bd9Sstevel@tonic-gate 		ctsort(p->ctid, p);
4997c478bd9Sstevel@tonic-gate }
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate static void
printsubtree(ps_t * p,int level)5027c478bd9Sstevel@tonic-gate printsubtree(ps_t *p, int level)
5037c478bd9Sstevel@tonic-gate {
5047c478bd9Sstevel@tonic-gate 	int printed;
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate 	printed = printone(p, level);
5077c478bd9Sstevel@tonic-gate 	if (level != 0 || printed == 1)
5087c478bd9Sstevel@tonic-gate 		level++;
5097c478bd9Sstevel@tonic-gate 	for (p = p->cp; p != NULL; p = p->sp)
5107c478bd9Sstevel@tonic-gate 		printsubtree(p, level);
5117c478bd9Sstevel@tonic-gate }
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate static void
markprocs(ps_t * p)5147c478bd9Sstevel@tonic-gate markprocs(ps_t *p)
5157c478bd9Sstevel@tonic-gate {
5167c478bd9Sstevel@tonic-gate 	if (!zflag || p->zoneid == zoneid)
5177c478bd9Sstevel@tonic-gate 		p->done = 1;
5187c478bd9Sstevel@tonic-gate 	for (p = p->cp; p != NULL; p = p->sp)
5197c478bd9Sstevel@tonic-gate 		markprocs(p);
5207c478bd9Sstevel@tonic-gate }
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate /*
5237c478bd9Sstevel@tonic-gate  * If there's no "top" process, we fake one; it will be the parent of
5247c478bd9Sstevel@tonic-gate  * all orphans.
5257c478bd9Sstevel@tonic-gate  */
5267c478bd9Sstevel@tonic-gate static ps_t *
fakepid0(void)5277c478bd9Sstevel@tonic-gate fakepid0(void)
5287c478bd9Sstevel@tonic-gate {
5297c478bd9Sstevel@tonic-gate 	ps_t *p0, *p;
5307c478bd9Sstevel@tonic-gate 	int n;
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 	if ((p0 = malloc(sizeof (ps_t))) == NULL) {
5337c478bd9Sstevel@tonic-gate 		perror("malloc()");
5347c478bd9Sstevel@tonic-gate 		exit(1);
5357c478bd9Sstevel@tonic-gate 	}
5367c478bd9Sstevel@tonic-gate 	(void) memset(p0, '\0', sizeof (ps_t));
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 	/* First build all partial process trees. */
5397c478bd9Sstevel@tonic-gate 	for (n = 0; n < nps; n++) {
5407c478bd9Sstevel@tonic-gate 		p = ps[n];
5417c478bd9Sstevel@tonic-gate 		if (p->pp == NULL)
5427c478bd9Sstevel@tonic-gate 			prsort(p);
5437c478bd9Sstevel@tonic-gate 	}
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate 	/* Then adopt all orphans. */
5467c478bd9Sstevel@tonic-gate 	for (n = 0; n < nps; n++) {
5477c478bd9Sstevel@tonic-gate 		p = ps[n];
5487c478bd9Sstevel@tonic-gate 		if (p->pp == NULL)
5497c478bd9Sstevel@tonic-gate 			insertchild(p0, p);
5507c478bd9Sstevel@tonic-gate 	}
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate 	/* We've made sure earlier there's room for this. */
5537c478bd9Sstevel@tonic-gate 	ps[nps++] = p0;
5547c478bd9Sstevel@tonic-gate 	return (p0);
5557c478bd9Sstevel@tonic-gate }
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate /* convert string containing zone name or id to a numeric id */
5587c478bd9Sstevel@tonic-gate static zoneid_t
getzone(char * arg)5597c478bd9Sstevel@tonic-gate getzone(char *arg)
5607c478bd9Sstevel@tonic-gate {
5617c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate 	if (zone_get_id(arg, &zoneid) != 0) {
5647c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: unknown zone: %s\n", command, arg);
5657c478bd9Sstevel@tonic-gate 		exit(1);
5667c478bd9Sstevel@tonic-gate 	}
5677c478bd9Sstevel@tonic-gate 	return (zoneid);
5687c478bd9Sstevel@tonic-gate }
569