xref: /titanic_50/usr/src/lib/libumem/common/envvar.c (revision 789d94c2889bedf502063bc22addcabfa798a438)
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
5*789d94c2Sjwadams  * Common Development and Distribution License (the "License").
6*789d94c2Sjwadams  * 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 /*
22*789d94c2Sjwadams  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <ctype.h>
297c478bd9Sstevel@tonic-gate #include <errno.h>
307c478bd9Sstevel@tonic-gate #include <limits.h>
317c478bd9Sstevel@tonic-gate #include <stdlib.h>
327c478bd9Sstevel@tonic-gate #include <string.h>
337c478bd9Sstevel@tonic-gate #include <dlfcn.h>
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #include "umem_base.h"
367c478bd9Sstevel@tonic-gate #include "vmem_base.h"
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate /*
397c478bd9Sstevel@tonic-gate  * A umem environment variable, like UMEM_DEBUG, is set to a series
407c478bd9Sstevel@tonic-gate  * of items, seperated by ',':
417c478bd9Sstevel@tonic-gate  *
427c478bd9Sstevel@tonic-gate  *   UMEM_DEBUG="audit=10,guards,firewall=512"
437c478bd9Sstevel@tonic-gate  *
447c478bd9Sstevel@tonic-gate  * This structure describes items.  Each item has a name, type, and
457c478bd9Sstevel@tonic-gate  * description.  During processing, an item read from the user may
467c478bd9Sstevel@tonic-gate  * be either "valid" or "invalid".
477c478bd9Sstevel@tonic-gate  *
487c478bd9Sstevel@tonic-gate  * A valid item has an argument, if required, and it is of the right
497c478bd9Sstevel@tonic-gate  * form (doesn't overflow, doesn't contain any unexpected characters).
507c478bd9Sstevel@tonic-gate  *
517c478bd9Sstevel@tonic-gate  * If the item is valid, item_flag_target != NULL, and:
527c478bd9Sstevel@tonic-gate  *	type is not CLEARFLAG, then (*item_flag_target) |= item_flag_value
537c478bd9Sstevel@tonic-gate  *	type is CLEARFLAG, then (*item_flag_target) &= ~item_flag_value
547c478bd9Sstevel@tonic-gate  */
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate #define	UMEM_ENV_ITEM_MAX	512
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate struct umem_env_item;
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate typedef int arg_process_t(const struct umem_env_item *item, const char *value);
617c478bd9Sstevel@tonic-gate #define	ARG_SUCCESS	0	/* processing successful */
627c478bd9Sstevel@tonic-gate #define	ARG_BAD		1	/* argument had a bad value */
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate typedef struct umem_env_item {
657c478bd9Sstevel@tonic-gate 	const char *item_name;	/* tag in environment variable */
667c478bd9Sstevel@tonic-gate 	const char *item_interface_stability;
677c478bd9Sstevel@tonic-gate 	enum {
687c478bd9Sstevel@tonic-gate 	    ITEM_INVALID,
697c478bd9Sstevel@tonic-gate 	    ITEM_FLAG,		/* only a flag.  No argument allowed */
707c478bd9Sstevel@tonic-gate 	    ITEM_CLEARFLAG,	/* only a flag, but clear instead of set */
717c478bd9Sstevel@tonic-gate 	    ITEM_OPTUINT,	/* optional integer argument */
727c478bd9Sstevel@tonic-gate 	    ITEM_UINT,		/* required integer argument */
737c478bd9Sstevel@tonic-gate 	    ITEM_OPTSIZE,	/* optional size_t argument */
747c478bd9Sstevel@tonic-gate 	    ITEM_SIZE,		/* required size_t argument */
757c478bd9Sstevel@tonic-gate 	    ITEM_SPECIAL	/* special argument processing */
767c478bd9Sstevel@tonic-gate 	} item_type;
777c478bd9Sstevel@tonic-gate 	const char *item_description;
787c478bd9Sstevel@tonic-gate 	uint_t *item_flag_target; /* the variable containing the flag */
797c478bd9Sstevel@tonic-gate 	uint_t item_flag_value;	/* the value to OR in */
807c478bd9Sstevel@tonic-gate 	uint_t *item_uint_target; /* the variable to hold the integer */
817c478bd9Sstevel@tonic-gate 	size_t *item_size_target;
827c478bd9Sstevel@tonic-gate 	arg_process_t *item_special; /* callback for special handling */
837c478bd9Sstevel@tonic-gate } umem_env_item_t;
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate #ifndef UMEM_STANDALONE
867c478bd9Sstevel@tonic-gate static arg_process_t umem_backend_process;
877c478bd9Sstevel@tonic-gate #endif
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate static arg_process_t umem_log_process;
907c478bd9Sstevel@tonic-gate 
91*789d94c2Sjwadams static size_t umem_size_tempval;
92*789d94c2Sjwadams static arg_process_t umem_size_process;
93*789d94c2Sjwadams 
947c478bd9Sstevel@tonic-gate const char *____umem_environ_msg_options = "-- UMEM_OPTIONS --";
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate static umem_env_item_t umem_options_items[] = {
977c478bd9Sstevel@tonic-gate #ifndef UMEM_STANDALONE
987c478bd9Sstevel@tonic-gate 	{ "backend",		"Evolving",	ITEM_SPECIAL,
997c478bd9Sstevel@tonic-gate 		"=sbrk for sbrk(2), =mmap for mmap(2)",
1007c478bd9Sstevel@tonic-gate 		NULL, 0, NULL, NULL,
1017c478bd9Sstevel@tonic-gate 		&umem_backend_process
1027c478bd9Sstevel@tonic-gate 	},
1037c478bd9Sstevel@tonic-gate #endif
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 	{ "concurrency",	"Private",	ITEM_UINT,
1067c478bd9Sstevel@tonic-gate 		"Max concurrency",
1077c478bd9Sstevel@tonic-gate 		NULL, 0,	&umem_max_ncpus
1087c478bd9Sstevel@tonic-gate 	},
1097c478bd9Sstevel@tonic-gate 	{ "max_contention",	"Private",	ITEM_UINT,
1107c478bd9Sstevel@tonic-gate 		"Maximum contention in a reap interval before the depot is "
1117c478bd9Sstevel@tonic-gate 		    "resized.",
1127c478bd9Sstevel@tonic-gate 		NULL, 0,	&umem_depot_contention
1137c478bd9Sstevel@tonic-gate 	},
1147c478bd9Sstevel@tonic-gate 	{ "nomagazines",	"Private",	ITEM_FLAG,
1157c478bd9Sstevel@tonic-gate 		"no caches will be multithreaded, and no caching will occur.",
1167c478bd9Sstevel@tonic-gate 		&umem_flags,	UMF_NOMAGAZINE
1177c478bd9Sstevel@tonic-gate 	},
1187c478bd9Sstevel@tonic-gate 	{ "reap_interval",	"Private",	ITEM_UINT,
1197c478bd9Sstevel@tonic-gate 		"Minimum time between reaps and updates, in seconds.",
1207c478bd9Sstevel@tonic-gate 		NULL, 0,	&umem_reap_interval
1217c478bd9Sstevel@tonic-gate 	},
1227c478bd9Sstevel@tonic-gate 
123*789d94c2Sjwadams 	{ "size_add",		"Private",	ITEM_SPECIAL,
124*789d94c2Sjwadams 		"add a size to the cache size table",
125*789d94c2Sjwadams 		NULL, 0, NULL,
126*789d94c2Sjwadams 		&umem_size_tempval,		&umem_size_process
127*789d94c2Sjwadams 	},
128*789d94c2Sjwadams 	{ "size_clear",		"Private",	ITEM_SPECIAL,
129*789d94c2Sjwadams 		"clear all but the largest size from the cache size table",
130*789d94c2Sjwadams 		NULL, 0, NULL,
131*789d94c2Sjwadams 		&umem_size_tempval,		&umem_size_process
132*789d94c2Sjwadams 	},
133*789d94c2Sjwadams 	{ "size_remove",	"Private",	ITEM_SPECIAL,
134*789d94c2Sjwadams 	    "remove a size from the cache size table",
135*789d94c2Sjwadams 		NULL, 0, NULL,
136*789d94c2Sjwadams 		&umem_size_tempval,		&umem_size_process
137*789d94c2Sjwadams 	},
138*789d94c2Sjwadams 
1397c478bd9Sstevel@tonic-gate #ifndef UMEM_STANDALONE
140*789d94c2Sjwadams 	{ "sbrk_minalloc",	"Private",	ITEM_SIZE,
141*789d94c2Sjwadams 		"The minimum allocation chunk for the sbrk(2) heap.",
142*789d94c2Sjwadams 		NULL, 0, NULL,	&vmem_sbrk_minalloc
143*789d94c2Sjwadams 	},
1447c478bd9Sstevel@tonic-gate 	{ "sbrk_pagesize",	"Private",	ITEM_SIZE,
1457c478bd9Sstevel@tonic-gate 		"The preferred page size for the sbrk(2) heap.",
1467c478bd9Sstevel@tonic-gate 		NULL, 0, NULL,	&vmem_sbrk_pagesize
1477c478bd9Sstevel@tonic-gate 	},
1487c478bd9Sstevel@tonic-gate #endif
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 	{ NULL, "-- end of UMEM_OPTIONS --",	ITEM_INVALID }
1517c478bd9Sstevel@tonic-gate };
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate const char *____umem_environ_msg_debug = "-- UMEM_DEBUG --";
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate static umem_env_item_t umem_debug_items[] = {
1567c478bd9Sstevel@tonic-gate 	{ "default",		"Unstable",	ITEM_FLAG,
1577c478bd9Sstevel@tonic-gate 		"audit,contents,guards",
1587c478bd9Sstevel@tonic-gate 		&umem_flags,
1597c478bd9Sstevel@tonic-gate 		UMF_AUDIT | UMF_CONTENTS | UMF_DEADBEEF | UMF_REDZONE
1607c478bd9Sstevel@tonic-gate 	},
1617c478bd9Sstevel@tonic-gate 	{ "audit",		"Unstable",	ITEM_OPTUINT,
1627c478bd9Sstevel@tonic-gate 		"Enable auditing.  optionally =frames to set the number of "
1637c478bd9Sstevel@tonic-gate 		    "stored stack frames",
1647c478bd9Sstevel@tonic-gate 		&umem_flags,	UMF_AUDIT,	&umem_stack_depth
1657c478bd9Sstevel@tonic-gate 	},
1667c478bd9Sstevel@tonic-gate 	{ "contents",		"Unstable",	ITEM_OPTSIZE,
1677c478bd9Sstevel@tonic-gate 		"Enable contents storing.  UMEM_LOGGING=contents also "
1687c478bd9Sstevel@tonic-gate 		    "required.  optionally =bytes to set the number of stored "
1697c478bd9Sstevel@tonic-gate 		    "bytes",
1707c478bd9Sstevel@tonic-gate 		&umem_flags,	UMF_CONTENTS, NULL,	&umem_content_maxsave
1717c478bd9Sstevel@tonic-gate 	},
1727c478bd9Sstevel@tonic-gate 	{ "guards",		"Unstable",	ITEM_FLAG,
1737c478bd9Sstevel@tonic-gate 		"Enables guards and special patterns",
1747c478bd9Sstevel@tonic-gate 		&umem_flags,	UMF_DEADBEEF | UMF_REDZONE
1757c478bd9Sstevel@tonic-gate 	},
1767c478bd9Sstevel@tonic-gate 	{ "verbose",		"Unstable",	ITEM_FLAG,
1777c478bd9Sstevel@tonic-gate 		"Enables writing error messages to stderr",
1787c478bd9Sstevel@tonic-gate 		&umem_output,	1
1797c478bd9Sstevel@tonic-gate 	},
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	{ "nosignal",	"Private",	ITEM_FLAG,
1827c478bd9Sstevel@tonic-gate 		"Abort if called from a signal handler.  Turns on 'audit'.  "
1837c478bd9Sstevel@tonic-gate 		    "Note that this is not always a bug.",
1847c478bd9Sstevel@tonic-gate 		&umem_flags,	UMF_AUDIT | UMF_CHECKSIGNAL
1857c478bd9Sstevel@tonic-gate 	},
1867c478bd9Sstevel@tonic-gate 	{ "firewall",		"Private",	ITEM_SIZE,
1877c478bd9Sstevel@tonic-gate 		"=minbytes.  Every object >= minbytes in size will have its "
1887c478bd9Sstevel@tonic-gate 		    "end against an unmapped page",
1897c478bd9Sstevel@tonic-gate 		&umem_flags,	UMF_FIREWALL,	NULL,	&umem_minfirewall
1907c478bd9Sstevel@tonic-gate 	},
1917c478bd9Sstevel@tonic-gate 	{ "lite",		"Private",	ITEM_FLAG,
1927c478bd9Sstevel@tonic-gate 		"debugging-lite",
1937c478bd9Sstevel@tonic-gate 		&umem_flags,	UMF_LITE
1947c478bd9Sstevel@tonic-gate 	},
1957c478bd9Sstevel@tonic-gate 	{ "maxverify",		"Private",	ITEM_SIZE,
1967c478bd9Sstevel@tonic-gate 		"=maxbytes, Maximum bytes to check when 'guards' is active. "
1977c478bd9Sstevel@tonic-gate 		    "Normally all bytes are checked.",
1987c478bd9Sstevel@tonic-gate 		NULL, 0, NULL,	&umem_maxverify
1997c478bd9Sstevel@tonic-gate 	},
2007c478bd9Sstevel@tonic-gate 	{ "noabort",		"Private",	ITEM_CLEARFLAG,
2017c478bd9Sstevel@tonic-gate 		"umem will not abort when a recoverable error occurs "
2027c478bd9Sstevel@tonic-gate 		    "(i.e. double frees, certain kinds of corruption)",
2037c478bd9Sstevel@tonic-gate 		&umem_abort,	1
2047c478bd9Sstevel@tonic-gate 	},
2057c478bd9Sstevel@tonic-gate 	{ "mtbf",		"Private",	ITEM_UINT,
2067c478bd9Sstevel@tonic-gate 		"=mtbf, the mean time between injected failures.  Works best "
2077c478bd9Sstevel@tonic-gate 		    "if prime.\n",
2087c478bd9Sstevel@tonic-gate 		NULL, 0,	&umem_mtbf
2097c478bd9Sstevel@tonic-gate 	},
2107c478bd9Sstevel@tonic-gate 	{ "random",		"Private",	ITEM_FLAG,
2117c478bd9Sstevel@tonic-gate 		"randomize flags on a per-cache basis",
2127c478bd9Sstevel@tonic-gate 		&umem_flags,	UMF_RANDOMIZE
2137c478bd9Sstevel@tonic-gate 	},
2147c478bd9Sstevel@tonic-gate 	{ "allverbose",		"Private",	ITEM_FLAG,
2157c478bd9Sstevel@tonic-gate 		"Enables writing all logged messages to stderr",
2167c478bd9Sstevel@tonic-gate 		&umem_output,	2
2177c478bd9Sstevel@tonic-gate 	},
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	{ NULL, "-- end of UMEM_DEBUG --",	ITEM_INVALID }
2207c478bd9Sstevel@tonic-gate };
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate const char *____umem_environ_msg_logging = "-- UMEM_LOGGING --";
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate static umem_env_item_t umem_logging_items[] = {
2257c478bd9Sstevel@tonic-gate 	{ "transaction",	"Unstable",	ITEM_SPECIAL,
2267c478bd9Sstevel@tonic-gate 		"If 'audit' is set in UMEM_DEBUG, the audit structures "
2277c478bd9Sstevel@tonic-gate 		    "from previous transactions are entered into this log.",
2287c478bd9Sstevel@tonic-gate 		NULL, 0, NULL,
2297c478bd9Sstevel@tonic-gate 		&umem_transaction_log_size,	&umem_log_process
2307c478bd9Sstevel@tonic-gate 	},
2317c478bd9Sstevel@tonic-gate 	{ "contents",		"Unstable",	ITEM_SPECIAL,
2327c478bd9Sstevel@tonic-gate 		"If 'audit' is set in UMEM_DEBUG, the contents of objects "
2337c478bd9Sstevel@tonic-gate 		    "are recorded in this log as they are freed.  If the "
2347c478bd9Sstevel@tonic-gate 		    "'contents' option is not set in UMEM_DEBUG, the first "
2357c478bd9Sstevel@tonic-gate 		    "256 bytes of each freed buffer will be saved.",
2367c478bd9Sstevel@tonic-gate 		&umem_flags,	UMF_CONTENTS,	NULL,
2377c478bd9Sstevel@tonic-gate 		&umem_content_log_size,		&umem_log_process
2387c478bd9Sstevel@tonic-gate 	},
2397c478bd9Sstevel@tonic-gate 	{ "fail",		"Unstable",	ITEM_SPECIAL,
2407c478bd9Sstevel@tonic-gate 		"Records are entered into this log for every failed "
2417c478bd9Sstevel@tonic-gate 		    "allocation.",
2427c478bd9Sstevel@tonic-gate 		NULL, 0, NULL,
2437c478bd9Sstevel@tonic-gate 		&umem_failure_log_size,		&umem_log_process
2447c478bd9Sstevel@tonic-gate 	},
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 	{ "slab",		"Private",	ITEM_SPECIAL,
2477c478bd9Sstevel@tonic-gate 		"Every slab created will be entered into this log.",
2487c478bd9Sstevel@tonic-gate 		NULL, 0, NULL,
2497c478bd9Sstevel@tonic-gate 		&umem_slab_log_size,		&umem_log_process
2507c478bd9Sstevel@tonic-gate 	},
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 	{ NULL, "-- end of UMEM_LOGGING --",	ITEM_INVALID }
2537c478bd9Sstevel@tonic-gate };
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate typedef struct umem_envvar {
2567c478bd9Sstevel@tonic-gate 	const char *env_name;
2577c478bd9Sstevel@tonic-gate 	const char *env_func;
2587c478bd9Sstevel@tonic-gate 	umem_env_item_t	*env_item_list;
2597c478bd9Sstevel@tonic-gate 	const char *env_getenv_result;
2607c478bd9Sstevel@tonic-gate 	const char *env_func_result;
2617c478bd9Sstevel@tonic-gate } umem_envvar_t;
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate static umem_envvar_t umem_envvars[] = {
2647c478bd9Sstevel@tonic-gate 	{ "UMEM_DEBUG",		"_umem_debug_init",	umem_debug_items },
2657c478bd9Sstevel@tonic-gate 	{ "UMEM_OPTIONS",	"_umem_options_init",	umem_options_items },
2667c478bd9Sstevel@tonic-gate 	{ "UMEM_LOGGING",	"_umem_logging_init",	umem_logging_items },
2677c478bd9Sstevel@tonic-gate 	{ NULL, NULL, NULL }
2687c478bd9Sstevel@tonic-gate };
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate static umem_envvar_t *env_current;
2717c478bd9Sstevel@tonic-gate #define	CURRENT		(env_current->env_name)
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate static int
2747c478bd9Sstevel@tonic-gate empty(const char *str)
2757c478bd9Sstevel@tonic-gate {
2767c478bd9Sstevel@tonic-gate 	char c;
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 	while ((c = *str) != '\0' && isspace(c))
2797c478bd9Sstevel@tonic-gate 		str++;
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 	return (*str == '\0');
2827c478bd9Sstevel@tonic-gate }
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate static int
2857c478bd9Sstevel@tonic-gate item_uint_process(const umem_env_item_t *item, const char *item_arg)
2867c478bd9Sstevel@tonic-gate {
2877c478bd9Sstevel@tonic-gate 	ulong_t result;
2887c478bd9Sstevel@tonic-gate 	char *endptr = "";
2897c478bd9Sstevel@tonic-gate 	int olderrno;
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 	olderrno = errno;
2927c478bd9Sstevel@tonic-gate 	errno = 0;
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 	if (empty(item_arg)) {
2957c478bd9Sstevel@tonic-gate 		goto badnumber;
2967c478bd9Sstevel@tonic-gate 	}
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	result = strtoul(item_arg, &endptr, 10);
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 	if (result == ULONG_MAX && errno == ERANGE) {
3017c478bd9Sstevel@tonic-gate 		errno = olderrno;
3027c478bd9Sstevel@tonic-gate 		goto overflow;
3037c478bd9Sstevel@tonic-gate 	}
3047c478bd9Sstevel@tonic-gate 	errno = olderrno;
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	if (*endptr != '\0')
3077c478bd9Sstevel@tonic-gate 		goto badnumber;
3087c478bd9Sstevel@tonic-gate 	if ((uint_t)result != result)
3097c478bd9Sstevel@tonic-gate 		goto overflow;
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 	(*item->item_uint_target) = (uint_t)result;
3127c478bd9Sstevel@tonic-gate 	return (ARG_SUCCESS);
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate badnumber:
3157c478bd9Sstevel@tonic-gate 	log_message("%s: %s: not a number\n", CURRENT, item->item_name);
3167c478bd9Sstevel@tonic-gate 	return (ARG_BAD);
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate overflow:
3197c478bd9Sstevel@tonic-gate 	log_message("%s: %s: overflowed\n", CURRENT, item->item_name);
3207c478bd9Sstevel@tonic-gate 	return (ARG_BAD);
3217c478bd9Sstevel@tonic-gate }
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate static int
3247c478bd9Sstevel@tonic-gate item_size_process(const umem_env_item_t *item, const char *item_arg)
3257c478bd9Sstevel@tonic-gate {
3267c478bd9Sstevel@tonic-gate 	ulong_t result;
3277c478bd9Sstevel@tonic-gate 	ulong_t result_arg;
3287c478bd9Sstevel@tonic-gate 	char *endptr = "";
3297c478bd9Sstevel@tonic-gate 	int olderrno;
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 	if (empty(item_arg))
3327c478bd9Sstevel@tonic-gate 		goto badnumber;
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 	olderrno = errno;
3357c478bd9Sstevel@tonic-gate 	errno = 0;
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 	result_arg = strtoul(item_arg, &endptr, 10);
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 	if (result_arg == ULONG_MAX && errno == ERANGE) {
3407c478bd9Sstevel@tonic-gate 		errno = olderrno;
3417c478bd9Sstevel@tonic-gate 		goto overflow;
3427c478bd9Sstevel@tonic-gate 	}
3437c478bd9Sstevel@tonic-gate 	errno = olderrno;
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate 	result = result_arg;
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 	switch (*endptr) {
3487c478bd9Sstevel@tonic-gate 	case 't':
3497c478bd9Sstevel@tonic-gate 	case 'T':
3507c478bd9Sstevel@tonic-gate 		result *= 1024;
3517c478bd9Sstevel@tonic-gate 		if (result < result_arg)
3527c478bd9Sstevel@tonic-gate 			goto overflow;
3537c478bd9Sstevel@tonic-gate 		/*FALLTHRU*/
3547c478bd9Sstevel@tonic-gate 	case 'g':
3557c478bd9Sstevel@tonic-gate 	case 'G':
3567c478bd9Sstevel@tonic-gate 		result *= 1024;
3577c478bd9Sstevel@tonic-gate 		if (result < result_arg)
3587c478bd9Sstevel@tonic-gate 			goto overflow;
3597c478bd9Sstevel@tonic-gate 		/*FALLTHRU*/
3607c478bd9Sstevel@tonic-gate 	case 'm':
3617c478bd9Sstevel@tonic-gate 	case 'M':
3627c478bd9Sstevel@tonic-gate 		result *= 1024;
3637c478bd9Sstevel@tonic-gate 		if (result < result_arg)
3647c478bd9Sstevel@tonic-gate 			goto overflow;
3657c478bd9Sstevel@tonic-gate 		/*FALLTHRU*/
3667c478bd9Sstevel@tonic-gate 	case 'k':
3677c478bd9Sstevel@tonic-gate 	case 'K':
3687c478bd9Sstevel@tonic-gate 		result *= 1024;
3697c478bd9Sstevel@tonic-gate 		if (result < result_arg)
3707c478bd9Sstevel@tonic-gate 			goto overflow;
3717c478bd9Sstevel@tonic-gate 		endptr++;		/* skip over the size character */
3727c478bd9Sstevel@tonic-gate 		break;
3737c478bd9Sstevel@tonic-gate 	default:
3747c478bd9Sstevel@tonic-gate 		break;			/* handled later */
3757c478bd9Sstevel@tonic-gate 	}
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate 	if (*endptr != '\0')
3787c478bd9Sstevel@tonic-gate 		goto badnumber;
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 	(*item->item_size_target) = result;
3817c478bd9Sstevel@tonic-gate 	return (ARG_SUCCESS);
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate badnumber:
3847c478bd9Sstevel@tonic-gate 	log_message("%s: %s: not a number\n", CURRENT, item->item_name);
3857c478bd9Sstevel@tonic-gate 	return (ARG_BAD);
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate overflow:
3887c478bd9Sstevel@tonic-gate 	log_message("%s: %s: overflowed\n", CURRENT, item->item_name);
3897c478bd9Sstevel@tonic-gate 	return (ARG_BAD);
3907c478bd9Sstevel@tonic-gate }
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate static int
3937c478bd9Sstevel@tonic-gate umem_log_process(const umem_env_item_t *item, const char *item_arg)
3947c478bd9Sstevel@tonic-gate {
3957c478bd9Sstevel@tonic-gate 	if (item_arg != NULL) {
3967c478bd9Sstevel@tonic-gate 		int ret;
3977c478bd9Sstevel@tonic-gate 		ret = item_size_process(item, item_arg);
3987c478bd9Sstevel@tonic-gate 		if (ret != ARG_SUCCESS)
3997c478bd9Sstevel@tonic-gate 			return (ret);
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate 		if (*item->item_size_target == 0)
4027c478bd9Sstevel@tonic-gate 			return (ARG_SUCCESS);
4037c478bd9Sstevel@tonic-gate 	} else
4047c478bd9Sstevel@tonic-gate 		*item->item_size_target = 64*1024;
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 	umem_logging = 1;
4077c478bd9Sstevel@tonic-gate 	return (ARG_SUCCESS);
4087c478bd9Sstevel@tonic-gate }
4097c478bd9Sstevel@tonic-gate 
410*789d94c2Sjwadams static int
411*789d94c2Sjwadams umem_size_process(const umem_env_item_t *item, const char *item_arg)
412*789d94c2Sjwadams {
413*789d94c2Sjwadams 	const char *name = item->item_name;
414*789d94c2Sjwadams 	void (*action_func)(size_t);
415*789d94c2Sjwadams 
416*789d94c2Sjwadams 	size_t result;
417*789d94c2Sjwadams 
418*789d94c2Sjwadams 	int ret;
419*789d94c2Sjwadams 
420*789d94c2Sjwadams 	if (strcmp(name, "size_clear") == 0) {
421*789d94c2Sjwadams 		if (item_arg != NULL) {
422*789d94c2Sjwadams 			log_message("%s: %s: does not take a value. ignored\n",
423*789d94c2Sjwadams 			    CURRENT, name);
424*789d94c2Sjwadams 			return (ARG_BAD);
425*789d94c2Sjwadams 		}
426*789d94c2Sjwadams 		umem_alloc_sizes_clear();
427*789d94c2Sjwadams 		return (ARG_SUCCESS);
428*789d94c2Sjwadams 	} else if (strcmp(name, "size_add") == 0) {
429*789d94c2Sjwadams 		action_func = umem_alloc_sizes_add;
430*789d94c2Sjwadams 	} else if (strcmp(name, "size_remove") == 0) {
431*789d94c2Sjwadams 		action_func = umem_alloc_sizes_remove;
432*789d94c2Sjwadams 	} else {
433*789d94c2Sjwadams 		log_message("%s: %s: internally unrecognized\n",
434*789d94c2Sjwadams 		    CURRENT, name, name, name);
435*789d94c2Sjwadams 		return (ARG_BAD);
436*789d94c2Sjwadams 	}
437*789d94c2Sjwadams 
438*789d94c2Sjwadams 	if (item_arg == NULL) {
439*789d94c2Sjwadams 		log_message("%s: %s: requires a value. ignored\n",
440*789d94c2Sjwadams 		    CURRENT, name);
441*789d94c2Sjwadams 		return (ARG_BAD);
442*789d94c2Sjwadams 	}
443*789d94c2Sjwadams 
444*789d94c2Sjwadams 	ret = item_size_process(item, item_arg);
445*789d94c2Sjwadams 	if (ret != ARG_SUCCESS)
446*789d94c2Sjwadams 		return (ret);
447*789d94c2Sjwadams 
448*789d94c2Sjwadams 	result = *item->item_size_target;
449*789d94c2Sjwadams 	action_func(result);
450*789d94c2Sjwadams 	return (ARG_SUCCESS);
451*789d94c2Sjwadams }
452*789d94c2Sjwadams 
4537c478bd9Sstevel@tonic-gate #ifndef UMEM_STANDALONE
4547c478bd9Sstevel@tonic-gate static int
4557c478bd9Sstevel@tonic-gate umem_backend_process(const umem_env_item_t *item, const char *item_arg)
4567c478bd9Sstevel@tonic-gate {
4577c478bd9Sstevel@tonic-gate 	const char *name = item->item_name;
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 	if (item_arg == NULL)
4607c478bd9Sstevel@tonic-gate 		goto fail;
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 	if (strcmp(item_arg, "sbrk") == 0)
4637c478bd9Sstevel@tonic-gate 		vmem_backend |= VMEM_BACKEND_SBRK;
4647c478bd9Sstevel@tonic-gate 	else if (strcmp(item_arg, "mmap") == 0)
4657c478bd9Sstevel@tonic-gate 		vmem_backend |= VMEM_BACKEND_MMAP;
4667c478bd9Sstevel@tonic-gate 	else
4677c478bd9Sstevel@tonic-gate 		goto fail;
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 	return (ARG_SUCCESS);
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate fail:
4727c478bd9Sstevel@tonic-gate 	log_message("%s: %s: must be %s=sbrk or %s=mmap\n",
4737c478bd9Sstevel@tonic-gate 	    CURRENT, name, name, name);
4747c478bd9Sstevel@tonic-gate 	return (ARG_BAD);
4757c478bd9Sstevel@tonic-gate }
4767c478bd9Sstevel@tonic-gate #endif
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate static int
4797c478bd9Sstevel@tonic-gate process_item(const umem_env_item_t *item, const char *item_arg)
4807c478bd9Sstevel@tonic-gate {
4817c478bd9Sstevel@tonic-gate 	int arg_required = 0;
4827c478bd9Sstevel@tonic-gate 	arg_process_t *processor;
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 	switch (item->item_type) {
4857c478bd9Sstevel@tonic-gate 	case ITEM_FLAG:
4867c478bd9Sstevel@tonic-gate 	case ITEM_CLEARFLAG:
4877c478bd9Sstevel@tonic-gate 	case ITEM_OPTUINT:
4887c478bd9Sstevel@tonic-gate 	case ITEM_OPTSIZE:
4897c478bd9Sstevel@tonic-gate 	case ITEM_SPECIAL:
4907c478bd9Sstevel@tonic-gate 		arg_required = 0;
4917c478bd9Sstevel@tonic-gate 		break;
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate 	case ITEM_UINT:
4947c478bd9Sstevel@tonic-gate 	case ITEM_SIZE:
4957c478bd9Sstevel@tonic-gate 		arg_required = 1;
4967c478bd9Sstevel@tonic-gate 		break;
4977c478bd9Sstevel@tonic-gate 	}
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate 	switch (item->item_type) {
5007c478bd9Sstevel@tonic-gate 	case ITEM_FLAG:
5017c478bd9Sstevel@tonic-gate 	case ITEM_CLEARFLAG:
5027c478bd9Sstevel@tonic-gate 		if (item_arg != NULL) {
5037c478bd9Sstevel@tonic-gate 			log_message("%s: %s: does not take a value. ignored\n",
5047c478bd9Sstevel@tonic-gate 			    CURRENT, item->item_name);
5057c478bd9Sstevel@tonic-gate 			return (1);
5067c478bd9Sstevel@tonic-gate 		}
5077c478bd9Sstevel@tonic-gate 		processor = NULL;
5087c478bd9Sstevel@tonic-gate 		break;
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate 	case ITEM_UINT:
5117c478bd9Sstevel@tonic-gate 	case ITEM_OPTUINT:
5127c478bd9Sstevel@tonic-gate 		processor = item_uint_process;
5137c478bd9Sstevel@tonic-gate 		break;
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate 	case ITEM_SIZE:
5167c478bd9Sstevel@tonic-gate 	case ITEM_OPTSIZE:
5177c478bd9Sstevel@tonic-gate 		processor = item_size_process;
5187c478bd9Sstevel@tonic-gate 		break;
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate 	case ITEM_SPECIAL:
5217c478bd9Sstevel@tonic-gate 		processor = item->item_special;
5227c478bd9Sstevel@tonic-gate 		break;
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate 	default:
5257c478bd9Sstevel@tonic-gate 		log_message("%s: %s: Invalid type.  Ignored\n",
5267c478bd9Sstevel@tonic-gate 		    CURRENT, item->item_name);
5277c478bd9Sstevel@tonic-gate 		return (1);
5287c478bd9Sstevel@tonic-gate 	}
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate 	if (arg_required && item_arg == NULL) {
5317c478bd9Sstevel@tonic-gate 		log_message("%s: %s: Required value missing\n",
5327c478bd9Sstevel@tonic-gate 		    CURRENT, item->item_name);
5337c478bd9Sstevel@tonic-gate 		goto invalid;
5347c478bd9Sstevel@tonic-gate 	}
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate 	if (item_arg != NULL || item->item_type == ITEM_SPECIAL) {
5377c478bd9Sstevel@tonic-gate 		if (processor(item, item_arg) != ARG_SUCCESS)
5387c478bd9Sstevel@tonic-gate 			goto invalid;
5397c478bd9Sstevel@tonic-gate 	}
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 	if (item->item_flag_target) {
5427c478bd9Sstevel@tonic-gate 		if (item->item_type == ITEM_CLEARFLAG)
5437c478bd9Sstevel@tonic-gate 			(*item->item_flag_target) &= ~item->item_flag_value;
5447c478bd9Sstevel@tonic-gate 		else
5457c478bd9Sstevel@tonic-gate 			(*item->item_flag_target) |= item->item_flag_value;
5467c478bd9Sstevel@tonic-gate 	}
5477c478bd9Sstevel@tonic-gate 	return (0);
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate invalid:
5507c478bd9Sstevel@tonic-gate 	return (1);
5517c478bd9Sstevel@tonic-gate }
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate #define	ENV_SHORT_BYTES	10	/* bytes to print on error */
5547c478bd9Sstevel@tonic-gate void
5557c478bd9Sstevel@tonic-gate umem_process_value(umem_env_item_t *item_list, const char *beg, const char *end)
5567c478bd9Sstevel@tonic-gate {
5577c478bd9Sstevel@tonic-gate 	char buf[UMEM_ENV_ITEM_MAX];
5587c478bd9Sstevel@tonic-gate 	char *argptr;
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate 	size_t count;
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate 	while (beg < end && isspace(*beg))
5637c478bd9Sstevel@tonic-gate 		beg++;
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate 	while (beg < end && isspace(*(end - 1)))
5667c478bd9Sstevel@tonic-gate 		end--;
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate 	if (beg >= end) {
5697c478bd9Sstevel@tonic-gate 		log_message("%s: empty option\n", CURRENT);
5707c478bd9Sstevel@tonic-gate 		return;
5717c478bd9Sstevel@tonic-gate 	}
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate 	count = end - beg;
5747c478bd9Sstevel@tonic-gate 
5757c478bd9Sstevel@tonic-gate 	if (count + 1 > sizeof (buf)) {
5767c478bd9Sstevel@tonic-gate 		char outbuf[ENV_SHORT_BYTES + 1];
5777c478bd9Sstevel@tonic-gate 		/*
5787c478bd9Sstevel@tonic-gate 		 * Have to do this, since sprintf("%10s",...) calls malloc()
5797c478bd9Sstevel@tonic-gate 		 */
5807c478bd9Sstevel@tonic-gate 		(void) strncpy(outbuf, beg, ENV_SHORT_BYTES);
5817c478bd9Sstevel@tonic-gate 		outbuf[ENV_SHORT_BYTES] = 0;
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate 		log_message("%s: argument \"%s...\" too long\n", CURRENT,
5847c478bd9Sstevel@tonic-gate 		    outbuf);
5857c478bd9Sstevel@tonic-gate 		return;
5867c478bd9Sstevel@tonic-gate 	}
5877c478bd9Sstevel@tonic-gate 
5887c478bd9Sstevel@tonic-gate 	(void) strncpy(buf, beg, count);
5897c478bd9Sstevel@tonic-gate 	buf[count] = 0;
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate 	argptr = strchr(buf, '=');
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate 	if (argptr != NULL)
5947c478bd9Sstevel@tonic-gate 		*argptr++ = 0;
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate 	for (; item_list->item_name != NULL; item_list++) {
5977c478bd9Sstevel@tonic-gate 		if (strcmp(buf, item_list->item_name) == 0) {
5987c478bd9Sstevel@tonic-gate 			(void) process_item(item_list, argptr);
5997c478bd9Sstevel@tonic-gate 			return;
6007c478bd9Sstevel@tonic-gate 		}
6017c478bd9Sstevel@tonic-gate 	}
6027c478bd9Sstevel@tonic-gate 	log_message("%s: '%s' not recognized\n", CURRENT, buf);
6037c478bd9Sstevel@tonic-gate }
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate /*ARGSUSED*/
6067c478bd9Sstevel@tonic-gate void
6077c478bd9Sstevel@tonic-gate umem_setup_envvars(int invalid)
6087c478bd9Sstevel@tonic-gate {
6097c478bd9Sstevel@tonic-gate 	umem_envvar_t *cur_env;
6107c478bd9Sstevel@tonic-gate 	static volatile enum {
6117c478bd9Sstevel@tonic-gate 		STATE_START,
6127c478bd9Sstevel@tonic-gate 		STATE_GETENV,
6132d9e8f45Sjwadams 		STATE_DLOPEN,
6147c478bd9Sstevel@tonic-gate 		STATE_DLSYM,
6157c478bd9Sstevel@tonic-gate 		STATE_FUNC,
6167c478bd9Sstevel@tonic-gate 		STATE_DONE
6177c478bd9Sstevel@tonic-gate 	} state = STATE_START;
6187c478bd9Sstevel@tonic-gate #ifndef UMEM_STANDALONE
6197c478bd9Sstevel@tonic-gate 	void *h;
6207c478bd9Sstevel@tonic-gate #endif
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate 	if (invalid) {
6237c478bd9Sstevel@tonic-gate 		const char *where;
6247c478bd9Sstevel@tonic-gate 		/*
6257c478bd9Sstevel@tonic-gate 		 * One of the calls below invoked malloc() recursively.  We
6267c478bd9Sstevel@tonic-gate 		 * remove any partial results and return.
6277c478bd9Sstevel@tonic-gate 		 */
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate 		switch (state) {
6307c478bd9Sstevel@tonic-gate 		case STATE_START:
6317c478bd9Sstevel@tonic-gate 			where = "before getenv(3C) calls -- "
6327c478bd9Sstevel@tonic-gate 			    "getenv(3C) results ignored.";
6337c478bd9Sstevel@tonic-gate 			break;
6347c478bd9Sstevel@tonic-gate 		case STATE_GETENV:
6357c478bd9Sstevel@tonic-gate 			where = "during getenv(3C) calls -- "
6367c478bd9Sstevel@tonic-gate 			    "getenv(3C) results ignored.";
6377c478bd9Sstevel@tonic-gate 			break;
6382d9e8f45Sjwadams 		case STATE_DLOPEN:
6392d9e8f45Sjwadams 			where = "during dlopen(3C) call -- "
6402d9e8f45Sjwadams 			    "_umem_*() results ignored.";
6412d9e8f45Sjwadams 			break;
6427c478bd9Sstevel@tonic-gate 		case STATE_DLSYM:
6437c478bd9Sstevel@tonic-gate 			where = "during dlsym(3C) call -- "
6447c478bd9Sstevel@tonic-gate 			    "_umem_*() results ignored.";
6457c478bd9Sstevel@tonic-gate 			break;
6467c478bd9Sstevel@tonic-gate 		case STATE_FUNC:
6477c478bd9Sstevel@tonic-gate 			where = "during _umem_*() call -- "
6487c478bd9Sstevel@tonic-gate 			    "_umem_*() results ignored.";
6497c478bd9Sstevel@tonic-gate 			break;
6507c478bd9Sstevel@tonic-gate 		case STATE_DONE:
6517c478bd9Sstevel@tonic-gate 			where = "after dlsym() or _umem_*() calls.";
6527c478bd9Sstevel@tonic-gate 			break;
6537c478bd9Sstevel@tonic-gate 		default:
6547c478bd9Sstevel@tonic-gate 			where = "at unknown point -- "
6557c478bd9Sstevel@tonic-gate 			    "_umem_*() results ignored.";
6567c478bd9Sstevel@tonic-gate 			break;
6577c478bd9Sstevel@tonic-gate 		}
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate 		log_message("recursive allocation %s\n", where);
6607c478bd9Sstevel@tonic-gate 
6617c478bd9Sstevel@tonic-gate 		for (cur_env = umem_envvars; cur_env->env_name != NULL;
6627c478bd9Sstevel@tonic-gate 		    cur_env++) {
6637c478bd9Sstevel@tonic-gate 			if (state == STATE_GETENV)
6647c478bd9Sstevel@tonic-gate 				cur_env->env_getenv_result = NULL;
6657c478bd9Sstevel@tonic-gate 			if (state != STATE_DONE)
6667c478bd9Sstevel@tonic-gate 				cur_env->env_func_result = NULL;
6677c478bd9Sstevel@tonic-gate 		}
6687c478bd9Sstevel@tonic-gate 
6697c478bd9Sstevel@tonic-gate 		state = STATE_DONE;
6707c478bd9Sstevel@tonic-gate 		return;
6717c478bd9Sstevel@tonic-gate 	}
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 	state = STATE_GETENV;
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate 	for (cur_env = umem_envvars; cur_env->env_name != NULL; cur_env++) {
6767c478bd9Sstevel@tonic-gate 		cur_env->env_getenv_result = getenv(cur_env->env_name);
6777c478bd9Sstevel@tonic-gate 		if (state == STATE_DONE)
6787c478bd9Sstevel@tonic-gate 			return;		/* recursed */
6797c478bd9Sstevel@tonic-gate 	}
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate #ifndef UMEM_STANDALONE
6822d9e8f45Sjwadams 	state = STATE_DLOPEN;
6832d9e8f45Sjwadams 
6847c478bd9Sstevel@tonic-gate 	/* get a handle to the "a.out" object */
6857c478bd9Sstevel@tonic-gate 	if ((h = dlopen(0, RTLD_FIRST | RTLD_LAZY)) != NULL) {
6867c478bd9Sstevel@tonic-gate 		for (cur_env = umem_envvars; cur_env->env_name != NULL;
6877c478bd9Sstevel@tonic-gate 		    cur_env++) {
6887c478bd9Sstevel@tonic-gate 			const char *(*func)(void);
6897c478bd9Sstevel@tonic-gate 			const char *value;
6907c478bd9Sstevel@tonic-gate 
6917c478bd9Sstevel@tonic-gate 			state = STATE_DLSYM;
6927c478bd9Sstevel@tonic-gate 			func = (const char *(*)(void))dlsym(h,
6937c478bd9Sstevel@tonic-gate 			    cur_env->env_func);
6947c478bd9Sstevel@tonic-gate 
6957c478bd9Sstevel@tonic-gate 			if (state == STATE_DONE)
6967c478bd9Sstevel@tonic-gate 				break;		/* recursed */
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate 			state = STATE_FUNC;
6997c478bd9Sstevel@tonic-gate 			if (func != NULL) {
7007c478bd9Sstevel@tonic-gate 				value = func();
7017c478bd9Sstevel@tonic-gate 				if (state == STATE_DONE)
7027c478bd9Sstevel@tonic-gate 					break;		/* recursed */
7037c478bd9Sstevel@tonic-gate 				cur_env->env_func_result = value;
7047c478bd9Sstevel@tonic-gate 			}
7057c478bd9Sstevel@tonic-gate 		}
7067c478bd9Sstevel@tonic-gate 		(void) dlclose(h);
7077c478bd9Sstevel@tonic-gate 	} else {
7087c478bd9Sstevel@tonic-gate 		(void) dlerror();		/* snarf dlerror() */
7097c478bd9Sstevel@tonic-gate 	}
7107c478bd9Sstevel@tonic-gate #endif /* UMEM_STANDALONE */
7117c478bd9Sstevel@tonic-gate 
7127c478bd9Sstevel@tonic-gate 	state = STATE_DONE;
7137c478bd9Sstevel@tonic-gate }
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate /*
7167c478bd9Sstevel@tonic-gate  * Process the environment variables.
7177c478bd9Sstevel@tonic-gate  */
7187c478bd9Sstevel@tonic-gate void
7197c478bd9Sstevel@tonic-gate umem_process_envvars(void)
7207c478bd9Sstevel@tonic-gate {
7217c478bd9Sstevel@tonic-gate 	const char *value;
7227c478bd9Sstevel@tonic-gate 	const char *end, *next;
7237c478bd9Sstevel@tonic-gate 	umem_envvar_t *cur_env;
7247c478bd9Sstevel@tonic-gate 
7257c478bd9Sstevel@tonic-gate 	for (cur_env = umem_envvars; cur_env->env_name != NULL; cur_env++) {
7267c478bd9Sstevel@tonic-gate 		env_current = cur_env;
7277c478bd9Sstevel@tonic-gate 
7287c478bd9Sstevel@tonic-gate 		value = cur_env->env_getenv_result;
7297c478bd9Sstevel@tonic-gate 		if (value == NULL)
7307c478bd9Sstevel@tonic-gate 			value = cur_env->env_func_result;
7317c478bd9Sstevel@tonic-gate 
7327c478bd9Sstevel@tonic-gate 		/* ignore if missing or empty */
7337c478bd9Sstevel@tonic-gate 		if (value == NULL)
7347c478bd9Sstevel@tonic-gate 			continue;
7357c478bd9Sstevel@tonic-gate 
7367c478bd9Sstevel@tonic-gate 		for (end = value; *end != '\0'; value = next) {
7377c478bd9Sstevel@tonic-gate 			end = strchr(value, ',');
7387c478bd9Sstevel@tonic-gate 			if (end != NULL)
7397c478bd9Sstevel@tonic-gate 				next = end + 1;		/* skip the comma */
7407c478bd9Sstevel@tonic-gate 			else
7417c478bd9Sstevel@tonic-gate 				next = end = value + strlen(value);
7427c478bd9Sstevel@tonic-gate 
7437c478bd9Sstevel@tonic-gate 			umem_process_value(cur_env->env_item_list, value, end);
7447c478bd9Sstevel@tonic-gate 		}
7457c478bd9Sstevel@tonic-gate 	}
7467c478bd9Sstevel@tonic-gate }
747