xref: /titanic_44/usr/src/cmd/ptools/pargs/pargs.c (revision cc401b3700f84e16c3a03f62783088a993d21466)
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
59acbbeafSnn35248  * Common Development and Distribution License (the "License").
69acbbeafSnn35248  * 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 /*
2207678296Ssl108498  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
25ebb8ac07SRobert Mustacchi /*
26*cc401b37SPatrick Mooney  * Copyright 2016 Joyent, Inc.
27ebb8ac07SRobert Mustacchi  */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * pargs examines and prints the arguments (argv), environment (environ),
317c478bd9Sstevel@tonic-gate  * and auxiliary vector of another process.
327c478bd9Sstevel@tonic-gate  *
337c478bd9Sstevel@tonic-gate  * This utility is made more complex because it must run in internationalized
347c478bd9Sstevel@tonic-gate  * environments.  The two key cases for pargs to manage are:
357c478bd9Sstevel@tonic-gate  *
367c478bd9Sstevel@tonic-gate  * 1. pargs and target run in the same locale: pargs must respect the
377c478bd9Sstevel@tonic-gate  * locale, but this case is straightforward.  Care is taken to correctly
387c478bd9Sstevel@tonic-gate  * use wide characters in order to print results properly.
397c478bd9Sstevel@tonic-gate  *
407c478bd9Sstevel@tonic-gate  * 2. pargs and target run in different locales: in this case, pargs examines
417c478bd9Sstevel@tonic-gate  * the string having assumed the victim's locale.  Unprintable (but valid)
427c478bd9Sstevel@tonic-gate  * characters are escaped.  Next, iconv(3c) is used to convert between the
437c478bd9Sstevel@tonic-gate  * target and pargs codeset.  Finally, a second pass to escape unprintable
447c478bd9Sstevel@tonic-gate  * (but valid) characters is made.
457c478bd9Sstevel@tonic-gate  *
467c478bd9Sstevel@tonic-gate  * In any case in which characters are encountered which are not valid in
477c478bd9Sstevel@tonic-gate  * their purported locale, the string "fails" and is treated as a traditional
487c478bd9Sstevel@tonic-gate  * 7-bit ASCII encoded string, and escaped accordingly.
497c478bd9Sstevel@tonic-gate  */
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate #include <stdio.h>
527c478bd9Sstevel@tonic-gate #include <stdlib.h>
537c478bd9Sstevel@tonic-gate #include <locale.h>
547c478bd9Sstevel@tonic-gate #include <wchar.h>
557c478bd9Sstevel@tonic-gate #include <iconv.h>
567c478bd9Sstevel@tonic-gate #include <langinfo.h>
577c478bd9Sstevel@tonic-gate #include <unistd.h>
587c478bd9Sstevel@tonic-gate #include <ctype.h>
597c478bd9Sstevel@tonic-gate #include <fcntl.h>
607c478bd9Sstevel@tonic-gate #include <string.h>
617c478bd9Sstevel@tonic-gate #include <strings.h>
627c478bd9Sstevel@tonic-gate #include <limits.h>
637c478bd9Sstevel@tonic-gate #include <pwd.h>
647c478bd9Sstevel@tonic-gate #include <grp.h>
657c478bd9Sstevel@tonic-gate #include <errno.h>
667c478bd9Sstevel@tonic-gate #include <setjmp.h>
677c478bd9Sstevel@tonic-gate #include <sys/types.h>
687c478bd9Sstevel@tonic-gate #include <sys/auxv.h>
697c478bd9Sstevel@tonic-gate #include <sys/archsystm.h>
707c478bd9Sstevel@tonic-gate #include <sys/proc.h>
717c478bd9Sstevel@tonic-gate #include <sys/elf.h>
727c478bd9Sstevel@tonic-gate #include <libproc.h>
737c478bd9Sstevel@tonic-gate #include <wctype.h>
747c478bd9Sstevel@tonic-gate #include <widec.h>
757c478bd9Sstevel@tonic-gate #include <elfcap.h>
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate typedef struct pargs_data {
787c478bd9Sstevel@tonic-gate 	struct ps_prochandle *pd_proc;	/* target proc handle */
797c478bd9Sstevel@tonic-gate 	psinfo_t *pd_psinfo;		/* target psinfo */
807c478bd9Sstevel@tonic-gate 	char *pd_locale;		/* target process locale */
817c478bd9Sstevel@tonic-gate 	int pd_conv_flags;		/* flags governing string conversion */
827c478bd9Sstevel@tonic-gate 	iconv_t pd_iconv;		/* iconv conversion descriptor */
837c478bd9Sstevel@tonic-gate 	size_t pd_argc;
847c478bd9Sstevel@tonic-gate 	uintptr_t *pd_argv;
857c478bd9Sstevel@tonic-gate 	char **pd_argv_strs;
867c478bd9Sstevel@tonic-gate 	size_t pd_envc;
877365cab2SMarcel Telka 	size_t pd_env_space;
887c478bd9Sstevel@tonic-gate 	uintptr_t *pd_envp;
897c478bd9Sstevel@tonic-gate 	char **pd_envp_strs;
907c478bd9Sstevel@tonic-gate 	size_t pd_auxc;
917c478bd9Sstevel@tonic-gate 	auxv_t *pd_auxv;
927c478bd9Sstevel@tonic-gate 	char **pd_auxv_strs;
937c478bd9Sstevel@tonic-gate 	char *pd_execname;
947c478bd9Sstevel@tonic-gate } pargs_data_t;
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate #define	CONV_USE_ICONV		0x01
977c478bd9Sstevel@tonic-gate #define	CONV_STRICT_ASCII	0x02
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate static char *command;
1007c478bd9Sstevel@tonic-gate static int dmodel;
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate #define	EXTRACT_BUFSZ 128		/* extract_string() initial size */
1037c478bd9Sstevel@tonic-gate #define	ENV_CHUNK 16			/* #env ptrs to read at a time */
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate static jmp_buf env;			/* malloc failure handling */
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate static void *
safe_zalloc(size_t size)1087c478bd9Sstevel@tonic-gate safe_zalloc(size_t size)
1097c478bd9Sstevel@tonic-gate {
1107c478bd9Sstevel@tonic-gate 	void *p;
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 	/*
1137c478bd9Sstevel@tonic-gate 	 * If the malloc fails we longjmp out to allow the code to Prelease()
1147c478bd9Sstevel@tonic-gate 	 * a stopped victim if needed.
1157c478bd9Sstevel@tonic-gate 	 */
1167c478bd9Sstevel@tonic-gate 	if ((p = malloc(size)) == NULL) {
1177c478bd9Sstevel@tonic-gate 		longjmp(env, errno);
1187c478bd9Sstevel@tonic-gate 	}
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 	bzero(p, size);
1217c478bd9Sstevel@tonic-gate 	return (p);
1227c478bd9Sstevel@tonic-gate }
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate static char *
safe_strdup(const char * s1)1257c478bd9Sstevel@tonic-gate safe_strdup(const char *s1)
1267c478bd9Sstevel@tonic-gate {
1277c478bd9Sstevel@tonic-gate 	char	*s2;
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	s2 = safe_zalloc(strlen(s1) + 1);
1307c478bd9Sstevel@tonic-gate 	(void) strcpy(s2, s1);
1317c478bd9Sstevel@tonic-gate 	return (s2);
1327c478bd9Sstevel@tonic-gate }
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate /*
1357c478bd9Sstevel@tonic-gate  * Given a wchar_t which might represent an 'escapable' sequence (see
1367c478bd9Sstevel@tonic-gate  * formats(5)), return the base ascii character needed to print that
1377c478bd9Sstevel@tonic-gate  * sequence.
1387c478bd9Sstevel@tonic-gate  *
1397c478bd9Sstevel@tonic-gate  * The comparisons performed may look suspect at first, but all are valid;
1407c478bd9Sstevel@tonic-gate  * the characters below all appear in the "Portable Character Set."  The
1417c478bd9Sstevel@tonic-gate  * Single Unix Spec says: "The wide-character value for each member of the
1427c478bd9Sstevel@tonic-gate  * Portable Character Set will equal its value when used as the lone
1437c478bd9Sstevel@tonic-gate  * character in an integer character constant."
1447c478bd9Sstevel@tonic-gate  */
1457c478bd9Sstevel@tonic-gate static uchar_t
get_interp_char(wchar_t wc)1467c478bd9Sstevel@tonic-gate get_interp_char(wchar_t wc)
1477c478bd9Sstevel@tonic-gate {
1487c478bd9Sstevel@tonic-gate 	switch (wc) {
1497c478bd9Sstevel@tonic-gate 	case L'\a':
1507c478bd9Sstevel@tonic-gate 		return ('a');
1517c478bd9Sstevel@tonic-gate 	case L'\b':
1527c478bd9Sstevel@tonic-gate 		return ('b');
1537c478bd9Sstevel@tonic-gate 	case L'\f':
1547c478bd9Sstevel@tonic-gate 		return ('f');
1557c478bd9Sstevel@tonic-gate 	case L'\n':
1567c478bd9Sstevel@tonic-gate 		return ('n');
1577c478bd9Sstevel@tonic-gate 	case L'\r':
1587c478bd9Sstevel@tonic-gate 		return ('r');
1597c478bd9Sstevel@tonic-gate 	case L'\t':
1607c478bd9Sstevel@tonic-gate 		return ('t');
1617c478bd9Sstevel@tonic-gate 	case L'\v':
1627c478bd9Sstevel@tonic-gate 		return ('v');
1637c478bd9Sstevel@tonic-gate 	case L'\\':
1647c478bd9Sstevel@tonic-gate 		return ('\\');
1657c478bd9Sstevel@tonic-gate 	}
1667c478bd9Sstevel@tonic-gate 	return ('\0');
1677c478bd9Sstevel@tonic-gate }
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate static char *
unctrl_str_strict_ascii(const char * src,int escape_slash,int * unprintable)1707c478bd9Sstevel@tonic-gate unctrl_str_strict_ascii(const char *src, int escape_slash, int *unprintable)
1717c478bd9Sstevel@tonic-gate {
1727c478bd9Sstevel@tonic-gate 	uchar_t *uc, *ucp, c, ic;
1737c478bd9Sstevel@tonic-gate 	uc = ucp = safe_zalloc((strlen(src) * 4) + 1);
1747c478bd9Sstevel@tonic-gate 	while ((c = *src++) != '\0') {
1757c478bd9Sstevel@tonic-gate 		/*
1767c478bd9Sstevel@tonic-gate 		 * Call get_interp_char *first*, since \ will otherwise not
1777c478bd9Sstevel@tonic-gate 		 * be escaped as \\.
1787c478bd9Sstevel@tonic-gate 		 */
1797c478bd9Sstevel@tonic-gate 		if ((ic = get_interp_char((wchar_t)c)) != '\0') {
1807c478bd9Sstevel@tonic-gate 			if (escape_slash || ic != '\\')
1817c478bd9Sstevel@tonic-gate 				*ucp++ = '\\';
1827c478bd9Sstevel@tonic-gate 			*ucp++ = ic;
1837c478bd9Sstevel@tonic-gate 		} else if (isascii(c) && isprint(c)) {
1847c478bd9Sstevel@tonic-gate 			*ucp++ = c;
1857c478bd9Sstevel@tonic-gate 		} else {
1867c478bd9Sstevel@tonic-gate 			*ucp++ = '\\';
1877c478bd9Sstevel@tonic-gate 			*ucp++ = ((c >> 6) & 7) + '0';
1887c478bd9Sstevel@tonic-gate 			*ucp++ = ((c >> 3) & 7) + '0';
1897c478bd9Sstevel@tonic-gate 			*ucp++ = (c & 7) + '0';
1907c478bd9Sstevel@tonic-gate 			*unprintable = 1;
1917c478bd9Sstevel@tonic-gate 		}
1927c478bd9Sstevel@tonic-gate 	}
1937c478bd9Sstevel@tonic-gate 	*ucp = '\0';
1947c478bd9Sstevel@tonic-gate 	return ((char *)uc);
1957c478bd9Sstevel@tonic-gate }
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate /*
1987c478bd9Sstevel@tonic-gate  * Convert control characters as described in format(5) to their readable
1997c478bd9Sstevel@tonic-gate  * representation; special care is taken to handle multibyte character sets.
2007c478bd9Sstevel@tonic-gate  *
2017c478bd9Sstevel@tonic-gate  * If escape_slash is true, escaping of '\' occurs.  The first time a string
2027c478bd9Sstevel@tonic-gate  * is unctrl'd, this should be '1'.  Subsequent iterations over the same
2037c478bd9Sstevel@tonic-gate  * string should set escape_slash to 0.  Otherwise you'll wind up with
2047c478bd9Sstevel@tonic-gate  * \ --> \\ --> \\\\.
2057c478bd9Sstevel@tonic-gate  */
2067c478bd9Sstevel@tonic-gate static char *
unctrl_str(const char * src,int escape_slash,int * unprintable)2077c478bd9Sstevel@tonic-gate unctrl_str(const char *src, int escape_slash, int *unprintable)
2087c478bd9Sstevel@tonic-gate {
2097c478bd9Sstevel@tonic-gate 	wchar_t wc;
2107c478bd9Sstevel@tonic-gate 	wchar_t *wide_src, *wide_srcp;
2117c478bd9Sstevel@tonic-gate 	wchar_t *wide_dest, *wide_destp;
2127c478bd9Sstevel@tonic-gate 	char *uc;
2137c478bd9Sstevel@tonic-gate 	size_t srcbufsz = strlen(src) + 1;
2147c478bd9Sstevel@tonic-gate 	size_t destbufsz = srcbufsz * 4;
2157c478bd9Sstevel@tonic-gate 	size_t srclen, destlen;
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	wide_srcp = wide_src = safe_zalloc(srcbufsz * sizeof (wchar_t));
2187c478bd9Sstevel@tonic-gate 	wide_destp = wide_dest = safe_zalloc(destbufsz * sizeof (wchar_t));
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	if ((srclen = mbstowcs(wide_src, src, srcbufsz - 1)) == (size_t)-1) {
2217c478bd9Sstevel@tonic-gate 		/*
2227c478bd9Sstevel@tonic-gate 		 * We can't trust the string, since in the locale in which
2237c478bd9Sstevel@tonic-gate 		 * this call is operating, the string contains an invalid
2247c478bd9Sstevel@tonic-gate 		 * multibyte sequence.  There isn't much to do here, so
2257c478bd9Sstevel@tonic-gate 		 * convert the string byte by byte to wide characters, as
2267c478bd9Sstevel@tonic-gate 		 * if it came from a C locale (char) string.  This isn't
2277c478bd9Sstevel@tonic-gate 		 * perfect, but at least the characters will make it to
2287c478bd9Sstevel@tonic-gate 		 * the screen.
2297c478bd9Sstevel@tonic-gate 		 */
2307c478bd9Sstevel@tonic-gate 		free(wide_src);
2317c478bd9Sstevel@tonic-gate 		free(wide_dest);
2327c478bd9Sstevel@tonic-gate 		return (unctrl_str_strict_ascii(src, escape_slash,
2337c478bd9Sstevel@tonic-gate 		    unprintable));
2347c478bd9Sstevel@tonic-gate 	}
2357c478bd9Sstevel@tonic-gate 	if (srclen == (srcbufsz - 1)) {
2367c478bd9Sstevel@tonic-gate 		wide_src[srclen] = L'\0';
2377c478bd9Sstevel@tonic-gate 	}
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 	while ((wc = *wide_srcp++) != L'\0') {
2407c478bd9Sstevel@tonic-gate 		char cvt_buf[MB_LEN_MAX];
2417c478bd9Sstevel@tonic-gate 		int len, i;
2427c478bd9Sstevel@tonic-gate 		char c = get_interp_char(wc);
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 		if ((c != '\0') && (escape_slash || c != '\\')) {
2457c478bd9Sstevel@tonic-gate 			/*
2467c478bd9Sstevel@tonic-gate 			 * Print "interpreted version" (\n, \a, etc).
2477c478bd9Sstevel@tonic-gate 			 */
2487c478bd9Sstevel@tonic-gate 			*wide_destp++ = L'\\';
2497c478bd9Sstevel@tonic-gate 			*wide_destp++ = (wchar_t)c;
2507c478bd9Sstevel@tonic-gate 			continue;
2517c478bd9Sstevel@tonic-gate 		}
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 		if (iswprint(wc)) {
2547c478bd9Sstevel@tonic-gate 			*wide_destp++ = wc;
2557c478bd9Sstevel@tonic-gate 			continue;
2567c478bd9Sstevel@tonic-gate 		}
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 		/*
2597c478bd9Sstevel@tonic-gate 		 * Convert the wide char back into (potentially several)
2607c478bd9Sstevel@tonic-gate 		 * multibyte characters, then escape out each of those bytes.
2617c478bd9Sstevel@tonic-gate 		 */
2627c478bd9Sstevel@tonic-gate 		bzero(cvt_buf, sizeof (cvt_buf));
2637c478bd9Sstevel@tonic-gate 		if ((len = wctomb(cvt_buf, wc)) == -1) {
2647c478bd9Sstevel@tonic-gate 			/*
2657c478bd9Sstevel@tonic-gate 			 * This is a totally invalid wide char; discard it.
2667c478bd9Sstevel@tonic-gate 			 */
2677c478bd9Sstevel@tonic-gate 			continue;
2687c478bd9Sstevel@tonic-gate 		}
2697c478bd9Sstevel@tonic-gate 		for (i = 0; i < len; i++) {
2707c478bd9Sstevel@tonic-gate 			uchar_t c = cvt_buf[i];
2717c478bd9Sstevel@tonic-gate 			*wide_destp++ = L'\\';
2727c478bd9Sstevel@tonic-gate 			*wide_destp++ = (wchar_t)('0' + ((c >> 6) & 7));
2737c478bd9Sstevel@tonic-gate 			*wide_destp++ = (wchar_t)('0' + ((c >> 3) & 7));
2747c478bd9Sstevel@tonic-gate 			*wide_destp++ = (wchar_t)('0' + (c & 7));
2757c478bd9Sstevel@tonic-gate 			*unprintable = 1;
2767c478bd9Sstevel@tonic-gate 		}
2777c478bd9Sstevel@tonic-gate 	}
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 	*wide_destp = '\0';
2807c478bd9Sstevel@tonic-gate 	destlen = (wide_destp - wide_dest) * MB_CUR_MAX + 1;
2817c478bd9Sstevel@tonic-gate 	uc = safe_zalloc(destlen);
2827c478bd9Sstevel@tonic-gate 	if (wcstombs(uc, wide_dest, destlen) == (size_t)-1) {
2837c478bd9Sstevel@tonic-gate 		/* If we've gotten this far, wcstombs shouldn't fail... */
2847c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: wcstombs failed unexpectedly: %s\n",
2857c478bd9Sstevel@tonic-gate 		    command, strerror(errno));
2867c478bd9Sstevel@tonic-gate 		exit(1);
2877c478bd9Sstevel@tonic-gate 	} else {
2887c478bd9Sstevel@tonic-gate 		char *tmp;
2897c478bd9Sstevel@tonic-gate 		/*
2907c478bd9Sstevel@tonic-gate 		 * Try to save memory; don't waste 3 * strlen in the
2917c478bd9Sstevel@tonic-gate 		 * common case.
2927c478bd9Sstevel@tonic-gate 		 */
2937c478bd9Sstevel@tonic-gate 		tmp = safe_strdup(uc);
2947c478bd9Sstevel@tonic-gate 		free(uc);
2957c478bd9Sstevel@tonic-gate 		uc = tmp;
2967c478bd9Sstevel@tonic-gate 	}
2977c478bd9Sstevel@tonic-gate 	free(wide_dest);
2987c478bd9Sstevel@tonic-gate 	free(wide_src);
2997c478bd9Sstevel@tonic-gate 	return (uc);
3007c478bd9Sstevel@tonic-gate }
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate /*
3037c478bd9Sstevel@tonic-gate  * These functions determine which characters are safe to be left unquoted.
3047c478bd9Sstevel@tonic-gate  * Rather than starting with every printable character and subtracting out the
3057c478bd9Sstevel@tonic-gate  * shell metacharacters, we take the more conservative approach of starting with
3067c478bd9Sstevel@tonic-gate  * a set of safe characters and adding those few common punctuation characters
3077c478bd9Sstevel@tonic-gate  * which are known to be safe.  The rules are:
3087c478bd9Sstevel@tonic-gate  *
3097c478bd9Sstevel@tonic-gate  * 	If this is a printable character (graph), and not punctuation, it is
3107c478bd9Sstevel@tonic-gate  * 	safe to leave unquoted.
3117c478bd9Sstevel@tonic-gate  *
3127c478bd9Sstevel@tonic-gate  * 	If it's one of known hard-coded safe characters, it's also safe to leave
3137c478bd9Sstevel@tonic-gate  * 	unquoted.
3147c478bd9Sstevel@tonic-gate  *
3157c478bd9Sstevel@tonic-gate  * 	Otherwise, the entire argument must be quoted.
3167c478bd9Sstevel@tonic-gate  *
3177c478bd9Sstevel@tonic-gate  * This will cause some strings to be unecessarily quoted, but it is safer than
3187c478bd9Sstevel@tonic-gate  * having a character unintentionally interpreted by the shell.
3197c478bd9Sstevel@tonic-gate  */
3207c478bd9Sstevel@tonic-gate static int
issafe_ascii(char c)3217c478bd9Sstevel@tonic-gate issafe_ascii(char c)
3227c478bd9Sstevel@tonic-gate {
3237c478bd9Sstevel@tonic-gate 	return (isalnum(c) || strchr("_.-/@:,", c) != NULL);
3247c478bd9Sstevel@tonic-gate }
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate static int
issafe(wchar_t wc)3277c478bd9Sstevel@tonic-gate issafe(wchar_t wc)
3287c478bd9Sstevel@tonic-gate {
3297c478bd9Sstevel@tonic-gate 	return ((iswgraph(wc) && !iswpunct(wc)) ||
3307c478bd9Sstevel@tonic-gate 	    wschr(L"_.-/@:,", wc) != NULL);
3317c478bd9Sstevel@tonic-gate }
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3347c478bd9Sstevel@tonic-gate static char *
quote_string_ascii(pargs_data_t * datap,char * src)3357c478bd9Sstevel@tonic-gate quote_string_ascii(pargs_data_t *datap, char *src)
3367c478bd9Sstevel@tonic-gate {
3377c478bd9Sstevel@tonic-gate 	char *dst;
3387c478bd9Sstevel@tonic-gate 	int quote_count = 0;
3397c478bd9Sstevel@tonic-gate 	int need_quote = 0;
3407c478bd9Sstevel@tonic-gate 	char *srcp, *dstp;
3417c478bd9Sstevel@tonic-gate 	size_t dstlen;
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 	for (srcp = src; *srcp != '\0'; srcp++) {
3447c478bd9Sstevel@tonic-gate 		if (!issafe_ascii(*srcp)) {
3457c478bd9Sstevel@tonic-gate 			need_quote = 1;
3467c478bd9Sstevel@tonic-gate 			if (*srcp == '\'')
3477c478bd9Sstevel@tonic-gate 				quote_count++;
3487c478bd9Sstevel@tonic-gate 		}
3497c478bd9Sstevel@tonic-gate 	}
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 	if (!need_quote)
3527c478bd9Sstevel@tonic-gate 		return (src);
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 	/*
3557c478bd9Sstevel@tonic-gate 	 * The only character we care about here is a single quote.  All the
3567c478bd9Sstevel@tonic-gate 	 * other unprintable characters (and backslashes) will have been dealt
3577c478bd9Sstevel@tonic-gate 	 * with by unctrl_str().  We make the following subtitution when we
3587c478bd9Sstevel@tonic-gate 	 * encounter a single quote:
3597c478bd9Sstevel@tonic-gate 	 *
3607c478bd9Sstevel@tonic-gate 	 * 	' = '"'"'
3617c478bd9Sstevel@tonic-gate 	 *
3627c478bd9Sstevel@tonic-gate 	 * In addition, we put single quotes around the entire argument.  For
3637c478bd9Sstevel@tonic-gate 	 * example:
3647c478bd9Sstevel@tonic-gate 	 *
3657c478bd9Sstevel@tonic-gate 	 * 	foo'bar = 'foo'"'"'bar'
3667c478bd9Sstevel@tonic-gate 	 */
3677c478bd9Sstevel@tonic-gate 	dstlen = strlen(src) + 3 + 4 * quote_count;
3687c478bd9Sstevel@tonic-gate 	dst = safe_zalloc(dstlen);
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 	dstp = dst;
3717c478bd9Sstevel@tonic-gate 	*dstp++ = '\'';
3727c478bd9Sstevel@tonic-gate 	for (srcp = src; *srcp != '\0'; srcp++, dstp++) {
3737c478bd9Sstevel@tonic-gate 		*dstp = *srcp;
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 		if (*srcp == '\'') {
3767c478bd9Sstevel@tonic-gate 			dstp[1] = '"';
3777c478bd9Sstevel@tonic-gate 			dstp[2] = '\'';
3787c478bd9Sstevel@tonic-gate 			dstp[3] = '"';
3797c478bd9Sstevel@tonic-gate 			dstp[4] = '\'';
3807c478bd9Sstevel@tonic-gate 			dstp += 4;
3817c478bd9Sstevel@tonic-gate 		}
3827c478bd9Sstevel@tonic-gate 	}
3837c478bd9Sstevel@tonic-gate 	*dstp++ = '\'';
3847c478bd9Sstevel@tonic-gate 	*dstp = '\0';
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate 	free(src);
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate 	return (dst);
3897c478bd9Sstevel@tonic-gate }
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate static char *
quote_string(pargs_data_t * datap,char * src)3927c478bd9Sstevel@tonic-gate quote_string(pargs_data_t *datap, char *src)
3937c478bd9Sstevel@tonic-gate {
3947c478bd9Sstevel@tonic-gate 	wchar_t *wide_src, *wide_srcp;
3957c478bd9Sstevel@tonic-gate 	wchar_t *wide_dest, *wide_destp;
3967c478bd9Sstevel@tonic-gate 	char *uc;
3977c478bd9Sstevel@tonic-gate 	size_t srcbufsz = strlen(src) + 1;
3987c478bd9Sstevel@tonic-gate 	size_t srclen;
3997c478bd9Sstevel@tonic-gate 	size_t destbufsz;
4007c478bd9Sstevel@tonic-gate 	size_t destlen;
4017c478bd9Sstevel@tonic-gate 	int quote_count = 0;
4027c478bd9Sstevel@tonic-gate 	int need_quote = 0;
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 	if (datap->pd_conv_flags & CONV_STRICT_ASCII)
4057c478bd9Sstevel@tonic-gate 		return (quote_string_ascii(datap, src));
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate 	wide_srcp = wide_src = safe_zalloc(srcbufsz * sizeof (wchar_t));
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate 	if ((srclen = mbstowcs(wide_src, src, srcbufsz - 1)) == (size_t)-1) {
4107c478bd9Sstevel@tonic-gate 		free(wide_src);
4117c478bd9Sstevel@tonic-gate 		return (quote_string_ascii(datap, src));
4127c478bd9Sstevel@tonic-gate 	}
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 	if (srclen == srcbufsz - 1)
4157c478bd9Sstevel@tonic-gate 		wide_src[srclen] = L'\0';
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 	for (wide_srcp = wide_src; *wide_srcp != '\0'; wide_srcp++) {
4187c478bd9Sstevel@tonic-gate 		if (!issafe(*wide_srcp)) {
4197c478bd9Sstevel@tonic-gate 			need_quote = 1;
4207c478bd9Sstevel@tonic-gate 			if (*wide_srcp == L'\'')
4217c478bd9Sstevel@tonic-gate 				quote_count++;
4227c478bd9Sstevel@tonic-gate 		}
4237c478bd9Sstevel@tonic-gate 	}
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 	if (!need_quote) {
4267c478bd9Sstevel@tonic-gate 		free(wide_src);
4277c478bd9Sstevel@tonic-gate 		return (src);
4287c478bd9Sstevel@tonic-gate 	}
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate 	/*
4317c478bd9Sstevel@tonic-gate 	 * See comment for quote_string_ascii(), above.
4327c478bd9Sstevel@tonic-gate 	 */
4337c478bd9Sstevel@tonic-gate 	destbufsz = srcbufsz + 3 + 4 * quote_count;
4347c478bd9Sstevel@tonic-gate 	wide_destp = wide_dest = safe_zalloc(destbufsz * sizeof (wchar_t));
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate 	*wide_destp++ = L'\'';
4377c478bd9Sstevel@tonic-gate 	for (wide_srcp = wide_src; *wide_srcp != L'\0';
4387c478bd9Sstevel@tonic-gate 	    wide_srcp++, wide_destp++) {
4397c478bd9Sstevel@tonic-gate 		*wide_destp = *wide_srcp;
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate 		if (*wide_srcp == L'\'') {
4427c478bd9Sstevel@tonic-gate 			wide_destp[1] = L'"';
4437c478bd9Sstevel@tonic-gate 			wide_destp[2] = L'\'';
4447c478bd9Sstevel@tonic-gate 			wide_destp[3] = L'"';
4457c478bd9Sstevel@tonic-gate 			wide_destp[4] = L'\'';
4467c478bd9Sstevel@tonic-gate 			wide_destp += 4;
4477c478bd9Sstevel@tonic-gate 		}
4487c478bd9Sstevel@tonic-gate 	}
4497c478bd9Sstevel@tonic-gate 	*wide_destp++ = L'\'';
4507c478bd9Sstevel@tonic-gate 	*wide_destp = L'\0';
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate 	destlen = destbufsz * MB_CUR_MAX + 1;
4537c478bd9Sstevel@tonic-gate 	uc = safe_zalloc(destlen);
4547c478bd9Sstevel@tonic-gate 	if (wcstombs(uc, wide_dest, destlen) == (size_t)-1) {
4557c478bd9Sstevel@tonic-gate 		/* If we've gotten this far, wcstombs shouldn't fail... */
4567c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: wcstombs failed unexpectedly: %s\n",
4577c478bd9Sstevel@tonic-gate 		    command, strerror(errno));
4587c478bd9Sstevel@tonic-gate 		exit(1);
4597c478bd9Sstevel@tonic-gate 	}
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 	free(wide_dest);
4627c478bd9Sstevel@tonic-gate 	free(wide_src);
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 	return (uc);
4657c478bd9Sstevel@tonic-gate }
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate /*
4697c478bd9Sstevel@tonic-gate  * Determine the locale of the target process by traversing its environment,
4707c478bd9Sstevel@tonic-gate  * making only one pass for efficiency's sake; stash the result in
4717c478bd9Sstevel@tonic-gate  * datap->pd_locale.
4727c478bd9Sstevel@tonic-gate  *
4737c478bd9Sstevel@tonic-gate  * It's possible that the process has called setlocale() to change its
4747c478bd9Sstevel@tonic-gate  * locale to something different, but we mostly care about making a good
4757c478bd9Sstevel@tonic-gate  * guess as to the locale at exec(2) time.
4767c478bd9Sstevel@tonic-gate  */
4777c478bd9Sstevel@tonic-gate static void
lookup_locale(pargs_data_t * datap)4787c478bd9Sstevel@tonic-gate lookup_locale(pargs_data_t *datap)
4797c478bd9Sstevel@tonic-gate {
4807c478bd9Sstevel@tonic-gate 	int i, j, composite = 0;
4817c478bd9Sstevel@tonic-gate 	size_t	len = 0;
4827c478bd9Sstevel@tonic-gate 	char	*pd_locale;
4837c478bd9Sstevel@tonic-gate 	char	*lc_all = NULL, *lang = NULL;
4847c478bd9Sstevel@tonic-gate 	char	*lcs[] = { NULL, NULL, NULL, NULL, NULL, NULL };
4857c478bd9Sstevel@tonic-gate 	static const char *cat_names[] = {
4867c478bd9Sstevel@tonic-gate 		"LC_CTYPE=",	"LC_NUMERIC=",	"LC_TIME=",
4877c478bd9Sstevel@tonic-gate 		"LC_COLLATE=",	"LC_MONETARY=",	"LC_MESSAGES="
4887c478bd9Sstevel@tonic-gate 	};
4897c478bd9Sstevel@tonic-gate 
4907c478bd9Sstevel@tonic-gate 	for (i = 0; i < datap->pd_envc; i++) {
4917c478bd9Sstevel@tonic-gate 		char *s = datap->pd_envp_strs[i];
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate 		if (s == NULL)
4947c478bd9Sstevel@tonic-gate 			continue;
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate 		if (strncmp("LC_ALL=", s, strlen("LC_ALL=")) == 0) {
4977c478bd9Sstevel@tonic-gate 			/*
4987c478bd9Sstevel@tonic-gate 			 * Minor optimization-- if we find LC_ALL we're done.
4997c478bd9Sstevel@tonic-gate 			 */
5007c478bd9Sstevel@tonic-gate 			lc_all = s + strlen("LC_ALL=");
5017c478bd9Sstevel@tonic-gate 			break;
5027c478bd9Sstevel@tonic-gate 		}
5037c478bd9Sstevel@tonic-gate 		for (j = 0; j <= _LastCategory; j++) {
5047c478bd9Sstevel@tonic-gate 			if (strncmp(cat_names[j], s,
5057c478bd9Sstevel@tonic-gate 			    strlen(cat_names[j])) == 0) {
5067c478bd9Sstevel@tonic-gate 				lcs[j] = s + strlen(cat_names[j]);
5077c478bd9Sstevel@tonic-gate 			}
5087c478bd9Sstevel@tonic-gate 		}
5097c478bd9Sstevel@tonic-gate 		if (strncmp("LANG=", s, strlen("LANG=")) == 0) {
5107c478bd9Sstevel@tonic-gate 			lang = s + strlen("LANG=");
5117c478bd9Sstevel@tonic-gate 		}
5127c478bd9Sstevel@tonic-gate 	}
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate 	if (lc_all && (*lc_all == '\0'))
5157c478bd9Sstevel@tonic-gate 		lc_all = NULL;
5167c478bd9Sstevel@tonic-gate 	if (lang && (*lang == '\0'))
5177c478bd9Sstevel@tonic-gate 		lang = NULL;
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate 	for (i = 0; i <= _LastCategory; i++) {
5207c478bd9Sstevel@tonic-gate 		if (lc_all != NULL) {
5217c478bd9Sstevel@tonic-gate 			lcs[i] = lc_all;
5227c478bd9Sstevel@tonic-gate 		} else if (lcs[i] != NULL) {
5237c478bd9Sstevel@tonic-gate 			lcs[i] = lcs[i];
5247c478bd9Sstevel@tonic-gate 		} else if (lang != NULL) {
5257c478bd9Sstevel@tonic-gate 			lcs[i] = lang;
5267c478bd9Sstevel@tonic-gate 		} else {
5277c478bd9Sstevel@tonic-gate 			lcs[i] = "C";
5287c478bd9Sstevel@tonic-gate 		}
5297c478bd9Sstevel@tonic-gate 		if ((i > 0) && (lcs[i] != lcs[i-1]))
5307c478bd9Sstevel@tonic-gate 			composite++;
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 		len += 1 + strlen(lcs[i]);	/* 1 extra byte for '/' */
5337c478bd9Sstevel@tonic-gate 	}
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 	if (composite == 0) {
5367c478bd9Sstevel@tonic-gate 		/* simple locale */
5377c478bd9Sstevel@tonic-gate 		pd_locale = safe_strdup(lcs[0]);
5387c478bd9Sstevel@tonic-gate 	} else {
5397c478bd9Sstevel@tonic-gate 		/* composite locale */
5407c478bd9Sstevel@tonic-gate 		pd_locale = safe_zalloc(len + 1);
5417c478bd9Sstevel@tonic-gate 		(void) snprintf(pd_locale, len + 1, "/%s/%s/%s/%s/%s/%s",
5427c478bd9Sstevel@tonic-gate 		    lcs[0], lcs[1], lcs[2], lcs[3], lcs[4], lcs[5]);
5437c478bd9Sstevel@tonic-gate 	}
5447c478bd9Sstevel@tonic-gate 	datap->pd_locale = pd_locale;
5457c478bd9Sstevel@tonic-gate }
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate /*
5487c478bd9Sstevel@tonic-gate  * Pull a string from the victim, regardless of size; this routine allocates
5497c478bd9Sstevel@tonic-gate  * memory for the string which must be freed by the caller.
5507c478bd9Sstevel@tonic-gate  */
5517c478bd9Sstevel@tonic-gate static char *
extract_string(pargs_data_t * datap,uintptr_t addr)5527c478bd9Sstevel@tonic-gate extract_string(pargs_data_t *datap, uintptr_t addr)
5537c478bd9Sstevel@tonic-gate {
5547c478bd9Sstevel@tonic-gate 	int size = EXTRACT_BUFSZ;
5557c478bd9Sstevel@tonic-gate 	char *result;
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate 	result = safe_zalloc(size);
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate 	for (;;) {
5607c478bd9Sstevel@tonic-gate 		if (Pread_string(datap->pd_proc, result, size, addr) < 0) {
5617c478bd9Sstevel@tonic-gate 			free(result);
5627c478bd9Sstevel@tonic-gate 			return (NULL);
5637c478bd9Sstevel@tonic-gate 		} else if (strlen(result) == (size - 1)) {
5647c478bd9Sstevel@tonic-gate 			free(result);
5657c478bd9Sstevel@tonic-gate 			size *= 2;
5667c478bd9Sstevel@tonic-gate 			result = safe_zalloc(size);
5677c478bd9Sstevel@tonic-gate 		} else {
5687c478bd9Sstevel@tonic-gate 			break;
5697c478bd9Sstevel@tonic-gate 		}
5707c478bd9Sstevel@tonic-gate 	}
5717c478bd9Sstevel@tonic-gate 	return (result);
5727c478bd9Sstevel@tonic-gate }
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate /*
5757c478bd9Sstevel@tonic-gate  * Utility function to read an array of pointers from the victim, adjusting
5767c478bd9Sstevel@tonic-gate  * for victim data model; returns the number of bytes successfully read.
5777c478bd9Sstevel@tonic-gate  */
5787c478bd9Sstevel@tonic-gate static ssize_t
read_ptr_array(pargs_data_t * datap,uintptr_t offset,uintptr_t * buf,size_t nelems)5797c478bd9Sstevel@tonic-gate read_ptr_array(pargs_data_t *datap, uintptr_t offset, uintptr_t *buf,
5807c478bd9Sstevel@tonic-gate     size_t nelems)
5817c478bd9Sstevel@tonic-gate {
5827c478bd9Sstevel@tonic-gate 	ssize_t res;
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate 	if (dmodel == PR_MODEL_NATIVE) {
5857c478bd9Sstevel@tonic-gate 		res = Pread(datap->pd_proc, buf, nelems * sizeof (uintptr_t),
5867c478bd9Sstevel@tonic-gate 		    offset);
5877c478bd9Sstevel@tonic-gate 	} else {
5887c478bd9Sstevel@tonic-gate 		int i;
5897c478bd9Sstevel@tonic-gate 		uint32_t *arr32 = safe_zalloc(nelems * sizeof (uint32_t));
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate 		res = Pread(datap->pd_proc, arr32, nelems * sizeof (uint32_t),
5927c478bd9Sstevel@tonic-gate 		    offset);
5937c478bd9Sstevel@tonic-gate 		if (res > 0) {
5947c478bd9Sstevel@tonic-gate 			for (i = 0; i < nelems; i++)
5957c478bd9Sstevel@tonic-gate 				buf[i] = arr32[i];
5967c478bd9Sstevel@tonic-gate 		}
5977c478bd9Sstevel@tonic-gate 		free(arr32);
5987c478bd9Sstevel@tonic-gate 	}
5997c478bd9Sstevel@tonic-gate 	return (res);
6007c478bd9Sstevel@tonic-gate }
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate /*
6037c478bd9Sstevel@tonic-gate  * Extract the argv array from the victim; store the pointer values in
6047c478bd9Sstevel@tonic-gate  * datap->pd_argv and the extracted strings in datap->pd_argv_strs.
6057c478bd9Sstevel@tonic-gate  */
6067c478bd9Sstevel@tonic-gate static void
get_args(pargs_data_t * datap)6077c478bd9Sstevel@tonic-gate get_args(pargs_data_t *datap)
6087c478bd9Sstevel@tonic-gate {
6097c478bd9Sstevel@tonic-gate 	size_t argc = datap->pd_psinfo->pr_argc;
6107c478bd9Sstevel@tonic-gate 	uintptr_t argvoff = datap->pd_psinfo->pr_argv;
6117c478bd9Sstevel@tonic-gate 	int i;
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate 	datap->pd_argc = argc;
6147c478bd9Sstevel@tonic-gate 	datap->pd_argv = safe_zalloc(argc * sizeof (uintptr_t));
6157c478bd9Sstevel@tonic-gate 
6167c478bd9Sstevel@tonic-gate 	if (read_ptr_array(datap, argvoff, datap->pd_argv, argc) <= 0) {
6177c478bd9Sstevel@tonic-gate 		free(datap->pd_argv);
6187c478bd9Sstevel@tonic-gate 		datap->pd_argv = NULL;
6197c478bd9Sstevel@tonic-gate 		return;
6207c478bd9Sstevel@tonic-gate 	}
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate 	datap->pd_argv_strs = safe_zalloc(argc * sizeof (char *));
6237c478bd9Sstevel@tonic-gate 	for (i = 0; i < argc; i++) {
6247c478bd9Sstevel@tonic-gate 		if (datap->pd_argv[i] == 0)
6257c478bd9Sstevel@tonic-gate 			continue;
6267c478bd9Sstevel@tonic-gate 		datap->pd_argv_strs[i] = extract_string(datap,
6277c478bd9Sstevel@tonic-gate 		    datap->pd_argv[i]);
6287c478bd9Sstevel@tonic-gate 	}
6297c478bd9Sstevel@tonic-gate }
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate /*ARGSUSED*/
6327c478bd9Sstevel@tonic-gate static int
build_env(void * data,struct ps_prochandle * pr,uintptr_t addr,const char * str)6337c478bd9Sstevel@tonic-gate build_env(void *data, struct ps_prochandle *pr, uintptr_t addr, const char *str)
6347c478bd9Sstevel@tonic-gate {
6357c478bd9Sstevel@tonic-gate 	pargs_data_t *datap = data;
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate 	if (datap->pd_envp != NULL) {
6387365cab2SMarcel Telka 		if (datap->pd_envc == datap->pd_env_space) {
6397365cab2SMarcel Telka 			/*
6407365cab2SMarcel Telka 			 * Not enough space for storing the env (it has more
6417365cab2SMarcel Telka 			 * items than before).  Try to grow both arrays.
6427365cab2SMarcel Telka 			 */
6437365cab2SMarcel Telka 			void *new = realloc(datap->pd_envp,
6447365cab2SMarcel Telka 			    sizeof (uintptr_t) * datap->pd_env_space * 2);
6457365cab2SMarcel Telka 			if (new == NULL)
6467365cab2SMarcel Telka 				return (1);
6477365cab2SMarcel Telka 			datap->pd_envp = new;
6487365cab2SMarcel Telka 
6497365cab2SMarcel Telka 			new = realloc(datap->pd_envp_strs,
6507365cab2SMarcel Telka 			    sizeof (char *) * datap->pd_env_space * 2);
6517365cab2SMarcel Telka 			if (new == NULL)
6527365cab2SMarcel Telka 				return (1);
6537365cab2SMarcel Telka 			datap->pd_envp_strs = new;
6547365cab2SMarcel Telka 
6557365cab2SMarcel Telka 			datap->pd_env_space *= 2;
6567365cab2SMarcel Telka 		}
657ee6c11c2SSimon Klinkert 
6587c478bd9Sstevel@tonic-gate 		datap->pd_envp[datap->pd_envc] = addr;
6597c478bd9Sstevel@tonic-gate 		if (str == NULL)
6607c478bd9Sstevel@tonic-gate 			datap->pd_envp_strs[datap->pd_envc] = NULL;
6617c478bd9Sstevel@tonic-gate 		else
6627c478bd9Sstevel@tonic-gate 			datap->pd_envp_strs[datap->pd_envc] = strdup(str);
6637c478bd9Sstevel@tonic-gate 	}
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate 	datap->pd_envc++;
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate 	return (0);
6687c478bd9Sstevel@tonic-gate }
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate static void
get_env(pargs_data_t * datap)6717c478bd9Sstevel@tonic-gate get_env(pargs_data_t *datap)
6727c478bd9Sstevel@tonic-gate {
6737c478bd9Sstevel@tonic-gate 	struct ps_prochandle *pr = datap->pd_proc;
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate 	datap->pd_envc = 0;
6767c478bd9Sstevel@tonic-gate 	(void) Penv_iter(pr, build_env, datap);
6777c478bd9Sstevel@tonic-gate 
6787365cab2SMarcel Telka 	/* We must allocate space for at least one entry */
6797365cab2SMarcel Telka 	datap->pd_env_space = datap->pd_envc != 0 ? datap->pd_envc : 1;
6807365cab2SMarcel Telka 	datap->pd_envp = safe_zalloc(sizeof (uintptr_t) * datap->pd_env_space);
6817365cab2SMarcel Telka 	datap->pd_envp_strs =
6827365cab2SMarcel Telka 	    safe_zalloc(sizeof (char *) * datap->pd_env_space);
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate 	datap->pd_envc = 0;
6857c478bd9Sstevel@tonic-gate 	(void) Penv_iter(pr, build_env, datap);
6867c478bd9Sstevel@tonic-gate }
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate /*
6897c478bd9Sstevel@tonic-gate  * The following at_* routines are used to decode data from the aux vector.
6907c478bd9Sstevel@tonic-gate  */
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate /*ARGSUSED*/
6937c478bd9Sstevel@tonic-gate static void
at_null(long val,char * instr,size_t n,char * str)6947c478bd9Sstevel@tonic-gate at_null(long val, char *instr, size_t n, char *str)
6957c478bd9Sstevel@tonic-gate {
6967c478bd9Sstevel@tonic-gate 	str[0] = '\0';
6977c478bd9Sstevel@tonic-gate }
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate /*ARGSUSED*/
7007c478bd9Sstevel@tonic-gate static void
at_str(long val,char * instr,size_t n,char * str)7017c478bd9Sstevel@tonic-gate at_str(long val, char *instr, size_t n, char *str)
7027c478bd9Sstevel@tonic-gate {
7037c478bd9Sstevel@tonic-gate 	str[0] = '\0';
7047c478bd9Sstevel@tonic-gate 	if (instr != NULL) {
7057c478bd9Sstevel@tonic-gate 		(void) strlcpy(str, instr, n);
7067c478bd9Sstevel@tonic-gate 	}
7077c478bd9Sstevel@tonic-gate }
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate /*
7107c478bd9Sstevel@tonic-gate  * Note: Don't forget to add a corresponding case to isainfo(1).
7117c478bd9Sstevel@tonic-gate  */
7127c478bd9Sstevel@tonic-gate 
7137c478bd9Sstevel@tonic-gate #define	FMT_AV(s, n, hwcap, mask, name)				\
7147c478bd9Sstevel@tonic-gate 	if ((hwcap) & (mask)) 					\
7157c478bd9Sstevel@tonic-gate 		(void) snprintf(s, n, "%s" name " | ", s)
7167c478bd9Sstevel@tonic-gate 
7177c478bd9Sstevel@tonic-gate /*ARGSUSED*/
7187c478bd9Sstevel@tonic-gate static void
at_hwcap(long val,char * instr,size_t n,char * str)7197c478bd9Sstevel@tonic-gate at_hwcap(long val, char *instr, size_t n, char *str)
7207c478bd9Sstevel@tonic-gate {
7217c478bd9Sstevel@tonic-gate #if defined(__sparc) || defined(__sparcv9)
72299f63845Sab196087 	(void) elfcap_hw1_to_str(ELFCAP_STYLE_UC, val, str, n,
72399f63845Sab196087 	    ELFCAP_FMT_PIPSPACE, EM_SPARC);
7247c478bd9Sstevel@tonic-gate 
7257c478bd9Sstevel@tonic-gate #elif defined(__i386) || defined(__amd64)
72699f63845Sab196087 	(void) elfcap_hw1_to_str(ELFCAP_STYLE_UC, val, str, n,
72799f63845Sab196087 	    ELFCAP_FMT_PIPSPACE, EM_386);
7287c478bd9Sstevel@tonic-gate #else
7297c478bd9Sstevel@tonic-gate #error	"port me"
7307c478bd9Sstevel@tonic-gate #endif
7317c478bd9Sstevel@tonic-gate }
7327c478bd9Sstevel@tonic-gate 
7337c478bd9Sstevel@tonic-gate /*ARGSUSED*/
7347c478bd9Sstevel@tonic-gate static void
at_hwcap2(long val,char * instr,size_t n,char * str)735ebb8ac07SRobert Mustacchi at_hwcap2(long val, char *instr, size_t n, char *str)
736ebb8ac07SRobert Mustacchi {
737ebb8ac07SRobert Mustacchi #if defined(__sparc) || defined(__sparcv9)
738ebb8ac07SRobert Mustacchi 	(void) elfcap_hw2_to_str(ELFCAP_STYLE_UC, val, str, n,
739ebb8ac07SRobert Mustacchi 	    ELFCAP_FMT_PIPSPACE, EM_SPARC);
740ebb8ac07SRobert Mustacchi 
741ebb8ac07SRobert Mustacchi #elif defined(__i386) || defined(__amd64)
742ebb8ac07SRobert Mustacchi 	(void) elfcap_hw2_to_str(ELFCAP_STYLE_UC, val, str, n,
743ebb8ac07SRobert Mustacchi 	    ELFCAP_FMT_PIPSPACE, EM_386);
744ebb8ac07SRobert Mustacchi #else
745ebb8ac07SRobert Mustacchi #error	"port me"
746ebb8ac07SRobert Mustacchi #endif
747ebb8ac07SRobert Mustacchi }
748ebb8ac07SRobert Mustacchi 
749ebb8ac07SRobert Mustacchi 
750ebb8ac07SRobert Mustacchi /*ARGSUSED*/
751ebb8ac07SRobert Mustacchi static void
at_uid(long val,char * instr,size_t n,char * str)7527c478bd9Sstevel@tonic-gate at_uid(long val, char *instr, size_t n, char *str)
7537c478bd9Sstevel@tonic-gate {
7547c478bd9Sstevel@tonic-gate 	struct passwd *pw = getpwuid((uid_t)val);
7557c478bd9Sstevel@tonic-gate 
7567c478bd9Sstevel@tonic-gate 	if ((pw == NULL) || (pw->pw_name == NULL))
7577c478bd9Sstevel@tonic-gate 		str[0] = '\0';
7587c478bd9Sstevel@tonic-gate 	else
7597c478bd9Sstevel@tonic-gate 		(void) snprintf(str, n, "%lu(%s)", val, pw->pw_name);
7607c478bd9Sstevel@tonic-gate }
7617c478bd9Sstevel@tonic-gate 
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate /*ARGSUSED*/
7647c478bd9Sstevel@tonic-gate static void
at_gid(long val,char * instr,size_t n,char * str)7657c478bd9Sstevel@tonic-gate at_gid(long val, char *instr, size_t n, char *str)
7667c478bd9Sstevel@tonic-gate {
7677c478bd9Sstevel@tonic-gate 	struct group *gr = getgrgid((gid_t)val);
7687c478bd9Sstevel@tonic-gate 
7697c478bd9Sstevel@tonic-gate 	if ((gr == NULL) || (gr->gr_name == NULL))
7707c478bd9Sstevel@tonic-gate 		str[0] = '\0';
7717c478bd9Sstevel@tonic-gate 	else
7727c478bd9Sstevel@tonic-gate 		(void) snprintf(str, n, "%lu(%s)", val, gr->gr_name);
7737c478bd9Sstevel@tonic-gate }
7747c478bd9Sstevel@tonic-gate 
7757c478bd9Sstevel@tonic-gate static struct auxfl {
7767c478bd9Sstevel@tonic-gate 	int af_flag;
7777c478bd9Sstevel@tonic-gate 	const char *af_name;
7787c478bd9Sstevel@tonic-gate } auxfl[] = {
7797c478bd9Sstevel@tonic-gate 	{ AF_SUN_SETUGID,	"setugid" },
7807c478bd9Sstevel@tonic-gate };
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate /*ARGSUSED*/
7837c478bd9Sstevel@tonic-gate static void
at_flags(long val,char * instr,size_t n,char * str)7847c478bd9Sstevel@tonic-gate at_flags(long val, char *instr, size_t n, char *str)
7857c478bd9Sstevel@tonic-gate {
7867c478bd9Sstevel@tonic-gate 	int i;
7877c478bd9Sstevel@tonic-gate 
7887c478bd9Sstevel@tonic-gate 	*str = '\0';
7897c478bd9Sstevel@tonic-gate 
7907c478bd9Sstevel@tonic-gate 	for (i = 0; i < sizeof (auxfl)/sizeof (struct auxfl); i++) {
7917c478bd9Sstevel@tonic-gate 		if ((val & auxfl[i].af_flag) != 0) {
7927c478bd9Sstevel@tonic-gate 			if (*str != '\0')
7937c478bd9Sstevel@tonic-gate 				(void) strlcat(str, ",", n);
7947c478bd9Sstevel@tonic-gate 			(void) strlcat(str, auxfl[i].af_name, n);
7957c478bd9Sstevel@tonic-gate 		}
7967c478bd9Sstevel@tonic-gate 	}
7977c478bd9Sstevel@tonic-gate }
7987c478bd9Sstevel@tonic-gate 
7997c478bd9Sstevel@tonic-gate #define	MAX_AT_NAME_LEN	15
8007c478bd9Sstevel@tonic-gate 
8017c478bd9Sstevel@tonic-gate struct aux_id {
8027c478bd9Sstevel@tonic-gate 	int aux_type;
8037c478bd9Sstevel@tonic-gate 	const char *aux_name;
8047c478bd9Sstevel@tonic-gate 	void (*aux_decode)(long, char *, size_t, char *);
8057c478bd9Sstevel@tonic-gate };
8067c478bd9Sstevel@tonic-gate 
8077c478bd9Sstevel@tonic-gate static struct aux_id aux_arr[] = {
8087c478bd9Sstevel@tonic-gate 	{ AT_NULL,		"AT_NULL",		at_null	},
8097c478bd9Sstevel@tonic-gate 	{ AT_IGNORE,		"AT_IGNORE",		at_null	},
8107c478bd9Sstevel@tonic-gate 	{ AT_EXECFD,		"AT_EXECFD",		at_null	},
8117c478bd9Sstevel@tonic-gate 	{ AT_PHDR,		"AT_PHDR",		at_null	},
8127c478bd9Sstevel@tonic-gate 	{ AT_PHENT,		"AT_PHENT",		at_null	},
8137c478bd9Sstevel@tonic-gate 	{ AT_PHNUM,		"AT_PHNUM",		at_null	},
8147c478bd9Sstevel@tonic-gate 	{ AT_PAGESZ,		"AT_PAGESZ",		at_null	},
8157c478bd9Sstevel@tonic-gate 	{ AT_BASE,		"AT_BASE",		at_null	},
8167c478bd9Sstevel@tonic-gate 	{ AT_FLAGS,		"AT_FLAGS",		at_null	},
8177c478bd9Sstevel@tonic-gate 	{ AT_ENTRY,		"AT_ENTRY",		at_null	},
8187c478bd9Sstevel@tonic-gate 	{ AT_SUN_UID,		"AT_SUN_UID",		at_uid	},
8197c478bd9Sstevel@tonic-gate 	{ AT_SUN_RUID,		"AT_SUN_RUID",		at_uid	},
8207c478bd9Sstevel@tonic-gate 	{ AT_SUN_GID,		"AT_SUN_GID",		at_gid	},
8217c478bd9Sstevel@tonic-gate 	{ AT_SUN_RGID,		"AT_SUN_RGID",		at_gid	},
8227c478bd9Sstevel@tonic-gate 	{ AT_SUN_LDELF,		"AT_SUN_LDELF",		at_null	},
8237c478bd9Sstevel@tonic-gate 	{ AT_SUN_LDSHDR,	"AT_SUN_LDSHDR",	at_null	},
8247c478bd9Sstevel@tonic-gate 	{ AT_SUN_LDNAME,	"AT_SUN_LDNAME",	at_null	},
8257c478bd9Sstevel@tonic-gate 	{ AT_SUN_LPAGESZ,	"AT_SUN_LPAGESZ",	at_null	},
8267c478bd9Sstevel@tonic-gate 	{ AT_SUN_PLATFORM,	"AT_SUN_PLATFORM",	at_str	},
8277c478bd9Sstevel@tonic-gate 	{ AT_SUN_EXECNAME,	"AT_SUN_EXECNAME",	at_str	},
8287c478bd9Sstevel@tonic-gate 	{ AT_SUN_HWCAP,		"AT_SUN_HWCAP",		at_hwcap },
829ebb8ac07SRobert Mustacchi 	{ AT_SUN_HWCAP2,	"AT_SUN_HWCAP2",	at_hwcap2 },
8307c478bd9Sstevel@tonic-gate 	{ AT_SUN_IFLUSH,	"AT_SUN_IFLUSH",	at_null	},
8317c478bd9Sstevel@tonic-gate 	{ AT_SUN_CPU,		"AT_SUN_CPU",		at_null	},
8327c478bd9Sstevel@tonic-gate 	{ AT_SUN_MMU,		"AT_SUN_MMU",		at_null	},
8337c478bd9Sstevel@tonic-gate 	{ AT_SUN_LDDATA,	"AT_SUN_LDDATA",	at_null	},
8347c478bd9Sstevel@tonic-gate 	{ AT_SUN_AUXFLAGS,	"AT_SUN_AUXFLAGS",	at_flags },
8359acbbeafSnn35248 	{ AT_SUN_EMULATOR,	"AT_SUN_EMULATOR",	at_str	},
8369acbbeafSnn35248 	{ AT_SUN_BRANDNAME,	"AT_SUN_BRANDNAME",	at_str	},
83707678296Ssl108498 	{ AT_SUN_BRAND_AUX1,	"AT_SUN_BRAND_AUX1",	at_null	},
83807678296Ssl108498 	{ AT_SUN_BRAND_AUX2,	"AT_SUN_BRAND_AUX2",	at_null	},
839*cc401b37SPatrick Mooney 	{ AT_SUN_BRAND_AUX3,	"AT_SUN_BRAND_AUX3",	at_null	},
840*cc401b37SPatrick Mooney 	{ AT_SUN_COMMPAGE,	"AT_SUN_COMMPAGE",	at_null	}
8417c478bd9Sstevel@tonic-gate };
8427c478bd9Sstevel@tonic-gate 
8437c478bd9Sstevel@tonic-gate #define	N_AT_ENTS (sizeof (aux_arr) / sizeof (struct aux_id))
8447c478bd9Sstevel@tonic-gate 
8457c478bd9Sstevel@tonic-gate /*
8467c478bd9Sstevel@tonic-gate  * Return the aux_id entry for the given aux type; returns NULL if not found.
8477c478bd9Sstevel@tonic-gate  */
8487c478bd9Sstevel@tonic-gate static struct aux_id *
aux_find(int type)8497c478bd9Sstevel@tonic-gate aux_find(int type)
8507c478bd9Sstevel@tonic-gate {
8517c478bd9Sstevel@tonic-gate 	int i;
8527c478bd9Sstevel@tonic-gate 
8537c478bd9Sstevel@tonic-gate 	for (i = 0; i < N_AT_ENTS; i++) {
8547c478bd9Sstevel@tonic-gate 		if (type == aux_arr[i].aux_type)
8557c478bd9Sstevel@tonic-gate 			return (&aux_arr[i]);
8567c478bd9Sstevel@tonic-gate 	}
8577c478bd9Sstevel@tonic-gate 
8587c478bd9Sstevel@tonic-gate 	return (NULL);
8597c478bd9Sstevel@tonic-gate }
8607c478bd9Sstevel@tonic-gate 
8617c478bd9Sstevel@tonic-gate static void
get_auxv(pargs_data_t * datap)8627c478bd9Sstevel@tonic-gate get_auxv(pargs_data_t *datap)
8637c478bd9Sstevel@tonic-gate {
8647c478bd9Sstevel@tonic-gate 	int i;
8657c478bd9Sstevel@tonic-gate 	const auxv_t *auxvp;
8667c478bd9Sstevel@tonic-gate 
8677c478bd9Sstevel@tonic-gate 	/*
8687c478bd9Sstevel@tonic-gate 	 * Fetch the aux vector from the target process.
8697c478bd9Sstevel@tonic-gate 	 */
8707c478bd9Sstevel@tonic-gate 	if (ps_pauxv(datap->pd_proc, &auxvp) != PS_OK)
8717c478bd9Sstevel@tonic-gate 		return;
8727c478bd9Sstevel@tonic-gate 
8737c478bd9Sstevel@tonic-gate 	for (i = 0; auxvp[i].a_type != AT_NULL; i++)
8747c478bd9Sstevel@tonic-gate 		continue;
8757c478bd9Sstevel@tonic-gate 
8767c478bd9Sstevel@tonic-gate 	datap->pd_auxc = i;
8777c478bd9Sstevel@tonic-gate 	datap->pd_auxv = safe_zalloc(i * sizeof (auxv_t));
8787c478bd9Sstevel@tonic-gate 	bcopy(auxvp, datap->pd_auxv, i * sizeof (auxv_t));
8797c478bd9Sstevel@tonic-gate 
8807c478bd9Sstevel@tonic-gate 	datap->pd_auxv_strs = safe_zalloc(datap->pd_auxc * sizeof (char *));
8817c478bd9Sstevel@tonic-gate 	for (i = 0; i < datap->pd_auxc; i++) {
8827c478bd9Sstevel@tonic-gate 		struct aux_id *aux = aux_find(datap->pd_auxv[i].a_type);
8837c478bd9Sstevel@tonic-gate 
8847c478bd9Sstevel@tonic-gate 		/*
8857c478bd9Sstevel@tonic-gate 		 * Grab strings for those entries which have a string-decoder.
8867c478bd9Sstevel@tonic-gate 		 */
8877c478bd9Sstevel@tonic-gate 		if ((aux != NULL) && (aux->aux_decode == at_str)) {
8887c478bd9Sstevel@tonic-gate 			datap->pd_auxv_strs[i] =
8897c478bd9Sstevel@tonic-gate 			    extract_string(datap, datap->pd_auxv[i].a_un.a_val);
8907c478bd9Sstevel@tonic-gate 		}
8917c478bd9Sstevel@tonic-gate 	}
8927c478bd9Sstevel@tonic-gate }
8937c478bd9Sstevel@tonic-gate 
8947c478bd9Sstevel@tonic-gate /*
8957c478bd9Sstevel@tonic-gate  * Prepare to convert characters in the victim's character set into user's
8967c478bd9Sstevel@tonic-gate  * character set.
8977c478bd9Sstevel@tonic-gate  */
8987c478bd9Sstevel@tonic-gate static void
setup_conversions(pargs_data_t * datap,int * diflocale)8997c478bd9Sstevel@tonic-gate setup_conversions(pargs_data_t *datap, int *diflocale)
9007c478bd9Sstevel@tonic-gate {
9017c478bd9Sstevel@tonic-gate 	char *mylocale = NULL, *mycharset = NULL;
9027c478bd9Sstevel@tonic-gate 	char *targetlocale = NULL, *targetcharset = NULL;
9037c478bd9Sstevel@tonic-gate 
9047c478bd9Sstevel@tonic-gate 	mycharset = safe_strdup(nl_langinfo(CODESET));
9057c478bd9Sstevel@tonic-gate 
9067c478bd9Sstevel@tonic-gate 	mylocale = setlocale(LC_CTYPE, NULL);
9077c478bd9Sstevel@tonic-gate 	if ((mylocale == NULL) || (strcmp(mylocale, "") == 0))
9087c478bd9Sstevel@tonic-gate 		mylocale = "C";
9097c478bd9Sstevel@tonic-gate 	mylocale = safe_strdup(mylocale);
9107c478bd9Sstevel@tonic-gate 
9117c478bd9Sstevel@tonic-gate 	if (datap->pd_conv_flags & CONV_STRICT_ASCII)
9127c478bd9Sstevel@tonic-gate 		goto done;
9137c478bd9Sstevel@tonic-gate 
9147c478bd9Sstevel@tonic-gate 	/*
9157c478bd9Sstevel@tonic-gate 	 * If the target's locale is "C" or "POSIX", go fast.
9167c478bd9Sstevel@tonic-gate 	 */
9177c478bd9Sstevel@tonic-gate 	if ((strcmp(datap->pd_locale, "C") == 0) ||
9187c478bd9Sstevel@tonic-gate 	    (strcmp(datap->pd_locale, "POSIX") == 0)) {
9197c478bd9Sstevel@tonic-gate 		datap->pd_conv_flags |= CONV_STRICT_ASCII;
9207c478bd9Sstevel@tonic-gate 		goto done;
9217c478bd9Sstevel@tonic-gate 	}
9227c478bd9Sstevel@tonic-gate 
9237c478bd9Sstevel@tonic-gate 	/*
9247c478bd9Sstevel@tonic-gate 	 * Switch to the victim's locale, and discover its character set.
9257c478bd9Sstevel@tonic-gate 	 */
9267c478bd9Sstevel@tonic-gate 	if (setlocale(LC_ALL, datap->pd_locale) == NULL) {
9277c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
9287c478bd9Sstevel@tonic-gate 		    "%s: Couldn't determine locale of target process.\n",
9297c478bd9Sstevel@tonic-gate 		    command);
9307c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
9317c478bd9Sstevel@tonic-gate 		    "%s: Some strings may not be displayed properly.\n",
9327c478bd9Sstevel@tonic-gate 		    command);
9337c478bd9Sstevel@tonic-gate 		goto done;
9347c478bd9Sstevel@tonic-gate 	}
9357c478bd9Sstevel@tonic-gate 
9367c478bd9Sstevel@tonic-gate 	/*
9377c478bd9Sstevel@tonic-gate 	 * Get LC_CTYPE part of target's locale, and its codeset.
9387c478bd9Sstevel@tonic-gate 	 */
9397c478bd9Sstevel@tonic-gate 	targetlocale = safe_strdup(setlocale(LC_CTYPE, NULL));
9407c478bd9Sstevel@tonic-gate 	targetcharset = safe_strdup(nl_langinfo(CODESET));
9417c478bd9Sstevel@tonic-gate 
9427c478bd9Sstevel@tonic-gate 	/*
9437c478bd9Sstevel@tonic-gate 	 * Now go fully back to the pargs user's locale.
9447c478bd9Sstevel@tonic-gate 	 */
9457c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
9467c478bd9Sstevel@tonic-gate 
9477c478bd9Sstevel@tonic-gate 	/*
9487c478bd9Sstevel@tonic-gate 	 * It's safe to bail here if the lc_ctype of the locales are the
9497c478bd9Sstevel@tonic-gate 	 * same-- we know that their encodings and characters sets are the same.
9507c478bd9Sstevel@tonic-gate 	 */
9517c478bd9Sstevel@tonic-gate 	if (strcmp(targetlocale, mylocale) == 0)
9527c478bd9Sstevel@tonic-gate 		goto done;
9537c478bd9Sstevel@tonic-gate 
9547c478bd9Sstevel@tonic-gate 	*diflocale = 1;
9557c478bd9Sstevel@tonic-gate 
9567c478bd9Sstevel@tonic-gate 	/*
9577c478bd9Sstevel@tonic-gate 	 * If the codeset of the victim matches our codeset then iconv need
9587c478bd9Sstevel@tonic-gate 	 * not be involved.
9597c478bd9Sstevel@tonic-gate 	 */
9607c478bd9Sstevel@tonic-gate 	if (strcmp(mycharset, targetcharset) == 0)
9617c478bd9Sstevel@tonic-gate 		goto done;
9627c478bd9Sstevel@tonic-gate 
9637c478bd9Sstevel@tonic-gate 	if ((datap->pd_iconv = iconv_open(mycharset, targetcharset))
9647c478bd9Sstevel@tonic-gate 	    == (iconv_t)-1) {
9657c478bd9Sstevel@tonic-gate 		/*
9667c478bd9Sstevel@tonic-gate 		 * EINVAL indicates there was no conversion available
9677c478bd9Sstevel@tonic-gate 		 * from victim charset to mycharset
9687c478bd9Sstevel@tonic-gate 		 */
9697c478bd9Sstevel@tonic-gate 		if (errno != EINVAL) {
9707c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
9717c478bd9Sstevel@tonic-gate 			    "%s: failed to initialize iconv: %s\n",
9727c478bd9Sstevel@tonic-gate 			    command, strerror(errno));
9737c478bd9Sstevel@tonic-gate 			exit(1);
9747c478bd9Sstevel@tonic-gate 		}
9757c478bd9Sstevel@tonic-gate 		datap->pd_conv_flags |= CONV_STRICT_ASCII;
9767c478bd9Sstevel@tonic-gate 	} else {
9777c478bd9Sstevel@tonic-gate 		datap->pd_conv_flags |= CONV_USE_ICONV;
9787c478bd9Sstevel@tonic-gate 	}
9797c478bd9Sstevel@tonic-gate done:
9807c478bd9Sstevel@tonic-gate 	free(mycharset);
9817c478bd9Sstevel@tonic-gate 	free(mylocale);
9827c478bd9Sstevel@tonic-gate 	free(targetcharset);
9837c478bd9Sstevel@tonic-gate 	free(targetlocale);
9847c478bd9Sstevel@tonic-gate }
9857c478bd9Sstevel@tonic-gate 
9867c478bd9Sstevel@tonic-gate static void
cleanup_conversions(pargs_data_t * datap)9877c478bd9Sstevel@tonic-gate cleanup_conversions(pargs_data_t *datap)
9887c478bd9Sstevel@tonic-gate {
9897c478bd9Sstevel@tonic-gate 	if (datap->pd_conv_flags & CONV_USE_ICONV) {
9907c478bd9Sstevel@tonic-gate 		(void) iconv_close(datap->pd_iconv);
9917c478bd9Sstevel@tonic-gate 	}
9927c478bd9Sstevel@tonic-gate }
9937c478bd9Sstevel@tonic-gate 
9947c478bd9Sstevel@tonic-gate static char *
convert_run_iconv(pargs_data_t * datap,const char * str)9957c478bd9Sstevel@tonic-gate convert_run_iconv(pargs_data_t *datap, const char *str)
9967c478bd9Sstevel@tonic-gate {
9977c478bd9Sstevel@tonic-gate 	size_t inleft, outleft, bufsz = 64;
9987c478bd9Sstevel@tonic-gate 	char *outstr, *outstrptr;
9997c478bd9Sstevel@tonic-gate 	const char *instrptr;
10007c478bd9Sstevel@tonic-gate 
10017c478bd9Sstevel@tonic-gate 	for (;;) {
10027c478bd9Sstevel@tonic-gate 		outstrptr = outstr = safe_zalloc(bufsz + 1);
10037c478bd9Sstevel@tonic-gate 		outleft = bufsz;
10047c478bd9Sstevel@tonic-gate 
10057c478bd9Sstevel@tonic-gate 		/*
10067c478bd9Sstevel@tonic-gate 		 * Generate the "initial shift state" sequence, placing that
10077c478bd9Sstevel@tonic-gate 		 * at the head of the string.
10087c478bd9Sstevel@tonic-gate 		 */
10097c478bd9Sstevel@tonic-gate 		inleft = 0;
10107c478bd9Sstevel@tonic-gate 		(void) iconv(datap->pd_iconv, NULL, &inleft,
10117c478bd9Sstevel@tonic-gate 		    &outstrptr, &outleft);
10127c478bd9Sstevel@tonic-gate 
10137c478bd9Sstevel@tonic-gate 		inleft = strlen(str);
10147c478bd9Sstevel@tonic-gate 		instrptr = str;
10157c478bd9Sstevel@tonic-gate 		if (iconv(datap->pd_iconv, &instrptr, &inleft, &outstrptr,
10167c478bd9Sstevel@tonic-gate 		    &outleft) != (size_t)-1) {
10177c478bd9Sstevel@tonic-gate 			/*
10187c478bd9Sstevel@tonic-gate 			 * Outstr must be null terminated upon exit from
10197c478bd9Sstevel@tonic-gate 			 * iconv().
10207c478bd9Sstevel@tonic-gate 			 */
10217c478bd9Sstevel@tonic-gate 			*(outstr + (bufsz - outleft)) = '\0';
10227c478bd9Sstevel@tonic-gate 			break;
10237c478bd9Sstevel@tonic-gate 		} else if (errno == E2BIG) {
10247c478bd9Sstevel@tonic-gate 			bufsz *= 2;
10257c478bd9Sstevel@tonic-gate 			free(outstr);
10267c478bd9Sstevel@tonic-gate 		} else if ((errno == EILSEQ) || (errno == EINVAL)) {
10277c478bd9Sstevel@tonic-gate 			free(outstr);
10287c478bd9Sstevel@tonic-gate 			return (NULL);
10297c478bd9Sstevel@tonic-gate 		} else {
10307c478bd9Sstevel@tonic-gate 			/*
10317c478bd9Sstevel@tonic-gate 			 * iconv() could in theory return EBADF, but that
10327c478bd9Sstevel@tonic-gate 			 * shouldn't happen.
10337c478bd9Sstevel@tonic-gate 			 */
10347c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
10357c478bd9Sstevel@tonic-gate 			    "%s: iconv(3C) failed unexpectedly: %s\n",
10367c478bd9Sstevel@tonic-gate 			    command, strerror(errno));
10377c478bd9Sstevel@tonic-gate 
10387c478bd9Sstevel@tonic-gate 			exit(1);
10397c478bd9Sstevel@tonic-gate 		}
10407c478bd9Sstevel@tonic-gate 	}
10417c478bd9Sstevel@tonic-gate 	return (outstr);
10427c478bd9Sstevel@tonic-gate }
10437c478bd9Sstevel@tonic-gate 
10447c478bd9Sstevel@tonic-gate /*
10457c478bd9Sstevel@tonic-gate  * Returns a freshly allocated string converted to the local character set,
10467c478bd9Sstevel@tonic-gate  * removed of unprintable characters.
10477c478bd9Sstevel@tonic-gate  */
10487c478bd9Sstevel@tonic-gate static char *
convert_str(pargs_data_t * datap,const char * str,int * unprintable)10497c478bd9Sstevel@tonic-gate convert_str(pargs_data_t *datap, const char *str, int *unprintable)
10507c478bd9Sstevel@tonic-gate {
10517c478bd9Sstevel@tonic-gate 	char *retstr, *tmp;
10527c478bd9Sstevel@tonic-gate 
10537c478bd9Sstevel@tonic-gate 	if (datap->pd_conv_flags & CONV_STRICT_ASCII) {
10547c478bd9Sstevel@tonic-gate 		retstr = unctrl_str_strict_ascii(str, 1, unprintable);
10557c478bd9Sstevel@tonic-gate 		return (retstr);
10567c478bd9Sstevel@tonic-gate 	}
10577c478bd9Sstevel@tonic-gate 
10587c478bd9Sstevel@tonic-gate 	if ((datap->pd_conv_flags & CONV_USE_ICONV) == 0) {
10597c478bd9Sstevel@tonic-gate 		/*
10607c478bd9Sstevel@tonic-gate 		 * If we aren't using iconv(), convert control chars in
10617c478bd9Sstevel@tonic-gate 		 * the string in pargs' locale, since that is the display
10627c478bd9Sstevel@tonic-gate 		 * locale.
10637c478bd9Sstevel@tonic-gate 		 */
10647c478bd9Sstevel@tonic-gate 		retstr = unctrl_str(str, 1, unprintable);
10657c478bd9Sstevel@tonic-gate 		return (retstr);
10667c478bd9Sstevel@tonic-gate 	}
10677c478bd9Sstevel@tonic-gate 
10687c478bd9Sstevel@tonic-gate 	/*
10697c478bd9Sstevel@tonic-gate 	 * The logic here is a bit (ahem) tricky.  Start by converting
10707c478bd9Sstevel@tonic-gate 	 * unprintable characters *in the target's locale*.  This should
10717c478bd9Sstevel@tonic-gate 	 * eliminate a variety of unprintable or illegal characters-- in
10727c478bd9Sstevel@tonic-gate 	 * short, it should leave us with something which iconv() won't
10737c478bd9Sstevel@tonic-gate 	 * have trouble with.
10747c478bd9Sstevel@tonic-gate 	 *
10757c478bd9Sstevel@tonic-gate 	 * After allowing iconv to convert characters as needed, run unctrl
10767c478bd9Sstevel@tonic-gate 	 * again in pargs' locale-- This time to make sure that any
10777c478bd9Sstevel@tonic-gate 	 * characters which aren't printable according to the *current*
10787c478bd9Sstevel@tonic-gate 	 * locale (independent of the current codeset) get taken care of.
10797c478bd9Sstevel@tonic-gate 	 * Without this second stage, we might (for example) fail to
10807c478bd9Sstevel@tonic-gate 	 * properly handle characters converted into the 646 character set
10817c478bd9Sstevel@tonic-gate 	 * (which are 8-bits wide), but which must be displayed in the C
10827c478bd9Sstevel@tonic-gate 	 * locale (which uses 646, but whose printable characters are a
10837c478bd9Sstevel@tonic-gate 	 * subset of the 7-bit characters).
10847c478bd9Sstevel@tonic-gate 	 *
10857c478bd9Sstevel@tonic-gate 	 * Note that assuming the victim's locale using LC_ALL will be
10867c478bd9Sstevel@tonic-gate 	 * problematic when pargs' messages are internationalized in the
10877c478bd9Sstevel@tonic-gate 	 * future (and it calls textdomain(3C)).  In this case, any
10887c478bd9Sstevel@tonic-gate 	 * error message fprintf'd in unctrl_str() will be in the wrong
10897c478bd9Sstevel@tonic-gate 	 * LC_MESSAGES class.  We'll cross that bridge when we come to it.
10907c478bd9Sstevel@tonic-gate 	 */
10917c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, datap->pd_locale);
10927c478bd9Sstevel@tonic-gate 	retstr = unctrl_str(str, 1, unprintable);
10937c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
10947c478bd9Sstevel@tonic-gate 
10957c478bd9Sstevel@tonic-gate 	tmp = retstr;
10967c478bd9Sstevel@tonic-gate 	if ((retstr = convert_run_iconv(datap, retstr)) == NULL) {
10977c478bd9Sstevel@tonic-gate 		/*
10987c478bd9Sstevel@tonic-gate 		 * In this (rare but real) case, the iconv() failed even
10997c478bd9Sstevel@tonic-gate 		 * though we unctrl'd the string.  Treat the original string
11007c478bd9Sstevel@tonic-gate 		 * (str) as a C locale string and strip it that way.
11017c478bd9Sstevel@tonic-gate 		 */
11027c478bd9Sstevel@tonic-gate 		free(tmp);
11037c478bd9Sstevel@tonic-gate 		return (unctrl_str_strict_ascii(str, 0, unprintable));
11047c478bd9Sstevel@tonic-gate 	}
11057c478bd9Sstevel@tonic-gate 
11067c478bd9Sstevel@tonic-gate 	free(tmp);
11077c478bd9Sstevel@tonic-gate 	tmp = retstr;
11087c478bd9Sstevel@tonic-gate 	/*
11097c478bd9Sstevel@tonic-gate 	 * Run unctrl_str, but make sure not to escape \ characters, which
11107c478bd9Sstevel@tonic-gate 	 * may have resulted from the first round of unctrl.
11117c478bd9Sstevel@tonic-gate 	 */
11127c478bd9Sstevel@tonic-gate 	retstr = unctrl_str(retstr, 0, unprintable);
11137c478bd9Sstevel@tonic-gate 	free(tmp);
11147c478bd9Sstevel@tonic-gate 	return (retstr);
11157c478bd9Sstevel@tonic-gate }
11167c478bd9Sstevel@tonic-gate 
11177c478bd9Sstevel@tonic-gate 
11187c478bd9Sstevel@tonic-gate static void
convert_array(pargs_data_t * datap,char ** arr,size_t count,int * unprintable)11197c478bd9Sstevel@tonic-gate convert_array(pargs_data_t *datap, char **arr, size_t count, int *unprintable)
11207c478bd9Sstevel@tonic-gate {
11217c478bd9Sstevel@tonic-gate 	int i;
11227c478bd9Sstevel@tonic-gate 	char *tmp;
11237c478bd9Sstevel@tonic-gate 
11247c478bd9Sstevel@tonic-gate 	if (arr == NULL)
11257c478bd9Sstevel@tonic-gate 		return;
11267c478bd9Sstevel@tonic-gate 
11277c478bd9Sstevel@tonic-gate 	for (i = 0; i < count; i++) {
11287c478bd9Sstevel@tonic-gate 		if ((tmp = arr[i]) == NULL)
11297c478bd9Sstevel@tonic-gate 			continue;
11307c478bd9Sstevel@tonic-gate 		arr[i] = convert_str(datap, arr[i], unprintable);
11317c478bd9Sstevel@tonic-gate 		free(tmp);
11327c478bd9Sstevel@tonic-gate 	}
11337c478bd9Sstevel@tonic-gate }
11347c478bd9Sstevel@tonic-gate 
11357c478bd9Sstevel@tonic-gate /*
11367c478bd9Sstevel@tonic-gate  * Free data allocated during the gathering phase.
11377c478bd9Sstevel@tonic-gate  */
11387c478bd9Sstevel@tonic-gate static void
free_data(pargs_data_t * datap)11397c478bd9Sstevel@tonic-gate free_data(pargs_data_t *datap)
11407c478bd9Sstevel@tonic-gate {
11417c478bd9Sstevel@tonic-gate 	int i;
11427c478bd9Sstevel@tonic-gate 
11437365cab2SMarcel Telka 	for (i = 0; i < datap->pd_argc; i++)
11447c478bd9Sstevel@tonic-gate 		free(datap->pd_argv_strs[i]);
11457c478bd9Sstevel@tonic-gate 	free(datap->pd_argv);
11467c478bd9Sstevel@tonic-gate 	free(datap->pd_argv_strs);
11477c478bd9Sstevel@tonic-gate 
11487365cab2SMarcel Telka 	for (i = 0; i < datap->pd_envc; i++)
11497c478bd9Sstevel@tonic-gate 		free(datap->pd_envp_strs[i]);
11507c478bd9Sstevel@tonic-gate 	free(datap->pd_envp);
11517c478bd9Sstevel@tonic-gate 	free(datap->pd_envp_strs);
11527c478bd9Sstevel@tonic-gate 
11537365cab2SMarcel Telka 	for (i = 0; i < datap->pd_auxc; i++)
11547c478bd9Sstevel@tonic-gate 		free(datap->pd_auxv_strs[i]);
11557c478bd9Sstevel@tonic-gate 	free(datap->pd_auxv);
11567c478bd9Sstevel@tonic-gate 	free(datap->pd_auxv_strs);
11577c478bd9Sstevel@tonic-gate }
11587c478bd9Sstevel@tonic-gate 
11597c478bd9Sstevel@tonic-gate static void
print_args(pargs_data_t * datap)11607c478bd9Sstevel@tonic-gate print_args(pargs_data_t *datap)
11617c478bd9Sstevel@tonic-gate {
11627c478bd9Sstevel@tonic-gate 	int i;
11637c478bd9Sstevel@tonic-gate 
11647c478bd9Sstevel@tonic-gate 	if (datap->pd_argv == NULL) {
11657c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: failed to read argv[]\n", command);
11667c478bd9Sstevel@tonic-gate 		return;
11677c478bd9Sstevel@tonic-gate 	}
11687c478bd9Sstevel@tonic-gate 
11697c478bd9Sstevel@tonic-gate 	for (i = 0; i < datap->pd_argc; i++) {
11707c478bd9Sstevel@tonic-gate 		(void) printf("argv[%d]: ", i);
11717c478bd9Sstevel@tonic-gate 		if (datap->pd_argv[i] == NULL) {
11727c478bd9Sstevel@tonic-gate 			(void) printf("<NULL>\n");
11737c478bd9Sstevel@tonic-gate 		} else if (datap->pd_argv_strs[i] == NULL) {
11747c478bd9Sstevel@tonic-gate 			(void) printf("<0x%0*lx>\n",
11757c478bd9Sstevel@tonic-gate 			    (dmodel == PR_MODEL_LP64)? 16 : 8,
11767c478bd9Sstevel@tonic-gate 			    (long)datap->pd_argv[i]);
11777c478bd9Sstevel@tonic-gate 		} else {
11787c478bd9Sstevel@tonic-gate 			(void) printf("%s\n", datap->pd_argv_strs[i]);
11797c478bd9Sstevel@tonic-gate 		}
11807c478bd9Sstevel@tonic-gate 	}
11817c478bd9Sstevel@tonic-gate }
11827c478bd9Sstevel@tonic-gate 
11837c478bd9Sstevel@tonic-gate static void
print_env(pargs_data_t * datap)11847c478bd9Sstevel@tonic-gate print_env(pargs_data_t *datap)
11857c478bd9Sstevel@tonic-gate {
11867c478bd9Sstevel@tonic-gate 	int i;
11877c478bd9Sstevel@tonic-gate 
11887c478bd9Sstevel@tonic-gate 	if (datap->pd_envp == NULL) {
11897c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: failed to read envp[]\n", command);
11907c478bd9Sstevel@tonic-gate 		return;
11917c478bd9Sstevel@tonic-gate 	}
11927c478bd9Sstevel@tonic-gate 
11937c478bd9Sstevel@tonic-gate 	for (i = 0; i < datap->pd_envc; i++) {
11947c478bd9Sstevel@tonic-gate 		(void) printf("envp[%d]: ", i);
11957c478bd9Sstevel@tonic-gate 		if (datap->pd_envp[i] == 0) {
11967c478bd9Sstevel@tonic-gate 			break;
11977c478bd9Sstevel@tonic-gate 		} else if (datap->pd_envp_strs[i] == NULL) {
11987c478bd9Sstevel@tonic-gate 			(void) printf("<0x%0*lx>\n",
11997c478bd9Sstevel@tonic-gate 			    (dmodel == PR_MODEL_LP64)? 16 : 8,
12007c478bd9Sstevel@tonic-gate 			    (long)datap->pd_envp[i]);
12017c478bd9Sstevel@tonic-gate 		} else {
12027c478bd9Sstevel@tonic-gate 			(void) printf("%s\n", datap->pd_envp_strs[i]);
12037c478bd9Sstevel@tonic-gate 		}
12047c478bd9Sstevel@tonic-gate 	}
12057c478bd9Sstevel@tonic-gate }
12067c478bd9Sstevel@tonic-gate 
12077c478bd9Sstevel@tonic-gate static int
print_cmdline(pargs_data_t * datap)12087c478bd9Sstevel@tonic-gate print_cmdline(pargs_data_t *datap)
12097c478bd9Sstevel@tonic-gate {
12107c478bd9Sstevel@tonic-gate 	int i;
12117c478bd9Sstevel@tonic-gate 
12127c478bd9Sstevel@tonic-gate 	/*
12137c478bd9Sstevel@tonic-gate 	 * Go through and check to see if we have valid data.  If not, print
12147c478bd9Sstevel@tonic-gate 	 * an error message and bail.
12157c478bd9Sstevel@tonic-gate 	 */
12167c478bd9Sstevel@tonic-gate 	for (i = 0; i < datap->pd_argc; i++) {
12178f68126cSBryan Cantrill 		if (datap->pd_argv == NULL || datap->pd_argv[i] == NULL ||
12187c478bd9Sstevel@tonic-gate 		    datap->pd_argv_strs[i] == NULL) {
12197c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: target has corrupted "
12207c478bd9Sstevel@tonic-gate 			    "argument list\n", command);
12217c478bd9Sstevel@tonic-gate 			return (1);
12227c478bd9Sstevel@tonic-gate 		}
12237c478bd9Sstevel@tonic-gate 
12247c478bd9Sstevel@tonic-gate 		datap->pd_argv_strs[i] =
12257c478bd9Sstevel@tonic-gate 		    quote_string(datap, datap->pd_argv_strs[i]);
12267c478bd9Sstevel@tonic-gate 	}
12277c478bd9Sstevel@tonic-gate 
12287c478bd9Sstevel@tonic-gate 	if (datap->pd_execname == NULL) {
12297c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: cannot determine name of "
12307c478bd9Sstevel@tonic-gate 		    "executable\n", command);
12317c478bd9Sstevel@tonic-gate 		return (1);
12327c478bd9Sstevel@tonic-gate 	}
12337c478bd9Sstevel@tonic-gate 
12347c478bd9Sstevel@tonic-gate 	(void) printf("%s ", datap->pd_execname);
12357c478bd9Sstevel@tonic-gate 
12367c478bd9Sstevel@tonic-gate 	for (i = 1; i < datap->pd_argc; i++)
12377c478bd9Sstevel@tonic-gate 		(void) printf("%s ", datap->pd_argv_strs[i]);
12387c478bd9Sstevel@tonic-gate 
12397c478bd9Sstevel@tonic-gate 	(void) printf("\n");
12407c478bd9Sstevel@tonic-gate 
12417c478bd9Sstevel@tonic-gate 	return (0);
12427c478bd9Sstevel@tonic-gate }
12437c478bd9Sstevel@tonic-gate 
12447c478bd9Sstevel@tonic-gate static void
print_auxv(pargs_data_t * datap)12457c478bd9Sstevel@tonic-gate print_auxv(pargs_data_t *datap)
12467c478bd9Sstevel@tonic-gate {
12477c478bd9Sstevel@tonic-gate 	int i;
12487c478bd9Sstevel@tonic-gate 	const auxv_t *pa;
12497c478bd9Sstevel@tonic-gate 
12507c478bd9Sstevel@tonic-gate 	/*
12517c478bd9Sstevel@tonic-gate 	 * Print the names and values of all the aux vector entries.
12527c478bd9Sstevel@tonic-gate 	 */
12537c478bd9Sstevel@tonic-gate 	for (i = 0; i < datap->pd_auxc; i++) {
12547c478bd9Sstevel@tonic-gate 		char type[32];
12557c478bd9Sstevel@tonic-gate 		char decode[PATH_MAX];
12567c478bd9Sstevel@tonic-gate 		struct aux_id *aux;
12577c478bd9Sstevel@tonic-gate 		long v;
12587c478bd9Sstevel@tonic-gate 		pa = &datap->pd_auxv[i];
12597c478bd9Sstevel@tonic-gate 
12607c478bd9Sstevel@tonic-gate 		aux = aux_find(pa->a_type);
12617c478bd9Sstevel@tonic-gate 		v = (long)pa->a_un.a_val;
12627c478bd9Sstevel@tonic-gate 
12637c478bd9Sstevel@tonic-gate 		if (aux != NULL) {
12647c478bd9Sstevel@tonic-gate 			/*
12657c478bd9Sstevel@tonic-gate 			 * Fetch aux vector type string and decoded
12667c478bd9Sstevel@tonic-gate 			 * representation of the value.
12677c478bd9Sstevel@tonic-gate 			 */
12687c478bd9Sstevel@tonic-gate 			(void) strlcpy(type, aux->aux_name, sizeof (type));
12697c478bd9Sstevel@tonic-gate 			aux->aux_decode(v, datap->pd_auxv_strs[i],
12707c478bd9Sstevel@tonic-gate 			    sizeof (decode), decode);
12717c478bd9Sstevel@tonic-gate 		} else {
12727c478bd9Sstevel@tonic-gate 			(void) snprintf(type, sizeof (type), "%d", pa->a_type);
12737c478bd9Sstevel@tonic-gate 			decode[0] = '\0';
12747c478bd9Sstevel@tonic-gate 		}
12757c478bd9Sstevel@tonic-gate 
12767c478bd9Sstevel@tonic-gate 		(void) printf("%-*s 0x%0*lx %s\n", MAX_AT_NAME_LEN, type,
12777c478bd9Sstevel@tonic-gate 		    (dmodel == PR_MODEL_LP64)? 16 : 8, v, decode);
12787c478bd9Sstevel@tonic-gate 	}
12797c478bd9Sstevel@tonic-gate }
12807c478bd9Sstevel@tonic-gate 
12817c478bd9Sstevel@tonic-gate int
main(int argc,char * argv[])12827c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
12837c478bd9Sstevel@tonic-gate {
12847c478bd9Sstevel@tonic-gate 	int aflag = 0, cflag = 0, eflag = 0, xflag = 0, lflag = 0;
12857c478bd9Sstevel@tonic-gate 	int errflg = 0, retc = 0;
12867c478bd9Sstevel@tonic-gate 	int opt;
12877c478bd9Sstevel@tonic-gate 	int error = 1;
12887c478bd9Sstevel@tonic-gate 	core_content_t content = 0;
12897c478bd9Sstevel@tonic-gate 
12907c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
12917c478bd9Sstevel@tonic-gate 
12927c478bd9Sstevel@tonic-gate 	if ((command = strrchr(argv[0], '/')) != NULL)
12937c478bd9Sstevel@tonic-gate 		command++;
12947c478bd9Sstevel@tonic-gate 	else
12957c478bd9Sstevel@tonic-gate 		command = argv[0];
12967c478bd9Sstevel@tonic-gate 
12977c478bd9Sstevel@tonic-gate 	while ((opt = getopt(argc, argv, "acelxF")) != EOF) {
12987c478bd9Sstevel@tonic-gate 		switch (opt) {
12997c478bd9Sstevel@tonic-gate 		case 'a':		/* show process arguments */
13007c478bd9Sstevel@tonic-gate 			content |= CC_CONTENT_STACK;
13017c478bd9Sstevel@tonic-gate 			aflag++;
13027c478bd9Sstevel@tonic-gate 			break;
13037c478bd9Sstevel@tonic-gate 		case 'c':		/* force 7-bit ascii */
13047c478bd9Sstevel@tonic-gate 			cflag++;
13057c478bd9Sstevel@tonic-gate 			break;
13067c478bd9Sstevel@tonic-gate 		case 'e':		/* show environment variables */
13077c478bd9Sstevel@tonic-gate 			content |= CC_CONTENT_STACK;
13087c478bd9Sstevel@tonic-gate 			eflag++;
13097c478bd9Sstevel@tonic-gate 			break;
13107c478bd9Sstevel@tonic-gate 		case 'l':
13117c478bd9Sstevel@tonic-gate 			lflag++;
13127c478bd9Sstevel@tonic-gate 			aflag++;	/* -l implies -a */
13137c478bd9Sstevel@tonic-gate 			break;
13147c478bd9Sstevel@tonic-gate 		case 'x':		/* show aux vector entries */
13157c478bd9Sstevel@tonic-gate 			xflag++;
13167c478bd9Sstevel@tonic-gate 			break;
13177c478bd9Sstevel@tonic-gate 		case 'F':
13187c478bd9Sstevel@tonic-gate 			/*
13197c478bd9Sstevel@tonic-gate 			 * Since we open the process read-only, there is no need
13207c478bd9Sstevel@tonic-gate 			 * for the -F flag.  It's a documented flag, so we
13217c478bd9Sstevel@tonic-gate 			 * consume it silently.
13227c478bd9Sstevel@tonic-gate 			 */
13237c478bd9Sstevel@tonic-gate 			break;
13247c478bd9Sstevel@tonic-gate 		default:
13257c478bd9Sstevel@tonic-gate 			errflg++;
13267c478bd9Sstevel@tonic-gate 			break;
13277c478bd9Sstevel@tonic-gate 		}
13287c478bd9Sstevel@tonic-gate 	}
13297c478bd9Sstevel@tonic-gate 
13307c478bd9Sstevel@tonic-gate 	/* -a is the default if no options are specified */
13317c478bd9Sstevel@tonic-gate 	if ((aflag + eflag + xflag + lflag) == 0) {
13327c478bd9Sstevel@tonic-gate 		aflag++;
13337c478bd9Sstevel@tonic-gate 		content |= CC_CONTENT_STACK;
13347c478bd9Sstevel@tonic-gate 	}
13357c478bd9Sstevel@tonic-gate 
13367c478bd9Sstevel@tonic-gate 	/* -l cannot be used with the -x or -e flags */
13377c478bd9Sstevel@tonic-gate 	if (lflag && (xflag || eflag)) {
13387c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "-l is incompatible with -x and -e\n");
13397c478bd9Sstevel@tonic-gate 		errflg++;
13407c478bd9Sstevel@tonic-gate 	}
13417c478bd9Sstevel@tonic-gate 
13427c478bd9Sstevel@tonic-gate 	argc -= optind;
13437c478bd9Sstevel@tonic-gate 	argv += optind;
13447c478bd9Sstevel@tonic-gate 
13457c478bd9Sstevel@tonic-gate 	if (errflg || argc <= 0) {
13467c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
1347ba5fdd2dSDavid Höppner 		    "usage:  %s [-aceFlx] { pid | core } ...\n"
13487c478bd9Sstevel@tonic-gate 		    "  (show process arguments and environment)\n"
13497c478bd9Sstevel@tonic-gate 		    "  -a: show process arguments (default)\n"
13507c478bd9Sstevel@tonic-gate 		    "  -c: interpret characters as 7-bit ascii regardless of "
13517c478bd9Sstevel@tonic-gate 		    "locale\n"
13527c478bd9Sstevel@tonic-gate 		    "  -e: show environment variables\n"
1353ba5fdd2dSDavid Höppner 		    "  -F: force grabbing of the target process\n"
13547c478bd9Sstevel@tonic-gate 		    "  -l: display arguments as command line\n"
1355ba5fdd2dSDavid Höppner 		    "  -x: show aux vector entries\n", command);
13567c478bd9Sstevel@tonic-gate 		return (2);
13577c478bd9Sstevel@tonic-gate 	}
13587c478bd9Sstevel@tonic-gate 
13597c478bd9Sstevel@tonic-gate 	while (argc-- > 0) {
13607c478bd9Sstevel@tonic-gate 		char *arg;
13617c478bd9Sstevel@tonic-gate 		int gret, r;
13627c478bd9Sstevel@tonic-gate 		psinfo_t psinfo;
13637c478bd9Sstevel@tonic-gate 		char *psargs_conv;
13647c478bd9Sstevel@tonic-gate 		struct ps_prochandle *Pr;
13657c478bd9Sstevel@tonic-gate 		pargs_data_t datap;
13667c478bd9Sstevel@tonic-gate 		char *info;
13677c478bd9Sstevel@tonic-gate 		size_t info_sz;
13687c478bd9Sstevel@tonic-gate 		int pstate;
13697c478bd9Sstevel@tonic-gate 		char execname[PATH_MAX];
13707c478bd9Sstevel@tonic-gate 		int unprintable;
13717c478bd9Sstevel@tonic-gate 		int diflocale;
13727c478bd9Sstevel@tonic-gate 
13737c478bd9Sstevel@tonic-gate 		(void) fflush(stdout);
13747c478bd9Sstevel@tonic-gate 		arg = *argv++;
13757c478bd9Sstevel@tonic-gate 
13767c478bd9Sstevel@tonic-gate 		/*
13777c478bd9Sstevel@tonic-gate 		 * Suppress extra blanks lines if we've encountered processes
13787c478bd9Sstevel@tonic-gate 		 * which can't be opened.
13797c478bd9Sstevel@tonic-gate 		 */
13807c478bd9Sstevel@tonic-gate 		if (error == 0) {
13817c478bd9Sstevel@tonic-gate 			(void) printf("\n");
13827c478bd9Sstevel@tonic-gate 		}
13837c478bd9Sstevel@tonic-gate 		error = 0;
13847c478bd9Sstevel@tonic-gate 
13857c478bd9Sstevel@tonic-gate 		/*
13867c478bd9Sstevel@tonic-gate 		 * First grab just the psinfo information, in case this
13877c478bd9Sstevel@tonic-gate 		 * process is a zombie (in which case proc_arg_grab() will
13887c478bd9Sstevel@tonic-gate 		 * fail).  If so, print a nice message and continue.
13897c478bd9Sstevel@tonic-gate 		 */
13907c478bd9Sstevel@tonic-gate 		if (proc_arg_psinfo(arg, PR_ARG_ANY, &psinfo,
13917c478bd9Sstevel@tonic-gate 		    &gret) == -1) {
13927c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: cannot examine %s: %s\n",
13937c478bd9Sstevel@tonic-gate 			    command, arg, Pgrab_error(gret));
13947c478bd9Sstevel@tonic-gate 			retc++;
13957c478bd9Sstevel@tonic-gate 			error = 1;
13967c478bd9Sstevel@tonic-gate 			continue;
13977c478bd9Sstevel@tonic-gate 		}
13987c478bd9Sstevel@tonic-gate 
13997c478bd9Sstevel@tonic-gate 		if (psinfo.pr_nlwp == 0) {
14007c478bd9Sstevel@tonic-gate 			(void) printf("%d: <defunct>\n", (int)psinfo.pr_pid);
14017c478bd9Sstevel@tonic-gate 			continue;
14027c478bd9Sstevel@tonic-gate 		}
14037c478bd9Sstevel@tonic-gate 
14047c478bd9Sstevel@tonic-gate 		/*
14057c478bd9Sstevel@tonic-gate 		 * If process is a "system" process (like pageout), just
14067c478bd9Sstevel@tonic-gate 		 * print its psargs and continue on.
14077c478bd9Sstevel@tonic-gate 		 */
14087c478bd9Sstevel@tonic-gate 		if (psinfo.pr_size == 0 && psinfo.pr_rssize == 0) {
14097c478bd9Sstevel@tonic-gate 			proc_unctrl_psinfo(&psinfo);
14107c478bd9Sstevel@tonic-gate 			if (!lflag)
14117c478bd9Sstevel@tonic-gate 				(void) printf("%d: ", (int)psinfo.pr_pid);
14127c478bd9Sstevel@tonic-gate 			(void) printf("%s\n", psinfo.pr_psargs);
14137c478bd9Sstevel@tonic-gate 			continue;
14147c478bd9Sstevel@tonic-gate 		}
14157c478bd9Sstevel@tonic-gate 
14167c478bd9Sstevel@tonic-gate 		/*
14177c478bd9Sstevel@tonic-gate 		 * Open the process readonly, since we do not need to write to
14187c478bd9Sstevel@tonic-gate 		 * the control file.
14197c478bd9Sstevel@tonic-gate 		 */
14207c478bd9Sstevel@tonic-gate 		if ((Pr = proc_arg_grab(arg, PR_ARG_ANY, PGRAB_RDONLY,
14217c478bd9Sstevel@tonic-gate 		    &gret)) == NULL) {
14227c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: cannot examine %s: %s\n",
14237c478bd9Sstevel@tonic-gate 			    command, arg, Pgrab_error(gret));
14247c478bd9Sstevel@tonic-gate 			retc++;
14257c478bd9Sstevel@tonic-gate 			error = 1;
14267c478bd9Sstevel@tonic-gate 			continue;
14277c478bd9Sstevel@tonic-gate 		}
14287c478bd9Sstevel@tonic-gate 
14297c478bd9Sstevel@tonic-gate 		pstate = Pstate(Pr);
14307c478bd9Sstevel@tonic-gate 
14317c478bd9Sstevel@tonic-gate 		if (pstate == PS_DEAD &&
14327c478bd9Sstevel@tonic-gate 		    (Pcontent(Pr) & content) != content) {
14337c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: core '%s' has "
14347c478bd9Sstevel@tonic-gate 			    "insufficient content\n", command, arg);
14357c478bd9Sstevel@tonic-gate 			retc++;
14367c478bd9Sstevel@tonic-gate 			continue;
14377c478bd9Sstevel@tonic-gate 		}
14387c478bd9Sstevel@tonic-gate 
14397c478bd9Sstevel@tonic-gate 		/*
14407c478bd9Sstevel@tonic-gate 		 * If malloc() fails, we return here so that we can let go
14417c478bd9Sstevel@tonic-gate 		 * of the victim, restore our locale, print a message,
14427c478bd9Sstevel@tonic-gate 		 * then exit.
14437c478bd9Sstevel@tonic-gate 		 */
14447c478bd9Sstevel@tonic-gate 		if ((r = setjmp(env)) != 0) {
14457c478bd9Sstevel@tonic-gate 			Prelease(Pr, 0);
14467c478bd9Sstevel@tonic-gate 			(void) setlocale(LC_ALL, "");
14477c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: out of memory: %s\n",
14487c478bd9Sstevel@tonic-gate 			    command, strerror(r));
14497c478bd9Sstevel@tonic-gate 			return (1);
14507c478bd9Sstevel@tonic-gate 		}
14517c478bd9Sstevel@tonic-gate 
14527c478bd9Sstevel@tonic-gate 		dmodel = Pstatus(Pr)->pr_dmodel;
14537c478bd9Sstevel@tonic-gate 		bzero(&datap, sizeof (datap));
14547c478bd9Sstevel@tonic-gate 		bcopy(Ppsinfo(Pr), &psinfo, sizeof (psinfo_t));
14557c478bd9Sstevel@tonic-gate 		datap.pd_proc = Pr;
14567c478bd9Sstevel@tonic-gate 		datap.pd_psinfo = &psinfo;
14577c478bd9Sstevel@tonic-gate 
14587c478bd9Sstevel@tonic-gate 		if (cflag)
14597c478bd9Sstevel@tonic-gate 			datap.pd_conv_flags |= CONV_STRICT_ASCII;
14607c478bd9Sstevel@tonic-gate 
14617c478bd9Sstevel@tonic-gate 		/*
14627c478bd9Sstevel@tonic-gate 		 * Strip control characters, then record process summary in
14637c478bd9Sstevel@tonic-gate 		 * a buffer, since we don't want to print anything out until
14647c478bd9Sstevel@tonic-gate 		 * after we release the process.
14657c478bd9Sstevel@tonic-gate 		 */
14667c478bd9Sstevel@tonic-gate 
14677c478bd9Sstevel@tonic-gate 		/*
14687c478bd9Sstevel@tonic-gate 		 * The process is neither a system process nor defunct.
14697c478bd9Sstevel@tonic-gate 		 *
14707c478bd9Sstevel@tonic-gate 		 * Do printing and post-processing (like name lookups) after
14717c478bd9Sstevel@tonic-gate 		 * gathering the raw data from the process and releasing it.
14727c478bd9Sstevel@tonic-gate 		 * This way, we don't deadlock on (for example) name lookup
14737c478bd9Sstevel@tonic-gate 		 * if we grabbed the nscd and do 'pargs -x'.
14747c478bd9Sstevel@tonic-gate 		 *
14757c478bd9Sstevel@tonic-gate 		 * We always fetch the environment of the target, so that we
14767c478bd9Sstevel@tonic-gate 		 * can make an educated guess about its locale.
14777c478bd9Sstevel@tonic-gate 		 */
14787c478bd9Sstevel@tonic-gate 		get_env(&datap);
14797c478bd9Sstevel@tonic-gate 		if (aflag != 0)
14807c478bd9Sstevel@tonic-gate 			get_args(&datap);
14817c478bd9Sstevel@tonic-gate 		if (xflag != 0)
14827c478bd9Sstevel@tonic-gate 			get_auxv(&datap);
14837c478bd9Sstevel@tonic-gate 
14847c478bd9Sstevel@tonic-gate 		/*
14857c478bd9Sstevel@tonic-gate 		 * If malloc() fails after this poiint, we return here to
14867c478bd9Sstevel@tonic-gate 		 * restore our locale and print a message.  If we don't
14877c478bd9Sstevel@tonic-gate 		 * reset this, we might erroneously try to Prelease a process
14887c478bd9Sstevel@tonic-gate 		 * twice.
14897c478bd9Sstevel@tonic-gate 		 */
14907c478bd9Sstevel@tonic-gate 		if ((r = setjmp(env)) != 0) {
14917c478bd9Sstevel@tonic-gate 			(void) setlocale(LC_ALL, "");
14927c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: out of memory: %s\n",
14937c478bd9Sstevel@tonic-gate 			    command, strerror(r));
14947c478bd9Sstevel@tonic-gate 			return (1);
14957c478bd9Sstevel@tonic-gate 		}
14967c478bd9Sstevel@tonic-gate 
14977c478bd9Sstevel@tonic-gate 		/*
14987c478bd9Sstevel@tonic-gate 		 * For the -l option, we need a proper name for this executable
14997c478bd9Sstevel@tonic-gate 		 * before we release it.
15007c478bd9Sstevel@tonic-gate 		 */
15017c478bd9Sstevel@tonic-gate 		if (lflag)
15027c478bd9Sstevel@tonic-gate 			datap.pd_execname = Pexecname(Pr, execname,
15037c478bd9Sstevel@tonic-gate 			    sizeof (execname));
15047c478bd9Sstevel@tonic-gate 
15057c478bd9Sstevel@tonic-gate 		Prelease(Pr, 0);
15067c478bd9Sstevel@tonic-gate 
15077c478bd9Sstevel@tonic-gate 		/*
15087c478bd9Sstevel@tonic-gate 		 * Crawl through the environment to determine the locale of
15097c478bd9Sstevel@tonic-gate 		 * the target.
15107c478bd9Sstevel@tonic-gate 		 */
15117c478bd9Sstevel@tonic-gate 		lookup_locale(&datap);
15127c478bd9Sstevel@tonic-gate 		diflocale = 0;
15137c478bd9Sstevel@tonic-gate 		setup_conversions(&datap, &diflocale);
15147c478bd9Sstevel@tonic-gate 
15157c478bd9Sstevel@tonic-gate 		if (lflag != 0) {
15167c478bd9Sstevel@tonic-gate 			unprintable = 0;
15177c478bd9Sstevel@tonic-gate 			convert_array(&datap, datap.pd_argv_strs,
15187c478bd9Sstevel@tonic-gate 			    datap.pd_argc, &unprintable);
15197c478bd9Sstevel@tonic-gate 			if (diflocale)
15207c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "%s: Warning, target "
15217c478bd9Sstevel@tonic-gate 				    "locale differs from current locale\n",
15227c478bd9Sstevel@tonic-gate 				    command);
15237c478bd9Sstevel@tonic-gate 			else if (unprintable)
15247c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "%s: Warning, command "
15257c478bd9Sstevel@tonic-gate 				    "line contains unprintable characters\n",
15267c478bd9Sstevel@tonic-gate 				    command);
15277c478bd9Sstevel@tonic-gate 
15287c478bd9Sstevel@tonic-gate 			retc += print_cmdline(&datap);
15297c478bd9Sstevel@tonic-gate 		} else {
15307c478bd9Sstevel@tonic-gate 			psargs_conv = convert_str(&datap, psinfo.pr_psargs,
15317c478bd9Sstevel@tonic-gate 			    &unprintable);
15327c478bd9Sstevel@tonic-gate 			info_sz = strlen(psargs_conv) + MAXPATHLEN + 32 + 1;
15337c478bd9Sstevel@tonic-gate 			info = malloc(info_sz);
15347c478bd9Sstevel@tonic-gate 			if (pstate == PS_DEAD) {
15357c478bd9Sstevel@tonic-gate 				(void) snprintf(info, info_sz,
15367c478bd9Sstevel@tonic-gate 				    "core '%s' of %d:\t%s\n",
15377c478bd9Sstevel@tonic-gate 				    arg, (int)psinfo.pr_pid, psargs_conv);
15387c478bd9Sstevel@tonic-gate 			} else {
15397c478bd9Sstevel@tonic-gate 				(void) snprintf(info, info_sz, "%d:\t%s\n",
15407c478bd9Sstevel@tonic-gate 				    (int)psinfo.pr_pid, psargs_conv);
15417c478bd9Sstevel@tonic-gate 			}
15427c478bd9Sstevel@tonic-gate 			(void) printf("%s", info);
15437c478bd9Sstevel@tonic-gate 			free(info);
15447c478bd9Sstevel@tonic-gate 			free(psargs_conv);
15457c478bd9Sstevel@tonic-gate 
15467c478bd9Sstevel@tonic-gate 			if (aflag != 0) {
15477c478bd9Sstevel@tonic-gate 				convert_array(&datap, datap.pd_argv_strs,
15487c478bd9Sstevel@tonic-gate 				    datap.pd_argc, &unprintable);
15497c478bd9Sstevel@tonic-gate 				print_args(&datap);
15507c478bd9Sstevel@tonic-gate 				if (eflag || xflag)
15517c478bd9Sstevel@tonic-gate 					(void) printf("\n");
15527c478bd9Sstevel@tonic-gate 			}
15537c478bd9Sstevel@tonic-gate 
15547c478bd9Sstevel@tonic-gate 			if (eflag != 0) {
15557c478bd9Sstevel@tonic-gate 				convert_array(&datap, datap.pd_envp_strs,
15567c478bd9Sstevel@tonic-gate 				    datap.pd_envc, &unprintable);
15577c478bd9Sstevel@tonic-gate 				print_env(&datap);
15587c478bd9Sstevel@tonic-gate 				if (xflag)
15597c478bd9Sstevel@tonic-gate 					(void) printf("\n");
15607c478bd9Sstevel@tonic-gate 			}
15617c478bd9Sstevel@tonic-gate 
15627c478bd9Sstevel@tonic-gate 			if (xflag != 0) {
15637c478bd9Sstevel@tonic-gate 				convert_array(&datap, datap.pd_auxv_strs,
15647c478bd9Sstevel@tonic-gate 				    datap.pd_auxc, &unprintable);
15657c478bd9Sstevel@tonic-gate 				print_auxv(&datap);
15667c478bd9Sstevel@tonic-gate 			}
15677c478bd9Sstevel@tonic-gate 		}
15687c478bd9Sstevel@tonic-gate 
15697c478bd9Sstevel@tonic-gate 		cleanup_conversions(&datap);
15707c478bd9Sstevel@tonic-gate 		free_data(&datap);
15717c478bd9Sstevel@tonic-gate 	}
15727c478bd9Sstevel@tonic-gate 
15737c478bd9Sstevel@tonic-gate 	return (retc != 0 ? 1 : 0);
15747c478bd9Sstevel@tonic-gate }
1575