1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* 23*7c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 25*7c478bd9Sstevel@tonic-gate */ 26*7c478bd9Sstevel@tonic-gate 27*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*7c478bd9Sstevel@tonic-gate 29*7c478bd9Sstevel@tonic-gate #include <string.h> 30*7c478bd9Sstevel@tonic-gate #include <limits.h> 31*7c478bd9Sstevel@tonic-gate #include <stdlib.h> 32*7c478bd9Sstevel@tonic-gate #include <sys/types.h> 33*7c478bd9Sstevel@tonic-gate 34*7c478bd9Sstevel@tonic-gate #include <inj.h> 35*7c478bd9Sstevel@tonic-gate #include <inj_err.h> 36*7c478bd9Sstevel@tonic-gate #include <inj_string.h> 37*7c478bd9Sstevel@tonic-gate 38*7c478bd9Sstevel@tonic-gate char * 39*7c478bd9Sstevel@tonic-gate inj_strdup(const char *s) 40*7c478bd9Sstevel@tonic-gate { 41*7c478bd9Sstevel@tonic-gate char *s1 = inj_alloc(strlen(s) + 1); 42*7c478bd9Sstevel@tonic-gate 43*7c478bd9Sstevel@tonic-gate (void) strcpy(s1, s); 44*7c478bd9Sstevel@tonic-gate return (s1); 45*7c478bd9Sstevel@tonic-gate } 46*7c478bd9Sstevel@tonic-gate 47*7c478bd9Sstevel@tonic-gate char * 48*7c478bd9Sstevel@tonic-gate inj_strndup(const char *s, size_t n) 49*7c478bd9Sstevel@tonic-gate { 50*7c478bd9Sstevel@tonic-gate char *s2 = inj_alloc(n + 1); 51*7c478bd9Sstevel@tonic-gate 52*7c478bd9Sstevel@tonic-gate (void) strncpy(s2, s, n + 1); 53*7c478bd9Sstevel@tonic-gate s2[n] = '\0'; 54*7c478bd9Sstevel@tonic-gate return (s2); 55*7c478bd9Sstevel@tonic-gate } 56*7c478bd9Sstevel@tonic-gate 57*7c478bd9Sstevel@tonic-gate void 58*7c478bd9Sstevel@tonic-gate inj_strfree(const char *s) 59*7c478bd9Sstevel@tonic-gate { 60*7c478bd9Sstevel@tonic-gate inj_free((void *)s, strlen(s) + 1); 61*7c478bd9Sstevel@tonic-gate } 62*7c478bd9Sstevel@tonic-gate 63*7c478bd9Sstevel@tonic-gate typedef struct type_desc { 64*7c478bd9Sstevel@tonic-gate int64_t td_min; 65*7c478bd9Sstevel@tonic-gate uint64_t td_max; 66*7c478bd9Sstevel@tonic-gate } type_desc_t; 67*7c478bd9Sstevel@tonic-gate 68*7c478bd9Sstevel@tonic-gate static const type_desc_t signed_types[] = { 69*7c478bd9Sstevel@tonic-gate { 0, 0 }, 70*7c478bd9Sstevel@tonic-gate { INT8_MIN, INT8_MAX }, 71*7c478bd9Sstevel@tonic-gate { INT16_MIN, INT16_MAX }, 72*7c478bd9Sstevel@tonic-gate { 0, 0 }, 73*7c478bd9Sstevel@tonic-gate { INT32_MIN, INT32_MAX }, 74*7c478bd9Sstevel@tonic-gate { 0, 0 }, 75*7c478bd9Sstevel@tonic-gate { 0, 0 }, 76*7c478bd9Sstevel@tonic-gate { 0, 0 }, 77*7c478bd9Sstevel@tonic-gate { INT64_MIN, INT64_MAX } 78*7c478bd9Sstevel@tonic-gate }; 79*7c478bd9Sstevel@tonic-gate 80*7c478bd9Sstevel@tonic-gate static const type_desc_t unsigned_types[] = { 81*7c478bd9Sstevel@tonic-gate { 0, 0 }, 82*7c478bd9Sstevel@tonic-gate { 0, UINT8_MAX }, 83*7c478bd9Sstevel@tonic-gate { 0, UINT16_MAX }, 84*7c478bd9Sstevel@tonic-gate { 0, 0 }, 85*7c478bd9Sstevel@tonic-gate { 0, UINT32_MAX }, 86*7c478bd9Sstevel@tonic-gate { 0, 0 }, 87*7c478bd9Sstevel@tonic-gate { 0, 0 }, 88*7c478bd9Sstevel@tonic-gate { 0, 0 }, 89*7c478bd9Sstevel@tonic-gate { 0, UINT64_MAX } 90*7c478bd9Sstevel@tonic-gate }; 91*7c478bd9Sstevel@tonic-gate 92*7c478bd9Sstevel@tonic-gate int 93*7c478bd9Sstevel@tonic-gate inj_strtoll(const char *str, int width, longlong_t *valp) 94*7c478bd9Sstevel@tonic-gate { 95*7c478bd9Sstevel@tonic-gate const type_desc_t *desc; 96*7c478bd9Sstevel@tonic-gate longlong_t val; 97*7c478bd9Sstevel@tonic-gate char *c; 98*7c478bd9Sstevel@tonic-gate 99*7c478bd9Sstevel@tonic-gate if (width != 0) { 100*7c478bd9Sstevel@tonic-gate assert(width / 8 < (sizeof (signed_types) / 101*7c478bd9Sstevel@tonic-gate sizeof (signed_types[0]))); 102*7c478bd9Sstevel@tonic-gate desc = &signed_types[width / 8]; 103*7c478bd9Sstevel@tonic-gate assert(desc->td_max != 0); 104*7c478bd9Sstevel@tonic-gate } 105*7c478bd9Sstevel@tonic-gate 106*7c478bd9Sstevel@tonic-gate errno = 0; 107*7c478bd9Sstevel@tonic-gate val = strtoll(str, &c, 0); 108*7c478bd9Sstevel@tonic-gate if (*c != '\0' || errno == EINVAL) 109*7c478bd9Sstevel@tonic-gate return (inj_set_errno(EINVAL)); 110*7c478bd9Sstevel@tonic-gate 111*7c478bd9Sstevel@tonic-gate if (errno == ERANGE || (width != 0 && (val < desc->td_min || 112*7c478bd9Sstevel@tonic-gate val > (longlong_t)desc->td_max))) 113*7c478bd9Sstevel@tonic-gate return (inj_set_errno(ERANGE)); 114*7c478bd9Sstevel@tonic-gate 115*7c478bd9Sstevel@tonic-gate if (valp != NULL) 116*7c478bd9Sstevel@tonic-gate *valp = val; 117*7c478bd9Sstevel@tonic-gate 118*7c478bd9Sstevel@tonic-gate return (0); 119*7c478bd9Sstevel@tonic-gate } 120*7c478bd9Sstevel@tonic-gate 121*7c478bd9Sstevel@tonic-gate int 122*7c478bd9Sstevel@tonic-gate inj_strtoull(const char *str, int width, u_longlong_t *valp) 123*7c478bd9Sstevel@tonic-gate { 124*7c478bd9Sstevel@tonic-gate const type_desc_t *desc; 125*7c478bd9Sstevel@tonic-gate u_longlong_t val; 126*7c478bd9Sstevel@tonic-gate char *c; 127*7c478bd9Sstevel@tonic-gate 128*7c478bd9Sstevel@tonic-gate if (width != 0) { 129*7c478bd9Sstevel@tonic-gate assert(width / 8 < (sizeof (unsigned_types) / 130*7c478bd9Sstevel@tonic-gate sizeof (unsigned_types[0]))); 131*7c478bd9Sstevel@tonic-gate desc = &unsigned_types[width / 8]; 132*7c478bd9Sstevel@tonic-gate assert(desc->td_max != 0); 133*7c478bd9Sstevel@tonic-gate } 134*7c478bd9Sstevel@tonic-gate 135*7c478bd9Sstevel@tonic-gate errno = 0; 136*7c478bd9Sstevel@tonic-gate val = strtoull(str, &c, 0); 137*7c478bd9Sstevel@tonic-gate if (*c != '\0' || errno == EINVAL) 138*7c478bd9Sstevel@tonic-gate return (inj_set_errno(EINVAL)); 139*7c478bd9Sstevel@tonic-gate 140*7c478bd9Sstevel@tonic-gate if (errno == ERANGE || (width != 0 && val > desc->td_max)) 141*7c478bd9Sstevel@tonic-gate return (inj_set_errno(ERANGE)); 142*7c478bd9Sstevel@tonic-gate 143*7c478bd9Sstevel@tonic-gate if (valp != NULL) 144*7c478bd9Sstevel@tonic-gate *valp = val; 145*7c478bd9Sstevel@tonic-gate 146*7c478bd9Sstevel@tonic-gate return (0); 147*7c478bd9Sstevel@tonic-gate } 148*7c478bd9Sstevel@tonic-gate 149*7c478bd9Sstevel@tonic-gate int 150*7c478bd9Sstevel@tonic-gate inj_strtime(hrtime_t *nsp, const char *units) 151*7c478bd9Sstevel@tonic-gate { 152*7c478bd9Sstevel@tonic-gate static const struct { 153*7c478bd9Sstevel@tonic-gate const char *name; 154*7c478bd9Sstevel@tonic-gate hrtime_t mul; 155*7c478bd9Sstevel@tonic-gate } suffix[] = { 156*7c478bd9Sstevel@tonic-gate { "ns", NANOSEC / NANOSEC }, 157*7c478bd9Sstevel@tonic-gate { "nsec", NANOSEC / NANOSEC }, 158*7c478bd9Sstevel@tonic-gate { "us", NANOSEC / MICROSEC }, 159*7c478bd9Sstevel@tonic-gate { "usec", NANOSEC / MICROSEC }, 160*7c478bd9Sstevel@tonic-gate { "ms", NANOSEC / MILLISEC }, 161*7c478bd9Sstevel@tonic-gate { "msec", NANOSEC / MILLISEC }, 162*7c478bd9Sstevel@tonic-gate { "s", NANOSEC / SEC }, 163*7c478bd9Sstevel@tonic-gate { "sec", NANOSEC / SEC }, 164*7c478bd9Sstevel@tonic-gate { "m", NANOSEC * (hrtime_t)60 }, 165*7c478bd9Sstevel@tonic-gate { "min", NANOSEC * (hrtime_t)60 }, 166*7c478bd9Sstevel@tonic-gate { "h", NANOSEC * (hrtime_t)(60 * 60) }, 167*7c478bd9Sstevel@tonic-gate { "hour", NANOSEC * (hrtime_t)(60 * 60) }, 168*7c478bd9Sstevel@tonic-gate { "d", NANOSEC * (hrtime_t)(24 * 60 * 60) }, 169*7c478bd9Sstevel@tonic-gate { "day", NANOSEC * (hrtime_t)(24 * 60 * 60) }, 170*7c478bd9Sstevel@tonic-gate { "hz", 0 }, 171*7c478bd9Sstevel@tonic-gate { NULL } 172*7c478bd9Sstevel@tonic-gate }; 173*7c478bd9Sstevel@tonic-gate 174*7c478bd9Sstevel@tonic-gate hrtime_t val = *nsp, mul = 1; 175*7c478bd9Sstevel@tonic-gate int i; 176*7c478bd9Sstevel@tonic-gate 177*7c478bd9Sstevel@tonic-gate for (i = 0; suffix[i].name != NULL; i++) { 178*7c478bd9Sstevel@tonic-gate if (strcasecmp(suffix[i].name, units) == 0) { 179*7c478bd9Sstevel@tonic-gate mul = suffix[i].mul; 180*7c478bd9Sstevel@tonic-gate break; 181*7c478bd9Sstevel@tonic-gate } 182*7c478bd9Sstevel@tonic-gate } 183*7c478bd9Sstevel@tonic-gate 184*7c478bd9Sstevel@tonic-gate if (suffix[i].name == NULL && *units != '\0') 185*7c478bd9Sstevel@tonic-gate return (inj_set_errno(EINVAL)); 186*7c478bd9Sstevel@tonic-gate 187*7c478bd9Sstevel@tonic-gate if (mul == 0) { 188*7c478bd9Sstevel@tonic-gate if (val != 0) 189*7c478bd9Sstevel@tonic-gate val = NANOSEC / val; /* compute val as value per sec */ 190*7c478bd9Sstevel@tonic-gate } else 191*7c478bd9Sstevel@tonic-gate val *= mul; 192*7c478bd9Sstevel@tonic-gate 193*7c478bd9Sstevel@tonic-gate *nsp = val; 194*7c478bd9Sstevel@tonic-gate return (0); 195*7c478bd9Sstevel@tonic-gate } 196*7c478bd9Sstevel@tonic-gate 197*7c478bd9Sstevel@tonic-gate static ulong_t 198*7c478bd9Sstevel@tonic-gate inj_hashfn_string(void *key) 199*7c478bd9Sstevel@tonic-gate { 200*7c478bd9Sstevel@tonic-gate size_t g, h = 0; 201*7c478bd9Sstevel@tonic-gate char *p; 202*7c478bd9Sstevel@tonic-gate 203*7c478bd9Sstevel@tonic-gate assert(key != NULL); 204*7c478bd9Sstevel@tonic-gate 205*7c478bd9Sstevel@tonic-gate for (p = key; *p != '\0'; p++) { 206*7c478bd9Sstevel@tonic-gate h = (h << 4) + *p; 207*7c478bd9Sstevel@tonic-gate 208*7c478bd9Sstevel@tonic-gate if ((g = (h & 0xf0000000)) != 0) { 209*7c478bd9Sstevel@tonic-gate h ^= (g >> 24); 210*7c478bd9Sstevel@tonic-gate h ^= g; 211*7c478bd9Sstevel@tonic-gate } 212*7c478bd9Sstevel@tonic-gate } 213*7c478bd9Sstevel@tonic-gate 214*7c478bd9Sstevel@tonic-gate return (h); 215*7c478bd9Sstevel@tonic-gate } 216*7c478bd9Sstevel@tonic-gate 217*7c478bd9Sstevel@tonic-gate static int 218*7c478bd9Sstevel@tonic-gate inj_hashcmp_string(void *k1, void *k2) 219*7c478bd9Sstevel@tonic-gate { 220*7c478bd9Sstevel@tonic-gate return (strcmp(k1, k2)); 221*7c478bd9Sstevel@tonic-gate } 222*7c478bd9Sstevel@tonic-gate 223*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 224*7c478bd9Sstevel@tonic-gate static void 225*7c478bd9Sstevel@tonic-gate inj_hashfree_string(inj_var_t *v, void *arg) 226*7c478bd9Sstevel@tonic-gate { 227*7c478bd9Sstevel@tonic-gate inj_strfree(inj_hash_get_key(v)); 228*7c478bd9Sstevel@tonic-gate } 229*7c478bd9Sstevel@tonic-gate 230*7c478bd9Sstevel@tonic-gate void 231*7c478bd9Sstevel@tonic-gate inj_strhash_create(inj_hash_t *h) 232*7c478bd9Sstevel@tonic-gate { 233*7c478bd9Sstevel@tonic-gate inj_hash_create(h, inj_hashfn_string, inj_hashcmp_string); 234*7c478bd9Sstevel@tonic-gate } 235*7c478bd9Sstevel@tonic-gate 236*7c478bd9Sstevel@tonic-gate int 237*7c478bd9Sstevel@tonic-gate inj_strhash_insert(inj_hash_t *h, const char *str, uintmax_t value) 238*7c478bd9Sstevel@tonic-gate { 239*7c478bd9Sstevel@tonic-gate return (inj_hash_insert(h, (void *)inj_strdup(str), value)); 240*7c478bd9Sstevel@tonic-gate } 241*7c478bd9Sstevel@tonic-gate 242*7c478bd9Sstevel@tonic-gate inj_var_t * 243*7c478bd9Sstevel@tonic-gate inj_strhash_lookup(inj_hash_t *h, const char *str) 244*7c478bd9Sstevel@tonic-gate { 245*7c478bd9Sstevel@tonic-gate return (inj_hash_lookup(h, (void *)str)); 246*7c478bd9Sstevel@tonic-gate } 247*7c478bd9Sstevel@tonic-gate 248*7c478bd9Sstevel@tonic-gate void 249*7c478bd9Sstevel@tonic-gate inj_strhash_destroy(inj_hash_t *h) 250*7c478bd9Sstevel@tonic-gate { 251*7c478bd9Sstevel@tonic-gate inj_hash_destroy(h, inj_hashfree_string, NULL); 252*7c478bd9Sstevel@tonic-gate } 253