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 57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 77c478bd9Sstevel@tonic-gate * with the License. 87c478bd9Sstevel@tonic-gate * 97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 127c478bd9Sstevel@tonic-gate * and limitations under the License. 137c478bd9Sstevel@tonic-gate * 147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 197c478bd9Sstevel@tonic-gate * 207c478bd9Sstevel@tonic-gate * CDDL HEADER END 217c478bd9Sstevel@tonic-gate */ 227c478bd9Sstevel@tonic-gate /* 23*2d9e8f45Sjwadams * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate #include <ctype.h> 307c478bd9Sstevel@tonic-gate #include <errno.h> 317c478bd9Sstevel@tonic-gate #include <limits.h> 327c478bd9Sstevel@tonic-gate #include <stdlib.h> 337c478bd9Sstevel@tonic-gate #include <string.h> 347c478bd9Sstevel@tonic-gate #include <dlfcn.h> 357c478bd9Sstevel@tonic-gate 367c478bd9Sstevel@tonic-gate #include "umem_base.h" 377c478bd9Sstevel@tonic-gate #include "vmem_base.h" 387c478bd9Sstevel@tonic-gate 397c478bd9Sstevel@tonic-gate /* 407c478bd9Sstevel@tonic-gate * A umem environment variable, like UMEM_DEBUG, is set to a series 417c478bd9Sstevel@tonic-gate * of items, seperated by ',': 427c478bd9Sstevel@tonic-gate * 437c478bd9Sstevel@tonic-gate * UMEM_DEBUG="audit=10,guards,firewall=512" 447c478bd9Sstevel@tonic-gate * 457c478bd9Sstevel@tonic-gate * This structure describes items. Each item has a name, type, and 467c478bd9Sstevel@tonic-gate * description. During processing, an item read from the user may 477c478bd9Sstevel@tonic-gate * be either "valid" or "invalid". 487c478bd9Sstevel@tonic-gate * 497c478bd9Sstevel@tonic-gate * A valid item has an argument, if required, and it is of the right 507c478bd9Sstevel@tonic-gate * form (doesn't overflow, doesn't contain any unexpected characters). 517c478bd9Sstevel@tonic-gate * 527c478bd9Sstevel@tonic-gate * If the item is valid, item_flag_target != NULL, and: 537c478bd9Sstevel@tonic-gate * type is not CLEARFLAG, then (*item_flag_target) |= item_flag_value 547c478bd9Sstevel@tonic-gate * type is CLEARFLAG, then (*item_flag_target) &= ~item_flag_value 557c478bd9Sstevel@tonic-gate */ 567c478bd9Sstevel@tonic-gate 577c478bd9Sstevel@tonic-gate #define UMEM_ENV_ITEM_MAX 512 587c478bd9Sstevel@tonic-gate 597c478bd9Sstevel@tonic-gate struct umem_env_item; 607c478bd9Sstevel@tonic-gate 617c478bd9Sstevel@tonic-gate typedef int arg_process_t(const struct umem_env_item *item, const char *value); 627c478bd9Sstevel@tonic-gate #define ARG_SUCCESS 0 /* processing successful */ 637c478bd9Sstevel@tonic-gate #define ARG_BAD 1 /* argument had a bad value */ 647c478bd9Sstevel@tonic-gate 657c478bd9Sstevel@tonic-gate typedef struct umem_env_item { 667c478bd9Sstevel@tonic-gate const char *item_name; /* tag in environment variable */ 677c478bd9Sstevel@tonic-gate const char *item_interface_stability; 687c478bd9Sstevel@tonic-gate enum { 697c478bd9Sstevel@tonic-gate ITEM_INVALID, 707c478bd9Sstevel@tonic-gate ITEM_FLAG, /* only a flag. No argument allowed */ 717c478bd9Sstevel@tonic-gate ITEM_CLEARFLAG, /* only a flag, but clear instead of set */ 727c478bd9Sstevel@tonic-gate ITEM_OPTUINT, /* optional integer argument */ 737c478bd9Sstevel@tonic-gate ITEM_UINT, /* required integer argument */ 747c478bd9Sstevel@tonic-gate ITEM_OPTSIZE, /* optional size_t argument */ 757c478bd9Sstevel@tonic-gate ITEM_SIZE, /* required size_t argument */ 767c478bd9Sstevel@tonic-gate ITEM_SPECIAL /* special argument processing */ 777c478bd9Sstevel@tonic-gate } item_type; 787c478bd9Sstevel@tonic-gate const char *item_description; 797c478bd9Sstevel@tonic-gate uint_t *item_flag_target; /* the variable containing the flag */ 807c478bd9Sstevel@tonic-gate uint_t item_flag_value; /* the value to OR in */ 817c478bd9Sstevel@tonic-gate uint_t *item_uint_target; /* the variable to hold the integer */ 827c478bd9Sstevel@tonic-gate size_t *item_size_target; 837c478bd9Sstevel@tonic-gate arg_process_t *item_special; /* callback for special handling */ 847c478bd9Sstevel@tonic-gate } umem_env_item_t; 857c478bd9Sstevel@tonic-gate 867c478bd9Sstevel@tonic-gate #ifndef UMEM_STANDALONE 877c478bd9Sstevel@tonic-gate static arg_process_t umem_backend_process; 887c478bd9Sstevel@tonic-gate #endif 897c478bd9Sstevel@tonic-gate 907c478bd9Sstevel@tonic-gate static arg_process_t umem_log_process; 917c478bd9Sstevel@tonic-gate 927c478bd9Sstevel@tonic-gate const char *____umem_environ_msg_options = "-- UMEM_OPTIONS --"; 937c478bd9Sstevel@tonic-gate 947c478bd9Sstevel@tonic-gate static umem_env_item_t umem_options_items[] = { 957c478bd9Sstevel@tonic-gate #ifndef UMEM_STANDALONE 967c478bd9Sstevel@tonic-gate { "backend", "Evolving", ITEM_SPECIAL, 977c478bd9Sstevel@tonic-gate "=sbrk for sbrk(2), =mmap for mmap(2)", 987c478bd9Sstevel@tonic-gate NULL, 0, NULL, NULL, 997c478bd9Sstevel@tonic-gate &umem_backend_process 1007c478bd9Sstevel@tonic-gate }, 1017c478bd9Sstevel@tonic-gate #endif 1027c478bd9Sstevel@tonic-gate 1037c478bd9Sstevel@tonic-gate { "concurrency", "Private", ITEM_UINT, 1047c478bd9Sstevel@tonic-gate "Max concurrency", 1057c478bd9Sstevel@tonic-gate NULL, 0, &umem_max_ncpus 1067c478bd9Sstevel@tonic-gate }, 1077c478bd9Sstevel@tonic-gate { "max_contention", "Private", ITEM_UINT, 1087c478bd9Sstevel@tonic-gate "Maximum contention in a reap interval before the depot is " 1097c478bd9Sstevel@tonic-gate "resized.", 1107c478bd9Sstevel@tonic-gate NULL, 0, &umem_depot_contention 1117c478bd9Sstevel@tonic-gate }, 1127c478bd9Sstevel@tonic-gate { "nomagazines", "Private", ITEM_FLAG, 1137c478bd9Sstevel@tonic-gate "no caches will be multithreaded, and no caching will occur.", 1147c478bd9Sstevel@tonic-gate &umem_flags, UMF_NOMAGAZINE 1157c478bd9Sstevel@tonic-gate }, 1167c478bd9Sstevel@tonic-gate { "reap_interval", "Private", ITEM_UINT, 1177c478bd9Sstevel@tonic-gate "Minimum time between reaps and updates, in seconds.", 1187c478bd9Sstevel@tonic-gate NULL, 0, &umem_reap_interval 1197c478bd9Sstevel@tonic-gate }, 1207c478bd9Sstevel@tonic-gate 1217c478bd9Sstevel@tonic-gate #ifndef UMEM_STANDALONE 1227c478bd9Sstevel@tonic-gate { "sbrk_pagesize", "Private", ITEM_SIZE, 1237c478bd9Sstevel@tonic-gate "The preferred page size for the sbrk(2) heap.", 1247c478bd9Sstevel@tonic-gate NULL, 0, NULL, &vmem_sbrk_pagesize 1257c478bd9Sstevel@tonic-gate }, 1267c478bd9Sstevel@tonic-gate #endif 1277c478bd9Sstevel@tonic-gate 1287c478bd9Sstevel@tonic-gate { NULL, "-- end of UMEM_OPTIONS --", ITEM_INVALID } 1297c478bd9Sstevel@tonic-gate }; 1307c478bd9Sstevel@tonic-gate 1317c478bd9Sstevel@tonic-gate const char *____umem_environ_msg_debug = "-- UMEM_DEBUG --"; 1327c478bd9Sstevel@tonic-gate 1337c478bd9Sstevel@tonic-gate static umem_env_item_t umem_debug_items[] = { 1347c478bd9Sstevel@tonic-gate { "default", "Unstable", ITEM_FLAG, 1357c478bd9Sstevel@tonic-gate "audit,contents,guards", 1367c478bd9Sstevel@tonic-gate &umem_flags, 1377c478bd9Sstevel@tonic-gate UMF_AUDIT | UMF_CONTENTS | UMF_DEADBEEF | UMF_REDZONE 1387c478bd9Sstevel@tonic-gate }, 1397c478bd9Sstevel@tonic-gate { "audit", "Unstable", ITEM_OPTUINT, 1407c478bd9Sstevel@tonic-gate "Enable auditing. optionally =frames to set the number of " 1417c478bd9Sstevel@tonic-gate "stored stack frames", 1427c478bd9Sstevel@tonic-gate &umem_flags, UMF_AUDIT, &umem_stack_depth 1437c478bd9Sstevel@tonic-gate }, 1447c478bd9Sstevel@tonic-gate { "contents", "Unstable", ITEM_OPTSIZE, 1457c478bd9Sstevel@tonic-gate "Enable contents storing. UMEM_LOGGING=contents also " 1467c478bd9Sstevel@tonic-gate "required. optionally =bytes to set the number of stored " 1477c478bd9Sstevel@tonic-gate "bytes", 1487c478bd9Sstevel@tonic-gate &umem_flags, UMF_CONTENTS, NULL, &umem_content_maxsave 1497c478bd9Sstevel@tonic-gate }, 1507c478bd9Sstevel@tonic-gate { "guards", "Unstable", ITEM_FLAG, 1517c478bd9Sstevel@tonic-gate "Enables guards and special patterns", 1527c478bd9Sstevel@tonic-gate &umem_flags, UMF_DEADBEEF | UMF_REDZONE 1537c478bd9Sstevel@tonic-gate }, 1547c478bd9Sstevel@tonic-gate { "verbose", "Unstable", ITEM_FLAG, 1557c478bd9Sstevel@tonic-gate "Enables writing error messages to stderr", 1567c478bd9Sstevel@tonic-gate &umem_output, 1 1577c478bd9Sstevel@tonic-gate }, 1587c478bd9Sstevel@tonic-gate 1597c478bd9Sstevel@tonic-gate { "nosignal", "Private", ITEM_FLAG, 1607c478bd9Sstevel@tonic-gate "Abort if called from a signal handler. Turns on 'audit'. " 1617c478bd9Sstevel@tonic-gate "Note that this is not always a bug.", 1627c478bd9Sstevel@tonic-gate &umem_flags, UMF_AUDIT | UMF_CHECKSIGNAL 1637c478bd9Sstevel@tonic-gate }, 1647c478bd9Sstevel@tonic-gate { "firewall", "Private", ITEM_SIZE, 1657c478bd9Sstevel@tonic-gate "=minbytes. Every object >= minbytes in size will have its " 1667c478bd9Sstevel@tonic-gate "end against an unmapped page", 1677c478bd9Sstevel@tonic-gate &umem_flags, UMF_FIREWALL, NULL, &umem_minfirewall 1687c478bd9Sstevel@tonic-gate }, 1697c478bd9Sstevel@tonic-gate { "lite", "Private", ITEM_FLAG, 1707c478bd9Sstevel@tonic-gate "debugging-lite", 1717c478bd9Sstevel@tonic-gate &umem_flags, UMF_LITE 1727c478bd9Sstevel@tonic-gate }, 1737c478bd9Sstevel@tonic-gate { "maxverify", "Private", ITEM_SIZE, 1747c478bd9Sstevel@tonic-gate "=maxbytes, Maximum bytes to check when 'guards' is active. " 1757c478bd9Sstevel@tonic-gate "Normally all bytes are checked.", 1767c478bd9Sstevel@tonic-gate NULL, 0, NULL, &umem_maxverify 1777c478bd9Sstevel@tonic-gate }, 1787c478bd9Sstevel@tonic-gate { "noabort", "Private", ITEM_CLEARFLAG, 1797c478bd9Sstevel@tonic-gate "umem will not abort when a recoverable error occurs " 1807c478bd9Sstevel@tonic-gate "(i.e. double frees, certain kinds of corruption)", 1817c478bd9Sstevel@tonic-gate &umem_abort, 1 1827c478bd9Sstevel@tonic-gate }, 1837c478bd9Sstevel@tonic-gate { "mtbf", "Private", ITEM_UINT, 1847c478bd9Sstevel@tonic-gate "=mtbf, the mean time between injected failures. Works best " 1857c478bd9Sstevel@tonic-gate "if prime.\n", 1867c478bd9Sstevel@tonic-gate NULL, 0, &umem_mtbf 1877c478bd9Sstevel@tonic-gate }, 1887c478bd9Sstevel@tonic-gate { "random", "Private", ITEM_FLAG, 1897c478bd9Sstevel@tonic-gate "randomize flags on a per-cache basis", 1907c478bd9Sstevel@tonic-gate &umem_flags, UMF_RANDOMIZE 1917c478bd9Sstevel@tonic-gate }, 1927c478bd9Sstevel@tonic-gate { "allverbose", "Private", ITEM_FLAG, 1937c478bd9Sstevel@tonic-gate "Enables writing all logged messages to stderr", 1947c478bd9Sstevel@tonic-gate &umem_output, 2 1957c478bd9Sstevel@tonic-gate }, 1967c478bd9Sstevel@tonic-gate 1977c478bd9Sstevel@tonic-gate { NULL, "-- end of UMEM_DEBUG --", ITEM_INVALID } 1987c478bd9Sstevel@tonic-gate }; 1997c478bd9Sstevel@tonic-gate 2007c478bd9Sstevel@tonic-gate const char *____umem_environ_msg_logging = "-- UMEM_LOGGING --"; 2017c478bd9Sstevel@tonic-gate 2027c478bd9Sstevel@tonic-gate static umem_env_item_t umem_logging_items[] = { 2037c478bd9Sstevel@tonic-gate { "transaction", "Unstable", ITEM_SPECIAL, 2047c478bd9Sstevel@tonic-gate "If 'audit' is set in UMEM_DEBUG, the audit structures " 2057c478bd9Sstevel@tonic-gate "from previous transactions are entered into this log.", 2067c478bd9Sstevel@tonic-gate NULL, 0, NULL, 2077c478bd9Sstevel@tonic-gate &umem_transaction_log_size, &umem_log_process 2087c478bd9Sstevel@tonic-gate }, 2097c478bd9Sstevel@tonic-gate { "contents", "Unstable", ITEM_SPECIAL, 2107c478bd9Sstevel@tonic-gate "If 'audit' is set in UMEM_DEBUG, the contents of objects " 2117c478bd9Sstevel@tonic-gate "are recorded in this log as they are freed. If the " 2127c478bd9Sstevel@tonic-gate "'contents' option is not set in UMEM_DEBUG, the first " 2137c478bd9Sstevel@tonic-gate "256 bytes of each freed buffer will be saved.", 2147c478bd9Sstevel@tonic-gate &umem_flags, UMF_CONTENTS, NULL, 2157c478bd9Sstevel@tonic-gate &umem_content_log_size, &umem_log_process 2167c478bd9Sstevel@tonic-gate }, 2177c478bd9Sstevel@tonic-gate { "fail", "Unstable", ITEM_SPECIAL, 2187c478bd9Sstevel@tonic-gate "Records are entered into this log for every failed " 2197c478bd9Sstevel@tonic-gate "allocation.", 2207c478bd9Sstevel@tonic-gate NULL, 0, NULL, 2217c478bd9Sstevel@tonic-gate &umem_failure_log_size, &umem_log_process 2227c478bd9Sstevel@tonic-gate }, 2237c478bd9Sstevel@tonic-gate 2247c478bd9Sstevel@tonic-gate { "slab", "Private", ITEM_SPECIAL, 2257c478bd9Sstevel@tonic-gate "Every slab created will be entered into this log.", 2267c478bd9Sstevel@tonic-gate NULL, 0, NULL, 2277c478bd9Sstevel@tonic-gate &umem_slab_log_size, &umem_log_process 2287c478bd9Sstevel@tonic-gate }, 2297c478bd9Sstevel@tonic-gate 2307c478bd9Sstevel@tonic-gate { NULL, "-- end of UMEM_LOGGING --", ITEM_INVALID } 2317c478bd9Sstevel@tonic-gate }; 2327c478bd9Sstevel@tonic-gate 2337c478bd9Sstevel@tonic-gate typedef struct umem_envvar { 2347c478bd9Sstevel@tonic-gate const char *env_name; 2357c478bd9Sstevel@tonic-gate const char *env_func; 2367c478bd9Sstevel@tonic-gate umem_env_item_t *env_item_list; 2377c478bd9Sstevel@tonic-gate const char *env_getenv_result; 2387c478bd9Sstevel@tonic-gate const char *env_func_result; 2397c478bd9Sstevel@tonic-gate } umem_envvar_t; 2407c478bd9Sstevel@tonic-gate 2417c478bd9Sstevel@tonic-gate static umem_envvar_t umem_envvars[] = { 2427c478bd9Sstevel@tonic-gate { "UMEM_DEBUG", "_umem_debug_init", umem_debug_items }, 2437c478bd9Sstevel@tonic-gate { "UMEM_OPTIONS", "_umem_options_init", umem_options_items }, 2447c478bd9Sstevel@tonic-gate { "UMEM_LOGGING", "_umem_logging_init", umem_logging_items }, 2457c478bd9Sstevel@tonic-gate { NULL, NULL, NULL } 2467c478bd9Sstevel@tonic-gate }; 2477c478bd9Sstevel@tonic-gate 2487c478bd9Sstevel@tonic-gate static umem_envvar_t *env_current; 2497c478bd9Sstevel@tonic-gate #define CURRENT (env_current->env_name) 2507c478bd9Sstevel@tonic-gate 2517c478bd9Sstevel@tonic-gate static int 2527c478bd9Sstevel@tonic-gate empty(const char *str) 2537c478bd9Sstevel@tonic-gate { 2547c478bd9Sstevel@tonic-gate char c; 2557c478bd9Sstevel@tonic-gate 2567c478bd9Sstevel@tonic-gate while ((c = *str) != '\0' && isspace(c)) 2577c478bd9Sstevel@tonic-gate str++; 2587c478bd9Sstevel@tonic-gate 2597c478bd9Sstevel@tonic-gate return (*str == '\0'); 2607c478bd9Sstevel@tonic-gate } 2617c478bd9Sstevel@tonic-gate 2627c478bd9Sstevel@tonic-gate static int 2637c478bd9Sstevel@tonic-gate item_uint_process(const umem_env_item_t *item, const char *item_arg) 2647c478bd9Sstevel@tonic-gate { 2657c478bd9Sstevel@tonic-gate ulong_t result; 2667c478bd9Sstevel@tonic-gate char *endptr = ""; 2677c478bd9Sstevel@tonic-gate int olderrno; 2687c478bd9Sstevel@tonic-gate 2697c478bd9Sstevel@tonic-gate olderrno = errno; 2707c478bd9Sstevel@tonic-gate errno = 0; 2717c478bd9Sstevel@tonic-gate 2727c478bd9Sstevel@tonic-gate if (empty(item_arg)) { 2737c478bd9Sstevel@tonic-gate goto badnumber; 2747c478bd9Sstevel@tonic-gate } 2757c478bd9Sstevel@tonic-gate 2767c478bd9Sstevel@tonic-gate result = strtoul(item_arg, &endptr, 10); 2777c478bd9Sstevel@tonic-gate 2787c478bd9Sstevel@tonic-gate if (result == ULONG_MAX && errno == ERANGE) { 2797c478bd9Sstevel@tonic-gate errno = olderrno; 2807c478bd9Sstevel@tonic-gate goto overflow; 2817c478bd9Sstevel@tonic-gate } 2827c478bd9Sstevel@tonic-gate errno = olderrno; 2837c478bd9Sstevel@tonic-gate 2847c478bd9Sstevel@tonic-gate if (*endptr != '\0') 2857c478bd9Sstevel@tonic-gate goto badnumber; 2867c478bd9Sstevel@tonic-gate if ((uint_t)result != result) 2877c478bd9Sstevel@tonic-gate goto overflow; 2887c478bd9Sstevel@tonic-gate 2897c478bd9Sstevel@tonic-gate (*item->item_uint_target) = (uint_t)result; 2907c478bd9Sstevel@tonic-gate return (ARG_SUCCESS); 2917c478bd9Sstevel@tonic-gate 2927c478bd9Sstevel@tonic-gate badnumber: 2937c478bd9Sstevel@tonic-gate log_message("%s: %s: not a number\n", CURRENT, item->item_name); 2947c478bd9Sstevel@tonic-gate return (ARG_BAD); 2957c478bd9Sstevel@tonic-gate 2967c478bd9Sstevel@tonic-gate overflow: 2977c478bd9Sstevel@tonic-gate log_message("%s: %s: overflowed\n", CURRENT, item->item_name); 2987c478bd9Sstevel@tonic-gate return (ARG_BAD); 2997c478bd9Sstevel@tonic-gate } 3007c478bd9Sstevel@tonic-gate 3017c478bd9Sstevel@tonic-gate static int 3027c478bd9Sstevel@tonic-gate item_size_process(const umem_env_item_t *item, const char *item_arg) 3037c478bd9Sstevel@tonic-gate { 3047c478bd9Sstevel@tonic-gate ulong_t result; 3057c478bd9Sstevel@tonic-gate ulong_t result_arg; 3067c478bd9Sstevel@tonic-gate char *endptr = ""; 3077c478bd9Sstevel@tonic-gate int olderrno; 3087c478bd9Sstevel@tonic-gate 3097c478bd9Sstevel@tonic-gate if (empty(item_arg)) 3107c478bd9Sstevel@tonic-gate goto badnumber; 3117c478bd9Sstevel@tonic-gate 3127c478bd9Sstevel@tonic-gate olderrno = errno; 3137c478bd9Sstevel@tonic-gate errno = 0; 3147c478bd9Sstevel@tonic-gate 3157c478bd9Sstevel@tonic-gate result_arg = strtoul(item_arg, &endptr, 10); 3167c478bd9Sstevel@tonic-gate 3177c478bd9Sstevel@tonic-gate if (result_arg == ULONG_MAX && errno == ERANGE) { 3187c478bd9Sstevel@tonic-gate errno = olderrno; 3197c478bd9Sstevel@tonic-gate goto overflow; 3207c478bd9Sstevel@tonic-gate } 3217c478bd9Sstevel@tonic-gate errno = olderrno; 3227c478bd9Sstevel@tonic-gate 3237c478bd9Sstevel@tonic-gate result = result_arg; 3247c478bd9Sstevel@tonic-gate 3257c478bd9Sstevel@tonic-gate switch (*endptr) { 3267c478bd9Sstevel@tonic-gate case 't': 3277c478bd9Sstevel@tonic-gate case 'T': 3287c478bd9Sstevel@tonic-gate result *= 1024; 3297c478bd9Sstevel@tonic-gate if (result < result_arg) 3307c478bd9Sstevel@tonic-gate goto overflow; 3317c478bd9Sstevel@tonic-gate /*FALLTHRU*/ 3327c478bd9Sstevel@tonic-gate case 'g': 3337c478bd9Sstevel@tonic-gate case 'G': 3347c478bd9Sstevel@tonic-gate result *= 1024; 3357c478bd9Sstevel@tonic-gate if (result < result_arg) 3367c478bd9Sstevel@tonic-gate goto overflow; 3377c478bd9Sstevel@tonic-gate /*FALLTHRU*/ 3387c478bd9Sstevel@tonic-gate case 'm': 3397c478bd9Sstevel@tonic-gate case 'M': 3407c478bd9Sstevel@tonic-gate result *= 1024; 3417c478bd9Sstevel@tonic-gate if (result < result_arg) 3427c478bd9Sstevel@tonic-gate goto overflow; 3437c478bd9Sstevel@tonic-gate /*FALLTHRU*/ 3447c478bd9Sstevel@tonic-gate case 'k': 3457c478bd9Sstevel@tonic-gate case 'K': 3467c478bd9Sstevel@tonic-gate result *= 1024; 3477c478bd9Sstevel@tonic-gate if (result < result_arg) 3487c478bd9Sstevel@tonic-gate goto overflow; 3497c478bd9Sstevel@tonic-gate endptr++; /* skip over the size character */ 3507c478bd9Sstevel@tonic-gate break; 3517c478bd9Sstevel@tonic-gate default: 3527c478bd9Sstevel@tonic-gate break; /* handled later */ 3537c478bd9Sstevel@tonic-gate } 3547c478bd9Sstevel@tonic-gate 3557c478bd9Sstevel@tonic-gate if (*endptr != '\0') 3567c478bd9Sstevel@tonic-gate goto badnumber; 3577c478bd9Sstevel@tonic-gate 3587c478bd9Sstevel@tonic-gate (*item->item_size_target) = result; 3597c478bd9Sstevel@tonic-gate return (ARG_SUCCESS); 3607c478bd9Sstevel@tonic-gate 3617c478bd9Sstevel@tonic-gate badnumber: 3627c478bd9Sstevel@tonic-gate log_message("%s: %s: not a number\n", CURRENT, item->item_name); 3637c478bd9Sstevel@tonic-gate return (ARG_BAD); 3647c478bd9Sstevel@tonic-gate 3657c478bd9Sstevel@tonic-gate overflow: 3667c478bd9Sstevel@tonic-gate log_message("%s: %s: overflowed\n", CURRENT, item->item_name); 3677c478bd9Sstevel@tonic-gate return (ARG_BAD); 3687c478bd9Sstevel@tonic-gate } 3697c478bd9Sstevel@tonic-gate 3707c478bd9Sstevel@tonic-gate static int 3717c478bd9Sstevel@tonic-gate umem_log_process(const umem_env_item_t *item, const char *item_arg) 3727c478bd9Sstevel@tonic-gate { 3737c478bd9Sstevel@tonic-gate if (item_arg != NULL) { 3747c478bd9Sstevel@tonic-gate int ret; 3757c478bd9Sstevel@tonic-gate ret = item_size_process(item, item_arg); 3767c478bd9Sstevel@tonic-gate if (ret != ARG_SUCCESS) 3777c478bd9Sstevel@tonic-gate return (ret); 3787c478bd9Sstevel@tonic-gate 3797c478bd9Sstevel@tonic-gate if (*item->item_size_target == 0) 3807c478bd9Sstevel@tonic-gate return (ARG_SUCCESS); 3817c478bd9Sstevel@tonic-gate } else 3827c478bd9Sstevel@tonic-gate *item->item_size_target = 64*1024; 3837c478bd9Sstevel@tonic-gate 3847c478bd9Sstevel@tonic-gate umem_logging = 1; 3857c478bd9Sstevel@tonic-gate return (ARG_SUCCESS); 3867c478bd9Sstevel@tonic-gate } 3877c478bd9Sstevel@tonic-gate 3887c478bd9Sstevel@tonic-gate #ifndef UMEM_STANDALONE 3897c478bd9Sstevel@tonic-gate static int 3907c478bd9Sstevel@tonic-gate umem_backend_process(const umem_env_item_t *item, const char *item_arg) 3917c478bd9Sstevel@tonic-gate { 3927c478bd9Sstevel@tonic-gate const char *name = item->item_name; 3937c478bd9Sstevel@tonic-gate 3947c478bd9Sstevel@tonic-gate if (item_arg == NULL) 3957c478bd9Sstevel@tonic-gate goto fail; 3967c478bd9Sstevel@tonic-gate 3977c478bd9Sstevel@tonic-gate if (strcmp(item_arg, "sbrk") == 0) 3987c478bd9Sstevel@tonic-gate vmem_backend |= VMEM_BACKEND_SBRK; 3997c478bd9Sstevel@tonic-gate else if (strcmp(item_arg, "mmap") == 0) 4007c478bd9Sstevel@tonic-gate vmem_backend |= VMEM_BACKEND_MMAP; 4017c478bd9Sstevel@tonic-gate else 4027c478bd9Sstevel@tonic-gate goto fail; 4037c478bd9Sstevel@tonic-gate 4047c478bd9Sstevel@tonic-gate return (ARG_SUCCESS); 4057c478bd9Sstevel@tonic-gate 4067c478bd9Sstevel@tonic-gate fail: 4077c478bd9Sstevel@tonic-gate log_message("%s: %s: must be %s=sbrk or %s=mmap\n", 4087c478bd9Sstevel@tonic-gate CURRENT, name, name, name); 4097c478bd9Sstevel@tonic-gate return (ARG_BAD); 4107c478bd9Sstevel@tonic-gate } 4117c478bd9Sstevel@tonic-gate #endif 4127c478bd9Sstevel@tonic-gate 4137c478bd9Sstevel@tonic-gate static int 4147c478bd9Sstevel@tonic-gate process_item(const umem_env_item_t *item, const char *item_arg) 4157c478bd9Sstevel@tonic-gate { 4167c478bd9Sstevel@tonic-gate int arg_required = 0; 4177c478bd9Sstevel@tonic-gate arg_process_t *processor; 4187c478bd9Sstevel@tonic-gate 4197c478bd9Sstevel@tonic-gate switch (item->item_type) { 4207c478bd9Sstevel@tonic-gate case ITEM_FLAG: 4217c478bd9Sstevel@tonic-gate case ITEM_CLEARFLAG: 4227c478bd9Sstevel@tonic-gate case ITEM_OPTUINT: 4237c478bd9Sstevel@tonic-gate case ITEM_OPTSIZE: 4247c478bd9Sstevel@tonic-gate case ITEM_SPECIAL: 4257c478bd9Sstevel@tonic-gate arg_required = 0; 4267c478bd9Sstevel@tonic-gate break; 4277c478bd9Sstevel@tonic-gate 4287c478bd9Sstevel@tonic-gate case ITEM_UINT: 4297c478bd9Sstevel@tonic-gate case ITEM_SIZE: 4307c478bd9Sstevel@tonic-gate arg_required = 1; 4317c478bd9Sstevel@tonic-gate break; 4327c478bd9Sstevel@tonic-gate } 4337c478bd9Sstevel@tonic-gate 4347c478bd9Sstevel@tonic-gate switch (item->item_type) { 4357c478bd9Sstevel@tonic-gate case ITEM_FLAG: 4367c478bd9Sstevel@tonic-gate case ITEM_CLEARFLAG: 4377c478bd9Sstevel@tonic-gate if (item_arg != NULL) { 4387c478bd9Sstevel@tonic-gate log_message("%s: %s: does not take a value. ignored\n", 4397c478bd9Sstevel@tonic-gate CURRENT, item->item_name); 4407c478bd9Sstevel@tonic-gate return (1); 4417c478bd9Sstevel@tonic-gate } 4427c478bd9Sstevel@tonic-gate processor = NULL; 4437c478bd9Sstevel@tonic-gate break; 4447c478bd9Sstevel@tonic-gate 4457c478bd9Sstevel@tonic-gate case ITEM_UINT: 4467c478bd9Sstevel@tonic-gate case ITEM_OPTUINT: 4477c478bd9Sstevel@tonic-gate processor = item_uint_process; 4487c478bd9Sstevel@tonic-gate break; 4497c478bd9Sstevel@tonic-gate 4507c478bd9Sstevel@tonic-gate case ITEM_SIZE: 4517c478bd9Sstevel@tonic-gate case ITEM_OPTSIZE: 4527c478bd9Sstevel@tonic-gate processor = item_size_process; 4537c478bd9Sstevel@tonic-gate break; 4547c478bd9Sstevel@tonic-gate 4557c478bd9Sstevel@tonic-gate case ITEM_SPECIAL: 4567c478bd9Sstevel@tonic-gate processor = item->item_special; 4577c478bd9Sstevel@tonic-gate break; 4587c478bd9Sstevel@tonic-gate 4597c478bd9Sstevel@tonic-gate default: 4607c478bd9Sstevel@tonic-gate log_message("%s: %s: Invalid type. Ignored\n", 4617c478bd9Sstevel@tonic-gate CURRENT, item->item_name); 4627c478bd9Sstevel@tonic-gate return (1); 4637c478bd9Sstevel@tonic-gate } 4647c478bd9Sstevel@tonic-gate 4657c478bd9Sstevel@tonic-gate if (arg_required && item_arg == NULL) { 4667c478bd9Sstevel@tonic-gate log_message("%s: %s: Required value missing\n", 4677c478bd9Sstevel@tonic-gate CURRENT, item->item_name); 4687c478bd9Sstevel@tonic-gate goto invalid; 4697c478bd9Sstevel@tonic-gate } 4707c478bd9Sstevel@tonic-gate 4717c478bd9Sstevel@tonic-gate if (item_arg != NULL || item->item_type == ITEM_SPECIAL) { 4727c478bd9Sstevel@tonic-gate if (processor(item, item_arg) != ARG_SUCCESS) 4737c478bd9Sstevel@tonic-gate goto invalid; 4747c478bd9Sstevel@tonic-gate } 4757c478bd9Sstevel@tonic-gate 4767c478bd9Sstevel@tonic-gate if (item->item_flag_target) { 4777c478bd9Sstevel@tonic-gate if (item->item_type == ITEM_CLEARFLAG) 4787c478bd9Sstevel@tonic-gate (*item->item_flag_target) &= ~item->item_flag_value; 4797c478bd9Sstevel@tonic-gate else 4807c478bd9Sstevel@tonic-gate (*item->item_flag_target) |= item->item_flag_value; 4817c478bd9Sstevel@tonic-gate } 4827c478bd9Sstevel@tonic-gate return (0); 4837c478bd9Sstevel@tonic-gate 4847c478bd9Sstevel@tonic-gate invalid: 4857c478bd9Sstevel@tonic-gate return (1); 4867c478bd9Sstevel@tonic-gate } 4877c478bd9Sstevel@tonic-gate 4887c478bd9Sstevel@tonic-gate #define ENV_SHORT_BYTES 10 /* bytes to print on error */ 4897c478bd9Sstevel@tonic-gate void 4907c478bd9Sstevel@tonic-gate umem_process_value(umem_env_item_t *item_list, const char *beg, const char *end) 4917c478bd9Sstevel@tonic-gate { 4927c478bd9Sstevel@tonic-gate char buf[UMEM_ENV_ITEM_MAX]; 4937c478bd9Sstevel@tonic-gate char *argptr; 4947c478bd9Sstevel@tonic-gate 4957c478bd9Sstevel@tonic-gate size_t count; 4967c478bd9Sstevel@tonic-gate 4977c478bd9Sstevel@tonic-gate while (beg < end && isspace(*beg)) 4987c478bd9Sstevel@tonic-gate beg++; 4997c478bd9Sstevel@tonic-gate 5007c478bd9Sstevel@tonic-gate while (beg < end && isspace(*(end - 1))) 5017c478bd9Sstevel@tonic-gate end--; 5027c478bd9Sstevel@tonic-gate 5037c478bd9Sstevel@tonic-gate if (beg >= end) { 5047c478bd9Sstevel@tonic-gate log_message("%s: empty option\n", CURRENT); 5057c478bd9Sstevel@tonic-gate return; 5067c478bd9Sstevel@tonic-gate } 5077c478bd9Sstevel@tonic-gate 5087c478bd9Sstevel@tonic-gate count = end - beg; 5097c478bd9Sstevel@tonic-gate 5107c478bd9Sstevel@tonic-gate if (count + 1 > sizeof (buf)) { 5117c478bd9Sstevel@tonic-gate char outbuf[ENV_SHORT_BYTES + 1]; 5127c478bd9Sstevel@tonic-gate /* 5137c478bd9Sstevel@tonic-gate * Have to do this, since sprintf("%10s",...) calls malloc() 5147c478bd9Sstevel@tonic-gate */ 5157c478bd9Sstevel@tonic-gate (void) strncpy(outbuf, beg, ENV_SHORT_BYTES); 5167c478bd9Sstevel@tonic-gate outbuf[ENV_SHORT_BYTES] = 0; 5177c478bd9Sstevel@tonic-gate 5187c478bd9Sstevel@tonic-gate log_message("%s: argument \"%s...\" too long\n", CURRENT, 5197c478bd9Sstevel@tonic-gate outbuf); 5207c478bd9Sstevel@tonic-gate return; 5217c478bd9Sstevel@tonic-gate } 5227c478bd9Sstevel@tonic-gate 5237c478bd9Sstevel@tonic-gate (void) strncpy(buf, beg, count); 5247c478bd9Sstevel@tonic-gate buf[count] = 0; 5257c478bd9Sstevel@tonic-gate 5267c478bd9Sstevel@tonic-gate argptr = strchr(buf, '='); 5277c478bd9Sstevel@tonic-gate 5287c478bd9Sstevel@tonic-gate if (argptr != NULL) 5297c478bd9Sstevel@tonic-gate *argptr++ = 0; 5307c478bd9Sstevel@tonic-gate 5317c478bd9Sstevel@tonic-gate for (; item_list->item_name != NULL; item_list++) { 5327c478bd9Sstevel@tonic-gate if (strcmp(buf, item_list->item_name) == 0) { 5337c478bd9Sstevel@tonic-gate (void) process_item(item_list, argptr); 5347c478bd9Sstevel@tonic-gate return; 5357c478bd9Sstevel@tonic-gate } 5367c478bd9Sstevel@tonic-gate } 5377c478bd9Sstevel@tonic-gate log_message("%s: '%s' not recognized\n", CURRENT, buf); 5387c478bd9Sstevel@tonic-gate } 5397c478bd9Sstevel@tonic-gate 5407c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 5417c478bd9Sstevel@tonic-gate void 5427c478bd9Sstevel@tonic-gate umem_setup_envvars(int invalid) 5437c478bd9Sstevel@tonic-gate { 5447c478bd9Sstevel@tonic-gate umem_envvar_t *cur_env; 5457c478bd9Sstevel@tonic-gate static volatile enum { 5467c478bd9Sstevel@tonic-gate STATE_START, 5477c478bd9Sstevel@tonic-gate STATE_GETENV, 548*2d9e8f45Sjwadams STATE_DLOPEN, 5497c478bd9Sstevel@tonic-gate STATE_DLSYM, 5507c478bd9Sstevel@tonic-gate STATE_FUNC, 5517c478bd9Sstevel@tonic-gate STATE_DONE 5527c478bd9Sstevel@tonic-gate } state = STATE_START; 5537c478bd9Sstevel@tonic-gate #ifndef UMEM_STANDALONE 5547c478bd9Sstevel@tonic-gate void *h; 5557c478bd9Sstevel@tonic-gate #endif 5567c478bd9Sstevel@tonic-gate 5577c478bd9Sstevel@tonic-gate if (invalid) { 5587c478bd9Sstevel@tonic-gate const char *where; 5597c478bd9Sstevel@tonic-gate /* 5607c478bd9Sstevel@tonic-gate * One of the calls below invoked malloc() recursively. We 5617c478bd9Sstevel@tonic-gate * remove any partial results and return. 5627c478bd9Sstevel@tonic-gate */ 5637c478bd9Sstevel@tonic-gate 5647c478bd9Sstevel@tonic-gate switch (state) { 5657c478bd9Sstevel@tonic-gate case STATE_START: 5667c478bd9Sstevel@tonic-gate where = "before getenv(3C) calls -- " 5677c478bd9Sstevel@tonic-gate "getenv(3C) results ignored."; 5687c478bd9Sstevel@tonic-gate break; 5697c478bd9Sstevel@tonic-gate case STATE_GETENV: 5707c478bd9Sstevel@tonic-gate where = "during getenv(3C) calls -- " 5717c478bd9Sstevel@tonic-gate "getenv(3C) results ignored."; 5727c478bd9Sstevel@tonic-gate break; 573*2d9e8f45Sjwadams case STATE_DLOPEN: 574*2d9e8f45Sjwadams where = "during dlopen(3C) call -- " 575*2d9e8f45Sjwadams "_umem_*() results ignored."; 576*2d9e8f45Sjwadams break; 5777c478bd9Sstevel@tonic-gate case STATE_DLSYM: 5787c478bd9Sstevel@tonic-gate where = "during dlsym(3C) call -- " 5797c478bd9Sstevel@tonic-gate "_umem_*() results ignored."; 5807c478bd9Sstevel@tonic-gate break; 5817c478bd9Sstevel@tonic-gate case STATE_FUNC: 5827c478bd9Sstevel@tonic-gate where = "during _umem_*() call -- " 5837c478bd9Sstevel@tonic-gate "_umem_*() results ignored."; 5847c478bd9Sstevel@tonic-gate break; 5857c478bd9Sstevel@tonic-gate case STATE_DONE: 5867c478bd9Sstevel@tonic-gate where = "after dlsym() or _umem_*() calls."; 5877c478bd9Sstevel@tonic-gate break; 5887c478bd9Sstevel@tonic-gate default: 5897c478bd9Sstevel@tonic-gate where = "at unknown point -- " 5907c478bd9Sstevel@tonic-gate "_umem_*() results ignored."; 5917c478bd9Sstevel@tonic-gate break; 5927c478bd9Sstevel@tonic-gate } 5937c478bd9Sstevel@tonic-gate 5947c478bd9Sstevel@tonic-gate log_message("recursive allocation %s\n", where); 5957c478bd9Sstevel@tonic-gate 5967c478bd9Sstevel@tonic-gate for (cur_env = umem_envvars; cur_env->env_name != NULL; 5977c478bd9Sstevel@tonic-gate cur_env++) { 5987c478bd9Sstevel@tonic-gate if (state == STATE_GETENV) 5997c478bd9Sstevel@tonic-gate cur_env->env_getenv_result = NULL; 6007c478bd9Sstevel@tonic-gate if (state != STATE_DONE) 6017c478bd9Sstevel@tonic-gate cur_env->env_func_result = NULL; 6027c478bd9Sstevel@tonic-gate } 6037c478bd9Sstevel@tonic-gate 6047c478bd9Sstevel@tonic-gate state = STATE_DONE; 6057c478bd9Sstevel@tonic-gate return; 6067c478bd9Sstevel@tonic-gate } 6077c478bd9Sstevel@tonic-gate 6087c478bd9Sstevel@tonic-gate state = STATE_GETENV; 6097c478bd9Sstevel@tonic-gate 6107c478bd9Sstevel@tonic-gate for (cur_env = umem_envvars; cur_env->env_name != NULL; cur_env++) { 6117c478bd9Sstevel@tonic-gate cur_env->env_getenv_result = getenv(cur_env->env_name); 6127c478bd9Sstevel@tonic-gate if (state == STATE_DONE) 6137c478bd9Sstevel@tonic-gate return; /* recursed */ 6147c478bd9Sstevel@tonic-gate } 6157c478bd9Sstevel@tonic-gate 6167c478bd9Sstevel@tonic-gate #ifndef UMEM_STANDALONE 617*2d9e8f45Sjwadams state = STATE_DLOPEN; 618*2d9e8f45Sjwadams 6197c478bd9Sstevel@tonic-gate /* get a handle to the "a.out" object */ 6207c478bd9Sstevel@tonic-gate if ((h = dlopen(0, RTLD_FIRST | RTLD_LAZY)) != NULL) { 6217c478bd9Sstevel@tonic-gate for (cur_env = umem_envvars; cur_env->env_name != NULL; 6227c478bd9Sstevel@tonic-gate cur_env++) { 6237c478bd9Sstevel@tonic-gate const char *(*func)(void); 6247c478bd9Sstevel@tonic-gate const char *value; 6257c478bd9Sstevel@tonic-gate 6267c478bd9Sstevel@tonic-gate state = STATE_DLSYM; 6277c478bd9Sstevel@tonic-gate func = (const char *(*)(void))dlsym(h, 6287c478bd9Sstevel@tonic-gate cur_env->env_func); 6297c478bd9Sstevel@tonic-gate 6307c478bd9Sstevel@tonic-gate if (state == STATE_DONE) 6317c478bd9Sstevel@tonic-gate break; /* recursed */ 6327c478bd9Sstevel@tonic-gate 6337c478bd9Sstevel@tonic-gate state = STATE_FUNC; 6347c478bd9Sstevel@tonic-gate if (func != NULL) { 6357c478bd9Sstevel@tonic-gate value = func(); 6367c478bd9Sstevel@tonic-gate if (state == STATE_DONE) 6377c478bd9Sstevel@tonic-gate break; /* recursed */ 6387c478bd9Sstevel@tonic-gate cur_env->env_func_result = value; 6397c478bd9Sstevel@tonic-gate } 6407c478bd9Sstevel@tonic-gate } 6417c478bd9Sstevel@tonic-gate (void) dlclose(h); 6427c478bd9Sstevel@tonic-gate } else { 6437c478bd9Sstevel@tonic-gate (void) dlerror(); /* snarf dlerror() */ 6447c478bd9Sstevel@tonic-gate } 6457c478bd9Sstevel@tonic-gate #endif /* UMEM_STANDALONE */ 6467c478bd9Sstevel@tonic-gate 6477c478bd9Sstevel@tonic-gate state = STATE_DONE; 6487c478bd9Sstevel@tonic-gate } 6497c478bd9Sstevel@tonic-gate 6507c478bd9Sstevel@tonic-gate /* 6517c478bd9Sstevel@tonic-gate * Process the environment variables. 6527c478bd9Sstevel@tonic-gate */ 6537c478bd9Sstevel@tonic-gate void 6547c478bd9Sstevel@tonic-gate umem_process_envvars(void) 6557c478bd9Sstevel@tonic-gate { 6567c478bd9Sstevel@tonic-gate const char *value; 6577c478bd9Sstevel@tonic-gate const char *end, *next; 6587c478bd9Sstevel@tonic-gate umem_envvar_t *cur_env; 6597c478bd9Sstevel@tonic-gate 6607c478bd9Sstevel@tonic-gate for (cur_env = umem_envvars; cur_env->env_name != NULL; cur_env++) { 6617c478bd9Sstevel@tonic-gate env_current = cur_env; 6627c478bd9Sstevel@tonic-gate 6637c478bd9Sstevel@tonic-gate value = cur_env->env_getenv_result; 6647c478bd9Sstevel@tonic-gate if (value == NULL) 6657c478bd9Sstevel@tonic-gate value = cur_env->env_func_result; 6667c478bd9Sstevel@tonic-gate 6677c478bd9Sstevel@tonic-gate /* ignore if missing or empty */ 6687c478bd9Sstevel@tonic-gate if (value == NULL) 6697c478bd9Sstevel@tonic-gate continue; 6707c478bd9Sstevel@tonic-gate 6717c478bd9Sstevel@tonic-gate for (end = value; *end != '\0'; value = next) { 6727c478bd9Sstevel@tonic-gate end = strchr(value, ','); 6737c478bd9Sstevel@tonic-gate if (end != NULL) 6747c478bd9Sstevel@tonic-gate next = end + 1; /* skip the comma */ 6757c478bd9Sstevel@tonic-gate else 6767c478bd9Sstevel@tonic-gate next = end = value + strlen(value); 6777c478bd9Sstevel@tonic-gate 6787c478bd9Sstevel@tonic-gate umem_process_value(cur_env->env_item_list, value, end); 6797c478bd9Sstevel@tonic-gate } 6807c478bd9Sstevel@tonic-gate } 6817c478bd9Sstevel@tonic-gate } 682