1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate  * pargs examines and prints the arguments (argv), environment (environ),
31*0Sstevel@tonic-gate  * and auxiliary vector of another process.
32*0Sstevel@tonic-gate  *
33*0Sstevel@tonic-gate  * This utility is made more complex because it must run in internationalized
34*0Sstevel@tonic-gate  * environments.  The two key cases for pargs to manage are:
35*0Sstevel@tonic-gate  *
36*0Sstevel@tonic-gate  * 1. pargs and target run in the same locale: pargs must respect the
37*0Sstevel@tonic-gate  * locale, but this case is straightforward.  Care is taken to correctly
38*0Sstevel@tonic-gate  * use wide characters in order to print results properly.
39*0Sstevel@tonic-gate  *
40*0Sstevel@tonic-gate  * 2. pargs and target run in different locales: in this case, pargs examines
41*0Sstevel@tonic-gate  * the string having assumed the victim's locale.  Unprintable (but valid)
42*0Sstevel@tonic-gate  * characters are escaped.  Next, iconv(3c) is used to convert between the
43*0Sstevel@tonic-gate  * target and pargs codeset.  Finally, a second pass to escape unprintable
44*0Sstevel@tonic-gate  * (but valid) characters is made.
45*0Sstevel@tonic-gate  *
46*0Sstevel@tonic-gate  * In any case in which characters are encountered which are not valid in
47*0Sstevel@tonic-gate  * their purported locale, the string "fails" and is treated as a traditional
48*0Sstevel@tonic-gate  * 7-bit ASCII encoded string, and escaped accordingly.
49*0Sstevel@tonic-gate  */
50*0Sstevel@tonic-gate 
51*0Sstevel@tonic-gate #include <stdio.h>
52*0Sstevel@tonic-gate #include <stdlib.h>
53*0Sstevel@tonic-gate #include <locale.h>
54*0Sstevel@tonic-gate #include <wchar.h>
55*0Sstevel@tonic-gate #include <iconv.h>
56*0Sstevel@tonic-gate #include <langinfo.h>
57*0Sstevel@tonic-gate #include <unistd.h>
58*0Sstevel@tonic-gate #include <ctype.h>
59*0Sstevel@tonic-gate #include <fcntl.h>
60*0Sstevel@tonic-gate #include <string.h>
61*0Sstevel@tonic-gate #include <strings.h>
62*0Sstevel@tonic-gate #include <limits.h>
63*0Sstevel@tonic-gate #include <pwd.h>
64*0Sstevel@tonic-gate #include <grp.h>
65*0Sstevel@tonic-gate #include <errno.h>
66*0Sstevel@tonic-gate #include <setjmp.h>
67*0Sstevel@tonic-gate #include <sys/types.h>
68*0Sstevel@tonic-gate #include <sys/auxv.h>
69*0Sstevel@tonic-gate #include <sys/archsystm.h>
70*0Sstevel@tonic-gate #include <sys/proc.h>
71*0Sstevel@tonic-gate #include <sys/elf.h>
72*0Sstevel@tonic-gate #include <libproc.h>
73*0Sstevel@tonic-gate #include <wctype.h>
74*0Sstevel@tonic-gate #include <widec.h>
75*0Sstevel@tonic-gate #include <elfcap.h>
76*0Sstevel@tonic-gate 
77*0Sstevel@tonic-gate typedef struct pargs_data {
78*0Sstevel@tonic-gate 	struct ps_prochandle *pd_proc;	/* target proc handle */
79*0Sstevel@tonic-gate 	psinfo_t *pd_psinfo;		/* target psinfo */
80*0Sstevel@tonic-gate 	char *pd_locale;		/* target process locale */
81*0Sstevel@tonic-gate 	int pd_conv_flags;		/* flags governing string conversion */
82*0Sstevel@tonic-gate 	iconv_t pd_iconv;		/* iconv conversion descriptor */
83*0Sstevel@tonic-gate 	size_t pd_argc;
84*0Sstevel@tonic-gate 	uintptr_t *pd_argv;
85*0Sstevel@tonic-gate 	char **pd_argv_strs;
86*0Sstevel@tonic-gate 	size_t pd_envc;
87*0Sstevel@tonic-gate 	uintptr_t *pd_envp;
88*0Sstevel@tonic-gate 	char **pd_envp_strs;
89*0Sstevel@tonic-gate 	size_t pd_auxc;
90*0Sstevel@tonic-gate 	auxv_t *pd_auxv;
91*0Sstevel@tonic-gate 	char **pd_auxv_strs;
92*0Sstevel@tonic-gate 	char *pd_execname;
93*0Sstevel@tonic-gate } pargs_data_t;
94*0Sstevel@tonic-gate 
95*0Sstevel@tonic-gate #define	CONV_USE_ICONV		0x01
96*0Sstevel@tonic-gate #define	CONV_STRICT_ASCII	0x02
97*0Sstevel@tonic-gate 
98*0Sstevel@tonic-gate static char *command;
99*0Sstevel@tonic-gate static int dmodel;
100*0Sstevel@tonic-gate 
101*0Sstevel@tonic-gate #define	EXTRACT_BUFSZ 128		/* extract_string() initial size */
102*0Sstevel@tonic-gate #define	ENV_CHUNK 16			/* #env ptrs to read at a time */
103*0Sstevel@tonic-gate 
104*0Sstevel@tonic-gate static jmp_buf env;			/* malloc failure handling */
105*0Sstevel@tonic-gate 
106*0Sstevel@tonic-gate static void *
107*0Sstevel@tonic-gate safe_zalloc(size_t size)
108*0Sstevel@tonic-gate {
109*0Sstevel@tonic-gate 	void *p;
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate 	/*
112*0Sstevel@tonic-gate 	 * If the malloc fails we longjmp out to allow the code to Prelease()
113*0Sstevel@tonic-gate 	 * a stopped victim if needed.
114*0Sstevel@tonic-gate 	 */
115*0Sstevel@tonic-gate 	if ((p = malloc(size)) == NULL) {
116*0Sstevel@tonic-gate 		longjmp(env, errno);
117*0Sstevel@tonic-gate 	}
118*0Sstevel@tonic-gate 
119*0Sstevel@tonic-gate 	bzero(p, size);
120*0Sstevel@tonic-gate 	return (p);
121*0Sstevel@tonic-gate }
122*0Sstevel@tonic-gate 
123*0Sstevel@tonic-gate static char *
124*0Sstevel@tonic-gate safe_strdup(const char *s1)
125*0Sstevel@tonic-gate {
126*0Sstevel@tonic-gate 	char	*s2;
127*0Sstevel@tonic-gate 
128*0Sstevel@tonic-gate 	s2 = safe_zalloc(strlen(s1) + 1);
129*0Sstevel@tonic-gate 	(void) strcpy(s2, s1);
130*0Sstevel@tonic-gate 	return (s2);
131*0Sstevel@tonic-gate }
132*0Sstevel@tonic-gate 
133*0Sstevel@tonic-gate /*
134*0Sstevel@tonic-gate  * Given a wchar_t which might represent an 'escapable' sequence (see
135*0Sstevel@tonic-gate  * formats(5)), return the base ascii character needed to print that
136*0Sstevel@tonic-gate  * sequence.
137*0Sstevel@tonic-gate  *
138*0Sstevel@tonic-gate  * The comparisons performed may look suspect at first, but all are valid;
139*0Sstevel@tonic-gate  * the characters below all appear in the "Portable Character Set."  The
140*0Sstevel@tonic-gate  * Single Unix Spec says: "The wide-character value for each member of the
141*0Sstevel@tonic-gate  * Portable Character Set will equal its value when used as the lone
142*0Sstevel@tonic-gate  * character in an integer character constant."
143*0Sstevel@tonic-gate  */
144*0Sstevel@tonic-gate static uchar_t
145*0Sstevel@tonic-gate get_interp_char(wchar_t wc)
146*0Sstevel@tonic-gate {
147*0Sstevel@tonic-gate 	switch (wc) {
148*0Sstevel@tonic-gate 	case L'\a':
149*0Sstevel@tonic-gate 		return ('a');
150*0Sstevel@tonic-gate 	case L'\b':
151*0Sstevel@tonic-gate 		return ('b');
152*0Sstevel@tonic-gate 	case L'\f':
153*0Sstevel@tonic-gate 		return ('f');
154*0Sstevel@tonic-gate 	case L'\n':
155*0Sstevel@tonic-gate 		return ('n');
156*0Sstevel@tonic-gate 	case L'\r':
157*0Sstevel@tonic-gate 		return ('r');
158*0Sstevel@tonic-gate 	case L'\t':
159*0Sstevel@tonic-gate 		return ('t');
160*0Sstevel@tonic-gate 	case L'\v':
161*0Sstevel@tonic-gate 		return ('v');
162*0Sstevel@tonic-gate 	case L'\\':
163*0Sstevel@tonic-gate 		return ('\\');
164*0Sstevel@tonic-gate 	}
165*0Sstevel@tonic-gate 	return ('\0');
166*0Sstevel@tonic-gate }
167*0Sstevel@tonic-gate 
168*0Sstevel@tonic-gate static char *
169*0Sstevel@tonic-gate unctrl_str_strict_ascii(const char *src, int escape_slash, int *unprintable)
170*0Sstevel@tonic-gate {
171*0Sstevel@tonic-gate 	uchar_t *uc, *ucp, c, ic;
172*0Sstevel@tonic-gate 	uc = ucp = safe_zalloc((strlen(src) * 4) + 1);
173*0Sstevel@tonic-gate 	while ((c = *src++) != '\0') {
174*0Sstevel@tonic-gate 		/*
175*0Sstevel@tonic-gate 		 * Call get_interp_char *first*, since \ will otherwise not
176*0Sstevel@tonic-gate 		 * be escaped as \\.
177*0Sstevel@tonic-gate 		 */
178*0Sstevel@tonic-gate 		if ((ic = get_interp_char((wchar_t)c)) != '\0') {
179*0Sstevel@tonic-gate 			if (escape_slash || ic != '\\')
180*0Sstevel@tonic-gate 				*ucp++ = '\\';
181*0Sstevel@tonic-gate 			*ucp++ = ic;
182*0Sstevel@tonic-gate 		} else if (isascii(c) && isprint(c)) {
183*0Sstevel@tonic-gate 			*ucp++ = c;
184*0Sstevel@tonic-gate 		} else {
185*0Sstevel@tonic-gate 			*ucp++ = '\\';
186*0Sstevel@tonic-gate 			*ucp++ = ((c >> 6) & 7) + '0';
187*0Sstevel@tonic-gate 			*ucp++ = ((c >> 3) & 7) + '0';
188*0Sstevel@tonic-gate 			*ucp++ = (c & 7) + '0';
189*0Sstevel@tonic-gate 			*unprintable = 1;
190*0Sstevel@tonic-gate 		}
191*0Sstevel@tonic-gate 	}
192*0Sstevel@tonic-gate 	*ucp = '\0';
193*0Sstevel@tonic-gate 	return ((char *)uc);
194*0Sstevel@tonic-gate }
195*0Sstevel@tonic-gate 
196*0Sstevel@tonic-gate /*
197*0Sstevel@tonic-gate  * Convert control characters as described in format(5) to their readable
198*0Sstevel@tonic-gate  * representation; special care is taken to handle multibyte character sets.
199*0Sstevel@tonic-gate  *
200*0Sstevel@tonic-gate  * If escape_slash is true, escaping of '\' occurs.  The first time a string
201*0Sstevel@tonic-gate  * is unctrl'd, this should be '1'.  Subsequent iterations over the same
202*0Sstevel@tonic-gate  * string should set escape_slash to 0.  Otherwise you'll wind up with
203*0Sstevel@tonic-gate  * \ --> \\ --> \\\\.
204*0Sstevel@tonic-gate  */
205*0Sstevel@tonic-gate static char *
206*0Sstevel@tonic-gate unctrl_str(const char *src, int escape_slash, int *unprintable)
207*0Sstevel@tonic-gate {
208*0Sstevel@tonic-gate 	wchar_t wc;
209*0Sstevel@tonic-gate 	wchar_t *wide_src, *wide_srcp;
210*0Sstevel@tonic-gate 	wchar_t *wide_dest, *wide_destp;
211*0Sstevel@tonic-gate 	char *uc;
212*0Sstevel@tonic-gate 	size_t srcbufsz = strlen(src) + 1;
213*0Sstevel@tonic-gate 	size_t destbufsz = srcbufsz * 4;
214*0Sstevel@tonic-gate 	size_t srclen, destlen;
215*0Sstevel@tonic-gate 
216*0Sstevel@tonic-gate 	wide_srcp = wide_src = safe_zalloc(srcbufsz * sizeof (wchar_t));
217*0Sstevel@tonic-gate 	wide_destp = wide_dest = safe_zalloc(destbufsz * sizeof (wchar_t));
218*0Sstevel@tonic-gate 
219*0Sstevel@tonic-gate 	if ((srclen = mbstowcs(wide_src, src, srcbufsz - 1)) == (size_t)-1) {
220*0Sstevel@tonic-gate 		/*
221*0Sstevel@tonic-gate 		 * We can't trust the string, since in the locale in which
222*0Sstevel@tonic-gate 		 * this call is operating, the string contains an invalid
223*0Sstevel@tonic-gate 		 * multibyte sequence.  There isn't much to do here, so
224*0Sstevel@tonic-gate 		 * convert the string byte by byte to wide characters, as
225*0Sstevel@tonic-gate 		 * if it came from a C locale (char) string.  This isn't
226*0Sstevel@tonic-gate 		 * perfect, but at least the characters will make it to
227*0Sstevel@tonic-gate 		 * the screen.
228*0Sstevel@tonic-gate 		 */
229*0Sstevel@tonic-gate 		free(wide_src);
230*0Sstevel@tonic-gate 		free(wide_dest);
231*0Sstevel@tonic-gate 		return (unctrl_str_strict_ascii(src, escape_slash,
232*0Sstevel@tonic-gate 		    unprintable));
233*0Sstevel@tonic-gate 	}
234*0Sstevel@tonic-gate 	if (srclen == (srcbufsz - 1)) {
235*0Sstevel@tonic-gate 		wide_src[srclen] = L'\0';
236*0Sstevel@tonic-gate 	}
237*0Sstevel@tonic-gate 
238*0Sstevel@tonic-gate 	while ((wc = *wide_srcp++) != L'\0') {
239*0Sstevel@tonic-gate 		char cvt_buf[MB_LEN_MAX];
240*0Sstevel@tonic-gate 		int len, i;
241*0Sstevel@tonic-gate 		char c = get_interp_char(wc);
242*0Sstevel@tonic-gate 
243*0Sstevel@tonic-gate 		if ((c != '\0') && (escape_slash || c != '\\')) {
244*0Sstevel@tonic-gate 			/*
245*0Sstevel@tonic-gate 			 * Print "interpreted version" (\n, \a, etc).
246*0Sstevel@tonic-gate 			 */
247*0Sstevel@tonic-gate 			*wide_destp++ = L'\\';
248*0Sstevel@tonic-gate 			*wide_destp++ = (wchar_t)c;
249*0Sstevel@tonic-gate 			continue;
250*0Sstevel@tonic-gate 		}
251*0Sstevel@tonic-gate 
252*0Sstevel@tonic-gate 		if (iswprint(wc)) {
253*0Sstevel@tonic-gate 			*wide_destp++ = wc;
254*0Sstevel@tonic-gate 			continue;
255*0Sstevel@tonic-gate 		}
256*0Sstevel@tonic-gate 
257*0Sstevel@tonic-gate 		/*
258*0Sstevel@tonic-gate 		 * Convert the wide char back into (potentially several)
259*0Sstevel@tonic-gate 		 * multibyte characters, then escape out each of those bytes.
260*0Sstevel@tonic-gate 		 */
261*0Sstevel@tonic-gate 		bzero(cvt_buf, sizeof (cvt_buf));
262*0Sstevel@tonic-gate 		if ((len = wctomb(cvt_buf, wc)) == -1) {
263*0Sstevel@tonic-gate 			/*
264*0Sstevel@tonic-gate 			 * This is a totally invalid wide char; discard it.
265*0Sstevel@tonic-gate 			 */
266*0Sstevel@tonic-gate 			continue;
267*0Sstevel@tonic-gate 		}
268*0Sstevel@tonic-gate 		for (i = 0; i < len; i++) {
269*0Sstevel@tonic-gate 			uchar_t c = cvt_buf[i];
270*0Sstevel@tonic-gate 			*wide_destp++ = L'\\';
271*0Sstevel@tonic-gate 			*wide_destp++ = (wchar_t)('0' + ((c >> 6) & 7));
272*0Sstevel@tonic-gate 			*wide_destp++ = (wchar_t)('0' + ((c >> 3) & 7));
273*0Sstevel@tonic-gate 			*wide_destp++ = (wchar_t)('0' + (c & 7));
274*0Sstevel@tonic-gate 			*unprintable = 1;
275*0Sstevel@tonic-gate 		}
276*0Sstevel@tonic-gate 	}
277*0Sstevel@tonic-gate 
278*0Sstevel@tonic-gate 	*wide_destp = '\0';
279*0Sstevel@tonic-gate 	destlen = (wide_destp - wide_dest) * MB_CUR_MAX + 1;
280*0Sstevel@tonic-gate 	uc = safe_zalloc(destlen);
281*0Sstevel@tonic-gate 	if (wcstombs(uc, wide_dest, destlen) == (size_t)-1) {
282*0Sstevel@tonic-gate 		/* If we've gotten this far, wcstombs shouldn't fail... */
283*0Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: wcstombs failed unexpectedly: %s\n",
284*0Sstevel@tonic-gate 		    command, strerror(errno));
285*0Sstevel@tonic-gate 		exit(1);
286*0Sstevel@tonic-gate 	} else {
287*0Sstevel@tonic-gate 		char *tmp;
288*0Sstevel@tonic-gate 		/*
289*0Sstevel@tonic-gate 		 * Try to save memory; don't waste 3 * strlen in the
290*0Sstevel@tonic-gate 		 * common case.
291*0Sstevel@tonic-gate 		 */
292*0Sstevel@tonic-gate 		tmp = safe_strdup(uc);
293*0Sstevel@tonic-gate 		free(uc);
294*0Sstevel@tonic-gate 		uc = tmp;
295*0Sstevel@tonic-gate 	}
296*0Sstevel@tonic-gate 	free(wide_dest);
297*0Sstevel@tonic-gate 	free(wide_src);
298*0Sstevel@tonic-gate 	return (uc);
299*0Sstevel@tonic-gate }
300*0Sstevel@tonic-gate 
301*0Sstevel@tonic-gate /*
302*0Sstevel@tonic-gate  * These functions determine which characters are safe to be left unquoted.
303*0Sstevel@tonic-gate  * Rather than starting with every printable character and subtracting out the
304*0Sstevel@tonic-gate  * shell metacharacters, we take the more conservative approach of starting with
305*0Sstevel@tonic-gate  * a set of safe characters and adding those few common punctuation characters
306*0Sstevel@tonic-gate  * which are known to be safe.  The rules are:
307*0Sstevel@tonic-gate  *
308*0Sstevel@tonic-gate  * 	If this is a printable character (graph), and not punctuation, it is
309*0Sstevel@tonic-gate  * 	safe to leave unquoted.
310*0Sstevel@tonic-gate  *
311*0Sstevel@tonic-gate  * 	If it's one of known hard-coded safe characters, it's also safe to leave
312*0Sstevel@tonic-gate  * 	unquoted.
313*0Sstevel@tonic-gate  *
314*0Sstevel@tonic-gate  * 	Otherwise, the entire argument must be quoted.
315*0Sstevel@tonic-gate  *
316*0Sstevel@tonic-gate  * This will cause some strings to be unecessarily quoted, but it is safer than
317*0Sstevel@tonic-gate  * having a character unintentionally interpreted by the shell.
318*0Sstevel@tonic-gate  */
319*0Sstevel@tonic-gate static int
320*0Sstevel@tonic-gate issafe_ascii(char c)
321*0Sstevel@tonic-gate {
322*0Sstevel@tonic-gate 	return (isalnum(c) || strchr("_.-/@:,", c) != NULL);
323*0Sstevel@tonic-gate }
324*0Sstevel@tonic-gate 
325*0Sstevel@tonic-gate static int
326*0Sstevel@tonic-gate issafe(wchar_t wc)
327*0Sstevel@tonic-gate {
328*0Sstevel@tonic-gate 	return ((iswgraph(wc) && !iswpunct(wc)) ||
329*0Sstevel@tonic-gate 	    wschr(L"_.-/@:,", wc) != NULL);
330*0Sstevel@tonic-gate }
331*0Sstevel@tonic-gate 
332*0Sstevel@tonic-gate /*ARGSUSED*/
333*0Sstevel@tonic-gate static char *
334*0Sstevel@tonic-gate quote_string_ascii(pargs_data_t *datap, char *src)
335*0Sstevel@tonic-gate {
336*0Sstevel@tonic-gate 	char *dst;
337*0Sstevel@tonic-gate 	int quote_count = 0;
338*0Sstevel@tonic-gate 	int need_quote = 0;
339*0Sstevel@tonic-gate 	char *srcp, *dstp;
340*0Sstevel@tonic-gate 	size_t dstlen;
341*0Sstevel@tonic-gate 
342*0Sstevel@tonic-gate 	for (srcp = src; *srcp != '\0'; srcp++) {
343*0Sstevel@tonic-gate 		if (!issafe_ascii(*srcp)) {
344*0Sstevel@tonic-gate 			need_quote = 1;
345*0Sstevel@tonic-gate 			if (*srcp == '\'')
346*0Sstevel@tonic-gate 				quote_count++;
347*0Sstevel@tonic-gate 		}
348*0Sstevel@tonic-gate 	}
349*0Sstevel@tonic-gate 
350*0Sstevel@tonic-gate 	if (!need_quote)
351*0Sstevel@tonic-gate 		return (src);
352*0Sstevel@tonic-gate 
353*0Sstevel@tonic-gate 	/*
354*0Sstevel@tonic-gate 	 * The only character we care about here is a single quote.  All the
355*0Sstevel@tonic-gate 	 * other unprintable characters (and backslashes) will have been dealt
356*0Sstevel@tonic-gate 	 * with by unctrl_str().  We make the following subtitution when we
357*0Sstevel@tonic-gate 	 * encounter a single quote:
358*0Sstevel@tonic-gate 	 *
359*0Sstevel@tonic-gate 	 * 	' = '"'"'
360*0Sstevel@tonic-gate 	 *
361*0Sstevel@tonic-gate 	 * In addition, we put single quotes around the entire argument.  For
362*0Sstevel@tonic-gate 	 * example:
363*0Sstevel@tonic-gate 	 *
364*0Sstevel@tonic-gate 	 * 	foo'bar = 'foo'"'"'bar'
365*0Sstevel@tonic-gate 	 */
366*0Sstevel@tonic-gate 	dstlen = strlen(src) + 3 + 4 * quote_count;
367*0Sstevel@tonic-gate 	dst = safe_zalloc(dstlen);
368*0Sstevel@tonic-gate 
369*0Sstevel@tonic-gate 	dstp = dst;
370*0Sstevel@tonic-gate 	*dstp++ = '\'';
371*0Sstevel@tonic-gate 	for (srcp = src; *srcp != '\0'; srcp++, dstp++) {
372*0Sstevel@tonic-gate 		*dstp = *srcp;
373*0Sstevel@tonic-gate 
374*0Sstevel@tonic-gate 		if (*srcp == '\'') {
375*0Sstevel@tonic-gate 			dstp[1] = '"';
376*0Sstevel@tonic-gate 			dstp[2] = '\'';
377*0Sstevel@tonic-gate 			dstp[3] = '"';
378*0Sstevel@tonic-gate 			dstp[4] = '\'';
379*0Sstevel@tonic-gate 			dstp += 4;
380*0Sstevel@tonic-gate 		}
381*0Sstevel@tonic-gate 	}
382*0Sstevel@tonic-gate 	*dstp++ = '\'';
383*0Sstevel@tonic-gate 	*dstp = '\0';
384*0Sstevel@tonic-gate 
385*0Sstevel@tonic-gate 	free(src);
386*0Sstevel@tonic-gate 
387*0Sstevel@tonic-gate 	return (dst);
388*0Sstevel@tonic-gate }
389*0Sstevel@tonic-gate 
390*0Sstevel@tonic-gate static char *
391*0Sstevel@tonic-gate quote_string(pargs_data_t *datap, char *src)
392*0Sstevel@tonic-gate {
393*0Sstevel@tonic-gate 	wchar_t *wide_src, *wide_srcp;
394*0Sstevel@tonic-gate 	wchar_t *wide_dest, *wide_destp;
395*0Sstevel@tonic-gate 	char *uc;
396*0Sstevel@tonic-gate 	size_t srcbufsz = strlen(src) + 1;
397*0Sstevel@tonic-gate 	size_t srclen;
398*0Sstevel@tonic-gate 	size_t destbufsz;
399*0Sstevel@tonic-gate 	size_t destlen;
400*0Sstevel@tonic-gate 	int quote_count = 0;
401*0Sstevel@tonic-gate 	int need_quote = 0;
402*0Sstevel@tonic-gate 
403*0Sstevel@tonic-gate 	if (datap->pd_conv_flags & CONV_STRICT_ASCII)
404*0Sstevel@tonic-gate 		return (quote_string_ascii(datap, src));
405*0Sstevel@tonic-gate 
406*0Sstevel@tonic-gate 	wide_srcp = wide_src = safe_zalloc(srcbufsz * sizeof (wchar_t));
407*0Sstevel@tonic-gate 
408*0Sstevel@tonic-gate 	if ((srclen = mbstowcs(wide_src, src, srcbufsz - 1)) == (size_t)-1) {
409*0Sstevel@tonic-gate 		free(wide_src);
410*0Sstevel@tonic-gate 		return (quote_string_ascii(datap, src));
411*0Sstevel@tonic-gate 	}
412*0Sstevel@tonic-gate 
413*0Sstevel@tonic-gate 	if (srclen == srcbufsz - 1)
414*0Sstevel@tonic-gate 		wide_src[srclen] = L'\0';
415*0Sstevel@tonic-gate 
416*0Sstevel@tonic-gate 	for (wide_srcp = wide_src; *wide_srcp != '\0'; wide_srcp++) {
417*0Sstevel@tonic-gate 		if (!issafe(*wide_srcp)) {
418*0Sstevel@tonic-gate 			need_quote = 1;
419*0Sstevel@tonic-gate 			if (*wide_srcp == L'\'')
420*0Sstevel@tonic-gate 				quote_count++;
421*0Sstevel@tonic-gate 		}
422*0Sstevel@tonic-gate 	}
423*0Sstevel@tonic-gate 
424*0Sstevel@tonic-gate 	if (!need_quote) {
425*0Sstevel@tonic-gate 		free(wide_src);
426*0Sstevel@tonic-gate 		return (src);
427*0Sstevel@tonic-gate 	}
428*0Sstevel@tonic-gate 
429*0Sstevel@tonic-gate 	/*
430*0Sstevel@tonic-gate 	 * See comment for quote_string_ascii(), above.
431*0Sstevel@tonic-gate 	 */
432*0Sstevel@tonic-gate 	destbufsz = srcbufsz + 3 + 4 * quote_count;
433*0Sstevel@tonic-gate 	wide_destp = wide_dest = safe_zalloc(destbufsz * sizeof (wchar_t));
434*0Sstevel@tonic-gate 
435*0Sstevel@tonic-gate 	*wide_destp++ = L'\'';
436*0Sstevel@tonic-gate 	for (wide_srcp = wide_src; *wide_srcp != L'\0';
437*0Sstevel@tonic-gate 	    wide_srcp++, wide_destp++) {
438*0Sstevel@tonic-gate 		*wide_destp = *wide_srcp;
439*0Sstevel@tonic-gate 
440*0Sstevel@tonic-gate 		if (*wide_srcp == L'\'') {
441*0Sstevel@tonic-gate 			wide_destp[1] = L'"';
442*0Sstevel@tonic-gate 			wide_destp[2] = L'\'';
443*0Sstevel@tonic-gate 			wide_destp[3] = L'"';
444*0Sstevel@tonic-gate 			wide_destp[4] = L'\'';
445*0Sstevel@tonic-gate 			wide_destp += 4;
446*0Sstevel@tonic-gate 		}
447*0Sstevel@tonic-gate 	}
448*0Sstevel@tonic-gate 	*wide_destp++ = L'\'';
449*0Sstevel@tonic-gate 	*wide_destp = L'\0';
450*0Sstevel@tonic-gate 
451*0Sstevel@tonic-gate 	destlen = destbufsz * MB_CUR_MAX + 1;
452*0Sstevel@tonic-gate 	uc = safe_zalloc(destlen);
453*0Sstevel@tonic-gate 	if (wcstombs(uc, wide_dest, destlen) == (size_t)-1) {
454*0Sstevel@tonic-gate 		/* If we've gotten this far, wcstombs shouldn't fail... */
455*0Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: wcstombs failed unexpectedly: %s\n",
456*0Sstevel@tonic-gate 		    command, strerror(errno));
457*0Sstevel@tonic-gate 		exit(1);
458*0Sstevel@tonic-gate 	}
459*0Sstevel@tonic-gate 
460*0Sstevel@tonic-gate 	free(wide_dest);
461*0Sstevel@tonic-gate 	free(wide_src);
462*0Sstevel@tonic-gate 
463*0Sstevel@tonic-gate 	return (uc);
464*0Sstevel@tonic-gate }
465*0Sstevel@tonic-gate 
466*0Sstevel@tonic-gate 
467*0Sstevel@tonic-gate /*
468*0Sstevel@tonic-gate  * Determine the locale of the target process by traversing its environment,
469*0Sstevel@tonic-gate  * making only one pass for efficiency's sake; stash the result in
470*0Sstevel@tonic-gate  * datap->pd_locale.
471*0Sstevel@tonic-gate  *
472*0Sstevel@tonic-gate  * It's possible that the process has called setlocale() to change its
473*0Sstevel@tonic-gate  * locale to something different, but we mostly care about making a good
474*0Sstevel@tonic-gate  * guess as to the locale at exec(2) time.
475*0Sstevel@tonic-gate  */
476*0Sstevel@tonic-gate static void
477*0Sstevel@tonic-gate lookup_locale(pargs_data_t *datap)
478*0Sstevel@tonic-gate {
479*0Sstevel@tonic-gate 	int i, j, composite = 0;
480*0Sstevel@tonic-gate 	size_t	len = 0;
481*0Sstevel@tonic-gate 	char	*pd_locale;
482*0Sstevel@tonic-gate 	char	*lc_all = NULL, *lang = NULL;
483*0Sstevel@tonic-gate 	char	*lcs[] = { NULL, NULL, NULL, NULL, NULL, NULL };
484*0Sstevel@tonic-gate 	static const char *cat_names[] = {
485*0Sstevel@tonic-gate 		"LC_CTYPE=",	"LC_NUMERIC=",	"LC_TIME=",
486*0Sstevel@tonic-gate 		"LC_COLLATE=",	"LC_MONETARY=",	"LC_MESSAGES="
487*0Sstevel@tonic-gate 	};
488*0Sstevel@tonic-gate 
489*0Sstevel@tonic-gate 	for (i = 0; i < datap->pd_envc; i++) {
490*0Sstevel@tonic-gate 		char *s = datap->pd_envp_strs[i];
491*0Sstevel@tonic-gate 
492*0Sstevel@tonic-gate 		if (s == NULL)
493*0Sstevel@tonic-gate 			continue;
494*0Sstevel@tonic-gate 
495*0Sstevel@tonic-gate 		if (strncmp("LC_ALL=", s, strlen("LC_ALL=")) == 0) {
496*0Sstevel@tonic-gate 			/*
497*0Sstevel@tonic-gate 			 * Minor optimization-- if we find LC_ALL we're done.
498*0Sstevel@tonic-gate 			 */
499*0Sstevel@tonic-gate 			lc_all = s + strlen("LC_ALL=");
500*0Sstevel@tonic-gate 			break;
501*0Sstevel@tonic-gate 		}
502*0Sstevel@tonic-gate 		for (j = 0; j <= _LastCategory; j++) {
503*0Sstevel@tonic-gate 			if (strncmp(cat_names[j], s,
504*0Sstevel@tonic-gate 			    strlen(cat_names[j])) == 0) {
505*0Sstevel@tonic-gate 				lcs[j] = s + strlen(cat_names[j]);
506*0Sstevel@tonic-gate 			}
507*0Sstevel@tonic-gate 		}
508*0Sstevel@tonic-gate 		if (strncmp("LANG=", s, strlen("LANG=")) == 0) {
509*0Sstevel@tonic-gate 			lang = s + strlen("LANG=");
510*0Sstevel@tonic-gate 		}
511*0Sstevel@tonic-gate 	}
512*0Sstevel@tonic-gate 
513*0Sstevel@tonic-gate 	if (lc_all && (*lc_all == '\0'))
514*0Sstevel@tonic-gate 		lc_all = NULL;
515*0Sstevel@tonic-gate 	if (lang && (*lang == '\0'))
516*0Sstevel@tonic-gate 		lang = NULL;
517*0Sstevel@tonic-gate 
518*0Sstevel@tonic-gate 	for (i = 0; i <= _LastCategory; i++) {
519*0Sstevel@tonic-gate 		if (lc_all != NULL) {
520*0Sstevel@tonic-gate 			lcs[i] = lc_all;
521*0Sstevel@tonic-gate 		} else if (lcs[i] != NULL) {
522*0Sstevel@tonic-gate 			lcs[i] = lcs[i];
523*0Sstevel@tonic-gate 		} else if (lang != NULL) {
524*0Sstevel@tonic-gate 			lcs[i] = lang;
525*0Sstevel@tonic-gate 		} else {
526*0Sstevel@tonic-gate 			lcs[i] = "C";
527*0Sstevel@tonic-gate 		}
528*0Sstevel@tonic-gate 		if ((i > 0) && (lcs[i] != lcs[i-1]))
529*0Sstevel@tonic-gate 			composite++;
530*0Sstevel@tonic-gate 
531*0Sstevel@tonic-gate 		len += 1 + strlen(lcs[i]);	/* 1 extra byte for '/' */
532*0Sstevel@tonic-gate 	}
533*0Sstevel@tonic-gate 
534*0Sstevel@tonic-gate 	if (composite == 0) {
535*0Sstevel@tonic-gate 		/* simple locale */
536*0Sstevel@tonic-gate 		pd_locale = safe_strdup(lcs[0]);
537*0Sstevel@tonic-gate 	} else {
538*0Sstevel@tonic-gate 		/* composite locale */
539*0Sstevel@tonic-gate 		pd_locale = safe_zalloc(len + 1);
540*0Sstevel@tonic-gate 		(void) snprintf(pd_locale, len + 1, "/%s/%s/%s/%s/%s/%s",
541*0Sstevel@tonic-gate 		    lcs[0], lcs[1], lcs[2], lcs[3], lcs[4], lcs[5]);
542*0Sstevel@tonic-gate 	}
543*0Sstevel@tonic-gate 	datap->pd_locale = pd_locale;
544*0Sstevel@tonic-gate }
545*0Sstevel@tonic-gate 
546*0Sstevel@tonic-gate /*
547*0Sstevel@tonic-gate  * Pull a string from the victim, regardless of size; this routine allocates
548*0Sstevel@tonic-gate  * memory for the string which must be freed by the caller.
549*0Sstevel@tonic-gate  */
550*0Sstevel@tonic-gate static char *
551*0Sstevel@tonic-gate extract_string(pargs_data_t *datap, uintptr_t addr)
552*0Sstevel@tonic-gate {
553*0Sstevel@tonic-gate 	int size = EXTRACT_BUFSZ;
554*0Sstevel@tonic-gate 	char *result;
555*0Sstevel@tonic-gate 
556*0Sstevel@tonic-gate 	result = safe_zalloc(size);
557*0Sstevel@tonic-gate 
558*0Sstevel@tonic-gate 	for (;;) {
559*0Sstevel@tonic-gate 		if (Pread_string(datap->pd_proc, result, size, addr) < 0) {
560*0Sstevel@tonic-gate 			free(result);
561*0Sstevel@tonic-gate 			return (NULL);
562*0Sstevel@tonic-gate 		} else if (strlen(result) == (size - 1)) {
563*0Sstevel@tonic-gate 			free(result);
564*0Sstevel@tonic-gate 			size *= 2;
565*0Sstevel@tonic-gate 			result = safe_zalloc(size);
566*0Sstevel@tonic-gate 		} else {
567*0Sstevel@tonic-gate 			break;
568*0Sstevel@tonic-gate 		}
569*0Sstevel@tonic-gate 	}
570*0Sstevel@tonic-gate 	return (result);
571*0Sstevel@tonic-gate }
572*0Sstevel@tonic-gate 
573*0Sstevel@tonic-gate /*
574*0Sstevel@tonic-gate  * Utility function to read an array of pointers from the victim, adjusting
575*0Sstevel@tonic-gate  * for victim data model; returns the number of bytes successfully read.
576*0Sstevel@tonic-gate  */
577*0Sstevel@tonic-gate static ssize_t
578*0Sstevel@tonic-gate read_ptr_array(pargs_data_t *datap, uintptr_t offset, uintptr_t *buf,
579*0Sstevel@tonic-gate     size_t nelems)
580*0Sstevel@tonic-gate {
581*0Sstevel@tonic-gate 	ssize_t res;
582*0Sstevel@tonic-gate 
583*0Sstevel@tonic-gate 	if (dmodel == PR_MODEL_NATIVE) {
584*0Sstevel@tonic-gate 		res = Pread(datap->pd_proc, buf, nelems * sizeof (uintptr_t),
585*0Sstevel@tonic-gate 		    offset);
586*0Sstevel@tonic-gate 	} else {
587*0Sstevel@tonic-gate 		int i;
588*0Sstevel@tonic-gate 		uint32_t *arr32 = safe_zalloc(nelems * sizeof (uint32_t));
589*0Sstevel@tonic-gate 
590*0Sstevel@tonic-gate 		res = Pread(datap->pd_proc, arr32, nelems * sizeof (uint32_t),
591*0Sstevel@tonic-gate 		    offset);
592*0Sstevel@tonic-gate 		if (res > 0) {
593*0Sstevel@tonic-gate 			for (i = 0; i < nelems; i++)
594*0Sstevel@tonic-gate 				buf[i] = arr32[i];
595*0Sstevel@tonic-gate 		}
596*0Sstevel@tonic-gate 		free(arr32);
597*0Sstevel@tonic-gate 	}
598*0Sstevel@tonic-gate 	return (res);
599*0Sstevel@tonic-gate }
600*0Sstevel@tonic-gate 
601*0Sstevel@tonic-gate /*
602*0Sstevel@tonic-gate  * Extract the argv array from the victim; store the pointer values in
603*0Sstevel@tonic-gate  * datap->pd_argv and the extracted strings in datap->pd_argv_strs.
604*0Sstevel@tonic-gate  */
605*0Sstevel@tonic-gate static void
606*0Sstevel@tonic-gate get_args(pargs_data_t *datap)
607*0Sstevel@tonic-gate {
608*0Sstevel@tonic-gate 	size_t argc = datap->pd_psinfo->pr_argc;
609*0Sstevel@tonic-gate 	uintptr_t argvoff = datap->pd_psinfo->pr_argv;
610*0Sstevel@tonic-gate 	int i;
611*0Sstevel@tonic-gate 
612*0Sstevel@tonic-gate 	datap->pd_argc = argc;
613*0Sstevel@tonic-gate 	datap->pd_argv = safe_zalloc(argc * sizeof (uintptr_t));
614*0Sstevel@tonic-gate 
615*0Sstevel@tonic-gate 	if (read_ptr_array(datap, argvoff, datap->pd_argv, argc) <= 0) {
616*0Sstevel@tonic-gate 		free(datap->pd_argv);
617*0Sstevel@tonic-gate 		datap->pd_argv = NULL;
618*0Sstevel@tonic-gate 		return;
619*0Sstevel@tonic-gate 	}
620*0Sstevel@tonic-gate 
621*0Sstevel@tonic-gate 	datap->pd_argv_strs = safe_zalloc(argc * sizeof (char *));
622*0Sstevel@tonic-gate 	for (i = 0; i < argc; i++) {
623*0Sstevel@tonic-gate 		if (datap->pd_argv[i] == 0)
624*0Sstevel@tonic-gate 			continue;
625*0Sstevel@tonic-gate 		datap->pd_argv_strs[i] = extract_string(datap,
626*0Sstevel@tonic-gate 		    datap->pd_argv[i]);
627*0Sstevel@tonic-gate 	}
628*0Sstevel@tonic-gate }
629*0Sstevel@tonic-gate 
630*0Sstevel@tonic-gate /*ARGSUSED*/
631*0Sstevel@tonic-gate static int
632*0Sstevel@tonic-gate build_env(void *data, struct ps_prochandle *pr, uintptr_t addr, const char *str)
633*0Sstevel@tonic-gate {
634*0Sstevel@tonic-gate 	pargs_data_t *datap = data;
635*0Sstevel@tonic-gate 
636*0Sstevel@tonic-gate 	if (datap->pd_envp != NULL) {
637*0Sstevel@tonic-gate 		datap->pd_envp[datap->pd_envc] = addr;
638*0Sstevel@tonic-gate 		if (str == NULL)
639*0Sstevel@tonic-gate 			datap->pd_envp_strs[datap->pd_envc] = NULL;
640*0Sstevel@tonic-gate 		else
641*0Sstevel@tonic-gate 			datap->pd_envp_strs[datap->pd_envc] = strdup(str);
642*0Sstevel@tonic-gate 	}
643*0Sstevel@tonic-gate 
644*0Sstevel@tonic-gate 	datap->pd_envc++;
645*0Sstevel@tonic-gate 
646*0Sstevel@tonic-gate 	return (0);
647*0Sstevel@tonic-gate }
648*0Sstevel@tonic-gate 
649*0Sstevel@tonic-gate static void
650*0Sstevel@tonic-gate get_env(pargs_data_t *datap)
651*0Sstevel@tonic-gate {
652*0Sstevel@tonic-gate 	struct ps_prochandle *pr = datap->pd_proc;
653*0Sstevel@tonic-gate 
654*0Sstevel@tonic-gate 	datap->pd_envc = 0;
655*0Sstevel@tonic-gate 	(void) Penv_iter(pr, build_env, datap);
656*0Sstevel@tonic-gate 
657*0Sstevel@tonic-gate 	datap->pd_envp = safe_zalloc(sizeof (uintptr_t) * datap->pd_envc);
658*0Sstevel@tonic-gate 	datap->pd_envp_strs = safe_zalloc(sizeof (char *) * datap->pd_envc);
659*0Sstevel@tonic-gate 
660*0Sstevel@tonic-gate 	datap->pd_envc = 0;
661*0Sstevel@tonic-gate 	(void) Penv_iter(pr, build_env, datap);
662*0Sstevel@tonic-gate }
663*0Sstevel@tonic-gate 
664*0Sstevel@tonic-gate /*
665*0Sstevel@tonic-gate  * The following at_* routines are used to decode data from the aux vector.
666*0Sstevel@tonic-gate  */
667*0Sstevel@tonic-gate 
668*0Sstevel@tonic-gate /*ARGSUSED*/
669*0Sstevel@tonic-gate static void
670*0Sstevel@tonic-gate at_null(long val, char *instr, size_t n, char *str)
671*0Sstevel@tonic-gate {
672*0Sstevel@tonic-gate 	str[0] = '\0';
673*0Sstevel@tonic-gate }
674*0Sstevel@tonic-gate 
675*0Sstevel@tonic-gate /*ARGSUSED*/
676*0Sstevel@tonic-gate static void
677*0Sstevel@tonic-gate at_str(long val, char *instr, size_t n, char *str)
678*0Sstevel@tonic-gate {
679*0Sstevel@tonic-gate 	str[0] = '\0';
680*0Sstevel@tonic-gate 	if (instr != NULL) {
681*0Sstevel@tonic-gate 		(void) strlcpy(str, instr, n);
682*0Sstevel@tonic-gate 	}
683*0Sstevel@tonic-gate }
684*0Sstevel@tonic-gate 
685*0Sstevel@tonic-gate /*
686*0Sstevel@tonic-gate  * Note: Don't forget to add a corresponding case to isainfo(1).
687*0Sstevel@tonic-gate  */
688*0Sstevel@tonic-gate 
689*0Sstevel@tonic-gate #define	FMT_AV(s, n, hwcap, mask, name)				\
690*0Sstevel@tonic-gate 	if ((hwcap) & (mask)) 					\
691*0Sstevel@tonic-gate 		(void) snprintf(s, n, "%s" name " | ", s)
692*0Sstevel@tonic-gate 
693*0Sstevel@tonic-gate /*ARGSUSED*/
694*0Sstevel@tonic-gate static void
695*0Sstevel@tonic-gate at_hwcap(long val, char *instr, size_t n, char *str)
696*0Sstevel@tonic-gate {
697*0Sstevel@tonic-gate #if defined(__sparc) || defined(__sparcv9)
698*0Sstevel@tonic-gate 	(void) hwcap_1_val2str(val, str, n, CAP_FMT_PIPSPACE, EM_SPARC);
699*0Sstevel@tonic-gate 
700*0Sstevel@tonic-gate #elif defined(__i386) || defined(__amd64)
701*0Sstevel@tonic-gate 	(void) hwcap_1_val2str(val, str, n, CAP_FMT_PIPSPACE, EM_386);
702*0Sstevel@tonic-gate #else
703*0Sstevel@tonic-gate #error	"port me"
704*0Sstevel@tonic-gate #endif
705*0Sstevel@tonic-gate }
706*0Sstevel@tonic-gate 
707*0Sstevel@tonic-gate /*ARGSUSED*/
708*0Sstevel@tonic-gate static void
709*0Sstevel@tonic-gate at_uid(long val, char *instr, size_t n, char *str)
710*0Sstevel@tonic-gate {
711*0Sstevel@tonic-gate 	struct passwd *pw = getpwuid((uid_t)val);
712*0Sstevel@tonic-gate 
713*0Sstevel@tonic-gate 	if ((pw == NULL) || (pw->pw_name == NULL))
714*0Sstevel@tonic-gate 		str[0] = '\0';
715*0Sstevel@tonic-gate 	else
716*0Sstevel@tonic-gate 		(void) snprintf(str, n, "%lu(%s)", val, pw->pw_name);
717*0Sstevel@tonic-gate }
718*0Sstevel@tonic-gate 
719*0Sstevel@tonic-gate 
720*0Sstevel@tonic-gate /*ARGSUSED*/
721*0Sstevel@tonic-gate static void
722*0Sstevel@tonic-gate at_gid(long val, char *instr, size_t n, char *str)
723*0Sstevel@tonic-gate {
724*0Sstevel@tonic-gate 	struct group *gr = getgrgid((gid_t)val);
725*0Sstevel@tonic-gate 
726*0Sstevel@tonic-gate 	if ((gr == NULL) || (gr->gr_name == NULL))
727*0Sstevel@tonic-gate 		str[0] = '\0';
728*0Sstevel@tonic-gate 	else
729*0Sstevel@tonic-gate 		(void) snprintf(str, n, "%lu(%s)", val, gr->gr_name);
730*0Sstevel@tonic-gate }
731*0Sstevel@tonic-gate 
732*0Sstevel@tonic-gate static struct auxfl {
733*0Sstevel@tonic-gate 	int af_flag;
734*0Sstevel@tonic-gate 	const char *af_name;
735*0Sstevel@tonic-gate } auxfl[] = {
736*0Sstevel@tonic-gate 	{ AF_SUN_SETUGID,	"setugid" },
737*0Sstevel@tonic-gate };
738*0Sstevel@tonic-gate 
739*0Sstevel@tonic-gate /*ARGSUSED*/
740*0Sstevel@tonic-gate static void
741*0Sstevel@tonic-gate at_flags(long val, char *instr, size_t n, char *str)
742*0Sstevel@tonic-gate {
743*0Sstevel@tonic-gate 	int i;
744*0Sstevel@tonic-gate 
745*0Sstevel@tonic-gate 	*str = '\0';
746*0Sstevel@tonic-gate 
747*0Sstevel@tonic-gate 	for (i = 0; i < sizeof (auxfl)/sizeof (struct auxfl); i++) {
748*0Sstevel@tonic-gate 		if ((val & auxfl[i].af_flag) != 0) {
749*0Sstevel@tonic-gate 			if (*str != '\0')
750*0Sstevel@tonic-gate 				(void) strlcat(str, ",", n);
751*0Sstevel@tonic-gate 			(void) strlcat(str, auxfl[i].af_name, n);
752*0Sstevel@tonic-gate 		}
753*0Sstevel@tonic-gate 	}
754*0Sstevel@tonic-gate }
755*0Sstevel@tonic-gate 
756*0Sstevel@tonic-gate #define	MAX_AT_NAME_LEN	15
757*0Sstevel@tonic-gate 
758*0Sstevel@tonic-gate struct aux_id {
759*0Sstevel@tonic-gate 	int aux_type;
760*0Sstevel@tonic-gate 	const char *aux_name;
761*0Sstevel@tonic-gate 	void (*aux_decode)(long, char *, size_t, char *);
762*0Sstevel@tonic-gate };
763*0Sstevel@tonic-gate 
764*0Sstevel@tonic-gate static struct aux_id aux_arr[] = {
765*0Sstevel@tonic-gate 	{ AT_NULL,		"AT_NULL",		at_null	},
766*0Sstevel@tonic-gate 	{ AT_IGNORE,		"AT_IGNORE",		at_null	},
767*0Sstevel@tonic-gate 	{ AT_EXECFD,		"AT_EXECFD",		at_null	},
768*0Sstevel@tonic-gate 	{ AT_PHDR,		"AT_PHDR",		at_null	},
769*0Sstevel@tonic-gate 	{ AT_PHENT,		"AT_PHENT",		at_null	},
770*0Sstevel@tonic-gate 	{ AT_PHNUM,		"AT_PHNUM",		at_null	},
771*0Sstevel@tonic-gate 	{ AT_PAGESZ,		"AT_PAGESZ",		at_null	},
772*0Sstevel@tonic-gate 	{ AT_BASE,		"AT_BASE",		at_null	},
773*0Sstevel@tonic-gate 	{ AT_FLAGS,		"AT_FLAGS",		at_null	},
774*0Sstevel@tonic-gate 	{ AT_ENTRY,		"AT_ENTRY",		at_null	},
775*0Sstevel@tonic-gate 	{ AT_SUN_UID,		"AT_SUN_UID",		at_uid	},
776*0Sstevel@tonic-gate 	{ AT_SUN_RUID,		"AT_SUN_RUID",		at_uid	},
777*0Sstevel@tonic-gate 	{ AT_SUN_GID,		"AT_SUN_GID",		at_gid	},
778*0Sstevel@tonic-gate 	{ AT_SUN_RGID,		"AT_SUN_RGID",		at_gid	},
779*0Sstevel@tonic-gate 	{ AT_SUN_LDELF,		"AT_SUN_LDELF",		at_null	},
780*0Sstevel@tonic-gate 	{ AT_SUN_LDSHDR,	"AT_SUN_LDSHDR",	at_null	},
781*0Sstevel@tonic-gate 	{ AT_SUN_LDNAME,	"AT_SUN_LDNAME",	at_null	},
782*0Sstevel@tonic-gate 	{ AT_SUN_LPAGESZ,	"AT_SUN_LPAGESZ",	at_null	},
783*0Sstevel@tonic-gate 	{ AT_SUN_PLATFORM,	"AT_SUN_PLATFORM",	at_str	},
784*0Sstevel@tonic-gate 	{ AT_SUN_EXECNAME,	"AT_SUN_EXECNAME",	at_str	},
785*0Sstevel@tonic-gate 	{ AT_SUN_HWCAP,		"AT_SUN_HWCAP",		at_hwcap },
786*0Sstevel@tonic-gate 	{ AT_SUN_IFLUSH,	"AT_SUN_IFLUSH",	at_null	},
787*0Sstevel@tonic-gate 	{ AT_SUN_CPU,		"AT_SUN_CPU",		at_null	},
788*0Sstevel@tonic-gate 	{ AT_SUN_MMU,		"AT_SUN_MMU",		at_null	},
789*0Sstevel@tonic-gate 	{ AT_SUN_LDDATA,	"AT_SUN_LDDATA",	at_null	},
790*0Sstevel@tonic-gate 	{ AT_SUN_AUXFLAGS,	"AT_SUN_AUXFLAGS",	at_flags },
791*0Sstevel@tonic-gate };
792*0Sstevel@tonic-gate 
793*0Sstevel@tonic-gate #define	N_AT_ENTS (sizeof (aux_arr) / sizeof (struct aux_id))
794*0Sstevel@tonic-gate 
795*0Sstevel@tonic-gate /*
796*0Sstevel@tonic-gate  * Return the aux_id entry for the given aux type; returns NULL if not found.
797*0Sstevel@tonic-gate  */
798*0Sstevel@tonic-gate static struct aux_id *
799*0Sstevel@tonic-gate aux_find(int type)
800*0Sstevel@tonic-gate {
801*0Sstevel@tonic-gate 	int i;
802*0Sstevel@tonic-gate 
803*0Sstevel@tonic-gate 	for (i = 0; i < N_AT_ENTS; i++) {
804*0Sstevel@tonic-gate 		if (type == aux_arr[i].aux_type)
805*0Sstevel@tonic-gate 			return (&aux_arr[i]);
806*0Sstevel@tonic-gate 	}
807*0Sstevel@tonic-gate 
808*0Sstevel@tonic-gate 	return (NULL);
809*0Sstevel@tonic-gate }
810*0Sstevel@tonic-gate 
811*0Sstevel@tonic-gate static void
812*0Sstevel@tonic-gate get_auxv(pargs_data_t *datap)
813*0Sstevel@tonic-gate {
814*0Sstevel@tonic-gate 	int i;
815*0Sstevel@tonic-gate 	const auxv_t *auxvp;
816*0Sstevel@tonic-gate 
817*0Sstevel@tonic-gate 	/*
818*0Sstevel@tonic-gate 	 * Fetch the aux vector from the target process.
819*0Sstevel@tonic-gate 	 */
820*0Sstevel@tonic-gate 	if (ps_pauxv(datap->pd_proc, &auxvp) != PS_OK)
821*0Sstevel@tonic-gate 		return;
822*0Sstevel@tonic-gate 
823*0Sstevel@tonic-gate 	for (i = 0; auxvp[i].a_type != AT_NULL; i++)
824*0Sstevel@tonic-gate 		continue;
825*0Sstevel@tonic-gate 
826*0Sstevel@tonic-gate 	datap->pd_auxc = i;
827*0Sstevel@tonic-gate 	datap->pd_auxv = safe_zalloc(i * sizeof (auxv_t));
828*0Sstevel@tonic-gate 	bcopy(auxvp, datap->pd_auxv, i * sizeof (auxv_t));
829*0Sstevel@tonic-gate 
830*0Sstevel@tonic-gate 	datap->pd_auxv_strs = safe_zalloc(datap->pd_auxc * sizeof (char *));
831*0Sstevel@tonic-gate 	for (i = 0; i < datap->pd_auxc; i++) {
832*0Sstevel@tonic-gate 		struct aux_id *aux = aux_find(datap->pd_auxv[i].a_type);
833*0Sstevel@tonic-gate 
834*0Sstevel@tonic-gate 		/*
835*0Sstevel@tonic-gate 		 * Grab strings for those entries which have a string-decoder.
836*0Sstevel@tonic-gate 		 */
837*0Sstevel@tonic-gate 		if ((aux != NULL) && (aux->aux_decode == at_str)) {
838*0Sstevel@tonic-gate 			datap->pd_auxv_strs[i] =
839*0Sstevel@tonic-gate 			    extract_string(datap, datap->pd_auxv[i].a_un.a_val);
840*0Sstevel@tonic-gate 		}
841*0Sstevel@tonic-gate 	}
842*0Sstevel@tonic-gate }
843*0Sstevel@tonic-gate 
844*0Sstevel@tonic-gate /*
845*0Sstevel@tonic-gate  * Prepare to convert characters in the victim's character set into user's
846*0Sstevel@tonic-gate  * character set.
847*0Sstevel@tonic-gate  */
848*0Sstevel@tonic-gate static void
849*0Sstevel@tonic-gate setup_conversions(pargs_data_t *datap, int *diflocale)
850*0Sstevel@tonic-gate {
851*0Sstevel@tonic-gate 	char *mylocale = NULL, *mycharset = NULL;
852*0Sstevel@tonic-gate 	char *targetlocale = NULL, *targetcharset = NULL;
853*0Sstevel@tonic-gate 
854*0Sstevel@tonic-gate 	mycharset = safe_strdup(nl_langinfo(CODESET));
855*0Sstevel@tonic-gate 
856*0Sstevel@tonic-gate 	mylocale = setlocale(LC_CTYPE, NULL);
857*0Sstevel@tonic-gate 	if ((mylocale == NULL) || (strcmp(mylocale, "") == 0))
858*0Sstevel@tonic-gate 		mylocale = "C";
859*0Sstevel@tonic-gate 	mylocale = safe_strdup(mylocale);
860*0Sstevel@tonic-gate 
861*0Sstevel@tonic-gate 	if (datap->pd_conv_flags & CONV_STRICT_ASCII)
862*0Sstevel@tonic-gate 		goto done;
863*0Sstevel@tonic-gate 
864*0Sstevel@tonic-gate 	/*
865*0Sstevel@tonic-gate 	 * If the target's locale is "C" or "POSIX", go fast.
866*0Sstevel@tonic-gate 	 */
867*0Sstevel@tonic-gate 	if ((strcmp(datap->pd_locale, "C") == 0) ||
868*0Sstevel@tonic-gate 	    (strcmp(datap->pd_locale, "POSIX") == 0)) {
869*0Sstevel@tonic-gate 		datap->pd_conv_flags |= CONV_STRICT_ASCII;
870*0Sstevel@tonic-gate 		goto done;
871*0Sstevel@tonic-gate 	}
872*0Sstevel@tonic-gate 
873*0Sstevel@tonic-gate 	/*
874*0Sstevel@tonic-gate 	 * Switch to the victim's locale, and discover its character set.
875*0Sstevel@tonic-gate 	 */
876*0Sstevel@tonic-gate 	if (setlocale(LC_ALL, datap->pd_locale) == NULL) {
877*0Sstevel@tonic-gate 		(void) fprintf(stderr,
878*0Sstevel@tonic-gate 		    "%s: Couldn't determine locale of target process.\n",
879*0Sstevel@tonic-gate 		    command);
880*0Sstevel@tonic-gate 		(void) fprintf(stderr,
881*0Sstevel@tonic-gate 		    "%s: Some strings may not be displayed properly.\n",
882*0Sstevel@tonic-gate 		    command);
883*0Sstevel@tonic-gate 		goto done;
884*0Sstevel@tonic-gate 	}
885*0Sstevel@tonic-gate 
886*0Sstevel@tonic-gate 	/*
887*0Sstevel@tonic-gate 	 * Get LC_CTYPE part of target's locale, and its codeset.
888*0Sstevel@tonic-gate 	 */
889*0Sstevel@tonic-gate 	targetlocale = safe_strdup(setlocale(LC_CTYPE, NULL));
890*0Sstevel@tonic-gate 	targetcharset = safe_strdup(nl_langinfo(CODESET));
891*0Sstevel@tonic-gate 
892*0Sstevel@tonic-gate 	/*
893*0Sstevel@tonic-gate 	 * Now go fully back to the pargs user's locale.
894*0Sstevel@tonic-gate 	 */
895*0Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
896*0Sstevel@tonic-gate 
897*0Sstevel@tonic-gate 	/*
898*0Sstevel@tonic-gate 	 * It's safe to bail here if the lc_ctype of the locales are the
899*0Sstevel@tonic-gate 	 * same-- we know that their encodings and characters sets are the same.
900*0Sstevel@tonic-gate 	 */
901*0Sstevel@tonic-gate 	if (strcmp(targetlocale, mylocale) == 0)
902*0Sstevel@tonic-gate 		goto done;
903*0Sstevel@tonic-gate 
904*0Sstevel@tonic-gate 	*diflocale = 1;
905*0Sstevel@tonic-gate 
906*0Sstevel@tonic-gate 	/*
907*0Sstevel@tonic-gate 	 * If the codeset of the victim matches our codeset then iconv need
908*0Sstevel@tonic-gate 	 * not be involved.
909*0Sstevel@tonic-gate 	 */
910*0Sstevel@tonic-gate 	if (strcmp(mycharset, targetcharset) == 0)
911*0Sstevel@tonic-gate 		goto done;
912*0Sstevel@tonic-gate 
913*0Sstevel@tonic-gate 	if ((datap->pd_iconv = iconv_open(mycharset, targetcharset))
914*0Sstevel@tonic-gate 	    == (iconv_t)-1) {
915*0Sstevel@tonic-gate 		/*
916*0Sstevel@tonic-gate 		 * EINVAL indicates there was no conversion available
917*0Sstevel@tonic-gate 		 * from victim charset to mycharset
918*0Sstevel@tonic-gate 		 */
919*0Sstevel@tonic-gate 		if (errno != EINVAL) {
920*0Sstevel@tonic-gate 			(void) fprintf(stderr,
921*0Sstevel@tonic-gate 			    "%s: failed to initialize iconv: %s\n",
922*0Sstevel@tonic-gate 			    command, strerror(errno));
923*0Sstevel@tonic-gate 			exit(1);
924*0Sstevel@tonic-gate 		}
925*0Sstevel@tonic-gate 		datap->pd_conv_flags |= CONV_STRICT_ASCII;
926*0Sstevel@tonic-gate 	} else {
927*0Sstevel@tonic-gate 		datap->pd_conv_flags |= CONV_USE_ICONV;
928*0Sstevel@tonic-gate 	}
929*0Sstevel@tonic-gate done:
930*0Sstevel@tonic-gate 	free(mycharset);
931*0Sstevel@tonic-gate 	free(mylocale);
932*0Sstevel@tonic-gate 	free(targetcharset);
933*0Sstevel@tonic-gate 	free(targetlocale);
934*0Sstevel@tonic-gate }
935*0Sstevel@tonic-gate 
936*0Sstevel@tonic-gate static void
937*0Sstevel@tonic-gate cleanup_conversions(pargs_data_t *datap)
938*0Sstevel@tonic-gate {
939*0Sstevel@tonic-gate 	if (datap->pd_conv_flags & CONV_USE_ICONV) {
940*0Sstevel@tonic-gate 		(void) iconv_close(datap->pd_iconv);
941*0Sstevel@tonic-gate 	}
942*0Sstevel@tonic-gate }
943*0Sstevel@tonic-gate 
944*0Sstevel@tonic-gate static char *
945*0Sstevel@tonic-gate convert_run_iconv(pargs_data_t *datap, const char *str)
946*0Sstevel@tonic-gate {
947*0Sstevel@tonic-gate 	size_t inleft, outleft, bufsz = 64;
948*0Sstevel@tonic-gate 	char *outstr, *outstrptr;
949*0Sstevel@tonic-gate 	const char *instrptr;
950*0Sstevel@tonic-gate 
951*0Sstevel@tonic-gate 	for (;;) {
952*0Sstevel@tonic-gate 		outstrptr = outstr = safe_zalloc(bufsz + 1);
953*0Sstevel@tonic-gate 		outleft = bufsz;
954*0Sstevel@tonic-gate 
955*0Sstevel@tonic-gate 		/*
956*0Sstevel@tonic-gate 		 * Generate the "initial shift state" sequence, placing that
957*0Sstevel@tonic-gate 		 * at the head of the string.
958*0Sstevel@tonic-gate 		 */
959*0Sstevel@tonic-gate 		inleft = 0;
960*0Sstevel@tonic-gate 		(void) iconv(datap->pd_iconv, NULL, &inleft,
961*0Sstevel@tonic-gate 		    &outstrptr, &outleft);
962*0Sstevel@tonic-gate 
963*0Sstevel@tonic-gate 		inleft = strlen(str);
964*0Sstevel@tonic-gate 		instrptr = str;
965*0Sstevel@tonic-gate 		if (iconv(datap->pd_iconv, &instrptr, &inleft, &outstrptr,
966*0Sstevel@tonic-gate 		    &outleft) != (size_t)-1) {
967*0Sstevel@tonic-gate 			/*
968*0Sstevel@tonic-gate 			 * Outstr must be null terminated upon exit from
969*0Sstevel@tonic-gate 			 * iconv().
970*0Sstevel@tonic-gate 			 */
971*0Sstevel@tonic-gate 			*(outstr + (bufsz - outleft)) = '\0';
972*0Sstevel@tonic-gate 			break;
973*0Sstevel@tonic-gate 		} else if (errno == E2BIG) {
974*0Sstevel@tonic-gate 			bufsz *= 2;
975*0Sstevel@tonic-gate 			free(outstr);
976*0Sstevel@tonic-gate 		} else if ((errno == EILSEQ) || (errno == EINVAL)) {
977*0Sstevel@tonic-gate 			free(outstr);
978*0Sstevel@tonic-gate 			return (NULL);
979*0Sstevel@tonic-gate 		} else {
980*0Sstevel@tonic-gate 			/*
981*0Sstevel@tonic-gate 			 * iconv() could in theory return EBADF, but that
982*0Sstevel@tonic-gate 			 * shouldn't happen.
983*0Sstevel@tonic-gate 			 */
984*0Sstevel@tonic-gate 			(void) fprintf(stderr,
985*0Sstevel@tonic-gate 			    "%s: iconv(3C) failed unexpectedly: %s\n",
986*0Sstevel@tonic-gate 			    command, strerror(errno));
987*0Sstevel@tonic-gate 
988*0Sstevel@tonic-gate 			exit(1);
989*0Sstevel@tonic-gate 		}
990*0Sstevel@tonic-gate 	}
991*0Sstevel@tonic-gate 	return (outstr);
992*0Sstevel@tonic-gate }
993*0Sstevel@tonic-gate 
994*0Sstevel@tonic-gate /*
995*0Sstevel@tonic-gate  * Returns a freshly allocated string converted to the local character set,
996*0Sstevel@tonic-gate  * removed of unprintable characters.
997*0Sstevel@tonic-gate  */
998*0Sstevel@tonic-gate static char *
999*0Sstevel@tonic-gate convert_str(pargs_data_t *datap, const char *str, int *unprintable)
1000*0Sstevel@tonic-gate {
1001*0Sstevel@tonic-gate 	char *retstr, *tmp;
1002*0Sstevel@tonic-gate 
1003*0Sstevel@tonic-gate 	if (datap->pd_conv_flags & CONV_STRICT_ASCII) {
1004*0Sstevel@tonic-gate 		retstr = unctrl_str_strict_ascii(str, 1, unprintable);
1005*0Sstevel@tonic-gate 		return (retstr);
1006*0Sstevel@tonic-gate 	}
1007*0Sstevel@tonic-gate 
1008*0Sstevel@tonic-gate 	if ((datap->pd_conv_flags & CONV_USE_ICONV) == 0) {
1009*0Sstevel@tonic-gate 		/*
1010*0Sstevel@tonic-gate 		 * If we aren't using iconv(), convert control chars in
1011*0Sstevel@tonic-gate 		 * the string in pargs' locale, since that is the display
1012*0Sstevel@tonic-gate 		 * locale.
1013*0Sstevel@tonic-gate 		 */
1014*0Sstevel@tonic-gate 		retstr = unctrl_str(str, 1, unprintable);
1015*0Sstevel@tonic-gate 		return (retstr);
1016*0Sstevel@tonic-gate 	}
1017*0Sstevel@tonic-gate 
1018*0Sstevel@tonic-gate 	/*
1019*0Sstevel@tonic-gate 	 * The logic here is a bit (ahem) tricky.  Start by converting
1020*0Sstevel@tonic-gate 	 * unprintable characters *in the target's locale*.  This should
1021*0Sstevel@tonic-gate 	 * eliminate a variety of unprintable or illegal characters-- in
1022*0Sstevel@tonic-gate 	 * short, it should leave us with something which iconv() won't
1023*0Sstevel@tonic-gate 	 * have trouble with.
1024*0Sstevel@tonic-gate 	 *
1025*0Sstevel@tonic-gate 	 * After allowing iconv to convert characters as needed, run unctrl
1026*0Sstevel@tonic-gate 	 * again in pargs' locale-- This time to make sure that any
1027*0Sstevel@tonic-gate 	 * characters which aren't printable according to the *current*
1028*0Sstevel@tonic-gate 	 * locale (independent of the current codeset) get taken care of.
1029*0Sstevel@tonic-gate 	 * Without this second stage, we might (for example) fail to
1030*0Sstevel@tonic-gate 	 * properly handle characters converted into the 646 character set
1031*0Sstevel@tonic-gate 	 * (which are 8-bits wide), but which must be displayed in the C
1032*0Sstevel@tonic-gate 	 * locale (which uses 646, but whose printable characters are a
1033*0Sstevel@tonic-gate 	 * subset of the 7-bit characters).
1034*0Sstevel@tonic-gate 	 *
1035*0Sstevel@tonic-gate 	 * Note that assuming the victim's locale using LC_ALL will be
1036*0Sstevel@tonic-gate 	 * problematic when pargs' messages are internationalized in the
1037*0Sstevel@tonic-gate 	 * future (and it calls textdomain(3C)).  In this case, any
1038*0Sstevel@tonic-gate 	 * error message fprintf'd in unctrl_str() will be in the wrong
1039*0Sstevel@tonic-gate 	 * LC_MESSAGES class.  We'll cross that bridge when we come to it.
1040*0Sstevel@tonic-gate 	 */
1041*0Sstevel@tonic-gate 	(void) setlocale(LC_ALL, datap->pd_locale);
1042*0Sstevel@tonic-gate 	retstr = unctrl_str(str, 1, unprintable);
1043*0Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1044*0Sstevel@tonic-gate 
1045*0Sstevel@tonic-gate 	tmp = retstr;
1046*0Sstevel@tonic-gate 	if ((retstr = convert_run_iconv(datap, retstr)) == NULL) {
1047*0Sstevel@tonic-gate 		/*
1048*0Sstevel@tonic-gate 		 * In this (rare but real) case, the iconv() failed even
1049*0Sstevel@tonic-gate 		 * though we unctrl'd the string.  Treat the original string
1050*0Sstevel@tonic-gate 		 * (str) as a C locale string and strip it that way.
1051*0Sstevel@tonic-gate 		 */
1052*0Sstevel@tonic-gate 		free(tmp);
1053*0Sstevel@tonic-gate 		return (unctrl_str_strict_ascii(str, 0, unprintable));
1054*0Sstevel@tonic-gate 	}
1055*0Sstevel@tonic-gate 
1056*0Sstevel@tonic-gate 	free(tmp);
1057*0Sstevel@tonic-gate 	tmp = retstr;
1058*0Sstevel@tonic-gate 	/*
1059*0Sstevel@tonic-gate 	 * Run unctrl_str, but make sure not to escape \ characters, which
1060*0Sstevel@tonic-gate 	 * may have resulted from the first round of unctrl.
1061*0Sstevel@tonic-gate 	 */
1062*0Sstevel@tonic-gate 	retstr = unctrl_str(retstr, 0, unprintable);
1063*0Sstevel@tonic-gate 	free(tmp);
1064*0Sstevel@tonic-gate 	return (retstr);
1065*0Sstevel@tonic-gate }
1066*0Sstevel@tonic-gate 
1067*0Sstevel@tonic-gate 
1068*0Sstevel@tonic-gate static void
1069*0Sstevel@tonic-gate convert_array(pargs_data_t *datap, char **arr, size_t count, int *unprintable)
1070*0Sstevel@tonic-gate {
1071*0Sstevel@tonic-gate 	int i;
1072*0Sstevel@tonic-gate 	char *tmp;
1073*0Sstevel@tonic-gate 
1074*0Sstevel@tonic-gate 	if (arr == NULL)
1075*0Sstevel@tonic-gate 		return;
1076*0Sstevel@tonic-gate 
1077*0Sstevel@tonic-gate 	for (i = 0; i < count; i++) {
1078*0Sstevel@tonic-gate 		if ((tmp = arr[i]) == NULL)
1079*0Sstevel@tonic-gate 			continue;
1080*0Sstevel@tonic-gate 		arr[i] = convert_str(datap, arr[i], unprintable);
1081*0Sstevel@tonic-gate 		free(tmp);
1082*0Sstevel@tonic-gate 	}
1083*0Sstevel@tonic-gate }
1084*0Sstevel@tonic-gate 
1085*0Sstevel@tonic-gate /*
1086*0Sstevel@tonic-gate  * Free data allocated during the gathering phase.
1087*0Sstevel@tonic-gate  */
1088*0Sstevel@tonic-gate static void
1089*0Sstevel@tonic-gate free_data(pargs_data_t *datap)
1090*0Sstevel@tonic-gate {
1091*0Sstevel@tonic-gate 	int i;
1092*0Sstevel@tonic-gate 
1093*0Sstevel@tonic-gate 	if (datap->pd_argv) {
1094*0Sstevel@tonic-gate 		for (i = 0; i < datap->pd_argc; i++) {
1095*0Sstevel@tonic-gate 			if (datap->pd_argv_strs[i] != NULL)
1096*0Sstevel@tonic-gate 				free(datap->pd_argv_strs[i]);
1097*0Sstevel@tonic-gate 		}
1098*0Sstevel@tonic-gate 		free(datap->pd_argv);
1099*0Sstevel@tonic-gate 		free(datap->pd_argv_strs);
1100*0Sstevel@tonic-gate 	}
1101*0Sstevel@tonic-gate 
1102*0Sstevel@tonic-gate 	if (datap->pd_envp) {
1103*0Sstevel@tonic-gate 		for (i = 0; i < datap->pd_envc; i++) {
1104*0Sstevel@tonic-gate 			if (datap->pd_envp_strs[i] != NULL)
1105*0Sstevel@tonic-gate 				free(datap->pd_envp_strs[i]);
1106*0Sstevel@tonic-gate 		}
1107*0Sstevel@tonic-gate 		free(datap->pd_envp);
1108*0Sstevel@tonic-gate 		free(datap->pd_envp_strs);
1109*0Sstevel@tonic-gate 	}
1110*0Sstevel@tonic-gate 
1111*0Sstevel@tonic-gate 	if (datap->pd_auxv) {
1112*0Sstevel@tonic-gate 		for (i = 0; i < datap->pd_auxc; i++) {
1113*0Sstevel@tonic-gate 			if (datap->pd_auxv_strs[i] != NULL)
1114*0Sstevel@tonic-gate 				free(datap->pd_auxv_strs[i]);
1115*0Sstevel@tonic-gate 		}
1116*0Sstevel@tonic-gate 		free(datap->pd_auxv);
1117*0Sstevel@tonic-gate 		free(datap->pd_auxv_strs);
1118*0Sstevel@tonic-gate 	}
1119*0Sstevel@tonic-gate }
1120*0Sstevel@tonic-gate 
1121*0Sstevel@tonic-gate static void
1122*0Sstevel@tonic-gate print_args(pargs_data_t *datap)
1123*0Sstevel@tonic-gate {
1124*0Sstevel@tonic-gate 	int i;
1125*0Sstevel@tonic-gate 
1126*0Sstevel@tonic-gate 	if (datap->pd_argv == NULL) {
1127*0Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: failed to read argv[]\n", command);
1128*0Sstevel@tonic-gate 		return;
1129*0Sstevel@tonic-gate 	}
1130*0Sstevel@tonic-gate 
1131*0Sstevel@tonic-gate 	for (i = 0; i < datap->pd_argc; i++) {
1132*0Sstevel@tonic-gate 		(void) printf("argv[%d]: ", i);
1133*0Sstevel@tonic-gate 		if (datap->pd_argv[i] == NULL) {
1134*0Sstevel@tonic-gate 			(void) printf("<NULL>\n");
1135*0Sstevel@tonic-gate 		} else if (datap->pd_argv_strs[i] == NULL) {
1136*0Sstevel@tonic-gate 			(void) printf("<0x%0*lx>\n",
1137*0Sstevel@tonic-gate 			    (dmodel == PR_MODEL_LP64)? 16 : 8,
1138*0Sstevel@tonic-gate 			    (long)datap->pd_argv[i]);
1139*0Sstevel@tonic-gate 		} else {
1140*0Sstevel@tonic-gate 			(void) printf("%s\n", datap->pd_argv_strs[i]);
1141*0Sstevel@tonic-gate 		}
1142*0Sstevel@tonic-gate 	}
1143*0Sstevel@tonic-gate }
1144*0Sstevel@tonic-gate 
1145*0Sstevel@tonic-gate static void
1146*0Sstevel@tonic-gate print_env(pargs_data_t *datap)
1147*0Sstevel@tonic-gate {
1148*0Sstevel@tonic-gate 	int i;
1149*0Sstevel@tonic-gate 
1150*0Sstevel@tonic-gate 	if (datap->pd_envp == NULL) {
1151*0Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: failed to read envp[]\n", command);
1152*0Sstevel@tonic-gate 		return;
1153*0Sstevel@tonic-gate 	}
1154*0Sstevel@tonic-gate 
1155*0Sstevel@tonic-gate 	for (i = 0; i < datap->pd_envc; i++) {
1156*0Sstevel@tonic-gate 		(void) printf("envp[%d]: ", i);
1157*0Sstevel@tonic-gate 		if (datap->pd_envp[i] == 0) {
1158*0Sstevel@tonic-gate 			break;
1159*0Sstevel@tonic-gate 		} else if (datap->pd_envp_strs[i] == NULL) {
1160*0Sstevel@tonic-gate 			(void) printf("<0x%0*lx>\n",
1161*0Sstevel@tonic-gate 			    (dmodel == PR_MODEL_LP64)? 16 : 8,
1162*0Sstevel@tonic-gate 			    (long)datap->pd_envp[i]);
1163*0Sstevel@tonic-gate 		} else {
1164*0Sstevel@tonic-gate 			(void) printf("%s\n", datap->pd_envp_strs[i]);
1165*0Sstevel@tonic-gate 		}
1166*0Sstevel@tonic-gate 	}
1167*0Sstevel@tonic-gate }
1168*0Sstevel@tonic-gate 
1169*0Sstevel@tonic-gate static int
1170*0Sstevel@tonic-gate print_cmdline(pargs_data_t *datap)
1171*0Sstevel@tonic-gate {
1172*0Sstevel@tonic-gate 	int i;
1173*0Sstevel@tonic-gate 
1174*0Sstevel@tonic-gate 	/*
1175*0Sstevel@tonic-gate 	 * Go through and check to see if we have valid data.  If not, print
1176*0Sstevel@tonic-gate 	 * an error message and bail.
1177*0Sstevel@tonic-gate 	 */
1178*0Sstevel@tonic-gate 	for (i = 0; i < datap->pd_argc; i++) {
1179*0Sstevel@tonic-gate 		if (datap->pd_argv[i] == NULL ||
1180*0Sstevel@tonic-gate 		    datap->pd_argv_strs[i] == NULL) {
1181*0Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: target has corrupted "
1182*0Sstevel@tonic-gate 			    "argument list\n", command);
1183*0Sstevel@tonic-gate 			return (1);
1184*0Sstevel@tonic-gate 		}
1185*0Sstevel@tonic-gate 
1186*0Sstevel@tonic-gate 		datap->pd_argv_strs[i] =
1187*0Sstevel@tonic-gate 		    quote_string(datap, datap->pd_argv_strs[i]);
1188*0Sstevel@tonic-gate 	}
1189*0Sstevel@tonic-gate 
1190*0Sstevel@tonic-gate 	if (datap->pd_execname == NULL) {
1191*0Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: cannot determine name of "
1192*0Sstevel@tonic-gate 		    "executable\n", command);
1193*0Sstevel@tonic-gate 		return (1);
1194*0Sstevel@tonic-gate 	}
1195*0Sstevel@tonic-gate 
1196*0Sstevel@tonic-gate 	(void) printf("%s ", datap->pd_execname);
1197*0Sstevel@tonic-gate 
1198*0Sstevel@tonic-gate 	for (i = 1; i < datap->pd_argc; i++)
1199*0Sstevel@tonic-gate 		(void) printf("%s ", datap->pd_argv_strs[i]);
1200*0Sstevel@tonic-gate 
1201*0Sstevel@tonic-gate 	(void) printf("\n");
1202*0Sstevel@tonic-gate 
1203*0Sstevel@tonic-gate 	return (0);
1204*0Sstevel@tonic-gate }
1205*0Sstevel@tonic-gate 
1206*0Sstevel@tonic-gate static void
1207*0Sstevel@tonic-gate print_auxv(pargs_data_t *datap)
1208*0Sstevel@tonic-gate {
1209*0Sstevel@tonic-gate 	int i;
1210*0Sstevel@tonic-gate 	const auxv_t *pa;
1211*0Sstevel@tonic-gate 
1212*0Sstevel@tonic-gate 	/*
1213*0Sstevel@tonic-gate 	 * Print the names and values of all the aux vector entries.
1214*0Sstevel@tonic-gate 	 */
1215*0Sstevel@tonic-gate 	for (i = 0; i < datap->pd_auxc; i++) {
1216*0Sstevel@tonic-gate 		char type[32];
1217*0Sstevel@tonic-gate 		char decode[PATH_MAX];
1218*0Sstevel@tonic-gate 		struct aux_id *aux;
1219*0Sstevel@tonic-gate 		long v;
1220*0Sstevel@tonic-gate 		pa = &datap->pd_auxv[i];
1221*0Sstevel@tonic-gate 
1222*0Sstevel@tonic-gate 		aux = aux_find(pa->a_type);
1223*0Sstevel@tonic-gate 		v = (long)pa->a_un.a_val;
1224*0Sstevel@tonic-gate 
1225*0Sstevel@tonic-gate 		if (aux != NULL) {
1226*0Sstevel@tonic-gate 			/*
1227*0Sstevel@tonic-gate 			 * Fetch aux vector type string and decoded
1228*0Sstevel@tonic-gate 			 * representation of the value.
1229*0Sstevel@tonic-gate 			 */
1230*0Sstevel@tonic-gate 			(void) strlcpy(type, aux->aux_name, sizeof (type));
1231*0Sstevel@tonic-gate 			aux->aux_decode(v, datap->pd_auxv_strs[i],
1232*0Sstevel@tonic-gate 			    sizeof (decode), decode);
1233*0Sstevel@tonic-gate 		} else {
1234*0Sstevel@tonic-gate 			(void) snprintf(type, sizeof (type), "%d", pa->a_type);
1235*0Sstevel@tonic-gate 			decode[0] = '\0';
1236*0Sstevel@tonic-gate 		}
1237*0Sstevel@tonic-gate 
1238*0Sstevel@tonic-gate 		(void) printf("%-*s 0x%0*lx %s\n", MAX_AT_NAME_LEN, type,
1239*0Sstevel@tonic-gate 		    (dmodel == PR_MODEL_LP64)? 16 : 8, v, decode);
1240*0Sstevel@tonic-gate 	}
1241*0Sstevel@tonic-gate }
1242*0Sstevel@tonic-gate 
1243*0Sstevel@tonic-gate int
1244*0Sstevel@tonic-gate main(int argc, char *argv[])
1245*0Sstevel@tonic-gate {
1246*0Sstevel@tonic-gate 	int aflag = 0, cflag = 0, eflag = 0, xflag = 0, lflag = 0;
1247*0Sstevel@tonic-gate 	int errflg = 0, retc = 0;
1248*0Sstevel@tonic-gate 	int opt;
1249*0Sstevel@tonic-gate 	int error = 1;
1250*0Sstevel@tonic-gate 	core_content_t content = 0;
1251*0Sstevel@tonic-gate 
1252*0Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1253*0Sstevel@tonic-gate 
1254*0Sstevel@tonic-gate 	if ((command = strrchr(argv[0], '/')) != NULL)
1255*0Sstevel@tonic-gate 		command++;
1256*0Sstevel@tonic-gate 	else
1257*0Sstevel@tonic-gate 		command = argv[0];
1258*0Sstevel@tonic-gate 
1259*0Sstevel@tonic-gate 	while ((opt = getopt(argc, argv, "acelxF")) != EOF) {
1260*0Sstevel@tonic-gate 		switch (opt) {
1261*0Sstevel@tonic-gate 		case 'a':		/* show process arguments */
1262*0Sstevel@tonic-gate 			content |= CC_CONTENT_STACK;
1263*0Sstevel@tonic-gate 			aflag++;
1264*0Sstevel@tonic-gate 			break;
1265*0Sstevel@tonic-gate 		case 'c':		/* force 7-bit ascii */
1266*0Sstevel@tonic-gate 			cflag++;
1267*0Sstevel@tonic-gate 			break;
1268*0Sstevel@tonic-gate 		case 'e':		/* show environment variables */
1269*0Sstevel@tonic-gate 			content |= CC_CONTENT_STACK;
1270*0Sstevel@tonic-gate 			eflag++;
1271*0Sstevel@tonic-gate 			break;
1272*0Sstevel@tonic-gate 		case 'l':
1273*0Sstevel@tonic-gate 			lflag++;
1274*0Sstevel@tonic-gate 			aflag++;	/* -l implies -a */
1275*0Sstevel@tonic-gate 			break;
1276*0Sstevel@tonic-gate 		case 'x':		/* show aux vector entries */
1277*0Sstevel@tonic-gate 			xflag++;
1278*0Sstevel@tonic-gate 			break;
1279*0Sstevel@tonic-gate 		case 'F':
1280*0Sstevel@tonic-gate 			/*
1281*0Sstevel@tonic-gate 			 * Since we open the process read-only, there is no need
1282*0Sstevel@tonic-gate 			 * for the -F flag.  It's a documented flag, so we
1283*0Sstevel@tonic-gate 			 * consume it silently.
1284*0Sstevel@tonic-gate 			 */
1285*0Sstevel@tonic-gate 			break;
1286*0Sstevel@tonic-gate 		default:
1287*0Sstevel@tonic-gate 			errflg++;
1288*0Sstevel@tonic-gate 			break;
1289*0Sstevel@tonic-gate 		}
1290*0Sstevel@tonic-gate 	}
1291*0Sstevel@tonic-gate 
1292*0Sstevel@tonic-gate 	/* -a is the default if no options are specified */
1293*0Sstevel@tonic-gate 	if ((aflag + eflag + xflag + lflag) == 0) {
1294*0Sstevel@tonic-gate 		aflag++;
1295*0Sstevel@tonic-gate 		content |= CC_CONTENT_STACK;
1296*0Sstevel@tonic-gate 	}
1297*0Sstevel@tonic-gate 
1298*0Sstevel@tonic-gate 	/* -l cannot be used with the -x or -e flags */
1299*0Sstevel@tonic-gate 	if (lflag && (xflag || eflag)) {
1300*0Sstevel@tonic-gate 		(void) fprintf(stderr, "-l is incompatible with -x and -e\n");
1301*0Sstevel@tonic-gate 		errflg++;
1302*0Sstevel@tonic-gate 	}
1303*0Sstevel@tonic-gate 
1304*0Sstevel@tonic-gate 	argc -= optind;
1305*0Sstevel@tonic-gate 	argv += optind;
1306*0Sstevel@tonic-gate 
1307*0Sstevel@tonic-gate 	if (errflg || argc <= 0) {
1308*0Sstevel@tonic-gate 		(void) fprintf(stderr,
1309*0Sstevel@tonic-gate 		    "usage:  %s [-acexF] { pid | core } ...\n"
1310*0Sstevel@tonic-gate 		    "  (show process arguments and environment)\n"
1311*0Sstevel@tonic-gate 		    "  -a: show process arguments (default)\n"
1312*0Sstevel@tonic-gate 		    "  -c: interpret characters as 7-bit ascii regardless of "
1313*0Sstevel@tonic-gate 		    "locale\n"
1314*0Sstevel@tonic-gate 		    "  -e: show environment variables\n"
1315*0Sstevel@tonic-gate 		    "  -l: display arguments as command line\n"
1316*0Sstevel@tonic-gate 		    "  -x: show aux vector entries\n"
1317*0Sstevel@tonic-gate 		    "  -F: force grabbing of the target process\n", command);
1318*0Sstevel@tonic-gate 		return (2);
1319*0Sstevel@tonic-gate 	}
1320*0Sstevel@tonic-gate 
1321*0Sstevel@tonic-gate 	while (argc-- > 0) {
1322*0Sstevel@tonic-gate 		char *arg;
1323*0Sstevel@tonic-gate 		int gret, r;
1324*0Sstevel@tonic-gate 		psinfo_t psinfo;
1325*0Sstevel@tonic-gate 		char *psargs_conv;
1326*0Sstevel@tonic-gate 		struct ps_prochandle *Pr;
1327*0Sstevel@tonic-gate 		pargs_data_t datap;
1328*0Sstevel@tonic-gate 		char *info;
1329*0Sstevel@tonic-gate 		size_t info_sz;
1330*0Sstevel@tonic-gate 		int pstate;
1331*0Sstevel@tonic-gate 		char execname[PATH_MAX];
1332*0Sstevel@tonic-gate 		int unprintable;
1333*0Sstevel@tonic-gate 		int diflocale;
1334*0Sstevel@tonic-gate 
1335*0Sstevel@tonic-gate 		(void) fflush(stdout);
1336*0Sstevel@tonic-gate 		arg = *argv++;
1337*0Sstevel@tonic-gate 
1338*0Sstevel@tonic-gate 		/*
1339*0Sstevel@tonic-gate 		 * Suppress extra blanks lines if we've encountered processes
1340*0Sstevel@tonic-gate 		 * which can't be opened.
1341*0Sstevel@tonic-gate 		 */
1342*0Sstevel@tonic-gate 		if (error == 0) {
1343*0Sstevel@tonic-gate 			(void) printf("\n");
1344*0Sstevel@tonic-gate 		}
1345*0Sstevel@tonic-gate 		error = 0;
1346*0Sstevel@tonic-gate 
1347*0Sstevel@tonic-gate 		/*
1348*0Sstevel@tonic-gate 		 * First grab just the psinfo information, in case this
1349*0Sstevel@tonic-gate 		 * process is a zombie (in which case proc_arg_grab() will
1350*0Sstevel@tonic-gate 		 * fail).  If so, print a nice message and continue.
1351*0Sstevel@tonic-gate 		 */
1352*0Sstevel@tonic-gate 		if (proc_arg_psinfo(arg, PR_ARG_ANY, &psinfo,
1353*0Sstevel@tonic-gate 		    &gret) == -1) {
1354*0Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: cannot examine %s: %s\n",
1355*0Sstevel@tonic-gate 			    command, arg, Pgrab_error(gret));
1356*0Sstevel@tonic-gate 			retc++;
1357*0Sstevel@tonic-gate 			error = 1;
1358*0Sstevel@tonic-gate 			continue;
1359*0Sstevel@tonic-gate 		}
1360*0Sstevel@tonic-gate 
1361*0Sstevel@tonic-gate 		if (psinfo.pr_nlwp == 0) {
1362*0Sstevel@tonic-gate 			(void) printf("%d: <defunct>\n", (int)psinfo.pr_pid);
1363*0Sstevel@tonic-gate 			continue;
1364*0Sstevel@tonic-gate 		}
1365*0Sstevel@tonic-gate 
1366*0Sstevel@tonic-gate 		/*
1367*0Sstevel@tonic-gate 		 * If process is a "system" process (like pageout), just
1368*0Sstevel@tonic-gate 		 * print its psargs and continue on.
1369*0Sstevel@tonic-gate 		 */
1370*0Sstevel@tonic-gate 		if (psinfo.pr_size == 0 && psinfo.pr_rssize == 0) {
1371*0Sstevel@tonic-gate 			proc_unctrl_psinfo(&psinfo);
1372*0Sstevel@tonic-gate 			if (!lflag)
1373*0Sstevel@tonic-gate 				(void) printf("%d: ", (int)psinfo.pr_pid);
1374*0Sstevel@tonic-gate 			(void) printf("%s\n", psinfo.pr_psargs);
1375*0Sstevel@tonic-gate 			continue;
1376*0Sstevel@tonic-gate 		}
1377*0Sstevel@tonic-gate 
1378*0Sstevel@tonic-gate 		/*
1379*0Sstevel@tonic-gate 		 * Open the process readonly, since we do not need to write to
1380*0Sstevel@tonic-gate 		 * the control file.
1381*0Sstevel@tonic-gate 		 */
1382*0Sstevel@tonic-gate 		if ((Pr = proc_arg_grab(arg, PR_ARG_ANY, PGRAB_RDONLY,
1383*0Sstevel@tonic-gate 		    &gret)) == NULL) {
1384*0Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: cannot examine %s: %s\n",
1385*0Sstevel@tonic-gate 			    command, arg, Pgrab_error(gret));
1386*0Sstevel@tonic-gate 			retc++;
1387*0Sstevel@tonic-gate 			error = 1;
1388*0Sstevel@tonic-gate 			continue;
1389*0Sstevel@tonic-gate 		}
1390*0Sstevel@tonic-gate 
1391*0Sstevel@tonic-gate 		pstate = Pstate(Pr);
1392*0Sstevel@tonic-gate 
1393*0Sstevel@tonic-gate 		if (pstate == PS_DEAD &&
1394*0Sstevel@tonic-gate 		    (Pcontent(Pr) & content) != content) {
1395*0Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: core '%s' has "
1396*0Sstevel@tonic-gate 			    "insufficient content\n", command, arg);
1397*0Sstevel@tonic-gate 			retc++;
1398*0Sstevel@tonic-gate 			continue;
1399*0Sstevel@tonic-gate 		}
1400*0Sstevel@tonic-gate 
1401*0Sstevel@tonic-gate 		/*
1402*0Sstevel@tonic-gate 		 * If malloc() fails, we return here so that we can let go
1403*0Sstevel@tonic-gate 		 * of the victim, restore our locale, print a message,
1404*0Sstevel@tonic-gate 		 * then exit.
1405*0Sstevel@tonic-gate 		 */
1406*0Sstevel@tonic-gate 		if ((r = setjmp(env)) != 0) {
1407*0Sstevel@tonic-gate 			Prelease(Pr, 0);
1408*0Sstevel@tonic-gate 			(void) setlocale(LC_ALL, "");
1409*0Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: out of memory: %s\n",
1410*0Sstevel@tonic-gate 			    command, strerror(r));
1411*0Sstevel@tonic-gate 			return (1);
1412*0Sstevel@tonic-gate 		}
1413*0Sstevel@tonic-gate 
1414*0Sstevel@tonic-gate 		dmodel = Pstatus(Pr)->pr_dmodel;
1415*0Sstevel@tonic-gate 		bzero(&datap, sizeof (datap));
1416*0Sstevel@tonic-gate 		bcopy(Ppsinfo(Pr), &psinfo, sizeof (psinfo_t));
1417*0Sstevel@tonic-gate 		datap.pd_proc = Pr;
1418*0Sstevel@tonic-gate 		datap.pd_psinfo = &psinfo;
1419*0Sstevel@tonic-gate 
1420*0Sstevel@tonic-gate 		if (cflag)
1421*0Sstevel@tonic-gate 			datap.pd_conv_flags |= CONV_STRICT_ASCII;
1422*0Sstevel@tonic-gate 
1423*0Sstevel@tonic-gate 		/*
1424*0Sstevel@tonic-gate 		 * Strip control characters, then record process summary in
1425*0Sstevel@tonic-gate 		 * a buffer, since we don't want to print anything out until
1426*0Sstevel@tonic-gate 		 * after we release the process.
1427*0Sstevel@tonic-gate 		 */
1428*0Sstevel@tonic-gate 
1429*0Sstevel@tonic-gate 		/*
1430*0Sstevel@tonic-gate 		 * The process is neither a system process nor defunct.
1431*0Sstevel@tonic-gate 		 *
1432*0Sstevel@tonic-gate 		 * Do printing and post-processing (like name lookups) after
1433*0Sstevel@tonic-gate 		 * gathering the raw data from the process and releasing it.
1434*0Sstevel@tonic-gate 		 * This way, we don't deadlock on (for example) name lookup
1435*0Sstevel@tonic-gate 		 * if we grabbed the nscd and do 'pargs -x'.
1436*0Sstevel@tonic-gate 		 *
1437*0Sstevel@tonic-gate 		 * We always fetch the environment of the target, so that we
1438*0Sstevel@tonic-gate 		 * can make an educated guess about its locale.
1439*0Sstevel@tonic-gate 		 */
1440*0Sstevel@tonic-gate 		get_env(&datap);
1441*0Sstevel@tonic-gate 		if (aflag != 0)
1442*0Sstevel@tonic-gate 			get_args(&datap);
1443*0Sstevel@tonic-gate 		if (xflag != 0)
1444*0Sstevel@tonic-gate 			get_auxv(&datap);
1445*0Sstevel@tonic-gate 
1446*0Sstevel@tonic-gate 		/*
1447*0Sstevel@tonic-gate 		 * If malloc() fails after this poiint, we return here to
1448*0Sstevel@tonic-gate 		 * restore our locale and print a message.  If we don't
1449*0Sstevel@tonic-gate 		 * reset this, we might erroneously try to Prelease a process
1450*0Sstevel@tonic-gate 		 * twice.
1451*0Sstevel@tonic-gate 		 */
1452*0Sstevel@tonic-gate 		if ((r = setjmp(env)) != 0) {
1453*0Sstevel@tonic-gate 			(void) setlocale(LC_ALL, "");
1454*0Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: out of memory: %s\n",
1455*0Sstevel@tonic-gate 			    command, strerror(r));
1456*0Sstevel@tonic-gate 			return (1);
1457*0Sstevel@tonic-gate 		}
1458*0Sstevel@tonic-gate 
1459*0Sstevel@tonic-gate 		/*
1460*0Sstevel@tonic-gate 		 * For the -l option, we need a proper name for this executable
1461*0Sstevel@tonic-gate 		 * before we release it.
1462*0Sstevel@tonic-gate 		 */
1463*0Sstevel@tonic-gate 		if (lflag)
1464*0Sstevel@tonic-gate 			datap.pd_execname = Pexecname(Pr, execname,
1465*0Sstevel@tonic-gate 			    sizeof (execname));
1466*0Sstevel@tonic-gate 
1467*0Sstevel@tonic-gate 		Prelease(Pr, 0);
1468*0Sstevel@tonic-gate 
1469*0Sstevel@tonic-gate 		/*
1470*0Sstevel@tonic-gate 		 * Crawl through the environment to determine the locale of
1471*0Sstevel@tonic-gate 		 * the target.
1472*0Sstevel@tonic-gate 		 */
1473*0Sstevel@tonic-gate 		lookup_locale(&datap);
1474*0Sstevel@tonic-gate 		diflocale = 0;
1475*0Sstevel@tonic-gate 		setup_conversions(&datap, &diflocale);
1476*0Sstevel@tonic-gate 
1477*0Sstevel@tonic-gate 		if (lflag != 0) {
1478*0Sstevel@tonic-gate 			unprintable = 0;
1479*0Sstevel@tonic-gate 			convert_array(&datap, datap.pd_argv_strs,
1480*0Sstevel@tonic-gate 			    datap.pd_argc, &unprintable);
1481*0Sstevel@tonic-gate 			if (diflocale)
1482*0Sstevel@tonic-gate 				(void) fprintf(stderr, "%s: Warning, target "
1483*0Sstevel@tonic-gate 				    "locale differs from current locale\n",
1484*0Sstevel@tonic-gate 				    command);
1485*0Sstevel@tonic-gate 			else if (unprintable)
1486*0Sstevel@tonic-gate 				(void) fprintf(stderr, "%s: Warning, command "
1487*0Sstevel@tonic-gate 				    "line contains unprintable characters\n",
1488*0Sstevel@tonic-gate 				    command);
1489*0Sstevel@tonic-gate 
1490*0Sstevel@tonic-gate 			retc += print_cmdline(&datap);
1491*0Sstevel@tonic-gate 		} else {
1492*0Sstevel@tonic-gate 			psargs_conv = convert_str(&datap, psinfo.pr_psargs,
1493*0Sstevel@tonic-gate 			    &unprintable);
1494*0Sstevel@tonic-gate 			info_sz = strlen(psargs_conv) + MAXPATHLEN + 32 + 1;
1495*0Sstevel@tonic-gate 			info = malloc(info_sz);
1496*0Sstevel@tonic-gate 			if (pstate == PS_DEAD) {
1497*0Sstevel@tonic-gate 				(void) snprintf(info, info_sz,
1498*0Sstevel@tonic-gate 				    "core '%s' of %d:\t%s\n",
1499*0Sstevel@tonic-gate 				    arg, (int)psinfo.pr_pid, psargs_conv);
1500*0Sstevel@tonic-gate 			} else {
1501*0Sstevel@tonic-gate 				(void) snprintf(info, info_sz, "%d:\t%s\n",
1502*0Sstevel@tonic-gate 				    (int)psinfo.pr_pid, psargs_conv);
1503*0Sstevel@tonic-gate 			}
1504*0Sstevel@tonic-gate 			(void) printf("%s", info);
1505*0Sstevel@tonic-gate 			free(info);
1506*0Sstevel@tonic-gate 			free(psargs_conv);
1507*0Sstevel@tonic-gate 
1508*0Sstevel@tonic-gate 			if (aflag != 0) {
1509*0Sstevel@tonic-gate 				convert_array(&datap, datap.pd_argv_strs,
1510*0Sstevel@tonic-gate 				    datap.pd_argc, &unprintable);
1511*0Sstevel@tonic-gate 				print_args(&datap);
1512*0Sstevel@tonic-gate 				if (eflag || xflag)
1513*0Sstevel@tonic-gate 					(void) printf("\n");
1514*0Sstevel@tonic-gate 			}
1515*0Sstevel@tonic-gate 
1516*0Sstevel@tonic-gate 			if (eflag != 0) {
1517*0Sstevel@tonic-gate 				convert_array(&datap, datap.pd_envp_strs,
1518*0Sstevel@tonic-gate 				    datap.pd_envc, &unprintable);
1519*0Sstevel@tonic-gate 				print_env(&datap);
1520*0Sstevel@tonic-gate 				if (xflag)
1521*0Sstevel@tonic-gate 					(void) printf("\n");
1522*0Sstevel@tonic-gate 			}
1523*0Sstevel@tonic-gate 
1524*0Sstevel@tonic-gate 			if (xflag != 0) {
1525*0Sstevel@tonic-gate 				convert_array(&datap, datap.pd_auxv_strs,
1526*0Sstevel@tonic-gate 				    datap.pd_auxc, &unprintable);
1527*0Sstevel@tonic-gate 				print_auxv(&datap);
1528*0Sstevel@tonic-gate 			}
1529*0Sstevel@tonic-gate 		}
1530*0Sstevel@tonic-gate 
1531*0Sstevel@tonic-gate 		cleanup_conversions(&datap);
1532*0Sstevel@tonic-gate 		free_data(&datap);
1533*0Sstevel@tonic-gate 	}
1534*0Sstevel@tonic-gate 
1535*0Sstevel@tonic-gate 	return (retc != 0 ? 1 : 0);
1536*0Sstevel@tonic-gate }
1537