xref: /illumos-gate/usr/src/cmd/ptools/ptree/ptree.c (revision a63fed2a)
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.
24a2e92fdbSJohn Levon  * Copyright 2019 Joyent, Inc.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  * ptree -- print family tree of processes
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include <assert.h>
327c478bd9Sstevel@tonic-gate #include <stdio.h>
337c478bd9Sstevel@tonic-gate #include <string.h>
347c478bd9Sstevel@tonic-gate #include <errno.h>
359f62da4dSJason King #include <err.h>
367c478bd9Sstevel@tonic-gate #include <fcntl.h>
379f62da4dSJason King #include <sys/debug.h>
387c478bd9Sstevel@tonic-gate #include <sys/types.h>
397c478bd9Sstevel@tonic-gate #include <sys/termios.h>
407c478bd9Sstevel@tonic-gate #include <unistd.h>
417c478bd9Sstevel@tonic-gate #include <stdlib.h>
427c478bd9Sstevel@tonic-gate #include <dirent.h>
437c478bd9Sstevel@tonic-gate #include <pwd.h>
447c478bd9Sstevel@tonic-gate #include <libproc.h>
457c478bd9Sstevel@tonic-gate #include <libzonecfg.h>
467c478bd9Sstevel@tonic-gate #include <limits.h>
477c478bd9Sstevel@tonic-gate #include <libcontract.h>
489f62da4dSJason King #include <locale.h>
497c478bd9Sstevel@tonic-gate #include <sys/contract.h>
507c478bd9Sstevel@tonic-gate #include <sys/ctfs.h>
517c478bd9Sstevel@tonic-gate #include <libcontract_priv.h>
527c478bd9Sstevel@tonic-gate #include <sys/stat.h>
53a2e92fdbSJohn Levon #include <stdbool.h>
547c478bd9Sstevel@tonic-gate 
559f62da4dSJason King #define	COLUMN_DEFAULT	80
569f62da4dSJason King #define	CHUNK_SIZE	256 /* Arbitrary amount */
577c478bd9Sstevel@tonic-gate #define	FAKEDPID0(p)	(p->pid == 0 && p->psargs[0] == '\0')
589f62da4dSJason King #define	HAS_SIBLING(p)	((p)->sp != NULL && (p)->sp->done != 0)
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate typedef struct ps {
617c478bd9Sstevel@tonic-gate 	int	done;
627c478bd9Sstevel@tonic-gate 	uid_t	uid;
637c478bd9Sstevel@tonic-gate 	uid_t	gid;
647c478bd9Sstevel@tonic-gate 	pid_t	pid;		/* pid == -1 indicates this is a contract */
657c478bd9Sstevel@tonic-gate 	pid_t	ppid;
667c478bd9Sstevel@tonic-gate 	pid_t	pgrp;
677c478bd9Sstevel@tonic-gate 	pid_t	sid;
687c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
697c478bd9Sstevel@tonic-gate 	ctid_t	ctid;
70a2e92fdbSJohn Levon 	char *svc_fmri;
717c478bd9Sstevel@tonic-gate 	timestruc_t start;
727c478bd9Sstevel@tonic-gate 	char	psargs[PRARGSZ];
737c478bd9Sstevel@tonic-gate 	struct ps *pp;		/* parent */
747c478bd9Sstevel@tonic-gate 	struct ps *sp;		/* sibling */
757c478bd9Sstevel@tonic-gate 	struct ps *cp;		/* child */
767c478bd9Sstevel@tonic-gate } ps_t;
777c478bd9Sstevel@tonic-gate 
789f62da4dSJason King enum { DASH = 0, BAR, CORNER, VRIGHT };
799f62da4dSJason King 
807c478bd9Sstevel@tonic-gate static	ps_t	**ps;		/* array of ps_t's */
817c478bd9Sstevel@tonic-gate static	unsigned psize;		/* size of array */
827c478bd9Sstevel@tonic-gate static	int	nps;		/* number of ps_t's */
837c478bd9Sstevel@tonic-gate static	ps_t	**ctps;		/* array of contract ps_t's */
847c478bd9Sstevel@tonic-gate static	unsigned ctsize;	/* size of contract array */
857c478bd9Sstevel@tonic-gate static	int	nctps;		/* number of contract ps_t's */
867c478bd9Sstevel@tonic-gate static	ps_t	*proc0;		/* process 0 */
877c478bd9Sstevel@tonic-gate static	ps_t	*proc1;		/* process 1 */
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate static	int	aflag = 0;
907c478bd9Sstevel@tonic-gate static	int	cflag = 0;
919f62da4dSJason King static	int	gflag = 0;
92a2e92fdbSJohn Levon static	int	sflag = 0;
93f21abddfSJason King static	int	wflag = 0;
947c478bd9Sstevel@tonic-gate static	int	zflag = 0;
957c478bd9Sstevel@tonic-gate static	zoneid_t zoneid;
96a2e92fdbSJohn Levon static	char *match_svc;
97a2e92fdbSJohn Levon static	char *match_inst;
989f62da4dSJason King static	int	columns;
999f62da4dSJason King 
1009f62da4dSJason King static const char *box_ascii[] = {
1019f62da4dSJason King 	[DASH] =	"-",
1029f62da4dSJason King 	[BAR] =		"|",
1039f62da4dSJason King 	[CORNER] =	"`",
1049f62da4dSJason King 	[VRIGHT] =	"+"
1059f62da4dSJason King };
1069f62da4dSJason King 
1079f62da4dSJason King static const char *box_utf8[] = {
1089f62da4dSJason King 	[DASH] =	"\xe2\x94\x80", /* \u2500 */
1099f62da4dSJason King 	[BAR] =		"\xe2\x94\x82", /* \u2502 */
1109f62da4dSJason King 	[CORNER] =	"\xe2\x94\x94", /* \u2514 */
1119f62da4dSJason King 	[VRIGHT] =	"\xe2\x94\x9c", /* \u251c */
1129f62da4dSJason King };
1139f62da4dSJason King 
1149f62da4dSJason King static const char **box;
1159f62da4dSJason King 
1169f62da4dSJason King static size_t get_termwidth(void);
1179f62da4dSJason King static const char **get_boxchars(void);
1189f62da4dSJason King static int add_proc(psinfo_t *, lwpsinfo_t *, void *);
119a2e92fdbSJohn Levon static bool match_proc(ps_t *);
120a2e92fdbSJohn Levon static void markprocs(ps_t *);
121a2e92fdbSJohn Levon static int printone(ps_t *, int);
1227c478bd9Sstevel@tonic-gate static void insertchild(ps_t *, ps_t *);
123a2e92fdbSJohn Levon static void prsort(ps_t *);
124a2e92fdbSJohn Levon static void printsubtree(ps_t *, int);
125a2e92fdbSJohn Levon static void p_get_svc_fmri(ps_t *, ct_stathdl_t);
126a2e92fdbSJohn Levon static char *parse_svc(const char *, char **);
127a2e92fdbSJohn Levon static zoneid_t getzone(const char *);
1287c478bd9Sstevel@tonic-gate static ps_t *fakepid0(void);
1297c478bd9Sstevel@tonic-gate 
1309f62da4dSJason King static void *zalloc(size_t);
1319f62da4dSJason King static void *xreallocarray(void *, size_t, size_t);
1329f62da4dSJason King static char *xstrdup(const char *);
1339f62da4dSJason King 
1349f62da4dSJason King static void __NORETURN
usage(void)1359f62da4dSJason King usage(void)
1369f62da4dSJason King {
1379f62da4dSJason King 	(void) fprintf(stderr,
1389f62da4dSJason King 	    "usage:\t%s [-ac] [-s svc] [-z zone] [ {pid|user} ... ]\n",
1399f62da4dSJason King 	    getprogname());
1409f62da4dSJason King 	(void) fprintf(stderr,
1419f62da4dSJason King 	    "  (show process trees)\n");
1429f62da4dSJason King 	(void) fprintf(stderr,
1439f62da4dSJason King 	    "  list can include process-ids and user names\n");
1449f62da4dSJason King 	(void) fprintf(stderr,
1459f62da4dSJason King 	    "  -a : include children of process 0\n");
1469f62da4dSJason King 	(void) fprintf(stderr,
1479f62da4dSJason King 	    "  -c : show contracts\n");
1489f62da4dSJason King 	(void) fprintf(stderr,
1499f62da4dSJason King 	    "  -g : use line drawing characters in output\n");
1509f62da4dSJason King 	(void) fprintf(stderr,
1519f62da4dSJason King 	    "  -s : print only processes with given service FMRI\n");
152f21abddfSJason King 	(void) fprintf(stderr,
153f21abddfSJason King 	    "  -w : allow lines to wrap instead of truncating\n");
1549f62da4dSJason King 	(void) fprintf(stderr,
1559f62da4dSJason King 	    "  -z : print only processes in given zone\n");
1569f62da4dSJason King 	exit(2);
1579f62da4dSJason King }
1589f62da4dSJason King 
1597c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv)1607c478bd9Sstevel@tonic-gate main(int argc, char **argv)
1617c478bd9Sstevel@tonic-gate {
1627c478bd9Sstevel@tonic-gate 	int opt;
1637c478bd9Sstevel@tonic-gate 	int errflg = 0;
1647c478bd9Sstevel@tonic-gate 	int n;
1657c478bd9Sstevel@tonic-gate 	int retc = 0;
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	ps_t *p;
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	/* options */
170f21abddfSJason King 	while ((opt = getopt(argc, argv, "acgs:wz:")) != EOF) {
1717c478bd9Sstevel@tonic-gate 		switch (opt) {
1727c478bd9Sstevel@tonic-gate 		case 'a':		/* include children of process 0 */
1737c478bd9Sstevel@tonic-gate 			aflag = 1;
1747c478bd9Sstevel@tonic-gate 			break;
1757c478bd9Sstevel@tonic-gate 		case 'c':		/* display contract ownership */
1767c478bd9Sstevel@tonic-gate 			aflag = cflag = 1;
1777c478bd9Sstevel@tonic-gate 			break;
1789f62da4dSJason King 		case 'g':
1799f62da4dSJason King 			gflag = 1;
1809f62da4dSJason King 			box = get_boxchars();
1819f62da4dSJason King 			break;
182a2e92fdbSJohn Levon 		case 's':
183a2e92fdbSJohn Levon 			sflag = 1;
184a2e92fdbSJohn Levon 			match_svc = parse_svc(optarg, &match_inst);
185a2e92fdbSJohn Levon 			break;
186f21abddfSJason King 		case 'w':
187f21abddfSJason King 			wflag = 1;
188f21abddfSJason King 			break;
1897c478bd9Sstevel@tonic-gate 		case 'z':		/* only processes in given zone */
1907c478bd9Sstevel@tonic-gate 			zflag = 1;
1917c478bd9Sstevel@tonic-gate 			zoneid = getzone(optarg);
1927c478bd9Sstevel@tonic-gate 			break;
1937c478bd9Sstevel@tonic-gate 		default:
1947c478bd9Sstevel@tonic-gate 			errflg = 1;
1957c478bd9Sstevel@tonic-gate 			break;
1967c478bd9Sstevel@tonic-gate 		}
1977c478bd9Sstevel@tonic-gate 	}
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 	argc -= optind;
2007c478bd9Sstevel@tonic-gate 	argv += optind;
2017c478bd9Sstevel@tonic-gate 
2029f62da4dSJason King 	if (errflg)
2039f62da4dSJason King 		usage();
2047c478bd9Sstevel@tonic-gate 
205f21abddfSJason King 	if (!wflag) {
206f21abddfSJason King 		columns = get_termwidth();
207f21abddfSJason King 		VERIFY3S(columns, >, 0);
208f21abddfSJason King 	}
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 	nps = 0;
2117c478bd9Sstevel@tonic-gate 	psize = 0;
2127c478bd9Sstevel@tonic-gate 	ps = NULL;
2137c478bd9Sstevel@tonic-gate 
2149f62da4dSJason King 	/* Currently, this can only fail if the 3rd argument is invalid */
215*a63fed2aSJason King 	VERIFY0(proc_walk(add_proc, NULL, PR_WALK_PROC|PR_WALK_INCLUDE_SYS));
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	if (proc0 == NULL)
2187c478bd9Sstevel@tonic-gate 		proc0 = fakepid0();
2197c478bd9Sstevel@tonic-gate 	if (proc1 == NULL)
2207c478bd9Sstevel@tonic-gate 		proc1 = proc0;
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	for (n = 0; n < nps; n++) {
2237c478bd9Sstevel@tonic-gate 		p = ps[n];
2247c478bd9Sstevel@tonic-gate 		if (p->pp == NULL)
2257c478bd9Sstevel@tonic-gate 			prsort(p);
2267c478bd9Sstevel@tonic-gate 	}
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 	if (cflag)
2297c478bd9Sstevel@tonic-gate 		/* Parent all orphan contracts to process 0. */
2307c478bd9Sstevel@tonic-gate 		for (n = 0; n < nctps; n++) {
2317c478bd9Sstevel@tonic-gate 			p = ctps[n];
2327c478bd9Sstevel@tonic-gate 			if (p->pp == NULL)
2337c478bd9Sstevel@tonic-gate 				insertchild(proc0, p);
2347c478bd9Sstevel@tonic-gate 		}
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 	if (argc == 0) {
2377c478bd9Sstevel@tonic-gate 		for (p = aflag ? proc0->cp : proc1->cp; p != NULL; p = p->sp) {
2387c478bd9Sstevel@tonic-gate 			markprocs(p);
2397c478bd9Sstevel@tonic-gate 			printsubtree(p, 0);
2407c478bd9Sstevel@tonic-gate 		}
2417c478bd9Sstevel@tonic-gate 		return (0);
2427c478bd9Sstevel@tonic-gate 	}
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	/*
2457c478bd9Sstevel@tonic-gate 	 * Initially, assume we're not going to find any processes.  If we do
2467c478bd9Sstevel@tonic-gate 	 * mark any, then set this to 0 to indicate no error.
2477c478bd9Sstevel@tonic-gate 	 */
2487c478bd9Sstevel@tonic-gate 	errflg = 1;
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 	while (argc-- > 0) {
2517c478bd9Sstevel@tonic-gate 		char *arg;
2527c478bd9Sstevel@tonic-gate 		char *next;
2537c478bd9Sstevel@tonic-gate 		pid_t pid;
2547c478bd9Sstevel@tonic-gate 		uid_t uid;
2557c478bd9Sstevel@tonic-gate 		int n;
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 		/* in case some silly person said 'ptree /proc/[0-9]*' */
2587c478bd9Sstevel@tonic-gate 		arg = strrchr(*argv, '/');
2597c478bd9Sstevel@tonic-gate 		if (arg++ == NULL)
2607c478bd9Sstevel@tonic-gate 			arg = *argv;
2617c478bd9Sstevel@tonic-gate 		argv++;
262f48205beScasper 		uid = (uid_t)-1;
2637c478bd9Sstevel@tonic-gate 		errno = 0;
2647c478bd9Sstevel@tonic-gate 		pid = strtoul(arg, &next, 10);
2657c478bd9Sstevel@tonic-gate 		if (errno != 0 || *next != '\0') {
2667c478bd9Sstevel@tonic-gate 			struct passwd *pw = getpwnam(arg);
2677c478bd9Sstevel@tonic-gate 			if (pw == NULL) {
2689f62da4dSJason King 				warnx("invalid username: %s", arg);
2697c478bd9Sstevel@tonic-gate 				retc = 1;
2707c478bd9Sstevel@tonic-gate 				continue;
2717c478bd9Sstevel@tonic-gate 			}
2727c478bd9Sstevel@tonic-gate 			uid = pw->pw_uid;
2737c478bd9Sstevel@tonic-gate 			pid = -1;
2747c478bd9Sstevel@tonic-gate 		}
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 		for (n = 0; n < nps; n++) {
2777c478bd9Sstevel@tonic-gate 			ps_t *p = ps[n];
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 			/*
2807c478bd9Sstevel@tonic-gate 			 * A match on pid causes the subtree starting at pid
2817c478bd9Sstevel@tonic-gate 			 * to be printed, regardless of the -a flag.
2827c478bd9Sstevel@tonic-gate 			 * For uid matches, we never include pid 0 and only
2837c478bd9Sstevel@tonic-gate 			 * include the children of pid 0 if -a was specified.
2847c478bd9Sstevel@tonic-gate 			 */
2857c478bd9Sstevel@tonic-gate 			if (p->pid == pid || (p->uid == uid && p->pid != 0 &&
2867c478bd9Sstevel@tonic-gate 			    (p->ppid != 0 || aflag))) {
2877c478bd9Sstevel@tonic-gate 				errflg = 0;
2887c478bd9Sstevel@tonic-gate 				markprocs(p);
2897c478bd9Sstevel@tonic-gate 				if (p->pid != 0)
2907c478bd9Sstevel@tonic-gate 					for (p = p->pp; p != NULL &&
2917c478bd9Sstevel@tonic-gate 					    p->done != 1 && p->pid != 0;
2927c478bd9Sstevel@tonic-gate 					    p = p->pp)
2937c478bd9Sstevel@tonic-gate 						if ((p->ppid != 0 || aflag) &&
294a2e92fdbSJohn Levon 						    match_proc(p))
2957c478bd9Sstevel@tonic-gate 							p->done = 1;
296f48205beScasper 				if (uid == (uid_t)-1)
2977c478bd9Sstevel@tonic-gate 					break;
2987c478bd9Sstevel@tonic-gate 			}
2997c478bd9Sstevel@tonic-gate 		}
3007c478bd9Sstevel@tonic-gate 	}
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 	printsubtree(proc0, 0);
3037c478bd9Sstevel@tonic-gate 	/*
3047c478bd9Sstevel@tonic-gate 	 * retc = 1 if an invalid username was supplied.
3057c478bd9Sstevel@tonic-gate 	 * errflg = 1 if no matching processes were found.
3067c478bd9Sstevel@tonic-gate 	 */
3077c478bd9Sstevel@tonic-gate 	return (retc || errflg);
3087c478bd9Sstevel@tonic-gate }
3097c478bd9Sstevel@tonic-gate 
3109f62da4dSJason King 
3119f62da4dSJason King #define	PIDWIDTH	6
3129f62da4dSJason King 
3139f62da4dSJason King static void
printlines(ps_t * p,int level)3149f62da4dSJason King printlines(ps_t *p, int level)
3159f62da4dSJason King {
3169f62da4dSJason King 	if (level == 0)
3179f62da4dSJason King 		return;
3189f62da4dSJason King 
3199f62da4dSJason King 	if (!gflag) {
3209f62da4dSJason King 		(void) printf("%*s", level * 2, "");
3219f62da4dSJason King 		return;
3229f62da4dSJason King 	}
3239f62da4dSJason King 
3249f62da4dSJason King 	for (int i = 1; i < level; i++) {
3259f62da4dSJason King 		ps_t *ancestor = p;
3269f62da4dSJason King 
3279f62da4dSJason King 		/* Find our ancestor at depth 'i' */
3289f62da4dSJason King 		for (int j = i; j < level; j++)
3299f62da4dSJason King 			ancestor = ancestor->pp;
3309f62da4dSJason King 
3319f62da4dSJason King 		(void) printf("%s ", HAS_SIBLING(ancestor) ? box[BAR] : " ");
3329f62da4dSJason King 	}
3339f62da4dSJason King 
3349f62da4dSJason King 	(void) printf("%s%s", HAS_SIBLING(p) ? box[VRIGHT] : box[CORNER],
3359f62da4dSJason King 	    box[DASH]);
3369f62da4dSJason King }
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate static int
printone(ps_t * p,int level)3397c478bd9Sstevel@tonic-gate printone(ps_t *p, int level)
3407c478bd9Sstevel@tonic-gate {
3417c478bd9Sstevel@tonic-gate 	int n, indent;
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 	if (p->done && !FAKEDPID0(p)) {
3447c478bd9Sstevel@tonic-gate 		indent = level * 2;
345f21abddfSJason King 
346f21abddfSJason King 		if (wflag) {
347f21abddfSJason King 			n = strlen(p->psargs);
348f21abddfSJason King 		} else {
349f21abddfSJason King 			if ((n = columns - PIDWIDTH - indent - 2) < 0)
350f21abddfSJason King 				n = 0;
351f21abddfSJason King 		}
352f21abddfSJason King 
3539f62da4dSJason King 		printlines(p, level);
3547c478bd9Sstevel@tonic-gate 		if (p->pid >= 0) {
3559f62da4dSJason King 			(void) printf("%-*d %.*s\n", PIDWIDTH, (int)p->pid, n,
3569f62da4dSJason King 			    p->psargs);
3577c478bd9Sstevel@tonic-gate 		} else {
3587c478bd9Sstevel@tonic-gate 			assert(cflag != 0);
3599f62da4dSJason King 			(void) printf("[process contract %d: %s]\n",
3609f62da4dSJason King 			    (int)p->ctid,
361a2e92fdbSJohn Levon 			    p->svc_fmri == NULL ? "?" : p->svc_fmri);
3627c478bd9Sstevel@tonic-gate 		}
3637c478bd9Sstevel@tonic-gate 		return (1);
3647c478bd9Sstevel@tonic-gate 	}
3657c478bd9Sstevel@tonic-gate 	return (0);
3667c478bd9Sstevel@tonic-gate }
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate static void
insertchild(ps_t * pp,ps_t * cp)3697c478bd9Sstevel@tonic-gate insertchild(ps_t *pp, ps_t *cp)
3707c478bd9Sstevel@tonic-gate {
3717c478bd9Sstevel@tonic-gate 	/* insert as child process of p */
3727c478bd9Sstevel@tonic-gate 	ps_t **here;
3737c478bd9Sstevel@tonic-gate 	ps_t *sp;
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 	/* sort by start time */
3767c478bd9Sstevel@tonic-gate 	for (here = &pp->cp, sp = pp->cp;
3777c478bd9Sstevel@tonic-gate 	    sp != NULL;
3787c478bd9Sstevel@tonic-gate 	    here = &sp->sp, sp = sp->sp) {
3797c478bd9Sstevel@tonic-gate 		if (cp->start.tv_sec < sp->start.tv_sec)
3807c478bd9Sstevel@tonic-gate 			break;
3817c478bd9Sstevel@tonic-gate 		if (cp->start.tv_sec == sp->start.tv_sec &&
3827c478bd9Sstevel@tonic-gate 		    cp->start.tv_nsec < sp->start.tv_nsec)
3837c478bd9Sstevel@tonic-gate 			break;
3847c478bd9Sstevel@tonic-gate 	}
3857c478bd9Sstevel@tonic-gate 	cp->pp = pp;
3867c478bd9Sstevel@tonic-gate 	cp->sp = sp;
3877c478bd9Sstevel@tonic-gate 	*here = cp;
3887c478bd9Sstevel@tonic-gate }
3897c478bd9Sstevel@tonic-gate 
390a2e92fdbSJohn Levon static ct_stathdl_t
ct_status_open(ctid_t ctid,struct stat64 * stp)391a2e92fdbSJohn Levon ct_status_open(ctid_t ctid, struct stat64 *stp)
392a2e92fdbSJohn Levon {
393a2e92fdbSJohn Levon 	ct_stathdl_t hdl;
394a2e92fdbSJohn Levon 	int fd;
395a2e92fdbSJohn Levon 
396a2e92fdbSJohn Levon 	if ((fd = contract_open(ctid, "process", "status", O_RDONLY)) == -1)
397a2e92fdbSJohn Levon 		return (NULL);
398a2e92fdbSJohn Levon 
399a2e92fdbSJohn Levon 	if (fstat64(fd, stp) == -1 || ct_status_read(fd, CTD_FIXED, &hdl)) {
400a2e92fdbSJohn Levon 		(void) close(fd);
401a2e92fdbSJohn Levon 		return (NULL);
402a2e92fdbSJohn Levon 	}
403a2e92fdbSJohn Levon 
404a2e92fdbSJohn Levon 	(void) close(fd);
405a2e92fdbSJohn Levon 
406a2e92fdbSJohn Levon 	return (hdl);
407a2e92fdbSJohn Levon }
408a2e92fdbSJohn Levon 
409a2e92fdbSJohn Levon /*
410a2e92fdbSJohn Levon  * strdup() failure is OK - better to report something than fail totally.
411a2e92fdbSJohn Levon  */
412a2e92fdbSJohn Levon static void
p_get_svc_fmri(ps_t * p,ct_stathdl_t inhdl)413a2e92fdbSJohn Levon p_get_svc_fmri(ps_t *p, ct_stathdl_t inhdl)
414a2e92fdbSJohn Levon {
415a2e92fdbSJohn Levon 	ct_stathdl_t hdl = inhdl;
416a2e92fdbSJohn Levon 	struct stat64 st;
417a2e92fdbSJohn Levon 	char *fmri;
418a2e92fdbSJohn Levon 
419a2e92fdbSJohn Levon 	if (hdl == NULL && (hdl = ct_status_open(p->ctid, &st)) == NULL)
420a2e92fdbSJohn Levon 		return;
421a2e92fdbSJohn Levon 
422a2e92fdbSJohn Levon 	if (ct_pr_status_get_svc_fmri(hdl, &fmri) == 0)
423a2e92fdbSJohn Levon 		p->svc_fmri = strdup(fmri);
424a2e92fdbSJohn Levon 
425a2e92fdbSJohn Levon 	if (inhdl == NULL)
426a2e92fdbSJohn Levon 		ct_status_free(hdl);
427a2e92fdbSJohn Levon }
428a2e92fdbSJohn Levon 
4297c478bd9Sstevel@tonic-gate static void
ctsort(ctid_t ctid,ps_t * p)4307c478bd9Sstevel@tonic-gate ctsort(ctid_t ctid, ps_t *p)
4317c478bd9Sstevel@tonic-gate {
4327c478bd9Sstevel@tonic-gate 	ps_t *pp;
433a2e92fdbSJohn Levon 	int n;
4347c478bd9Sstevel@tonic-gate 	ct_stathdl_t hdl;
4357c478bd9Sstevel@tonic-gate 	struct stat64 st;
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate 	for (n = 0; n < nctps; n++)
4387c478bd9Sstevel@tonic-gate 		if (ctps[n]->ctid == ctid) {
4397c478bd9Sstevel@tonic-gate 			insertchild(ctps[n], p);
4407c478bd9Sstevel@tonic-gate 			return;
4417c478bd9Sstevel@tonic-gate 		}
4427c478bd9Sstevel@tonic-gate 
443a2e92fdbSJohn Levon 	if ((hdl = ct_status_open(ctid, &st)) == NULL)
4447c478bd9Sstevel@tonic-gate 		return;
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 	if (nctps >= ctsize) {
4479f62da4dSJason King 		ctsize += CHUNK_SIZE;
4489f62da4dSJason King 		ctps = xreallocarray(ctps, ctsize, sizeof (ps_t *));
4497c478bd9Sstevel@tonic-gate 	}
4509f62da4dSJason King 	pp = zalloc(sizeof (*pp));
4517c478bd9Sstevel@tonic-gate 	ctps[nctps++] = pp;
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 	pp->pid = -1;
4547c478bd9Sstevel@tonic-gate 	pp->ctid = ctid;
455a2e92fdbSJohn Levon 
456a2e92fdbSJohn Levon 	p_get_svc_fmri(pp, hdl);
457a2e92fdbSJohn Levon 
4587c478bd9Sstevel@tonic-gate 	pp->start.tv_sec = st.st_ctime;
4597c478bd9Sstevel@tonic-gate 	insertchild(pp, p);
4607c478bd9Sstevel@tonic-gate 
4616b31a7daSacruz 	pp->zoneid = ct_status_get_zoneid(hdl);
462a2e92fdbSJohn Levon 
4636b31a7daSacruz 	/*
4646b31a7daSacruz 	 * In a zlogin <zonename>, the contract belongs to the
4656b31a7daSacruz 	 * global zone and the shell opened belongs to <zonename>.
4666b31a7daSacruz 	 * If the -c and -z zonename flags are used together, then
4670630b7d7Sacruz 	 * we need to adjust the zoneid in the contract's ps_t as
4686b31a7daSacruz 	 * follows:
4690630b7d7Sacruz 	 *
4706b31a7daSacruz 	 * ptree -c -z <zonename> --> zoneid == p->zoneid
4710630b7d7Sacruz 	 * ptree -c -z global	  --> zoneid == pp->zoneid
4726b31a7daSacruz 	 *
4736b31a7daSacruz 	 * The approach assumes that no tool can create processes in
4746b31a7daSacruz 	 * different zones under the same contract. If this is
4756b31a7daSacruz 	 * possible, ptree will need to refactor how it builds
4766b31a7daSacruz 	 * its internal tree of ps_t's
4776b31a7daSacruz 	 */
4786b31a7daSacruz 	if (zflag && p->zoneid != pp->zoneid &&
4796b31a7daSacruz 	    (zoneid == p->zoneid || zoneid == pp->zoneid))
4806b31a7daSacruz 		pp->zoneid = p->zoneid;
4817c478bd9Sstevel@tonic-gate 	if (ct_status_get_state(hdl) == CTS_OWNED) {
4827c478bd9Sstevel@tonic-gate 		pp->ppid = ct_status_get_holder(hdl);
4837c478bd9Sstevel@tonic-gate 		prsort(pp);
4847c478bd9Sstevel@tonic-gate 	} else if (ct_status_get_state(hdl) == CTS_INHERITED) {
4857c478bd9Sstevel@tonic-gate 		ctsort(ct_status_get_holder(hdl), pp);
4867c478bd9Sstevel@tonic-gate 	}
4877c478bd9Sstevel@tonic-gate 	ct_status_free(hdl);
4887c478bd9Sstevel@tonic-gate }
4897c478bd9Sstevel@tonic-gate 
4907c478bd9Sstevel@tonic-gate static void
prsort(ps_t * p)4917c478bd9Sstevel@tonic-gate prsort(ps_t *p)
4927c478bd9Sstevel@tonic-gate {
4937c478bd9Sstevel@tonic-gate 	int n;
4947c478bd9Sstevel@tonic-gate 	ps_t *pp;
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate 	/* If this node already has a parent, it's sorted */
4977c478bd9Sstevel@tonic-gate 	if (p->pp != NULL)
4987c478bd9Sstevel@tonic-gate 		return;
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate 	for (n = 0; n < nps; n++) {
5017c478bd9Sstevel@tonic-gate 		pp = ps[n];
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate 		if (pp != NULL && p != pp && p->ppid == pp->pid) {
5046b31a7daSacruz 			if (cflag && p->pid >= 0 &&
5056b31a7daSacruz 			    p->ctid != -1 && p->ctid != pp->ctid) {
5067c478bd9Sstevel@tonic-gate 				ctsort(p->ctid, p);
5077c478bd9Sstevel@tonic-gate 			} else {
5087c478bd9Sstevel@tonic-gate 				insertchild(pp, p);
5097c478bd9Sstevel@tonic-gate 				prsort(pp);
5107c478bd9Sstevel@tonic-gate 			}
5117c478bd9Sstevel@tonic-gate 			return;
5127c478bd9Sstevel@tonic-gate 		}
5137c478bd9Sstevel@tonic-gate 	}
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate 	/* File parentless processes under their contracts */
5167c478bd9Sstevel@tonic-gate 	if (cflag && p->pid >= 0)
5177c478bd9Sstevel@tonic-gate 		ctsort(p->ctid, p);
5187c478bd9Sstevel@tonic-gate }
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate static void
printsubtree(ps_t * p,int level)5217c478bd9Sstevel@tonic-gate printsubtree(ps_t *p, int level)
5227c478bd9Sstevel@tonic-gate {
5237c478bd9Sstevel@tonic-gate 	int printed;
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 	printed = printone(p, level);
5267c478bd9Sstevel@tonic-gate 	if (level != 0 || printed == 1)
5277c478bd9Sstevel@tonic-gate 		level++;
5287c478bd9Sstevel@tonic-gate 	for (p = p->cp; p != NULL; p = p->sp)
5297c478bd9Sstevel@tonic-gate 		printsubtree(p, level);
5307c478bd9Sstevel@tonic-gate }
5317c478bd9Sstevel@tonic-gate 
532a2e92fdbSJohn Levon /*
533a2e92fdbSJohn Levon  * Match against the service name (and just the final component), and any
534a2e92fdbSJohn Levon  * specified instance name.
535a2e92fdbSJohn Levon  */
536a2e92fdbSJohn Levon static bool
match_proc(ps_t * p)537a2e92fdbSJohn Levon match_proc(ps_t *p)
538a2e92fdbSJohn Levon {
539a2e92fdbSJohn Levon 	bool matched = false;
540a2e92fdbSJohn Levon 	const char *cp;
541a2e92fdbSJohn Levon 	char *p_inst;
542a2e92fdbSJohn Levon 	char *p_svc;
543a2e92fdbSJohn Levon 
544a2e92fdbSJohn Levon 	if (zflag && p->zoneid != zoneid)
545a2e92fdbSJohn Levon 		return (false);
546a2e92fdbSJohn Levon 
547a2e92fdbSJohn Levon 	if (!sflag)
548a2e92fdbSJohn Levon 		return (true);
549a2e92fdbSJohn Levon 
550a2e92fdbSJohn Levon 	if (p->svc_fmri == NULL)
551a2e92fdbSJohn Levon 		return (false);
552a2e92fdbSJohn Levon 
553a2e92fdbSJohn Levon 	p_svc = parse_svc(p->svc_fmri, &p_inst);
554a2e92fdbSJohn Levon 
555a2e92fdbSJohn Levon 	if (strcmp(p_svc, match_svc) != 0 &&
556a2e92fdbSJohn Levon 	    ((cp = strrchr(p_svc, '/')) == NULL ||
557a2e92fdbSJohn Levon 	    strcmp(cp + 1, match_svc) != 0)) {
558a2e92fdbSJohn Levon 		goto out;
559a2e92fdbSJohn Levon 	}
560a2e92fdbSJohn Levon 
561a2e92fdbSJohn Levon 	if (strlen(match_inst) == 0 ||
562a2e92fdbSJohn Levon 	    strcmp(p_inst, match_inst) == 0)
563a2e92fdbSJohn Levon 		matched = true;
564a2e92fdbSJohn Levon 
565a2e92fdbSJohn Levon out:
566a2e92fdbSJohn Levon 	free(p_svc);
567a2e92fdbSJohn Levon 	free(p_inst);
568a2e92fdbSJohn Levon 	return (matched);
569a2e92fdbSJohn Levon }
570a2e92fdbSJohn Levon 
5717c478bd9Sstevel@tonic-gate static void
markprocs(ps_t * p)5727c478bd9Sstevel@tonic-gate markprocs(ps_t *p)
5737c478bd9Sstevel@tonic-gate {
574a2e92fdbSJohn Levon 	if (match_proc(p))
5757c478bd9Sstevel@tonic-gate 		p->done = 1;
576a2e92fdbSJohn Levon 
5777c478bd9Sstevel@tonic-gate 	for (p = p->cp; p != NULL; p = p->sp)
5787c478bd9Sstevel@tonic-gate 		markprocs(p);
5797c478bd9Sstevel@tonic-gate }
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate /*
5827c478bd9Sstevel@tonic-gate  * If there's no "top" process, we fake one; it will be the parent of
5837c478bd9Sstevel@tonic-gate  * all orphans.
5847c478bd9Sstevel@tonic-gate  */
5857c478bd9Sstevel@tonic-gate static ps_t *
fakepid0(void)5867c478bd9Sstevel@tonic-gate fakepid0(void)
5877c478bd9Sstevel@tonic-gate {
5887c478bd9Sstevel@tonic-gate 	ps_t *p0, *p;
5897c478bd9Sstevel@tonic-gate 	int n;
5907c478bd9Sstevel@tonic-gate 
5919f62da4dSJason King 	p0 = zalloc(sizeof (*p0));
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate 	/* First build all partial process trees. */
5947c478bd9Sstevel@tonic-gate 	for (n = 0; n < nps; n++) {
5957c478bd9Sstevel@tonic-gate 		p = ps[n];
5967c478bd9Sstevel@tonic-gate 		if (p->pp == NULL)
5977c478bd9Sstevel@tonic-gate 			prsort(p);
5987c478bd9Sstevel@tonic-gate 	}
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate 	/* Then adopt all orphans. */
6017c478bd9Sstevel@tonic-gate 	for (n = 0; n < nps; n++) {
6027c478bd9Sstevel@tonic-gate 		p = ps[n];
6037c478bd9Sstevel@tonic-gate 		if (p->pp == NULL)
6047c478bd9Sstevel@tonic-gate 			insertchild(p0, p);
6057c478bd9Sstevel@tonic-gate 	}
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate 	/* We've made sure earlier there's room for this. */
6087c478bd9Sstevel@tonic-gate 	ps[nps++] = p0;
6097c478bd9Sstevel@tonic-gate 	return (p0);
6107c478bd9Sstevel@tonic-gate }
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate /* convert string containing zone name or id to a numeric id */
6137c478bd9Sstevel@tonic-gate static zoneid_t
getzone(const char * arg)614a2e92fdbSJohn Levon getzone(const char *arg)
6157c478bd9Sstevel@tonic-gate {
6167c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
6177c478bd9Sstevel@tonic-gate 
6189f62da4dSJason King 	if (zone_get_id(arg, &zoneid) != 0)
6199f62da4dSJason King 		err(EXIT_FAILURE, "unknown zone: %s", arg);
6209f62da4dSJason King 
6217c478bd9Sstevel@tonic-gate 	return (zoneid);
6227c478bd9Sstevel@tonic-gate }
623a2e92fdbSJohn Levon 
624a2e92fdbSJohn Levon /* svc:/mysvc:default ->  mysvc, default */
625a2e92fdbSJohn Levon static char *
parse_svc(const char * arg,char ** instp)626a2e92fdbSJohn Levon parse_svc(const char *arg, char **instp)
627a2e92fdbSJohn Levon {
628a2e92fdbSJohn Levon 	const char *p = arg;
629a2e92fdbSJohn Levon 	char *ret;
630a2e92fdbSJohn Levon 	char *cp;
631a2e92fdbSJohn Levon 
632a2e92fdbSJohn Levon 	if (strncmp(p, "svc:/", strlen("svc:/")) == 0)
633a2e92fdbSJohn Levon 		p += strlen("svc:/");
634a2e92fdbSJohn Levon 
6359f62da4dSJason King 	ret = xstrdup(p);
636a2e92fdbSJohn Levon 
637a2e92fdbSJohn Levon 	if ((cp = strrchr(ret, ':')) != NULL) {
638a2e92fdbSJohn Levon 		*cp = '\0';
639a2e92fdbSJohn Levon 		cp++;
640a2e92fdbSJohn Levon 	} else {
641a2e92fdbSJohn Levon 		cp = "";
642a2e92fdbSJohn Levon 	}
643a2e92fdbSJohn Levon 
6449f62da4dSJason King 	*instp = xstrdup(cp);
6459f62da4dSJason King 	return (ret);
6469f62da4dSJason King }
6479f62da4dSJason King 
6489f62da4dSJason King static int
add_proc(psinfo_t * info,lwpsinfo_t * lwp __unused,void * arg __unused)6499f62da4dSJason King add_proc(psinfo_t *info, lwpsinfo_t *lwp __unused, void *arg __unused)
6509f62da4dSJason King {
6519f62da4dSJason King 	ps_t *p;
6529f62da4dSJason King 
6539f62da4dSJason King 	/*
6549f62da4dSJason King 	 * We make sure there is always a free slot in the table
6559f62da4dSJason King 	 * in case we need to add a fake p0;
6569f62da4dSJason King 	 */
6579f62da4dSJason King 	if (nps + 1 >= psize) {
6589f62da4dSJason King 		psize += CHUNK_SIZE;
6599f62da4dSJason King 		ps = xreallocarray(ps, psize, sizeof (ps_t));
6609f62da4dSJason King 	}
6619f62da4dSJason King 
6629f62da4dSJason King 	p = zalloc(sizeof (*p));
6639f62da4dSJason King 	ps[nps++] = p;
6649f62da4dSJason King 	p->done = 0;
6659f62da4dSJason King 	p->uid = info->pr_uid;
6669f62da4dSJason King 	p->gid = info->pr_gid;
6679f62da4dSJason King 	p->pid = info->pr_pid;
6689f62da4dSJason King 	p->ppid = info->pr_ppid;
6699f62da4dSJason King 	p->pgrp = info->pr_pgid;
6709f62da4dSJason King 	p->sid = info->pr_sid;
6719f62da4dSJason King 	p->zoneid = info->pr_zoneid;
6729f62da4dSJason King 	p->ctid = info->pr_contract;
6739f62da4dSJason King 	p->start = info->pr_start;
6749f62da4dSJason King 	proc_unctrl_psinfo(info);
6759f62da4dSJason King 	if (info->pr_nlwp == 0)
6769f62da4dSJason King 		(void) strcpy(p->psargs, "<defunct>");
6779f62da4dSJason King 	else if (info->pr_psargs[0] == '\0')
6789f62da4dSJason King 		(void) strncpy(p->psargs, info->pr_fname,
6799f62da4dSJason King 		    sizeof (p->psargs));
6809f62da4dSJason King 	else
6819f62da4dSJason King 		(void) strncpy(p->psargs, info->pr_psargs,
6829f62da4dSJason King 		    sizeof (p->psargs));
6839f62da4dSJason King 	p->psargs[sizeof (p->psargs)-1] = '\0';
6849f62da4dSJason King 	p->pp = NULL;
6859f62da4dSJason King 	p->sp = NULL;
6869f62da4dSJason King 
6879f62da4dSJason King 	if (sflag)
6889f62da4dSJason King 		p_get_svc_fmri(p, NULL);
6899f62da4dSJason King 
6909f62da4dSJason King 	if (p->pid == p->ppid)
6919f62da4dSJason King 		proc0 = p;
6929f62da4dSJason King 	if (p->pid == 1)
6939f62da4dSJason King 		proc1 = p;
6949f62da4dSJason King 
6959f62da4dSJason King 	return (0);
6969f62da4dSJason King }
6979f62da4dSJason King 
6989f62da4dSJason King 
6999f62da4dSJason King static size_t
get_termwidth(void)7009f62da4dSJason King get_termwidth(void)
7019f62da4dSJason King {
7029f62da4dSJason King 	char *s;
7039f62da4dSJason King 
7049f62da4dSJason King 	if ((s = getenv("COLUMNS")) != NULL) {
7059f62da4dSJason King 		unsigned long n;
7069f62da4dSJason King 
7079f62da4dSJason King 		errno = 0;
7089f62da4dSJason King 		n = strtoul(s, NULL, 10);
7099f62da4dSJason King 		if (n != 0 && errno == 0) {
7109f62da4dSJason King 			/* Sanity check on the range */
7119f62da4dSJason King 			if (n > INT_MAX)
7129f62da4dSJason King 				n = COLUMN_DEFAULT;
7139f62da4dSJason King 			return (n);
7149f62da4dSJason King 		}
715a2e92fdbSJohn Levon 	}
716a2e92fdbSJohn Levon 
7179f62da4dSJason King 	struct winsize winsize;
7189f62da4dSJason King 
7199f62da4dSJason King 	if (isatty(STDOUT_FILENO) &&
7209f62da4dSJason King 	    ioctl(STDOUT_FILENO, TIOCGWINSZ, &winsize) == 0 &&
7219f62da4dSJason King 	    winsize.ws_col != 0) {
7229f62da4dSJason King 		return (winsize.ws_col);
7239f62da4dSJason King 	}
7249f62da4dSJason King 
7259f62da4dSJason King 	return (COLUMN_DEFAULT);
7269f62da4dSJason King }
7279f62da4dSJason King 
7289f62da4dSJason King static const char **
get_boxchars(void)7299f62da4dSJason King get_boxchars(void)
7309f62da4dSJason King {
7319f62da4dSJason King 	char *loc = setlocale(LC_ALL, "");
7329f62da4dSJason King 
7339f62da4dSJason King 	if (loc == NULL)
7349f62da4dSJason King 		return (box_ascii);
7359f62da4dSJason King 
7369f62da4dSJason King 	const char *p = strstr(loc, "UTF-8");
7379f62da4dSJason King 
7389f62da4dSJason King 	/*
7399f62da4dSJason King 	 * Only use the UTF-8 box drawing characters if the locale ends
7409f62da4dSJason King 	 * with "UTF-8".
7419f62da4dSJason King 	 */
7429f62da4dSJason King 	if (p != NULL && p[5] == '\0')
7439f62da4dSJason King 		return (box_utf8);
7449f62da4dSJason King 
7459f62da4dSJason King 	return (box_ascii);
7469f62da4dSJason King }
7479f62da4dSJason King 
7489f62da4dSJason King static void *
zalloc(size_t len)7499f62da4dSJason King zalloc(size_t len)
7509f62da4dSJason King {
7519f62da4dSJason King 	void *p = calloc(1, len);
7529f62da4dSJason King 
7539f62da4dSJason King 	if (p == NULL)
7549f62da4dSJason King 		err(EXIT_FAILURE, "calloc");
7559f62da4dSJason King 	return (p);
7569f62da4dSJason King }
7579f62da4dSJason King 
7589f62da4dSJason King static void *
xreallocarray(void * ptr,size_t nelem,size_t elsize)7599f62da4dSJason King xreallocarray(void *ptr, size_t nelem, size_t elsize)
7609f62da4dSJason King {
7619f62da4dSJason King 	void *p = reallocarray(ptr, nelem, elsize);
7629f62da4dSJason King 
7639f62da4dSJason King 	if (p == NULL)
7649f62da4dSJason King 		err(EXIT_FAILURE, "reallocarray");
7659f62da4dSJason King 	return (p);
7669f62da4dSJason King }
7679f62da4dSJason King 
7689f62da4dSJason King static char *
xstrdup(const char * s)7699f62da4dSJason King xstrdup(const char *s)
7709f62da4dSJason King {
7719f62da4dSJason King 	char *news = strdup(s);
7729f62da4dSJason King 
7739f62da4dSJason King 	if (news == NULL)
7749f62da4dSJason King 		err(EXIT_FAILURE, "strdup");
7759f62da4dSJason King 	return (news);
776a2e92fdbSJohn Levon }
777