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 <stdio.h> 30*7c478bd9Sstevel@tonic-gate #include <stdlib.h> 31*7c478bd9Sstevel@tonic-gate #include <string.h> 32*7c478bd9Sstevel@tonic-gate #include <strings.h> 33*7c478bd9Sstevel@tonic-gate #include <pool.h> 34*7c478bd9Sstevel@tonic-gate #include "pool_internal.h" 35*7c478bd9Sstevel@tonic-gate 36*7c478bd9Sstevel@tonic-gate /* 37*7c478bd9Sstevel@tonic-gate * libpool Value Manipulation Routines 38*7c478bd9Sstevel@tonic-gate * 39*7c478bd9Sstevel@tonic-gate * pool_value.c implements the value (pool_value_t) functionality for 40*7c478bd9Sstevel@tonic-gate * libpool. The datatypes supported are: uint64_t, int64_t, double, 41*7c478bd9Sstevel@tonic-gate * uchar_t (boolean), const char * (string). Values are used to 42*7c478bd9Sstevel@tonic-gate * represent data stored to and retrieved from the datastore in a 43*7c478bd9Sstevel@tonic-gate * simple discriminated union. 44*7c478bd9Sstevel@tonic-gate * 45*7c478bd9Sstevel@tonic-gate * Values are dynamically allocated using pool_value_alloc() and 46*7c478bd9Sstevel@tonic-gate * destroyed using pool_value_free(). 47*7c478bd9Sstevel@tonic-gate * 48*7c478bd9Sstevel@tonic-gate * Values may be allocated statically for internal use in 49*7c478bd9Sstevel@tonic-gate * libpool. Statically allocated pool_value_t variables must be 50*7c478bd9Sstevel@tonic-gate * initialised with the POOL_VALUE_INITIALIZER macro, otherwise the 51*7c478bd9Sstevel@tonic-gate * results are unpredictable. 52*7c478bd9Sstevel@tonic-gate * 53*7c478bd9Sstevel@tonic-gate * A pool_value_t variable can be used to store values in any of the 54*7c478bd9Sstevel@tonic-gate * supported datatypes. 55*7c478bd9Sstevel@tonic-gate * 56*7c478bd9Sstevel@tonic-gate * A pool_value_t's name and string value are limited in size to 57*7c478bd9Sstevel@tonic-gate * PV_NAME_MAX_LEN and PV_VALUE_MAX_LEN respectively. Attempting to 58*7c478bd9Sstevel@tonic-gate * store values which are greater than this in length will fail with a 59*7c478bd9Sstevel@tonic-gate * POE_BADPARAM error. 60*7c478bd9Sstevel@tonic-gate */ 61*7c478bd9Sstevel@tonic-gate 62*7c478bd9Sstevel@tonic-gate /* 63*7c478bd9Sstevel@tonic-gate * Get the uint64_t data held by the value. If the data type isn't 64*7c478bd9Sstevel@tonic-gate * uint64_t return PO_FAIL and set pool_error to be POE_BAD_PROP_TYPE. 65*7c478bd9Sstevel@tonic-gate */ 66*7c478bd9Sstevel@tonic-gate int 67*7c478bd9Sstevel@tonic-gate pool_value_get_uint64(const pool_value_t *pv, uint64_t *result) 68*7c478bd9Sstevel@tonic-gate { 69*7c478bd9Sstevel@tonic-gate if (pv->pv_class != POC_UINT) { 70*7c478bd9Sstevel@tonic-gate pool_seterror(POE_BAD_PROP_TYPE); 71*7c478bd9Sstevel@tonic-gate return (PO_FAIL); 72*7c478bd9Sstevel@tonic-gate } 73*7c478bd9Sstevel@tonic-gate *result = pv->pv_u.u; 74*7c478bd9Sstevel@tonic-gate return (PO_SUCCESS); 75*7c478bd9Sstevel@tonic-gate } 76*7c478bd9Sstevel@tonic-gate 77*7c478bd9Sstevel@tonic-gate /* 78*7c478bd9Sstevel@tonic-gate * Get the int64_t data held by the value. If the data type isn't 79*7c478bd9Sstevel@tonic-gate * int64_t return PO_FAIL and set pool_error to be POE_BAD_PROP_TYPE. 80*7c478bd9Sstevel@tonic-gate */ 81*7c478bd9Sstevel@tonic-gate int 82*7c478bd9Sstevel@tonic-gate pool_value_get_int64(const pool_value_t *pv, int64_t *result) 83*7c478bd9Sstevel@tonic-gate { 84*7c478bd9Sstevel@tonic-gate if (pv->pv_class != POC_INT) { 85*7c478bd9Sstevel@tonic-gate pool_seterror(POE_BAD_PROP_TYPE); 86*7c478bd9Sstevel@tonic-gate return (PO_FAIL); 87*7c478bd9Sstevel@tonic-gate } 88*7c478bd9Sstevel@tonic-gate *result = pv->pv_u.i; 89*7c478bd9Sstevel@tonic-gate return (PO_SUCCESS); 90*7c478bd9Sstevel@tonic-gate } 91*7c478bd9Sstevel@tonic-gate 92*7c478bd9Sstevel@tonic-gate /* 93*7c478bd9Sstevel@tonic-gate * Get the double data held by the value. If the data type isn't 94*7c478bd9Sstevel@tonic-gate * double return PO_FAIL and set pool_error to be POE_BAD_PROP_TYPE. 95*7c478bd9Sstevel@tonic-gate */ 96*7c478bd9Sstevel@tonic-gate 97*7c478bd9Sstevel@tonic-gate int 98*7c478bd9Sstevel@tonic-gate pool_value_get_double(const pool_value_t *pv, double *result) 99*7c478bd9Sstevel@tonic-gate { 100*7c478bd9Sstevel@tonic-gate if (pv->pv_class != POC_DOUBLE) { 101*7c478bd9Sstevel@tonic-gate pool_seterror(POE_BAD_PROP_TYPE); 102*7c478bd9Sstevel@tonic-gate return (PO_FAIL); 103*7c478bd9Sstevel@tonic-gate } 104*7c478bd9Sstevel@tonic-gate *result = pv->pv_u.d; 105*7c478bd9Sstevel@tonic-gate return (PO_SUCCESS); 106*7c478bd9Sstevel@tonic-gate } 107*7c478bd9Sstevel@tonic-gate 108*7c478bd9Sstevel@tonic-gate /* 109*7c478bd9Sstevel@tonic-gate * Get the boolean data held by the value. If the data type isn't 110*7c478bd9Sstevel@tonic-gate * boolean return PO_FAIL and set pool_error to be POE_BAD_PROP_TYPE. 111*7c478bd9Sstevel@tonic-gate */ 112*7c478bd9Sstevel@tonic-gate int 113*7c478bd9Sstevel@tonic-gate pool_value_get_bool(const pool_value_t *pv, uchar_t *result) 114*7c478bd9Sstevel@tonic-gate { 115*7c478bd9Sstevel@tonic-gate if (pv->pv_class != POC_BOOL) { 116*7c478bd9Sstevel@tonic-gate pool_seterror(POE_BAD_PROP_TYPE); 117*7c478bd9Sstevel@tonic-gate return (PO_FAIL); 118*7c478bd9Sstevel@tonic-gate } 119*7c478bd9Sstevel@tonic-gate *result = pv->pv_u.b; 120*7c478bd9Sstevel@tonic-gate return (PO_SUCCESS); 121*7c478bd9Sstevel@tonic-gate } 122*7c478bd9Sstevel@tonic-gate 123*7c478bd9Sstevel@tonic-gate /* 124*7c478bd9Sstevel@tonic-gate * Get the string data held by the value. If the data type isn't 125*7c478bd9Sstevel@tonic-gate * string return PO_FAIL and set pool_error to be POE_BAD_PROP_TYPE. 126*7c478bd9Sstevel@tonic-gate */ 127*7c478bd9Sstevel@tonic-gate int 128*7c478bd9Sstevel@tonic-gate pool_value_get_string(const pool_value_t *pv, const char **result) 129*7c478bd9Sstevel@tonic-gate { 130*7c478bd9Sstevel@tonic-gate if (pv->pv_class != POC_STRING) { 131*7c478bd9Sstevel@tonic-gate pool_seterror(POE_BAD_PROP_TYPE); 132*7c478bd9Sstevel@tonic-gate return (PO_FAIL); 133*7c478bd9Sstevel@tonic-gate } 134*7c478bd9Sstevel@tonic-gate *result = pv->pv_u.s; 135*7c478bd9Sstevel@tonic-gate return (PO_SUCCESS); 136*7c478bd9Sstevel@tonic-gate } 137*7c478bd9Sstevel@tonic-gate 138*7c478bd9Sstevel@tonic-gate /* 139*7c478bd9Sstevel@tonic-gate * Get the type of the data held by the value. If the value has never 140*7c478bd9Sstevel@tonic-gate * been used to store data, then the type is POC_INVAL. 141*7c478bd9Sstevel@tonic-gate */ 142*7c478bd9Sstevel@tonic-gate pool_value_class_t 143*7c478bd9Sstevel@tonic-gate pool_value_get_type(const pool_value_t *pv) 144*7c478bd9Sstevel@tonic-gate { 145*7c478bd9Sstevel@tonic-gate return (pv->pv_class); 146*7c478bd9Sstevel@tonic-gate } 147*7c478bd9Sstevel@tonic-gate 148*7c478bd9Sstevel@tonic-gate /* 149*7c478bd9Sstevel@tonic-gate * Set the value's data to the supplied uint64_t data. Update the type 150*7c478bd9Sstevel@tonic-gate * of the value data to POC_UINT. 151*7c478bd9Sstevel@tonic-gate */ 152*7c478bd9Sstevel@tonic-gate void 153*7c478bd9Sstevel@tonic-gate pool_value_set_uint64(pool_value_t *pv, uint64_t val) 154*7c478bd9Sstevel@tonic-gate { 155*7c478bd9Sstevel@tonic-gate if (pv->pv_class == POC_STRING) 156*7c478bd9Sstevel@tonic-gate atom_free(pv->pv_u.s); 157*7c478bd9Sstevel@tonic-gate pv->pv_class = POC_UINT; 158*7c478bd9Sstevel@tonic-gate pv->pv_u.u = val; 159*7c478bd9Sstevel@tonic-gate } 160*7c478bd9Sstevel@tonic-gate 161*7c478bd9Sstevel@tonic-gate /* 162*7c478bd9Sstevel@tonic-gate * Set the value's data to the supplied int64_t data. Update the type 163*7c478bd9Sstevel@tonic-gate * of the value data to POC_INT. 164*7c478bd9Sstevel@tonic-gate */ 165*7c478bd9Sstevel@tonic-gate void 166*7c478bd9Sstevel@tonic-gate pool_value_set_int64(pool_value_t *pv, int64_t val) 167*7c478bd9Sstevel@tonic-gate { 168*7c478bd9Sstevel@tonic-gate if (pv->pv_class == POC_STRING) 169*7c478bd9Sstevel@tonic-gate atom_free(pv->pv_u.s); 170*7c478bd9Sstevel@tonic-gate pv->pv_class = POC_INT; 171*7c478bd9Sstevel@tonic-gate pv->pv_u.i = val; 172*7c478bd9Sstevel@tonic-gate } 173*7c478bd9Sstevel@tonic-gate 174*7c478bd9Sstevel@tonic-gate /* 175*7c478bd9Sstevel@tonic-gate * Set the value's data to the supplied double data. Update the type 176*7c478bd9Sstevel@tonic-gate * of the value data to POC_DOUBLE. 177*7c478bd9Sstevel@tonic-gate */ 178*7c478bd9Sstevel@tonic-gate 179*7c478bd9Sstevel@tonic-gate void 180*7c478bd9Sstevel@tonic-gate pool_value_set_double(pool_value_t *pv, double val) 181*7c478bd9Sstevel@tonic-gate { 182*7c478bd9Sstevel@tonic-gate if (pv->pv_class == POC_STRING) 183*7c478bd9Sstevel@tonic-gate atom_free(pv->pv_u.s); 184*7c478bd9Sstevel@tonic-gate pv->pv_class = POC_DOUBLE; 185*7c478bd9Sstevel@tonic-gate pv->pv_u.d = val; 186*7c478bd9Sstevel@tonic-gate } 187*7c478bd9Sstevel@tonic-gate 188*7c478bd9Sstevel@tonic-gate /* 189*7c478bd9Sstevel@tonic-gate * Set the value's data to the supplied uchar_t data. Update the type 190*7c478bd9Sstevel@tonic-gate * of the value data to POC_BOOL. 191*7c478bd9Sstevel@tonic-gate */ 192*7c478bd9Sstevel@tonic-gate void 193*7c478bd9Sstevel@tonic-gate pool_value_set_bool(pool_value_t *pv, uchar_t val) 194*7c478bd9Sstevel@tonic-gate { 195*7c478bd9Sstevel@tonic-gate if (pv->pv_class == POC_STRING) 196*7c478bd9Sstevel@tonic-gate atom_free(pv->pv_u.s); 197*7c478bd9Sstevel@tonic-gate pv->pv_class = POC_BOOL; 198*7c478bd9Sstevel@tonic-gate pv->pv_u.b = !!val; /* Lock value at 0 or 1 */ 199*7c478bd9Sstevel@tonic-gate } 200*7c478bd9Sstevel@tonic-gate 201*7c478bd9Sstevel@tonic-gate /* 202*7c478bd9Sstevel@tonic-gate * Try to make an internal copy of the val, returning PO_SUCCESS or 203*7c478bd9Sstevel@tonic-gate * PO_FAIL if the copy works or fails. 204*7c478bd9Sstevel@tonic-gate */ 205*7c478bd9Sstevel@tonic-gate int 206*7c478bd9Sstevel@tonic-gate pool_value_set_string(pool_value_t *pv, const char *val) 207*7c478bd9Sstevel@tonic-gate { 208*7c478bd9Sstevel@tonic-gate if (pv->pv_class == POC_STRING) 209*7c478bd9Sstevel@tonic-gate atom_free(pv->pv_u.s); 210*7c478bd9Sstevel@tonic-gate pv->pv_class = POC_STRING; 211*7c478bd9Sstevel@tonic-gate if (val == NULL || strlen(val) >= PV_VALUE_MAX_LEN) { 212*7c478bd9Sstevel@tonic-gate pool_seterror(POE_BADPARAM); 213*7c478bd9Sstevel@tonic-gate return (PO_FAIL); 214*7c478bd9Sstevel@tonic-gate } else { 215*7c478bd9Sstevel@tonic-gate if ((pv->pv_u.s = atom_string(val)) == NULL) 216*7c478bd9Sstevel@tonic-gate return (PO_FAIL); 217*7c478bd9Sstevel@tonic-gate } 218*7c478bd9Sstevel@tonic-gate return (PO_SUCCESS); 219*7c478bd9Sstevel@tonic-gate } 220*7c478bd9Sstevel@tonic-gate 221*7c478bd9Sstevel@tonic-gate /* 222*7c478bd9Sstevel@tonic-gate * Allocate a pool_value_t structure and initialise it to 0. Set the 223*7c478bd9Sstevel@tonic-gate * type to POC_INVAL and return a pointer to the new pool_value_t. If 224*7c478bd9Sstevel@tonic-gate * memory allocation fails, set POE_SYSTEM and return NULL. 225*7c478bd9Sstevel@tonic-gate */ 226*7c478bd9Sstevel@tonic-gate pool_value_t * 227*7c478bd9Sstevel@tonic-gate pool_value_alloc(void) 228*7c478bd9Sstevel@tonic-gate { 229*7c478bd9Sstevel@tonic-gate pool_value_t *val; 230*7c478bd9Sstevel@tonic-gate 231*7c478bd9Sstevel@tonic-gate if ((val = malloc(sizeof (pool_value_t))) == NULL) { 232*7c478bd9Sstevel@tonic-gate pool_seterror(POE_SYSTEM); 233*7c478bd9Sstevel@tonic-gate return (NULL); 234*7c478bd9Sstevel@tonic-gate } 235*7c478bd9Sstevel@tonic-gate (void) memset(val, 0, sizeof (pool_value_t)); 236*7c478bd9Sstevel@tonic-gate val->pv_class = POC_INVAL; 237*7c478bd9Sstevel@tonic-gate return (val); 238*7c478bd9Sstevel@tonic-gate } 239*7c478bd9Sstevel@tonic-gate 240*7c478bd9Sstevel@tonic-gate /* 241*7c478bd9Sstevel@tonic-gate * Free any atoms associated with the value and then free the value 242*7c478bd9Sstevel@tonic-gate * itself. 243*7c478bd9Sstevel@tonic-gate */ 244*7c478bd9Sstevel@tonic-gate void 245*7c478bd9Sstevel@tonic-gate pool_value_free(pool_value_t *pv) 246*7c478bd9Sstevel@tonic-gate { 247*7c478bd9Sstevel@tonic-gate if (pv->pv_name) 248*7c478bd9Sstevel@tonic-gate atom_free(pv->pv_name); 249*7c478bd9Sstevel@tonic-gate if (pv->pv_class == POC_STRING) 250*7c478bd9Sstevel@tonic-gate atom_free(pv->pv_u.s); 251*7c478bd9Sstevel@tonic-gate free(pv); 252*7c478bd9Sstevel@tonic-gate } 253*7c478bd9Sstevel@tonic-gate 254*7c478bd9Sstevel@tonic-gate /* 255*7c478bd9Sstevel@tonic-gate * Return a pointer to the name of the value. This may be NULL if the 256*7c478bd9Sstevel@tonic-gate * name has never been set. 257*7c478bd9Sstevel@tonic-gate */ 258*7c478bd9Sstevel@tonic-gate const char * 259*7c478bd9Sstevel@tonic-gate pool_value_get_name(const pool_value_t *pv) 260*7c478bd9Sstevel@tonic-gate { 261*7c478bd9Sstevel@tonic-gate return (pv->pv_name); 262*7c478bd9Sstevel@tonic-gate } 263*7c478bd9Sstevel@tonic-gate 264*7c478bd9Sstevel@tonic-gate /* 265*7c478bd9Sstevel@tonic-gate * Set the name of the value to the supplied name. 266*7c478bd9Sstevel@tonic-gate */ 267*7c478bd9Sstevel@tonic-gate int 268*7c478bd9Sstevel@tonic-gate pool_value_set_name(pool_value_t *pv, const char *name) 269*7c478bd9Sstevel@tonic-gate { 270*7c478bd9Sstevel@tonic-gate if (name == NULL || strlen(name) >= PV_NAME_MAX_LEN) { 271*7c478bd9Sstevel@tonic-gate pool_seterror(POE_BADPARAM); 272*7c478bd9Sstevel@tonic-gate return (PO_FAIL); 273*7c478bd9Sstevel@tonic-gate } else { 274*7c478bd9Sstevel@tonic-gate if (pv->pv_name) 275*7c478bd9Sstevel@tonic-gate atom_free(pv->pv_name); 276*7c478bd9Sstevel@tonic-gate if ((pv->pv_name = atom_string(name)) == NULL) 277*7c478bd9Sstevel@tonic-gate return (PO_FAIL); 278*7c478bd9Sstevel@tonic-gate } 279*7c478bd9Sstevel@tonic-gate return (PO_SUCCESS); 280*7c478bd9Sstevel@tonic-gate } 281*7c478bd9Sstevel@tonic-gate 282*7c478bd9Sstevel@tonic-gate /* 283*7c478bd9Sstevel@tonic-gate * Use the supplied nvpair_t to set the name, type and value of the 284*7c478bd9Sstevel@tonic-gate * supplied pool_value_t. 285*7c478bd9Sstevel@tonic-gate * 286*7c478bd9Sstevel@tonic-gate * Return: PO_SUCCESS/PO_FAIL 287*7c478bd9Sstevel@tonic-gate */ 288*7c478bd9Sstevel@tonic-gate int 289*7c478bd9Sstevel@tonic-gate pool_value_from_nvpair(pool_value_t *pv, nvpair_t *pn) 290*7c478bd9Sstevel@tonic-gate { 291*7c478bd9Sstevel@tonic-gate uchar_t bval; 292*7c478bd9Sstevel@tonic-gate uint64_t uval; 293*7c478bd9Sstevel@tonic-gate int64_t ival; 294*7c478bd9Sstevel@tonic-gate double dval; 295*7c478bd9Sstevel@tonic-gate uint_t nelem; 296*7c478bd9Sstevel@tonic-gate uchar_t *dval_b; 297*7c478bd9Sstevel@tonic-gate char *sval; 298*7c478bd9Sstevel@tonic-gate 299*7c478bd9Sstevel@tonic-gate if (pool_value_set_name(pv, nvpair_name(pn)) != PO_SUCCESS) 300*7c478bd9Sstevel@tonic-gate return (PO_FAIL); 301*7c478bd9Sstevel@tonic-gate switch (nvpair_type(pn)) { 302*7c478bd9Sstevel@tonic-gate case DATA_TYPE_BYTE: 303*7c478bd9Sstevel@tonic-gate if (nvpair_value_byte(pn, &bval) != 0) { 304*7c478bd9Sstevel@tonic-gate pool_seterror(POE_SYSTEM); 305*7c478bd9Sstevel@tonic-gate return (PO_FAIL); 306*7c478bd9Sstevel@tonic-gate } 307*7c478bd9Sstevel@tonic-gate pool_value_set_bool(pv, bval); 308*7c478bd9Sstevel@tonic-gate break; 309*7c478bd9Sstevel@tonic-gate case DATA_TYPE_BYTE_ARRAY: 310*7c478bd9Sstevel@tonic-gate if (nvpair_value_byte_array(pn, &dval_b, &nelem) != 0) { 311*7c478bd9Sstevel@tonic-gate pool_seterror(POE_SYSTEM); 312*7c478bd9Sstevel@tonic-gate return (PO_FAIL); 313*7c478bd9Sstevel@tonic-gate } 314*7c478bd9Sstevel@tonic-gate (void) memcpy(&dval, dval_b, sizeof (double)); 315*7c478bd9Sstevel@tonic-gate pool_value_set_double(pv, dval); 316*7c478bd9Sstevel@tonic-gate break; 317*7c478bd9Sstevel@tonic-gate case DATA_TYPE_INT64: 318*7c478bd9Sstevel@tonic-gate if (nvpair_value_int64(pn, &ival) != 0) { 319*7c478bd9Sstevel@tonic-gate pool_seterror(POE_SYSTEM); 320*7c478bd9Sstevel@tonic-gate return (PO_FAIL); 321*7c478bd9Sstevel@tonic-gate } 322*7c478bd9Sstevel@tonic-gate pool_value_set_int64(pv, ival); 323*7c478bd9Sstevel@tonic-gate break; 324*7c478bd9Sstevel@tonic-gate case DATA_TYPE_UINT64: 325*7c478bd9Sstevel@tonic-gate if (nvpair_value_uint64(pn, &uval) != 0) { 326*7c478bd9Sstevel@tonic-gate pool_seterror(POE_SYSTEM); 327*7c478bd9Sstevel@tonic-gate return (PO_FAIL); 328*7c478bd9Sstevel@tonic-gate } 329*7c478bd9Sstevel@tonic-gate pool_value_set_uint64(pv, uval); 330*7c478bd9Sstevel@tonic-gate break; 331*7c478bd9Sstevel@tonic-gate case DATA_TYPE_STRING: 332*7c478bd9Sstevel@tonic-gate if (nvpair_value_string(pn, &sval) != 0) { 333*7c478bd9Sstevel@tonic-gate pool_seterror(POE_SYSTEM); 334*7c478bd9Sstevel@tonic-gate return (PO_FAIL); 335*7c478bd9Sstevel@tonic-gate } 336*7c478bd9Sstevel@tonic-gate if (pool_value_set_string(pv, sval) != PO_SUCCESS) 337*7c478bd9Sstevel@tonic-gate return (PO_FAIL); 338*7c478bd9Sstevel@tonic-gate break; 339*7c478bd9Sstevel@tonic-gate default: 340*7c478bd9Sstevel@tonic-gate pool_seterror(POE_SYSTEM); 341*7c478bd9Sstevel@tonic-gate return (PO_FAIL); 342*7c478bd9Sstevel@tonic-gate } 343*7c478bd9Sstevel@tonic-gate return (PO_SUCCESS); 344*7c478bd9Sstevel@tonic-gate } 345*7c478bd9Sstevel@tonic-gate 346*7c478bd9Sstevel@tonic-gate /* 347*7c478bd9Sstevel@tonic-gate * Check to see if the values held by two supplied values are 348*7c478bd9Sstevel@tonic-gate * equal. First compare the pointers to see if we are comparing to 349*7c478bd9Sstevel@tonic-gate * ourselves, if we are return PO_TRUE. If not, get the types and 350*7c478bd9Sstevel@tonic-gate * ensure they match, if they don't return PO_FALSE. Then do a type 351*7c478bd9Sstevel@tonic-gate * specific comparison returning PO_TRUE or PO_FALSE accordingly. 352*7c478bd9Sstevel@tonic-gate */ 353*7c478bd9Sstevel@tonic-gate int 354*7c478bd9Sstevel@tonic-gate pool_value_equal(pool_value_t *pv1, pool_value_t *pv2) 355*7c478bd9Sstevel@tonic-gate { 356*7c478bd9Sstevel@tonic-gate uint64_t uval1, uval2; 357*7c478bd9Sstevel@tonic-gate int64_t ival1, ival2; 358*7c478bd9Sstevel@tonic-gate double dval1, dval2; 359*7c478bd9Sstevel@tonic-gate uchar_t bval1, bval2; 360*7c478bd9Sstevel@tonic-gate const char *sval1, *sval2; 361*7c478bd9Sstevel@tonic-gate pool_value_class_t type; 362*7c478bd9Sstevel@tonic-gate 363*7c478bd9Sstevel@tonic-gate if (pv1 == pv2) /* optimisation */ 364*7c478bd9Sstevel@tonic-gate return (PO_TRUE); 365*7c478bd9Sstevel@tonic-gate 366*7c478bd9Sstevel@tonic-gate type = pool_value_get_type(pv1); 367*7c478bd9Sstevel@tonic-gate if (type != pool_value_get_type(pv2)) 368*7c478bd9Sstevel@tonic-gate return (PO_FALSE); 369*7c478bd9Sstevel@tonic-gate 370*7c478bd9Sstevel@tonic-gate switch (type) { 371*7c478bd9Sstevel@tonic-gate case POC_UINT: 372*7c478bd9Sstevel@tonic-gate (void) pool_value_get_uint64(pv1, &uval1); 373*7c478bd9Sstevel@tonic-gate (void) pool_value_get_uint64(pv2, &uval2); 374*7c478bd9Sstevel@tonic-gate if (uval1 == uval2) 375*7c478bd9Sstevel@tonic-gate return (PO_TRUE); 376*7c478bd9Sstevel@tonic-gate break; 377*7c478bd9Sstevel@tonic-gate case POC_INT: 378*7c478bd9Sstevel@tonic-gate (void) pool_value_get_int64(pv1, &ival1); 379*7c478bd9Sstevel@tonic-gate (void) pool_value_get_int64(pv2, &ival2); 380*7c478bd9Sstevel@tonic-gate if (ival1 == ival2) 381*7c478bd9Sstevel@tonic-gate return (PO_TRUE); 382*7c478bd9Sstevel@tonic-gate break; 383*7c478bd9Sstevel@tonic-gate case POC_DOUBLE: 384*7c478bd9Sstevel@tonic-gate (void) pool_value_get_double(pv1, &dval1); 385*7c478bd9Sstevel@tonic-gate (void) pool_value_get_double(pv2, &dval2); 386*7c478bd9Sstevel@tonic-gate if (dval1 == dval2) 387*7c478bd9Sstevel@tonic-gate return (PO_TRUE); 388*7c478bd9Sstevel@tonic-gate break; 389*7c478bd9Sstevel@tonic-gate case POC_BOOL: 390*7c478bd9Sstevel@tonic-gate (void) pool_value_get_bool(pv1, &bval1); 391*7c478bd9Sstevel@tonic-gate (void) pool_value_get_bool(pv2, &bval2); 392*7c478bd9Sstevel@tonic-gate if (bval1 == bval2) 393*7c478bd9Sstevel@tonic-gate return (PO_TRUE); 394*7c478bd9Sstevel@tonic-gate break; 395*7c478bd9Sstevel@tonic-gate case POC_STRING: 396*7c478bd9Sstevel@tonic-gate (void) pool_value_get_string(pv1, &sval1); 397*7c478bd9Sstevel@tonic-gate (void) pool_value_get_string(pv2, &sval2); 398*7c478bd9Sstevel@tonic-gate if (strcmp(sval1, sval2) == 0) 399*7c478bd9Sstevel@tonic-gate return (PO_TRUE); 400*7c478bd9Sstevel@tonic-gate break; 401*7c478bd9Sstevel@tonic-gate } 402*7c478bd9Sstevel@tonic-gate return (PO_FALSE); 403*7c478bd9Sstevel@tonic-gate } 404*7c478bd9Sstevel@tonic-gate 405*7c478bd9Sstevel@tonic-gate #ifdef DEBUG 406*7c478bd9Sstevel@tonic-gate /* 407*7c478bd9Sstevel@tonic-gate * Trace pool_value_t details using dprintf 408*7c478bd9Sstevel@tonic-gate */ 409*7c478bd9Sstevel@tonic-gate void 410*7c478bd9Sstevel@tonic-gate pool_value_dprintf(const pool_value_t *pv) 411*7c478bd9Sstevel@tonic-gate { 412*7c478bd9Sstevel@tonic-gate const char *class_name[] = { 413*7c478bd9Sstevel@tonic-gate "POC_UINT", 414*7c478bd9Sstevel@tonic-gate "POC_INT", 415*7c478bd9Sstevel@tonic-gate "POC_DOUBLE", 416*7c478bd9Sstevel@tonic-gate "POC_BOOL", 417*7c478bd9Sstevel@tonic-gate "POC_STRING" 418*7c478bd9Sstevel@tonic-gate }; 419*7c478bd9Sstevel@tonic-gate 420*7c478bd9Sstevel@tonic-gate dprintf("name: %s\n", pv->pv_name ? pv->pv_name : "NULL"); 421*7c478bd9Sstevel@tonic-gate if (pv->pv_class >= POC_UINT && pv->pv_class <= POC_STRING) 422*7c478bd9Sstevel@tonic-gate dprintf("type: %s\n", class_name[pv->pv_class]); 423*7c478bd9Sstevel@tonic-gate else 424*7c478bd9Sstevel@tonic-gate dprintf("type: POC_INVAL\n"); 425*7c478bd9Sstevel@tonic-gate switch (pv->pv_class) { 426*7c478bd9Sstevel@tonic-gate case POC_UINT: 427*7c478bd9Sstevel@tonic-gate dprintf("value: %llu\n", pv->pv_u.u); 428*7c478bd9Sstevel@tonic-gate break; 429*7c478bd9Sstevel@tonic-gate case POC_INT: 430*7c478bd9Sstevel@tonic-gate dprintf("value: %lld\n", pv->pv_u.i); 431*7c478bd9Sstevel@tonic-gate break; 432*7c478bd9Sstevel@tonic-gate case POC_DOUBLE: 433*7c478bd9Sstevel@tonic-gate dprintf("value: %f\n", pv->pv_u.d); 434*7c478bd9Sstevel@tonic-gate break; 435*7c478bd9Sstevel@tonic-gate case POC_BOOL: 436*7c478bd9Sstevel@tonic-gate dprintf("value: %s\n", pv->pv_u.b ? "true" : "false"); 437*7c478bd9Sstevel@tonic-gate break; 438*7c478bd9Sstevel@tonic-gate case POC_STRING: 439*7c478bd9Sstevel@tonic-gate dprintf("value: %s\n", pv->pv_u.s); 440*7c478bd9Sstevel@tonic-gate break; 441*7c478bd9Sstevel@tonic-gate default: 442*7c478bd9Sstevel@tonic-gate dprintf("value: invalid\n"); 443*7c478bd9Sstevel@tonic-gate break; 444*7c478bd9Sstevel@tonic-gate } 445*7c478bd9Sstevel@tonic-gate } 446*7c478bd9Sstevel@tonic-gate #endif /* DEBUG */ 447