xref: /onnv-gate/usr/src/cmd/ptools/ptree/ptree.c (revision 4473:5ee53e509691)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
54321Scasper  * Common Development and Distribution License (the "License").
64321Scasper  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
224321Scasper  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate  * ptree -- print family tree of processes
280Sstevel@tonic-gate  */
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #include <assert.h>
330Sstevel@tonic-gate #include <stdio.h>
340Sstevel@tonic-gate #include <string.h>
350Sstevel@tonic-gate #include <errno.h>
360Sstevel@tonic-gate #include <fcntl.h>
370Sstevel@tonic-gate #include <sys/types.h>
380Sstevel@tonic-gate #include <sys/termios.h>
390Sstevel@tonic-gate #include <unistd.h>
400Sstevel@tonic-gate #include <stdlib.h>
410Sstevel@tonic-gate #include <dirent.h>
420Sstevel@tonic-gate #include <pwd.h>
430Sstevel@tonic-gate #include <libproc.h>
440Sstevel@tonic-gate #include <libzonecfg.h>
450Sstevel@tonic-gate #include <limits.h>
460Sstevel@tonic-gate #include <libcontract.h>
470Sstevel@tonic-gate #include <sys/contract.h>
480Sstevel@tonic-gate #include <sys/ctfs.h>
490Sstevel@tonic-gate #include <libcontract_priv.h>
500Sstevel@tonic-gate #include <sys/stat.h>
510Sstevel@tonic-gate 
520Sstevel@tonic-gate #define	FAKEDPID0(p)	(p->pid == 0 && p->psargs[0] == '\0')
530Sstevel@tonic-gate 
540Sstevel@tonic-gate typedef struct ps {
550Sstevel@tonic-gate 	int	done;
560Sstevel@tonic-gate 	uid_t	uid;
570Sstevel@tonic-gate 	uid_t	gid;
580Sstevel@tonic-gate 	pid_t	pid;		/* pid == -1 indicates this is a contract */
590Sstevel@tonic-gate 	pid_t	ppid;
600Sstevel@tonic-gate 	pid_t	pgrp;
610Sstevel@tonic-gate 	pid_t	sid;
620Sstevel@tonic-gate 	zoneid_t zoneid;
630Sstevel@tonic-gate 	ctid_t	ctid;
640Sstevel@tonic-gate 	timestruc_t start;
650Sstevel@tonic-gate 	char	psargs[PRARGSZ];
660Sstevel@tonic-gate 	struct ps *pp;		/* parent */
670Sstevel@tonic-gate 	struct ps *sp;		/* sibling */
680Sstevel@tonic-gate 	struct ps *cp;		/* child */
690Sstevel@tonic-gate } ps_t;
700Sstevel@tonic-gate 
710Sstevel@tonic-gate static	ps_t	**ps;		/* array of ps_t's */
720Sstevel@tonic-gate static	unsigned psize;		/* size of array */
730Sstevel@tonic-gate static	int	nps;		/* number of ps_t's */
740Sstevel@tonic-gate static	ps_t	**ctps;		/* array of contract ps_t's */
750Sstevel@tonic-gate static	unsigned ctsize;	/* size of contract array */
760Sstevel@tonic-gate static	int	nctps;		/* number of contract ps_t's */
770Sstevel@tonic-gate static	ps_t	*proc0;		/* process 0 */
780Sstevel@tonic-gate static	ps_t	*proc1;		/* process 1 */
790Sstevel@tonic-gate 
800Sstevel@tonic-gate static	char	*command;
810Sstevel@tonic-gate 
820Sstevel@tonic-gate static	int	aflag = 0;
830Sstevel@tonic-gate static	int	cflag = 0;
840Sstevel@tonic-gate static	int	zflag = 0;
850Sstevel@tonic-gate static	zoneid_t zoneid;
860Sstevel@tonic-gate static	int	columns = 80;
870Sstevel@tonic-gate 
880Sstevel@tonic-gate static void markprocs(ps_t *p);
890Sstevel@tonic-gate static int printone(ps_t *p, int level);
900Sstevel@tonic-gate static void insertchild(ps_t *, ps_t *);
910Sstevel@tonic-gate static void prsort(ps_t *p);
920Sstevel@tonic-gate static void printsubtree(ps_t *p, int level);
930Sstevel@tonic-gate static zoneid_t getzone(char *arg);
940Sstevel@tonic-gate static ps_t *fakepid0(void);
950Sstevel@tonic-gate 
960Sstevel@tonic-gate int
main(int argc,char ** argv)970Sstevel@tonic-gate main(int argc, char **argv)
980Sstevel@tonic-gate {
990Sstevel@tonic-gate 	psinfo_t info;	/* process information structure from /proc */
1000Sstevel@tonic-gate 	int opt;
1010Sstevel@tonic-gate 	int errflg = 0;
1020Sstevel@tonic-gate 	struct winsize winsize;
1030Sstevel@tonic-gate 	char *s;
1040Sstevel@tonic-gate 	int n;
1050Sstevel@tonic-gate 	int retc = 0;
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate 	DIR *dirp;
1080Sstevel@tonic-gate 	struct dirent *dentp;
1090Sstevel@tonic-gate 	char	pname[100];
1100Sstevel@tonic-gate 	int	pdlen;
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate 	ps_t *p;
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate 	if ((command = strrchr(argv[0], '/')) == NULL)
1150Sstevel@tonic-gate 		command = argv[0];
1160Sstevel@tonic-gate 	else
1170Sstevel@tonic-gate 		command++;
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate 	/* options */
1200Sstevel@tonic-gate 	while ((opt = getopt(argc, argv, "acz:")) != EOF) {
1210Sstevel@tonic-gate 		switch (opt) {
1220Sstevel@tonic-gate 		case 'a':		/* include children of process 0 */
1230Sstevel@tonic-gate 			aflag = 1;
1240Sstevel@tonic-gate 			break;
1250Sstevel@tonic-gate 		case 'c':		/* display contract ownership */
1260Sstevel@tonic-gate 			aflag = cflag = 1;
1270Sstevel@tonic-gate 			break;
1280Sstevel@tonic-gate 		case 'z':		/* only processes in given zone */
1290Sstevel@tonic-gate 			zflag = 1;
1300Sstevel@tonic-gate 			zoneid = getzone(optarg);
1310Sstevel@tonic-gate 			break;
1320Sstevel@tonic-gate 		default:
1330Sstevel@tonic-gate 			errflg = 1;
1340Sstevel@tonic-gate 			break;
1350Sstevel@tonic-gate 		}
1360Sstevel@tonic-gate 	}
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate 	argc -= optind;
1390Sstevel@tonic-gate 	argv += optind;
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate 	if (errflg) {
1420Sstevel@tonic-gate 		(void) fprintf(stderr,
1430Sstevel@tonic-gate 		    "usage:\t%s [-ac] [-z zone] [ {pid|user} ... ]\n",
1440Sstevel@tonic-gate 		    command);
1450Sstevel@tonic-gate 		(void) fprintf(stderr,
1460Sstevel@tonic-gate 		    "  (show process trees)\n");
1470Sstevel@tonic-gate 		(void) fprintf(stderr,
1480Sstevel@tonic-gate 		    "  list can include process-ids and user names\n");
1490Sstevel@tonic-gate 		(void) fprintf(stderr,
1500Sstevel@tonic-gate 		    "  -a : include children of process 0\n");
1510Sstevel@tonic-gate 		(void) fprintf(stderr,
1520Sstevel@tonic-gate 		    "  -c : show contract ownership\n");
1530Sstevel@tonic-gate 		(void) fprintf(stderr,
1540Sstevel@tonic-gate 		    "  -z : print only processes in given zone\n");
1550Sstevel@tonic-gate 		return (2);
1560Sstevel@tonic-gate 	}
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 	/*
1590Sstevel@tonic-gate 	 * Kind of a hack to determine the width of the output...
1600Sstevel@tonic-gate 	 */
1610Sstevel@tonic-gate 	if ((s = getenv("COLUMNS")) != NULL && (n = atoi(s)) > 0)
1620Sstevel@tonic-gate 		columns = n;
1630Sstevel@tonic-gate 	else if (isatty(fileno(stdout)) &&
1640Sstevel@tonic-gate 	    ioctl(fileno(stdout), TIOCGWINSZ, &winsize) == 0 &&
1650Sstevel@tonic-gate 	    winsize.ws_col != 0)
1660Sstevel@tonic-gate 		columns = winsize.ws_col;
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 	nps = 0;
1690Sstevel@tonic-gate 	psize = 0;
1700Sstevel@tonic-gate 	ps = NULL;
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate 	/*
1730Sstevel@tonic-gate 	 * Search the /proc directory for all processes.
1740Sstevel@tonic-gate 	 */
1750Sstevel@tonic-gate 	if ((dirp = opendir("/proc")) == NULL) {
1760Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: cannot open /proc directory\n",
1770Sstevel@tonic-gate 		    command);
1780Sstevel@tonic-gate 		return (1);
1790Sstevel@tonic-gate 	}
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 	(void) strcpy(pname, "/proc");
1820Sstevel@tonic-gate 	pdlen = strlen(pname);
1830Sstevel@tonic-gate 	pname[pdlen++] = '/';
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 	/* for each active process --- */
1860Sstevel@tonic-gate 	while (dentp = readdir(dirp)) {
1870Sstevel@tonic-gate 		int	procfd;	/* filedescriptor for /proc/nnnnn/psinfo */
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 		if (dentp->d_name[0] == '.')		/* skip . and .. */
1900Sstevel@tonic-gate 			continue;
1910Sstevel@tonic-gate 		(void) strcpy(pname + pdlen, dentp->d_name);
1920Sstevel@tonic-gate 		(void) strcpy(pname + strlen(pname), "/psinfo");
1930Sstevel@tonic-gate retry:
1940Sstevel@tonic-gate 		if ((procfd = open(pname, O_RDONLY)) == -1)
1950Sstevel@tonic-gate 			continue;
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate 		/*
1980Sstevel@tonic-gate 		 * Get the info structure for the process and close quickly.
1990Sstevel@tonic-gate 		 */
2000Sstevel@tonic-gate 		if (read(procfd, &info, sizeof (info)) != sizeof (info)) {
2010Sstevel@tonic-gate 			int	saverr = errno;
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate 			(void) close(procfd);
2040Sstevel@tonic-gate 			if (saverr == EAGAIN)
2050Sstevel@tonic-gate 				goto retry;
2060Sstevel@tonic-gate 			if (saverr != ENOENT)
2070Sstevel@tonic-gate 				perror(pname);
2080Sstevel@tonic-gate 			continue;
2090Sstevel@tonic-gate 		}
2100Sstevel@tonic-gate 		(void) close(procfd);
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate 		/*
2130Sstevel@tonic-gate 		 * We make sure there's always a free slot in the table
2140Sstevel@tonic-gate 		 * in case we need to add a fake p0.
2150Sstevel@tonic-gate 		 */
2160Sstevel@tonic-gate 		if (nps + 1 >= psize) {
2170Sstevel@tonic-gate 			if ((psize *= 2) == 0)
2180Sstevel@tonic-gate 				psize = 20;
2190Sstevel@tonic-gate 			if ((ps = realloc(ps, psize*sizeof (ps_t *))) == NULL) {
2200Sstevel@tonic-gate 				perror("realloc()");
2210Sstevel@tonic-gate 				return (1);
2220Sstevel@tonic-gate 			}
2230Sstevel@tonic-gate 		}
2240Sstevel@tonic-gate 		if ((p = malloc(sizeof (ps_t))) == NULL) {
2250Sstevel@tonic-gate 			perror("malloc()");
2260Sstevel@tonic-gate 			return (1);
2270Sstevel@tonic-gate 		}
2280Sstevel@tonic-gate 		ps[nps++] = p;
2290Sstevel@tonic-gate 		p->done = 0;
2300Sstevel@tonic-gate 		p->uid = info.pr_uid;
2310Sstevel@tonic-gate 		p->gid = info.pr_gid;
2320Sstevel@tonic-gate 		p->pid = info.pr_pid;
2330Sstevel@tonic-gate 		p->ppid = info.pr_ppid;
2340Sstevel@tonic-gate 		p->pgrp = info.pr_pgid;
2350Sstevel@tonic-gate 		p->sid = info.pr_sid;
2360Sstevel@tonic-gate 		p->zoneid = info.pr_zoneid;
2370Sstevel@tonic-gate 		p->ctid = info.pr_contract;
2380Sstevel@tonic-gate 		p->start = info.pr_start;
2390Sstevel@tonic-gate 		proc_unctrl_psinfo(&info);
2400Sstevel@tonic-gate 		if (info.pr_nlwp == 0)
2410Sstevel@tonic-gate 			(void) strcpy(p->psargs, "<defunct>");
2420Sstevel@tonic-gate 		else if (info.pr_psargs[0] == '\0')
2430Sstevel@tonic-gate 			(void) strncpy(p->psargs, info.pr_fname,
2440Sstevel@tonic-gate 			    sizeof (p->psargs));
2450Sstevel@tonic-gate 		else
2460Sstevel@tonic-gate 			(void) strncpy(p->psargs, info.pr_psargs,
2470Sstevel@tonic-gate 			    sizeof (p->psargs));
2480Sstevel@tonic-gate 		p->psargs[sizeof (p->psargs)-1] = '\0';
2490Sstevel@tonic-gate 		p->pp = NULL;
2500Sstevel@tonic-gate 		p->sp = NULL;
2510Sstevel@tonic-gate 		p->cp = NULL;
2520Sstevel@tonic-gate 		if (p->pid == p->ppid)
2530Sstevel@tonic-gate 			proc0 = p;
2540Sstevel@tonic-gate 		if (p->pid == 1)
2550Sstevel@tonic-gate 			proc1 = p;
2560Sstevel@tonic-gate 	}
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate 	(void) closedir(dirp);
2590Sstevel@tonic-gate 	if (proc0 == NULL)
2600Sstevel@tonic-gate 		proc0 = fakepid0();
2610Sstevel@tonic-gate 	if (proc1 == NULL)
2620Sstevel@tonic-gate 		proc1 = proc0;
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 	for (n = 0; n < nps; n++) {
2650Sstevel@tonic-gate 		p = ps[n];
2660Sstevel@tonic-gate 		if (p->pp == NULL)
2670Sstevel@tonic-gate 			prsort(p);
2680Sstevel@tonic-gate 	}
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate 	if (cflag)
2710Sstevel@tonic-gate 		/* Parent all orphan contracts to process 0. */
2720Sstevel@tonic-gate 		for (n = 0; n < nctps; n++) {
2730Sstevel@tonic-gate 			p = ctps[n];
2740Sstevel@tonic-gate 			if (p->pp == NULL)
2750Sstevel@tonic-gate 				insertchild(proc0, p);
2760Sstevel@tonic-gate 		}
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate 	if (argc == 0) {
2790Sstevel@tonic-gate 		for (p = aflag ? proc0->cp : proc1->cp; p != NULL; p = p->sp) {
2800Sstevel@tonic-gate 			markprocs(p);
2810Sstevel@tonic-gate 			printsubtree(p, 0);
2820Sstevel@tonic-gate 		}
2830Sstevel@tonic-gate 		return (0);
2840Sstevel@tonic-gate 	}
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate 	/*
2870Sstevel@tonic-gate 	 * Initially, assume we're not going to find any processes.  If we do
2880Sstevel@tonic-gate 	 * mark any, then set this to 0 to indicate no error.
2890Sstevel@tonic-gate 	 */
2900Sstevel@tonic-gate 	errflg = 1;
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 	while (argc-- > 0) {
2930Sstevel@tonic-gate 		char *arg;
2940Sstevel@tonic-gate 		char *next;
2950Sstevel@tonic-gate 		pid_t pid;
2960Sstevel@tonic-gate 		uid_t uid;
2970Sstevel@tonic-gate 		int n;
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate 		/* in case some silly person said 'ptree /proc/[0-9]*' */
3000Sstevel@tonic-gate 		arg = strrchr(*argv, '/');
3010Sstevel@tonic-gate 		if (arg++ == NULL)
3020Sstevel@tonic-gate 			arg = *argv;
3030Sstevel@tonic-gate 		argv++;
3044321Scasper 		uid = (uid_t)-1;
3050Sstevel@tonic-gate 		errno = 0;
3060Sstevel@tonic-gate 		pid = strtoul(arg, &next, 10);
3070Sstevel@tonic-gate 		if (errno != 0 || *next != '\0') {
3080Sstevel@tonic-gate 			struct passwd *pw = getpwnam(arg);
3090Sstevel@tonic-gate 			if (pw == NULL) {
3100Sstevel@tonic-gate 				(void) fprintf(stderr,
3114470Sacruz 				    "%s: invalid username: %s\n",
3124470Sacruz 				    command, arg);
3130Sstevel@tonic-gate 				retc = 1;
3140Sstevel@tonic-gate 				continue;
3150Sstevel@tonic-gate 			}
3160Sstevel@tonic-gate 			uid = pw->pw_uid;
3170Sstevel@tonic-gate 			pid = -1;
3180Sstevel@tonic-gate 		}
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate 		for (n = 0; n < nps; n++) {
3210Sstevel@tonic-gate 			ps_t *p = ps[n];
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate 			/*
3240Sstevel@tonic-gate 			 * A match on pid causes the subtree starting at pid
3250Sstevel@tonic-gate 			 * to be printed, regardless of the -a flag.
3260Sstevel@tonic-gate 			 * For uid matches, we never include pid 0 and only
3270Sstevel@tonic-gate 			 * include the children of pid 0 if -a was specified.
3280Sstevel@tonic-gate 			 */
3290Sstevel@tonic-gate 			if (p->pid == pid || (p->uid == uid && p->pid != 0 &&
3300Sstevel@tonic-gate 			    (p->ppid != 0 || aflag))) {
3310Sstevel@tonic-gate 				errflg = 0;
3320Sstevel@tonic-gate 				markprocs(p);
3330Sstevel@tonic-gate 				if (p->pid != 0)
3340Sstevel@tonic-gate 					for (p = p->pp; p != NULL &&
3350Sstevel@tonic-gate 					    p->done != 1 && p->pid != 0;
3360Sstevel@tonic-gate 					    p = p->pp)
3370Sstevel@tonic-gate 						if ((p->ppid != 0 || aflag) &&
3380Sstevel@tonic-gate 						    (!zflag ||
3390Sstevel@tonic-gate 						    p->zoneid == zoneid))
3400Sstevel@tonic-gate 							p->done = 1;
3414321Scasper 				if (uid == (uid_t)-1)
3420Sstevel@tonic-gate 					break;
3430Sstevel@tonic-gate 			}
3440Sstevel@tonic-gate 		}
3450Sstevel@tonic-gate 	}
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate 	printsubtree(proc0, 0);
3480Sstevel@tonic-gate 	/*
3490Sstevel@tonic-gate 	 * retc = 1 if an invalid username was supplied.
3500Sstevel@tonic-gate 	 * errflg = 1 if no matching processes were found.
3510Sstevel@tonic-gate 	 */
3520Sstevel@tonic-gate 	return (retc || errflg);
3530Sstevel@tonic-gate }
3540Sstevel@tonic-gate 
3550Sstevel@tonic-gate #define	PIDWIDTH	5
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate static int
printone(ps_t * p,int level)3580Sstevel@tonic-gate printone(ps_t *p, int level)
3590Sstevel@tonic-gate {
3600Sstevel@tonic-gate 	int n, indent;
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate 	if (p->done && !FAKEDPID0(p)) {
3630Sstevel@tonic-gate 		indent = level * 2;
3640Sstevel@tonic-gate 		if ((n = columns - PIDWIDTH - indent - 2) < 0)
3650Sstevel@tonic-gate 			n = 0;
3660Sstevel@tonic-gate 		if (p->pid >= 0) {
3670Sstevel@tonic-gate 			(void) printf("%*.*s%-*d %.*s\n", indent, indent, " ",
3680Sstevel@tonic-gate 			    PIDWIDTH, (int)p->pid, n, p->psargs);
3690Sstevel@tonic-gate 		} else {
3700Sstevel@tonic-gate 			assert(cflag != 0);
3710Sstevel@tonic-gate 			(void) printf("%*.*s[process contract %d]\n",
3720Sstevel@tonic-gate 			    indent, indent, " ", (int)p->ctid);
3730Sstevel@tonic-gate 		}
3740Sstevel@tonic-gate 		return (1);
3750Sstevel@tonic-gate 	}
3760Sstevel@tonic-gate 	return (0);
3770Sstevel@tonic-gate }
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate static void
insertchild(ps_t * pp,ps_t * cp)3800Sstevel@tonic-gate insertchild(ps_t *pp, ps_t *cp)
3810Sstevel@tonic-gate {
3820Sstevel@tonic-gate 	/* insert as child process of p */
3830Sstevel@tonic-gate 	ps_t **here;
3840Sstevel@tonic-gate 	ps_t *sp;
3850Sstevel@tonic-gate 
3860Sstevel@tonic-gate 	/* sort by start time */
3870Sstevel@tonic-gate 	for (here = &pp->cp, sp = pp->cp;
3880Sstevel@tonic-gate 	    sp != NULL;
3890Sstevel@tonic-gate 	    here = &sp->sp, sp = sp->sp) {
3900Sstevel@tonic-gate 		if (cp->start.tv_sec < sp->start.tv_sec)
3910Sstevel@tonic-gate 			break;
3920Sstevel@tonic-gate 		if (cp->start.tv_sec == sp->start.tv_sec &&
3930Sstevel@tonic-gate 		    cp->start.tv_nsec < sp->start.tv_nsec)
3940Sstevel@tonic-gate 			break;
3950Sstevel@tonic-gate 	}
3960Sstevel@tonic-gate 	cp->pp = pp;
3970Sstevel@tonic-gate 	cp->sp = sp;
3980Sstevel@tonic-gate 	*here = cp;
3990Sstevel@tonic-gate }
4000Sstevel@tonic-gate 
4010Sstevel@tonic-gate static void
ctsort(ctid_t ctid,ps_t * p)4020Sstevel@tonic-gate ctsort(ctid_t ctid, ps_t *p)
4030Sstevel@tonic-gate {
4040Sstevel@tonic-gate 	ps_t *pp;
4050Sstevel@tonic-gate 	int fd, n;
4060Sstevel@tonic-gate 	ct_stathdl_t hdl;
4070Sstevel@tonic-gate 	struct stat64 st;
4080Sstevel@tonic-gate 
4090Sstevel@tonic-gate 	for (n = 0; n < nctps; n++)
4100Sstevel@tonic-gate 		if (ctps[n]->ctid == ctid) {
4110Sstevel@tonic-gate 			insertchild(ctps[n], p);
4120Sstevel@tonic-gate 			return;
4130Sstevel@tonic-gate 		}
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate 	if ((fd = contract_open(ctid, "process", "status", O_RDONLY)) == -1)
4160Sstevel@tonic-gate 		return;
4170Sstevel@tonic-gate 	if (fstat64(fd, &st) == -1 || ct_status_read(fd, CTD_COMMON, &hdl)) {
4180Sstevel@tonic-gate 		(void) close(fd);
4190Sstevel@tonic-gate 		return;
4200Sstevel@tonic-gate 	}
4210Sstevel@tonic-gate 	(void) close(fd);
4220Sstevel@tonic-gate 
4230Sstevel@tonic-gate 	if (nctps >= ctsize) {
4240Sstevel@tonic-gate 		if ((ctsize *= 2) == 0)
4250Sstevel@tonic-gate 			ctsize = 20;
4260Sstevel@tonic-gate 		if ((ctps = realloc(ctps, ctsize * sizeof (ps_t *))) == NULL) {
4270Sstevel@tonic-gate 			perror("realloc()");
4280Sstevel@tonic-gate 			exit(1);
4290Sstevel@tonic-gate 		}
4300Sstevel@tonic-gate 	}
4310Sstevel@tonic-gate 	pp = calloc(sizeof (ps_t), 1);
4320Sstevel@tonic-gate 	if (pp == NULL) {
4330Sstevel@tonic-gate 		perror("calloc()");
4340Sstevel@tonic-gate 		exit(1);
4350Sstevel@tonic-gate 	}
4360Sstevel@tonic-gate 	ctps[nctps++] = pp;
4370Sstevel@tonic-gate 
4380Sstevel@tonic-gate 	pp->pid = -1;
4390Sstevel@tonic-gate 	pp->ctid = ctid;
4400Sstevel@tonic-gate 	pp->start.tv_sec = st.st_ctime;
4410Sstevel@tonic-gate 	insertchild(pp, p);
4420Sstevel@tonic-gate 
4434470Sacruz 	pp->zoneid = ct_status_get_zoneid(hdl);
4444470Sacruz 	/*
4454470Sacruz 	 * In a zlogin <zonename>, the contract belongs to the
4464470Sacruz 	 * global zone and the shell opened belongs to <zonename>.
4474470Sacruz 	 * If the -c and -z zonename flags are used together, then
448*4473Sacruz 	 * we need to adjust the zoneid in the contract's ps_t as
4494470Sacruz 	 * follows:
450*4473Sacruz 	 *
4514470Sacruz 	 * ptree -c -z <zonename> --> zoneid == p->zoneid
452*4473Sacruz 	 * ptree -c -z global	  --> zoneid == pp->zoneid
4534470Sacruz 	 *
4544470Sacruz 	 * The approach assumes that no tool can create processes in
4554470Sacruz 	 * different zones under the same contract. If this is
4564470Sacruz 	 * possible, ptree will need to refactor how it builds
4574470Sacruz 	 * its internal tree of ps_t's
4584470Sacruz 	 */
4594470Sacruz 	if (zflag && p->zoneid != pp->zoneid &&
4604470Sacruz 	    (zoneid == p->zoneid || zoneid == pp->zoneid))
4614470Sacruz 		pp->zoneid = p->zoneid;
4620Sstevel@tonic-gate 	if (ct_status_get_state(hdl) == CTS_OWNED) {
4630Sstevel@tonic-gate 		pp->ppid = ct_status_get_holder(hdl);
4640Sstevel@tonic-gate 		prsort(pp);
4650Sstevel@tonic-gate 	} else if (ct_status_get_state(hdl) == CTS_INHERITED) {
4660Sstevel@tonic-gate 		ctsort(ct_status_get_holder(hdl), pp);
4670Sstevel@tonic-gate 	}
4680Sstevel@tonic-gate 	ct_status_free(hdl);
4690Sstevel@tonic-gate }
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate static void
prsort(ps_t * p)4720Sstevel@tonic-gate prsort(ps_t *p)
4730Sstevel@tonic-gate {
4740Sstevel@tonic-gate 	int n;
4750Sstevel@tonic-gate 	ps_t *pp;
4760Sstevel@tonic-gate 
4770Sstevel@tonic-gate 	/* If this node already has a parent, it's sorted */
4780Sstevel@tonic-gate 	if (p->pp != NULL)
4790Sstevel@tonic-gate 		return;
4800Sstevel@tonic-gate 
4810Sstevel@tonic-gate 	for (n = 0; n < nps; n++) {
4820Sstevel@tonic-gate 		pp = ps[n];
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate 		if (pp != NULL && p != pp && p->ppid == pp->pid) {
4854470Sacruz 			if (cflag && p->pid >= 0 &&
4864470Sacruz 			    p->ctid != -1 && p->ctid != pp->ctid) {
4870Sstevel@tonic-gate 				ctsort(p->ctid, p);
4880Sstevel@tonic-gate 			} else {
4890Sstevel@tonic-gate 				insertchild(pp, p);
4900Sstevel@tonic-gate 				prsort(pp);
4910Sstevel@tonic-gate 			}
4920Sstevel@tonic-gate 			return;
4930Sstevel@tonic-gate 		}
4940Sstevel@tonic-gate 	}
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate 	/* File parentless processes under their contracts */
4970Sstevel@tonic-gate 	if (cflag && p->pid >= 0)
4980Sstevel@tonic-gate 		ctsort(p->ctid, p);
4990Sstevel@tonic-gate }
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate static void
printsubtree(ps_t * p,int level)5020Sstevel@tonic-gate printsubtree(ps_t *p, int level)
5030Sstevel@tonic-gate {
5040Sstevel@tonic-gate 	int printed;
5050Sstevel@tonic-gate 
5060Sstevel@tonic-gate 	printed = printone(p, level);
5070Sstevel@tonic-gate 	if (level != 0 || printed == 1)
5080Sstevel@tonic-gate 		level++;
5090Sstevel@tonic-gate 	for (p = p->cp; p != NULL; p = p->sp)
5100Sstevel@tonic-gate 		printsubtree(p, level);
5110Sstevel@tonic-gate }
5120Sstevel@tonic-gate 
5130Sstevel@tonic-gate static void
markprocs(ps_t * p)5140Sstevel@tonic-gate markprocs(ps_t *p)
5150Sstevel@tonic-gate {
5160Sstevel@tonic-gate 	if (!zflag || p->zoneid == zoneid)
5170Sstevel@tonic-gate 		p->done = 1;
5180Sstevel@tonic-gate 	for (p = p->cp; p != NULL; p = p->sp)
5190Sstevel@tonic-gate 		markprocs(p);
5200Sstevel@tonic-gate }
5210Sstevel@tonic-gate 
5220Sstevel@tonic-gate /*
5230Sstevel@tonic-gate  * If there's no "top" process, we fake one; it will be the parent of
5240Sstevel@tonic-gate  * all orphans.
5250Sstevel@tonic-gate  */
5260Sstevel@tonic-gate static ps_t *
fakepid0(void)5270Sstevel@tonic-gate fakepid0(void)
5280Sstevel@tonic-gate {
5290Sstevel@tonic-gate 	ps_t *p0, *p;
5300Sstevel@tonic-gate 	int n;
5310Sstevel@tonic-gate 
5320Sstevel@tonic-gate 	if ((p0 = malloc(sizeof (ps_t))) == NULL) {
5330Sstevel@tonic-gate 		perror("malloc()");
5340Sstevel@tonic-gate 		exit(1);
5350Sstevel@tonic-gate 	}
5360Sstevel@tonic-gate 	(void) memset(p0, '\0', sizeof (ps_t));
5370Sstevel@tonic-gate 
5380Sstevel@tonic-gate 	/* First build all partial process trees. */
5390Sstevel@tonic-gate 	for (n = 0; n < nps; n++) {
5400Sstevel@tonic-gate 		p = ps[n];
5410Sstevel@tonic-gate 		if (p->pp == NULL)
5420Sstevel@tonic-gate 			prsort(p);
5430Sstevel@tonic-gate 	}
5440Sstevel@tonic-gate 
5450Sstevel@tonic-gate 	/* Then adopt all orphans. */
5460Sstevel@tonic-gate 	for (n = 0; n < nps; n++) {
5470Sstevel@tonic-gate 		p = ps[n];
5480Sstevel@tonic-gate 		if (p->pp == NULL)
5490Sstevel@tonic-gate 			insertchild(p0, p);
5500Sstevel@tonic-gate 	}
5510Sstevel@tonic-gate 
5520Sstevel@tonic-gate 	/* We've made sure earlier there's room for this. */
5530Sstevel@tonic-gate 	ps[nps++] = p0;
5540Sstevel@tonic-gate 	return (p0);
5550Sstevel@tonic-gate }
5560Sstevel@tonic-gate 
5570Sstevel@tonic-gate /* convert string containing zone name or id to a numeric id */
5580Sstevel@tonic-gate static zoneid_t
getzone(char * arg)5590Sstevel@tonic-gate getzone(char *arg)
5600Sstevel@tonic-gate {
5610Sstevel@tonic-gate 	zoneid_t zoneid;
5620Sstevel@tonic-gate 
5630Sstevel@tonic-gate 	if (zone_get_id(arg, &zoneid) != 0) {
5640Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: unknown zone: %s\n", command, arg);
5650Sstevel@tonic-gate 		exit(1);
5660Sstevel@tonic-gate 	}
5670Sstevel@tonic-gate 	return (zoneid);
5680Sstevel@tonic-gate }
569