xref: /illumos-gate/usr/src/cmd/ptools/ptree/ptree.c (revision 21ca3fa0)
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>
49*21ca3fa0SBill Sommerfeld #include <langinfo.h>
507c478bd9Sstevel@tonic-gate #include <sys/contract.h>
517c478bd9Sstevel@tonic-gate #include <sys/ctfs.h>
527c478bd9Sstevel@tonic-gate #include <libcontract_priv.h>
537c478bd9Sstevel@tonic-gate #include <sys/stat.h>
54a2e92fdbSJohn Levon #include <stdbool.h>
557c478bd9Sstevel@tonic-gate 
569f62da4dSJason King #define	COLUMN_DEFAULT	80
579f62da4dSJason King #define	CHUNK_SIZE	256 /* Arbitrary amount */
587c478bd9Sstevel@tonic-gate #define	FAKEDPID0(p)	(p->pid == 0 && p->psargs[0] == '\0')
599f62da4dSJason King #define	HAS_SIBLING(p)	((p)->sp != NULL && (p)->sp->done != 0)
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate typedef struct ps {
627c478bd9Sstevel@tonic-gate 	int	done;
637c478bd9Sstevel@tonic-gate 	uid_t	uid;
647c478bd9Sstevel@tonic-gate 	uid_t	gid;
657c478bd9Sstevel@tonic-gate 	pid_t	pid;		/* pid == -1 indicates this is a contract */
667c478bd9Sstevel@tonic-gate 	pid_t	ppid;
677c478bd9Sstevel@tonic-gate 	pid_t	pgrp;
687c478bd9Sstevel@tonic-gate 	pid_t	sid;
697c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
707c478bd9Sstevel@tonic-gate 	ctid_t	ctid;
71a2e92fdbSJohn Levon 	char *svc_fmri;
727c478bd9Sstevel@tonic-gate 	timestruc_t start;
737c478bd9Sstevel@tonic-gate 	char	psargs[PRARGSZ];
747c478bd9Sstevel@tonic-gate 	struct ps *pp;		/* parent */
757c478bd9Sstevel@tonic-gate 	struct ps *sp;		/* sibling */
767c478bd9Sstevel@tonic-gate 	struct ps *cp;		/* child */
777c478bd9Sstevel@tonic-gate } ps_t;
787c478bd9Sstevel@tonic-gate 
799f62da4dSJason King enum { DASH = 0, BAR, CORNER, VRIGHT };
809f62da4dSJason King 
817c478bd9Sstevel@tonic-gate static	ps_t	**ps;		/* array of ps_t's */
827c478bd9Sstevel@tonic-gate static	unsigned psize;		/* size of array */
837c478bd9Sstevel@tonic-gate static	int	nps;		/* number of ps_t's */
847c478bd9Sstevel@tonic-gate static	ps_t	**ctps;		/* array of contract ps_t's */
857c478bd9Sstevel@tonic-gate static	unsigned ctsize;	/* size of contract array */
867c478bd9Sstevel@tonic-gate static	int	nctps;		/* number of contract ps_t's */
877c478bd9Sstevel@tonic-gate static	ps_t	*proc0;		/* process 0 */
887c478bd9Sstevel@tonic-gate static	ps_t	*proc1;		/* process 1 */
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate static	int	aflag = 0;
917c478bd9Sstevel@tonic-gate static	int	cflag = 0;
929f62da4dSJason King static	int	gflag = 0;
93a2e92fdbSJohn Levon static	int	sflag = 0;
94f21abddfSJason King static	int	wflag = 0;
957c478bd9Sstevel@tonic-gate static	int	zflag = 0;
967c478bd9Sstevel@tonic-gate static	zoneid_t zoneid;
97a2e92fdbSJohn Levon static	char *match_svc;
98a2e92fdbSJohn Levon static	char *match_inst;
999f62da4dSJason King static	int	columns;
1007c478bd9Sstevel@tonic-gate 
1019f62da4dSJason King static const char *box_ascii[] = {
1029f62da4dSJason King 	[DASH] =	"-",
1039f62da4dSJason King 	[BAR] =		"|",
1049f62da4dSJason King 	[CORNER] =	"`",
1059f62da4dSJason King 	[VRIGHT] =	"+"
1069f62da4dSJason King };
1079f62da4dSJason King 
1089f62da4dSJason King static const char *box_utf8[] = {
1099f62da4dSJason King 	[DASH] =	"\xe2\x94\x80", /* \u2500 */
1109f62da4dSJason King 	[BAR] =		"\xe2\x94\x82", /* \u2502 */
1119f62da4dSJason King 	[CORNER] =	"\xe2\x94\x94", /* \u2514 */
1129f62da4dSJason King 	[VRIGHT] =	"\xe2\x94\x9c", /* \u251c */
1139f62da4dSJason King };
1149f62da4dSJason King 
1159f62da4dSJason King static const char **box;
1169f62da4dSJason King 
1179f62da4dSJason King static size_t get_termwidth(void);
1189f62da4dSJason King static const char **get_boxchars(void);
1199f62da4dSJason King static int add_proc(psinfo_t *, lwpsinfo_t *, void *);
120a2e92fdbSJohn Levon static bool match_proc(ps_t *);
121a2e92fdbSJohn Levon static void markprocs(ps_t *);
122a2e92fdbSJohn Levon static int printone(ps_t *, int);
1237c478bd9Sstevel@tonic-gate static void insertchild(ps_t *, ps_t *);
124a2e92fdbSJohn Levon static void prsort(ps_t *);
125a2e92fdbSJohn Levon static void printsubtree(ps_t *, int);
126a2e92fdbSJohn Levon static void p_get_svc_fmri(ps_t *, ct_stathdl_t);
127a2e92fdbSJohn Levon static char *parse_svc(const char *, char **);
128a2e92fdbSJohn Levon static zoneid_t getzone(const char *);
1297c478bd9Sstevel@tonic-gate static ps_t *fakepid0(void);
1307c478bd9Sstevel@tonic-gate 
1319f62da4dSJason King static void *zalloc(size_t);
1329f62da4dSJason King static void *xreallocarray(void *, size_t, size_t);
1339f62da4dSJason King static char *xstrdup(const char *);
1349f62da4dSJason King 
1359f62da4dSJason King static void __NORETURN
usage(void)1369f62da4dSJason King usage(void)
1379f62da4dSJason King {
1389f62da4dSJason King 	(void) fprintf(stderr,
1399f62da4dSJason King 	    "usage:\t%s [-ac] [-s svc] [-z zone] [ {pid|user} ... ]\n",
1409f62da4dSJason King 	    getprogname());
1419f62da4dSJason King 	(void) fprintf(stderr,
1429f62da4dSJason King 	    "  (show process trees)\n");
1439f62da4dSJason King 	(void) fprintf(stderr,
1449f62da4dSJason King 	    "  list can include process-ids and user names\n");
1459f62da4dSJason King 	(void) fprintf(stderr,
1469f62da4dSJason King 	    "  -a : include children of process 0\n");
1479f62da4dSJason King 	(void) fprintf(stderr,
1489f62da4dSJason King 	    "  -c : show contracts\n");
1499f62da4dSJason King 	(void) fprintf(stderr,
1509f62da4dSJason King 	    "  -g : use line drawing characters in output\n");
1519f62da4dSJason King 	(void) fprintf(stderr,
1529f62da4dSJason King 	    "  -s : print only processes with given service FMRI\n");
1539f62da4dSJason King 	(void) fprintf(stderr,
154f21abddfSJason King 	    "  -w : allow lines to wrap instead of truncating\n");
155f21abddfSJason King 	(void) fprintf(stderr,
1569f62da4dSJason King 	    "  -z : print only processes in given zone\n");
1579f62da4dSJason King 	exit(2);
1589f62da4dSJason King }
1599f62da4dSJason King 
1607c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv)1617c478bd9Sstevel@tonic-gate main(int argc, char **argv)
1627c478bd9Sstevel@tonic-gate {
1637c478bd9Sstevel@tonic-gate 	int opt;
1647c478bd9Sstevel@tonic-gate 	int errflg = 0;
1657c478bd9Sstevel@tonic-gate 	int n;
1667c478bd9Sstevel@tonic-gate 	int retc = 0;
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	ps_t *p;
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	/* options */
171f21abddfSJason King 	while ((opt = getopt(argc, argv, "acgs:wz:")) != EOF) {
1727c478bd9Sstevel@tonic-gate 		switch (opt) {
1737c478bd9Sstevel@tonic-gate 		case 'a':		/* include children of process 0 */
1747c478bd9Sstevel@tonic-gate 			aflag = 1;
1757c478bd9Sstevel@tonic-gate 			break;
1767c478bd9Sstevel@tonic-gate 		case 'c':		/* display contract ownership */
1777c478bd9Sstevel@tonic-gate 			aflag = cflag = 1;
1787c478bd9Sstevel@tonic-gate 			break;
1799f62da4dSJason King 		case 'g':
1809f62da4dSJason King 			gflag = 1;
1819f62da4dSJason King 			box = get_boxchars();
1829f62da4dSJason King 			break;
183a2e92fdbSJohn Levon 		case 's':
184a2e92fdbSJohn Levon 			sflag = 1;
185a2e92fdbSJohn Levon 			match_svc = parse_svc(optarg, &match_inst);
186a2e92fdbSJohn Levon 			break;
187f21abddfSJason King 		case 'w':
188f21abddfSJason King 			wflag = 1;
189f21abddfSJason King 			break;
1907c478bd9Sstevel@tonic-gate 		case 'z':		/* only processes in given zone */
1917c478bd9Sstevel@tonic-gate 			zflag = 1;
1927c478bd9Sstevel@tonic-gate 			zoneid = getzone(optarg);
1937c478bd9Sstevel@tonic-gate 			break;
1947c478bd9Sstevel@tonic-gate 		default:
1957c478bd9Sstevel@tonic-gate 			errflg = 1;
1967c478bd9Sstevel@tonic-gate 			break;
1977c478bd9Sstevel@tonic-gate 		}
1987c478bd9Sstevel@tonic-gate 	}
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	argc -= optind;
2017c478bd9Sstevel@tonic-gate 	argv += optind;
2027c478bd9Sstevel@tonic-gate 
2039f62da4dSJason King 	if (errflg)
2049f62da4dSJason King 		usage();
2057c478bd9Sstevel@tonic-gate 
206f21abddfSJason King 	if (!wflag) {
2079f62da4dSJason King 		columns = get_termwidth();
2089f62da4dSJason King 		VERIFY3S(columns, >, 0);
209f21abddfSJason King 	}
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	nps = 0;
2127c478bd9Sstevel@tonic-gate 	psize = 0;
2137c478bd9Sstevel@tonic-gate 	ps = NULL;
2147c478bd9Sstevel@tonic-gate 
2159f62da4dSJason King 	/* Currently, this can only fail if the 3rd argument is invalid */
216a63fed2aSJason King 	VERIFY0(proc_walk(add_proc, NULL, PR_WALK_PROC|PR_WALK_INCLUDE_SYS));
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 	if (proc0 == NULL)
2197c478bd9Sstevel@tonic-gate 		proc0 = fakepid0();
2207c478bd9Sstevel@tonic-gate 	if (proc1 == NULL)
2217c478bd9Sstevel@tonic-gate 		proc1 = proc0;
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 	for (n = 0; n < nps; n++) {
2247c478bd9Sstevel@tonic-gate 		p = ps[n];
2257c478bd9Sstevel@tonic-gate 		if (p->pp == NULL)
2267c478bd9Sstevel@tonic-gate 			prsort(p);
2277c478bd9Sstevel@tonic-gate 	}
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 	if (cflag)
2307c478bd9Sstevel@tonic-gate 		/* Parent all orphan contracts to process 0. */
2317c478bd9Sstevel@tonic-gate 		for (n = 0; n < nctps; n++) {
2327c478bd9Sstevel@tonic-gate 			p = ctps[n];
2337c478bd9Sstevel@tonic-gate 			if (p->pp == NULL)
2347c478bd9Sstevel@tonic-gate 				insertchild(proc0, p);
2357c478bd9Sstevel@tonic-gate 		}
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 	if (argc == 0) {
2387c478bd9Sstevel@tonic-gate 		for (p = aflag ? proc0->cp : proc1->cp; p != NULL; p = p->sp) {
2397c478bd9Sstevel@tonic-gate 			markprocs(p);
2407c478bd9Sstevel@tonic-gate 			printsubtree(p, 0);
2417c478bd9Sstevel@tonic-gate 		}
2427c478bd9Sstevel@tonic-gate 		return (0);
2437c478bd9Sstevel@tonic-gate 	}
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	/*
2467c478bd9Sstevel@tonic-gate 	 * Initially, assume we're not going to find any processes.  If we do
2477c478bd9Sstevel@tonic-gate 	 * mark any, then set this to 0 to indicate no error.
2487c478bd9Sstevel@tonic-gate 	 */
2497c478bd9Sstevel@tonic-gate 	errflg = 1;
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 	while (argc-- > 0) {
2527c478bd9Sstevel@tonic-gate 		char *arg;
2537c478bd9Sstevel@tonic-gate 		char *next;
2547c478bd9Sstevel@tonic-gate 		pid_t pid;
2557c478bd9Sstevel@tonic-gate 		uid_t uid;
2567c478bd9Sstevel@tonic-gate 		int n;
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 		/* in case some silly person said 'ptree /proc/[0-9]*' */
2597c478bd9Sstevel@tonic-gate 		arg = strrchr(*argv, '/');
2607c478bd9Sstevel@tonic-gate 		if (arg++ == NULL)
2617c478bd9Sstevel@tonic-gate 			arg = *argv;
2627c478bd9Sstevel@tonic-gate 		argv++;
263f48205beScasper 		uid = (uid_t)-1;
2647c478bd9Sstevel@tonic-gate 		errno = 0;
2657c478bd9Sstevel@tonic-gate 		pid = strtoul(arg, &next, 10);
2667c478bd9Sstevel@tonic-gate 		if (errno != 0 || *next != '\0') {
2677c478bd9Sstevel@tonic-gate 			struct passwd *pw = getpwnam(arg);
2687c478bd9Sstevel@tonic-gate 			if (pw == NULL) {
2699f62da4dSJason King 				warnx("invalid username: %s", arg);
2707c478bd9Sstevel@tonic-gate 				retc = 1;
2717c478bd9Sstevel@tonic-gate 				continue;
2727c478bd9Sstevel@tonic-gate 			}
2737c478bd9Sstevel@tonic-gate 			uid = pw->pw_uid;
2747c478bd9Sstevel@tonic-gate 			pid = -1;
2757c478bd9Sstevel@tonic-gate 		}
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 		for (n = 0; n < nps; n++) {
2787c478bd9Sstevel@tonic-gate 			ps_t *p = ps[n];
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 			/*
2817c478bd9Sstevel@tonic-gate 			 * A match on pid causes the subtree starting at pid
2827c478bd9Sstevel@tonic-gate 			 * to be printed, regardless of the -a flag.
2837c478bd9Sstevel@tonic-gate 			 * For uid matches, we never include pid 0 and only
2847c478bd9Sstevel@tonic-gate 			 * include the children of pid 0 if -a was specified.
2857c478bd9Sstevel@tonic-gate 			 */
2867c478bd9Sstevel@tonic-gate 			if (p->pid == pid || (p->uid == uid && p->pid != 0 &&
2877c478bd9Sstevel@tonic-gate 			    (p->ppid != 0 || aflag))) {
2887c478bd9Sstevel@tonic-gate 				errflg = 0;
2897c478bd9Sstevel@tonic-gate 				markprocs(p);
2907c478bd9Sstevel@tonic-gate 				if (p->pid != 0)
2917c478bd9Sstevel@tonic-gate 					for (p = p->pp; p != NULL &&
2927c478bd9Sstevel@tonic-gate 					    p->done != 1 && p->pid != 0;
2937c478bd9Sstevel@tonic-gate 					    p = p->pp)
2947c478bd9Sstevel@tonic-gate 						if ((p->ppid != 0 || aflag) &&
295a2e92fdbSJohn Levon 						    match_proc(p))
2967c478bd9Sstevel@tonic-gate 							p->done = 1;
297f48205beScasper 				if (uid == (uid_t)-1)
2987c478bd9Sstevel@tonic-gate 					break;
2997c478bd9Sstevel@tonic-gate 			}
3007c478bd9Sstevel@tonic-gate 		}
3017c478bd9Sstevel@tonic-gate 	}
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 	printsubtree(proc0, 0);
3047c478bd9Sstevel@tonic-gate 	/*
3057c478bd9Sstevel@tonic-gate 	 * retc = 1 if an invalid username was supplied.
3067c478bd9Sstevel@tonic-gate 	 * errflg = 1 if no matching processes were found.
3077c478bd9Sstevel@tonic-gate 	 */
3087c478bd9Sstevel@tonic-gate 	return (retc || errflg);
3097c478bd9Sstevel@tonic-gate }
3107c478bd9Sstevel@tonic-gate 
3119f62da4dSJason King 
3129f62da4dSJason King #define	PIDWIDTH	6
3139f62da4dSJason King 
3149f62da4dSJason King static void
printlines(ps_t * p,int level)3159f62da4dSJason King printlines(ps_t *p, int level)
3169f62da4dSJason King {
3179f62da4dSJason King 	if (level == 0)
3189f62da4dSJason King 		return;
3199f62da4dSJason King 
3209f62da4dSJason King 	if (!gflag) {
3219f62da4dSJason King 		(void) printf("%*s", level * 2, "");
3229f62da4dSJason King 		return;
3239f62da4dSJason King 	}
3249f62da4dSJason King 
3259f62da4dSJason King 	for (int i = 1; i < level; i++) {
3269f62da4dSJason King 		ps_t *ancestor = p;
3279f62da4dSJason King 
3289f62da4dSJason King 		/* Find our ancestor at depth 'i' */
3299f62da4dSJason King 		for (int j = i; j < level; j++)
3309f62da4dSJason King 			ancestor = ancestor->pp;
3319f62da4dSJason King 
3329f62da4dSJason King 		(void) printf("%s ", HAS_SIBLING(ancestor) ? box[BAR] : " ");
3339f62da4dSJason King 	}
3349f62da4dSJason King 
3359f62da4dSJason King 	(void) printf("%s%s", HAS_SIBLING(p) ? box[VRIGHT] : box[CORNER],
3369f62da4dSJason King 	    box[DASH]);
3379f62da4dSJason King }
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate static int
printone(ps_t * p,int level)3407c478bd9Sstevel@tonic-gate printone(ps_t *p, int level)
3417c478bd9Sstevel@tonic-gate {
3427c478bd9Sstevel@tonic-gate 	int n, indent;
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 	if (p->done && !FAKEDPID0(p)) {
3457c478bd9Sstevel@tonic-gate 		indent = level * 2;
346f21abddfSJason King 
347f21abddfSJason King 		if (wflag) {
348f21abddfSJason King 			n = strlen(p->psargs);
349f21abddfSJason King 		} else {
3507c478bd9Sstevel@tonic-gate 			if ((n = columns - PIDWIDTH - indent - 2) < 0)
3517c478bd9Sstevel@tonic-gate 				n = 0;
352f21abddfSJason King 		}
353f21abddfSJason King 
3549f62da4dSJason King 		printlines(p, level);
3557c478bd9Sstevel@tonic-gate 		if (p->pid >= 0) {
3569f62da4dSJason King 			(void) printf("%-*d %.*s\n", PIDWIDTH, (int)p->pid, n,
3579f62da4dSJason King 			    p->psargs);
3587c478bd9Sstevel@tonic-gate 		} else {
3597c478bd9Sstevel@tonic-gate 			assert(cflag != 0);
3609f62da4dSJason King 			(void) printf("[process contract %d: %s]\n",
3619f62da4dSJason King 			    (int)p->ctid,
362a2e92fdbSJohn Levon 			    p->svc_fmri == NULL ? "?" : p->svc_fmri);
3637c478bd9Sstevel@tonic-gate 		}
3647c478bd9Sstevel@tonic-gate 		return (1);
3657c478bd9Sstevel@tonic-gate 	}
3667c478bd9Sstevel@tonic-gate 	return (0);
3677c478bd9Sstevel@tonic-gate }
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate static void
insertchild(ps_t * pp,ps_t * cp)3707c478bd9Sstevel@tonic-gate insertchild(ps_t *pp, ps_t *cp)
3717c478bd9Sstevel@tonic-gate {
3727c478bd9Sstevel@tonic-gate 	/* insert as child process of p */
3737c478bd9Sstevel@tonic-gate 	ps_t **here;
3747c478bd9Sstevel@tonic-gate 	ps_t *sp;
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 	/* sort by start time */
3777c478bd9Sstevel@tonic-gate 	for (here = &pp->cp, sp = pp->cp;
3787c478bd9Sstevel@tonic-gate 	    sp != NULL;
3797c478bd9Sstevel@tonic-gate 	    here = &sp->sp, sp = sp->sp) {
3807c478bd9Sstevel@tonic-gate 		if (cp->start.tv_sec < sp->start.tv_sec)
3817c478bd9Sstevel@tonic-gate 			break;
3827c478bd9Sstevel@tonic-gate 		if (cp->start.tv_sec == sp->start.tv_sec &&
3837c478bd9Sstevel@tonic-gate 		    cp->start.tv_nsec < sp->start.tv_nsec)
3847c478bd9Sstevel@tonic-gate 			break;
3857c478bd9Sstevel@tonic-gate 	}
3867c478bd9Sstevel@tonic-gate 	cp->pp = pp;
3877c478bd9Sstevel@tonic-gate 	cp->sp = sp;
3887c478bd9Sstevel@tonic-gate 	*here = cp;
3897c478bd9Sstevel@tonic-gate }
3907c478bd9Sstevel@tonic-gate 
391a2e92fdbSJohn Levon static ct_stathdl_t
ct_status_open(ctid_t ctid,struct stat64 * stp)392a2e92fdbSJohn Levon ct_status_open(ctid_t ctid, struct stat64 *stp)
393a2e92fdbSJohn Levon {
394a2e92fdbSJohn Levon 	ct_stathdl_t hdl;
395a2e92fdbSJohn Levon 	int fd;
396a2e92fdbSJohn Levon 
397a2e92fdbSJohn Levon 	if ((fd = contract_open(ctid, "process", "status", O_RDONLY)) == -1)
398a2e92fdbSJohn Levon 		return (NULL);
399a2e92fdbSJohn Levon 
400a2e92fdbSJohn Levon 	if (fstat64(fd, stp) == -1 || ct_status_read(fd, CTD_FIXED, &hdl)) {
401a2e92fdbSJohn Levon 		(void) close(fd);
402a2e92fdbSJohn Levon 		return (NULL);
403a2e92fdbSJohn Levon 	}
404a2e92fdbSJohn Levon 
405a2e92fdbSJohn Levon 	(void) close(fd);
406a2e92fdbSJohn Levon 
407a2e92fdbSJohn Levon 	return (hdl);
408a2e92fdbSJohn Levon }
409a2e92fdbSJohn Levon 
410a2e92fdbSJohn Levon /*
411a2e92fdbSJohn Levon  * strdup() failure is OK - better to report something than fail totally.
412a2e92fdbSJohn Levon  */
413a2e92fdbSJohn Levon static void
p_get_svc_fmri(ps_t * p,ct_stathdl_t inhdl)414a2e92fdbSJohn Levon p_get_svc_fmri(ps_t *p, ct_stathdl_t inhdl)
415a2e92fdbSJohn Levon {
416a2e92fdbSJohn Levon 	ct_stathdl_t hdl = inhdl;
417a2e92fdbSJohn Levon 	struct stat64 st;
418a2e92fdbSJohn Levon 	char *fmri;
419a2e92fdbSJohn Levon 
420a2e92fdbSJohn Levon 	if (hdl == NULL && (hdl = ct_status_open(p->ctid, &st)) == NULL)
421a2e92fdbSJohn Levon 		return;
422a2e92fdbSJohn Levon 
423a2e92fdbSJohn Levon 	if (ct_pr_status_get_svc_fmri(hdl, &fmri) == 0)
424a2e92fdbSJohn Levon 		p->svc_fmri = strdup(fmri);
425a2e92fdbSJohn Levon 
426a2e92fdbSJohn Levon 	if (inhdl == NULL)
427a2e92fdbSJohn Levon 		ct_status_free(hdl);
428a2e92fdbSJohn Levon }
429a2e92fdbSJohn Levon 
4307c478bd9Sstevel@tonic-gate static void
ctsort(ctid_t ctid,ps_t * p)4317c478bd9Sstevel@tonic-gate ctsort(ctid_t ctid, ps_t *p)
4327c478bd9Sstevel@tonic-gate {
4337c478bd9Sstevel@tonic-gate 	ps_t *pp;
434a2e92fdbSJohn Levon 	int n;
4357c478bd9Sstevel@tonic-gate 	ct_stathdl_t hdl;
4367c478bd9Sstevel@tonic-gate 	struct stat64 st;
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate 	for (n = 0; n < nctps; n++)
4397c478bd9Sstevel@tonic-gate 		if (ctps[n]->ctid == ctid) {
4407c478bd9Sstevel@tonic-gate 			insertchild(ctps[n], p);
4417c478bd9Sstevel@tonic-gate 			return;
4427c478bd9Sstevel@tonic-gate 		}
4437c478bd9Sstevel@tonic-gate 
444a2e92fdbSJohn Levon 	if ((hdl = ct_status_open(ctid, &st)) == NULL)
4457c478bd9Sstevel@tonic-gate 		return;
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate 	if (nctps >= ctsize) {
4489f62da4dSJason King 		ctsize += CHUNK_SIZE;
4499f62da4dSJason King 		ctps = xreallocarray(ctps, ctsize, sizeof (ps_t *));
4507c478bd9Sstevel@tonic-gate 	}
4519f62da4dSJason King 	pp = zalloc(sizeof (*pp));
4527c478bd9Sstevel@tonic-gate 	ctps[nctps++] = pp;
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate 	pp->pid = -1;
4557c478bd9Sstevel@tonic-gate 	pp->ctid = ctid;
456a2e92fdbSJohn Levon 
457a2e92fdbSJohn Levon 	p_get_svc_fmri(pp, hdl);
458a2e92fdbSJohn Levon 
4597c478bd9Sstevel@tonic-gate 	pp->start.tv_sec = st.st_ctime;
4607c478bd9Sstevel@tonic-gate 	insertchild(pp, p);
4617c478bd9Sstevel@tonic-gate 
4626b31a7daSacruz 	pp->zoneid = ct_status_get_zoneid(hdl);
463a2e92fdbSJohn Levon 
4646b31a7daSacruz 	/*
4656b31a7daSacruz 	 * In a zlogin <zonename>, the contract belongs to the
4666b31a7daSacruz 	 * global zone and the shell opened belongs to <zonename>.
4676b31a7daSacruz 	 * If the -c and -z zonename flags are used together, then
4686b31a7daSacruz 	 * we need to adjust the zoneid in the contract's ps_t as
4696b31a7daSacruz 	 * follows:
4706b31a7daSacruz 	 *
4716b31a7daSacruz 	 * ptree -c -z <zonename> --> zoneid == p->zoneid
4726b31a7daSacruz 	 * ptree -c -z global	  --> zoneid == pp->zoneid
4736b31a7daSacruz 	 *
4746b31a7daSacruz 	 * The approach assumes that no tool can create processes in
4756b31a7daSacruz 	 * different zones under the same contract. If this is
4766b31a7daSacruz 	 * possible, ptree will need to refactor how it builds
4776b31a7daSacruz 	 * its internal tree of ps_t's
4786b31a7daSacruz 	 */
4796b31a7daSacruz 	if (zflag && p->zoneid != pp->zoneid &&
4806b31a7daSacruz 	    (zoneid == p->zoneid || zoneid == pp->zoneid))
4816b31a7daSacruz 		pp->zoneid = p->zoneid;
4827c478bd9Sstevel@tonic-gate 	if (ct_status_get_state(hdl) == CTS_OWNED) {
4837c478bd9Sstevel@tonic-gate 		pp->ppid = ct_status_get_holder(hdl);
4847c478bd9Sstevel@tonic-gate 		prsort(pp);
4857c478bd9Sstevel@tonic-gate 	} else if (ct_status_get_state(hdl) == CTS_INHERITED) {
4867c478bd9Sstevel@tonic-gate 		ctsort(ct_status_get_holder(hdl), pp);
4877c478bd9Sstevel@tonic-gate 	}
4887c478bd9Sstevel@tonic-gate 	ct_status_free(hdl);
4897c478bd9Sstevel@tonic-gate }
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate static void
prsort(ps_t * p)4927c478bd9Sstevel@tonic-gate prsort(ps_t *p)
4937c478bd9Sstevel@tonic-gate {
4947c478bd9Sstevel@tonic-gate 	int n;
4957c478bd9Sstevel@tonic-gate 	ps_t *pp;
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate 	/* If this node already has a parent, it's sorted */
4987c478bd9Sstevel@tonic-gate 	if (p->pp != NULL)
4997c478bd9Sstevel@tonic-gate 		return;
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate 	for (n = 0; n < nps; n++) {
5027c478bd9Sstevel@tonic-gate 		pp = ps[n];
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate 		if (pp != NULL && p != pp && p->ppid == pp->pid) {
5056b31a7daSacruz 			if (cflag && p->pid >= 0 &&
5066b31a7daSacruz 			    p->ctid != -1 && p->ctid != pp->ctid) {
5077c478bd9Sstevel@tonic-gate 				ctsort(p->ctid, p);
5087c478bd9Sstevel@tonic-gate 			} else {
5097c478bd9Sstevel@tonic-gate 				insertchild(pp, p);
5107c478bd9Sstevel@tonic-gate 				prsort(pp);
5117c478bd9Sstevel@tonic-gate 			}
5127c478bd9Sstevel@tonic-gate 			return;
5137c478bd9Sstevel@tonic-gate 		}
5147c478bd9Sstevel@tonic-gate 	}
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate 	/* File parentless processes under their contracts */
5177c478bd9Sstevel@tonic-gate 	if (cflag && p->pid >= 0)
5187c478bd9Sstevel@tonic-gate 		ctsort(p->ctid, p);
5197c478bd9Sstevel@tonic-gate }
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate static void
printsubtree(ps_t * p,int level)5227c478bd9Sstevel@tonic-gate printsubtree(ps_t *p, int level)
5237c478bd9Sstevel@tonic-gate {
5247c478bd9Sstevel@tonic-gate 	int printed;
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate 	printed = printone(p, level);
5277c478bd9Sstevel@tonic-gate 	if (level != 0 || printed == 1)
5287c478bd9Sstevel@tonic-gate 		level++;
5297c478bd9Sstevel@tonic-gate 	for (p = p->cp; p != NULL; p = p->sp)
5307c478bd9Sstevel@tonic-gate 		printsubtree(p, level);
5317c478bd9Sstevel@tonic-gate }
5327c478bd9Sstevel@tonic-gate 
533a2e92fdbSJohn Levon /*
534a2e92fdbSJohn Levon  * Match against the service name (and just the final component), and any
535a2e92fdbSJohn Levon  * specified instance name.
536a2e92fdbSJohn Levon  */
537a2e92fdbSJohn Levon static bool
match_proc(ps_t * p)538a2e92fdbSJohn Levon match_proc(ps_t *p)
539a2e92fdbSJohn Levon {
540a2e92fdbSJohn Levon 	bool matched = false;
541a2e92fdbSJohn Levon 	const char *cp;
542a2e92fdbSJohn Levon 	char *p_inst;
543a2e92fdbSJohn Levon 	char *p_svc;
544a2e92fdbSJohn Levon 
545a2e92fdbSJohn Levon 	if (zflag && p->zoneid != zoneid)
546a2e92fdbSJohn Levon 		return (false);
547a2e92fdbSJohn Levon 
548a2e92fdbSJohn Levon 	if (!sflag)
549a2e92fdbSJohn Levon 		return (true);
550a2e92fdbSJohn Levon 
551a2e92fdbSJohn Levon 	if (p->svc_fmri == NULL)
552a2e92fdbSJohn Levon 		return (false);
553a2e92fdbSJohn Levon 
554a2e92fdbSJohn Levon 	p_svc = parse_svc(p->svc_fmri, &p_inst);
555a2e92fdbSJohn Levon 
556a2e92fdbSJohn Levon 	if (strcmp(p_svc, match_svc) != 0 &&
557a2e92fdbSJohn Levon 	    ((cp = strrchr(p_svc, '/')) == NULL ||
558a2e92fdbSJohn Levon 	    strcmp(cp + 1, match_svc) != 0)) {
559a2e92fdbSJohn Levon 		goto out;
560a2e92fdbSJohn Levon 	}
561a2e92fdbSJohn Levon 
562a2e92fdbSJohn Levon 	if (strlen(match_inst) == 0 ||
563a2e92fdbSJohn Levon 	    strcmp(p_inst, match_inst) == 0)
564a2e92fdbSJohn Levon 		matched = true;
565a2e92fdbSJohn Levon 
566a2e92fdbSJohn Levon out:
567a2e92fdbSJohn Levon 	free(p_svc);
568a2e92fdbSJohn Levon 	free(p_inst);
569a2e92fdbSJohn Levon 	return (matched);
570a2e92fdbSJohn Levon }
571a2e92fdbSJohn Levon 
5727c478bd9Sstevel@tonic-gate static void
markprocs(ps_t * p)5737c478bd9Sstevel@tonic-gate markprocs(ps_t *p)
5747c478bd9Sstevel@tonic-gate {
575a2e92fdbSJohn Levon 	if (match_proc(p))
5767c478bd9Sstevel@tonic-gate 		p->done = 1;
577a2e92fdbSJohn Levon 
5787c478bd9Sstevel@tonic-gate 	for (p = p->cp; p != NULL; p = p->sp)
5797c478bd9Sstevel@tonic-gate 		markprocs(p);
5807c478bd9Sstevel@tonic-gate }
5817c478bd9Sstevel@tonic-gate 
5827c478bd9Sstevel@tonic-gate /*
5837c478bd9Sstevel@tonic-gate  * If there's no "top" process, we fake one; it will be the parent of
5847c478bd9Sstevel@tonic-gate  * all orphans.
5857c478bd9Sstevel@tonic-gate  */
5867c478bd9Sstevel@tonic-gate static ps_t *
fakepid0(void)5877c478bd9Sstevel@tonic-gate fakepid0(void)
5887c478bd9Sstevel@tonic-gate {
5897c478bd9Sstevel@tonic-gate 	ps_t *p0, *p;
5907c478bd9Sstevel@tonic-gate 	int n;
5917c478bd9Sstevel@tonic-gate 
5929f62da4dSJason King 	p0 = zalloc(sizeof (*p0));
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 	/* First build all partial process trees. */
5957c478bd9Sstevel@tonic-gate 	for (n = 0; n < nps; n++) {
5967c478bd9Sstevel@tonic-gate 		p = ps[n];
5977c478bd9Sstevel@tonic-gate 		if (p->pp == NULL)
5987c478bd9Sstevel@tonic-gate 			prsort(p);
5997c478bd9Sstevel@tonic-gate 	}
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate 	/* Then adopt all orphans. */
6027c478bd9Sstevel@tonic-gate 	for (n = 0; n < nps; n++) {
6037c478bd9Sstevel@tonic-gate 		p = ps[n];
6047c478bd9Sstevel@tonic-gate 		if (p->pp == NULL)
6057c478bd9Sstevel@tonic-gate 			insertchild(p0, p);
6067c478bd9Sstevel@tonic-gate 	}
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate 	/* We've made sure earlier there's room for this. */
6097c478bd9Sstevel@tonic-gate 	ps[nps++] = p0;
6107c478bd9Sstevel@tonic-gate 	return (p0);
6117c478bd9Sstevel@tonic-gate }
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate /* convert string containing zone name or id to a numeric id */
6147c478bd9Sstevel@tonic-gate static zoneid_t
getzone(const char * arg)615a2e92fdbSJohn Levon getzone(const char *arg)
6167c478bd9Sstevel@tonic-gate {
6177c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
6187c478bd9Sstevel@tonic-gate 
6199f62da4dSJason King 	if (zone_get_id(arg, &zoneid) != 0)
6209f62da4dSJason King 		err(EXIT_FAILURE, "unknown zone: %s", arg);
6219f62da4dSJason King 
6227c478bd9Sstevel@tonic-gate 	return (zoneid);
6237c478bd9Sstevel@tonic-gate }
624a2e92fdbSJohn Levon 
625a2e92fdbSJohn Levon /* svc:/mysvc:default ->  mysvc, default */
626a2e92fdbSJohn Levon static char *
parse_svc(const char * arg,char ** instp)627a2e92fdbSJohn Levon parse_svc(const char *arg, char **instp)
628a2e92fdbSJohn Levon {
629a2e92fdbSJohn Levon 	const char *p = arg;
630a2e92fdbSJohn Levon 	char *ret;
631a2e92fdbSJohn Levon 	char *cp;
632a2e92fdbSJohn Levon 
633a2e92fdbSJohn Levon 	if (strncmp(p, "svc:/", strlen("svc:/")) == 0)
634a2e92fdbSJohn Levon 		p += strlen("svc:/");
635a2e92fdbSJohn Levon 
6369f62da4dSJason King 	ret = xstrdup(p);
637a2e92fdbSJohn Levon 
638a2e92fdbSJohn Levon 	if ((cp = strrchr(ret, ':')) != NULL) {
639a2e92fdbSJohn Levon 		*cp = '\0';
640a2e92fdbSJohn Levon 		cp++;
641a2e92fdbSJohn Levon 	} else {
642a2e92fdbSJohn Levon 		cp = "";
643a2e92fdbSJohn Levon 	}
644a2e92fdbSJohn Levon 
6459f62da4dSJason King 	*instp = xstrdup(cp);
6469f62da4dSJason King 	return (ret);
647a2e92fdbSJohn Levon }
648a2e92fdbSJohn Levon 
6499f62da4dSJason King static int
add_proc(psinfo_t * info,lwpsinfo_t * lwp __unused,void * arg __unused)6509f62da4dSJason King add_proc(psinfo_t *info, lwpsinfo_t *lwp __unused, void *arg __unused)
6519f62da4dSJason King {
6529f62da4dSJason King 	ps_t *p;
6539f62da4dSJason King 
6549f62da4dSJason King 	/*
6559f62da4dSJason King 	 * We make sure there is always a free slot in the table
6569f62da4dSJason King 	 * in case we need to add a fake p0;
6579f62da4dSJason King 	 */
6589f62da4dSJason King 	if (nps + 1 >= psize) {
6599f62da4dSJason King 		psize += CHUNK_SIZE;
6609f62da4dSJason King 		ps = xreallocarray(ps, psize, sizeof (ps_t));
6619f62da4dSJason King 	}
6629f62da4dSJason King 
6639f62da4dSJason King 	p = zalloc(sizeof (*p));
6649f62da4dSJason King 	ps[nps++] = p;
6659f62da4dSJason King 	p->done = 0;
6669f62da4dSJason King 	p->uid = info->pr_uid;
6679f62da4dSJason King 	p->gid = info->pr_gid;
6689f62da4dSJason King 	p->pid = info->pr_pid;
6699f62da4dSJason King 	p->ppid = info->pr_ppid;
6709f62da4dSJason King 	p->pgrp = info->pr_pgid;
6719f62da4dSJason King 	p->sid = info->pr_sid;
6729f62da4dSJason King 	p->zoneid = info->pr_zoneid;
6739f62da4dSJason King 	p->ctid = info->pr_contract;
6749f62da4dSJason King 	p->start = info->pr_start;
6759f62da4dSJason King 	proc_unctrl_psinfo(info);
6769f62da4dSJason King 	if (info->pr_nlwp == 0)
6779f62da4dSJason King 		(void) strcpy(p->psargs, "<defunct>");
6789f62da4dSJason King 	else if (info->pr_psargs[0] == '\0')
6799f62da4dSJason King 		(void) strncpy(p->psargs, info->pr_fname,
6809f62da4dSJason King 		    sizeof (p->psargs));
6819f62da4dSJason King 	else
6829f62da4dSJason King 		(void) strncpy(p->psargs, info->pr_psargs,
6839f62da4dSJason King 		    sizeof (p->psargs));
6849f62da4dSJason King 	p->psargs[sizeof (p->psargs)-1] = '\0';
6859f62da4dSJason King 	p->pp = NULL;
6869f62da4dSJason King 	p->sp = NULL;
6879f62da4dSJason King 
6889f62da4dSJason King 	if (sflag)
6899f62da4dSJason King 		p_get_svc_fmri(p, NULL);
6909f62da4dSJason King 
6919f62da4dSJason King 	if (p->pid == p->ppid)
6929f62da4dSJason King 		proc0 = p;
6939f62da4dSJason King 	if (p->pid == 1)
6949f62da4dSJason King 		proc1 = p;
6959f62da4dSJason King 
6969f62da4dSJason King 	return (0);
6979f62da4dSJason King }
6989f62da4dSJason King 
6999f62da4dSJason King 
7009f62da4dSJason King static size_t
get_termwidth(void)7019f62da4dSJason King get_termwidth(void)
7029f62da4dSJason King {
7039f62da4dSJason King 	char *s;
7049f62da4dSJason King 
7059f62da4dSJason King 	if ((s = getenv("COLUMNS")) != NULL) {
7069f62da4dSJason King 		unsigned long n;
7079f62da4dSJason King 
7089f62da4dSJason King 		errno = 0;
7099f62da4dSJason King 		n = strtoul(s, NULL, 10);
7109f62da4dSJason King 		if (n != 0 && errno == 0) {
7119f62da4dSJason King 			/* Sanity check on the range */
7129f62da4dSJason King 			if (n > INT_MAX)
7139f62da4dSJason King 				n = COLUMN_DEFAULT;
7149f62da4dSJason King 			return (n);
7159f62da4dSJason King 		}
7169f62da4dSJason King 	}
7179f62da4dSJason King 
7189f62da4dSJason King 	struct winsize winsize;
7199f62da4dSJason King 
7209f62da4dSJason King 	if (isatty(STDOUT_FILENO) &&
7219f62da4dSJason King 	    ioctl(STDOUT_FILENO, TIOCGWINSZ, &winsize) == 0 &&
7229f62da4dSJason King 	    winsize.ws_col != 0) {
7239f62da4dSJason King 		return (winsize.ws_col);
7249f62da4dSJason King 	}
7259f62da4dSJason King 
7269f62da4dSJason King 	return (COLUMN_DEFAULT);
7279f62da4dSJason King }
7289f62da4dSJason King 
7299f62da4dSJason King static const char **
get_boxchars(void)7309f62da4dSJason King get_boxchars(void)
7319f62da4dSJason King {
732*21ca3fa0SBill Sommerfeld 	char *codeset;
7339f62da4dSJason King 
734*21ca3fa0SBill Sommerfeld 	(void) setlocale(LC_CTYPE, "");
7359f62da4dSJason King 
736*21ca3fa0SBill Sommerfeld 	codeset = nl_langinfo(CODESET);
7379f62da4dSJason King 
7389f62da4dSJason King 	/*
739*21ca3fa0SBill Sommerfeld 	 * Only use the UTF-8 box drawing characters if the locale uses
740*21ca3fa0SBill Sommerfeld 	 * the UTF-8 codeset.
7419f62da4dSJason King 	 */
742*21ca3fa0SBill Sommerfeld 	if (codeset != NULL && strcmp(codeset, "UTF-8") == 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